0000: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
0010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0020: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0040: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
0050: 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 ** This file is
0060: 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 an amalgamation
0070: 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74 65 of many separate
0080: 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20 C source files
0090: 66 72 6f 6d 20 53 51 4c 69 74 65 0a 2a 2a 20 76 from SQLite.** v
00a0: 65 72 73 69 6f 6e 20 33 2e 36 2e 31 38 2e 20 20 ersion 3.6.18.
00b0: 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 6c 6c By combining all
00c0: 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 the individual
00d0: 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69 6e 74 C code files int
00e0: 6f 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e 67 6c o this .** singl
00f0: 65 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 74 68 e large file, th
0100: 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20 63 61 e entire code ca
0110: 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 61 73 n be compiled as
0120: 20 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 74 69 a one translati
0130: 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 68 69 on.** unit. Thi
0140: 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 63 6f s allows many co
0150: 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 6f 70 mpilers to do op
0160: 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 61 74 timizations that
0170: 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a would not be.**
0180: 20 70 6f 73 73 69 62 6c 65 20 69 66 20 74 68 65 possible if the
0190: 20 66 69 6c 65 73 20 77 65 72 65 20 63 6f 6d 70 files were comp
01a0: 69 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e iled separately.
01b0: 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d Performance im
01c0: 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 6f 66 provements.** of
01d0: 20 35 25 20 61 72 65 20 6d 6f 72 65 20 61 72 65 5% are more are
01e0: 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e 20 77 commonly seen w
01f0: 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f hen SQLite is co
0200: 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69 6e 67 mpiled as a sing
0210: 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f le.** translatio
0220: 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 n unit..**.** Th
0230: 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c 20 79 is file is all y
0240: 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 69 ou need to compi
0250: 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f 20 75 le SQLite. To u
0260: 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f 74 68 se SQLite in oth
0270: 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 2c 20 er.** programs,
0280: 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20 66 69 you need this fi
0290: 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71 6c 69 le and the "sqli
02a0: 74 65 33 2e 68 22 20 68 65 61 64 65 72 20 66 69 te3.h" header fi
02b0: 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 0a le that defines.
02c0: 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 69 ** the programmi
02d0: 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 ng interface to
02e0: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 the SQLite libra
02f0: 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64 6f 20 ry. (If you do
0300: 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 68 65 not have .** the
0310: 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68 65 61 "sqlite3.h" hea
0320: 64 65 72 20 66 69 6c 65 20 61 74 20 68 61 6e 64 der file at hand
0330: 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64 20 , you will find
0340: 61 20 63 6f 70 79 20 65 6d 62 65 64 64 65 64 20 a copy embedded
0350: 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 74 65 within.** the te
0360: 78 74 20 6f 66 20 74 68 69 73 20 66 69 6c 65 2e xt of this file.
0370: 20 20 53 65 61 72 63 68 20 66 6f 72 20 22 42 65 Search for "Be
0380: 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 33 gin file sqlite3
0390: 2e 68 22 20 74 6f 20 66 69 6e 64 20 74 68 65 20 .h" to find the
03a0: 73 74 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20 start.** of the
03b0: 65 6d 62 65 64 64 65 64 20 73 71 6c 69 74 65 33 embedded sqlite3
03c0: 2e 68 20 68 65 61 64 65 72 20 66 69 6c 65 2e 29 .h header file.)
03d0: 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65 Additional code
03e0: 20 66 69 6c 65 73 20 6d 61 79 20 62 65 20 6e 65 files may be ne
03f0: 65 64 65 64 0a 2a 2a 20 69 66 20 79 6f 75 20 77 eded.** if you w
0400: 61 6e 74 20 61 20 77 72 61 70 70 65 72 20 74 6f ant a wrapper to
0410: 20 69 6e 74 65 72 66 61 63 65 20 53 51 4c 69 74 interface SQLit
0420: 65 20 77 69 74 68 20 79 6f 75 72 20 63 68 6f 69 e with your choi
0430: 63 65 20 6f 66 20 70 72 6f 67 72 61 6d 6d 69 6e ce of programmin
0440: 67 0a 2a 2a 20 6c 61 6e 67 75 61 67 65 2e 20 54 g.** language. T
0450: 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 he code for the
0460: 22 73 71 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e "sqlite3" comman
0470: 64 2d 6c 69 6e 65 20 73 68 65 6c 6c 20 69 73 20 d-line shell is
0480: 61 6c 73 6f 20 69 6e 20 61 0a 2a 2a 20 73 65 70 also in a.** sep
0490: 61 72 61 74 65 20 66 69 6c 65 2e 20 54 68 69 73 arate file. This
04a0: 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f file contains o
04b0: 6e 6c 79 20 63 6f 64 65 20 66 6f 72 20 74 68 65 nly code for the
04c0: 20 63 6f 72 65 20 53 51 4c 69 74 65 20 6c 69 62 core SQLite lib
04d0: 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rary..**.** This
04e0: 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 77 61 amalgamation wa
04f0: 73 20 67 65 6e 65 72 61 74 65 64 20 6f 6e 20 32 s generated on 2
0500: 30 30 39 2d 30 39 2d 30 39 20 31 36 3a 31 31 3a 009-09-09 16:11:
0510: 30 36 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 66 69 06 UTC..*/.#defi
0520: 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 31 ne SQLITE_CORE 1
0530: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
0540: 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 31 0a 23 AMALGAMATION 1.#
0550: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52 ifndef SQLITE_PR
0560: 49 56 41 54 45 0a 23 20 64 65 66 69 6e 65 20 53 IVATE.# define S
0570: 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 QLITE_PRIVATE st
0580: 61 74 69 63 0a 23 65 6e 64 69 66 0a 23 69 66 6e atic.#endif.#ifn
0590: 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23 def SQLITE_API.#
05a0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 define SQLITE_A
05b0: 50 49 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a PI.#endif./*****
05c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
05d0: 66 69 6c 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 file sqliteInt.h
05e0: 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
05f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0600: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
0610: 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
0620: 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
0630: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
0640: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
0650: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
0660: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
0670: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
0680: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
0690: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
06a0: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
06b0: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
06c0: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
06d0: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
06e0: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
06f0: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
0700: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
0710: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
0720: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
0730: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
0740: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0750: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0760: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0770: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0780: 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 69 6e 74 .** Internal int
0790: 65 72 66 61 63 65 20 64 65 66 69 6e 69 74 69 6f erface definitio
07a0: 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a ns for SQLite..*
07b0: 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 *.*/.#ifndef _SQ
07c0: 4c 49 54 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69 LITEINT_H_.#defi
07d0: 6e 65 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 5f ne _SQLITEINT_H_
07e0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 ../*.** These #d
07f0: 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e efines should en
0800: 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 able >2GB file s
0810: 75 70 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20 upport on POSIX
0820: 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c if the.** underl
0830: 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 ying operating s
0840: 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 ystem supports i
0850: 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 t. If the OS la
0860: 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c cks.** large fil
0870: 65 20 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66 e support, or if
0880: 20 74 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f the OS is windo
0890: 77 73 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 ws, these should
08a0: 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a be no-ops..**.*
08b0: 2a 20 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20 * Ticket #2739:
08c0: 20 54 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f The _LARGEFILE_
08d0: 53 4f 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73 SOURCE macro mus
08e0: 74 20 61 70 70 65 61 72 20 62 65 66 6f 72 65 20 t appear before
08f0: 61 6e 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69 any.** system #i
0900: 6e 63 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c ncludes. Hence,
0910: 20 74 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 this block of c
0920: 6f 64 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 ode must be the
0930: 76 65 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f very first.** co
0940: 64 65 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65 de in all source
0950: 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 files..**.** La
0960: 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 rge file support
0970: 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 can be disabled
0980: 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c using the -DSQL
0990: 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 ITE_DISABLE_LFS
09a0: 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 switch.** on the
09b0: 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e compiler comman
09c0: 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 d line. This is
09d0: 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f necessary if yo
09e0: 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a u are compiling.
09f0: 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d ** on a recent m
0a00: 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20 achine (ex: Red
0a10: 48 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 Hat 7.2) but you
0a20: 20 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 want your code
0a30: 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e to work.** on an
0a40: 20 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 older machine (
0a50: 65 78 3a 20 52 65 64 20 48 61 74 20 36 2e 30 29 ex: Red Hat 6.0)
0a60: 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c . If you compil
0a70: 65 20 6f 6e 20 52 65 64 20 48 61 74 20 37 2e 32 e on Red Hat 7.2
0a80: 0a 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 .** without this
0a90: 20 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 option, LFS is
0aa0: 65 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 enable. But LFS
0ab0: 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 does not exist
0ac0: 69 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a in the kernel.**
0ad0: 20 69 6e 20 52 65 64 20 48 61 74 20 36 2e 30 2c in Red Hat 6.0,
0ae0: 20 73 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e so the code won
0af0: 27 74 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 't work. Hence,
0b00: 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e for maximum bin
0b10: 61 72 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 ary.** portabili
0b20: 74 79 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d ty you should om
0b30: 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69 it LFS..**.** Si
0b40: 6d 69 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f milar is true fo
0b50: 72 20 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53 r Mac OS X. LFS
0b60: 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 is only support
0b70: 65 64 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39 ed on Mac OS X 9
0b80: 20 61 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23 and later..*/.#
0b90: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 ifndef SQLITE_DI
0ba0: 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 SABLE_LFS.# defi
0bb0: 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 ne _LARGE_FILE
0bc0: 20 20 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 1.# ifndef
0bd0: 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 _FILE_OFFSET_BIT
0be0: 53 0a 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 S.# define _FI
0bf0: 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 LE_OFFSET_BITS 6
0c00: 34 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 4.# endif.# defi
0c10: 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f ne _LARGEFILE_SO
0c20: 55 52 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f URCE 1.#endif../
0c30: 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 74 68 65 *.** Include the
0c40: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 68 configuration h
0c50: 65 61 64 65 72 20 6f 75 74 70 75 74 20 62 79 20 eader output by
0c60: 27 63 6f 6e 66 69 67 75 72 65 27 20 69 66 20 77 'configure' if w
0c70: 65 27 72 65 20 75 73 69 6e 67 20 74 68 65 0a 2a e're using the.*
0c80: 2a 20 61 75 74 6f 63 6f 6e 66 2d 62 61 73 65 64 * autoconf-based
0c90: 20 62 75 69 6c 64 0a 2a 2f 0a 23 69 66 64 65 66 build.*/.#ifdef
0ca0: 20 5f 48 41 56 45 5f 53 51 4c 49 54 45 5f 43 4f _HAVE_SQLITE_CO
0cb0: 4e 46 49 47 5f 48 0a 23 69 6e 63 6c 75 64 65 20 NFIG_H.#include
0cc0: 22 63 6f 6e 66 69 67 2e 68 22 0a 23 65 6e 64 69 "config.h".#endi
0cd0: 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f../************
0ce0: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 ** Include sqlit
0cf0: 65 4c 69 6d 69 74 2e 68 20 69 6e 20 74 68 65 20 eLimit.h in the
0d00: 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 middle of sqlite
0d10: 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
0d20: 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
0d30: 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 ** Begin file sq
0d40: 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a liteLimit.h ****
0d50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0d60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0d70: 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 */./*.** 2007 Ma
0d80: 79 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 y 7.**.** The au
0d90: 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
0da0: 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
0db0: 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
0dc0: 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
0dd0: 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
0de0: 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
0df0: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
0e00: 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
0e10: 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
0e20: 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
0e30: 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
0e40: 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
0e50: 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
0e60: 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
0e70: 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
0e80: 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
0e90: 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
0ea0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0eb0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0ec0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0ed0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0ee0: 2a 2a 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 66 **.** .** This f
0ef0: 69 6c 65 20 64 65 66 69 6e 65 73 20 76 61 72 69 ile defines vari
0f00: 6f 75 73 20 6c 69 6d 69 74 73 20 6f 66 20 77 68 ous limits of wh
0f10: 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 70 72 at SQLite can pr
0f20: 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 ocess..**.** @(#
0f30: 29 20 24 49 64 3a 20 73 71 6c 69 74 65 4c 69 6d ) $Id: sqliteLim
0f40: 69 74 2e 68 2c 76 20 31 2e 31 30 20 32 30 30 39 it.h,v 1.10 2009
0f50: 2f 30 31 2f 31 30 20 31 36 3a 31 35 3a 30 39 20 /01/10 16:15:09
0f60: 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
0f70: 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 $.*/../*.** The
0f80: 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f maximum length o
0f90: 66 20 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 f a TEXT or BLOB
0fa0: 20 69 6e 20 62 79 74 65 73 2e 20 20 20 54 68 69 in bytes. Thi
0fb0: 73 20 61 6c 73 6f 0a 2a 2a 20 6c 69 6d 69 74 73 s also.** limits
0fc0: 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 72 the size of a r
0fd0: 6f 77 20 69 6e 20 61 20 74 61 62 6c 65 20 6f 72 ow in a table or
0fe0: 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 index..**.** Th
0ff0: 65 20 68 61 72 64 20 6c 69 6d 69 74 20 69 73 20 e hard limit is
1000: 74 68 65 20 61 62 69 6c 69 74 79 20 6f 66 20 61 the ability of a
1010: 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 32-bit signed i
1020: 6e 74 65 67 65 72 0a 2a 2a 20 74 6f 20 63 6f 75 nteger.** to cou
1030: 6e 74 20 74 68 65 20 73 69 7a 65 3a 20 32 5e 33 nt the size: 2^3
1040: 31 2d 31 20 6f 72 20 32 31 34 37 34 38 33 36 34 1-1 or 214748364
1050: 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 7..*/.#ifndef SQ
1060: 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a LITE_MAX_LENGTH.
1070: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
1080: 4d 41 58 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 MAX_LENGTH 10000
1090: 30 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 00000.#endif../*
10a0: 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 .** This is the
10b0: 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
10c0: 66 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c f.**.** * Col
10d0: 75 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 0a umns in a table.
10e0: 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 ** * Columns
10f0: 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 20 in an index.**
1100: 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 * Columns in a
1110: 20 76 69 65 77 0a 2a 2a 20 20 20 20 2a 20 54 65 view.** * Te
1120: 72 6d 73 20 69 6e 20 74 68 65 20 53 45 54 20 63 rms in the SET c
1130: 6c 61 75 73 65 20 6f 66 20 61 6e 20 55 50 44 41 lause of an UPDA
1140: 54 45 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 TE statement.**
1150: 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 * Terms in th
1160: 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 e result set of
1170: 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 a SELECT stateme
1180: 6e 74 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 nt.** * Terms
1190: 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59 in the GROUP BY
11a0: 20 6f 72 20 4f 52 44 45 52 20 42 59 20 63 6c 61 or ORDER BY cla
11b0: 75 73 65 73 20 6f 66 20 61 20 53 45 4c 45 43 54 uses of a SELECT
11c0: 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 20 statement..**
11d0: 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 * Terms in the
11e0: 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 6f VALUES clause o
11f0: 66 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 f an INSERT stat
1200: 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ement.**.** The
1210: 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69 74 hard upper limit
1220: 20 68 65 72 65 20 69 73 20 33 32 36 37 36 2e 20 here is 32676.
1230: 20 4d 6f 73 74 20 64 61 74 61 62 61 73 65 20 70 Most database p
1240: 65 6f 70 6c 65 20 77 69 6c 6c 0a 2a 2a 20 74 65 eople will.** te
1250: 6c 6c 20 79 6f 75 20 74 68 61 74 20 69 6e 20 61 ll you that in a
1260: 20 77 65 6c 6c 2d 6e 6f 72 6d 61 6c 69 7a 65 64 well-normalized
1270: 20 64 61 74 61 62 61 73 65 2c 20 79 6f 75 20 75 database, you u
1280: 73 75 61 6c 6c 79 20 73 68 6f 75 6c 64 0a 2a 2a sually should.**
1290: 20 6e 6f 74 20 68 61 76 65 20 6d 6f 72 65 20 74 not have more t
12a0: 68 61 6e 20 61 20 64 6f 7a 65 6e 20 6f 72 20 73 han a dozen or s
12b0: 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 79 o columns in any
12c0: 20 74 61 62 6c 65 2e 20 20 41 6e 64 20 69 66 0a table. And if.
12d0: 2a 2a 20 74 68 61 74 20 69 73 20 74 68 65 20 63 ** that is the c
12e0: 61 73 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f ase, there is no
12f0: 20 70 6f 69 6e 74 20 69 6e 20 68 61 76 69 6e 67 point in having
1300: 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 66 65 77 more than a few
1310: 0a 2a 2a 20 64 6f 7a 65 6e 20 76 61 6c 75 65 73 .** dozen values
1320: 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f in any of the o
1330: 74 68 65 72 20 73 69 74 75 61 74 69 6f 6e 73 20 ther situations
1340: 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e described above.
1350: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
1360: 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 23 20 TE_MAX_COLUMN.#
1370: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
1380: 58 5f 43 4f 4c 55 4d 4e 20 32 30 30 30 0a 23 65 X_COLUMN 2000.#e
1390: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
13a0: 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f maximum length o
13b0: 66 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 f a single SQL s
13c0: 74 61 74 65 6d 65 6e 74 20 69 6e 20 62 79 74 65 tatement in byte
13d0: 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 75 73 65 64 s..**.** It used
13e0: 20 74 6f 20 62 65 20 74 68 65 20 63 61 73 65 20 to be the case
13f0: 74 68 61 74 20 73 65 74 74 69 6e 67 20 74 68 69 that setting thi
1400: 73 20 76 61 6c 75 65 20 74 6f 20 7a 65 72 6f 20 s value to zero
1410: 77 6f 75 6c 64 0a 2a 2a 20 74 75 72 6e 20 74 68 would.** turn th
1420: 65 20 6c 69 6d 69 74 20 6f 66 66 2e 20 20 54 68 e limit off. Th
1430: 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 at is no longer
1440: 74 72 75 65 2e 20 20 49 74 20 69 73 20 6e 6f 74 true. It is not
1450: 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 possible.** to
1460: 74 75 72 6e 20 74 68 69 73 20 6c 69 6d 69 74 20 turn this limit
1470: 6f 66 66 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 off..*/.#ifndef
1480: 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c SQLITE_MAX_SQL_L
1490: 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 ENGTH.# define S
14a0: 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 QLITE_MAX_SQL_LE
14b0: 4e 47 54 48 20 31 30 30 30 30 30 30 30 30 30 0a NGTH 1000000000.
14c0: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
14d0: 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 e maximum depth
14e0: 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e of an expression
14f0: 20 74 72 65 65 2e 20 54 68 69 73 20 69 73 20 6c tree. This is l
1500: 69 6d 69 74 65 64 20 74 6f 20 0a 2a 2a 20 73 6f imited to .** so
1510: 6d 65 20 65 78 74 65 6e 74 20 62 79 20 53 51 4c me extent by SQL
1520: 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 ITE_MAX_SQL_LENG
1530: 54 48 2e 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 TH. But sometime
1540: 20 79 6f 75 20 6d 69 67 68 74 20 0a 2a 2a 20 77 you might .** w
1550: 61 6e 74 20 74 6f 20 70 6c 61 63 65 20 6d 6f 72 ant to place mor
1560: 65 20 73 65 76 65 72 65 20 6c 69 6d 69 74 73 20 e severe limits
1570: 6f 6e 20 74 68 65 20 63 6f 6d 70 6c 65 78 69 74 on the complexit
1580: 79 20 6f 66 20 61 6e 20 0a 2a 2a 20 65 78 70 72 y of an .** expr
1590: 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 ession..**.** A
15a0: 76 61 6c 75 65 20 6f 66 20 30 20 75 73 65 64 20 value of 0 used
15b0: 74 6f 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 to mean that the
15c0: 20 6c 69 6d 69 74 20 77 61 73 20 6e 6f 74 20 65 limit was not e
15d0: 6e 66 6f 72 63 65 64 2e 0a 2a 2a 20 42 75 74 20 nforced..** But
15e0: 74 68 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 that is no longe
15f0: 72 20 74 72 75 65 2e 20 20 54 68 65 20 6c 69 6d r true. The lim
1600: 69 74 20 69 73 20 6e 6f 77 20 73 74 72 69 63 74 it is now strict
1610: 6c 79 20 65 6e 66 6f 72 63 65 64 0a 2a 2a 20 61 ly enforced.** a
1620: 74 20 61 6c 6c 20 74 69 6d 65 73 2e 0a 2a 2f 0a t all times..*/.
1630: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
1640: 41 58 5f 45 58 50 52 5f 44 45 50 54 48 0a 23 20 AX_EXPR_DEPTH.#
1650: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
1660: 58 5f 45 58 50 52 5f 44 45 50 54 48 20 31 30 30 X_EXPR_DEPTH 100
1670: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
1680: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 The maximum numb
1690: 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 er of terms in a
16a0: 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 compound SELECT
16b0: 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 statement..** T
16c0: 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f he code generato
16d0: 72 20 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 20 53 r for compound S
16e0: 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 ELECT statements
16f0: 20 64 6f 65 73 20 6f 6e 65 0a 2a 2a 20 6c 65 76 does one.** lev
1700: 65 6c 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 el of recursion
1710: 66 6f 72 20 65 61 63 68 20 74 65 72 6d 2e 20 20 for each term.
1720: 41 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 A stack overflow
1730: 20 63 61 6e 20 72 65 73 75 6c 74 0a 2a 2a 20 69 can result.** i
1740: 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
1750: 74 65 72 6d 73 20 69 73 20 74 6f 6f 20 6c 61 72 terms is too lar
1760: 67 65 2e 20 20 49 6e 20 70 72 61 63 74 69 63 65 ge. In practice
1770: 2c 20 6d 6f 73 74 20 53 51 4c 0a 2a 2a 20 6e 65 , most SQL.** ne
1780: 76 65 72 20 68 61 73 20 6d 6f 72 65 20 74 68 61 ver has more tha
1790: 6e 20 33 20 6f 72 20 34 20 74 65 72 6d 73 2e 20 n 3 or 4 terms.
17a0: 20 55 73 65 20 61 20 76 61 6c 75 65 20 6f 66 20 Use a value of
17b0: 30 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 0 to disable.**
17c0: 61 6e 79 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 any limit on the
17d0: 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 number of terms
17e0: 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 74 20 53 in a compount S
17f0: 45 4c 45 43 54 2e 0a 2a 2f 0a 23 69 66 6e 64 65 ELECT..*/.#ifnde
1800: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d f SQLITE_MAX_COM
1810: 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 23 20 64 POUND_SELECT.# d
1820: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 efine SQLITE_MAX
1830: 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 _COMPOUND_SELECT
1840: 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 500.#endif../*.
1850: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e ** The maximum n
1860: 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73 umber of opcodes
1870: 20 69 6e 20 61 20 56 44 42 45 20 70 72 6f 67 72 in a VDBE progr
1880: 61 6d 2e 0a 2a 2a 20 4e 6f 74 20 63 75 72 72 65 am..** Not curre
1890: 6e 74 6c 79 20 65 6e 66 6f 72 63 65 64 2e 0a 2a ntly enforced..*
18a0: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
18b0: 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 0a 23 20 64 _MAX_VDBE_OP.# d
18c0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 efine SQLITE_MAX
18d0: 5f 56 44 42 45 5f 4f 50 20 32 35 30 30 30 0a 23 _VDBE_OP 25000.#
18e0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
18f0: 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
1900: 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 of arguments to
1910: 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e an SQL function.
1920: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
1930: 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f TE_MAX_FUNCTION_
1940: 41 52 47 0a 23 20 64 65 66 69 6e 65 20 53 51 4c ARG.# define SQL
1950: 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e ITE_MAX_FUNCTION
1960: 5f 41 52 47 20 31 32 37 0a 23 65 6e 64 69 66 0a _ARG 127.#endif.
1970: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d ./*.** The maxim
1980: 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d um number of in-
1990: 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 74 6f 20 memory pages to
19a0: 75 73 65 20 66 6f 72 20 74 68 65 20 6d 61 69 6e use for the main
19b0: 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 database.** tab
19c0: 6c 65 20 61 6e 64 20 66 6f 72 20 74 65 6d 70 6f le and for tempo
19d0: 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 54 68 rary tables. Th
19e0: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
19f0: 5f 43 41 43 48 45 5f 53 49 5a 45 0a 2a 2f 0a 23 _CACHE_SIZE.*/.#
1a00: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 ifndef SQLITE_DE
1a10: 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 FAULT_CACHE_SIZE
1a20: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
1a30: 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 _DEFAULT_CACHE_S
1a40: 49 5a 45 20 20 32 30 30 30 0a 23 65 6e 64 69 66 IZE 2000.#endif
1a50: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1a60: 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 DEFAULT_TEMP_CAC
1a70: 48 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 HE_SIZE.# define
1a80: 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
1a90: 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 20 TEMP_CACHE_SIZE
1aa0: 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 500.#endif../*.
1ab0: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e ** The maximum n
1ac0: 75 6d 62 65 72 20 6f 66 20 61 74 74 61 63 68 65 umber of attache
1ad0: 64 20 64 61 74 61 62 61 73 65 73 2e 20 20 54 68 d databases. Th
1ae0: 69 73 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 is must be betwe
1af0: 65 6e 20 30 0a 2a 2a 20 61 6e 64 20 33 30 2e 20 en 0.** and 30.
1b00: 20 54 68 65 20 75 70 70 65 72 20 62 6f 75 6e 64 The upper bound
1b10: 20 6f 6e 20 33 30 20 69 73 20 62 65 63 61 75 73 on 30 is becaus
1b20: 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 e a 32-bit integ
1b30: 65 72 20 62 69 74 6d 61 70 0a 2a 2a 20 69 73 20 er bitmap.** is
1b40: 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 used internally
1b50: 74 6f 20 74 72 61 63 6b 20 61 74 74 61 63 68 65 to track attache
1b60: 64 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2f 0a d databases..*/.
1b70: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
1b80: 41 58 5f 41 54 54 41 43 48 45 44 0a 23 20 64 65 AX_ATTACHED.# de
1b90: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
1ba0: 41 54 54 41 43 48 45 44 20 31 30 0a 23 65 6e 64 ATTACHED 10.#end
1bb0: 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d if.../*.** The m
1bc0: 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 aximum value of
1bd0: 61 20 3f 6e 6e 6e 20 77 69 6c 64 63 61 72 64 20 a ?nnn wildcard
1be0: 74 68 61 74 20 74 68 65 20 70 61 72 73 65 72 20 that the parser
1bf0: 77 69 6c 6c 20 61 63 63 65 70 74 2e 0a 2a 2f 0a will accept..*/.
1c00: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
1c10: 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 AX_VARIABLE_NUMB
1c20: 45 52 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 ER.# define SQLI
1c30: 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f TE_MAX_VARIABLE_
1c40: 4e 55 4d 42 45 52 20 39 39 39 0a 23 65 6e 64 69 NUMBER 999.#endi
1c50: 66 0a 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 70 61 f../* Maximum pa
1c60: 67 65 20 73 69 7a 65 2e 20 20 54 68 65 20 75 70 ge size. The up
1c70: 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 69 per bound on thi
1c80: 73 20 76 61 6c 75 65 20 69 73 20 33 32 37 36 38 s value is 32768
1c90: 2e 20 20 54 68 69 73 20 61 20 6c 69 6d 69 74 0a . This a limit.
1ca0: 2a 2a 20 69 6d 70 6f 73 65 64 20 62 79 20 74 68 ** imposed by th
1cb0: 65 20 6e 65 63 65 73 73 69 74 79 20 6f 66 20 73 e necessity of s
1cc0: 74 6f 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65 toring the value
1cd0: 20 69 6e 20 61 20 32 2d 62 79 74 65 20 75 6e 73 in a 2-byte uns
1ce0: 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a igned integer.**
1cf0: 20 61 6e 64 20 74 68 65 20 66 61 63 74 20 74 68 and the fact th
1d00: 61 74 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 at the page size
1d10: 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 must be a power
1d20: 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 of 2..**.** If
1d30: 74 68 69 73 20 6c 69 6d 69 74 20 69 73 20 63 68 this limit is ch
1d40: 61 6e 67 65 64 2c 20 74 68 65 6e 20 74 68 65 20 anged, then the
1d50: 63 6f 6d 70 69 6c 65 64 20 6c 69 62 72 61 72 79 compiled library
1d60: 20 69 73 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a is technically.
1d70: 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 ** incompatible
1d80: 77 69 74 68 20 61 6e 20 53 51 4c 69 74 65 20 6c with an SQLite l
1d90: 69 62 72 61 72 79 20 63 6f 6d 70 69 6c 65 64 20 ibrary compiled
1da0: 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 with a different
1db0: 20 6c 69 6d 69 74 2e 20 49 66 0a 2a 2a 20 61 20 limit. If.** a
1dc0: 70 72 6f 63 65 73 73 20 6f 70 65 72 61 74 69 6e process operatin
1dd0: 67 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 g on a database
1de0: 77 69 74 68 20 61 20 70 61 67 65 2d 73 69 7a 65 with a page-size
1df0: 20 6f 66 20 36 35 35 33 36 20 62 79 74 65 73 20 of 65536 bytes
1e00: 0a 2a 2a 20 63 72 61 73 68 65 73 2c 20 74 68 65 .** crashes, the
1e10: 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 n an instance of
1e20: 20 53 51 4c 69 74 65 20 63 6f 6d 70 69 6c 65 64 SQLite compiled
1e30: 20 77 69 74 68 20 74 68 65 20 64 65 66 61 75 6c with the defaul
1e40: 74 20 70 61 67 65 2d 73 69 7a 65 20 0a 2a 2a 20 t page-size .**
1e50: 6c 69 6d 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 limit will not b
1e60: 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 e able to rollba
1e70: 63 6b 20 74 68 65 20 61 62 6f 72 74 65 64 20 74 ck the aborted t
1e80: 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 ransaction. This
1e90: 20 63 6f 75 6c 64 0a 2a 2a 20 6c 65 61 64 20 74 could.** lead t
1ea0: 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 o database corru
1eb0: 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 ption..*/.#ifnde
1ec0: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 f SQLITE_MAX_PAG
1ed0: 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 E_SIZE.# define
1ee0: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
1ef0: 53 49 5a 45 20 33 32 37 36 38 0a 23 65 6e 64 69 SIZE 32768.#endi
1f00: 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 f.../*.** The de
1f10: 66 61 75 6c 74 20 73 69 7a 65 20 6f 66 20 61 20 fault size of a
1f20: 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 0a 2a database page..*
1f30: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1f40: 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 _DEFAULT_PAGE_SI
1f50: 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 ZE.# define SQLI
1f60: 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f TE_DEFAULT_PAGE_
1f70: 53 49 5a 45 20 31 30 32 34 0a 23 65 6e 64 69 66 SIZE 1024.#endif
1f80: 0a 23 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41 .#if SQLITE_DEFA
1f90: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51 ULT_PAGE_SIZE>SQ
1fa0: 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 LITE_MAX_PAGE_SI
1fb0: 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 ZE.# undef SQLIT
1fc0: 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 E_DEFAULT_PAGE_S
1fd0: 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c IZE.# define SQL
1fe0: 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 ITE_DEFAULT_PAGE
1ff0: 5f 53 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58 _SIZE SQLITE_MAX
2000: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69 _PAGE_SIZE.#endi
2010: 66 0a 0a 2f 2a 0a 2a 2a 20 4f 72 64 69 6e 61 72 f../*.** Ordinar
2020: 69 6c 79 2c 20 69 66 20 6e 6f 20 76 61 6c 75 65 ily, if no value
2030: 20 69 73 20 65 78 70 6c 69 63 69 74 6c 79 20 70 is explicitly p
2040: 72 6f 76 69 64 65 64 2c 20 53 51 4c 69 74 65 20 rovided, SQLite
2050: 63 72 65 61 74 65 73 20 64 61 74 61 62 61 73 65 creates database
2060: 73 0a 2a 2a 20 77 69 74 68 20 70 61 67 65 20 73 s.** with page s
2070: 69 7a 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ize SQLITE_DEFAU
2080: 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2e 20 48 6f LT_PAGE_SIZE. Ho
2090: 77 65 76 65 72 2c 20 62 61 73 65 64 20 6f 6e 20 wever, based on
20a0: 63 65 72 74 61 69 6e 0a 2a 2a 20 64 65 76 69 63 certain.** devic
20b0: 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 e characteristic
20c0: 73 20 28 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 s (sector-size a
20d0: 6e 64 20 61 74 6f 6d 69 63 20 77 72 69 74 65 28 nd atomic write(
20e0: 29 20 73 75 70 70 6f 72 74 29 2c 0a 2a 2a 20 53 ) support),.** S
20f0: 51 4c 69 74 65 20 6d 61 79 20 63 68 6f 6f 73 65 QLite may choose
2100: 20 61 20 6c 61 72 67 65 72 20 76 61 6c 75 65 2e a larger value.
2110: 20 54 68 69 73 20 63 6f 6e 73 74 61 6e 74 20 69 This constant i
2120: 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 s the maximum va
2130: 6c 75 65 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 lue.** SQLite wi
2140: 6c 6c 20 63 68 6f 6f 73 65 20 6f 6e 20 69 74 73 ll choose on its
2150: 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 own..*/.#ifndef
2160: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 SQLITE_MAX_DEFA
2170: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 ULT_PAGE_SIZE.#
2180: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
2190: 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 X_DEFAULT_PAGE_S
21a0: 49 5a 45 20 38 31 39 32 0a 23 65 6e 64 69 66 0a IZE 8192.#endif.
21b0: 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 #if SQLITE_MAX_D
21c0: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
21d0: 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 >SQLITE_MAX_PAGE
21e0: 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 _SIZE.# undef SQ
21f0: 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 LITE_MAX_DEFAULT
2200: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 _PAGE_SIZE.# def
2210: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 ine SQLITE_MAX_D
2220: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
2230: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 SQLITE_MAX_PAGE
2240: 5f 53 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 0a 2f _SIZE.#endif.../
2250: 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d *.** Maximum num
2260: 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
2270: 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c one database fil
2280: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 e..**.** This is
2290: 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 74 68 65 really just the
22a0: 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 default value f
22b0: 6f 72 20 74 68 65 20 6d 61 78 5f 70 61 67 65 5f or the max_page_
22c0: 63 6f 75 6e 74 20 70 72 61 67 6d 61 2e 0a 2a 2a count pragma..**
22d0: 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e 20 This value can
22e0: 62 65 20 6c 6f 77 65 72 65 64 20 28 6f 72 20 72 be lowered (or r
22f0: 61 69 73 65 64 29 20 61 74 20 72 75 6e 2d 74 69 aised) at run-ti
2300: 6d 65 20 75 73 69 6e 67 20 74 68 61 74 20 74 68 me using that th
2310: 65 0a 2a 2a 20 6d 61 78 5f 70 61 67 65 5f 63 6f e.** max_page_co
2320: 75 6e 74 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 unt macro..*/.#i
2330: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
2340: 5f 50 41 47 45 5f 43 4f 55 4e 54 0a 23 20 64 65 _PAGE_COUNT.# de
2350: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
2360: 50 41 47 45 5f 43 4f 55 4e 54 20 31 30 37 33 37 PAGE_COUNT 10737
2370: 34 31 38 32 33 0a 23 65 6e 64 69 66 0a 0a 2f 2a 41823.#endif../*
2380: 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 .** Maximum leng
2390: 74 68 20 28 69 6e 20 62 79 74 65 73 29 20 6f 66 th (in bytes) of
23a0: 20 74 68 65 20 70 61 74 74 65 72 6e 20 69 6e 20 the pattern in
23b0: 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a a LIKE or GLOB.*
23c0: 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23 * operator..*/.#
23d0: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 ifndef SQLITE_MA
23e0: 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c X_LIKE_PATTERN_L
23f0: 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 ENGTH.# define S
2400: 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 QLITE_MAX_LIKE_P
2410: 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 35 30 ATTERN_LENGTH 50
2420: 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 000.#endif../*.*
2430: 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 * Maximum depth
2440: 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 66 6f 72 of recursion for
2450: 20 74 72 69 67 67 65 72 73 2e 0a 2a 2f 0a 23 69 triggers..*/.#i
2460: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
2470: 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 0a 23 _TRIGGER_DEPTH.#
2480: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d define SQLITE_M
2490: 41 58 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 AX_TRIGGER_DEPTH
24a0: 20 31 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 1000.#endif../*
24b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
24c0: 64 20 6f 66 20 73 71 6c 69 74 65 4c 69 6d 69 74 d of sqliteLimit
24d0: 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
24e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
24f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
2500: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f ************* Co
2510: 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 ntinuing where w
2520: 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 e left off in sq
2530: 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a liteInt.h ******
2540: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
2550: 2a 20 44 69 73 61 62 6c 65 20 6e 75 69 73 61 6e * Disable nuisan
2560: 63 65 20 77 61 72 6e 69 6e 67 73 20 6f 6e 20 42 ce warnings on B
2570: 6f 72 6c 61 6e 64 20 63 6f 6d 70 69 6c 65 72 73 orland compilers
2580: 20 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 */.#if defined(
2590: 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23 70 __BORLANDC__).#p
25a0: 72 61 67 6d 61 20 77 61 72 6e 20 2d 72 63 68 20 ragma warn -rch
25b0: 2f 2a 20 75 6e 72 65 61 63 68 61 62 6c 65 20 63 /* unreachable c
25c0: 6f 64 65 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77 ode */.#pragma w
25d0: 61 72 6e 20 2d 63 63 63 20 2f 2a 20 43 6f 6e 64 arn -ccc /* Cond
25e0: 69 74 69 6f 6e 20 69 73 20 61 6c 77 61 79 73 20 ition is always
25f0: 74 72 75 65 20 6f 72 20 66 61 6c 73 65 20 2a 2f true or false */
2600: 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 61 .#pragma warn -a
2610: 75 73 20 2f 2a 20 41 73 73 69 67 6e 65 64 20 76 us /* Assigned v
2620: 61 6c 75 65 20 69 73 20 6e 65 76 65 72 20 75 73 alue is never us
2630: 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 ed */.#pragma wa
2640: 72 6e 20 2d 63 73 75 20 2f 2a 20 43 6f 6d 70 61 rn -csu /* Compa
2650: 72 69 6e 67 20 73 69 67 6e 65 64 20 61 6e 64 20 ring signed and
2660: 75 6e 73 69 67 6e 65 64 20 2a 2f 0a 23 70 72 61 unsigned */.#pra
2670: 67 6d 61 20 77 61 72 6e 20 2d 73 70 61 20 2f 2a gma warn -spa /*
2680: 20 53 75 73 70 69 63 69 6f 75 73 20 70 6f 69 6e Suspicious poin
2690: 74 65 72 20 61 72 69 74 68 6d 65 74 69 63 20 2a ter arithmetic *
26a0: 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4e 65 65 /.#endif../* Nee
26b0: 64 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20 ded for various
26c0: 64 65 66 69 6e 69 74 69 6f 6e 73 2e 2e 2e 20 2a definitions... *
26d0: 2f 0a 23 69 66 6e 64 65 66 20 5f 47 4e 55 5f 53 /.#ifndef _GNU_S
26e0: 4f 55 52 43 45 0a 23 20 64 65 66 69 6e 65 20 5f OURCE.# define _
26f0: 47 4e 55 5f 53 4f 55 52 43 45 0a 23 65 6e 64 69 GNU_SOURCE.#endi
2700: 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 f../*.** Include
2710: 20 73 74 61 6e 64 61 72 64 20 68 65 61 64 65 72 standard header
2720: 20 66 69 6c 65 73 20 61 73 20 6e 65 63 65 73 73 files as necess
2730: 61 72 79 0a 2a 2f 0a 23 69 66 64 65 66 20 48 41 ary.*/.#ifdef HA
2740: 56 45 5f 53 54 44 49 4e 54 5f 48 0a 23 69 6e 63 VE_STDINT_H.#inc
2750: 6c 75 64 65 20 3c 73 74 64 69 6e 74 2e 68 3e 0a lude <stdint.h>.
2760: 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 48 41 #endif.#ifdef HA
2770: 56 45 5f 49 4e 54 54 59 50 45 53 5f 48 0a 23 69 VE_INTTYPES_H.#i
2780: 6e 63 6c 75 64 65 20 3c 69 6e 74 74 79 70 65 73 nclude <inttypes
2790: 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 .h>.#endif..#def
27a0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 ine SQLITE_INDEX
27b0: 5f 53 41 4d 50 4c 45 53 20 31 30 0a 0a 2f 2a 0a _SAMPLES 10../*.
27c0: 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 69 73 ** This macro is
27d0: 20 75 73 65 64 20 74 6f 20 22 68 69 64 65 22 20 used to "hide"
27e0: 73 6f 6d 65 20 75 67 6c 69 6e 65 73 73 20 69 6e some ugliness in
27f0: 20 63 61 73 74 69 6e 67 20 61 6e 20 69 6e 74 0a casting an int.
2800: 2a 2a 20 76 61 6c 75 65 20 74 6f 20 61 20 70 74 ** value to a pt
2810: 72 20 76 61 6c 75 65 20 75 6e 64 65 72 20 74 68 r value under th
2820: 65 20 4d 53 56 43 20 36 34 2d 62 69 74 20 63 6f e MSVC 64-bit co
2830: 6d 70 69 6c 65 72 2e 20 20 20 43 61 73 74 69 6e mpiler. Castin
2840: 67 0a 2a 2a 20 6e 6f 6e 20 36 34 2d 62 69 74 20 g.** non 64-bit
2850: 76 61 6c 75 65 73 20 74 6f 20 70 74 72 20 74 79 values to ptr ty
2860: 70 65 73 20 72 65 73 75 6c 74 73 20 69 6e 20 61 pes results in a
2870: 20 22 68 61 72 64 22 20 65 72 72 6f 72 20 77 69 "hard" error wi
2880: 74 68 20 0a 2a 2a 20 74 68 65 20 4d 53 56 43 20 th .** the MSVC
2890: 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65 72 20 64-bit compiler
28a0: 77 68 69 63 68 20 74 68 69 73 20 61 74 74 65 6d which this attem
28b0: 70 74 73 20 74 6f 20 61 76 6f 69 64 2e 20 20 0a pts to avoid. .
28c0: 2a 2a 0a 2a 2a 20 41 20 73 69 6d 70 6c 65 20 63 **.** A simple c
28d0: 6f 6d 70 69 6c 65 72 20 70 72 61 67 6d 61 20 6f ompiler pragma o
28e0: 72 20 63 61 73 74 69 6e 67 20 73 65 71 75 65 6e r casting sequen
28f0: 63 65 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ce could not be
2900: 66 6f 75 6e 64 0a 2a 2a 20 74 6f 20 63 6f 72 72 found.** to corr
2910: 65 63 74 20 74 68 69 73 20 69 6e 20 61 6c 6c 20 ect this in all
2920: 73 69 74 75 61 74 69 6f 6e 73 2c 20 73 6f 20 74 situations, so t
2930: 68 69 73 20 6d 61 63 72 6f 20 77 61 73 20 69 6e his macro was in
2940: 74 72 6f 64 75 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 troduced..**.**
2950: 49 74 20 63 6f 75 6c 64 20 62 65 20 61 72 67 75 It could be argu
2960: 65 64 20 74 68 61 74 20 74 68 65 20 69 6e 74 70 ed that the intp
2970: 74 72 5f 74 20 74 79 70 65 20 63 6f 75 6c 64 20 tr_t type could
2980: 62 65 20 75 73 65 64 20 69 6e 20 74 68 69 73 0a be used in this.
2990: 2a 2a 20 63 61 73 65 2c 20 62 75 74 20 74 68 61 ** case, but tha
29a0: 74 20 74 79 70 65 20 69 73 20 6e 6f 74 20 61 76 t type is not av
29b0: 61 69 6c 61 62 6c 65 20 6f 6e 20 61 6c 6c 20 63 ailable on all c
29c0: 6f 6d 70 69 6c 65 72 73 2c 20 6f 72 20 0a 2a 2a ompilers, or .**
29d0: 20 72 65 71 75 69 72 65 73 20 74 68 65 20 23 69 requires the #i
29e0: 6e 63 6c 75 64 65 20 6f 66 20 73 70 65 63 69 66 nclude of specif
29f0: 69 63 20 68 65 61 64 65 72 73 20 77 68 69 63 68 ic headers which
2a00: 20 64 69 66 66 65 72 73 20 62 65 74 77 65 65 6e differs between
2a10: 0a 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 2e 0a 2a .** platforms..*
2a20: 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 33 38 36 *.** Ticket #386
2a30: 30 3a 20 20 54 68 65 20 6c 6c 76 6d 2d 67 63 63 0: The llvm-gcc
2a40: 2d 34 2e 32 20 63 6f 6d 70 69 6c 65 72 20 66 72 -4.2 compiler fr
2a50: 6f 6d 20 41 70 70 6c 65 20 63 68 6f 6b 65 73 20 om Apple chokes
2a60: 6f 6e 0a 2a 2a 20 74 68 65 20 28 28 76 6f 69 64 on.** the ((void
2a70: 2a 29 26 28 28 63 68 61 72 2a 29 30 29 5b 58 5d *)&((char*)0)[X]
2a80: 29 20 63 6f 6e 73 74 72 75 63 74 2e 20 20 42 75 ) construct. Bu
2a90: 74 20 4d 53 56 43 20 63 68 6f 6b 65 73 20 6f 6e t MSVC chokes on
2aa0: 20 28 28 76 6f 69 64 2a 29 28 58 29 29 2e 0a 2a ((void*)(X))..*
2ab0: 2a 20 53 6f 20 77 65 20 68 61 76 65 20 74 6f 20 * So we have to
2ac0: 64 65 66 69 6e 65 20 74 68 65 20 6d 61 63 72 6f define the macro
2ad0: 73 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 77 s in different w
2ae0: 61 79 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e ays depending on
2af0: 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 the.** compiler
2b00: 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 ..*/.#if defined
2b10: 28 5f 5f 47 4e 55 43 5f 5f 29 0a 23 20 69 66 20 (__GNUC__).# if
2b20: 64 65 66 69 6e 65 64 28 48 41 56 45 5f 53 54 44 defined(HAVE_STD
2b30: 49 4e 54 5f 48 29 0a 23 20 20 20 64 65 66 69 6e INT_H).# defin
2b40: 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f e SQLITE_INT_TO_
2b50: 50 54 52 28 58 29 20 20 28 28 76 6f 69 64 2a 29 PTR(X) ((void*)
2b60: 28 69 6e 74 70 74 72 5f 74 29 28 58 29 29 0a 23 (intptr_t)(X)).#
2b70: 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
2b80: 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 58 29 20 20 _PTR_TO_INT(X)
2b90: 28 28 69 6e 74 29 28 69 6e 74 70 74 72 5f 74 29 ((int)(intptr_t)
2ba0: 28 58 29 29 0a 23 20 65 6c 73 65 0a 23 20 20 20 (X)).# else.#
2bb0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e define SQLITE_IN
2bc0: 54 5f 54 4f 5f 50 54 52 28 58 29 20 20 28 28 76 T_TO_PTR(X) ((v
2bd0: 6f 69 64 2a 29 28 58 29 29 0a 23 20 20 20 64 65 oid*)(X)).# de
2be0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 5f fine SQLITE_PTR_
2bf0: 54 4f 5f 49 4e 54 28 58 29 20 20 28 28 69 6e 74 TO_INT(X) ((int
2c00: 29 28 58 29 29 0a 23 20 65 6e 64 69 66 0a 23 65 )(X)).# endif.#e
2c10: 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c lse.# define SQL
2c20: 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 58 ITE_INT_TO_PTR(X
2c30: 29 20 20 20 28 28 76 6f 69 64 2a 29 26 28 28 63 ) ((void*)&((c
2c40: 68 61 72 2a 29 30 29 5b 58 5d 29 0a 23 20 64 65 har*)0)[X]).# de
2c50: 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 5f fine SQLITE_PTR_
2c60: 54 4f 5f 49 4e 54 28 58 29 20 20 20 28 28 69 6e TO_INT(X) ((in
2c70: 74 29 28 28 28 63 68 61 72 2a 29 58 29 2d 28 63 t)(((char*)X)-(c
2c80: 68 61 72 2a 29 30 29 29 0a 23 65 6e 64 69 66 0a har*)0)).#endif.
2c90: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ../*.** The SQLI
2ca0: 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 6d 61 TE_THREADSAFE ma
2cb0: 63 72 6f 20 6d 75 73 74 20 62 65 20 64 65 66 69 cro must be defi
2cc0: 6e 65 64 20 61 73 20 65 69 74 68 65 72 20 30 20 ned as either 0
2cd0: 6f 72 20 31 2e 0a 2a 2a 20 4f 6c 64 65 72 20 76 or 1..** Older v
2ce0: 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 ersions of SQLit
2cf0: 65 20 75 73 65 64 20 61 6e 20 6f 70 74 69 6f 6e e used an option
2d00: 61 6c 20 54 48 52 45 41 44 53 41 46 45 20 6d 61 al THREADSAFE ma
2d10: 63 72 6f 2e 0a 2a 2a 20 57 65 20 73 75 70 70 6f cro..** We suppo
2d20: 72 74 20 74 68 61 74 20 66 6f 72 20 6c 65 67 61 rt that for lega
2d30: 63 79 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e cy.*/.#if !defin
2d40: 65 64 28 53 51 4c 49 54 45 5f 54 48 52 45 41 44 ed(SQLITE_THREAD
2d50: 53 41 46 45 29 0a 23 69 66 20 64 65 66 69 6e 65 SAFE).#if define
2d60: 64 28 54 48 52 45 41 44 53 41 46 45 29 0a 23 20 d(THREADSAFE).#
2d70: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 define SQLITE_TH
2d80: 52 45 41 44 53 41 46 45 20 54 48 52 45 41 44 53 READSAFE THREADS
2d90: 41 46 45 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 AFE.#else.# defi
2da0: 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 ne SQLITE_THREAD
2db0: 53 41 46 45 20 31 0a 23 65 6e 64 69 66 0a 23 65 SAFE 1.#endif.#e
2dc0: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
2dd0: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d SQLITE_DEFAULT_M
2de0: 45 4d 53 54 41 54 55 53 20 6d 61 63 72 6f 20 6d EMSTATUS macro m
2df0: 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 61 ust be defined a
2e00: 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 31 2e s either 0 or 1.
2e10: 0a 2a 2a 20 49 74 20 64 65 74 65 72 6d 69 6e 65 .** It determine
2e20: 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 s whether or not
2e30: 20 74 68 65 20 66 65 61 74 75 72 65 73 20 72 65 the features re
2e40: 6c 61 74 65 64 20 74 6f 20 0a 2a 2a 20 53 51 4c lated to .** SQL
2e50: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 ITE_CONFIG_MEMST
2e60: 41 54 55 53 20 61 72 65 20 61 76 61 69 6c 61 62 ATUS are availab
2e70: 6c 65 20 62 79 20 64 65 66 61 75 6c 74 20 6f 72 le by default or
2e80: 20 6e 6f 74 2e 20 54 68 69 73 20 76 61 6c 75 65 not. This value
2e90: 20 63 61 6e 0a 2a 2a 20 62 65 20 6f 76 65 72 72 can.** be overr
2ea0: 69 64 64 65 6e 20 61 74 20 72 75 6e 74 69 6d 65 idden at runtime
2eb0: 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 using the sqlit
2ec0: 65 33 5f 63 6f 6e 66 69 67 28 29 20 41 50 49 2e e3_config() API.
2ed0: 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .*/.#if !defined
2ee0: 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f (SQLITE_DEFAULT_
2ef0: 4d 45 4d 53 54 41 54 55 53 29 0a 23 20 64 65 66 MEMSTATUS).# def
2f00: 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ine SQLITE_DEFAU
2f10: 4c 54 5f 4d 45 4d 53 54 41 54 55 53 20 31 0a 23 LT_MEMSTATUS 1.#
2f20: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 61 endif../*.** Exa
2f30: 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 ctly one of the
2f40: 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 following macros
2f50: 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 must be defined
2f60: 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 in order to.**
2f70: 73 70 65 63 69 66 79 20 77 68 69 63 68 20 6d 65 specify which me
2f80: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
2f90: 73 75 62 73 79 73 74 65 6d 20 74 6f 20 75 73 65 subsystem to use
2fa0: 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 ..**.** SQLI
2fb0: 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 TE_SYSTEM_MALLOC
2fc0: 20 20 20 20 20 20 20 20 20 20 2f 2f 20 55 73 65 // Use
2fd0: 20 6e 6f 72 6d 61 6c 20 73 79 73 74 65 6d 20 6d normal system m
2fe0: 61 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 20 53 alloc().** S
2ff0: 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 20 20 QLITE_MEMDEBUG
3000: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 //
3010: 44 65 62 75 67 67 69 6e 67 20 76 65 72 73 69 6f Debugging versio
3020: 6e 20 6f 66 20 73 79 73 74 65 6d 20 6d 61 6c 6c n of system mall
3030: 6f 63 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49 oc().** SQLI
3040: 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 20 TE_MEMORY_SIZE
3050: 20 20 20 20 20 20 20 20 20 20 2f 2f 20 69 6e 74 // int
3060: 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 ernal allocator
3070: 23 31 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 #1.** SQLITE
3080: 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 20 _MMAP_HEAP_SIZE
3090: 20 20 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 // inter
30a0: 6e 61 6c 20 6d 6d 61 70 28 29 20 61 6c 6c 6f 63 nal mmap() alloc
30b0: 61 74 6f 72 0a 2a 2a 20 20 20 20 20 53 51 4c 49 ator.** SQLI
30c0: 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 TE_POW2_MEMORY_S
30d0: 49 5a 45 20 20 20 20 20 20 20 2f 2f 20 69 6e 74 IZE // int
30e0: 65 72 6e 61 6c 20 70 6f 77 65 72 2d 6f 66 2d 74 ernal power-of-t
30f0: 77 6f 20 61 6c 6c 6f 63 61 74 6f 72 0a 2a 2a 0a wo allocator.**.
3100: 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 ** If none of th
3110: 65 20 61 62 6f 76 65 20 61 72 65 20 64 65 66 69 e above are defi
3120: 6e 65 64 2c 20 74 68 65 6e 20 73 65 74 20 53 51 ned, then set SQ
3130: 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c LITE_SYSTEM_MALL
3140: 4f 43 20 61 73 0a 2a 2a 20 74 68 65 20 64 65 66 OC as.** the def
3150: 61 75 6c 74 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 ault..*/.#if def
3160: 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 59 53 54 ined(SQLITE_SYST
3170: 45 4d 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66 69 6e EM_MALLOC)+defin
3180: 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 ed(SQLITE_MEMDEB
3190: 55 47 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 UG)+\. define
31a0: 64 28 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f d(SQLITE_MEMORY_
31b0: 53 49 5a 45 29 2b 64 65 66 69 6e 65 64 28 53 51 SIZE)+defined(SQ
31c0: 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 LITE_MMAP_HEAP_S
31d0: 49 5a 45 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e IZE)+\. defin
31e0: 65 64 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d ed(SQLITE_POW2_M
31f0: 45 4d 4f 52 59 5f 53 49 5a 45 29 3e 31 0a 23 20 EMORY_SIZE)>1.#
3200: 65 72 72 6f 72 20 22 41 74 20 6d 6f 73 74 20 6f error "At most o
3210: 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ne of the follow
3220: 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 ing compile-time
3230: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f configuration o
3240: 70 74 69 6f 6e 73 5c 0a 20 69 73 20 61 6c 6c 6f ptions\. is allo
3250: 77 73 3a 20 53 51 4c 49 54 45 5f 53 59 53 54 45 ws: SQLITE_SYSTE
3260: 4d 5f 4d 41 4c 4c 4f 43 2c 20 53 51 4c 49 54 45 M_MALLOC, SQLITE
3270: 5f 4d 45 4d 44 45 42 55 47 2c 20 53 51 4c 49 54 _MEMDEBUG, SQLIT
3280: 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 2c 5c 0a E_MEMORY_SIZE,\.
3290: 20 53 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 SQLITE_MMAP_HEA
32a0: 50 5f 53 49 5a 45 2c 20 53 51 4c 49 54 45 5f 50 P_SIZE, SQLITE_P
32b0: 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 22 OW2_MEMORY_SIZE"
32c0: 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 .#endif.#if defi
32d0: 6e 65 64 28 53 51 4c 49 54 45 5f 53 59 53 54 45 ned(SQLITE_SYSTE
32e0: 4d 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 M_MALLOC)+define
32f0: 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 d(SQLITE_MEMDEBU
3300: 47 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 G)+\. defined
3310: 28 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 (SQLITE_MEMORY_S
3320: 49 5a 45 29 2b 64 65 66 69 6e 65 64 28 53 51 4c IZE)+defined(SQL
3330: 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 ITE_MMAP_HEAP_SI
3340: 5a 45 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 ZE)+\. define
3350: 64 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 d(SQLITE_POW2_ME
3360: 4d 4f 52 59 5f 53 49 5a 45 29 3d 3d 30 0a 23 20 MORY_SIZE)==0.#
3370: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 define SQLITE_SY
3380: 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 31 0a 23 65 STEM_MALLOC 1.#e
3390: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 ndif../*.** If S
33a0: 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 QLITE_MALLOC_SOF
33b0: 54 5f 4c 49 4d 49 54 20 69 73 20 6e 6f 74 20 7a T_LIMIT is not z
33c0: 65 72 6f 2c 20 74 68 65 6e 20 74 72 79 20 74 6f ero, then try to
33d0: 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 69 7a keep the.** siz
33e0: 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c es of memory all
33f0: 6f 63 61 74 69 6f 6e 73 20 62 65 6c 6f 77 20 74 ocations below t
3400: 68 69 73 20 76 61 6c 75 65 20 77 68 65 72 65 20 his value where
3410: 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23 69 66 possible..*/.#if
3420: 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
3430: 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d _MALLOC_SOFT_LIM
3440: 49 54 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c IT).# define SQL
3450: 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f ITE_MALLOC_SOFT_
3460: 4c 49 4d 49 54 20 31 30 32 34 0a 23 65 6e 64 69 LIMIT 1024.#endi
3470: 66 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 6e 65 65 64 f../*.** We need
3480: 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 to define _XOPE
3490: 4e 5f 53 4f 55 52 43 45 20 61 73 20 66 6f 6c 6c N_SOURCE as foll
34a0: 6f 77 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 ows in order to
34b0: 65 6e 61 62 6c 65 0a 2a 2a 20 72 65 63 75 72 73 enable.** recurs
34c0: 69 76 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 6d ive mutexes on m
34d0: 6f 73 74 20 55 6e 69 78 20 73 79 73 74 65 6d 73 ost Unix systems
34e0: 2e 20 20 42 75 74 20 4d 61 63 20 4f 53 20 58 20 . But Mac OS X
34f0: 69 73 20 64 69 66 66 65 72 65 6e 74 2e 0a 2a 2a is different..**
3500: 20 54 68 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 The _XOPEN_SOUR
3510: 43 45 20 64 65 66 69 6e 65 20 63 61 75 73 65 73 CE define causes
3520: 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 4d 61 problems for Ma
3530: 63 20 4f 53 20 58 20 77 65 20 61 72 65 20 74 6f c OS X we are to
3540: 6c 64 2c 0a 2a 2a 20 73 6f 20 69 74 20 69 73 20 ld,.** so it is
3550: 6f 6d 69 74 74 65 64 20 74 68 65 72 65 2e 20 20 omitted there.
3560: 53 65 65 20 74 69 63 6b 65 74 20 23 32 36 37 33 See ticket #2673
3570: 2e 0a 2a 2a 0a 2a 2a 20 4c 61 74 65 72 20 77 65 ..**.** Later we
3580: 20 6c 65 61 72 6e 20 74 68 61 74 20 5f 58 4f 50 learn that _XOP
3590: 45 4e 5f 53 4f 55 52 43 45 20 69 73 20 70 6f 6f EN_SOURCE is poo
35a0: 72 6c 79 20 6f 72 20 69 6e 63 6f 72 72 65 63 74 rly or incorrect
35b0: 6c 79 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 ly.** implemente
35c0: 64 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d d on some system
35d0: 73 2e 20 20 53 6f 20 77 65 20 61 76 6f 69 64 20 s. So we avoid
35e0: 64 65 66 69 6e 69 6e 67 20 69 74 20 61 74 20 61 defining it at a
35f0: 6c 6c 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61 ll.** if it is a
3600: 6c 72 65 61 64 79 20 64 65 66 69 6e 65 64 20 6f lready defined o
3610: 72 20 69 66 20 69 74 20 69 73 20 75 6e 6e 65 65 r if it is unnee
3620: 64 65 64 20 62 65 63 61 75 73 65 20 77 65 20 61 ded because we a
3630: 72 65 0a 2a 2a 20 6e 6f 74 20 64 6f 69 6e 67 20 re.** not doing
3640: 61 20 74 68 72 65 61 64 73 61 66 65 20 62 75 69 a threadsafe bui
3650: 6c 64 2e 20 20 54 69 63 6b 65 74 20 23 32 36 38 ld. Ticket #268
3660: 31 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 1..**.** See als
3670: 6f 20 74 69 63 6b 65 74 20 23 32 37 34 31 2e 0a o ticket #2741..
3680: 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 */.#if !defined(
3690: 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 29 20 26 _XOPEN_SOURCE) &
36a0: 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 44 41 52 & !defined(__DAR
36b0: 57 49 4e 5f 5f 29 20 26 26 20 21 64 65 66 69 6e WIN__) && !defin
36c0: 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 ed(__APPLE__) &&
36d0: 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
36e0: 46 45 0a 23 20 20 64 65 66 69 6e 65 20 5f 58 4f FE.# define _XO
36f0: 50 45 4e 5f 53 4f 55 52 43 45 20 35 30 30 20 20 PEN_SOURCE 500
3700: 2f 2a 20 4e 65 65 64 65 64 20 74 6f 20 65 6e 61 /* Needed to ena
3710: 62 6c 65 20 70 74 68 72 65 61 64 20 72 65 63 75 ble pthread recu
3720: 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 2a 2f rsive mutexes */
3730: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
3740: 68 65 20 54 43 4c 20 68 65 61 64 65 72 73 20 61 he TCL headers a
3750: 72 65 20 6f 6e 6c 79 20 6e 65 65 64 65 64 20 77 re only needed w
3760: 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 hen compiling th
3770: 65 20 54 43 4c 20 62 69 6e 64 69 6e 67 73 2e 0a e TCL bindings..
3780: 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 */.#if defined(S
3790: 51 4c 49 54 45 5f 54 43 4c 29 20 7c 7c 20 64 65 QLITE_TCL) || de
37a0: 66 69 6e 65 64 28 54 43 4c 53 48 29 0a 23 20 69 fined(TCLSH).# i
37b0: 6e 63 6c 75 64 65 20 3c 74 63 6c 2e 68 3e 0a 23 nclude <tcl.h>.#
37c0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e endif../*.** Man
37d0: 79 20 70 65 6f 70 6c 65 20 61 72 65 20 66 61 69 y people are fai
37e0: 6c 69 6e 67 20 74 6f 20 73 65 74 20 2d 44 4e 44 ling to set -DND
37f0: 45 42 55 47 3d 31 20 77 68 65 6e 20 63 6f 6d 70 EBUG=1 when comp
3800: 69 6c 69 6e 67 20 53 51 4c 69 74 65 2e 0a 2a 2a iling SQLite..**
3810: 20 53 65 74 74 69 6e 67 20 4e 44 45 42 55 47 20 Setting NDEBUG
3820: 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 65 20 73 makes the code s
3830: 6d 61 6c 6c 65 72 20 61 6e 64 20 72 75 6e 20 66 maller and run f
3840: 61 73 74 65 72 2e 20 20 53 6f 20 74 68 65 20 66 aster. So the f
3850: 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 6c 69 6e 65 ollowing.** line
3860: 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 61 s are added to a
3870: 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 73 65 74 utomatically set
3880: 20 4e 44 45 42 55 47 20 75 6e 6c 65 73 73 20 74 NDEBUG unless t
3890: 68 65 20 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 he -DSQLITE_DEBU
38a0: 47 3d 31 0a 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 G=1.** option is
38b0: 20 73 65 74 2e 20 20 54 68 75 73 20 4e 44 45 42 set. Thus NDEB
38c0: 55 47 20 62 65 63 6f 6d 65 73 20 61 6e 20 6f 70 UG becomes an op
38d0: 74 2d 69 6e 20 72 61 74 68 65 72 20 74 68 61 6e t-in rather than
38e0: 20 61 6e 20 6f 70 74 2d 6f 75 74 0a 2a 2a 20 66 an opt-out.** f
38f0: 65 61 74 75 72 65 2e 0a 2a 2f 0a 23 69 66 20 21 eature..*/.#if !
3900: 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 defined(NDEBUG)
3910: 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
3920: 54 45 5f 44 45 42 55 47 29 20 0a 23 20 64 65 66 TE_DEBUG) .# def
3930: 69 6e 65 20 4e 44 45 42 55 47 20 31 0a 23 65 6e ine NDEBUG 1.#en
3940: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 dif../*.** The t
3950: 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 estcase() macro
3960: 69 73 20 75 73 65 64 20 74 6f 20 61 69 64 20 69 is used to aid i
3970: 6e 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 n coverage testi
3980: 6e 67 2e 20 20 57 68 65 6e 20 0a 2a 2a 20 64 6f ng. When .** do
3990: 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 ing coverage tes
39a0: 74 69 6e 67 2c 20 74 68 65 20 63 6f 6e 64 69 74 ting, the condit
39b0: 69 6f 6e 20 69 6e 73 69 64 65 20 74 68 65 20 61 ion inside the a
39c0: 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 65 rgument to.** te
39d0: 73 74 63 61 73 65 28 29 20 6d 75 73 74 20 62 65 stcase() must be
39e0: 20 65 76 61 6c 75 61 74 65 64 20 62 6f 74 68 20 evaluated both
39f0: 74 72 75 65 20 61 6e 64 20 66 61 6c 73 65 20 69 true and false i
3a00: 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 67 65 n order to.** ge
3a10: 74 20 66 75 6c 6c 20 62 72 61 6e 63 68 20 63 6f t full branch co
3a20: 76 65 72 61 67 65 2e 20 20 54 68 65 20 74 65 73 verage. The tes
3a30: 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 69 73 tcase() macro is
3a40: 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 74 6f 20 inserted.** to
3a50: 68 65 6c 70 20 65 6e 73 75 72 65 20 61 64 65 71 help ensure adeq
3a60: 75 61 74 65 20 74 65 73 74 20 63 6f 76 65 72 61 uate test covera
3a70: 67 65 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65 ge in places whe
3a80: 72 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6e re simple.** con
3a90: 64 69 74 69 6f 6e 2f 64 65 63 69 73 69 6f 6e 20 dition/decision
3aa0: 63 6f 76 65 72 61 67 65 20 69 73 20 69 6e 61 64 coverage is inad
3ab0: 65 71 75 61 74 65 2e 20 20 46 6f 72 20 65 78 61 equate. For exa
3ac0: 6d 70 6c 65 2c 20 74 65 73 74 63 61 73 65 28 29 mple, testcase()
3ad0: 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 .** can be used
3ae0: 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 62 6f 75 to make sure bou
3af0: 6e 64 61 72 79 20 76 61 6c 75 65 73 20 61 72 65 ndary values are
3b00: 20 74 65 73 74 65 64 2e 20 20 46 6f 72 0a 2a 2a tested. For.**
3b10: 20 62 69 74 6d 61 73 6b 20 74 65 73 74 73 2c 20 bitmask tests,
3b20: 74 65 73 74 63 61 73 65 28 29 20 63 61 6e 20 62 testcase() can b
3b30: 65 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 e used to make s
3b40: 75 72 65 20 65 61 63 68 20 62 69 74 0a 2a 2a 20 ure each bit.**
3b50: 69 73 20 73 69 67 6e 69 66 69 63 61 6e 74 20 61 is significant a
3b60: 6e 64 20 75 73 65 64 20 61 74 20 6c 65 61 73 74 nd used at least
3b70: 20 6f 6e 63 65 2e 20 20 4f 6e 20 73 77 69 74 63 once. On switc
3b80: 68 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 h statements.**
3b90: 77 68 65 72 65 20 6d 75 6c 74 69 70 6c 65 20 63 where multiple c
3ba0: 61 73 65 73 20 67 6f 20 74 6f 20 74 68 65 20 73 ases go to the s
3bb0: 61 6d 65 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 ame block of cod
3bc0: 65 2c 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2a e, testcase().**
3bd0: 20 63 61 6e 20 69 6e 73 75 72 65 20 74 68 61 74 can insure that
3be0: 20 61 6c 6c 20 63 61 73 65 73 20 61 72 65 20 65 all cases are e
3bf0: 76 61 6c 75 61 74 65 64 2e 0a 2a 2a 0a 2a 2f 0a valuated..**.*/.
3c00: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 4f #ifdef SQLITE_CO
3c10: 56 45 52 41 47 45 5f 54 45 53 54 0a 53 51 4c 49 VERAGE_TEST.SQLI
3c20: 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
3c30: 64 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67 d sqlite3Coverag
3c40: 65 28 69 6e 74 29 3b 0a 23 20 64 65 66 69 6e 65 e(int);.# define
3c50: 20 74 65 73 74 63 61 73 65 28 58 29 20 20 69 66 testcase(X) if
3c60: 28 20 58 20 29 7b 20 73 71 6c 69 74 65 33 43 6f ( X ){ sqlite3Co
3c70: 76 65 72 61 67 65 28 5f 5f 4c 49 4e 45 5f 5f 29 verage(__LINE__)
3c80: 3b 20 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 ; }.#else.# defi
3c90: 6e 65 20 74 65 73 74 63 61 73 65 28 58 29 0a 23 ne testcase(X).#
3ca0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
3cb0: 20 54 45 53 54 4f 4e 4c 59 20 6d 61 63 72 6f 20 TESTONLY macro
3cc0: 69 73 20 75 73 65 64 20 74 6f 20 65 6e 63 6c 6f is used to enclo
3cd0: 73 65 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c se variable decl
3ce0: 61 72 61 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 6f arations or.** o
3cf0: 74 68 65 72 20 62 69 74 73 20 6f 66 20 63 6f 64 ther bits of cod
3d00: 65 20 74 68 61 74 20 61 72 65 20 6e 65 65 64 65 e that are neede
3d10: 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 65 d to support the
3d20: 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 77 69 arguments.** wi
3d30: 74 68 69 6e 20 74 65 73 74 63 61 73 65 28 29 20 thin testcase()
3d40: 61 6e 64 20 61 73 73 65 72 74 28 29 20 6d 61 63 and assert() mac
3d50: 72 6f 73 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 ros..*/.#if !def
3d60: 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 ined(NDEBUG) ||
3d70: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 defined(SQLITE_C
3d80: 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0a 23 20 OVERAGE_TEST).#
3d90: 64 65 66 69 6e 65 20 54 45 53 54 4f 4e 4c 59 28 define TESTONLY(
3da0: 58 29 20 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 X) X.#else.# de
3db0: 66 69 6e 65 20 54 45 53 54 4f 4e 4c 59 28 58 29 fine TESTONLY(X)
3dc0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 .#endif../*.** S
3dd0: 6f 6d 65 74 69 6d 65 73 20 77 65 20 6e 65 65 64 ometimes we need
3de0: 20 61 20 73 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 a small amount
3df0: 6f 66 20 63 6f 64 65 20 73 75 63 68 20 61 73 20 of code such as
3e00: 61 20 76 61 72 69 61 62 6c 65 20 69 6e 69 74 69 a variable initi
3e10: 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 alization.** to
3e20: 73 65 74 75 70 20 66 6f 72 20 61 20 6c 61 74 65 setup for a late
3e30: 72 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 r assert() state
3e40: 6d 65 6e 74 2e 20 20 57 65 20 64 6f 20 6e 6f 74 ment. We do not
3e50: 20 77 61 6e 74 20 74 68 69 73 20 63 6f 64 65 20 want this code
3e60: 74 6f 0a 2a 2a 20 61 70 70 65 61 72 20 77 68 65 to.** appear whe
3e70: 6e 20 61 73 73 65 72 74 28 29 20 69 73 20 64 69 n assert() is di
3e80: 73 61 62 6c 65 64 2e 20 20 54 68 65 20 66 6f 6c sabled. The fol
3e90: 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 lowing macro is
3ea0: 74 68 65 72 65 66 6f 72 65 0a 2a 2a 20 75 73 65 therefore.** use
3eb0: 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 61 d to contain tha
3ec0: 74 20 73 65 74 75 70 20 63 6f 64 65 2e 20 20 54 t setup code. T
3ed0: 68 65 20 22 56 56 41 22 20 61 63 72 6f 6e 79 6d he "VVA" acronym
3ee0: 20 73 74 61 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 stands for.** "
3ef0: 56 65 72 69 66 69 63 61 74 69 6f 6e 2c 20 56 61 Verification, Va
3f00: 6c 69 64 61 74 69 6f 6e 2c 20 61 6e 64 20 41 63 lidation, and Ac
3f10: 63 72 65 64 69 74 61 74 69 6f 6e 22 2e 20 20 49 creditation". I
3f20: 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 n other words, t
3f30: 68 65 0a 2a 2a 20 63 6f 64 65 20 77 69 74 68 69 he.** code withi
3f40: 6e 20 56 56 41 5f 4f 4e 4c 59 28 29 20 77 69 6c n VVA_ONLY() wil
3f50: 6c 20 6f 6e 6c 79 20 72 75 6e 20 64 75 72 69 6e l only run durin
3f60: 67 20 76 65 72 69 66 69 63 61 74 69 6f 6e 20 70 g verification p
3f70: 72 6f 63 65 73 73 65 73 2e 0a 2a 2f 0a 23 69 66 rocesses..*/.#if
3f80: 6e 64 65 66 20 4e 44 45 42 55 47 0a 23 20 64 65 ndef NDEBUG.# de
3f90: 66 69 6e 65 20 56 56 41 5f 4f 4e 4c 59 28 58 29 fine VVA_ONLY(X)
3fa0: 20 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 X.#else.# defi
3fb0: 6e 65 20 56 56 41 5f 4f 4e 4c 59 28 58 29 0a 23 ne VVA_ONLY(X).#
3fc0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
3fd0: 20 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 ALWAYS and NEVE
3fe0: 52 20 6d 61 63 72 6f 73 20 73 75 72 72 6f 75 6e R macros surroun
3ff0: 64 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 d boolean expres
4000: 73 69 6f 6e 73 20 77 68 69 63 68 20 0a 2a 2a 20 sions which .**
4010: 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 are intended to
4020: 61 6c 77 61 79 73 20 62 65 20 74 72 75 65 20 6f always be true o
4030: 72 20 66 61 6c 73 65 2c 20 72 65 73 70 65 63 74 r false, respect
4040: 69 76 65 6c 79 2e 20 20 53 75 63 68 0a 2a 2a 20 ively. Such.**
4050: 65 78 70 72 65 73 73 69 6f 6e 73 20 63 6f 75 6c expressions coul
4060: 64 20 62 65 20 6f 6d 69 74 74 65 64 20 66 72 6f d be omitted fro
4070: 6d 20 74 68 65 20 63 6f 64 65 20 63 6f 6d 70 6c m the code compl
4080: 65 74 65 6c 79 2e 20 20 42 75 74 20 74 68 65 79 etely. But they
4090: 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64 .** are included
40a0: 20 69 6e 20 61 20 66 65 77 20 63 61 73 65 73 20 in a few cases
40b0: 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e 68 61 in order to enha
40c0: 6e 63 65 20 74 68 65 20 72 65 73 69 6c 69 65 6e nce the resilien
40d0: 63 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 ce.** of SQLite
40e0: 74 6f 20 75 6e 65 78 70 65 63 74 65 64 20 62 65 to unexpected be
40f0: 68 61 76 69 6f 72 20 2d 20 74 6f 20 6d 61 6b 65 havior - to make
4100: 20 74 68 65 20 63 6f 64 65 20 22 73 65 6c 66 2d the code "self-
4110: 68 65 61 6c 69 6e 67 22 0a 2a 2a 20 6f 72 20 22 healing".** or "
4120: 64 75 63 74 69 6c 65 22 20 72 61 74 68 65 72 20 ductile" rather
4130: 74 68 61 6e 20 62 65 69 6e 67 20 22 62 72 69 74 than being "brit
4140: 74 6c 65 22 20 61 6e 64 20 63 72 61 73 68 69 6e tle" and crashin
4150: 67 20 61 74 20 74 68 65 20 66 69 72 73 74 0a 2a g at the first.*
4160: 2a 20 68 69 6e 74 20 6f 66 20 75 6e 70 6c 61 6e * hint of unplan
4170: 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a ned behavior..**
4180: 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 .** In other wor
4190: 64 73 2c 20 41 4c 57 41 59 53 20 61 6e 64 20 4e ds, ALWAYS and N
41a0: 45 56 45 52 20 61 72 65 20 61 64 64 65 64 20 66 EVER are added f
41b0: 6f 72 20 64 65 66 65 6e 73 69 76 65 20 63 6f 64 or defensive cod
41c0: 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 64 6f e..**.** When do
41d0: 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 ing coverage tes
41e0: 74 69 6e 67 20 41 4c 57 41 59 53 20 61 6e 64 20 ting ALWAYS and
41f0: 4e 45 56 45 52 20 61 72 65 20 68 61 72 64 2d 63 NEVER are hard-c
4200: 6f 64 65 64 20 74 6f 0a 2a 2a 20 62 65 20 74 72 oded to.** be tr
4210: 75 65 20 61 6e 64 20 66 61 6c 73 65 20 73 6f 20 ue and false so
4220: 74 68 61 74 20 74 68 65 20 75 6e 72 65 61 63 68 that the unreach
4230: 61 62 6c 65 20 63 6f 64 65 20 74 68 65 6e 20 73 able code then s
4240: 70 65 63 69 66 79 20 77 69 6c 6c 0a 2a 2a 20 6e pecify will.** n
4250: 6f 74 20 62 65 20 63 6f 75 6e 74 65 64 20 61 73 ot be counted as
4260: 20 75 6e 74 65 73 74 65 64 20 63 6f 64 65 2e 0a untested code..
4270: 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 */.#if defined(S
4280: 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 QLITE_COVERAGE_T
4290: 45 53 54 29 0a 23 20 64 65 66 69 6e 65 20 41 4c EST).# define AL
42a0: 57 41 59 53 28 58 29 20 20 20 20 20 20 28 31 29 WAYS(X) (1)
42b0: 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52 28 .# define NEVER(
42c0: 58 29 20 20 20 20 20 20 20 28 30 29 0a 23 65 6c X) (0).#el
42d0: 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 if !defined(NDEB
42e0: 55 47 29 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 UG).# define ALW
42f0: 41 59 53 28 58 29 20 20 20 20 20 20 28 28 58 29 AYS(X) ((X)
4300: 3f 31 3a 28 61 73 73 65 72 74 28 30 29 2c 30 29 ?1:(assert(0),0)
4310: 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52 ).# define NEVER
4320: 28 58 29 20 20 20 20 20 20 20 28 28 58 29 3f 28 (X) ((X)?(
4330: 61 73 73 65 72 74 28 30 29 2c 31 29 3a 30 29 0a assert(0),1):0).
4340: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 41 #else.# define A
4350: 4c 57 41 59 53 28 58 29 20 20 20 20 20 20 28 58 LWAYS(X) (X
4360: 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52 ).# define NEVER
4370: 28 58 29 20 20 20 20 20 20 20 28 58 29 0a 23 65 (X) (X).#e
4380: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
4390: 6d 61 63 72 6f 20 75 6e 6c 69 6b 65 6c 79 28 29 macro unlikely()
43a0: 20 69 73 20 61 20 68 69 6e 74 20 74 68 61 74 20 is a hint that
43b0: 73 75 72 72 6f 75 6e 64 73 20 61 20 62 6f 6f 6c surrounds a bool
43c0: 65 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f ean.** expressio
43d0: 6e 20 74 68 61 74 20 69 73 20 75 73 75 61 6c 6c n that is usuall
43e0: 79 20 66 61 6c 73 65 2e 20 20 4d 61 63 72 6f 20 y false. Macro
43f0: 6c 69 6b 65 6c 79 28 29 20 73 75 72 72 6f 75 6e likely() surroun
4400: 64 73 0a 2a 2a 20 61 20 62 6f 6f 6c 65 61 6e 20 ds.** a boolean
4410: 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 expression that
4420: 69 73 20 75 73 75 61 6c 6c 79 20 74 72 75 65 2e is usually true.
4430: 20 20 47 43 43 20 69 73 20 61 62 6c 65 20 74 6f GCC is able to
4440: 0a 2a 2a 20 75 73 65 20 74 68 65 73 65 20 68 69 .** use these hi
4450: 6e 74 73 20 74 6f 20 67 65 6e 65 72 61 74 65 20 nts to generate
4460: 62 65 74 74 65 72 20 63 6f 64 65 2c 20 73 6f 6d better code, som
4470: 65 74 69 6d 65 73 2e 0a 2a 2f 0a 23 69 66 20 64 etimes..*/.#if d
4480: 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 efined(__GNUC__)
4490: 20 26 26 20 30 0a 23 20 64 65 66 69 6e 65 20 6c && 0.# define l
44a0: 69 6b 65 6c 79 28 58 29 20 20 20 20 5f 5f 62 75 ikely(X) __bu
44b0: 69 6c 74 69 6e 5f 65 78 70 65 63 74 28 28 58 29 iltin_expect((X)
44c0: 2c 31 29 0a 23 20 64 65 66 69 6e 65 20 75 6e 6c ,1).# define unl
44d0: 69 6b 65 6c 79 28 58 29 20 20 5f 5f 62 75 69 6c ikely(X) __buil
44e0: 74 69 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 30 tin_expect((X),0
44f0: 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ).#else.# define
4500: 20 6c 69 6b 65 6c 79 28 58 29 20 20 20 20 21 21 likely(X) !!
4510: 28 58 29 0a 23 20 64 65 66 69 6e 65 20 75 6e 6c (X).# define unl
4520: 69 6b 65 6c 79 28 58 29 20 20 21 21 28 58 29 0a ikely(X) !!(X).
4530: 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a #endif../*******
4540: 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
4550: 73 71 6c 69 74 65 33 2e 68 20 69 6e 20 74 68 65 sqlite3.h in the
4560: 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 middle of sqlit
4570: 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
4580: 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
4590: 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
45a0: 6c 65 20 73 71 6c 69 74 65 33 2e 68 20 2a 2a 2a le sqlite3.h ***
45b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45d0: 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
45e0: 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 01 September 15.
45f0: 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
4600: 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
4610: 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
4620: 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
4630: 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
4640: 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
4650: 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
4660: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
4670: 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
4680: 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
4690: 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
46a0: 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
46b0: 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
46c0: 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
46d0: 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
46e0: 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
46f0: 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
4700: 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
4710: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4720: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4730: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4740: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
4750: 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 * This header fi
4760: 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 le defines the i
4770: 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68 nterface that th
4780: 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 e SQLite library
4790: 0a 2a 2a 20 70 72 65 73 65 6e 74 73 20 74 6f 20 .** presents to
47a0: 63 6c 69 65 6e 74 20 70 72 6f 67 72 61 6d 73 2e client programs.
47b0: 20 20 49 66 20 61 20 43 2d 66 75 6e 63 74 69 6f If a C-functio
47c0: 6e 2c 20 73 74 72 75 63 74 75 72 65 2c 20 64 61 n, structure, da
47d0: 74 61 74 79 70 65 2c 0a 2a 2a 20 6f 72 20 63 6f tatype,.** or co
47e0: 6e 73 74 61 6e 74 20 64 65 66 69 6e 69 74 69 6f nstant definitio
47f0: 6e 20 64 6f 65 73 20 6e 6f 74 20 61 70 70 65 61 n does not appea
4800: 72 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 r in this file,
4810: 74 68 65 6e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f then it is.** no
4820: 74 20 61 20 70 75 62 6c 69 73 68 65 64 20 41 50 t a published AP
4830: 49 20 6f 66 20 53 51 4c 69 74 65 2c 20 69 73 20 I of SQLite, is
4840: 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 subject to chang
4850: 65 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6e 6f 74 e without.** not
4860: 69 63 65 2c 20 61 6e 64 20 73 68 6f 75 6c 64 20 ice, and should
4870: 6e 6f 74 20 62 65 20 72 65 66 65 72 65 6e 63 65 not be reference
4880: 64 20 62 79 20 70 72 6f 67 72 61 6d 73 20 74 68 d by programs th
4890: 61 74 20 75 73 65 20 53 51 4c 69 74 65 2e 0a 2a at use SQLite..*
48a0: 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 *.** Some of the
48b0: 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 74 68 61 definitions tha
48c0: 74 20 61 72 65 20 69 6e 20 74 68 69 73 20 66 69 t are in this fi
48d0: 6c 65 20 61 72 65 20 6d 61 72 6b 65 64 20 61 73 le are marked as
48e0: 0a 2a 2a 20 22 65 78 70 65 72 69 6d 65 6e 74 61 .** "experimenta
48f0: 6c 22 2e 20 20 45 78 70 65 72 69 6d 65 6e 74 61 l". Experimenta
4900: 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 l interfaces are
4910: 20 6e 6f 72 6d 61 6c 6c 79 20 6e 65 77 0a 2a 2a normally new.**
4920: 20 66 65 61 74 75 72 65 73 20 72 65 63 65 6e 74 features recent
4930: 6c 79 20 61 64 64 65 64 20 74 6f 20 53 51 4c 69 ly added to SQLi
4940: 74 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 61 te. We do not a
4950: 6e 74 69 63 69 70 61 74 65 20 63 68 61 6e 67 65 nticipate change
4960: 73 0a 2a 2a 20 74 6f 20 65 78 70 65 72 69 6d 65 s.** to experime
4970: 6e 74 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 ntal interfaces
4980: 62 75 74 20 72 65 73 65 72 76 65 20 74 68 65 20 but reserve the
4990: 72 69 67 68 74 20 74 6f 20 6d 61 6b 65 20 6d 69 right to make mi
49a0: 6e 6f 72 20 63 68 61 6e 67 65 73 0a 2a 2a 20 69 nor changes.** i
49b0: 66 20 65 78 70 65 72 69 65 6e 63 65 20 66 72 6f f experience fro
49c0: 6d 20 75 73 65 20 22 69 6e 20 74 68 65 20 77 69 m use "in the wi
49d0: 6c 64 22 20 73 75 67 67 65 73 74 20 73 75 63 68 ld" suggest such
49e0: 20 63 68 61 6e 67 65 73 20 61 72 65 20 70 72 75 changes are pru
49f0: 64 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 dent..**.** The
4a00: 6f 66 66 69 63 69 61 6c 20 43 2d 6c 61 6e 67 75 official C-langu
4a10: 61 67 65 20 41 50 49 20 64 6f 63 75 6d 65 6e 74 age API document
4a20: 61 74 69 6f 6e 20 66 6f 72 20 53 51 4c 69 74 65 ation for SQLite
4a30: 20 69 73 20 64 65 72 69 76 65 64 0a 2a 2a 20 66 is derived.** f
4a40: 72 6f 6d 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 rom comments in
4a50: 74 68 69 73 20 66 69 6c 65 2e 20 20 54 68 69 73 this file. This
4a60: 20 66 69 6c 65 20 69 73 20 74 68 65 20 61 75 74 file is the aut
4a70: 68 6f 72 69 74 61 74 69 76 65 20 73 6f 75 72 63 horitative sourc
4a80: 65 0a 2a 2a 20 6f 6e 20 68 6f 77 20 53 51 4c 69 e.** on how SQLi
4a90: 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 te interfaces ar
4aa0: 65 20 73 75 70 70 6f 73 65 20 74 6f 20 6f 70 65 e suppose to ope
4ab0: 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rate..**.** The
4ac0: 6e 61 6d 65 20 6f 66 20 74 68 69 73 20 66 69 6c name of this fil
4ad0: 65 20 75 6e 64 65 72 20 63 6f 6e 66 69 67 75 72 e under configur
4ae0: 61 74 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 ation management
4af0: 20 69 73 20 22 73 71 6c 69 74 65 2e 68 2e 69 6e is "sqlite.h.in
4b00: 22 2e 0a 2a 2a 20 54 68 65 20 6d 61 6b 65 66 69 "..** The makefi
4b10: 6c 65 20 6d 61 6b 65 73 20 73 6f 6d 65 20 6d 69 le makes some mi
4b20: 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f 20 74 nor changes to t
4b30: 68 69 73 20 66 69 6c 65 20 28 73 75 63 68 20 61 his file (such a
4b40: 73 20 69 6e 73 65 72 74 69 6e 67 0a 2a 2a 20 74 s inserting.** t
4b50: 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 he version numbe
4b60: 72 29 20 61 6e 64 20 63 68 61 6e 67 65 73 20 69 r) and changes i
4b70: 74 73 20 6e 61 6d 65 20 74 6f 20 22 73 71 6c 69 ts name to "sqli
4b80: 74 65 33 2e 68 22 20 61 73 0a 2a 2a 20 70 61 72 te3.h" as.** par
4b90: 74 20 6f 66 20 74 68 65 20 62 75 69 6c 64 20 70 t of the build p
4ba0: 72 6f 63 65 73 73 2e 0a 2a 2f 0a 23 69 66 6e 64 rocess..*/.#ifnd
4bb0: 65 66 20 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23 ef _SQLITE3_H_.#
4bc0: 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 33 5f define _SQLITE3_
4bd0: 48 5f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 H_.#include <std
4be0: 61 72 67 2e 68 3e 20 20 20 20 20 2f 2a 20 4e 65 arg.h> /* Ne
4bf0: 65 64 65 64 20 66 6f 72 20 74 68 65 20 64 65 66 eded for the def
4c00: 69 6e 69 74 69 6f 6e 20 6f 66 20 76 61 5f 6c 69 inition of va_li
4c10: 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b st */../*.** Mak
4c20: 65 20 73 75 72 65 20 77 65 20 63 61 6e 20 63 61 e sure we can ca
4c30: 6c 6c 20 74 68 69 73 20 73 74 75 66 66 20 66 72 ll this stuff fr
4c40: 6f 6d 20 43 2b 2b 2e 0a 2a 2f 0a 23 69 66 20 30 om C++..*/.#if 0
4c50: 0a 65 78 74 65 72 6e 20 22 43 22 20 7b 0a 23 65 .extern "C" {.#e
4c60: 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 ndif.../*.** Add
4c70: 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 the ability to
4c80: 6f 76 65 72 72 69 64 65 20 27 65 78 74 65 72 6e override 'extern
4c90: 27 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c '.*/.#ifndef SQL
4ca0: 49 54 45 5f 45 58 54 45 52 4e 0a 23 20 64 65 66 ITE_EXTERN.# def
4cb0: 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54 45 52 ine SQLITE_EXTER
4cc0: 4e 20 65 78 74 65 72 6e 0a 23 65 6e 64 69 66 0a N extern.#endif.
4cd0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
4ce0: 41 50 49 0a 23 20 64 65 66 69 6e 65 20 53 51 4c API.# define SQL
4cf0: 49 54 45 5f 41 50 49 0a 23 65 6e 64 69 66 0a 0a ITE_API.#endif..
4d00: 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6e 6f 2d ./*.** These no-
4d10: 6f 70 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73 op macros are us
4d20: 65 64 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 69 ed in front of i
4d30: 6e 74 65 72 66 61 63 65 73 20 74 6f 20 6d 61 72 nterfaces to mar
4d40: 6b 20 74 68 6f 73 65 0a 2a 2a 20 69 6e 74 65 72 k those.** inter
4d50: 66 61 63 65 73 20 61 73 20 65 69 74 68 65 72 20 faces as either
4d60: 64 65 70 72 65 63 61 74 65 64 20 6f 72 20 65 78 deprecated or ex
4d70: 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 4e 65 77 perimental. New
4d80: 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a applications.**
4d90: 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75 73 65 20 should not use
4da0: 64 65 70 72 65 63 61 74 65 64 20 69 6e 74 65 72 deprecated inter
4db0: 66 61 63 65 73 20 2d 20 74 68 65 79 20 61 72 65 faces - they are
4dc0: 20 73 75 70 70 6f 72 74 20 66 6f 72 20 62 61 63 support for bac
4dd0: 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 kwards.** compat
4de0: 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 20 20 41 ibility only. A
4df0: 70 70 6c 69 63 61 74 69 6f 6e 20 77 72 69 74 65 pplication write
4e00: 72 73 20 73 68 6f 75 6c 64 20 62 65 20 61 77 61 rs should be awa
4e10: 72 65 20 74 68 61 74 0a 2a 2a 20 65 78 70 65 72 re that.** exper
4e20: 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66 61 63 imental interfac
4e30: 65 73 20 61 72 65 20 73 75 62 6a 65 63 74 20 74 es are subject t
4e40: 6f 20 63 68 61 6e 67 65 20 69 6e 20 70 6f 69 6e o change in poin
4e50: 74 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a t releases..**.*
4e60: 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 75 * These macros u
4e70: 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 sed to resolve t
4e80: 6f 20 76 61 72 69 6f 75 73 20 6b 69 6e 64 73 20 o various kinds
4e90: 6f 66 20 63 6f 6d 70 69 6c 65 72 20 6d 61 67 69 of compiler magi
4ea0: 63 20 74 68 61 74 0a 2a 2a 20 77 6f 75 6c 64 20 c that.** would
4eb0: 67 65 6e 65 72 61 74 65 20 77 61 72 6e 69 6e 67 generate warning
4ec0: 20 6d 65 73 73 61 67 65 73 20 77 68 65 6e 20 74 messages when t
4ed0: 68 65 79 20 77 65 72 65 20 75 73 65 64 2e 20 20 hey were used.
4ee0: 42 75 74 20 74 68 61 74 0a 2a 2a 20 63 6f 6d 70 But that.** comp
4ef0: 69 6c 65 72 20 6d 61 67 69 63 20 65 6e 64 65 64 iler magic ended
4f00: 20 75 70 20 67 65 6e 65 72 61 74 69 6e 67 20 73 up generating s
4f10: 75 63 68 20 61 20 66 6c 75 72 72 79 20 6f 66 20 uch a flurry of
4f20: 62 75 67 20 72 65 70 6f 72 74 73 0a 2a 2a 20 74 bug reports.** t
4f30: 68 61 74 20 77 65 20 68 61 76 65 20 74 61 6b 65 hat we have take
4f40: 6e 20 69 74 20 61 6c 6c 20 6f 75 74 20 61 6e 64 n it all out and
4f50: 20 67 6f 6e 65 20 62 61 63 6b 20 74 6f 20 75 73 gone back to us
4f60: 69 6e 67 20 73 69 6d 70 6c 65 0a 2a 2a 20 6e 6f ing simple.** no
4f70: 6f 70 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 op macros..*/.#d
4f80: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 50 efine SQLITE_DEP
4f90: 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65 20 RECATED.#define
4fa0: 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e SQLITE_EXPERIMEN
4fb0: 54 41 4c 0a 0a 2f 2a 0a 2a 2a 20 45 6e 73 75 72 TAL../*.** Ensur
4fc0: 65 20 74 68 65 73 65 20 73 79 6d 62 6f 6c 73 20 e these symbols
4fd0: 77 65 72 65 20 6e 6f 74 20 64 65 66 69 6e 65 64 were not defined
4fe0: 20 62 79 20 73 6f 6d 65 20 70 72 65 76 69 6f 75 by some previou
4ff0: 73 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a s header file..*
5000: 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
5010: 56 45 52 53 49 4f 4e 0a 23 20 75 6e 64 65 66 20 VERSION.# undef
5020: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 0a 23 SQLITE_VERSION.#
5030: 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
5040: 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 ITE_VERSION_NUMB
5050: 45 52 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 ER.# undef SQLIT
5060: 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 E_VERSION_NUMBER
5070: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 .#endif../*.** C
5080: 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c 65 API3REF: Compile
5090: 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20 56 65 -Time Library Ve
50a0: 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20 7b 48 rsion Numbers {H
50b0: 31 30 30 31 30 7d 20 3c 53 36 30 31 30 30 3e 0a 10010} <S60100>.
50c0: 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 **.** The SQLITE
50d0: 5f 56 45 52 53 49 4f 4e 20 61 6e 64 20 53 51 4c _VERSION and SQL
50e0: 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 ITE_VERSION_NUMB
50f0: 45 52 20 23 64 65 66 69 6e 65 73 20 69 6e 0a 2a ER #defines in.*
5100: 2a 20 74 68 65 20 73 71 6c 69 74 65 33 2e 68 20 * the sqlite3.h
5110: 66 69 6c 65 20 73 70 65 63 69 66 79 20 74 68 65 file specify the
5120: 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 version of SQLi
5130: 74 65 20 77 69 74 68 20 77 68 69 63 68 0a 2a 2a te with which.**
5140: 20 74 68 61 74 20 68 65 61 64 65 72 20 66 69 6c that header fil
5150: 65 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 2e e is associated.
5160: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 76 65 72 73 .**.** The "vers
5170: 69 6f 6e 22 20 6f 66 20 53 51 4c 69 74 65 20 69 ion" of SQLite i
5180: 73 20 61 20 73 74 72 69 6e 67 20 6f 66 20 74 68 s a string of th
5190: 65 20 66 6f 72 6d 20 22 57 2e 58 2e 59 22 20 6f e form "W.X.Y" o
51a0: 72 20 22 57 2e 58 2e 59 2e 5a 22 2e 0a 2a 2a 20 r "W.X.Y.Z"..**
51b0: 54 68 65 20 57 20 76 61 6c 75 65 20 69 73 20 6d The W value is m
51c0: 61 6a 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d ajor version num
51d0: 62 65 72 20 61 6e 64 20 69 73 20 61 6c 77 61 79 ber and is alway
51e0: 73 20 33 20 69 6e 20 53 51 4c 69 74 65 33 2e 0a s 3 in SQLite3..
51f0: 2a 2a 20 54 68 65 20 57 20 76 61 6c 75 65 20 6f ** The W value o
5200: 6e 6c 79 20 63 68 61 6e 67 65 73 20 77 68 65 6e nly changes when
5210: 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 backwards compa
5220: 74 69 62 69 6c 69 74 79 20 69 73 0a 2a 2a 20 62 tibility is.** b
5230: 72 6f 6b 65 6e 20 61 6e 64 20 77 65 20 69 6e 74 roken and we int
5240: 65 6e 64 20 74 6f 20 6e 65 76 65 72 20 62 72 65 end to never bre
5250: 61 6b 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d ak backwards com
5260: 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 54 patibility..** T
5270: 68 65 20 58 20 76 61 6c 75 65 20 69 73 20 74 68 he X value is th
5280: 65 20 6d 69 6e 6f 72 20 76 65 72 73 69 6f 6e 20 e minor version
5290: 6e 75 6d 62 65 72 20 61 6e 64 20 6f 6e 6c 79 20 number and only
52a0: 63 68 61 6e 67 65 73 20 77 68 65 6e 0a 2a 2a 20 changes when.**
52b0: 74 68 65 72 65 20 61 72 65 20 6d 61 6a 6f 72 20 there are major
52c0: 66 65 61 74 75 72 65 20 65 6e 68 61 6e 63 65 6d feature enhancem
52d0: 65 6e 74 73 20 74 68 61 74 20 61 72 65 20 66 6f ents that are fo
52e0: 72 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 6c rwards compatibl
52f0: 65 0a 2a 2a 20 62 75 74 20 6e 6f 74 20 62 61 63 e.** but not bac
5300: 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 6c kwards compatibl
5310: 65 2e 0a 2a 2a 20 54 68 65 20 59 20 76 61 6c 75 e..** The Y valu
5320: 65 20 69 73 20 74 68 65 20 72 65 6c 65 61 73 65 e is the release
5330: 20 6e 75 6d 62 65 72 20 61 6e 64 20 69 73 20 69 number and is i
5340: 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74 68 0a ncremented with.
5350: 2a 2a 20 65 61 63 68 20 72 65 6c 65 61 73 65 20 ** each release
5360: 62 75 74 20 72 65 73 65 74 73 20 62 61 63 6b 20 but resets back
5370: 74 6f 20 30 20 77 68 65 6e 65 76 65 72 20 58 20 to 0 whenever X
5380: 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 0a is incremented..
5390: 2a 2a 20 54 68 65 20 5a 20 76 61 6c 75 65 20 6f ** The Z value o
53a0: 6e 6c 79 20 61 70 70 65 61 72 73 20 6f 6e 20 62 nly appears on b
53b0: 72 61 6e 63 68 20 72 65 6c 65 61 73 65 73 2e 0a ranch releases..
53c0: 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 **.** The SQLITE
53d0: 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 _VERSION_NUMBER
53e0: 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 is an integer th
53f0: 61 74 20 69 73 20 63 6f 6d 70 75 74 65 64 20 61 at is computed a
5400: 73 0a 2a 2a 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a s.** follows:.**
5410: 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e .** <blockquote>
5420: 3c 70 72 65 3e 0a 2a 2a 20 53 51 4c 49 54 45 5f <pre>.** SQLITE_
5430: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 3d VERSION_NUMBER =
5440: 20 57 2a 31 30 30 30 30 30 30 20 2b 20 58 2a 31 W*1000000 + X*1
5450: 30 30 30 20 2b 20 59 0a 2a 2a 20 3c 2f 70 72 65 000 + Y.** </pre
5460: 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a ></blockquote>.*
5470: 2a 0a 2a 2a 20 53 69 6e 63 65 20 76 65 72 73 69 *.** Since versi
5480: 6f 6e 20 33 2e 36 2e 31 38 2c 20 53 51 4c 69 74 on 3.6.18, SQLit
5490: 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20 68 61 e source code ha
54a0: 73 20 62 65 65 6e 20 73 74 6f 72 65 64 20 69 6e s been stored in
54b0: 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d the.** <a href=
54c0: 22 68 74 74 70 3a 2f 2f 77 77 77 2e 66 6f 73 73 "http://www.foss
54d0: 69 6c 2d 73 63 6d 2e 6f 72 67 2f 22 3e 66 6f 73 il-scm.org/">fos
54e0: 73 69 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f sil configuratio
54f0: 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 0a 2a 2a 20 n management.**
5500: 73 79 73 74 65 6d 3c 2f 61 3e 2e 20 20 54 68 65 system</a>. The
5510: 20 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 SQLITE_SOURCE_I
5520: 44 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20 61 20 D.** macro is a
5530: 73 74 72 69 6e 67 20 77 68 69 63 68 20 69 64 65 string which ide
5540: 6e 74 69 66 69 65 73 20 61 20 70 61 72 74 69 63 ntifies a partic
5550: 75 6c 61 72 20 63 68 65 63 6b 2d 69 6e 20 6f 66 ular check-in of
5560: 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 74 68 69 SQLite.** withi
5570: 6e 20 69 74 73 20 63 6f 6e 66 69 67 75 72 61 74 n its configurat
5580: 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73 ion management s
5590: 79 73 74 65 6d 2e 20 20 54 68 65 20 73 74 72 69 ystem. The stri
55a0: 6e 67 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a ng contains the.
55b0: 2a 2a 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 ** date and time
55c0: 20 6f 66 20 74 68 65 20 63 68 65 63 6b 2d 69 6e of the check-in
55d0: 20 28 55 54 43 29 20 61 6e 64 20 61 6e 20 53 48 (UTC) and an SH
55e0: 41 31 20 68 61 73 68 20 6f 66 20 74 68 65 20 65 A1 hash of the e
55f0: 6e 74 69 72 65 0a 2a 2a 20 73 6f 75 72 63 65 20 ntire.** source
5600: 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 tree..**.** See
5610: 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 6c also: [sqlite3_l
5620: 69 62 76 65 72 73 69 6f 6e 28 29 5d 2c 0a 2a 2a ibversion()],.**
5630: 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 [sqlite3_libver
5640: 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 5d 2c 20 sion_number()],
5650: 5b 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 [sqlite3_sourcei
5660: 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 d()],.** [sqlite
5670: 5f 76 65 72 73 69 6f 6e 28 29 5d 20 61 6e 64 20 _version()] and
5680: 5b 73 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 [sqlite_source_i
5690: 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 d()]..**.** Requ
56a0: 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 30 31 irements: [H1001
56b0: 31 5d 20 5b 48 31 30 30 31 34 5d 0a 2a 2f 0a 23 1] [H10014].*/.#
56c0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 45 define SQLITE_VE
56d0: 52 53 49 4f 4e 20 20 20 20 20 20 20 20 22 33 2e RSION "3.
56e0: 36 2e 31 38 22 0a 23 64 65 66 69 6e 65 20 53 51 6.18".#define SQ
56f0: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d LITE_VERSION_NUM
5700: 42 45 52 20 33 30 30 36 30 31 38 0a 23 64 65 66 BER 3006018.#def
5710: 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 55 52 43 ine SQLITE_SOURC
5720: 45 5f 49 44 20 20 20 20 20 20 22 32 30 30 39 2d E_ID "2009-
5730: 30 39 2d 30 39 20 31 36 3a 31 30 3a 35 31 20 66 09-09 16:10:51 f
5740: 30 63 37 32 61 35 33 63 35 64 35 37 64 37 34 38 0c72a53c5d57d748
5750: 37 62 34 38 61 30 36 61 34 30 38 31 36 31 35 33 7b48a06a40816153
5760: 66 34 37 61 61 61 63 22 0a 0a 2f 2a 0a 2a 2a 20 f47aaac"../*.**
5770: 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 54 69 CAPI3REF: Run-Ti
5780: 6d 65 20 4c 69 62 72 61 72 79 20 56 65 72 73 69 me Library Versi
5790: 6f 6e 20 4e 75 6d 62 65 72 73 20 7b 48 31 30 30 on Numbers {H100
57a0: 32 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 20 20} <S60100>.**
57b0: 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 KEYWORDS: sqlite
57c0: 33 5f 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3_version.**.**
57d0: 54 68 65 73 65 20 69 6e 74 65 72 66 61 63 65 73 These interfaces
57e0: 20 70 72 6f 76 69 64 65 20 74 68 65 20 73 61 6d provide the sam
57f0: 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 e information as
5800: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 56 45 52 the [SQLITE_VER
5810: 53 49 4f 4e 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 SION],.** [SQLIT
5820: 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 E_VERSION_NUMBER
5830: 5d 2c 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 53 ], and [SQLITE_S
5840: 4f 55 52 43 45 5f 49 44 5d 20 23 64 65 66 69 6e OURCE_ID] #defin
5850: 65 73 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 es in the header
5860: 2c 0a 2a 2a 20 62 75 74 20 61 72 65 20 61 73 73 ,.** but are ass
5870: 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
5880: 20 6c 69 62 72 61 72 79 20 69 6e 73 74 65 61 64 library instead
5890: 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 66 of the header f
58a0: 69 6c 65 2e 20 20 43 61 75 74 69 6f 75 73 0a 2a ile. Cautious.*
58b0: 2a 20 70 72 6f 67 72 61 6d 6d 65 72 73 20 6d 69 * programmers mi
58c0: 67 68 74 20 69 6e 63 6c 75 64 65 20 61 73 73 65 ght include asse
58d0: 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 rt() statements
58e0: 69 6e 20 74 68 65 69 72 20 61 70 70 6c 69 63 61 in their applica
58f0: 74 69 6f 6e 20 74 6f 0a 2a 2a 20 76 65 72 69 66 tion to.** verif
5900: 79 20 74 68 61 74 20 76 61 6c 75 65 73 20 72 65 y that values re
5910: 74 75 72 6e 65 64 20 62 79 20 74 68 65 73 65 20 turned by these
5920: 69 6e 74 65 72 66 61 63 65 73 20 6d 61 74 63 68 interfaces match
5930: 20 74 68 65 20 6d 61 63 72 6f 73 20 69 6e 0a 2a the macros in.*
5940: 2a 20 74 68 65 20 68 65 61 64 65 72 2c 20 61 6e * the header, an
5950: 64 20 74 68 75 73 20 69 6e 73 75 72 65 20 74 68 d thus insure th
5960: 61 74 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 at the applicati
5970: 6f 6e 20 69 73 0a 2a 2a 20 63 6f 6d 70 69 6c 65 on is.** compile
5980: 64 20 77 69 74 68 20 6d 61 74 63 68 69 6e 67 20 d with matching
5990: 6c 69 62 72 61 72 79 20 61 6e 64 20 68 65 61 64 library and head
59a0: 65 72 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 er files..**.**
59b0: 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 <blockquote><pre
59c0: 3e 0a 2a 2a 20 61 73 73 65 72 74 28 20 73 71 6c >.** assert( sql
59d0: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f ite3_libversion_
59e0: 6e 75 6d 62 65 72 28 29 3d 3d 53 51 4c 49 54 45 number()==SQLITE
59f0: 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 _VERSION_NUMBER
5a00: 29 3b 0a 2a 2a 20 61 73 73 65 72 74 28 20 73 74 );.** assert( st
5a10: 72 63 6d 70 28 73 71 6c 69 74 65 33 5f 73 6f 75 rcmp(sqlite3_sou
5a20: 72 63 65 69 64 28 29 2c 53 51 4c 49 54 45 5f 53 rceid(),SQLITE_S
5a30: 4f 55 52 43 45 5f 49 44 29 3d 3d 30 20 29 3b 0a OURCE_ID)==0 );.
5a40: 2a 2a 20 61 73 73 65 72 74 28 20 73 74 72 63 6d ** assert( strcm
5a50: 70 28 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 p(sqlite3_libver
5a60: 73 69 6f 6e 2c 53 51 4c 49 54 45 5f 56 45 52 53 sion,SQLITE_VERS
5a70: 49 4f 4e 29 3d 3d 30 20 29 3b 0a 2a 2a 20 3c 2f ION)==0 );.** </
5a80: 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 pre></blockquote
5a90: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
5aa0: 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29 te3_libversion()
5ab0: 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
5ac0: 73 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72 s the same infor
5ad0: 6d 61 74 69 6f 6e 20 61 73 20 69 73 0a 2a 2a 20 mation as is.**
5ae0: 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 in the sqlite3_v
5af0: 65 72 73 69 6f 6e 5b 5d 20 73 74 72 69 6e 67 20 ersion[] string
5b00: 63 6f 6e 73 74 61 6e 74 2e 20 20 54 68 65 20 66 constant. The f
5b10: 75 6e 63 74 69 6f 6e 20 69 73 20 70 72 6f 76 69 unction is provi
5b20: 64 65 64 0a 2a 2a 20 66 6f 72 20 75 73 65 20 69 ded.** for use i
5b30: 6e 20 44 4c 4c 73 20 73 69 6e 63 65 20 44 4c 4c n DLLs since DLL
5b40: 20 75 73 65 72 73 20 75 73 75 61 6c 6c 79 20 64 users usually d
5b50: 6f 20 6e 6f 74 20 68 61 76 65 20 64 69 72 65 63 o not have direc
5b60: 74 20 61 63 63 65 73 73 20 74 6f 20 73 74 72 69 t access to stri
5b70: 6e 67 0a 2a 2a 20 63 6f 6e 73 74 61 6e 74 73 20 ng.** constants
5b80: 77 69 74 68 69 6e 20 74 68 65 20 44 4c 4c 2e 20 within the DLL.
5b90: 20 53 69 6d 69 6c 61 72 6c 79 2c 20 74 68 65 20 Similarly, the
5ba0: 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 sqlite3_sourceid
5bb0: 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 72 () function.** r
5bc0: 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 eturns the same
5bd0: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 20 69 information as i
5be0: 73 20 69 6e 20 74 68 65 20 5b 53 51 4c 49 54 45 s in the [SQLITE
5bf0: 5f 53 4f 55 52 43 45 5f 49 44 5d 20 23 64 65 66 _SOURCE_ID] #def
5c00: 69 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 68 65 ine of.** the he
5c10: 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a ader file..**.**
5c20: 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 See also: [sqli
5c30: 74 65 5f 76 65 72 73 69 6f 6e 28 29 5d 20 61 6e te_version()] an
5c40: 64 20 5b 73 71 6c 69 74 65 5f 73 6f 75 72 63 65 d [sqlite_source
5c50: 5f 69 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 _id()]..**.** Re
5c60: 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 quirements: [H10
5c70: 30 32 31 5d 20 5b 48 31 30 30 32 32 5d 20 5b 48 021] [H10022] [H
5c80: 31 30 30 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 10023].*/.SQLITE
5c90: 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 _API const char
5ca0: 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b sqlite3_version[
5cb0: 5d 20 3d 20 53 51 4c 49 54 45 5f 56 45 52 53 49 ] = SQLITE_VERSI
5cc0: 4f 4e 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 ON;.SQLITE_API c
5cd0: 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
5ce0: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 76 6f e3_libversion(vo
5cf0: 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 id);.SQLITE_API
5d00: 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
5d10: 74 65 33 5f 73 6f 75 72 63 65 69 64 28 76 6f 69 te3_sourceid(voi
5d20: 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 d);.SQLITE_API i
5d30: 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 nt sqlite3_libve
5d40: 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 rsion_number(voi
5d50: 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 d);../*.** CAPI3
5d60: 52 45 46 3a 20 54 65 73 74 20 54 6f 20 53 65 65 REF: Test To See
5d70: 20 49 66 20 54 68 65 20 4c 69 62 72 61 72 79 20 If The Library
5d80: 49 73 20 54 68 72 65 61 64 73 61 66 65 20 7b 48 Is Threadsafe {H
5d90: 31 30 31 30 30 7d 20 3c 53 36 30 31 30 30 3e 0a 10100} <S60100>.
5da0: 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61 6e **.** SQLite can
5db0: 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 be compiled wit
5dc0: 68 20 6f 72 20 77 69 74 68 6f 75 74 20 6d 75 74 h or without mut
5dd0: 65 78 65 73 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 exes. When.** t
5de0: 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 he [SQLITE_THREA
5df0: 44 53 41 46 45 5d 20 43 20 70 72 65 70 72 6f 63 DSAFE] C preproc
5e00: 65 73 73 6f 72 20 6d 61 63 72 6f 20 69 73 20 31 essor macro is 1
5e10: 20 6f 72 20 32 2c 20 6d 75 74 65 78 65 73 0a 2a or 2, mutexes.*
5e20: 2a 20 61 72 65 20 65 6e 61 62 6c 65 64 20 61 6e * are enabled an
5e30: 64 20 53 51 4c 69 74 65 20 69 73 20 74 68 72 65 d SQLite is thre
5e40: 61 64 73 61 66 65 2e 20 20 57 68 65 6e 20 74 68 adsafe. When th
5e50: 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 54 48 52 e.** [SQLITE_THR
5e60: 45 41 44 53 41 46 45 5d 20 6d 61 63 72 6f 20 69 EADSAFE] macro i
5e70: 73 20 30 2c 20 0a 2a 2a 20 74 68 65 20 6d 75 74 s 0, .** the mut
5e80: 65 78 65 73 20 61 72 65 20 6f 6d 69 74 74 65 64 exes are omitted
5e90: 2e 20 20 57 69 74 68 6f 75 74 20 74 68 65 20 6d . Without the m
5ea0: 75 74 65 78 65 73 2c 20 69 74 20 69 73 20 6e 6f utexes, it is no
5eb0: 74 20 73 61 66 65 0a 2a 2a 20 74 6f 20 75 73 65 t safe.** to use
5ec0: 20 53 51 4c 69 74 65 20 63 6f 6e 63 75 72 72 65 SQLite concurre
5ed0: 6e 74 6c 79 20 66 72 6f 6d 20 6d 6f 72 65 20 74 ntly from more t
5ee0: 68 61 6e 20 6f 6e 65 20 74 68 72 65 61 64 2e 0a han one thread..
5ef0: 2a 2a 0a 2a 2a 20 45 6e 61 62 6c 69 6e 67 20 6d **.** Enabling m
5f00: 75 74 65 78 65 73 20 69 6e 63 75 72 73 20 61 20 utexes incurs a
5f10: 6d 65 61 73 75 72 61 62 6c 65 20 70 65 72 66 6f measurable perfo
5f20: 72 6d 61 6e 63 65 20 70 65 6e 61 6c 74 79 2e 0a rmance penalty..
5f30: 2a 2a 20 53 6f 20 69 66 20 73 70 65 65 64 20 69 ** So if speed i
5f40: 73 20 6f 66 20 75 74 6d 6f 73 74 20 69 6d 70 6f s of utmost impo
5f50: 72 74 61 6e 63 65 2c 20 69 74 20 6d 61 6b 65 73 rtance, it makes
5f60: 20 73 65 6e 73 65 20 74 6f 20 64 69 73 61 62 6c sense to disabl
5f70: 65 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 65 73 e.** the mutexes
5f80: 2e 20 20 42 75 74 20 66 6f 72 20 6d 61 78 69 6d . But for maxim
5f90: 75 6d 20 73 61 66 65 74 79 2c 20 6d 75 74 65 78 um safety, mutex
5fa0: 65 73 20 73 68 6f 75 6c 64 20 62 65 20 65 6e 61 es should be ena
5fb0: 62 6c 65 64 2e 0a 2a 2a 20 54 68 65 20 64 65 66 bled..** The def
5fc0: 61 75 6c 74 20 62 65 68 61 76 69 6f 72 20 69 73 ault behavior is
5fd0: 20 66 6f 72 20 6d 75 74 65 78 65 73 20 74 6f 20 for mutexes to
5fe0: 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a be enabled..**.*
5ff0: 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 * This interface
6000: 20 63 61 6e 20 62 65 20 75 73 65 64 20 62 79 20 can be used by
6010: 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 an application t
6020: 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 o make sure that
6030: 20 74 68 65 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 the.** version
6040: 6f 66 20 53 51 4c 69 74 65 20 74 68 61 74 20 69 of SQLite that i
6050: 74 20 69 73 20 6c 69 6e 6b 69 6e 67 20 61 67 61 t is linking aga
6060: 69 6e 73 74 20 77 61 73 20 63 6f 6d 70 69 6c 65 inst was compile
6070: 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 64 65 d with.** the de
6080: 73 69 72 65 64 20 73 65 74 74 69 6e 67 20 6f 66 sired setting of
6090: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 the [SQLITE_THR
60a0: 45 41 44 53 41 46 45 5d 20 6d 61 63 72 6f 2e 0a EADSAFE] macro..
60b0: 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 **.** This inter
60c0: 66 61 63 65 20 6f 6e 6c 79 20 72 65 70 6f 72 74 face only report
60d0: 73 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 s on the compile
60e0: 2d 74 69 6d 65 20 6d 75 74 65 78 20 73 65 74 74 -time mutex sett
60f0: 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 53 ing.** of the [S
6100: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
6110: 5d 20 66 6c 61 67 2e 20 20 49 66 20 53 51 4c 69 ] flag. If SQLi
6120: 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 te is compiled w
6130: 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 48 ith.** SQLITE_TH
6140: 52 45 41 44 53 41 46 45 3d 31 20 74 68 65 6e 20 READSAFE=1 then
6150: 6d 75 74 65 78 65 73 20 61 72 65 20 65 6e 61 62 mutexes are enab
6160: 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 20 62 led by default b
6170: 75 74 0a 2a 2a 20 63 61 6e 20 62 65 20 66 75 6c ut.** can be ful
6180: 6c 79 20 6f 72 20 70 61 72 74 69 61 6c 6c 79 20 ly or partially
6190: 64 69 73 61 62 6c 65 64 20 75 73 69 6e 67 20 61 disabled using a
61a0: 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
61b0: 33 5f 63 6f 6e 66 69 67 28 29 5d 0a 2a 2a 20 77 3_config()].** w
61c0: 69 74 68 20 74 68 65 20 76 65 72 62 73 20 5b 53 ith the verbs [S
61d0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e QLITE_CONFIG_SIN
61e0: 47 4c 45 54 48 52 45 41 44 5d 2c 20 5b 53 51 4c GLETHREAD], [SQL
61f0: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 ITE_CONFIG_MULTI
6200: 54 48 52 45 41 44 5d 2c 0a 2a 2a 20 6f 72 20 5b THREAD],.** or [
6210: 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 SQLITE_CONFIG_MU
6220: 54 45 58 5d 2e 20 20 54 68 65 20 72 65 74 75 72 TEX]. The retur
6230: 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 n value of this
6240: 66 75 6e 63 74 69 6f 6e 20 73 68 6f 77 73 0a 2a function shows.*
6250: 2a 20 6f 6e 6c 79 20 74 68 65 20 64 65 66 61 75 * only the defau
6260: 6c 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 lt compile-time
6270: 73 65 74 74 69 6e 67 2c 20 6e 6f 74 20 61 6e 79 setting, not any
6280: 20 72 75 6e 2d 74 69 6d 65 20 63 68 61 6e 67 65 run-time change
6290: 73 0a 2a 2a 20 74 6f 20 74 68 61 74 20 73 65 74 s.** to that set
62a0: 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 ting..**.** See
62b0: 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d the [threading m
62c0: 6f 64 65 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 ode] documentati
62d0: 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 on for additiona
62e0: 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a l information..*
62f0: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
6300: 73 3a 20 5b 48 31 30 31 30 31 5d 20 5b 48 31 30 s: [H10101] [H10
6310: 31 30 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 102].*/.SQLITE_A
6320: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 PI int sqlite3_t
6330: 68 72 65 61 64 73 61 66 65 28 76 6f 69 64 29 3b hreadsafe(void);
6340: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
6350: 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 : Database Conne
6360: 63 74 69 6f 6e 20 48 61 6e 64 6c 65 20 7b 48 31 ction Handle {H1
6370: 32 30 30 30 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2000} <S40200>.*
6380: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 64 61 74 * KEYWORDS: {dat
6390: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
63a0: 7d 20 7b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e } {database conn
63b0: 65 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 ections}.**.** E
63c0: 61 63 68 20 6f 70 65 6e 20 53 51 4c 69 74 65 20 ach open SQLite
63d0: 64 61 74 61 62 61 73 65 20 69 73 20 72 65 70 72 database is repr
63e0: 65 73 65 6e 74 65 64 20 62 79 20 61 20 70 6f 69 esented by a poi
63f0: 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 nter to an insta
6400: 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6f 70 nce of.** the op
6410: 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 6e aque structure n
6420: 61 6d 65 64 20 22 73 71 6c 69 74 65 33 22 2e 20 amed "sqlite3".
6430: 20 49 74 20 69 73 20 75 73 65 66 75 6c 20 74 6f It is useful to
6440: 20 74 68 69 6e 6b 20 6f 66 20 61 6e 20 73 71 6c think of an sql
6450: 69 74 65 33 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 ite3.** pointer
6460: 61 73 20 61 6e 20 6f 62 6a 65 63 74 2e 20 20 54 as an object. T
6470: 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e he [sqlite3_open
6480: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 ()], [sqlite3_op
6490: 65 6e 31 36 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 en16()], and.**
64a0: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 [sqlite3_open_v2
64b0: 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 61 ()] interfaces a
64c0: 72 65 20 69 74 73 20 63 6f 6e 73 74 72 75 63 74 re its construct
64d0: 6f 72 73 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 ors, and [sqlite
64e0: 33 5f 63 6c 6f 73 65 28 29 5d 0a 2a 2a 20 69 73 3_close()].** is
64f0: 20 69 74 73 20 64 65 73 74 72 75 63 74 6f 72 2e its destructor.
6500: 20 20 54 68 65 72 65 20 61 72 65 20 6d 61 6e 79 There are many
6510: 20 6f 74 68 65 72 20 69 6e 74 65 72 66 61 63 65 other interface
6520: 73 20 28 73 75 63 68 20 61 73 0a 2a 2a 20 5b 73 s (such as.** [s
6530: 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
6540: 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 2()], [sqlite3_c
6550: 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 reate_function()
6560: 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 ], and.** [sqlit
6570: 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 e3_busy_timeout(
6580: 29 5d 20 74 6f 20 6e 61 6d 65 20 62 75 74 20 74 )] to name but t
6590: 68 72 65 65 29 20 74 68 61 74 20 61 72 65 20 6d hree) that are m
65a0: 65 74 68 6f 64 73 20 6f 6e 20 61 6e 0a 2a 2a 20 ethods on an.**
65b0: 73 71 6c 69 74 65 33 20 6f 62 6a 65 63 74 2e 0a sqlite3 object..
65c0: 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
65d0: 74 20 73 71 6c 69 74 65 33 20 73 71 6c 69 74 65 t sqlite3 sqlite
65e0: 33 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 3;../*.** CAPI3R
65f0: 45 46 3a 20 36 34 2d 42 69 74 20 49 6e 74 65 67 EF: 64-Bit Integ
6600: 65 72 20 54 79 70 65 73 20 7b 48 31 30 32 30 30 er Types {H10200
6610: 7d 20 3c 53 31 30 31 31 30 3e 0a 2a 2a 20 4b 45 } <S10110>.** KE
6620: 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 5f 69 YWORDS: sqlite_i
6630: 6e 74 36 34 20 73 71 6c 69 74 65 5f 75 69 6e 74 nt64 sqlite_uint
6640: 36 34 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65 64.**.** Because
6650: 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 72 6f there is no cro
6660: 73 73 2d 70 6c 61 74 66 6f 72 6d 20 77 61 79 20 ss-platform way
6670: 74 6f 20 73 70 65 63 69 66 79 20 36 34 2d 62 69 to specify 64-bi
6680: 74 20 69 6e 74 65 67 65 72 20 74 79 70 65 73 0a t integer types.
6690: 2a 2a 20 53 51 4c 69 74 65 20 69 6e 63 6c 75 64 ** SQLite includ
66a0: 65 73 20 74 79 70 65 64 65 66 73 20 66 6f 72 20 es typedefs for
66b0: 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 61 6e 64-bit signed an
66c0: 64 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 d unsigned integ
66d0: 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ers..**.** The s
66e0: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 61 6e 64 qlite3_int64 and
66f0: 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 sqlite3_uint64
6700: 61 72 65 20 74 68 65 20 70 72 65 66 65 72 72 65 are the preferre
6710: 64 20 74 79 70 65 20 64 65 66 69 6e 69 74 69 6f d type definitio
6720: 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ns..** The sqlit
6730: 65 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 6c 69 e_int64 and sqli
6740: 74 65 5f 75 69 6e 74 36 34 20 74 79 70 65 73 20 te_uint64 types
6750: 61 72 65 20 73 75 70 70 6f 72 74 65 64 20 66 6f are supported fo
6760: 72 20 62 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63 r backwards.** c
6770: 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c ompatibility onl
6780: 79 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 y..**.** Require
6790: 6d 65 6e 74 73 3a 20 5b 48 31 30 32 30 31 5d 20 ments: [H10201]
67a0: 5b 48 31 30 32 30 32 5d 0a 2a 2f 0a 23 69 66 64 [H10202].*/.#ifd
67b0: 65 66 20 53 51 4c 49 54 45 5f 49 4e 54 36 34 5f ef SQLITE_INT64_
67c0: 54 59 50 45 0a 20 20 74 79 70 65 64 65 66 20 53 TYPE. typedef S
67d0: 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 QLITE_INT64_TYPE
67e0: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 sqlite_int64;.
67f0: 20 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 typedef unsigne
6800: 64 20 53 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 d SQLITE_INT64_T
6810: 59 50 45 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 YPE sqlite_uint6
6820: 34 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 4;.#elif defined
6830: 28 5f 4d 53 43 5f 56 45 52 29 20 7c 7c 20 64 65 (_MSC_VER) || de
6840: 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 fined(__BORLANDC
6850: 5f 5f 29 0a 20 20 74 79 70 65 64 65 66 20 5f 5f __). typedef __
6860: 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 69 6e 74 int64 sqlite_int
6870: 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 6e 64;. typedef un
6880: 73 69 67 6e 65 64 20 5f 5f 69 6e 74 36 34 20 73 signed __int64 s
6890: 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 qlite_uint64;.#e
68a0: 6c 73 65 0a 20 20 74 79 70 65 64 65 66 20 6c 6f lse. typedef lo
68b0: 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 73 71 6c 69 ng long int sqli
68c0: 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 te_int64;. type
68d0: 64 65 66 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e def unsigned lon
68e0: 67 20 6c 6f 6e 67 20 69 6e 74 20 73 71 6c 69 74 g long int sqlit
68f0: 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6e 64 69 66 e_uint64;.#endif
6900: 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f .typedef sqlite_
6910: 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 69 6e int64 sqlite3_in
6920: 74 36 34 3b 0a 74 79 70 65 64 65 66 20 73 71 6c t64;.typedef sql
6930: 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 ite_uint64 sqlit
6940: 65 33 5f 75 69 6e 74 36 34 3b 0a 0a 2f 2a 0a 2a e3_uint64;../*.*
6950: 2a 20 49 66 20 63 6f 6d 70 69 6c 69 6e 67 20 66 * If compiling f
6960: 6f 72 20 61 20 70 72 6f 63 65 73 73 6f 72 20 74 or a processor t
6970: 68 61 74 20 6c 61 63 6b 73 20 66 6c 6f 61 74 69 hat lacks floati
6980: 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 ng point support
6990: 2c 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 65 20 ,.** substitute
69a0: 69 6e 74 65 67 65 72 20 66 6f 72 20 66 6c 6f 61 integer for floa
69b0: 74 69 6e 67 2d 70 6f 69 6e 74 2e 0a 2a 2f 0a 23 ting-point..*/.#
69c0: 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
69d0: 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
69e0: 0a 23 20 64 65 66 69 6e 65 20 64 6f 75 62 6c 65 .# define double
69f0: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 0a 23 sqlite3_int64.#
6a00: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 endif../*.** CAP
6a10: 49 33 52 45 46 3a 20 43 6c 6f 73 69 6e 67 20 41 I3REF: Closing A
6a20: 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 Database Connec
6a30: 74 69 6f 6e 20 7b 48 31 32 30 31 30 7d 20 3c 53 tion {H12010} <S
6a40: 33 30 31 30 30 3e 3c 53 34 30 32 30 30 3e 0a 2a 30100><S40200>.*
6a50: 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
6a60: 65 20 69 73 20 74 68 65 20 64 65 73 74 72 75 63 e is the destruc
6a70: 74 6f 72 20 66 6f 72 20 74 68 65 20 5b 73 71 6c tor for the [sql
6a80: 69 74 65 33 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a ite3] object..**
6a90: 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 .** Applications
6aa0: 20 73 68 6f 75 6c 64 20 5b 73 71 6c 69 74 65 33 should [sqlite3
6ab0: 5f 66 69 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61 _finalize | fina
6ac0: 6c 69 7a 65 5d 20 61 6c 6c 20 5b 70 72 65 70 61 lize] all [prepa
6ad0: 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 0a red statements].
6ae0: 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f ** and [sqlite3_
6af0: 62 6c 6f 62 5f 63 6c 6f 73 65 20 7c 20 63 6c 6f blob_close | clo
6b00: 73 65 5d 20 61 6c 6c 20 5b 42 4c 4f 42 20 68 61 se] all [BLOB ha
6b10: 6e 64 6c 65 73 5d 20 61 73 73 6f 63 69 61 74 65 ndles] associate
6b20: 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 5b 73 d with.** the [s
6b30: 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 20 70 qlite3] object p
6b40: 72 69 6f 72 20 74 6f 20 61 74 74 65 6d 70 74 69 rior to attempti
6b50: 6e 67 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 ng to close the
6b60: 6f 62 6a 65 63 74 2e 0a 2a 2a 20 54 68 65 20 5b object..** The [
6b70: 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d sqlite3_next_stm
6b80: 74 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 t()] interface c
6b90: 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6c 6f an be used to lo
6ba0: 63 61 74 65 20 61 6c 6c 0a 2a 2a 20 5b 70 72 65 cate all.** [pre
6bb0: 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 pared statements
6bc0: 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 ] associated wit
6bd0: 68 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f h a [database co
6be0: 6e 6e 65 63 74 69 6f 6e 5d 20 69 66 20 64 65 73 nnection] if des
6bf0: 69 72 65 64 2e 0a 2a 2a 20 54 79 70 69 63 61 6c ired..** Typical
6c00: 20 63 6f 64 65 20 6d 69 67 68 74 20 6c 6f 6f 6b code might look
6c10: 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a like this:.**.*
6c20: 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 * <blockquote><p
6c30: 72 65 3e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 re>.** sqlite3_s
6c40: 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 2a 2a 20 77 tmt *pStmt;.** w
6c50: 68 69 6c 65 28 20 28 70 53 74 6d 74 20 3d 20 73 hile( (pStmt = s
6c60: 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 qlite3_next_stmt
6c70: 28 64 62 2c 20 30 29 29 21 3d 30 20 29 7b 0a 2a (db, 0))!=0 ){.*
6c80: 2a 20 26 6e 62 73 70 3b 20 20 20 73 71 6c 69 74 * sqlit
6c90: 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d e3_finalize(pStm
6ca0: 74 29 3b 0a 2a 2a 20 7d 0a 2a 2a 20 3c 2f 70 72 t);.** }.** </pr
6cb0: 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a e></blockquote>.
6cc0: 2a 2a 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 74 65 **.** If [sqlite
6cd0: 33 5f 63 6c 6f 73 65 28 29 5d 20 69 73 20 69 6e 3_close()] is in
6ce0: 76 6f 6b 65 64 20 77 68 69 6c 65 20 61 20 74 72 voked while a tr
6cf0: 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 ansaction is ope
6d00: 6e 2c 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 n,.** the transa
6d10: 63 74 69 6f 6e 20 69 73 20 61 75 74 6f 6d 61 74 ction is automat
6d20: 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 ically rolled ba
6d30: 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 20 ck..**.** The C
6d40: 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 parameter to [sq
6d50: 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 lite3_close(C)]
6d60: 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 61 must be either a
6d70: 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 NULL.** pointer
6d80: 20 6f 72 20 61 6e 20 5b 73 71 6c 69 74 65 33 5d or an [sqlite3]
6d90: 20 6f 62 6a 65 63 74 20 70 6f 69 6e 74 65 72 20 object pointer
6da0: 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d obtained.** from
6db0: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 [sqlite3_open()
6dc0: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e ], [sqlite3_open
6dd0: 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 16()], or.** [sq
6de0: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d lite3_open_v2()]
6df0: 2c 20 61 6e 64 20 6e 6f 74 20 70 72 65 76 69 6f , and not previo
6e00: 75 73 6c 79 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a usly closed..**.
6e10: 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
6e20: 0a 2a 2a 20 5b 48 31 32 30 31 31 5d 20 5b 48 31 .** [H12011] [H1
6e30: 32 30 31 32 5d 20 5b 48 31 32 30 31 33 5d 20 5b 2012] [H12013] [
6e40: 48 31 32 30 31 34 5d 20 5b 48 31 32 30 31 35 5d H12014] [H12015]
6e50: 20 5b 48 31 32 30 31 39 5d 0a 2a 2f 0a 53 51 4c [H12019].*/.SQL
6e60: 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
6e70: 74 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 te3_close(sqlite
6e80: 33 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 3 *);../*.** The
6e90: 20 74 79 70 65 20 66 6f 72 20 61 20 63 61 6c 6c type for a call
6ea0: 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a back function..*
6eb0: 2a 20 54 68 69 73 20 69 73 20 6c 65 67 61 63 79 * This is legacy
6ec0: 20 61 6e 64 20 64 65 70 72 65 63 61 74 65 64 2e and deprecated.
6ed0: 20 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64 It is included
6ee0: 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 0a for historical.
6ef0: 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 ** compatibility
6f00: 20 61 6e 64 20 69 73 20 6e 6f 74 20 64 6f 63 75 and is not docu
6f10: 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64 mented..*/.typed
6f20: 65 66 20 69 6e 74 20 28 2a 73 71 6c 69 74 65 33 ef int (*sqlite3
6f30: 5f 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a _callback)(void*
6f40: 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 20 63 68 61 ,int,char**, cha
6f50: 72 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 r**);../*.** CAP
6f60: 49 33 52 45 46 3a 20 4f 6e 65 2d 53 74 65 70 20 I3REF: One-Step
6f70: 51 75 65 72 79 20 45 78 65 63 75 74 69 6f 6e 20 Query Execution
6f80: 49 6e 74 65 72 66 61 63 65 20 7b 48 31 32 31 30 Interface {H1210
6f90: 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 0} <S10000>.**.*
6fa0: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 * The sqlite3_ex
6fb0: 65 63 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 ec() interface i
6fc0: 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 77 s a convenient w
6fd0: 61 79 20 6f 66 20 72 75 6e 6e 69 6e 67 20 6f 6e ay of running on
6fe0: 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 53 51 4c e or more.** SQL
6ff0: 20 73 74 61 74 65 6d 65 6e 74 73 20 77 69 74 68 statements with
7000: 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 77 72 out having to wr
7010: 69 74 65 20 61 20 6c 6f 74 20 6f 66 20 43 20 63 ite a lot of C c
7020: 6f 64 65 2e 20 20 54 68 65 20 55 54 46 2d 38 20 ode. The UTF-8
7030: 65 6e 63 6f 64 65 64 0a 2a 2a 20 53 51 4c 20 73 encoded.** SQL s
7040: 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 70 61 tatements are pa
7050: 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 73 ssed in as the s
7060: 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
7070: 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 to sqlite3_exec(
7080: 29 2e 0a 2a 2a 20 54 68 65 20 73 74 61 74 65 6d )..** The statem
7090: 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 74 ents are evaluat
70a0: 65 64 20 6f 6e 65 20 62 79 20 6f 6e 65 20 75 6e ed one by one un
70b0: 74 69 6c 20 65 69 74 68 65 72 20 61 6e 20 65 72 til either an er
70c0: 72 6f 72 20 6f 72 0a 2a 2a 20 61 6e 20 69 6e 74 ror or.** an int
70d0: 65 72 72 75 70 74 20 69 73 20 65 6e 63 6f 75 6e errupt is encoun
70e0: 74 65 72 65 64 2c 20 6f 72 20 75 6e 74 69 6c 20 tered, or until
70f0: 74 68 65 79 20 61 72 65 20 61 6c 6c 20 64 6f 6e they are all don
7100: 65 2e 20 20 54 68 65 20 33 72 64 20 70 61 72 61 e. The 3rd para
7110: 6d 65 74 65 72 0a 2a 2a 20 69 73 20 61 6e 20 6f meter.** is an o
7120: 70 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b ptional callback
7130: 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 that is invoked
7140: 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72 once for each r
7150: 6f 77 20 6f 66 20 61 6e 79 20 71 75 65 72 79 0a ow of any query.
7160: 2a 2a 20 72 65 73 75 6c 74 73 20 70 72 6f 64 75 ** results produ
7170: 63 65 64 20 62 79 20 74 68 65 20 53 51 4c 20 73 ced by the SQL s
7180: 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 tatements. The
7190: 35 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 65 5th parameter te
71a0: 6c 6c 73 20 77 68 65 72 65 0a 2a 2a 20 74 6f 20 lls where.** to
71b0: 77 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20 write any error
71c0: 6d 65 73 73 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 messages..**.**
71d0: 54 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 The error messag
71e0: 65 20 70 61 73 73 65 64 20 62 61 63 6b 20 74 68 e passed back th
71f0: 72 6f 75 67 68 20 74 68 65 20 35 74 68 20 70 61 rough the 5th pa
7200: 72 61 6d 65 74 65 72 20 69 73 20 68 65 6c 64 0a rameter is held.
7210: 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 ** in memory obt
7220: 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 ained from [sqli
7230: 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 te3_malloc()].
7240: 54 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 6f 72 To avoid a memor
7250: 79 20 6c 65 61 6b 2c 0a 2a 2a 20 74 68 65 20 63 y leak,.** the c
7260: 61 6c 6c 69 6e 67 20 61 70 70 6c 69 63 61 74 69 alling applicati
7270: 6f 6e 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 5b on should call [
7280: 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 sqlite3_free()]
7290: 6f 6e 20 61 6e 79 20 65 72 72 6f 72 0a 2a 2a 20 on any error.**
72a0: 6d 65 73 73 61 67 65 20 72 65 74 75 72 6e 65 64 message returned
72b0: 20 74 68 72 6f 75 67 68 20 74 68 65 20 35 74 68 through the 5th
72c0: 20 70 61 72 61 6d 65 74 65 72 20 77 68 65 6e 20 parameter when
72d0: 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 it has finished
72e0: 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 65 72 72 using.** the err
72f0: 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a or message..**.*
7300: 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 * If the SQL sta
7310: 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 32 6e tement in the 2n
7320: 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 4e d parameter is N
7330: 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 ULL or an empty
7340: 73 74 72 69 6e 67 0a 2a 2a 20 6f 72 20 61 20 73 string.** or a s
7350: 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 tring containing
7360: 20 6f 6e 6c 79 20 77 68 69 74 65 73 70 61 63 65 only whitespace
7370: 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2c 20 74 and comments, t
7380: 68 65 6e 20 6e 6f 20 53 51 4c 0a 2a 2a 20 73 74 hen no SQL.** st
7390: 61 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 atements are eva
73a0: 6c 75 61 74 65 64 20 61 6e 64 20 74 68 65 20 64 luated and the d
73b0: 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 63 atabase is not c
73c0: 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 hanged..**.** Th
73d0: 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 e sqlite3_exec()
73e0: 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 69 6d interface is im
73f0: 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 65 72 plemented in ter
7400: 6d 73 20 6f 66 0a 2a 2a 20 5b 73 71 6c 69 74 65 ms of.** [sqlite
7410: 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 3_prepare_v2()],
7420: 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
7430: 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f ], and [sqlite3_
7440: 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 finalize()]..**
7450: 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 The sqlite3_exec
7460: 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 () routine does
7470: 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65 20 64 nothing to the d
7480: 61 74 61 62 61 73 65 20 74 68 61 74 20 63 61 6e atabase that can
7490: 6e 6f 74 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 62 not be done.** b
74a0: 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 y [sqlite3_prepa
74b0: 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 re_v2()], [sqlit
74c0: 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e 64 20 e3_step()], and
74d0: 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a [sqlite3_finaliz
74e0: 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 e()]..**.** The
74f0: 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 first parameter
7500: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 to [sqlite3_exec
7510: 28 29 5d 20 6d 75 73 74 20 62 65 20 61 6e 20 76 ()] must be an v
7520: 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a 2a 2a alid and open.**
7530: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
7540: 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 ction]..**.** Th
7550: 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
7560: 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 62 ction must not b
7570: 65 20 63 6c 6f 73 65 64 20 77 68 69 6c 65 0a 2a e closed while.*
7580: 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 * [sqlite3_exec(
7590: 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a )] is running..*
75a0: 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 *.** The calling
75b0: 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 function should
75c0: 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f 66 72 use [sqlite3_fr
75d0: 65 65 28 29 5d 20 74 6f 20 66 72 65 65 0a 2a 2a ee()] to free.**
75e0: 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 the memory that
75f0: 20 2a 65 72 72 6d 73 67 20 69 73 20 6c 65 66 74 *errmsg is left
7600: 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 6f 6e 63 pointing at onc
7610: 65 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d e the error.** m
7620: 65 73 73 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e essage is no lon
7630: 67 65 72 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a ger needed..**.*
7640: 2a 20 54 68 65 20 53 51 4c 20 73 74 61 74 65 6d * The SQL statem
7650: 65 6e 74 20 74 65 78 74 20 69 6e 20 74 68 65 20 ent text in the
7660: 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 2nd parameter to
7670: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 [sqlite3_exec()
7680: 5d 0a 2a 2a 20 6d 75 73 74 20 72 65 6d 61 69 6e ].** must remain
7690: 20 75 6e 63 68 61 6e 67 65 64 20 77 68 69 6c 65 unchanged while
76a0: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 [sqlite3_exec()
76b0: 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a ] is running..**
76c0: 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
76d0: 3a 0a 2a 2a 20 5b 48 31 32 31 30 31 5d 20 5b 48 :.** [H12101] [H
76e0: 31 32 31 30 32 5d 20 5b 48 31 32 31 30 34 5d 20 12102] [H12104]
76f0: 5b 48 31 32 31 30 35 5d 20 5b 48 31 32 31 30 37 [H12105] [H12107
7700: 5d 20 5b 48 31 32 31 31 30 5d 20 5b 48 31 32 31 ] [H12110] [H121
7710: 31 33 5d 20 5b 48 31 32 31 31 36 5d 0a 2a 2a 20 13] [H12116].**
7720: 5b 48 31 32 31 31 39 5d 20 5b 48 31 32 31 32 32 [H12119] [H12122
7730: 5d 20 5b 48 31 32 31 32 35 5d 20 5b 48 31 32 31 ] [H12125] [H121
7740: 33 31 5d 20 5b 48 31 32 31 33 34 5d 20 5b 48 31 31] [H12134] [H1
7750: 32 31 33 37 5d 20 5b 48 31 32 31 33 38 5d 0a 2a 2137] [H12138].*
7760: 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
7770: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 0a 20 sqlite3_exec(.
7780: 20 73 71 6c 69 74 65 33 2a 2c 20 20 20 20 20 20 sqlite3*,
7790: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77a0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
77b0: 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 n open database
77c0: 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
77d0: 2a 73 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20 *sql,
77e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77f0: 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61 /* SQL to be eva
7800: 6c 75 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 luated */. int
7810: 28 2a 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 (*callback)(void
7820: 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 63 68 61 *,int,char**,cha
7830: 72 2a 2a 29 2c 20 20 2f 2a 20 43 61 6c 6c 62 61 r**), /* Callba
7840: 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 ck function */.
7850: 20 76 6f 69 64 20 2a 2c 20 20 20 20 20 20 20 20 void *,
7860: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7870: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 /* 1
7880: 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 st argument to c
7890: 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 63 68 61 allback */. cha
78a0: 72 20 2a 2a 65 72 72 6d 73 67 20 20 20 20 20 20 r **errmsg
78b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78c0: 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 /* Error
78d0: 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 65 72 msg written her
78e0: 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 e */.);../*.** C
78f0: 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c 74 20 API3REF: Result
7900: 43 6f 64 65 73 20 7b 48 31 30 32 31 30 7d 20 3c Codes {H10210} <
7910: 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f S10700>.** KEYWO
7920: 52 44 53 3a 20 53 51 4c 49 54 45 5f 4f 4b 20 7b RDS: SQLITE_OK {
7930: 65 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 72 72 error code} {err
7940: 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59 or codes}.** KEY
7950: 57 4f 52 44 53 3a 20 7b 72 65 73 75 6c 74 20 63 WORDS: {result c
7960: 6f 64 65 7d 20 7b 72 65 73 75 6c 74 20 63 6f 64 ode} {result cod
7970: 65 73 7d 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 53 es}.**.** Many S
7980: 51 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 QLite functions
7990: 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 return an intege
79a0: 72 20 72 65 73 75 6c 74 20 63 6f 64 65 20 66 72 r result code fr
79b0: 6f 6d 20 74 68 65 20 73 65 74 20 73 68 6f 77 6e om the set shown
79c0: 0a 2a 2a 20 68 65 72 65 20 69 6e 20 6f 72 64 65 .** here in orde
79d0: 72 20 74 6f 20 69 6e 64 69 63 61 74 65 73 20 73 r to indicates s
79e0: 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72 uccess or failur
79f0: 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 65 72 72 e..**.** New err
7a00: 6f 72 20 63 6f 64 65 73 20 6d 61 79 20 62 65 20 or codes may be
7a10: 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 added in future
7a20: 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 versions of SQLi
7a30: 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c te..**.** See al
7a40: 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 so: [SQLITE_IOER
7a50: 52 5f 52 45 41 44 20 7c 20 65 78 74 65 6e 64 65 R_READ | extende
7a60: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 0a d result codes].
7a70: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
7a80: 45 5f 4f 4b 20 20 20 20 20 20 20 20 20 20 20 30 E_OK 0
7a90: 20 20 20 2f 2a 20 53 75 63 63 65 73 73 66 75 6c /* Successful
7aa0: 20 72 65 73 75 6c 74 20 2a 2f 0a 2f 2a 20 62 65 result */./* be
7ab0: 67 69 6e 6e 69 6e 67 2d 6f 66 2d 65 72 72 6f 72 ginning-of-error
7ac0: 2d 63 6f 64 65 73 20 2a 2f 0a 23 64 65 66 69 6e -codes */.#defin
7ad0: 65 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 20 e SQLITE_ERROR
7ae0: 20 20 20 20 20 20 31 20 20 20 2f 2a 20 53 51 4c 1 /* SQL
7af0: 20 65 72 72 6f 72 20 6f 72 20 6d 69 73 73 69 6e error or missin
7b00: 67 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 g database */.#d
7b10: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 efine SQLITE_INT
7b20: 45 52 4e 41 4c 20 20 20 20 20 32 20 20 20 2f 2a ERNAL 2 /*
7b30: 20 49 6e 74 65 72 6e 61 6c 20 6c 6f 67 69 63 20 Internal logic
7b40: 65 72 72 6f 72 20 69 6e 20 53 51 4c 69 74 65 20 error in SQLite
7b50: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
7b60: 45 5f 50 45 52 4d 20 20 20 20 20 20 20 20 20 33 E_PERM 3
7b70: 20 20 20 2f 2a 20 41 63 63 65 73 73 20 70 65 72 /* Access per
7b80: 6d 69 73 73 69 6f 6e 20 64 65 6e 69 65 64 20 2a mission denied *
7b90: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
7ba0: 5f 41 42 4f 52 54 20 20 20 20 20 20 20 20 34 20 _ABORT 4
7bb0: 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 72 6f /* Callback ro
7bc0: 75 74 69 6e 65 20 72 65 71 75 65 73 74 65 64 20 utine requested
7bd0: 61 6e 20 61 62 6f 72 74 20 2a 2f 0a 23 64 65 66 an abort */.#def
7be0: 69 6e 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20 ine SQLITE_BUSY
7bf0: 20 20 20 20 20 20 20 20 35 20 20 20 2f 2a 20 54 5 /* T
7c00: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
7c10: 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64 is locked */.#d
7c20: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 efine SQLITE_LOC
7c30: 4b 45 44 20 20 20 20 20 20 20 36 20 20 20 2f 2a KED 6 /*
7c40: 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 A table in the
7c50: 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b database is lock
7c60: 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ed */.#define SQ
7c70: 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 LITE_NOMEM
7c80: 20 20 37 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 7 /* A mallo
7c90: 63 28 29 20 66 61 69 6c 65 64 20 2a 2f 0a 23 64 c() failed */.#d
7ca0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 41 efine SQLITE_REA
7cb0: 44 4f 4e 4c 59 20 20 20 20 20 38 20 20 20 2f 2a DONLY 8 /*
7cc0: 20 41 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 Attempt to writ
7cd0: 65 20 61 20 72 65 61 64 6f 6e 6c 79 20 64 61 74 e a readonly dat
7ce0: 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 abase */.#define
7cf0: 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 SQLITE_INTERRUP
7d00: 54 20 20 20 20 39 20 20 20 2f 2a 20 4f 70 65 72 T 9 /* Oper
7d10: 61 74 69 6f 6e 20 74 65 72 6d 69 6e 61 74 65 64 ation terminated
7d20: 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 by sqlite3_inte
7d30: 72 72 75 70 74 28 29 2a 2f 0a 23 64 65 66 69 6e rrupt()*/.#defin
7d40: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 20 e SQLITE_IOERR
7d50: 20 20 20 20 20 31 30 20 20 20 2f 2a 20 53 6f 6d 10 /* Som
7d60: 65 20 6b 69 6e 64 20 6f 66 20 64 69 73 6b 20 49 e kind of disk I
7d70: 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 /O error occurre
7d80: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c d */.#define SQL
7d90: 49 54 45 5f 43 4f 52 52 55 50 54 20 20 20 20 20 ITE_CORRUPT
7da0: 31 31 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 11 /* The data
7db0: 62 61 73 65 20 64 69 73 6b 20 69 6d 61 67 65 20 base disk image
7dc0: 69 73 20 6d 61 6c 66 6f 72 6d 65 64 20 2a 2f 0a is malformed */.
7dd0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e #define SQLITE_N
7de0: 4f 54 46 4f 55 4e 44 20 20 20 20 31 32 20 20 20 OTFOUND 12
7df0: 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 54 61 62 /* NOT USED. Tab
7e00: 6c 65 20 6f 72 20 72 65 63 6f 72 64 20 6e 6f 74 le or record not
7e10: 20 66 6f 75 6e 64 20 2a 2f 0a 23 64 65 66 69 6e found */.#defin
7e20: 65 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 20 20 e SQLITE_FULL
7e30: 20 20 20 20 20 31 33 20 20 20 2f 2a 20 49 6e 73 13 /* Ins
7e40: 65 72 74 69 6f 6e 20 66 61 69 6c 65 64 20 62 65 ertion failed be
7e50: 63 61 75 73 65 20 64 61 74 61 62 61 73 65 20 69 cause database i
7e60: 73 20 66 75 6c 6c 20 2a 2f 0a 23 64 65 66 69 6e s full */.#defin
7e70: 65 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 e SQLITE_CANTOPE
7e80: 4e 20 20 20 20 31 34 20 20 20 2f 2a 20 55 6e 61 N 14 /* Una
7e90: 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 ble to open the
7ea0: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
7eb0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
7ec0: 50 52 4f 54 4f 43 4f 4c 20 20 20 20 31 35 20 20 PROTOCOL 15
7ed0: 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 44 61 /* NOT USED. Da
7ee0: 74 61 62 61 73 65 20 6c 6f 63 6b 20 70 72 6f 74 tabase lock prot
7ef0: 6f 63 6f 6c 20 65 72 72 6f 72 20 2a 2f 0a 23 64 ocol error */.#d
7f00: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4d 50 efine SQLITE_EMP
7f10: 54 59 20 20 20 20 20 20 20 31 36 20 20 20 2f 2a TY 16 /*
7f20: 20 44 61 74 61 62 61 73 65 20 69 73 20 65 6d 70 Database is emp
7f30: 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ty */.#define SQ
7f40: 4c 49 54 45 5f 53 43 48 45 4d 41 20 20 20 20 20 LITE_SCHEMA
7f50: 20 31 37 20 20 20 2f 2a 20 54 68 65 20 64 61 74 17 /* The dat
7f60: 61 62 61 73 65 20 73 63 68 65 6d 61 20 63 68 61 abase schema cha
7f70: 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 nged */.#define
7f80: 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 20 20 SQLITE_TOOBIG
7f90: 20 20 20 31 38 20 20 20 2f 2a 20 53 74 72 69 6e 18 /* Strin
7fa0: 67 20 6f 72 20 42 4c 4f 42 20 65 78 63 65 65 64 g or BLOB exceed
7fb0: 73 20 73 69 7a 65 20 6c 69 6d 69 74 20 2a 2f 0a s size limit */.
7fc0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
7fd0: 4f 4e 53 54 52 41 49 4e 54 20 20 31 39 20 20 20 ONSTRAINT 19
7fe0: 2f 2a 20 41 62 6f 72 74 20 64 75 65 20 74 6f 20 /* Abort due to
7ff0: 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 constraint viola
8000: 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 tion */.#define
8010: 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 20 SQLITE_MISMATCH
8020: 20 20 20 32 30 20 20 20 2f 2a 20 44 61 74 61 20 20 /* Data
8030: 74 79 70 65 20 6d 69 73 6d 61 74 63 68 20 2a 2f type mismatch */
8040: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8050: 4d 49 53 55 53 45 20 20 20 20 20 20 32 31 20 20 MISUSE 21
8060: 20 2f 2a 20 4c 69 62 72 61 72 79 20 75 73 65 64 /* Library used
8070: 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 2a 2f 0a incorrectly */.
8080: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e #define SQLITE_N
8090: 4f 4c 46 53 20 20 20 20 20 20 20 32 32 20 20 20 OLFS 22
80a0: 2f 2a 20 55 73 65 73 20 4f 53 20 66 65 61 74 75 /* Uses OS featu
80b0: 72 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 65 res not supporte
80c0: 64 20 6f 6e 20 68 6f 73 74 20 2a 2f 0a 23 64 65 d on host */.#de
80d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 55 54 48 fine SQLITE_AUTH
80e0: 20 20 20 20 20 20 20 20 32 33 20 20 20 2f 2a 20 23 /*
80f0: 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 64 65 Authorization de
8100: 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 nied */.#define
8110: 53 51 4c 49 54 45 5f 46 4f 52 4d 41 54 20 20 20 SQLITE_FORMAT
8120: 20 20 20 32 34 20 20 20 2f 2a 20 41 75 78 69 6c 24 /* Auxil
8130: 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 6f iary database fo
8140: 72 6d 61 74 20 65 72 72 6f 72 20 2a 2f 0a 23 64 rmat error */.#d
8150: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 41 4e efine SQLITE_RAN
8160: 47 45 20 20 20 20 20 20 20 32 35 20 20 20 2f 2a GE 25 /*
8170: 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 2nd parameter t
8180: 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 20 6f o sqlite3_bind o
8190: 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0a 23 ut of range */.#
81a0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f define SQLITE_NO
81b0: 54 41 44 42 20 20 20 20 20 20 32 36 20 20 20 2f TADB 26 /
81c0: 2a 20 46 69 6c 65 20 6f 70 65 6e 65 64 20 74 68 * File opened th
81d0: 61 74 20 69 73 20 6e 6f 74 20 61 20 64 61 74 61 at is not a data
81e0: 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 23 64 65 base file */.#de
81f0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 4f 57 20 fine SQLITE_ROW
8200: 20 20 20 20 20 20 20 20 31 30 30 20 20 2f 2a 20 100 /*
8210: 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 68 sqlite3_step() h
8220: 61 73 20 61 6e 6f 74 68 65 72 20 72 6f 77 20 72 as another row r
8230: 65 61 64 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 eady */.#define
8240: 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 20 20 SQLITE_DONE
8250: 20 20 20 31 30 31 20 20 2f 2a 20 73 71 6c 69 74 101 /* sqlit
8260: 65 33 5f 73 74 65 70 28 29 20 68 61 73 20 66 69 e3_step() has fi
8270: 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e 67 nished executing
8280: 20 2a 2f 0a 2f 2a 20 65 6e 64 2d 6f 66 2d 65 72 */./* end-of-er
8290: 72 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 0a 2f 2a ror-codes */../*
82a0: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 78 .** CAPI3REF: Ex
82b0: 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f tended Result Co
82c0: 64 65 73 20 7b 48 31 30 32 32 30 7d 20 3c 53 31 des {H10220} <S1
82d0: 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0700>.** KEYWORD
82e0: 53 3a 20 7b 65 78 74 65 6e 64 65 64 20 65 72 72 S: {extended err
82f0: 6f 72 20 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64 or code} {extend
8300: 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73 7d 0a ed error codes}.
8310: 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 65 78 ** KEYWORDS: {ex
8320: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f tended result co
8330: 64 65 7d 20 7b 65 78 74 65 6e 64 65 64 20 72 65 de} {extended re
8340: 73 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a 2a sult codes}.**.*
8350: 2a 20 49 6e 20 69 74 73 20 64 65 66 61 75 6c 74 * In its default
8360: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 configuration,
8370: 53 51 4c 69 74 65 20 41 50 49 20 72 6f 75 74 69 SQLite API routi
8380: 6e 65 73 20 72 65 74 75 72 6e 20 6f 6e 65 20 6f nes return one o
8390: 66 20 32 36 20 69 6e 74 65 67 65 72 0a 2a 2a 20 f 26 integer.**
83a0: 5b 53 51 4c 49 54 45 5f 4f 4b 20 7c 20 72 65 73 [SQLITE_OK | res
83b0: 75 6c 74 20 63 6f 64 65 73 5d 2e 20 20 48 6f 77 ult codes]. How
83c0: 65 76 65 72 2c 20 65 78 70 65 72 69 65 6e 63 65 ever, experience
83d0: 20 68 61 73 20 73 68 6f 77 6e 20 74 68 61 74 20 has shown that
83e0: 6d 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 73 65 many of.** these
83f0: 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72 result codes ar
8400: 65 20 74 6f 6f 20 63 6f 61 72 73 65 2d 67 72 61 e too coarse-gra
8410: 69 6e 65 64 2e 20 20 54 68 65 79 20 64 6f 20 6e ined. They do n
8420: 6f 74 20 70 72 6f 76 69 64 65 20 61 73 0a 2a 2a ot provide as.**
8430: 20 6d 75 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f much informatio
8440: 6e 20 61 62 6f 75 74 20 70 72 6f 62 6c 65 6d 73 n about problems
8450: 20 61 73 20 70 72 6f 67 72 61 6d 6d 65 72 73 20 as programmers
8460: 6d 69 67 68 74 20 6c 69 6b 65 2e 20 20 49 6e 20 might like. In
8470: 61 6e 20 65 66 66 6f 72 74 20 74 6f 0a 2a 2a 20 an effort to.**
8480: 61 64 64 72 65 73 73 20 74 68 69 73 2c 20 6e 65 address this, ne
8490: 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 wer versions of
84a0: 53 51 4c 69 74 65 20 28 76 65 72 73 69 6f 6e 20 SQLite (version
84b0: 33 2e 33 2e 38 20 61 6e 64 20 6c 61 74 65 72 29 3.3.8 and later)
84c0: 20 69 6e 63 6c 75 64 65 0a 2a 2a 20 73 75 70 70 include.** supp
84d0: 6f 72 74 20 66 6f 72 20 61 64 64 69 74 69 6f 6e ort for addition
84e0: 61 6c 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 al result codes
84f0: 74 68 61 74 20 70 72 6f 76 69 64 65 20 6d 6f 72 that provide mor
8500: 65 20 64 65 74 61 69 6c 65 64 20 69 6e 66 6f 72 e detailed infor
8510: 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 mation.** about
8520: 65 72 72 6f 72 73 2e 20 54 68 65 20 65 78 74 65 errors. The exte
8530: 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 nded result code
8540: 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20 6f 72 s are enabled or
8550: 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 6f 6e 20 disabled.** on
8560: 61 20 70 65 72 20 64 61 74 61 62 61 73 65 20 63 a per database c
8570: 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 20 onnection basis
8580: 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 using the.** [sq
8590: 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 lite3_extended_r
85a0: 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 5d 20 41 esult_codes()] A
85b0: 50 49 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f PI..**.** Some o
85c0: 66 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 f the available
85d0: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 extended result
85e0: 63 6f 64 65 73 20 61 72 65 20 6c 69 73 74 65 64 codes are listed
85f0: 20 68 65 72 65 2e 0a 2a 2a 20 4f 6e 65 20 6d 61 here..** One ma
8600: 79 20 65 78 70 65 63 74 20 74 68 65 20 6e 75 6d y expect the num
8610: 62 65 72 20 6f 66 20 65 78 74 65 6e 64 65 64 20 ber of extended
8620: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77 69 6c result codes wil
8630: 6c 20 62 65 20 65 78 70 61 6e 64 0a 2a 2a 20 6f l be expand.** o
8640: 76 65 72 20 74 69 6d 65 2e 20 20 53 6f 66 74 77 ver time. Softw
8650: 61 72 65 20 74 68 61 74 20 75 73 65 73 20 65 78 are that uses ex
8660: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f tended result co
8670: 64 65 73 20 73 68 6f 75 6c 64 20 65 78 70 65 63 des should expec
8680: 74 0a 2a 2a 20 74 6f 20 73 65 65 20 6e 65 77 20 t.** to see new
8690: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 69 6e 20 result codes in
86a0: 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 future releases
86b0: 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a of SQLite..**.**
86c0: 20 54 68 65 20 53 51 4c 49 54 45 5f 4f 4b 20 72 The SQLITE_OK r
86d0: 65 73 75 6c 74 20 63 6f 64 65 20 77 69 6c 6c 20 esult code will
86e0: 6e 65 76 65 72 20 62 65 20 65 78 74 65 6e 64 65 never be extende
86f0: 64 2e 20 20 49 74 20 77 69 6c 6c 20 61 6c 77 61 d. It will alwa
8700: 79 73 0a 2a 2a 20 62 65 20 65 78 61 63 74 6c 79 ys.** be exactly
8710: 20 7a 65 72 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e zero..*/.#defin
8720: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 e SQLITE_IOERR_R
8730: 45 41 44 20 20 20 20 20 20 20 20 20 20 20 20 20 EAD
8740: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c (SQLITE_IOERR |
8750: 20 28 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 (1<<8)).#define
8760: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 SQLITE_IOERR_SH
8770: 4f 52 54 5f 52 45 41 44 20 20 20 20 20 20 20 20 ORT_READ
8780: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 (SQLITE_IOERR |
8790: 28 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 (2<<8)).#define
87a0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 SQLITE_IOERR_WRI
87b0: 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 28 TE (
87c0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
87d0: 33 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 3<<8)).#define S
87e0: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e QLITE_IOERR_FSYN
87f0: 43 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 C (S
8800: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 34 QLITE_IOERR | (4
8810: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 <<8)).#define SQ
8820: 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 LITE_IOERR_DIR_F
8830: 53 59 4e 43 20 20 20 20 20 20 20 20 20 28 53 51 SYNC (SQ
8840: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 35 3c LITE_IOERR | (5<
8850: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
8860: 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 ITE_IOERR_TRUNCA
8870: 54 45 20 20 20 20 20 20 20 20 20 20 28 53 51 4c TE (SQL
8880: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 36 3c 3c ITE_IOERR | (6<<
8890: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 8)).#define SQLI
88a0: 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 20 TE_IOERR_FSTAT
88b0: 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 (SQLI
88c0: 54 45 5f 49 4f 45 52 52 20 7c 20 28 37 3c 3c 38 TE_IOERR | (7<<8
88d0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
88e0: 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 20 20 E_IOERR_UNLOCK
88f0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
8900: 45 5f 49 4f 45 52 52 20 7c 20 28 38 3c 3c 38 29 E_IOERR | (8<<8)
8910: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
8920: 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 20 20 20 _IOERR_RDLOCK
8930: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
8940: 5f 49 4f 45 52 52 20 7c 20 28 39 3c 3c 38 29 29 _IOERR | (9<<8))
8950: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8960: 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 20 20 20 IOERR_DELETE
8970: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
8980: 49 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38 29 29 IOERR | (10<<8))
8990: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
89a0: 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 20 20 IOERR_BLOCKED
89b0: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
89c0: 49 4f 45 52 52 20 7c 20 28 31 31 3c 3c 38 29 29 IOERR | (11<<8))
89d0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
89e0: 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 20 20 20 20 IOERR_NOMEM
89f0: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
8a00: 49 4f 45 52 52 20 7c 20 28 31 32 3c 3c 38 29 29 IOERR | (12<<8))
8a10: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8a20: 49 4f 45 52 52 5f 41 43 43 45 53 53 20 20 20 20 IOERR_ACCESS
8a30: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
8a40: 49 4f 45 52 52 20 7c 20 28 31 33 3c 3c 38 29 29 IOERR | (13<<8))
8a50: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8a60: 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 IOERR_CHECKRESER
8a70: 56 45 44 4c 4f 43 4b 20 28 53 51 4c 49 54 45 5f VEDLOCK (SQLITE_
8a80: 49 4f 45 52 52 20 7c 20 28 31 34 3c 3c 38 29 29 IOERR | (14<<8))
8a90: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8aa0: 49 4f 45 52 52 5f 4c 4f 43 4b 20 20 20 20 20 20 IOERR_LOCK
8ab0: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
8ac0: 49 4f 45 52 52 20 7c 20 28 31 35 3c 3c 38 29 29 IOERR | (15<<8))
8ad0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8ae0: 49 4f 45 52 52 5f 43 4c 4f 53 45 20 20 20 20 20 IOERR_CLOSE
8af0: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
8b00: 49 4f 45 52 52 20 7c 20 28 31 36 3c 3c 38 29 29 IOERR | (16<<8))
8b10: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8b20: 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 20 IOERR_DIR_CLOSE
8b30: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
8b40: 49 4f 45 52 52 20 7c 20 28 31 37 3c 3c 38 29 29 IOERR | (17<<8))
8b50: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8b60: 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 LOCKED_SHAREDCAC
8b70: 48 45 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f HE (SQLITE_
8b80: 4c 4f 43 4b 45 44 20 7c 20 28 31 3c 3c 38 29 20 LOCKED | (1<<8)
8b90: 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 )../*.** CAPI3RE
8ba0: 46 3a 20 46 6c 61 67 73 20 46 6f 72 20 46 69 6c F: Flags For Fil
8bb0: 65 20 4f 70 65 6e 20 4f 70 65 72 61 74 69 6f 6e e Open Operation
8bc0: 73 20 7b 48 31 30 32 33 30 7d 20 3c 48 31 31 31 s {H10230} <H111
8bd0: 32 30 3e 20 3c 48 31 32 37 30 30 3e 0a 2a 2a 0a 20> <H12700>.**.
8be0: 2a 2a 20 54 68 65 73 65 20 62 69 74 20 76 61 6c ** These bit val
8bf0: 75 65 73 20 61 72 65 20 69 6e 74 65 6e 64 65 64 ues are intended
8c00: 20 66 6f 72 20 75 73 65 20 69 6e 20 74 68 65 0a for use in the.
8c10: 2a 2a 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 ** 3rd parameter
8c20: 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 to the [sqlite3
8c30: 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e 74 65 _open_v2()] inte
8c40: 72 66 61 63 65 20 61 6e 64 0a 2a 2a 20 69 6e 20 rface and.** in
8c50: 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 the 4th paramete
8c60: 72 20 74 6f 20 74 68 65 20 78 4f 70 65 6e 20 6d r to the xOpen m
8c70: 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a 20 ethod of the.**
8c80: 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 [sqlite3_vfs] ob
8c90: 6a 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ject..*/.#define
8ca0: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
8cb0: 44 4f 4e 4c 59 20 20 20 20 20 20 20 20 20 30 78 DONLY 0x
8cc0: 30 30 30 30 30 30 30 31 20 20 2f 2a 20 4f 6b 20 00000001 /* Ok
8cd0: 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e for sqlite3_open
8ce0: 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 _v2() */.#define
8cf0: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
8d00: 44 57 52 49 54 45 20 20 20 20 20 20 20 20 30 78 DWRITE 0x
8d10: 30 30 30 30 30 30 30 32 20 20 2f 2a 20 4f 6b 20 00000002 /* Ok
8d20: 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e for sqlite3_open
8d30: 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 _v2() */.#define
8d40: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 SQLITE_OPEN_CRE
8d50: 41 54 45 20 20 20 20 20 20 20 20 20 20 20 30 78 ATE 0x
8d60: 30 30 30 30 30 30 30 34 20 20 2f 2a 20 4f 6b 20 00000004 /* Ok
8d70: 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e for sqlite3_open
8d80: 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 _v2() */.#define
8d90: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c SQLITE_OPEN_DEL
8da0: 45 54 45 4f 4e 43 4c 4f 53 45 20 20 20 20 30 78 ETEONCLOSE 0x
8db0: 30 30 30 30 30 30 30 38 20 20 2f 2a 20 56 46 53 00000008 /* VFS
8dc0: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 only */.#define
8dd0: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 SQLITE_OPEN_EXC
8de0: 4c 55 53 49 56 45 20 20 20 20 20 20 20 20 30 78 LUSIVE 0x
8df0: 30 30 30 30 30 30 31 30 20 20 2f 2a 20 56 46 53 00000010 /* VFS
8e00: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 only */.#define
8e10: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 SQLITE_OPEN_MAI
8e20: 4e 5f 44 42 20 20 20 20 20 20 20 20 20 20 30 78 N_DB 0x
8e30: 30 30 30 30 30 31 30 30 20 20 2f 2a 20 56 46 53 00000100 /* VFS
8e40: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 only */.#define
8e50: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d SQLITE_OPEN_TEM
8e60: 50 5f 44 42 20 20 20 20 20 20 20 20 20 20 30 78 P_DB 0x
8e70: 30 30 30 30 30 32 30 30 20 20 2f 2a 20 56 46 53 00000200 /* VFS
8e80: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 only */.#define
8e90: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 SQLITE_OPEN_TRA
8ea0: 4e 53 49 45 4e 54 5f 44 42 20 20 20 20 20 30 78 NSIENT_DB 0x
8eb0: 30 30 30 30 30 34 30 30 20 20 2f 2a 20 56 46 53 00000400 /* VFS
8ec0: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 only */.#define
8ed0: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 SQLITE_OPEN_MAI
8ee0: 4e 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30 78 N_JOURNAL 0x
8ef0: 30 30 30 30 30 38 30 30 20 20 2f 2a 20 56 46 53 00000800 /* VFS
8f00: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 only */.#define
8f10: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d SQLITE_OPEN_TEM
8f20: 50 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30 78 P_JOURNAL 0x
8f30: 30 30 30 30 31 30 30 30 20 20 2f 2a 20 56 46 53 00001000 /* VFS
8f40: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 only */.#define
8f50: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 SQLITE_OPEN_SUB
8f60: 4a 4f 55 52 4e 41 4c 20 20 20 20 20 20 20 30 78 JOURNAL 0x
8f70: 30 30 30 30 32 30 30 30 20 20 2f 2a 20 56 46 53 00002000 /* VFS
8f80: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 only */.#define
8f90: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 SQLITE_OPEN_MAS
8fa0: 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 20 20 30 78 TER_JOURNAL 0x
8fb0: 30 30 30 30 34 30 30 30 20 20 2f 2a 20 56 46 53 00004000 /* VFS
8fc0: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 only */.#define
8fd0: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d SQLITE_OPEN_NOM
8fe0: 55 54 45 58 20 20 20 20 20 20 20 20 20 20 30 78 UTEX 0x
8ff0: 30 30 30 30 38 30 30 30 20 20 2f 2a 20 4f 6b 20 00008000 /* Ok
9000: 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e for sqlite3_open
9010: 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 _v2() */.#define
9020: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c SQLITE_OPEN_FUL
9030: 4c 4d 55 54 45 58 20 20 20 20 20 20 20 20 30 78 LMUTEX 0x
9040: 30 30 30 31 30 30 30 30 20 20 2f 2a 20 4f 6b 20 00010000 /* Ok
9050: 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e for sqlite3_open
9060: 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 _v2() */.#define
9070: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 SQLITE_OPEN_SHA
9080: 52 45 44 43 41 43 48 45 20 20 20 20 20 20 30 78 REDCACHE 0x
9090: 30 30 30 32 30 30 30 30 20 20 2f 2a 20 4f 6b 20 00020000 /* Ok
90a0: 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e for sqlite3_open
90b0: 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 _v2() */.#define
90c0: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 SQLITE_OPEN_PRI
90d0: 56 41 54 45 43 41 43 48 45 20 20 20 20 20 30 78 VATECACHE 0x
90e0: 30 30 30 34 30 30 30 30 20 20 2f 2a 20 4f 6b 20 00040000 /* Ok
90f0: 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e for sqlite3_open
9100: 5f 76 32 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 _v2() */../*.**
9110: 43 41 50 49 33 52 45 46 3a 20 44 65 76 69 63 65 CAPI3REF: Device
9120: 20 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 Characteristics
9130: 20 7b 48 31 30 32 34 30 7d 20 3c 48 31 31 31 32 {H10240} <H1112
9140: 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65 0>.**.** The xDe
9150: 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65 73 viceCapabilities
9160: 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 5b method of the [
9170: 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
9180: 64 73 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65 ds].** object re
9190: 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 turns an integer
91a0: 20 77 68 69 63 68 20 69 73 20 61 20 76 65 63 74 which is a vect
91b0: 6f 72 20 6f 66 20 74 68 65 20 74 68 65 73 65 0a or of the these.
91c0: 2a 2a 20 62 69 74 20 76 61 6c 75 65 73 20 65 78 ** bit values ex
91d0: 70 72 65 73 73 69 6e 67 20 49 2f 4f 20 63 68 61 pressing I/O cha
91e0: 72 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 racteristics of
91f0: 74 68 65 20 6d 61 73 73 20 73 74 6f 72 61 67 65 the mass storage
9200: 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61 74 20 .** device that
9210: 68 6f 6c 64 73 20 74 68 65 20 66 69 6c 65 20 74 holds the file t
9220: 68 61 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33 hat the [sqlite3
9230: 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 _io_methods].**
9240: 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a refers to..**.**
9250: 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 The SQLITE_IOCA
9260: 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 72 74 P_ATOMIC propert
9270: 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6c 6c y means that all
9280: 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61 6e writes of.** an
9290: 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f 6d 69 y size are atomi
92a0: 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 c. The SQLITE_I
92b0: 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 OCAP_ATOMICnnn v
92c0: 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 68 alues.** mean th
92d0: 61 74 20 77 72 69 74 65 73 20 6f 66 20 62 6c 6f at writes of blo
92e0: 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e 6e 6e cks that are nnn
92f0: 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 bytes in size a
9300: 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65 nd.** are aligne
9310: 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73 73 20 d to an address
9320: 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e 74 65 which is an inte
9330: 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0a ger multiple of.
9340: 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d 69 ** nnn are atomi
9350: 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 c. The SQLITE_I
9360: 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 OCAP_SAFE_APPEND
9370: 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a 20 value means.**
9380: 74 68 61 74 20 77 68 65 6e 20 64 61 74 61 20 69 that when data i
9390: 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20 s appended to a
93a0: 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 20 69 file, the data i
93b0: 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66 69 s appended.** fi
93c0: 72 73 74 20 74 68 65 6e 20 74 68 65 20 73 69 7a rst then the siz
93d0: 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 e of the file is
93e0: 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 65 72 extended, never
93f0: 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77 61 the other.** wa
9400: 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 20 53 y around. The S
9410: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 QLITE_IOCAP_SEQU
9420: 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79 20 ENTIAL property
9430: 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e means that.** in
9440: 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 72 69 formation is wri
9450: 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e 20 tten to disk in
9460: 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 20 61 the same order a
9470: 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57 s calls.** to xW
9480: 72 69 74 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 rite()..*/.#defi
9490: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f ne SQLITE_IOCAP_
94a0: 41 54 4f 4d 49 43 20 20 20 20 20 20 20 20 20 20 ATOMIC
94b0: 30 78 30 30 30 30 30 30 30 31 0a 23 64 65 66 69 0x00000001.#defi
94c0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f ne SQLITE_IOCAP_
94d0: 41 54 4f 4d 49 43 35 31 32 20 20 20 20 20 20 20 ATOMIC512
94e0: 30 78 30 30 30 30 30 30 30 32 0a 23 64 65 66 69 0x00000002.#defi
94f0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f ne SQLITE_IOCAP_
9500: 41 54 4f 4d 49 43 31 4b 20 20 20 20 20 20 20 20 ATOMIC1K
9510: 30 78 30 30 30 30 30 30 30 34 0a 23 64 65 66 69 0x00000004.#defi
9520: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f ne SQLITE_IOCAP_
9530: 41 54 4f 4d 49 43 32 4b 20 20 20 20 20 20 20 20 ATOMIC2K
9540: 30 78 30 30 30 30 30 30 30 38 0a 23 64 65 66 69 0x00000008.#defi
9550: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f ne SQLITE_IOCAP_
9560: 41 54 4f 4d 49 43 34 4b 20 20 20 20 20 20 20 20 ATOMIC4K
9570: 30 78 30 30 30 30 30 30 31 30 0a 23 64 65 66 69 0x00000010.#defi
9580: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f ne SQLITE_IOCAP_
9590: 41 54 4f 4d 49 43 38 4b 20 20 20 20 20 20 20 20 ATOMIC8K
95a0: 30 78 30 30 30 30 30 30 32 30 0a 23 64 65 66 69 0x00000020.#defi
95b0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f ne SQLITE_IOCAP_
95c0: 41 54 4f 4d 49 43 31 36 4b 20 20 20 20 20 20 20 ATOMIC16K
95d0: 30 78 30 30 30 30 30 30 34 30 0a 23 64 65 66 69 0x00000040.#defi
95e0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f ne SQLITE_IOCAP_
95f0: 41 54 4f 4d 49 43 33 32 4b 20 20 20 20 20 20 20 ATOMIC32K
9600: 30 78 30 30 30 30 30 30 38 30 0a 23 64 65 66 69 0x00000080.#defi
9610: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f ne SQLITE_IOCAP_
9620: 41 54 4f 4d 49 43 36 34 4b 20 20 20 20 20 20 20 ATOMIC64K
9630: 30 78 30 30 30 30 30 31 30 30 0a 23 64 65 66 69 0x00000100.#defi
9640: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f ne SQLITE_IOCAP_
9650: 53 41 46 45 5f 41 50 50 45 4e 44 20 20 20 20 20 SAFE_APPEND
9660: 30 78 30 30 30 30 30 32 30 30 0a 23 64 65 66 69 0x00000200.#defi
9670: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f ne SQLITE_IOCAP_
9680: 53 45 51 55 45 4e 54 49 41 4c 20 20 20 20 20 20 SEQUENTIAL
9690: 30 78 30 30 30 30 30 34 30 30 0a 0a 2f 2a 0a 2a 0x00000400../*.*
96a0: 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6c 65 * CAPI3REF: File
96b0: 20 4c 6f 63 6b 69 6e 67 20 4c 65 76 65 6c 73 20 Locking Levels
96c0: 7b 48 31 30 32 35 30 7d 20 3c 48 31 31 31 32 30 {H10250} <H11120
96d0: 3e 20 3c 48 31 31 33 31 30 3e 0a 2a 2a 0a 2a 2a > <H11310>.**.**
96e0: 20 53 51 4c 69 74 65 20 75 73 65 73 20 6f 6e 65 SQLite uses one
96f0: 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 of these intege
9700: 72 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 20 r values as the
9710: 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 second.** argume
9720: 6e 74 20 74 6f 20 63 61 6c 6c 73 20 69 74 20 6d nt to calls it m
9730: 61 6b 65 73 20 74 6f 20 74 68 65 20 78 4c 6f 63 akes to the xLoc
9740: 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 k() and xUnlock(
9750: 29 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 ) methods.** of
9760: 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d an [sqlite3_io_m
9770: 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 0a ethods] object..
9780: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
9790: 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 20 20 20 20 20 E_LOCK_NONE
97a0: 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 0.#define S
97b0: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 QLITE_LOCK_SHARE
97c0: 44 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 D 1.#defi
97d0: 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 ne SQLITE_LOCK_R
97e0: 45 53 45 52 56 45 44 20 20 20 20 20 20 32 0a 23 ESERVED 2.#
97f0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f define SQLITE_LO
9800: 43 4b 5f 50 45 4e 44 49 4e 47 20 20 20 20 20 20 CK_PENDING
9810: 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 3.#define SQLIT
9820: 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 E_LOCK_EXCLUSIVE
9830: 20 20 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 4../*.** CA
9840: 50 49 33 52 45 46 3a 20 53 79 6e 63 68 72 6f 6e PI3REF: Synchron
9850: 69 7a 61 74 69 6f 6e 20 54 79 70 65 20 46 6c 61 ization Type Fla
9860: 67 73 20 7b 48 31 30 32 36 30 7d 20 3c 48 31 31 gs {H10260} <H11
9870: 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 120>.**.** When
9880: 53 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 SQLite invokes t
9890: 68 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68 6f he xSync() metho
98a0: 64 20 6f 66 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 d of an.** [sqli
98b0: 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 te3_io_methods]
98c0: 6f 62 6a 65 63 74 20 69 74 20 75 73 65 73 20 61 object it uses a
98d0: 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 0a combination of.
98e0: 2a 2a 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 ** these integer
98f0: 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 20 73 values as the s
9900: 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a econd argument..
9910: 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 53 **.** When the S
9920: 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f QLITE_SYNC_DATAO
9930: 4e 4c 59 20 66 6c 61 67 20 69 73 20 75 73 65 64 NLY flag is used
9940: 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 , it means that
9950: 74 68 65 0a 2a 2a 20 73 79 6e 63 20 6f 70 65 72 the.** sync oper
9960: 61 74 69 6f 6e 20 6f 6e 6c 79 20 6e 65 65 64 73 ation only needs
9970: 20 74 6f 20 66 6c 75 73 68 20 64 61 74 61 20 74 to flush data t
9980: 6f 20 6d 61 73 73 20 73 74 6f 72 61 67 65 2e 20 o mass storage.
9990: 20 49 6e 6f 64 65 0a 2a 2a 20 69 6e 66 6f 72 6d Inode.** inform
99a0: 61 74 69 6f 6e 20 6e 65 65 64 20 6e 6f 74 20 62 ation need not b
99b0: 65 20 66 6c 75 73 68 65 64 2e 20 49 66 20 74 68 e flushed. If th
99c0: 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74 e lower four bit
99d0: 73 20 6f 66 20 74 68 65 20 66 6c 61 67 0a 2a 2a s of the flag.**
99e0: 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f 53 59 equal SQLITE_SY
99f0: 4e 43 5f 4e 4f 52 4d 41 4c 2c 20 74 68 61 74 20 NC_NORMAL, that
9a00: 6d 65 61 6e 73 20 74 6f 20 75 73 65 20 6e 6f 72 means to use nor
9a10: 6d 61 6c 20 66 73 79 6e 63 28 29 20 73 65 6d 61 mal fsync() sema
9a20: 6e 74 69 63 73 2e 0a 2a 2a 20 49 66 20 74 68 65 ntics..** If the
9a30: 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74 73 lower four bits
9a40: 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f 53 59 equal SQLITE_SY
9a50: 4e 43 5f 46 55 4c 4c 2c 20 74 68 61 74 20 6d 65 NC_FULL, that me
9a60: 61 6e 73 0a 2a 2a 20 74 6f 20 75 73 65 20 4d 61 ans.** to use Ma
9a70: 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66 75 6c c OS X style ful
9a80: 6c 73 79 6e 63 20 69 6e 73 74 65 61 64 20 6f 66 lsync instead of
9a90: 20 66 73 79 6e 63 28 29 2e 0a 2a 2f 0a 23 64 65 fsync()..*/.#de
9aa0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 fine SQLITE_SYNC
9ab0: 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 20 20 30 _NORMAL 0
9ac0: 78 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 x00002.#define S
9ad0: 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20 QLITE_SYNC_FULL
9ae0: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 33 0x00003
9af0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
9b00: 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 20 20 SYNC_DATAONLY
9b10: 20 20 20 30 78 30 30 30 31 30 0a 0a 2f 2a 0a 2a 0x00010../*.*
9b20: 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 * CAPI3REF: OS I
9b30: 6e 74 65 72 66 61 63 65 20 4f 70 65 6e 20 46 69 nterface Open Fi
9b40: 6c 65 20 48 61 6e 64 6c 65 20 7b 48 31 31 31 31 le Handle {H1111
9b50: 30 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 0a 2a 0} <S20110>.**.*
9b60: 2a 20 41 6e 20 5b 73 71 6c 69 74 65 33 5f 66 69 * An [sqlite3_fi
9b70: 6c 65 5d 20 6f 62 6a 65 63 74 20 72 65 70 72 65 le] object repre
9b80: 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 66 69 sents an open fi
9b90: 6c 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 5b 73 le in the .** [s
9ba0: 71 6c 69 74 65 33 5f 76 66 73 20 7c 20 4f 53 20 qlite3_vfs | OS
9bb0: 69 6e 74 65 72 66 61 63 65 20 6c 61 79 65 72 5d interface layer]
9bc0: 2e 20 20 49 6e 64 69 76 69 64 75 61 6c 20 4f 53 . Individual OS
9bd0: 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 69 6d interface.** im
9be0: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 plementations wi
9bf0: 6c 6c 0a 2a 2a 20 77 61 6e 74 20 74 6f 20 73 75 ll.** want to su
9c00: 62 63 6c 61 73 73 20 74 68 69 73 20 6f 62 6a 65 bclass this obje
9c10: 63 74 20 62 79 20 61 70 70 65 6e 64 69 6e 67 20 ct by appending
9c20: 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 additional field
9c30: 73 0a 2a 2a 20 66 6f 72 20 74 68 65 69 72 20 6f s.** for their o
9c40: 77 6e 20 75 73 65 2e 20 20 54 68 65 20 70 4d 65 wn use. The pMe
9c50: 74 68 6f 64 73 20 65 6e 74 72 79 20 69 73 20 61 thods entry is a
9c60: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a pointer to an.*
9c70: 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 * [sqlite3_io_me
9c80: 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 74 68 thods] object th
9c90: 61 74 20 64 65 66 69 6e 65 73 20 6d 65 74 68 6f at defines metho
9ca0: 64 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 69 6e ds for performin
9cb0: 67 0a 2a 2a 20 49 2f 4f 20 6f 70 65 72 61 74 69 g.** I/O operati
9cc0: 6f 6e 73 20 6f 6e 20 74 68 65 20 6f 70 65 6e 20 ons on the open
9cd0: 66 69 6c 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 file..*/.typedef
9ce0: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
9cf0: 66 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 69 6c file sqlite3_fil
9d00: 65 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 e;.struct sqlite
9d10: 33 5f 66 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 3_file {. const
9d20: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
9d30: 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 io_methods *pMet
9d40: 68 6f 64 73 3b 20 20 2f 2a 20 4d 65 74 68 6f 64 hods; /* Method
9d50: 73 20 66 6f 72 20 61 6e 20 6f 70 65 6e 20 66 69 s for an open fi
9d60: 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 le */.};../*.**
9d70: 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 CAPI3REF: OS Int
9d80: 65 72 66 61 63 65 20 46 69 6c 65 20 56 69 72 74 erface File Virt
9d90: 75 61 6c 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 ual Methods Obje
9da0: 63 74 20 7b 48 31 31 31 32 30 7d 20 3c 53 32 30 ct {H11120} <S20
9db0: 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 110>.**.** Every
9dc0: 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 62 79 20 file opened by
9dd0: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 the [sqlite3_vfs
9de0: 5d 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 70 ] xOpen method p
9df0: 6f 70 75 6c 61 74 65 73 20 61 6e 0a 2a 2a 20 5b opulates an.** [
9e00: 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 sqlite3_file] ob
9e10: 6a 65 63 74 20 28 6f 72 2c 20 6d 6f 72 65 20 63 ject (or, more c
9e20: 6f 6d 6d 6f 6e 6c 79 2c 20 61 20 73 75 62 63 6c ommonly, a subcl
9e30: 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 ass of the.** [s
9e40: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a qlite3_file] obj
9e50: 65 63 74 29 20 77 69 74 68 20 61 20 70 6f 69 6e ect) with a poin
9e60: 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e ter to an instan
9e70: 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 ce of this objec
9e80: 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 t..** This objec
9e90: 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 6d 65 t defines the me
9ea0: 74 68 6f 64 73 20 75 73 65 64 20 74 6f 20 70 65 thods used to pe
9eb0: 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20 6f 70 rform various op
9ec0: 65 72 61 74 69 6f 6e 73 0a 2a 2a 20 61 67 61 69 erations.** agai
9ed0: 6e 73 74 20 74 68 65 20 6f 70 65 6e 20 66 69 6c nst the open fil
9ee0: 65 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 e represented by
9ef0: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 the [sqlite3_fi
9f00: 6c 65 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a le] object..**.*
9f10: 2a 20 49 66 20 74 68 65 20 78 4f 70 65 6e 20 6d * If the xOpen m
9f20: 65 74 68 6f 64 20 73 65 74 73 20 74 68 65 20 73 ethod sets the s
9f30: 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 qlite3_file.pMet
9f40: 68 6f 64 73 20 65 6c 65 6d 65 6e 74 20 0a 2a 2a hods element .**
9f50: 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 to a non-NULL p
9f60: 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 74 68 65 ointer, then the
9f70: 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
9f80: 6f 64 73 2e 78 43 6c 6f 73 65 20 6d 65 74 68 6f ods.xClose metho
9f90: 64 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e 76 6f d.** may be invo
9fa0: 6b 65 64 20 65 76 65 6e 20 69 66 20 74 68 65 20 ked even if the
9fb0: 78 4f 70 65 6e 20 72 65 70 6f 72 74 65 64 20 74 xOpen reported t
9fc0: 68 61 74 20 69 74 20 66 61 69 6c 65 64 2e 20 20 hat it failed.
9fd0: 54 68 65 0a 2a 2a 20 6f 6e 6c 79 20 77 61 79 20 The.** only way
9fe0: 74 6f 20 70 72 65 76 65 6e 74 20 61 20 63 61 6c to prevent a cal
9ff0: 6c 20 74 6f 20 78 43 6c 6f 73 65 20 66 6f 6c 6c l to xClose foll
a000: 6f 77 69 6e 67 20 61 20 66 61 69 6c 65 64 20 78 owing a failed x
a010: 4f 70 65 6e 0a 2a 2a 20 69 73 20 66 6f 72 20 74 Open.** is for t
a020: 68 65 20 78 4f 70 65 6e 20 74 6f 20 73 65 74 20 he xOpen to set
a030: 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 the sqlite3_file
a040: 2e 70 4d 65 74 68 6f 64 73 20 65 6c 65 6d 65 6e .pMethods elemen
a050: 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a t to NULL..**.**
a060: 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d The flags argum
a070: 65 6e 74 20 74 6f 20 78 53 79 6e 63 20 6d 61 79 ent to xSync may
a080: 20 62 65 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 be one of [SQLI
a090: 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 5d 20 TE_SYNC_NORMAL]
a0a0: 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 59 or.** [SQLITE_SY
a0b0: 4e 43 5f 46 55 4c 4c 5d 2e 20 20 54 68 65 20 66 NC_FULL]. The f
a0c0: 69 72 73 74 20 63 68 6f 69 63 65 20 69 73 20 74 irst choice is t
a0d0: 68 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28 he normal fsync(
a0e0: 29 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 )..** The second
a0f0: 20 63 68 6f 69 63 65 20 69 73 20 61 20 4d 61 63 choice is a Mac
a100: 20 4f 53 20 58 20 73 74 79 6c 65 20 66 75 6c 6c OS X style full
a110: 73 79 6e 63 2e 20 20 54 68 65 20 5b 53 51 4c 49 sync. The [SQLI
a120: 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 TE_SYNC_DATAONLY
a130: 5d 0a 2a 2a 20 66 6c 61 67 20 6d 61 79 20 62 65 ].** flag may be
a140: 20 4f 52 65 64 20 69 6e 20 74 6f 20 69 6e 64 69 ORed in to indi
a150: 63 61 74 65 20 74 68 61 74 20 6f 6e 6c 79 20 74 cate that only t
a160: 68 65 20 64 61 74 61 20 6f 66 20 74 68 65 20 66 he data of the f
a170: 69 6c 65 0a 2a 2a 20 61 6e 64 20 6e 6f 74 20 69 ile.** and not i
a180: 74 73 20 69 6e 6f 64 65 20 6e 65 65 64 73 20 74 ts inode needs t
a190: 6f 20 62 65 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a o be synced..**.
a1a0: 2a 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 76 ** The integer v
a1b0: 61 6c 75 65 73 20 74 6f 20 78 4c 6f 63 6b 28 29 alues to xLock()
a1c0: 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 61 and xUnlock() a
a1d0: 72 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 3c 75 6c re one of.** <ul
a1e0: 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 >.** <li> [SQLIT
a1f0: 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 0a 2a 2a E_LOCK_NONE],.**
a200: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f <li> [SQLITE_LO
a210: 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a 20 3c CK_SHARED],.** <
a220: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b li> [SQLITE_LOCK
a230: 5f 52 45 53 45 52 56 45 44 5d 2c 0a 2a 2a 20 3c _RESERVED],.** <
a240: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b li> [SQLITE_LOCK
a250: 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 0a 2a 2a _PENDING], or.**
a260: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f <li> [SQLITE_LO
a270: 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 2e 0a 2a CK_EXCLUSIVE]..*
a280: 2a 20 3c 2f 75 6c 3e 0a 2a 2a 20 78 4c 6f 63 6b * </ul>.** xLock
a290: 28 29 20 69 6e 63 72 65 61 73 65 73 20 74 68 65 () increases the
a2a0: 20 6c 6f 63 6b 2e 20 78 55 6e 6c 6f 63 6b 28 29 lock. xUnlock()
a2b0: 20 64 65 63 72 65 61 73 65 73 20 74 68 65 20 6c decreases the l
a2c0: 6f 63 6b 2e 0a 2a 2a 20 54 68 65 20 78 43 68 65 ock..** The xChe
a2d0: 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 29 ckReservedLock()
a2e0: 20 6d 65 74 68 6f 64 20 63 68 65 63 6b 73 20 77 method checks w
a2f0: 68 65 74 68 65 72 20 61 6e 79 20 64 61 74 61 62 hether any datab
a300: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 0a ase connection,.
a310: 2a 2a 20 65 69 74 68 65 72 20 69 6e 20 74 68 69 ** either in thi
a320: 73 20 70 72 6f 63 65 73 73 20 6f 72 20 69 6e 20 s process or in
a330: 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 some other proce
a340: 73 73 2c 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 ss, is holding a
a350: 20 52 45 53 45 52 56 45 44 2c 0a 2a 2a 20 50 45 RESERVED,.** PE
a360: 4e 44 49 4e 47 2c 20 6f 72 20 45 58 43 4c 55 53 NDING, or EXCLUS
a370: 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 IVE lock on the
a380: 66 69 6c 65 2e 20 20 49 74 20 72 65 74 75 72 6e file. It return
a390: 73 20 74 72 75 65 0a 2a 2a 20 69 66 20 73 75 63 s true.** if suc
a3a0: 68 20 61 20 6c 6f 63 6b 20 65 78 69 73 74 73 20 h a lock exists
a3b0: 61 6e 64 20 66 61 6c 73 65 20 6f 74 68 65 72 77 and false otherw
a3c0: 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 ise..**.** The x
a3d0: 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 6d 65 FileControl() me
a3e0: 74 68 6f 64 20 69 73 20 61 20 67 65 6e 65 72 69 thod is a generi
a3f0: 63 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 c interface that
a400: 20 61 6c 6c 6f 77 73 20 63 75 73 74 6f 6d 0a 2a allows custom.*
a410: 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 * VFS implementa
a420: 74 69 6f 6e 73 20 74 6f 20 64 69 72 65 63 74 6c tions to directl
a430: 79 20 63 6f 6e 74 72 6f 6c 20 61 6e 20 6f 70 65 y control an ope
a440: 6e 20 66 69 6c 65 20 75 73 69 6e 67 20 74 68 65 n file using the
a450: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c .** [sqlite3_fil
a460: 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 e_control()] int
a470: 65 72 66 61 63 65 2e 20 20 54 68 65 20 73 65 63 erface. The sec
a480: 6f 6e 64 20 22 6f 70 22 20 61 72 67 75 6d 65 6e ond "op" argumen
a490: 74 20 69 73 20 61 6e 0a 2a 2a 20 69 6e 74 65 67 t is an.** integ
a4a0: 65 72 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 20 er opcode. The
a4b0: 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 third argument i
a4c0: 73 20 61 20 67 65 6e 65 72 69 63 20 70 6f 69 6e s a generic poin
a4d0: 74 65 72 20 69 6e 74 65 6e 64 65 64 20 74 6f 0a ter intended to.
a4e0: 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 ** point to a st
a4f0: 72 75 63 74 75 72 65 20 74 68 61 74 20 6d 61 79 ructure that may
a500: 20 63 6f 6e 74 61 69 6e 20 61 72 67 75 6d 65 6e contain argumen
a510: 74 73 20 6f 72 20 73 70 61 63 65 20 69 6e 20 77 ts or space in w
a520: 68 69 63 68 20 74 6f 0a 2a 2a 20 77 72 69 74 65 hich to.** write
a530: 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 2e 20 return values.
a540: 20 50 6f 74 65 6e 74 69 61 6c 20 75 73 65 73 20 Potential uses
a550: 66 6f 72 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c for xFileControl
a560: 28 29 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20 66 () might be.** f
a570: 75 6e 63 74 69 6f 6e 73 20 74 6f 20 65 6e 61 62 unctions to enab
a580: 6c 65 20 62 6c 6f 63 6b 69 6e 67 20 6c 6f 63 6b le blocking lock
a590: 73 20 77 69 74 68 20 74 69 6d 65 6f 75 74 73 2c s with timeouts,
a5a0: 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 0a 2a to change the.*
a5b0: 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 * locking strate
a5c0: 67 79 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 gy (for example
a5d0: 74 6f 20 75 73 65 20 64 6f 74 2d 66 69 6c 65 20 to use dot-file
a5e0: 6c 6f 63 6b 73 29 2c 20 74 6f 20 69 6e 71 75 69 locks), to inqui
a5f0: 72 65 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65 20 re.** about the
a600: 73 74 61 74 75 73 20 6f 66 20 61 20 6c 6f 63 6b status of a lock
a610: 2c 20 6f 72 20 74 6f 20 62 72 65 61 6b 20 73 74 , or to break st
a620: 61 6c 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20 ale locks. The
a630: 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65 20 72 SQLite.** core r
a640: 65 73 65 72 76 65 73 20 61 6c 6c 20 6f 70 63 6f eserves all opco
a650: 64 65 73 20 6c 65 73 73 20 74 68 61 6e 20 31 30 des less than 10
a660: 30 20 66 6f 72 20 69 74 73 20 6f 77 6e 20 75 73 0 for its own us
a670: 65 2e 0a 2a 2a 20 41 20 5b 53 51 4c 49 54 45 5f e..** A [SQLITE_
a680: 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 FCNTL_LOCKSTATE
a690: 7c 20 6c 69 73 74 20 6f 66 20 6f 70 63 6f 64 65 | list of opcode
a6a0: 73 5d 20 6c 65 73 73 20 74 68 61 6e 20 31 30 30 s] less than 100
a6b0: 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a is available..*
a6c0: 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 * Applications t
a6d0: 68 61 74 20 64 65 66 69 6e 65 20 61 20 63 75 73 hat define a cus
a6e0: 74 6f 6d 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c tom xFileControl
a6f0: 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20 75 method should u
a700: 73 65 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 67 72 se opcodes.** gr
a710: 65 61 74 65 72 20 74 68 61 6e 20 31 30 30 20 74 eater than 100 t
a720: 6f 20 61 76 6f 69 64 20 63 6f 6e 66 6c 69 63 74 o avoid conflict
a730: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 65 s..**.** The xSe
a740: 63 74 6f 72 53 69 7a 65 28 29 20 6d 65 74 68 6f ctorSize() metho
a750: 64 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 65 d returns the se
a760: 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 65 ctor size of the
a770: 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61 74 20 .** device that
a780: 75 6e 64 65 72 6c 69 65 73 20 74 68 65 20 66 69 underlies the fi
a790: 6c 65 2e 20 20 54 68 65 20 73 65 63 74 6f 72 20 le. The sector
a7a0: 73 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a 20 6d size is the.** m
a7b0: 69 6e 69 6d 75 6d 20 77 72 69 74 65 20 74 68 61 inimum write tha
a7c0: 74 20 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d t can be perform
a7d0: 65 64 20 77 69 74 68 6f 75 74 20 64 69 73 74 75 ed without distu
a7e0: 72 62 69 6e 67 0a 2a 2a 20 6f 74 68 65 72 20 62 rbing.** other b
a7f0: 79 74 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 ytes in the file
a800: 2e 20 20 54 68 65 20 78 44 65 76 69 63 65 43 68 . The xDeviceCh
a810: 61 72 61 63 74 65 72 69 73 74 69 63 73 28 29 0a aracteristics().
a820: 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e ** method return
a830: 73 20 61 20 62 69 74 20 76 65 63 74 6f 72 20 64 s a bit vector d
a840: 65 73 63 72 69 62 69 6e 67 20 62 65 68 61 76 69 escribing behavi
a850: 6f 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 75 6e ors of the.** un
a860: 64 65 72 6c 79 69 6e 67 20 64 65 76 69 63 65 3a derlying device:
a870: 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c .**.** <ul>.** <
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 5d 0a 2a 2a 20 3c 6c 69 P_ATOMIC].** <li
a8a0: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f > [SQLITE_IOCAP_
a8b0: 41 54 4f 4d 49 43 35 31 32 5d 0a 2a 2a 20 3c 6c ATOMIC512].** <l
a8c0: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 i> [SQLITE_IOCAP
a8d0: 5f 41 54 4f 4d 49 43 31 4b 5d 0a 2a 2a 20 3c 6c _ATOMIC1K].** <l
a8e0: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 i> [SQLITE_IOCAP
a8f0: 5f 41 54 4f 4d 49 43 32 4b 5d 0a 2a 2a 20 3c 6c _ATOMIC2K].** <l
a900: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 i> [SQLITE_IOCAP
a910: 5f 41 54 4f 4d 49 43 34 4b 5d 0a 2a 2a 20 3c 6c _ATOMIC4K].** <l
a920: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 i> [SQLITE_IOCAP
a930: 5f 41 54 4f 4d 49 43 38 4b 5d 0a 2a 2a 20 3c 6c _ATOMIC8K].** <l
a940: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 i> [SQLITE_IOCAP
a950: 5f 41 54 4f 4d 49 43 31 36 4b 5d 0a 2a 2a 20 3c _ATOMIC16K].** <
a960: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 li> [SQLITE_IOCA
a970: 50 5f 41 54 4f 4d 49 43 33 32 4b 5d 0a 2a 2a 20 P_ATOMIC32K].**
a980: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 <li> [SQLITE_IOC
a990: 41 50 5f 41 54 4f 4d 49 43 36 34 4b 5d 0a 2a 2a AP_ATOMIC64K].**
a9a0: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f <li> [SQLITE_IO
a9b0: 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 5d CAP_SAFE_APPEND]
a9c0: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
a9d0: 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 _IOCAP_SEQUENTIA
a9e0: 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a L].** </ul>.**.*
a9f0: 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 * The SQLITE_IOC
aa00: 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 72 AP_ATOMIC proper
aa10: 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6c ty means that al
aa20: 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61 l writes of.** a
aa30: 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f 6d ny size are atom
aa40: 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f ic. The SQLITE_
aa50: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 IOCAP_ATOMICnnn
aa60: 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 values.** mean t
aa70: 68 61 74 20 77 72 69 74 65 73 20 6f 66 20 62 6c hat writes of bl
aa80: 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e 6e ocks that are nn
aa90: 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 n bytes in size
aaa0: 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e and.** are align
aab0: 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73 73 ed to an address
aac0: 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e 74 which is an int
aad0: 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 eger multiple of
aae0: 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d .** nnn are atom
aaf0: 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f ic. The SQLITE_
ab00: 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e IOCAP_SAFE_APPEN
ab10: 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a D value means.**
ab20: 20 74 68 61 74 20 77 68 65 6e 20 64 61 74 61 20 that when data
ab30: 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61 is appended to a
ab40: 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 20 file, the data
ab50: 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66 is appended.** f
ab60: 69 72 73 74 20 74 68 65 6e 20 74 68 65 20 73 69 irst then the si
ab70: 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 ze of the file i
ab80: 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 65 s extended, neve
ab90: 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77 r the other.** w
aba0: 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 20 ay around. The
abb0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 SQLITE_IOCAP_SEQ
abc0: 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79 UENTIAL property
abd0: 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69 means that.** i
abe0: 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 72 nformation is wr
abf0: 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e itten to disk in
ac00: 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 20 the same order
ac10: 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 as calls.** to x
ac20: 57 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 Write()..**.** I
ac30: 66 20 78 52 65 61 64 28 29 20 72 65 74 75 72 6e f xRead() return
ac40: 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 s SQLITE_IOERR_S
ac50: 48 4f 52 54 5f 52 45 41 44 20 69 74 20 6d 75 73 HORT_READ it mus
ac60: 74 20 61 6c 73 6f 20 66 69 6c 6c 0a 2a 2a 20 69 t also fill.** i
ac70: 6e 20 74 68 65 20 75 6e 72 65 61 64 20 70 6f 72 n the unread por
ac80: 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 75 66 tions of the buf
ac90: 66 65 72 20 77 69 74 68 20 7a 65 72 6f 73 2e 20 fer with zeros.
aca0: 20 41 20 56 46 53 20 74 68 61 74 0a 2a 2a 20 66 A VFS that.** f
acb0: 61 69 6c 73 20 74 6f 20 7a 65 72 6f 2d 66 69 6c ails to zero-fil
acc0: 6c 20 73 68 6f 72 74 20 72 65 61 64 73 20 6d 69 l short reads mi
acd0: 67 68 74 20 73 65 65 6d 20 74 6f 20 77 6f 72 6b ght seem to work
ace0: 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20 66 . However,.** f
acf0: 61 69 6c 75 72 65 20 74 6f 20 7a 65 72 6f 2d 66 ailure to zero-f
ad00: 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 73 20 ill short reads
ad10: 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20 will eventually
ad20: 6c 65 61 64 20 74 6f 0a 2a 2a 20 64 61 74 61 62 lead to.** datab
ad30: 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a ase corruption..
ad40: 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
ad50: 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
ad60: 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 69 6f 5f hods sqlite3_io_
ad70: 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 methods;.struct
ad80: 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
ad90: 64 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72 73 ds {. int iVers
ada0: 69 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c ion;. int (*xCl
adb0: 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c ose)(sqlite3_fil
adc0: 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 65 e*);. int (*xRe
add0: 61 64 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ad)(sqlite3_file
ade0: 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41 *, void*, int iA
adf0: 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 mt, sqlite3_int6
ae00: 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20 4 iOfst);. int
ae10: 28 2a 78 57 72 69 74 65 29 28 73 71 6c 69 74 65 (*xWrite)(sqlite
ae20: 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 3_file*, const v
ae30: 6f 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74 2c 20 oid*, int iAmt,
ae40: 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f sqlite3_int64 iO
ae50: 66 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 54 fst);. int (*xT
ae60: 72 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65 33 runcate)(sqlite3
ae70: 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f _file*, sqlite3_
ae80: 69 6e 74 36 34 20 73 69 7a 65 29 3b 0a 20 20 69 int64 size);. i
ae90: 6e 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 nt (*xSync)(sqli
aea0: 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 66 te3_file*, int f
aeb0: 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 lags);. int (*x
aec0: 46 69 6c 65 53 69 7a 65 29 28 73 71 6c 69 74 65 FileSize)(sqlite
aed0: 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33 3_file*, sqlite3
aee0: 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 3b 0a _int64 *pSize);.
aef0: 20 20 69 6e 74 20 28 2a 78 4c 6f 63 6b 29 28 73 int (*xLock)(s
af00: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
af10: 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55 6e 6c t);. int (*xUnl
af20: 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c ock)(sqlite3_fil
af30: 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 e*, int);. int
af40: 28 2a 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 (*xCheckReserved
af50: 4c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 Lock)(sqlite3_fi
af60: 6c 65 2a 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 le*, int *pResOu
af70: 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 6c t);. int (*xFil
af80: 65 43 6f 6e 74 72 6f 6c 29 28 73 71 6c 69 74 65 eControl)(sqlite
af90: 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 6f 70 2c 3_file*, int op,
afa0: 20 76 6f 69 64 20 2a 70 41 72 67 29 3b 0a 20 20 void *pArg);.
afb0: 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69 7a int (*xSectorSiz
afc0: 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a e)(sqlite3_file*
afd0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 76 69 );. int (*xDevi
afe0: 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
aff0: 73 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a s)(sqlite3_file*
b000: 29 3b 0a 20 20 2f 2a 20 41 64 64 69 74 69 6f 6e );. /* Addition
b010: 61 6c 20 6d 65 74 68 6f 64 73 20 6d 61 79 20 62 al methods may b
b020: 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 e added in futur
b030: 65 20 72 65 6c 65 61 73 65 73 20 2a 2f 0a 7d 3b e releases */.};
b040: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
b050: 3a 20 53 74 61 6e 64 61 72 64 20 46 69 6c 65 20 : Standard File
b060: 43 6f 6e 74 72 6f 6c 20 4f 70 63 6f 64 65 73 20 Control Opcodes
b070: 7b 48 31 31 33 31 30 7d 20 3c 53 33 30 38 30 30 {H11310} <S30800
b080: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e >.**.** These in
b090: 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 teger constants
b0a0: 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 are opcodes for
b0b0: 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c the xFileControl
b0c0: 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 method.** of th
b0d0: 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 e [sqlite3_io_me
b0e0: 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61 6e thods] object an
b0f0: 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 d for the [sqlit
b100: 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 e3_file_control(
b110: 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e )].** interface.
b120: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 .**.** The [SQLI
b130: 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 TE_FCNTL_LOCKSTA
b140: 54 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 73 TE] opcode is us
b150: 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 ed for debugging
b160: 2e 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63 6f 64 . This.** opcod
b170: 65 20 63 61 75 73 65 73 20 74 68 65 20 78 46 69 e causes the xFi
b180: 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 leControl method
b190: 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 75 to write the cu
b1a0: 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 0a 2a rrent state of.*
b1b0: 2a 20 74 68 65 20 6c 6f 63 6b 20 28 6f 6e 65 20 * the lock (one
b1c0: 6f 66 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f of [SQLITE_LOCK_
b1d0: 4e 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c NONE], [SQLITE_L
b1e0: 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a 20 OCK_SHARED],.**
b1f0: 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 [SQLITE_LOCK_RES
b200: 45 52 56 45 44 5d 2c 20 5b 53 51 4c 49 54 45 5f ERVED], [SQLITE_
b210: 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f LOCK_PENDING], o
b220: 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45 r [SQLITE_LOCK_E
b230: 58 43 4c 55 53 49 56 45 5d 29 0a 2a 2a 20 69 6e XCLUSIVE]).** in
b240: 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 to an integer th
b250: 61 74 20 74 68 65 20 70 41 72 67 20 61 72 67 75 at the pArg argu
b260: 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f 2e 20 ment points to.
b270: 54 68 69 73 20 63 61 70 61 62 69 6c 69 74 79 0a This capability.
b280: 2a 2a 20 69 73 20 75 73 65 64 20 64 75 72 69 6e ** is used durin
b290: 67 20 74 65 73 74 69 6e 67 20 61 6e 64 20 6f 6e g testing and on
b2a0: 6c 79 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 ly needs to be s
b2b0: 75 70 70 6f 72 74 65 64 20 77 68 65 6e 20 53 51 upported when SQ
b2c0: 4c 49 54 45 5f 54 45 53 54 0a 2a 2a 20 69 73 20 LITE_TEST.** is
b2d0: 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 64 65 66 defined..*/.#def
b2e0: 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c ine SQLITE_FCNTL
b2f0: 5f 4c 4f 43 4b 53 54 41 54 45 20 20 20 20 20 20 _LOCKSTATE
b300: 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 1.#define SQLI
b310: 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 TE_GET_LOCKPROXY
b320: 46 49 4c 45 20 20 20 20 20 20 32 0a 23 64 65 66 FILE 2.#def
b330: 69 6e 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c ine SQLITE_SET_L
b340: 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 20 20 20 OCKPROXYFILE
b350: 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 3.#define SQLI
b360: 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 20 20 20 TE_LAST_ERRNO
b370: 20 20 20 20 20 20 20 20 20 20 34 0a 0a 2f 2a 0a 4../*.
b380: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 ** CAPI3REF: Mut
b390: 65 78 20 48 61 6e 64 6c 65 20 7b 48 31 37 31 31 ex Handle {H1711
b3a0: 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a 0a 2a 0} <S20130>.**.*
b3b0: 2a 20 54 68 65 20 6d 75 74 65 78 20 6d 6f 64 75 * The mutex modu
b3c0: 6c 65 20 77 69 74 68 69 6e 20 53 51 4c 69 74 65 le within SQLite
b3d0: 20 64 65 66 69 6e 65 73 20 5b 73 71 6c 69 74 65 defines [sqlite
b3e0: 33 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65 20 61 3_mutex] to be a
b3f0: 6e 0a 2a 2a 20 61 62 73 74 72 61 63 74 20 74 79 n.** abstract ty
b400: 70 65 20 66 6f 72 20 61 20 6d 75 74 65 78 20 6f pe for a mutex o
b410: 62 6a 65 63 74 2e 20 20 54 68 65 20 53 51 4c 69 bject. The SQLi
b420: 74 65 20 63 6f 72 65 20 6e 65 76 65 72 20 6c 6f te core never lo
b430: 6f 6b 73 0a 2a 2a 20 61 74 20 74 68 65 20 69 6e oks.** at the in
b440: 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 ternal represent
b450: 61 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73 71 6c ation of an [sql
b460: 69 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20 49 74 ite3_mutex]. It
b470: 20 6f 6e 6c 79 0a 2a 2a 20 64 65 61 6c 73 20 77 only.** deals w
b480: 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 ith pointers to
b490: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 the [sqlite3_mut
b4a0: 65 78 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a ex] object..**.*
b4b0: 2a 20 4d 75 74 65 78 65 73 20 61 72 65 20 63 72 * Mutexes are cr
b4c0: 65 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c eated using [sql
b4d0: 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
b4e0: 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ()]..*/.typedef
b4f0: 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d struct sqlite3_m
b500: 75 74 65 78 20 73 71 6c 69 74 65 33 5f 6d 75 74 utex sqlite3_mut
b510: 65 78 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 ex;../*.** CAPI3
b520: 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63 REF: OS Interfac
b530: 65 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 34 30 e Object {H11140
b540: 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a } <S20100>.**.**
b550: 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
b560: 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 the sqlite3_vfs
b570: 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74 object defines t
b580: 68 65 20 69 6e 74 65 72 66 61 63 65 20 62 65 74 he interface bet
b590: 77 65 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 69 ween.** the SQLi
b5a0: 74 65 20 63 6f 72 65 20 61 6e 64 20 74 68 65 20 te core and the
b5b0: 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 underlying opera
b5c0: 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 54 68 ting system. Th
b5d0: 65 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20 74 68 e "vfs".** in th
b5e0: 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f 62 e name of the ob
b5f0: 6a 65 63 74 20 73 74 61 6e 64 73 20 66 6f 72 20 ject stands for
b600: 22 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 "virtual file sy
b610: 73 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 stem"..**.** The
b620: 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 56 value of the iV
b630: 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 73 20 ersion field is
b640: 69 6e 69 74 69 61 6c 6c 79 20 31 20 62 75 74 20 initially 1 but
b650: 6d 61 79 20 62 65 20 6c 61 72 67 65 72 20 69 6e may be larger in
b660: 0a 2a 2a 20 66 75 74 75 72 65 20 76 65 72 73 69 .** future versi
b670: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 ons of SQLite.
b680: 41 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 Additional field
b690: 73 20 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65 s may be appende
b6a0: 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 6f 62 6a d to this.** obj
b6b0: 65 63 74 20 77 68 65 6e 20 74 68 65 20 69 56 65 ect when the iVe
b6c0: 72 73 69 6f 6e 20 76 61 6c 75 65 20 69 73 20 69 rsion value is i
b6d0: 6e 63 72 65 61 73 65 64 2e 20 20 4e 6f 74 65 20 ncreased. Note
b6e0: 74 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75 that the structu
b6f0: 72 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c re.** of the sql
b700: 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 20 ite3_vfs object
b710: 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 74 changes in the t
b720: 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 74 77 65 ransaction betwe
b730: 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 76 65 72 en.** SQLite ver
b740: 73 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64 20 33 sion 3.5.9 and 3
b750: 2e 36 2e 30 20 61 6e 64 20 79 65 74 20 74 68 65 .6.0 and yet the
b760: 20 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 iVersion field
b770: 77 61 73 20 6e 6f 74 0a 2a 2a 20 6d 6f 64 69 66 was not.** modif
b780: 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ied..**.** The s
b790: 7a 4f 73 46 69 6c 65 20 66 69 65 6c 64 20 69 73 zOsFile field is
b7a0: 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
b7b0: 20 73 75 62 63 6c 61 73 73 65 64 20 5b 73 71 6c subclassed [sql
b7c0: 69 74 65 33 5f 66 69 6c 65 5d 0a 2a 2a 20 73 74 ite3_file].** st
b7d0: 72 75 63 74 75 72 65 20 75 73 65 64 20 62 79 20 ructure used by
b7e0: 74 68 69 73 20 56 46 53 2e 20 20 6d 78 50 61 74 this VFS. mxPat
b7f0: 68 6e 61 6d 65 20 69 73 20 74 68 65 20 6d 61 78 hname is the max
b800: 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 0a 2a imum length of.*
b810: 2a 20 61 20 70 61 74 68 6e 61 6d 65 20 69 6e 20 * a pathname in
b820: 74 68 69 73 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 this VFS..**.**
b830: 52 65 67 69 73 74 65 72 65 64 20 73 71 6c 69 74 Registered sqlit
b840: 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 20 61 e3_vfs objects a
b850: 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e re kept on a lin
b860: 6b 65 64 20 6c 69 73 74 20 66 6f 72 6d 65 64 20 ked list formed
b870: 62 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78 74 20 by.** the pNext
b880: 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 5b 73 pointer. The [s
b890: 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 qlite3_vfs_regis
b8a0: 74 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 ter()].** and [s
b8b0: 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 qlite3_vfs_unreg
b8c0: 69 73 74 65 72 28 29 5d 20 69 6e 74 65 72 66 61 ister()] interfa
b8d0: 63 65 73 20 6d 61 6e 61 67 65 20 74 68 69 73 20 ces manage this
b8e0: 6c 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74 68 72 list.** in a thr
b8f0: 65 61 64 2d 73 61 66 65 20 77 61 79 2e 20 20 54 ead-safe way. T
b900: 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f he [sqlite3_vfs_
b910: 66 69 6e 64 28 29 5d 20 69 6e 74 65 72 66 61 63 find()] interfac
b920: 65 0a 2a 2a 20 73 65 61 72 63 68 65 73 20 74 68 e.** searches th
b930: 65 20 6c 69 73 74 2e 20 20 4e 65 69 74 68 65 72 e list. Neither
b940: 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
b950: 20 63 6f 64 65 20 6e 6f 72 20 74 68 65 20 56 46 code nor the VF
b960: 53 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 S.** implementat
b970: 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 20 74 ion should use t
b980: 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 65 72 he pNext pointer
b990: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e 65 78 ..**.** The pNex
b9a0: 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 6f t field is the o
b9b0: 6e 6c 79 20 66 69 65 6c 64 20 69 6e 20 74 68 65 nly field in the
b9c0: 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20 sqlite3_vfs.**
b9d0: 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 53 structure that S
b9e0: 51 4c 69 74 65 20 77 69 6c 6c 20 65 76 65 72 20 QLite will ever
b9f0: 6d 6f 64 69 66 79 2e 20 20 53 51 4c 69 74 65 20 modify. SQLite
ba00: 77 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65 73 73 will only access
ba10: 0a 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 .** or modify th
ba20: 69 73 20 66 69 65 6c 64 20 77 68 69 6c 65 20 68 is field while h
ba30: 6f 6c 64 69 6e 67 20 61 20 70 61 72 74 69 63 75 olding a particu
ba40: 6c 61 72 20 73 74 61 74 69 63 20 6d 75 74 65 78 lar static mutex
ba50: 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 ..** The applica
ba60: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 tion should neve
ba70: 72 20 6d 6f 64 69 66 79 20 61 6e 79 74 68 69 6e r modify anythin
ba80: 67 20 77 69 74 68 69 6e 20 74 68 65 20 73 71 6c g within the sql
ba90: 69 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62 6a 65 ite3_vfs.** obje
baa0: 63 74 20 6f 6e 63 65 20 74 68 65 20 6f 62 6a 65 ct once the obje
bab0: 63 74 20 68 61 73 20 62 65 65 6e 20 72 65 67 69 ct has been regi
bac0: 73 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 stered..**.** Th
bad0: 65 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20 68 6f e zName field ho
bae0: 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 lds the name of
baf0: 74 68 65 20 56 46 53 20 6d 6f 64 75 6c 65 2e 20 the VFS module.
bb00: 20 54 68 65 20 6e 61 6d 65 20 6d 75 73 74 0a 2a The name must.*
bb10: 2a 20 62 65 20 75 6e 69 71 75 65 20 61 63 72 6f * be unique acro
bb20: 73 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64 75 6c ss all VFS modul
bb30: 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 es..**.** SQLite
bb40: 20 77 69 6c 6c 20 67 75 61 72 61 6e 74 65 65 20 will guarantee
bb50: 74 68 61 74 20 74 68 65 20 7a 46 69 6c 65 6e 61 that the zFilena
bb60: 6d 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 me parameter to
bb70: 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 65 69 74 68 xOpen.** is eith
bb80: 65 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 er a NULL pointe
bb90: 72 20 6f 72 20 73 74 72 69 6e 67 20 6f 62 74 61 r or string obta
bba0: 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 78 46 75 ined.** from xFu
bbb0: 6c 6c 50 61 74 68 6e 61 6d 65 28 29 2e 20 20 53 llPathname(). S
bbc0: 51 4c 69 74 65 20 66 75 72 74 68 65 72 20 67 75 QLite further gu
bbd0: 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a 2a 2a arantees that.**
bbe0: 20 74 68 65 20 73 74 72 69 6e 67 20 77 69 6c 6c the string will
bbf0: 20 62 65 20 76 61 6c 69 64 20 61 6e 64 20 75 6e be valid and un
bc00: 63 68 61 6e 67 65 64 20 75 6e 74 69 6c 20 78 43 changed until xC
bc10: 6c 6f 73 65 28 29 20 69 73 0a 2a 2a 20 63 61 6c lose() is.** cal
bc20: 6c 65 64 2e 20 42 65 63 61 75 73 65 20 6f 66 20 led. Because of
bc30: 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e the previous sen
bc40: 74 65 6e 63 65 2c 0a 2a 2a 20 74 68 65 20 5b 73 tence,.** the [s
bc50: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63 61 6e qlite3_file] can
bc60: 20 73 61 66 65 6c 79 20 73 74 6f 72 65 20 61 20 safely store a
bc70: 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a pointer to the.*
bc80: 2a 20 66 69 6c 65 6e 61 6d 65 20 69 66 20 69 74 * filename if it
bc90: 20 6e 65 65 64 73 20 74 6f 20 72 65 6d 65 6d 62 needs to rememb
bca0: 65 72 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 er the filename
bcb0: 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2e for some reason.
bcc0: 0a 2a 2a 20 49 66 20 74 68 65 20 7a 46 69 6c 65 .** If the zFile
bcd0: 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69 name parameter i
bce0: 73 20 78 4f 70 65 6e 20 69 73 20 61 20 4e 55 4c s xOpen is a NUL
bcf0: 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 78 L pointer then x
bd00: 4f 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 76 Open.** must inv
bd10: 65 6e 74 20 69 74 73 20 6f 77 6e 20 74 65 6d 70 ent its own temp
bd20: 6f 72 61 72 79 20 6e 61 6d 65 20 66 6f 72 20 74 orary name for t
bd30: 68 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 65 76 he file. Whenev
bd40: 65 72 20 74 68 65 20 0a 2a 2a 20 78 46 69 6c 65 er the .** xFile
bd50: 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69 name parameter i
bd60: 73 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c 20 61 s NULL it will a
bd70: 6c 73 6f 20 62 65 20 74 68 65 20 63 61 73 65 20 lso be the case
bd80: 74 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c 61 67 that the.** flag
bd90: 73 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c 6c s parameter will
bda0: 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 include [SQLITE
bdb0: 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
bdc0: 4f 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 OSE]..**.** The
bdd0: 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 flags argument t
bde0: 6f 20 78 4f 70 65 6e 28 29 20 69 6e 63 6c 75 64 o xOpen() includ
bdf0: 65 73 20 61 6c 6c 20 62 69 74 73 20 73 65 74 20 es all bits set
be00: 69 6e 0a 2a 2a 20 74 68 65 20 66 6c 61 67 73 20 in.** the flags
be10: 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c argument to [sql
be20: 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e ite3_open_v2()].
be30: 20 20 4f 72 20 69 66 20 5b 73 71 6c 69 74 65 33 Or if [sqlite3
be40: 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 6f 72 20 5b _open()].** or [
be50: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 sqlite3_open16()
be60: 5d 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 ] is used, then
be70: 66 6c 61 67 73 20 69 6e 63 6c 75 64 65 73 20 61 flags includes a
be80: 74 20 6c 65 61 73 74 0a 2a 2a 20 5b 53 51 4c 49 t least.** [SQLI
be90: 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
bea0: 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 E] | [SQLITE_OPE
beb0: 4e 5f 43 52 45 41 54 45 5d 2e 20 0a 2a 2a 20 49 N_CREATE]. .** I
bec0: 66 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e 73 20 f xOpen() opens
bed0: 61 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e 6c 79 a file read-only
bee0: 20 74 68 65 6e 20 69 74 20 73 65 74 73 20 2a 70 then it sets *p
bef0: 4f 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a 20 69 OutFlags to.** i
bf00: 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f 4f nclude [SQLITE_O
bf10: 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e 20 20 PEN_READONLY].
bf20: 4f 74 68 65 72 20 62 69 74 73 20 69 6e 20 2a 70 Other bits in *p
bf30: 4f 75 74 46 6c 61 67 73 20 6d 61 79 20 62 65 20 OutFlags may be
bf40: 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 set..**.** SQLit
bf50: 65 20 77 69 6c 6c 20 61 6c 73 6f 20 61 64 64 20 e will also add
bf60: 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f one of the follo
bf70: 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68 wing flags to th
bf80: 65 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 63 61 6c e xOpen().** cal
bf90: 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 l, depending on
bfa0: 74 68 65 20 6f 62 6a 65 63 74 20 62 65 69 6e 67 the object being
bfb0: 20 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c opened:.**.** <
bfc0: 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 ul>.** <li> [SQ
bfd0: 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 LITE_OPEN_MAIN_D
bfe0: 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c B].** <li> [SQL
bff0: 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f ITE_OPEN_MAIN_JO
c000: 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 URNAL].** <li>
c010: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d [SQLITE_OPEN_TEM
c020: 50 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b P_DB].** <li> [
c030: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 SQLITE_OPEN_TEMP
c040: 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 _JOURNAL].** <li
c050: 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f > [SQLITE_OPEN_
c060: 54 52 41 4e 53 49 45 4e 54 5f 44 42 5d 0a 2a 2a TRANSIENT_DB].**
c070: 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f <li> [SQLITE_O
c080: 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 5d 0a PEN_SUBJOURNAL].
c090: 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 ** <li> [SQLITE
c0a0: 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 _OPEN_MASTER_JOU
c0b0: 52 4e 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a RNAL].** </ul>.*
c0c0: 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 49 2f *.** The file I/
c0d0: 4f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e O implementation
c0e0: 20 63 61 6e 20 75 73 65 20 74 68 65 20 6f 62 6a can use the obj
c0f0: 65 63 74 20 74 79 70 65 20 66 6c 61 67 73 20 74 ect type flags t
c100: 6f 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 65 20 o.** change the
c110: 77 61 79 20 69 74 20 64 65 61 6c 73 20 77 69 74 way it deals wit
c120: 68 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 65 78 h files. For ex
c130: 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63 ample, an applic
c140: 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 64 6f ation.** that do
c150: 65 73 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 es not care abou
c160: 74 20 63 72 61 73 68 20 72 65 63 6f 76 65 72 79 t crash recovery
c170: 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6d 69 67 or rollback mig
c180: 68 74 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6f ht make.** the o
c190: 70 65 6e 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c pen of a journal
c1a0: 20 66 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 file a no-op.
c1b0: 57 72 69 74 65 73 20 74 6f 20 74 68 69 73 20 6a Writes to this j
c1c0: 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 0a 2a 2a 20 ournal would.**
c1d0: 61 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73 2c 20 also be no-ops,
c1e0: 61 6e 64 20 61 6e 79 20 61 74 74 65 6d 70 74 20 and any attempt
c1f0: 74 6f 20 72 65 61 64 20 74 68 65 20 6a 6f 75 72 to read the jour
c200: 6e 61 6c 20 77 6f 75 6c 64 20 72 65 74 75 72 6e nal would return
c210: 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 .** SQLITE_IOERR
c220: 2e 20 20 4f 72 20 74 68 65 20 69 6d 70 6c 65 6d . Or the implem
c230: 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20 72 entation might r
c240: 65 63 6f 67 6e 69 7a 65 20 74 68 61 74 20 61 20 ecognize that a
c250: 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 database.** file
c260: 20 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67 20 70 will be doing p
c270: 61 67 65 2d 61 6c 69 67 6e 65 64 20 73 65 63 74 age-aligned sect
c280: 6f 72 20 72 65 61 64 73 20 61 6e 64 20 77 72 69 or reads and wri
c290: 74 65 73 20 69 6e 20 61 20 72 61 6e 64 6f 6d 0a tes in a random.
c2a0: 2a 2a 20 6f 72 64 65 72 20 61 6e 64 20 73 65 74 ** order and set
c2b0: 20 75 70 20 69 74 73 20 49 2f 4f 20 73 75 62 73 up its I/O subs
c2c0: 79 73 74 65 6d 20 61 63 63 6f 72 64 69 6e 67 6c ystem accordingl
c2d0: 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 y..**.** SQLite
c2e0: 6d 69 67 68 74 20 61 6c 73 6f 20 61 64 64 20 6f might also add o
c2f0: 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ne of the follow
c300: 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68 65 ing flags to the
c310: 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 3a 0a 2a xOpen method:.*
c320: 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ul>.** <li
c330: 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 > [SQLITE_OPEN_D
c340: 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a ELETEONCLOSE].**
c350: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 <li> [SQLITE_OP
c360: 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 0a 2a 2a EN_EXCLUSIVE].**
c370: 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 </ul>.**.** The
c380: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 [SQLITE_OPEN_DE
c390: 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66 6c 61 LETEONCLOSE] fla
c3a0: 67 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c 65 g means the file
c3b0: 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 65 should be.** de
c3c0: 6c 65 74 65 64 20 77 68 65 6e 20 69 74 20 69 73 leted when it is
c3d0: 20 63 6c 6f 73 65 64 2e 20 20 54 68 65 20 5b 53 closed. The [S
c3e0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 QLITE_OPEN_DELET
c3f0: 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77 69 6c EONCLOSE].** wil
c400: 6c 20 62 65 20 73 65 74 20 66 6f 72 20 54 45 4d l be set for TEM
c410: 50 20 20 64 61 74 61 62 61 73 65 73 2c 20 6a 6f P databases, jo
c420: 75 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72 20 73 urnals and for s
c430: 75 62 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a 0a 2a ubjournals..**.*
c440: 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 * The [SQLITE_OP
c450: 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20 66 6c EN_EXCLUSIVE] fl
c460: 61 67 20 69 73 20 61 6c 77 61 79 73 20 75 73 65 ag is always use
c470: 64 20 69 6e 20 63 6f 6e 6a 75 6e 63 74 69 6f 6e d in conjunction
c480: 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 53 51 .** with the [SQ
c490: 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 LITE_OPEN_CREATE
c4a0: 5d 20 66 6c 61 67 2c 20 77 68 69 63 68 20 61 72 ] flag, which ar
c4b0: 65 20 62 6f 74 68 20 64 69 72 65 63 74 6c 79 0a e both directly.
c4c0: 2a 2a 20 61 6e 61 6c 6f 67 6f 75 73 20 74 6f 20 ** analogous to
c4d0: 74 68 65 20 4f 5f 45 58 43 4c 20 61 6e 64 20 4f the O_EXCL and O
c4e0: 5f 43 52 45 41 54 20 66 6c 61 67 73 20 6f 66 20 _CREAT flags of
c4f0: 74 68 65 20 50 4f 53 49 58 20 6f 70 65 6e 28 29 the POSIX open()
c500: 0a 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 53 51 .** API. The SQ
c510: 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 LITE_OPEN_EXCLUS
c520: 49 56 45 20 66 6c 61 67 2c 20 77 68 65 6e 20 70 IVE flag, when p
c530: 61 69 72 65 64 20 77 69 74 68 20 74 68 65 20 0a aired with the .
c540: 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 ** SQLITE_OPEN_C
c550: 52 45 41 54 45 2c 20 69 73 20 75 73 65 64 20 74 REATE, is used t
c560: 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 o indicate that
c570: 66 69 6c 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 file should alwa
c580: 79 73 0a 2a 2a 20 62 65 20 63 72 65 61 74 65 64 ys.** be created
c590: 2c 20 61 6e 64 20 74 68 61 74 20 69 74 20 69 73 , and that it is
c5a0: 20 61 6e 20 65 72 72 6f 72 20 69 66 20 69 74 20 an error if it
c5b0: 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e 0a already exists..
c5c0: 2a 2a 20 49 74 20 69 73 20 3c 69 3e 6e 6f 74 3c ** It is <i>not<
c5d0: 2f 69 3e 20 75 73 65 64 20 74 6f 20 69 6e 64 69 /i> used to indi
c5e0: 63 61 74 65 20 74 68 65 20 66 69 6c 65 20 73 68 cate the file sh
c5f0: 6f 75 6c 64 20 62 65 20 6f 70 65 6e 65 64 20 0a ould be opened .
c600: 2a 2a 20 66 6f 72 20 65 78 63 6c 75 73 69 76 65 ** for exclusive
c610: 20 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 41 access..**.** A
c620: 74 20 6c 65 61 73 74 20 73 7a 4f 73 46 69 6c 65 t least szOsFile
c630: 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 bytes of memory
c640: 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 are allocated b
c650: 79 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f 20 68 y SQLite.** to h
c660: 6f 6c 64 20 74 68 65 20 20 5b 73 71 6c 69 74 65 old the [sqlite
c670: 33 5f 66 69 6c 65 5d 20 73 74 72 75 63 74 75 72 3_file] structur
c680: 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
c690: 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e third.** argumen
c6a0: 74 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 68 65 t to xOpen. The
c6b0: 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 64 6f xOpen method do
c6c0: 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 0a 2a es not have to.*
c6d0: 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 * allocate the s
c6e0: 74 72 75 63 74 75 72 65 3b 20 69 74 20 73 68 6f tructure; it sho
c6f0: 75 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 69 74 uld just fill it
c700: 20 69 6e 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a in. Note that.
c710: 2a 2a 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 ** the xOpen met
c720: 68 6f 64 20 6d 75 73 74 20 73 65 74 20 74 68 65 hod must set the
c730: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d sqlite3_file.pM
c740: 65 74 68 6f 64 73 20 74 6f 20 65 69 74 68 65 72 ethods to either
c750: 0a 2a 2a 20 61 20 76 61 6c 69 64 20 5b 73 71 6c .** a valid [sql
c760: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d ite3_io_methods]
c770: 20 6f 62 6a 65 63 74 20 6f 72 20 74 6f 20 4e 55 object or to NU
c780: 4c 4c 2e 20 20 78 4f 70 65 6e 20 6d 75 73 74 20 LL. xOpen must
c790: 64 6f 0a 2a 2a 20 74 68 69 73 20 65 76 65 6e 20 do.** this even
c7a0: 69 66 20 74 68 65 20 6f 70 65 6e 20 66 61 69 6c if the open fail
c7b0: 73 2e 20 20 53 51 4c 69 74 65 20 65 78 70 65 63 s. SQLite expec
c7c0: 74 73 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 ts that the sqli
c7d0: 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68 6f 64 te3_file.pMethod
c7e0: 73 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 77 69 6c s.** element wil
c7f0: 6c 20 62 65 20 76 61 6c 69 64 20 61 66 74 65 72 l be valid after
c800: 20 78 4f 70 65 6e 20 72 65 74 75 72 6e 73 20 72 xOpen returns r
c810: 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 egardless of the
c820: 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 66 success.** or f
c830: 61 69 6c 75 72 65 20 6f 66 20 74 68 65 20 78 4f ailure of the xO
c840: 70 65 6e 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 pen call..**.**
c850: 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 The flags argume
c860: 6e 74 20 74 6f 20 78 41 63 63 65 73 73 28 29 20 nt to xAccess()
c870: 6d 61 79 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 may be [SQLITE_A
c880: 43 43 45 53 53 5f 45 58 49 53 54 53 5d 0a 2a 2a CCESS_EXISTS].**
c890: 20 74 6f 20 74 65 73 74 20 66 6f 72 20 74 68 65 to test for the
c8a0: 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20 61 20 existence of a
c8b0: 66 69 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 45 file, or [SQLITE
c8c0: 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 _ACCESS_READWRIT
c8d0: 45 5d 20 74 6f 0a 2a 2a 20 74 65 73 74 20 77 68 E] to.** test wh
c8e0: 65 74 68 65 72 20 61 20 66 69 6c 65 20 69 73 20 ether a file is
c8f0: 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69 readable and wri
c900: 74 61 62 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 table, or [SQLIT
c910: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 5d 0a 2a E_ACCESS_READ].*
c920: 2a 20 74 6f 20 74 65 73 74 20 77 68 65 74 68 65 * to test whethe
c930: 72 20 61 20 66 69 6c 65 20 69 73 20 61 74 20 6c r a file is at l
c940: 65 61 73 74 20 72 65 61 64 61 62 6c 65 2e 20 20 east readable.
c950: 20 54 68 65 20 66 69 6c 65 20 63 61 6e 20 62 65 The file can be
c960: 20 61 0a 2a 2a 20 64 69 72 65 63 74 6f 72 79 2e a.** directory.
c970: 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 .**.** SQLite wi
c980: 6c 6c 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61 ll always alloca
c990: 74 65 20 61 74 20 6c 65 61 73 74 20 6d 78 50 61 te at least mxPa
c9a0: 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 20 66 thname+1 bytes f
c9b0: 6f 72 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 or the.** output
c9c0: 20 62 75 66 66 65 72 20 78 46 75 6c 6c 50 61 74 buffer xFullPat
c9d0: 68 6e 61 6d 65 2e 20 20 54 68 65 20 65 78 61 63 hname. The exac
c9e0: 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 t size of the ou
c9f0: 74 70 75 74 20 62 75 66 66 65 72 0a 2a 2a 20 69 tput buffer.** i
ca00: 73 20 61 6c 73 6f 20 70 61 73 73 65 64 20 61 73 s also passed as
ca10: 20 61 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 a parameter to
ca20: 62 6f 74 68 20 20 6d 65 74 68 6f 64 73 2e 20 49 both methods. I
ca30: 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 f the output buf
ca40: 66 65 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 fer.** is not la
ca50: 72 67 65 20 65 6e 6f 75 67 68 2c 20 5b 53 51 4c rge enough, [SQL
ca60: 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 73 68 ITE_CANTOPEN] sh
ca70: 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 ould be returned
ca80: 2e 20 53 69 6e 63 65 20 74 68 69 73 20 69 73 0a . Since this is.
ca90: 2a 2a 20 68 61 6e 64 6c 65 64 20 61 73 20 61 20 ** handled as a
caa0: 66 61 74 61 6c 20 65 72 72 6f 72 20 62 79 20 53 fatal error by S
cab0: 51 4c 69 74 65 2c 20 76 66 73 20 69 6d 70 6c 65 QLite, vfs imple
cac0: 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c mentations shoul
cad0: 64 20 65 6e 64 65 61 76 6f 72 0a 2a 2a 20 74 6f d endeavor.** to
cae0: 20 70 72 65 76 65 6e 74 20 74 68 69 73 20 62 79 prevent this by
caf0: 20 73 65 74 74 69 6e 67 20 6d 78 50 61 74 68 6e setting mxPathn
cb00: 61 6d 65 20 74 6f 20 61 20 73 75 66 66 69 63 69 ame to a suffici
cb10: 65 6e 74 6c 79 20 6c 61 72 67 65 20 76 61 6c 75 ently large valu
cb20: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 61 e..**.** The xRa
cb30: 6e 64 6f 6d 6e 65 73 73 28 29 2c 20 78 53 6c 65 ndomness(), xSle
cb40: 65 70 28 29 2c 20 61 6e 64 20 78 43 75 72 72 65 ep(), and xCurre
cb50: 6e 74 54 69 6d 65 28 29 20 69 6e 74 65 72 66 61 ntTime() interfa
cb60: 63 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 73 ces.** are not s
cb70: 74 72 69 63 74 6c 79 20 61 20 70 61 72 74 20 6f trictly a part o
cb80: 66 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d f the filesystem
cb90: 2c 20 62 75 74 20 74 68 65 79 20 61 72 65 0a 2a , but they are.*
cba0: 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 * included in th
cbb0: 65 20 56 46 53 20 73 74 72 75 63 74 75 72 65 20 e VFS structure
cbc0: 66 6f 72 20 63 6f 6d 70 6c 65 74 65 6e 65 73 73 for completeness
cbd0: 2e 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d ..** The xRandom
cbe0: 6e 65 73 73 28 29 20 66 75 6e 63 74 69 6f 6e 20 ness() function
cbf0: 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 74 75 attempts to retu
cc00: 72 6e 20 6e 42 79 74 65 73 20 62 79 74 65 73 0a rn nBytes bytes.
cc10: 2a 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 61 6c 69 ** of good-quali
cc20: 74 79 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e ty randomness in
cc30: 74 6f 20 7a 4f 75 74 2e 20 20 54 68 65 20 72 65 to zOut. The re
cc40: 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a turn value is.**
cc50: 20 74 68 65 20 61 63 74 75 61 6c 20 6e 75 6d 62 the actual numb
cc60: 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 72 er of bytes of r
cc70: 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e andomness obtain
cc80: 65 64 2e 0a 2a 2a 20 54 68 65 20 78 53 6c 65 65 ed..** The xSlee
cc90: 70 28 29 20 6d 65 74 68 6f 64 20 63 61 75 73 65 p() method cause
cca0: 73 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 s the calling th
ccb0: 72 65 61 64 20 74 6f 20 73 6c 65 65 70 20 66 6f read to sleep fo
ccc0: 72 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 74 68 r at.** least th
ccd0: 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 e number of micr
cce0: 6f 73 65 63 6f 6e 64 73 20 67 69 76 65 6e 2e 20 oseconds given.
ccf0: 20 54 68 65 20 78 43 75 72 72 65 6e 74 54 69 6d The xCurrentTim
cd00: 65 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65 e().** method re
cd10: 74 75 72 6e 73 20 61 20 4a 75 6c 69 61 6e 20 44 turns a Julian D
cd20: 61 79 20 4e 75 6d 62 65 72 20 66 6f 72 20 74 68 ay Number for th
cd30: 65 20 63 75 72 72 65 6e 74 20 64 61 74 65 20 61 e current date a
cd40: 6e 64 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2f 0a 74 nd time..**.*/.t
cd50: 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 ypedef struct sq
cd60: 6c 69 74 65 33 5f 76 66 73 20 73 71 6c 69 74 65 lite3_vfs sqlite
cd70: 33 5f 76 66 73 3b 0a 73 74 72 75 63 74 20 73 71 3_vfs;.struct sq
cd80: 6c 69 74 65 33 5f 76 66 73 20 7b 0a 20 20 69 6e lite3_vfs {. in
cd90: 74 20 69 56 65 72 73 69 6f 6e 3b 20 20 20 20 20 t iVersion;
cda0: 20 20 20 20 20 20 20 2f 2a 20 53 74 72 75 63 74 /* Struct
cdb0: 75 72 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 ure version numb
cdc0: 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4f 73 er */. int szOs
cdd0: 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 File;
cde0: 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 75 62 63 /* Size of subc
cdf0: 6c 61 73 73 65 64 20 73 71 6c 69 74 65 33 5f 66 lassed sqlite3_f
ce00: 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 ile */. int mxP
ce10: 61 74 68 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 athname;
ce20: 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 66 69 6c /* Maximum fil
ce30: 65 20 70 61 74 68 6e 61 6d 65 20 6c 65 6e 67 74 e pathname lengt
ce40: 68 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 h */. sqlite3_v
ce50: 66 73 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 fs *pNext;
ce60: 2f 2a 20 4e 65 78 74 20 72 65 67 69 73 74 65 72 /* Next register
ce70: 65 64 20 56 46 53 20 2a 2f 0a 20 20 63 6f 6e 73 ed VFS */. cons
ce80: 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 t char *zName;
ce90: 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
cea0: 74 68 69 73 20 76 69 72 74 75 61 6c 20 66 69 6c this virtual fil
ceb0: 65 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20 76 6f e system */. vo
cec0: 69 64 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20 id *pAppData;
ced0: 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
cee0: 72 20 74 6f 20 61 70 70 6c 69 63 61 74 69 6f 6e r to application
cef0: 2d 73 70 65 63 69 66 69 63 20 64 61 74 61 20 2a -specific data *
cf00: 2f 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 /. int (*xOpen)
cf10: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 (sqlite3_vfs*, c
cf20: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
cf30: 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c , sqlite3_file*,
cf40: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
cf50: 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a int flags, int *
cf60: 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 69 6e pOutFlags);. in
cf70: 74 20 28 2a 78 44 65 6c 65 74 65 29 28 73 71 6c t (*xDelete)(sql
cf80: 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 ite3_vfs*, const
cf90: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e char *zName, in
cfa0: 74 20 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 6e t syncDir);. in
cfb0: 74 20 28 2a 78 41 63 63 65 73 73 29 28 73 71 6c t (*xAccess)(sql
cfc0: 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 ite3_vfs*, const
cfd0: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e char *zName, in
cfe0: 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 52 t flags, int *pR
cff0: 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a esOut);. int (*
d000: 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 28 73 xFullPathname)(s
d010: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e qlite3_vfs*, con
d020: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
d030: 69 6e 74 20 6e 4f 75 74 2c 20 63 68 61 72 20 2a int nOut, char *
d040: 7a 4f 75 74 29 3b 0a 20 20 76 6f 69 64 20 2a 28 zOut);. void *(
d050: 2a 78 44 6c 4f 70 65 6e 29 28 73 71 6c 69 74 65 *xDlOpen)(sqlite
d060: 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 3_vfs*, const ch
d070: 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a ar *zFilename);.
d080: 20 20 76 6f 69 64 20 28 2a 78 44 6c 45 72 72 6f void (*xDlErro
d090: 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c r)(sqlite3_vfs*,
d0a0: 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 int nByte, char
d0b0: 20 2a 7a 45 72 72 4d 73 67 29 3b 0a 20 20 76 6f *zErrMsg);. vo
d0c0: 69 64 20 28 2a 28 2a 78 44 6c 53 79 6d 29 28 73 id (*(*xDlSym)(s
d0d0: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 76 6f 69 64 qlite3_vfs*,void
d0e0: 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a *, const char *z
d0f0: 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 3b 0a Symbol))(void);.
d100: 20 20 76 6f 69 64 20 28 2a 78 44 6c 43 6c 6f 73 void (*xDlClos
d110: 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c e)(sqlite3_vfs*,
d120: 20 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 28 void*);. int (
d130: 2a 78 52 61 6e 64 6f 6d 6e 65 73 73 29 28 73 71 *xRandomness)(sq
d140: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 lite3_vfs*, int
d150: 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 4f 75 nByte, char *zOu
d160: 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 6c 65 t);. int (*xSle
d170: 65 70 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a ep)(sqlite3_vfs*
d180: 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 6f 6e , int microsecon
d190: 64 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 75 ds);. int (*xCu
d1a0: 72 72 65 6e 74 54 69 6d 65 29 28 73 71 6c 69 74 rrentTime)(sqlit
d1b0: 65 33 5f 76 66 73 2a 2c 20 64 6f 75 62 6c 65 2a e3_vfs*, double*
d1c0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 47 65 74 4c );. int (*xGetL
d1d0: 61 73 74 45 72 72 6f 72 29 28 73 71 6c 69 74 65 astError)(sqlite
d1e0: 33 5f 76 66 73 2a 2c 20 69 6e 74 2c 20 63 68 61 3_vfs*, int, cha
d1f0: 72 20 2a 29 3b 0a 20 20 2f 2a 20 4e 65 77 20 66 r *);. /* New f
d200: 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61 70 70 ields may be app
d210: 65 6e 64 65 64 20 69 6e 20 66 69 67 75 72 65 20 ended in figure
d220: 76 65 72 73 69 6f 6e 73 2e 20 20 54 68 65 20 69 versions. The i
d230: 56 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 76 61 6c Version. ** val
d240: 75 65 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65 6e ue will incremen
d250: 74 20 77 68 65 6e 65 76 65 72 20 74 68 69 73 20 t whenever this
d260: 68 61 70 70 65 6e 73 2e 20 2a 2f 0a 7d 3b 0a 0a happens. */.};..
d270: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
d280: 46 6c 61 67 73 20 66 6f 72 20 74 68 65 20 78 41 Flags for the xA
d290: 63 63 65 73 73 20 56 46 53 20 6d 65 74 68 6f 64 ccess VFS method
d2a0: 20 7b 48 31 31 31 39 30 7d 20 3c 48 31 31 31 34 {H11190} <H1114
d2b0: 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 0>.**.** These i
d2c0: 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 nteger constants
d2d0: 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 can be used as
d2e0: 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 the third parame
d2f0: 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 78 41 ter to.** the xA
d300: 63 63 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 ccess method of
d310: 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d an [sqlite3_vfs]
d320: 20 6f 62 6a 65 63 74 2e 20 7b 45 4e 44 7d 20 20 object. {END}
d330: 54 68 65 79 20 64 65 74 65 72 6d 69 6e 65 0a 2a They determine.*
d340: 2a 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 70 * what kind of p
d350: 65 72 6d 69 73 73 69 6f 6e 73 20 74 68 65 20 78 ermissions the x
d360: 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 69 73 Access method is
d370: 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a 2a 2a looking for..**
d380: 20 57 69 74 68 20 53 51 4c 49 54 45 5f 41 43 43 With SQLITE_ACC
d390: 45 53 53 5f 45 58 49 53 54 53 2c 20 74 68 65 20 ESS_EXISTS, the
d3a0: 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a xAccess method.*
d3b0: 2a 20 73 69 6d 70 6c 79 20 63 68 65 63 6b 73 20 * simply checks
d3c0: 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c 65 whether the file
d3d0: 20 65 78 69 73 74 73 2e 0a 2a 2a 20 57 69 74 68 exists..** With
d3e0: 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 SQLITE_ACCESS_R
d3f0: 45 41 44 57 52 49 54 45 2c 20 74 68 65 20 78 41 EADWRITE, the xA
d400: 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 ccess method.**
d410: 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 20 74 checks whether t
d420: 68 65 20 66 69 6c 65 20 69 73 20 62 6f 74 68 20 he file is both
d430: 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69 readable and wri
d440: 74 61 62 6c 65 2e 0a 2a 2a 20 57 69 74 68 20 53 table..** With S
d450: 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 QLITE_ACCESS_REA
d460: 44 2c 20 74 68 65 20 78 41 63 63 65 73 73 20 6d D, the xAccess m
d470: 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b 73 20 ethod.** checks
d480: 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c 65 whether the file
d490: 20 69 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2f is readable..*/
d4a0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
d4b0: 41 43 43 45 53 53 5f 45 58 49 53 54 53 20 20 20 ACCESS_EXISTS
d4c0: 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 0.#define SQLIT
d4d0: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 E_ACCESS_READWRI
d4e0: 54 45 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c TE 1.#define SQL
d4f0: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 20 ITE_ACCESS_READ
d500: 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 43 41 2../*.** CA
d510: 50 49 33 52 45 46 3a 20 49 6e 69 74 69 61 6c 69 PI3REF: Initiali
d520: 7a 65 20 54 68 65 20 53 51 4c 69 74 65 20 4c 69 ze The SQLite Li
d530: 62 72 61 72 79 20 7b 48 31 30 31 33 30 7d 20 3c brary {H10130} <
d540: 53 32 30 30 30 30 3e 3c 53 33 30 31 30 30 3e 0a S20000><S30100>.
d550: 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
d560: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 3_initialize() r
d570: 6f 75 74 69 6e 65 20 69 6e 69 74 69 61 6c 69 7a outine initializ
d580: 65 73 20 74 68 65 0a 2a 2a 20 53 51 4c 69 74 65 es the.** SQLite
d590: 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 73 library. The s
d5a0: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
d5b0: 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 65 61 ) routine.** dea
d5c0: 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20 72 65 73 llocates any res
d5d0: 6f 75 72 63 65 73 20 74 68 61 74 20 77 65 72 65 ources that were
d5e0: 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 allocated by sq
d5f0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
d600: 28 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c ()..**.** A call
d610: 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 to sqlite3_init
d620: 69 61 6c 69 7a 65 28 29 20 69 73 20 61 6e 20 22 ialize() is an "
d630: 65 66 66 65 63 74 69 76 65 22 20 63 61 6c 6c 20 effective" call
d640: 69 66 20 69 74 20 69 73 0a 2a 2a 20 74 68 65 20 if it is.** the
d650: 66 69 72 73 74 20 74 69 6d 65 20 73 71 6c 69 74 first time sqlit
d660: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
d670: 69 73 20 69 6e 76 6f 6b 65 64 20 64 75 72 69 6e is invoked durin
d680: 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f g the lifetime o
d690: 66 0a 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73 f.** the process
d6a0: 2c 20 6f 72 20 69 66 20 69 74 20 69 73 20 74 68 , or if it is th
d6b0: 65 20 66 69 72 73 74 20 74 69 6d 65 20 73 71 6c e first time sql
d6c0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
d6d0: 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 ) is invoked.**
d6e0: 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c following a call
d6f0: 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 74 to sqlite3_shut
d700: 64 6f 77 6e 28 29 2e 20 20 4f 6e 6c 79 20 61 6e down(). Only an
d710: 20 65 66 66 65 63 74 69 76 65 20 63 61 6c 6c 0a effective call.
d720: 2a 2a 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6e ** of sqlite3_in
d730: 69 74 69 61 6c 69 7a 65 28 29 20 64 6f 65 73 20 itialize() does
d740: 61 6e 79 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 any initializati
d750: 6f 6e 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20 63 on. All other c
d760: 61 6c 6c 73 0a 2a 2a 20 61 72 65 20 68 61 72 6d alls.** are harm
d770: 6c 65 73 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a less no-ops..**.
d780: 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c ** A call to sql
d790: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 ite3_shutdown()
d7a0: 69 73 20 61 6e 20 22 65 66 66 65 63 74 69 76 65 is an "effective
d7b0: 22 20 63 61 6c 6c 20 69 66 20 69 74 20 69 73 20 " call if it is
d7c0: 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 63 61 6c the first.** cal
d7d0: 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 l to sqlite3_shu
d7e0: 74 64 6f 77 6e 28 29 20 73 69 6e 63 65 20 74 68 tdown() since th
d7f0: 65 20 6c 61 73 74 20 73 71 6c 69 74 65 33 5f 69 e last sqlite3_i
d800: 6e 69 74 69 61 6c 69 7a 65 28 29 2e 20 20 4f 6e nitialize(). On
d810: 6c 79 0a 2a 2a 20 61 6e 20 65 66 66 65 63 74 69 ly.** an effecti
d820: 76 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 ve call to sqlit
d830: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 64 6f e3_shutdown() do
d840: 65 73 20 61 6e 79 20 64 65 69 6e 69 74 69 61 6c es any deinitial
d850: 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 20 41 6c 6c 20 ization..** All
d860: 6f 74 68 65 72 20 63 61 6c 6c 73 20 74 6f 20 73 other calls to s
d870: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
d880: 29 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e ) are harmless n
d890: 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 6d 6f o-ops..**.** Amo
d8a0: 6e 67 20 6f 74 68 65 72 20 74 68 69 6e 67 73 2c ng other things,
d8b0: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
d8c0: 69 7a 65 28 29 20 73 68 61 6c 6c 20 69 6e 76 6f ize() shall invo
d8d0: 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 ke.** sqlite3_os
d8e0: 5f 69 6e 69 74 28 29 2e 20 20 53 69 6d 69 6c 61 _init(). Simila
d8f0: 72 6c 79 2c 20 73 71 6c 69 74 65 33 5f 73 68 75 rly, sqlite3_shu
d900: 74 64 6f 77 6e 28 29 0a 2a 2a 20 73 68 61 6c 6c tdown().** shall
d910: 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f invoke sqlite3_
d920: 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a 2a 20 os_end()..**.**
d930: 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 The sqlite3_init
d940: 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65 ialize() routine
d950: 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
d960: 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 2e _OK] on success.
d970: 0a 2a 2a 20 49 66 20 66 6f 72 20 73 6f 6d 65 20 .** If for some
d980: 72 65 61 73 6f 6e 2c 20 73 71 6c 69 74 65 33 5f reason, sqlite3_
d990: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 initialize() is
d9a0: 75 6e 61 62 6c 65 20 74 6f 20 69 6e 69 74 69 61 unable to initia
d9b0: 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72 lize.** the libr
d9c0: 61 72 79 20 28 70 65 72 68 61 70 73 20 69 74 20 ary (perhaps it
d9d0: 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c is unable to all
d9e0: 6f 63 61 74 65 20 61 20 6e 65 65 64 65 64 20 72 ocate a needed r
d9f0: 65 73 6f 75 72 63 65 20 73 75 63 68 0a 2a 2a 20 esource such.**
da00: 61 73 20 61 20 6d 75 74 65 78 29 20 69 74 20 72 as a mutex) it r
da10: 65 74 75 72 6e 73 20 61 6e 20 5b 65 72 72 6f 72 eturns an [error
da20: 20 63 6f 64 65 5d 20 6f 74 68 65 72 20 74 68 61 code] other tha
da30: 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a n [SQLITE_OK]..*
da40: 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
da50: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 6f _initialize() ro
da60: 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
da70: 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 6d 61 internally by ma
da80: 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 53 51 4c 69 ny other.** SQLi
da90: 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 73 6f te interfaces so
daa0: 20 74 68 61 74 20 61 6e 20 61 70 70 6c 69 63 61 that an applica
dab0: 74 69 6f 6e 20 75 73 75 61 6c 6c 79 20 64 6f 65 tion usually doe
dac0: 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0a 2a 2a s not need to.**
dad0: 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f invoke sqlite3_
dae0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 69 72 initialize() dir
daf0: 65 63 74 6c 79 2e 20 20 46 6f 72 20 65 78 61 6d ectly. For exam
db00: 70 6c 65 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 ple, [sqlite3_op
db10: 65 6e 28 29 5d 0a 2a 2a 20 63 61 6c 6c 73 20 73 en()].** calls s
db20: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
db30: 65 28 29 20 73 6f 20 74 68 65 20 53 51 4c 69 74 e() so the SQLit
db40: 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 62 e library will b
db50: 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a e automatically.
db60: 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 ** initialized w
db70: 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 hen [sqlite3_ope
db80: 6e 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 20 69 n()] is called i
db90: 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62 65 20 f it has not be
dba0: 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a 2a 20 61 initialized.** a
dbb0: 6c 72 65 61 64 79 2e 20 20 48 6f 77 65 76 65 72 lready. However
dbc0: 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 63 , if SQLite is c
dbd0: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 ompiled with the
dbe0: 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 [SQLITE_OMIT_AU
dbf0: 54 4f 49 4e 49 54 5d 0a 2a 2a 20 63 6f 6d 70 69 TOINIT].** compi
dc00: 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2c 20 le-time option,
dc10: 74 68 65 6e 20 74 68 65 20 61 75 74 6f 6d 61 74 then the automat
dc20: 69 63 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 ic calls to sqli
dc30: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
dc40: 0a 2a 2a 20 61 72 65 20 6f 6d 69 74 74 65 64 20 .** are omitted
dc50: 61 6e 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 and the applicat
dc60: 69 6f 6e 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 ion must call sq
dc70: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
dc80: 28 29 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 70 () directly.** p
dc90: 72 69 6f 72 20 74 6f 20 75 73 69 6e 67 20 61 6e rior to using an
dca0: 79 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 69 y other SQLite i
dcb0: 6e 74 65 72 66 61 63 65 2e 20 20 46 6f 72 20 6d nterface. For m
dcc0: 61 78 69 6d 75 6d 20 70 6f 72 74 61 62 69 6c 69 aximum portabili
dcd0: 74 79 2c 0a 2a 2a 20 69 74 20 69 73 20 72 65 63 ty,.** it is rec
dce0: 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 61 70 ommended that ap
dcf0: 70 6c 69 63 61 74 69 6f 6e 73 20 61 6c 77 61 79 plications alway
dd00: 73 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 s invoke sqlite3
dd10: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 0a 2a 2a _initialize().**
dd20: 20 64 69 72 65 63 74 6c 79 20 70 72 69 6f 72 20 directly prior
dd30: 74 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f 74 68 to using any oth
dd40: 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 er SQLite interf
dd50: 61 63 65 2e 20 20 46 75 74 75 72 65 20 72 65 6c ace. Future rel
dd60: 65 61 73 65 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 eases.** of SQLi
dd70: 74 65 20 6d 61 79 20 72 65 71 75 69 72 65 20 74 te may require t
dd80: 68 69 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 his. In other w
dd90: 6f 72 64 73 2c 20 74 68 65 20 62 65 68 61 76 69 ords, the behavi
dda0: 6f 72 20 65 78 68 69 62 69 74 65 64 0a 2a 2a 20 or exhibited.**
ddb0: 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 when SQLite is c
ddc0: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 5b 53 51 ompiled with [SQ
ddd0: 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e LITE_OMIT_AUTOIN
dde0: 49 54 5d 20 6d 69 67 68 74 20 62 65 63 6f 6d 65 IT] might become
ddf0: 20 74 68 65 0a 2a 2a 20 64 65 66 61 75 6c 74 20 the.** default
de00: 62 65 68 61 76 69 6f 72 20 69 6e 20 73 6f 6d 65 behavior in some
de10: 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 20 future release
de20: 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a of SQLite..**.**
de30: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f The sqlite3_os_
de40: 69 6e 69 74 28 29 20 72 6f 75 74 69 6e 65 20 64 init() routine d
de50: 6f 65 73 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 oes operating-sy
de60: 73 74 65 6d 20 73 70 65 63 69 66 69 63 0a 2a 2a stem specific.**
de70: 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
de80: 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 of the SQLite li
de90: 62 72 61 72 79 2e 20 20 54 68 65 20 73 71 6c 69 brary. The sqli
dea0: 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 te3_os_end().**
deb0: 72 6f 75 74 69 6e 65 20 75 6e 64 6f 65 73 20 74 routine undoes t
dec0: 68 65 20 65 66 66 65 63 74 20 6f 66 20 73 71 6c he effect of sql
ded0: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 ite3_os_init().
dee0: 20 54 79 70 69 63 61 6c 20 74 61 73 6b 73 0a 2a Typical tasks.*
def0: 2a 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 * performed by t
df00: 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6e hese routines in
df10: 63 6c 75 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e clude allocation
df20: 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6f 6e or deallocation
df30: 0a 2a 2a 20 6f 66 20 73 74 61 74 69 63 20 72 65 .** of static re
df40: 73 6f 75 72 63 65 73 2c 20 69 6e 69 74 69 61 6c sources, initial
df50: 69 7a 61 74 69 6f 6e 20 6f 66 20 67 6c 6f 62 61 ization of globa
df60: 6c 20 76 61 72 69 61 62 6c 65 73 2c 0a 2a 2a 20 l variables,.**
df70: 73 65 74 74 69 6e 67 20 75 70 20 61 20 64 65 66 setting up a def
df80: 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 ault [sqlite3_vf
df90: 73 5d 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 73 65 s] module, or se
dfa0: 74 74 69 6e 67 20 75 70 0a 2a 2a 20 61 20 64 65 tting up.** a de
dfb0: 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 fault configurat
dfc0: 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 ion using [sqlit
dfd0: 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a 2a 2a e3_config()]..**
dfe0: 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 .** The applicat
dff0: 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 ion should never
e000: 20 69 6e 76 6f 6b 65 20 65 69 74 68 65 72 20 73 invoke either s
e010: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 qlite3_os_init()
e020: 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f .** or sqlite3_o
e030: 73 5f 65 6e 64 28 29 20 64 69 72 65 63 74 6c 79 s_end() directly
e040: 2e 20 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 . The applicati
e050: 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 69 on should only i
e060: 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 nvoke.** sqlite3
e070: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e _initialize() an
e080: 64 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f d sqlite3_shutdo
e090: 77 6e 28 29 2e 20 20 54 68 65 20 73 71 6c 69 74 wn(). The sqlit
e0a0: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20 e3_os_init().**
e0b0: 69 6e 74 65 72 66 61 63 65 20 69 73 20 63 61 6c interface is cal
e0c0: 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c led automaticall
e0d0: 79 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69 y by sqlite3_ini
e0e0: 74 69 61 6c 69 7a 65 28 29 20 61 6e 64 0a 2a 2a tialize() and.**
e0f0: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
e100: 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 73 ) is called by s
e110: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
e120: 29 2e 20 20 41 70 70 72 6f 70 72 69 61 74 65 0a ). Appropriate.
e130: 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
e140: 6e 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f ns for sqlite3_o
e150: 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c s_init() and sql
e160: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a ite3_os_end().**
e170: 20 61 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20 are built into
e180: 53 51 4c 69 74 65 20 77 68 65 6e 20 69 74 20 69 SQLite when it i
e190: 73 20 63 6f 6d 70 69 6c 65 64 20 66 6f 72 20 55 s compiled for U
e1a0: 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20 6f 72 nix, Windows, or
e1b0: 20 4f 53 2f 32 2e 0a 2a 2a 20 57 68 65 6e 20 5b OS/2..** When [
e1c0: 63 75 73 74 6f 6d 20 62 75 69 6c 64 73 20 7c 20 custom builds |
e1d0: 62 75 69 6c 74 20 66 6f 72 20 6f 74 68 65 72 20 built for other
e1e0: 70 6c 61 74 66 6f 72 6d 73 5d 0a 2a 2a 20 28 75 platforms].** (u
e1f0: 73 69 6e 67 20 74 68 65 20 5b 53 51 4c 49 54 45 sing the [SQLITE
e200: 5f 4f 53 5f 4f 54 48 45 52 3d 31 5d 20 63 6f 6d _OS_OTHER=1] com
e210: 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6f 70 74 pile-time.** opt
e220: 69 6f 6e 29 20 74 68 65 20 61 70 70 6c 69 63 61 ion) the applica
e230: 74 69 6f 6e 20 6d 75 73 74 20 73 75 70 70 6c 79 tion must supply
e240: 20 61 20 73 75 69 74 61 62 6c 65 20 69 6d 70 6c a suitable impl
e250: 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 0a 2a ementation for.*
e260: 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 * sqlite3_os_ini
e270: 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f t() and sqlite3_
e280: 6f 73 5f 65 6e 64 28 29 2e 20 20 41 6e 20 61 70 os_end(). An ap
e290: 70 6c 69 63 61 74 69 6f 6e 2d 73 75 70 70 6c 69 plication-suppli
e2a0: 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 ed.** implementa
e2b0: 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f tion of sqlite3_
e2c0: 6f 73 5f 69 6e 69 74 28 29 20 6f 72 20 73 71 6c os_init() or sql
e2d0: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a ite3_os_end().**
e2e0: 20 6d 75 73 74 20 72 65 74 75 72 6e 20 5b 53 51 must return [SQ
e2f0: 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 LITE_OK] on succ
e300: 65 73 73 20 61 6e 64 20 73 6f 6d 65 20 6f 74 68 ess and some oth
e310: 65 72 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 er [error code]
e320: 75 70 6f 6e 0a 2a 2a 20 66 61 69 6c 75 72 65 2e upon.** failure.
e330: 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
e340: 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 nt sqlite3_initi
e350: 61 6c 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c alize(void);.SQL
e360: 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
e370: 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 76 6f 69 te3_shutdown(voi
e380: 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 d);.SQLITE_API i
e390: 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e nt sqlite3_os_in
e3a0: 69 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 it(void);.SQLITE
e3b0: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
e3c0: 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 3b 0a 0a _os_end(void);..
e3d0: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
e3e0: 43 6f 6e 66 69 67 75 72 69 6e 67 20 54 68 65 20 Configuring The
e3f0: 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79 20 7b SQLite Library {
e400: 48 31 34 31 30 30 7d 20 3c 53 32 30 30 30 30 3e H14100} <S20000>
e410: 3c 53 33 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45 <S30200>.** EXPE
e420: 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 RIMENTAL.**.** T
e430: 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 he sqlite3_confi
e440: 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 g() interface is
e450: 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 67 6c used to make gl
e460: 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 obal configurati
e470: 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f on.** changes to
e480: 20 53 51 4c 69 74 65 20 69 6e 20 6f 72 64 65 72 SQLite in order
e490: 20 74 6f 20 74 75 6e 65 20 53 51 4c 69 74 65 20 to tune SQLite
e4a0: 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 63 20 to the specific
e4b0: 6e 65 65 64 73 20 6f 66 0a 2a 2a 20 74 68 65 20 needs of.** the
e4c0: 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54 68 application. Th
e4d0: 65 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 e default config
e4e0: 75 72 61 74 69 6f 6e 20 69 73 20 72 65 63 6f 6d uration is recom
e4f0: 6d 65 6e 64 65 64 20 66 6f 72 20 6d 6f 73 74 0a mended for most.
e500: 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 ** applications
e510: 61 6e 64 20 73 6f 20 74 68 69 73 20 72 6f 75 74 and so this rout
e520: 69 6e 65 20 69 73 20 75 73 75 61 6c 6c 79 20 6e ine is usually n
e530: 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20 20 49 ot necessary. I
e540: 74 20 69 73 0a 2a 2a 20 70 72 6f 76 69 64 65 64 t is.** provided
e550: 20 74 6f 20 73 75 70 70 6f 72 74 20 72 61 72 65 to support rare
e560: 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 77 69 applications wi
e570: 74 68 20 75 6e 75 73 75 61 6c 20 6e 65 65 64 73 th unusual needs
e580: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
e590: 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 te3_config() int
e5a0: 65 72 66 61 63 65 20 69 73 20 6e 6f 74 20 74 68 erface is not th
e5b0: 72 65 61 64 73 61 66 65 2e 20 20 54 68 65 20 61 readsafe. The a
e5c0: 70 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20 6d 75 pplication.** mu
e5d0: 73 74 20 69 6e 73 75 72 65 20 74 68 61 74 20 6e st insure that n
e5e0: 6f 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 69 o other SQLite i
e5f0: 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 69 6e nterfaces are in
e600: 76 6f 6b 65 64 20 62 79 20 6f 74 68 65 72 0a 2a voked by other.*
e610: 2a 20 74 68 72 65 61 64 73 20 77 68 69 6c 65 20 * threads while
e620: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
e630: 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 20 46 75 is running. Fu
e640: 72 74 68 65 72 6d 6f 72 65 2c 20 73 71 6c 69 74 rthermore, sqlit
e650: 65 33 5f 63 6f 6e 66 69 67 28 29 0a 2a 2a 20 6d e3_config().** m
e660: 61 79 20 6f 6e 6c 79 20 62 65 20 69 6e 76 6f 6b ay only be invok
e670: 65 64 20 70 72 69 6f 72 20 74 6f 20 6c 69 62 72 ed prior to libr
e680: 61 72 79 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ary initializati
e690: 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c on using.** [sql
e6a0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
e6b0: 29 5d 20 6f 72 20 61 66 74 65 72 20 73 68 75 74 )] or after shut
e6c0: 64 6f 77 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 down by [sqlite3
e6d0: 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2e 0a 2a 2a _shutdown()]..**
e6e0: 20 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 Note, however,
e6f0: 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e that sqlite3_con
e700: 66 69 67 28 29 20 63 61 6e 20 62 65 20 63 61 6c fig() can be cal
e710: 6c 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 led as part of t
e720: 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 he.** implementa
e730: 74 69 6f 6e 20 6f 66 20 61 6e 20 61 70 70 6c 69 tion of an appli
e740: 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 5b cation-defined [
e750: 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 sqlite3_os_init(
e760: 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 )]..**.** The fi
e770: 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 rst argument to
e780: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
e790: 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a is an integer.*
e7a0: 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 * [SQLITE_CONFIG
e7b0: 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 20 7c 20 _SINGLETHREAD |
e7c0: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 configuration op
e7d0: 74 69 6f 6e 5d 20 74 68 61 74 20 64 65 74 65 72 tion] that deter
e7e0: 6d 69 6e 65 73 0a 2a 2a 20 77 68 61 74 20 70 72 mines.** what pr
e7f0: 6f 70 65 72 74 79 20 6f 66 20 53 51 4c 69 74 65 operty of SQLite
e800: 20 69 73 20 74 6f 20 62 65 20 63 6f 6e 66 69 67 is to be config
e810: 75 72 65 64 2e 20 20 53 75 62 73 65 71 75 65 6e ured. Subsequen
e820: 74 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 76 t arguments.** v
e830: 61 72 79 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e ary depending on
e840: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e the [SQLITE_CON
e850: 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 FIG_SINGLETHREAD
e860: 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e | configuration
e870: 20 6f 70 74 69 6f 6e 5d 0a 2a 2a 20 69 6e 20 74 option].** in t
e880: 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
e890: 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 t..**.** When a
e8a0: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 configuration op
e8b0: 74 69 6f 6e 20 69 73 20 73 65 74 2c 20 73 71 6c tion is set, sql
e8c0: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 72 65 ite3_config() re
e8d0: 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b turns [SQLITE_OK
e8e0: 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 ]..** If the opt
e8f0: 69 6f 6e 20 69 73 20 75 6e 6b 6e 6f 77 6e 20 6f ion is unknown o
e900: 72 20 53 51 4c 69 74 65 20 69 73 20 75 6e 61 62 r SQLite is unab
e910: 6c 65 20 74 6f 20 73 65 74 20 74 68 65 20 6f 70 le to set the op
e920: 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 74 68 69 tion.** then thi
e930: 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e s routine return
e940: 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72 s a non-zero [er
e950: 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a ror code]..**.**
e960: 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
e970: 2a 20 5b 48 31 34 31 30 33 5d 20 5b 48 31 34 31 * [H14103] [H141
e980: 30 36 5d 20 5b 48 31 34 31 32 30 5d 20 5b 48 31 06] [H14120] [H1
e990: 34 31 32 33 5d 20 5b 48 31 34 31 32 36 5d 20 5b 4123] [H14126] [
e9a0: 48 31 34 31 32 39 5d 20 5b 48 31 34 31 33 32 5d H14129] [H14132]
e9b0: 20 5b 48 31 34 31 33 35 5d 0a 2a 2a 20 5b 48 31 [H14135].** [H1
e9c0: 34 31 33 38 5d 20 5b 48 31 34 31 34 31 5d 20 5b 4138] [H14141] [
e9d0: 48 31 34 31 34 34 5d 20 5b 48 31 34 31 34 37 5d H14144] [H14147]
e9e0: 20 5b 48 31 34 31 35 30 5d 20 5b 48 31 34 31 35 [H14150] [H1415
e9f0: 33 5d 20 5b 48 31 34 31 35 36 5d 20 5b 48 31 34 3] [H14156] [H14
ea00: 31 35 39 5d 0a 2a 2a 20 5b 48 31 34 31 36 32 5d 159].** [H14162]
ea10: 20 5b 48 31 34 31 36 35 5d 20 5b 48 31 34 31 36 [H14165] [H1416
ea20: 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 8].*/.SQLITE_API
ea30: 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 SQLITE_EXPERIME
ea40: 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 NTAL int sqlite3
ea50: 5f 63 6f 6e 66 69 67 28 69 6e 74 2c 20 2e 2e 2e _config(int, ...
ea60: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
ea70: 45 46 3a 20 43 6f 6e 66 69 67 75 72 65 20 64 61 EF: Configure da
ea80: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
ea90: 6e 73 20 20 7b 48 31 34 32 30 30 7d 20 3c 53 32 ns {H14200} <S2
eaa0: 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 0000>.** EXPERIM
eab0: 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ENTAL.**.** The
eac0: 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 sqlite3_db_confi
ead0: 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 g() interface is
eae0: 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 63 6f used to make co
eaf0: 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 63 nfiguration.** c
eb00: 68 61 6e 67 65 73 20 74 6f 20 61 20 5b 64 61 74 hanges to a [dat
eb10: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
eb20: 5d 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 63 ]. The interfac
eb30: 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 0a e is similar to.
eb40: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ** [sqlite3_conf
eb50: 69 67 28 29 5d 20 65 78 63 65 70 74 20 74 68 61 ig()] except tha
eb60: 74 20 74 68 65 20 63 68 61 6e 67 65 73 20 61 70 t the changes ap
eb70: 70 6c 79 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a ply to a single.
eb80: 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ** [database con
eb90: 6e 65 63 74 69 6f 6e 5d 20 28 73 70 65 63 69 66 nection] (specif
eba0: 69 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 ied in the first
ebb0: 20 61 72 67 75 6d 65 6e 74 29 2e 20 20 54 68 65 argument). The
ebc0: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 .** sqlite3_db_c
ebd0: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 onfig() interfac
ebe0: 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 e can only be us
ebf0: 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 ed immediately a
ec00: 66 74 65 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 fter.** the data
ec10: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
ec20: 69 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 is created using
ec30: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 [sqlite3_open()
ec40: 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f ],.** [sqlite3_o
ec50: 70 65 6e 31 36 28 29 5d 2c 20 6f 72 20 5b 73 71 pen16()], or [sq
ec60: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d lite3_open_v2()]
ec70: 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 . .**.** The se
ec80: 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f cond argument to
ec90: 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 sqlite3_db_conf
eca0: 69 67 28 44 2c 56 2c 2e 2e 2e 29 20 20 69 73 20 ig(D,V,...) is
ecb0: 74 68 65 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 61 the.** configura
ecc0: 74 69 6f 6e 20 76 65 72 62 20 2d 20 61 6e 20 69 tion verb - an i
ecd0: 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68 61 74 nteger code that
ece0: 20 69 6e 64 69 63 61 74 65 73 20 77 68 61 74 0a indicates what.
ecf0: 2a 2a 20 61 73 70 65 63 74 20 6f 66 20 74 68 65 ** aspect of the
ed00: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
ed10: 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20 ction] is being
ed20: 63 6f 6e 66 69 67 75 72 65 64 2e 0a 2a 2a 20 54 configured..** T
ed30: 68 65 20 6f 6e 6c 79 20 63 68 6f 69 63 65 20 66 he only choice f
ed40: 6f 72 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 or this value is
ed50: 20 5b 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 [SQLITE_DBCONFI
ed60: 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d 2e 0a 2a 2a G_LOOKASIDE]..**
ed70: 20 4e 65 77 20 76 65 72 62 73 20 61 72 65 20 6c New verbs are l
ed80: 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 64 64 65 ikely to be adde
ed90: 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 d in future rele
eda0: 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a ases of SQLite..
edb0: 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 61 72 ** Additional ar
edc0: 67 75 6d 65 6e 74 73 20 64 65 70 65 6e 64 20 6f guments depend o
edd0: 6e 20 74 68 65 20 76 65 72 62 2e 0a 2a 2a 0a 2a n the verb..**.*
ede0: 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
edf0: 2a 2a 20 5b 48 31 34 32 30 33 5d 20 5b 48 31 34 ** [H14203] [H14
ee00: 32 30 36 5d 20 5b 48 31 34 32 30 39 5d 20 5b 48 206] [H14209] [H
ee10: 31 34 32 31 32 5d 20 5b 48 31 34 32 31 35 5d 0a 14212] [H14215].
ee20: 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 */.SQLITE_API SQ
ee30: 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 LITE_EXPERIMENTA
ee40: 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 L int sqlite3_db
ee50: 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74 65 33 2a _config(sqlite3*
ee60: 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3b 0a , int op, ...);.
ee70: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
ee80: 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 Memory Allocati
ee90: 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 31 30 on Routines {H10
eea0: 31 35 35 7d 20 3c 53 32 30 31 32 30 3e 0a 2a 2a 155} <S20120>.**
eeb0: 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
eec0: 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
eed0: 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 64 of this object d
eee0: 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 efines the inter
eef0: 66 61 63 65 20 62 65 74 77 65 65 6e 20 53 51 4c face between SQL
ef00: 69 74 65 0a 2a 2a 20 61 6e 64 20 6c 6f 77 2d 6c ite.** and low-l
ef10: 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f evel memory allo
ef20: 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e cation routines.
ef30: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 .**.** This obje
ef40: 63 74 20 69 73 20 75 73 65 64 20 69 6e 20 6f 6e ct is used in on
ef50: 6c 79 20 6f 6e 65 20 70 6c 61 63 65 20 69 6e 20 ly one place in
ef60: 74 68 65 20 53 51 4c 69 74 65 20 69 6e 74 65 72 the SQLite inter
ef70: 66 61 63 65 2e 0a 2a 2a 20 41 20 70 6f 69 6e 74 face..** A point
ef80: 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 er to an instanc
ef90: 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
efa0: 20 69 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 is the argument
efb0: 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f to.** [sqlite3_
efc0: 63 6f 6e 66 69 67 28 29 5d 20 77 68 65 6e 20 74 config()] when t
efd0: 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e he configuration
efe0: 20 6f 70 74 69 6f 6e 20 69 73 0a 2a 2a 20 5b 53 option is.** [S
eff0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c QLITE_CONFIG_MAL
f000: 4c 4f 43 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f LOC] or [SQLITE_
f010: 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f 43 CONFIG_GETMALLOC
f020: 5d 2e 20 20 0a 2a 2a 20 42 79 20 63 72 65 61 74 ]. .** By creat
f030: 69 6e 67 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 ing an instance
f040: 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 0a 2a of this object.*
f050: 2a 20 61 6e 64 20 70 61 73 73 69 6e 67 20 69 74 * and passing it
f060: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e to [sqlite3_con
f070: 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e fig]([SQLITE_CON
f080: 46 49 47 5f 4d 41 4c 4c 4f 43 5d 29 0a 2a 2a 20 FIG_MALLOC]).**
f090: 64 75 72 69 6e 67 20 63 6f 6e 66 69 67 75 72 61 during configura
f0a0: 74 69 6f 6e 2c 20 61 6e 20 61 70 70 6c 69 63 61 tion, an applica
f0b0: 74 69 6f 6e 20 63 61 6e 20 73 70 65 63 69 66 79 tion can specify
f0c0: 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 0a an alternative.
f0d0: 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ** memory alloca
f0e0: 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 66 tion subsystem f
f0f0: 6f 72 20 53 51 4c 69 74 65 20 74 6f 20 75 73 65 or SQLite to use
f100: 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 0a for all of its.
f110: 2a 2a 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 ** dynamic memor
f120: 79 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e y needs..**.** N
f130: 6f 74 65 20 74 68 61 74 20 53 51 4c 69 74 65 20 ote that SQLite
f140: 63 6f 6d 65 73 20 77 69 74 68 20 73 65 76 65 72 comes with sever
f150: 61 6c 20 5b 62 75 69 6c 74 2d 69 6e 20 6d 65 6d al [built-in mem
f160: 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73 5d 0a ory allocators].
f170: 2a 2a 20 74 68 61 74 20 61 72 65 20 70 65 72 66 ** that are perf
f180: 65 63 74 6c 79 20 61 64 65 71 75 61 74 65 20 66 ectly adequate f
f190: 6f 72 20 74 68 65 20 6f 76 65 72 77 68 65 6c 6d or the overwhelm
f1a0: 69 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20 ing majority of
f1b0: 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 applications.**
f1c0: 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 6f 62 and that this ob
f1d0: 6a 65 63 74 20 69 73 20 6f 6e 6c 79 20 75 73 65 ject is only use
f1e0: 66 75 6c 20 74 6f 20 61 20 74 69 6e 79 20 6d 69 ful to a tiny mi
f1f0: 6e 6f 72 69 74 79 20 6f 66 20 61 70 70 6c 69 63 nority of applic
f200: 61 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68 20 73 ations.** with s
f210: 70 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d 6f 72 pecialized memor
f220: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 y allocation req
f230: 75 69 72 65 6d 65 6e 74 73 2e 20 20 54 68 69 73 uirements. This
f240: 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 61 6c object is.** al
f250: 73 6f 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 so used during t
f260: 65 73 74 69 6e 67 20 6f 66 20 53 51 4c 69 74 65 esting of SQLite
f270: 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 70 65 in order to spe
f280: 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 cify an alternat
f290: 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c ive.** memory al
f2a0: 6c 6f 63 61 74 6f 72 20 74 68 61 74 20 73 69 6d locator that sim
f2b0: 75 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20 6f 75 ulates memory ou
f2c0: 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 t-of-memory cond
f2d0: 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f 72 64 itions in.** ord
f2e0: 65 72 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 er to verify tha
f2f0: 74 20 53 51 4c 69 74 65 20 72 65 63 6f 76 65 72 t SQLite recover
f300: 73 20 67 72 61 63 65 66 75 6c 6c 79 20 66 72 6f s gracefully fro
f310: 6d 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64 69 74 m such.** condit
f320: 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ions..**.** The
f330: 78 4d 61 6c 6c 6f 63 20 61 6e 64 20 78 46 72 65 xMalloc and xFre
f340: 65 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 20 77 e methods must w
f350: 6f 72 6b 20 6c 69 6b 65 20 74 68 65 0a 2a 2a 20 ork like the.**
f360: 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 65 malloc() and fre
f370: 65 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 66 72 e() functions fr
f380: 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 om the standard
f390: 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 20 54 68 C library..** Th
f3a0: 65 20 78 52 65 61 6c 6c 6f 63 20 6d 65 74 68 6f e xRealloc metho
f3b0: 64 20 6d 75 73 74 20 77 6f 72 6b 20 6c 69 6b 65 d must work like
f3c0: 20 72 65 61 6c 6c 6f 63 28 29 20 66 72 6f 6d 20 realloc() from
f3d0: 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c the standard C l
f3e0: 69 62 72 61 72 79 0a 2a 2a 20 77 69 74 68 20 74 ibrary.** with t
f3f0: 68 65 20 65 78 63 65 70 74 69 6f 6e 20 74 68 61 he exception tha
f400: 74 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 t if the second
f410: 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 52 65 61 argument to xRea
f420: 6c 6c 6f 63 20 69 73 20 7a 65 72 6f 2c 0a 2a 2a lloc is zero,.**
f430: 20 78 52 65 61 6c 6c 6f 63 20 6d 75 73 74 20 62 xRealloc must b
f440: 65 20 61 20 6e 6f 2d 6f 70 20 2d 20 69 74 20 6d e a no-op - it m
f450: 75 73 74 20 6e 6f 74 20 70 65 72 66 6f 72 6d 20 ust not perform
f460: 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f any allocation o
f470: 72 0a 2a 2a 20 64 65 61 6c 6c 6f 63 61 74 69 6f r.** deallocatio
f480: 6e 2e 20 20 53 51 4c 69 74 65 20 67 75 61 72 61 n. SQLite guara
f490: 6e 74 65 65 64 73 20 74 68 61 74 20 74 68 65 20 nteeds that the
f4a0: 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
f4b0: 74 6f 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63 20 69 to.** xRealloc i
f4c0: 73 20 61 6c 77 61 79 73 20 61 20 76 61 6c 75 65 s always a value
f4d0: 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 20 70 returned by a p
f4e0: 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 78 52 6f rior call to xRo
f4f0: 75 6e 64 75 70 2e 0a 2a 2a 20 41 6e 64 20 73 6f undup..** And so
f500: 20 69 6e 20 63 61 73 65 73 20 77 68 65 72 65 20 in cases where
f510: 78 52 6f 75 6e 64 75 70 20 61 6c 77 61 79 73 20 xRoundup always
f520: 72 65 74 75 72 6e 73 20 61 20 70 6f 73 69 74 69 returns a positi
f530: 76 65 20 6e 75 6d 62 65 72 2c 0a 2a 2a 20 78 52 ve number,.** xR
f540: 65 61 6c 6c 6f 63 20 63 61 6e 20 70 65 72 66 6f ealloc can perfo
f550: 72 6d 20 65 78 61 63 74 6c 79 20 61 73 20 74 68 rm exactly as th
f560: 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72 61 e standard libra
f570: 72 79 20 72 65 61 6c 6c 6f 63 28 29 20 61 6e 64 ry realloc() and
f580: 0a 2a 2a 20 73 74 69 6c 6c 20 62 65 20 69 6e 20 .** still be in
f590: 63 6f 6d 70 6c 69 61 6e 63 65 20 77 69 74 68 20 compliance with
f5a0: 74 68 69 73 20 73 70 65 63 69 66 69 63 61 74 69 this specificati
f5b0: 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 78 53 69 7a 65 20 on..**.** xSize
f5c0: 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 should return th
f5d0: 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 e allocated size
f5e0: 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c of a memory all
f5f0: 6f 63 61 74 69 6f 6e 0a 2a 2a 20 70 72 65 76 69 ocation.** previ
f600: 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 ously obtained f
f610: 72 6f 6d 20 78 4d 61 6c 6c 6f 63 20 6f 72 20 78 rom xMalloc or x
f620: 52 65 61 6c 6c 6f 63 2e 20 20 54 68 65 20 61 6c Realloc. The al
f630: 6c 6f 63 61 74 65 64 20 73 69 7a 65 0a 2a 2a 20 located size.**
f640: 69 73 20 61 6c 77 61 79 73 20 61 74 20 6c 65 61 is always at lea
f650: 73 74 20 61 73 20 62 69 67 20 61 73 20 74 68 65 st as big as the
f660: 20 72 65 71 75 65 73 74 65 64 20 73 69 7a 65 20 requested size
f670: 62 75 74 20 6d 61 79 20 62 65 20 6c 61 72 67 65 but may be large
f680: 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 6f r..**.** The xRo
f690: 75 6e 64 75 70 20 6d 65 74 68 6f 64 20 72 65 74 undup method ret
f6a0: 75 72 6e 73 20 77 68 61 74 20 77 6f 75 6c 64 20 urns what would
f6b0: 62 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 be the allocated
f6c0: 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 61 20 6d 65 size of.** a me
f6d0: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
f6e0: 67 69 76 65 6e 20 61 20 70 61 72 74 69 63 75 6c given a particul
f6f0: 61 72 20 72 65 71 75 65 73 74 65 64 20 73 69 7a ar requested siz
f700: 65 2e 20 20 4d 6f 73 74 20 6d 65 6d 6f 72 79 0a e. Most memory.
f710: 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 73 20 72 6f ** allocators ro
f720: 75 6e 64 20 75 70 20 6d 65 6d 6f 72 79 20 61 6c und up memory al
f730: 6c 6f 63 61 74 69 6f 6e 73 20 61 74 20 6c 65 61 locations at lea
f740: 73 74 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6d st to the next m
f750: 75 6c 74 69 70 6c 65 0a 2a 2a 20 6f 66 20 38 2e ultiple.** of 8.
f760: 20 20 53 6f 6d 65 20 61 6c 6c 6f 63 61 74 6f 72 Some allocator
f770: 73 20 72 6f 75 6e 64 20 75 70 20 74 6f 20 61 20 s round up to a
f780: 6c 61 72 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 larger multiple
f790: 6f 72 20 74 6f 20 61 20 70 6f 77 65 72 20 6f 66 or to a power of
f7a0: 20 32 2e 0a 2a 2a 20 45 76 65 72 79 20 6d 65 6d 2..** Every mem
f7b0: 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 ory allocation r
f7c0: 65 71 75 65 73 74 20 63 6f 6d 69 6e 67 20 69 6e equest coming in
f7d0: 20 74 68 72 6f 75 67 68 20 5b 73 71 6c 69 74 65 through [sqlite
f7e0: 33 5f 6d 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 6f 3_malloc()].** o
f7f0: 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c r [sqlite3_reall
f800: 6f 63 28 29 5d 20 66 69 72 73 74 20 63 61 6c 6c oc()] first call
f810: 73 20 78 52 6f 75 6e 64 75 70 2e 20 20 49 66 20 s xRoundup. If
f820: 78 52 6f 75 6e 64 75 70 20 72 65 74 75 72 6e 73 xRoundup returns
f830: 20 30 2c 20 0a 2a 2a 20 74 68 61 74 20 63 61 75 0, .** that cau
f840: 73 65 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f ses the correspo
f850: 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c nding memory all
f860: 6f 63 61 74 69 6f 6e 20 74 6f 20 66 61 69 6c 2e ocation to fail.
f870: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74 .**.** The xInit
f880: 20 6d 65 74 68 6f 64 20 69 6e 69 74 69 61 6c 69 method initiali
f890: 7a 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 zes the memory a
f8a0: 6c 6c 6f 63 61 74 6f 72 2e 20 20 28 46 6f 72 20 llocator. (For
f8b0: 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 69 74 20 6d example,.** it m
f8c0: 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 6e ight allocate an
f8d0: 79 20 72 65 71 75 69 72 65 20 6d 75 74 65 78 65 y require mutexe
f8e0: 73 20 6f 72 20 69 6e 69 74 69 61 6c 69 7a 65 20 s or initialize
f8f0: 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a 2a 2a internal data.**
f900: 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 54 68 structures. Th
f910: 65 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 e xShutdown meth
f920: 6f 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 od is invoked (i
f930: 6e 64 69 72 65 63 74 6c 79 29 20 62 79 0a 2a 2a ndirectly) by.**
f940: 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f [sqlite3_shutdo
f950: 77 6e 28 29 5d 20 61 6e 64 20 73 68 6f 75 6c 64 wn()] and should
f960: 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20 deallocate any
f970: 72 65 73 6f 75 72 63 65 73 20 61 63 71 75 69 72 resources acquir
f980: 65 64 0a 2a 2a 20 62 79 20 78 49 6e 69 74 2e 20 ed.** by xInit.
f990: 20 54 68 65 20 70 41 70 70 44 61 74 61 20 70 6f The pAppData po
f9a0: 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73 inter is used as
f9b0: 20 74 68 65 20 6f 6e 6c 79 20 70 61 72 61 6d 65 the only parame
f9c0: 74 65 72 20 74 6f 0a 2a 2a 20 78 49 6e 69 74 20 ter to.** xInit
f9d0: 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 2e 0a 2a and xShutdown..*
f9e0: 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 68 6f 6c 64 *.** SQLite hold
f9f0: 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4d 55 s the [SQLITE_MU
fa00: 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
fa10: 52 5d 20 6d 75 74 65 78 20 77 68 65 6e 20 69 74 R] mutex when it
fa20: 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 74 68 65 20 invokes.** the
fa30: 78 49 6e 69 74 20 6d 65 74 68 6f 64 2c 20 73 6f xInit method, so
fa40: 20 74 68 65 20 78 49 6e 69 74 20 6d 65 74 68 6f the xInit metho
fa50: 64 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 74 68 d need not be th
fa60: 72 65 61 64 73 61 66 65 2e 20 20 54 68 65 0a 2a readsafe. The.*
fa70: 2a 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 * xShutdown meth
fa80: 6f 64 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 od is only calle
fa90: 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f d from [sqlite3_
faa0: 73 68 75 74 64 6f 77 6e 28 29 5d 20 73 6f 20 69 shutdown()] so i
fab0: 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 t does.** not ne
fac0: 65 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 73 ed to be threads
fad0: 61 66 65 20 65 69 74 68 65 72 2e 20 20 46 6f 72 afe either. For
fae0: 20 61 6c 6c 20 6f 74 68 65 72 20 6d 65 74 68 6f all other metho
faf0: 64 73 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 68 6f ds, SQLite.** ho
fb00: 6c 64 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f lds the [SQLITE_
fb10: 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d MUTEX_STATIC_MEM
fb20: 5d 20 6d 75 74 65 78 20 61 73 20 6c 6f 6e 67 20 ] mutex as long
fb30: 61 73 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 as the.** [SQLIT
fb40: 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 E_CONFIG_MEMSTAT
fb50: 55 53 5d 20 63 6f 6e 66 69 67 75 72 61 74 69 6f US] configuratio
fb60: 6e 20 6f 70 74 69 6f 6e 20 69 73 20 74 75 72 6e n option is turn
fb70: 65 64 20 6f 6e 20 28 77 68 69 63 68 0a 2a 2a 20 ed on (which.**
fb80: 69 74 20 69 73 20 62 79 20 64 65 66 61 75 6c 74 it is by default
fb90: 29 20 61 6e 64 20 73 6f 20 74 68 65 20 6d 65 74 ) and so the met
fba0: 68 6f 64 73 20 61 72 65 20 61 75 74 6f 6d 61 74 hods are automat
fbb0: 69 63 61 6c 6c 79 20 73 65 72 69 61 6c 69 7a 65 ically serialize
fbc0: 64 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 69 d..** However, i
fbd0: 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 f [SQLITE_CONFIG
fbe0: 5f 4d 45 4d 53 54 41 54 55 53 5d 20 69 73 20 64 _MEMSTATUS] is d
fbf0: 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 isabled, then th
fc00: 65 20 6f 74 68 65 72 0a 2a 2a 20 6d 65 74 68 6f e other.** metho
fc10: 64 73 20 6d 75 73 74 20 62 65 20 74 68 72 65 61 ds must be threa
fc20: 64 73 61 66 65 20 6f 72 20 65 6c 73 65 20 6d 61 dsafe or else ma
fc30: 6b 65 20 74 68 65 69 72 20 6f 77 6e 20 61 72 72 ke their own arr
fc40: 61 6e 67 65 6d 65 6e 74 73 20 66 6f 72 0a 2a 2a angements for.**
fc50: 20 73 65 72 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a serialization..
fc60: 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c **.** SQLite wil
fc70: 6c 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 78 l never invoke x
fc80: 49 6e 69 74 28 29 20 6d 6f 72 65 20 74 68 61 6e Init() more than
fc90: 20 6f 6e 63 65 20 77 69 74 68 6f 75 74 20 61 6e once without an
fca0: 20 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a 2a 20 intervening.**
fcb0: 63 61 6c 6c 20 74 6f 20 78 53 68 75 74 64 6f 77 call to xShutdow
fcc0: 6e 28 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 n()..*/.typedef
fcd0: 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d struct sqlite3_m
fce0: 65 6d 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 em_methods sqlit
fcf0: 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 3b 0a e3_mem_methods;.
fd00: 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d struct sqlite3_m
fd10: 65 6d 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 76 em_methods {. v
fd20: 6f 69 64 20 2a 28 2a 78 4d 61 6c 6c 6f 63 29 28 oid *(*xMalloc)(
fd30: 69 6e 74 29 3b 20 20 20 20 20 20 20 20 20 2f 2a int); /*
fd40: 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 Memory allocati
fd50: 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 on function */.
fd60: 20 76 6f 69 64 20 28 2a 78 46 72 65 65 29 28 76 void (*xFree)(v
fd70: 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20 oid*);
fd80: 2f 2a 20 46 72 65 65 20 61 20 70 72 69 6f 72 20 /* Free a prior
fd90: 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 allocation */.
fda0: 76 6f 69 64 20 2a 28 2a 78 52 65 61 6c 6c 6f 63 void *(*xRealloc
fdb0: 29 28 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f )(void*,int); /
fdc0: 2a 20 52 65 73 69 7a 65 20 61 6e 20 61 6c 6c 6f * Resize an allo
fdd0: 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 cation */. int
fde0: 28 2a 78 53 69 7a 65 29 28 76 6f 69 64 2a 29 3b (*xSize)(void*);
fdf0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
fe00: 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 turn the size of
fe10: 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a an allocation *
fe20: 2f 0a 20 20 69 6e 74 20 28 2a 78 52 6f 75 6e 64 /. int (*xRound
fe30: 75 70 29 28 69 6e 74 29 3b 20 20 20 20 20 20 20 up)(int);
fe40: 20 20 20 2f 2a 20 52 6f 75 6e 64 20 75 70 20 72 /* Round up r
fe50: 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 61 equest size to a
fe60: 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 2a llocation size *
fe70: 2f 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 /. int (*xInit)
fe80: 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20 (void*);
fe90: 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 /* Initialize
fea0: 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
feb0: 63 61 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 cator */. void
fec0: 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 76 6f 69 (*xShutdown)(voi
fed0: 64 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 69 d*); /* Dei
fee0: 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 nitialize the me
fef0: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 2a mory allocator *
ff00: 2f 0a 20 20 76 6f 69 64 20 2a 70 41 70 70 44 61 /. void *pAppDa
ff10: 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ta;
ff20: 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 /* Argument t
ff30: 6f 20 78 49 6e 69 74 28 29 20 61 6e 64 20 78 53 o xInit() and xS
ff40: 68 75 74 64 6f 77 6e 28 29 20 2a 2f 0a 7d 3b 0a hutdown() */.};.
ff50: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
ff60: 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f Configuration O
ff70: 70 74 69 6f 6e 73 20 7b 48 31 30 31 36 30 7d 20 ptions {H10160}
ff80: 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 <S20000>.** EXPE
ff90: 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 RIMENTAL.**.** T
ffa0: 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 hese constants a
ffb0: 72 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 re the available
ffc0: 20 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 67 75 integer configu
ffd0: 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 ration options t
ffe0: 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61 hat.** can be pa
fff0: 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 ssed as the firs
10000 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 t argument to th
10010 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 e [sqlite3_confi
10020 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a g()] interface..
10030 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e 66 69 67 **.** New config
10040 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 uration options
10050 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e 20 may be added in
10060 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 future releases
10070 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 45 78 of SQLite..** Ex
10080 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 75 72 61 isting configura
10090 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 69 67 tion options mig
100a0 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 6e 75 ht be discontinu
100b0 65 64 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e ed. Application
100c0 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 68 65 63 s.** should chec
100d0 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 k the return cod
100e0 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f e from [sqlite3_
100f0 63 6f 6e 66 69 67 28 29 5d 20 74 6f 20 6d 61 6b config()] to mak
10100 65 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 74 e sure that.** t
10110 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 20 he call worked.
10120 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f The [sqlite3_co
10130 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 nfig()] interfac
10140 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 0a e will return a.
10150 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 ** non-zero [err
10160 6f 72 20 63 6f 64 65 5d 20 69 66 20 61 20 64 69 or code] if a di
10170 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20 75 6e scontinued or un
10180 73 75 70 70 6f 72 74 65 64 20 63 6f 6e 66 69 67 supported config
10190 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 0a 2a uration option.*
101a0 2a 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a * is invoked..**
101b0 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e .** <dl>.** <dt>
101c0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 SQLITE_CONFIG_SI
101d0 4e 47 4c 45 54 48 52 45 41 44 3c 2f 64 74 3e 0a NGLETHREAD</dt>.
101e0 2a 2a 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65 ** <dd>There are
101f0 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f no arguments to
10200 20 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 this option. T
10210 68 69 73 20 6f 70 74 69 6f 6e 20 64 69 73 61 62 his option disab
10220 6c 65 73 0a 2a 2a 20 61 6c 6c 20 6d 75 74 65 78 les.** all mutex
10230 69 6e 67 20 61 6e 64 20 70 75 74 73 20 53 51 4c ing and puts SQL
10240 69 74 65 20 69 6e 74 6f 20 61 20 6d 6f 64 65 20 ite into a mode
10250 77 68 65 72 65 20 69 74 20 63 61 6e 20 6f 6e 6c where it can onl
10260 79 20 62 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 y be used.** by
10270 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 2e a single thread.
10280 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
10290 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 SQLITE_CONFIG_MU
102a0 4c 54 49 54 48 52 45 41 44 3c 2f 64 74 3e 0a 2a LTITHREAD</dt>.*
102b0 2a 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20 * <dd>There are
102c0 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 no arguments to
102d0 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 this option. Th
102e0 69 73 20 6f 70 74 69 6f 6e 20 64 69 73 61 62 6c is option disabl
102f0 65 73 0a 2a 2a 20 6d 75 74 65 78 69 6e 67 20 6f es.** mutexing o
10300 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e n [database conn
10310 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b 70 72 65 ection] and [pre
10320 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
10330 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 54 68 65 objects..** The
10340 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 20 application is
10350 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
10360 73 65 72 69 61 6c 69 7a 69 6e 67 20 61 63 63 65 serializing acce
10370 73 73 20 74 6f 0a 2a 2a 20 5b 64 61 74 61 62 61 ss to.** [databa
10380 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 se connections]
10390 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 and [prepared st
103a0 61 74 65 6d 65 6e 74 73 5d 2e 20 20 42 75 74 20 atements]. But
103b0 6f 74 68 65 72 20 6d 75 74 65 78 65 73 0a 2a 2a other mutexes.**
103c0 20 61 72 65 20 65 6e 61 62 6c 65 64 20 73 6f 20 are enabled so
103d0 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c that SQLite will
103e0 20 62 65 20 73 61 66 65 20 74 6f 20 75 73 65 20 be safe to use
103f0 69 6e 20 61 20 6d 75 6c 74 69 2d 74 68 72 65 61 in a multi-threa
10400 64 65 64 0a 2a 2a 20 65 6e 76 69 72 6f 6e 6d 65 ded.** environme
10410 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 6f nt as long as no
10420 20 74 77 6f 20 74 68 72 65 61 64 73 20 61 74 74 two threads att
10430 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 empt to use the
10440 73 61 6d 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 same.** [databas
10450 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 74 e connection] at
10460 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 the same time.
10470 20 53 65 65 20 74 68 65 20 5b 74 68 72 65 61 64 See the [thread
10480 69 6e 67 20 6d 6f 64 65 5d 0a 2a 2a 20 64 6f 63 ing mode].** doc
10490 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 umentation for a
104a0 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
104b0 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a ation.</dd>.**.*
104c0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e * <dt>SQLITE_CON
104d0 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 3c 2f FIG_SERIALIZED</
104e0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65 dt>.** <dd>There
104f0 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74 are no argument
10500 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e s to this option
10510 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 65 . This option e
10520 6e 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c 20 6d 75 nables.** all mu
10530 74 65 78 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 texes including
10540 74 68 65 20 72 65 63 75 72 73 69 76 65 0a 2a 2a the recursive.**
10550 20 6d 75 74 65 78 65 73 20 6f 6e 20 5b 64 61 74 mutexes on [dat
10560 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
10570 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 ] and [prepared
10580 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a 65 63 statement] objec
10590 74 73 2e 0a 2a 2a 20 49 6e 20 74 68 69 73 20 6d ts..** In this m
105a0 6f 64 65 20 28 77 68 69 63 68 20 69 73 20 74 68 ode (which is th
105b0 65 20 64 65 66 61 75 6c 74 20 77 68 65 6e 20 53 e default when S
105c0 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 QLite is compile
105d0 64 20 77 69 74 68 0a 2a 2a 20 5b 53 51 4c 49 54 d with.** [SQLIT
105e0 45 5f 54 48 52 45 41 44 53 41 46 45 3d 31 5d 29 E_THREADSAFE=1])
105f0 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 the SQLite libr
10600 61 72 79 20 77 69 6c 6c 20 69 74 73 65 6c 66 20 ary will itself
10610 73 65 72 69 61 6c 69 7a 65 20 61 63 63 65 73 73 serialize access
10620 0a 2a 2a 20 74 6f 20 5b 64 61 74 61 62 61 73 65 .** to [database
10630 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 61 6e connections] an
10640 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 d [prepared stat
10650 65 6d 65 6e 74 73 5d 20 73 6f 20 74 68 61 74 20 ements] so that
10660 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 the.** applicati
10670 6f 6e 20 69 73 20 66 72 65 65 20 74 6f 20 75 73 on is free to us
10680 65 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 e the same [data
10690 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
106a0 20 6f 72 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 or the.** same
106b0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
106c0 65 6e 74 5d 20 69 6e 20 64 69 66 66 65 72 65 6e ent] in differen
106d0 74 20 74 68 72 65 61 64 73 20 61 74 20 74 68 65 t threads at the
106e0 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 20 53 same time..** S
106f0 65 65 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e ee the [threadin
10700 67 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 6e 74 g mode] document
10710 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 ation for additi
10720 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
10730 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
10740 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d >SQLITE_CONFIG_M
10750 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 ALLOC</dt>.** <d
10760 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 d>This option ta
10770 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 kes a single arg
10780 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 ument which is a
10790 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a pointer to an.*
107a0 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 * instance of th
107b0 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d e [sqlite3_mem_m
107c0 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72 ethods] structur
107d0 65 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 e. The argument
107e0 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 61 6c specifies.** al
107f0 74 65 72 6e 61 74 69 76 65 20 6c 6f 77 2d 6c 65 ternative low-le
10800 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 vel memory alloc
10810 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 74 ation routines t
10820 6f 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61 o be used in pla
10830 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6d 65 6d ce of.** the mem
10840 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 ory allocation r
10850 6f 75 74 69 6e 65 73 20 62 75 69 6c 74 20 69 6e outines built in
10860 74 6f 20 53 51 4c 69 74 65 2e 3c 2f 64 64 3e 0a to SQLite.</dd>.
10870 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
10880 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f _CONFIG_GETMALLO
10890 43 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 C</dt>.** <dd>Th
108a0 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 is option takes
108b0 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e a single argumen
108c0 74 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 t which is a poi
108d0 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e nter to an.** in
108e0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 stance of the [s
108f0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
10900 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 ds] structure.
10910 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d The [sqlite3_mem
10920 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 73 74 72 _methods].** str
10930 75 63 74 75 72 65 20 69 73 20 66 69 6c 6c 65 64 ucture is filled
10940 20 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e with the curren
10950 74 6c 79 20 64 65 66 69 6e 65 64 20 6d 65 6d 6f tly defined memo
10960 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f ry allocation ro
10970 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 68 69 73 20 utines..** This
10980 6f 70 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 option can be us
10990 65 64 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74 ed to overload t
109a0 68 65 20 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72 he default memor
109b0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 y allocation.**
109c0 72 6f 75 74 69 6e 65 73 20 77 69 74 68 20 61 20 routines with a
109d0 77 72 61 70 70 65 72 20 74 68 61 74 20 73 69 6d wrapper that sim
109e0 75 6c 61 74 69 6f 6e 73 20 6d 65 6d 6f 72 79 20 ulations memory
109f0 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 75 allocation failu
10a00 72 65 20 6f 72 0a 2a 2a 20 74 72 61 63 6b 73 20 re or.** tracks
10a10 6d 65 6d 6f 72 79 20 75 73 61 67 65 2c 20 66 6f memory usage, fo
10a20 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64 64 3e 0a r example.</dd>.
10a30 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
10a40 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 _CONFIG_MEMSTATU
10a50 53 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 S</dt>.** <dd>Th
10a60 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 is option takes
10a70 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 single argument
10a80 6f 66 20 74 79 70 65 20 69 6e 74 2c 20 69 6e 74 of type int, int
10a90 65 72 70 72 65 74 65 64 20 61 73 20 61 20 0a 2a erpreted as a .*
10aa0 2a 20 62 6f 6f 6c 65 61 6e 2c 20 77 68 69 63 68 * boolean, which
10ab0 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69 73 61 enables or disa
10ac0 62 6c 65 73 20 74 68 65 20 63 6f 6c 6c 65 63 74 bles the collect
10ad0 69 6f 6e 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c ion of memory al
10ae0 6c 6f 63 61 74 69 6f 6e 20 0a 2a 2a 20 73 74 61 location .** sta
10af0 74 69 73 74 69 63 73 2e 20 57 68 65 6e 20 64 69 tistics. When di
10b00 73 61 62 6c 65 64 2c 20 74 68 65 20 66 6f 6c 6c sabled, the foll
10b10 6f 77 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 74 owing SQLite int
10b20 65 72 66 61 63 65 73 20 62 65 63 6f 6d 65 20 0a erfaces become .
10b30 2a 2a 20 6e 6f 6e 2d 6f 70 65 72 61 74 69 6f 6e ** non-operation
10b40 61 6c 3a 0a 2a 2a 20 20 20 3c 75 6c 3e 0a 2a 2a al:.** <ul>.**
10b50 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 <li> [sqlite3
10b60 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 0a _memory_used()].
10b70 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 ** <li> [sqlit
10b80 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 e3_memory_highwa
10b90 74 65 72 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e ter()].** <li>
10ba0 20 5b 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 [sqlite3_soft_h
10bb0 65 61 70 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 eap_limit()].**
10bc0 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f <li> [sqlite3_
10bd0 73 74 61 74 75 73 28 29 5d 0a 2a 2a 20 20 20 3c status()].** <
10be0 2f 75 6c 3e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a /ul>.** </dd>.**
10bf0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
10c00 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 3c 2f 64 ONFIG_SCRATCH</d
10c10 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f t>.** <dd>This o
10c20 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65 73 20 ption specifies
10c30 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 a static memory
10c40 62 75 66 66 65 72 20 74 68 61 74 20 53 51 4c 69 buffer that SQLi
10c50 74 65 20 63 61 6e 20 75 73 65 20 66 6f 72 0a 2a te can use for.*
10c60 2a 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 * scratch memory
10c70 2e 20 20 54 68 65 72 65 20 61 72 65 20 74 68 72 . There are thr
10c80 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 20 20 41 ee arguments: A
10c90 20 70 6f 69 6e 74 65 72 20 61 6e 20 38 2d 62 79 pointer an 8-by
10ca0 74 65 0a 2a 2a 20 61 6c 69 67 6e 65 64 20 6d 65 te.** aligned me
10cb0 6d 6f 72 79 20 62 75 66 66 65 72 20 66 72 6f 6d mory buffer from
10cc0 20 77 68 69 63 68 20 74 68 65 20 73 63 72 61 63 which the scrac
10cd0 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 77 69 h allocations wi
10ce0 6c 6c 20 62 65 0a 2a 2a 20 64 72 61 77 6e 2c 20 ll be.** drawn,
10cf0 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 63 68 the size of each
10d00 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 scratch allocat
10d10 69 6f 6e 20 28 73 7a 29 2c 0a 2a 2a 20 61 6e 64 ion (sz),.** and
10d20 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d the maximum num
10d30 62 65 72 20 6f 66 20 73 63 72 61 74 63 68 20 61 ber of scratch a
10d40 6c 6c 6f 63 61 74 69 6f 6e 73 20 28 4e 29 2e 20 llocations (N).
10d50 20 54 68 65 20 73 7a 0a 2a 2a 20 61 72 67 75 6d The sz.** argum
10d60 65 6e 74 20 6d 75 73 74 20 62 65 20 61 20 6d 75 ent must be a mu
10d70 6c 74 69 70 6c 65 20 6f 66 20 31 36 2e 20 54 68 ltiple of 16. Th
10d80 65 20 73 7a 20 70 61 72 61 6d 65 74 65 72 20 73 e sz parameter s
10d90 68 6f 75 6c 64 20 62 65 20 61 20 66 65 77 20 62 hould be a few b
10da0 79 74 65 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74 ytes.** larger t
10db0 68 61 6e 20 74 68 65 20 61 63 74 75 61 6c 20 73 han the actual s
10dc0 63 72 61 74 63 68 20 73 70 61 63 65 20 72 65 71 cratch space req
10dd0 75 69 72 65 64 20 64 75 65 20 74 6f 20 69 6e 74 uired due to int
10de0 65 72 6e 61 6c 20 6f 76 65 72 68 65 61 64 2e 0a ernal overhead..
10df0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 ** The first arg
10e00 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 ument should poi
10e10 6e 74 65 72 20 74 6f 20 61 6e 20 38 2d 62 79 74 nter to an 8-byt
10e20 65 20 61 6c 69 67 6e 65 64 20 62 75 66 66 65 72 e aligned buffer
10e30 0a 2a 2a 20 6f 66 20 61 74 20 6c 65 61 73 74 20 .** of at least
10e40 73 7a 2a 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 sz*N bytes of me
10e50 6d 6f 72 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 mory..** SQLite
10e60 77 69 6c 6c 20 75 73 65 20 6e 6f 20 6d 6f 72 65 will use no more
10e70 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 than one scratc
10e80 68 20 62 75 66 66 65 72 20 61 74 20 6f 6e 63 65 h buffer at once
10e90 20 70 65 72 20 74 68 72 65 61 64 2c 20 73 6f 0a per thread, so.
10ea0 2a 2a 20 4e 20 73 68 6f 75 6c 64 20 62 65 20 73 ** N should be s
10eb0 65 74 20 74 6f 20 74 68 65 20 65 78 70 65 63 74 et to the expect
10ec0 65 64 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 ed maximum numbe
10ed0 72 20 6f 66 20 74 68 72 65 61 64 73 2e 20 20 54 r of threads. T
10ee0 68 65 20 73 7a 0a 2a 2a 20 70 61 72 61 6d 65 74 he sz.** paramet
10ef0 65 72 20 73 68 6f 75 6c 64 20 62 65 20 36 20 74 er should be 6 t
10f00 69 6d 65 73 20 74 68 65 20 73 69 7a 65 20 6f 66 imes the size of
10f10 20 74 68 65 20 6c 61 72 67 65 73 74 20 64 61 74 the largest dat
10f20 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 2e abase page size.
10f30 0a 2a 2a 20 53 63 72 61 74 63 68 20 62 75 66 66 .** Scratch buff
10f40 65 72 73 20 61 72 65 20 75 73 65 64 20 61 73 20 ers are used as
10f50 70 61 72 74 20 6f 66 20 74 68 65 20 62 74 72 65 part of the btre
10f60 65 20 62 61 6c 61 6e 63 65 20 6f 70 65 72 61 74 e balance operat
10f70 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 54 68 65 20 ion. If.** The
10f80 62 74 72 65 65 20 62 61 6c 61 6e 63 65 72 20 6e btree balancer n
10f90 65 65 64 73 20 61 64 64 69 74 69 6f 6e 61 6c 20 eeds additional
10fa0 6d 65 6d 6f 72 79 20 62 65 79 6f 6e 64 20 77 68 memory beyond wh
10fb0 61 74 20 69 73 20 70 72 6f 76 69 64 65 64 20 62 at is provided b
10fc0 79 0a 2a 2a 20 73 63 72 61 74 63 68 20 62 75 66 y.** scratch buf
10fd0 66 65 72 73 20 6f 72 20 69 66 20 6e 6f 20 73 63 fers or if no sc
10fe0 72 61 74 63 68 20 62 75 66 66 65 72 20 73 70 61 ratch buffer spa
10ff0 63 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2c ce is specified,
11000 20 74 68 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 then SQLite.**
11010 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 goes to [sqlite3
11020 5f 6d 61 6c 6c 6f 63 28 29 5d 20 74 6f 20 6f 62 _malloc()] to ob
11030 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 tain the memory
11040 69 74 20 6e 65 65 64 73 2e 3c 2f 64 64 3e 0a 2a it needs.</dd>.*
11050 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
11060 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
11070 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
11080 73 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69 s option specifi
11090 65 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f es a static memo
110a0 72 79 20 62 75 66 66 65 72 20 74 68 61 74 20 53 ry buffer that S
110b0 51 4c 69 74 65 20 63 61 6e 20 75 73 65 20 66 6f QLite can use fo
110c0 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 r.** the databas
110d0 65 20 70 61 67 65 20 63 61 63 68 65 20 77 69 74 e page cache wit
110e0 68 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 h the default pa
110f0 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 ge cache impleme
11100 6e 61 74 69 6f 6e 2e 20 20 0a 2a 2a 20 54 68 69 nation. .** Thi
11110 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 s configuration
11120 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 should not be us
11130 65 64 20 69 66 20 61 6e 20 61 70 70 6c 69 63 61 ed if an applica
11140 74 69 6f 6e 2d 64 65 66 69 6e 65 20 70 61 67 65 tion-define page
11150 0a 2a 2a 20 63 61 63 68 65 20 69 6d 70 6c 65 6d .** cache implem
11160 65 6e 74 61 74 69 6f 6e 20 69 73 20 6c 6f 61 64 entation is load
11170 65 64 20 75 73 69 6e 67 20 74 68 65 20 53 51 4c ed using the SQL
11180 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 ITE_CONFIG_PCACH
11190 45 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 E option..** The
111a0 72 65 20 61 72 65 20 74 68 72 65 65 20 61 72 67 re are three arg
111b0 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f uments to this o
111c0 70 74 69 6f 6e 3a 20 41 20 70 6f 69 6e 74 65 72 ption: A pointer
111d0 20 74 6f 20 38 2d 62 79 74 65 20 61 6c 69 67 6e to 8-byte align
111e0 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 74 68 ed.** memory, th
111f0 65 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 70 e size of each p
11200 61 67 65 20 62 75 66 66 65 72 20 28 73 7a 29 2c age buffer (sz),
11210 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 and the number
11220 6f 66 20 70 61 67 65 73 20 28 4e 29 2e 0a 2a 2a of pages (N)..**
11230 20 54 68 65 20 73 7a 20 61 72 67 75 6d 65 6e 74 The sz argument
11240 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20 73 should be the s
11250 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 ize of the large
11260 73 74 20 64 61 74 61 62 61 73 65 20 70 61 67 65 st database page
11270 0a 2a 2a 20 28 61 20 70 6f 77 65 72 20 6f 66 20 .** (a power of
11280 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31 32 20 two between 512
11290 61 6e 64 20 33 32 37 36 38 29 20 70 6c 75 73 20 and 32768) plus
112a0 61 20 6c 69 74 74 6c 65 20 65 78 74 72 61 20 66 a little extra f
112b0 6f 72 20 65 61 63 68 0a 2a 2a 20 70 61 67 65 20 or each.** page
112c0 68 65 61 64 65 72 2e 20 20 54 68 65 20 70 61 67 header. The pag
112d0 65 20 68 65 61 64 65 72 20 73 69 7a 65 20 69 73 e header size is
112e0 20 32 30 20 74 6f 20 34 30 20 62 79 74 65 73 20 20 to 40 bytes
112f0 64 65 70 65 6e 64 69 6e 67 20 6f 6e 0a 2a 2a 20 depending on.**
11300 74 68 65 20 68 6f 73 74 20 61 72 63 68 69 74 65 the host archite
11310 63 74 75 72 65 2e 20 20 49 74 20 69 73 20 68 61 cture. It is ha
11320 72 6d 6c 65 73 73 2c 20 61 70 61 72 74 20 66 72 rmless, apart fr
11330 6f 6d 20 74 68 65 20 77 61 73 74 65 64 20 6d 65 om the wasted me
11340 6d 6f 72 79 2c 0a 2a 2a 20 74 6f 20 6d 61 6b 65 mory,.** to make
11350 20 73 7a 20 61 20 6c 69 74 74 6c 65 20 74 6f 6f sz a little too
11360 20 6c 61 72 67 65 2e 20 20 54 68 65 20 66 69 72 large. The fir
11370 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 73 st.** argument s
11380 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f 20 61 hould point to a
11390 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 n allocation of
113a0 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 62 79 at least sz*N by
113b0 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a tes of memory..*
113c0 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 * SQLite will us
113d0 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 70 72 6f e the memory pro
113e0 76 69 64 65 64 20 62 79 20 74 68 65 20 66 69 72 vided by the fir
113f0 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 st argument to s
11400 61 74 69 73 66 79 20 69 74 73 0a 2a 2a 20 6d 65 atisfy its.** me
11410 6d 6f 72 79 20 6e 65 65 64 73 20 66 6f 72 20 74 mory needs for t
11420 68 65 20 66 69 72 73 74 20 4e 20 70 61 67 65 73 he first N pages
11430 20 74 68 61 74 20 69 74 20 61 64 64 73 20 74 6f that it adds to
11440 20 63 61 63 68 65 2e 20 20 49 66 20 61 64 64 69 cache. If addi
11450 74 69 6f 6e 61 6c 0a 2a 2a 20 70 61 67 65 20 63 tional.** page c
11460 61 63 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 6e ache memory is n
11470 65 65 64 65 64 20 62 65 79 6f 6e 64 20 77 68 61 eeded beyond wha
11480 74 20 69 73 20 70 72 6f 76 69 64 65 64 20 62 79 t is provided by
11490 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 74 68 this option, th
114a0 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 67 6f 65 en.** SQLite goe
114b0 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 s to [sqlite3_ma
114c0 6c 6c 6f 63 28 29 5d 20 66 6f 72 20 74 68 65 20 lloc()] for the
114d0 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 6f 72 61 additional stora
114e0 67 65 20 73 70 61 63 65 2e 0a 2a 2a 20 54 68 65 ge space..** The
114f0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
11500 6d 69 67 68 74 20 75 73 65 20 6f 6e 65 20 6f 72 might use one or
11510 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 4e 20 62 more of the N b
11520 75 66 66 65 72 73 20 74 6f 20 68 6f 6c 64 20 0a uffers to hold .
11530 2a 2a 20 6d 65 6d 6f 72 79 20 61 63 63 6f 75 6e ** memory accoun
11540 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ting information
11550 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 6e . The pointer in
11560 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
11570 65 6e 74 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 ent must.** be a
11580 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 ligned to an 8-b
11590 79 74 65 20 62 6f 75 6e 64 61 72 79 20 6f 72 20 yte boundary or
115a0 73 75 62 73 65 71 75 65 6e 74 20 62 65 68 61 76 subsequent behav
115b0 69 6f 72 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a ior of SQLite.**
115c0 20 77 69 6c 6c 20 62 65 20 75 6e 64 65 66 69 6e will be undefin
115d0 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c ed.</dd>.**.** <
115e0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 dt>SQLITE_CONFIG
115f0 5f 48 45 41 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 _HEAP</dt>.** <d
11600 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 70 d>This option sp
11610 65 63 69 66 69 65 73 20 61 20 73 74 61 74 69 63 ecifies a static
11620 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 memory buffer t
11630 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 hat SQLite will
11640 75 73 65 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 6f use.** for all o
11650 66 20 69 74 73 20 64 79 6e 61 6d 69 63 20 6d 65 f its dynamic me
11660 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
11670 6e 65 65 64 73 20 62 65 79 6f 6e 64 20 74 68 6f needs beyond tho
11680 73 65 20 70 72 6f 76 69 64 65 64 0a 2a 2a 20 66 se provided.** f
11690 6f 72 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f or by [SQLITE_CO
116a0 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20 61 6e NFIG_SCRATCH] an
116b0 64 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 d [SQLITE_CONFIG
116c0 5f 50 41 47 45 43 41 43 48 45 5d 2e 0a 2a 2a 20 _PAGECACHE]..**
116d0 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20 There are three
116e0 61 72 67 75 6d 65 6e 74 73 3a 20 41 6e 20 38 2d arguments: An 8-
116f0 62 79 74 65 20 61 6c 69 67 6e 65 64 20 70 6f 69 byte aligned poi
11700 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 6d 6f nter to the memo
11710 72 79 2c 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 ry,.** the numbe
11720 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
11730 65 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 2c e memory buffer,
11740 20 61 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d and the minimum
11750 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 allocation size
11760 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 72 73 ..** If the firs
11770 74 20 70 6f 69 6e 74 65 72 20 28 74 68 65 20 6d t pointer (the m
11780 65 6d 6f 72 79 20 70 6f 69 6e 74 65 72 29 20 69 emory pointer) i
11790 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 51 4c s NULL, then SQL
117a0 69 74 65 20 72 65 76 65 72 74 73 0a 2a 2a 20 74 ite reverts.** t
117b0 6f 20 75 73 69 6e 67 20 69 74 73 20 64 65 66 61 o using its defa
117c0 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ult memory alloc
117d0 61 74 6f 72 20 28 74 68 65 20 73 79 73 74 65 6d ator (the system
117e0 20 6d 61 6c 6c 6f 63 28 29 20 69 6d 70 6c 65 6d malloc() implem
117f0 65 6e 74 61 74 69 6f 6e 29 2c 0a 2a 2a 20 75 6e entation),.** un
11800 64 6f 69 6e 67 20 61 6e 79 20 70 72 69 6f 72 20 doing any prior
11810 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 53 invocation of [S
11820 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c QLITE_CONFIG_MAL
11830 4c 4f 43 5d 2e 20 20 49 66 20 74 68 65 0a 2a 2a LOC]. If the.**
11840 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 72 20 memory pointer
11850 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20 is not NULL and
11860 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45 5f 45 either [SQLITE_E
11870 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 5d 20 6f NABLE_MEMSYS3] o
11880 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 r.** [SQLITE_ENA
11890 42 4c 45 5f 4d 45 4d 53 59 53 35 5d 20 61 72 65 BLE_MEMSYS5] are
118a0 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 defined, then t
118b0 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6d he alternative m
118c0 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 emory.** allocat
118d0 6f 72 20 69 73 20 65 6e 67 61 67 65 64 20 74 6f or is engaged to
118e0 20 68 61 6e 64 6c 65 20 61 6c 6c 20 6f 66 20 53 handle all of S
118f0 51 4c 69 74 65 73 20 6d 65 6d 6f 72 79 20 61 6c QLites memory al
11900 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 0a location needs..
11910 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 6f 69 ** The first poi
11920 6e 74 65 72 20 28 74 68 65 20 6d 65 6d 6f 72 79 nter (the memory
11930 20 70 6f 69 6e 74 65 72 29 20 6d 75 73 74 20 62 pointer) must b
11940 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 e aligned to an
11950 38 2d 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 8-byte.** bounda
11960 72 79 20 6f 72 20 73 75 62 73 65 71 75 65 6e 74 ry or subsequent
11970 20 62 65 68 61 76 69 6f 72 20 6f 66 20 53 51 4c behavior of SQL
11980 69 74 65 20 77 69 6c 6c 20 62 65 20 75 6e 64 65 ite will be unde
11990 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a fined.</dd>.**.*
119a0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e * <dt>SQLITE_CON
119b0 46 49 47 5f 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a FIG_MUTEX</dt>.*
119c0 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f * <dd>This optio
119d0 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 n takes a single
119e0 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 argument which
119f0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
11a00 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f an.** instance o
11a10 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d f the [sqlite3_m
11a20 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74 utex_methods] st
11a30 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 61 72 ructure. The ar
11a40 67 75 6d 65 6e 74 20 73 70 65 63 69 66 69 65 73 gument specifies
11a50 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20 .** alternative
11a60 6c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 low-level mutex
11a70 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75 routines to be u
11a80 73 65 64 20 69 6e 20 70 6c 61 63 65 0a 2a 2a 20 sed in place.**
11a90 74 68 65 20 6d 75 74 65 78 20 72 6f 75 74 69 6e the mutex routin
11aa0 65 73 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 es built into SQ
11ab0 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a Lite.</dd>.**.**
11ac0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 <dt>SQLITE_CONF
11ad0 49 47 5f 47 45 54 4d 55 54 45 58 3c 2f 64 74 3e IG_GETMUTEX</dt>
11ae0 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 .** <dd>This opt
11af0 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 ion takes a sing
11b00 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 le argument whic
11b10 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 h is a pointer t
11b20 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 o an.** instance
11b30 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
11b40 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 _mutex_methods]
11b50 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 0a structure. The.
11b60 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 ** [sqlite3_mute
11b70 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 73 74 x_methods].** st
11b80 72 75 63 74 75 72 65 20 69 73 20 66 69 6c 6c 65 ructure is fille
11b90 64 20 77 69 74 68 20 74 68 65 20 63 75 72 72 65 d with the curre
11ba0 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 6d 75 74 ntly defined mut
11bb0 65 78 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 ex routines..**
11bc0 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 61 6e 20 This option can
11bd0 62 65 20 75 73 65 64 20 74 6f 20 6f 76 65 72 6c be used to overl
11be0 6f 61 64 20 74 68 65 20 64 65 66 61 75 6c 74 20 oad the default
11bf0 6d 75 74 65 78 20 61 6c 6c 6f 63 61 74 69 6f 6e mutex allocation
11c00 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 69 74 .** routines wit
11c10 68 20 61 20 77 72 61 70 70 65 72 20 75 73 65 64 h a wrapper used
11c20 20 74 6f 20 74 72 61 63 6b 20 6d 75 74 65 78 20 to track mutex
11c30 75 73 61 67 65 20 66 6f 72 20 70 65 72 66 6f 72 usage for perfor
11c40 6d 61 6e 63 65 0a 2a 2a 20 70 72 6f 66 69 6c 69 mance.** profili
11c50 6e 67 20 6f 72 20 74 65 73 74 69 6e 67 2c 20 66 ng or testing, f
11c60 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64 64 3e or example.</dd>
11c70 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
11c80 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 E_CONFIG_LOOKASI
11c90 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 DE</dt>.** <dd>T
11ca0 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 his option takes
11cb0 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 two arguments t
11cc0 68 61 74 20 64 65 74 65 72 6d 69 6e 65 20 74 68 hat determine th
11cd0 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 6d 65 6d e default.** mem
11ce0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c ory allocation l
11cf0 6f 6f 6b 61 73 69 64 65 20 6f 70 74 69 6d 69 7a ookaside optimiz
11d00 61 74 69 6f 6e 2e 20 20 54 68 65 20 66 69 72 73 ation. The firs
11d10 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 t argument is th
11d20 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61 63 e.** size of eac
11d30 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 h lookaside buff
11d40 65 72 20 73 6c 6f 74 20 61 6e 64 20 74 68 65 20 er slot and the
11d50 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20 6e 75 second is the nu
11d60 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f 74 73 mber of.** slots
11d70 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 65 61 allocated to ea
11d80 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ch database conn
11d90 65 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 ection. This op
11da0 74 69 6f 6e 20 73 65 74 73 20 74 68 65 0a 2a 2a tion sets the.**
11db0 20 3c 69 3e 64 65 66 61 75 6c 74 3c 2f 69 3e 20 <i>default</i>
11dc0 6c 6f 6f 6b 61 73 69 64 65 20 73 69 7a 65 2e 20 lookaside size.
11dd0 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 44 42 43 The [SQLITE_DBC
11de0 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d ONFIG_LOOKASIDE]
11df0 0a 2a 2a 20 76 65 72 62 20 74 6f 20 5b 73 71 6c .** verb to [sql
11e00 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 ite3_db_config()
11e10 5d 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f ] can be used to
11e20 20 63 68 61 6e 67 65 20 74 68 65 20 6c 6f 6f 6b change the look
11e30 61 73 69 64 65 0a 2a 2a 20 63 6f 6e 66 69 67 75 aside.** configu
11e40 72 61 74 69 6f 6e 20 6f 6e 20 69 6e 64 69 76 69 ration on indivi
11e50 64 75 61 6c 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 dual connections
11e60 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
11e70 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 >SQLITE_CONFIG_P
11e80 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 CACHE</dt>.** <d
11e90 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 d>This option ta
11ea0 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 kes a single arg
11eb0 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 ument which is a
11ec0 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 pointer to.** a
11ed0 6e 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 n [sqlite3_pcach
11ee0 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 e_methods] objec
11ef0 74 2e 20 20 54 68 69 73 20 6f 62 6a 65 63 74 20 t. This object
11f00 73 70 65 63 69 66 69 65 73 20 74 68 65 20 69 6e specifies the in
11f10 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 61 20 terface.** to a
11f20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63 68 custom page cach
11f30 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
11f40 2e 20 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 . SQLite makes
11f50 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a a copy of the.**
11f60 20 6f 62 6a 65 63 74 20 61 6e 64 20 75 73 65 73 object and uses
11f70 20 69 74 20 66 6f 72 20 70 61 67 65 20 63 61 63 it for page cac
11f80 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
11f90 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a tions.</dd>.**.*
11fa0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e * <dt>SQLITE_CON
11fb0 46 49 47 5f 47 45 54 50 43 41 43 48 45 3c 2f 64 FIG_GETPCACHE</d
11fc0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f t>.** <dd>This o
11fd0 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 ption takes a si
11fe0 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 ngle argument wh
11ff0 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ich is a pointer
12000 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 to an.** [sqlit
12010 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 e3_pcache_method
12020 73 5d 20 6f 62 6a 65 63 74 2e 20 20 53 51 4c 69 s] object. SQLi
12030 74 65 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 te copies of the
12040 20 63 75 72 72 65 6e 74 0a 2a 2a 20 70 61 67 65 current.** page
12050 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 cache implement
12060 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 61 74 20 ation into that
12070 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a object.</dd>.**.
12080 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 ** </dl>.*/.#def
12090 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ine SQLITE_CONFI
120a0 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 20 20 G_SINGLETHREAD
120b0 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64 65 1 /* nil */.#de
120c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 fine SQLITE_CONF
120d0 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 20 20 IG_MULTITHREAD
120e0 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64 2 /* nil */.#d
120f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
12100 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 20 20 FIG_SERIALIZED
12110 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 3 /* nil */.#
12120 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
12130 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 NFIG_MALLOC
12140 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 65 33 4 /* sqlite3
12150 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f _mem_methods* */
12160 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
12170 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f 43 CONFIG_GETMALLOC
12180 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c 69 74 5 /* sqlit
12190 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20 e3_mem_methods*
121a0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
121b0 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 E_CONFIG_SCRATCH
121c0 20 20 20 20 20 20 20 36 20 20 2f 2a 20 76 6f 69 6 /* voi
121d0 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 d*, int sz, int
121e0 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c N */.#define SQL
121f0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 ITE_CONFIG_PAGEC
12200 41 43 48 45 20 20 20 20 20 37 20 20 2f 2a 20 76 ACHE 7 /* v
12210 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e oid*, int sz, in
12220 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 t N */.#define S
12230 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41 QLITE_CONFIG_HEA
12240 50 20 20 20 20 20 20 20 20 20 20 38 20 20 2f 2a P 8 /*
12250 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 42 79 74 void*, int nByt
12260 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f 0a 23 64 e, int min */.#d
12270 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
12280 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 20 20 FIG_MEMSTATUS
12290 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 61 6e 20 9 /* boolean
122a0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
122b0 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 20 20 E_CONFIG_MUTEX
122c0 20 20 20 20 20 20 31 30 20 20 2f 2a 20 73 71 6c 10 /* sql
122d0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
122e0 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ds* */.#define S
122f0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 QLITE_CONFIG_GET
12300 4d 55 54 45 58 20 20 20 20 20 31 31 20 20 2f 2a MUTEX 11 /*
12310 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
12320 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f 2a 20 70 72 ethods* */./* pr
12330 65 76 69 6f 75 73 6c 79 20 53 51 4c 49 54 45 5f eviously SQLITE_
12340 43 4f 4e 46 49 47 5f 43 48 55 4e 4b 41 4c 4c 4f CONFIG_CHUNKALLO
12350 43 20 31 32 20 77 68 69 63 68 20 69 73 20 6e 6f C 12 which is no
12360 77 20 75 6e 75 73 65 64 2e 20 2a 2f 20 0a 23 64 w unused. */ .#d
12370 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
12380 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20 FIG_LOOKASIDE
12390 20 31 33 20 20 2f 2a 20 69 6e 74 20 69 6e 74 20 13 /* int int
123a0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
123b0 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 E_CONFIG_PCACHE
123c0 20 20 20 20 20 20 31 34 20 20 2f 2a 20 73 71 6c 14 /* sql
123d0 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 ite3_pcache_meth
123e0 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ods* */.#define
123f0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 SQLITE_CONFIG_GE
12400 54 50 43 41 43 48 45 20 20 20 20 31 35 20 20 2f TPCACHE 15 /
12410 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 * sqlite3_pcache
12420 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 0a 2f 2a _methods* */../*
12430 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f .** CAPI3REF: Co
12440 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f 70 74 69 nfiguration Opti
12450 6f 6e 73 20 7b 48 31 30 31 37 30 7d 20 3c 53 32 ons {H10170} <S2
12460 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 0000>.** EXPERIM
12470 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 ENTAL.**.** Thes
12480 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 e constants are
12490 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e the available in
124a0 74 65 67 65 72 20 63 6f 6e 66 69 67 75 72 61 74 teger configurat
124b0 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 ion options that
124c0 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61 73 73 65 .** can be passe
124d0 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
124e0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
124f0 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 [sqlite3_db_conf
12500 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e ig()] interface.
12510 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e 66 69 .**.** New confi
12520 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 guration options
12530 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e may be added in
12540 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
12550 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 45 of SQLite..** E
12560 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 75 72 xisting configur
12570 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 69 ation options mi
12580 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 6e ght be discontin
12590 75 65 64 2e 20 20 41 70 70 6c 69 63 61 74 69 6f ued. Applicatio
125a0 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 68 65 ns.** should che
125b0 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f ck the return co
125c0 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 de from [sqlite3
125d0 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 74 6f _db_config()] to
125e0 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a make sure that.
125f0 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b ** the call work
12600 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 ed. The [sqlite
12610 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 3_db_config()] i
12620 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 nterface will re
12630 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 turn a.** non-ze
12640 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 ro [error code]
12650 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 if a discontinue
12660 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 d or unsupported
12670 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f configuration o
12680 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f ption.** is invo
12690 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a ked..**.** <dl>.
126a0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42 ** <dt>SQLITE_DB
126b0 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 CONFIG_LOOKASIDE
126c0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
126d0 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 74 s option takes t
126e0 68 72 65 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 hree additional
126f0 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 64 arguments that d
12700 65 74 65 72 6d 69 6e 65 20 74 68 65 20 0a 2a 2a etermine the .**
12710 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f [lookaside memo
12720 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f ry allocator] co
12730 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 6f 72 20 nfiguration for
12740 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
12750 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 20 54 68 nnection]..** Th
12760 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
12770 20 28 74 68 65 20 74 68 69 72 64 20 70 61 72 61 (the third para
12780 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 meter to [sqlite
12790 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 3_db_config()] i
127a0 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 s a.** pointer t
127b0 6f 20 61 6e 20 6d 65 6d 6f 72 79 20 62 75 66 66 o an memory buff
127c0 65 72 20 74 6f 20 75 73 65 20 66 6f 72 20 6c 6f er to use for lo
127d0 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 2e 0a okaside memory..
127e0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 ** The first arg
127f0 75 6d 65 6e 74 20 6d 61 79 20 62 65 20 4e 55 4c ument may be NUL
12800 4c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 L in which case
12810 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 6c 6f SQLite will allo
12820 63 61 74 65 20 74 68 65 0a 2a 2a 20 6c 6f 6f 6b cate the.** look
12830 61 73 69 64 65 20 62 75 66 66 65 72 20 69 74 73 aside buffer its
12840 65 6c 66 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 elf using [sqlit
12850 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 e3_malloc()]. T
12860 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
12870 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 73 69 7a nt is the.** siz
12880 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f 6b 61 73 e of each lookas
12890 69 64 65 20 62 75 66 66 65 72 20 73 6c 6f 74 20 ide buffer slot
128a0 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 61 72 and the third ar
128b0 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75 gument is the nu
128c0 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f 74 73 mber of.** slots
128d0 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 . The size of t
128e0 68 65 20 62 75 66 66 65 72 20 69 6e 20 74 68 65 he buffer in the
128f0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
12900 6d 75 73 74 20 62 65 20 67 72 65 61 74 65 72 20 must be greater
12910 74 68 61 6e 0a 2a 2a 20 6f 72 20 65 71 75 61 6c than.** or equal
12920 20 74 6f 20 74 68 65 20 70 72 6f 64 75 63 74 20 to the product
12930 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e of the second an
12940 64 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 d third argument
12950 73 2e 20 20 54 68 65 20 62 75 66 66 65 72 0a 2a s. The buffer.*
12960 2a 20 6d 75 73 74 20 62 65 20 61 6c 69 67 6e 65 * must be aligne
12970 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 65 20 62 d to an 8-byte b
12980 6f 75 6e 64 61 72 79 2e 20 20 49 66 20 74 68 65 oundary. If the
12990 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
129a0 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 6d 75 6c is not.** a mul
129b0 74 69 70 6c 65 20 6f 66 20 38 2c 20 69 74 20 69 tiple of 8, it i
129c0 73 20 69 6e 74 65 72 6e 61 6c 6c 79 20 72 6f 75 s internally rou
129d0 6e 64 65 64 20 64 6f 77 6e 20 74 6f 20 74 68 65 nded down to the
129e0 20 6e 65 78 74 20 73 6d 61 6c 6c 65 72 0a 2a 2a next smaller.**
129f0 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2e 20 multiple of 8.
12a00 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49 See also: [SQLI
12a10 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 TE_CONFIG_LOOKAS
12a20 49 44 45 5d 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 IDE]</dd>.**.**
12a30 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 </dl>.*/.#define
12a40 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 SQLITE_DBCONFIG
12a50 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20 20 31 30 _LOOKASIDE 10
12a60 30 31 20 20 2f 2a 20 76 6f 69 64 2a 20 69 6e 74 01 /* void* int
12a70 20 69 6e 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 int */.../*.**
12a80 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 CAPI3REF: Enable
12a90 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74 65 Or Disable Exte
12aa0 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f 64 65 nded Result Code
12ab0 73 20 7b 48 31 32 32 30 30 7d 20 3c 53 31 30 37 s {H12200} <S107
12ac0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 00>.**.** The sq
12ad0 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 lite3_extended_r
12ae0 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 20 72 6f esult_codes() ro
12af0 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 utine enables or
12b00 20 64 69 73 61 62 6c 65 73 20 74 68 65 0a 2a 2a disables the.**
12b10 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c [extended resul
12b20 74 20 63 6f 64 65 73 5d 20 66 65 61 74 75 72 65 t codes] feature
12b30 20 6f 66 20 53 51 4c 69 74 65 2e 20 54 68 65 20 of SQLite. The
12b40 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 0a extended result.
12b50 2a 2a 20 63 6f 64 65 73 20 61 72 65 20 64 69 73 ** codes are dis
12b60 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 abled by default
12b70 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20 for historical
12b80 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 63 6f compatibility co
12b90 6e 73 69 64 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a nsiderations..**
12ba0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
12bb0 3a 0a 2a 2a 20 5b 48 31 32 32 30 31 5d 20 5b 48 :.** [H12201] [H
12bc0 31 32 32 30 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 12202].*/.SQLITE
12bd0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
12be0 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 _extended_result
12bf0 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33 2a 2c _codes(sqlite3*,
12c00 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a int onoff);../*
12c10 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 61 .** CAPI3REF: La
12c20 73 74 20 49 6e 73 65 72 74 20 52 6f 77 69 64 20 st Insert Rowid
12c30 7b 48 31 32 32 32 30 7d 20 3c 53 31 30 37 30 30 {H12220} <S10700
12c40 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65 6e 74 >.**.** Each ent
12c50 72 79 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 20 ry in an SQLite
12c60 74 61 62 6c 65 20 68 61 73 20 61 20 75 6e 69 71 table has a uniq
12c70 75 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 ue 64-bit signed
12c80 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20 .** integer key
12c90 63 61 6c 6c 65 64 20 74 68 65 20 5b 52 4f 57 49 called the [ROWI
12ca0 44 20 7c 20 22 72 6f 77 69 64 22 5d 2e 20 54 68 D | "rowid"]. Th
12cb0 65 20 72 6f 77 69 64 20 69 73 20 61 6c 77 61 79 e rowid is alway
12cc0 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 61 s available.** a
12cd0 73 20 61 6e 20 75 6e 64 65 63 6c 61 72 65 64 20 s an undeclared
12ce0 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 52 4f 57 column named ROW
12cf0 49 44 2c 20 4f 49 44 2c 20 6f 72 20 5f 52 4f 57 ID, OID, or _ROW
12d00 49 44 5f 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 ID_ as long as t
12d10 68 6f 73 65 0a 2a 2a 20 6e 61 6d 65 73 20 61 72 hose.** names ar
12d20 65 20 6e 6f 74 20 61 6c 73 6f 20 75 73 65 64 20 e not also used
12d30 62 79 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 by explicitly de
12d40 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 73 2e 20 clared columns.
12d50 49 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 If.** the table
12d60 68 61 73 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 has a column of
12d70 74 79 70 65 20 5b 49 4e 54 45 47 45 52 20 50 52 type [INTEGER PR
12d80 49 4d 41 52 59 20 4b 45 59 5d 20 74 68 65 6e 20 IMARY KEY] then
12d90 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 that column.** i
12da0 73 20 61 6e 6f 74 68 65 72 20 61 6c 69 61 73 20 s another alias
12db0 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a for the rowid..*
12dc0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
12dd0 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 5b 72 e returns the [r
12de0 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 6d 6f 73 owid] of the mos
12df0 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75 63 63 t recent.** succ
12e00 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 5d 20 essful [INSERT]
12e10 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
12e20 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61 74 61 e from the [data
12e30 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
12e40 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 73 74 .** in the first
12e50 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 6e argument. If n
12e60 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e o successful [IN
12e70 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65 20 65 SERT]s.** have e
12e80 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f 6e 20 ver occurred on
12e90 74 68 61 74 20 64 61 74 61 62 61 73 65 20 63 6f that database co
12ea0 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f 20 69 nnection, zero i
12eb0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
12ec0 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52 54 5d * If an [INSERT]
12ed0 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 61 occurs within a
12ee0 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e 20 74 trigger, then t
12ef0 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 he [rowid] of th
12f00 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 72 6f e inserted.** ro
12f10 77 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 w is returned by
12f20 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 73 this routine as
12f30 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 72 69 long as the tri
12f40 67 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 2e gger is running.
12f50 0a 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74 68 65 .** But once the
12f60 20 74 72 69 67 67 65 72 20 74 65 72 6d 69 6e 61 trigger termina
12f70 74 65 73 2c 20 74 68 65 20 76 61 6c 75 65 20 72 tes, the value r
12f80 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 eturned by this
12f90 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76 65 72 routine.** rever
12fa0 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20 76 ts to the last v
12fb0 61 6c 75 65 20 69 6e 73 65 72 74 65 64 20 62 65 alue inserted be
12fc0 66 6f 72 65 20 74 68 65 20 74 72 69 67 67 65 72 fore the trigger
12fd0 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e fired..**.** An
12fe0 20 5b 49 4e 53 45 52 54 5d 20 74 68 61 74 20 66 [INSERT] that f
12ff0 61 69 6c 73 20 64 75 65 20 74 6f 20 61 20 63 6f ails due to a co
13000 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 nstraint violati
13010 6f 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 73 on is not a.** s
13020 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 uccessful [INSER
13030 54 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 T] and does not
13040 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 change the value
13050 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 returned by thi
13060 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 20 54 s.** routine. T
13070 68 75 73 20 49 4e 53 45 52 54 20 4f 52 20 46 41 hus INSERT OR FA
13080 49 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20 49 47 IL, INSERT OR IG
13090 4e 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f 52 20 NORE, INSERT OR
130a0 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61 6e 64 ROLLBACK,.** and
130b0 20 49 4e 53 45 52 54 20 4f 52 20 41 42 4f 52 54 INSERT OR ABORT
130c0 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 make no changes
130d0 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e 20 76 to the return v
130e0 61 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 alue of this.**
130f0 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74 68 65 routine when the
13100 69 72 20 69 6e 73 65 72 74 69 6f 6e 20 66 61 69 ir insertion fai
13110 6c 73 2e 20 20 57 68 65 6e 20 49 4e 53 45 52 54 ls. When INSERT
13120 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a 20 65 OR REPLACE.** e
13130 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f 6e 73 ncounters a cons
13140 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e traint violation
13150 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 66 61 , it does not fa
13160 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e 53 45 il. The.** INSE
13170 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f 20 RT continues to
13180 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74 65 72 completion after
13190 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73 20 74 deleting rows t
131a0 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 74 68 hat caused.** th
131b0 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 72 6f e constraint pro
131c0 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54 20 4f blem so INSERT O
131d0 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c 20 61 R REPLACE will a
131e0 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a 2a 20 lways change.**
131f0 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 the return value
13200 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 of this interfa
13210 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 ce..**.** For th
13220 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 e purposes of th
13230 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e 20 5b is routine, an [
13240 49 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e 73 69 INSERT] is consi
13250 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65 20 73 dered to.** be s
13260 75 63 63 65 73 73 66 75 6c 20 65 76 65 6e 20 69 uccessful even i
13270 66 20 69 74 20 69 73 20 73 75 62 73 65 71 75 65 f it is subseque
13280 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b ntly rolled back
13290 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
132a0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 32 31 ents:.** [H12221
132b0 5d 20 5b 48 31 32 32 32 33 5d 0a 2a 2a 0a 2a 2a ] [H12223].**.**
132c0 20 49 66 20 61 20 73 65 70 61 72 61 74 65 20 74 If a separate t
132d0 68 72 65 61 64 20 70 65 72 66 6f 72 6d 73 20 61 hread performs a
132e0 20 6e 65 77 20 5b 49 4e 53 45 52 54 5d 20 6f 6e new [INSERT] on
132f0 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 64 61 74 the same.** dat
13300 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
13310 20 77 68 69 6c 65 20 74 68 65 20 5b 73 71 6c 69 while the [sqli
13320 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f te3_last_insert_
13330 72 6f 77 69 64 28 29 5d 0a 2a 2a 20 66 75 6e 63 rowid()].** func
13340 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 20 tion is running
13350 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67 65 73 and thus changes
13360 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74 the last insert
13370 20 5b 72 6f 77 69 64 5d 2c 0a 2a 2a 20 74 68 65 [rowid],.** the
13380 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 n the value retu
13390 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 rned by [sqlite3
133a0 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 _last_insert_row
133b0 69 64 28 29 5d 20 69 73 0a 2a 2a 20 75 6e 70 72 id()] is.** unpr
133c0 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6d 69 edictable and mi
133d0 67 68 74 20 6e 6f 74 20 65 71 75 61 6c 20 65 69 ght not equal ei
133e0 74 68 65 72 20 74 68 65 20 6f 6c 64 20 6f 72 20 ther the old or
133f0 74 68 65 20 6e 65 77 0a 2a 2a 20 6c 61 73 74 20 the new.** last
13400 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 5d 2e 0a insert [rowid]..
13410 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 */.SQLITE_API sq
13420 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 lite3_int64 sqli
13430 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f te3_last_insert_
13440 72 6f 77 69 64 28 73 71 6c 69 74 65 33 2a 29 3b rowid(sqlite3*);
13450 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
13460 3a 20 43 6f 75 6e 74 20 54 68 65 20 4e 75 6d 62 : Count The Numb
13470 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64 69 66 er Of Rows Modif
13480 69 65 64 20 7b 48 31 32 32 34 30 7d 20 3c 53 31 ied {H12240} <S1
13490 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 0600>.**.** This
134a0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
134b0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
134c0 64 61 74 61 62 61 73 65 20 72 6f 77 73 20 74 68 database rows th
134d0 61 74 20 77 65 72 65 20 63 68 61 6e 67 65 64 0a at were changed.
134e0 2a 2a 20 6f 72 20 69 6e 73 65 72 74 65 64 20 6f ** or inserted o
134f0 72 20 64 65 6c 65 74 65 64 20 62 79 20 74 68 65 r deleted by the
13500 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 most recently c
13510 6f 6d 70 6c 65 74 65 64 20 53 51 4c 20 73 74 61 ompleted SQL sta
13520 74 65 6d 65 6e 74 0a 2a 2a 20 6f 6e 20 74 68 65 tement.** on the
13530 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
13540 63 74 69 6f 6e 5d 20 73 70 65 63 69 66 69 65 64 ction] specified
13550 20 62 79 20 74 68 65 20 66 69 72 73 74 20 70 61 by the first pa
13560 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f 6e 6c 79 rameter..** Only
13570 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 61 72 changes that ar
13580 65 20 64 69 72 65 63 74 6c 79 20 73 70 65 63 69 e directly speci
13590 66 69 65 64 20 62 79 20 74 68 65 20 5b 49 4e 53 fied by the [INS
135a0 45 52 54 5d 2c 20 5b 55 50 44 41 54 45 5d 2c 0a ERT], [UPDATE],.
135b0 2a 2a 20 6f 72 20 5b 44 45 4c 45 54 45 5d 20 73 ** or [DELETE] s
135c0 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f 75 tatement are cou
135d0 6e 74 65 64 2e 20 20 41 75 78 69 6c 69 61 72 79 nted. Auxiliary
135e0 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 64 20 changes caused
135f0 62 79 0a 2a 2a 20 74 72 69 67 67 65 72 73 20 61 by.** triggers a
13600 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 2e 20 re not counted.
13610 55 73 65 20 74 68 65 20 5b 73 71 6c 69 74 65 33 Use the [sqlite3
13620 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 _total_changes()
13630 5d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 6f ] function.** to
13640 20 66 69 6e 64 20 74 68 65 20 74 6f 74 61 6c 20 find the total
13650 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 number of change
13660 73 20 69 6e 63 6c 75 64 69 6e 67 20 63 68 61 6e s including chan
13670 67 65 73 20 63 61 75 73 65 64 20 62 79 20 74 72 ges caused by tr
13680 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 43 68 iggers..**.** Ch
13690 61 6e 67 65 73 20 74 6f 20 61 20 76 69 65 77 20 anges to a view
136a0 74 68 61 74 20 61 72 65 20 73 69 6d 75 6c 61 74 that are simulat
136b0 65 64 20 62 79 20 61 6e 20 5b 49 4e 53 54 45 41 ed by an [INSTEA
136c0 44 20 4f 46 20 74 72 69 67 67 65 72 5d 0a 2a 2a D OF trigger].**
136d0 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 are not counted
136e0 2e 20 20 4f 6e 6c 79 20 72 65 61 6c 20 74 61 62 . Only real tab
136f0 6c 65 20 63 68 61 6e 67 65 73 20 61 72 65 20 63 le changes are c
13700 6f 75 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 ounted..**.** A
13710 22 72 6f 77 20 63 68 61 6e 67 65 22 20 69 73 20 "row change" is
13720 61 20 63 68 61 6e 67 65 20 74 6f 20 61 20 73 69 a change to a si
13730 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 20 73 69 ngle row of a si
13740 6e 67 6c 65 20 74 61 62 6c 65 0a 2a 2a 20 63 61 ngle table.** ca
13750 75 73 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52 used by an INSER
13760 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 T, DELETE, or UP
13770 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 DATE statement.
13780 20 52 6f 77 73 20 74 68 61 74 0a 2a 2a 20 61 72 Rows that.** ar
13790 65 20 63 68 61 6e 67 65 64 20 61 73 20 73 69 64 e changed as sid
137a0 65 20 65 66 66 65 63 74 73 20 6f 66 20 5b 52 45 e effects of [RE
137b0 50 4c 41 43 45 5d 20 63 6f 6e 73 74 72 61 69 6e PLACE] constrain
137c0 74 20 72 65 73 6f 6c 75 74 69 6f 6e 2c 0a 2a 2a t resolution,.**
137d0 20 72 6f 6c 6c 62 61 63 6b 2c 20 41 42 4f 52 54 rollback, ABORT
137e0 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 5b 44 52 processing, [DR
137f0 4f 50 20 54 41 42 4c 45 5d 2c 20 6f 72 20 62 79 OP TABLE], or by
13800 20 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6d 65 any other.** me
13810 63 68 61 6e 69 73 6d 73 20 64 6f 20 6e 6f 74 20 chanisms do not
13820 63 6f 75 6e 74 20 61 73 20 64 69 72 65 63 74 20 count as direct
13830 72 6f 77 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a row changes..**.
13840 2a 2a 20 41 20 22 74 72 69 67 67 65 72 20 63 6f ** A "trigger co
13850 6e 74 65 78 74 22 20 69 73 20 61 20 73 63 6f 70 ntext" is a scop
13860 65 20 6f 66 20 65 78 65 63 75 74 69 6f 6e 20 74 e of execution t
13870 68 61 74 20 62 65 67 69 6e 73 20 61 6e 64 0a 2a hat begins and.*
13880 2a 20 65 6e 64 73 20 77 69 74 68 20 74 68 65 20 * ends with the
13890 73 63 72 69 70 74 20 6f 66 20 61 20 5b 43 52 45 script of a [CRE
138a0 41 54 45 20 54 52 49 47 47 45 52 20 7c 20 74 72 ATE TRIGGER | tr
138b0 69 67 67 65 72 5d 2e 20 0a 2a 2a 20 4d 6f 73 74 igger]. .** Most
138c0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 SQL statements
138d0 61 72 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 are.** evaluated
138e0 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e 79 20 outside of any
138f0 74 72 69 67 67 65 72 2e 20 20 54 68 69 73 20 69 trigger. This i
13900 73 20 74 68 65 20 22 74 6f 70 20 6c 65 76 65 6c s the "top level
13910 22 0a 2a 2a 20 74 72 69 67 67 65 72 20 63 6f 6e ".** trigger con
13920 74 65 78 74 2e 20 20 49 66 20 61 20 74 72 69 67 text. If a trig
13930 67 65 72 20 66 69 72 65 73 20 66 72 6f 6d 20 74 ger fires from t
13940 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20 61 0a he top level, a.
13950 2a 2a 20 6e 65 77 20 74 72 69 67 67 65 72 20 63 ** new trigger c
13960 6f 6e 74 65 78 74 20 69 73 20 65 6e 74 65 72 65 ontext is entere
13970 64 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 d for the durati
13980 6f 6e 20 6f 66 20 74 68 61 74 20 6f 6e 65 0a 2a on of that one.*
13990 2a 20 74 72 69 67 67 65 72 2e 20 20 53 75 62 74 * trigger. Subt
139a0 72 69 67 67 65 72 73 20 63 72 65 61 74 65 20 73 riggers create s
139b0 75 62 63 6f 6e 74 65 78 74 73 20 66 6f 72 20 74 ubcontexts for t
139c0 68 65 69 72 20 64 75 72 61 74 69 6f 6e 2e 0a 2a heir duration..*
139d0 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 5b 73 71 *.** Calling [sq
139e0 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 6f 72 lite3_exec()] or
139f0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
13a00 5d 20 72 65 63 75 72 73 69 76 65 6c 79 20 64 6f ] recursively do
13a10 65 73 0a 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65 es.** not create
13a20 20 61 20 6e 65 77 20 74 72 69 67 67 65 72 20 63 a new trigger c
13a30 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 ontext..**.** Th
13a40 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
13a50 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
13a60 66 20 64 69 72 65 63 74 20 72 6f 77 20 63 68 61 f direct row cha
13a70 6e 67 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6d nges in the.** m
13a80 6f 73 74 20 72 65 63 65 6e 74 20 49 4e 53 45 52 ost recent INSER
13a90 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 T, UPDATE, or DE
13aa0 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 77 LETE statement w
13ab0 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a ithin the same.*
13ac0 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 * trigger contex
13ad0 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 75 73 2c 20 77 t..**.** Thus, w
13ae0 68 65 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 hen called from
13af0 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20 74 the top level, t
13b00 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
13b10 75 72 6e 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 urns the.** numb
13b20 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20 69 6e er of changes in
13b30 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
13b40 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c INSERT, UPDATE,
13b50 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 or DELETE.** th
13b60 61 74 20 61 6c 73 6f 20 6f 63 63 75 72 72 65 64 at also occurred
13b70 20 61 74 20 74 68 65 20 74 6f 70 20 6c 65 76 65 at the top leve
13b80 6c 2e 20 20 57 69 74 68 69 6e 20 74 68 65 20 62 l. Within the b
13b90 6f 64 79 20 6f 66 20 61 20 74 72 69 67 67 65 72 ody of a trigger
13ba0 2c 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 ,.** the sqlite3
13bb0 5f 63 68 61 6e 67 65 73 28 29 20 69 6e 74 65 72 _changes() inter
13bc0 66 61 63 65 20 63 61 6e 20 62 65 20 63 61 6c 6c face can be call
13bd0 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e ed to find the n
13be0 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 6e umber of.** chan
13bf0 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 ges in the most
13c00 72 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 recently complet
13c10 65 64 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 ed INSERT, UPDAT
13c20 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 E, or DELETE.**
13c30 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e statement within
13c40 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 the body of the
13c50 20 73 61 6d 65 20 74 72 69 67 67 65 72 2e 0a 2a same trigger..*
13c60 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 6e * However, the n
13c70 75 6d 62 65 72 20 72 65 74 75 72 6e 65 64 20 64 umber returned d
13c80 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 oes not include
13c90 63 68 61 6e 67 65 73 0a 2a 2a 20 63 61 75 73 65 changes.** cause
13ca0 64 20 62 79 20 73 75 62 74 72 69 67 67 65 72 73 d by subtriggers
13cb0 20 73 69 6e 63 65 20 74 68 6f 73 65 20 68 61 76 since those hav
13cc0 65 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 6e 74 e their own cont
13cd0 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 ext..**.** See a
13ce0 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 lso the [sqlite3
13cf0 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 _total_changes()
13d00 5d 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64 20 ] interface and
13d10 74 68 65 0a 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68 the.** [count_ch
13d20 61 6e 67 65 73 20 70 72 61 67 6d 61 5d 2e 0a 2a anges pragma]..*
13d30 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
13d40 73 3a 0a 2a 2a 20 5b 48 31 32 32 34 31 5d 20 5b s:.** [H12241] [
13d50 48 31 32 32 34 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 H12243].**.** If
13d60 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72 65 a separate thre
13d70 61 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 ad makes changes
13d80 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 on the same dat
13d90 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
13da0 0a 2a 2a 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 .** while [sqlit
13db0 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 73 e3_changes()] is
13dc0 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 74 68 running then th
13dd0 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
13de0 0a 2a 2a 20 69 73 20 75 6e 70 72 65 64 69 63 74 .** is unpredict
13df0 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d 65 61 able and not mea
13e00 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 ningful..*/.SQLI
13e10 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
13e20 65 33 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 74 e3_changes(sqlit
13e30 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 e3*);../*.** CAP
13e40 49 33 52 45 46 3a 20 54 6f 74 61 6c 20 4e 75 6d I3REF: Total Num
13e50 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64 69 ber Of Rows Modi
13e60 66 69 65 64 20 7b 48 31 32 32 36 30 7d 20 3c 53 fied {H12260} <S
13e70 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 10600>.**.** Thi
13e80 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
13e90 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
13ea0 20 72 6f 77 20 63 68 61 6e 67 65 73 20 63 61 75 row changes cau
13eb0 73 65 64 20 62 79 20 5b 49 4e 53 45 52 54 5d 2c sed by [INSERT],
13ec0 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 20 6f 72 20 .** [UPDATE] or
13ed0 5b 44 45 4c 45 54 45 5d 20 73 74 61 74 65 6d 65 [DELETE] stateme
13ee0 6e 74 73 20 73 69 6e 63 65 20 74 68 65 20 5b 64 nts since the [d
13ef0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
13f00 6f 6e 5d 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a on] was opened..
13f10 2a 2a 20 54 68 65 20 63 6f 75 6e 74 20 69 6e 63 ** The count inc
13f20 6c 75 64 65 73 20 61 6c 6c 20 63 68 61 6e 67 65 ludes all change
13f30 73 20 66 72 6f 6d 20 61 6c 6c 20 0a 2a 2a 20 5b s from all .** [
13f40 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 7c CREATE TRIGGER |
13f50 20 74 72 69 67 67 65 72 5d 20 63 6f 6e 74 65 78 trigger] contex
13f60 74 73 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a ts. However,.**
13f70 20 74 68 65 20 63 6f 75 6e 74 20 64 6f 65 73 20 the count does
13f80 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68 61 6e not include chan
13f90 67 65 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c ges used to impl
13fa0 65 6d 65 6e 74 20 5b 52 45 50 4c 41 43 45 5d 20 ement [REPLACE]
13fb0 63 6f 6e 73 74 72 61 69 6e 74 73 2c 0a 2a 2a 20 constraints,.**
13fc0 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 6f 72 20 do rollbacks or
13fd0 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e 67 ABORT processing
13fe0 2c 20 6f 72 20 5b 44 52 4f 50 20 54 41 42 4c 45 , or [DROP TABLE
13ff0 5d 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 54 ] processing. T
14000 68 65 0a 2a 2a 20 63 6f 75 6e 74 20 64 6f 65 73 he.** count does
14010 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 72 6f 77 not include row
14020 73 20 6f 66 20 76 69 65 77 73 20 74 68 61 74 20 s of views that
14030 66 69 72 65 20 61 6e 20 5b 49 4e 53 54 45 41 44 fire an [INSTEAD
14040 20 4f 46 20 74 72 69 67 67 65 72 5d 2c 0a 2a 2a OF trigger],.**
14050 20 74 68 6f 75 67 68 20 69 66 20 74 68 65 20 49 though if the I
14060 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 NSTEAD OF trigge
14070 72 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20 r makes changes
14080 6f 66 20 69 74 73 20 6f 77 6e 2c 20 74 68 6f 73 of its own, thos
14090 65 20 63 68 61 6e 67 65 73 20 0a 2a 2a 20 61 72 e changes .** ar
140a0 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 20 54 68 e counted..** Th
140b0 65 20 63 68 61 6e 67 65 73 20 61 72 65 20 63 6f e changes are co
140c0 75 6e 74 65 64 20 61 73 20 73 6f 6f 6e 20 61 73 unted as soon as
140d0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 the statement t
140e0 68 61 74 20 6d 61 6b 65 73 20 74 68 65 6d 20 69 hat makes them i
140f0 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 20 28 s.** completed (
14100 77 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 when the stateme
14110 6e 74 20 68 61 6e 64 6c 65 20 69 73 20 70 61 73 nt handle is pas
14120 73 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f sed to [sqlite3_
14130 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b reset()] or.** [
14140 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
14150 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 ()])..**.** See
14160 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 also the [sqlite
14170 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 6e 74 3_changes()] int
14180 65 72 66 61 63 65 20 61 6e 64 20 74 68 65 0a 2a erface and the.*
14190 2a 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 * [count_changes
141a0 20 70 72 61 67 6d 61 5d 2e 0a 2a 2a 0a 2a 2a 20 pragma]..**.**
141b0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
141c0 20 5b 48 31 32 32 36 31 5d 20 5b 48 31 32 32 36 [H12261] [H1226
141d0 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 65 3].**.** If a se
141e0 70 61 72 61 74 65 20 74 68 72 65 61 64 20 6d 61 parate thread ma
141f0 6b 65 73 20 63 68 61 6e 67 65 73 20 6f 6e 20 74 kes changes on t
14200 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
14210 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 connection.** w
14220 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f hile [sqlite3_to
14230 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 tal_changes()] i
14240 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 74 s running then t
14250 68 65 20 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 he value.** retu
14260 72 6e 65 64 20 69 73 20 75 6e 70 72 65 64 69 63 rned is unpredic
14270 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d 65 table and not me
14280 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c aningful..*/.SQL
14290 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
142a0 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 te3_total_change
142b0 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a s(sqlite3*);../*
142c0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e .** CAPI3REF: In
142d0 74 65 72 72 75 70 74 20 41 20 4c 6f 6e 67 2d 52 terrupt A Long-R
142e0 75 6e 6e 69 6e 67 20 51 75 65 72 79 20 7b 48 31 unning Query {H1
142f0 32 32 37 30 7d 20 3c 53 33 30 35 30 30 3e 0a 2a 2270} <S30500>.*
14300 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
14310 6f 6e 20 63 61 75 73 65 73 20 61 6e 79 20 70 65 on causes any pe
14320 6e 64 69 6e 67 20 64 61 74 61 62 61 73 65 20 6f nding database o
14330 70 65 72 61 74 69 6f 6e 20 74 6f 20 61 62 6f 72 peration to abor
14340 74 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 t and.** return
14350 61 74 20 69 74 73 20 65 61 72 6c 69 65 73 74 20 at its earliest
14360 6f 70 70 6f 72 74 75 6e 69 74 79 2e 20 54 68 69 opportunity. Thi
14370 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 79 70 s routine is typ
14380 69 63 61 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 65 64 ically.** called
14390 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 in response to
143a0 61 20 75 73 65 72 20 61 63 74 69 6f 6e 20 73 75 a user action su
143b0 63 68 20 61 73 20 70 72 65 73 73 69 6e 67 20 22 ch as pressing "
143c0 43 61 6e 63 65 6c 22 0a 2a 2a 20 6f 72 20 43 74 Cancel".** or Ct
143d0 72 6c 2d 43 20 77 68 65 72 65 20 74 68 65 20 75 rl-C where the u
143e0 73 65 72 20 77 61 6e 74 73 20 61 20 6c 6f 6e 67 ser wants a long
143f0 20 71 75 65 72 79 20 6f 70 65 72 61 74 69 6f 6e query operation
14400 20 74 6f 20 68 61 6c 74 0a 2a 2a 20 69 6d 6d 65 to halt.** imme
14410 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 diately..**.** I
14420 74 20 69 73 20 73 61 66 65 20 74 6f 20 63 61 6c t is safe to cal
14430 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 l this routine f
14440 72 6f 6d 20 61 20 74 68 72 65 61 64 20 64 69 66 rom a thread dif
14450 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 0a ferent from the.
14460 2a 2a 20 74 68 72 65 61 64 20 74 68 61 74 20 69 ** thread that i
14470 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e s currently runn
14480 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 ing the database
14490 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 operation. But
144a0 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 73 61 it.** is not sa
144b0 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 fe to call this
144c0 72 6f 75 74 69 6e 65 20 77 69 74 68 20 61 20 5b routine with a [
144d0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
144e0 69 6f 6e 5d 20 74 68 61 74 0a 2a 2a 20 69 73 20 ion] that.** is
144f0 63 6c 6f 73 65 64 20 6f 72 20 6d 69 67 68 74 20 closed or might
14500 63 6c 6f 73 65 20 62 65 66 6f 72 65 20 73 71 6c close before sql
14510 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 ite3_interrupt()
14520 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 returns..**.**
14530 49 66 20 61 6e 20 53 51 4c 20 6f 70 65 72 61 74 If an SQL operat
14540 69 6f 6e 20 69 73 20 76 65 72 79 20 6e 65 61 72 ion is very near
14550 6c 79 20 66 69 6e 69 73 68 65 64 20 61 74 20 74 ly finished at t
14560 68 65 20 74 69 6d 65 20 77 68 65 6e 0a 2a 2a 20 he time when.**
14570 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 sqlite3_interrup
14580 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 t() is called, t
14590 68 65 6e 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 hen it might not
145a0 20 68 61 76 65 20 61 6e 20 6f 70 70 6f 72 74 75 have an opportu
145b0 6e 69 74 79 0a 2a 2a 20 74 6f 20 62 65 20 69 6e nity.** to be in
145c0 74 65 72 72 75 70 74 65 64 20 61 6e 64 20 6d 69 terrupted and mi
145d0 67 68 74 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 ght continue to
145e0 63 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a completion..**.*
145f0 2a 20 41 6e 20 53 51 4c 20 6f 70 65 72 61 74 69 * An SQL operati
14600 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 74 65 72 on that is inter
14610 72 75 70 74 65 64 20 77 69 6c 6c 20 72 65 74 75 rupted will retu
14620 72 6e 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 rn [SQLITE_INTER
14630 52 55 50 54 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 RUPT]..** If the
14640 20 69 6e 74 65 72 72 75 70 74 65 64 20 53 51 4c interrupted SQL
14650 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e operation is an
14660 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c INSERT, UPDATE,
14670 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 or DELETE.** th
14680 61 74 20 69 73 20 69 6e 73 69 64 65 20 61 6e 20 at is inside an
14690 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63 explicit transac
146a0 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 65 tion, then the e
146b0 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f ntire transactio
146c0 6e 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 72 6f 6c n.** will be rol
146d0 6c 65 64 20 62 61 63 6b 20 61 75 74 6f 6d 61 74 led back automat
146e0 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 ically..**.** Th
146f0 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 e sqlite3_interr
14700 75 70 74 28 44 29 20 63 61 6c 6c 20 69 73 20 69 upt(D) call is i
14710 6e 20 65 66 66 65 63 74 20 75 6e 74 69 6c 20 61 n effect until a
14720 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e ll currently run
14730 6e 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 ning.** SQL stat
14740 65 6d 65 6e 74 73 20 6f 6e 20 5b 64 61 74 61 62 ements on [datab
14750 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
14760 44 20 63 6f 6d 70 6c 65 74 65 2e 20 20 41 6e 79 D complete. Any
14770 20 6e 65 77 20 53 51 4c 20 73 74 61 74 65 6d 65 new SQL stateme
14780 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 nts.** that are
14790 73 74 61 72 74 65 64 20 61 66 74 65 72 20 74 68 started after th
147a0 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 e sqlite3_interr
147b0 75 70 74 28 29 20 63 61 6c 6c 20 61 6e 64 20 62 upt() call and b
147c0 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a 20 72 75 efore the .** ru
147d0 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 nning statements
147e0 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 72 reaches zero ar
147f0 65 20 69 6e 74 65 72 72 75 70 74 65 64 20 61 73 e interrupted as
14800 20 69 66 20 74 68 65 79 20 68 61 64 20 62 65 65 if they had bee
14810 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 70 72 69 n.** running pri
14820 6f 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 or to the sqlite
14830 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 63 61 3_interrupt() ca
14840 6c 6c 2e 20 20 4e 65 77 20 53 51 4c 20 73 74 61 ll. New SQL sta
14850 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 tements.** that
14860 61 72 65 20 73 74 61 72 74 65 64 20 61 66 74 65 are started afte
14870 72 20 74 68 65 20 72 75 6e 6e 69 6e 67 20 73 74 r the running st
14880 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74 20 72 65 atement count re
14890 61 63 68 65 73 20 7a 65 72 6f 20 61 72 65 0a 2a aches zero are.*
148a0 2a 20 6e 6f 74 20 65 66 66 65 63 74 65 64 20 62 * not effected b
148b0 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e y the sqlite3_in
148c0 74 65 72 72 75 70 74 28 29 2e 0a 2a 2a 20 41 20 terrupt()..** A
148d0 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
148e0 69 6e 74 65 72 72 75 70 74 28 44 29 20 74 68 61 interrupt(D) tha
148f0 74 20 6f 63 63 75 72 73 20 77 68 65 6e 20 74 68 t occurs when th
14900 65 72 65 20 61 72 65 20 6e 6f 20 72 75 6e 6e 69 ere are no runni
14910 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d ng.** SQL statem
14920 65 6e 74 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 ents is a no-op
14930 61 6e 64 20 68 61 73 20 6e 6f 20 65 66 66 65 63 and has no effec
14940 74 20 6f 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 t on SQL stateme
14950 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 nts.** that are
14960 73 74 61 72 74 65 64 20 61 66 74 65 72 20 74 68 started after th
14970 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 e sqlite3_interr
14980 75 70 74 28 29 20 63 61 6c 6c 20 72 65 74 75 72 upt() call retur
14990 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ns..**.** Requir
149a0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 ements:.** [H122
149b0 37 31 5d 20 5b 48 31 32 32 37 32 5d 0a 2a 2a 0a 71] [H12272].**.
149c0 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 ** If the databa
149d0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6c se connection cl
149e0 6f 73 65 73 20 77 68 69 6c 65 20 5b 73 71 6c 69 oses while [sqli
149f0 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 5d te3_interrupt()]
14a00 0a 2a 2a 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 .** is running t
14a10 68 65 6e 20 62 61 64 20 74 68 69 6e 67 73 20 77 hen bad things w
14a20 69 6c 6c 20 6c 69 6b 65 6c 79 20 68 61 70 70 65 ill likely happe
14a30 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 n..*/.SQLITE_API
14a40 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 69 6e void sqlite3_in
14a50 74 65 72 72 75 70 74 28 73 71 6c 69 74 65 33 2a terrupt(sqlite3*
14a60 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
14a70 45 46 3a 20 44 65 74 65 72 6d 69 6e 65 20 49 66 EF: Determine If
14a80 20 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e An SQL Statemen
14a90 74 20 49 73 20 43 6f 6d 70 6c 65 74 65 20 7b 48 t Is Complete {H
14aa0 31 30 35 31 30 7d 20 3c 53 37 30 32 30 30 3e 0a 10510} <S70200>.
14ab0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 **.** These rout
14ac0 69 6e 65 73 20 61 72 65 20 75 73 65 66 75 6c 20 ines are useful
14ad0 64 75 72 69 6e 67 20 63 6f 6d 6d 61 6e 64 2d 6c during command-l
14ae0 69 6e 65 20 69 6e 70 75 74 20 74 6f 20 64 65 74 ine input to det
14af0 65 72 6d 69 6e 65 20 69 66 20 74 68 65 0a 2a 2a ermine if the.**
14b00 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 currently enter
14b10 65 64 20 74 65 78 74 20 73 65 65 6d 73 20 74 6f ed text seems to
14b20 20 66 6f 72 6d 20 61 20 63 6f 6d 70 6c 65 74 65 form a complete
14b30 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 6f SQL statement o
14b40 72 0a 2a 2a 20 69 66 20 61 64 64 69 74 69 6f 6e r.** if addition
14b50 61 6c 20 69 6e 70 75 74 20 69 73 20 6e 65 65 64 al input is need
14b60 65 64 20 62 65 66 6f 72 65 20 73 65 6e 64 69 6e ed before sendin
14b70 67 20 74 68 65 20 74 65 78 74 20 69 6e 74 6f 0a g the text into.
14b80 2a 2a 20 53 51 4c 69 74 65 20 66 6f 72 20 70 61 ** SQLite for pa
14b90 72 73 69 6e 67 2e 20 20 54 68 65 73 65 20 72 6f rsing. These ro
14ba0 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 31 20 utines return 1
14bb0 69 66 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 if the input str
14bc0 69 6e 67 0a 2a 2a 20 61 70 70 65 61 72 73 20 74 ing.** appears t
14bd0 6f 20 62 65 20 61 20 63 6f 6d 70 6c 65 74 65 20 o be a complete
14be0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 SQL statement.
14bf0 41 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6a A statement is j
14c00 75 64 67 65 64 20 74 6f 20 62 65 0a 2a 2a 20 63 udged to be.** c
14c10 6f 6d 70 6c 65 74 65 20 69 66 20 69 74 20 65 6e omplete if it en
14c20 64 73 20 77 69 74 68 20 61 20 73 65 6d 69 63 6f ds with a semico
14c30 6c 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64 20 69 73 lon token and is
14c40 20 6e 6f 74 20 61 20 70 72 65 66 69 78 20 6f 66 not a prefix of
14c50 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 a.** well-forme
14c60 64 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 d CREATE TRIGGER
14c70 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 53 65 6d statement. Sem
14c80 69 63 6f 6c 6f 6e 73 20 74 68 61 74 20 61 72 65 icolons that are
14c90 20 65 6d 62 65 64 64 65 64 20 77 69 74 68 69 6e embedded within
14ca0 0a 2a 2a 20 73 74 72 69 6e 67 20 6c 69 74 65 72 .** string liter
14cb0 61 6c 73 20 6f 72 20 71 75 6f 74 65 64 20 69 64 als or quoted id
14cc0 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 73 20 6f entifier names o
14cd0 72 20 63 6f 6d 6d 65 6e 74 73 20 61 72 65 20 6e r comments are n
14ce0 6f 74 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e ot.** independen
14cf0 74 20 74 6f 6b 65 6e 73 20 28 74 68 65 79 20 61 t tokens (they a
14d00 72 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 re part of the t
14d10 6f 6b 65 6e 20 69 6e 20 77 68 69 63 68 20 74 68 oken in which th
14d20 65 79 20 61 72 65 0a 2a 2a 20 65 6d 62 65 64 64 ey are.** embedd
14d30 65 64 29 20 61 6e 64 20 74 68 75 73 20 64 6f 20 ed) and thus do
14d40 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 61 20 73 not count as a s
14d50 74 61 74 65 6d 65 6e 74 20 74 65 72 6d 69 6e 61 tatement termina
14d60 74 6f 72 2e 20 20 57 68 69 74 65 73 70 61 63 65 tor. Whitespace
14d70 0a 2a 2a 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 .** and comments
14d80 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74 68 65 that follow the
14d90 20 66 69 6e 61 6c 20 73 65 6d 69 63 6f 6c 6f 6e final semicolon
14da0 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a are ignored..**
14db0 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e .** These routin
14dc0 65 73 20 72 65 74 75 72 6e 20 30 20 69 66 20 74 es return 0 if t
14dd0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 he statement is
14de0 69 6e 63 6f 6d 70 6c 65 74 65 2e 20 20 49 66 20 incomplete. If
14df0 61 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f a.** memory allo
14e00 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 cation fails, th
14e10 65 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 en SQLITE_NOMEM
14e20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
14e30 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
14e40 73 20 64 6f 20 6e 6f 74 20 70 61 72 73 65 20 74 s do not parse t
14e50 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
14e60 73 20 74 68 75 73 0a 2a 2a 20 77 69 6c 6c 20 6e s thus.** will n
14e70 6f 74 20 64 65 74 65 63 74 20 73 79 6e 74 61 63 ot detect syntac
14e80 74 69 63 61 6c 6c 79 20 69 6e 63 6f 72 72 65 63 tically incorrec
14e90 74 20 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 t SQL..**.** If
14ea0 53 51 4c 69 74 65 20 68 61 73 20 6e 6f 74 20 62 SQLite has not b
14eb0 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 een initialized
14ec0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 69 using [sqlite3_i
14ed0 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 70 72 69 nitialize()] pri
14ee0 6f 72 20 0a 2a 2a 20 74 6f 20 69 6e 76 6f 6b 69 or .** to invoki
14ef0 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c ng sqlite3_compl
14f00 65 74 65 31 36 28 29 20 74 68 65 6e 20 73 71 6c ete16() then sql
14f10 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
14f20 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 ) is invoked.**
14f30 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 automatically by
14f40 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 sqlite3_complet
14f50 65 31 36 28 29 2e 20 20 49 66 20 74 68 61 74 20 e16(). If that
14f60 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 66 initialization f
14f70 61 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 ails,.** then th
14f80 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 e return value f
14f90 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 rom sqlite3_comp
14fa0 6c 65 74 65 31 36 28 29 20 77 69 6c 6c 20 62 65 lete16() will be
14fb0 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 72 65 67 non-zero.** reg
14fc0 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 ardless of wheth
14fd0 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e er or not the in
14fe0 70 75 74 20 53 51 4c 20 69 73 20 63 6f 6d 70 6c put SQL is compl
14ff0 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ete..**.** Requi
15000 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 35 31 31 rements: [H10511
15010 5d 20 5b 48 31 30 35 31 32 5d 0a 2a 2a 0a 2a 2a ] [H10512].**.**
15020 20 54 68 65 20 69 6e 70 75 74 20 74 6f 20 5b 73 The input to [s
15030 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 qlite3_complete(
15040 29 5d 20 6d 75 73 74 20 62 65 20 61 20 7a 65 72 )] must be a zer
15050 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 o-terminated.**
15060 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a UTF-8 string..**
15070 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f .** The input to
15080 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 [sqlite3_comple
15090 74 65 31 36 28 29 5d 20 6d 75 73 74 20 62 65 20 te16()] must be
150a0 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 a zero-terminate
150b0 64 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69 d.** UTF-16 stri
150c0 6e 67 20 69 6e 20 6e 61 74 69 76 65 20 62 79 74 ng in native byt
150d0 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53 51 4c 49 e order..*/.SQLI
150e0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
150f0 65 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73 e3_complete(cons
15100 74 20 63 68 61 72 20 2a 73 71 6c 29 3b 0a 53 51 t char *sql);.SQ
15110 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
15120 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 ite3_complete16(
15130 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 29 const void *sql)
15140 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
15150 46 3a 20 52 65 67 69 73 74 65 72 20 41 20 43 61 F: Register A Ca
15160 6c 6c 62 61 63 6b 20 54 6f 20 48 61 6e 64 6c 65 llback To Handle
15170 20 53 51 4c 49 54 45 5f 42 55 53 59 20 45 72 72 SQLITE_BUSY Err
15180 6f 72 73 20 7b 48 31 32 33 31 30 7d 20 3c 53 34 ors {H12310} <S4
15190 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 0400>.**.** This
151a0 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61 20 routine sets a
151b0 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f callback functio
151c0 6e 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 n that might be
151d0 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 invoked whenever
151e0 0a 2a 2a 20 61 6e 20 61 74 74 65 6d 70 74 20 69 .** an attempt i
151f0 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 61 s made to open a
15200 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 database table
15210 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72 that another thr
15220 65 61 64 0a 2a 2a 20 6f 72 20 70 72 6f 63 65 73 ead.** or proces
15230 73 20 68 61 73 20 6c 6f 63 6b 65 64 2e 0a 2a 2a s has locked..**
15240 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79 20 .** If the busy
15250 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 4c 4c callback is NULL
15260 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 42 , then [SQLITE_B
15270 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f USY] or [SQLITE_
15280 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a IOERR_BLOCKED].*
15290 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d * is returned im
152a0 6d 65 64 69 61 74 65 6c 79 20 75 70 6f 6e 20 65 mediately upon e
152b0 6e 63 6f 75 6e 74 65 72 69 6e 67 20 74 68 65 20 ncountering the
152c0 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 62 75 73 lock. If the bus
152d0 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 y callback.** is
152e0 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 not NULL, then
152f0 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c the callback wil
15300 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 l be invoked wit
15310 68 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e h two arguments.
15320 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
15330 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
15340 20 68 61 6e 64 6c 65 72 20 69 73 20 61 20 63 6f handler is a co
15350 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 py of the void*
15360 70 6f 69 6e 74 65 72 20 77 68 69 63 68 0a 2a 2a pointer which.**
15370 20 69 73 20 74 68 65 20 74 68 69 72 64 20 61 72 is the third ar
15380 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 gument to sqlite
15390 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 3_busy_handler()
153a0 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 . The second ar
153b0 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 gument to.** the
153c0 20 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 handler callbac
153d0 6b 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 k is the number
153e0 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 74 68 of times that th
153f0 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 68 e busy handler h
15400 61 73 0a 2a 2a 20 62 65 65 6e 20 69 6e 76 6f 6b as.** been invok
15410 65 64 20 66 6f 72 20 74 68 69 73 20 6c 6f 63 6b ed for this lock
15420 69 6e 67 20 65 76 65 6e 74 2e 20 20 49 66 20 74 ing event. If t
15430 68 65 0a 2a 2a 20 62 75 73 79 20 63 61 6c 6c 62 he.** busy callb
15440 61 63 6b 20 72 65 74 75 72 6e 73 20 30 2c 20 74 ack returns 0, t
15450 68 65 6e 20 6e 6f 20 61 64 64 69 74 69 6f 6e 61 hen no additiona
15460 6c 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 6d l attempts are m
15470 61 64 65 20 74 6f 0a 2a 2a 20 61 63 63 65 73 73 ade to.** access
15480 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e the database an
15490 64 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 d [SQLITE_BUSY]
154a0 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 or [SQLITE_IOERR
154b0 5f 42 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 74 _BLOCKED] is ret
154c0 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 urned..** If the
154d0 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e callback return
154e0 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
154f0 20 61 6e 6f 74 68 65 72 20 61 74 74 65 6d 70 74 another attempt
15500 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 6f .** is made to o
15510 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 pen the database
15520 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 for reading and
15530 20 74 68 65 20 63 79 63 6c 65 20 72 65 70 65 61 the cycle repea
15540 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 ts..**.** The pr
15550 65 73 65 6e 63 65 20 6f 66 20 61 20 62 75 73 79 esence of a busy
15560 20 68 61 6e 64 6c 65 72 20 64 6f 65 73 20 6e 6f handler does no
15570 74 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 t guarantee that
15580 20 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f it will be invo
15590 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 72 ked.** when ther
155a0 65 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e e is lock conten
155b0 74 69 6f 6e 2e 20 49 66 20 53 51 4c 69 74 65 20 tion. If SQLite
155c0 64 65 74 65 72 6d 69 6e 65 73 20 74 68 61 74 20 determines that
155d0 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 invoking the bus
155e0 79 0a 2a 2a 20 68 61 6e 64 6c 65 72 20 63 6f 75 y.** handler cou
155f0 6c 64 20 72 65 73 75 6c 74 20 69 6e 20 61 20 64 ld result in a d
15600 65 61 64 6c 6f 63 6b 2c 20 69 74 20 77 69 6c 6c eadlock, it will
15610 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 72 65 go ahead and re
15620 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 turn [SQLITE_BUS
15630 59 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 Y].** or [SQLITE
15640 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 _IOERR_BLOCKED]
15650 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b instead of invok
15660 69 6e 67 20 74 68 65 20 62 75 73 79 20 68 61 6e ing the busy han
15670 64 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 dler..** Conside
15680 72 20 61 20 73 63 65 6e 61 72 69 6f 20 77 68 65 r a scenario whe
15690 72 65 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 69 re one process i
156a0 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 61 64 s holding a read
156b0 20 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a 20 69 74 lock that.** it
156c0 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20 70 72 is trying to pr
156d0 6f 6d 6f 74 65 20 74 6f 20 61 20 72 65 73 65 72 omote to a reser
156e0 76 65 64 20 6c 6f 63 6b 20 61 6e 64 0a 2a 2a 20 ved lock and.**
156f0 61 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 a second process
15700 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 is holding a re
15710 73 65 72 76 65 64 20 6c 6f 63 6b 20 74 68 61 74 served lock that
15720 20 69 74 20 69 73 20 74 72 79 69 6e 67 0a 2a 2a it is trying.**
15730 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 to promote to a
15740 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
15750 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 72 6f . The first pro
15760 63 65 73 73 20 63 61 6e 6e 6f 74 20 70 72 6f 63 cess cannot proc
15770 65 65 64 0a 2a 2a 20 62 65 63 61 75 73 65 20 69 eed.** because i
15780 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 t is blocked by
15790 74 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 the second and t
157a0 68 65 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 he second proces
157b0 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70 72 6f 63 s cannot.** proc
157c0 65 65 64 20 62 65 63 61 75 73 65 20 69 74 20 69 eed because it i
157d0 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 s blocked by the
157e0 20 66 69 72 73 74 2e 20 20 49 66 20 62 6f 74 68 first. If both
157f0 20 70 72 6f 63 65 73 73 65 73 0a 2a 2a 20 69 6e processes.** in
15800 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 voke the busy ha
15810 6e 64 6c 65 72 73 2c 20 6e 65 69 74 68 65 72 20 ndlers, neither
15820 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20 70 72 will make any pr
15830 6f 67 72 65 73 73 2e 20 20 54 68 65 72 65 66 6f ogress. Therefo
15840 72 65 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 72 65 re,.** SQLite re
15850 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 42 55 turns [SQLITE_BU
15860 53 59 5d 20 66 6f 72 20 74 68 65 20 66 69 72 73 SY] for the firs
15870 74 20 70 72 6f 63 65 73 73 2c 20 68 6f 70 69 6e t process, hopin
15880 67 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 77 g that this.** w
15890 69 6c 6c 20 69 6e 64 75 63 65 20 74 68 65 20 66 ill induce the f
158a0 69 72 73 74 20 70 72 6f 63 65 73 73 20 74 6f 20 irst process to
158b0 72 65 6c 65 61 73 65 20 69 74 73 20 72 65 61 64 release its read
158c0 20 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c 6f 77 0a lock and allow.
158d0 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 ** the second pr
158e0 6f 63 65 73 73 20 74 6f 20 70 72 6f 63 65 65 64 ocess to proceed
158f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 ..**.** The defa
15900 75 6c 74 20 62 75 73 79 20 63 61 6c 6c 62 61 63 ult busy callbac
15910 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a k is NULL..**.**
15920 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 42 55 53 The [SQLITE_BUS
15930 59 5d 20 65 72 72 6f 72 20 69 73 20 63 6f 6e 76 Y] error is conv
15940 65 72 74 65 64 20 74 6f 20 5b 53 51 4c 49 54 45 erted to [SQLITE
15950 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a _IOERR_BLOCKED].
15960 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 ** when SQLite i
15970 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 s in the middle
15980 6f 66 20 61 20 6c 61 72 67 65 20 74 72 61 6e 73 of a large trans
15990 61 63 74 69 6f 6e 20 77 68 65 72 65 20 61 6c 6c action where all
159a0 20 74 68 65 0a 2a 2a 20 63 68 61 6e 67 65 73 20 the.** changes
159b0 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69 6e 74 will not fit int
159c0 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 o the in-memory
159d0 63 61 63 68 65 2e 20 20 53 51 4c 69 74 65 20 77 cache. SQLite w
159e0 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 ill.** already h
159f0 6f 6c 64 20 61 20 52 45 53 45 52 56 45 44 20 6c old a RESERVED l
15a00 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
15a10 61 73 65 20 66 69 6c 65 2c 20 62 75 74 20 69 74 ase file, but it
15a20 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 70 72 6f needs.** to pro
15a30 6d 6f 74 65 20 74 68 69 73 20 6c 6f 63 6b 20 74 mote this lock t
15a40 6f 20 45 58 43 4c 55 53 49 56 45 20 73 6f 20 74 o EXCLUSIVE so t
15a50 68 61 74 20 69 74 20 63 61 6e 20 73 70 69 6c 6c hat it can spill
15a60 20 63 61 63 68 65 0a 2a 2a 20 70 61 67 65 73 20 cache.** pages
15a70 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
15a80 65 20 66 69 6c 65 20 77 69 74 68 6f 75 74 20 68 e file without h
15a90 61 72 6d 20 74 6f 20 63 6f 6e 63 75 72 72 65 6e arm to concurren
15aa0 74 0a 2a 2a 20 72 65 61 64 65 72 73 2e 20 20 49 t.** readers. I
15ab0 66 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20 74 f it is unable t
15ac0 6f 20 70 72 6f 6d 6f 74 65 20 74 68 65 20 6c 6f o promote the lo
15ad0 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 69 6e 2d ck, then the in-
15ae0 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61 63 68 65 20 memory.** cache
15af0 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 69 6e 20 will be left in
15b00 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 an inconsistent
15b10 73 74 61 74 65 20 61 6e 64 20 73 6f 20 74 68 65 state and so the
15b20 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 error.** code i
15b30 73 20 70 72 6f 6d 6f 74 65 64 20 66 72 6f 6d 20 s promoted from
15b40 74 68 65 20 72 65 6c 61 74 69 76 65 6c 79 20 62 the relatively b
15b50 65 6e 69 67 6e 20 5b 53 51 4c 49 54 45 5f 42 55 enign [SQLITE_BU
15b60 53 59 5d 20 74 6f 0a 2a 2a 20 74 68 65 20 6d 6f SY] to.** the mo
15b70 72 65 20 73 65 76 65 72 65 20 5b 53 51 4c 49 54 re severe [SQLIT
15b80 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d E_IOERR_BLOCKED]
15b90 2e 20 20 54 68 69 73 20 65 72 72 6f 72 20 63 6f . This error co
15ba0 64 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a 2a 2a 20 de promotion.**
15bb0 66 6f 72 63 65 73 20 61 6e 20 61 75 74 6f 6d 61 forces an automa
15bc0 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 tic rollback of
15bd0 74 68 65 20 63 68 61 6e 67 65 73 2e 20 20 53 65 the changes. Se
15be0 65 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 e the.** <a href
15bf0 3d 22 2f 63 76 73 74 72 61 63 2f 77 69 6b 69 3f ="/cvstrac/wiki?
15c00 70 3d 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c p=CorruptionFoll
15c10 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72 22 3e owingBusyError">
15c20 0a 2a 2a 20 43 6f 72 72 75 70 74 69 6f 6e 46 6f .** CorruptionFo
15c30 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72 llowingBusyError
15c40 3c 2f 61 3e 20 77 69 6b 69 20 70 61 67 65 20 66 </a> wiki page f
15c50 6f 72 20 61 20 64 69 73 63 75 73 73 69 6f 6e 20 or a discussion
15c60 6f 66 20 77 68 79 0a 2a 2a 20 74 68 69 73 20 69 of why.** this i
15c70 73 20 69 6d 70 6f 72 74 61 6e 74 2e 0a 2a 2a 0a s important..**.
15c80 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c ** There can onl
15c90 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 y be a single bu
15ca0 73 79 20 68 61 6e 64 6c 65 72 20 64 65 66 69 6e sy handler defin
15cb0 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 5b ed for each.** [
15cc0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
15cd0 69 6f 6e 5d 2e 20 20 53 65 74 74 69 6e 67 20 61 ion]. Setting a
15ce0 20 6e 65 77 20 62 75 73 79 20 68 61 6e 64 6c 65 new busy handle
15cf0 72 20 63 6c 65 61 72 73 20 61 6e 79 0a 2a 2a 20 r clears any.**
15d00 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 68 previously set h
15d10 61 6e 64 6c 65 72 2e 20 20 4e 6f 74 65 20 74 68 andler. Note th
15d20 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 at calling [sqli
15d30 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 te3_busy_timeout
15d40 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 6c 73 6f ()].** will also
15d50 20 73 65 74 20 6f 72 20 63 6c 65 61 72 20 74 68 set or clear th
15d60 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a e busy handler..
15d70 2a 2a 0a 2a 2a 20 54 68 65 20 62 75 73 79 20 63 **.** The busy c
15d80 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 20 6e allback should n
15d90 6f 74 20 74 61 6b 65 20 61 6e 79 20 61 63 74 69 ot take any acti
15da0 6f 6e 73 20 77 68 69 63 68 20 6d 6f 64 69 66 79 ons which modify
15db0 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 the.** database
15dc0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 connection that
15dd0 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73 invoked the bus
15de0 79 20 68 61 6e 64 6c 65 72 2e 20 20 41 6e 79 20 y handler. Any
15df0 73 75 63 68 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 such actions.**
15e00 72 65 73 75 6c 74 20 69 6e 20 75 6e 64 65 66 69 result in undefi
15e10 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a ned behavior..**
15e20 20 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 .** Requirement
15e30 73 3a 0a 2a 2a 20 5b 48 31 32 33 31 31 5d 20 5b s:.** [H12311] [
15e40 48 31 32 33 31 32 5d 20 5b 48 31 32 33 31 34 5d H12312] [H12314]
15e50 20 5b 48 31 32 33 31 36 5d 20 5b 48 31 32 33 31 [H12316] [H1231
15e60 38 5d 0a 2a 2a 0a 2a 2a 20 41 20 62 75 73 79 20 8].**.** A busy
15e70 68 61 6e 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74 handler must not
15e80 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 61 62 close the datab
15e90 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a ase connection.*
15ea0 2a 20 6f 72 20 5b 70 72 65 70 61 72 65 64 20 73 * or [prepared s
15eb0 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 tatement] that i
15ec0 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73 79 20 nvoked the busy
15ed0 68 61 6e 64 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 handler..*/.SQLI
15ee0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
15ef0 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 e3_busy_handler(
15f00 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 sqlite3*, int(*)
15f10 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 20 76 6f 69 (void*,int), voi
15f20 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 d*);../*.** CAPI
15f30 33 52 45 46 3a 20 53 65 74 20 41 20 42 75 73 79 3REF: Set A Busy
15f40 20 54 69 6d 65 6f 75 74 20 7b 48 31 32 33 34 30 Timeout {H12340
15f50 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a } <S40410>.**.**
15f60 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 This routine se
15f70 74 73 20 61 20 5b 73 71 6c 69 74 65 33 5f 62 75 ts a [sqlite3_bu
15f80 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20 62 75 73 sy_handler | bus
15f90 79 20 68 61 6e 64 6c 65 72 5d 20 74 68 61 74 20 y handler] that
15fa0 73 6c 65 65 70 73 0a 2a 2a 20 66 6f 72 20 61 20 sleeps.** for a
15fb0 73 70 65 63 69 66 69 65 64 20 61 6d 6f 75 6e 74 specified amount
15fc0 20 6f 66 20 74 69 6d 65 20 77 68 65 6e 20 61 20 of time when a
15fd0 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2e table is locked.
15fe0 20 20 54 68 65 20 68 61 6e 64 6c 65 72 0a 2a 2a The handler.**
15ff0 20 77 69 6c 6c 20 73 6c 65 65 70 20 6d 75 6c 74 will sleep mult
16000 69 70 6c 65 20 74 69 6d 65 73 20 75 6e 74 69 6c iple times until
16010 20 61 74 20 6c 65 61 73 74 20 22 6d 73 22 20 6d at least "ms" m
16020 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 illiseconds of s
16030 6c 65 65 70 69 6e 67 0a 2a 2a 20 68 61 76 65 20 leeping.** have
16040 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 7b 48 31 accumulated. {H1
16050 32 33 34 33 7d 20 41 66 74 65 72 20 22 6d 73 22 2343} After "ms"
16060 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 milliseconds of
16070 20 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a 20 74 68 sleeping,.** th
16080 65 20 68 61 6e 64 6c 65 72 20 72 65 74 75 72 6e e handler return
16090 73 20 30 20 77 68 69 63 68 20 63 61 75 73 65 73 s 0 which causes
160a0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
160b0 5d 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a 20 5b ] to return.** [
160c0 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 SQLITE_BUSY] or
160d0 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c [SQLITE_IOERR_BL
160e0 4f 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20 43 61 OCKED]..**.** Ca
160f0 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 lling this routi
16100 6e 65 20 77 69 74 68 20 61 6e 20 61 72 67 75 6d ne with an argum
16110 65 6e 74 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 ent less than or
16120 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 0a 2a equal to zero.*
16130 2a 20 74 75 72 6e 73 20 6f 66 66 20 61 6c 6c 20 * turns off all
16140 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2e 0a 2a busy handlers..*
16150 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 6f *.** There can o
16160 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20 nly be a single
16170 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 busy handler for
16180 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a a particular.**
16190 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
161a0 63 74 69 6f 6e 5d 20 61 6e 79 20 61 6e 79 20 67 ction] any any g
161b0 69 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20 20 49 66 iven moment. If
161c0 20 61 6e 6f 74 68 65 72 20 62 75 73 79 20 68 61 another busy ha
161d0 6e 64 6c 65 72 0a 2a 2a 20 77 61 73 20 64 65 66 ndler.** was def
161e0 69 6e 65 64 20 20 28 75 73 69 6e 67 20 5b 73 71 ined (using [sq
161f0 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c lite3_busy_handl
16200 65 72 28 29 5d 29 20 70 72 69 6f 72 20 74 6f 20 er()]) prior to
16210 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 calling.** this
16220 72 6f 75 74 69 6e 65 2c 20 74 68 61 74 20 6f 74 routine, that ot
16230 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c 65 72 her busy handler
16240 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a is cleared..**.
16250 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
16260 0a 2a 2a 20 5b 48 31 32 33 34 31 5d 20 5b 48 31 .** [H12341] [H1
16270 32 33 34 33 5d 20 5b 48 31 32 33 34 34 5d 0a 2a 2343] [H12344].*
16280 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
16290 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 sqlite3_busy_ti
162a0 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 2a 2c 20 meout(sqlite3*,
162b0 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 int ms);../*.**
162c0 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 76 65 6e CAPI3REF: Conven
162d0 69 65 6e 63 65 20 52 6f 75 74 69 6e 65 73 20 46 ience Routines F
162e0 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75 65 72 69 or Running Queri
162f0 65 73 20 7b 48 31 32 33 37 30 7d 20 3c 53 31 30 es {H12370} <S10
16300 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65 66 69 6e 000>.**.** Defin
16310 69 74 69 6f 6e 3a 20 41 20 3c 62 3e 72 65 73 75 ition: A <b>resu
16320 6c 74 20 74 61 62 6c 65 3c 2f 62 3e 20 69 73 20 lt table</b> is
16330 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74 72 75 memory data stru
16340 63 74 75 72 65 20 63 72 65 61 74 65 64 20 62 79 cture created by
16350 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 the.** [sqlite3
16360 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20 69 6e _get_table()] in
16370 74 65 72 66 61 63 65 2e 20 20 41 20 72 65 73 75 terface. A resu
16380 6c 74 20 74 61 62 6c 65 20 72 65 63 6f 72 64 73 lt table records
16390 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 the.** complete
163a0 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20 66 query results f
163b0 72 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 rom one or more
163c0 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 queries..**.** T
163d0 68 65 20 74 61 62 6c 65 20 63 6f 6e 63 65 70 74 he table concept
163e0 75 61 6c 6c 79 20 68 61 73 20 61 20 6e 75 6d 62 ually has a numb
163f0 65 72 20 6f 66 20 72 6f 77 73 20 61 6e 64 20 63 er of rows and c
16400 6f 6c 75 6d 6e 73 2e 20 20 42 75 74 0a 2a 2a 20 olumns. But.**
16410 74 68 65 73 65 20 6e 75 6d 62 65 72 73 20 61 72 these numbers ar
16420 65 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 e not part of th
16430 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 69 e result table i
16440 74 73 65 6c 66 2e 20 20 54 68 65 73 65 0a 2a 2a tself. These.**
16450 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f 62 74 numbers are obt
16460 61 69 6e 65 64 20 73 65 70 61 72 61 74 65 6c 79 ained separately
16470 2e 20 20 4c 65 74 20 4e 20 62 65 20 74 68 65 20 . Let N be the
16480 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a 2a number of rows.*
16490 2a 20 61 6e 64 20 4d 20 62 65 20 74 68 65 20 6e * and M be the n
164a0 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
164b0 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 6c 74 ..**.** A result
164c0 20 74 61 62 6c 65 20 69 73 20 61 6e 20 61 72 72 table is an arr
164d0 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 ay of pointers t
164e0 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 o zero-terminate
164f0 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 2e d UTF-8 strings.
16500 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 28 4e .** There are (N
16510 2b 31 29 2a 4d 20 65 6c 65 6d 65 6e 74 73 20 69 +1)*M elements i
16520 6e 20 74 68 65 20 61 72 72 61 79 2e 20 20 54 68 n the array. Th
16530 65 20 66 69 72 73 74 20 4d 20 70 6f 69 6e 74 65 e first M pointe
16540 72 73 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 7a rs point.** to z
16550 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 ero-terminated s
16560 74 72 69 6e 67 73 20 74 68 61 74 20 20 63 6f 6e trings that con
16570 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f tain the names o
16580 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 2e 0a 2a f the columns..*
16590 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 * The remaining
165a0 65 6e 74 72 69 65 73 20 61 6c 6c 20 70 6f 69 6e entries all poin
165b0 74 20 74 6f 20 71 75 65 72 79 20 72 65 73 75 6c t to query resul
165c0 74 73 2e 20 20 4e 55 4c 4c 20 76 61 6c 75 65 73 ts. NULL values
165d0 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 4e 55 result.** in NU
165e0 4c 4c 20 70 6f 69 6e 74 65 72 73 2e 20 20 41 6c LL pointers. Al
165f0 6c 20 6f 74 68 65 72 20 76 61 6c 75 65 73 20 61 l other values a
16600 72 65 20 69 6e 20 74 68 65 69 72 20 55 54 46 2d re in their UTF-
16610 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 8 zero-terminate
16620 64 0a 2a 2a 20 73 74 72 69 6e 67 20 72 65 70 72 d.** string repr
16630 65 73 65 6e 74 61 74 69 6f 6e 20 61 73 20 72 65 esentation as re
16640 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 turned by [sqlit
16650 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 e3_column_text()
16660 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 6c ]..**.** A resul
16670 74 20 74 61 62 6c 65 20 6d 69 67 68 74 20 63 6f t table might co
16680 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 6f 72 20 nsist of one or
16690 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f more memory allo
166a0 63 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49 74 20 69 cations..** It i
166b0 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 70 61 s not safe to pa
166c0 73 73 20 61 20 72 65 73 75 6c 74 20 74 61 62 6c ss a result tabl
166d0 65 20 64 69 72 65 63 74 6c 79 20 74 6f 20 5b 73 e directly to [s
166e0 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a qlite3_free()]..
166f0 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c ** A result tabl
16700 65 20 73 68 6f 75 6c 64 20 62 65 20 64 65 61 6c e should be deal
16710 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 5b 73 located using [s
16720 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c qlite3_free_tabl
16730 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 61 e()]..**.** As a
16740 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 74 68 65 n example of the
16750 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 66 6f result table fo
16760 72 6d 61 74 2c 20 73 75 70 70 6f 73 65 20 61 20 rmat, suppose a
16770 71 75 65 72 79 20 72 65 73 75 6c 74 0a 2a 2a 20 query result.**
16780 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a is as follows:.*
16790 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 *.** <blockquote
167a0 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20 ><pre>.**
167b0 20 4e 61 6d 65 20 20 20 20 20 20 20 20 7c 20 41 Name | A
167c0 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 2d 2d 2d ge.** ---
167d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
167e0 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 20 20 41 ----.** A
167f0 6c 69 63 65 20 20 20 20 20 20 20 7c 20 34 33 0a lice | 43.
16800 2a 2a 20 20 20 20 20 20 20 20 42 6f 62 20 20 20 ** Bob
16810 20 20 20 20 20 20 7c 20 32 38 0a 2a 2a 20 20 20 | 28.**
16820 20 20 20 20 20 43 69 6e 64 79 20 20 20 20 20 20 Cindy
16830 20 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 65 3e 3c | 21.** </pre><
16840 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a /blockquote>.**.
16850 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 77 6f ** There are two
16860 20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 29 20 61 column (M==2) a
16870 6e 64 20 74 68 72 65 65 20 72 6f 77 73 20 28 4e nd three rows (N
16880 3d 3d 33 29 2e 20 20 54 68 75 73 20 74 68 65 0a ==3). Thus the.
16890 2a 2a 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 ** result table
168a0 68 61 73 20 38 20 65 6e 74 72 69 65 73 2e 20 20 has 8 entries.
168b0 53 75 70 70 6f 73 65 20 74 68 65 20 72 65 73 75 Suppose the resu
168c0 6c 74 20 74 61 62 6c 65 20 69 73 20 73 74 6f 72 lt table is stor
168d0 65 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 72 72 61 ed.** in an arra
168e0 79 20 6e 61 6d 65 73 20 61 7a 52 65 73 75 6c 74 y names azResult
168f0 2e 20 20 54 68 65 6e 20 61 7a 52 65 73 75 6c 74 . Then azResult
16900 20 68 6f 6c 64 73 20 74 68 69 73 20 63 6f 6e 74 holds this cont
16910 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 ent:.**.** <bloc
16920 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 kquote><pre>.**
16930 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 azResult&
16940 23 39 31 3b 30 5d 20 3d 20 22 4e 61 6d 65 22 3b #91;0] = "Name";
16950 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 .** azRes
16960 75 6c 74 26 23 39 31 3b 31 5d 20 3d 20 22 41 67 ult[1] = "Ag
16970 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a e";.** az
16980 52 65 73 75 6c 74 26 23 39 31 3b 32 5d 20 3d 20 Result[2] =
16990 22 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 20 20 20 "Alice";.**
169a0 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b azResult[
169b0 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 20 20 20 3] = "43";.**
169c0 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 azResult	
169d0 31 3b 34 5d 20 3d 20 22 42 6f 62 22 3b 0a 2a 2a 1;4] = "Bob";.**
169e0 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 azResult
169f0 26 23 39 31 3b 35 5d 20 3d 20 22 32 38 22 3b 0a [5] = "28";.
16a00 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 ** azResu
16a10 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22 43 69 6e lt[6] = "Cin
16a20 64 79 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 dy";.** a
16a30 7a 52 65 73 75 6c 74 26 23 39 31 3b 37 5d 20 3d zResult[7] =
16a40 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e "21";.** </pre>
16a50 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a </blockquote>.**
16a60 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
16a70 67 65 74 5f 74 61 62 6c 65 28 29 20 66 75 6e 63 get_table() func
16a80 74 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 6f tion evaluates o
16a90 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 73 65 ne or more.** se
16aa0 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 61 74 65 micolon-separate
16ab0 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 d SQL statements
16ac0 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 in the zero-ter
16ad0 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 0a 2a 2a minated UTF-8.**
16ae0 20 73 74 72 69 6e 67 20 6f 66 20 69 74 73 20 32 string of its 2
16af0 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 49 nd parameter. I
16b00 74 20 72 65 74 75 72 6e 73 20 61 20 72 65 73 75 t returns a resu
16b10 6c 74 20 74 61 62 6c 65 20 74 6f 20 74 68 65 0a lt table to the.
16b20 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69 76 65 6e ** pointer given
16b30 20 69 6e 20 69 74 73 20 33 72 64 20 70 61 72 61 in its 3rd para
16b40 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 meter..**.** Aft
16b50 65 72 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 er the calling f
16b60 75 6e 63 74 69 6f 6e 20 68 61 73 20 66 69 6e 69 unction has fini
16b70 73 68 65 64 20 75 73 69 6e 67 20 74 68 65 20 72 shed using the r
16b80 65 73 75 6c 74 2c 20 69 74 20 73 68 6f 75 6c 64 esult, it should
16b90 0a 2a 2a 20 70 61 73 73 20 74 68 65 20 70 6f 69 .** pass the poi
16ba0 6e 74 65 72 20 74 6f 20 74 68 65 20 72 65 73 75 nter to the resu
16bb0 6c 74 20 74 61 62 6c 65 20 74 6f 20 73 71 6c 69 lt table to sqli
16bc0 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 te3_free_table()
16bd0 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 in order to.**
16be0 72 65 6c 65 61 73 65 20 74 68 65 20 6d 65 6d 6f release the memo
16bf0 72 79 20 74 68 61 74 20 77 61 73 20 6d 61 6c 6c ry that was mall
16c00 6f 63 65 64 2e 20 20 42 65 63 61 75 73 65 20 6f oced. Because o
16c10 66 20 74 68 65 20 77 61 79 20 74 68 65 0a 2a 2a f the way the.**
16c20 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
16c30 28 29 5d 20 68 61 70 70 65 6e 73 20 77 69 74 68 ()] happens with
16c40 69 6e 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 in sqlite3_get_t
16c50 61 62 6c 65 28 29 2c 20 74 68 65 20 63 61 6c 6c able(), the call
16c60 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 ing.** function
16c70 6d 75 73 74 20 6e 6f 74 20 74 72 79 20 74 6f 20 must not try to
16c80 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33 5f 66 72 call [sqlite3_fr
16c90 65 65 28 29 5d 20 64 69 72 65 63 74 6c 79 2e 20 ee()] directly.
16ca0 20 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 Only.** [sqlite
16cb0 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 5d 20 3_free_table()]
16cc0 69 73 20 61 62 6c 65 20 74 6f 20 72 65 6c 65 61 is able to relea
16cd0 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 70 72 se the memory pr
16ce0 6f 70 65 72 6c 79 20 61 6e 64 20 73 61 66 65 6c operly and safel
16cf0 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c y..**.** The sql
16d00 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 ite3_get_table()
16d10 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 69 6d interface is im
16d20 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 77 plemented as a w
16d30 72 61 70 70 65 72 20 61 72 6f 75 6e 64 0a 2a 2a rapper around.**
16d40 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 [sqlite3_exec()
16d50 5d 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f ]. The sqlite3_
16d60 67 65 74 5f 74 61 62 6c 65 28 29 20 72 6f 75 74 get_table() rout
16d70 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 ine does not hav
16d80 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f 20 61 e access.** to a
16d90 6e 79 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 ny internal data
16da0 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 53 structures of S
16db0 51 4c 69 74 65 2e 20 20 49 74 20 75 73 65 73 20 QLite. It uses
16dc0 6f 6e 6c 79 20 74 68 65 20 70 75 62 6c 69 63 0a only the public.
16dd0 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 64 65 66 ** interface def
16de0 69 6e 65 64 20 68 65 72 65 2e 20 20 41 73 20 61 ined here. As a
16df0 20 63 6f 6e 73 65 71 75 65 6e 63 65 2c 20 65 72 consequence, er
16e00 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75 72 20 rors that occur
16e10 69 6e 20 74 68 65 0a 2a 2a 20 77 72 61 70 70 65 in the.** wrappe
16e20 72 20 6c 61 79 65 72 20 6f 75 74 73 69 64 65 20 r layer outside
16e30 6f 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 of the internal
16e40 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d [sqlite3_exec()]
16e50 20 63 61 6c 6c 20 61 72 65 20 6e 6f 74 0a 2a 2a call are not.**
16e60 20 72 65 66 6c 65 63 74 65 64 20 69 6e 20 73 75 reflected in su
16e70 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 bsequent calls t
16e80 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f o [sqlite3_errco
16e90 64 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 de()] or [sqlite
16ea0 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 0a 3_errmsg()]..**.
16eb0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
16ec0 0a 2a 2a 20 5b 48 31 32 33 37 31 5d 20 5b 48 31 .** [H12371] [H1
16ed0 32 33 37 33 5d 20 5b 48 31 32 33 37 34 5d 20 5b 2373] [H12374] [
16ee0 48 31 32 33 37 36 5d 20 5b 48 31 32 33 37 39 5d H12376] [H12379]
16ef0 20 5b 48 31 32 33 38 32 5d 0a 2a 2f 0a 53 51 4c [H12382].*/.SQL
16f00 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
16f10 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 0a 20 te3_get_table(.
16f20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 sqlite3 *db,
16f30 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 /* An ope
16f40 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 n database */.
16f50 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c const char *zSql
16f60 2c 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20 , /* SQL to
16f70 62 65 20 65 76 61 6c 75 61 74 65 64 20 2a 2f 0a be evaluated */.
16f80 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73 char ***pazRes
16f90 75 6c 74 2c 20 20 20 20 2f 2a 20 52 65 73 75 6c ult, /* Resul
16fa0 74 73 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 ts of the query
16fb0 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 6f 77 2c */. int *pnRow,
16fc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
16fd0 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 72 mber of result r
16fe0 6f 77 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 ows written here
16ff0 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c */. int *pnCol
17000 75 6d 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e umn, /* N
17010 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 umber of result
17020 63 6f 6c 75 6d 6e 73 20 77 72 69 74 74 65 6e 20 columns written
17030 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a here */. char *
17040 2a 70 7a 45 72 72 6d 73 67 20 20 20 20 20 20 20 *pzErrmsg
17050 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72 69 /* Error msg wri
17060 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b 0a tten here */.);.
17070 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
17080 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 sqlite3_free_tab
17090 6c 65 28 63 68 61 72 20 2a 2a 72 65 73 75 6c 74 le(char **result
170a0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
170b0 45 46 3a 20 46 6f 72 6d 61 74 74 65 64 20 53 74 EF: Formatted St
170c0 72 69 6e 67 20 50 72 69 6e 74 69 6e 67 20 46 75 ring Printing Fu
170d0 6e 63 74 69 6f 6e 73 20 7b 48 31 37 34 30 30 7d nctions {H17400}
170e0 20 3c 53 37 30 30 30 30 3e 3c 53 32 30 30 30 30 <S70000><S20000
170f0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f >.**.** These ro
17100 75 74 69 6e 65 73 20 61 72 65 20 77 6f 72 6b 2d utines are work-
17110 61 6c 69 6b 65 73 20 6f 66 20 74 68 65 20 22 70 alikes of the "p
17120 72 69 6e 74 66 28 29 22 20 66 61 6d 69 6c 79 20 rintf()" family
17130 6f 66 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 of functions.**
17140 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 from the standar
17150 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a d C library..**.
17160 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d ** The sqlite3_m
17170 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73 71 6c printf() and sql
17180 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 29 20 ite3_vmprintf()
17190 72 6f 75 74 69 6e 65 73 20 77 72 69 74 65 20 74 routines write t
171a0 68 65 69 72 0a 2a 2a 20 72 65 73 75 6c 74 73 20 heir.** results
171b0 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 into memory obta
171c0 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 ined from [sqlit
171d0 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a e3_malloc()]..**
171e0 20 54 68 65 20 73 74 72 69 6e 67 73 20 72 65 74 The strings ret
171f0 75 72 6e 65 64 20 62 79 20 74 68 65 73 65 20 74 urned by these t
17200 77 6f 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 wo routines shou
17210 6c 64 20 62 65 0a 2a 2a 20 72 65 6c 65 61 73 65 ld be.** release
17220 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 66 72 d by [sqlite3_fr
17230 65 65 28 29 5d 2e 20 20 42 6f 74 68 20 72 6f 75 ee()]. Both rou
17240 74 69 6e 65 73 20 72 65 74 75 72 6e 20 61 0a 2a tines return a.*
17250 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 * NULL pointer i
17260 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f f [sqlite3_mallo
17270 63 28 29 5d 20 69 73 20 75 6e 61 62 6c 65 20 74 c()] is unable t
17280 6f 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f 75 67 o allocate enoug
17290 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 74 6f 20 68 h.** memory to h
172a0 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 69 6e old the resultin
172b0 67 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 g string..**.**
172c0 49 6e 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 In sqlite3_snpri
172d0 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 20 69 73 ntf() routine is
172e0 20 73 69 6d 69 6c 61 72 20 74 6f 20 22 73 6e 70 similar to "snp
172f0 72 69 6e 74 66 28 29 22 20 66 72 6f 6d 0a 2a 2a rintf()" from.**
17300 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 the standard C
17310 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 72 65 library. The re
17320 73 75 6c 74 20 69 73 20 77 72 69 74 74 65 6e 20 sult is written
17330 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 62 75 66 66 into the.** buff
17340 65 72 20 73 75 70 70 6c 69 65 64 20 61 73 20 74 er supplied as t
17350 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
17360 74 65 72 20 77 68 6f 73 65 20 73 69 7a 65 20 69 ter whose size i
17370 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68 s given by.** th
17380 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
17390 72 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 r. Note that the
173a0 20 6f 72 64 65 72 20 6f 66 20 74 68 65 0a 2a 2a order of the.**
173b0 20 66 69 72 73 74 20 74 77 6f 20 70 61 72 61 6d first two param
173c0 65 74 65 72 73 20 69 73 20 72 65 76 65 72 73 65 eters is reverse
173d0 64 20 66 72 6f 6d 20 73 6e 70 72 69 6e 74 66 28 d from snprintf(
173e0 29 2e 20 20 54 68 69 73 20 69 73 20 61 6e 0a 2a ). This is an.*
173f0 2a 20 68 69 73 74 6f 72 69 63 61 6c 20 61 63 63 * historical acc
17400 69 64 65 6e 74 20 74 68 61 74 20 63 61 6e 6e 6f ident that canno
17410 74 20 62 65 20 66 69 78 65 64 20 77 69 74 68 6f t be fixed witho
17420 75 74 20 62 72 65 61 6b 69 6e 67 0a 2a 2a 20 62 ut breaking.** b
17430 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 ackwards compati
17440 62 69 6c 69 74 79 2e 20 20 4e 6f 74 65 20 61 6c bility. Note al
17450 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f so that sqlite3_
17460 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 72 65 snprintf().** re
17470 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
17480 74 6f 20 69 74 73 20 62 75 66 66 65 72 20 69 6e to its buffer in
17490 73 74 65 61 64 20 6f 66 20 74 68 65 20 6e 75 6d stead of the num
174a0 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72 61 63 ber of.** charac
174b0 74 65 72 73 20 61 63 74 75 61 6c 6c 79 20 77 72 ters actually wr
174c0 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 62 itten into the b
174d0 75 66 66 65 72 2e 20 20 57 65 20 61 64 6d 69 74 uffer. We admit
174e0 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6e 75 6d that.** the num
174f0 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 ber of character
17500 73 20 77 72 69 74 74 65 6e 20 77 6f 75 6c 64 20 s written would
17510 62 65 20 61 20 6d 6f 72 65 20 75 73 65 66 75 6c be a more useful
17520 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 return.** value
17530 20 62 75 74 20 77 65 20 63 61 6e 6e 6f 74 20 63 but we cannot c
17540 68 61 6e 67 65 20 74 68 65 20 69 6d 70 6c 65 6d hange the implem
17550 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 entation of sqli
17560 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a te3_snprintf().*
17570 2a 20 6e 6f 77 20 77 69 74 68 6f 75 74 20 62 72 * now without br
17580 65 61 6b 69 6e 67 20 63 6f 6d 70 61 74 69 62 69 eaking compatibi
17590 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 6c lity..**.** As l
175a0 6f 6e 67 20 61 73 20 74 68 65 20 62 75 66 66 65 ong as the buffe
175b0 72 20 73 69 7a 65 20 69 73 20 67 72 65 61 74 65 r size is greate
175c0 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 73 71 6c r than zero, sql
175d0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a ite3_snprintf().
175e0 2a 2a 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 ** guarantees th
175f0 61 74 20 74 68 65 20 62 75 66 66 65 72 20 69 73 at the buffer is
17600 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 65 72 always zero-ter
17610 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 20 66 69 minated. The fi
17620 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 rst.** parameter
17630 20 22 6e 22 20 69 73 20 74 68 65 20 74 6f 74 61 "n" is the tota
17640 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 l size of the bu
17650 66 66 65 72 2c 20 69 6e 63 6c 75 64 69 6e 67 20 ffer, including
17660 73 70 61 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 space for.** the
17670 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 zero terminator
17680 2e 20 20 53 6f 20 74 68 65 20 6c 6f 6e 67 65 73 . So the longes
17690 74 20 73 74 72 69 6e 67 20 74 68 61 74 20 63 61 t string that ca
176a0 6e 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 0a n be completely.
176b0 2a 2a 20 77 72 69 74 74 65 6e 20 77 69 6c 6c 20 ** written will
176c0 62 65 20 6e 2d 31 20 63 68 61 72 61 63 74 65 72 be n-1 character
176d0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 s..**.** These r
176e0 6f 75 74 69 6e 65 73 20 61 6c 6c 20 69 6d 70 6c outines all impl
176f0 65 6d 65 6e 74 20 73 6f 6d 65 20 61 64 64 69 74 ement some addit
17700 69 6f 6e 61 6c 20 66 6f 72 6d 61 74 74 69 6e 67 ional formatting
17710 0a 2a 2a 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 .** options that
17720 20 61 72 65 20 75 73 65 66 75 6c 20 66 6f 72 20 are useful for
17730 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 53 51 4c constructing SQL
17740 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 statements..**
17750 41 6c 6c 20 6f 66 20 74 68 65 20 75 73 75 61 6c All of the usual
17760 20 70 72 69 6e 74 66 28 29 20 66 6f 72 6d 61 74 printf() format
17770 74 69 6e 67 20 6f 70 74 69 6f 6e 73 20 61 70 70 ting options app
17780 6c 79 2e 20 20 49 6e 20 61 64 64 69 74 69 6f 6e ly. In addition
17790 2c 20 74 68 65 72 65 0a 2a 2a 20 69 73 20 61 72 , there.** is ar
177a0 65 20 22 25 71 22 2c 20 22 25 51 22 2c 20 61 6e e "%q", "%Q", an
177b0 64 20 22 25 7a 22 20 6f 70 74 69 6f 6e 73 2e 0a d "%z" options..
177c0 2a 2a 0a 2a 2a 20 54 68 65 20 25 71 20 6f 70 74 **.** The %q opt
177d0 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 ion works like %
177e0 73 20 69 6e 20 74 68 61 74 20 69 74 20 73 75 62 s in that it sub
177f0 73 74 69 74 75 74 65 73 20 61 20 6e 75 6c 6c 2d stitutes a null-
17800 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73 74 terminated.** st
17810 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 61 72 ring from the ar
17820 67 75 6d 65 6e 74 20 6c 69 73 74 2e 20 20 42 75 gument list. Bu
17830 74 20 25 71 20 61 6c 73 6f 20 64 6f 75 62 6c 65 t %q also double
17840 73 20 65 76 65 72 79 20 27 5c 27 27 20 63 68 61 s every '\'' cha
17850 72 61 63 74 65 72 2e 0a 2a 2a 20 25 71 20 69 73 racter..** %q is
17860 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 73 designed for us
17870 65 20 69 6e 73 69 64 65 20 61 20 73 74 72 69 6e e inside a strin
17880 67 20 6c 69 74 65 72 61 6c 2e 20 20 42 79 20 64 g literal. By d
17890 6f 75 62 6c 69 6e 67 20 65 61 63 68 20 27 5c 27 oubling each '\'
178a0 27 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20 69 '.** character i
178b0 74 20 65 73 63 61 70 65 73 20 74 68 61 74 20 63 t escapes that c
178c0 68 61 72 61 63 74 65 72 20 61 6e 64 20 61 6c 6c haracter and all
178d0 6f 77 73 20 69 74 20 74 6f 20 62 65 20 69 6e 73 ows it to be ins
178e0 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 erted into.** th
178f0 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 e string..**.**
17900 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 73 73 For example, ass
17910 75 6d 65 20 74 68 65 20 73 74 72 69 6e 67 20 76 ume the string v
17920 61 72 69 61 62 6c 65 20 7a 54 65 78 74 20 63 6f ariable zText co
17930 6e 74 61 69 6e 73 20 74 65 78 74 20 61 73 20 66 ntains text as f
17940 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 ollows:.**.** <b
17950 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a lockquote><pre>.
17960 2a 2a 20 20 63 68 61 72 20 2a 7a 54 65 78 74 20 ** char *zText
17970 3d 20 22 49 74 27 73 20 61 20 68 61 70 70 79 20 = "It's a happy
17980 64 61 79 21 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e day!";.** </pre>
17990 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a </blockquote>.**
179a0 0a 2a 2a 20 4f 6e 65 20 63 61 6e 20 75 73 65 20 .** One can use
179b0 74 68 69 73 20 74 65 78 74 20 69 6e 20 61 6e 20 this text in an
179c0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 73 SQL statement as
179d0 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
179e0 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 <blockquote><pre
179f0 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53 51 4c >.** char *zSQL
17a00 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e = sqlite3_mprin
17a10 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 tf("INSERT INTO
17a20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 27 25 71 table VALUES('%q
17a30 27 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a 2a 20 ')", zText);.**
17a40 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 sqlite3_exec(db
17a50 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 30 29 , zSQL, 0, 0, 0)
17a60 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 72 ;.** sqlite3_fr
17a70 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 ee(zSQL);.** </p
17a80 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e re></blockquote>
17a90 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 .**.** Because t
17aa0 68 65 20 25 71 20 66 6f 72 6d 61 74 20 73 74 72 he %q format str
17ab0 69 6e 67 20 69 73 20 75 73 65 64 2c 20 74 68 65 ing is used, the
17ac0 20 27 5c 27 27 20 63 68 61 72 61 63 74 65 72 20 '\'' character
17ad0 69 6e 20 7a 54 65 78 74 0a 2a 2a 20 69 73 20 65 in zText.** is e
17ae0 73 63 61 70 65 64 20 61 6e 64 20 74 68 65 20 53 scaped and the S
17af0 51 4c 20 67 65 6e 65 72 61 74 65 64 20 69 73 20 QL generated is
17b00 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
17b10 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 * <blockquote><p
17b20 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 54 20 49 re>.** INSERT I
17b30 4e 54 4f 20 74 61 62 6c 65 31 20 56 41 4c 55 45 NTO table1 VALUE
17b40 53 28 27 49 74 27 27 73 20 61 20 68 61 70 70 79 S('It''s a happy
17b50 20 64 61 79 21 27 29 0a 2a 2a 20 3c 2f 70 72 65 day!').** </pre
17b60 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a ></blockquote>.*
17b70 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 6f 72 *.** This is cor
17b80 72 65 63 74 2e 20 20 48 61 64 20 77 65 20 75 73 rect. Had we us
17b90 65 64 20 25 73 20 69 6e 73 74 65 61 64 20 6f 66 ed %s instead of
17ba0 20 25 71 2c 20 74 68 65 20 67 65 6e 65 72 61 74 %q, the generat
17bb0 65 64 20 53 51 4c 0a 2a 2a 20 77 6f 75 6c 64 20 ed SQL.** would
17bc0 68 61 76 65 20 6c 6f 6f 6b 65 64 20 6c 69 6b 65 have looked like
17bd0 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c this:.**.** <bl
17be0 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a ockquote><pre>.*
17bf0 2a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 * INSERT INTO t
17c00 61 62 6c 65 31 20 56 41 4c 55 45 53 28 27 49 74 able1 VALUES('It
17c10 27 73 20 61 20 68 61 70 70 79 20 64 61 79 21 27 's a happy day!'
17c20 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c );.** </pre></bl
17c30 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 ockquote>.**.**
17c40 54 68 69 73 20 73 65 63 6f 6e 64 20 65 78 61 6d This second exam
17c50 70 6c 65 20 69 73 20 61 6e 20 53 51 4c 20 73 79 ple is an SQL sy
17c60 6e 74 61 78 20 65 72 72 6f 72 2e 20 20 41 73 20 ntax error. As
17c70 61 20 67 65 6e 65 72 61 6c 20 72 75 6c 65 20 79 a general rule y
17c80 6f 75 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 6c 77 ou should.** alw
17c90 61 79 73 20 75 73 65 20 25 71 20 69 6e 73 74 65 ays use %q inste
17ca0 61 64 20 6f 66 20 25 73 20 77 68 65 6e 20 69 6e ad of %s when in
17cb0 73 65 72 74 69 6e 67 20 74 65 78 74 20 69 6e 74 serting text int
17cc0 6f 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 o a string liter
17cd0 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 25 51 al..**.** The %Q
17ce0 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 option works li
17cf0 6b 65 20 25 71 20 65 78 63 65 70 74 20 69 74 20 ke %q except it
17d00 61 6c 73 6f 20 61 64 64 73 20 73 69 6e 67 6c 65 also adds single
17d10 20 71 75 6f 74 65 73 20 61 72 6f 75 6e 64 0a 2a quotes around.*
17d20 2a 20 74 68 65 20 6f 75 74 73 69 64 65 20 6f 66 * the outside of
17d30 20 74 68 65 20 74 6f 74 61 6c 20 73 74 72 69 6e the total strin
17d40 67 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 g. Additionally
17d50 2c 20 69 66 20 74 68 65 20 70 61 72 61 6d 65 74 , if the paramet
17d60 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72 67 er in the.** arg
17d70 75 6d 65 6e 74 20 6c 69 73 74 20 69 73 20 61 20 ument list is a
17d80 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 25 51 NULL pointer, %Q
17d90 20 73 75 62 73 74 69 74 75 74 65 73 20 74 68 65 substitutes the
17da0 20 74 65 78 74 20 22 4e 55 4c 4c 22 20 28 77 69 text "NULL" (wi
17db0 74 68 6f 75 74 0a 2a 2a 20 73 69 6e 67 6c 65 20 thout.** single
17dc0 71 75 6f 74 65 73 29 20 69 6e 20 70 6c 61 63 65 quotes) in place
17dd0 20 6f 66 20 74 68 65 20 25 51 20 6f 70 74 69 6f of the %Q optio
17de0 6e 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d n. So, for exam
17df0 70 6c 65 2c 20 6f 6e 65 20 63 6f 75 6c 64 20 73 ple, one could s
17e00 61 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b ay:.**.** <block
17e10 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 quote><pre>.**
17e20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c char *zSQL = sql
17e30 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e ite3_mprintf("IN
17e40 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20 SERT INTO table
17e50 56 41 4c 55 45 53 28 25 51 29 22 2c 20 7a 54 65 VALUES(%Q)", zTe
17e60 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 xt);.** sqlite3
17e70 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20 _exec(db, zSQL,
17e80 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 71 0, 0, 0);.** sq
17e90 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c 29 lite3_free(zSQL)
17ea0 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f ;.** </pre></blo
17eb0 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 ckquote>.**.** T
17ec0 68 65 20 63 6f 64 65 20 61 62 6f 76 65 20 77 69 he code above wi
17ed0 6c 6c 20 72 65 6e 64 65 72 20 61 20 63 6f 72 72 ll render a corr
17ee0 65 63 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e ect SQL statemen
17ef0 74 20 69 6e 20 74 68 65 20 7a 53 51 4c 0a 2a 2a t in the zSQL.**
17f00 20 76 61 72 69 61 62 6c 65 20 65 76 65 6e 20 69 variable even i
17f10 66 20 74 68 65 20 7a 54 65 78 74 20 76 61 72 69 f the zText vari
17f20 61 62 6c 65 20 69 73 20 61 20 4e 55 4c 4c 20 70 able is a NULL p
17f30 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 ointer..**.** Th
17f40 65 20 22 25 7a 22 20 66 6f 72 6d 61 74 74 69 6e e "%z" formattin
17f50 67 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 65 g option works e
17f60 78 61 63 74 6c 79 20 6c 69 6b 65 20 22 25 73 22 xactly like "%s"
17f70 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 61 64 64 with the.** add
17f80 69 74 69 6f 6e 20 74 68 61 74 20 61 66 74 65 72 ition that after
17f90 20 74 68 65 20 73 74 72 69 6e 67 20 68 61 73 20 the string has
17fa0 62 65 65 6e 20 72 65 61 64 20 61 6e 64 20 63 6f been read and co
17fb0 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 pied into.** the
17fc0 20 72 65 73 75 6c 74 2c 20 5b 73 71 6c 69 74 65 result, [sqlite
17fd0 33 5f 66 72 65 65 28 29 5d 20 69 73 20 63 61 6c 3_free()] is cal
17fe0 6c 65 64 20 6f 6e 20 74 68 65 20 69 6e 70 75 74 led on the input
17ff0 20 73 74 72 69 6e 67 2e 20 7b 45 4e 44 7d 0a 2a string. {END}.*
18000 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
18010 73 3a 0a 2a 2a 20 5b 48 31 37 34 30 33 5d 20 5b s:.** [H17403] [
18020 48 31 37 34 30 36 5d 20 5b 48 31 37 34 30 37 5d H17406] [H17407]
18030 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
18040 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d 70 72 har *sqlite3_mpr
18050 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 2a intf(const char*
18060 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 41 50 ,...);.SQLITE_AP
18070 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f I char *sqlite3_
18080 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 vmprintf(const c
18090 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a har*, va_list);.
180a0 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 SQLITE_API char
180b0 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 *sqlite3_snprint
180c0 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f 6e 73 f(int,char*,cons
180d0 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 0a t char*, ...);..
180e0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
180f0 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 6f Memory Allocatio
18100 6e 20 53 75 62 73 79 73 74 65 6d 20 7b 48 31 37 n Subsystem {H17
18110 33 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 300} <S20000>.**
18120 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63 .** The SQLite c
18130 6f 72 65 20 20 75 73 65 73 20 74 68 65 73 65 20 ore uses these
18140 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 20 66 three routines f
18150 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 6f 77 or all of its ow
18160 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 6d 65 n.** internal me
18170 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
18180 6e 65 65 64 73 2e 20 22 43 6f 72 65 22 20 69 6e needs. "Core" in
18190 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 the previous se
181a0 6e 74 65 6e 63 65 0a 2a 2a 20 64 6f 65 73 20 6e ntence.** does n
181b0 6f 74 20 69 6e 63 6c 75 64 65 20 6f 70 65 72 61 ot include opera
181c0 74 69 6e 67 2d 73 79 73 74 65 6d 20 73 70 65 63 ting-system spec
181d0 69 66 69 63 20 56 46 53 20 69 6d 70 6c 65 6d 65 ific VFS impleme
181e0 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a ntation. The.**
181f0 20 57 69 6e 64 6f 77 73 20 56 46 53 20 75 73 65 Windows VFS use
18200 73 20 6e 61 74 69 76 65 20 6d 61 6c 6c 6f 63 28 s native malloc(
18210 29 20 61 6e 64 20 66 72 65 65 28 29 20 66 6f 72 ) and free() for
18220 20 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f 6e 73 some operations
18230 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
18240 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 6f 75 te3_malloc() rou
18250 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70 tine returns a p
18260 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f 63 ointer to a bloc
18270 6b 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 20 61 k.** of memory a
18280 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 t least N bytes
18290 69 6e 20 6c 65 6e 67 74 68 2c 20 77 68 65 72 65 in length, where
182a0 20 4e 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 N is the parame
182b0 74 65 72 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74 ter..** If sqlit
182c0 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 20 75 e3_malloc() is u
182d0 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 6e 20 nable to obtain
182e0 73 75 66 66 69 63 69 65 6e 74 20 66 72 65 65 0a sufficient free.
182f0 2a 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 20 72 65 ** memory, it re
18300 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 turns a NULL poi
18310 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 70 61 nter. If the pa
18320 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a 2a 2a 20 rameter N to.**
18330 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
18340 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 is zero or nega
18350 74 69 76 65 20 74 68 65 6e 20 73 71 6c 69 74 65 tive then sqlite
18360 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 74 75 72 3_malloc() retur
18370 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 ns.** a NULL poi
18380 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c nter..**.** Call
18390 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ing sqlite3_free
183a0 28 29 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 () with a pointe
183b0 72 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 74 r previously ret
183c0 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 urned.** by sqli
183d0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 te3_malloc() or
183e0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
183f0 29 20 72 65 6c 65 61 73 65 73 20 74 68 61 74 20 ) releases that
18400 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 74 68 61 memory so.** tha
18410 74 20 69 74 20 6d 69 67 68 74 20 62 65 20 72 65 t it might be re
18420 75 73 65 64 2e 20 20 54 68 65 20 73 71 6c 69 74 used. The sqlit
18430 65 33 5f 66 72 65 65 28 29 20 72 6f 75 74 69 6e e3_free() routin
18440 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 20 e is.** a no-op
18450 69 66 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 if is called wit
18460 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 h a NULL pointer
18470 2e 20 20 50 61 73 73 69 6e 67 20 61 20 4e 55 4c . Passing a NUL
18480 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 L pointer.** to
18490 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 sqlite3_free() i
184a0 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 41 66 74 s harmless. Aft
184b0 65 72 20 62 65 69 6e 67 20 66 72 65 65 64 2c 20 er being freed,
184c0 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f 75 6c 64 memory.** should
184d0 20 6e 65 69 74 68 65 72 20 62 65 20 72 65 61 64 neither be read
184e0 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e 20 20 45 nor written. E
184f0 76 65 6e 20 72 65 61 64 69 6e 67 20 70 72 65 76 ven reading prev
18500 69 6f 75 73 6c 79 20 66 72 65 65 64 0a 2a 2a 20 iously freed.**
18510 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 72 65 73 memory might res
18520 75 6c 74 20 69 6e 20 61 20 73 65 67 6d 65 6e 74 ult in a segment
18530 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f 72 20 6f ation fault or o
18540 74 68 65 72 20 73 65 76 65 72 65 20 65 72 72 6f ther severe erro
18550 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 6f 72 r..** Memory cor
18560 72 75 70 74 69 6f 6e 2c 20 61 20 73 65 67 6d 65 ruption, a segme
18570 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 2c 20 6f ntation fault, o
18580 72 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 r other severe e
18590 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 20 72 65 rror.** might re
185a0 73 75 6c 74 20 69 66 20 73 71 6c 69 74 65 33 5f sult if sqlite3_
185b0 66 72 65 65 28 29 20 69 73 20 63 61 6c 6c 65 64 free() is called
185c0 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e 55 4c 4c with a non-NULL
185d0 20 70 6f 69 6e 74 65 72 20 74 68 61 74 0a 2a 2a pointer that.**
185e0 20 77 61 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65 was not obtaine
185f0 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d d from sqlite3_m
18600 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 alloc() or sqlit
18610 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a e3_realloc()..**
18620 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
18630 72 65 61 6c 6c 6f 63 28 29 20 69 6e 74 65 72 66 realloc() interf
18640 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 ace attempts to
18650 72 65 73 69 7a 65 20 61 0a 2a 2a 20 70 72 69 6f resize a.** prio
18660 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 r memory allocat
18670 69 6f 6e 20 74 6f 20 62 65 20 61 74 20 6c 65 61 ion to be at lea
18680 73 74 20 4e 20 62 79 74 65 73 2c 20 77 68 65 72 st N bytes, wher
18690 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 73 65 e N is the.** se
186a0 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 cond parameter.
186b0 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f The memory allo
186c0 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 72 65 73 cation to be res
186d0 69 7a 65 64 20 69 73 20 74 68 65 20 66 69 72 73 ized is the firs
186e0 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2e 20 t.** parameter.
186f0 20 49 66 20 74 68 65 20 66 69 72 73 74 20 70 61 If the first pa
18700 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
18710 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20 e3_realloc().**
18720 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 is a NULL pointe
18730 72 20 74 68 65 6e 20 69 74 73 20 62 65 68 61 76 r then its behav
18740 69 6f 72 20 69 73 20 69 64 65 6e 74 69 63 61 6c ior is identical
18750 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 to calling.** s
18760 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 4e 29 qlite3_malloc(N)
18770 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 where N is the
18780 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
18790 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c to sqlite3_real
187a0 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 74 68 65 loc()..** If the
187b0 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
187c0 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 r to sqlite3_rea
187d0 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f 20 6f lloc() is zero o
187e0 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 74 68 r.** negative th
187f0 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 en the behavior
18800 69 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 is exactly the s
18810 61 6d 65 20 61 73 20 63 61 6c 6c 69 6e 67 0a 2a ame as calling.*
18820 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 50 * sqlite3_free(P
18830 29 20 77 68 65 72 65 20 50 20 69 73 20 74 68 65 ) where P is the
18840 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 first parameter
18850 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c to sqlite3_real
18860 6c 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c 69 74 65 loc()..** sqlite
18870 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 74 75 3_realloc() retu
18880 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f rns a pointer to
18890 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
188a0 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74 20 6c 65 tion.** of at le
188b0 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e 20 73 ast N bytes in s
188c0 69 7a 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20 73 ize or NULL if s
188d0 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72 79 ufficient memory
188e0 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e is unavailable.
188f0 0a 2a 2a 20 49 66 20 4d 20 69 73 20 74 68 65 20 .** If M is the
18900 73 69 7a 65 20 6f 66 20 74 68 65 20 70 72 69 6f size of the prio
18910 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 74 68 r allocation, th
18920 65 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62 79 74 65 en min(N,M) byte
18930 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 72 69 6f s.** of the prio
18940 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 72 65 r allocation are
18950 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 copied into the
18960 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 62 75 beginning of bu
18970 66 66 65 72 20 72 65 74 75 72 6e 65 64 0a 2a 2a ffer returned.**
18980 20 62 79 20 73 71 6c 69 74 65 33 5f 72 65 61 6c by sqlite3_real
18990 6c 6f 63 28 29 20 61 6e 64 20 74 68 65 20 70 72 loc() and the pr
189a0 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 ior allocation i
189b0 73 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66 20 73 s freed..** If s
189c0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 qlite3_realloc()
189d0 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2c 20 74 returns NULL, t
189e0 68 65 6e 20 74 68 65 20 70 72 69 6f 72 20 61 6c hen the prior al
189f0 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e location.** is n
18a00 6f 74 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 ot freed..**.**
18a10 54 68 65 20 6d 65 6d 6f 72 79 20 72 65 74 75 72 The memory retur
18a20 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d ned by sqlite3_m
18a30 61 6c 6c 6f 63 28 29 20 61 6e 64 20 73 71 6c 69 alloc() and sqli
18a40 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a te3_realloc().**
18a50 20 69 73 20 61 6c 77 61 79 73 20 61 6c 69 67 6e is always align
18a60 65 64 20 74 6f 20 61 74 20 6c 65 61 73 74 20 61 ed to at least a
18a70 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64 61 72 n 8 byte boundar
18a80 79 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 y. {END}.**.** T
18a90 68 65 20 64 65 66 61 75 6c 74 20 69 6d 70 6c 65 he default imple
18aa0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
18ab0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
18ac0 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 75 73 65 on subsystem use
18ad0 73 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 28 s.** the malloc(
18ae0 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20 61 6e 64 ), realloc() and
18af0 20 66 72 65 65 28 29 20 70 72 6f 76 69 64 65 64 free() provided
18b00 20 62 79 20 74 68 65 20 73 74 61 6e 64 61 72 64 by the standard
18b10 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 20 7b C library..** {
18b20 48 31 37 33 38 32 7d 20 48 6f 77 65 76 65 72 2c H17382} However,
18b30 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f if SQLite is co
18b40 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a mpiled with the.
18b50 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 ** SQLITE_MEMORY
18b60 5f 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c 2f 69 3e _SIZE=<i>NNN</i>
18b70 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 C preprocessor
18b80 6d 61 63 72 6f 20 28 77 68 65 72 65 20 3c 69 3e macro (where <i>
18b90 4e 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73 20 61 6e NNN</i>.** is an
18ba0 20 69 6e 74 65 67 65 72 29 2c 20 74 68 65 6e 20 integer), then
18bb0 53 51 4c 69 74 65 20 63 72 65 61 74 65 20 61 20 SQLite create a
18bc0 73 74 61 74 69 63 20 61 72 72 61 79 20 6f 66 20 static array of
18bd0 61 74 20 6c 65 61 73 74 0a 2a 2a 20 3c 69 3e 4e at least.** <i>N
18be0 4e 4e 3c 2f 69 3e 20 62 79 74 65 73 20 69 6e 20 NN</i> bytes in
18bf0 73 69 7a 65 20 61 6e 64 20 75 73 65 73 20 74 68 size and uses th
18c00 61 74 20 61 72 72 61 79 20 66 6f 72 20 61 6c 6c at array for all
18c10 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 63 0a of its dynamic.
18c20 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ** memory alloca
18c30 74 69 6f 6e 20 6e 65 65 64 73 2e 20 7b 45 4e 44 tion needs. {END
18c40 7d 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 6d 65 } Additional me
18c50 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 6f mory allocator o
18c60 70 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79 20 62 65 ptions.** may be
18c70 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 added in future
18c80 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a releases..**.**
18c90 20 49 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69 In SQLite versi
18ca0 6f 6e 20 33 2e 35 2e 30 20 61 6e 64 20 33 2e 35 on 3.5.0 and 3.5
18cb0 2e 31 2c 20 69 74 20 77 61 73 20 70 6f 73 73 69 .1, it was possi
18cc0 62 6c 65 20 74 6f 20 64 65 66 69 6e 65 0a 2a 2a ble to define.**
18cd0 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 the SQLITE_OMIT
18ce0 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 41 54 49 _MEMORY_ALLOCATI
18cf0 4f 4e 20 77 68 69 63 68 20 77 6f 75 6c 64 20 63 ON which would c
18d00 61 75 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 ause the built-i
18d10 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 n.** implementat
18d20 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 72 6f 75 ion of these rou
18d30 74 69 6e 65 73 20 74 6f 20 62 65 20 6f 6d 69 74 tines to be omit
18d40 74 65 64 2e 20 20 54 68 61 74 20 63 61 70 61 62 ted. That capab
18d50 69 6c 69 74 79 0a 2a 2a 20 69 73 20 6e 6f 20 6c ility.** is no l
18d60 6f 6e 67 65 72 20 70 72 6f 76 69 64 65 64 2e 20 onger provided.
18d70 20 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 6e 20 6d Only built-in m
18d80 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73 emory allocators
18d90 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a 2a 2a can be used..**
18da0 0a 2a 2a 20 54 68 65 20 57 69 6e 64 6f 77 73 20 .** The Windows
18db0 4f 53 20 69 6e 74 65 72 66 61 63 65 20 6c 61 79 OS interface lay
18dc0 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 74 68 65 20 er calls.** the
18dd0 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 system malloc()
18de0 61 6e 64 20 66 72 65 65 28 29 20 64 69 72 65 63 and free() direc
18df0 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76 65 72 74 tly when convert
18e00 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 73 ing.** filenames
18e10 20 62 65 74 77 65 65 6e 20 74 68 65 20 55 54 46 between the UTF
18e20 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 -8 encoding used
18e30 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 61 6e by SQLite.** an
18e40 64 20 77 68 61 74 65 76 65 72 20 66 69 6c 65 6e d whatever filen
18e50 61 6d 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 ame encoding is
18e60 75 73 65 64 20 62 79 20 74 68 65 20 70 61 72 74 used by the part
18e70 69 63 75 6c 61 72 20 57 69 6e 64 6f 77 73 0a 2a icular Windows.*
18e80 2a 20 69 6e 73 74 61 6c 6c 61 74 69 6f 6e 2e 20 * installation.
18e90 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 Memory allocati
18ea0 6f 6e 20 65 72 72 6f 72 73 20 61 72 65 20 64 65 on errors are de
18eb0 74 65 63 74 65 64 2c 20 62 75 74 0a 2a 2a 20 74 tected, but.** t
18ec0 68 65 79 20 61 72 65 20 72 65 70 6f 72 74 65 64 hey are reported
18ed0 20 62 61 63 6b 20 61 73 20 5b 53 51 4c 49 54 45 back as [SQLITE
18ee0 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f 72 0a 2a 2a _CANTOPEN] or.**
18ef0 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5d 20 [SQLITE_IOERR]
18f00 72 61 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c rather than [SQL
18f10 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a ITE_NOMEM]..**.*
18f20 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
18f30 2a 2a 20 5b 48 31 37 33 30 33 5d 20 5b 48 31 37 ** [H17303] [H17
18f40 33 30 34 5d 20 5b 48 31 37 33 30 35 5d 20 5b 48 304] [H17305] [H
18f50 31 37 33 30 36 5d 20 5b 48 31 37 33 31 30 5d 20 17306] [H17310]
18f60 5b 48 31 37 33 31 32 5d 20 5b 48 31 37 33 31 35 [H17312] [H17315
18f70 5d 20 5b 48 31 37 33 31 38 5d 0a 2a 2a 20 5b 48 ] [H17318].** [H
18f80 31 37 33 32 31 5d 20 5b 48 31 37 33 32 32 5d 20 17321] [H17322]
18f90 5b 48 31 37 33 32 33 5d 0a 2a 2a 0a 2a 2a 20 54 [H17323].**.** T
18fa0 68 65 20 70 6f 69 6e 74 65 72 20 61 72 67 75 6d he pointer argum
18fb0 65 6e 74 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 ents to [sqlite3
18fc0 5f 66 72 65 65 28 29 5d 20 61 6e 64 20 5b 73 71 _free()] and [sq
18fd0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d lite3_realloc()]
18fe0 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 .** must be eith
18ff0 65 72 20 4e 55 4c 4c 20 6f 72 20 65 6c 73 65 20 er NULL or else
19000 70 6f 69 6e 74 65 72 73 20 6f 62 74 61 69 6e 65 pointers obtaine
19010 64 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a 2a d from a prior.*
19020 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 * invocation of
19030 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 [sqlite3_malloc(
19040 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 )] or [sqlite3_r
19050 65 61 6c 6c 6f 63 28 29 5d 20 74 68 61 74 20 68 ealloc()] that h
19060 61 76 65 0a 2a 2a 20 6e 6f 74 20 79 65 74 20 62 ave.** not yet b
19070 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a een released..**
19080 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 .** The applicat
19090 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 72 65 61 ion must not rea
190a0 64 20 6f 72 20 77 72 69 74 65 20 61 6e 79 20 70 d or write any p
190b0 61 72 74 20 6f 66 0a 2a 2a 20 61 20 62 6c 6f 63 art of.** a bloc
190c0 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 61 66 74 65 k of memory afte
190d0 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 72 65 r it has been re
190e0 6c 65 61 73 65 64 20 75 73 69 6e 67 0a 2a 2a 20 leased using.**
190f0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d [sqlite3_free()]
19100 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 or [sqlite3_rea
19110 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 lloc()]..*/.SQLI
19120 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
19130 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 29 ite3_malloc(int)
19140 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
19150 64 20 2a 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c d *sqlite3_reall
19160 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a oc(void*, int);.
19170 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
19180 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f 69 sqlite3_free(voi
19190 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 d*);../*.** CAPI
191a0 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 3REF: Memory All
191b0 6f 63 61 74 6f 72 20 53 74 61 74 69 73 74 69 63 ocator Statistic
191c0 73 20 7b 48 31 37 33 37 30 7d 20 3c 53 33 30 32 s {H17370} <S302
191d0 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 10>.**.** SQLite
191e0 20 70 72 6f 76 69 64 65 73 20 74 68 65 73 65 20 provides these
191f0 74 77 6f 20 69 6e 74 65 72 66 61 63 65 73 20 66 two interfaces f
19200 6f 72 20 72 65 70 6f 72 74 69 6e 67 20 6f 6e 20 or reporting on
19210 74 68 65 20 73 74 61 74 75 73 0a 2a 2a 20 6f 66 the status.** of
19220 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61 the [sqlite3_ma
19230 6c 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65 lloc()], [sqlite
19240 33 5f 66 72 65 65 28 29 5d 2c 20 61 6e 64 20 5b 3_free()], and [
19250 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
19260 29 5d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2c 20 )].** routines,
19270 77 68 69 63 68 20 66 6f 72 6d 20 74 68 65 20 62 which form the b
19280 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 uilt-in memory a
19290 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
192a0 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 tem..**.** Requi
192b0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 rements:.** [H17
192c0 33 37 31 5d 20 5b 48 31 37 33 37 33 5d 20 5b 48 371] [H17373] [H
192d0 31 37 33 37 34 5d 20 5b 48 31 37 33 37 35 5d 0a 17374] [H17375].
192e0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 */.SQLITE_API sq
192f0 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 lite3_int64 sqli
19300 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 te3_memory_used(
19310 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 void);.SQLITE_AP
19320 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 I sqlite3_int64
19330 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 sqlite3_memory_h
19340 69 67 68 77 61 74 65 72 28 69 6e 74 20 72 65 73 ighwater(int res
19350 65 74 46 6c 61 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 etFlag);../*.**
19360 43 41 50 49 33 52 45 46 3a 20 50 73 65 75 64 6f CAPI3REF: Pseudo
19370 2d 52 61 6e 64 6f 6d 20 4e 75 6d 62 65 72 20 47 -Random Number G
19380 65 6e 65 72 61 74 6f 72 20 7b 48 31 37 33 39 30 enerator {H17390
19390 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a } <S20000>.**.**
193a0 20 53 51 4c 69 74 65 20 63 6f 6e 74 61 69 6e 73 SQLite contains
193b0 20 61 20 68 69 67 68 2d 71 75 61 6c 69 74 79 20 a high-quality
193c0 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 pseudo-random nu
193d0 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 28 mber generator (
193e0 50 52 4e 47 29 20 75 73 65 64 20 74 6f 0a 2a 2a PRNG) used to.**
193f0 20 73 65 6c 65 63 74 20 72 61 6e 64 6f 6d 20 5b select random [
19400 52 4f 57 49 44 20 7c 20 52 4f 57 49 44 73 5d 20 ROWID | ROWIDs]
19410 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 6e when inserting n
19420 65 77 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 ew records into
19430 61 20 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20 a table that.**
19440 61 6c 72 65 61 64 79 20 75 73 65 73 20 74 68 65 already uses the
19450 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c largest possibl
19460 65 20 5b 52 4f 57 49 44 5d 2e 20 20 54 68 65 20 e [ROWID]. The
19470 50 52 4e 47 20 69 73 20 61 6c 73 6f 20 75 73 65 PRNG is also use
19480 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 62 75 69 d for.** the bui
19490 6c 64 2d 69 6e 20 72 61 6e 64 6f 6d 28 29 20 61 ld-in random() a
194a0 6e 64 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 29 20 nd randomblob()
194b0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 SQL functions.
194c0 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 61 This interface a
194d0 6c 6c 6f 77 73 0a 2a 2a 20 61 70 70 6c 69 63 61 llows.** applica
194e0 74 69 6f 6e 73 20 74 6f 20 61 63 63 65 73 73 20 tions to access
194f0 74 68 65 20 73 61 6d 65 20 50 52 4e 47 20 66 6f the same PRNG fo
19500 72 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73 r other purposes
19510 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 ..**.** A call t
19520 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 o this routine s
19530 74 6f 72 65 73 20 4e 20 62 79 74 65 73 20 6f 66 tores N bytes of
19540 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f randomness into
19550 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a buffer P..**.**
19560 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 The first time
19570 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
19580 69 6e 76 6f 6b 65 64 20 28 65 69 74 68 65 72 20 invoked (either
19590 69 6e 74 65 72 6e 61 6c 6c 79 20 6f 72 20 62 79 internally or by
195a0 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 .** the applicat
195b0 69 6f 6e 29 20 74 68 65 20 50 52 4e 47 20 69 73 ion) the PRNG is
195c0 20 73 65 65 64 65 64 20 75 73 69 6e 67 20 72 61 seeded using ra
195d0 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65 ndomness obtaine
195e0 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 78 52 d.** from the xR
195f0 61 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64 andomness method
19600 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 of the default
19610 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 [sqlite3_vfs] ob
19620 6a 65 63 74 2e 0a 2a 2a 20 4f 6e 20 61 6c 6c 20 ject..** On all
19630 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 subsequent invoc
19640 61 74 69 6f 6e 73 2c 20 74 68 65 20 70 73 65 75 ations, the pseu
19650 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 73 do-randomness is
19660 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 69 6e generated.** in
19670 74 65 72 6e 61 6c 6c 79 20 61 6e 64 20 77 69 74 ternally and wit
19680 68 6f 75 74 20 72 65 63 6f 75 72 73 65 20 74 6f hout recourse to
19690 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 the [sqlite3_vf
196a0 73 5d 20 78 52 61 6e 64 6f 6d 6e 65 73 73 0a 2a s] xRandomness.*
196b0 2a 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 * method..**.**
196c0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
196d0 20 5b 48 31 37 33 39 32 5d 0a 2a 2f 0a 53 51 4c [H17392].*/.SQL
196e0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
196f0 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 ite3_randomness(
19700 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 50 29 3b int N, void *P);
19710 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
19720 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 20 41 : Compile-Time A
19730 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 43 61 6c uthorization Cal
19740 6c 62 61 63 6b 73 20 7b 48 31 32 35 30 30 7d 20 lbacks {H12500}
19750 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S70100>.**.** T
19760 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 69 his routine regi
19770 73 74 65 72 73 20 61 20 61 75 74 68 6f 72 69 7a sters a authoriz
19780 65 72 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 er callback with
19790 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a a particular.**
197a0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
197b0 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c 69 65 64 ction], supplied
197c0 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 in the first ar
197d0 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 61 gument..** The a
197e0 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
197f0 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73 ck is invoked as
19800 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 SQL statements
19810 61 72 65 20 62 65 69 6e 67 20 63 6f 6d 70 69 6c are being compil
19820 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c 69 74 65 ed.** by [sqlite
19830 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72 20 3_prepare()] or
19840 69 74 73 20 76 61 72 69 61 6e 74 73 20 5b 73 71 its variants [sq
19850 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
19860 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ()],.** [sqlite3
19870 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 61 6e _prepare16()] an
19880 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 d [sqlite3_prepa
19890 72 65 31 36 5f 76 32 28 29 5d 2e 20 20 41 74 20 re16_v2()]. At
198a0 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f 69 6e 74 various.** point
198b0 73 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d s during the com
198c0 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63 65 73 73 pilation process
198d0 2c 20 61 73 20 6c 6f 67 69 63 20 69 73 20 62 65 , as logic is be
198e0 69 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 ing created.** t
198f0 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 69 6f 75 o perform variou
19900 73 20 61 63 74 69 6f 6e 73 2c 20 74 68 65 20 61 s actions, the a
19910 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
19920 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f ck is invoked to
19930 0a 2a 2a 20 73 65 65 20 69 66 20 74 68 6f 73 65 .** see if those
19940 20 61 63 74 69 6f 6e 73 20 61 72 65 20 61 6c 6c actions are all
19950 6f 77 65 64 2e 20 20 54 68 65 20 61 75 74 68 6f owed. The autho
19960 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 73 rizer callback s
19970 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72 6e 20 hould.** return
19980 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74 6f 20 61 [SQLITE_OK] to a
19990 6c 6c 6f 77 20 74 68 65 20 61 63 74 69 6f 6e 2c llow the action,
199a0 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d [SQLITE_IGNORE]
199b0 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68 65 to disallow the
199c0 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 61 63 74 .** specific act
199d0 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77 20 74 68 ion but allow th
199e0 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
199f0 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62 to continue to b
19a00 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2c 20 6f e.** compiled, o
19a10 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d 20 r [SQLITE_DENY]
19a20 74 6f 20 63 61 75 73 65 20 74 68 65 20 65 6e 74 to cause the ent
19a30 69 72 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e ire SQL statemen
19a40 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65 6a 65 63 t to be.** rejec
19a50 74 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f ted with an erro
19a60 72 2e 20 20 49 66 20 74 68 65 20 61 75 74 68 6f r. If the autho
19a70 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 rizer callback r
19a80 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79 20 76 61 eturns.** any va
19a90 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e 20 5b lue other than [
19aa0 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 2c 20 SQLITE_IGNORE],
19ab0 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 6f 72 20 [SQLITE_OK], or
19ac0 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d 0a 2a 2a [SQLITE_DENY].**
19ad0 20 74 68 65 6e 20 74 68 65 20 5b 73 71 6c 69 74 then the [sqlit
19ae0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
19af0 20 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20 63 or equivalent c
19b00 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 all that trigger
19b10 65 64 0a 2a 2a 20 74 68 65 20 61 75 74 68 6f 72 ed.** the author
19b20 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c 20 77 izer will fail w
19b30 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 ith an error mes
19b40 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e sage..**.** When
19b50 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 the callback re
19b60 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b turns [SQLITE_OK
19b70 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 ], that means th
19b80 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 72 e operation.** r
19b90 65 71 75 65 73 74 65 64 20 69 73 20 6f 6b 2e 20 equested is ok.
19ba0 20 57 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 When the callba
19bb0 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 ck returns [SQLI
19bc0 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0a 2a 2a TE_DENY], the.**
19bd0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
19be0 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 69 76 e_v2()] or equiv
19bf0 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 74 20 alent call that
19c00 74 72 69 67 67 65 72 65 64 20 74 68 65 0a 2a 2a triggered the.**
19c10 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69 6c 6c authorizer will
19c20 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20 65 72 fail with an er
19c30 72 6f 72 20 6d 65 73 73 61 67 65 20 65 78 70 6c ror message expl
19c40 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a 2a 20 61 aining that.** a
19c50 63 63 65 73 73 20 69 73 20 64 65 6e 69 65 64 2e ccess is denied.
19c60 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 .**.** The firs
19c70 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 t parameter to t
19c80 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 he authorizer ca
19c90 6c 6c 62 61 63 6b 20 69 73 20 61 20 63 6f 70 79 llback is a copy
19ca0 20 6f 66 20 74 68 65 20 74 68 69 72 64 0a 2a 2a of the third.**
19cb0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 parameter to th
19cc0 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 e sqlite3_set_au
19cd0 74 68 6f 72 69 7a 65 72 28 29 20 69 6e 74 65 72 thorizer() inter
19ce0 66 61 63 65 2e 20 54 68 65 20 73 65 63 6f 6e 64 face. The second
19cf0 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 6f parameter.** to
19d00 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 the callback is
19d10 20 61 6e 20 69 6e 74 65 67 65 72 20 5b 53 51 4c an integer [SQL
19d20 49 54 45 5f 43 4f 50 59 20 7c 20 61 63 74 69 6f ITE_COPY | actio
19d30 6e 20 63 6f 64 65 5d 20 74 68 61 74 20 73 70 65 n code] that spe
19d40 63 69 66 69 65 73 0a 2a 2a 20 74 68 65 20 70 61 cifies.** the pa
19d50 72 74 69 63 75 6c 61 72 20 61 63 74 69 6f 6e 20 rticular action
19d60 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 64 to be authorized
19d70 2e 20 54 68 65 20 74 68 69 72 64 20 74 68 72 6f . The third thro
19d80 75 67 68 20 73 69 78 74 68 20 70 61 72 61 6d 65 ugh sixth parame
19d90 74 65 72 73 0a 2a 2a 20 74 6f 20 74 68 65 20 63 ters.** to the c
19da0 61 6c 6c 62 61 63 6b 20 61 72 65 20 7a 65 72 6f allback are zero
19db0 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 -terminated stri
19dc0 6e 67 73 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ngs that contain
19dd0 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64 additional.** d
19de0 65 74 61 69 6c 73 20 61 62 6f 75 74 20 74 68 65 etails about the
19df0 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 75 action to be au
19e00 74 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 thorized..**.**
19e10 49 66 20 74 68 65 20 61 63 74 69 6f 6e 20 63 6f If the action co
19e20 64 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 52 45 de is [SQLITE_RE
19e30 41 44 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 AD].** and the c
19e40 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 allback returns
19e50 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 [SQLITE_IGNORE]
19e60 74 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 70 72 65 then the.** [pre
19e70 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
19e80 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 63 6f statement is co
19e90 6e 73 74 72 75 63 74 65 64 20 74 6f 20 73 75 62 nstructed to sub
19ea0 73 74 69 74 75 74 65 0a 2a 2a 20 61 20 4e 55 4c stitute.** a NUL
19eb0 4c 20 76 61 6c 75 65 20 69 6e 20 70 6c 61 63 65 L value in place
19ec0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f of the table co
19ed0 6c 75 6d 6e 20 74 68 61 74 20 77 6f 75 6c 64 20 lumn that would
19ee0 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 72 65 61 have.** been rea
19ef0 64 20 69 66 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d d if [SQLITE_OK]
19f00 20 68 61 64 20 62 65 65 6e 20 72 65 74 75 72 6e had been return
19f10 65 64 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 45 ed. The [SQLITE
19f20 5f 49 47 4e 4f 52 45 5d 0a 2a 2a 20 72 65 74 75 _IGNORE].** retu
19f30 72 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 rn can be used t
19f40 6f 20 64 65 6e 79 20 61 6e 20 75 6e 74 72 75 73 o deny an untrus
19f50 74 65 64 20 75 73 65 72 20 61 63 63 65 73 73 20 ted user access
19f60 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a to individual.**
19f70 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 61 20 74 61 columns of a ta
19f80 62 6c 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 61 ble..** If the a
19f90 63 74 69 6f 6e 20 63 6f 64 65 20 69 73 20 5b 53 ction code is [S
19fa0 51 4c 49 54 45 5f 44 45 4c 45 54 45 5d 20 61 6e QLITE_DELETE] an
19fb0 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 d the callback r
19fc0 65 74 75 72 6e 73 0a 2a 2a 20 5b 53 51 4c 49 54 eturns.** [SQLIT
19fd0 45 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 E_IGNORE] then t
19fe0 68 65 20 5b 44 45 4c 45 54 45 5d 20 6f 70 65 72 he [DELETE] oper
19ff0 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73 20 62 ation proceeds b
1a000 75 74 20 74 68 65 0a 2a 2a 20 5b 74 72 75 6e 63 ut the.** [trunc
1a010 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ate optimization
1a020 5d 20 69 73 20 64 69 73 61 62 6c 65 64 20 61 6e ] is disabled an
1a030 64 20 61 6c 6c 20 72 6f 77 73 20 61 72 65 20 64 d all rows are d
1a040 65 6c 65 74 65 64 20 69 6e 64 69 76 69 64 75 61 eleted individua
1a050 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 75 lly..**.** An au
1a060 74 68 6f 72 69 7a 65 72 20 69 73 20 75 73 65 64 thorizer is used
1a070 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 when [sqlite3_p
1a080 72 65 70 61 72 65 20 7c 20 70 72 65 70 61 72 69 repare | prepari
1a090 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 ng].** SQL state
1a0a0 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75 6e ments from an un
1a0b0 74 72 75 73 74 65 64 20 73 6f 75 72 63 65 2c 20 trusted source,
1a0c0 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 to ensure that t
1a0d0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
1a0e0 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72 79 20 s.** do not try
1a0f0 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 20 74 to access data t
1a100 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f hey are not allo
1a110 77 65 64 20 74 6f 20 73 65 65 2c 20 6f 72 20 74 wed to see, or t
1a120 68 61 74 20 74 68 65 79 20 64 6f 20 6e 6f 74 0a hat they do not.
1a130 2a 2a 20 74 72 79 20 74 6f 20 65 78 65 63 75 74 ** try to execut
1a140 65 20 6d 61 6c 69 63 69 6f 75 73 20 73 74 61 74 e malicious stat
1a150 65 6d 65 6e 74 73 20 74 68 61 74 20 64 61 6d 61 ements that dama
1a160 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ge the database.
1a170 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 For.** example
1a180 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e , an application
1a190 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 73 65 may allow a use
1a1a0 72 20 74 6f 20 65 6e 74 65 72 20 61 72 62 69 74 r to enter arbit
1a1b0 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75 65 72 rary.** SQL quer
1a1c0 69 65 73 20 66 6f 72 20 65 76 61 6c 75 61 74 69 ies for evaluati
1a1d0 6f 6e 20 62 79 20 61 20 64 61 74 61 62 61 73 65 on by a database
1a1e0 2e 20 20 42 75 74 20 74 68 65 20 61 70 70 6c 69 . But the appli
1a1f0 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a 20 6e cation does.** n
1a200 6f 74 20 77 61 6e 74 20 74 68 65 20 75 73 65 72 ot want the user
1a210 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 6d to be able to m
1a220 61 6b 65 20 61 72 62 69 74 72 61 72 79 20 63 68 ake arbitrary ch
1a230 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 anges to the.**
1a240 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 61 75 database. An au
1a250 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64 20 74 thorizer could t
1a260 68 65 6e 20 62 65 20 70 75 74 20 69 6e 20 70 6c hen be put in pl
1a270 61 63 65 20 77 68 69 6c 65 20 74 68 65 0a 2a 2a ace while the.**
1a280 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53 51 user-entered SQ
1a290 4c 20 69 73 20 62 65 69 6e 67 20 5b 73 71 6c 69 L is being [sqli
1a2a0 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70 72 te3_prepare | pr
1a2b0 65 70 61 72 65 64 5d 20 74 68 61 74 0a 2a 2a 20 epared] that.**
1a2c0 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 79 74 disallows everyt
1a2d0 68 69 6e 67 20 65 78 63 65 70 74 20 5b 53 45 4c hing except [SEL
1a2e0 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 73 2e ECT] statements.
1a2f0 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 .**.** Applicati
1a300 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f ons that need to
1a310 20 70 72 6f 63 65 73 73 20 53 51 4c 20 66 72 6f process SQL fro
1a320 6d 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75 72 m untrusted sour
1a330 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61 6c 73 ces.** might als
1a340 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77 65 72 o consider lower
1a350 69 6e 67 20 72 65 73 6f 75 72 63 65 20 6c 69 6d ing resource lim
1a360 69 74 73 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 its using [sqlit
1a370 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 61 e3_limit()].** a
1a380 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64 61 74 61 nd limiting data
1a390 62 61 73 65 20 73 69 7a 65 20 75 73 69 6e 67 20 base size using
1a3a0 74 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f the [max_page_co
1a3b0 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a 2a 2a unt] [PRAGMA].**
1a3c0 20 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f 20 in addition to
1a3d0 75 73 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 69 using an authori
1a3e0 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 zer..**.** Only
1a3f0 61 20 73 69 6e 67 6c 65 20 61 75 74 68 6f 72 69 a single authori
1a400 7a 65 72 20 63 61 6e 20 62 65 20 69 6e 20 70 6c zer can be in pl
1a410 61 63 65 20 6f 6e 20 61 20 64 61 74 61 62 61 73 ace on a databas
1a420 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 e connection.**
1a430 61 74 20 61 20 74 69 6d 65 2e 20 20 45 61 63 68 at a time. Each
1a440 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
1a450 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 _set_authorizer
1a460 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a 2a 2a overrides the.**
1a470 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e 20 previous call.
1a480 20 44 69 73 61 62 6c 65 20 74 68 65 20 61 75 74 Disable the aut
1a490 68 6f 72 69 7a 65 72 20 62 79 20 69 6e 73 74 61 horizer by insta
1a4a0 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63 61 6c lling a NULL cal
1a4b0 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20 61 75 lback..** The au
1a4c0 74 68 6f 72 69 7a 65 72 20 69 73 20 64 69 73 61 thorizer is disa
1a4d0 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e bled by default.
1a4e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
1a4f0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d rizer callback m
1a500 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 ust not do anyth
1a510 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f ing that will mo
1a520 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 dify.** the data
1a530 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1a540 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 that invoked the
1a550 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c authorizer call
1a560 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 back..** Note th
1a570 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 at [sqlite3_prep
1a580 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 are_v2()] and [s
1a590 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 62 qlite3_step()] b
1a5a0 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 72 oth modify their
1a5b0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e .** database con
1a5c0 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 nections for the
1a5d0 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 meaning of "mod
1a5e0 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 72 ify" in this par
1a5f0 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 57 68 agraph..**.** Wh
1a600 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 en [sqlite3_prep
1a610 61 72 65 5f 76 32 28 29 5d 20 69 73 20 75 73 65 are_v2()] is use
1a620 64 20 74 6f 20 70 72 65 70 61 72 65 20 61 20 73 d to prepare a s
1a630 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a 2a 2a tatement, the.**
1a640 20 73 74 61 74 65 6d 65 6e 74 20 6d 69 67 68 74 statement might
1a650 20 62 65 20 72 65 2d 70 72 65 70 61 72 65 64 20 be re-prepared
1a660 64 75 72 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f during [sqlite3_
1a670 73 74 65 70 28 29 5d 20 64 75 65 20 74 6f 20 61 step()] due to a
1a680 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e .** schema chan
1a690 67 65 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 ge. Hence, the
1a6a0 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 application shou
1a6b0 6c 64 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 ld ensure that t
1a6c0 68 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 75 he.** correct au
1a6d0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1a6e0 6b 20 72 65 6d 61 69 6e 73 20 69 6e 20 70 6c 61 k remains in pla
1a6f0 63 65 20 64 75 72 69 6e 67 20 74 68 65 20 5b 73 ce during the [s
1a700 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a qlite3_step()]..
1a710 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
1a720 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 the authorizer c
1a730 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b allback is invok
1a740 65 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 0a 2a ed only during.*
1a750 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 * [sqlite3_prepa
1a760 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 re()] or its var
1a770 69 61 6e 74 73 2e 20 20 41 75 74 68 6f 72 69 7a iants. Authoriz
1a780 61 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 ation is not.**
1a790 70 65 72 66 6f 72 6d 65 64 20 64 75 72 69 6e 67 performed during
1a7a0 20 73 74 61 74 65 6d 65 6e 74 20 65 76 61 6c 75 statement evalu
1a7b0 61 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69 74 65 ation in [sqlite
1a7c0 33 5f 73 74 65 70 28 29 5d 2c 20 75 6e 6c 65 73 3_step()], unles
1a7d0 73 0a 2a 2a 20 61 73 20 73 74 61 74 65 64 20 69 s.** as stated i
1a7e0 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 n the previous p
1a7f0 61 72 61 67 72 61 70 68 2c 20 73 71 6c 69 74 65 aragraph, sqlite
1a800 33 5f 73 74 65 70 28 29 20 69 6e 76 6f 6b 65 73 3_step() invokes
1a810 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 70 .** sqlite3_prep
1a820 61 72 65 5f 76 32 28 29 20 74 6f 20 72 65 70 72 are_v2() to repr
1a830 65 70 61 72 65 20 61 20 73 74 61 74 65 6d 65 6e epare a statemen
1a840 74 20 61 66 74 65 72 20 61 20 73 63 68 65 6d 61 t after a schema
1a850 20 63 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 52 change..**.** R
1a860 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
1a870 5b 48 31 32 35 30 31 5d 20 5b 48 31 32 35 30 32 [H12501] [H12502
1a880 5d 20 5b 48 31 32 35 30 33 5d 20 5b 48 31 32 35 ] [H12503] [H125
1a890 30 34 5d 20 5b 48 31 32 35 30 35 5d 20 5b 48 31 04] [H12505] [H1
1a8a0 32 35 30 36 5d 20 5b 48 31 32 35 30 37 5d 20 5b 2506] [H12507] [
1a8b0 48 31 32 35 31 30 5d 0a 2a 2a 20 5b 48 31 32 35 H12510].** [H125
1a8c0 31 31 5d 20 5b 48 31 32 35 31 32 5d 20 5b 48 31 11] [H12512] [H1
1a8d0 32 35 32 30 5d 20 5b 48 31 32 35 32 31 5d 20 5b 2520] [H12521] [
1a8e0 48 31 32 35 32 32 5d 0a 2a 2f 0a 53 51 4c 49 54 H12522].*/.SQLIT
1a8f0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1a900 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 3_set_authorizer
1a910 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 0a 20 20 (. sqlite3*,.
1a920 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69 int (*xAuth)(voi
1a930 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 d*,int,const cha
1a940 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 r*,const char*,c
1a950 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 onst char*,const
1a960 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69 64 20 char*),. void
1a970 2a 70 55 73 65 72 44 61 74 61 0a 29 3b 0a 0a 2f *pUserData.);../
1a980 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 *.** CAPI3REF: A
1a990 75 74 68 6f 72 69 7a 65 72 20 52 65 74 75 72 6e uthorizer Return
1a9a0 20 43 6f 64 65 73 20 7b 48 31 32 35 39 30 7d 20 Codes {H12590}
1a9b0 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <H12500>.**.** T
1a9c0 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f he [sqlite3_set_
1a9d0 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 74 authorizer | aut
1a9e0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
1a9f0 20 66 75 6e 63 74 69 6f 6e 5d 20 6d 75 73 74 0a function] must.
1aa00 2a 2a 20 72 65 74 75 72 6e 20 65 69 74 68 65 72 ** return either
1aa10 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 72 20 [SQLITE_OK] or
1aa20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 74 77 6f one of these two
1aa30 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 20 6f 72 constants in or
1aa40 64 65 72 0a 2a 2a 20 74 6f 20 73 69 67 6e 61 6c der.** to signal
1aa50 20 53 51 4c 69 74 65 20 77 68 65 74 68 65 72 20 SQLite whether
1aa60 6f 72 20 6e 6f 74 20 74 68 65 20 61 63 74 69 6f or not the actio
1aa70 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e 20 n is permitted.
1aa80 20 53 65 65 20 74 68 65 0a 2a 2a 20 5b 73 71 6c See the.** [sql
1aa90 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 ite3_set_authori
1aaa0 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 zer | authorizer
1aab0 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 5d 20 documentation]
1aac0 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a for additional.*
1aad0 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a * information..*
1aae0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1aaf0 5f 44 45 4e 59 20 20 20 31 20 20 20 2f 2a 20 41 _DENY 1 /* A
1ab00 62 6f 72 74 20 74 68 65 20 53 51 4c 20 73 74 61 bort the SQL sta
1ab10 74 65 6d 65 6e 74 20 77 69 74 68 20 61 6e 20 65 tement with an e
1ab20 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 rror */.#define
1ab30 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 32 20 SQLITE_IGNORE 2
1ab40 20 20 2f 2a 20 44 6f 6e 27 74 20 61 6c 6c 6f 77 /* Don't allow
1ab50 20 61 63 63 65 73 73 2c 20 62 75 74 20 64 6f 6e access, but don
1ab60 27 74 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 't generate an e
1ab70 72 72 6f 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 rror */../*.** C
1ab80 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69 API3REF: Authori
1ab90 7a 65 72 20 41 63 74 69 6f 6e 20 43 6f 64 65 73 zer Action Codes
1aba0 20 7b 48 31 32 35 35 30 7d 20 3c 48 31 32 35 30 {H12550} <H1250
1abb0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 0>.**.** The [sq
1abc0 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 lite3_set_author
1abd0 69 7a 65 72 28 29 5d 20 69 6e 74 65 72 66 61 63 izer()] interfac
1abe0 65 20 72 65 67 69 73 74 65 72 73 20 61 20 63 61 e registers a ca
1abf0 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0a llback function.
1ac00 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b ** that is invok
1ac10 65 64 20 74 6f 20 61 75 74 68 6f 72 69 7a 65 20 ed to authorize
1ac20 63 65 72 74 61 69 6e 20 53 51 4c 20 73 74 61 74 certain SQL stat
1ac30 65 6d 65 6e 74 20 61 63 74 69 6f 6e 73 2e 20 20 ement actions.
1ac40 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 The.** second pa
1ac50 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 63 rameter to the c
1ac60 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 6e allback is an in
1ac70 74 65 67 65 72 20 63 6f 64 65 20 74 68 61 74 20 teger code that
1ac80 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68 61 specifies.** wha
1ac90 74 20 61 63 74 69 6f 6e 20 69 73 20 62 65 69 6e t action is bein
1aca0 67 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20 54 g authorized. T
1acb0 68 65 73 65 20 61 72 65 20 74 68 65 20 69 6e 74 hese are the int
1acc0 65 67 65 72 20 61 63 74 69 6f 6e 20 63 6f 64 65 eger action code
1acd0 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 61 75 s that.** the au
1ace0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1acf0 6b 20 6d 61 79 20 62 65 20 70 61 73 73 65 64 2e k may be passed.
1ad00 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 63 74 .**.** These act
1ad10 69 6f 6e 20 63 6f 64 65 20 76 61 6c 75 65 73 20 ion code values
1ad20 73 69 67 6e 69 66 79 20 77 68 61 74 20 6b 69 6e signify what kin
1ad30 64 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 20 69 d of operation i
1ad40 73 20 74 6f 20 62 65 0a 2a 2a 20 61 75 74 68 6f s to be.** autho
1ad50 72 69 7a 65 64 2e 20 20 54 68 65 20 33 72 64 20 rized. The 3rd
1ad60 61 6e 64 20 34 74 68 20 70 61 72 61 6d 65 74 65 and 4th paramete
1ad70 72 73 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72 rs to the author
1ad80 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6c 6c 62 ization.** callb
1ad90 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c ack function wil
1ada0 6c 20 62 65 20 70 61 72 61 6d 65 74 65 72 73 20 l be parameters
1adb0 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e 64 69 6e or NULL dependin
1adc0 67 20 6f 6e 20 77 68 69 63 68 20 6f 66 20 74 68 g on which of th
1add0 65 73 65 0a 2a 2a 20 63 6f 64 65 73 20 69 73 20 ese.** codes is
1ade0 75 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f used as the seco
1adf0 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 nd parameter. T
1ae00 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 he 5th parameter
1ae10 20 74 6f 20 74 68 65 0a 2a 2a 20 61 75 74 68 6f to the.** autho
1ae20 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 rizer callback i
1ae30 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 s the name of th
1ae40 65 20 64 61 74 61 62 61 73 65 20 28 22 6d 61 69 e database ("mai
1ae50 6e 22 2c 20 22 74 65 6d 70 22 2c 0a 2a 2a 20 65 n", "temp",.** e
1ae60 74 63 2e 29 20 69 66 20 61 70 70 6c 69 63 61 62 tc.) if applicab
1ae70 6c 65 2e 20 20 54 68 65 20 36 74 68 20 70 61 72 le. The 6th par
1ae80 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75 ameter to the au
1ae90 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1aea0 6b 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61 6d 65 k.** is the name
1aeb0 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f of the inner-mo
1aec0 73 74 20 74 72 69 67 67 65 72 20 6f 72 20 76 69 st trigger or vi
1aed0 65 77 20 74 68 61 74 20 69 73 20 72 65 73 70 6f ew that is respo
1aee0 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 74 68 nsible for.** th
1aef0 65 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 74 e access attempt
1af00 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 69 73 or NULL if this
1af10 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 74 20 access attempt
1af20 69 73 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d is directly from
1af30 0a 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c 20 53 51 .** top-level SQ
1af40 4c 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 L code..**.** Re
1af50 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
1af60 48 31 32 35 35 31 5d 20 5b 48 31 32 35 35 32 5d H12551] [H12552]
1af70 20 5b 48 31 32 35 35 33 5d 20 5b 48 31 32 35 35 [H12553] [H1255
1af80 34 5d 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 4].*/./*********
1af90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1afa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1afb0 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ** 3rd *********
1afc0 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a 2a 2a *** 4th ********
1afd0 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ***/.#define SQL
1afe0 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58 ITE_CREATE_INDEX
1aff0 20 20 20 20 20 20 20 20 20 20 31 20 20 20 2f 2a 1 /*
1b000 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 Index Name
1b010 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
1b020 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1b030 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 20 TE_CREATE_TABLE
1b040 20 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a 20 2 /*
1b050 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1b060 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
1b070 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1b080 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e E_CREATE_TEMP_IN
1b090 44 45 58 20 20 20 20 20 33 20 20 20 2f 2a 20 49 DEX 3 /* I
1b0a0 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54 ndex Name T
1b0b0 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a able Name *
1b0c0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1b0d0 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 _CREATE_TEMP_TAB
1b0e0 4c 45 20 20 20 20 20 34 20 20 20 2f 2a 20 54 61 LE 4 /* Ta
1b0f0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 ble Name NU
1b100 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f LL */
1b110 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1b120 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 47 CREATE_TEMP_TRIG
1b130 47 45 52 20 20 20 35 20 20 20 2f 2a 20 54 72 69 GER 5 /* Tri
1b140 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 gger Name Tab
1b150 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a le Name */.
1b160 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
1b170 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 20 REATE_TEMP_VIEW
1b180 20 20 20 20 20 36 20 20 20 2f 2a 20 56 69 65 77 6 /* View
1b190 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c Name NULL
1b1a0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 */.#
1b1b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 define SQLITE_CR
1b1c0 45 41 54 45 5f 54 52 49 47 47 45 52 20 20 20 20 EATE_TRIGGER
1b1d0 20 20 20 20 37 20 20 20 2f 2a 20 54 72 69 67 67 7 /* Trigg
1b1e0 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 er Name Table
1b1f0 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 Name */.#d
1b200 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 efine SQLITE_CRE
1b210 41 54 45 5f 56 49 45 57 20 20 20 20 20 20 20 20 ATE_VIEW
1b220 20 20 20 38 20 20 20 2f 2a 20 56 69 65 77 20 4e 8 /* View N
1b230 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 ame NULL
1b240 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 */.#de
1b250 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4c 45 fine SQLITE_DELE
1b260 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 TE
1b270 20 20 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 9 /* Table N
1b280 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 ame NULL
1b290 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 */.#def
1b2a0 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f ine SQLITE_DROP_
1b2b0 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20 INDEX
1b2c0 31 30 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 10 /* Index Na
1b2d0 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 me Table Na
1b2e0 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 me */.#defi
1b2f0 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 ne SQLITE_DROP_T
1b300 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 31 ABLE 1
1b310 31 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 1 /* Table Nam
1b320 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 e NULL
1b330 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
1b340 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 e SQLITE_DROP_TE
1b350 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 20 31 32 MP_INDEX 12
1b360 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 /* Index Name
1b370 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 Table Name
1b380 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
1b390 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d SQLITE_DROP_TEM
1b3a0 50 5f 54 41 42 4c 45 20 20 20 20 20 20 31 33 20 P_TABLE 13
1b3b0 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 /* Table Name
1b3c0 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 NULL
1b3d0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1b3e0 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 SQLITE_DROP_TEMP
1b3f0 5f 54 52 49 47 47 45 52 20 20 20 20 31 34 20 20 _TRIGGER 14
1b400 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 /* Trigger Name
1b410 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 Table Name
1b420 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1b430 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f QLITE_DROP_TEMP_
1b440 56 49 45 57 20 20 20 20 20 20 20 31 35 20 20 20 VIEW 15
1b450 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 /* View Name
1b460 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 NULL
1b470 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1b480 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 LITE_DROP_TRIGGE
1b490 52 20 20 20 20 20 20 20 20 20 31 36 20 20 20 2f R 16 /
1b4a0 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 20 * Trigger Name
1b4b0 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 Table Name
1b4c0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1b4d0 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 20 20 20 ITE_DROP_VIEW
1b4e0 20 20 20 20 20 20 20 20 20 31 37 20 20 20 2f 2a 17 /*
1b4f0 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 20 View Name
1b500 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 NULL
1b510 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1b520 54 45 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20 TE_INSERT
1b530 20 20 20 20 20 20 20 20 31 38 20 20 20 2f 2a 20 18 /*
1b540 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1b550 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
1b560 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1b570 45 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20 20 E_PRAGMA
1b580 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20 50 19 /* P
1b590 72 61 67 6d 61 20 4e 61 6d 65 20 20 20 20 20 31 ragma Name 1
1b5a0 73 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c 20 2a st arg or NULL *
1b5b0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1b5c0 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 20 20 _READ
1b5d0 20 20 20 20 20 20 32 30 20 20 20 2f 2a 20 54 61 20 /* Ta
1b5e0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f ble Name Co
1b5f0 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f lumn Name */
1b600 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1b610 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 20 20 SELECT
1b620 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4e 55 4c 21 /* NUL
1b630 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c L NUL
1b640 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a L */.
1b650 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 #define SQLITE_T
1b660 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20 20 RANSACTION
1b670 20 20 20 20 32 32 20 20 20 2f 2a 20 4f 70 65 72 22 /* Oper
1b680 61 74 69 6f 6e 20 20 20 20 20 20 20 4e 55 4c 4c ation NULL
1b690 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 */.#
1b6a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 50 define SQLITE_UP
1b6b0 44 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 DATE
1b6c0 20 20 20 32 33 20 20 20 2f 2a 20 54 61 62 6c 65 23 /* Table
1b6d0 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c 75 6d Name Colum
1b6e0 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 n Name */.#d
1b6f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 54 54 efine SQLITE_ATT
1b700 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 ACH
1b710 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 65 6e 61 24 /* Filena
1b720 6d 65 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 me NULL
1b730 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 */.#de
1b740 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 54 41 fine SQLITE_DETA
1b750 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 CH
1b760 20 32 35 20 20 20 2f 2a 20 44 61 74 61 62 61 73 25 /* Databas
1b770 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20 20 20 e Name NULL
1b780 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 */.#def
1b790 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 ine SQLITE_ALTER
1b7a0 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 _TABLE
1b7b0 32 36 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 26 /* Database
1b7c0 20 4e 61 6d 65 20 20 20 54 61 62 6c 65 20 4e 61 Name Table Na
1b7d0 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 me */.#defi
1b7e0 6e 65 20 53 51 4c 49 54 45 5f 52 45 49 4e 44 45 ne SQLITE_REINDE
1b7f0 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 X 2
1b800 37 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 7 /* Index Nam
1b810 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 e NULL
1b820 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
1b830 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a 45 e SQLITE_ANALYZE
1b840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 38 28
1b850 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
1b860 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 NULL
1b870 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
1b880 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 SQLITE_CREATE_V
1b890 54 41 42 4c 45 20 20 20 20 20 20 20 20 32 39 20 TABLE 29
1b8a0 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 /* Table Name
1b8b0 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65 Module Name
1b8c0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1b8d0 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41 42 SQLITE_DROP_VTAB
1b8e0 4c 45 20 20 20 20 20 20 20 20 20 20 33 30 20 20 LE 30
1b8f0 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 /* Table Name
1b900 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65 20 Module Name
1b910 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1b920 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 20 20 QLITE_FUNCTION
1b930 20 20 20 20 20 20 20 20 20 20 20 33 31 20 20 20 31
1b940 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 /* NULL
1b950 20 20 20 46 75 6e 63 74 69 6f 6e 20 4e 61 6d 65 Function Name
1b960 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1b970 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e 54 20 20 LITE_SAVEPOINT
1b980 20 20 20 20 20 20 20 20 20 20 33 32 20 20 20 2f 32 /
1b990 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20 20 20 * Operation
1b9a0 20 20 53 61 76 65 70 6f 69 6e 74 20 4e 61 6d 65 Savepoint Name
1b9b0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1b9c0 49 54 45 5f 43 4f 50 59 20 20 20 20 20 20 20 20 ITE_COPY
1b9d0 20 20 20 20 20 20 20 20 20 20 30 20 20 20 2f 2a 0 /*
1b9e0 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 64 20 No longer used
1b9f0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 */../*.** CAPI3R
1ba00 45 46 3a 20 54 72 61 63 69 6e 67 20 41 6e 64 20 EF: Tracing And
1ba10 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e 63 74 69 Profiling Functi
1ba20 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20 3c 53 36 ons {H12280} <S6
1ba30 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 0400>.** EXPERIM
1ba40 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 ENTAL.**.** Thes
1ba50 65 20 72 6f 75 74 69 6e 65 73 20 72 65 67 69 73 e routines regis
1ba60 74 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e ter callback fun
1ba70 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 ctions that can
1ba80 62 65 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 be used for.** t
1ba90 72 61 63 69 6e 67 20 61 6e 64 20 70 72 6f 66 69 racing and profi
1baa0 6c 69 6e 67 20 74 68 65 20 65 78 65 63 75 74 69 ling the executi
1bab0 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d on of SQL statem
1bac0 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ents..**.** The
1bad0 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f callback functio
1bae0 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 79 20 n registered by
1baf0 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 29 20 sqlite3_trace()
1bb00 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 0a 2a 2a is invoked at.**
1bb10 20 76 61 72 69 6f 75 73 20 74 69 6d 65 73 20 77 various times w
1bb20 68 65 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 hen an SQL state
1bb30 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20 72 75 ment is being ru
1bb40 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 74 n by [sqlite3_st
1bb50 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 63 61 ep()]..** The ca
1bb60 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 61 llback returns a
1bb70 20 55 54 46 2d 38 20 72 65 6e 64 65 72 69 6e 67 UTF-8 rendering
1bb80 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 of the SQL stat
1bb90 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a 20 61 73 ement text.** as
1bba0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 66 the statement f
1bbb0 69 72 73 74 20 62 65 67 69 6e 73 20 65 78 65 63 irst begins exec
1bbc0 75 74 69 6e 67 2e 20 20 41 64 64 69 74 69 6f 6e uting. Addition
1bbd0 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 6f 63 63 al callbacks occ
1bbe0 75 72 0a 2a 2a 20 61 73 20 65 61 63 68 20 74 72 ur.** as each tr
1bbf0 69 67 67 65 72 65 64 20 73 75 62 70 72 6f 67 72 iggered subprogr
1bc00 61 6d 20 69 73 20 65 6e 74 65 72 65 64 2e 20 20 am is entered.
1bc10 54 68 65 20 63 61 6c 6c 62 61 63 6b 73 20 66 6f The callbacks fo
1bc20 72 20 74 72 69 67 67 65 72 73 0a 2a 2a 20 63 6f r triggers.** co
1bc30 6e 74 61 69 6e 20 61 20 55 54 46 2d 38 20 53 51 ntain a UTF-8 SQ
1bc40 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20 69 L comment that i
1bc50 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 74 72 dentifies the tr
1bc60 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 igger..**.** The
1bc70 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 callback functi
1bc80 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 79 on registered by
1bc90 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 sqlite3_profile
1bca0 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a () is invoked.**
1bcb0 20 61 73 20 65 61 63 68 20 53 51 4c 20 73 74 61 as each SQL sta
1bcc0 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 65 73 2e tement finishes.
1bcd0 20 20 54 68 65 20 70 72 6f 66 69 6c 65 20 63 61 The profile ca
1bce0 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 73 0a llback contains.
1bcf0 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 ** the original
1bd00 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74 20 61 statement text a
1bd10 6e 64 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f nd an estimate o
1bd20 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 69 6d f wall-clock tim
1bd30 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c 6f 6e 67 e.** of how long
1bd40 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20 that statement
1bd50 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a 2a 2a 0a took to run..**.
1bd60 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
1bd70 0a 2a 2a 20 5b 48 31 32 32 38 31 5d 20 5b 48 31 .** [H12281] [H1
1bd80 32 32 38 32 5d 20 5b 48 31 32 32 38 33 5d 20 5b 2282] [H12283] [
1bd90 48 31 32 32 38 34 5d 20 5b 48 31 32 32 38 35 5d H12284] [H12285]
1bda0 20 5b 48 31 32 32 38 37 5d 20 5b 48 31 32 32 38 [H12287] [H1228
1bdb0 38 5d 20 5b 48 31 32 32 38 39 5d 0a 2a 2a 20 5b 8] [H12289].** [
1bdc0 48 31 32 32 39 30 5d 0a 2a 2f 0a 53 51 4c 49 54 H12290].*/.SQLIT
1bdd0 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 E_API SQLITE_EXP
1bde0 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 2a ERIMENTAL void *
1bdf0 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73 71 sqlite3_trace(sq
1be00 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78 54 lite3*, void(*xT
1be10 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 race)(void*,cons
1be20 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a 29 t char*), void*)
1be30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c ;.SQLITE_API SQL
1be40 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c ITE_EXPERIMENTAL
1be50 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 void *sqlite3_p
1be60 72 6f 66 69 6c 65 28 73 71 6c 69 74 65 33 2a 2c rofile(sqlite3*,
1be70 0a 20 20 20 76 6f 69 64 28 2a 78 50 72 6f 66 69 . void(*xProfi
1be80 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 le)(void*,const
1be90 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 75 69 char*,sqlite3_ui
1bea0 6e 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a nt64), void*);..
1beb0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
1bec0 51 75 65 72 79 20 50 72 6f 67 72 65 73 73 20 43 Query Progress C
1bed0 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 31 30 allbacks {H12910
1bee0 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a } <S60400>.**.**
1bef0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f This routine co
1bf00 6e 66 69 67 75 72 65 73 20 61 20 63 61 6c 6c 62 nfigures a callb
1bf10 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d 20 74 ack function - t
1bf20 68 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73 20 63 he.** progress c
1bf30 61 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74 20 69 allback - that i
1bf40 73 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 6f 64 s invoked period
1bf50 69 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 6c 6f ically during lo
1bf60 6e 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 63 61 ng.** running ca
1bf70 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f lls to [sqlite3_
1bf80 65 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65 exec()], [sqlite
1bf90 33 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a 2a 2a 3_step()] and.**
1bfa0 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 [sqlite3_get_ta
1bfb0 62 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78 61 6d ble()]. An exam
1bfc0 70 6c 65 20 75 73 65 20 66 6f 72 20 74 68 69 73 ple use for this
1bfd0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 .** interface is
1bfe0 20 74 6f 20 6b 65 65 70 20 61 20 47 55 49 20 75 to keep a GUI u
1bff0 70 64 61 74 65 64 20 64 75 72 69 6e 67 20 61 20 pdated during a
1c000 6c 61 72 67 65 20 71 75 65 72 79 2e 0a 2a 2a 0a large query..**.
1c010 2a 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 65 ** If the progre
1c020 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 ss callback retu
1c030 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 rns non-zero, th
1c040 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a e operation is.*
1c050 2a 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20 20 * interrupted.
1c060 54 68 69 73 20 66 65 61 74 75 72 65 20 63 61 6e This feature can
1c070 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c be used to impl
1c080 65 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e 63 ement a.** "Canc
1c090 65 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61 20 el" button on a
1c0a0 47 55 49 20 70 72 6f 67 72 65 73 73 20 64 69 61 GUI progress dia
1c0b0 6c 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20 54 log box..**.** T
1c0c0 68 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 he progress hand
1c0d0 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 ler must not do
1c0e0 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 anything that wi
1c0f0 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 ll modify.** the
1c100 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1c110 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 tion that invoke
1c120 64 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 68 d the progress h
1c130 61 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74 65 20 andler..** Note
1c140 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 that [sqlite3_pr
1c150 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 epare_v2()] and
1c160 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
1c170 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 both modify the
1c180 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 ir.** database c
1c190 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 onnections for t
1c1a0 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d he meaning of "m
1c1b0 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 odify" in this p
1c1c0 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 aragraph..**.**
1c1d0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
1c1e0 20 5b 48 31 32 39 31 31 5d 20 5b 48 31 32 39 31 [H12911] [H1291
1c1f0 32 5d 20 5b 48 31 32 39 31 33 5d 20 5b 48 31 32 2] [H12913] [H12
1c200 39 31 34 5d 20 5b 48 31 32 39 31 35 5d 20 5b 48 914] [H12915] [H
1c210 31 32 39 31 36 5d 20 5b 48 31 32 39 31 37 5d 20 12916] [H12917]
1c220 5b 48 31 32 39 31 38 5d 0a 2a 2a 0a 2a 2f 0a 53 [H12918].**.*/.S
1c230 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
1c240 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f qlite3_progress_
1c250 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33 2a handler(sqlite3*
1c260 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28 76 6f , int, int(*)(vo
1c270 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f id*), void*);../
1c280 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f *.** CAPI3REF: O
1c290 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 61 74 pening A New Dat
1c2a0 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e abase Connection
1c2b0 20 7b 48 31 32 37 30 30 7d 20 3c 53 34 30 32 30 {H12700} <S4020
1c2c0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 0>.**.** These r
1c2d0 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 61 6e 20 outines open an
1c2e0 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 SQLite database
1c2f0 66 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d 65 20 file whose name
1c300 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65 0a is given by the.
1c310 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 ** filename argu
1c320 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c 65 6e 61 ment. The filena
1c330 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 69 me argument is i
1c340 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 55 54 nterpreted as UT
1c350 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 F-8 for.** sqlit
1c360 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 e3_open() and sq
1c370 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 lite3_open_v2()
1c380 61 6e 64 20 61 73 20 55 54 46 2d 31 36 20 69 6e and as UTF-16 in
1c390 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 the native byte
1c3a0 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20 73 71 .** order for sq
1c3b0 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e 20 lite3_open16().
1c3c0 41 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e A [database conn
1c3d0 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20 69 ection] handle i
1c3e0 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72 65 74 s usually.** ret
1c3f0 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 62 2c 20 urned in *ppDb,
1c400 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 even if an error
1c410 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 6f 6e occurs. The on
1c420 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20 ly exception is
1c430 74 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c 69 74 that.** if SQLit
1c440 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 e is unable to a
1c450 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 llocate memory t
1c460 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 71 6c 69 o hold the [sqli
1c470 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a 2a 20 te3] object,.**
1c480 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 20 77 a NULL will be w
1c490 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 70 44 ritten into *ppD
1c4a0 62 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 70 b instead of a p
1c4b0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73 ointer to the [s
1c4c0 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a 65 63 qlite3].** objec
1c4d0 74 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 61 t. If the databa
1c4e0 73 65 20 69 73 20 6f 70 65 6e 65 64 20 28 61 6e se is opened (an
1c4f0 64 2f 6f 72 20 63 72 65 61 74 65 64 29 20 73 75 d/or created) su
1c500 63 63 65 73 73 66 75 6c 6c 79 2c 20 74 68 65 6e ccessfully, then
1c510 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 .** [SQLITE_OK]
1c520 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f 74 is returned. Ot
1c530 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f herwise an [erro
1c540 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 r code] is retur
1c550 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b 73 71 ned. The.** [sq
1c560 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 20 lite3_errmsg()]
1c570 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d or [sqlite3_errm
1c580 73 67 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 73 sg16()] routines
1c590 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
1c5a0 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45 6e 67 obtain.** an Eng
1c5b0 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20 64 65 lish language de
1c5c0 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 scription of the
1c5d0 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 error..**.** Th
1c5e0 65 20 64 65 66 61 75 6c 74 20 65 6e 63 6f 64 69 e default encodi
1c5f0 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 ng for the datab
1c600 61 73 65 20 77 69 6c 6c 20 62 65 20 55 54 46 2d ase will be UTF-
1c610 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 8 if.** sqlite3_
1c620 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 open() or sqlite
1c630 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 63 3_open_v2() is c
1c640 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55 54 46 alled and.** UTF
1c650 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 69 76 -16 in the nativ
1c660 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 66 20 e byte order if
1c670 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 sqlite3_open16()
1c680 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 is used..**.**
1c690 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 Whether or not a
1c6a0 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 n error occurs w
1c6b0 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 64 hen it is opened
1c6c0 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a 20 61 , resources.** a
1c6d0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
1c6e0 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
1c6f0 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20 nection] handle
1c700 73 68 6f 75 6c 64 20 62 65 20 72 65 6c 65 61 73 should be releas
1c710 65 64 20 62 79 0a 2a 2a 20 70 61 73 73 69 6e 67 ed by.** passing
1c720 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f it to [sqlite3_
1c730 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20 69 74 close()] when it
1c740 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 is no longer re
1c750 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 quired..**.** Th
1c760 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 e sqlite3_open_v
1c770 32 28 29 20 69 6e 74 65 72 66 61 63 65 20 77 6f 2() interface wo
1c780 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 rks like sqlite3
1c790 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63 65 70 _open().** excep
1c7a0 74 20 74 68 61 74 20 69 74 20 61 63 63 65 70 74 t that it accept
1c7b0 73 20 74 77 6f 20 61 64 64 69 74 69 6f 6e 61 6c s two additional
1c7c0 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 parameters for
1c7d0 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e 74 72 additional contr
1c7e0 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65 20 6e ol.** over the n
1c7f0 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ew database conn
1c800 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 6c 61 ection. The fla
1c810 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 61 6e gs parameter can
1c820 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 take one of.**
1c830 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 the following th
1c840 72 65 65 20 76 61 6c 75 65 73 2c 20 6f 70 74 69 ree values, opti
1c850 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65 64 20 onally combined
1c860 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b 53 51 with the .** [SQ
1c870 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 LITE_OPEN_NOMUTE
1c880 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e X], [SQLITE_OPEN
1c890 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 20 5b 53 51 _FULLMUTEX], [SQ
1c8a0 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 LITE_OPEN_SHARED
1c8b0 43 41 43 48 45 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f CACHE],.** and/o
1c8c0 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 r [SQLITE_OPEN_P
1c8d0 52 49 56 41 54 45 43 41 43 48 45 5d 20 66 6c 61 RIVATECACHE] fla
1c8e0 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a gs:.**.** <dl>.*
1c8f0 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 * <dt>[SQLITE_OP
1c900 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c 2f 64 74 EN_READONLY]</dt
1c910 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74 >.** <dd>The dat
1c920 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 abase is opened
1c930 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 in read-only mod
1c940 65 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 62 e. If the datab
1c950 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 ase does not.**
1c960 61 6c 72 65 61 64 79 20 65 78 69 73 74 2c 20 61 already exist, a
1c970 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 n error is retur
1c980 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ned.</dd>.**.**
1c990 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e <dt>[SQLITE_OPEN
1c9a0 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f 64 74 3e _READWRITE]</dt>
1c9b0 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74 61 .** <dd>The data
1c9c0 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66 base is opened f
1c9d0 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 or reading and w
1c9e0 72 69 74 69 6e 67 20 69 66 20 70 6f 73 73 69 62 riting if possib
1c9f0 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e 67 0a 2a le, or reading.*
1ca00 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 66 69 * only if the fi
1ca10 6c 65 20 69 73 20 77 72 69 74 65 20 70 72 6f 74 le is write prot
1ca20 65 63 74 65 64 20 62 79 20 74 68 65 20 6f 70 65 ected by the ope
1ca30 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 rating system.
1ca40 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63 61 73 In either.** cas
1ca50 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d e the database m
1ca60 75 73 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 ust already exis
1ca70 74 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 t, otherwise an
1ca80 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 error is returne
1ca90 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 d.</dd>.**.** <d
1caa0 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 t>[SQLITE_OPEN_R
1cab0 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 51 4c EADWRITE] | [SQL
1cac0 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d ITE_OPEN_CREATE]
1cad0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 </dt>.** <dd>The
1cae0 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 database is ope
1caf0 6e 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20 ned for reading
1cb00 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 61 6e 64 and writing, and
1cb10 20 69 73 20 63 72 65 61 74 65 73 20 69 74 20 69 is creates it i
1cb20 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e 6f 74 f.** it does not
1cb30 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e 20 already exist.
1cb40 54 68 69 73 20 69 73 20 74 68 65 20 62 65 68 61 This is the beha
1cb50 76 69 6f 72 20 74 68 61 74 20 69 73 20 61 6c 77 vior that is alw
1cb60 61 79 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 ays used for.**
1cb70 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 61 sqlite3_open() a
1cb80 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 nd sqlite3_open1
1cb90 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6().</dd>.** </d
1cba0 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 l>.**.** If the
1cbb0 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 3rd parameter to
1cbc0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 sqlite3_open_v2
1cbd0 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20 6f 66 () is not one of
1cbe0 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e 61 74 the.** combinat
1cbf0 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f 76 65 ions shown above
1cc00 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 or one of the c
1cc10 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68 6f 77 ombinations show
1cc20 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69 6e 65 64 n above combined
1cc30 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 53 51 .** with the [SQ
1cc40 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 LITE_OPEN_NOMUTE
1cc50 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e X], [SQLITE_OPEN
1cc60 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 0a 2a 2a 20 _FULLMUTEX],.**
1cc70 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 [SQLITE_OPEN_SHA
1cc80 52 45 44 43 41 43 48 45 5d 20 61 6e 64 2f 6f 72 REDCACHE] and/or
1cc90 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 [SQLITE_OPEN_SH
1cca0 41 52 45 44 43 41 43 48 45 5d 20 66 6c 61 67 73 AREDCACHE] flags
1ccb0 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 62 65 ,.** then the be
1ccc0 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 havior is undefi
1ccd0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ned..**.** If th
1cce0 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e e [SQLITE_OPEN_N
1ccf0 4f 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 20 OMUTEX] flag is
1cd00 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 64 61 set, then the da
1cd10 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1cd20 6e 0a 2a 2a 20 6f 70 65 6e 73 20 69 6e 20 74 68 n.** opens in th
1cd30 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 20 5b e multi-thread [
1cd40 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 threading mode]
1cd50 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 73 as long as the s
1cd60 69 6e 67 6c 65 2d 74 68 72 65 61 64 0a 2a 2a 20 ingle-thread.**
1cd70 6d 6f 64 65 20 68 61 73 20 6e 6f 74 20 62 65 65 mode has not bee
1cd80 6e 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 n set at compile
1cd90 2d 74 69 6d 65 20 6f 72 20 73 74 61 72 74 2d 74 -time or start-t
1cda0 69 6d 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 ime. If the.**
1cdb0 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c [SQLITE_OPEN_FUL
1cdc0 4c 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 20 LMUTEX] flag is
1cdd0 73 65 74 20 74 68 65 6e 20 74 68 65 20 64 61 74 set then the dat
1cde0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1cdf0 20 6f 70 65 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 opens.** in the
1ce00 20 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 serialized [thr
1ce10 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 75 6e 6c eading mode] unl
1ce20 65 73 73 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 ess single-threa
1ce30 64 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 d was.** previou
1ce40 73 6c 79 20 73 65 6c 65 63 74 65 64 20 61 74 20 sly selected at
1ce50 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 20 compile-time or
1ce60 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 20 54 start-time..** T
1ce70 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f he [SQLITE_OPEN_
1ce80 53 48 41 52 45 44 43 41 43 48 45 5d 20 66 6c 61 SHAREDCACHE] fla
1ce90 67 20 63 61 75 73 65 73 20 74 68 65 20 64 61 74 g causes the dat
1cea0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1ceb0 20 74 6f 20 62 65 0a 2a 2a 20 65 6c 69 67 69 62 to be.** eligib
1cec0 6c 65 20 74 6f 20 75 73 65 20 5b 73 68 61 72 65 le to use [share
1ced0 64 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 72 d cache mode], r
1cee0 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 egardless of whe
1cef0 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 68 61 72 ther or not shar
1cf00 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 73 20 65 ed.** cache is e
1cf10 6e 61 62 6c 65 64 20 75 73 69 6e 67 20 5b 73 71 nabled using [sq
1cf20 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 lite3_enable_sha
1cf30 72 65 64 5f 63 61 63 68 65 28 29 5d 2e 20 20 54 red_cache()]. T
1cf40 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 he.** [SQLITE_OP
1cf50 45 4e 5f 50 52 49 56 41 54 45 43 41 43 48 45 5d EN_PRIVATECACHE]
1cf60 20 66 6c 61 67 20 63 61 75 73 65 73 20 74 68 65 flag causes the
1cf70 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1cf80 74 69 6f 6e 20 74 6f 20 6e 6f 74 0a 2a 2a 20 70 tion to not.** p
1cf90 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 5b 73 articipate in [s
1cfa0 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 hared cache mode
1cfb0 5d 20 65 76 65 6e 20 69 66 20 69 74 20 69 73 20 ] even if it is
1cfc0 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 enabled..**.** I
1cfd0 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 f the filename i
1cfe0 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 2c 20 74 68 s ":memory:", th
1cff0 65 6e 20 61 20 70 72 69 76 61 74 65 2c 20 74 65 en a private, te
1d000 6d 70 6f 72 61 72 79 20 69 6e 2d 6d 65 6d 6f 72 mporary in-memor
1d010 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 y database.** is
1d020 20 63 72 65 61 74 65 64 20 66 6f 72 20 74 68 65 created for the
1d030 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 connection. Th
1d040 69 73 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 is in-memory dat
1d050 61 62 61 73 65 20 77 69 6c 6c 20 76 61 6e 69 73 abase will vanis
1d060 68 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 64 61 h when.** the da
1d070 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1d080 6e 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 46 75 n is closed. Fu
1d090 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 ture versions of
1d0a0 20 53 51 4c 69 74 65 20 6d 69 67 68 74 0a 2a 2a SQLite might.**
1d0b0 20 6d 61 6b 65 20 75 73 65 20 6f 66 20 61 64 64 make use of add
1d0c0 69 74 69 6f 6e 61 6c 20 73 70 65 63 69 61 6c 20 itional special
1d0d0 66 69 6c 65 6e 61 6d 65 73 20 74 68 61 74 20 62 filenames that b
1d0e0 65 67 69 6e 20 77 69 74 68 20 74 68 65 20 22 3a egin with the ":
1d0f0 22 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 " character..**
1d100 49 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 It is recommende
1d110 64 20 74 68 61 74 20 77 68 65 6e 20 61 20 64 61 d that when a da
1d120 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 tabase filename
1d130 61 63 74 75 61 6c 6c 79 20 64 6f 65 73 20 62 65 actually does be
1d140 67 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 22 3a gin with.** a ":
1d150 22 20 63 68 61 72 61 63 74 65 72 20 79 6f 75 20 " character you
1d160 73 68 6f 75 6c 64 20 70 72 65 66 69 78 20 74 68 should prefix th
1d170 65 20 66 69 6c 65 6e 61 6d 65 20 77 69 74 68 20 e filename with
1d180 61 20 70 61 74 68 6e 61 6d 65 20 73 75 63 68 20 a pathname such
1d190 61 73 0a 2a 2a 20 22 2e 2f 22 20 74 6f 20 61 76 as.** "./" to av
1d1a0 6f 69 64 20 61 6d 62 69 67 75 69 74 79 2e 0a 2a oid ambiguity..*
1d1b0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65 *.** If the file
1d1c0 6e 61 6d 65 20 69 73 20 61 6e 20 65 6d 70 74 79 name is an empty
1d1d0 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 61 20 string, then a
1d1e0 70 72 69 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 private, tempora
1d1f0 72 79 0a 2a 2a 20 6f 6e 2d 64 69 73 6b 20 64 61 ry.** on-disk da
1d200 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 63 tabase will be c
1d210 72 65 61 74 65 64 2e 20 20 54 68 69 73 20 70 72 reated. This pr
1d220 69 76 61 74 65 20 64 61 74 61 62 61 73 65 20 77 ivate database w
1d230 69 6c 6c 20 62 65 0a 2a 2a 20 61 75 74 6f 6d 61 ill be.** automa
1d240 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 tically deleted
1d250 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 64 as soon as the d
1d260 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1d270 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a on is closed..**
1d280 0a 2a 2a 20 54 68 65 20 66 6f 75 72 74 68 20 70 .** The fourth p
1d290 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 arameter to sqli
1d2a0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 te3_open_v2() is
1d2b0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1d2c0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 .** [sqlite3_vfs
1d2d0 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20 64 65 ] object that de
1d2e0 66 69 6e 65 73 20 74 68 65 20 6f 70 65 72 61 74 fines the operat
1d2f0 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 ing system inter
1d300 66 61 63 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 face that.** the
1d310 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f new database co
1d320 6e 6e 65 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 nnection should
1d330 75 73 65 2e 20 20 49 66 20 74 68 65 20 66 6f 75 use. If the fou
1d340 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 rth parameter is
1d350 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 .** a NULL point
1d360 65 72 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 er then the defa
1d370 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 ult [sqlite3_vfs
1d380 5d 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 ] object is used
1d390 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74 65 20 ..**.** <b>Note
1d3a0 74 6f 20 57 69 6e 64 6f 77 73 20 75 73 65 72 73 to Windows users
1d3b0 3a 3c 2f 62 3e 20 20 54 68 65 20 65 6e 63 6f 64 :</b> The encod
1d3c0 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74 68 65 ing used for the
1d3d0 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 filename argume
1d3e0 6e 74 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 65 33 nt.** of sqlite3
1d3f0 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 _open() and sqli
1d400 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 6d 75 te3_open_v2() mu
1d410 73 74 20 62 65 20 55 54 46 2d 38 2c 20 6e 6f 74 st be UTF-8, not
1d420 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 63 6f 64 whatever.** cod
1d430 65 70 61 67 65 20 69 73 20 63 75 72 72 65 6e 74 epage is current
1d440 6c 79 20 64 65 66 69 6e 65 64 2e 20 20 46 69 6c ly defined. Fil
1d450 65 6e 61 6d 65 73 20 63 6f 6e 74 61 69 6e 69 6e enames containin
1d460 67 20 69 6e 74 65 72 6e 61 74 69 6f 6e 61 6c 0a g international.
1d470 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 6d 75 ** characters mu
1d480 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 st be converted
1d490 74 6f 20 55 54 46 2d 38 20 70 72 69 6f 72 20 74 to UTF-8 prior t
1d4a0 6f 20 70 61 73 73 69 6e 67 20 74 68 65 6d 20 69 o passing them i
1d4b0 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f nto.** sqlite3_o
1d4c0 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33 pen() or sqlite3
1d4d0 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a 0a 2a _open_v2()..**.*
1d4e0 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
1d4f0 2a 2a 20 5b 48 31 32 37 30 31 5d 20 5b 48 31 32 ** [H12701] [H12
1d500 37 30 32 5d 20 5b 48 31 32 37 30 33 5d 20 5b 48 702] [H12703] [H
1d510 31 32 37 30 34 5d 20 5b 48 31 32 37 30 36 5d 20 12704] [H12706]
1d520 5b 48 31 32 37 30 37 5d 20 5b 48 31 32 37 30 39 [H12707] [H12709
1d530 5d 20 5b 48 31 32 37 31 31 5d 0a 2a 2a 20 5b 48 ] [H12711].** [H
1d540 31 32 37 31 32 5d 20 5b 48 31 32 37 31 33 5d 20 12712] [H12713]
1d550 5b 48 31 32 37 31 34 5d 20 5b 48 31 32 37 31 37 [H12714] [H12717
1d560 5d 20 5b 48 31 32 37 31 39 5d 20 5b 48 31 32 37 ] [H12719] [H127
1d570 32 31 5d 20 5b 48 31 32 37 32 33 5d 0a 2a 2f 0a 21] [H12723].*/.
1d580 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1d590 71 6c 69 74 65 33 5f 6f 70 65 6e 28 0a 20 20 63 qlite3_open(. c
1d5a0 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e onst char *filen
1d5b0 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 ame, /* Databa
1d5c0 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 se filename (UTF
1d5d0 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 -8) */. sqlite3
1d5e0 20 2a 2a 70 70 44 62 20 20 20 20 20 20 20 20 20 **ppDb
1d5f0 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 /* OUT: SQLite
1d600 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a db handle */.);.
1d610 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1d620 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 0a 20 qlite3_open16(.
1d630 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 66 69 6c const void *fil
1d640 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 ename, /* Data
1d650 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 base filename (U
1d660 54 46 2d 31 36 29 20 2a 2f 0a 20 20 73 71 6c 69 TF-16) */. sqli
1d670 74 65 33 20 2a 2a 70 70 44 62 20 20 20 20 20 20 te3 **ppDb
1d680 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 /* OUT: SQLi
1d690 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a te db handle */.
1d6a0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
1d6b0 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 t sqlite3_open_v
1d6c0 32 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2(. const char
1d6d0 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 *filename, /*
1d6e0 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d Database filenam
1d6f0 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 e (UTF-8) */. s
1d700 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c 20 20 qlite3 **ppDb,
1d710 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 /* OUT: S
1d720 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 QLite db handle
1d730 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 */. int flags,
1d740 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1d750 46 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 Flags */. const
1d760 20 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20 20 char *zVfs
1d770 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 56 46 /* Name of VF
1d780 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65 20 S module to use
1d790 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 */.);../*.** CAP
1d7a0 49 33 52 45 46 3a 20 45 72 72 6f 72 20 43 6f 64 I3REF: Error Cod
1d7b0 65 73 20 41 6e 64 20 4d 65 73 73 61 67 65 73 20 es And Messages
1d7c0 7b 48 31 32 38 30 30 7d 20 3c 53 36 30 32 30 30 {H12800} <S60200
1d7d0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
1d7e0 74 65 33 5f 65 72 72 63 6f 64 65 28 29 20 69 6e te3_errcode() in
1d7f0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
1d800 74 68 65 20 6e 75 6d 65 72 69 63 20 5b 72 65 73 the numeric [res
1d810 75 6c 74 20 63 6f 64 65 5d 20 6f 72 0a 2a 2a 20 ult code] or.**
1d820 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 [extended result
1d830 20 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 6d code] for the m
1d840 6f 73 74 20 72 65 63 65 6e 74 20 66 61 69 6c 65 ost recent faile
1d850 64 20 73 71 6c 69 74 65 33 5f 2a 20 41 50 49 20 d sqlite3_* API
1d860 63 61 6c 6c 0a 2a 2a 20 61 73 73 6f 63 69 61 74 call.** associat
1d870 65 64 20 77 69 74 68 20 61 20 5b 64 61 74 61 62 ed with a [datab
1d880 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e ase connection].
1d890 20 49 66 20 61 20 70 72 69 6f 72 20 41 50 49 20 If a prior API
1d8a0 63 61 6c 6c 20 66 61 69 6c 65 64 0a 2a 2a 20 62 call failed.** b
1d8b0 75 74 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 ut the most rece
1d8c0 6e 74 20 41 50 49 20 63 61 6c 6c 20 73 75 63 63 nt API call succ
1d8d0 65 65 64 65 64 2c 20 74 68 65 20 72 65 74 75 72 eeded, the retur
1d8e0 6e 20 76 61 6c 75 65 20 66 72 6f 6d 0a 2a 2a 20 n value from.**
1d8f0 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 sqlite3_errcode(
1d900 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 ) is undefined.
1d910 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 74 The sqlite3_ext
1d920 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 29 0a ended_errcode().
1d930 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 ** interface is
1d940 74 68 65 20 73 61 6d 65 20 65 78 63 65 70 74 20 the same except
1d950 74 68 61 74 20 69 74 20 61 6c 77 61 79 73 20 72 that it always r
1d960 65 74 75 72 6e 73 20 74 68 65 20 0a 2a 2a 20 5b eturns the .** [
1d970 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 extended result
1d980 63 6f 64 65 5d 20 65 76 65 6e 20 77 68 65 6e 20 code] even when
1d990 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 extended result
1d9a0 63 6f 64 65 73 20 61 72 65 0a 2a 2a 20 64 69 73 codes are.** dis
1d9b0 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 abled..**.** The
1d9c0 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 sqlite3_errmsg(
1d9d0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 ) and sqlite3_er
1d9e0 72 6d 73 67 31 36 28 29 20 72 65 74 75 72 6e 20 rmsg16() return
1d9f0 45 6e 67 6c 69 73 68 2d 6c 61 6e 67 75 61 67 65 English-language
1da00 0a 2a 2a 20 74 65 78 74 20 74 68 61 74 20 64 65 .** text that de
1da10 73 63 72 69 62 65 73 20 74 68 65 20 65 72 72 6f scribes the erro
1da20 72 2c 20 61 73 20 65 69 74 68 65 72 20 55 54 46 r, as either UTF
1da30 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 72 65 73 -8 or UTF-16 res
1da40 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 4d 65 pectively..** Me
1da50 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 mory to hold the
1da60 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 error message s
1da70 74 72 69 6e 67 20 69 73 20 6d 61 6e 61 67 65 64 tring is managed
1da80 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2a 20 internally..**
1da90 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 The application
1daa0 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
1dab0 20 77 6f 72 72 79 20 61 62 6f 75 74 20 66 72 65 worry about fre
1dac0 65 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 2e eing the result.
1dad0 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 .** However, the
1dae0 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 6d 69 error string mi
1daf0 67 68 74 20 62 65 20 6f 76 65 72 77 72 69 74 74 ght be overwritt
1db00 65 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 en or deallocate
1db10 64 20 62 79 0a 2a 2a 20 73 75 62 73 65 71 75 65 d by.** subseque
1db20 6e 74 20 63 61 6c 6c 73 20 74 6f 20 6f 74 68 65 nt calls to othe
1db30 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 r SQLite interfa
1db40 63 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a ce functions..**
1db50 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 73 65 72 .** When the ser
1db60 69 61 6c 69 7a 65 64 20 5b 74 68 72 65 61 64 69 ialized [threadi
1db70 6e 67 20 6d 6f 64 65 5d 20 69 73 20 69 6e 20 75 ng mode] is in u
1db80 73 65 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 se, it might be
1db90 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 the.** case that
1dba0 20 61 20 73 65 63 6f 6e 64 20 65 72 72 6f 72 20 a second error
1dbb0 6f 63 63 75 72 73 20 6f 6e 20 61 20 73 65 70 61 occurs on a sepa
1dbc0 72 61 74 65 20 74 68 72 65 61 64 20 69 6e 20 62 rate thread in b
1dbd0 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 74 69 etween.** the ti
1dbe0 6d 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 me of the first
1dbf0 65 72 72 6f 72 20 61 6e 64 20 74 68 65 20 63 61 error and the ca
1dc00 6c 6c 20 74 6f 20 74 68 65 73 65 20 69 6e 74 65 ll to these inte
1dc10 72 66 61 63 65 73 2e 0a 2a 2a 20 57 68 65 6e 20 rfaces..** When
1dc20 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20 74 68 that happens, th
1dc30 65 20 73 65 63 6f 6e 64 20 65 72 72 6f 72 20 77 e second error w
1dc40 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64 20 ill be reported
1dc50 73 69 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 69 since these.** i
1dc60 6e 74 65 72 66 61 63 65 73 20 61 6c 77 61 79 73 nterfaces always
1dc70 20 72 65 70 6f 72 74 20 74 68 65 20 6d 6f 73 74 report the most
1dc80 20 72 65 63 65 6e 74 20 72 65 73 75 6c 74 2e 20 recent result.
1dc90 20 54 6f 20 61 76 6f 69 64 0a 2a 2a 20 74 68 69 To avoid.** thi
1dca0 73 2c 20 65 61 63 68 20 74 68 72 65 61 64 20 63 s, each thread c
1dcb0 61 6e 20 6f 62 74 61 69 6e 20 65 78 63 6c 75 73 an obtain exclus
1dcc0 69 76 65 20 75 73 65 20 6f 66 20 74 68 65 20 5b ive use of the [
1dcd0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1dce0 69 6f 6e 5d 20 44 0a 2a 2a 20 62 79 20 69 6e 76 ion] D.** by inv
1dcf0 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d oking [sqlite3_m
1dd00 75 74 65 78 5f 65 6e 74 65 72 5d 28 5b 73 71 6c utex_enter]([sql
1dd10 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 44 ite3_db_mutex](D
1dd20 29 29 20 62 65 66 6f 72 65 20 62 65 67 69 6e 6e )) before beginn
1dd30 69 6e 67 0a 2a 2a 20 74 6f 20 75 73 65 20 44 20 ing.** to use D
1dd40 61 6e 64 20 69 6e 76 6f 6b 69 6e 67 20 5b 73 71 and invoking [sq
1dd50 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1dd60 65 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62 5f 6d e]([sqlite3_db_m
1dd70 75 74 65 78 5d 28 44 29 29 20 61 66 74 65 72 0a utex](D)) after.
1dd80 2a 2a 20 61 6c 6c 20 63 61 6c 6c 73 20 74 6f 20 ** all calls to
1dd90 74 68 65 20 69 6e 74 65 72 66 61 63 65 73 20 6c the interfaces l
1dda0 69 73 74 65 64 20 68 65 72 65 20 61 72 65 20 63 isted here are c
1ddb0 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 ompleted..**.**
1ddc0 49 66 20 61 6e 20 69 6e 74 65 72 66 61 63 65 20 If an interface
1ddd0 66 61 69 6c 73 20 77 69 74 68 20 53 51 4c 49 54 fails with SQLIT
1dde0 45 5f 4d 49 53 55 53 45 2c 20 74 68 61 74 20 6d E_MISUSE, that m
1ddf0 65 61 6e 73 20 74 68 65 20 69 6e 74 65 72 66 61 eans the interfa
1de00 63 65 0a 2a 2a 20 77 61 73 20 69 6e 76 6f 6b 65 ce.** was invoke
1de10 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 62 79 d incorrectly by
1de20 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
1de30 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c . In that case,
1de40 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f the.** error co
1de50 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 20 6d de and message m
1de60 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 ay or may not be
1de70 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 set..**.** Requ
1de80 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
1de90 32 38 30 31 5d 20 5b 48 31 32 38 30 32 5d 20 5b 2801] [H12802] [
1dea0 48 31 32 38 30 33 5d 20 5b 48 31 32 38 30 37 5d H12803] [H12807]
1deb0 20 5b 48 31 32 38 30 38 5d 20 5b 48 31 32 38 30 [H12808] [H1280
1dec0 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 9].*/.SQLITE_API
1ded0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 int sqlite3_err
1dee0 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 code(sqlite3 *db
1def0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
1df00 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 t sqlite3_extend
1df10 65 64 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 ed_errcode(sqlit
1df20 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f e3 *db);.SQLITE_
1df30 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
1df40 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 sqlite3_errmsg(s
1df50 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 qlite3*);.SQLITE
1df60 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
1df70 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 *sqlite3_errmsg1
1df80 36 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 6(sqlite3*);../*
1df90 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 .** CAPI3REF: SQ
1dfa0 4c 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 L Statement Obje
1dfb0 63 74 20 7b 48 31 33 30 30 30 7d 20 3c 48 31 33 ct {H13000} <H13
1dfc0 30 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 010>.** KEYWORDS
1dfd0 3a 20 7b 70 72 65 70 61 72 65 64 20 73 74 61 74 : {prepared stat
1dfe0 65 6d 65 6e 74 7d 20 7b 70 72 65 70 61 72 65 64 ement} {prepared
1dff0 20 73 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a 0a statements}.**.
1e000 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
1e010 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 f this object re
1e020 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c presents a singl
1e030 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e e SQL statement.
1e040 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20 .** This object
1e050 69 73 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e 6f is variously kno
1e060 77 6e 20 61 73 20 61 20 22 70 72 65 70 61 72 65 wn as a "prepare
1e070 64 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 d statement" or
1e080 61 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20 53 a.** "compiled S
1e090 51 4c 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 QL statement" or
1e0a0 20 73 69 6d 70 6c 79 20 61 73 20 61 20 22 73 74 simply as a "st
1e0b0 61 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a 20 atement"..**.**
1e0c0 54 68 65 20 6c 69 66 65 20 6f 66 20 61 20 73 74 The life of a st
1e0d0 61 74 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20 67 atement object g
1e0e0 6f 65 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 oes something li
1e0f0 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c ke this:.**.** <
1e100 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65 61 ol>.** <li> Crea
1e110 74 65 20 74 68 65 20 6f 62 6a 65 63 74 20 75 73 te the object us
1e120 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 ing [sqlite3_pre
1e130 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 61 20 pare_v2()] or a
1e140 72 65 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 related.**
1e150 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c 69 function..** <li
1e160 3e 20 42 69 6e 64 20 76 61 6c 75 65 73 20 74 6f > Bind values to
1e170 20 5b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 [host parameter
1e180 73 5d 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c s] using the sql
1e190 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a 2a ite3_bind_*().**
1e1a0 20 20 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 interfaces
1e1b0 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74 68 ..** <li> Run th
1e1c0 65 20 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e 67 e SQL by calling
1e1d0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
1e1e0 5d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 ] one or more ti
1e1f0 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65 73 mes..** <li> Res
1e200 65 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 et the statement
1e210 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
1e220 72 65 73 65 74 28 29 5d 20 74 68 65 6e 20 67 6f reset()] then go
1e230 20 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74 6f back.** to
1e240 20 73 74 65 70 20 32 2e 20 20 44 6f 20 74 68 69 step 2. Do thi
1e250 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 74 s zero or more t
1e260 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44 65 imes..** <li> De
1e270 73 74 72 6f 79 20 74 68 65 20 6f 62 6a 65 63 74 stroy the object
1e280 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
1e290 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 finalize()]..**
1e2a0 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 </ol>.**.** Refe
1e2b0 72 20 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74 69 r to documentati
1e2c0 6f 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 6c on on individual
1e2d0 20 6d 65 74 68 6f 64 73 20 61 62 6f 76 65 20 66 methods above f
1e2e0 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a or additional.**
1e2f0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f information..*/
1e300 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
1e310 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 73 71 6c sqlite3_stmt sql
1e320 69 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a 2a ite3_stmt;../*.*
1e330 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d * CAPI3REF: Run-
1e340 74 69 6d 65 20 4c 69 6d 69 74 73 20 7b 48 31 32 time Limits {H12
1e350 37 36 30 7d 20 3c 53 32 30 36 30 30 3e 0a 2a 2a 760} <S20600>.**
1e360 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 .** This interfa
1e370 63 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 73 69 ce allows the si
1e380 7a 65 20 6f 66 20 76 61 72 69 6f 75 73 20 63 6f ze of various co
1e390 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 6c nstructs to be l
1e3a0 69 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20 63 imited.** on a c
1e3b0 6f 6e 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f 6e onnection by con
1e3c0 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 2e 20 20 nection basis.
1e3d0 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 The first parame
1e3e0 74 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b 64 ter is the.** [d
1e3f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1e400 6f 6e 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74 20 on] whose limit
1e410 69 73 20 74 6f 20 62 65 20 73 65 74 20 6f 72 20 is to be set or
1e420 71 75 65 72 69 65 64 2e 20 20 54 68 65 0a 2a 2a queried. The.**
1e430 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
1e440 72 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 r is one of the
1e450 5b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 [limit categorie
1e460 73 5d 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 s] that define a
1e470 0a 2a 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f 6e .** class of con
1e480 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 73 69 structs to be si
1e490 7a 65 20 6c 69 6d 69 74 65 64 2e 20 20 54 68 65 ze limited. The
1e4a0 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 third parameter
1e4b0 20 69 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c is the.** new l
1e4c0 69 6d 69 74 20 66 6f 72 20 74 68 61 74 20 63 6f imit for that co
1e4d0 6e 73 74 72 75 63 74 2e 20 20 54 68 65 20 66 75 nstruct. The fu
1e4e0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 nction returns t
1e4f0 68 65 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a 2a 2a he old limit..**
1e500 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65 77 20 6c .** If the new l
1e510 69 6d 69 74 20 69 73 20 61 20 6e 65 67 61 74 69 imit is a negati
1e520 76 65 20 6e 75 6d 62 65 72 2c 20 74 68 65 20 6c ve number, the l
1e530 69 6d 69 74 20 69 73 20 75 6e 63 68 61 6e 67 65 imit is unchange
1e540 64 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6c 69 d..** For the li
1e550 6d 69 74 20 63 61 74 65 67 6f 72 79 20 6f 66 20 mit category of
1e560 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 59 5a SQLITE_LIMIT_XYZ
1e570 20 74 68 65 72 65 20 69 73 20 61 20 0a 2a 2a 20 there is a .**
1e580 5b 6c 69 6d 69 74 73 20 7c 20 68 61 72 64 20 75 [limits | hard u
1e590 70 70 65 72 20 62 6f 75 6e 64 5d 0a 2a 2a 20 73 pper bound].** s
1e5a0 65 74 20 62 79 20 61 20 63 6f 6d 70 69 6c 65 2d et by a compile-
1e5b0 74 69 6d 65 20 43 20 70 72 65 70 72 6f 63 65 73 time C preproces
1e5c0 73 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65 64 20 sor macro named
1e5d0 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 53 51 .** [limits | SQ
1e5e0 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 5d 2e 0a 2a LITE_MAX_XYZ]..*
1e5f0 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54 5f 22 * (The "_LIMIT_"
1e600 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69 73 20 in the name is
1e610 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d 41 58 changed to "_MAX
1e620 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 74 73 _".).** Attempts
1e630 20 74 6f 20 69 6e 63 72 65 61 73 65 20 61 20 6c to increase a l
1e640 69 6d 69 74 20 61 62 6f 76 65 20 69 74 73 20 68 imit above its h
1e650 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64 20 ard upper bound
1e660 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c 79 20 are.** silently
1e670 74 72 75 6e 63 61 74 65 64 20 74 6f 20 74 68 65 truncated to the
1e680 20 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69 hard upper limi
1e690 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 69 6d t..**.** Run tim
1e6a0 65 20 6c 69 6d 69 74 73 20 61 72 65 20 69 6e 74 e limits are int
1e6b0 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e ended for use in
1e6c0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 applications th
1e6d0 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 6f 74 at manage.** bot
1e6e0 68 20 74 68 65 69 72 20 6f 77 6e 20 69 6e 74 65 h their own inte
1e6f0 72 6e 61 6c 20 64 61 74 61 62 61 73 65 20 61 6e rnal database an
1e700 64 20 61 6c 73 6f 20 64 61 74 61 62 61 73 65 73 d also databases
1e710 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 72 6f that are contro
1e720 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74 72 75 lled.** by untru
1e730 73 74 65 64 20 65 78 74 65 72 6e 61 6c 20 73 6f sted external so
1e740 75 72 63 65 73 2e 20 20 41 6e 20 65 78 61 6d 70 urces. An examp
1e750 6c 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d le application m
1e760 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 77 65 62 ight be a.** web
1e770 20 62 72 6f 77 73 65 72 20 74 68 61 74 20 68 61 browser that ha
1e780 73 20 69 74 73 20 6f 77 6e 20 64 61 74 61 62 61 s its own databa
1e790 73 65 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 ses for storing
1e7a0 68 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a 20 73 history and.** s
1e7b0 65 70 61 72 61 74 65 20 64 61 74 61 62 61 73 65 eparate database
1e7c0 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 s controlled by
1e7d0 4a 61 76 61 53 63 72 69 70 74 20 61 70 70 6c 69 JavaScript appli
1e7e0 63 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f 61 64 cations download
1e7f0 65 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20 49 6e ed.** off the In
1e800 74 65 72 6e 65 74 2e 20 20 54 68 65 20 69 6e 74 ternet. The int
1e810 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 73 20 ernal databases
1e820 63 61 6e 20 62 65 20 67 69 76 65 6e 20 74 68 65 can be given the
1e830 0a 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66 61 75 .** large, defau
1e840 6c 74 20 6c 69 6d 69 74 73 2e 20 20 44 61 74 61 lt limits. Data
1e850 62 61 73 65 73 20 6d 61 6e 61 67 65 64 20 62 79 bases managed by
1e860 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 external source
1e870 73 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69 76 65 s can.** be give
1e880 6e 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 6c n much smaller l
1e890 69 6d 69 74 73 20 64 65 73 69 67 6e 65 64 20 74 imits designed t
1e8a0 6f 20 70 72 65 76 65 6e 74 20 61 20 64 65 6e 69 o prevent a deni
1e8b0 61 6c 20 6f 66 20 73 65 72 76 69 63 65 0a 2a 2a al of service.**
1e8c0 20 61 74 74 61 63 6b 2e 20 20 44 65 76 65 6c 6f attack. Develo
1e8d0 70 65 72 73 20 6d 69 67 68 74 20 61 6c 73 6f 20 pers might also
1e8e0 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 want to use the
1e8f0 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 [sqlite3_set_aut
1e900 68 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 69 6e horizer()].** in
1e910 74 65 72 66 61 63 65 20 74 6f 20 66 75 72 74 68 terface to furth
1e920 65 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 72 75 er control untru
1e930 73 74 65 64 20 53 51 4c 2e 20 20 54 68 65 20 73 sted SQL. The s
1e940 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
1e950 61 73 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 ase.** created b
1e960 79 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 73 y an untrusted s
1e970 63 72 69 70 74 20 63 61 6e 20 62 65 20 63 6f 6e cript can be con
1e980 74 61 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 tained using the
1e990 0a 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f .** [max_page_co
1e9a0 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e 0a 2a unt] [PRAGMA]..*
1e9b0 2a 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 69 6d *.** New run-tim
1e9c0 65 20 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 e limit categori
1e9d0 65 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 es may be added
1e9e0 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 in future releas
1e9f0 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 es..**.** Requir
1ea00 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 37 ements:.** [H127
1ea10 36 32 5d 20 5b 48 31 32 37 36 36 5d 20 5b 48 31 62] [H12766] [H1
1ea20 32 37 36 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 2769].*/.SQLITE_
1ea30 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1ea40 6c 69 6d 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 limit(sqlite3*,
1ea50 69 6e 74 20 69 64 2c 20 69 6e 74 20 6e 65 77 56 int id, int newV
1ea60 61 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 al);../*.** CAPI
1ea70 33 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 3REF: Run-Time L
1ea80 69 6d 69 74 20 43 61 74 65 67 6f 72 69 65 73 20 imit Categories
1ea90 7b 48 31 32 37 39 30 7d 20 3c 48 31 32 37 36 30 {H12790} <H12760
1eaa0 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b >.** KEYWORDS: {
1eab0 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79 7d 20 limit category}
1eac0 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 {limit categorie
1ead0 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 s}.**.** These c
1eae0 6f 6e 73 74 61 6e 74 73 20 64 65 66 69 6e 65 20 onstants define
1eaf0 76 61 72 69 6f 75 73 20 70 65 72 66 6f 72 6d 61 various performa
1eb00 6e 63 65 20 6c 69 6d 69 74 73 0a 2a 2a 20 74 68 nce limits.** th
1eb10 61 74 20 63 61 6e 20 62 65 20 6c 6f 77 65 72 65 at can be lowere
1eb20 64 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 73 d at run-time us
1eb30 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d ing [sqlite3_lim
1eb40 69 74 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 79 it()]..** The sy
1eb50 6e 6f 70 73 69 73 20 6f 66 20 74 68 65 20 6d 65 nopsis of the me
1eb60 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61 anings of the va
1eb70 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 69 73 20 rious limits is
1eb80 73 68 6f 77 6e 20 62 65 6c 6f 77 2e 0a 2a 2a 20 shown below..**
1eb90 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 Additional infor
1eba0 6d 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 mation is availa
1ebb0 62 6c 65 20 61 74 20 5b 6c 69 6d 69 74 73 20 7c ble at [limits |
1ebc0 20 4c 69 6d 69 74 73 20 69 6e 20 53 51 4c 69 74 Limits in SQLit
1ebd0 65 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a e]..**.** <dl>.*
1ebe0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d * <dt>SQLITE_LIM
1ebf0 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a IT_LENGTH</dt>.*
1ec00 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
1ec10 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 73 74 m size of any st
1ec20 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 6f 72 20 ring or BLOB or
1ec30 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64 3e 0a 2a table row.<dd>.*
1ec40 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
1ec50 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 LIMIT_SQL_LENGTH
1ec60 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 </dt>.** <dd>The
1ec70 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 maximum length
1ec80 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d of an SQL statem
1ec90 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ent.</dd>.**.**
1eca0 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 <dt>SQLITE_LIMIT
1ecb0 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a 2a 20 _COLUMN</dt>.**
1ecc0 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 <dd>The maximum
1ecd0 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
1ece0 73 20 69 6e 20 61 20 74 61 62 6c 65 20 64 65 66 s in a table def
1ecf0 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e 20 74 68 inition or in th
1ed00 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20 e.** result set
1ed10 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 6f 72 of a [SELECT] or
1ed20 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d the maximum num
1ed30 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
1ed40 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 6f 72 n an index.** or
1ed50 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20 in an ORDER BY
1ed60 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 or GROUP BY clau
1ed70 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c se.</dd>.**.** <
1ed80 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f dt>SQLITE_LIMIT_
1ed90 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74 3e 0a EXPR_DEPTH</dt>.
1eda0 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d ** <dd>The maxim
1edb0 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 um depth of the
1edc0 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20 61 6e parse tree on an
1edd0 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c 2f 64 y expression.</d
1ede0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
1edf0 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 ITE_LIMIT_COMPOU
1ee00 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a 2a ND_SELECT</dt>.*
1ee10 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
1ee20 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d m number of term
1ee30 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 s in a compound
1ee40 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
1ee50 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
1ee60 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44 >SQLITE_LIMIT_VD
1ee70 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 BE_OP</dt>.** <d
1ee80 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 d>The maximum nu
1ee90 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63 74 mber of instruct
1eea0 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74 75 61 ions in a virtua
1eeb0 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67 72 61 l machine progra
1eec0 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6d 70 m.** used to imp
1eed0 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20 73 74 lement an SQL st
1eee0 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a atement.</dd>.**
1eef0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c .** <dt>SQLITE_L
1ef00 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 IMIT_FUNCTION_AR
1ef10 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 G</dt>.** <dd>Th
1ef20 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
1ef30 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e of arguments on
1ef40 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f 64 64 a function.</dd
1ef50 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
1ef60 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 TE_LIMIT_ATTACHE
1ef70 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 D</dt>.** <dd>Th
1ef80 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
1ef90 20 6f 66 20 5b 41 54 54 41 43 48 20 7c 20 61 74 of [ATTACH | at
1efa0 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 tached databases
1efb0 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 ].</dd>.**.** <d
1efc0 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t>SQLITE_LIMIT_L
1efd0 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 IKE_PATTERN_LENG
1efe0 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 TH</dt>.** <dd>T
1eff0 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 he maximum lengt
1f000 68 20 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e h of the pattern
1f010 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
1f020 20 5b 4c 49 4b 45 5d 20 6f 72 0a 2a 2a 20 5b 47 [LIKE] or.** [G
1f030 4c 4f 42 5d 20 6f 70 65 72 61 74 6f 72 73 2e 3c LOB] operators.<
1f040 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
1f050 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 QLITE_LIMIT_VARI
1f060 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 74 3e ABLE_NUMBER</dt>
1f070 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 .** <dd>The maxi
1f080 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 mum number of va
1f090 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20 53 51 riables in an SQ
1f0a0 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 L statement that
1f0b0 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75 6e 64 can.** be bound
1f0c0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
1f0d0 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 >SQLITE_LIMIT_TR
1f0e0 49 47 47 45 52 5f 44 45 50 54 48 3c 2f 64 74 3e IGGER_DEPTH</dt>
1f0f0 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 .** <dd>The maxi
1f100 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 72 65 63 mum depth of rec
1f110 75 72 73 69 6f 6e 20 66 6f 72 20 74 72 69 67 67 ursion for trigg
1f120 65 72 73 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 ers.</dd>.** </d
1f130 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 l>.*/.#define SQ
1f140 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
1f150 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 H
1f160 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 0.#define S
1f170 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f QLITE_LIMIT_SQL_
1f180 4c 45 4e 47 54 48 20 20 20 20 20 20 20 20 20 20 LENGTH
1f190 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 1.#define
1f1a0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c SQLITE_LIMIT_COL
1f1b0 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 UMN
1f1c0 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 2.#define
1f1d0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 SQLITE_LIMIT_EX
1f1e0 50 52 5f 44 45 50 54 48 20 20 20 20 20 20 20 20 PR_DEPTH
1f1f0 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 3.#defin
1f200 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 e SQLITE_LIMIT_C
1f210 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 20 OMPOUND_SELECT
1f220 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 4.#defi
1f230 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f ne SQLITE_LIMIT_
1f240 56 44 42 45 5f 4f 50 20 20 20 20 20 20 20 20 20 VDBE_OP
1f250 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 5.#def
1f260 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 ine SQLITE_LIMIT
1f270 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20 20 _FUNCTION_ARG
1f280 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 6.#de
1f290 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 fine SQLITE_LIMI
1f2a0 54 5f 41 54 54 41 43 48 45 44 20 20 20 20 20 20 T_ATTACHED
1f2b0 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 7.#d
1f2c0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d efine SQLITE_LIM
1f2d0 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f IT_LIKE_PATTERN_
1f2e0 4c 45 4e 47 54 48 20 20 20 20 20 20 20 38 0a 23 LENGTH 8.#
1f2f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 define SQLITE_LI
1f300 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d MIT_VARIABLE_NUM
1f310 42 45 52 20 20 20 20 20 20 20 20 20 20 20 39 0a BER 9.
1f320 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
1f330 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50 IMIT_TRIGGER_DEP
1f340 54 48 20 20 20 20 20 20 20 20 20 20 20 20 31 30 TH 10
1f350 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
1f360 3a 20 43 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 53 : Compiling An S
1f370 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 QL Statement {H1
1f380 33 30 31 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 3010} <S10000>.*
1f390 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c * KEYWORDS: {SQL
1f3a0 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 statement compi
1f3b0 6c 65 72 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65 78 ler}.**.** To ex
1f3c0 65 63 75 74 65 20 61 6e 20 53 51 4c 20 71 75 65 ecute an SQL que
1f3d0 72 79 2c 20 69 74 20 6d 75 73 74 20 66 69 72 73 ry, it must firs
1f3e0 74 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 69 6e t be compiled in
1f3f0 74 6f 20 61 20 62 79 74 65 2d 63 6f 64 65 0a 2a to a byte-code.*
1f400 2a 20 70 72 6f 67 72 61 6d 20 75 73 69 6e 67 20 * program using
1f410 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75 one of these rou
1f420 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tines..**.** The
1f430 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c first argument,
1f440 20 22 64 62 22 2c 20 69 73 20 61 20 5b 64 61 74 "db", is a [dat
1f450 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1f460 5d 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 ] obtained from
1f470 61 0a 2a 2a 20 70 72 69 6f 72 20 73 75 63 63 65 a.** prior succe
1f480 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 ssful call to [s
1f490 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 qlite3_open()],
1f4a0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 [sqlite3_open_v2
1f4b0 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 ()] or.** [sqlit
1f4c0 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 20 20 54 e3_open16()]. T
1f4d0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
1f4e0 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 ection must not
1f4f0 68 61 76 65 20 62 65 65 6e 20 63 6c 6f 73 65 64 have been closed
1f500 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f ..**.** The seco
1f510 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 22 7a 53 nd argument, "zS
1f520 71 6c 22 2c 20 69 73 20 74 68 65 20 73 74 61 74 ql", is the stat
1f530 65 6d 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d 70 ement to be comp
1f540 69 6c 65 64 2c 20 65 6e 63 6f 64 65 64 0a 2a 2a iled, encoded.**
1f550 20 61 73 20 65 69 74 68 65 72 20 55 54 46 2d 38 as either UTF-8
1f560 20 6f 72 20 55 54 46 2d 31 36 2e 20 20 54 68 65 or UTF-16. The
1f570 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
1f580 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 () and sqlite3_p
1f590 72 65 70 61 72 65 5f 76 32 28 29 0a 2a 2a 20 69 repare_v2().** i
1f5a0 6e 74 65 72 66 61 63 65 73 20 75 73 65 20 55 54 nterfaces use UT
1f5b0 46 2d 38 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 F-8, and sqlite3
1f5c0 5f 70 72 65 70 61 72 65 31 36 28 29 20 61 6e 64 _prepare16() and
1f5d0 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
1f5e0 31 36 5f 76 32 28 29 0a 2a 2a 20 75 73 65 20 55 16_v2().** use U
1f5f0 54 46 2d 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 TF-16..**.** If
1f600 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 6d 65 the nByte argume
1f610 6e 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 nt is less than
1f620 7a 65 72 6f 2c 20 74 68 65 6e 20 7a 53 71 6c 20 zero, then zSql
1f630 69 73 20 72 65 61 64 20 75 70 20 74 6f 20 74 68 is read up to th
1f640 65 0a 2a 2a 20 66 69 72 73 74 20 7a 65 72 6f 20 e.** first zero
1f650 74 65 72 6d 69 6e 61 74 6f 72 2e 20 49 66 20 6e terminator. If n
1f660 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 Byte is non-nega
1f670 74 69 76 65 2c 20 74 68 65 6e 20 69 74 20 69 73 tive, then it is
1f680 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 the maximum.**
1f690 6e 75 6d 62 65 72 20 6f 66 20 20 62 79 74 65 73 number of bytes
1f6a0 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c 2e read from zSql.
1f6b0 20 20 57 68 65 6e 20 6e 42 79 74 65 20 69 73 20 When nByte is
1f6c0 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 non-negative, th
1f6d0 65 0a 2a 2a 20 7a 53 71 6c 20 73 74 72 69 6e 67 e.** zSql string
1f6e0 20 65 6e 64 73 20 61 74 20 65 69 74 68 65 72 20 ends at either
1f6f0 74 68 65 20 66 69 72 73 74 20 27 5c 30 30 30 27 the first '\000'
1f700 20 6f 72 20 27 5c 75 30 30 30 30 27 20 63 68 61 or '\u0000' cha
1f710 72 61 63 74 65 72 20 6f 72 0a 2a 2a 20 74 68 65 racter or.** the
1f720 20 6e 42 79 74 65 2d 74 68 20 62 79 74 65 2c 20 nByte-th byte,
1f730 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 whichever comes
1f740 66 69 72 73 74 2e 20 49 66 20 74 68 65 20 63 61 first. If the ca
1f750 6c 6c 65 72 20 6b 6e 6f 77 73 0a 2a 2a 20 74 68 ller knows.** th
1f760 61 74 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 at the supplied
1f770 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 2d 74 65 string is nul-te
1f780 72 6d 69 6e 61 74 65 64 2c 20 74 68 65 6e 20 74 rminated, then t
1f790 68 65 72 65 20 69 73 20 61 20 73 6d 61 6c 6c 0a here is a small.
1f7a0 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61 ** performance a
1f7b0 64 76 61 6e 74 61 67 65 20 74 6f 20 62 65 20 67 dvantage to be g
1f7c0 61 69 6e 65 64 20 62 79 20 70 61 73 73 69 6e 67 ained by passing
1f7d0 20 61 6e 20 6e 42 79 74 65 20 70 61 72 61 6d 65 an nByte parame
1f7e0 74 65 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 65 ter that.** is e
1f7f0 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75 6d 62 qual to the numb
1f800 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 er of bytes in t
1f810 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 he input string
1f820 3c 69 3e 69 6e 63 6c 75 64 69 6e 67 3c 2f 69 3e <i>including</i>
1f830 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d .** the nul-term
1f840 69 6e 61 74 6f 72 20 62 79 74 65 73 2e 0a 2a 2a inator bytes..**
1f850 0a 2a 2a 20 49 66 20 70 7a 54 61 69 6c 20 69 73 .** If pzTail is
1f860 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a not NULL then *
1f870 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74 pzTail is made t
1f880 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 o point to the f
1f890 69 72 73 74 20 62 79 74 65 0a 2a 2a 20 70 61 73 irst byte.** pas
1f8a0 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
1f8b0 20 66 69 72 73 74 20 53 51 4c 20 73 74 61 74 65 first SQL state
1f8c0 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 20 54 ment in zSql. T
1f8d0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e hese routines on
1f8e0 6c 79 0a 2a 2a 20 63 6f 6d 70 69 6c 65 20 74 68 ly.** compile th
1f8f0 65 20 66 69 72 73 74 20 73 74 61 74 65 6d 65 6e e first statemen
1f900 74 20 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 2a 70 t in zSql, so *p
1f910 7a 54 61 69 6c 20 69 73 20 6c 65 66 74 20 70 6f zTail is left po
1f920 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 77 68 61 inting to.** wha
1f930 74 20 72 65 6d 61 69 6e 73 20 75 6e 63 6f 6d 70 t remains uncomp
1f940 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 53 iled..**.** *ppS
1f950 74 6d 74 20 69 73 20 6c 65 66 74 20 70 6f 69 6e tmt is left poin
1f960 74 69 6e 67 20 74 6f 20 61 20 63 6f 6d 70 69 6c ting to a compil
1f970 65 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 ed [prepared sta
1f980 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 63 61 6e tement] that can
1f990 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 be.** executed
1f9a0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 using [sqlite3_s
1f9b0 74 65 70 28 29 5d 2e 20 20 49 66 20 74 68 65 72 tep()]. If ther
1f9c0 65 20 69 73 20 61 6e 20 65 72 72 6f 72 2c 20 2a e is an error, *
1f9d0 70 70 53 74 6d 74 20 69 73 20 73 65 74 0a 2a 2a ppStmt is set.**
1f9e0 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68 to NULL. If th
1f9f0 65 20 69 6e 70 75 74 20 74 65 78 74 20 63 6f 6e e input text con
1fa00 74 61 69 6e 73 20 6e 6f 20 53 51 4c 20 28 69 66 tains no SQL (if
1fa10 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 61 6e the input is an
1fa20 20 65 6d 70 74 79 0a 2a 2a 20 73 74 72 69 6e 67 empty.** string
1fa30 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29 20 74 or a comment) t
1fa40 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73 20 73 hen *ppStmt is s
1fa50 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 54 et to NULL..** T
1fa60 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 he calling proce
1fa70 64 75 72 65 20 69 73 20 72 65 73 70 6f 6e 73 69 dure is responsi
1fa80 62 6c 65 20 66 6f 72 20 64 65 6c 65 74 69 6e 67 ble for deleting
1fa90 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a the compiled.**
1faa0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 75 SQL statement u
1fab0 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 sing [sqlite3_fi
1fac0 6e 61 6c 69 7a 65 28 29 5d 20 61 66 74 65 72 20 nalize()] after
1fad0 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 it has finished
1fae0 77 69 74 68 20 69 74 2e 0a 2a 2a 20 70 70 53 74 with it..** ppSt
1faf0 6d 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55 mt may not be NU
1fb00 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 LL..**.** On suc
1fb10 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b cess, [SQLITE_OK
1fb20 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f ] is returned, o
1fb30 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 therwise an [err
1fb40 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 or code] is retu
1fb50 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rned..**.** The
1fb60 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
1fb70 76 32 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 v2() and sqlite3
1fb80 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 20 _prepare16_v2()
1fb90 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 0a 2a interfaces are.*
1fba0 2a 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f * recommended fo
1fbb0 72 20 61 6c 6c 20 6e 65 77 20 70 72 6f 67 72 61 r all new progra
1fbc0 6d 73 2e 20 54 68 65 20 74 77 6f 20 6f 6c 64 65 ms. The two olde
1fbd0 72 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 r interfaces are
1fbe0 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20 66 6f 72 retained.** for
1fbf0 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 backwards compa
1fc00 74 69 62 69 6c 69 74 79 2c 20 62 75 74 20 74 68 tibility, but th
1fc10 65 69 72 20 75 73 65 20 69 73 20 64 69 73 63 6f eir use is disco
1fc20 75 72 61 67 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 uraged..** In th
1fc30 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 e "v2" interface
1fc40 73 2c 20 74 68 65 20 70 72 65 70 61 72 65 64 20 s, the prepared
1fc50 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 68 61 statement.** tha
1fc60 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 28 74 t is returned (t
1fc70 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 he [sqlite3_stmt
1fc80 5d 20 6f 62 6a 65 63 74 29 20 63 6f 6e 74 61 69 ] object) contai
1fc90 6e 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 ns a copy of the
1fca0 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c .** original SQL
1fcb0 20 74 65 78 74 2e 20 54 68 69 73 20 63 61 75 73 text. This caus
1fcc0 65 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f es the [sqlite3_
1fcd0 73 74 65 70 28 29 5d 20 69 6e 74 65 72 66 61 63 step()] interfac
1fce0 65 20 74 6f 0a 2a 2a 20 62 65 68 61 76 65 20 61 e to.** behave a
1fcf0 20 64 69 66 66 65 72 65 6e 74 6c 79 20 69 6e 20 differently in
1fd00 74 77 6f 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 two ways:.**.**
1fd10 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 <ol>.** <li>.**
1fd20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 If the database
1fd30 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 2c 20 schema changes,
1fd40 69 6e 73 74 65 61 64 20 6f 66 20 72 65 74 75 72 instead of retur
1fd50 6e 69 6e 67 20 5b 53 51 4c 49 54 45 5f 53 43 48 ning [SQLITE_SCH
1fd60 45 4d 41 5d 20 61 73 20 69 74 0a 2a 2a 20 61 6c EMA] as it.** al
1fd70 77 61 79 73 20 75 73 65 64 20 74 6f 20 64 6f 2c ways used to do,
1fd80 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
1fd90 5d 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 ] will automatic
1fda0 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 20 74 ally recompile t
1fdb0 68 65 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d he SQL.** statem
1fdc0 65 6e 74 20 61 6e 64 20 74 72 79 20 74 6f 20 72 ent and try to r
1fdd0 75 6e 20 69 74 20 61 67 61 69 6e 2e 20 20 49 66 un it again. If
1fde0 20 74 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 the schema has
1fdf0 63 68 61 6e 67 65 64 20 69 6e 0a 2a 2a 20 61 20 changed in.** a
1fe00 77 61 79 20 74 68 61 74 20 6d 61 6b 65 73 20 74 way that makes t
1fe10 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 20 he statement no
1fe20 6c 6f 6e 67 65 72 20 76 61 6c 69 64 2c 20 5b 73 longer valid, [s
1fe30 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 qlite3_step()] w
1fe40 69 6c 6c 20 73 74 69 6c 6c 0a 2a 2a 20 72 65 74 ill still.** ret
1fe50 75 72 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 urn [SQLITE_SCHE
1fe60 4d 41 5d 2e 20 20 42 75 74 20 75 6e 6c 69 6b 65 MA]. But unlike
1fe70 20 74 68 65 20 6c 65 67 61 63 79 20 62 65 68 61 the legacy beha
1fe80 76 69 6f 72 2c 20 5b 53 51 4c 49 54 45 5f 53 43 vior, [SQLITE_SC
1fe90 48 45 4d 41 5d 20 69 73 0a 2a 2a 20 6e 6f 77 20 HEMA] is.** now
1fea0 61 20 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 a fatal error.
1feb0 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 Calling [sqlite3
1fec0 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 _prepare_v2()] a
1fed0 67 61 69 6e 20 77 69 6c 6c 20 6e 6f 74 20 6d 61 gain will not ma
1fee0 6b 65 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 ke the.** error
1fef0 67 6f 20 61 77 61 79 2e 20 20 4e 6f 74 65 3a 20 go away. Note:
1ff00 75 73 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 use [sqlite3_err
1ff10 6d 73 67 28 29 5d 20 74 6f 20 66 69 6e 64 20 74 msg()] to find t
1ff20 68 65 20 74 65 78 74 0a 2a 2a 20 6f 66 20 74 68 he text.** of th
1ff30 65 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 e parsing error
1ff40 74 68 61 74 20 72 65 73 75 6c 74 73 20 69 6e 20 that results in
1ff50 61 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d an [SQLITE_SCHEM
1ff60 41 5d 20 72 65 74 75 72 6e 2e 0a 2a 2a 20 3c 2f A] return..** </
1ff70 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 0a 2a li>.**.** <li>.*
1ff80 2a 20 57 68 65 6e 20 61 6e 20 65 72 72 6f 72 20 * When an error
1ff90 6f 63 63 75 72 73 2c 20 5b 73 71 6c 69 74 65 33 occurs, [sqlite3
1ffa0 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 72 65 _step()] will re
1ffb0 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 turn one of the
1ffc0 64 65 74 61 69 6c 65 64 0a 2a 2a 20 5b 65 72 72 detailed.** [err
1ffd0 6f 72 20 63 6f 64 65 73 5d 20 6f 72 20 5b 65 78 or codes] or [ex
1ffe0 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 tended error cod
1fff0 65 73 5d 2e 20 20 54 68 65 20 6c 65 67 61 63 79 es]. The legacy
20000 20 62 65 68 61 76 69 6f 72 20 77 61 73 20 74 68 behavior was th
20010 61 74 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 at.** [sqlite3_s
20020 74 65 70 28 29 5d 20 77 6f 75 6c 64 20 6f 6e 6c tep()] would onl
20030 79 20 72 65 74 75 72 6e 20 61 20 67 65 6e 65 72 y return a gener
20040 69 63 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 ic [SQLITE_ERROR
20050 5d 20 72 65 73 75 6c 74 20 63 6f 64 65 0a 2a 2a ] result code.**
20060 20 61 6e 64 20 79 6f 75 20 77 6f 75 6c 64 20 68 and you would h
20070 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 20 73 65 ave to make a se
20080 63 6f 6e 64 20 63 61 6c 6c 20 74 6f 20 5b 73 71 cond call to [sq
20090 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 69 lite3_reset()] i
200a0 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 66 69 n order.** to fi
200b0 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e nd the underlyin
200c0 67 20 63 61 75 73 65 20 6f 66 20 74 68 65 20 70 g cause of the p
200d0 72 6f 62 6c 65 6d 2e 20 57 69 74 68 20 74 68 65 roblem. With the
200e0 20 22 76 32 22 20 70 72 65 70 61 72 65 0a 2a 2a "v2" prepare.**
200f0 20 69 6e 74 65 72 66 61 63 65 73 2c 20 74 68 65 interfaces, the
20100 20 75 6e 64 65 72 6c 79 69 6e 67 20 72 65 61 73 underlying reas
20110 6f 6e 20 66 6f 72 20 74 68 65 20 65 72 72 6f 72 on for the error
20120 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d is returned imm
20130 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 20 3c 2f 6c ediately..** </l
20140 69 3e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a i>.** </ol>.**.*
20150 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
20160 2a 2a 20 5b 48 31 33 30 31 31 5d 20 5b 48 31 33 ** [H13011] [H13
20170 30 31 32 5d 20 5b 48 31 33 30 31 33 5d 20 5b 48 012] [H13013] [H
20180 31 33 30 31 34 5d 20 5b 48 31 33 30 31 35 5d 20 13014] [H13015]
20190 5b 48 31 33 30 31 36 5d 20 5b 48 31 33 30 31 39 [H13016] [H13019
201a0 5d 20 5b 48 31 33 30 32 31 5d 0a 2a 2a 0a 2a 2f ] [H13021].**.*/
201b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
201c0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 sqlite3_prepare(
201d0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
201e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 /* Da
201f0 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f tabase handle */
20200 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
20210 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 Sql, /* SQ
20220 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 L statement, UTF
20230 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 -8 encoded */.
20240 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 int nByte,
20250 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d /* Maxim
20260 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 um length of zSq
20270 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 l in bytes. */.
20280 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a sqlite3_stmt **
20290 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a ppStmt, /* OUT:
202a0 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c Statement handl
202b0 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 e */. const cha
202c0 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f r **pzTail /
202d0 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 * OUT: Pointer t
202e0 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e o unused portion
202f0 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 of zSql */.);.S
20300 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
20310 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
20320 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
20330 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
20340 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a atabase handle *
20350 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
20360 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 zSql, /* S
20370 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 QL statement, UT
20380 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 F-8 encoded */.
20390 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 int nByte,
203a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 /* Maxi
203b0 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 mum length of zS
203c0 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a ql in bytes. */.
203d0 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a sqlite3_stmt *
203e0 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 *ppStmt, /* OUT
203f0 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 : Statement hand
20400 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 le */. const ch
20410 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 ar **pzTail
20420 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 /* OUT: Pointer
20430 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f to unused portio
20440 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a n of zSql */.);.
20450 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
20460 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 qlite3_prepare16
20470 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
20480 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
20490 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a atabase handle *
204a0 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
204b0 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 zSql, /* S
204c0 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 QL statement, UT
204d0 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f 0a F-16 encoded */.
204e0 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 int nByte,
204f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 /* Max
20500 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a imum length of z
20510 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f Sql in bytes. */
20520 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 . sqlite3_stmt
20530 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 **ppStmt, /* OU
20540 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e T: Statement han
20550 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 dle */. const v
20560 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 oid **pzTail
20570 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 /* OUT: Pointer
20580 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 to unused porti
20590 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b on of zSql */.);
205a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
205b0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 sqlite3_prepare1
205c0 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 6_v2(. sqlite3
205d0 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 *db,
205e0 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 /* Database hand
205f0 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f le */. const vo
20600 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 id *zSql,
20610 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 /* SQL statement
20620 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 , UTF-16 encoded
20630 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c */. int nByte,
20640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
20650 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 Maximum length
20660 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 of zSql in bytes
20670 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 . */. sqlite3_s
20680 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f tmt **ppStmt, /
20690 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 * OUT: Statement
206a0 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e handle */. con
206b0 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c st void **pzTail
206c0 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 /* OUT: Poi
206d0 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 nter to unused p
206e0 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a ortion of zSql *
206f0 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 /.);../*.** CAPI
20700 33 52 45 46 3a 20 52 65 74 72 69 65 76 69 6e 67 3REF: Retrieving
20710 20 53 74 61 74 65 6d 65 6e 74 20 53 51 4c 20 7b Statement SQL {
20720 48 31 33 31 30 30 7d 20 3c 48 31 33 30 30 30 3e H13100} <H13000>
20730 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 .**.** This inte
20740 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 73 65 rface can be use
20750 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 61 20 d to retrieve a
20760 73 61 76 65 64 20 63 6f 70 79 20 6f 66 20 74 68 saved copy of th
20770 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 53 51 e original.** SQ
20780 4c 20 74 65 78 74 20 75 73 65 64 20 74 6f 20 63 L text used to c
20790 72 65 61 74 65 20 61 20 5b 70 72 65 70 61 72 65 reate a [prepare
207a0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 66 20 d statement] if
207b0 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20 77 that statement w
207c0 61 73 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 75 as.** compiled u
207d0 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c sing either [sql
207e0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
207f0 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 )] or [sqlite3_p
20800 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a repare16_v2()]..
20810 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
20820 74 73 3a 0a 2a 2a 20 5b 48 31 33 31 30 31 5d 20 ts:.** [H13101]
20830 5b 48 31 33 31 30 32 5d 20 5b 48 31 33 31 30 33 [H13102] [H13103
20840 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
20850 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
20860 74 65 33 5f 73 71 6c 28 73 71 6c 69 74 65 33 5f te3_sql(sqlite3_
20870 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f stmt *pStmt);../
20880 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 *.** CAPI3REF: D
20890 79 6e 61 6d 69 63 61 6c 6c 79 20 54 79 70 65 64 ynamically Typed
208a0 20 56 61 6c 75 65 20 4f 62 6a 65 63 74 20 7b 48 Value Object {H
208b0 31 35 30 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 15000} <S20200>.
208c0 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 ** KEYWORDS: {pr
208d0 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
208e0 76 61 6c 75 65 7d 20 7b 75 6e 70 72 6f 74 65 63 value} {unprotec
208f0 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ted sqlite3_valu
20900 65 7d 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 e}.**.** SQLite
20910 75 73 65 73 20 74 68 65 20 73 71 6c 69 74 65 33 uses the sqlite3
20920 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 74 6f _value object to
20930 20 72 65 70 72 65 73 65 6e 74 20 61 6c 6c 20 76 represent all v
20940 61 6c 75 65 73 0a 2a 2a 20 74 68 61 74 20 63 61 alues.** that ca
20950 6e 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 n be stored in a
20960 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e database table.
20970 20 53 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e SQLite uses dyn
20980 61 6d 69 63 20 74 79 70 69 6e 67 0a 2a 2a 20 66 amic typing.** f
20990 6f 72 20 74 68 65 20 76 61 6c 75 65 73 20 69 74 or the values it
209a0 20 73 74 6f 72 65 73 2e 20 56 61 6c 75 65 73 20 stores. Values
209b0 73 74 6f 72 65 64 20 69 6e 20 73 71 6c 69 74 65 stored in sqlite
209c0 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 0a 3_value objects.
209d0 2a 2a 20 63 61 6e 20 62 65 20 69 6e 74 65 67 65 ** can be intege
209e0 72 73 2c 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 rs, floating poi
209f0 6e 74 20 76 61 6c 75 65 73 2c 20 73 74 72 69 6e nt values, strin
20a00 67 73 2c 20 42 4c 4f 42 73 2c 20 6f 72 20 4e 55 gs, BLOBs, or NU
20a10 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 73 71 6c LL..**.** An sql
20a20 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
20a30 74 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 20 t may be either
20a40 22 70 72 6f 74 65 63 74 65 64 22 20 6f 72 20 22 "protected" or "
20a50 75 6e 70 72 6f 74 65 63 74 65 64 22 2e 0a 2a 2a unprotected"..**
20a60 20 53 6f 6d 65 20 69 6e 74 65 72 66 61 63 65 73 Some interfaces
20a70 20 72 65 71 75 69 72 65 20 61 20 70 72 6f 74 65 require a prote
20a80 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
20a90 75 65 2e 20 20 4f 74 68 65 72 20 69 6e 74 65 72 ue. Other inter
20aa0 66 61 63 65 73 0a 2a 2a 20 77 69 6c 6c 20 61 63 faces.** will ac
20ab0 63 65 70 74 20 65 69 74 68 65 72 20 61 20 70 72 cept either a pr
20ac0 6f 74 65 63 74 65 64 20 6f 72 20 61 6e 20 75 6e otected or an un
20ad0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
20ae0 33 5f 76 61 6c 75 65 2e 0a 2a 2a 20 45 76 65 72 3_value..** Ever
20af0 79 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 y interface that
20b00 20 61 63 63 65 70 74 73 20 73 71 6c 69 74 65 33 accepts sqlite3
20b10 5f 76 61 6c 75 65 20 61 72 67 75 6d 65 6e 74 73 _value arguments
20b20 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68 specifies.** wh
20b30 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 69 74 20 ether or not it
20b40 72 65 71 75 69 72 65 73 20 61 20 70 72 6f 74 65 requires a prote
20b50 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
20b60 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 ue..**.** The te
20b70 72 6d 73 20 22 70 72 6f 74 65 63 74 65 64 22 20 rms "protected"
20b80 61 6e 64 20 22 75 6e 70 72 6f 74 65 63 74 65 64 and "unprotected
20b90 22 20 72 65 66 65 72 20 74 6f 20 77 68 65 74 68 " refer to wheth
20ba0 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 61 20 6d er or not.** a m
20bb0 75 74 65 78 20 69 73 20 68 65 6c 64 2e 20 20 41 utex is held. A
20bc0 20 69 6e 74 65 72 6e 61 6c 20 6d 75 74 65 78 20 internal mutex
20bd0 69 73 20 68 65 6c 64 20 66 6f 72 20 61 20 70 72 is held for a pr
20be0 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 otected.** sqlit
20bf0 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 e3_value object
20c00 62 75 74 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 but no mutex is
20c10 68 65 6c 64 20 66 6f 72 20 61 6e 20 75 6e 70 72 held for an unpr
20c20 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 otected.** sqlit
20c30 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e e3_value object.
20c40 20 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 If SQLite is c
20c50 6f 6d 70 69 6c 65 64 20 74 6f 20 62 65 20 73 69 ompiled to be si
20c60 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a ngle-threaded.**
20c70 20 28 77 69 74 68 20 5b 53 51 4c 49 54 45 5f 54 (with [SQLITE_T
20c80 48 52 45 41 44 53 41 46 45 3d 30 5d 20 61 6e 64 HREADSAFE=0] and
20c90 20 77 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 74 with [sqlite3_t
20ca0 68 72 65 61 64 73 61 66 65 28 29 5d 20 72 65 74 hreadsafe()] ret
20cb0 75 72 6e 69 6e 67 20 30 29 0a 2a 2a 20 6f 72 20 urning 0).** or
20cc0 69 66 20 53 51 4c 69 74 65 20 69 73 20 72 75 6e if SQLite is run
20cd0 20 69 6e 20 6f 6e 65 20 6f 66 20 72 65 64 75 63 in one of reduc
20ce0 65 64 20 6d 75 74 65 78 20 6d 6f 64 65 73 20 0a ed mutex modes .
20cf0 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ** [SQLITE_CONFI
20d00 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 5d 20 G_SINGLETHREAD]
20d10 6f 72 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 or [SQLITE_CONFI
20d20 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d 0a 2a G_MULTITHREAD].*
20d30 2a 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 * then there is
20d40 6e 6f 20 64 69 73 74 69 6e 63 74 69 6f 6e 20 62 no distinction b
20d50 65 74 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64 etween protected
20d60 20 61 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64 and unprotected
20d70 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 .** sqlite3_valu
20d80 65 20 6f 62 6a 65 63 74 73 20 61 6e 64 20 74 68 e objects and th
20d90 65 79 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 ey can be used i
20da0 6e 74 65 72 63 68 61 6e 67 65 61 62 6c 79 2e 20 nterchangeably.
20db0 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20 66 6f 72 However,.** for
20dc0 20 6d 61 78 69 6d 75 6d 20 63 6f 64 65 20 70 6f maximum code po
20dd0 72 74 61 62 69 6c 69 74 79 20 69 74 20 69 73 20 rtability it is
20de0 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 recommended that
20df0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a applications.**
20e00 20 73 74 69 6c 6c 20 6d 61 6b 65 20 74 68 65 20 still make the
20e10 64 69 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 distinction betw
20e20 65 65 6e 20 62 65 74 77 65 65 6e 20 70 72 6f 74 een between prot
20e30 65 63 74 65 64 20 61 6e 64 20 75 6e 70 72 6f 74 ected and unprot
20e40 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 ected.** sqlite3
20e50 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 65 _value objects e
20e60 76 65 6e 20 77 68 65 6e 20 6e 6f 74 20 73 74 72 ven when not str
20e70 69 63 74 6c 79 20 72 65 71 75 69 72 65 64 2e 0a ictly required..
20e80 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
20e90 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 3_value objects
20ea0 74 68 61 74 20 61 72 65 20 70 61 73 73 65 64 20 that are passed
20eb0 61 73 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e as parameters in
20ec0 74 6f 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d to the.** implem
20ed0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 5b 61 70 70 entation of [app
20ee0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
20ef0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d 20 SQL functions]
20f00 61 72 65 20 70 72 6f 74 65 63 74 65 64 2e 0a 2a are protected..*
20f10 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 * The sqlite3_va
20f20 6c 75 65 20 6f 62 6a 65 63 74 20 72 65 74 75 72 lue object retur
20f30 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 ned by.** [sqlit
20f40 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 e3_column_value(
20f50 29 5d 20 69 73 20 75 6e 70 72 6f 74 65 63 74 65 )] is unprotecte
20f60 64 2e 0a 2a 2a 20 55 6e 70 72 6f 74 65 63 74 65 d..** Unprotecte
20f70 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 d sqlite3_value
20f80 6f 62 6a 65 63 74 73 20 6d 61 79 20 6f 6e 6c 79 objects may only
20f90 20 62 65 20 75 73 65 64 20 77 69 74 68 0a 2a 2a be used with.**
20fa0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 [sqlite3_result
20fb0 5f 76 61 6c 75 65 28 29 5d 20 61 6e 64 20 5b 73 _value()] and [s
20fc0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 qlite3_bind_valu
20fd0 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 e()]..** The [sq
20fe0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 lite3_value_blob
20ff0 20 7c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 | sqlite3_value
21000 5f 74 79 70 65 28 29 5d 20 66 61 6d 69 6c 79 20 _type()] family
21010 6f 66 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 of.** interfaces
21020 20 72 65 71 75 69 72 65 20 70 72 6f 74 65 63 74 require protect
21030 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
21040 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a 74 79 70 objects..*/.typ
21050 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 20 edef struct Mem
21060 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 3b 0a 0a sqlite3_value;..
21070 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
21080 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e SQL Function Con
21090 74 65 78 74 20 4f 62 6a 65 63 74 20 7b 48 31 36 text Object {H16
210a0 30 30 31 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 001} <S20200>.**
210b0 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 78 74 20 .** The context
210c0 69 6e 20 77 68 69 63 68 20 61 6e 20 53 51 4c 20 in which an SQL
210d0 66 75 6e 63 74 69 6f 6e 20 65 78 65 63 75 74 65 function execute
210e0 73 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 s is stored in a
210f0 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e n.** sqlite3_con
21100 74 65 78 74 20 6f 62 6a 65 63 74 2e 20 20 41 20 text object. A
21110 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 pointer to an sq
21120 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f 62 lite3_context ob
21130 6a 65 63 74 0a 2a 2a 20 69 73 20 61 6c 77 61 79 ject.** is alway
21140 73 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 s first paramete
21150 72 20 74 6f 20 5b 61 70 70 6c 69 63 61 74 69 6f r to [applicatio
21160 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 n-defined SQL fu
21170 6e 63 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 54 68 65 nctions]..** The
21180 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 application-def
21190 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f ined SQL functio
211a0 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e n implementation
211b0 20 77 69 6c 6c 20 70 61 73 73 20 74 68 69 73 0a will pass this.
211c0 2a 2a 20 70 6f 69 6e 74 65 72 20 74 68 72 6f 75 ** pointer throu
211d0 67 68 20 69 6e 74 6f 20 63 61 6c 6c 73 20 74 6f gh into calls to
211e0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 [sqlite3_result
211f0 5f 69 6e 74 20 7c 20 73 71 6c 69 74 65 33 5f 72 _int | sqlite3_r
21200 65 73 75 6c 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71 esult()],.** [sq
21210 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f lite3_aggregate_
21220 63 6f 6e 74 65 78 74 28 29 5d 2c 20 5b 73 71 6c context()], [sql
21230 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 ite3_user_data()
21240 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 ],.** [sqlite3_c
21250 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 ontext_db_handle
21260 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 67 65 ()], [sqlite3_ge
21270 74 5f 61 75 78 64 61 74 61 28 29 5d 2c 0a 2a 2a t_auxdata()],.**
21280 20 61 6e 64 2f 6f 72 20 5b 73 71 6c 69 74 65 33 and/or [sqlite3
21290 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 5d 2e _set_auxdata()].
212a0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
212b0 63 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ct sqlite3_conte
212c0 78 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 xt sqlite3_conte
212d0 78 74 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 xt;../*.** CAPI3
212e0 52 45 46 3a 20 42 69 6e 64 69 6e 67 20 56 61 6c REF: Binding Val
212f0 75 65 73 20 54 6f 20 50 72 65 70 61 72 65 64 20 ues To Prepared
21300 53 74 61 74 65 6d 65 6e 74 73 20 7b 48 31 33 35 Statements {H135
21310 30 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 20 00} <S70300>.**
21320 4b 45 59 57 4f 52 44 53 3a 20 7b 68 6f 73 74 20 KEYWORDS: {host
21330 70 61 72 61 6d 65 74 65 72 7d 20 7b 68 6f 73 74 parameter} {host
21340 20 70 61 72 61 6d 65 74 65 72 73 7d 20 7b 68 6f parameters} {ho
21350 73 74 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d st parameter nam
21360 65 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 e}.** KEYWORDS:
21370 7b 53 51 4c 20 70 61 72 61 6d 65 74 65 72 7d 20 {SQL parameter}
21380 7b 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 7d {SQL parameters}
21390 20 7b 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 {parameter bind
213a0 69 6e 67 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 ing}.**.** In th
213b0 65 20 53 51 4c 20 73 74 72 69 6e 67 73 20 69 6e e SQL strings in
213c0 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f put to [sqlite3_
213d0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e prepare_v2()] an
213e0 64 20 69 74 73 20 76 61 72 69 61 6e 74 73 2c 0a d its variants,.
213f0 2a 2a 20 6c 69 74 65 72 61 6c 73 20 6d 61 79 20 ** literals may
21400 62 65 20 72 65 70 6c 61 63 65 64 20 62 79 20 61 be replaced by a
21410 20 5b 70 61 72 61 6d 65 74 65 72 5d 20 74 68 61 [parameter] tha
21420 74 20 6d 61 74 63 68 65 73 20 6f 6e 65 20 6f 66 t matches one of
21430 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 74 65 following.** te
21440 6d 70 6c 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c mplates:.**.** <
21450 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 0a 2a ul>.** <li> ?.*
21460 2a 20 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a 2a 2a 20 * <li> ?NNN.**
21470 3c 6c 69 3e 20 20 3a 56 56 56 0a 2a 2a 20 3c 6c <li> :VVV.** <l
21480 69 3e 20 20 40 56 56 56 0a 2a 2a 20 3c 6c 69 3e i> @VVV.** <li>
21490 20 20 24 56 56 56 0a 2a 2a 20 3c 2f 75 6c 3e 0a $VVV.** </ul>.
214a0 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 74 65 6d **.** In the tem
214b0 70 6c 61 74 65 73 20 61 62 6f 76 65 2c 20 4e 4e plates above, NN
214c0 4e 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 N represents an
214d0 69 6e 74 65 67 65 72 20 6c 69 74 65 72 61 6c 2c integer literal,
214e0 0a 2a 2a 20 61 6e 64 20 56 56 56 20 72 65 70 72 .** and VVV repr
214f0 65 73 65 6e 74 73 20 61 6e 20 61 6c 70 68 61 6e esents an alphan
21500 75 6d 65 72 69 63 20 69 64 65 6e 74 69 66 65 72 umeric identifer
21510 2e 20 20 54 68 65 20 76 61 6c 75 65 73 20 6f 66 . The values of
21520 20 74 68 65 73 65 0a 2a 2a 20 70 61 72 61 6d 65 these.** parame
21530 74 65 72 73 20 28 61 6c 73 6f 20 63 61 6c 6c 65 ters (also calle
21540 64 20 22 68 6f 73 74 20 70 61 72 61 6d 65 74 65 d "host paramete
21550 72 20 6e 61 6d 65 73 22 20 6f 72 20 22 53 51 4c r names" or "SQL
21560 20 70 61 72 61 6d 65 74 65 72 73 22 29 0a 2a 2a parameters").**
21570 20 63 61 6e 20 62 65 20 73 65 74 20 75 73 69 6e can be set usin
21580 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 g the sqlite3_bi
21590 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 nd_*() routines
215a0 64 65 66 69 6e 65 64 20 68 65 72 65 2e 0a 2a 2a defined here..**
215b0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 .** The first ar
215c0 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 73 71 gument to the sq
215d0 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 lite3_bind_*() r
215e0 6f 75 74 69 6e 65 73 20 69 73 20 61 6c 77 61 79 outines is alway
215f0 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 s.** a pointer t
21600 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 o the [sqlite3_s
21610 74 6d 74 5d 20 6f 62 6a 65 63 74 20 72 65 74 75 tmt] object retu
21620 72 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 5b 73 71 rned from.** [sq
21630 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
21640 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 69 61 ()] or its varia
21650 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 nts..**.** The s
21660 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
21670 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 s the index of t
21680 68 65 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 he SQL parameter
21690 20 74 6f 20 62 65 20 73 65 74 2e 0a 2a 2a 20 54 to be set..** T
216a0 68 65 20 6c 65 66 74 6d 6f 73 74 20 53 51 4c 20 he leftmost SQL
216b0 70 61 72 61 6d 65 74 65 72 20 68 61 73 20 61 6e parameter has an
216c0 20 69 6e 64 65 78 20 6f 66 20 31 2e 20 20 57 68 index of 1. Wh
216d0 65 6e 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 en the same name
216e0 64 0a 2a 2a 20 53 51 4c 20 70 61 72 61 6d 65 74 d.** SQL paramet
216f0 65 72 20 69 73 20 75 73 65 64 20 6d 6f 72 65 20 er is used more
21700 74 68 61 6e 20 6f 6e 63 65 2c 20 73 65 63 6f 6e than once, secon
21710 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 d and subsequent
21720 0a 2a 2a 20 6f 63 63 75 72 72 65 6e 63 65 73 20 .** occurrences
21730 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 69 6e have the same in
21740 64 65 78 20 61 73 20 74 68 65 20 66 69 72 73 74 dex as the first
21750 20 6f 63 63 75 72 72 65 6e 63 65 2e 0a 2a 2a 20 occurrence..**
21760 54 68 65 20 69 6e 64 65 78 20 66 6f 72 20 6e 61 The index for na
21770 6d 65 64 20 70 61 72 61 6d 65 74 65 72 73 20 63 med parameters c
21780 61 6e 20 62 65 20 6c 6f 6f 6b 65 64 20 75 70 20 an be looked up
21790 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 using the.** [sq
217a0 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d lite3_bind_param
217b0 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 20 41 50 eter_index()] AP
217c0 49 20 69 66 20 64 65 73 69 72 65 64 2e 20 20 54 I if desired. T
217d0 68 65 20 69 6e 64 65 78 0a 2a 2a 20 66 6f 72 20 he index.** for
217e0 22 3f 4e 4e 4e 22 20 70 61 72 61 6d 65 74 65 72 "?NNN" parameter
217f0 73 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f s is the value o
21800 66 20 4e 4e 4e 2e 0a 2a 2a 20 54 68 65 20 4e 4e f NNN..** The NN
21810 4e 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20 N value must be
21820 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 74 68 between 1 and th
21830 65 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 e [sqlite3_limit
21840 28 29 5d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 ()].** parameter
21850 20 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 [SQLITE_LIMIT_V
21860 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 ARIABLE_NUMBER]
21870 28 64 65 66 61 75 6c 74 20 76 61 6c 75 65 3a 20 (default value:
21880 39 39 39 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 999)..**.** The
21890 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 third argument i
218a0 73 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62 s the value to b
218b0 69 6e 64 20 74 6f 20 74 68 65 20 70 61 72 61 6d ind to the param
218c0 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 eter..**.** In t
218d0 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 74 68 hose routines th
218e0 61 74 20 68 61 76 65 20 61 20 66 6f 75 72 74 68 at have a fourth
218f0 20 61 72 67 75 6d 65 6e 74 2c 20 69 74 73 20 76 argument, its v
21900 61 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a 20 6e alue is the.** n
21910 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
21920 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e n the parameter.
21930 20 20 54 6f 20 62 65 20 63 6c 65 61 72 3a 20 74 To be clear: t
21940 68 65 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a he value is the.
21950 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 3c 75 3e ** number of <u>
21960 62 79 74 65 73 3c 2f 75 3e 20 69 6e 20 74 68 65 bytes</u> in the
21970 20 76 61 6c 75 65 2c 20 6e 6f 74 20 74 68 65 20 value, not the
21980 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 number of charac
21990 74 65 72 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 ters..** If the
219a0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 fourth parameter
219b0 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 is negative, th
219c0 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 e length of the
219d0 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 74 68 65 string is.** the
219e0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
219f0 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 up to the first
21a00 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 zero terminator
21a10 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 66 74 ..**.** The fift
21a20 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 h argument to sq
21a30 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 lite3_bind_blob(
21a40 29 2c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f ), sqlite3_bind_
21a50 74 65 78 74 28 29 2c 20 61 6e 64 0a 2a 2a 20 73 text(), and.** s
21a60 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 qlite3_bind_text
21a70 31 36 28 29 20 69 73 20 61 20 64 65 73 74 72 75 16() is a destru
21a80 63 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69 73 ctor used to dis
21a90 70 6f 73 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 pose of the BLOB
21aa0 20 6f 72 0a 2a 2a 20 73 74 72 69 6e 67 20 61 66 or.** string af
21ab0 74 65 72 20 53 51 4c 69 74 65 20 68 61 73 20 66 ter SQLite has f
21ac0 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 74 2e inished with it.
21ad0 20 49 66 20 74 68 65 20 66 69 66 74 68 20 61 72 If the fifth ar
21ae0 67 75 6d 65 6e 74 20 69 73 0a 2a 2a 20 74 68 65 gument is.** the
21af0 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65 20 5b special value [
21b00 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 2c 20 SQLITE_STATIC],
21b10 74 68 65 6e 20 53 51 4c 69 74 65 20 61 73 73 75 then SQLite assu
21b20 6d 65 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 mes that the.**
21b30 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 69 information is i
21b40 6e 20 73 74 61 74 69 63 2c 20 75 6e 6d 61 6e 61 n static, unmana
21b50 67 65 64 20 73 70 61 63 65 20 61 6e 64 20 64 6f ged space and do
21b60 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 es not need to b
21b70 65 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66 20 74 e freed..** If t
21b80 68 65 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e he fifth argumen
21b90 74 20 68 61 73 20 74 68 65 20 76 61 6c 75 65 20 t has the value
21ba0 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e [SQLITE_TRANSIEN
21bb0 54 5d 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 T], then.** SQLi
21bc0 74 65 20 6d 61 6b 65 73 20 69 74 73 20 6f 77 6e te makes its own
21bd0 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 private copy of
21be0 20 74 68 65 20 64 61 74 61 20 69 6d 6d 65 64 69 the data immedi
21bf0 61 74 65 6c 79 2c 20 62 65 66 6f 72 65 0a 2a 2a ately, before.**
21c00 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e the sqlite3_bin
21c10 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 20 72 65 d_*() routine re
21c20 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 turns..**.** The
21c30 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 sqlite3_bind_ze
21c40 72 6f 62 6c 6f 62 28 29 20 72 6f 75 74 69 6e 65 roblob() routine
21c50 20 62 69 6e 64 73 20 61 20 42 4c 4f 42 20 6f 66 binds a BLOB of
21c60 20 6c 65 6e 67 74 68 20 4e 20 74 68 61 74 0a 2a length N that.*
21c70 2a 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 * is filled with
21c80 20 7a 65 72 6f 65 73 2e 20 20 41 20 7a 65 72 6f zeroes. A zero
21c90 62 6c 6f 62 20 75 73 65 73 20 61 20 66 69 78 65 blob uses a fixe
21ca0 64 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f d amount of memo
21cb0 72 79 0a 2a 2a 20 28 6a 75 73 74 20 61 6e 20 69 ry.** (just an i
21cc0 6e 74 65 67 65 72 20 74 6f 20 68 6f 6c 64 20 69 nteger to hold i
21cd0 74 73 20 73 69 7a 65 29 20 77 68 69 6c 65 20 69 ts size) while i
21ce0 74 20 69 73 20 62 65 69 6e 67 20 70 72 6f 63 65 t is being proce
21cf0 73 73 65 64 2e 0a 2a 2a 20 5a 65 72 6f 62 6c 6f ssed..** Zeroblo
21d00 62 73 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 bs are intended
21d10 74 6f 20 73 65 72 76 65 20 61 73 20 70 6c 61 63 to serve as plac
21d20 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 42 4c 4f eholders for BLO
21d30 42 73 20 77 68 6f 73 65 0a 2a 2a 20 63 6f 6e 74 Bs whose.** cont
21d40 65 6e 74 20 69 73 20 6c 61 74 65 72 20 77 72 69 ent is later wri
21d50 74 74 65 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 tten using.** [s
21d60 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e qlite3_blob_open
21d70 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 | incremental B
21d80 4c 4f 42 20 49 2f 4f 5d 20 72 6f 75 74 69 6e 65 LOB I/O] routine
21d90 73 2e 0a 2a 2a 20 41 20 6e 65 67 61 74 69 76 65 s..** A negative
21da0 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 7a value for the z
21db0 65 72 6f 62 6c 6f 62 20 72 65 73 75 6c 74 73 20 eroblob results
21dc0 69 6e 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 in a zero-length
21dd0 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 BLOB..**.** The
21de0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 sqlite3_bind_*(
21df0 29 20 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 ) routines must
21e00 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 0a be called after.
21e10 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ** [sqlite3_prep
21e20 61 72 65 5f 76 32 28 29 5d 20 28 61 6e 64 20 69 are_v2()] (and i
21e30 74 73 20 76 61 72 69 61 6e 74 73 29 20 6f 72 20 ts variants) or
21e40 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 [sqlite3_reset()
21e50 5d 20 61 6e 64 0a 2a 2a 20 62 65 66 6f 72 65 20 ] and.** before
21e60 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
21e70 2e 0a 2a 2a 20 42 69 6e 64 69 6e 67 73 20 61 72 ..** Bindings ar
21e80 65 20 6e 6f 74 20 63 6c 65 61 72 65 64 20 62 79 e not cleared by
21e90 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 the [sqlite3_re
21ea0 73 65 74 28 29 5d 20 72 6f 75 74 69 6e 65 2e 0a set()] routine..
21eb0 2a 2a 20 55 6e 62 6f 75 6e 64 20 70 61 72 61 6d ** Unbound param
21ec0 65 74 65 72 73 20 61 72 65 20 69 6e 74 65 72 70 eters are interp
21ed0 72 65 74 65 64 20 61 73 20 4e 55 4c 4c 2e 0a 2a reted as NULL..*
21ee0 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
21ef0 6e 65 73 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 nes return [SQLI
21f00 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 TE_OK] on succes
21f10 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f s or an error co
21f20 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e de if.** anythin
21f30 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 20 20 5b g goes wrong. [
21f40 53 51 4c 49 54 45 5f 52 41 4e 47 45 5d 20 69 73 SQLITE_RANGE] is
21f50 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 returned if the
21f60 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 6e parameter.** in
21f70 64 65 78 20 69 73 20 6f 75 74 20 6f 66 20 72 61 dex is out of ra
21f80 6e 67 65 2e 20 20 5b 53 51 4c 49 54 45 5f 4e 4f nge. [SQLITE_NO
21f90 4d 45 4d 5d 20 69 73 20 72 65 74 75 72 6e 65 64 MEM] is returned
21fa0 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 if malloc() fai
21fb0 6c 73 2e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d ls..** [SQLITE_M
21fc0 49 53 55 53 45 5d 20 6d 69 67 68 74 20 62 65 20 ISUSE] might be
21fd0 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 73 returned if thes
21fe0 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 e routines are c
21ff0 61 6c 6c 65 64 20 6f 6e 20 61 0a 2a 2a 20 76 69 alled on a.** vi
22000 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 74 68 rtual machine th
22010 61 74 20 69 73 20 74 68 65 20 77 72 6f 6e 67 20 at is the wrong
22020 73 74 61 74 65 20 6f 72 20 77 68 69 63 68 20 68 state or which h
22030 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
22040 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 44 65 finalized..** De
22050 74 65 63 74 69 6f 6e 20 6f 66 20 6d 69 73 75 73 tection of misus
22060 65 20 69 73 20 75 6e 72 65 6c 69 61 62 6c 65 2e e is unreliable.
22070 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 Applications s
22080 68 6f 75 6c 64 20 6e 6f 74 20 64 65 70 65 6e 64 hould not depend
22090 0a 2a 2a 20 6f 6e 20 53 51 4c 49 54 45 5f 4d 49 .** on SQLITE_MI
220a0 53 55 53 45 20 72 65 74 75 72 6e 73 2e 20 20 53 SUSE returns. S
220b0 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 69 73 20 QLITE_MISUSE is
220c0 69 6e 74 65 6e 64 65 64 20 74 6f 20 69 6e 64 69 intended to indi
220d0 63 61 74 65 20 61 0a 2a 2a 20 61 20 6c 6f 67 69 cate a.** a logi
220e0 63 20 65 72 72 6f 72 20 69 6e 20 74 68 65 20 61 c error in the a
220f0 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 46 75 74 pplication. Fut
22100 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 ure versions of
22110 53 51 4c 69 74 65 20 6d 69 67 68 74 0a 2a 2a 20 SQLite might.**
22120 70 61 6e 69 63 20 72 61 74 68 65 72 20 74 68 61 panic rather tha
22130 6e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f n return SQLITE_
22140 4d 49 53 55 53 45 2e 0a 2a 2a 0a 2a 2a 20 53 65 MISUSE..**.** Se
22150 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 e also: [sqlite3
22160 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f _bind_parameter_
22170 63 6f 75 6e 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71 count()],.** [sq
22180 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d lite3_bind_param
22190 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e eter_name()], an
221a0 64 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f d [sqlite3_bind_
221b0 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 parameter_index(
221c0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 )]..**.** Requir
221d0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 35 ements:.** [H135
221e0 30 36 5d 20 5b 48 31 33 35 30 39 5d 20 5b 48 31 06] [H13509] [H1
221f0 33 35 31 32 5d 20 5b 48 31 33 35 31 35 5d 20 5b 3512] [H13515] [
22200 48 31 33 35 31 38 5d 20 5b 48 31 33 35 32 31 5d H13518] [H13521]
22210 20 5b 48 31 33 35 32 34 5d 20 5b 48 31 33 35 32 [H13524] [H1352
22220 37 5d 0a 2a 2a 20 5b 48 31 33 35 33 30 5d 20 5b 7].** [H13530] [
22230 48 31 33 35 33 33 5d 20 5b 48 31 33 35 33 36 5d H13533] [H13536]
22240 20 5b 48 31 33 35 33 39 5d 20 5b 48 31 33 35 34 [H13539] [H1354
22250 32 5d 20 5b 48 31 33 35 34 35 5d 20 5b 48 31 33 2] [H13545] [H13
22260 35 34 38 5d 20 5b 48 31 33 35 35 31 5d 0a 2a 2a 548] [H13551].**
22270 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
22280 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
22290 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d blob(sqlite3_stm
222a0 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 t*, int, const v
222b0 6f 69 64 2a 2c 20 69 6e 74 20 6e 2c 20 76 6f 69 oid*, int n, voi
222c0 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 d(*)(void*));.SQ
222d0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
222e0 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 ite3_bind_double
222f0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
22300 69 6e 74 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 int, double);.SQ
22310 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
22320 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 ite3_bind_int(sq
22330 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
22340 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 , int);.SQLITE_A
22350 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
22360 69 6e 64 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 ind_int64(sqlite
22370 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 73 71 3_stmt*, int, sq
22380 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 lite3_int64);.SQ
22390 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
223a0 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 ite3_bind_null(s
223b0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
223c0 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 t);.SQLITE_API i
223d0 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
223e0 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d text(sqlite3_stm
223f0 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 t*, int, const c
22400 68 61 72 2a 2c 20 69 6e 74 20 6e 2c 20 76 6f 69 har*, int n, voi
22410 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 d(*)(void*));.SQ
22420 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
22430 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 ite3_bind_text16
22440 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
22450 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a int, const void*
22460 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 , int, void(*)(v
22470 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 oid*));.SQLITE_A
22480 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
22490 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 ind_value(sqlite
224a0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 3_stmt*, int, co
224b0 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 nst sqlite3_valu
224c0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 e*);.SQLITE_API
224d0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
224e0 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 _zeroblob(sqlite
224f0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 69 6e 3_stmt*, int, in
22500 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 t n);../*.** CAP
22510 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f 66 I3REF: Number Of
22520 20 53 51 4c 20 50 61 72 61 6d 65 74 65 72 73 20 SQL Parameters
22530 7b 48 31 33 36 30 30 7d 20 3c 53 37 30 33 30 30 {H13600} <S70300
22540 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 >.**.** This rou
22550 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73 65 64 tine can be used
22560 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d to find the num
22570 62 65 72 20 6f 66 20 5b 53 51 4c 20 70 61 72 61 ber of [SQL para
22580 6d 65 74 65 72 73 5d 0a 2a 2a 20 69 6e 20 61 20 meters].** in a
22590 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
225a0 65 6e 74 5d 2e 20 20 53 51 4c 20 70 61 72 61 6d ent]. SQL param
225b0 65 74 65 72 73 20 61 72 65 20 74 6f 6b 65 6e 73 eters are tokens
225c0 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d 20 of the.** form
225d0 22 3f 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 3a 41 "?", "?NNN", ":A
225e0 41 41 22 2c 20 22 24 41 41 41 22 2c 20 6f 72 20 AA", "$AAA", or
225f0 22 40 41 41 41 22 20 74 68 61 74 20 73 65 72 76 "@AAA" that serv
22600 65 20 61 73 0a 2a 2a 20 70 6c 61 63 65 68 6f 6c e as.** placehol
22610 64 65 72 73 20 66 6f 72 20 76 61 6c 75 65 73 20 ders for values
22620 74 68 61 74 20 61 72 65 20 5b 73 71 6c 69 74 65 that are [sqlite
22630 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62 6f 3_bind_blob | bo
22640 75 6e 64 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 70 und].** to the p
22650 61 72 61 6d 65 74 65 72 73 20 61 74 20 61 20 6c arameters at a l
22660 61 74 65 72 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a ater time..**.**
22670 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 This routine ac
22680 74 75 61 6c 6c 79 20 72 65 74 75 72 6e 73 20 74 tually returns t
22690 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 he index of the
226a0 6c 61 72 67 65 73 74 20 28 72 69 67 68 74 6d 6f largest (rightmo
226b0 73 74 29 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 st).** parameter
226c0 2e 20 46 6f 72 20 61 6c 6c 20 66 6f 72 6d 73 20 . For all forms
226d0 65 78 63 65 70 74 20 3f 4e 4e 4e 2c 20 74 68 69 except ?NNN, thi
226e0 73 20 77 69 6c 6c 20 63 6f 72 72 65 73 70 6f 6e s will correspon
226f0 64 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 d to the.** numb
22700 65 72 20 6f 66 20 75 6e 69 71 75 65 20 70 61 72 er of unique par
22710 61 6d 65 74 65 72 73 2e 20 20 49 66 20 70 61 72 ameters. If par
22720 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 3f ameters of the ?
22730 4e 4e 4e 20 61 72 65 20 75 73 65 64 2c 0a 2a 2a NNN are used,.**
22740 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 67 61 there may be ga
22750 70 73 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a ps in the list..
22760 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
22770 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c [sqlite3_bind_bl
22780 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 ob|sqlite3_bind(
22790 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f )],.** [sqlite3_
227a0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e bind_parameter_n
227b0 61 6d 65 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b ame()], and.** [
227c0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
227d0 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e ameter_index()].
227e0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
227f0 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 30 31 5d nts:.** [H13601]
22800 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
22810 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
22820 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 parameter_count(
22830 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a sqlite3_stmt*);.
22840 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
22850 20 4e 61 6d 65 20 4f 66 20 41 20 48 6f 73 74 20 Name Of A Host
22860 50 61 72 61 6d 65 74 65 72 20 7b 48 31 33 36 32 Parameter {H1362
22870 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 0} <S70300>.**.*
22880 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
22890 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
228a0 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 to the name of
228b0 74 68 65 20 6e 2d 74 68 0a 2a 2a 20 5b 53 51 4c the n-th.** [SQL
228c0 20 70 61 72 61 6d 65 74 65 72 5d 20 69 6e 20 61 parameter] in a
228d0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
228e0 6d 65 6e 74 5d 2e 0a 2a 2a 20 53 51 4c 20 70 61 ment]..** SQL pa
228f0 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 rameters of the
22900 66 6f 72 6d 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 form "?NNN" or "
22910 3a 41 41 41 22 20 6f 72 20 22 40 41 41 41 22 20 :AAA" or "@AAA"
22920 6f 72 20 22 24 41 41 41 22 0a 2a 2a 20 68 61 76 or "$AAA".** hav
22930 65 20 61 20 6e 61 6d 65 20 77 68 69 63 68 20 69 e a name which i
22940 73 20 74 68 65 20 73 74 72 69 6e 67 20 22 3f 4e s the string "?N
22950 4e 4e 22 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 NN" or ":AAA" or
22960 20 22 40 41 41 41 22 20 6f 72 20 22 24 41 41 41 "@AAA" or "$AAA
22970 22 0a 2a 2a 20 72 65 73 70 65 63 74 69 76 65 6c ".** respectivel
22980 79 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 y..** In other w
22990 6f 72 64 73 2c 20 74 68 65 20 69 6e 69 74 69 61 ords, the initia
229a0 6c 20 22 3a 22 20 6f 72 20 22 24 22 20 6f 72 20 l ":" or "$" or
229b0 22 40 22 20 6f 72 20 22 3f 22 0a 2a 2a 20 69 73 "@" or "?".** is
229c0 20 69 6e 63 6c 75 64 65 64 20 61 73 20 70 61 72 included as par
229d0 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 2e 0a 2a t of the name..*
229e0 2a 20 50 61 72 61 6d 65 74 65 72 73 20 6f 66 20 * Parameters of
229f0 74 68 65 20 66 6f 72 6d 20 22 3f 22 20 77 69 74 the form "?" wit
22a00 68 6f 75 74 20 61 20 66 6f 6c 6c 6f 77 69 6e 67 hout a following
22a10 20 69 6e 74 65 67 65 72 20 68 61 76 65 20 6e 6f integer have no
22a20 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64 20 61 72 65 name.** and are
22a30 20 61 6c 73 6f 20 72 65 66 65 72 72 65 64 20 74 also referred t
22a40 6f 20 61 73 20 22 61 6e 6f 6e 79 6d 6f 75 73 20 o as "anonymous
22a50 70 61 72 61 6d 65 74 65 72 73 22 2e 0a 2a 2a 0a parameters"..**.
22a60 2a 2a 20 54 68 65 20 66 69 72 73 74 20 68 6f 73 ** The first hos
22a70 74 20 70 61 72 61 6d 65 74 65 72 20 68 61 73 20 t parameter has
22a80 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2c 20 6e an index of 1, n
22a90 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ot 0..**.** If t
22aa0 68 65 20 76 61 6c 75 65 20 6e 20 69 73 20 6f 75 he value n is ou
22ab0 74 20 6f 66 20 72 61 6e 67 65 20 6f 72 20 69 66 t of range or if
22ac0 20 74 68 65 20 6e 2d 74 68 20 70 61 72 61 6d 65 the n-th parame
22ad0 74 65 72 20 69 73 0a 2a 2a 20 6e 61 6d 65 6c 65 ter is.** namele
22ae0 73 73 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 ss, then NULL is
22af0 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 returned. The
22b00 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 returned string
22b10 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20 69 6e 20 is.** always in
22b20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 65 UTF-8 encoding e
22b30 76 65 6e 20 69 66 20 74 68 65 20 6e 61 6d 65 64 ven if the named
22b40 20 70 61 72 61 6d 65 74 65 72 20 77 61 73 0a 2a parameter was.*
22b50 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 73 70 65 * originally spe
22b60 63 69 66 69 65 64 20 61 73 20 55 54 46 2d 31 36 cified as UTF-16
22b70 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 in [sqlite3_pre
22b80 70 61 72 65 31 36 28 29 5d 20 6f 72 0a 2a 2a 20 pare16()] or.**
22b90 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
22ba0 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 16_v2()]..**.**
22bb0 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 See also: [sqlit
22bc0 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c e3_bind_blob|sql
22bd0 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a ite3_bind()],.**
22be0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 [sqlite3_bind_p
22bf0 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 arameter_count()
22c00 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 ], and.** [sqlit
22c10 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 e3_bind_paramete
22c20 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a r_index()]..**.*
22c30 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
22c40 2a 2a 20 5b 48 31 33 36 32 31 5d 0a 2a 2f 0a 53 ** [H13621].*/.S
22c50 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
22c60 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 69 char *sqlite3_bi
22c70 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d nd_parameter_nam
22c80 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c e(sqlite3_stmt*,
22c90 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 int);../*.** CA
22ca0 50 49 33 52 45 46 3a 20 49 6e 64 65 78 20 4f 66 PI3REF: Index Of
22cb0 20 41 20 50 61 72 61 6d 65 74 65 72 20 57 69 74 A Parameter Wit
22cc0 68 20 41 20 47 69 76 65 6e 20 4e 61 6d 65 20 7b h A Given Name {
22cd0 48 31 33 36 34 30 7d 20 3c 53 37 30 33 30 30 3e H13640} <S70300>
22ce0 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 .**.** Return th
22cf0 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 53 51 e index of an SQ
22d00 4c 20 70 61 72 61 6d 65 74 65 72 20 67 69 76 65 L parameter give
22d10 6e 20 69 74 73 20 6e 61 6d 65 2e 20 20 54 68 65 n its name. The
22d20 0a 2a 2a 20 69 6e 64 65 78 20 76 61 6c 75 65 20 .** index value
22d30 72 65 74 75 72 6e 65 64 20 69 73 20 73 75 69 74 returned is suit
22d40 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20 able for use as
22d50 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 61 the second.** pa
22d60 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 rameter to [sqli
22d70 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 te3_bind_blob|sq
22d80 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2e 20 20 lite3_bind()].
22d90 41 20 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 65 74 A zero.** is ret
22da0 75 72 6e 65 64 20 69 66 20 6e 6f 20 6d 61 74 63 urned if no matc
22db0 68 69 6e 67 20 70 61 72 61 6d 65 74 65 72 20 69 hing parameter i
22dc0 73 20 66 6f 75 6e 64 2e 20 20 54 68 65 20 70 61 s found. The pa
22dd0 72 61 6d 65 74 65 72 0a 2a 2a 20 6e 61 6d 65 20 rameter.** name
22de0 6d 75 73 74 20 62 65 20 67 69 76 65 6e 20 69 6e must be given in
22df0 20 55 54 46 2d 38 20 65 76 65 6e 20 69 66 20 74 UTF-8 even if t
22e00 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 he original stat
22e10 65 6d 65 6e 74 0a 2a 2a 20 77 61 73 20 70 72 65 ement.** was pre
22e20 70 61 72 65 64 20 66 72 6f 6d 20 55 54 46 2d 31 pared from UTF-1
22e30 36 20 74 65 78 74 20 75 73 69 6e 67 20 5b 73 71 6 text using [sq
22e40 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f lite3_prepare16_
22e50 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 v2()]..**.** See
22e60 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f also: [sqlite3_
22e70 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 bind_blob|sqlite
22e80 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 3_bind()],.** [s
22e90 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
22ea0 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 20 meter_count()],
22eb0 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f and.** [sqlite3_
22ec0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 bind_parameter_i
22ed0 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 ndex()]..**.** R
22ee0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
22ef0 5b 48 31 33 36 34 31 5d 0a 2a 2f 0a 53 51 4c 49 [H13641].*/.SQLI
22f00 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
22f10 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 e3_bind_paramete
22f20 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65 33 5f r_index(sqlite3_
22f30 73 74 6d 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 stmt*, const cha
22f40 72 20 2a 7a 4e 61 6d 65 29 3b 0a 0a 2f 2a 0a 2a r *zName);../*.*
22f50 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 65 * CAPI3REF: Rese
22f60 74 20 41 6c 6c 20 42 69 6e 64 69 6e 67 73 20 4f t All Bindings O
22f70 6e 20 41 20 50 72 65 70 61 72 65 64 20 53 74 61 n A Prepared Sta
22f80 74 65 6d 65 6e 74 20 7b 48 31 33 36 36 30 7d 20 tement {H13660}
22f90 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 43 <S70300>.**.** C
22fa0 6f 6e 74 72 61 72 79 20 74 6f 20 74 68 65 20 69 ontrary to the i
22fb0 6e 74 75 69 74 69 6f 6e 20 6f 66 20 6d 61 6e 79 ntuition of many
22fc0 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 , [sqlite3_reset
22fd0 28 29 5d 20 64 6f 65 73 20 6e 6f 74 20 72 65 73 ()] does not res
22fe0 65 74 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 et.** the [sqlit
22ff0 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62 e3_bind_blob | b
23000 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 61 20 5b 70 indings] on a [p
23010 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
23020 74 5d 2e 0a 2a 2a 20 55 73 65 20 74 68 69 73 20 t]..** Use this
23030 72 6f 75 74 69 6e 65 20 74 6f 20 72 65 73 65 74 routine to reset
23040 20 61 6c 6c 20 68 6f 73 74 20 70 61 72 61 6d 65 all host parame
23050 74 65 72 73 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a ters to NULL..**
23060 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
23070 3a 0a 2a 2a 20 5b 48 31 33 36 36 31 5d 0a 2a 2f :.** [H13661].*/
23080 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
23090 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 sqlite3_clear_bi
230a0 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 ndings(sqlite3_s
230b0 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 tmt*);../*.** CA
230c0 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f PI3REF: Number O
230d0 66 20 43 6f 6c 75 6d 6e 73 20 49 6e 20 41 20 52 f Columns In A R
230e0 65 73 75 6c 74 20 53 65 74 20 7b 48 31 33 37 31 esult Set {H1371
230f0 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 0} <S10700>.**.*
23100 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
23110 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
23120 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 n the result set
23130 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 returned by the
23140 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 .** [prepared st
23150 61 74 65 6d 65 6e 74 5d 2e 20 54 68 69 73 20 72 atement]. This r
23160 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 30 outine returns 0
23170 20 69 66 20 70 53 74 6d 74 20 69 73 20 61 6e 20 if pStmt is an
23180 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 SQL.** statement
23190 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 that does not r
231a0 65 74 75 72 6e 20 64 61 74 61 20 28 66 6f 72 20 eturn data (for
231b0 65 78 61 6d 70 6c 65 20 61 6e 20 5b 55 50 44 41 example an [UPDA
231c0 54 45 5d 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 TE])..**.** Requ
231d0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
231e0 33 37 31 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 3711].*/.SQLITE_
231f0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
23200 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c column_count(sql
23210 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
23220 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
23230 45 46 3a 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 73 EF: Column Names
23240 20 49 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74 In A Result Set
23250 20 7b 48 31 33 37 32 30 7d 20 3c 53 31 30 37 30 {H13720} <S1070
23260 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 0>.**.** These r
23270 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 outines return t
23280 68 65 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 he name assigned
23290 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 to a particular
232a0 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 20 74 68 column.** in th
232b0 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 e result set of
232c0 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 a [SELECT] state
232d0 6d 65 6e 74 2e 20 20 54 68 65 20 73 71 6c 69 74 ment. The sqlit
232e0 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 e3_column_name()
232f0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 72 65 .** interface re
23300 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
23310 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e to a zero-termin
23320 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e ated UTF-8 strin
23330 67 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 g.** and sqlite3
23340 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 _column_name16()
23350 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
23360 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 er to a zero-ter
23370 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46 2d 31 minated.** UTF-1
23380 36 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 66 6 string. The f
23390 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 irst parameter i
233a0 73 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 s the [prepared
233b0 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 74 68 statement].** th
233c0 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 at implements th
233d0 65 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 e [SELECT] state
233e0 6d 65 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 ment. The second
233f0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 parameter is th
23400 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 e.** column numb
23410 65 72 2e 20 20 54 68 65 20 6c 65 66 74 6d 6f 73 er. The leftmos
23420 74 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 75 6d 62 t column is numb
23430 65 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 0..**.** The
23440 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 returned string
23450 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69 64 pointer is valid
23460 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 74 68 until either th
23470 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 e [prepared stat
23480 65 6d 65 6e 74 5d 0a 2a 2a 20 69 73 20 64 65 73 ement].** is des
23490 74 72 6f 79 65 64 20 62 79 20 5b 73 71 6c 69 74 troyed by [sqlit
234a0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f e3_finalize()] o
234b0 72 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 r until the next
234c0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 call to.** sqli
234d0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 te3_column_name(
234e0 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c ) or sqlite3_col
234f0 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 20 6f 6e 20 umn_name16() on
23500 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 2e the same column.
23510 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 .**.** If sqlite
23520 33 5f 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 3_malloc() fails
23530 20 64 75 72 69 6e 67 20 74 68 65 20 70 72 6f 63 during the proc
23540 65 73 73 69 6e 67 20 6f 66 20 65 69 74 68 65 72 essing of either
23550 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 28 66 6f 72 routine.** (for
23560 20 65 78 61 6d 70 6c 65 20 64 75 72 69 6e 67 20 example during
23570 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f a conversion fro
23580 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 31 m UTF-8 to UTF-1
23590 36 29 20 74 68 65 6e 20 61 0a 2a 2a 20 4e 55 4c 6) then a.** NUL
235a0 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 L pointer is ret
235b0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 urned..**.** The
235c0 20 6e 61 6d 65 20 6f 66 20 61 20 72 65 73 75 6c name of a resul
235d0 74 20 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 t column is the
235e0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 22 41 53 value of the "AS
235f0 22 20 63 6c 61 75 73 65 20 66 6f 72 0a 2a 2a 20 " clause for.**
23600 74 68 61 74 20 63 6f 6c 75 6d 6e 2c 20 69 66 20 that column, if
23610 74 68 65 72 65 20 69 73 20 61 6e 20 41 53 20 63 there is an AS c
23620 6c 61 75 73 65 2e 20 20 49 66 20 74 68 65 72 65 lause. If there
23630 20 69 73 20 6e 6f 20 41 53 20 63 6c 61 75 73 65 is no AS clause
23640 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6e 61 6d .** then the nam
23650 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 e of the column
23660 69 73 20 75 6e 73 70 65 63 69 66 69 65 64 20 61 is unspecified a
23670 6e 64 20 6d 61 79 20 63 68 61 6e 67 65 20 66 72 nd may change fr
23680 6f 6d 0a 2a 2a 20 6f 6e 65 20 72 65 6c 65 61 73 om.** one releas
23690 65 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 74 e of SQLite to t
236a0 68 65 20 6e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 52 he next..**.** R
236b0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
236c0 5b 48 31 33 37 32 31 5d 20 5b 48 31 33 37 32 33 [H13721] [H13723
236d0 5d 20 5b 48 31 33 37 32 34 5d 20 5b 48 31 33 37 ] [H13724] [H137
236e0 32 35 5d 20 5b 48 31 33 37 32 36 5d 20 5b 48 31 25] [H13726] [H1
236f0 33 37 32 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 3727].*/.SQLITE_
23700 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
23710 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e sqlite3_column_n
23720 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ame(sqlite3_stmt
23730 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 *, int N);.SQLIT
23740 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
23750 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
23760 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f _name16(sqlite3_
23770 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 0a stmt*, int N);..
23780 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
23790 53 6f 75 72 63 65 20 4f 66 20 44 61 74 61 20 49 Source Of Data I
237a0 6e 20 41 20 51 75 65 72 79 20 52 65 73 75 6c 74 n A Query Result
237b0 20 7b 48 31 33 37 34 30 7d 20 3c 53 31 30 37 30 {H13740} <S1070
237c0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 0>.**.** These r
237d0 6f 75 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 outines provide
237e0 61 20 6d 65 61 6e 73 20 74 6f 20 64 65 74 65 72 a means to deter
237f0 6d 69 6e 65 20 77 68 61 74 20 63 6f 6c 75 6d 6e mine what column
23800 20 6f 66 20 77 68 61 74 0a 2a 2a 20 74 61 62 6c of what.** tabl
23810 65 20 69 6e 20 77 68 69 63 68 20 64 61 74 61 62 e in which datab
23820 61 73 65 20 61 20 72 65 73 75 6c 74 20 6f 66 20 ase a result of
23830 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 a [SELECT] state
23840 6d 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f 6d 2e ment comes from.
23850 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 .** The name of
23860 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 the database or
23870 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20 table or column
23880 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64 20 can be returned
23890 61 73 0a 2a 2a 20 65 69 74 68 65 72 20 61 20 55 as.** either a U
238a0 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 73 TF-8 or UTF-16 s
238b0 74 72 69 6e 67 2e 20 20 54 68 65 20 5f 64 61 74 tring. The _dat
238c0 61 62 61 73 65 5f 20 72 6f 75 74 69 6e 65 73 20 abase_ routines
238d0 72 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 64 61 return.** the da
238e0 74 61 62 61 73 65 20 6e 61 6d 65 2c 20 74 68 65 tabase name, the
238f0 20 5f 74 61 62 6c 65 5f 20 72 6f 75 74 69 6e 65 _table_ routine
23900 73 20 72 65 74 75 72 6e 20 74 68 65 20 74 61 62 s return the tab
23910 6c 65 20 6e 61 6d 65 2c 20 61 6e 64 0a 2a 2a 20 le name, and.**
23920 74 68 65 20 6f 72 69 67 69 6e 5f 20 72 6f 75 74 the origin_ rout
23930 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68 65 20 ines return the
23940 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2a 20 column name..**
23950 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 The returned str
23960 69 6e 67 20 69 73 20 76 61 6c 69 64 20 75 6e 74 ing is valid unt
23970 69 6c 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 il the [prepared
23980 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 73 20 64 statement] is d
23990 65 73 74 72 6f 79 65 64 0a 2a 2a 20 75 73 69 6e estroyed.** usin
239a0 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c g [sqlite3_final
239b0 69 7a 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 ize()] or until
239c0 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 the same informa
239d0 74 69 6f 6e 20 69 73 20 72 65 71 75 65 73 74 65 tion is requeste
239e0 64 0a 2a 2a 20 61 67 61 69 6e 20 69 6e 20 61 20 d.** again in a
239f0 64 69 66 66 65 72 65 6e 74 20 65 6e 63 6f 64 69 different encodi
23a00 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 ng..**.** The na
23a10 6d 65 73 20 72 65 74 75 72 6e 65 64 20 61 72 65 mes returned are
23a20 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 75 6e the original un
23a30 2d 61 6c 69 61 73 65 64 20 6e 61 6d 65 73 20 6f -aliased names o
23a40 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 f the.** databas
23a50 65 2c 20 74 61 62 6c 65 2c 20 61 6e 64 20 63 6f e, table, and co
23a60 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 lumn..**.** The
23a70 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 first argument t
23a80 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 o the following
23a90 63 61 6c 6c 73 20 69 73 20 61 20 5b 70 72 65 70 calls is a [prep
23aa0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e ared statement].
23ab0 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 .** These functi
23ac0 6f 6e 73 20 72 65 74 75 72 6e 20 69 6e 66 6f 72 ons return infor
23ad0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 mation about the
23ae0 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 72 65 74 75 Nth column retu
23af0 72 6e 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 73 rned by.** the s
23b00 74 61 74 65 6d 65 6e 74 2c 20 77 68 65 72 65 20 tatement, where
23b10 4e 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 N is the second
23b20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e function argumen
23b30 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 t..**.** If the
23b40 4e 74 68 20 63 6f 6c 75 6d 6e 20 72 65 74 75 72 Nth column retur
23b50 6e 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65 ned by the state
23b60 6d 65 6e 74 20 69 73 20 61 6e 20 65 78 70 72 65 ment is an expre
23b70 73 73 69 6f 6e 20 6f 72 0a 2a 2a 20 73 75 62 71 ssion or.** subq
23b80 75 65 72 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 uery and is not
23b90 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 a column value,
23ba0 74 68 65 6e 20 61 6c 6c 20 6f 66 20 74 68 65 73 then all of thes
23bb0 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 e functions retu
23bc0 72 6e 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 54 68 65 rn.** NULL. The
23bd0 73 65 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 se routine might
23be0 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 4e 55 4c also return NUL
23bf0 4c 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c L if a memory al
23c00 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 0a 2a location error.*
23c10 2a 20 6f 63 63 75 72 73 2e 20 20 4f 74 68 65 72 * occurs. Other
23c20 77 69 73 65 2c 20 74 68 65 79 20 72 65 74 75 72 wise, they retur
23c30 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 n the name of th
23c40 65 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 e attached datab
23c50 61 73 65 2c 20 74 61 62 6c 65 0a 2a 2a 20 61 6e ase, table.** an
23c60 64 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 71 75 d column that qu
23c70 65 72 79 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d ery result colum
23c80 6e 20 77 61 73 20 65 78 74 72 61 63 74 65 64 20 n was extracted
23c90 66 72 6f 6d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 77 from..**.** As w
23ca0 69 74 68 20 61 6c 6c 20 6f 74 68 65 72 20 53 51 ith all other SQ
23cb0 4c 69 74 65 20 41 50 49 73 2c 20 74 68 6f 73 65 Lite APIs, those
23cc0 20 70 6f 73 74 66 69 78 65 64 20 77 69 74 68 20 postfixed with
23cd0 22 31 36 22 20 72 65 74 75 72 6e 0a 2a 2a 20 55 "16" return.** U
23ce0 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 73 74 TF-16 encoded st
23cf0 72 69 6e 67 73 2c 20 74 68 65 20 6f 74 68 65 72 rings, the other
23d00 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 functions retur
23d10 6e 20 55 54 46 2d 38 2e 20 7b 45 4e 44 7d 0a 2a n UTF-8. {END}.*
23d20 2a 0a 2a 2a 20 54 68 65 73 65 20 41 50 49 73 20 *.** These APIs
23d30 61 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 are only availab
23d40 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 le if the librar
23d50 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 y was compiled w
23d60 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 ith the.** [SQLI
23d70 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e TE_ENABLE_COLUMN
23d80 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65 _METADATA] C-pre
23d90 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c processor symbol
23da0 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 defined..**.**
23db0 7b 41 31 33 37 35 31 7d 0a 2a 2a 20 49 66 20 74 {A13751}.** If t
23dc0 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72 65 61 wo or more threa
23dd0 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20 6d ds call one or m
23de0 6f 72 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75 ore of these rou
23df0 74 69 6e 65 73 20 61 67 61 69 6e 73 74 20 74 68 tines against th
23e00 65 20 73 61 6d 65 0a 2a 2a 20 70 72 65 70 61 72 e same.** prepar
23e10 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 ed statement and
23e20 20 63 6f 6c 75 6d 6e 20 61 74 20 74 68 65 20 73 column at the s
23e30 61 6d 65 20 74 69 6d 65 20 74 68 65 6e 20 74 68 ame time then th
23e40 65 20 72 65 73 75 6c 74 73 20 61 72 65 0a 2a 2a e results are.**
23e50 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a undefined..**.*
23e60 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
23e70 2a 2a 20 5b 48 31 33 37 34 31 5d 20 5b 48 31 33 ** [H13741] [H13
23e80 37 34 32 5d 20 5b 48 31 33 37 34 33 5d 20 5b 48 742] [H13743] [H
23e90 31 33 37 34 34 5d 20 5b 48 31 33 37 34 35 5d 20 13744] [H13745]
23ea0 5b 48 31 33 37 34 36 5d 20 5b 48 31 33 37 34 38 [H13746] [H13748
23eb0 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 77 6f 20 6f ].**.** If two o
23ec0 72 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63 r more threads c
23ed0 61 6c 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a all one or more.
23ee0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ** [sqlite3_colu
23ef0 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 mn_database_name
23f00 20 7c 20 63 6f 6c 75 6d 6e 20 6d 65 74 61 64 61 | column metada
23f10 74 61 20 69 6e 74 65 72 66 61 63 65 73 5d 0a 2a ta interfaces].*
23f20 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 5b * for the same [
23f30 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
23f40 6e 74 5d 20 61 6e 64 20 72 65 73 75 6c 74 20 63 nt] and result c
23f50 6f 6c 75 6d 6e 0a 2a 2a 20 61 74 20 74 68 65 20 olumn.** at the
23f60 73 61 6d 65 20 74 69 6d 65 20 74 68 65 6e 20 74 same time then t
23f70 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 he results are u
23f80 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c ndefined..*/.SQL
23f90 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 ITE_API const ch
23fa0 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ar *sqlite3_colu
23fb0 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 mn_database_name
23fc0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 (sqlite3_stmt*,i
23fd0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 nt);.SQLITE_API
23fe0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
23ff0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 te3_column_datab
24000 61 73 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 ase_name16(sqlit
24010 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 e3_stmt*,int);.S
24020 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
24030 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f char *sqlite3_co
24040 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 28 lumn_table_name(
24050 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e sqlite3_stmt*,in
24060 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 t);.SQLITE_API c
24070 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
24080 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f e3_column_table_
24090 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 name16(sqlite3_s
240a0 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 tmt*,int);.SQLIT
240b0 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
240c0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
240d0 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c _origin_name(sql
240e0 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b ite3_stmt*,int);
240f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
24100 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
24110 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 column_origin_na
24120 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d me16(sqlite3_stm
24130 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 t*,int);../*.**
24140 43 41 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72 CAPI3REF: Declar
24150 65 64 20 44 61 74 61 74 79 70 65 20 4f 66 20 41 ed Datatype Of A
24160 20 51 75 65 72 79 20 52 65 73 75 6c 74 20 7b 48 Query Result {H
24170 31 33 37 36 30 7d 20 3c 53 31 30 37 30 30 3e 0a 13760} <S10700>.
24180 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 **.** The first
24190 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 5b parameter is a [
241a0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
241b0 6e 74 5d 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 nt]..** If this
241c0 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 5b statement is a [
241d0 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e SELECT] statemen
241e0 74 20 61 6e 64 20 74 68 65 20 4e 74 68 20 63 6f t and the Nth co
241f0 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72 lumn of the.** r
24200 65 74 75 72 6e 65 64 20 72 65 73 75 6c 74 20 73 eturned result s
24210 65 74 20 6f 66 20 74 68 61 74 20 5b 53 45 4c 45 et of that [SELE
24220 43 54 5d 20 69 73 20 61 20 74 61 62 6c 65 20 63 CT] is a table c
24230 6f 6c 75 6d 6e 20 28 6e 6f 74 20 61 6e 0a 2a 2a olumn (not an.**
24240 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 73 expression or s
24250 75 62 71 75 65 72 79 29 20 74 68 65 6e 20 74 68 ubquery) then th
24260 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 20 e declared type
24270 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 of the table.**
24280 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 6e column is return
24290 65 64 2e 20 20 49 66 20 74 68 65 20 4e 74 68 20 ed. If the Nth
242a0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 column of the re
242b0 73 75 6c 74 20 73 65 74 20 69 73 20 61 6e 0a 2a sult set is an.*
242c0 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 * expression or
242d0 73 75 62 71 75 65 72 79 2c 20 74 68 65 6e 20 61 subquery, then a
242e0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 NULL pointer is
242f0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 returned..** Th
24300 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e e returned strin
24310 67 20 69 73 20 61 6c 77 61 79 73 20 55 54 46 2d g is always UTF-
24320 38 20 65 6e 63 6f 64 65 64 2e 20 7b 45 4e 44 7d 8 encoded. {END}
24330 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 .**.** For examp
24340 6c 65 2c 20 67 69 76 65 6e 20 74 68 65 20 64 61 le, given the da
24350 74 61 62 61 73 65 20 73 63 68 65 6d 61 3a 0a 2a tabase schema:.*
24360 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c *.** CREATE TABL
24370 45 20 74 31 28 63 31 20 56 41 52 49 41 4e 54 29 E t1(c1 VARIANT)
24380 3b 0a 2a 2a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 ;.**.** and the
24390 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d following statem
243a0 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c ent to be compil
243b0 65 64 3a 0a 2a 2a 0a 2a 2a 20 53 45 4c 45 43 54 ed:.**.** SELECT
243c0 20 63 31 20 2b 20 31 2c 20 63 31 20 46 52 4f 4d c1 + 1, c1 FROM
243d0 20 74 31 3b 0a 2a 2a 0a 2a 2a 20 74 68 69 73 20 t1;.**.** this
243e0 72 6f 75 74 69 6e 65 20 77 6f 75 6c 64 20 72 65 routine would re
243f0 74 75 72 6e 20 74 68 65 20 73 74 72 69 6e 67 20 turn the string
24400 22 56 41 52 49 41 4e 54 22 20 66 6f 72 20 74 68 "VARIANT" for th
24410 65 20 73 65 63 6f 6e 64 20 72 65 73 75 6c 74 0a e second result.
24420 2a 2a 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 31 29 ** column (i==1)
24430 2c 20 61 6e 64 20 61 20 4e 55 4c 4c 20 70 6f 69 , and a NULL poi
24440 6e 74 65 72 20 66 6f 72 20 74 68 65 20 66 69 72 nter for the fir
24450 73 74 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e st result column
24460 20 28 69 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 53 (i==0)..**.** S
24470 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e 61 6d QLite uses dynam
24480 69 63 20 72 75 6e 2d 74 69 6d 65 20 74 79 70 69 ic run-time typi
24490 6e 67 2e 20 20 53 6f 20 6a 75 73 74 20 62 65 63 ng. So just bec
244a0 61 75 73 65 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a ause a column.**
244b0 20 69 73 20 64 65 63 6c 61 72 65 64 20 74 6f 20 is declared to
244c0 63 6f 6e 74 61 69 6e 20 61 20 70 61 72 74 69 63 contain a partic
244d0 75 6c 61 72 20 74 79 70 65 20 64 6f 65 73 20 6e ular type does n
244e0 6f 74 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 ot mean that the
244f0 0a 2a 2a 20 64 61 74 61 20 73 74 6f 72 65 64 20 .** data stored
24500 69 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 in that column i
24510 73 20 6f 66 20 74 68 65 20 64 65 63 6c 61 72 65 s of the declare
24520 64 20 74 79 70 65 2e 20 20 53 51 4c 69 74 65 20 d type. SQLite
24530 69 73 0a 2a 2a 20 73 74 72 6f 6e 67 6c 79 20 74 is.** strongly t
24540 79 70 65 64 2c 20 62 75 74 20 74 68 65 20 74 79 yped, but the ty
24550 70 69 6e 67 20 69 73 20 64 79 6e 61 6d 69 63 20 ping is dynamic
24560 6e 6f 74 20 73 74 61 74 69 63 2e 20 20 54 79 70 not static. Typ
24570 65 0a 2a 2a 20 69 73 20 61 73 73 6f 63 69 61 74 e.** is associat
24580 65 64 20 77 69 74 68 20 69 6e 64 69 76 69 64 75 ed with individu
24590 61 6c 20 76 61 6c 75 65 73 2c 20 6e 6f 74 20 77 al values, not w
245a0 69 74 68 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 ith the containe
245b0 72 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 68 6f rs.** used to ho
245c0 6c 64 20 74 68 6f 73 65 20 76 61 6c 75 65 73 2e ld those values.
245d0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
245e0 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 36 31 5d nts:.** [H13761]
245f0 20 5b 48 31 33 37 36 32 5d 20 5b 48 31 33 37 36 [H13762] [H1376
24600 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 3].*/.SQLITE_API
24610 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
24620 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c ite3_column_decl
24630 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d type(sqlite3_stm
24640 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t*,int);.SQLITE_
24650 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
24660 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 sqlite3_column_d
24670 65 63 6c 74 79 70 65 31 36 28 73 71 6c 69 74 65 ecltype16(sqlite
24680 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 3_stmt*,int);../
24690 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 *.** CAPI3REF: E
246a0 76 61 6c 75 61 74 65 20 41 6e 20 53 51 4c 20 53 valuate An SQL S
246b0 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 32 30 30 tatement {H13200
246c0 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a } <S10000>.**.**
246d0 20 41 66 74 65 72 20 61 20 5b 70 72 65 70 61 72 After a [prepar
246e0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 68 61 ed statement] ha
246f0 73 20 62 65 65 6e 20 70 72 65 70 61 72 65 64 20 s been prepared
24700 75 73 69 6e 67 20 65 69 74 68 65 72 0a 2a 2a 20 using either.**
24710 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
24720 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 _v2()] or [sqlit
24730 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 e3_prepare16_v2(
24740 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 )] or one of the
24750 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 legacy.** inter
24760 66 61 63 65 73 20 5b 73 71 6c 69 74 65 33 5f 70 faces [sqlite3_p
24770 72 65 70 61 72 65 28 29 5d 20 6f 72 20 5b 73 71 repare()] or [sq
24780 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 lite3_prepare16(
24790 29 5d 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f )], this functio
247a0 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 63 61 6c n.** must be cal
247b0 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 led one or more
247c0 74 69 6d 65 73 20 74 6f 20 65 76 61 6c 75 61 74 times to evaluat
247d0 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e e the statement.
247e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 74 61 69 .**.** The detai
247f0 6c 73 20 6f 66 20 74 68 65 20 62 65 68 61 76 69 ls of the behavi
24800 6f 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 or of the sqlite
24810 33 5f 73 74 65 70 28 29 20 69 6e 74 65 72 66 61 3_step() interfa
24820 63 65 20 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 ce depend.** on
24830 77 68 65 74 68 65 72 20 74 68 65 20 73 74 61 74 whether the stat
24840 65 6d 65 6e 74 20 77 61 73 20 70 72 65 70 61 72 ement was prepar
24850 65 64 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77 ed using the new
24860 65 72 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 er "v2" interfac
24870 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 e.** [sqlite3_pr
24880 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 epare_v2()] and
24890 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
248a0 31 36 5f 76 32 28 29 5d 20 6f 72 20 74 68 65 20 16_v2()] or the
248b0 6f 6c 64 65 72 20 6c 65 67 61 63 79 0a 2a 2a 20 older legacy.**
248c0 69 6e 74 65 72 66 61 63 65 20 5b 73 71 6c 69 74 interface [sqlit
248d0 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 61 6e e3_prepare()] an
248e0 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 d [sqlite3_prepa
248f0 72 65 31 36 28 29 5d 2e 20 20 54 68 65 20 75 73 re16()]. The us
24900 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 65 77 20 e of the.** new
24910 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 20 69 "v2" interface i
24920 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f s recommended fo
24930 72 20 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f r new applicatio
24940 6e 73 20 62 75 74 20 74 68 65 20 6c 65 67 61 63 ns but the legac
24950 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 77 y.** interface w
24960 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 ill continue to
24970 62 65 20 73 75 70 70 6f 72 74 65 64 2e 0a 2a 2a be supported..**
24980 0a 2a 2a 20 49 6e 20 74 68 65 20 6c 65 67 61 63 .** In the legac
24990 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 y interface, the
249a0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 77 69 return value wi
249b0 6c 6c 20 62 65 20 65 69 74 68 65 72 20 5b 53 51 ll be either [SQ
249c0 4c 49 54 45 5f 42 55 53 59 5d 2c 0a 2a 2a 20 5b LITE_BUSY],.** [
249d0 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53 SQLITE_DONE], [S
249e0 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 5b 53 51 4c QLITE_ROW], [SQL
249f0 49 54 45 5f 45 52 52 4f 52 5d 2c 20 6f 72 20 5b ITE_ERROR], or [
24a00 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e 0a SQLITE_MISUSE]..
24a10 2a 2a 20 57 69 74 68 20 74 68 65 20 22 76 32 22 ** With the "v2"
24a20 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 79 20 interface, any
24a30 6f 66 20 74 68 65 20 6f 74 68 65 72 20 5b 72 65 of the other [re
24a40 73 75 6c 74 20 63 6f 64 65 73 5d 20 6f 72 0a 2a sult codes] or.*
24a50 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 * [extended resu
24a60 6c 74 20 63 6f 64 65 73 5d 20 6d 69 67 68 74 20 lt codes] might
24a70 62 65 20 72 65 74 75 72 6e 65 64 20 61 73 20 77 be returned as w
24a80 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 ell..**.** [SQLI
24a90 54 45 5f 42 55 53 59 5d 20 6d 65 61 6e 73 20 74 TE_BUSY] means t
24aa0 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 hat the database
24ab0 20 65 6e 67 69 6e 65 20 77 61 73 20 75 6e 61 62 engine was unab
24ac0 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 le to acquire th
24ad0 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 6c 6f e.** database lo
24ae0 63 6b 73 20 69 74 20 6e 65 65 64 73 20 74 6f 20 cks it needs to
24af0 64 6f 20 69 74 73 20 6a 6f 62 2e 20 20 49 66 20 do its job. If
24b00 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 the statement is
24b10 20 61 20 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f a [COMMIT].** o
24b20 72 20 6f 63 63 75 72 73 20 6f 75 74 73 69 64 65 r occurs outside
24b30 20 6f 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20 of an explicit
24b40 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 transaction, the
24b50 6e 20 79 6f 75 20 63 61 6e 20 72 65 74 72 79 20 n you can retry
24b60 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 the.** statement
24b70 2e 20 20 49 66 20 74 68 65 20 73 74 61 74 65 6d . If the statem
24b80 65 6e 74 20 69 73 20 6e 6f 74 20 61 20 5b 43 4f ent is not a [CO
24b90 4d 4d 49 54 5d 20 61 6e 64 20 6f 63 63 75 72 73 MMIT] and occurs
24ba0 20 77 69 74 68 69 6e 20 61 0a 2a 2a 20 65 78 70 within a.** exp
24bb0 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f licit transactio
24bc0 6e 20 74 68 65 6e 20 79 6f 75 20 73 68 6f 75 6c n then you shoul
24bd0 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 d rollback the t
24be0 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 ransaction befor
24bf0 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e e.** continuing.
24c00 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 .**.** [SQLITE_D
24c10 4f 4e 45 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 ONE] means that
24c20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 the statement ha
24c30 73 20 66 69 6e 69 73 68 65 64 20 65 78 65 63 75 s finished execu
24c40 74 69 6e 67 0a 2a 2a 20 73 75 63 63 65 73 73 66 ting.** successf
24c50 75 6c 6c 79 2e 20 20 73 71 6c 69 74 65 33 5f 73 ully. sqlite3_s
24c60 74 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74 tep() should not
24c70 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e be called again
24c80 20 6f 6e 20 74 68 69 73 20 76 69 72 74 75 61 6c on this virtual
24c90 0a 2a 2a 20 6d 61 63 68 69 6e 65 20 77 69 74 68 .** machine with
24ca0 6f 75 74 20 66 69 72 73 74 20 63 61 6c 6c 69 6e out first callin
24cb0 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 g [sqlite3_reset
24cc0 28 29 5d 20 74 6f 20 72 65 73 65 74 20 74 68 65 ()] to reset the
24cd0 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 virtual.** mach
24ce0 69 6e 65 20 62 61 63 6b 20 74 6f 20 69 74 73 20 ine back to its
24cf0 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e 0a 2a initial state..*
24d00 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20 *.** If the SQL
24d10 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 statement being
24d20 65 78 65 63 75 74 65 64 20 72 65 74 75 72 6e 73 executed returns
24d30 20 61 6e 79 20 64 61 74 61 2c 20 74 68 65 6e 20 any data, then
24d40 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 0a 2a 2a 20 [SQLITE_ROW].**
24d50 69 73 20 72 65 74 75 72 6e 65 64 20 65 61 63 68 is returned each
24d60 20 74 69 6d 65 20 61 20 6e 65 77 20 72 6f 77 20 time a new row
24d70 6f 66 20 64 61 74 61 20 69 73 20 72 65 61 64 79 of data is ready
24d80 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20 for processing
24d90 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 by the.** caller
24da0 2e 20 54 68 65 20 76 61 6c 75 65 73 20 6d 61 79 . The values may
24db0 20 62 65 20 61 63 63 65 73 73 65 64 20 75 73 69 be accessed usi
24dc0 6e 67 20 74 68 65 20 5b 63 6f 6c 75 6d 6e 20 61 ng the [column a
24dd0 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d ccess functions]
24de0 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 ..** sqlite3_ste
24df0 70 28 29 20 69 73 20 63 61 6c 6c 65 64 20 61 67 p() is called ag
24e00 61 69 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 ain to retrieve
24e10 74 68 65 20 6e 65 78 74 20 72 6f 77 20 6f 66 20 the next row of
24e20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c data..**.** [SQL
24e30 49 54 45 5f 45 52 52 4f 52 5d 20 6d 65 61 6e 73 ITE_ERROR] means
24e40 20 74 68 61 74 20 61 20 72 75 6e 2d 74 69 6d 65 that a run-time
24e50 20 65 72 72 6f 72 20 28 73 75 63 68 20 61 73 20 error (such as
24e60 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 a constraint.**
24e70 76 69 6f 6c 61 74 69 6f 6e 29 20 68 61 73 20 6f violation) has o
24e80 63 63 75 72 72 65 64 2e 20 20 73 71 6c 69 74 65 ccurred. sqlite
24e90 33 5f 73 74 65 70 28 29 20 73 68 6f 75 6c 64 20 3_step() should
24ea0 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 not be called ag
24eb0 61 69 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d ain on.** the VM
24ec0 2e 20 4d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 . More informati
24ed0 6f 6e 20 6d 61 79 20 62 65 20 66 6f 75 6e 64 20 on may be found
24ee0 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 by calling [sqli
24ef0 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a te3_errmsg()]..*
24f00 2a 20 57 69 74 68 20 74 68 65 20 6c 65 67 61 63 * With the legac
24f10 79 20 69 6e 74 65 72 66 61 63 65 2c 20 61 20 6d y interface, a m
24f20 6f 72 65 20 73 70 65 63 69 66 69 63 20 65 72 72 ore specific err
24f30 6f 72 20 63 6f 64 65 20 28 66 6f 72 20 65 78 61 or code (for exa
24f40 6d 70 6c 65 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 mple,.** [SQLITE
24f50 5f 49 4e 54 45 52 52 55 50 54 5d 2c 20 5b 53 51 _INTERRUPT], [SQ
24f60 4c 49 54 45 5f 53 43 48 45 4d 41 5d 2c 20 5b 53 LITE_SCHEMA], [S
24f70 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5d 2c 20 QLITE_CORRUPT],
24f80 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 0a 2a 2a and so forth).**
24f90 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 can be obtained
24fa0 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c by calling [sql
24fb0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 6e ite3_reset()] on
24fc0 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 the.** [prepare
24fd0 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 d statement]. I
24fe0 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 n the "v2" inter
24ff0 66 61 63 65 2c 0a 2a 2a 20 74 68 65 20 6d 6f 72 face,.** the mor
25000 65 20 73 70 65 63 69 66 69 63 20 65 72 72 6f 72 e specific error
25010 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
25020 64 20 64 69 72 65 63 74 6c 79 20 62 79 20 73 71 d directly by sq
25030 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2a lite3_step()..**
25040 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 .** [SQLITE_MISU
25050 53 45 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 SE] means that t
25060 68 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 he this routine
25070 77 61 73 20 63 61 6c 6c 65 64 20 69 6e 61 70 70 was called inapp
25080 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 20 50 ropriately..** P
25090 65 72 68 61 70 73 20 69 74 20 77 61 73 20 63 61 erhaps it was ca
250a0 6c 6c 65 64 20 6f 6e 20 61 20 5b 70 72 65 70 61 lled on a [prepa
250b0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 red statement] t
250c0 68 61 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 hat has.** alrea
250d0 64 79 20 62 65 65 6e 20 5b 73 71 6c 69 74 65 33 dy been [sqlite3
250e0 5f 66 69 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61 _finalize | fina
250f0 6c 69 7a 65 64 5d 20 6f 72 20 6f 6e 20 6f 6e 65 lized] or on one
25100 20 74 68 61 74 20 68 61 64 0a 2a 2a 20 70 72 65 that had.** pre
25110 76 69 6f 75 73 6c 79 20 72 65 74 75 72 6e 65 64 viously returned
25120 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 [SQLITE_ERROR]
25130 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d or [SQLITE_DONE]
25140 2e 20 20 4f 72 20 69 74 20 63 6f 75 6c 64 0a 2a . Or it could.*
25150 2a 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 * be the case th
25160 61 74 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 at the same data
25170 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
25180 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 62 79 is being used by
25190 20 74 77 6f 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 two or.** more
251a0 74 68 72 65 61 64 73 20 61 74 20 74 68 65 20 73 threads at the s
251b0 61 6d 65 20 6d 6f 6d 65 6e 74 20 69 6e 20 74 69 ame moment in ti
251c0 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 47 6f 6f me..**.** <b>Goo
251d0 66 79 20 49 6e 74 65 72 66 61 63 65 20 41 6c 65 fy Interface Ale
251e0 72 74 3a 3c 2f 62 3e 20 49 6e 20 74 68 65 20 6c rt:</b> In the l
251f0 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 65 2c egacy interface,
25200 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 the sqlite3_ste
25210 70 28 29 0a 2a 2a 20 41 50 49 20 61 6c 77 61 79 p().** API alway
25220 73 20 72 65 74 75 72 6e 73 20 61 20 67 65 6e 65 s returns a gene
25230 72 69 63 20 65 72 72 6f 72 20 63 6f 64 65 2c 20 ric error code,
25240 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2c 20 [SQLITE_ERROR],
25250 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79 0a 2a 2a following any.**
25260 20 65 72 72 6f 72 20 6f 74 68 65 72 20 74 68 61 error other tha
25270 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 n [SQLITE_BUSY]
25280 61 6e 64 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 and [SQLITE_MISU
25290 53 45 5d 2e 20 20 59 6f 75 20 6d 75 73 74 20 63 SE]. You must c
252a0 61 6c 6c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f all.** [sqlite3_
252b0 72 65 73 65 74 28 29 5d 20 6f 72 20 5b 73 71 6c reset()] or [sql
252c0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d ite3_finalize()]
252d0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 66 69 6e in order to fin
252e0 64 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20 d one of the.**
252f0 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f 72 20 specific [error
25300 63 6f 64 65 73 5d 20 74 68 61 74 20 62 65 74 74 codes] that bett
25310 65 72 20 64 65 73 63 72 69 62 65 73 20 74 68 65 er describes the
25320 20 65 72 72 6f 72 2e 0a 2a 2a 20 57 65 20 61 64 error..** We ad
25330 6d 69 74 20 74 68 61 74 20 74 68 69 73 20 69 73 mit that this is
25340 20 61 20 67 6f 6f 66 79 20 64 65 73 69 67 6e 2e a goofy design.
25350 20 20 54 68 65 20 70 72 6f 62 6c 65 6d 20 68 61 The problem ha
25360 73 20 62 65 65 6e 20 66 69 78 65 64 0a 2a 2a 20 s been fixed.**
25370 77 69 74 68 20 74 68 65 20 22 76 32 22 20 69 6e with the "v2" in
25380 74 65 72 66 61 63 65 2e 20 20 49 66 20 79 6f 75 terface. If you
25390 20 70 72 65 70 61 72 65 20 61 6c 6c 20 6f 66 20 prepare all of
253a0 79 6f 75 72 20 53 51 4c 20 73 74 61 74 65 6d 65 your SQL stateme
253b0 6e 74 73 0a 2a 2a 20 75 73 69 6e 67 20 65 69 74 nts.** using eit
253c0 68 65 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 her [sqlite3_pre
253d0 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 pare_v2()] or [s
253e0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 qlite3_prepare16
253f0 5f 76 32 28 29 5d 20 69 6e 73 74 65 61 64 0a 2a _v2()] instead.*
25400 2a 20 6f 66 20 74 68 65 20 6c 65 67 61 63 79 20 * of the legacy
25410 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
25420 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
25430 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 69 6e _prepare16()] in
25440 74 65 72 66 61 63 65 73 2c 0a 2a 2a 20 74 68 65 terfaces,.** the
25450 6e 20 74 68 65 20 6d 6f 72 65 20 73 70 65 63 69 n the more speci
25460 66 69 63 20 5b 65 72 72 6f 72 20 63 6f 64 65 73 fic [error codes
25470 5d 20 61 72 65 20 72 65 74 75 72 6e 65 64 20 64 ] are returned d
25480 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 73 71 irectly.** by sq
25490 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20 54 lite3_step(). T
254a0 68 65 20 75 73 65 20 6f 66 20 74 68 65 20 22 76 he use of the "v
254b0 32 22 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 2" interface is
254c0 72 65 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a recommended..**.
254d0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
254e0 0a 2a 2a 20 5b 48 31 33 32 30 32 5d 20 5b 48 31 .** [H13202] [H1
254f0 35 33 30 34 5d 20 5b 48 31 35 33 30 36 5d 20 5b 5304] [H15306] [
25500 48 31 35 33 30 38 5d 20 5b 48 31 35 33 31 30 5d H15308] [H15310]
25510 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
25520 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 nt sqlite3_step(
25530 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a sqlite3_stmt*);.
25540 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
25550 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d Number of colum
25560 6e 73 20 69 6e 20 61 20 72 65 73 75 6c 74 20 73 ns in a result s
25570 65 74 20 7b 48 31 33 37 37 30 7d 20 3c 53 31 30 et {H13770} <S10
25580 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 700>.**.** Retur
25590 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
255a0 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 63 values in the c
255b0 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 urrent row of th
255c0 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2a e result set..**
255d0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
255e0 3a 0a 2a 2a 20 5b 48 31 33 37 37 31 5d 20 5b 48 :.** [H13771] [H
255f0 31 33 37 37 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 13772].*/.SQLITE
25600 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
25610 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 _data_count(sqli
25620 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
25630 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
25640 46 3a 20 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44 F: Fundamental D
25650 61 74 61 74 79 70 65 73 20 7b 48 31 30 32 36 35 atatypes {H10265
25660 7d 20 3c 53 31 30 31 31 30 3e 3c 53 31 30 31 32 } <S10110><S1012
25670 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 0>.** KEYWORDS:
25680 53 51 4c 49 54 45 5f 54 45 58 54 0a 2a 2a 0a 2a SQLITE_TEXT.**.*
25690 2a 20 7b 48 31 30 32 36 36 7d 20 45 76 65 72 79 * {H10266} Every
256a0 20 76 61 6c 75 65 20 69 6e 20 53 51 4c 69 74 65 value in SQLite
256b0 20 68 61 73 20 6f 6e 65 20 6f 66 20 66 69 76 65 has one of five
256c0 20 66 75 6e 64 61 6d 65 6e 74 61 6c 20 64 61 74 fundamental dat
256d0 61 74 79 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 atypes:.**.** <u
256e0 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 l>.** <li> 64-bi
256f0 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
25700 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 .** <li> 64-bit
25710 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f IEEE floating po
25720 69 6e 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c 6c int number.** <l
25730 69 3e 20 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c 69 i> string.** <li
25740 3e 20 42 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20 4e > BLOB.** <li> N
25750 55 4c 4c 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b 45 4e ULL.** </ul> {EN
25760 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 D}.**.** These c
25770 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 63 6f 64 onstants are cod
25780 65 73 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74 es for each of t
25790 68 6f 73 65 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a hose types..**.*
257a0 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 * Note that the
257b0 53 51 4c 49 54 45 5f 54 45 58 54 20 63 6f 6e 73 SQLITE_TEXT cons
257c0 74 61 6e 74 20 77 61 73 20 61 6c 73 6f 20 75 73 tant was also us
257d0 65 64 20 69 6e 20 53 51 4c 69 74 65 20 76 65 72 ed in SQLite ver
257e0 73 69 6f 6e 20 32 0a 2a 2a 20 66 6f 72 20 61 20 sion 2.** for a
257f0 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 66 66 65 completely diffe
25800 72 65 6e 74 20 6d 65 61 6e 69 6e 67 2e 20 20 53 rent meaning. S
25810 6f 66 74 77 61 72 65 20 74 68 61 74 20 6c 69 6e oftware that lin
25820 6b 73 20 61 67 61 69 6e 73 74 20 62 6f 74 68 0a ks against both.
25830 2a 2a 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f ** SQLite versio
25840 6e 20 32 20 61 6e 64 20 53 51 4c 69 74 65 20 76 n 2 and SQLite v
25850 65 72 73 69 6f 6e 20 33 20 73 68 6f 75 6c 64 20 ersion 3 should
25860 75 73 65 20 53 51 4c 49 54 45 33 5f 54 45 58 54 use SQLITE3_TEXT
25870 2c 20 6e 6f 74 0a 2a 2a 20 53 51 4c 49 54 45 5f , not.** SQLITE_
25880 54 45 58 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 TEXT..*/.#define
25890 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 SQLITE_INTEGER
258a0 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 1.#define SQLIT
258b0 45 5f 46 4c 4f 41 54 20 20 20 20 32 0a 23 64 65 E_FLOAT 2.#de
258c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 fine SQLITE_BLOB
258d0 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 4.#define S
258e0 51 4c 49 54 45 5f 4e 55 4c 4c 20 20 20 20 20 35 QLITE_NULL 5
258f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
25900 45 58 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 EXT.# undef SQLI
25910 54 45 5f 54 45 58 54 0a 23 65 6c 73 65 0a 23 20 TE_TEXT.#else.#
25920 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
25930 58 54 20 20 20 20 20 33 0a 23 65 6e 64 69 66 0a XT 3.#endif.
25940 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f #define SQLITE3_
25950 54 45 58 54 20 20 20 20 20 33 0a 0a 2f 2a 0a 2a TEXT 3../*.*
25960 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 75 * CAPI3REF: Resu
25970 6c 74 20 56 61 6c 75 65 73 20 46 72 6f 6d 20 41 lt Values From A
25980 20 51 75 65 72 79 20 7b 48 31 33 38 30 30 7d 20 Query {H13800}
25990 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 <S10700>.** KEYW
259a0 4f 52 44 53 3a 20 7b 63 6f 6c 75 6d 6e 20 61 63 ORDS: {column ac
259b0 63 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 7d 0a cess functions}.
259c0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 **.** These rout
259d0 69 6e 65 73 20 66 6f 72 6d 20 74 68 65 20 22 72 ines form the "r
259e0 65 73 75 6c 74 20 73 65 74 20 71 75 65 72 79 22 esult set query"
259f0 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a interface..**.*
25a00 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 * These routines
25a10 20 72 65 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74 return informat
25a20 69 6f 6e 20 61 62 6f 75 74 20 61 20 73 69 6e 67 ion about a sing
25a30 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 le column of the
25a40 20 63 75 72 72 65 6e 74 0a 2a 2a 20 72 65 73 75 current.** resu
25a50 6c 74 20 72 6f 77 20 6f 66 20 61 20 71 75 65 72 lt row of a quer
25a60 79 2e 20 20 49 6e 20 65 76 65 72 79 20 63 61 73 y. In every cas
25a70 65 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 e the first argu
25a80 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 ment is a pointe
25a90 72 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 70 72 65 r.** to the [pre
25aa0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
25ab0 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 65 that is being e
25ac0 76 61 6c 75 61 74 65 64 20 28 74 68 65 20 5b 73 valuated (the [s
25ad0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 0a 2a 2a qlite3_stmt*].**
25ae0 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e that was return
25af0 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 ed from [sqlite3
25b00 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f _prepare_v2()] o
25b10 72 20 6f 6e 65 20 6f 66 20 69 74 73 20 76 61 72 r one of its var
25b20 69 61 6e 74 73 29 0a 2a 2a 20 61 6e 64 20 74 68 iants).** and th
25b30 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
25b40 74 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f t is the index o
25b50 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72 f the column for
25b60 20 77 68 69 63 68 20 69 6e 66 6f 72 6d 61 74 69 which informati
25b70 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 on.** should be
25b80 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 6c returned. The l
25b90 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f eftmost column o
25ba0 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 f the result set
25bb0 20 68 61 73 20 74 68 65 20 69 6e 64 65 78 20 30 has the index 0
25bc0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 ..**.** If the S
25bd0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 64 6f 65 QL statement doe
25be0 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
25bf0 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 point to a valid
25c00 20 72 6f 77 2c 20 6f 72 20 69 66 20 74 68 65 0a row, or if the.
25c10 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 ** column index
25c20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c is out of range,
25c30 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 75 the result is u
25c40 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 ndefined..** The
25c50 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 61 79 20 se routines may
25c60 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 77 only be called w
25c70 68 65 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 hen the most rec
25c80 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b ent call to.** [
25c90 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
25ca0 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b 53 51 has returned [SQ
25cb0 4c 49 54 45 5f 52 4f 57 5d 20 61 6e 64 20 6e 65 LITE_ROW] and ne
25cc0 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 ither.** [sqlite
25cd0 33 5f 72 65 73 65 74 28 29 5d 20 6e 6f 72 20 5b 3_reset()] nor [
25ce0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
25cf0 28 29 5d 20 68 61 76 65 20 62 65 65 6e 20 63 61 ()] have been ca
25d00 6c 6c 65 64 20 73 75 62 73 65 71 75 65 6e 74 6c lled subsequentl
25d10 79 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 y..** If any of
25d20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 these routines a
25d30 72 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 re called after
25d40 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 [sqlite3_reset()
25d50 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ] or.** [sqlite3
25d60 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 _finalize()] or
25d70 61 66 74 65 72 20 5b 73 71 6c 69 74 65 33 5f 73 after [sqlite3_s
25d80 74 65 70 28 29 5d 20 68 61 73 20 72 65 74 75 72 tep()] has retur
25d90 6e 65 64 0a 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 ned.** something
25da0 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c other than [SQL
25db0 49 54 45 5f 52 4f 57 5d 2c 20 74 68 65 20 72 65 ITE_ROW], the re
25dc0 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 sults are undefi
25dd0 6e 65 64 2e 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 ned..** If [sqli
25de0 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b te3_step()] or [
25df0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d sqlite3_reset()]
25e00 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e or [sqlite3_fin
25e10 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 61 72 65 20 alize()].** are
25e20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 20 64 69 called from a di
25e30 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 20 77 fferent thread w
25e40 68 69 6c 65 20 61 6e 79 20 6f 66 20 74 68 65 73 hile any of thes
25e50 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 e routines.** ar
25e60 65 20 70 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20 e pending, then
25e70 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 the results are
25e80 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a undefined..**.**
25e90 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c The sqlite3_col
25ea0 75 6d 6e 5f 74 79 70 65 28 29 20 72 6f 75 74 69 umn_type() routi
25eb0 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a ne returns the.*
25ec0 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 * [SQLITE_INTEGE
25ed0 52 20 7c 20 64 61 74 61 74 79 70 65 20 63 6f 64 R | datatype cod
25ee0 65 5d 20 66 6f 72 20 74 68 65 20 69 6e 69 74 69 e] for the initi
25ef0 61 6c 20 64 61 74 61 20 74 79 70 65 0a 2a 2a 20 al data type.**
25f00 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f of the result co
25f10 6c 75 6d 6e 2e 20 20 54 68 65 20 72 65 74 75 72 lumn. The retur
25f20 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6f 6e 65 ned value is one
25f30 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 of [SQLITE_INTE
25f40 47 45 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 GER],.** [SQLITE
25f50 5f 46 4c 4f 41 54 5d 2c 20 5b 53 51 4c 49 54 45 _FLOAT], [SQLITE
25f60 5f 54 45 58 54 5d 2c 20 5b 53 51 4c 49 54 45 5f _TEXT], [SQLITE_
25f70 42 4c 4f 42 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 BLOB], or [SQLIT
25f80 45 5f 4e 55 4c 4c 5d 2e 20 20 54 68 65 20 76 61 E_NULL]. The va
25f90 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 lue.** returned
25fa0 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d by sqlite3_colum
25fb0 6e 5f 74 79 70 65 28 29 20 69 73 20 6f 6e 6c 79 n_type() is only
25fc0 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 6e meaningful if n
25fd0 6f 20 74 79 70 65 0a 2a 2a 20 63 6f 6e 76 65 72 o type.** conver
25fe0 73 69 6f 6e 73 20 68 61 76 65 20 6f 63 63 75 72 sions have occur
25ff0 72 65 64 20 61 73 20 64 65 73 63 72 69 62 65 64 red as described
26000 20 62 65 6c 6f 77 2e 20 20 41 66 74 65 72 20 61 below. After a
26010 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e type conversion
26020 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 72 ,.** the value r
26030 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
26040 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 e3_column_type()
26050 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 is undefined.
26060 46 75 74 75 72 65 0a 2a 2a 20 76 65 72 73 69 6f Future.** versio
26070 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d 61 79 ns of SQLite may
26080 20 63 68 61 6e 67 65 20 74 68 65 20 62 65 68 61 change the beha
26090 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 5f vior of sqlite3_
260a0 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 0a 2a 2a column_type().**
260b0 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 74 79 70 following a typ
260c0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a e conversion..**
260d0 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c .** If the resul
260e0 74 20 69 73 20 61 20 42 4c 4f 42 20 6f 72 20 55 t is a BLOB or U
260f0 54 46 2d 38 20 73 74 72 69 6e 67 20 74 68 65 6e TF-8 string then
26100 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c the sqlite3_col
26110 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 72 umn_bytes().** r
26120 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 outine returns t
26130 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
26140 65 73 20 69 6e 20 74 68 61 74 20 42 4c 4f 42 20 es in that BLOB
26150 6f 72 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 49 66 or string..** If
26160 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 the result is a
26170 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2c 20 UTF-16 string,
26180 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c then sqlite3_col
26190 75 6d 6e 5f 62 79 74 65 73 28 29 20 63 6f 6e 76 umn_bytes() conv
261a0 65 72 74 73 0a 2a 2a 20 74 68 65 20 73 74 72 69 erts.** the stri
261b0 6e 67 20 74 6f 20 55 54 46 2d 38 20 61 6e 64 20 ng to UTF-8 and
261c0 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 then returns the
261d0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
261e0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 ..** If the resu
261f0 6c 74 20 69 73 20 61 20 6e 75 6d 65 72 69 63 20 lt is a numeric
26200 76 61 6c 75 65 20 74 68 65 6e 20 73 71 6c 69 74 value then sqlit
26210 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 e3_column_bytes(
26220 29 20 75 73 65 73 0a 2a 2a 20 5b 73 71 6c 69 74 ) uses.** [sqlit
26230 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20 74 e3_snprintf()] t
26240 6f 20 63 6f 6e 76 65 72 74 20 74 68 61 74 20 76 o convert that v
26250 61 6c 75 65 20 74 6f 20 61 20 55 54 46 2d 38 20 alue to a UTF-8
26260 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 string and retur
26270 6e 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 ns.** the number
26280 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 61 of bytes in tha
26290 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 54 68 65 t string..** The
262a0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
262b0 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 does not include
262c0 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e the zero termin
262d0 61 74 6f 72 20 61 74 20 74 68 65 20 65 6e 64 0a ator at the end.
262e0 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 ** of the string
262f0 2e 20 20 46 6f 72 20 63 6c 61 72 69 74 79 3a 20 . For clarity:
26300 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
26310 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ed is the number
26320 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 of.** bytes in
26330 74 68 65 20 73 74 72 69 6e 67 2c 20 6e 6f 74 20 the string, not
26340 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 the number of ch
26350 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 aracters..**.**
26360 53 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 Strings returned
26370 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 by sqlite3_colu
26380 6d 6e 5f 74 65 78 74 28 29 20 61 6e 64 20 73 71 mn_text() and sq
26390 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
263a0 74 31 36 28 29 2c 0a 2a 2a 20 65 76 65 6e 20 65 t16(),.** even e
263b0 6d 70 74 79 20 73 74 72 69 6e 67 73 2c 20 61 72 mpty strings, ar
263c0 65 20 61 6c 77 61 79 73 20 7a 65 72 6f 20 74 65 e always zero te
263d0 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 20 72 rminated. The r
263e0 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 66 eturn.** value f
263f0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 rom sqlite3_colu
26400 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 72 20 61 20 mn_blob() for a
26410 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 zero-length BLOB
26420 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 is an arbitrary
26430 0a 2a 2a 20 70 6f 69 6e 74 65 72 2c 20 70 6f 73 .** pointer, pos
26440 73 69 62 6c 79 20 65 76 65 6e 20 61 20 4e 55 4c sibly even a NUL
26450 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a L pointer..**.**
26460 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c The sqlite3_col
26470 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 72 6f umn_bytes16() ro
26480 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 utine is similar
26490 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 to sqlite3_colu
264a0 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 62 75 mn_bytes().** bu
264b0 74 20 6c 65 61 76 65 73 20 74 68 65 20 72 65 73 t leaves the res
264c0 75 6c 74 20 69 6e 20 55 54 46 2d 31 36 20 69 6e ult in UTF-16 in
264d0 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 native byte ord
264e0 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 55 54 er instead of UT
264f0 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 7a 65 72 6f F-8..** The zero
26500 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 73 20 6e terminator is n
26510 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 ot included in t
26520 68 69 73 20 63 6f 75 6e 74 2e 0a 2a 2a 0a 2a 2a his count..**.**
26530 20 54 68 65 20 6f 62 6a 65 63 74 20 72 65 74 75 The object retu
26540 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 rned by [sqlite3
26550 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d _column_value()]
26560 20 69 73 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f is an.** [unpro
26570 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
26580 61 6c 75 65 5d 20 6f 62 6a 65 63 74 2e 20 20 41 alue] object. A
26590 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 n unprotected sq
265a0 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
265b0 63 74 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 ct.** may only b
265c0 65 20 75 73 65 64 20 77 69 74 68 20 5b 73 71 6c e used with [sql
265d0 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 ite3_bind_value(
265e0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
265f0 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d 2e result_value()].
26600 0a 2a 2a 20 49 66 20 74 68 65 20 5b 75 6e 70 72 .** If the [unpr
26610 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
26620 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 72 65 value] object re
26630 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71 turned by.** [sq
26640 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c lite3_column_val
26650 75 65 28 29 5d 20 69 73 20 75 73 65 64 20 69 6e ue()] is used in
26660 20 61 6e 79 20 6f 74 68 65 72 20 77 61 79 2c 20 any other way,
26670 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73 0a including calls.
26680 2a 2a 20 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c ** to routines l
26690 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c ike [sqlite3_val
266a0 75 65 5f 69 6e 74 28 29 5d 2c 20 5b 73 71 6c 69 ue_int()], [sqli
266b0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 te3_value_text()
266c0 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 ],.** or [sqlite
266d0 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 3_value_bytes()]
266e0 2c 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 , then the behav
266f0 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ior is undefined
26700 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f ..**.** These ro
26710 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 20 74 utines attempt t
26720 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 76 61 o convert the va
26730 6c 75 65 20 77 68 65 72 65 20 61 70 70 72 6f 70 lue where approp
26740 72 69 61 74 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 riate. For.** e
26750 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 69 xample, if the i
26760 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e nternal represen
26770 74 61 74 69 6f 6e 20 69 73 20 46 4c 4f 41 54 20 tation is FLOAT
26780 61 6e 64 20 61 20 74 65 78 74 20 72 65 73 75 6c and a text resul
26790 74 0a 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65 t.** is requeste
267a0 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 d, [sqlite3_snpr
267b0 69 6e 74 66 28 29 5d 20 69 73 20 75 73 65 64 20 intf()] is used
267c0 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 70 65 internally to pe
267d0 72 66 6f 72 6d 20 74 68 65 0a 2a 2a 20 63 6f 6e rform the.** con
267e0 76 65 72 73 69 6f 6e 20 61 75 74 6f 6d 61 74 69 version automati
267f0 63 61 6c 6c 79 2e 20 20 54 68 65 20 66 6f 6c 6c cally. The foll
26800 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 74 61 owing table deta
26810 69 6c 73 20 74 68 65 20 63 6f 6e 76 65 72 73 69 ils the conversi
26820 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 ons.** that are
26830 61 70 70 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c applied:.**.** <
26840 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c blockquote>.** <
26850 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 22 31 22 table border="1"
26860 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 49 6e >.** <tr><th> In
26870 74 65 72 6e 61 6c 3c 62 72 3e 54 79 70 65 20 3c ternal<br>Type <
26880 74 68 3e 20 52 65 71 75 65 73 74 65 64 3c 62 72 th> Requested<br
26890 3e 54 79 70 65 20 3c 74 68 3e 20 20 43 6f 6e 76 >Type <th> Conv
268a0 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 ersion.**.** <tr
268b0 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c ><td> NULL <
268c0 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74 td> INTEGER <t
268d0 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30 0a 2a d> Result is 0.*
268e0 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c * <tr><td> NULL
268f0 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 <td> FLOAT
26900 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 <td> Result i
26910 73 20 30 2e 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 s 0.0.** <tr><td
26920 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 > NULL <td>
26930 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 52 TEXT <td> R
26940 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f esult is NULL po
26950 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 inter.** <tr><td
26960 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 > NULL <td>
26970 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 52 BLOB <td> R
26980 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f esult is NULL po
26990 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 inter.** <tr><td
269a0 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 > INTEGER <td>
269b0 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 FLOAT <td> C
269c0 6f 6e 76 65 72 74 20 66 72 6f 6d 20 69 6e 74 65 onvert from inte
269d0 67 65 72 20 74 6f 20 66 6c 6f 61 74 0a 2a 2a 20 ger to float.**
269e0 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 <tr><td> INTEGER
269f0 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 <td> TEXT
26a00 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64 <td> ASCII rend
26a10 65 72 69 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 ering of the int
26a20 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e eger.** <tr><td>
26a30 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 INTEGER <td>
26a40 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 BLOB <td> Sa
26a50 6d 65 20 61 73 20 49 4e 54 45 47 45 52 2d 3e 54 me as INTEGER->T
26a60 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 EXT.** <tr><td>
26a70 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 49 4e FLOAT <td> IN
26a80 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e TEGER <td> Con
26a90 76 65 72 74 20 66 72 6f 6d 20 66 6c 6f 61 74 20 vert from float
26aa0 74 6f 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 to integer.** <t
26ab0 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 r><td> FLOAT
26ac0 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c <td> TEXT <
26ad0 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72 td> ASCII render
26ae0 69 6e 67 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 ing of the float
26af0 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c .** <tr><td> FL
26b00 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f OAT <td> BLO
26b10 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 B <td> Same a
26b20 73 20 46 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a 2a s FLOAT->TEXT.**
26b30 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 <tr><td> TEXT
26b40 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 <td> INTEGER
26b50 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f 69 28 <td> Use atoi(
26b60 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 ).** <tr><td> T
26b70 45 58 54 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f EXT <td> FLO
26b80 41 54 20 20 20 20 3c 74 64 3e 20 55 73 65 20 61 AT <td> Use a
26b90 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 tof().** <tr><td
26ba0 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 > TEXT <td>
26bb0 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 4e BLOB <td> N
26bc0 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 3c 74 72 3e o change.** <tr>
26bd0 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 <td> BLOB <t
26be0 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 d> INTEGER <td
26bf0 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 54 45 58 > Convert to TEX
26c00 54 20 74 68 65 6e 20 75 73 65 20 61 74 6f 69 28 T then use atoi(
26c10 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 ).** <tr><td> B
26c20 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f LOB <td> FLO
26c30 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 AT <td> Conve
26c40 72 74 20 74 6f 20 54 45 58 54 20 74 68 65 6e 20 rt to TEXT then
26c50 75 73 65 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 use atof().** <t
26c60 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 r><td> BLOB
26c70 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c <td> TEXT <
26c80 74 64 3e 20 41 64 64 20 61 20 7a 65 72 6f 20 74 td> Add a zero t
26c90 65 72 6d 69 6e 61 74 6f 72 20 69 66 20 6e 65 65 erminator if nee
26ca0 64 65 64 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a ded.** </table>.
26cb0 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e ** </blockquote>
26cc0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 .**.** The table
26cd0 20 61 62 6f 76 65 20 6d 61 6b 65 73 20 72 65 66 above makes ref
26ce0 65 72 65 6e 63 65 20 74 6f 20 73 74 61 6e 64 61 erence to standa
26cf0 72 64 20 43 20 6c 69 62 72 61 72 79 20 66 75 6e rd C library fun
26d00 63 74 69 6f 6e 73 20 61 74 6f 69 28 29 0a 2a 2a ctions atoi().**
26d10 20 61 6e 64 20 61 74 6f 66 28 29 2e 20 20 53 51 and atof(). SQ
26d20 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 Lite does not re
26d30 61 6c 6c 79 20 75 73 65 20 74 68 65 73 65 20 66 ally use these f
26d40 75 6e 63 74 69 6f 6e 73 2e 20 20 49 74 20 68 61 unctions. It ha
26d50 73 20 69 74 73 0a 2a 2a 20 6f 77 6e 20 65 71 75 s its.** own equ
26d60 69 76 61 6c 65 6e 74 20 69 6e 74 65 72 6e 61 6c ivalent internal
26d70 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 20 routines. The
26d80 61 74 6f 69 28 29 20 61 6e 64 20 61 74 6f 66 28 atoi() and atof(
26d90 29 20 6e 61 6d 65 73 20 61 72 65 0a 2a 2a 20 75 ) names are.** u
26da0 73 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 sed in the table
26db0 20 66 6f 72 20 62 72 65 76 69 74 79 20 61 6e 64 for brevity and
26dc0 20 62 65 63 61 75 73 65 20 74 68 65 79 20 61 72 because they ar
26dd0 65 20 66 61 6d 69 6c 69 61 72 20 74 6f 20 6d 6f e familiar to mo
26de0 73 74 0a 2a 2a 20 43 20 70 72 6f 67 72 61 6d 6d st.** C programm
26df0 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 ers..**.** Note
26e00 74 68 61 74 20 77 68 65 6e 20 74 79 70 65 20 63 that when type c
26e10 6f 6e 76 65 72 73 69 6f 6e 73 20 6f 63 63 75 72 onversions occur
26e20 2c 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 , pointers retur
26e30 6e 65 64 20 62 79 20 70 72 69 6f 72 0a 2a 2a 20 ned by prior.**
26e40 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 calls to sqlite3
26e50 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 _column_blob(),
26e60 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
26e70 65 78 74 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a 2a ext(), and/or.**
26e80 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
26e90 74 65 78 74 31 36 28 29 20 6d 61 79 20 62 65 20 text16() may be
26ea0 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 20 invalidated..**
26eb0 54 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 Type conversions
26ec0 20 61 6e 64 20 70 6f 69 6e 74 65 72 20 69 6e 76 and pointer inv
26ed0 61 6c 69 64 61 74 69 6f 6e 73 20 6d 69 67 68 74 alidations might
26ee0 20 6f 63 63 75 72 0a 2a 2a 20 69 6e 20 74 68 65 occur.** in the
26ef0 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 73 65 73 following cases
26f00 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 :.**.** <ul>.**
26f10 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c <li> The initial
26f20 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 20 42 4c content is a BL
26f30 4f 42 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 OB and sqlite3_c
26f40 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a olumn_text() or.
26f50 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ** sqlite3_
26f60 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 column_text16()
26f70 69 73 20 63 61 6c 6c 65 64 2e 20 20 41 20 7a 65 is called. A ze
26f80 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20 6d 69 ro-terminator mi
26f90 67 68 74 0a 2a 2a 20 20 20 20 20 20 6e 65 65 64 ght.** need
26fa0 20 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 to be added to
26fb0 74 68 65 20 73 74 72 69 6e 67 2e 3c 2f 6c 69 3e the string.</li>
26fc0 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 .** <li> The ini
26fd0 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 tial content is
26fe0 55 54 46 2d 38 20 74 65 78 74 20 61 6e 64 20 73 UTF-8 text and s
26ff0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
27000 74 65 73 31 36 28 29 20 6f 72 0a 2a 2a 20 20 20 tes16() or.**
27010 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d sqlite3_colum
27020 6e 5f 74 65 78 74 31 36 28 29 20 69 73 20 63 61 n_text16() is ca
27030 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e 74 65 lled. The conte
27040 6e 74 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 nt must be conve
27050 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 74 6f 20 rted.** to
27060 55 54 46 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a 2a 20 UTF-16.</li>.**
27070 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c <li> The initial
27080 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 46 2d content is UTF-
27090 31 36 20 74 65 78 74 20 61 6e 64 20 73 71 6c 69 16 text and sqli
270a0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 te3_column_bytes
270b0 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 () or.** sq
270c0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
270d0 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 t() is called.
270e0 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 The content must
270f0 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a be converted.**
27100 20 20 20 20 20 20 74 6f 20 55 54 46 2d 38 2e 3c to UTF-8.<
27110 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a /li>.** </ul>.**
27120 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 .** Conversions
27130 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36 62 65 between UTF-16be
27140 20 61 6e 64 20 55 54 46 2d 31 36 6c 65 20 61 72 and UTF-16le ar
27150 65 20 61 6c 77 61 79 73 20 64 6f 6e 65 20 69 6e e always done in
27160 20 70 6c 61 63 65 20 61 6e 64 20 64 6f 0a 2a 2a place and do.**
27170 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20 not invalidate
27180 61 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 2c a prior pointer,
27190 20 74 68 6f 75 67 68 20 6f 66 20 63 6f 75 72 73 though of cours
271a0 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 e the content of
271b0 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a 20 74 the buffer.** t
271c0 68 61 74 20 74 68 65 20 70 72 69 6f 72 20 70 6f hat the prior po
271d0 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 inter points to
271e0 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 6d will have been m
271f0 6f 64 69 66 69 65 64 2e 20 20 4f 74 68 65 72 20 odified. Other
27200 6b 69 6e 64 73 0a 2a 2a 20 6f 66 20 63 6f 6e 76 kinds.** of conv
27210 65 72 73 69 6f 6e 20 61 72 65 20 64 6f 6e 65 20 ersion are done
27220 69 6e 20 70 6c 61 63 65 20 77 68 65 6e 20 69 74 in place when it
27230 20 69 73 20 70 6f 73 73 69 62 6c 65 2c 20 62 75 is possible, bu
27240 74 20 73 6f 6d 65 74 69 6d 65 73 20 74 68 65 79 t sometimes they
27250 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 .** are not poss
27260 69 62 6c 65 20 61 6e 64 20 69 6e 20 74 68 6f 73 ible and in thos
27270 65 20 63 61 73 65 73 20 70 72 69 6f 72 20 70 6f e cases prior po
27280 69 6e 74 65 72 73 20 61 72 65 20 69 6e 76 61 6c inters are inval
27290 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 idated..**.** Th
272a0 65 20 73 61 66 65 73 74 20 61 6e 64 20 65 61 73 e safest and eas
272b0 69 65 73 74 20 74 6f 20 72 65 6d 65 6d 62 65 72 iest to remember
272c0 20 70 6f 6c 69 63 79 20 69 73 20 74 6f 20 69 6e policy is to in
272d0 76 6f 6b 65 20 74 68 65 73 65 20 72 6f 75 74 69 voke these routi
272e0 6e 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f 66 nes.** in one of
272f0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 the following w
27300 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a ays:.**.** <ul>.
27310 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f ** <li>sqlite3_
27320 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 66 6f column_text() fo
27330 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 llowed by sqlite
27340 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3_column_bytes()
27350 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 </li>.** <li>sq
27360 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f lite3_column_blo
27370 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 b() followed by
27380 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
27390 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 ytes()</li>.**
273a0 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 <li>sqlite3_colu
273b0 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 6f 6c 6c mn_text16() foll
273c0 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f owed by sqlite3_
273d0 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 column_bytes16()
273e0 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a </li>.** </ul>.*
273f0 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f *.** In other wo
27400 72 64 73 2c 20 79 6f 75 20 73 68 6f 75 6c 64 20 rds, you should
27410 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 63 6f 6c call sqlite3_col
27420 75 6d 6e 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 73 umn_text(),.** s
27430 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c qlite3_column_bl
27440 6f 62 28 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 ob(), or sqlite3
27450 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 _column_text16()
27460 20 66 69 72 73 74 20 74 6f 20 66 6f 72 63 65 20 first to force
27470 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e the result.** in
27480 74 6f 20 74 68 65 20 64 65 73 69 72 65 64 20 66 to the desired f
27490 6f 72 6d 61 74 2c 20 74 68 65 6e 20 69 6e 76 6f ormat, then invo
274a0 6b 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d ke sqlite3_colum
274b0 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a 2a 20 n_bytes() or.**
274c0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
274d0 79 74 65 73 31 36 28 29 20 74 6f 20 66 69 6e 64 ytes16() to find
274e0 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
274f0 20 72 65 73 75 6c 74 2e 20 20 44 6f 20 6e 6f 74 result. Do not
27500 20 6d 69 78 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f mix calls.** to
27510 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
27520 74 65 78 74 28 29 20 6f 72 20 73 71 6c 69 74 65 text() or sqlite
27530 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 3_column_blob()
27540 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a with calls to.**
27550 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
27560 62 79 74 65 73 31 36 28 29 2c 20 61 6e 64 20 64 bytes16(), and d
27570 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c 6c 73 20 o not mix calls
27580 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d to sqlite3_colum
27590 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 77 69 n_text16().** wi
275a0 74 68 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 th calls to sqli
275b0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 te3_column_bytes
275c0 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f ()..**.** The po
275d0 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64 20 inters returned
275e0 61 72 65 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 are valid until
275f0 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f a type conversio
27600 6e 20 6f 63 63 75 72 73 20 61 73 0a 2a 2a 20 64 n occurs as.** d
27610 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2c 20 escribed above,
27620 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65 or until [sqlite
27630 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b 73 71 3_step()] or [sq
27640 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f lite3_reset()] o
27650 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 r.** [sqlite3_fi
27660 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20 63 61 6c nalize()] is cal
27670 6c 65 64 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 led. The memory
27680 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20 68 space used to h
27690 6f 6c 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 61 old strings.** a
276a0 6e 64 20 42 4c 4f 42 73 20 69 73 20 66 72 65 65 nd BLOBs is free
276b0 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e d automatically.
276c0 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f 62 3e 20 Do <b>not</b>
276d0 70 61 73 73 20 74 68 65 20 70 6f 69 6e 74 65 72 pass the pointer
276e0 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 5b 73 s returned.** [s
276f0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c qlite3_column_bl
27700 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f ob()], [sqlite3_
27710 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2c 20 column_text()],
27720 65 74 63 2e 20 69 6e 74 6f 0a 2a 2a 20 5b 73 71 etc. into.** [sq
27730 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a lite3_free()]..*
27740 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 *.** If a memory
27750 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f allocation erro
27760 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 r occurs during
27770 74 68 65 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f the evaluation o
27780 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 73 f any.** of thes
27790 65 20 72 6f 75 74 69 6e 65 73 2c 20 61 20 64 65 e routines, a de
277a0 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 20 72 fault value is r
277b0 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 64 65 eturned. The de
277c0 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 69 fault value.** i
277d0 73 20 65 69 74 68 65 72 20 74 68 65 20 69 6e 74 s either the int
277e0 65 67 65 72 20 30 2c 20 74 68 65 20 66 6c 6f 61 eger 0, the floa
277f0 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 ting point numbe
27800 72 20 30 2e 30 2c 20 6f 72 20 61 20 4e 55 4c 4c r 0.0, or a NULL
27810 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20 53 75 .** pointer. Su
27820 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 bsequent calls t
27830 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f o [sqlite3_errco
27840 64 65 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 de()] will retur
27850 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d n.** [SQLITE_NOM
27860 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 EM]..**.** Requi
27870 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 rements:.** [H13
27880 38 30 33 5d 20 5b 48 31 33 38 30 36 5d 20 5b 48 803] [H13806] [H
27890 31 33 38 30 39 5d 20 5b 48 31 33 38 31 32 5d 20 13809] [H13812]
278a0 5b 48 31 33 38 31 35 5d 20 5b 48 31 33 38 31 38 [H13815] [H13818
278b0 5d 20 5b 48 31 33 38 32 31 5d 20 5b 48 31 33 38 ] [H13821] [H138
278c0 32 34 5d 0a 2a 2a 20 5b 48 31 33 38 32 37 5d 20 24].** [H13827]
278d0 5b 48 31 33 38 33 30 5d 0a 2a 2f 0a 53 51 4c 49 [H13830].*/.SQLI
278e0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
278f0 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d d *sqlite3_colum
27900 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 n_blob(sqlite3_s
27910 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b tmt*, int iCol);
27920 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
27930 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
27940 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d ytes(sqlite3_stm
27950 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 t*, int iCol);.S
27960 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
27970 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
27980 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d es16(sqlite3_stm
27990 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 t*, int iCol);.S
279a0 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 QLITE_API double
279b0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
279c0 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 double(sqlite3_s
279d0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b tmt*, int iCol);
279e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
279f0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 sqlite3_column_i
27a00 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a nt(sqlite3_stmt*
27a10 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c , int iCol);.SQL
27a20 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
27a30 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 63 6f int64 sqlite3_co
27a40 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c 69 74 lumn_int64(sqlit
27a50 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 e3_stmt*, int iC
27a60 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 ol);.SQLITE_API
27a70 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
27a80 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c har *sqlite3_col
27a90 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 65 33 umn_text(sqlite3
27aa0 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c _stmt*, int iCol
27ab0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f );.SQLITE_API co
27ac0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
27ad0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 3_column_text16(
27ae0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
27af0 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 nt iCol);.SQLITE
27b00 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
27b10 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71 6c _column_type(sql
27b20 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 ite3_stmt*, int
27b30 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 iCol);.SQLITE_AP
27b40 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 I sqlite3_value
27b50 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
27b60 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 74 value(sqlite3_st
27b70 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a mt*, int iCol);.
27b80 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
27b90 20 44 65 73 74 72 6f 79 20 41 20 50 72 65 70 61 Destroy A Prepa
27ba0 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 red Statement Ob
27bb0 6a 65 63 74 20 7b 48 31 33 33 30 30 7d 20 3c 53 ject {H13300} <S
27bc0 37 30 33 30 30 3e 3c 53 33 30 31 30 30 3e 0a 2a 70300><S30100>.*
27bd0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
27be0 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75 6e 63 _finalize() func
27bf0 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 tion is called t
27c00 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 72 65 70 o delete a [prep
27c10 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e ared statement].
27c20 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61 74 65 .** If the state
27c30 6d 65 6e 74 20 77 61 73 20 65 78 65 63 75 74 65 ment was execute
27c40 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f d successfully o
27c50 72 20 6e 6f 74 20 65 78 65 63 75 74 65 64 20 61 r not executed a
27c60 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a 2a 20 53 t all, then.** S
27c70 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
27c80 72 6e 65 64 2e 20 49 66 20 65 78 65 63 75 74 69 rned. If executi
27c90 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d on of the statem
27ca0 65 6e 74 20 66 61 69 6c 65 64 20 74 68 65 6e 20 ent failed then
27cb0 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 an.** [error cod
27cc0 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 20 e] or [extended
27cd0 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 error code] is r
27ce0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eturned..**.** T
27cf0 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 his routine can
27d00 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 6e 79 be called at any
27d10 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20 74 68 point during th
27d20 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 e execution of t
27d30 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 he.** [prepared
27d40 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 66 20 statement]. If
27d50 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 the virtual mach
27d60 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 63 ine has not.** c
27d70 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75 74 69 ompleted executi
27d80 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 on when this rou
27d90 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 tine is called,
27da0 74 68 61 74 20 69 73 20 6c 69 6b 65 0a 2a 2a 20 that is like.**
27db0 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 61 6e 20 encountering an
27dc0 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b 73 71 6c error or an [sql
27dd0 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 20 7c ite3_interrupt |
27de0 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a 2a 2a 20 interrupt]..**
27df0 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 64 61 74 Incomplete updat
27e00 65 73 20 6d 61 79 20 62 65 20 72 6f 6c 6c 65 64 es may be rolled
27e10 20 62 61 63 6b 20 61 6e 64 20 74 72 61 6e 73 61 back and transa
27e20 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c 65 64 2c ctions canceled,
27e30 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e .** depending on
27e40 20 74 68 65 20 63 69 72 63 75 6d 73 74 61 6e 63 the circumstanc
27e50 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 5b es, and the.** [
27e60 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 65 74 75 error code] retu
27e70 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b 53 51 rned will be [SQ
27e80 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a LITE_ABORT]..**.
27e90 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
27ea0 0a 2a 2a 20 5b 48 31 31 33 30 32 5d 20 5b 48 31 .** [H11302] [H1
27eb0 31 33 30 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 1304].*/.SQLITE_
27ec0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
27ed0 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 finalize(sqlite3
27ee0 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a _stmt *pStmt);..
27ef0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
27f00 52 65 73 65 74 20 41 20 50 72 65 70 61 72 65 64 Reset A Prepared
27f10 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63 Statement Objec
27f20 74 20 7b 48 31 33 33 33 30 7d 20 3c 53 37 30 33 t {H13330} <S703
27f30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 00>.**.** The sq
27f40 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 66 75 lite3_reset() fu
27f50 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
27f60 20 74 6f 20 72 65 73 65 74 20 61 20 5b 70 72 65 to reset a [pre
27f70 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
27f80 0a 2a 2a 20 6f 62 6a 65 63 74 20 62 61 63 6b 20 .** object back
27f90 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73 to its initial s
27fa0 74 61 74 65 2c 20 72 65 61 64 79 20 74 6f 20 62 tate, ready to b
27fb0 65 20 72 65 2d 65 78 65 63 75 74 65 64 2e 0a 2a e re-executed..*
27fc0 2a 20 41 6e 79 20 53 51 4c 20 73 74 61 74 65 6d * Any SQL statem
27fd0 65 6e 74 20 76 61 72 69 61 62 6c 65 73 20 74 68 ent variables th
27fe0 61 74 20 68 61 64 20 76 61 6c 75 65 73 20 62 6f at had values bo
27ff0 75 6e 64 20 74 6f 20 74 68 65 6d 20 75 73 69 6e und to them usin
28000 67 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 g.** the [sqlite
28010 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 73 71 3_bind_blob | sq
28020 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 41 lite3_bind_*() A
28030 50 49 5d 20 72 65 74 61 69 6e 20 74 68 65 69 72 PI] retain their
28040 20 76 61 6c 75 65 73 2e 0a 2a 2a 20 55 73 65 20 values..** Use
28050 5b 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 [sqlite3_clear_b
28060 69 6e 64 69 6e 67 73 28 29 5d 20 74 6f 20 72 65 indings()] to re
28070 73 65 74 20 74 68 65 20 62 69 6e 64 69 6e 67 73 set the bindings
28080 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 32 7d ..**.** {H11332}
28090 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 The [sqlite3_re
280a0 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66 61 63 set(S)] interfac
280b0 65 20 72 65 73 65 74 73 20 74 68 65 20 5b 70 72 e resets the [pr
280c0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
280d0 5d 20 53 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ] S.**
280e0 62 61 63 6b 20 74 6f 20 74 68 65 20 62 65 67 69 back to the begi
280f0 6e 6e 69 6e 67 20 6f 66 20 69 74 73 20 70 72 6f nning of its pro
28100 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 gram..**.** {H11
28110 33 33 34 7d 20 49 66 20 74 68 65 20 6d 6f 73 74 334} If the most
28120 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 recent call to
28130 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 [sqlite3_step(S)
28140 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 ] for the.**
28150 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 [prepared
28160 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 72 65 74 statement] S ret
28170 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 52 4f urned [SQLITE_RO
28180 57 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f W] or [SQLITE_DO
28190 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 NE],.**
281a0 20 6f 72 20 69 66 20 5b 73 71 6c 69 74 65 33 5f or if [sqlite3_
281b0 73 74 65 70 28 53 29 5d 20 68 61 73 20 6e 65 76 step(S)] has nev
281c0 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20 63 er before been c
281d0 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a 2a 2a 20 20 alled on S,.**
281e0 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 then [sq
281f0 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20 lite3_reset(S)]
28200 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
28210 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 OK]..**.** {H113
28220 33 36 7d 20 49 66 20 74 68 65 20 6d 6f 73 74 20 36} If the most
28230 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 5b recent call to [
28240 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d sqlite3_step(S)]
28250 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 for the.**
28260 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 [prepared s
28270 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e 64 69 tatement] S indi
28280 63 61 74 65 64 20 61 6e 20 65 72 72 6f 72 2c 20 cated an error,
28290 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 then.**
282a0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
282b0 53 29 5d 20 72 65 74 75 72 6e 73 20 61 6e 20 61 S)] returns an a
282c0 70 70 72 6f 70 72 69 61 74 65 20 5b 65 72 72 6f ppropriate [erro
282d0 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 7b r code]..**.** {
282e0 48 31 31 33 33 38 7d 20 54 68 65 20 5b 73 71 6c H11338} The [sql
282f0 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20 69 ite3_reset(S)] i
28300 6e 74 65 72 66 61 63 65 20 64 6f 65 73 20 6e 6f nterface does no
28310 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c t change the val
28320 75 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ues.**
28330 6f 66 20 61 6e 79 20 5b 73 71 6c 69 74 65 33 5f of any [sqlite3_
28340 62 69 6e 64 5f 62 6c 6f 62 7c 62 69 6e 64 69 6e bind_blob|bindin
28350 67 73 5d 20 6f 6e 20 74 68 65 20 5b 70 72 65 70 gs] on the [prep
28360 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
28370 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 S..*/.SQLITE_API
28380 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 73 int sqlite3_res
28390 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 et(sqlite3_stmt
283a0 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 *pStmt);../*.**
283b0 43 41 50 49 33 52 45 46 3a 20 43 72 65 61 74 65 CAPI3REF: Create
283c0 20 4f 72 20 52 65 64 65 66 69 6e 65 20 53 51 4c Or Redefine SQL
283d0 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 36 31 Functions {H161
283e0 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 20 00} <S20200>.**
283f0 4b 45 59 57 4f 52 44 53 3a 20 7b 66 75 6e 63 74 KEYWORDS: {funct
28400 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f 75 ion creation rou
28410 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 tines}.** KEYWOR
28420 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 6f 6e DS: {application
28430 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e -defined SQL fun
28440 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45 59 57 4f 52 ction}.** KEYWOR
28450 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 6f 6e DS: {application
28460 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e -defined SQL fun
28470 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 ctions}.**.** Th
28480 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e ese two function
28490 73 20 28 63 6f 6c 6c 65 63 74 69 76 65 6c 79 20 s (collectively
284a0 6b 6e 6f 77 6e 20 61 73 20 22 66 75 6e 63 74 69 known as "functi
284b0 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f 75 74 on creation rout
284c0 69 6e 65 73 22 29 0a 2a 2a 20 61 72 65 20 75 73 ines").** are us
284d0 65 64 20 74 6f 20 61 64 64 20 53 51 4c 20 66 75 ed to add SQL fu
284e0 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72 65 nctions or aggre
284f0 67 61 74 65 73 20 6f 72 20 74 6f 20 72 65 64 65 gates or to rede
28500 66 69 6e 65 20 74 68 65 20 62 65 68 61 76 69 6f fine the behavio
28510 72 0a 2a 2a 20 6f 66 20 65 78 69 73 74 69 6e 67 r.** of existing
28520 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f SQL functions o
28530 72 20 61 67 67 72 65 67 61 74 65 73 2e 20 20 54 r aggregates. T
28540 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e he only differen
28550 63 65 20 62 65 74 77 65 65 6e 20 74 68 65 0a 2a ce between the.*
28560 2a 20 74 77 6f 20 69 73 20 74 68 61 74 20 74 68 * two is that th
28570 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
28580 65 72 2c 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 er, the name of
28590 74 68 65 20 28 73 63 61 6c 61 72 29 20 66 75 6e the (scalar) fun
285a0 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 ction or.** aggr
285b0 65 67 61 74 65 2c 20 69 73 20 65 6e 63 6f 64 65 egate, is encode
285c0 64 20 69 6e 20 55 54 46 2d 38 20 66 6f 72 20 73 d in UTF-8 for s
285d0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
285e0 6e 63 74 69 6f 6e 28 29 20 61 6e 64 20 55 54 46 nction() and UTF
285f0 2d 31 36 0a 2a 2a 20 66 6f 72 20 73 71 6c 69 74 -16.** for sqlit
28600 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
28610 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 on16()..**.** Th
28620 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
28630 72 20 69 73 20 74 68 65 20 5b 64 61 74 61 62 61 r is the [databa
28640 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 se connection] t
28650 6f 20 77 68 69 63 68 20 74 68 65 20 53 51 4c 0a o which the SQL.
28660 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 ** function is t
28670 6f 20 62 65 20 61 64 64 65 64 2e 20 20 49 66 20 o be added. If
28680 61 20 73 69 6e 67 6c 65 20 70 72 6f 67 72 61 6d a single program
28690 20 75 73 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 uses more than
286a0 6f 6e 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 one database.**
286b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 74 65 72 connection inter
286c0 6e 61 6c 6c 79 2c 20 74 68 65 6e 20 53 51 4c 20 nally, then SQL
286d0 66 75 6e 63 74 69 6f 6e 73 20 6d 75 73 74 20 62 functions must b
286e0 65 20 61 64 64 65 64 20 69 6e 64 69 76 69 64 75 e added individu
286f0 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65 61 63 68 20 ally to.** each
28700 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
28710 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ion..**.** The s
28720 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
28730 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 is the name of t
28740 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 he SQL function
28750 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 6f 72 to be created or
28760 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e 20 20 .** redefined.
28770 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 The length of th
28780 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d 69 74 65 e name is limite
28790 64 20 74 6f 20 32 35 35 20 62 79 74 65 73 2c 20 d to 255 bytes,
287a0 65 78 63 6c 75 73 69 76 65 20 6f 66 0a 2a 2a 20 exclusive of.**
287b0 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 the zero-termina
287c0 74 6f 72 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 tor. Note that
287d0 74 68 65 20 6e 61 6d 65 20 6c 65 6e 67 74 68 20 the name length
287e0 6c 69 6d 69 74 20 69 73 20 69 6e 20 62 79 74 65 limit is in byte
287f0 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68 61 72 61 63 s, not.** charac
28800 74 65 72 73 2e 20 20 41 6e 79 20 61 74 74 65 6d ters. Any attem
28810 70 74 20 74 6f 20 63 72 65 61 74 65 20 61 20 66 pt to create a f
28820 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20 6c unction with a l
28830 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a 2a 20 77 69 onger name.** wi
28840 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 5b 53 51 ll result in [SQ
28850 4c 49 54 45 5f 45 52 52 4f 52 5d 20 62 65 69 6e LITE_ERROR] bein
28860 67 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a g returned..**.*
28870 2a 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61 * The third para
28880 6d 65 74 65 72 20 28 6e 41 72 67 29 0a 2a 2a 20 meter (nArg).**
28890 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
288a0 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 arguments that
288b0 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e the SQL function
288c0 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 or.** aggregate
288d0 20 74 61 6b 65 73 2e 20 49 66 20 74 68 69 73 20 takes. If this
288e0 70 61 72 61 6d 65 74 65 72 20 69 73 20 2d 31 2c parameter is -1,
288f0 20 74 68 65 6e 20 74 68 65 20 53 51 4c 20 66 75 then the SQL fu
28900 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 nction or.** agg
28910 72 65 67 61 74 65 20 6d 61 79 20 74 61 6b 65 20 regate may take
28920 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 any number of ar
28930 67 75 6d 65 6e 74 73 20 62 65 74 77 65 65 6e 20 guments between
28940 30 20 61 6e 64 20 74 68 65 20 6c 69 6d 69 74 0a 0 and the limit.
28950 2a 2a 20 73 65 74 20 62 79 20 5b 73 71 6c 69 74 ** set by [sqlit
28960 65 33 5f 6c 69 6d 69 74 5d 28 5b 53 51 4c 49 54 e3_limit]([SQLIT
28970 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e E_LIMIT_FUNCTION
28980 5f 41 52 47 5d 29 2e 20 20 49 66 20 74 68 65 20 _ARG]). If the
28990 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 6d 65 74 third.** paramet
289a0 65 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 er is less than
289b0 2d 31 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 -1 or greater th
289c0 61 6e 20 31 32 37 20 74 68 65 6e 20 74 68 65 20 an 127 then the
289d0 62 65 68 61 76 69 6f 72 20 69 73 0a 2a 2a 20 75 behavior is.** u
289e0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 ndefined..**.**
289f0 54 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d The fourth param
28a00 65 74 65 72 2c 20 65 54 65 78 74 52 65 70 2c 20 eter, eTextRep,
28a10 73 70 65 63 69 66 69 65 73 20 77 68 61 74 0a 2a specifies what.*
28a20 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 38 20 7c * [SQLITE_UTF8 |
28a30 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 5d 20 text encoding]
28a40 74 68 69 73 20 53 51 4c 20 66 75 6e 63 74 69 6f this SQL functio
28a50 6e 20 70 72 65 66 65 72 73 20 66 6f 72 0a 2a 2a n prefers for.**
28a60 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 73 2e its parameters.
28a70 20 20 41 6e 79 20 53 51 4c 20 66 75 6e 63 74 69 Any SQL functi
28a80 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f on implementatio
28a90 6e 20 73 68 6f 75 6c 64 20 62 65 20 61 62 6c 65 n should be able
28aa0 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 77 6f 72 6b to work.** work
28ab0 20 77 69 74 68 20 55 54 46 2d 38 2c 20 55 54 46 with UTF-8, UTF
28ac0 2d 31 36 6c 65 2c 20 6f 72 20 55 54 46 2d 31 36 -16le, or UTF-16
28ad0 62 65 2e 20 20 42 75 74 20 73 6f 6d 65 20 69 6d be. But some im
28ae0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6d 61 plementations ma
28af0 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 65 66 66 y be.** more eff
28b00 69 63 69 65 6e 74 20 77 69 74 68 20 6f 6e 65 20 icient with one
28b10 65 6e 63 6f 64 69 6e 67 20 74 68 61 6e 20 61 6e encoding than an
28b20 6f 74 68 65 72 2e 20 20 41 6e 20 61 70 70 6c 69 other. An appli
28b30 63 61 74 69 6f 6e 20 6d 61 79 0a 2a 2a 20 69 6e cation may.** in
28b40 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 72 65 voke sqlite3_cre
28b50 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 20 6f ate_function() o
28b60 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 r sqlite3_create
28b70 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 20 6d 75 _function16() mu
28b80 6c 74 69 70 6c 65 0a 2a 2a 20 74 69 6d 65 73 20 ltiple.** times
28b90 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 66 75 with the same fu
28ba0 6e 63 74 69 6f 6e 20 62 75 74 20 77 69 74 68 20 nction but with
28bb0 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 73 different values
28bc0 20 6f 66 20 65 54 65 78 74 52 65 70 2e 0a 2a 2a of eTextRep..**
28bd0 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 69 When multiple i
28be0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f mplementations o
28bf0 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 f the same funct
28c00 69 6f 6e 20 61 72 65 20 61 76 61 69 6c 61 62 6c ion are availabl
28c10 65 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c e, SQLite.** wil
28c20 6c 20 70 69 63 6b 20 74 68 65 20 6f 6e 65 20 74 l pick the one t
28c30 68 61 74 20 69 6e 76 6f 6c 76 65 73 20 74 68 65 hat involves the
28c40 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74 20 6f 66 least amount of
28c50 20 64 61 74 61 20 63 6f 6e 76 65 72 73 69 6f 6e data conversion
28c60 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 ..** If there is
28c70 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 69 only a single i
28c80 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 68 mplementation wh
28c90 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 63 61 72 ich does not car
28ca0 65 20 77 68 61 74 20 74 65 78 74 0a 2a 2a 20 65 e what text.** e
28cb0 6e 63 6f 64 69 6e 67 20 69 73 20 75 73 65 64 2c ncoding is used,
28cc0 20 74 68 65 6e 20 74 68 65 20 66 6f 75 72 74 68 then the fourth
28cd0 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 argument should
28ce0 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 4e 59 5d be [SQLITE_ANY]
28cf0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 66 74 ..**.** The fift
28d00 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 h parameter is a
28d10 6e 20 61 72 62 69 74 72 61 72 79 20 70 6f 69 6e n arbitrary poin
28d20 74 65 72 2e 20 20 54 68 65 20 69 6d 70 6c 65 6d ter. The implem
28d30 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a entation of the.
28d40 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 ** function can
28d50 67 61 69 6e 20 61 63 63 65 73 73 20 74 6f 20 74 gain access to t
28d60 68 69 73 20 70 6f 69 6e 74 65 72 20 75 73 69 6e his pointer usin
28d70 67 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 72 5f g [sqlite3_user_
28d80 64 61 74 61 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 data()]..**.** T
28d90 68 65 20 73 65 76 65 6e 74 68 2c 20 65 69 67 68 he seventh, eigh
28da0 74 68 20 61 6e 64 20 6e 69 6e 74 68 20 70 61 72 th and ninth par
28db0 61 6d 65 74 65 72 73 2c 20 78 46 75 6e 63 2c 20 ameters, xFunc,
28dc0 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61 6c xStep and xFinal
28dd0 2c 20 61 72 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 , are.** pointer
28de0 73 20 74 6f 20 43 2d 6c 61 6e 67 75 61 67 65 20 s to C-language
28df0 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 functions that i
28e00 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c mplement the SQL
28e10 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 function or.**
28e20 61 67 67 72 65 67 61 74 65 2e 20 41 20 73 63 61 aggregate. A sca
28e30 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e lar SQL function
28e40 20 72 65 71 75 69 72 65 73 20 61 6e 20 69 6d 70 requires an imp
28e50 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
28e60 68 65 20 78 46 75 6e 63 0a 2a 2a 20 63 61 6c 6c he xFunc.** call
28e70 62 61 63 6b 20 6f 6e 6c 79 2c 20 4e 55 4c 4c 20 back only, NULL
28e80 70 6f 69 6e 74 65 72 73 20 73 68 6f 75 6c 64 20 pointers should
28e90 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 be passed as the
28ea0 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61 xStep and xFina
28eb0 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 2e l.** parameters.
28ec0 20 41 6e 20 61 67 67 72 65 67 61 74 65 20 53 51 An aggregate SQ
28ed0 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 L function requi
28ee0 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 res an implement
28ef0 61 74 69 6f 6e 20 6f 66 20 78 53 74 65 70 0a 2a ation of xStep.*
28f00 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20 61 6e 64 * and xFinal and
28f10 20 4e 55 4c 4c 20 73 68 6f 75 6c 64 20 62 65 20 NULL should be
28f20 70 61 73 73 65 64 20 66 6f 72 20 78 46 75 6e 63 passed for xFunc
28f30 2e 20 54 6f 20 64 65 6c 65 74 65 20 61 6e 20 65 . To delete an e
28f40 78 69 73 74 69 6e 67 0a 2a 2a 20 53 51 4c 20 66 xisting.** SQL f
28f50 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 unction or aggre
28f60 67 61 74 65 2c 20 70 61 73 73 20 4e 55 4c 4c 20 gate, pass NULL
28f70 66 6f 72 20 61 6c 6c 20 74 68 72 65 65 20 66 75 for all three fu
28f80 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 73 nction callbacks
28f90 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 70 65 ..**.** It is pe
28fa0 72 6d 69 74 74 65 64 20 74 6f 20 72 65 67 69 73 rmitted to regis
28fb0 74 65 72 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 ter multiple imp
28fc0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 lementations of
28fd0 74 68 65 20 73 61 6d 65 0a 2a 2a 20 66 75 6e 63 the same.** func
28fe0 74 69 6f 6e 73 20 77 69 74 68 20 74 68 65 20 73 tions with the s
28ff0 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77 69 74 ame name but wit
29000 68 20 65 69 74 68 65 72 20 64 69 66 66 65 72 69 h either differi
29010 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 0a 2a 2a ng numbers of.**
29020 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 64 69 arguments or di
29030 66 66 65 72 69 6e 67 20 70 72 65 66 65 72 72 65 ffering preferre
29040 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 73 d text encodings
29050 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 . SQLite will u
29060 73 65 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d se.** the implem
29070 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 6d 6f entation that mo
29080 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 74 63 68 st closely match
29090 65 73 20 74 68 65 20 77 61 79 20 69 6e 20 77 68 es the way in wh
290a0 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 4c 20 66 ich the.** SQL f
290b0 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 2e unction is used.
290c0 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 A function imp
290d0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 68 lementation with
290e0 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 0a a non-negative.
290f0 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d 65 74 65 ** nArg paramete
29100 72 20 69 73 20 61 20 62 65 74 74 65 72 20 6d 61 r is a better ma
29110 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 tch than a funct
29120 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ion implementati
29130 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 6e 65 67 on with.** a neg
29140 61 74 69 76 65 20 6e 41 72 67 2e 20 20 41 20 66 ative nArg. A f
29150 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 unction where th
29160 65 20 70 72 65 66 65 72 72 65 64 20 74 65 78 74 e preferred text
29170 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 6d 61 74 encoding.** mat
29180 63 68 65 73 20 74 68 65 20 64 61 74 61 62 61 73 ches the databas
29190 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 20 e encoding is a
291a0 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74 63 68 20 better.** match
291b0 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 than a function
291c0 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f 64 69 where the encodi
291d0 6e 67 20 69 73 20 64 69 66 66 65 72 65 6e 74 2e ng is different.
291e0 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74 69 6f 6e .** A function
291f0 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f 64 where the encod
29200 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65 20 69 ing difference i
29210 73 20 62 65 74 77 65 65 6e 20 55 54 46 31 36 6c s between UTF16l
29220 65 20 61 6e 64 20 55 54 46 31 36 62 65 0a 2a 2a e and UTF16be.**
29230 20 69 73 20 61 20 63 6c 6f 73 65 72 20 6d 61 74 is a closer mat
29240 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 ch than a functi
29250 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 on where the enc
29260 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65 oding difference
29270 20 69 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20 55 is.** between U
29280 54 46 38 20 61 6e 64 20 55 54 46 31 36 2e 0a 2a TF8 and UTF16..*
29290 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e 20 66 75 *.** Built-in fu
292a0 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 6f nctions may be o
292b0 76 65 72 6c 6f 61 64 65 64 20 62 79 20 6e 65 77 verloaded by new
292c0 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 application-def
292d0 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 2e 0a ined functions..
292e0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 70 70 ** The first app
292f0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
29300 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 function with a
29310 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f 76 65 72 given name over
29320 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20 62 75 69 rides all.** bui
29330 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 lt-in functions
29340 69 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74 in the same [dat
29350 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
29360 5d 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 ] with the same
29370 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73 65 71 75 name..** Subsequ
29380 65 6e 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d ent application-
29390 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
293a0 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61 s of the same na
293b0 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72 69 64 65 me only override
293c0 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70 70 6c 69 .** prior appli
293d0 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 cation-defined f
293e0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 61 72 unctions that ar
293f0 65 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68 e an exact match
29400 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 for the.** numb
29410 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 er of parameters
29420 20 61 6e 64 20 70 72 65 66 65 72 72 65 64 20 65 and preferred e
29430 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 41 ncoding..**.** A
29440 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 n application-de
29450 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 fined function i
29460 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 63 s permitted to c
29470 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 53 51 4c all other.** SQL
29480 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 2e 20 ite interfaces.
29490 20 48 6f 77 65 76 65 72 2c 20 73 75 63 68 20 63 However, such c
294a0 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74 0a 2a 2a alls must not.**
294b0 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 61 62 close the datab
294c0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6e ase connection n
294d0 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f 72 20 72 or finalize or r
294e0 65 73 65 74 20 74 68 65 20 70 72 65 70 61 72 65 eset the prepare
294f0 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 d.** statement i
29500 6e 20 77 68 69 63 68 20 74 68 65 20 66 75 6e 63 n which the func
29510 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e tion is running.
29520 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
29530 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 31 30 33 5d nts:.** [H16103]
29540 20 5b 48 31 36 31 30 36 5d 20 5b 48 31 36 31 30 [H16106] [H1610
29550 39 5d 20 5b 48 31 36 31 31 32 5d 20 5b 48 31 36 9] [H16112] [H16
29560 31 31 38 5d 20 5b 48 31 36 31 32 31 5d 20 5b 48 118] [H16121] [H
29570 31 36 31 32 37 5d 0a 2a 2a 20 5b 48 31 36 31 33 16127].** [H1613
29580 30 5d 20 5b 48 31 36 31 33 33 5d 20 5b 48 31 36 0] [H16133] [H16
29590 31 33 36 5d 20 5b 48 31 36 31 33 39 5d 20 5b 48 136] [H16139] [H
295a0 31 36 31 34 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 16142].*/.SQLITE
295b0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
295c0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
295d0 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
295e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
295f0 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 FunctionName,.
29600 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 int nArg,. int
29610 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 eTextRep,. void
29620 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 28 *pApp,. void (
29630 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f *xFunc)(sqlite3_
29640 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c context*,int,sql
29650 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 ite3_value**),.
29660 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 void (*xStep)(s
29670 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
29680 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
29690 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 e**),. void (*x
296a0 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 Final)(sqlite3_c
296b0 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 53 51 4c 49 ontext*).);.SQLI
296c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
296d0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
296e0 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 on16(. sqlite3
296f0 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 *db,. const voi
29700 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 d *zFunctionName
29710 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 ,. int nArg,.
29720 69 6e 74 20 65 54 65 78 74 52 65 70 2c 0a 20 20 int eTextRep,.
29730 76 6f 69 64 20 2a 70 41 70 70 2c 0a 20 20 76 6f void *pApp,. vo
29740 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 id (*xFunc)(sqli
29750 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 te3_context*,int
29760 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a ,sqlite3_value**
29770 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 ),. void (*xSte
29780 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 p)(sqlite3_conte
29790 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f xt*,int,sqlite3_
297a0 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 value**),. void
297b0 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 (*xFinal)(sqlit
297c0 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a e3_context*).);.
297d0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
297e0 20 54 65 78 74 20 45 6e 63 6f 64 69 6e 67 73 20 Text Encodings
297f0 7b 48 31 30 32 36 37 7d 20 3c 53 35 30 32 30 30 {H10267} <S50200
29800 3e 20 3c 48 31 36 31 30 30 3e 0a 2a 2a 0a 2a 2a > <H16100>.**.**
29810 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 20 These constant
29820 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72 20 63 define integer c
29830 6f 64 65 73 20 74 68 61 74 20 72 65 70 72 65 73 odes that repres
29840 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75 73 0a ent the various.
29850 2a 2a 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 ** text encoding
29860 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 20 53 s supported by S
29870 51 4c 69 74 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e QLite..*/.#defin
29880 65 20 53 51 4c 49 54 45 5f 55 54 46 38 20 20 20 e SQLITE_UTF8
29890 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 1.#defin
298a0 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 e SQLITE_UTF16LE
298b0 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 2.#defin
298c0 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 e SQLITE_UTF16BE
298d0 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 3.#defin
298e0 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 20 e SQLITE_UTF16
298f0 20 20 20 20 20 20 20 20 34 20 20 20 20 2f 2a 20 4 /*
29900 55 73 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 Use native byte
29910 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 order */.#define
29920 20 53 51 4c 49 54 45 5f 41 4e 59 20 20 20 20 20 SQLITE_ANY
29930 20 20 20 20 20 20 20 35 20 20 20 20 2f 2a 20 73 5 /* s
29940 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
29950 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 23 nction only */.#
29960 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 define SQLITE_UT
29970 46 31 36 5f 41 4c 49 47 4e 45 44 20 20 38 20 20 F16_ALIGNED 8
29980 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 72 65 /* sqlite3_cre
29990 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20 6f 6e ate_collation on
299a0 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ly */../*.** CAP
299b0 49 33 52 45 46 3a 20 44 65 70 72 65 63 61 74 65 I3REF: Deprecate
299c0 64 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 44 d Functions.** D
299d0 45 50 52 45 43 41 54 45 44 0a 2a 2a 0a 2a 2a 20 EPRECATED.**.**
299e0 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 These functions
299f0 61 72 65 20 5b 64 65 70 72 65 63 61 74 65 64 5d are [deprecated]
29a00 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 6d . In order to m
29a10 61 69 6e 74 61 69 6e 0a 2a 2a 20 62 61 63 6b 77 aintain.** backw
29a20 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 ards compatibili
29a30 74 79 20 77 69 74 68 20 6f 6c 64 65 72 20 63 6f ty with older co
29a40 64 65 2c 20 74 68 65 73 65 20 66 75 6e 63 74 69 de, these functi
29a50 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 0a 2a 2a ons continue .**
29a60 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 65 64 to be supported
29a70 2e 20 20 48 6f 77 65 76 65 72 2c 20 6e 65 77 20 . However, new
29a80 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f applications sho
29a90 75 6c 64 20 61 76 6f 69 64 0a 2a 2a 20 74 68 65 uld avoid.** the
29aa0 20 75 73 65 20 6f 66 20 74 68 65 73 65 20 66 75 use of these fu
29ab0 6e 63 74 69 6f 6e 73 2e 20 20 54 6f 20 68 65 6c nctions. To hel
29ac0 70 20 65 6e 63 6f 75 72 61 67 65 20 70 65 6f 70 p encourage peop
29ad0 6c 65 20 74 6f 20 61 76 6f 69 64 0a 2a 2a 20 75 le to avoid.** u
29ae0 73 69 6e 67 20 74 68 65 73 65 20 66 75 6e 63 74 sing these funct
29af0 69 6f 6e 73 2c 20 77 65 20 61 72 65 20 6e 6f 74 ions, we are not
29b00 20 67 6f 69 6e 67 20 74 6f 20 74 65 6c 6c 20 79 going to tell y
29b10 6f 75 20 77 68 61 74 20 74 68 65 79 20 64 6f 2e ou what they do.
29b20 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
29b30 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 TE_OMIT_DEPRECAT
29b40 45 44 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 ED.SQLITE_API SQ
29b50 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 LITE_DEPRECATED
29b60 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 67 72 int sqlite3_aggr
29b70 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c 69 egate_count(sqli
29b80 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 te3_context*);.S
29b90 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 QLITE_API SQLITE
29ba0 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 20 _DEPRECATED int
29bb0 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64 28 sqlite3_expired(
29bc0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a sqlite3_stmt*);.
29bd0 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 SQLITE_API SQLIT
29be0 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 E_DEPRECATED int
29bf0 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 sqlite3_transfe
29c00 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 r_bindings(sqlit
29c10 65 33 5f 73 74 6d 74 2a 2c 20 73 71 6c 69 74 65 e3_stmt*, sqlite
29c20 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 45 3_stmt*);.SQLITE
29c30 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 _API SQLITE_DEPR
29c40 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 ECATED int sqlit
29c50 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 e3_global_recove
29c60 72 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f r(void);.SQLITE_
29c70 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45 API SQLITE_DEPRE
29c80 43 41 54 45 44 20 76 6f 69 64 20 73 71 6c 69 74 CATED void sqlit
29c90 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 e3_thread_cleanu
29ca0 70 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f p(void);.SQLITE_
29cb0 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45 API SQLITE_DEPRE
29cc0 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 65 CATED int sqlite
29cd0 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 76 3_memory_alarm(v
29ce0 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c oid(*)(void*,sql
29cf0 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 29 2c ite3_int64,int),
29d00 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e void*,sqlite3_in
29d10 74 36 34 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a t64);.#endif../*
29d20 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62 .** CAPI3REF: Ob
29d30 74 61 69 6e 69 6e 67 20 53 51 4c 20 46 75 6e 63 taining SQL Func
29d40 74 69 6f 6e 20 50 61 72 61 6d 65 74 65 72 20 56 tion Parameter V
29d50 61 6c 75 65 73 20 7b 48 31 35 31 30 30 7d 20 3c alues {H15100} <
29d60 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S20200>.**.** Th
29d70 65 20 43 2d 6c 61 6e 67 75 61 67 65 20 69 6d 70 e C-language imp
29d80 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 53 lementation of S
29d90 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 QL functions and
29da0 20 61 67 67 72 65 67 61 74 65 73 20 75 73 65 73 aggregates uses
29db0 0a 2a 2a 20 74 68 69 73 20 73 65 74 20 6f 66 20 .** this set of
29dc0 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e interface routin
29dd0 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 es to access the
29de0 20 70 61 72 61 6d 65 74 65 72 20 76 61 6c 75 65 parameter value
29df0 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 66 75 6e 63 s on.** the func
29e00 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74 tion or aggregat
29e10 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 46 75 e..**.** The xFu
29e20 6e 63 20 28 66 6f 72 20 73 63 61 6c 61 72 20 66 nc (for scalar f
29e30 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20 78 53 74 unctions) or xSt
29e40 65 70 20 28 66 6f 72 20 61 67 67 72 65 67 61 74 ep (for aggregat
29e50 65 73 29 20 70 61 72 61 6d 65 74 65 72 73 0a 2a es) parameters.*
29e60 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72 * to [sqlite3_cr
29e70 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d eate_function()]
29e80 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 and [sqlite3_cr
29e90 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 eate_function16(
29ea0 29 5d 0a 2a 2a 20 64 65 66 69 6e 65 20 63 61 6c )].** define cal
29eb0 6c 62 61 63 6b 73 20 74 68 61 74 20 69 6d 70 6c lbacks that impl
29ec0 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 66 75 ement the SQL fu
29ed0 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 nctions and aggr
29ee0 65 67 61 74 65 73 2e 0a 2a 2a 20 54 68 65 20 34 egates..** The 4
29ef0 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 th parameter to
29f00 74 68 65 73 65 20 63 61 6c 6c 62 61 63 6b 73 20 these callbacks
29f10 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 is an array of p
29f20 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 5b 70 ointers to.** [p
29f30 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 rotected sqlite3
29f40 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 2e _value] objects.
29f50 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 5b There is one [
29f60 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
29f70 62 6a 65 63 74 20 66 6f 72 0a 2a 2a 20 65 61 63 bject for.** eac
29f80 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 h parameter to t
29f90 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e he SQL function.
29fa0 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 These routines
29fb0 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 are used to.**
29fc0 65 78 74 72 61 63 74 20 76 61 6c 75 65 73 20 66 extract values f
29fd0 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 65 33 rom the [sqlite3
29fe0 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 2e _value] objects.
29ff0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 .**.** These rou
2a000 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6e 6c 79 20 tines work only
2a010 77 69 74 68 20 5b 70 72 6f 74 65 63 74 65 64 20 with [protected
2a020 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
2a030 62 6a 65 63 74 73 2e 0a 2a 2a 20 41 6e 79 20 61 bjects..** Any a
2a040 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 ttempt to use th
2a050 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e 20 ese routines on
2a060 61 6e 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 an [unprotected
2a070 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a 2a sqlite3_value].*
2a080 2a 20 6f 62 6a 65 63 74 20 72 65 73 75 6c 74 73 * object results
2a090 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65 in undefined be
2a0a0 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 havior..**.** Th
2a0b0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 ese routines wor
2a0c0 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 k just like the
2a0d0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 5b 63 corresponding [c
2a0e0 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e olumn access fun
2a0f0 63 74 69 6f 6e 73 5d 0a 2a 2a 20 65 78 63 65 70 ctions].** excep
2a100 74 20 74 68 61 74 20 20 74 68 65 73 65 20 72 6f t that these ro
2a110 75 74 69 6e 65 73 20 74 61 6b 65 20 61 20 73 69 utines take a si
2a120 6e 67 6c 65 20 5b 70 72 6f 74 65 63 74 65 64 20 ngle [protected
2a130 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
2a140 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74 65 72 bject.** pointer
2a150 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 5b 73 instead of a [s
2a160 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 20 70 6f qlite3_stmt*] po
2a170 69 6e 74 65 72 20 61 6e 64 20 61 6e 20 69 6e 74 inter and an int
2a180 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 eger column numb
2a190 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 er..**.** The sq
2a1a0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
2a1b0 31 36 28 29 20 69 6e 74 65 72 66 61 63 65 20 65 16() interface e
2a1c0 78 74 72 61 63 74 73 20 61 20 55 54 46 2d 31 36 xtracts a UTF-16
2a1d0 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 74 68 string.** in th
2a1e0 65 20 6e 61 74 69 76 65 20 62 79 74 65 2d 6f 72 e native byte-or
2a1f0 64 65 72 20 6f 66 20 74 68 65 20 68 6f 73 74 20 der of the host
2a200 6d 61 63 68 69 6e 65 2e 20 20 54 68 65 0a 2a 2a machine. The.**
2a210 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
2a220 65 78 74 31 36 62 65 28 29 20 61 6e 64 20 73 71 ext16be() and sq
2a230 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
2a240 31 36 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 16le() interface
2a250 73 0a 2a 2a 20 65 78 74 72 61 63 74 20 55 54 46 s.** extract UTF
2a260 2d 31 36 20 73 74 72 69 6e 67 73 20 61 73 20 62 -16 strings as b
2a270 69 67 2d 65 6e 64 69 61 6e 20 61 6e 64 20 6c 69 ig-endian and li
2a280 74 74 6c 65 2d 65 6e 64 69 61 6e 20 72 65 73 70 ttle-endian resp
2a290 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 ectively..**.**
2a2a0 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 The sqlite3_valu
2a2b0 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 29 e_numeric_type()
2a2c0 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d interface attem
2a2d0 70 74 73 20 74 6f 20 61 70 70 6c 79 0a 2a 2a 20 pts to apply.**
2a2e0 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74 79 numeric affinity
2a2f0 20 74 6f 20 74 68 65 20 76 61 6c 75 65 2e 20 20 to the value.
2a300 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 This means that
2a310 61 6e 20 61 74 74 65 6d 70 74 20 69 73 0a 2a 2a an attempt is.**
2a320 20 6d 61 64 65 20 74 6f 20 63 6f 6e 76 65 72 74 made to convert
2a330 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 61 6e the value to an
2a340 20 69 6e 74 65 67 65 72 20 6f 72 20 66 6c 6f 61 integer or floa
2a350 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 49 66 0a ting point. If.
2a360 2a 2a 20 73 75 63 68 20 61 20 63 6f 6e 76 65 72 ** such a conver
2a370 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 sion is possible
2a380 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 without loss of
2a390 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 69 6e information (in
2a3a0 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c other.** words,
2a3b0 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 if the value is
2a3c0 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 6c a string that l
2a3d0 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d 62 ooks like a numb
2a3e0 65 72 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 er).** then the
2a3f0 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 65 conversion is pe
2a400 72 66 6f 72 6d 65 64 2e 20 20 4f 74 68 65 72 77 rformed. Otherw
2a410 69 73 65 20 6e 6f 20 63 6f 6e 76 65 72 73 69 6f ise no conversio
2a420 6e 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 54 68 65 n occurs..** The
2a430 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 [SQLITE_INTEGER
2a440 20 7c 20 64 61 74 61 74 79 70 65 5d 20 61 66 74 | datatype] aft
2a450 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 er conversion is
2a460 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
2a470 20 50 6c 65 61 73 65 20 70 61 79 20 70 61 72 74 Please pay part
2a480 69 63 75 6c 61 72 20 61 74 74 65 6e 74 69 6f 6e icular attention
2a490 20 74 6f 20 74 68 65 20 66 61 63 74 20 74 68 61 to the fact tha
2a4a0 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 72 65 t the pointer re
2a4b0 74 75 72 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 5b turned.** from [
2a4c0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c sqlite3_value_bl
2a4d0 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f ob()], [sqlite3_
2a4e0 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 20 6f value_text()], o
2a4f0 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 61 r.** [sqlite3_va
2a500 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 20 63 61 lue_text16()] ca
2a510 6e 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64 n be invalidated
2a520 20 62 79 20 61 20 73 75 62 73 65 71 75 65 6e 74 by a subsequent
2a530 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c call to.** [sql
2a540 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
2a550 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 ()], [sqlite3_va
2a560 6c 75 65 5f 62 79 74 65 73 31 36 28 29 5d 2c 20 lue_bytes16()],
2a570 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 [sqlite3_value_t
2a580 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 ext()],.** or [s
2a590 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
2a5a0 74 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 t16()]..**.** Th
2a5b0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 75 73 ese routines mus
2a5c0 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d t be called from
2a5d0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
2a5e0 20 61 73 0a 2a 2a 20 74 68 65 20 53 51 4c 20 66 as.** the SQL f
2a5f0 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 73 75 70 unction that sup
2a600 70 6c 69 65 64 20 74 68 65 20 5b 73 71 6c 69 74 plied the [sqlit
2a610 65 33 5f 76 61 6c 75 65 2a 5d 20 70 61 72 61 6d e3_value*] param
2a620 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 eters..**.** Req
2a630 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
2a640 31 35 31 30 33 5d 20 5b 48 31 35 31 30 36 5d 20 15103] [H15106]
2a650 5b 48 31 35 31 30 39 5d 20 5b 48 31 35 31 31 32 [H15109] [H15112
2a660 5d 20 5b 48 31 35 31 31 35 5d 20 5b 48 31 35 31 ] [H15115] [H151
2a670 31 38 5d 20 5b 48 31 35 31 32 31 5d 20 5b 48 31 18] [H15121] [H1
2a680 35 31 32 34 5d 0a 2a 2a 20 5b 48 31 35 31 32 37 5124].** [H15127
2a690 5d 20 5b 48 31 35 31 33 30 5d 20 5b 48 31 35 31 ] [H15130] [H151
2a6a0 33 33 5d 20 5b 48 31 35 31 33 36 5d 0a 2a 2f 0a 33] [H15136].*/.
2a6b0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
2a6c0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 void *sqlite3_v
2a6d0 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 69 74 65 alue_blob(sqlite
2a6e0 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 3_value*);.SQLIT
2a6f0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
2a700 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73 71 3_value_bytes(sq
2a710 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 lite3_value*);.S
2a720 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
2a730 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
2a740 73 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 s16(sqlite3_valu
2a750 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 e*);.SQLITE_API
2a760 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 76 double sqlite3_v
2a770 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 71 6c 69 alue_double(sqli
2a780 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
2a790 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
2a7a0 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 73 71 te3_value_int(sq
2a7b0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 lite3_value*);.S
2a7c0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
2a7d0 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 3_int64 sqlite3_
2a7e0 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c 69 value_int64(sqli
2a7f0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
2a800 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e ITE_API const un
2a810 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c signed char *sql
2a820 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
2a830 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b sqlite3_value*);
2a840 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
2a850 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
2a860 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71 6c value_text16(sql
2a870 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 ite3_value*);.SQ
2a880 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
2a890 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c oid *sqlite3_val
2a8a0 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69 ue_text16le(sqli
2a8b0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
2a8c0 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
2a8d0 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 id *sqlite3_valu
2a8e0 65 5f 74 65 78 74 31 36 62 65 28 73 71 6c 69 74 e_text16be(sqlit
2a8f0 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 e3_value*);.SQLI
2a900 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
2a910 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 73 71 e3_value_type(sq
2a920 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 lite3_value*);.S
2a930 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
2a940 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 lite3_value_nume
2a950 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33 ric_type(sqlite3
2a960 5f 76 61 6c 75 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a _value*);../*.**
2a970 20 43 41 50 49 33 52 45 46 3a 20 4f 62 74 61 69 CAPI3REF: Obtai
2a980 6e 20 41 67 67 72 65 67 61 74 65 20 46 75 6e 63 n Aggregate Func
2a990 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 7b 48 31 tion Context {H1
2a9a0 36 32 31 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 6210} <S20200>.*
2a9b0 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 *.** The impleme
2a9c0 6e 74 61 74 69 6f 6e 20 6f 66 20 61 67 67 72 65 ntation of aggre
2a9d0 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f gate SQL functio
2a9e0 6e 73 20 75 73 65 20 74 68 69 73 20 72 6f 75 74 ns use this rout
2a9f0 69 6e 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 0a ine to allocate.
2aa00 2a 2a 20 61 20 73 74 72 75 63 74 75 72 65 20 66 ** a structure f
2aa10 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 69 72 or storing their
2aa20 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 state..**.** Th
2aa30 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 65 e first time the
2aa40 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 sqlite3_aggrega
2aa50 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 72 6f 75 te_context() rou
2aa60 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 tine is called f
2aa70 6f 72 20 61 0a 2a 2a 20 70 61 72 74 69 63 75 6c or a.** particul
2aa80 61 72 20 61 67 67 72 65 67 61 74 65 2c 20 53 51 ar aggregate, SQ
2aa90 4c 69 74 65 20 61 6c 6c 6f 63 61 74 65 73 20 6e Lite allocates n
2aaa0 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2c Bytes of memory,
2aab0 20 7a 65 72 6f 65 73 20 6f 75 74 20 74 68 61 74 zeroes out that
2aac0 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 61 6e 64 20 .** memory, and
2aad0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
2aae0 72 20 74 6f 20 69 74 2e 20 4f 6e 20 73 65 63 6f r to it. On seco
2aaf0 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e nd and subsequen
2ab00 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 t calls to.** sq
2ab10 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f lite3_aggregate_
2ab20 63 6f 6e 74 65 78 74 28 29 20 66 6f 72 20 74 68 context() for th
2ab30 65 20 73 61 6d 65 20 61 67 67 72 65 67 61 74 65 e same aggregate
2ab40 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64 65 78 2c function index,
2ab50 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 62 75 66 .** the same buf
2ab60 66 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e fer is returned.
2ab70 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 The implementat
2ab80 69 6f 6e 20 6f 66 20 74 68 65 20 61 67 67 72 65 ion of the aggre
2ab90 67 61 74 65 20 63 61 6e 20 75 73 65 0a 2a 2a 20 gate can use.**
2aba0 74 68 65 20 72 65 74 75 72 6e 65 64 20 62 75 66 the returned buf
2abb0 66 65 72 20 74 6f 20 61 63 63 75 6d 75 6c 61 74 fer to accumulat
2abc0 65 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 51 e data..**.** SQ
2abd0 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 61 6c Lite automatical
2abe0 6c 79 20 66 72 65 65 73 20 74 68 65 20 61 6c 6c ly frees the all
2abf0 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 77 68 ocated buffer wh
2ac00 65 6e 20 74 68 65 20 61 67 67 72 65 67 61 74 65 en the aggregate
2ac10 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6e 63 6c 75 .** query conclu
2ac20 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 des..**.** The f
2ac30 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 73 irst parameter s
2ac40 68 6f 75 6c 64 20 62 65 20 61 20 63 6f 70 79 20 hould be a copy
2ac50 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 of the.** [sqlit
2ac60 65 33 5f 63 6f 6e 74 65 78 74 20 7c 20 53 51 4c e3_context | SQL
2ac70 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 function contex
2ac80 74 5d 20 74 68 61 74 20 69 73 20 74 68 65 20 66 t] that is the f
2ac90 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 0a 2a irst parameter.*
2aca0 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 * to the callbac
2acb0 6b 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 69 k routine that i
2acc0 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 61 67 mplements the ag
2acd0 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
2ace0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
2acf0 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c tine must be cal
2ad00 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d led from the sam
2ad10 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 e thread in whic
2ad20 68 0a 2a 2a 20 74 68 65 20 61 67 67 72 65 67 61 h.** the aggrega
2ad30 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 te SQL function
2ad40 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a is running..**.*
2ad50 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
2ad60 2a 2a 20 5b 48 31 36 32 31 31 5d 20 5b 48 31 36 ** [H16211] [H16
2ad70 32 31 33 5d 20 5b 48 31 36 32 31 35 5d 20 5b 48 213] [H16215] [H
2ad80 31 36 32 31 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 16217].*/.SQLITE
2ad90 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 _API void *sqlit
2ada0 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e e3_aggregate_con
2adb0 74 65 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e text(sqlite3_con
2adc0 74 65 78 74 2a 2c 20 69 6e 74 20 6e 42 79 74 65 text*, int nByte
2add0 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 s);../*.** CAPI3
2ade0 52 45 46 3a 20 55 73 65 72 20 44 61 74 61 20 46 REF: User Data F
2adf0 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 or Functions {H1
2ae00 36 32 34 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 6240} <S20200>.*
2ae10 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
2ae20 5f 75 73 65 72 5f 64 61 74 61 28 29 20 69 6e 74 _user_data() int
2ae30 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 erface returns a
2ae40 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 copy of.** the
2ae50 70 6f 69 6e 74 65 72 20 74 68 61 74 20 77 61 73 pointer that was
2ae60 20 74 68 65 20 70 55 73 65 72 44 61 74 61 20 70 the pUserData p
2ae70 61 72 61 6d 65 74 65 72 20 28 74 68 65 20 35 74 arameter (the 5t
2ae80 68 20 70 61 72 61 6d 65 74 65 72 29 0a 2a 2a 20 h parameter).**
2ae90 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f of the [sqlite3_
2aea0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 create_function(
2aeb0 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 )].** and [sqlit
2aec0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
2aed0 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 73 on16()] routines
2aee0 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c 6c 79 that originally
2aef0 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 20 74 .** registered t
2af00 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 he application d
2af10 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e efined function.
2af20 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 {END}.**.** Thi
2af30 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 s routine must b
2af40 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 e called from th
2af50 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e e same thread in
2af60 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 61 70 which.** the ap
2af70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
2af80 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 d function is ru
2af90 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 nning..**.** Req
2afa0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
2afb0 31 36 32 34 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 16243].*/.SQLITE
2afc0 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 _API void *sqlit
2afd0 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 6c e3_user_data(sql
2afe0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a ite3_context*);.
2aff0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
2b000 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 Database Connec
2b010 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74 69 6f tion For Functio
2b020 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53 36 30 ns {H16250} <S60
2b030 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 600><S20200>.**.
2b040 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 ** The sqlite3_c
2b050 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 ontext_db_handle
2b060 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 () interface ret
2b070 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a urns a copy of.*
2b080 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f * the pointer to
2b090 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
2b0a0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68 65 20 onnection] (the
2b0b0 31 73 74 20 70 61 72 61 6d 65 74 65 72 29 0a 2a 1st parameter).*
2b0c0 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 * of the [sqlite
2b0d0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 3_create_functio
2b0e0 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c n()].** and [sql
2b0f0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 ite3_create_func
2b100 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e tion16()] routin
2b110 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c es that original
2b120 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 ly.** registered
2b130 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
2b140 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f defined functio
2b150 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 n..**.** Require
2b160 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 35 ments:.** [H1625
2b170 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 3].*/.SQLITE_API
2b180 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 sqlite3 *sqlite
2b190 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
2b1a0 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 dle(sqlite3_cont
2b1b0 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ext*);../*.** CA
2b1c0 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69 6f 6e PI3REF: Function
2b1d0 20 41 75 78 69 6c 69 61 72 79 20 44 61 74 61 20 Auxiliary Data
2b1e0 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 32 30 30 {H16270} <S20200
2b1f0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c >.**.** The foll
2b200 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63 74 69 owing two functi
2b210 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65 64 20 ons may be used
2b220 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20 66 75 by scalar SQL fu
2b230 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 61 73 nctions to.** as
2b240 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61 74 61 sociate metadata
2b250 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 20 76 with argument v
2b260 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20 73 61 alues. If the sa
2b270 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61 73 73 me value is pass
2b280 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69 70 6c ed to.** multipl
2b290 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66 e invocations of
2b2a0 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20 66 75 the same SQL fu
2b2b0 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20 71 75 nction during qu
2b2c0 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c 20 75 ery execution, u
2b2d0 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63 69 72 nder.** some cir
2b2e0 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65 20 61 cumstances the a
2b2f0 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61 64 61 ssociated metada
2b300 74 61 20 6d 61 79 20 62 65 20 70 72 65 73 65 72 ta may be preser
2b310 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a 2a 2a ved. This may.**
2b320 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20 65 78 be used, for ex
2b330 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20 61 20 ample, to add a
2b340 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73 73 69 regular-expressi
2b350 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63 61 6c on matching scal
2b360 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 ar.** function.
2b370 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76 65 72 The compiled ver
2b380 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65 67 75 sion of the regu
2b390 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 lar expression i
2b3a0 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 6d s stored as.** m
2b3b0 65 74 61 64 61 74 61 20 61 73 73 6f 63 69 61 74 etadata associat
2b3c0 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 20 ed with the SQL
2b3d0 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 value passed as
2b3e0 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 the regular expr
2b3f0 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74 65 72 ession.** patter
2b400 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 n. The compiled
2b410 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 regular express
2b420 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75 73 65 ion can be reuse
2b430 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a d on multiple.**
2b440 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66 20 invocations of
2b450 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f the same functio
2b460 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20 6f 72 n so that the or
2b470 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e 20 73 iginal pattern s
2b480 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20 6e 6f tring.** does no
2b490 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65 63 t need to be rec
2b4a0 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63 68 20 ompiled on each
2b4b0 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a invocation..**.*
2b4c0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 * The sqlite3_ge
2b4d0 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e 74 65 t_auxdata() inte
2b4e0 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 rface returns a
2b4f0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d pointer to the m
2b500 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73 6f 63 etadata.** assoc
2b510 69 61 74 65 64 20 62 79 20 74 68 65 20 73 71 6c iated by the sql
2b520 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 ite3_set_auxdata
2b530 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 () function with
2b540 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e the Nth argumen
2b550 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20 74 68 t.** value to th
2b560 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 e application-de
2b570 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e 20 fined function.
2b580 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61 20 68 If no metadata h
2b590 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a 2a 20 as been ever.**
2b5a0 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74 68 65 been set for the
2b5b0 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20 6f 66 Nth argument of
2b5c0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c 20 6f the function, o
2b5d0 72 20 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 r if the corresp
2b5e0 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 onding.** functi
2b5f0 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68 61 73 on parameter has
2b600 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20 74 changed since t
2b610 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77 61 73 he meta-data was
2b620 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 73 71 set,.** then sq
2b630 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 lite3_get_auxdat
2b640 61 28 29 20 72 65 74 75 72 6e 73 20 61 20 4e 55 a() returns a NU
2b650 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a LL pointer..**.*
2b660 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 65 * The sqlite3_se
2b670 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e 74 65 t_auxdata() inte
2b680 72 66 61 63 65 20 73 61 76 65 73 20 74 68 65 20 rface saves the
2b690 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 6f 69 6e metadata.** poin
2b6a0 74 65 64 20 74 6f 20 62 79 20 69 74 73 20 33 72 ted to by its 3r
2b6b0 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 20 74 d parameter as t
2b6c0 68 65 20 6d 65 74 61 64 61 74 61 20 66 6f 72 20 he metadata for
2b6d0 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 72 67 75 the N-th.** argu
2b6e0 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 70 70 6c ment of the appl
2b6f0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
2b700 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 62 73 65 function. Subse
2b710 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 quent.** calls t
2b720 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 o sqlite3_get_au
2b730 78 64 61 74 61 28 29 20 6d 69 67 68 74 20 72 65 xdata() might re
2b740 74 75 72 6e 20 74 68 69 73 20 64 61 74 61 2c 20 turn this data,
2b750 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 6e 6f 74 if it has.** not
2b760 20 62 65 65 6e 20 64 65 73 74 72 6f 79 65 64 2e been destroyed.
2b770 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74 .** If it is not
2b780 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 20 77 69 NULL, SQLite wi
2b790 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 64 65 ll invoke the de
2b7a0 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 75 6e 63 structor.** func
2b7b0 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 20 74 68 tion given by th
2b7c0 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 e 4th parameter
2b7d0 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 to sqlite3_set_a
2b7e0 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a 2a 20 74 uxdata() on.** t
2b7f0 68 65 20 6d 65 74 61 64 61 74 61 20 77 68 65 6e he metadata when
2b800 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
2b810 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 ng function para
2b820 6d 65 74 65 72 20 63 68 61 6e 67 65 73 0a 2a 2a meter changes.**
2b830 20 6f 72 20 77 68 65 6e 20 74 68 65 20 53 51 4c or when the SQL
2b840 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 6c statement compl
2b850 65 74 65 73 2c 20 77 68 69 63 68 65 76 65 72 20 etes, whichever
2b860 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a 2a 2a 0a comes first..**.
2b870 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 66 72 65 ** SQLite is fre
2b880 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 64 65 e to call the de
2b890 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 72 6f structor and dro
2b8a0 70 20 6d 65 74 61 64 61 74 61 20 6f 6e 20 61 6e p metadata on an
2b8b0 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 6f y.** parameter o
2b8c0 66 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e 20 61 f any function a
2b8d0 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 54 68 65 t any time. The
2b8e0 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 65 20 only guarantee
2b8f0 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 64 is that.** the d
2b900 65 73 74 72 75 63 74 6f 72 20 77 69 6c 6c 20 62 estructor will b
2b910 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 e called before
2b920 74 68 65 20 6d 65 74 61 64 61 74 61 20 69 73 20 the metadata is
2b930 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 dropped..**.** I
2b940 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 65 74 61 n practice, meta
2b950 64 61 74 61 20 69 73 20 70 72 65 73 65 72 76 65 data is preserve
2b960 64 20 62 65 74 77 65 65 6e 20 66 75 6e 63 74 69 d between functi
2b970 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a 2a 2a 20 on calls for.**
2b980 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 expressions that
2b990 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 61 74 are constant at
2b9a0 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e 20 54 compile time. T
2b9b0 68 69 73 20 69 6e 63 6c 75 64 65 73 20 6c 69 74 his includes lit
2b9c0 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 73 20 61 eral.** values a
2b9d0 6e 64 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73 nd SQL variables
2b9e0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f ..**.** These ro
2b9f0 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 20 63 utines must be c
2ba00 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 alled from the s
2ba10 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 ame thread in wh
2ba20 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 4c 20 66 ich.** the SQL f
2ba30 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 unction is runni
2ba40 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ng..**.** Requir
2ba50 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 ements:.** [H162
2ba60 37 32 5d 20 5b 48 31 36 32 37 34 5d 20 5b 48 31 72] [H16274] [H1
2ba70 36 32 37 36 5d 20 5b 48 31 36 32 37 37 5d 20 5b 6276] [H16277] [
2ba80 48 31 36 32 37 38 5d 20 5b 48 31 36 32 37 39 5d H16278] [H16279]
2ba90 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
2baa0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 67 65 74 oid *sqlite3_get
2bab0 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 65 33 _auxdata(sqlite3
2bac0 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 4e _context*, int N
2bad0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f );.SQLITE_API vo
2bae0 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 id sqlite3_set_a
2baf0 75 78 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63 uxdata(sqlite3_c
2bb00 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 4e 2c 20 ontext*, int N,
2bb10 76 6f 69 64 2a 2c 20 76 6f 69 64 20 28 2a 29 28 void*, void (*)(
2bb20 76 6f 69 64 2a 29 29 3b 0a 0a 0a 2f 2a 0a 2a 2a void*));.../*.**
2bb30 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 73 74 CAPI3REF: Const
2bb40 61 6e 74 73 20 44 65 66 69 6e 69 6e 67 20 53 70 ants Defining Sp
2bb50 65 63 69 61 6c 20 44 65 73 74 72 75 63 74 6f 72 ecial Destructor
2bb60 20 42 65 68 61 76 69 6f 72 20 7b 48 31 30 32 38 Behavior {H1028
2bb70 30 7d 20 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 0} <S30100>.**.*
2bb80 2a 20 54 68 65 73 65 20 61 72 65 20 73 70 65 63 * These are spec
2bb90 69 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 20 74 ial values for t
2bba0 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 74 68 he destructor th
2bbb0 61 74 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 at is passed in
2bbc0 61 73 20 74 68 65 0a 2a 2a 20 66 69 6e 61 6c 20 as the.** final
2bbd0 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 6f 75 74 argument to rout
2bbe0 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c 69 74 ines like [sqlit
2bbf0 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 29 e3_result_blob()
2bc00 5d 2e 20 20 49 66 20 74 68 65 20 64 65 73 74 72 ]. If the destr
2bc10 75 63 74 6f 72 0a 2a 2a 20 61 72 67 75 6d 65 6e uctor.** argumen
2bc20 74 20 69 73 20 53 51 4c 49 54 45 5f 53 54 41 54 t is SQLITE_STAT
2bc30 49 43 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 IC, it means tha
2bc40 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 70 6f t the content po
2bc50 69 6e 74 65 72 20 69 73 20 63 6f 6e 73 74 61 6e inter is constan
2bc60 74 0a 2a 2a 20 61 6e 64 20 77 69 6c 6c 20 6e 65 t.** and will ne
2bc70 76 65 72 20 63 68 61 6e 67 65 2e 20 20 49 74 20 ver change. It
2bc80 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
2bc90 20 62 65 20 64 65 73 74 72 6f 79 65 64 2e 20 20 be destroyed.
2bca0 54 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 52 The.** SQLITE_TR
2bcb0 41 4e 53 49 45 4e 54 20 76 61 6c 75 65 20 6d 65 ANSIENT value me
2bcc0 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e ans that the con
2bcd0 74 65 6e 74 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 tent will likely
2bce0 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 74 68 change in.** th
2bcf0 65 20 6e 65 61 72 20 66 75 74 75 72 65 20 61 6e e near future an
2bd00 64 20 74 68 61 74 20 53 51 4c 69 74 65 20 73 68 d that SQLite sh
2bd10 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f 77 ould make its ow
2bd20 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f n private copy o
2bd30 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 f.** the content
2bd40 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
2bd50 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 79 70 g..**.** The typ
2bd60 65 64 65 66 20 69 73 20 6e 65 63 65 73 73 61 72 edef is necessar
2bd70 79 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 y to work around
2bd80 20 70 72 6f 62 6c 65 6d 73 20 69 6e 20 63 65 72 problems in cer
2bd90 74 61 69 6e 0a 2a 2a 20 43 2b 2b 20 63 6f 6d 70 tain.** C++ comp
2bda0 69 6c 65 72 73 2e 20 20 53 65 65 20 74 69 63 6b ilers. See tick
2bdb0 65 74 20 23 32 31 39 31 2e 0a 2a 2f 0a 74 79 70 et #2191..*/.typ
2bdc0 65 64 65 66 20 76 6f 69 64 20 28 2a 73 71 6c 69 edef void (*sqli
2bdd0 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 te3_destructor_t
2bde0 79 70 65 29 28 76 6f 69 64 2a 29 3b 0a 23 64 65 ype)(void*);.#de
2bdf0 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 fine SQLITE_STAT
2be00 49 43 20 20 20 20 20 20 28 28 73 71 6c 69 74 65 IC ((sqlite
2be10 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 3_destructor_typ
2be20 65 29 30 29 0a 23 64 65 66 69 6e 65 20 53 51 4c e)0).#define SQL
2be30 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 20 20 ITE_TRANSIENT
2be40 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 ((sqlite3_destru
2be50 63 74 6f 72 5f 74 79 70 65 29 2d 31 29 0a 0a 2f ctor_type)-1)../
2be60 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 *.** CAPI3REF: S
2be70 65 74 74 69 6e 67 20 54 68 65 20 52 65 73 75 6c etting The Resul
2be80 74 20 4f 66 20 41 6e 20 53 51 4c 20 46 75 6e 63 t Of An SQL Func
2be90 74 69 6f 6e 20 7b 48 31 36 34 30 30 7d 20 3c 53 tion {H16400} <S
2bea0 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20200>.**.** The
2beb0 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 se routines are
2bec0 75 73 65 64 20 62 79 20 74 68 65 20 78 46 75 6e used by the xFun
2bed0 63 20 6f 72 20 78 46 69 6e 61 6c 20 63 61 6c 6c c or xFinal call
2bee0 62 61 63 6b 73 20 74 68 61 74 0a 2a 2a 20 69 6d backs that.** im
2bef0 70 6c 65 6d 65 6e 74 20 53 51 4c 20 66 75 6e 63 plement SQL func
2bf00 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 65 67 tions and aggreg
2bf10 61 74 65 73 2e 20 20 53 65 65 0a 2a 2a 20 5b 73 ates. See.** [s
2bf20 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
2bf30 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 nction()] and [s
2bf40 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
2bf50 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a 20 66 nction16()].** f
2bf60 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e or additional in
2bf70 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a formation..**.**
2bf80 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 These functions
2bf90 20 77 6f 72 6b 20 76 65 72 79 20 6d 75 63 68 20 work very much
2bfa0 6c 69 6b 65 20 74 68 65 20 5b 70 61 72 61 6d 65 like the [parame
2bfb0 74 65 72 20 62 69 6e 64 69 6e 67 5d 20 66 61 6d ter binding] fam
2bfc0 69 6c 79 20 6f 66 0a 2a 2a 20 66 75 6e 63 74 69 ily of.** functi
2bfd0 6f 6e 73 20 75 73 65 64 20 74 6f 20 62 69 6e 64 ons used to bind
2bfe0 20 76 61 6c 75 65 73 20 74 6f 20 68 6f 73 74 20 values to host
2bff0 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 70 72 parameters in pr
2c000 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
2c010 73 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 74 s..** Refer to t
2c020 68 65 20 5b 53 51 4c 20 70 61 72 61 6d 65 74 65 he [SQL paramete
2c030 72 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e r] documentation
2c040 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
2c050 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a information..**.
2c060 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 ** The sqlite3_r
2c070 65 73 75 6c 74 5f 62 6c 6f 62 28 29 20 69 6e 74 esult_blob() int
2c080 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20 erface sets the
2c090 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61 result from.** a
2c0a0 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 n application-de
2c0b0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 fined function t
2c0c0 6f 20 62 65 20 74 68 65 20 42 4c 4f 42 20 77 68 o be the BLOB wh
2c0d0 6f 73 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 70 ose content is p
2c0e0 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 ointed.** to by
2c0f0 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
2c100 65 74 65 72 20 61 6e 64 20 77 68 69 63 68 20 69 eter and which i
2c110 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67 20 77 s N bytes long w
2c120 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a here N is the.**
2c130 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 third parameter
2c140 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
2c150 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 te3_result_zerob
2c160 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63 65 73 lob() interfaces
2c170 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 set the result
2c180 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 of.** the applic
2c190 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 ation-defined fu
2c1a0 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 42 nction to be a B
2c1b0 4c 4f 42 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 LOB containing a
2c1c0 6c 6c 20 7a 65 72 6f 0a 2a 2a 20 62 79 74 65 73 ll zero.** bytes
2c1d0 20 61 6e 64 20 4e 20 62 79 74 65 73 20 69 6e 20 and N bytes in
2c1e0 73 69 7a 65 2c 20 77 68 65 72 65 20 4e 20 69 73 size, where N is
2c1f0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
2c200 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e e 2nd parameter.
2c210 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
2c220 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 e3_result_double
2c230 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 () interface set
2c240 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 72 6f s the result fro
2c250 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61 74 m.** an applicat
2c260 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ion-defined func
2c270 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 66 6c 6f tion to be a flo
2c280 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 ating point valu
2c290 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 e specified.** b
2c2a0 79 20 69 74 73 20 32 6e 64 20 61 72 67 75 6d 65 y its 2nd argume
2c2b0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 nt..**.** The sq
2c2c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
2c2d0 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 or() and sqlite3
2c2e0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 _result_error16(
2c2f0 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 63 ) functions.** c
2c300 61 75 73 65 20 74 68 65 20 69 6d 70 6c 65 6d 65 ause the impleme
2c310 6e 74 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f nted SQL functio
2c320 6e 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 78 n to throw an ex
2c330 63 65 70 74 69 6f 6e 2e 0a 2a 2a 20 53 51 4c 69 ception..** SQLi
2c340 74 65 20 75 73 65 73 20 74 68 65 20 73 74 72 69 te uses the stri
2c350 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ng pointed to by
2c360 20 74 68 65 0a 2a 2a 20 32 6e 64 20 70 61 72 61 the.** 2nd para
2c370 6d 65 74 65 72 20 6f 66 20 73 71 6c 69 74 65 33 meter of sqlite3
2c380 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 _result_error()
2c390 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c or sqlite3_resul
2c3a0 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a 20 61 t_error16().** a
2c3b0 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 6e s the text of an
2c3c0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 20 error message.
2c3d0 20 53 51 4c 69 74 65 20 69 6e 74 65 72 70 72 65 SQLite interpre
2c3e0 74 73 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 ts the error.**
2c3f0 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 66 message string f
2c400 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 73 75 rom sqlite3_resu
2c410 6c 74 5f 65 72 72 6f 72 28 29 20 61 73 20 55 54 lt_error() as UT
2c420 46 2d 38 2e 20 53 51 4c 69 74 65 0a 2a 2a 20 69 F-8. SQLite.** i
2c430 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 73 74 nterprets the st
2c440 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74 65 ring from sqlite
2c450 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 3_result_error16
2c460 28 29 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20 () as UTF-16 in
2c470 6e 61 74 69 76 65 0a 2a 2a 20 62 79 74 65 20 6f native.** byte o
2c480 72 64 65 72 2e 20 20 49 66 20 74 68 65 20 74 68 rder. If the th
2c490 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f ird parameter to
2c4a0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2c4b0 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72 20 73 71 error().** or sq
2c4c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
2c4d0 6f 72 31 36 28 29 20 69 73 20 6e 65 67 61 74 69 or16() is negati
2c4e0 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74 ve then SQLite t
2c4f0 61 6b 65 73 20 61 73 20 74 68 65 20 65 72 72 6f akes as the erro
2c500 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 61 6c 6c r.** message all
2c510 20 74 65 78 74 20 75 70 20 74 68 72 6f 75 67 68 text up through
2c520 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 the first zero
2c530 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 character..** If
2c540 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d the third param
2c550 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f eter to sqlite3_
2c560 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 6f result_error() o
2c570 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 r.** sqlite3_res
2c580 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69 73 ult_error16() is
2c590 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 74 68 non-negative th
2c5a0 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 en SQLite takes
2c5b0 74 68 61 74 20 6d 61 6e 79 0a 2a 2a 20 62 79 74 that many.** byt
2c5c0 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63 74 65 es (not characte
2c5d0 72 73 29 20 66 72 6f 6d 20 74 68 65 20 32 6e 64 rs) from the 2nd
2c5e0 20 70 61 72 61 6d 65 74 65 72 20 61 73 20 74 68 parameter as th
2c5f0 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e e error message.
2c600 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2c610 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 result_error() a
2c620 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c nd sqlite3_resul
2c630 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a 20 72 t_error16().** r
2c640 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 61 20 70 outines make a p
2c650 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20 74 rivate copy of t
2c660 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
2c670 20 74 65 78 74 20 62 65 66 6f 72 65 0a 2a 2a 20 text before.**
2c680 74 68 65 79 20 72 65 74 75 72 6e 2e 20 20 48 65 they return. He
2c690 6e 63 65 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 nce, the calling
2c6a0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 64 65 function can de
2c6b0 61 6c 6c 6f 63 61 74 65 20 6f 72 0a 2a 2a 20 6d allocate or.** m
2c6c0 6f 64 69 66 79 20 74 68 65 20 74 65 78 74 20 61 odify the text a
2c6d0 66 74 65 72 20 74 68 65 79 20 72 65 74 75 72 6e fter they return
2c6e0 20 77 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a 2a without harm..*
2c6f0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 * The sqlite3_re
2c700 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 sult_error_code(
2c710 29 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 6e 67 ) function chang
2c720 65 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 es the error cod
2c730 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62 79 e.** returned by
2c740 20 53 51 4c 69 74 65 20 61 73 20 61 20 72 65 73 SQLite as a res
2c750 75 6c 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 ult of an error
2c760 69 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 in a function.
2c770 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a 20 74 By default,.** t
2c780 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 he error code is
2c790 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 SQLITE_ERROR.
2c7a0 41 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c A subsequent cal
2c7b0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 l to sqlite3_res
2c7c0 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 6f ult_error().** o
2c7d0 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 r sqlite3_result
2c7e0 5f 65 72 72 6f 72 31 36 28 29 20 72 65 73 65 74 _error16() reset
2c7f0 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 s the error code
2c800 20 74 6f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 to SQLITE_ERROR
2c810 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
2c820 74 65 33 5f 72 65 73 75 6c 74 5f 74 6f 6f 62 69 te3_result_toobi
2c830 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 g() interface ca
2c840 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f 20 74 uses SQLite to t
2c850 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a 2a hrow an error.**
2c860 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 indicating that
2c870 20 61 20 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f a string or BLO
2c880 42 20 69 73 20 74 6f 20 6c 6f 6e 67 20 74 6f 20 B is to long to
2c890 72 65 70 72 65 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a represent..**.**
2c8a0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 The sqlite3_res
2c8b0 75 6c 74 5f 6e 6f 6d 65 6d 28 29 20 69 6e 74 65 ult_nomem() inte
2c8c0 72 66 61 63 65 20 63 61 75 73 65 73 20 53 51 4c rface causes SQL
2c8d0 69 74 65 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 ite to throw an
2c8e0 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61 74 error.** indicat
2c8f0 69 6e 67 20 74 68 61 74 20 61 20 6d 65 6d 6f 72 ing that a memor
2c900 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 y allocation fai
2c910 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 led..**.** The s
2c920 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
2c930 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 t() interface se
2c940 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 ts the return va
2c950 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70 lue.** of the ap
2c960 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
2c970 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 d function to be
2c980 20 74 68 65 20 33 32 2d 62 69 74 20 73 69 67 6e the 32-bit sign
2c990 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 61 ed integer.** va
2c9a0 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 lue given in the
2c9b0 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2nd argument..*
2c9c0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 * The sqlite3_re
2c9d0 73 75 6c 74 5f 69 6e 74 36 34 28 29 20 69 6e 74 sult_int64() int
2c9e0 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20 erface sets the
2c9f0 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 return value.**
2ca00 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 of the applicati
2ca10 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 on-defined funct
2ca20 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 36 34 ion to be the 64
2ca30 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 -bit signed inte
2ca40 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76 ger.** value giv
2ca50 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 20 61 72 en in the 2nd ar
2ca60 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 gument..**.** Th
2ca70 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
2ca80 5f 6e 75 6c 6c 28 29 20 69 6e 74 65 72 66 61 63 _null() interfac
2ca90 65 20 73 65 74 73 20 74 68 65 20 72 65 74 75 72 e sets the retur
2caa0 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 n value.** of th
2cab0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 e application-de
2cac0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 fined function t
2cad0 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a o be NULL..**.**
2cae0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 The sqlite3_res
2caf0 75 6c 74 5f 74 65 78 74 28 29 2c 20 73 71 6c 69 ult_text(), sqli
2cb00 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 te3_result_text1
2cb10 36 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6(),.** sqlite3_
2cb20 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 28 result_text16le(
2cb30 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 ), and sqlite3_r
2cb40 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 29 esult_text16be()
2cb50 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 73 interfaces.** s
2cb60 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 et the return va
2cb70 6c 75 65 20 6f 66 20 74 68 65 20 61 70 70 6c 69 lue of the appli
2cb80 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 cation-defined f
2cb90 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 0a 2a 2a unction to be.**
2cba0 20 61 20 74 65 78 74 20 73 74 72 69 6e 67 20 77 a text string w
2cbb0 68 69 63 68 20 69 73 20 72 65 70 72 65 73 65 6e hich is represen
2cbc0 74 65 64 20 61 73 20 55 54 46 2d 38 2c 20 55 54 ted as UTF-8, UT
2cbd0 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 65 F-16 native byte
2cbe0 20 6f 72 64 65 72 2c 0a 2a 2a 20 55 54 46 2d 31 order,.** UTF-1
2cbf0 36 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c 6 little endian,
2cc00 20 6f 72 20 55 54 46 2d 31 36 20 62 69 67 20 65 or UTF-16 big e
2cc10 6e 64 69 61 6e 2c 20 72 65 73 70 65 63 74 69 76 ndian, respectiv
2cc20 65 6c 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 74 ely..** SQLite t
2cc30 61 6b 65 73 20 74 68 65 20 74 65 78 74 20 72 65 akes the text re
2cc40 73 75 6c 74 20 66 72 6f 6d 20 74 68 65 20 61 70 sult from the ap
2cc50 70 6c 69 63 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a plication from.*
2cc60 2a 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 * the 2nd parame
2cc70 74 65 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ter of the sqlit
2cc80 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 e3_result_text*
2cc90 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 49 interfaces..** I
2cca0 66 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 f the 3rd parame
2ccb0 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 ter to the sqlit
2ccc0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 e3_result_text*
2ccd0 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69 73 interfaces.** is
2cce0 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 negative, then
2ccf0 53 51 4c 69 74 65 20 74 61 6b 65 73 20 72 65 73 SQLite takes res
2cd00 75 6c 74 20 74 65 78 74 20 66 72 6f 6d 20 74 68 ult text from th
2cd10 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 0a e 2nd parameter.
2cd20 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 66 ** through the f
2cd30 69 72 73 74 20 7a 65 72 6f 20 63 68 61 72 61 63 irst zero charac
2cd40 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 33 ter..** If the 3
2cd50 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 rd parameter to
2cd60 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 the sqlite3_resu
2cd70 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 lt_text* interfa
2cd80 63 65 73 0a 2a 2a 20 69 73 20 6e 6f 6e 2d 6e 65 ces.** is non-ne
2cd90 67 61 74 69 76 65 2c 20 74 68 65 6e 20 61 73 20 gative, then as
2cda0 6d 61 6e 79 20 62 79 74 65 73 20 28 6e 6f 74 20 many bytes (not
2cdb0 63 68 61 72 61 63 74 65 72 73 29 20 6f 66 20 74 characters) of t
2cdc0 68 65 20 74 65 78 74 0a 2a 2a 20 70 6f 69 6e 74 he text.** point
2cdd0 65 64 20 74 6f 20 62 79 20 74 68 65 20 32 6e 64 ed to by the 2nd
2cde0 20 70 61 72 61 6d 65 74 65 72 20 61 72 65 20 74 parameter are t
2cdf0 61 6b 65 6e 20 61 73 20 74 68 65 20 61 70 70 6c aken as the appl
2ce00 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 0a ication-defined.
2ce10 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 75 ** function resu
2ce20 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 34 74 lt..** If the 4t
2ce30 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 h parameter to t
2ce40 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c he sqlite3_resul
2ce50 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 t_text* interfac
2ce60 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 es.** or sqlite3
2ce70 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20 _result_blob is
2ce80 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 a non-NULL point
2ce90 65 72 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 er, then SQLite
2cea0 63 61 6c 6c 73 20 74 68 61 74 0a 2a 2a 20 66 75 calls that.** fu
2ceb0 6e 63 74 69 6f 6e 20 61 73 20 74 68 65 20 64 65 nction as the de
2cec0 73 74 72 75 63 74 6f 72 20 6f 6e 20 74 68 65 20 structor on the
2ced0 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 72 65 73 text or BLOB res
2cee0 75 6c 74 20 77 68 65 6e 20 69 74 20 68 61 73 0a ult when it has.
2cef0 2a 2a 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e ** finished usin
2cf00 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e 0a 2a g that result..*
2cf10 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 61 72 * If the 4th par
2cf20 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 ameter to the sq
2cf30 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2cf40 74 2a 20 69 6e 74 65 72 66 61 63 65 73 20 6f 72 t* interfaces or
2cf50 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 to.** sqlite3_r
2cf60 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20 74 68 esult_blob is th
2cf70 65 20 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 e special consta
2cf80 6e 74 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 nt SQLITE_STATIC
2cf90 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 0a 2a 2a , then SQLite.**
2cfa0 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 assumes that th
2cfb0 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 72 e text or BLOB r
2cfc0 65 73 75 6c 74 20 69 73 20 69 6e 20 63 6f 6e 73 esult is in cons
2cfd0 74 61 6e 74 20 73 70 61 63 65 20 61 6e 64 20 64 tant space and d
2cfe0 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 6f 70 79 20 oes not.** copy
2cff0 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 the content of t
2d000 68 65 20 70 61 72 61 6d 65 74 65 72 20 6e 6f 72 he parameter nor
2d010 20 63 61 6c 6c 20 61 20 64 65 73 74 72 75 63 74 call a destruct
2d020 6f 72 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e or on the conten
2d030 74 0a 2a 2a 20 77 68 65 6e 20 69 74 20 68 61 73 t.** when it has
2d040 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20 finished using
2d050 74 68 61 74 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 that result..**
2d060 49 66 20 74 68 65 20 34 74 68 20 70 61 72 61 6d If the 4th param
2d070 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 eter to the sqli
2d080 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a te3_result_text*
2d090 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 6f interfaces.** o
2d0a0 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 r sqlite3_result
2d0b0 5f 62 6c 6f 62 20 69 73 20 74 68 65 20 73 70 65 _blob is the spe
2d0c0 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 53 51 cial constant SQ
2d0d0 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 0a 2a LITE_TRANSIENT.*
2d0e0 2a 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d 61 * then SQLite ma
2d0f0 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 kes a copy of th
2d100 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 73 70 e result into sp
2d110 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ace obtained fro
2d120 6d 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74 m.** from [sqlit
2d130 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62 65 66 e3_malloc()] bef
2d140 6f 72 65 20 69 74 20 72 65 74 75 72 6e 73 2e 0a ore it returns..
2d150 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
2d160 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 3_result_value()
2d170 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 interface sets
2d180 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a the result of.**
2d190 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
2d1a0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
2d1b0 6e 20 74 6f 20 62 65 20 61 20 63 6f 70 79 20 74 n to be a copy t
2d1c0 68 65 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74 he.** [unprotect
2d1d0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
2d1e0 5d 20 6f 62 6a 65 63 74 20 73 70 65 63 69 66 69 ] object specifi
2d1f0 65 64 20 62 79 20 74 68 65 20 32 6e 64 20 70 61 ed by the 2nd pa
2d200 72 61 6d 65 74 65 72 2e 20 20 54 68 65 0a 2a 2a rameter. The.**
2d210 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2d220 76 61 6c 75 65 28 29 20 69 6e 74 65 72 66 61 63 value() interfac
2d230 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f e makes a copy o
2d240 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 f the [sqlite3_v
2d250 61 6c 75 65 5d 0a 2a 2a 20 73 6f 20 74 68 61 74 alue].** so that
2d260 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 the [sqlite3_va
2d270 6c 75 65 5d 20 73 70 65 63 69 66 69 65 64 20 69 lue] specified i
2d280 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 n the parameter
2d290 6d 61 79 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a may change or.**
2d2a0 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 be deallocated
2d2b0 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 72 65 after sqlite3_re
2d2c0 73 75 6c 74 5f 76 61 6c 75 65 28 29 20 72 65 74 sult_value() ret
2d2d0 75 72 6e 73 20 77 69 74 68 6f 75 74 20 68 61 72 urns without har
2d2e0 6d 2e 0a 2a 2a 20 41 20 5b 70 72 6f 74 65 63 74 m..** A [protect
2d2f0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
2d300 5d 20 6f 62 6a 65 63 74 20 6d 61 79 20 61 6c 77 ] object may alw
2d310 61 79 73 20 62 65 20 75 73 65 64 20 77 68 65 72 ays be used wher
2d320 65 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 e an.** [unprote
2d330 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
2d340 75 65 5d 20 6f 62 6a 65 63 74 20 69 73 20 72 65 ue] object is re
2d350 71 75 69 72 65 64 2c 20 73 6f 20 65 69 74 68 65 quired, so eithe
2d360 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 5b 73 71 r.** kind of [sq
2d370 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a lite3_value] obj
2d380 65 63 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 ect can be used
2d390 77 69 74 68 20 74 68 69 73 20 69 6e 74 65 72 66 with this interf
2d3a0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ace..**.** If th
2d3b0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ese routines are
2d3c0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 called from wit
2d3d0 68 69 6e 20 74 68 65 20 64 69 66 66 65 72 65 6e hin the differen
2d3e0 74 20 74 68 72 65 61 64 0a 2a 2a 20 74 68 61 6e t thread.** than
2d3f0 20 74 68 65 20 6f 6e 65 20 63 6f 6e 74 61 69 6e the one contain
2d400 69 6e 67 20 74 68 65 20 61 70 70 6c 69 63 61 74 ing the applicat
2d410 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ion-defined func
2d420 74 69 6f 6e 20 74 68 61 74 20 72 65 63 65 69 76 tion that receiv
2d430 65 64 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 ed.** the [sqlit
2d440 65 33 5f 63 6f 6e 74 65 78 74 5d 20 70 6f 69 6e e3_context] poin
2d450 74 65 72 2c 20 74 68 65 20 72 65 73 75 6c 74 73 ter, the results
2d460 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a are undefined..
2d470 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
2d480 74 73 3a 0a 2a 2a 20 5b 48 31 36 34 30 33 5d 20 ts:.** [H16403]
2d490 5b 48 31 36 34 30 36 5d 20 5b 48 31 36 34 30 39 [H16406] [H16409
2d4a0 5d 20 5b 48 31 36 34 31 32 5d 20 5b 48 31 36 34 ] [H16412] [H164
2d4b0 31 35 5d 20 5b 48 31 36 34 31 38 5d 20 5b 48 31 15] [H16418] [H1
2d4c0 36 34 32 31 5d 20 5b 48 31 36 34 32 34 5d 0a 2a 6421] [H16424].*
2d4d0 2a 20 5b 48 31 36 34 32 37 5d 20 5b 48 31 36 34 * [H16427] [H164
2d4e0 33 30 5d 20 5b 48 31 36 34 33 33 5d 20 5b 48 31 30] [H16433] [H1
2d4f0 36 34 33 36 5d 20 5b 48 31 36 34 33 39 5d 20 5b 6436] [H16439] [
2d500 48 31 36 34 34 32 5d 20 5b 48 31 36 34 34 35 5d H16442] [H16445]
2d510 20 5b 48 31 36 34 34 38 5d 0a 2a 2a 20 5b 48 31 [H16448].** [H1
2d520 36 34 35 31 5d 20 5b 48 31 36 34 35 34 5d 20 5b 6451] [H16454] [
2d530 48 31 36 34 35 37 5d 20 5b 48 31 36 34 36 30 5d H16457] [H16460]
2d540 20 5b 48 31 36 34 36 33 5d 0a 2a 2f 0a 53 51 4c [H16463].*/.SQL
2d550 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
2d560 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 ite3_result_blob
2d570 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
2d580 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 *, const void*,
2d590 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 int, void(*)(voi
2d5a0 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 d*));.SQLITE_API
2d5b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
2d5c0 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69 sult_double(sqli
2d5d0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64 6f te3_context*, do
2d5e0 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 uble);.SQLITE_AP
2d5f0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
2d600 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69 esult_error(sqli
2d610 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f te3_context*, co
2d620 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b nst char*, int);
2d630 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
2d640 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2d650 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f error16(sqlite3_
2d660 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 context*, const
2d670 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c void*, int);.SQL
2d680 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
2d690 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
2d6a0 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 33 r_toobig(sqlite3
2d6b0 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 _context*);.SQLI
2d6c0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
2d6d0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
2d6e0 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63 _nomem(sqlite3_c
2d6f0 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 ontext*);.SQLITE
2d700 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
2d710 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 3_result_error_c
2d720 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ode(sqlite3_cont
2d730 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ext*, int);.SQLI
2d740 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
2d750 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 73 te3_result_int(s
2d760 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
2d770 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 int);.SQLITE_AP
2d780 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
2d790 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69 esult_int64(sqli
2d7a0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 71 te3_context*, sq
2d7b0 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 lite3_int64);.SQ
2d7c0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
2d7d0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c lite3_result_nul
2d7e0 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 l(sqlite3_contex
2d7f0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 t*);.SQLITE_API
2d800 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
2d810 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 65 33 ult_text(sqlite3
2d820 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 _context*, const
2d830 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 6f 69 char*, int, voi
2d840 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 d(*)(void*));.SQ
2d850 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
2d860 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2d870 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 t16(sqlite3_cont
2d880 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 ext*, const void
2d890 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 *, int, void(*)(
2d8a0 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f void*));.SQLITE_
2d8b0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
2d8c0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 _result_text16le
2d8d0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
2d8e0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 *, const void*,
2d8f0 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 int,void(*)(void
2d900 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 *));.SQLITE_API
2d910 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
2d920 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73 71 6c ult_text16be(sql
2d930 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 ite3_context*, c
2d940 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c onst void*, int,
2d950 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b void(*)(void*));
2d960 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
2d970 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2d980 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f value(sqlite3_co
2d990 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f ntext*, sqlite3_
2d9a0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f value*);.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 7a 65 72 6f 62 6c 6f 62 _result_zeroblob
2d9d0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
2d9e0 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a *, int n);../*.*
2d9f0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 66 69 * CAPI3REF: Defi
2da00 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 6e 67 ne New Collating
2da10 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31 36 36 Sequences {H166
2da20 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 2a 0a 00} <S20300>.**.
2da30 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f ** These functio
2da40 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 ns are used to a
2da50 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e dd new collation
2da60 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20 74 68 sequences to th
2da70 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 e.** [database c
2da80 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 onnection] speci
2da90 66 69 65 64 20 61 73 20 74 68 65 20 66 69 72 73 fied as the firs
2daa0 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a t argument..**.*
2dab0 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 * The name of th
2dac0 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 e new collation
2dad0 73 65 71 75 65 6e 63 65 20 69 73 20 73 70 65 63 sequence is spec
2dae0 69 66 69 65 64 20 61 73 20 61 20 55 54 46 2d 38 ified as a UTF-8
2daf0 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 73 string.** for s
2db00 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
2db10 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 73 71 llation() and sq
2db20 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c lite3_create_col
2db30 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a 20 61 lation_v2().** a
2db40 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 nd a UTF-16 stri
2db50 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 ng for sqlite3_c
2db60 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 reate_collation1
2db70 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61 73 65 6(). In all case
2db80 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 69 73 s.** the name is
2db90 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 passed as the s
2dba0 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 econd function a
2dbb0 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 rgument..**.** T
2dbc0 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e he third argumen
2dbd0 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 20 t may be one of
2dbe0 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b 53 the constants [S
2dbf0 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a 2a 20 QLITE_UTF8],.**
2dc00 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d [SQLITE_UTF16LE]
2dc10 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 54 46 , or [SQLITE_UTF
2dc20 31 36 42 45 5d 2c 20 69 6e 64 69 63 61 74 69 6e 16BE], indicatin
2dc30 67 20 74 68 61 74 20 74 68 65 20 75 73 65 72 2d g that the user-
2dc40 73 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f 75 74 supplied.** rout
2dc50 69 6e 65 20 65 78 70 65 63 74 73 20 74 6f 20 62 ine expects to b
2dc60 65 20 70 61 73 73 65 64 20 70 6f 69 6e 74 65 72 e passed pointer
2dc70 73 20 74 6f 20 73 74 72 69 6e 67 73 20 65 6e 63 s to strings enc
2dc80 6f 64 65 64 20 75 73 69 6e 67 20 55 54 46 2d 38 oded using UTF-8
2dc90 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 ,.** UTF-16 litt
2dca0 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 le-endian, or UT
2dcb0 46 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 2c F-16 big-endian,
2dcc0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54 respectively. T
2dcd0 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67 75 he.** third argu
2dce0 6d 65 6e 74 20 6d 69 67 68 74 20 61 6c 73 6f 20 ment might also
2dcf0 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 be [SQLITE_UTF16
2dd00 5d 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 ] to indicate th
2dd10 61 74 20 74 68 65 20 72 6f 75 74 69 6e 65 0a 2a at the routine.*
2dd20 2a 20 65 78 70 65 63 74 73 20 70 6f 69 6e 74 65 * expects pointe
2dd30 72 73 20 74 6f 20 62 65 20 55 54 46 2d 31 36 20 rs to be UTF-16
2dd40 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65 20 6e strings in the n
2dd50 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 ative byte order
2dd60 2c 20 6f 72 20 74 68 65 0a 2a 2a 20 61 72 67 75 , or the.** argu
2dd70 6d 65 6e 74 20 63 61 6e 20 62 65 20 5b 53 51 4c ment can be [SQL
2dd80 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 ITE_UTF16_ALIGNE
2dd90 44 5d 20 69 66 20 74 68 65 0a 2a 2a 20 74 68 65 D] if the.** the
2dda0 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63 74 73 routine expects
2ddb0 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31 36 2d pointers to 16-
2ddc0 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e 65 64 bit word aligned
2ddd0 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66 20 55 strings.** of U
2dde0 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 TF-16 in the nat
2ddf0 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e 0a ive byte order..
2de00 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 **.** A pointer
2de10 74 6f 20 74 68 65 20 75 73 65 72 20 73 75 70 70 to the user supp
2de20 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 6d 75 73 lied routine mus
2de30 74 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 t be passed as t
2de40 68 65 20 66 69 66 74 68 0a 2a 2a 20 61 72 67 75 he fifth.** argu
2de50 6d 65 6e 74 2e 20 20 49 66 20 69 74 20 69 73 20 ment. If it is
2de60 4e 55 4c 4c 2c 20 74 68 69 73 20 69 73 20 74 68 NULL, this is th
2de70 65 20 73 61 6d 65 20 61 73 20 64 65 6c 65 74 69 e same as deleti
2de80 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e ng the collation
2de90 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 28 73 6f .** sequence (so
2dea0 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e that SQLite can
2deb0 6e 6f 74 20 63 61 6c 6c 20 69 74 20 61 6e 79 6d not call it anym
2dec0 6f 72 65 29 2e 0a 2a 2a 20 45 61 63 68 20 74 69 ore)..** Each ti
2ded0 6d 65 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 me the applicati
2dee0 6f 6e 20 73 75 70 70 6c 69 65 64 20 66 75 6e 63 on supplied func
2def0 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 2c tion is invoked,
2df00 20 69 74 20 69 73 20 70 61 73 73 65 64 0a 2a 2a it is passed.**
2df10 20 61 73 20 69 74 73 20 66 69 72 73 74 20 70 61 as its first pa
2df20 72 61 6d 65 74 65 72 20 61 20 63 6f 70 79 20 6f rameter a copy o
2df30 66 20 74 68 65 20 76 6f 69 64 2a 20 70 61 73 73 f the void* pass
2df40 65 64 20 61 73 20 74 68 65 20 66 6f 75 72 74 68 ed as the fourth
2df50 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 argument.** to
2df60 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
2df70 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f 72 20 73 71 ollation() or sq
2df80 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c lite3_create_col
2df90 6c 61 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a lation16()..**.*
2dfa0 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 * The remaining
2dfb0 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 arguments to the
2dfc0 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75 70 application-sup
2dfd0 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 61 72 plied routine ar
2dfe0 65 20 74 77 6f 20 73 74 72 69 6e 67 73 2c 0a 2a e two strings,.*
2dff0 2a 20 65 61 63 68 20 72 65 70 72 65 73 65 6e 74 * each represent
2e000 65 64 20 62 79 20 61 20 28 6c 65 6e 67 74 68 2c ed by a (length,
2e010 20 64 61 74 61 29 20 70 61 69 72 20 61 6e 64 20 data) pair and
2e020 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20 65 encoded in the e
2e030 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 61 74 20 ncoding.** that
2e040 77 61 73 20 70 61 73 73 65 64 20 61 73 20 74 68 was passed as th
2e050 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 e third argument
2e060 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c 61 74 when the collat
2e070 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 61 73 ion sequence was
2e080 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 2e 20 .** registered.
2e090 7b 45 4e 44 7d 20 20 54 68 65 20 61 70 70 6c 69 {END} The appli
2e0a0 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 63 cation defined c
2e0b0 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 ollation routine
2e0c0 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72 should.** retur
2e0d0 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f n negative, zero
2e0e0 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 or positive if
2e0f0 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e 67 the first string
2e100 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 0a 2a is less than,.*
2e110 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 * equal to, or g
2e120 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 reater than the
2e130 73 65 63 6f 6e 64 20 73 74 72 69 6e 67 2e 20 69 second string. i
2e140 2e 65 2e 20 28 53 54 52 49 4e 47 31 20 2d 20 53 .e. (STRING1 - S
2e150 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a 2a 2a 20 54 TRING2)..**.** T
2e160 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 he sqlite3_creat
2e170 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 e_collation_v2()
2e180 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 works like sqli
2e190 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 te3_create_colla
2e1a0 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 63 65 70 74 tion().** except
2e1b0 20 74 68 61 74 20 69 74 20 74 61 6b 65 73 20 61 that it takes a
2e1c0 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65 6e 74 n extra argument
2e1d0 20 77 68 69 63 68 20 69 73 20 61 20 64 65 73 74 which is a dest
2e1e0 72 75 63 74 6f 72 20 66 6f 72 0a 2a 2a 20 74 68 ructor for.** th
2e1f0 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e 20 20 54 68 e collation. Th
2e200 65 20 64 65 73 74 72 75 63 74 6f 72 20 69 73 20 e destructor is
2e210 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 called when the
2e220 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 collation is.**
2e230 64 65 73 74 72 6f 79 65 64 20 61 6e 64 20 69 73 destroyed and is
2e240 20 70 61 73 73 65 64 20 61 20 63 6f 70 79 20 6f passed a copy o
2e250 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 f the fourth par
2e260 61 6d 65 74 65 72 20 76 6f 69 64 2a 20 70 6f 69 ameter void* poi
2e270 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 20 73 nter.** of the s
2e280 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
2e290 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 2e 0a 2a 2a llation_v2()..**
2e2a0 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61 72 65 20 Collations are
2e2b0 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e 20 74 destroyed when t
2e2c0 68 65 79 20 61 72 65 20 6f 76 65 72 72 69 64 64 hey are overridd
2e2d0 65 6e 20 62 79 20 6c 61 74 65 72 20 63 61 6c 6c en by later call
2e2e0 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c s to the.** coll
2e2f0 61 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 66 ation creation f
2e300 75 6e 63 74 69 6f 6e 73 20 6f 72 20 77 68 65 6e unctions or when
2e310 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
2e320 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 63 6c onnection] is cl
2e330 6f 73 65 64 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 osed.** using [s
2e340 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e qlite3_close()].
2e350 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
2e360 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 [sqlite3_colla
2e370 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 5d 20 61 tion_needed()] a
2e380 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c nd [sqlite3_coll
2e390 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 29 ation_needed16()
2e3a0 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 ]..**.** Require
2e3b0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 36 30 ments:.** [H1660
2e3c0 33 5d 20 5b 48 31 36 36 30 34 5d 20 5b 48 31 36 3] [H16604] [H16
2e3d0 36 30 36 5d 20 5b 48 31 36 36 30 39 5d 20 5b 48 606] [H16609] [H
2e3e0 31 36 36 31 32 5d 20 5b 48 31 36 36 31 35 5d 20 16612] [H16615]
2e3f0 5b 48 31 36 36 31 38 5d 20 5b 48 31 36 36 32 31 [H16618] [H16621
2e400 5d 0a 2a 2a 20 5b 48 31 36 36 32 34 5d 20 5b 48 ].** [H16624] [H
2e410 31 36 36 32 37 5d 20 5b 48 31 36 36 33 30 5d 0a 16627] [H16630].
2e420 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
2e430 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 t sqlite3_create
2e440 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71 _collation(. sq
2e450 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 lite3*, . const
2e460 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 char *zName, .
2e470 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a int eTextRep, .
2e480 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a void*,. int(*
2e490 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c xCompare)(void*,
2e4a0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c int,const void*,
2e4b0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 int,const void*)
2e4c0 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .);.SQLITE_API i
2e4d0 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 nt sqlite3_creat
2e4e0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 0a e_collation_v2(.
2e4f0 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 sqlite3*, . c
2e500 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
2e510 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 , . int eTextRe
2e520 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 p, . void*,. i
2e530 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f nt(*xCompare)(vo
2e540 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f id*,int,const vo
2e550 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f id*,int,const vo
2e560 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44 id*),. void(*xD
2e570 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a 29 estroy)(void*).)
2e580 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
2e590 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
2e5a0 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 73 collation16(. s
2e5b0 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 qlite3*, . cons
2e5c0 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a 20 t void *zName,.
2e5d0 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a int eTextRep, .
2e5e0 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a void*,. int(*
2e5f0 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c xCompare)(void*,
2e600 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c int,const void*,
2e610 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 int,const void*)
2e620 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .);../*.** CAPI3
2e630 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 4e REF: Collation N
2e640 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b 73 20 eeded Callbacks
2e650 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33 30 30 {H16700} <S20300
2e660 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 >.**.** To avoid
2e670 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67 69 73 having to regis
2e680 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 6f ter all collatio
2e690 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65 66 6f n sequences befo
2e6a0 72 65 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a re a database.**
2e6b0 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 61 20 can be used, a
2e6c0 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b 20 single callback
2e6d0 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 function may be
2e6e0 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20 registered with
2e6f0 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 the.** [database
2e700 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f 20 connection] to
2e710 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 65 76 be called whenev
2e720 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 20 er an undefined
2e730 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 collation.** seq
2e740 75 65 6e 63 65 20 69 73 20 72 65 71 75 69 72 65 uence is require
2e750 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
2e760 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 67 69 function is regi
2e770 73 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 stered using the
2e780 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 sqlite3_collati
2e790 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50 49 2c on_needed() API,
2e7a0 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 70 .** then it is p
2e7b0 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65 73 20 assed the names
2e7c0 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c of undefined col
2e7d0 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 lation sequences
2e7e0 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a 20 65 as strings.** e
2e7f0 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e ncoded in UTF-8.
2e800 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73 71 6c {H16703} If sql
2e810 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e ite3_collation_n
2e820 65 65 64 65 64 31 36 28 29 20 69 73 20 75 73 65 eeded16() is use
2e830 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20 d,.** the names
2e840 61 72 65 20 70 61 73 73 65 64 20 61 73 20 55 54 are passed as UT
2e850 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e 65 20 F-16 in machine
2e860 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 native byte orde
2e870 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 r..** A call to
2e880 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 either function
2e890 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65 78 69 replaces any exi
2e8a0 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b 2e 0a sting callback..
2e8b0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 **.** When the c
2e8c0 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b allback is invok
2e8d0 65 64 2c 20 74 68 65 20 66 69 72 73 74 20 61 72 ed, the first ar
2e8e0 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 69 73 gument passed is
2e8f0 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74 68 a copy.** of th
2e900 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
2e910 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c t to sqlite3_col
2e920 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 lation_needed()
2e930 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f or.** sqlite3_co
2e940 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 llation_needed16
2e950 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 (). The second
2e960 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
2e970 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e database.** conn
2e980 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 68 69 ection. The thi
2e990 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f rd argument is o
2e9a0 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 55 54 ne of [SQLITE_UT
2e9b0 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 54 46 F8], [SQLITE_UTF
2e9c0 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 16BE],.** or [SQ
2e9d0 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c 20 69 LITE_UTF16LE], i
2e9e0 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6d 6f ndicating the mo
2e9f0 73 74 20 64 65 73 69 72 61 62 6c 65 20 66 6f 72 st desirable for
2ea00 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 m of the collati
2ea10 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 66 on.** sequence f
2ea20 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 64 unction required
2ea30 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 . The fourth pa
2ea40 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e rameter is the n
2ea50 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 ame of the.** re
2ea60 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e quired collation
2ea70 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a sequence..**.**
2ea80 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 The callback fu
2ea90 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65 nction should re
2eaa0 67 69 73 74 65 72 20 74 68 65 20 64 65 73 69 72 gister the desir
2eab0 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 73 69 ed collation usi
2eac0 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 ng.** [sqlite3_c
2ead0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 reate_collation(
2eae0 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 )], [sqlite3_cre
2eaf0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 ate_collation16(
2eb00 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 )], or.** [sqlit
2eb10 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 e3_create_collat
2eb20 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a ion_v2()]..**.**
2eb30 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
2eb40 2a 20 5b 48 31 36 37 30 32 5d 20 5b 48 31 36 37 * [H16702] [H167
2eb50 30 34 5d 20 5b 48 31 36 37 30 36 5d 0a 2a 2f 0a 04] [H16706].*/.
2eb60 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
2eb70 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e qlite3_collation
2eb80 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 6c 69 74 _needed(. sqlit
2eb90 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c 20 0a e3*, . void*, .
2eba0 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c void(*)(void*,
2ebb0 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 sqlite3*,int eTe
2ebc0 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72 xtRep,const char
2ebd0 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 *).);.SQLITE_API
2ebe0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c int sqlite3_col
2ebf0 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 lation_needed16(
2ec00 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 . sqlite3*, .
2ec10 76 6f 69 64 2a 2c 0a 20 20 76 6f 69 64 28 2a 29 void*,. void(*)
2ec20 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c (void*,sqlite3*,
2ec30 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e int eTextRep,con
2ec40 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f 2a st void*).);../*
2ec50 0a 2a 2a 20 53 70 65 63 69 66 79 20 74 68 65 20 .** Specify the
2ec60 6b 65 79 20 66 6f 72 20 61 6e 20 65 6e 63 72 79 key for an encry
2ec70 70 74 65 64 20 64 61 74 61 62 61 73 65 2e 20 20 pted database.
2ec80 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f This routine sho
2ec90 75 6c 64 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 uld be.** called
2eca0 20 72 69 67 68 74 20 61 66 74 65 72 20 73 71 6c right after sql
2ecb0 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a 2a 0a ite3_open()..**.
2ecc0 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69 ** The code to i
2ecd0 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50 mplement this AP
2ece0 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 I is not availab
2ecf0 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63 le in the public
2ed00 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53 release.** of S
2ed10 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 QLite..*/.SQLITE
2ed20 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
2ed30 5f 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33 20 _key(. sqlite3
2ed40 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 *db,
2ed50 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
2ed60 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79 65 64 se to be rekeyed
2ed70 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
2ed80 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 *pKey, int nKey
2ed90 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 /* The key
2eda0 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 */.);../*.** Cha
2edb0 6e 67 65 20 74 68 65 20 6b 65 79 20 6f 6e 20 61 nge the key on a
2edc0 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 2e n open database.
2edd0 20 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 If the current
2ede0 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 database is not
2edf0 0a 2a 2a 20 65 6e 63 72 79 70 74 65 64 2c 20 74 .** encrypted, t
2ee00 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
2ee10 20 65 6e 63 72 79 70 74 20 69 74 2e 20 20 49 66 encrypt it. If
2ee20 20 70 4e 65 77 3d 3d 30 20 6f 72 20 6e 4e 65 77 pNew==0 or nNew
2ee30 3d 3d 30 2c 20 74 68 65 0a 2a 2a 20 64 61 74 61 ==0, the.** data
2ee40 62 61 73 65 20 69 73 20 64 65 63 72 79 70 74 65 base is decrypte
2ee50 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 d..**.** The cod
2ee60 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 e to implement t
2ee70 68 69 73 20 41 50 49 20 69 73 20 6e 6f 74 20 61 his API is not a
2ee80 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 vailable in the
2ee90 70 75 62 6c 69 63 20 72 65 6c 65 61 73 65 0a 2a public release.*
2eea0 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a * of SQLite..*/.
2eeb0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
2eec0 71 6c 69 74 65 33 5f 72 65 6b 65 79 28 0a 20 20 qlite3_rekey(.
2eed0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
2eee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2eef0 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 62 65 * Database to be
2ef00 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20 63 6f rekeyed */. co
2ef10 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 nst void *pKey,
2ef20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f 2a 20 int nKey /*
2ef30 54 68 65 20 6e 65 77 20 6b 65 79 20 2a 2f 0a 29 The new key */.)
2ef40 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
2ef50 46 3a 20 53 75 73 70 65 6e 64 20 45 78 65 63 75 F: Suspend Execu
2ef60 74 69 6f 6e 20 46 6f 72 20 41 20 53 68 6f 72 74 tion For A Short
2ef70 20 54 69 6d 65 20 7b 48 31 30 35 33 30 7d 20 3c Time {H10530} <
2ef80 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S40410>.**.** Th
2ef90 65 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28 e sqlite3_sleep(
2efa0 29 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 73 65 ) function cause
2efb0 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 s the current th
2efc0 72 65 61 64 20 74 6f 20 73 75 73 70 65 6e 64 20 read to suspend
2efd0 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 66 6f 72 execution.** for
2efe0 20 61 74 20 6c 65 61 73 74 20 61 20 6e 75 6d 62 at least a numb
2eff0 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e er of millisecon
2f000 64 73 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 ds specified in
2f010 69 74 73 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a its parameter..*
2f020 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 72 *.** If the oper
2f030 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64 6f 65 ating system doe
2f040 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 6c s not support sl
2f050 65 65 70 20 72 65 71 75 65 73 74 73 20 77 69 74 eep requests wit
2f060 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 h.** millisecond
2f070 20 74 69 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e time resolution
2f080 2c 20 74 68 65 6e 20 74 68 65 20 74 69 6d 65 20 , then the time
2f090 77 69 6c 6c 20 62 65 20 72 6f 75 6e 64 65 64 20 will be rounded
2f0a0 75 70 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 65 61 up to.** the nea
2f0b0 72 65 73 74 20 73 65 63 6f 6e 64 2e 20 54 68 65 rest second. The
2f0c0 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 number of milli
2f0d0 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 seconds of sleep
2f0e0 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 actually.** req
2f0f0 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 uested from the
2f100 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
2f110 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
2f120 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d 70 6c 65 .** SQLite imple
2f130 6d 65 6e 74 73 20 74 68 69 73 20 69 6e 74 65 72 ments this inter
2f140 66 61 63 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 face by calling
2f150 74 68 65 20 78 53 6c 65 65 70 28 29 0a 2a 2a 20 the xSleep().**
2f160 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 64 65 method of the de
2f170 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 fault [sqlite3_v
2f180 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a fs] object..**.*
2f190 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 * Requirements:
2f1a0 5b 48 31 30 35 33 33 5d 20 5b 48 31 30 35 33 36 [H10533] [H10536
2f1b0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
2f1c0 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 int sqlite3_slee
2f1d0 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 p(int);../*.** C
2f1e0 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 API3REF: Name Of
2f1f0 20 54 68 65 20 46 6f 6c 64 65 72 20 48 6f 6c 64 The Folder Hold
2f200 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 20 46 69 ing Temporary Fi
2f210 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 3c 53 32 les {H10310} <S2
2f220 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 0000>.**.** If t
2f230 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 his global varia
2f240 62 6c 65 20 69 73 20 6d 61 64 65 20 74 6f 20 70 ble is made to p
2f250 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 oint to a string
2f260 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 which is.** the
2f270 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f 6c 64 65 name of a folde
2f280 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 65 63 74 r (a.k.a. direct
2f290 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c 6c 20 74 ory), then all t
2f2a0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 0a 2a emporary files.*
2f2b0 2a 20 63 72 65 61 74 65 64 20 62 79 20 53 51 4c * created by SQL
2f2c0 69 74 65 20 77 69 6c 6c 20 62 65 20 70 6c 61 63 ite will be plac
2f2d0 65 64 20 69 6e 20 74 68 61 74 20 64 69 72 65 63 ed in that direc
2f2e0 74 6f 72 79 2e 20 20 49 66 20 74 68 69 73 20 76 tory. If this v
2f2f0 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 ariable.** is a
2f300 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 NULL pointer, th
2f310 65 6e 20 53 51 4c 69 74 65 20 70 65 72 66 6f 72 en SQLite perfor
2f320 6d 73 20 61 20 73 65 61 72 63 68 20 66 6f 72 20 ms a search for
2f330 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a an appropriate.*
2f340 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 * temporary file
2f350 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a directory..**.*
2f360 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 * It is not safe
2f370 20 74 6f 20 72 65 61 64 20 6f 72 20 6d 6f 64 69 to read or modi
2f380 66 79 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 fy this variable
2f390 20 69 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e in more than on
2f3a0 65 0a 2a 2a 20 74 68 72 65 61 64 20 61 74 20 61 e.** thread at a
2f3b0 20 74 69 6d 65 2e 20 20 49 74 20 69 73 20 6e 6f time. It is no
2f3c0 74 20 73 61 66 65 20 74 6f 20 72 65 61 64 20 6f t safe to read o
2f3d0 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61 r modify this va
2f3e0 72 69 61 62 6c 65 0a 2a 2a 20 69 66 20 61 20 5b riable.** if a [
2f3f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
2f400 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20 75 73 ion] is being us
2f410 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 ed at the same t
2f420 69 6d 65 20 69 6e 20 61 20 73 65 70 61 72 61 74 ime in a separat
2f430 65 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a 20 e.** thread..**
2f440 49 74 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 It is intended t
2f450 68 61 74 20 74 68 69 73 20 76 61 72 69 61 62 6c hat this variabl
2f460 65 20 62 65 20 73 65 74 20 6f 6e 63 65 0a 2a 2a e be set once.**
2f470 20 61 73 20 70 61 72 74 20 6f 66 20 70 72 6f 63 as part of proc
2f480 65 73 73 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ess initializati
2f490 6f 6e 20 61 6e 64 20 62 65 66 6f 72 65 20 61 6e on and before an
2f4a0 79 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 y SQLite interfa
2f4b0 63 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 68 ce.** routines h
2f4c0 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 ave been called
2f4d0 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 76 61 and that this va
2f4e0 72 69 61 62 6c 65 20 72 65 6d 61 69 6e 20 75 6e riable remain un
2f4f0 63 68 61 6e 67 65 64 0a 2a 2a 20 74 68 65 72 65 changed.** there
2f500 61 66 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 after..**.** The
2f510 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 [temp_store_dir
2f520 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 6d ectory pragma] m
2f530 61 79 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76 ay modify this v
2f540 61 72 69 61 62 6c 65 20 61 6e 64 20 63 61 75 73 ariable and caus
2f550 65 0a 2a 2a 20 69 74 20 74 6f 20 70 6f 69 6e 74 e.** it to point
2f560 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 to memory obtai
2f570 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 ned from [sqlite
2f580 33 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 46 75 72 74 3_malloc]. Furt
2f590 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20 74 68 65 20 hermore,.** the
2f5a0 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 [temp_store_dire
2f5b0 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 61 6c ctory pragma] al
2f5c0 77 61 79 73 20 61 73 73 75 6d 65 73 20 74 68 61 ways assumes tha
2f5d0 74 20 61 6e 79 20 73 74 72 69 6e 67 0a 2a 2a 20 t any string.**
2f5e0 74 68 61 74 20 74 68 69 73 20 76 61 72 69 61 62 that this variab
2f5f0 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 le points to is
2f600 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f held in memory o
2f610 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 0a 2a 2a btained from .**
2f620 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
2f630 5d 20 61 6e 64 20 74 68 65 20 70 72 61 67 6d 61 ] and the pragma
2f640 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 6f 20 may attempt to
2f650 66 72 65 65 20 74 68 61 74 20 6d 65 6d 6f 72 79 free that memory
2f660 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 .** using [sqlit
2f670 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a 20 48 65 6e e3_free]..** Hen
2f680 63 65 2c 20 69 66 20 74 68 69 73 20 76 61 72 69 ce, if this vari
2f690 61 62 6c 65 20 69 73 20 6d 6f 64 69 66 69 65 64 able is modified
2f6a0 20 64 69 72 65 63 74 6c 79 2c 20 65 69 74 68 65 directly, eithe
2f6b0 72 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0a 2a r it should be.*
2f6c0 2a 20 6d 61 64 65 20 4e 55 4c 4c 20 6f 72 20 6d * made NULL or m
2f6d0 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 ade to point to
2f6e0 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 memory obtained
2f6f0 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 from [sqlite3_ma
2f700 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20 65 6c 73 65 lloc].** or else
2f710 20 74 68 65 20 75 73 65 20 6f 66 20 74 68 65 20 the use of the
2f720 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 [temp_store_dire
2f730 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 73 68 ctory pragma] sh
2f740 6f 75 6c 64 20 62 65 20 61 76 6f 69 64 65 64 2e ould be avoided.
2f750 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
2f760 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 74 65 6d har *sqlite3_tem
2f770 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 0a 2f 2a p_directory;../*
2f780 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 .** CAPI3REF: Te
2f790 73 74 20 46 6f 72 20 41 75 74 6f 2d 43 6f 6d 6d st For Auto-Comm
2f7a0 69 74 20 4d 6f 64 65 20 7b 48 31 32 39 33 30 7d it Mode {H12930}
2f7b0 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 <S60200>.** KEY
2f7c0 57 4f 52 44 53 3a 20 7b 61 75 74 6f 63 6f 6d 6d WORDS: {autocomm
2f7d0 69 74 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a 20 54 it mode}.**.** T
2f7e0 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 he sqlite3_get_a
2f7f0 75 74 6f 63 6f 6d 6d 69 74 28 29 20 69 6e 74 65 utocommit() inte
2f800 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 6e 6f rface returns no
2f810 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20 7a 65 72 n-zero or.** zer
2f820 6f 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 64 o if the given d
2f830 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
2f840 6f 6e 20 69 73 20 6f 72 20 69 73 20 6e 6f 74 20 on is or is not
2f850 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f in autocommit mo
2f860 64 65 2c 0a 2a 2a 20 72 65 73 70 65 63 74 69 76 de,.** respectiv
2f870 65 6c 79 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74 ely. Autocommit
2f880 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79 20 64 mode is on by d
2f890 65 66 61 75 6c 74 2e 0a 2a 2a 20 41 75 74 6f 63 efault..** Autoc
2f8a0 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 64 69 ommit mode is di
2f8b0 73 61 62 6c 65 64 20 62 79 20 61 20 5b 42 45 47 sabled by a [BEG
2f8c0 49 4e 5d 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a IN] statement..*
2f8d0 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 * Autocommit mod
2f8e0 65 20 69 73 20 72 65 2d 65 6e 61 62 6c 65 64 20 e is re-enabled
2f8f0 62 79 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 6f 72 by a [COMMIT] or
2f900 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a 0a [ROLLBACK]..**.
2f910 2a 2a 20 49 66 20 63 65 72 74 61 69 6e 20 6b 69 ** If certain ki
2f920 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20 6f 63 nds of errors oc
2f930 63 75 72 20 6f 6e 20 61 20 73 74 61 74 65 6d 65 cur on a stateme
2f940 6e 74 20 77 69 74 68 69 6e 20 61 20 6d 75 6c 74 nt within a mult
2f950 69 2d 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 i-statement.** t
2f960 72 61 6e 73 61 63 74 69 6f 6e 20 28 65 72 72 6f ransaction (erro
2f970 72 73 20 69 6e 63 6c 75 64 69 6e 67 20 5b 53 51 rs including [SQ
2f980 4c 49 54 45 5f 46 55 4c 4c 5d 2c 20 5b 53 51 4c LITE_FULL], [SQL
2f990 49 54 45 5f 49 4f 45 52 52 5d 2c 0a 2a 2a 20 5b ITE_IOERR],.** [
2f9a0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b SQLITE_NOMEM], [
2f9b0 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c 20 61 6e SQLITE_BUSY], an
2f9c0 64 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 d [SQLITE_INTERR
2f9d0 55 50 54 5d 29 20 74 68 65 6e 20 74 68 65 0a 2a UPT]) then the.*
2f9e0 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 69 * transaction mi
2f9f0 67 68 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 ght be rolled ba
2fa00 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ck automatically
2fa10 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 61 79 20 . The only way
2fa20 74 6f 0a 2a 2a 20 66 69 6e 64 20 6f 75 74 20 77 to.** find out w
2fa30 68 65 74 68 65 72 20 53 51 4c 69 74 65 20 61 75 hether SQLite au
2fa40 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f 6c 6c tomatically roll
2fa50 65 64 20 62 61 63 6b 20 74 68 65 20 74 72 61 6e ed back the tran
2fa60 73 61 63 74 69 6f 6e 20 61 66 74 65 72 0a 2a 2a saction after.**
2fa70 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74 6f 20 an error is to
2fa80 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f use this functio
2fa90 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 n..**.** If anot
2faa0 68 65 72 20 74 68 72 65 61 64 20 63 68 61 6e 67 her thread chang
2fab0 65 73 20 74 68 65 20 61 75 74 6f 63 6f 6d 6d 69 es the autocommi
2fac0 74 20 73 74 61 74 75 73 20 6f 66 20 74 68 65 20 t status of the
2fad0 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e database.** conn
2fae0 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 69 ection while thi
2faf0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75 6e s routine is run
2fb00 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 72 ning, then the r
2fb10 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 eturn value.** i
2fb20 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a s undefined..**.
2fb30 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
2fb40 20 5b 48 31 32 39 33 31 5d 20 5b 48 31 32 39 33 [H12931] [H1293
2fb50 32 5d 20 5b 48 31 32 39 33 33 5d 20 5b 48 31 32 2] [H12933] [H12
2fb60 39 33 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 934].*/.SQLITE_A
2fb70 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 PI int sqlite3_g
2fb80 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 73 71 et_autocommit(sq
2fb90 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 lite3*);../*.**
2fba0 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64 20 54 CAPI3REF: Find T
2fbb0 68 65 20 44 61 74 61 62 61 73 65 20 48 61 6e 64 he Database Hand
2fbc0 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72 65 64 le Of A Prepared
2fbd0 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 31 Statement {H131
2fbe0 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 2a 2a 0a 20} <S60600>.**.
2fbf0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 64 ** The sqlite3_d
2fc00 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72 66 61 b_handle interfa
2fc10 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 5b ce returns the [
2fc20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
2fc30 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 74 ion] handle.** t
2fc40 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65 70 61 o which a [prepa
2fc50 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 62 red statement] b
2fc60 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 5b 64 61 elongs. The [da
2fc70 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
2fc80 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62 n].** returned b
2fc90 79 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e y sqlite3_db_han
2fca0 64 6c 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 dle is the same
2fcb0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
2fcc0 74 69 6f 6e 5d 20 74 68 61 74 20 77 61 73 20 74 tion] that was t
2fcd0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
2fce0 74 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 73 71 6c t.** to the [sql
2fcf0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
2fd00 29 5d 20 63 61 6c 6c 20 28 6f 72 20 69 74 73 20 )] call (or its
2fd10 76 61 72 69 61 6e 74 73 29 20 74 68 61 74 20 77 variants) that w
2fd20 61 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 72 as used to.** cr
2fd30 65 61 74 65 20 74 68 65 20 73 74 61 74 65 6d 65 eate the stateme
2fd40 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 nt in the first
2fd50 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 place..**.** Req
2fd60 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 33 31 uirements: [H131
2fd70 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 23].*/.SQLITE_AP
2fd80 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 I sqlite3 *sqlit
2fd90 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c e3_db_handle(sql
2fda0 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a ite3_stmt*);../*
2fdb0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 .** CAPI3REF: Fi
2fdc0 6e 64 20 74 68 65 20 6e 65 78 74 20 70 72 65 70 nd the next prep
2fdd0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 7b ared statement {
2fde0 48 31 33 31 34 30 7d 20 3c 53 36 30 36 30 30 3e H13140} <S60600>
2fdf0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 .**.** This inte
2fe00 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 rface returns a
2fe10 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e pointer to the n
2fe20 65 78 74 20 5b 70 72 65 70 61 72 65 64 20 73 74 ext [prepared st
2fe30 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 72 0a 2a atement] after.*
2fe40 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 69 61 74 * pStmt associat
2fe50 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 61 74 ed with the [dat
2fe60 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
2fe70 5d 20 70 44 62 2e 20 20 49 66 20 70 53 74 6d 74 ] pDb. If pStmt
2fe80 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 65 6e is NULL.** then
2fe90 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 this interface
2fea0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
2feb0 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 70 r to the first p
2fec0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
2fed0 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 t.** associated
2fee0 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73 with the databas
2fef0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 44 62 e connection pDb
2ff00 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 61 72 65 . If no prepare
2ff10 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 73 d statement.** s
2ff20 61 74 69 73 66 69 65 73 20 74 68 65 20 63 6f 6e atisfies the con
2ff30 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 69 73 20 ditions of this
2ff40 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 65 74 75 routine, it retu
2ff50 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 rns NULL..**.**
2ff60 54 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f The [database co
2ff70 6e 6e 65 63 74 69 6f 6e 5d 20 70 6f 69 6e 74 65 nnection] pointe
2ff80 72 20 44 20 69 6e 20 61 20 63 61 6c 6c 20 74 6f r D in a call to
2ff90 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6e 65 78 .** [sqlite3_nex
2ffa0 74 5f 73 74 6d 74 28 44 2c 53 29 5d 20 6d 75 73 t_stmt(D,S)] mus
2ffb0 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20 6f 70 t refer to an op
2ffc0 65 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 en database.** c
2ffd0 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e 64 20 69 6e onnection and in
2ffe0 20 70 61 72 74 69 63 75 6c 61 72 20 6d 75 73 74 particular must
2fff0 20 6e 6f 74 20 62 65 20 61 20 4e 55 4c 4c 20 70 not be a NULL p
30000 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 ointer..**.** Re
30010 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 33 quirements: [H13
30020 31 34 33 5d 20 5b 48 31 33 31 34 36 5d 20 5b 48 143] [H13146] [H
30030 31 33 31 34 39 5d 20 5b 48 31 33 31 35 32 5d 0a 13149] [H13152].
30040 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 */.SQLITE_API sq
30050 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 71 6c 69 lite3_stmt *sqli
30060 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 73 71 te3_next_stmt(sq
30070 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 71 6c 69 lite3 *pDb, sqli
30080 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
30090 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
300a0 46 3a 20 43 6f 6d 6d 69 74 20 41 6e 64 20 52 6f F: Commit And Ro
300b0 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 69 63 61 74 llback Notificat
300c0 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48 ion Callbacks {H
300d0 31 32 39 35 30 7d 20 3c 53 36 30 34 30 30 3e 0a 12950} <S60400>.
300e0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
300f0 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 20 3_commit_hook()
30100 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 interface regist
30110 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 0a 2a ers a callback.*
30120 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 * function to be
30130 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 invoked wheneve
30140 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 r a transaction
30150 69 73 20 5b 43 4f 4d 4d 49 54 20 7c 20 63 6f 6d is [COMMIT | com
30160 6d 69 74 74 65 64 5d 2e 0a 2a 2a 20 41 6e 79 20 mitted]..** Any
30170 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 20 callback set by
30180 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 a previous call
30190 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 to sqlite3_commi
301a0 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f 72 20 t_hook().** for
301b0 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
301c0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 e connection is
301d0 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 20 54 overridden..** T
301e0 68 65 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 he sqlite3_rollb
301f0 61 63 6b 5f 68 6f 6f 6b 28 29 20 69 6e 74 65 72 ack_hook() inter
30200 66 61 63 65 20 72 65 67 69 73 74 65 72 73 20 61 face registers a
30210 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e callback.** fun
30220 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 76 6f ction to be invo
30230 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61 20 74 ked whenever a t
30240 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 5b 52 ransaction is [R
30250 4f 4c 4c 42 41 43 4b 20 7c 20 72 6f 6c 6c 65 64 OLLBACK | rolled
30260 20 62 61 63 6b 5d 2e 0a 2a 2a 20 41 6e 79 20 63 back]..** Any c
30270 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 20 61 allback set by a
30280 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 previous call t
30290 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 o sqlite3_commit
302a0 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f 72 20 74 _hook().** for t
302b0 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
302c0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f connection is o
302d0 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 20 54 68 verridden..** Th
302e0 65 20 70 41 72 67 20 61 72 67 75 6d 65 6e 74 20 e pArg argument
302f0 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 is passed throug
30300 68 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 h to the callbac
30310 6b 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 6c k..** If the cal
30320 6c 62 61 63 6b 20 6f 6e 20 61 20 63 6f 6d 6d 69 lback on a commi
30330 74 20 68 6f 6f 6b 20 66 75 6e 63 74 69 6f 6e 20 t hook function
30340 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f returns non-zero
30350 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 6f ,.** then the co
30360 6d 6d 69 74 20 69 73 20 63 6f 6e 76 65 72 74 65 mmit is converte
30370 64 20 69 6e 74 6f 20 61 20 72 6f 6c 6c 62 61 63 d into a rollbac
30380 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 k..**.** If anot
30390 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 her function was
303a0 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 previously regi
303b0 73 74 65 72 65 64 2c 20 69 74 73 0a 2a 2a 20 70 stered, its.** p
303c0 41 72 67 20 76 61 6c 75 65 20 69 73 20 72 65 74 Arg value is ret
303d0 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73 urned. Otherwis
303e0 65 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e e NULL is return
303f0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 ed..**.** The ca
30400 6c 6c 62 61 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 llback implement
30410 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 64 ation must not d
30420 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 o anything that
30430 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 will modify.** t
30440 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
30450 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f ection that invo
30460 6b 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b ked the callback
30470 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73 0a 2a . Any actions.*
30480 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 * to modify the
30490 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
304a0 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65 66 65 ion must be defe
304b0 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74 65 72 rred until after
304c0 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 the.** completi
304d0 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 on of the [sqlit
304e0 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c 6c 20 e3_step()] call
304f0 74 68 61 74 20 74 72 69 67 67 65 72 65 64 20 74 that triggered t
30500 68 65 20 63 6f 6d 6d 69 74 0a 2a 2a 20 6f 72 20 he commit.** or
30510 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20 69 6e rollback hook in
30520 20 74 68 65 20 66 69 72 73 74 20 70 6c 61 63 65 the first place
30530 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b ..** Note that [
30540 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
30550 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 v2()] and [sqlit
30560 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 e3_step()] both
30570 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 modify their.**
30580 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
30590 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 ions for the mea
305a0 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 ning of "modify"
305b0 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 in this paragra
305c0 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 ph..**.** Regist
305d0 65 72 69 6e 67 20 61 20 4e 55 4c 4c 20 66 75 6e ering a NULL fun
305e0 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 74 ction disables t
305f0 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a he callback..**.
30600 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 6f 6d 6d ** When the comm
30610 69 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b it hook callback
30620 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
30630 20 7a 65 72 6f 2c 20 74 68 65 20 5b 43 4f 4d 4d zero, the [COMM
30640 49 54 5d 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e IT].** operation
30650 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 63 is allowed to c
30660 6f 6e 74 69 6e 75 65 20 6e 6f 72 6d 61 6c 6c 79 ontinue normally
30670 2e 20 20 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 . If the commit
30680 20 68 6f 6f 6b 0a 2a 2a 20 72 65 74 75 72 6e 73 hook.** returns
30690 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 non-zero, then
306a0 74 68 65 20 5b 43 4f 4d 4d 49 54 5d 20 69 73 20 the [COMMIT] is
306b0 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 converted into a
306c0 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a 20 [ROLLBACK]..**
306d0 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f The rollback hoo
306e0 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 k is invoked on
306f0 61 20 72 6f 6c 6c 62 61 63 6b 20 74 68 61 74 20 a rollback that
30700 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 61 20 63 results from a c
30710 6f 6d 6d 69 74 0a 2a 2a 20 68 6f 6f 6b 20 72 65 ommit.** hook re
30720 74 75 72 6e 69 6e 67 20 6e 6f 6e 2d 7a 65 72 6f turning non-zero
30730 2c 20 6a 75 73 74 20 61 73 20 69 74 20 77 6f 75 , just as it wou
30740 6c 64 20 62 65 20 77 69 74 68 20 61 6e 79 20 6f ld be with any o
30750 74 68 65 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a ther rollback..*
30760 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 *.** For the pur
30770 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 41 50 poses of this AP
30780 49 2c 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e I, a transaction
30790 20 69 73 20 73 61 69 64 20 74 6f 20 68 61 76 65 is said to have
307a0 20 62 65 65 6e 0a 2a 2a 20 72 6f 6c 6c 65 64 20 been.** rolled
307b0 62 61 63 6b 20 69 66 20 61 6e 20 65 78 70 6c 69 back if an expli
307c0 63 69 74 20 22 52 4f 4c 4c 42 41 43 4b 22 20 73 cit "ROLLBACK" s
307d0 74 61 74 65 6d 65 6e 74 20 69 73 20 65 78 65 63 tatement is exec
307e0 75 74 65 64 2c 20 6f 72 0a 2a 2a 20 61 6e 20 65 uted, or.** an e
307f0 72 72 6f 72 20 6f 72 20 63 6f 6e 73 74 72 61 69 rror or constrai
30800 6e 74 20 63 61 75 73 65 73 20 61 6e 20 69 6d 70 nt causes an imp
30810 6c 69 63 69 74 20 72 6f 6c 6c 62 61 63 6b 20 74 licit rollback t
30820 6f 20 6f 63 63 75 72 2e 0a 2a 2a 20 54 68 65 20 o occur..** The
30830 72 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 rollback callbac
30840 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 k is not invoked
30850 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f if a transactio
30860 6e 20 69 73 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 n is.** automati
30870 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 cally rolled bac
30880 6b 20 62 65 63 61 75 73 65 20 74 68 65 20 64 61 k because the da
30890 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
308a0 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 20 n is closed..**
308b0 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6c The rollback cal
308c0 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 69 6e 76 lback is not inv
308d0 6f 6b 65 64 20 69 66 20 61 20 74 72 61 6e 73 61 oked if a transa
308e0 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 72 6f 6c 6c ction is.** roll
308f0 65 64 20 62 61 63 6b 20 62 65 63 61 75 73 65 20 ed back because
30900 61 20 63 6f 6d 6d 69 74 20 63 61 6c 6c 62 61 63 a commit callbac
30910 6b 20 72 65 74 75 72 6e 65 64 20 6e 6f 6e 2d 7a k returned non-z
30920 65 72 6f 2e 0a 2a 2a 20 3c 74 6f 64 6f 3e 20 43 ero..** <todo> C
30930 68 65 63 6b 20 6f 6e 20 74 68 69 73 20 3c 2f 74 heck on this </t
30940 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 odo>.**.** See a
30950 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 lso the [sqlite3
30960 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 5d 20 _update_hook()]
30970 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a interface..**.**
30980 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
30990 2a 20 5b 48 31 32 39 35 31 5d 20 5b 48 31 32 39 * [H12951] [H129
309a0 35 32 5d 20 5b 48 31 32 39 35 33 5d 20 5b 48 31 52] [H12953] [H1
309b0 32 39 35 34 5d 20 5b 48 31 32 39 35 35 5d 0a 2a 2954] [H12955].*
309c0 2a 20 5b 48 31 32 39 36 31 5d 20 5b 48 31 32 39 * [H12961] [H129
309d0 36 32 5d 20 5b 48 31 32 39 36 33 5d 20 5b 48 31 62] [H12963] [H1
309e0 32 39 36 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 2964].*/.SQLITE_
309f0 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 API void *sqlite
30a00 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 73 71 3_commit_hook(sq
30a10 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76 lite3*, int(*)(v
30a20 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 53 oid*), void*);.S
30a30 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
30a40 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b sqlite3_rollback
30a50 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20 _hook(sqlite3*,
30a60 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 29 2c void(*)(void *),
30a70 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 void*);../*.**
30a80 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 20 43 CAPI3REF: Data C
30a90 68 61 6e 67 65 20 4e 6f 74 69 66 69 63 61 74 69 hange Notificati
30aa0 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 on Callbacks {H1
30ab0 32 39 37 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2970} <S60400>.*
30ac0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
30ad0 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 20 69 _update_hook() i
30ae0 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 65 nterface registe
30af0 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 rs a callback fu
30b00 6e 63 74 69 6f 6e 0a 2a 2a 20 77 69 74 68 20 74 nction.** with t
30b10 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
30b20 6e 65 63 74 69 6f 6e 5d 20 69 64 65 6e 74 69 66 nection] identif
30b30 69 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 ied by the first
30b40 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 argument.** to
30b50 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 be invoked whene
30b60 76 65 72 20 61 20 72 6f 77 20 69 73 20 75 70 64 ver a row is upd
30b70 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 20 6f ated, inserted o
30b80 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 41 6e r deleted..** An
30b90 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 y callback set b
30ba0 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c y a previous cal
30bb0 6c 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 l to this functi
30bc0 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 on.** for the sa
30bd0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
30be0 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 ection is overri
30bf0 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 dden..**.** The
30c00 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
30c10 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
30c20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 the function to
30c30 69 6e 76 6f 6b 65 20 77 68 65 6e 20 61 0a 2a 2a invoke when a.**
30c40 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c row is updated,
30c50 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c inserted or del
30c60 65 74 65 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72 eted..** The fir
30c70 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 st argument to t
30c80 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 he callback is a
30c90 20 63 6f 70 79 20 6f 66 20 74 68 65 20 74 68 69 copy of the thi
30ca0 72 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 rd argument.** t
30cb0 6f 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 o sqlite3_update
30cc0 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a 20 54 68 65 20 _hook()..** The
30cd0 73 65 63 6f 6e 64 20 63 61 6c 6c 62 61 63 6b 20 second callback
30ce0 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e 65 20 argument is one
30cf0 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 53 45 52 of [SQLITE_INSER
30d00 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44 45 4c 45 T], [SQLITE_DELE
30d10 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 TE],.** or [SQLI
30d20 54 45 5f 55 50 44 41 54 45 5d 2c 20 64 65 70 65 TE_UPDATE], depe
30d30 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70 65 nding on the ope
30d40 72 61 74 69 6f 6e 20 74 68 61 74 20 63 61 75 73 ration that caus
30d50 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a ed the callback.
30d60 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 ** to be invoked
30d70 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 ..** The third a
30d80 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 nd fourth argume
30d90 6e 74 73 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 nts to the callb
30da0 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e ack contain poin
30db0 74 65 72 73 20 74 6f 20 74 68 65 0a 2a 2a 20 64 ters to the.** d
30dc0 61 74 61 62 61 73 65 20 61 6e 64 20 74 61 62 6c atabase and tabl
30dd0 65 20 6e 61 6d 65 20 63 6f 6e 74 61 69 6e 69 6e e name containin
30de0 67 20 74 68 65 20 61 66 66 65 63 74 65 64 20 72 g the affected r
30df0 6f 77 2e 0a 2a 2a 20 54 68 65 20 66 69 6e 61 6c ow..** The final
30e00 20 63 61 6c 6c 62 61 63 6b 20 70 61 72 61 6d 65 callback parame
30e10 74 65 72 20 69 73 20 74 68 65 20 5b 72 6f 77 69 ter is the [rowi
30e20 64 5d 20 6f 66 20 74 68 65 20 72 6f 77 2e 0a 2a d] of the row..*
30e30 2a 20 49 6e 20 74 68 65 20 63 61 73 65 20 6f 66 * In the case of
30e40 20 61 6e 20 75 70 64 61 74 65 2c 20 74 68 69 73 an update, this
30e50 20 69 73 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 is the [rowid]
30e60 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74 65 after the update
30e70 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a 2a 2a takes place..**
30e80 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 20 68 .** The update h
30e90 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b ook is not invok
30ea0 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e 61 6c ed when internal
30eb0 20 73 79 73 74 65 6d 20 74 61 62 6c 65 73 20 61 system tables a
30ec0 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 20 28 re.** modified (
30ed0 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61 73 74 i.e. sqlite_mast
30ee0 65 72 20 61 6e 64 20 73 71 6c 69 74 65 5f 73 65 er and sqlite_se
30ef0 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 quence)..**.** I
30f00 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 69 6d n the current im
30f10 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74 68 plementation, th
30f20 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 0a 2a 2a e update hook.**
30f30 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 is not invoked
30f40 77 68 65 6e 20 64 75 70 6c 69 63 61 74 69 6f 6e when duplication
30f50 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74 65 rows are delete
30f60 64 20 62 65 63 61 75 73 65 20 6f 66 20 61 6e 0a d because of an.
30f70 2a 2a 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 ** [ON CONFLICT
30f80 7c 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 52 45 | ON CONFLICT RE
30f90 50 4c 41 43 45 5d 20 63 6c 61 75 73 65 2e 20 20 PLACE] clause.
30fa0 4e 6f 72 20 69 73 20 74 68 65 20 75 70 64 61 74 Nor is the updat
30fb0 65 20 68 6f 6f 6b 0a 2a 2a 20 69 6e 76 6f 6b 65 e hook.** invoke
30fc0 64 20 77 68 65 6e 20 72 6f 77 73 20 61 72 65 20 d when rows are
30fd0 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20 74 68 deleted using th
30fe0 65 20 5b 74 72 75 6e 63 61 74 65 20 6f 70 74 69 e [truncate opti
30ff0 6d 69 7a 61 74 69 6f 6e 5d 2e 0a 2a 2a 20 54 68 mization]..** Th
31000 65 20 65 78 63 65 70 74 69 6f 6e 73 20 64 65 66 e exceptions def
31010 69 6e 65 64 20 69 6e 20 74 68 69 73 20 70 61 72 ined in this par
31020 61 67 72 61 70 68 20 6d 69 67 68 74 20 63 68 61 agraph might cha
31030 6e 67 65 20 69 6e 20 61 20 66 75 74 75 72 65 0a nge in a future.
31040 2a 2a 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51 ** release of SQ
31050 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 Lite..**.** The
31060 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 6d 70 6c update hook impl
31070 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 73 74 20 ementation must
31080 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 not do anything
31090 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79 that will modify
310a0 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
310b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 connection that
310c0 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 75 70 64 invoked the upd
310d0 61 74 65 20 68 6f 6f 6b 2e 20 20 41 6e 79 20 61 ate hook. Any a
310e0 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d 6f 64 ctions.** to mod
310f0 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73 65 ify the database
31100 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 connection must
31110 20 62 65 20 64 65 66 65 72 72 65 64 20 75 6e 74 be deferred unt
31120 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a 2a 20 il after the.**
31130 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 74 68 completion of th
31140 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 e [sqlite3_step(
31150 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 )] call that tri
31160 67 67 65 72 65 64 20 74 68 65 20 75 70 64 61 74 ggered the updat
31170 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 e hook..** Note
31180 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 that [sqlite3_pr
31190 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 epare_v2()] and
311a0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
311b0 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 both modify the
311c0 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 ir.** database c
311d0 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 onnections for t
311e0 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d he meaning of "m
311f0 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 odify" in this p
31200 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 aragraph..**.**
31210 49 66 20 61 6e 6f 74 68 65 72 20 66 75 6e 63 74 If another funct
31220 69 6f 6e 20 77 61 73 20 70 72 65 76 69 6f 75 73 ion was previous
31230 6c 79 20 72 65 67 69 73 74 65 72 65 64 2c 20 69 ly registered, i
31240 74 73 20 70 41 72 67 20 76 61 6c 75 65 0a 2a 2a ts pArg value.**
31250 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f is returned. O
31260 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 20 69 73 therwise NULL is
31270 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
31280 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 73 See also the [s
31290 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f qlite3_commit_ho
312a0 6f 6b 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 ok()] and [sqlit
312b0 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b e3_rollback_hook
312c0 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 ()].** interface
312d0 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 s..**.** Require
312e0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 37 ments:.** [H1297
312f0 31 5d 20 5b 48 31 32 39 37 33 5d 20 5b 48 31 32 1] [H12973] [H12
31300 39 37 35 5d 20 5b 48 31 32 39 37 37 5d 20 5b 48 975] [H12977] [H
31310 31 32 39 37 39 5d 20 5b 48 31 32 39 38 31 5d 20 12979] [H12981]
31320 5b 48 31 32 39 38 33 5d 20 5b 48 31 32 39 38 36 [H12983] [H12986
31330 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
31340 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 70 void *sqlite3_up
31350 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c date_hook(. sql
31360 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 28 2a ite3*, . void(*
31370 29 28 76 6f 69 64 20 2a 2c 69 6e 74 20 2c 63 68 )(void *,int ,ch
31380 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 61 72 20 ar const *,char
31390 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 65 33 5f const *,sqlite3_
313a0 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 2a 0a int64),. void*.
313b0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
313c0 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20 44 69 EF: Enable Or Di
313d0 73 61 62 6c 65 20 53 68 61 72 65 64 20 50 61 67 sable Shared Pag
313e0 65 72 20 43 61 63 68 65 20 7b 48 31 30 33 33 30 er Cache {H10330
313f0 7d 20 3c 53 33 30 39 30 30 3e 0a 2a 2a 20 4b 45 } <S30900>.** KE
31400 59 57 4f 52 44 53 3a 20 7b 73 68 61 72 65 64 20 YWORDS: {shared
31410 63 61 63 68 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 cache}.**.** Thi
31420 73 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 s routine enable
31430 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 s or disables th
31440 65 20 73 68 61 72 69 6e 67 20 6f 66 20 74 68 65 e sharing of the
31450 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65 0a database cache.
31460 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61 20 64 61 ** and schema da
31470 74 61 20 73 74 72 75 63 74 75 72 65 73 20 62 65 ta structures be
31480 74 77 65 65 6e 20 5b 64 61 74 61 62 61 73 65 20 tween [database
31490 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c 20 63 6f 6e connection | con
314a0 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a 20 74 6f 20 nections].** to
314b0 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
314c0 65 2e 20 53 68 61 72 69 6e 67 20 69 73 20 65 6e e. Sharing is en
314d0 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 72 67 abled if the arg
314e0 75 6d 65 6e 74 20 69 73 20 74 72 75 65 0a 2a 2a ument is true.**
314f0 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 69 66 and disabled if
31500 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 the argument is
31510 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 43 61 false..**.** Ca
31520 63 68 65 20 73 68 61 72 69 6e 67 20 69 73 20 65 che sharing is e
31530 6e 61 62 6c 65 64 20 61 6e 64 20 64 69 73 61 62 nabled and disab
31540 6c 65 64 20 66 6f 72 20 61 6e 20 65 6e 74 69 72 led for an entir
31550 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 20 54 68 e process..** Th
31560 69 73 20 69 73 20 61 20 63 68 61 6e 67 65 20 61 is is a change a
31570 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65 72 73 s of SQLite vers
31580 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e 20 70 72 ion 3.5.0. In pr
31590 69 6f 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 ior versions of
315a0 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68 61 72 69 SQLite,.** shari
315b0 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 64 20 6f ng was enabled o
315c0 72 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 65 r disabled for e
315d0 61 63 68 20 74 68 72 65 61 64 20 73 65 70 61 72 ach thread separ
315e0 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ately..**.** The
315f0 20 63 61 63 68 65 20 73 68 61 72 69 6e 67 20 6d cache sharing m
31600 6f 64 65 20 73 65 74 20 62 79 20 74 68 69 73 20 ode set by this
31610 69 6e 74 65 72 66 61 63 65 20 65 66 66 65 63 74 interface effect
31620 73 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 s all subsequent
31630 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 .** calls to [sq
31640 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b lite3_open()], [
31650 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
31660 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 )], and [sqlite3
31670 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 20 45 _open16()]..** E
31680 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 65 xisting database
31690 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 6f 6e connections con
316a0 74 69 6e 75 65 20 75 73 65 20 74 68 65 20 73 68 tinue use the sh
316b0 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20 74 68 aring mode.** th
316c0 61 74 20 77 61 73 20 69 6e 20 65 66 66 65 63 74 at was in effect
316d0 20 61 74 20 74 68 65 20 74 69 6d 65 20 74 68 65 at the time the
316e0 79 20 77 65 72 65 20 6f 70 65 6e 65 64 2e 0a 2a y were opened..*
316f0 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 62 *.** Virtual tab
31700 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 les cannot be us
31710 65 64 20 77 69 74 68 20 61 20 73 68 61 72 65 64 ed with a shared
31720 20 63 61 63 68 65 2e 20 20 57 68 65 6e 20 73 68 cache. When sh
31730 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 73 ared.** cache is
31740 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 5b 73 enabled, the [s
31750 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f qlite3_create_mo
31760 64 75 6c 65 28 29 5d 20 41 50 49 20 75 73 65 64 dule()] API used
31770 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 to register.**
31780 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 77 virtual tables w
31790 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75 72 ill always retur
317a0 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a n an error..**.*
317b0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
317c0 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f eturns [SQLITE_O
317d0 4b 5d 20 69 66 20 73 68 61 72 65 64 20 63 61 63 K] if shared cac
317e0 68 65 20 77 61 73 20 65 6e 61 62 6c 65 64 20 6f he was enabled o
317f0 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 73 75 r disabled.** su
31800 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 41 6e 20 ccessfully. An
31810 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 [error code] is
31820 72 65 74 75 72 6e 65 64 20 6f 74 68 65 72 77 69 returned otherwi
31830 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 64 se..**.** Shared
31840 20 63 61 63 68 65 20 69 73 20 64 69 73 61 62 6c cache is disabl
31850 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20 42 ed by default. B
31860 75 74 20 74 68 69 73 20 6d 69 67 68 74 20 63 68 ut this might ch
31870 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75 74 75 72 ange in.** futur
31880 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 e releases of SQ
31890 4c 69 74 65 2e 20 20 41 70 70 6c 69 63 61 74 69 Lite. Applicati
318a0 6f 6e 73 20 74 68 61 74 20 63 61 72 65 20 61 62 ons that care ab
318b0 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a 20 63 61 out shared.** ca
318c0 63 68 65 20 73 65 74 74 69 6e 67 20 73 68 6f 75 che setting shou
318d0 6c 64 20 73 65 74 20 69 74 20 65 78 70 6c 69 63 ld set it explic
318e0 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 itly..**.** See
318f0 41 6c 73 6f 3a 20 20 5b 53 51 4c 69 74 65 20 53 Also: [SQLite S
31900 68 61 72 65 64 2d 43 61 63 68 65 20 4d 6f 64 65 hared-Cache Mode
31910 5d 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ].**.** Requirem
31920 65 6e 74 73 3a 20 5b 48 31 30 33 33 31 5d 20 5b ents: [H10331] [
31930 48 31 30 33 33 36 5d 20 5b 48 31 30 33 33 37 5d H10336] [H10337]
31940 20 5b 48 31 30 33 33 39 5d 0a 2a 2f 0a 53 51 4c [H10339].*/.SQL
31950 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
31960 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 te3_enable_share
31970 64 5f 63 61 63 68 65 28 69 6e 74 29 3b 0a 0a 2f d_cache(int);../
31980 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 *.** CAPI3REF: A
31990 74 74 65 6d 70 74 20 54 6f 20 46 72 65 65 20 48 ttempt To Free H
319a0 65 61 70 20 4d 65 6d 6f 72 79 20 7b 48 31 37 33 eap Memory {H173
319b0 34 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 40} <S30220>.**.
319c0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 ** The sqlite3_r
319d0 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 elease_memory()
319e0 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 interface attemp
319f0 74 73 20 74 6f 20 66 72 65 65 20 4e 20 62 79 74 ts to free N byt
31a00 65 73 0a 2a 2a 20 6f 66 20 68 65 61 70 20 6d 65 es.** of heap me
31a10 6d 6f 72 79 20 62 79 20 64 65 61 6c 6c 6f 63 61 mory by dealloca
31a20 74 69 6e 67 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 ting non-essenti
31a30 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 al memory alloca
31a40 74 69 6f 6e 73 0a 2a 2a 20 68 65 6c 64 20 62 79 tions.** held by
31a50 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c 69 the database li
31a60 62 72 61 72 79 2e 20 7b 45 4e 44 7d 20 20 4d 65 brary. {END} Me
31a70 6d 6f 72 79 20 75 73 65 64 20 74 6f 20 63 61 63 mory used to cac
31a80 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 70 he database.** p
31a90 61 67 65 73 20 74 6f 20 69 6d 70 72 6f 76 65 20 ages to improve
31aa0 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 73 20 61 performance is a
31ab0 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e 6f 6e n example of non
31ac0 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 -essential memor
31ad0 79 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 y..** sqlite3_re
31ae0 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 72 lease_memory() r
31af0 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
31b00 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 r of bytes actua
31b10 6c 6c 79 20 66 72 65 65 64 2c 0a 2a 2a 20 77 68 lly freed,.** wh
31b20 69 63 68 20 6d 69 67 68 74 20 62 65 20 6d 6f 72 ich might be mor
31b30 65 20 6f 72 20 6c 65 73 73 20 74 68 61 6e 20 74 e or less than t
31b40 68 65 20 61 6d 6f 75 6e 74 20 72 65 71 75 65 73 he amount reques
31b50 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ted..**.** Requi
31b60 72 65 6d 65 6e 74 73 3a 20 5b 48 31 37 33 34 31 rements: [H17341
31b70 5d 20 5b 48 31 37 33 34 32 5d 0a 2a 2f 0a 53 51 ] [H17342].*/.SQ
31b80 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
31b90 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d ite3_release_mem
31ba0 6f 72 79 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a ory(int);../*.**
31bb0 20 43 41 50 49 33 52 45 46 3a 20 49 6d 70 6f 73 CAPI3REF: Impos
31bc0 65 20 41 20 4c 69 6d 69 74 20 4f 6e 20 48 65 61 e A Limit On Hea
31bd0 70 20 53 69 7a 65 20 7b 48 31 37 33 35 30 7d 20 p Size {H17350}
31be0 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S30220>.**.** T
31bf0 68 65 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f he sqlite3_soft_
31c00 68 65 61 70 5f 6c 69 6d 69 74 28 29 20 69 6e 74 heap_limit() int
31c10 65 72 66 61 63 65 20 70 6c 61 63 65 73 20 61 20 erface places a
31c20 22 73 6f 66 74 22 20 6c 69 6d 69 74 0a 2a 2a 20 "soft" limit.**
31c30 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 on the amount of
31c40 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 74 68 61 heap memory tha
31c50 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61 74 t may be allocat
31c60 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a ed by SQLite..**
31c70 20 49 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 If an internal
31c80 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 72 65 allocation is re
31c90 71 75 65 73 74 65 64 20 74 68 61 74 20 77 6f 75 quested that wou
31ca0 6c 64 20 65 78 63 65 65 64 20 74 68 65 0a 2a 2a ld exceed the.**
31cb0 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 soft heap limit
31cc0 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 , [sqlite3_relea
31cd0 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 69 73 20 se_memory()] is
31ce0 69 6e 76 6f 6b 65 64 20 6f 6e 65 20 6f 72 0a 2a invoked one or.*
31cf0 2a 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 * more times to
31d00 66 72 65 65 20 75 70 20 73 6f 6d 65 20 73 70 61 free up some spa
31d10 63 65 20 62 65 66 6f 72 65 20 74 68 65 20 61 6c ce before the al
31d20 6c 6f 63 61 74 69 6f 6e 20 69 73 20 70 65 72 66 location is perf
31d30 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ormed..**.** The
31d40 20 6c 69 6d 69 74 20 69 73 20 63 61 6c 6c 65 64 limit is called
31d50 20 22 73 6f 66 74 22 2c 20 62 65 63 61 75 73 65 "soft", because
31d60 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c if [sqlite3_rel
31d70 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 0a 2a ease_memory()].*
31d80 2a 20 63 61 6e 6e 6f 74 20 66 72 65 65 20 73 75 * cannot free su
31d90 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72 79 20 fficient memory
31da0 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 20 6c to prevent the l
31db0 69 6d 69 74 20 66 72 6f 6d 20 62 65 69 6e 67 20 imit from being
31dc0 65 78 63 65 65 64 65 64 2c 0a 2a 2a 20 74 68 65 exceeded,.** the
31dd0 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 memory is alloc
31de0 61 74 65 64 20 61 6e 79 77 61 79 20 61 6e 64 20 ated anyway and
31df0 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70 65 72 the current oper
31e00 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73 2e 0a ation proceeds..
31e10 2a 2a 0a 2a 2a 20 41 20 6e 65 67 61 74 69 76 65 **.** A negative
31e20 20 6f 72 20 7a 65 72 6f 20 76 61 6c 75 65 20 66 or zero value f
31e30 6f 72 20 4e 20 6d 65 61 6e 73 20 74 68 61 74 20 or N means that
31e40 74 68 65 72 65 20 69 73 20 6e 6f 20 73 6f 66 74 there is no soft
31e50 20 68 65 61 70 20 6c 69 6d 69 74 20 61 6e 64 0a heap limit and.
31e60 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 ** [sqlite3_rele
31e70 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 77 69 ase_memory()] wi
31e80 6c 6c 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 ll only be calle
31e90 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 69 73 d when memory is
31ea0 20 65 78 68 61 75 73 74 65 64 2e 0a 2a 2a 20 54 exhausted..** T
31eb0 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 he default value
31ec0 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 68 65 for the soft he
31ed0 61 70 20 6c 69 6d 69 74 20 69 73 20 7a 65 72 6f ap limit is zero
31ee0 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6d ..**.** SQLite m
31ef0 61 6b 65 73 20 61 20 62 65 73 74 20 65 66 66 6f akes a best effo
31f00 72 74 20 74 6f 20 68 6f 6e 6f 72 20 74 68 65 20 rt to honor the
31f10 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 2e soft heap limit.
31f20 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65 20 73 .** But if the s
31f30 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 63 oft heap limit c
31f40 61 6e 6e 6f 74 20 62 65 20 68 6f 6e 6f 72 65 64 annot be honored
31f50 2c 20 65 78 65 63 75 74 69 6f 6e 20 77 69 6c 6c , execution will
31f60 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 .** continue wit
31f70 68 6f 75 74 20 65 72 72 6f 72 20 6f 72 20 6e 6f hout error or no
31f80 74 69 66 69 63 61 74 69 6f 6e 2e 20 20 54 68 69 tification. Thi
31f90 73 20 69 73 20 77 68 79 20 74 68 65 20 6c 69 6d s is why the lim
31fa0 69 74 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 it is.** called
31fb0 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 2e 20 a "soft" limit.
31fc0 20 49 74 20 69 73 20 61 64 76 69 73 6f 72 79 20 It is advisory
31fd0 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 72 69 6f only..**.** Prio
31fe0 72 20 74 6f 20 53 51 4c 69 74 65 20 76 65 72 73 r to SQLite vers
31ff0 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 69 73 20 ion 3.5.0, this
32000 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f 6e routine only con
32010 73 74 72 61 69 6e 65 64 20 74 68 65 20 6d 65 6d strained the mem
32020 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 ory.** allocated
32030 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74 68 72 by a single thr
32040 65 61 64 20 2d 20 74 68 65 20 73 61 6d 65 20 74 ead - the same t
32050 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 20 74 hread in which t
32060 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 his routine.** r
32070 75 6e 73 2e 20 20 42 65 67 69 6e 6e 69 6e 67 20 uns. Beginning
32080 77 69 74 68 20 53 51 4c 69 74 65 20 76 65 72 73 with SQLite vers
32090 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 65 20 73 ion 3.5.0, the s
320a0 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 69 oft heap limit i
320b0 73 0a 2a 2a 20 61 70 70 6c 69 65 64 20 74 6f 20 s.** applied to
320c0 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 54 68 65 all threads. The
320d0 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65 64 value specified
320e0 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 68 65 for the soft he
320f0 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 69 73 20 61 ap limit.** is a
32100 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e n upper bound on
32110 20 74 68 65 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 the total memor
32120 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 6f 72 y allocation for
32130 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 49 6e all threads. In
32140 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e .** version 3.5.
32150 30 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 65 0 there is no me
32160 63 68 61 6e 69 73 6d 20 66 6f 72 20 6c 69 6d 69 chanism for limi
32170 74 69 6e 67 20 74 68 65 20 68 65 61 70 20 75 73 ting the heap us
32180 61 67 65 20 66 6f 72 0a 2a 2a 20 69 6e 64 69 76 age for.** indiv
32190 69 64 75 61 6c 20 74 68 72 65 61 64 73 2e 0a 2a idual threads..*
321a0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
321b0 73 3a 0a 2a 2a 20 5b 48 31 36 33 35 31 5d 20 5b s:.** [H16351] [
321c0 48 31 36 33 35 32 5d 20 5b 48 31 36 33 35 33 5d H16352] [H16353]
321d0 20 5b 48 31 36 33 35 34 5d 20 5b 48 31 36 33 35 [H16354] [H1635
321e0 35 5d 20 5b 48 31 36 33 35 38 5d 0a 2a 2f 0a 53 5] [H16358].*/.S
321f0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
32200 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 qlite3_soft_heap
32210 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a 2f 2a _limit(int);../*
32220 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 78 .** CAPI3REF: Ex
32230 74 72 61 63 74 20 4d 65 74 61 64 61 74 61 20 41 tract Metadata A
32240 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20 4f 66 bout A Column Of
32250 20 41 20 54 61 62 6c 65 20 7b 48 31 32 38 35 30 A Table {H12850
32260 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a 0a 2a 2a } <S60300>.**.**
32270 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 This routine re
32280 74 75 72 6e 73 20 6d 65 74 61 64 61 74 61 20 61 turns metadata a
32290 62 6f 75 74 20 61 20 73 70 65 63 69 66 69 63 20 bout a specific
322a0 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 70 65 63 column of a spec
322b0 69 66 69 63 0a 2a 2a 20 64 61 74 61 62 61 73 65 ific.** database
322c0 20 74 61 62 6c 65 20 61 63 63 65 73 73 69 62 6c table accessibl
322d0 65 20 75 73 69 6e 67 20 74 68 65 20 5b 64 61 74 e using the [dat
322e0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
322f0 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70 61 73 73 ] handle.** pass
32300 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ed as the first
32310 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e function argumen
32320 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c t..**.** The col
32330 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66 69 65 umn is identifie
32340 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 2c d by the second,
32350 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 third and fourt
32360 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 0a h parameters to.
32370 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ** this function
32380 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 . The second par
32390 61 6d 65 74 65 72 20 69 73 20 65 69 74 68 65 72 ameter is either
323a0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
323b0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28 69 2e database.** (i.
323c0 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70 e. "main", "temp
323d0 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68 65 64 " or an attached
323e0 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e 74 61 database) conta
323f0 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63 69 66 ining the specif
32400 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 ied.** table or
32410 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 20 4e NULL. If it is N
32420 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 61 74 ULL, then all at
32430 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 tached databases
32440 20 61 72 65 20 73 65 61 72 63 68 65 64 0a 2a 2a are searched.**
32450 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 75 for the table u
32460 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 61 6c sing the same al
32470 67 6f 72 69 74 68 6d 20 75 73 65 64 20 62 79 20 gorithm used by
32480 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 67 the database eng
32490 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f 6c 76 ine to.** resolv
324a0 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61 e unqualified ta
324b0 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73 2e 0a ble references..
324c0 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 **.** The third
324d0 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d and fourth param
324e0 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 75 eters to this fu
324f0 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 74 nction are the t
32500 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a able and column.
32510 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 ** name of the d
32520 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c 20 72 esired column, r
32530 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e 65 69 espectively. Nei
32540 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 70 61 ther of these pa
32550 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61 79 20 rameters.** may
32560 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4d be NULL..**.** M
32570 65 74 61 64 61 74 61 20 69 73 20 72 65 74 75 72 etadata is retur
32580 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67 20 74 ned by writing t
32590 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 o the memory loc
325a0 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20 61 73 ations passed as
325b0 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e 64 20 the 5th.** and
325c0 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61 6d subsequent param
325d0 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 75 eters to this fu
325e0 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f 66 20 74 nction. Any of t
325f0 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73 20 6d hese arguments m
32600 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c 20 69 ay be.** NULL, i
32610 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 n which case the
32620 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 65 corresponding e
32630 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61 64 61 lement of metada
32640 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a ta is omitted..*
32650 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 *.** <blockquote
32660 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 >.** <table bord
32670 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c er="1">.** <tr><
32680 74 68 3e 20 50 61 72 61 6d 65 74 65 72 20 3c 74 th> Parameter <t
32690 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e 54 79 70 h> Output<br>Typ
326a0 65 20 3c 74 68 3e 20 20 44 65 73 63 72 69 70 74 e <th> Descript
326b0 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74 ion.**.** <tr><t
326c0 64 3e 20 35 74 68 20 3c 74 64 3e 20 63 6f 6e 73 d> 5th <td> cons
326d0 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 44 61 74 t char* <td> Dat
326e0 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72 3e 3c 74 a type.** <tr><t
326f0 64 3e 20 36 74 68 20 3c 74 64 3e 20 63 6f 6e 73 d> 6th <td> cons
32700 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 4e 61 6d t char* <td> Nam
32710 65 20 6f 66 20 64 65 66 61 75 6c 74 20 63 6f 6c e of default col
32720 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a lation sequence.
32730 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37 74 68 20 ** <tr><td> 7th
32740 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 <td> int
32750 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f <td> True if co
32760 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f 54 20 4e lumn has a NOT N
32770 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a ULL constraint.*
32780 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74 68 20 3c * <tr><td> 8th <
32790 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 20 td> int
327a0 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f 6c <td> True if col
327b0 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20 74 umn is part of t
327c0 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 0a 2a he PRIMARY KEY.*
327d0 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74 68 20 3c * <tr><td> 9th <
327e0 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 20 td> int
327f0 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f 6c <td> True if col
32800 75 6d 6e 20 69 73 20 5b 41 55 54 4f 49 4e 43 52 umn is [AUTOINCR
32810 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f 74 61 62 6c EMENT].** </tabl
32820 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f e>.** </blockquo
32830 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 te>.**.** The me
32840 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 mory pointed to
32850 62 79 20 74 68 65 20 63 68 61 72 61 63 74 65 72 by the character
32860 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e pointers return
32870 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 65 ed for the.** de
32880 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 61 claration type a
32890 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 nd collation seq
328a0 75 65 6e 63 65 20 69 73 20 76 61 6c 69 64 20 6f uence is valid o
328b0 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 nly until the ne
328c0 78 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 61 6e xt.** call to an
328d0 79 20 53 51 4c 69 74 65 20 41 50 49 20 66 75 6e y SQLite API fun
328e0 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ction..**.** If
328f0 74 68 65 20 73 70 65 63 69 66 69 65 64 20 74 61 the specified ta
32900 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20 ble is actually
32910 61 20 76 69 65 77 2c 20 61 6e 20 5b 65 72 72 6f a view, an [erro
32920 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 r code] is retur
32930 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ned..**.** If th
32940 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 75 e specified colu
32950 6d 6e 20 69 73 20 22 72 6f 77 69 64 22 2c 20 22 mn is "rowid", "
32960 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 64 5f oid" or "_rowid_
32970 22 20 61 6e 64 20 61 6e 0a 2a 2a 20 5b 49 4e 54 " and an.** [INT
32980 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY
32990 5d 20 63 6f 6c 75 6d 6e 20 68 61 73 20 62 65 65 ] column has bee
329a0 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63 n explicitly dec
329b0 6c 61 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20 lared, then the
329c0 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65 output.** parame
329d0 74 65 72 73 20 61 72 65 20 73 65 74 20 66 6f 72 ters are set for
329e0 20 74 68 65 20 65 78 70 6c 69 63 69 74 6c 79 20 the explicitly
329f0 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 2e declared column.
32a00 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 0a If there is no.
32a10 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 ** explicitly de
32a20 63 6c 61 72 65 64 20 5b 49 4e 54 45 47 45 52 20 clared [INTEGER
32a30 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 63 6f 6c PRIMARY KEY] col
32a40 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20 6f 75 umn, then the ou
32a50 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 tput.** paramete
32a60 72 73 20 61 72 65 20 73 65 74 20 61 73 20 66 6f rs are set as fo
32a70 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 llows:.**.** <pr
32a80 65 3e 0a 2a 2a 20 20 20 20 20 64 61 74 61 20 74 e>.** data t
32a90 79 70 65 3a 20 22 49 4e 54 45 47 45 52 22 0a 2a ype: "INTEGER".*
32aa0 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69 6f 6e 20 * collation
32ab0 73 65 71 75 65 6e 63 65 3a 20 22 42 49 4e 41 52 sequence: "BINAR
32ac0 59 22 0a 2a 2a 20 20 20 20 20 6e 6f 74 20 6e 75 Y".** not nu
32ad0 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 20 70 72 69 ll: 0.** pri
32ae0 6d 61 72 79 20 6b 65 79 3a 20 31 0a 2a 2a 20 20 mary key: 1.**
32af0 20 20 20 61 75 74 6f 20 69 6e 63 72 65 6d 65 6e auto incremen
32b00 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 65 3e 0a 2a t: 0.** </pre>.*
32b10 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
32b20 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 6f 6e 65 20 on may load one
32b30 6f 72 20 6d 6f 72 65 20 73 63 68 65 6d 61 73 20 or more schemas
32b40 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 66 69 from database fi
32b50 6c 65 73 2e 20 49 66 20 61 6e 0a 2a 2a 20 65 72 les. If an.** er
32b60 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e ror occurs durin
32b70 67 20 74 68 69 73 20 70 72 6f 63 65 73 73 2c 20 g this process,
32b80 6f 72 20 69 66 20 74 68 65 20 72 65 71 75 65 73 or if the reques
32b90 74 65 64 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c ted table or col
32ba0 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 umn.** cannot be
32bb0 20 66 6f 75 6e 64 2c 20 61 6e 20 5b 65 72 72 6f found, an [erro
32bc0 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 r code] is retur
32bd0 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 ned and an error
32be0 20 6d 65 73 73 61 67 65 20 6c 65 66 74 0a 2a 2a message left.**
32bf0 20 69 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 in the [databas
32c00 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 e connection] (t
32c10 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 20 75 o be retrieved u
32c20 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 65 72 72 sing sqlite3_err
32c30 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 msg())..**.** Th
32c40 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20 61 is API is only a
32c50 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20 vailable if the
32c60 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 library was comp
32c70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a iled with the.**
32c80 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f [SQLITE_ENABLE_
32c90 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 5d COLUMN_METADATA]
32ca0 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20 C-preprocessor
32cb0 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a symbol defined..
32cc0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
32cd0 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f t sqlite3_table_
32ce0 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 28 column_metadata(
32cf0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
32d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
32d10 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e * Connection han
32d20 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 dle */. const c
32d30 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 20 20 har *zDbName,
32d40 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
32d50 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f name or NULL */
32d60 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
32d70 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20 2f TableName, /
32d80 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a * Table name */.
32d90 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 const char *zC
32da0 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20 2f 2a olumnName, /*
32db0 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a Column name */.
32dc0 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70 char const **p
32dd0 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20 2f 2a zDataType, /*
32de0 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61 72 65 OUTPUT: Declare
32df0 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f 0a 20 d data type */.
32e00 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a char const **pz
32e10 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f 2a 20 CollSeq, /*
32e20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74 69 6f OUTPUT: Collatio
32e30 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20 n sequence name
32e40 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74 4e 75 */. int *pNotNu
32e50 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ll,
32e60 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 /* OUTPUT: True
32e70 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e if NOT NULL con
32e80 73 74 72 61 69 6e 74 20 65 78 69 73 74 73 20 2a straint exists *
32e90 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d 61 72 /. int *pPrimar
32ea0 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 yKey,
32eb0 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20 /* OUTPUT: True
32ec0 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74 20 6f if column part o
32ed0 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 f PK */. int *p
32ee0 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20 20 20 Autoinc
32ef0 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a /* OUTPUT:
32f00 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 True if column
32f10 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d 65 6e is auto-incremen
32f20 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 t */.);../*.** C
32f30 41 50 49 33 52 45 46 3a 20 4c 6f 61 64 20 41 6e API3REF: Load An
32f40 20 45 78 74 65 6e 73 69 6f 6e 20 7b 48 31 32 36 Extension {H126
32f50 30 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 00} <S20500>.**.
32f60 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 ** This interfac
32f70 65 20 6c 6f 61 64 73 20 61 6e 20 53 51 4c 69 74 e loads an SQLit
32f80 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62 72 e extension libr
32f90 61 72 79 20 66 72 6f 6d 20 74 68 65 20 6e 61 6d ary from the nam
32fa0 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b ed file..**.** {
32fb0 48 31 32 36 30 31 7d 20 54 68 65 20 73 71 6c 69 H12601} The sqli
32fc0 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 te3_load_extensi
32fd0 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 on() interface a
32fe0 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f 61 64 20 ttempts to load
32ff0 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 53 an.** S
33000 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 QLite extension
33010 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 65 library containe
33020 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 7a 46 d in the file zF
33030 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 ile..**.** {H126
33040 30 32 7d 20 54 68 65 20 65 6e 74 72 79 20 70 6f 02} The entry po
33050 69 6e 74 20 69 73 20 7a 50 72 6f 63 2e 0a 2a 2a int is zProc..**
33060 0a 2a 2a 20 7b 48 31 32 36 30 33 7d 20 7a 50 72 .** {H12603} zPr
33070 6f 63 20 6d 61 79 20 62 65 20 30 2c 20 69 6e 20 oc may be 0, in
33080 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20 6e which case the n
33090 61 6d 65 20 6f 66 20 74 68 65 20 65 6e 74 72 79 ame of the entry
330a0 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 20 20 point.**
330b0 20 20 20 64 65 66 61 75 6c 74 73 20 74 6f 20 22 defaults to "
330c0 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f sqlite3_extensio
330d0 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 0a 2a 2a 20 7b n_init"..**.** {
330e0 48 31 32 36 30 34 7d 20 54 68 65 20 73 71 6c 69 H12604} The sqli
330f0 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 te3_load_extensi
33100 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 on() interface s
33110 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 20 hall return.**
33120 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f [SQLITE_
33130 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 61 OK] on success a
33140 6e 64 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 nd [SQLITE_ERROR
33150 5d 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 ] if something g
33160 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a oes wrong..**.**
33170 20 7b 48 31 32 36 30 35 7d 20 49 66 20 61 6e 20 {H12605} If an
33180 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 64 error occurs and
33190 20 70 7a 45 72 72 4d 73 67 20 69 73 20 6e 6f 74 pzErrMsg is not
331a0 20 30 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 0, then the.**
331b0 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
331c0 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 3_load_extension
331d0 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 73 68 ()] interface sh
331e0 61 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a all attempt to.*
331f0 2a 20 20 20 20 20 20 20 20 20 20 66 69 6c 6c 20 * fill
33200 2a 70 7a 45 72 72 4d 73 67 20 77 69 74 68 20 65 *pzErrMsg with e
33210 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 rror message tex
33220 74 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f t stored in memo
33230 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f ry.** o
33240 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 btained from [sq
33250 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e lite3_malloc()].
33260 20 7b 45 4e 44 7d 20 20 54 68 65 20 63 61 6c 6c {END} The call
33270 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 ing function.**
33280 20 20 20 20 20 20 20 20 20 73 68 6f 75 6c 64 20 should
33290 66 72 65 65 20 74 68 69 73 20 6d 65 6d 6f 72 79 free this memory
332a0 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c by calling [sql
332b0 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a ite3_free()]..**
332c0 0a 2a 2a 20 7b 48 31 32 36 30 36 7d 20 45 78 74 .** {H12606} Ext
332d0 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6d ension loading m
332e0 75 73 74 20 62 65 20 65 6e 61 62 6c 65 64 20 75 ust be enabled u
332f0 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 sing.**
33300 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 [sqlite3_enable
33310 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 _load_extension(
33320 29 5d 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c )] prior to call
33330 69 6e 67 20 74 68 69 73 20 41 50 49 2c 0a 2a 2a ing this API,.**
33340 20 20 20 20 20 20 20 20 20 20 6f 74 68 65 72 77 otherw
33350 69 73 65 20 61 6e 20 65 72 72 6f 72 20 77 69 6c ise an error wil
33360 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a l be returned..*
33370 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
33380 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 sqlite3_load_ex
33390 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 tension(. sqlit
333a0 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
333b0 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 74 /* Load the ext
333c0 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 ension into this
333d0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
333e0 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 tion */. const
333f0 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 char *zFile,
33400 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 /* Name of the s
33410 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 63 6f hared library co
33420 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 ntaining extensi
33430 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 on */. const ch
33440 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a ar *zProc, /*
33450 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 44 Entry point. D
33460 65 72 69 76 65 64 20 66 72 6f 6d 20 7a 46 69 6c erived from zFil
33470 65 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72 e if 0 */. char
33480 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20 **pzErrMsg
33490 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d /* Put error m
334a0 65 73 73 61 67 65 20 68 65 72 65 20 69 66 20 6e essage here if n
334b0 6f 74 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a ot 0 */.);../*.*
334c0 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 * CAPI3REF: Enab
334d0 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 le Or Disable Ex
334e0 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 20 tension Loading
334f0 7b 48 31 32 36 32 30 7d 20 3c 53 32 30 35 30 30 {H12620} <S20500
33500 3e 0a 2a 2a 0a 2a 2a 20 53 6f 20 61 73 20 6e 6f >.**.** So as no
33510 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 72 69 t to open securi
33520 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65 ty holes in olde
33530 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 r applications t
33540 68 61 74 20 61 72 65 0a 2a 2a 20 75 6e 70 72 65 hat are.** unpre
33550 70 61 72 65 64 20 74 6f 20 64 65 61 6c 20 77 69 pared to deal wi
33560 74 68 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 th extension loa
33570 64 69 6e 67 2c 20 61 6e 64 20 61 73 20 61 20 6d ding, and as a m
33580 65 61 6e 73 20 6f 66 20 64 69 73 61 62 6c 69 6e eans of disablin
33590 67 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 20 6c g.** extension l
335a0 6f 61 64 69 6e 67 20 77 68 69 6c 65 20 65 76 61 oading while eva
335b0 6c 75 61 74 69 6e 67 20 75 73 65 72 2d 65 6e 74 luating user-ent
335c0 65 72 65 64 20 53 51 4c 2c 20 74 68 65 20 66 6f ered SQL, the fo
335d0 6c 6c 6f 77 69 6e 67 20 41 50 49 0a 2a 2a 20 69 llowing API.** i
335e0 73 20 70 72 6f 76 69 64 65 64 20 74 6f 20 74 75 s provided to tu
335f0 72 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f rn the [sqlite3_
33600 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 load_extension()
33610 5d 20 6d 65 63 68 61 6e 69 73 6d 20 6f 6e 20 61 ] mechanism on a
33620 6e 64 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20 45 78 nd off..**.** Ex
33630 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 tension loading
33640 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c is off by defaul
33650 74 2e 20 53 65 65 20 74 69 63 6b 65 74 20 23 31 t. See ticket #1
33660 38 36 33 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 863..**.** {H126
33670 32 31 7d 20 43 61 6c 6c 20 74 68 65 20 73 71 6c 21} Call the sql
33680 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 ite3_enable_load
33690 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 72 6f 75 _extension() rou
336a0 74 69 6e 65 20 77 69 74 68 20 6f 6e 6f 66 66 3d tine with onoff=
336b0 3d 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 =1.** t
336c0 6f 20 74 75 72 6e 20 65 78 74 65 6e 73 69 6f 6e o turn extension
336d0 20 6c 6f 61 64 69 6e 67 20 6f 6e 20 61 6e 64 20 loading on and
336e0 63 61 6c 6c 20 69 74 20 77 69 74 68 20 6f 6e 6f call it with ono
336f0 66 66 3d 3d 30 20 74 6f 20 74 75 72 6e 0a 2a 2a ff==0 to turn.**
33700 20 20 20 20 20 20 20 20 20 20 69 74 20 62 61 63 it bac
33710 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a 2a 2a 0a k off again..**.
33720 2a 2a 20 7b 48 31 32 36 32 32 7d 20 45 78 74 65 ** {H12622} Exte
33730 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 nsion loading is
33740 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74 2e off by default.
33750 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
33760 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c nt sqlite3_enabl
33770 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e e_load_extension
33780 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e (sqlite3 *db, in
33790 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a t onoff);../*.**
337a0 20 43 41 50 49 33 52 45 46 3a 20 41 75 74 6f 6d CAPI3REF: Autom
337b0 61 74 69 63 61 6c 6c 79 20 4c 6f 61 64 20 41 6e atically Load An
337c0 20 45 78 74 65 6e 73 69 6f 6e 73 20 7b 48 31 32 Extensions {H12
337d0 36 34 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 640} <S20500>.**
337e0 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 63 61 6e .** This API can
337f0 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 74 20 70 be invoked at p
33800 72 6f 67 72 61 6d 20 73 74 61 72 74 75 70 20 69 rogram startup i
33810 6e 20 6f 72 64 65 72 20 74 6f 20 72 65 67 69 73 n order to regis
33820 74 65 72 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f ter.** one or mo
33830 72 65 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 re statically li
33840 6e 6b 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 nked extensions
33850 74 68 61 74 20 77 69 6c 6c 20 62 65 20 61 76 61 that will be ava
33860 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c ilable.** to all
33870 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 20 63 new [database c
33880 6f 6e 6e 65 63 74 69 6f 6e 73 5d 2e 20 7b 45 4e onnections]. {EN
33890 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f D}.**.** This ro
338a0 75 74 69 6e 65 20 73 74 6f 72 65 73 20 61 20 70 utine stores a p
338b0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65 78 ointer to the ex
338c0 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 61 72 tension in an ar
338d0 72 61 79 20 74 68 61 74 20 69 73 0a 2a 2a 20 6f ray that is.** o
338e0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 btained from [sq
338f0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e lite3_malloc()].
33900 20 20 49 66 20 79 6f 75 20 72 75 6e 20 61 20 6d If you run a m
33910 65 6d 6f 72 79 20 6c 65 61 6b 20 63 68 65 63 6b emory leak check
33920 65 72 0a 2a 2a 20 6f 6e 20 79 6f 75 72 20 70 72 er.** on your pr
33930 6f 67 72 61 6d 20 61 6e 64 20 69 74 20 72 65 70 ogram and it rep
33940 6f 72 74 73 20 61 20 6c 65 61 6b 20 62 65 63 61 orts a leak beca
33950 75 73 65 20 6f 66 20 74 68 69 73 20 61 72 72 61 use of this arra
33960 79 2c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 5b 73 71 y, invoke.** [sq
33970 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f lite3_reset_auto
33980 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72 _extension()] pr
33990 69 6f 72 20 74 6f 20 73 68 75 74 64 6f 77 6e 20 ior to shutdown
339a0 74 6f 20 66 72 65 65 20 74 68 65 20 6d 65 6d 6f to free the memo
339b0 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 ry..**.** {H1264
339c0 31 7d 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 1} This function
339d0 20 72 65 67 69 73 74 65 72 73 20 61 6e 20 65 78 registers an ex
339e0 74 65 6e 73 69 6f 6e 20 65 6e 74 72 79 20 70 6f tension entry po
339f0 69 6e 74 20 74 68 61 74 20 69 73 0a 2a 2a 20 20 int that is.**
33a00 20 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 automati
33a10 63 61 6c 6c 79 20 69 6e 76 6f 6b 65 64 20 77 68 cally invoked wh
33a20 65 6e 65 76 65 72 20 61 20 6e 65 77 20 5b 64 61 enever a new [da
33a30 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
33a40 6e 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 n].** i
33a50 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 5b s opened using [
33a60 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c sqlite3_open()],
33a70 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 [sqlite3_open16
33a80 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 ()],.**
33a90 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 or [sqlite3_ope
33aa0 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b n_v2()]..**.** {
33ab0 48 31 32 36 34 32 7d 20 44 75 70 6c 69 63 61 74 H12642} Duplicat
33ac0 65 20 65 78 74 65 6e 73 69 6f 6e 73 20 61 72 65 e extensions are
33ad0 20 64 65 74 65 63 74 65 64 20 73 6f 20 63 61 6c detected so cal
33ae0 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e ling this routin
33af0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 75 e.** mu
33b00 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74 ltiple times wit
33b10 68 20 74 68 65 20 73 61 6d 65 20 65 78 74 65 6e h the same exten
33b20 73 69 6f 6e 20 69 73 20 68 61 72 6d 6c 65 73 73 sion is harmless
33b30 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 33 7d ..**.** {H12643}
33b40 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 This routine st
33b50 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 ores a pointer t
33b60 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 o the extension
33b70 69 6e 20 61 6e 20 61 72 72 61 79 0a 2a 2a 20 20 in an array.**
33b80 20 20 20 20 20 20 20 20 74 68 61 74 20 69 73 20 that is
33b90 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 obtained from [s
33ba0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
33bb0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 34 7d ..**.** {H12644}
33bc0 20 41 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e Automatic exten
33bd0 73 69 6f 6e 73 20 61 70 70 6c 79 20 61 63 72 6f sions apply acro
33be0 73 73 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 0a ss all threads..
33bf0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
33c00 74 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 t sqlite3_auto_e
33c10 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28 2a xtension(void (*
33c20 78 45 6e 74 72 79 50 6f 69 6e 74 29 28 76 6f 69 xEntryPoint)(voi
33c30 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 d));../*.** CAPI
33c40 33 52 45 46 3a 20 52 65 73 65 74 20 41 75 74 6f 3REF: Reset Auto
33c50 6d 61 74 69 63 20 45 78 74 65 6e 73 69 6f 6e 20 matic Extension
33c60 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 36 30 7d Loading {H12660}
33c70 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20500>.**.**
33c80 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 This function di
33c90 73 61 62 6c 65 73 20 61 6c 6c 20 70 72 65 76 69 sables all previ
33ca0 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 ously registered
33cb0 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 65 78 automatic.** ex
33cc0 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44 7d 20 tensions. {END}
33cd0 20 49 74 20 75 6e 64 6f 65 73 20 74 68 65 20 65 It undoes the e
33ce0 66 66 65 63 74 20 6f 66 20 61 6c 6c 20 70 72 69 ffect of all pri
33cf0 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 61 or.** [sqlite3_a
33d00 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d uto_extension()]
33d10 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 calls..**.** {H
33d20 31 32 36 36 31 7d 20 54 68 69 73 20 66 75 6e 63 12661} This func
33d30 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 61 6c tion disables al
33d40 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 l previously reg
33d50 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 20 20 20 istered.**
33d60 20 20 20 20 61 75 74 6f 6d 61 74 69 63 20 65 78 automatic ex
33d70 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 tensions..**.**
33d80 7b 48 31 32 36 36 32 7d 20 54 68 69 73 20 66 75 {H12662} This fu
33d90 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 nction disables
33da0 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 automatic extens
33db0 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74 68 72 65 ions in all thre
33dc0 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ads..*/.SQLITE_A
33dd0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
33de0 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e reset_auto_exten
33df0 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a sion(void);../*.
33e00 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e ****** EXPERIMEN
33e10 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74 6f TAL - subject to
33e20 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20 change without
33e30 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a notice *********
33e40 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 *****.**.** The
33e50 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 interface to the
33e60 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d virtual-table m
33e70 65 63 68 61 6e 69 73 6d 20 69 73 20 63 75 72 72 echanism is curr
33e80 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 ently considered
33e90 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 .** to be experi
33ea0 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 mental. The int
33eb0 65 72 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 erface might cha
33ec0 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 nge in incompati
33ed0 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 ble ways..** If
33ee0 74 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65 this is a proble
33ef0 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f m for you, do no
33f00 74 20 75 73 65 20 74 68 65 20 69 6e 74 65 72 66 t use the interf
33f10 61 63 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 ace at this time
33f20 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 ..**.** When the
33f30 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d virtual-table m
33f40 65 63 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 echanism stabili
33f50 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 zes, we will dec
33f60 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 lare the.** inte
33f70 72 66 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 rface fixed, sup
33f80 70 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 port it indefini
33f90 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 tely, and remove
33fa0 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a this comment..*
33fb0 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 /../*.** Structu
33fc0 72 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 res used by the
33fd0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e virtual table in
33fe0 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79 70 65 64 terface.*/.typed
33ff0 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
34000 33 5f 76 74 61 62 20 73 71 6c 69 74 65 33 5f 76 3_vtab sqlite3_v
34010 74 61 62 3b 0a 74 79 70 65 64 65 66 20 73 74 72 tab;.typedef str
34020 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 uct sqlite3_inde
34030 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65 33 5f 69 x_info sqlite3_i
34040 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79 70 65 64 ndex_info;.typed
34050 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
34060 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 73 71 3_vtab_cursor sq
34070 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
34080 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 r;.typedef struc
34090 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
340a0 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 3b sqlite3_module;
340b0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
340c0 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 : Virtual Table
340d0 4f 62 6a 65 63 74 20 7b 48 31 38 30 30 30 7d 20 Object {H18000}
340e0 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 <S20400>.** KEYW
340f0 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 6d 6f ORDS: sqlite3_mo
34100 64 75 6c 65 20 7b 76 69 72 74 75 61 6c 20 74 61 dule {virtual ta
34110 62 6c 65 20 6d 6f 64 75 6c 65 7d 0a 2a 2a 20 45 ble module}.** E
34120 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a XPERIMENTAL.**.*
34130 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 * This structure
34140 2c 20 73 6f 6d 65 74 69 6d 65 73 20 63 61 6c 6c , sometimes call
34150 65 64 20 61 20 61 20 22 76 69 72 74 75 61 6c 20 ed a a "virtual
34160 74 61 62 6c 65 20 6d 6f 64 75 6c 65 22 2c 20 0a table module", .
34170 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 ** defines the i
34180 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
34190 20 61 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c a [virtual tabl
341a0 65 73 5d 2e 20 20 0a 2a 2a 20 54 68 69 73 20 73 es]. .** This s
341b0 74 72 75 63 74 75 72 65 20 63 6f 6e 73 69 73 74 tructure consist
341c0 73 20 6d 6f 73 74 6c 79 20 6f 66 20 6d 65 74 68 s mostly of meth
341d0 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 ods for the modu
341e0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 le..**.** A virt
341f0 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 ual table module
34200 20 69 73 20 63 72 65 61 74 65 64 20 62 79 20 66 is created by f
34210 69 6c 6c 69 6e 67 20 69 6e 20 61 20 70 65 72 73 illing in a pers
34220 69 73 74 65 6e 74 0a 2a 2a 20 69 6e 73 74 61 6e istent.** instan
34230 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 ce of this struc
34240 74 75 72 65 20 61 6e 64 20 70 61 73 73 69 6e 67 ture and passing
34250 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
34260 61 74 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 74 at instance.** t
34270 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 o [sqlite3_creat
34280 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6f 72 20 5b e_module()] or [
34290 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d sqlite3_create_m
342a0 6f 64 75 6c 65 5f 76 32 28 29 5d 2e 0a 2a 2a 20 odule_v2()]..**
342b0 54 68 65 20 72 65 67 69 73 74 72 61 74 69 6f 6e The registration
342c0 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 remains valid u
342d0 6e 74 69 6c 20 69 74 20 69 73 20 72 65 70 6c 61 ntil it is repla
342e0 63 65 64 20 62 79 20 61 20 64 69 66 66 65 72 65 ced by a differe
342f0 6e 74 0a 2a 2a 20 6d 6f 64 75 6c 65 20 6f 72 20 nt.** module or
34300 75 6e 74 69 6c 20 74 68 65 20 5b 64 61 74 61 62 until the [datab
34310 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
34320 63 6c 6f 73 65 73 2e 20 20 54 68 65 20 63 6f 6e closes. The con
34330 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 69 73 20 tent.** of this
34340 73 74 72 75 63 74 75 72 65 20 6d 75 73 74 20 6e structure must n
34350 6f 74 20 63 68 61 6e 67 65 20 77 68 69 6c 65 20 ot change while
34360 69 74 20 69 73 20 72 65 67 69 73 74 65 72 65 64 it is registered
34370 20 77 69 74 68 0a 2a 2a 20 61 6e 79 20 64 61 74 with.** any dat
34380 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
34390 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 ..*/.struct sqli
343a0 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20 69 te3_module {. i
343b0 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69 nt iVersion;. i
343c0 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28 73 71 nt (*xCreate)(sq
343d0 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70 41 lite3*, void *pA
343e0 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ux,.
343f0 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e int argc, con
34400 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 st char *const*a
34410 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 rgv,.
34420 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 sqlite3_vtab
34430 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72 2a **ppVTab, char*
34440 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e *);. int (*xCon
34450 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a 2c 20 nect)(sqlite3*,
34460 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 20 void *pAux,.
34470 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 61 int a
34480 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 rgc, const char
34490 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20 *const*argv,.
344a0 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
344b0 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 te3_vtab **ppVTa
344c0 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e b, char**);. in
344d0 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 29 28 t (*xBestIndex)(
344e0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
344f0 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 Tab, sqlite3_ind
34500 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e 74 ex_info*);. int
34510 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29 28 (*xDisconnect)(
34520 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
34530 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 Tab);. int (*xD
34540 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f estroy)(sqlite3_
34550 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 vtab *pVTab);.
34560 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c int (*xOpen)(sql
34570 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 ite3_vtab *pVTab
34580 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 , sqlite3_vtab_c
34590 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 ursor **ppCursor
345a0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f 73 );. int (*xClos
345b0 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f e)(sqlite3_vtab_
345c0 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 cursor*);. int
345d0 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c 69 74 (*xFilter)(sqlit
345e0 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c e3_vtab_cursor*,
345f0 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e int idxNum, con
34600 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c st char *idxStr,
34610 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
34620 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 int argc, sqlit
34630 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 e3_value **argv)
34640 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 ;. int (*xNext)
34650 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 (sqlite3_vtab_cu
34660 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a rsor*);. int (*
34670 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f 76 74 xEof)(sqlite3_vt
34680 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 ab_cursor*);. i
34690 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 73 71 nt (*xColumn)(sq
346a0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
346b0 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 r*, sqlite3_cont
346c0 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e ext*, int);. in
346d0 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71 6c 69 t (*xRowid)(sqli
346e0 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a te3_vtab_cursor*
346f0 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 , sqlite3_int64
34700 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e 74 20 *pRowid);. int
34710 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c 69 74 (*xUpdate)(sqlit
34720 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74 2c 20 e3_vtab *, int,
34730 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
34740 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 , sqlite3_int64
34750 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65 67 *);. int (*xBeg
34760 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 in)(sqlite3_vtab
34770 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 *pVTab);. int
34780 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 65 33 (*xSync)(sqlite3
34790 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 _vtab *pVTab);.
347a0 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29 28 int (*xCommit)(
347b0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
347c0 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 Tab);. int (*xR
347d0 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 65 33 ollback)(sqlite3
347e0 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 _vtab *pVTab);.
347f0 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e 63 int (*xFindFunc
34800 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 tion)(sqlite3_vt
34810 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e ab *pVtab, int n
34820 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 Arg, const char
34830 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 *zName,.
34840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 v
34850 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28 73 oid (**pxFunc)(s
34860 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
34870 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
34880 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 20 20 e**),.
34890 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 voi
348a0 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20 69 6e d **ppArg);. in
348b0 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73 71 6c t (*xRename)(sql
348c0 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
348d0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e , const char *zN
348e0 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 ew);.};../*.** C
348f0 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c API3REF: Virtual
34900 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e 67 20 Table Indexing
34910 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 31 38 Information {H18
34920 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 100} <S20400>.**
34930 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 KEYWORDS: sqlit
34940 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a e3_index_info.**
34950 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
34960 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
34970 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 index_info struc
34980 74 75 72 65 20 61 6e 64 20 69 74 73 20 73 75 62 ture and its sub
34990 73 74 72 75 63 74 75 72 65 73 20 69 73 20 75 73 structures is us
349a0 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20 69 6e ed to.** pass in
349b0 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 61 formation into a
349c0 6e 64 20 72 65 63 65 69 76 65 20 74 68 65 20 72 nd receive the r
349d0 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20 5b 78 eply from the [x
349e0 42 65 73 74 49 6e 64 65 78 5d 0a 2a 2a 20 6d 65 BestIndex].** me
349f0 74 68 6f 64 20 6f 66 20 61 20 5b 76 69 72 74 75 thod of a [virtu
34a00 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d al table module]
34a10 2e 20 20 54 68 65 20 66 69 65 6c 64 73 20 75 6e . The fields un
34a20 64 65 72 20 2a 2a 49 6e 70 75 74 73 2a 2a 20 61 der **Inputs** a
34a30 72 65 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 73 re the.** inputs
34a40 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78 20 61 to xBestIndex a
34a50 6e 64 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 nd are read-only
34a60 2e 20 20 78 42 65 73 74 49 6e 64 65 78 20 69 6e . xBestIndex in
34a70 73 65 72 74 73 20 69 74 73 0a 2a 2a 20 72 65 73 serts its.** res
34a80 75 6c 74 73 20 69 6e 74 6f 20 74 68 65 20 2a 2a ults into the **
34a90 4f 75 74 70 75 74 73 2a 2a 20 66 69 65 6c 64 73 Outputs** fields
34aa0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 43 6f 6e ..**.** The aCon
34ab0 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 20 straint[] array
34ac0 72 65 63 6f 72 64 73 20 57 48 45 52 45 20 63 6c records WHERE cl
34ad0 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 ause constraints
34ae0 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a of the form:.**
34af0 0a 2a 2a 20 3c 70 72 65 3e 63 6f 6c 75 6d 6e 20 .** <pre>column
34b00 4f 50 20 65 78 70 72 3c 2f 70 72 65 3e 0a 2a 2a OP expr</pre>.**
34b10 0a 2a 2a 20 77 68 65 72 65 20 4f 50 20 69 73 20 .** where OP is
34b20 3d 2c 20 26 6c 74 3b 2c 20 26 6c 74 3b 3d 2c 20 =, <, <=,
34b30 26 67 74 3b 2c 20 6f 72 20 26 67 74 3b 3d 2e 20 >, or >=.
34b40 20 54 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 The particular
34b50 6f 70 65 72 61 74 6f 72 20 69 73 0a 2a 2a 20 73 operator is.** s
34b60 74 6f 72 65 64 20 69 6e 20 61 43 6f 6e 73 74 72 tored in aConstr
34b70 61 69 6e 74 5b 5d 2e 6f 70 2e 20 20 54 68 65 20 aint[].op. The
34b80 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c index of the col
34b90 75 6d 6e 20 69 73 20 73 74 6f 72 65 64 20 69 6e umn is stored in
34ba0 0a 2a 2a 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b .** aConstraint[
34bb0 5d 2e 69 43 6f 6c 75 6d 6e 2e 20 20 61 43 6f 6e ].iColumn. aCon
34bc0 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 straint[].usable
34bd0 20 69 73 20 54 52 55 45 20 69 66 20 74 68 65 0a is TRUE if the.
34be0 2a 2a 20 65 78 70 72 20 6f 6e 20 74 68 65 20 72 ** expr on the r
34bf0 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 63 ight-hand side c
34c00 61 6e 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 an be evaluated
34c10 28 61 6e 64 20 74 68 75 73 20 74 68 65 20 63 6f (and thus the co
34c20 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 69 73 20 75 nstraint.** is u
34c30 73 61 62 6c 65 29 20 61 6e 64 20 66 61 6c 73 65 sable) and false
34c40 20 69 66 20 69 74 20 63 61 6e 6e 6f 74 2e 0a 2a if it cannot..*
34c50 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a *.** The optimiz
34c60 65 72 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 er automatically
34c70 20 69 6e 76 65 72 74 73 20 74 65 72 6d 73 20 6f inverts terms o
34c80 66 20 74 68 65 20 66 6f 72 6d 20 22 65 78 70 72 f the form "expr
34c90 20 4f 50 20 63 6f 6c 75 6d 6e 22 0a 2a 2a 20 61 OP column".** a
34ca0 6e 64 20 6d 61 6b 65 73 20 6f 74 68 65 72 20 73 nd makes other s
34cb0 69 6d 70 6c 69 66 69 63 61 74 69 6f 6e 73 20 74 implifications t
34cc0 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 o the WHERE clau
34cd0 73 65 20 69 6e 20 61 6e 20 61 74 74 65 6d 70 74 se in an attempt
34ce0 20 74 6f 0a 2a 2a 20 67 65 74 20 61 73 20 6d 61 to.** get as ma
34cf0 6e 79 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 ny WHERE clause
34d00 74 65 72 6d 73 20 69 6e 74 6f 20 74 68 65 20 66 terms into the f
34d10 6f 72 6d 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 orm shown above
34d20 61 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 20 as possible..**
34d30 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b The aConstraint[
34d40 5d 20 61 72 72 61 79 20 6f 6e 6c 79 20 72 65 70 ] array only rep
34d50 6f 72 74 73 20 57 48 45 52 45 20 63 6c 61 75 73 orts WHERE claus
34d60 65 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 63 e terms in the c
34d70 6f 72 72 65 63 74 0a 2a 2a 20 66 6f 72 6d 20 74 orrect.** form t
34d80 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68 65 hat refer to the
34d90 20 70 61 72 74 69 63 75 6c 61 72 20 76 69 72 74 particular virt
34da0 75 61 6c 20 74 61 62 6c 65 20 62 65 69 6e 67 20 ual table being
34db0 71 75 65 72 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 queried..**.** I
34dc0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
34dd0 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c the ORDER BY cl
34de0 61 75 73 65 20 69 73 20 73 74 6f 72 65 64 20 69 ause is stored i
34df0 6e 20 61 4f 72 64 65 72 42 79 5b 5d 2e 0a 2a 2a n aOrderBy[]..**
34e00 20 45 61 63 68 20 74 65 72 6d 20 6f 66 20 61 4f Each term of aO
34e10 72 64 65 72 42 79 20 72 65 63 6f 72 64 73 20 61 rderBy records a
34e20 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 4f column of the O
34e30 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 0a RDER BY clause..
34e40 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 42 65 73 74 **.** The [xBest
34e50 49 6e 64 65 78 5d 20 6d 65 74 68 6f 64 20 6d 75 Index] method mu
34e60 73 74 20 66 69 6c 6c 20 61 43 6f 6e 73 74 72 61 st fill aConstra
34e70 69 6e 74 55 73 61 67 65 5b 5d 20 77 69 74 68 20 intUsage[] with
34e80 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 information.** a
34e90 62 6f 75 74 20 77 68 61 74 20 70 61 72 61 6d 65 bout what parame
34ea0 74 65 72 73 20 74 6f 20 70 61 73 73 20 74 6f 20 ters to pass to
34eb0 78 46 69 6c 74 65 72 2e 20 20 49 66 20 61 72 67 xFilter. If arg
34ec0 76 49 6e 64 65 78 3e 30 20 74 68 65 6e 0a 2a 2a vIndex>0 then.**
34ed0 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 the right-hand
34ee0 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f 72 72 side of the corr
34ef0 65 73 70 6f 6e 64 69 6e 67 20 61 43 6f 6e 73 74 esponding aConst
34f00 72 61 69 6e 74 5b 5d 20 69 73 20 65 76 61 6c 75 raint[] is evalu
34f10 61 74 65 64 0a 2a 2a 20 61 6e 64 20 62 65 63 6f ated.** and beco
34f20 6d 65 73 20 74 68 65 20 61 72 67 76 49 6e 64 65 mes the argvInde
34f30 78 2d 74 68 20 65 6e 74 72 79 20 69 6e 20 61 72 x-th entry in ar
34f40 67 76 2e 20 20 49 66 20 61 43 6f 6e 73 74 72 61 gv. If aConstra
34f50 69 6e 74 55 73 61 67 65 5b 5d 2e 6f 6d 69 74 0a intUsage[].omit.
34f60 2a 2a 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e ** is true, then
34f70 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 the constraint
34f80 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 is assumed to be
34f90 20 66 75 6c 6c 79 20 68 61 6e 64 6c 65 64 20 62 fully handled b
34fa0 79 20 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c y the.** virtual
34fb0 20 74 61 62 6c 65 20 61 6e 64 20 69 73 20 6e 6f table and is no
34fc0 74 20 63 68 65 63 6b 65 64 20 61 67 61 69 6e 20 t checked again
34fd0 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a by SQLite..**.**
34fe0 20 54 68 65 20 69 64 78 4e 75 6d 20 61 6e 64 20 The idxNum and
34ff0 69 64 78 50 74 72 20 76 61 6c 75 65 73 20 61 72 idxPtr values ar
35000 65 20 72 65 63 6f 72 64 65 64 20 61 6e 64 20 70 e recorded and p
35010 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 0a 2a assed into the.*
35020 2a 20 5b 78 46 69 6c 74 65 72 5d 20 6d 65 74 68 * [xFilter] meth
35030 6f 64 2e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f od..** [sqlite3_
35040 66 72 65 65 28 29 5d 20 69 73 20 75 73 65 64 20 free()] is used
35050 74 6f 20 66 72 65 65 20 69 64 78 50 74 72 20 69 to free idxPtr i
35060 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 66 0a 2a f and only iff.*
35070 2a 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 50 * needToFreeIdxP
35080 74 72 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a tr is true..**.*
35090 2a 20 54 68 65 20 6f 72 64 65 72 42 79 43 6f 6e * The orderByCon
350a0 73 75 6d 65 64 20 6d 65 61 6e 73 20 74 68 61 74 sumed means that
350b0 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 5b 78 46 output from [xF
350c0 69 6c 74 65 72 5d 2f 5b 78 4e 65 78 74 5d 20 77 ilter]/[xNext] w
350d0 69 6c 6c 20 6f 63 63 75 72 20 69 6e 0a 2a 2a 20 ill occur in.**
350e0 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72 64 65 the correct orde
350f0 72 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 r to satisfy the
35100 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 ORDER BY clause
35110 20 73 6f 20 74 68 61 74 20 6e 6f 20 73 65 70 61 so that no sepa
35120 72 61 74 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 rate.** sorting
35130 73 74 65 70 20 69 73 20 72 65 71 75 69 72 65 64 step is required
35140 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 ..**.** The esti
35150 6d 61 74 65 64 43 6f 73 74 20 76 61 6c 75 65 20 matedCost value
35160 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f is an estimate o
35170 66 20 74 68 65 20 63 6f 73 74 20 6f 66 20 64 6f f the cost of do
35180 69 6e 67 20 74 68 65 0a 2a 2a 20 70 61 72 74 69 ing the.** parti
35190 63 75 6c 61 72 20 6c 6f 6f 6b 75 70 2e 20 20 41 cular lookup. A
351a0 20 66 75 6c 6c 20 73 63 61 6e 20 6f 66 20 61 20 full scan of a
351b0 74 61 62 6c 65 20 77 69 74 68 20 4e 20 65 6e 74 table with N ent
351c0 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 ries should have
351d0 0a 2a 2a 20 61 20 63 6f 73 74 20 6f 66 20 4e 2e .** a cost of N.
351e0 20 20 41 20 62 69 6e 61 72 79 20 73 65 61 72 63 A binary searc
351f0 68 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 66 20 h of a table of
35200 4e 20 65 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 N entries should
35210 20 68 61 76 65 20 61 0a 2a 2a 20 63 6f 73 74 20 have a.** cost
35220 6f 66 20 61 70 70 72 6f 78 69 6d 61 74 65 6c 79 of approximately
35230 20 6c 6f 67 28 4e 29 2e 0a 2a 2f 0a 73 74 72 75 log(N)..*/.stru
35240 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 ct sqlite3_index
35250 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a 20 49 6e 70 _info {. /* Inp
35260 75 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f uts */. int nCo
35270 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20 20 nstraint;
35280 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
35290 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6e entries in aCon
352a0 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 73 74 72 straint */. str
352b0 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 uct sqlite3_inde
352c0 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 7b 0a 20 x_constraint {.
352d0 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b int iColumn;
352e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
352f0 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 66 74 2d Column on left-
35300 68 61 6e 64 20 73 69 64 65 20 6f 66 20 63 6f 6e hand side of con
35310 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 straint */.
35320 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f 70 unsigned char op
35330 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e ; /* Con
35340 73 74 72 61 69 6e 74 20 6f 70 65 72 61 74 6f 72 straint operator
35350 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 */. unsigne
35360 64 20 63 68 61 72 20 75 73 61 62 6c 65 3b 20 20 d char usable;
35370 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
35380 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 is constraint is
35390 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 usable */.
353a0 69 6e 74 20 69 54 65 72 6d 4f 66 66 73 65 74 3b int iTermOffset;
353b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 /* Use
353c0 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2d 20 78 d internally - x
353d0 42 65 73 74 49 6e 64 65 78 20 73 68 6f 75 6c 64 BestIndex should
353e0 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 20 7d 20 2a ignore */. } *
353f0 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 aConstraint;
35400 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 /* Table
35410 20 6f 66 20 57 48 45 52 45 20 63 6c 61 75 73 65 of WHERE clause
35420 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a constraints */.
35430 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b 20 int nOrderBy;
35440 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
35450 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 Number of terms
35460 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 in the ORDER BY
35470 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 clause */. stru
35480 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 ct sqlite3_index
35490 5f 6f 72 64 65 72 62 79 20 7b 0a 20 20 20 20 20 _orderby {.
354a0 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 int iColumn;
354b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c /* Col
354c0 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 umn number */.
354d0 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 unsigned char
354e0 20 64 65 73 63 3b 20 20 20 20 20 20 20 2f 2a 20 desc; /*
354f0 54 72 75 65 20 66 6f 72 20 44 45 53 43 2e 20 20 True for DESC.
35500 46 61 6c 73 65 20 66 6f 72 20 41 53 43 2e 20 2a False for ASC. *
35510 2f 0a 20 20 7d 20 2a 61 4f 72 64 65 72 42 79 3b /. } *aOrderBy;
35520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
35530 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 * The ORDER BY c
35540 6c 61 75 73 65 20 2a 2f 0a 20 20 2f 2a 20 4f 75 lause */. /* Ou
35550 74 70 75 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 tputs */. struc
35560 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f t sqlite3_index_
35570 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65 constraint_usage
35580 20 7b 0a 20 20 20 20 69 6e 74 20 61 72 67 76 49 {. int argvI
35590 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 ndex;
355a0 2f 2a 20 69 66 20 3e 30 2c 20 63 6f 6e 73 74 72 /* if >0, constr
355b0 61 69 6e 74 20 69 73 20 70 61 72 74 20 6f 66 20 aint is part of
355c0 61 72 67 76 20 74 6f 20 78 46 69 6c 74 65 72 20 argv to xFilter
355d0 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 */. unsigned
355e0 63 68 61 72 20 6f 6d 69 74 3b 20 20 20 20 20 20 char omit;
355f0 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 64 65 20 61 /* Do not code a
35600 20 74 65 73 74 20 66 6f 72 20 74 68 69 73 20 63 test for this c
35610 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 7d onstraint */. }
35620 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 *aConstraintUsa
35630 67 65 3b 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d ge;. int idxNum
35640 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
35650 20 2f 2a 20 4e 75 6d 62 65 72 20 75 73 65 64 20 /* Number used
35660 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65 20 to identify the
35670 69 6e 64 65 78 20 2a 2f 0a 20 20 63 68 61 72 20 index */. char
35680 2a 69 64 78 53 74 72 3b 20 20 20 20 20 20 20 20 *idxStr;
35690 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 2c /* String,
356a0 20 70 6f 73 73 69 62 6c 79 20 6f 62 74 61 69 6e possibly obtain
356b0 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f ed from sqlite3_
356c0 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 69 6e 74 20 malloc */. int
356d0 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 needToFreeIdxStr
356e0 3b 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 69 ; /* Free i
356f0 64 78 53 74 72 20 75 73 69 6e 67 20 73 71 6c 69 dxStr using sqli
35700 74 65 33 5f 66 72 65 65 28 29 20 69 66 20 74 72 te3_free() if tr
35710 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 64 65 ue */. int orde
35720 72 42 79 43 6f 6e 73 75 6d 65 64 3b 20 20 20 20 rByConsumed;
35730 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6f 75 /* True if ou
35740 74 70 75 74 20 69 73 20 61 6c 72 65 61 64 79 20 tput is already
35750 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 64 6f 75 ordered */. dou
35760 62 6c 65 20 65 73 74 69 6d 61 74 65 64 43 6f 73 ble estimatedCos
35770 74 3b 20 20 20 20 20 20 2f 2a 20 45 73 74 69 6d t; /* Estim
35780 61 74 65 64 20 63 6f 73 74 20 6f 66 20 75 73 69 ated cost of usi
35790 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f ng this index */
357a0 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .};.#define SQLI
357b0 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 TE_INDEX_CONSTRA
357c0 49 4e 54 5f 45 51 20 20 20 20 32 0a 23 64 65 66 INT_EQ 2.#def
357d0 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 ine SQLITE_INDEX
357e0 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 20 20 _CONSTRAINT_GT
357f0 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 4.#define SQLI
35800 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 TE_INDEX_CONSTRA
35810 49 4e 54 5f 4c 45 20 20 20 20 38 0a 23 64 65 66 INT_LE 8.#def
35820 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 ine SQLITE_INDEX
35830 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20 20 _CONSTRAINT_LT
35840 20 20 31 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 16.#define SQL
35850 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 ITE_INDEX_CONSTR
35860 41 49 4e 54 5f 47 45 20 20 20 20 33 32 0a 23 64 AINT_GE 32.#d
35870 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 efine SQLITE_IND
35880 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 EX_CONSTRAINT_MA
35890 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 TCH 64../*.** CA
358a0 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 65 72 PI3REF: Register
358b0 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 A Virtual Table
358c0 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
358d0 7b 48 31 38 32 30 30 7d 20 3c 53 32 30 34 30 30 {H18200} <S20400
358e0 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
358f0 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 L.**.** This rou
35900 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 tine is used to
35910 72 65 67 69 73 74 65 72 20 61 20 6e 65 77 20 5b register a new [
35920 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f virtual table mo
35930 64 75 6c 65 5d 20 6e 61 6d 65 2e 0a 2a 2a 20 4d dule] name..** M
35940 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d 75 73 74 odule names must
35950 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 62 be registered b
35960 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 74 69 6e efore.** creatin
35970 67 20 61 20 6e 65 77 20 5b 76 69 72 74 75 61 6c g a new [virtual
35980 20 74 61 62 6c 65 5d 20 75 73 69 6e 67 20 74 68 table] using th
35990 65 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 62 65 66 e module, or bef
359a0 6f 72 65 20 75 73 69 6e 67 20 61 0a 2a 2a 20 70 ore using a.** p
359b0 72 65 65 78 69 73 74 69 6e 67 20 5b 76 69 72 74 reexisting [virt
359c0 75 61 6c 20 74 61 62 6c 65 5d 20 66 6f 72 20 74 ual table] for t
359d0 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a he module..**.**
359e0 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 The module name
359f0 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20 6f is registered o
35a00 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 n the [database
35a10 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 connection] spec
35a20 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 ified.** by the
35a30 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e first parameter.
35a40 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 The name of th
35a50 65 20 6d 6f 64 75 6c 65 20 69 73 20 67 69 76 65 e module is give
35a60 6e 20 62 79 20 74 68 65 20 0a 2a 2a 20 73 65 63 n by the .** sec
35a70 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 ond parameter.
35a80 54 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 The third parame
35a90 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ter is a pointer
35aa0 20 74 6f 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 to.** the imple
35ab0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
35ac0 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 [virtual table
35ad0 6d 6f 64 75 6c 65 5d 2e 20 20 20 54 68 65 20 66 module]. The f
35ae0 6f 75 72 74 68 0a 2a 2a 20 70 61 72 61 6d 65 74 ourth.** paramet
35af0 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 er is an arbitra
35b00 72 79 20 63 6c 69 65 6e 74 20 64 61 74 61 20 70 ry client data p
35b10 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20 70 ointer that is p
35b20 61 73 73 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a assed through.**
35b30 20 69 6e 74 6f 20 74 68 65 20 5b 78 43 72 65 61 into the [xCrea
35b40 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e 65 63 te] and [xConnec
35b50 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 t] methods of th
35b60 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 e virtual table
35b70 6d 6f 64 75 6c 65 0a 2a 2a 20 77 68 65 6e 20 61 module.** when a
35b80 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 61 62 new virtual tab
35b90 6c 65 20 69 73 20 62 65 20 62 65 69 6e 67 20 63 le is be being c
35ba0 72 65 61 74 65 64 20 6f 72 20 72 65 69 6e 69 74 reated or reinit
35bb0 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 ialized..**.** T
35bc0 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 68 61 his interface ha
35bd0 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 s exactly the sa
35be0 6d 65 20 65 66 66 65 63 74 20 61 73 20 63 61 6c me effect as cal
35bf0 6c 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ling.** [sqlite3
35c00 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 _create_module_v
35c10 32 28 29 5d 20 77 69 74 68 20 61 20 4e 55 4c 4c 2()] with a NULL
35c20 20 63 6c 69 65 6e 74 20 64 61 74 61 20 64 65 73 client data des
35c30 74 72 75 63 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 tructor..*/.SQLI
35c40 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 TE_API SQLITE_EX
35c50 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 PERIMENTAL int s
35c60 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f qlite3_create_mo
35c70 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 dule(. sqlite3
35c80 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 *db,
35c90 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e /* SQLite con
35ca0 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 nection to regis
35cb0 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 ter module with
35cc0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
35cd0 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 *zName,
35ce0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d /* Name of the m
35cf0 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 odule */. const
35d00 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 sqlite3_module
35d10 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 *p, /* Methods
35d20 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 for the module
35d30 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 65 */. void *pClie
35d40 6e 74 44 61 74 61 20 20 20 20 20 20 20 20 20 20 ntData
35d50 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 20 66 /* Client data f
35d60 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e or xCreate/xConn
35d70 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a ect */.);../*.**
35d80 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 CAPI3REF: Regis
35d90 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 54 61 ter A Virtual Ta
35da0 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 ble Implementati
35db0 6f 6e 20 7b 48 31 38 32 31 30 7d 20 3c 53 32 30 on {H18210} <S20
35dc0 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 400>.** EXPERIME
35dd0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 NTAL.**.** This
35de0 72 6f 75 74 69 6e 65 20 69 73 20 69 64 65 6e 74 routine is ident
35df0 69 63 61 6c 20 74 6f 20 74 68 65 20 5b 73 71 6c ical to the [sql
35e00 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 ite3_create_modu
35e10 6c 65 28 29 5d 20 6d 65 74 68 6f 64 2c 0a 2a 2a le()] method,.**
35e20 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20 except that it
35e30 68 61 73 20 61 6e 20 65 78 74 72 61 20 70 61 72 has an extra par
35e40 61 6d 65 74 65 72 20 74 6f 20 73 70 65 63 69 66 ameter to specif
35e50 79 20 0a 2a 2a 20 61 20 64 65 73 74 72 75 63 74 y .** a destruct
35e60 6f 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 or function for
35e70 74 68 65 20 63 6c 69 65 6e 74 20 64 61 74 61 20 the client data
35e80 70 6f 69 6e 74 65 72 2e 20 20 53 51 4c 69 74 65 pointer. SQLite
35e90 20 77 69 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b 65 20 will.** invoke
35ea0 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 the destructor f
35eb0 75 6e 63 74 69 6f 6e 20 28 69 66 20 69 74 20 69 unction (if it i
35ec0 73 20 6e 6f 74 20 4e 55 4c 4c 29 20 77 68 65 6e s not NULL) when
35ed0 20 53 51 4c 69 74 65 0a 2a 2a 20 6e 6f 20 6c 6f SQLite.** no lo
35ee0 6e 67 65 72 20 6e 65 65 64 73 20 74 68 65 20 70 nger needs the p
35ef0 43 6c 69 65 6e 74 44 61 74 61 20 70 6f 69 6e 74 ClientData point
35f00 65 72 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f er. .*/.SQLITE_
35f10 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 API SQLITE_EXPER
35f20 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 IMENTAL int sqli
35f30 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c te3_create_modul
35f40 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 e_v2(. sqlite3
35f50 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 *db,
35f60 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e /* SQLite con
35f70 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 nection to regis
35f80 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 ter module with
35f90 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
35fa0 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 *zName,
35fb0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d /* Name of the m
35fc0 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 odule */. const
35fd0 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 sqlite3_module
35fe0 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 *p, /* Methods
35ff0 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 for the module
36000 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 65 */. void *pClie
36010 6e 74 44 61 74 61 2c 20 20 20 20 20 20 20 20 20 ntData,
36020 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 20 66 /* Client data f
36030 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e or xCreate/xConn
36040 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 28 2a 78 ect */. void(*x
36050 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 20 Destroy)(void*)
36060 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 /* Module de
36070 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f structor functio
36080 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 n */.);../*.** C
36090 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c API3REF: Virtual
360a0 20 54 61 62 6c 65 20 49 6e 73 74 61 6e 63 65 20 Table Instance
360b0 4f 62 6a 65 63 74 20 7b 48 31 38 30 31 30 7d 20 Object {H18010}
360c0 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 <S20400>.** KEYW
360d0 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 74 ORDS: sqlite3_vt
360e0 61 62 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 ab.** EXPERIMENT
360f0 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 5b AL.**.** Every [
36100 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f virtual table mo
36110 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 61 dule] implementa
36120 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62 63 tion uses a subc
36130 6c 61 73 73 0a 2a 2a 20 6f 66 20 74 68 65 20 66 lass.** of the f
36140 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
36150 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 20 61 re to describe a
36160 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 73 74 particular inst
36170 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 5b ance.** of the [
36180 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e 20 virtual table].
36190 20 45 61 63 68 20 73 75 62 63 6c 61 73 73 20 77 Each subclass w
361a0 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f 72 ill.** be tailor
361b0 65 64 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 ed to the specif
361c0 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68 65 20 ic needs of the
361d0 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 module implement
361e0 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70 75 ation..** The pu
361f0 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 73 75 rpose of this su
36200 70 65 72 63 6c 61 73 73 20 69 73 20 74 6f 20 64 perclass is to d
36210 65 66 69 6e 65 20 63 65 72 74 61 69 6e 20 66 69 efine certain fi
36220 65 6c 64 73 20 74 68 61 74 20 61 72 65 0a 2a 2a elds that are.**
36230 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6d common to all m
36240 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 odule implementa
36250 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 tions..**.** Vir
36260 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 65 74 68 tual tables meth
36270 6f 64 73 20 63 61 6e 20 73 65 74 20 61 6e 20 65 ods can set an e
36280 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 79 20 rror message by
36290 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a 20 73 assigning a.** s
362a0 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 tring obtained f
362b0 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 rom [sqlite3_mpr
362c0 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72 72 4d intf()] to zErrM
362d0 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f 64 20 sg. The method
362e0 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 63 should.** take c
362f0 61 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 69 are that any pri
36300 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66 72 65 or string is fre
36310 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 ed by a call to
36320 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d [sqlite3_free()]
36330 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 73 73 .** prior to ass
36340 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 73 74 72 igning a new str
36350 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e 20 ing to zErrMsg.
36360 20 41 66 74 65 72 20 74 68 65 20 65 72 72 6f 72 After the error
36370 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 64 message.** is d
36380 65 6c 69 76 65 72 65 64 20 75 70 20 74 6f 20 74 elivered up to t
36390 68 65 20 63 6c 69 65 6e 74 20 61 70 70 6c 69 63 he client applic
363a0 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 72 69 6e ation, the strin
363b0 67 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 g will be automa
363c0 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 tically.** freed
363d0 20 62 79 20 73 71 6c 69 74 65 33 5f 66 72 65 65 by sqlite3_free
363e0 28 29 20 61 6e 64 20 74 68 65 20 7a 45 72 72 4d () and the zErrM
363f0 73 67 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 65 sg field will be
36400 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 73 74 72 75 zeroed..*/.stru
36410 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 ct sqlite3_vtab
36420 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 {. const sqlite
36430 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 3_module *pModul
36440 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c e; /* The modul
36450 65 20 66 6f 72 20 74 68 69 73 20 76 69 72 74 75 e for this virtu
36460 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e al table */. in
36470 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 t nRef;
36480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
36490 20 4e 4f 20 4c 4f 4e 47 45 52 20 55 53 45 44 20 NO LONGER USED
364a0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d */. char *zErrM
364b0 73 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 sg;
364c0 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 /* Error me
364d0 73 73 61 67 65 20 66 72 6f 6d 20 73 71 6c 69 74 ssage from sqlit
364e0 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a e3_mprintf() */.
364f0 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 /* Virtual tab
36500 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f le implementatio
36510 6e 73 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c ns will typicall
36520 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c y add additional
36530 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f fields */.};../
36540 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 *.** CAPI3REF: V
36550 69 72 74 75 61 6c 20 54 61 62 6c 65 20 43 75 72 irtual Table Cur
36560 73 6f 72 20 4f 62 6a 65 63 74 20 20 7b 48 31 38 sor Object {H18
36570 30 32 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 020} <S20400>.**
36580 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 KEYWORDS: sqlit
36590 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b e3_vtab_cursor {
365a0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 virtual table cu
365b0 72 73 6f 72 7d 0a 2a 2a 20 45 58 50 45 52 49 4d rsor}.** EXPERIM
365c0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 ENTAL.**.** Ever
365d0 79 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 y [virtual table
365e0 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 module] impleme
365f0 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 ntation uses a s
36600 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a ubclass of the.*
36610 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 * following stru
36620 63 74 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 cture to describ
36630 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 70 e cursors that p
36640 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 0a 2a 2a oint into the.**
36650 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d [virtual table]
36660 20 61 6e 64 20 61 72 65 20 75 73 65 64 0a 2a 2a and are used.**
36670 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 to loop through
36680 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 the virtual tab
36690 6c 65 2e 20 20 43 75 72 73 6f 72 73 20 61 72 65 le. Cursors are
366a0 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 created using t
366b0 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d he.** [sqlite3_m
366c0 6f 64 75 6c 65 2e 78 4f 70 65 6e 20 7c 20 78 4f odule.xOpen | xO
366d0 70 65 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 pen] method of t
366e0 68 65 20 6d 6f 64 75 6c 65 20 61 6e 64 20 61 72 he module and ar
366f0 65 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a 20 62 e destroyed.** b
36700 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d y the [sqlite3_m
36710 6f 64 75 6c 65 2e 78 43 6c 6f 73 65 20 7c 20 78 odule.xClose | x
36720 43 6c 6f 73 65 5d 20 6d 65 74 68 6f 64 2e 20 20 Close] method.
36730 43 75 73 73 6f 72 73 20 61 72 65 20 75 73 65 64 Cussors are used
36740 0a 2a 2a 20 62 79 20 74 68 65 20 5b 78 46 69 6c .** by the [xFil
36750 74 65 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c 20 5b ter], [xNext], [
36760 78 45 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d 6e 5d xEof], [xColumn]
36770 2c 20 61 6e 64 20 5b 78 52 6f 77 69 64 5d 20 6d , and [xRowid] m
36780 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 68 65 ethods.** of the
36790 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68 20 6d module. Each m
367a0 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 odule implementa
367b0 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69 6e 65 tion will define
367c0 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 .** the content
367d0 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74 72 75 of a cursor stru
367e0 63 74 75 72 65 20 74 6f 20 73 75 69 74 20 69 74 cture to suit it
367f0 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a 2a 0a s own needs..**.
36800 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63 6c 61 ** This supercla
36810 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f 72 64 ss exists in ord
36820 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66 69 65 er to define fie
36830 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72 73 6f lds of the curso
36840 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 6f r that.** are co
36850 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d 70 6c mmon to all impl
36860 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a ementations..*/.
36870 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 struct sqlite3_v
36880 74 61 62 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 tab_cursor {. s
36890 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 qlite3_vtab *pVt
368a0 61 62 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 74 ab; /* Virt
368b0 75 61 6c 20 74 61 62 6c 65 20 6f 66 20 74 68 69 ual table of thi
368c0 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 2f 2a s cursor */. /*
368d0 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 Virtual table i
368e0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 mplementations w
368f0 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64 ill typically ad
36900 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 d additional fie
36910 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a lds */.};../*.**
36920 20 43 41 50 49 33 52 45 46 3a 20 44 65 63 6c 61 CAPI3REF: Decla
36930 72 65 20 54 68 65 20 53 63 68 65 6d 61 20 4f 66 re The Schema Of
36940 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 A Virtual Table
36950 20 7b 48 31 38 32 38 30 7d 20 3c 53 32 30 34 30 {H18280} <S2040
36960 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
36970 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 43 AL.**.** The [xC
36980 72 65 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e reate] and [xCon
36990 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 nect] methods of
369a0 20 61 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 a.** [virtual t
369b0 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 63 61 6c able module] cal
369c0 6c 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 l this interface
369d0 0a 2a 2a 20 74 6f 20 64 65 63 6c 61 72 65 20 74 .** to declare t
369e0 68 65 20 66 6f 72 6d 61 74 20 28 74 68 65 20 6e he format (the n
369f0 61 6d 65 73 20 61 6e 64 20 64 61 74 61 74 79 70 ames and datatyp
36a00 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e es of the column
36a10 73 29 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72 s) of.** the vir
36a20 74 75 61 6c 20 74 61 62 6c 65 73 20 74 68 65 79 tual tables they
36a30 20 69 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 implement..*/.S
36a40 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 QLITE_API SQLITE
36a50 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e _EXPERIMENTAL in
36a60 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 t sqlite3_declar
36a70 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33 2a 2c e_vtab(sqlite3*,
36a80 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 51 const char *zSQ
36a90 4c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 L);../*.** CAPI3
36aa0 52 45 46 3a 20 4f 76 65 72 6c 6f 61 64 20 41 20 REF: Overload A
36ab0 46 75 6e 63 74 69 6f 6e 20 46 6f 72 20 41 20 56 Function For A V
36ac0 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b 48 31 irtual Table {H1
36ad0 38 33 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 8300} <S20400>.*
36ae0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
36af0 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 62 *.** Virtual tab
36b00 6c 65 73 20 63 61 6e 20 70 72 6f 76 69 64 65 20 les can provide
36b10 61 6c 74 65 72 6e 61 74 69 76 65 20 69 6d 70 6c alternative impl
36b20 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 66 ementations of f
36b30 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 75 73 69 6e unctions.** usin
36b40 67 20 74 68 65 20 5b 78 46 69 6e 64 46 75 6e 63 g the [xFindFunc
36b50 74 69 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 tion] method of
36b60 74 68 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 the [virtual tab
36b70 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 0a 2a 2a le module]. .**
36b80 20 42 75 74 20 67 6c 6f 62 61 6c 20 76 65 72 73 But global vers
36b90 69 6f 6e 73 20 6f 66 20 74 68 6f 73 65 20 66 75 ions of those fu
36ba0 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73 74 20 nctions.** must
36bb0 65 78 69 73 74 20 69 6e 20 6f 72 64 65 72 20 74 exist in order t
36bc0 6f 20 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 2e o be overloaded.
36bd0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 .**.** This API
36be0 6d 61 6b 65 73 20 73 75 72 65 20 61 20 67 6c 6f makes sure a glo
36bf0 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f 66 20 61 bal version of a
36c00 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 function with a
36c10 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 6e particular.** n
36c20 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65 72 20 6f ame and number o
36c30 66 20 70 61 72 61 6d 65 74 65 72 73 20 65 78 69 f parameters exi
36c40 73 74 73 2e 20 20 49 66 20 6e 6f 20 73 75 63 68 sts. If no such
36c50 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 function exists
36c60 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20 .** before this
36c70 41 50 49 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 API is called, a
36c80 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 73 new function is
36c90 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 69 created. The i
36ca0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a mplementation.**
36cb0 20 6f 66 20 74 68 65 20 6e 65 77 20 66 75 6e 63 of the new func
36cc0 74 69 6f 6e 20 61 6c 77 61 79 73 20 63 61 75 73 tion always caus
36cd0 65 73 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 es an exception
36ce0 74 6f 20 62 65 20 74 68 72 6f 77 6e 2e 20 20 53 to be thrown. S
36cf0 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 66 75 6e o.** the new fun
36d00 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 6f 6f ction is not goo
36d10 64 20 66 6f 72 20 61 6e 79 74 68 69 6e 67 20 62 d for anything b
36d20 79 20 69 74 73 65 6c 66 2e 20 20 49 74 73 20 6f y itself. Its o
36d30 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f 73 65 20 69 nly.** purpose i
36d40 73 20 74 6f 20 62 65 20 61 20 70 6c 61 63 65 68 s to be a placeh
36d50 6f 6c 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 older function t
36d60 68 61 74 20 63 61 6e 20 62 65 20 6f 76 65 72 6c hat can be overl
36d70 6f 61 64 65 64 0a 2a 2a 20 62 79 20 61 20 5b 76 oaded.** by a [v
36d80 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e 0a 2a irtual table]..*
36d90 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c /.SQLITE_API SQL
36da0 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c ITE_EXPERIMENTAL
36db0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65 int sqlite3_ove
36dc0 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 73 rload_function(s
36dd0 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 qlite3*, const c
36de0 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20 har *zFuncName,
36df0 69 6e 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a 0a 2a int nArg);../*.*
36e00 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 * The interface
36e10 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 to the virtual-t
36e20 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 64 able mechanism d
36e30 65 66 69 6e 65 64 20 61 62 6f 76 65 20 28 62 61 efined above (ba
36e40 63 6b 20 75 70 0a 2a 2a 20 74 6f 20 61 20 63 6f ck up.** to a co
36e50 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61 62 6c 79 mment remarkably
36e60 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 similar to this
36e70 20 6f 6e 65 29 20 69 73 20 63 75 72 72 65 6e 74 one) is current
36e80 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a ly considered.**
36e90 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d 65 6e to be experimen
36ea0 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 66 tal. The interf
36eb0 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 ace might change
36ec0 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 in incompatible
36ed0 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 69 ways..** If thi
36ee0 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 66 s is a problem f
36ef0 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 75 or you, do not u
36f00 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 se the interface
36f10 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a at this time..*
36f20 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 69 *.** When the vi
36f30 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68 rtual-table mech
36f40 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 73 anism stabilizes
36f50 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 72 , we will declar
36f60 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 61 e the.** interfa
36f70 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 6f 72 ce fixed, suppor
36f80 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 65 6c t it indefinitel
36f90 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 y, and remove th
36fa0 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a 0a 2a is comment..**.*
36fb0 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 ***** EXPERIMENT
36fc0 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74 6f 20 AL - subject to
36fd0 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e change without n
36fe0 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a otice **********
36ff0 2a 2a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 ****.*/../*.** C
37000 41 50 49 33 52 45 46 3a 20 41 20 48 61 6e 64 6c API3REF: A Handl
37010 65 20 54 6f 20 41 6e 20 4f 70 65 6e 20 42 4c 4f e To An Open BLO
37020 42 20 7b 48 31 37 38 30 30 7d 20 3c 53 33 30 32 B {H17800} <S302
37030 33 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 30>.** KEYWORDS:
37040 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 7d 20 7b {BLOB handle} {
37050 42 4c 4f 42 20 68 61 6e 64 6c 65 73 7d 0a 2a 2a BLOB handles}.**
37060 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
37070 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 of this object r
37080 65 70 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 epresents an ope
37090 6e 20 42 4c 4f 42 20 6f 6e 20 77 68 69 63 68 0a n BLOB on which.
370a0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 ** [sqlite3_blob
370b0 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e _open | incremen
370c0 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 63 61 tal BLOB I/O] ca
370d0 6e 20 62 65 20 70 65 72 66 6f 72 6d 65 64 2e 0a n be performed..
370e0 2a 2a 20 4f 62 6a 65 63 74 73 20 6f 66 20 74 68 ** Objects of th
370f0 69 73 20 74 79 70 65 20 61 72 65 20 63 72 65 61 is type are crea
37100 74 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f ted by [sqlite3_
37110 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 blob_open()].**
37120 61 6e 64 20 64 65 73 74 72 6f 79 65 64 20 62 79 and destroyed by
37130 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 [sqlite3_blob_c
37140 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 lose()]..** The
37150 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 [sqlite3_blob_re
37160 61 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 ad()] and [sqlit
37170 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d e3_blob_write()]
37180 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 63 interfaces.** c
37190 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 an be used to re
371a0 61 64 20 6f 72 20 77 72 69 74 65 20 73 6d 61 6c ad or write smal
371b0 6c 20 73 75 62 73 65 63 74 69 6f 6e 73 20 6f 66 l subsections of
371c0 20 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20 54 68 the BLOB..** Th
371d0 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f e [sqlite3_blob_
371e0 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 bytes()] interfa
371f0 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 ce returns the s
37200 69 7a 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 ize of the BLOB
37210 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 79 70 in bytes..*/.typ
37220 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
37230 74 65 33 5f 62 6c 6f 62 20 73 71 6c 69 74 65 33 te3_blob sqlite3
37240 5f 62 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 _blob;../*.** CA
37250 50 49 33 52 45 46 3a 20 4f 70 65 6e 20 41 20 42 PI3REF: Open A B
37260 4c 4f 42 20 46 6f 72 20 49 6e 63 72 65 6d 65 6e LOB For Incremen
37270 74 61 6c 20 49 2f 4f 20 7b 48 31 37 38 31 30 7d tal I/O {H17810}
37280 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 <S30230>.**.**
37290 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 73 20 This interfaces
372a0 6f 70 65 6e 73 20 61 20 5b 42 4c 4f 42 20 68 61 opens a [BLOB ha
372b0 6e 64 6c 65 20 7c 20 68 61 6e 64 6c 65 5d 20 74 ndle | handle] t
372c0 6f 20 74 68 65 20 42 4c 4f 42 20 6c 6f 63 61 74 o the BLOB locat
372d0 65 64 0a 2a 2a 20 69 6e 20 72 6f 77 20 69 52 6f ed.** in row iRo
372e0 77 2c 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c 75 6d w, column zColum
372f0 6e 2c 20 74 61 62 6c 65 20 7a 54 61 62 6c 65 20 n, table zTable
37300 69 6e 20 64 61 74 61 62 61 73 65 20 7a 44 62 3b in database zDb;
37310 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 .** in other wor
37320 64 73 2c 20 74 68 65 20 73 61 6d 65 20 42 4c 4f ds, the same BLO
37330 42 20 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 B that would be
37340 73 65 6c 65 63 74 65 64 20 62 79 3a 0a 2a 2a 0a selected by:.**.
37350 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 ** <pre>.**
37360 53 45 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e 20 46 SELECT zColumn F
37370 52 4f 4d 20 7a 44 62 2e 7a 54 61 62 6c 65 20 57 ROM zDb.zTable W
37380 48 45 52 45 20 5b 72 6f 77 69 64 5d 20 3d 20 69 HERE [rowid] = i
37390 52 6f 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 20 7b Row;.** </pre> {
373a0 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 END}.**.** If th
373b0 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 e flags paramete
373c0 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 r is non-zero, t
373d0 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 69 73 20 hen the BLOB is
373e0 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 0a opened for read.
373f0 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 61 63 63 ** and write acc
37400 65 73 73 2e 20 49 66 20 69 74 20 69 73 20 7a 65 ess. If it is ze
37410 72 6f 2c 20 74 68 65 20 42 4c 4f 42 20 69 73 20 ro, the BLOB is
37420 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 20 opened for read
37430 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f access..**.** No
37440 74 65 20 74 68 61 74 20 74 68 65 20 64 61 74 61 te that the data
37450 62 61 73 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 base name is not
37460 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 68 the filename th
37470 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 at contains.** t
37480 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20 he database but
37490 72 61 74 68 65 72 20 74 68 65 20 73 79 6d 62 6f rather the symbo
374a0 6c 69 63 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 lic name of the
374b0 64 61 74 61 62 61 73 65 20 74 68 61 74 0a 2a 2a database that.**
374c0 20 69 73 20 61 73 73 69 67 6e 65 64 20 77 68 65 is assigned whe
374d0 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 n the database i
374e0 73 20 63 6f 6e 6e 65 63 74 65 64 20 75 73 69 6e s connected usin
374f0 67 20 5b 41 54 54 41 43 48 5d 2e 0a 2a 2a 20 46 g [ATTACH]..** F
37500 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 or the main data
37510 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20 64 base file, the d
37520 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 atabase name is
37530 22 6d 61 69 6e 22 2e 0a 2a 2a 20 46 6f 72 20 54 "main"..** For T
37540 45 4d 50 20 74 61 62 6c 65 73 2c 20 74 68 65 20 EMP tables, the
37550 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 database name is
37560 20 22 74 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20 4f "temp"..**.** O
37570 6e 20 73 75 63 63 65 73 73 2c 20 5b 53 51 4c 49 n success, [SQLI
37580 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e TE_OK] is return
37590 65 64 20 61 6e 64 20 74 68 65 20 6e 65 77 20 5b ed and the new [
375a0 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 73 20 BLOB handle] is
375b0 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 written.** to *p
375c0 70 42 6c 6f 62 2e 20 4f 74 68 65 72 77 69 73 65 pBlob. Otherwise
375d0 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d an [error code]
375e0 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
375f0 20 2a 70 70 42 6c 6f 62 20 69 73 20 73 65 74 0a *ppBlob is set.
37600 2a 2a 20 74 6f 20 62 65 20 61 20 6e 75 6c 6c 20 ** to be a null
37610 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 54 68 69 73 pointer..** This
37620 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74 function sets t
37630 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
37640 6e 65 63 74 69 6f 6e 5d 20 65 72 72 6f 72 20 63 nection] error c
37650 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 0a ode and message.
37660 2a 2a 20 61 63 63 65 73 73 69 62 6c 65 20 76 69 ** accessible vi
37670 61 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f a [sqlite3_errco
37680 64 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 de()] and [sqlit
37690 65 33 5f 65 72 72 6d 73 67 28 29 5d 20 61 6e 64 e3_errmsg()] and
376a0 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63 related.** func
376b0 74 69 6f 6e 73 2e 20 20 4e 6f 74 65 20 74 68 61 tions. Note tha
376c0 74 20 74 68 65 20 2a 70 70 42 6c 6f 62 20 76 61 t the *ppBlob va
376d0 72 69 61 62 6c 65 20 69 73 20 61 6c 77 61 79 73 riable is always
376e0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 6e 20 initialized in
376f0 61 0a 2a 2a 20 77 61 79 20 74 68 61 74 20 6d 61 a.** way that ma
37700 6b 65 73 20 69 74 20 73 61 66 65 20 74 6f 20 69 kes it safe to i
37710 6e 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f 62 nvoke [sqlite3_b
37720 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 20 6f 6e 20 lob_close()] on
37730 2a 70 70 42 6c 6f 62 0a 2a 2a 20 72 65 67 61 72 *ppBlob.** regar
37740 64 6c 65 73 73 20 6f 66 20 74 68 65 20 73 75 63 dless of the suc
37750 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72 65 20 cess or failure
37760 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e of this routine.
37770 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f .**.** If the ro
37780 77 20 74 68 61 74 20 61 20 42 4c 4f 42 20 68 61 w that a BLOB ha
37790 6e 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 ndle points to i
377a0 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e s modified by an
377b0 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20 5b 44 .** [UPDATE], [D
377c0 45 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20 5b 4f ELETE], or by [O
377d0 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64 65 N CONFLICT] side
377e0 2d 65 66 66 65 63 74 73 0a 2a 2a 20 74 68 65 6e -effects.** then
377f0 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 the BLOB handle
37800 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65 is marked as "e
37810 78 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68 69 73 xpired"..** This
37820 20 69 73 20 74 72 75 65 20 69 66 20 61 6e 79 20 is true if any
37830 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 6f column of the ro
37840 77 20 69 73 20 63 68 61 6e 67 65 64 2c 20 65 76 w is changed, ev
37850 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f en a column.** o
37860 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e ther than the on
37870 65 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c e the BLOB handl
37880 65 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a 2a 2a e is open on..**
37890 20 43 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 Calls to [sqlit
378a0 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 e3_blob_read()]
378b0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f and [sqlite3_blo
378c0 62 5f 77 72 69 74 65 28 29 5d 20 66 6f 72 0a 2a b_write()] for.*
378d0 2a 20 61 20 65 78 70 69 72 65 64 20 42 4c 4f 42 * a expired BLOB
378e0 20 68 61 6e 64 6c 65 20 66 61 69 6c 20 77 69 74 handle fail wit
378f0 68 20 61 6e 20 72 65 74 75 72 6e 20 63 6f 64 65 h an return code
37900 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 of [SQLITE_ABOR
37910 54 5d 2e 0a 2a 2a 20 43 68 61 6e 67 65 73 20 77 T]..** Changes w
37920 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 42 4c ritten into a BL
37930 4f 42 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 OB prior to the
37940 42 4c 4f 42 20 65 78 70 69 72 69 6e 67 20 61 72 BLOB expiring ar
37950 65 20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 e not.** rollbac
37960 6b 20 62 79 20 74 68 65 20 65 78 70 69 72 61 74 k by the expirat
37970 69 6f 6e 20 6f 66 20 74 68 65 20 42 4c 4f 42 2e ion of the BLOB.
37980 20 20 53 75 63 68 20 63 68 61 6e 67 65 73 20 77 Such changes w
37990 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a ill eventually.*
379a0 2a 20 63 6f 6d 6d 69 74 20 69 66 20 74 68 65 20 * commit if the
379b0 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6e 74 transaction cont
379c0 69 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 inues to complet
379d0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 ion..**.** Use t
379e0 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 he [sqlite3_blob
379f0 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66 _bytes()] interf
37a00 61 63 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 ace to determine
37a10 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 the size of.**
37a20 74 68 65 20 6f 70 65 6e 65 64 20 62 6c 6f 62 2e the opened blob.
37a30 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20 The size of a
37a40 62 6c 6f 62 20 6d 61 79 20 6e 6f 74 20 62 65 20 blob may not be
37a50 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 0a changed by this.
37a60 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 20 55 ** interface. U
37a70 73 65 20 74 68 65 20 5b 55 50 44 41 54 45 5d 20 se the [UPDATE]
37a80 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 63 SQL command to c
37a90 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f hange the size o
37aa0 66 20 61 0a 2a 2a 20 62 6c 6f 62 2e 0a 2a 2a 0a f a.** blob..**.
37ab0 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f ** The [sqlite3_
37ac0 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d bind_zeroblob()]
37ad0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 and [sqlite3_re
37ae0 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d sult_zeroblob()]
37af0 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 61 interfaces.** a
37b00 6e 64 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 nd the built-in
37b10 5b 7a 65 72 6f 62 6c 6f 62 5d 20 53 51 4c 20 66 [zeroblob] SQL f
37b20 75 6e 63 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 unction can be u
37b30 73 65 64 2c 20 69 66 20 64 65 73 69 72 65 64 2c sed, if desired,
37b40 0a 2a 2a 20 74 6f 20 63 72 65 61 74 65 20 61 6e .** to create an
37b50 20 65 6d 70 74 79 2c 20 7a 65 72 6f 2d 66 69 6c empty, zero-fil
37b60 6c 65 64 20 62 6c 6f 62 20 69 6e 20 77 68 69 63 led blob in whic
37b70 68 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 h to read or wri
37b80 74 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68 69 73 te using.** this
37b90 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a interface..**.*
37ba0 2a 20 54 6f 20 61 76 6f 69 64 20 61 20 72 65 73 * To avoid a res
37bb0 6f 75 72 63 65 20 6c 65 61 6b 2c 20 65 76 65 72 ource leak, ever
37bc0 79 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e y open [BLOB han
37bd0 64 6c 65 5d 20 73 68 6f 75 6c 64 20 65 76 65 6e dle] should even
37be0 74 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 72 65 6c tually.** be rel
37bf0 65 61 73 65 64 20 62 79 20 61 20 63 61 6c 6c 20 eased by a call
37c00 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 to [sqlite3_blob
37c10 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a _close()]..**.**
37c20 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
37c30 2a 20 5b 48 31 37 38 31 33 5d 20 5b 48 31 37 38 * [H17813] [H178
37c40 31 34 5d 20 5b 48 31 37 38 31 36 5d 20 5b 48 31 14] [H17816] [H1
37c50 37 38 31 39 5d 20 5b 48 31 37 38 32 31 5d 20 5b 7819] [H17821] [
37c60 48 31 37 38 32 34 5d 0a 2a 2f 0a 53 51 4c 49 54 H17824].*/.SQLIT
37c70 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
37c80 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 3_blob_open(. s
37c90 71 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e 73 74 qlite3*,. const
37ca0 20 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20 63 6f char *zDb,. co
37cb0 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 nst char *zTable
37cc0 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ,. const char *
37cd0 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69 74 zColumn,. sqlit
37ce0 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20 e3_int64 iRow,.
37cf0 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 73 71 int flags,. sq
37d00 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 lite3_blob **ppB
37d10 6c 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 lob.);../*.** CA
37d20 50 49 33 52 45 46 3a 20 43 6c 6f 73 65 20 41 20 PI3REF: Close A
37d30 42 4c 4f 42 20 48 61 6e 64 6c 65 20 7b 48 31 37 BLOB Handle {H17
37d40 38 33 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 830} <S30230>.**
37d50 0a 2a 2a 20 43 6c 6f 73 65 73 20 61 6e 20 6f 70 .** Closes an op
37d60 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d en [BLOB handle]
37d70 2e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 ..**.** Closing
37d80 61 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63 61 75 a BLOB shall cau
37d90 73 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 se the current t
37da0 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f ransaction to co
37db0 6d 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65 72 65 mmit.** if there
37dc0 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 42 4c are no other BL
37dd0 4f 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e 67 20 OBs, no pending
37de0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
37df0 6e 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 nts, and the.**
37e00 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
37e10 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f 63 ion is in [autoc
37e20 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 20 ommit mode]..**
37e30 49 66 20 61 6e 79 20 77 72 69 74 65 73 20 77 65 If any writes we
37e40 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 42 re made to the B
37e50 4c 4f 42 2c 20 74 68 65 79 20 6d 69 67 68 74 20 LOB, they might
37e60 62 65 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 be held in cache
37e70 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 6c .** until the cl
37e80 6f 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 66 ose operation if
37e90 20 74 68 65 79 20 77 69 6c 6c 20 66 69 74 2e 0a they will fit..
37ea0 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 74 68 **.** Closing th
37eb0 65 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66 6f 72 e BLOB often for
37ec0 63 65 73 20 74 68 65 20 63 68 61 6e 67 65 73 0a ces the changes.
37ed0 2a 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61 ** out to disk a
37ee0 6e 64 20 73 6f 20 69 66 20 61 6e 79 20 49 2f 4f nd so if any I/O
37ef0 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74 errors occur, t
37f00 68 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 hey will likely
37f10 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 occur.** at the
37f20 74 69 6d 65 20 77 68 65 6e 20 74 68 65 20 42 4c time when the BL
37f30 4f 42 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 41 OB is closed. A
37f40 6e 79 20 65 72 72 6f 72 73 20 74 68 61 74 20 6f ny errors that o
37f50 63 63 75 72 20 64 75 72 69 6e 67 0a 2a 2a 20 63 ccur during.** c
37f60 6c 6f 73 69 6e 67 20 61 72 65 20 72 65 70 6f 72 losing are repor
37f70 74 65 64 20 61 73 20 61 20 6e 6f 6e 2d 7a 65 72 ted as a non-zer
37f80 6f 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e 0a o return value..
37f90 2a 2a 0a 2a 2a 20 54 68 65 20 42 4c 4f 42 20 69 **.** The BLOB i
37fa0 73 20 63 6c 6f 73 65 64 20 75 6e 63 6f 6e 64 69 s closed uncondi
37fb0 74 69 6f 6e 61 6c 6c 79 2e 20 20 45 76 65 6e 20 tionally. Even
37fc0 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 if this routine
37fd0 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 20 65 72 returns.** an er
37fe0 72 6f 72 20 63 6f 64 65 2c 20 74 68 65 20 42 4c ror code, the BL
37ff0 4f 42 20 69 73 20 73 74 69 6c 6c 20 63 6c 6f 73 OB is still clos
38000 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e ed..**.** Callin
38010 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 g this routine w
38020 69 74 68 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 ith a null point
38030 65 72 20 28 77 68 69 63 68 20 61 73 20 77 6f 75 er (which as wou
38040 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 0a 2a ld be returned.*
38050 2a 20 62 79 20 66 61 69 6c 65 64 20 63 61 6c 6c * by failed call
38060 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f to [sqlite3_blo
38070 62 5f 6f 70 65 6e 28 29 5d 29 20 69 73 20 61 20 b_open()]) is a
38080 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a harmless no-op..
38090 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
380a0 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 33 33 5d 20 ts:.** [H17833]
380b0 5b 48 31 37 38 33 36 5d 20 5b 48 31 37 38 33 39 [H17836] [H17839
380c0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
380d0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
380e0 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 62 _close(sqlite3_b
380f0 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 lob *);../*.** C
38100 41 50 49 33 52 45 46 3a 20 52 65 74 75 72 6e 20 API3REF: Return
38110 54 68 65 20 53 69 7a 65 20 4f 66 20 41 6e 20 4f The Size Of An O
38120 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 34 30 pen BLOB {H17840
38130 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a } <S30230>.**.**
38140 20 52 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a Returns the siz
38150 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 e in bytes of th
38160 65 20 42 4c 4f 42 20 61 63 63 65 73 73 69 62 6c e BLOB accessibl
38170 65 20 76 69 61 20 74 68 65 20 0a 2a 2a 20 73 75 e via the .** su
38180 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 ccessfully opene
38190 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 d [BLOB handle]
381a0 69 6e 20 69 74 73 20 6f 6e 6c 79 20 61 72 67 75 in its only argu
381b0 6d 65 6e 74 2e 20 20 54 68 65 0a 2a 2a 20 69 6e ment. The.** in
381c0 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 49 cremental blob I
381d0 2f 4f 20 72 6f 75 74 69 6e 65 73 20 63 61 6e 20 /O routines can
381e0 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20 6f 76 65 only read or ove
381f0 72 77 72 69 74 69 6e 67 20 65 78 69 73 74 69 6e rwriting existin
38200 67 0a 2a 2a 20 62 6c 6f 62 20 63 6f 6e 74 65 6e g.** blob conten
38210 74 3b 20 74 68 65 79 20 63 61 6e 6e 6f 74 20 63 t; they cannot c
38220 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f hange the size o
38230 66 20 61 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 f a blob..**.**
38240 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c This routine onl
38250 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c y works on a [BL
38260 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 OB handle] which
38270 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 has been create
38280 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 d.** by a prior
38290 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 successful call
382a0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 to [sqlite3_blob
382b0 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 _open()] and whi
382c0 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 ch has not.** be
382d0 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 en closed by [sq
382e0 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 lite3_blob_close
382f0 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e ()]. Passing an
38300 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 y other pointer
38310 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f in.** to this ro
38320 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e utine results in
38330 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70 undefined and p
38340 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61 robably undesira
38350 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a ble behavior..**
38360 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
38370 3a 0a 2a 2a 20 5b 48 31 37 38 34 33 5d 0a 2a 2f :.** [H17843].*/
38380 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
38390 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 sqlite3_blob_byt
383a0 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 es(sqlite3_blob
383b0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
383c0 52 45 46 3a 20 52 65 61 64 20 44 61 74 61 20 46 REF: Read Data F
383d0 72 6f 6d 20 41 20 42 4c 4f 42 20 49 6e 63 72 65 rom A BLOB Incre
383e0 6d 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38 35 30 mentally {H17850
383f0 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a } <S30230>.**.**
38400 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
38410 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64 s used to read d
38420 61 74 61 20 66 72 6f 6d 20 61 6e 20 6f 70 65 6e ata from an open
38430 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 [BLOB handle] i
38440 6e 74 6f 20 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d nto a.** caller-
38450 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e supplied buffer.
38460 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 N bytes of data
38470 20 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f are copied into
38480 20 62 75 66 66 65 72 20 5a 0a 2a 2a 20 66 72 6f buffer Z.** fro
38490 6d 20 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c m the open BLOB,
384a0 20 73 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66 starting at off
384b0 73 65 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a set iOffset..**.
384c0 2a 2a 20 49 66 20 6f 66 66 73 65 74 20 69 4f 66 ** If offset iOf
384d0 66 73 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 fset is less tha
384e0 6e 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 n N bytes from t
384f0 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c he end of the BL
38500 4f 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 OB,.** [SQLITE_E
38510 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 RROR] is returne
38520 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 d and no data is
38530 20 72 65 61 64 2e 20 20 49 66 20 4e 20 6f 72 20 read. If N or
38540 69 4f 66 66 73 65 74 20 69 73 0a 2a 2a 20 6c 65 iOffset is.** le
38550 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 5b 53 ss than zero, [S
38560 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 QLITE_ERROR] is
38570 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 returned and no
38580 64 61 74 61 20 69 73 20 72 65 61 64 2e 0a 2a 2a data is read..**
38590 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 The size of the
385a0 20 62 6c 6f 62 20 28 61 6e 64 20 68 65 6e 63 65 blob (and hence
385b0 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c the maximum val
385c0 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65 74 29 ue of N+iOffset)
385d0 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65 74 65 72 .** can be deter
385e0 6d 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 20 mined using the
385f0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 [sqlite3_blob_by
38600 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 tes()] interface
38610 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74 65 6d ..**.** An attem
38620 70 74 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 pt to read from
38630 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c 4f 42 an expired [BLOB
38640 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 20 77 handle] fails w
38650 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 ith an.** error
38660 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f code of [SQLITE_
38670 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 4f 6e ABORT]..**.** On
38680 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54 45 success, SQLITE
38690 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
386a0 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 .** Otherwise, a
386b0 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f n [error code] o
386c0 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 64 20 65 r an [extended e
386d0 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 rror code] is re
386e0 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 turned..**.** Th
386f0 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 is routine only
38700 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 works on a [BLOB
38710 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68 handle] which h
38720 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 0a as been created.
38730 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73 75 ** by a prior su
38740 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f ccessful call to
38750 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f [sqlite3_blob_o
38760 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68 pen()] and which
38770 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e has not.** been
38780 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c 69 closed by [sqli
38790 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 te3_blob_close()
387a0 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e 79 20 ]. Passing any
387b0 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 69 6e other pointer in
387c0 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 74 .** to this rout
387d0 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e 20 75 ine results in u
387e0 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70 72 6f ndefined and pro
387f0 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61 62 6c bably undesirabl
38800 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a e behavior..**.*
38810 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c * See also: [sql
38820 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 ite3_blob_write(
38830 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 )]..**.** Requir
38840 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 ements:.** [H178
38850 35 33 5d 20 5b 48 31 37 38 35 36 5d 20 5b 48 31 53] [H17856] [H1
38860 37 38 35 39 5d 20 5b 48 31 37 38 36 32 5d 20 5b 7859] [H17862] [
38870 48 31 37 38 36 33 5d 20 5b 48 31 37 38 36 35 5d H17863] [H17865]
38880 20 5b 48 31 37 38 36 38 5d 0a 2a 2f 0a 53 51 4c [H17868].*/.SQL
38890 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
388a0 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 te3_blob_read(sq
388b0 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 76 6f lite3_blob *, vo
388c0 69 64 20 2a 5a 2c 20 69 6e 74 20 4e 2c 20 69 6e id *Z, int N, in
388d0 74 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a t iOffset);../*.
388e0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 57 72 69 ** CAPI3REF: Wri
388f0 74 65 20 44 61 74 61 20 49 6e 74 6f 20 41 20 42 te Data Into A B
38900 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c LOB Incrementall
38910 79 20 7b 48 31 37 38 37 30 7d 20 3c 53 33 30 32 y {H17870} <S302
38920 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 30>.**.** This f
38930 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 unction is used
38940 74 6f 20 77 72 69 74 65 20 64 61 74 61 20 69 6e to write data in
38950 74 6f 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 to an open [BLOB
38960 20 68 61 6e 64 6c 65 5d 20 66 72 6f 6d 20 61 0a handle] from a.
38970 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69 ** caller-suppli
38980 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62 79 74 ed buffer. N byt
38990 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 es of data are c
389a0 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62 opied from the b
389b0 75 66 66 65 72 20 5a 0a 2a 2a 20 69 6e 74 6f 20 uffer Z.** into
389c0 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 the open BLOB, s
389d0 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 tarting at offse
389e0 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a t iOffset..**.**
389f0 20 49 66 20 74 68 65 20 5b 42 4c 4f 42 20 68 61 If the [BLOB ha
38a00 6e 64 6c 65 5d 20 70 61 73 73 65 64 20 61 73 20 ndle] passed as
38a10 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
38a20 6e 74 20 77 61 73 20 6e 6f 74 20 6f 70 65 6e 65 nt was not opene
38a30 64 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 d for.** writing
38a40 20 28 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 (the flags para
38a50 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 meter to [sqlite
38a60 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 77 3_blob_open()] w
38a70 61 73 20 7a 65 72 6f 29 2c 0a 2a 2a 20 74 68 69 as zero),.** thi
38a80 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
38a90 6e 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f ns [SQLITE_READO
38aa0 4e 4c 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 NLY]..**.** This
38ab0 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e function may on
38ac0 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 63 6f ly modify the co
38ad0 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 42 4c ntents of the BL
38ae0 4f 42 3b 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 OB; it is.** not
38af0 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e 63 possible to inc
38b00 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f rease the size o
38b10 66 20 61 20 42 4c 4f 42 20 75 73 69 6e 67 20 74 f a BLOB using t
38b20 68 69 73 20 41 50 49 2e 0a 2a 2a 20 49 66 20 6f his API..** If o
38b30 66 66 73 65 74 20 69 4f 66 66 73 65 74 20 69 73 ffset iOffset is
38b40 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74 less than N byt
38b50 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 es from the end
38b60 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 of the BLOB,.**
38b70 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 [SQLITE_ERROR] i
38b80 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e s returned and n
38b90 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65 o data is writte
38ba0 6e 2e 20 20 49 66 20 4e 20 69 73 0a 2a 2a 20 6c n. If N is.** l
38bb0 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 20 5b 53 ess than zero [S
38bc0 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 QLITE_ERROR] is
38bd0 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 returned and no
38be0 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 2e data is written.
38bf0 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 .** The size of
38c00 74 68 65 20 42 4c 4f 42 20 28 61 6e 64 20 68 65 the BLOB (and he
38c10 6e 63 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 nce the maximum
38c20 76 61 6c 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73 value of N+iOffs
38c30 65 74 29 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65 et).** can be de
38c40 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20 74 termined using t
38c50 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 he [sqlite3_blob
38c60 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66 _bytes()] interf
38c70 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 ace..**.** An at
38c80 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 74 tempt to write t
38c90 6f 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c o an expired [BL
38ca0 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 OB handle] fails
38cb0 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f with an.** erro
38cc0 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 r code of [SQLIT
38cd0 45 5f 41 42 4f 52 54 5d 2e 20 20 57 72 69 74 65 E_ABORT]. Write
38ce0 73 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 74 68 s to the BLOB th
38cf0 61 74 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20 62 at occurred.** b
38d00 65 66 6f 72 65 20 74 68 65 20 5b 42 4c 4f 42 20 efore the [BLOB
38d10 68 61 6e 64 6c 65 5d 20 65 78 70 69 72 65 64 20 handle] expired
38d20 61 72 65 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 are not rolled b
38d30 61 63 6b 20 62 79 20 74 68 65 0a 2a 2a 20 65 78 ack by the.** ex
38d40 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 piration of the
38d50 68 61 6e 64 6c 65 2c 20 74 68 6f 75 67 68 20 6f handle, though o
38d60 66 20 63 6f 75 72 73 65 20 74 68 6f 73 65 20 63 f course those c
38d70 68 61 6e 67 65 73 20 6d 69 67 68 74 0a 2a 2a 20 hanges might.**
38d80 68 61 76 65 20 62 65 65 6e 20 6f 76 65 72 77 72 have been overwr
38d90 69 74 74 65 6e 20 62 79 20 74 68 65 20 73 74 61 itten by the sta
38da0 74 65 6d 65 6e 74 20 74 68 61 74 20 65 78 70 69 tement that expi
38db0 72 65 64 20 74 68 65 20 42 4c 4f 42 20 68 61 6e red the BLOB han
38dc0 64 6c 65 0a 2a 2a 20 6f 72 20 62 79 20 6f 74 68 dle.** or by oth
38dd0 65 72 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 73 er independent s
38de0 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a tatements..**.**
38df0 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c On success, SQL
38e00 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
38e10 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 ed..** Otherwise
38e20 2c 20 61 6e 20 20 5b 65 72 72 6f 72 20 63 6f 64 , an [error cod
38e30 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64 e] or an [extend
38e40 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 ed error code] i
38e50 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
38e60 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f * This routine o
38e70 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b nly works on a [
38e80 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 BLOB handle] whi
38e90 63 68 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 ch has been crea
38ea0 74 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f ted.** by a prio
38eb0 72 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c r successful cal
38ec0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c l to [sqlite3_bl
38ed0 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 ob_open()] and w
38ee0 68 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 hich has not.**
38ef0 62 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b been closed by [
38f00 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f sqlite3_blob_clo
38f10 73 65 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 se()]. Passing
38f20 61 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 any other pointe
38f30 72 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 r in.** to this
38f40 72 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20 routine results
38f50 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 in undefined and
38f60 20 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 probably undesi
38f70 72 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a rable behavior..
38f80 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
38f90 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 [sqlite3_blob_re
38fa0 61 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 ad()]..**.** Req
38fb0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
38fc0 31 37 38 37 33 5d 20 5b 48 31 37 38 37 34 5d 20 17873] [H17874]
38fd0 5b 48 31 37 38 37 35 5d 20 5b 48 31 37 38 37 36 [H17875] [H17876
38fe0 5d 20 5b 48 31 37 38 37 37 5d 20 5b 48 31 37 38 ] [H17877] [H178
38ff0 37 39 5d 20 5b 48 31 37 38 38 32 5d 20 5b 48 31 79] [H17882] [H1
39000 37 38 38 35 5d 0a 2a 2a 20 5b 48 31 37 38 38 38 7885].** [H17888
39010 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
39020 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
39030 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62 _write(sqlite3_b
39040 6c 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 lob *, const voi
39050 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 d *z, int n, int
39060 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a iOffset);../*.*
39070 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 * CAPI3REF: Virt
39080 75 61 6c 20 46 69 6c 65 20 53 79 73 74 65 6d 20 ual File System
39090 4f 62 6a 65 63 74 73 20 7b 48 31 31 32 30 30 7d Objects {H11200}
390a0 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20100>.**.**
390b0 41 20 76 69 72 74 75 61 6c 20 66 69 6c 65 73 79 A virtual filesy
390c0 73 74 65 6d 20 28 56 46 53 29 20 69 73 20 61 6e stem (VFS) is an
390d0 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f [sqlite3_vfs] o
390e0 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 53 51 bject.** that SQ
390f0 4c 69 74 65 20 75 73 65 73 20 74 6f 20 69 6e 74 Lite uses to int
39100 65 72 61 63 74 0a 2a 2a 20 77 69 74 68 20 74 68 eract.** with th
39110 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 e underlying ope
39120 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 rating system.
39130 4d 6f 73 74 20 53 51 4c 69 74 65 20 62 75 69 6c Most SQLite buil
39140 64 73 20 63 6f 6d 65 20 77 69 74 68 20 61 0a 2a ds come with a.*
39150 2a 20 73 69 6e 67 6c 65 20 64 65 66 61 75 6c 74 * single default
39160 20 56 46 53 20 74 68 61 74 20 69 73 20 61 70 70 VFS that is app
39170 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 ropriate for the
39180 20 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e 0a host computer..
39190 2a 2a 20 4e 65 77 20 56 46 53 65 73 20 63 61 6e ** New VFSes can
391a0 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 61 be registered a
391b0 6e 64 20 65 78 69 73 74 69 6e 67 20 56 46 53 65 nd existing VFSe
391c0 73 20 63 61 6e 20 62 65 20 75 6e 72 65 67 69 73 s can be unregis
391d0 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 6f tered..** The fo
391e0 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63 llowing interfac
391f0 65 73 20 61 72 65 20 70 72 6f 76 69 64 65 64 2e es are provided.
39200 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
39210 65 33 5f 76 66 73 5f 66 69 6e 64 28 29 20 69 6e e3_vfs_find() in
39220 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
39230 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 56 a pointer to a V
39240 46 53 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d FS given its nam
39250 65 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 e..** Names are
39260 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 0a case sensitive..
39270 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 7a 65 72 ** Names are zer
39280 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 o-terminated UTF
39290 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 49 -8 strings..** I
392a0 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 f there is no ma
392b0 74 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f 69 6e tch, a NULL poin
392c0 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e ter is returned.
392d0 0a 2a 2a 20 49 66 20 7a 56 66 73 4e 61 6d 65 20 .** If zVfsName
392e0 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 is NULL then the
392f0 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 default VFS is
39300 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
39310 4e 65 77 20 56 46 53 65 73 20 61 72 65 20 72 65 New VFSes are re
39320 67 69 73 74 65 72 65 64 20 77 69 74 68 20 73 71 gistered with sq
39330 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 lite3_vfs_regist
39340 65 72 28 29 2e 0a 2a 2a 20 45 61 63 68 20 6e 65 er()..** Each ne
39350 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 68 w VFS becomes th
39360 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 66 e default VFS if
39370 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66 6c the makeDflt fl
39380 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 54 68 ag is set..** Th
39390 65 20 73 61 6d 65 20 56 46 53 20 63 61 6e 20 62 e same VFS can b
393a0 65 20 72 65 67 69 73 74 65 72 65 64 20 6d 75 6c e registered mul
393b0 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74 68 tiple times with
393c0 6f 75 74 20 69 6e 6a 75 72 79 2e 0a 2a 2a 20 54 out injury..** T
393d0 6f 20 6d 61 6b 65 20 61 6e 20 65 78 69 73 74 69 o make an existi
393e0 6e 67 20 56 46 53 20 69 6e 74 6f 20 74 68 65 20 ng VFS into the
393f0 64 65 66 61 75 6c 74 20 56 46 53 2c 20 72 65 67 default VFS, reg
39400 69 73 74 65 72 20 69 74 20 61 67 61 69 6e 0a 2a ister it again.*
39410 2a 20 77 69 74 68 20 74 68 65 20 6d 61 6b 65 44 * with the makeD
39420 66 6c 74 20 66 6c 61 67 20 73 65 74 2e 20 20 49 flt flag set. I
39430 66 20 74 77 6f 20 64 69 66 66 65 72 65 6e 74 20 f two different
39440 56 46 53 65 73 20 77 69 74 68 20 74 68 65 0a 2a VFSes with the.*
39450 2a 20 73 61 6d 65 20 6e 61 6d 65 20 61 72 65 20 * same name are
39460 72 65 67 69 73 74 65 72 65 64 2c 20 74 68 65 20 registered, the
39470 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 behavior is unde
39480 66 69 6e 65 64 2e 20 20 49 66 20 61 0a 2a 2a 20 fined. If a.**
39490 56 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65 VFS is registere
394a0 64 20 77 69 74 68 20 61 20 6e 61 6d 65 20 74 68 d with a name th
394b0 61 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61 6e at is NULL or an
394c0 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 0a 2a empty string,.*
394d0 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 * then the behav
394e0 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ior is undefined
394f0 2e 0a 2a 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74 ..**.** Unregist
39500 65 72 20 61 20 56 46 53 20 77 69 74 68 20 74 68 er a VFS with th
39510 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e e sqlite3_vfs_un
39520 72 65 67 69 73 74 65 72 28 29 20 69 6e 74 65 72 register() inter
39530 66 61 63 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 face..** If the
39540 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 75 default VFS is u
39550 6e 72 65 67 69 73 74 65 72 65 64 2c 20 61 6e 6f nregistered, ano
39560 74 68 65 72 20 56 46 53 20 69 73 20 63 68 6f 73 ther VFS is chos
39570 65 6e 20 61 73 0a 2a 2a 20 74 68 65 20 64 65 66 en as.** the def
39580 61 75 6c 74 2e 20 20 54 68 65 20 63 68 6f 69 63 ault. The choic
39590 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 56 46 e for the new VF
395a0 53 20 69 73 20 61 72 62 69 74 72 61 72 79 2e 0a S is arbitrary..
395b0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
395c0 74 73 3a 0a 2a 2a 20 5b 48 31 31 32 30 33 5d 20 ts:.** [H11203]
395d0 5b 48 31 31 32 30 36 5d 20 5b 48 31 31 32 30 39 [H11206] [H11209
395e0 5d 20 5b 48 31 31 32 31 32 5d 20 5b 48 31 31 32 ] [H11212] [H112
395f0 31 35 5d 20 5b 48 31 31 32 31 38 5d 0a 2a 2f 0a 15] [H11218].*/.
39600 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
39610 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f e3_vfs *sqlite3_
39620 76 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 vfs_find(const c
39630 68 61 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a har *zVfsName);.
39640 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
39650 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 qlite3_vfs_regis
39660 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a ter(sqlite3_vfs*
39670 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 3b , int makeDflt);
39680 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
39690 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 sqlite3_vfs_unre
396a0 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 gister(sqlite3_v
396b0 66 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 fs*);../*.** CAP
396c0 49 33 52 45 46 3a 20 4d 75 74 65 78 65 73 20 7b I3REF: Mutexes {
396d0 48 31 37 30 30 30 7d 20 3c 53 32 30 30 30 30 3e H17000} <S20000>
396e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 .**.** The SQLit
396f0 65 20 63 6f 72 65 20 75 73 65 73 20 74 68 65 73 e core uses thes
39700 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 74 e routines for t
39710 68 72 65 61 64 0a 2a 2a 20 73 79 6e 63 68 72 6f hread.** synchro
39720 6e 69 7a 61 74 69 6f 6e 2e 20 54 68 6f 75 67 68 nization. Though
39730 20 74 68 65 79 20 61 72 65 20 69 6e 74 65 6e 64 they are intend
39740 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a ed for internal.
39750 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 ** use by SQLite
39760 2c 20 63 6f 64 65 20 74 68 61 74 20 6c 69 6e 6b , code that link
39770 73 20 61 67 61 69 6e 73 74 20 53 51 4c 69 74 65 s against SQLite
39780 20 69 73 0a 2a 2a 20 70 65 72 6d 69 74 74 65 64 is.** permitted
39790 20 74 6f 20 75 73 65 20 61 6e 79 20 6f 66 20 74 to use any of t
397a0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a hese routines..*
397b0 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 *.** The SQLite
397c0 73 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6e 74 source code cont
397d0 61 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20 69 6d ains multiple im
397e0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a plementations.**
397f0 20 6f 66 20 74 68 65 73 65 20 6d 75 74 65 78 20 of these mutex
39800 72 6f 75 74 69 6e 65 73 2e 20 20 41 6e 20 61 70 routines. An ap
39810 70 72 6f 70 72 69 61 74 65 20 69 6d 70 6c 65 6d propriate implem
39820 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 entation.** is s
39830 65 6c 65 63 74 65 64 20 61 75 74 6f 6d 61 74 69 elected automati
39840 63 61 6c 6c 79 20 61 74 20 63 6f 6d 70 69 6c 65 cally at compile
39850 2d 74 69 6d 65 2e 20 20 54 68 65 20 66 6f 6c 6c -time. The foll
39860 6f 77 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65 owing.** impleme
39870 6e 74 61 74 69 6f 6e 73 20 61 72 65 20 61 76 61 ntations are ava
39880 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 53 51 ilable in the SQ
39890 4c 69 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a Lite core:.**.**
398a0 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20 <ul>.** <li>
398b0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 SQLITE_MUTEX_OS2
398c0 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 .** <li> SQLIT
398d0 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 0a E_MUTEX_PTHREAD.
398e0 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 ** <li> SQLITE
398f0 5f 4d 55 54 45 58 5f 57 33 32 0a 2a 2a 20 3c 6c _MUTEX_W32.** <l
39900 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 i> SQLITE_MUTE
39910 58 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a X_NOOP.** </ul>.
39920 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 **.** The SQLITE
39930 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 69 6d 70 6c _MUTEX_NOOP impl
39940 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 20 ementation is a
39950 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 0a set of routines.
39960 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 ** that does no
39970 72 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 real locking and
39980 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 is appropriate
39990 66 6f 72 20 75 73 65 20 69 6e 0a 2a 2a 20 61 20 for use in.** a
399a0 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 single-threaded
399b0 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54 68 application. Th
399c0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f e SQLITE_MUTEX_O
399d0 53 32 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 S2,.** SQLITE_MU
399e0 54 45 58 5f 50 54 48 52 45 41 44 2c 20 61 6e 64 TEX_PTHREAD, and
399f0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 SQLITE_MUTEX_W3
39a00 32 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2 implementation
39a10 73 0a 2a 2a 20 61 72 65 20 61 70 70 72 6f 70 72 s.** are appropr
39a20 69 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 iate for use on
39a30 4f 53 2f 32 2c 20 55 6e 69 78 2c 20 61 6e 64 20 OS/2, Unix, and
39a40 57 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 Windows..**.** I
39a50 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 f SQLite is comp
39a60 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51 iled with the SQ
39a70 4c 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 45 LITE_MUTEX_APPDE
39a80 46 20 70 72 65 70 72 6f 63 65 73 73 6f 72 0a 2a F preprocessor.*
39a90 2a 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 * macro defined
39aa0 28 77 69 74 68 20 22 2d 44 53 51 4c 49 54 45 5f (with "-DSQLITE_
39ab0 4d 55 54 45 58 5f 41 50 50 44 45 46 3d 31 22 29 MUTEX_APPDEF=1")
39ac0 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 0a , then no mutex.
39ad0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
39ae0 6e 20 69 73 20 69 6e 63 6c 75 64 65 64 20 77 69 n is included wi
39af0 74 68 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 th the library.
39b00 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
39b10 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 .** application
39b20 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20 63 75 must supply a cu
39b30 73 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c 65 stom mutex imple
39b40 6d 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67 20 mentation using
39b50 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 the.** [SQLITE_C
39b60 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74 ONFIG_MUTEX] opt
39b70 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ion of the sqlit
39b80 65 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e 63 e3_config() func
39b90 74 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20 63 tion.** before c
39ba0 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 alling sqlite3_i
39bb0 6e 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 20 61 nitialize() or a
39bc0 6e 79 20 6f 74 68 65 72 20 70 75 62 6c 69 63 20 ny other public
39bd0 73 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e 63 sqlite3_.** func
39be0 74 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73 20 tion that calls
39bf0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
39c00 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 ze()..**.** {H17
39c10 30 31 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 011} The sqlite3
39c20 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 _mutex_alloc() r
39c30 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 outine allocates
39c40 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 a new.** mutex
39c50 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f and returns a po
39c60 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 7b 48 31 inter to it. {H1
39c70 37 30 31 32 7d 20 49 66 20 69 74 20 72 65 74 75 7012} If it retu
39c80 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 rns NULL.** that
39c90 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 means that a mu
39ca0 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 tex could not be
39cb0 20 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 48 31 37 allocated. {H17
39cc0 30 31 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 20 77 013} SQLite.** w
39cd0 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 ill unwind its s
39ce0 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 tack and return
39cf0 61 6e 20 65 72 72 6f 72 2e 20 7b 48 31 37 30 31 an error. {H1701
39d00 34 7d 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 4} The argument.
39d10 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 ** to sqlite3_mu
39d20 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f tex_alloc() is o
39d30 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 ne of these inte
39d40 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a ger constants:.*
39d50 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ul>.** <li
39d60 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
39d70 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 FAST.** <li> SQ
39d80 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
39d90 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 SIVE.** <li> SQ
39da0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
39db0 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e C_MASTER.** <li>
39dc0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
39dd0 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 TATIC_MEM.** <li
39de0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
39df0 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c STATIC_MEM2.** <
39e00 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
39e10 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a X_STATIC_PRNG.**
39e20 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
39e30 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a TEX_STATIC_LRU.*
39e40 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
39e50 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 UTEX_STATIC_LRU2
39e60 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 .** </ul>.**.**
39e70 7b 48 31 37 30 31 35 7d 20 54 68 65 20 66 69 72 {H17015} The fir
39e80 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 st two constants
39e90 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d cause sqlite3_m
39ea0 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 utex_alloc() to
39eb0 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 create.** a new
39ec0 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 mutex. The new
39ed0 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 mutex is recursi
39ee0 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d ve when SQLITE_M
39ef0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a UTEX_RECURSIVE.*
39f00 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f * is used but no
39f10 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f t necessarily so
39f20 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 when SQLITE_MUT
39f30 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e EX_FAST is used.
39f40 20 7b 45 4e 44 7d 0a 2a 2a 20 54 68 65 20 6d 75 {END}.** The mu
39f50 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 tex implementati
39f60 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 on does not need
39f70 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 to make a disti
39f80 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 nction.** betwee
39f90 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 n SQLITE_MUTEX_R
39fa0 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c ECURSIVE and SQL
39fb0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 ITE_MUTEX_FAST i
39fc0 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 f it does.** not
39fd0 20 77 61 6e 74 20 74 6f 2e 20 20 7b 48 31 37 30 want to. {H170
39fe0 31 36 7d 20 42 75 74 20 53 51 4c 69 74 65 20 77 16} But SQLite w
39ff0 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 ill only request
3a000 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 a recursive mut
3a010 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 ex in.** cases w
3a020 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e here it really n
3a030 65 65 64 73 20 6f 6e 65 2e 20 20 7b 45 4e 44 7d eeds one. {END}
3a040 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e If a faster non
3a050 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 -recursive mutex
3a060 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
3a070 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 on is available
3a080 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 on the host plat
3a090 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 form, the mutex
3a0a0 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 subsystem.** mig
3a0b0 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 ht return such a
3a0c0 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e mutex in respon
3a0d0 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 se to SQLITE_MUT
3a0e0 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b EX_FAST..**.** {
3a0f0 48 31 37 30 31 37 7d 20 54 68 65 20 6f 74 68 65 H17017} The othe
3a100 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 r allowed parame
3a110 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f ters to sqlite3_
3a120 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 mutex_alloc() ea
3a130 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 ch return.** a p
3a140 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 ointer to a stat
3a150 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d ic preexisting m
3a160 75 74 65 78 2e 20 7b 45 4e 44 7d 20 20 53 69 78 utex. {END} Six
3a170 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 static mutexes
3a180 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 are.** used by t
3a190 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 he current versi
3a1a0 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 on of SQLite. F
3a1b0 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f uture versions o
3a1c0 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 f SQLite.** may
3a1d0 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 add additional s
3a1e0 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20 tatic mutexes.
3a1f0 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 Static mutexes a
3a200 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a re for internal.
3a210 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 ** use by SQLite
3a220 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 only. Applicat
3a230 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51 ions that use SQ
3a240 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f Lite mutexes sho
3a250 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 uld.** use only
3a260 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 the dynamic mute
3a270 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 xes returned by
3a280 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
3a290 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d T or.** SQLITE_M
3a2a0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a UTEX_RECURSIVE..
3a2b0 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d 20 4e **.** {H17018} N
3a2c0 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 ote that if one
3a2d0 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d of the dynamic m
3a2e0 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 utex parameters
3a2f0 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 (SQLITE_MUTEX_FA
3a300 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f ST.** or SQLITE_
3a310 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 MUTEX_RECURSIVE)
3a320 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 is used then sq
3a330 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
3a340 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 c().** returns a
3a350 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 different mutex
3a360 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 on every call.
3a370 20 7b 48 31 37 30 33 34 7d 20 42 75 74 20 66 6f {H17034} But fo
3a380 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20 r the static.**
3a390 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 mutex types, the
3a3a0 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 same mutex is r
3a3b0 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 eturned on every
3a3c0 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a call that has.*
3a3d0 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 * the same type
3a3e0 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 number..**.** {H
3a3f0 31 37 30 31 39 7d 20 54 68 65 20 73 71 6c 69 74 17019} The sqlit
3a400 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 20 e3_mutex_free()
3a410 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 routine dealloca
3a420 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 tes a previously
3a430 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 64 79 .** allocated dy
3a440 6e 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b 48 31 namic mutex. {H1
3a450 37 30 32 30 7d 20 53 51 4c 69 74 65 20 69 73 20 7020} SQLite is
3a460 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c careful to deall
3a470 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 64 ocate every.** d
3a480 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 68 61 ynamic mutex tha
3a490 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 20 t it allocates.
3a4a0 7b 41 31 37 30 32 31 7d 20 54 68 65 20 64 79 6e {A17021} The dyn
3a4b0 61 6d 69 63 20 6d 75 74 65 78 65 73 20 6d 75 73 amic mutexes mus
3a4c0 74 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20 75 t not be in.** u
3a4d0 73 65 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 se when they are
3a4e0 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 41 deallocated. {A
3a4f0 31 37 30 32 32 7d 20 41 74 74 65 6d 70 74 69 6e 17022} Attemptin
3a500 67 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 g to deallocate
3a510 61 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65 a static.** mute
3a520 78 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 x results in und
3a530 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e efined behavior.
3a540 20 7b 48 31 37 30 32 33 7d 20 53 51 4c 69 74 65 {H17023} SQLite
3a550 20 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74 never deallocat
3a560 65 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 6d es.** a static m
3a570 75 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a utex. {END}.**.*
3a580 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
3a590 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 tex_enter() and
3a5a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
3a5b0 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 y() routines att
3a5c0 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 empt.** to enter
3a5d0 20 61 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32 a mutex. {H1702
3a5e0 34 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 4} If another th
3a5f0 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 read is already
3a600 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 within the mutex
3a610 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 ,.** sqlite3_mut
3a620 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 ex_enter() will
3a630 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 block and sqlite
3a640 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 3_mutex_try() wi
3a650 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c ll return.** SQL
3a660 49 54 45 5f 42 55 53 59 2e 20 7b 48 31 37 30 32 ITE_BUSY. {H1702
3a670 35 7d 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 5} The sqlite3_
3a680 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 mutex_try() inte
3a690 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 5b 53 rface returns [S
3a6a0 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f QLITE_OK].** upo
3a6b0 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 n successful ent
3a6c0 72 79 2e 20 20 7b 48 31 37 30 32 36 7d 20 4d 75 ry. {H17026} Mu
3a6d0 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 texes created us
3a6e0 69 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 ing.** SQLITE_MU
3a6f0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 TEX_RECURSIVE ca
3a700 6e 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c n be entered mul
3a710 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 tiple times by t
3a720 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 0a he same thread..
3a730 2a 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e 20 73 ** {H17027} In s
3a740 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a uch cases the,.*
3a750 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 * mutex must be
3a760 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 exited an equal
3a770 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 number of times
3a780 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 before another t
3a790 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 hread.** can ent
3a7a0 65 72 2e 20 20 7b 41 31 37 30 32 38 7d 20 49 66 er. {A17028} If
3a7b0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
3a7c0 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 tries to enter
3a7d0 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e any other.** kin
3a7e0 64 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72 65 20 d of mutex more
3a7f0 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 than once, the b
3a800 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 ehavior is undef
3a810 69 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 39 ined..** {H17029
3a820 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 } SQLite will ne
3a830 76 65 72 20 65 78 68 69 62 69 74 0a 2a 2a 20 73 ver exhibit.** s
3a840 75 63 68 20 62 65 68 61 76 69 6f 72 20 69 6e 20 uch behavior in
3a850 69 74 73 20 6f 77 6e 20 75 73 65 20 6f 66 20 6d its own use of m
3a860 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f utexes..**.** So
3a870 6d 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72 20 me systems (for
3a880 65 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77 73 example, Windows
3a890 20 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70 70 95) do not supp
3a8a0 6f 72 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f ort the operatio
3a8b0 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 n.** implemented
3a8c0 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 by sqlite3_mute
3a8d0 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68 6f x_try(). On tho
3a8e0 73 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c 69 se systems, sqli
3a8f0 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 0a te3_mutex_try().
3a900 2a 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 ** will always r
3a910 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 eturn SQLITE_BUS
3a920 59 2e 20 20 7b 48 31 37 30 33 30 7d 20 54 68 65 Y. {H17030} The
3a930 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e 6c SQLite core onl
3a940 79 20 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 73 y ever uses.** s
3a950 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
3a960 28 29 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a () as an optimiz
3a970 61 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 69 73 ation so this is
3a980 20 61 63 63 65 70 74 61 62 6c 65 20 62 65 68 61 acceptable beha
3a990 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 vior..**.** {H17
3a9a0 30 33 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 031} The sqlite3
3a9b0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 _mutex_leave() r
3a9c0 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d outine exits a m
3a9d0 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a utex that was.**
3a9e0 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 previously ente
3a9f0 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 red by the same
3aa00 74 68 72 65 61 64 2e 20 20 7b 41 31 37 30 33 32 thread. {A17032
3aa10 7d 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a } The behavior.*
3aa20 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 * is undefined i
3aa30 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e f the mutex is n
3aa40 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 ot currently ent
3aa50 65 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 ered by the.** c
3aa60 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 6f 72 alling thread or
3aa70 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
3aa80 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 7b 48 y allocated. {H
3aa90 31 37 30 33 33 7d 20 53 51 4c 69 74 65 20 77 69 17033} SQLite wi
3aaa0 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f 20 65 ll.** never do e
3aab0 69 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a ither. {END}.**.
3aac0 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 ** If the argume
3aad0 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 nt to sqlite3_mu
3aae0 74 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71 6c tex_enter(), sql
3aaf0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
3ab00 2c 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f , or.** sqlite3_
3ab10 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69 73 mutex_leave() is
3ab20 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c a NULL pointer,
3ab30 20 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65 20 then all three
3ab40 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68 61 routines.** beha
3ab50 76 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a ve as no-ops..**
3ab60 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 .** See also: [s
3ab70 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
3ab80 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 d()] and [sqlite
3ab90 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
3aba0 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 )]..*/.SQLITE_AP
3abb0 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 I sqlite3_mutex
3abc0 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 *sqlite3_mutex_a
3abd0 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 lloc(int);.SQLIT
3abe0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
3abf0 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71 e3_mutex_free(sq
3ac00 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 lite3_mutex*);.S
3ac10 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
3ac20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
3ac30 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 er(sqlite3_mutex
3ac40 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 *);.SQLITE_API i
3ac50 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 nt sqlite3_mutex
3ac60 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 _try(sqlite3_mut
3ac70 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ex*);.SQLITE_API
3ac80 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 void sqlite3_mu
3ac90 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 tex_leave(sqlite
3aca0 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 3_mutex*);../*.*
3acb0 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 * CAPI3REF: Mute
3acc0 78 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74 x Methods Object
3acd0 20 7b 48 31 37 31 32 30 7d 20 3c 53 32 30 31 33 {H17120} <S2013
3ace0 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
3acf0 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 AL.**.** An inst
3ad00 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
3ad10 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 ucture defines t
3ad20 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 he low-level rou
3ad30 74 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f tines.** used to
3ad40 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75 73 allocate and us
3ad50 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a e mutexes..**.**
3ad60 20 55 73 75 61 6c 6c 79 2c 20 74 68 65 20 64 65 Usually, the de
3ad70 66 61 75 6c 74 20 6d 75 74 65 78 20 69 6d 70 6c fault mutex impl
3ad80 65 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f 76 ementations prov
3ad90 69 64 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 ided by SQLite a
3ada0 72 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 re.** sufficient
3adb0 2c 20 68 6f 77 65 76 65 72 20 74 68 65 20 75 73 , however the us
3adc0 65 72 20 68 61 73 20 74 68 65 20 6f 70 74 69 6f er has the optio
3add0 6e 20 6f 66 20 73 75 62 73 74 69 74 75 74 69 6e n of substitutin
3ade0 67 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d g a custom.** im
3adf0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 plementation for
3ae00 20 73 70 65 63 69 61 6c 69 7a 65 64 20 64 65 70 specialized dep
3ae10 6c 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 74 loyments or syst
3ae20 65 6d 73 20 66 6f 72 20 77 68 69 63 68 20 53 51 ems for which SQ
3ae30 4c 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 Lite.** does not
3ae40 20 70 72 6f 76 69 64 65 20 61 20 73 75 69 74 61 provide a suita
3ae50 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ble implementati
3ae60 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 on. In this case
3ae70 2c 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 63 72 , the user.** cr
3ae80 65 61 74 65 73 20 61 6e 64 20 70 6f 70 75 6c 61 eates and popula
3ae90 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 tes an instance
3aea0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
3aeb0 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f 20 e to pass.** to
3aec0 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
3aed0 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 along with the
3aee0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
3aef0 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a UTEX] option..**
3af00 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 61 Additionally, a
3af10 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
3af20 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e is structure can
3af30 20 62 65 20 75 73 65 64 20 61 73 20 61 6e 0a 2a be used as an.*
3af40 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c * output variabl
3af50 65 20 77 68 65 6e 20 71 75 65 72 79 69 6e 67 20 e when querying
3af60 74 68 65 20 73 79 73 74 65 6d 20 66 6f 72 20 74 the system for t
3af70 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78 he current mutex
3af80 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
3af90 6f 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 5b 53 on, using the [S
3afa0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 QLITE_CONFIG_GET
3afb0 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a MUTEX] option..*
3afc0 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 49 *.** The xMutexI
3afd0 6e 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69 6e nit method defin
3afe0 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75 63 ed by this struc
3aff0 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 ture is invoked
3b000 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79 as.** part of sy
3b010 73 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61 74 stem initializat
3b020 69 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74 ion by the sqlit
3b030 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
3b040 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31 function..** {H1
3b050 37 30 30 31 7d 20 54 68 65 20 78 4d 75 74 65 78 7001} The xMutex
3b060 49 6e 69 74 20 72 6f 75 74 69 6e 65 20 73 68 61 Init routine sha
3b070 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20 ll be called by
3b080 53 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f 72 20 SQLite once for
3b090 65 61 63 68 0a 2a 2a 20 65 66 66 65 63 74 69 76 each.** effectiv
3b0a0 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 e call to [sqlit
3b0b0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d e3_initialize()]
3b0c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 ..**.** The xMut
3b0d0 65 78 45 6e 64 20 6d 65 74 68 6f 64 20 64 65 66 exEnd method def
3b0e0 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72 ined by this str
3b0f0 75 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 ucture is invoke
3b100 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 d as.** part of
3b110 73 79 73 74 65 6d 20 73 68 75 74 64 6f 77 6e 20 system shutdown
3b120 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 by the sqlite3_s
3b130 68 75 74 64 6f 77 6e 28 29 20 66 75 6e 63 74 69 hutdown() functi
3b140 6f 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70 6c 65 on. The.** imple
3b150 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 mentation of thi
3b160 73 20 6d 65 74 68 6f 64 20 69 73 20 65 78 70 65 s method is expe
3b170 63 74 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 cted to release
3b180 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a all outstanding.
3b190 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 6f 62 74 ** resources obt
3b1a0 61 69 6e 65 64 20 62 79 20 74 68 65 20 6d 75 74 ained by the mut
3b1b0 65 78 20 6d 65 74 68 6f 64 73 20 69 6d 70 6c 65 ex methods imple
3b1c0 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70 65 63 mentation, espec
3b1d0 69 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65 20 6f ially.** those o
3b1e0 62 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 78 btained by the x
3b1f0 4d 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64 MutexInit method
3b200 2e 20 7b 48 31 37 30 30 33 7d 20 54 68 65 20 78 . {H17003} The x
3b210 4d 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20 69 6e MutexEnd().** in
3b220 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65 terface shall be
3b230 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f invoked once fo
3b240 72 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b r each call to [
3b250 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e sqlite3_shutdown
3b260 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 ()]..**.** The r
3b270 65 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e 20 6d emaining seven m
3b280 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64 20 62 ethods defined b
3b290 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 y this structure
3b2a0 20 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a (xMutexAlloc,.*
3b2b0 2a 20 78 4d 75 74 65 78 46 72 65 65 2c 20 78 4d * xMutexFree, xM
3b2c0 75 74 65 78 45 6e 74 65 72 2c 20 78 4d 75 74 65 utexEnter, xMute
3b2d0 78 54 72 79 2c 20 78 4d 75 74 65 78 4c 65 61 76 xTry, xMutexLeav
3b2e0 65 2c 20 78 4d 75 74 65 78 48 65 6c 64 20 61 6e e, xMutexHeld an
3b2f0 64 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74 68 65 d.** xMutexNothe
3b300 6c 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 ld) implement th
3b310 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 e following inte
3b320 72 66 61 63 65 73 20 28 72 65 73 70 65 63 74 69 rfaces (respecti
3b330 76 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c vely):.**.** <ul
3b340 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 >.** <li> [sq
3b350 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
3b360 63 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 c()] </li>.**
3b370 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d <li> [sqlite3_m
3b380 75 74 65 78 5f 66 72 65 65 28 29 5d 20 3c 2f 6c utex_free()] </l
3b390 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 i>.** <li> [s
3b3a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
3b3b0 65 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 er()] </li>.**
3b3c0 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f <li> [sqlite3_
3b3d0 6d 75 74 65 78 5f 74 72 79 28 29 5d 20 3c 2f 6c mutex_try()] </l
3b3e0 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 i>.** <li> [s
3b3f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
3b400 76 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 ve()] </li>.**
3b410 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f <li> [sqlite3_
3b420 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 3c 2f mutex_held()] </
3b430 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b li>.** <li> [
3b440 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f sqlite3_mutex_no
3b450 74 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a theld()] </li>.*
3b460 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 * </ul>.**.** Th
3b470 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 e only differenc
3b480 65 20 69 73 20 74 68 61 74 20 74 68 65 20 70 75 e is that the pu
3b490 62 6c 69 63 20 73 71 6c 69 74 65 33 5f 58 58 58 blic sqlite3_XXX
3b4a0 20 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75 6d 65 functions enume
3b4b0 72 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65 20 73 rated.** above s
3b4c0 69 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65 20 61 ilently ignore a
3b4d0 6e 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 74 ny invocations t
3b4e0 68 61 74 20 70 61 73 73 20 61 20 4e 55 4c 4c 20 hat pass a NULL
3b4f0 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 0a pointer instead.
3b500 2a 2a 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 ** of a valid mu
3b510 74 65 78 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 tex handle. The
3b520 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
3b530 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 73 20 64 of the methods d
3b540 65 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 69 efined.** by thi
3b550 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 s structure are
3b560 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 not required to
3b570 68 61 6e 64 6c 65 20 74 68 69 73 20 63 61 73 65 handle this case
3b580 2c 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a , the results.**
3b590 20 6f 66 20 70 61 73 73 69 6e 67 20 61 20 4e 55 of passing a NU
3b5a0 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 LL pointer inste
3b5b0 61 64 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 ad of a valid mu
3b5c0 74 65 78 20 68 61 6e 64 6c 65 20 61 72 65 20 75 tex handle are u
3b5d0 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69 2e 65 ndefined.** (i.e
3b5e0 2e 20 69 74 20 69 73 20 61 63 63 65 70 74 61 62 . it is acceptab
3b5f0 6c 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e le to provide an
3b600 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
3b610 74 68 61 74 20 73 65 67 66 61 75 6c 74 73 20 69 that segfaults i
3b620 66 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 73 65 f.** it is passe
3b630 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 d a NULL pointer
3b640 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 )..**.** The xMu
3b650 74 65 78 49 6e 69 74 28 29 20 6d 65 74 68 6f 64 texInit() method
3b660 20 6d 75 73 74 20 62 65 20 74 68 72 65 61 64 73 must be threads
3b670 61 66 65 2e 20 20 49 74 20 6d 75 73 74 20 62 65 afe. It must be
3b680 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 harmless to.**
3b690 69 6e 76 6f 6b 65 20 78 4d 75 74 65 78 49 6e 69 invoke xMutexIni
3b6a0 74 28 29 20 6d 75 74 69 70 6c 65 20 74 69 6d 65 t() mutiple time
3b6b0 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d s within the sam
3b6c0 65 20 70 72 6f 63 65 73 73 20 61 6e 64 20 77 69 e process and wi
3b6d0 74 68 6f 75 74 0a 2a 2a 20 69 6e 74 65 72 76 65 thout.** interve
3b6e0 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f 20 78 4d ning calls to xM
3b6f0 75 74 65 78 45 6e 64 28 29 2e 20 20 53 65 63 6f utexEnd(). Seco
3b700 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e nd and subsequen
3b710 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 78 4d t calls to.** xM
3b720 75 74 65 78 49 6e 69 74 28 29 20 6d 75 73 74 20 utexInit() must
3b730 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a be no-ops..**.**
3b740 20 78 4d 75 74 65 78 49 6e 69 74 28 29 20 6d 75 xMutexInit() mu
3b750 73 74 20 6e 6f 74 20 75 73 65 20 53 51 4c 69 74 st not use SQLit
3b760 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
3b770 69 6f 6e 20 28 5b 73 71 6c 69 74 65 33 5f 6d 61 ion ([sqlite3_ma
3b780 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 61 6e 64 20 69 lloc()].** and i
3b790 74 73 20 61 73 73 6f 63 69 61 74 65 73 29 2e 20 ts associates).
3b7a0 20 53 69 6d 69 6c 61 72 6c 79 2c 20 78 4d 75 74 Similarly, xMut
3b7b0 65 78 41 6c 6c 6f 63 28 29 20 6d 75 73 74 20 6e exAlloc() must n
3b7c0 6f 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 65 ot use SQLite me
3b7d0 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 mory.** allocati
3b7e0 6f 6e 20 66 6f 72 20 61 20 73 74 61 74 69 63 20 on for a static
3b7f0 6d 75 74 65 78 2e 20 20 48 6f 77 65 76 65 72 20 mutex. However
3b800 78 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20 6d 61 xMutexAlloc() ma
3b810 79 20 75 73 65 20 53 51 4c 69 74 65 0a 2a 2a 20 y use SQLite.**
3b820 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
3b830 6e 20 66 6f 72 20 61 20 66 61 73 74 20 6f 72 20 n for a fast or
3b840 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 2e recursive mutex.
3b850 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 .**.** SQLite wi
3b860 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 4d ll invoke the xM
3b870 75 74 65 78 45 6e 64 28 29 20 6d 65 74 68 6f 64 utexEnd() method
3b880 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73 when [sqlite3_s
3b890 68 75 74 64 6f 77 6e 28 29 5d 20 69 73 0a 2a 2a hutdown()] is.**
3b8a0 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 6f 6e 6c called, but onl
3b8b0 79 20 69 66 20 74 68 65 20 70 72 69 6f 72 20 63 y if the prior c
3b8c0 61 6c 6c 20 74 6f 20 78 4d 75 74 65 78 49 6e 69 all to xMutexIni
3b8d0 74 20 72 65 74 75 72 6e 65 64 20 53 51 4c 49 54 t returned SQLIT
3b8e0 45 5f 4f 4b 2e 0a 2a 2a 20 49 66 20 78 4d 75 74 E_OK..** If xMut
3b8f0 65 78 49 6e 69 74 20 66 61 69 6c 73 20 69 6e 20 exInit fails in
3b900 61 6e 79 20 77 61 79 2c 20 69 74 20 69 73 20 65 any way, it is e
3b910 78 70 65 63 74 65 64 20 74 6f 20 63 6c 65 61 6e xpected to clean
3b920 20 75 70 20 61 66 74 65 72 20 69 74 73 65 6c 66 up after itself
3b930 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 74 .** prior to ret
3b940 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 74 79 70 65 64 urning..*/.typed
3b950 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
3b960 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 3_mutex_methods
3b970 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
3b980 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 thods;.struct sq
3b990 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
3b9a0 6f 64 73 20 7b 0a 20 20 69 6e 74 20 28 2a 78 4d ods {. int (*xM
3b9b0 75 74 65 78 49 6e 69 74 29 28 76 6f 69 64 29 3b utexInit)(void);
3b9c0 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 45 . int (*xMutexE
3b9d0 6e 64 29 28 76 6f 69 64 29 3b 0a 20 20 73 71 6c nd)(void);. sql
3b9e0 69 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a 78 4d ite3_mutex *(*xM
3b9f0 75 74 65 78 41 6c 6c 6f 63 29 28 69 6e 74 29 3b utexAlloc)(int);
3ba00 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 . void (*xMutex
3ba10 46 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 Free)(sqlite3_mu
3ba20 74 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 tex *);. void (
3ba30 2a 78 4d 75 74 65 78 45 6e 74 65 72 29 28 73 71 *xMutexEnter)(sq
3ba40 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a lite3_mutex *);.
3ba50 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 54 72 int (*xMutexTr
3ba60 79 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 y)(sqlite3_mutex
3ba70 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d *);. void (*xM
3ba80 75 74 65 78 4c 65 61 76 65 29 28 73 71 6c 69 74 utexLeave)(sqlit
3ba90 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 e3_mutex *);. i
3baa0 6e 74 20 28 2a 78 4d 75 74 65 78 48 65 6c 64 29 nt (*xMutexHeld)
3bab0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
3bac0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 );. int (*xMute
3bad0 78 4e 6f 74 68 65 6c 64 29 28 73 71 6c 69 74 65 xNotheld)(sqlite
3bae0 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 7d 3b 0a 0a 3_mutex *);.};..
3baf0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
3bb00 4d 75 74 65 78 20 56 65 72 69 66 69 63 61 74 69 Mutex Verificati
3bb10 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 31 37 on Routines {H17
3bb20 30 38 30 7d 20 3c 53 32 30 31 33 30 3e 20 3c 53 080} <S20130> <S
3bb30 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 30800>.**.** The
3bb40 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
3bb50 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 eld() and sqlite
3bb60 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
3bb70 29 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 ) routines.** ar
3bb80 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 e intended for u
3bb90 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 se inside assert
3bba0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 7b () statements. {
3bbb0 48 31 37 30 38 31 7d 20 54 68 65 20 53 51 4c 69 H17081} The SQLi
3bbc0 74 65 20 63 6f 72 65 0a 2a 2a 20 6e 65 76 65 72 te core.** never
3bbd0 20 75 73 65 73 20 74 68 65 73 65 20 72 6f 75 74 uses these rout
3bbe0 69 6e 65 73 20 65 78 63 65 70 74 20 69 6e 73 69 ines except insi
3bbf0 64 65 20 61 6e 20 61 73 73 65 72 74 28 29 20 61 de an assert() a
3bc00 6e 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a nd applications.
3bc10 2a 2a 20 61 72 65 20 61 64 76 69 73 65 64 20 74 ** are advised t
3bc20 6f 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6c 65 61 o follow the lea
3bc30 64 20 6f 66 20 74 68 65 20 63 6f 72 65 2e 20 20 d of the core.
3bc40 7b 48 31 37 30 38 32 7d 20 54 68 65 20 63 6f 72 {H17082} The cor
3bc50 65 20 6f 6e 6c 79 0a 2a 2a 20 70 72 6f 76 69 64 e only.** provid
3bc60 65 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f es implementatio
3bc70 6e 73 20 66 6f 72 20 74 68 65 73 65 20 72 6f 75 ns for these rou
3bc80 74 69 6e 65 73 20 77 68 65 6e 20 69 74 20 69 73 tines when it is
3bc90 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 69 74 compiled.** wit
3bca0 68 20 74 68 65 20 53 51 4c 49 54 45 5f 44 45 42 h the SQLITE_DEB
3bcb0 55 47 20 66 6c 61 67 2e 20 20 7b 41 31 37 30 38 UG flag. {A1708
3bcc0 37 7d 20 45 78 74 65 72 6e 61 6c 20 6d 75 74 65 7} External mute
3bcd0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
3bce0 73 0a 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 72 65 s.** are only re
3bcf0 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64 quired to provid
3bd00 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 e these routines
3bd10 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 if SQLITE_DEBUG
3bd20 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61 is.** defined a
3bd30 6e 64 20 69 66 20 4e 44 45 42 55 47 20 69 73 20 nd if NDEBUG is
3bd40 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a not defined..**.
3bd50 2a 2a 20 7b 48 31 37 30 38 33 7d 20 54 68 65 73 ** {H17083} Thes
3bd60 65 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c e routines shoul
3bd70 64 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 d return true if
3bd80 20 74 68 65 20 6d 75 74 65 78 20 69 6e 20 74 68 the mutex in th
3bd90 65 69 72 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 eir argument.**
3bda0 69 73 20 68 65 6c 64 20 6f 72 20 6e 6f 74 20 68 is held or not h
3bdb0 65 6c 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c eld, respectivel
3bdc0 79 2c 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e y, by the callin
3bdd0 67 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 g thread..**.**
3bde0 7b 58 31 37 30 38 34 7d 20 54 68 65 20 69 6d 70 {X17084} The imp
3bdf0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e lementation is n
3be00 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 70 ot required to p
3be10 72 6f 76 69 64 65 64 20 76 65 72 73 69 6f 6e 73 rovided versions
3be20 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 72 6f 75 of these.** rou
3be30 74 69 6e 65 73 20 74 68 61 74 20 61 63 74 75 61 tines that actua
3be40 6c 6c 79 20 77 6f 72 6b 2e 20 49 66 20 74 68 65 lly work. If the
3be50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
3be60 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 65 does not provide
3be70 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 76 65 72 73 working.** vers
3be80 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 20 72 6f ions of these ro
3be90 75 74 69 6e 65 73 2c 20 69 74 20 73 68 6f 75 6c utines, it shoul
3bea0 64 20 61 74 20 6c 65 61 73 74 20 70 72 6f 76 69 d at least provi
3beb0 64 65 20 73 74 75 62 73 20 74 68 61 74 20 61 6c de stubs that al
3bec0 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e 20 74 ways.** return t
3bed0 72 75 65 20 73 6f 20 74 68 61 74 20 6f 6e 65 20 rue so that one
3bee0 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 73 70 75 does not get spu
3bef0 72 69 6f 75 73 20 61 73 73 65 72 74 69 6f 6e 20 rious assertion
3bf00 66 61 69 6c 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 failures..**.**
3bf10 7b 48 31 37 30 38 35 7d 20 49 66 20 74 68 65 20 {H17085} If the
3bf20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 argument to sqli
3bf30 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 te3_mutex_held()
3bf40 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 is a NULL point
3bf50 65 72 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72 er then.** the r
3bf60 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65 outine should re
3bf70 74 75 72 6e 20 31 2e 20 20 7b 45 4e 44 7d 20 54 turn 1. {END} T
3bf80 68 69 73 20 73 65 65 6d 73 20 63 6f 75 6e 74 65 his seems counte
3bf90 72 2d 69 6e 74 75 69 74 69 76 65 20 73 69 6e 63 r-intuitive sinc
3bfa0 65 0a 2a 2a 20 63 6c 65 61 72 6c 79 20 74 68 65 e.** clearly the
3bfb0 20 6d 75 74 65 78 20 63 61 6e 6e 6f 74 20 62 65 mutex cannot be
3bfc0 20 68 65 6c 64 20 69 66 20 69 74 20 64 6f 65 73 held if it does
3bfd0 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 42 75 74 not exist. But
3bfe0 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 65 61 73 the.** the reas
3bff0 6f 6e 20 74 68 65 20 6d 75 74 65 78 20 64 6f 65 on the mutex doe
3c000 73 20 6e 6f 74 20 65 78 69 73 74 20 69 73 20 62 s not exist is b
3c010 65 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 64 ecause the build
3c020 20 69 73 20 6e 6f 74 0a 2a 2a 20 75 73 69 6e 67 is not.** using
3c030 20 6d 75 74 65 78 65 73 2e 20 20 41 6e 64 20 77 mutexes. And w
3c040 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 e do not want th
3c050 65 20 61 73 73 65 72 74 28 29 20 63 6f 6e 74 61 e assert() conta
3c060 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 63 61 6c ining the.** cal
3c070 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 l to sqlite3_mut
3c080 65 78 5f 68 65 6c 64 28 29 20 74 6f 20 66 61 69 ex_held() to fai
3c090 6c 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f l, so a non-zero
3c0a0 20 72 65 74 75 72 6e 20 69 73 0a 2a 2a 20 74 68 return is.** th
3c0b0 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 68 e appropriate th
3c0c0 69 6e 67 20 74 6f 20 64 6f 2e 20 20 7b 48 31 37 ing to do. {H17
3c0d0 30 38 36 7d 20 54 68 65 20 73 71 6c 69 74 65 33 086} The sqlite3
3c0e0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 _mutex_notheld()
3c0f0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 73 68 .** interface sh
3c100 6f 75 6c 64 20 61 6c 73 6f 20 72 65 74 75 72 6e ould also return
3c110 20 31 20 77 68 65 6e 20 67 69 76 65 6e 20 61 20 1 when given a
3c120 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f NULL pointer..*/
3c130 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
3c140 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
3c150 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
3c160 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 *);.SQLITE_API i
3c170 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 nt sqlite3_mutex
3c180 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 _notheld(sqlite3
3c190 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a _mutex*);../*.**
3c1a0 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 CAPI3REF: Mutex
3c1b0 20 54 79 70 65 73 20 7b 48 31 37 30 30 31 7d 20 Types {H17001}
3c1c0 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <H17000>.**.** T
3c1d0 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 he [sqlite3_mute
3c1e0 78 5f 61 6c 6c 6f 63 28 29 5d 20 69 6e 74 65 72 x_alloc()] inter
3c1f0 66 61 63 65 20 74 61 6b 65 73 20 61 20 73 69 6e face takes a sin
3c200 67 6c 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 gle argument.**
3c210 77 68 69 63 68 20 69 73 20 6f 6e 65 20 6f 66 20 which is one of
3c220 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f these integer co
3c230 6e 73 74 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 nstants..**.** T
3c240 68 65 20 73 65 74 20 6f 66 20 73 74 61 74 69 63 he set of static
3c250 20 6d 75 74 65 78 65 73 20 6d 61 79 20 63 68 61 mutexes may cha
3c260 6e 67 65 20 66 72 6f 6d 20 6f 6e 65 20 53 51 4c nge from one SQL
3c270 69 74 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74 ite release to t
3c280 68 65 0a 2a 2a 20 6e 65 78 74 2e 20 20 41 70 70 he.** next. App
3c290 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f lications that o
3c2a0 76 65 72 72 69 64 65 20 74 68 65 20 62 75 69 6c verride the buil
3c2b0 74 2d 69 6e 20 6d 75 74 65 78 20 6c 6f 67 69 63 t-in mutex logic
3c2c0 20 6d 75 73 74 20 62 65 0a 2a 2a 20 70 72 65 70 must be.** prep
3c2d0 61 72 65 64 20 74 6f 20 61 63 63 6f 6d 6d 6f 64 ared to accommod
3c2e0 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 ate additional s
3c2f0 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 0a 2a tatic mutexes..*
3c300 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
3c310 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20 20 20 _MUTEX_FAST
3c320 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 0.#defin
3c330 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 e SQLITE_MUTEX_R
3c340 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20 ECURSIVE
3c350 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 1.#define SQLITE
3c360 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
3c370 53 54 45 52 20 20 20 20 32 0a 23 64 65 66 69 6e STER 2.#defin
3c380 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 e SQLITE_MUTEX_S
3c390 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 TATIC_MEM
3c3a0 33 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 61 3 /* sqlite3_ma
3c3b0 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e lloc() */.#defin
3c3c0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 e SQLITE_MUTEX_S
3c3d0 54 41 54 49 43 5f 4d 45 4d 32 20 20 20 20 20 20 TATIC_MEM2
3c3e0 34 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 20 2a 4 /* NOT USED *
3c3f0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
3c400 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f 50 _MUTEX_STATIC_OP
3c410 45 4e 20 20 20 20 20 20 34 20 20 2f 2a 20 73 71 EN 4 /* sq
3c420 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 29 lite3BtreeOpen()
3c430 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
3c440 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
3c450 50 52 4e 47 20 20 20 20 20 20 35 20 20 2f 2a 20 PRNG 5 /*
3c460 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 28 29 sqlite3_random()
3c470 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
3c480 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
3c490 4c 52 55 20 20 20 20 20 20 20 36 20 20 2f 2a 20 LRU 6 /*
3c4a0 6c 72 75 20 70 61 67 65 20 6c 69 73 74 20 2a 2f lru page list */
3c4b0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3c4c0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 MUTEX_STATIC_LRU
3c4d0 32 20 20 20 20 20 20 37 20 20 2f 2a 20 6c 72 75 2 7 /* lru
3c4e0 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 0a 2f page list */../
3c4f0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 *.** CAPI3REF: R
3c500 65 74 72 69 65 76 65 20 74 68 65 20 6d 75 74 65 etrieve the mute
3c510 78 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 x for a database
3c520 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 37 connection {H17
3c530 30 30 32 7d 20 3c 48 31 37 30 30 30 3e 0a 2a 2a 002} <H17000>.**
3c540 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 .** This interfa
3c550 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 ce returns a poi
3c560 6e 74 65 72 20 74 68 65 20 5b 73 71 6c 69 74 65 nter the [sqlite
3c570 33 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74 20 3_mutex] object
3c580 74 68 61 74 20 0a 2a 2a 20 73 65 72 69 61 6c 69 that .** seriali
3c590 7a 65 73 20 61 63 63 65 73 73 20 74 6f 20 74 68 zes access to th
3c5a0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
3c5b0 65 63 74 69 6f 6e 5d 20 67 69 76 65 6e 20 69 6e ection] given in
3c5c0 20 74 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a the argument.**
3c5d0 20 77 68 65 6e 20 74 68 65 20 5b 74 68 72 65 61 when the [threa
3c5e0 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53 65 ding mode] is Se
3c5f0 72 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 rialized..** If
3c600 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d the [threading m
3c610 6f 64 65 5d 20 69 73 20 53 69 6e 67 6c 65 2d 74 ode] is Single-t
3c620 68 72 65 61 64 20 6f 72 20 4d 75 6c 74 69 2d 74 hread or Multi-t
3c630 68 72 65 61 64 20 74 68 65 6e 20 74 68 69 73 0a hread then this.
3c640 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 ** routine retur
3c650 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 ns a NULL pointe
3c660 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 r..*/.SQLITE_API
3c670 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
3c680 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 sqlite3_db_mutex
3c690 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a (sqlite3*);../*.
3c6a0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 77 ** CAPI3REF: Low
3c6b0 2d 4c 65 76 65 6c 20 43 6f 6e 74 72 6f 6c 20 4f -Level Control O
3c6c0 66 20 44 61 74 61 62 61 73 65 20 46 69 6c 65 73 f Database Files
3c6d0 20 7b 48 31 31 33 30 30 7d 20 3c 53 33 30 38 30 {H11300} <S3080
3c6e0 30 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 31 0>.**.** {H11301
3c6f0 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 } The [sqlite3_f
3c700 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 ile_control()] i
3c710 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73 20 61 nterface makes a
3c720 20 64 69 72 65 63 74 20 63 61 6c 6c 20 74 6f 20 direct call to
3c730 74 68 65 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 the.** xFileCont
3c740 72 6f 6c 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 rol method for t
3c750 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d he [sqlite3_io_m
3c760 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61 ethods] object a
3c770 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 ssociated.** wit
3c780 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 h a particular d
3c790 61 74 61 62 61 73 65 20 69 64 65 6e 74 69 66 69 atabase identifi
3c7a0 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 ed by the second
3c7b0 20 61 72 67 75 6d 65 6e 74 2e 20 7b 48 31 31 33 argument. {H113
3c7c0 30 32 7d 20 54 68 65 0a 2a 2a 20 6e 61 6d 65 20 02} The.** name
3c7d0 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
3c7e0 69 73 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69 is the name assi
3c7f0 67 6e 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 gned to the data
3c800 62 61 73 65 20 62 79 20 74 68 65 0a 2a 2a 20 3c base by the.** <
3c810 61 20 68 72 65 66 3d 22 6c 61 6e 67 5f 61 74 74 a href="lang_att
3c820 61 63 68 2e 68 74 6d 6c 22 3e 41 54 54 41 43 48 ach.html">ATTACH
3c830 3c 2f 61 3e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 </a> SQL command
3c840 20 74 68 61 74 20 6f 70 65 6e 65 64 20 74 68 65 that opened the
3c850 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 7b 48 .** database. {H
3c860 31 31 33 30 33 7d 20 54 6f 20 63 6f 6e 74 72 6f 11303} To contro
3c870 6c 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 l the main datab
3c880 61 73 65 20 66 69 6c 65 2c 20 75 73 65 20 74 68 ase file, use th
3c890 65 20 6e 61 6d 65 20 22 6d 61 69 6e 22 0a 2a 2a e name "main".**
3c8a0 20 6f 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 or a NULL point
3c8b0 65 72 2e 20 7b 48 31 31 33 30 34 7d 20 54 68 65 er. {H11304} The
3c8c0 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 third and fourt
3c8d0 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 h parameters to
3c8e0 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
3c8f0 61 72 65 20 70 61 73 73 65 64 20 64 69 72 65 63 are passed direc
3c900 74 6c 79 20 74 68 72 6f 75 67 68 20 74 6f 20 74 tly through to t
3c910 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68 he second and th
3c920 69 72 64 20 70 61 72 61 6d 65 74 65 72 73 20 6f ird parameters o
3c930 66 0a 2a 2a 20 74 68 65 20 78 46 69 6c 65 43 6f f.** the xFileCo
3c940 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 20 7b ntrol method. {
3c950 48 31 31 33 30 35 7d 20 54 68 65 20 72 65 74 75 H11305} The retu
3c960 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 rn value of the
3c970 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 0a 2a 2a 20 xFileControl.**
3c980 6d 65 74 68 6f 64 20 62 65 63 6f 6d 65 73 20 74 method becomes t
3c990 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
3c9a0 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e of this routine.
3c9b0 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 36 7d 20 .**.** {H11306}
3c9c0 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 If the second pa
3c9d0 72 61 6d 65 74 65 72 20 28 7a 44 62 4e 61 6d 65 rameter (zDbName
3c9e0 29 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 ) does not match
3c9f0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 79 the name of any
3ca00 0a 2a 2a 20 6f 70 65 6e 20 64 61 74 61 62 61 73 .** open databas
3ca10 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 4c e file, then SQL
3ca20 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 ITE_ERROR is ret
3ca30 75 72 6e 65 64 2e 20 7b 48 31 31 33 30 37 7d 20 urned. {H11307}
3ca40 54 68 69 73 20 65 72 72 6f 72 0a 2a 2a 20 63 6f This error.** co
3ca50 64 65 20 69 73 20 6e 6f 74 20 72 65 6d 65 6d 62 de is not rememb
3ca60 65 72 65 64 20 61 6e 64 20 77 69 6c 6c 20 6e 6f ered and will no
3ca70 74 20 62 65 20 72 65 63 61 6c 6c 65 64 20 62 79 t be recalled by
3ca80 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 [sqlite3_errcod
3ca90 65 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 e()].** or [sqli
3caa0 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 20 7b te3_errmsg()]. {
3cab0 41 31 31 33 30 38 7d 20 54 68 65 20 75 6e 64 65 A11308} The unde
3cac0 72 6c 79 69 6e 67 20 78 46 69 6c 65 43 6f 6e 74 rlying xFileCont
3cad0 72 6f 6c 20 6d 65 74 68 6f 64 20 6d 69 67 68 74 rol method might
3cae0 0a 2a 2a 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 .** also return
3caf0 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 7b SQLITE_ERROR. {
3cb00 41 31 31 33 30 39 7d 20 54 68 65 72 65 20 69 73 A11309} There is
3cb10 20 6e 6f 20 77 61 79 20 74 6f 20 64 69 73 74 69 no way to disti
3cb20 6e 67 75 69 73 68 20 62 65 74 77 65 65 6e 0a 2a nguish between.*
3cb30 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 7a * an incorrect z
3cb40 44 62 4e 61 6d 65 20 61 6e 64 20 61 6e 20 53 51 DbName and an SQ
3cb50 4c 49 54 45 5f 45 52 52 4f 52 20 72 65 74 75 72 LITE_ERROR retur
3cb60 6e 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 n from the under
3cb70 6c 79 69 6e 67 0a 2a 2a 20 78 46 69 6c 65 43 6f lying.** xFileCo
3cb80 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 7b 45 ntrol method. {E
3cb90 4e 44 7d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c ND}.**.** See al
3cba0 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 so: [SQLITE_FCNT
3cbb0 4c 5f 4c 4f 43 4b 53 54 41 54 45 5d 0a 2a 2f 0a L_LOCKSTATE].*/.
3cbc0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
3cbd0 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 qlite3_file_cont
3cbe0 72 6f 6c 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f rol(sqlite3*, co
3cbf0 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d nst char *zDbNam
3cc00 65 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 2a e, int op, void*
3cc10 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
3cc20 45 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 74 65 EF: Testing Inte
3cc30 72 66 61 63 65 20 7b 48 31 31 34 30 30 7d 20 3c rface {H11400} <
3cc40 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S30800>.**.** Th
3cc50 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 e sqlite3_test_c
3cc60 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 ontrol() interfa
3cc70 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 ce is used to re
3cc80 61 64 20 6f 75 74 20 69 6e 74 65 72 6e 61 6c 0a ad out internal.
3cc90 2a 2a 20 73 74 61 74 65 20 6f 66 20 53 51 4c 69 ** state of SQLi
3cca0 74 65 20 61 6e 64 20 74 6f 20 69 6e 6a 65 63 74 te and to inject
3ccb0 20 66 61 75 6c 74 73 20 69 6e 74 6f 20 53 51 4c faults into SQL
3ccc0 69 74 65 20 66 6f 72 20 74 65 73 74 69 6e 67 0a ite for testing.
3ccd0 2a 2a 20 70 75 72 70 6f 73 65 73 2e 20 20 54 68 ** purposes. Th
3cce0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
3ccf0 72 20 69 73 20 61 6e 20 6f 70 65 72 61 74 69 6f r is an operatio
3cd00 6e 20 63 6f 64 65 20 74 68 61 74 20 64 65 74 65 n code that dete
3cd10 72 6d 69 6e 65 73 0a 2a 2a 20 74 68 65 20 6e 75 rmines.** the nu
3cd20 6d 62 65 72 2c 20 6d 65 61 6e 69 6e 67 2c 20 61 mber, meaning, a
3cd30 6e 64 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 nd operation of
3cd40 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 70 all subsequent p
3cd50 61 72 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a arameters..**.**
3cd60 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 This interface
3cd70 69 73 20 6e 6f 74 20 66 6f 72 20 75 73 65 20 62 is not for use b
3cd80 79 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20 y applications.
3cd90 20 49 74 20 65 78 69 73 74 73 20 73 6f 6c 65 6c It exists solel
3cda0 79 0a 2a 2a 20 66 6f 72 20 76 65 72 69 66 79 69 y.** for verifyi
3cdb0 6e 67 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f ng the correct o
3cdc0 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 peration of the
3cdd0 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 SQLite library.
3cde0 20 44 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e Depending.** on
3cdf0 20 68 6f 77 20 74 68 65 20 53 51 4c 69 74 65 20 how the SQLite
3ce00 6c 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 library is compi
3ce10 6c 65 64 2c 20 74 68 69 73 20 69 6e 74 65 72 66 led, this interf
3ce20 61 63 65 20 6d 69 67 68 74 20 6e 6f 74 20 65 78 ace might not ex
3ce30 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 ist..**.** The d
3ce40 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 6f 70 etails of the op
3ce50 65 72 61 74 69 6f 6e 20 63 6f 64 65 73 2c 20 74 eration codes, t
3ce60 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 2c 20 74 heir meanings, t
3ce70 68 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a he parameters.**
3ce80 20 74 68 65 79 20 74 61 6b 65 2c 20 61 6e 64 20 they take, and
3ce90 77 68 61 74 20 74 68 65 79 20 64 6f 20 61 72 65 what they do are
3cea0 20 61 6c 6c 20 73 75 62 6a 65 63 74 20 74 6f 20 all subject to
3ceb0 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e change without n
3cec0 6f 74 69 63 65 2e 0a 2a 2a 20 55 6e 6c 69 6b 65 otice..** Unlike
3ced0 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 53 51 4c most of the SQL
3cee0 69 74 65 20 41 50 49 2c 20 74 68 69 73 20 66 75 ite API, this fu
3cef0 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 75 nction is not gu
3cf00 61 72 61 6e 74 65 65 64 20 74 6f 0a 2a 2a 20 6f aranteed to.** o
3cf10 70 65 72 61 74 65 20 63 6f 6e 73 69 73 74 65 6e perate consisten
3cf20 74 6c 79 20 66 72 6f 6d 20 6f 6e 65 20 72 65 6c tly from one rel
3cf30 65 61 73 65 20 74 6f 20 74 68 65 20 6e 65 78 74 ease to the next
3cf40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
3cf50 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 65 73 74 int sqlite3_test
3cf60 5f 63 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f 70 2c _control(int op,
3cf70 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ...);../*.** CA
3cf80 50 49 33 52 45 46 3a 20 54 65 73 74 69 6e 67 20 PI3REF: Testing
3cf90 49 6e 74 65 72 66 61 63 65 20 4f 70 65 72 61 74 Interface Operat
3cfa0 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 31 34 31 ion Codes {H1141
3cfb0 30 7d 20 3c 48 31 31 34 30 30 3e 0a 2a 2a 0a 2a 0} <H11400>.**.*
3cfc0 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 * These constant
3cfd0 73 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 s are the valid
3cfe0 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 70 operation code p
3cff0 61 72 61 6d 65 74 65 72 73 20 75 73 65 64 0a 2a arameters used.*
3d000 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 * as the first a
3d010 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69 rgument to [sqli
3d020 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c te3_test_control
3d030 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ()]..**.** These
3d040 20 70 61 72 61 6d 65 74 65 72 73 20 61 6e 64 20 parameters and
3d050 74 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 20 61 their meanings a
3d060 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 re subject to ch
3d070 61 6e 67 65 0a 2a 2a 20 77 69 74 68 6f 75 74 20 ange.** without
3d080 6e 6f 74 69 63 65 2e 20 20 54 68 65 73 65 20 76 notice. These v
3d090 61 6c 75 65 73 20 61 72 65 20 66 6f 72 20 74 65 alues are for te
3d0a0 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f sting purposes o
3d0b0 6e 6c 79 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74 nly..** Applicat
3d0c0 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 ions should not
3d0d0 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 use any of these
3d0e0 20 70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 74 parameters or t
3d0f0 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 74 he.** [sqlite3_t
3d100 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 est_control()] i
3d110 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 65 nterface..*/.#de
3d120 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 fine SQLITE_TEST
3d130 43 54 52 4c 5f 50 52 4e 47 5f 53 41 56 45 20 20 CTRL_PRNG_SAVE
3d140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 5.
3d150 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 #define SQLITE_T
3d160 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 ESTCTRL_PRNG_RES
3d170 54 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20 TORE
3d180 20 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 6.#define SQLIT
3d190 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f E_TESTCTRL_PRNG_
3d1a0 52 45 53 45 54 20 20 20 20 20 20 20 20 20 20 20 RESET
3d1b0 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 7.#define SQ
3d1c0 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 49 LITE_TESTCTRL_BI
3d1d0 54 56 45 43 5f 54 45 53 54 20 20 20 20 20 20 20 TVEC_TEST
3d1e0 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 8.#define
3d1f0 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c SQLITE_TESTCTRL
3d200 5f 46 41 55 4c 54 5f 49 4e 53 54 41 4c 4c 20 20 _FAULT_INSTALL
3d210 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 9.#def
3d220 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 ine SQLITE_TESTC
3d230 54 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f TRL_BENIGN_MALLO
3d240 43 5f 48 4f 4f 4b 53 20 20 20 20 20 31 30 0a 23 C_HOOKS 10.#
3d250 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
3d260 53 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f 42 STCTRL_PENDING_B
3d270 59 54 45 20 20 20 20 20 20 20 20 20 20 20 20 31 YTE 1
3d280 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 1.#define SQLITE
3d290 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45 52 54 _TESTCTRL_ASSERT
3d2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3d2b0 20 20 31 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 12.#define SQL
3d2c0 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 4c 57 ITE_TESTCTRL_ALW
3d2d0 41 59 53 20 20 20 20 20 20 20 20 20 20 20 20 20 AYS
3d2e0 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20 13.#define
3d2f0 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f SQLITE_TESTCTRL_
3d300 52 45 53 45 52 56 45 20 20 20 20 20 20 20 20 20 RESERVE
3d310 20 20 20 20 20 20 20 20 31 34 0a 0a 2f 2a 0a 2a 14../*.*
3d320 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 69 * CAPI3REF: SQLi
3d330 74 65 20 52 75 6e 74 69 6d 65 20 53 74 61 74 75 te Runtime Statu
3d340 73 20 7b 48 31 37 32 30 30 7d 20 3c 53 36 30 32 s {H17200} <S602
3d350 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 00>.** EXPERIMEN
3d360 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 TAL.**.** This i
3d370 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 nterface is used
3d380 20 74 6f 20 72 65 74 72 69 65 76 65 20 72 75 6e to retrieve run
3d390 74 69 6d 65 20 73 74 61 74 75 73 20 69 6e 66 6f time status info
3d3a0 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 rmation.** about
3d3b0 20 74 68 65 20 70 72 65 66 6f 72 6d 61 6e 63 65 the preformance
3d3c0 20 6f 66 20 53 51 4c 69 74 65 2c 20 61 6e 64 20 of SQLite, and
3d3d0 6f 70 74 69 6f 6e 61 6c 6c 79 20 74 6f 20 72 65 optionally to re
3d3e0 73 65 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 68 set various.** h
3d3f0 69 67 68 77 61 74 65 72 20 6d 61 72 6b 73 2e 20 ighwater marks.
3d400 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
3d410 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 67 65 ent is an intege
3d420 72 20 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 74 68 r code for.** th
3d430 65 20 73 70 65 63 69 66 69 63 20 70 61 72 61 6d e specific param
3d440 65 74 65 72 20 74 6f 20 6d 65 61 73 75 72 65 2e eter to measure.
3d450 20 20 52 65 63 6f 67 6e 69 7a 65 64 20 69 6e 74 Recognized int
3d460 65 67 65 72 20 63 6f 64 65 73 0a 2a 2a 20 61 72 eger codes.** ar
3d470 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5b 53 e of the form [S
3d480 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d QLITE_STATUS_MEM
3d490 4f 52 59 5f 55 53 45 44 20 7c 20 53 51 4c 49 54 ORY_USED | SQLIT
3d4a0 45 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a E_STATUS_...]..*
3d4b0 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 * The current va
3d4c0 6c 75 65 20 6f 66 20 74 68 65 20 70 61 72 61 6d lue of the param
3d4d0 65 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 eter is returned
3d4e0 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e into *pCurrent.
3d4f0 0a 2a 2a 20 54 68 65 20 68 69 67 68 65 73 74 20 .** The highest
3d500 72 65 63 6f 72 64 65 64 20 76 61 6c 75 65 20 69 recorded value i
3d510 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 s returned in *p
3d520 48 69 67 68 77 61 74 65 72 2e 20 20 49 66 20 74 Highwater. If t
3d530 68 65 0a 2a 2a 20 72 65 73 65 74 46 6c 61 67 20 he.** resetFlag
3d540 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 is true, then th
3d550 65 20 68 69 67 68 65 73 74 20 72 65 63 6f 72 64 e highest record
3d560 20 76 61 6c 75 65 20 69 73 20 72 65 73 65 74 20 value is reset
3d570 61 66 74 65 72 0a 2a 2a 20 2a 70 48 69 67 68 77 after.** *pHighw
3d580 61 74 65 72 20 69 73 20 77 72 69 74 74 65 6e 2e ater is written.
3d590 20 53 6f 6d 65 20 70 61 72 61 6d 65 74 65 72 73 Some parameters
3d5a0 20 64 6f 20 6e 6f 74 20 72 65 63 6f 72 64 20 74 do not record t
3d5b0 68 65 20 68 69 67 68 65 73 74 0a 2a 2a 20 76 61 he highest.** va
3d5c0 6c 75 65 2e 20 20 46 6f 72 20 74 68 6f 73 65 20 lue. For those
3d5d0 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f parameters.** no
3d5e0 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e thing is written
3d5f0 20 69 6e 74 6f 20 2a 70 48 69 67 68 77 61 74 65 into *pHighwate
3d600 72 20 61 6e 64 20 74 68 65 20 72 65 73 65 74 46 r and the resetF
3d610 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a lag is ignored..
3d620 2a 2a 20 4f 74 68 65 72 20 70 61 72 61 6d 65 74 ** Other paramet
3d630 65 72 73 20 72 65 63 6f 72 64 20 6f 6e 6c 79 20 ers record only
3d640 74 68 65 20 68 69 67 68 77 61 74 65 72 20 6d 61 the highwater ma
3d650 72 6b 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 63 rk and not the c
3d660 75 72 72 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e urrent.** value.
3d670 20 20 46 6f 72 20 74 68 65 73 65 20 6c 61 74 74 For these latt
3d680 65 72 20 70 61 72 61 6d 65 74 65 72 73 20 6e 6f er parameters no
3d690 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e thing is written
3d6a0 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e into *pCurrent.
3d6b0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
3d6c0 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ine returns SQLI
3d6d0 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 TE_OK on success
3d6e0 20 61 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a and a non-zero.
3d6f0 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 ** [error code]
3d700 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a on failure..**.*
3d710 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
3d720 73 20 74 68 72 65 61 64 73 61 66 65 20 62 75 74 s threadsafe but
3d730 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 is not atomic.
3d740 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 This routine ca
3d750 6e 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 n be.** called w
3d760 68 69 6c 65 20 6f 74 68 65 72 20 74 68 72 65 61 hile other threa
3d770 64 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 74 ds are running t
3d780 68 65 20 73 61 6d 65 20 6f 72 20 64 69 66 66 65 he same or diffe
3d790 72 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a 20 69 rent SQLite.** i
3d7a0 6e 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65 nterfaces. Howe
3d7b0 76 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 72 ver the values r
3d7c0 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 75 72 eturned in *pCur
3d7d0 72 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70 48 69 rent and.** *pHi
3d7e0 67 68 77 61 74 65 72 20 72 65 66 6c 65 63 74 20 ghwater reflect
3d7f0 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 53 51 the status of SQ
3d800 4c 69 74 65 20 61 74 20 64 69 66 66 65 72 65 6e Lite at differen
3d810 74 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 6d 65 t points in time
3d820 0a 2a 2a 20 61 6e 64 20 69 74 20 69 73 20 70 6f .** and it is po
3d830 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 ssible that anot
3d840 68 65 72 20 74 68 72 65 61 64 20 6d 69 67 68 74 her thread might
3d850 20 63 68 61 6e 67 65 20 74 68 65 20 70 61 72 61 change the para
3d860 6d 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65 74 77 meter.** in betw
3d870 65 65 6e 20 74 68 65 20 74 69 6d 65 73 20 77 68 een the times wh
3d880 65 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e 64 en *pCurrent and
3d890 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 72 65 *pHighwater are
3d8a0 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 written..**.**
3d8b0 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 See also: [sqlit
3d8c0 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 0a e3_db_status()].
3d8d0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 */.SQLITE_API SQ
3d8e0 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 LITE_EXPERIMENTA
3d8f0 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 L int sqlite3_st
3d900 61 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 atus(int op, int
3d910 20 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 *pCurrent, int
3d920 2a 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74 *pHighwater, int
3d930 20 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a 0a 2f resetFlag);.../
3d940 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 *.** CAPI3REF: S
3d950 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 73 tatus Parameters
3d960 20 7b 48 31 37 32 35 30 7d 20 3c 48 31 37 32 30 {H17250} <H1720
3d970 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
3d980 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 AL.**.** These i
3d990 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 nteger constants
3d9a0 20 64 65 73 69 67 6e 61 74 65 20 76 61 72 69 6f designate vario
3d9b0 75 73 20 72 75 6e 2d 74 69 6d 65 20 73 74 61 74 us run-time stat
3d9c0 75 73 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a us parameters.**
3d9d0 20 74 68 61 74 20 63 61 6e 20 62 65 20 72 65 74 that can be ret
3d9e0 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 urned by [sqlite
3d9f0 33 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 3_status()]..**.
3da00 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 ** <dl>.** <dt>S
3da10 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d QLITE_STATUS_MEM
3da20 4f 52 59 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a ORY_USED</dt>.**
3da30 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 <dd>This parame
3da40 74 65 72 20 69 73 20 74 68 65 20 63 75 72 72 65 ter is the curre
3da50 6e 74 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d nt amount of mem
3da60 6f 72 79 20 63 68 65 63 6b 65 64 20 6f 75 74 0a ory checked out.
3da70 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 ** using [sqlite
3da80 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 65 69 74 3_malloc()], eit
3da90 68 65 72 20 64 69 72 65 63 74 6c 79 20 6f 72 20 her directly or
3daa0 69 6e 64 69 72 65 63 74 6c 79 2e 20 20 54 68 65 indirectly. The
3dab0 0a 2a 2a 20 66 69 67 75 72 65 20 69 6e 63 6c 75 .** figure inclu
3dac0 64 65 73 20 63 61 6c 6c 73 20 6d 61 64 65 20 74 des calls made t
3dad0 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f o [sqlite3_mallo
3dae0 63 28 29 5d 20 62 79 20 74 68 65 20 61 70 70 6c c()] by the appl
3daf0 69 63 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 69 ication.** and i
3db00 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 75 nternal memory u
3db10 73 61 67 65 20 62 79 20 74 68 65 20 53 51 4c 69 sage by the SQLi
3db20 74 65 20 6c 69 62 72 61 72 79 2e 20 20 53 63 72 te library. Scr
3db30 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 63 atch memory.** c
3db40 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b 53 51 ontrolled by [SQ
3db50 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 LITE_CONFIG_SCRA
3db60 54 43 48 5d 20 61 6e 64 20 61 75 78 69 6c 69 61 TCH] and auxilia
3db70 72 79 20 70 61 67 65 2d 63 61 63 68 65 0a 2a 2a ry page-cache.**
3db80 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 72 6f 6c 6c memory controll
3db90 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f ed by [SQLITE_CO
3dba0 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 20 NFIG_PAGECACHE]
3dbb0 69 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 is not included
3dbc0 69 6e 0a 2a 2a 20 74 68 69 73 20 70 61 72 61 6d in.** this param
3dbd0 65 74 65 72 2e 20 20 54 68 65 20 61 6d 6f 75 6e eter. The amoun
3dbe0 74 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 t returned is th
3dbf0 65 20 73 75 6d 20 6f 66 20 74 68 65 20 61 6c 6c e sum of the all
3dc00 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73 69 7a 65 73 ocation.** sizes
3dc10 20 61 73 20 72 65 70 6f 72 74 65 64 20 62 79 20 as reported by
3dc20 74 68 65 20 78 53 69 7a 65 20 6d 65 74 68 6f 64 the xSize method
3dc30 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d in [sqlite3_mem
3dc40 5f 6d 65 74 68 6f 64 73 5d 2e 3c 2f 64 64 3e 0a _methods].</dd>.
3dc50 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
3dc60 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 _STATUS_MALLOC_S
3dc70 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e IZE</dt>.** <dd>
3dc80 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 This parameter r
3dc90 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 ecords the large
3dca0 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 st memory alloca
3dcb0 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 tion request.**
3dcc0 68 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c 69 74 handed to [sqlit
3dcd0 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 e3_malloc()] or
3dce0 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 [sqlite3_realloc
3dcf0 28 29 5d 20 28 6f 72 20 74 68 65 69 72 0a 2a 2a ()] (or their.**
3dd00 20 69 6e 74 65 72 6e 61 6c 20 65 71 75 69 76 61 internal equiva
3dd10 6c 65 6e 74 73 29 2e 20 20 4f 6e 6c 79 20 74 68 lents). Only th
3dd20 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
3dd30 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 in the.** *pHig
3dd40 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 hwater parameter
3dd50 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 to [sqlite3_sta
3dd60 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 tus()] is of int
3dd70 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 erest. .** The
3dd80 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e value written in
3dd90 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 to the *pCurrent
3dda0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e parameter is un
3ddb0 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a defined.</dd>.**
3ddc0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 .** <dt>SQLITE_S
3ddd0 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f TATUS_PAGECACHE_
3dde0 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 USED</dt>.** <dd
3ddf0 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 >This parameter
3de00 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 returns the numb
3de10 65 72 20 6f 66 20 70 61 67 65 73 20 75 73 65 64 er of pages used
3de20 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 5b out of the.** [
3de30 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 pagecache memory
3de40 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 74 68 61 74 allocator] that
3de50 20 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20 was configured
3de60 75 73 69 6e 67 20 0a 2a 2a 20 5b 53 51 4c 49 54 using .** [SQLIT
3de70 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 E_CONFIG_PAGECAC
3de80 48 45 5d 2e 20 20 54 68 65 0a 2a 2a 20 76 61 6c HE]. The.** val
3de90 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 69 ue returned is i
3dea0 6e 20 70 61 67 65 73 2c 20 6e 6f 74 20 69 6e 20 n pages, not in
3deb0 62 79 74 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a bytes.</dd>.**.*
3dec0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 * <dt>SQLITE_STA
3ded0 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 TUS_PAGECACHE_OV
3dee0 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c ERFLOW</dt>.** <
3def0 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 dd>This paramete
3df00 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 r returns the nu
3df10 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
3df20 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 61 page cache.** a
3df30 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20 llocation which
3df40 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 could not be sta
3df50 74 69 73 66 69 65 64 20 62 79 20 74 68 65 20 5b tisfied by the [
3df60 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 SQLITE_CONFIG_PA
3df70 47 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75 66 66 GECACHE].** buff
3df80 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f 72 er and where for
3df90 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 ced to overflow
3dfa0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c to [sqlite3_mall
3dfb0 6f 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20 72 oc()]. The.** r
3dfc0 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 6e eturned value in
3dfd0 63 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74 69 6f cludes allocatio
3dfe0 6e 73 20 74 68 61 74 20 6f 76 65 72 66 6c 6f 77 ns that overflow
3dff0 65 64 20 62 65 63 61 75 73 65 20 74 68 65 79 0a ed because they.
3e000 2a 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c 61 72 ** where too lar
3e010 67 65 20 28 74 68 65 79 20 77 65 72 65 20 6c 61 ge (they were la
3e020 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 22 73 rger than the "s
3e030 7a 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a z" parameter to.
3e040 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ** [SQLITE_CONFI
3e050 47 5f 50 41 47 45 43 41 43 48 45 5d 29 20 61 6e G_PAGECACHE]) an
3e060 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 d allocations th
3e070 61 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65 at overflowed be
3e080 63 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70 61 63 cause.** no spac
3e090 65 20 77 61 73 20 6c 65 66 74 20 69 6e 20 74 68 e was left in th
3e0a0 65 20 70 61 67 65 20 63 61 63 68 65 2e 3c 2f 64 e page cache.</d
3e0b0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
3e0c0 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 ITE_STATUS_PAGEC
3e0d0 41 43 48 45 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a ACHE_SIZE</dt>.*
3e0e0 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d * <dd>This param
3e0f0 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 eter records the
3e100 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 largest memory
3e110 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 allocation reque
3e120 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 st.** handed to
3e130 5b 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 [pagecache memor
3e140 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f y allocator]. O
3e150 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65 nly the value re
3e160 74 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a turned in the.**
3e170 20 2a 70 48 69 67 68 77 61 74 65 72 20 70 61 72 *pHighwater par
3e180 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 ameter to [sqlit
3e190 65 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20 e3_status()] is
3e1a0 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a of interest. .*
3e1b0 2a 20 54 68 65 20 76 61 6c 75 65 20 77 72 69 74 * The value writ
3e1c0 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43 ten into the *pC
3e1d0 75 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72 urrent parameter
3e1e0 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f is undefined.</
3e1f0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
3e200 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 LITE_STATUS_SCRA
3e210 54 43 48 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a TCH_USED</dt>.**
3e220 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 <dd>This parame
3e230 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 ter returns the
3e240 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 number of alloca
3e250 74 69 6f 6e 73 20 75 73 65 64 20 6f 75 74 20 6f tions used out o
3e260 66 20 74 68 65 0a 2a 2a 20 5b 73 63 72 61 74 63 f the.** [scratc
3e270 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 h memory allocat
3e280 6f 72 5d 20 63 6f 6e 66 69 67 75 72 65 64 20 75 or] configured u
3e290 73 69 6e 67 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f sing.** [SQLITE_
3e2a0 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 2e CONFIG_SCRATCH].
3e2b0 20 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 The value retu
3e2c0 72 6e 65 64 20 69 73 20 69 6e 20 61 6c 6c 6f 63 rned is in alloc
3e2d0 61 74 69 6f 6e 73 2c 20 6e 6f 74 0a 2a 2a 20 69 ations, not.** i
3e2e0 6e 20 62 79 74 65 73 2e 20 20 53 69 6e 63 65 20 n bytes. Since
3e2f0 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20 a single thread
3e300 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 6f 6e may only have on
3e310 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 e scratch alloca
3e320 74 69 6f 6e 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 tion.** outstand
3e330 69 6e 67 20 61 74 20 74 69 6d 65 2c 20 74 68 69 ing at time, thi
3e340 73 20 70 61 72 61 6d 65 74 65 72 20 61 6c 73 6f s parameter also
3e350 20 72 65 70 6f 72 74 73 20 74 68 65 20 6e 75 6d reports the num
3e360 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 0a 2a ber of threads.*
3e370 2a 20 75 73 69 6e 67 20 73 63 72 61 74 63 68 20 * using scratch
3e380 6d 65 6d 6f 72 79 20 61 74 20 74 68 65 20 73 61 memory at the sa
3e390 6d 65 20 74 69 6d 65 2e 3c 2f 64 64 3e 0a 2a 2a me time.</dd>.**
3e3a0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 .** <dt>SQLITE_S
3e3b0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 TATUS_SCRATCH_OV
3e3c0 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c ERFLOW</dt>.** <
3e3d0 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 dd>This paramete
3e3e0 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 r returns the nu
3e3f0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
3e400 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 0a scratch memory.
3e410 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 ** allocation wh
3e420 69 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 ich could not be
3e430 20 73 74 61 74 69 73 66 69 65 64 20 62 79 20 74 statisfied by t
3e440 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 he [SQLITE_CONFI
3e450 47 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20 62 75 G_SCRATCH].** bu
3e460 66 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 ffer and where f
3e470 6f 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f orced to overflo
3e480 77 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 w to [sqlite3_ma
3e490 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 76 61 lloc()]. The va
3e4a0 6c 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 lues.** returned
3e4b0 20 69 6e 63 6c 75 64 65 20 6f 76 65 72 66 6c 6f include overflo
3e4c0 77 73 20 62 65 63 61 75 73 65 20 74 68 65 20 72 ws because the r
3e4d0 65 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 equested allocat
3e4e0 69 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a 20 6c ion was too.** l
3e4f0 61 72 67 65 72 20 28 74 68 61 74 20 69 73 2c 20 arger (that is,
3e500 62 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75 because the requ
3e510 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e ested allocation
3e520 20 77 61 73 20 6c 61 72 67 65 72 20 74 68 61 6e was larger than
3e530 20 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70 61 72 the.** "sz" par
3e540 61 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c 49 54 ameter to [SQLIT
3e550 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 E_CONFIG_SCRATCH
3e560 5d 29 20 61 6e 64 20 62 65 63 61 75 73 65 20 6e ]) and because n
3e570 6f 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 o scratch buffer
3e580 0a 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65 20 61 .** slots were a
3e590 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c 2f 64 vailable..** </d
3e5a0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
3e5b0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 ITE_STATUS_SCRAT
3e5c0 43 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 CH_SIZE</dt>.**
3e5d0 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 <dd>This paramet
3e5e0 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c er records the l
3e5f0 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c argest memory al
3e600 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 location request
3e610 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 .** handed to [s
3e620 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c cratch memory al
3e630 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 locator]. Only
3e640 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
3e650 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 ed in the.** *pH
3e660 69 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 ighwater paramet
3e670 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 er to [sqlite3_s
3e680 74 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 tatus()] is of i
3e690 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 nterest. .** Th
3e6a0 65 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 e value written
3e6b0 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 into the *pCurre
3e6c0 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 nt parameter is
3e6d0 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a undefined.</dd>.
3e6e0 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
3e6f0 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53 _STATUS_PARSER_S
3e700 54 41 43 4b 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 TACK</dt>.** <dd
3e710 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 >This parameter
3e720 72 65 63 6f 72 64 73 20 74 68 65 20 64 65 65 70 records the deep
3e730 65 73 74 20 70 61 72 73 65 72 20 73 74 61 63 6b est parser stack
3e740 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a . It is only.**
3e750 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 53 meaningful if S
3e760 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 QLite is compile
3e770 64 20 77 69 74 68 20 5b 59 59 54 52 41 43 4b 4d d with [YYTRACKM
3e780 41 58 53 54 41 43 4b 44 45 50 54 48 5d 2e 3c 2f AXSTACKDEPTH].</
3e790 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a dd>.** </dl>.**.
3e7a0 2a 2a 20 4e 65 77 20 73 74 61 74 75 73 20 70 61 ** New status pa
3e7b0 72 61 6d 65 74 65 72 73 20 6d 61 79 20 62 65 20 rameters may be
3e7c0 61 64 64 65 64 20 66 72 6f 6d 20 74 69 6d 65 20 added from time
3e7d0 74 6f 20 74 69 6d 65 2e 0a 2a 2f 0a 23 64 65 66 to time..*/.#def
3e7e0 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 ine SQLITE_STATU
3e7f0 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20 20 20 S_MEMORY_USED
3e800 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 0.#define
3e810 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 SQLITE_STATUS_P
3e820 41 47 45 43 41 43 48 45 5f 55 53 45 44 20 20 20 AGECACHE_USED
3e830 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 1.#define SQ
3e840 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 LITE_STATUS_PAGE
3e850 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 20 20 CACHE_OVERFLOW
3e860 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 2.#define SQLIT
3e870 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 E_STATUS_SCRATCH
3e880 5f 55 53 45 44 20 20 20 20 20 20 20 20 20 33 0a _USED 3.
3e890 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
3e8a0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 TATUS_SCRATCH_OV
3e8b0 45 52 46 4c 4f 57 20 20 20 20 20 34 0a 23 64 65 ERFLOW 4.#de
3e8c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 fine SQLITE_STAT
3e8d0 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 20 20 US_MALLOC_SIZE
3e8e0 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 5.#defin
3e8f0 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f e SQLITE_STATUS_
3e900 50 41 52 53 45 52 5f 53 54 41 43 4b 20 20 20 20 PARSER_STACK
3e910 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 53 6.#define S
3e920 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 QLITE_STATUS_PAG
3e930 45 43 41 43 48 45 5f 53 49 5a 45 20 20 20 20 20 ECACHE_SIZE
3e940 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 7.#define SQLI
3e950 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 TE_STATUS_SCRATC
3e960 48 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 38 H_SIZE 8
3e970 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
3e980 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 : Database Conne
3e990 63 74 69 6f 6e 20 53 74 61 74 75 73 20 7b 48 31 ction Status {H1
3e9a0 37 35 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 7500} <S60200>.*
3e9b0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
3e9c0 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 *.** This interf
3e9d0 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 ace is used to r
3e9e0 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65 20 etrieve runtime
3e9f0 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 status informati
3ea00 6f 6e 20 0a 2a 2a 20 61 62 6f 75 74 20 61 20 73 on .** about a s
3ea10 69 6e 67 6c 65 20 5b 64 61 74 61 62 61 73 65 20 ingle [database
3ea20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 68 connection]. Th
3ea30 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
3ea40 20 69 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 is the.** datab
3ea50 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f ase connection o
3ea60 62 6a 65 63 74 20 74 6f 20 62 65 20 69 6e 74 65 bject to be inte
3ea70 72 72 6f 67 61 74 65 64 2e 20 20 54 68 65 20 73 rrogated. The s
3ea80 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a econd argument.*
3ea90 2a 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 74 * is the paramet
3eaa0 65 72 20 74 6f 20 69 6e 74 65 72 72 6f 67 61 74 er to interrogat
3eab0 65 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 e. Currently, t
3eac0 68 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 he only allowed
3ead0 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 value.** for the
3eae0 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
3eaf0 72 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 53 r is [SQLITE_DBS
3eb00 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f TATUS_LOOKASIDE_
3eb10 55 53 45 44 5d 2e 0a 2a 2a 20 41 64 64 69 74 69 USED]..** Additi
3eb20 6f 6e 61 6c 20 6f 70 74 69 6f 6e 73 20 77 69 6c onal options wil
3eb30 6c 20 6c 69 6b 65 6c 79 20 61 70 70 65 61 72 20 l likely appear
3eb40 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 in future releas
3eb50 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a es of SQLite..**
3eb60 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 .** The current
3eb70 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65 71 value of the req
3eb80 75 65 73 74 65 64 20 70 61 72 61 6d 65 74 65 72 uested parameter
3eb90 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f is written into
3eba0 20 2a 70 43 75 72 0a 2a 2a 20 61 6e 64 20 74 68 *pCur.** and th
3ebb0 65 20 68 69 67 68 65 73 74 20 69 6e 73 74 61 6e e highest instan
3ebc0 74 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69 73 taneous value is
3ebd0 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 written into *p
3ebe0 48 69 77 74 72 2e 20 20 49 66 0a 2a 2a 20 74 68 Hiwtr. If.** th
3ebf0 65 20 72 65 73 65 74 46 6c 67 20 69 73 20 74 72 e resetFlg is tr
3ec00 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69 67 ue, then the hig
3ec10 68 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f hest instantaneo
3ec20 75 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 72 us value is.** r
3ec30 65 73 65 74 20 62 61 63 6b 20 64 6f 77 6e 20 74 eset back down t
3ec40 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 o the current va
3ec50 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 lue..**.** See a
3ec60 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 lso: [sqlite3_st
3ec70 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c atus()] and [sql
3ec80 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 ite3_stmt_status
3ec90 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ()]..*/.SQLITE_A
3eca0 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 PI SQLITE_EXPERI
3ecb0 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 MENTAL int sqlit
3ecc0 65 33 5f 64 62 5f 73 74 61 74 75 73 28 73 71 6c e3_db_status(sql
3ecd0 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 69 ite3*, int op, i
3ece0 6e 74 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 nt *pCur, int *p
3ecf0 48 69 77 74 72 2c 20 69 6e 74 20 72 65 73 65 74 Hiwtr, int reset
3ed00 46 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 Flg);../*.** CAP
3ed10 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 I3REF: Status Pa
3ed20 72 61 6d 65 74 65 72 73 20 66 6f 72 20 64 61 74 rameters for dat
3ed30 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
3ed40 73 20 7b 48 31 37 35 32 30 7d 20 3c 48 31 37 35 s {H17520} <H175
3ed50 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 00>.** EXPERIMEN
3ed60 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 TAL.**.** These
3ed70 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 constants are th
3ed80 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 e available inte
3ed90 67 65 72 20 22 76 65 72 62 73 22 20 74 68 61 74 ger "verbs" that
3eda0 20 63 61 6e 20 62 65 20 70 61 73 73 65 64 20 61 can be passed a
3edb0 73 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 s.** the second
3edc0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
3edd0 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 [sqlite3_db_stat
3ede0 75 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e us()] interface.
3edf0 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 76 65 72 62 73 .**.** New verbs
3ee00 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e may be added in
3ee10 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
3ee20 20 6f 66 20 53 51 4c 69 74 65 2e 20 45 78 69 73 of SQLite. Exis
3ee30 74 69 6e 67 20 76 65 72 62 73 0a 2a 2a 20 6d 69 ting verbs.** mi
3ee40 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 6e ght be discontin
3ee50 75 65 64 2e 20 41 70 70 6c 69 63 61 74 69 6f 6e ued. Application
3ee60 73 20 73 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 s should check t
3ee70 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 he return code f
3ee80 72 6f 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f rom.** [sqlite3_
3ee90 64 62 5f 73 74 61 74 75 73 28 29 5d 20 74 6f 20 db_status()] to
3eea0 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 make sure that t
3eeb0 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 0a he call worked..
3eec0 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f ** The [sqlite3_
3eed0 64 62 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 db_status()] int
3eee0 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74 75 erface will retu
3eef0 72 6e 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 65 72 rn a non-zero er
3ef00 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 66 20 61 ror code.** if a
3ef10 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72 discontinued or
3ef20 20 75 6e 73 75 70 70 6f 72 74 65 64 20 76 65 72 unsupported ver
3ef30 62 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a b is invoked..**
3ef40 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e .** <dl>.** <dt>
3ef50 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f SQLITE_DBSTATUS_
3ef60 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3c 2f LOOKASIDE_USED</
3ef70 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 dt>.** <dd>This
3ef80 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e parameter return
3ef90 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
3efa0 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 lookaside memory
3efb0 20 73 6c 6f 74 73 20 63 75 72 72 65 6e 74 6c 79 slots currently
3efc0 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 2e .** checked out.
3efd0 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a </dd>.** </dl>.*
3efe0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
3eff0 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 _DBSTATUS_LOOKAS
3f000 49 44 45 5f 55 53 45 44 20 20 20 20 20 30 0a 0a IDE_USED 0..
3f010 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
3f020 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d Prepared Statem
3f030 65 6e 74 20 53 74 61 74 75 73 20 7b 48 31 37 35 ent Status {H175
3f040 35 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 50} <S60200>.**
3f050 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
3f060 2a 2a 20 45 61 63 68 20 70 72 65 70 61 72 65 64 ** Each prepared
3f070 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 69 6e 74 statement maint
3f080 61 69 6e 73 20 76 61 72 69 6f 75 73 0a 2a 2a 20 ains various.**
3f090 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 [SQLITE_STMTSTAT
3f0a0 55 53 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 US_SORT | counte
3f0b0 72 73 5d 20 74 68 61 74 20 6d 65 61 73 75 72 65 rs] that measure
3f0c0 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f the number.** o
3f0d0 66 20 74 69 6d 65 73 20 69 74 20 68 61 73 20 70 f times it has p
3f0e0 65 72 66 6f 72 6d 65 64 20 73 70 65 63 69 66 69 erformed specifi
3f0f0 63 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 54 c operations. T
3f100 68 65 73 65 20 63 6f 75 6e 74 65 72 73 20 63 61 hese counters ca
3f110 6e 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 n.** be used to
3f120 6d 6f 6e 69 74 6f 72 20 74 68 65 20 70 65 72 66 monitor the perf
3f130 6f 72 6d 61 6e 63 65 20 63 68 61 72 61 63 74 65 ormance characte
3f140 72 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 70 ristics of the p
3f150 72 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 repared.** state
3f160 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65 78 61 6d ments. For exam
3f170 70 6c 65 2c 20 69 66 20 74 68 65 20 6e 75 6d 62 ple, if the numb
3f180 65 72 20 6f 66 20 74 61 62 6c 65 20 73 74 65 70 er of table step
3f190 73 20 67 72 65 61 74 6c 79 20 65 78 63 65 65 64 s greatly exceed
3f1a0 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 s.** the number
3f1b0 6f 66 20 74 61 62 6c 65 20 73 65 61 72 63 68 65 of table searche
3f1c0 73 20 6f 72 20 72 65 73 75 6c 74 20 72 6f 77 73 s or result rows
3f1d0 2c 20 74 68 61 74 20 77 6f 75 6c 64 20 74 65 6e , that would ten
3f1e0 64 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a d to indicate.**
3f1f0 20 74 68 61 74 20 74 68 65 20 70 72 65 70 61 72 that the prepar
3f200 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 ed statement is
3f210 75 73 69 6e 67 20 61 20 66 75 6c 6c 20 74 61 62 using a full tab
3f220 6c 65 20 73 63 61 6e 20 72 61 74 68 65 72 20 74 le scan rather t
3f230 68 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e han.** an index.
3f240 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e .**.** This in
3f250 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 terface is used
3f260 74 6f 20 72 65 74 72 69 65 76 65 20 61 6e 64 20 to retrieve and
3f270 72 65 73 65 74 20 63 6f 75 6e 74 65 72 20 76 61 reset counter va
3f280 6c 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b lues from.** a [
3f290 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
3f2a0 6e 74 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20 nt]. The first
3f2b0 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
3f2c0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
3f2d0 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 6f 20 nt.** object to
3f2e0 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e be interrogated.
3f2f0 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 The second arg
3f300 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69 ument.** is an i
3f310 6e 74 65 67 65 72 20 63 6f 64 65 20 66 6f 72 20 nteger code for
3f320 61 20 73 70 65 63 69 66 69 63 20 5b 53 51 4c 49 a specific [SQLI
3f330 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f TE_STMTSTATUS_SO
3f340 52 54 20 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a RT | counter].**
3f350 20 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 to be interroga
3f360 74 65 64 2e 20 0a 2a 2a 20 54 68 65 20 63 75 72 ted. .** The cur
3f370 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 rent value of th
3f380 65 20 72 65 71 75 65 73 74 65 64 20 63 6f 75 6e e requested coun
3f390 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e ter is returned.
3f3a0 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 65 74 .** If the reset
3f3b0 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 Flg is true, the
3f3c0 6e 20 74 68 65 20 63 6f 75 6e 74 65 72 20 69 73 n the counter is
3f3d0 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 reset to zero a
3f3e0 66 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 fter this.** int
3f3f0 65 72 66 61 63 65 20 63 61 6c 6c 20 72 65 74 75 erface call retu
3f400 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 rns..**.** See a
3f410 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 lso: [sqlite3_st
3f420 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c atus()] and [sql
3f430 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 ite3_db_status()
3f440 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 ]..*/.SQLITE_API
3f450 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 SQLITE_EXPERIME
3f460 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 NTAL int sqlite3
3f470 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71 6c _stmt_status(sql
3f480 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 ite3_stmt*, int
3f490 6f 70 2c 69 6e 74 20 72 65 73 65 74 46 6c 67 29 op,int resetFlg)
3f4a0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
3f4b0 46 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d 65 F: Status Parame
3f4c0 74 65 72 73 20 66 6f 72 20 70 72 65 70 61 72 65 ters for prepare
3f4d0 64 20 73 74 61 74 65 6d 65 6e 74 73 20 7b 48 31 d statements {H1
3f4e0 37 35 37 30 7d 20 3c 48 31 37 35 35 30 3e 0a 2a 7570} <H17550>.*
3f4f0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
3f500 2a 0a 2a 2a 20 54 68 65 73 65 20 70 72 65 70 72 *.** These prepr
3f510 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 64 ocessor macros d
3f520 65 66 69 6e 65 20 69 6e 74 65 67 65 72 20 63 6f efine integer co
3f530 64 65 73 20 74 68 61 74 20 6e 61 6d 65 20 63 6f des that name co
3f540 75 6e 74 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20 unter.** values
3f550 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
3f560 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d the [sqlite3_stm
3f570 74 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65 t_status()] inte
3f580 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 rface..** The me
3f590 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61 anings of the va
3f5a0 72 69 6f 75 73 20 63 6f 75 6e 74 65 72 73 20 61 rious counters a
3f5b0 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a re as follows:.*
3f5c0 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 *.** <dl>.** <dt
3f5d0 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 >SQLITE_STMTSTAT
3f5e0 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 US_FULLSCAN_STEP
3f5f0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
3f600 73 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 s is the number
3f610 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 53 51 of times that SQ
3f620 4c 69 74 65 20 68 61 73 20 73 74 65 70 70 65 64 Lite has stepped
3f630 20 66 6f 72 77 61 72 64 20 69 6e 0a 2a 2a 20 61 forward in.** a
3f640 20 74 61 62 6c 65 20 61 73 20 70 61 72 74 20 6f table as part o
3f650 66 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 f a full table s
3f660 63 61 6e 2e 20 20 4c 61 72 67 65 20 6e 75 6d 62 can. Large numb
3f670 65 72 73 20 66 6f 72 20 74 68 69 73 20 63 6f 75 ers for this cou
3f680 6e 74 65 72 0a 2a 2a 20 6d 61 79 20 69 6e 64 69 nter.** may indi
3f690 63 61 74 65 20 6f 70 70 6f 72 74 75 6e 69 74 69 cate opportuniti
3f6a0 65 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e es for performan
3f6b0 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 74 ce improvement t
3f6c0 68 72 6f 75 67 68 20 0a 2a 2a 20 63 61 72 65 66 hrough .** caref
3f6d0 75 6c 20 75 73 65 20 6f 66 20 69 6e 64 69 63 65 ul use of indice
3f6e0 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 s.</dd>.**.** <d
3f6f0 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 t>SQLITE_STMTSTA
3f700 54 55 53 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a TUS_SORT</dt>.**
3f710 20 3c 64 64 3e 54 68 69 73 20 69 73 20 74 68 65 <dd>This is the
3f720 20 6e 75 6d 62 65 72 20 6f 66 20 73 6f 72 74 20 number of sort
3f730 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74 20 operations that
3f740 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a have occurred..*
3f750 2a 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c * A non-zero val
3f760 75 65 20 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 ue in this count
3f770 65 72 20 6d 61 79 20 69 6e 64 69 63 61 74 65 20 er may indicate
3f780 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 an opportunity t
3f790 6f 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74 o.** improvement
3f7a0 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 68 72 performance thr
3f7b0 6f 75 67 68 20 63 61 72 65 66 75 6c 20 75 73 65 ough careful use
3f7c0 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64 of indices.</dd
3f7d0 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f >.**.** </dl>.*/
3f7e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3f7f0 53 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 STMTSTATUS_FULLS
3f800 43 41 4e 5f 53 54 45 50 20 20 20 20 20 31 0a 23 CAN_STEP 1.#
3f810 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
3f820 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20 20 20 MTSTATUS_SORT
3f830 20 20 20 20 20 20 20 20 20 20 20 32 0a 0a 2f 2a 2../*
3f840 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 75 .** CAPI3REF: Cu
3f850 73 74 6f 6d 20 50 61 67 65 20 43 61 63 68 65 20 stom Page Cache
3f860 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49 Object.** EXPERI
3f870 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 MENTAL.**.** The
3f880 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 sqlite3_pcache
3f890 74 79 70 65 20 69 73 20 6f 70 61 71 75 65 2e 20 type is opaque.
3f8a0 20 49 74 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 It is implement
3f8b0 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 6c 75 ed by.** the plu
3f8c0 67 67 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 ggable module.
3f8d0 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 The SQLite core
3f8e0 68 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65 has no knowledge
3f8f0 20 6f 66 0a 2a 2a 20 69 74 73 20 73 69 7a 65 20 of.** its size
3f900 6f 72 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 or internal stru
3f910 63 74 75 72 65 20 61 6e 64 20 6e 65 76 65 72 20 cture and never
3f920 64 65 61 6c 73 20 77 69 74 68 20 74 68 65 0a 2a deals with the.*
3f930 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 * sqlite3_pcache
3f940 20 6f 62 6a 65 63 74 20 65 78 63 65 70 74 20 62 object except b
3f950 79 20 68 6f 6c 64 69 6e 67 20 61 6e 64 20 70 61 y holding and pa
3f960 73 73 69 6e 67 20 70 6f 69 6e 74 65 72 73 0a 2a ssing pointers.*
3f970 2a 20 74 6f 20 74 68 65 20 6f 62 6a 65 63 74 2e * to the object.
3f980 0a 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73 71 6c 69 .**.** See [sqli
3f990 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
3f9a0 64 73 5d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e ds] for addition
3f9b0 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a al information..
3f9c0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
3f9d0 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 t sqlite3_pcache
3f9e0 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 3b sqlite3_pcache;
3f9f0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
3fa00 3a 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 44 65 : Application De
3fa10 66 69 6e 65 64 20 50 61 67 65 20 43 61 63 68 65 fined Page Cache
3fa20 2e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b ..** KEYWORDS: {
3fa30 70 61 67 65 20 63 61 63 68 65 7d 0a 2a 2a 20 45 page cache}.** E
3fa40 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a XPERIMENTAL.**.*
3fa50 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 * The [sqlite3_c
3fa60 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 onfig]([SQLITE_C
3fa70 4f 4e 46 49 47 5f 50 43 41 43 48 45 5d 2c 20 2e ONFIG_PCACHE], .
3fa80 2e 2e 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 ..) interface ca
3fa90 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 61 6e n.** register an
3faa0 20 61 6c 74 65 72 6e 61 74 69 76 65 20 70 61 67 alternative pag
3fab0 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e e cache implemen
3fac0 74 61 74 69 6f 6e 20 62 79 20 70 61 73 73 69 6e tation by passin
3fad0 67 20 69 6e 20 61 6e 20 0a 2a 2a 20 69 6e 73 74 g in an .** inst
3fae0 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69 ance of the sqli
3faf0 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
3fb00 64 73 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 ds structure. Th
3fb10 65 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20 74 68 e majority of th
3fb20 65 20 0a 2a 2a 20 68 65 61 70 20 6d 65 6d 6f 72 e .** heap memor
3fb30 79 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 y used by SQLite
3fb40 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 is used by the
3fb50 70 61 67 65 20 63 61 63 68 65 20 74 6f 20 63 61 page cache to ca
3fb60 63 68 65 20 64 61 74 61 20 72 65 61 64 20 0a 2a che data read .*
3fb70 2a 20 66 72 6f 6d 2c 20 6f 72 20 72 65 61 64 79 * from, or ready
3fb80 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74 to be written t
3fb90 6f 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 o, the database
3fba0 66 69 6c 65 2e 20 42 79 20 69 6d 70 6c 65 6d 65 file. By impleme
3fbb0 6e 74 69 6e 67 20 61 20 0a 2a 2a 20 63 75 73 74 nting a .** cust
3fbc0 6f 6d 20 70 61 67 65 20 63 61 63 68 65 20 75 73 om page cache us
3fbd0 69 6e 67 20 74 68 69 73 20 41 50 49 2c 20 61 6e ing this API, an
3fbe0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e application can
3fbf0 20 63 6f 6e 74 72 6f 6c 20 6d 6f 72 65 20 0a 2a control more .*
3fc00 2a 20 70 72 65 63 69 73 65 6c 79 20 74 68 65 20 * precisely the
3fc10 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 amount of memory
3fc20 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 53 51 4c consumed by SQL
3fc30 69 74 65 2c 20 74 68 65 20 77 61 79 20 69 6e 20 ite, the way in
3fc40 77 68 69 63 68 20 0a 2a 2a 20 74 68 61 74 20 6d which .** that m
3fc50 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74 emory is allocat
3fc60 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64 2c ed and released,
3fc70 20 61 6e 64 20 74 68 65 20 70 6f 6c 69 63 69 65 and the policie
3fc80 73 20 75 73 65 64 20 74 6f 20 0a 2a 2a 20 64 65 s used to .** de
3fc90 74 65 72 6d 69 6e 65 20 65 78 61 63 74 6c 79 20 termine exactly
3fca0 77 68 69 63 68 20 70 61 72 74 73 20 6f 66 20 61 which parts of a
3fcb0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
3fcc0 72 65 20 63 61 63 68 65 64 20 61 6e 64 20 66 6f re cached and fo
3fcd0 72 20 0a 2a 2a 20 68 6f 77 20 6c 6f 6e 67 2e 0a r .** how long..
3fce0 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e **.** The conten
3fcf0 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 ts of the sqlite
3fd00 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 3_pcache_methods
3fd10 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 63 structure are c
3fd20 6f 70 69 65 64 20 74 6f 20 61 6e 0a 2a 2a 20 69 opied to an.** i
3fd30 6e 74 65 72 6e 61 6c 20 62 75 66 66 65 72 20 62 nternal buffer b
3fd40 79 20 53 51 4c 69 74 65 20 77 69 74 68 69 6e 20 y SQLite within
3fd50 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c the call to [sql
3fd60 69 74 65 33 5f 63 6f 6e 66 69 67 5d 2e 20 20 48 ite3_config]. H
3fd70 65 6e 63 65 0a 2a 2a 20 74 68 65 20 61 70 70 6c ence.** the appl
3fd80 69 63 61 74 69 6f 6e 20 6d 61 79 20 64 69 73 63 ication may disc
3fd90 61 72 64 20 74 68 65 20 70 61 72 61 6d 65 74 65 ard the paramete
3fda0 72 20 61 66 74 65 72 20 74 68 65 20 63 61 6c 6c r after the call
3fdb0 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f to.** [sqlite3_
3fdc0 63 6f 6e 66 69 67 28 29 5d 20 72 65 74 75 72 6e config()] return
3fdd0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e s..**.** The xIn
3fde0 69 74 28 29 20 6d 65 74 68 6f 64 20 69 73 20 63 it() method is c
3fdf0 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 alled once for e
3fe00 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c ach call to [sql
3fe10 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
3fe20 29 5d 0a 2a 2a 20 28 75 73 75 61 6c 6c 79 20 6f )].** (usually o
3fe30 6e 6c 79 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 nly once during
3fe40 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 the lifetime of
3fe50 74 68 65 20 70 72 6f 63 65 73 73 29 2e 20 49 74 the process). It
3fe60 20 69 73 20 70 61 73 73 65 64 0a 2a 2a 20 61 20 is passed.** a
3fe70 63 6f 70 79 20 6f 66 20 74 68 65 20 73 71 6c 69 copy of the sqli
3fe80 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
3fe90 64 73 2e 70 41 72 67 20 76 61 6c 75 65 2e 20 49 ds.pArg value. I
3fea0 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f t can be used to
3feb0 20 73 65 74 0a 2a 2a 20 75 70 20 67 6c 6f 62 61 set.** up globa
3fec0 6c 20 73 74 72 75 63 74 75 72 65 73 20 61 6e 64 l structures and
3fed0 20 6d 75 74 65 78 65 73 20 72 65 71 75 69 72 65 mutexes require
3fee0 64 20 62 79 20 74 68 65 20 63 75 73 74 6f 6d 20 d by the custom
3fef0 70 61 67 65 20 63 61 63 68 65 20 0a 2a 2a 20 69 page cache .** i
3ff00 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 0a mplementation. .
3ff10 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 68 75 74 64 **.** The xShutd
3ff20 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20 69 73 20 own() method is
3ff30 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 called from with
3ff40 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 in [sqlite3_shut
3ff50 64 6f 77 6e 28 29 5d 2c 20 0a 2a 2a 20 69 66 20 down()], .** if
3ff60 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
3ff70 69 6e 76 6f 6b 65 73 20 74 68 69 73 20 41 50 49 invokes this API
3ff80 2e 20 49 74 20 63 61 6e 20 62 65 20 75 73 65 64 . It can be used
3ff90 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 0a 2a 2a to clean up .**
3ffa0 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 any outstanding
3ffb0 20 72 65 73 6f 75 72 63 65 73 20 62 65 66 6f 72 resources befor
3ffc0 65 20 70 72 6f 63 65 73 73 20 73 68 75 74 64 6f e process shutdo
3ffd0 77 6e 2c 20 69 66 20 72 65 71 75 69 72 65 64 2e wn, if required.
3ffe0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 68 6f .**.** SQLite ho
3fff0 6c 64 73 20 61 20 5b 53 51 4c 49 54 45 5f 4d 55 lds a [SQLITE_MU
40000 54 45 58 5f 52 45 43 55 52 53 49 56 45 5d 20 6d TEX_RECURSIVE] m
40010 75 74 65 78 20 77 68 65 6e 20 69 74 20 69 6e 76 utex when it inv
40020 6f 6b 65 73 0a 2a 2a 20 74 68 65 20 78 49 6e 69 okes.** the xIni
40030 74 20 6d 65 74 68 6f 64 2c 20 73 6f 20 74 68 65 t method, so the
40040 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 6e 65 xInit method ne
40050 65 64 20 6e 6f 74 20 62 65 20 74 68 72 65 61 64 ed not be thread
40060 73 61 66 65 2e 20 20 54 68 65 0a 2a 2a 20 78 53 safe. The.** xS
40070 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 20 69 hutdown method i
40080 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 s only called fr
40090 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 om [sqlite3_shut
400a0 64 6f 77 6e 28 29 5d 20 73 6f 20 69 74 20 64 6f down()] so it do
400b0 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 es.** not need t
400c0 6f 20 62 65 20 74 68 72 65 61 64 73 61 66 65 20 o be threadsafe
400d0 65 69 74 68 65 72 2e 20 20 41 6c 6c 20 6f 74 68 either. All oth
400e0 65 72 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 20 er methods must
400f0 62 65 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a be threadsafe.**
40100 20 69 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 65 in multithreade
40110 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 0a d applications..
40120 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c **.** SQLite wil
40130 6c 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 78 l never invoke x
40140 49 6e 69 74 28 29 20 6d 6f 72 65 20 74 68 61 6e Init() more than
40150 20 6f 6e 63 65 20 77 69 74 68 6f 75 74 20 61 6e once without an
40160 20 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a 2a 20 intervening.**
40170 63 61 6c 6c 20 74 6f 20 78 53 68 75 74 64 6f 77 call to xShutdow
40180 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 n()..**.** The x
40190 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f 64 20 Create() method
401a0 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 73 74 is used to const
401b0 72 75 63 74 20 61 20 6e 65 77 20 63 61 63 68 65 ruct a new cache
401c0 20 69 6e 73 74 61 6e 63 65 2e 20 20 53 51 4c 69 instance. SQLi
401d0 74 65 0a 2a 2a 20 77 69 6c 6c 20 74 79 70 69 63 te.** will typic
401e0 61 6c 6c 79 20 63 72 65 61 74 65 20 6f 6e 65 20 ally create one
401f0 63 61 63 68 65 20 69 6e 73 74 61 6e 63 65 20 66 cache instance f
40200 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 64 61 74 or each open dat
40210 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 74 abase file,.** t
40220 68 6f 75 67 68 20 74 68 69 73 20 69 73 20 6e 6f hough this is no
40230 74 20 67 75 61 72 61 6e 74 65 65 64 2e 20 54 68 t guaranteed. Th
40240 65 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 61 6d e.** first param
40250 65 74 65 72 2c 20 73 7a 50 61 67 65 2c 20 69 73 eter, szPage, is
40260 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 the size in byt
40270 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 es of the pages
40280 74 68 61 74 20 6d 75 73 74 0a 2a 2a 20 62 65 20 that must.** be
40290 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 allocated by the
402a0 20 63 61 63 68 65 2e 20 20 73 7a 50 61 67 65 20 cache. szPage
402b0 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 20 70 6f will not be a po
402c0 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 73 7a 50 wer of two. szP
402d0 61 67 65 0a 2a 2a 20 77 69 6c 6c 20 74 68 65 20 age.** will the
402e0 70 61 67 65 20 73 69 7a 65 20 6f 66 20 74 68 65 page size of the
402f0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
40300 68 61 74 20 69 73 20 74 6f 20 62 65 20 63 61 63 hat is to be cac
40310 68 65 64 20 70 6c 75 73 20 61 6e 0a 2a 2a 20 69 hed plus an.** i
40320 6e 63 72 65 6d 65 6e 74 20 28 68 65 72 65 20 63 ncrement (here c
40330 61 6c 6c 65 64 20 22 52 22 29 20 6f 66 20 61 62 alled "R") of ab
40340 6f 75 74 20 31 30 30 20 6f 72 20 32 30 30 2e 20 out 100 or 200.
40350 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 SQLite will use
40360 20 74 68 65 0a 2a 2a 20 65 78 74 72 61 20 52 20 the.** extra R
40370 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61 bytes on each pa
40380 67 65 20 74 6f 20 73 74 6f 72 65 20 6d 65 74 61 ge to store meta
40390 64 61 74 61 20 61 62 6f 75 74 20 74 68 65 20 75 data about the u
403a0 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 64 61 74 nderlying.** dat
403b0 61 62 61 73 65 20 70 61 67 65 20 6f 6e 20 64 69 abase page on di
403c0 73 6b 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f sk. The value o
403d0 66 20 52 20 64 65 70 65 6e 64 73 0a 2a 2a 20 6f f R depends.** o
403e0 6e 20 74 68 65 20 53 51 4c 69 74 65 20 76 65 72 n the SQLite ver
403f0 73 69 6f 6e 2c 20 74 68 65 20 74 61 72 67 65 74 sion, the target
40400 20 70 6c 61 74 66 6f 72 6d 2c 20 61 6e 64 20 68 platform, and h
40410 6f 77 20 53 51 4c 69 74 65 20 77 61 73 20 63 6f ow SQLite was co
40420 6d 70 69 6c 65 64 2e 0a 2a 2a 20 52 20 69 73 20 mpiled..** R is
40430 63 6f 6e 73 74 61 6e 74 20 66 6f 72 20 61 20 70 constant for a p
40440 61 72 74 69 63 75 6c 61 72 20 62 75 69 6c 64 20 articular build
40450 6f 66 20 53 51 4c 69 74 65 2e 20 20 54 68 65 20 of SQLite. The
40460 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
40470 74 6f 0a 2a 2a 20 78 43 72 65 61 74 65 28 29 2c to.** xCreate(),
40480 20 62 50 75 72 67 65 61 62 6c 65 2c 20 69 73 20 bPurgeable, is
40490 74 72 75 65 20 69 66 20 74 68 65 20 63 61 63 68 true if the cach
404a0 65 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 20 e being created
404b0 77 69 6c 6c 0a 2a 2a 20 62 65 20 75 73 65 64 20 will.** be used
404c0 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 61 73 to cache databas
404d0 65 20 70 61 67 65 73 20 6f 66 20 61 20 66 69 6c e pages of a fil
404e0 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b e stored on disk
404f0 2c 20 6f 72 0a 2a 2a 20 66 61 6c 73 65 20 69 66 , or.** false if
40500 20 69 74 20 69 73 20 75 73 65 64 20 66 6f 72 20 it is used for
40510 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 an in-memory dat
40520 61 62 61 73 65 2e 20 54 68 65 20 63 61 63 68 65 abase. The cache
40530 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a implementation.
40540 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 ** does not have
40550 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 to do anything
40560 73 70 65 63 69 61 6c 20 62 61 73 65 64 20 77 69 special based wi
40570 74 68 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 th the value of
40580 62 50 75 72 67 65 61 62 6c 65 3b 0a 2a 2a 20 69 bPurgeable;.** i
40590 74 20 69 73 20 70 75 72 65 6c 79 20 61 64 76 69 t is purely advi
405a0 73 6f 72 79 2e 20 20 4f 6e 20 61 20 63 61 63 68 sory. On a cach
405b0 65 20 77 68 65 72 65 20 62 50 75 72 67 65 61 62 e where bPurgeab
405c0 6c 65 20 69 73 20 66 61 6c 73 65 2c 20 53 51 4c le is false, SQL
405d0 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 ite will.** neve
405e0 72 20 69 6e 76 6f 6b 65 20 78 55 6e 70 69 6e 28 r invoke xUnpin(
405f0 29 20 65 78 63 65 70 74 20 74 6f 20 64 65 6c 69 ) except to deli
40600 62 65 72 61 74 65 6c 79 20 64 65 6c 65 74 65 20 berately delete
40610 61 20 70 61 67 65 2e 0a 2a 2a 20 49 6e 20 6f 74 a page..** In ot
40620 68 65 72 20 77 6f 72 64 73 2c 20 61 20 63 61 63 her words, a cac
40630 68 65 20 63 72 65 61 74 65 64 20 77 69 74 68 20 he created with
40640 62 50 75 72 67 65 61 62 6c 65 20 73 65 74 20 74 bPurgeable set t
40650 6f 20 66 61 6c 73 65 20 77 69 6c 6c 0a 2a 2a 20 o false will.**
40660 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 61 6e never contain an
40670 79 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 y unpinned pages
40680 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 61 63 ..**.** The xCac
40690 68 65 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 hesize() method
406a0 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 61 74 may be called at
406b0 20 61 6e 79 20 74 69 6d 65 20 62 79 20 53 51 4c any time by SQL
406c0 69 74 65 20 74 6f 20 73 65 74 20 74 68 65 0a 2a ite to set the.*
406d0 2a 20 73 75 67 67 65 73 74 65 64 20 6d 61 78 69 * suggested maxi
406e0 6d 75 6d 20 63 61 63 68 65 2d 73 69 7a 65 20 28 mum cache-size (
406f0 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
40700 73 74 6f 72 65 64 20 62 79 29 20 74 68 65 20 63 stored by) the c
40710 61 63 68 65 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 ache.** instance
40720 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 passed as the f
40730 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 irst argument. T
40740 68 69 73 20 69 73 20 74 68 65 20 76 61 6c 75 65 his is the value
40750 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e configured usin
40760 67 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 g.** the SQLite
40770 22 5b 50 52 41 47 4d 41 20 63 61 63 68 65 5f 73 "[PRAGMA cache_s
40780 69 7a 65 5d 22 20 63 6f 6d 6d 61 6e 64 2e 20 41 ize]" command. A
40790 73 20 77 69 74 68 20 74 68 65 20 62 50 75 72 67 s with the bPurg
407a0 65 61 62 6c 65 20 70 61 72 61 6d 65 74 65 72 2c eable parameter,
407b0 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e .** the implemen
407c0 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 65 tation is not re
407d0 71 75 69 72 65 64 20 74 6f 20 64 6f 20 61 6e 79 quired to do any
407e0 74 68 69 6e 67 20 77 69 74 68 20 74 68 69 73 0a thing with this.
407f0 2a 2a 20 76 61 6c 75 65 3b 20 69 74 20 69 73 20 ** value; it is
40800 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a advisory only..*
40810 2a 0a 2a 2a 20 54 68 65 20 78 50 61 67 65 63 6f *.** The xPageco
40820 75 6e 74 28 29 20 6d 65 74 68 6f 64 20 73 68 6f unt() method sho
40830 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 6e uld return the n
40840 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 umber of pages c
40850 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 73 74 6f 72 urrently.** stor
40860 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e ed in the cache.
40870 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 78 46 65 74 .** .** The xFet
40880 63 68 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 ch() method is u
40890 73 65 64 20 74 6f 20 66 65 74 63 68 20 61 20 70 sed to fetch a p
408a0 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 age and return a
408b0 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 pointer to it.
408c0 0a 2a 2a 20 41 20 27 70 61 67 65 27 2c 20 69 6e .** A 'page', in
408d0 20 74 68 69 73 20 63 6f 6e 74 65 78 74 2c 20 69 this context, i
408e0 73 20 61 20 62 75 66 66 65 72 20 6f 66 20 73 7a s a buffer of sz
408f0 50 61 67 65 20 62 79 74 65 73 20 61 6c 69 67 6e Page bytes align
40900 65 64 20 61 74 20 61 6e 0a 2a 2a 20 38 2d 62 79 ed at an.** 8-by
40910 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 54 68 65 te boundary. The
40920 20 70 61 67 65 20 74 6f 20 62 65 20 66 65 74 63 page to be fetc
40930 68 65 64 20 69 73 20 64 65 74 65 72 6d 69 6e 65 hed is determine
40940 64 20 62 79 20 74 68 65 20 6b 65 79 2e 20 54 68 d by the key. Th
40950 65 0a 2a 2a 20 6d 69 6d 69 6d 75 6d 20 6b 65 79 e.** mimimum key
40960 20 76 61 6c 75 65 20 69 73 20 31 2e 20 41 66 74 value is 1. Aft
40970 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 72 er it has been r
40980 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 78 etrieved using x
40990 46 65 74 63 68 2c 20 74 68 65 20 70 61 67 65 20 Fetch, the page
409a0 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 .** is considere
409b0 64 20 74 6f 20 62 65 20 22 70 69 6e 6e 65 64 22 d to be "pinned"
409c0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 ..**.** If the r
409d0 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 equested page is
409e0 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
409f0 70 61 67 65 20 63 61 63 68 65 2c 20 74 68 65 6e page cache, then
40a00 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 0a the page cache.
40a10 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
40a20 6e 20 6d 75 73 74 20 72 65 74 75 72 6e 20 61 20 n must return a
40a30 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 pointer to the p
40a40 61 67 65 20 62 75 66 66 65 72 20 77 69 74 68 20 age buffer with
40a50 69 74 73 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 its content.** i
40a60 6e 74 61 63 74 2e 20 20 49 66 20 74 68 65 20 72 ntact. If the r
40a70 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 equested page is
40a80 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 not already in
40a90 74 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e 20 the cache, then
40aa0 74 68 65 0a 2a 2a 20 62 65 68 61 76 69 6f 72 20 the.** behavior
40ab0 6f 66 20 74 68 65 20 63 61 63 68 65 20 69 6d 70 of the cache imp
40ac0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 64 lementation is d
40ad0 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 etermined by the
40ae0 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a value of the.**
40af0 20 63 72 65 61 74 65 46 6c 61 67 20 70 61 72 61 createFlag para
40b00 6d 65 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 meter passed to
40b10 78 46 65 74 63 68 2c 20 61 63 63 6f 72 64 69 6e xFetch, accordin
40b20 67 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 g to the followi
40b30 6e 67 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 ng table:.**.**
40b40 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 31 20 <table border=1
40b50 77 69 64 74 68 3d 38 35 25 20 61 6c 69 67 6e 3d width=85% align=
40b60 63 65 6e 74 65 72 3e 0a 2a 2a 20 3c 74 72 3e 3c center>.** <tr><
40b70 74 68 3e 20 63 72 65 61 74 65 46 6c 61 67 20 3c th> createFlag <
40b80 74 68 3e 20 42 65 68 61 76 69 6f 75 72 20 77 68 th> Behaviour wh
40b90 65 6e 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 en page is not a
40ba0 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 65 0a lready in cache.
40bb0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 30 20 3c 74 ** <tr><td> 0 <t
40bc0 64 3e 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 d> Do not alloca
40bd0 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 20 te a new page.
40be0 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 Return NULL..**
40bf0 3c 74 72 3e 3c 74 64 3e 20 31 20 3c 74 64 3e 20 <tr><td> 1 <td>
40c00 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 Allocate a new p
40c10 61 67 65 20 69 66 20 69 74 20 65 61 73 79 20 61 age if it easy a
40c20 6e 64 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f nd convenient to
40c30 20 64 6f 20 73 6f 2e 0a 2a 2a 20 20 20 20 20 20 do so..**
40c40 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 Other
40c50 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c wise return NULL
40c60 2e 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 32 20 ..** <tr><td> 2
40c70 3c 74 64 3e 20 4d 61 6b 65 20 65 76 65 72 79 20 <td> Make every
40c80 65 66 66 6f 72 74 20 74 6f 20 61 6c 6c 6f 63 61 effort to alloca
40c90 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 20 te a new page.
40ca0 4f 6e 6c 79 20 72 65 74 75 72 6e 0a 2a 2a 20 20 Only return.**
40cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e N
40cc0 55 4c 4c 20 69 66 20 61 6c 6c 6f 63 61 74 69 6e ULL if allocatin
40cd0 67 20 61 20 6e 65 77 20 70 61 67 65 20 69 73 20 g a new page is
40ce0 65 66 66 65 63 74 69 76 65 6c 79 20 69 6d 70 6f effectively impo
40cf0 73 73 69 62 6c 65 2e 0a 2a 2a 20 3c 2f 74 61 62 ssible..** </tab
40d00 6c 65 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 le>.**.** SQLite
40d10 20 77 69 6c 6c 20 6e 6f 72 6d 61 6c 6c 79 20 69 will normally i
40d20 6e 76 6f 6b 65 20 78 46 65 74 63 68 28 29 20 77 nvoke xFetch() w
40d30 69 74 68 20 61 20 63 72 65 61 74 65 46 6c 61 67 ith a createFlag
40d40 20 6f 66 20 30 20 6f 72 20 31 2e 20 20 49 66 0a of 0 or 1. If.
40d50 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 78 46 65 ** a call to xFe
40d60 74 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74 tch() with creat
40d70 65 46 6c 61 67 3d 3d 31 20 72 65 74 75 72 6e 73 eFlag==1 returns
40d80 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 51 4c 69 NULL, then SQLi
40d90 74 65 20 77 69 6c 6c 0a 2a 2a 20 61 74 74 65 6d te will.** attem
40da0 70 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e 65 20 pt to unpin one
40db0 6f 72 20 6d 6f 72 65 20 63 61 63 68 65 20 70 61 or more cache pa
40dc0 67 65 73 20 62 79 20 73 70 69 6c 6c 69 6e 67 20 ges by spilling
40dd0 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a the content of.*
40de0 2a 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74 * pinned pages t
40df0 6f 20 64 69 73 6b 20 61 6e 64 20 73 79 6e 63 68 o disk and synch
40e00 69 6e 67 20 74 68 65 20 6f 70 65 72 61 74 69 6e ing the operatin
40e10 67 20 73 79 73 74 65 6d 20 64 69 73 6b 20 63 61 g system disk ca
40e20 63 68 65 2e 20 41 66 74 65 72 0a 2a 2a 20 61 74 che. After.** at
40e30 74 65 6d 70 74 69 6e 67 20 74 6f 20 75 6e 70 69 tempting to unpi
40e40 6e 20 70 61 67 65 73 2c 20 74 68 65 20 78 46 65 n pages, the xFe
40e50 74 63 68 28 29 20 6d 65 74 68 6f 64 20 77 69 6c tch() method wil
40e60 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 67 61 l be invoked aga
40e70 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 63 72 65 in with.** a cre
40e80 61 74 65 46 6c 61 67 20 6f 66 20 32 2e 0a 2a 2a ateFlag of 2..**
40e90 0a 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69 73 20 .** xUnpin() is
40ea0 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65 called by SQLite
40eb0 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 with a pointer
40ec0 74 6f 20 61 20 63 75 72 72 65 6e 74 6c 79 20 70 to a currently p
40ed0 69 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20 61 73 inned page.** as
40ee0 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 its second argu
40ef0 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 74 68 69 ment. If the thi
40f00 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20 64 69 rd parameter, di
40f10 73 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d 7a 65 scard, is non-ze
40f20 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 ro,.** then the
40f30 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 65 page should be e
40f40 76 69 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 victed from the
40f50 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20 63 cache. In this c
40f60 61 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a 20 61 ase SQLite .** a
40f70 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 ssumes that the
40f80 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 70 61 next time the pa
40f90 67 65 20 69 73 20 72 65 74 72 69 65 76 65 64 20 ge is retrieved
40fa0 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 75 from the cache u
40fb0 73 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46 65 74 sing.** the xFet
40fc0 63 68 28 29 20 6d 65 74 68 6f 64 2c 20 69 74 20 ch() method, it
40fd0 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e 20 will be zeroed.
40fe0 49 66 20 74 68 65 20 64 69 73 63 61 72 64 20 70 If the discard p
40ff0 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 7a arameter is.** z
41000 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 61 ero, then the pa
41010 67 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 ge is considered
41020 20 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65 64 2e to be unpinned.
41030 20 54 68 65 20 63 61 63 68 65 20 69 6d 70 6c 65 The cache imple
41040 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d 61 79 mentation.** may
41050 20 63 68 6f 6f 73 65 20 74 6f 20 65 76 69 63 74 choose to evict
41060 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 unpinned pages
41070 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a 2a 0a at any time..**.
41080 2a 2a 20 54 68 65 20 63 61 63 68 65 20 69 73 20 ** The cache is
41090 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 not required to
410a0 70 65 72 66 6f 72 6d 20 61 6e 79 20 72 65 66 65 perform any refe
410b0 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 2e 20 rence counting.
410c0 41 20 73 69 6e 67 6c 65 20 0a 2a 2a 20 63 61 6c A single .** cal
410d0 6c 20 74 6f 20 78 55 6e 70 69 6e 28 29 20 75 6e l to xUnpin() un
410e0 70 69 6e 73 20 74 68 65 20 70 61 67 65 20 72 65 pins the page re
410f0 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 gardless of the
41100 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 6f 72 20 number of prior
41110 63 61 6c 6c 73 20 0a 2a 2a 20 74 6f 20 78 46 65 calls .** to xFe
41120 74 63 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tch()..**.** The
41130 20 78 52 65 6b 65 79 28 29 20 6d 65 74 68 6f 64 xRekey() method
41140 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e is used to chan
41150 67 65 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 ge the key value
41160 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
41170 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 70 61 73 the.** page pas
41180 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e sed as the secon
41190 64 20 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d 20 d argument from
411a0 6f 6c 64 4b 65 79 20 74 6f 20 6e 65 77 4b 65 79 oldKey to newKey
411b0 2e 20 49 66 20 74 68 65 20 63 61 63 68 65 0a 2a . If the cache.*
411c0 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e * previously con
411d0 74 61 69 6e 73 20 61 6e 20 65 6e 74 72 79 20 61 tains an entry a
411e0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e ssociated with n
411f0 65 77 4b 65 79 2c 20 69 74 20 73 68 6f 75 6c 64 ewKey, it should
41200 20 62 65 0a 2a 2a 20 64 69 73 63 61 72 64 65 64 be.** discarded
41210 2e 20 41 6e 79 20 70 72 69 6f 72 20 63 61 63 68 . Any prior cach
41220 65 20 65 6e 74 72 79 20 61 73 73 6f 63 69 61 74 e entry associat
41230 65 64 20 77 69 74 68 20 6e 65 77 4b 65 79 20 69 ed with newKey i
41240 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f 74 s guaranteed not
41250 0a 2a 2a 20 74 6f 20 62 65 20 70 69 6e 6e 65 64 .** to be pinned
41260 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c ..**.** When SQL
41270 69 74 65 20 63 61 6c 6c 73 20 74 68 65 20 78 54 ite calls the xT
41280 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f 64 runcate() method
41290 2c 20 74 68 65 20 63 61 63 68 65 20 6d 75 73 74 , the cache must
412a0 20 64 69 73 63 61 72 64 20 61 6c 6c 0a 2a 2a 20 discard all.**
412b0 65 78 69 73 74 69 6e 67 20 63 61 63 68 65 20 65 existing cache e
412c0 6e 74 72 69 65 73 20 77 69 74 68 20 70 61 67 65 ntries with page
412d0 20 6e 75 6d 62 65 72 73 20 28 6b 65 79 73 29 20 numbers (keys)
412e0 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 greater than or
412f0 65 71 75 61 6c 0a 2a 2a 20 74 6f 20 74 68 65 20 equal.** to the
41300 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 4c 69 value of the iLi
41310 6d 69 74 20 70 61 72 61 6d 65 74 65 72 20 70 61 mit parameter pa
41320 73 73 65 64 20 74 6f 20 78 54 72 75 6e 63 61 74 ssed to xTruncat
41330 65 28 29 2e 20 49 66 20 61 6e 79 0a 2a 2a 20 6f e(). If any.** o
41340 66 20 74 68 65 73 65 20 70 61 67 65 73 20 61 72 f these pages ar
41350 65 20 70 69 6e 6e 65 64 2c 20 74 68 65 79 20 61 e pinned, they a
41360 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 75 6e re implicitly un
41370 70 69 6e 6e 65 64 2c 20 6d 65 61 6e 69 6e 67 20 pinned, meaning
41380 74 68 61 74 0a 2a 2a 20 74 68 65 79 20 63 61 6e that.** they can
41390 20 62 65 20 73 61 66 65 6c 79 20 64 69 73 63 61 be safely disca
413a0 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rded..**.** The
413b0 78 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68 6f xDestroy() metho
413c0 64 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 6c d is used to del
413d0 65 74 65 20 61 20 63 61 63 68 65 20 61 6c 6c 6f ete a cache allo
413e0 63 61 74 65 64 20 62 79 20 78 43 72 65 61 74 65 cated by xCreate
413f0 28 29 2e 0a 2a 2a 20 41 6c 6c 20 72 65 73 6f 75 ()..** All resou
41400 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 rces associated
41410 77 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69 with the specifi
41420 65 64 20 63 61 63 68 65 20 73 68 6f 75 6c 64 20 ed cache should
41430 62 65 20 66 72 65 65 64 2e 20 41 66 74 65 72 0a be freed. After.
41440 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78 ** calling the x
41450 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68 6f 64 Destroy() method
41460 2c 20 53 51 4c 69 74 65 20 63 6f 6e 73 69 64 65 , SQLite conside
41470 72 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f rs the [sqlite3_
41480 70 63 61 63 68 65 2a 5d 0a 2a 2a 20 68 61 6e 64 pcache*].** hand
41490 6c 65 20 69 6e 76 61 6c 69 64 2c 20 61 6e 64 20 le invalid, and
414a0 77 69 6c 6c 20 6e 6f 74 20 75 73 65 20 69 74 20 will not use it
414b0 77 69 74 68 20 61 6e 79 20 6f 74 68 65 72 20 73 with any other s
414c0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 qlite3_pcache_me
414d0 74 68 6f 64 73 0a 2a 2a 20 66 75 6e 63 74 69 6f thods.** functio
414e0 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ns..*/.typedef s
414f0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63 truct sqlite3_pc
41500 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 71 6c ache_methods sql
41510 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 ite3_pcache_meth
41520 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 ods;.struct sqli
41530 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
41540 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 70 41 72 ds {. void *pAr
41550 67 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 g;. int (*xInit
41560 29 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 )(void*);. void
41570 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 76 6f (*xShutdown)(vo
41580 69 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f id*);. sqlite3_
41590 70 63 61 63 68 65 20 2a 28 2a 78 43 72 65 61 74 pcache *(*xCreat
415a0 65 29 28 69 6e 74 20 73 7a 50 61 67 65 2c 20 69 e)(int szPage, i
415b0 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 3b 0a nt bPurgeable);.
415c0 20 20 76 6f 69 64 20 28 2a 78 43 61 63 68 65 73 void (*xCaches
415d0 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 70 63 61 ize)(sqlite3_pca
415e0 63 68 65 2a 2c 20 69 6e 74 20 6e 43 61 63 68 65 che*, int nCache
415f0 73 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 size);. int (*x
41600 50 61 67 65 63 6f 75 6e 74 29 28 73 71 6c 69 74 Pagecount)(sqlit
41610 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 20 20 76 e3_pcache*);. v
41620 6f 69 64 20 2a 28 2a 78 46 65 74 63 68 29 28 73 oid *(*xFetch)(s
41630 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 qlite3_pcache*,
41640 75 6e 73 69 67 6e 65 64 20 6b 65 79 2c 20 69 6e unsigned key, in
41650 74 20 63 72 65 61 74 65 46 6c 61 67 29 3b 0a 20 t createFlag);.
41660 20 76 6f 69 64 20 28 2a 78 55 6e 70 69 6e 29 28 void (*xUnpin)(
41670 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c sqlite3_pcache*,
41680 20 76 6f 69 64 2a 2c 20 69 6e 74 20 64 69 73 63 void*, int disc
41690 61 72 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 ard);. void (*x
416a0 52 65 6b 65 79 29 28 73 71 6c 69 74 65 33 5f 70 Rekey)(sqlite3_p
416b0 63 61 63 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 75 cache*, void*, u
416c0 6e 73 69 67 6e 65 64 20 6f 6c 64 4b 65 79 2c 20 nsigned oldKey,
416d0 75 6e 73 69 67 6e 65 64 20 6e 65 77 4b 65 79 29 unsigned newKey)
416e0 3b 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 75 6e ;. void (*xTrun
416f0 63 61 74 65 29 28 73 71 6c 69 74 65 33 5f 70 63 cate)(sqlite3_pc
41700 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65 64 20 ache*, unsigned
41710 69 4c 69 6d 69 74 29 3b 0a 20 20 76 6f 69 64 20 iLimit);. void
41720 28 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 (*xDestroy)(sqli
41730 74 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 7d 3b te3_pcache*);.};
41740 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
41750 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 : Online Backup
41760 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49 Object.** EXPERI
41770 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 MENTAL.**.** The
41780 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 sqlite3_backup
41790 6f 62 6a 65 63 74 20 72 65 63 6f 72 64 73 20 73 object records s
417a0 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e tate information
417b0 20 61 62 6f 75 74 20 61 6e 20 6f 6e 67 6f 69 6e about an ongoin
417c0 67 0a 2a 2a 20 6f 6e 6c 69 6e 65 20 62 61 63 6b g.** online back
417d0 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 54 up operation. T
417e0 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 he sqlite3_backu
417f0 70 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 p object is crea
41800 74 65 64 20 62 79 0a 2a 2a 20 61 20 63 61 6c 6c ted by.** a call
41810 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 to [sqlite3_bac
41820 6b 75 70 5f 69 6e 69 74 28 29 5d 20 61 6e 64 20 kup_init()] and
41830 69 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 is destroyed by
41840 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 a call to.** [sq
41850 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
41860 69 73 68 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 ish()]..**.** Se
41870 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 e Also: [Using t
41880 68 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 he SQLite Online
41890 20 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2f 0a Backup API].*/.
418a0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 typedef struct s
418b0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 73 71 qlite3_backup sq
418c0 6c 69 74 65 33 5f 62 61 63 6b 75 70 3b 0a 0a 2f lite3_backup;../
418d0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f *.** CAPI3REF: O
418e0 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50 49 nline Backup API
418f0 2e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 ..** EXPERIMENTA
41900 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 L.**.** This API
41910 20 69 73 20 75 73 65 64 20 74 6f 20 6f 76 65 72 is used to over
41920 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e write the conten
41930 74 73 20 6f 66 20 6f 6e 65 20 64 61 74 61 62 61 ts of one databa
41940 73 65 20 77 69 74 68 20 74 68 61 74 0a 2a 2a 20 se with that.**
41950 6f 66 20 61 6e 6f 74 68 65 72 2e 20 49 74 20 69 of another. It i
41960 73 20 75 73 65 66 75 6c 20 65 69 74 68 65 72 20 s useful either
41970 66 6f 72 20 63 72 65 61 74 69 6e 67 20 62 61 63 for creating bac
41980 6b 75 70 73 20 6f 66 20 64 61 74 61 62 61 73 65 kups of database
41990 73 20 6f 72 0a 2a 2a 20 66 6f 72 20 63 6f 70 79 s or.** for copy
419a0 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 ing in-memory da
419b0 74 61 62 61 73 65 73 20 74 6f 20 6f 72 20 66 72 tabases to or fr
419c0 6f 6d 20 70 65 72 73 69 73 74 65 6e 74 20 66 69 om persistent fi
419d0 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 les. .**.** See
419e0 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 Also: [Using the
419f0 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20 42 SQLite Online B
41a00 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2a 0a 2a 2a ackup API].**.**
41a10 20 45 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 Exclusive acces
41a20 73 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f s is required to
41a30 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
41a40 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 74 68 database for th
41a50 65 20 0a 2a 2a 20 64 75 72 61 74 69 6f 6e 20 6f e .** duration o
41a60 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 2e f the operation.
41a70 20 48 6f 77 65 76 65 72 20 74 68 65 20 73 6f 75 However the sou
41a80 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 rce database is
41a90 6f 6e 6c 79 0a 2a 2a 20 72 65 61 64 2d 6c 6f 63 only.** read-loc
41aa0 6b 65 64 20 77 68 69 6c 65 20 69 74 20 69 73 20 ked while it is
41ab0 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 20 72 actually being r
41ac0 65 61 64 2c 20 69 74 20 69 73 20 6e 6f 74 20 6c ead, it is not l
41ad0 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e 74 69 6e 75 ocked.** continu
41ae0 6f 75 73 6c 79 20 66 6f 72 20 74 68 65 20 65 6e ously for the en
41af0 74 69 72 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 tire operation.
41b00 54 68 75 73 2c 20 74 68 65 20 62 61 63 6b 75 70 Thus, the backup
41b10 20 6d 61 79 20 62 65 0a 2a 2a 20 70 65 72 66 6f may be.** perfo
41b20 72 6d 65 64 20 6f 6e 20 61 20 6c 69 76 65 20 64 rmed on a live d
41b30 61 74 61 62 61 73 65 20 77 69 74 68 6f 75 74 20 atabase without
41b40 70 72 65 76 65 6e 74 69 6e 67 20 6f 74 68 65 72 preventing other
41b50 20 75 73 65 72 73 20 66 72 6f 6d 0a 2a 2a 20 77 users from.** w
41b60 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 riting to the da
41b70 74 61 62 61 73 65 20 66 6f 72 20 61 6e 20 65 78 tabase for an ex
41b80 74 65 6e 64 65 64 20 70 65 72 69 6f 64 20 6f 66 tended period of
41b90 20 74 69 6d 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 6f time..** .** To
41ba0 20 70 65 72 66 6f 72 6d 20 61 20 62 61 63 6b 75 perform a backu
41bb0 70 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 2a 2a p operation: .**
41bc0 20 20 20 3c 6f 6c 3e 0a 2a 2a 20 20 20 20 20 3c <ol>.** <
41bd0 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 li><b>sqlite3_ba
41be0 63 6b 75 70 5f 69 6e 69 74 28 29 3c 2f 62 3e 20 ckup_init()</b>
41bf0 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 74 is called once t
41c00 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 o initialize the
41c10 0a 2a 2a 20 20 20 20 20 20 20 20 20 62 61 63 6b .** back
41c20 75 70 2c 20 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e up, .** <li>
41c30 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 <b>sqlite3_backu
41c40 70 5f 73 74 65 70 28 29 3c 2f 62 3e 20 69 73 20 p_step()</b> is
41c50 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f called one or mo
41c60 72 65 20 74 69 6d 65 73 20 74 6f 20 74 72 61 6e re times to tran
41c70 73 66 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 sfer .**
41c80 20 74 68 65 20 64 61 74 61 20 62 65 74 77 65 65 the data betwee
41c90 6e 20 74 68 65 20 74 77 6f 20 64 61 74 61 62 61 n the two databa
41ca0 73 65 73 2c 20 61 6e 64 20 66 69 6e 61 6c 6c 79 ses, and finally
41cb0 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 .** <li><b>s
41cc0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 qlite3_backup_fi
41cd0 6e 69 73 68 28 29 3c 2f 62 3e 20 69 73 20 63 61 nish()</b> is ca
41ce0 6c 6c 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 lled to release
41cf0 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 0a 2a all resources .*
41d00 2a 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69 * associ
41d10 61 74 65 64 20 77 69 74 68 20 74 68 65 20 62 61 ated with the ba
41d20 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 ckup operation.
41d30 0a 2a 2a 20 20 20 3c 2f 6f 6c 3e 0a 2a 2a 20 54 .** </ol>.** T
41d40 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 65 here should be e
41d50 78 61 63 74 6c 79 20 6f 6e 65 20 63 61 6c 6c 20 xactly one call
41d60 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
41d70 70 5f 66 69 6e 69 73 68 28 29 20 66 6f 72 20 65 p_finish() for e
41d80 61 63 68 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 ach.** successfu
41d90 6c 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 l call to sqlite
41da0 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2e 3_backup_init().
41db0 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 .**.** <b>sqlite
41dc0 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 3c 3_backup_init()<
41dd0 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 /b>.**.** The fi
41de0 72 73 74 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 rst two argument
41df0 73 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c s passed to [sql
41e00 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 ite3_backup_init
41e10 28 29 5d 20 61 72 65 20 74 68 65 20 64 61 74 61 ()] are the data
41e20 62 61 73 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 61 base.** handle a
41e30 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
41e40 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 he destination d
41e50 61 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 20 atabase and the
41e60 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 0a 2a database name .*
41e70 2a 20 75 73 65 64 20 74 6f 20 61 74 74 61 63 68 * used to attach
41e80 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
41e90 20 64 61 74 61 62 61 73 65 20 74 6f 20 74 68 65 database to the
41ea0 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 64 61 74 handle. The dat
41eb0 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 69 73 abase name.** is
41ec0 20 22 6d 61 69 6e 22 20 66 6f 72 20 74 68 65 20 "main" for the
41ed0 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 22 main database, "
41ee0 74 65 6d 70 22 20 66 6f 72 20 74 68 65 20 74 65 temp" for the te
41ef0 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 mporary database
41f00 2c 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 , or.** the name
41f10 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61 specified as pa
41f20 72 74 20 6f 66 20 74 68 65 20 5b 41 54 54 41 43 rt of the [ATTAC
41f30 48 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20 H] statement if
41f40 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
41f50 69 73 0a 2a 2a 20 61 6e 20 61 74 74 61 63 68 65 is.** an attache
41f60 64 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 d database. The
41f70 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 third and fourth
41f80 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65 arguments passe
41f90 64 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65 33 d to .** sqlite3
41fa0 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 20 69 _backup_init() i
41fb0 64 65 6e 74 69 66 79 20 74 68 65 20 5b 64 61 74 dentify the [dat
41fc0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
41fd0 5d 0a 2a 2a 20 61 6e 64 20 64 61 74 61 62 61 73 ].** and databas
41fe0 65 20 6e 61 6d 65 20 75 73 65 64 0a 2a 2a 20 74 e name used.** t
41ff0 6f 20 61 63 63 65 73 73 20 74 68 65 20 73 6f 75 o access the sou
42000 72 63 65 20 64 61 74 61 62 61 73 65 2e 20 54 68 rce database. Th
42010 65 20 76 61 6c 75 65 73 20 70 61 73 73 65 64 20 e values passed
42020 66 6f 72 20 74 68 65 20 73 6f 75 72 63 65 20 61 for the source a
42030 6e 64 20 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 nd .** destinati
42040 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e on [database con
42050 6e 65 63 74 69 6f 6e 5d 20 70 61 72 61 6d 65 74 nection] paramet
42060 65 72 73 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 ers must not be
42070 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 the same..**.**
42080 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
42090 72 73 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 rs within sqlite
420a0 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2c 3_backup_init(),
420b0 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20 72 65 then NULL is re
420c0 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 61 6e turned.** and an
420d0 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 error code and
420e0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72 error message wr
420f0 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 5b itten into the [
42100 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
42110 69 6f 6e 5d 20 0a 2a 2a 20 70 61 73 73 65 64 20 ion] .** passed
42120 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 as the first arg
42130 75 6d 65 6e 74 2e 20 54 68 65 79 20 6d 61 79 20 ument. They may
42140 62 65 20 72 65 74 72 69 65 76 65 64 20 75 73 69 be retrieved usi
42150 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 ng the.** [sqlit
42160 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 5b e3_errcode()], [
42170 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 sqlite3_errmsg()
42180 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f ], and [sqlite3_
42190 65 72 72 6d 73 67 31 36 28 29 5d 20 66 75 6e 63 errmsg16()] func
421a0 74 69 6f 6e 73 2e 0a 2a 2a 20 4f 74 68 65 72 77 tions..** Otherw
421b0 69 73 65 2c 20 69 66 20 73 75 63 63 65 73 73 66 ise, if successf
421c0 75 6c 2c 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ul, a pointer to
421d0 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 an [sqlite3_bac
421e0 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73 0a 2a kup] object is.*
421f0 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 * returned. This
42200 20 70 6f 69 6e 74 65 72 20 6d 61 79 20 62 65 20 pointer may be
42210 75 73 65 64 20 77 69 74 68 20 74 68 65 20 73 71 used with the sq
42220 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
42230 70 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 p() and.** sqlit
42240 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 e3_backup_finish
42250 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 () functions to
42260 70 65 72 66 6f 72 6d 20 74 68 65 20 73 70 65 63 perform the spec
42270 69 66 69 65 64 20 62 61 63 6b 75 70 20 0a 2a 2a ified backup .**
42280 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a operation..**.*
42290 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 * <b>sqlite3_bac
422a0 6b 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e 0a 2a kup_step()</b>.*
422b0 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 5b 73 *.** Function [s
422c0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
422d0 65 70 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f ep()] is used to
422e0 20 63 6f 70 79 20 75 70 20 74 6f 20 6e 50 61 67 copy up to nPag
422f0 65 20 70 61 67 65 73 20 62 65 74 77 65 65 6e 20 e pages between
42300 0a 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 20 61 .** the source a
42310 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 nd destination d
42320 61 74 61 62 61 73 65 73 2c 20 77 68 65 72 65 20 atabases, where
42330 6e 50 61 67 65 20 69 73 20 74 68 65 20 76 61 6c nPage is the val
42340 75 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 73 65 ue of the .** se
42350 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 70 cond parameter p
42360 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 assed to sqlite3
42370 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 _backup_step().
42380 49 66 20 6e 50 61 67 65 20 69 73 20 61 20 6e 65 If nPage is a ne
42390 67 61 74 69 76 65 0a 2a 2a 20 76 61 6c 75 65 2c gative.** value,
423a0 20 61 6c 6c 20 72 65 6d 61 69 6e 69 6e 67 20 73 all remaining s
423b0 6f 75 72 63 65 20 70 61 67 65 73 20 61 72 65 20 ource pages are
423c0 63 6f 70 69 65 64 2e 20 49 66 20 74 68 65 20 72 copied. If the r
423d0 65 71 75 69 72 65 64 20 70 61 67 65 73 20 61 72 equired pages ar
423e0 65 20 0a 2a 2a 20 73 75 63 63 65 73 66 75 6c 6c e .** succesfull
423f0 79 20 63 6f 70 69 65 64 2c 20 62 75 74 20 74 68 y copied, but th
42400 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 6f ere are still mo
42410 72 65 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 re pages to copy
42420 20 62 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a 20 before the .**
42430 62 61 63 6b 75 70 20 69 73 20 63 6f 6d 70 6c 65 backup is comple
42440 74 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 5b te, it returns [
42450 53 51 4c 49 54 45 5f 4f 4b 5d 2e 20 49 66 20 6e SQLITE_OK]. If n
42460 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 o error occured
42470 61 6e 64 20 74 68 65 72 65 20 0a 2a 2a 20 61 72 and there .** ar
42480 65 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 e no more pages
42490 74 6f 20 63 6f 70 79 2c 20 74 68 65 6e 20 5b 53 to copy, then [S
424a0 51 4c 49 54 45 5f 44 4f 4e 45 5d 20 69 73 20 72 QLITE_DONE] is r
424b0 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 eturned. If an e
424c0 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c rror .** occurs,
424d0 20 74 68 65 6e 20 61 6e 20 53 51 4c 69 74 65 20 then an SQLite
424e0 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
424f0 74 75 72 6e 65 64 2e 20 41 73 20 77 65 6c 6c 20 turned. As well
42500 61 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 61 as [SQLITE_OK] a
42510 6e 64 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f nd.** [SQLITE_DO
42520 4e 45 5d 2c 20 61 20 63 61 6c 6c 20 74 6f 20 73 NE], a call to s
42530 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
42540 65 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 ep() may return
42550 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 [SQLITE_READONLY
42560 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f ],.** [SQLITE_NO
42570 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55 MEM], [SQLITE_BU
42580 53 59 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 SY], [SQLITE_LOC
42590 4b 45 44 5d 2c 20 6f 72 20 61 6e 0a 2a 2a 20 5b KED], or an.** [
425a0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 SQLITE_IOERR_ACC
425b0 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45 ESS | SQLITE_IOE
425c0 52 52 5f 58 58 58 5d 20 65 78 74 65 6e 64 65 64 RR_XXX] extended
425d0 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a error code..**.
425e0 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73 20 74 68 ** As well as th
425f0 65 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 e case where the
42600 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
42610 61 62 61 73 65 20 66 69 6c 65 20 77 61 73 20 6f abase file was o
42620 70 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 72 65 61 pened for.** rea
42630 64 2d 6f 6e 6c 79 20 61 63 63 65 73 73 2c 20 73 d-only access, s
42640 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
42650 65 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 ep() may return
42660 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 [SQLITE_READONLY
42670 5d 20 69 66 0a 2a 2a 20 74 68 65 20 64 65 73 74 ] if.** the dest
42680 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e ination is an in
42690 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
426a0 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e with a differen
426b0 74 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20 66 t page size.** f
426c0 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 64 rom the source d
426d0 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 atabase..**.** I
426e0 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 f sqlite3_backup
426f0 5f 73 74 65 70 28 29 20 63 61 6e 6e 6f 74 20 6f _step() cannot o
42700 62 74 61 69 6e 20 61 20 72 65 71 75 69 72 65 64 btain a required
42710 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 6c 6f 63 file-system loc
42720 6b 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 5b k, then.** the [
42730 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e sqlite3_busy_han
42740 64 6c 65 72 20 7c 20 62 75 73 79 2d 68 61 6e 64 dler | busy-hand
42750 6c 65 72 20 66 75 6e 63 74 69 6f 6e 5d 0a 2a 2a ler function].**
42760 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 66 20 is invoked (if
42770 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64 one is specified
42780 29 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 62 75 ). If the .** bu
42790 73 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 75 72 sy-handler retur
427a0 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 62 65 66 6f ns non-zero befo
427b0 72 65 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 re the lock is a
427c0 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 0a vailable, then .
427d0 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d ** [SQLITE_BUSY]
427e0 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 is returned to
427f0 74 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e 20 74 the caller. In t
42800 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c his case the cal
42810 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f l to.** sqlite3_
42820 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 backup_step() ca
42830 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c 61 74 n be retried lat
42840 65 72 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 er. If the sourc
42850 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 e.** [database c
42860 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 69 73 onnection].** is
42870 20 62 65 69 6e 67 20 75 73 65 64 20 74 6f 20 77 being used to w
42880 72 69 74 65 20 74 6f 20 74 68 65 20 73 6f 75 72 rite to the sour
42890 63 65 20 64 61 74 61 62 61 73 65 20 77 68 65 6e ce database when
428a0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
428b0 73 74 65 70 28 29 0a 2a 2a 20 69 73 20 63 61 6c step().** is cal
428c0 6c 65 64 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 led, then [SQLIT
428d0 45 5f 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 74 E_LOCKED] is ret
428e0 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c urned immediatel
428f0 79 2e 20 41 67 61 69 6e 2c 20 69 6e 20 74 68 69 y. Again, in thi
42900 73 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 63 61 s.** case the ca
42910 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 ll to sqlite3_ba
42920 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6e 20 ckup_step() can
42930 62 65 20 72 65 74 72 69 65 64 20 6c 61 74 65 72 be retried later
42940 20 6f 6e 2e 20 49 66 0a 2a 2a 20 5b 53 51 4c 49 on. If.** [SQLI
42950 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 TE_IOERR_ACCESS
42960 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 | SQLITE_IOERR_X
42970 58 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d XX], [SQLITE_NOM
42980 45 4d 5d 2c 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 EM], or.** [SQLI
42990 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 20 69 73 20 TE_READONLY] is
429a0 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 0a returned, then .
429b0 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 ** there is no p
429c0 6f 69 6e 74 20 69 6e 20 72 65 74 72 79 69 6e 67 oint in retrying
429d0 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c the call to sql
429e0 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
429f0 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 65 72 (). These .** er
42a00 72 6f 72 73 20 61 72 65 20 63 6f 6e 73 69 64 65 rors are conside
42a10 72 65 64 20 66 61 74 61 6c 2e 20 41 74 20 74 68 red fatal. At th
42a20 69 73 20 70 6f 69 6e 74 20 74 68 65 20 61 70 70 is point the app
42a30 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 61 63 lication must ac
42a40 63 65 70 74 20 0a 2a 2a 20 74 68 61 74 20 74 68 cept .** that th
42a50 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 e backup operati
42a60 6f 6e 20 68 61 73 20 66 61 69 6c 65 64 20 61 6e on has failed an
42a70 64 20 70 61 73 73 20 74 68 65 20 62 61 63 6b 75 d pass the backu
42a80 70 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 6e 64 p operation hand
42a90 6c 65 20 0a 2a 2a 20 74 6f 20 74 68 65 20 73 71 le .** to the sq
42aa0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
42ab0 69 73 68 28 29 20 74 6f 20 72 65 6c 65 61 73 65 ish() to release
42ac0 20 61 73 73 6f 63 69 61 74 65 64 20 72 65 73 6f associated reso
42ad0 75 72 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 6c urces..**.** Fol
42ae0 6c 6f 77 69 6e 67 20 74 68 65 20 66 69 72 73 74 lowing the first
42af0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
42b00 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 20 _backup_step(),
42b10 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
42b20 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 k is.** obtained
42b30 20 6f 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 on the destinat
42b40 69 6f 6e 20 66 69 6c 65 2e 20 49 74 20 69 73 20 ion file. It is
42b50 6e 6f 74 20 72 65 6c 65 61 73 65 64 20 75 6e 74 not released unt
42b60 69 6c 20 65 69 74 68 65 72 20 0a 2a 2a 20 73 71 il either .** sq
42b70 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
42b80 69 73 68 28 29 20 69 73 20 63 61 6c 6c 65 64 20 ish() is called
42b90 6f 72 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 or the backup op
42ba0 65 72 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 6c eration is compl
42bb0 65 74 65 20 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 ete .** and sqli
42bc0 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 te3_backup_step(
42bd0 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 ) returns [SQLIT
42be0 45 5f 44 4f 4e 45 5d 2e 20 41 64 64 69 74 69 6f E_DONE]. Additio
42bf0 6e 61 6c 6c 79 2c 20 65 61 63 68 20 74 69 6d 65 nally, each time
42c00 20 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 73 .** a call to s
42c10 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
42c20 65 70 28 29 20 69 73 20 6d 61 64 65 20 61 20 5b ep() is made a [
42c30 73 68 61 72 65 64 20 6c 6f 63 6b 5d 20 69 73 20 shared lock] is
42c40 6f 62 74 61 69 6e 65 64 20 6f 6e 0a 2a 2a 20 74 obtained on.** t
42c50 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
42c60 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 6c 6f se file. This lo
42c70 63 6b 20 69 73 20 72 65 6c 65 61 73 65 64 20 62 ck is released b
42c80 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20 73 71 6c efore the.** sql
42c90 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
42ca0 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e () call returns.
42cb0 20 42 65 63 61 75 73 65 20 74 68 65 20 73 6f 75 Because the sou
42cc0 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 rce database is
42cd0 6e 6f 74 0a 2a 2a 20 6c 6f 63 6b 65 64 20 62 65 not.** locked be
42ce0 74 77 65 65 6e 20 63 61 6c 6c 73 20 74 6f 20 73 tween calls to s
42cf0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
42d00 65 70 28 29 2c 20 69 74 20 6d 61 79 20 62 65 20 ep(), it may be
42d10 6d 6f 64 69 66 69 65 64 20 6d 69 64 2d 77 61 79 modified mid-way
42d20 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 .** through the
42d30 62 61 63 6b 75 70 20 70 72 6f 63 65 64 75 72 65 backup procedure
42d40 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 . If the source
42d50 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69 database is modi
42d60 66 69 65 64 20 62 79 20 61 6e 0a 2a 2a 20 65 78 fied by an.** ex
42d70 74 65 72 6e 61 6c 20 70 72 6f 63 65 73 73 20 6f ternal process o
42d80 72 20 76 69 61 20 61 20 64 61 74 61 62 61 73 65 r via a database
42d90 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 74 68 65 connection othe
42da0 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 20 62 r than the one b
42db0 65 69 6e 67 0a 2a 2a 20 75 73 65 64 20 62 79 20 eing.** used by
42dc0 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 the backup opera
42dd0 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 62 tion, then the b
42de0 61 63 6b 75 70 20 77 69 6c 6c 20 62 65 20 74 72 ackup will be tr
42df0 61 6e 73 70 61 72 65 6e 74 6c 79 0a 2a 2a 20 72 ansparently.** r
42e00 65 73 74 61 72 74 65 64 20 62 79 20 74 68 65 20 estarted by the
42e10 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c next call to sql
42e20 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
42e30 28 29 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 (). If the sourc
42e40 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69 e .** database i
42e50 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 74 68 s modified by th
42e60 65 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 e using the same
42e70 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
42e80 74 69 6f 6e 20 61 73 20 69 73 20 75 73 65 64 0a tion as is used.
42e90 2a 2a 20 62 79 20 74 68 65 20 62 61 63 6b 75 70 ** by the backup
42ea0 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e operation, then
42eb0 20 74 68 65 20 62 61 63 6b 75 70 20 64 61 74 61 the backup data
42ec0 62 61 73 65 20 69 73 20 74 72 61 6e 73 70 61 72 base is transpar
42ed0 65 6e 74 6c 79 20 0a 2a 2a 20 75 70 64 61 74 65 ently .** update
42ee0 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 d at the same ti
42ef0 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c me..**.** <b>sql
42f00 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
42f10 73 68 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 4f sh()</b>.**.** O
42f20 6e 63 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b nce sqlite3_back
42f30 75 70 5f 73 74 65 70 28 29 20 68 61 73 20 72 65 up_step() has re
42f40 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 44 turned [SQLITE_D
42f50 4f 4e 45 5d 2c 20 6f 72 20 77 68 65 6e 20 74 68 ONE], or when th
42f60 65 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f e .** applicatio
42f70 6e 20 77 69 73 68 65 73 20 74 6f 20 61 62 61 6e n wishes to aban
42f80 64 6f 6e 20 74 68 65 20 62 61 63 6b 75 70 20 6f don the backup o
42f90 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 20 5b 73 peration, the [s
42fa0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 0a 2a qlite3_backup].*
42fb0 2a 20 6f 62 6a 65 63 74 20 73 68 6f 75 6c 64 20 * object should
42fc0 62 65 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c be passed to sql
42fd0 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
42fe0 73 68 28 29 2e 20 54 68 69 73 20 72 65 6c 65 61 sh(). This relea
42ff0 73 65 73 20 61 6c 6c 0a 2a 2a 20 72 65 73 6f 75 ses all.** resou
43000 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 rces associated
43010 77 69 74 68 20 74 68 65 20 62 61 63 6b 75 70 20 with the backup
43020 6f 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 73 71 operation. If sq
43030 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
43040 70 28 29 0a 2a 2a 20 68 61 73 20 6e 6f 74 20 79 p().** has not y
43050 65 74 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c et returned [SQL
43060 49 54 45 5f 44 4f 4e 45 5d 2c 20 74 68 65 6e 20 ITE_DONE], then
43070 61 6e 79 20 61 63 74 69 76 65 20 77 72 69 74 65 any active write
43080 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 -transaction on
43090 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 the.** destinati
430a0 6f 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 72 on database is r
430b0 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 65 20 olled back. The
430c0 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d [sqlite3_backup]
430d0 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 76 61 6c object is inval
430e0 69 64 0a 2a 2a 20 61 6e 64 20 6d 61 79 20 6e 6f id.** and may no
430f0 74 20 62 65 20 75 73 65 64 20 66 6f 6c 6c 6f 77 t be used follow
43100 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 ing a call to sq
43110 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
43120 69 73 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ish()..**.** The
43130 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
43140 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 by sqlite3_backu
43150 70 5f 66 69 6e 69 73 68 20 69 73 20 5b 53 51 4c p_finish is [SQL
43160 49 54 45 5f 4f 4b 5d 20 69 66 20 6e 6f 20 65 72 ITE_OK] if no er
43170 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2c ror.** occurred,
43180 20 72 65 67 61 72 64 6c 65 73 73 20 6f 72 20 77 regardless or w
43190 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 71 hether or not sq
431a0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
431b0 70 28 29 20 77 61 73 20 63 61 6c 6c 65 64 0a 2a p() was called.*
431c0 2a 20 61 20 73 75 66 66 69 63 69 65 6e 74 20 6e * a sufficient n
431d0 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 umber of times t
431e0 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 62 o complete the b
431f0 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e ackup operation.
43200 20 4f 72 2c 20 69 66 0a 2a 2a 20 61 6e 20 6f 75 Or, if.** an ou
43210 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 t-of-memory cond
43220 69 74 69 6f 6e 20 6f 72 20 49 4f 20 65 72 72 6f ition or IO erro
43230 72 20 6f 63 63 75 72 65 64 20 64 75 72 69 6e 67 r occured during
43240 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 a call to.** sq
43250 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
43260 70 28 29 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 p() then [SQLITE
43270 5f 4e 4f 4d 45 4d 5d 20 6f 72 20 61 6e 0a 2a 2a _NOMEM] or an.**
43280 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 [SQLITE_IOERR_A
43290 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 CCESS | SQLITE_I
432a0 4f 45 52 52 5f 58 58 58 5d 20 65 72 72 6f 72 20 OERR_XXX] error
432b0 63 6f 64 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 code.** is retur
432c0 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ned. In this cas
432d0 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 e the error code
432e0 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 and an error me
432f0 73 73 61 67 65 20 61 72 65 0a 2a 2a 20 77 72 69 ssage are.** wri
43300 74 74 65 6e 20 74 6f 20 74 68 65 20 64 65 73 74 tten to the dest
43310 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 73 ination [databas
43320 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a e connection]..*
43330 2a 0a 2a 2a 20 41 20 72 65 74 75 72 6e 20 6f 66 *.** A return of
43340 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f [SQLITE_BUSY] o
43350 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 r [SQLITE_LOCKED
43360 5d 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 62 ] from sqlite3_b
43370 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69 73 0a ackup_step() is.
43380 2a 2a 20 6e 6f 74 20 61 20 70 65 72 6d 61 6e 65 ** not a permane
43390 6e 74 20 65 72 72 6f 72 20 61 6e 64 20 64 6f 65 nt error and doe
433a0 73 20 6e 6f 74 20 61 66 66 65 63 74 20 74 68 65 s not affect the
433b0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 return value of
433c0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b .** sqlite3_back
433d0 75 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a up_finish()..**.
433e0 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 ** <b>sqlite3_ba
433f0 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 ckup_remaining()
43400 2c 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 , sqlite3_backup
43410 5f 70 61 67 65 63 6f 75 6e 74 28 29 3c 2f 62 3e _pagecount()</b>
43420 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c 6c .**.** Each call
43430 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b to sqlite3_back
43440 75 70 5f 73 74 65 70 28 29 20 73 65 74 73 20 74 up_step() sets t
43450 77 6f 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 wo values stored
43460 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62 internally.** b
43470 79 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 y an [sqlite3_ba
43480 63 6b 75 70 5d 20 6f 62 6a 65 63 74 2e 20 54 68 ckup] object. Th
43490 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
434a0 73 20 73 74 69 6c 6c 20 74 6f 20 62 65 20 62 61 s still to be ba
434b0 63 6b 65 64 0a 2a 2a 20 75 70 2c 20 77 68 69 63 cked.** up, whic
434c0 68 20 6d 61 79 20 62 65 20 71 75 65 72 69 65 64 h may be queried
434d0 20 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63 6b by sqlite3_back
434e0 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 2c 20 up_remaining(),
434f0 61 6e 64 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a and the total.**
43500 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
43510 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 in the source d
43520 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77 68 atabase file, wh
43530 69 63 68 20 6d 61 79 20 62 65 20 71 75 65 72 69 ich may be queri
43540 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 ed by.** sqlite3
43550 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e _backup_pagecoun
43560 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 t()..**.** The v
43570 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 62 alues returned b
43580 79 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e y these function
43590 73 20 61 72 65 20 6f 6e 6c 79 20 75 70 64 61 74 s are only updat
435a0 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 ed by.** sqlite3
435b0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 _backup_step().
435c0 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 If the source da
435d0 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69 66 69 tabase is modifi
435e0 65 64 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b ed during a back
435f0 75 70 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2c up.** operation,
43600 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 73 then the values
43610 20 61 72 65 20 6e 6f 74 20 75 70 64 61 74 65 64 are not updated
43620 20 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72 20 to account for
43630 61 6e 79 20 65 78 74 72 61 0a 2a 2a 20 70 61 67 any extra.** pag
43640 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 es that need to
43650 62 65 20 75 70 64 61 74 65 64 20 6f 72 20 74 68 be updated or th
43660 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 73 6f e size of the so
43670 75 72 63 65 20 64 61 74 61 62 61 73 65 20 66 69 urce database fi
43680 6c 65 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 2e 0a le.** changing..
43690 2a 2a 0a 2a 2a 20 3c 62 3e 43 6f 6e 63 75 72 72 **.** <b>Concurr
436a0 65 6e 74 20 55 73 61 67 65 20 6f 66 20 44 61 74 ent Usage of Dat
436b0 61 62 61 73 65 20 48 61 6e 64 6c 65 73 3c 2f 62 abase Handles</b
436c0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 75 72 >.**.** The sour
436d0 63 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ce [database con
436e0 6e 65 63 74 69 6f 6e 5d 20 6d 61 79 20 62 65 20 nection] may be
436f0 75 73 65 64 20 62 79 20 74 68 65 20 61 70 70 6c used by the appl
43700 69 63 61 74 69 6f 6e 20 66 6f 72 20 6f 74 68 65 ication for othe
43710 72 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 77 68 r.** purposes wh
43720 69 6c 65 20 61 20 62 61 63 6b 75 70 20 6f 70 65 ile a backup ope
43730 72 61 74 69 6f 6e 20 69 73 20 75 6e 64 65 72 77 ration is underw
43740 61 79 20 6f 72 20 62 65 69 6e 67 20 69 6e 69 74 ay or being init
43750 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 53 ialized..** If S
43760 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 QLite is compile
43770 64 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 65 64 d and configured
43780 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 72 65 to support thre
43790 61 64 73 61 66 65 20 64 61 74 61 62 61 73 65 0a adsafe database.
437a0 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20 ** connections,
437b0 74 68 65 6e 20 74 68 65 20 73 6f 75 72 63 65 20 then the source
437c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
437d0 69 6f 6e 20 6d 61 79 20 62 65 20 75 73 65 64 20 ion may be used
437e0 63 6f 6e 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 concurrently.**
437f0 66 72 6f 6d 20 77 69 74 68 69 6e 20 6f 74 68 65 from within othe
43800 72 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a r threads..**.**
43810 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 61 70 However, the ap
43820 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 67 plication must g
43830 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 uarantee that th
43840 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 e destination da
43850 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 tabase.** connec
43860 74 69 6f 6e 20 68 61 6e 64 6c 65 20 69 73 20 6e tion handle is n
43870 6f 74 20 70 61 73 73 65 64 20 74 6f 20 61 6e 79 ot passed to any
43880 20 6f 74 68 65 72 20 41 50 49 20 28 62 79 20 61 other API (by a
43890 6e 79 20 74 68 72 65 61 64 29 20 61 66 74 65 72 ny thread) after
438a0 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 .** sqlite3_bac
438b0 6b 75 70 5f 69 6e 69 74 28 29 20 69 73 20 63 61 kup_init() is ca
438c0 6c 6c 65 64 20 61 6e 64 20 62 65 66 6f 72 65 20 lled and before
438d0 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
438e0 67 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c g call to.** sql
438f0 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
43900 73 68 28 29 2e 20 55 6e 66 6f 72 74 75 6e 61 74 sh(). Unfortunat
43910 65 6c 79 20 53 51 4c 69 74 65 20 64 6f 65 73 20 ely SQLite does
43920 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 63 68 not currently ch
43930 65 63 6b 0a 2a 2a 20 66 6f 72 20 74 68 69 73 2c eck.** for this,
43940 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 if the applicat
43950 69 6f 6e 20 64 6f 65 73 20 75 73 65 20 74 68 65 ion does use the
43960 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64 61 destination [da
43970 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
43980 6e 5d 0a 2a 2a 20 66 6f 72 20 73 6f 6d 65 20 6f n].** for some o
43990 74 68 65 72 20 70 75 72 70 6f 73 65 20 64 75 72 ther purpose dur
439a0 69 6e 67 20 61 20 62 61 63 6b 75 70 20 6f 70 65 ing a backup ope
439b0 72 61 74 69 6f 6e 2c 20 74 68 69 6e 67 73 20 6d ration, things m
439c0 61 79 20 61 70 70 65 61 72 20 74 6f 0a 2a 2a 20 ay appear to.**
439d0 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 62 work correctly b
439e0 75 74 20 69 6e 20 66 61 63 74 20 62 65 20 73 75 ut in fact be su
439f0 62 74 6c 79 20 6d 61 6c 66 75 6e 63 74 69 6f 6e btly malfunction
43a00 69 6e 67 2e 20 20 55 73 65 20 6f 66 20 74 68 65 ing. Use of the
43a10 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 .** destination
43a20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
43a30 69 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 63 6b ion while a back
43a40 75 70 20 69 73 20 69 6e 20 70 72 6f 67 72 65 73 up is in progres
43a50 73 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f 20 s might.** also
43a60 63 61 75 73 65 20 61 20 6d 75 74 65 78 20 64 65 cause a mutex de
43a70 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 75 adlock..**.** Fu
43a80 72 74 68 65 72 6d 6f 72 65 2c 20 69 66 20 72 75 rthermore, if ru
43a90 6e 6e 69 6e 67 20 69 6e 20 5b 73 68 61 72 65 64 nning in [shared
43aa0 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 74 68 cache mode], th
43ab0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 e application mu
43ac0 73 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 st.** guarantee
43ad0 74 68 61 74 20 74 68 65 20 73 68 61 72 65 64 20 that the shared
43ae0 63 61 63 68 65 20 75 73 65 64 20 62 79 20 74 68 cache used by th
43af0 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 e destination da
43b00 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 6e 6f 74 tabase.** is not
43b10 20 61 63 63 65 73 73 65 64 20 77 68 69 6c 65 20 accessed while
43b20 74 68 65 20 62 61 63 6b 75 70 20 69 73 20 72 75 the backup is ru
43b30 6e 6e 69 6e 67 2e 20 49 6e 20 70 72 61 63 74 69 nning. In practi
43b40 63 65 20 74 68 69 73 20 6d 65 61 6e 73 0a 2a 2a ce this means.**
43b50 20 74 68 61 74 20 74 68 65 20 61 70 70 6c 69 63 that the applic
43b60 61 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61 ation must guara
43b70 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 66 69 ntee that the fi
43b80 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 20 62 le-system file b
43b90 65 69 6e 67 20 0a 2a 2a 20 62 61 63 6b 65 64 20 eing .** backed
43ba0 75 70 20 74 6f 20 69 73 20 6e 6f 74 20 61 63 63 up to is not acc
43bb0 65 73 73 65 64 20 62 79 20 61 6e 79 20 63 6f 6e essed by any con
43bc0 6e 65 63 74 69 6f 6e 20 77 69 74 68 69 6e 20 74 nection within t
43bd0 68 65 20 70 72 6f 63 65 73 73 2c 0a 2a 2a 20 6e he process,.** n
43be0 6f 74 20 6a 75 73 74 20 74 68 65 20 73 70 65 63 ot just the spec
43bf0 69 66 69 63 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 ific connection
43c00 74 68 61 74 20 77 61 73 20 70 61 73 73 65 64 20 that was passed
43c10 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
43c20 70 5f 69 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 p_init()..**.**
43c30 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 The [sqlite3_bac
43c40 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 74 73 65 kup] object itse
43c50 6c 66 20 69 73 20 70 61 72 74 69 61 6c 6c 79 20 lf is partially
43c60 74 68 72 65 61 64 73 61 66 65 2e 20 4d 75 6c 74 threadsafe. Mult
43c70 69 70 6c 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 iple .** threads
43c80 20 6d 61 79 20 73 61 66 65 6c 79 20 6d 61 6b 65 may safely make
43c90 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 63 75 72 multiple concur
43ca0 72 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 rent calls to sq
43cb0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
43cc0 70 28 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c p()..** However,
43cd0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 the sqlite3_bac
43ce0 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 kup_remaining()
43cf0 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b and sqlite3_back
43d00 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 0a 2a up_pagecount().*
43d10 2a 20 41 50 49 73 20 61 72 65 20 6e 6f 74 20 73 * APIs are not s
43d20 74 72 69 63 74 6c 79 20 73 70 65 61 6b 69 6e 67 trictly speaking
43d30 20 74 68 72 65 61 64 73 61 66 65 2e 20 49 66 20 threadsafe. If
43d40 74 68 65 79 20 61 72 65 20 69 6e 76 6f 6b 65 64 they are invoked
43d50 20 61 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 at the.** same
43d60 74 69 6d 65 20 61 73 20 61 6e 6f 74 68 65 72 20 time as another
43d70 74 68 72 65 61 64 20 69 73 20 69 6e 76 6f 6b 69 thread is invoki
43d80 6e 67 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ng sqlite3_backu
43d90 70 5f 73 74 65 70 28 29 20 69 74 20 69 73 0a 2a p_step() it is.*
43da0 2a 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 * possible that
43db0 74 68 65 79 20 72 65 74 75 72 6e 20 69 6e 76 61 they return inva
43dc0 6c 69 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 lid values..*/.S
43dd0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
43de0 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69 74 65 3_backup *sqlite
43df0 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 0a 20 3_backup_init(.
43e00 20 73 71 6c 69 74 65 33 20 2a 70 44 65 73 74 2c sqlite3 *pDest,
43e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43e20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 /* Desti
43e30 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 nation database
43e40 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 handle */. cons
43e50 74 20 63 68 61 72 20 2a 7a 44 65 73 74 4e 61 6d t char *zDestNam
43e60 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
43e70 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f /* Destinatio
43e80 6e 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 n database name
43e90 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 53 */. sqlite3 *pS
43ea0 6f 75 72 63 65 2c 20 20 20 20 20 20 20 20 20 20 ource,
43eb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
43ec0 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 ource database h
43ed0 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 andle */. const
43ee0 20 63 68 61 72 20 2a 7a 53 6f 75 72 63 65 4e 61 char *zSourceNa
43ef0 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 me
43f00 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 61 /* Source data
43f10 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a base name */.);.
43f20 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
43f30 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
43f40 65 70 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ep(sqlite3_backu
43f50 70 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 29 p *p, int nPage)
43f60 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
43f70 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
43f80 66 69 6e 69 73 68 28 73 71 6c 69 74 65 33 5f 62 finish(sqlite3_b
43f90 61 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c 49 54 ackup *p);.SQLIT
43fa0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
43fb0 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 3_backup_remaini
43fc0 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ng(sqlite3_backu
43fd0 70 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 41 50 p *p);.SQLITE_AP
43fe0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 I int sqlite3_ba
43ff0 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 73 ckup_pagecount(s
44000 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 qlite3_backup *p
44010 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
44020 45 46 3a 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66 EF: Unlock Notif
44030 69 63 61 74 69 6f 6e 0a 2a 2a 20 45 58 50 45 52 ication.** EXPER
44040 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 57 68 IMENTAL.**.** Wh
44050 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 73 68 en running in sh
44060 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c ared-cache mode,
44070 20 61 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 a database oper
44080 61 74 69 6f 6e 20 6d 61 79 20 66 61 69 6c 20 77 ation may fail w
44090 69 74 68 0a 2a 2a 20 61 6e 20 5b 53 51 4c 49 54 ith.** an [SQLIT
440a0 45 5f 4c 4f 43 4b 45 44 5d 20 65 72 72 6f 72 20 E_LOCKED] error
440b0 69 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20 if the required
440c0 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 73 68 61 locks on the sha
440d0 72 65 64 2d 63 61 63 68 65 20 6f 72 0a 2a 2a 20 red-cache or.**
440e0 69 6e 64 69 76 69 64 75 61 6c 20 74 61 62 6c 65 individual table
440f0 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 s within the sha
44100 72 65 64 2d 63 61 63 68 65 20 63 61 6e 6e 6f 74 red-cache cannot
44110 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 20 53 65 be obtained. Se
44120 65 0a 2a 2a 20 5b 53 51 4c 69 74 65 20 53 68 61 e.** [SQLite Sha
44130 72 65 64 2d 43 61 63 68 65 20 4d 6f 64 65 5d 20 red-Cache Mode]
44140 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 69 6f for a descriptio
44150 6e 20 6f 66 20 73 68 61 72 65 64 2d 63 61 63 68 n of shared-cach
44160 65 20 6c 6f 63 6b 69 6e 67 2e 20 0a 2a 2a 20 54 e locking. .** T
44170 68 69 73 20 41 50 49 20 6d 61 79 20 62 65 20 75 his API may be u
44180 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 sed to register
44190 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 a callback that
441a0 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f SQLite will invo
441b0 6b 65 20 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 ke .** when the
441c0 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 65 connection curre
441d0 6e 74 6c 79 20 68 6f 6c 64 69 6e 67 20 74 68 65 ntly holding the
441e0 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 20 72 required lock r
441f0 65 6c 69 6e 71 75 69 73 68 65 73 20 69 74 2e 0a elinquishes it..
44200 2a 2a 20 54 68 69 73 20 41 50 49 20 69 73 20 6f ** This API is o
44210 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 nly available if
44220 20 74 68 65 20 6c 69 62 72 61 72 79 20 77 61 73 the library was
44230 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 compiled with t
44240 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e he.** [SQLITE_EN
44250 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 ABLE_UNLOCK_NOTI
44260 46 59 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 FY] C-preprocess
44270 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 or symbol define
44280 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 d..**.** See Als
44290 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53 51 o: [Using the SQ
442a0 4c 69 74 65 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 Lite Unlock Noti
442b0 66 69 63 61 74 69 6f 6e 20 46 65 61 74 75 72 65 fication Feature
442c0 5d 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 64 2d ]..**.** Shared-
442d0 63 61 63 68 65 20 6c 6f 63 6b 73 20 61 72 65 20 cache locks are
442e0 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20 61 20 released when a
442f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
44300 69 6f 6e 20 63 6f 6e 63 6c 75 64 65 73 0a 2a 2a ion concludes.**
44310 20 69 74 73 20 63 75 72 72 65 6e 74 20 74 72 61 its current tra
44320 6e 73 61 63 74 69 6f 6e 2c 20 65 69 74 68 65 72 nsaction, either
44330 20 62 79 20 63 6f 6d 6d 69 74 74 69 6e 67 20 69 by committing i
44340 74 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 69 74 20 t or rolling it
44350 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 back. .**.** Whe
44360 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 n a connection (
44370 6b 6e 6f 77 6e 20 61 73 20 74 68 65 20 62 6c 6f known as the blo
44380 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 cked connection)
44390 20 66 61 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e fails to obtain
443a0 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 a.** shared-cac
443b0 68 65 20 6c 6f 63 6b 20 61 6e 64 20 53 51 4c 49 he lock and SQLI
443c0 54 45 5f 4c 4f 43 4b 45 44 20 69 73 20 72 65 74 TE_LOCKED is ret
443d0 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c urned to the cal
443e0 6c 65 72 2c 20 74 68 65 0a 2a 2a 20 69 64 65 6e ler, the.** iden
443f0 74 69 74 79 20 6f 66 20 74 68 65 20 64 61 74 61 tity of the data
44400 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
44410 28 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f (the blocking co
44420 6e 6e 65 63 74 69 6f 6e 29 20 74 68 61 74 0a 2a nnection) that.*
44430 2a 20 68 61 73 20 6c 6f 63 6b 65 64 20 74 68 65 * has locked the
44440 20 72 65 71 75 69 72 65 64 20 72 65 73 6f 75 72 required resour
44450 63 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 74 ce is stored int
44460 65 72 6e 61 6c 6c 79 2e 20 41 66 74 65 72 20 61 ernally. After a
44470 6e 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f n .** applicatio
44480 6e 20 72 65 63 65 69 76 65 73 20 61 6e 20 53 51 n receives an SQ
44490 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72 72 6f LITE_LOCKED erro
444a0 72 2c 20 69 74 20 6d 61 79 20 63 61 6c 6c 20 74 r, it may call t
444b0 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 75 6e he.** sqlite3_un
444c0 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 6d 65 lock_notify() me
444d0 74 68 6f 64 20 77 69 74 68 20 74 68 65 20 62 6c thod with the bl
444e0 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e ocked connection
444f0 20 68 61 6e 64 6c 65 20 61 73 20 0a 2a 2a 20 74 handle as .** t
44500 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
44510 74 20 74 6f 20 72 65 67 69 73 74 65 72 20 66 6f t to register fo
44520 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 r a callback tha
44530 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 t will be invoke
44540 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 62 6c d.** when the bl
44550 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f ocking connectio
44560 6e 73 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 ns current trans
44570 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 action is conclu
44580 64 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c ded. The.** call
44590 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 back is invoked
445a0 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 from within the
445b0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 5d 20 6f [sqlite3_step] o
445c0 72 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 r [sqlite3_close
445d0 5d 0a 2a 2a 20 63 61 6c 6c 20 74 68 61 74 20 63 ].** call that c
445e0 6f 6e 63 6c 75 64 65 73 20 74 68 65 20 62 6c 6f oncludes the blo
445f0 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e cking connection
44600 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a s transaction..*
44610 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f *.** If sqlite3_
44620 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 unlock_notify()
44630 69 73 20 63 61 6c 6c 65 64 20 69 6e 20 61 20 6d is called in a m
44640 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 ulti-threaded ap
44650 70 6c 69 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74 68 plication,.** th
44660 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 20 ere is a chance
44670 74 68 61 74 20 74 68 65 20 62 6c 6f 63 6b 69 6e that the blockin
44680 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 6c g connection wil
44690 6c 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a l have already.*
446a0 2a 20 63 6f 6e 63 6c 75 64 65 64 20 69 74 73 20 * concluded its
446b0 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 79 20 74 transaction by t
446c0 68 65 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f he time sqlite3_
446d0 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 unlock_notify()
446e0 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 49 is invoked..** I
446f0 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20 f this happens,
44700 74 68 65 6e 20 74 68 65 20 73 70 65 63 69 66 69 then the specifi
44710 65 64 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 ed callback is i
44720 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 65 nvoked immediate
44730 6c 79 2c 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 ly,.** from with
44740 69 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 in the call to s
44750 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f qlite3_unlock_no
44760 74 69 66 79 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 tify()..**.** If
44770 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e the blocked con
44780 6e 65 63 74 69 6f 6e 20 69 73 20 61 74 74 65 6d nection is attem
44790 70 74 69 6e 67 20 74 6f 20 6f 62 74 61 69 6e 20 pting to obtain
447a0 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 a write-lock on
447b0 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 a.** shared-cach
447c0 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 6d 6f 72 e table, and mor
447d0 65 20 74 68 61 6e 20 6f 6e 65 20 6f 74 68 65 72 e than one other
447e0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 connection curr
447f0 65 6e 74 6c 79 20 68 6f 6c 64 73 0a 2a 2a 20 61 ently holds.** a
44800 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 read-lock on th
44810 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 74 68 e same table, th
44820 65 6e 20 53 51 4c 69 74 65 20 61 72 62 69 74 72 en SQLite arbitr
44830 61 72 69 6c 79 20 73 65 6c 65 63 74 73 20 6f 6e arily selects on
44840 65 20 6f 66 20 0a 2a 2a 20 74 68 65 20 6f 74 68 e of .** the oth
44850 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 er connections t
44860 6f 20 75 73 65 20 61 73 20 74 68 65 20 62 6c 6f o use as the blo
44870 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e cking connection
44880 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 61 ..**.** There ma
44890 79 20 62 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65 y be at most one
448a0 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 unlock-notify c
448b0 61 6c 6c 62 61 63 6b 20 72 65 67 69 73 74 65 72 allback register
448c0 65 64 20 62 79 20 61 20 0a 2a 2a 20 62 6c 6f 63 ed by a .** bloc
448d0 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 ked connection.
448e0 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 If sqlite3_unloc
448f0 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 63 61 k_notify() is ca
44900 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a lled when the.**
44910 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 blocked connect
44920 69 6f 6e 20 61 6c 72 65 61 64 79 20 68 61 73 20 ion already has
44930 61 20 72 65 67 69 73 74 65 72 65 64 20 75 6e 6c a registered unl
44940 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 ock-notify callb
44950 61 63 6b 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 ack,.** then the
44960 20 6e 65 77 20 63 61 6c 6c 62 61 63 6b 20 72 65 new callback re
44970 70 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 2e 20 places the old.
44980 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 If sqlite3_unloc
44990 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 0a 2a 2a k_notify() is.**
449a0 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e called with a N
449b0 55 4c 4c 20 70 6f 69 6e 74 65 72 20 61 73 20 69 ULL pointer as i
449c0 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 ts second argume
449d0 6e 74 2c 20 74 68 65 6e 20 61 6e 79 20 65 78 69 nt, then any exi
449e0 73 74 69 6e 67 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d sting.** unlock-
449f0 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 notify callback
44a00 69 73 20 63 61 6e 63 65 6c 6c 65 64 2e 20 54 68 is cancelled. Th
44a10 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 e blocked connec
44a20 74 69 6f 6e 73 20 0a 2a 2a 20 75 6e 6c 6f 63 6b tions .** unlock
44a30 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
44a40 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 63 61 6e may also be can
44a50 63 65 6c 65 64 20 62 79 20 63 6c 6f 73 69 6e 67 celed by closing
44a60 20 74 68 65 20 62 6c 6f 63 6b 65 64 0a 2a 2a 20 the blocked.**
44a70 63 6f 6e 6e 65 63 74 69 6f 6e 20 75 73 69 6e 67 connection using
44a80 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 [sqlite3_close(
44a90 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 6e )]..**.** The un
44aa0 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c lock-notify call
44ab0 62 61 63 6b 20 69 73 20 6e 6f 74 20 72 65 65 6e back is not reen
44ac0 74 72 61 6e 74 2e 20 49 66 20 61 6e 20 61 70 70 trant. If an app
44ad0 6c 69 63 61 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 lication invokes
44ae0 0a 2a 2a 20 61 6e 79 20 73 71 6c 69 74 65 33 5f .** any sqlite3_
44af0 78 78 78 20 41 50 49 20 66 75 6e 63 74 69 6f 6e xxx API function
44b00 73 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 6e s from within an
44b10 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 unlock-notify c
44b20 61 6c 6c 62 61 63 6b 2c 20 61 0a 2a 2a 20 63 72 allback, a.** cr
44b30 61 73 68 20 6f 72 20 64 65 61 64 6c 6f 63 6b 20 ash or deadlock
44b40 6d 61 79 20 62 65 20 74 68 65 20 72 65 73 75 6c may be the resul
44b50 74 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 t..**.** Unless
44b60 64 65 61 64 6c 6f 63 6b 20 69 73 20 64 65 74 65 deadlock is dete
44b70 63 74 65 64 20 28 73 65 65 20 62 65 6c 6f 77 29 cted (see below)
44b80 2c 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b , sqlite3_unlock
44b90 5f 6e 6f 74 69 66 79 28 29 20 61 6c 77 61 79 73 _notify() always
44ba0 0a 2a 2a 20 72 65 74 75 72 6e 73 20 53 51 4c 49 .** returns SQLI
44bb0 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e TE_OK..**.** <b>
44bc0 43 61 6c 6c 62 61 63 6b 20 49 6e 76 6f 63 61 74 Callback Invocat
44bd0 69 6f 6e 20 44 65 74 61 69 6c 73 3c 2f 62 3e 0a ion Details</b>.
44be0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 75 6e **.** When an un
44bf0 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c lock-notify call
44c00 62 61 63 6b 20 69 73 20 72 65 67 69 73 74 65 72 back is register
44c10 65 64 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74 ed, the applicat
44c20 69 6f 6e 20 70 72 6f 76 69 64 65 73 20 61 20 0a ion provides a .
44c30 2a 2a 20 73 69 6e 67 6c 65 20 76 6f 69 64 2a 20 ** single void*
44c40 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20 pointer that is
44c50 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 63 61 passed to the ca
44c60 6c 6c 62 61 63 6b 20 77 68 65 6e 20 69 74 20 69 llback when it i
44c70 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 48 6f s invoked..** Ho
44c80 77 65 76 65 72 2c 20 74 68 65 20 73 69 67 6e 61 wever, the signa
44c90 74 75 72 65 20 6f 66 20 74 68 65 20 63 61 6c 6c ture of the call
44ca0 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 61 6c back function al
44cb0 6c 6f 77 73 20 53 51 4c 69 74 65 20 74 6f 20 70 lows SQLite to p
44cc0 61 73 73 0a 2a 2a 20 69 74 20 61 6e 20 61 72 72 ass.** it an arr
44cd0 61 79 20 6f 66 20 76 6f 69 64 2a 20 63 6f 6e 74 ay of void* cont
44ce0 65 78 74 20 70 6f 69 6e 74 65 72 73 2e 20 54 68 ext pointers. Th
44cf0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
44d00 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 61 6e passed to.** an
44d10 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 unlock-notify c
44d20 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 70 6f 69 allback is a poi
44d30 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 nter to an array
44d40 20 6f 66 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 of void* pointe
44d50 72 73 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 rs,.** and the s
44d60 65 63 6f 6e 64 20 69 73 20 74 68 65 20 6e 75 6d econd is the num
44d70 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
44d80 6e 20 74 68 65 20 61 72 72 61 79 2e 0a 2a 2a 0a n the array..**.
44d90 2a 2a 20 57 68 65 6e 20 61 20 62 6c 6f 63 6b 69 ** When a blocki
44da0 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 ng connections t
44db0 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f ransaction is co
44dc0 6e 63 6c 75 64 65 64 2c 20 74 68 65 72 65 20 6d ncluded, there m
44dd0 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 74 68 ay be.** more th
44de0 61 6e 20 6f 6e 65 20 62 6c 6f 63 6b 65 64 20 63 an one blocked c
44df0 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 68 onnection that h
44e00 61 73 20 72 65 67 69 73 74 65 72 65 64 20 66 6f as registered fo
44e10 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 r an unlock-noti
44e20 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 fy.** callback.
44e30 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73 If two or more s
44e40 75 63 68 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e uch blocked conn
44e50 65 63 74 69 6f 6e 73 20 68 61 76 65 20 73 70 65 ections have spe
44e60 63 69 66 69 65 64 20 74 68 65 0a 2a 2a 20 73 61 cified the.** sa
44e70 6d 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 me callback func
44e80 74 69 6f 6e 2c 20 74 68 65 6e 20 69 6e 73 74 65 tion, then inste
44e90 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74 ad of invoking t
44ea0 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 he callback func
44eb0 74 69 6f 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 tion.** multiple
44ec0 20 74 69 6d 65 73 2c 20 69 74 20 69 73 20 69 6e times, it is in
44ed0 76 6f 6b 65 64 20 6f 6e 63 65 20 77 69 74 68 20 voked once with
44ee0 74 68 65 20 73 65 74 20 6f 66 20 76 6f 69 64 2a the set of void*
44ef0 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 context pointer
44f00 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 s.** specified b
44f10 79 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f y the blocked co
44f20 6e 6e 65 63 74 69 6f 6e 73 20 62 75 6e 64 6c 65 nnections bundle
44f30 64 20 74 6f 67 65 74 68 65 72 20 69 6e 74 6f 20 d together into
44f40 61 6e 20 61 72 72 61 79 2e 0a 2a 2a 20 54 68 69 an array..** Thi
44f50 73 20 67 69 76 65 73 20 74 68 65 20 61 70 70 6c s gives the appl
44f60 69 63 61 74 69 6f 6e 20 61 6e 20 6f 70 70 6f 72 ication an oppor
44f70 74 75 6e 69 74 79 20 74 6f 20 70 72 69 6f 72 69 tunity to priori
44f80 74 69 7a 65 20 61 6e 79 20 61 63 74 69 6f 6e 73 tize any actions
44f90 20 0a 2a 2a 20 72 65 6c 61 74 65 64 20 74 6f 20 .** related to
44fa0 74 68 65 20 73 65 74 20 6f 66 20 75 6e 62 6c 6f the set of unblo
44fb0 63 6b 65 64 20 64 61 74 61 62 61 73 65 20 63 6f cked database co
44fc0 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a nnections..**.**
44fd0 20 3c 62 3e 44 65 61 64 6c 6f 63 6b 20 44 65 74 <b>Deadlock Det
44fe0 65 63 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a ection</b>.**.**
44ff0 20 41 73 73 75 6d 69 6e 67 20 74 68 61 74 20 61 Assuming that a
45000 66 74 65 72 20 72 65 67 69 73 74 65 72 69 6e 67 fter registering
45010 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e for an unlock-n
45020 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 61 otify callback a
45030 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 61 .** database wa
45040 69 74 73 20 66 6f 72 20 74 68 65 20 63 61 6c 6c its for the call
45050 62 61 63 6b 20 74 6f 20 62 65 20 69 73 73 75 65 back to be issue
45060 64 20 62 65 66 6f 72 65 20 74 61 6b 69 6e 67 20 d before taking
45070 61 6e 79 20 66 75 72 74 68 65 72 0a 2a 2a 20 61 any further.** a
45080 63 74 69 6f 6e 20 28 61 20 72 65 61 73 6f 6e 61 ction (a reasona
45090 62 6c 65 20 61 73 73 75 6d 70 74 69 6f 6e 29 2c ble assumption),
450a0 20 74 68 65 6e 20 75 73 69 6e 67 20 74 68 69 73 then using this
450b0 20 41 50 49 20 6d 61 79 20 63 61 75 73 65 20 74 API may cause t
450c0 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f he.** applicatio
450d0 6e 20 74 6f 20 64 65 61 64 6c 6f 63 6b 2e 20 46 n to deadlock. F
450e0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 63 or example, if c
450f0 6f 6e 6e 65 63 74 69 6f 6e 20 58 20 69 73 20 77 onnection X is w
45100 61 69 74 69 6e 67 20 66 6f 72 0a 2a 2a 20 63 6f aiting for.** co
45110 6e 6e 65 63 74 69 6f 6e 20 59 27 73 20 74 72 61 nnection Y's tra
45120 6e 73 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 nsaction to be c
45130 6f 6e 63 6c 75 64 65 64 2c 20 61 6e 64 20 73 69 oncluded, and si
45140 6d 69 6c 61 72 6c 79 20 63 6f 6e 6e 65 63 74 69 milarly connecti
45150 6f 6e 0a 2a 2a 20 59 20 69 73 20 77 61 69 74 69 on.** Y is waiti
45160 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e ng on connection
45170 20 58 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e X's transaction
45180 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 63 , then neither c
45190 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c onnection.** wil
451a0 6c 20 70 72 6f 63 65 65 64 20 61 6e 64 20 74 68 l proceed and th
451b0 65 20 73 79 73 74 65 6d 20 6d 61 79 20 72 65 6d e system may rem
451c0 61 69 6e 20 64 65 61 64 6c 6f 63 6b 65 64 20 69 ain deadlocked i
451d0 6e 64 65 66 69 6e 69 74 65 6c 79 2e 0a 2a 2a 0a ndefinitely..**.
451e0 2a 2a 20 54 6f 20 61 76 6f 69 64 20 74 68 69 73 ** To avoid this
451f0 20 73 63 65 6e 61 72 69 6f 2c 20 74 68 65 20 73 scenario, the s
45200 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f qlite3_unlock_no
45210 74 69 66 79 28 29 20 70 65 72 66 6f 72 6d 73 20 tify() performs
45220 64 65 61 64 6c 6f 63 6b 0a 2a 2a 20 64 65 74 65 deadlock.** dete
45230 63 74 69 6f 6e 2e 20 49 66 20 61 20 67 69 76 65 ction. If a give
45240 6e 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 n call to sqlite
45250 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 3_unlock_notify(
45260 29 20 77 6f 75 6c 64 20 70 75 74 20 74 68 65 0a ) would put the.
45270 2a 2a 20 73 79 73 74 65 6d 20 69 6e 20 61 20 64 ** system in a d
45280 65 61 64 6c 6f 63 6b 65 64 20 73 74 61 74 65 2c eadlocked state,
45290 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 then SQLITE_LOC
452a0 4b 45 44 20 69 73 20 72 65 74 75 72 6e 65 64 20 KED is returned
452b0 61 6e 64 20 6e 6f 0a 2a 2a 20 75 6e 6c 6f 63 6b and no.** unlock
452c0 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
452d0 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2e 20 is registered.
452e0 54 68 65 20 73 79 73 74 65 6d 20 69 73 20 73 61 The system is sa
452f0 69 64 20 74 6f 20 62 65 20 69 6e 0a 2a 2a 20 61 id to be in.** a
45300 20 64 65 61 64 6c 6f 63 6b 65 64 20 73 74 61 74 deadlocked stat
45310 65 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 e if connection
45320 41 20 68 61 73 20 72 65 67 69 73 74 65 72 65 64 A has registered
45330 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e for an unlock-n
45340 6f 74 69 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 otify.** callbac
45350 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c 75 73 k on the conclus
45360 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f ion of connectio
45370 6e 20 42 27 73 20 74 72 61 6e 73 61 63 74 69 6f n B's transactio
45380 6e 2c 20 61 6e 64 20 63 6f 6e 6e 65 63 74 69 6f n, and connectio
45390 6e 0a 2a 2a 20 42 20 68 61 73 20 69 74 73 65 6c n.** B has itsel
453a0 66 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 f registered for
453b0 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 an unlock-notif
453c0 79 20 63 61 6c 6c 62 61 63 6b 20 77 68 65 6e 20 y callback when
453d0 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 41 27 connection.** A'
453e0 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 s transaction is
453f0 20 63 6f 6e 63 6c 75 64 65 64 2e 20 49 6e 64 69 concluded. Indi
45400 72 65 63 74 20 64 65 61 64 6c 6f 63 6b 20 69 73 rect deadlock is
45410 20 61 6c 73 6f 20 64 65 74 65 63 74 65 64 2c 20 also detected,
45420 73 6f 0a 2a 2a 20 74 68 65 20 73 79 73 74 65 6d so.** the system
45430 20 69 73 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 is also conside
45440 72 65 64 20 74 6f 20 62 65 20 64 65 61 64 6c 6f red to be deadlo
45450 63 6b 65 64 20 69 66 20 63 6f 6e 6e 65 63 74 69 cked if connecti
45460 6f 6e 20 42 20 68 61 73 0a 2a 2a 20 72 65 67 69 on B has.** regi
45470 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 6e stered for an un
45480 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c lock-notify call
45490 62 61 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 back on the conc
454a0 6c 75 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 lusion of connec
454b0 74 69 6f 6e 0a 2a 2a 20 43 27 73 20 74 72 61 6e tion.** C's tran
454c0 73 61 63 74 69 6f 6e 2c 20 77 68 65 72 65 20 63 saction, where c
454d0 6f 6e 6e 65 63 74 69 6f 6e 20 43 20 69 73 20 77 onnection C is w
454e0 61 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 aiting on connec
454f0 74 69 6f 6e 20 41 2e 20 41 6e 79 0a 2a 2a 20 6e tion A. Any.** n
45500 75 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 umber of levels
45510 6f 66 20 69 6e 64 69 72 65 63 74 69 6f 6e 20 61 of indirection a
45520 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2a 0a 2a re allowed..**.*
45530 2a 20 3c 62 3e 54 68 65 20 22 44 52 4f 50 20 54 * <b>The "DROP T
45540 41 42 4c 45 22 20 45 78 63 65 70 74 69 6f 6e 3c ABLE" Exception<
45550 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 /b>.**.** When a
45560 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
45570 33 5f 73 74 65 70 28 29 5d 20 72 65 74 75 72 6e 3_step()] return
45580 73 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c s SQLITE_LOCKED,
45590 20 69 74 20 69 73 20 61 6c 6d 6f 73 74 20 0a 2a it is almost .*
455a0 2a 20 61 6c 77 61 79 73 20 61 70 70 72 6f 70 72 * always appropr
455b0 69 61 74 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c iate to call sql
455c0 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 ite3_unlock_noti
455d0 66 79 28 29 2e 20 54 68 65 72 65 20 69 73 20 68 fy(). There is h
455e0 6f 77 65 76 65 72 2c 0a 2a 2a 20 6f 6e 65 20 65 owever,.** one e
455f0 78 63 65 70 74 69 6f 6e 2e 20 57 68 65 6e 20 65 xception. When e
45600 78 65 63 75 74 69 6e 67 20 61 20 22 44 52 4f 50 xecuting a "DROP
45610 20 54 41 42 4c 45 22 20 6f 72 20 22 44 52 4f 50 TABLE" or "DROP
45620 20 49 4e 44 45 58 22 20 73 74 61 74 65 6d 65 6e INDEX" statemen
45630 74 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 63 68 65 t,.** SQLite che
45640 63 6b 73 20 69 66 20 74 68 65 72 65 20 61 72 65 cks if there are
45650 20 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 65 any currently e
45660 78 65 63 75 74 69 6e 67 20 53 45 4c 45 43 54 20 xecuting SELECT
45670 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 statements.** th
45680 61 74 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68 65 at belong to the
45690 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e same connection
456a0 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 2c 20 . If there are,
456b0 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73 SQLITE_LOCKED is
456c0 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 49 6e .** returned. In
456d0 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 this case there
456e0 20 69 73 20 6e 6f 20 22 62 6c 6f 63 6b 69 6e 67 is no "blocking
456f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 22 2c 20 73 6f connection", so
45700 20 69 6e 76 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c invoking.** sql
45710 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 ite3_unlock_noti
45720 66 79 28 29 20 72 65 73 75 6c 74 73 20 69 6e 20 fy() results in
45730 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 the unlock-notif
45740 79 20 63 61 6c 6c 62 61 63 6b 20 62 65 69 6e 67 y callback being
45750 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 .** invoked imme
45760 64 69 61 74 65 6c 79 2e 20 49 66 20 74 68 65 20 diately. If the
45770 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 65 6e application then
45780 20 72 65 2d 61 74 74 65 6d 70 74 73 20 74 68 65 re-attempts the
45790 20 22 44 52 4f 50 20 54 41 42 4c 45 22 0a 2a 2a "DROP TABLE".**
457a0 20 6f 72 20 22 44 52 4f 50 20 49 4e 44 45 58 22 or "DROP INDEX"
457b0 20 71 75 65 72 79 2c 20 61 6e 20 69 6e 66 69 6e query, an infin
457c0 69 74 65 20 6c 6f 6f 70 20 6d 69 67 68 74 20 62 ite loop might b
457d0 65 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a e the result..**
457e0 0a 2a 2a 20 4f 6e 65 20 77 61 79 20 61 72 6f 75 .** One way arou
457f0 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 nd this problem
45800 69 73 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 is to check the
45810 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 extended error c
45820 6f 64 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 ode returned.**
45830 62 79 20 61 6e 20 73 71 6c 69 74 65 33 5f 73 74 by an sqlite3_st
45840 65 70 28 29 20 63 61 6c 6c 2e 20 49 66 20 74 68 ep() call. If th
45850 65 72 65 20 69 73 20 61 20 62 6c 6f 63 6b 69 6e ere is a blockin
45860 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 g connection, th
45870 65 6e 20 74 68 65 0a 2a 2a 20 65 78 74 65 6e 64 en the.** extend
45880 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 ed error code is
45890 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4c set to SQLITE_L
458a0 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 OCKED_SHAREDCACH
458b0 45 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 6e E. Otherwise, in
458c0 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20 .** the special
458d0 22 44 52 4f 50 20 54 41 42 4c 45 2f 49 4e 44 45 "DROP TABLE/INDE
458e0 58 22 20 63 61 73 65 2c 20 74 68 65 20 65 78 74 X" case, the ext
458f0 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 ended error code
45900 20 69 73 20 6a 75 73 74 20 0a 2a 2a 20 53 51 4c is just .** SQL
45910 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 53 ITE_LOCKED..*/.S
45920 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
45930 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 lite3_unlock_not
45940 69 66 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a ify(. sqlite3 *
45950 70 42 6c 6f 63 6b 65 64 2c 20 20 20 20 20 20 20 pBlocked,
45960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45970 20 20 20 2f 2a 20 57 61 69 74 69 6e 67 20 63 6f /* Waiting co
45980 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f nnection */. vo
45990 69 64 20 28 2a 78 4e 6f 74 69 66 79 29 28 76 6f id (*xNotify)(vo
459a0 69 64 20 2a 2a 61 70 41 72 67 2c 20 69 6e 74 20 id **apArg, int
459b0 6e 41 72 67 29 2c 20 20 20 20 2f 2a 20 43 61 6c nArg), /* Cal
459c0 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 74 lback function t
459d0 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20 76 6f o invoke */. vo
459e0 69 64 20 2a 70 4e 6f 74 69 66 79 41 72 67 20 20 id *pNotifyArg
459f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45a00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 /* Arg
45a10 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f ument to pass to
45a20 20 78 4e 6f 74 69 66 79 20 2a 2f 0a 29 3b 0a 0a xNotify */.);..
45a30 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
45a40 20 53 74 72 69 6e 67 20 43 6f 6d 70 61 72 69 73 String Comparis
45a50 6f 6e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 on.** EXPERIMENT
45a60 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 AL.**.** The [sq
45a70 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 29 lite3_strnicmp()
45a80 5d 20 41 50 49 20 61 6c 6c 6f 77 73 20 61 70 70 ] API allows app
45a90 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 65 78 lications and ex
45aa0 74 65 6e 73 69 6f 6e 73 20 74 6f 0a 2a 2a 20 63 tensions to.** c
45ab0 6f 6d 70 61 72 65 20 74 68 65 20 63 6f 6e 74 65 ompare the conte
45ac0 6e 74 73 20 6f 66 20 74 77 6f 20 62 75 66 66 65 nts of two buffe
45ad0 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 55 54 rs containing UT
45ae0 46 2d 38 20 73 74 72 69 6e 67 73 20 69 6e 20 61 F-8 strings in a
45af0 0a 2a 2a 20 63 61 73 65 2d 69 6e 64 65 6e 64 65 .** case-indende
45b00 6e 74 20 66 61 73 68 69 6f 6e 2c 20 75 73 69 6e nt fashion, usin
45b10 67 20 74 68 65 20 73 61 6d 65 20 64 65 66 69 6e g the same defin
45b20 69 74 69 6f 6e 20 6f 66 20 63 61 73 65 20 69 6e ition of case in
45b30 64 65 70 65 6e 64 65 6e 63 65 20 0a 2a 2a 20 74 dependence .** t
45b40 68 61 74 20 53 51 4c 69 74 65 20 75 73 65 73 20 hat SQLite uses
45b50 69 6e 74 65 72 6e 61 6c 6c 79 20 77 68 65 6e 20 internally when
45b60 63 6f 6d 70 61 72 69 6e 67 20 69 64 65 6e 74 69 comparing identi
45b70 66 69 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 fiers..*/.SQLITE
45b80 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
45b90 5f 73 74 72 6e 69 63 6d 70 28 63 6f 6e 73 74 20 _strnicmp(const
45ba0 63 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68 char *, const ch
45bb0 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a ar *, int);../*.
45bc0 2a 2a 20 55 6e 64 6f 20 74 68 65 20 68 61 63 6b ** Undo the hack
45bd0 20 74 68 61 74 20 63 6f 6e 76 65 72 74 73 20 66 that converts f
45be0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 74 79 loating point ty
45bf0 70 65 73 20 74 6f 20 69 6e 74 65 67 65 72 20 66 pes to integer f
45c00 6f 72 0a 2a 2a 20 62 75 69 6c 64 73 20 6f 6e 20 or.** builds on
45c10 70 72 6f 63 65 73 73 6f 72 73 20 77 69 74 68 6f processors witho
45c20 75 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e ut floating poin
45c30 74 20 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69 t support..*/.#i
45c40 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
45c50 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a _FLOATING_POINT.
45c60 23 20 75 6e 64 65 66 20 64 6f 75 62 6c 65 0a 23 # undef double.#
45c70 65 6e 64 69 66 0a 0a 23 69 66 20 30 0a 7d 20 20 endif..#if 0.}
45c80 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 27 65 /* End of the 'e
45c90 78 74 65 72 6e 20 22 43 22 27 20 62 6c 6f 63 6b xtern "C"' block
45ca0 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 */.#endif.#endi
45cb0 66 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f.../***********
45cc0 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 *** End of sqlit
45cd0 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e3.h ***********
45ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45d00 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
45d10 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
45d20 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
45d30 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 in sqliteInt.h
45d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45d50 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
45d60 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 61 73 68 *** Include hash
45d70 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
45d80 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 of sqliteInt.h
45d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45da0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
45db0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 *** Begin file h
45dc0 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ash.h **********
45dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45df0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 **/./*.** 2001 S
45e00 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a eptember 22.**.*
45e10 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
45e20 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
45e30 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
45e40 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
45e50 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
45e60 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
45e70 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
45e80 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
45e90 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
45ea0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
45eb0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
45ec0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
45ed0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
45ee0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
45ef0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
45f00 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
45f10 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
45f20 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
45f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
45f70 69 73 20 69 73 20 74 68 65 20 68 65 61 64 65 72 is is the header
45f80 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 67 65 file for the ge
45f90 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c 65 neric hash-table
45fa0 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a implemenation.*
45fb0 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 * used in SQLite
45fc0 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73 ..**.** $Id: has
45fd0 68 2e 68 2c 76 20 31 2e 31 35 20 32 30 30 39 2f h.h,v 1.15 2009/
45fe0 30 35 2f 30 32 20 31 33 3a 32 39 3a 33 38 20 64 05/02 13:29:38 d
45ff0 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e rh Exp $.*/.#ifn
46000 64 65 66 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 def _SQLITE_HASH
46010 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c _H_.#define _SQL
46020 49 54 45 5f 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20 ITE_HASH_H_../*
46030 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 Forward declarat
46040 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72 ions of structur
46050 65 73 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 es. */.typedef s
46060 74 72 75 63 74 20 48 61 73 68 20 48 61 73 68 3b truct Hash Hash;
46070 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
46080 48 61 73 68 45 6c 65 6d 20 48 61 73 68 45 6c 65 HashElem HashEle
46090 6d 3b 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65 74 m;../* A complet
460a0 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 e hash table is
460b0 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
460c0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
460d0 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 69 ucture..** The i
460e0 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 nternals of this
460f0 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 69 structure are i
46100 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 ntended to be op
46110 61 71 75 65 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a aque -- client.*
46120 2a 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 6f * code should no
46130 74 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63 63 t attempt to acc
46140 65 73 73 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 ess or modify th
46150 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73 e fields of this
46160 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 69 structure.** di
46170 72 65 63 74 6c 79 2e 20 20 43 68 61 6e 67 65 20 rectly. Change
46180 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f this structure o
46190 6e 6c 79 20 62 79 20 75 73 69 6e 67 20 74 68 65 nly by using the
461a0 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e routines below.
461b0 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d .** However, som
461c0 65 20 6f 66 20 74 68 65 20 22 70 72 6f 63 65 64 e of the "proced
461d0 75 72 65 73 22 20 61 6e 64 20 22 66 75 6e 63 74 ures" and "funct
461e0 69 6f 6e 73 22 20 66 6f 72 20 6d 6f 64 69 66 79 ions" for modify
461f0 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 63 63 65 73 ing and.** acces
46200 73 69 6e 67 20 74 68 69 73 20 73 74 72 75 63 74 sing this struct
46210 75 72 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6d ure are really m
46220 61 63 72 6f 73 2c 20 73 6f 20 77 65 20 63 61 6e acros, so we can
46230 27 74 20 72 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a 't really make.*
46240 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 * this structure
46250 20 6f 70 61 71 75 65 2e 0a 2a 2a 0a 2a 2a 20 41 opaque..**.** A
46260 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 ll elements of t
46270 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 61 72 he hash table ar
46280 65 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f e on a single do
46290 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 ubly-linked list
462a0 2e 0a 2a 2a 20 48 61 73 68 2e 66 69 72 73 74 20 ..** Hash.first
462b0 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 68 65 points to the he
462c0 61 64 20 6f 66 20 74 68 69 73 20 6c 69 73 74 2e ad of this list.
462d0 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 .**.** There are
462e0 20 48 61 73 68 2e 68 74 73 69 7a 65 20 62 75 63 Hash.htsize buc
462f0 6b 65 74 73 2e 20 20 45 61 63 68 20 62 75 63 6b kets. Each buck
46300 65 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 et points to a s
46310 70 6f 74 20 69 6e 0a 2a 2a 20 74 68 65 20 67 6c pot in.** the gl
46320 6f 62 61 6c 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b obal doubly-link
46330 65 64 20 6c 69 73 74 2e 20 20 54 68 65 20 63 6f ed list. The co
46340 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 75 ntents of the bu
46350 63 6b 65 74 20 61 72 65 20 74 68 65 0a 2a 2a 20 cket are the.**
46360 65 6c 65 6d 65 6e 74 20 70 6f 69 6e 74 65 64 20 element pointed
46370 74 6f 20 70 6c 75 73 20 74 68 65 20 6e 65 78 74 to plus the next
46380 20 5f 68 74 2e 63 6f 75 6e 74 2d 31 20 65 6c 65 _ht.count-1 ele
46390 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 6c 69 73 ments in the lis
463a0 74 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 2e 68 74 t..**.** Hash.ht
463b0 73 69 7a 65 20 61 6e 64 20 48 61 73 68 2e 68 74 size and Hash.ht
463c0 20 6d 61 79 20 62 65 20 7a 65 72 6f 2e 20 20 49 may be zero. I
463d0 6e 20 74 68 61 74 20 63 61 73 65 20 6c 6f 6f 6b n that case look
463e0 75 70 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 62 79 up is done.** by
463f0 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68 a linear search
46400 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 6c of the global l
46410 69 73 74 2e 20 20 46 6f 72 20 73 6d 61 6c 6c 20 ist. For small
46420 74 61 62 6c 65 73 2c 20 74 68 65 20 0a 2a 2a 20 tables, the .**
46430 48 61 73 68 2e 68 74 20 74 61 62 6c 65 20 69 73 Hash.ht table is
46440 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65 64 never allocated
46450 20 62 65 63 61 75 73 65 20 69 66 20 74 68 65 72 because if ther
46460 65 20 61 72 65 20 66 65 77 20 65 6c 65 6d 65 6e e are few elemen
46470 74 73 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61 62 ts.** in the tab
46480 6c 65 2c 20 69 74 20 69 73 20 66 61 73 74 65 72 le, it is faster
46490 20 74 6f 20 64 6f 20 61 20 6c 69 6e 65 61 72 20 to do a linear
464a0 73 65 61 72 63 68 20 74 68 61 6e 20 74 6f 20 6d search than to m
464b0 61 6e 61 67 65 0a 2a 2a 20 74 68 65 20 68 61 73 anage.** the has
464c0 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 h table..*/.stru
464d0 63 74 20 48 61 73 68 20 7b 0a 20 20 75 6e 73 69 ct Hash {. unsi
464e0 67 6e 65 64 20 69 6e 74 20 68 74 73 69 7a 65 3b gned int htsize;
464f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
46500 6f 66 20 62 75 63 6b 65 74 73 20 69 6e 20 74 68 of buckets in th
46510 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a e hash table */.
46520 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 unsigned int c
46530 6f 75 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e ount; /* N
46540 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
46550 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a in this table *
46560 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 66 69 /. HashElem *fi
46570 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a rst; /*
46580 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 The first eleme
46590 6e 74 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 nt of the array
465a0 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 */. struct _ht
465b0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f { /
465c0 2a 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 * the hash table
465d0 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 6f 75 6e */. int coun
465e0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
465f0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
46600 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 68 69 entries with thi
46610 73 20 68 61 73 68 20 2a 2f 0a 20 20 20 20 48 61 s hash */. Ha
46620 73 68 45 6c 65 6d 20 2a 63 68 61 69 6e 3b 20 20 shElem *chain;
46630 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
46640 74 65 72 20 74 6f 20 66 69 72 73 74 20 65 6e 74 ter to first ent
46650 72 79 20 77 69 74 68 20 74 68 69 73 20 68 61 73 ry with this has
46660 68 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a 7d 3b h */. } *ht;.};
46670 0a 0a 2f 2a 20 45 61 63 68 20 65 6c 65 6d 65 6e ../* Each elemen
46680 74 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 t in the hash ta
46690 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e ble is an instan
466a0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
466b0 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 ing .** structur
466c0 65 2e 20 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 e. All elements
466d0 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61 are stored on a
466e0 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c single doubly-l
466f0 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a inked list..**.*
46700 2a 20 41 67 61 69 6e 2c 20 74 68 69 73 20 73 74 * Again, this st
46710 72 75 63 74 75 72 65 20 69 73 20 69 6e 74 65 6e ructure is inten
46720 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 ded to be opaque
46730 2c 20 62 75 74 20 69 74 20 63 61 6e 27 74 20 72 , but it can't r
46740 65 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70 61 71 eally.** be opaq
46750 75 65 20 62 65 63 61 75 73 65 20 69 74 20 69 73 ue because it is
46760 20 75 73 65 64 20 62 79 20 6d 61 63 72 6f 73 2e used by macros.
46770 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73 68 45 .*/.struct HashE
46780 6c 65 6d 20 7b 0a 20 20 48 61 73 68 45 6c 65 6d lem {. HashElem
46790 20 2a 6e 65 78 74 2c 20 2a 70 72 65 76 3b 20 20 *next, *prev;
467a0 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 61 6e 64 /* Next and
467b0 20 70 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e previous elemen
467c0 74 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 ts in the table
467d0 2a 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b */. void *data;
467e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
467f0 20 20 2f 2a 20 44 61 74 61 20 61 73 73 6f 63 69 /* Data associ
46800 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65 ated with this e
46810 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 lement */. cons
46820 74 20 63 68 61 72 20 2a 70 4b 65 79 3b 20 69 6e t char *pKey; in
46830 74 20 6e 4b 65 79 3b 20 20 2f 2a 20 4b 65 79 20 t nKey; /* Key
46840 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
46850 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a this element */.
46860 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 };../*.** Access
46870 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f 20 64 routines. To d
46880 65 6c 65 74 65 2c 20 69 6e 73 65 72 74 20 61 20 elete, insert a
46890 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f NULL pointer..*/
468a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
468b0 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 void sqlite3Hash
468c0 49 6e 69 74 28 48 61 73 68 2a 29 3b 0a 53 51 4c Init(Hash*);.SQL
468d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
468e0 20 2a 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 *sqlite3HashIns
468f0 65 72 74 28 48 61 73 68 2a 2c 20 63 6f 6e 73 74 ert(Hash*, const
46900 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e 74 char *pKey, int
46910 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 70 44 61 nKey, void *pDa
46920 74 61 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ta);.SQLITE_PRIV
46930 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
46940 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 3HashFind(const
46950 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 63 68 61 Hash*, const cha
46960 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 r *pKey, int nKe
46970 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 y);.SQLITE_PRIVA
46980 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 TE void sqlite3H
46990 61 73 68 43 6c 65 61 72 28 48 61 73 68 2a 29 3b ashClear(Hash*);
469a0 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 ../*.** Macros f
469b0 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 or looping over
469c0 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 all elements of
469d0 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 a hash table. T
469e0 68 65 20 69 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c he idiom is.** l
469f0 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 ike this:.**.**
46a00 20 20 48 61 73 68 20 68 3b 0a 2a 2a 20 20 20 48 Hash h;.** H
46a10 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 2a 2a 20 20 ashElem *p;.**
46a20 20 2e 2e 2e 0a 2a 2a 20 20 20 66 6f 72 28 70 3d ....** for(p=
46a30 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 sqliteHashFirst(
46a40 26 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 &h); p; p=sqlite
46a50 48 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 2a 2a HashNext(p)){.**
46a60 20 20 20 20 20 53 6f 6d 65 53 74 72 75 63 74 75 SomeStructu
46a70 72 65 20 2a 70 44 61 74 61 20 3d 20 73 71 6c 69 re *pData = sqli
46a80 74 65 48 61 73 68 44 61 74 61 28 70 29 3b 0a 2a teHashData(p);.*
46a90 2a 20 20 20 20 20 2f 2f 20 64 6f 20 73 6f 6d 65 * // do some
46aa0 74 68 69 6e 67 20 77 69 74 68 20 70 44 61 74 61 thing with pData
46ab0 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65 66 69 .** }.*/.#defi
46ac0 6e 65 20 73 71 6c 69 74 65 48 61 73 68 46 69 72 ne sqliteHashFir
46ad0 73 74 28 48 29 20 20 28 28 48 29 2d 3e 66 69 72 st(H) ((H)->fir
46ae0 73 74 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 st).#define sqli
46af0 74 65 48 61 73 68 4e 65 78 74 28 45 29 20 20 20 teHashNext(E)
46b00 28 28 45 29 2d 3e 6e 65 78 74 29 0a 23 64 65 66 ((E)->next).#def
46b10 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 44 61 ine sqliteHashDa
46b20 74 61 28 45 29 20 20 20 28 28 45 29 2d 3e 64 61 ta(E) ((E)->da
46b30 74 61 29 0a 2f 2a 20 23 64 65 66 69 6e 65 20 73 ta)./* #define s
46b40 71 6c 69 74 65 48 61 73 68 4b 65 79 28 45 29 20 qliteHashKey(E)
46b50 20 20 20 28 28 45 29 2d 3e 70 4b 65 79 29 20 2f ((E)->pKey) /
46b60 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 2f 2a / NOT USED */./*
46b70 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 #define sqliteH
46b80 61 73 68 4b 65 79 73 69 7a 65 28 45 29 20 28 28 ashKeysize(E) ((
46b90 45 29 2d 3e 6e 4b 65 79 29 20 20 2f 2f 20 4e 4f E)->nKey) // NO
46ba0 54 20 55 53 45 44 20 2a 2f 0a 0a 2f 2a 0a 2a 2a T USED */../*.**
46bb0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
46bc0 65 73 20 69 6e 20 61 20 68 61 73 68 20 74 61 62 es in a hash tab
46bd0 6c 65 0a 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65 le.*/./* #define
46be0 20 73 71 6c 69 74 65 48 61 73 68 43 6f 75 6e 74 sqliteHashCount
46bf0 28 48 29 20 20 28 28 48 29 2d 3e 63 6f 75 6e 74 (H) ((H)->count
46c00 29 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f ) // NOT USED */
46c10 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c ..#endif /* _SQL
46c20 49 54 45 5f 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a ITE_HASH_H_ */..
46c30 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
46c40 45 6e 64 20 6f 66 20 68 61 73 68 2e 68 20 2a 2a End of hash.h **
46c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
46c80 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
46c90 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
46ca0 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
46cb0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
46cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
46cd0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
46ce0 49 6e 63 6c 75 64 65 20 70 61 72 73 65 2e 68 20 Include parse.h
46cf0 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
46d00 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
46d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
46d20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
46d30 42 65 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65 Begin file parse
46d40 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
46d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
46d70 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 4d 49 20 #define TK_SEMI
46d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46d90 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 1.#de
46da0 66 69 6e 65 20 54 4b 5f 45 58 50 4c 41 49 4e 20 fine TK_EXPLAIN
46db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46dc0 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 2.#defin
46dd0 65 20 54 4b 5f 51 55 45 52 59 20 20 20 20 20 20 e TK_QUERY
46de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46df0 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 54 3.#define T
46e00 4b 5f 50 4c 41 4e 20 20 20 20 20 20 20 20 20 20 K_PLAN
46e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46e20 20 20 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 4.#define TK_B
46e30 45 47 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 EGIN
46e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
46e50 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 52 41 4e .#define TK_TRAN
46e60 53 41 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 SACTION
46e70 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 6.#d
46e80 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 45 efine TK_DEFERRE
46e90 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 D
46ea0 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 7.#defi
46eb0 6e 65 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45 20 ne TK_IMMEDIATE
46ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46ed0 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 8.#define
46ee0 54 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 TK_EXCLUSIVE
46ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46f00 20 20 20 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 9.#define TK_
46f10 43 4f 4d 4d 49 54 20 20 20 20 20 20 20 20 20 20 COMMIT
46f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
46f30 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e 44 0.#define TK_END
46f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46f50 20 20 20 20 20 20 20 20 20 20 20 20 31 31 0a 23 11.#
46f60 64 65 66 69 6e 65 20 54 4b 5f 52 4f 4c 4c 42 41 define TK_ROLLBA
46f70 43 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 CK
46f80 20 20 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 12.#def
46f90 69 6e 65 20 54 4b 5f 53 41 56 45 50 4f 49 4e 54 ine TK_SAVEPOINT
46fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46fb0 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 13.#define
46fc0 20 54 4b 5f 52 45 4c 45 41 53 45 20 20 20 20 20 TK_RELEASE
46fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46fe0 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20 54 4b 14.#define TK
46ff0 5f 54 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 _TO
47000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47010 31 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 41 15.#define TK_TA
47020 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 BLE
47030 20 20 20 20 20 20 20 20 20 20 20 20 20 31 36 0a 16.
47040 23 64 65 66 69 6e 65 20 54 4b 5f 43 52 45 41 54 #define TK_CREAT
47050 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
47060 20 20 20 20 20 20 20 20 20 20 31 37 0a 23 64 65 17.#de
47070 66 69 6e 65 20 54 4b 5f 49 46 20 20 20 20 20 20 fine TK_IF
47080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47090 20 20 20 20 20 20 20 31 38 0a 23 64 65 66 69 6e 18.#defin
470a0 65 20 54 4b 5f 4e 4f 54 20 20 20 20 20 20 20 20 e TK_NOT
470b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
470c0 20 20 20 20 31 39 0a 23 64 65 66 69 6e 65 20 54 19.#define T
470d0 4b 5f 45 58 49 53 54 53 20 20 20 20 20 20 20 20 K_EXISTS
470e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
470f0 20 32 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 20.#define TK_T
47100 45 4d 50 20 20 20 20 20 20 20 20 20 20 20 20 20 EMP
47110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 31 21
47120 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 50 20 20 .#define TK_LP
47130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47140 20 20 20 20 20 20 20 20 20 20 20 32 32 0a 23 64 22.#d
47150 65 66 69 6e 65 20 54 4b 5f 52 50 20 20 20 20 20 efine TK_RP
47160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47170 20 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 69 23.#defi
47180 6e 65 20 54 4b 5f 41 53 20 20 20 20 20 20 20 20 ne TK_AS
47190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
471a0 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20 24.#define
471b0 54 4b 5f 43 4f 4d 4d 41 20 20 20 20 20 20 20 20 TK_COMMA
471c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
471d0 20 20 32 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 25.#define TK_
471e0 49 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ID
471f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2
47200 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 44 6.#define TK_IND
47210 45 58 45 44 20 20 20 20 20 20 20 20 20 20 20 20 EXED
47220 20 20 20 20 20 20 20 20 20 20 20 20 32 37 0a 23 27.#
47230 64 65 66 69 6e 65 20 54 4b 5f 41 42 4f 52 54 20 define TK_ABORT
47240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47250 20 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 28.#def
47260 69 6e 65 20 54 4b 5f 41 46 54 45 52 20 20 20 20 ine TK_AFTER
47270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47280 20 20 20 20 20 20 32 39 0a 23 64 65 66 69 6e 65 29.#define
47290 20 54 4b 5f 41 4e 41 4c 59 5a 45 20 20 20 20 20 TK_ANALYZE
472a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
472b0 20 20 20 33 30 0a 23 64 65 66 69 6e 65 20 54 4b 30.#define TK
472c0 5f 41 53 43 20 20 20 20 20 20 20 20 20 20 20 20 _ASC
472d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
472e0 33 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 54 31.#define TK_AT
472f0 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 TACH
47300 20 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a 32.
47310 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 46 4f 52 #define TK_BEFOR
47320 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
47330 20 20 20 20 20 20 20 20 20 20 33 33 0a 23 64 65 33.#de
47340 66 69 6e 65 20 54 4b 5f 42 59 20 20 20 20 20 20 fine TK_BY
47350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47360 20 20 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e 34.#defin
47370 65 20 54 4b 5f 43 41 53 43 41 44 45 20 20 20 20 e TK_CASCADE
47380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47390 20 20 20 20 33 35 0a 23 64 65 66 69 6e 65 20 54 35.#define T
473a0 4b 5f 43 41 53 54 20 20 20 20 20 20 20 20 20 20 K_CAST
473b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
473c0 20 33 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 36.#define TK_C
473d0 4f 4c 55 4d 4e 4b 57 20 20 20 20 20 20 20 20 20 OLUMNKW
473e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 37 37
473f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 46 .#define TK_CONF
47400 4c 49 43 54 20 20 20 20 20 20 20 20 20 20 20 20 LICT
47410 20 20 20 20 20 20 20 20 20 20 20 33 38 0a 23 64 38.#d
47420 65 66 69 6e 65 20 54 4b 5f 44 41 54 41 42 41 53 efine TK_DATABAS
47430 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
47440 20 20 20 20 20 20 20 20 33 39 0a 23 64 65 66 69 39.#defi
47450 6e 65 20 54 4b 5f 44 45 53 43 20 20 20 20 20 20 ne TK_DESC
47460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47470 20 20 20 20 20 34 30 0a 23 64 65 66 69 6e 65 20 40.#define
47480 54 4b 5f 44 45 54 41 43 48 20 20 20 20 20 20 20 TK_DETACH
47490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
474a0 20 20 34 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41.#define TK_
474b0 45 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 EACH
474c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
474d0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 41 49 2.#define TK_FAI
474e0 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 L
474f0 20 20 20 20 20 20 20 20 20 20 20 20 34 33 0a 23 43.#
47500 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 20 20 20 define TK_FOR
47510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47520 20 20 20 20 20 20 20 20 20 34 34 0a 23 64 65 66 44.#def
47530 69 6e 65 20 54 4b 5f 49 47 4e 4f 52 45 20 20 20 ine TK_IGNORE
47540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47550 20 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e 65 45.#define
47560 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 20 20 20 TK_INITIALLY
47570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47580 20 20 20 34 36 0a 23 64 65 66 69 6e 65 20 54 4b 46.#define TK
47590 5f 49 4e 53 54 45 41 44 20 20 20 20 20 20 20 20 _INSTEAD
475a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
475b0 34 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 47.#define TK_LI
475c0 4b 45 5f 4b 57 20 20 20 20 20 20 20 20 20 20 20 KE_KW
475d0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 38 0a 48.
475e0 23 64 65 66 69 6e 65 20 54 4b 5f 4d 41 54 43 48 #define TK_MATCH
475f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47600 20 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65 49.#de
47610 66 69 6e 65 20 54 4b 5f 4b 45 59 20 20 20 20 20 fine TK_KEY
47620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47630 20 20 20 20 20 20 20 35 30 0a 23 64 65 66 69 6e 50.#defin
47640 65 20 54 4b 5f 4f 46 20 20 20 20 20 20 20 20 20 e TK_OF
47650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47660 20 20 20 20 35 31 0a 23 64 65 66 69 6e 65 20 54 51.#define T
47670 4b 5f 4f 46 46 53 45 54 20 20 20 20 20 20 20 20 K_OFFSET
47680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47690 20 35 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 52.#define TK_P
476a0 52 41 47 4d 41 20 20 20 20 20 20 20 20 20 20 20 RAGMA
476b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 33 53
476c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 41 49 53 .#define TK_RAIS
476d0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
476e0 20 20 20 20 20 20 20 20 20 20 20 35 34 0a 23 64 54.#d
476f0 65 66 69 6e 65 20 54 4b 5f 52 45 50 4c 41 43 45 efine TK_REPLACE
47700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47710 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 69 55.#defi
47720 6e 65 20 54 4b 5f 52 45 53 54 52 49 43 54 20 20 ne TK_RESTRICT
47730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47740 20 20 20 20 20 35 36 0a 23 64 65 66 69 6e 65 20 56.#define
47750 54 4b 5f 52 4f 57 20 20 20 20 20 20 20 20 20 20 TK_ROW
47760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47770 20 20 35 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 57.#define TK_
47780 54 52 49 47 47 45 52 20 20 20 20 20 20 20 20 20 TRIGGER
47790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
477a0 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 43 8.#define TK_VAC
477b0 55 55 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 UUM
477c0 20 20 20 20 20 20 20 20 20 20 20 20 35 39 0a 23 59.#
477d0 64 65 66 69 6e 65 20 54 4b 5f 56 49 45 57 20 20 define TK_VIEW
477e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
477f0 20 20 20 20 20 20 20 20 20 36 30 0a 23 64 65 66 60.#def
47800 69 6e 65 20 54 4b 5f 56 49 52 54 55 41 4c 20 20 ine TK_VIRTUAL
47810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47820 20 20 20 20 20 20 36 31 0a 23 64 65 66 69 6e 65 61.#define
47830 20 54 4b 5f 52 45 49 4e 44 45 58 20 20 20 20 20 TK_REINDEX
47840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47850 20 20 20 36 32 0a 23 64 65 66 69 6e 65 20 54 4b 62.#define TK
47860 5f 52 45 4e 41 4d 45 20 20 20 20 20 20 20 20 20 _RENAME
47870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47880 36 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 54 63.#define TK_CT
47890 49 4d 45 5f 4b 57 20 20 20 20 20 20 20 20 20 20 IME_KW
478a0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 34 0a 64.
478b0 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 59 20 20 #define TK_ANY
478c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
478d0 20 20 20 20 20 20 20 20 20 20 36 35 0a 23 64 65 65.#de
478e0 66 69 6e 65 20 54 4b 5f 4f 52 20 20 20 20 20 20 fine TK_OR
478f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47900 20 20 20 20 20 20 20 36 36 0a 23 64 65 66 69 6e 66.#defin
47910 65 20 54 4b 5f 41 4e 44 20 20 20 20 20 20 20 20 e TK_AND
47920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47930 20 20 20 20 36 37 0a 23 64 65 66 69 6e 65 20 54 67.#define T
47940 4b 5f 49 53 20 20 20 20 20 20 20 20 20 20 20 20 K_IS
47950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47960 20 36 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 68.#define TK_B
47970 45 54 57 45 45 4e 20 20 20 20 20 20 20 20 20 20 ETWEEN
47980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 39 69
47990 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 20 20 .#define TK_IN
479a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
479b0 20 20 20 20 20 20 20 20 20 20 20 37 30 0a 23 64 70.#d
479c0 65 66 69 6e 65 20 54 4b 5f 49 53 4e 55 4c 4c 20 efine TK_ISNULL
479d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
479e0 20 20 20 20 20 20 20 20 37 31 0a 23 64 65 66 69 71.#defi
479f0 6e 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 ne TK_NOTNULL
47a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a10 20 20 20 20 20 37 32 0a 23 64 65 66 69 6e 65 20 72.#define
47a20 54 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 20 20 TK_NE
47a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a40 20 20 37 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 73.#define TK_
47a50 45 51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 EQ
47a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
47a70 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 54 20 4.#define TK_GT
47a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a90 20 20 20 20 20 20 20 20 20 20 20 20 37 35 0a 23 75.#
47aa0 64 65 66 69 6e 65 20 54 4b 5f 4c 45 20 20 20 20 define TK_LE
47ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ac0 20 20 20 20 20 20 20 20 20 37 36 0a 23 64 65 66 76.#def
47ad0 69 6e 65 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 ine TK_LT
47ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47af0 20 20 20 20 20 20 37 37 0a 23 64 65 66 69 6e 65 77.#define
47b00 20 54 4b 5f 47 45 20 20 20 20 20 20 20 20 20 20 TK_GE
47b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b20 20 20 20 37 38 0a 23 64 65 66 69 6e 65 20 54 4b 78.#define TK
47b30 5f 45 53 43 41 50 45 20 20 20 20 20 20 20 20 20 _ESCAPE
47b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b50 37 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 79.#define TK_BI
47b60 54 41 4e 44 20 20 20 20 20 20 20 20 20 20 20 20 TAND
47b70 20 20 20 20 20 20 20 20 20 20 20 20 20 38 30 0a 80.
47b80 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 4f 52 #define TK_BITOR
47b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ba0 20 20 20 20 20 20 20 20 20 20 38 31 0a 23 64 65 81.#de
47bb0 66 69 6e 65 20 54 4b 5f 4c 53 48 49 46 54 20 20 fine TK_LSHIFT
47bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47bd0 20 20 20 20 20 20 20 38 32 0a 23 64 65 66 69 6e 82.#defin
47be0 65 20 54 4b 5f 52 53 48 49 46 54 20 20 20 20 20 e TK_RSHIFT
47bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c00 20 20 20 20 38 33 0a 23 64 65 66 69 6e 65 20 54 83.#define T
47c10 4b 5f 50 4c 55 53 20 20 20 20 20 20 20 20 20 20 K_PLUS
47c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c30 20 38 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 84.#define TK_M
47c40 49 4e 55 53 20 20 20 20 20 20 20 20 20 20 20 20 INUS
47c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 35 85
47c60 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 41 52 .#define TK_STAR
47c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c80 20 20 20 20 20 20 20 20 20 20 20 38 36 0a 23 64 86.#d
47c90 65 66 69 6e 65 20 54 4b 5f 53 4c 41 53 48 20 20 efine TK_SLASH
47ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47cb0 20 20 20 20 20 20 20 20 38 37 0a 23 64 65 66 69 87.#defi
47cc0 6e 65 20 54 4b 5f 52 45 4d 20 20 20 20 20 20 20 ne TK_REM
47cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ce0 20 20 20 20 20 38 38 0a 23 64 65 66 69 6e 65 20 88.#define
47cf0 54 4b 5f 43 4f 4e 43 41 54 20 20 20 20 20 20 20 TK_CONCAT
47d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d10 20 20 38 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 89.#define TK_
47d20 43 4f 4c 4c 41 54 45 20 20 20 20 20 20 20 20 20 COLLATE
47d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
47d40 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4d 49 0.#define TK_UMI
47d50 4e 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20 NUS
47d60 20 20 20 20 20 20 20 20 20 20 20 20 39 31 0a 23 91.#
47d70 64 65 66 69 6e 65 20 54 4b 5f 55 50 4c 55 53 20 define TK_UPLUS
47d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d90 20 20 20 20 20 20 20 20 20 39 32 0a 23 64 65 66 92.#def
47da0 69 6e 65 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 ine TK_BITNOT
47db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47dc0 20 20 20 20 20 20 39 33 0a 23 64 65 66 69 6e 65 93.#define
47dd0 20 54 4b 5f 53 54 52 49 4e 47 20 20 20 20 20 20 TK_STRING
47de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47df0 20 20 20 39 34 0a 23 64 65 66 69 6e 65 20 54 4b 94.#define TK
47e00 5f 4a 4f 49 4e 5f 4b 57 20 20 20 20 20 20 20 20 _JOIN_KW
47e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e20 39 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 95.#define TK_CO
47e30 4e 53 54 52 41 49 4e 54 20 20 20 20 20 20 20 20 NSTRAINT
47e40 20 20 20 20 20 20 20 20 20 20 20 20 20 39 36 0a 96.
47e50 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 41 55 #define TK_DEFAU
47e60 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 LT
47e70 20 20 20 20 20 20 20 20 20 20 39 37 0a 23 64 65 97.#de
47e80 66 69 6e 65 20 54 4b 5f 4e 55 4c 4c 20 20 20 20 fine TK_NULL
47e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ea0 20 20 20 20 20 20 20 39 38 0a 23 64 65 66 69 6e 98.#defin
47eb0 65 20 54 4b 5f 50 52 49 4d 41 52 59 20 20 20 20 e TK_PRIMARY
47ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ed0 20 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 54 99.#define T
47ee0 4b 5f 55 4e 49 51 55 45 20 20 20 20 20 20 20 20 K_UNIQUE
47ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f00 20 31 30 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 100.#define TK_
47f10 43 48 45 43 4b 20 20 20 20 20 20 20 20 20 20 20 CHECK
47f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
47f30 30 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 01.#define TK_RE
47f40 46 45 52 45 4e 43 45 53 20 20 20 20 20 20 20 20 FERENCES
47f50 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 32 102
47f60 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 55 54 4f .#define TK_AUTO
47f70 49 4e 43 52 20 20 20 20 20 20 20 20 20 20 20 20 INCR
47f80 20 20 20 20 20 20 20 20 20 20 20 31 30 33 0a 23 103.#
47f90 64 65 66 69 6e 65 20 54 4b 5f 4f 4e 20 20 20 20 define TK_ON
47fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47fb0 20 20 20 20 20 20 20 20 20 31 30 34 0a 23 64 65 104.#de
47fc0 66 69 6e 65 20 54 4b 5f 44 45 4c 45 54 45 20 20 fine TK_DELETE
47fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47fe0 20 20 20 20 20 20 20 31 30 35 0a 23 64 65 66 69 105.#defi
47ff0 6e 65 20 54 4b 5f 55 50 44 41 54 45 20 20 20 20 ne TK_UPDATE
48000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48010 20 20 20 20 20 31 30 36 0a 23 64 65 66 69 6e 65 106.#define
48020 20 54 4b 5f 49 4e 53 45 52 54 20 20 20 20 20 20 TK_INSERT
48030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48040 20 20 20 31 30 37 0a 23 64 65 66 69 6e 65 20 54 107.#define T
48050 4b 5f 53 45 54 20 20 20 20 20 20 20 20 20 20 20 K_SET
48060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48070 20 31 30 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 108.#define TK_
48080 44 45 46 45 52 52 41 42 4c 45 20 20 20 20 20 20 DEFERRABLE
48090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
480a0 30 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f 09.#define TK_FO
480b0 52 45 49 47 4e 20 20 20 20 20 20 20 20 20 20 20 REIGN
480c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 30 110
480d0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 52 4f 50 .#define TK_DROP
480e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
480f0 20 20 20 20 20 20 20 20 20 20 20 31 31 31 0a 23 111.#
48100 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 4f 4e 20 define TK_UNION
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 31 31 32 0a 23 64 65 112.#de
48130 66 69 6e 65 20 54 4b 5f 41 4c 4c 20 20 20 20 20 fine TK_ALL
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 31 31 33 0a 23 64 65 66 69 113.#defi
48160 6e 65 20 54 4b 5f 45 58 43 45 50 54 20 20 20 20 ne TK_EXCEPT
48170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48180 20 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65 114.#define
48190 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 20 20 TK_INTERSECT
481a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
481b0 20 20 20 31 31 35 0a 23 64 65 66 69 6e 65 20 54 115.#define T
481c0 4b 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 K_SELECT
481d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
481e0 20 31 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 116.#define TK_
481f0 44 49 53 54 49 4e 43 54 20 20 20 20 20 20 20 20 DISTINCT
48200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48210 31 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 4f 17.#define TK_DO
48220 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
48230 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 38 118
48240 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 52 4f 4d .#define TK_FROM
48250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48260 20 20 20 20 20 20 20 20 20 20 20 31 31 39 0a 23 119.#
48270 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e 20 20 define TK_JOIN
48280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48290 20 20 20 20 20 20 20 20 20 31 32 30 0a 23 64 65 120.#de
482a0 66 69 6e 65 20 54 4b 5f 55 53 49 4e 47 20 20 20 fine TK_USING
482b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
482c0 20 20 20 20 20 20 20 31 32 31 0a 23 64 65 66 69 121.#defi
482d0 6e 65 20 54 4b 5f 4f 52 44 45 52 20 20 20 20 20 ne TK_ORDER
482e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
482f0 20 20 20 20 20 31 32 32 0a 23 64 65 66 69 6e 65 122.#define
48300 20 54 4b 5f 47 52 4f 55 50 20 20 20 20 20 20 20 TK_GROUP
48310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48320 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65 20 54 123.#define T
48330 4b 5f 48 41 56 49 4e 47 20 20 20 20 20 20 20 20 K_HAVING
48340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48350 20 31 32 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 124.#define TK_
48360 4c 49 4d 49 54 20 20 20 20 20 20 20 20 20 20 20 LIMIT
48370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48380 32 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 57 48 25.#define TK_WH
48390 45 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ERE
483a0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 36 126
483b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54 4f .#define TK_INTO
483c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
483d0 20 20 20 20 20 20 20 20 20 20 20 31 32 37 0a 23 127.#
483e0 64 65 66 69 6e 65 20 54 4b 5f 56 41 4c 55 45 53 define TK_VALUES
483f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48400 20 20 20 20 20 20 20 20 20 31 32 38 0a 23 64 65 128.#de
48410 66 69 6e 65 20 54 4b 5f 49 4e 54 45 47 45 52 20 fine TK_INTEGER
48420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48430 20 20 20 20 20 20 20 31 32 39 0a 23 64 65 66 69 129.#defi
48440 6e 65 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20 20 ne TK_FLOAT
48450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48460 20 20 20 20 20 31 33 30 0a 23 64 65 66 69 6e 65 130.#define
48470 20 54 4b 5f 42 4c 4f 42 20 20 20 20 20 20 20 20 TK_BLOB
48480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48490 20 20 20 31 33 31 0a 23 64 65 66 69 6e 65 20 54 131.#define T
484a0 4b 5f 52 45 47 49 53 54 45 52 20 20 20 20 20 20 K_REGISTER
484b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
484c0 20 31 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 132.#define TK_
484d0 56 41 52 49 41 42 4c 45 20 20 20 20 20 20 20 20 VARIABLE
484e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
484f0 33 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 33.#define TK_CA
48500 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 SE
48510 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34 134
48520 0a 23 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 4e .#define TK_WHEN
48530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48540 20 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23 135.#
48550 64 65 66 69 6e 65 20 54 4b 5f 54 48 45 4e 20 20 define TK_THEN
48560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48570 20 20 20 20 20 20 20 20 20 31 33 36 0a 23 64 65 136.#de
48580 66 69 6e 65 20 54 4b 5f 45 4c 53 45 20 20 20 20 fine TK_ELSE
48590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
485a0 20 20 20 20 20 20 20 31 33 37 0a 23 64 65 66 69 137.#defi
485b0 6e 65 20 54 4b 5f 49 4e 44 45 58 20 20 20 20 20 ne TK_INDEX
485c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
485d0 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e 65 138.#define
485e0 20 54 4b 5f 41 4c 54 45 52 20 20 20 20 20 20 20 TK_ALTER
485f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48600 20 20 20 31 33 39 0a 23 64 65 66 69 6e 65 20 54 139.#define T
48610 4b 5f 41 44 44 20 20 20 20 20 20 20 20 20 20 20 K_ADD
48620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48630 20 31 34 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 140.#define TK_
48640 54 4f 5f 54 45 58 54 20 20 20 20 20 20 20 20 20 TO_TEXT
48650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48660 34 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 41.#define TK_TO
48670 5f 42 4c 4f 42 20 20 20 20 20 20 20 20 20 20 20 _BLOB
48680 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 32 142
48690 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 4e .#define TK_TO_N
486a0 55 4d 45 52 49 43 20 20 20 20 20 20 20 20 20 20 UMERIC
486b0 20 20 20 20 20 20 20 20 20 20 20 31 34 33 0a 23 143.#
486c0 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 49 4e 54 define TK_TO_INT
486d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
486e0 20 20 20 20 20 20 20 20 20 31 34 34 0a 23 64 65 144.#de
486f0 66 69 6e 65 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 fine TK_TO_REAL
48700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48710 20 20 20 20 20 20 20 31 34 35 0a 23 64 65 66 69 145.#defi
48720 6e 65 20 54 4b 5f 45 4e 44 5f 4f 46 5f 46 49 4c ne TK_END_OF_FIL
48730 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
48740 20 20 20 20 20 31 34 36 0a 23 64 65 66 69 6e 65 146.#define
48750 20 54 4b 5f 49 4c 4c 45 47 41 4c 20 20 20 20 20 TK_ILLEGAL
48760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48770 20 20 20 31 34 37 0a 23 64 65 66 69 6e 65 20 54 147.#define T
48780 4b 5f 53 50 41 43 45 20 20 20 20 20 20 20 20 20 K_SPACE
48790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
487a0 20 31 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 148.#define TK_
487b0 55 4e 43 4c 4f 53 45 44 5f 53 54 52 49 4e 47 20 UNCLOSED_STRING
487c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
487d0 34 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 55 49.#define TK_FU
487e0 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 NCTION
487f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 30 150
48800 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 .#define TK_COLU
48810 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 MN
48820 20 20 20 20 20 20 20 20 20 20 20 31 35 31 0a 23 151.#
48830 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 5f 46 55 define TK_AGG_FU
48840 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 NCTION
48850 20 20 20 20 20 20 20 20 20 31 35 32 0a 23 64 65 152.#de
48860 66 69 6e 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 fine TK_AGG_COLU
48870 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 MN
48880 20 20 20 20 20 20 20 31 35 33 0a 23 64 65 66 69 153.#defi
48890 6e 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 ne TK_CONST_FUNC
488a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
488b0 20 20 20 20 20 31 35 34 0a 0a 2f 2a 2a 2a 2a 2a 154../*****
488c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
488d0 20 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a parse.h *******
488e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
488f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48900 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
48910 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
48920 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
48930 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 ft off in sqlite
48940 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
48950 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 6e 63 6c 75 ********/.#inclu
48960 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 23 69 6e de <stdio.h>.#in
48970 63 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e 68 3e clude <stdlib.h>
48980 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e .#include <strin
48990 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 61 g.h>.#include <a
489a0 73 73 65 72 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 ssert.h>.#includ
489b0 65 20 3c 73 74 64 64 65 66 2e 68 3e 0a 0a 2f 2a e <stddef.h>../*
489c0 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 69 6e 67 .** If compiling
489d0 20 66 6f 72 20 61 20 70 72 6f 63 65 73 73 6f 72 for a processor
489e0 20 74 68 61 74 20 6c 61 63 6b 73 20 66 6c 6f 61 that lacks floa
489f0 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f ting point suppo
48a00 72 74 2c 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 rt,.** substitut
48a10 65 20 69 6e 74 65 67 65 72 20 66 6f 72 20 66 6c e integer for fl
48a20 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 0a 2a 2f 0a oating-point.*/.
48a30 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
48a40 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
48a50 54 0a 23 20 64 65 66 69 6e 65 20 64 6f 75 62 6c T.# define doubl
48a60 65 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 e sqlite_int64.#
48a70 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 define LONGDOUB
48a80 4c 45 5f 54 59 50 45 20 73 71 6c 69 74 65 5f 69 LE_TYPE sqlite_i
48a90 6e 74 36 34 0a 23 20 69 66 6e 64 65 66 20 53 51 nt64.# ifndef SQ
48aa0 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 20 LITE_BIG_DBL.#
48ab0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 define SQLITE_B
48ac0 49 47 5f 44 42 4c 20 28 28 28 73 71 6c 69 74 65 IG_DBL (((sqlite
48ad0 33 5f 69 6e 74 36 34 29 31 29 3c 3c 35 30 29 0a 3_int64)1)<<50).
48ae0 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 # endif.# define
48af0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 SQLITE_OMIT_DAT
48b00 45 54 49 4d 45 5f 46 55 4e 43 53 20 31 0a 23 20 ETIME_FUNCS 1.#
48b10 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d define SQLITE_OM
48b20 49 54 5f 54 52 41 43 45 20 31 0a 23 20 75 6e 64 IT_TRACE 1.# und
48b30 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f ef SQLITE_MIXED_
48b40 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f ENDIAN_64BIT_FLO
48b50 41 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 AT.# undef SQLIT
48b60 45 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 65 6e E_HAVE_ISNAN.#en
48b70 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 dif.#ifndef SQLI
48b80 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 64 65 66 TE_BIG_DBL.# def
48b90 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 ine SQLITE_BIG_D
48ba0 42 4c 20 28 31 65 39 39 29 0a 23 65 6e 64 69 66 BL (1e99).#endif
48bb0 0a 0a 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d ../*.** OMIT_TEM
48bc0 50 44 42 20 69 73 20 73 65 74 20 74 6f 20 31 20 PDB is set to 1
48bd0 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 if SQLITE_OMIT_T
48be0 45 4d 50 44 42 20 69 73 20 64 65 66 69 6e 65 64 EMPDB is defined
48bf0 2c 20 6f 72 20 30 0a 2a 2a 20 61 66 74 65 72 77 , or 0.** afterw
48c00 61 72 64 2e 20 48 61 76 69 6e 67 20 74 68 69 73 ard. Having this
48c10 20 6d 61 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73 macro allows us
48c20 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 43 20 to cause the C
48c30 63 6f 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20 compiler .** to
48c40 6f 6d 69 74 20 63 6f 64 65 20 75 73 65 64 20 62 omit code used b
48c50 79 20 54 45 4d 50 20 74 61 62 6c 65 73 20 77 69 y TEMP tables wi
48c60 74 68 6f 75 74 20 6d 65 73 73 79 20 23 69 66 6e thout messy #ifn
48c70 64 65 66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a def statements..
48c80 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
48c90 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 23 64 65 _OMIT_TEMPDB.#de
48ca0 66 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42 fine OMIT_TEMPDB
48cb0 20 31 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 1.#else.#define
48cc0 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 30 0a 23 OMIT_TEMPDB 0.#
48cd0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 endif../*.** If
48ce0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 the following ma
48cf0 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20 31 2c cro is set to 1,
48d00 20 74 68 65 6e 20 4e 55 4c 4c 20 76 61 6c 75 65 then NULL value
48d10 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 s are considered
48d20 0a 2a 2a 20 64 69 73 74 69 6e 63 74 20 77 68 65 .** distinct whe
48d30 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 n determining wh
48d40 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 77 6f ether or not two
48d50 20 65 6e 74 72 69 65 73 20 61 72 65 20 74 68 65 entries are the
48d60 20 73 61 6d 65 0a 2a 2a 20 69 6e 20 61 20 55 4e same.** in a UN
48d70 49 51 55 45 20 69 6e 64 65 78 2e 20 20 54 68 69 IQUE index. Thi
48d80 73 20 69 73 20 74 68 65 20 77 61 79 20 50 6f 73 s is the way Pos
48d90 74 67 72 65 53 51 4c 2c 20 4f 72 61 63 6c 65 2c tgreSQL, Oracle,
48da0 20 44 42 32 2c 20 4d 79 53 51 4c 2c 0a 2a 2a 20 DB2, MySQL,.**
48db0 4f 43 45 4c 4f 54 2c 20 61 6e 64 20 46 69 72 65 OCELOT, and Fire
48dc0 62 69 72 64 20 61 6c 6c 20 77 6f 72 6b 2e 20 20 bird all work.
48dd0 54 68 65 20 53 51 4c 39 32 20 73 70 65 63 20 65 The SQL92 spec e
48de0 78 70 6c 69 63 69 74 6c 79 20 73 61 79 73 20 74 xplicitly says t
48df0 68 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 77 61 his.** is the wa
48e00 79 20 74 68 69 6e 67 73 20 61 72 65 20 73 75 70 y things are sup
48e10 70 6f 73 65 20 74 6f 20 77 6f 72 6b 2e 0a 2a 2a pose to work..**
48e20 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f .** If the follo
48e30 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65 wing macro is se
48e40 74 20 74 6f 20 30 2c 20 74 68 65 20 4e 55 4c 4c t to 0, the NULL
48e50 73 20 61 72 65 20 69 6e 64 69 73 74 69 6e 63 74 s are indistinct
48e60 20 66 6f 72 0a 2a 2a 20 61 20 55 4e 49 51 55 45 for.** a UNIQUE
48e70 20 69 6e 64 65 78 2e 20 20 49 6e 20 74 68 69 73 index. In this
48e80 20 6d 6f 64 65 2c 20 79 6f 75 20 63 61 6e 20 6f mode, you can o
48e90 6e 6c 79 20 68 61 76 65 20 61 20 73 69 6e 67 6c nly have a singl
48ea0 65 20 4e 55 4c 4c 20 65 6e 74 72 79 0a 2a 2a 20 e NULL entry.**
48eb0 66 6f 72 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63 for a column dec
48ec0 6c 61 72 65 64 20 55 4e 49 51 55 45 2e 20 20 54 lared UNIQUE. T
48ed0 68 69 73 20 69 73 20 74 68 65 20 77 61 79 20 49 his is the way I
48ee0 6e 66 6f 72 6d 69 78 20 61 6e 64 20 53 51 4c 20 nformix and SQL
48ef0 53 65 72 76 65 72 0a 2a 2a 20 77 6f 72 6b 2e 0a Server.** work..
48f00 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 55 4c 4c 5f */.#define NULL_
48f10 44 49 53 54 49 4e 43 54 5f 46 4f 52 5f 55 4e 49 DISTINCT_FOR_UNI
48f20 51 55 45 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 QUE 1../*.** The
48f30 20 22 66 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e "file format" n
48f40 75 6d 62 65 72 20 69 73 20 61 6e 20 69 6e 74 65 umber is an inte
48f50 67 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 ger that is incr
48f60 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 emented whenever
48f70 0a 2a 2a 20 74 68 65 20 56 44 42 45 2d 6c 65 76 .** the VDBE-lev
48f80 65 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63 el file format c
48f90 68 61 6e 67 65 73 2e 20 20 54 68 65 20 66 6f 6c hanges. The fol
48fa0 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65 lowing macros de
48fb0 66 69 6e 65 20 74 68 65 0a 2a 2a 20 74 68 65 20 fine the.** the
48fc0 64 65 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72 default file for
48fd0 6d 61 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61 mat for new data
48fe0 62 61 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61 bases and the ma
48ff0 78 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 ximum file forma
49000 74 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69 t.** that the li
49010 62 72 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0a brary can read..
49020 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
49030 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 E_MAX_FILE_FORMA
49040 54 20 34 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 T 4.#ifndef SQLI
49050 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f TE_DEFAULT_FILE_
49060 46 4f 52 4d 41 54 0a 23 20 64 65 66 69 6e 65 20 FORMAT.# define
49070 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 SQLITE_DEFAULT_F
49080 49 4c 45 5f 46 4f 52 4d 41 54 20 31 0a 23 65 6e ILE_FORMAT 1.#en
49090 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
490a0 49 54 45 5f 44 45 46 41 55 4c 54 5f 52 45 43 55 ITE_DEFAULT_RECU
490b0 52 53 49 56 45 5f 54 52 49 47 47 45 52 53 0a 23 RSIVE_TRIGGERS.#
490c0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 define SQLITE_D
490d0 45 46 41 55 4c 54 5f 52 45 43 55 52 53 49 56 45 EFAULT_RECURSIVE
490e0 5f 54 52 49 47 47 45 52 53 20 30 0a 23 65 6e 64 _TRIGGERS 0.#end
490f0 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 69 64 if../*.** Provid
49100 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 e a default valu
49110 65 20 66 6f 72 20 53 51 4c 49 54 45 5f 54 45 4d e for SQLITE_TEM
49120 50 5f 53 54 4f 52 45 20 69 6e 20 63 61 73 65 20 P_STORE in case
49130 69 74 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66 it is not specif
49140 69 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f ied.** on the co
49150 6d 6d 61 6e 64 2d 6c 69 6e 65 0a 2a 2f 0a 23 69 mmand-line.*/.#i
49160 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d fndef SQLITE_TEM
49170 50 5f 53 54 4f 52 45 0a 23 20 64 65 66 69 6e 65 P_STORE.# define
49180 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f SQLITE_TEMP_STO
49190 52 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a RE 1.#endif../*.
491a0 2a 2a 20 47 43 43 20 64 6f 65 73 20 6e 6f 74 20 ** GCC does not
491b0 64 65 66 69 6e 65 20 74 68 65 20 6f 66 66 73 65 define the offse
491c0 74 6f 66 28 29 20 6d 61 63 72 6f 20 73 6f 20 77 tof() macro so w
491d0 65 27 6c 6c 20 68 61 76 65 20 74 6f 20 64 6f 20 e'll have to do
491e0 69 74 0a 2a 2a 20 6f 75 72 73 65 6c 76 65 73 2e it.** ourselves.
491f0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 6f 66 66 73 .*/.#ifndef offs
49200 65 74 6f 66 0a 23 64 65 66 69 6e 65 20 6f 66 66 etof.#define off
49210 73 65 74 6f 66 28 53 54 52 55 43 54 55 52 45 2c setof(STRUCTURE,
49220 46 49 45 4c 44 29 20 28 28 69 6e 74 29 28 28 63 FIELD) ((int)((c
49230 68 61 72 2a 29 26 28 28 53 54 52 55 43 54 55 52 har*)&((STRUCTUR
49240 45 2a 29 30 29 2d 3e 46 49 45 4c 44 29 29 0a 23 E*)0)->FIELD)).#
49250 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 endif../*.** Che
49260 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69 ck to see if thi
49270 73 20 6d 61 63 68 69 6e 65 20 75 73 65 73 20 45 s machine uses E
49280 42 43 44 49 43 2e 20 20 28 59 65 73 2c 20 62 65 BCDIC. (Yes, be
49290 6c 69 65 76 65 20 69 74 20 6f 72 0a 2a 2a 20 6e lieve it or.** n
492a0 6f 74 2c 20 74 68 65 72 65 20 61 72 65 20 73 74 ot, there are st
492b0 69 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f 75 74 ill machines out
492c0 20 74 68 65 72 65 20 74 68 61 74 20 75 73 65 20 there that use
492d0 45 42 43 44 49 43 2e 29 0a 2a 2f 0a 23 69 66 20 EBCDIC.).*/.#if
492e0 27 41 27 20 3d 3d 20 27 5c 33 30 31 27 0a 23 20 'A' == '\301'.#
492f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 42 define SQLITE_EB
49300 43 44 49 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 CDIC 1.#else.# d
49310 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 53 43 efine SQLITE_ASC
49320 49 49 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a II 1.#endif../*.
49330 2a 2a 20 49 6e 74 65 67 65 72 73 20 6f 66 20 6b ** Integers of k
49340 6e 6f 77 6e 20 73 69 7a 65 73 2e 20 20 54 68 65 nown sizes. The
49350 73 65 20 74 79 70 65 64 65 66 73 20 6d 69 67 68 se typedefs migh
49360 74 20 63 68 61 6e 67 65 20 66 6f 72 20 61 72 63 t change for arc
49370 68 69 74 65 63 74 75 72 65 73 0a 2a 2a 20 77 68 hitectures.** wh
49380 65 72 65 20 74 68 65 20 73 69 7a 65 73 20 76 65 ere the sizes ve
49390 72 79 2e 20 20 50 72 65 70 72 6f 63 65 73 73 6f ry. Preprocesso
493a0 72 20 6d 61 63 72 6f 73 20 61 72 65 20 61 76 61 r macros are ava
493b0 69 6c 61 62 6c 65 20 73 6f 20 74 68 61 74 20 74 ilable so that t
493c0 68 65 0a 2a 2a 20 74 79 70 65 73 20 63 61 6e 20 he.** types can
493d0 62 65 20 63 6f 6e 76 65 6e 69 65 6e 74 6c 79 20 be conveniently
493e0 72 65 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d redefined at com
493f0 70 69 6c 65 2d 74 79 70 65 2e 20 20 4c 69 6b 65 pile-type. Like
49400 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 this:.**.**
49410 20 20 20 20 20 63 63 20 27 2d 44 55 49 4e 54 50 cc '-DUINTP
49420 54 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c 6f 6e TR_TYPE=long lon
49430 67 20 69 6e 74 27 20 2e 2e 2e 0a 2a 2f 0a 23 69 g int' ....*/.#i
49440 66 6e 64 65 66 20 55 49 4e 54 33 32 5f 54 59 50 fndef UINT32_TYP
49450 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 55 E.# ifdef HAVE_U
49460 49 4e 54 33 32 5f 54 0a 23 20 20 64 65 66 69 6e INT32_T.# defin
49470 65 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 69 e UINT32_TYPE ui
49480 6e 74 33 32 5f 74 0a 23 20 65 6c 73 65 0a 23 20 nt32_t.# else.#
49490 20 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54 define UINT32_T
494a0 59 50 45 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 YPE unsigned int
494b0 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a .# endif.#endif.
494c0 23 69 66 6e 64 65 66 20 55 49 4e 54 31 36 5f 54 #ifndef UINT16_T
494d0 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 YPE.# ifdef HAVE
494e0 5f 55 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 _UINT16_T.# def
494f0 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20 ine UINT16_TYPE
49500 75 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a uint16_t.# else.
49510 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 31 36 # define UINT16
49520 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73 _TYPE unsigned s
49530 68 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 66 hort int.# endif
49540 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
49550 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 66 64 INT16_TYPE.# ifd
49560 65 66 20 48 41 56 45 5f 49 4e 54 31 36 5f 54 0a ef HAVE_INT16_T.
49570 23 20 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f # define INT16_
49580 54 59 50 45 20 69 6e 74 31 36 5f 74 0a 23 20 65 TYPE int16_t.# e
49590 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e lse.# define IN
495a0 54 31 36 5f 54 59 50 45 20 73 68 6f 72 74 20 69 T16_TYPE short i
495b0 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 nt.# endif.#endi
495c0 66 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 38 5f f.#ifndef UINT8_
495d0 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 TYPE.# ifdef HAV
495e0 45 5f 55 49 4e 54 38 5f 54 0a 23 20 20 64 65 66 E_UINT8_T.# def
495f0 69 6e 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 ine UINT8_TYPE u
49600 69 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 20 int8_t.# else.#
49610 20 64 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59 define UINT8_TY
49620 50 45 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 PE unsigned char
49630 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a .# endif.#endif.
49640 23 69 66 6e 64 65 66 20 49 4e 54 38 5f 54 59 50 #ifndef INT8_TYP
49650 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 49 E.# ifdef HAVE_I
49660 4e 54 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 NT8_T.# define
49670 49 4e 54 38 5f 54 59 50 45 20 69 6e 74 38 5f 74 INT8_TYPE int8_t
49680 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e .# else.# defin
49690 65 20 49 4e 54 38 5f 54 59 50 45 20 73 69 67 6e e INT8_TYPE sign
496a0 65 64 20 63 68 61 72 0a 23 20 65 6e 64 69 66 0a ed char.# endif.
496b0 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4c #endif.#ifndef L
496c0 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 0a 23 ONGDOUBLE_TYPE.#
496d0 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 define LONGDOUB
496e0 4c 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64 6f 75 LE_TYPE long dou
496f0 62 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 ble.#endif.typed
49700 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 ef sqlite_int64
49710 69 36 34 3b 20 20 20 20 20 20 20 20 20 20 2f 2a i64; /*
49720 20 38 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 8-byte signed i
49730 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 nteger */.typede
49740 66 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 f sqlite_uint64
49750 75 36 34 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 u64; /*
49760 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 8-byte unsigned
49770 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 integer */.typed
49780 65 66 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 ef UINT32_TYPE u
49790 33 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 32; /*
497a0 20 34 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 4-byte unsigned
497b0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 integer */.type
497c0 64 65 66 20 55 49 4e 54 31 36 5f 54 59 50 45 20 def UINT16_TYPE
497d0 75 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 2f u16; /
497e0 2a 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 * 2-byte unsigne
497f0 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 d integer */.typ
49800 65 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45 20 edef INT16_TYPE
49810 69 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 i16;
49820 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 /* 2-byte signed
49830 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 integer */.type
49840 64 65 66 20 55 49 4e 54 38 5f 54 59 50 45 20 75 def UINT8_TYPE u
49850 38 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 8; /
49860 2a 20 31 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 * 1-byte unsigne
49870 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 d integer */.typ
49880 65 64 65 66 20 49 4e 54 38 5f 54 59 50 45 20 69 edef INT8_TYPE i
49890 38 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 8;
498a0 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 /* 1-byte signed
498b0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f 2a 0a integer */../*.
498c0 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 ** SQLITE_MAX_U3
498d0 32 20 69 73 20 61 20 75 36 34 20 63 6f 6e 73 74 2 is a u64 const
498e0 61 6e 74 20 74 68 61 74 20 69 73 20 74 68 65 20 ant that is the
498f0 6d 61 78 69 6d 75 6d 20 75 36 34 20 76 61 6c 75 maximum u64 valu
49900 65 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 e.** that can be
49910 20 73 74 6f 72 65 64 20 69 6e 20 61 20 75 33 32 stored in a u32
49920 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 without loss of
49930 20 64 61 74 61 2e 20 20 54 68 65 20 76 61 6c 75 data. The valu
49940 65 0a 2a 2a 20 69 73 20 30 78 30 30 30 30 30 30 e.** is 0x000000
49950 30 30 66 66 66 66 66 66 66 66 2e 20 20 42 75 74 00ffffffff. But
49960 20 62 65 63 61 75 73 65 20 6f 66 20 71 75 69 72 because of quir
49970 6b 73 20 6f 66 20 73 6f 6d 65 20 63 6f 6d 70 69 ks of some compi
49980 6c 65 72 73 2c 20 77 65 0a 2a 2a 20 68 61 76 65 lers, we.** have
49990 20 74 6f 20 73 70 65 63 69 66 79 20 74 68 65 20 to specify the
499a0 76 61 6c 75 65 20 69 6e 20 74 68 65 20 6c 65 73 value in the les
499b0 73 20 69 6e 74 75 69 74 69 76 65 20 6d 61 6e 6e s intuitive mann
499c0 65 72 20 73 68 6f 77 6e 3a 0a 2a 2f 0a 23 64 65 er shown:.*/.#de
499d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
499e0 55 33 32 20 20 28 28 28 28 75 36 34 29 31 29 3c U32 ((((u64)1)<
499f0 3c 33 32 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 4d <32)-1)../*.** M
49a00 61 63 72 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 acros to determi
49a10 6e 65 20 77 68 65 74 68 65 72 20 74 68 65 20 6d ne whether the m
49a20 61 63 68 69 6e 65 20 69 73 20 62 69 67 20 6f 72 achine is big or
49a30 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c 0a little endian,.
49a40 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 61 74 20 ** evaluated at
49a50 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 runtime..*/.#ifd
49a60 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 ef SQLITE_AMALGA
49a70 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 MATION.SQLITE_PR
49a80 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 IVATE const int
49a90 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a sqlite3one = 1;.
49aa0 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 #else.SQLITE_PRI
49ab0 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 VATE const int s
49ac0 71 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 6e 64 69 qlite3one;.#endi
49ad0 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 69 33 f.#if defined(i3
49ae0 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 86) || defined(_
49af0 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 _i386__) || defi
49b00 6e 65 64 28 5f 4d 5f 49 58 38 36 29 5c 0a 20 20 ned(_M_IX86)\.
49b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
49b20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 || de
49b30 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 29 20 fined(__x86_64)
49b40 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 || defined(__x86
49b50 5f 36 34 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 _64__).# define
49b60 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e SQLITE_BIGENDIAN
49b70 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 53 0.# define S
49b80 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 QLITE_LITTLEENDI
49b90 41 4e 20 31 0a 23 20 64 65 66 69 6e 65 20 53 51 AN 1.# define SQ
49ba0 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 LITE_UTF16NATIVE
49bb0 20 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 SQLITE_UTF16LE
49bc0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
49bd0 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e SQLITE_BIGENDIAN
49be0 20 20 20 20 28 2a 28 63 68 61 72 20 2a 29 28 26 (*(char *)(&
49bf0 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 30 29 0a sqlite3one)==0).
49c00 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
49c10 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 28 2a 28 LITTLEENDIAN (*(
49c20 63 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65 33 char *)(&sqlite3
49c30 6f 6e 65 29 3d 3d 31 29 0a 23 20 64 65 66 69 6e one)==1).# defin
49c40 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 e SQLITE_UTF16NA
49c50 54 49 56 45 20 28 53 51 4c 49 54 45 5f 42 49 47 TIVE (SQLITE_BIG
49c60 45 4e 44 49 41 4e 3f 53 51 4c 49 54 45 5f 55 54 ENDIAN?SQLITE_UT
49c70 46 31 36 42 45 3a 53 51 4c 49 54 45 5f 55 54 46 F16BE:SQLITE_UTF
49c80 31 36 4c 45 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 16LE).#endif../*
49c90 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 20 66 6f .** Constants fo
49ca0 72 20 74 68 65 20 6c 61 72 67 65 73 74 20 61 6e r the largest an
49cb0 64 20 73 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 d smallest possi
49cc0 62 6c 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 ble 64-bit signe
49cd0 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 d integers..** T
49ce0 68 65 73 65 20 6d 61 63 72 6f 73 20 61 72 65 20 hese macros are
49cf0 64 65 73 69 67 6e 65 64 20 74 6f 20 77 6f 72 6b designed to work
49d00 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 62 6f correctly on bo
49d10 74 68 20 33 32 2d 62 69 74 20 61 6e 64 20 36 34 th 32-bit and 64
49d20 2d 62 69 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 -bit.** compiler
49d30 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 41 s..*/.#define LA
49d40 52 47 45 53 54 5f 49 4e 54 36 34 20 20 28 30 78 RGEST_INT64 (0x
49d50 66 66 66 66 66 66 66 66 7c 28 28 28 69 36 34 29 ffffffff|(((i64)
49d60 30 78 37 66 66 66 66 66 66 66 29 3c 3c 33 32 29 0x7fffffff)<<32)
49d70 29 0a 23 64 65 66 69 6e 65 20 53 4d 41 4c 4c 45 ).#define SMALLE
49d80 53 54 5f 49 4e 54 36 34 20 28 28 28 69 36 34 29 ST_INT64 (((i64)
49d90 2d 31 29 20 2d 20 4c 41 52 47 45 53 54 5f 49 4e -1) - LARGEST_IN
49da0 54 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20 52 6f 75 T64)../* .** Rou
49db0 6e 64 20 75 70 20 61 20 6e 75 6d 62 65 72 20 74 nd up a number t
49dc0 6f 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67 65 o the next large
49dd0 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2e r multiple of 8.
49de0 20 20 54 68 69 73 20 69 73 20 75 73 65 64 0a 2a This is used.*
49df0 2a 20 74 6f 20 66 6f 72 63 65 20 38 2d 62 79 74 * to force 8-byt
49e00 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 6e 20 36 e alignment on 6
49e10 34 2d 62 69 74 20 61 72 63 68 69 74 65 63 74 75 4-bit architectu
49e20 72 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 res..*/.#define
49e30 52 4f 55 4e 44 38 28 78 29 20 20 20 20 20 28 28 ROUND8(x) ((
49e40 28 78 29 2b 37 29 26 7e 37 29 0a 0a 2f 2a 0a 2a (x)+7)&~7)../*.*
49e50 2a 20 52 6f 75 6e 64 20 64 6f 77 6e 20 74 6f 20 * Round down to
49e60 74 68 65 20 6e 65 61 72 65 73 74 20 6d 75 6c 74 the nearest mult
49e70 69 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a 23 64 65 iple of 8.*/.#de
49e80 66 69 6e 65 20 52 4f 55 4e 44 44 4f 57 4e 38 28 fine ROUNDDOWN8(
49e90 78 29 20 28 28 78 29 26 7e 37 29 0a 0a 2f 2a 0a x) ((x)&~7)../*.
49ea0 2a 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 ** Assert that t
49eb0 68 65 20 70 6f 69 6e 74 65 72 20 58 20 69 73 20 he pointer X is
49ec0 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d aligned to an 8-
49ed0 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 0a 2a byte boundary..*
49ee0 2f 0a 23 64 65 66 69 6e 65 20 45 49 47 48 54 5f /.#define EIGHT_
49ef0 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 58 BYTE_ALIGNMENT(X
49f00 29 20 20 20 28 28 28 28 63 68 61 72 2a 29 28 58 ) ((((char*)(X
49f10 29 20 2d 20 28 63 68 61 72 2a 29 30 29 26 37 29 ) - (char*)0)&7)
49f20 3d 3d 30 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 ==0).../*.** An
49f30 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
49f40 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
49f50 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 73 ure is used to s
49f60 74 6f 72 65 20 74 68 65 20 62 75 73 79 2d 68 61 tore the busy-ha
49f70 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 ndler.** callbac
49f80 6b 20 66 6f 72 20 61 20 67 69 76 65 6e 20 73 71 k for a given sq
49f90 6c 69 74 65 20 68 61 6e 64 6c 65 2e 20 0a 2a 2a lite handle. .**
49fa0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 62 .** The sqlite.b
49fb0 75 73 79 48 61 6e 64 6c 65 72 20 6d 65 6d 62 65 usyHandler membe
49fc0 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 20 r of the sqlite
49fd0 73 74 72 75 63 74 20 63 6f 6e 74 61 69 6e 73 20 struct contains
49fe0 74 68 65 20 62 75 73 79 0a 2a 2a 20 63 61 6c 6c the busy.** call
49ff0 62 61 63 6b 20 66 6f 72 20 74 68 65 20 64 61 74 back for the dat
4a000 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 45 61 abase handle. Ea
4a010 63 68 20 70 61 67 65 72 20 6f 70 65 6e 65 64 20 ch pager opened
4a020 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 0a 2a via the sqlite.*
4a030 2a 20 68 61 6e 64 6c 65 20 69 73 20 70 61 73 73 * handle is pass
4a040 65 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ed a pointer to
4a050 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 6c sqlite.busyHandl
4a060 65 72 2e 20 54 68 65 20 62 75 73 79 2d 68 61 6e er. The busy-han
4a070 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b dler.** callback
4a080 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e is currently in
4a090 76 6f 6b 65 64 20 6f 6e 6c 79 20 66 72 6f 6d 20 voked only from
4a0a0 77 69 74 68 69 6e 20 70 61 67 65 72 2e 63 2e 0a within pager.c..
4a0b0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
4a0c0 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20 42 75 t BusyHandler Bu
4a0d0 73 79 48 61 6e 64 6c 65 72 3b 0a 73 74 72 75 63 syHandler;.struc
4a0e0 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20 7b 0a t BusyHandler {.
4a0f0 20 20 69 6e 74 20 28 2a 78 46 75 6e 63 29 28 76 int (*xFunc)(v
4a100 6f 69 64 20 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 oid *,int); /*
4a110 54 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 The busy callbac
4a120 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 k */. void *pAr
4a130 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
4a140 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 /* First arg t
4a150 6f 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 o busy callback
4a160 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 73 79 3b 20 */. int nBusy;
4a170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4a180 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 65 64 20 77 /* Incremented w
4a190 69 74 68 20 65 61 63 68 20 62 75 73 79 20 63 61 ith each busy ca
4a1a0 6c 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ll */.};../*.**
4a1b0 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 61 73 74 Name of the mast
4a1c0 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c er database tabl
4a1d0 65 2e 20 20 54 68 65 20 6d 61 73 74 65 72 20 64 e. The master d
4a1e0 61 74 61 62 61 73 65 20 74 61 62 6c 65 0a 2a 2a atabase table.**
4a1f0 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 74 61 is a special ta
4a200 62 6c 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74 ble that holds t
4a210 68 65 20 6e 61 6d 65 73 20 61 6e 64 20 61 74 74 he names and att
4a220 72 69 62 75 74 65 73 20 6f 66 20 61 6c 6c 0a 2a ributes of all.*
4a230 2a 20 75 73 65 72 20 74 61 62 6c 65 73 20 61 6e * user tables an
4a240 64 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 23 64 d indices..*/.#d
4a250 65 66 69 6e 65 20 4d 41 53 54 45 52 5f 4e 41 4d efine MASTER_NAM
4a260 45 20 20 20 20 20 20 20 22 73 71 6c 69 74 65 5f E "sqlite_
4a270 6d 61 73 74 65 72 22 0a 23 64 65 66 69 6e 65 20 master".#define
4a280 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 4d 45 TEMP_MASTER_NAME
4a290 20 20 22 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d "sqlite_temp_m
4a2a0 61 73 74 65 72 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 aster"../*.** Th
4a2b0 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 74 e root-page of t
4a2c0 68 65 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 he master databa
4a2d0 73 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 se table..*/.#de
4a2e0 66 69 6e 65 20 4d 41 53 54 45 52 5f 52 4f 4f 54 fine MASTER_ROOT
4a2f0 20 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 1../*.**
4a300 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 The name of the
4a310 73 63 68 65 6d 61 20 74 61 62 6c 65 2e 0a 2a 2f schema table..*/
4a320 0a 23 64 65 66 69 6e 65 20 53 43 48 45 4d 41 5f .#define SCHEMA_
4a330 54 41 42 4c 45 28 78 29 20 20 28 28 21 4f 4d 49 TABLE(x) ((!OMI
4a340 54 5f 54 45 4d 50 44 42 29 26 26 28 78 3d 3d 31 T_TEMPDB)&&(x==1
4a350 29 3f 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 )?TEMP_MASTER_NA
4a360 4d 45 3a 4d 41 53 54 45 52 5f 4e 41 4d 45 29 0a ME:MASTER_NAME).
4a370 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6e 76 65 6e 69 ./*.** A conveni
4a380 65 6e 63 65 20 6d 61 63 72 6f 20 74 68 61 74 20 ence macro that
4a390 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 returns the numb
4a3a0 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 er of elements i
4a3b0 6e 0a 2a 2a 20 61 6e 20 61 72 72 61 79 2e 0a 2a n.** an array..*
4a3c0 2f 0a 23 64 65 66 69 6e 65 20 41 72 72 61 79 53 /.#define ArrayS
4a3d0 69 7a 65 28 58 29 20 20 20 20 28 28 69 6e 74 29 ize(X) ((int)
4a3e0 28 73 69 7a 65 6f 66 28 58 29 2f 73 69 7a 65 6f (sizeof(X)/sizeo
4a3f0 66 28 58 5b 30 5d 29 29 29 0a 0a 2f 2a 0a 2a 2a f(X[0])))../*.**
4a400 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 The following v
4a410 61 6c 75 65 20 61 73 20 61 20 64 65 73 74 72 75 alue as a destru
4a420 63 74 6f 72 20 6d 65 61 6e 73 20 74 6f 20 75 73 ctor means to us
4a430 65 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 e sqlite3DbFree(
4a440 29 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e )..** This is an
4a450 20 69 6e 74 65 72 6e 61 6c 20 65 78 74 65 6e 73 internal extens
4a460 69 6f 6e 20 74 6f 20 53 51 4c 49 54 45 5f 53 54 ion to SQLITE_ST
4a470 41 54 49 43 20 61 6e 64 20 53 51 4c 49 54 45 5f ATIC and SQLITE_
4a480 54 52 41 4e 53 49 45 4e 54 2e 0a 2a 2f 0a 23 64 TRANSIENT..*/.#d
4a490 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 59 4e efine SQLITE_DYN
4a4a0 41 4d 49 43 20 20 20 28 28 73 71 6c 69 74 65 33 AMIC ((sqlite3
4a4b0 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 _destructor_type
4a4c0 29 73 71 6c 69 74 65 33 44 62 46 72 65 65 29 0a )sqlite3DbFree).
4a4d0 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 49 ./*.** When SQLI
4a4e0 54 45 5f 4f 4d 49 54 5f 57 53 44 20 69 73 20 64 TE_OMIT_WSD is d
4a4f0 65 66 69 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73 efined, it means
4a500 20 74 68 61 74 20 74 68 65 20 74 61 72 67 65 74 that the target
4a510 20 70 6c 61 74 66 6f 72 6d 20 64 6f 65 73 0a 2a platform does.*
4a520 2a 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 57 72 * not support Wr
4a530 69 74 61 62 6c 65 20 53 74 61 74 69 63 20 44 61 itable Static Da
4a540 74 61 20 28 57 53 44 29 20 73 75 63 68 20 61 73 ta (WSD) such as
4a550 20 67 6c 6f 62 61 6c 20 61 6e 64 20 73 74 61 74 global and stat
4a560 69 63 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a ic variables..**
4a570 20 41 6c 6c 20 76 61 72 69 61 62 6c 65 73 20 6d All variables m
4a580 75 73 74 20 65 69 74 68 65 72 20 62 65 20 6f 6e ust either be on
4a590 20 74 68 65 20 73 74 61 63 6b 20 6f 72 20 64 79 the stack or dy
4a5a0 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 namically alloca
4a5b0 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 ted from.** the
4a5c0 68 65 61 70 2e 20 20 57 68 65 6e 20 57 53 44 20 heap. When WSD
4a5d0 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 2c 20 is unsupported,
4a5e0 74 68 65 20 76 61 72 69 61 62 6c 65 20 64 65 63 the variable dec
4a5f0 6c 61 72 61 74 69 6f 6e 73 20 73 63 61 74 74 65 larations scatte
4a600 72 65 64 0a 2a 2a 20 74 68 72 6f 75 67 68 6f 75 red.** throughou
4a610 74 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 64 t the SQLite cod
4a620 65 20 6d 75 73 74 20 62 65 63 6f 6d 65 20 63 6f e must become co
4a630 6e 73 74 61 6e 74 73 20 69 6e 73 74 65 61 64 2e nstants instead.
4a640 20 20 54 68 65 20 53 51 4c 49 54 45 5f 57 53 44 The SQLITE_WSD
4a650 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20 75 73 65 .** macro is use
4a660 64 20 66 6f 72 20 74 68 69 73 20 70 75 72 70 6f d for this purpo
4a670 73 65 2e 20 20 41 6e 64 20 69 6e 73 74 65 61 64 se. And instead
4a680 20 6f 66 20 72 65 66 65 72 65 6e 63 69 6e 67 20 of referencing
4a690 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 the variable.**
4a6a0 64 69 72 65 63 74 6c 79 2c 20 77 65 20 75 73 65 directly, we use
4a6b0 20 69 74 73 20 63 6f 6e 73 74 61 6e 74 20 61 73 its constant as
4a6c0 20 61 20 6b 65 79 20 74 6f 20 6c 6f 6f 6b 75 70 a key to lookup
4a6d0 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 61 6c the run-time al
4a6e0 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 75 66 66 65 located.** buffe
4a6f0 72 20 74 68 61 74 20 68 6f 6c 64 73 20 72 65 61 r that holds rea
4a700 6c 20 76 61 72 69 61 62 6c 65 2e 20 20 54 68 65 l variable. The
4a710 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 61 6c 73 constant is als
4a720 6f 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 65 o the initialize
4a730 72 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72 75 6e r.** for the run
4a740 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65 64 20 -time allocated
4a750 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e buffer..**.** In
4a760 20 74 68 65 20 75 73 75 61 6c 20 63 61 73 65 20 the usual case
4a770 77 68 65 72 65 20 57 53 44 20 69 73 20 73 75 70 where WSD is sup
4a780 70 6f 72 74 65 64 2c 20 74 68 65 20 53 51 4c 49 ported, the SQLI
4a790 54 45 5f 57 53 44 20 61 6e 64 20 47 4c 4f 42 41 TE_WSD and GLOBA
4a7a0 4c 0a 2a 2a 20 6d 61 63 72 6f 73 20 62 65 63 6f L.** macros beco
4a7b0 6d 65 20 6e 6f 2d 6f 70 73 20 61 6e 64 20 68 61 me no-ops and ha
4a7c0 76 65 20 7a 65 72 6f 20 70 65 72 66 6f 72 6d 61 ve zero performa
4a7d0 6e 63 65 20 69 6d 70 61 63 74 2e 0a 2a 2f 0a 23 nce impact..*/.#
4a7e0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
4a7f0 54 5f 57 53 44 0a 20 20 23 64 65 66 69 6e 65 20 T_WSD. #define
4a800 53 51 4c 49 54 45 5f 57 53 44 20 63 6f 6e 73 74 SQLITE_WSD const
4a810 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f 42 41 . #define GLOBA
4a820 4c 28 74 2c 76 29 20 28 2a 28 74 2a 29 73 71 6c L(t,v) (*(t*)sql
4a830 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28 28 76 ite3_wsd_find((v
4a840 6f 69 64 2a 29 26 28 76 29 2c 20 73 69 7a 65 6f oid*)&(v), sizeo
4a850 66 28 76 29 29 29 0a 20 20 23 64 65 66 69 6e 65 f(v))). #define
4a860 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
4a870 6e 66 69 67 20 47 4c 4f 42 41 4c 28 73 74 72 75 nfig GLOBAL(stru
4a880 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 ct Sqlite3Config
4a890 2c 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 29 , sqlite3Config)
4a8a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 20 69 6e .SQLITE_API in
4a8b0 74 20 73 71 6c 69 74 65 33 5f 77 73 64 5f 69 6e t sqlite3_wsd_in
4a8c0 69 74 28 69 6e 74 20 4e 2c 20 69 6e 74 20 4a 29 it(int N, int J)
4a8d0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 20 76 ;.SQLITE_API v
4a8e0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 77 73 64 oid *sqlite3_wsd
4a8f0 5f 66 69 6e 64 28 76 6f 69 64 20 2a 4b 2c 20 69 _find(void *K, i
4a900 6e 74 20 4c 29 3b 0a 23 65 6c 73 65 0a 20 20 23 nt L);.#else. #
4a910 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 53 define SQLITE_WS
4a920 44 20 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f D . #define GLO
4a930 42 41 4c 28 74 2c 76 29 20 76 0a 20 20 23 64 65 BAL(t,v) v. #de
4a940 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 fine sqlite3Glob
4a950 61 6c 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 alConfig sqlite3
4a960 43 6f 6e 66 69 67 0a 23 65 6e 64 69 66 0a 0a 2f Config.#endif../
4a970 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
4a980 6e 67 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73 ng macros are us
4a990 65 64 20 74 6f 20 73 75 70 70 72 65 73 73 20 63 ed to suppress c
4a9a0 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 ompiler warnings
4a9b0 20 61 6e 64 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 and to.** make
4a9c0 69 74 20 63 6c 65 61 72 20 74 6f 20 68 75 6d 61 it clear to huma
4a9d0 6e 20 72 65 61 64 65 72 73 20 77 68 65 6e 20 61 n readers when a
4a9e0 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65 function parame
4a9f0 74 65 72 20 69 73 20 64 65 6c 69 62 65 72 61 74 ter is deliberat
4aa00 65 6c 79 20 0a 2a 2a 20 6c 65 66 74 20 75 6e 75 ely .** left unu
4aa10 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 62 sed within the b
4aa20 6f 64 79 20 6f 66 20 61 20 66 75 6e 63 74 69 6f ody of a functio
4aa30 6e 2e 20 54 68 69 73 20 75 73 75 61 6c 6c 79 20 n. This usually
4aa40 68 61 70 70 65 6e 73 20 77 68 65 6e 0a 2a 2a 20 happens when.**
4aa50 61 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 a function is ca
4aa60 6c 6c 65 64 20 76 69 61 20 61 20 66 75 6e 63 74 lled via a funct
4aa70 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 20 46 6f 72 ion pointer. For
4aa80 20 65 78 61 6d 70 6c 65 20 74 68 65 20 0a 2a 2a example the .**
4aa90 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
4aaa0 6f 66 20 61 6e 20 53 51 4c 20 61 67 67 72 65 67 of an SQL aggreg
4aab0 61 74 65 20 73 74 65 70 20 63 61 6c 6c 62 61 63 ate step callbac
4aac0 6b 20 6d 61 79 20 6e 6f 74 20 75 73 65 20 74 68 k may not use th
4aad0 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 e.** parameter i
4aae0 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6e 75 ndicating the nu
4aaf0 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
4ab00 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 s passed to the
4ab10 61 67 67 72 65 67 61 74 65 2c 0a 2a 2a 20 69 66 aggregate,.** if
4ab20 20 69 74 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 it knows that t
4ab30 68 69 73 20 69 73 20 65 6e 66 6f 72 63 65 64 20 his is enforced
4ab40 65 6c 73 65 77 68 65 72 65 2e 0a 2a 2a 0a 2a 2a elsewhere..**.**
4ab50 20 57 68 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e When a function
4ab60 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f parameter is no
4ab70 74 20 75 73 65 64 20 61 74 20 61 6c 6c 20 77 69 t used at all wi
4ab80 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 thin the body of
4ab90 20 61 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 a function,.**
4aba0 69 74 20 69 73 20 67 65 6e 65 72 61 6c 6c 79 20 it is generally
4abb0 6e 61 6d 65 64 20 22 4e 6f 74 55 73 65 64 22 20 named "NotUsed"
4abc0 6f 72 20 22 4e 6f 74 55 73 65 64 32 22 20 74 6f or "NotUsed2" to
4abd0 20 6d 61 6b 65 20 74 68 69 6e 67 73 20 65 76 65 make things eve
4abe0 6e 20 63 6c 65 61 72 65 72 2e 0a 2a 2a 20 48 6f n clearer..** Ho
4abf0 77 65 76 65 72 2c 20 74 68 65 73 65 20 6d 61 63 wever, these mac
4ac00 72 6f 73 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 ros may also be
4ac10 75 73 65 64 20 74 6f 20 73 75 70 70 72 65 73 73 used to suppress
4ac20 20 77 61 72 6e 69 6e 67 73 20 72 65 6c 61 74 65 warnings relate
4ac30 64 20 74 6f 0a 2a 2a 20 70 61 72 61 6d 65 74 65 d to.** paramete
4ac40 72 73 20 74 68 61 74 20 6d 61 79 20 6f 72 20 6d rs that may or m
4ac50 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20 64 ay not be used d
4ac60 65 70 65 6e 64 69 6e 67 20 6f 6e 20 63 6f 6d 70 epending on comp
4ac70 69 6c 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 2e ilation options.
4ac80 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 20 .** For example
4ac90 74 68 6f 73 65 20 70 61 72 61 6d 65 74 65 72 73 those parameters
4aca0 20 6f 6e 6c 79 20 75 73 65 64 20 69 6e 20 61 73 only used in as
4acb0 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
4acc0 73 2e 20 49 6e 20 74 68 65 73 65 0a 2a 2a 20 63 s. In these.** c
4acd0 61 73 65 73 20 74 68 65 20 70 61 72 61 6d 65 74 ases the paramet
4ace0 65 72 73 20 61 72 65 20 6e 61 6d 65 64 20 61 73 ers are named as
4acf0 20 70 65 72 20 74 68 65 20 75 73 75 61 6c 20 63 per the usual c
4ad00 6f 6e 76 65 6e 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 onventions..*/.#
4ad10 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41 define UNUSED_PA
4ad20 52 41 4d 45 54 45 52 28 78 29 20 28 76 6f 69 64 RAMETER(x) (void
4ad30 29 28 78 29 0a 23 64 65 66 69 6e 65 20 55 4e 55 )(x).#define UNU
4ad40 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 78 SED_PARAMETER2(x
4ad50 2c 79 29 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ,y) UNUSED_PARAM
4ad60 45 54 45 52 28 78 29 2c 55 4e 55 53 45 44 5f 50 ETER(x),UNUSED_P
4ad70 41 52 41 4d 45 54 45 52 28 79 29 0a 0a 2f 2a 0a ARAMETER(y)../*.
4ad80 2a 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 ** Forward refer
4ad90 65 6e 63 65 73 20 74 6f 20 73 74 72 75 63 74 75 ences to structu
4ada0 72 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 res.*/.typedef s
4adb0 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 41 67 truct AggInfo Ag
4adc0 67 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 gInfo;.typedef s
4add0 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78 truct AuthContex
4ade0 74 20 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 74 t AuthContext;.t
4adf0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 41 75 ypedef struct Au
4ae00 74 6f 69 6e 63 49 6e 66 6f 20 41 75 74 6f 69 6e toincInfo Autoin
4ae10 63 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 cInfo;.typedef s
4ae20 74 72 75 63 74 20 42 69 74 76 65 63 20 42 69 74 truct Bitvec Bit
4ae30 76 65 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72 vec;.typedef str
4ae40 75 63 74 20 52 6f 77 53 65 74 20 52 6f 77 53 65 uct RowSet RowSe
4ae50 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 t;.typedef struc
4ae60 74 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 65 t CollSeq CollSe
4ae70 71 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 q;.typedef struc
4ae80 74 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e 3b t Column Column;
4ae90 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4aea0 44 62 20 44 62 3b 0a 74 79 70 65 64 65 66 20 73 Db Db;.typedef s
4aeb0 74 72 75 63 74 20 53 63 68 65 6d 61 20 53 63 68 truct Schema Sch
4aec0 65 6d 61 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ema;.typedef str
4aed0 75 63 74 20 45 78 70 72 20 45 78 70 72 3b 0a 74 uct Expr Expr;.t
4aee0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78 ypedef struct Ex
4aef0 70 72 4c 69 73 74 20 45 78 70 72 4c 69 73 74 3b prList ExprList;
4af00 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4af10 45 78 70 72 53 70 61 6e 20 45 78 70 72 53 70 61 ExprSpan ExprSpa
4af20 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 n;.typedef struc
4af30 74 20 46 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70 t FKey FKey;.typ
4af40 65 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 edef struct Func
4af50 44 65 66 20 46 75 6e 63 44 65 66 3b 0a 74 79 70 Def FuncDef;.typ
4af60 65 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 edef struct Func
4af70 44 65 66 48 61 73 68 20 46 75 6e 63 44 65 66 48 DefHash FuncDefH
4af80 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ash;.typedef str
4af90 75 63 74 20 49 64 4c 69 73 74 20 49 64 4c 69 73 uct IdList IdLis
4afa0 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 t;.typedef struc
4afb0 74 20 49 6e 64 65 78 20 49 6e 64 65 78 3b 0a 74 t Index Index;.t
4afc0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e ypedef struct In
4afd0 64 65 78 53 61 6d 70 6c 65 20 49 6e 64 65 78 53 dexSample IndexS
4afe0 61 6d 70 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 ample;.typedef s
4aff0 74 72 75 63 74 20 4b 65 79 43 6c 61 73 73 20 4b truct KeyClass K
4b000 65 79 43 6c 61 73 73 3b 0a 74 79 70 65 64 65 66 eyClass;.typedef
4b010 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 struct KeyInfo
4b020 4b 65 79 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 KeyInfo;.typedef
4b030 20 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 struct Lookasid
4b040 65 20 4c 6f 6f 6b 61 73 69 64 65 3b 0a 74 79 70 e Lookaside;.typ
4b050 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f 6f 6b edef struct Look
4b060 61 73 69 64 65 53 6c 6f 74 20 4c 6f 6f 6b 61 73 asideSlot Lookas
4b070 69 64 65 53 6c 6f 74 3b 0a 74 79 70 65 64 65 66 ideSlot;.typedef
4b080 20 73 74 72 75 63 74 20 4d 6f 64 75 6c 65 20 4d struct Module M
4b090 6f 64 75 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 odule;.typedef s
4b0a0 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 truct NameContex
4b0b0 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 3b 0a 74 t NameContext;.t
4b0c0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61 ypedef struct Pa
4b0d0 72 73 65 20 50 61 72 73 65 3b 0a 74 79 70 65 64 rse Parse;.typed
4b0e0 65 66 20 73 74 72 75 63 74 20 53 61 76 65 70 6f ef struct Savepo
4b0f0 69 6e 74 20 53 61 76 65 70 6f 69 6e 74 3b 0a 74 int Savepoint;.t
4b100 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 65 ypedef struct Se
4b110 6c 65 63 74 20 53 65 6c 65 63 74 3b 0a 74 79 70 lect Select;.typ
4b120 65 64 65 66 20 73 74 72 75 63 74 20 53 72 63 4c edef struct SrcL
4b130 69 73 74 20 53 72 63 4c 69 73 74 3b 0a 74 79 70 ist SrcList;.typ
4b140 65 64 65 66 20 73 74 72 75 63 74 20 53 74 72 41 edef struct StrA
4b150 63 63 75 6d 20 53 74 72 41 63 63 75 6d 3b 0a 74 ccum StrAccum;.t
4b160 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61 ypedef struct Ta
4b170 62 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70 65 64 ble Table;.typed
4b180 65 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 4c ef struct TableL
4b190 6f 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b 0a 74 ock TableLock;.t
4b1a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 6f ypedef struct To
4b1b0 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70 65 64 ken Token;.typed
4b1c0 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 ef struct Trigge
4b1d0 72 50 72 67 20 54 72 69 67 67 65 72 50 72 67 3b rPrg TriggerPrg;
4b1e0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b1f0 54 72 69 67 67 65 72 53 74 65 70 20 54 72 69 67 TriggerStep Trig
4b200 67 65 72 53 74 65 70 3b 0a 74 79 70 65 64 65 66 gerStep;.typedef
4b210 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 struct Trigger
4b220 54 72 69 67 67 65 72 3b 0a 74 79 70 65 64 65 66 Trigger;.typedef
4b230 20 73 74 72 75 63 74 20 55 6e 70 61 63 6b 65 64 struct Unpacked
4b240 52 65 63 6f 72 64 20 55 6e 70 61 63 6b 65 64 52 Record UnpackedR
4b250 65 63 6f 72 64 3b 0a 74 79 70 65 64 65 66 20 73 ecord;.typedef s
4b260 74 72 75 63 74 20 56 54 61 62 6c 65 20 56 54 61 truct VTable VTa
4b270 62 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ble;.typedef str
4b280 75 63 74 20 57 61 6c 6b 65 72 20 57 61 6c 6b 65 uct Walker Walke
4b290 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 r;.typedef struc
4b2a0 74 20 57 68 65 72 65 50 6c 61 6e 20 57 68 65 72 t WherePlan Wher
4b2b0 65 50 6c 61 6e 3b 0a 74 79 70 65 64 65 66 20 73 ePlan;.typedef s
4b2c0 74 72 75 63 74 20 57 68 65 72 65 49 6e 66 6f 20 truct WhereInfo
4b2d0 57 68 65 72 65 49 6e 66 6f 3b 0a 74 79 70 65 64 WhereInfo;.typed
4b2e0 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 4c ef struct WhereL
4b2f0 65 76 65 6c 20 57 68 65 72 65 4c 65 76 65 6c 3b evel WhereLevel;
4b300 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 20 73 6f ../*.** Defer so
4b310 75 72 63 69 6e 67 20 76 64 62 65 2e 68 20 61 6e urcing vdbe.h an
4b320 64 20 62 74 72 65 65 2e 68 20 75 6e 74 69 6c 20 d btree.h until
4b330 61 66 74 65 72 20 74 68 65 20 22 75 38 22 20 61 after the "u8" a
4b340 6e 64 20 0a 2a 2a 20 22 42 75 73 79 48 61 6e 64 nd .** "BusyHand
4b350 6c 65 72 22 20 74 79 70 65 64 65 66 73 2e 20 76 ler" typedefs. v
4b360 64 62 65 2e 68 20 61 6c 73 6f 20 72 65 71 75 69 dbe.h also requi
4b370 72 65 73 20 61 20 66 65 77 20 6f 66 20 74 68 65 res a few of the
4b380 20 6f 70 61 71 75 65 0a 2a 2a 20 70 6f 69 6e 74 opaque.** point
4b390 65 72 20 74 79 70 65 73 20 28 69 2e 65 2e 20 46 er types (i.e. F
4b3a0 75 6e 63 44 65 66 29 20 64 65 66 69 6e 65 64 20 uncDef) defined
4b3b0 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a above..*/./*****
4b3c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
4b3d0 65 20 62 74 72 65 65 2e 68 20 69 6e 20 74 68 65 e btree.h in the
4b3e0 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 middle of sqlit
4b3f0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
4b400 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
4b410 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
4b420 66 69 6c 65 20 62 74 72 65 65 2e 68 20 2a 2a 2a file btree.h ***
4b430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b450 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
4b460 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
4b470 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
4b480 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
4b490 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
4b4a0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
4b4b0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
4b4c0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
4b4d0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
4b4e0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
4b4f0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
4b500 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
4b510 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
4b520 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
4b530 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
4b540 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
4b550 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
4b560 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
4b570 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
4b580 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
4b590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b5d0 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 .** This header
4b5e0 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 file defines the
4b5f0 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 interface that
4b600 74 68 65 20 73 71 6c 69 74 65 20 42 2d 54 72 65 the sqlite B-Tre
4b610 65 20 66 69 6c 65 0a 2a 2a 20 73 75 62 73 79 73 e file.** subsys
4b620 74 65 6d 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e tem. See commen
4b630 74 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 ts in the source
4b640 20 63 6f 64 65 20 66 6f 72 20 61 20 64 65 74 61 code for a deta
4b650 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e iled description
4b660 0a 2a 2a 20 6f 66 20 77 68 61 74 20 65 61 63 68 .** of what each
4b670 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 interface routi
4b680 6e 65 20 64 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 40 ne does..**.** @
4b690 28 23 29 20 24 49 64 3a 20 62 74 72 65 65 2e 68 (#) $Id: btree.h
4b6a0 2c 76 20 31 2e 31 32 30 20 32 30 30 39 2f 30 37 ,v 1.120 2009/07
4b6b0 2f 32 32 20 30 30 3a 33 35 3a 32 34 20 64 72 68 /22 00:35:24 drh
4b6c0 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 Exp $.*/.#ifnde
4b6d0 66 20 5f 42 54 52 45 45 5f 48 5f 0a 23 64 65 66 f _BTREE_H_.#def
4b6e0 69 6e 65 20 5f 42 54 52 45 45 5f 48 5f 0a 0a 2f ine _BTREE_H_../
4b6f0 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 64 65 66 * TODO: This def
4b700 69 6e 69 74 69 6f 6e 20 69 73 20 6a 75 73 74 20 inition is just
4b710 69 6e 63 6c 75 64 65 64 20 73 6f 20 6f 74 68 65 included so othe
4b720 72 20 6d 6f 64 75 6c 65 73 20 63 6f 6d 70 69 6c r modules compil
4b730 65 2e 20 49 74 0a 2a 2a 20 6e 65 65 64 73 20 74 e. It.** needs t
4b740 6f 20 62 65 20 72 65 76 69 73 69 74 65 64 2e 0a o be revisited..
4b750 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
4b760 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 31 E_N_BTREE_META 1
4b770 30 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 64 65 66 69 0../*.** If defi
4b780 6e 65 64 20 61 73 20 6e 6f 6e 2d 7a 65 72 6f 2c ned as non-zero,
4b790 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 auto-vacuum is
4b7a0 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 enabled by defau
4b7b0 6c 74 2e 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a lt. Otherwise.**
4b7c0 20 69 74 20 6d 75 73 74 20 62 65 20 74 75 72 6e it must be turn
4b7d0 65 64 20 6f 6e 20 66 6f 72 20 65 61 63 68 20 64 ed on for each d
4b7e0 61 74 61 62 61 73 65 20 75 73 69 6e 67 20 22 50 atabase using "P
4b7f0 52 41 47 4d 41 20 61 75 74 6f 5f 76 61 63 75 75 RAGMA auto_vacuu
4b800 6d 20 3d 20 31 22 2e 0a 2a 2f 0a 23 69 66 6e 64 m = 1"..*/.#ifnd
4b810 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ef SQLITE_DEFAUL
4b820 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 23 T_AUTOVACUUM. #
4b830 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
4b840 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d FAULT_AUTOVACUUM
4b850 20 30 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 0.#endif..#defi
4b860 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 ne BTREE_AUTOVAC
4b870 55 55 4d 5f 4e 4f 4e 45 20 30 20 20 20 20 20 20 UUM_NONE 0
4b880 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 6f 20 61 /* Do not do a
4b890 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 64 uto-vacuum */.#d
4b8a0 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f efine BTREE_AUTO
4b8b0 56 41 43 55 55 4d 5f 46 55 4c 4c 20 31 20 20 20 VACUUM_FULL 1
4b8c0 20 20 20 20 20 2f 2a 20 44 6f 20 66 75 6c 6c 20 /* Do full
4b8d0 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 auto-vacuum */.#
4b8e0 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54 define BTREE_AUT
4b8f0 4f 56 41 43 55 55 4d 5f 49 4e 43 52 20 32 20 20 OVACUUM_INCR 2
4b900 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 /* Increme
4b910 6e 74 61 6c 20 76 61 63 75 75 6d 20 2a 2f 0a 0a ntal vacuum */..
4b920 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64 65 /*.** Forward de
4b930 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74 clarations of st
4b940 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65 64 ructure.*/.typed
4b950 65 66 20 73 74 72 75 63 74 20 42 74 72 65 65 20 ef struct Btree
4b960 42 74 72 65 65 3b 0a 74 79 70 65 64 65 66 20 73 Btree;.typedef s
4b970 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 42 truct BtCursor B
4b980 74 43 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 tCursor;.typedef
4b990 20 73 74 72 75 63 74 20 42 74 53 68 61 72 65 64 struct BtShared
4b9a0 20 42 74 53 68 61 72 65 64 3b 0a 74 79 70 65 64 BtShared;.typed
4b9b0 65 66 20 73 74 72 75 63 74 20 42 74 72 65 65 4d ef struct BtreeM
4b9c0 75 74 65 78 41 72 72 61 79 20 42 74 72 65 65 4d utexArray BtreeM
4b9d0 75 74 65 78 41 72 72 61 79 3b 0a 0a 2f 2a 0a 2a utexArray;../*.*
4b9e0 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 * This structure
4b9f0 20 72 65 63 6f 72 64 73 20 61 6c 6c 20 6f 66 20 records all of
4ba00 74 68 65 20 42 74 72 65 65 73 20 74 68 61 74 20 the Btrees that
4ba10 6e 65 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 need to hold.**
4ba20 61 20 6d 75 74 65 78 20 62 65 66 6f 72 65 20 77 a mutex before w
4ba30 65 20 65 6e 74 65 72 20 73 71 6c 69 74 65 33 56 e enter sqlite3V
4ba40 64 62 65 45 78 65 63 28 29 2e 20 20 54 68 65 20 dbeExec(). The
4ba50 42 74 72 65 65 73 20 61 72 65 0a 2a 2a 20 61 72 Btrees are.** ar
4ba60 65 20 70 6c 61 63 65 64 20 69 6e 20 61 42 74 72 e placed in aBtr
4ba70 65 65 5b 5d 20 69 6e 20 6f 72 64 65 72 20 6f 66 ee[] in order of
4ba80 20 61 42 74 72 65 65 5b 5d 2d 3e 70 42 74 2e 20 aBtree[]->pBt.
4ba90 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 65 That way,.** we
4baa0 20 63 61 6e 20 61 6c 77 61 79 73 20 6c 6f 63 6b can always lock
4bab0 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 6d and unlock them
4bac0 20 61 6c 6c 20 71 75 69 63 6b 6c 79 2e 0a 2a 2f all quickly..*/
4bad0 0a 73 74 72 75 63 74 20 42 74 72 65 65 4d 75 74 .struct BtreeMut
4bae0 65 78 41 72 72 61 79 20 7b 0a 20 20 69 6e 74 20 exArray {. int
4baf0 6e 4d 75 74 65 78 3b 0a 20 20 42 74 72 65 65 20 nMutex;. Btree
4bb00 2a 61 42 74 72 65 65 5b 53 51 4c 49 54 45 5f 4d *aBtree[SQLITE_M
4bb10 41 58 5f 41 54 54 41 43 48 45 44 2b 31 5d 3b 0a AX_ATTACHED+1];.
4bb20 7d 3b 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 };...SQLITE_PRIV
4bb30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4bb40 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 treeOpen(. cons
4bb50 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
4bb60 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 e, /* Name of
4bb70 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f database file to
4bb80 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 open */. sqlit
4bb90 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
4bba0 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 /* Associate
4bbb0 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 d database conne
4bbc0 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 72 65 65 ction */. Btree
4bbd0 20 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 20 20 **ppBtree,
4bbe0 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 6f 70 /* Return op
4bbf0 65 6e 20 42 74 72 65 65 2a 20 68 65 72 65 20 2a en Btree* here *
4bc00 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
4bc10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4bc20 46 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 76 Flags */. int v
4bc30 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 fsFlags
4bc40 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 /* Flags pas
4bc50 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 56 sed through to V
4bc60 46 53 20 6f 70 65 6e 20 2a 2f 0a 29 3b 0a 0a 2f FS open */.);../
4bc70 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 * The flags para
4bc80 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 meter to sqlite3
4bc90 42 74 72 65 65 4f 70 65 6e 20 63 61 6e 20 62 65 BtreeOpen can be
4bca0 20 74 68 65 20 62 69 74 77 69 73 65 20 6f 72 20 the bitwise or
4bcb0 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 of the.** follow
4bcc0 69 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a ing values..**.*
4bcd0 2a 20 4e 4f 54 45 3a 20 20 54 68 65 73 65 20 76 * NOTE: These v
4bce0 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 74 63 68 alues must match
4bcf0 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
4bd00 6e 67 20 50 41 47 45 52 5f 20 76 61 6c 75 65 73 ng PAGER_ values
4bd10 20 69 6e 0a 2a 2a 20 70 61 67 65 72 2e 68 2e 0a in.** pager.h..
4bd20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 */.#define BTREE
4bd30 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 31 _OMIT_JOURNAL 1
4bd40 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 /* Do not use
4bd50 6a 6f 75 72 6e 61 6c 2e 20 20 4e 6f 20 61 72 67 journal. No arg
4bd60 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 ument */.#define
4bd70 20 42 54 52 45 45 5f 4e 4f 5f 52 45 41 44 4c 4f BTREE_NO_READLO
4bd80 43 4b 20 20 20 32 20 20 2f 2a 20 4f 6d 69 74 20 CK 2 /* Omit
4bd90 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61 readlocks on rea
4bda0 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 23 donly files */.#
4bdb0 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4d 45 4d define BTREE_MEM
4bdc0 4f 52 59 20 20 20 20 20 20 20 20 34 20 20 2f 2a ORY 4 /*
4bdd0 20 49 6e 2d 6d 65 6d 6f 72 79 20 44 42 2e 20 20 In-memory DB.
4bde0 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 No argument */.#
4bdf0 64 65 66 69 6e 65 20 42 54 52 45 45 5f 52 45 41 define BTREE_REA
4be00 44 4f 4e 4c 59 20 20 20 20 20 20 38 20 20 2f 2a DONLY 8 /*
4be10 20 4f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 Open the databa
4be20 73 65 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 se in read-only
4be30 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 mode */.#define
4be40 42 54 52 45 45 5f 52 45 41 44 57 52 49 54 45 20 BTREE_READWRITE
4be50 20 20 20 31 36 20 20 2f 2a 20 4f 70 65 6e 20 66 16 /* Open f
4be60 6f 72 20 62 6f 74 68 20 72 65 61 64 69 6e 67 20 or both reading
4be70 61 6e 64 20 77 72 69 74 69 6e 67 20 2a 2f 0a 23 and writing */.#
4be80 64 65 66 69 6e 65 20 42 54 52 45 45 5f 43 52 45 define BTREE_CRE
4be90 41 54 45 20 20 20 20 20 20 20 33 32 20 20 2f 2a ATE 32 /*
4bea0 20 43 72 65 61 74 65 20 74 68 65 20 64 61 74 61 Create the data
4beb0 62 61 73 65 20 69 66 20 69 74 20 64 6f 65 73 20 base if it does
4bec0 6e 6f 74 20 65 78 69 73 74 20 2a 2f 0a 0a 53 51 not exist */..SQ
4bed0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4bee0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f sqlite3BtreeClo
4bef0 73 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 se(Btree*);.SQLI
4bf00 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4bf10 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 qlite3BtreeSetCa
4bf20 63 68 65 53 69 7a 65 28 42 74 72 65 65 2a 2c 69 cheSize(Btree*,i
4bf30 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
4bf40 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4bf50 74 72 65 65 53 65 74 53 61 66 65 74 79 4c 65 76 treeSetSafetyLev
4bf60 65 6c 28 42 74 72 65 65 2a 2c 69 6e 74 2c 69 6e el(Btree*,int,in
4bf70 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
4bf80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4bf90 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 reeSyncDisabled(
4bfa0 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Btree*);.SQLITE_
4bfb0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4bfc0 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53 te3BtreeSetPageS
4bfd0 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e ize(Btree *p, in
4bfe0 74 20 6e 50 61 67 65 73 69 7a 65 2c 20 69 6e 74 t nPagesize, int
4bff0 20 6e 52 65 73 65 72 76 65 2c 20 69 6e 74 20 65 nReserve, int e
4c000 46 69 78 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 Fix);.SQLITE_PRI
4c010 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4c020 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 BtreeGetPageSize
4c030 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
4c040 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4c050 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65 ite3BtreeMaxPage
4c060 43 6f 75 6e 74 28 42 74 72 65 65 2a 2c 69 6e 74 Count(Btree*,int
4c070 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c080 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c090 65 65 47 65 74 52 65 73 65 72 76 65 28 42 74 72 eeGetReserve(Btr
4c0a0 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ee*);.SQLITE_PRI
4c0b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4c0c0 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75 BtreeSetAutoVacu
4c0d0 75 6d 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 29 um(Btree *, int)
4c0e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4c0f0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4c100 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 eGetAutoVacuum(B
4c110 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f tree *);.SQLITE_
4c120 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4c130 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 te3BtreeBeginTra
4c140 6e 73 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a ns(Btree*,int);.
4c150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c160 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
4c170 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 ommitPhaseOne(Bt
4c180 72 65 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 ree*, const char
4c190 20 2a 7a 4d 61 73 74 65 72 29 3b 0a 53 51 4c 49 *zMaster);.SQLI
4c1a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c1b0 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
4c1c0 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 65 2a tPhaseTwo(Btree*
4c1d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c1e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c1f0 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 2a 29 eeCommit(Btree*)
4c200 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4c210 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4c220 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 2a eRollback(Btree*
4c230 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c240 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c250 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72 65 eeBeginStmt(Btre
4c260 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f e*,int);.SQLITE_
4c270 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4c280 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 te3BtreeCreateTa
4c290 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2a ble(Btree*, int*
4c2a0 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 53 51 , int flags);.SQ
4c2b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4c2c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 sqlite3BtreeIsI
4c2d0 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a nTrans(Btree*);.
4c2e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c2f0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 nt sqlite3BtreeI
4c300 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74 72 sInReadTrans(Btr
4c310 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ee*);.SQLITE_PRI
4c320 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4c330 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 BtreeIsInBackup(
4c340 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Btree*);.SQLITE_
4c350 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
4c360 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 lite3BtreeSchema
4c370 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 76 (Btree *, int, v
4c380 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 29 29 3b oid(*)(void *));
4c390 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4c3a0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4c3b0 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42 74 72 SchemaLocked(Btr
4c3c0 65 65 20 2a 70 42 74 72 65 65 29 3b 0a 53 51 4c ee *pBtree);.SQL
4c3d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4c3e0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 6b sqlite3BtreeLock
4c3f0 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 42 74 Table(Btree *pBt
4c400 72 65 65 2c 20 69 6e 74 20 69 54 61 62 2c 20 75 ree, int iTab, u
4c410 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a 8 isWriteLock);.
4c420 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c430 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 nt sqlite3BtreeS
4c440 61 76 65 70 6f 69 6e 74 28 42 74 72 65 65 20 2a avepoint(Btree *
4c450 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53 51 , int, int);..SQ
4c460 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
4c470 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
4c480 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 BtreeGetFilename
4c490 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 (Btree *);.SQLIT
4c4a0 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
4c4b0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 char *sqlite3Btr
4c4c0 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 eeGetJournalname
4c4d0 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 (Btree *);.SQLIT
4c4e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4c4f0 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 lite3BtreeCopyFi
4c500 6c 65 28 42 74 72 65 65 20 2a 2c 20 42 74 72 65 le(Btree *, Btre
4c510 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 e *);..SQLITE_PR
4c520 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4c530 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 3BtreeIncrVacuum
4c540 28 42 74 72 65 65 20 2a 29 3b 0a 0a 2f 2a 20 54 (Btree *);../* T
4c550 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 he flags paramet
4c560 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 er to sqlite3Btr
4c570 65 65 43 72 65 61 74 65 54 61 62 6c 65 20 63 61 eeCreateTable ca
4c580 6e 20 62 65 20 74 68 65 20 62 69 74 77 69 73 65 n be the bitwise
4c590 20 4f 52 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f OR.** of the fo
4c5a0 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 3a 0a 2a llowing flags:.*
4c5b0 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f /.#define BTREE_
4c5c0 49 4e 54 4b 45 59 20 20 20 20 20 31 20 20 20 20 INTKEY 1
4c5d0 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 6f 6e 6c /* Table has onl
4c5e0 79 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 y 64-bit signed
4c5f0 69 6e 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a integer keys */.
4c600 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 5a 45 #define BTREE_ZE
4c610 52 4f 44 41 54 41 20 20 20 32 20 20 20 20 2f 2a RODATA 2 /*
4c620 20 54 61 62 6c 65 20 68 61 73 20 6b 65 79 73 20 Table has keys
4c630 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a only - no data *
4c640 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f /.#define BTREE_
4c650 4c 45 41 46 44 41 54 41 20 20 20 34 20 20 20 20 LEAFDATA 4
4c660 2f 2a 20 44 61 74 61 20 73 74 6f 72 65 64 20 69 /* Data stored i
4c670 6e 20 6c 65 61 76 65 73 20 6f 6e 6c 79 2e 20 20 n leaves only.
4c680 49 6d 70 6c 69 65 73 20 49 4e 54 4b 45 59 20 2a Implies INTKEY *
4c690 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 /..SQLITE_PRIVAT
4c6a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c6b0 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65 eeDropTable(Btre
4c6c0 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a e*, int, int*);.
4c6d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c6e0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
4c6f0 6c 65 61 72 54 61 62 6c 65 28 42 74 72 65 65 2a learTable(Btree*
4c700 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 , int, int*);.SQ
4c710 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
4c720 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 d sqlite3BtreeTr
4c730 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 72 ipAllCursors(Btr
4c740 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 ee*, int);..SQLI
4c750 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
4c760 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d sqlite3BtreeGetM
4c770 65 74 61 28 42 74 72 65 65 20 2a 70 42 74 72 65 eta(Btree *pBtre
4c780 65 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 e, int idx, u32
4c790 2a 70 56 61 6c 75 65 29 3b 0a 53 51 4c 49 54 45 *pValue);.SQLITE
4c7a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4c7b0 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d ite3BtreeUpdateM
4c7c0 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 eta(Btree*, int
4c7d0 69 64 78 2c 20 75 33 32 20 76 61 6c 75 65 29 3b idx, u32 value);
4c7e0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f ../*.** The seco
4c7f0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 nd parameter to
4c800 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d sqlite3BtreeGetM
4c810 65 74 61 20 6f 72 20 73 71 6c 69 74 65 33 42 74 eta or sqlite3Bt
4c820 72 65 65 55 70 64 61 74 65 4d 65 74 61 0a 2a 2a reeUpdateMeta.**
4c830 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f should be one o
4c840 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
4c850 76 61 6c 75 65 73 2e 20 54 68 65 20 69 6e 74 65 values. The inte
4c860 67 65 72 20 76 61 6c 75 65 73 20 61 72 65 20 61 ger values are a
4c870 73 73 69 67 6e 65 64 20 0a 2a 2a 20 74 6f 20 63 ssigned .** to c
4c880 6f 6e 73 74 61 6e 74 73 20 73 6f 20 74 68 61 74 onstants so that
4c890 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74 the offset of t
4c8a0 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
4c8b0 20 66 69 65 6c 64 20 69 6e 20 61 6e 0a 2a 2a 20 field in an.**
4c8c0 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 SQLite database
4c8d0 68 65 61 64 65 72 20 6d 61 79 20 62 65 20 66 6f header may be fo
4c8e0 75 6e 64 20 75 73 69 6e 67 20 74 68 65 20 66 6f und using the fo
4c8f0 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 75 6c 61 3a llowing formula:
4c900 0a 2a 2a 0a 2a 2a 20 20 20 6f 66 66 73 65 74 20 .**.** offset
4c910 3d 20 33 36 20 2b 20 28 69 64 78 20 2a 20 34 29 = 36 + (idx * 4)
4c920 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 .**.** For examp
4c930 6c 65 2c 20 74 68 65 20 66 72 65 65 2d 70 61 67 le, the free-pag
4c940 65 2d 63 6f 75 6e 74 20 66 69 65 6c 64 20 69 73 e-count field is
4c950 20 6c 6f 63 61 74 65 64 20 61 74 20 62 79 74 65 located at byte
4c960 20 6f 66 66 73 65 74 20 33 36 20 6f 66 0a 2a 2a offset 36 of.**
4c970 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
4c980 6c 65 20 68 65 61 64 65 72 2e 20 54 68 65 20 69 le header. The i
4c990 6e 63 72 2d 76 61 63 75 75 6d 2d 66 6c 61 67 20 ncr-vacuum-flag
4c9a0 66 69 65 6c 64 20 69 73 20 6c 6f 63 61 74 65 64 field is located
4c9b0 20 61 74 0a 2a 2a 20 62 79 74 65 20 6f 66 66 73 at.** byte offs
4c9c0 65 74 20 36 34 20 28 3d 3d 20 33 36 2b 34 2a 37 et 64 (== 36+4*7
4c9d0 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 )..*/.#define BT
4c9e0 52 45 45 5f 46 52 45 45 5f 50 41 47 45 5f 43 4f REE_FREE_PAGE_CO
4c9f0 55 4e 54 20 20 20 20 20 30 0a 23 64 65 66 69 6e UNT 0.#defin
4ca00 65 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 e BTREE_SCHEMA_V
4ca10 45 52 53 49 4f 4e 20 20 20 20 20 20 31 0a 23 64 ERSION 1.#d
4ca20 65 66 69 6e 65 20 42 54 52 45 45 5f 46 49 4c 45 efine BTREE_FILE
4ca30 5f 46 4f 52 4d 41 54 20 20 20 20 20 20 20 20 20 _FORMAT
4ca40 32 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 2.#define BTREE_
4ca50 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 DEFAULT_CACHE_SI
4ca60 5a 45 20 20 33 0a 23 64 65 66 69 6e 65 20 42 54 ZE 3.#define BT
4ca70 52 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 REE_LARGEST_ROOT
4ca80 5f 50 41 47 45 20 20 20 34 0a 23 64 65 66 69 6e _PAGE 4.#defin
4ca90 65 20 42 54 52 45 45 5f 54 45 58 54 5f 45 4e 43 e BTREE_TEXT_ENC
4caa0 4f 44 49 4e 47 20 20 20 20 20 20 20 35 0a 23 64 ODING 5.#d
4cab0 65 66 69 6e 65 20 42 54 52 45 45 5f 55 53 45 52 efine BTREE_USER
4cac0 5f 56 45 52 53 49 4f 4e 20 20 20 20 20 20 20 20 _VERSION
4cad0 36 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 6.#define BTREE_
4cae0 49 4e 43 52 5f 56 41 43 55 55 4d 20 20 20 20 20 INCR_VACUUM
4caf0 20 20 20 20 37 0a 0a 53 51 4c 49 54 45 5f 50 52 7..SQLITE_PR
4cb00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4cb10 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a 20 20 3BtreeCursor(.
4cb20 42 74 72 65 65 2a 2c 20 20 20 20 20 20 20 20 20 Btree*,
4cb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cb40 20 20 20 20 20 2f 2a 20 42 54 72 65 65 20 63 6f /* BTree co
4cb50 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20 74 ntaining table t
4cb60 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 o open */. int
4cb70 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 iTable,
4cb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cb90 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 6f 6f /* Index of roo
4cba0 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 t page */. int
4cbb0 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 wrFlag,
4cbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cbd0 20 2f 2a 20 31 20 66 6f 72 20 77 72 69 74 69 6e /* 1 for writin
4cbe0 67 2e 20 20 30 20 66 6f 72 20 72 65 61 64 2d 6f g. 0 for read-o
4cbf0 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 nly */. struct
4cc00 4b 65 79 49 6e 66 6f 2a 2c 20 20 20 20 20 20 20 KeyInfo*,
4cc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4cc20 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 First argument
4cc30 74 6f 20 63 6f 6d 70 61 72 65 20 66 75 6e 63 74 to compare funct
4cc40 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f ion */. BtCurso
4cc50 72 20 2a 70 43 75 72 73 6f 72 20 20 20 20 20 20 r *pCursor
4cc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4cc70 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 65 20 Space to write
4cc80 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 cursor structure
4cc90 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 */.);.SQLITE_PR
4cca0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4ccb0 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 3BtreeCursorSize
4ccc0 28 76 6f 69 64 29 3b 0a 0a 53 51 4c 49 54 45 5f (void);..SQLITE_
4ccd0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4cce0 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 te3BtreeCloseCur
4ccf0 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a sor(BtCursor*);.
4cd00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4cd10 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d nt sqlite3BtreeM
4cd20 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 ovetoUnpacked(.
4cd30 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 55 6e BtCursor*,. Un
4cd40 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 packedRecord *pU
4cd50 6e 4b 65 79 2c 0a 20 20 69 36 34 20 69 6e 74 4b nKey,. i64 intK
4cd60 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c 0a ey,. int bias,.
4cd70 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a 53 int *pRes.);.S
4cd80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4cd90 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 t sqlite3BtreeCu
4cda0 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 43 rsorHasMoved(BtC
4cdb0 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 ursor*, int*);.S
4cdc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4cdd0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 t sqlite3BtreeDe
4cde0 6c 65 74 65 28 42 74 43 75 72 73 6f 72 2a 29 3b lete(BtCursor*);
4cdf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4ce00 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4ce10 49 6e 73 65 72 74 28 42 74 43 75 72 73 6f 72 2a Insert(BtCursor*
4ce20 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b , const void *pK
4ce30 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20 ey, i64 nKey,.
4ce40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ce50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ce60 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 const void *pDat
4ce70 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 a, int nData,.
4ce80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ce90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cea0 69 6e 74 20 6e 5a 65 72 6f 2c 20 69 6e 74 20 62 int nZero, int b
4ceb0 69 61 73 2c 20 69 6e 74 20 73 65 65 6b 52 65 73 ias, int seekRes
4cec0 75 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ult);.SQLITE_PRI
4ced0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4cee0 42 74 72 65 65 46 69 72 73 74 28 42 74 43 75 72 BtreeFirst(BtCur
4cef0 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 sor*, int *pRes)
4cf00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4cf10 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4cf20 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 2a 2c eLast(BtCursor*,
4cf30 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c int *pRes);.SQL
4cf40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4cf50 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 sqlite3BtreeNext
4cf60 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 (BtCursor*, int
4cf70 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 *pRes);.SQLITE_P
4cf80 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4cf90 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 e3BtreeEof(BtCur
4cfa0 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 sor*);.SQLITE_PR
4cfb0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4cfc0 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 42 3BtreePrevious(B
4cfd0 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 tCursor*, int *p
4cfe0 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 Res);.SQLITE_PRI
4cff0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4d000 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74 43 BtreeKeySize(BtC
4d010 75 72 73 6f 72 2a 2c 20 69 36 34 20 2a 70 53 69 ursor*, i64 *pSi
4d020 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ze);.SQLITE_PRIV
4d030 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4d040 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72 treeKey(BtCursor
4d050 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 *, u32 offset, u
4d060 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 32 amt, void*);.
4d070 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
4d080 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
4d090 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 e3BtreeKeyFetch(
4d0a0 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a BtCursor*, int *
4d0b0 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 pAmt);.SQLITE_PR
4d0c0 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 IVATE const void
4d0d0 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 *sqlite3BtreeDa
4d0e0 74 61 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 taFetch(BtCursor
4d0f0 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 *, int *pAmt);.S
4d100 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4d110 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 t sqlite3BtreeDa
4d120 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a taSize(BtCursor*
4d130 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 3b 0a 53 , u32 *pSize);.S
4d140 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4d150 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 t sqlite3BtreeDa
4d160 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 ta(BtCursor*, u3
4d170 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 2 offset, u32 am
4d180 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 t, void*);.SQLIT
4d190 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
4d1a0 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 qlite3BtreeSetCa
4d1b0 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 73 chedRowid(BtCurs
4d1c0 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 or*, sqlite3_int
4d1d0 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 64);.SQLITE_PRIV
4d1e0 41 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 ATE sqlite3_int6
4d1f0 34 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 4 sqlite3BtreeGe
4d200 74 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 tCachedRowid(BtC
4d210 75 72 73 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 ursor*);..SQLITE
4d220 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
4d230 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 qlite3BtreeInteg
4d240 72 69 74 79 43 68 65 63 6b 28 42 74 72 65 65 2a rityCheck(Btree*
4d250 2c 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 69 6e , int *aRoot, in
4d260 74 20 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20 69 6e t nRoot, int, in
4d270 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
4d280 41 54 45 20 73 74 72 75 63 74 20 50 61 67 65 72 ATE struct Pager
4d290 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 *sqlite3BtreePa
4d2a0 67 65 72 28 42 74 72 65 65 2a 29 3b 0a 0a 53 51 ger(Btree*);..SQ
4d2b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4d2c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74 sqlite3BtreePut
4d2d0 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 Data(BtCursor*,
4d2e0 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 u32 offset, u32
4d2f0 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c amt, void*);.SQL
4d300 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
4d310 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 sqlite3BtreeCac
4d320 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72 heOverflow(BtCur
4d330 73 6f 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 sor *);.SQLITE_P
4d340 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
4d350 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 te3BtreeClearCur
4d360 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 29 3b sor(BtCursor *);
4d370 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
4d380 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4d390 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4d3a0 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 42 74 CursorIsValid(Bt
4d3b0 43 75 72 73 6f 72 2a 29 3b 0a 23 65 6e 64 69 66 Cursor*);.#endif
4d3c0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
4d3d0 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 _OMIT_BTREECOUNT
4d3e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4d3f0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4d400 43 6f 75 6e 74 28 42 74 43 75 72 73 6f 72 20 2a Count(BtCursor *
4d410 2c 20 69 36 34 20 2a 29 3b 0a 23 65 6e 64 69 66 , i64 *);.#endif
4d420 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
4d430 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 TEST.SQLITE_PRIV
4d440 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4d450 74 72 65 65 43 75 72 73 6f 72 49 6e 66 6f 28 42 treeCursorInfo(B
4d460 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 2c 20 tCursor*, int*,
4d470 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
4d480 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
4d490 33 42 74 72 65 65 43 75 72 73 6f 72 4c 69 73 74 3BtreeCursorList
4d4a0 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64 69 66 (Btree*);.#endif
4d4b0 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 ../*.** If we ar
4d4c0 65 20 6e 6f 74 20 75 73 69 6e 67 20 73 68 61 72 e not using shar
4d4d0 65 64 20 63 61 63 68 65 2c 20 74 68 65 6e 20 74 ed cache, then t
4d4e0 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 here is no need
4d4f0 74 6f 0a 2a 2a 20 75 73 65 20 6d 75 74 65 78 65 to.** use mutexe
4d500 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 s to access the
4d510 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75 BtShared structu
4d520 72 65 73 2e 20 20 53 6f 20 6d 61 6b 65 20 74 68 res. So make th
4d530 65 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 4c e.** Enter and L
4d540 65 61 76 65 20 70 72 6f 63 65 64 75 72 65 73 20 eave procedures
4d550 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 6e 64 no-ops..*/.#ifnd
4d560 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
4d570 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 HARED_CACHE.SQLI
4d580 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
4d590 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e d sqlite3BtreeEn
4d5a0 74 65 72 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c ter(Btree*);.SQL
4d5b0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
4d5c0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 id sqlite3BtreeE
4d5d0 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 2a nterAll(sqlite3*
4d5e0 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e );.#else.# defin
4d5f0 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e e sqlite3BtreeEn
4d600 74 65 72 28 58 29 20 0a 23 20 64 65 66 69 6e 65 ter(X) .# define
4d610 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
4d620 65 72 41 6c 6c 28 58 29 0a 23 65 6e 64 69 66 0a erAll(X).#endif.
4d630 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
4d640 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
4d650 5f 43 41 43 48 45 29 20 26 26 20 53 51 4c 49 54 _CACHE) && SQLIT
4d660 45 5f 54 48 52 45 41 44 53 41 46 45 0a 53 51 4c E_THREADSAFE.SQL
4d670 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
4d680 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c id sqlite3BtreeL
4d690 65 61 76 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 eave(Btree*);.SQ
4d6a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
4d6b0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
4d6c0 45 6e 74 65 72 43 75 72 73 6f 72 28 42 74 43 75 EnterCursor(BtCu
4d6d0 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 rsor*);.SQLITE_P
4d6e0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
4d6f0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 lite3BtreeLeaveC
4d700 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 ursor(BtCursor*)
4d710 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4d720 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 void sqlite3B
4d730 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c treeLeaveAll(sql
4d740 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ite3*);.SQLITE_P
4d750 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
4d760 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 lite3BtreeMutexA
4d770 72 72 61 79 45 6e 74 65 72 28 42 74 72 65 65 4d rrayEnter(BtreeM
4d780 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c utexArray*);.SQL
4d790 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
4d7a0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d id sqlite3BtreeM
4d7b0 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 42 utexArrayLeave(B
4d7c0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a 29 treeMutexArray*)
4d7d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4d7e0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 void sqlite3B
4d7f0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e treeMutexArrayIn
4d800 73 65 72 74 28 42 74 72 65 65 4d 75 74 65 78 41 sert(BtreeMutexA
4d810 72 72 61 79 2a 2c 20 42 74 72 65 65 2a 29 3b 0a rray*, Btree*);.
4d820 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 #ifndef NDEBUG.
4d830 20 2f 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e /* These routin
4d840 65 73 20 61 72 65 20 75 73 65 64 20 69 6e 73 69 es are used insi
4d850 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 de assert() stat
4d860 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a ements only. */.
4d870 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
4d880 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4d890 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65 eHoldsMutex(Btre
4d8a0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
4d8b0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
4d8c0 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 3BtreeHoldsAllMu
4d8d0 74 65 78 65 73 28 73 71 6c 69 74 65 33 2a 29 3b texes(sqlite3*);
4d8e0 0a 23 65 6e 64 69 66 0a 23 65 6c 73 65 0a 0a 23 .#endif.#else..#
4d8f0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 define sqlite3B
4d900 74 72 65 65 4c 65 61 76 65 28 58 29 0a 23 20 64 treeLeave(X).# d
4d910 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 efine sqlite3Btr
4d920 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 58 29 eeEnterCursor(X)
4d930 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
4d940 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 3BtreeLeaveCurso
4d950 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 r(X).# define sq
4d960 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 lite3BtreeLeaveA
4d970 6c 6c 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 ll(X).# define s
4d980 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
4d990 41 72 72 61 79 45 6e 74 65 72 28 58 29 0a 23 20 ArrayEnter(X).#
4d9a0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 define sqlite3Bt
4d9b0 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 reeMutexArrayLea
4d9c0 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 ve(X).# define s
4d9d0 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
4d9e0 41 72 72 61 79 49 6e 73 65 72 74 28 58 2c 59 29 ArrayInsert(X,Y)
4d9f0 0a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ..# define sqlit
4da00 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
4da10 78 28 58 29 20 31 0a 23 20 64 65 66 69 6e 65 20 x(X) 1.# define
4da20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
4da30 73 41 6c 6c 4d 75 74 65 78 65 73 28 58 29 20 31 sAllMutexes(X) 1
4da40 0a 23 65 6e 64 69 66 0a 0a 0a 23 65 6e 64 69 66 .#endif...#endif
4da50 20 2f 2a 20 5f 42 54 52 45 45 5f 48 5f 20 2a 2f /* _BTREE_H_ */
4da60 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
4da70 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e 68 * End of btree.h
4da80 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
4da90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4daa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dab0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
4dac0 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
4dad0 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
4dae0 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a n sqliteInt.h **
4daf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4db00 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
4db10 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 2e 68 * Include vdbe.h
4db20 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
4db30 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a f sqliteInt.h **
4db40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4db50 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
4db60 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 * Begin file vdb
4db70 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
4db80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4db90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dba0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 /./*.** 2001 Sep
4dbb0 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 tember 15.**.**
4dbc0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
4dbd0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
4dbe0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
4dbf0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
4dc00 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
4dc10 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
4dc20 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
4dc30 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
4dc40 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
4dc50 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
4dc60 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
4dc70 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
4dc80 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
4dc90 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
4dca0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
4dcb0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
4dcc0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
4dcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dd10 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 61 64 ********.** Head
4dd20 65 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 er file for the
4dd30 56 69 72 74 75 61 6c 20 44 61 74 61 42 61 73 65 Virtual DataBase
4dd40 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29 0a 2a Engine (VDBE).*
4dd50 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 *.** This header
4dd60 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 defines the int
4dd70 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76 69 erface to the vi
4dd80 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 rtual database e
4dd90 6e 67 69 6e 65 0a 2a 2a 20 6f 72 20 56 44 42 45 ngine.** or VDBE
4dda0 2e 20 20 54 68 65 20 56 44 42 45 20 69 6d 70 6c . The VDBE impl
4ddb0 65 6d 65 6e 74 73 20 61 6e 20 61 62 73 74 72 61 ements an abstra
4ddc0 63 74 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 ct machine that
4ddd0 72 75 6e 73 20 61 0a 2a 2a 20 73 69 6d 70 6c 65 runs a.** simple
4dde0 20 70 72 6f 67 72 61 6d 20 74 6f 20 61 63 63 65 program to acce
4ddf0 73 73 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 ss and modify th
4de00 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 e underlying dat
4de10 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 abase..**.** $Id
4de20 3a 20 76 64 62 65 2e 68 2c 76 20 31 2e 31 34 32 : vdbe.h,v 1.142
4de30 20 32 30 30 39 2f 30 37 2f 32 34 20 31 37 3a 35 2009/07/24 17:5
4de40 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 8:53 danielk1977
4de50 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 Exp $.*/.#ifnde
4de60 66 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f 48 f _SQLITE_VDBE_H
4de70 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 _.#define _SQLIT
4de80 45 5f 56 44 42 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a E_VDBE_H_../*.**
4de90 20 41 20 73 69 6e 67 6c 65 20 56 44 42 45 20 69 A single VDBE i
4dea0 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75 s an opaque stru
4deb0 63 74 75 72 65 20 6e 61 6d 65 64 20 22 56 64 62 cture named "Vdb
4dec0 65 22 2e 20 20 4f 6e 6c 79 20 72 6f 75 74 69 6e e". Only routin
4ded0 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 6f 75 es.** in the sou
4dee0 72 63 65 20 66 69 6c 65 20 73 71 6c 69 74 65 56 rce file sqliteV
4def0 64 62 65 2e 63 20 61 72 65 20 61 6c 6c 6f 77 65 dbe.c are allowe
4df00 64 20 74 6f 20 73 65 65 20 74 68 65 20 69 6e 73 d to see the ins
4df10 69 64 65 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20 ides.** of this
4df20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 structure..*/.ty
4df30 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 pedef struct Vdb
4df40 65 20 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a 20 54 e Vdbe;../*.** T
4df50 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 he names of the
4df60 66 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65 73 20 following types
4df70 64 65 63 6c 61 72 65 64 20 69 6e 20 76 64 62 65 declared in vdbe
4df80 49 6e 74 2e 68 20 61 72 65 20 72 65 71 75 69 72 Int.h are requir
4df90 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 56 64 ed.** for the Vd
4dfa0 62 65 4f 70 20 64 65 66 69 6e 69 74 69 6f 6e 2e beOp definition.
4dfb0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
4dfc0 63 74 20 56 64 62 65 46 75 6e 63 20 56 64 62 65 ct VdbeFunc Vdbe
4dfd0 46 75 6e 63 3b 0a 74 79 70 65 64 65 66 20 73 74 Func;.typedef st
4dfe0 72 75 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a 74 79 ruct Mem Mem;.ty
4dff0 70 65 64 65 66 20 73 74 72 75 63 74 20 53 75 62 pedef struct Sub
4e000 50 72 6f 67 72 61 6d 20 53 75 62 50 72 6f 67 72 Program SubProgr
4e010 61 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e am;../*.** A sin
4e020 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 gle instruction
4e030 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d of the virtual m
4e040 61 63 68 69 6e 65 20 68 61 73 20 61 6e 20 6f 70 achine has an op
4e050 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 61 73 20 6d code.** and as m
4e060 61 6e 79 20 61 73 20 74 68 72 65 65 20 6f 70 65 any as three ope
4e070 72 61 6e 64 73 2e 20 20 54 68 65 20 69 6e 73 74 rands. The inst
4e080 72 75 63 74 69 6f 6e 20 69 73 20 72 65 63 6f 72 ruction is recor
4e090 64 65 64 0a 2a 2a 20 61 73 20 61 6e 20 69 6e 73 ded.** as an ins
4e0a0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
4e0b0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
4e0c0 3a 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 :.*/.struct Vdbe
4e0d0 4f 70 20 7b 0a 20 20 75 38 20 6f 70 63 6f 64 65 Op {. u8 opcode
4e0e0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 ; /* Wh
4e0f0 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 at operation to
4e100 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 73 69 67 perform */. sig
4e110 6e 65 64 20 63 68 61 72 20 70 34 74 79 70 65 3b ned char p4type;
4e120 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 50 /* One of the P
4e130 34 5f 78 78 78 20 63 6f 6e 73 74 61 6e 74 73 20 4_xxx constants
4e140 66 6f 72 20 70 34 20 2a 2f 0a 20 20 75 38 20 6f for p4 */. u8 o
4e150 70 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 pflags;
4e160 2f 2a 20 4e 6f 74 20 63 75 72 72 65 6e 74 6c 79 /* Not currently
4e170 20 75 73 65 64 20 2a 2f 0a 20 20 75 38 20 70 35 used */. u8 p5
4e180 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
4e190 2a 20 46 69 66 74 68 20 70 61 72 61 6d 65 74 65 * Fifth paramete
4e1a0 72 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 r is an unsigned
4e1b0 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 character */.
4e1c0 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 int p1;
4e1d0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70 65 /* First ope
4e1e0 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 32 rand */. int p2
4e1f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
4e200 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 Second paramete
4e210 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75 6d r (often the jum
4e220 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20 2a p destination) *
4e230 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20 20 20 20 /. int p3;
4e240 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
4e250 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 2a hird parameter *
4e260 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 /. union {
4e270 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 75 72 74 /* fourt
4e280 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 h parameter */.
4e290 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 int i;
4e2a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 /* Int
4e2b0 65 67 65 72 20 76 61 6c 75 65 20 69 66 20 70 34 eger value if p4
4e2c0 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 2a type==P4_INT32 *
4e2d0 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b 20 20 /. void *p;
4e2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4e2f0 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65 72 20 Generic pointer
4e300 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 */. char *z;
4e310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4e320 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61 Pointer to data
4e330 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63 68 61 for string (cha
4e340 72 20 61 72 72 61 79 29 20 74 79 70 65 73 20 2a r array) types *
4e350 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36 34 3b /. i64 *pI64;
4e360 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4e370 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 Used when p4type
4e380 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a 2f 0a is P4_INT64 */.
4e390 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52 65 61 double *pRea
4e3a0 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 l; /* Us
4e3b0 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
4e3c0 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20 20 20 s P4_REAL */.
4e3d0 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b FuncDef *pFunc;
4e3e0 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 /* Used
4e3f0 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 when p4type is P
4e400 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20 20 20 4_FUNCDEF */.
4e410 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 VdbeFunc *pVdbe
4e420 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65 64 20 Func; /* Used
4e430 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 when p4type is P
4e440 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a 20 20 4_VDBEFUNC */.
4e450 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
4e460 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 ; /* Used
4e470 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 when p4type is
4e480 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a 20 20 P4_COLLSEQ */.
4e490 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 Mem *pMem;
4e4a0 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 /* Used
4e4b0 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 when p4type is
4e4c0 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20 56 54 P4_MEM */. VT
4e4d0 61 62 6c 65 20 2a 70 56 74 61 62 3b 20 20 20 20 able *pVtab;
4e4e0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 /* Used whe
4e4f0 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 56 n p4type is P4_V
4e500 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79 49 6e TAB */. KeyIn
4e510 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 20 fo *pKeyInfo;
4e520 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 /* Used when p
4e530 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45 59 49 4type is P4_KEYI
4e540 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a NFO */. int *
4e550 61 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ai;
4e560 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 /* Used when p
4e570 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e 54 41 4type is P4_INTA
4e580 52 52 41 59 20 2a 2f 0a 20 20 20 20 53 75 62 50 RRAY */. SubP
4e590 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d rogram *pProgram
4e5a0 3b 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 ; /* Used when
4e5b0 70 34 74 79 70 65 20 69 73 20 50 34 5f 53 55 42 p4type is P4_SUB
4e5c0 50 52 4f 47 52 41 4d 20 2a 2f 0a 20 20 7d 20 70 PROGRAM */. } p
4e5d0 34 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 4;.#ifdef SQLITE
4e5e0 5f 44 45 42 55 47 0a 20 20 63 68 61 72 20 2a 7a _DEBUG. char *z
4e5f0 43 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 Comment;
4e600 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 6f 20 /* Comment to
4e610 69 6d 70 72 6f 76 65 20 72 65 61 64 61 62 69 6c improve readabil
4e620 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 ity */.#endif.#i
4e630 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c fdef VDBE_PROFIL
4e640 45 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 E. int cnt;
4e650 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4e660 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 Number of times
4e670 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e this instruction
4e680 20 77 61 73 20 65 78 65 63 75 74 65 64 20 2a 2f was executed */
4e690 0a 20 20 75 36 34 20 63 79 63 6c 65 73 3b 20 20 . u64 cycles;
4e6a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
4e6b0 6f 74 61 6c 20 74 69 6d 65 20 73 70 65 6e 74 20 otal time spent
4e6c0 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 executing this i
4e6d0 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23 65 nstruction */.#e
4e6e0 6e 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20 ndif.};.typedef
4e6f0 73 74 72 75 63 74 20 56 64 62 65 4f 70 20 56 64 struct VdbeOp Vd
4e700 62 65 4f 70 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 beOp;.../*.** A
4e710 73 75 62 2d 72 6f 75 74 69 6e 65 20 75 73 65 64 sub-routine used
4e720 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 to implement a
4e730 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e trigger program.
4e740 0a 2a 2f 0a 73 74 72 75 63 74 20 53 75 62 50 72 .*/.struct SubPr
4e750 6f 67 72 61 6d 20 7b 0a 20 20 56 64 62 65 4f 70 ogram {. VdbeOp
4e760 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20 20 *aOp;
4e770 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 /* Array
4e780 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 of opcodes for
4e790 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 sub-program */.
4e7a0 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20 int nOp;
4e7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4e7c0 2a 20 45 6c 65 6d 65 6e 74 73 20 69 6e 20 61 4f * Elements in aO
4e7d0 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 p[] */. int nMe
4e7e0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m;
4e7f0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
4e800 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 of memory cells
4e810 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 required */. i
4e820 6e 74 20 6e 43 73 72 3b 20 20 20 20 20 20 20 20 nt nCsr;
4e830 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4e840 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 Number of cursor
4e850 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 s required */.
4e860 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 int nRef;
4e870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4e880 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 Number of point
4e890 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 ers to this stru
4e8a0 63 74 75 72 65 20 2a 2f 0a 20 20 76 6f 69 64 20 cture */. void
4e8b0 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 *token;
4e8c0 20 20 20 20 20 20 20 20 20 2f 2a 20 69 64 20 74 /* id t
4e8d0 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 64 20 hat may be used
4e8e0 74 6f 20 72 65 63 75 72 73 69 76 65 20 74 72 69 to recursive tri
4e8f0 67 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ggers */.};../*.
4e900 2a 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76 65 72 ** A smaller ver
4e910 73 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70 20 75 sion of VdbeOp u
4e920 73 65 64 20 66 6f 72 20 74 68 65 20 56 64 62 65 sed for the Vdbe
4e930 41 64 64 4f 70 4c 69 73 74 28 29 20 66 75 6e 63 AddOpList() func
4e940 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 tion because.**
4e950 69 74 20 74 61 6b 65 73 20 75 70 20 6c 65 73 73 it takes up less
4e960 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72 75 63 space..*/.struc
4e970 74 20 56 64 62 65 4f 70 4c 69 73 74 20 7b 0a 20 t VdbeOpList {.
4e980 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 u8 opcode;
4e990 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 /* What ope
4e9a0 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 ration to perfor
4e9b0 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 m */. signed ch
4e9c0 61 72 20 70 31 3b 20 20 20 20 20 2f 2a 20 46 69 ar p1; /* Fi
4e9d0 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 rst operand */.
4e9e0 20 73 69 67 6e 65 64 20 63 68 61 72 20 70 32 3b signed char p2;
4e9f0 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 /* Second p
4ea00 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20 arameter (often
4ea10 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 the jump destina
4ea20 74 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67 6e 65 tion) */. signe
4ea30 64 20 63 68 61 72 20 70 33 3b 20 20 20 20 20 2f d char p3; /
4ea40 2a 20 54 68 69 72 64 20 70 61 72 61 6d 65 74 65 * Third paramete
4ea50 72 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 r */.};.typedef
4ea60 73 74 72 75 63 74 20 56 64 62 65 4f 70 4c 69 73 struct VdbeOpLis
4ea70 74 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f t VdbeOpList;../
4ea80 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c *.** Allowed val
4ea90 75 65 73 20 6f 66 20 56 64 62 65 4f 70 2e 70 34 ues of VdbeOp.p4
4eaa0 74 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 type.*/.#define
4eab0 50 34 5f 4e 4f 54 55 53 45 44 20 20 20 20 30 20 P4_NOTUSED 0
4eac0 20 20 2f 2a 20 54 68 65 20 50 34 20 70 61 72 61 /* The P4 para
4ead0 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65 meter is not use
4eae0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f d */.#define P4_
4eaf0 44 59 4e 41 4d 49 43 20 20 28 2d 31 29 20 20 2f DYNAMIC (-1) /
4eb00 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 * Pointer to a s
4eb10 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 tring obtained f
4eb20 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 rom sqliteMalloc
4eb30 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 () */.#define P4
4eb40 5f 53 54 41 54 49 43 20 20 20 28 2d 32 29 20 20 _STATIC (-2)
4eb50 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 /* Pointer to a
4eb60 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f static string */
4eb70 0a 23 64 65 66 69 6e 65 20 50 34 5f 43 4f 4c 4c .#define P4_COLL
4eb80 53 45 51 20 20 28 2d 34 29 20 20 2f 2a 20 50 34 SEQ (-4) /* P4
4eb90 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
4eba0 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 a CollSeq struc
4ebb0 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ture */.#define
4ebc0 50 34 5f 46 55 4e 43 44 45 46 20 20 28 2d 35 29 P4_FUNCDEF (-5)
4ebd0 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 /* P4 is a poi
4ebe0 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63 44 65 nter to a FuncDe
4ebf0 66 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 f structure */.#
4ec00 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 define P4_KEYINF
4ec10 4f 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 20 69 O (-6) /* P4 i
4ec20 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
4ec30 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 KeyInfo structu
4ec40 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 re */.#define P4
4ec50 5f 56 44 42 45 46 55 4e 43 20 28 2d 37 29 20 20 _VDBEFUNC (-7)
4ec60 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 /* P4 is a point
4ec70 65 72 20 74 6f 20 61 20 56 64 62 65 46 75 6e 63 er to a VdbeFunc
4ec80 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 structure */.#d
4ec90 65 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20 20 20 efine P4_MEM
4eca0 20 20 28 2d 38 29 20 20 2f 2a 20 50 34 20 69 73 (-8) /* P4 is
4ecb0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
4ecc0 4d 65 6d 2a 20 20 20 20 73 74 72 75 63 74 75 72 Mem* structur
4ecd0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f e */.#define P4_
4ece0 54 52 41 4e 53 49 45 4e 54 20 28 2d 39 29 20 2f TRANSIENT (-9) /
4ecf0 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 * P4 is a pointe
4ed00 72 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 r to a transient
4ed10 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 string */.#defi
4ed20 6e 65 20 50 34 5f 56 54 41 42 20 20 20 20 20 28 ne P4_VTAB (
4ed30 2d 31 30 29 20 2f 2a 20 50 34 20 69 73 20 61 20 -10) /* P4 is a
4ed40 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 pointer to an sq
4ed50 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 lite3_vtab struc
4ed60 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ture */.#define
4ed70 50 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d 31 31 P4_MPRINTF (-11
4ed80 29 20 2f 2a 20 50 34 20 69 73 20 61 20 73 74 72 ) /* P4 is a str
4ed90 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ing obtained fro
4eda0 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 m sqlite3_mprint
4edb0 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 f() */.#define P
4edc0 34 5f 52 45 41 4c 20 20 20 20 20 28 2d 31 32 29 4_REAL (-12)
4edd0 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d 62 /* P4 is a 64-b
4ede0 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e it floating poin
4edf0 74 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 t value */.#defi
4ee00 6e 65 20 50 34 5f 49 4e 54 36 34 20 20 20 20 28 ne P4_INT64 (
4ee10 2d 31 33 29 20 2f 2a 20 50 34 20 69 73 20 61 20 -13) /* P4 is a
4ee20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 64-bit signed in
4ee30 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 teger */.#define
4ee40 20 50 34 5f 49 4e 54 33 32 20 20 20 20 28 2d 31 P4_INT32 (-1
4ee50 34 29 20 2f 2a 20 50 34 20 69 73 20 61 20 33 32 4) /* P4 is a 32
4ee60 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 -bit signed inte
4ee70 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ger */.#define P
4ee80 34 5f 49 4e 54 41 52 52 41 59 20 28 2d 31 35 29 4_INTARRAY (-15)
4ee90 20 2f 2a 20 50 34 20 69 73 20 61 20 76 65 63 74 /* P4 is a vect
4eea0 6f 72 20 6f 66 20 33 32 2d 62 69 74 20 69 6e 74 or of 32-bit int
4eeb0 65 67 65 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 egers */.#define
4eec0 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 20 P4_SUBPROGRAM
4eed0 28 2d 31 38 29 20 2f 2a 20 50 34 20 69 73 20 61 (-18) /* P4 is a
4eee0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 75 pointer to a Su
4eef0 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75 bProgram structu
4ef00 72 65 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 20 61 re */../* When a
4ef10 64 64 69 6e 67 20 61 20 50 34 20 61 72 67 75 6d dding a P4 argum
4ef20 65 6e 74 20 75 73 69 6e 67 20 50 34 5f 4b 45 59 ent using P4_KEY
4ef30 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 6f 66 20 INFO, a copy of
4ef40 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 the KeyInfo stru
4ef50 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d 61 64 65 cture.** is made
4ef60 2e 20 20 54 68 61 74 20 63 6f 70 79 20 69 73 20 . That copy is
4ef70 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 freed when the V
4ef80 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 dbe is finalized
4ef90 2e 20 20 42 75 74 20 69 66 20 74 68 65 0a 2a 2a . But if the.**
4efa0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 50 34 5f argument is P4_
4efb0 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c KEYINFO_HANDOFF,
4efc0 20 74 68 65 20 70 61 73 73 65 64 20 69 6e 20 70 the passed in p
4efd0 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 2e 20 ointer is used.
4efe0 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 67 65 74 It still.** get
4eff0 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 s freed when the
4f000 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a Vdbe is finaliz
4f010 65 64 20 73 6f 20 69 74 20 73 74 69 6c 6c 20 73 ed so it still s
4f020 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 69 6e 65 hould be obtaine
4f030 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69 6e 67 d.** from a sing
4f040 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 le sqliteMalloc(
4f050 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70 79 20 ). But no copy
4f060 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 65 20 is made and the
4f070 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 calling.** funct
4f080 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a ion should *not*
4f090 20 74 72 79 20 74 6f 20 66 72 65 65 20 74 68 65 try to free the
4f0a0 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 65 KeyInfo..*/.#de
4f0b0 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f fine P4_KEYINFO_
4f0c0 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 0a 23 64 HANDOFF (-16).#d
4f0d0 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f efine P4_KEYINFO
4f0e0 5f 53 54 41 54 49 43 20 20 28 2d 31 37 29 0a 0a _STATIC (-17)..
4f0f0 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 2e 61 /*.** The Vdbe.a
4f100 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79 20 63 6f ColName array co
4f110 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d 20 73 74 ntains 5n Mem st
4f120 72 75 63 74 75 72 65 73 2c 20 77 68 65 72 65 20 ructures, where
4f130 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20 6e 75 6d n is the .** num
4f140 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f ber of columns o
4f150 66 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 20 f data returned
4f160 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 by the statement
4f170 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4f 4c ..*/.#define COL
4f180 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20 20 30 0a NAME_NAME 0.
4f190 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f #define COLNAME_
4f1a0 44 45 43 4c 54 59 50 45 20 31 0a 23 64 65 66 69 DECLTYPE 1.#defi
4f1b0 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 ne COLNAME_DATAB
4f1c0 41 53 45 20 32 0a 23 64 65 66 69 6e 65 20 43 4f ASE 2.#define CO
4f1d0 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20 20 20 33 LNAME_TABLE 3
4f1e0 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 .#define COLNAME
4f1f0 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 69 66 64 _COLUMN 4.#ifd
4f200 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
4f210 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 _COLUMN_METADATA
4f220 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d .# define COLNAM
4f230 45 5f 4e 20 20 20 20 20 20 20 20 35 20 20 20 20 E_N 5
4f240 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 43 /* Number of C
4f250 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d 62 6f OLNAME_xxx symbo
4f260 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 20 69 66 ls */.#else.# if
4f270 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
4f280 44 45 43 4c 54 59 50 45 0a 23 20 20 20 64 65 66 DECLTYPE.# def
4f290 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 ine COLNAME_N
4f2a0 20 20 20 31 20 20 20 20 20 20 2f 2a 20 53 74 6f 1 /* Sto
4f2b0 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61 6d 65 re only the name
4f2c0 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 20 20 64 */.# else.# d
4f2d0 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 efine COLNAME_N
4f2e0 20 20 20 20 20 32 20 20 20 20 20 20 2f 2a 20 53 2 /* S
4f2f0 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 20 61 6e tore the name an
4f300 64 20 64 65 63 6c 74 79 70 65 20 2a 2f 0a 23 20 d decltype */.#
4f310 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a endif.#endif../*
4f320 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
4f330 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72 74 73 g macro converts
4f340 20 61 20 72 65 6c 61 74 69 76 65 20 61 64 64 72 a relative addr
4f350 65 73 73 20 69 6e 20 74 68 65 20 70 32 20 66 69 ess in the p2 fi
4f360 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64 62 65 eld.** of a Vdbe
4f370 4f 70 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 Op structure int
4f380 6f 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d o a negative num
4f390 62 65 72 20 73 6f 20 74 68 61 74 20 0a 2a 2a 20 ber so that .**
4f3a0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
4f3b0 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20 74 68 61 List() knows tha
4f3c0 74 20 74 68 65 20 61 64 64 72 65 73 73 20 69 73 t the address is
4f3d0 20 72 65 6c 61 74 69 76 65 2e 20 20 43 61 6c 6c relative. Call
4f3e0 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63 72 6f ing.** the macro
4f3f0 20 61 67 61 69 6e 20 72 65 73 74 6f 72 65 73 20 again restores
4f400 74 68 65 20 61 64 64 72 65 73 73 2e 0a 2a 2f 0a the address..*/.
4f410 23 64 65 66 69 6e 65 20 41 44 44 52 28 58 29 20 #define ADDR(X)
4f420 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a (-1-(X))../*.**
4f430 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 73 63 The makefile sc
4f440 61 6e 73 20 74 68 65 20 76 64 62 65 2e 63 20 73 ans the vdbe.c s
4f450 6f 75 72 63 65 20 66 69 6c 65 20 61 6e 64 20 63 ource file and c
4f460 72 65 61 74 65 73 20 74 68 65 20 22 6f 70 63 6f reates the "opco
4f470 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64 65 72 des.h".** header
4f480 20 66 69 6c 65 20 74 68 61 74 20 64 65 66 69 6e file that defin
4f490 65 73 20 61 20 6e 75 6d 62 65 72 20 66 6f 72 20 es a number for
4f4a0 65 61 63 68 20 6f 70 63 6f 64 65 20 75 73 65 64 each opcode used
4f4b0 20 62 79 20 74 68 65 20 56 44 42 45 2e 0a 2a 2f by the VDBE..*/
4f4c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
4f4d0 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64 65 73 Include opcodes
4f4e0 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
4f4f0 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a of vdbe.h *****
4f500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
4f510 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
4f520 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f Begin file opco
4f530 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a des.h **********
4f540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
4f560 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c ./* Automaticall
4f570 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f y generated. Do
4f580 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 not edit */./*
4f590 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 See the mkopcode
4f5a0 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 h.awk script for
4f5b0 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64 65 66 details */.#def
4f5c0 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20 20 20 ine OP_VNext
4f5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f5e0 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 1.#d
4f5f0 65 66 69 6e 65 20 4f 50 5f 41 66 66 69 6e 69 74 efine OP_Affinit
4f600 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y
4f610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 2.
4f620 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 75 6d #define OP_Colum
4f630 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
4f640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f650 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 74 3.#define OP_Set
4f660 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20 Cookie
4f670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f680 20 20 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 4.#define OP_S
4f690 65 65 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 eek
4f6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f6b0 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 4f 50 5.#define OP
4f6c0 5f 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20 _Real
4f6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f6e0 20 20 20 20 31 33 30 20 20 20 2f 2a 20 73 61 6d 130 /* sam
4f6f0 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20 20 20 e as TK_FLOAT
4f700 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 */.#define OP_S
4f710 65 71 75 65 6e 63 65 20 20 20 20 20 20 20 20 20 equence
4f720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f730 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 4f 50 6.#define OP
4f740 5f 53 61 76 65 70 6f 69 6e 74 20 20 20 20 20 20 _Savepoint
4f750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f760 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 7.#define
4f770 4f 50 5f 47 65 20 20 20 20 20 20 20 20 20 20 20 OP_Ge
4f780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f790 20 20 20 20 20 20 20 37 38 20 20 20 2f 2a 20 73 78 /* s
4f7a0 61 6d 65 20 61 73 20 54 4b 5f 47 45 20 20 20 20 ame as TK_GE
4f7b0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
4f7c0 5f 52 6f 77 4b 65 79 20 20 20 20 20 20 20 20 20 _RowKey
4f7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f7e0 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 8.#define
4f7f0 4f 50 5f 53 43 6f 70 79 20 20 20 20 20 20 20 20 OP_SCopy
4f800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f810 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 9.#defin
4f820 65 20 4f 50 5f 45 71 20 20 20 20 20 20 20 20 20 e OP_Eq
4f830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f840 20 20 20 20 20 20 20 20 20 37 34 20 20 20 2f 2a 74 /*
4f850 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 20 20 same as TK_EQ
4f860 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
4f870 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 20 20 20 OP_OpenWrite
4f880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f890 20 20 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e 10.#defin
4f8a0 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20 20 20 e OP_NotNull
4f8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f8c0 20 20 20 20 20 20 20 20 20 37 32 20 20 20 2f 2a 72 /*
4f8d0 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e same as TK_NOTN
4f8e0 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ULL */.#define
4f8f0 4f 50 5f 49 66 20 20 20 20 20 20 20 20 20 20 20 OP_If
4f900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f910 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e 11.#defin
4f920 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 20 e OP_ToInt
4f930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f940 20 20 20 20 20 20 20 20 31 34 34 20 20 20 2f 2a 144 /*
4f950 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 same as TK_TO_I
4f960 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 NT */.#define
4f970 4f 50 5f 53 74 72 69 6e 67 38 20 20 20 20 20 20 OP_String8
4f980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f990 20 20 20 20 20 20 20 39 34 20 20 20 2f 2a 20 73 94 /* s
4f9a0 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 4e 47 ame as TK_STRING
4f9b0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
4f9c0 5f 43 6f 6c 6c 53 65 71 20 20 20 20 20 20 20 20 _CollSeq
4f9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f9e0 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20 12.#define
4f9f0 4f 50 5f 4f 70 65 6e 52 65 61 64 20 20 20 20 20 OP_OpenRead
4fa00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fa10 20 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 13.#defin
4fa20 65 20 4f 50 5f 45 78 70 69 72 65 20 20 20 20 20 e OP_Expire
4fa30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fa40 20 20 20 20 20 20 20 20 20 31 34 0a 23 64 65 66 14.#def
4fa50 69 6e 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 ine OP_AutoCommi
4fa60 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
4fa70 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 64 15.#d
4fa80 65 66 69 6e 65 20 4f 50 5f 47 74 20 20 20 20 20 efine OP_Gt
4fa90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4faa0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 35 20 75
4fab0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
4fac0 47 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 GT */.#def
4fad0 69 6e 65 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 ine OP_Pagecount
4fae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4faf0 20 20 20 20 20 20 20 20 20 20 20 31 36 0a 23 64 16.#d
4fb00 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 72 69 efine OP_Integri
4fb10 74 79 43 6b 20 20 20 20 20 20 20 20 20 20 20 20 tyCk
4fb20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 37 0a 17.
4fb30 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72 74 20 #define OP_Sort
4fb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4fb60 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 70 8.#define OP_Cop
4fb70 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y
4fb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb90 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 20.#define OP_T
4fba0 72 61 63 65 20 20 20 20 20 20 20 20 20 20 20 20 race
4fbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fbc0 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20 4f 50 21.#define OP
4fbd0 5f 46 75 6e 63 74 69 6f 6e 20 20 20 20 20 20 20 _Function
4fbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fbf0 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20 22.#define
4fc00 4f 50 5f 49 66 4e 65 67 20 20 20 20 20 20 20 20 OP_IfNeg
4fc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc20 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 69 6e 23.#defin
4fc30 65 20 4f 50 5f 41 6e 64 20 20 20 20 20 20 20 20 e OP_And
4fc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc50 20 20 20 20 20 20 20 20 20 36 37 20 20 20 2f 2a 67 /*
4fc60 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 20 same as TK_AND
4fc70 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
4fc80 4f 50 5f 53 75 62 74 72 61 63 74 20 20 20 20 20 OP_Subtract
4fc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fca0 20 20 20 20 20 20 20 38 35 20 20 20 2f 2a 20 73 85 /* s
4fcb0 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 20 ame as TK_MINUS
4fcc0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
4fcd0 5f 4e 6f 6f 70 20 20 20 20 20 20 20 20 20 20 20 _Noop
4fce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fcf0 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20 24.#define
4fd00 4f 50 5f 50 72 6f 67 72 61 6d 20 20 20 20 20 20 OP_Program
4fd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd20 20 20 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e 25.#defin
4fd30 65 20 4f 50 5f 52 65 74 75 72 6e 20 20 20 20 20 e OP_Return
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 20 20 32 36 0a 23 64 65 66 26.#def
4fd60 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 ine OP_Remainder
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 20 20 38 38 20 20 20 88
4fd90 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 45 /* same as TK_RE
4fda0 4d 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e M */.#defin
4fdb0 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 20 20 e OP_NewRowid
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 20 20 32 37 0a 23 64 65 66 27.#def
4fde0 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 20 ine OP_Multiply
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 20 20 38 36 20 20 20 86
4fe10 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 /* same as TK_ST
4fe20 41 52 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e AR */.#defin
4fe30 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 20 20 e OP_Variable
4fe40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe50 20 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 28.#def
4fe60 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67 20 20 20 ine OP_String
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 20 20 32 39 0a 23 64 29.#d
4fe90 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 41 66 66 efine OP_RealAff
4fea0 69 6e 69 74 79 20 20 20 20 20 20 20 20 20 20 20 inity
4feb0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 30 0a 30.
4fec0 23 64 65 66 69 6e 65 20 4f 50 5f 56 52 65 6e 61 #define OP_VRena
4fed0 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 me
4fee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
4fef0 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 72 1.#define OP_Par
4ff00 73 65 53 63 68 65 6d 61 20 20 20 20 20 20 20 20 seSchema
4ff10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff20 20 33 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 32.#define OP_V
4ff30 4f 70 65 6e 20 20 20 20 20 20 20 20 20 20 20 20 Open
4ff40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff50 20 20 20 33 33 0a 23 64 65 66 69 6e 65 20 4f 50 33.#define OP
4ff60 5f 43 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 _Close
4ff70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff80 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e 65 20 34.#define
4ff90 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 20 20 OP_CreateIndex
4ffa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ffb0 20 20 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e 35.#defin
4ffc0 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 20 20 e OP_IsUnique
4ffd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ffe0 20 20 20 20 20 20 20 20 20 33 36 0a 23 64 65 66 36.#def
4fff0 69 6e 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 ine OP_NotFound
50000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50010 20 20 20 20 20 20 20 20 20 20 20 33 37 0a 23 64 37.#d
50020 65 66 69 6e 65 20 4f 50 5f 49 6e 74 36 34 20 20 efine OP_Int64
50030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50040 20 20 20 20 20 20 20 20 20 20 20 20 20 33 38 0a 38.
50050 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75 73 74 42 #define OP_MustB
50060 65 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 eInt
50070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
50080 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 9.#define OP_Hal
50090 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
500a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
500b0 20 34 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 40.#define OP_R
500c0 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20 owid
500d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
500e0 20 20 20 34 31 0a 23 64 65 66 69 6e 65 20 4f 50 41.#define OP
500f0 5f 49 64 78 4c 54 20 20 20 20 20 20 20 20 20 20 _IdxLT
50100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50110 20 20 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20 42.#define
50120 4f 50 5f 41 64 64 49 6d 6d 20 20 20 20 20 20 20 OP_AddImm
50130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50140 20 20 20 20 20 20 20 34 33 0a 23 64 65 66 69 6e 43.#defin
50150 65 20 4f 50 5f 52 6f 77 44 61 74 61 20 20 20 20 e OP_RowData
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 34 34 0a 23 64 65 66 44.#def
50180 69 6e 65 20 4f 50 5f 4d 65 6d 4d 61 78 20 20 20 ine OP_MemMax
50190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
501a0 20 20 20 20 20 20 20 20 20 20 20 34 35 0a 23 64 45.#d
501b0 65 66 69 6e 65 20 4f 50 5f 4f 72 20 20 20 20 20 efine OP_Or
501c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
501d0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 36 20 66
501e0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
501f0 4f 52 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 OR */.#def
50200 69 6e 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 ine OP_NotExists
50210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50220 20 20 20 20 20 20 20 20 20 20 20 34 36 0a 23 64 46.#d
50230 65 66 69 6e 65 20 4f 50 5f 47 6f 73 75 62 20 20 efine OP_Gosub
50240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50250 20 20 20 20 20 20 20 20 20 20 20 20 20 34 37 0a 47.
50260 23 64 65 66 69 6e 65 20 4f 50 5f 44 69 76 69 64 #define OP_Divid
50270 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
50280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 8
50290 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 7 /* same as T
502a0 4b 5f 53 4c 41 53 48 20 20 20 20 2a 2f 0a 23 64 K_SLASH */.#d
502b0 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 65 72 efine OP_Integer
502c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
502d0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 38 0a 48.
502e0 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 4e 75 6d #define OP_ToNum
502f0 65 72 69 63 20 20 20 20 20 20 20 20 20 20 20 20 eric
50300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 14
50310 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 3 /* same as T
50320 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2a 2f 0a 23 K_TO_NUMERIC*/.#
50330 64 65 66 69 6e 65 20 4f 50 5f 50 72 65 76 20 20 define OP_Prev
50340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 39 49
50360 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53 .#define OP_RowS
50370 65 74 52 65 61 64 20 20 20 20 20 20 20 20 20 20 etRead
50380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50390 35 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 50.#define OP_Co
503a0 6e 63 61 74 20 20 20 20 20 20 20 20 20 20 20 20 ncat
503b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503c0 20 20 38 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 89 /* same a
503d0 73 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 2a 2f s TK_CONCAT */
503e0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53 .#define OP_RowS
503f0 65 74 41 64 64 20 20 20 20 20 20 20 20 20 20 20 etAdd
50400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50410 35 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 51.#define OP_Bi
50420 74 41 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 tAnd
50430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50440 20 20 38 30 20 20 20 2f 2a 20 73 61 6d 65 20 61 80 /* same a
50450 73 20 54 4b 5f 42 49 54 41 4e 44 20 20 20 2a 2f s TK_BITAND */
50460 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 6f 6c .#define OP_VCol
50470 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 umn
50480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50490 35 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 72 52.#define OP_Cr
504a0 65 61 74 65 54 61 62 6c 65 20 20 20 20 20 20 20 eateTable
504b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
504c0 20 20 35 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53.#define OP_
504d0 4c 61 73 74 20 20 20 20 20 20 20 20 20 20 20 20 Last
504e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
504f0 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65 20 4f 54.#define O
50500 50 5f 53 65 65 6b 4c 65 20 20 20 20 20 20 20 20 P_SeekLe
50510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50520 20 20 20 20 20 20 35 35 0a 23 64 65 66 69 6e 65 55.#define
50530 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 20 20 20 20 OP_IsNull
50540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50550 20 20 20 20 20 20 20 20 37 31 20 20 20 2f 2a 20 71 /*
50560 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55 4c same as TK_ISNUL
50570 4c 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f L */.#define O
50580 50 5f 49 6e 63 72 56 61 63 75 75 6d 20 20 20 20 P_IncrVacuum
50590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
505a0 20 20 20 20 20 20 35 36 0a 23 64 65 66 69 6e 65 56.#define
505b0 20 4f 50 5f 49 64 78 52 6f 77 69 64 20 20 20 20 OP_IdxRowid
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 35 37 0a 23 64 65 66 69 57.#defi
505e0 6e 65 20 4f 50 5f 53 68 69 66 74 52 69 67 68 74 ne OP_ShiftRight
505f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50600 20 20 20 20 20 20 20 20 20 20 38 33 20 20 20 2f 83 /
50610 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 53 48 * same as TK_RSH
50620 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 IFT */.#define
50630 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 20 20 OP_ResetCount
50640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50650 20 20 20 20 20 20 20 20 35 38 0a 23 64 65 66 69 58.#defi
50660 6e 65 20 4f 50 5f 59 69 65 6c 64 20 20 20 20 20 ne OP_Yield
50670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50680 20 20 20 20 20 20 20 20 20 20 35 39 0a 23 64 65 59.#de
50690 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 72 69 67 fine OP_DropTrig
506a0 67 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 ger
506b0 20 20 20 20 20 20 20 20 20 20 20 20 36 30 0a 23 60.#
506c0 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 49 6e define OP_DropIn
506d0 64 65 78 20 20 20 20 20 20 20 20 20 20 20 20 20 dex
506e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 31 61
506f0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 72 61 .#define OP_Para
50700 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m
50710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50720 36 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 62.#define OP_Id
50730 78 47 45 20 20 20 20 20 20 20 20 20 20 20 20 20 xGE
50740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50750 20 20 36 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 63.#define OP_
50760 49 64 78 44 65 6c 65 74 65 20 20 20 20 20 20 20 IdxDelete
50770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50780 20 20 20 20 36 34 0a 23 64 65 66 69 6e 65 20 4f 64.#define O
50790 50 5f 56 61 63 75 75 6d 20 20 20 20 20 20 20 20 P_Vacuum
507a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507b0 20 20 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65 65.#define
507c0 20 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20 20 20 OP_IfNot
507d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507e0 20 20 20 20 20 20 20 20 36 38 0a 23 64 65 66 69 68.#defi
507f0 6e 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 20 ne OP_DropTable
50800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50810 20 20 20 20 20 20 20 20 20 20 36 39 0a 23 64 65 69.#de
50820 66 69 6e 65 20 4f 50 5f 53 65 65 6b 4c 74 20 20 fine OP_SeekLt
50830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50840 20 20 20 20 20 20 20 20 20 20 20 20 37 30 0a 23 70.#
50850 64 65 66 69 6e 65 20 4f 50 5f 4d 61 6b 65 52 65 define OP_MakeRe
50860 63 6f 72 64 20 20 20 20 20 20 20 20 20 20 20 20 cord
50870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 39 79
50880 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 42 6c .#define OP_ToBl
50890 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ob
508a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
508b0 34 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 42 /* same as
508c0 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 2a 2f 0a 23 TK_TO_BLOB */.#
508d0 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 75 6c 74 define OP_Result
508e0 52 6f 77 20 20 20 20 20 20 20 20 20 20 20 20 20 Row
508f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 30 90
50900 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 6c 65 .#define OP_Dele
50910 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 te
50920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50930 39 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 67 91.#define OP_Ag
50940 67 46 69 6e 61 6c 20 20 20 20 20 20 20 20 20 20 gFinal
50950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50960 20 20 39 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 92.#define OP_
50970 43 6f 6d 70 61 72 65 20 20 20 20 20 20 20 20 20 Compare
50980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50990 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65 20 4f 95.#define O
509a0 50 5f 53 68 69 66 74 4c 65 66 74 20 20 20 20 20 P_ShiftLeft
509b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
509c0 20 20 20 20 20 20 38 32 20 20 20 2f 2a 20 73 61 82 /* sa
509d0 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49 46 54 20 me as TK_LSHIFT
509e0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
509f0 47 6f 74 6f 20 20 20 20 20 20 20 20 20 20 20 20 Goto
50a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a10 20 20 20 20 39 36 0a 23 64 65 66 69 6e 65 20 4f 96.#define O
50a20 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 20 20 20 20 P_TableLock
50a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a40 20 20 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65 97.#define
50a50 20 4f 50 5f 43 6c 65 61 72 20 20 20 20 20 20 20 OP_Clear
50a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a70 20 20 20 20 20 20 20 20 39 38 0a 23 64 65 66 69 98.#defi
50a80 6e 65 20 4f 50 5f 4c 65 20 20 20 20 20 20 20 20 ne OP_Le
50a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50aa0 20 20 20 20 20 20 20 20 20 20 37 36 20 20 20 2f 76 /
50ab0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45 20 * same as TK_LE
50ac0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
50ad0 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 OP_VerifyCookie
50ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50af0 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 66 69 99.#defi
50b00 6e 65 20 4f 50 5f 41 67 67 53 74 65 70 20 20 20 ne OP_AggStep
50b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b20 20 20 20 20 20 20 20 20 20 31 30 30 0a 23 64 65 100.#de
50b30 66 69 6e 65 20 4f 50 5f 54 6f 54 65 78 74 20 20 fine OP_ToText
50b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b50 20 20 20 20 20 20 20 20 20 20 20 31 34 31 20 20 141
50b60 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 /* same as TK_T
50b70 4f 5f 54 45 58 54 20 20 2a 2f 0a 23 64 65 66 69 O_TEXT */.#defi
50b80 6e 65 20 4f 50 5f 4e 6f 74 20 20 20 20 20 20 20 ne OP_Not
50b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ba0 20 20 20 20 20 20 20 20 20 20 31 39 20 20 20 2f 19 /
50bb0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 * same as TK_NOT
50bc0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
50bd0 20 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 20 20 OP_ToReal
50be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50bf0 20 20 20 20 20 20 20 31 34 35 20 20 20 2f 2a 20 145 /*
50c00 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 52 45 same as TK_TO_RE
50c10 41 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f AL */.#define O
50c20 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 20 20 P_Transaction
50c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c40 20 20 20 20 20 31 30 31 0a 23 64 65 66 69 6e 65 101.#define
50c50 20 4f 50 5f 56 46 69 6c 74 65 72 20 20 20 20 20 OP_VFilter
50c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c70 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 66 69 102.#defi
50c80 6e 65 20 4f 50 5f 4e 65 20 20 20 20 20 20 20 20 ne OP_Ne
50c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ca0 20 20 20 20 20 20 20 20 20 20 37 33 20 20 20 2f 73 /
50cb0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 20 * same as TK_NE
50cc0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
50cd0 20 4f 50 5f 56 44 65 73 74 72 6f 79 20 20 20 20 OP_VDestroy
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 31 30 33 0a 23 64 65 66 69 103.#defi
50d00 6e 65 20 4f 50 5f 42 69 74 4f 72 20 20 20 20 20 ne OP_BitOr
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 38 31 20 20 20 2f 81 /
50d30 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 * same as TK_BIT
50d40 4f 52 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 OR */.#define
50d50 20 4f 50 5f 4e 65 78 74 20 20 20 20 20 20 20 20 OP_Next
50d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d70 20 20 20 20 20 20 20 31 30 34 0a 23 64 65 66 69 104.#defi
50d80 6e 65 20 4f 50 5f 43 6f 75 6e 74 20 20 20 20 20 ne OP_Count
50d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50da0 20 20 20 20 20 20 20 20 20 31 30 35 0a 23 64 65 105.#de
50db0 66 69 6e 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 fine OP_IdxInser
50dc0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
50dd0 20 20 20 20 20 20 20 20 20 20 20 31 30 36 0a 23 106.#
50de0 64 65 66 69 6e 65 20 4f 50 5f 4c 74 20 20 20 20 define OP_Lt
50df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 37 77
50e10 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
50e20 5f 4c 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 _LT */.#de
50e30 66 69 6e 65 20 4f 50 5f 53 65 65 6b 47 65 20 20 fine OP_SeekGe
50e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e50 20 20 20 20 20 20 20 20 20 20 20 31 30 37 0a 23 107.#
50e60 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74 define OP_Insert
50e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e80 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 38 108
50e90 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 73 74 .#define OP_Dest
50ea0 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 20 20 roy
50eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
50ec0 30 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 09.#define OP_Re
50ed0 61 64 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 adCookie
50ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ef0 20 31 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 110.#define OP_
50f00 52 6f 77 53 65 74 54 65 73 74 20 20 20 20 20 20 RowSetTest
50f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f20 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65 20 4f 111.#define O
50f30 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 20 P_LoadAnalysis
50f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f50 20 20 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65 112.#define
50f60 20 4f 50 5f 45 78 70 6c 61 69 6e 20 20 20 20 20 OP_Explain
50f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f80 20 20 20 20 20 20 20 31 31 33 0a 23 64 65 66 69 113.#defi
50f90 6e 65 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c ne OP_HaltIfNull
50fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50fb0 20 20 20 20 20 20 20 20 20 31 31 34 0a 23 64 65 114.#de
50fc0 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75 fine OP_OpenPseu
50fd0 64 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 do
50fe0 20 20 20 20 20 20 20 20 20 20 20 31 31 35 0a 23 115.#
50ff0 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 45 70 define OP_OpenEp
51000 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 20 20 hemeral
51010 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 36 116
51020 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75 6c 6c .#define OP_Null
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51050 31 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 17.#define OP_Mo
51060 76 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ve
51070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51080 20 31 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 118.#define OP_
51090 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 Blob
510a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
510b0 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65 20 4f 119.#define O
510c0 50 5f 41 64 64 20 20 20 20 20 20 20 20 20 20 20 P_Add
510d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
510e0 20 20 20 20 20 20 38 34 20 20 20 2f 2a 20 73 61 84 /* sa
510f0 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 20 20 20 me as TK_PLUS
51100 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
51110 52 65 77 69 6e 64 20 20 20 20 20 20 20 20 20 20 Rewind
51120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51130 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65 20 4f 120.#define O
51140 50 5f 53 65 65 6b 47 74 20 20 20 20 20 20 20 20 P_SeekGt
51150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51160 20 20 20 20 20 31 32 31 0a 23 64 65 66 69 6e 65 121.#define
51170 20 4f 50 5f 56 42 65 67 69 6e 20 20 20 20 20 20 OP_VBegin
51180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51190 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 66 69 122.#defi
511a0 6e 65 20 4f 50 5f 56 55 70 64 61 74 65 20 20 20 ne OP_VUpdate
511b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
511c0 20 20 20 20 20 20 20 20 20 31 32 33 0a 23 64 65 123.#de
511d0 66 69 6e 65 20 4f 50 5f 49 66 5a 65 72 6f 20 20 fine OP_IfZero
511e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
511f0 20 20 20 20 20 20 20 20 20 20 20 31 32 34 0a 23 124.#
51200 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4e 6f 74 define OP_BitNot
51210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 33 93
51230 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
51240 5f 42 49 54 4e 4f 54 20 20 20 2a 2f 0a 23 64 65 _BITNOT */.#de
51250 66 69 6e 65 20 4f 50 5f 56 43 72 65 61 74 65 20 fine OP_VCreate
51260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51270 20 20 20 20 20 20 20 20 20 20 20 31 32 35 0a 23 125.#
51280 64 65 66 69 6e 65 20 4f 50 5f 46 6f 75 6e 64 20 define OP_Found
51290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
512a0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 36 126
512b0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 50 6f .#define OP_IfPo
512c0 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s
512d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
512e0 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75 27.#define OP_Nu
512f0 6c 6c 52 6f 77 20 20 20 20 20 20 20 20 20 20 20 llRow
51300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51310 20 31 32 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 128.#define OP_
51320 4a 75 6d 70 20 20 20 20 20 20 20 20 20 20 20 20 Jump
51330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51340 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65 20 4f 129.#define O
51350 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 20 20 P_Permutation
51360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51370 20 20 20 20 20 31 33 31 0a 0a 2f 2a 20 54 68 65 131../* The
51380 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63 6f 64 following opcod
51390 65 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65 76 e values are nev
513a0 65 72 20 75 73 65 64 20 2a 2f 0a 23 64 65 66 69 er used */.#defi
513b0 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 ne OP_NotUsed_13
513c0 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2
513d0 20 20 20 20 20 20 20 20 20 31 33 32 0a 23 64 65 132.#de
513e0 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f fine OP_NotUsed_
513f0 31 33 33 20 20 20 20 20 20 20 20 20 20 20 20 20 133
51400 20 20 20 20 20 20 20 20 20 20 20 31 33 33 0a 23 133.#
51410 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 define OP_NotUse
51420 64 5f 31 33 34 20 20 20 20 20 20 20 20 20 20 20 d_134
51430 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34 134
51440 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 .#define OP_NotU
51450 73 65 64 5f 31 33 35 20 20 20 20 20 20 20 20 20 sed_135
51460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51470 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 35.#define OP_No
51480 74 55 73 65 64 5f 31 33 36 20 20 20 20 20 20 20 tUsed_136
51490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
514a0 20 31 33 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 136.#define OP_
514b0 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 20 20 20 NotUsed_137
514c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
514d0 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65 20 4f 137.#define O
514e0 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38 20 20 20 P_NotUsed_138
514f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51500 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e 65 138.#define
51510 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 39 20 OP_NotUsed_139
51520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51530 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 66 69 139.#defi
51540 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 34 ne OP_NotUsed_14
51550 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
51560 20 20 20 20 20 20 20 20 20 31 34 30 0a 0a 0a 2f 140.../
51570 2a 20 50 72 6f 70 65 72 74 69 65 73 20 73 75 63 * Properties suc
51580 68 20 61 73 20 22 6f 75 74 32 22 20 6f 72 20 22 h as "out2" or "
51590 6a 75 6d 70 22 20 74 68 61 74 20 61 72 65 20 73 jump" that are s
515a0 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a 20 63 pecified in.** c
515b0 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e omments followin
515c0 67 20 74 68 65 20 22 63 61 73 65 22 20 66 6f 72 g the "case" for
515d0 20 65 61 63 68 20 6f 70 63 6f 64 65 20 69 6e 20 each opcode in
515e0 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a 20 61 72 the vdbe.c.** ar
515f0 65 20 65 6e 63 6f 64 65 64 20 69 6e 74 6f 20 62 e encoded into b
51600 69 74 76 65 63 74 6f 72 73 20 61 73 20 66 6f 6c itvectors as fol
51610 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 lows:.*/.#define
51620 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20 20 20 20 OPFLG_JUMP
51630 20 20 20 20 20 20 20 30 78 30 30 30 31 20 20 2f 0x0001 /
51640 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68 6f 6c 64 * jump: P2 hold
51650 73 20 6a 6d 70 20 74 61 72 67 65 74 20 2a 2f 0a s jmp target */.
51660 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 #define OPFLG_OU
51670 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20 30 78 T2_PRERELEASE 0x
51680 30 30 30 32 20 20 2f 2a 20 6f 75 74 32 2d 70 72 0002 /* out2-pr
51690 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a 23 64 65 erelease: */.#de
516a0 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 31 20 20 fine OPFLG_IN1
516b0 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 0x000
516c0 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20 50 31 20 4 /* in1: P1
516d0 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 is an input */.#
516e0 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 32 define OPFLG_IN2
516f0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 0x0
51700 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20 20 20 50 008 /* in2: P
51710 32 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 2 is an input */
51720 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 .#define OPFLG_I
51730 4e 33 20 20 20 20 20 20 20 20 20 20 20 20 20 30 N3 0
51740 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 3a 20 20 x0010 /* in3:
51750 20 50 33 20 69 73 20 61 6e 20 69 6e 70 75 74 20 P3 is an input
51760 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 */.#define OPFLG
51770 5f 4f 55 54 33 20 20 20 20 20 20 20 20 20 20 20 _OUT3
51780 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 75 74 33 0x0020 /* out3
51790 3a 20 20 50 33 20 69 73 20 61 6e 20 6f 75 74 70 : P3 is an outp
517a0 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 ut */.#define OP
517b0 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 FLG_INITIALIZER
517c0 7b 5c 0a 2f 2a 20 20 20 30 20 2a 2f 20 30 78 30 {\./* 0 */ 0x0
517d0 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 0, 0x01, 0x00, 0
517e0 78 30 30 2c 20 30 78 31 30 2c 20 30 78 30 38 2c x00, 0x10, 0x08,
517f0 20 30 78 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 0x02, 0x00,\./*
51800 20 20 20 38 20 2a 2f 20 30 78 30 30 2c 20 30 78 8 */ 0x00, 0x
51810 30 34 2c 20 30 78 30 30 2c 20 30 78 30 35 2c 20 04, 0x00, 0x05,
51820 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
51830 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 31 36 20 , 0x00,\./* 16
51840 2a 2f 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 */ 0x02, 0x00, 0
51850 78 30 31 2c 20 30 78 30 34 2c 20 30 78 30 34 2c x01, 0x04, 0x04,
51860 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
51870 35 2c 5c 0a 2f 2a 20 20 32 34 20 2a 2f 20 30 78 5,\./* 24 */ 0x
51880 30 30 2c 20 30 78 30 31 2c 20 30 78 30 34 2c 20 00, 0x01, 0x04,
51890 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 32 0x02, 0x00, 0x02
518a0 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 5c 0a 2f , 0x04, 0x00,\./
518b0 2a 20 20 33 32 20 2a 2f 20 30 78 30 30 2c 20 30 * 32 */ 0x00, 0
518c0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 32 2c x00, 0x00, 0x02,
518d0 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30 78 30 0x11, 0x11, 0x0
518e0 32 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 20 34 30 2, 0x05,\./* 40
518f0 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 32 2c 20 */ 0x00, 0x02,
51900 30 78 31 31 2c 20 30 78 30 34 2c 20 30 78 30 30 0x11, 0x04, 0x00
51910 2c 20 30 78 30 38 2c 20 30 78 31 31 2c 20 30 78 , 0x08, 0x11, 0x
51920 30 31 2c 5c 0a 2f 2a 20 20 34 38 20 2a 2f 20 30 01,\./* 48 */ 0
51930 78 30 32 2c 20 30 78 30 31 2c 20 30 78 32 31 2c x02, 0x01, 0x21,
51940 20 30 78 30 38 2c 20 30 78 30 30 2c 20 30 78 30 0x08, 0x00, 0x0
51950 32 2c 20 30 78 30 31 2c 20 30 78 31 31 2c 5c 0a 2, 0x01, 0x11,\.
51960 2f 2a 20 20 35 36 20 2a 2f 20 30 78 30 31 2c 20 /* 56 */ 0x01,
51970 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 34 0x02, 0x00, 0x04
51980 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
51990 30 32 2c 20 30 78 31 31 2c 5c 0a 2f 2a 20 20 36 02, 0x11,\./* 6
519a0 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 4 */ 0x00, 0x00,
519b0 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 30 0x2c, 0x2c, 0x0
519c0 35 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 20 30 5, 0x00, 0x11, 0
519d0 78 30 35 2c 5c 0a 2f 2a 20 20 37 32 20 2a 2f 20 x05,\./* 72 */
519e0 30 78 30 35 2c 20 30 78 31 35 2c 20 30 78 31 35 0x05, 0x15, 0x15
519f0 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 , 0x15, 0x15, 0x
51a00 31 35 2c 20 30 78 31 35 2c 20 30 78 30 30 2c 5c 15, 0x15, 0x00,\
51a10 0a 2f 2a 20 20 38 30 20 2a 2f 20 30 78 32 63 2c ./* 80 */ 0x2c,
51a20 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 0x2c, 0x2c, 0x2
51a30 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 c, 0x2c, 0x2c, 0
51a40 78 32 63 2c 20 30 78 32 63 2c 5c 0a 2f 2a 20 20 x2c, 0x2c,\./*
51a50 38 38 20 2a 2f 20 30 78 32 63 2c 20 30 78 32 63 88 */ 0x2c, 0x2c
51a60 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
51a70 30 30 2c 20 30 78 30 34 2c 20 30 78 30 32 2c 20 00, 0x04, 0x02,
51a80 30 78 30 30 2c 5c 0a 2f 2a 20 20 39 36 20 2a 2f 0x00,\./* 96 */
51a90 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 0x01, 0x00, 0x0
51aa0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
51ab0 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c x00, 0x01, 0x00,
51ac0 5c 0a 2f 2a 20 31 30 34 20 2a 2f 20 30 78 30 31 \./* 104 */ 0x01
51ad0 2c 20 30 78 30 32 2c 20 30 78 30 38 2c 20 30 78 , 0x02, 0x08, 0x
51ae0 31 31 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20 11, 0x00, 0x02,
51af0 30 78 30 32 2c 20 30 78 31 35 2c 5c 0a 2f 2a 20 0x02, 0x15,\./*
51b00 31 31 32 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 112 */ 0x00, 0x0
51b10 30 2c 20 30 78 31 30 2c 20 30 78 30 30 2c 20 30 0, 0x10, 0x00, 0
51b20 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 30 2c x00, 0x02, 0x00,
51b30 20 30 78 30 32 2c 5c 0a 2f 2a 20 31 32 30 20 2a 0x02,\./* 120 *
51b40 2f 20 30 78 30 31 2c 20 30 78 31 31 2c 20 30 78 / 0x01, 0x11, 0x
51b50 30 30 2c 20 30 78 30 30 2c 20 30 78 30 35 2c 20 00, 0x00, 0x05,
51b60 30 78 30 30 2c 20 30 78 31 31 2c 20 30 78 30 35 0x00, 0x11, 0x05
51b70 2c 5c 0a 2f 2a 20 31 32 38 20 2a 2f 20 30 78 30 ,\./* 128 */ 0x0
51b80 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 0, 0x01, 0x02, 0
51b90 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
51ba0 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 0x00, 0x00,\./*
51bb0 20 31 33 36 20 2a 2f 20 30 78 30 30 2c 20 30 78 136 */ 0x00, 0x
51bc0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
51bd0 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 34 0x00, 0x04, 0x04
51be0 2c 20 30 78 30 34 2c 5c 0a 2f 2a 20 31 34 34 20 , 0x04,\./* 144
51bf0 2a 2f 20 30 78 30 34 2c 20 30 78 30 34 2c 7d 0a */ 0x04, 0x04,}.
51c00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
51c10 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 73 2e End of opcodes.
51c20 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
51c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
51c50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
51c60 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
51c70 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
51c80 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a vdbe.h ********
51c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
51ca0 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74 79 70 ../*.** Prototyp
51cb0 65 73 20 66 6f 72 20 74 68 65 20 56 44 42 45 20 es for the VDBE
51cc0 69 6e 74 65 72 66 61 63 65 2e 20 20 53 65 65 20 interface. See
51cd0 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 comments on the
51ce0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a implementation.*
51cf0 2a 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 * for a descript
51d00 69 6f 6e 20 6f 66 20 77 68 61 74 20 65 61 63 68 ion of what each
51d10 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e of these routin
51d20 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 51 4c 49 es does..*/.SQLI
51d30 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 TE_PRIVATE Vdbe
51d40 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 *sqlite3VdbeCrea
51d50 74 65 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 te(sqlite3*);.SQ
51d60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
51d70 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
51d80 70 30 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 p0(Vdbe*,int);.S
51d90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
51da0 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 t sqlite3VdbeAdd
51db0 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e Op1(Vdbe*,int,in
51dc0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
51dd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
51de0 62 65 41 64 64 4f 70 32 28 56 64 62 65 2a 2c 69 beAddOp2(Vdbe*,i
51df0 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c nt,int,int);.SQL
51e00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
51e10 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
51e20 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 3(Vdbe*,int,int,
51e30 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 int,int);.SQLITE
51e40 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
51e50 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 56 ite3VdbeAddOp4(V
51e60 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 dbe*,int,int,int
51e70 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 20 ,int,const char
51e80 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 *zP4,int);.SQLIT
51e90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
51ea0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 lite3VdbeAddOpLi
51eb0 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 20 6e 4f st(Vdbe*, int nO
51ec0 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f p, VdbeOpList co
51ed0 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 4c 49 54 nst *aOp);.SQLIT
51ee0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
51ef0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
51f00 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 P1(Vdbe*, int ad
51f10 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a 53 51 4c dr, int P1);.SQL
51f20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
51f30 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
51f40 67 65 50 32 28 56 64 62 65 2a 2c 20 69 6e 74 20 geP2(Vdbe*, int
51f50 61 64 64 72 2c 20 69 6e 74 20 50 32 29 3b 0a 53 addr, int P2);.S
51f60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
51f70 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 id sqlite3VdbeCh
51f80 61 6e 67 65 50 33 28 56 64 62 65 2a 2c 20 69 6e angeP3(Vdbe*, in
51f90 74 20 61 64 64 72 2c 20 69 6e 74 20 50 33 29 3b t addr, int P3);
51fa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
51fb0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
51fc0 43 68 61 6e 67 65 50 35 28 56 64 62 65 2a 2c 20 ChangeP5(Vdbe*,
51fd0 75 38 20 50 35 29 3b 0a 53 51 4c 49 54 45 5f 50 u8 P5);.SQLITE_P
51fe0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
51ff0 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
52000 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 29 Vdbe*, int addr)
52010 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
52020 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
52030 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 eChangeToNoop(Vd
52040 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 be*, int addr, i
52050 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 nt N);.SQLITE_PR
52060 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
52070 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 56 e3VdbeChangeP4(V
52080 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 dbe*, int addr,
52090 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c const char *zP4,
520a0 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f int N);.SQLITE_
520b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
520c0 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 ite3VdbeUsesBtre
520d0 65 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 e(Vdbe*, int);.S
520e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 QLITE_PRIVATE Vd
520f0 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 64 62 beOp *sqlite3Vdb
52100 65 47 65 74 4f 70 28 56 64 62 65 2a 2c 20 69 6e eGetOp(Vdbe*, in
52110 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
52120 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
52130 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62 65 beMakeLabel(Vdbe
52140 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
52150 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
52160 64 62 65 44 65 6c 65 74 65 28 56 64 62 65 2a 29 dbeDelete(Vdbe*)
52170 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
52180 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
52190 65 4d 61 6b 65 52 65 61 64 79 28 56 64 62 65 2a eMakeReady(Vdbe*
521a0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 ,int,int,int,int
521b0 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ,int,int);.SQLIT
521c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
521d0 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a lite3VdbeFinaliz
521e0 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 e(Vdbe*);.SQLITE
521f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
52200 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 lite3VdbeResolve
52210 4c 61 62 65 6c 28 56 64 62 65 2a 2c 20 69 6e 74 Label(Vdbe*, int
52220 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
52230 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
52240 65 43 75 72 72 65 6e 74 41 64 64 72 28 56 64 62 eCurrentAddr(Vdb
52250 65 2a 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 e*);.#ifdef SQLI
52260 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f TE_DEBUG.SQLITE_
52270 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
52280 6c 69 74 65 33 56 64 62 65 4d 61 79 41 62 6f 72 lite3VdbeMayAbor
52290 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 t(Vdbe*);.SQLITE
522a0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
522b0 73 71 6c 69 74 65 33 56 64 62 65 54 72 61 63 65 sqlite3VdbeTrace
522c0 28 56 64 62 65 2a 2c 46 49 4c 45 2a 29 3b 0a 23 (Vdbe*,FILE*);.#
522d0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 endif.SQLITE_PRI
522e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
522f0 33 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65 3VdbeResetStepRe
52300 73 75 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c sult(Vdbe*);.SQL
52310 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
52320 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 sqlite3VdbeReset
52330 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f (Vdbe*);.SQLITE_
52340 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
52350 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f ite3VdbeSetNumCo
52360 6c 73 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 ls(Vdbe*,int);.S
52370 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
52380 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 t sqlite3VdbeSet
52390 43 6f 6c 4e 61 6d 65 28 56 64 62 65 2a 2c 20 69 ColName(Vdbe*, i
523a0 6e 74 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 nt, int, const c
523b0 68 61 72 20 2a 2c 20 76 6f 69 64 28 2a 29 28 76 har *, void(*)(v
523c0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 oid*));.SQLITE_P
523d0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
523e0 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e te3VdbeCountChan
523f0 67 65 73 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 ges(Vdbe*);.SQLI
52400 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 TE_PRIVATE sqlit
52410 65 33 20 2a 73 71 6c 69 74 65 33 56 64 62 65 44 e3 *sqlite3VdbeD
52420 62 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 b(Vdbe*);.SQLITE
52430 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
52440 6c 69 74 65 33 56 64 62 65 53 65 74 53 71 6c 28 lite3VdbeSetSql(
52450 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 Vdbe*, const cha
52460 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 r *z, int n, int
52470 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
52480 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
52490 62 65 53 77 61 70 28 56 64 62 65 2a 2c 56 64 62 beSwap(Vdbe*,Vdb
524a0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
524b0 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 ATE VdbeOp *sqli
524c0 74 65 33 56 64 62 65 54 61 6b 65 4f 70 41 72 72 te3VdbeTakeOpArr
524d0 61 79 28 56 64 62 65 2a 2c 20 69 6e 74 2a 2c 20 ay(Vdbe*, int*,
524e0 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int*);.SQLITE_PR
524f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
52500 65 33 56 64 62 65 50 72 6f 67 72 61 6d 44 65 6c e3VdbeProgramDel
52510 65 74 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 53 ete(sqlite3 *, S
52520 75 62 50 72 6f 67 72 61 6d 20 2a 2c 20 69 6e 74 ubProgram *, int
52530 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
52540 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f E_ENABLE_MEMORY_
52550 4d 41 4e 41 47 45 4d 45 4e 54 0a 53 51 4c 49 54 MANAGEMENT.SQLIT
52560 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
52570 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 lite3VdbeRelease
52580 4d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 23 65 6e Memory(int);.#en
52590 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
525a0 54 45 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 TE UnpackedRecor
525b0 64 20 2a 73 71 6c 69 74 65 33 56 64 62 65 52 65 d *sqlite3VdbeRe
525c0 63 6f 72 64 55 6e 70 61 63 6b 28 4b 65 79 49 6e cordUnpack(KeyIn
525d0 66 6f 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f fo*,int,const vo
525e0 69 64 2a 2c 63 68 61 72 2a 2c 69 6e 74 29 3b 0a id*,char*,int);.
525f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
52600 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 oid sqlite3VdbeD
52610 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 eleteUnpackedRec
52620 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f ord(UnpackedReco
52630 72 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 rd*);.SQLITE_PRI
52640 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
52650 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 VdbeRecordCompar
52660 65 28 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 e(int,const void
52670 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 *,UnpackedRecord
52680 2a 29 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 4e 44 *);...#ifndef ND
52690 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 EBUG.SQLITE_PRIV
526a0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
526b0 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 e3VdbeComment(Vd
526c0 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a be*, const char*
526d0 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 , ...);.# define
526e0 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 20 VdbeComment(X)
526f0 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d sqlite3VdbeComm
52700 65 6e 74 20 58 0a 53 51 4c 49 54 45 5f 50 52 49 ent X.SQLITE_PRI
52710 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
52720 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 te3VdbeNoopComme
52730 6e 74 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 nt(Vdbe*, const
52740 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 char*, ...);.# d
52750 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f efine VdbeNoopCo
52760 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65 mment(X) sqlite
52770 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 3VdbeNoopComment
52780 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e X.#else.# defin
52790 65 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 e VdbeComment(X)
527a0 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 4e 6f .# define VdbeNo
527b0 6f 70 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 65 6e opComment(X).#en
527c0 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a dif..#endif../**
527d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
527e0 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a of vdbe.h *****
527f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
52820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
52830 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
52840 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c left off in sql
52850 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
52860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
52870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
52880 6c 75 64 65 20 70 61 67 65 72 2e 68 20 69 6e 20 lude pager.h in
52890 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 the middle of sq
528a0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a liteInt.h ******
528b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
528c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
528d0 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 68 20 in file pager.h
528e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
528f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
52910 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 ** 2001 Septembe
52920 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 15.**.** The a
52930 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
52940 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
52950 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
52960 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
52970 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
52980 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
52990 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
529a0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
529b0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
529c0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
529d0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
529e0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
529f0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
52a00 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
52a10 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
52a20 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
52a30 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
52a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52a80 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 ***.** This head
52a90 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 er file defines
52aa0 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 the interface th
52ab0 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 70 61 at the sqlite pa
52ac0 67 65 20 63 61 63 68 65 0a 2a 2a 20 73 75 62 73 ge cache.** subs
52ad0 79 73 74 65 6d 2e 20 20 54 68 65 20 70 61 67 65 ystem. The page
52ae0 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d cache subsystem
52af0 20 72 65 61 64 73 20 61 6e 64 20 77 72 69 74 65 reads and write
52b00 73 20 61 20 66 69 6c 65 20 61 20 70 61 67 65 0a s a file a page.
52b10 2a 2a 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64 ** at a time and
52b20 20 70 72 6f 76 69 64 65 73 20 61 20 6a 6f 75 72 provides a jour
52b30 6e 61 6c 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b nal for rollback
52b40 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 ..**.** @(#) $Id
52b50 3a 20 70 61 67 65 72 2e 68 2c 76 20 31 2e 31 30 : pager.h,v 1.10
52b60 34 20 32 30 30 39 2f 30 37 2f 32 34 20 31 39 3a 4 2009/07/24 19:
52b70 30 31 3a 31 39 20 64 72 68 20 45 78 70 20 24 0a 01:19 drh Exp $.
52b80 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 47 */..#ifndef _PAG
52b90 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50 ER_H_.#define _P
52ba0 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44 AGER_H_../*.** D
52bb0 65 66 61 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73 efault maximum s
52bc0 69 7a 65 20 66 6f 72 20 70 65 72 73 69 73 74 65 ize for persiste
52bd0 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 nt journal files
52be0 2e 20 41 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a . A negative .**
52bf0 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20 value means no
52c00 6c 69 6d 69 74 2e 20 54 68 69 73 20 76 61 6c 75 limit. This valu
52c10 65 20 6d 61 79 20 62 65 20 6f 76 65 72 72 69 64 e may be overrid
52c20 64 65 6e 20 75 73 69 6e 67 20 74 68 65 20 0a 2a den using the .*
52c30 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f * sqlite3PagerJo
52c40 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29 urnalSizeLimit()
52c50 20 41 50 49 2e 20 53 65 65 20 61 6c 73 6f 20 22 API. See also "
52c60 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 PRAGMA journal_s
52c70 69 7a 65 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23 ize_limit"..*/.#
52c80 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 ifndef SQLITE_DE
52c90 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 FAULT_JOURNAL_SI
52ca0 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64 65 66 69 ZE_LIMIT. #defi
52cb0 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ne SQLITE_DEFAUL
52cc0 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c T_JOURNAL_SIZE_L
52cd0 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a IMIT -1.#endif..
52ce0 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 75 /*.** The type u
52cf0 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74 sed to represent
52d00 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 a page number.
52d10 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 The first page
52d20 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20 in a file.** is
52d30 63 61 6c 6c 65 64 20 70 61 67 65 20 31 2e 20 20 called page 1.
52d40 30 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 70 0 is used to rep
52d50 72 65 73 65 6e 74 20 22 6e 6f 74 20 61 20 70 61 resent "not a pa
52d60 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ge"..*/.typedef
52d70 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a u32 Pgno;../*.**
52d80 20 45 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 20 Each open file
52d90 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 61 20 is managed by a
52da0 73 65 70 61 72 61 74 65 20 69 6e 73 74 61 6e 63 separate instanc
52db0 65 20 6f 66 20 74 68 65 20 22 50 61 67 65 72 22 e of the "Pager"
52dc0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 structure..*/.t
52dd0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61 ypedef struct Pa
52de0 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a ger Pager;../*.*
52df0 2a 20 48 61 6e 64 6c 65 20 74 79 70 65 20 66 6f * Handle type fo
52e00 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 r pages..*/.type
52e10 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72 def struct PgHdr
52e20 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 DbPage;../*.**
52e30 50 61 67 65 20 6e 75 6d 62 65 72 20 50 41 47 45 Page number PAGE
52e40 52 5f 4d 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76 R_MJ_PGNO is nev
52e50 65 72 20 75 73 65 64 20 69 6e 20 61 6e 20 53 51 er used in an SQ
52e60 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 28 69 Lite database (i
52e70 74 20 69 73 0a 2a 2a 20 72 65 73 65 72 76 65 64 t is.** reserved
52e80 20 66 6f 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f for working aro
52e90 75 6e 64 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f und a windows/po
52ea0 73 69 78 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c six incompatibil
52eb0 69 74 79 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75 ity). It is.** u
52ec0 73 65 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e sed in the journ
52ed0 61 6c 20 74 6f 20 73 69 67 6e 69 66 79 20 74 68 al to signify th
52ee0 61 74 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 at the remainder
52ef0 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
52f00 66 69 6c 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f file .** is devo
52f10 74 65 64 20 74 6f 20 73 74 6f 72 69 6e 67 20 61 ted to storing a
52f20 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
52f30 6e 61 6d 65 20 2d 20 74 68 65 72 65 20 61 72 65 name - there are
52f40 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 74 no more pages t
52f50 6f 0a 2a 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 o.** roll back.
52f60 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 See comments for
52f70 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d function writeM
52f80 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69 asterJournal() i
52f90 6e 20 70 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f n pager.c .** fo
52fa0 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 r details..*/.#d
52fb0 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 efine PAGER_MJ_P
52fc0 47 4e 4f 28 78 29 20 28 28 50 67 6e 6f 29 28 28 GNO(x) ((Pgno)((
52fd0 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 PENDING_BYTE/((x
52fe0 29 2d 3e 70 61 67 65 53 69 7a 65 29 29 2b 31 29 )->pageSize))+1)
52ff0 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 )../*.** Allowed
53000 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 values for the
53010 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 flags parameter
53020 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f to sqlite3PagerO
53030 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 pen()..**.** NOT
53040 45 3a 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 E: These values
53050 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 63 must match the c
53060 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 42 54 52 orresponding BTR
53070 45 45 5f 20 76 61 6c 75 65 73 20 69 6e 20 62 74 EE_ values in bt
53080 72 65 65 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e ree.h..*/.#defin
53090 65 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 e PAGER_OMIT_JOU
530a0 52 4e 41 4c 20 20 30 78 30 30 30 31 20 20 20 20 RNAL 0x0001
530b0 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 61 20 /* Do not use a
530c0 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
530d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 */.#define PAGE
530e0 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 R_NO_READLOCK
530f0 30 78 30 30 30 32 20 20 20 20 2f 2a 20 4f 6d 69 0x0002 /* Omi
53100 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 t readlocks on r
53110 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f eadonly files */
53120 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 ../*.** Valid va
53130 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 65 63 lues for the sec
53140 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ond argument to
53150 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b sqlite3PagerLock
53160 69 6e 67 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 ingMode()..*/.#d
53170 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b efine PAGER_LOCK
53180 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 INGMODE_QUERY
53190 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 -1.#define PA
531a0 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
531b0 4e 4f 52 4d 41 4c 20 20 20 20 20 20 30 0a 23 64 NORMAL 0.#d
531c0 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b efine PAGER_LOCK
531d0 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 INGMODE_EXCLUSIV
531e0 45 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c E 1../*.** Val
531f0 69 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 id values for th
53200 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
53210 74 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 t to sqlite3Page
53220 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a rJournalMode()..
53230 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 */.#define PAGER
53240 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 _JOURNALMODE_QUE
53250 52 59 20 20 20 20 20 20 2d 31 0a 23 64 65 66 69 RY -1.#defi
53260 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c ne PAGER_JOURNAL
53270 4d 4f 44 45 5f 44 45 4c 45 54 45 20 20 20 20 20 MODE_DELETE
53280 20 30 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 0 /* Commit b
53290 79 20 64 65 6c 65 74 69 6e 67 20 6a 6f 75 72 6e y deleting journ
532a0 61 6c 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 al file */.#defi
532b0 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c ne PAGER_JOURNAL
532c0 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 20 20 20 MODE_PERSIST
532d0 20 31 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 1 /* Commit b
532e0 79 20 7a 65 72 6f 69 6e 67 20 6a 6f 75 72 6e 61 y zeroing journa
532f0 6c 20 68 65 61 64 65 72 20 2a 2f 0a 23 64 65 66 l header */.#def
53300 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 ine PAGER_JOURNA
53310 4c 4d 4f 44 45 5f 4f 46 46 20 20 20 20 20 20 20 LMODE_OFF
53320 20 20 32 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 2 /* Journal
53330 20 6f 6d 69 74 74 65 64 2e 20 20 2a 2f 0a 23 64 omitted. */.#d
53340 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 efine PAGER_JOUR
53350 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 NALMODE_TRUNCATE
53360 20 20 20 20 33 20 20 20 2f 2a 20 43 6f 6d 6d 69 3 /* Commi
53370 74 20 62 79 20 74 72 75 6e 63 61 74 69 6e 67 20 t by truncating
53380 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 journal */.#defi
53390 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c ne PAGER_JOURNAL
533a0 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 MODE_MEMORY
533b0 20 34 20 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 4 /* In-memor
533c0 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a y journal file *
533d0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 6d /../*.** The rem
533e0 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 66 ainder of this f
533f0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ile contains the
53400 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 declarations of
53410 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a the functions.*
53420 2a 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74 * that make up t
53430 68 65 20 50 61 67 65 72 20 73 75 62 2d 73 79 73 he Pager sub-sys
53440 74 65 6d 20 41 50 49 2e 20 53 65 65 20 73 6f 75 tem API. See sou
53450 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 rce code comment
53460 73 20 66 6f 72 20 0a 2a 2a 20 61 20 64 65 74 61 s for .** a deta
53470 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e iled description
53480 20 6f 66 20 65 61 63 68 20 72 6f 75 74 69 6e 65 of each routine
53490 2e 0a 2a 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e ..*/../* Open an
534a0 64 20 63 6c 6f 73 65 20 61 20 50 61 67 65 72 20 d close a Pager
534b0 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a connection. */ .
534c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
534d0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f nt sqlite3PagerO
534e0 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 pen(. sqlite3_v
534f0 66 73 2a 2c 0a 20 20 50 61 67 65 72 20 2a 2a 70 fs*,. Pager **p
53500 70 50 61 67 65 72 2c 0a 20 20 63 6f 6e 73 74 20 pPager,. const
53510 63 68 61 72 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20 char*,. int,.
53520 69 6e 74 2c 0a 20 20 69 6e 74 2c 0a 20 20 76 6f int,. int,. vo
53530 69 64 28 2a 29 28 44 62 50 61 67 65 2a 29 0a 29 id(*)(DbPage*).)
53540 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
53550 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
53560 72 43 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50 rClose(Pager *pP
53570 61 67 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ager);.SQLITE_PR
53580 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
53590 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 3PagerReadFilehe
535a0 61 64 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 ader(Pager*, int
535b0 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a , unsigned char*
535c0 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 );../* Functions
535d0 20 75 73 65 64 20 74 6f 20 63 6f 6e 66 69 67 75 used to configu
535e0 72 65 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 re a Pager objec
535f0 74 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t. */.SQLITE_PRI
53600 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
53610 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 3PagerSetBusyhan
53620 64 6c 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 dler(Pager*, int
53630 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 (*)(void *), voi
53640 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 d *);.SQLITE_PRI
53650 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
53660 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
53670 28 50 61 67 65 72 2a 2c 20 75 31 36 2a 2c 20 69 (Pager*, u16*, i
53680 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
53690 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
536a0 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 agerMaxPageCount
536b0 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 (Pager*, int);.S
536c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
536d0 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 id sqlite3PagerS
536e0 65 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 etCachesize(Page
536f0 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 r*, int);.SQLITE
53700 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
53710 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 lite3PagerSetSaf
53720 65 74 79 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c etyLevel(Pager*,
53730 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 int,int);.SQLITE
53740 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
53750 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 ite3PagerLocking
53760 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e Mode(Pager *, in
53770 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
53780 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
53790 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 gerJournalMode(P
537a0 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 ager *, int);.SQ
537b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 LITE_PRIVATE i64
537c0 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 sqlite3PagerJou
537d0 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61 rnalSizeLimit(Pa
537e0 67 65 72 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c ger *, i64);.SQL
537f0 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 ITE_PRIVATE sqli
53800 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73 71 6c te3_backup **sql
53810 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 ite3PagerBackupP
53820 74 72 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 tr(Pager*);../*
53830 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 Functions used t
53840 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c o obtain and rel
53850 65 61 73 65 20 70 61 67 65 20 72 65 66 65 72 65 ease page refere
53860 6e 63 65 73 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 nces. */ .SQLITE
53870 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
53880 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65 ite3PagerAcquire
53890 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
538a0 50 67 6e 6f 20 70 67 6e 6f 2c 20 44 62 50 61 67 Pgno pgno, DbPag
538b0 65 20 2a 2a 70 70 50 61 67 65 2c 20 69 6e 74 20 e **ppPage, int
538c0 63 6c 72 46 6c 61 67 29 3b 0a 23 64 65 66 69 6e clrFlag);.#defin
538d0 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 e sqlite3PagerGe
538e0 74 28 41 2c 42 2c 43 29 20 73 71 6c 69 74 65 33 t(A,B,C) sqlite3
538f0 50 61 67 65 72 41 63 71 75 69 72 65 28 41 2c 42 PagerAcquire(A,B
53900 2c 43 2c 30 29 0a 53 51 4c 49 54 45 5f 50 52 49 ,C,0).SQLITE_PRI
53910 56 41 54 45 20 44 62 50 61 67 65 20 2a 73 71 6c VATE DbPage *sql
53920 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 ite3PagerLookup(
53930 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 Pager *pPager, P
53940 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51 4c 49 54 gno pgno);.SQLIT
53950 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
53960 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 44 qlite3PagerRef(D
53970 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f bPage*);.SQLITE_
53980 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
53990 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44 ite3PagerUnref(D
539a0 62 50 61 67 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65 bPage*);../* Ope
539b0 72 61 74 69 6f 6e 73 20 6f 6e 20 70 61 67 65 20 rations on page
539c0 72 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a 53 references. */.S
539d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
539e0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 t sqlite3PagerWr
539f0 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 ite(DbPage*);.SQ
53a00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
53a10 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f d sqlite3PagerDo
53a20 6e 74 57 72 69 74 65 28 44 62 50 61 67 65 2a 29 ntWrite(DbPage*)
53a30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
53a40 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
53a50 72 4d 6f 76 65 70 61 67 65 28 50 61 67 65 72 2a rMovepage(Pager*
53a60 2c 44 62 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e ,DbPage*,Pgno,in
53a70 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
53a80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
53a90 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 gerPageRefcount(
53aa0 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 DbPage*);.SQLITE
53ab0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
53ac0 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
53ad0 74 61 28 44 62 50 61 67 65 20 2a 29 3b 20 0a 53 ta(DbPage *); .S
53ae0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
53af0 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 id *sqlite3Pager
53b00 47 65 74 45 78 74 72 61 28 44 62 50 61 67 65 20 GetExtra(DbPage
53b10 2a 29 3b 20 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f *); ../* Functio
53b20 6e 73 20 75 73 65 64 20 74 6f 20 6d 61 6e 61 67 ns used to manag
53b30 65 20 70 61 67 65 72 20 74 72 61 6e 73 61 63 74 e pager transact
53b40 69 6f 6e 73 20 61 6e 64 20 73 61 76 65 70 6f 69 ions and savepoi
53b50 6e 74 73 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 nts. */.SQLITE_P
53b60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
53b70 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
53b80 28 50 61 67 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a (Pager*, int*);.
53b90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
53ba0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 nt sqlite3PagerB
53bb0 65 67 69 6e 28 50 61 67 65 72 2a 2c 20 69 6e 74 egin(Pager*, int
53bc0 20 65 78 46 6c 61 67 2c 20 69 6e 74 29 3b 0a 53 exFlag, int);.S
53bd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
53be0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f t sqlite3PagerCo
53bf0 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 50 61 67 mmitPhaseOne(Pag
53c00 65 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a er*,const char *
53c10 7a 4d 61 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53 zMaster, int);.S
53c20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
53c30 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 t sqlite3PagerSy
53c40 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nc(Pager *pPager
53c50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
53c60 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
53c70 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f erCommitPhaseTwo
53c80 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.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 52 6f 6c 6c 62 61 63 ite3PagerRollbac
53cb0 6b 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 k(Pager*);.SQLIT
53cc0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
53cd0 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 lite3PagerOpenSa
53ce0 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 vepoint(Pager *p
53cf0 50 61 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 Pager, int n);.S
53d00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
53d10 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 t sqlite3PagerSa
53d20 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 vepoint(Pager *p
53d30 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 Pager, int op, i
53d40 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a nt iSavepoint);.
53d50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
53d60 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 nt sqlite3PagerS
53d70 68 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 haredLock(Pager
53d80 2a 70 50 61 67 65 72 29 3b 0a 0a 2f 2a 20 46 75 *pPager);../* Fu
53d90 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 nctions used to
53da0 71 75 65 72 79 20 70 61 67 65 72 20 73 74 61 74 query pager stat
53db0 65 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 61 74 e and configurat
53dc0 69 6f 6e 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion. */.SQLITE_P
53dd0 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 RIVATE u8 sqlite
53de0 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 3PagerIsreadonly
53df0 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
53e00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
53e10 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e ite3PagerRefcoun
53e20 74 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 t(Pager*);.SQLIT
53e30 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
53e40 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 char *sqlite3Pag
53e50 65 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 erFilename(Pager
53e60 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
53e70 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 TE const sqlite3
53e80 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67 _vfs *sqlite3Pag
53e90 65 72 56 66 73 28 50 61 67 65 72 2a 29 3b 0a 53 erVfs(Pager*);.S
53ea0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
53eb0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69 lite3_file *sqli
53ec0 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 67 te3PagerFile(Pag
53ed0 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 er*);.SQLITE_PRI
53ee0 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 VATE const char
53ef0 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 *sqlite3PagerJou
53f00 72 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29 rnalname(Pager*)
53f10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
53f20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
53f30 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 2a 29 3b rNosync(Pager*);
53f40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
53f50 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 void *sqlite3Pag
53f60 65 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 erTempSpace(Page
53f70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
53f80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
53f90 61 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65 agerIsMemdb(Page
53fa0 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f r*);../* Functio
53fb0 6e 73 20 75 73 65 64 20 74 6f 20 74 72 75 6e 63 ns used to trunc
53fc0 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ate the database
53fd0 20 66 69 6c 65 2e 20 2a 2f 0a 53 51 4c 49 54 45 file. */.SQLITE
53fe0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
53ff0 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 lite3PagerTrunca
54000 74 65 49 6d 61 67 65 28 50 61 67 65 72 2a 2c 50 teImage(Pager*,P
54010 67 6e 6f 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 gno);../* Functi
54020 6f 6e 73 20 74 6f 20 73 75 70 70 6f 72 74 20 74 ons to support t
54030 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 esting and debug
54040 67 69 6e 67 2e 20 2a 2f 0a 23 69 66 20 21 64 65 ging. */.#if !de
54050 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c fined(NDEBUG) ||
54060 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
54070 54 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 TEST).SQLITE_PRI
54080 56 41 54 45 20 20 20 50 67 6e 6f 20 73 71 6c 69 VATE Pgno sqli
54090 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 te3PagerPagenumb
540a0 65 72 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c er(DbPage*);.SQL
540b0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
540c0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 t sqlite3PagerIs
540d0 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 writeable(DbPage
540e0 2a 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 *);.#endif.#ifde
540f0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
54100 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
54110 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 nt *sqlite3Pager
54120 53 74 61 74 73 28 50 61 67 65 72 2a 29 3b 0a 53 Stats(Pager*);.S
54130 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
54140 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
54150 72 52 65 66 64 75 6d 70 28 50 61 67 65 72 2a 29 rRefdump(Pager*)
54160 3b 0a 20 20 76 6f 69 64 20 64 69 73 61 62 6c 65 ;. void disable
54170 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 _simulated_io_er
54180 72 6f 72 73 28 76 6f 69 64 29 3b 0a 20 20 76 6f rors(void);. vo
54190 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 id enable_simula
541a0 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f ted_io_errors(vo
541b0 69 64 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 id);.#else.# def
541c0 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 ine disable_simu
541d0 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 lated_io_errors(
541e0 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c ).# define enabl
541f0 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 e_simulated_io_e
54200 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a rrors().#endif..
54210 23 65 6e 64 69 66 20 2f 2a 20 5f 50 41 47 45 52 #endif /* _PAGER
54220 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a _H_ */../*******
54230 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 ******* End of p
54240 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ager.h *********
54250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54270 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
54280 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
54290 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
542a0 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e off in sqliteIn
542b0 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
542c0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
542d0 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
542e0 70 63 61 63 68 65 2e 68 20 69 6e 20 74 68 65 20 pcache.h in the
542f0 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 middle of sqlite
54300 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
54310 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
54320 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
54330 6c 65 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a le pcache.h ****
54340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54360 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
54370 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 08 August 05.**.
54380 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
54390 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
543a0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
543b0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
543c0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
543d0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
543e0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
543f0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
54400 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
54410 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
54420 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
54430 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
54440 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
54450 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
54460 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
54470 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
54480 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
54490 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
544a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
544b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
544c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
544d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
544e0 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 his header file
544f0 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 defines the inte
54500 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20 73 rface that the s
54510 71 6c 69 74 65 20 70 61 67 65 20 63 61 63 68 65 qlite page cache
54520 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 0a .** subsystem. .
54530 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 **.** @(#) $Id:
54540 70 63 61 63 68 65 2e 68 2c 76 20 31 2e 32 30 20 pcache.h,v 1.20
54550 32 30 30 39 2f 30 37 2f 32 35 20 31 31 3a 34 36 2009/07/25 11:46
54560 3a 34 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :49 danielk1977
54570 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 Exp $.*/..#ifnde
54580 66 20 5f 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79 f _PCACHE_H_..ty
54590 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48 pedef struct PgH
545a0 64 72 20 50 67 48 64 72 3b 0a 74 79 70 65 64 65 dr PgHdr;.typede
545b0 66 20 73 74 72 75 63 74 20 50 43 61 63 68 65 20 f struct PCache
545c0 50 43 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 PCache;../*.** E
545d0 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 very page in the
545e0 20 63 61 63 68 65 20 69 73 20 63 6f 6e 74 72 6f cache is contro
545f0 6c 6c 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 lled by an insta
54600 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
54610 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 wing.** structur
54620 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48 e..*/.struct PgH
54630 64 72 20 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61 dr {. void *pDa
54640 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ta;
54650 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 /* Content
54660 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f of this page */
54670 0a 20 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b . void *pExtra;
54680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54690 20 20 2f 2a 20 45 78 74 72 61 20 63 6f 6e 74 65 /* Extra conte
546a0 6e 74 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 nt */. PgHdr *p
546b0 44 69 72 74 79 3b 20 20 20 20 20 20 20 20 20 20 Dirty;
546c0 20 20 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 69 /* Transi
546d0 65 6e 74 20 6c 69 73 74 20 6f 66 20 64 69 72 74 ent list of dirt
546e0 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 6e y pages */. Pgn
546f0 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 o pgno;
54700 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
54710 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 age number for t
54720 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 61 his page */. Pa
54730 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 ger *pPager;
54740 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
54750 54 68 65 20 70 61 67 65 72 20 74 68 69 73 20 70 The pager this p
54760 61 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a age is part of *
54770 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
54780 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 75 33 CHECK_PAGES. u3
54790 32 20 70 61 67 65 48 61 73 68 3b 20 20 20 20 20 2 pageHash;
547a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
547b0 48 61 73 68 20 6f 66 20 70 61 67 65 20 63 6f 6e Hash of page con
547c0 74 65 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 tent */.#endif.
547d0 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 u16 flags;
547e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
547f0 2f 2a 20 50 47 48 44 52 20 66 6c 61 67 73 20 64 /* PGHDR flags d
54800 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a efined below */.
54810 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a . /************
54820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 **********. **
54860 45 6c 65 6d 65 6e 74 73 20 61 62 6f 76 65 20 61 Elements above a
54870 72 65 20 70 75 62 6c 69 63 2e 20 20 41 6c 6c 20 re public. All
54880 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 that follows is
54890 70 72 69 76 61 74 65 20 74 6f 20 70 63 61 63 68 private to pcach
548a0 65 2e 63 0a 20 20 2a 2a 20 61 6e 64 20 73 68 6f e.c. ** and sho
548b0 75 6c 64 20 6e 6f 74 20 62 65 20 61 63 63 65 73 uld not be acces
548c0 73 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 sed by other mod
548d0 75 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 31 36 ules.. */. i16
548e0 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 nRef;
548f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
54900 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20 6f umber of users o
54910 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 f this page */.
54920 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b PCache *pCache;
54930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54940 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 6f 77 /* Cache that ow
54950 6e 73 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a ns this page */.
54960 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 . PgHdr *pDirty
54970 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 Next;
54980 20 20 2f 2a 20 4e 65 78 74 20 65 6c 65 6d 65 6e /* Next elemen
54990 74 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 t in list of dir
549a0 74 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 ty pages */. Pg
549b0 48 64 72 20 2a 70 44 69 72 74 79 50 72 65 76 3b Hdr *pDirtyPrev;
549c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
549d0 50 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 Previous element
549e0 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 in list of dirt
549f0 79 20 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f y pages */.};../
54a00 2a 20 42 69 74 20 76 61 6c 75 65 73 20 66 6f 72 * Bit values for
54a10 20 50 67 48 64 72 2e 66 6c 61 67 73 20 2a 2f 0a PgHdr.flags */.
54a20 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44 49 #define PGHDR_DI
54a30 52 54 59 20 20 20 20 20 20 20 20 20 20 20 20 20 RTY
54a40 30 78 30 30 32 20 20 2f 2a 20 50 61 67 65 20 68 0x002 /* Page h
54a50 61 73 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 as changed */.#d
54a60 65 66 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 efine PGHDR_NEED
54a70 5f 53 59 4e 43 20 20 20 20 20 20 20 20 20 30 78 _SYNC 0x
54a80 30 30 34 20 20 2f 2a 20 46 73 79 6e 63 20 74 68 004 /* Fsync th
54a90 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
54aa0 61 6c 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 al before.
54ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54ad0 20 2a 2a 20 77 72 69 74 69 6e 67 20 74 68 69 73 ** writing this
54ae0 20 70 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 page to the dat
54af0 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 abase */.#define
54b00 20 50 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 PGHDR_NEED_READ
54b10 20 20 20 20 20 20 20 20 20 30 78 30 30 38 20 20 0x008
54b20 2f 2a 20 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e /* Content is un
54b30 72 65 61 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 read */.#define
54b40 50 47 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49 PGHDR_REUSE_UNLI
54b50 4b 45 4c 59 20 20 20 20 30 78 30 31 30 20 20 2f KELY 0x010 /
54b60 2a 20 41 20 68 69 6e 74 20 74 68 61 74 20 72 65 * A hint that re
54b70 75 73 65 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 use is unlikely
54b80 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 */.#define PGHDR
54b90 5f 44 4f 4e 54 5f 57 52 49 54 45 20 20 20 20 20 _DONT_WRITE
54ba0 20 20 20 30 78 30 32 30 20 20 2f 2a 20 44 6f 20 0x020 /* Do
54bb0 6e 6f 74 20 77 72 69 74 65 20 63 6f 6e 74 65 6e not write conten
54bc0 74 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a t to disk */../*
54bd0 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 Initialize and
54be0 73 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 shutdown the pag
54bf0 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 e cache subsyste
54c00 6d 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 m */.SQLITE_PRIV
54c10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
54c20 63 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 cacheInitialize(
54c30 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 void);.SQLITE_PR
54c40 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
54c50 65 33 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e e3PcacheShutdown
54c60 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65 (void);../* Page
54c70 20 63 61 63 68 65 20 62 75 66 66 65 72 20 6d 61 cache buffer ma
54c80 6e 61 67 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65 nagement:.** The
54c90 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c se routines impl
54ca0 65 6d 65 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e ement SQLITE_CON
54cb0 46 49 47 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a FIG_PAGECACHE..*
54cc0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
54cd0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 void sqlite3PCa
54ce0 63 68 65 42 75 66 66 65 72 53 65 74 75 70 28 76 cheBufferSetup(v
54cf0 6f 69 64 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 oid *, int sz, i
54d00 6e 74 20 6e 29 3b 0a 0a 2f 2a 20 43 72 65 61 74 nt n);../* Creat
54d10 65 20 61 20 6e 65 77 20 70 61 67 65 72 20 63 61 e a new pager ca
54d20 63 68 65 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65 che..** Under me
54d30 6d 6f 72 79 20 73 74 72 65 73 73 2c 20 69 6e 76 mory stress, inv
54d40 6f 6b 65 20 78 53 74 72 65 73 73 20 74 6f 20 74 oke xStress to t
54d50 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 ry to make pages
54d60 20 63 6c 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20 clean..** Only
54d70 63 6c 65 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e clean and unpinn
54d80 65 64 20 70 61 67 65 73 20 63 61 6e 20 62 65 20 ed pages can be
54d90 72 65 63 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51 reclaimed..*/.SQ
54da0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
54db0 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f d sqlite3PcacheO
54dc0 70 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 pen(. int szPag
54dd0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
54de0 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
54df0 20 65 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 every page */.
54e00 20 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 int szExtra,
54e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54e20 2f 2a 20 45 78 74 72 61 20 73 70 61 63 65 20 61 /* Extra space a
54e30 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 ssociated with e
54e40 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e ach page */. in
54e50 74 20 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20 t bPurgeable,
54e60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
54e70 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72 True if pages ar
54e80 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f e on backing sto
54e90 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 re */. int (*xS
54ea0 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 20 50 67 tress)(void*, Pg
54eb0 48 64 72 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 Hdr*), /* Call t
54ec0 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 o try to make pa
54ed0 67 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 ges clean */. v
54ee0 6f 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 oid *pStress,
54ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
54f00 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 Argument to xSt
54f10 72 65 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 ress */. PCache
54f20 20 2a 70 54 6f 49 6e 69 74 20 20 20 20 20 20 20 *pToInit
54f30 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 /* Prea
54f40 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 llocated space f
54f50 6f 72 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f or the PCache */
54f60 0a 29 3b 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74 .);../* Modify t
54f70 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 66 74 he page-size aft
54f80 65 72 20 74 68 65 20 63 61 63 68 65 20 68 61 73 er the cache has
54f90 20 62 65 65 6e 20 63 72 65 61 74 65 64 2e 20 2a been created. *
54fa0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
54fb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
54fc0 63 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 cheSetPageSize(P
54fd0 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a Cache *, int);..
54fe0 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 /* Return the si
54ff0 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61 ze in bytes of a
55000 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 PCache object.
55010 20 55 73 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f Used to preallo
55020 63 61 74 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20 cate.** storage
55030 73 70 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 space..*/.SQLITE
55040 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
55050 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 76 ite3PcacheSize(v
55060 6f 69 64 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65 oid);../* One re
55070 6c 65 61 73 65 20 70 65 72 20 73 75 63 63 65 73 lease per succes
55080 73 66 75 6c 20 66 65 74 63 68 2e 20 20 50 61 67 sful fetch. Pag
55090 65 20 69 73 20 70 69 6e 6e 65 64 20 75 6e 74 69 e is pinned unti
550a0 6c 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52 l released..** R
550b0 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64 eference counted
550c0 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 . .*/.SQLITE_PRI
550d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
550e0 50 63 61 63 68 65 46 65 74 63 68 28 50 43 61 63 PcacheFetch(PCac
550f0 68 65 2a 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63 he*, Pgno, int c
55100 72 65 61 74 65 46 6c 61 67 2c 20 50 67 48 64 72 reateFlag, PgHdr
55110 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
55120 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
55130 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 PcacheRelease(Pg
55140 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 Hdr*);..SQLITE_P
55150 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
55160 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 50 67 te3PcacheDrop(Pg
55170 48 64 72 2a 29 3b 20 20 20 20 20 20 20 20 20 2f Hdr*); /
55180 2a 20 52 65 6d 6f 76 65 20 70 61 67 65 20 66 72 * Remove page fr
55190 6f 6d 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 om cache */.SQLI
551a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
551b0 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b sqlite3PcacheMak
551c0 65 44 69 72 74 79 28 50 67 48 64 72 2a 29 3b 20 eDirty(PgHdr*);
551d0 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 /* Make sure
551e0 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 64 page is marked d
551f0 69 72 74 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 irty */.SQLITE_P
55200 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
55210 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 te3PcacheMakeCle
55220 61 6e 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f an(PgHdr*); /
55230 2a 20 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 * Mark a single
55240 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f page as clean */
55250 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
55260 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
55270 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 heCleanAll(PCach
55280 65 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 e*); /* Mark
55290 61 6c 6c 20 64 69 72 74 79 20 6c 69 73 74 20 70 all dirty list p
552a0 61 67 65 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f ages as clean */
552b0 0a 0a 2f 2a 20 43 68 61 6e 67 65 20 61 20 70 61 ../* Change a pa
552c0 67 65 20 6e 75 6d 62 65 72 2e 20 20 55 73 65 64 ge number. Used
552d0 20 62 79 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e by incr-vacuum.
552e0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
552f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
55300 63 61 63 68 65 4d 6f 76 65 28 50 67 48 64 72 2a cacheMove(PgHdr*
55310 2c 20 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d , Pgno);../* Rem
55320 6f 76 65 20 61 6c 6c 20 70 61 67 65 73 20 77 69 ove all pages wi
55330 74 68 20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65 th pgno>x. Rese
55340 74 20 74 68 65 20 63 61 63 68 65 20 69 66 20 78 t the cache if x
55350 3d 3d 30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ==0 */.SQLITE_PR
55360 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
55370 65 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 e3PcacheTruncate
55380 28 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78 (PCache*, Pgno x
55390 29 3b 0a 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73 );../* Get a lis
553a0 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70 t of all dirty p
553b0 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 ages in the cach
553c0 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 e, sorted by pag
553d0 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49 e number */.SQLI
553e0 54 45 5f 50 52 49 56 41 54 45 20 50 67 48 64 72 TE_PRIVATE PgHdr
553f0 20 2a 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 *sqlite3PcacheD
55400 69 72 74 79 4c 69 73 74 28 50 43 61 63 68 65 2a irtyList(PCache*
55410 29 3b 0a 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64 );../* Reset and
55420 20 63 6c 6f 73 65 20 74 68 65 20 63 61 63 68 65 close the cache
55430 20 6f 62 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54 object */.SQLIT
55440 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
55450 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 qlite3PcacheClos
55460 65 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 e(PCache*);../*
55470 43 6c 65 61 72 20 66 6c 61 67 73 20 66 72 6f 6d Clear flags from
55480 20 70 61 67 65 73 20 6f 66 20 74 68 65 20 70 61 pages of the pa
55490 67 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 ge cache */.SQLI
554a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
554b0 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 sqlite3PcacheCle
554c0 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63 arSyncFlags(PCac
554d0 68 65 20 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63 61 he *);../* Disca
554e0 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 rd the contents
554f0 6f 66 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a of the cache */.
55500 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
55510 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
55520 65 43 6c 65 61 72 28 50 43 61 63 68 65 2a 29 3b eClear(PCache*);
55530 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 ../* Return the
55540 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
55550 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 outstanding page
55560 20 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53 references */.S
55570 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
55580 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 t sqlite3PcacheR
55590 65 66 43 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 efCount(PCache*)
555a0 3b 0a 0a 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 ;../* Increment
555b0 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f the reference co
555c0 75 6e 74 20 6f 66 20 61 6e 20 65 78 69 73 74 69 unt of an existi
555d0 6e 67 20 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54 ng page */.SQLIT
555e0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
555f0 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 28 qlite3PcacheRef(
55600 50 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 PgHdr*);..SQLITE
55610 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
55620 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 ite3PcachePageRe
55630 66 63 6f 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a fcount(PgHdr*);.
55640 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 ./* Return the t
55650 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 otal number of p
55660 61 67 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 ages stored in t
55670 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 he cache */.SQLI
55680 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
55690 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 qlite3PcachePage
556a0 63 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a count(PCache*);.
556b0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
556c0 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 ITE_CHECK_PAGES)
556d0 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
556e0 54 45 5f 44 45 42 55 47 29 0a 2f 2a 20 49 74 65 TE_DEBUG)./* Ite
556f0 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 6c 6c rate through all
55700 20 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72 dirty pages cur
55710 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e rently stored in
55720 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 69 73 the cache. This
55730 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 .** interface is
55740 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
55750 69 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f if SQLITE_CHECK_
55760 50 41 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 PAGES is defined
55770 20 77 68 65 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 when the .** li
55780 62 72 61 72 79 20 69 73 20 62 75 69 6c 74 2e 0a brary is built..
55790 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
557a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
557b0 61 63 68 65 49 74 65 72 61 74 65 44 69 72 74 79 acheIterateDirty
557c0 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c (PCache *pCache,
557d0 20 76 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 void (*xIter)(P
557e0 67 48 64 72 20 2a 29 29 3b 0a 23 65 6e 64 69 66 gHdr *));.#endif
557f0 0a 0a 2f 2a 20 53 65 74 20 61 6e 64 20 67 65 74 ../* Set and get
55800 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63 the suggested c
55810 61 63 68 65 2d 73 69 7a 65 20 66 6f 72 20 74 68 ache-size for th
55820 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 e specified page
55830 72 2d 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 49 r-cache..**.** I
55840 66 20 6e 6f 20 67 6c 6f 62 61 6c 20 6d 61 78 69 f no global maxi
55850 6d 75 6d 20 69 73 20 63 6f 6e 66 69 67 75 72 65 mum is configure
55860 64 2c 20 74 68 65 6e 20 74 68 65 20 73 79 73 74 d, then the syst
55870 65 6d 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c em attempts to l
55880 69 6d 69 74 0a 2a 2a 20 74 68 65 20 74 6f 74 61 imit.** the tota
55890 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 l number of page
558a0 73 20 63 61 63 68 65 64 20 62 79 20 70 75 72 67 s cached by purg
558b0 65 61 62 6c 65 20 70 61 67 65 72 2d 63 61 63 68 eable pager-cach
558c0 65 73 20 74 6f 20 74 68 65 20 73 75 6d 0a 2a 2a es to the sum.**
558d0 20 6f 66 20 74 68 65 20 73 75 67 67 65 73 74 65 of the suggeste
558e0 64 20 63 61 63 68 65 2d 73 69 7a 65 73 2e 0a 2a d cache-sizes..*
558f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
55900 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
55910 63 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 cheSetCachesize(
55920 50 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a PCache *, int);.
55930 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
55940 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ST.SQLITE_PRIVAT
55950 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
55960 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65 28 cheGetCachesize(
55970 50 43 61 63 68 65 20 2a 29 3b 0a 23 65 6e 64 69 PCache *);.#endi
55980 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
55990 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d _ENABLE_MEMORY_M
559a0 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 20 54 72 79 ANAGEMENT./* Try
559b0 20 74 6f 20 72 65 74 75 72 6e 20 6d 65 6d 6f 72 to return memor
559c0 79 20 75 73 65 64 20 62 79 20 74 68 65 20 70 63 y used by the pc
559d0 61 63 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 74 ache module to t
559e0 68 65 20 6d 61 69 6e 20 6d 65 6d 6f 72 79 20 68 he main memory h
559f0 65 61 70 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 eap */.SQLITE_PR
55a00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
55a10 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65 3PcacheReleaseMe
55a20 6d 6f 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 mory(int);.#endi
55a30 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
55a40 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 _TEST.SQLITE_PRI
55a50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
55a60 33 50 63 61 63 68 65 53 74 61 74 73 28 69 6e 74 3PcacheStats(int
55a70 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a *,int*,int*,int*
55a80 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 );.#endif..SQLIT
55a90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
55aa0 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 74 44 qlite3PCacheSetD
55ab0 65 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 0a 23 efault(void);..#
55ac0 65 6e 64 69 66 20 2f 2a 20 5f 50 43 41 43 48 45 endif /* _PCACHE
55ad0 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a _H_ */../*******
55ae0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 ******* End of p
55af0 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a cache.h ********
55b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55b20 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
55b30 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
55b40 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
55b50 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e off in sqliteIn
55b60 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
55b70 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a ******/../******
55b80 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 ******** Include
55b90 20 6f 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 os.h in the mid
55ba0 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 dle of sqliteInt
55bb0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
55bc0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
55bd0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
55be0 69 6c 65 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a ile os.h *******
55bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55c10 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
55c20 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 36 001 September 16
55c30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
55c40 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
55c50 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
55c60 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
55c70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
55c80 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
55c90 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
55ca0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
55cb0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
55cc0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
55cd0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
55ce0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
55cf0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
55d00 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
55d10 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
55d20 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
55d30 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
55d40 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
55d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55d90 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
55da0 68 65 61 64 65 72 20 66 69 6c 65 20 28 74 6f 67 header file (tog
55db0 65 74 68 65 72 20 77 69 74 68 20 69 73 20 63 6f ether with is co
55dc0 6d 70 61 6e 69 6f 6e 20 43 20 73 6f 75 72 63 65 mpanion C source
55dd0 2d 63 6f 64 65 20 66 69 6c 65 0a 2a 2a 20 22 6f -code file.** "o
55de0 73 2e 63 22 29 20 61 74 74 65 6d 70 74 20 74 6f s.c") attempt to
55df0 20 61 62 73 74 72 61 63 74 20 74 68 65 20 75 6e abstract the un
55e00 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 derlying operati
55e10 6e 67 20 73 79 73 74 65 6d 20 73 6f 20 74 68 61 ng system so tha
55e20 74 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 t.** the SQLite
55e30 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 77 6f 72 library will wor
55e40 6b 20 6f 6e 20 62 6f 74 68 20 50 4f 53 49 58 20 k on both POSIX
55e50 61 6e 64 20 77 69 6e 64 6f 77 73 20 73 79 73 74 and windows syst
55e60 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ems..**.** This
55e70 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 23 header file is #
55e80 69 6e 63 6c 75 64 65 2d 65 64 20 62 79 20 73 71 include-ed by sq
55e90 6c 69 74 65 49 6e 74 2e 68 20 61 6e 64 20 74 68 liteInt.h and th
55ea0 75 73 20 65 6e 64 73 20 75 70 0a 2a 2a 20 62 65 us ends up.** be
55eb0 69 6e 67 20 69 6e 63 6c 75 64 65 64 20 62 79 20 ing included by
55ec0 65 76 65 72 79 20 73 6f 75 72 63 65 20 66 69 6c every source fil
55ed0 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 e..**.** $Id: os
55ee0 2e 68 2c 76 20 31 2e 31 30 38 20 32 30 30 39 2f .h,v 1.108 2009/
55ef0 30 32 2f 30 35 20 31 36 3a 33 31 3a 34 36 20 64 02/05 16:31:46 d
55f00 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e rh Exp $.*/.#ifn
55f10 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 def _SQLITE_OS_H
55f20 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 _.#define _SQLIT
55f30 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 46 E_OS_H_../*.** F
55f40 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 igure out if we
55f50 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 are dealing with
55f60 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20 Unix, Windows,
55f70 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a or some other.**
55f80 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
55f90 6d 2e 20 20 41 66 74 65 72 20 74 68 65 20 66 6f m. After the fo
55fa0 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 llowing block of
55fb0 20 70 72 65 70 72 6f 63 65 73 73 20 6d 61 63 72 preprocess macr
55fc0 6f 73 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53 51 os,.** all of SQ
55fd0 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53 51 LITE_OS_UNIX, SQ
55fe0 4c 49 54 45 5f 4f 53 5f 57 49 4e 2c 20 53 51 4c LITE_OS_WIN, SQL
55ff0 49 54 45 5f 4f 53 5f 4f 53 32 2c 20 61 6e 64 20 ITE_OS_OS2, and
56000 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 20 SQLITE_OS_OTHER
56010 0a 2a 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65 64 .** will defined
56020 20 74 6f 20 65 69 74 68 65 72 20 31 20 6f 72 20 to either 1 or
56030 30 2e 20 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 0. One of the f
56040 6f 75 72 20 77 69 6c 6c 20 62 65 20 31 2e 20 20 our will be 1.
56050 54 68 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74 68 The other .** th
56060 72 65 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a 2a ree will be 0..*
56070 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 /.#if defined(SQ
56080 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 LITE_OS_OTHER).#
56090 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 if SQLITE_OS_OT
560a0 48 45 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65 66 HER==1.# undef
560b0 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a SQLITE_OS_UNIX.
560c0 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 # define SQLIT
560d0 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 E_OS_UNIX 0.#
560e0 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f undef SQLITE_OS_
560f0 57 49 4e 0a 23 20 20 20 64 65 66 69 6e 65 20 53 WIN.# define S
56100 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 QLITE_OS_WIN 0.#
56110 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f undef SQLITE_
56120 4f 53 5f 4f 53 32 0a 23 20 20 20 64 65 66 69 6e OS_OS2.# defin
56130 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 e SQLITE_OS_OS2
56140 30 0a 23 20 65 6c 73 65 0a 23 20 20 20 75 6e 64 0.# else.# und
56150 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 ef SQLITE_OS_OTH
56160 45 52 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 ER.# endif.#endi
56170 66 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 f.#if !defined(S
56180 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 29 20 26 QLITE_OS_UNIX) &
56190 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 & !defined(SQLIT
561a0 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 64 65 E_OS_OTHER).# de
561b0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f fine SQLITE_OS_O
561c0 54 48 45 52 20 30 0a 23 20 69 66 6e 64 65 66 20 THER 0.# ifndef
561d0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 SQLITE_OS_WIN.#
561e0 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 if defined(_WI
561f0 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 N32) || defined(
56200 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 WIN32) || define
56210 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c 7c d(__CYGWIN__) ||
56220 20 64 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47 57 defined(__MINGW
56230 33 32 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 32__) || defined
56240 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23 (__BORLANDC__).#
56250 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 define SQLI
56260 54 45 5f 4f 53 5f 57 49 4e 20 31 0a 23 20 20 20 TE_OS_WIN 1.#
56270 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
56280 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20 OS_UNIX 0.#
56290 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
562a0 5f 4f 53 32 20 30 0a 23 20 20 20 65 6c 69 66 20 _OS2 0.# elif
562b0 64 65 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f 29 defined(__EMX__)
562c0 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32 || defined(_OS2
562d0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53 32 ) || defined(OS2
562e0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 ) || defined(_OS
562f0 32 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 2_) || defined(_
56300 5f 4f 53 32 5f 5f 29 0a 23 20 20 20 20 20 64 65 _OS2__).# de
56310 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 fine SQLITE_OS_W
56320 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e IN 0.# defin
56330 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 e SQLITE_OS_UNIX
56340 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 0.# define
56350 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 31 0a SQLITE_OS_OS2 1.
56360 23 20 20 20 65 6c 73 65 0a 23 20 20 20 20 20 64 # else.# d
56370 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
56380 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 WIN 0.# defi
56390 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 ne SQLITE_OS_UNI
563a0 58 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65 X 1.# define
563b0 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 SQLITE_OS_OS2 0
563c0 0a 23 20 20 65 6e 64 69 66 0a 23 20 65 6c 73 65 .# endif.# else
563d0 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 .# define SQLIT
563e0 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 64 E_OS_UNIX 0.# d
563f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
56400 4f 53 32 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 OS2 0.# endif.#e
56410 6c 73 65 0a 23 20 69 66 6e 64 65 66 20 53 51 4c lse.# ifndef SQL
56420 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64 65 ITE_OS_WIN.# de
56430 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 fine SQLITE_OS_W
56440 49 4e 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e IN 0.# endif.#en
56450 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 dif../*.** Deter
56460 6d 69 6e 65 20 69 66 20 77 65 20 61 72 65 20 64 mine if we are d
56470 65 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e 64 ealing with Wind
56480 6f 77 73 43 45 20 2d 20 77 68 69 63 68 20 68 61 owsCE - which ha
56490 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64 75 s a much.** redu
564a0 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 ced API..*/.#if
564b0 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 5f 57 defined(_WIN32_W
564c0 43 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c CE).# define SQL
564d0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 31 0a 23 ITE_OS_WINCE 1.#
564e0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 else.# define SQ
564f0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 30 0a LITE_OS_WINCE 0.
56500 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 #endif.../*.** D
56510 65 66 69 6e 65 20 74 68 65 20 6d 61 78 69 6d 75 efine the maximu
56520 6d 20 73 69 7a 65 20 6f 66 20 61 20 74 65 6d 70 m size of a temp
56530 6f 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 0a 2a orary filename.*
56540 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
56550 57 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 77 WIN.# include <w
56560 69 6e 64 6f 77 73 2e 68 3e 0a 23 20 64 65 66 69 indows.h>.# defi
56570 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 ne SQLITE_TEMPNA
56580 4d 45 5f 53 49 5a 45 20 28 4d 41 58 5f 50 41 54 ME_SIZE (MAX_PAT
56590 48 2b 35 30 29 0a 23 65 6c 69 66 20 53 51 4c 49 H+50).#elif SQLI
565a0 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 69 66 20 28 TE_OS_OS2.# if (
565b0 5f 5f 47 4e 55 43 5f 5f 20 3e 20 33 20 7c 7c 20 __GNUC__ > 3 ||
565c0 5f 5f 47 4e 55 43 5f 5f 20 3d 3d 20 33 20 26 26 __GNUC__ == 3 &&
565d0 20 5f 5f 47 4e 55 43 5f 4d 49 4e 4f 52 5f 5f 20 __GNUC_MINOR__
565e0 3e 3d 20 33 29 20 26 26 20 64 65 66 69 6e 65 64 >= 3) && defined
565f0 28 4f 53 32 5f 48 49 47 48 5f 4d 45 4d 4f 52 59 (OS2_HIGH_MEMORY
56600 29 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6f 73 ).# include <os
56610 32 73 61 66 65 2e 68 3e 20 2f 2a 20 68 61 73 20 2safe.h> /* has
56620 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 62 to be included b
56630 65 66 6f 72 65 20 6f 73 32 2e 68 20 66 6f 72 20 efore os2.h for
56640 6c 69 6e 6b 69 6e 67 20 74 6f 20 77 6f 72 6b 20 linking to work
56650 2a 2f 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 */.# endif.# def
56660 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 44 41 54 45 ine INCL_DOSDATE
56670 54 49 4d 45 0a 23 20 64 65 66 69 6e 65 20 49 4e TIME.# define IN
56680 43 4c 5f 44 4f 53 46 49 4c 45 4d 47 52 0a 23 20 CL_DOSFILEMGR.#
56690 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 45 define INCL_DOSE
566a0 52 52 4f 52 53 0a 23 20 64 65 66 69 6e 65 20 49 RRORS.# define I
566b0 4e 43 4c 5f 44 4f 53 4d 49 53 43 0a 23 20 64 65 NCL_DOSMISC.# de
566c0 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 50 52 4f fine INCL_DOSPRO
566d0 43 45 53 53 0a 23 20 64 65 66 69 6e 65 20 49 4e CESS.# define IN
566e0 43 4c 5f 44 4f 53 4d 4f 44 55 4c 45 4d 47 52 0a CL_DOSMODULEMGR.
566f0 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f # define INCL_DO
56700 53 53 45 4d 41 50 48 4f 52 45 53 0a 23 20 69 6e SSEMAPHORES.# in
56710 63 6c 75 64 65 20 3c 6f 73 32 2e 68 3e 0a 23 20 clude <os2.h>.#
56720 69 6e 63 6c 75 64 65 20 3c 75 63 6f 6e 76 2e 68 include <uconv.h
56730 3e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 >.# define SQLIT
56740 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 E_TEMPNAME_SIZE
56750 28 43 43 48 4d 41 58 50 41 54 48 43 4f 4d 50 29 (CCHMAXPATHCOMP)
56760 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
56770 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f SQLITE_TEMPNAME_
56780 53 49 5a 45 20 32 30 30 0a 23 65 6e 64 69 66 0a SIZE 200.#endif.
56790 0a 2f 2a 20 49 66 20 74 68 65 20 53 45 54 5f 46 ./* If the SET_F
567a0 55 4c 4c 53 59 4e 43 20 6d 61 63 72 6f 20 69 73 ULLSYNC macro is
567b0 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 61 62 6f not defined abo
567c0 76 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 69 74 ve, then make it
567d0 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a 23 .** a no-op.*/.#
567e0 69 66 6e 64 65 66 20 53 45 54 5f 46 55 4c 4c 53 ifndef SET_FULLS
567f0 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 53 45 54 YNC.# define SET
56800 5f 46 55 4c 4c 53 59 4e 43 28 78 2c 79 29 0a 23 _FULLSYNC(x,y).#
56810 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
56820 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f 66 default size of
56830 20 61 20 64 69 73 6b 20 73 65 63 74 6f 72 0a 2a a disk sector.*
56840 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
56850 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f _DEFAULT_SECTOR_
56860 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 SIZE.# define SQ
56870 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 LITE_DEFAULT_SEC
56880 54 4f 52 5f 53 49 5a 45 20 35 31 32 0a 23 65 6e TOR_SIZE 512.#en
56890 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 6d 70 6f dif../*.** Tempo
568a0 72 61 72 79 20 66 69 6c 65 73 20 61 72 65 20 6e rary files are n
568b0 61 6d 65 64 20 73 74 61 72 74 69 6e 67 20 77 69 amed starting wi
568c0 74 68 20 74 68 69 73 20 70 72 65 66 69 78 20 66 th this prefix f
568d0 6f 6c 6c 6f 77 65 64 20 62 79 20 31 36 20 72 61 ollowed by 16 ra
568e0 6e 64 6f 6d 0a 2a 2a 20 61 6c 70 68 61 6e 75 6d ndom.** alphanum
568f0 65 72 69 63 20 63 68 61 72 61 63 74 65 72 73 2c eric characters,
56900 20 61 6e 64 20 6e 6f 20 66 69 6c 65 20 65 78 74 and no file ext
56910 65 6e 73 69 6f 6e 2e 20 54 68 65 79 20 61 72 65 ension. They are
56920 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a 2a stored in the.*
56930 2a 20 4f 53 27 73 20 73 74 61 6e 64 61 72 64 20 * OS's standard
56940 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 64 temporary file d
56950 69 72 65 63 74 6f 72 79 2c 20 61 6e 64 20 61 72 irectory, and ar
56960 65 20 64 65 6c 65 74 65 64 20 70 72 69 6f 72 20 e deleted prior
56970 74 6f 20 65 78 69 74 2e 0a 2a 2a 20 49 66 20 73 to exit..** If s
56980 71 6c 69 74 65 20 69 73 20 62 65 69 6e 67 20 65 qlite is being e
56990 6d 62 65 64 64 65 64 20 69 6e 20 61 6e 6f 74 68 mbedded in anoth
569a0 65 72 20 70 72 6f 67 72 61 6d 2c 20 79 6f 75 20 er program, you
569b0 6d 61 79 20 77 69 73 68 20 74 6f 20 63 68 61 6e may wish to chan
569c0 67 65 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 78 ge the.** prefix
569d0 20 74 6f 20 72 65 66 6c 65 63 74 20 79 6f 75 72 to reflect your
569e0 20 70 72 6f 67 72 61 6d 27 73 20 6e 61 6d 65 2c program's name,
569f0 20 73 6f 20 74 68 61 74 20 69 66 20 79 6f 75 72 so that if your
56a00 20 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a program exits.*
56a10 2a 20 70 72 65 6d 61 74 75 72 65 6c 79 2c 20 6f * prematurely, o
56a20 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c ld temporary fil
56a30 65 73 20 63 61 6e 20 62 65 20 65 61 73 69 6c 79 es can be easily
56a40 20 69 64 65 6e 74 69 66 69 65 64 2e 20 54 68 69 identified. Thi
56a50 73 20 63 61 6e 20 62 65 20 64 6f 6e 65 0a 2a 2a s can be done.**
56a60 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f using -DSQLITE_
56a70 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 TEMP_FILE_PREFIX
56a80 3d 6d 79 70 72 65 66 69 78 5f 20 6f 6e 20 74 68 =myprefix_ on th
56a90 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 e compiler comma
56aa0 6e 64 20 6c 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32 nd line..**.** 2
56ab0 30 30 36 2d 31 30 2d 33 31 3a 20 20 54 68 65 20 006-10-31: The
56ac0 64 65 66 61 75 6c 74 20 70 72 65 66 69 78 20 75 default prefix u
56ad0 73 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69 74 sed to be "sqlit
56ae0 65 5f 22 2e 20 20 42 75 74 20 74 68 65 6e 0a 2a e_". But then.*
56af0 2a 20 4d 63 61 66 65 65 20 73 74 61 72 74 65 64 * Mcafee started
56b00 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e using SQLite in
56b10 20 74 68 65 69 72 20 61 6e 74 69 2d 76 69 72 75 their anti-viru
56b20 73 20 70 72 6f 64 75 63 74 20 61 6e 64 20 69 74 s product and it
56b30 0a 2a 2a 20 73 74 61 72 74 65 64 20 70 75 74 74 .** started putt
56b40 69 6e 67 20 66 69 6c 65 73 20 77 69 74 68 20 74 ing files with t
56b50 68 65 20 22 73 71 6c 69 74 65 22 20 6e 61 6d 65 he "sqlite" name
56b60 20 69 6e 20 74 68 65 20 63 3a 2f 74 65 6d 70 20 in the c:/temp
56b70 66 6f 6c 64 65 72 2e 0a 2a 2a 20 54 68 69 73 20 folder..** This
56b80 61 6e 6e 6f 79 65 64 20 6d 61 6e 79 20 77 69 6e annoyed many win
56b90 64 6f 77 73 20 75 73 65 72 73 2e 20 20 54 68 6f dows users. Tho
56ba0 73 65 20 75 73 65 72 73 20 77 6f 75 6c 64 20 74 se users would t
56bb0 68 65 6e 20 64 6f 20 61 20 0a 2a 2a 20 47 6f 6f hen do a .** Goo
56bc0 67 6c 65 20 73 65 61 72 63 68 20 66 6f 72 20 22 gle search for "
56bd0 73 71 6c 69 74 65 22 2c 20 66 69 6e 64 20 74 68 sqlite", find th
56be0 65 20 74 65 6c 65 70 68 6f 6e 65 20 6e 75 6d 62 e telephone numb
56bf0 65 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65 ers of the.** de
56c00 76 65 6c 6f 70 65 72 73 20 61 6e 64 20 63 61 6c velopers and cal
56c10 6c 20 74 6f 20 77 61 6b 65 20 74 68 65 6d 20 75 l to wake them u
56c20 70 20 61 74 20 6e 69 67 68 74 20 61 6e 64 20 63 p at night and c
56c30 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a 20 46 6f 72 20 omplain..** For
56c40 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65 this reason, the
56c50 20 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 70 72 default name pr
56c60 65 66 69 78 20 69 73 20 63 68 61 6e 67 65 64 20 efix is changed
56c70 74 6f 20 62 65 20 22 73 71 6c 69 74 65 22 20 0a to be "sqlite" .
56c80 2a 2a 20 73 70 65 6c 6c 65 64 20 62 61 63 6b 77 ** spelled backw
56c90 61 72 64 73 2e 20 20 53 6f 20 74 68 65 20 74 65 ards. So the te
56ca0 6d 70 20 66 69 6c 65 73 20 61 72 65 20 73 74 69 mp files are sti
56cb0 6c 6c 20 69 64 65 6e 74 69 66 69 65 64 2c 20 62 ll identified, b
56cc0 75 74 0a 2a 2a 20 61 6e 79 62 6f 64 79 20 73 6d ut.** anybody sm
56cd0 61 72 74 20 65 6e 6f 75 67 68 20 74 6f 20 66 69 art enough to fi
56ce0 67 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f 64 gure out the cod
56cf0 65 20 69 73 20 61 6c 73 6f 20 6c 69 6b 65 6c 79 e is also likely
56d00 20 73 6d 61 72 74 0a 2a 2a 20 65 6e 6f 75 67 68 smart.** enough
56d10 20 74 6f 20 6b 6e 6f 77 20 74 68 61 74 20 63 61 to know that ca
56d20 6c 6c 69 6e 67 20 74 68 65 20 64 65 76 65 6c 6f lling the develo
56d30 70 65 72 20 77 69 6c 6c 20 6e 6f 74 20 68 65 6c per will not hel
56d40 70 20 67 65 74 20 72 69 64 0a 2a 2a 20 6f 66 20 p get rid.** of
56d50 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 the file..*/.#if
56d60 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 ndef SQLITE_TEMP
56d70 5f 46 49 4c 45 5f 50 52 45 46 49 58 0a 23 20 64 _FILE_PREFIX.# d
56d80 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d efine SQLITE_TEM
56d90 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 20 22 65 P_FILE_PREFIX "e
56da0 74 69 6c 71 73 5f 22 0a 23 65 6e 64 69 66 0a 0a tilqs_".#endif..
56db0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
56dc0 69 6e 67 20 76 61 6c 75 65 73 20 6d 61 79 20 62 ing values may b
56dd0 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
56de0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
56df0 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c to.** sqlite3OsL
56e00 6f 63 6b 28 29 2e 20 54 68 65 20 76 61 72 69 6f ock(). The vario
56e10 75 73 20 6c 6f 63 6b 73 20 65 78 68 69 62 69 74 us locks exhibit
56e20 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
56e30 65 6d 61 6e 74 69 63 73 3a 0a 2a 2a 0a 2a 2a 20 emantics:.**.**
56e40 53 48 41 52 45 44 3a 20 20 20 20 41 6e 79 20 6e SHARED: Any n
56e50 75 6d 62 65 72 20 6f 66 20 70 72 6f 63 65 73 73 umber of process
56e60 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 53 48 es may hold a SH
56e70 41 52 45 44 20 6c 6f 63 6b 20 73 69 6d 75 6c 74 ARED lock simult
56e80 61 6e 65 6f 75 73 6c 79 2e 0a 2a 2a 20 52 45 53 aneously..** RES
56e90 45 52 56 45 44 3a 20 20 41 20 73 69 6e 67 6c 65 ERVED: A single
56ea0 20 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f 6c process may hol
56eb0 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 d a RESERVED loc
56ec0 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a k on a file at.*
56ed0 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79 * any
56ee0 20 74 69 6d 65 2e 20 4f 74 68 65 72 20 70 72 6f time. Other pro
56ef0 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 20 cesses may hold
56f00 61 6e 64 20 6f 62 74 61 69 6e 20 6e 65 77 20 53 and obtain new S
56f10 48 41 52 45 44 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 HARED locks..**
56f20 50 45 4e 44 49 4e 47 3a 20 20 20 41 20 73 69 6e PENDING: A sin
56f30 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 79 20 gle process may
56f40 68 6f 6c 64 20 61 20 50 45 4e 44 49 4e 47 20 6c hold a PENDING l
56f50 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 ock on a file at
56f60 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 .** a
56f70 6e 79 20 6f 6e 65 20 74 69 6d 65 2e 20 45 78 69 ny one time. Exi
56f80 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 sting SHARED loc
56f90 6b 73 20 6d 61 79 20 70 65 72 73 69 73 74 2c 20 ks may persist,
56fa0 62 75 74 20 6e 6f 20 6e 65 77 0a 2a 2a 20 20 20 but no new.**
56fb0 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 20 SHARED
56fc0 6c 6f 63 6b 73 20 6d 61 79 20 62 65 20 6f 62 74 locks may be obt
56fd0 61 69 6e 65 64 20 62 79 20 6f 74 68 65 72 20 70 ained by other p
56fe0 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 45 58 43 rocesses..** EXC
56ff0 4c 55 53 49 56 45 3a 20 41 6e 20 45 58 43 4c 55 LUSIVE: An EXCLU
57000 53 49 56 45 20 6c 6f 63 6b 20 70 72 65 63 6c 75 SIVE lock preclu
57010 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f des all other lo
57020 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 50 45 4e 44 49 cks..**.** PENDI
57030 4e 47 5f 4c 4f 43 4b 20 6d 61 79 20 6e 6f 74 20 NG_LOCK may not
57040 62 65 20 70 61 73 73 65 64 20 64 69 72 65 63 74 be passed direct
57050 6c 79 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4c ly to sqlite3OsL
57060 6f 63 6b 28 29 2e 20 49 6e 73 74 65 61 64 2c 20 ock(). Instead,
57070 61 0a 2a 2a 20 70 72 6f 63 65 73 73 20 74 68 61 a.** process tha
57080 74 20 72 65 71 75 65 73 74 73 20 61 6e 20 45 58 t requests an EX
57090 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6d 61 79 CLUSIVE lock may
570a0 20 61 63 74 75 61 6c 6c 79 20 6f 62 74 61 69 6e actually obtain
570b0 20 61 20 50 45 4e 44 49 4e 47 0a 2a 2a 20 6c 6f a PENDING.** lo
570c0 63 6b 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 ck. This can be
570d0 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 upgraded to an E
570e0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 62 79 XCLUSIVE lock by
570f0 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 a subsequent ca
57100 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 ll to.** sqlite3
57110 4f 73 4c 6f 63 6b 28 29 2e 0a 2a 2f 0a 23 64 65 OsLock()..*/.#de
57120 66 69 6e 65 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 fine NO_LOCK
57130 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 0.#define S
57140 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 31 HARED_LOCK 1
57150 0a 23 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 .#define RESERVE
57160 44 5f 4c 4f 43 4b 20 20 20 32 0a 23 64 65 66 69 D_LOCK 2.#defi
57170 6e 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 ne PENDING_LOCK
57180 20 20 20 33 0a 23 64 65 66 69 6e 65 20 45 58 43 3.#define EXC
57190 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 34 0a 0a LUSIVE_LOCK 4..
571a0 2f 2a 0a 2a 2a 20 46 69 6c 65 20 4c 6f 63 6b 69 /*.** File Locki
571b0 6e 67 20 4e 6f 74 65 73 3a 20 20 28 4d 6f 73 74 ng Notes: (Most
571c0 6c 79 20 61 62 6f 75 74 20 77 69 6e 64 6f 77 73 ly about windows
571d0 20 62 75 74 20 61 6c 73 6f 20 73 6f 6d 65 20 69 but also some i
571e0 6e 66 6f 20 66 6f 72 20 55 6e 69 78 29 0a 2a 2a nfo for Unix).**
571f0 0a 2a 2a 20 57 65 20 63 61 6e 6e 6f 74 20 75 73 .** We cannot us
57200 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f e LockFileEx() o
57210 72 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 r UnlockFileEx()
57220 20 6f 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 on Win95/98/ME
57230 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 6f 73 65 because.** those
57240 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e functions are n
57250 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 53 ot available. S
57260 6f 20 77 65 20 75 73 65 20 6f 6e 6c 79 20 4c 6f o we use only Lo
57270 63 6b 46 69 6c 65 28 29 20 61 6e 64 0a 2a 2a 20 ckFile() and.**
57280 55 6e 6c 6f 63 6b 46 69 6c 65 28 29 2e 0a 2a 2a UnlockFile()..**
57290 0a 2a 2a 20 4c 6f 63 6b 46 69 6c 65 28 29 20 70 .** LockFile() p
572a0 72 65 76 65 6e 74 73 20 6e 6f 74 20 6a 75 73 74 revents not just
572b0 20 77 72 69 74 69 6e 67 20 62 75 74 20 61 6c 73 writing but als
572c0 6f 20 72 65 61 64 69 6e 67 20 62 79 20 6f 74 68 o reading by oth
572d0 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a er processes..**
572e0 20 41 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 69 A SHARED_LOCK i
572f0 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f s obtained by lo
57300 63 6b 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 72 cking a single r
57310 61 6e 64 6f 6d 6c 79 2d 63 68 6f 73 65 6e 20 0a andomly-chosen .
57320 2a 2a 20 62 79 74 65 20 6f 75 74 20 6f 66 20 61 ** byte out of a
57330 20 73 70 65 63 69 66 69 63 20 72 61 6e 67 65 20 specific range
57340 6f 66 20 62 79 74 65 73 2e 20 54 68 65 20 6c 6f of bytes. The lo
57350 63 6b 20 62 79 74 65 20 69 73 20 6f 62 74 61 69 ck byte is obtai
57360 6e 65 64 20 61 74 20 0a 2a 2a 20 72 61 6e 64 6f ned at .** rando
57370 6d 20 73 6f 20 74 77 6f 20 73 65 70 61 72 61 74 m so two separat
57380 65 20 72 65 61 64 65 72 73 20 63 61 6e 20 70 72 e readers can pr
57390 6f 62 61 62 6c 79 20 61 63 63 65 73 73 20 74 68 obably access th
573a0 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 0a 2a e file at the .*
573b0 2a 20 73 61 6d 65 20 74 69 6d 65 2c 20 75 6e 6c * same time, unl
573c0 65 73 73 20 74 68 65 79 20 61 72 65 20 75 6e 6c ess they are unl
573d0 75 63 6b 79 20 61 6e 64 20 63 68 6f 6f 73 65 20 ucky and choose
573e0 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 20 62 79 the same lock by
573f0 74 65 2e 0a 2a 2a 20 41 6e 20 45 58 43 4c 55 53 te..** An EXCLUS
57400 49 56 45 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 IVE_LOCK is obta
57410 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 ined by locking
57420 61 6c 6c 20 62 79 74 65 73 20 69 6e 20 74 68 65 all bytes in the
57430 20 72 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 72 65 range..** There
57440 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 6f 6e 65 can only be one
57450 20 77 72 69 74 65 72 2e 20 20 41 20 52 45 53 45 writer. A RESE
57460 52 56 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 RVED_LOCK is obt
57470 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 ained by locking
57480 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 62 79 74 .** a single byt
57490 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 68 e of the file th
574a0 61 74 20 69 73 20 64 65 73 69 67 6e 61 74 65 64 at is designated
574b0 20 61 73 20 74 68 65 20 72 65 73 65 72 76 65 64 as the reserved
574c0 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 lock byte..** A
574d0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 69 73 PENDING_LOCK is
574e0 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 obtained by loc
574f0 6b 69 6e 67 20 61 20 64 65 73 69 67 6e 61 74 65 king a designate
57500 64 20 62 79 74 65 20 64 69 66 66 65 72 65 6e 74 d byte different
57510 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 52 45 53 from.** the RES
57520 45 52 56 45 44 5f 4c 4f 43 4b 20 62 79 74 65 2e ERVED_LOCK byte.
57530 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57 69 6e 4e 54 2f .**.** On WinNT/
57540 32 4b 2f 58 50 20 73 79 73 74 65 6d 73 2c 20 4c 2K/XP systems, L
57550 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 6e 64 20 ockFileEx() and
57560 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 UnlockFileEx() a
57570 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 0a 2a 2a re available,.**
57580 20 77 68 69 63 68 20 6d 65 61 6e 73 20 77 65 20 which means we
57590 63 61 6e 20 75 73 65 20 72 65 61 64 65 72 2f 77 can use reader/w
575a0 72 69 74 65 72 20 6c 6f 63 6b 73 2e 20 20 57 68 riter locks. Wh
575b0 65 6e 20 72 65 61 64 65 72 2f 77 72 69 74 65 72 en reader/writer
575c0 20 6c 6f 63 6b 73 0a 2a 2a 20 61 72 65 20 75 73 locks.** are us
575d0 65 64 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 ed, the lock is
575e0 70 6c 61 63 65 64 20 6f 6e 20 74 68 65 20 73 61 placed on the sa
575f0 6d 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 me range of byte
57600 73 20 74 68 61 74 20 69 73 20 75 73 65 64 0a 2a s that is used.*
57610 2a 20 66 6f 72 20 70 72 6f 62 61 62 69 6c 69 73 * for probabilis
57620 74 69 63 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 57 tic locking in W
57630 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 48 65 6e in95/98/ME. Hen
57640 63 65 2c 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 ce, the locking
57650 73 63 68 65 6d 65 0a 2a 2a 20 77 69 6c 6c 20 73 scheme.** will s
57660 75 70 70 6f 72 74 20 74 77 6f 20 6f 72 20 6d 6f upport two or mo
57670 72 65 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 re Win95 readers
57680 20 6f 72 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 or two or more
57690 57 69 6e 4e 54 20 72 65 61 64 65 72 73 2e 0a 2a WinNT readers..*
576a0 2a 20 42 75 74 20 61 20 73 69 6e 67 6c 65 20 57 * But a single W
576b0 69 6e 39 35 20 72 65 61 64 65 72 20 77 69 6c 6c in95 reader will
576c0 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 57 69 lock out all Wi
576d0 6e 4e 54 20 72 65 61 64 65 72 73 20 61 6e 64 20 nNT readers and
576e0 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 57 69 6e 4e a single.** WinN
576f0 54 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f T reader will lo
57700 63 6b 20 6f 75 74 20 61 6c 6c 20 6f 74 68 65 72 ck out all other
57710 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 2e 0a Win95 readers..
57720 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 **.** The follow
57730 69 6e 67 20 23 64 65 66 69 6e 65 73 20 73 70 65 ing #defines spe
57740 63 69 66 79 20 74 68 65 20 72 61 6e 67 65 20 6f cify the range o
57750 66 20 62 79 74 65 73 20 75 73 65 64 20 66 6f 72 f bytes used for
57760 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 53 48 41 locking..** SHA
57770 52 45 44 5f 53 49 5a 45 20 69 73 20 74 68 65 20 RED_SIZE is the
57780 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
57790 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 available in the
577a0 20 70 6f 6f 6c 20 66 72 6f 6d 20 77 68 69 63 68 pool from which
577b0 0a 2a 2a 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 .** a random byt
577c0 65 20 69 73 20 73 65 6c 65 63 74 65 64 20 66 6f e is selected fo
577d0 72 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e r a shared lock.
577e0 20 20 54 68 65 20 70 6f 6f 6c 20 6f 66 20 62 79 The pool of by
577f0 74 65 73 20 66 6f 72 0a 2a 2a 20 73 68 61 72 65 tes for.** share
57800 64 20 6c 6f 63 6b 73 20 62 65 67 69 6e 73 20 61 d locks begins a
57810 74 20 53 48 41 52 45 44 5f 46 49 52 53 54 2e 20 t SHARED_FIRST.
57820 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6d 65 20 .**.** The same
57830 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 locking strategy
57840 20 61 6e 64 0a 2a 2a 20 62 79 74 65 20 72 61 6e and.** byte ran
57850 67 65 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 ges are used for
57860 20 55 6e 69 78 2e 20 20 54 68 69 73 20 6c 65 61 Unix. This lea
57870 76 65 73 20 6f 70 65 6e 20 74 68 65 20 70 6f 73 ves open the pos
57880 73 69 62 6c 69 74 79 20 6f 66 20 68 61 76 69 6e siblity of havin
57890 67 0a 2a 2a 20 63 6c 69 65 6e 74 73 20 6f 6e 20 g.** clients on
578a0 77 69 6e 39 35 2c 20 77 69 6e 4e 54 2c 20 61 6e win95, winNT, an
578b0 64 20 75 6e 69 78 20 61 6c 6c 20 74 61 6c 6b 69 d unix all talki
578c0 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 73 ng to the same s
578d0 68 61 72 65 64 20 66 69 6c 65 0a 2a 2a 20 61 6e hared file.** an
578e0 64 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f d all locking co
578f0 72 72 65 63 74 6c 79 2e 20 20 54 6f 20 64 6f 20 rrectly. To do
57900 73 6f 20 77 6f 75 6c 64 20 72 65 71 75 69 72 65 so would require
57910 20 74 68 61 74 20 73 61 6d 62 61 20 28 6f 72 20 that samba (or
57920 77 68 61 74 65 76 65 72 0a 2a 2a 20 74 6f 6f 6c whatever.** tool
57930 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 66 is being used f
57940 6f 72 20 66 69 6c 65 20 73 68 61 72 69 6e 67 29 or file sharing)
57950 20 69 6d 70 6c 65 6d 65 6e 74 73 20 6c 6f 63 6b implements lock
57960 73 20 63 6f 72 72 65 63 74 6c 79 20 62 65 74 77 s correctly betw
57970 65 65 6e 0a 2a 2a 20 77 69 6e 64 6f 77 73 20 61 een.** windows a
57980 6e 64 20 75 6e 69 78 2e 20 20 49 27 6d 20 67 75 nd unix. I'm gu
57990 65 73 73 69 6e 67 20 74 68 61 74 20 69 73 6e 27 essing that isn'
579a0 74 20 6c 69 6b 65 6c 79 20 74 6f 20 68 61 70 70 t likely to happ
579b0 65 6e 2c 20 62 75 74 20 62 79 0a 2a 2a 20 75 73 en, but by.** us
579c0 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 ing the same loc
579d0 6b 69 6e 67 20 72 61 6e 67 65 20 77 65 20 61 72 king range we ar
579e0 65 20 61 74 20 6c 65 61 73 74 20 6f 70 65 6e 20 e at least open
579f0 74 6f 20 74 68 65 20 70 6f 73 73 69 62 69 6c 69 to the possibili
57a00 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69 6e ty..**.** Lockin
57a10 67 20 69 6e 20 77 69 6e 64 6f 77 73 20 69 73 20 g in windows is
57a20 6d 61 6e 64 69 74 6f 72 79 2e 20 20 46 6f 72 20 manditory. For
57a30 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 77 65 20 this reason, we
57a40 63 61 6e 6e 6f 74 20 73 74 6f 72 65 0a 2a 2a 20 cannot store.**
57a50 61 63 74 75 61 6c 20 64 61 74 61 20 69 6e 20 74 actual data in t
57a60 68 65 20 62 79 74 65 73 20 75 73 65 64 20 66 6f he bytes used fo
57a70 72 20 6c 6f 63 6b 69 6e 67 2e 20 20 54 68 65 20 r locking. The
57a80 70 61 67 65 72 20 6e 65 76 65 72 20 61 6c 6c 6f pager never allo
57a90 63 61 74 65 73 0a 2a 2a 20 74 68 65 20 70 61 67 cates.** the pag
57aa0 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 6c es involved in l
57ab0 6f 63 6b 69 6e 67 20 74 68 65 72 65 66 6f 72 65 ocking therefore
57ac0 2e 20 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69 . SHARED_SIZE i
57ad0 73 20 73 65 6c 65 63 74 65 64 20 73 6f 0a 2a 2a s selected so.**
57ae0 20 74 68 61 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 that all locks
57af0 77 69 6c 6c 20 66 69 74 20 6f 6e 20 61 20 73 69 will fit on a si
57b00 6e 67 6c 65 20 70 61 67 65 20 65 76 65 6e 20 61 ngle page even a
57b10 74 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 70 61 t the minimum pa
57b20 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 50 45 4e 44 ge size..** PEND
57b30 49 4e 47 5f 42 59 54 45 20 64 65 66 69 6e 65 73 ING_BYTE defines
57b40 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
57b50 66 20 74 68 65 20 6c 6f 63 6b 73 2e 20 20 42 79 f the locks. By
57b60 20 64 65 66 61 75 6c 74 20 50 45 4e 44 49 4e 47 default PENDING
57b70 5f 42 59 54 45 0a 2a 2a 20 69 73 20 73 65 74 20 _BYTE.** is set
57b80 68 69 67 68 20 73 6f 20 74 68 61 74 20 77 65 20 high so that we
57b90 64 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 61 6c don't have to al
57ba0 6c 6f 63 61 74 65 20 61 6e 20 75 6e 75 73 65 64 locate an unused
57bb0 20 70 61 67 65 20 65 78 63 65 70 74 0a 2a 2a 20 page except.**
57bc0 66 6f 72 20 76 65 72 79 20 6c 61 72 67 65 20 64 for very large d
57bd0 61 74 61 62 61 73 65 73 2e 20 20 42 75 74 20 6f atabases. But o
57be0 6e 65 20 73 68 6f 75 6c 64 20 74 65 73 74 20 74 ne should test t
57bf0 68 65 20 70 61 67 65 20 73 6b 69 70 70 69 6e 67 he page skipping
57c00 20 6c 6f 67 69 63 20 0a 2a 2a 20 62 79 20 73 65 logic .** by se
57c10 74 74 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59 tting PENDING_BY
57c20 54 45 20 6c 6f 77 20 61 6e 64 20 72 75 6e 6e 69 TE low and runni
57c30 6e 67 20 74 68 65 20 65 6e 74 69 72 65 20 72 65 ng the entire re
57c40 67 72 65 73 73 69 6f 6e 20 73 75 69 74 65 2e 0a gression suite..
57c50 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 **.** Changing t
57c60 68 65 20 76 61 6c 75 65 20 6f 66 20 50 45 4e 44 he value of PEND
57c70 49 4e 47 5f 42 59 54 45 20 72 65 73 75 6c 74 73 ING_BYTE results
57c80 20 69 6e 20 61 20 73 75 62 74 6c 79 20 69 6e 63 in a subtly inc
57c90 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 66 69 6c ompatible.** fil
57ca0 65 20 66 6f 72 6d 61 74 2e 20 20 44 65 70 65 6e e format. Depen
57cb0 64 69 6e 67 20 6f 6e 20 68 6f 77 20 69 74 20 69 ding on how it i
57cc0 73 20 63 68 61 6e 67 65 64 2c 20 79 6f 75 20 6d s changed, you m
57cd0 69 67 68 74 20 6e 6f 74 20 6e 6f 74 69 63 65 0a ight not notice.
57ce0 2a 2a 20 74 68 65 20 69 6e 63 6f 6d 70 61 74 69 ** the incompati
57cf0 62 69 6c 69 74 79 20 72 69 67 68 74 20 61 77 61 bility right awa
57d00 79 2c 20 65 76 65 6e 20 72 75 6e 6e 69 6e 67 20 y, even running
57d10 61 20 66 75 6c 6c 20 72 65 67 72 65 73 73 69 6f a full regressio
57d20 6e 20 74 65 73 74 2e 0a 2a 2a 20 54 68 65 20 64 n test..** The d
57d30 65 66 61 75 6c 74 20 6c 6f 63 61 74 69 6f 6e 20 efault location
57d40 6f 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 of PENDING_BYTE
57d50 69 73 20 74 68 65 20 66 69 72 73 74 20 62 79 74 is the first byt
57d60 65 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 47 e past the.** 1G
57d70 42 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a 2a B boundary..**.*
57d80 2f 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e /.#define PENDIN
57d90 47 5f 42 59 54 45 20 20 20 20 20 20 73 71 6c 69 G_BYTE sqli
57da0 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 0a 23 te3PendingByte.#
57db0 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 44 5f define RESERVED_
57dc0 42 59 54 45 20 20 20 20 20 28 50 45 4e 44 49 4e BYTE (PENDIN
57dd0 47 5f 42 59 54 45 2b 31 29 0a 23 64 65 66 69 6e G_BYTE+1).#defin
57de0 65 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 20 e SHARED_FIRST
57df0 20 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 54 (PENDING_BYT
57e00 45 2b 32 29 0a 23 64 65 66 69 6e 65 20 53 48 41 E+2).#define SHA
57e10 52 45 44 5f 53 49 5a 45 20 20 20 20 20 20 20 35 RED_SIZE 5
57e20 31 30 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 10../*.** Wrappe
57e30 72 20 61 72 6f 75 6e 64 20 4f 53 20 73 70 65 63 r around OS spec
57e40 69 66 69 63 20 73 71 6c 69 74 65 33 5f 6f 73 5f ific sqlite3_os_
57e50 69 6e 69 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e init() function.
57e60 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
57e70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
57e80 49 6e 69 74 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 Init(void);../*
57e90 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f .** Functions fo
57ea0 72 20 61 63 63 65 73 73 69 6e 67 20 73 71 6c 69 r accessing sqli
57eb0 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 te3_file methods
57ec0 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/.SQLITE_PRIV
57ed0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
57ee0 73 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 sClose(sqlite3_f
57ef0 69 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ile*);.SQLITE_PR
57f00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
57f10 33 4f 73 52 65 61 64 28 73 71 6c 69 74 65 33 5f 3OsRead(sqlite3_
57f20 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e file*, void*, in
57f30 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 t amt, i64 offse
57f40 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
57f50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
57f60 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69 Write(sqlite3_fi
57f70 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a le*, const void*
57f80 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f , int amt, i64 o
57f90 66 66 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50 ffset);.SQLITE_P
57fa0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
57fb0 65 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c e3OsTruncate(sql
57fc0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34 20 ite3_file*, i64
57fd0 73 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 size);.SQLITE_PR
57fe0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
57ff0 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 3OsSync(sqlite3_
58000 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c file*, int);.SQL
58010 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
58020 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
58030 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c e(sqlite3_file*,
58040 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 i64 *pSize);.SQ
58050 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
58060 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 sqlite3OsLock(s
58070 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
58080 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
58090 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
580a0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 Unlock(sqlite3_f
580b0 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ile*, int);.SQLI
580c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
580d0 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 qlite3OsCheckRes
580e0 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
580f0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
58100 2a 70 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49 54 *pResOut);.SQLIT
58110 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
58120 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 lite3OsFileContr
58130 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a ol(sqlite3_file*
58140 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 64 65 ,int,void*);.#de
58150 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 fine SQLITE_FCNT
58160 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 30 L_DB_UNCHANGED 0
58170 78 63 61 30 39 33 66 61 30 0a 53 51 4c 49 54 45 xca093fa0.SQLITE
58180 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
58190 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 ite3OsSectorSize
581a0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
581b0 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 d);.SQLITE_PRIVA
581c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
581d0 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
581e0 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 stics(sqlite3_fi
581f0 6c 65 20 2a 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a le *id);../* .**
58200 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 Functions for a
58210 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 ccessing sqlite3
58220 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a 2f _vfs methods .*/
58230 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
58240 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 int sqlite3OsOpe
58250 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c n(sqlite3_vfs *,
58260 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 const char *, s
58270 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
58280 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 t, int *);.SQLIT
58290 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
582a0 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 lite3OsDelete(sq
582b0 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e lite3_vfs *, con
582c0 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b st char *, int);
582d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
582e0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 int sqlite3OsAcc
582f0 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ess(sqlite3_vfs
58300 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c *, const char *,
58310 20 69 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73 4f int, int *pResO
58320 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ut);.SQLITE_PRIV
58330 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
58340 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 71 sFullPathname(sq
58350 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e lite3_vfs *, con
58360 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 st char *, int,
58370 63 68 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65 66 char *);.#ifndef
58380 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 SQLITE_OMIT_LOA
58390 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 D_EXTENSION.SQLI
583a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
583b0 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e *sqlite3OsDlOpen
583c0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 (sqlite3_vfs *,
583d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 const char *);.S
583e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
583f0 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 id sqlite3OsDlEr
58400 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ror(sqlite3_vfs
58410 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b *, int, char *);
58420 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
58430 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f 73 void (*sqlite3Os
58440 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 DlSym(sqlite3_vf
58450 73 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f 6e s *, void *, con
58460 73 74 20 63 68 61 72 20 2a 29 29 28 76 6f 69 64 st char *))(void
58470 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
58480 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 E void sqlite3Os
58490 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f DlClose(sqlite3_
584a0 76 66 73 20 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a vfs *, void *);.
584b0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
584c0 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e _OMIT_LOAD_EXTEN
584d0 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 SION */.SQLITE_P
584e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
584f0 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 e3OsRandomness(s
58500 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e qlite3_vfs *, in
58510 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 t, char *);.SQLI
58520 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
58530 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71 qlite3OsSleep(sq
58540 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 lite3_vfs *, int
58550 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
58560 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 E int sqlite3OsC
58570 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 urrentTime(sqlit
58580 65 33 5f 76 66 73 20 2a 2c 20 64 6f 75 62 6c 65 e3_vfs *, double
58590 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 *);../*.** Conve
585a0 6e 69 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e 73 nience functions
585b0 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 6e 64 for opening and
585c0 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 75 closing files u
585d0 73 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33 sing .** sqlite3
585e0 5f 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 6f 62 74 _malloc() to obt
585f0 61 69 6e 20 73 70 61 63 65 20 66 6f 72 20 74 68 ain space for th
58600 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 73 74 e file-handle st
58610 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 ructure..*/.SQLI
58620 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
58630 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c qlite3OsOpenMall
58640 6f 63 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a oc(sqlite3_vfs *
58650 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 , const char *,
58660 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a 2c sqlite3_file **,
58670 20 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 int,int*);.SQLI
58680 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
58690 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 qlite3OsCloseFre
586a0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
586b0 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 );..#endif /* _S
586c0 51 4c 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f 0a 0a QLITE_OS_H_ */..
586d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
586e0 45 6e 64 20 6f 66 20 6f 73 2e 68 20 2a 2a 2a 2a End of os.h ****
586f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
58720 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
58730 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
58740 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
58750 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
58760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
58770 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
58780 49 6e 63 6c 75 64 65 20 6d 75 74 65 78 2e 68 20 Include mutex.h
58790 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
587a0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
587b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
587c0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
587d0 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 Begin file mutex
587e0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
587f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
58810 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 /*.** 2007 Augus
58820 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 t 28.**.** The a
58830 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
58840 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
58850 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
58860 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
58870 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
58880 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
58890 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
588a0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
588b0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
588c0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
588d0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
588e0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
588f0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
58900 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
58910 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
58920 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
58930 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
58940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58980 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
58990 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ile contains the
589a0 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20 66 common header f
589b0 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 or all mutex imp
589c0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a lementations..**
589d0 20 54 68 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 The sqliteInt.h
589e0 20 68 65 61 64 65 72 20 23 69 6e 63 6c 75 64 65 header #include
589f0 73 20 74 68 69 73 20 66 69 6c 65 20 73 6f 20 74 s this file so t
58a00 68 61 74 20 69 74 20 69 73 20 61 76 61 69 6c 61 hat it is availa
58a10 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 73 6f ble.** to all so
58a20 75 72 63 65 20 66 69 6c 65 73 2e 20 20 57 65 20 urce files. We
58a30 62 72 65 61 6b 20 69 74 20 6f 75 74 20 69 6e 20 break it out in
58a40 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65 an effort to kee
58a50 70 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65 p the code.** be
58a60 74 74 65 72 20 6f 72 67 61 6e 69 7a 65 64 2e 0a tter organized..
58a70 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 73 6f 75 **.** NOTE: sou
58a80 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 rce files should
58a90 20 2a 6e 6f 74 2a 20 23 69 6e 63 6c 75 64 65 20 *not* #include
58aa0 74 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 this header file
58ab0 20 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 20 53 6f directly..** So
58ac0 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c urce files shoul
58ad0 64 20 23 69 6e 63 6c 75 64 65 20 74 68 65 20 73 d #include the s
58ae0 71 6c 69 74 65 49 6e 74 2e 68 20 66 69 6c 65 20 qliteInt.h file
58af0 61 6e 64 20 6c 65 74 20 74 68 61 74 20 66 69 6c and let that fil
58b00 65 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 74 68 69 e.** include thi
58b10 73 20 6f 6e 65 20 69 6e 64 69 72 65 63 74 6c 79 s one indirectly
58b20 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 ..**.** $Id: mut
58b30 65 78 2e 68 2c 76 20 31 2e 39 20 32 30 30 38 2f ex.h,v 1.9 2008/
58b40 31 30 2f 30 37 20 31 35 3a 32 35 3a 34 38 20 64 10/07 15:25:48 d
58b50 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a rh Exp $.*/.../*
58b60 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77 .** Figure out w
58b70 68 61 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 hat version of t
58b80 68 65 20 63 6f 64 65 20 74 6f 20 75 73 65 2e 20 he code to use.
58b90 20 54 68 65 20 63 68 6f 69 63 65 73 20 61 72 65 The choices are
58ba0 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f .**.** SQLITE_
58bb0 4d 55 54 45 58 5f 4f 4d 49 54 20 20 20 20 20 20 MUTEX_OMIT
58bc0 20 20 20 4e 6f 20 6d 75 74 65 78 20 6c 6f 67 69 No mutex logi
58bd0 63 2e 20 20 4e 6f 74 20 65 76 65 6e 20 73 74 75 c. Not even stu
58be0 62 73 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20 20 bs. The.**
58bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58c00 20 20 20 20 20 20 20 20 6d 75 74 65 78 65 73 20 mutexes
58c10 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 63 61 6e implemention can
58c20 6e 6f 74 20 62 65 20 6f 76 65 72 72 69 64 64 65 not be overridde
58c30 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 n.**
58c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58c50 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a at start-time..
58c60 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d **.** SQLITE_M
58c70 55 54 45 58 5f 4e 4f 4f 50 20 20 20 20 20 20 20 UTEX_NOOP
58c80 20 20 46 6f 72 20 73 69 6e 67 6c 65 2d 74 68 72 For single-thr
58c90 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f eaded applicatio
58ca0 6e 73 2e 20 20 4e 6f 0a 2a 2a 20 20 20 20 20 20 ns. No.**
58cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58cc0 20 20 20 20 20 20 20 6d 75 74 75 61 6c 20 65 78 mutual ex
58cd0 63 6c 75 73 69 6f 6e 20 69 73 20 70 72 6f 76 69 clusion is provi
58ce0 64 65 64 2e 20 20 42 75 74 20 74 68 69 73 0a 2a ded. But this.*
58cf0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
58d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6d im
58d10 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e plementation can
58d20 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 61 be overridden a
58d30 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
58d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58d50 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a start-time..**.
58d60 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 ** SQLITE_MUTE
58d70 58 5f 50 54 48 52 45 41 44 53 20 20 20 20 20 46 X_PTHREADS F
58d80 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 or multi-threade
58d90 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f d applications o
58da0 6e 20 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 n Unix..**.**
58db0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 SQLITE_MUTEX_W32
58dc0 20 20 20 20 20 20 20 20 20 20 46 6f 72 20 6d 75 For mu
58dd0 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 70 lti-threaded app
58de0 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 57 69 6e lications on Win
58df0 33 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 32..**.** SQLI
58e00 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 20 20 20 TE_MUTEX_OS2
58e10 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d For multi-
58e20 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 threaded applica
58e30 74 69 6f 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a tions on OS/2..*
58e40 2f 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 54 48 /.#if !SQLITE_TH
58e50 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e READSAFE.# defin
58e60 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f e SQLITE_MUTEX_O
58e70 4d 49 54 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 MIT.#endif.#if S
58e80 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
58e90 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c && !defined(SQL
58ea0 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a ITE_MUTEX_NOOP).
58eb0 23 20 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f # if SQLITE_OS_
58ec0 55 4e 49 58 0a 23 20 20 20 20 64 65 66 69 6e 65 UNIX.# define
58ed0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 SQLITE_MUTEX_PT
58ee0 48 52 45 41 44 53 0a 23 20 20 65 6c 69 66 20 53 HREADS.# elif S
58ef0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 QLITE_OS_WIN.#
58f00 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
58f10 4d 55 54 45 58 5f 57 33 32 0a 23 20 20 65 6c 69 MUTEX_W32.# eli
58f20 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a f SQLITE_OS_OS2.
58f30 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 # define SQLI
58f40 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 23 20 20 TE_MUTEX_OS2.#
58f50 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65 else.# define
58f60 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f SQLITE_MUTEX_NO
58f70 4f 50 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 OP.# endif.#end
58f80 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
58f90 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a E_MUTEX_OMIT./*.
58fa0 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 ** If this is a
58fb0 6e 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 61 no-op implementa
58fc0 74 69 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 tion, implement
58fd0 65 76 65 72 79 74 68 69 6e 67 20 61 73 20 6d 61 everything as ma
58fe0 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 cros..*/.#define
58ff0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
59000 6c 6c 6f 63 28 58 29 20 20 20 20 28 28 73 71 6c lloc(X) ((sql
59010 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23 ite3_mutex*)8).#
59020 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d define sqlite3_m
59030 75 74 65 78 5f 66 72 65 65 28 58 29 0a 23 64 65 utex_free(X).#de
59040 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 fine sqlite3_mut
59050 65 78 5f 65 6e 74 65 72 28 58 29 0a 23 64 65 66 ex_enter(X).#def
59060 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ine sqlite3_mute
59070 78 5f 74 72 79 28 58 29 20 20 20 20 20 20 53 51 x_try(X) SQ
59080 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 LITE_OK.#define
59090 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
590a0 61 76 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73 ave(X).#define s
590b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
590c0 64 28 58 29 20 20 20 20 20 31 0a 23 64 65 66 69 d(X) 1.#defi
590d0 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ne sqlite3_mutex
590e0 5f 6e 6f 74 68 65 6c 64 28 58 29 20 20 31 0a 23 _notheld(X) 1.#
590f0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 define sqlite3Mu
59100 74 65 78 41 6c 6c 6f 63 28 58 29 20 20 20 20 20 texAlloc(X)
59110 20 28 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ((sqlite3_mutex
59120 2a 29 38 29 0a 23 64 65 66 69 6e 65 20 73 71 6c *)8).#define sql
59130 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 20 ite3MutexInit()
59140 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b SQLITE_OK
59150 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 .#define sqlite3
59160 4d 75 74 65 78 45 6e 64 28 29 0a 23 65 6e 64 69 MutexEnd().#endi
59170 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c f /* defined(SQL
59180 49 54 45 5f 4f 4d 49 54 5f 4d 55 54 45 58 29 20 ITE_OMIT_MUTEX)
59190 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
591a0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 *** End of mutex
591b0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
591c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
591d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
591e0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
591f0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
59200 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
59210 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 in sqliteInt.h
59220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59230 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 **/.../*.** Each
59240 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
59250 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79 o be accessed by
59260 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 61 the system is a
59270 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 n instance.** of
59280 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
59290 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 65 tructure. There
592a0 20 61 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 are normally tw
592b0 6f 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 o of these struc
592c0 74 75 72 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 tures.** in the
592d0 73 71 6c 69 74 65 2e 61 44 62 5b 5d 20 61 72 72 sqlite.aDb[] arr
592e0 61 79 2e 20 20 61 44 62 5b 30 5d 20 69 73 20 74 ay. aDb[0] is t
592f0 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
59300 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 61 44 62 file and.** aDb
59310 5b 31 5d 20 69 73 20 74 68 65 20 64 61 74 61 62 [1] is the datab
59320 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f ase file used to
59330 20 68 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 hold temporary
59340 74 61 62 6c 65 73 2e 20 20 41 64 64 69 74 69 6f tables. Additio
59350 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 nal.** databases
59360 20 6d 61 79 20 62 65 20 61 74 74 61 63 68 65 64 may be attached
59370 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 44 62 20 7b ..*/.struct Db {
59380 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 . char *zName;
59390 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
593a0 6f 66 20 74 68 69 73 20 64 61 74 61 62 61 73 65 of this database
593b0 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 */. Btree *pBt
593c0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ; /* Th
593d0 65 20 42 2a 54 72 65 65 20 73 74 72 75 63 74 75 e B*Tree structu
593e0 72 65 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 re for this data
593f0 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 base file */. u
59400 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 8 inTrans;
59410 20 20 20 20 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 /* 0: not wr
59420 69 74 61 62 6c 65 2e 20 20 31 3a 20 54 72 61 6e itable. 1: Tran
59430 73 61 63 74 69 6f 6e 2e 20 20 32 3a 20 43 68 65 saction. 2: Che
59440 63 6b 70 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 20 ckpoint */. u8
59450 73 61 66 65 74 79 5f 6c 65 76 65 6c 3b 20 20 20 safety_level;
59460 20 20 2f 2a 20 48 6f 77 20 61 67 67 72 65 73 73 /* How aggress
59470 69 76 65 20 61 74 20 73 79 6e 63 69 6e 67 20 64 ive at syncing d
59480 61 74 61 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 ata to disk */.
59490 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 Schema *pSchema
594a0 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 ; /* Pointer
594b0 20 74 6f 20 64 61 74 61 62 61 73 65 20 73 63 68 to database sch
594c0 65 6d 61 20 28 70 6f 73 73 69 62 6c 79 20 73 68 ema (possibly sh
594d0 61 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ared) */.};../*.
594e0 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
594f0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
59500 73 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 73 structure stores
59510 20 61 20 64 61 74 61 62 61 73 65 20 73 63 68 65 a database sche
59520 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ma..**.** If the
59530 72 65 20 61 72 65 20 6e 6f 20 76 69 72 74 75 61 re are no virtua
59540 6c 20 74 61 62 6c 65 73 20 63 6f 6e 66 69 67 75 l tables configu
59550 72 65 64 20 69 6e 20 74 68 69 73 20 73 63 68 65 red in this sche
59560 6d 61 2c 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d ma, the.** Schem
59570 61 2e 64 62 20 76 61 72 69 61 62 6c 65 20 69 73 a.db variable is
59580 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 set to NULL. Af
59590 74 65 72 20 74 68 65 20 66 69 72 73 74 20 76 69 ter the first vi
595a0 72 74 75 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 rtual table.** h
595b0 61 73 20 62 65 65 6e 20 61 64 64 65 64 2c 20 69 as been added, i
595c0 74 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e t is set to poin
595d0 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 t to the databas
595e0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a e connection .**
595f0 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 used to create
59600 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 the connection.
59610 4f 6e 63 65 20 61 20 76 69 72 74 75 61 6c 20 74 Once a virtual t
59620 61 62 6c 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a able has been.**
59630 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 53 63 added to the Sc
59640 68 65 6d 61 20 73 74 72 75 63 74 75 72 65 20 61 hema structure a
59650 6e 64 20 74 68 65 20 53 63 68 65 6d 61 2e 64 62 nd the Schema.db
59660 20 76 61 72 69 61 62 6c 65 20 70 6f 70 75 6c 61 variable popula
59670 74 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 ted, .** only th
59680 61 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e at database conn
59690 65 63 74 69 6f 6e 20 6d 61 79 20 75 73 65 20 74 ection may use t
596a0 68 65 20 53 63 68 65 6d 61 20 74 6f 20 70 72 65 he Schema to pre
596b0 70 61 72 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 pare .** stateme
596c0 6e 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 nts..*/.struct S
596d0 63 68 65 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63 chema {. int sc
596e0 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f hema_cookie; /
596f0 2a 20 44 61 74 61 62 61 73 65 20 73 63 68 65 6d * Database schem
59700 61 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 a version number
59710 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a for this file *
59720 2f 0a 20 20 48 61 73 68 20 74 62 6c 48 61 73 68 /. Hash tblHash
59730 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 ; /* All
59740 74 61 62 6c 65 73 20 69 6e 64 65 78 65 64 20 62 tables indexed b
59750 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 y name */. Hash
59760 20 69 64 78 48 61 73 68 3b 20 20 20 20 20 20 20 idxHash;
59770 20 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20 /* All (named)
59780 69 6e 64 69 63 65 73 20 69 6e 64 65 78 65 64 20 indices indexed
59790 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 by name */. Has
597a0 68 20 74 72 69 67 48 61 73 68 3b 20 20 20 20 20 h trigHash;
597b0 20 20 2f 2a 20 41 6c 6c 20 74 72 69 67 67 65 72 /* All trigger
597c0 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d s indexed by nam
597d0 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 53 e */. Table *pS
597e0 65 71 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 54 eqTab; /* T
597f0 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e he sqlite_sequen
59800 63 65 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 ce table used by
59810 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a AUTOINCREMENT *
59820 2f 0a 20 20 75 38 20 66 69 6c 65 5f 66 6f 72 6d /. u8 file_form
59830 61 74 3b 20 20 20 20 20 20 2f 2a 20 53 63 68 65 at; /* Sche
59840 6d 61 20 66 6f 72 6d 61 74 20 76 65 72 73 69 6f ma format versio
59850 6e 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 n for this file
59860 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 */. u8 enc;
59870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 /* Tex
59880 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 t encoding used
59890 62 79 20 74 68 69 73 20 64 61 74 61 62 61 73 65 by this database
598a0 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b */. u16 flags;
598b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c /* Fl
598c0 61 67 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ags associated w
598d0 69 74 68 20 74 68 69 73 20 73 63 68 65 6d 61 20 ith this schema
598e0 2a 2f 0a 20 20 69 6e 74 20 63 61 63 68 65 5f 73 */. int cache_s
598f0 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d ize; /* Num
59900 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 ber of pages to
59910 75 73 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 use in the cache
59920 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
59930 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
59940 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 20 2a ABLE. sqlite3 *
59950 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 22 db; /* "
59960 4f 77 6e 65 72 22 20 63 6f 6e 6e 65 63 74 69 6f Owner" connectio
59970 6e 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20 61 n. See comment a
59980 62 6f 76 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d bove */.#endif.}
59990 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d ;../*.** These m
599a0 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 acros can be use
599b0 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c 20 d to test, set,
599c0 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 6e or clear bits in
599d0 20 74 68 65 20 0a 2a 2a 20 44 62 2e 66 6c 61 67 the .** Db.flag
599e0 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 s field..*/.#def
599f0 69 6e 65 20 44 62 48 61 73 50 72 6f 70 65 72 74 ine DbHasPropert
59a00 79 28 44 2c 49 2c 50 29 20 20 20 20 20 28 28 28 y(D,I,P) (((
59a10 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 D)->aDb[I].pSche
59a20 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 3d 3d ma->flags&(P))==
59a30 28 50 29 29 0a 23 64 65 66 69 6e 65 20 44 62 48 (P)).#define DbH
59a40 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 44 2c asAnyProperty(D,
59a50 49 2c 50 29 20 20 28 28 28 44 29 2d 3e 61 44 62 I,P) (((D)->aDb
59a60 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 [I].pSchema->fla
59a70 67 73 26 28 50 29 29 21 3d 30 29 0a 23 64 65 66 gs&(P))!=0).#def
59a80 69 6e 65 20 44 62 53 65 74 50 72 6f 70 65 72 74 ine DbSetPropert
59a90 79 28 44 2c 49 2c 50 29 20 20 20 20 20 28 44 29 y(D,I,P) (D)
59aa0 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 ->aDb[I].pSchema
59ab0 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a 23 64 65 ->flags|=(P).#de
59ac0 66 69 6e 65 20 44 62 43 6c 65 61 72 50 72 6f 70 fine DbClearProp
59ad0 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 28 44 erty(D,I,P) (D
59ae0 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d )->aDb[I].pSchem
59af0 61 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 29 0a 0a a->flags&=~(P)..
59b00 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 /*.** Allowed va
59b10 6c 75 65 73 20 66 6f 72 20 74 68 65 20 44 42 2e lues for the DB.
59b20 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2a 0a flags field..**.
59b30 2a 2a 20 54 68 65 20 44 42 5f 53 63 68 65 6d 61 ** The DB_Schema
59b40 4c 6f 61 64 65 64 20 66 6c 61 67 20 69 73 20 73 Loaded flag is s
59b50 65 74 20 61 66 74 65 72 20 74 68 65 20 64 61 74 et after the dat
59b60 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 abase schema has
59b70 20 62 65 65 6e 0a 2a 2a 20 72 65 61 64 20 69 6e been.** read in
59b80 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 to internal hash
59b90 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 44 tables..**.** D
59ba0 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20 6d B_UnresetViews m
59bb0 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f 72 eans that one or
59bc0 20 6d 6f 72 65 20 76 69 65 77 73 20 68 61 76 65 more views have
59bd0 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 68 column names th
59be0 61 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 at.** have been
59bf0 66 69 6c 6c 65 64 20 6f 75 74 2e 20 20 49 66 20 filled out. If
59c00 74 68 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 the schema chang
59c10 65 73 2c 20 74 68 65 73 65 20 63 6f 6c 75 6d 6e es, these column
59c20 20 6e 61 6d 65 73 20 6d 69 67 68 74 0a 2a 2a 20 names might.**
59c30 63 68 61 6e 67 65 73 20 61 6e 64 20 73 6f 20 74 changes and so t
59c40 68 65 20 76 69 65 77 20 77 69 6c 6c 20 6e 65 65 he view will nee
59c50 64 20 74 6f 20 62 65 20 72 65 73 65 74 2e 0a 2a d to be reset..*
59c60 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 53 63 68 /.#define DB_Sch
59c70 65 6d 61 4c 6f 61 64 65 64 20 20 20 20 30 78 30 emaLoaded 0x0
59c80 30 30 31 20 20 2f 2a 20 54 68 65 20 73 63 68 65 001 /* The sche
59c90 6d 61 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 64 ma has been load
59ca0 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 ed */.#define DB
59cb0 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20 20 20 _UnresetViews
59cc0 20 30 78 30 30 30 32 20 20 2f 2a 20 53 6f 6d 65 0x0002 /* Some
59cd0 20 76 69 65 77 73 20 68 61 76 65 20 64 65 66 69 views have defi
59ce0 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 ned column names
59cf0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 45 */.#define DB_E
59d00 6d 70 74 79 20 20 20 20 20 20 20 20 20 20 20 30 mpty 0
59d10 78 30 30 30 34 20 20 2f 2a 20 54 68 65 20 66 69 x0004 /* The fi
59d20 6c 65 20 69 73 20 65 6d 70 74 79 20 28 6c 65 6e le is empty (len
59d30 67 74 68 20 30 20 62 79 74 65 73 29 20 2a 2f 0a gth 0 bytes) */.
59d40 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 ./*.** The numbe
59d50 72 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 6b r of different k
59d60 69 6e 64 73 20 6f 66 20 74 68 69 6e 67 73 20 74 inds of things t
59d70 68 61 74 20 63 61 6e 20 62 65 20 6c 69 6d 69 74 hat can be limit
59d80 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 ed.** using the
59d90 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 20 sqlite3_limit()
59da0 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 interface..*/.#d
59db0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 4c efine SQLITE_N_L
59dc0 49 4d 49 54 20 28 53 51 4c 49 54 45 5f 4c 49 4d IMIT (SQLITE_LIM
59dd0 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 IT_TRIGGER_DEPTH
59de0 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b 61 +1)../*.** Looka
59df0 73 69 64 65 20 6d 61 6c 6c 6f 63 20 69 73 20 61 side malloc is a
59e00 20 73 65 74 20 6f 66 20 66 69 78 65 64 2d 73 69 set of fixed-si
59e10 7a 65 20 62 75 66 66 65 72 73 20 74 68 61 74 20 ze buffers that
59e20 63 61 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 can be used.** t
59e30 6f 20 73 61 74 69 73 66 79 20 73 6d 61 6c 6c 20 o satisfy small
59e40 74 72 61 6e 73 69 65 6e 74 20 6d 65 6d 6f 72 79 transient memory
59e50 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 allocation requ
59e60 65 73 74 73 20 66 6f 72 20 6f 62 6a 65 63 74 73 ests for objects
59e70 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 .** associated w
59e80 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 ith a particular
59e90 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
59ea0 74 69 6f 6e 2e 20 20 54 68 65 20 75 73 65 20 6f tion. The use o
59eb0 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 6d f.** lookaside m
59ec0 61 6c 6c 6f 63 20 70 72 6f 76 69 64 65 73 20 61 alloc provides a
59ed0 20 73 69 67 6e 69 66 69 63 61 6e 74 20 70 65 72 significant per
59ee0 66 6f 72 6d 61 6e 63 65 20 65 6e 68 61 6e 63 65 formance enhance
59ef0 6d 65 6e 74 0a 2a 2a 20 28 61 70 70 72 6f 78 20 ment.** (approx
59f00 31 30 25 29 20 62 79 20 61 76 6f 69 64 69 6e 67 10%) by avoiding
59f10 20 6e 75 6d 65 72 6f 75 73 20 6d 61 6c 6c 6f 63 numerous malloc
59f20 2f 66 72 65 65 20 72 65 71 75 65 73 74 73 20 77 /free requests w
59f30 68 69 6c 65 20 70 61 72 73 69 6e 67 0a 2a 2a 20 hile parsing.**
59f40 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a SQL statements..
59f50 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f 6f 6b 61 73 **.** The Lookas
59f60 69 64 65 20 73 74 72 75 63 74 75 72 65 20 68 6f ide structure ho
59f70 6c 64 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f lds configuratio
59f80 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 n information ab
59f90 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 6f 6f 6b 61 out the.** looka
59fa0 73 69 64 65 20 6d 61 6c 6c 6f 63 20 73 75 62 73 side malloc subs
59fb0 79 73 74 65 6d 2e 20 20 45 61 63 68 20 61 76 61 ystem. Each ava
59fc0 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 61 6c ilable memory al
59fd0 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a 2a 2a 20 74 location in.** t
59fe0 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 73 75 62 he lookaside sub
59ff0 73 79 73 74 65 6d 20 69 73 20 73 74 6f 72 65 64 system is stored
5a000 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 on a linked lis
5a010 74 20 6f 66 20 4c 6f 6f 6b 61 73 69 64 65 53 6c t of LookasideSl
5a020 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 0a 2a ot.** objects..*
5a030 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 61 *.** Lookaside a
5a040 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 6f llocations are o
5a050 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20 nly allowed for
5a060 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 objects that are
5a070 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 associated.** w
5a080 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 ith a particular
5a090 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
5a0a0 74 69 6f 6e 2e 20 20 48 65 6e 63 65 2c 20 73 63 tion. Hence, sc
5a0b0 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e hema information
5a0c0 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 73 74 cannot.** be st
5a0d0 6f 72 65 64 20 69 6e 20 6c 6f 6f 6b 61 73 69 64 ored in lookasid
5a0e0 65 20 62 65 63 61 75 73 65 20 69 6e 20 73 68 61 e because in sha
5a0f0 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 20 74 red cache mode t
5a100 68 65 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d he schema inform
5a110 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 68 61 72 ation.** is shar
5a120 65 64 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 64 ed by multiple d
5a130 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
5a140 6f 6e 73 2e 20 20 54 68 65 72 65 66 6f 72 65 2c ons. Therefore,
5a150 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a 2a while parsing.*
5a160 2a 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 * schema informa
5a170 74 69 6f 6e 2c 20 74 68 65 20 4c 6f 6f 6b 61 73 tion, the Lookas
5a180 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 66 6c 61 ide.bEnabled fla
5a190 67 20 69 73 20 63 6c 65 61 72 65 64 20 73 6f 20 g is cleared so
5a1a0 74 68 61 74 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 that.** lookasid
5a1b0 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 e allocations ar
5a1c0 65 20 6e 6f 74 20 75 73 65 64 20 74 6f 20 63 6f e not used to co
5a1d0 6e 73 74 72 75 63 74 20 74 68 65 20 73 63 68 65 nstruct the sche
5a1e0 6d 61 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a 73 ma objects..*/.s
5a1f0 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 20 truct Lookaside
5a200 7b 0a 20 20 75 31 36 20 73 7a 3b 20 20 20 20 20 {. u16 sz;
5a210 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
5a220 69 7a 65 20 6f 66 20 65 61 63 68 20 62 75 66 66 ize of each buff
5a230 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 er in bytes */.
5a240 20 75 38 20 62 45 6e 61 62 6c 65 64 3b 20 20 20 u8 bEnabled;
5a250 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 73 /* Fals
5a260 65 20 74 6f 20 64 69 73 61 62 6c 65 20 6e 65 77 e to disable new
5a270 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63 lookaside alloc
5a280 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75 38 20 62 ations */. u8 b
5a290 4d 61 6c 6c 6f 63 65 64 3b 20 20 20 20 20 20 20 Malloced;
5a2a0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 /* True if p
5a2b0 53 74 61 72 74 20 6f 62 74 61 69 6e 65 64 20 66 Start obtained f
5a2c0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c rom sqlite3_mall
5a2d0 6f 63 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f oc() */. int nO
5a2e0 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ut;
5a2f0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
5a300 75 66 66 65 72 73 20 63 75 72 72 65 6e 74 6c 79 uffers currently
5a310 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a checked out */.
5a320 20 20 69 6e 74 20 6d 78 4f 75 74 3b 20 20 20 20 int mxOut;
5a330 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 69 67 /* Hig
5a340 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20 hwater mark for
5a350 6e 4f 75 74 20 2a 2f 0a 20 20 4c 6f 6f 6b 61 73 nOut */. Lookas
5a360 69 64 65 53 6c 6f 74 20 2a 70 46 72 65 65 3b 20 ideSlot *pFree;
5a370 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 76 61 /* List of ava
5a380 69 6c 61 62 6c 65 20 62 75 66 66 65 72 73 20 2a ilable buffers *
5a390 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 61 72 74 /. void *pStart
5a3a0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 ; /* F
5a3b0 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 76 61 irst byte of ava
5a3c0 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 70 ilable memory sp
5a3d0 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ace */. void *p
5a3e0 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 End;
5a3f0 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 70 /* First byte p
5a400 61 73 74 20 65 6e 64 20 6f 66 20 61 76 61 69 6c ast end of avail
5a410 61 62 6c 65 20 73 70 61 63 65 20 2a 2f 0a 7d 3b able space */.};
5a420 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 .struct Lookasid
5a430 65 53 6c 6f 74 20 7b 0a 20 20 4c 6f 6f 6b 61 73 eSlot {. Lookas
5a440 69 64 65 53 6c 6f 74 20 2a 70 4e 65 78 74 3b 20 ideSlot *pNext;
5a450 20 20 20 2f 2a 20 4e 65 78 74 20 62 75 66 66 65 /* Next buffe
5a460 72 20 69 6e 20 74 68 65 20 6c 69 73 74 20 6f 66 r in the list of
5a470 20 66 72 65 65 20 62 75 66 66 65 72 73 20 2a 2f free buffers */
5a480 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 68 61 73 .};../*.** A has
5a490 68 20 74 61 62 6c 65 20 66 6f 72 20 66 75 6e 63 h table for func
5a4a0 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73 tion definitions
5a4b0 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 20 65 61 63 ..**.** Hash eac
5a4c0 68 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 h FuncDef struct
5a4d0 75 72 65 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 20 ure into one of
5a4e0 74 68 65 20 46 75 6e 63 44 65 66 48 61 73 68 2e the FuncDefHash.
5a4f0 61 5b 5d 20 73 6c 6f 74 73 2e 0a 2a 2a 20 43 6f a[] slots..** Co
5a500 6c 6c 69 73 69 6f 6e 73 20 61 72 65 20 6f 6e 20 llisions are on
5a510 74 68 65 20 46 75 6e 63 44 65 66 2e 70 48 61 73 the FuncDef.pHas
5a520 68 20 63 68 61 69 6e 2e 0a 2a 2f 0a 73 74 72 75 h chain..*/.stru
5a530 63 74 20 46 75 6e 63 44 65 66 48 61 73 68 20 7b ct FuncDefHash {
5a540 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 5b 32 33 . FuncDef *a[23
5a550 5d 3b 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 ]; /* Hash
5a560 20 74 61 62 6c 65 20 66 6f 72 20 66 75 6e 63 74 table for funct
5a570 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a ions */.};../*.*
5a580 2a 20 45 61 63 68 20 64 61 74 61 62 61 73 65 20 * Each database
5a590 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
5a5a0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
5a5b0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a structure..**.**
5a5c0 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 61 73 74 The sqlite.last
5a5d0 52 6f 77 69 64 20 72 65 63 6f 72 64 73 20 74 68 Rowid records th
5a5e0 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f e last insert ro
5a5f0 77 69 64 20 67 65 6e 65 72 61 74 65 64 20 62 79 wid generated by
5a600 20 61 6e 0a 2a 2a 20 69 6e 73 65 72 74 20 73 74 an.** insert st
5a610 61 74 65 6d 65 6e 74 2e 20 20 49 6e 73 65 72 74 atement. Insert
5a620 73 20 6f 6e 20 76 69 65 77 73 20 64 6f 20 6e 6f s on views do no
5a630 74 20 61 66 66 65 63 74 20 69 74 73 20 76 61 6c t affect its val
5a640 75 65 2e 20 20 45 61 63 68 0a 2a 2a 20 74 72 69 ue. Each.** tri
5a650 67 67 65 72 20 68 61 73 20 69 74 73 20 6f 77 6e gger has its own
5a660 20 63 6f 6e 74 65 78 74 2c 20 73 6f 20 74 68 61 context, so tha
5a670 74 20 6c 61 73 74 52 6f 77 69 64 20 63 61 6e 20 t lastRowid can
5a680 62 65 20 75 70 64 61 74 65 64 20 69 6e 73 69 64 be updated insid
5a690 65 0a 2a 2a 20 74 72 69 67 67 65 72 73 20 61 73 e.** triggers as
5a6a0 20 75 73 75 61 6c 2e 20 20 54 68 65 20 70 72 65 usual. The pre
5a6b0 76 69 6f 75 73 20 76 61 6c 75 65 20 77 69 6c 6c vious value will
5a6c0 20 62 65 20 72 65 73 74 6f 72 65 64 20 6f 6e 63 be restored onc
5a6d0 65 20 74 68 65 20 74 72 69 67 67 65 72 0a 2a 2a e the trigger.**
5a6e0 20 65 78 69 74 73 2e 20 20 55 70 6f 6e 20 65 6e exits. Upon en
5a6f0 74 65 72 69 6e 67 20 61 20 62 65 66 6f 72 65 20 tering a before
5a700 6f 72 20 69 6e 73 74 65 61 64 20 6f 66 20 74 72 or instead of tr
5a710 69 67 67 65 72 2c 20 6c 61 73 74 52 6f 77 69 64 igger, lastRowid
5a720 20 69 73 20 6e 6f 0a 2a 2a 20 6c 6f 6e 67 65 72 is no.** longer
5a730 20 28 73 69 6e 63 65 20 61 66 74 65 72 20 76 65 (since after ve
5a740 72 73 69 6f 6e 20 32 2e 38 2e 31 32 29 20 72 65 rsion 2.8.12) re
5a750 73 65 74 20 74 6f 20 2d 31 2e 0a 2a 2a 0a 2a 2a set to -1..**.**
5a760 20 54 68 65 20 73 71 6c 69 74 65 2e 6e 43 68 61 The sqlite.nCha
5a770 6e 67 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 75 nge does not cou
5a780 6e 74 20 63 68 61 6e 67 65 73 20 77 69 74 68 69 nt changes withi
5a790 6e 20 74 72 69 67 67 65 72 73 20 61 6e 64 20 6b n triggers and k
5a7a0 65 65 70 73 20 6e 6f 0a 2a 2a 20 63 6f 6e 74 65 eeps no.** conte
5a7b0 78 74 2e 20 20 49 74 20 69 73 20 72 65 73 65 74 xt. It is reset
5a7c0 20 61 74 20 73 74 61 72 74 20 6f 66 20 73 71 6c at start of sql
5a7d0 69 74 65 33 5f 65 78 65 63 2e 0a 2a 2a 20 54 68 ite3_exec..** Th
5a7e0 65 20 73 71 6c 69 74 65 2e 6c 73 43 68 61 6e 67 e sqlite.lsChang
5a7f0 65 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 e represents the
5a800 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 number of chang
5a810 65 73 20 6d 61 64 65 20 62 79 20 74 68 65 20 6c es made by the l
5a820 61 73 74 0a 2a 2a 20 69 6e 73 65 72 74 2c 20 75 ast.** insert, u
5a830 70 64 61 74 65 2c 20 6f 72 20 64 65 6c 65 74 65 pdate, or delete
5a840 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 74 20 statement. It
5a850 72 65 6d 61 69 6e 73 20 63 6f 6e 73 74 61 6e 74 remains constant
5a860 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68 65 0a throughout the.
5a870 2a 2a 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 73 ** length of a s
5a880 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 69 73 20 tatement and is
5a890 74 68 65 6e 20 75 70 64 61 74 65 64 20 62 79 20 then updated by
5a8a0 4f 50 5f 53 65 74 43 6f 75 6e 74 73 2e 20 20 49 OP_SetCounts. I
5a8b0 74 20 6b 65 65 70 73 20 61 0a 2a 2a 20 63 6f 6e t keeps a.** con
5a8c0 74 65 78 74 20 73 74 61 63 6b 20 6a 75 73 74 20 text stack just
5a8d0 6c 69 6b 65 20 6c 61 73 74 52 6f 77 69 64 20 73 like lastRowid s
5a8e0 6f 20 74 68 61 74 20 74 68 65 20 63 6f 75 6e 74 o that the count
5a8f0 20 6f 66 20 63 68 61 6e 67 65 73 0a 2a 2a 20 77 of changes.** w
5a900 69 74 68 69 6e 20 61 20 74 72 69 67 67 65 72 20 ithin a trigger
5a910 69 73 20 6e 6f 74 20 73 65 65 6e 20 6f 75 74 73 is not seen outs
5a920 69 64 65 20 74 68 65 20 74 72 69 67 67 65 72 2e ide the trigger.
5a930 20 20 43 68 61 6e 67 65 73 20 74 6f 20 76 69 65 Changes to vie
5a940 77 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 61 66 66 ws do not.** aff
5a950 65 63 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 ect the value of
5a960 20 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a 20 54 68 lsChange..** Th
5a970 65 20 73 71 6c 69 74 65 2e 63 73 43 68 61 6e 67 e sqlite.csChang
5a980 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 e keeps track of
5a990 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
5a9a0 75 72 72 65 6e 74 20 63 68 61 6e 67 65 73 20 28 urrent changes (
5a9b0 73 69 6e 63 65 0a 2a 2a 20 74 68 65 20 6c 61 73 since.** the las
5a9c0 74 20 73 74 61 74 65 6d 65 6e 74 29 20 61 6e 64 t statement) and
5a9d0 20 69 73 20 75 73 65 64 20 74 6f 20 75 70 64 61 is used to upda
5a9e0 74 65 20 73 71 6c 69 74 65 5f 6c 73 43 68 61 6e te sqlite_lsChan
5a9f0 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 ge..**.** The me
5aa00 6d 62 65 72 20 76 61 72 69 61 62 6c 65 73 20 73 mber variables s
5aa10 71 6c 69 74 65 2e 65 72 72 43 6f 64 65 2c 20 73 qlite.errCode, s
5aa20 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67 20 61 6e qlite.zErrMsg an
5aa30 64 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67 d sqlite.zErrMsg
5aa40 31 36 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 16.** store the
5aa50 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f most recent erro
5aa60 72 20 63 6f 64 65 20 61 6e 64 2c 20 69 66 20 61 r code and, if a
5aa70 70 70 6c 69 63 61 62 6c 65 2c 20 73 74 72 69 6e pplicable, strin
5aa80 67 2e 20 54 68 65 0a 2a 2a 20 69 6e 74 65 72 6e g. The.** intern
5aa90 61 6c 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 al function sqli
5aaa0 74 65 33 45 72 72 6f 72 28 29 20 69 73 20 75 73 te3Error() is us
5aab0 65 64 20 74 6f 20 73 65 74 20 74 68 65 73 65 20 ed to set these
5aac0 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 63 6f 6e variables.** con
5aad0 73 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 73 74 sistently..*/.st
5aae0 72 75 63 74 20 73 71 6c 69 74 65 33 20 7b 0a 20 ruct sqlite3 {.
5aaf0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
5ab00 66 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f fs; /
5ab10 2a 20 4f 53 20 49 6e 74 65 72 66 61 63 65 20 2a * OS Interface *
5ab20 2f 0a 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 20 /. int nDb;
5ab30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ab40 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
5ab50 61 63 6b 65 6e 64 73 20 63 75 72 72 65 6e 74 6c ackends currentl
5ab60 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20 44 62 y in use */. Db
5ab70 20 2a 61 44 62 3b 20 20 20 20 20 20 20 20 20 20 *aDb;
5ab80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
5ab90 6c 6c 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 20 ll backends */.
5aba0 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 int flags;
5abb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5abc0 2a 20 4d 69 73 63 65 6c 6c 61 6e 65 6f 75 73 20 * Miscellaneous
5abd0 66 6c 61 67 73 2e 20 53 65 65 20 62 65 6c 6f 77 flags. See below
5abe0 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c */. int openFl
5abf0 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
5ac00 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 /* Flags pas
5ac10 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 sed to sqlite3_v
5ac20 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 fs.xOpen() */.
5ac30 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20 20 int errCode;
5ac40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5ac50 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 Most recent err
5ac60 6f 72 20 63 6f 64 65 20 28 53 51 4c 49 54 45 5f or code (SQLITE_
5ac70 2a 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 4d *) */. int errM
5ac80 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 ask;
5ac90 20 20 20 20 20 20 2f 2a 20 26 20 72 65 73 75 6c /* & resul
5aca0 74 20 63 6f 64 65 73 20 77 69 74 68 20 74 68 69 t codes with thi
5acb0 73 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 s before returni
5acc0 6e 67 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f 43 ng */. u8 autoC
5acd0 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20 ommit;
5ace0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 75 74 /* The aut
5acf0 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 2e 20 2a o-commit flag. *
5ad00 2f 0a 20 20 75 38 20 74 65 6d 70 5f 73 74 6f 72 /. u8 temp_stor
5ad10 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
5ad20 20 20 2f 2a 20 31 3a 20 66 69 6c 65 20 32 3a 20 /* 1: file 2:
5ad30 6d 65 6d 6f 72 79 20 30 3a 20 64 65 66 61 75 6c memory 0: defaul
5ad40 74 20 2a 2f 0a 20 20 75 38 20 6d 61 6c 6c 6f 63 t */. u8 malloc
5ad50 46 61 69 6c 65 64 3b 20 20 20 20 20 20 20 20 20 Failed;
5ad60 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
5ad70 77 65 20 68 61 76 65 20 73 65 65 6e 20 61 20 6d we have seen a m
5ad80 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 2a 2f alloc failure */
5ad90 0a 20 20 75 38 20 64 66 6c 74 4c 6f 63 6b 4d 6f . u8 dfltLockMo
5ada0 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 de;
5adb0 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 63 6b /* Default lock
5adc0 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 61 74 74 ing-mode for att
5add0 61 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 75 ached dbs */. u
5ade0 38 20 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 8 dfltJournalMod
5adf0 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e; /*
5ae00 44 65 66 61 75 6c 74 20 6a 6f 75 72 6e 61 6c 20 Default journal
5ae10 6d 6f 64 65 20 66 6f 72 20 61 74 74 61 63 68 65 mode for attache
5ae20 64 20 64 62 73 20 2a 2f 0a 20 20 73 69 67 6e 65 d dbs */. signe
5ae30 64 20 63 68 61 72 20 6e 65 78 74 41 75 74 6f 76 d char nextAutov
5ae40 61 63 3b 20 20 20 20 20 20 2f 2a 20 41 75 74 6f ac; /* Auto
5ae50 76 61 63 20 73 65 74 74 69 6e 67 20 61 66 74 65 vac setting afte
5ae60 72 20 56 41 43 55 55 4d 20 69 66 20 3e 3d 30 20 r VACUUM if >=0
5ae70 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 74 50 61 67 */. int nextPag
5ae80 65 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 esize;
5ae90 20 20 20 2f 2a 20 50 61 67 65 73 69 7a 65 20 61 /* Pagesize a
5aea0 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 20 3e fter VACUUM if >
5aeb0 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62 6c 0 */. int nTabl
5aec0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
5aed0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
5aee0 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 f tables in the
5aef0 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 43 6f database */. Co
5af00 6c 6c 53 65 71 20 2a 70 44 66 6c 74 43 6f 6c 6c llSeq *pDfltColl
5af10 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
5af20 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 he default colla
5af30 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 28 42 ting sequence (B
5af40 49 4e 41 52 59 29 20 2a 2f 0a 20 20 69 36 34 20 INARY) */. i64
5af50 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 20 lastRowid;
5af60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 4f 57 /* ROW
5af70 49 44 20 6f 66 20 6d 6f 73 74 20 72 65 63 65 6e ID of most recen
5af80 74 20 69 6e 73 65 72 74 20 28 73 65 65 20 61 62 t insert (see ab
5af90 6f 76 65 29 20 2a 2f 0a 20 20 75 33 32 20 6d 61 ove) */. u32 ma
5afa0 67 69 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 gic;
5afb0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 69 63 /* Magic
5afc0 20 6e 75 6d 62 65 72 20 66 6f 72 20 64 65 74 65 number for dete
5afd0 63 74 20 6c 69 62 72 61 72 79 20 6d 69 73 75 73 ct library misus
5afe0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e e */. int nChan
5aff0 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
5b000 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 /* Value re
5b010 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 turned by sqlite
5b020 33 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a 20 3_changes() */.
5b030 20 69 6e 74 20 6e 54 6f 74 61 6c 43 68 61 6e 67 int nTotalChang
5b040 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e; /
5b050 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 * Value returned
5b060 20 62 79 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 by sqlite3_tota
5b070 6c 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a 20 l_changes() */.
5b080 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
5b090 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 2f mutex; /
5b0a0 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 74 * Connection mut
5b0b0 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 4c 69 6d ex */. int aLim
5b0c0 69 74 5b 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49 it[SQLITE_N_LIMI
5b0d0 54 5d 3b 20 20 20 2f 2a 20 4c 69 6d 69 74 73 20 T]; /* Limits
5b0e0 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 */. struct sqli
5b0f0 74 65 33 49 6e 69 74 49 6e 66 6f 20 7b 20 20 20 te3InitInfo {
5b100 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f /* Informatio
5b110 6e 20 75 73 65 64 20 64 75 72 69 6e 67 20 69 6e n used during in
5b120 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a itialization */.
5b130 20 20 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 int iDb;
5b140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b150 2f 2a 20 57 68 65 6e 20 62 61 63 6b 20 69 73 20 /* When back is
5b160 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 being initialize
5b170 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 77 d */. int new
5b180 54 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 Tnum;
5b190 20 20 20 20 20 2f 2a 20 52 6f 6f 74 70 61 67 65 /* Rootpage
5b1a0 20 6f 66 20 74 61 62 6c 65 20 62 65 69 6e 67 20 of table being
5b1b0 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 initialized */.
5b1c0 20 20 20 75 38 20 62 75 73 79 3b 20 20 20 20 20 u8 busy;
5b1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5b1e0 2a 20 54 52 55 45 20 69 66 20 63 75 72 72 65 6e * TRUE if curren
5b1f0 74 6c 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 tly initializing
5b200 20 2a 2f 0a 20 20 20 20 75 38 20 6f 72 70 68 61 */. u8 orpha
5b210 6e 54 72 69 67 67 65 72 3b 20 20 20 20 20 20 20 nTrigger;
5b220 20 20 20 20 2f 2a 20 4c 61 73 74 20 73 74 61 74 /* Last stat
5b230 65 6d 65 6e 74 20 69 73 20 6f 72 70 68 61 6e 65 ement is orphane
5b240 64 20 54 45 4d 50 20 74 72 69 67 67 65 72 20 2a d TEMP trigger *
5b250 2f 0a 20 20 7d 20 69 6e 69 74 3b 0a 20 20 69 6e /. } init;. in
5b260 74 20 6e 45 78 74 65 6e 73 69 6f 6e 3b 20 20 20 t nExtension;
5b270 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
5b280 75 6d 62 65 72 20 6f 66 20 6c 6f 61 64 65 64 20 umber of loaded
5b290 65 78 74 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20 20 extensions */.
5b2a0 76 6f 69 64 20 2a 2a 61 45 78 74 65 6e 73 69 6f void **aExtensio
5b2b0 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a n; /*
5b2c0 20 41 72 72 61 79 20 6f 66 20 73 68 61 72 65 64 Array of shared
5b2d0 20 6c 69 62 72 61 72 79 20 68 61 6e 64 6c 65 73 library handles
5b2e0 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 56 64 62 */. struct Vdb
5b2f0 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 e *pVdbe;
5b300 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 /* List of a
5b310 63 74 69 76 65 20 76 69 72 74 75 61 6c 20 6d 61 ctive virtual ma
5b320 63 68 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20 chines */. int
5b330 61 63 74 69 76 65 56 64 62 65 43 6e 74 3b 20 20 activeVdbeCnt;
5b340 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
5b350 62 65 72 20 6f 66 20 56 44 42 45 73 20 63 75 72 ber of VDBEs cur
5b360 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 rently executing
5b370 20 2a 2f 0a 20 20 69 6e 74 20 77 72 69 74 65 56 */. int writeV
5b380 64 62 65 43 6e 74 3b 20 20 20 20 20 20 20 20 20 dbeCnt;
5b390 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
5b3a0 20 61 63 74 69 76 65 20 56 44 42 45 73 20 74 68 active VDBEs th
5b3b0 61 74 20 61 72 65 20 77 72 69 74 69 6e 67 20 2a at are writing *
5b3c0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 61 63 /. void (*xTrac
5b3d0 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 e)(void*,const c
5b3e0 68 61 72 2a 29 3b 20 20 20 20 20 20 20 20 2f 2a har*); /*
5b3f0 20 54 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 Trace function
5b400 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 54 72 61 63 */. void *pTrac
5b410 65 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 eArg;
5b420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5b430 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 * Argument to th
5b440 65 20 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e e trace function
5b450 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 50 72 */. void (*xPr
5b460 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e ofile)(void*,con
5b470 73 74 20 63 68 61 72 2a 2c 75 36 34 29 3b 20 20 st char*,u64);
5b480 2f 2a 20 50 72 6f 66 69 6c 69 6e 67 20 66 75 6e /* Profiling fun
5b490 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 ction */. void
5b4a0 2a 70 50 72 6f 66 69 6c 65 41 72 67 3b 20 20 20 *pProfileArg;
5b4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b4c0 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
5b4d0 20 74 6f 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 to profile func
5b4e0 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a tion */. void *
5b4f0 70 43 6f 6d 6d 69 74 41 72 67 3b 20 20 20 20 20 pCommitArg;
5b500 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
5b510 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 6d 6d rgument to xComm
5b520 69 74 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 20 itCallback() */
5b530 20 20 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d . int (*xComm
5b540 69 74 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 itCallback)(void
5b550 2a 29 3b 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 *); /* Invoke
5b560 64 20 61 74 20 65 76 65 72 79 20 63 6f 6d 6d 69 d at every commi
5b570 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 52 t. */. void *pR
5b580 6f 6c 6c 62 61 63 6b 41 72 67 3b 20 20 20 20 20 ollbackArg;
5b590 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 /* Arg
5b5a0 75 6d 65 6e 74 20 74 6f 20 78 52 6f 6c 6c 62 61 ument to xRollba
5b5b0 63 6b 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 20 ckCallback() */
5b5c0 20 20 0a 20 20 76 6f 69 64 20 28 2a 78 52 6f 6c . void (*xRol
5b5d0 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 29 28 76 lbackCallback)(v
5b5e0 6f 69 64 2a 29 3b 20 2f 2a 20 49 6e 76 6f 6b 65 oid*); /* Invoke
5b5f0 64 20 61 74 20 65 76 65 72 79 20 63 6f 6d 6d 69 d at every commi
5b600 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 t. */. void *pU
5b610 70 64 61 74 65 41 72 67 3b 0a 20 20 76 6f 69 64 pdateArg;. void
5b620 20 28 2a 78 55 70 64 61 74 65 43 61 6c 6c 62 61 (*xUpdateCallba
5b630 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 63 ck)(void*,int, c
5b640 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 onst char*,const
5b650 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 69 6e char*,sqlite_in
5b660 74 36 34 29 3b 0a 20 20 76 6f 69 64 28 2a 78 43 t64);. void(*xC
5b670 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f 69 64 2a ollNeeded)(void*
5b680 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 ,sqlite3*,int eT
5b690 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 extRep,const cha
5b6a0 72 2a 29 3b 0a 20 20 76 6f 69 64 28 2a 78 43 6f r*);. void(*xCo
5b6b0 6c 6c 4e 65 65 64 65 64 31 36 29 28 76 6f 69 64 llNeeded16)(void
5b6c0 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 *,sqlite3*,int e
5b6d0 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f TextRep,const vo
5b6e0 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 70 43 id*);. void *pC
5b6f0 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20 20 ollNeededArg;.
5b700 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
5b710 45 72 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Err; /*
5b720 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 Most recent err
5b730 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 or message */.
5b740 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 char *zErrMsg;
5b750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5b760 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 Most recent err
5b770 6f 72 20 6d 65 73 73 61 67 65 20 28 55 54 46 2d or message (UTF-
5b780 38 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a 20 20 8 encoded) */.
5b790 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 31 36 3b char *zErrMsg16;
5b7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5b7b0 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 Most recent err
5b7c0 6f 72 20 6d 65 73 73 61 67 65 20 28 55 54 46 2d or message (UTF-
5b7d0 31 36 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a 20 16 encoded) */.
5b7e0 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 76 6f 6c union {. vol
5b7f0 61 74 69 6c 65 20 69 6e 74 20 69 73 49 6e 74 65 atile int isInte
5b800 72 72 75 70 74 65 64 3b 20 2f 2a 20 54 72 75 65 rrupted; /* True
5b810 20 69 66 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 if sqlite3_inte
5b820 72 72 75 70 74 20 68 61 73 20 62 65 65 6e 20 63 rrupt has been c
5b830 61 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 64 6f 75 alled */. dou
5b840 62 6c 65 20 6e 6f 74 55 73 65 64 31 3b 20 20 20 ble notUsed1;
5b850 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 /* Spac
5b860 65 72 20 2a 2f 0a 20 20 7d 20 75 31 3b 0a 20 20 er */. } u1;.
5b870 4c 6f 6f 6b 61 73 69 64 65 20 6c 6f 6f 6b 61 73 Lookaside lookas
5b880 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ide; /*
5b890 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f Lookaside mallo
5b8a0 63 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 c configuration
5b8b0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
5b8c0 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 E_OMIT_AUTHORIZA
5b8d0 54 49 4f 4e 0a 20 20 69 6e 74 20 28 2a 78 41 75 TION. int (*xAu
5b8e0 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f th)(void*,int,co
5b8f0 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 nst char*,const
5b900 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 char*,const char
5b910 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a *,const char*);.
5b920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b940 2f 2a 20 41 63 63 65 73 73 20 61 75 74 68 6f 72 /* Access author
5b950 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e ization function
5b960 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 74 */. void *pAut
5b970 68 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 hArg;
5b980 20 20 20 20 2f 2a 20 31 73 74 20 61 72 67 75 6d /* 1st argum
5b990 65 6e 74 20 74 6f 20 74 68 65 20 61 63 63 65 73 ent to the acces
5b9a0 73 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 s auth function
5b9b0 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 */.#endif.#ifnde
5b9c0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 f SQLITE_OMIT_PR
5b9d0 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a OGRESS_CALLBACK.
5b9e0 20 20 69 6e 74 20 28 2a 78 50 72 6f 67 72 65 73 int (*xProgres
5b9f0 73 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 s)(void *);
5ba00 2f 2a 20 54 68 65 20 70 72 6f 67 72 65 73 73 20 /* The progress
5ba10 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f callback */. vo
5ba20 69 64 20 2a 70 50 72 6f 67 72 65 73 73 41 72 67 id *pProgressArg
5ba30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 ; /* A
5ba40 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 70 rgument to the p
5ba50 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b rogress callback
5ba60 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72 6f 67 72 */. int nProgr
5ba70 65 73 73 4f 70 73 3b 20 20 20 20 20 20 20 20 20 essOps;
5ba80 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
5ba90 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 70 72 6f opcodes for pro
5baa0 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a gress callback *
5bab0 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 /.#endif.#ifndef
5bac0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
5bad0 54 55 41 4c 54 41 42 4c 45 0a 20 20 48 61 73 68 TUALTABLE. Hash
5bae0 20 61 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 20 aModule;
5baf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 6f 70 /* pop
5bb00 75 6c 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 ulated by sqlite
5bb10 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 3_create_module(
5bb20 29 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 56 ) */. Table *pV
5bb30 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 Tab;
5bb40 20 20 20 20 20 2f 2a 20 76 74 61 62 20 77 69 74 /* vtab wit
5bb50 68 20 61 63 74 69 76 65 20 43 6f 6e 6e 65 63 74 h active Connect
5bb60 2f 43 72 65 61 74 65 20 6d 65 74 68 6f 64 20 2a /Create method *
5bb70 2f 0a 20 20 56 54 61 62 6c 65 20 2a 2a 61 56 54 /. VTable **aVT
5bb80 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 rans;
5bb90 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 /* Virtual tab
5bba0 6c 65 73 20 77 69 74 68 20 6f 70 65 6e 20 74 72 les with open tr
5bbb0 61 6e 73 61 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 ansactions */.
5bbc0 69 6e 74 20 6e 56 54 72 61 6e 73 3b 20 20 20 20 int nVTrans;
5bbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5bbe0 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 Allocated size
5bbf0 6f 66 20 61 56 54 72 61 6e 73 20 2a 2f 0a 20 20 of aVTrans */.
5bc00 56 54 61 62 6c 65 20 2a 70 44 69 73 63 6f 6e 6e VTable *pDisconn
5bc10 65 63 74 3b 20 20 20 20 2f 2a 20 44 69 73 63 6f ect; /* Disco
5bc20 6e 6e 65 63 74 20 74 68 65 73 65 20 69 6e 20 6e nnect these in n
5bc30 65 78 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 ext sqlite3_prep
5bc40 61 72 65 28 29 20 2a 2f 0a 23 65 6e 64 69 66 0a are() */.#endif.
5bc50 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 61 46 FuncDefHash aF
5bc60 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 unc;
5bc70 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66 /* Hash table of
5bc80 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 75 6e 63 connection func
5bc90 74 69 6f 6e 73 20 2a 2f 0a 20 20 48 61 73 68 20 tions */. Hash
5bca0 61 43 6f 6c 6c 53 65 71 3b 20 20 20 20 20 20 20 aCollSeq;
5bcb0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 /* All
5bcc0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
5bcd0 63 65 73 20 2a 2f 0a 20 20 42 75 73 79 48 61 6e ces */. BusyHan
5bce0 64 6c 65 72 20 62 75 73 79 48 61 6e 64 6c 65 72 dler busyHandler
5bcf0 3b 20 20 20 20 20 20 2f 2a 20 42 75 73 79 20 63 ; /* Busy c
5bd00 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 allback */. int
5bd10 20 62 75 73 79 54 69 6d 65 6f 75 74 3b 20 20 20 busyTimeout;
5bd20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75 /* Bu
5bd30 73 79 20 68 61 6e 64 6c 65 72 20 74 69 6d 65 6f sy handler timeo
5bd40 75 74 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0a 20 ut, in msec */.
5bd50 20 44 62 20 61 44 62 53 74 61 74 69 63 5b 32 5d Db aDbStatic[2]
5bd60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
5bd70 2a 20 53 74 61 74 69 63 20 73 70 61 63 65 20 66 * Static space f
5bd80 6f 72 20 74 68 65 20 32 20 64 65 66 61 75 6c 74 or the 2 default
5bd90 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 20 20 53 backends */. S
5bda0 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 avepoint *pSavep
5bdb0 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 oint; /*
5bdc0 4c 69 73 74 20 6f 66 20 61 63 74 69 76 65 20 73 List of active s
5bdd0 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 avepoints */. i
5bde0 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 nt nSavepoint;
5bdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5be00 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 74 72 Number of non-tr
5be10 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f ansaction savepo
5be20 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 ints */. int nS
5be30 74 61 74 65 6d 65 6e 74 3b 20 20 20 20 20 20 20 tatement;
5be40 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
5be50 72 20 6f 66 20 6e 65 73 74 65 64 20 73 74 61 74 r of nested stat
5be60 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f ement-transactio
5be70 6e 73 20 20 2a 2f 0a 20 20 75 38 20 69 73 54 72 ns */. u8 isTr
5be80 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 ansactionSavepoi
5be90 6e 74 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 nt; /* True i
5bea0 66 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 f the outermost
5beb0 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 20 54 savepoint is a T
5bec0 53 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c S */..#ifdef SQL
5bed0 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 ITE_ENABLE_UNLOC
5bee0 4b 5f 4e 4f 54 49 46 59 0a 20 20 2f 2a 20 54 68 K_NOTIFY. /* Th
5bef0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 e following vari
5bf00 61 62 6c 65 73 20 61 72 65 20 61 6c 6c 20 70 72 ables are all pr
5bf10 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 53 otected by the S
5bf20 54 41 54 49 43 5f 4d 41 53 54 45 52 20 0a 20 20 TATIC_MASTER .
5bf30 2a 2a 20 6d 75 74 65 78 2c 20 6e 6f 74 20 62 79 ** mutex, not by
5bf40 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 20 sqlite3.mutex.
5bf50 54 68 65 79 20 61 72 65 20 75 73 65 64 20 62 79 They are used by
5bf60 20 63 6f 64 65 20 69 6e 20 6e 6f 74 69 66 79 2e code in notify.
5bf70 63 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 c. . **. ** Wh
5bf80 65 6e 20 58 2e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e en X.pUnlockConn
5bf90 65 63 74 69 6f 6e 3d 3d 59 2c 20 74 68 61 74 20 ection==Y, that
5bfa0 6d 65 61 6e 73 20 74 68 61 74 20 58 20 69 73 20 means that X is
5bfb0 77 61 69 74 69 6e 67 20 66 6f 72 20 59 20 74 6f waiting for Y to
5bfc0 0a 20 20 2a 2a 20 75 6e 6c 6f 63 6b 20 73 6f 20 . ** unlock so
5bfd0 74 68 61 74 20 69 74 20 63 61 6e 20 70 72 6f 63 that it can proc
5bfe0 65 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 eed.. **. ** W
5bff0 68 65 6e 20 58 2e 70 42 6c 6f 63 6b 69 6e 67 43 hen X.pBlockingC
5c000 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20 74 68 onnection==Y, th
5c010 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 73 6f at means that so
5c020 6d 65 74 68 69 6e 67 20 74 68 61 74 20 58 20 74 mething that X t
5c030 72 69 65 64 0a 20 20 2a 2a 20 74 72 69 65 64 20 ried. ** tried
5c040 74 6f 20 64 6f 20 72 65 63 65 6e 74 6c 79 20 66 to do recently f
5c050 61 69 6c 65 64 20 77 69 74 68 20 61 6e 20 53 51 ailed with an SQ
5c060 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72 72 6f LITE_LOCKED erro
5c070 72 20 64 75 65 20 74 6f 20 6c 6f 63 6b 73 0a 20 r due to locks.
5c080 20 2a 2a 20 68 65 6c 64 20 62 79 20 59 2e 0a 20 ** held by Y..
5c090 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 */. sqlite3 *p
5c0a0 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 BlockingConnecti
5c0b0 6f 6e 3b 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f on; /* Connectio
5c0c0 6e 20 74 68 61 74 20 63 61 75 73 65 64 20 53 51 n that caused SQ
5c0d0 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 2a 2f 0a 20 LITE_LOCKED */.
5c0e0 20 73 71 6c 69 74 65 33 20 2a 70 55 6e 6c 6f 63 sqlite3 *pUnloc
5c0f0 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 20 20 20 kConnection;
5c100 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 /* Connec
5c110 74 69 6f 6e 20 74 6f 20 77 61 74 63 68 20 66 6f tion to watch fo
5c120 72 20 75 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 76 6f r unlock */. vo
5c130 69 64 20 2a 70 55 6e 6c 6f 63 6b 41 72 67 3b 20 id *pUnlockArg;
5c140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c150 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 /* Argument
5c160 74 6f 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 to xUnlockNotify
5c170 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 55 6e */. void (*xUn
5c180 6c 6f 63 6b 4e 6f 74 69 66 79 29 28 76 6f 69 64 lockNotify)(void
5c190 20 2a 2a 2c 20 69 6e 74 29 3b 20 20 2f 2a 20 55 **, int); /* U
5c1a0 6e 6c 6f 63 6b 20 6e 6f 74 69 66 79 20 63 61 6c nlock notify cal
5c1b0 6c 62 61 63 6b 20 2a 2f 0a 20 20 73 71 6c 69 74 lback */. sqlit
5c1c0 65 33 20 2a 70 4e 65 78 74 42 6c 6f 63 6b 65 64 e3 *pNextBlocked
5c1d0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 ; /* Next
5c1e0 20 69 6e 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 in list of all
5c1f0 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 blocked connecti
5c200 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b ons */.#endif.};
5c210 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 ../*.** A macro
5c220 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 to discover the
5c230 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 64 61 encoding of a da
5c240 74 61 62 61 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 tabase..*/.#defi
5c250 6e 65 20 45 4e 43 28 64 62 29 20 28 28 64 62 29 ne ENC(db) ((db)
5c260 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 68 65 6d 61 ->aDb[0].pSchema
5c270 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a 2a 2a 20 50 6f ->enc)../*.** Po
5c280 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f ssible values fo
5c290 72 20 74 68 65 20 73 71 6c 69 74 65 2e 66 6c 61 r the sqlite.fla
5c2a0 67 73 20 61 6e 64 20 6f 72 20 44 62 2e 66 6c 61 gs and or Db.fla
5c2b0 67 73 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a gs fields..**.**
5c2c0 20 4f 6e 20 73 71 6c 69 74 65 2e 66 6c 61 67 73 On sqlite.flags
5c2d0 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 49 6e 54 , the SQLITE_InT
5c2e0 72 61 6e 73 20 76 61 6c 75 65 20 6d 65 61 6e 73 rans value means
5c2f0 20 74 68 61 74 20 77 65 20 68 61 76 65 0a 2a 2a that we have.**
5c300 20 65 78 65 63 75 74 65 64 20 61 20 42 45 47 49 executed a BEGI
5c310 4e 2e 20 20 4f 6e 20 44 62 2e 66 6c 61 67 73 2c N. On Db.flags,
5c320 20 53 51 4c 49 54 45 5f 49 6e 54 72 61 6e 73 20 SQLITE_InTrans
5c330 6d 65 61 6e 73 20 61 20 73 74 61 74 65 6d 65 6e means a statemen
5c340 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e t.** transaction
5c350 20 69 73 20 61 63 74 69 76 65 20 6f 6e 20 74 68 is active on th
5c360 61 74 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 at particular da
5c370 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a tabase file..*/.
5c380 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 #define SQLITE_V
5c390 64 62 65 54 72 61 63 65 20 20 20 20 20 20 30 78 dbeTrace 0x
5c3a0 30 30 30 30 30 30 30 31 20 20 2f 2a 20 54 72 75 00000001 /* Tru
5c3b0 65 20 74 6f 20 74 72 61 63 65 20 56 44 42 45 20 e to trace VDBE
5c3c0 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 execution */.#de
5c3d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e 54 72 fine SQLITE_InTr
5c3e0 61 6e 73 20 20 20 20 20 20 20 20 30 78 30 30 30 ans 0x000
5c3f0 30 30 30 30 38 20 20 2f 2a 20 54 72 75 65 20 69 00008 /* True i
5c400 66 20 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 f in a transacti
5c410 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 on */.#define SQ
5c420 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 LITE_InternChang
5c430 65 73 20 20 30 78 30 30 30 30 30 30 31 30 20 20 es 0x00000010
5c440 2f 2a 20 55 6e 63 6f 6d 6d 69 74 74 65 64 20 48 /* Uncommitted H
5c450 61 73 68 20 74 61 62 6c 65 20 63 68 61 6e 67 65 ash table change
5c460 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s */.#define SQL
5c470 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 ITE_FullColNames
5c480 20 20 20 30 78 30 30 30 30 30 30 32 30 20 20 2f 0x00000020 /
5c490 2a 20 53 68 6f 77 20 66 75 6c 6c 20 63 6f 6c 75 * Show full colu
5c4a0 6d 6e 20 6e 61 6d 65 73 20 6f 6e 20 53 45 4c 45 mn names on SELE
5c4b0 43 54 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 CT */.#define SQ
5c4c0 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d LITE_ShortColNam
5c4d0 65 73 20 20 30 78 30 30 30 30 30 30 34 30 20 20 es 0x00000040
5c4e0 2f 2a 20 53 68 6f 77 20 73 68 6f 72 74 20 63 6f /* Show short co
5c4f0 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 2a 2f 0a 23 lumns names */.#
5c500 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 6f define SQLITE_Co
5c510 75 6e 74 52 6f 77 73 20 20 20 20 20 20 30 78 30 untRows 0x0
5c520 30 30 30 30 30 38 30 20 20 2f 2a 20 43 6f 75 6e 0000080 /* Coun
5c530 74 20 72 6f 77 73 20 63 68 61 6e 67 65 64 20 62 t rows changed b
5c540 79 20 49 4e 53 45 52 54 2c 20 2a 2f 0a 20 20 20 y INSERT, */.
5c550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c570 20 20 20 20 20 20 20 2f 2a 20 20 20 44 45 4c 45 /* DELE
5c580 54 45 2c 20 6f 72 20 55 50 44 41 54 45 20 61 6e TE, or UPDATE an
5c590 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 20 20 d return */.
5c5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c5c0 20 20 20 20 20 20 2f 2a 20 20 20 74 68 65 20 63 /* the c
5c5d0 6f 75 6e 74 20 75 73 69 6e 67 20 61 20 63 61 6c ount using a cal
5c5e0 6c 62 61 63 6b 2e 20 2a 2f 0a 23 64 65 66 69 6e lback. */.#defin
5c5f0 65 20 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c e SQLITE_NullCal
5c600 6c 62 61 63 6b 20 20 20 30 78 30 30 30 30 30 31 lback 0x000001
5c610 30 30 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 00 /* Invoke th
5c620 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20 e callback once
5c630 69 66 20 74 68 65 20 2a 2f 0a 20 20 20 20 20 20 if the */.
5c640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c660 20 20 20 20 2f 2a 20 20 20 72 65 73 75 6c 74 20 /* result
5c670 73 65 74 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a set is empty */.
5c680 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
5c690 71 6c 54 72 61 63 65 20 20 20 20 20 20 20 30 78 qlTrace 0x
5c6a0 30 30 30 30 30 32 30 30 20 20 2f 2a 20 44 65 62 00000200 /* Deb
5c6b0 75 67 20 70 72 69 6e 74 20 53 51 4c 20 61 73 20 ug print SQL as
5c6c0 69 74 20 65 78 65 63 75 74 65 73 20 2a 2f 0a 23 it executes */.#
5c6d0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 64 define SQLITE_Vd
5c6e0 62 65 4c 69 73 74 69 6e 67 20 20 20 20 30 78 30 beListing 0x0
5c6f0 30 30 30 30 34 30 30 20 20 2f 2a 20 44 65 62 75 0000400 /* Debu
5c700 67 20 6c 69 73 74 69 6e 67 73 20 6f 66 20 56 44 g listings of VD
5c710 42 45 20 70 72 6f 67 72 61 6d 73 20 2a 2f 0a 23 BE programs */.#
5c720 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 72 define SQLITE_Wr
5c730 69 74 65 53 63 68 65 6d 61 20 20 20 20 30 78 30 iteSchema 0x0
5c740 30 30 30 30 38 30 30 20 20 2f 2a 20 4f 4b 20 74 0000800 /* OK t
5c750 6f 20 75 70 64 61 74 65 20 53 51 4c 49 54 45 5f o update SQLITE_
5c760 4d 41 53 54 45 52 20 2a 2f 0a 23 64 65 66 69 6e MASTER */.#defin
5c770 65 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61 64 6c e SQLITE_NoReadl
5c780 6f 63 6b 20 20 20 20 20 30 78 30 30 30 30 31 30 ock 0x000010
5c790 30 30 20 20 2f 2a 20 52 65 61 64 6c 6f 63 6b 73 00 /* Readlocks
5c7a0 20 61 72 65 20 6f 6d 69 74 74 65 64 20 77 68 65 are omitted whe
5c7b0 6e 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n .
5c7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **
5c7e0 61 63 63 65 73 73 69 6e 67 20 72 65 61 64 2d 6f accessing read-o
5c7f0 6e 6c 79 20 64 61 74 61 62 61 73 65 73 20 2a 2f nly databases */
5c800 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5c810 49 67 6e 6f 72 65 43 68 65 63 6b 73 20 20 20 30 IgnoreChecks 0
5c820 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20 44 6f x00002000 /* Do
5c830 20 6e 6f 74 20 65 6e 66 6f 72 63 65 20 63 68 65 not enforce che
5c840 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a ck constraints *
5c850 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5c860 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 _ReadUncommitted
5c870 20 30 78 30 30 30 30 34 30 30 30 20 2f 2a 20 46 0x00004000 /* F
5c880 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 or shared-cache
5c890 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 mode */.#define
5c8a0 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c SQLITE_LegacyFil
5c8b0 65 46 6d 74 20 20 30 78 30 30 30 30 38 30 30 30 eFmt 0x00008000
5c8c0 20 20 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 20 /* Create new
5c8d0 64 61 74 61 62 61 73 65 73 20 69 6e 20 66 6f 72 databases in for
5c8e0 6d 61 74 20 31 20 2a 2f 0a 23 64 65 66 69 6e 65 mat 1 */.#define
5c8f0 20 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e SQLITE_FullFSyn
5c900 63 20 20 20 20 20 20 30 78 30 30 30 31 30 30 30 c 0x0001000
5c910 30 20 20 2f 2a 20 55 73 65 20 66 75 6c 6c 20 66 0 /* Use full f
5c920 73 79 6e 63 20 6f 6e 20 74 68 65 20 62 61 63 6b sync on the back
5c930 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 end */.#define S
5c940 51 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 QLITE_LoadExtens
5c950 69 6f 6e 20 20 30 78 30 30 30 32 30 30 30 30 20 ion 0x00020000
5c960 20 2f 2a 20 45 6e 61 62 6c 65 20 6c 6f 61 64 5f /* Enable load_
5c970 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 0a 23 64 extension */..#d
5c980 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 65 63 efine SQLITE_Rec
5c990 6f 76 65 72 79 4d 6f 64 65 20 20 20 30 78 30 30 overyMode 0x00
5c9a0 30 34 30 30 30 30 20 20 2f 2a 20 49 67 6e 6f 72 040000 /* Ignor
5c9b0 65 20 73 63 68 65 6d 61 20 65 72 72 6f 72 73 20 e schema errors
5c9c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5c9d0 45 5f 52 65 76 65 72 73 65 4f 72 64 65 72 20 20 E_ReverseOrder
5c9e0 20 30 78 30 30 31 30 30 30 30 30 20 20 2f 2a 20 0x00100000 /*
5c9f0 52 65 76 65 72 73 65 20 75 6e 6f 72 64 65 72 65 Reverse unordere
5ca00 64 20 53 45 4c 45 43 54 73 20 2a 2f 0a 23 64 65 d SELECTs */.#de
5ca10 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 65 63 54 fine SQLITE_RecT
5ca20 72 69 67 67 65 72 73 20 20 20 20 30 78 30 30 32 riggers 0x002
5ca30 30 30 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65 00000 /* Enable
5ca40 20 72 65 63 75 72 73 69 76 65 20 74 72 69 67 67 recursive trigg
5ca50 65 72 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 6f ers */../*.** Po
5ca60 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f ssible values fo
5ca70 72 20 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 r the sqlite.mag
5ca80 69 63 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 68 65 ic field..** The
5ca90 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f 62 74 numbers are obt
5caa0 61 69 6e 65 64 20 61 74 20 72 61 6e 64 6f 6d 20 ained at random
5cab0 61 6e 64 20 68 61 76 65 20 6e 6f 20 73 70 65 63 and have no spec
5cac0 69 61 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f 74 68 ial meaning, oth
5cad0 65 72 0a 2a 2a 20 74 68 61 6e 20 62 65 69 6e 67 er.** than being
5cae0 20 64 69 73 74 69 6e 63 74 20 66 72 6f 6d 20 6f distinct from o
5caf0 6e 65 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 23 ne another..*/.#
5cb00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
5cb10 47 49 43 5f 4f 50 45 4e 20 20 20 20 20 30 78 61 GIC_OPEN 0xa
5cb20 30 32 39 61 36 39 37 20 20 2f 2a 20 44 61 74 61 029a697 /* Data
5cb30 62 61 73 65 20 69 73 20 6f 70 65 6e 20 2a 2f 0a base is open */.
5cb40 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d #define SQLITE_M
5cb50 41 47 49 43 5f 43 4c 4f 53 45 44 20 20 20 30 78 AGIC_CLOSED 0x
5cb60 39 66 33 63 32 64 33 33 20 20 2f 2a 20 44 61 74 9f3c2d33 /* Dat
5cb70 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65 64 20 abase is closed
5cb80 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5cb90 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 20 20 20 E_MAGIC_SICK
5cba0 20 30 78 34 62 37 37 31 32 39 30 20 20 2f 2a 20 0x4b771290 /*
5cbb0 45 72 72 6f 72 20 61 6e 64 20 61 77 61 69 74 69 Error and awaiti
5cbc0 6e 67 20 63 6c 6f 73 65 20 2a 2f 0a 23 64 65 66 ng close */.#def
5cbd0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 ine SQLITE_MAGIC
5cbe0 5f 42 55 53 59 20 20 20 20 20 30 78 66 30 33 62 _BUSY 0xf03b
5cbf0 37 39 30 36 20 20 2f 2a 20 44 61 74 61 62 61 73 7906 /* Databas
5cc00 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 e currently in u
5cc10 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 se */.#define SQ
5cc20 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 LITE_MAGIC_ERROR
5cc30 20 20 20 20 30 78 62 35 33 35 37 39 33 30 20 20 0xb5357930
5cc40 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f 4d 49 53 /* An SQLITE_MIS
5cc50 55 53 45 20 65 72 72 6f 72 20 6f 63 63 75 72 72 USE error occurr
5cc60 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 ed */../*.** Eac
5cc70 68 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 h SQL function i
5cc80 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20 s defined by an
5cc90 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
5cca0 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 following.** str
5ccb0 75 63 74 75 72 65 2e 20 20 41 20 70 6f 69 6e 74 ucture. A point
5ccc0 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 er to this struc
5ccd0 74 75 72 65 20 69 73 20 73 74 6f 72 65 64 20 69 ture is stored i
5cce0 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61 46 75 n the sqlite.aFu
5ccf0 6e 63 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 nc.** hash table
5cd00 2e 20 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65 . When multiple
5cd10 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 functions have
5cd20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20 74 the same name, t
5cd30 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a he hash table.**
5cd40 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 6c 69 6e points to a lin
5cd50 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73 ked list of thes
5cd60 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f e structures..*/
5cd70 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 20 .struct FuncDef
5cd80 7b 0a 20 20 69 31 36 20 6e 41 72 67 3b 20 20 20 {. i16 nArg;
5cd90 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
5cda0 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e er of arguments.
5cdb0 20 20 2d 31 20 6d 65 61 6e 73 20 75 6e 6c 69 6d -1 means unlim
5cdc0 69 74 65 64 20 2a 2f 0a 20 20 75 38 20 69 50 72 ited */. u8 iPr
5cdd0 65 66 45 6e 63 3b 20 20 20 20 20 20 20 20 20 2f efEnc; /
5cde0 2a 20 50 72 65 66 65 72 72 65 64 20 74 65 78 74 * Preferred text
5cdf0 20 65 6e 63 6f 64 69 6e 67 20 28 53 51 4c 49 54 encoding (SQLIT
5ce00 45 5f 55 54 46 38 2c 20 31 36 4c 45 2c 20 31 36 E_UTF8, 16LE, 16
5ce10 42 45 29 20 2a 2f 0a 20 20 75 38 20 66 6c 61 67 BE) */. u8 flag
5ce20 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a s; /*
5ce30 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f Some combinatio
5ce40 6e 20 6f 66 20 53 51 4c 49 54 45 5f 46 55 4e 43 n of SQLITE_FUNC
5ce50 5f 2a 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 _* */. void *pU
5ce60 73 65 72 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 serData; /*
5ce70 55 73 65 72 20 64 61 74 61 20 70 61 72 61 6d 65 User data parame
5ce80 74 65 72 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 ter */. FuncDef
5ce90 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a *pNext; /*
5cea0 20 4e 65 78 74 20 66 75 6e 63 74 69 6f 6e 20 77 Next function w
5ceb0 69 74 68 20 73 61 6d 65 20 6e 61 6d 65 20 2a 2f ith same name */
5cec0 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 . void (*xFunc)
5ced0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
5cee0 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 *,int,sqlite3_va
5cef0 6c 75 65 2a 2a 29 3b 20 2f 2a 20 52 65 67 75 6c lue**); /* Regul
5cf00 61 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 ar function */.
5cf10 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 void (*xStep)(s
5cf20 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
5cf30 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
5cf40 65 2a 2a 29 3b 20 2f 2a 20 41 67 67 72 65 67 61 e**); /* Aggrega
5cf50 74 65 20 73 74 65 70 20 2a 2f 0a 20 20 76 6f 69 te step */. voi
5cf60 64 20 28 2a 78 46 69 6e 61 6c 69 7a 65 29 28 73 d (*xFinalize)(s
5cf70 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 qlite3_context*)
5cf80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5cf90 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 66 69 /* Aggregate fi
5cfa0 6e 61 6c 69 7a 65 72 20 2a 2f 0a 20 20 63 68 61 nalizer */. cha
5cfb0 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 r *zName;
5cfc0 20 20 2f 2a 20 53 51 4c 20 6e 61 6d 65 20 6f 66 /* SQL name of
5cfd0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 2a the function. *
5cfe0 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 48 61 /. FuncDef *pHa
5cff0 73 68 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 sh; /* Next
5d000 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e with a differen
5d010 74 20 6e 61 6d 65 20 62 75 74 20 74 68 65 20 73 t name but the s
5d020 61 6d 65 20 68 61 73 68 20 2a 2f 0a 7d 3b 0a 0a ame hash */.};..
5d030 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 /*.** Possible v
5d040 61 6c 75 65 73 20 66 6f 72 20 46 75 6e 63 44 65 alues for FuncDe
5d050 66 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 f.flags.*/.#defi
5d060 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c ne SQLITE_FUNC_L
5d070 49 4b 45 20 20 20 20 20 30 78 30 31 20 2f 2a 20 IKE 0x01 /*
5d080 43 61 6e 64 69 64 61 74 65 20 66 6f 72 20 74 68 Candidate for th
5d090 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69 7a 61 74 e LIKE optimizat
5d0a0 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ion */.#define S
5d0b0 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 20 QLITE_FUNC_CASE
5d0c0 20 20 20 20 30 78 30 32 20 2f 2a 20 43 61 73 65 0x02 /* Case
5d0d0 2d 73 65 6e 73 69 74 69 76 65 20 4c 49 4b 45 2d -sensitive LIKE-
5d0e0 74 79 70 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f type function */
5d0f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5d100 46 55 4e 43 5f 45 50 48 45 4d 20 20 20 20 30 78 FUNC_EPHEM 0x
5d110 30 34 20 2f 2a 20 45 70 68 65 6d 65 72 61 6c 2e 04 /* Ephemeral.
5d120 20 20 44 65 6c 65 74 65 20 77 69 74 68 20 56 44 Delete with VD
5d130 42 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 BE */.#define SQ
5d140 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f LITE_FUNC_NEEDCO
5d150 4c 4c 20 30 78 30 38 20 2f 2a 20 73 71 6c 69 74 LL 0x08 /* sqlit
5d160 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 e3GetFuncCollSeq
5d170 28 29 20 6d 69 67 68 74 20 62 65 20 63 61 6c 6c () might be call
5d180 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ed */.#define SQ
5d190 4c 49 54 45 5f 46 55 4e 43 5f 50 52 49 56 41 54 LITE_FUNC_PRIVAT
5d1a0 45 20 20 30 78 31 30 20 2f 2a 20 41 6c 6c 6f 77 E 0x10 /* Allow
5d1b0 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 ed for internal
5d1c0 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 use only */.#def
5d1d0 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f ine SQLITE_FUNC_
5d1e0 43 4f 55 4e 54 20 20 20 20 30 78 32 30 20 2f 2a COUNT 0x20 /*
5d1f0 20 42 75 69 6c 74 2d 69 6e 20 63 6f 75 6e 74 28 Built-in count(
5d200 2a 29 20 61 67 67 72 65 67 61 74 65 20 2a 2f 0a *) aggregate */.
5d210 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
5d220 77 69 6e 67 20 74 68 72 65 65 20 6d 61 63 72 6f wing three macro
5d230 73 2c 20 46 55 4e 43 54 49 4f 4e 28 29 2c 20 4c s, FUNCTION(), L
5d240 49 4b 45 46 55 4e 43 28 29 20 61 6e 64 20 41 47 IKEFUNC() and AG
5d250 47 52 45 47 41 54 45 28 29 20 61 72 65 0a 2a 2a GREGATE() are.**
5d260 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 used to create
5d270 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 65 72 73 the initializers
5d280 20 66 6f 72 20 74 68 65 20 46 75 6e 63 44 65 66 for the FuncDef
5d290 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a 0a structures..**.
5d2a0 2a 2a 20 20 20 46 55 4e 43 54 49 4f 4e 28 7a 4e ** FUNCTION(zN
5d2b0 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c ame, nArg, iArg,
5d2c0 20 62 4e 43 2c 20 78 46 75 6e 63 29 0a 2a 2a 20 bNC, xFunc).**
5d2d0 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 65 61 Used to crea
5d2e0 74 65 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63 te a scalar func
5d2f0 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 tion definition
5d300 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e of a function zN
5d310 61 6d 65 20 0a 2a 2a 20 20 20 20 20 69 6d 70 6c ame .** impl
5d320 65 6d 65 6e 74 65 64 20 62 79 20 43 20 66 75 6e emented by C fun
5d330 63 74 69 6f 6e 20 78 46 75 6e 63 20 74 68 61 74 ction xFunc that
5d340 20 61 63 63 65 70 74 73 20 6e 41 72 67 20 61 72 accepts nArg ar
5d350 67 75 6d 65 6e 74 73 2e 20 54 68 65 0a 2a 2a 20 guments. The.**
5d360 20 20 20 20 76 61 6c 75 65 20 70 61 73 73 65 64 value passed
5d370 20 61 73 20 69 41 72 67 20 69 73 20 63 61 73 74 as iArg is cast
5d380 20 74 6f 20 61 20 28 76 6f 69 64 2a 29 20 61 6e to a (void*) an
5d390 64 20 6d 61 64 65 20 61 76 61 69 6c 61 62 6c 65 d made available
5d3a0 0a 2a 2a 20 20 20 20 20 61 73 20 74 68 65 20 75 .** as the u
5d3b0 73 65 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65 ser-data (sqlite
5d3c0 33 5f 75 73 65 72 5f 64 61 74 61 28 29 29 20 66 3_user_data()) f
5d3d0 6f 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e or the function.
5d3e0 20 49 66 20 0a 2a 2a 20 20 20 20 20 61 72 67 75 If .** argu
5d3f0 6d 65 6e 74 20 62 4e 43 20 69 73 20 74 72 75 65 ment bNC is true
5d400 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 49 54 , then the SQLIT
5d410 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 E_FUNC_NEEDCOLL
5d420 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 0a flag is set..**.
5d430 2a 2a 20 20 20 41 47 47 52 45 47 41 54 45 28 7a ** AGGREGATE(z
5d440 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 Name, nArg, iArg
5d450 2c 20 62 4e 43 2c 20 78 53 74 65 70 2c 20 78 46 , bNC, xStep, xF
5d460 69 6e 61 6c 29 0a 2a 2a 20 20 20 20 20 55 73 65 inal).** Use
5d470 64 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 61 d to create an a
5d480 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
5d490 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 6d 70 n definition imp
5d4a0 6c 65 6d 65 6e 74 65 64 20 62 79 0a 2a 2a 20 20 lemented by.**
5d4b0 20 20 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f the C functio
5d4c0 6e 73 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 ns xStep and xFi
5d4d0 6e 61 6c 2e 20 54 68 65 20 66 69 72 73 74 20 66 nal. The first f
5d4e0 6f 75 72 20 70 61 72 61 6d 65 74 65 72 73 0a 2a our parameters.*
5d4f0 2a 20 20 20 20 20 61 72 65 20 69 6e 74 65 72 70 * are interp
5d500 72 65 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d reted in the sam
5d510 65 20 77 61 79 20 61 73 20 74 68 65 20 66 69 72 e way as the fir
5d520 73 74 20 34 20 70 61 72 61 6d 65 74 65 72 73 20 st 4 parameters
5d530 74 6f 0a 2a 2a 20 20 20 20 20 46 55 4e 43 54 49 to.** FUNCTI
5d540 4f 4e 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 4c 49 ON()..**.** LI
5d550 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 KEFUNC(zName, nA
5d560 72 67 2c 20 70 41 72 67 2c 20 66 6c 61 67 73 29 rg, pArg, flags)
5d570 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 .** Used to
5d580 63 72 65 61 74 65 20 61 20 73 63 61 6c 61 72 20 create a scalar
5d590 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 function definit
5d5a0 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f ion of a functio
5d5b0 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20 n zName .**
5d5c0 74 68 61 74 20 61 63 63 65 70 74 73 20 6e 41 72 that accepts nAr
5d5d0 67 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 g arguments and
5d5e0 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 is implemented b
5d5f0 79 20 61 20 63 61 6c 6c 20 74 6f 20 43 20 0a 2a y a call to C .*
5d600 2a 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 6c * function l
5d610 69 6b 65 46 75 6e 63 2e 20 41 72 67 75 6d 65 6e ikeFunc. Argumen
5d620 74 20 70 41 72 67 20 69 73 20 63 61 73 74 20 74 t pArg is cast t
5d630 6f 20 61 20 28 76 6f 69 64 20 2a 29 20 61 6e 64 o a (void *) and
5d640 20 6d 61 64 65 0a 2a 2a 20 20 20 20 20 61 76 61 made.** ava
5d650 69 6c 61 62 6c 65 20 61 73 20 74 68 65 20 66 75 ilable as the fu
5d660 6e 63 74 69 6f 6e 20 75 73 65 72 2d 64 61 74 61 nction user-data
5d670 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 (sqlite3_user_d
5d680 61 74 61 28 29 29 2e 20 54 68 65 0a 2a 2a 20 20 ata()). The.**
5d690 20 20 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 FuncDef.flags
5d6a0 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 variable is set
5d6b0 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61 to the value pa
5d6c0 73 73 65 64 20 61 73 20 74 68 65 20 66 6c 61 67 ssed as the flag
5d6d0 73 0a 2a 2a 20 20 20 20 20 70 61 72 61 6d 65 74 s.** paramet
5d6e0 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 er..*/.#define F
5d6f0 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e UNCTION(zName, n
5d700 41 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 Arg, iArg, bNC,
5d710 78 46 75 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 67 xFunc) \. {nArg
5d720 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 62 , SQLITE_UTF8, b
5d730 4e 43 2a 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e NC*SQLITE_FUNC_N
5d740 45 45 44 43 4f 4c 4c 2c 20 5c 0a 20 20 20 53 51 EEDCOLL, \. SQ
5d750 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 LITE_INT_TO_PTR(
5d760 69 41 72 67 29 2c 20 30 2c 20 78 46 75 6e 63 2c iArg), 0, xFunc,
5d770 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 0, 0, #zName, 0
5d780 7d 0a 23 64 65 66 69 6e 65 20 53 54 52 5f 46 55 }.#define STR_FU
5d790 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 NCTION(zName, nA
5d7a0 72 67 2c 20 70 41 72 67 2c 20 62 4e 43 2c 20 78 rg, pArg, bNC, x
5d7b0 46 75 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 67 2c Func) \. {nArg,
5d7c0 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 62 4e SQLITE_UTF8, bN
5d7d0 43 2a 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 C*SQLITE_FUNC_NE
5d7e0 45 44 43 4f 4c 4c 2c 20 5c 0a 20 20 20 70 41 72 EDCOLL, \. pAr
5d7f0 67 2c 20 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 g, 0, xFunc, 0,
5d800 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 0, #zName, 0}.#d
5d810 65 66 69 6e 65 20 4c 49 4b 45 46 55 4e 43 28 7a efine LIKEFUNC(z
5d820 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c Name, nArg, arg,
5d830 20 66 6c 61 67 73 29 20 5c 0a 20 20 7b 6e 41 72 flags) \. {nAr
5d840 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 g, SQLITE_UTF8,
5d850 66 6c 61 67 73 2c 20 28 76 6f 69 64 20 2a 29 61 flags, (void *)a
5d860 72 67 2c 20 30 2c 20 6c 69 6b 65 46 75 6e 63 2c rg, 0, likeFunc,
5d870 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 0, 0, #zName, 0
5d880 7d 0a 23 64 65 66 69 6e 65 20 41 47 47 52 45 47 }.#define AGGREG
5d890 41 54 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c ATE(zName, nArg,
5d8a0 20 61 72 67 2c 20 6e 63 2c 20 78 53 74 65 70 2c arg, nc, xStep,
5d8b0 20 78 46 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e 41 xFinal) \. {nA
5d8c0 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c rg, SQLITE_UTF8,
5d8d0 20 6e 63 2a 53 51 4c 49 54 45 5f 46 55 4e 43 5f nc*SQLITE_FUNC_
5d8e0 4e 45 45 44 43 4f 4c 4c 2c 20 5c 0a 20 20 20 53 NEEDCOLL, \. S
5d8f0 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 QLITE_INT_TO_PTR
5d900 28 61 72 67 29 2c 20 30 2c 20 30 2c 20 78 53 74 (arg), 0, 0, xSt
5d910 65 70 2c 78 46 69 6e 61 6c 2c 23 7a 4e 61 6d 65 ep,xFinal,#zName
5d920 2c 30 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 ,0}../*.** All c
5d930 75 72 72 65 6e 74 20 73 61 76 65 70 6f 69 6e 74 urrent savepoint
5d940 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 s are stored in
5d950 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 74 a linked list st
5d960 61 72 74 69 6e 67 20 61 74 0a 2a 2a 20 73 71 6c arting at.** sql
5d970 69 74 65 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e ite3.pSavepoint.
5d980 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 The first eleme
5d990 6e 74 20 69 6e 20 74 68 65 20 6c 69 73 74 20 69 nt in the list i
5d9a0 73 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e s the most recen
5d9b0 74 6c 79 0a 2a 2a 20 6f 70 65 6e 65 64 20 73 61 tly.** opened sa
5d9c0 76 65 70 6f 69 6e 74 2e 20 53 61 76 65 70 6f 69 vepoint. Savepoi
5d9d0 6e 74 73 20 61 72 65 20 61 64 64 65 64 20 74 6f nts are added to
5d9e0 20 74 68 65 20 6c 69 73 74 20 62 79 20 74 68 65 the list by the
5d9f0 20 76 64 62 65 0a 2a 2a 20 4f 50 5f 53 61 76 65 vdbe.** OP_Save
5da00 70 6f 69 6e 74 20 69 6e 73 74 72 75 63 74 69 6f point instructio
5da10 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 61 76 n..*/.struct Sav
5da20 65 70 6f 69 6e 74 20 7b 0a 20 20 63 68 61 72 20 epoint {. char
5da30 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 *zName;
5da40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5da50 2a 20 53 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 * Savepoint name
5da60 20 28 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 (nul-terminated
5da70 29 20 2a 2f 0a 20 20 53 61 76 65 70 6f 69 6e 74 ) */. Savepoint
5da80 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 *pNext;
5da90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
5daa0 72 65 6e 74 20 73 61 76 65 70 6f 69 6e 74 20 28 rent savepoint (
5dab0 69 66 20 61 6e 79 29 20 2a 2f 0a 7d 3b 0a 0a 2f if any) */.};../
5dac0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
5dad0 6e 67 20 61 72 65 20 75 73 65 64 20 61 73 20 74 ng are used as t
5dae0 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
5daf0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 53 61 ter to sqlite3Sa
5db00 76 65 70 6f 69 6e 74 28 29 2c 0a 2a 2a 20 61 6e vepoint(),.** an
5db10 64 20 61 73 20 74 68 65 20 50 31 20 61 72 67 75 d as the P1 argu
5db20 6d 65 6e 74 20 74 6f 20 74 68 65 20 4f 50 5f 53 ment to the OP_S
5db30 61 76 65 70 6f 69 6e 74 20 69 6e 73 74 72 75 63 avepoint instruc
5db40 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 tion..*/.#define
5db50 20 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e SAVEPOINT_BEGIN
5db60 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 0.#define
5db70 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 SAVEPOINT_RELEAS
5db80 45 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 E 1.#define S
5db90 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
5dba0 4b 20 20 20 32 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 K 2.../*.** Ea
5dbb0 63 68 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c 65 ch SQLite module
5dbc0 20 28 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 (virtual table
5dbd0 64 65 66 69 6e 69 74 69 6f 6e 29 20 69 73 20 64 definition) is d
5dbe0 65 66 69 6e 65 64 20 62 79 20 61 6e 0a 2a 2a 20 efined by an.**
5dbf0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
5dc00 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
5dc10 75 72 65 2c 20 73 74 6f 72 65 64 20 69 6e 20 74 ure, stored in t
5dc20 68 65 20 73 71 6c 69 74 65 33 2e 61 4d 6f 64 75 he sqlite3.aModu
5dc30 6c 65 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 le.** hash table
5dc40 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 6f 64 75 ..*/.struct Modu
5dc50 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c le {. const sql
5dc60 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f ite3_module *pMo
5dc70 64 75 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 43 dule; /* C
5dc80 61 6c 6c 62 61 63 6b 20 70 6f 69 6e 74 65 72 73 allback pointers
5dc90 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
5dca0 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 *zName;
5dcb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 /* Na
5dcc0 6d 65 20 70 61 73 73 65 64 20 74 6f 20 63 72 65 me passed to cre
5dcd0 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a ate_module() */.
5dce0 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 20 20 void *pAux;
5dcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5dd00 20 20 20 20 20 20 20 2f 2a 20 70 41 75 78 20 70 /* pAux p
5dd10 61 73 73 65 64 20 74 6f 20 63 72 65 61 74 65 5f assed to create_
5dd20 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 76 6f module() */. vo
5dd30 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76 id (*xDestroy)(v
5dd40 6f 69 64 20 2a 29 3b 20 20 20 20 20 20 20 20 20 oid *);
5dd50 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 /* Module des
5dd60 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e tructor function
5dd70 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 69 6e */.};../*.** in
5dd80 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
5dd90 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 each column of a
5dda0 6e 20 53 51 4c 20 74 61 62 6c 65 20 69 73 20 68 n SQL table is h
5ddb0 65 6c 64 20 69 6e 20 61 6e 20 69 6e 73 74 61 6e eld in an instan
5ddc0 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 ce.** of this st
5ddd0 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 ructure..*/.stru
5dde0 63 74 20 43 6f 6c 75 6d 6e 20 7b 0a 20 20 63 68 ct Column {. ch
5ddf0 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 2f ar *zName; /
5de00 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 63 * Name of this c
5de10 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 72 20 olumn */. Expr
5de20 2a 70 44 66 6c 74 3b 20 20 20 20 20 2f 2a 20 44 *pDflt; /* D
5de30 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 efault value of
5de40 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 this column */.
5de50 20 63 68 61 72 20 2a 7a 44 66 6c 74 3b 20 20 20 char *zDflt;
5de60 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 74 65 /* Original te
5de70 78 74 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c xt of the defaul
5de80 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 63 68 61 t value */. cha
5de90 72 20 2a 7a 54 79 70 65 3b 20 20 20 20 20 2f 2a r *zType; /*
5dea0 20 44 61 74 61 20 74 79 70 65 20 66 6f 72 20 74 Data type for t
5deb0 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 his column */.
5dec0 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 char *zColl;
5ded0 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 /* Collating se
5dee0 71 75 65 6e 63 65 2e 20 20 49 66 20 4e 55 4c 4c quence. If NULL
5def0 2c 20 75 73 65 20 74 68 65 20 64 65 66 61 75 6c , use the defaul
5df00 74 20 2a 2f 0a 20 20 75 38 20 6e 6f 74 4e 75 6c t */. u8 notNul
5df10 6c 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 l; /* True
5df20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 4e 4f if there is a NO
5df30 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e T NULL constrain
5df40 74 20 2a 2f 0a 20 20 75 38 20 69 73 50 72 69 6d t */. u8 isPrim
5df50 4b 65 79 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 Key; /* True
5df60 69 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 if this column i
5df70 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 50 52 s part of the PR
5df80 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 63 IMARY KEY */. c
5df90 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20 20 har affinity;
5dfa0 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 51 /* One of the SQ
5dfb0 4c 49 54 45 5f 41 46 46 5f 2e 2e 2e 20 76 61 6c LITE_AFF_... val
5dfc0 75 65 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ues */.#ifndef S
5dfd0 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
5dfe0 41 4c 54 41 42 4c 45 0a 20 20 75 38 20 69 73 48 ALTABLE. u8 isH
5dff0 69 64 64 65 6e 3b 20 20 20 20 20 2f 2a 20 54 72 idden; /* Tr
5e000 75 65 20 69 66 20 74 68 69 73 20 63 6f 6c 75 6d ue if this colum
5e010 6e 20 69 73 20 27 68 69 64 64 65 6e 27 20 2a 2f n is 'hidden' */
5e020 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a .#endif.};../*.*
5e030 2a 20 41 20 22 43 6f 6c 6c 61 74 69 6e 67 20 53 * A "Collating S
5e040 65 71 75 65 6e 63 65 22 20 69 73 20 64 65 66 69 equence" is defi
5e050 6e 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e ned by an instan
5e060 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
5e070 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 ing.** structure
5e080 2e 20 43 6f 6e 63 65 70 74 75 61 6c 6c 79 2c 20 . Conceptually,
5e090 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 a collating sequ
5e0a0 65 6e 63 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 ence consists of
5e0b0 20 61 20 6e 61 6d 65 20 61 6e 64 0a 2a 2a 20 61 a name and.** a
5e0c0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 6f 75 74 comparison rout
5e0d0 69 6e 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 ine that defines
5e0e0 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68 the order of th
5e0f0 61 74 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a at sequence..**.
5e100 2a 2a 20 54 68 65 72 65 20 6d 61 79 20 74 77 6f ** There may two
5e110 20 73 65 70 61 72 61 74 65 20 69 6d 70 6c 65 6d separate implem
5e120 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 entations of the
5e130 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 collation funct
5e140 69 6f 6e 2c 20 6f 6e 65 0a 2a 2a 20 74 68 61 74 ion, one.** that
5e150 20 70 72 6f 63 65 73 73 65 73 20 74 65 78 74 20 processes text
5e160 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e in UTF-8 encodin
5e170 67 20 28 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 29 g (CollSeq.xCmp)
5e180 20 61 6e 64 20 61 6e 6f 74 68 65 72 20 74 68 61 and another tha
5e190 74 0a 2a 2a 20 70 72 6f 63 65 73 73 65 73 20 74 t.** processes t
5e1a0 65 78 74 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 ext encoded in U
5e1b0 54 46 2d 31 36 20 28 43 6f 6c 6c 53 65 71 2e 78 TF-16 (CollSeq.x
5e1c0 43 6d 70 31 36 29 2c 20 75 73 69 6e 67 20 74 68 Cmp16), using th
5e1d0 65 20 6d 61 63 68 69 6e 65 0a 2a 2a 20 6e 61 74 e machine.** nat
5e1e0 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e 20 ive byte order.
5e1f0 57 68 65 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e When a collation
5e200 20 73 65 71 75 65 6e 63 65 20 69 73 20 69 6e 76 sequence is inv
5e210 6f 6b 65 64 2c 20 53 51 4c 69 74 65 20 73 65 6c oked, SQLite sel
5e220 65 63 74 73 0a 2a 2a 20 74 68 65 20 76 65 72 73 ects.** the vers
5e230 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 72 65 ion that will re
5e240 71 75 69 72 65 20 74 68 65 20 6c 65 61 73 74 20 quire the least
5e250 65 78 70 65 6e 73 69 76 65 20 65 6e 63 6f 64 69 expensive encodi
5e260 6e 67 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f ng.** translatio
5e270 6e 73 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a ns, if any..**.*
5e280 2a 20 54 68 65 20 43 6f 6c 6c 53 65 71 2e 70 55 * The CollSeq.pU
5e290 73 65 72 20 6d 65 6d 62 65 72 20 76 61 72 69 61 ser member varia
5e2a0 62 6c 65 20 69 73 20 61 6e 20 65 78 74 72 61 20 ble is an extra
5e2b0 70 61 72 61 6d 65 74 65 72 20 74 68 61 74 20 70 parameter that p
5e2c0 61 73 73 65 64 20 69 6e 0a 2a 2a 20 61 73 20 74 assed in.** as t
5e2d0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
5e2e0 74 20 74 6f 20 74 68 65 20 55 54 46 2d 38 20 63 t to the UTF-8 c
5e2f0 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 omparison functi
5e300 6f 6e 2c 20 78 43 6d 70 2e 0a 2a 2a 20 43 6f 6c on, xCmp..** Col
5e310 6c 53 65 71 2e 70 55 73 65 72 31 36 20 69 73 20 lSeq.pUser16 is
5e320 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66 the equivalent f
5e330 6f 72 20 74 68 65 20 55 54 46 2d 31 36 20 63 6f or the UTF-16 co
5e340 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f mparison functio
5e350 6e 2c 0a 2a 2a 20 78 43 6d 70 31 36 2e 0a 2a 2a n,.** xCmp16..**
5e360 0a 2a 2a 20 49 66 20 62 6f 74 68 20 43 6f 6c 6c .** If both Coll
5e370 53 65 71 2e 78 43 6d 70 20 61 6e 64 20 43 6f 6c Seq.xCmp and Col
5e380 6c 53 65 71 2e 78 43 6d 70 31 36 20 61 72 65 20 lSeq.xCmp16 are
5e390 4e 55 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 NULL, it means t
5e3a0 68 61 74 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c 61 hat the.** colla
5e3b0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 ting sequence is
5e3c0 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 49 6e 64 undefined. Ind
5e3d0 69 63 65 73 20 62 75 69 6c 74 20 6f 6e 20 61 6e ices built on an
5e3e0 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 63 6f undefined.** co
5e3f0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
5e400 20 6d 61 79 20 6e 6f 74 20 62 65 20 72 65 61 64 may not be read
5e410 20 6f 72 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a or written..*/.
5e420 73 74 72 75 63 74 20 43 6f 6c 6c 53 65 71 20 7b struct CollSeq {
5e430 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 . char *zName;
5e440 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
5e450 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e of the collatin
5e460 67 20 73 65 71 75 65 6e 63 65 2c 20 55 54 46 2d g sequence, UTF-
5e470 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 75 8 encoded */. u
5e480 38 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 8 enc;
5e490 20 20 20 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 /* Text enc
5e4a0 6f 64 69 6e 67 20 68 61 6e 64 6c 65 64 20 62 79 oding handled by
5e4b0 20 78 43 6d 70 28 29 20 2a 2f 0a 20 20 75 38 20 xCmp() */. u8
5e4c0 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 type;
5e4d0 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 /* One of the
5e4e0 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 2e 2e 2e SQLITE_COLL_...
5e4f0 20 76 61 6c 75 65 73 20 62 65 6c 6f 77 20 2a 2f values below */
5e500 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 3b 20 . void *pUser;
5e510 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
5e520 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 t argument to xC
5e530 6d 70 28 29 20 2a 2f 0a 20 20 69 6e 74 20 28 2a mp() */. int (*
5e540 78 43 6d 70 29 28 76 6f 69 64 2a 2c 69 6e 74 2c xCmp)(void*,int,
5e550 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e const void*, in
5e560 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b t, const void*);
5e570 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
5e580 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 void*); /* Dest
5e590 72 75 63 74 6f 72 20 66 6f 72 20 70 55 73 65 72 ructor for pUser
5e5a0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c */.};../*.** Al
5e5b0 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20 lowed values of
5e5c0 43 6f 6c 6c 53 65 71 2e 74 79 70 65 3a 0a 2a 2f CollSeq.type:.*/
5e5d0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5e5e0 43 4f 4c 4c 5f 42 49 4e 41 52 59 20 20 31 20 20 COLL_BINARY 1
5e5f0 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 6d /* The default m
5e600 65 6d 63 6d 70 28 29 20 63 6f 6c 6c 61 74 69 6e emcmp() collatin
5e610 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 g sequence */.#d
5e620 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c efine SQLITE_COL
5e630 4c 5f 4e 4f 43 41 53 45 20 20 32 20 20 2f 2a 20 L_NOCASE 2 /*
5e640 54 68 65 20 62 75 69 6c 74 2d 69 6e 20 4e 4f 43 The built-in NOC
5e650 41 53 45 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 ASE collating se
5e660 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e quence */.#defin
5e670 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 52 45 e SQLITE_COLL_RE
5e680 56 45 52 53 45 20 33 20 20 2f 2a 20 54 68 65 20 VERSE 3 /* The
5e690 62 75 69 6c 74 2d 69 6e 20 52 45 56 45 52 53 45 built-in REVERSE
5e6a0 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
5e6b0 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 nce */.#define S
5e6c0 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 20 QLITE_COLL_USER
5e6d0 20 20 20 30 20 20 2f 2a 20 41 6e 79 20 6f 74 68 0 /* Any oth
5e6e0 65 72 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 er user-defined
5e6f0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
5e700 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 ce */../*.** A s
5e710 6f 72 74 20 6f 72 64 65 72 20 63 61 6e 20 62 65 ort order can be
5e720 20 65 69 74 68 65 72 20 41 53 43 20 6f 72 20 44 either ASC or D
5e730 45 53 43 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ESC..*/.#define
5e740 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 20 20 SQLITE_SO_ASC
5e750 20 20 20 20 30 20 20 2f 2a 20 53 6f 72 74 20 69 0 /* Sort i
5e760 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 n ascending orde
5e770 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c r */.#define SQL
5e780 49 54 45 5f 53 4f 5f 44 45 53 43 20 20 20 20 20 ITE_SO_DESC
5e790 20 31 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 1 /* Sort in a
5e7a0 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a scending order *
5e7b0 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 /../*.** Column
5e7c0 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 2e 0a affinity types..
5e7d0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 75 73 65 64 **.** These used
5e7e0 20 74 6f 20 68 61 76 65 20 6d 6e 65 6d 6f 6e 69 to have mnemoni
5e7f0 63 20 6e 61 6d 65 20 6c 69 6b 65 20 27 69 27 20 c name like 'i'
5e800 66 6f 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 for SQLITE_AFF_I
5e810 4e 54 45 47 45 52 20 61 6e 64 0a 2a 2a 20 27 74 NTEGER and.** 't
5e820 27 20 66 6f 72 20 53 51 4c 49 54 45 5f 41 46 46 ' for SQLITE_AFF
5e830 5f 54 45 58 54 2e 20 20 42 75 74 20 77 65 20 63 _TEXT. But we c
5e840 61 6e 20 73 61 76 65 20 61 20 6c 69 74 74 6c 65 an save a little
5e850 20 73 70 61 63 65 20 61 6e 64 20 69 6d 70 72 6f space and impro
5e860 76 65 0a 2a 2a 20 74 68 65 20 73 70 65 65 64 20 ve.** the speed
5e870 61 20 6c 69 74 74 6c 65 20 62 79 20 6e 75 6d 62 a little by numb
5e880 65 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65 73 ering the values
5e890 20 63 6f 6e 73 65 63 75 74 69 76 65 6c 79 2e 20 consecutively.
5e8a0 20 0a 2a 2a 0a 2a 2a 20 42 75 74 20 72 61 74 68 .**.** But rath
5e8b0 65 72 20 74 68 61 6e 20 73 74 61 72 74 20 77 69 er than start wi
5e8c0 74 68 20 30 20 6f 72 20 31 2c 20 77 65 20 62 65 th 0 or 1, we be
5e8d0 67 69 6e 20 77 69 74 68 20 27 61 27 2e 20 20 54 gin with 'a'. T
5e8e0 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 68 65 6e hat way,.** when
5e8f0 20 6d 75 6c 74 69 70 6c 65 20 61 66 66 69 6e 69 multiple affini
5e900 74 79 20 74 79 70 65 73 20 61 72 65 20 63 6f 6e ty types are con
5e910 63 61 74 65 6e 61 74 65 64 20 69 6e 74 6f 20 61 catenated into a
5e920 20 73 74 72 69 6e 67 20 61 6e 64 0a 2a 2a 20 75 string and.** u
5e930 73 65 64 20 61 73 20 74 68 65 20 50 34 20 6f 70 sed as the P4 op
5e940 65 72 61 6e 64 2c 20 74 68 65 79 20 77 69 6c 6c erand, they will
5e950 20 62 65 20 6d 6f 72 65 20 72 65 61 64 61 62 6c be more readabl
5e960 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 61 6c e..**.** Note al
5e970 73 6f 20 74 68 61 74 20 74 68 65 20 6e 75 6d 65 so that the nume
5e980 72 69 63 20 74 79 70 65 73 20 61 72 65 20 67 72 ric types are gr
5e990 6f 75 70 65 64 20 74 6f 67 65 74 68 65 72 20 73 ouped together s
5e9a0 6f 20 74 68 61 74 20 74 65 73 74 69 6e 67 0a 2a o that testing.*
5e9b0 2a 20 66 6f 72 20 61 20 6e 75 6d 65 72 69 63 20 * for a numeric
5e9c0 74 79 70 65 20 69 73 20 61 20 73 69 6e 67 6c 65 type is a single
5e9d0 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a comparison..*/.
5e9e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
5e9f0 46 46 5f 54 45 58 54 20 20 20 20 20 27 61 27 0a FF_TEXT 'a'.
5ea00 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
5ea10 46 46 5f 4e 4f 4e 45 20 20 20 20 20 27 62 27 0a FF_NONE 'b'.
5ea20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
5ea30 46 46 5f 4e 55 4d 45 52 49 43 20 20 27 63 27 0a FF_NUMERIC 'c'.
5ea40 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
5ea50 46 46 5f 49 4e 54 45 47 45 52 20 20 27 64 27 0a FF_INTEGER 'd'.
5ea60 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
5ea70 46 46 5f 52 45 41 4c 20 20 20 20 20 27 65 27 0a FF_REAL 'e'.
5ea80 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 .#define sqlite3
5ea90 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 IsNumericAffinit
5eaa0 79 28 58 29 20 20 28 28 58 29 3e 3d 53 51 4c 49 y(X) ((X)>=SQLI
5eab0 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 29 0a TE_AFF_NUMERIC).
5eac0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 ./*.** The SQLIT
5ead0 45 5f 41 46 46 5f 4d 41 53 4b 20 76 61 6c 75 65 E_AFF_MASK value
5eae0 73 20 6d 61 73 6b 73 20 6f 66 66 20 74 68 65 20 s masks off the
5eaf0 73 69 67 6e 69 66 69 63 61 6e 74 20 62 69 74 73 significant bits
5eb00 20 6f 66 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 of an.** affini
5eb10 74 79 20 76 61 6c 75 65 2e 20 0a 2a 2f 0a 23 64 ty value. .*/.#d
5eb20 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 efine SQLITE_AFF
5eb30 5f 4d 41 53 4b 20 20 20 20 20 30 78 36 37 0a 0a _MASK 0x67..
5eb40 2f 2a 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c /*.** Additional
5eb50 20 62 69 74 20 76 61 6c 75 65 73 20 74 68 61 74 bit values that
5eb60 20 63 61 6e 20 62 65 20 4f 52 65 64 20 77 69 74 can be ORed wit
5eb70 68 20 61 6e 20 61 66 66 69 6e 69 74 79 20 77 69 h an affinity wi
5eb80 74 68 6f 75 74 0a 2a 2a 20 63 68 61 6e 67 69 6e thout.** changin
5eb90 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 2e 0a g the affinity..
5eba0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5ebb0 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 20 20 30 E_JUMPIFNULL 0
5ebc0 78 30 38 20 20 2f 2a 20 6a 75 6d 70 73 20 69 66 x08 /* jumps if
5ebd0 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 either operand
5ebe0 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 is NULL */.#defi
5ebf0 6e 65 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 ne SQLITE_STOREP
5ec00 32 20 20 20 20 20 20 30 78 31 30 20 20 2f 2a 20 2 0x10 /*
5ec10 53 74 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20 Store result in
5ec20 72 65 67 5b 50 32 5d 20 72 61 74 68 65 72 20 74 reg[P2] rather t
5ec30 68 61 6e 20 6a 75 6d 70 20 2a 2f 0a 0a 2f 2a 0a han jump */../*.
5ec40 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 20 6f 66 20 ** An object of
5ec50 74 68 69 73 20 74 79 70 65 20 69 73 20 63 72 65 this type is cre
5ec60 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 76 69 ated for each vi
5ec70 72 74 75 61 6c 20 74 61 62 6c 65 20 70 72 65 73 rtual table pres
5ec80 65 6e 74 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 ent in.** the da
5ec90 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 0a tabase schema. .
5eca0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 **.** If the dat
5ecb0 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 abase schema is
5ecc0 73 68 61 72 65 64 2c 20 74 68 65 6e 20 74 68 65 shared, then the
5ecd0 72 65 20 69 73 20 6f 6e 65 20 69 6e 73 74 61 6e re is one instan
5ece0 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74 ce of this.** st
5ecf0 72 75 63 74 75 72 65 20 66 6f 72 20 65 61 63 68 ructure for each
5ed00 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
5ed10 74 69 6f 6e 20 28 73 71 6c 69 74 65 33 2a 29 20 tion (sqlite3*)
5ed20 74 68 61 74 20 75 73 65 73 20 74 68 65 20 73 68 that uses the sh
5ed30 61 72 65 64 0a 2a 2a 20 73 63 68 65 6d 61 2e 20 ared.** schema.
5ed40 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 This is because
5ed50 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f each database co
5ed60 6e 6e 65 63 74 69 6f 6e 20 72 65 71 75 69 72 65 nnection require
5ed70 73 20 69 74 73 20 6f 77 6e 20 75 6e 69 71 75 65 s its own unique
5ed80 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 .** instance of
5ed90 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 the sqlite3_vtab
5eda0 2a 20 68 61 6e 64 6c 65 20 75 73 65 64 20 74 6f * handle used to
5edb0 20 61 63 63 65 73 73 20 74 68 65 20 76 69 72 74 access the virt
5edc0 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a 20 69 6d ual table .** im
5edd0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 73 71 plementation. sq
5ede0 6c 69 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 lite3_vtab* hand
5edf0 6c 65 73 20 63 61 6e 20 6e 6f 74 20 62 65 20 73 les can not be s
5ee00 68 61 72 65 64 20 62 65 74 77 65 65 6e 20 0a 2a hared between .*
5ee10 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * database conne
5ee20 63 74 69 6f 6e 73 2c 20 65 76 65 6e 20 77 68 65 ctions, even whe
5ee30 6e 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 n the rest of th
5ee40 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 e in-memory data
5ee50 62 61 73 65 20 0a 2a 2a 20 73 63 68 65 6d 61 20 base .** schema
5ee60 69 73 20 73 68 61 72 65 64 2c 20 61 73 20 74 68 is shared, as th
5ee70 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
5ee80 20 6f 66 74 65 6e 20 73 74 6f 72 65 73 20 74 68 often stores th
5ee90 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f e database.** co
5eea0 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 nnection handle
5eeb0 70 61 73 73 65 64 20 74 6f 20 69 74 20 76 69 61 passed to it via
5eec0 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 28 29 20 the xConnect()
5eed0 6f 72 20 78 43 72 65 61 74 65 28 29 20 6d 65 74 or xCreate() met
5eee0 68 6f 64 0a 2a 2a 20 64 75 72 69 6e 67 20 69 6e hod.** during in
5eef0 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 6e 74 itialization int
5ef00 65 72 6e 61 6c 6c 79 2e 20 54 68 69 73 20 64 61 ernally. This da
5ef10 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
5ef20 6e 20 68 61 6e 64 6c 65 20 6d 61 79 0a 2a 2a 20 n handle may.**
5ef30 74 68 65 6e 20 75 73 65 64 20 62 79 20 74 68 65 then used by the
5ef40 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 virtual table i
5ef50 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f mplementation to
5ef60 20 61 63 63 65 73 73 20 72 65 61 6c 20 74 61 62 access real tab
5ef70 6c 65 73 20 0a 2a 2a 20 77 69 74 68 69 6e 20 74 les .** within t
5ef80 68 65 20 64 61 74 61 62 61 73 65 2e 20 53 6f 20 he database. So
5ef90 74 68 61 74 20 74 68 65 79 20 61 70 70 65 61 72 that they appear
5efa0 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 as part of the
5efb0 63 61 6c 6c 65 72 73 20 0a 2a 2a 20 74 72 61 6e callers .** tran
5efc0 73 61 63 74 69 6f 6e 2c 20 74 68 65 73 65 20 61 saction, these a
5efd0 63 63 65 73 73 65 73 20 6e 65 65 64 20 74 6f 20 ccesses need to
5efe0 62 65 20 6d 61 64 65 20 76 69 61 20 74 68 65 20 be made via the
5eff0 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 0a 2a same database .*
5f000 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 * connection as
5f010 74 68 61 74 20 75 73 65 64 20 74 6f 20 65 78 65 that used to exe
5f020 63 75 74 65 20 53 51 4c 20 6f 70 65 72 61 74 69 cute SQL operati
5f030 6f 6e 73 20 6f 6e 20 74 68 65 20 76 69 72 74 75 ons on the virtu
5f040 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 al table..**.**
5f050 41 6c 6c 20 56 54 61 62 6c 65 20 6f 62 6a 65 63 All VTable objec
5f060 74 73 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f ts that correspo
5f070 6e 64 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 74 nd to a single t
5f080 61 62 6c 65 20 69 6e 20 61 20 73 68 61 72 65 64 able in a shared
5f090 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 .** database sch
5f0a0 65 6d 61 20 61 72 65 20 69 6e 69 74 69 61 6c 6c ema are initiall
5f0b0 79 20 73 74 6f 72 65 64 20 69 6e 20 61 20 6c 69 y stored in a li
5f0c0 6e 6b 65 64 2d 6c 69 73 74 20 70 6f 69 6e 74 65 nked-list pointe
5f0d0 64 20 74 6f 20 62 79 0a 2a 2a 20 74 68 65 20 54 d to by.** the T
5f0e0 61 62 6c 65 2e 70 56 54 61 62 6c 65 20 6d 65 6d able.pVTable mem
5f0f0 62 65 72 20 76 61 72 69 61 62 6c 65 20 6f 66 20 ber variable of
5f100 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
5f110 67 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 2e 0a g Table object..
5f120 2a 2a 20 57 68 65 6e 20 61 6e 20 73 71 6c 69 74 ** When an sqlit
5f130 65 33 5f 70 72 65 70 61 72 65 28 29 20 6f 70 65 e3_prepare() ope
5f140 72 61 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 ration is requir
5f150 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 ed to access the
5f160 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c virtual.** tabl
5f170 65 2c 20 69 74 20 73 65 61 72 63 68 65 73 20 74 e, it searches t
5f180 68 65 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20 he list for the
5f190 56 54 61 62 6c 65 20 74 68 61 74 20 63 6f 72 72 VTable that corr
5f1a0 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 0a 2a esponds to the.*
5f1b0 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * database conne
5f1c0 63 74 69 6f 6e 20 64 6f 69 6e 67 20 74 68 65 20 ction doing the
5f1d0 70 72 65 70 61 72 69 6e 67 20 73 6f 20 61 73 20 preparing so as
5f1e0 74 6f 20 75 73 65 20 74 68 65 20 63 6f 72 72 65 to use the corre
5f1f0 63 74 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 74 ct.** sqlite3_vt
5f200 61 62 2a 20 68 61 6e 64 6c 65 20 69 6e 20 74 68 ab* handle in th
5f210 65 20 63 6f 6d 70 69 6c 65 64 20 71 75 65 72 79 e compiled query
5f220 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 ..**.** When an
5f230 69 6e 2d 6d 65 6d 6f 72 79 20 54 61 62 6c 65 20 in-memory Table
5f240 6f 62 6a 65 63 74 20 69 73 20 64 65 6c 65 74 65 object is delete
5f250 64 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 d (for example w
5f260 68 65 6e 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d hen the.** schem
5f270 61 20 69 73 20 62 65 69 6e 67 20 72 65 6c 6f 61 a is being reloa
5f280 64 65 64 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 ded for some rea
5f290 73 6f 6e 29 2c 20 74 68 65 20 56 54 61 62 6c 65 son), the VTable
5f2a0 20 6f 62 6a 65 63 74 73 20 61 72 65 20 6e 6f 74 objects are not
5f2b0 20 0a 2a 2a 20 64 65 6c 65 74 65 64 20 61 6e 64 .** deleted and
5f2c0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 the sqlite3_vta
5f2d0 62 2a 20 68 61 6e 64 6c 65 73 20 61 72 65 20 6e b* handles are n
5f2e0 6f 74 20 78 44 69 73 63 6f 6e 6e 65 63 74 28 29 ot xDisconnect()
5f2f0 65 64 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 ed .** immediate
5f300 6c 79 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65 ly. Instead, the
5f310 79 20 61 72 65 20 6d 6f 76 65 64 20 66 72 6f 6d y are moved from
5f320 20 74 68 65 20 54 61 62 6c 65 2e 70 56 54 61 62 the Table.pVTab
5f330 6c 65 20 6c 69 73 74 20 74 6f 0a 2a 2a 20 61 6e le list to.** an
5f340 6f 74 68 65 72 20 6c 69 6e 6b 65 64 20 6c 69 73 other linked lis
5f350 74 20 68 65 61 64 65 64 20 62 79 20 74 68 65 20 t headed by the
5f360 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e sqlite3.pDisconn
5f370 65 63 74 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 ect member of th
5f380 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 e.** correspondi
5f390 6e 67 20 73 71 6c 69 74 65 33 20 73 74 72 75 63 ng sqlite3 struc
5f3a0 74 75 72 65 2e 20 54 68 65 79 20 61 72 65 20 74 ture. They are t
5f3b0 68 65 6e 20 64 65 6c 65 74 65 64 2f 78 44 69 73 hen deleted/xDis
5f3c0 63 6f 6e 6e 65 63 74 65 64 20 0a 2a 2a 20 6e 65 connected .** ne
5f3d0 78 74 20 74 69 6d 65 20 61 20 73 74 61 74 65 6d xt time a statem
5f3e0 65 6e 74 20 69 73 20 70 72 65 70 61 72 65 64 20 ent is prepared
5f3f0 75 73 69 6e 67 20 73 61 69 64 20 73 71 6c 69 74 using said sqlit
5f400 65 33 2a 2e 20 54 68 69 73 20 69 73 20 64 6f 6e e3*. This is don
5f410 65 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 64 65 e.** to avoid de
5f420 61 64 6c 6f 63 6b 20 69 73 73 75 65 73 20 69 6e adlock issues in
5f430 76 6f 6c 76 69 6e 67 20 6d 75 6c 74 69 70 6c 65 volving multiple
5f440 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 20 6d sqlite3.mutex m
5f450 75 74 65 78 65 73 2e 0a 2a 2a 20 52 65 66 65 72 utexes..** Refer
5f460 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f to comments abo
5f470 76 65 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 ve function sqli
5f480 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 te3VtabUnlockLis
5f490 74 28 29 20 66 6f 72 20 61 6e 0a 2a 2a 20 65 78 t() for an.** ex
5f4a0 70 6c 61 6e 61 74 69 6f 6e 20 61 73 20 74 6f 20 planation as to
5f4b0 77 68 79 20 69 74 20 69 73 20 73 61 66 65 20 74 why it is safe t
5f4c0 6f 20 61 64 64 20 61 6e 20 65 6e 74 72 79 20 74 o add an entry t
5f4d0 6f 20 61 6e 20 73 71 6c 69 74 65 33 2e 70 44 69 o an sqlite3.pDi
5f4e0 73 63 6f 6e 6e 65 63 74 0a 2a 2a 20 6c 69 73 74 sconnect.** list
5f4f0 20 77 69 74 68 6f 75 74 20 68 6f 6c 64 69 6e 67 without holding
5f500 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
5f510 6e 67 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 ng sqlite3.mutex
5f520 20 6d 75 74 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 mutex..**.** Th
5f530 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 6f 62 6a e memory for obj
5f540 65 63 74 73 20 6f 66 20 74 68 69 73 20 74 79 70 ects of this typ
5f550 65 20 69 73 20 61 6c 77 61 79 73 20 61 6c 6c 6f e is always allo
5f560 63 61 74 65 64 20 62 79 20 0a 2a 2a 20 73 71 6c cated by .** sql
5f570 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28 29 2c 20 ite3DbMalloc(),
5f580 75 73 69 6e 67 20 74 68 65 20 63 6f 6e 6e 65 63 using the connec
5f590 74 69 6f 6e 20 68 61 6e 64 6c 65 20 73 74 6f 72 tion handle stor
5f5a0 65 64 20 69 6e 20 56 54 61 62 6c 65 2e 64 62 20 ed in VTable.db
5f5b0 61 73 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 as .** the first
5f5c0 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 argument..*/.st
5f5d0 72 75 63 74 20 56 54 61 62 6c 65 20 7b 0a 20 20 ruct VTable {.
5f5e0 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 sqlite3 *db;
5f5f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
5f600 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
5f610 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
5f620 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 this table */.
5f630 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 20 20 Module *pMod;
5f640 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
5f650 69 6e 74 65 72 20 74 6f 20 6d 6f 64 75 6c 65 20 inter to module
5f660 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a implementation *
5f670 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 /. sqlite3_vtab
5f680 20 2a 70 56 74 61 62 3b 20 20 20 20 20 20 2f 2a *pVtab; /*
5f690 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 74 61 62 Pointer to vtab
5f6a0 20 69 6e 73 74 61 6e 63 65 20 2a 2f 0a 20 20 69 instance */. i
5f6b0 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 nt nRef;
5f6c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
5f6d0 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 er of pointers t
5f6e0 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 o this structure
5f6f0 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a 70 4e */. VTable *pN
5f700 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ext;
5f710 2f 2a 20 4e 65 78 74 20 69 6e 20 6c 69 6e 6b 65 /* Next in linke
5f720 64 20 6c 69 73 74 20 28 73 65 65 20 61 62 6f 76 d list (see abov
5f730 65 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 e) */.};../*.**
5f740 45 61 63 68 20 53 51 4c 20 74 61 62 6c 65 20 69 Each SQL table i
5f750 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e s represented in
5f760 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e 20 69 6e memory by an in
5f770 73 74 61 6e 63 65 20 6f 66 20 74 68 65 0a 2a 2a stance of the.**
5f780 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
5f790 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c ture..**.** Tabl
5f7a0 65 2e 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e e.zName is the n
5f7b0 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
5f7c0 2e 20 20 54 68 65 20 63 61 73 65 20 6f 66 20 74 . The case of t
5f7d0 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 43 he original.** C
5f7e0 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 REATE TABLE stat
5f7f0 65 6d 65 6e 74 20 69 73 20 73 74 6f 72 65 64 2c ement is stored,
5f800 20 62 75 74 20 63 61 73 65 20 69 73 20 6e 6f 74 but case is not
5f810 20 73 69 67 6e 69 66 69 63 61 6e 74 20 66 6f 72 significant for
5f820 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e .** comparisons.
5f830 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 6e 43 6f .**.** Table.nCo
5f840 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 l is the number
5f850 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
5f860 69 73 20 74 61 62 6c 65 2e 20 20 54 61 62 6c 65 is table. Table
5f870 2e 61 43 6f 6c 20 69 73 20 61 0a 2a 2a 20 70 6f .aCol is a.** po
5f880 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 inter to an arra
5f890 79 20 6f 66 20 43 6f 6c 75 6d 6e 20 73 74 72 75 y of Column stru
5f8a0 63 74 75 72 65 73 2c 20 6f 6e 65 20 66 6f 72 20 ctures, one for
5f8b0 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a each column..**.
5f8c0 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 ** If the table
5f8d0 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 has an INTEGER P
5f8e0 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e RIMARY KEY, then
5f8f0 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 Table.iPKey is
5f900 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 the index of.**
5f910 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 the column that
5f920 69 73 20 74 68 61 74 20 6b 65 79 2e 20 20 20 4f is that key. O
5f930 74 68 65 72 77 69 73 65 20 54 61 62 6c 65 2e 69 therwise Table.i
5f940 50 4b 65 79 20 69 73 20 6e 65 67 61 74 69 76 65 PKey is negative
5f950 2e 20 20 4e 6f 74 65 0a 2a 2a 20 74 68 61 74 20 . Note.** that
5f960 74 68 65 20 64 61 74 61 74 79 70 65 20 6f 66 20 the datatype of
5f970 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 the PRIMARY KEY
5f980 6d 75 73 74 20 62 65 20 49 4e 54 45 47 45 52 20 must be INTEGER
5f990 66 6f 72 20 74 68 69 73 20 66 69 65 6c 64 20 74 for this field t
5f9a0 6f 0a 2a 2a 20 62 65 20 73 65 74 2e 20 20 41 6e o.** be set. An
5f9b0 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY
5f9c0 20 4b 45 59 20 69 73 20 75 73 65 64 20 61 73 20 KEY is used as
5f9d0 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 65 61 the rowid for ea
5f9e0 63 68 20 72 6f 77 20 6f 66 0a 2a 2a 20 74 68 65 ch row of.** the
5f9f0 20 74 61 62 6c 65 2e 20 20 49 66 20 61 20 74 61 table. If a ta
5fa00 62 6c 65 20 68 61 73 20 6e 6f 20 49 4e 54 45 47 ble has no INTEG
5fa10 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY,
5fa20 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 20 72 6f then a random ro
5fa30 77 69 64 0a 2a 2a 20 69 73 20 67 65 6e 65 72 61 wid.** is genera
5fa40 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 ted for each row
5fa50 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 of the table.
5fa60 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 TF_HasPrimaryKey
5fa70 20 69 73 20 73 65 74 20 69 66 0a 2a 2a 20 74 68 is set if.** th
5fa80 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 79 20 e table has any
5fa90 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 49 4e 54 PRIMARY KEY, INT
5faa0 45 47 45 52 20 6f 72 20 6f 74 68 65 72 77 69 73 EGER or otherwis
5fab0 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 74 e..**.** Table.t
5fac0 6e 75 6d 20 69 73 20 74 68 65 20 70 61 67 65 20 num is the page
5fad0 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 number for the r
5fae0 6f 6f 74 20 42 54 72 65 65 20 70 61 67 65 20 6f oot BTree page o
5faf0 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20 74 f the table in t
5fb00 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 he.** database f
5fb10 69 6c 65 2e 20 20 49 66 20 54 61 62 6c 65 2e 69 ile. If Table.i
5fb20 44 62 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 Db is the index
5fb30 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
5fb40 74 61 62 6c 65 20 62 61 63 6b 65 6e 64 0a 2a 2a table backend.**
5fb50 20 69 6e 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d in sqlite.aDb[]
5fb60 2e 20 20 30 20 69 73 20 66 6f 72 20 74 68 65 20 . 0 is for the
5fb70 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 61 6e main database an
5fb80 64 20 31 20 69 73 20 66 6f 72 20 74 68 65 20 66 d 1 is for the f
5fb90 69 6c 65 20 74 68 61 74 0a 2a 2a 20 68 6f 6c 64 ile that.** hold
5fba0 73 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c s temporary tabl
5fbb0 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 20 es and indices.
5fbc0 20 49 66 20 54 46 5f 45 70 68 65 6d 65 72 61 6c If TF_Ephemeral
5fbd0 20 69 73 20 73 65 74 0a 2a 2a 20 74 68 65 6e 20 is set.** then
5fbe0 74 68 65 20 74 61 62 6c 65 20 69 73 20 73 74 6f the table is sto
5fbf0 72 65 64 20 69 6e 20 61 20 66 69 6c 65 20 74 68 red in a file th
5fc00 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 at is automatica
5fc10 6c 6c 79 20 64 65 6c 65 74 65 64 0a 2a 2a 20 77 lly deleted.** w
5fc20 68 65 6e 20 74 68 65 20 56 44 42 45 20 63 75 72 hen the VDBE cur
5fc30 73 6f 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65 sor to the table
5fc40 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 49 6e 20 is closed. In
5fc50 74 68 69 73 20 63 61 73 65 20 54 61 62 6c 65 2e this case Table.
5fc60 74 6e 75 6d 20 0a 2a 2a 20 72 65 66 65 72 73 20 tnum .** refers
5fc70 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 VDBE cursor numb
5fc80 65 72 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 er that holds th
5fc90 65 20 74 61 62 6c 65 20 6f 70 65 6e 2c 20 6e 6f e table open, no
5fca0 74 20 74 6f 20 74 68 65 20 72 6f 6f 74 0a 2a 2a t to the root.**
5fcb0 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 page number. T
5fcc0 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 73 20 ransient tables
5fcd0 61 72 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 are used to hold
5fce0 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 the results of
5fcf0 61 0a 2a 2a 20 73 75 62 2d 71 75 65 72 79 20 74 a.** sub-query t
5fd00 68 61 74 20 61 70 70 65 61 72 73 20 69 6e 73 74 hat appears inst
5fd10 65 61 64 20 6f 66 20 61 20 72 65 61 6c 20 74 61 ead of a real ta
5fd20 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 ble name in the
5fd30 46 52 4f 4d 20 63 6c 61 75 73 65 20 0a 2a 2a 20 FROM clause .**
5fd40 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 of a SELECT stat
5fd50 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 ement..*/.struct
5fd60 20 54 61 62 6c 65 20 7b 0a 20 20 73 71 6c 69 74 Table {. sqlit
5fd70 65 33 20 2a 64 62 4d 65 6d 3b 20 20 20 20 20 20 e3 *dbMem;
5fd80 2f 2a 20 44 42 20 63 6f 6e 6e 65 63 74 69 6f 6e /* DB connection
5fd90 20 75 73 65 64 20 66 6f 72 20 6c 6f 6f 6b 61 73 used for lookas
5fda0 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e ide allocations.
5fdb0 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d */. char *zNam
5fdc0 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 e; /* Na
5fdd0 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
5fde0 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 or view */. int
5fdf0 20 69 50 4b 65 79 3b 20 20 20 20 20 20 20 20 20 iPKey;
5fe00 20 20 2f 2a 20 49 66 20 6e 6f 74 20 6e 65 67 61 /* If not nega
5fe10 74 69 76 65 2c 20 75 73 65 20 61 43 6f 6c 5b 69 tive, use aCol[i
5fe20 50 4b 65 79 5d 20 61 73 20 74 68 65 20 70 72 69 PKey] as the pri
5fe30 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20 20 69 6e mary key */. in
5fe40 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 t nCol;
5fe50 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
5fe60 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 columns in this
5fe70 74 61 62 6c 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d table */. Colum
5fe80 6e 20 2a 61 43 6f 6c 3b 20 20 20 20 20 20 20 20 n *aCol;
5fe90 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 /* Information a
5fea0 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e bout each column
5feb0 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e */. Index *pIn
5fec0 64 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69 dex; /* Li
5fed0 73 74 20 6f 66 20 53 51 4c 20 69 6e 64 65 78 65 st of SQL indexe
5fee0 73 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e s on this table.
5fef0 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 */. int tnum;
5ff00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f /* Ro
5ff10 6f 74 20 42 54 72 65 65 20 6e 6f 64 65 20 66 6f ot BTree node fo
5ff20 72 20 74 68 69 73 20 74 61 62 6c 65 20 28 73 65 r this table (se
5ff30 65 20 6e 6f 74 65 20 61 62 6f 76 65 29 20 2a 2f e note above) */
5ff40 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 . Select *pSele
5ff50 63 74 3b 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 20 ct; /* NULL
5ff60 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 50 6f 69 for tables. Poi
5ff70 6e 74 73 20 74 6f 20 64 65 66 69 6e 69 74 69 6f nts to definitio
5ff80 6e 20 69 66 20 61 20 76 69 65 77 2e 20 2a 2f 0a n if a view. */.
5ff90 20 20 75 31 36 20 6e 52 65 66 3b 20 20 20 20 20 u16 nRef;
5ffa0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
5ffb0 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 of pointers to
5ffc0 74 68 69 73 20 54 61 62 6c 65 20 2a 2f 0a 20 20 this Table */.
5ffd0 75 38 20 74 61 62 46 6c 61 67 73 3b 20 20 20 20 u8 tabFlags;
5ffe0 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 /* Mask of
5fff0 54 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 TF_* values */.
60000 20 75 38 20 6b 65 79 43 6f 6e 66 3b 20 20 20 20 u8 keyConf;
60010 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f /* What to
60020 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 75 do in case of u
60030 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 niqueness confli
60040 63 74 20 6f 6e 20 69 50 4b 65 79 20 2a 2f 0a 20 ct on iPKey */.
60050 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20 FKey *pFKey;
60060 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 /* Linked
60070 6c 69 73 74 20 6f 66 20 61 6c 6c 20 66 6f 72 65 list of all fore
60080 69 67 6e 20 6b 65 79 73 20 69 6e 20 74 68 69 73 ign keys in this
60090 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 table */. char
600a0 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 20 20 20 *zColAff;
600b0 20 2f 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e /* String defin
600c0 69 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 ing the affinity
600d0 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 of each column
600e0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
600f0 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 45 E_OMIT_CHECK. E
60100 78 70 72 20 2a 70 43 68 65 63 6b 3b 20 20 20 20 xpr *pCheck;
60110 20 20 20 20 2f 2a 20 54 68 65 20 41 4e 44 20 6f /* The AND o
60120 66 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 f all CHECK cons
60130 74 72 61 69 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 traints */.#endi
60140 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 f.#ifndef SQLITE
60150 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 _OMIT_ALTERTABLE
60160 0a 20 20 69 6e 74 20 61 64 64 43 6f 6c 4f 66 66 . int addColOff
60170 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 set; /* Offse
60180 74 20 69 6e 20 43 52 45 41 54 45 20 54 41 42 4c t in CREATE TABL
60190 45 20 73 74 6d 74 20 74 6f 20 61 64 64 20 61 20 E stmt to add a
601a0 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65 new column */.#e
601b0 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c ndif.#ifndef SQL
601c0 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
601d0 54 41 42 4c 45 0a 20 20 56 54 61 62 6c 65 20 2a TABLE. VTable *
601e0 70 56 54 61 62 6c 65 3b 20 20 20 20 20 2f 2a 20 pVTable; /*
601f0 4c 69 73 74 20 6f 66 20 56 54 61 62 6c 65 20 6f List of VTable o
60200 62 6a 65 63 74 73 2e 20 2a 2f 0a 20 20 69 6e 74 bjects. */. int
60210 20 6e 4d 6f 64 75 6c 65 41 72 67 3b 20 20 20 20 nModuleArg;
60220 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
60230 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 rguments to the
60240 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 68 61 72 module */. char
60250 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 20 **azModuleArg;
60260 20 2f 2a 20 54 65 78 74 20 6f 66 20 61 6c 6c 20 /* Text of all
60270 6d 6f 64 75 6c 65 20 61 72 67 73 2e 20 5b 30 5d module args. [0]
60280 20 69 73 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 is module name
60290 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 54 72 69 67 */.#endif. Trig
602a0 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20 ger *pTrigger;
602b0 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67 /* List of trig
602c0 67 65 72 73 20 73 74 6f 72 65 64 20 69 6e 20 70 gers stored in p
602d0 53 63 68 65 6d 61 20 2a 2f 0a 20 20 53 63 68 65 Schema */. Sche
602e0 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 ma *pSchema;
602f0 20 2f 2a 20 53 63 68 65 6d 61 20 74 68 61 74 20 /* Schema that
60300 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 74 61 contains this ta
60310 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a ble */. Table *
60320 70 4e 65 78 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a pNextZombie; /*
60330 20 4e 65 78 74 20 6f 6e 20 74 68 65 20 50 61 72 Next on the Par
60340 73 65 2e 70 5a 6f 6d 62 69 65 54 61 62 20 6c 69 se.pZombieTab li
60350 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 st */.};../*.**
60360 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 Allowed values f
60370 6f 72 20 54 61 62 65 2e 74 61 62 46 6c 61 67 73 or Tabe.tabFlags
60380 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f ..*/.#define TF_
60390 52 65 61 64 6f 6e 6c 79 20 20 20 20 20 20 20 20 Readonly
603a0 30 78 30 31 20 20 20 20 2f 2a 20 52 65 61 64 2d 0x01 /* Read-
603b0 6f 6e 6c 79 20 73 79 73 74 65 6d 20 74 61 62 6c only system tabl
603c0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f e */.#define TF_
603d0 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 Ephemeral
603e0 30 78 30 32 20 20 20 20 2f 2a 20 41 6e 20 65 70 0x02 /* An ep
603f0 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 2a 2f hemeral table */
60400 0a 23 64 65 66 69 6e 65 20 54 46 5f 48 61 73 50 .#define TF_HasP
60410 72 69 6d 61 72 79 4b 65 79 20 20 20 30 78 30 34 rimaryKey 0x04
60420 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 /* Table has
60430 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a a primary key *
60440 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 41 75 74 /.#define TF_Aut
60450 6f 69 6e 63 72 65 6d 65 6e 74 20 20 20 30 78 30 oincrement 0x0
60460 38 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 8 /* Integer
60470 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20 61 primary key is a
60480 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a utoincrement */.
60490 23 64 65 66 69 6e 65 20 54 46 5f 56 69 72 74 75 #define TF_Virtu
604a0 61 6c 20 20 20 20 20 20 20 20 20 30 78 31 30 20 al 0x10
604b0 20 20 20 2f 2a 20 49 73 20 61 20 76 69 72 74 75 /* Is a virtu
604c0 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 al table */.#def
604d0 69 6e 65 20 54 46 5f 4e 65 65 64 4d 65 74 61 64 ine TF_NeedMetad
604e0 61 74 61 20 20 20 20 30 78 32 30 20 20 20 20 2f ata 0x20 /
604f0 2a 20 61 43 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61 * aCol[].zType a
60500 6e 64 20 61 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20 nd aCol[].pColl
60510 6d 69 73 73 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a missing */..../*
60520 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73 65 65 20 .** Test to see
60530 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 whether or not a
60540 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 74 table is a virt
60550 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73 ual table. This
60560 20 69 73 0a 2a 2a 20 64 6f 6e 65 20 61 73 20 61 is.** done as a
60570 20 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69 macro so that i
60580 74 20 77 69 6c 6c 20 62 65 20 6f 70 74 69 6d 69 t will be optimi
60590 7a 65 64 20 6f 75 74 20 77 68 65 6e 20 76 69 72 zed out when vir
605a0 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 73 75 tual.** table su
605b0 70 70 6f 72 74 20 69 73 20 6f 6d 69 74 74 65 64 pport is omitted
605c0 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e from the build.
605d0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
605e0 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
605f0 41 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 49 ABLE.# define I
60600 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20 sVirtual(X)
60610 20 28 28 28 58 29 2d 3e 74 61 62 46 6c 61 67 73 (((X)->tabFlags
60620 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d & TF_Virtual)!=
60630 30 29 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48 0).# define IsH
60640 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 28 iddenColumn(X) (
60650 28 58 29 2d 3e 69 73 48 69 64 64 65 6e 29 0a 23 (X)->isHidden).#
60660 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 else.# define I
60670 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20 sVirtual(X)
60680 20 30 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48 0.# define IsH
60690 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 30 iddenColumn(X) 0
606a0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 .#endif../*.** E
606b0 61 63 68 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 ach foreign key
606c0 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 61 6e constraint is an
606d0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
606e0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
606f0 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f ture..**.** A fo
60700 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61 73 73 reign key is ass
60710 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 77 6f ociated with two
60720 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 22 66 tables. The "f
60730 72 6f 6d 22 20 74 61 62 6c 65 20 69 73 0a 2a 2a rom" table is.**
60740 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 the table that
60750 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 52 45 46 contains the REF
60760 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 74 ERENCES clause t
60770 68 61 74 20 63 72 65 61 74 65 73 20 74 68 65 20 hat creates the
60780 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20 foreign.** key.
60790 20 54 68 65 20 22 74 6f 22 20 74 61 62 6c 65 20 The "to" table
607a0 69 73 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 is the table tha
607b0 74 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68 t is named in th
607c0 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 e REFERENCES cla
607d0 75 73 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 use..** Consider
607e0 20 74 68 69 73 20 65 78 61 6d 70 6c 65 3a 0a 2a this example:.*
607f0 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 *.** CREATE
60800 54 41 42 4c 45 20 65 78 31 28 0a 2a 2a 20 20 20 TABLE ex1(.**
60810 20 20 20 20 61 20 49 4e 54 45 47 45 52 20 50 52 a INTEGER PR
60820 49 4d 41 52 59 20 4b 45 59 2c 0a 2a 2a 20 20 20 IMARY KEY,.**
60830 20 20 20 20 62 20 49 4e 54 45 47 45 52 20 43 4f b INTEGER CO
60840 4e 53 54 52 41 49 4e 54 20 66 6b 31 20 52 45 46 NSTRAINT fk1 REF
60850 45 52 45 4e 43 45 53 20 65 78 32 28 78 29 0a 2a ERENCES ex2(x).*
60860 2a 20 20 20 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46 * );.**.** F
60870 6f 72 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 22 or foreign key "
60880 66 6b 31 22 2c 20 74 68 65 20 66 72 6f 6d 2d 74 fk1", the from-t
60890 61 62 6c 65 20 69 73 20 22 65 78 31 22 20 61 6e able is "ex1" an
608a0 64 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 d the to-table i
608b0 73 20 22 65 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45 s "ex2"..**.** E
608c0 61 63 68 20 52 45 46 45 52 45 4e 43 45 53 20 63 ach REFERENCES c
608d0 6c 61 75 73 65 20 67 65 6e 65 72 61 74 65 73 20 lause generates
608e0 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
608f0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
60900 75 63 74 75 72 65 0a 2a 2a 20 77 68 69 63 68 20 ucture.** which
60910 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 is attached to t
60920 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 2e 20 20 he from-table.
60930 54 68 65 20 74 6f 2d 74 61 62 6c 65 20 6e 65 65 The to-table nee
60940 64 20 6e 6f 74 20 65 78 69 73 74 20 77 68 65 6e d not exist when
60950 0a 2a 2a 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 .** the from-tab
60960 6c 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 le is created.
60970 54 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 The existence of
60980 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 the to-table is
60990 20 6e 6f 74 20 63 68 65 63 6b 65 64 2e 0a 2a 2f not checked..*/
609a0 0a 73 74 72 75 63 74 20 46 4b 65 79 20 7b 0a 20 .struct FKey {.
609b0 20 54 61 62 6c 65 20 2a 70 46 72 6f 6d 3b 20 20 Table *pFrom;
609c0 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 /* The table
609d0 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
609e0 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 e REFERENCES cla
609f0 75 73 65 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 use */. FKey *p
60a00 4e 65 78 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 NextFrom; /* Ne
60a10 78 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 xt foreign key i
60a20 6e 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 n pFrom */. cha
60a30 72 20 2a 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f r *zTo; /
60a40 2a 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 * Name of table
60a50 74 68 61 74 20 74 68 65 20 6b 65 79 20 70 6f 69 that the key poi
60a60 6e 74 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 nts to */. int
60a70 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a nCol; /*
60a80 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d Number of colum
60a90 6e 73 20 69 6e 20 74 68 69 73 20 6b 65 79 20 2a ns in this key *
60aa0 2f 0a 20 20 75 38 20 69 73 44 65 66 65 72 72 65 /. u8 isDeferre
60ab0 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 d; /* True if
60ac0 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 constraint chec
60ad0 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 king is deferred
60ae0 20 74 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f 0a till COMMIT */.
60af0 20 20 75 38 20 75 70 64 61 74 65 43 6f 6e 66 3b u8 updateConf;
60b00 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 /* How to re
60b10 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 solve conflicts
60b20 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 55 50 that occur on UP
60b30 44 41 54 45 20 2a 2f 0a 20 20 75 38 20 64 65 6c DATE */. u8 del
60b40 65 74 65 43 6f 6e 66 3b 20 20 20 20 2f 2a 20 48 eteConf; /* H
60b50 6f 77 20 74 6f 20 72 65 73 6f 6c 76 65 20 63 6f ow to resolve co
60b60 6e 66 6c 69 63 74 73 20 74 68 61 74 20 6f 63 63 nflicts that occ
60b70 75 72 20 6f 6e 20 44 45 4c 45 54 45 20 2a 2f 0a ur on DELETE */.
60b80 20 20 75 38 20 69 6e 73 65 72 74 43 6f 6e 66 3b u8 insertConf;
60b90 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 /* How to re
60ba0 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 solve conflicts
60bb0 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 49 4e that occur on IN
60bc0 53 45 52 54 20 2a 2f 0a 20 20 73 74 72 75 63 74 SERT */. struct
60bd0 20 73 43 6f 6c 4d 61 70 20 7b 20 20 2f 2a 20 4d sColMap { /* M
60be0 61 70 70 69 6e 67 20 6f 66 20 63 6f 6c 75 6d 6e apping of column
60bf0 73 20 69 6e 20 70 46 72 6f 6d 20 74 6f 20 63 6f s in pFrom to co
60c00 6c 75 6d 6e 73 20 69 6e 20 7a 54 6f 20 2a 2f 0a lumns in zTo */.
60c10 20 20 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20 int iFrom;
60c20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
60c30 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 46 72 of column in pFr
60c40 6f 6d 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a om */. char *
60c50 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 zCol; /*
60c60 4e 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 Name of column i
60c70 6e 20 7a 54 6f 2e 20 20 49 66 20 30 20 75 73 65 n zTo. If 0 use
60c80 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a PRIMARY KEY */.
60c90 20 20 7d 20 61 43 6f 6c 5b 31 5d 3b 20 20 20 20 } aCol[1];
60ca0 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 /* One entry
60cb0 20 66 6f 72 20 65 61 63 68 20 6f 66 20 6e 43 6f for each of nCo
60cc0 6c 20 63 6f 6c 75 6d 6e 20 73 20 2a 2f 0a 7d 3b l column s */.};
60cd0 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 73 ../*.** SQLite s
60ce0 75 70 70 6f 72 74 73 20 6d 61 6e 79 20 64 69 66 upports many dif
60cf0 66 65 72 65 6e 74 20 77 61 79 73 20 74 6f 20 72 ferent ways to r
60d00 65 73 6f 6c 76 65 20 61 20 63 6f 6e 73 74 72 61 esolve a constra
60d10 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 2e 20 20 52 int.** error. R
60d20 4f 4c 4c 42 41 43 4b 20 70 72 6f 63 65 73 73 69 OLLBACK processi
60d30 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 ng means that a
60d40 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 constraint viola
60d50 74 69 6f 6e 0a 2a 2a 20 63 61 75 73 65 73 20 74 tion.** causes t
60d60 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20 he operation in
60d70 70 72 6f 63 65 73 73 20 74 6f 20 66 61 69 6c 20 process to fail
60d80 61 6e 64 20 66 6f 72 20 74 68 65 20 63 75 72 72 and for the curr
60d90 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a ent transaction.
60da0 2a 2a 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 ** to be rolled
60db0 62 61 63 6b 2e 20 20 41 42 4f 52 54 20 70 72 6f back. ABORT pro
60dc0 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 cessing means th
60dd0 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20 70 e operation in p
60de0 72 6f 63 65 73 73 0a 2a 2a 20 66 61 69 6c 73 20 rocess.** fails
60df0 61 6e 64 20 61 6e 79 20 70 72 69 6f 72 20 63 68 and any prior ch
60e00 61 6e 67 65 73 20 66 72 6f 6d 20 74 68 61 74 20 anges from that
60e10 6f 6e 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 one operation ar
60e20 65 20 62 61 63 6b 65 64 20 6f 75 74 2c 0a 2a 2a e backed out,.**
60e30 20 62 75 74 20 74 68 65 20 74 72 61 6e 73 61 63 but the transac
60e40 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 6f 6c 6c tion is not roll
60e50 65 64 20 62 61 63 6b 2e 20 20 46 41 49 4c 20 70 ed back. FAIL p
60e60 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 20 rocessing means
60e70 74 68 61 74 0a 2a 2a 20 74 68 65 20 6f 70 65 72 that.** the oper
60e80 61 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 ation in progres
60e90 73 20 73 74 6f 70 73 20 61 6e 64 20 72 65 74 75 s stops and retu
60ea0 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 rns an error cod
60eb0 65 2e 20 20 42 75 74 20 70 72 69 6f 72 0a 2a 2a e. But prior.**
60ec0 20 63 68 61 6e 67 65 73 20 64 75 65 20 74 6f 20 changes due to
60ed0 74 68 65 20 73 61 6d 65 20 6f 70 65 72 61 74 69 the same operati
60ee0 6f 6e 20 61 72 65 20 6e 6f 74 20 62 61 63 6b 65 on are not backe
60ef0 64 20 6f 75 74 20 61 6e 64 20 6e 6f 20 72 6f 6c d out and no rol
60f00 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e lback.** occurs.
60f10 20 20 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20 74 IGNORE means t
60f20 68 61 74 20 74 68 65 20 70 61 72 74 69 63 75 6c hat the particul
60f30 61 72 20 72 6f 77 20 74 68 61 74 20 63 61 75 73 ar row that caus
60f40 65 64 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e ed the constrain
60f50 74 0a 2a 2a 20 65 72 72 6f 72 20 69 73 20 6e 6f t.** error is no
60f60 74 20 69 6e 73 65 72 74 65 64 20 6f 72 20 75 70 t inserted or up
60f70 64 61 74 65 64 2e 20 20 50 72 6f 63 65 73 73 69 dated. Processi
60f80 6e 67 20 63 6f 6e 74 69 6e 75 65 73 20 61 6e 64 ng continues and
60f90 20 6e 6f 20 65 72 72 6f 72 0a 2a 2a 20 69 73 20 no error.** is
60fa0 72 65 74 75 72 6e 65 64 2e 20 20 52 45 50 4c 41 returned. REPLA
60fb0 43 45 20 6d 65 61 6e 73 20 74 68 61 74 20 70 72 CE means that pr
60fc0 65 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 eexisting databa
60fd0 73 65 20 72 6f 77 73 20 74 68 61 74 20 63 61 75 se rows that cau
60fe0 73 65 64 0a 2a 2a 20 61 20 55 4e 49 51 55 45 20 sed.** a UNIQUE
60ff0 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 constraint viola
61000 74 69 6f 6e 20 61 72 65 20 72 65 6d 6f 76 65 64 tion are removed
61010 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 77 so that the new
61020 20 69 6e 73 65 72 74 20 6f 72 0a 2a 2a 20 75 70 insert or.** up
61030 64 61 74 65 20 63 61 6e 20 70 72 6f 63 65 65 64 date can proceed
61040 2e 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f . Processing co
61050 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 ntinues and no e
61060 72 72 6f 72 20 69 73 20 72 65 70 6f 72 74 65 64 rror is reported
61070 2e 0a 2a 2a 0a 2a 2a 20 52 45 53 54 52 49 43 54 ..**.** RESTRICT
61080 2c 20 53 45 54 4e 55 4c 4c 2c 20 61 6e 64 20 43 , SETNULL, and C
61090 41 53 43 41 44 45 20 61 63 74 69 6f 6e 73 20 61 ASCADE actions a
610a0 70 70 6c 79 20 6f 6e 6c 79 20 74 6f 20 66 6f 72 pply only to for
610b0 65 69 67 6e 20 6b 65 79 73 2e 0a 2a 2a 20 52 45 eign keys..** RE
610c0 53 54 52 49 43 54 20 69 73 20 74 68 65 20 73 61 STRICT is the sa
610d0 6d 65 20 61 73 20 41 42 4f 52 54 20 66 6f 72 20 me as ABORT for
610e0 49 4d 4d 45 44 49 41 54 45 20 66 6f 72 65 69 67 IMMEDIATE foreig
610f0 6e 20 6b 65 79 73 20 61 6e 64 20 74 68 65 0a 2a n keys and the.*
61100 2a 20 73 61 6d 65 20 61 73 20 52 4f 4c 4c 42 41 * same as ROLLBA
61110 43 4b 20 66 6f 72 20 44 45 46 45 52 52 45 44 20 CK for DEFERRED
61120 6b 65 79 73 2e 20 20 53 45 54 4e 55 4c 4c 20 6d keys. SETNULL m
61130 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 66 6f eans that the fo
61140 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 20 69 73 20 reign.** key is
61150 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 20 43 41 set to NULL. CA
61160 53 43 41 44 45 20 6d 65 61 6e 73 20 74 68 61 74 SCADE means that
61170 20 61 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44 a DELETE or UPD
61180 41 54 45 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 ATE of the.** re
61190 66 65 72 65 6e 63 65 64 20 74 61 62 6c 65 20 72 ferenced table r
611a0 6f 77 20 69 73 20 70 72 6f 70 61 67 61 74 65 64 ow is propagated
611b0 20 69 6e 74 6f 20 74 68 65 20 72 6f 77 20 74 68 into the row th
611c0 61 74 20 68 6f 6c 64 73 20 74 68 65 0a 2a 2a 20 at holds the.**
611d0 66 6f 72 65 69 67 6e 20 6b 65 79 2e 0a 2a 2a 20 foreign key..**
611e0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
611f0 67 20 73 79 6d 62 6f 6c 69 63 20 76 61 6c 75 65 g symbolic value
61200 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 72 65 s are used to re
61210 63 6f 72 64 20 77 68 69 63 68 20 74 79 70 65 0a cord which type.
61220 2a 2a 20 6f 66 20 61 63 74 69 6f 6e 20 74 6f 20 ** of action to
61230 74 61 6b 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 take..*/.#define
61240 20 4f 45 5f 4e 6f 6e 65 20 20 20 20 20 30 20 20 OE_None 0
61250 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 /* There is no
61260 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f 20 63 68 constraint to ch
61270 65 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f eck */.#define O
61280 45 5f 52 6f 6c 6c 62 61 63 6b 20 31 20 20 20 2f E_Rollback 1 /
61290 2a 20 46 61 69 6c 20 74 68 65 20 6f 70 65 72 61 * Fail the opera
612a0 74 69 6f 6e 20 61 6e 64 20 72 6f 6c 6c 62 61 63 tion and rollbac
612b0 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f k the transactio
612c0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f n */.#define OE_
612d0 41 62 6f 72 74 20 20 20 20 32 20 20 20 2f 2a 20 Abort 2 /*
612e0 42 61 63 6b 20 6f 75 74 20 63 68 61 6e 67 65 73 Back out changes
612f0 20 62 75 74 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 but do no rollb
61300 61 63 6b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ack transaction
61310 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 46 61 */.#define OE_Fa
61320 69 6c 20 20 20 20 20 33 20 20 20 2f 2a 20 53 74 il 3 /* St
61330 6f 70 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e op the operation
61340 20 62 75 74 20 6c 65 61 76 65 20 61 6c 6c 20 70 but leave all p
61350 72 69 6f 72 20 63 68 61 6e 67 65 73 20 2a 2f 0a rior changes */.
61360 23 64 65 66 69 6e 65 20 4f 45 5f 49 67 6e 6f 72 #define OE_Ignor
61370 65 20 20 20 34 20 20 20 2f 2a 20 49 67 6e 6f 72 e 4 /* Ignor
61380 65 20 74 68 65 20 65 72 72 6f 72 2e 20 44 6f 20 e the error. Do
61390 6e 6f 74 20 64 6f 20 74 68 65 20 49 4e 53 45 52 not do the INSER
613a0 54 20 6f 72 20 55 50 44 41 54 45 20 2a 2f 0a 23 T or UPDATE */.#
613b0 64 65 66 69 6e 65 20 4f 45 5f 52 65 70 6c 61 63 define OE_Replac
613c0 65 20 20 35 20 20 20 2f 2a 20 44 65 6c 65 74 65 e 5 /* Delete
613d0 20 65 78 69 73 74 69 6e 67 20 72 65 63 6f 72 64 existing record
613e0 2c 20 74 68 65 6e 20 64 6f 20 49 4e 53 45 52 54 , then do INSERT
613f0 20 6f 72 20 55 50 44 41 54 45 20 2a 2f 0a 0a 23 or UPDATE */..#
61400 64 65 66 69 6e 65 20 4f 45 5f 52 65 73 74 72 69 define OE_Restri
61410 63 74 20 36 20 20 20 2f 2a 20 4f 45 5f 41 62 6f ct 6 /* OE_Abo
61420 72 74 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45 rt for IMMEDIATE
61430 2c 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 66 6f , OE_Rollback fo
61440 72 20 44 45 46 45 52 52 45 44 20 2a 2f 0a 23 64 r DEFERRED */.#d
61450 65 66 69 6e 65 20 4f 45 5f 53 65 74 4e 75 6c 6c efine OE_SetNull
61460 20 20 37 20 20 20 2f 2a 20 53 65 74 20 74 68 65 7 /* Set the
61470 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 61 6c foreign key val
61480 75 65 20 74 6f 20 4e 55 4c 4c 20 2a 2f 0a 23 64 ue to NULL */.#d
61490 65 66 69 6e 65 20 4f 45 5f 53 65 74 44 66 6c 74 efine OE_SetDflt
614a0 20 20 38 20 20 20 2f 2a 20 53 65 74 20 74 68 65 8 /* Set the
614b0 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 61 6c foreign key val
614c0 75 65 20 74 6f 20 69 74 73 20 64 65 66 61 75 6c ue to its defaul
614d0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f t */.#define OE_
614e0 43 61 73 63 61 64 65 20 20 39 20 20 20 2f 2a 20 Cascade 9 /*
614f0 43 61 73 63 61 64 65 20 74 68 65 20 63 68 61 6e Cascade the chan
61500 67 65 73 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 ges */..#define
61510 4f 45 5f 44 65 66 61 75 6c 74 20 20 39 39 20 20 OE_Default 99
61520 2f 2a 20 44 6f 20 77 68 61 74 65 76 65 72 20 74 /* Do whatever t
61530 68 65 20 64 65 66 61 75 6c 74 20 61 63 74 69 6f he default actio
61540 6e 20 69 73 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 n is */.../*.**
61550 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
61560 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
61570 75 63 74 75 72 65 20 69 73 20 70 61 73 73 65 64 ucture is passed
61580 20 61 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a as the first.**
61590 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c argument to sql
615a0 69 74 65 33 56 64 62 65 4b 65 79 43 6f 6d 70 61 ite3VdbeKeyCompa
615b0 72 65 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 re and is used t
615c0 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 0a 2a o control the .*
615d0 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 * comparison of
615e0 74 68 65 20 74 77 6f 20 69 6e 64 65 78 20 6b 65 the two index ke
615f0 79 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4b 65 ys..*/.struct Ke
61600 79 49 6e 66 6f 20 7b 0a 20 20 73 71 6c 69 74 65 yInfo {. sqlite
61610 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 3 *db; /*
61620 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
61630 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 nnection */. u8
61640 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 enc;
61650 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 /* Text encodi
61660 6e 67 20 2d 20 6f 6e 65 20 6f 66 20 74 68 65 20 ng - one of the
61670 54 45 58 54 5f 55 74 66 2a 20 76 61 6c 75 65 73 TEXT_Utf* values
61680 20 2a 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 */. u16 nField
61690 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ; /* Num
616a0 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
616b0 6e 20 61 43 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 n aColl[] */. u
616c0 38 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 8 *aSortOrder;
616d0 20 20 20 2f 2a 20 49 66 20 64 65 66 69 6e 65 64 /* If defined
616e0 20 61 6e 20 61 53 6f 72 74 4f 72 64 65 72 5b 69 an aSortOrder[i
616f0 5d 20 69 73 20 74 72 75 65 2c 20 73 6f 72 74 20 ] is true, sort
61700 44 45 53 43 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 DESC */. CollSe
61710 71 20 2a 61 43 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a q *aColl[1]; /*
61720 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 Collating seque
61730 6e 63 65 20 66 6f 72 20 65 61 63 68 20 74 65 72 nce for each ter
61740 6d 20 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a m of the key */.
61750 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 };../*.** An ins
61760 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
61770 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
61780 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 holds informati
61790 6f 6e 20 61 62 6f 75 74 20 61 0a 2a 2a 20 73 69 on about a.** si
617a0 6e 67 6c 65 20 69 6e 64 65 78 20 72 65 63 6f 72 ngle index recor
617b0 64 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 d that has alrea
617c0 64 79 20 62 65 65 6e 20 70 61 72 73 65 64 20 6f dy been parsed o
617d0 75 74 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 75 ut into individu
617e0 61 6c 0a 2a 2a 20 76 61 6c 75 65 73 2e 0a 2a 2a al.** values..**
617f0 0a 2a 2a 20 41 20 72 65 63 6f 72 64 20 69 73 20 .** A record is
61800 61 6e 20 6f 62 6a 65 63 74 20 74 68 61 74 20 63 an object that c
61810 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d ontains one or m
61820 6f 72 65 20 66 69 65 6c 64 73 20 6f 66 20 64 61 ore fields of da
61830 74 61 2e 0a 2a 2a 20 52 65 63 6f 72 64 73 20 61 ta..** Records a
61840 72 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 re used to store
61850 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 the content of
61860 61 20 74 61 62 6c 65 20 72 6f 77 20 61 6e 64 20 a table row and
61870 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 68 65 20 to store.** the
61880 6b 65 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 2e key of an index.
61890 20 20 41 20 62 6c 6f 62 20 65 6e 63 6f 64 69 6e A blob encodin
618a0 67 20 6f 66 20 61 20 72 65 63 6f 72 64 20 69 73 g of a record is
618b0 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74 created by.** t
618c0 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 he OP_MakeRecord
618d0 20 6f 70 63 6f 64 65 20 6f 66 20 74 68 65 20 56 opcode of the V
618e0 44 42 45 20 61 6e 64 20 69 73 20 64 69 73 61 73 DBE and is disas
618f0 73 65 6d 62 6c 65 64 20 62 79 20 74 68 65 0a 2a sembled by the.*
61900 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f * OP_Column opco
61910 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 de..**.** This s
61920 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 61 tructure holds a
61930 20 72 65 63 6f 72 64 20 74 68 61 74 20 68 61 73 record that has
61940 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 64 69 already been di
61950 73 61 73 73 65 6d 62 6c 65 64 0a 2a 2a 20 69 6e sassembled.** in
61960 74 6f 20 69 74 73 20 63 6f 6e 73 74 69 74 75 65 to its constitue
61970 6e 74 20 66 69 65 6c 64 73 2e 0a 2a 2f 0a 73 74 nt fields..*/.st
61980 72 75 63 74 20 55 6e 70 61 63 6b 65 64 52 65 63 ruct UnpackedRec
61990 6f 72 64 20 7b 0a 20 20 4b 65 79 49 6e 66 6f 20 ord {. KeyInfo
619a0 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 2f 2a 20 43 *pKeyInfo; /* C
619b0 6f 6c 6c 61 74 69 6f 6e 20 61 6e 64 20 73 6f 72 ollation and sor
619c0 74 2d 6f 72 64 65 72 20 69 6e 66 6f 72 6d 61 74 t-order informat
619d0 69 6f 6e 20 2a 2f 0a 20 20 75 31 36 20 6e 46 69 ion */. u16 nFi
619e0 65 6c 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 eld; /*
619f0 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
61a00 73 20 69 6e 20 61 70 4d 65 6d 5b 5d 20 2a 2f 0a s in apMem[] */.
61a10 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 u16 flags;
61a20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e /* Boolean
61a30 20 73 65 74 74 69 6e 67 73 2e 20 20 55 4e 50 41 settings. UNPA
61a40 43 4b 45 44 5f 2e 2e 2e 20 62 65 6c 6f 77 20 2a CKED_... below *
61a50 2f 0a 20 20 69 36 34 20 72 6f 77 69 64 3b 20 20 /. i64 rowid;
61a60 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 /* Used
61a70 62 79 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 by UNPACKED_PREF
61a80 49 58 5f 53 45 41 52 43 48 20 2a 2f 0a 20 20 4d IX_SEARCH */. M
61a90 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 em *aMem;
61aa0 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 2a 2f 0a /* Values */.
61ab0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 };../*.** Allowe
61ac0 64 20 76 61 6c 75 65 73 20 6f 66 20 55 6e 70 61 d values of Unpa
61ad0 63 6b 65 64 52 65 63 6f 72 64 2e 66 6c 61 67 73 ckedRecord.flags
61ae0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 .*/.#define UNPA
61af0 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 20 CKED_NEED_FREE
61b00 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4d 65 0x0001 /* Me
61b10 6d 6f 72 79 20 69 73 20 66 72 6f 6d 20 73 71 6c mory is from sql
61b20 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a ite3Malloc() */.
61b30 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 #define UNPACKED
61b40 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 20 30 _NEED_DESTROY 0
61b50 78 30 30 30 32 20 20 2f 2a 20 61 70 4d 65 6d 5b x0002 /* apMem[
61b60 5d 73 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 ]s should all be
61b70 20 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 23 64 destroyed */.#d
61b80 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 49 efine UNPACKED_I
61b90 47 4e 4f 52 45 5f 52 4f 57 49 44 20 20 30 78 30 GNORE_ROWID 0x0
61ba0 30 30 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 004 /* Ignore t
61bb0 72 61 69 6c 69 6e 67 20 72 6f 77 69 64 20 6f 6e railing rowid on
61bc0 20 6b 65 79 31 20 2a 2f 0a 23 64 65 66 69 6e 65 key1 */.#define
61bd0 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 UNPACKED_INCRKE
61be0 59 20 20 20 20 20 20 20 30 78 30 30 30 38 20 20 Y 0x0008
61bf0 2f 2a 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79 /* Make this key
61c00 20 61 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67 an epsilon larg
61c10 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e er */.#define UN
61c20 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 PACKED_PREFIX_MA
61c30 54 43 48 20 20 30 78 30 30 31 30 20 20 2f 2a 20 TCH 0x0010 /*
61c40 41 20 70 72 65 66 69 78 20 6d 61 74 63 68 20 69 A prefix match i
61c50 73 20 63 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 s considered OK
61c60 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 */.#define UNPAC
61c70 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 KED_PREFIX_SEARC
61c80 48 20 30 78 30 30 32 30 20 20 2f 2a 20 41 20 70 H 0x0020 /* A p
61c90 72 65 66 69 78 20 6d 61 74 63 68 20 69 73 20 63 refix match is c
61ca0 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a onsidered OK */.
61cb0 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 ./*.** Each SQL
61cc0 69 6e 64 65 78 20 69 73 20 72 65 70 72 65 73 65 index is represe
61cd0 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62 nted in memory b
61ce0 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 y an.** instance
61cf0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
61d00 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a g structure..**.
61d10 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f ** The columns o
61d20 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 f the table that
61d30 20 61 72 65 20 74 6f 20 62 65 20 69 6e 64 65 78 are to be index
61d40 65 64 20 61 72 65 20 64 65 73 63 72 69 62 65 64 ed are described
61d50 0a 2a 2a 20 62 79 20 74 68 65 20 61 69 43 6f 6c .** by the aiCol
61d60 75 6d 6e 5b 5d 20 66 69 65 6c 64 20 6f 66 20 74 umn[] field of t
61d70 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 his structure.
61d80 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73 75 70 For example, sup
61d90 70 6f 73 65 0a 2a 2a 20 77 65 20 68 61 76 65 20 pose.** we have
61da0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 the following ta
61db0 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 3a 0a 2a ble and index:.*
61dc0 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 *.** CREATE
61dd0 54 41 42 4c 45 20 45 78 31 28 63 31 20 69 6e 74 TABLE Ex1(c1 int
61de0 2c 20 63 32 20 69 6e 74 2c 20 63 33 20 74 65 78 , c2 int, c3 tex
61df0 74 29 3b 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 t);.** CREAT
61e00 45 20 49 4e 44 45 58 20 45 78 32 20 4f 4e 20 45 E INDEX Ex2 ON E
61e10 78 31 28 63 33 2c 63 31 29 3b 0a 2a 2a 0a 2a 2a x1(c3,c1);.**.**
61e20 20 49 6e 20 74 68 65 20 54 61 62 6c 65 20 73 74 In the Table st
61e30 72 75 63 74 75 72 65 20 64 65 73 63 72 69 62 69 ructure describi
61e40 6e 67 20 45 78 31 2c 20 6e 43 6f 6c 3d 3d 33 20 ng Ex1, nCol==3
61e50 62 65 63 61 75 73 65 20 74 68 65 72 65 20 61 72 because there ar
61e60 65 0a 2a 2a 20 74 68 72 65 65 20 63 6f 6c 75 6d e.** three colum
61e70 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e ns in the table.
61e80 20 20 49 6e 20 74 68 65 20 49 6e 64 65 78 20 73 In the Index s
61e90 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69 62 tructure describ
61ea0 69 6e 67 0a 2a 2a 20 45 78 32 2c 20 6e 43 6f 6c ing.** Ex2, nCol
61eb0 75 6d 6e 3d 3d 32 20 73 69 6e 63 65 20 32 20 6f umn==2 since 2 o
61ec0 66 20 74 68 65 20 33 20 63 6f 6c 75 6d 6e 73 20 f the 3 columns
61ed0 6f 66 20 45 78 31 20 61 72 65 20 69 6e 64 65 78 of Ex1 are index
61ee0 65 64 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 ed..** The value
61ef0 20 6f 66 20 61 69 43 6f 6c 75 6d 6e 20 69 73 20 of aiColumn is
61f00 7b 32 2c 20 30 7d 2e 20 20 61 69 43 6f 6c 75 6d {2, 0}. aiColum
61f10 6e 5b 30 5d 3d 3d 32 20 62 65 63 61 75 73 65 20 n[0]==2 because
61f20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 63 6f the .** first co
61f30 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64 65 78 lumn to be index
61f40 65 64 20 28 63 33 29 20 68 61 73 20 61 6e 20 69 ed (c3) has an i
61f50 6e 64 65 78 20 6f 66 20 32 20 69 6e 20 45 78 31 ndex of 2 in Ex1
61f60 2e 61 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54 68 65 20 .aCol[]..** The
61f70 73 65 63 6f 6e 64 20 63 6f 6c 75 6d 6e 20 74 6f second column to
61f80 20 62 65 20 69 6e 64 65 78 65 64 20 28 63 31 29 be indexed (c1)
61f90 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 has an index of
61fa0 20 30 20 69 6e 0a 2a 2a 20 45 78 31 2e 61 43 6f 0 in.** Ex1.aCo
61fb0 6c 5b 5d 2c 20 68 65 6e 63 65 20 45 78 32 2e 61 l[], hence Ex2.a
61fc0 69 43 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2e 0a 2a iColumn[1]==0..*
61fd0 2a 0a 2a 2a 20 54 68 65 20 49 6e 64 65 78 2e 6f *.** The Index.o
61fe0 6e 45 72 72 6f 72 20 66 69 65 6c 64 20 64 65 74 nError field det
61ff0 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 ermines whether
62000 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64 65 78 or not the index
62010 65 64 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 6d 75 ed columns.** mu
62020 73 74 20 62 65 20 75 6e 69 71 75 65 20 61 6e 64 st be unique and
62030 20 77 68 61 74 20 74 6f 20 64 6f 20 69 66 20 74 what to do if t
62040 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20 57 68 hey are not. Wh
62050 65 6e 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 en Index.onError
62060 3d 4f 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20 69 74 20 =OE_None,.** it
62070 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20 6e 6f means this is no
62080 74 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65 78 t a unique index
62090 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 74 20 . Otherwise it
620a0 69 73 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65 is a unique inde
620b0 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 76 61 6c x.** and the val
620c0 75 65 20 6f 66 20 49 6e 64 65 78 2e 6f 6e 45 72 ue of Index.onEr
620d0 72 6f 72 20 69 6e 64 69 63 61 74 65 20 74 68 65 ror indicate the
620e0 20 77 68 69 63 68 20 63 6f 6e 66 6c 69 63 74 20 which conflict
620f0 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 2a 2a 20 61 resolution .** a
62100 6c 67 6f 72 69 74 68 6d 20 74 6f 20 65 6d 70 6c lgorithm to empl
62110 6f 79 20 77 68 65 6e 65 76 65 72 20 61 6e 20 61 oy whenever an a
62120 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 ttempt is made t
62130 6f 20 69 6e 73 65 72 74 20 61 20 6e 6f 6e 2d 75 o insert a non-u
62140 6e 69 71 75 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 nique.** element
62150 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e 64 65 ..*/.struct Inde
62160 78 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d x {. char *zNam
62170 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f e; /* Name o
62180 66 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a f this index */.
62190 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 int nColumn;
621a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
621b0 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 columns in the t
621c0 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 68 69 able used by thi
621d0 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 s index */. int
621e0 20 2a 61 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f 2a *aiColumn; /*
621f0 20 57 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20 61 Which columns a
62200 72 65 20 75 73 65 64 20 62 79 20 74 68 69 73 20 re used by this
62210 69 6e 64 65 78 2e 20 20 31 73 74 20 69 73 20 30 index. 1st is 0
62220 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 2a */. unsigned *
62230 61 69 52 6f 77 45 73 74 3b 20 2f 2a 20 52 65 73 aiRowEst; /* Res
62240 75 6c 74 20 6f 66 20 41 4e 41 4c 59 5a 45 3a 20 ult of ANALYZE:
62250 45 73 74 2e 20 72 6f 77 73 20 73 65 6c 65 63 74 Est. rows select
62260 65 64 20 62 79 20 65 61 63 68 20 63 6f 6c 75 6d ed by each colum
62270 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 n */. Table *pT
62280 61 62 6c 65 3b 20 20 20 2f 2a 20 54 68 65 20 53 able; /* The S
62290 51 4c 20 74 61 62 6c 65 20 62 65 69 6e 67 20 69 QL table being i
622a0 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 ndexed */. int
622b0 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 tnum; /*
622c0 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 Page containing
622d0 72 6f 6f 74 20 6f 66 20 74 68 69 73 20 69 6e 64 root of this ind
622e0 65 78 20 69 6e 20 64 61 74 61 62 61 73 65 20 66 ex in database f
622f0 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6f 6e 45 72 ile */. u8 onEr
62300 72 6f 72 3b 20 20 20 20 20 20 2f 2a 20 4f 45 5f ror; /* OE_
62310 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 Abort, OE_Ignore
62320 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 6f 72 , OE_Replace, or
62330 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 75 38 OE_None */. u8
62340 20 61 75 74 6f 49 6e 64 65 78 3b 20 20 20 20 2f autoIndex; /
62350 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 75 74 * True if is aut
62360 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74 omatically creat
62370 65 64 20 28 65 78 3a 20 62 79 20 55 4e 49 51 55 ed (ex: by UNIQU
62380 45 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 E) */. char *zC
62390 6f 6c 41 66 66 3b 20 20 20 2f 2a 20 53 74 72 69 olAff; /* Stri
623a0 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 ng defining the
623b0 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61 63 68 affinity of each
623c0 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 column */. Ind
623d0 65 78 20 2a 70 4e 65 78 74 3b 20 20 20 20 2f 2a ex *pNext; /*
623e0 20 54 68 65 20 6e 65 78 74 20 69 6e 64 65 78 20 The next index
623f0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
62400 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20 2a the same table *
62410 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 /. Schema *pSch
62420 65 6d 61 3b 20 2f 2a 20 53 63 68 65 6d 61 20 63 ema; /* Schema c
62430 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 20 69 ontaining this i
62440 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a 61 53 ndex */. u8 *aS
62450 6f 72 74 4f 72 64 65 72 3b 20 20 2f 2a 20 41 72 ortOrder; /* Ar
62460 72 61 79 20 6f 66 20 73 69 7a 65 20 49 6e 64 65 ray of size Inde
62470 78 2e 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75 65 3d x.nColumn. True=
62480 3d 44 45 53 43 2c 20 46 61 6c 73 65 3d 3d 41 53 =DESC, False==AS
62490 43 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a C */. char **az
624a0 43 6f 6c 6c 3b 20 20 20 2f 2a 20 41 72 72 61 79 Coll; /* Array
624b0 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 of collation se
624c0 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 66 6f 72 quence names for
624d0 20 69 6e 64 65 78 20 2a 2f 0a 20 20 49 6e 64 65 index */. Inde
624e0 78 53 61 6d 70 6c 65 20 2a 61 53 61 6d 70 6c 65 xSample *aSample
624f0 3b 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 ; /* Array of
62500 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 SQLITE_INDEX_SA
62510 4d 50 4c 45 53 20 73 61 6d 70 6c 65 73 20 2a 2f MPLES samples */
62520 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 .};../*.** Each
62530 73 61 6d 70 6c 65 20 73 74 6f 72 65 64 20 69 6e sample stored in
62540 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 the sqlite_stat
62550 32 20 74 61 62 6c 65 20 69 73 20 72 65 70 72 65 2 table is repre
62560 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 sented in memory
62570 20 0a 2a 2a 20 75 73 69 6e 67 20 61 20 73 74 72 .** using a str
62580 75 63 74 75 72 65 20 6f 66 20 74 68 69 73 20 74 ucture of this t
62590 79 70 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 ype..*/.struct I
625a0 6e 64 65 78 53 61 6d 70 6c 65 20 7b 0a 20 20 75 ndexSample {. u
625b0 6e 69 6f 6e 20 7b 0a 20 20 20 20 63 68 61 72 20 nion {. char
625c0 2a 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 61 *z; /* Va
625d0 6c 75 65 20 69 66 20 65 54 79 70 65 20 69 73 20 lue if eType is
625e0 53 51 4c 49 54 45 5f 54 45 58 54 20 6f 72 20 53 SQLITE_TEXT or S
625f0 51 4c 49 54 45 5f 42 4c 4f 42 20 2a 2f 0a 20 20 QLITE_BLOB */.
62600 20 20 64 6f 75 62 6c 65 20 72 3b 20 20 20 20 20 double r;
62610 20 20 2f 2a 20 56 61 6c 75 65 20 69 66 20 65 54 /* Value if eT
62620 79 70 65 20 69 73 20 53 51 4c 49 54 45 5f 46 4c ype is SQLITE_FL
62630 4f 41 54 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e OAT or SQLITE_IN
62640 54 45 47 45 52 20 2a 2f 0a 20 20 7d 20 75 3b 0a TEGER */. } u;.
62650 20 20 75 38 20 65 54 79 70 65 3b 20 20 20 20 20 u8 eType;
62660 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 55 /* SQLITE_NU
62670 4c 4c 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 LL, SQLITE_INTEG
62680 45 52 20 2e 2e 2e 20 65 74 63 2e 20 2a 2f 0a 20 ER ... etc. */.
62690 20 75 38 20 6e 42 79 74 65 3b 20 20 20 20 20 20 u8 nByte;
626a0 20 20 20 2f 2a 20 53 69 7a 65 20 69 6e 20 62 79 /* Size in by
626b0 74 65 20 6f 66 20 74 65 78 74 20 6f 72 20 62 6c te of text or bl
626c0 6f 62 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ob. */.};../*.**
626d0 20 45 61 63 68 20 74 6f 6b 65 6e 20 63 6f 6d 69 Each token comi
626e0 6e 67 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 65 ng out of the le
626f0 78 65 72 20 69 73 20 61 6e 20 69 6e 73 74 61 6e xer is an instan
62700 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 ce of.** this st
62710 72 75 63 74 75 72 65 2e 20 20 54 6f 6b 65 6e 73 ructure. Tokens
62720 20 61 72 65 20 61 6c 73 6f 20 75 73 65 64 20 61 are also used a
62730 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70 s part of an exp
62740 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e ression..**.** N
62750 6f 74 65 20 69 66 20 54 6f 6b 65 6e 2e 7a 3d 3d ote if Token.z==
62760 30 20 74 68 65 6e 20 54 6f 6b 65 6e 2e 64 79 6e 0 then Token.dyn
62770 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 61 72 65 and Token.n are
62780 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 0a 2a undefined and.*
62790 2a 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 72 61 * may contain ra
627a0 6e 64 6f 6d 20 76 61 6c 75 65 73 2e 20 20 44 6f ndom values. Do
627b0 20 6e 6f 74 20 6d 61 6b 65 20 61 6e 79 20 61 73 not make any as
627c0 73 75 6d 70 74 69 6f 6e 73 20 61 62 6f 75 74 20 sumptions about
627d0 54 6f 6b 65 6e 2e 64 79 6e 0a 2a 2a 20 61 6e 64 Token.dyn.** and
627e0 20 54 6f 6b 65 6e 2e 6e 20 77 68 65 6e 20 54 6f Token.n when To
627f0 6b 65 6e 2e 7a 3d 3d 30 2e 0a 2a 2f 0a 73 74 72 ken.z==0..*/.str
62800 75 63 74 20 54 6f 6b 65 6e 20 7b 0a 20 20 63 6f uct Token {. co
62810 6e 73 74 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 nst char *z;
62820 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 /* Text of the
62830 74 6f 6b 65 6e 2e 20 20 4e 6f 74 20 4e 55 4c 4c token. Not NULL
62840 2d 74 65 72 6d 69 6e 61 74 65 64 21 20 2a 2f 0a -terminated! */.
62850 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e unsigned int n
62860 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f ; /* Number o
62870 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 f characters in
62880 74 68 69 73 20 74 6f 6b 65 6e 20 2a 2f 0a 7d 3b this token */.};
62890 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
628a0 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 nce of this stru
628b0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 69 cture contains i
628c0 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 65 nformation neede
628d0 64 20 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a d to generate.**
628e0 20 63 6f 64 65 20 66 6f 72 20 61 20 53 45 4c 45 code for a SELE
628f0 43 54 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 CT that contains
62900 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
62910 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 45 ions..**.** If E
62920 78 70 72 2e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 xpr.op==TK_AGG_C
62930 4f 4c 55 4d 4e 20 6f 72 20 54 4b 5f 41 47 47 5f OLUMN or TK_AGG_
62940 46 55 4e 43 54 49 4f 4e 20 74 68 65 6e 20 45 78 FUNCTION then Ex
62950 70 72 2e 70 41 67 67 49 6e 66 6f 20 69 73 20 61 pr.pAggInfo is a
62960 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 .** pointer to t
62970 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 his structure.
62980 54 68 65 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e The Expr.iColumn
62990 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 69 6e field is the in
629a0 64 65 78 20 69 6e 0a 2a 2a 20 41 67 67 49 6e 66 dex in.** AggInf
629b0 6f 2e 61 43 6f 6c 5b 5d 20 6f 72 20 41 67 67 49 o.aCol[] or AggI
629c0 6e 66 6f 2e 61 46 75 6e 63 5b 5d 20 6f 66 20 69 nfo.aFunc[] of i
629d0 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 65 nformation neede
629e0 64 20 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a d to generate.**
629f0 20 63 6f 64 65 20 66 6f 72 20 74 68 61 74 20 6e code for that n
62a00 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 67 67 49 6e ode..**.** AggIn
62a10 66 6f 2e 70 47 72 6f 75 70 42 79 20 61 6e 64 20 fo.pGroupBy and
62a20 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 2e 70 45 AggInfo.aFunc.pE
62a30 78 70 72 20 70 6f 69 6e 74 20 74 6f 20 66 69 65 xpr point to fie
62a40 6c 64 73 20 77 69 74 68 69 6e 20 74 68 65 0a 2a lds within the.*
62a50 2a 20 6f 72 69 67 69 6e 61 6c 20 53 65 6c 65 63 * original Selec
62a60 74 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 t structure that
62a70 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 53 describes the S
62a80 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
62a90 20 20 54 68 65 73 65 0a 2a 2a 20 66 69 65 6c 64 These.** field
62aa0 73 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f s do not need to
62ab0 20 62 65 20 66 72 65 65 64 20 77 68 65 6e 20 64 be freed when d
62ac0 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 65 20 eallocating the
62ad0 41 67 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72 AggInfo structur
62ae0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41 67 67 e..*/.struct Agg
62af0 49 6e 66 6f 20 7b 0a 20 20 75 38 20 64 69 72 65 Info {. u8 dire
62b00 63 74 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 ctMode;
62b10 20 2f 2a 20 44 69 72 65 63 74 20 72 65 6e 64 65 /* Direct rende
62b20 72 69 6e 67 20 6d 6f 64 65 20 6d 65 61 6e 73 20 ring mode means
62b30 74 61 6b 65 20 64 61 74 61 20 64 69 72 65 63 74 take data direct
62b40 6c 79 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ly.
62b50 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **
62b60 66 72 6f 6d 20 73 6f 75 72 63 65 20 74 61 62 6c from source tabl
62b70 65 73 20 72 61 74 68 65 72 20 74 68 61 6e 20 66 es rather than f
62b80 72 6f 6d 20 61 63 63 75 6d 75 6c 61 74 6f 72 73 rom accumulators
62b90 20 2a 2f 0a 20 20 75 38 20 75 73 65 53 6f 72 74 */. u8 useSort
62ba0 69 6e 67 49 64 78 3b 20 20 20 20 20 20 20 2f 2a ingIdx; /*
62bb0 20 49 6e 20 64 69 72 65 63 74 20 6d 6f 64 65 2c In direct mode,
62bc0 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 73 reference the s
62bd0 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 72 61 74 orting index rat
62be0 68 65 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 her.
62bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a **
62c00 20 74 68 61 6e 20 74 68 65 20 73 6f 75 72 63 65 than the source
62c10 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 table */. int
62c20 73 6f 72 74 69 6e 67 49 64 78 3b 20 20 20 20 20 sortingIdx;
62c30 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 /* Cursor nu
62c40 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f 72 74 mber of the sort
62c50 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 45 ing index */. E
62c60 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 xprList *pGroupB
62c70 79 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 67 72 y; /* The gr
62c80 6f 75 70 20 62 79 20 63 6c 61 75 73 65 20 2a 2f oup by clause */
62c90 0a 20 20 69 6e 74 20 6e 53 6f 72 74 69 6e 67 43 . int nSortingC
62ca0 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 olumn; /* Nu
62cb0 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
62cc0 69 6e 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 in the sorting i
62cd0 6e 64 65 78 20 2a 2f 0a 20 20 73 74 72 75 63 74 ndex */. struct
62ce0 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 7b 20 20 AggInfo_col {
62cf0 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 63 6f /* For each co
62d00 6c 75 6d 6e 20 75 73 65 64 20 69 6e 20 73 6f 75 lumn used in sou
62d10 72 63 65 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 rce tables */.
62d20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 Table *pTab;
62d30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f /* So
62d40 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 urce table */.
62d50 20 20 69 6e 74 20 69 54 61 62 6c 65 3b 20 20 20 int iTable;
62d60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 /* Cu
62d70 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 rsor number of t
62d80 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 he source table
62d90 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 */. int iColu
62da0 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 mn;
62db0 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 /* Column number
62dc0 20 77 69 74 68 69 6e 20 74 68 65 20 73 6f 75 72 within the sour
62dd0 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 ce table */.
62de0 69 6e 74 20 69 53 6f 72 74 65 72 43 6f 6c 75 6d int iSorterColum
62df0 6e 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 n; /* Colu
62e00 6d 6e 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 mn number in the
62e10 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a sorting index *
62e20 2f 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 /. int iMem;
62e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
62e40 2a 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f * Memory locatio
62e50 6e 20 74 68 61 74 20 61 63 74 73 20 61 73 20 61 n that acts as a
62e60 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 ccumulator */.
62e70 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 Expr *pExpr;
62e80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
62e90 65 20 6f 72 69 67 69 6e 61 6c 20 65 78 70 72 65 e original expre
62ea0 73 73 69 6f 6e 20 2a 2f 0a 20 20 7d 20 2a 61 43 ssion */. } *aC
62eb0 6f 6c 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d ol;. int nColum
62ec0 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a n; /*
62ed0 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 64 20 Number of used
62ee0 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6c 5b entries in aCol[
62ef0 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 ] */. int nColu
62f00 6d 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 2f mnAlloc; /
62f10 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 * Number of slot
62f20 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 s allocated for
62f30 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 aCol[] */. int
62f40 6e 41 63 63 75 6d 75 6c 61 74 6f 72 3b 20 20 20 nAccumulator;
62f50 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
62f60 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 73 68 columns that sh
62f70 6f 77 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 ow through to th
62f80 65 20 6f 75 74 70 75 74 2e 0a 20 20 20 20 20 20 e output..
62f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62fa0 20 20 20 20 2a 2a 20 41 64 64 69 74 69 6f 6e 61 ** Additiona
62fb0 6c 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 73 l columns are us
62fc0 65 64 20 6f 6e 6c 79 20 61 73 20 70 61 72 61 6d ed only as param
62fd0 65 74 65 72 73 20 74 6f 0a 20 20 20 20 20 20 20 eters to.
62fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62ff0 20 20 20 2a 2a 20 61 67 67 72 65 67 61 74 65 20 ** aggregate
63000 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 functions */. s
63010 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 truct AggInfo_fu
63020 6e 63 20 7b 20 20 20 2f 2a 20 46 6f 72 20 65 61 nc { /* For ea
63030 63 68 20 61 67 67 72 65 67 61 74 65 20 66 75 6e ch aggregate fun
63040 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 45 78 70 ction */. Exp
63050 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 r *pExpr;
63060 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 /* Express
63070 69 6f 6e 20 65 6e 63 6f 64 69 6e 67 20 74 68 65 ion encoding the
63080 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 function */.
63090 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b FuncDef *pFunc;
630a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
630b0 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
630c0 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ion implementati
630d0 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4d on */. int iM
630e0 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 em;
630f0 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6c 6f 63 /* Memory loc
63100 61 74 69 6f 6e 20 74 68 61 74 20 61 63 74 73 20 ation that acts
63110 61 73 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 2a as accumulator *
63120 2f 0a 20 20 20 20 69 6e 74 20 69 44 69 73 74 69 /. int iDisti
63130 6e 63 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f nct; /
63140 2a 20 45 70 68 65 6d 65 72 61 6c 20 74 61 62 6c * Ephemeral tabl
63150 65 20 75 73 65 64 20 74 6f 20 65 6e 66 6f 72 63 e used to enforc
63160 65 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20 e DISTINCT */.
63170 7d 20 2a 61 46 75 6e 63 3b 0a 20 20 69 6e 74 20 } *aFunc;. int
63180 6e 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 nFunc;
63190 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
631a0 20 65 6e 74 72 69 65 73 20 69 6e 20 61 46 75 6e entries in aFun
631b0 63 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 c[] */. int nFu
631c0 6e 63 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 ncAlloc;
631d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c /* Number of sl
631e0 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f ots allocated fo
631f0 72 20 61 46 75 6e 63 5b 5d 20 2a 2f 0a 7d 3b 0a r aFunc[] */.};.
63200 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6e 6f 64 65 ./*.** Each node
63210 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f of an expressio
63220 6e 20 69 6e 20 74 68 65 20 70 61 72 73 65 20 74 n in the parse t
63230 72 65 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e ree is an instan
63240 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 ce.** of this st
63250 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 45 ructure..**.** E
63260 78 70 72 2e 6f 70 20 69 73 20 74 68 65 20 6f 70 xpr.op is the op
63270 63 6f 64 65 2e 20 54 68 65 20 69 6e 74 65 67 65 code. The intege
63280 72 20 70 61 72 73 65 72 20 74 6f 6b 65 6e 20 63 r parser token c
63290 6f 64 65 73 20 61 72 65 20 72 65 75 73 65 64 0a odes are reused.
632a0 2a 2a 20 61 73 20 6f 70 63 6f 64 65 73 20 68 65 ** as opcodes he
632b0 72 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c re. For example,
632c0 20 74 68 65 20 70 61 72 73 65 72 20 64 65 66 69 the parser defi
632d0 6e 65 73 20 54 4b 5f 47 45 20 74 6f 20 62 65 20 nes TK_GE to be
632e0 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 63 6f an integer.** co
632f0 64 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 de representing
63300 74 68 65 20 22 3e 3d 22 20 6f 70 65 72 61 74 6f the ">=" operato
63310 72 2e 20 54 68 69 73 20 73 61 6d 65 20 69 6e 74 r. This same int
63320 65 67 65 72 20 63 6f 64 65 20 69 73 20 72 65 75 eger code is reu
63330 73 65 64 0a 2a 2a 20 74 6f 20 72 65 70 72 65 73 sed.** to repres
63340 65 6e 74 20 74 68 65 20 67 72 65 61 74 65 72 2d ent the greater-
63350 74 68 61 6e 2d 6f 72 2d 65 71 75 61 6c 2d 74 6f than-or-equal-to
63360 20 6f 70 65 72 61 74 6f 72 20 69 6e 20 74 68 65 operator in the
63370 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 expression.** t
63380 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ree..**.** If th
63390 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
633a0 61 6e 20 53 51 4c 20 6c 69 74 65 72 61 6c 20 28 an SQL literal (
633b0 54 4b 5f 49 4e 54 45 47 45 52 2c 20 54 4b 5f 46 TK_INTEGER, TK_F
633c0 4c 4f 41 54 2c 20 54 4b 5f 42 4c 4f 42 2c 20 0a LOAT, TK_BLOB, .
633d0 2a 2a 20 6f 72 20 54 4b 5f 53 54 52 49 4e 47 29 ** or TK_STRING)
633e0 2c 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 , then Expr.toke
633f0 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 n contains the t
63400 65 78 74 20 6f 66 20 74 68 65 20 53 51 4c 20 6c ext of the SQL l
63410 69 74 65 72 61 6c 2e 20 49 66 0a 2a 2a 20 74 68 iteral. If.** th
63420 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
63430 61 20 76 61 72 69 61 62 6c 65 20 28 54 4b 5f 56 a variable (TK_V
63440 41 52 49 41 42 4c 45 29 2c 20 74 68 65 6e 20 45 ARIABLE), then E
63450 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 xpr.token contai
63460 6e 73 20 74 68 65 20 0a 2a 2a 20 76 61 72 69 61 ns the .** varia
63470 62 6c 65 20 6e 61 6d 65 2e 20 46 69 6e 61 6c 6c ble name. Finall
63480 79 2c 20 69 66 20 74 68 65 20 65 78 70 72 65 73 y, if the expres
63490 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 66 sion is an SQL f
634a0 75 6e 63 74 69 6f 6e 20 28 54 4b 5f 46 55 4e 43 unction (TK_FUNC
634b0 54 49 4f 4e 29 2c 0a 2a 2a 20 74 68 65 6e 20 45 TION),.** then E
634c0 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 xpr.token contai
634d0 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 ns the name of t
634e0 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a he function..**.
634f0 2a 2a 20 45 78 70 72 2e 70 52 69 67 68 74 20 61 ** Expr.pRight a
63500 6e 64 20 45 78 70 72 2e 70 4c 65 66 74 20 61 72 nd Expr.pLeft ar
63510 65 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20 72 e the left and r
63520 69 67 68 74 20 73 75 62 65 78 70 72 65 73 73 69 ight subexpressi
63530 6f 6e 73 20 6f 66 20 61 0a 2a 2a 20 62 69 6e 61 ons of a.** bina
63540 72 79 20 6f 70 65 72 61 74 6f 72 2e 20 45 69 74 ry operator. Eit
63550 68 65 72 20 6f 72 20 62 6f 74 68 20 6d 61 79 20 her or both may
63560 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45 be NULL..**.** E
63570 78 70 72 2e 78 2e 70 4c 69 73 74 20 69 73 20 61 xpr.x.pList is a
63580 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e list of argumen
63590 74 73 20 69 66 20 74 68 65 20 65 78 70 72 65 73 ts if the expres
635a0 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 66 sion is an SQL f
635b0 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 61 20 43 41 unction,.** a CA
635c0 53 45 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 SE expression or
635d0 20 61 6e 20 49 4e 20 65 78 70 72 65 73 73 69 6f an IN expressio
635e0 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c n of the form "<
635f0 6c 68 73 3e 20 49 4e 20 28 3c 79 3e 2c 20 3c 7a lhs> IN (<y>, <z
63600 3e 2e 2e 2e 29 22 2e 0a 2a 2a 20 45 78 70 72 2e >...)"..** Expr.
63610 78 2e 70 53 65 6c 65 63 74 20 69 73 20 75 73 65 x.pSelect is use
63620 64 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 d if the express
63630 69 6f 6e 20 69 73 20 61 20 73 75 62 2d 73 65 6c ion is a sub-sel
63640 65 63 74 20 6f 72 20 61 6e 20 65 78 70 72 65 73 ect or an expres
63650 73 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 66 sion of.** the f
63660 6f 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 28 53 orm "<lhs> IN (S
63670 45 4c 45 43 54 20 2e 2e 2e 29 22 2e 20 49 66 20 ELECT ...)". If
63680 74 68 65 20 45 50 5f 78 49 73 53 65 6c 65 63 74 the EP_xIsSelect
63690 20 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 bit is set in t
636a0 68 65 0a 2a 2a 20 45 78 70 72 2e 66 6c 61 67 73 he.** Expr.flags
636b0 20 6d 61 73 6b 2c 20 74 68 65 6e 20 45 78 70 72 mask, then Expr
636c0 2e 78 2e 70 53 65 6c 65 63 74 20 69 73 20 76 61 .x.pSelect is va
636d0 6c 69 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 lid. Otherwise,
636e0 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 69 73 20 Expr.x.pList is
636f0 0a 2a 2a 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a .** valid..**.**
63700 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f An expression o
63710 66 20 74 68 65 20 66 6f 72 6d 20 49 44 20 6f 72 f the form ID or
63720 20 49 44 2e 49 44 20 72 65 66 65 72 73 20 74 6f ID.ID refers to
63730 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 74 a column in a t
63740 61 62 6c 65 2e 0a 2a 2a 20 46 6f 72 20 73 75 63 able..** For suc
63750 68 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 45 h expressions, E
63760 78 70 72 2e 6f 70 20 69 73 20 73 65 74 20 74 6f xpr.op is set to
63770 20 54 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 45 TK_COLUMN and E
63780 78 70 72 2e 69 54 61 62 6c 65 20 69 73 0a 2a 2a xpr.iTable is.**
63790 20 74 68 65 20 69 6e 74 65 67 65 72 20 63 75 72 the integer cur
637a0 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 sor number of a
637b0 56 44 42 45 20 63 75 72 73 6f 72 20 70 6f 69 6e VDBE cursor poin
637c0 74 69 6e 67 20 74 6f 20 74 68 61 74 20 74 61 62 ting to that tab
637d0 6c 65 20 61 6e 64 0a 2a 2a 20 45 78 70 72 2e 69 le and.** Expr.i
637e0 43 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 63 6f Column is the co
637f0 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 lumn number for
63800 74 68 65 20 73 70 65 63 69 66 69 63 20 63 6f 6c the specific col
63810 75 6d 6e 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 umn. If the.**
63820 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 75 73 expression is us
63830 65 64 20 61 73 20 61 20 72 65 73 75 6c 74 20 69 ed as a result i
63840 6e 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 53 n an aggregate S
63850 45 4c 45 43 54 2c 20 74 68 65 6e 20 74 68 65 0a ELECT, then the.
63860 2a 2a 20 76 61 6c 75 65 20 69 73 20 61 6c 73 6f ** value is also
63870 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 45 stored in the E
63880 78 70 72 2e 69 41 67 67 20 63 6f 6c 75 6d 6e 20 xpr.iAgg column
63890 69 6e 20 74 68 65 20 61 67 67 72 65 67 61 74 65 in the aggregate
638a0 20 73 6f 20 74 68 61 74 0a 2a 2a 20 69 74 20 63 so that.** it c
638b0 61 6e 20 62 65 20 61 63 63 65 73 73 65 64 20 61 an be accessed a
638c0 66 74 65 72 20 61 6c 6c 20 61 67 67 72 65 67 61 fter all aggrega
638d0 74 65 73 20 61 72 65 20 63 6f 6d 70 75 74 65 64 tes are computed
638e0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 ..**.** If the e
638f0 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 xpression is an
63900 75 6e 62 6f 75 6e 64 20 76 61 72 69 61 62 6c 65 unbound variable
63910 20 6d 61 72 6b 65 72 20 28 61 20 71 75 65 73 74 marker (a quest
63920 69 6f 6e 20 6d 61 72 6b 20 0a 2a 2a 20 63 68 61 ion mark .** cha
63930 72 61 63 74 65 72 20 27 3f 27 20 69 6e 20 74 68 racter '?' in th
63940 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 29 20 e original SQL)
63950 74 68 65 6e 20 74 68 65 20 45 78 70 72 2e 69 54 then the Expr.iT
63960 61 62 6c 65 20 68 6f 6c 64 73 20 74 68 65 20 69 able holds the i
63970 6e 64 65 78 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 ndex .** number
63980 66 6f 72 20 74 68 61 74 20 76 61 72 69 61 62 6c for that variabl
63990 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
639a0 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 expression is a
639b0 73 75 62 71 75 65 72 79 20 74 68 65 6e 20 45 78 subquery then Ex
639c0 70 72 2e 69 43 6f 6c 75 6d 6e 20 68 6f 6c 64 73 pr.iColumn holds
639d0 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 72 an integer.** r
639e0 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 63 egister number c
639f0 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 65 ontaining the re
63a00 73 75 6c 74 20 6f 66 20 74 68 65 20 73 75 62 71 sult of the subq
63a10 75 65 72 79 2e 20 20 49 66 20 74 68 65 0a 2a 2a uery. If the.**
63a20 20 73 75 62 71 75 65 72 79 20 67 69 76 65 73 20 subquery gives
63a30 61 20 63 6f 6e 73 74 61 6e 74 20 72 65 73 75 6c a constant resul
63a40 74 2c 20 74 68 65 6e 20 69 54 61 62 6c 65 20 69 t, then iTable i
63a50 73 20 2d 31 2e 20 20 49 66 20 74 68 65 20 73 75 s -1. If the su
63a60 62 71 75 65 72 79 0a 2a 2a 20 67 69 76 65 73 20 bquery.** gives
63a70 61 20 64 69 66 66 65 72 65 6e 74 20 61 6e 73 77 a different answ
63a80 65 72 20 61 74 20 64 69 66 66 65 72 65 6e 74 20 er at different
63a90 74 69 6d 65 73 20 64 75 72 69 6e 67 20 73 74 61 times during sta
63aa0 74 65 6d 65 6e 74 20 70 72 6f 63 65 73 73 69 6e tement processin
63ab0 67 0a 2a 2a 20 74 68 65 6e 20 69 54 61 62 6c 65 g.** then iTable
63ac0 20 69 73 20 74 68 65 20 61 64 64 72 65 73 73 20 is the address
63ad0 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 of a subroutine
63ae0 74 68 61 74 20 63 6f 6d 70 75 74 65 73 20 74 68 that computes th
63af0 65 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a e subquery..**.*
63b00 2a 20 49 66 20 74 68 65 20 45 78 70 72 20 69 73 * If the Expr is
63b10 20 6f 66 20 74 79 70 65 20 4f 50 5f 43 6f 6c 75 of type OP_Colu
63b20 6d 6e 2c 20 61 6e 64 20 74 68 65 20 74 61 62 6c mn, and the tabl
63b30 65 20 69 74 20 69 73 20 73 65 6c 65 63 74 69 6e e it is selectin
63b40 67 20 66 72 6f 6d 0a 2a 2a 20 69 73 20 61 20 64 g from.** is a d
63b50 69 73 6b 20 74 61 62 6c 65 20 6f 72 20 74 68 65 isk table or the
63b60 20 22 6f 6c 64 2e 2a 22 20 70 73 65 75 64 6f 2d "old.*" pseudo-
63b70 74 61 62 6c 65 2c 20 74 68 65 6e 20 70 54 61 62 table, then pTab
63b80 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a points to the.*
63b90 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 * corresponding
63ba0 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e table definition
63bb0 2e 0a 2a 2a 0a 2a 2a 20 41 4c 4c 4f 43 41 54 49 ..**.** ALLOCATI
63bc0 4f 4e 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 ON NOTES:.**.**
63bd0 45 78 70 72 20 6f 62 6a 65 63 74 73 20 63 61 6e Expr objects can
63be0 20 75 73 65 20 61 20 6c 6f 74 20 6f 66 20 6d 65 use a lot of me
63bf0 6d 6f 72 79 20 73 70 61 63 65 20 69 6e 20 64 61 mory space in da
63c00 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 20 tabase schema.
63c10 54 6f 0a 2a 2a 20 68 65 6c 70 20 72 65 64 75 63 To.** help reduc
63c20 65 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65 e memory require
63c30 6d 65 6e 74 73 2c 20 73 6f 6d 65 74 69 6d 65 73 ments, sometimes
63c40 20 61 6e 20 45 78 70 72 20 6f 62 6a 65 63 74 20 an Expr object
63c50 77 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 75 6e 63 will be.** trunc
63c60 61 74 65 64 2e 20 20 41 6e 64 20 74 6f 20 72 65 ated. And to re
63c70 64 75 63 65 20 74 68 65 20 6e 75 6d 62 65 72 20 duce the number
63c80 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 of memory alloca
63c90 74 69 6f 6e 73 2c 20 73 6f 6d 65 74 69 6d 65 73 tions, sometimes
63ca0 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 .** two or more
63cb0 45 78 70 72 20 6f 62 6a 65 63 74 73 20 77 69 6c Expr objects wil
63cc0 6c 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 l be stored in a
63cd0 20 73 69 6e 67 6c 65 20 6d 65 6d 6f 72 79 20 61 single memory a
63ce0 6c 6c 6f 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74 6f llocation,.** to
63cf0 67 65 74 68 65 72 20 77 69 74 68 20 45 78 70 72 gether with Expr
63d00 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 73 2e .zToken strings.
63d10 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 50 .**.** If the EP
63d20 5f 52 65 64 75 63 65 64 20 61 6e 64 20 45 50 5f _Reduced and EP_
63d30 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 73 20 TokenOnly flags
63d40 61 72 65 20 73 65 74 20 77 68 65 6e 0a 2a 2a 20 are set when.**
63d50 61 6e 20 45 78 70 72 20 6f 62 6a 65 63 74 20 69 an Expr object i
63d60 73 20 74 72 75 6e 63 61 74 65 64 2e 20 20 57 68 s truncated. Wh
63d70 65 6e 20 45 50 5f 52 65 64 75 63 65 64 20 69 73 en EP_Reduced is
63d80 20 73 65 74 2c 20 74 68 65 6e 20 61 6c 6c 0a 2a set, then all.*
63d90 2a 20 74 68 65 20 63 68 69 6c 64 20 45 78 70 72 * the child Expr
63da0 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 65 20 objects in the
63db0 45 78 70 72 2e 70 4c 65 66 74 20 61 6e 64 20 45 Expr.pLeft and E
63dc0 78 70 72 2e 70 52 69 67 68 74 20 73 75 62 74 72 xpr.pRight subtr
63dd0 65 65 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 74 61 ees.** are conta
63de0 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 ined within the
63df0 73 61 6d 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f same memory allo
63e00 63 61 74 69 6f 6e 2e 20 20 4e 6f 74 65 2c 20 68 cation. Note, h
63e10 6f 77 65 76 65 72 2c 20 74 68 61 74 0a 2a 2a 20 owever, that.**
63e20 74 68 65 20 73 75 62 74 72 65 65 73 20 69 6e 20 the subtrees in
63e30 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 6f 72 20 Expr.x.pList or
63e40 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 20 61 Expr.x.pSelect a
63e50 72 65 20 61 6c 77 61 79 73 20 73 65 70 61 72 61 re always separa
63e60 74 65 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 tely.** allocate
63e70 64 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 d, regardless of
63e80 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
63e90 45 50 5f 52 65 64 75 63 65 64 20 69 73 20 73 65 EP_Reduced is se
63ea0 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 70 t..*/.struct Exp
63eb0 72 20 7b 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 r {. u8 op;
63ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
63ed0 4f 70 65 72 61 74 69 6f 6e 20 70 65 72 66 6f 72 Operation perfor
63ee0 6d 65 64 20 62 79 20 74 68 69 73 20 6e 6f 64 65 med by this node
63ef0 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e */. char affin
63f00 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ity; /*
63f10 54 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 The affinity of
63f20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 30 20 the column or 0
63f30 69 66 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 if not a column
63f40 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 */. u16 flags;
63f50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
63f60 61 72 69 6f 75 73 20 66 6c 61 67 73 2e 20 20 45 arious flags. E
63f70 50 5f 2a 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f P_* See below */
63f80 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 63 . union {. c
63f90 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20 har *zToken;
63fa0 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 76 /* Token v
63fb0 61 6c 75 65 2e 20 5a 65 72 6f 20 74 65 72 6d 69 alue. Zero termi
63fc0 6e 61 74 65 64 20 61 6e 64 20 64 65 71 75 6f 74 nated and dequot
63fd0 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 56 ed */. int iV
63fe0 61 6c 75 65 3b 20 20 20 20 20 20 20 20 20 20 20 alue;
63ff0 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 /* Integer valu
64000 65 20 69 66 20 45 50 5f 49 6e 74 56 61 6c 75 65 e if EP_IntValue
64010 20 2a 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20 2f 2a */. } u;.. /*
64020 20 49 66 20 74 68 65 20 45 50 5f 54 6f 6b 65 6e If the EP_Token
64030 4f 6e 6c 79 20 66 6c 61 67 20 69 73 20 73 65 74 Only flag is set
64040 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 in the Expr.fla
64050 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20 6e 6f gs mask, then no
64060 0a 20 20 2a 2a 20 73 70 61 63 65 20 69 73 20 61 . ** space is a
64070 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 llocated for the
64080 20 66 69 65 6c 64 73 20 62 65 6c 6f 77 20 74 68 fields below th
64090 69 73 20 70 6f 69 6e 74 2e 20 41 6e 20 61 74 74 is point. An att
640a0 65 6d 70 74 20 74 6f 0a 20 20 2a 2a 20 61 63 63 empt to. ** acc
640b0 65 73 73 20 74 68 65 6d 20 77 69 6c 6c 20 72 65 ess them will re
640c0 73 75 6c 74 20 69 6e 20 61 20 73 65 67 66 61 75 sult in a segfau
640d0 6c 74 20 6f 72 20 6d 61 6c 66 75 6e 63 74 69 6f lt or malfunctio
640e0 6e 2e 20 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n. . **********
640f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 ***********/..
64130 45 78 70 72 20 2a 70 4c 65 66 74 3b 20 20 20 20 Expr *pLeft;
64140 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 73 /* Left s
64150 75 62 6e 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 ubnode */. Expr
64160 20 2a 70 52 69 67 68 74 3b 20 20 20 20 20 20 20 *pRight;
64170 20 20 20 2f 2a 20 52 69 67 68 74 20 73 75 62 6e /* Right subn
64180 6f 64 65 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b ode */. union {
64190 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 . ExprList *p
641a0 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20 46 75 6e List; /* Fun
641b0 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 ction arguments
641c0 6f 72 20 69 6e 20 22 3c 65 78 70 72 3e 20 49 4e or in "<expr> IN
641d0 20 28 3c 65 78 70 72 2d 6c 69 73 74 29 22 20 2a (<expr-list)" *
641e0 2f 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 /. Select *pS
641f0 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 55 73 elect; /* Us
64200 65 64 20 66 6f 72 20 73 75 62 2d 73 65 6c 65 63 ed for sub-selec
64210 74 73 20 61 6e 64 20 22 3c 65 78 70 72 3e 20 49 ts and "<expr> I
64220 4e 20 28 3c 73 65 6c 65 63 74 3e 29 22 20 2a 2f N (<select>)" */
64230 0a 20 20 7d 20 78 3b 0a 20 20 43 6f 6c 6c 53 65 . } x;. CollSe
64240 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 q *pColl;
64250 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f /* The collatio
64260 6e 20 74 79 70 65 20 6f 66 20 74 68 65 20 63 6f n type of the co
64270 6c 75 6d 6e 20 6f 72 20 30 20 2a 2f 0a 0a 20 20 lumn or 0 */..
64280 2f 2a 20 49 66 20 74 68 65 20 45 50 5f 52 65 64 /* If the EP_Red
64290 75 63 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 uced flag is set
642a0 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 in the Expr.fla
642b0 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20 6e 6f gs mask, then no
642c0 0a 20 20 2a 2a 20 73 70 61 63 65 20 69 73 20 61 . ** space is a
642d0 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 llocated for the
642e0 20 66 69 65 6c 64 73 20 62 65 6c 6f 77 20 74 68 fields below th
642f0 69 73 20 70 6f 69 6e 74 2e 20 41 6e 20 61 74 74 is point. An att
64300 65 6d 70 74 20 74 6f 0a 20 20 2a 2a 20 61 63 63 empt to. ** acc
64310 65 73 73 20 74 68 65 6d 20 77 69 6c 6c 20 72 65 ess them will re
64320 73 75 6c 74 20 69 6e 20 61 20 73 65 67 66 61 75 sult in a segfau
64330 6c 74 20 6f 72 20 6d 61 6c 66 75 6e 63 74 69 6f lt or malfunctio
64340 6e 2e 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.. ***********
64350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 69 **********/.. i
64390 6e 74 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 nt iTable;
643a0 20 20 20 20 20 20 2f 2a 20 54 4b 5f 43 4f 4c 55 /* TK_COLU
643b0 4d 4e 3a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 MN: cursor numbe
643c0 72 20 6f 66 20 74 61 62 6c 65 20 68 6f 6c 64 69 r of table holdi
643d0 6e 67 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 ng column.
643e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
643f0 20 20 20 2a 2a 20 54 4b 5f 52 45 47 49 53 54 45 ** TK_REGISTE
64400 52 3a 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 R: register numb
64410 65 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 er.
64420 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 ** T
64430 4b 5f 54 52 49 47 47 45 52 3a 20 31 20 2d 3e 20 K_TRIGGER: 1 ->
64440 6e 65 77 2c 20 30 20 2d 3e 20 6f 6c 64 20 2a 2f new, 0 -> old */
64450 0a 20 20 69 31 36 20 69 43 6f 6c 75 6d 6e 3b 20 . i16 iColumn;
64460 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f /* TK_
64470 43 4f 4c 55 4d 4e 3a 20 63 6f 6c 75 6d 6e 20 69 COLUMN: column i
64480 6e 64 65 78 2e 20 20 2d 31 20 66 6f 72 20 72 6f ndex. -1 for ro
64490 77 69 64 20 2a 2f 0a 20 20 69 31 36 20 69 41 67 wid */. i16 iAg
644a0 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
644b0 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 /* Which entry i
644c0 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c n pAggInfo->aCol
644d0 5b 5d 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 [] or ->aFunc[]
644e0 2a 2f 0a 20 20 69 31 36 20 69 52 69 67 68 74 4a */. i16 iRightJ
644f0 6f 69 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 oinTable; /* I
64500 66 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 f EP_FromJoin, t
64510 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f he right table o
64520 66 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 f the join */.
64530 75 38 20 66 6c 61 67 73 32 3b 20 20 20 20 20 20 u8 flags2;
64540 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 /* Second
64550 20 73 65 74 20 6f 66 20 66 6c 61 67 73 2e 20 20 set of flags.
64560 45 50 32 5f 2e 2e 2e 20 2a 2f 0a 20 20 75 38 20 EP2_... */. u8
64570 6f 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 op2;
64580 20 20 20 20 2f 2a 20 49 66 20 61 20 54 4b 5f 52 /* If a TK_R
64590 45 47 49 53 54 45 52 2c 20 74 68 65 20 6f 72 69 EGISTER, the ori
645a0 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 45 ginal value of E
645b0 78 70 72 2e 6f 70 20 2a 2f 0a 20 20 41 67 67 49 xpr.op */. AggI
645c0 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20 nfo *pAggInfo;
645d0 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 54 4b /* Used by TK
645e0 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 _AGG_COLUMN and
645f0 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 TK_AGG_FUNCTION
64600 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 */. Table *pTab
64610 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
64620 61 62 6c 65 20 66 6f 72 20 54 4b 5f 43 4f 4c 55 able for TK_COLU
64630 4d 4e 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 MN expressions.
64640 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 */.#if SQLITE_MA
64650 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 X_EXPR_DEPTH>0.
64660 20 69 6e 74 20 6e 48 65 69 67 68 74 3b 20 20 20 int nHeight;
64670 20 20 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68 /* Heigh
64680 74 20 6f 66 20 74 68 65 20 74 72 65 65 20 68 65 t of the tree he
64690 61 64 65 64 20 62 79 20 74 68 69 73 20 6e 6f 64 aded by this nod
646a0 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a e */.#endif.};..
646b0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
646c0 69 6e 67 20 61 72 65 20 74 68 65 20 6d 65 61 6e ing are the mean
646d0 69 6e 67 73 20 6f 66 20 62 69 74 73 20 69 6e 20 ings of bits in
646e0 74 68 65 20 45 78 70 72 2e 66 6c 61 67 73 20 66 the Expr.flags f
646f0 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ield..*/.#define
64700 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 20 20 30 EP_FromJoin 0
64710 78 30 30 30 31 20 20 2f 2a 20 4f 72 69 67 69 6e x0001 /* Origin
64720 61 74 65 64 20 69 6e 20 4f 4e 20 6f 72 20 55 53 ated in ON or US
64730 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 ING clause of a
64740 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 join */.#define
64750 45 50 5f 41 67 67 20 20 20 20 20 20 20 20 30 78 EP_Agg 0x
64760 30 30 30 32 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 0002 /* Contain
64770 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 61 67 s one or more ag
64780 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
64790 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f s */.#define EP_
647a0 52 65 73 6f 6c 76 65 64 20 20 20 30 78 30 30 30 Resolved 0x000
647b0 34 20 20 2f 2a 20 49 44 73 20 68 61 76 65 20 62 4 /* IDs have b
647c0 65 65 6e 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 een resolved to
647d0 43 4f 4c 55 4d 4e 73 20 2a 2f 0a 23 64 65 66 69 COLUMNs */.#defi
647e0 6e 65 20 45 50 5f 45 72 72 6f 72 20 20 20 20 20 ne EP_Error
647f0 20 30 78 30 30 30 38 20 20 2f 2a 20 45 78 70 72 0x0008 /* Expr
64800 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 ession contains
64810 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 65 72 72 6f one or more erro
64820 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 rs */.#define EP
64830 5f 44 69 73 74 69 6e 63 74 20 20 20 30 78 30 30 _Distinct 0x00
64840 31 30 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 10 /* Aggregate
64850 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 44 function with D
64860 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 ISTINCT keyword
64870 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 56 61 */.#define EP_Va
64880 72 53 65 6c 65 63 74 20 20 30 78 30 30 32 30 20 rSelect 0x0020
64890 20 2f 2a 20 70 53 65 6c 65 63 74 20 69 73 20 63 /* pSelect is c
648a0 6f 72 72 65 6c 61 74 65 64 2c 20 6e 6f 74 20 63 orrelated, not c
648b0 6f 6e 73 74 61 6e 74 20 2a 2f 0a 23 64 65 66 69 onstant */.#defi
648c0 6e 65 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 20 ne EP_DblQuoted
648d0 20 30 78 30 30 34 30 20 20 2f 2a 20 74 6f 6b 65 0x0040 /* toke
648e0 6e 2e 7a 20 77 61 73 20 6f 72 69 67 69 6e 61 6c n.z was original
648f0 6c 79 20 69 6e 20 22 2e 2e 2e 22 20 2a 2f 0a 23 ly in "..." */.#
64900 64 65 66 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 define EP_InfixF
64910 75 6e 63 20 20 30 78 30 30 38 30 20 20 2f 2a 20 unc 0x0080 /*
64920 54 72 75 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 True for an infi
64930 78 20 66 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 x function: LIKE
64940 2c 20 47 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 , GLOB, etc */.#
64950 64 65 66 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c define EP_ExpCol
64960 6c 61 74 65 20 30 78 30 31 30 30 20 20 2f 2a 20 late 0x0100 /*
64970 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e Collating sequen
64980 63 65 20 73 70 65 63 69 66 69 65 64 20 65 78 70 ce specified exp
64990 6c 69 63 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 licitly */.#defi
649a0 6e 65 20 45 50 5f 41 6e 79 41 66 66 20 20 20 20 ne EP_AnyAff
649b0 20 30 78 30 32 30 30 20 20 2f 2a 20 43 61 6e 20 0x0200 /* Can
649c0 74 61 6b 65 20 61 20 63 61 63 68 65 64 20 63 6f take a cached co
649d0 6c 75 6d 6e 20 6f 66 20 61 6e 79 20 61 66 66 69 lumn of any affi
649e0 6e 69 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 nity */.#define
649f0 45 50 5f 46 69 78 65 64 44 65 73 74 20 20 30 78 EP_FixedDest 0x
64a00 30 34 30 30 20 20 2f 2a 20 52 65 73 75 6c 74 20 0400 /* Result
64a10 6e 65 65 64 65 64 20 69 6e 20 61 20 73 70 65 63 needed in a spec
64a20 69 66 69 63 20 72 65 67 69 73 74 65 72 20 2a 2f ific register */
64a30 0a 23 64 65 66 69 6e 65 20 45 50 5f 49 6e 74 56 .#define EP_IntV
64a40 61 6c 75 65 20 20 20 30 78 30 38 30 30 20 20 2f alue 0x0800 /
64a50 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 * Integer value
64a60 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 75 2e 69 contained in u.i
64a70 56 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e 65 Value */.#define
64a80 20 45 50 5f 78 49 73 53 65 6c 65 63 74 20 20 30 EP_xIsSelect 0
64a90 78 31 30 30 30 20 20 2f 2a 20 78 2e 70 53 65 6c x1000 /* x.pSel
64aa0 65 63 74 20 69 73 20 76 61 6c 69 64 20 28 6f 74 ect is valid (ot
64ab0 68 65 72 77 69 73 65 20 78 2e 70 4c 69 73 74 20 herwise x.pList
64ac0 69 73 29 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 is) */..#define
64ad0 45 50 5f 52 65 64 75 63 65 64 20 20 20 20 30 78 EP_Reduced 0x
64ae0 32 30 30 30 20 20 2f 2a 20 45 78 70 72 20 73 74 2000 /* Expr st
64af0 72 75 63 74 20 69 73 20 45 58 50 52 5f 52 45 44 ruct is EXPR_RED
64b00 55 43 45 44 53 49 5a 45 20 62 79 74 65 73 20 6f UCEDSIZE bytes o
64b10 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 nly */.#define E
64b20 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 20 30 78 34 P_TokenOnly 0x4
64b30 30 30 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72 000 /* Expr str
64b40 75 63 74 20 69 73 20 45 58 50 52 5f 54 4f 4b 45 uct is EXPR_TOKE
64b50 4e 4f 4e 4c 59 53 49 5a 45 20 62 79 74 65 73 20 NONLYSIZE bytes
64b60 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 only */.#define
64b70 45 50 5f 53 74 61 74 69 63 20 20 20 20 20 30 78 EP_Static 0x
64b80 38 30 30 30 20 20 2f 2a 20 48 65 6c 64 20 69 6e 8000 /* Held in
64b90 20 6d 65 6d 6f 72 79 20 6e 6f 74 20 6f 62 74 61 memory not obta
64ba0 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 ined from malloc
64bb0 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 () */../*.** The
64bc0 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 following are t
64bd0 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 he meanings of b
64be0 69 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e its in the Expr.
64bf0 66 6c 61 67 73 32 20 66 69 65 6c 64 2e 0a 2a 2f flags2 field..*/
64c00 0a 23 64 65 66 69 6e 65 20 45 50 32 5f 4d 61 6c .#define EP2_Mal
64c10 6c 6f 63 65 64 54 6f 6b 65 6e 20 20 30 78 30 30 locedToken 0x00
64c20 30 31 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 73 01 /* Need to s
64c30 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 20 45 qlite3DbFree() E
64c40 78 70 72 2e 7a 54 6f 6b 65 6e 20 2a 2f 0a 23 64 xpr.zToken */.#d
64c50 65 66 69 6e 65 20 45 50 32 5f 49 72 72 65 64 75 efine EP2_Irredu
64c60 63 69 62 6c 65 20 20 20 20 30 78 30 30 30 32 20 cible 0x0002
64c70 20 2f 2a 20 43 61 6e 6e 6f 74 20 45 58 50 52 44 /* Cannot EXPRD
64c80 55 50 5f 52 45 44 55 43 45 20 74 68 69 73 20 45 UP_REDUCE this E
64c90 78 70 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 xpr */../*.** Th
64ca0 65 20 70 73 65 75 64 6f 2d 72 6f 75 74 69 6e 65 e pseudo-routine
64cb0 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 49 sqlite3ExprSetI
64cc0 72 72 65 64 75 63 69 62 6c 65 20 73 65 74 73 20 rreducible sets
64cd0 74 68 65 20 45 50 32 5f 49 72 72 65 64 75 63 69 the EP2_Irreduci
64ce0 62 6c 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 61 ble.** flag on a
64cf0 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73 74 72 n expression str
64d00 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 66 6c ucture. This fl
64d10 61 67 20 69 73 20 75 73 65 64 20 66 6f 72 20 56 ag is used for V
64d20 56 26 41 20 6f 6e 6c 79 2e 20 20 54 68 65 0a 2a V&A only. The.*
64d30 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6d 70 * routine is imp
64d40 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 6d 61 lemented as a ma
64d50 63 72 6f 20 74 68 61 74 20 6f 6e 6c 79 20 77 6f cro that only wo
64d60 72 6b 73 20 77 68 65 6e 20 69 6e 20 64 65 62 75 rks when in debu
64d70 67 67 69 6e 67 20 6d 6f 64 65 2c 0a 2a 2a 20 73 gging mode,.** s
64d80 6f 20 61 73 20 6e 6f 74 20 74 6f 20 62 75 72 64 o as not to burd
64d90 65 6e 20 70 72 6f 64 75 63 74 69 6f 6e 20 63 6f en production co
64da0 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 de..*/.#ifdef SQ
64db0 4c 49 54 45 5f 44 45 42 55 47 0a 23 20 64 65 66 LITE_DEBUG.# def
64dc0 69 6e 65 20 45 78 70 72 53 65 74 49 72 72 65 64 ine ExprSetIrred
64dd0 75 63 69 62 6c 65 28 58 29 20 20 28 58 29 2d 3e ucible(X) (X)->
64de0 66 6c 61 67 73 32 20 7c 3d 20 45 50 32 5f 49 72 flags2 |= EP2_Ir
64df0 72 65 64 75 63 69 62 6c 65 0a 23 65 6c 73 65 0a reducible.#else.
64e00 23 20 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 # define ExprSet
64e10 49 72 72 65 64 75 63 69 62 6c 65 28 58 29 0a 23 Irreducible(X).#
64e20 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
64e30 73 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 se macros can be
64e40 20 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73 used to test, s
64e50 65 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74 et, or clear bit
64e60 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 s in the .** Exp
64e70 72 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a r.flags field..*
64e80 2f 0a 23 64 65 66 69 6e 65 20 45 78 70 72 48 61 /.#define ExprHa
64e90 73 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 sProperty(E,P)
64ea0 20 20 20 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 (((E)->flags&
64eb0 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 (P))==(P)).#defi
64ec0 6e 65 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f ne ExprHasAnyPro
64ed0 70 65 72 74 79 28 45 2c 50 29 20 20 28 28 28 45 perty(E,P) (((E
64ee0 29 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30 )->flags&(P))!=0
64ef0 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 53 65 ).#define ExprSe
64f00 74 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 tProperty(E,P)
64f10 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 (E)->flags|=(
64f20 50 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 43 P).#define ExprC
64f30 6c 65 61 72 50 72 6f 70 65 72 74 79 28 45 2c 50 learProperty(E,P
64f40 29 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d ) (E)->flags&=
64f50 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 ~(P)../*.** Macr
64f60 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 os to determine
64f70 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
64f80 74 65 73 20 72 65 71 75 69 72 65 64 20 62 79 20 tes required by
64f90 61 20 6e 6f 72 6d 61 6c 20 45 78 70 72 20 0a 2a a normal Expr .*
64fa0 2a 20 73 74 72 75 63 74 2c 20 61 6e 20 45 78 70 * struct, an Exp
64fb0 72 20 73 74 72 75 63 74 20 77 69 74 68 20 74 68 r struct with th
64fc0 65 20 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61 e EP_Reduced fla
64fd0 67 20 73 65 74 20 69 6e 20 45 78 70 72 2e 66 6c g set in Expr.fl
64fe0 61 67 73 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 45 ags .** and an E
64ff0 78 70 72 20 73 74 72 75 63 74 20 77 69 74 68 20 xpr struct with
65000 74 68 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 the EP_TokenOnly
65010 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 64 flag set..*/.#d
65020 65 66 69 6e 65 20 45 58 50 52 5f 46 55 4c 4c 53 efine EXPR_FULLS
65030 49 5a 45 20 20 20 20 20 20 20 20 20 20 20 73 69 IZE si
65040 7a 65 6f 66 28 45 78 70 72 29 20 20 20 20 20 20 zeof(Expr)
65050 20 20 20 20 20 2f 2a 20 46 75 6c 6c 20 73 69 7a /* Full siz
65060 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 e */.#define EXP
65070 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 20 20 R_REDUCEDSIZE
65080 20 20 20 20 20 6f 66 66 73 65 74 6f 66 28 45 78 offsetof(Ex
65090 70 72 2c 69 54 61 62 6c 65 29 20 20 2f 2a 20 43 pr,iTable) /* C
650a0 6f 6d 6d 6f 6e 20 66 65 61 74 75 72 65 73 20 2a ommon features *
650b0 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f 54 /.#define EXPR_T
650c0 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 20 20 20 OKENONLYSIZE
650d0 20 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c offsetof(Expr,
650e0 70 4c 65 66 74 29 20 20 20 2f 2a 20 46 65 77 65 pLeft) /* Fewe
650f0 72 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 0a 2f r features */../
65100 2a 0a 2a 2a 20 46 6c 61 67 73 20 70 61 73 73 65 *.** Flags passe
65110 64 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 d to the sqlite3
65120 45 78 70 72 44 75 70 28 29 20 66 75 6e 63 74 69 ExprDup() functi
65130 6f 6e 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 on. See the head
65140 65 72 20 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20 61 er comment .** a
65150 62 6f 76 65 20 73 71 6c 69 74 65 33 45 78 70 72 bove sqlite3Expr
65160 44 75 70 28 29 20 66 6f 72 20 64 65 74 61 69 6c Dup() for detail
65170 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 s..*/.#define EX
65180 50 52 44 55 50 5f 52 45 44 55 43 45 20 20 20 20 PRDUP_REDUCE
65190 20 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 0x0001 /*
651a0 55 73 65 64 20 72 65 64 75 63 65 64 2d 73 69 7a Used reduced-siz
651b0 65 20 45 78 70 72 20 6e 6f 64 65 73 20 2a 2f 0a e Expr nodes */.
651c0 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 ./*.** A list of
651d0 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 45 expressions. E
651e0 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 6d ach expression m
651f0 61 79 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 68 61 ay optionally ha
65200 76 65 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20 20 41 ve a.** name. A
65210 6e 20 65 78 70 72 2f 6e 61 6d 65 20 63 6f 6d 62 n expr/name comb
65220 69 6e 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 ination can be u
65230 73 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 sed in several w
65240 61 79 73 2c 20 73 75 63 68 0a 2a 2a 20 61 73 20 ays, such.** as
65250 74 68 65 20 6c 69 73 74 20 6f 66 20 22 65 78 70 the list of "exp
65260 72 20 41 53 20 49 44 22 20 66 69 65 6c 64 73 20 r AS ID" fields
65270 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 22 53 45 4c following a "SEL
65280 45 43 54 22 20 6f 72 20 69 6e 20 74 68 65 0a 2a ECT" or in the.*
65290 2a 20 6c 69 73 74 20 6f 66 20 22 49 44 20 3d 20 * list of "ID =
652a0 65 78 70 72 22 20 69 74 65 6d 73 20 69 6e 20 61 expr" items in a
652b0 6e 20 55 50 44 41 54 45 2e 20 20 41 20 6c 69 73 n UPDATE. A lis
652c0 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 t of expressions
652d0 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 can.** also be
652e0 75 73 65 64 20 61 73 20 74 68 65 20 61 72 67 75 used as the argu
652f0 6d 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 ment to a functi
65300 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 on, in which cas
65310 65 20 74 68 65 20 61 2e 7a 4e 61 6d 65 0a 2a 2a e the a.zName.**
65320 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 75 73 field is not us
65330 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 ed..*/.struct Ex
65340 70 72 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20 6e prList {. int n
65350 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 Expr;
65360 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
65370 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 20 74 68 xpressions on th
65380 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 e list */. int
65390 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 nAlloc;
653a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
653b0 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 entries allocate
653c0 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 d below */. int
653d0 20 69 45 43 75 72 73 6f 72 3b 20 20 20 20 20 20 iECursor;
653e0 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73 /* VDBE Curs
653f0 6f 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 or associated wi
65400 74 68 20 74 68 69 73 20 45 78 70 72 4c 69 73 74 th this ExprList
65410 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 */. struct Exp
65420 72 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 rList_item {.
65430 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 Expr *pExpr;
65440 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c /* The l
65450 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f ist of expressio
65460 6e 73 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a ns */. char *
65470 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 zName;
65480 20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f 63 69 /* Token associ
65490 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65 ated with this e
654a0 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 xpression */.
654b0 20 63 68 61 72 20 2a 7a 53 70 61 6e 3b 20 20 20 char *zSpan;
654c0 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 /* Origi
654d0 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20 nal text of the
654e0 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 expression */.
654f0 20 20 75 38 20 73 6f 72 74 4f 72 64 65 72 3b 20 u8 sortOrder;
65500 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f /* 1 fo
65510 72 20 44 45 53 43 20 6f 72 20 30 20 66 6f 72 20 r DESC or 0 for
65520 41 53 43 20 2a 2f 0a 20 20 20 20 75 38 20 64 6f ASC */. u8 do
65530 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ne;
65540 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20 69 /* A flag to i
65550 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70 72 6f ndicate when pro
65560 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e 69 73 cessing is finis
65570 68 65 64 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 hed */. u16 i
65580 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 Col;
65590 20 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20 42 /* For ORDER B
655a0 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 Y, column number
655b0 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a in result set *
655c0 2f 0a 20 20 20 20 75 31 36 20 69 41 6c 69 61 73 /. u16 iAlias
655d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
655e0 49 6e 64 65 78 20 69 6e 74 6f 20 50 61 72 73 65 Index into Parse
655f0 2e 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a 4e .aAlias[] for zN
65600 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20 20 ame */. } *a;
65610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
65620 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 /* One entry for
65630 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e each expression
65640 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e */.};../*.** An
65650 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
65660 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 s structure is u
65670 73 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 sed by the parse
65680 72 20 74 6f 20 72 65 63 6f 72 64 20 62 6f 74 68 r to record both
65690 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 20 74 72 .** the parse tr
656a0 65 65 20 66 6f 72 20 61 6e 20 65 78 70 72 65 73 ee for an expres
656b0 73 69 6f 6e 20 61 6e 64 20 74 68 65 20 73 70 61 sion and the spa
656c0 6e 20 6f 66 20 69 6e 70 75 74 20 74 65 78 74 20 n of input text
656d0 66 6f 72 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 for an.** expres
656e0 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 sion..*/.struct
656f0 45 78 70 72 53 70 61 6e 20 7b 0a 20 20 45 78 70 ExprSpan {. Exp
65700 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 r *pExpr;
65710 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 /* The expres
65720 73 69 6f 6e 20 70 61 72 73 65 20 74 72 65 65 20 sion parse tree
65730 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
65740 2a 7a 53 74 61 72 74 3b 20 20 20 2f 2a 20 46 69 *zStart; /* Fi
65750 72 73 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 rst character of
65760 20 69 6e 70 75 74 20 74 65 78 74 20 2a 2f 0a 20 input text */.
65770 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e const char *zEn
65780 64 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 68 d; /* One ch
65790 61 72 61 63 74 65 72 20 70 61 73 74 20 74 68 65 aracter past the
657a0 20 65 6e 64 20 6f 66 20 69 6e 70 75 74 20 74 65 end of input te
657b0 78 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 xt */.};../*.**
657c0 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
657d0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 his structure ca
657e0 6e 20 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 n hold a simple
657f0 6c 69 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 list of identifi
65800 65 72 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 ers,.** such as
65810 74 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 the list "a,b,c"
65820 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e in the followin
65830 67 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a g statements:.**
65840 0a 2a 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 .** INSERT
65850 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 INTO t(a,b,c) VA
65860 4c 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 LUES ...;.**
65870 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 CREATE INDEX i
65880 64 78 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a dx ON t(a,b,c);.
65890 2a 2a 20 20 20 20 20 20 43 52 45 41 54 45 20 54 ** CREATE T
658a0 52 49 47 47 45 52 20 74 72 69 67 20 42 45 46 4f RIGGER trig BEFO
658b0 52 45 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 RE UPDATE ON t(a
658c0 2c 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a ,b,c) ...;.**.**
658d0 20 54 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 The IdList.a.id
658e0 78 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 x field is used
658f0 77 68 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 when the IdList
65900 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c represents the l
65910 69 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e ist of.** column
65920 20 6e 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 names after a t
65930 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 able name in an
65940 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 INSERT statement
65950 2e 20 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d . In the statem
65960 65 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e ent.**.** IN
65970 53 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c SERT INTO t(a,b,
65980 63 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 c) ....**.** If
65990 22 61 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 "a" is the k-th
659a0 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 column of table
659b0 22 74 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 "t", then IdList
659c0 2e 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f .a[0].idx==k..*/
659d0 0a 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b .struct IdList {
659e0 0a 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 . struct IdList
659f0 5f 69 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 _item {. char
65a00 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a *zName; /*
65a10 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 Name of the ide
65a20 6e 74 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 ntifier */. i
65a30 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 nt idx;
65a40 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d /* Index in som
65a50 65 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f e Table.aCol[] o
65a60 66 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 f a column named
65a70 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 zName */. } *a
65a80 3b 0a 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 ;. int nId;
65a90 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
65aa0 66 20 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e f identifiers on
65ab0 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 the list */. i
65ac0 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 nt nAlloc;
65ad0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
65ae0 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 ries allocated f
65af0 6f 72 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a or a[] below */.
65b00 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 };../*.** The bi
65b10 74 6d 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 tmask datatype d
65b20 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 efined below is
65b30 75 73 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 used for various
65b40 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a optimizations..
65b50 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 **.** Changing t
65b60 68 69 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 his from a 64-bi
65b70 74 20 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 t to a 32-bit ty
65b80 70 65 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 pe limits the nu
65b90 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 mber of.** table
65ba0 73 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 s in a join to 3
65bb0 32 20 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 2 instead of 64.
65bc0 20 20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 But it also re
65bd0 64 75 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a duces the size.*
65be0 2a 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 * of the library
65bf0 20 62 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e by 738 bytes on
65c00 20 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 ix86..*/.typede
65c10 66 20 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a f u64 Bitmask;..
65c20 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 /*.** The number
65c30 20 6f 66 20 62 69 74 73 20 69 6e 20 61 20 42 69 of bits in a Bi
65c40 74 6d 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65 tmask. "BMS" me
65c50 61 6e 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a ans "BitMask Siz
65c60 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 e"..*/.#define B
65c70 4d 53 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f MS ((int)(sizeo
65c80 66 28 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a f(Bitmask)*8))..
65c90 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
65ca0 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 64 65 ing structure de
65cb0 73 63 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d scribes the FROM
65cc0 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c clause of a SEL
65cd0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a ECT statement..*
65ce0 2a 20 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 * Each table or
65cf0 73 75 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 subquery in the
65d00 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 FROM clause is a
65d10 20 73 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e separate elemen
65d20 74 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c t of.** the SrcL
65d30 69 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a ist.a[] array..*
65d40 2a 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 *.** With the ad
65d50 64 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 dition of multip
65d60 6c 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 le database supp
65d70 6f 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 ort, the followi
65d80 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 ng structure.**
65d90 63 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 can also be used
65da0 20 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 to describe a p
65db0 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 articular table
65dc0 73 75 63 68 20 61 73 20 74 68 65 20 74 61 62 6c such as the tabl
65dd0 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 e that.** is mod
65de0 69 66 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 ified by an INSE
65df0 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 RT, DELETE, or U
65e00 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e PDATE statement.
65e10 20 20 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 In standard SQ
65e20 4c 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 L,.** such a tab
65e30 6c 65 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d le must be a sim
65e40 70 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 ple name: ID. B
65e50 75 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 ut in SQLite, th
65e60 65 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e e table can.** n
65e70 6f 77 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 ow be identified
65e80 20 62 79 20 61 20 64 61 74 61 62 61 73 65 20 6e by a database n
65e90 61 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e ame, a dot, then
65ea0 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a the table name:
65eb0 20 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 ID.ID..**.** Th
65ec0 65 20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 e jointype start
65ed0 73 20 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 s out showing th
65ee0 65 20 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 e join type betw
65ef0 65 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 een the current
65f00 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 table.** and the
65f10 20 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 next table on t
65f20 68 65 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 he list. The pa
65f30 72 73 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 rser builds the
65f40 6c 69 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a list this way..*
65f50 2a 20 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 * But sqlite3Src
65f60 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 ListShiftJoinTyp
65f70 65 28 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 e() later shifts
65f80 20 74 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 the jointypes s
65f90 6f 20 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a o that each.** j
65fa0 6f 69 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 ointype expresse
65fb0 73 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 s the join betwe
65fc0 65 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 en the table and
65fd0 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 the previous ta
65fe0 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 ble..*/.struct S
65ff0 72 63 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e rcList {. i16 n
66000 53 72 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e Src; /* N
66010 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 umber of tables
66020 6f 72 20 73 75 62 71 75 65 72 69 65 73 20 69 6e or subqueries in
66030 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 the FROM clause
66040 20 2a 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 */. i16 nAlloc
66050 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 ; /* Number
66060 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f of entries allo
66070 63 61 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c cated in a[] bel
66080 6f 77 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 ow */. struct S
66090 72 63 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 rcList_item {.
660a0 20 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 char *zDatabas
660b0 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 e; /* Name of d
660c0 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 atabase holding
660d0 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 this table */.
660e0 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
660f0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
66100 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 he table */.
66110 63 68 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 char *zAlias;
66120 20 20 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 /* The "B" par
66130 74 20 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 t of a "A AS B"
66140 70 68 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 phrase. zName i
66150 73 20 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 s the "A" */.
66160 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 Table *pTab;
66170 20 20 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 /* An SQL tab
66180 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 le corresponding
66190 20 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 to zName */.
661a0 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 Select *pSelect
661b0 3b 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 ; /* A SELECT s
661c0 74 61 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e tatement used in
661d0 20 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c place of a tabl
661e0 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 e name */. u8
661f0 20 69 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 isPopulated;
66200 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 /* Temporary tab
66210 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 le associated wi
66220 74 68 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 th SELECT is pop
66230 75 6c 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 ulated */. u8
66240 20 6a 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 jointype;
66250 2f 2a 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 /* Type of join
66260 62 65 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c between this abl
66270 65 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f e and the previo
66280 75 73 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74 us */. u8 not
66290 49 6e 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54 Indexed; /* T
662a0 72 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20 rue if there is
662b0 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c a NOT INDEXED cl
662c0 61 75 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ause */. int
662d0 69 43 75 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a iCursor; /*
662e0 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 The VDBE cursor
662f0 20 6e 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20 number used to
66300 61 63 63 65 73 73 20 74 68 69 73 20 74 61 62 6c access this tabl
66310 65 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 e */. Expr *p
66320 4f 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 On; /* Th
66330 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 e ON clause of a
66340 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c join */. IdL
66350 69 73 74 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f ist *pUsing; /
66360 2a 20 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 * The USING clau
66370 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a se of a join */.
66380 20 20 20 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55 Bitmask colU
66390 73 65 64 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28 sed; /* Bit N (
663a0 31 3c 3c 4e 29 20 73 65 74 20 69 66 20 63 6f 6c 1<<N) set if col
663b0 75 6d 6e 20 4e 20 6f 66 20 70 54 61 62 20 69 73 umn N of pTab is
663c0 20 75 73 65 64 20 2a 2f 0a 20 20 20 20 63 68 61 used */. cha
663d0 72 20 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f r *zIndex; /
663e0 2a 20 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f * Identifier fro
663f0 6d 20 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a m "INDEXED BY <z
66400 49 6e 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a Index>" clause *
66410 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e /. Index *pIn
66420 64 65 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 dex; /* Index
66430 20 73 74 72 75 63 74 75 72 65 20 63 6f 72 72 65 structure corre
66440 73 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64 sponding to zInd
66450 65 78 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 ex, if any */.
66460 7d 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 } a[1];
66470 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 /* One entry
66480 20 66 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 for each identi
66490 66 69 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 fier on the list
664a0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 */.};../*.** Pe
664b0 72 6d 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f rmitted values o
664c0 66 20 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e f the SrcList.a.
664d0 6a 6f 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a jointype field.*
664e0 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e /.#define JT_INN
664f0 45 52 20 20 20 20 20 30 78 30 30 30 31 20 20 20 ER 0x0001
66500 20 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 /* Any kind of
66510 69 6e 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a inner or cross j
66520 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a oin */.#define J
66530 54 5f 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 T_CROSS 0x00
66540 30 32 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 02 /* Explici
66550 74 20 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f t use of the CRO
66560 53 53 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 SS keyword */.#d
66570 65 66 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c efine JT_NATURAL
66580 20 20 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 0x0004 /*
66590 54 72 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 True for a "natu
665a0 72 61 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 ral" join */.#de
665b0 66 69 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 fine JT_LEFT
665c0 20 20 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 0x0008 /* L
665d0 65 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a eft outer join *
665e0 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 /.#define JT_RIG
665f0 48 54 20 20 20 20 20 30 78 30 30 31 30 20 20 20 HT 0x0010
66600 20 2f 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 /* Right outer
66610 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 join */.#define
66620 4a 54 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 JT_OUTER 0x0
66630 30 32 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 020 /* The "O
66640 55 54 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 UTER" keyword is
66650 20 70 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 present */.#def
66660 69 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 ine JT_ERROR
66670 20 30 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 0x0040 /* un
66680 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f known or unsuppo
66690 72 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a rted join type *
666a0 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 /.../*.** A Wher
666b0 65 50 6c 61 6e 20 6f 62 6a 65 63 74 20 68 6f 6c ePlan object hol
666c0 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 ds information t
666d0 68 61 74 20 64 65 73 63 72 69 62 65 73 20 61 20 hat describes a
666e0 6c 6f 6f 6b 75 70 0a 2a 2a 20 73 74 72 61 74 65 lookup.** strate
666f0 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f gy..**.** This o
66700 62 6a 65 63 74 20 69 73 20 69 6e 74 65 6e 64 65 bject is intende
66710 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 6f d to be opaque o
66720 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 77 68 utside of the wh
66730 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a ere.c module..**
66740 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 It is included
66750 68 65 72 65 20 6f 6e 6c 79 20 73 6f 20 74 68 61 here only so tha
66760 74 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 20 t that compiler
66770 77 69 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20 62 69 will know how bi
66780 67 20 69 74 0a 2a 2a 20 69 73 2e 20 20 4e 6f 6e g it.** is. Non
66790 65 20 6f 66 20 74 68 65 20 66 69 65 6c 64 73 20 e of the fields
667a0 69 6e 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 in this object s
667b0 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 6f 75 hould be used ou
667c0 74 73 69 64 65 20 6f 66 0a 2a 2a 20 74 68 65 20 tside of.** the
667d0 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a where.c module..
667e0 2a 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 65 **.** Within the
667f0 20 75 6e 69 6f 6e 2c 20 70 49 64 78 20 69 73 20 union, pIdx is
66800 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 only used when w
66810 73 46 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 44 sFlags&WHERE_IND
66820 45 58 45 44 20 69 73 20 74 72 75 65 2e 0a 2a 2a EXED is true..**
66830 20 70 54 65 72 6d 20 69 73 20 6f 6e 6c 79 20 75 pTerm is only u
66840 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67 73 sed when wsFlags
66850 26 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 &WHERE_MULTI_OR
66860 69 73 20 74 72 75 65 2e 20 20 41 6e 64 20 70 56 is true. And pV
66870 74 61 62 49 64 78 0a 2a 2a 20 69 73 20 6f 6e 6c tabIdx.** is onl
66880 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c y used when wsFl
66890 61 67 73 26 57 48 45 52 45 5f 56 49 52 54 55 41 ags&WHERE_VIRTUA
668a0 4c 54 41 42 4c 45 20 69 73 20 74 72 75 65 2e 20 LTABLE is true.
668b0 20 49 74 20 69 73 20 6e 65 76 65 72 20 74 68 65 It is never the
668c0 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20 6d 6f .** case that mo
668d0 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 re than one of t
668e0 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 hese conditions
668f0 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 72 75 is true..*/.stru
66900 63 74 20 57 68 65 72 65 50 6c 61 6e 20 7b 0a 20 ct WherePlan {.
66910 20 75 33 32 20 77 73 46 6c 61 67 73 3b 20 20 20 u32 wsFlags;
66920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
66930 2f 2a 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73 /* WHERE_* flags
66940 20 74 68 61 74 20 64 65 73 63 72 69 62 65 20 74 that describe t
66950 68 65 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 he strategy */.
66960 20 75 33 32 20 6e 45 71 3b 20 20 20 20 20 20 20 u32 nEq;
66970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
66980 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 /* Number of ==
66990 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 constraints */.
669a0 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 49 6e 64 union {. Ind
669b0 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 ex *pIdx;
669c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
669d0 6e 64 65 78 20 77 68 65 6e 20 57 48 45 52 45 5f ndex when WHERE_
669e0 49 4e 44 45 58 45 44 20 69 73 20 74 72 75 65 20 INDEXED is true
669f0 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 57 68 */. struct Wh
66a00 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 ereTerm *pTerm;
66a10 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 /* WHERE c
66a20 6c 61 75 73 65 20 74 65 72 6d 20 66 6f 72 20 4f lause term for O
66a30 52 2d 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 20 R-search */.
66a40 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e sqlite3_index_in
66a50 66 6f 20 2a 70 56 74 61 62 49 64 78 3b 20 20 2f fo *pVtabIdx; /
66a60 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 * Virtual table
66a70 69 6e 64 65 78 20 74 6f 20 75 73 65 20 2a 2f 0a index to use */.
66a80 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a } u;.};../*.**
66a90 20 46 6f 72 20 65 61 63 68 20 6e 65 73 74 65 64 For each nested
66aa0 20 6c 6f 6f 70 20 69 6e 20 61 20 57 48 45 52 45 loop in a WHERE
66ab0 20 63 6c 61 75 73 65 20 69 6d 70 6c 65 6d 65 6e clause implemen
66ac0 74 61 74 69 6f 6e 2c 20 74 68 65 20 57 68 65 72 tation, the Wher
66ad0 65 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 eInfo.** structu
66ae0 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 re contains a si
66af0 6e 67 6c 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 ngle instance of
66b00 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e this structure.
66b10 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 This structure
66b20 0a 2a 2a 20 69 73 20 69 6e 74 65 6e 64 65 64 20 .** is intended
66b30 74 6f 20 62 65 20 70 72 69 76 61 74 65 20 74 68 to be private th
66b40 65 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f e the where.c mo
66b50 64 75 6c 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 dule and should
66b60 6e 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 not be.** access
66b70 20 6f 72 20 6d 6f 64 69 66 69 65 64 20 62 79 20 or modified by
66b80 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 2a other modules..*
66b90 2a 0a 2a 2a 20 54 68 65 20 70 49 64 78 49 6e 66 *.** The pIdxInf
66ba0 6f 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 o field is used
66bb0 74 6f 20 68 65 6c 70 20 70 69 63 6b 20 74 68 65 to help pick the
66bc0 20 62 65 73 74 20 69 6e 64 65 78 20 6f 6e 20 61 best index on a
66bd0 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c .** virtual tabl
66be0 65 2e 20 20 54 68 65 20 70 49 64 78 49 6e 66 6f e. The pIdxInfo
66bf0 20 70 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69 6e pointer contain
66c00 73 20 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 69 6e s indexing.** in
66c10 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 formation for th
66c20 65 20 69 2d 74 68 20 74 61 62 6c 65 20 69 6e 20 e i-th table in
66c30 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 the FROM clause
66c40 62 65 66 6f 72 65 20 72 65 6f 72 64 65 72 69 6e before reorderin
66c50 67 2e 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70 49 g..** All the pI
66c60 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73 20 dxInfo pointers
66c70 61 72 65 20 66 72 65 65 64 20 62 79 20 77 68 65 are freed by whe
66c80 72 65 49 6e 66 6f 46 72 65 65 28 29 20 69 6e 20 reInfoFree() in
66c90 77 68 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c 6c 20 where.c..** All
66ca0 6f 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f other informatio
66cb0 6e 20 69 6e 20 74 68 65 20 69 2d 74 68 20 57 68 n in the i-th Wh
66cc0 65 72 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74 20 ereLevel object
66cd0 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 for the i-th tab
66ce0 6c 65 0a 2a 2a 20 61 66 74 65 72 20 46 52 4f 4d le.** after FROM
66cf0 20 63 6c 61 75 73 65 20 6f 72 64 65 72 69 6e 67 clause ordering
66d00 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 ..*/.struct Wher
66d10 65 4c 65 76 65 6c 20 7b 0a 20 20 57 68 65 72 65 eLevel {. Where
66d20 50 6c 61 6e 20 70 6c 61 6e 3b 20 20 20 20 20 20 Plan plan;
66d30 20 2f 2a 20 71 75 65 72 79 20 70 6c 61 6e 20 66 /* query plan f
66d40 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 or this element
66d50 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 of the FROM clau
66d60 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 66 se */. int iLef
66d70 74 4a 6f 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a tJoin; /*
66d80 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 Memory cell use
66d90 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 4c d to implement L
66da0 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 2a EFT OUTER JOIN *
66db0 2f 0a 20 20 69 6e 74 20 69 54 61 62 43 75 72 3b /. int iTabCur;
66dc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
66dd0 20 56 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 VDBE cursor use
66de0 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 d to access the
66df0 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 table */. int i
66e00 49 64 78 43 75 72 3b 20 20 20 20 20 20 20 20 20 IdxCur;
66e10 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 /* The VDBE cur
66e20 73 6f 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 sor used to acce
66e30 73 73 20 70 49 64 78 20 2a 2f 0a 20 20 69 6e 74 ss pIdx */. int
66e40 20 61 64 64 72 42 72 6b 3b 20 20 20 20 20 20 20 addrBrk;
66e50 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 /* Jump here
66e60 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 to break out of
66e70 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e the loop */. in
66e80 74 20 61 64 64 72 4e 78 74 3b 20 20 20 20 20 20 t addrNxt;
66e90 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 /* Jump here
66ea0 20 74 6f 20 73 74 61 72 74 20 74 68 65 20 6e 65 to start the ne
66eb0 78 74 20 49 4e 20 63 6f 6d 62 69 6e 61 74 69 6f xt IN combinatio
66ec0 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 n */. int addrC
66ed0 6f 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ont; /*
66ee0 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e Jump here to con
66ef0 74 69 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e tinue with the n
66f00 65 78 74 20 6c 6f 6f 70 20 63 79 63 6c 65 20 2a ext loop cycle *
66f10 2f 0a 20 20 69 6e 74 20 61 64 64 72 46 69 72 73 /. int addrFirs
66f20 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 t; /* Fir
66f30 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f st instruction o
66f40 66 20 69 6e 74 65 72 69 6f 72 20 6f 66 20 74 68 f interior of th
66f50 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38 20 69 e loop */. u8 i
66f60 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 From;
66f70 20 20 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 /* Which entry
66f80 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 in the FROM cla
66f90 75 73 65 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 use */. u8 op,
66fa0 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f p5; /
66fb0 2a 20 4f 70 63 6f 64 65 20 61 6e 64 20 50 35 20 * Opcode and P5
66fc0 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68 of the opcode th
66fd0 61 74 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 at ends the loop
66fe0 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20 70 32 */. int p1, p2
66ff0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f ; /* O
67000 70 65 72 61 6e 64 73 20 6f 66 20 74 68 65 20 6f perands of the o
67010 70 63 6f 64 65 20 75 73 65 64 20 74 6f 20 65 6e pcode used to en
67020 64 73 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 ds the loop */.
67030 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 union {
67040 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d /* Inform
67050 61 74 69 6f 6e 20 74 68 61 74 20 64 65 70 65 6e ation that depen
67060 64 73 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 ds on plan.wsFla
67070 67 73 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 gs */. struct
67080 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 6e {. int nIn
67090 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
670a0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
670b0 69 65 73 20 69 6e 20 61 49 6e 4c 6f 6f 70 5b 5d ies in aInLoop[]
670c0 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 */. struct
670d0 20 49 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20 20 20 InLoop {.
670e0 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20 int iCur;
670f0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
67100 56 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 VDBE cursor used
67110 20 62 79 20 74 68 69 73 20 49 4e 20 6f 70 65 72 by this IN oper
67120 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 ator */.
67130 69 6e 74 20 61 64 64 72 49 6e 54 6f 70 3b 20 20 int addrInTop;
67140 20 20 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 /* Top of
67150 20 74 68 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f 0a the IN loop */.
67160 20 20 20 20 20 20 7d 20 2a 61 49 6e 4c 6f 6f 70 } *aInLoop
67170 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 ; /* I
67180 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
67190 20 65 61 63 68 20 6e 65 73 74 65 64 20 49 4e 20 each nested IN
671a0 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 operator */.
671b0 7d 20 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 } in;
671c0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 /* Used wh
671d0 65 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 26 en plan.wsFlags&
671e0 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 2a 2f WHERE_IN_ABLE */
671f0 0a 20 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 54 68 . } u;.. /* Th
67200 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c e following fiel
67210 64 20 69 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 d is really not
67220 70 61 72 74 20 6f 66 20 74 68 65 20 63 75 72 72 part of the curr
67230 65 6e 74 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a ent level. But.
67240 20 20 2a 2a 20 77 65 20 6e 65 65 64 20 61 20 70 ** we need a p
67250 6c 61 63 65 20 74 6f 20 63 61 63 68 65 20 76 69 lace to cache vi
67260 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 64 65 rtual table inde
67270 78 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f x information fo
67280 72 20 65 61 63 68 0a 20 20 2a 2a 20 76 69 72 74 r each. ** virt
67290 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 74 68 65 ual table in the
672a0 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e 64 FROM clause and
672b0 20 74 68 65 20 57 68 65 72 65 4c 65 76 65 6c 20 the WhereLevel
672c0 73 74 72 75 63 74 75 72 65 20 69 73 0a 20 20 2a structure is. *
672d0 2a 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 70 * a convenient p
672e0 6c 61 63 65 20 73 69 6e 63 65 20 74 68 65 72 65 lace since there
672f0 20 69 73 20 6f 6e 65 20 57 68 65 72 65 4c 65 76 is one WhereLev
67300 65 6c 20 66 6f 72 20 65 61 63 68 20 46 52 4f 4d el for each FROM
67310 20 63 6c 61 75 73 65 0a 20 20 2a 2a 20 65 6c 65 clause. ** ele
67320 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c ment.. */. sql
67330 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 ite3_index_info
67340 2a 70 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20 49 *pIdxInfo; /* I
67350 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e 2d ndex info for n-
67360 74 68 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 th source table
67370 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 */.};../*.** Fla
67380 67 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 gs appropriate f
67390 6f 72 20 74 68 65 20 77 63 74 72 6c 46 6c 61 67 or the wctrlFlag
673a0 73 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 s parameter of s
673b0 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e qlite3WhereBegin
673c0 28 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 ().** and the Wh
673d0 65 72 65 49 6e 66 6f 2e 77 63 74 72 6c 46 6c 61 ereInfo.wctrlFla
673e0 67 73 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 gs member..*/.#d
673f0 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 efine WHERE_ORDE
67400 52 42 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78 30 RBY_NORMAL 0x0
67410 30 30 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 000 /* No-op */.
67420 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 #define WHERE_OR
67430 44 45 52 42 59 5f 4d 49 4e 20 20 20 20 20 20 30 DERBY_MIN 0
67440 78 30 30 30 31 20 2f 2a 20 4f 52 44 45 52 20 42 x0001 /* ORDER B
67450 59 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 Y processing for
67460 20 6d 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a 23 min() func */.#
67470 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 define WHERE_ORD
67480 45 52 42 59 5f 4d 41 58 20 20 20 20 20 20 30 78 ERBY_MAX 0x
67490 30 30 30 32 20 2f 2a 20 4f 52 44 45 52 20 42 59 0002 /* ORDER BY
674a0 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 processing for
674b0 6d 61 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 max() func */.#d
674c0 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 50 efine WHERE_ONEP
674d0 41 53 53 5f 44 45 53 49 52 45 44 20 20 30 78 30 ASS_DESIRED 0x0
674e0 30 30 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20 64 004 /* Want to d
674f0 6f 20 6f 6e 65 2d 70 61 73 73 20 55 50 44 41 54 o one-pass UPDAT
67500 45 2f 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65 66 E/DELETE */.#def
67510 69 6e 65 20 57 48 45 52 45 5f 44 55 50 4c 49 43 ine WHERE_DUPLIC
67520 41 54 45 53 5f 4f 4b 20 20 20 20 30 78 30 30 30 ATES_OK 0x000
67530 38 20 2f 2a 20 4f 6b 20 74 6f 20 72 65 74 75 72 8 /* Ok to retur
67540 6e 20 61 20 72 6f 77 20 6d 6f 72 65 20 74 68 61 n a row more tha
67550 6e 20 6f 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e n once */.#defin
67560 65 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 e WHERE_OMIT_OPE
67570 4e 20 20 20 20 20 20 20 20 30 78 30 30 31 30 20 N 0x0010
67580 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 /* Table cursor
67590 61 72 65 20 61 6c 72 65 61 64 79 20 6f 70 65 6e are already open
675a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 */.#define WHER
675b0 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 20 20 20 20 E_OMIT_CLOSE
675c0 20 20 20 30 78 30 30 32 30 20 2f 2a 20 4f 6d 69 0x0020 /* Omi
675d0 74 20 63 6c 6f 73 65 20 6f 66 20 74 61 62 6c 65 t close of table
675e0 20 26 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73 & index cursors
675f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 */.#define WHER
67600 45 5f 46 4f 52 43 45 5f 54 41 42 4c 45 20 20 20 E_FORCE_TABLE
67610 20 20 20 30 78 30 30 34 30 20 2f 2a 20 44 6f 20 0x0040 /* Do
67620 6e 6f 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 not use an index
67630 2d 6f 6e 6c 79 20 73 65 61 72 63 68 20 2a 2f 0a -only search */.
67640 0a 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 ./*.** The WHERE
67650 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 clause processi
67660 6e 67 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 ng routine has t
67670 77 6f 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a wo halves. The.
67680 2a 2a 20 66 69 72 73 74 20 70 61 72 74 20 64 6f ** first part do
67690 65 73 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 es the start of
676a0 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 the WHERE loop a
676b0 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a nd the second.**
676c0 20 68 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74 half does the t
676d0 61 69 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45 ail of the WHERE
676e0 20 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61 loop. An insta
676f0 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 nce of.** this s
67700 74 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75 tructure is retu
67710 72 6e 65 64 20 62 79 20 74 68 65 20 66 69 72 73 rned by the firs
67720 74 20 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65 t half and passe
67730 64 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65 d.** into the se
67740 63 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76 cond half to giv
67750 65 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74 e some continuit
67760 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 y..*/.struct Whe
67770 72 65 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65 reInfo {. Parse
67780 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 *pParse;
67790 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 /* Parsing and c
677a0 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 ode generating c
677b0 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 75 31 36 20 ontext */. u16
677c0 77 63 74 72 6c 46 6c 61 67 73 3b 20 20 20 20 20 wctrlFlags;
677d0 20 2f 2a 20 46 6c 61 67 73 20 6f 72 69 67 69 6e /* Flags origin
677e0 61 6c 6c 79 20 70 61 73 73 65 64 20 74 6f 20 73 ally passed to s
677f0 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e qlite3WhereBegin
67800 28 29 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f 6e 65 () */. u8 okOne
67810 50 61 73 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 Pass; /*
67820 4f 6b 20 74 6f 20 75 73 65 20 6f 6e 65 2d 70 61 Ok to use one-pa
67830 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 ss algorithm for
67840 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 UPDATE or DELET
67850 45 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a E */. SrcList *
67860 70 54 61 62 4c 69 73 74 3b 20 20 20 20 20 20 20 pTabList;
67870 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 /* List of
67880 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a tables in the j
67890 6f 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f oin */. int iTo
678a0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
678b0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 /* The v
678c0 65 72 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 ery beginning of
678d0 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 the WHERE loop
678e0 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e */. int iContin
678f0 75 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ue;
67900 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 /* Jump here
67910 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 to continue wit
67920 68 20 6e 65 78 74 20 72 65 63 6f 72 64 20 2a 2f h next record */
67930 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 3b 20 20 . int iBreak;
67940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67950 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 /* Jump here t
67960 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 o break out of t
67970 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 he loop */. int
67980 20 6e 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20 nLevel;
67990 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
679a0 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 umber of nested
679b0 6c 6f 6f 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 loop */. struct
679c0 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 WhereClause *pW
679d0 43 3b 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f C; /* Deco
679e0 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 mposition of the
679f0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f WHERE clause */
67a00 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20 61 5b . WhereLevel a[
67a10 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 1];
67a20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e /* Information
67a30 20 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 about each nest
67a40 20 6c 6f 6f 70 20 69 6e 20 57 48 45 52 45 20 2a loop in WHERE *
67a50 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 61 /.};../*.** A Na
67a60 6d 65 43 6f 6e 74 65 78 74 20 64 65 66 69 6e 65 meContext define
67a70 73 20 61 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 s a context in w
67a80 68 69 63 68 20 74 6f 20 72 65 73 6f 6c 76 65 20 hich to resolve
67a90 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e table and column
67aa0 0a 2a 2a 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 .** names. The
67ab0 63 6f 6e 74 65 78 74 20 63 6f 6e 73 69 73 74 73 context consists
67ac0 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 20 74 61 of a list of ta
67ad0 62 6c 65 73 20 28 74 68 65 20 70 53 72 63 4c 69 bles (the pSrcLi
67ae0 73 74 29 20 66 69 65 6c 64 20 61 6e 64 0a 2a 2a st) field and.**
67af0 20 61 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 a list of named
67b00 20 65 78 70 72 65 73 73 69 6f 6e 20 28 70 45 4c expression (pEL
67b10 69 73 74 29 2e 20 20 54 68 65 20 6e 61 6d 65 64 ist). The named
67b20 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
67b30 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2e may.** be NULL.
67b40 20 20 54 68 65 20 70 53 72 63 20 63 6f 72 72 65 The pSrc corre
67b50 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 46 52 sponds to the FR
67b60 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 OM clause of a S
67b70 45 4c 45 43 54 20 6f 72 0a 2a 2a 20 74 6f 20 74 ELECT or.** to t
67b80 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6f he table being o
67b90 70 65 72 61 74 65 64 20 6f 6e 20 62 79 20 49 4e perated on by IN
67ba0 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 SERT, UPDATE, or
67bb0 20 44 45 4c 45 54 45 2e 20 20 54 68 65 0a 2a 2a DELETE. The.**
67bc0 20 70 45 4c 69 73 74 20 63 6f 72 72 65 73 70 6f pEList correspo
67bd0 6e 64 73 20 74 6f 20 74 68 65 20 72 65 73 75 6c nds to the resul
67be0 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 t set of a SELEC
67bf0 54 20 61 6e 64 20 69 73 20 4e 55 4c 4c 20 66 6f T and is NULL fo
67c00 72 0a 2a 2a 20 6f 74 68 65 72 20 73 74 61 74 65 r.** other state
67c10 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 61 6d ments..**.** Nam
67c20 65 43 6f 6e 74 65 78 74 73 20 63 61 6e 20 62 65 eContexts can be
67c30 20 6e 65 73 74 65 64 2e 20 20 57 68 65 6e 20 72 nested. When r
67c40 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 2c 20 esolving names,
67c50 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 0a the inner-most .
67c60 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 73 65 ** context is se
67c70 61 72 63 68 65 64 20 66 69 72 73 74 2e 20 20 49 arched first. I
67c80 66 20 6e 6f 20 6d 61 74 63 68 20 69 73 20 66 6f f no match is fo
67c90 75 6e 64 2c 20 74 68 65 20 6e 65 78 74 20 6f 75 und, the next ou
67ca0 74 65 72 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 ter.** context i
67cb0 73 20 63 68 65 63 6b 65 64 2e 20 20 49 66 20 74 s checked. If t
67cc0 68 65 72 65 20 69 73 20 73 74 69 6c 6c 20 6e 6f here is still no
67cd0 20 6d 61 74 63 68 2c 20 74 68 65 20 6e 65 78 74 match, the next
67ce0 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 69 73 20 63 context.** is c
67cf0 68 65 63 6b 65 64 2e 20 20 54 68 69 73 20 70 72 hecked. This pr
67d00 6f 63 65 73 73 20 63 6f 6e 74 69 6e 75 65 73 20 ocess continues
67d10 75 6e 74 69 6c 20 65 69 74 68 65 72 20 61 20 6d until either a m
67d20 61 74 63 68 20 69 73 20 66 6f 75 6e 64 0a 2a 2a atch is found.**
67d30 20 6f 72 20 61 6c 6c 20 63 6f 6e 74 65 78 74 73 or all contexts
67d40 20 61 72 65 20 63 68 65 63 6b 2e 20 20 57 68 65 are check. Whe
67d50 6e 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 n a match is fou
67d60 6e 64 2c 20 74 68 65 20 6e 52 65 66 20 6d 65 6d nd, the nRef mem
67d70 62 65 72 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f ber of.** the co
67d80 6e 74 65 78 74 20 63 6f 6e 74 61 69 6e 69 6e 67 ntext containing
67d90 20 74 68 65 20 6d 61 74 63 68 20 69 73 20 69 6e the match is in
67da0 63 72 65 6d 65 6e 74 65 64 2e 20 0a 2a 2a 0a 2a cremented. .**.*
67db0 2a 20 45 61 63 68 20 73 75 62 71 75 65 72 79 20 * Each subquery
67dc0 67 65 74 73 20 61 20 6e 65 77 20 4e 61 6d 65 43 gets a new NameC
67dd0 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 70 4e 65 ontext. The pNe
67de0 78 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 xt field points
67df0 74 6f 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f to the.** NameCo
67e00 6e 74 65 78 74 20 69 6e 20 74 68 65 20 70 61 72 ntext in the par
67e10 65 6e 74 20 71 75 65 72 79 2e 20 20 54 68 75 73 ent query. Thus
67e20 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 the process of
67e30 73 63 61 6e 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 scanning the.**
67e40 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6c 69 73 74 NameContext list
67e50 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 corresponds to
67e60 73 65 61 72 63 68 69 6e 67 20 74 68 72 6f 75 67 searching throug
67e70 68 20 73 75 63 63 65 73 73 69 76 65 6c 79 20 6f h successively o
67e80 75 74 65 72 0a 2a 2a 20 73 75 62 71 75 65 72 69 uter.** subqueri
67e90 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 es looking for a
67ea0 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 72 75 63 match..*/.struc
67eb0 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 7b 0a t NameContext {.
67ec0 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b Parse *pParse;
67ed0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 /* The pa
67ee0 72 73 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69 73 rser */. SrcLis
67ef0 74 20 2a 70 53 72 63 4c 69 73 74 3b 20 20 20 2f t *pSrcList; /
67f00 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 61 * One or more ta
67f10 62 6c 65 73 20 75 73 65 64 20 74 6f 20 72 65 73 bles used to res
67f20 6f 6c 76 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 olve names */.
67f30 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 ExprList *pEList
67f40 3b 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c ; /* Optional
67f50 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 list of named e
67f60 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 xpressions */.
67f70 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 int nRef;
67f80 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
67f90 66 20 6e 61 6d 65 73 20 72 65 73 6f 6c 76 65 64 f names resolved
67fa0 20 62 79 20 74 68 69 73 20 63 6f 6e 74 65 78 74 by this context
67fb0 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 */. int nErr;
67fc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
67fd0 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 mber of errors e
67fe0 6e 63 6f 75 6e 74 65 72 65 64 20 77 68 69 6c 65 ncountered while
67ff0 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 resolving names
68000 20 2a 2f 0a 20 20 75 38 20 61 6c 6c 6f 77 41 67 */. u8 allowAg
68010 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 g; /* Ag
68020 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
68030 73 20 61 6c 6c 6f 77 65 64 20 68 65 72 65 20 2a s allowed here *
68040 2f 0a 20 20 75 38 20 68 61 73 41 67 67 3b 20 20 /. u8 hasAgg;
68050 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
68060 20 69 66 20 61 67 67 72 65 67 61 74 65 73 20 61 if aggregates a
68070 72 65 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20 re seen */. u8
68080 69 73 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20 isCheck;
68090 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65 73 /* True if res
680a0 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 olving names in
680b0 61 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 a CHECK constrai
680c0 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 65 70 nt */. int nDep
680d0 74 68 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 th; /*
680e0 44 65 70 74 68 20 6f 66 20 73 75 62 71 75 65 72 Depth of subquer
680f0 79 20 72 65 63 75 72 73 69 6f 6e 2e 20 31 20 66 y recursion. 1 f
68100 6f 72 20 6e 6f 20 72 65 63 75 72 73 69 6f 6e 20 or no recursion
68110 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 */. AggInfo *pA
68120 67 67 49 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e 66 ggInfo; /* Inf
68130 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 ormation about a
68140 67 67 72 65 67 61 74 65 73 20 61 74 20 74 68 69 ggregates at thi
68150 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e 61 6d s level */. Nam
68160 65 43 6f 6e 74 65 78 74 20 2a 70 4e 65 78 74 3b eContext *pNext;
68170 20 20 2f 2a 20 4e 65 78 74 20 6f 75 74 65 72 20 /* Next outer
68180 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20 4e name context. N
68190 55 4c 4c 20 66 6f 72 20 6f 75 74 65 72 6d 6f 73 ULL for outermos
681a0 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 t */.};../*.** A
681b0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
681c0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
681d0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 cture contains a
681e0 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a ll information.*
681f0 2a 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 * needed to gene
68200 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 rate code for a
68210 73 69 6e 67 6c 65 20 53 45 4c 45 43 54 20 73 74 single SELECT st
68220 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 6e atement..**.** n
68230 4c 69 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 Limit is set to
68240 2d 31 20 69 66 20 74 68 65 72 65 20 69 73 20 6e -1 if there is n
68250 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 20 o LIMIT clause.
68260 20 6e 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 nOffset is set
68270 74 6f 20 30 2e 0a 2a 2a 20 49 66 20 74 68 65 72 to 0..** If ther
68280 65 20 69 73 20 61 20 4c 49 4d 49 54 20 63 6c 61 e is a LIMIT cla
68290 75 73 65 2c 20 74 68 65 20 70 61 72 73 65 72 20 use, the parser
682a0 73 65 74 73 20 6e 4c 69 6d 69 74 20 74 6f 20 74 sets nLimit to t
682b0 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a he value of the.
682c0 2a 2a 20 6c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 ** limit and nOf
682d0 66 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 fset to the valu
682e0 65 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 e of the offset
682f0 28 6f 72 20 30 20 69 66 20 74 68 65 72 65 20 69 (or 0 if there i
68300 73 20 6e 6f 74 0a 2a 2a 20 6f 66 66 73 65 74 29 s not.** offset)
68310 2e 20 20 42 75 74 20 6c 61 74 65 72 20 6f 6e 2c . But later on,
68320 20 6e 4c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 nLimit and nOff
68330 73 65 74 20 62 65 63 6f 6d 65 20 74 68 65 20 6d set become the m
68340 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 0a emory locations.
68350 2a 2a 20 69 6e 20 74 68 65 20 56 44 42 45 20 74 ** in the VDBE t
68360 68 61 74 20 72 65 63 6f 72 64 20 74 68 65 20 6c hat record the l
68370 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65 74 20 imit and offset
68380 63 6f 75 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 counters..**.**
68390 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 5d 20 65 addrOpenEphm[] e
683a0 6e 74 72 69 65 73 20 63 6f 6e 74 61 69 6e 20 74 ntries contain t
683b0 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 4f 50 he address of OP
683c0 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f _OpenEphemeral o
683d0 70 63 6f 64 65 73 2e 0a 2a 2a 20 54 68 65 73 65 pcodes..** These
683e0 20 61 64 64 72 65 73 73 65 73 20 6d 75 73 74 20 addresses must
683f0 62 65 20 73 74 6f 72 65 64 20 73 6f 20 74 68 61 be stored so tha
68400 74 20 77 65 20 63 61 6e 20 67 6f 20 62 61 63 6b t we can go back
68410 20 61 6e 64 20 66 69 6c 6c 20 69 6e 0a 2a 2a 20 and fill in.**
68420 74 68 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20 61 the P4_KEYINFO a
68430 6e 64 20 50 32 20 70 61 72 61 6d 65 74 65 72 73 nd P2 parameters
68440 20 6c 61 74 65 72 2e 20 20 4e 65 69 74 68 65 72 later. Neither
68450 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 6e 6f 72 the KeyInfo nor
68460 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f .** the number o
68470 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 50 32 20 f columns in P2
68480 63 61 6e 20 62 65 20 63 6f 6d 70 75 74 65 64 20 can be computed
68490 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
684a0 0a 2a 2a 20 61 73 20 74 68 65 20 4f 50 5f 4f 70 .** as the OP_Op
684b0 65 6e 45 70 68 6d 20 69 6e 73 74 72 75 63 74 69 enEphm instructi
684c0 6f 6e 20 69 73 20 63 6f 64 65 64 20 62 65 63 61 on is coded beca
684d0 75 73 65 20 6e 6f 74 0a 2a 2a 20 65 6e 6f 75 67 use not.** enoug
684e0 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 h information ab
684f0 6f 75 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 out the compound
68500 20 71 75 65 72 79 20 69 73 20 6b 6e 6f 77 6e 20 query is known
68510 61 74 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a at that point..*
68520 2a 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f * The KeyInfo fo
68530 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 30 r addrOpenTran[0
68540 5d 20 61 6e 64 20 5b 31 5d 20 63 6f 6e 74 61 69 ] and [1] contai
68550 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 ns collating seq
68560 75 65 6e 63 65 73 0a 2a 2a 20 66 6f 72 20 74 68 uences.** for th
68570 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 e result set. T
68580 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 he KeyInfo for a
68590 64 64 72 4f 70 65 6e 54 72 61 6e 5b 32 5d 20 63 ddrOpenTran[2] c
685a0 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e ontains collatin
685b0 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 20 66 g.** sequences f
685c0 6f 72 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 or the ORDER BY
685d0 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74 72 75 63 clause..*/.struc
685e0 74 20 53 65 6c 65 63 74 20 7b 0a 20 20 45 78 70 t Select {. Exp
685f0 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 rList *pEList;
68600 20 20 20 20 2f 2a 20 54 68 65 20 66 69 65 6c 64 /* The field
68610 73 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 s of the result
68620 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 */. u8 op;
68630 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
68640 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e 49 4f 4e 20 ne of: TK_UNION
68650 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e 54 45 52 53 TK_ALL TK_INTERS
68660 45 43 54 20 54 4b 5f 45 58 43 45 50 54 20 2a 2f ECT TK_EXCEPT */
68670 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 . char affinity
68680 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b ; /* Mak
68690 65 52 65 63 6f 72 64 20 77 69 74 68 20 74 68 69 eRecord with thi
686a0 73 20 61 66 66 69 6e 69 74 79 20 66 6f 72 20 53 s affinity for S
686b0 52 54 5f 53 65 74 20 2a 2f 0a 20 20 75 31 36 20 RT_Set */. u16
686c0 73 65 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 20 selFlags;
686d0 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 53 46 /* Various SF
686e0 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 53 _* values */. S
686f0 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 rcList *pSrc;
68700 20 20 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f /* The FRO
68710 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 M clause */. Ex
68720 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 pr *pWhere;
68730 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 /* The WHER
68740 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 E clause */. Ex
68750 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 prList *pGroupBy
68760 3b 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 ; /* The GROU
68770 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 P BY clause */.
68780 20 45 78 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 Expr *pHaving;
68790 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 48 /* The H
687a0 41 56 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a AVING clause */.
687b0 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 ExprList *pOrd
687c0 65 72 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 erBy; /* The
687d0 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 ORDER BY clause
687e0 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72 */. Select *pPr
687f0 69 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 ior; /* P
68800 72 69 6f 72 20 73 65 6c 65 63 74 20 69 6e 20 61 rior select in a
68810 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 compound select
68820 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 statement */.
68830 53 65 6c 65 63 74 20 2a 70 4e 65 78 74 3b 20 20 Select *pNext;
68840 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 73 /* Next s
68850 65 6c 65 63 74 20 74 6f 20 74 68 65 20 6c 65 66 elect to the lef
68860 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 t in a compound
68870 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 52 69 */. Select *pRi
68880 67 68 74 6d 6f 73 74 3b 20 20 20 20 2f 2a 20 52 ghtmost; /* R
68890 69 67 68 74 2d 6d 6f 73 74 20 73 65 6c 65 63 74 ight-most select
688a0 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 in a compound s
688b0 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 elect statement
688c0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 */. Expr *pLimi
688d0 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c t; /* L
688e0 49 4d 49 54 20 65 78 70 72 65 73 73 69 6f 6e 2e IMIT expression.
688f0 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 NULL means not
68900 75 73 65 64 2e 20 2a 2f 0a 20 20 45 78 70 72 20 used. */. Expr
68910 2a 70 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 *pOffset;
68920 20 20 2f 2a 20 4f 46 46 53 45 54 20 65 78 70 72 /* OFFSET expr
68930 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 ession. NULL mea
68940 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a ns not used. */.
68950 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c 20 69 4f int iLimit, iO
68960 66 66 73 65 74 3b 20 20 20 2f 2a 20 4d 65 6d 6f ffset; /* Memo
68970 72 79 20 72 65 67 69 73 74 65 72 73 20 68 6f 6c ry registers hol
68980 64 69 6e 67 20 4c 49 4d 49 54 20 26 20 4f 46 46 ding LIMIT & OFF
68990 53 45 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a SET counters */.
689a0 20 20 69 6e 74 20 61 64 64 72 4f 70 65 6e 45 70 int addrOpenEp
689b0 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50 5f 4f hm[3]; /* OP_O
689c0 70 65 6e 45 70 68 65 6d 20 6f 70 63 6f 64 65 73 penEphem opcodes
689d0 20 72 65 6c 61 74 65 64 20 74 6f 20 74 68 69 73 related to this
689e0 20 73 65 6c 65 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f select */.};../
689f0 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c *.** Allowed val
68a00 75 65 73 20 66 6f 72 20 53 65 6c 65 63 74 2e 73 ues for Select.s
68a10 65 6c 46 6c 61 67 73 2e 20 20 54 68 65 20 22 53 elFlags. The "S
68a20 46 22 20 70 72 65 66 69 78 20 73 74 61 6e 64 73 F" prefix stands
68a30 20 66 6f 72 0a 2a 2a 20 22 53 65 6c 65 63 74 20 for.** "Select
68a40 46 6c 61 67 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e Flag"..*/.#defin
68a50 65 20 53 46 5f 44 69 73 74 69 6e 63 74 20 20 20 e SF_Distinct
68a60 20 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 0x0001 /*
68a70 4f 75 74 70 75 74 20 73 68 6f 75 6c 64 20 62 65 Output should be
68a80 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a 23 64 65 DISTINCT */.#de
68a90 66 69 6e 65 20 53 46 5f 52 65 73 6f 6c 76 65 64 fine SF_Resolved
68aa0 20 20 20 20 20 20 20 20 30 78 30 30 30 32 20 20 0x0002
68ab0 2f 2a 20 49 64 65 6e 74 69 66 69 65 72 73 20 68 /* Identifiers h
68ac0 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 ave been resolve
68ad0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f d */.#define SF_
68ae0 41 67 67 72 65 67 61 74 65 20 20 20 20 20 20 20 Aggregate
68af0 30 78 30 30 30 34 20 20 2f 2a 20 43 6f 6e 74 61 0x0004 /* Conta
68b00 69 6e 73 20 61 67 67 72 65 67 61 74 65 20 66 75 ins aggregate fu
68b10 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 nctions */.#defi
68b20 6e 65 20 53 46 5f 55 73 65 73 45 70 68 65 6d 65 ne SF_UsesEpheme
68b30 72 61 6c 20 20 20 30 78 30 30 30 38 20 20 2f 2a ral 0x0008 /*
68b40 20 55 73 65 73 20 74 68 65 20 4f 70 65 6e 45 70 Uses the OpenEp
68b50 68 65 6d 65 72 61 6c 20 6f 70 63 6f 64 65 20 2a hemeral opcode *
68b60 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 45 78 70 /.#define SF_Exp
68b70 61 6e 64 65 64 20 20 20 20 20 20 20 20 30 78 30 anded 0x0
68b80 30 31 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 53 010 /* sqlite3S
68b90 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 63 61 electExpand() ca
68ba0 6c 6c 65 64 20 6f 6e 20 74 68 69 73 20 2a 2f 0a lled on this */.
68bb0 23 64 65 66 69 6e 65 20 53 46 5f 48 61 73 54 79 #define SF_HasTy
68bc0 70 65 49 6e 66 6f 20 20 20 20 20 30 78 30 30 32 peInfo 0x002
68bd0 30 20 20 2f 2a 20 46 52 4f 4d 20 73 75 62 71 75 0 /* FROM subqu
68be0 65 72 69 65 73 20 68 61 76 65 20 54 61 62 6c 65 eries have Table
68bf0 20 6d 65 74 61 64 61 74 61 20 2a 2f 0a 0a 0a 2f metadata */.../
68c00 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 *.** The results
68c10 20 6f 66 20 61 20 73 65 6c 65 63 74 20 63 61 6e of a select can
68c20 20 62 65 20 64 69 73 74 72 69 62 75 74 65 64 20 be distributed
68c30 69 6e 20 73 65 76 65 72 61 6c 20 77 61 79 73 2e in several ways.
68c40 20 20 54 68 65 0a 2a 2a 20 22 53 52 54 22 20 70 The.** "SRT" p
68c50 72 65 66 69 78 20 6d 65 61 6e 73 20 22 53 45 4c refix means "SEL
68c60 45 43 54 20 52 65 73 75 6c 74 20 54 79 70 65 22 ECT Result Type"
68c70 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 ..*/.#define SRT
68c80 5f 55 6e 69 6f 6e 20 20 20 20 20 20 20 20 31 20 _Union 1
68c90 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 /* Store result
68ca0 20 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 as keys in an i
68cb0 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ndex */.#define
68cc0 53 52 54 5f 45 78 63 65 70 74 20 20 20 20 20 20 SRT_Except
68cd0 20 32 20 20 2f 2a 20 52 65 6d 6f 76 65 20 72 65 2 /* Remove re
68ce0 73 75 6c 74 20 66 72 6f 6d 20 61 20 55 4e 49 4f sult from a UNIO
68cf0 4e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 N index */.#defi
68d00 6e 65 20 53 52 54 5f 45 78 69 73 74 73 20 20 20 ne SRT_Exists
68d10 20 20 20 20 33 20 20 2f 2a 20 53 74 6f 72 65 20 3 /* Store
68d20 31 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 1 if the result
68d30 69 73 20 6e 6f 74 20 65 6d 70 74 79 20 2a 2f 0a is not empty */.
68d40 23 64 65 66 69 6e 65 20 53 52 54 5f 44 69 73 63 #define SRT_Disc
68d50 61 72 64 20 20 20 20 20 20 34 20 20 2f 2a 20 44 ard 4 /* D
68d60 6f 20 6e 6f 74 20 73 61 76 65 20 74 68 65 20 72 o not save the r
68d70 65 73 75 6c 74 73 20 61 6e 79 77 68 65 72 65 20 esults anywhere
68d80 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f 52 44 45 52 */../* The ORDER
68d90 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 69 67 BY clause is ig
68da0 6e 6f 72 65 64 20 66 6f 72 20 61 6c 6c 20 6f 66 nored for all of
68db0 20 74 68 65 20 61 62 6f 76 65 20 2a 2f 0a 23 64 the above */.#d
68dc0 65 66 69 6e 65 20 49 67 6e 6f 72 61 62 6c 65 4f efine IgnorableO
68dd0 72 64 65 72 62 79 28 58 29 20 28 28 58 2d 3e 65 rderby(X) ((X->e
68de0 44 65 73 74 29 3c 3d 53 52 54 5f 44 69 73 63 61 Dest)<=SRT_Disca
68df0 72 64 29 0a 0a 23 64 65 66 69 6e 65 20 53 52 54 rd)..#define SRT
68e00 5f 4f 75 74 70 75 74 20 20 20 20 20 20 20 35 20 _Output 5
68e10 20 2f 2a 20 4f 75 74 70 75 74 20 65 61 63 68 20 /* Output each
68e20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f row of result */
68e30 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 4d 65 6d .#define SRT_Mem
68e40 20 20 20 20 20 20 20 20 20 20 36 20 20 2f 2a 20 6 /*
68e50 53 74 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20 Store result in
68e60 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 2a 2f a memory cell */
68e70 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 53 65 74 .#define SRT_Set
68e80 20 20 20 20 20 20 20 20 20 20 37 20 20 2f 2a 20 7 /*
68e90 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 Store results as
68ea0 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 keys in an inde
68eb0 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 x */.#define SRT
68ec0 5f 54 61 62 6c 65 20 20 20 20 20 20 20 20 38 20 _Table 8
68ed0 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 /* Store result
68ee0 20 61 73 20 64 61 74 61 20 77 69 74 68 20 61 6e as data with an
68ef0 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 77 69 64 automatic rowid
68f00 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f */.#define SRT_
68f10 45 70 68 65 6d 54 61 62 20 20 20 20 20 39 20 20 EphemTab 9
68f20 2f 2a 20 43 72 65 61 74 65 20 74 72 61 6e 73 69 /* Create transi
68f30 65 6e 74 20 74 61 62 20 61 6e 64 20 73 74 6f 72 ent tab and stor
68f40 65 20 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 e like SRT_Table
68f50 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f */.#define SRT_
68f60 43 6f 72 6f 75 74 69 6e 65 20 20 20 31 30 20 20 Coroutine 10
68f70 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73 69 /* Generate a si
68f80 6e 67 6c 65 20 72 6f 77 20 6f 66 20 72 65 73 75 ngle row of resu
68f90 6c 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 lt */../*.** A s
68fa0 74 72 75 63 74 75 72 65 20 75 73 65 64 20 74 6f tructure used to
68fb0 20 63 75 73 74 6f 6d 69 7a 65 20 74 68 65 20 62 customize the b
68fc0 65 68 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 ehavior of sqlit
68fd0 65 33 53 65 6c 65 63 74 28 29 2e 20 53 65 65 0a e3Select(). See.
68fe0 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 ** comments abov
68ff0 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 e sqlite3Select(
69000 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a ) for details..*
69010 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
69020 20 53 65 6c 65 63 74 44 65 73 74 20 53 65 6c 65 SelectDest Sele
69030 63 74 44 65 73 74 3b 0a 73 74 72 75 63 74 20 53 ctDest;.struct S
69040 65 6c 65 63 74 44 65 73 74 20 7b 0a 20 20 75 38 electDest {. u8
69050 20 65 44 65 73 74 3b 20 20 20 20 20 20 20 20 20 eDest;
69060 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 /* How to dispos
69070 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73 e of the results
69080 20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 */. u8 affinit
69090 79 3b 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e y; /* Affin
690a0 69 74 79 20 75 73 65 64 20 77 68 65 6e 20 65 44 ity used when eD
690b0 65 73 74 3d 3d 53 52 54 5f 53 65 74 20 2a 2f 0a est==SRT_Set */.
690c0 20 20 69 6e 74 20 69 50 61 72 6d 3b 20 20 20 20 int iParm;
690d0 20 20 20 20 2f 2a 20 41 20 70 61 72 61 6d 65 74 /* A paramet
690e0 65 72 20 75 73 65 64 20 62 79 20 74 68 65 20 65 er used by the e
690f0 44 65 73 74 20 64 69 73 70 6f 73 61 6c 20 6d 65 Dest disposal me
69100 74 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 69 4d thod */. int iM
69110 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 em; /* B
69120 61 73 65 20 72 65 67 69 73 74 65 72 20 77 68 65 ase register whe
69130 72 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 77 re results are w
69140 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 ritten */. int
69150 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a nMem; /*
69160 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 Number of regis
69170 74 65 72 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a ters allocated *
69180 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69 /.};../*.** Duri
69190 6e 67 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 ng code generati
691a0 6f 6e 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 73 on of statements
691b0 20 74 68 61 74 20 64 6f 20 69 6e 73 65 72 74 73 that do inserts
691c0 20 69 6e 74 6f 20 41 55 54 4f 49 4e 43 52 45 4d into AUTOINCREM
691d0 45 4e 54 20 0a 2a 2a 20 74 61 62 6c 65 73 2c 20 ENT .** tables,
691e0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e the following in
691f0 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 61 74 74 formation is att
69200 61 63 68 65 64 20 74 6f 20 74 68 65 20 54 61 62 ached to the Tab
69210 6c 65 2e 75 2e 61 75 74 6f 49 6e 63 2e 70 0a 2a le.u.autoInc.p.*
69220 2a 20 70 6f 69 6e 74 65 72 20 6f 66 20 65 61 63 * pointer of eac
69230 68 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 h autoincrement
69240 74 61 62 6c 65 20 74 6f 20 72 65 63 6f 72 64 20 table to record
69250 73 6f 6d 65 20 73 69 64 65 20 69 6e 66 6f 72 6d some side inform
69260 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 74 68 ation that.** th
69270 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 e code generator
69280 20 6e 65 65 64 73 2e 20 20 57 65 20 68 61 76 65 needs. We have
69290 20 74 6f 20 6b 65 65 70 20 70 65 72 2d 74 61 62 to keep per-tab
692a0 6c 65 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 le autoincrement
692b0 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 .** information
692c0 69 6e 20 63 61 73 65 20 69 6e 73 65 72 74 73 20 in case inserts
692d0 61 72 65 20 64 6f 77 6e 20 77 69 74 68 69 6e 20 are down within
692e0 74 72 69 67 67 65 72 73 2e 20 20 54 72 69 67 67 triggers. Trigg
692f0 65 72 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6e 6f ers do not.** no
69300 72 6d 61 6c 6c 79 20 63 6f 6f 72 64 69 6e 61 74 rmally coordinat
69310 65 20 74 68 65 69 72 20 61 63 74 69 76 69 74 69 e their activiti
69320 65 73 2c 20 62 75 74 20 77 65 20 64 6f 20 6e 65 es, but we do ne
69330 65 64 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 ed to coordinate
69340 20 74 68 65 0a 2a 2a 20 6c 6f 61 64 69 6e 67 20 the.** loading
69350 61 6e 64 20 73 61 76 69 6e 67 20 6f 66 20 61 75 and saving of au
69360 74 6f 69 6e 63 72 65 6d 65 6e 74 20 69 6e 66 6f toincrement info
69370 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 rmation..*/.stru
69380 63 74 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 7b ct AutoincInfo {
69390 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a . AutoincInfo *
693a0 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 pNext; /* Next
693b0 20 69 6e 66 6f 20 62 6c 6f 63 6b 20 69 6e 20 61 info block in a
693c0 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c list of them al
693d0 6c 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 l */. Table *pT
693e0 61 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ab; /*
693f0 54 61 62 6c 65 20 74 68 69 73 20 69 6e 66 6f 20 Table this info
69400 62 6c 6f 63 6b 20 72 65 66 65 72 73 20 74 6f 20 block refers to
69410 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 */. int iDb;
69420 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
69430 64 65 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 dex in sqlite3.a
69440 44 62 5b 5d 20 6f 66 20 64 61 74 61 62 61 73 65 Db[] of database
69450 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f holding pTab */
69460 0a 20 20 69 6e 74 20 72 65 67 43 74 72 3b 20 20 . int regCtr;
69470 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f /* Memo
69480 72 79 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 ry register hold
69490 69 6e 67 20 74 68 65 20 72 6f 77 69 64 20 63 6f ing the rowid co
694a0 75 6e 74 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a unter */.};../*.
694b0 2a 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63 ** Size of the c
694c0 6f 6c 75 6d 6e 20 63 61 63 68 65 0a 2a 2f 0a 23 olumn cache.*/.#
694d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e 5f ifndef SQLITE_N_
694e0 43 4f 4c 43 41 43 48 45 0a 23 20 64 65 66 69 6e COLCACHE.# defin
694f0 65 20 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 e SQLITE_N_COLCA
69500 43 48 45 20 31 30 0a 23 65 6e 64 69 66 0a 0a 2f CHE 10.#endif../
69510 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 6f 6e *.** At least on
69520 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 e instance of th
69530 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
69540 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65 64 cture is created
69550 20 66 6f 72 20 65 61 63 68 20 0a 2a 2a 20 74 72 for each .** tr
69560 69 67 67 65 72 20 74 68 61 74 20 6d 61 79 20 62 igger that may b
69570 65 20 66 69 72 65 64 20 77 68 69 6c 65 20 70 61 e fired while pa
69580 72 73 69 6e 67 20 61 6e 20 49 4e 53 45 52 54 2c rsing an INSERT,
69590 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 UPDATE or DELET
695a0 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 E.** statement.
695b0 41 6c 6c 20 73 75 63 68 20 6f 62 6a 65 63 74 73 All such objects
695c0 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 are stored in t
695d0 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 he linked list h
695e0 65 61 64 65 64 20 61 74 0a 2a 2a 20 50 61 72 73 eaded at.** Pars
695f0 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 61 6e e.pTriggerPrg an
69600 64 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 73 d deleted once s
69610 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 61 tatement compila
69620 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a tion has been.**
69630 20 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a completed..**.*
69640 2a 20 41 20 56 64 62 65 20 73 75 62 2d 70 72 6f * A Vdbe sub-pro
69650 67 72 61 6d 20 74 68 61 74 20 69 6d 70 6c 65 6d gram that implem
69660 65 6e 74 73 20 74 68 65 20 62 6f 64 79 20 61 6e ents the body an
69670 64 20 57 48 45 4e 20 63 6c 61 75 73 65 20 6f 66 d WHEN clause of
69680 20 74 72 69 67 67 65 72 0a 2a 2a 20 54 72 69 67 trigger.** Trig
69690 67 65 72 50 72 67 2e 70 54 72 69 67 67 65 72 2c gerPrg.pTrigger,
696a0 20 61 73 73 75 6d 69 6e 67 20 61 20 64 65 66 61 assuming a defa
696b0 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 ult ON CONFLICT
696c0 63 6c 61 75 73 65 20 6f 66 0a 2a 2a 20 54 72 69 clause of.** Tri
696d0 67 67 65 72 50 72 67 2e 6f 72 63 6f 6e 66 2c 20 ggerPrg.orconf,
696e0 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 is stored in the
696f0 20 54 72 69 67 67 65 72 50 72 67 2e 70 50 72 6f TriggerPrg.pPro
69700 67 72 61 6d 20 76 61 72 69 61 62 6c 65 2e 0a 2a gram variable..*
69710 2a 20 54 68 65 20 50 61 72 73 65 2e 70 54 72 69 * The Parse.pTri
69720 67 67 65 72 50 72 67 20 6c 69 73 74 20 6e 65 76 ggerPrg list nev
69730 65 72 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f 20 er contains two
69740 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 68 65 entries with the
69750 20 73 61 6d 65 0a 2a 2a 20 76 61 6c 75 65 73 20 same.** values
69760 66 6f 72 20 62 6f 74 68 20 70 54 72 69 67 67 65 for both pTrigge
69770 72 20 61 6e 64 20 6f 72 63 6f 6e 66 2e 0a 2a 2a r and orconf..**
69780 0a 2a 2a 20 54 68 65 20 54 72 69 67 67 65 72 50 .** The TriggerP
69790 72 67 2e 6f 6c 64 6d 61 73 6b 20 76 61 72 69 61 rg.oldmask varia
697a0 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 61 20 ble is set to a
697b0 6d 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f mask of old.* co
697c0 6c 75 6d 6e 73 0a 2a 2a 20 61 63 63 65 73 73 65 lumns.** accesse
697d0 64 20 28 6f 72 20 73 65 74 20 74 6f 20 30 20 66 d (or set to 0 f
697e0 6f 72 20 74 72 69 67 67 65 72 73 20 66 69 72 65 or triggers fire
697f0 64 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 d as a result of
69800 20 49 4e 53 45 52 54 20 0a 2a 2a 20 73 74 61 74 INSERT .** stat
69810 65 6d 65 6e 74 73 29 2e 0a 2a 2f 0a 73 74 72 75 ements)..*/.stru
69820 63 74 20 54 72 69 67 67 65 72 50 72 67 20 7b 0a ct TriggerPrg {.
69830 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 Trigger *pTrig
69840 67 65 72 3b 20 20 20 20 20 20 2f 2a 20 54 72 69 ger; /* Tri
69850 67 67 65 72 20 74 68 69 73 20 70 72 6f 67 72 61 gger this progra
69860 6d 20 77 61 73 20 63 6f 64 65 64 20 66 72 6f 6d m was coded from
69870 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 */. int orconf
69880 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
69890 20 44 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 Default ON CONF
698a0 4c 49 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 LICT policy */.
698b0 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 SubProgram *pPr
698c0 6f 67 72 61 6d 3b 20 20 20 2f 2a 20 50 72 6f 67 ogram; /* Prog
698d0 72 61 6d 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 ram implementing
698e0 20 70 54 72 69 67 67 65 72 2f 6f 72 63 6f 6e 66 pTrigger/orconf
698f0 20 2a 2f 0a 20 20 75 33 32 20 6f 6c 64 6d 61 73 */. u32 oldmas
69900 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a k; /*
69910 20 4d 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 Mask of old.* c
69920 6f 6c 75 6d 6e 73 20 61 63 63 65 73 73 65 64 20 olumns accessed
69930 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 */. TriggerPrg
69940 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 *pNext; /*
69950 4e 65 78 74 20 65 6e 74 72 79 20 69 6e 20 50 61 Next entry in Pa
69960 72 73 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 rse.pTriggerPrg
69970 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a list */.};../*.*
69980 2a 20 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 * An SQL parser
69990 63 6f 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 context. A copy
699a0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
699b0 72 65 20 69 73 20 70 61 73 73 65 64 20 74 68 72 re is passed thr
699c0 6f 75 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 ough.** the pars
699d0 65 72 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f er and down into
699e0 20 61 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 all the parser
699f0 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 action routine i
69a00 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 n order to.** ca
69a10 72 72 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 rry around infor
69a20 6d 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 mation that is g
69a30 6c 6f 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 lobal to the ent
69a40 69 72 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a ire parse..**.**
69a50 20 54 68 65 20 73 74 72 75 63 74 75 72 65 20 69 The structure i
69a60 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 s divided into t
69a70 77 6f 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 wo parts. When
69a80 74 68 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 the parser and c
69a90 6f 64 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 ode.** generate
69aa0 63 61 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 call themselves
69ab0 72 65 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 recursively, the
69ac0 20 66 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 first part of t
69ad0 68 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 he structure.**
69ae0 69 73 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 is constant but
69af0 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 the second part
69b00 69 73 20 72 65 73 65 74 20 61 74 20 74 68 65 20 is reset at the
69b10 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e beginning and en
69b20 64 20 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 d of.** each rec
69b30 75 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 ursion..**.** Th
69b40 65 20 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 e nTableLock and
69b50 20 61 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 aTableLock vari
69b60 61 62 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 ables are only u
69b70 73 65 64 20 69 66 20 74 68 65 20 73 68 61 72 65 sed if the share
69b80 64 2d 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 d-cache .** feat
69b90 75 72 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 ure is enabled (
69ba0 69 66 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d if sqlite3Tsd()-
69bb0 3e 75 73 65 53 68 61 72 65 64 44 61 74 61 20 69 >useSharedData i
69bc0 73 20 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 s true). They ar
69bd0 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f e.** used to sto
69be0 72 65 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 re the set of ta
69bf0 62 6c 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 ble-locks requir
69c00 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d ed by the statem
69c10 65 6e 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d ent being.** com
69c20 70 69 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 piled. Function
69c30 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b sqlite3TableLock
69c40 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 () is used to ad
69c50 64 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 d entries to the
69c60 0a 2a 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 .** list..*/.str
69c70 75 63 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 uct Parse {. sq
69c80 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 lite3 *db;
69c90 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 /* The main d
69ca0 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 atabase structur
69cb0 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 e */. int rc;
69cc0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
69cd0 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 eturn code from
69ce0 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 execution */. c
69cf0 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 har *zErrMsg;
69d00 20 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 /* An error
69d10 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 message */. Vdb
69d20 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 e *pVdbe;
69d30 20 20 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 /* An engine f
69d40 6f 72 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 or executing dat
69d50 61 62 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a abase bytecode *
69d60 2f 0a 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 /. u8 colNamesS
69d70 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 et; /* TRUE
69d80 20 61 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e after OP_Column
69d90 4e 61 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 Name has been is
69da0 73 75 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f sued to pVdbe */
69db0 0a 20 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b . u8 nameClash;
69dc0 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 /* A per
69dd0 6d 61 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d manent table nam
69de0 65 20 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 e clashes with t
69df0 65 6d 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a emp table name *
69e00 2f 0a 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 /. u8 checkSche
69e10 6d 61 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 ma; /* Caus
69e20 65 73 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 es schema cookie
69e30 20 63 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 check after an
69e40 65 72 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 error */. u8 ne
69e50 73 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 sted;
69e60 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 /* Number of nes
69e70 74 65 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 ted calls to the
69e80 20 70 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e parser/code gen
69e90 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 erator */. u8 p
69ea0 61 72 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 arseError;
69eb0 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 /* True after a
69ec0 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 parsing error.
69ed0 20 54 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f Ticket #1794 */
69ee0 0a 20 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 . u8 nTempReg;
69ef0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
69f00 72 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 r of temporary r
69f10 65 67 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d egisters in aTem
69f20 70 52 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e pReg[] */. u8 n
69f30 54 65 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 TempInUse;
69f40 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 /* Number of aT
69f50 65 6d 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 empReg[] current
69f60 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a ly checked out *
69f70 2f 0a 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 /. int aTempReg
69f80 5b 38 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 [8]; /* Hold
69f90 69 6e 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d ing area for tem
69fa0 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73 porary registers
69fb0 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 */. int nRange
69fc0 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 Reg; /* Si
69fd0 7a 65 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 ze of the tempor
69fe0 61 72 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f ary register blo
69ff0 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e ck */. int iRan
6a000 67 65 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 geReg; /*
6a010 46 69 72 73 74 20 72 65 67 69 73 74 65 72 20 69 First register i
6a020 6e 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 n temporary regi
6a030 73 74 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 ster block */.
6a040 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 int nErr;
6a050 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
6a060 66 20 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f f errors seen */
6a070 0a 20 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 . int nTab;
6a080 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
6a090 72 20 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 r of previously
6a0a0 61 6c 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 allocated VDBE c
6a0b0 75 72 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 ursors */. int
6a0c0 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 nMem;
6a0d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 /* Number of me
6a0e0 6d 6f 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 mory cells used
6a0f0 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 so far */. int
6a100 6e 53 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 nSet;
6a110 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 /* Number of se
6a120 74 73 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a ts used so far *
6a130 2f 0a 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 /. int ckBase;
6a140 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 /* Base
6a150 20 72 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 register of dat
6a160 61 20 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 a during check c
6a170 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 onstraints */.
6a180 69 6e 74 20 69 43 61 63 68 65 4c 65 76 65 6c 3b int iCacheLevel;
6a190 20 20 20 20 20 2f 2a 20 43 6f 6c 43 61 63 68 65 /* ColCache
6a1a0 20 76 61 6c 69 64 20 77 68 65 6e 20 61 43 6f 6c valid when aCol
6a1b0 43 61 63 68 65 5b 5d 2e 69 4c 65 76 65 6c 3c 3d Cache[].iLevel<=
6a1c0 69 43 61 63 68 65 4c 65 76 65 6c 20 2a 2f 0a 20 iCacheLevel */.
6a1d0 20 69 6e 74 20 69 43 61 63 68 65 43 6e 74 3b 20 int iCacheCnt;
6a1e0 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 /* Counter
6a1f0 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 used to generat
6a200 65 20 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 6c 72 e aColCache[].lr
6a210 75 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 u values */. u8
6a220 20 6e 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 20 nColCache;
6a230 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
6a240 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 63 entries in the c
6a250 6f 6c 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a 20 olumn cache */.
6a260 20 75 38 20 69 43 6f 6c 43 61 63 68 65 3b 20 20 u8 iColCache;
6a270 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e /* Next en
6a280 74 72 79 20 6f 66 20 74 68 65 20 63 61 63 68 65 try of the cache
6a290 20 74 6f 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20 to replace */.
6a2a0 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 struct yColCach
6a2b0 65 20 7b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 e {. int iTab
6a2c0 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a le; /*
6a2d0 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 Table cursor nu
6a2e0 6d 62 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 mber */. int
6a2f0 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 iColumn;
6a300 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d /* Table colum
6a310 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 n number */.
6a320 75 38 20 61 66 66 43 68 61 6e 67 65 3b 20 20 20 u8 affChange;
6a330 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
6a340 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20 68 this register h
6a350 61 73 20 68 61 64 20 61 6e 20 61 66 66 69 6e 69 as had an affini
6a360 74 79 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 ty change */.
6a370 20 75 38 20 74 65 6d 70 52 65 67 3b 20 20 20 20 u8 tempReg;
6a380 20 20 20 20 20 20 20 2f 2a 20 69 52 65 67 20 69 /* iReg i
6a390 73 20 61 20 74 65 6d 70 20 72 65 67 69 73 74 65 s a temp registe
6a3a0 72 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 r that needs to
6a3b0 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 be freed */.
6a3c0 69 6e 74 20 69 4c 65 76 65 6c 3b 20 20 20 20 20 int iLevel;
6a3d0 20 20 20 20 20 20 2f 2a 20 4e 65 73 74 69 6e 67 /* Nesting
6a3e0 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 20 20 69 6e level */. in
6a3f0 74 20 69 52 65 67 3b 20 20 20 20 20 20 20 20 20 t iReg;
6a400 20 20 20 20 2f 2a 20 52 65 67 20 77 69 74 68 20 /* Reg with
6a410 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f value of this co
6a420 6c 75 6d 6e 2e 20 30 20 6d 65 61 6e 73 20 6e 6f lumn. 0 means no
6a430 6e 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c ne. */. int l
6a440 72 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ru;
6a450 20 2f 2a 20 4c 65 61 73 74 20 72 65 63 65 6e 74 /* Least recent
6a460 6c 79 20 75 73 65 64 20 65 6e 74 72 79 20 68 61 ly used entry ha
6a470 73 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 s the smallest v
6a480 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c alue */. } aCol
6a490 43 61 63 68 65 5b 53 51 4c 49 54 45 5f 4e 5f 43 Cache[SQLITE_N_C
6a4a0 4f 4c 43 41 43 48 45 5d 3b 20 20 2f 2a 20 4f 6e OLCACHE]; /* On
6a4b0 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d e for each colum
6a4c0 6e 20 63 61 63 68 65 20 65 6e 74 72 79 20 2a 2f n cache entry */
6a4d0 0a 20 20 75 33 32 20 77 72 69 74 65 4d 61 73 6b . u32 writeMask
6a4e0 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 ; /* Start
6a4f0 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 a write transac
6a500 74 69 6f 6e 20 6f 6e 20 74 68 65 73 65 20 64 61 tion on these da
6a510 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 33 32 tabases */. u32
6a520 20 63 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 20 20 cookieMask;
6a530 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 /* Bitmask of
6a540 73 63 68 65 6d 61 20 76 65 72 69 66 69 65 64 20 schema verified
6a550 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 databases */. u
6a560 38 20 69 73 4d 75 6c 74 69 57 72 69 74 65 3b 20 8 isMultiWrite;
6a570 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 /* True if s
6a580 74 61 74 65 6d 65 6e 74 20 6d 61 79 20 61 66 66 tatement may aff
6a590 65 63 74 2f 69 6e 73 65 72 74 20 6d 75 6c 74 69 ect/insert multi
6a5a0 70 6c 65 20 72 6f 77 73 20 2a 2f 0a 20 20 75 38 ple rows */. u8
6a5b0 20 6d 61 79 41 62 6f 72 74 3b 20 20 20 20 20 20 mayAbort;
6a5c0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74 /* True if st
6a5d0 61 74 65 6d 65 6e 74 20 6d 61 79 20 74 68 72 6f atement may thro
6a5e0 77 20 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70 w an ABORT excep
6a5f0 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 6f tion */. int co
6a600 6f 6b 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f okieGoto; /
6a610 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f * Address of OP_
6a620 47 6f 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 Goto to cookie v
6a630 65 72 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 erifier subrouti
6a640 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b ne */. int cook
6a650 69 65 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d ieValue[SQLITE_M
6a660 41 58 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 AX_ATTACHED+2];
6a670 20 2f 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f /* Values of co
6a680 6f 6b 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 okies to verify
6a690 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
6a6a0 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
6a6b0 43 48 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 CHE. int nTable
6a6c0 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 Lock; /*
6a6d0 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 Number of locks
6a6e0 69 6e 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f in aTableLock */
6a6f0 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 . TableLock *aT
6a700 61 62 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 ableLock; /* Req
6a710 75 69 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b uired table lock
6a720 73 20 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 s for shared-cac
6a730 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 he mode */.#endi
6a740 66 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 f. int regRowid
6a750 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 ; /* Regi
6a760 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 ster holding row
6a770 69 64 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 id of CREATE TAB
6a780 4c 45 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e LE entry */. in
6a790 74 20 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 t regRoot;
6a7a0 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 /* Register h
6a7b0 6f 6c 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 olding root page
6a7c0 20 6e 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 number for new
6a7d0 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 41 75 74 objects */. Aut
6a7e0 6f 69 6e 63 49 6e 66 6f 20 2a 70 41 69 6e 63 3b oincInfo *pAinc;
6a7f0 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e /* Information
6a800 20 61 62 6f 75 74 20 41 55 54 4f 49 4e 43 52 45 about AUTOINCRE
6a810 4d 45 4e 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f MENT counters */
6a820 0a 20 20 69 6e 74 20 6e 4d 61 78 41 72 67 3b 20 . int nMaxArg;
6a830 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 61 /* Max a
6a840 72 67 73 20 70 61 73 73 65 64 20 74 6f 20 75 73 rgs passed to us
6a850 65 72 20 66 75 6e 63 74 69 6f 6e 20 62 79 20 73 er function by s
6a860 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 0a 20 ub-program */..
6a870 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
6a880 75 73 65 64 20 77 68 69 6c 65 20 63 6f 64 69 6e used while codin
6a890 67 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 g trigger progra
6a8a0 6d 73 2e 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a ms. */. Parse *
6a8b0 70 54 6f 70 6c 65 76 65 6c 3b 20 20 20 20 2f 2a pToplevel; /*
6a8c0 20 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 Parse structure
6a8d0 20 66 6f 72 20 6d 61 69 6e 20 70 72 6f 67 72 61 for main progra
6a8e0 6d 20 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a 20 m (or NULL) */.
6a8f0 20 54 61 62 6c 65 20 2a 70 54 72 69 67 67 65 72 Table *pTrigger
6a900 54 61 62 3b 20 20 2f 2a 20 54 61 62 6c 65 20 74 Tab; /* Table t
6a910 72 69 67 67 65 72 73 20 61 72 65 20 62 65 69 6e riggers are bein
6a920 67 20 63 6f 64 65 64 20 66 6f 72 20 2a 2f 0a 20 g coded for */.
6a930 20 75 33 32 20 6f 6c 64 6d 61 73 6b 3b 20 20 20 u32 oldmask;
6a940 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 /* Mask of
6a950 20 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 20 72 old.* columns r
6a960 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 75 eferenced */. u
6a970 38 20 65 54 72 69 67 67 65 72 4f 70 3b 20 20 20 8 eTriggerOp;
6a980 20 20 20 20 2f 2a 20 54 4b 5f 55 50 44 41 54 45 /* TK_UPDATE
6a990 2c 20 54 4b 5f 49 4e 53 45 52 54 20 6f 72 20 54 , TK_INSERT or T
6a9a0 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75 38 K_DELETE */. u8
6a9b0 20 65 4f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 eOrconf;
6a9c0 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e /* Default ON
6a9d0 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 CONFLICT policy
6a9e0 20 66 6f 72 20 74 72 69 67 67 65 72 20 73 74 65 for trigger ste
6a9f0 70 73 20 2a 2f 0a 0a 20 20 2f 2a 20 41 62 6f 76 ps */.. /* Abov
6aa00 65 20 69 73 20 63 6f 6e 73 74 61 6e 74 20 62 65 e is constant be
6aa10 74 77 65 65 6e 20 72 65 63 75 72 73 69 6f 6e 73 tween recursions
6aa20 2e 20 20 42 65 6c 6f 77 20 69 73 20 72 65 73 65 . Below is rese
6aa30 74 20 62 65 66 6f 72 65 20 61 6e 64 20 61 66 74 t before and aft
6aa40 65 72 0a 20 20 2a 2a 20 65 61 63 68 20 72 65 63 er. ** each rec
6aa50 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20 20 69 6e 74 ursion */.. int
6aa60 20 6e 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 nVar;
6aa70 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27 /* Number of '
6aa80 3f 27 20 76 61 72 69 61 62 6c 65 73 20 73 65 65 ?' variables see
6aa90 6e 20 69 6e 20 74 68 65 20 53 51 4c 20 73 6f 20 n in the SQL so
6aaa0 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 far */. int nVa
6aab0 72 45 78 70 72 3b 20 20 20 20 20 20 20 20 2f 2a rExpr; /*
6aac0 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 64 20 Number of used
6aad0 73 6c 6f 74 73 20 69 6e 20 61 70 56 61 72 45 78 slots in apVarEx
6aae0 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 pr[] */. int nV
6aaf0 61 72 45 78 70 72 41 6c 6c 6f 63 3b 20 20 20 2f arExprAlloc; /
6ab00 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f * Number of allo
6ab10 63 61 74 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 cated slots in a
6ab20 70 56 61 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 pVarExpr[] */.
6ab30 45 78 70 72 20 2a 2a 61 70 56 61 72 45 78 70 72 Expr **apVarExpr
6ab40 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 ; /* Pointers
6ab50 20 74 6f 20 3a 61 61 61 20 61 6e 64 20 24 61 61 to :aaa and $aa
6ab60 61 61 20 77 69 6c 64 63 61 72 64 20 65 78 70 72 aa wildcard expr
6ab70 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 essions */. int
6ab80 20 6e 41 6c 69 61 73 3b 20 20 20 20 20 20 20 20 nAlias;
6ab90 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
6aba0 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 73 65 liased result se
6abb0 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 t columns */. i
6abc0 6e 74 20 6e 41 6c 69 61 73 41 6c 6c 6f 63 3b 20 nt nAliasAlloc;
6abd0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6abe0 20 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 allocated slots
6abf0 20 66 6f 72 20 61 41 6c 69 61 73 5b 5d 20 2a 2f for aAlias[] */
6ac00 0a 20 20 69 6e 74 20 2a 61 41 6c 69 61 73 3b 20 . int *aAlias;
6ac10 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 /* Regis
6ac20 74 65 72 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 ter used to hold
6ac30 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 aliased result
6ac40 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b */. u8 explain;
6ac50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
6ac60 65 20 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e e if the EXPLAIN
6ac70 20 66 6c 61 67 20 69 73 20 66 6f 75 6e 64 20 6f flag is found o
6ac80 6e 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 n the query */.
6ac90 20 54 6f 6b 65 6e 20 73 4e 61 6d 65 54 6f 6b 65 Token sNameToke
6aca0 6e 3b 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 77 n; /* Token w
6acb0 69 74 68 20 75 6e 71 75 61 6c 69 66 69 65 64 20 ith unqualified
6acc0 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 6e 61 schema object na
6acd0 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c me */. Token sL
6ace0 61 73 74 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20 astToken; /*
6acf0 54 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 20 70 The last token p
6ad00 61 72 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 arsed */. const
6ad10 20 63 68 61 72 20 2a 7a 54 61 69 6c 3b 20 20 20 char *zTail;
6ad20 2f 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 74 20 /* All SQL text
6ad30 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 73 65 past the last se
6ad40 6d 69 63 6f 6c 6f 6e 20 70 61 72 73 65 64 20 2a micolon parsed *
6ad50 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 54 /. Table *pNewT
6ad60 61 62 6c 65 3b 20 20 20 20 2f 2a 20 41 20 74 61 able; /* A ta
6ad70 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 ble being constr
6ad80 75 63 74 65 64 20 62 79 20 43 52 45 41 54 45 20 ucted by CREATE
6ad90 54 41 42 4c 45 20 2a 2f 0a 20 20 54 72 69 67 67 TABLE */. Trigg
6ada0 65 72 20 2a 70 4e 65 77 54 72 69 67 67 65 72 3b er *pNewTrigger;
6adb0 20 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 /* Trigger
6adc0 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 20 under construct
6add0 62 79 20 61 20 43 52 45 41 54 45 20 54 52 49 47 by a CREATE TRIG
6ade0 47 45 52 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 GER */. const c
6adf0 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 har *zAuthContex
6ae00 74 3b 20 2f 2a 20 54 68 65 20 36 74 68 20 70 61 t; /* The 6th pa
6ae10 72 61 6d 65 74 65 72 20 74 6f 20 64 62 2d 3e 78 rameter to db->x
6ae20 41 75 74 68 20 63 61 6c 6c 62 61 63 6b 73 20 2a Auth callbacks *
6ae30 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
6ae40 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
6ae50 4c 45 0a 20 20 54 6f 6b 65 6e 20 73 41 72 67 3b LE. Token sArg;
6ae60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ae70 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 /* Complete text
6ae80 20 6f 66 20 61 20 6d 6f 64 75 6c 65 20 61 72 67 of a module arg
6ae90 75 6d 65 6e 74 20 2a 2f 0a 20 20 75 38 20 64 65 ument */. u8 de
6aea0 63 6c 61 72 65 56 74 61 62 3b 20 20 20 20 20 20 clareVtab;
6aeb0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
6aec0 20 69 6e 73 69 64 65 20 73 71 6c 69 74 65 33 5f inside sqlite3_
6aed0 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 2a declare_vtab() *
6aee0 2f 0a 20 20 69 6e 74 20 6e 56 74 61 62 4c 6f 63 /. int nVtabLoc
6aef0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f k; /
6af00 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 69 72 74 * Number of virt
6af10 75 61 6c 20 74 61 62 6c 65 73 20 74 6f 20 6c 6f ual tables to lo
6af20 63 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 2a ck */. Table **
6af30 61 70 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20 apVtabLock;
6af40 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
6af50 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 virtual tables
6af60 6e 65 65 64 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 needing locking
6af70 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 */.#endif. int
6af80 6e 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 nHeight;
6af90 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f /* Expressio
6afa0 6e 20 74 72 65 65 20 68 65 69 67 68 74 20 6f 66 n tree height of
6afb0 20 63 75 72 72 65 6e 74 20 73 75 62 2d 73 65 6c current sub-sel
6afc0 65 63 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a ect */. Table *
6afd0 70 5a 6f 6d 62 69 65 54 61 62 3b 20 20 20 20 20 pZombieTab;
6afe0 20 2f 2a 20 4c 69 73 74 20 6f 66 20 54 61 62 6c /* List of Tabl
6aff0 65 20 6f 62 6a 65 63 74 73 20 74 6f 20 64 65 6c e objects to del
6b000 65 74 65 20 61 66 74 65 72 20 63 6f 64 65 20 67 ete after code g
6b010 65 6e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 en */. TriggerP
6b020 72 67 20 2a 70 54 72 69 67 67 65 72 50 72 67 3b rg *pTriggerPrg;
6b030 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 /* Linked li
6b040 73 74 20 6f 66 20 63 6f 64 65 64 20 74 72 69 67 st of coded trig
6b050 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 gers */.};..#ifd
6b060 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
6b070 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 23 64 IRTUALTABLE. #d
6b080 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 efine IN_DECLARE
6b090 5f 56 54 41 42 20 30 0a 23 65 6c 73 65 0a 20 20 _VTAB 0.#else.
6b0a0 23 64 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 #define IN_DECLA
6b0b0 52 45 5f 56 54 41 42 20 28 70 50 61 72 73 65 2d RE_VTAB (pParse-
6b0c0 3e 64 65 63 6c 61 72 65 56 74 61 62 29 0a 23 65 >declareVtab).#e
6b0d0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 ndif../*.** An i
6b0e0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
6b0f0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
6b100 72 65 20 63 61 6e 20 62 65 20 64 65 63 6c 61 72 re can be declar
6b110 65 64 20 6f 6e 20 61 20 73 74 61 63 6b 20 61 6e ed on a stack an
6b120 64 20 75 73 65 64 0a 2a 2a 20 74 6f 20 73 61 76 d used.** to sav
6b130 65 20 74 68 65 20 50 61 72 73 65 2e 7a 41 75 74 e the Parse.zAut
6b140 68 43 6f 6e 74 65 78 74 20 76 61 6c 75 65 20 73 hContext value s
6b150 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 o that it can be
6b160 20 72 65 73 74 6f 72 65 64 20 6c 61 74 65 72 2e restored later.
6b170 0a 2a 2f 0a 73 74 72 75 63 74 20 41 75 74 68 43 .*/.struct AuthC
6b180 6f 6e 74 65 78 74 20 7b 0a 20 20 63 6f 6e 73 74 ontext {. const
6b190 20 63 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 char *zAuthCont
6b1a0 65 78 74 3b 20 20 20 2f 2a 20 50 75 74 20 73 61 ext; /* Put sa
6b1b0 76 65 64 20 50 61 72 73 65 2e 7a 41 75 74 68 43 ved Parse.zAuthC
6b1c0 6f 6e 74 65 78 74 20 68 65 72 65 20 2a 2f 0a 20 ontext here */.
6b1d0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 Parse *pParse;
6b1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6b1f0 54 68 65 20 50 61 72 73 65 20 73 74 72 75 63 74 The Parse struct
6b200 75 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ure */.};../*.**
6b210 20 42 69 74 66 69 65 6c 64 20 66 6c 61 67 73 20 Bitfield flags
6b220 66 6f 72 20 50 35 20 76 61 6c 75 65 20 69 6e 20 for P5 value in
6b230 4f 50 5f 49 6e 73 65 72 74 20 61 6e 64 20 4f 50 OP_Insert and OP
6b240 5f 44 65 6c 65 74 65 0a 2a 2f 0a 23 64 65 66 69 _Delete.*/.#defi
6b250 6e 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 ne OPFLAG_NCHANG
6b260 45 20 20 20 20 20 20 20 30 78 30 31 20 20 20 20 E 0x01
6b270 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 /* Set to update
6b280 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a db->nChange */.
6b290 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4c #define OPFLAG_L
6b2a0 41 53 54 52 4f 57 49 44 20 20 20 20 20 30 78 30 ASTROWID 0x0
6b2b0 32 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 75 2 /* Set to u
6b2c0 70 64 61 74 65 20 64 62 2d 3e 6c 61 73 74 52 6f pdate db->lastRo
6b2d0 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f wid */.#define O
6b2e0 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 20 20 PFLAG_ISUPDATE
6b2f0 20 20 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54 0x04 /* T
6b300 68 69 73 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 his OP_Insert is
6b310 20 61 6e 20 73 71 6c 20 55 50 44 41 54 45 20 2a an sql UPDATE *
6b320 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 /.#define OPFLAG
6b330 5f 41 50 50 45 4e 44 20 20 20 20 20 20 20 20 30 _APPEND 0
6b340 78 30 38 20 20 20 20 2f 2a 20 54 68 69 73 20 69 x08 /* This i
6b350 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 s likely to be a
6b360 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 23 64 65 66 n append */.#def
6b370 69 6e 65 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 ine OPFLAG_USESE
6b380 45 4b 52 45 53 55 4c 54 20 30 78 31 30 20 20 20 EKRESULT 0x10
6b390 20 2f 2a 20 54 72 79 20 74 6f 20 61 76 6f 69 64 /* Try to avoid
6b3a0 20 61 20 73 65 65 6b 20 69 6e 20 42 74 72 65 65 a seek in Btree
6b3b0 49 6e 73 65 72 74 28 29 20 2a 2f 0a 23 64 65 66 Insert() */.#def
6b3c0 69 6e 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52 ine OPFLAG_CLEAR
6b3d0 43 41 43 48 45 20 20 20 20 30 78 32 30 20 20 20 CACHE 0x20
6b3e0 20 2f 2a 20 43 6c 65 61 72 20 70 73 65 75 64 6f /* Clear pseudo
6b3f0 2d 74 61 62 6c 65 20 63 61 63 68 65 20 69 6e 20 -table cache in
6b400 4f 50 5f 43 6f 6c 75 6d 6e 20 2a 2f 0a 0a 2f 2a OP_Column */../*
6b410 0a 20 2a 20 45 61 63 68 20 74 72 69 67 67 65 72 . * Each trigger
6b420 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 present in the
6b430 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 database schema
6b440 69 73 20 73 74 6f 72 65 64 20 61 73 20 61 6e 20 is stored as an
6b450 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 2a 20 73 instance of. * s
6b460 74 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 0a truct Trigger. .
6b470 20 2a 0a 20 2a 20 50 6f 69 6e 74 65 72 73 20 74 *. * Pointers t
6b480 6f 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 o instances of s
6b490 74 72 75 63 74 20 54 72 69 67 67 65 72 20 61 72 truct Trigger ar
6b4a0 65 20 73 74 6f 72 65 64 20 69 6e 20 74 77 6f 20 e stored in two
6b4b0 77 61 79 73 2e 0a 20 2a 20 31 2e 20 49 6e 20 74 ways.. * 1. In t
6b4c0 68 65 20 22 74 72 69 67 48 61 73 68 22 20 68 61 he "trigHash" ha
6b4d0 73 68 20 74 61 62 6c 65 20 28 70 61 72 74 20 6f sh table (part o
6b4e0 66 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 74 f the sqlite3* t
6b4f0 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 hat represents t
6b500 68 65 20 0a 20 2a 20 20 20 20 64 61 74 61 62 61 he . * databa
6b510 73 65 29 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 se). This allows
6b520 20 54 72 69 67 67 65 72 20 73 74 72 75 63 74 75 Trigger structu
6b530 72 65 73 20 74 6f 20 62 65 20 72 65 74 72 69 65 res to be retrie
6b540 76 65 64 20 62 79 20 6e 61 6d 65 2e 0a 20 2a 20 ved by name.. *
6b550 32 2e 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20 2. All triggers
6b560 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
6b570 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 66 a single table f
6b580 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 orm a linked lis
6b590 74 2c 20 75 73 69 6e 67 20 74 68 65 0a 20 2a 20 t, using the. *
6b5a0 20 20 20 70 4e 65 78 74 20 6d 65 6d 62 65 72 20 pNext member
6b5b0 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 of struct Trigge
6b5c0 72 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 r. A pointer to
6b5d0 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e the first elemen
6b5e0 74 20 6f 66 20 74 68 65 0a 20 2a 20 20 20 20 6c t of the. * l
6b5f0 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 73 74 inked list is st
6b600 6f 72 65 64 20 61 73 20 74 68 65 20 22 70 54 72 ored as the "pTr
6b610 69 67 67 65 72 22 20 6d 65 6d 62 65 72 20 6f 66 igger" member of
6b620 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 0a the associated.
6b630 20 2a 20 20 20 20 73 74 72 75 63 74 20 54 61 62 * struct Tab
6b640 6c 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 22 73 le.. *. * The "s
6b650 74 65 70 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 tep_list" member
6b660 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 points to the f
6b670 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 irst element of
6b680 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 20 2a a linked list. *
6b690 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
6b6a0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 73 SQL statements s
6b6b0 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20 pecified as the
6b6c0 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e trigger program.
6b6d0 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 . */.struct Trig
6b6e0 67 65 72 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e ger {. char *zN
6b6f0 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ame;
6b700 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 /* The name of t
6b710 68 65 20 74 72 69 67 67 65 72 20 20 20 20 20 20 he trigger
6b720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6b730 20 20 2a 2f 0a 20 20 63 68 61 72 20 2a 74 61 62 */. char *tab
6b740 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f le; /
6b750 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 * The table or v
6b760 69 65 77 20 74 6f 20 77 68 69 63 68 20 74 68 65 iew to which the
6b770 20 74 72 69 67 67 65 72 20 61 70 70 6c 69 65 73 trigger applies
6b780 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 */. u8 op;
6b790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6b7a0 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 One of TK_DELET
6b7b0 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b E, TK_UPDATE, TK
6b7c0 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 _INSERT
6b7d0 2a 2f 0a 20 20 75 38 20 74 72 5f 74 6d 3b 20 20 */. u8 tr_tm;
6b7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6b7f0 4f 6e 65 20 6f 66 20 54 52 49 47 47 45 52 5f 42 One of TRIGGER_B
6b800 45 46 4f 52 45 2c 20 54 52 49 47 47 45 52 5f 41 EFORE, TRIGGER_A
6b810 46 54 45 52 20 2a 2f 0a 20 20 45 78 70 72 20 2a FTER */. Expr *
6b820 70 57 68 65 6e 3b 20 20 20 20 20 20 20 20 20 20 pWhen;
6b830 20 20 2f 2a 20 54 68 65 20 57 48 45 4e 20 63 6c /* The WHEN cl
6b840 61 75 73 65 20 6f 66 20 74 68 65 20 65 78 70 72 ause of the expr
6b850 65 73 73 69 6f 6e 20 28 6d 61 79 20 62 65 20 4e ession (may be N
6b860 55 4c 4c 29 20 2a 2f 0a 20 20 49 64 4c 69 73 74 ULL) */. IdList
6b870 20 2a 70 43 6f 6c 75 6d 6e 73 3b 20 20 20 20 20 *pColumns;
6b880 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
6b890 61 6e 20 55 50 44 41 54 45 20 4f 46 20 3c 63 6f an UPDATE OF <co
6b8a0 6c 75 6d 6e 2d 6c 69 73 74 3e 20 74 72 69 67 67 lumn-list> trigg
6b8b0 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 er,.
6b8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6b8d0 20 74 68 65 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 the <column-lis
6b8e0 74 3e 20 69 73 20 73 74 6f 72 65 64 20 68 65 72 t> is stored her
6b8f0 65 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 e */. Schema *p
6b900 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f Schema; /
6b910 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e * Schema contain
6b920 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72 20 ing the trigger
6b930 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 54 61 */. Schema *pTa
6b940 62 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 bSchema; /*
6b950 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e Schema containin
6b960 67 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 g the table */.
6b970 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 74 TriggerStep *st
6b980 65 70 5f 6c 69 73 74 3b 20 2f 2a 20 4c 69 6e 6b ep_list; /* Link
6b990 20 6c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 list of trigger
6b9a0 20 70 72 6f 67 72 61 6d 20 73 74 65 70 73 20 20 program steps
6b9b0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 */.
6b9c0 54 72 69 67 67 65 72 20 2a 70 4e 65 78 74 3b 20 Trigger *pNext;
6b9d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 /* Next
6b9e0 74 72 69 67 67 65 72 20 61 73 73 6f 63 69 61 74 trigger associat
6b9f0 65 64 20 77 69 74 68 20 74 68 65 20 74 61 62 6c ed with the tabl
6ba00 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 e */.};../*.** A
6ba10 20 74 72 69 67 67 65 72 20 69 73 20 65 69 74 68 trigger is eith
6ba20 65 72 20 61 20 42 45 46 4f 52 45 20 6f 72 20 61 er a BEFORE or a
6ba30 6e 20 41 46 54 45 52 20 74 72 69 67 67 65 72 2e n AFTER trigger.
6ba40 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
6ba50 63 6f 6e 73 74 61 6e 74 73 0a 2a 2a 20 64 65 74 constants.** det
6ba60 65 72 6d 69 6e 65 20 77 68 69 63 68 2e 20 0a 2a ermine which. .*
6ba70 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 *.** If there ar
6ba80 65 20 6d 75 6c 74 69 70 6c 65 20 74 72 69 67 67 e multiple trigg
6ba90 65 72 73 2c 20 79 6f 75 20 6d 69 67 68 74 20 6f ers, you might o
6baa0 66 20 73 6f 6d 65 20 42 45 46 4f 52 45 20 61 6e f some BEFORE an
6bab0 64 20 73 6f 6d 65 20 41 46 54 45 52 2e 0a 2a 2a d some AFTER..**
6bac0 20 49 6e 20 74 68 61 74 20 63 61 73 65 73 2c 20 In that cases,
6bad0 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 the constants be
6bae0 6c 6f 77 20 63 61 6e 20 62 65 20 4f 52 65 64 20 low can be ORed
6baf0 74 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 23 64 65 together..*/.#de
6bb00 66 69 6e 65 20 54 52 49 47 47 45 52 5f 42 45 46 fine TRIGGER_BEF
6bb10 4f 52 45 20 20 31 0a 23 64 65 66 69 6e 65 20 54 ORE 1.#define T
6bb20 52 49 47 47 45 52 5f 41 46 54 45 52 20 20 20 32 RIGGER_AFTER 2
6bb30 0a 0a 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61 ../*. * An insta
6bb40 6e 63 65 20 6f 66 20 73 74 72 75 63 74 20 54 72 nce of struct Tr
6bb50 69 67 67 65 72 53 74 65 70 20 69 73 20 75 73 65 iggerStep is use
6bb60 64 20 74 6f 20 73 74 6f 72 65 20 61 20 73 69 6e d to store a sin
6bb70 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e gle SQL statemen
6bb80 74 0a 20 2a 20 74 68 61 74 20 69 73 20 61 20 70 t. * that is a p
6bb90 61 72 74 20 6f 66 20 61 20 74 72 69 67 67 65 72 art of a trigger
6bba0 2d 70 72 6f 67 72 61 6d 2e 20 0a 20 2a 0a 20 2a -program. . *. *
6bbb0 20 49 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74 Instances of st
6bbc0 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 ruct TriggerStep
6bbd0 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 are stored in a
6bbe0 20 73 69 6e 67 6c 79 20 6c 69 6e 6b 65 64 20 6c singly linked l
6bbf0 69 73 74 20 28 6c 69 6e 6b 65 64 0a 20 2a 20 75 ist (linked. * u
6bc00 73 69 6e 67 20 74 68 65 20 22 70 4e 65 78 74 22 sing the "pNext"
6bc10 20 6d 65 6d 62 65 72 29 20 72 65 66 65 72 65 6e member) referen
6bc20 63 65 64 20 62 79 20 74 68 65 20 22 73 74 65 70 ced by the "step
6bc30 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 6f 66 _list" member of
6bc40 20 74 68 65 20 0a 20 2a 20 61 73 73 6f 63 69 61 the . * associa
6bc50 74 65 64 20 73 74 72 75 63 74 20 54 72 69 67 67 ted struct Trigg
6bc60 65 72 20 69 6e 73 74 61 6e 63 65 2e 20 54 68 65 er instance. The
6bc70 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f first element o
6bc80 66 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 f the linked lis
6bc90 74 20 69 73 0a 20 2a 20 74 68 65 20 66 69 72 73 t is. * the firs
6bca0 74 20 73 74 65 70 20 6f 66 20 74 68 65 20 74 72 t step of the tr
6bcb0 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e 0a 20 igger-program..
6bcc0 2a 20 0a 20 2a 20 54 68 65 20 22 6f 70 22 20 6d * . * The "op" m
6bcd0 65 6d 62 65 72 20 69 6e 64 69 63 61 74 65 73 20 ember indicates
6bce0 77 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20 whether this is
6bcf0 61 20 22 44 45 4c 45 54 45 22 2c 20 22 49 4e 53 a "DELETE", "INS
6bd00 45 52 54 22 2c 20 22 55 50 44 41 54 45 22 20 6f ERT", "UPDATE" o
6bd10 72 0a 20 2a 20 22 53 45 4c 45 43 54 22 20 73 74 r. * "SELECT" st
6bd20 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 6d 65 61 atement. The mea
6bd30 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 6f 74 68 nings of the oth
6bd40 65 72 20 6d 65 6d 62 65 72 73 20 69 73 20 64 65 er members is de
6bd50 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 termined by the
6bd60 0a 20 2a 20 76 61 6c 75 65 20 6f 66 20 22 6f 70 . * value of "op
6bd70 22 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 2a " as follows:. *
6bd80 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 49 4e . * (op == TK_IN
6bd90 53 45 52 54 29 0a 20 2a 20 6f 72 63 6f 6e 66 20 SERT). * orconf
6bda0 20 20 20 2d 3e 20 73 74 6f 72 65 73 20 74 68 65 -> stores the
6bdb0 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 ON CONFLICT alg
6bdc0 6f 72 69 74 68 6d 0a 20 2a 20 70 53 65 6c 65 63 orithm. * pSelec
6bdd0 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69 t -> If this i
6bde0 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f s an INSERT INTO
6bdf0 20 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 ... SELECT ...
6be00 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a statement, then.
6be10 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
6be20 74 68 69 73 20 73 74 6f 72 65 73 20 61 20 70 6f this stores a po
6be30 69 6e 74 65 72 20 74 6f 20 74 68 65 20 53 45 4c inter to the SEL
6be40 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 4f ECT statement. O
6be50 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 therwise NULL..
6be60 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 * target -> A
6be70 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 token holding t
6be80 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f he quoted name o
6be90 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 69 f the table to i
6bea0 6e 73 65 72 74 20 69 6e 74 6f 2e 0a 20 2a 20 70 nsert into.. * p
6beb0 45 78 70 72 4c 69 73 74 20 2d 3e 20 49 66 20 74 ExprList -> If t
6bec0 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 his is an INSERT
6bed0 20 49 4e 54 4f 20 2e 2e 2e 20 56 41 4c 55 45 53 INTO ... VALUES
6bee0 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20 ... statement,
6bef0 74 68 65 6e 0a 20 2a 20 20 20 20 20 20 20 20 20 then. *
6bf00 20 20 20 20 20 74 68 69 73 20 73 74 6f 72 65 73 this stores
6bf10 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69 6e values to be in
6bf20 73 65 72 74 65 64 2e 20 4f 74 68 65 72 77 69 73 serted. Otherwis
6bf30 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 49 64 4c 69 e NULL.. * pIdLi
6bf40 73 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 st -> If this
6bf50 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 is an INSERT INT
6bf60 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75 6d 6e 2d 6e O ... (<column-n
6bf70 61 6d 65 73 3e 29 20 56 41 4c 55 45 53 20 2e 2e ames>) VALUES ..
6bf80 2e 20 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 . . *
6bf90 20 20 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 statement, th
6bfa0 65 6e 20 74 68 69 73 20 73 74 6f 72 65 73 20 74 en this stores t
6bfb0 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 20 he column-names
6bfc0 74 6f 20 62 65 0a 20 2a 20 20 20 20 20 20 20 20 to be. *
6bfd0 20 20 20 20 20 20 69 6e 73 65 72 74 65 64 20 69 inserted i
6bfe0 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d nto.. *. * (op =
6bff0 3d 20 54 4b 5f 44 45 4c 45 54 45 29 0a 20 2a 20 = TK_DELETE). *
6c000 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 target -> A t
6c010 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 oken holding the
6c020 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 quoted name of
6c030 74 68 65 20 74 61 62 6c 65 20 74 6f 20 64 65 6c the table to del
6c040 65 74 65 20 66 72 6f 6d 2e 0a 20 2a 20 70 57 68 ete from.. * pWh
6c050 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20 57 48 ere -> The WH
6c060 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 ERE clause of th
6c070 65 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 e DELETE stateme
6c080 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73 70 65 nt if one is spe
6c090 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20 20 20 cified.. *
6c0a0 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 69 73 Otherwis
6c0b0 65 20 4e 55 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 28 e NULL.. * . * (
6c0c0 6f 70 20 3d 3d 20 54 4b 5f 55 50 44 41 54 45 29 op == TK_UPDATE)
6c0d0 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e . * target ->
6c0e0 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 A token holding
6c0f0 20 74 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65 the quoted name
6c100 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f of the table to
6c110 20 75 70 64 61 74 65 20 72 6f 77 73 20 6f 66 2e update rows of.
6c120 0a 20 2a 20 70 57 68 65 72 65 20 20 20 20 2d 3e . * pWhere ->
6c130 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 The WHERE claus
6c140 65 20 6f 66 20 74 68 65 20 55 50 44 41 54 45 20 e of the UPDATE
6c150 73 74 61 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65 statement if one
6c160 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a 20 is specified..
6c170 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f * O
6c180 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 therwise NULL..
6c190 2a 20 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 41 * pExprList -> A
6c1a0 20 6c 69 73 74 20 6f 66 20 74 68 65 20 63 6f 6c list of the col
6c1b0 75 6d 6e 73 20 74 6f 20 75 70 64 61 74 65 20 61 umns to update a
6c1c0 6e 64 20 74 68 65 20 65 78 70 72 65 73 73 69 6f nd the expressio
6c1d0 6e 73 20 74 6f 20 75 70 64 61 74 65 0a 20 2a 20 ns to update. *
6c1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 the
6c1f0 6d 20 74 6f 2e 20 53 65 65 20 73 71 6c 69 74 65 m to. See sqlite
6c200 33 55 70 64 61 74 65 28 29 20 64 6f 63 75 6d 65 3Update() docume
6c210 6e 74 61 74 69 6f 6e 20 6f 66 20 22 70 43 68 61 ntation of "pCha
6c220 6e 67 65 73 22 0a 20 2a 20 20 20 20 20 20 20 20 nges". *
6c230 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 0a argument..
6c240 20 2a 20 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 * . */.struct T
6c250 72 69 67 67 65 72 53 74 65 70 20 7b 0a 20 20 75 riggerStep {. u
6c260 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 8 op;
6c270 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b /* One of TK
6c280 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41 _DELETE, TK_UPDA
6c290 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 TE, TK_INSERT, T
6c2a0 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a 20 20 75 38 K_SELECT */. u8
6c2b0 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 orconf;
6c2c0 20 20 20 2f 2a 20 4f 45 5f 52 6f 6c 6c 62 61 63 /* OE_Rollbac
6c2d0 6b 20 65 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67 k etc. */. Trig
6c2e0 67 65 72 20 2a 70 54 72 69 67 3b 20 20 20 20 20 ger *pTrig;
6c2f0 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 20 /* The trigger
6c300 74 68 61 74 20 74 68 69 73 20 73 74 65 70 20 69 that this step i
6c310 73 20 61 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 s a part of */.
6c320 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 Select *pSelect
6c330 3b 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 ; /* SELECT
6c340 73 74 61 74 6d 65 6e 74 20 6f 72 20 52 48 53 20 statment or RHS
6c350 6f 66 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e of INSERT INTO .
6c360 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 2a 2f 0a . SELECT ... */.
6c370 20 20 54 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20 Token target;
6c380 20 20 20 20 20 20 20 2f 2a 20 54 61 72 67 65 74 /* Target
6c390 20 74 61 62 6c 65 20 66 6f 72 20 44 45 4c 45 54 table for DELET
6c3a0 45 2c 20 55 50 44 41 54 45 2c 20 49 4e 53 45 52 E, UPDATE, INSER
6c3b0 54 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 T */. Expr *pWh
6c3c0 65 72 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 ere; /* T
6c3d0 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
6c3e0 66 6f 72 20 44 45 4c 45 54 45 20 6f 72 20 55 50 for DELETE or UP
6c3f0 44 41 54 45 20 73 74 65 70 73 20 2a 2f 0a 20 20 DATE steps */.
6c400 45 78 70 72 4c 69 73 74 20 2a 70 45 78 70 72 4c ExprList *pExprL
6c410 69 73 74 3b 20 2f 2a 20 53 45 54 20 63 6c 61 75 ist; /* SET clau
6c420 73 65 20 66 6f 72 20 55 50 44 41 54 45 2e 20 20 se for UPDATE.
6c430 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 66 6f VALUES clause fo
6c440 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 49 64 r INSERT */. Id
6c450 4c 69 73 74 20 2a 70 49 64 4c 69 73 74 3b 20 20 List *pIdList;
6c460 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d /* Column nam
6c470 65 73 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f es for INSERT */
6c480 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a . TriggerStep *
6c490 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 pNext; /* Next
6c4a0 69 6e 20 74 68 65 20 6c 69 6e 6b 2d 6c 69 73 74 in the link-list
6c4b0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 */. TriggerSte
6c4c0 70 20 2a 70 4c 61 73 74 3b 20 20 2f 2a 20 4c 61 p *pLast; /* La
6c4d0 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 st element in li
6c4e0 6e 6b 2d 6c 69 73 74 2e 20 56 61 6c 69 64 20 66 nk-list. Valid f
6c4f0 6f 72 20 31 73 74 20 65 6c 65 6d 20 6f 6e 6c 79 or 1st elem only
6c500 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 */.};../*.** Th
6c510 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
6c520 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 69 cture contains i
6c530 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 nformation used
6c540 62 79 20 74 68 65 20 73 71 6c 69 74 65 46 69 78 by the sqliteFix
6c550 2e 2e 2e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 ....** routines
6c560 61 73 20 74 68 65 79 20 77 61 6c 6b 20 74 68 65 as they walk the
6c570 20 70 61 72 73 65 20 74 72 65 65 20 74 6f 20 6d parse tree to m
6c580 61 6b 65 20 64 61 74 61 62 61 73 65 20 72 65 66 ake database ref
6c590 65 72 65 6e 63 65 73 0a 2a 2a 20 65 78 70 6c 69 erences.** expli
6c5a0 63 69 74 2e 20 20 0a 2a 2f 0a 74 79 70 65 64 65 cit. .*/.typede
6c5b0 66 20 73 74 72 75 63 74 20 44 62 46 69 78 65 72 f struct DbFixer
6c5c0 20 44 62 46 69 78 65 72 3b 0a 73 74 72 75 63 74 DbFixer;.struct
6c5d0 20 44 62 46 69 78 65 72 20 7b 0a 20 20 50 61 72 DbFixer {. Par
6c5e0 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 se *pParse;
6c5f0 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 /* The parsing
6c600 63 6f 6e 74 65 78 74 2e 20 20 45 72 72 6f 72 20 context. Error
6c610 6d 65 73 73 61 67 65 73 20 77 72 69 74 74 65 6e messages written
6c620 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 here */. const
6c630 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 2f char *zDb; /
6c640 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 * Make sure all
6c650 6f 62 6a 65 63 74 73 20 61 72 65 20 63 6f 6e 74 objects are cont
6c660 61 69 6e 65 64 20 69 6e 20 74 68 69 73 20 64 61 ained in this da
6c670 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 tabase */. cons
6c680 74 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 t char *zType;
6c690 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20 63 /* Type of the c
6c6a0 6f 6e 74 61 69 6e 65 72 20 2d 20 75 73 65 64 20 ontainer - used
6c6b0 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 for error messag
6c6c0 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f es */. const To
6c6d0 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 2f 2a 20 4e ken *pName; /* N
6c6e0 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 ame of the conta
6c6f0 69 6e 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 iner - used for
6c700 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a error messages *
6c710 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f /.};../*.** An o
6c720 62 6a 65 63 74 65 64 20 75 73 65 64 20 74 6f 20 bjected used to
6c730 61 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 74 accumulate the t
6c740 65 78 74 20 6f 66 20 61 20 73 74 72 69 6e 67 20 ext of a string
6c750 77 68 65 72 65 20 77 65 0a 2a 2a 20 64 6f 20 6e where we.** do n
6c760 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 6b ot necessarily k
6c770 6e 6f 77 20 68 6f 77 20 62 69 67 20 74 68 65 20 now how big the
6c780 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 69 string will be i
6c790 6e 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 n the end..*/.st
6c7a0 72 75 63 74 20 53 74 72 41 63 63 75 6d 20 7b 0a ruct StrAccum {.
6c7b0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
6c7c0 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e /* Option
6c7d0 61 6c 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 al database for
6c7e0 6c 6f 6f 6b 61 73 69 64 65 2e 20 20 43 61 6e 20 lookaside. Can
6c7f0 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 61 be NULL */. cha
6c800 72 20 2a 7a 42 61 73 65 3b 20 20 20 20 20 20 20 r *zBase;
6c810 20 20 2f 2a 20 41 20 62 61 73 65 20 61 6c 6c 6f /* A base allo
6c820 63 61 74 69 6f 6e 2e 20 20 4e 6f 74 20 66 72 6f cation. Not fro
6c830 6d 20 6d 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 20 63 m malloc. */. c
6c840 68 61 72 20 2a 7a 54 65 78 74 3b 20 20 20 20 20 har *zText;
6c850 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e /* The strin
6c860 67 20 63 6f 6c 6c 65 63 74 65 64 20 73 6f 20 66 g collected so f
6c870 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 43 68 ar */. int nCh
6c880 61 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ar; /*
6c890 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74 Length of the st
6c8a0 72 69 6e 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 ring so far */.
6c8b0 20 69 6e 74 20 20 6e 41 6c 6c 6f 63 3b 20 20 20 int nAlloc;
6c8c0 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 /* Amount
6c8d0 6f 66 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 of space allocat
6c8e0 65 64 20 69 6e 20 7a 54 65 78 74 20 2a 2f 0a 20 ed in zText */.
6c8f0 20 69 6e 74 20 20 6d 78 41 6c 6c 6f 63 3b 20 20 int mxAlloc;
6c900 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d /* Maximum
6c910 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 allowed string
6c920 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 75 38 20 20 length */. u8
6c930 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 mallocFailed;
6c940 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65 /* Becomes true
6c950 20 69 66 20 61 6e 79 20 6d 65 6d 6f 72 79 20 61 if any memory a
6c960 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20 llocation fails
6c970 2a 2f 0a 20 20 75 38 20 20 20 75 73 65 4d 61 6c */. u8 useMal
6c980 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 loc; /* Tru
6c990 65 20 69 66 20 7a 54 65 78 74 20 69 73 20 65 6e e if zText is en
6c9a0 6c 61 72 67 65 61 62 6c 65 20 75 73 69 6e 67 20 largeable using
6c9b0 72 65 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 38 20 realloc */. u8
6c9c0 20 20 74 6f 6f 42 69 67 3b 20 20 20 20 20 20 20 tooBig;
6c9d0 20 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 /* Becomes tru
6c9e0 65 20 69 66 20 73 74 72 69 6e 67 20 73 69 7a 65 e if string size
6c9f0 20 65 78 63 65 65 64 73 20 6c 69 6d 69 74 73 20 exceeds limits
6ca00 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 70 */.};../*.** A p
6ca10 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 ointer to this s
6ca20 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64 tructure is used
6ca30 20 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 to communicate
6ca40 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 information.** f
6ca50 72 6f 6d 20 73 71 6c 69 74 65 33 49 6e 69 74 20 rom sqlite3Init
6ca60 61 6e 64 20 4f 50 5f 50 61 72 73 65 53 63 68 65 and OP_ParseSche
6ca70 6d 61 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 ma into the sqli
6ca80 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2e te3InitCallback.
6ca90 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
6caa0 63 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a ct {. sqlite3 *
6cab0 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 db; /* Th
6cac0 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 e database being
6cad0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
6cae0 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 int iDb;
6caf0 20 20 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d /* 0 for m
6cb00 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 20 31 ain database. 1
6cb10 20 66 6f 72 20 54 45 4d 50 2c 20 32 2e 2e 20 66 for TEMP, 2.. f
6cb20 6f 72 20 41 54 54 41 43 48 65 64 20 2a 2f 0a 20 or ATTACHed */.
6cb30 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 char **pzErrMsg
6cb40 3b 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 ; /* Error me
6cb50 73 73 61 67 65 20 73 74 6f 72 65 64 20 68 65 72 ssage stored her
6cb60 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 e */. int rc;
6cb70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
6cb80 73 75 6c 74 20 63 6f 64 65 20 73 74 6f 72 65 64 sult code stored
6cb90 20 68 65 72 65 20 2a 2f 0a 7d 20 49 6e 69 74 44 here */.} InitD
6cba0 61 74 61 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 ata;../*.** Stru
6cbb0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 cture containing
6cbc0 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 global configur
6cbd0 61 74 69 6f 6e 20 64 61 74 61 20 66 6f 72 20 74 ation data for t
6cbe0 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 he SQLite librar
6cbf0 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 y..**.** This st
6cc00 72 75 63 74 75 72 65 20 61 6c 73 6f 20 63 6f 6e ructure also con
6cc10 74 61 69 6e 73 20 73 6f 6d 65 20 73 74 61 74 65 tains some state
6cc20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f information..*/
6cc30 0a 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 .struct Sqlite3C
6cc40 6f 6e 66 69 67 20 7b 0a 20 20 69 6e 74 20 62 4d onfig {. int bM
6cc50 65 6d 73 74 61 74 3b 20 20 20 20 20 20 20 20 20 emstat;
6cc60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
6cc70 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 6d 65 rue to enable me
6cc80 6d 6f 72 79 20 73 74 61 74 75 73 20 2a 2f 0a 20 mory status */.
6cc90 20 69 6e 74 20 62 43 6f 72 65 4d 75 74 65 78 3b int bCoreMutex;
6cca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ccb0 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e /* True to en
6ccc0 61 62 6c 65 20 63 6f 72 65 20 6d 75 74 65 78 69 able core mutexi
6ccd0 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 46 75 6c ng */. int bFul
6cce0 6c 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 lMutex;
6ccf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
6cd00 65 20 74 6f 20 65 6e 61 62 6c 65 20 66 75 6c 6c e to enable full
6cd10 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69 mutexing */. i
6cd20 6e 74 20 6d 78 53 74 72 6c 65 6e 3b 20 20 20 20 nt mxStrlen;
6cd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6cd40 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 74 72 69 /* Maximum stri
6cd50 6e 67 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69 ng length */. i
6cd60 6e 74 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 3b 20 nt szLookaside;
6cd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6cd80 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b /* Default look
6cd90 61 73 69 64 65 20 62 75 66 66 65 72 20 73 69 7a aside buffer siz
6cda0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 6f 6b e */. int nLook
6cdb0 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 aside;
6cdc0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 /* Defa
6cdd0 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 ult lookaside bu
6cde0 66 66 65 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 ffer count */.
6cdf0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
6ce00 6f 64 73 20 6d 3b 20 20 20 20 20 20 20 20 20 20 ods m;
6ce10 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d /* Low-level m
6ce20 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
6ce30 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 interface */.
6ce40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
6ce50 74 68 6f 64 73 20 6d 75 74 65 78 3b 20 20 20 20 thods mutex;
6ce60 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d /* Low-level m
6ce70 75 74 65 78 20 69 6e 74 65 72 66 61 63 65 20 2a utex interface *
6ce80 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 /. sqlite3_pcac
6ce90 68 65 5f 6d 65 74 68 6f 64 73 20 70 63 61 63 68 he_methods pcach
6cea0 65 3b 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 e; /* Low-lev
6ceb0 65 6c 20 70 61 67 65 2d 63 61 63 68 65 20 69 6e el page-cache in
6cec0 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 76 6f 69 terface */. voi
6ced0 64 20 2a 70 48 65 61 70 3b 20 20 20 20 20 20 20 d *pHeap;
6cee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6cef0 2a 20 48 65 61 70 20 73 74 6f 72 61 67 65 20 73 * Heap storage s
6cf00 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 pace */. int nH
6cf10 65 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 eap;
6cf20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
6cf30 69 7a 65 20 6f 66 20 70 48 65 61 70 5b 5d 20 2a ize of pHeap[] *
6cf40 2f 0a 20 20 69 6e 74 20 6d 6e 52 65 71 2c 20 6d /. int mnReq, m
6cf50 78 52 65 71 3b 20 20 20 20 20 20 20 20 20 20 20 xReq;
6cf60 20 20 20 20 20 20 2f 2a 20 4d 69 6e 20 61 6e 64 /* Min and
6cf70 20 6d 61 78 20 68 65 61 70 20 72 65 71 75 65 73 max heap reques
6cf80 74 73 20 73 69 7a 65 73 20 2a 2f 0a 20 20 76 6f ts sizes */. vo
6cf90 69 64 20 2a 70 53 63 72 61 74 63 68 3b 20 20 20 id *pScratch;
6cfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6cfb0 2f 2a 20 53 63 72 61 74 63 68 20 6d 65 6d 6f 72 /* Scratch memor
6cfc0 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 y */. int szScr
6cfd0 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 atch;
6cfe0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
6cff0 20 6f 66 20 65 61 63 68 20 73 63 72 61 74 63 68 of each scratch
6d000 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 buffer */. int
6d010 20 6e 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 nScratch;
6d020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6d030 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 63 72 61 * Number of scra
6d040 74 63 68 20 62 75 66 66 65 72 73 20 2a 2f 0a 20 tch buffers */.
6d050 20 76 6f 69 64 20 2a 70 50 61 67 65 3b 20 20 20 void *pPage;
6d060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d070 20 20 20 2f 2a 20 50 61 67 65 20 63 61 63 68 65 /* Page cache
6d080 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 memory */. int
6d090 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 szPage;
6d0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6d0b0 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 70 * Size of each p
6d0c0 61 67 65 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a age in pPage[] *
6d0d0 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 /. int nPage;
6d0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d0f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
6d100 6f 66 20 70 61 67 65 73 20 69 6e 20 70 50 61 67 of pages in pPag
6d110 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 e[] */. int mxP
6d120 61 72 73 65 72 53 74 61 63 6b 3b 20 20 20 20 20 arserStack;
6d130 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 /* ma
6d140 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 ximum depth of t
6d150 68 65 20 70 61 72 73 65 72 20 73 74 61 63 6b 20 he parser stack
6d160 2a 2f 0a 20 20 69 6e 74 20 73 68 61 72 65 64 43 */. int sharedC
6d170 61 63 68 65 45 6e 61 62 6c 65 64 3b 20 20 20 20 acheEnabled;
6d180 20 20 20 20 20 20 20 2f 2a 20 74 72 75 65 20 69 /* true i
6d190 66 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d f shared-cache m
6d1a0 6f 64 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 ode enabled */.
6d1b0 20 2f 2a 20 54 68 65 20 61 62 6f 76 65 20 6d 69 /* The above mi
6d1c0 67 68 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a ght be initializ
6d1d0 65 64 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 ed to non-zero.
6d1e0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e The following n
6d1f0 65 65 64 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 eed to always.
6d200 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 62 65 20 ** initially be
6d210 7a 65 72 6f 2c 20 68 6f 77 65 76 65 72 2e 20 2a zero, however. *
6d220 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20 /. int isInit;
6d230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d240 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 /* True af
6d250 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ter initializati
6d260 6f 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 on has finished
6d270 2a 2f 0a 20 20 69 6e 74 20 69 6e 50 72 6f 67 72 */. int inProgr
6d280 65 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ess;
6d290 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77 /* True w
6d2a0 68 69 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 hile initializat
6d2b0 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 ion in progress
6d2c0 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 75 74 65 78 */. int isMutex
6d2d0 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 Init;
6d2e0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 /* True a
6d2f0 66 74 65 72 20 6d 75 74 65 78 65 73 20 61 72 65 fter mutexes are
6d300 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
6d310 20 20 69 6e 74 20 69 73 4d 61 6c 6c 6f 63 49 6e int isMallocIn
6d320 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 it;
6d330 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 /* True afte
6d340 72 20 6d 61 6c 6c 6f 63 20 69 73 20 69 6e 69 74 r malloc is init
6d350 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 ialized */. int
6d360 20 69 73 50 43 61 63 68 65 49 6e 69 74 3b 20 20 isPCacheInit;
6d370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6d380 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c * True after mal
6d390 6c 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a loc is initializ
6d3a0 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ed */. sqlite3_
6d3b0 6d 75 74 65 78 20 2a 70 49 6e 69 74 4d 75 74 65 mutex *pInitMute
6d3c0 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 x; /* Mut
6d3d0 65 78 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 ex used by sqlit
6d3e0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
6d3f0 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 49 6e 69 */. int nRefIni
6d400 74 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 tMutex;
6d410 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6d420 20 6f 66 20 75 73 65 72 73 20 6f 66 20 70 49 6e of users of pIn
6d430 69 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f itMutex */.};../
6d440 2a 0a 2a 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 *.** Context poi
6d450 6e 74 65 72 20 70 61 73 73 65 64 20 64 6f 77 6e nter passed down
6d460 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 72 65 through the tre
6d470 65 2d 77 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63 e-walk..*/.struc
6d480 74 20 57 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74 t Walker {. int
6d490 20 28 2a 78 45 78 70 72 43 61 6c 6c 62 61 63 6b (*xExprCallback
6d4a0 29 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a )(Walker*, Expr*
6d4b0 29 3b 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 ); /* Callba
6d4c0 63 6b 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f ck for expressio
6d4d0 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 ns */. int (*xS
6d4e0 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 29 28 57 electCallback)(W
6d4f0 61 6c 6b 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b alker*,Select*);
6d500 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f /* Callback fo
6d510 72 20 53 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50 r SELECTs */. P
6d520 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 arse *pParse;
6d530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d540 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 /* Pars
6d550 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a er context. */.
6d560 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 union {
6d570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d580 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
6d590 78 74 72 61 20 64 61 74 61 20 66 6f 72 20 63 61 xtra data for ca
6d5a0 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61 llback */. Na
6d5b0 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20 meContext *pNC;
6d5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d5d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69 /* Nami
6d5e0 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
6d5f0 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
6d600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d610 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6d620 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 2a 2f Integer value */
6d630 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 . } u;.};../* F
6d640 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 orward declarati
6d650 6f 6e 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ons */.SQLITE_PR
6d660 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6d670 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 3WalkExpr(Walker
6d680 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 *, Expr*);.SQLIT
6d690 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6d6a0 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 lite3WalkExprLis
6d6b0 74 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c t(Walker*, ExprL
6d6c0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ist*);.SQLITE_PR
6d6d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6d6e0 33 57 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 3WalkSelect(Walk
6d6f0 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 er*, Select*);.S
6d700 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6d710 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c t sqlite3WalkSel
6d720 65 63 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c ectExpr(Walker*,
6d730 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 Select*);.SQLIT
6d740 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6d750 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 lite3WalkSelectF
6d760 72 6f 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c rom(Walker*, Sel
6d770 65 63 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 ect*);../*.** Re
6d780 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 turn code from t
6d790 68 65 20 70 61 72 73 65 2d 74 72 65 65 20 77 61 he parse-tree wa
6d7a0 6c 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 lking primitives
6d7b0 20 61 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61 and their.** ca
6d7c0 6c 6c 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66 llbacks..*/.#def
6d7d0 69 6e 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 ine WRC_Continue
6d7e0 20 20 20 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69 0 /* Conti
6d7f0 6e 75 65 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68 nue down into ch
6d800 69 6c 64 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e ildren */.#defin
6d810 65 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 e WRC_Prune
6d820 20 20 31 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68 1 /* Omit ch
6d830 69 6c 64 72 65 6e 20 62 75 74 20 63 6f 6e 74 69 ildren but conti
6d840 6e 75 65 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c nue walking sibl
6d850 69 6e 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ings */.#define
6d860 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 WRC_Abort
6d870 32 20 20 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74 2 /* Abandon t
6d880 68 65 20 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a he tree walk */.
6d890 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 ./*.** Assuming
6d8a0 7a 49 6e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 zIn points to th
6d8b0 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 e first byte of
6d8c0 61 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 a UTF-8 characte
6d8d0 72 2c 0a 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49 r,.** advance zI
6d8e0 6e 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 n to point to th
6d8f0 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 e first byte of
6d900 74 68 65 20 6e 65 78 74 20 55 54 46 2d 38 20 63 the next UTF-8 c
6d910 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65 haracter..*/.#de
6d920 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4b 49 50 fine SQLITE_SKIP
6d930 5f 55 54 46 38 28 7a 49 6e 29 20 7b 20 20 20 20 _UTF8(zIn) {
6d940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d950 20 20 20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a \. if( (*(z
6d960 49 6e 2b 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20 In++))>=0xc0 ){
6d970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d980 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
6d990 20 20 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 while( (*zIn
6d9a0 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b & 0xc0)==0x80 ){
6d9b0 20 7a 49 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20 zIn++; }
6d9c0 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 \. }
6d9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
6da00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 .}../*.** The SQ
6da10 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
6da20 54 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 65 T macro can be e
6da30 69 74 68 65 72 20 61 20 63 6f 6e 73 74 61 6e 74 ither a constant
6da40 20 28 66 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e (for production
6da50 0a 2a 2a 20 62 75 69 6c 64 73 29 20 6f 72 20 61 .** builds) or a
6da60 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28 function call (
6da70 66 6f 72 20 64 65 62 75 67 67 69 6e 67 29 2e 20 for debugging).
6da80 20 49 66 20 69 74 20 69 73 20 61 20 66 75 6e 63 If it is a func
6da90 74 69 6f 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74 tion call,.** it
6daa0 20 61 6c 6c 6f 77 73 20 74 68 65 20 6f 70 65 72 allows the oper
6dab0 61 74 6f 72 20 74 6f 20 73 65 74 20 61 20 62 72 ator to set a br
6dac0 65 61 6b 70 6f 69 6e 74 20 61 74 20 74 68 65 20 eakpoint at the
6dad0 73 70 6f 74 20 77 68 65 72 65 20 64 61 74 61 62 spot where datab
6dae0 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f ase.** corruptio
6daf0 6e 20 69 73 20 66 69 72 73 74 20 64 65 74 65 63 n is first detec
6db00 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ted..*/.#ifdef S
6db10 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 QLITE_DEBUG.SQLI
6db20 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
6db30 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28 sqlite3Corrupt(
6db40 76 6f 69 64 29 3b 0a 23 20 64 65 66 69 6e 65 20 void);.# define
6db50 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
6db60 4b 50 54 20 73 71 6c 69 74 65 33 43 6f 72 72 75 KPT sqlite3Corru
6db70 70 74 28 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 pt().#else.# def
6db80 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 ine SQLITE_CORRU
6db90 50 54 5f 42 4b 50 54 20 53 51 4c 49 54 45 5f 43 PT_BKPT SQLITE_C
6dba0 4f 52 52 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f ORRUPT.#endif../
6dbb0 2a 0a 2a 2a 20 54 68 65 20 63 74 79 70 65 2e 68 *.** The ctype.h
6dbc0 20 68 65 61 64 65 72 20 69 73 20 6e 65 65 64 65 header is neede
6dbd0 64 20 66 6f 72 20 6e 6f 6e 2d 41 53 43 49 49 20 d for non-ASCII
6dbe0 73 79 73 74 65 6d 73 2e 20 20 49 74 20 69 73 20 systems. It is
6dbf0 61 6c 73 6f 0a 2a 2a 20 6e 65 65 64 65 64 20 62 also.** needed b
6dc00 79 20 46 54 53 33 20 77 68 65 6e 20 46 54 53 33 y FTS3 when FTS3
6dc10 20 69 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 is included in
6dc20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e the amalgamation
6dc30 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 ..*/.#if !define
6dc40 64 28 53 51 4c 49 54 45 5f 41 53 43 49 49 29 20 d(SQLITE_ASCII)
6dc50 7c 7c 20 5c 0a 20 20 20 20 28 64 65 66 69 6e 65 || \. (define
6dc60 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
6dc70 46 54 53 33 29 20 26 26 20 64 65 66 69 6e 65 64 FTS3) && defined
6dc80 28 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 (SQLITE_AMALGAMA
6dc90 54 49 4f 4e 29 29 0a 23 20 69 6e 63 6c 75 64 65 TION)).# include
6dca0 20 3c 63 74 79 70 65 2e 68 3e 0a 23 65 6e 64 69 <ctype.h>.#endi
6dcb0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c f../*.** The fol
6dcc0 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 69 lowing macros mi
6dcd0 6d 69 63 20 74 68 65 20 73 74 61 6e 64 61 72 64 mic the standard
6dce0 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f library functio
6dcf0 6e 73 20 74 6f 75 70 70 65 72 28 29 2c 0a 2a 2a ns toupper(),.**
6dd00 20 69 73 73 70 61 63 65 28 29 2c 20 69 73 61 6c isspace(), isal
6dd10 6e 75 6d 28 29 2c 20 69 73 64 69 67 69 74 28 29 num(), isdigit()
6dd20 20 61 6e 64 20 69 73 78 64 69 67 69 74 28 29 2c and isxdigit(),
6dd30 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54 respectively. T
6dd40 68 65 0a 2a 2a 20 73 71 6c 69 74 65 20 76 65 72 he.** sqlite ver
6dd50 73 69 6f 6e 73 20 6f 6e 6c 79 20 77 6f 72 6b 20 sions only work
6dd60 66 6f 72 20 41 53 43 49 49 20 63 68 61 72 61 63 for ASCII charac
6dd70 74 65 72 73 2c 20 72 65 67 61 72 64 6c 65 73 73 ters, regardless
6dd80 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 23 of locale..*/.#
6dd90 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 ifdef SQLITE_ASC
6dda0 49 49 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 II.# define sqli
6ddb0 74 65 33 54 6f 75 70 70 65 72 28 78 29 20 20 28 te3Toupper(x) (
6ddc0 28 78 29 26 7e 28 73 71 6c 69 74 65 33 43 74 79 (x)&~(sqlite3Cty
6ddd0 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 peMap[(unsigned
6dde0 63 68 61 72 29 28 78 29 5d 26 30 78 32 30 29 29 char)(x)]&0x20))
6ddf0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6de00 33 49 73 73 70 61 63 65 28 78 29 20 20 20 28 73 3Isspace(x) (s
6de10 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 qlite3CtypeMap[(
6de20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 unsigned char)(x
6de30 29 5d 26 30 78 30 31 29 0a 23 20 64 65 66 69 6e )]&0x01).# defin
6de40 65 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d e sqlite3Isalnum
6de50 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43 74 (x) (sqlite3Ct
6de60 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 ypeMap[(unsigned
6de70 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 36 29 char)(x)]&0x06)
6de80 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6de90 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 28 73 3Isalpha(x) (s
6dea0 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 qlite3CtypeMap[(
6deb0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 unsigned char)(x
6dec0 29 5d 26 30 78 30 32 29 0a 23 20 64 65 66 69 6e )]&0x02).# defin
6ded0 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 e sqlite3Isdigit
6dee0 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43 74 (x) (sqlite3Ct
6def0 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 ypeMap[(unsigned
6df00 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 34 29 char)(x)]&0x04)
6df10 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6df20 33 49 73 78 64 69 67 69 74 28 78 29 20 20 28 73 3Isxdigit(x) (s
6df30 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 qlite3CtypeMap[(
6df40 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 unsigned char)(x
6df50 29 5d 26 30 78 30 38 29 0a 23 20 64 65 66 69 6e )]&0x08).# defin
6df60 65 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 e sqlite3Tolower
6df70 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 55 70 (x) (sqlite3Up
6df80 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 perToLower[(unsi
6df90 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 29 0a gned char)(x)]).
6dfa0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 #else.# define s
6dfb0 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 78 29 qlite3Toupper(x)
6dfc0 20 20 20 74 6f 75 70 70 65 72 28 28 75 6e 73 69 toupper((unsi
6dfd0 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 gned char)(x)).#
6dfe0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 define sqlite3I
6dff0 73 73 70 61 63 65 28 78 29 20 20 20 69 73 73 70 sspace(x) issp
6e000 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68 ace((unsigned ch
6e010 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 ar)(x)).# define
6e020 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 sqlite3Isalnum(
6e030 78 29 20 20 20 69 73 61 6c 6e 75 6d 28 28 75 6e x) isalnum((un
6e040 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 signed char)(x))
6e050 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6e060 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 69 73 3Isalpha(x) is
6e070 61 6c 70 68 61 28 28 75 6e 73 69 67 6e 65 64 20 alpha((unsigned
6e080 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 char)(x)).# defi
6e090 6e 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 ne sqlite3Isdigi
6e0a0 74 28 78 29 20 20 20 69 73 64 69 67 69 74 28 28 t(x) isdigit((
6e0b0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 unsigned char)(x
6e0c0 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 )).# define sqli
6e0d0 74 65 33 49 73 78 64 69 67 69 74 28 78 29 20 20 te3Isxdigit(x)
6e0e0 69 73 78 64 69 67 69 74 28 28 75 6e 73 69 67 6e isxdigit((unsign
6e0f0 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 ed char)(x)).# d
6e100 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c efine sqlite3Tol
6e110 6f 77 65 72 28 78 29 20 20 20 74 6f 6c 6f 77 65 ower(x) tolowe
6e120 72 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 r((unsigned char
6e130 29 28 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a )(x)).#endif../*
6e140 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e .** Internal fun
6e150 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 ction prototypes
6e160 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
6e170 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 TE int sqlite3St
6e180 72 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 rICmp(const char
6e190 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *, const char *
6e1a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6e1b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e E int sqlite3IsN
6e1c0 75 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72 umber(const char
6e1d0 2a 2c 20 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51 *, int*, u8);.SQ
6e1e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6e1f0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
6e200 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 (const char*);.#
6e210 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 define sqlite3St
6e220 72 4e 49 43 6d 70 20 73 71 6c 69 74 65 33 5f 73 rNICmp sqlite3_s
6e230 74 72 6e 69 63 6d 70 0a 0a 53 51 4c 49 54 45 5f trnicmp..SQLITE_
6e240 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6e250 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f te3MallocInit(vo
6e260 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 id);.SQLITE_PRIV
6e270 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6e280 4d 61 6c 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b MallocEnd(void);
6e290 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e2a0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c void *sqlite3Mal
6e2b0 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 loc(int);.SQLITE
6e2c0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
6e2d0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
6e2e0 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 (int);.SQLITE_PR
6e2f0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
6e300 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 te3DbMallocZero(
6e310 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a sqlite3*, int);.
6e320 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6e330 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 oid *sqlite3DbMa
6e340 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 2a llocRaw(sqlite3*
6e350 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
6e360 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
6e370 69 74 65 33 44 62 53 74 72 44 75 70 28 73 71 6c ite3DbStrDup(sql
6e380 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 ite3*,const char
6e390 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6e3a0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
6e3b0 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69 74 65 DbStrNDup(sqlite
6e3c0 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 3*,const char*,
6e3d0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6e3e0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
6e3f0 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c e3Realloc(void*,
6e400 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6e410 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
6e420 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 te3DbReallocOrFr
6e430 65 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f ee(sqlite3 *, vo
6e440 69 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 id *, int);.SQLI
6e450 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6e460 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f *sqlite3DbReallo
6e470 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 c(sqlite3 *, voi
6e480 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 d *, int);.SQLIT
6e490 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6e4a0 71 6c 69 74 65 33 44 62 46 72 65 65 28 73 71 6c qlite3DbFree(sql
6e4b0 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 ite3*, void*);.S
6e4c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6e4d0 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 t sqlite3MallocS
6e4e0 69 7a 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 ize(void*);.SQLI
6e4f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6e500 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 qlite3DbMallocSi
6e510 7a 65 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 ze(sqlite3*, voi
6e520 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 d*);.SQLITE_PRIV
6e530 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
6e540 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69 3ScratchMalloc(i
6e550 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6e560 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6e570 53 63 72 61 74 63 68 46 72 65 65 28 76 6f 69 64 ScratchFree(void
6e580 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6e590 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
6e5a0 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b PageMalloc(int);
6e5b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e5c0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
6e5d0 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c Free(void*);.SQL
6e5e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6e5f0 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 sqlite3MemSetDe
6e600 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c fault(void);.SQL
6e610 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6e620 20 73 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 sqlite3BenignMa
6e630 6c 6c 6f 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28 llocHooks(void (
6e640 2a 29 28 76 6f 69 64 29 2c 20 76 6f 69 64 20 28 *)(void), void (
6e650 2a 29 28 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54 *)(void));.SQLIT
6e660 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6e670 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d lite3MemoryAlarm
6e680 28 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c (void (*)(void*,
6e690 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 sqlite3_int64,
6e6a0 69 6e 74 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c int), void*, sql
6e6b0 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 0a 2f 2a ite3_int64);../*
6e6c0 0a 2a 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20 77 .** On systems w
6e6d0 69 74 68 20 61 6d 70 6c 65 20 73 74 61 63 6b 20 ith ample stack
6e6e0 73 70 61 63 65 20 61 6e 64 20 74 68 61 74 20 73 space and that s
6e6f0 75 70 70 6f 72 74 20 61 6c 6c 6f 63 61 28 29 2c upport alloca(),
6e700 20 6d 61 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20 make.** use of
6e710 61 6c 6c 6f 63 61 28 29 20 74 6f 20 6f 62 74 61 alloca() to obta
6e720 69 6e 20 73 70 61 63 65 20 66 6f 72 20 6c 61 72 in space for lar
6e730 67 65 20 61 75 74 6f 6d 61 74 69 63 20 6f 62 6a ge automatic obj
6e740 65 63 74 73 2e 20 20 42 79 20 64 65 66 61 75 6c ects. By defaul
6e750 74 2c 0a 2a 2a 20 6f 62 74 61 69 6e 20 73 70 61 t,.** obtain spa
6e760 63 65 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 ce from malloc()
6e770 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f ..**.** The allo
6e780 63 61 28 29 20 72 6f 75 74 69 6e 65 20 6e 65 76 ca() routine nev
6e790 65 72 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e er returns NULL.
6e7a0 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73 This will caus
6e7b0 65 20 63 6f 64 65 20 70 61 74 68 73 0a 2a 2a 20 e code paths.**
6e7c0 74 68 61 74 20 64 65 61 6c 20 77 69 74 68 20 73 that deal with s
6e7d0 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 qlite3StackAlloc
6e7e0 28 29 20 66 61 69 6c 75 72 65 73 20 74 6f 20 62 () failures to b
6e7f0 65 20 75 6e 72 65 61 63 68 61 62 6c 65 2e 0a 2a e unreachable..*
6e800 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
6e810 55 53 45 5f 41 4c 4c 4f 43 41 0a 23 20 64 65 66 USE_ALLOCA.# def
6e820 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b ine sqlite3Stack
6e830 41 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 20 20 20 AllocRaw(D,N)
6e840 61 6c 6c 6f 63 61 28 4e 29 0a 23 20 64 65 66 69 alloca(N).# defi
6e850 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 ne sqlite3StackA
6e860 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 20 20 6d llocZero(D,N) m
6e870 65 6d 73 65 74 28 61 6c 6c 6f 63 61 28 4e 29 2c emset(alloca(N),
6e880 20 30 2c 20 4e 29 0a 23 20 64 65 66 69 6e 65 20 0, N).# define
6e890 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 sqlite3StackFree
6e8a0 28 44 2c 50 29 20 20 20 20 20 20 20 0a 23 65 6c (D,P) .#el
6e8b0 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 se.# define sqli
6e8c0 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 te3StackAllocRaw
6e8d0 28 44 2c 4e 29 20 20 20 73 71 6c 69 74 65 33 44 (D,N) sqlite3D
6e8e0 62 4d 61 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 0a bMallocRaw(D,N).
6e8f0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
6e900 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 44 StackAllocZero(D
6e910 2c 4e 29 20 20 73 71 6c 69 74 65 33 44 62 4d 61 ,N) sqlite3DbMa
6e920 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 0a 23 20 llocZero(D,N).#
6e930 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 define sqlite3St
6e940 61 63 6b 46 72 65 65 28 44 2c 50 29 20 20 20 20 ackFree(D,P)
6e950 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
6e960 28 44 2c 50 29 0a 23 65 6e 64 69 66 0a 0a 23 69 (D,P).#endif..#i
6e970 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
6e980 4c 45 5f 4d 45 4d 53 59 53 33 0a 53 51 4c 49 54 LE_MEMSYS3.SQLIT
6e990 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
6e9a0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
6e9b0 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 ods *sqlite3MemG
6e9c0 65 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 3b etMemsys3(void);
6e9d0 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
6e9e0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
6e9f0 53 59 53 35 0a 53 51 4c 49 54 45 5f 50 52 49 56 SYS5.SQLITE_PRIV
6ea00 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 ATE const sqlite
6ea10 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 3_mem_methods *s
6ea20 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 qlite3MemGetMems
6ea30 79 73 35 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 ys5(void);.#endi
6ea40 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 f...#ifndef SQLI
6ea50 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 53 51 TE_MUTEX_OMIT.SQ
6ea60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 LITE_PRIVATE s
6ea70 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
6ea80 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 hods *sqlite3Def
6ea90 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 3b aultMutex(void);
6eaa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6eab0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
6eac0 2a 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c *sqlite3MutexAll
6ead0 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f oc(int);.SQLITE_
6eae0 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
6eaf0 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76 lite3MutexInit(v
6eb00 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 oid);.SQLITE_PRI
6eb10 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
6eb20 65 33 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 e3MutexEnd(void)
6eb30 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 ;.#endif..SQLITE
6eb40 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6eb50 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 ite3StatusValue(
6eb60 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6eb70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6eb80 33 53 74 61 74 75 73 41 64 64 28 69 6e 74 2c 20 3StatusAdd(int,
6eb90 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6eba0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6ebb0 33 53 74 61 74 75 73 53 65 74 28 69 6e 74 2c 20 3StatusSet(int,
6ebc0 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 int);..SQLITE_PR
6ebd0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6ebe0 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a 3IsNaN(double);.
6ebf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ec00 76 6f 69 64 20 73 71 6c 69 74 65 33 56 58 50 72 void sqlite3VXPr
6ec10 69 6e 74 66 28 53 74 72 41 63 63 75 6d 2a 2c 20 intf(StrAccum*,
6ec20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a int, const char*
6ec30 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 , va_list);.SQLI
6ec40 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
6ec50 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 *sqlite3MPrintf(
6ec60 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 sqlite3*,const c
6ec70 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 har*, ...);.SQLI
6ec80 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
6ec90 2a 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 *sqlite3VMPrintf
6eca0 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 (sqlite3*,const
6ecb0 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b char*, va_list);
6ecc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ecd0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 char *sqlite3MAp
6ece0 70 65 6e 64 66 28 73 71 6c 69 74 65 33 2a 2c 63 pendf(sqlite3*,c
6ecf0 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a har*,const char*
6ed00 2c 2e 2e 2e 29 3b 0a 23 69 66 20 64 65 66 69 6e ,...);.#if defin
6ed10 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 ed(SQLITE_TEST)
6ed20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 || defined(SQLIT
6ed30 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f E_DEBUG).SQLITE_
6ed40 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
6ed50 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
6ed60 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e f(const char*, .
6ed70 2e 2e 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 ..);.#endif.#if
6ed80 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 defined(SQLITE_T
6ed90 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 EST).SQLITE_PRIV
6eda0 41 54 45 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 ATE void *sqli
6edb0 74 65 33 54 65 73 74 54 65 78 74 54 6f 50 74 72 te3TestTextToPtr
6edc0 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 (const char*);.#
6edd0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 endif.SQLITE_PRI
6ede0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6edf0 33 53 65 74 53 74 72 69 6e 67 28 63 68 61 72 20 3SetString(char
6ee00 2a 2a 2c 20 73 71 6c 69 74 65 33 2a 2c 20 63 6f **, sqlite3*, co
6ee10 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b nst char*, ...);
6ee20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ee30 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f void sqlite3Erro
6ee40 72 4d 73 67 28 50 61 72 73 65 2a 2c 20 63 6f 6e rMsg(Parse*, con
6ee50 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a st char*, ...);.
6ee60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6ee70 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 oid sqlite3Error
6ee80 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b 0a 53 Clear(Parse*);.S
6ee90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6eea0 74 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 t sqlite3Dequote
6eeb0 28 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f (char*);.SQLITE_
6eec0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6eed0 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 63 te3KeywordCode(c
6eee0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
6eef0 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 ar*, int);.SQLIT
6ef00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6ef10 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28 50 lite3RunParser(P
6ef20 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 arse*, const cha
6ef30 72 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 r*, char **);.SQ
6ef40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6ef50 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43 d sqlite3FinishC
6ef60 6f 64 69 6e 67 28 50 61 72 73 65 2a 29 3b 0a 53 oding(Parse*);.S
6ef70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6ef80 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 t sqlite3GetTemp
6ef90 52 65 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c Reg(Parse*);.SQL
6efa0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6efb0 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
6efc0 65 6d 70 52 65 67 28 50 61 72 73 65 2a 2c 69 6e empReg(Parse*,in
6efd0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6efe0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 TE int sqlite3Ge
6eff0 74 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 tTempRange(Parse
6f000 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *,int);.SQLITE_P
6f010 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6f020 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 te3ReleaseTempRa
6f030 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e 74 2c 69 nge(Parse*,int,i
6f040 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6f050 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
6f060 33 45 78 70 72 41 6c 6c 6f 63 28 73 71 6c 69 74 3ExprAlloc(sqlit
6f070 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 54 6f e3*,int,const To
6f080 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ken*,int);.SQLIT
6f090 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
6f0a0 73 71 6c 69 74 65 33 45 78 70 72 28 73 71 6c 69 sqlite3Expr(sqli
6f0b0 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 te3*,int,const c
6f0c0 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
6f0d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6f0e0 65 33 45 78 70 72 41 74 74 61 63 68 53 75 62 74 e3ExprAttachSubt
6f0f0 72 65 65 73 28 73 71 6c 69 74 65 33 2a 2c 45 78 rees(sqlite3*,Ex
6f100 70 72 2a 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 pr*,Expr*,Expr*)
6f110 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6f120 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 50 45 Expr *sqlite3PE
6f130 78 70 72 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c xpr(Parse*, int,
6f140 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20 63 Expr*, Expr*, c
6f150 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 onst Token*);.SQ
6f160 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
6f170 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e r *sqlite3ExprAn
6f180 64 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a d(sqlite3*,Expr*
6f190 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
6f1a0 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 _PRIVATE Expr *s
6f1b0 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 qlite3ExprFuncti
6f1c0 6f 6e 28 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 on(Parse*,ExprLi
6f1d0 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 st*, Token*);.SQ
6f1e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6f1f0 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 73 73 d sqlite3ExprAss
6f200 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50 61 72 ignVarNumber(Par
6f210 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c se*, Expr*);.SQL
6f220 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6f230 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 sqlite3ExprClea
6f240 72 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 r(sqlite3*, Expr
6f250 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6f260 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
6f270 78 70 72 44 65 6c 65 74 65 28 73 71 6c 69 74 65 xprDelete(sqlite
6f280 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 3*, Expr*);.SQLI
6f290 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c TE_PRIVATE ExprL
6f2a0 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 ist *sqlite3Expr
6f2b0 4c 69 73 74 41 70 70 65 6e 64 28 50 61 72 73 65 ListAppend(Parse
6f2c0 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 *,ExprList*,Expr
6f2d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6f2e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
6f2f0 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 50 xprListSetName(P
6f300 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c arse*,ExprList*,
6f310 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c Token*,int);.SQL
6f320 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6f330 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
6f340 53 65 74 53 70 61 6e 28 50 61 72 73 65 2a 2c 45 SetSpan(Parse*,E
6f350 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 53 70 61 xprList*,ExprSpa
6f360 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
6f370 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6f380 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73 ExprListDelete(s
6f390 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 qlite3*, ExprLis
6f3a0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
6f3b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
6f3c0 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 nit(sqlite3*, ch
6f3d0 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ar**);.SQLITE_PR
6f3e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6f3f0 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 3InitCallback(vo
6f400 69 64 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a id*, int, char**
6f410 2c 20 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 , char**);.SQLIT
6f420 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f430 71 6c 69 74 65 33 50 72 61 67 6d 61 28 50 61 72 qlite3Pragma(Par
6f440 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e se*,Token*,Token
6f450 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 *,Token*,int);.S
6f460 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6f470 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 id sqlite3ResetI
6f480 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 nternalSchema(sq
6f490 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 lite3*, int);.SQ
6f4a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6f4b0 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 d sqlite3BeginPa
6f4c0 72 73 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b rse(Parse*,int);
6f4d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f4e0 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d void sqlite3Comm
6f4f0 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 itInternalChange
6f500 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c s(sqlite3*);.SQL
6f510 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c ITE_PRIVATE Tabl
6f520 65 20 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74 e *sqlite3Result
6f530 53 65 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73 SetOfSelect(Pars
6f540 65 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c e*,Select*);.SQL
6f550 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6f560 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 sqlite3OpenMast
6f570 65 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 2c erTable(Parse *,
6f580 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6f590 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6f5a0 65 33 53 74 61 72 74 54 61 62 6c 65 28 50 61 72 e3StartTable(Par
6f5b0 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e se*,Token*,Token
6f5c0 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e *,int,int,int,in
6f5d0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6f5e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
6f5f0 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c ddColumn(Parse*,
6f600 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
6f610 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6f620 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 ite3AddNotNull(P
6f630 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c arse*, int);.SQL
6f640 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6f650 20 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 sqlite3AddPrima
6f660 72 79 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 ryKey(Parse*, Ex
6f670 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e prList*, int, in
6f680 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
6f690 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6f6a0 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 ite3AddCheckCons
6f6b0 74 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 45 traint(Parse*, E
6f6c0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
6f6d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6f6e0 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 e3AddColumnType(
6f6f0 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a Parse*,Token*);.
6f700 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6f710 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 oid sqlite3AddDe
6f720 66 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 faultValue(Parse
6f730 2a 2c 45 78 70 72 53 70 61 6e 2a 29 3b 0a 53 51 *,ExprSpan*);.SQ
6f740 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6f750 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c d sqlite3AddColl
6f760 61 74 65 54 79 70 65 28 50 61 72 73 65 2a 2c 20 ateType(Parse*,
6f770 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
6f780 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6f790 69 74 65 33 45 6e 64 54 61 62 6c 65 28 50 61 72 ite3EndTable(Par
6f7a0 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e se*,Token*,Token
6f7b0 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 0a 53 51 4c *,Select*);..SQL
6f7c0 49 54 45 5f 50 52 49 56 41 54 45 20 42 69 74 76 ITE_PRIVATE Bitv
6f7d0 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74 76 65 ec *sqlite3Bitve
6f7e0 63 43 72 65 61 74 65 28 75 33 32 29 3b 0a 53 51 cCreate(u32);.SQ
6f7f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6f800 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 sqlite3BitvecTe
6f810 73 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 st(Bitvec*, u32)
6f820 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6f830 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 int sqlite3Bitv
6f840 65 63 53 65 74 28 42 69 74 76 65 63 2a 2c 20 75 ecSet(Bitvec*, u
6f850 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 32);.SQLITE_PRIV
6f860 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6f870 42 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76 BitvecClear(Bitv
6f880 65 63 2a 2c 20 75 33 32 2c 20 76 6f 69 64 2a 29 ec*, u32, void*)
6f890 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6f8a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 void sqlite3Bit
6f8b0 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 76 65 vecDestroy(Bitve
6f8c0 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 c*);.SQLITE_PRIV
6f8d0 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 42 ATE u32 sqlite3B
6f8e0 69 74 76 65 63 53 69 7a 65 28 42 69 74 76 65 63 itvecSize(Bitvec
6f8f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6f900 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 TE int sqlite3Bi
6f910 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74 28 tvecBuiltinTest(
6f920 69 6e 74 2c 69 6e 74 2a 29 3b 0a 0a 53 51 4c 49 int,int*);..SQLI
6f930 54 45 5f 50 52 49 56 41 54 45 20 52 6f 77 53 65 TE_PRIVATE RowSe
6f940 74 20 2a 73 71 6c 69 74 65 33 52 6f 77 53 65 74 t *sqlite3RowSet
6f950 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 76 Init(sqlite3*, v
6f960 6f 69 64 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69 oid*, unsigned i
6f970 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6f980 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6f990 52 6f 77 53 65 74 43 6c 65 61 72 28 52 6f 77 53 RowSetClear(RowS
6f9a0 65 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 et*);.SQLITE_PRI
6f9b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6f9c0 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 52 6f 3RowSetInsert(Ro
6f9d0 77 53 65 74 2a 2c 20 69 36 34 29 3b 0a 53 51 4c wSet*, i64);.SQL
6f9e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6f9f0 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73 sqlite3RowSetTes
6fa00 74 28 52 6f 77 53 65 74 2a 2c 20 75 38 20 69 42 t(RowSet*, u8 iB
6fa10 61 74 63 68 2c 20 69 36 34 29 3b 0a 53 51 4c 49 atch, i64);.SQLI
6fa20 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6fa30 71 6c 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 qlite3RowSetNext
6fa40 28 52 6f 77 53 65 74 2a 2c 20 69 36 34 2a 29 3b (RowSet*, i64*);
6fa50 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
6fa60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 void sqlite3Cre
6fa70 61 74 65 56 69 65 77 28 50 61 72 73 65 2a 2c 54 ateView(Parse*,T
6fa80 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b oken*,Token*,Tok
6fa90 65 6e 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 2c en*,Select*,int,
6faa0 69 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 int);..#if !defi
6fab0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
6fac0 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 VIEW) || !define
6fad0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 d(SQLITE_OMIT_VI
6fae0 52 54 55 41 4c 54 41 42 4c 45 29 0a 53 51 4c 49 RTUALTABLE).SQLI
6faf0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
6fb00 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 sqlite3ViewGetC
6fb10 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65 olumnNames(Parse
6fb20 2a 2c 54 61 62 6c 65 2a 29 3b 0a 23 65 6c 73 65 *,Table*);.#else
6fb30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6fb40 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 3ViewGetColumnNa
6fb50 6d 65 73 28 41 2c 42 29 20 30 0a 23 65 6e 64 69 mes(A,B) 0.#endi
6fb60 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 f..SQLITE_PRIVAT
6fb70 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 E void sqlite3Dr
6fb80 6f 70 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 opTable(Parse*,
6fb90 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 SrcList*, int, i
6fba0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6fbb0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6fbc0 44 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62 6c DeleteTable(Tabl
6fbd0 65 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c e*);.#ifndef SQL
6fbe0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 ITE_OMIT_AUTOINC
6fbf0 52 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 REMENT.SQLITE_PR
6fc00 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
6fc10 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e ite3Autoincremen
6fc20 74 42 65 67 69 6e 28 50 61 72 73 65 20 2a 70 50 tBegin(Parse *pP
6fc30 61 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 arse);.SQLITE_PR
6fc40 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
6fc50 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e ite3Autoincremen
6fc60 74 45 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72 tEnd(Parse *pPar
6fc70 73 65 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 se);.#else.# def
6fc80 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 6f 69 ine sqlite3Autoi
6fc90 6e 63 72 65 6d 65 6e 74 42 65 67 69 6e 28 58 29 ncrementBegin(X)
6fca0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6fcb0 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 3AutoincrementEn
6fcc0 64 28 58 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 d(X).#endif.SQLI
6fcd0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6fce0 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 50 61 sqlite3Insert(Pa
6fcf0 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 rse*, SrcList*,
6fd00 45 78 70 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63 ExprList*, Selec
6fd10 74 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74 t*, IdList*, int
6fd20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6fd30 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 E void *sqlite3A
6fd40 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c rrayAllocate(sql
6fd50 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c ite3*,void*,int,
6fd60 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e int,int*,int*,in
6fd70 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
6fd80 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 ATE IdList *sqli
6fd90 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28 te3IdListAppend(
6fda0 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 sqlite3*, IdList
6fdb0 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
6fdc0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6fdd0 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65 qlite3IdListInde
6fde0 78 28 49 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20 x(IdList*,const
6fdf0 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
6fe00 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a RIVATE SrcList *
6fe10 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e sqlite3SrcListEn
6fe20 6c 61 72 67 65 28 73 71 6c 69 74 65 33 2a 2c 20 large(sqlite3*,
6fe30 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 SrcList*, int, i
6fe40 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6fe50 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c ATE SrcList *sql
6fe60 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e ite3SrcListAppen
6fe70 64 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c d(sqlite3*, SrcL
6fe80 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f ist*, Token*, To
6fe90 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
6fea0 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 IVATE SrcList *s
6feb0 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 qlite3SrcListApp
6fec0 65 6e 64 46 72 6f 6d 54 65 72 6d 28 50 61 72 73 endFromTerm(Pars
6fed0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f e*, SrcList*, To
6fee0 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20 ken*, Token*,.
6fef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ff00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ff10 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65 Token*, Sele
6ff20 63 74 2a 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69 ct*, Expr*, IdLi
6ff30 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
6ff40 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6ff50 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 3SrcListIndexedB
6ff60 79 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 y(Parse *, SrcLi
6ff70 73 74 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a st *, Token *);.
6ff80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6ff90 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65 nt sqlite3Indexe
6ffa0 64 42 79 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 dByLookup(Parse
6ffb0 2a 2c 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 *, struct SrcLis
6ffc0 74 5f 69 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 t_item *);.SQLIT
6ffd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6ffe0 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 qlite3SrcListShi
6fff0 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 ftJoinType(SrcLi
70000 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
70010 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70020 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 3SrcListAssignCu
70030 72 73 6f 72 73 28 50 61 72 73 65 2a 2c 20 53 72 rsors(Parse*, Sr
70040 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f cList*);.SQLITE_
70050 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
70060 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 ite3IdListDelete
70070 28 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 (sqlite3*, IdLis
70080 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
70090 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
700a0 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 73 71 SrcListDelete(sq
700b0 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a lite3*, SrcList*
700c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
700d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 E void sqlite3Cr
700e0 65 61 74 65 49 6e 64 65 78 28 50 61 72 73 65 2a eateIndex(Parse*
700f0 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 ,Token*,Token*,S
70100 72 63 4c 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 rcList*,ExprList
70110 2a 2c 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 *,int,Token*,.
70120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70130 20 20 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e Token*, in
70140 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
70150 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
70160 69 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61 ite3DropIndex(Pa
70170 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 rse*, SrcList*,
70180 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
70190 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
701a0 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 20 53 Select(Parse*, S
701b0 65 6c 65 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 elect*, SelectDe
701c0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
701d0 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c VATE Select *sql
701e0 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 50 61 ite3SelectNew(Pa
701f0 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 rse*,ExprList*,S
70200 72 63 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 rcList*,Expr*,Ex
70210 70 72 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 prList*,.
70220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70230 20 20 45 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 Expr*,ExprList
70240 2a 2c 69 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 *,int,Expr*,Expr
70250 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
70260 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
70270 65 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c 69 electDelete(sqli
70280 74 65 33 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a te3*, Select*);.
70290 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 SQLITE_PRIVATE T
702a0 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 63 able *sqlite3Src
702b0 4c 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 ListLookup(Parse
702c0 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 *, SrcList*);.SQ
702d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
702e0 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e sqlite3IsReadOn
702f0 6c 79 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 ly(Parse*, Table
70300 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
70310 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
70320 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 ite3OpenTable(Pa
70330 72 73 65 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 rse*, int iCur,
70340 69 6e 74 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c int iDb, Table*,
70350 20 69 6e 74 29 3b 0a 23 69 66 20 64 65 66 69 6e int);.#if defin
70360 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ed(SQLITE_ENABLE
70370 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c _UPDATE_DELETE_L
70380 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 IMIT) && !define
70390 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 d(SQLITE_OMIT_SU
703a0 42 51 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 BQUERY).SQLITE_P
703b0 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
703c0 69 74 65 33 4c 69 6d 69 74 57 68 65 72 65 28 50 ite3LimitWhere(P
703d0 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 arse *, SrcList
703e0 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 4c *, Expr *, ExprL
703f0 69 73 74 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 ist *, Expr *, E
70400 78 70 72 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a xpr *, char *);.
70410 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 #endif.SQLITE_PR
70420 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
70430 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 e3DeleteFrom(Par
70440 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 se*, SrcList*, E
70450 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
70460 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
70470 65 33 55 70 64 61 74 65 28 50 61 72 73 65 2a 2c e3Update(Parse*,
70480 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c SrcList*, ExprL
70490 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 ist*, Expr*, int
704a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
704b0 45 20 57 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c E WhereInfo *sql
704c0 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 50 ite3WhereBegin(P
704d0 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c arse*, SrcList*,
704e0 20 45 78 70 72 2a 2c 20 45 78 70 72 4c 69 73 74 Expr*, ExprList
704f0 2a 2a 2c 20 75 31 36 29 3b 0a 53 51 4c 49 54 45 **, u16);.SQLITE
70500 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
70510 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 57 68 lite3WhereEnd(Wh
70520 65 72 65 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 ereInfo*);.SQLIT
70530 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
70540 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 lite3ExprCodeGet
70550 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 20 54 Column(Parse*, T
70560 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c able*, int, int,
70570 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
70580 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
70590 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d sqlite3ExprCodeM
705a0 6f 76 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c ove(Parse*, int,
705b0 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
705c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
705d0 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 sqlite3ExprCodeC
705e0 6f 70 79 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c opy(Parse*, int,
705f0 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
70600 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
70610 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
70620 53 74 6f 72 65 28 50 61 72 73 65 2a 2c 20 69 6e Store(Parse*, in
70630 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 t, int, int);.SQ
70640 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
70650 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 d sqlite3ExprCac
70660 68 65 50 75 73 68 28 50 61 72 73 65 2a 29 3b 0a hePush(Parse*);.
70670 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70680 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
70690 61 63 68 65 50 6f 70 28 50 61 72 73 65 2a 2c 20 achePop(Parse*,
706a0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
706b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
706c0 33 45 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65 3ExprCacheRemove
706d0 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 (Parse*, int);.S
706e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
706f0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 id sqlite3ExprCa
70700 63 68 65 43 6c 65 61 72 28 50 61 72 73 65 2a 29 cheClear(Parse*)
70710 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70720 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
70730 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 rCacheAffinityCh
70740 61 6e 67 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 ange(Parse*, int
70750 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
70760 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
70770 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 te3ExprHardCopy(
70780 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b Parse*,int,int);
70790 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
707a0 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
707b0 6f 64 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 ode(Parse*, Expr
707c0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
707d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
707e0 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 te3ExprCodeTemp(
707f0 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 Parse*, Expr*, i
70800 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nt*);.SQLITE_PRI
70810 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
70820 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 50 ExprCodeTarget(P
70830 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e arse*, Expr*, in
70840 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
70850 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 TE int sqlite3Ex
70860 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28 50 prCodeAndCache(P
70870 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e arse*, Expr*, in
70880 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
70890 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
708a0 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 xprCodeConstants
708b0 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b (Parse*, Expr*);
708c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
708d0 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
708e0 6f 64 65 45 78 70 72 4c 69 73 74 28 50 61 72 73 odeExprList(Pars
708f0 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 e*, ExprList*, i
70900 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
70910 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
70920 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 lite3ExprIfTrue(
70930 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 Parse*, Expr*, i
70940 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
70950 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
70960 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 lite3ExprIfFalse
70970 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 (Parse*, Expr*,
70980 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
70990 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 E_PRIVATE Table
709a0 2a 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c *sqlite3FindTabl
709b0 65 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 e(sqlite3*,const
709c0 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 char*, const ch
709d0 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
709e0 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 VATE Table *sqli
709f0 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 50 te3LocateTable(P
70a00 61 72 73 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 arse*,int isView
70a10 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f ,const char*, co
70a20 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
70a30 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 TE_PRIVATE Index
70a40 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 *sqlite3FindInd
70a50 65 78 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 ex(sqlite3*,cons
70a60 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 t char*, const c
70a70 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
70a80 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
70a90 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 e3UnlinkAndDelet
70aa0 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c eTable(sqlite3*,
70ab0 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 int,const char*)
70ac0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70ad0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c void sqlite3Unl
70ae0 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 inkAndDeleteInde
70af0 78 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 x(sqlite3*,int,c
70b00 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
70b10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
70b20 20 73 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50 sqlite3Vacuum(P
70b30 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 arse*);.SQLITE_P
70b40 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
70b50 65 33 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72 e3RunVacuum(char
70b60 2a 2a 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 **, sqlite3*);.S
70b70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
70b80 61 72 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 ar *sqlite3NameF
70b90 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 romToken(sqlite3
70ba0 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
70bb0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
70bc0 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 qlite3ExprCompar
70bd0 65 28 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b e(Expr*, Expr*);
70be0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70bf0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
70c00 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 AnalyzeAggregate
70c10 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 s(NameContext*,
70c20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
70c30 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
70c40 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 te3ExprAnalyzeAg
70c50 67 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 gList(NameContex
70c60 74 2a 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 t*,ExprList*);.S
70c70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 QLITE_PRIVATE Vd
70c80 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64 be *sqlite3GetVd
70c90 62 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 be(Parse*);.SQLI
70ca0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
70cb0 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 *sqlite3CreateId
70cc0 45 78 70 72 28 50 61 72 73 65 20 2a 2c 20 63 6f Expr(Parse *, co
70cd0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.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 50 72 6e 67 53 61 76 65 53 sqlite3PrngSaveS
70d00 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 tate(void);.SQLI
70d10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
70d20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f sqlite3PrngResto
70d30 72 65 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 reState(void);.S
70d40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
70d50 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 id sqlite3PrngRe
70d60 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 3b 0a setState(void);.
70d70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70d80 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 oid sqlite3Rollb
70d90 61 63 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 ackAll(sqlite3*)
70da0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70db0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 void sqlite3Cod
70dc0 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 50 61 eVerifySchema(Pa
70dd0 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 rse*, int);.SQLI
70de0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
70df0 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e sqlite3BeginTran
70e00 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 saction(Parse*,
70e10 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
70e20 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70e30 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 3CommitTransacti
70e40 6f 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 on(Parse*);.SQLI
70e50 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
70e60 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 sqlite3RollbackT
70e70 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 ransaction(Parse
70e80 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
70e90 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
70ea0 61 76 65 70 6f 69 6e 74 28 50 61 72 73 65 2a 2c avepoint(Parse*,
70eb0 20 69 6e 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 int, Token*);.S
70ec0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
70ed0 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 id sqlite3CloseS
70ee0 61 76 65 70 6f 69 6e 74 73 28 73 71 6c 69 74 65 avepoints(sqlite
70ef0 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 3 *);.SQLITE_PRI
70f00 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
70f10 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 ExprIsConstant(E
70f20 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
70f30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
70f40 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 3ExprIsConstantN
70f50 6f 74 4a 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53 otJoin(Expr*);.S
70f60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
70f70 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 t sqlite3ExprIsC
70f80 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f onstantOrFunctio
70f90 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 n(Expr*);.SQLITE
70fa0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
70fb0 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 ite3ExprIsIntege
70fc0 72 28 45 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a r(Expr*, int*);.
70fd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
70fe0 6e 74 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 nt sqlite3IsRowi
70ff0 64 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a d(const char*);.
71000 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
71010 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 oid sqlite3Gener
71020 61 74 65 52 6f 77 44 65 6c 65 74 65 28 50 61 72 ateRowDelete(Par
71030 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 se*, Table*, int
71040 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 54 72 69 67 , int, int, Trig
71050 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c ger *, int);.SQL
71060 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
71070 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 sqlite3Generate
71080 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28 50 RowIndexDelete(P
71090 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 arse*, Table*, i
710a0 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 nt, int*);.SQLIT
710b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
710c0 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 lite3GenerateInd
710d0 65 78 4b 65 79 28 50 61 72 73 65 2a 2c 20 49 6e exKey(Parse*, In
710e0 64 65 78 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 dex*, int, int,
710f0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
71100 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
71110 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 3GenerateConstra
71120 69 6e 74 43 68 65 63 6b 73 28 50 61 72 73 65 2a intChecks(Parse*
71130 2c 54 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c ,Table*,int,int,
71140 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
71150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71160 20 20 20 20 20 20 69 6e 74 2a 2c 69 6e 74 2c 69 int*,int,i
71170 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a 29 nt,int,int,int*)
71180 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
71190 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d void sqlite3Com
711a0 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 50 pleteInsertion(P
711b0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 arse*, Table*, i
711c0 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 69 nt, int, int*, i
711d0 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 nt, int, int);.S
711e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
711f0 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 t sqlite3OpenTab
71200 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 50 61 72 leAndIndices(Par
71210 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 se*, Table*, int
71220 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
71230 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
71240 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 te3BeginWriteOpe
71250 72 61 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 ration(Parse*, i
71260 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
71270 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
71280 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 50 61 lite3MayAbort(Pa
71290 72 73 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 rse *);.SQLITE_P
712a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
712b0 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e te3HaltConstrain
712c0 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63 t(Parse*, int, c
712d0 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 har*, int);.SQLI
712e0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
712f0 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 *sqlite3ExprDup(
71300 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69 sqlite3*,Expr*,i
71310 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
71320 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71 ATE ExprList *sq
71330 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 lite3ExprListDup
71340 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 4c 69 (sqlite3*,ExprLi
71350 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 st*,int);.SQLITE
71360 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 _PRIVATE SrcList
71370 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 *sqlite3SrcList
71380 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 72 63 Dup(sqlite3*,Src
71390 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 List*,int);.SQLI
713a0 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 TE_PRIVATE IdLis
713b0 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 t *sqlite3IdList
713c0 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 49 64 4c Dup(sqlite3*,IdL
713d0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ist*);.SQLITE_PR
713e0 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 IVATE Select *sq
713f0 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 73 lite3SelectDup(s
71400 71 6c 69 74 65 33 2a 2c 53 65 6c 65 63 74 2a 2c qlite3*,Select*,
71410 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
71420 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
71430 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28 46 3FuncDefInsert(F
71440 75 6e 63 44 65 66 48 61 73 68 2a 2c 20 46 75 6e uncDefHash*, Fun
71450 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 cDef*);.SQLITE_P
71460 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a RIVATE FuncDef *
71470 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 sqlite3FindFunct
71480 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e ion(sqlite3*,con
71490 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e 74 st char*,int,int
714a0 2c 75 38 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 ,u8,int);.SQLITE
714b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
714c0 6c 69 74 65 33 52 65 67 69 73 74 65 72 42 75 69 lite3RegisterBui
714d0 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 73 71 ltinFunctions(sq
714e0 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f lite3*);.SQLITE_
714f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71500 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65 ite3RegisterDate
71510 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76 6f TimeFunctions(vo
71520 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 id);.SQLITE_PRIV
71530 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
71540 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75 RegisterGlobalFu
71550 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 3b 0a 23 nctions(void);.#
71560 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
71570 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
71580 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53 E int sqlite3S
71590 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65 33 2a afetyOn(sqlite3*
715a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
715b0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53 E int sqlite3S
715c0 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33 afetyOff(sqlite3
715d0 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 *);.#else.# defi
715e0 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 ne sqlite3Safety
715f0 4f 6e 28 41 29 20 30 0a 23 20 64 65 66 69 6e 65 On(A) 0.# define
71600 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
71610 66 28 41 29 20 30 0a 23 65 6e 64 69 66 0a 53 51 f(A) 0.#endif.SQ
71620 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
71630 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 sqlite3SafetyCh
71640 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b eckOk(sqlite3*);
71650 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71660 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 int sqlite3Safet
71670 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 yCheckSickOrOk(s
71680 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 qlite3*);.SQLITE
71690 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
716a0 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 lite3ChangeCooki
716b0 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a e(Parse*, int);.
716c0 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
716d0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 LITE_OMIT_VIEW)
716e0 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
716f0 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 TE_OMIT_TRIGGER)
71700 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71710 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 74 65 void sqlite3Mate
71720 72 69 61 6c 69 7a 65 56 69 65 77 28 50 61 72 73 rializeView(Pars
71730 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70 72 e*, Table*, Expr
71740 2a 2c 20 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a *, int);.#endif.
71750 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
71760 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 51 4c OMIT_TRIGGER.SQL
71770 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
71780 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 id sqlite3BeginT
71790 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54 rigger(Parse*, T
717a0 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 oken*,Token*,int
717b0 2c 69 6e 74 2c 49 64 4c 69 73 74 2a 2c 53 72 63 ,int,IdList*,Src
717c0 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 List*,.
717d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
717e0 20 20 45 78 70 72 2a 2c 69 6e 74 2c 20 69 6e 74 Expr*,int, int
717f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71800 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
71810 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 50 61 FinishTrigger(Pa
71820 72 73 65 2a 2c 20 54 72 69 67 67 65 72 53 74 65 rse*, TriggerSte
71830 70 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c p*, Token*);.SQL
71840 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
71850 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 id sqlite3DropTr
71860 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 53 72 igger(Parse*, Sr
71870 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 cList*, int);.SQ
71880 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
71890 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 oid sqlite3DropT
718a0 72 69 67 67 65 72 50 74 72 28 50 61 72 73 65 2a riggerPtr(Parse*
718b0 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c , Trigger*);.SQL
718c0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 ITE_PRIVATE Tr
718d0 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33 54 72 igger *sqlite3Tr
718e0 69 67 67 65 72 73 45 78 69 73 74 28 50 61 72 73 iggersExist(Pars
718f0 65 20 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 e *, Table*, int
71900 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 , ExprList*, int
71910 20 2a 70 4d 61 73 6b 29 3b 0a 53 51 4c 49 54 45 *pMask);.SQLITE
71920 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 _PRIVATE Trigg
71930 65 72 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 er *sqlite3Trigg
71940 65 72 4c 69 73 74 28 50 61 72 73 65 20 2a 2c 20 erList(Parse *,
71950 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 Table *);.SQLITE
71960 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
71970 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 sqlite3CodeRowTr
71980 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54 72 igger(Parse*, Tr
71990 69 67 67 65 72 20 2a 2c 20 69 6e 74 2c 20 45 78 igger *, int, Ex
719a0 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 54 61 prList*, int, Ta
719b0 62 6c 65 20 2a 2c 0a 20 20 20 20 20 20 20 20 20 ble *,.
719c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
719d0 20 20 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 int, int, int
719e0 2c 20 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 73 , int);. void s
719f0 71 6c 69 74 65 56 69 65 77 54 72 69 67 67 65 72 qliteViewTrigger
71a00 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a s(Parse*, Table*
71a10 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78 , Expr*, int, Ex
71a20 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 prList*);.SQLITE
71a30 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
71a40 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 sqlite3DeleteTri
71a50 67 67 65 72 53 74 65 70 28 73 71 6c 69 74 65 33 ggerStep(sqlite3
71a60 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 29 *, TriggerStep*)
71a70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
71a80 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a TriggerStep *
71a90 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 sqlite3TriggerSe
71aa0 6c 65 63 74 53 74 65 70 28 73 71 6c 69 74 65 33 lectStep(sqlite3
71ab0 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 *,Select*);.SQLI
71ac0 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 TE_PRIVATE Tri
71ad0 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 ggerStep *sqlite
71ae0 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 3TriggerInsertSt
71af0 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 ep(sqlite3*,Toke
71b00 6e 2a 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20 n*, IdList*,.
71b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71b30 20 20 20 20 20 45 78 70 72 4c 69 73 74 2a 2c 53 ExprList*,S
71b40 65 6c 65 63 74 2a 2c 75 38 29 3b 0a 53 51 4c 49 elect*,u8);.SQLI
71b50 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 TE_PRIVATE Tri
71b60 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 ggerStep *sqlite
71b70 33 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74 3TriggerUpdateSt
71b80 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 ep(sqlite3*,Toke
71b90 6e 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 n*,ExprList*, Ex
71ba0 70 72 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 pr*, u8);.SQLITE
71bb0 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 _PRIVATE Trigg
71bc0 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 erStep *sqlite3T
71bd0 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 riggerDeleteStep
71be0 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a (sqlite3*,Token*
71bf0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
71c00 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
71c10 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 sqlite3DeleteTri
71c20 67 67 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54 gger(sqlite3*, T
71c30 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 rigger*);.SQLITE
71c40 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
71c50 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 sqlite3UnlinkAnd
71c60 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 DeleteTrigger(sq
71c70 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 lite3*,int,const
71c80 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
71c90 50 52 49 56 41 54 45 20 20 20 75 33 32 20 73 71 PRIVATE u32 sq
71ca0 6c 69 74 65 33 54 72 69 67 67 65 72 4f 6c 64 6d lite3TriggerOldm
71cb0 61 73 6b 28 50 61 72 73 65 2a 2c 54 72 69 67 67 ask(Parse*,Trigg
71cc0 65 72 2a 2c 69 6e 74 2c 45 78 70 72 4c 69 73 74 er*,int,ExprList
71cd0 2a 2c 54 61 62 6c 65 2a 2c 69 6e 74 29 3b 0a 23 *,Table*,int);.#
71ce0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 define sqlite3P
71cf0 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 29 20 arseToplevel(p)
71d00 28 28 70 29 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 ((p)->pToplevel
71d10 3f 20 28 70 29 2d 3e 70 54 6f 70 6c 65 76 65 6c ? (p)->pToplevel
71d20 20 3a 20 28 70 29 29 0a 23 65 6c 73 65 0a 23 20 : (p)).#else.#
71d30 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 define sqlite3Tr
71d40 69 67 67 65 72 73 45 78 69 73 74 28 42 2c 43 2c iggersExist(B,C,
71d50 44 2c 45 2c 46 29 20 30 0a 23 20 64 65 66 69 6e D,E,F) 0.# defin
71d60 65 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 e sqlite3DeleteT
71d70 72 69 67 67 65 72 28 41 2c 42 29 0a 23 20 64 65 rigger(A,B).# de
71d80 66 69 6e 65 20 73 71 6c 69 74 65 33 44 72 6f 70 fine sqlite3Drop
71d90 54 72 69 67 67 65 72 50 74 72 28 41 2c 42 29 0a TriggerPtr(A,B).
71da0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
71db0 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 UnlinkAndDeleteT
71dc0 72 69 67 67 65 72 28 41 2c 42 2c 43 29 0a 23 20 rigger(A,B,C).#
71dd0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f define sqlite3Co
71de0 64 65 52 6f 77 54 72 69 67 67 65 72 28 41 2c 42 deRowTrigger(A,B
71df0 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 2c 4a ,C,D,E,F,G,H,I,J
71e00 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
71e10 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 58 2c e3TriggerList(X,
71e20 20 59 29 20 30 0a 23 20 64 65 66 69 6e 65 20 73 Y) 0.# define s
71e30 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 qlite3ParseTople
71e40 76 65 6c 28 70 29 20 70 0a 23 20 64 65 66 69 6e vel(p) p.# defin
71e50 65 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 e sqlite3Trigger
71e60 4f 6c 64 6d 61 73 6b 28 41 2c 42 2c 43 2c 44 2c Oldmask(A,B,C,D,
71e70 45 2c 46 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 E,F) 0.#endif..S
71e80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
71e90 74 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 t sqlite3JoinTyp
71ea0 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a e(Parse*, Token*
71eb0 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a , Token*, Token*
71ec0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71ed0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 E void sqlite3Cr
71ee0 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 50 eateForeignKey(P
71ef0 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a arse*, ExprList*
71f00 2c 20 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 4c 69 , Token*, ExprLi
71f10 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 st*, int);.SQLIT
71f20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
71f30 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 qlite3DeferForei
71f40 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20 69 6e gnKey(Parse*, in
71f50 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 t);.#ifndef SQLI
71f60 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a TE_OMIT_AUTHORIZ
71f70 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 ATION.SQLITE_PRI
71f80 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
71f90 74 65 33 41 75 74 68 52 65 61 64 28 50 61 72 73 te3AuthRead(Pars
71fa0 65 2a 2c 45 78 70 72 2a 2c 53 63 68 65 6d 61 2a e*,Expr*,Schema*
71fb0 2c 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 ,SrcList*);.SQLI
71fc0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
71fd0 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 sqlite3AuthChec
71fe0 6b 28 50 61 72 73 65 2a 2c 69 6e 74 2c 20 63 6f k(Parse*,int, co
71ff0 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 nst char*, const
72000 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 char*, const ch
72010 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
72020 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
72030 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 te3AuthContextPu
72040 73 68 28 50 61 72 73 65 2a 2c 20 41 75 74 68 43 sh(Parse*, AuthC
72050 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 ontext*, const c
72060 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
72070 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
72080 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 ite3AuthContextP
72090 6f 70 28 41 75 74 68 43 6f 6e 74 65 78 74 2a 29 op(AuthContext*)
720a0 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ;.#else.# define
720b0 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 sqlite3AuthRead
720c0 28 61 2c 62 2c 63 2c 64 29 0a 23 20 64 65 66 69 (a,b,c,d).# defi
720d0 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 ne sqlite3AuthCh
720e0 65 63 6b 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 eck(a,b,c,d,e)
720f0 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64 65 SQLITE_OK.# de
72100 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 fine sqlite3Auth
72110 43 6f 6e 74 65 78 74 50 75 73 68 28 61 2c 62 2c ContextPush(a,b,
72120 63 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 c).# define sqli
72130 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f te3AuthContextPo
72140 70 28 61 29 20 20 28 28 76 6f 69 64 29 28 61 29 p(a) ((void)(a)
72150 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f ).#endif.SQLITE_
72160 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
72170 69 74 65 33 41 74 74 61 63 68 28 50 61 72 73 65 ite3Attach(Parse
72180 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c *, Expr*, Expr*,
72190 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
721a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
721b0 69 74 65 33 44 65 74 61 63 68 28 50 61 72 73 65 ite3Detach(Parse
721c0 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 *, Expr*);.SQLIT
721d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
721e0 6c 69 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 lite3BtreeFactor
721f0 79 28 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 20 y(const sqlite3
72200 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *db, const char
72210 2a 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 *zFilename,.
72220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72230 20 20 20 69 6e 74 20 6f 6d 69 74 4a 6f 75 72 6e int omitJourn
72240 61 6c 2c 20 69 6e 74 20 6e 43 61 63 68 65 2c 20 al, int nCache,
72250 69 6e 74 20 66 6c 61 67 73 2c 20 42 74 72 65 65 int flags, Btree
72260 20 2a 2a 70 70 42 74 72 65 65 29 3b 0a 53 51 4c **ppBtree);.SQL
72270 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
72280 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 44 sqlite3FixInit(D
72290 62 46 69 78 65 72 2a 2c 20 50 61 72 73 65 2a 2c bFixer*, Parse*,
722a0 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 int, const char
722b0 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 *, const Token*)
722c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
722d0 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 int sqlite3FixS
722e0 72 63 4c 69 73 74 28 44 62 46 69 78 65 72 2a 2c rcList(DbFixer*,
722f0 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 SrcList*);.SQLI
72300 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
72310 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 qlite3FixSelect(
72320 44 62 46 69 78 65 72 2a 2c 20 53 65 6c 65 63 74 DbFixer*, Select
72330 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
72340 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 TE int sqlite3Fi
72350 78 45 78 70 72 28 44 62 46 69 78 65 72 2a 2c 20 xExpr(DbFixer*,
72360 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
72370 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
72380 65 33 46 69 78 45 78 70 72 4c 69 73 74 28 44 62 e3FixExprList(Db
72390 46 69 78 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 Fixer*, ExprList
723a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
723b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 TE int sqlite3Fi
723c0 78 54 72 69 67 67 65 72 53 74 65 70 28 44 62 46 xTriggerStep(DbF
723d0 69 78 65 72 2a 2c 20 54 72 69 67 67 65 72 53 74 ixer*, TriggerSt
723e0 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ep*);.SQLITE_PRI
723f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
72400 41 74 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 20 AtoF(const char
72410 2a 7a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 53 51 *z, double*);.SQ
72420 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
72430 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 sqlite3GetInt32
72440 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 (const char *, i
72450 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nt*);.SQLITE_PRI
72460 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
72470 46 69 74 73 49 6e 36 34 42 69 74 73 28 63 6f 6e FitsIn64Bits(con
72480 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b st char *, int);
72490 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
724a0 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 31 36 int sqlite3Utf16
724b0 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f ByteLen(const vo
724c0 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e id *pData, int n
724d0 43 68 61 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Char);.SQLITE_PR
724e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
724f0 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e 3Utf8CharLen(con
72500 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 st char *pData,
72510 69 6e 74 20 6e 42 79 74 65 29 3b 0a 53 51 4c 49 int nByte);.SQLI
72520 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
72530 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 63 qlite3Utf8Read(c
72540 6f 6e 73 74 20 75 38 2a 2c 20 63 6f 6e 73 74 20 onst u8*, const
72550 75 38 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f u8**);../*.** Ro
72560 75 74 69 6e 65 73 20 74 6f 20 72 65 61 64 20 61 utines to read a
72570 6e 64 20 77 72 69 74 65 20 76 61 72 69 61 62 6c nd write variabl
72580 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e-length integer
72590 73 2e 20 20 54 68 65 73 65 20 75 73 65 64 20 74 s. These used t
725a0 6f 0a 2a 2a 20 62 65 20 64 65 66 69 6e 65 64 20 o.** be defined
725b0 6c 6f 63 61 6c 6c 79 2c 20 62 75 74 20 6e 6f 77 locally, but now
725c0 20 77 65 20 75 73 65 20 74 68 65 20 76 61 72 69 we use the vari
725d0 6e 74 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 nt routines in t
725e0 68 65 20 75 74 69 6c 2e 63 0a 2a 2a 20 66 69 6c he util.c.** fil
725f0 65 2e 20 20 43 6f 64 65 20 73 68 6f 75 6c 64 20 e. Code should
72600 75 73 65 20 74 68 65 20 4d 41 43 52 4f 20 66 6f use the MACRO fo
72610 72 6d 73 20 62 65 6c 6f 77 2c 20 61 73 20 74 68 rms below, as th
72620 65 20 56 61 72 69 6e 74 33 32 20 76 65 72 73 69 e Varint32 versi
72630 6f 6e 73 0a 2a 2a 20 61 72 65 20 63 6f 64 65 64 ons.** are coded
72640 20 74 6f 20 61 73 73 75 6d 65 20 74 68 65 20 73 to assume the s
72650 69 6e 67 6c 65 20 62 79 74 65 20 63 61 73 65 20 ingle byte case
72660 69 73 20 61 6c 72 65 61 64 79 20 68 61 6e 64 6c is already handl
72670 65 64 20 28 77 68 69 63 68 20 0a 2a 2a 20 74 68 ed (which .** th
72680 65 20 4d 41 43 52 4f 20 66 6f 72 6d 20 64 6f 65 e MACRO form doe
72690 73 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 s)..*/.SQLITE_PR
726a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
726b0 33 50 75 74 56 61 72 69 6e 74 28 75 6e 73 69 67 3PutVarint(unsig
726c0 6e 65 64 20 63 68 61 72 2a 2c 20 75 36 34 29 3b ned char*, u64);
726d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
726e0 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 int sqlite3PutVa
726f0 72 69 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 20 rint32(unsigned
72700 63 68 61 72 2a 2c 20 75 33 32 29 3b 0a 53 51 4c char*, u32);.SQL
72710 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 ITE_PRIVATE u8 s
72720 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 qlite3GetVarint(
72730 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
72740 68 61 72 20 2a 2c 20 75 36 34 20 2a 29 3b 0a 53 har *, u64 *);.S
72750 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 QLITE_PRIVATE u8
72760 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e sqlite3GetVarin
72770 74 33 32 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e t32(const unsign
72780 65 64 20 63 68 61 72 20 2a 2c 20 75 33 32 20 2a ed char *, u32 *
72790 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
727a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 72 E int sqlite3Var
727b0 69 6e 74 4c 65 6e 28 75 36 34 20 76 29 3b 0a 0a intLen(u64 v);..
727c0 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64 65 72 /*.** The header
727d0 20 6f 66 20 61 20 72 65 63 6f 72 64 20 63 6f 6e of a record con
727e0 73 69 73 74 73 20 6f 66 20 61 20 73 65 71 75 65 sists of a seque
727f0 6e 63 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e nce variable-len
72800 67 74 68 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a gth integers..**
72810 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 73 20 These integers
72820 61 72 65 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 are almost alway
72830 73 20 73 6d 61 6c 6c 20 61 6e 64 20 61 72 65 20 s small and are
72840 65 6e 63 6f 64 65 64 20 61 73 20 61 20 73 69 6e encoded as a sin
72850 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20 54 68 65 gle byte..** The
72860 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f following macro
72870 73 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65 s take advantage
72880 20 74 68 69 73 20 66 61 63 74 20 74 6f 20 70 72 this fact to pr
72890 6f 76 69 64 65 20 61 20 66 61 73 74 20 65 6e 63 ovide a fast enc
728a0 6f 64 65 0a 2a 2a 20 61 6e 64 20 64 65 63 6f 64 ode.** and decod
728b0 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 e of the integer
728c0 73 20 69 6e 20 61 20 72 65 63 6f 72 64 20 68 65 s in a record he
728d0 61 64 65 72 2e 20 20 49 74 20 69 73 20 66 61 73 ader. It is fas
728e0 74 65 72 20 66 6f 72 20 74 68 65 20 63 6f 6d 6d ter for the comm
728f0 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 65 on.** case where
72900 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 73 20 the integer is
72910 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 20 20 a single byte.
72920 49 74 20 69 73 20 61 20 6c 69 74 74 6c 65 20 73 It is a little s
72930 6c 6f 77 65 72 20 77 68 65 6e 20 74 68 65 0a 2a lower when the.*
72940 2a 20 69 6e 74 65 67 65 72 20 69 73 20 74 77 6f * integer is two
72950 20 6f 72 20 6d 6f 72 65 20 62 79 74 65 73 2e 20 or more bytes.
72960 20 42 75 74 20 6f 76 65 72 61 6c 6c 20 69 74 20 But overall it
72970 69 73 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a is faster..**.**
72980 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 The following e
72990 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 65 xpressions are e
729a0 71 75 69 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a quivalent:.**.**
729b0 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65 33 x = sqlite3
729c0 47 65 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 GetVarint32( A,
729d0 26 42 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d &B );.** x =
729e0 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e sqlite3PutVarin
729f0 74 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a t32( A, B );.**.
72a00 2a 2a 20 20 20 20 20 78 20 3d 20 67 65 74 56 61 ** x = getVa
72a10 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a rint32( A, B );.
72a20 2a 2a 20 20 20 20 20 78 20 3d 20 70 75 74 56 61 ** x = putVa
72a30 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a rint32( A, B );.
72a40 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 **.*/.#define ge
72a50 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20 tVarint32(A,B)
72a60 28 75 38 29 28 28 2a 28 41 29 3c 28 75 38 29 30 (u8)((*(A)<(u8)0
72a70 78 38 30 29 20 3f 20 28 28 42 29 20 3d 20 28 75 x80) ? ((B) = (u
72a80 33 32 29 2a 28 41 29 29 2c 31 20 3a 20 73 71 6c 32)*(A)),1 : sql
72a90 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 ite3GetVarint32(
72aa0 28 41 29 2c 20 28 75 33 32 20 2a 29 26 28 42 29 (A), (u32 *)&(B)
72ab0 29 29 0a 23 64 65 66 69 6e 65 20 70 75 74 56 61 )).#define putVa
72ac0 72 69 6e 74 33 32 28 41 2c 42 29 20 20 28 75 38 rint32(A,B) (u8
72ad0 29 28 28 28 75 33 32 29 28 42 29 3c 28 75 33 32 )(((u32)(B)<(u32
72ae0 29 30 78 38 30 29 20 3f 20 28 2a 28 41 29 20 3d )0x80) ? (*(A) =
72af0 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 (unsigned char)
72b00 28 42 29 29 2c 31 20 3a 20 73 71 6c 69 74 65 33 (B)),1 : sqlite3
72b10 50 75 74 56 61 72 69 6e 74 33 32 28 28 41 29 2c PutVarint32((A),
72b20 20 28 42 29 29 29 0a 23 64 65 66 69 6e 65 20 67 (B))).#define g
72b30 65 74 56 61 72 69 6e 74 20 20 20 20 73 71 6c 69 etVarint sqli
72b40 74 65 33 47 65 74 56 61 72 69 6e 74 0a 23 64 65 te3GetVarint.#de
72b50 66 69 6e 65 20 70 75 74 56 61 72 69 6e 74 20 20 fine putVarint
72b60 20 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 sqlite3PutVari
72b70 6e 74 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt...SQLITE_PRIV
72b80 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
72b90 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 sqlite3IndexAffi
72ba0 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20 nityStr(Vdbe *,
72bb0 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49 54 45 Index *);.SQLITE
72bc0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
72bd0 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 lite3TableAffini
72be0 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20 54 61 tyStr(Vdbe *, Ta
72bf0 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ble *);.SQLITE_P
72c00 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 RIVATE char sqli
72c10 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 te3CompareAffini
72c20 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 ty(Expr *pExpr,
72c30 63 68 61 72 20 61 66 66 32 29 3b 0a 53 51 4c 49 char aff2);.SQLI
72c40 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
72c50 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e qlite3IndexAffin
72c60 69 74 79 4f 6b 28 45 78 70 72 20 2a 70 45 78 70 ityOk(Expr *pExp
72c70 72 2c 20 63 68 61 72 20 69 64 78 5f 61 66 66 69 r, char idx_affi
72c80 6e 69 74 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 nity);.SQLITE_PR
72c90 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 IVATE char sqlit
72ca0 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 45 e3ExprAffinity(E
72cb0 78 70 72 20 2a 70 45 78 70 72 29 3b 0a 53 51 4c xpr *pExpr);.SQL
72cc0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
72cd0 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 63 6f sqlite3Atoi64(co
72ce0 6e 73 74 20 63 68 61 72 2a 2c 20 69 36 34 2a 29 nst char*, i64*)
72cf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72d00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 void sqlite3Err
72d10 6f 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 or(sqlite3*, int
72d20 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e , const char*,..
72d30 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .);.SQLITE_PRIVA
72d40 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
72d50 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 HexToBlob(sqlite
72d60 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 3*, const char *
72d70 7a 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 z, int n);.SQLIT
72d80 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
72d90 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 lite3TwoPartName
72da0 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 (Parse *, Token
72db0 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 *, Token *, Toke
72dc0 6e 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 n **);.SQLITE_PR
72dd0 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 IVATE const char
72de0 20 2a 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 *sqlite3ErrStr(
72df0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
72e00 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
72e10 52 65 61 64 53 63 68 65 6d 61 28 50 61 72 73 65 ReadSchema(Parse
72e20 20 2a 70 50 61 72 73 65 29 3b 0a 53 51 4c 49 54 *pParse);.SQLIT
72e30 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 E_PRIVATE CollSe
72e40 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 43 6f q *sqlite3FindCo
72e50 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a 2c 75 llSeq(sqlite3*,u
72e60 38 20 65 6e 63 2c 20 63 6f 6e 73 74 20 63 68 61 8 enc, const cha
72e70 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f r*,int);.SQLITE_
72e80 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 PRIVATE CollSeq
72e90 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f *sqlite3LocateCo
72ea0 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 llSeq(Parse *pPa
72eb0 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a rse, const char*
72ec0 7a 4e 61 6d 65 29 3b 0a 53 51 4c 49 54 45 5f 50 zName);.SQLITE_P
72ed0 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a RIVATE CollSeq *
72ee0 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 sqlite3ExprCollS
72ef0 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 eq(Parse *pParse
72f00 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a , Expr *pExpr);.
72f10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 SQLITE_PRIVATE E
72f20 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 xpr *sqlite3Expr
72f30 53 65 74 43 6f 6c 6c 28 50 61 72 73 65 20 2a 70 SetColl(Parse *p
72f40 50 61 72 73 65 2c 20 45 78 70 72 20 2a 2c 20 54 Parse, Expr *, T
72f50 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f oken *);.SQLITE_
72f60 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
72f70 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 te3CheckCollSeq(
72f80 50 61 72 73 65 20 2a 2c 20 43 6f 6c 6c 53 65 71 Parse *, CollSeq
72f90 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
72fa0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 ATE int sqlite3C
72fb0 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 heckObjectName(P
72fc0 61 72 73 65 20 2a 2c 20 63 6f 6e 73 74 20 63 68 arse *, const ch
72fd0 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ar *);.SQLITE_PR
72fe0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
72ff0 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 e3VdbeSetChanges
73000 28 73 71 6c 69 74 65 33 20 2a 2c 20 69 6e 74 29 (sqlite3 *, int)
73010 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ;..SQLITE_PRIVAT
73020 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 E const void *sq
73030 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 73 lite3ValueText(s
73040 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75 qlite3_value*, u
73050 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 8);.SQLITE_PRIVA
73060 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 TE int sqlite3Va
73070 6c 75 65 42 79 74 65 73 28 73 71 6c 69 74 65 33 lueBytes(sqlite3
73080 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 51 _value*, u8);.SQ
73090 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
730a0 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 d sqlite3ValueSe
730b0 74 53 74 72 28 73 71 6c 69 74 65 33 5f 76 61 6c tStr(sqlite3_val
730c0 75 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 ue*, int, const
730d0 76 6f 69 64 20 2a 2c 75 38 2c 20 0a 20 20 20 20 void *,u8, .
730e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
730f0 20 20 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 void(*)(void
73100 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *));.SQLITE_PRIV
73110 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
73120 56 61 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65 ValueFree(sqlite
73130 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 3_value*);.SQLIT
73140 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 E_PRIVATE sqlite
73150 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 3_value *sqlite3
73160 56 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33 ValueNew(sqlite3
73170 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
73180 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
73190 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69 74 65 3Utf16to8(sqlite
731a0 33 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 3 *, const void*
731b0 2c 20 69 6e 74 29 3b 0a 23 69 66 64 65 66 20 53 , int);.#ifdef S
731c0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 QLITE_ENABLE_STA
731d0 54 32 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 T2.SQLITE_PRIVAT
731e0 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 E char *sqlite3U
731f0 74 66 38 74 6f 31 36 28 73 71 6c 69 74 65 33 20 tf8to16(sqlite3
73200 2a 2c 20 75 38 2c 20 63 68 61 72 20 2a 2c 20 69 *, u8, char *, i
73210 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 23 65 6e 64 nt, int *);.#end
73220 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 if.SQLITE_PRIVAT
73230 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c E int sqlite3Val
73240 75 65 46 72 6f 6d 45 78 70 72 28 73 71 6c 69 74 ueFromExpr(sqlit
73250 65 33 20 2a 2c 20 45 78 70 72 20 2a 2c 20 75 38 e3 *, Expr *, u8
73260 2c 20 75 38 2c 20 73 71 6c 69 74 65 33 5f 76 61 , u8, sqlite3_va
73270 6c 75 65 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f lue **);.SQLITE_
73280 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
73290 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 ite3ValueApplyAf
732a0 66 69 6e 69 74 79 28 73 71 6c 69 74 65 33 5f 76 finity(sqlite3_v
732b0 61 6c 75 65 20 2a 2c 20 75 38 2c 20 75 38 29 3b alue *, u8, u8);
732c0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
732d0 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c AMALGAMATION.SQL
732e0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
732f0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
73300 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f sqlite3UpperToLo
73310 77 65 72 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 wer[];.SQLITE_PR
73320 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 IVATE const unsi
73330 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 gned char sqlite
73340 33 43 74 79 70 65 4d 61 70 5b 5d 3b 0a 53 51 4c 3CtypeMap[];.SQL
73350 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 ITE_PRIVATE SQLI
73360 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 53 71 TE_WSD struct Sq
73370 6c 69 74 65 33 43 6f 6e 66 69 67 20 73 71 6c 69 lite3Config sqli
73380 74 65 33 43 6f 6e 66 69 67 3b 0a 53 51 4c 49 54 te3Config;.SQLIT
73390 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 E_PRIVATE SQLITE
733a0 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61 73 68 _WSD FuncDefHash
733b0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 sqlite3GlobalFu
733c0 6e 63 74 69 6f 6e 73 3b 0a 53 51 4c 49 54 45 5f nctions;.SQLITE_
733d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
733e0 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 3b 0a te3PendingByte;.
733f0 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 #endif.SQLITE_PR
73400 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
73410 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 e3RootPageMoved(
73420 44 62 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a Db*, int, int);.
73430 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
73440 6f 69 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64 oid sqlite3Reind
73450 65 78 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e ex(Parse*, Token
73460 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
73470 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
73480 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63 sqlite3AlterFunc
73490 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b tions(sqlite3*);
734a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
734b0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 void sqlite3Alte
734c0 72 52 65 6e 61 6d 65 54 61 62 6c 65 28 50 61 72 rRenameTable(Par
734d0 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 se*, SrcList*, T
734e0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
734f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
73500 65 33 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 e3GetToken(const
73510 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
73520 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 , int *);.SQLITE
73530 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
73540 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 lite3NestedParse
73550 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 (Parse*, const c
73560 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 har*, ...);.SQLI
73570 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
73580 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 sqlite3ExpirePre
73590 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 paredStatements(
735a0 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 sqlite3*);.SQLIT
735b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
735c0 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c qlite3CodeSubsel
735d0 65 63 74 28 50 61 72 73 65 20 2a 2c 20 45 78 70 ect(Parse *, Exp
735e0 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a r *, int, int);.
735f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
73600 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 oid sqlite3Selec
73610 74 50 72 65 70 28 50 61 72 73 65 2a 2c 20 53 65 tPrep(Parse*, Se
73620 6c 65 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 lect*, NameConte
73630 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 xt*);.SQLITE_PRI
73640 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
73650 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
73660 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 (NameContext*, E
73670 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
73680 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
73690 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e e3ResolveSelectN
736a0 61 6d 65 73 28 50 61 72 73 65 2a 2c 20 53 65 6c ames(Parse*, Sel
736b0 65 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 ect*, NameContex
736c0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
736d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
736e0 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 esolveOrderGroup
736f0 42 79 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 By(Parse*, Selec
73700 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 t*, ExprList*, c
73710 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
73720 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
73730 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 sqlite3ColumnDe
73740 66 61 75 6c 74 28 56 64 62 65 20 2a 2c 20 54 61 fault(Vdbe *, Ta
73750 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 ble *, int, int)
73760 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73770 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 void sqlite3Alt
73780 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d erFinishAddColum
73790 6e 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e n(Parse *, Token
737a0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
737b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
737c0 41 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c AlterBeginAddCol
737d0 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 53 72 63 umn(Parse *, Src
737e0 4c 69 73 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f List *);.SQLITE_
737f0 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 PRIVATE CollSeq
73800 2a 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 *sqlite3GetCollS
73810 65 71 28 73 71 6c 69 74 65 33 2a 2c 20 75 38 2c eq(sqlite3*, u8,
73820 20 43 6f 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e 73 CollSeq *, cons
73830 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
73840 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 _PRIVATE char sq
73850 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 lite3AffinityTyp
73860 65 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a e(const char*);.
73870 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
73880 6f 69 64 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 oid sqlite3Analy
73890 7a 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e ze(Parse*, Token
738a0 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
738b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
738c0 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 qlite3InvokeBusy
738d0 48 61 6e 64 6c 65 72 28 42 75 73 79 48 61 6e 64 Handler(BusyHand
738e0 6c 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ler*);.SQLITE_PR
738f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
73900 33 46 69 6e 64 44 62 28 73 71 6c 69 74 65 33 2a 3FindDb(sqlite3*
73910 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 , Token*);.SQLIT
73920 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
73930 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 lite3FindDbName(
73940 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 sqlite3 *, const
73950 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 char *);.SQLITE
73960 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
73970 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 ite3AnalysisLoad
73980 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 69 44 (sqlite3*,int iD
73990 42 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 B);.SQLITE_PRIVA
739a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
739b0 65 6c 65 74 65 49 6e 64 65 78 53 61 6d 70 6c 65 eleteIndexSample
739c0 73 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54 s(Index*);.SQLIT
739d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
739e0 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 qlite3DefaultRow
739f0 45 73 74 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c Est(Index*);.SQL
73a00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
73a10 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 sqlite3Register
73a20 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71 LikeFunctions(sq
73a30 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 lite3*, int);.SQ
73a40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
73a50 20 73 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 sqlite3IsLikeFu
73a60 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c nction(sqlite3*,
73a70 45 78 70 72 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a Expr*,int*,char*
73a80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73a90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 69 E void sqlite3Mi
73aa0 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 nimumFileFormat(
73ab0 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 Parse*, int, int
73ac0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73ad0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 E void sqlite3Sc
73ae0 68 65 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 29 hemaFree(void *)
73af0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73b00 20 53 63 68 65 6d 61 20 2a 73 71 6c 69 74 65 33 Schema *sqlite3
73b10 53 63 68 65 6d 61 47 65 74 28 73 71 6c 69 74 65 SchemaGet(sqlite
73b20 33 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 53 3 *, Btree *);.S
73b30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
73b40 74 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 t sqlite3SchemaT
73b50 6f 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a oIndex(sqlite3 *
73b60 64 62 2c 20 53 63 68 65 6d 61 20 2a 29 3b 0a 53 db, Schema *);.S
73b70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 4b 65 QLITE_PRIVATE Ke
73b80 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 49 6e yInfo *sqlite3In
73b90 64 65 78 4b 65 79 69 6e 66 6f 28 50 61 72 73 65 dexKeyinfo(Parse
73ba0 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 *, Index *);.SQ
73bb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
73bc0 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 sqlite3CreateFu
73bd0 6e 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f nc(sqlite3 *, co
73be0 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c nst char *, int,
73bf0 20 69 6e 74 2c 20 76 6f 69 64 20 2a 2c 20 0a 20 int, void *, .
73c00 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 void (*)(sqlite
73c10 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 3_context*,int,s
73c20 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 qlite3_value **)
73c30 2c 0a 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c ,. void (*)(sql
73c40 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e ite3_context*,in
73c50 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 t,sqlite3_value
73c60 2a 2a 29 2c 20 76 6f 69 64 20 28 2a 29 28 73 71 **), void (*)(sq
73c70 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29 lite3_context*))
73c80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73c90 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 int sqlite3ApiE
73ca0 78 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c xit(sqlite3 *db,
73cb0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
73cc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
73cd0 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 3OpenTempDatabas
73ce0 65 28 50 61 72 73 65 20 2a 29 3b 0a 0a 53 51 4c e(Parse *);..SQL
73cf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
73d00 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
73d10 49 6e 69 74 28 53 74 72 41 63 63 75 6d 2a 2c 20 Init(StrAccum*,
73d20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 char*, int, int)
73d30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73d40 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 void sqlite3Str
73d50 41 63 63 75 6d 41 70 70 65 6e 64 28 53 74 72 41 AccumAppend(StrA
73d60 63 63 75 6d 2a 2c 63 6f 6e 73 74 20 63 68 61 72 ccum*,const char
73d70 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *,int);.SQLITE_P
73d80 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
73d90 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 ite3StrAccumFini
73da0 73 68 28 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 sh(StrAccum*);.S
73db0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
73dc0 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 id sqlite3StrAcc
73dd0 75 6d 52 65 73 65 74 28 53 74 72 41 63 63 75 6d umReset(StrAccum
73de0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
73df0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
73e00 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 53 65 electDestInit(Se
73e10 6c 65 63 74 44 65 73 74 2a 2c 69 6e 74 2c 69 6e lectDest*,int,in
73e20 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 t);..SQLITE_PRIV
73e30 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
73e40 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 73 71 BackupRestart(sq
73e50 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 3b lite3_backup *);
73e60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73e70 76 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b void sqlite3Back
73e80 75 70 55 70 64 61 74 65 28 73 71 6c 69 74 65 33 upUpdate(sqlite3
73e90 5f 62 61 63 6b 75 70 20 2a 2c 20 50 67 6e 6f 2c _backup *, Pgno,
73ea0 20 63 6f 6e 73 74 20 75 38 20 2a 29 3b 0a 0a 2f const u8 *);../
73eb0 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 *.** The interfa
73ec0 63 65 20 74 6f 20 74 68 65 20 4c 45 4d 4f 4e 2d ce to the LEMON-
73ed0 67 65 6e 65 72 61 74 65 64 20 70 61 72 73 65 72 generated parser
73ee0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
73ef0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
73f00 50 61 72 73 65 72 41 6c 6c 6f 63 28 76 6f 69 64 ParserAlloc(void
73f10 2a 28 2a 29 28 73 69 7a 65 5f 74 29 29 3b 0a 53 *(*)(size_t));.S
73f20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
73f30 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 id sqlite3Parser
73f40 46 72 65 65 28 76 6f 69 64 2a 2c 20 76 6f 69 64 Free(void*, void
73f50 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c (*)(void*));.SQL
73f60 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
73f70 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 28 76 sqlite3Parser(v
73f80 6f 69 64 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65 6e oid*, int, Token
73f90 2c 20 50 61 72 73 65 2a 29 3b 0a 23 69 66 64 65 , Parse*);.#ifde
73fa0 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 f YYTRACKMAXSTAC
73fb0 4b 44 45 50 54 48 0a 53 51 4c 49 54 45 5f 50 52 KDEPTH.SQLITE_PR
73fc0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
73fd0 74 65 33 50 61 72 73 65 72 53 74 61 63 6b 50 65 te3ParserStackPe
73fe0 61 6b 28 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 ak(void*);.#endi
73ff0 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 f..SQLITE_PRIVAT
74000 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 E void sqlite3Au
74010 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 toLoadExtensions
74020 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 69 66 6e (sqlite3*);.#ifn
74030 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
74040 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 LOAD_EXTENSION.S
74050 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
74060 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 void sqlite3Clos
74070 65 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 eExtensions(sqli
74080 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 te3*);.#else.# d
74090 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6c 6f efine sqlite3Clo
740a0 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 58 29 0a seExtensions(X).
740b0 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 #endif..#ifndef
740c0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
740d0 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f ED_CACHE.SQLITE_
740e0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
740f0 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 qlite3TableLock(
74100 50 61 72 73 65 20 2a 2c 20 69 6e 74 2c 20 69 6e Parse *, int, in
74110 74 2c 20 75 38 2c 20 63 6f 6e 73 74 20 63 68 61 t, u8, const cha
74120 72 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 r *);.#else. #d
74130 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 61 62 efine sqlite3Tab
74140 6c 65 4c 6f 63 6b 28 76 2c 77 2c 78 2c 79 2c 7a leLock(v,w,x,y,z
74150 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ).#endif..#ifdef
74160 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
74170 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
74180 74 20 73 71 6c 69 74 65 33 55 74 66 38 54 6f 38 t sqlite3Utf8To8
74190 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 (unsigned char*)
741a0 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ;.#endif..#ifdef
741b0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
741c0 54 55 41 4c 54 41 42 4c 45 0a 23 20 20 64 65 66 TUALTABLE.# def
741d0 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 43 ine sqlite3VtabC
741e0 6c 65 61 72 28 59 29 0a 23 20 20 64 65 66 69 6e lear(Y).# defin
741f0 65 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e e sqlite3VtabSyn
74200 63 28 58 2c 59 29 20 53 51 4c 49 54 45 5f 4f 4b c(X,Y) SQLITE_OK
74210 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 .# define sqlit
74220 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 58 e3VtabRollback(X
74230 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 ).# define sqli
74240 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 58 29 te3VtabCommit(X)
74250 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 .# define sqlit
74260 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 e3VtabInSync(db)
74270 20 30 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 0.# define sql
74280 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 58 29 20 ite3VtabLock(X)
74290 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 .# define sqlit
742a0 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 58 29 0a e3VtabUnlock(X).
742b0 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 # define sqlite
742c0 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 3VtabUnlockList(
742d0 58 29 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f X).#else.SQLITE_
742e0 50 52 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 PRIVATE void
742f0 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 sqlite3VtabClear
74300 28 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 (Table*);.SQLITE
74310 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 _PRIVATE int
74320 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 sqlite3VtabSync(
74330 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 sqlite3 *db, cha
74340 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 r **);.SQLITE_PR
74350 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c IVATE int sql
74360 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b ite3VtabRollback
74370 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 (sqlite3 *db);.S
74380 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
74390 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 int sqlite3Vtab
743a0 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a Commit(sqlite3 *
743b0 64 62 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 db);.SQLITE_PRIV
743c0 41 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 ATE void sqli
743d0 74 65 33 56 74 61 62 4c 6f 63 6b 28 56 54 61 62 te3VtabLock(VTab
743e0 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 le *);.SQLITE_PR
743f0 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 71 IVATE void sq
74400 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 lite3VtabUnlock(
74410 56 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 VTable *);.SQLIT
74420 45 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f 69 E_PRIVATE voi
74430 64 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c d sqlite3VtabUnl
74440 6f 63 6b 4c 69 73 74 28 73 71 6c 69 74 65 33 2a ockList(sqlite3*
74450 29 3b 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c );.# define sql
74460 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 ite3VtabInSync(d
74470 62 29 20 28 28 64 62 29 2d 3e 6e 56 54 72 61 6e b) ((db)->nVTran
74480 73 3e 30 20 26 26 20 28 64 62 29 2d 3e 61 56 54 s>0 && (db)->aVT
74490 72 61 6e 73 3d 3d 30 29 0a 23 65 6e 64 69 66 0a rans==0).#endif.
744a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
744b0 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 4d oid sqlite3VtabM
744c0 61 6b 65 57 72 69 74 61 62 6c 65 28 50 61 72 73 akeWritable(Pars
744d0 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 e*,Table*);.SQLI
744e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
744f0 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e sqlite3VtabBegin
74500 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f Parse(Parse*, To
74510 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f ken*, Token*, To
74520 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
74530 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
74540 65 33 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 e3VtabFinishPars
74550 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a e(Parse*, Token*
74560 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
74570 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 E void sqlite3Vt
74580 61 62 41 72 67 49 6e 69 74 28 50 61 72 73 65 2a abArgInit(Parse*
74590 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
745a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 E void sqlite3Vt
745b0 61 62 41 72 67 45 78 74 65 6e 64 28 50 61 72 73 abArgExtend(Pars
745c0 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c e*, Token*);.SQL
745d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
745e0 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 sqlite3VtabCallC
745f0 72 65 61 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 reate(sqlite3*,
74600 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 int, const char
74610 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c *, char **);.SQL
74620 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
74630 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 sqlite3VtabCallC
74640 6f 6e 6e 65 63 74 28 50 61 72 73 65 2a 2c 20 54 onnect(Parse*, T
74650 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 able*);.SQLITE_P
74660 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
74670 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f e3VtabCallDestro
74680 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c y(sqlite3*, int,
74690 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a const char *);.
746a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
746b0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 nt sqlite3VtabBe
746c0 67 69 6e 28 73 71 6c 69 74 65 33 20 2a 2c 20 56 gin(sqlite3 *, V
746d0 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 Table *);.SQLITE
746e0 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 _PRIVATE FuncDef
746f0 20 2a 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 *sqlite3VtabOve
74700 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 73 71 rloadFunction(sq
74710 6c 69 74 65 33 20 2a 2c 46 75 6e 63 44 65 66 2a lite3 *,FuncDef*
74720 2c 20 69 6e 74 20 6e 41 72 67 2c 20 45 78 70 72 , int nArg, Expr
74730 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
74740 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 TE void sqlite3I
74750 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28 73 nvalidFunction(s
74760 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
74770 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
74780 65 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e**);.SQLITE_PRI
74790 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
747a0 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 TransferBindings
747b0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2c (sqlite3_stmt *,
747c0 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 sqlite3_stmt *)
747d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
747e0 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 70 72 int sqlite3Repr
747f0 65 70 61 72 65 28 56 64 62 65 2a 29 3b 0a 53 51 epare(Vdbe*);.SQ
74800 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
74810 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 d sqlite3ExprLis
74820 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 50 61 72 tCheckLength(Par
74830 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 se*, ExprList*,
74840 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 const char*);.SQ
74850 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c LITE_PRIVATE Col
74860 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 42 69 6e lSeq *sqlite3Bin
74870 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 aryCompareCollSe
74880 71 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 q(Parse *, Expr
74890 2a 2c 20 45 78 70 72 20 2a 29 3b 0a 53 51 4c 49 *, Expr *);.SQLI
748a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
748b0 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f qlite3TempInMemo
748c0 72 79 28 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ry(const sqlite3
748d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
748e0 54 45 20 56 54 61 62 6c 65 20 2a 73 71 6c 69 74 TE VTable *sqlit
748f0 65 33 47 65 74 56 54 61 62 6c 65 28 73 71 6c 69 e3GetVTable(sqli
74900 74 65 33 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 0a te3*, Table*);..
74910 0a 0a 2f 2a 0a 2a 2a 20 41 76 61 69 6c 61 62 6c ../*.** Availabl
74920 65 20 66 61 75 6c 74 20 69 6e 6a 65 63 74 6f 72 e fault injector
74930 73 2e 20 20 53 68 6f 75 6c 64 20 62 65 20 6e 75 s. Should be nu
74940 6d 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67 mbered beginning
74950 20 77 69 74 68 20 30 2e 0a 2a 2f 0a 23 64 65 66 with 0..*/.#def
74960 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c 54 ine SQLITE_FAULT
74970 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43 20 INJECTOR_MALLOC
74980 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 0.#define SQ
74990 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54 LITE_FAULTINJECT
749a0 4f 52 5f 43 4f 55 4e 54 20 20 20 20 20 20 31 0a OR_COUNT 1.
749b0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 ./*.** The inter
749c0 66 61 63 65 20 74 6f 20 74 68 65 20 63 6f 64 65 face to the code
749d0 20 69 6e 20 66 61 75 6c 74 2e 63 20 75 73 65 64 in fault.c used
749e0 20 66 6f 72 20 69 64 65 6e 74 69 66 79 69 6e 67 for identifying
749f0 20 22 62 65 6e 69 67 6e 22 0a 2a 2a 20 6d 61 6c "benign".** mal
74a00 6c 6f 63 20 66 61 69 6c 75 72 65 73 2e 20 54 68 loc failures. Th
74a10 69 73 20 69 73 20 6f 6e 6c 79 20 70 72 65 73 65 is is only prese
74a20 6e 74 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 nt if SQLITE_OMI
74a30 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2a T_BUILTIN_TEST.*
74a40 2a 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 * is not defined
74a50 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
74a60 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e ITE_OMIT_BUILTIN
74a70 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 _TEST.SQLITE_PRI
74a80 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
74a90 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 te3BeginBenignMa
74aa0 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 53 51 4c 49 lloc(void);.SQLI
74ab0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
74ac0 64 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 d sqlite3EndBeni
74ad0 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a gnMalloc(void);.
74ae0 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 #else. #define
74af0 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 sqlite3BeginBeni
74b00 67 6e 4d 61 6c 6c 6f 63 28 29 0a 20 20 23 64 65 gnMalloc(). #de
74b10 66 69 6e 65 20 73 71 6c 69 74 65 33 45 6e 64 42 fine sqlite3EndB
74b20 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 23 65 enignMalloc().#e
74b30 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 49 4e ndif..#define IN
74b40 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 20 20 20 _INDEX_ROWID
74b50 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 1.#define
74b60 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 20 20 IN_INDEX_EPH
74b70 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 2.#def
74b80 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 ine IN_INDEX_IND
74b90 45 58 20 20 20 20 20 20 20 20 20 20 20 33 0a 53 EX 3.S
74ba0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
74bb0 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 t sqlite3FindInI
74bc0 6e 64 65 78 28 50 61 72 73 65 20 2a 2c 20 45 78 ndex(Parse *, Ex
74bd0 70 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 0a 23 69 pr *, int*);..#i
74be0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
74bf0 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a LE_ATOMIC_WRITE.
74c00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
74c10 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 int sqlite3Jour
74c20 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f nalOpen(sqlite3_
74c30 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 vfs *, const cha
74c40 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c r *, sqlite3_fil
74c50 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a e *, int, int);.
74c60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
74c70 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 int sqlite3Jour
74c80 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 5f nalSize(sqlite3_
74c90 76 66 73 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 vfs *);.SQLITE_P
74ca0 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c RIVATE int sql
74cb0 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 ite3JournalCreat
74cc0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
74cd0 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 );.#else. #defi
74ce0 6e 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 ne sqlite3Journa
74cf0 6c 53 69 7a 65 28 70 56 66 73 29 20 28 28 70 56 lSize(pVfs) ((pV
74d00 66 73 29 2d 3e 73 7a 4f 73 46 69 6c 65 29 0a 23 fs)->szOsFile).#
74d10 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
74d20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
74d30 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e e3MemJournalOpen
74d40 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 (sqlite3_file *)
74d50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74d60 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 4a int sqlite3MemJ
74d70 6f 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 29 ournalSize(void)
74d80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74d90 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d 65 int sqlite3IsMe
74da0 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 33 mJournal(sqlite3
74db0 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 69 66 20 53 _file *);..#if S
74dc0 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 QLITE_MAX_EXPR_D
74dd0 45 50 54 48 3e 30 0a 53 51 4c 49 54 45 5f 50 52 EPTH>0.SQLITE_PR
74de0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
74df0 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 ite3ExprSetHeigh
74e00 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c t(Parse *pParse,
74e10 20 45 78 70 72 20 2a 70 29 3b 0a 53 51 4c 49 54 Expr *p);.SQLIT
74e20 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
74e30 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 sqlite3SelectExp
74e40 72 48 65 69 67 68 74 28 53 65 6c 65 63 74 20 2a rHeight(Select *
74e50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
74e60 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 45 E int sqlite3E
74e70 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 50 xprCheckHeight(P
74e80 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c arse*, int);.#el
74e90 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c se. #define sql
74ea0 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 ite3ExprSetHeigh
74eb0 74 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e 65 t(x,y). #define
74ec0 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 sqlite3SelectEx
74ed0 70 72 48 65 69 67 68 74 28 78 29 20 30 0a 20 20 prHeight(x) 0.
74ee0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 #define sqlite3E
74ef0 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 78 xprCheckHeight(x
74f00 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 ,y).#endif..SQLI
74f10 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 TE_PRIVATE u32 s
74f20 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 63 qlite3Get4byte(c
74f30 6f 6e 73 74 20 75 38 2a 29 3b 0a 53 51 4c 49 54 onst u8*);.SQLIT
74f40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
74f50 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 75 qlite3Put4byte(u
74f60 38 2a 2c 20 75 33 32 29 3b 0a 0a 23 69 66 64 65 8*, u32);..#ifde
74f70 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
74f80 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 53 51 UNLOCK_NOTIFY.SQ
74f90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
74fa0 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 oid sqlite3Conne
74fb0 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 73 71 6c ctionBlocked(sql
74fc0 69 74 65 33 20 2a 2c 20 73 71 6c 69 74 65 33 20 ite3 *, sqlite3
74fd0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
74fe0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
74ff0 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 3ConnectionUnloc
75000 6b 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29 ked(sqlite3 *db)
75010 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75020 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 void sqlite3C
75030 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 onnectionClosed(
75040 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23 65 sqlite3 *db);.#e
75050 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 lse. #define sq
75060 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 lite3ConnectionB
75070 6c 6f 63 6b 65 64 28 78 2c 79 29 0a 20 20 23 64 locked(x,y). #d
75080 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 6e efine sqlite3Con
75090 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 nectionUnlocked(
750a0 78 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c x). #define sql
750b0 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c ite3ConnectionCl
750c0 6f 73 65 64 28 78 29 0a 23 65 6e 64 69 66 0a 0a osed(x).#endif..
750d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
750e0 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
750f0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
75100 33 50 61 72 73 65 72 54 72 61 63 65 28 46 49 4c 3ParserTrace(FIL
75110 45 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e E*, char *);.#en
75120 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 dif../*.** If th
75130 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 20 e SQLITE_ENABLE
75140 49 4f 54 52 41 43 45 20 65 78 69 73 74 73 20 74 IOTRACE exists t
75150 68 65 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 76 hen the global v
75160 61 72 69 61 62 6c 65 0a 2a 2a 20 73 71 6c 69 74 ariable.** sqlit
75170 65 33 49 6f 54 72 61 63 65 20 69 73 20 61 20 70 e3IoTrace is a p
75180 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 72 69 6e ointer to a prin
75190 74 66 2d 6c 69 6b 65 20 72 6f 75 74 69 6e 65 20 tf-like routine
751a0 75 73 65 64 20 74 6f 0a 2a 2a 20 70 72 69 6e 74 used to.** print
751b0 20 49 2f 4f 20 74 72 61 63 69 6e 67 20 6d 65 73 I/O tracing mes
751c0 73 61 67 65 73 2e 20 0a 2a 2f 0a 23 69 66 64 65 sages. .*/.#ifde
751d0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
751e0 49 4f 54 52 41 43 45 0a 23 20 64 65 66 69 6e 65 IOTRACE.# define
751f0 20 49 4f 54 52 41 43 45 28 41 29 20 20 69 66 28 IOTRACE(A) if(
75200 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20 sqlite3IoTrace
75210 29 7b 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 ){ sqlite3IoTrac
75220 65 20 41 3b 20 7d 0a 53 51 4c 49 54 45 5f 50 52 e A; }.SQLITE_PR
75230 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
75240 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 ite3VdbeIOTraceS
75250 71 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 ql(Vdbe*);.SQLIT
75260 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 E_PRIVATE void (
75270 2a 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 29 *sqlite3IoTrace)
75280 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e (const char*,...
75290 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e );.#else.# defin
752a0 65 20 49 4f 54 52 41 43 45 28 41 29 0a 23 20 64 e IOTRACE(A).# d
752b0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 efine sqlite3Vdb
752c0 65 49 4f 54 72 61 63 65 53 71 6c 28 58 29 0a 23 eIOTraceSql(X).#
752d0 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f endif..#endif../
752e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
752f0 6e 64 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e nd of sqliteInt.
75300 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
75310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
75320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
75330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
75340 65 67 69 6e 20 66 69 6c 65 20 67 6c 6f 62 61 6c egin file global
75350 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
75360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
75370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
75380 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 *.** 2008 June 1
75390 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 3.**.** The auth
753a0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
753b0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
753c0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
753d0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
753e0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
753f0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
75400 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
75410 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
75420 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
75430 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
75440 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
75450 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
75460 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
75470 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
75480 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
75490 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
754a0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
754b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
754c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
754d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
754e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
754f0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
75500 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 contains defini
75510 74 69 6f 6e 73 20 6f 66 20 67 6c 6f 62 61 6c 20 tions of global
75520 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 63 6f variables and co
75530 6e 74 61 6e 74 73 2e 0a 2a 2f 0a 0a 0a 2f 2a 20 ntants..*/.../*
75540 41 6e 20 61 72 72 61 79 20 74 6f 20 6d 61 70 20 An array to map
75550 61 6c 6c 20 75 70 70 65 72 2d 63 61 73 65 20 63 all upper-case c
75560 68 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20 74 haracters into t
75570 68 65 69 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 heir correspondi
75580 6e 67 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61 73 65 ng.** lower-case
75590 20 63 68 61 72 61 63 74 65 72 2e 20 0a 2a 2a 0a character. .**.
755a0 2a 2a 20 53 51 4c 69 74 65 20 6f 6e 6c 79 20 63 ** SQLite only c
755b0 6f 6e 73 69 64 65 72 73 20 55 53 2d 41 53 43 49 onsiders US-ASCI
755c0 49 20 28 6f 72 20 45 42 43 44 49 43 29 20 63 68 I (or EBCDIC) ch
755d0 61 72 61 63 74 65 72 73 2e 20 20 57 65 20 64 6f aracters. We do
755e0 20 6e 6f 74 0a 2a 2a 20 68 61 6e 64 6c 65 20 63 not.** handle c
755f0 61 73 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 ase conversions
75600 66 6f 72 20 74 68 65 20 55 54 46 20 63 68 61 72 for the UTF char
75610 61 63 74 65 72 20 73 65 74 20 73 69 6e 63 65 20 acter set since
75620 74 68 65 20 74 61 62 6c 65 73 0a 2a 2a 20 69 6e the tables.** in
75630 76 6f 6c 76 65 64 20 61 72 65 20 6e 65 61 72 6c volved are nearl
75640 79 20 61 73 20 62 69 67 20 6f 72 20 62 69 67 67 y as big or bigg
75650 65 72 20 74 68 61 6e 20 53 51 4c 69 74 65 20 69 er than SQLite i
75660 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 tself..*/.SQLITE
75670 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 _PRIVATE const u
75680 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c nsigned char sql
75690 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 ite3UpperToLower
756a0 5b 5d 20 3d 20 7b 0a 23 69 66 64 65 66 20 53 51 [] = {.#ifdef SQ
756b0 4c 49 54 45 5f 41 53 43 49 49 0a 20 20 20 20 20 LITE_ASCII.
756c0 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c 20 0, 1, 2, 3,
756d0 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c 20 4, 5, 6, 7,
756e0 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c 20 8, 9, 10, 11,
756f0 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c 20 12, 13, 14, 15,
75700 31 36 2c 20 31 37 2c 0a 20 20 20 20 20 31 38 2c 16, 17,. 18,
75710 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c 19, 20, 21, 22,
75720 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c 23, 24, 25, 26,
75730 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c 27, 28, 29, 30,
75740 20 33 31 2c 20 33 32 2c 20 33 33 2c 20 33 34 2c 31, 32, 33, 34,
75750 20 33 35 2c 0a 20 20 20 20 20 33 36 2c 20 33 37 35,. 36, 37
75760 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 20 34 31 , 38, 39, 40, 41
75770 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 20 34 35 , 42, 43, 44, 45
75780 2c 20 34 36 2c 20 34 37 2c 20 34 38 2c 20 34 39 , 46, 47, 48, 49
75790 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35 33 , 50, 51, 52, 53
757a0 2c 0a 20 20 20 20 20 35 34 2c 20 35 35 2c 20 35 ,. 54, 55, 5
757b0 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 20 36 6, 57, 58, 59, 6
757c0 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 20 36 0, 61, 62, 63, 6
757d0 34 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 4, 97, 98, 99,10
757e0 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 0a 20 0,101,102,103,.
757f0 20 20 20 31 30 34 2c 31 30 35 2c 31 30 36 2c 31 104,105,106,1
75800 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 07,108,109,110,1
75810 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 31 11,112,113,114,1
75820 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 15,116,117,118,1
75830 31 39 2c 31 32 30 2c 31 32 31 2c 0a 20 20 20 20 19,120,121,.
75840 31 32 32 2c 20 39 31 2c 20 39 32 2c 20 39 33 2c 122, 91, 92, 93,
75850 20 39 34 2c 20 39 35 2c 20 39 36 2c 20 39 37 2c 94, 95, 96, 97,
75860 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 98, 99,100,101,
75870 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c 102,103,104,105,
75880 31 30 36 2c 31 30 37 2c 0a 20 20 20 20 31 30 38 106,107,. 108
75890 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32 ,109,110,111,112
758a0 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 31 36 ,113,114,115,116
758b0 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 ,117,118,119,120
758c0 2c 31 32 31 2c 31 32 32 2c 31 32 33 2c 31 32 34 ,121,122,123,124
758d0 2c 31 32 35 2c 0a 20 20 20 20 31 32 36 2c 31 32 ,125,. 126,12
758e0 37 2c 31 32 38 2c 31 32 39 2c 31 33 30 2c 31 33 7,128,129,130,13
758f0 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 1,132,133,134,13
75900 35 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 31 33 5,136,137,138,13
75910 39 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 34 9,140,141,142,14
75920 33 2c 0a 20 20 20 20 31 34 34 2c 31 34 35 2c 31 3,. 144,145,1
75930 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 46,147,148,149,1
75940 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 31 50,151,152,153,1
75950 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c 31 54,155,156,157,1
75960 35 38 2c 31 35 39 2c 31 36 30 2c 31 36 31 2c 0a 58,159,160,161,.
75970 20 20 20 20 31 36 32 2c 31 36 33 2c 31 36 34 2c 162,163,164,
75980 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 165,166,167,168,
75990 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 37 32 2c 169,170,171,172,
759a0 31 37 33 2c 31 37 34 2c 31 37 35 2c 31 37 36 2c 173,174,175,176,
759b0 31 37 37 2c 31 37 38 2c 31 37 39 2c 0a 20 20 20 177,178,179,.
759c0 20 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 33 180,181,182,183
759d0 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 37 ,184,185,186,187
759e0 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 31 ,188,189,190,191
759f0 2c 31 39 32 2c 31 39 33 2c 31 39 34 2c 31 39 35 ,192,193,194,195
75a00 2c 31 39 36 2c 31 39 37 2c 0a 20 20 20 20 31 39 ,196,197,. 19
75a10 38 2c 31 39 39 2c 32 30 30 2c 32 30 31 2c 32 30 8,199,200,201,20
75a20 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 2,203,204,205,20
75a30 36 2c 32 30 37 2c 32 30 38 2c 32 30 39 2c 32 31 6,207,208,209,21
75a40 30 2c 32 31 31 2c 32 31 32 2c 32 31 33 2c 32 31 0,211,212,213,21
75a50 34 2c 32 31 35 2c 0a 20 20 20 20 32 31 36 2c 32 4,215,. 216,2
75a60 31 37 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 32 17,218,219,220,2
75a70 32 31 2c 32 32 32 2c 32 32 33 2c 32 32 34 2c 32 21,222,223,224,2
75a80 32 35 2c 32 32 36 2c 32 32 37 2c 32 32 38 2c 32 25,226,227,228,2
75a90 32 39 2c 32 33 30 2c 32 33 31 2c 32 33 32 2c 32 29,230,231,232,2
75aa0 33 33 2c 0a 20 20 20 20 32 33 34 2c 32 33 35 2c 33,. 234,235,
75ab0 32 33 36 2c 32 33 37 2c 32 33 38 2c 32 33 39 2c 236,237,238,239,
75ac0 32 34 30 2c 32 34 31 2c 32 34 32 2c 32 34 33 2c 240,241,242,243,
75ad0 32 34 34 2c 32 34 35 2c 32 34 36 2c 32 34 37 2c 244,245,246,247,
75ae0 32 34 38 2c 32 34 39 2c 32 35 30 2c 32 35 31 2c 248,249,250,251,
75af0 0a 20 20 20 20 32 35 32 2c 32 35 33 2c 32 35 34 . 252,253,254
75b00 2c 32 35 35 0a 23 65 6e 64 69 66 0a 23 69 66 64 ,255.#endif.#ifd
75b10 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 ef SQLITE_EBCDIC
75b20 0a 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 32 . 0, 1, 2
75b30 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 20 20 36 , 3, 4, 5, 6
75b40 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 20 31 30 , 7, 8, 9, 10
75b50 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 20 31 34 , 11, 12, 13, 14
75b60 2c 20 31 35 2c 20 2f 2a 20 30 78 20 2a 2f 0a 20 , 15, /* 0x */.
75b70 20 20 20 20 31 36 2c 20 31 37 2c 20 31 38 2c 20 16, 17, 18,
75b80 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c 20 19, 20, 21, 22,
75b90 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c 20 23, 24, 25, 26,
75ba0 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c 20 27, 28, 29, 30,
75bb0 33 31 2c 20 2f 2a 20 31 78 20 2a 2f 0a 20 20 20 31, /* 1x */.
75bc0 20 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33 35 32, 33, 34, 35
75bd0 2c 20 33 36 2c 20 33 37 2c 20 33 38 2c 20 33 39 , 36, 37, 38, 39
75be0 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 20 34 33 , 40, 41, 42, 43
75bf0 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 20 34 37 , 44, 45, 46, 47
75c00 2c 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20 20 , /* 2x */.
75c10 34 38 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c 20 48, 49, 50, 51,
75c20 35 32 2c 20 35 33 2c 20 35 34 2c 20 35 35 2c 20 52, 53, 54, 55,
75c30 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 20 56, 57, 58, 59,
75c40 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 20 60, 61, 62, 63,
75c50 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20 20 36 34 /* 3x */. 64
75c60 2c 20 36 35 2c 20 36 36 2c 20 36 37 2c 20 36 38 , 65, 66, 67, 68
75c70 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 20 37 32 , 69, 70, 71, 72
75c80 2c 20 37 33 2c 20 37 34 2c 20 37 35 2c 20 37 36 , 73, 74, 75, 76
75c90 2c 20 37 37 2c 20 37 38 2c 20 37 39 2c 20 2f 2a , 77, 78, 79, /*
75ca0 20 34 78 20 2a 2f 0a 20 20 20 20 20 38 30 2c 20 4x */. 80,
75cb0 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c 20 81, 82, 83, 84,
75cc0 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c 20 85, 86, 87, 88,
75cd0 38 39 2c 20 39 30 2c 20 39 31 2c 20 39 32 2c 20 89, 90, 91, 92,
75ce0 39 33 2c 20 39 34 2c 20 39 35 2c 20 2f 2a 20 35 93, 94, 95, /* 5
75cf0 78 20 2a 2f 0a 20 20 20 20 20 39 36 2c 20 39 37 x */. 96, 97
75d00 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 39 , 66, 67, 68, 69
75d10 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 33 , 70, 71, 72, 73
75d20 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 ,106,107,108,109
75d30 2c 31 31 30 2c 31 31 31 2c 20 2f 2a 20 36 78 20 ,110,111, /* 6x
75d40 2a 2f 0a 20 20 20 20 31 31 32 2c 20 38 31 2c 20 */. 112, 81,
75d50 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 20 82, 83, 84, 85,
75d60 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 31 86, 87, 88, 89,1
75d70 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c 31 22,123,124,125,1
75d80 32 36 2c 31 32 37 2c 20 2f 2a 20 37 78 20 2a 2f 26,127, /* 7x */
75d90 0a 20 20 20 20 31 32 38 2c 31 32 39 2c 31 33 30 . 128,129,130
75da0 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 ,131,132,133,134
75db0 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 38 ,135,136,137,138
75dc0 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 32 ,139,140,141,142
75dd0 2c 31 34 33 2c 20 2f 2a 20 38 78 20 2a 2f 0a 20 ,143, /* 8x */.
75de0 20 20 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 31 144,145,146,1
75df0 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 47,148,149,150,1
75e00 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 31 51,152,153,154,1
75e10 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 36 2c 31 55,156,157,156,1
75e20 35 39 2c 20 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 59, /* 9x */.
75e30 20 31 36 30 2c 31 36 31 2c 31 36 32 2c 31 36 33 160,161,162,163
75e40 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 ,164,165,166,167
75e50 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c 31 37 31 ,168,169,170,171
75e60 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 37 35 ,140,141,142,175
75e70 2c 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 20 31 , /* Ax */. 1
75e80 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 31 76,177,178,179,1
75e90 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c 31 80,181,182,183,1
75ea0 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c 31 84,185,186,187,1
75eb0 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c 20 88,189,190,191,
75ec0 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 20 31 39 32 /* Bx */. 192
75ed0 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 ,129,130,131,132
75ee0 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 ,133,134,135,136
75ef0 2c 31 33 37 2c 32 30 32 2c 32 30 33 2c 32 30 34 ,137,202,203,204
75f00 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a ,205,206,207, /*
75f10 20 43 78 20 2a 2f 0a 20 20 20 20 32 30 38 2c 31 Cx */. 208,1
75f20 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 45,146,147,148,1
75f30 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 49,150,151,152,1
75f40 35 33 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 32 53,218,219,220,2
75f50 32 31 2c 32 32 32 2c 32 32 33 2c 20 2f 2a 20 44 21,222,223, /* D
75f60 78 20 2a 2f 0a 20 20 20 20 32 32 34 2c 32 32 35 x */. 224,225
75f70 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 ,162,163,164,165
75f80 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 ,166,167,168,169
75f90 2c 32 33 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 ,232,203,204,205
75fa0 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 45 78 20 ,206,207, /* Ex
75fb0 2a 2f 0a 20 20 20 20 32 33 39 2c 32 34 30 2c 32 */. 239,240,2
75fc0 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 32 41,242,243,244,2
75fd0 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 32 45,246,247,248,2
75fe0 34 39 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 49,219,220,221,2
75ff0 32 32 2c 32 35 35 2c 20 2f 2a 20 46 78 20 2a 2f 22,255, /* Fx */
76000 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a .#endif.};../*.*
76010 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
76020 32 35 36 20 62 79 74 65 20 6c 6f 6f 6b 75 70 20 256 byte lookup
76030 74 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f table is used to
76040 20 73 75 70 70 6f 72 74 20 53 51 4c 69 74 65 73 support SQLites
76050 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 65 71 75 built-in.** equ
76060 69 76 61 6c 65 6e 74 73 20 74 6f 20 74 68 65 20 ivalents to the
76070 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 6e 64 61 following standa
76080 72 64 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 rd library funct
76090 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 73 ions:.**.** is
760a0 73 70 61 63 65 28 29 20 20 20 20 20 20 20 20 20 space()
760b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
760c0 78 30 31 0a 2a 2a 20 20 20 69 73 61 6c 70 68 61 x01.** isalpha
760d0 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ()
760e0 20 20 20 20 20 20 20 20 20 20 30 78 30 32 0a 2a 0x02.*
760f0 2a 20 20 20 69 73 64 69 67 69 74 28 29 20 20 20 * isdigit()
76100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76110 20 20 20 20 20 30 78 30 34 0a 2a 2a 20 20 20 69 0x04.** i
76120 73 61 6c 6e 75 6d 28 29 20 20 20 20 20 20 20 20 salnum()
76130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76140 30 78 30 36 0a 2a 2a 20 20 20 69 73 78 64 69 67 0x06.** isxdig
76150 69 74 28 29 20 20 20 20 20 20 20 20 20 20 20 20 it()
76160 20 20 20 20 20 20 20 20 20 20 20 30 78 30 38 0a 0x08.
76170 2a 2a 20 20 20 74 6f 75 70 70 65 72 28 29 20 20 ** toupper()
76180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76190 20 20 20 20 20 20 30 78 32 30 0a 2a 2a 0a 2a 2a 0x20.**.**
761a0 20 42 69 74 20 30 78 32 30 20 69 73 20 73 65 74 Bit 0x20 is set
761b0 20 69 66 20 74 68 65 20 6d 61 70 70 65 64 20 63 if the mapped c
761c0 68 61 72 61 63 74 65 72 20 72 65 71 75 69 72 65 haracter require
761d0 73 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74 6f s translation to
761e0 20 75 70 70 65 72 0a 2a 2a 20 63 61 73 65 2e 20 upper.** case.
761f0 69 2e 65 2e 20 69 66 20 74 68 65 20 63 68 61 72 i.e. if the char
76200 61 63 74 65 72 20 69 73 20 61 20 6c 6f 77 65 72 acter is a lower
76210 2d 63 61 73 65 20 41 53 43 49 49 20 63 68 61 72 -case ASCII char
76220 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 78 20 69 acter..** If x i
76230 73 20 61 20 6c 6f 77 65 72 2d 63 61 73 65 20 41 s a lower-case A
76240 53 43 49 49 20 63 68 61 72 61 63 74 65 72 2c 20 SCII character,
76250 74 68 65 6e 20 69 74 73 20 75 70 70 65 72 2d 63 then its upper-c
76260 61 73 65 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a ase equivalent.*
76270 2a 20 69 73 20 28 78 20 2d 20 30 78 32 30 29 2e * is (x - 0x20).
76280 20 54 68 65 72 65 66 6f 72 65 20 74 6f 75 70 70 Therefore toupp
76290 65 72 28 29 20 63 61 6e 20 62 65 20 69 6d 70 6c er() can be impl
762a0 65 6d 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a 2a emented as:.**.*
762b0 2a 20 20 20 28 78 20 26 20 7e 28 6d 61 70 5b 78 * (x & ~(map[x
762c0 5d 26 30 78 32 30 29 29 0a 2a 2a 0a 2a 2a 20 53 ]&0x20)).**.** S
762d0 74 61 6e 64 61 72 64 20 66 75 6e 63 74 69 6f 6e tandard function
762e0 20 74 6f 6c 6f 77 65 72 28 29 20 69 73 20 69 6d tolower() is im
762f0 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 plemented using
76300 74 68 65 20 73 71 6c 69 74 65 33 55 70 70 65 72 the sqlite3Upper
76310 54 6f 4c 6f 77 65 72 5b 5d 0a 2a 2a 20 61 72 72 ToLower[].** arr
76320 61 79 2e 20 74 6f 6c 6f 77 65 72 28 29 20 69 73 ay. tolower() is
76330 20 75 73 65 64 20 6d 6f 72 65 20 6f 66 74 65 6e used more often
76340 20 74 68 61 6e 20 74 6f 75 70 70 65 72 28 29 20 than toupper()
76350 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a by SQLite..**.**
76360 20 53 51 4c 69 74 65 27 73 20 76 65 72 73 69 6f SQLite's versio
76370 6e 73 20 61 72 65 20 69 64 65 6e 74 69 63 61 6c ns are identical
76380 20 74 6f 20 74 68 65 20 73 74 61 6e 64 61 72 64 to the standard
76390 20 76 65 72 73 69 6f 6e 73 20 61 73 73 75 6d 69 versions assumi
763a0 6e 67 20 61 0a 2a 2a 20 6c 6f 63 61 6c 65 20 6f ng a.** locale o
763b0 66 20 22 43 22 2e 20 54 68 65 79 20 61 72 65 20 f "C". They are
763c0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6d implemented as m
763d0 61 63 72 6f 73 20 69 6e 20 73 71 6c 69 74 65 49 acros in sqliteI
763e0 6e 74 2e 68 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 nt.h..*/.#ifdef
763f0 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53 51 4c SQLITE_ASCII.SQL
76400 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
76410 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
76420 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b sqlite3CtypeMap[
76430 32 35 36 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c 256] = {. 0x00,
76440 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76450 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
76460 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 30 x00, 0x00, /* 0
76470 30 2e 2e 30 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 0..07 .......
76480 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 . */. 0x00, 0x0
76490 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 1, 0x01, 0x01, 0
764a0 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 30 2c x01, 0x01, 0x00,
764b0 20 30 78 30 30 2c 20 20 2f 2a 20 30 38 2e 2e 30 0x00, /* 08..0
764c0 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f f ........ */
764d0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 . 0x00, 0x00, 0
764e0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
764f0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76500 30 2c 20 20 2f 2a 20 31 30 2e 2e 31 37 20 20 20 0, /* 10..17
76510 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 ........ */. 0
76520 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
76530 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76540 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 0, 0x00, 0x00,
76550 2f 2a 20 31 38 2e 2e 31 66 20 20 20 20 2e 2e 2e /* 18..1f ...
76560 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 31 2c ..... */. 0x01,
76570 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76580 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
76590 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32 x00, 0x00, /* 2
765a0 30 2e 2e 32 37 20 20 20 20 20 21 22 23 24 25 26 0..27 !"#$%&
765b0 27 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 ' */. 0x00, 0x0
765c0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
765d0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
765e0 20 30 78 30 30 2c 20 20 2f 2a 20 32 38 2e 2e 32 0x00, /* 28..2
765f0 66 20 20 20 20 28 29 2a 2b 2c 2d 2e 2f 20 2a 2f f ()*+,-./ */
76600 0a 20 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 . 0x0c, 0x0c, 0
76610 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c x0c, 0x0c, 0x0c,
76620 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 0x0c, 0x0c, 0x0
76630 63 2c 20 20 2f 2a 20 33 30 2e 2e 33 37 20 20 20 c, /* 30..37
76640 20 30 31 32 33 34 35 36 37 20 2a 2f 0a 20 20 30 01234567 */. 0
76650 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 30 2c x0c, 0x0c, 0x00,
76660 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76670 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 0, 0x00, 0x00,
76680 2f 2a 20 33 38 2e 2e 33 66 20 20 20 20 38 39 3a /* 38..3f 89:
76690 3b 3c 3d 3e 3f 20 2a 2f 0a 0a 20 20 30 78 30 30 ;<=>? */.. 0x00
766a0 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 , 0x0a, 0x0a, 0x
766b0 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 0a, 0x0a, 0x0a,
766c0 30 78 30 61 2c 20 30 78 30 32 2c 20 20 2f 2a 20 0x0a, 0x02, /*
766d0 34 30 2e 2e 34 37 20 20 20 20 40 41 42 43 44 45 40..47 @ABCDE
766e0 46 47 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 FG */. 0x02, 0x
766f0 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 02, 0x02, 0x02,
76700 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 0x02, 0x02, 0x02
76710 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34 38 2e 2e , 0x02, /* 48..
76720 34 66 20 20 20 20 48 49 4a 4b 4c 4d 4e 4f 20 2a 4f HIJKLMNO *
76730 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32 2c 20 /. 0x02, 0x02,
76740 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 0x02, 0x02, 0x02
76750 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 , 0x02, 0x02, 0x
76760 30 32 2c 20 20 2f 2a 20 35 30 2e 2e 35 37 20 20 02, /* 50..57
76770 20 20 50 51 52 53 54 55 56 57 20 2a 2f 0a 20 20 PQRSTUVW */.
76780 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 0x02, 0x02, 0x02
76790 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
767a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
767b0 20 2f 2a 20 35 38 2e 2e 35 66 20 20 20 20 58 59 /* 58..5f XY
767c0 5a 5b 5c 5d 5e 5f 20 2a 2f 0a 20 20 30 78 30 30 Z[\]^_ */. 0x00
767d0 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 , 0x2a, 0x2a, 0x
767e0 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 2a, 0x2a, 0x2a,
767f0 30 78 32 61 2c 20 30 78 32 32 2c 20 20 2f 2a 20 0x2a, 0x22, /*
76800 36 30 2e 2e 36 37 20 20 20 20 60 61 62 63 64 65 60..67 `abcde
76810 66 67 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 fg */. 0x22, 0x
76820 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 22, 0x22, 0x22,
76830 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 0x22, 0x22, 0x22
76840 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36 38 2e 2e , 0x22, /* 68..
76850 36 66 20 20 20 20 68 69 6a 6b 6c 6d 6e 6f 20 2a 6f hijklmno *
76860 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32 2c 20 /. 0x22, 0x22,
76870 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 0x22, 0x22, 0x22
76880 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 , 0x22, 0x22, 0x
76890 32 32 2c 20 20 2f 2a 20 37 30 2e 2e 37 37 20 20 22, /* 70..77
768a0 20 20 70 71 72 73 74 75 76 77 20 2a 2f 0a 20 20 pqrstuvw */.
768b0 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 0x22, 0x22, 0x22
768c0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
768d0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
768e0 20 2f 2a 20 37 38 2e 2e 37 66 20 20 20 20 78 79 /* 78..7f xy
768f0 7a 7b 7c 7d 7e 2e 20 2a 2f 0a 0a 20 20 30 78 30 z{|}~. */.. 0x0
76900 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
76910 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
76920 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 0x00, 0x00, /*
76930 20 38 30 2e 2e 38 37 20 20 20 20 2e 2e 2e 2e 2e 80..87 .....
76940 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 ... */. 0x00, 0
76950 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
76960 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76970 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 38 38 2e 0, 0x00, /* 88.
76980 2e 38 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 .8f ........
76990 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c */. 0x00, 0x00,
769a0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
769b0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
769c0 78 30 30 2c 20 20 2f 2a 20 39 30 2e 2e 39 37 20 x00, /* 90..97
769d0 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 ........ */.
769e0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
769f0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
76a00 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
76a10 20 20 2f 2a 20 39 38 2e 2e 39 66 20 20 20 20 2e /* 98..9f .
76a20 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 ....... */. 0x0
76a30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
76a40 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
76a50 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 0x00, 0x00, /*
76a60 20 61 30 2e 2e 61 37 20 20 20 20 2e 2e 2e 2e 2e a0..a7 .....
76a70 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 ... */. 0x00, 0
76a80 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
76a90 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76aa0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 61 38 2e 0, 0x00, /* a8.
76ab0 2e 61 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 .af ........
76ac0 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c */. 0x00, 0x00,
76ad0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76ae0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
76af0 78 30 30 2c 20 20 2f 2a 20 62 30 2e 2e 62 37 20 x00, /* b0..b7
76b00 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 ........ */.
76b10 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76b20 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
76b30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
76b40 20 20 2f 2a 20 62 38 2e 2e 62 66 20 20 20 20 2e /* b8..bf .
76b50 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 0a 20 20 30 78 ....... */.. 0x
76b60 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
76b70 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76b80 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f , 0x00, 0x00, /
76b90 2a 20 63 30 2e 2e 63 37 20 20 20 20 2e 2e 2e 2e * c0..c7 ....
76ba0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 .... */. 0x00,
76bb0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76bc0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
76bd0 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 63 38 00, 0x00, /* c8
76be0 2e 2e 63 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e ..cf ........
76bf0 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 */. 0x00, 0x00
76c00 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
76c10 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
76c20 30 78 30 30 2c 20 20 2f 2a 20 64 30 2e 2e 64 37 0x00, /* d0..d7
76c30 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a ........ */.
76c40 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 0x00, 0x00, 0x
76c50 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
76c60 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76c70 2c 20 20 2f 2a 20 64 38 2e 2e 64 66 20 20 20 20 , /* d8..df
76c80 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 ........ */. 0x
76c90 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
76ca0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76cb0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f , 0x00, 0x00, /
76cc0 2a 20 65 30 2e 2e 65 37 20 20 20 20 2e 2e 2e 2e * e0..e7 ....
76cd0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 .... */. 0x00,
76ce0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76cf0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
76d00 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 65 38 00, 0x00, /* e8
76d10 2e 2e 65 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e ..ef ........
76d20 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 */. 0x00, 0x00
76d30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
76d40 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
76d50 30 78 30 30 2c 20 20 2f 2a 20 66 30 2e 2e 66 37 0x00, /* f0..f7
76d60 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a ........ */.
76d70 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 0x00, 0x00, 0x
76d80 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
76d90 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76da0 20 20 20 2f 2a 20 66 38 2e 2e 66 66 20 20 20 20 /* f8..ff
76db0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 7d 3b 0a 23 ........ */.};.#
76dc0 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 endif..../*.** T
76dd0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 69 6e he following sin
76de0 67 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 gleton contains
76df0 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 the global confi
76e00 67 75 72 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 guration for.**
76e10 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 the SQLite libra
76e20 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ry..*/.SQLITE_PR
76e30 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 IVATE SQLITE_WSD
76e40 20 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 struct Sqlite3C
76e50 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e onfig sqlite3Con
76e60 66 69 67 20 3d 20 7b 0a 20 20 20 53 51 4c 49 54 fig = {. SQLIT
76e70 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 E_DEFAULT_MEMSTA
76e80 54 55 53 2c 20 20 2f 2a 20 62 4d 65 6d 73 74 61 TUS, /* bMemsta
76e90 74 20 2a 2f 0a 20 20 20 31 2c 20 20 20 20 20 20 t */. 1,
76ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76eb0 20 20 20 2f 2a 20 62 43 6f 72 65 4d 75 74 65 78 /* bCoreMutex
76ec0 20 2a 2f 0a 20 20 20 53 51 4c 49 54 45 5f 54 48 */. SQLITE_TH
76ed0 52 45 41 44 53 41 46 45 3d 3d 31 2c 20 20 20 20 READSAFE==1,
76ee0 20 20 2f 2a 20 62 46 75 6c 6c 4d 75 74 65 78 20 /* bFullMutex
76ef0 2a 2f 0a 20 20 20 30 78 37 66 66 66 66 66 66 65 */. 0x7ffffffe
76f00 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
76f10 20 2f 2a 20 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a /* mxStrlen */.
76f20 20 20 20 31 30 30 2c 20 20 20 20 20 20 20 20 20 100,
76f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
76f40 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a szLookaside */.
76f50 20 20 20 35 30 30 2c 20 20 20 20 20 20 20 20 20 500,
76f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
76f70 20 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 nLookaside */.
76f80 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 {0,0,0,0,0,0,0
76f90 2c 30 7d 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ,0}, /*
76fa0 6d 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 m */. {0,0,0,0
76fb0 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 20 ,0,0,0,0,0},
76fc0 20 20 20 2f 2a 20 6d 75 74 65 78 20 2a 2f 0a 20 /* mutex */.
76fd0 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 {0,0,0,0,0,0,0
76fe0 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 2f 2a 20 ,0,0,0,0}, /*
76ff0 70 63 61 63 68 65 20 2a 2f 0a 20 20 20 28 76 6f pcache */. (vo
77000 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 id*)0,
77010 20 20 20 20 20 20 20 20 2f 2a 20 70 48 65 61 70 /* pHeap
77020 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
77030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77040 20 20 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a 20 20 /* nHeap */.
77050 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 20 20 0, 0,
77060 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d /* m
77070 6e 48 65 61 70 2c 20 6d 78 48 65 61 70 20 2a 2f nHeap, mxHeap */
77080 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 . (void*)0,
77090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
770a0 2a 20 70 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 * pScratch */.
770b0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
770c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
770d0 7a 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30 zScratch */. 0
770e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
770f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 53 63 /* nSc
77100 72 61 74 63 68 20 2a 2f 0a 20 20 20 28 76 6f 69 ratch */. (voi
77110 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 d*)0,
77120 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 /* pPage
77130 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
77140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77150 20 2f 2a 20 73 7a 50 61 67 65 20 2a 2f 0a 20 20 /* szPage */.
77160 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
77170 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e /* n
77180 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20 Page */. 0,
77190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
771a0 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 72 73 65 /* mxParse
771b0 72 53 74 61 63 6b 20 2a 2f 0a 20 20 20 30 2c 20 rStack */. 0,
771c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
771d0 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61 72 65 /* share
771e0 64 43 61 63 68 65 45 6e 61 62 6c 65 64 20 2a 2f dCacheEnabled */
771f0 0a 20 20 20 2f 2a 20 41 6c 6c 20 74 68 65 20 72 . /* All the r
77200 65 73 74 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 est should alway
77210 73 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 s be initialized
77220 20 74 6f 20 7a 65 72 6f 20 2a 2f 0a 20 20 20 30 to zero */. 0
77230 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
77240 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73 49 /* isI
77250 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 nit */. 0,
77260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77270 20 20 20 20 20 2f 2a 20 69 6e 50 72 6f 67 72 65 /* inProgre
77280 73 73 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 ss */. 0,
77290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
772a0 20 20 20 20 2f 2a 20 69 73 4d 75 74 65 78 49 6e /* isMutexIn
772b0 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 it */. 0,
772c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
772d0 20 20 20 20 2f 2a 20 69 73 4d 61 6c 6c 6f 63 49 /* isMallocI
772e0 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 nit */. 0,
772f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77300 20 20 20 20 20 2f 2a 20 69 73 50 43 61 63 68 65 /* isPCache
77310 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 Init */. 0,
77320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77330 20 20 20 20 20 20 2f 2a 20 70 49 6e 69 74 4d 75 /* pInitMu
77340 74 65 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 tex */. 0,
77350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77360 20 20 20 20 20 2f 2a 20 6e 52 65 66 49 6e 69 74 /* nRefInit
77370 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a Mutex */.};.../*
77380 0a 2a 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 .** Hash table f
77390 6f 72 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 or global functi
773a0 6f 6e 73 20 2d 20 66 75 6e 63 74 69 6f 6e 73 20 ons - functions
773b0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a common to all.**
773c0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
773d0 74 69 6f 6e 73 2e 20 20 41 66 74 65 72 20 69 6e tions. After in
773e0 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2c 20 74 68 itialization, th
773f0 69 73 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 72 is table is.** r
77400 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c ead-only..*/.SQL
77410 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 ITE_PRIVATE SQLI
77420 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61 TE_WSD FuncDefHa
77430 73 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sh sqlite3Global
77440 46 75 6e 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 0a 2a Functions;../*.*
77450 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 * The value of t
77460 68 65 20 22 70 65 6e 64 69 6e 67 22 20 62 79 74 he "pending" byt
77470 65 20 6d 75 73 74 20 62 65 20 30 78 34 30 30 30 e must be 0x4000
77480 30 30 30 30 20 28 31 20 62 79 74 65 20 70 61 73 0000 (1 byte pas
77490 74 20 74 68 65 0a 2a 2a 20 31 2d 67 69 62 61 62 t the.** 1-gibab
774a0 79 74 65 20 62 6f 75 6e 64 61 72 79 29 20 69 6e yte boundary) in
774b0 20 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 64 61 a compatible da
774c0 74 61 62 61 73 65 2e 20 20 53 51 4c 69 74 65 20 tabase. SQLite
774d0 6e 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 74 68 never uses.** th
774e0 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 e database page
774f0 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
77500 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 2e 20 e pending byte.
77510 20 49 74 20 6e 65 76 65 72 20 61 74 74 65 6d 70 It never attemp
77520 74 73 0a 2a 2a 20 74 6f 20 72 65 61 64 20 6f 72 ts.** to read or
77530 20 77 72 69 74 65 20 74 68 61 74 20 70 61 67 65 write that page
77540 2e 20 20 54 68 65 20 70 65 6e 64 69 6e 67 20 62 . The pending b
77550 79 74 65 20 70 61 67 65 20 69 73 20 73 65 74 20 yte page is set
77560 61 73 73 69 67 6e 0a 2a 2a 20 66 6f 72 20 75 73 assign.** for us
77570 65 20 62 79 20 74 68 65 20 56 46 53 20 6c 61 79 e by the VFS lay
77580 65 72 73 20 61 73 20 73 70 61 63 65 20 66 6f 72 ers as space for
77590 20 6d 61 6e 61 67 69 6e 67 20 66 69 6c 65 20 6c managing file l
775a0 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 69 ocks..**.** Duri
775b0 6e 67 20 74 65 73 74 69 6e 67 2c 20 69 74 20 69 ng testing, it i
775c0 73 20 6f 66 74 65 6e 20 64 65 73 69 72 61 62 6c s often desirabl
775d0 65 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 70 65 e to move the pe
775e0 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 0a 2a 2a nding byte to.**
775f0 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 6f 73 a different pos
77600 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 69 6c ition in the fil
77610 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 e. This allows
77620 63 6f 64 65 20 74 68 61 74 20 68 61 73 20 74 6f code that has to
77630 0a 2a 2a 20 64 65 61 6c 20 77 69 74 68 20 74 68 .** deal with th
77640 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74 e pending byte t
77650 6f 20 72 75 6e 20 6f 6e 20 66 69 6c 65 73 20 74 o run on files t
77660 68 61 74 20 61 72 65 20 6d 75 63 68 20 73 6d 61 hat are much sma
77670 6c 6c 65 72 0a 2a 2a 20 74 68 61 6e 20 31 20 47 ller.** than 1 G
77680 69 42 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 iB. The sqlite3
77690 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 _test_control()
776a0 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 interface can be
776b0 20 75 73 65 64 20 74 6f 0a 2a 2a 20 6d 6f 76 65 used to.** move
776c0 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 the pending byt
776d0 65 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 4f 52 54 41 e..**.** IMPORTA
776e0 4e 54 3a 20 20 43 68 61 6e 67 69 6e 67 20 74 68 NT: Changing th
776f0 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74 e pending byte t
77700 6f 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65 o any value othe
77710 72 20 74 68 61 6e 0a 2a 2a 20 30 78 34 30 30 30 r than.** 0x4000
77720 30 30 30 30 20 72 65 73 75 6c 74 73 20 69 6e 20 0000 results in
77730 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 an incompatible
77740 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f database file fo
77750 72 6d 61 74 21 0a 2a 2a 20 43 68 61 6e 67 69 6e rmat!.** Changin
77760 67 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 g the pending by
77770 74 65 20 64 75 72 69 6e 67 20 6f 70 65 72 61 74 te during operat
77780 69 6e 67 20 72 65 73 75 6c 74 73 20 69 6e 20 75 ing results in u
77790 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 61 6e 64 20 ndefined.** and
777a0 64 69 6c 65 74 65 72 69 6f 75 73 20 62 65 68 61 dileterious beha
777b0 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f vior..*/.SQLITE_
777c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
777d0 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 20 3d te3PendingByte =
777e0 20 30 78 34 30 30 30 30 30 30 30 3b 0a 0a 2f 2a 0x40000000;../*
777f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
77800 64 20 6f 66 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a d of global.c **
77810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
77820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
77830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
77840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
77850 67 69 6e 20 66 69 6c 65 20 73 74 61 74 75 73 2e gin file status.
77860 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
77870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
77880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
77890 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 38 .** 2008 June 18
778a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
778b0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
778c0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
778d0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
778e0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
778f0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
77900 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
77910 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
77920 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
77930 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
77940 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
77950 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
77960 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
77970 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
77980 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
77990 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
779a0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
779b0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
779c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
779d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
779e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
779f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
77a00 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c **.** This modul
77a10 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 e implements the
77a20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 sqlite3_status(
77a30 29 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64 20 ) interface and
77a40 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63 74 related.** funct
77a50 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 ionality..**.**
77a60 24 49 64 3a 20 73 74 61 74 75 73 2e 63 2c 76 20 $Id: status.c,v
77a70 31 2e 39 20 32 30 30 38 2f 30 39 2f 30 32 20 30 1.9 2008/09/02 0
77a80 30 3a 35 32 3a 35 32 20 64 72 68 20 45 78 70 20 0:52:52 drh Exp
77a90 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 72 69 $.*/../*.** Vari
77aa0 61 62 6c 65 73 20 69 6e 20 77 68 69 63 68 20 74 ables in which t
77ab0 6f 20 72 65 63 6f 72 64 20 73 74 61 74 75 73 20 o record status
77ac0 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a information..*/.
77ad0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 typedef struct s
77ae0 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 20 73 qlite3StatType s
77af0 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 3b 0a qlite3StatType;.
77b00 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 static SQLITE_WS
77b10 44 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 D struct sqlite3
77b20 53 74 61 74 54 79 70 65 20 7b 0a 20 20 69 6e 74 StatType {. int
77b30 20 6e 6f 77 56 61 6c 75 65 5b 39 5d 3b 20 20 20 nowValue[9];
77b40 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 /* Current
77b50 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 value */. int
77b60 6d 78 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 mxValue[9];
77b70 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
77b80 76 61 6c 75 65 20 2a 2f 0a 7d 20 73 71 6c 69 74 value */.} sqlit
77b90 65 33 53 74 61 74 20 3d 20 7b 20 7b 30 2c 7d 2c e3Stat = { {0,},
77ba0 20 7b 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 68 {0,} };.../* Th
77bb0 65 20 22 77 73 64 53 74 61 74 22 20 6d 61 63 72 e "wsdStat" macr
77bc0 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 o will resolve t
77bd0 6f 20 74 68 65 20 73 74 61 74 75 73 20 69 6e 66 o the status inf
77be0 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 74 61 74 ormation.** stat
77bf0 65 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 e vector. If wr
77c00 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 itable static da
77c10 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 ta is unsupporte
77c20 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c d on the target,
77c30 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c .** we have to l
77c40 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65 20 ocate the state
77c50 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 vector at run-ti
77c60 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 me. In the more
77c70 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 common.** case
77c80 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 where writable s
77c90 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73 75 tatic data is su
77ca0 70 70 6f 72 74 65 64 2c 20 77 73 64 53 74 61 74 pported, wsdStat
77cb0 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 can refer direc
77cc0 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 tly.** to the "s
77cd0 71 6c 69 74 65 33 53 74 61 74 22 20 73 74 61 74 qlite3Stat" stat
77ce0 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 e vector declare
77cf0 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 d above..*/.#ifd
77d00 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 ef SQLITE_OMIT_W
77d10 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 SD.# define wsdS
77d20 74 61 74 49 6e 69 74 20 20 73 71 6c 69 74 65 33 tatInit sqlite3
77d30 53 74 61 74 54 79 70 65 20 2a 78 20 3d 20 26 47 StatType *x = &G
77d40 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 53 74 61 LOBAL(sqlite3Sta
77d50 74 54 79 70 65 2c 73 71 6c 69 74 65 33 53 74 61 tType,sqlite3Sta
77d60 74 29 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 t).# define wsdS
77d70 74 61 74 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 tat x[0].#else.#
77d80 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 49 define wsdStatI
77d90 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64 nit.# define wsd
77da0 53 74 61 74 20 73 71 6c 69 74 65 33 53 74 61 74 Stat sqlite3Stat
77db0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
77dc0 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e eturn the curren
77dd0 74 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 t value of a sta
77de0 74 75 73 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a tus parameter..*
77df0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
77e00 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 61 74 int sqlite3Stat
77e10 75 73 56 61 6c 75 65 28 69 6e 74 20 6f 70 29 7b usValue(int op){
77e20 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a . wsdStatInit;.
77e30 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 assert( op>=0
77e40 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 && op<ArraySize(
77e50 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 wsdStat.nowValue
77e60 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 ) );. return ws
77e70 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f dStat.nowValue[o
77e80 70 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 p];.}../*.** Add
77e90 20 4e 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 N to the value
77ea0 6f 66 20 61 20 73 74 61 74 75 73 20 72 65 63 6f of a status reco
77eb0 72 64 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d rd. It is assum
77ec0 65 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 ed that the.** c
77ed0 61 6c 6c 65 72 20 68 6f 6c 64 73 20 61 70 70 72 aller holds appr
77ee0 6f 70 72 69 61 74 65 20 6c 6f 63 6b 73 2e 0a 2a opriate locks..*
77ef0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
77f00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 void sqlite3Sta
77f10 74 75 73 41 64 64 28 69 6e 74 20 6f 70 2c 20 69 tusAdd(int op, i
77f20 6e 74 20 4e 29 7b 0a 20 20 77 73 64 53 74 61 74 nt N){. wsdStat
77f30 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 Init;. assert(
77f40 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 op>=0 && op<Arra
77f50 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f ySize(wsdStat.no
77f60 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 77 73 64 wValue) );. wsd
77f70 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 Stat.nowValue[op
77f80 5d 20 2b 3d 20 4e 3b 0a 20 20 69 66 28 20 77 73 ] += N;. if( ws
77f90 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f dStat.nowValue[o
77fa0 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78 56 61 6c p]>wsdStat.mxVal
77fb0 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 77 73 ue[op] ){. ws
77fc0 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 dStat.mxValue[op
77fd0 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 ] = wsdStat.nowV
77fe0 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a alue[op];. }.}.
77ff0 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 ./*.** Set the v
78000 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 alue of a status
78010 20 74 6f 20 58 2e 0a 2a 2f 0a 53 51 4c 49 54 45 to X..*/.SQLITE
78020 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
78030 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 69 lite3StatusSet(i
78040 6e 74 20 6f 70 2c 20 69 6e 74 20 58 29 7b 0a 20 nt op, int X){.
78050 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 wsdStatInit;.
78060 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 assert( op>=0 &&
78070 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 op<ArraySize(ws
78080 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 dStat.nowValue)
78090 29 3b 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f 77 );. wsdStat.now
780a0 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 58 3b 0a 20 Value[op] = X;.
780b0 20 69 66 28 20 77 73 64 53 74 61 74 2e 6e 6f 77 if( wsdStat.now
780c0 56 61 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74 61 Value[op]>wsdSta
780d0 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29 7b t.mxValue[op] ){
780e0 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 . wsdStat.mxV
780f0 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 alue[op] = wsdSt
78100 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b at.nowValue[op];
78110 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 . }.}../*.** Qu
78120 65 72 79 20 73 74 61 74 75 73 20 69 6e 66 6f 72 ery status infor
78130 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 mation..**.** Th
78140 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f is implementatio
78150 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 72 n assumes that r
78160 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e eading or writin
78170 67 20 61 6e 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 g an aligned.**
78180 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 32-bit integer i
78190 73 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 s an atomic oper
781a0 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 61 74 20 ation. If that
781b0 61 73 73 75 6d 70 74 69 6f 6e 20 69 73 20 6e 6f assumption is no
781c0 74 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 t true,.** then
781d0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
781e0 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e 0a not threadsafe..
781f0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
78200 74 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 t sqlite3_status
78210 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 (int op, int *pC
78220 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 urrent, int *pHi
78230 67 68 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73 ghwater, int res
78240 65 74 46 6c 61 67 29 7b 0a 20 20 77 73 64 53 74 etFlag){. wsdSt
78250 61 74 49 6e 69 74 3b 0a 20 20 69 66 28 20 6f 70 atInit;. if( op
78260 3c 30 20 7c 7c 20 6f 70 3e 3d 41 72 72 61 79 53 <0 || op>=ArrayS
78270 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 ize(wsdStat.nowV
78280 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 72 65 74 alue) ){. ret
78290 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 urn SQLITE_MISUS
782a0 45 3b 0a 20 20 7d 0a 20 20 2a 70 43 75 72 72 65 E;. }. *pCurre
782b0 6e 74 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 nt = wsdStat.now
782c0 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70 48 Value[op];. *pH
782d0 69 67 68 77 61 74 65 72 20 3d 20 77 73 64 53 74 ighwater = wsdSt
782e0 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b 0a at.mxValue[op];.
782f0 20 20 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 if( resetFlag
78300 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d ){. wsdStat.m
78310 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 xValue[op] = wsd
78320 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 Stat.nowValue[op
78330 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 ];. }. return
78340 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
78350 0a 2a 2a 20 51 75 65 72 79 20 73 74 61 74 75 73 .** Query status
78360 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 information for
78370 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 a single databa
78380 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2f se connection.*/
78390 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
783a0 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 sqlite3_db_statu
783b0 73 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 s(. sqlite3 *db
783c0 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 , /* Th
783d0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
783e0 63 74 69 6f 6e 20 77 68 6f 73 65 20 73 74 61 74 ction whose stat
783f0 75 73 20 69 73 20 64 65 73 69 72 65 64 20 2a 2f us is desired */
78400 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 . int op,
78410 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 /* Stat
78420 75 73 20 76 65 72 62 20 2a 2f 0a 20 20 69 6e 74 us verb */. int
78430 20 2a 70 43 75 72 72 65 6e 74 2c 20 20 20 20 20 *pCurrent,
78440 20 20 20 2f 2a 20 57 72 69 74 65 20 63 75 72 72 /* Write curr
78450 65 6e 74 20 76 61 6c 75 65 20 68 65 72 65 20 2a ent value here *
78460 2f 0a 20 20 69 6e 74 20 2a 70 48 69 67 68 77 61 /. int *pHighwa
78470 74 65 72 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 ter, /* Wri
78480 74 65 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 te high-water ma
78490 72 6b 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 rk here */. int
784a0 20 72 65 73 65 74 46 6c 61 67 20 20 20 20 20 20 resetFlag
784b0 20 20 20 2f 2a 20 52 65 73 65 74 20 68 69 67 68 /* Reset high
784c0 2d 77 61 74 65 72 20 6d 61 72 6b 20 69 66 20 74 -water mark if t
784d0 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 rue */.){. swit
784e0 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 ch( op ){. ca
784f0 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 se SQLITE_DBSTAT
78500 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 US_LOOKASIDE_USE
78510 44 3a 20 7b 0a 20 20 20 20 20 20 2a 70 43 75 72 D: {. *pCur
78520 72 65 6e 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 rent = db->looka
78530 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 side.nOut;.
78540 20 2a 70 48 69 67 68 77 61 74 65 72 20 3d 20 64 *pHighwater = d
78550 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f b->lookaside.mxO
78560 75 74 3b 0a 20 20 20 20 20 20 69 66 28 20 72 65 ut;. if( re
78570 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 setFlag ){.
78580 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 db->lookaside
78590 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f .mxOut = db->loo
785a0 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 kaside.nOut;.
785b0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
785c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
785d0 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 ult: {. ret
785e0 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
785f0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
78600 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
78610 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
78620 2a 2a 20 45 6e 64 20 6f 66 20 73 74 61 74 75 73 ** End of status
78630 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
78640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78660 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
78670 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 64 61 ** Begin file da
78680 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a te.c ***********
78690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
786a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
786b0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4f 63 */./*.** 2003 Oc
786c0 74 6f 62 65 72 20 33 31 0a 2a 2a 0a 2a 2a 20 54 tober 31.**.** T
786d0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
786e0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
786f0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
78700 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
78710 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
78720 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
78730 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
78740 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
78750 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
78760 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
78770 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
78780 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
78790 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
787a0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
787b0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
787c0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
787d0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
787e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
787f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78820 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
78830 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 file contains th
78840 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 e C functions th
78850 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 64 61 74 at implement dat
78860 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 66 75 e and time.** fu
78870 6e 63 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 nctions for SQLi
78880 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 te. .**.** Ther
78890 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 e is only one ex
788a0 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20 69 6e ported symbol in
788b0 20 74 68 69 73 20 66 69 6c 65 20 2d 20 74 68 65 this file - the
788c0 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c function.** sql
788d0 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65 ite3RegisterDate
788e0 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29 20 TimeFunctions()
788f0 66 6f 75 6e 64 20 61 74 20 74 68 65 20 62 6f 74 found at the bot
78900 74 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e tom of the file.
78910 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f .** All other co
78920 64 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 de has file scop
78930 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64 61 e..**.** $Id: da
78940 74 65 2e 63 2c 76 20 31 2e 31 30 37 20 32 30 30 te.c,v 1.107 200
78950 39 2f 30 35 2f 30 33 20 32 30 3a 32 33 3a 35 33 9/05/03 20:23:53
78960 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a drh Exp $.**.**
78970 20 53 51 4c 69 74 65 20 70 72 6f 63 65 73 73 65 SQLite processe
78980 73 20 61 6c 6c 20 74 69 6d 65 73 20 61 6e 64 20 s all times and
78990 64 61 74 65 73 20 61 73 20 4a 75 6c 69 61 6e 20 dates as Julian
789a0 44 61 79 20 6e 75 6d 62 65 72 73 2e 20 20 54 68 Day numbers. Th
789b0 65 0a 2a 2a 20 64 61 74 65 73 20 61 6e 64 20 74 e.** dates and t
789c0 69 6d 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 imes are stored
789d0 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 as the number of
789e0 20 64 61 79 73 20 73 69 6e 63 65 20 6e 6f 6f 6e days since noon
789f0 0a 2a 2a 20 69 6e 20 47 72 65 65 6e 77 69 63 68 .** in Greenwich
78a00 20 6f 6e 20 4e 6f 76 65 6d 62 65 72 20 32 34 2c on November 24,
78a10 20 34 37 31 34 20 42 2e 43 2e 20 61 63 63 6f 72 4714 B.C. accor
78a20 64 69 6e 67 20 74 6f 20 74 68 65 20 47 72 65 67 ding to the Greg
78a30 6f 72 69 61 6e 0a 2a 2a 20 63 61 6c 65 6e 64 61 orian.** calenda
78a40 72 20 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a 2a r system. .**.**
78a50 20 31 39 37 30 2d 30 31 2d 30 31 20 30 30 3a 30 1970-01-01 00:0
78a60 30 3a 30 30 20 69 73 20 4a 44 20 32 34 34 30 35 0:00 is JD 24405
78a70 38 37 2e 35 0a 2a 2a 20 32 30 30 30 2d 30 31 2d 87.5.** 2000-01-
78a80 30 31 20 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 01 00:00:00 is J
78a90 44 20 32 34 35 31 35 34 34 2e 35 0a 2a 2a 0a 2a D 2451544.5.**.*
78aa0 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 * This implement
78ab0 69 6f 6e 20 72 65 71 75 69 72 65 73 20 79 65 61 ion requires yea
78ac0 72 73 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 rs to be express
78ad0 65 64 20 61 73 20 61 20 34 2d 64 69 67 69 74 20 ed as a 4-digit
78ae0 6e 75 6d 62 65 72 0a 2a 2a 20 77 68 69 63 68 20 number.** which
78af0 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 6c 79 20 means that only
78b00 64 61 74 65 73 20 62 65 74 77 65 65 6e 20 30 30 dates between 00
78b10 30 30 2d 30 31 2d 30 31 20 61 6e 64 20 39 39 39 00-01-01 and 999
78b20 39 2d 31 32 2d 33 31 20 63 61 6e 0a 2a 2a 20 62 9-12-31 can.** b
78b30 65 20 72 65 70 72 65 73 65 6e 74 65 64 2c 20 65 e represented, e
78b40 76 65 6e 20 74 68 6f 75 67 68 20 6a 75 6c 69 61 ven though julia
78b50 6e 20 64 61 79 20 6e 75 6d 62 65 72 73 20 61 6c n day numbers al
78b60 6c 6f 77 20 61 20 6d 75 63 68 20 77 69 64 65 72 low a much wider
78b70 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 74 .** range of dat
78b80 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 47 72 es..**.** The Gr
78b90 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 egorian calendar
78ba0 20 73 79 73 74 65 6d 20 69 73 20 75 73 65 64 20 system is used
78bb0 66 6f 72 20 61 6c 6c 20 64 61 74 65 73 20 61 6e for all dates an
78bc0 64 20 74 69 6d 65 73 2c 0a 2a 2a 20 65 76 65 6e d times,.** even
78bd0 20 74 68 6f 73 65 20 74 68 61 74 20 70 72 65 64 those that pred
78be0 61 74 65 20 74 68 65 20 47 72 65 67 6f 72 69 61 ate the Gregoria
78bf0 6e 20 63 61 6c 65 6e 64 61 72 2e 20 20 48 69 73 n calendar. His
78c00 74 6f 72 69 61 6e 73 20 75 73 75 61 6c 6c 79 0a torians usually.
78c10 2a 2a 20 75 73 65 20 74 68 65 20 4a 75 6c 69 61 ** use the Julia
78c20 6e 20 63 61 6c 65 6e 64 61 72 20 66 6f 72 20 64 n calendar for d
78c30 61 74 65 73 20 70 72 69 6f 72 20 74 6f 20 31 35 ates prior to 15
78c40 38 32 2d 31 30 2d 31 35 20 61 6e 64 20 66 6f 72 82-10-15 and for
78c50 20 73 6f 6d 65 0a 2a 2a 20 64 61 74 65 73 20 61 some.** dates a
78c60 66 74 65 72 77 61 72 64 73 2c 20 64 65 70 65 6e fterwards, depen
78c70 64 69 6e 67 20 6f 6e 20 6c 6f 63 61 6c 65 2e 20 ding on locale.
78c80 20 42 65 77 61 72 65 20 6f 66 20 74 68 69 73 20 Beware of this
78c90 64 69 66 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a 2a difference..**.*
78ca0 2a 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e * The conversion
78cb0 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 65 20 algorithms are
78cc0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 61 73 65 implemented base
78cd0 64 20 6f 6e 20 64 65 73 63 72 69 70 74 69 6f 6e d on description
78ce0 73 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c s.** in the foll
78cf0 6f 77 69 6e 67 20 74 65 78 74 3a 0a 2a 2a 0a 2a owing text:.**.*
78d00 2a 20 20 20 20 20 20 4a 65 61 6e 20 4d 65 65 75 * Jean Meeu
78d10 73 0a 2a 2a 20 20 20 20 20 20 41 73 74 72 6f 6e s.** Astron
78d20 6f 6d 69 63 61 6c 20 41 6c 67 6f 72 69 74 68 6d omical Algorithm
78d30 73 2c 20 32 6e 64 20 45 64 69 74 69 6f 6e 2c 20 s, 2nd Edition,
78d40 31 39 39 38 0a 2a 2a 20 20 20 20 20 20 49 53 42 1998.** ISB
78d50 4d 20 30 2d 39 34 33 33 39 36 2d 36 31 2d 31 0a M 0-943396-61-1.
78d60 2a 2a 20 20 20 20 20 20 57 69 6c 6c 6d 61 6e 6e ** Willmann
78d70 2d 42 65 6c 6c 2c 20 49 6e 63 0a 2a 2a 20 20 20 -Bell, Inc.**
78d80 20 20 20 52 69 63 68 6d 6f 6e 64 2c 20 56 69 72 Richmond, Vir
78d90 67 69 6e 69 61 20 28 55 53 41 29 0a 2a 2f 0a 23 ginia (USA).*/.#
78da0 69 6e 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e include <time.h>
78db0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
78dc0 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 _OMIT_DATETIME_F
78dd0 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 72 UNCS../*.** On r
78de0 65 63 65 6e 74 20 57 69 6e 64 6f 77 73 20 70 6c ecent Windows pl
78df0 61 74 66 6f 72 6d 73 2c 20 74 68 65 20 6c 6f 63 atforms, the loc
78e00 61 6c 74 69 6d 65 5f 73 28 29 20 66 75 6e 63 74 altime_s() funct
78e10 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 ion is available
78e20 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 74 .** as part of t
78e30 68 65 20 22 53 65 63 75 72 65 20 43 52 54 22 2e he "Secure CRT".
78e40 20 49 74 20 69 73 20 65 73 73 65 6e 74 69 61 6c It is essential
78e50 6c 79 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f ly equivalent to
78e60 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 .** localtime_r
78e70 28 29 20 61 76 61 69 6c 61 62 6c 65 20 75 6e 64 () available und
78e80 65 72 20 6d 6f 73 74 20 50 4f 53 49 58 20 70 6c er most POSIX pl
78e90 61 74 66 6f 72 6d 73 2c 20 65 78 63 65 70 74 20 atforms, except
78ea0 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 6f 72 64 that the .** ord
78eb0 65 72 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 er of the parame
78ec0 74 65 72 73 20 69 73 20 72 65 76 65 72 73 65 64 ters is reversed
78ed0 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 68 74 74 70 ..**.** See http
78ee0 3a 2f 2f 6d 73 64 6e 2e 6d 69 63 72 6f 73 6f 66 ://msdn.microsof
78ef0 74 2e 63 6f 6d 2f 65 6e 2d 75 73 2f 6c 69 62 72 t.com/en-us/libr
78f00 61 72 79 2f 61 34 34 32 78 33 79 65 28 56 53 2e ary/a442x3ye(VS.
78f10 38 30 29 2e 61 73 70 78 2e 0a 2a 2a 0a 2a 2a 20 80).aspx..**.**
78f20 49 66 20 74 68 65 20 75 73 65 72 20 68 61 73 20 If the user has
78f30 6e 6f 74 20 69 6e 64 69 63 61 74 65 64 20 74 6f not indicated to
78f40 20 75 73 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 use localtime_r
78f50 28 29 20 6f 72 20 6c 6f 63 61 6c 74 69 6d 65 5f () or localtime_
78f60 73 28 29 0a 2a 2a 20 61 6c 72 65 61 64 79 2c 20 s().** already,
78f70 63 68 65 63 6b 20 66 6f 72 20 61 6e 20 4d 53 56 check for an MSV
78f80 43 20 62 75 69 6c 64 20 65 6e 76 69 72 6f 6e 6d C build environm
78f90 65 6e 74 20 74 68 61 74 20 70 72 6f 76 69 64 65 ent that provide
78fa0 73 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f s .** localtime_
78fb0 73 28 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 s()..*/.#if !def
78fc0 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 ined(HAVE_LOCALT
78fd0 49 4d 45 5f 52 29 20 26 26 20 21 64 65 66 69 6e IME_R) && !defin
78fe0 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d ed(HAVE_LOCALTIM
78ff0 45 5f 53 29 20 26 26 20 5c 0a 20 20 20 20 20 64 E_S) && \. d
79000 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 efined(_MSC_VER)
79010 20 26 26 20 64 65 66 69 6e 65 64 28 5f 43 52 54 && defined(_CRT
79020 5f 49 4e 53 45 43 55 52 45 5f 44 45 50 52 45 43 _INSECURE_DEPREC
79030 41 54 45 29 0a 23 64 65 66 69 6e 65 20 48 41 56 ATE).#define HAV
79040 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 20 31 0a E_LOCALTIME_S 1.
79050 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 #endif../*.** A
79060 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 68 6f structure for ho
79070 6c 64 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 64 lding a single d
79080 61 74 65 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2f ate and time..*/
79090 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
790a0 44 61 74 65 54 69 6d 65 20 44 61 74 65 54 69 6d DateTime DateTim
790b0 65 3b 0a 73 74 72 75 63 74 20 44 61 74 65 54 69 e;.struct DateTi
790c0 6d 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 me {. sqlite3_i
790d0 6e 74 36 34 20 69 4a 44 3b 20 2f 2a 20 54 68 65 nt64 iJD; /* The
790e0 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 julian day numb
790f0 65 72 20 74 69 6d 65 73 20 38 36 34 30 30 30 30 er times 8640000
79100 30 20 2a 2f 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 0 */. int Y, M,
79110 20 44 3b 20 20 20 20 20 20 20 2f 2a 20 59 65 61 D; /* Yea
79120 72 2c 20 6d 6f 6e 74 68 2c 20 61 6e 64 20 64 61 r, month, and da
79130 79 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 6d 3b y */. int h, m;
79140 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 75 /* Hou
79150 72 20 61 6e 64 20 6d 69 6e 75 74 65 73 20 2a 2f r and minutes */
79160 0a 20 20 69 6e 74 20 74 7a 3b 20 20 20 20 20 20 . int tz;
79170 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 7a 6f 6e /* Timezon
79180 65 20 6f 66 66 73 65 74 20 69 6e 20 6d 69 6e 75 e offset in minu
79190 74 65 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 tes */. double
791a0 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 s; /* S
791b0 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 63 68 61 72 econds */. char
791c0 20 76 61 6c 69 64 59 4d 44 3b 20 20 20 20 20 2f validYMD; /
791d0 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 59 2c * True (1) if Y,
791e0 4d 2c 44 20 61 72 65 20 76 61 6c 69 64 20 2a 2f M,D are valid */
791f0 0a 20 20 63 68 61 72 20 76 61 6c 69 64 48 4d 53 . char validHMS
79200 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 ; /* True (1
79210 29 20 69 66 20 68 2c 6d 2c 73 20 61 72 65 20 76 ) if h,m,s are v
79220 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 alid */. char v
79230 61 6c 69 64 4a 44 3b 20 20 20 20 20 20 2f 2a 20 alidJD; /*
79240 54 72 75 65 20 28 31 29 20 69 66 20 69 4a 44 20 True (1) if iJD
79250 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 is valid */. ch
79260 61 72 20 76 61 6c 69 64 54 5a 3b 20 20 20 20 20 ar validTZ;
79270 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 /* True (1) if
79280 74 7a 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 7d tz is valid */.}
79290 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 ;.../*.** Conver
792a0 74 20 7a 44 61 74 65 20 69 6e 74 6f 20 6f 6e 65 t zDate into one
792b0 20 6f 72 20 6d 6f 72 65 20 69 6e 74 65 67 65 72 or more integer
792c0 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 61 s. Additional a
792d0 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f 6d 65 rguments.** come
792e0 20 69 6e 20 67 72 6f 75 70 73 20 6f 66 20 35 20 in groups of 5
792f0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
79300 2a 20 20 20 20 20 20 20 4e 20 20 20 20 20 20 20 * N
79310 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 69 74 73 number of digits
79320 20 69 6e 20 74 68 65 20 69 6e 74 65 67 65 72 0a in the integer.
79330 2a 2a 20 20 20 20 20 20 20 6d 69 6e 20 20 20 20 ** min
79340 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 minimum allowed
79350 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e value of the in
79360 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d teger.** m
79370 61 78 20 20 20 20 20 6d 61 78 69 6d 75 6d 20 61 ax maximum a
79380 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 llowed value of
79390 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 the integer.**
793a0 20 20 20 20 20 6e 65 78 74 43 20 20 20 66 69 72 nextC fir
793b0 73 74 20 63 68 61 72 61 63 74 65 72 20 61 66 74 st character aft
793c0 65 72 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a er the integer.*
793d0 2a 20 20 20 20 20 20 20 70 56 61 6c 20 20 20 20 * pVal
793e0 77 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 where to write t
793f0 68 65 20 69 6e 74 65 67 65 72 73 20 76 61 6c 75 he integers valu
79400 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 e..**.** Convers
79410 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 6e ions continue un
79420 74 69 6c 20 6f 6e 65 20 77 69 74 68 20 6e 65 78 til one with nex
79430 74 43 3d 3d 30 20 69 73 20 65 6e 63 6f 75 6e 74 tC==0 is encount
79440 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 75 6e ered..** The fun
79450 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 ction returns th
79460 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 75 63 63 e number of succ
79470 65 73 73 66 75 6c 20 63 6f 6e 76 65 72 73 69 6f essful conversio
79480 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ns..*/.static in
79490 74 20 67 65 74 44 69 67 69 74 73 28 63 6f 6e 73 t getDigits(cons
794a0 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 2e t char *zDate, .
794b0 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 ..){. va_list a
794c0 70 3b 0a 20 20 69 6e 74 20 76 61 6c 3b 0a 20 20 p;. int val;.
794d0 69 6e 74 20 4e 3b 0a 20 20 69 6e 74 20 6d 69 6e int N;. int min
794e0 3b 0a 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 69 ;. int max;. i
794f0 6e 74 20 6e 65 78 74 43 3b 0a 20 20 69 6e 74 20 nt nextC;. int
79500 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 63 6e 74 *pVal;. int cnt
79510 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 = 0;. va_start
79520 28 61 70 2c 20 7a 44 61 74 65 29 3b 0a 20 20 64 (ap, zDate);. d
79530 6f 7b 0a 20 20 20 20 4e 20 3d 20 76 61 5f 61 72 o{. N = va_ar
79540 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 g(ap, int);.
79550 6d 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c min = va_arg(ap,
79560 20 69 6e 74 29 3b 0a 20 20 20 20 6d 61 78 20 3d int);. max =
79570 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 va_arg(ap, int)
79580 3b 0a 20 20 20 20 6e 65 78 74 43 20 3d 20 76 61 ;. nextC = va
79590 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 _arg(ap, int);.
795a0 20 20 20 70 56 61 6c 20 3d 20 76 61 5f 61 72 67 pVal = va_arg
795b0 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 (ap, int*);.
795c0 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 val = 0;. whi
795d0 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 20 le( N-- ){.
795e0 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 if( !sqlite3Isd
795f0 69 67 69 74 28 2a 7a 44 61 74 65 29 20 29 7b 0a igit(*zDate) ){.
79600 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
79610 5f 67 65 74 44 69 67 69 74 73 3b 0a 20 20 20 20 _getDigits;.
79620 20 20 7d 0a 20 20 20 20 20 20 76 61 6c 20 3d 20 }. val =
79630 76 61 6c 2a 31 30 20 2b 20 2a 7a 44 61 74 65 20 val*10 + *zDate
79640 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 7a 44 61 - '0';. zDa
79650 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 te++;. }.
79660 69 66 28 20 76 61 6c 3c 6d 69 6e 20 7c 7c 20 76 if( val<min || v
79670 61 6c 3e 6d 61 78 20 7c 7c 20 28 6e 65 78 74 43 al>max || (nextC
79680 21 3d 30 20 26 26 20 6e 65 78 74 43 21 3d 2a 7a !=0 && nextC!=*z
79690 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20 67 Date) ){. g
796a0 6f 74 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74 oto end_getDigit
796b0 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 56 s;. }. *pV
796c0 61 6c 20 3d 20 76 61 6c 3b 0a 20 20 20 20 7a 44 al = val;. zD
796d0 61 74 65 2b 2b 3b 0a 20 20 20 20 63 6e 74 2b 2b ate++;. cnt++
796e0 3b 0a 20 20 7d 77 68 69 6c 65 28 20 6e 65 78 74 ;. }while( next
796f0 43 20 29 3b 0a 65 6e 64 5f 67 65 74 44 69 67 69 C );.end_getDigi
79700 74 73 3a 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 ts:. va_end(ap)
79710 3b 0a 20 20 72 65 74 75 72 6e 20 63 6e 74 3b 0a ;. return cnt;.
79720 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 65 }../*.** Read te
79730 78 74 20 66 72 6f 6d 20 7a 5b 5d 20 61 6e 64 20 xt from z[] and
79740 63 6f 6e 76 65 72 74 20 69 6e 74 6f 20 61 20 66 convert into a f
79750 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 loating point nu
79760 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a mber. Return.**
79770 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 the number of d
79780 69 67 69 74 73 20 63 6f 6e 76 65 72 74 65 64 2e igits converted.
79790 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 56 .*/.#define getV
797a0 61 6c 75 65 20 73 71 6c 69 74 65 33 41 74 6f 46 alue sqlite3AtoF
797b0 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20 ../*.** Parse a
797c0 74 69 6d 65 7a 6f 6e 65 20 65 78 74 65 6e 73 69 timezone extensi
797d0 6f 6e 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f 66 on on the end of
797e0 20 61 20 64 61 74 65 2d 74 69 6d 65 2e 0a 2a 2a a date-time..**
797f0 20 54 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 The extension i
79800 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a s of the form:.*
79810 2a 0a 2a 2a 20 20 20 20 20 20 20 20 28 2b 2f 2d *.** (+/-
79820 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 4f 72 20 )HH:MM.**.** Or
79830 74 68 65 20 22 7a 75 6c 75 22 20 6e 6f 74 61 74 the "zulu" notat
79840 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 ion:.**.**
79850 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 Z.**.** If the
79860 20 70 61 72 73 65 20 69 73 20 73 75 63 63 65 73 parse is succes
79870 73 66 75 6c 2c 20 77 72 69 74 65 20 74 68 65 20 sful, write the
79880 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6e 75 74 65 number of minute
79890 73 0a 2a 2a 20 6f 66 20 63 68 61 6e 67 65 20 69 s.** of change i
798a0 6e 20 70 2d 3e 74 7a 20 61 6e 64 20 72 65 74 75 n p->tz and retu
798b0 72 6e 20 30 2e 20 20 49 66 20 61 20 70 61 72 73 rn 0. If a pars
798c0 65 72 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c er error occurs,
798d0 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a .** return non-z
798e0 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 69 73 ero..**.** A mis
798f0 73 69 6e 67 20 73 70 65 63 69 66 69 65 72 20 69 sing specifier i
79900 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 s not considered
79910 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 an error..*/.st
79920 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 54 69 atic int parseTi
79930 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74 20 63 68 61 mezone(const cha
79940 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 r *zDate, DateTi
79950 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 73 67 me *p){. int sg
79960 6e 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48 72 n = 0;. int nHr
79970 2c 20 6e 4d 6e 3b 0a 20 20 69 6e 74 20 63 3b 0a , nMn;. int c;.
79980 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 while( sqlite3
79990 49 73 73 70 61 63 65 28 2a 7a 44 61 74 65 29 20 Isspace(*zDate)
799a0 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 ){ zDate++; }.
799b0 70 2d 3e 74 7a 20 3d 20 30 3b 0a 20 20 63 20 3d p->tz = 0;. c =
799c0 20 2a 7a 44 61 74 65 3b 0a 20 20 69 66 28 20 63 *zDate;. if( c
799d0 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 67 6e =='-' ){. sgn
799e0 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 20 69 = -1;. }else i
799f0 66 28 20 63 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 f( c=='+' ){.
79a00 20 73 67 6e 20 3d 20 2b 31 3b 0a 20 20 7d 65 6c sgn = +1;. }el
79a10 73 65 20 69 66 28 20 63 3d 3d 27 5a 27 20 7c 7c se if( c=='Z' ||
79a20 20 63 3d 3d 27 7a 27 20 29 7b 0a 20 20 20 20 7a c=='z' ){. z
79a30 44 61 74 65 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f Date++;. goto
79a40 20 7a 75 6c 75 5f 74 69 6d 65 3b 0a 20 20 7d 65 zulu_time;. }e
79a50 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
79a60 63 21 3d 30 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 c!=0;. }. zDat
79a70 65 2b 2b 3b 0a 20 20 69 66 28 20 67 65 74 44 69 e++;. if( getDi
79a80 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 gits(zDate, 2, 0
79a90 2c 20 31 34 2c 20 27 3a 27 2c 20 26 6e 48 72 2c , 14, ':', &nHr,
79aa0 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 6e 2, 0, 59, 0, &n
79ab0 4d 6e 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 Mn)!=2 ){. re
79ac0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 turn 1;. }. zD
79ad0 61 74 65 20 2b 3d 20 35 3b 0a 20 20 70 2d 3e 74 ate += 5;. p->t
79ae0 7a 20 3d 20 73 67 6e 2a 28 6e 4d 6e 20 2b 20 6e z = sgn*(nMn + n
79af0 48 72 2a 36 30 29 3b 0a 7a 75 6c 75 5f 74 69 6d Hr*60);.zulu_tim
79b00 65 3a 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 e:. while( sqli
79b10 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61 74 te3Isspace(*zDat
79b20 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d e) ){ zDate++; }
79b30 0a 20 20 72 65 74 75 72 6e 20 2a 7a 44 61 74 65 . return *zDate
79b40 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 !=0;.}../*.** Pa
79b50 72 73 65 20 74 69 6d 65 73 20 6f 66 20 74 68 65 rse times of the
79b60 20 66 6f 72 6d 20 48 48 3a 4d 4d 20 6f 72 20 48 form HH:MM or H
79b70 48 3a 4d 4d 3a 53 53 20 6f 72 20 48 48 3a 4d 4d H:MM:SS or HH:MM
79b80 3a 53 53 2e 46 46 46 46 2e 0a 2a 2a 20 54 68 65 :SS.FFFF..** The
79b90 20 48 48 2c 20 4d 4d 2c 20 61 6e 64 20 53 53 20 HH, MM, and SS
79ba0 6d 75 73 74 20 65 61 63 68 20 62 65 20 65 78 61 must each be exa
79bb0 63 74 6c 79 20 32 20 64 69 67 69 74 73 2e 20 20 ctly 2 digits.
79bc0 54 68 65 0a 2a 2a 20 66 72 61 63 74 69 6f 6e 61 The.** fractiona
79bd0 6c 20 73 65 63 6f 6e 64 73 20 46 46 46 46 20 63 l seconds FFFF c
79be0 61 6e 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 an be one or mor
79bf0 65 20 64 69 67 69 74 73 2e 0a 2a 2a 0a 2a 2a 20 e digits..**.**
79c00 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 72 Return 1 if ther
79c10 65 20 69 73 20 61 20 70 61 72 73 69 6e 67 20 65 e is a parsing e
79c20 72 72 6f 72 20 61 6e 64 20 30 20 6f 6e 20 73 75 rror and 0 on su
79c30 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 ccess..*/.static
79c40 20 69 6e 74 20 70 61 72 73 65 48 68 4d 6d 53 73 int parseHhMmSs
79c50 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 (const char *zDa
79c60 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 te, DateTime *p)
79c70 7b 0a 20 20 69 6e 74 20 68 2c 20 6d 2c 20 73 3b {. int h, m, s;
79c80 0a 20 20 64 6f 75 62 6c 65 20 6d 73 20 3d 20 30 . double ms = 0
79c90 2e 30 3b 0a 20 20 69 66 28 20 67 65 74 44 69 67 .0;. if( getDig
79ca0 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c its(zDate, 2, 0,
79cb0 20 32 34 2c 20 27 3a 27 2c 20 26 68 2c 20 32 2c 24, ':', &h, 2,
79cc0 20 30 2c 20 35 39 2c 20 30 2c 20 26 6d 29 21 3d 0, 59, 0, &m)!=
79cd0 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 2 ){. return
79ce0 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20 2b 1;. }. zDate +
79cf0 3d 20 35 3b 0a 20 20 69 66 28 20 2a 7a 44 61 74 = 5;. if( *zDat
79d00 65 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 7a 44 e==':' ){. zD
79d10 61 74 65 2b 2b 3b 0a 20 20 20 20 69 66 28 20 67 ate++;. if( g
79d20 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 etDigits(zDate,
79d30 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 73 29 2, 0, 59, 0, &s)
79d40 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 !=1 ){. ret
79d50 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 1;. }.
79d60 20 7a 44 61 74 65 20 2b 3d 20 32 3b 0a 20 20 20 zDate += 2;.
79d70 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27 2e 27 if( *zDate=='.'
79d80 20 26 26 20 73 71 6c 69 74 65 33 49 73 64 69 67 && sqlite3Isdig
79d90 69 74 28 7a 44 61 74 65 5b 31 5d 29 20 29 7b 0a it(zDate[1]) ){.
79da0 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 53 63 double rSc
79db0 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20 ale = 1.0;.
79dc0 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 20 20 zDate++;.
79dd0 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 while( sqlite3Is
79de0 64 69 67 69 74 28 2a 7a 44 61 74 65 29 20 29 7b digit(*zDate) ){
79df0 0a 20 20 20 20 20 20 20 20 6d 73 20 3d 20 6d 73 . ms = ms
79e00 2a 31 30 2e 30 20 2b 20 2a 7a 44 61 74 65 20 2d *10.0 + *zDate -
79e10 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 72 53 '0';. rS
79e20 63 61 6c 65 20 2a 3d 20 31 30 2e 30 3b 0a 20 20 cale *= 10.0;.
79e30 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 zDate++;.
79e40 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 73 20 }. ms
79e50 2f 3d 20 72 53 63 61 6c 65 3b 0a 20 20 20 20 7d /= rScale;. }
79e60 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 20 . }else{. s
79e70 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 = 0;. }. p->va
79e80 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e lidJD = 0;. p->
79e90 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 validHMS = 1;.
79ea0 70 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 2d 3e 6d p->h = h;. p->m
79eb0 20 3d 20 6d 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 = m;. p->s = s
79ec0 20 2b 20 6d 73 3b 0a 20 20 69 66 28 20 70 61 72 + ms;. if( par
79ed0 73 65 54 69 6d 65 7a 6f 6e 65 28 7a 44 61 74 65 seTimezone(zDate
79ee0 2c 20 70 29 20 29 20 72 65 74 75 72 6e 20 31 3b , p) ) return 1;
79ef0 0a 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 . p->validTZ =
79f00 28 70 2d 3e 74 7a 21 3d 30 29 3f 31 3a 30 3b 0a (p->tz!=0)?1:0;.
79f10 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
79f20 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 66 72 6f *.** Convert fro
79f30 6d 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a m YYYY-MM-DD HH:
79f40 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c 69 61 6e 20 MM:SS to julian
79f50 64 61 79 2e 20 20 57 65 20 61 6c 77 61 79 73 20 day. We always
79f60 61 73 73 75 6d 65 0a 2a 2a 20 74 68 61 74 20 74 assume.** that t
79f70 68 65 20 59 59 59 59 2d 4d 4d 2d 44 44 20 69 73 he YYYY-MM-DD is
79f80 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 according to th
79f90 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 e Gregorian cale
79fa0 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 ndar..**.** Refe
79fb0 72 65 6e 63 65 3a 20 20 4d 65 65 75 73 20 70 61 rence: Meeus pa
79fc0 67 65 20 36 31 0a 2a 2f 0a 73 74 61 74 69 63 20 ge 61.*/.static
79fd0 76 6f 69 64 20 63 6f 6d 70 75 74 65 4a 44 28 44 void computeJD(D
79fe0 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 ateTime *p){. i
79ff0 6e 74 20 59 2c 20 4d 2c 20 44 2c 20 41 2c 20 42 nt Y, M, D, A, B
7a000 2c 20 58 31 2c 20 58 32 3b 0a 0a 20 20 69 66 28 , X1, X2;.. if(
7a010 20 70 2d 3e 76 61 6c 69 64 4a 44 20 29 20 72 65 p->validJD ) re
7a020 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 76 turn;. if( p->v
7a030 61 6c 69 64 59 4d 44 20 29 7b 0a 20 20 20 20 59 alidYMD ){. Y
7a040 20 3d 20 70 2d 3e 59 3b 0a 20 20 20 20 4d 20 3d = p->Y;. M =
7a050 20 70 2d 3e 4d 3b 0a 20 20 20 20 44 20 3d 20 70 p->M;. D = p
7a060 2d 3e 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ->D;. }else{.
7a070 20 20 59 20 3d 20 32 30 30 30 3b 20 20 2f 2a 20 Y = 2000; /*
7a080 49 66 20 6e 6f 20 59 4d 44 20 73 70 65 63 69 66 If no YMD specif
7a090 69 65 64 2c 20 61 73 73 75 6d 65 20 32 30 30 30 ied, assume 2000
7a0a0 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 20 20 20 20 4d -Jan-01 */. M
7a0b0 20 3d 20 31 3b 0a 20 20 20 20 44 20 3d 20 31 3b = 1;. D = 1;
7a0c0 0a 20 20 7d 0a 20 20 69 66 28 20 4d 3c 3d 32 20 . }. if( M<=2
7a0d0 29 7b 0a 20 20 20 20 59 2d 2d 3b 0a 20 20 20 20 ){. Y--;.
7a0e0 4d 20 2b 3d 20 31 32 3b 0a 20 20 7d 0a 20 20 41 M += 12;. }. A
7a0f0 20 3d 20 59 2f 31 30 30 3b 0a 20 20 42 20 3d 20 = Y/100;. B =
7a100 32 20 2d 20 41 20 2b 20 28 41 2f 34 29 3b 0a 20 2 - A + (A/4);.
7a110 20 58 31 20 3d 20 33 36 35 32 35 2a 28 59 2b 34 X1 = 36525*(Y+4
7a120 37 31 36 29 2f 31 30 30 3b 0a 20 20 58 32 20 3d 716)/100;. X2 =
7a130 20 33 30 36 30 30 31 2a 28 4d 2b 31 29 2f 31 30 306001*(M+1)/10
7a140 30 30 30 3b 0a 20 20 70 2d 3e 69 4a 44 20 3d 20 000;. p->iJD =
7a150 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 (sqlite3_int64)(
7a160 28 58 31 20 2b 20 58 32 20 2b 20 44 20 2b 20 42 (X1 + X2 + D + B
7a170 20 2d 20 31 35 32 34 2e 35 20 29 20 2a 20 38 36 - 1524.5 ) * 86
7a180 34 30 30 30 30 30 29 3b 0a 20 20 70 2d 3e 76 61 400000);. p->va
7a190 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 69 66 28 lidJD = 1;. if(
7a1a0 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 7b 0a p->validHMS ){.
7a1b0 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 70 2d p->iJD += p-
7a1c0 3e 68 2a 33 36 30 30 30 30 30 20 2b 20 70 2d 3e >h*3600000 + p->
7a1d0 6d 2a 36 30 30 30 30 20 2b 20 28 73 71 6c 69 74 m*60000 + (sqlit
7a1e0 65 33 5f 69 6e 74 36 34 29 28 70 2d 3e 73 2a 31 e3_int64)(p->s*1
7a1f0 30 30 30 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 000);. if( p-
7a200 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 20 >validTZ ){.
7a210 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 70 2d 3e 74 p->iJD -= p->t
7a220 7a 2a 36 30 30 30 30 3b 0a 20 20 20 20 20 20 70 z*60000;. p
7a230 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b 0a ->validYMD = 0;.
7a240 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d p->validHM
7a250 53 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e S = 0;. p->
7a260 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 validTZ = 0;.
7a270 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
7a280 50 61 72 73 65 20 64 61 74 65 73 20 6f 66 20 74 Parse dates of t
7a290 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 he form.**.**
7a2a0 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a YYYY-MM-DD HH:
7a2b0 4d 4d 3a 53 53 2e 46 46 46 0a 2a 2a 20 20 20 20 MM:SS.FFF.**
7a2c0 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d YYYY-MM-DD HH:M
7a2d0 4d 3a 53 53 0a 2a 2a 20 20 20 20 20 59 59 59 59 M:SS.** YYYY
7a2e0 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 0a 2a 2a 20 -MM-DD HH:MM.**
7a2f0 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a YYYY-MM-DD.*
7a300 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 72 *.** Write the r
7a310 65 73 75 6c 74 20 69 6e 74 6f 20 74 68 65 20 44 esult into the D
7a320 61 74 65 54 69 6d 65 20 73 74 72 75 63 74 75 72 ateTime structur
7a330 65 20 61 6e 64 20 72 65 74 75 72 6e 20 30 0a 2a e and return 0.*
7a340 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 * on success and
7a350 20 31 20 69 66 20 74 68 65 20 69 6e 70 75 74 20 1 if the input
7a360 73 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 61 20 string is not a
7a370 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 64 well-formed.** d
7a380 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ate..*/.static i
7a390 6e 74 20 70 61 72 73 65 59 79 79 79 4d 6d 44 64 nt parseYyyyMmDd
7a3a0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 (const char *zDa
7a3b0 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 te, DateTime *p)
7a3c0 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c {. int Y, M, D,
7a3d0 20 6e 65 67 3b 0a 0a 20 20 69 66 28 20 7a 44 61 neg;.. if( zDa
7a3e0 74 65 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 te[0]=='-' ){.
7a3f0 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 6e zDate++;. n
7a400 65 67 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b eg = 1;. }else{
7a410 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 . neg = 0;.
7a420 7d 0a 20 20 69 66 28 20 67 65 74 44 69 67 69 74 }. if( getDigit
7a430 73 28 7a 44 61 74 65 2c 34 2c 30 2c 39 39 39 39 s(zDate,4,0,9999
7a440 2c 27 2d 27 2c 26 59 2c 32 2c 31 2c 31 32 2c 27 ,'-',&Y,2,1,12,'
7a450 2d 27 2c 26 4d 2c 32 2c 31 2c 33 31 2c 30 2c 26 -',&M,2,1,31,0,&
7a460 44 29 21 3d 33 20 29 7b 0a 20 20 20 20 72 65 74 D)!=3 ){. ret
7a470 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 urn 1;. }. zDa
7a480 74 65 20 2b 3d 20 31 30 3b 0a 20 20 77 68 69 6c te += 10;. whil
7a490 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 e( sqlite3Isspac
7a4a0 65 28 2a 7a 44 61 74 65 29 20 7c 7c 20 27 54 27 e(*zDate) || 'T'
7a4b0 3d 3d 2a 28 75 38 2a 29 7a 44 61 74 65 20 29 7b ==*(u8*)zDate ){
7a4c0 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 69 66 zDate++; }. if
7a4d0 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 44 ( parseHhMmSs(zD
7a4e0 61 74 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 20 ate, p)==0 ){.
7a4f0 20 20 2f 2a 20 57 65 20 67 6f 74 20 74 68 65 20 /* We got the
7a500 74 69 6d 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 time */. }else
7a510 69 66 28 20 2a 7a 44 61 74 65 3d 3d 30 20 29 7b if( *zDate==0 ){
7a520 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 . p->validHMS
7a530 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 0;. }else{.
7a540 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
7a550 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 . p->validJD =
7a560 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 0;. p->validYMD
7a570 20 3d 20 31 3b 0a 20 20 70 2d 3e 59 20 3d 20 6e = 1;. p->Y = n
7a580 65 67 20 3f 20 2d 59 20 3a 20 59 3b 0a 20 20 70 eg ? -Y : Y;. p
7a590 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 70 2d 3e 44 20 ->M = M;. p->D
7a5a0 3d 20 44 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 = D;. if( p->va
7a5b0 6c 69 64 54 5a 20 29 7b 0a 20 20 20 20 63 6f 6d lidTZ ){. com
7a5c0 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 7d 0a 20 puteJD(p);. }.
7a5d0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
7a5e0 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 69 6d 65 .** Set the time
7a5f0 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 to the current
7a600 74 69 6d 65 20 72 65 70 6f 72 74 65 64 20 62 79 time reported by
7a610 20 74 68 65 20 56 46 53 0a 2a 2f 0a 73 74 61 74 the VFS.*/.stat
7a620 69 63 20 76 6f 69 64 20 73 65 74 44 61 74 65 54 ic void setDateT
7a630 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 73 71 6c imeToCurrent(sql
7a640 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
7a650 6e 74 65 78 74 2c 20 44 61 74 65 54 69 6d 65 20 ntext, DateTime
7a660 2a 70 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72 3b *p){. double r;
7a670 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
7a680 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
7a690 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 _db_handle(conte
7a6a0 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 xt);. sqlite3Os
7a6b0 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e CurrentTime(db->
7a6c0 70 56 66 73 2c 20 26 72 29 3b 0a 20 20 70 2d 3e pVfs, &r);. p->
7a6d0 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 iJD = (sqlite3_i
7a6e0 6e 74 36 34 29 28 72 2a 38 36 34 30 30 30 30 30 nt64)(r*86400000
7a6f0 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 70 2d 3e .0 + 0.5);. p->
7a700 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 7d 0a 0a validJD = 1;.}..
7a710 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f /*.** Attempt to
7a720 20 70 61 72 73 65 20 74 68 65 20 67 69 76 65 6e parse the given
7a730 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 4a string into a J
7a740 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d 62 65 72 ulian Day Number
7a750 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 . Return.** the
7a760 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 number of error
7a770 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c s..**.** The fol
7a780 6c 6f 77 69 6e 67 20 61 72 65 20 61 63 63 65 70 lowing are accep
7a790 74 61 62 6c 65 20 66 6f 72 6d 73 20 66 6f 72 20 table forms for
7a7a0 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 the input string
7a7b0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 59 59 :.**.** YYY
7a7c0 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 Y-MM-DD HH:MM:SS
7a7d0 2e 46 46 46 20 20 2b 2f 2d 48 48 3a 4d 4d 0a 2a .FFF +/-HH:MM.*
7a7e0 2a 20 20 20 20 20 20 44 44 44 44 2e 44 44 20 0a * DDDD.DD .
7a7f0 2a 2a 20 20 20 20 20 20 6e 6f 77 0a 2a 2a 0a 2a ** now.**.*
7a800 2a 20 49 6e 20 74 68 65 20 66 69 72 73 74 20 66 * In the first f
7a810 6f 72 6d 2c 20 74 68 65 20 2b 2f 2d 48 48 3a 4d orm, the +/-HH:M
7a820 4d 20 69 73 20 61 6c 77 61 79 73 20 6f 70 74 69 M is always opti
7a830 6f 6e 61 6c 2e 20 20 54 68 65 20 66 72 61 63 74 onal. The fract
7a840 69 6f 6e 61 6c 0a 2a 2a 20 73 65 63 6f 6e 64 73 ional.** seconds
7a850 20 65 78 74 65 6e 73 69 6f 6e 20 28 74 68 65 20 extension (the
7a860 22 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69 6f ".FFF") is optio
7a870 6e 61 6c 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 nal. The second
7a880 73 20 70 6f 72 74 69 6f 6e 0a 2a 2a 20 28 22 3a s portion.** (":
7a890 53 53 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69 SS.FFF") is opti
7a8a0 6f 6e 2e 20 20 54 68 65 20 79 65 61 72 20 61 6e on. The year an
7a8b0 64 20 64 61 74 65 20 63 61 6e 20 62 65 20 6f 6d d date can be om
7a8c0 69 74 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a itted as long.**
7a8d0 20 61 73 20 74 68 65 72 65 20 69 73 20 61 20 74 as there is a t
7a8e0 69 6d 65 20 73 74 72 69 6e 67 2e 20 20 54 68 65 ime string. The
7a8f0 20 74 69 6d 65 20 73 74 72 69 6e 67 20 63 61 6e time string can
7a900 20 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c be omitted as l
7a910 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 ong.** as there
7a920 69 73 20 61 20 79 65 61 72 20 61 6e 64 20 64 61 is a year and da
7a930 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e te..*/.static in
7a940 74 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d t parseDateOrTim
7a950 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e e(. sqlite3_con
7a960 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a text *context, .
7a970 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
7a980 61 74 65 2c 20 0a 20 20 44 61 74 65 54 69 6d 65 ate, . DateTime
7a990 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 69 73 52 *p.){. int isR
7a9a0 65 61 6c 4e 75 6d 3b 20 20 20 20 2f 2a 20 52 65 ealNum; /* Re
7a9b0 74 75 72 6e 20 66 72 6f 6d 20 73 71 6c 69 74 65 turn from sqlite
7a9c0 33 49 73 4e 75 6d 62 65 72 28 29 2e 20 20 4e 6f 3IsNumber(). No
7a9d0 74 20 75 73 65 64 20 2a 2f 0a 20 20 69 66 28 20 t used */. if(
7a9e0 70 61 72 73 65 59 79 79 79 4d 6d 44 64 28 7a 44 parseYyyyMmDd(zD
7a9f0 61 74 65 2c 70 29 3d 3d 30 20 29 7b 0a 20 20 20 ate,p)==0 ){.
7aa00 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c return 0;. }el
7aa10 73 65 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d se if( parseHhMm
7aa20 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20 Ss(zDate, p)==0
7aa30 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
7aa40 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c . }else if( sql
7aa50 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 61 74 ite3StrICmp(zDat
7aa60 65 2c 22 6e 6f 77 22 29 3d 3d 30 29 7b 0a 20 20 e,"now")==0){.
7aa70 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f 43 setDateTimeToC
7aa80 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c 20 urrent(context,
7aa90 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 p);. return 0
7aaa0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 ;. }else if( sq
7aab0 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 7a 44 lite3IsNumber(zD
7aac0 61 74 65 2c 20 26 69 73 52 65 61 6c 4e 75 6d 2c ate, &isRealNum,
7aad0 20 53 51 4c 49 54 45 5f 55 54 46 38 29 20 29 7b SQLITE_UTF8) ){
7aae0 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20 . double r;.
7aaf0 20 20 20 67 65 74 56 61 6c 75 65 28 7a 44 61 74 getValue(zDat
7ab00 65 2c 20 26 72 29 3b 0a 20 20 20 20 70 2d 3e 69 e, &r);. p->i
7ab10 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e JD = (sqlite3_in
7ab20 74 36 34 29 28 72 2a 38 36 34 30 30 30 30 30 2e t64)(r*86400000.
7ab30 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70 2d 0 + 0.5);. p-
7ab40 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 >validJD = 1;.
7ab50 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
7ab60 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f return 1;.}../
7ab70 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 *.** Compute the
7ab80 20 59 65 61 72 2c 20 4d 6f 6e 74 68 2c 20 61 6e Year, Month, an
7ab90 64 20 44 61 79 20 66 72 6f 6d 20 74 68 65 20 6a d Day from the j
7aba0 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 ulian day number
7abb0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
7abc0 20 63 6f 6d 70 75 74 65 59 4d 44 28 44 61 74 65 computeYMD(Date
7abd0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Time *p){. int
7abe0 5a 2c 20 41 2c 20 42 2c 20 43 2c 20 44 2c 20 45 Z, A, B, C, D, E
7abf0 2c 20 58 31 3b 0a 20 20 69 66 28 20 70 2d 3e 76 , X1;. if( p->v
7ac00 61 6c 69 64 59 4d 44 20 29 20 72 65 74 75 72 6e alidYMD ) return
7ac10 3b 0a 20 20 69 66 28 20 21 70 2d 3e 76 61 6c 69 ;. if( !p->vali
7ac20 64 4a 44 20 29 7b 0a 20 20 20 20 70 2d 3e 59 20 dJD ){. p->Y
7ac30 3d 20 32 30 30 30 3b 0a 20 20 20 20 70 2d 3e 4d = 2000;. p->M
7ac40 20 3d 20 31 3b 0a 20 20 20 20 70 2d 3e 44 20 3d = 1;. p->D =
7ac50 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 1;. }else{.
7ac60 20 5a 20 3d 20 28 69 6e 74 29 28 28 70 2d 3e 69 Z = (int)((p->i
7ac70 4a 44 20 2b 20 34 33 32 30 30 30 30 30 29 2f 38 JD + 43200000)/8
7ac80 36 34 30 30 30 30 30 29 3b 0a 20 20 20 20 41 20 6400000);. A
7ac90 3d 20 28 69 6e 74 29 28 28 5a 20 2d 20 31 38 36 = (int)((Z - 186
7aca0 37 32 31 36 2e 32 35 29 2f 33 36 35 32 34 2e 32 7216.25)/36524.2
7acb0 35 29 3b 0a 20 20 20 20 41 20 3d 20 5a 20 2b 20 5);. A = Z +
7acc0 31 20 2b 20 41 20 2d 20 28 41 2f 34 29 3b 0a 20 1 + A - (A/4);.
7acd0 20 20 20 42 20 3d 20 41 20 2b 20 31 35 32 34 3b B = A + 1524;
7ace0 0a 20 20 20 20 43 20 3d 20 28 69 6e 74 29 28 28 . C = (int)((
7acf0 42 20 2d 20 31 32 32 2e 31 29 2f 33 36 35 2e 32 B - 122.1)/365.2
7ad00 35 29 3b 0a 20 20 20 20 44 20 3d 20 28 33 36 35 5);. D = (365
7ad10 32 35 2a 43 29 2f 31 30 30 3b 0a 20 20 20 20 45 25*C)/100;. E
7ad20 20 3d 20 28 69 6e 74 29 28 28 42 2d 44 29 2f 33 = (int)((B-D)/3
7ad30 30 2e 36 30 30 31 29 3b 0a 20 20 20 20 58 31 20 0.6001);. X1
7ad40 3d 20 28 69 6e 74 29 28 33 30 2e 36 30 30 31 2a = (int)(30.6001*
7ad50 45 29 3b 0a 20 20 20 20 70 2d 3e 44 20 3d 20 42 E);. p->D = B
7ad60 20 2d 20 44 20 2d 20 58 31 3b 0a 20 20 20 20 70 - D - X1;. p
7ad70 2d 3e 4d 20 3d 20 45 3c 31 34 20 3f 20 45 2d 31 ->M = E<14 ? E-1
7ad80 20 3a 20 45 2d 31 33 3b 0a 20 20 20 20 70 2d 3e : E-13;. p->
7ad90 59 20 3d 20 70 2d 3e 4d 3e 32 20 3f 20 43 20 2d Y = p->M>2 ? C -
7ada0 20 34 37 31 36 20 3a 20 43 20 2d 20 34 37 31 35 4716 : C - 4715
7adb0 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 ;. }. p->valid
7adc0 59 4d 44 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a YMD = 1;.}../*.*
7add0 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 48 6f * Compute the Ho
7ade0 75 72 2c 20 4d 69 6e 75 74 65 2c 20 61 6e 64 20 ur, Minute, and
7adf0 53 65 63 6f 6e 64 73 20 66 72 6f 6d 20 74 68 65 Seconds from the
7ae00 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 julian day numb
7ae10 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f er..*/.static vo
7ae20 69 64 20 63 6f 6d 70 75 74 65 48 4d 53 28 44 61 id computeHMS(Da
7ae30 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e teTime *p){. in
7ae40 74 20 73 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 t s;. if( p->va
7ae50 6c 69 64 48 4d 53 20 29 20 72 65 74 75 72 6e 3b lidHMS ) return;
7ae60 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b . computeJD(p);
7ae70 0a 20 20 73 20 3d 20 28 69 6e 74 29 28 28 70 2d . s = (int)((p-
7ae80 3e 69 4a 44 20 2b 20 34 33 32 30 30 30 30 30 29 >iJD + 43200000)
7ae90 20 25 20 38 36 34 30 30 30 30 30 29 3b 0a 20 20 % 86400000);.
7aea0 70 2d 3e 73 20 3d 20 73 2f 31 30 30 30 2e 30 3b p->s = s/1000.0;
7aeb0 0a 20 20 73 20 3d 20 28 69 6e 74 29 70 2d 3e 73 . s = (int)p->s
7aec0 3b 0a 20 20 70 2d 3e 73 20 2d 3d 20 73 3b 0a 20 ;. p->s -= s;.
7aed0 20 70 2d 3e 68 20 3d 20 73 2f 33 36 30 30 3b 0a p->h = s/3600;.
7aee0 20 20 73 20 2d 3d 20 70 2d 3e 68 2a 33 36 30 30 s -= p->h*3600
7aef0 3b 0a 20 20 70 2d 3e 6d 20 3d 20 73 2f 36 30 3b ;. p->m = s/60;
7af00 0a 20 20 70 2d 3e 73 20 2b 3d 20 73 20 2d 20 70 . p->s += s - p
7af10 2d 3e 6d 2a 36 30 3b 0a 20 20 70 2d 3e 76 61 6c ->m*60;. p->val
7af20 69 64 48 4d 53 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a idHMS = 1;.}../*
7af30 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 62 6f 74 68 .** Compute both
7af40 20 59 4d 44 20 61 6e 64 20 48 4d 53 0a 2a 2f 0a YMD and HMS.*/.
7af50 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 static void comp
7af60 75 74 65 59 4d 44 5f 48 4d 53 28 44 61 74 65 54 uteYMD_HMS(DateT
7af70 69 6d 65 20 2a 70 29 7b 0a 20 20 63 6f 6d 70 75 ime *p){. compu
7af80 74 65 59 4d 44 28 70 29 3b 0a 20 20 63 6f 6d 70 teYMD(p);. comp
7af90 75 74 65 48 4d 53 28 70 29 3b 0a 7d 0a 0a 2f 2a uteHMS(p);.}../*
7afa0 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 59 4d .** Clear the YM
7afb0 44 20 61 6e 64 20 48 4d 53 20 61 6e 64 20 74 68 D and HMS and th
7afc0 65 20 54 5a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 e TZ.*/.static v
7afd0 6f 69 64 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 oid clearYMD_HMS
7afe0 5f 54 5a 28 44 61 74 65 54 69 6d 65 20 2a 70 29 _TZ(DateTime *p)
7aff0 7b 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 {. p->validYMD
7b000 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48 = 0;. p->validH
7b010 4d 53 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c MS = 0;. p->val
7b020 69 64 54 5a 20 3d 20 30 3b 0a 7d 0a 0a 23 69 66 idTZ = 0;.}..#if
7b030 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
7b040 5f 4c 4f 43 41 4c 54 49 4d 45 0a 2f 2a 0a 2a 2a _LOCALTIME./*.**
7b050 20 43 6f 6d 70 75 74 65 20 74 68 65 20 64 69 66 Compute the dif
7b060 66 65 72 65 6e 63 65 20 28 69 6e 20 6d 69 6c 6c ference (in mill
7b070 69 73 65 63 6f 6e 64 73 29 0a 2a 2a 20 62 65 74 iseconds).** bet
7b080 77 65 65 6e 20 6c 6f 63 61 6c 74 69 6d 65 20 61 ween localtime a
7b090 6e 64 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 nd UTC (a.k.a. G
7b0a0 4d 54 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 MT).** for the t
7b0b0 69 6d 65 20 76 61 6c 75 65 20 70 20 77 68 65 72 ime value p wher
7b0c0 65 20 70 20 69 73 20 69 6e 20 55 54 43 2e 0a 2a e p is in UTC..*
7b0d0 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 /.static sqlite3
7b0e0 5f 69 6e 74 36 34 20 6c 6f 63 61 6c 74 69 6d 65 _int64 localtime
7b0f0 4f 66 66 73 65 74 28 44 61 74 65 54 69 6d 65 20 Offset(DateTime
7b100 2a 70 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 *p){. DateTime
7b110 78 2c 20 79 3b 0a 20 20 74 69 6d 65 5f 74 20 74 x, y;. time_t t
7b120 3b 0a 20 20 78 20 3d 20 2a 70 3b 0a 20 20 63 6f ;. x = *p;. co
7b130 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 mputeYMD_HMS(&x)
7b140 3b 0a 20 20 69 66 28 20 78 2e 59 3c 31 39 37 31 ;. if( x.Y<1971
7b150 20 7c 7c 20 78 2e 59 3e 3d 32 30 33 38 20 29 7b || x.Y>=2038 ){
7b160 0a 20 20 20 20 78 2e 59 20 3d 20 32 30 30 30 3b . x.Y = 2000;
7b170 0a 20 20 20 20 78 2e 4d 20 3d 20 31 3b 0a 20 20 . x.M = 1;.
7b180 20 20 78 2e 44 20 3d 20 31 3b 0a 20 20 20 20 78 x.D = 1;. x
7b190 2e 68 20 3d 20 30 3b 0a 20 20 20 20 78 2e 6d 20 .h = 0;. x.m
7b1a0 3d 20 30 3b 0a 20 20 20 20 78 2e 73 20 3d 20 30 = 0;. x.s = 0
7b1b0 2e 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 .0;. } else {.
7b1c0 20 20 20 69 6e 74 20 73 20 3d 20 28 69 6e 74 29 int s = (int)
7b1d0 28 78 2e 73 20 2b 20 30 2e 35 29 3b 0a 20 20 20 (x.s + 0.5);.
7b1e0 20 78 2e 73 20 3d 20 73 3b 0a 20 20 7d 0a 20 20 x.s = s;. }.
7b1f0 78 2e 74 7a 20 3d 20 30 3b 0a 20 20 78 2e 76 61 x.tz = 0;. x.va
7b200 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 63 6f 6d lidJD = 0;. com
7b210 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 74 20 puteJD(&x);. t
7b220 3d 20 78 2e 69 4a 44 2f 31 30 30 30 20 2d 20 32 = x.iJD/1000 - 2
7b230 31 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30 1086676*(i64)100
7b240 30 30 3b 0a 23 69 66 64 65 66 20 48 41 56 45 5f 00;.#ifdef HAVE_
7b250 4c 4f 43 41 4c 54 49 4d 45 5f 52 0a 20 20 7b 0a LOCALTIME_R. {.
7b260 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c struct tm sL
7b270 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 6c 74 ocal;. localt
7b280 69 6d 65 5f 72 28 26 74 2c 20 26 73 4c 6f 63 61 ime_r(&t, &sLoca
7b290 6c 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 73 4c l);. y.Y = sL
7b2a0 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31 ocal.tm_year + 1
7b2b0 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 73 900;. y.M = s
7b2c0 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 Local.tm_mon + 1
7b2d0 3b 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c 6f 63 ;. y.D = sLoc
7b2e0 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 al.tm_mday;.
7b2f0 79 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f y.h = sLocal.tm_
7b300 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 hour;. y.m =
7b310 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 sLocal.tm_min;.
7b320 20 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e y.s = sLocal.
7b330 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 6c 69 tm_sec;. }.#eli
7b340 66 20 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c f defined(HAVE_L
7b350 4f 43 41 4c 54 49 4d 45 5f 53 29 0a 20 20 7b 0a OCALTIME_S). {.
7b360 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c struct tm sL
7b370 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 6c 74 ocal;. localt
7b380 69 6d 65 5f 73 28 26 73 4c 6f 63 61 6c 2c 20 26 ime_s(&sLocal, &
7b390 74 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 73 4c t);. y.Y = sL
7b3a0 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31 ocal.tm_year + 1
7b3b0 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 73 900;. y.M = s
7b3c0 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 Local.tm_mon + 1
7b3d0 3b 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c 6f 63 ;. y.D = sLoc
7b3e0 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 al.tm_mday;.
7b3f0 79 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f y.h = sLocal.tm_
7b400 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 hour;. y.m =
7b410 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 sLocal.tm_min;.
7b420 20 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e y.s = sLocal.
7b430 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 6c 73 tm_sec;. }.#els
7b440 65 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 e. {. struct
7b450 20 74 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 tm *pTm;. sq
7b460 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
7b470 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c r(sqlite3MutexAl
7b480 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
7b490 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 _STATIC_MASTER))
7b4a0 3b 0a 20 20 20 20 70 54 6d 20 3d 20 6c 6f 63 61 ;. pTm = loca
7b4b0 6c 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 79 ltime(&t);. y
7b4c0 2e 59 20 3d 20 70 54 6d 2d 3e 74 6d 5f 79 65 61 .Y = pTm->tm_yea
7b4d0 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e r + 1900;. y.
7b4e0 4d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 6f 6e 20 M = pTm->tm_mon
7b4f0 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 70 + 1;. y.D = p
7b500 54 6d 2d 3e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 Tm->tm_mday;.
7b510 20 79 2e 68 20 3d 20 70 54 6d 2d 3e 74 6d 5f 68 y.h = pTm->tm_h
7b520 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 70 our;. y.m = p
7b530 54 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 Tm->tm_min;.
7b540 79 2e 73 20 3d 20 70 54 6d 2d 3e 74 6d 5f 73 65 y.s = pTm->tm_se
7b550 63 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d c;. sqlite3_m
7b560 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 utex_leave(sqlit
7b570 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
7b580 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
7b590 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d 0a 23 _MASTER));. }.#
7b5a0 65 6e 64 69 66 0a 20 20 79 2e 76 61 6c 69 64 59 endif. y.validY
7b5b0 4d 44 20 3d 20 31 3b 0a 20 20 79 2e 76 61 6c 69 MD = 1;. y.vali
7b5c0 64 48 4d 53 20 3d 20 31 3b 0a 20 20 79 2e 76 61 dHMS = 1;. y.va
7b5d0 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 79 2e 76 lidJD = 0;. y.v
7b5e0 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 63 6f alidTZ = 0;. co
7b5f0 6d 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20 72 mputeJD(&y);. r
7b600 65 74 75 72 6e 20 79 2e 69 4a 44 20 2d 20 78 2e eturn y.iJD - x.
7b610 69 4a 44 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a iJD;.}.#endif /*
7b620 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 SQLITE_OMIT_LOC
7b630 41 4c 54 49 4d 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a ALTIME */../*.**
7b640 20 50 72 6f 63 65 73 73 20 61 20 6d 6f 64 69 66 Process a modif
7b650 69 65 72 20 74 6f 20 61 20 64 61 74 65 2d 74 69 ier to a date-ti
7b660 6d 65 20 73 74 61 6d 70 2e 20 20 54 68 65 20 6d me stamp. The m
7b670 6f 64 69 66 69 65 72 73 20 61 72 65 0a 2a 2a 20 odifiers are.**
7b680 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
7b690 2a 20 20 20 20 20 4e 4e 4e 20 64 61 79 73 0a 2a * NNN days.*
7b6a0 2a 20 20 20 20 20 4e 4e 4e 20 68 6f 75 72 73 0a * NNN hours.
7b6b0 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 69 6e 75 74 ** NNN minut
7b6c0 65 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 2e 4e 4e es.** NNN.NN
7b6d0 4e 4e 20 73 65 63 6f 6e 64 73 0a 2a 2a 20 20 20 NN seconds.**
7b6e0 20 20 4e 4e 4e 20 6d 6f 6e 74 68 73 0a 2a 2a 20 NNN months.**
7b6f0 20 20 20 20 4e 4e 4e 20 79 65 61 72 73 0a 2a 2a NNN years.**
7b700 20 20 20 20 20 73 74 61 72 74 20 6f 66 20 6d 6f start of mo
7b710 6e 74 68 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 nth.** start
7b720 20 6f 66 20 79 65 61 72 0a 2a 2a 20 20 20 20 20 of year.**
7b730 73 74 61 72 74 20 6f 66 20 77 65 65 6b 0a 2a 2a start of week.**
7b740 20 20 20 20 20 73 74 61 72 74 20 6f 66 20 64 61 start of da
7b750 79 0a 2a 2a 20 20 20 20 20 77 65 65 6b 64 61 79 y.** weekday
7b760 20 4e 0a 2a 2a 20 20 20 20 20 75 6e 69 78 65 70 N.** unixep
7b770 6f 63 68 0a 2a 2a 20 20 20 20 20 6c 6f 63 61 6c och.** local
7b780 74 69 6d 65 0a 2a 2a 20 20 20 20 20 75 74 63 0a time.** utc.
7b790 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 6f **.** Return 0 o
7b7a0 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 31 20 n success and 1
7b7b0 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 if there is any
7b7c0 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 2e 0a 2a kind of error..*
7b7d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 /.static int par
7b7e0 73 65 4d 6f 64 69 66 69 65 72 28 63 6f 6e 73 74 seModifier(const
7b7f0 20 63 68 61 72 20 2a 7a 4d 6f 64 2c 20 44 61 74 char *zMod, Dat
7b800 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 eTime *p){. int
7b810 20 72 63 20 3d 20 31 3b 0a 20 20 69 6e 74 20 6e rc = 1;. int n
7b820 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20 ;. double r;.
7b830 63 68 61 72 20 2a 7a 2c 20 7a 42 75 66 5b 33 30 char *z, zBuf[30
7b840 5d 3b 0a 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 ];. z = zBuf;.
7b850 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 41 72 72 61 for(n=0; n<Arra
7b860 79 53 69 7a 65 28 7a 42 75 66 29 2d 31 20 26 26 ySize(zBuf)-1 &&
7b870 20 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b 2b 29 7b 0a zMod[n]; n++){.
7b880 20 20 20 20 7a 5b 6e 5d 20 3d 20 28 63 68 61 72 z[n] = (char
7b890 29 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c )sqlite3UpperToL
7b8a0 6f 77 65 72 5b 28 75 38 29 7a 4d 6f 64 5b 6e 5d ower[(u8)zMod[n]
7b8b0 5d 3b 0a 20 20 7d 0a 20 20 7a 5b 6e 5d 20 3d 20 ];. }. z[n] =
7b8c0 30 3b 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 30 0;. switch( z[0
7b8d0 5d 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c ] ){.#ifndef SQL
7b8e0 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 ITE_OMIT_LOCALTI
7b8f0 4d 45 0a 20 20 20 20 63 61 73 65 20 27 6c 27 3a ME. case 'l':
7b900 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 6c {. /* l
7b910 6f 63 61 6c 74 69 6d 65 0a 20 20 20 20 20 20 2a ocaltime. *
7b920 2a 0a 20 20 20 20 20 20 2a 2a 20 41 73 73 75 6d *. ** Assum
7b930 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 ing the current
7b940 74 69 6d 65 20 76 61 6c 75 65 20 69 73 20 55 54 time value is UT
7b950 43 20 28 61 2e 6b 2e 61 2e 20 47 4d 54 29 2c 20 C (a.k.a. GMT),
7b960 73 68 69 66 74 20 69 74 20 74 6f 0a 20 20 20 20 shift it to.
7b970 20 20 2a 2a 20 73 68 6f 77 20 6c 6f 63 61 6c 20 ** show local
7b980 74 69 6d 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 time.. */.
7b990 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 if( strcmp(
7b9a0 7a 2c 20 22 6c 6f 63 61 6c 74 69 6d 65 22 29 3d z, "localtime")=
7b9b0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f =0 ){. co
7b9c0 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 mputeJD(p);.
7b9d0 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 6c 6f p->iJD += lo
7b9e0 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29 caltimeOffset(p)
7b9f0 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 ;. clearY
7ba00 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 MD_HMS_TZ(p);.
7ba10 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 rc = 0;.
7ba20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
7ba30 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a k;. }.#endif.
7ba40 20 20 20 20 63 61 73 65 20 27 75 27 3a 20 7b 0a case 'u': {.
7ba50 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a /*. *
7ba60 2a 20 20 20 20 75 6e 69 78 65 70 6f 63 68 0a 20 * unixepoch.
7ba70 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
7ba80 20 54 72 65 61 74 20 74 68 65 20 63 75 72 72 65 Treat the curre
7ba90 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69 nt value of p->i
7baa0 4a 44 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 JD as the number
7bab0 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 of. ** sec
7bac0 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 2e onds since 1970.
7bad0 20 20 43 6f 6e 76 65 72 74 20 74 6f 20 61 20 72 Convert to a r
7bae0 65 61 6c 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e eal julian day n
7baf0 75 6d 62 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a umber.. */.
7bb00 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 if( strcmp
7bb10 28 7a 2c 20 22 75 6e 69 78 65 70 6f 63 68 22 29 (z, "unixepoch")
7bb20 3d 3d 30 20 26 26 20 70 2d 3e 76 61 6c 69 64 4a ==0 && p->validJ
7bb30 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e D ){. p->
7bb40 69 4a 44 20 3d 20 28 70 2d 3e 69 4a 44 20 2b 20 iJD = (p->iJD +
7bb50 34 33 32 30 30 29 2f 38 36 34 30 30 20 2b 20 32 43200)/86400 + 2
7bb60 31 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30 1086676*(i64)100
7bb70 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 63 00000;. c
7bb80 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 learYMD_HMS_TZ(p
7bb90 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
7bba0 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 0;. }.#ifnd
7bbb0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c ef SQLITE_OMIT_L
7bbc0 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 20 20 65 OCALTIME. e
7bbd0 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a lse if( strcmp(z
7bbe0 2c 20 22 75 74 63 22 29 3d 3d 30 20 29 7b 0a 20 , "utc")==0 ){.
7bbf0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 sqlite3_i
7bc00 6e 74 36 34 20 63 31 3b 0a 20 20 20 20 20 20 20 nt64 c1;.
7bc10 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 computeJD(p);.
7bc20 20 20 20 20 20 20 20 63 31 20 3d 20 6c 6f 63 61 c1 = loca
7bc30 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a ltimeOffset(p);.
7bc40 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d p->iJD -
7bc50 3d 20 63 31 3b 0a 20 20 20 20 20 20 20 20 63 6c = c1;. cl
7bc60 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 earYMD_HMS_TZ(p)
7bc70 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 ;. p->iJD
7bc80 20 2b 3d 20 63 31 20 2d 20 6c 6f 63 61 6c 74 69 += c1 - localti
7bc90 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20 meOffset(p);.
7bca0 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 rc = 0;.
7bcb0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
7bcc0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
7bcd0 20 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 case 'w': {.
7bce0 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a /*. **
7bcf0 20 20 20 20 77 65 65 6b 64 61 79 20 4e 0a 20 20 weekday N.
7bd00 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
7bd10 4d 6f 76 65 20 74 68 65 20 64 61 74 65 20 74 6f Move the date to
7bd20 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 6f the same time o
7bd30 6e 20 74 68 65 20 6e 65 78 74 20 6f 63 63 75 72 n the next occur
7bd40 72 65 6e 63 65 20 6f 66 0a 20 20 20 20 20 20 2a rence of. *
7bd50 2a 20 77 65 65 6b 64 61 79 20 4e 20 77 68 65 72 * weekday N wher
7bd60 65 20 30 3d 3d 53 75 6e 64 61 79 2c 20 31 3d 3d e 0==Sunday, 1==
7bd70 4d 6f 6e 64 61 79 2c 20 61 6e 64 20 73 6f 20 66 Monday, and so f
7bd80 6f 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 orth. If the.
7bd90 20 20 20 20 2a 2a 20 64 61 74 65 20 69 73 20 61 ** date is a
7bda0 6c 72 65 61 64 79 20 6f 6e 20 74 68 65 20 61 70 lready on the ap
7bdb0 70 72 6f 70 72 69 61 74 65 20 77 65 65 6b 64 61 propriate weekda
7bdc0 79 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d y, this is a no-
7bdd0 6f 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 op.. */.
7bde0 20 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a if( strncmp(z
7bdf0 2c 20 22 77 65 65 6b 64 61 79 20 22 2c 20 38 29 , "weekday ", 8)
7be00 3d 3d 30 20 26 26 20 67 65 74 56 61 6c 75 65 28 ==0 && getValue(
7be10 26 7a 5b 38 5d 2c 26 72 29 3e 30 0a 20 20 20 20 &z[8],&r)>0.
7be20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 &&
7be30 28 6e 3d 28 69 6e 74 29 72 29 3d 3d 72 20 26 26 (n=(int)r)==r &&
7be40 20 6e 3e 3d 30 20 26 26 20 72 3c 37 20 29 7b 0a n>=0 && r<7 ){.
7be50 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
7be60 69 6e 74 36 34 20 5a 3b 0a 20 20 20 20 20 20 20 int64 Z;.
7be70 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 computeYMD_HMS(
7be80 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 p);. p->v
7be90 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 alidTZ = 0;.
7bea0 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d p->validJD =
7beb0 20 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 0;. comp
7bec0 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 uteJD(p);.
7bed0 20 20 5a 20 3d 20 28 28 70 2d 3e 69 4a 44 20 2b Z = ((p->iJD +
7bee0 20 31 32 39 36 30 30 30 30 30 29 2f 38 36 34 30 129600000)/8640
7bef0 30 30 30 30 29 20 25 20 37 3b 0a 20 20 20 20 20 0000) % 7;.
7bf00 20 20 20 69 66 28 20 5a 3e 6e 20 29 20 5a 20 2d if( Z>n ) Z -
7bf10 3d 20 37 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e = 7;. p->
7bf20 69 4a 44 20 2b 3d 20 28 6e 20 2d 20 5a 29 2a 38 iJD += (n - Z)*8
7bf30 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 6400000;.
7bf40 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a clearYMD_HMS_TZ
7bf50 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 (p);. rc
7bf60 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 0;. }.
7bf70 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
7bf80 20 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b 0a case 's': {.
7bf90 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a /*. *
7bfa0 2a 20 20 20 20 73 74 61 72 74 20 6f 66 20 54 54 * start of TT
7bfb0 54 54 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 TTT. **.
7bfc0 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 ** Move the d
7bfd0 61 74 65 20 62 61 63 6b 77 61 72 64 73 20 74 6f ate backwards to
7bfe0 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
7bff0 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 f the current da
7c000 79 2c 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d y,. ** or m
7c010 6f 6e 74 68 20 6f 72 20 79 65 61 72 2e 0a 20 20 onth or year..
7c020 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
7c030 20 73 74 72 6e 63 6d 70 28 7a 2c 20 22 73 74 61 strncmp(z, "sta
7c040 72 74 20 6f 66 20 22 2c 20 39 29 21 3d 30 20 29 rt of ", 9)!=0 )
7c050 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7a 20 break;. z
7c060 2b 3d 20 39 3b 0a 20 20 20 20 20 20 63 6f 6d 70 += 9;. comp
7c070 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 uteYMD(p);.
7c080 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 p->validHMS = 1
7c090 3b 0a 20 20 20 20 20 20 70 2d 3e 68 20 3d 20 70 ;. p->h = p
7c0a0 2d 3e 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 ->m = 0;. p
7c0b0 2d 3e 73 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20 ->s = 0.0;.
7c0c0 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b p->validTZ = 0;
7c0d0 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a . p->validJ
7c0e0 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 D = 0;. if(
7c0f0 20 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 strcmp(z,"month
7c100 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ")==0 ){.
7c110 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20 20 p->D = 1;.
7c120 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 rc = 0;.
7c130 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d }else if( strcm
7c140 70 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 p(z,"year")==0 )
7c150 7b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 {. comput
7c160 65 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20 20 eYMD(p);.
7c170 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 p->M = 1;.
7c180 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 p->D = 1;.
7c190 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 rc = 0;.
7c1a0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 }else if( str
7c1b0 63 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 cmp(z,"day")==0
7c1c0 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
7c1d0 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 0;. }.
7c1e0 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
7c1f0 20 20 63 61 73 65 20 27 2b 27 3a 0a 20 20 20 20 case '+':.
7c200 63 61 73 65 20 27 2d 27 3a 0a 20 20 20 20 63 61 case '-':. ca
7c210 73 65 20 27 30 27 3a 0a 20 20 20 20 63 61 73 65 se '0':. case
7c220 20 27 31 27 3a 0a 20 20 20 20 63 61 73 65 20 27 '1':. case '
7c230 32 27 3a 0a 20 20 20 20 63 61 73 65 20 27 33 27 2':. case '3'
7c240 3a 0a 20 20 20 20 63 61 73 65 20 27 34 27 3a 0a :. case '4':.
7c250 20 20 20 20 63 61 73 65 20 27 35 27 3a 0a 20 20 case '5':.
7c260 20 20 63 61 73 65 20 27 36 27 3a 0a 20 20 20 20 case '6':.
7c270 63 61 73 65 20 27 37 27 3a 0a 20 20 20 20 63 61 case '7':. ca
7c280 73 65 20 27 38 27 3a 0a 20 20 20 20 63 61 73 65 se '8':. case
7c290 20 27 39 27 3a 20 7b 0a 20 20 20 20 20 20 64 6f '9': {. do
7c2a0 75 62 6c 65 20 72 52 6f 75 6e 64 65 72 3b 0a 20 uble rRounder;.
7c2b0 20 20 20 20 20 6e 20 3d 20 67 65 74 56 61 6c 75 n = getValu
7c2c0 65 28 7a 2c 20 26 72 29 3b 0a 20 20 20 20 20 20 e(z, &r);.
7c2d0 61 73 73 65 72 74 28 20 6e 3e 3d 31 20 29 3b 0a assert( n>=1 );.
7c2e0 20 20 20 20 20 20 69 66 28 20 7a 5b 6e 5d 3d 3d if( z[n]==
7c2f0 27 3a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ':' ){. /
7c300 2a 20 41 20 6d 6f 64 69 66 69 65 72 20 6f 66 20 * A modifier of
7c310 74 68 65 20 66 6f 72 6d 20 28 2b 7c 2d 29 48 48 the form (+|-)HH
7c320 3a 4d 4d 3a 53 53 2e 46 46 46 20 61 64 64 73 20 :MM:SS.FFF adds
7c330 28 6f 72 20 73 75 62 74 72 61 63 74 73 29 20 74 (or subtracts) t
7c340 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 70 he. ** sp
7c350 65 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f ecified number o
7c360 66 20 68 6f 75 72 73 2c 20 6d 69 6e 75 74 65 73 f hours, minutes
7c370 2c 20 73 65 63 6f 6e 64 73 2c 20 61 6e 64 20 66 , seconds, and f
7c380 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 ractional second
7c390 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 s. ** to
7c3a0 74 68 65 20 74 69 6d 65 2e 20 20 54 68 65 20 22 the time. The "
7c3b0 2e 46 46 46 22 20 6d 61 79 20 62 65 20 6f 6d 69 .FFF" may be omi
7c3c0 74 74 65 64 2e 20 20 54 68 65 20 22 3a 53 53 2e tted. The ":SS.
7c3d0 46 46 46 22 20 6d 61 79 20 62 65 0a 20 20 20 20 FFF" may be.
7c3e0 20 20 20 20 2a 2a 20 6f 6d 69 74 74 65 64 2e 0a ** omitted..
7c3f0 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
7c400 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
7c410 32 20 3d 20 7a 3b 0a 20 20 20 20 20 20 20 20 44 2 = z;. D
7c420 61 74 65 54 69 6d 65 20 74 78 3b 0a 20 20 20 20 ateTime tx;.
7c430 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 sqlite3_int6
7c440 34 20 64 61 79 3b 0a 20 20 20 20 20 20 20 20 69 4 day;. i
7c450 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 f( !sqlite3Isdig
7c460 69 74 28 2a 7a 32 29 20 29 20 7a 32 2b 2b 3b 0a it(*z2) ) z2++;.
7c470 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 memset(&
7c480 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 74 78 tx, 0, sizeof(tx
7c490 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ));. if(
7c4a0 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 32 2c 20 parseHhMmSs(z2,
7c4b0 26 74 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 &tx) ) break;.
7c4c0 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 computeJD(
7c4d0 26 74 78 29 3b 0a 20 20 20 20 20 20 20 20 74 78 &tx);. tx
7c4e0 2e 69 4a 44 20 2d 3d 20 34 33 32 30 30 30 30 30 .iJD -= 43200000
7c4f0 3b 0a 20 20 20 20 20 20 20 20 64 61 79 20 3d 20 ;. day =
7c500 74 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 3b tx.iJD/86400000;
7c510 0a 20 20 20 20 20 20 20 20 74 78 2e 69 4a 44 20 . tx.iJD
7c520 2d 3d 20 64 61 79 2a 38 36 34 30 30 30 30 30 3b -= day*86400000;
7c530 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 30 . if( z[0
7c540 5d 3d 3d 27 2d 27 20 29 20 74 78 2e 69 4a 44 20 ]=='-' ) tx.iJD
7c550 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 = -tx.iJD;.
7c560 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b computeJD(p);
7c570 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d . clearYM
7c580 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 D_HMS_TZ(p);.
7c590 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 74 p->iJD += t
7c5a0 78 2e 69 4a 44 3b 0a 20 20 20 20 20 20 20 20 72 x.iJD;. r
7c5b0 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 c = 0;. b
7c5c0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
7c5d0 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 20 20 z += n;.
7c5e0 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 while( sqlite3
7c5f0 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b Isspace(*z) ) z+
7c600 2b 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c +;. n = sql
7c610 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b ite3Strlen30(z);
7c620 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 31 30 20 . if( n>10
7c630 7c 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b 3b 0a || n<3 ) break;.
7c640 20 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d 31 5d if( z[n-1]
7c650 3d 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31 5d 20 =='s' ){ z[n-1]
7c660 3d 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20 20 20 = 0; n--; }.
7c670 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a computeJD(p);.
7c680 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 rc = 0;.
7c690 20 20 20 20 72 52 6f 75 6e 64 65 72 20 3d 20 72 rRounder = r
7c6a0 3c 30 20 3f 20 2d 30 2e 35 20 3a 20 2b 30 2e 35 <0 ? -0.5 : +0.5
7c6b0 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3d 3d 33 ;. if( n==3
7c6c0 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 && strcmp(z,"da
7c6d0 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 y")==0 ){.
7c6e0 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c p->iJD += (sql
7c6f0 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 ite3_int64)(r*86
7c700 34 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 400000.0 + rRoun
7c710 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 der);. }els
7c720 65 20 69 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 e if( n==4 && st
7c730 72 63 6d 70 28 7a 2c 22 68 6f 75 72 22 29 3d 3d rcmp(z,"hour")==
7c740 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 0 ){. p->
7c750 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f iJD += (sqlite3_
7c760 69 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 int64)(r*(864000
7c770 30 30 2e 30 2f 32 34 2e 30 29 20 2b 20 72 52 6f 00.0/24.0) + rRo
7c780 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 under);. }e
7c790 6c 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 lse if( n==6 &&
7c7a0 73 74 72 63 6d 70 28 7a 2c 22 6d 69 6e 75 74 65 strcmp(z,"minute
7c7b0 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ")==0 ){.
7c7c0 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 p->iJD += (sqli
7c7d0 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 te3_int64)(r*(86
7c7e0 34 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 400000.0/(24.0*6
7c7f0 30 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 0.0)) + rRounder
7c800 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 );. }else i
7c810 66 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d f( n==6 && strcm
7c820 70 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d 3d 30 p(z,"second")==0
7c830 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 ){. p->i
7c840 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 JD += (sqlite3_i
7c850 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30 nt64)(r*(8640000
7c860 30 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30 2a 36 0.0/(24.0*60.0*6
7c870 30 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 0.0)) + rRounder
7c880 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 );. }else i
7c890 66 28 20 6e 3d 3d 35 20 26 26 20 73 74 72 63 6d f( n==5 && strcm
7c8a0 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 p(z,"month")==0
7c8b0 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 ){. int x
7c8c0 2c 20 79 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d , y;. com
7c8d0 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a puteYMD_HMS(p);.
7c8e0 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 2b 3d 20 p->M +=
7c8f0 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 (int)r;.
7c900 78 20 3d 20 70 2d 3e 4d 3e 30 20 3f 20 28 70 2d x = p->M>0 ? (p-
7c910 3e 4d 2d 31 29 2f 31 32 20 3a 20 28 70 2d 3e 4d >M-1)/12 : (p->M
7c920 2d 31 32 29 2f 31 32 3b 0a 20 20 20 20 20 20 20 -12)/12;.
7c930 20 70 2d 3e 59 20 2b 3d 20 78 3b 0a 20 20 20 20 p->Y += x;.
7c940 20 20 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a 31 32 p->M -= x*12
7c950 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c ;. p->val
7c960 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 idJD = 0;.
7c970 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a computeJD(p);.
7c980 20 20 20 20 20 20 20 20 79 20 3d 20 28 69 6e 74 y = (int
7c990 29 72 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 )r;. if(
7c9a0 79 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 y!=r ){.
7c9b0 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c p->iJD += (sql
7c9c0 69 74 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d ite3_int64)((r -
7c9d0 20 79 29 2a 33 30 2e 30 2a 38 36 34 30 30 30 30 y)*30.0*8640000
7c9e0 30 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0.0 + rRounder);
7c9f0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
7ca00 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 }else if( n==4
7ca10 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61 && strcmp(z,"yea
7ca20 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 r")==0 ){.
7ca30 20 20 69 6e 74 20 79 20 3d 20 28 69 6e 74 29 72 int y = (int)r
7ca40 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 ;. comput
7ca50 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 eYMD_HMS(p);.
7ca60 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 79 3b 0a p->Y += y;.
7ca70 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 p->valid
7ca80 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 JD = 0;.
7ca90 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 computeJD(p);.
7caa0 20 20 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 if( y!=r )
7cab0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 {. p->i
7cac0 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 JD += (sqlite3_i
7cad0 6e 74 36 34 29 28 28 72 20 2d 20 79 29 2a 33 36 nt64)((r - y)*36
7cae0 35 2e 30 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 5.0*86400000.0 +
7caf0 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 rRounder);.
7cb00 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
7cb10 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 e{. rc =
7cb20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 1;. }.
7cb30 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a clearYMD_HMS_TZ
7cb40 28 70 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b (p);. break
7cb50 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
7cb60 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 65 ult: {. bre
7cb70 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ak;. }. }.
7cb80 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
7cb90 0a 2a 2a 20 50 72 6f 63 65 73 73 20 74 69 6d 65 .** Process time
7cba0 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 function argume
7cbb0 6e 74 73 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 nts. argv[0] is
7cbc0 20 61 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 a date-time sta
7cbd0 6d 70 2e 0a 2a 2a 20 61 72 67 76 5b 31 5d 20 61 mp..** argv[1] a
7cbe0 6e 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 nd following are
7cbf0 20 6d 6f 64 69 66 69 65 72 73 2e 20 20 50 61 72 modifiers. Par
7cc00 73 65 20 74 68 65 6d 20 61 6c 6c 20 61 6e 64 20 se them all and
7cc10 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 72 65 73 write.** the res
7cc20 75 6c 74 69 6e 67 20 74 69 6d 65 20 69 6e 74 6f ulting time into
7cc30 20 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 the DateTime st
7cc40 72 75 63 74 75 72 65 20 70 2e 20 20 52 65 74 75 ructure p. Retu
7cc50 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 rn 0.** on succe
7cc60 73 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 ss and 1 if ther
7cc70 65 20 61 72 65 20 61 6e 79 20 65 72 72 6f 72 73 e are any errors
7cc80 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 ..**.** If there
7cc90 20 61 72 65 20 7a 65 72 6f 20 70 61 72 61 6d 65 are zero parame
7cca0 74 65 72 73 20 28 69 66 20 65 76 65 6e 20 61 72 ters (if even ar
7ccb0 67 76 5b 30 5d 20 69 73 20 75 6e 64 65 66 69 6e gv[0] is undefin
7ccc0 65 64 29 0a 2a 2a 20 74 68 65 6e 20 61 73 73 75 ed).** then assu
7ccd0 6d 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c me a default val
7cce0 75 65 20 6f 66 20 22 6e 6f 77 22 20 66 6f 72 20 ue of "now" for
7ccf0 61 72 67 76 5b 30 5d 2e 0a 2a 2f 0a 73 74 61 74 argv[0]..*/.stat
7cd00 69 63 20 69 6e 74 20 69 73 44 61 74 65 28 0a 20 ic int isDate(.
7cd10 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
7cd20 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e *context, . in
7cd30 74 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 t argc, . sqlit
7cd40 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 2c e3_value **argv,
7cd50 20 0a 20 20 44 61 74 65 54 69 6d 65 20 2a 70 0a . DateTime *p.
7cd60 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f ){. int i;. co
7cd70 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
7cd80 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 65 54 79 70 r *z;. int eTyp
7cd90 65 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 e;. memset(p, 0
7cda0 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 , sizeof(*p));.
7cdb0 20 69 66 28 20 61 72 67 63 3d 3d 30 20 29 7b 0a if( argc==0 ){.
7cdc0 20 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 setDateTimeT
7cdd0 6f 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 oCurrent(context
7cde0 2c 20 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 , p);. }else if
7cdf0 28 20 28 65 54 79 70 65 20 3d 20 73 71 6c 69 74 ( (eType = sqlit
7ce00 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 e3_value_type(ar
7ce10 67 76 5b 30 5d 29 29 3d 3d 53 51 4c 49 54 45 5f gv[0]))==SQLITE_
7ce20 46 4c 4f 41 54 0a 20 20 20 20 20 20 20 20 20 20 FLOAT.
7ce30 20 20 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 || eTyp
7ce40 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 e==SQLITE_INTEGE
7ce50 52 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 R ){. p->iJD
7ce60 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 = (sqlite3_int64
7ce70 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f )(sqlite3_value_
7ce80 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 2a double(argv[0])*
7ce90 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 86400000.0 + 0.5
7cea0 29 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a );. p->validJ
7ceb0 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a D = 1;. }else{.
7cec0 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f z = sqlite3_
7ced0 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
7cee0 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 20 0]);. if( !z
7cef0 7c 7c 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 || parseDateOrTi
7cf00 6d 65 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 me(context, (cha
7cf10 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20 r*)z, p) ){.
7cf20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
7cf30 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 3b }. }. for(i=1;
7cf40 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 i<argc; i++){.
7cf50 20 20 20 69 66 28 20 28 7a 20 3d 20 73 71 6c 69 if( (z = sqli
7cf60 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
7cf70 72 67 76 5b 69 5d 29 29 3d 3d 30 20 7c 7c 20 70 rgv[i]))==0 || p
7cf80 61 72 73 65 4d 6f 64 69 66 69 65 72 28 28 63 68 arseModifier((ch
7cf90 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 ar*)z, p) ){.
7cfa0 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
7cfb0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
7cfc0 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 0;.}.../*.** The
7cfd0 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 following routi
7cfe0 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 nes implement th
7cff0 65 20 76 61 72 69 6f 75 73 20 64 61 74 65 20 61 e various date a
7d000 6e 64 20 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e nd time function
7d010 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a s.** of SQLite..
7d020 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 6a 75 6c */../*.** jul
7d030 69 61 6e 64 61 79 28 20 54 49 4d 45 53 54 52 49 ianday( TIMESTRI
7d040 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e NG, MOD, MOD, ..
7d050 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 .).**.** Return
7d060 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e the julian day n
7d070 75 6d 62 65 72 20 6f 66 20 74 68 65 20 64 61 74 umber of the dat
7d080 65 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 e specified in t
7d090 68 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2f 0a he arguments.*/.
7d0a0 73 74 61 74 69 63 20 76 6f 69 64 20 6a 75 6c 69 static void juli
7d0b0 61 6e 64 61 79 46 75 6e 63 28 0a 20 20 73 71 6c andayFunc(. sql
7d0c0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
7d0d0 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
7d0e0 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
7d0f0 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 ue **argv.){. D
7d100 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 ateTime x;. if(
7d110 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c isDate(context,
7d120 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 argc, argv, &x)
7d130 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6d 70 75 ==0 ){. compu
7d140 74 65 4a 44 28 26 78 29 3b 0a 20 20 20 20 73 71 teJD(&x);. sq
7d150 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 lite3_result_dou
7d160 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 78 2e 69 ble(context, x.i
7d170 4a 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a JD/86400000.0);.
7d180 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 }.}../*.**
7d190 64 61 74 65 74 69 6d 65 28 20 54 49 4d 45 53 54 datetime( TIMEST
7d1a0 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 RING, MOD, MOD,
7d1b0 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ...).**.** Retur
7d1c0 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a n YYYY-MM-DD HH:
7d1d0 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20 MM:SS.*/.static
7d1e0 76 6f 69 64 20 64 61 74 65 74 69 6d 65 46 75 6e void datetimeFun
7d1f0 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c(. sqlite3_con
7d200 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
7d210 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c int argc,. sql
7d220 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
7d230 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 v.){. DateTime
7d240 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 x;. if( isDate(
7d250 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 context, argc, a
7d260 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 rgv, &x)==0 ){.
7d270 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 char zBuf[100
7d280 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d ];. computeYM
7d290 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 D_HMS(&x);. s
7d2a0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
7d2b0 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 sizeof(zBuf), zB
7d2c0 75 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d 25 uf, "%04d-%02d-%
7d2d0 30 32 64 20 25 30 32 64 3a 25 30 32 64 3a 25 30 02d %02d:%02d:%0
7d2e0 32 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 2d",.
7d2f0 20 20 20 20 20 20 20 20 20 20 78 2e 59 2c 20 78 x.Y, x
7d300 2e 4d 2c 20 78 2e 44 2c 20 78 2e 68 2c 20 78 2e .M, x.D, x.h, x.
7d310 6d 2c 20 28 69 6e 74 29 28 78 2e 73 29 29 3b 0a m, (int)(x.s));.
7d320 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
7d330 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c lt_text(context,
7d340 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 zBuf, -1, SQLIT
7d350 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 E_TRANSIENT);.
7d360 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 74 69 }.}../*.** ti
7d370 6d 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 me( TIMESTRING,
7d380 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a MOD, MOD, ...).*
7d390 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 48 48 3a 4d *.** Return HH:M
7d3a0 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 M:SS.*/.static v
7d3b0 6f 69 64 20 74 69 6d 65 46 75 6e 63 28 0a 20 20 oid timeFunc(.
7d3c0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
7d3d0 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 *context,. int
7d3e0 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f argc,. sqlite3_
7d3f0 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a value **argv.){.
7d400 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 DateTime x;.
7d410 69 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 if( isDate(conte
7d420 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 xt, argc, argv,
7d430 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 &x)==0 ){. ch
7d440 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 ar zBuf[100];.
7d450 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 26 78 29 computeHMS(&x)
7d460 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e ;. sqlite3_sn
7d470 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 printf(sizeof(zB
7d480 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 32 64 uf), zBuf, "%02d
7d490 3a 25 30 32 64 3a 25 30 32 64 22 2c 20 78 2e 68 :%02d:%02d", x.h
7d4a0 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 78 2e 73 29 , x.m, (int)x.s)
7d4b0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 ;. sqlite3_re
7d4c0 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 sult_text(contex
7d4d0 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c t, zBuf, -1, SQL
7d4e0 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a ITE_TRANSIENT);.
7d4f0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 }.}../*.**
7d500 64 61 74 65 28 20 54 49 4d 45 53 54 52 49 4e 47 date( TIMESTRING
7d510 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 , MOD, MOD, ...)
7d520 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 59 59 .**.** Return YY
7d530 59 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a 73 74 61 74 YY-MM-DD.*/.stat
7d540 69 63 20 76 6f 69 64 20 64 61 74 65 46 75 6e 63 ic void dateFunc
7d550 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
7d560 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
7d570 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 int argc,. sqli
7d580 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
7d590 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 .){. DateTime x
7d5a0 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63 ;. if( isDate(c
7d5b0 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 ontext, argc, ar
7d5c0 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 gv, &x)==0 ){.
7d5d0 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d char zBuf[100]
7d5e0 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 ;. computeYMD
7d5f0 28 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 (&x);. sqlite
7d600 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 3_snprintf(sizeo
7d610 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 f(zBuf), zBuf, "
7d620 25 30 34 64 2d 25 30 32 64 2d 25 30 32 64 22 2c %04d-%02d-%02d",
7d630 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 29 3b x.Y, x.M, x.D);
7d640 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 . sqlite3_res
7d650 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
7d660 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 , zBuf, -1, SQLI
7d670 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 TE_TRANSIENT);.
7d680 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 73 }.}../*.** s
7d690 74 72 66 74 69 6d 65 28 20 46 4f 52 4d 41 54 2c trftime( FORMAT,
7d6a0 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 TIMESTRING, MOD
7d6b0 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a , MOD, ...).**.*
7d6c0 2a 20 52 65 74 75 72 6e 20 61 20 73 74 72 69 6e * Return a strin
7d6d0 67 20 64 65 73 63 72 69 62 65 64 20 62 79 20 46 g described by F
7d6e0 4f 52 4d 41 54 2e 20 20 43 6f 6e 76 65 72 73 69 ORMAT. Conversi
7d6f0 6f 6e 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a ons as follows:.
7d700 2a 2a 0a 2a 2a 20 20 20 25 64 20 20 64 61 79 20 **.** %d day
7d710 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 25 66 of month.** %f
7d720 20 20 2a 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 ** fractional
7d730 73 65 63 6f 6e 64 73 20 20 53 53 2e 53 53 53 0a seconds SS.SSS.
7d740 2a 2a 20 20 20 25 48 20 20 68 6f 75 72 20 30 30 ** %H hour 00
7d750 2d 32 34 0a 2a 2a 20 20 20 25 6a 20 20 64 61 79 -24.** %j day
7d760 20 6f 66 20 79 65 61 72 20 30 30 30 2d 33 36 36 of year 000-366
7d770 0a 2a 2a 20 20 20 25 4a 20 20 2a 2a 20 4a 75 6c .** %J ** Jul
7d780 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 0a 2a ian day number.*
7d790 2a 20 20 20 25 6d 20 20 6d 6f 6e 74 68 20 30 31 * %m month 01
7d7a0 2d 31 32 0a 2a 2a 20 20 20 25 4d 20 20 6d 69 6e -12.** %M min
7d7b0 75 74 65 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25 ute 00-59.** %
7d7c0 73 20 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 s seconds since
7d7d0 20 31 39 37 30 2d 30 31 2d 30 31 0a 2a 2a 20 20 1970-01-01.**
7d7e0 20 25 53 20 20 73 65 63 6f 6e 64 73 20 30 30 2d %S seconds 00-
7d7f0 35 39 0a 2a 2a 20 20 20 25 77 20 20 64 61 79 20 59.** %w day
7d800 6f 66 20 77 65 65 6b 20 30 2d 36 20 20 73 75 6e of week 0-6 sun
7d810 64 61 79 3d 3d 30 0a 2a 2a 20 20 20 25 57 20 20 day==0.** %W
7d820 77 65 65 6b 20 6f 66 20 79 65 61 72 20 30 30 2d week of year 00-
7d830 35 33 0a 2a 2a 20 20 20 25 59 20 20 79 65 61 72 53.** %Y year
7d840 20 30 30 30 30 2d 39 39 39 39 0a 2a 2a 20 20 20 0000-9999.**
7d850 25 25 20 20 25 0a 2a 2f 0a 73 74 61 74 69 63 20 %% %.*/.static
7d860 76 6f 69 64 20 73 74 72 66 74 69 6d 65 46 75 6e void strftimeFun
7d870 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c(. sqlite3_con
7d880 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
7d890 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c int argc,. sql
7d8a0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
7d8b0 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 v.){. DateTime
7d8c0 78 3b 0a 20 20 75 36 34 20 6e 3b 0a 20 20 73 69 x;. u64 n;. si
7d8d0 7a 65 5f 74 20 69 2c 6a 3b 0a 20 20 63 68 61 72 ze_t i,j;. char
7d8e0 20 2a 7a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a *z;. sqlite3 *
7d8f0 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 db;. const char
7d900 20 2a 7a 46 6d 74 20 3d 20 28 63 6f 6e 73 74 20 *zFmt = (const
7d910 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 char*)sqlite3_va
7d920 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d lue_text(argv[0]
7d930 29 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 31 );. char zBuf[1
7d940 30 30 5d 3b 0a 20 20 69 66 28 20 7a 46 6d 74 3d 00];. if( zFmt=
7d950 3d 30 20 7c 7c 20 69 73 44 61 74 65 28 63 6f 6e =0 || isDate(con
7d960 74 65 78 74 2c 20 61 72 67 63 2d 31 2c 20 61 72 text, argc-1, ar
7d970 67 76 2b 31 2c 20 26 78 29 20 29 20 72 65 74 75 gv+1, &x) ) retu
7d980 72 6e 3b 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 rn;. db = sqlit
7d990 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 e3_context_db_ha
7d9a0 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 ndle(context);.
7d9b0 20 66 6f 72 28 69 3d 30 2c 20 6e 3d 31 3b 20 7a for(i=0, n=1; z
7d9c0 46 6d 74 5b 69 5d 3b 20 69 2b 2b 2c 20 6e 2b 2b Fmt[i]; i++, n++
7d9d0 29 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b ){. if( zFmt[
7d9e0 69 5d 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 i]=='%' ){.
7d9f0 20 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 2b switch( zFmt[i+
7da00 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 1] ){. ca
7da10 73 65 20 27 64 27 3a 0a 20 20 20 20 20 20 20 20 se 'd':.
7da20 63 61 73 65 20 27 48 27 3a 0a 20 20 20 20 20 20 case 'H':.
7da30 20 20 63 61 73 65 20 27 6d 27 3a 0a 20 20 20 20 case 'm':.
7da40 20 20 20 20 63 61 73 65 20 27 4d 27 3a 0a 20 20 case 'M':.
7da50 20 20 20 20 20 20 63 61 73 65 20 27 53 27 3a 0a case 'S':.
7da60 20 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27 case 'W'
7da70 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 2b 2b 3b :. n++;
7da80 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 61 . /* fa
7da90 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 ll thru */.
7daa0 20 20 20 63 61 73 65 20 27 77 27 3a 0a 20 20 20 case 'w':.
7dab0 20 20 20 20 20 63 61 73 65 20 27 25 27 3a 0a 20 case '%':.
7dac0 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
7dad0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 66 27 case 'f'
7dae0 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d :. n +=
7daf0 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 8;. br
7db00 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
7db10 65 20 27 6a 27 3a 0a 20 20 20 20 20 20 20 20 20 e 'j':.
7db20 20 6e 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 n += 3;.
7db30 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
7db40 20 20 63 61 73 65 20 27 59 27 3a 0a 20 20 20 20 case 'Y':.
7db50 20 20 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 n += 8;.
7db60 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
7db70 20 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a case 's':
7db80 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4a . case 'J
7db90 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b ':. n +
7dba0 3d 20 35 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 50;.
7dbb0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 break;. d
7dbc0 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 efault:.
7dbd0 20 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45 52 return; /* ER
7dbe0 52 4f 52 2e 20 20 72 65 74 75 72 6e 20 61 20 4e ROR. return a N
7dbf0 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 ULL */. }.
7dc00 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d 0a i++;. }.
7dc10 20 20 7d 0a 20 20 74 65 73 74 63 61 73 65 28 20 }. testcase(
7dc20 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d n==sizeof(zBuf)-
7dc30 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 1 );. testcase(
7dc40 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 n==sizeof(zBuf)
7dc50 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
7dc60 6e 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d n==(u64)db->aLim
7dc70 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
7dc80 4c 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20 74 LENGTH]+1 );. t
7dc90 65 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 36 34 estcase( n==(u64
7dca0 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 )db->aLimit[SQLI
7dcb0 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
7dcc0 20 29 3b 0a 20 20 69 66 28 20 6e 3c 73 69 7a 65 );. if( n<size
7dcd0 6f 66 28 7a 42 75 66 29 20 29 7b 0a 20 20 20 20 of(zBuf) ){.
7dce0 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 7d 65 6c 73 z = zBuf;. }els
7dcf0 65 20 69 66 28 20 6e 3e 28 75 36 34 29 64 62 2d e if( n>(u64)db-
7dd00 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
7dd10 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a IMIT_LENGTH] ){.
7dd20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
7dd30 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 lt_error_toobig(
7dd40 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 72 65 context);. re
7dd50 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 turn;. }else{.
7dd60 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 z = sqlite3Db
7dd70 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 28 69 MallocRaw(db, (i
7dd80 6e 74 29 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a nt)n);. if( z
7dd90 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==0 ){. sql
7dda0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
7ddb0 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 r_nomem(context)
7ddc0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a ;. return;.
7ddd0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6d 70 }. }. comp
7dde0 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 63 6f 6d uteJD(&x);. com
7ddf0 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b puteYMD_HMS(&x);
7de00 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 7a 46 . for(i=j=0; zF
7de10 6d 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 mt[i]; i++){.
7de20 20 69 66 28 20 7a 46 6d 74 5b 69 5d 21 3d 27 25 if( zFmt[i]!='%
7de30 27 20 29 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b ' ){. z[j++
7de40 5d 20 3d 20 7a 46 6d 74 5b 69 5d 3b 0a 20 20 20 ] = zFmt[i];.
7de50 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 2b }else{. i+
7de60 2b 3b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 +;. switch(
7de70 20 7a 46 6d 74 5b 69 5d 20 29 7b 0a 20 20 20 20 zFmt[i] ){.
7de80 20 20 20 20 63 61 73 65 20 27 64 27 3a 20 20 73 case 'd': s
7de90 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
7dea0 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 3, &z[j],"%02d",
7deb0 78 2e 44 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 x.D); j+=2; brea
7dec0 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 k;. case
7ded0 27 66 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 'f': {.
7dee0 20 64 6f 75 62 6c 65 20 73 20 3d 20 78 2e 73 3b double s = x.s;
7def0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 . if( s
7df00 3e 35 39 2e 39 39 39 20 29 20 73 20 3d 20 35 39 >59.999 ) s = 59
7df10 2e 39 39 39 3b 0a 20 20 20 20 20 20 20 20 20 20 .999;.
7df20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
7df30 28 37 2c 20 26 7a 5b 6a 5d 2c 22 25 30 36 2e 33 (7, &z[j],"%06.3
7df40 66 22 2c 20 73 29 3b 0a 20 20 20 20 20 20 20 20 f", s);.
7df50 20 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 j += sqlite3St
7df60 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 rlen30(&z[j]);.
7df70 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
7df80 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
7df90 20 20 63 61 73 65 20 27 48 27 3a 20 20 73 71 6c case 'H': sql
7dfa0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c ite3_snprintf(3,
7dfb0 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e &z[j],"%02d",x.
7dfc0 68 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b h); j+=2; break;
7dfd0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 57 . case 'W
7dfe0 27 3a 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 ': /* Fall thru
7dff0 2a 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 */. case
7e000 27 6a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 'j': {.
7e010 20 69 6e 74 20 6e 44 61 79 3b 20 20 20 20 20 20 int nDay;
7e020 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
7e030 20 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20 31 of days since 1
7e040 73 74 20 64 61 79 20 6f 66 20 79 65 61 72 20 2a st day of year *
7e050 2f 0a 20 20 20 20 20 20 20 20 20 20 44 61 74 65 /. Date
7e060 54 69 6d 65 20 79 20 3d 20 78 3b 0a 20 20 20 20 Time y = x;.
7e070 20 20 20 20 20 20 79 2e 76 61 6c 69 64 4a 44 20 y.validJD
7e080 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 79 = 0;. y
7e090 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 .M = 1;.
7e0a0 20 20 79 2e 44 20 3d 20 31 3b 0a 20 20 20 20 20 y.D = 1;.
7e0b0 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 computeJD(&
7e0c0 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 44 y);. nD
7e0d0 61 79 20 3d 20 28 69 6e 74 29 28 28 78 2e 69 4a ay = (int)((x.iJ
7e0e0 44 2d 79 2e 69 4a 44 2b 34 33 32 30 30 30 30 30 D-y.iJD+43200000
7e0f0 29 2f 38 36 34 30 30 30 30 30 29 3b 0a 20 20 20 )/86400000);.
7e100 20 20 20 20 20 20 20 69 66 28 20 7a 46 6d 74 5b if( zFmt[
7e110 69 5d 3d 3d 27 57 27 20 29 7b 0a 20 20 20 20 20 i]=='W' ){.
7e120 20 20 20 20 20 20 20 69 6e 74 20 77 64 3b 20 20 int wd;
7e130 20 2f 2a 20 30 3d 4d 6f 6e 64 61 79 2c 20 31 3d /* 0=Monday, 1=
7e140 54 75 65 73 64 61 79 2c 20 2e 2e 2e 20 36 3d 53 Tuesday, ... 6=S
7e150 75 6e 64 61 79 20 2a 2f 0a 20 20 20 20 20 20 20 unday */.
7e160 20 20 20 20 20 77 64 20 3d 20 28 69 6e 74 29 28 wd = (int)(
7e170 28 28 78 2e 69 4a 44 2b 34 33 32 30 30 30 30 30 ((x.iJD+43200000
7e180 29 2f 38 36 34 30 30 30 30 30 29 25 37 29 3b 0a )/86400000)%7);.
7e190 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
7e1a0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 te3_snprintf(3,
7e1b0 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 6e 44 &z[j],"%02d",(nD
7e1c0 61 79 2b 37 2d 77 64 29 2f 37 29 3b 0a 20 20 20 ay+7-wd)/7);.
7e1d0 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 32 3b j += 2;
7e1e0 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
7e1f0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 {. sq
7e200 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 34 lite3_snprintf(4
7e210 2c 20 26 7a 5b 6a 5d 2c 22 25 30 33 64 22 2c 6e , &z[j],"%03d",n
7e220 44 61 79 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 Day+1);.
7e230 20 20 20 20 6a 20 2b 3d 20 33 3b 0a 20 20 20 20 j += 3;.
7e240 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
7e250 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
7e260 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 }. case
7e270 27 4a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 'J': {.
7e280 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
7e290 66 28 32 30 2c 20 26 7a 5b 6a 5d 2c 22 25 2e 31 f(20, &z[j],"%.1
7e2a0 36 67 22 2c 78 2e 69 4a 44 2f 38 36 34 30 30 30 6g",x.iJD/864000
7e2b0 30 30 2e 30 29 3b 0a 20 20 20 20 20 20 20 20 20 00.0);.
7e2c0 20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 j+=sqlite3Strle
7e2d0 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 n30(&z[j]);.
7e2e0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
7e2f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 }. c
7e300 61 73 65 20 27 6d 27 3a 20 20 73 71 6c 69 74 65 ase 'm': sqlite
7e310 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 3_snprintf(3, &z
7e320 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 4d 29 3b [j],"%02d",x.M);
7e330 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 j+=2; break;.
7e340 20 20 20 20 20 20 63 61 73 65 20 27 4d 27 3a 20 case 'M':
7e350 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
7e360 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 f(3, &z[j],"%02d
7e370 22 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 3b 20 62 72 ",x.m); j+=2; br
7e380 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
7e390 65 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 20 e 's': {.
7e3a0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
7e3b0 6e 74 66 28 33 30 2c 26 7a 5b 6a 5d 2c 22 25 6c ntf(30,&z[j],"%l
7e3c0 6c 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ld",.
7e3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7e3e0 28 69 36 34 29 28 78 2e 69 4a 44 2f 31 30 30 30 (i64)(x.iJD/1000
7e3f0 20 2d 20 32 31 30 38 36 36 37 36 2a 28 69 36 34 - 21086676*(i64
7e400 29 31 30 30 30 30 29 29 3b 0a 20 20 20 20 20 20 )10000));.
7e410 20 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33 j += sqlite3
7e420 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b Strlen30(&z[j]);
7e430 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
7e440 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
7e450 20 20 20 20 63 61 73 65 20 27 53 27 3a 20 20 73 case 'S': s
7e460 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
7e470 33 2c 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 3,&z[j],"%02d",(
7e480 69 6e 74 29 78 2e 73 29 3b 20 6a 2b 3d 32 3b 20 int)x.s); j+=2;
7e490 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
7e4a0 61 73 65 20 27 77 27 3a 20 7b 0a 20 20 20 20 20 ase 'w': {.
7e4b0 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 28 63 z[j++] = (c
7e4c0 68 61 72 29 28 28 28 78 2e 69 4a 44 2b 31 32 39 har)(((x.iJD+129
7e4d0 36 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 600000)/86400000
7e4e0 29 20 25 20 37 29 20 2b 20 27 30 27 3b 0a 20 20 ) % 7) + '0';.
7e4f0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
7e500 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
7e510 20 63 61 73 65 20 27 59 27 3a 20 7b 0a 20 20 20 case 'Y': {.
7e520 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 sqlite3_s
7e530 6e 70 72 69 6e 74 66 28 35 2c 26 7a 5b 6a 5d 2c nprintf(5,&z[j],
7e540 22 25 30 34 64 22 2c 78 2e 59 29 3b 20 6a 2b 3d "%04d",x.Y); j+=
7e550 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
7e560 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 &z[j]);.
7e570 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
7e580 20 7d 0a 20 20 20 20 20 20 20 20 64 65 66 61 75 }. defau
7e590 6c 74 3a 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 lt: z[j++] = '
7e5a0 25 27 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 %'; break;.
7e5b0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 7a }. }. }. z
7e5c0 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 [j] = 0;. sqlit
7e5d0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 e3_result_text(c
7e5e0 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c 0a 20 ontext, z, -1,.
7e5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7e600 20 20 20 20 20 7a 3d 3d 7a 42 75 66 20 3f 20 53 z==zBuf ? S
7e610 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 QLITE_TRANSIENT
7e620 3a 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 : SQLITE_DYNAMIC
7e630 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 );.}../*.** curr
7e640 65 6e 74 5f 74 69 6d 65 28 29 0a 2a 2a 0a 2a 2a ent_time().**.**
7e650 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 This function r
7e660 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 eturns the same
7e670 76 61 6c 75 65 20 61 73 20 74 69 6d 65 28 27 6e value as time('n
7e680 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ow')..*/.static
7e690 76 6f 69 64 20 63 74 69 6d 65 46 75 6e 63 28 0a void ctimeFunc(.
7e6a0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
7e6b0 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
7e6c0 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c t NotUsed,. sql
7e6d0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 ite3_value **Not
7e6e0 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 Used2.){. UNUSE
7e6f0 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 D_PARAMETER2(Not
7e700 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b Used, NotUsed2);
7e710 0a 20 20 74 69 6d 65 46 75 6e 63 28 63 6f 6e 74 . timeFunc(cont
7e720 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f ext, 0, 0);.}../
7e730 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 64 61 74 *.** current_dat
7e740 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 e().**.** This f
7e750 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
7e760 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 the same value a
7e770 73 20 64 61 74 65 28 27 6e 6f 77 27 29 2e 0a 2a s date('now')..*
7e780 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 64 /.static void cd
7e790 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 ateFunc(. sqlit
7e7a0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
7e7b0 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 ext,. int NotUs
7e7c0 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 ed,. sqlite3_va
7e7d0 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 lue **NotUsed2.)
7e7e0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
7e7f0 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e ETER2(NotUsed, N
7e800 6f 74 55 73 65 64 32 29 3b 0a 20 20 64 61 74 65 otUsed2);. date
7e810 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c Func(context, 0,
7e820 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 0);.}../*.** cu
7e830 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 rrent_timestamp(
7e840 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ).**.** This fun
7e850 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 ction returns th
7e860 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20 e same value as
7e870 64 61 74 65 74 69 6d 65 28 27 6e 6f 77 27 29 2e datetime('now').
7e880 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
7e890 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 63 28 0a ctimestampFunc(.
7e8a0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
7e8b0 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
7e8c0 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c t NotUsed,. sql
7e8d0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 ite3_value **Not
7e8e0 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 Used2.){. UNUSE
7e8f0 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 D_PARAMETER2(Not
7e900 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b Used, NotUsed2);
7e910 0a 20 20 64 61 74 65 74 69 6d 65 46 75 6e 63 28 . datetimeFunc(
7e920 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a context, 0, 0);.
7e930 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 }.#endif /* !def
7e940 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
7e950 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 29 _DATETIME_FUNCS)
7e960 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 */..#ifdef SQLI
7e970 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 TE_OMIT_DATETIME
7e980 5f 46 55 4e 43 53 0a 2f 2a 0a 2a 2a 20 49 66 20 _FUNCS./*.** If
7e990 74 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 the library is c
7e9a0 6f 6d 70 69 6c 65 64 20 74 6f 20 6f 6d 69 74 20 ompiled to omit
7e9b0 74 68 65 20 66 75 6c 6c 2d 73 63 61 6c 65 20 64 the full-scale d
7e9c0 61 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 ate and time.**
7e9d0 68 61 6e 64 6c 69 6e 67 20 28 74 6f 20 67 65 74 handling (to get
7e9e0 20 61 20 73 6d 61 6c 6c 65 72 20 62 69 6e 61 72 a smaller binar
7e9f0 79 29 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e y), the followin
7ea00 67 20 6d 69 6e 69 6d 61 6c 20 76 65 72 73 69 6f g minimal versio
7ea10 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 66 75 6e 63 n.** of the func
7ea20 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 5f 74 69 tions current_ti
7ea30 6d 65 28 29 2c 20 63 75 72 72 65 6e 74 5f 64 61 me(), current_da
7ea40 74 65 28 29 20 61 6e 64 20 63 75 72 72 65 6e 74 te() and current
7ea50 5f 74 69 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 20 _timestamp().**
7ea60 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 73 are included ins
7ea70 74 65 61 64 2e 20 54 68 69 73 20 69 73 20 74 6f tead. This is to
7ea80 20 73 75 70 70 6f 72 74 20 63 6f 6c 75 6d 6e 20 support column
7ea90 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 74 68 61 declarations tha
7eaa0 74 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 22 44 45 t.** include "DE
7eab0 46 41 55 4c 54 20 43 55 52 52 45 4e 54 5f 54 49 FAULT CURRENT_TI
7eac0 4d 45 22 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 54 ME" etc..**.** T
7ead0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 his function use
7eae0 73 20 74 68 65 20 43 2d 6c 69 62 72 61 72 79 20 s the C-library
7eaf0 66 75 6e 63 74 69 6f 6e 73 20 74 69 6d 65 28 29 functions time()
7eb00 2c 20 67 6d 74 69 6d 65 28 29 0a 2a 2a 20 61 6e , gmtime().** an
7eb10 64 20 73 74 72 66 74 69 6d 65 28 29 2e 20 54 68 d strftime(). Th
7eb20 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 e format string
7eb30 74 6f 20 70 61 73 73 20 74 6f 20 73 74 72 66 74 to pass to strft
7eb40 69 6d 65 28 29 20 69 73 20 73 75 70 70 6c 69 65 ime() is supplie
7eb50 64 0a 2a 2a 20 61 73 20 74 68 65 20 75 73 65 72 d.** as the user
7eb60 2d 64 61 74 61 20 66 6f 72 20 74 68 65 20 66 75 -data for the fu
7eb70 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 nction..*/.stati
7eb80 63 20 76 6f 69 64 20 63 75 72 72 65 6e 74 54 69 c void currentTi
7eb90 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 meFunc(. sqlite
7eba0 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
7ebb0 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a xt,. int argc,.
7ebc0 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
7ebd0 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 74 69 6d 65 **argv.){. time
7ebe0 5f 74 20 74 3b 0a 20 20 63 68 61 72 20 2a 7a 46 _t t;. char *zF
7ebf0 6f 72 6d 61 74 20 3d 20 28 63 68 61 72 20 2a 29 ormat = (char *)
7ec00 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 sqlite3_user_dat
7ec10 61 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 a(context);. sq
7ec20 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 64 6f 75 lite3 *db;. dou
7ec30 62 6c 65 20 72 54 3b 0a 20 20 63 68 61 72 20 7a ble rT;. char z
7ec40 42 75 66 5b 32 30 5d 3b 0a 0a 20 20 55 4e 55 53 Buf[20];.. UNUS
7ec50 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 ED_PARAMETER(arg
7ec60 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 c);. UNUSED_PAR
7ec70 41 4d 45 54 45 52 28 61 72 67 76 29 3b 0a 0a 20 AMETER(argv);..
7ec80 20 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f db = sqlite3_co
7ec90 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 ntext_db_handle(
7eca0 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 context);. sqli
7ecb0 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 te3OsCurrentTime
7ecc0 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 54 29 3b (db->pVfs, &rT);
7ecd0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
7ece0 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
7ecf0 49 4e 54 0a 20 20 74 20 3d 20 38 36 34 30 30 2e INT. t = 86400.
7ed00 30 2a 28 72 54 20 2d 20 32 34 34 30 35 38 37 2e 0*(rT - 2440587.
7ed10 35 29 20 2b 20 30 2e 35 3b 0a 23 65 6c 73 65 0a 5) + 0.5;.#else.
7ed20 20 20 2f 2a 20 77 69 74 68 6f 75 74 20 66 6c 6f /* without flo
7ed30 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 ating point supp
7ed40 6f 72 74 2c 20 72 54 20 77 69 6c 6c 20 68 61 76 ort, rT will hav
7ed50 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 6c e. ** already l
7ed60 6f 73 74 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 ost fractional d
7ed70 61 79 20 70 72 65 63 69 73 69 6f 6e 2e 0a 20 20 ay precision..
7ed80 2a 2f 0a 20 20 74 20 3d 20 38 36 34 30 30 20 2a */. t = 86400 *
7ed90 20 28 72 54 20 2d 20 32 34 34 30 35 38 37 29 20 (rT - 2440587)
7eda0 2d 20 34 33 32 30 30 3b 0a 23 65 6e 64 69 66 0a - 43200;.#endif.
7edb0 23 69 66 64 65 66 20 48 41 56 45 5f 47 4d 54 49 #ifdef HAVE_GMTI
7edc0 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 74 72 ME_R. {. str
7edd0 75 63 74 20 74 6d 20 73 4e 6f 77 3b 0a 20 20 20 uct tm sNow;.
7ede0 20 67 6d 74 69 6d 65 5f 72 28 26 74 2c 20 26 73 gmtime_r(&t, &s
7edf0 4e 6f 77 29 3b 0a 20 20 20 20 73 74 72 66 74 69 Now);. strfti
7ee00 6d 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f me(zBuf, 20, zFo
7ee10 72 6d 61 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 rmat, &sNow);.
7ee20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 }.#else. {.
7ee30 73 74 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b 0a struct tm *pTm;.
7ee40 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
7ee50 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d x_enter(sqlite3M
7ee60 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
7ee70 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
7ee80 53 54 45 52 29 29 3b 0a 20 20 20 20 70 54 6d 20 STER));. pTm
7ee90 3d 20 67 6d 74 69 6d 65 28 26 74 29 3b 0a 20 20 = gmtime(&t);.
7eea0 20 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c strftime(zBuf,
7eeb0 20 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 70 54 20, zFormat, pT
7eec0 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f m);. sqlite3_
7eed0 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 mutex_leave(sqli
7eee0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
7eef0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
7ef00 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d 0a C_MASTER));. }.
7ef10 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 #endif.. sqlite
7ef20 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 3_result_text(co
7ef30 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c ntext, zBuf, -1,
7ef40 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
7ef50 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a T);.}.#endif../*
7ef60 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
7ef70 6e 20 72 65 67 69 73 74 65 72 65 64 20 61 6c 6c n registered all
7ef80 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 43 20 of the above C
7ef90 66 75 6e 63 74 69 6f 6e 73 20 61 73 20 53 51 4c functions as SQL
7efa0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 .** functions.
7efb0 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 74 This should be t
7efc0 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 he only routine
7efd0 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 in this file wit
7efe0 68 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 6c 69 h.** external li
7eff0 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 nkage..*/.SQLITE
7f000 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
7f010 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 lite3RegisterDat
7f020 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76 eTimeFunctions(v
7f030 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 53 oid){. static S
7f040 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 QLITE_WSD FuncDe
7f050 66 20 61 44 61 74 65 54 69 6d 65 46 75 6e 63 73 f aDateTimeFuncs
7f060 5b 5d 20 3d 20 7b 0a 23 69 66 6e 64 65 66 20 53 [] = {.#ifndef S
7f070 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 QLITE_OMIT_DATET
7f080 49 4d 45 5f 46 55 4e 43 53 0a 20 20 20 20 46 55 IME_FUNCS. FU
7f090 4e 43 54 49 4f 4e 28 6a 75 6c 69 61 6e 64 61 79 NCTION(julianday
7f0a0 2c 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 , -1, 0,
7f0b0 30 2c 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 0, juliandayFunc
7f0c0 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e ),. FUNCTION
7f0d0 28 64 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 (date,
7f0e0 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 64 61 74 -1, 0, 0, dat
7f0f0 65 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 eFunc ),.
7f100 20 20 46 55 4e 43 54 49 4f 4e 28 74 69 6d 65 2c FUNCTION(time,
7f110 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c -1,
7f120 20 30 2c 20 30 2c 20 74 69 6d 65 46 75 6e 63 20 0, 0, timeFunc
7f130 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 ),. FUNC
7f140 54 49 4f 4e 28 64 61 74 65 74 69 6d 65 2c 20 20 TION(datetime,
7f150 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c -1, 0, 0,
7f160 20 64 61 74 65 74 69 6d 65 46 75 6e 63 20 20 29 datetimeFunc )
7f170 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 ,. FUNCTION(s
7f180 74 72 66 74 69 6d 65 2c 20 20 20 20 20 20 20 20 trftime,
7f190 20 2d 31 2c 20 30 2c 20 30 2c 20 73 74 72 66 74 -1, 0, 0, strft
7f1a0 69 6d 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 imeFunc ),.
7f1b0 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 FUNCTION(current
7f1c0 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 30 _time, 0, 0
7f1d0 2c 20 30 2c 20 63 74 69 6d 65 46 75 6e 63 20 20 , 0, ctimeFunc
7f1e0 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 ),. FUNCTI
7f1f0 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 ON(current_times
7f200 74 61 6d 70 2c 20 30 2c 20 30 2c 20 30 2c 20 63 tamp, 0, 0, 0, c
7f210 74 69 6d 65 73 74 61 6d 70 46 75 6e 63 29 2c 0a timestampFunc),.
7f220 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 FUNCTION(cur
7f230 72 65 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20 rent_date,
7f240 30 2c 20 30 2c 20 30 2c 20 63 64 61 74 65 46 75 0, 0, 0, cdateFu
7f250 6e 63 20 20 20 20 20 29 2c 0a 23 65 6c 73 65 0a nc ),.#else.
7f260 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e STR_FUNCTION
7f270 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20 20 (current_time,
7f280 20 20 20 20 30 2c 20 22 25 48 3a 25 4d 3a 25 53 0, "%H:%M:%S
7f290 22 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 63 ", 0, c
7f2a0 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29 2c urrentTimeFunc),
7f2b0 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f . STR_FUNCTIO
7f2c0 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 N(current_timest
7f2d0 61 6d 70 2c 20 30 2c 20 22 25 59 2d 25 6d 2d 25 amp, 0, "%Y-%m-%
7f2e0 64 22 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 d", 0,
7f2f0 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29 currentTimeFunc)
7f300 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49 ,. STR_FUNCTI
7f310 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 74 65 2c ON(current_date,
7f320 20 20 20 20 20 20 30 2c 20 22 25 59 2d 25 6d 2d 0, "%Y-%m-
7f330 25 64 20 25 48 3a 25 4d 3a 25 53 22 2c 20 30 2c %d %H:%M:%S", 0,
7f340 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 currentTimeFunc
7f350 29 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20 ),.#endif. };.
7f360 20 69 6e 74 20 69 3b 0a 20 20 46 75 6e 63 44 65 int i;. FuncDe
7f370 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 fHash *pHash = &
7f380 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61 GLOBAL(FuncDefHa
7f390 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sh, sqlite3Globa
7f3a0 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 46 lFunctions);. F
7f3b0 75 6e 63 44 65 66 20 2a 61 46 75 6e 63 20 3d 20 uncDef *aFunc =
7f3c0 28 46 75 6e 63 44 65 66 2a 29 26 47 4c 4f 42 41 (FuncDef*)&GLOBA
7f3d0 4c 28 46 75 6e 63 44 65 66 2c 20 61 44 61 74 65 L(FuncDef, aDate
7f3e0 54 69 6d 65 46 75 6e 63 73 29 3b 0a 0a 20 20 66 TimeFuncs);.. f
7f3f0 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 or(i=0; i<ArrayS
7f400 69 7a 65 28 61 44 61 74 65 54 69 6d 65 46 75 6e ize(aDateTimeFun
7f410 63 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 cs); i++){. s
7f420 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 qlite3FuncDefIns
7f430 65 72 74 28 70 48 61 73 68 2c 20 26 61 46 75 6e ert(pHash, &aFun
7f440 63 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a c[i]);. }.}../*
7f450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
7f460 64 20 6f 66 20 64 61 74 65 2e 63 20 2a 2a 2a 2a d of date.c ****
7f470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
7f4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
7f4b0 67 69 6e 20 66 69 6c 65 20 6f 73 2e 63 20 2a 2a gin file os.c **
7f4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
7f4f0 0a 2a 2a 20 32 30 30 35 20 4e 6f 76 65 6d 62 65 .** 2005 Novembe
7f500 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 29.**.** The a
7f510 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
7f520 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
7f530 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
7f540 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
7f550 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
7f560 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
7f570 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
7f580 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
7f590 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
7f5a0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
7f5b0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
7f5c0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
7f5d0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
7f5e0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
7f5f0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
7f600 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
7f610 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
7f620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f660 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
7f670 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
7f680 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 63 s OS interface c
7f690 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d ode that is comm
7f6a0 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 61 72 63 on to all.** arc
7f6b0 68 69 74 65 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a hitectures..**.*
7f6c0 2a 20 24 49 64 3a 20 6f 73 2e 63 2c 76 20 31 2e * $Id: os.c,v 1.
7f6d0 31 32 37 20 32 30 30 39 2f 30 37 2f 32 37 20 31 127 2009/07/27 1
7f6e0 31 3a 34 31 3a 32 31 20 64 61 6e 69 65 6c 6b 31 1:41:21 danielk1
7f6f0 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 64 65 977 Exp $.*/.#de
7f700 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53 5f fine _SQLITE_OS_
7f710 43 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53 51 4c C_ 1.#undef _SQL
7f720 49 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a 2a ITE_OS_C_../*.**
7f730 20 54 68 65 20 64 65 66 61 75 6c 74 20 53 51 4c The default SQL
7f740 69 74 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 ite sqlite3_vfs
7f750 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
7f760 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 0a do not allocate.
7f770 2a 2a 20 6d 65 6d 6f 72 79 20 28 61 63 74 75 61 ** memory (actua
7f780 6c 6c 79 2c 20 6f 73 5f 75 6e 69 78 2e 63 20 61 lly, os_unix.c a
7f790 6c 6c 6f 63 61 74 65 73 20 61 20 73 6d 61 6c 6c llocates a small
7f7a0 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 amount of memor
7f7b0 79 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e y.** from within
7f7c0 20 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75 74 20 OsOpen()), but
7f7d0 73 6f 6d 65 20 74 68 69 72 64 2d 70 61 72 74 79 some third-party
7f7e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
7f7f0 20 6d 61 79 2e 0a 2a 2a 20 53 6f 20 77 65 20 74 may..** So we t
7f800 65 73 74 20 74 68 65 20 65 66 66 65 63 74 73 20 est the effects
7f810 6f 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 of a malloc() fa
7f820 69 6c 69 6e 67 20 61 6e 64 20 74 68 65 20 73 71 iling and the sq
7f830 6c 69 74 65 33 4f 73 58 58 58 28 29 0a 2a 2a 20 lite3OsXXX().**
7f840 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 69 function returni
7f850 6e 67 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f ng SQLITE_IOERR_
7f860 4e 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68 65 20 NOMEM using the
7f870 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 DO_OS_MALLOC_TES
7f880 54 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 T macro..**.** T
7f890 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e he following fun
7f8a0 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 73 74 72 ctions are instr
7f8b0 75 6d 65 6e 74 65 64 20 66 6f 72 20 6d 61 6c 6c umented for mall
7f8c0 6f 63 28 29 20 66 61 69 6c 75 72 65 20 0a 2a 2a oc() failure .**
7f8d0 20 74 65 73 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 testing:.**.**
7f8e0 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 sqlite3OsOpe
7f8f0 6e 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 n().** sqlit
7f900 65 33 4f 73 52 65 61 64 28 29 0a 2a 2a 20 20 20 e3OsRead().**
7f910 20 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 sqlite3OsWrite
7f920 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 ().** sqlite
7f930 33 4f 73 53 79 6e 63 28 29 0a 2a 2a 20 20 20 20 3OsSync().**
7f940 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 sqlite3OsLock()
7f950 0a 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e .**.*/.#if defin
7f960 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 ed(SQLITE_TEST)
7f970 26 26 20 28 53 51 4c 49 54 45 5f 4f 53 5f 57 49 && (SQLITE_OS_WI
7f980 4e 3d 3d 30 29 0a 20 20 23 64 65 66 69 6e 65 20 N==0). #define
7f990 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 DO_OS_MALLOC_TES
7f9a0 54 28 78 29 20 69 66 20 28 21 78 20 7c 7c 20 21 T(x) if (!x || !
7f9b0 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 sqlite3IsMemJour
7f9c0 6e 61 6c 28 78 29 29 20 7b 20 20 20 20 20 5c 0a nal(x)) { \.
7f9d0 20 20 20 20 76 6f 69 64 20 2a 70 54 73 74 41 6c void *pTstAl
7f9e0 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c loc = sqlite3Mal
7f9f0 6c 6f 63 28 31 30 29 3b 20 20 20 20 20 20 20 20 loc(10);
7fa00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7fa10 20 20 20 20 20 5c 0a 20 20 20 20 69 66 20 28 21 \. if (!
7fa20 70 54 73 74 41 6c 6c 6f 63 29 20 72 65 74 75 72 pTstAlloc) retur
7fa30 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e n SQLITE_IOERR_N
7fa40 4f 4d 45 4d 3b 20 20 20 20 20 20 20 20 20 20 20 OMEM;
7fa50 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
7fa60 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
7fa70 54 73 74 41 6c 6c 6f 63 29 3b 20 20 20 20 20 20 TstAlloc);
7fa80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7fa90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7faa0 20 20 20 5c 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 \. }.#else.
7fab0 20 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53 5f 4d #define DO_OS_M
7fac0 41 4c 4c 4f 43 5f 54 45 53 54 28 78 29 0a 23 65 ALLOC_TEST(x).#e
7fad0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
7fae0 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e following routin
7faf0 65 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 65 6e es are convenien
7fb00 63 65 20 77 72 61 70 70 65 72 73 20 61 72 6f 75 ce wrappers arou
7fb10 6e 64 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 nd methods.** of
7fb20 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c the sqlite3_fil
7fb30 65 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 e object. This
7fb40 69 73 20 6d 6f 73 74 6c 79 20 6a 75 73 74 20 73 is mostly just s
7fb50 79 6e 74 61 63 74 69 63 20 73 75 67 61 72 2e 20 yntactic sugar.
7fb60 41 6c 6c 0a 2a 2a 20 6f 66 20 74 68 69 73 20 77 All.** of this w
7fb70 6f 75 6c 64 20 62 65 20 63 6f 6d 70 6c 65 74 65 ould be complete
7fb80 6c 79 20 61 75 74 6f 6d 61 74 69 63 20 69 66 20 ly automatic if
7fb90 53 51 4c 69 74 65 20 77 65 72 65 20 63 6f 64 65 SQLite were code
7fba0 64 20 75 73 69 6e 67 0a 2a 2a 20 43 2b 2b 20 69 d using.** C++ i
7fbb0 6e 73 74 65 61 64 20 6f 66 20 70 6c 61 69 6e 20 nstead of plain
7fbc0 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 old C..*/.SQLITE
7fbd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
7fbe0 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 6c 69 ite3OsClose(sqli
7fbf0 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 29 7b 0a te3_file *pId){.
7fc00 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
7fc10 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 49 64 2d E_OK;. if( pId-
7fc20 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 >pMethods ){.
7fc30 20 72 63 20 3d 20 70 49 64 2d 3e 70 4d 65 74 68 rc = pId->pMeth
7fc40 6f 64 73 2d 3e 78 43 6c 6f 73 65 28 70 49 64 29 ods->xClose(pId)
7fc50 3b 0a 20 20 20 20 70 49 64 2d 3e 70 4d 65 74 68 ;. pId->pMeth
7fc60 6f 64 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 ods = 0;. }. r
7fc70 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 eturn rc;.}.SQLI
7fc80 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
7fc90 71 6c 69 74 65 33 4f 73 52 65 61 64 28 73 71 6c qlite3OsRead(sql
7fca0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 76 ite3_file *id, v
7fcb0 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 oid *pBuf, int a
7fcc0 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b mt, i64 offset){
7fcd0 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f . DO_OS_MALLOC_
7fce0 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 TEST(id);. retu
7fcf0 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
7fd00 3e 78 52 65 61 64 28 69 64 2c 20 70 42 75 66 2c >xRead(id, pBuf,
7fd10 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d amt, offset);.}
7fd20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7fd30 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 int sqlite3OsWri
7fd40 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 te(sqlite3_file
7fd50 2a 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 *id, const void
7fd60 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 *pBuf, int amt,
7fd70 69 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 20 44 i64 offset){. D
7fd80 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
7fd90 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 (id);. return i
7fda0 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 57 72 d->pMethods->xWr
7fdb0 69 74 65 28 69 64 2c 20 70 42 75 66 2c 20 61 6d ite(id, pBuf, am
7fdc0 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51 t, offset);.}.SQ
7fdd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
7fde0 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 sqlite3OsTrunca
7fdf0 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 te(sqlite3_file
7fe00 2a 69 64 2c 20 69 36 34 20 73 69 7a 65 29 7b 0a *id, i64 size){.
7fe10 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 return id->pMe
7fe20 74 68 6f 64 73 2d 3e 78 54 72 75 6e 63 61 74 65 thods->xTruncate
7fe30 28 69 64 2c 20 73 69 7a 65 29 3b 0a 7d 0a 53 51 (id, size);.}.SQ
7fe40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
7fe50 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 sqlite3OsSync(s
7fe60 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
7fe70 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 44 int flags){. D
7fe80 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
7fe90 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 (id);. return i
7fea0 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 79 d->pMethods->xSy
7feb0 6e 63 28 69 64 2c 20 66 6c 61 67 73 29 3b 0a 7d nc(id, flags);.}
7fec0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7fed0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c int sqlite3OsFil
7fee0 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 eSize(sqlite3_fi
7fef0 6c 65 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69 le *id, i64 *pSi
7ff00 7a 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c ze){. DO_OS_MAL
7ff10 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 LOC_TEST(id);.
7ff20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 return id->pMeth
7ff30 6f 64 73 2d 3e 78 46 69 6c 65 53 69 7a 65 28 69 ods->xFileSize(i
7ff40 64 2c 20 70 53 69 7a 65 29 3b 0a 7d 0a 53 51 4c d, pSize);.}.SQL
7ff50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
7ff60 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 sqlite3OsLock(sq
7ff70 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
7ff80 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 int lockType){.
7ff90 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
7ffa0 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e ST(id);. return
7ffb0 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 id->pMethods->x
7ffc0 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70 Lock(id, lockTyp
7ffd0 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 e);.}.SQLITE_PRI
7ffe0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
7fff0 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 OsUnlock(sqlite3
80000 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c _file *id, int l
80010 6f 63 6b 54 79 70 65 29 7b 0a 20 20 72 65 74 75 ockType){. retu
80020 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
80030 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 6c 6f 63 >xUnlock(id, loc
80040 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 kType);.}.SQLITE
80050 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
80060 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 ite3OsCheckReser
80070 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f vedLock(sqlite3_
80080 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 file *id, int *p
80090 52 65 73 4f 75 74 29 7b 0a 20 20 44 4f 5f 4f 53 ResOut){. DO_OS
800a0 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 _MALLOC_TEST(id)
800b0 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 ;. return id->p
800c0 4d 65 74 68 6f 64 73 2d 3e 78 43 68 65 63 6b 52 Methods->xCheckR
800d0 65 73 65 72 76 65 64 4c 6f 63 6b 28 69 64 2c 20 eservedLock(id,
800e0 70 52 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 pResOut);.}.SQLI
800f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
80100 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 qlite3OsFileCont
80110 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 rol(sqlite3_file
80120 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f *id, int op, vo
80130 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 72 65 74 id *pArg){. ret
80140 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 urn id->pMethods
80150 2d 3e 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 ->xFileControl(i
80160 64 2c 20 6f 70 2c 20 70 41 72 67 29 3b 0a 7d 0a d, op, pArg);.}.
80170 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
80180 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 nt sqlite3OsSect
80190 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 orSize(sqlite3_f
801a0 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 ile *id){. int
801b0 28 2a 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 (*xSectorSize)(s
801c0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 20 3d 20 qlite3_file*) =
801d0 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 id->pMethods->xS
801e0 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 72 65 74 ectorSize;. ret
801f0 75 72 6e 20 28 78 53 65 63 74 6f 72 53 69 7a 65 urn (xSectorSize
80200 20 3f 20 78 53 65 63 74 6f 72 53 69 7a 65 28 69 ? xSectorSize(i
80210 64 29 20 3a 20 53 51 4c 49 54 45 5f 44 45 46 41 d) : SQLITE_DEFA
80220 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 29 ULT_SECTOR_SIZE)
80230 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
80240 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
80250 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
80260 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 stics(sqlite3_fi
80270 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 le *id){. retur
80280 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e n id->pMethods->
80290 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 xDeviceCharacter
802a0 69 73 74 69 63 73 28 69 64 29 3b 0a 7d 0a 0a 2f istics(id);.}../
802b0 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72 *.** The next gr
802c0 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 oup of routines
802d0 61 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 are convenience
802e0 77 72 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20 wrappers around
802f0 74 68 65 0a 2a 2a 20 56 46 53 20 6d 65 74 68 6f the.** VFS metho
80300 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ds..*/.SQLITE_PR
80310 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
80320 33 4f 73 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 3OsOpen(. sqlit
80330 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 e3_vfs *pVfs, .
80340 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 const char *zPa
80350 74 68 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 th, . sqlite3_f
80360 69 6c 65 20 2a 70 46 69 6c 65 2c 20 0a 20 20 69 ile *pFile, . i
80370 6e 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 nt flags, . int
80380 20 2a 70 46 6c 61 67 73 4f 75 74 0a 29 7b 0a 20 *pFlagsOut.){.
80390 20 69 6e 74 20 72 63 3b 0a 20 20 44 4f 5f 4f 53 int rc;. DO_OS
803a0 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 30 29 3b _MALLOC_TEST(0);
803b0 0a 20 20 2f 2a 20 30 78 37 66 31 66 20 69 73 20 . /* 0x7f1f is
803c0 61 20 6d 61 73 6b 20 6f 66 20 53 51 4c 49 54 45 a mask of SQLITE
803d0 5f 4f 50 45 4e 5f 20 66 6c 61 67 73 20 74 68 61 _OPEN_ flags tha
803e0 74 20 61 72 65 20 76 61 6c 69 64 20 74 6f 20 62 t are valid to b
803f0 65 20 70 61 73 73 65 64 0a 20 20 2a 2a 20 64 6f e passed. ** do
80400 77 6e 20 69 6e 74 6f 20 74 68 65 20 56 46 53 20 wn into the VFS
80410 6c 61 79 65 72 2e 20 20 53 6f 6d 65 20 53 51 4c layer. Some SQL
80420 49 54 45 5f 4f 50 45 4e 5f 20 66 6c 61 67 73 20 ITE_OPEN_ flags
80430 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 20 20 (for example,.
80440 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 ** SQLITE_OPEN_F
80450 55 4c 4c 4d 55 54 45 58 20 6f 72 20 53 51 4c 49 ULLMUTEX or SQLI
80460 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 TE_OPEN_SHAREDCA
80470 43 48 45 29 20 61 72 65 20 62 6c 6f 63 6b 65 64 CHE) are blocked
80480 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 72 65 61 before. ** rea
80490 63 68 69 6e 67 20 74 68 65 20 56 46 53 2e 20 2a ching the VFS. *
804a0 2f 0a 20 20 72 63 20 3d 20 70 56 66 73 2d 3e 78 /. rc = pVfs->x
804b0 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 Open(pVfs, zPath
804c0 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 20 26 , pFile, flags &
804d0 20 30 78 37 66 31 66 2c 20 70 46 6c 61 67 73 4f 0x7f1f, pFlagsO
804e0 75 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 ut);. assert( r
804f0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c==SQLITE_OK ||
80500 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 73 3d pFile->pMethods=
80510 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 =0 );. return r
80520 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 c;.}.SQLITE_PRIV
80530 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
80540 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f sDelete(sqlite3_
80550 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 vfs *pVfs, const
80560 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 69 6e char *zPath, in
80570 74 20 64 69 72 53 79 6e 63 29 7b 0a 20 20 72 65 t dirSync){. re
80580 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 65 6c 65 turn pVfs->xDele
80590 74 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 te(pVfs, zPath,
805a0 64 69 72 53 79 6e 63 29 3b 0a 7d 0a 53 51 4c 49 dirSync);.}.SQLI
805b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
805c0 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 0a qlite3OsAccess(.
805d0 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
805e0 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 Vfs, . const ch
805f0 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e ar *zPath, . in
80600 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 t flags, . int
80610 2a 70 52 65 73 4f 75 74 0a 29 7b 0a 20 20 44 4f *pResOut.){. DO
80620 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 _OS_MALLOC_TEST(
80630 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 0);. return pVf
80640 73 2d 3e 78 41 63 63 65 73 73 28 70 56 66 73 2c s->xAccess(pVfs,
80650 20 7a 50 61 74 68 2c 20 66 6c 61 67 73 2c 20 70 zPath, flags, p
80660 52 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 ResOut);.}.SQLIT
80670 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
80680 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e lite3OsFullPathn
80690 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 ame(. sqlite3_v
806a0 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e fs *pVfs, . con
806b0 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 st char *zPath,
806c0 0a 20 20 69 6e 74 20 6e 50 61 74 68 4f 75 74 2c . int nPathOut,
806d0 20 0a 20 20 63 68 61 72 20 2a 7a 50 61 74 68 4f . char *zPathO
806e0 75 74 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 ut.){. return p
806f0 56 66 73 2d 3e 78 46 75 6c 6c 50 61 74 68 6e 61 Vfs->xFullPathna
80700 6d 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 me(pVfs, zPath,
80710 6e 50 61 74 68 4f 75 74 2c 20 7a 50 61 74 68 4f nPathOut, zPathO
80720 75 74 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 ut);.}.#ifndef S
80730 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f QLITE_OMIT_LOAD_
80740 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 EXTENSION.SQLITE
80750 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
80760 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 qlite3OsDlOpen(s
80770 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
80780 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 , const char *zP
80790 61 74 68 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 ath){. return p
807a0 56 66 73 2d 3e 78 44 6c 4f 70 65 6e 28 70 56 66 Vfs->xDlOpen(pVf
807b0 73 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a 53 51 4c s, zPath);.}.SQL
807c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
807d0 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f sqlite3OsDlErro
807e0 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 r(sqlite3_vfs *p
807f0 56 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 Vfs, int nByte,
80800 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a char *zBufOut){.
80810 20 20 70 56 66 73 2d 3e 78 44 6c 45 72 72 6f 72 pVfs->xDlError
80820 28 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 (pVfs, nByte, zB
80830 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 ufOut);.}.SQLITE
80840 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a _PRIVATE void (*
80850 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 sqlite3OsDlSym(s
80860 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
80870 2c 20 76 6f 69 64 20 2a 70 48 64 6c 65 2c 20 63 , void *pHdle, c
80880 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 29 onst char *zSym)
80890 29 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 )(void){. retur
808a0 6e 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70 n pVfs->xDlSym(p
808b0 56 66 73 2c 20 70 48 64 6c 65 2c 20 7a 53 79 6d Vfs, pHdle, zSym
808c0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
808d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
808e0 4f 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 OsDlClose(sqlite
808f0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 3_vfs *pVfs, voi
80900 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 70 d *pHandle){. p
80910 56 66 73 2d 3e 78 44 6c 43 6c 6f 73 65 28 70 56 Vfs->xDlClose(pV
80920 66 73 2c 20 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a fs, pHandle);.}.
80930 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
80940 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e _OMIT_LOAD_EXTEN
80950 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 SION */.SQLITE_P
80960 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
80970 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 e3OsRandomness(s
80980 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
80990 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 , int nByte, cha
809a0 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 72 r *zBufOut){. r
809b0 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 52 61 6e eturn pVfs->xRan
809c0 64 6f 6d 6e 65 73 73 28 70 56 66 73 2c 20 6e 42 domness(pVfs, nB
809d0 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d yte, zBufOut);.}
809e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
809f0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 int sqlite3OsSle
80a00 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ep(sqlite3_vfs *
80a10 70 56 66 73 2c 20 69 6e 74 20 6e 4d 69 63 72 6f pVfs, int nMicro
80a20 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 ){. return pVfs
80a30 2d 3e 78 53 6c 65 65 70 28 70 56 66 73 2c 20 6e ->xSleep(pVfs, n
80a40 4d 69 63 72 6f 29 3b 0a 7d 0a 53 51 4c 49 54 45 Micro);.}.SQLITE
80a50 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
80a60 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d ite3OsCurrentTim
80a70 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 e(sqlite3_vfs *p
80a80 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 54 69 Vfs, double *pTi
80a90 6d 65 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e meOut){. return
80aa0 20 70 56 66 73 2d 3e 78 43 75 72 72 65 6e 74 54 pVfs->xCurrentT
80ab0 69 6d 65 28 70 56 66 73 2c 20 70 54 69 6d 65 4f ime(pVfs, pTimeO
80ac0 75 74 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 ut);.}..SQLITE_P
80ad0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
80ae0 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0a e3OsOpenMalloc(.
80af0 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
80b00 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 Vfs, . const ch
80b10 61 72 20 2a 7a 46 69 6c 65 2c 20 0a 20 20 73 71 ar *zFile, . sq
80b20 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a 70 70 46 lite3_file **ppF
80b30 69 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 ile, . int flag
80b40 73 2c 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c s,. int *pOutFl
80b50 61 67 73 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 ags.){. int rc
80b60 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
80b70 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
80b80 70 46 69 6c 65 3b 0a 20 20 70 46 69 6c 65 20 3d pFile;. pFile =
80b90 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a (sqlite3_file *
80ba0 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 )sqlite3Malloc(p
80bb0 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a Vfs->szOsFile);.
80bc0 20 20 69 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 if( pFile ){.
80bd0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
80be0 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c sOpen(pVfs, zFil
80bf0 65 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c e, pFile, flags,
80c00 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 pOutFlags);.
80c10 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
80c20 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 OK ){. sqli
80c30 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b te3_free(pFile);
80c40 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
80c50 20 20 2a 70 70 46 69 6c 65 20 3d 20 70 46 69 6c *ppFile = pFil
80c60 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 e;. }. }. r
80c70 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 eturn rc;.}.SQLI
80c80 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
80c90 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 qlite3OsCloseFre
80ca0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
80cb0 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 pFile){. int rc
80cc0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
80cd0 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
80ce0 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f . rc = sqlite3O
80cf0 73 43 6c 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 sClose(pFile);.
80d00 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 sqlite3_free(pF
80d10 69 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 ile);. return r
80d20 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
80d30 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 function is a w
80d40 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68 rapper around th
80d50 65 20 4f 53 20 73 70 65 63 69 66 69 63 20 69 6d e OS specific im
80d60 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 0a plementation of.
80d70 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e ** sqlite3_os_in
80d80 69 74 28 29 2e 20 54 68 65 20 70 75 72 70 6f 73 it(). The purpos
80d90 65 20 6f 66 20 74 68 65 20 77 72 61 70 70 65 72 e of the wrapper
80da0 20 69 73 20 74 6f 20 70 72 6f 76 69 64 65 20 74 is to provide t
80db0 68 65 0a 2a 2a 20 61 62 69 6c 69 74 79 20 74 6f he.** ability to
80dc0 20 73 69 6d 75 6c 61 74 65 20 61 20 6d 61 6c 6c simulate a mall
80dd0 6f 63 20 66 61 69 6c 75 72 65 2c 20 73 6f 20 74 oc failure, so t
80de0 68 61 74 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 hat the handling
80df0 20 6f 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 of an.** error
80e00 69 6e 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e in sqlite3_os_in
80e10 69 74 28 29 20 62 79 20 74 68 65 20 75 70 70 65 it() by the uppe
80e20 72 20 6c 61 79 65 72 73 20 63 61 6e 20 62 65 20 r layers can be
80e30 74 65 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 tested..*/.SQLIT
80e40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
80e50 6c 69 74 65 33 4f 73 49 6e 69 74 28 76 6f 69 64 lite3OsInit(void
80e60 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 ){. void *p = s
80e70 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 31 30 qlite3_malloc(10
80e80 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 );. if( p==0 )
80e90 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
80ea0 4d 45 4d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 MEM;. sqlite3_f
80eb0 72 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ree(p);. return
80ec0 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 sqlite3_os_init
80ed0 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ();.}../*.** The
80ee0 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 72 65 67 list of all reg
80ef0 69 73 74 65 72 65 64 20 56 46 53 20 69 6d 70 6c istered VFS impl
80f00 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a ementations..*/.
80f10 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 static sqlite3_v
80f20 66 73 20 2a 20 53 51 4c 49 54 45 5f 57 53 44 20 fs * SQLITE_WSD
80f30 76 66 73 4c 69 73 74 20 3d 20 30 3b 0a 23 64 65 vfsList = 0;.#de
80f40 66 69 6e 65 20 76 66 73 4c 69 73 74 20 47 4c 4f fine vfsList GLO
80f50 42 41 4c 28 73 71 6c 69 74 65 33 5f 76 66 73 20 BAL(sqlite3_vfs
80f60 2a 2c 20 76 66 73 4c 69 73 74 29 0a 0a 2f 2a 0a *, vfsList)../*.
80f70 2a 2a 20 4c 6f 63 61 74 65 20 61 20 56 46 53 20 ** Locate a VFS
80f80 62 79 20 6e 61 6d 65 2e 20 20 49 66 20 6e 6f 20 by name. If no
80f90 6e 61 6d 65 20 69 73 20 67 69 76 65 6e 2c 20 73 name is given, s
80fa0 69 6d 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 imply return the
80fb0 0a 2a 2a 20 66 69 72 73 74 20 56 46 53 20 6f 6e .** first VFS on
80fc0 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 the list..*/.SQ
80fd0 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
80fe0 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 _vfs *sqlite3_vf
80ff0 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 s_find(const cha
81000 72 20 2a 7a 56 66 73 29 7b 0a 20 20 73 71 6c 69 r *zVfs){. sqli
81010 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 te3_vfs *pVfs =
81020 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 0;.#if SQLITE_TH
81030 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 READSAFE. sqlit
81040 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b e3_mutex *mutex;
81050 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
81060 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
81070 49 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 INIT. int rc =
81080 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
81090 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 ze();. if( rc )
810a0 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 return 0;.#endi
810b0 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 f.#if SQLITE_THR
810c0 45 41 44 53 41 46 45 0a 20 20 6d 75 74 65 78 20 EADSAFE. mutex
810d0 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c = sqlite3MutexAl
810e0 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
810f0 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b _STATIC_MASTER);
81100 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 .#endif. sqlite
81110 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 3_mutex_enter(mu
81120 74 65 78 29 3b 0a 20 20 66 6f 72 28 70 56 66 73 tex);. for(pVfs
81130 20 3d 20 76 66 73 4c 69 73 74 3b 20 70 56 66 73 = vfsList; pVfs
81140 3b 20 70 56 66 73 3d 70 56 66 73 2d 3e 70 4e 65 ; pVfs=pVfs->pNe
81150 78 74 29 7b 0a 20 20 20 20 69 66 28 20 7a 56 66 xt){. if( zVf
81160 73 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 s==0 ) break;.
81170 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a 56 66 if( strcmp(zVf
81180 73 2c 20 70 56 66 73 2d 3e 7a 4e 61 6d 65 29 3d s, pVfs->zName)=
81190 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a =0 ) break;. }.
811a0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
811b0 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 leave(mutex);.
811c0 72 65 74 75 72 6e 20 70 56 66 73 3b 0a 7d 0a 0a return pVfs;.}..
811d0 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 61 20 56 /*.** Unlink a V
811e0 46 53 20 66 72 6f 6d 20 74 68 65 20 6c 69 6e 6b FS from the link
811f0 65 64 20 6c 69 73 74 0a 2a 2f 0a 73 74 61 74 69 ed list.*/.stati
81200 63 20 76 6f 69 64 20 76 66 73 55 6e 6c 69 6e 6b c void vfsUnlink
81210 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
81220 66 73 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 fs){. assert( s
81230 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
81240 64 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c d(sqlite3MutexAl
81250 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
81260 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 _STATIC_MASTER))
81270 20 29 3b 0a 20 20 69 66 28 20 70 56 66 73 3d 3d );. if( pVfs==
81280 30 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 2d 6f 0 ){. /* No-o
81290 70 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 p */. }else if(
812a0 20 76 66 73 4c 69 73 74 3d 3d 70 56 66 73 20 29 vfsList==pVfs )
812b0 7b 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 {. vfsList =
812c0 70 56 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d pVfs->pNext;. }
812d0 65 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73 74 else if( vfsList
812e0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
812f0 76 66 73 20 2a 70 20 3d 20 76 66 73 4c 69 73 74 vfs *p = vfsList
81300 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e ;. while( p->
81310 70 4e 65 78 74 20 26 26 20 70 2d 3e 70 4e 65 78 pNext && p->pNex
81320 74 21 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 t!=pVfs ){.
81330 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 p = p->pNext;.
81340 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e }. if( p->
81350 70 4e 65 78 74 3d 3d 70 56 66 73 20 29 7b 0a 20 pNext==pVfs ){.
81360 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 p->pNext =
81370 70 56 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 pVfs->pNext;.
81380 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
81390 52 65 67 69 73 74 65 72 20 61 20 56 46 53 20 77 Register a VFS w
813a0 69 74 68 20 74 68 65 20 73 79 73 74 65 6d 2e 20 ith the system.
813b0 20 49 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20 It is harmless
813c0 74 6f 20 72 65 67 69 73 74 65 72 20 74 68 65 20 to register the
813d0 73 61 6d 65 0a 2a 2a 20 56 46 53 20 6d 75 6c 74 same.** VFS mult
813e0 69 70 6c 65 20 74 69 6d 65 73 2e 20 20 54 68 65 iple times. The
813f0 20 6e 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73 new VFS becomes
81400 20 74 68 65 20 64 65 66 61 75 6c 74 20 69 66 20 the default if
81410 6d 61 6b 65 44 66 6c 74 20 69 73 0a 2a 2a 20 74 makeDflt is.** t
81420 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 rue..*/.SQLITE_A
81430 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 PI int sqlite3_v
81440 66 73 5f 72 65 67 69 73 74 65 72 28 73 71 6c 69 fs_register(sqli
81450 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
81460 6e 74 20 6d 61 6b 65 44 66 6c 74 29 7b 0a 20 20 nt makeDflt){.
81470 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d sqlite3_mutex *m
81480 75 74 65 78 20 3d 20 30 3b 0a 23 69 66 6e 64 65 utex = 0;.#ifnde
81490 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
814a0 54 4f 49 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 TOINIT. int rc
814b0 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 = sqlite3_initia
814c0 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 lize();. if( rc
814d0 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 ) return rc;.#e
814e0 6e 64 69 66 0a 20 20 6d 75 74 65 78 20 3d 20 73 ndif. mutex = s
814f0 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
81500 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
81510 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 ATIC_MASTER);.
81520 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
81530 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 ter(mutex);. vf
81540 73 55 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 sUnlink(pVfs);.
81550 20 69 66 28 20 6d 61 6b 65 44 66 6c 74 20 7c 7c if( makeDflt ||
81560 20 76 66 73 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 vfsList==0 ){.
81570 20 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d pVfs->pNext =
81580 20 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 76 66 vfsList;. vf
81590 73 4c 69 73 74 20 3d 20 70 56 66 73 3b 0a 20 20 sList = pVfs;.
815a0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 56 66 73 2d }else{. pVfs-
815b0 3e 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 >pNext = vfsList
815c0 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 76 66 73 ->pNext;. vfs
815d0 4c 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 56 List->pNext = pV
815e0 66 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 fs;. }. assert
815f0 28 76 66 73 4c 69 73 74 29 3b 0a 20 20 73 71 6c (vfsList);. sql
81600 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
81610 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 (mutex);. retur
81620 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
81630 2f 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 /*.** Unregister
81640 20 61 20 56 46 53 20 73 6f 20 74 68 61 74 20 69 a VFS so that i
81650 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 t is no longer a
81660 63 63 65 73 73 69 62 6c 65 2e 0a 2a 2f 0a 53 51 ccessible..*/.SQ
81670 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
81680 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 ite3_vfs_unregis
81690 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ter(sqlite3_vfs
816a0 2a 70 56 66 73 29 7b 0a 23 69 66 20 53 51 4c 49 *pVfs){.#if SQLI
816b0 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 TE_THREADSAFE.
816c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d sqlite3_mutex *m
816d0 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 utex = sqlite3Mu
816e0 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
816f0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
81700 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 TER);.#endif. s
81710 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
81720 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 er(mutex);. vfs
81730 55 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 Unlink(pVfs);.
81740 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
81750 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 ave(mutex);. re
81760 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
81770 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
81780 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 2e 63 20 2a ** End of os.c *
81790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
817a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
817b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
817c0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
817d0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 61 ** Begin file fa
817e0 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ult.c **********
817f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81810 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 61 */./*.** 2008 Ja
81820 6e 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 n 22.**.** The a
81830 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
81840 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
81850 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
81860 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
81870 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
81880 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
81890 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
818a0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
818b0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
818c0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
818d0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
818e0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
818f0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
81900 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
81910 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
81920 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
81930 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
81940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81980 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66 ***.**.** $Id: f
81990 61 75 6c 74 2e 63 2c 76 20 31 2e 31 31 20 32 30 ault.c,v 1.11 20
819a0 30 38 2f 30 39 2f 30 32 20 30 30 3a 35 32 3a 35 08/09/02 00:52:5
819b0 32 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2 drh Exp $.*/..
819c0 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 /*.** This file
819d0 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f contains code to
819e0 20 73 75 70 70 6f 72 74 20 74 68 65 20 63 6f 6e support the con
819f0 63 65 70 74 20 6f 66 20 22 62 65 6e 69 67 6e 22 cept of "benign"
81a00 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c .** malloc fail
81a10 75 72 65 73 20 28 77 68 65 6e 20 74 68 65 20 78 ures (when the x
81a20 4d 61 6c 6c 6f 63 28 29 20 6f 72 20 78 52 65 61 Malloc() or xRea
81a30 6c 6c 6f 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 lloc() method of
81a40 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f the.** sqlite3_
81a50 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 73 74 72 75 mem_methods stru
81a60 63 74 75 72 65 20 66 61 69 6c 73 20 74 6f 20 61 cture fails to a
81a70 6c 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b 20 llocate a block
81a80 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6e 64 of memory.** and
81a90 20 72 65 74 75 72 6e 73 20 30 29 2e 20 0a 2a 2a returns 0). .**
81aa0 0a 2a 2a 20 4d 6f 73 74 20 6d 61 6c 6c 6f 63 20 .** Most malloc
81ab0 66 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e failures are non
81ac0 2d 62 65 6e 69 67 6e 2e 20 41 66 74 65 72 20 74 -benign. After t
81ad0 68 65 79 20 6f 63 63 75 72 2c 20 53 51 4c 69 74 hey occur, SQLit
81ae0 65 0a 2a 2a 20 61 62 61 6e 64 6f 6e 73 20 74 68 e.** abandons th
81af0 65 20 63 75 72 72 65 6e 74 20 6f 70 65 72 61 74 e current operat
81b00 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 73 20 ion and returns
81b10 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 28 75 an error code (u
81b20 73 75 61 6c 6c 79 0a 2a 2a 20 53 51 4c 49 54 45 sually.** SQLITE
81b30 5f 4e 4f 4d 45 4d 29 20 74 6f 20 74 68 65 20 75 _NOMEM) to the u
81b40 73 65 72 2e 20 48 6f 77 65 76 65 72 2c 20 73 6f ser. However, so
81b50 6d 65 74 69 6d 65 73 20 61 20 66 61 75 6c 74 20 metimes a fault
81b60 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 is not necessari
81b70 6c 79 0a 2a 2a 20 66 61 74 61 6c 2e 20 46 6f 72 ly.** fatal. For
81b80 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 61 20 6d example, if a m
81b90 61 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 6c alloc fails whil
81ba0 65 20 72 65 73 69 7a 69 6e 67 20 61 20 68 61 73 e resizing a has
81bb0 68 20 74 61 62 6c 65 2c 20 74 68 69 73 20 0a 2a h table, this .*
81bc0 2a 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 * is completely
81bd0 72 65 63 6f 76 65 72 61 62 6c 65 20 73 69 6d 70 recoverable simp
81be0 6c 79 20 62 79 20 6e 6f 74 20 63 61 72 72 79 69 ly by not carryi
81bf0 6e 67 20 6f 75 74 20 74 68 65 20 72 65 73 69 7a ng out the resiz
81c00 65 2e 20 54 68 65 20 0a 2a 2a 20 68 61 73 68 20 e. The .** hash
81c10 74 61 62 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 69 table will conti
81c20 6e 75 65 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 nue to function
81c30 6e 6f 72 6d 61 6c 6c 79 2e 20 20 53 6f 20 61 20 normally. So a
81c40 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 0a malloc failure .
81c50 2a 2a 20 64 75 72 69 6e 67 20 61 20 68 61 73 68 ** during a hash
81c60 20 74 61 62 6c 65 20 72 65 73 69 7a 65 20 69 73 table resize is
81c70 20 61 20 62 65 6e 69 67 6e 20 66 61 75 6c 74 2e a benign fault.
81c80 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 .*/...#ifndef SQ
81c90 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 LITE_OMIT_BUILTI
81ca0 4e 5f 54 45 53 54 0a 0a 2f 2a 0a 2a 2a 20 47 6c N_TEST../*.** Gl
81cb0 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2e 0a obal variables..
81cc0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
81cd0 74 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f t BenignMallocHo
81ce0 6f 6b 73 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 oks BenignMalloc
81cf0 48 6f 6f 6b 73 3b 0a 73 74 61 74 69 63 20 53 51 Hooks;.static SQ
81d00 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 LITE_WSD struct
81d10 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b BenignMallocHook
81d20 73 20 7b 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 s {. void (*xBe
81d30 6e 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 nignBegin)(void)
81d40 3b 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 ;. void (*xBeni
81d50 67 6e 45 6e 64 29 28 76 6f 69 64 29 3b 0a 7d 20 gnEnd)(void);.}
81d60 73 71 6c 69 74 65 33 48 6f 6f 6b 73 20 3d 20 7b sqlite3Hooks = {
81d70 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 20 54 68 65 0, 0 };../* The
81d80 20 22 77 73 64 48 6f 6f 6b 73 22 20 6d 61 63 72 "wsdHooks" macr
81d90 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 o will resolve t
81da0 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 o the appropriat
81db0 65 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f e BenignMallocHo
81dc0 6f 6b 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 oks.** structure
81dd0 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 . If writable s
81de0 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75 6e tatic data is un
81df0 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 supported on the
81e00 20 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68 target,.** we h
81e10 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 ave to locate th
81e20 65 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 e state vector a
81e30 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 t run-time. In
81e40 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a the more common.
81e50 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 72 ** case where wr
81e60 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 itable static da
81e70 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c ta is supported,
81e80 20 77 73 64 48 6f 6f 6b 73 20 63 61 6e 20 72 65 wsdHooks can re
81e90 66 65 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 fer directly.**
81ea0 74 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33 48 to the "sqlite3H
81eb0 6f 6f 6b 73 22 20 73 74 61 74 65 20 76 65 63 74 ooks" state vect
81ec0 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76 or declared abov
81ed0 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c e..*/.#ifdef SQL
81ee0 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64 ITE_OMIT_WSD.# d
81ef0 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e efine wsdHooksIn
81f00 69 74 20 5c 0a 20 20 42 65 6e 69 67 6e 4d 61 6c it \. BenignMal
81f10 6c 6f 63 48 6f 6f 6b 73 20 2a 78 20 3d 20 26 47 locHooks *x = &G
81f20 4c 4f 42 41 4c 28 42 65 6e 69 67 6e 4d 61 6c 6c LOBAL(BenignMall
81f30 6f 63 48 6f 6f 6b 73 2c 73 71 6c 69 74 65 33 48 ocHooks,sqlite3H
81f40 6f 6f 6b 73 29 0a 23 20 64 65 66 69 6e 65 20 77 ooks).# define w
81f50 73 64 48 6f 6f 6b 73 20 78 5b 30 5d 0a 23 65 6c sdHooks x[0].#el
81f60 73 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 se.# define wsdH
81f70 6f 6f 6b 73 49 6e 69 74 0a 23 20 64 65 66 69 6e ooksInit.# defin
81f80 65 20 77 73 64 48 6f 6f 6b 73 20 73 71 6c 69 74 e wsdHooks sqlit
81f90 65 33 48 6f 6f 6b 73 0a 23 65 6e 64 69 66 0a 0a e3Hooks.#endif..
81fa0 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 ./*.** Register
81fb0 68 6f 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 77 68 hooks to call wh
81fc0 65 6e 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 en sqlite3BeginB
81fd0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 6e enignMalloc() an
81fe0 64 0a 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64 42 d.** sqlite3EndB
81ff0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 72 enignMalloc() ar
82000 65 20 63 61 6c 6c 65 64 2c 20 72 65 73 70 65 63 e called, respec
82010 74 69 76 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 tively..*/.SQLIT
82020 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
82030 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c qlite3BenignMall
82040 6f 63 48 6f 6f 6b 73 28 0a 20 20 76 6f 69 64 20 ocHooks(. void
82050 28 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e 29 28 (*xBenignBegin)(
82060 76 6f 69 64 29 2c 0a 20 20 76 6f 69 64 20 28 2a void),. void (*
82070 78 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69 64 xBenignEnd)(void
82080 29 0a 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 ).){. wsdHooksI
82090 6e 69 74 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e nit;. wsdHooks.
820a0 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20 78 xBenignBegin = x
820b0 42 65 6e 69 67 6e 42 65 67 69 6e 3b 0a 20 20 77 BenignBegin;. w
820c0 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 sdHooks.xBenignE
820d0 6e 64 20 3d 20 78 42 65 6e 69 67 6e 45 6e 64 3b nd = xBenignEnd;
820e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 28 .}../*.** This (
820f0 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
82100 4d 61 6c 6c 6f 63 28 29 29 20 69 73 20 63 61 6c Malloc()) is cal
82110 6c 65 64 20 62 79 20 53 51 4c 69 74 65 20 63 6f led by SQLite co
82120 64 65 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 de to indicate t
82130 68 61 74 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e hat.** subsequen
82140 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 t malloc failure
82150 73 20 61 72 65 20 62 65 6e 69 67 6e 2e 20 41 20 s are benign. A
82160 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 call to sqlite3E
82170 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 ndBenignMalloc()
82180 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 74 68 .** indicates th
82190 61 74 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 at subsequent ma
821a0 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 lloc failures ar
821b0 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 0a 2a 2f e non-benign..*/
821c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
821d0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 void sqlite3Begi
821e0 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f nBenignMalloc(vo
821f0 69 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 id){. wsdHooksI
82200 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 64 48 6f nit;. if( wsdHo
82210 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e oks.xBenignBegin
82220 20 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 ){. wsdHooks
82230 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 28 29 3b .xBenignBegin();
82240 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 . }.}.SQLITE_PR
82250 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
82260 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f e3EndBenignMallo
82270 63 28 76 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f c(void){. wsdHo
82280 6f 6b 73 49 6e 69 74 3b 0a 20 20 69 66 28 20 77 oksInit;. if( w
82290 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 sdHooks.xBenignE
822a0 6e 64 20 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f nd ){. wsdHoo
822b0 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 28 29 3b ks.xBenignEnd();
822c0 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 20 . }.}..#endif
822d0 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 /* #ifndef SQLI
822e0 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f TE_OMIT_BUILTIN_
822f0 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a TEST */../******
82300 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
82310 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a fault.c ********
82320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82340 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
82350 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
82360 69 6c 65 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a ile mem0.c *****
82370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82390 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
823a0 30 30 38 20 4f 63 74 6f 62 65 72 20 32 38 0a 2a 008 October 28.*
823b0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
823c0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
823d0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
823e0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
823f0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
82400 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
82410 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
82420 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
82430 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
82440 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
82450 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
82460 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
82470 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
82480 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
82490 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
824a0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
824b0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
824c0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
824d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
824e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
824f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
82510 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
82520 6e 74 61 69 6e 73 20 61 20 6e 6f 2d 6f 70 20 6d ntains a no-op m
82530 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
82540 20 64 72 69 76 65 72 73 20 66 6f 72 20 75 73 65 drivers for use
82550 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f when.** SQLITE_
82560 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 20 69 73 20 64 ZERO_MALLOC is d
82570 65 66 69 6e 65 64 2e 20 20 54 68 65 20 61 6c 6c efined. The all
82580 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 ocation drivers
82590 69 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 68 implemented.** h
825a0 65 72 65 20 61 6c 77 61 79 73 20 66 61 69 6c 2e ere always fail.
825b0 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f SQLite will no
825c0 74 20 6f 70 65 72 61 74 65 20 77 69 74 68 20 74 t operate with t
825d0 68 65 73 65 20 64 72 69 76 65 72 73 2e 20 20 54 hese drivers. T
825e0 68 65 73 65 0a 2a 2a 20 61 72 65 20 6d 65 72 65 hese.** are mere
825f0 6c 79 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 2e ly placeholders.
82600 20 20 52 65 61 6c 20 64 72 69 76 65 72 73 20 6d Real drivers m
82610 75 73 74 20 62 65 20 73 75 62 73 74 69 74 75 74 ust be substitut
82620 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 ed using.** sqli
82630 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 62 65 66 te3_config() bef
82640 6f 72 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 ore SQLite will
82650 6f 70 65 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 operate..**.** $
82660 49 64 3a 20 6d 65 6d 30 2e 63 2c 76 20 31 2e 31 Id: mem0.c,v 1.1
82670 20 32 30 30 38 2f 31 30 2f 32 38 20 31 38 3a 35 2008/10/28 18:5
82680 38 3a 32 30 20 64 72 68 20 45 78 70 20 24 0a 2a 8:20 drh Exp $.*
82690 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 /../*.** This ve
826a0 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d rsion of the mem
826b0 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 ory allocator is
826c0 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49 the default. I
826d0 74 20 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65 t is.** used whe
826e0 6e 20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72 n no other memor
826f0 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73 y allocator is s
82700 70 65 63 69 66 69 65 64 20 75 73 69 6e 67 20 63 pecified using c
82710 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d ompile-time.** m
82720 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 acros..*/.#ifdef
82730 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c SQLITE_ZERO_MAL
82740 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 LOC../*.** No-op
82750 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 61 6c 6c versions of all
82760 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
82770 6f 6e 20 72 6f 75 74 69 6e 65 73 0a 2a 2f 0a 73 on routines.*/.s
82780 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 tatic void *sqli
82790 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 te3MemMalloc(int
827a0 20 6e 42 79 74 65 29 7b 20 72 65 74 75 72 6e 20 nByte){ return
827b0 30 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 0; }.static void
827c0 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 sqlite3MemFree(
827d0 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72 void *pPrior){ r
827e0 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 20 eturn; }.static
827f0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d void *sqlite3Mem
82800 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 Realloc(void *pP
82810 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 rior, int nByte)
82820 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 { return 0; }.st
82830 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 atic int sqlite3
82840 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50 MemSize(void *pP
82850 72 69 6f 72 29 7b 20 72 65 74 75 72 6e 20 30 3b rior){ return 0;
82860 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 }.static int sq
82870 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 lite3MemRoundup(
82880 69 6e 74 20 6e 29 7b 20 72 65 74 75 72 6e 20 6e int n){ return n
82890 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 ; }.static int s
828a0 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f qlite3MemInit(vo
828b0 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 20 72 65 id *NotUsed){ re
828c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
828d0 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 }.static void sq
828e0 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e lite3MemShutdown
828f0 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b (void *NotUsed){
82900 20 72 65 74 75 72 6e 3b 20 7d 0a 0a 2f 2a 0a 2a return; }../*.*
82910 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
82920 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 s the only routi
82930 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 ne in this file
82940 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20 6c 69 with external li
82950 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 nkage..**.** Pop
82960 75 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 ulate the low-le
82970 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 vel memory alloc
82980 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 ation function p
82990 6f 69 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 ointers in.** sq
829a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
829b0 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 g.m with pointer
829c0 73 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 s to the routine
829d0 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a s in this file..
829e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
829f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 E void sqlite3Me
82a00 6d 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 mSetDefault(void
82a10 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 ){. static cons
82a20 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 t sqlite3_mem_me
82a30 74 68 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 thods defaultMet
82a40 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 hods = {. sq
82a50 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a lite3MemMalloc,.
82a60 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 sqlite3MemF
82a70 72 65 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 ree,. sqlite
82a80 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 3MemRealloc,.
82a90 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 sqlite3MemSize
82aa0 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
82ab0 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 mRoundup,. s
82ac0 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 qlite3MemInit,.
82ad0 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 sqlite3MemSh
82ae0 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 utdown,. 0.
82af0 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f };. sqlite3_co
82b00 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 nfig(SQLITE_CONF
82b10 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 IG_MALLOC, &defa
82b20 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a ultMethods);.}..
82b30 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
82b40 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a _ZERO_MALLOC */.
82b50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
82b60 20 45 6e 64 20 6f 66 20 6d 65 6d 30 2e 63 20 2a End of mem0.c *
82b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
82ba0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
82bb0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 31 Begin file mem1
82bc0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
82bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
82bf0 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 ./*.** 2007 Augu
82c00 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 14.**.** The
82c10 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
82c20 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
82c30 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
82c40 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
82c50 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
82c60 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
82c70 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
82c80 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
82c90 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
82ca0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
82cb0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
82cc0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
82cd0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
82ce0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
82cf0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
82d00 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
82d10 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
82d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82d60 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
82d70 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f file contains lo
82d80 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
82d90 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 llocation driver
82da0 73 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51 s for when.** SQ
82db0 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 Lite will use th
82dc0 65 20 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62 e standard C-lib
82dd0 72 61 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c rary malloc/real
82de0 6c 6f 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61 loc/free interfa
82df0 63 65 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 ce.** to obtain
82e00 74 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 the memory it ne
82e10 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 eds..**.** This
82e20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6d file contains im
82e30 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 plementations of
82e40 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d the low-level m
82e50 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
82e60 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 73 70 65 .** routines spe
82e70 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 73 71 cified in the sq
82e80 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
82e90 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 s object..**.**
82ea0 24 49 64 3a 20 6d 65 6d 31 2e 63 2c 76 20 31 2e $Id: mem1.c,v 1.
82eb0 33 30 20 32 30 30 39 2f 30 33 2f 32 33 20 30 34 30 2009/03/23 04
82ec0 3a 33 33 3a 33 33 20 64 61 6e 69 65 6c 6b 31 39 :33:33 danielk19
82ed0 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 77 Exp $.*/../*.
82ee0 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 ** This version
82ef0 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c of the memory al
82f00 6c 6f 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 locator is the d
82f10 65 66 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a efault. It is.*
82f20 2a 20 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f * used when no o
82f30 74 68 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ther memory allo
82f40 63 61 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 cator is specifi
82f50 65 64 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 ed using compile
82f60 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e -time.** macros.
82f70 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
82f80 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a E_SYSTEM_MALLOC.
82f90 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 6d 61 6c 6c ./*.** Like mall
82fa0 6f 63 28 29 2c 20 62 75 74 20 72 65 6d 65 6d 62 oc(), but rememb
82fb0 65 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 er the size of t
82fc0 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a he allocation.**
82fd0 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 so that we can
82fe0 66 69 6e 64 20 69 74 20 6c 61 74 65 72 20 75 73 find it later us
82ff0 69 6e 67 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 ing sqlite3MemSi
83000 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 ze()..**.** For
83010 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 this low-level r
83020 6f 75 74 69 6e 65 2c 20 77 65 20 61 72 65 20 67 outine, we are g
83030 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e uaranteed that n
83040 42 79 74 65 3e 30 20 62 65 63 61 75 73 65 0a 2a Byte>0 because.*
83050 2a 20 63 61 73 65 73 20 6f 66 20 6e 42 79 74 65 * cases of nByte
83060 3c 3d 30 20 77 69 6c 6c 20 62 65 20 69 6e 74 65 <=0 will be inte
83070 72 63 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c rcepted and deal
83080 74 20 77 69 74 68 20 62 79 20 68 69 67 68 65 72 t with by higher
83090 20 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e level.** routin
830a0 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f es..*/.static vo
830b0 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 id *sqlite3MemMa
830c0 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b lloc(int nByte){
830d0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
830e0 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e *p;. assert( n
830f0 42 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74 Byte>0 );. nByt
83100 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 e = ROUND8(nByte
83110 29 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 );. p = malloc(
83120 20 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 nByte+8 );. if
83130 28 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 ( p ){. p[0]
83140 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b = nByte;. p++
83150 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 ;. }. return (
83160 76 6f 69 64 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a void *)p;.}../*.
83170 2a 2a 20 4c 69 6b 65 20 66 72 65 65 28 29 20 62 ** Like free() b
83180 75 74 20 77 6f 72 6b 73 20 66 6f 72 20 61 6c 6c ut works for all
83190 6f 63 61 74 69 6f 6e 73 20 6f 62 74 61 69 6e 65 ocations obtaine
831a0 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 65 d from sqlite3Me
831b0 6d 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 mMalloc().** or
831c0 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f sqlite3MemReallo
831d0 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 c()..**.** For t
831e0 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f his low-level ro
831f0 75 74 69 6e 65 2c 20 77 65 20 61 6c 72 65 61 64 utine, we alread
83200 79 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 y know that pPri
83210 6f 72 21 3d 30 20 73 69 6e 63 65 0a 2a 2a 20 63 or!=0 since.** c
83220 61 73 65 73 20 77 68 65 72 65 20 70 50 72 69 6f ases where pPrio
83230 72 3d 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 r==0 will have b
83240 65 65 6e 20 69 6e 74 65 63 65 70 74 65 64 20 61 een intecepted a
83250 6e 64 20 64 65 61 6c 74 20 77 69 74 68 0a 2a 2a nd dealt with.**
83260 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c by higher-level
83270 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 routines..*/.st
83280 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 atic void sqlite
83290 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 3MemFree(void *p
832a0 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 Prior){. sqlite
832b0 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 71 3_int64 *p = (sq
832c0 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 lite3_int64*)pPr
832d0 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ior;. assert( p
832e0 50 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 70 2d Prior!=0 );. p-
832f0 2d 3b 0a 20 20 66 72 65 65 28 70 29 3b 0a 7d 0a -;. free(p);.}.
83300 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 72 65 61 6c ./*.** Like real
83310 6c 6f 63 28 29 2e 20 20 52 65 73 69 7a 65 20 61 loc(). Resize a
83320 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 n allocation pre
83330 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 viously obtained
83340 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 from.** sqlite3
83350 4d 65 6d 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a MemMalloc()..**.
83360 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d ** For this low-
83370 6c 65 76 65 6c 20 69 6e 74 65 72 66 61 63 65 2c level interface,
83380 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 we know that pP
83390 72 69 6f 72 21 3d 30 2e 20 20 43 61 73 65 73 20 rior!=0. Cases
833a0 77 68 65 72 65 0a 2a 2a 20 70 50 72 69 6f 72 3d where.** pPrior=
833b0 3d 30 20 77 68 69 6c 65 20 68 61 76 65 20 62 65 =0 while have be
833c0 65 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 en intercepted b
833d0 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 y higher-level r
833e0 6f 75 74 69 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 outine and.** re
833f0 64 69 72 65 63 74 65 64 20 74 6f 20 78 4d 61 6c directed to xMal
83400 6c 6f 63 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c loc. Similarly,
83410 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 6e 42 we know that nB
83420 79 74 65 3e 30 20 62 65 63 61 75 73 65 73 0a 2a yte>0 becauses.*
83430 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 6e 42 * cases where nB
83440 79 74 65 3c 3d 30 20 77 69 6c 6c 20 68 61 76 65 yte<=0 will have
83450 20 62 65 65 6e 20 69 6e 74 65 72 63 65 70 74 65 been intercepte
83460 64 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 d by higher-leve
83470 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 6e l.** routines an
83480 64 20 72 65 64 69 72 65 63 74 65 64 20 74 6f 20 d redirected to
83490 78 46 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 xFree..*/.static
834a0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 void *sqlite3Me
834b0 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 mRealloc(void *p
834c0 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 Prior, int nByte
834d0 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 ){. sqlite3_int
834e0 36 34 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 64 *p = (sqlite3
834f0 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a _int64*)pPrior;.
83500 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 assert( pPrior
83510 21 3d 30 20 26 26 20 6e 42 79 74 65 3e 30 20 29 !=0 && nByte>0 )
83520 3b 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e ;. nByte = ROUN
83530 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d D8(nByte);. p =
83540 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a (sqlite3_int64*
83550 29 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a )pPrior;. p--;.
83560 20 20 70 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2c p = realloc(p,
83570 20 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 nByte+8 );. if
83580 28 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 ( p ){. p[0]
83590 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b = nByte;. p++
835a0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 ;. }. return (
835b0 76 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a void*)p;.}../*.*
835c0 2a 20 52 65 70 6f 72 74 20 74 68 65 20 61 6c 6c * Report the all
835d0 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 ocated size of a
835e0 20 70 72 69 6f 72 20 72 65 74 75 72 6e 20 66 72 prior return fr
835f0 6f 6d 20 78 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 om xMalloc().**
83600 6f 72 20 78 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a or xRealloc()..*
83610 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c /.static int sql
83620 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 ite3MemSize(void
83630 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c *pPrior){. sql
83640 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 ite3_int64 *p;.
83650 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 if( pPrior==0 )
83660 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d return 0;. p =
83670 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a (sqlite3_int64*
83680 29 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a )pPrior;. p--;.
83690 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 5b return (int)p[
836a0 30 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 0];.}../*.** Rou
836b0 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20 nd up a request
836c0 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 size to the next
836d0 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f valid allocatio
836e0 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 n size..*/.stati
836f0 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d c int sqlite3Mem
83700 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a Roundup(int n){.
83710 20 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28 return ROUND8(
83720 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 n);.}../*.** Ini
83730 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 tialize this mod
83740 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ule..*/.static i
83750 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 nt sqlite3MemIni
83760 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 t(void *NotUsed)
83770 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
83780 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
83790 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
837a0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e K;.}../*.** Dein
837b0 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f itialize this mo
837c0 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 dule..*/.static
837d0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 void sqlite3MemS
837e0 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f hutdown(void *No
837f0 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 tUsed){. UNUSED
83800 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
83810 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d ed);. return;.}
83820 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
83830 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 tine is the only
83840 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 routine in this
83850 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 file with exter
83860 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a nal linkage..**.
83870 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 ** Populate the
83880 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 low-level memory
83890 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 allocation func
838a0 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e tion pointers in
838b0 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 .** sqlite3Globa
838c0 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 lConfig.m with p
838d0 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 ointers to the r
838e0 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 outines in this
838f0 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f file..*/.SQLITE_
83900 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
83910 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c ite3MemSetDefaul
83920 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 t(void){. stati
83930 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
83940 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 mem_methods defa
83950 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 ultMethods = {.
83960 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 sqlite3MemMa
83970 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 lloc,. sqlit
83980 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 e3MemFree,.
83990 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f sqlite3MemReallo
839a0 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d c,. sqlite3M
839b0 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c emSize,. sql
839c0 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a ite3MemRoundup,.
839d0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 sqlite3MemI
839e0 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 nit,. sqlite
839f0 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 3MemShutdown,.
83a00 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 0. };. sqli
83a10 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 te3_config(SQLIT
83a20 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c E_CONFIG_MALLOC,
83a30 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 &defaultMethods
83a40 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 );.}..#endif /*
83a50 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 SQLITE_SYSTEM_MA
83a60 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a LLOC */../******
83a70 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
83a80 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mem1.c *********
83a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83ab0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
83ac0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
83ad0 69 6c 65 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a ile mem2.c *****
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 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
83b10 30 30 37 20 41 75 67 75 73 74 20 31 35 0a 2a 2a 007 August 15.**
83b20 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
83b30 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
83b40 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
83b50 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
83b60 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
83b70 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
83b80 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
83b90 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
83ba0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
83bb0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
83bc0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
83bd0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
83be0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
83bf0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
83c00 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
83c10 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
83c20 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
83c30 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
83c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
83c80 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
83c90 74 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 tains low-level
83ca0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
83cb0 6e 20 64 72 69 76 65 72 73 20 66 6f 72 20 77 68 n drivers for wh
83cc0 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c en.** SQLite wil
83cd0 6c 20 75 73 65 20 74 68 65 20 73 74 61 6e 64 61 l use the standa
83ce0 72 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c rd C-library mal
83cf0 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 loc/realloc/free
83d00 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f interface.** to
83d10 20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f obtain the memo
83d20 72 79 20 69 74 20 6e 65 65 64 73 20 77 68 69 6c ry it needs whil
83d30 65 20 61 64 64 69 6e 67 20 6c 6f 74 73 20 6f 66 e adding lots of
83d40 20 61 64 64 69 74 69 6f 6e 61 6c 20 64 65 62 75 additional debu
83d50 67 67 69 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 gging.** informa
83d60 74 69 6f 6e 20 74 6f 20 65 61 63 68 20 61 6c 6c tion to each all
83d70 6f 63 61 74 69 6f 6e 20 69 6e 20 6f 72 64 65 72 ocation in order
83d80 20 74 6f 20 68 65 6c 70 20 64 65 74 65 63 74 20 to help detect
83d90 61 6e 64 20 66 69 78 20 6d 65 6d 6f 72 79 0a 2a and fix memory.*
83da0 2a 20 6c 65 61 6b 73 20 61 6e 64 20 6d 65 6d 6f * leaks and memo
83db0 72 79 20 75 73 61 67 65 20 65 72 72 6f 72 73 2e ry usage errors.
83dc0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
83dd0 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d contains implem
83de0 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 entations of the
83df0 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 low-level memor
83e00 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 y allocation.**
83e10 72 6f 75 74 69 6e 65 73 20 73 70 65 63 69 66 69 routines specifi
83e20 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 ed in the sqlite
83e30 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 3_mem_methods ob
83e40 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a ject..**.** $Id:
83e50 20 6d 65 6d 32 2e 63 2c 76 20 31 2e 34 35 20 32 mem2.c,v 1.45 2
83e60 30 30 39 2f 30 33 2f 32 33 20 30 34 3a 33 33 3a 009/03/23 04:33:
83e70 33 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 33 danielk1977 E
83e80 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 xp $.*/../*.** T
83e90 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 his version of t
83ea0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
83eb0 74 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 tor is used only
83ec0 20 69 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 if the.** SQLIT
83ed0 45 5f 4d 45 4d 44 45 42 55 47 20 6d 61 63 72 6f E_MEMDEBUG macro
83ee0 20 69 73 20 64 65 66 69 6e 65 64 0a 2a 2f 0a 23 is defined.*/.#
83ef0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 45 4d ifdef SQLITE_MEM
83f00 44 45 42 55 47 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 DEBUG../*.** The
83f10 20 62 61 63 6b 74 72 61 63 65 20 66 75 6e 63 74 backtrace funct
83f20 69 6f 6e 61 6c 69 74 79 20 69 73 20 6f 6e 6c 79 ionality is only
83f30 20 61 76 61 69 6c 61 62 6c 65 20 77 69 74 68 20 available with
83f40 47 4c 49 42 43 0a 2a 2f 0a 23 69 66 64 65 66 20 GLIBC.*/.#ifdef
83f50 5f 5f 47 4c 49 42 43 5f 5f 0a 20 20 65 78 74 65 __GLIBC__. exte
83f60 72 6e 20 69 6e 74 20 62 61 63 6b 74 72 61 63 65 rn int backtrace
83f70 28 76 6f 69 64 2a 2a 2c 69 6e 74 29 3b 0a 20 20 (void**,int);.
83f80 65 78 74 65 72 6e 20 76 6f 69 64 20 62 61 63 6b extern void back
83f90 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 trace_symbols_fd
83fa0 28 76 6f 69 64 2a 63 6f 6e 73 74 2a 2c 69 6e 74 (void*const*,int
83fb0 2c 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 ,int);.#else.# d
83fc0 65 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65 28 efine backtrace(
83fd0 41 2c 42 29 20 31 0a 23 20 64 65 66 69 6e 65 20 A,B) 1.# define
83fe0 62 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c backtrace_symbol
83ff0 73 5f 66 64 28 41 2c 42 2c 43 29 0a 23 65 6e 64 s_fd(A,B,C).#end
84000 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6d if../*.** Each m
84010 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
84020 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 looks like this
84030 3a 0a 2a 2a 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d :.**.** -------
84040 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84050 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84060 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84070 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84080 2d 0a 2a 2a 20 20 7c 20 54 69 74 6c 65 20 7c 20 -.** | Title |
84090 20 62 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 backtrace point
840a0 65 72 73 20 7c 20 20 4d 65 6d 42 6c 6f 63 6b 48 ers | MemBlockH
840b0 64 72 20 7c 20 20 61 6c 6c 6f 63 61 74 69 6f 6e dr | allocation
840c0 20 7c 20 20 45 6e 64 47 75 61 72 64 20 7c 0a 2a | EndGuard |.*
840d0 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * -------------
840e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
840f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84100 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84110 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a -----------.**.*
84120 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f * The applicatio
84130 6e 20 63 6f 64 65 20 73 65 65 73 20 6f 6e 6c 79 n code sees only
84140 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
84150 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 57 e allocation. W
84160 65 20 68 61 76 65 0a 2a 2a 20 74 6f 20 62 61 63 e have.** to bac
84170 6b 20 75 70 20 66 72 6f 6d 20 74 68 65 20 61 6c k up from the al
84180 6c 6f 63 61 74 69 6f 6e 20 70 6f 69 6e 74 65 72 location pointer
84190 20 74 6f 20 66 69 6e 64 20 74 68 65 20 4d 65 6d to find the Mem
841a0 42 6c 6f 63 6b 48 64 72 2e 20 20 54 68 65 0a 2a BlockHdr. The.*
841b0 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 74 65 * MemBlockHdr te
841c0 6c 6c 73 20 75 73 20 74 68 65 20 73 69 7a 65 20 lls us the size
841d0 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f of the allocatio
841e0 6e 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 n and the number
841f0 20 6f 66 0a 2a 2a 20 62 61 63 6b 74 72 61 63 65 of.** backtrace
84200 20 70 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 72 pointers. Ther
84210 65 20 69 73 20 61 6c 73 6f 20 61 20 67 75 61 72 e is also a guar
84220 64 20 77 6f 72 64 20 61 74 20 74 68 65 20 65 6e d word at the en
84230 64 20 6f 66 20 74 68 65 0a 2a 2a 20 4d 65 6d 42 d of the.** MemB
84240 6c 6f 63 6b 48 64 72 2e 0a 2a 2f 0a 73 74 72 75 lockHdr..*/.stru
84250 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7b ct MemBlockHdr {
84260 0a 20 20 69 36 34 20 69 53 69 7a 65 3b 20 20 20 . i64 iSize;
84270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
84280 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
84290 66 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f f this allocatio
842a0 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 n */. struct Me
842b0 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4e 65 78 74 mBlockHdr *pNext
842c0 2c 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 4c 69 , *pPrev; /* Li
842d0 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c nked list of all
842e0 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 unfreed memory
842f0 2a 2f 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74 */. char nBackt
84300 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 race;
84310 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
84320 65 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 73 er of backtraces
84330 20 6f 6e 20 74 68 69 73 20 61 6c 6c 6f 63 20 2a on this alloc *
84340 2f 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 /. char nBacktr
84350 61 63 65 53 6c 6f 74 73 3b 20 20 20 20 20 20 20 aceSlots;
84360 20 20 20 20 20 20 20 20 2f 2a 20 41 76 61 69 6c /* Avail
84370 61 62 6c 65 20 62 61 63 6b 74 72 61 63 65 20 73 able backtrace s
84380 6c 6f 74 73 20 2a 2f 0a 20 20 73 68 6f 72 74 20 lots */. short
84390 6e 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20 20 nTitle;
843a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
843b0 20 42 79 74 65 73 20 6f 66 20 74 69 74 6c 65 3b Bytes of title;
843c0 20 69 6e 63 6c 75 64 65 73 20 27 5c 30 27 20 2a includes '\0' *
843d0 2f 0a 20 20 69 6e 74 20 69 46 6f 72 65 47 75 61 /. int iForeGua
843e0 72 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rd;
843f0 20 20 20 20 20 20 20 20 2f 2a 20 47 75 61 72 64 /* Guard
84400 20 77 6f 72 64 20 66 6f 72 20 73 61 6e 69 74 79 word for sanity
84410 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 75 */.};../*.** Gu
84420 61 72 64 20 77 6f 72 64 73 0a 2a 2f 0a 23 64 65 ard words.*/.#de
84430 66 69 6e 65 20 46 4f 52 45 47 55 41 52 44 20 30 fine FOREGUARD 0
84440 78 38 30 46 35 45 31 35 33 0a 23 64 65 66 69 6e x80F5E153.#defin
84450 65 20 52 45 41 52 47 55 41 52 44 20 30 78 45 34 e REARGUARD 0xE4
84460 36 37 36 42 35 33 0a 0a 2f 2a 0a 2a 2a 20 4e 75 676B53../*.** Nu
84470 6d 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 73 mber of malloc s
84480 69 7a 65 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 ize increments t
84490 6f 20 74 72 61 63 6b 2e 0a 2a 2f 0a 23 64 65 66 o track..*/.#def
844a0 69 6e 65 20 4e 43 53 49 5a 45 20 20 31 30 30 30 ine NCSIZE 1000
844b0 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 ../*.** All of t
844c0 68 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 he static variab
844d0 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 69 73 les used by this
844e0 20 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c module are coll
844f0 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 ected.** into a
84500 73 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65 single structure
84510 20 6e 61 6d 65 64 20 22 6d 65 6d 22 2e 20 20 54 named "mem". T
84520 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 his is to keep t
84530 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 he.** static var
84540 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 iables organized
84550 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e and to reduce n
84560 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 amespace polluti
84570 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 on.** when this
84580 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e module is combin
84590 65 64 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e ed with other in
845a0 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f the amalgamatio
845b0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 n..*/.static str
845c0 75 63 74 20 7b 0a 20 20 0a 20 20 2f 2a 0a 20 20 uct {. . /*.
845d0 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 ** Mutex to cont
845e0 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 rol access to th
845f0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
84600 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 ion subsystem..
84610 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 */. sqlite3_mu
84620 74 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f tex *mutex;.. /
84630 2a 0a 20 20 2a 2a 20 48 65 61 64 20 61 6e 64 20 *. ** Head and
84640 74 61 69 6c 20 6f 66 20 61 20 6c 69 6e 6b 65 64 tail of a linked
84650 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 75 74 list of all out
84660 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 standing allocat
84670 69 6f 6e 73 0a 20 20 2a 2f 0a 20 20 73 74 72 75 ions. */. stru
84680 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
84690 70 46 69 72 73 74 3b 0a 20 20 73 74 72 75 63 74 pFirst;. struct
846a0 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4c MemBlockHdr *pL
846b0 61 73 74 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a ast;. . /*. *
846c0 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * The number of
846d0 6c 65 76 65 6c 73 20 6f 66 20 62 61 63 6b 74 72 levels of backtr
846e0 61 63 65 20 74 6f 20 73 61 76 65 20 69 6e 20 6e ace to save in n
846f0 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a ew allocations..
84700 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b */. int nBack
84710 74 72 61 63 65 3b 0a 20 20 76 6f 69 64 20 28 2a trace;. void (*
84720 78 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c xBacktrace)(int,
84730 20 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 3b 0a int, void **);.
84740 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 69 74 6c 65 . /*. ** Title
84750 20 74 65 78 74 20 74 6f 20 69 6e 73 65 72 74 20 text to insert
84760 69 6e 20 66 72 6f 6e 74 20 6f 66 20 65 61 63 68 in front of each
84770 20 62 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 6e block. */. in
84780 74 20 6e 54 69 74 6c 65 3b 20 20 20 20 20 20 20 t nTitle;
84790 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 7a 54 69 /* Bytes of zTi
847a0 74 6c 65 20 74 6f 20 73 61 76 65 2e 20 20 49 6e tle to save. In
847b0 63 6c 75 64 65 73 20 27 5c 30 27 20 61 6e 64 20 cludes '\0' and
847c0 70 61 64 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 padding */. cha
847d0 72 20 7a 54 69 74 6c 65 5b 31 30 30 5d 3b 20 20 r zTitle[100];
847e0 2f 2a 20 54 68 65 20 74 69 74 6c 65 20 74 65 78 /* The title tex
847f0 74 20 2a 2f 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a t */.. /* . **
84800 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 44 69 sqlite3MallocDi
84810 73 61 6c 6c 6f 77 28 29 20 69 6e 63 72 65 6d 65 sallow() increme
84820 6e 74 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e nts the followin
84830 67 20 63 6f 75 6e 74 65 72 2e 0a 20 20 2a 2a 20 g counter.. **
84840 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c sqlite3MallocAll
84850 6f 77 28 29 20 64 65 63 72 65 6d 65 6e 74 73 20 ow() decrements
84860 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 64 it.. */. int d
84870 69 73 61 6c 6c 6f 77 3b 20 2f 2a 20 44 6f 20 6e isallow; /* Do n
84880 6f 74 20 61 6c 6c 6f 77 20 6d 65 6d 6f 72 79 20 ot allow memory
84890 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 0a 20 allocation */..
848a0 20 2f 2a 0a 20 20 2a 2a 20 47 61 74 68 65 72 20 /*. ** Gather
848b0 73 74 61 74 69 73 74 69 63 73 20 6f 6e 20 74 68 statistics on th
848c0 65 20 73 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 e sizes of memor
848d0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 y allocations..
848e0 20 2a 2a 20 6e 41 6c 6c 6f 63 5b 69 5d 20 69 73 ** nAlloc[i] is
848f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 the number of a
84900 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 llocation attemp
84910 74 73 20 6f 66 20 69 2a 38 0a 20 20 2a 2a 20 62 ts of i*8. ** b
84920 79 74 65 73 2e 20 20 69 3d 3d 4e 43 53 49 5a 45 ytes. i==NCSIZE
84930 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
84940 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 f allocation att
84950 65 6d 70 74 73 20 66 6f 72 0a 20 20 2a 2a 20 73 empts for. ** s
84960 69 7a 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 4e izes more than N
84970 43 53 49 5a 45 2a 38 20 62 79 74 65 73 2e 0a 20 CSIZE*8 bytes..
84980 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 */. int nAlloc
84990 5b 4e 43 53 49 5a 45 5d 3b 20 20 20 20 20 20 2f [NCSIZE]; /
849a0 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f * Total number o
849b0 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f f allocations */
849c0 0a 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 5b . int nCurrent[
849d0 4e 43 53 49 5a 45 5d 3b 20 20 20 20 2f 2a 20 43 NCSIZE]; /* C
849e0 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 urrent number of
849f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a allocations */.
84a00 20 20 69 6e 74 20 6d 78 43 75 72 72 65 6e 74 5b int mxCurrent[
84a10 4e 43 53 49 5a 45 5d 3b 20 20 20 2f 2a 20 48 69 NCSIZE]; /* Hi
84a20 67 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 ghwater mark for
84a30 20 6e 43 75 72 72 65 6e 74 20 2a 2f 0a 0a 7d 20 nCurrent */..}
84a40 6d 65 6d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a mem;.../*.** Adj
84a50 75 73 74 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 ust memory usage
84a60 20 73 74 61 74 69 73 74 69 63 73 0a 2a 2f 0a 73 statistics.*/.s
84a70 74 61 74 69 63 20 76 6f 69 64 20 61 64 6a 75 73 tatic void adjus
84a80 74 53 74 61 74 73 28 69 6e 74 20 69 53 69 7a 65 tStats(int iSize
84a90 2c 20 69 6e 74 20 69 6e 63 72 65 6d 65 6e 74 29 , int increment)
84aa0 7b 0a 20 20 69 6e 74 20 69 20 3d 20 52 4f 55 4e {. int i = ROUN
84ab0 44 38 28 69 53 69 7a 65 29 2f 38 3b 0a 20 20 69 D8(iSize)/8;. i
84ac0 66 28 20 69 3e 4e 43 53 49 5a 45 2d 31 20 29 7b f( i>NCSIZE-1 ){
84ad0 0a 20 20 20 20 69 20 3d 20 4e 43 53 49 5a 45 20 . i = NCSIZE
84ae0 2d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 - 1;. }. if( i
84af0 6e 63 72 65 6d 65 6e 74 3e 30 20 29 7b 0a 20 20 ncrement>0 ){.
84b00 20 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2b mem.nAlloc[i]+
84b10 2b 3b 0a 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72 +;. mem.nCurr
84b20 65 6e 74 5b 69 5d 2b 2b 3b 0a 20 20 20 20 69 66 ent[i]++;. if
84b30 28 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 ( mem.nCurrent[i
84b40 5d 3e 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b ]>mem.mxCurrent[
84b50 69 5d 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e i] ){. mem.
84b60 6d 78 43 75 72 72 65 6e 74 5b 69 5d 20 3d 20 6d mxCurrent[i] = m
84b70 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3b 0a em.nCurrent[i];.
84b80 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
84b90 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b mem.nCurrent[
84ba0 69 5d 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 i]--;. assert
84bb0 28 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 ( mem.nCurrent[i
84bc0 5d 3e 3d 30 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f ]>=0 );. }.}../
84bd0 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 61 6c *.** Given an al
84be0 6c 6f 63 61 74 69 6f 6e 2c 20 66 69 6e 64 20 74 location, find t
84bf0 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 66 he MemBlockHdr f
84c00 6f 72 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 69 or that allocati
84c10 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 on..**.** This r
84c20 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 outine checks th
84c30 65 20 67 75 61 72 64 73 20 61 74 20 65 69 74 68 e guards at eith
84c40 65 72 20 65 6e 64 20 6f 66 20 74 68 65 20 61 6c er end of the al
84c50 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 location and.**
84c60 69 66 20 74 68 65 79 20 61 72 65 20 69 6e 63 6f if they are inco
84c70 72 72 65 63 74 20 69 74 20 61 73 73 65 72 74 73 rrect it asserts
84c80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 ..*/.static stru
84c90 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
84ca0 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 sqlite3MemsysGet
84cb0 48 65 61 64 65 72 28 76 6f 69 64 20 2a 70 41 6c Header(void *pAl
84cc0 6c 6f 63 61 74 69 6f 6e 29 7b 0a 20 20 73 74 72 location){. str
84cd0 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 uct MemBlockHdr
84ce0 2a 70 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b *p;. int *pInt;
84cf0 0a 20 20 75 38 20 2a 70 55 38 3b 0a 20 20 69 6e . u8 *pU8;. in
84d00 74 20 6e 52 65 73 65 72 76 65 3b 0a 0a 20 20 70 t nReserve;.. p
84d10 20 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c = (struct MemBl
84d20 6f 63 6b 48 64 72 2a 29 70 41 6c 6c 6f 63 61 74 ockHdr*)pAllocat
84d30 69 6f 6e 3b 0a 20 20 70 2d 2d 3b 0a 20 20 61 73 ion;. p--;. as
84d40 73 65 72 74 28 20 70 2d 3e 69 46 6f 72 65 47 75 sert( p->iForeGu
84d50 61 72 64 3d 3d 28 69 6e 74 29 46 4f 52 45 47 55 ard==(int)FOREGU
84d60 41 52 44 20 29 3b 0a 20 20 6e 52 65 73 65 72 76 ARD );. nReserv
84d70 65 20 3d 20 52 4f 55 4e 44 38 28 70 2d 3e 69 53 e = ROUND8(p->iS
84d80 69 7a 65 29 3b 0a 20 20 70 49 6e 74 20 3d 20 28 ize);. pInt = (
84d90 69 6e 74 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e int*)pAllocation
84da0 3b 0a 20 20 70 55 38 20 3d 20 28 75 38 2a 29 70 ;. pU8 = (u8*)p
84db0 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 61 73 Allocation;. as
84dc0 73 65 72 74 28 20 70 49 6e 74 5b 6e 52 65 73 65 sert( pInt[nRese
84dd0 72 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d rve/sizeof(int)]
84de0 3d 3d 28 69 6e 74 29 52 45 41 52 47 55 41 52 44 ==(int)REARGUARD
84df0 20 29 3b 0a 20 20 2f 2a 20 54 68 69 73 20 63 68 );. /* This ch
84e00 65 63 6b 73 20 61 6e 79 20 6f 66 20 74 68 65 20 ecks any of the
84e10 22 65 78 74 72 61 22 20 62 79 74 65 73 20 61 6c "extra" bytes al
84e20 6c 6f 63 61 74 65 64 20 64 75 65 0a 20 20 2a 2a located due. **
84e30 20 74 6f 20 72 6f 75 6e 64 69 6e 67 20 75 70 20 to rounding up
84e40 74 6f 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 to an 8 byte bou
84e50 6e 64 61 72 79 20 74 6f 20 65 6e 73 75 72 65 20 ndary to ensure
84e60 0a 20 20 2a 2a 20 74 68 65 79 20 68 61 76 65 6e . ** they haven
84e70 27 74 20 62 65 65 6e 20 6f 76 65 72 77 72 69 74 't been overwrit
84e80 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c ten.. */. whil
84e90 65 28 20 6e 52 65 73 65 72 76 65 2d 2d 20 3e 20 e( nReserve-- >
84ea0 70 2d 3e 69 53 69 7a 65 20 29 20 61 73 73 65 72 p->iSize ) asser
84eb0 74 28 20 70 55 38 5b 6e 52 65 73 65 72 76 65 5d t( pU8[nReserve]
84ec0 3d 3d 30 78 36 35 20 29 3b 0a 20 20 72 65 74 75 ==0x65 );. retu
84ed0 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rn p;.}../*.** R
84ee0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
84ef0 20 6f 66 20 62 79 74 65 73 20 63 75 72 72 65 6e of bytes curren
84f00 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 tly allocated at
84f10 20 61 64 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 address p..*/.s
84f20 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 tatic int sqlite
84f30 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 3MemSize(void *p
84f40 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 ){. struct MemB
84f50 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 lockHdr *pHdr;.
84f60 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 if( !p ){. r
84f70 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 eturn 0;. }. p
84f80 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d Hdr = sqlite3Mem
84f90 73 79 73 47 65 74 48 65 61 64 65 72 28 70 29 3b sysGetHeader(p);
84fa0 0a 20 20 72 65 74 75 72 6e 20 70 48 64 72 2d 3e . return pHdr->
84fb0 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 iSize;.}../*.**
84fc0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d Initialize the m
84fd0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
84fe0 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 subsystem..*/.s
84ff0 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 tatic int sqlite
85000 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 3MemInit(void *N
85010 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 otUsed){. UNUSE
85020 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
85030 73 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 sed);. assert(
85040 28 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 4d (sizeof(struct M
85050 65 6d 42 6c 6f 63 6b 48 64 72 29 26 37 29 20 3d emBlockHdr)&7) =
85060 3d 20 30 20 29 3b 0a 20 20 69 66 28 20 21 73 71 = 0 );. if( !sq
85070 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
85080 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 g.bMemstat ){.
85090 20 20 2f 2a 20 49 66 20 6d 65 6d 6f 72 79 20 73 /* If memory s
850a0 74 61 74 75 73 20 69 73 20 65 6e 61 62 6c 65 64 tatus is enabled
850b0 2c 20 74 68 65 6e 20 74 68 65 20 6d 61 6c 6c 6f , then the mallo
850c0 63 2e 63 20 77 72 61 70 70 65 72 20 77 69 6c 6c c.c wrapper will
850d0 20 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 already. **
850e0 68 6f 6c 64 20 74 68 65 20 53 54 41 54 49 43 5f hold the STATIC_
850f0 4d 45 4d 20 6d 75 74 65 78 20 77 68 65 6e 20 74 MEM mutex when t
85100 68 65 20 72 6f 75 74 69 6e 65 73 20 68 65 72 65 he routines here
85110 20 61 72 65 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f are invoked. */
85120 0a 20 20 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d . mem.mutex =
85130 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
85140 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
85150 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d STATIC_MEM);. }
85160 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
85170 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 _OK;.}../*.** De
85180 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d initialize the m
85190 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
851a0 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 subsystem..*/.s
851b0 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 tatic void sqlit
851c0 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f e3MemShutdown(vo
851d0 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 id *NotUsed){.
851e0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
851f0 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d (NotUsed);. mem
85200 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 7d 0a 0a 2f .mutex = 0;.}../
85210 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 *.** Round up a
85220 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 request size to
85230 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 the next valid a
85240 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a llocation size..
85250 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 */.static int sq
85260 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 lite3MemRoundup(
85270 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e int n){. return
85280 20 52 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f ROUND8(n);.}../
85290 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 *.** Allocate nB
852a0 79 74 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d yte bytes of mem
852b0 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ory..*/.static v
852c0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d oid *sqlite3MemM
852d0 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 alloc(int nByte)
852e0 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c {. struct MemBl
852f0 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 ockHdr *pHdr;.
85300 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 void **pBt;. ch
85310 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 2a 70 49 ar *z;. int *pI
85320 6e 74 3b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 nt;. void *p =
85330 30 3b 0a 20 20 69 6e 74 20 74 6f 74 61 6c 53 69 0;. int totalSi
85340 7a 65 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 ze;. int nReser
85350 76 65 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 ve;. sqlite3_mu
85360 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 tex_enter(mem.mu
85370 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 tex);. assert(
85380 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 mem.disallow==0
85390 29 3b 0a 20 20 6e 52 65 73 65 72 76 65 20 3d 20 );. nReserve =
853a0 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 ROUND8(nByte);.
853b0 20 74 6f 74 61 6c 53 69 7a 65 20 3d 20 6e 52 65 totalSize = nRe
853c0 73 65 72 76 65 20 2b 20 73 69 7a 65 6f 66 28 2a serve + sizeof(*
853d0 70 48 64 72 29 20 2b 20 73 69 7a 65 6f 66 28 69 pHdr) + sizeof(i
853e0 6e 74 29 20 2b 0a 20 20 20 20 20 20 20 20 20 20 nt) +.
853f0 20 20 20 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 mem.nBacktr
85400 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a ace*sizeof(void*
85410 29 20 2b 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a ) + mem.nTitle;.
85420 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 74 6f 74 p = malloc(tot
85430 61 6c 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 alSize);. if( p
85440 20 29 7b 0a 20 20 20 20 7a 20 3d 20 70 3b 0a 20 ){. z = p;.
85450 20 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a pBt = (void**
85460 29 26 7a 5b 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b )&z[mem.nTitle];
85470 0a 20 20 20 20 70 48 64 72 20 3d 20 28 73 74 72 . pHdr = (str
85480 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a uct MemBlockHdr*
85490 29 26 70 42 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74 )&pBt[mem.nBackt
854a0 72 61 63 65 5d 3b 0a 20 20 20 20 70 48 64 72 2d race];. pHdr-
854b0 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 >pNext = 0;.
854c0 70 48 64 72 2d 3e 70 50 72 65 76 20 3d 20 6d 65 pHdr->pPrev = me
854d0 6d 2e 70 4c 61 73 74 3b 0a 20 20 20 20 69 66 28 m.pLast;. if(
854e0 20 6d 65 6d 2e 70 4c 61 73 74 20 29 7b 0a 20 20 mem.pLast ){.
854f0 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 2d 3e 70 mem.pLast->p
85500 4e 65 78 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 Next = pHdr;.
85510 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 }else{. me
85520 6d 2e 70 46 69 72 73 74 20 3d 20 70 48 64 72 3b m.pFirst = pHdr;
85530 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 2e 70 . }. mem.p
85540 4c 61 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 Last = pHdr;.
85550 20 70 48 64 72 2d 3e 69 46 6f 72 65 47 75 61 72 pHdr->iForeGuar
85560 64 20 3d 20 46 4f 52 45 47 55 41 52 44 3b 0a 20 d = FOREGUARD;.
85570 20 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 pHdr->nBacktr
85580 61 63 65 53 6c 6f 74 73 20 3d 20 6d 65 6d 2e 6e aceSlots = mem.n
85590 42 61 63 6b 74 72 61 63 65 3b 0a 20 20 20 20 70 Backtrace;. p
855a0 48 64 72 2d 3e 6e 54 69 74 6c 65 20 3d 20 6d 65 Hdr->nTitle = me
855b0 6d 2e 6e 54 69 74 6c 65 3b 0a 20 20 20 20 69 66 m.nTitle;. if
855c0 28 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 ( mem.nBacktrace
855d0 20 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a ){. void *
855e0 61 41 64 64 72 5b 34 30 5d 3b 0a 20 20 20 20 20 aAddr[40];.
855f0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
85600 65 20 3d 20 62 61 63 6b 74 72 61 63 65 28 61 41 e = backtrace(aA
85610 64 64 72 2c 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 ddr, mem.nBacktr
85620 61 63 65 2b 31 29 2d 31 3b 0a 20 20 20 20 20 20 ace+1)-1;.
85630 6d 65 6d 63 70 79 28 70 42 74 2c 20 26 61 41 64 memcpy(pBt, &aAd
85640 64 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 42 61 dr[1], pHdr->nBa
85650 63 6b 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 cktrace*sizeof(v
85660 6f 69 64 2a 29 29 3b 0a 20 20 20 20 20 20 61 73 oid*));. as
85670 73 65 72 74 28 70 42 74 5b 30 5d 29 3b 0a 20 20 sert(pBt[0]);.
85680 20 20 20 20 69 66 28 20 6d 65 6d 2e 78 42 61 63 if( mem.xBac
85690 6b 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 ktrace ){.
856a0 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 mem.xBacktrace
856b0 28 6e 42 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42 (nByte, pHdr->nB
856c0 61 63 6b 74 72 61 63 65 2d 31 2c 20 26 61 41 64 acktrace-1, &aAd
856d0 64 72 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a dr[1]);. }.
856e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
856f0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
85700 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 e = 0;. }.
85710 20 69 66 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 if( mem.nTitle
85720 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ){. memcpy(
85730 7a 2c 20 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d z, mem.zTitle, m
85740 65 6d 2e 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20 em.nTitle);.
85750 7d 0a 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a }. pHdr->iSiz
85760 65 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 e = nByte;. a
85770 64 6a 75 73 74 53 74 61 74 73 28 6e 42 79 74 65 djustStats(nByte
85780 2c 20 2b 31 29 3b 0a 20 20 20 20 70 49 6e 74 20 , +1);. pInt
85790 3d 20 28 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d = (int*)&pHdr[1]
857a0 3b 0a 20 20 20 20 70 49 6e 74 5b 6e 52 65 73 65 ;. pInt[nRese
857b0 72 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d rve/sizeof(int)]
857c0 20 3d 20 52 45 41 52 47 55 41 52 44 3b 0a 20 20 = REARGUARD;.
857d0 20 20 6d 65 6d 73 65 74 28 70 49 6e 74 2c 20 30 memset(pInt, 0
857e0 78 36 35 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a x65, nReserve);.
857f0 20 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 70 p = (void*)p
85800 49 6e 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 Int;. }. sqlit
85810 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
85820 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 em.mutex);. ret
85830 75 72 6e 20 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a urn p; .}../*.**
85840 20 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f Free memory..*/
85850 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c .static void sql
85860 69 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 ite3MemFree(void
85870 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 74 72 *pPrior){. str
85880 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 uct MemBlockHdr
85890 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a *pHdr;. void **
858a0 70 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a pBt;. char *z;.
858b0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
858c0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 3GlobalConfig.bM
858d0 65 6d 73 74 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 emstat || mem.mu
858e0 74 65 78 21 3d 30 20 29 3b 0a 20 20 70 48 64 72 tex!=0 );. pHdr
858f0 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 = sqlite3Memsys
85900 47 65 74 48 65 61 64 65 72 28 70 50 72 69 6f 72 GetHeader(pPrior
85910 29 3b 0a 20 20 70 42 74 20 3d 20 28 76 6f 69 64 );. pBt = (void
85920 2a 2a 29 70 48 64 72 3b 0a 20 20 70 42 74 20 2d **)pHdr;. pBt -
85930 3d 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 = pHdr->nBacktra
85940 63 65 53 6c 6f 74 73 3b 0a 20 20 73 71 6c 69 74 ceSlots;. sqlit
85950 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
85960 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 em.mutex);. if(
85970 20 70 48 64 72 2d 3e 70 50 72 65 76 20 29 7b 0a pHdr->pPrev ){.
85980 20 20 20 20 61 73 73 65 72 74 28 20 70 48 64 72 assert( pHdr
85990 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d ->pPrev->pNext==
859a0 70 48 64 72 20 29 3b 0a 20 20 20 20 70 48 64 72 pHdr );. pHdr
859b0 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d ->pPrev->pNext =
859c0 20 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 pHdr->pNext;.
859d0 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
859e0 74 28 20 6d 65 6d 2e 70 46 69 72 73 74 3d 3d 70 t( mem.pFirst==p
859f0 48 64 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 Hdr );. mem.p
85a00 46 69 72 73 74 20 3d 20 70 48 64 72 2d 3e 70 4e First = pHdr->pN
85a10 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 ext;. }. if( p
85a20 48 64 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 Hdr->pNext ){.
85a30 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e assert( pHdr->
85a40 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 48 pNext->pPrev==pH
85a50 64 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e dr );. pHdr->
85a60 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 pNext->pPrev = p
85a70 48 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 65 Hdr->pPrev;. }e
85a80 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
85a90 20 6d 65 6d 2e 70 4c 61 73 74 3d 3d 70 48 64 72 mem.pLast==pHdr
85aa0 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 );. mem.pLas
85ab0 74 20 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b t = pHdr->pPrev;
85ac0 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 . }. z = (char
85ad0 2a 29 70 42 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 *)pBt;. z -= pH
85ae0 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 61 64 dr->nTitle;. ad
85af0 6a 75 73 74 53 74 61 74 73 28 70 48 64 72 2d 3e justStats(pHdr->
85b00 69 53 69 7a 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 iSize, -1);. me
85b10 6d 73 65 74 28 7a 2c 20 30 78 32 62 2c 20 73 69 mset(z, 0x2b, si
85b20 7a 65 6f 66 28 76 6f 69 64 2a 29 2a 70 48 64 72 zeof(void*)*pHdr
85b30 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 ->nBacktraceSlot
85b40 73 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 s + sizeof(*pHdr
85b50 29 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ) +.
85b60 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a pHdr->iSiz
85b70 65 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 e + sizeof(int)
85b80 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b + pHdr->nTitle);
85b90 0a 20 20 66 72 65 65 28 7a 29 3b 0a 20 20 73 71 . free(z);. sq
85ba0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
85bb0 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a e(mem.mutex); .
85bc0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 }../*.** Change
85bd0 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 the size of an e
85be0 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 xisting memory a
85bf0 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a llocation..**.**
85c00 20 46 6f 72 20 74 68 69 73 20 64 65 62 75 67 67 For this debugg
85c10 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ing implementati
85c20 6f 6e 2c 20 77 65 20 2a 61 6c 77 61 79 73 2a 20 on, we *always*
85c30 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 make a copy of t
85c40 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e he.** allocation
85c50 20 69 6e 74 6f 20 61 20 6e 65 77 20 70 6c 61 63 into a new plac
85c60 65 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e e in memory. In
85c70 20 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 this way, if th
85c80 65 20 0a 2a 2a 20 68 69 67 68 65 72 20 6c 65 76 e .** higher lev
85c90 65 6c 20 63 6f 64 65 20 69 73 20 75 73 69 6e 67 el code is using
85ca0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
85cb0 6f 6c 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 old allocation,
85cc0 69 74 20 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d it is .** much m
85cd0 6f 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72 ore likely to br
85ce0 65 61 6b 20 61 6e 64 20 77 65 20 61 72 65 20 6d eak and we are m
85cf0 75 63 68 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 uch more liking
85d00 74 6f 20 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65 to find.** the e
85d10 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rror..*/.static
85d20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d void *sqlite3Mem
85d30 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 Realloc(void *pP
85d40 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 rior, int nByte)
85d50 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c {. struct MemBl
85d60 6f 63 6b 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b ockHdr *pOldHdr;
85d70 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 . void *pNew;.
85d80 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 assert( mem.dis
85d90 61 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 4f allow==0 );. pO
85da0 6c 64 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d ldHdr = sqlite3M
85db0 65 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 70 emsysGetHeader(p
85dc0 50 72 69 6f 72 29 3b 0a 20 20 70 4e 65 77 20 3d Prior);. pNew =
85dd0 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f sqlite3MemMallo
85de0 63 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 c(nByte);. if(
85df0 70 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 pNew ){. memc
85e00 70 79 28 70 4e 65 77 2c 20 70 50 72 69 6f 72 2c py(pNew, pPrior,
85e10 20 6e 42 79 74 65 3c 70 4f 6c 64 48 64 72 2d 3e nByte<pOldHdr->
85e20 69 53 69 7a 65 20 3f 20 6e 42 79 74 65 20 3a 20 iSize ? nByte :
85e30 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b pOldHdr->iSize);
85e40 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3e 70 . if( nByte>p
85e50 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 29 7b OldHdr->iSize ){
85e60 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 28 . memset(&(
85e70 28 63 68 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c (char*)pNew)[pOl
85e80 64 48 64 72 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 dHdr->iSize], 0x
85e90 32 62 2c 20 6e 42 79 74 65 20 2d 20 70 4f 6c 64 2b, nByte - pOld
85ea0 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 Hdr->iSize);.
85eb0 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 }. sqlite3Me
85ec0 6d 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 mFree(pPrior);.
85ed0 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 }. return pNew
85ee0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c ;.}../*.** Popul
85ef0 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 ate the low-leve
85f00 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 l memory allocat
85f10 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 ion function poi
85f20 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 nters in.** sqli
85f30 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
85f40 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 m with pointers
85f50 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 to the routines
85f60 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f in this file..*/
85f70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
85f80 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 void sqlite3MemS
85f90 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b etDefault(void){
85fa0 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
85fb0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
85fc0 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f ods defaultMetho
85fd0 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 ds = {. sqli
85fe0 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 te3MemMalloc,.
85ff0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 sqlite3MemFre
86000 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d e,. sqlite3M
86010 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 emRealloc,.
86020 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a sqlite3MemSize,.
86030 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 sqlite3MemR
86040 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c oundup,. sql
86050 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 ite3MemInit,.
86060 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 sqlite3MemShut
86070 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d down,. 0. }
86080 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ;. sqlite3_conf
86090 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ig(SQLITE_CONFIG
860a0 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c _MALLOC, &defaul
860b0 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a tMethods);.}../*
860c0 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 .** Set the numb
860d0 65 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 20 er of backtrace
860e0 6c 65 76 65 6c 73 20 6b 65 70 74 20 66 6f 72 20 levels kept for
860f0 65 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e each allocation.
86100 0a 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 7a .** A value of z
86110 65 72 6f 20 74 75 72 6e 73 20 6f 66 66 20 62 61 ero turns off ba
86120 63 6b 74 72 61 63 69 6e 67 2e 20 20 54 68 65 20 cktracing. The
86130 6e 75 6d 62 65 72 20 69 73 20 61 6c 77 61 79 73 number is always
86140 20 72 6f 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74 rounded.** up t
86150 6f 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 o a multiple of
86160 32 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 2..*/.SQLITE_PRI
86170 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
86180 33 4d 65 6d 64 65 62 75 67 42 61 63 6b 74 72 61 3MemdebugBacktra
86190 63 65 28 69 6e 74 20 64 65 70 74 68 29 7b 0a 20 ce(int depth){.
861a0 20 69 66 28 20 64 65 70 74 68 3c 30 20 29 7b 20 if( depth<0 ){
861b0 64 65 70 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69 depth = 0; }. i
861c0 66 28 20 64 65 70 74 68 3e 32 30 20 29 7b 20 64 f( depth>20 ){ d
861d0 65 70 74 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64 epth = 20; }. d
861e0 65 70 74 68 20 3d 20 28 64 65 70 74 68 2b 31 29 epth = (depth+1)
861f0 26 30 78 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 &0xfe;. mem.nBa
86200 63 6b 74 72 61 63 65 20 3d 20 64 65 70 74 68 3b cktrace = depth;
86210 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .}..SQLITE_PRIVA
86220 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d TE void sqlite3M
86230 65 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 emdebugBacktrace
86240 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a Callback(void (*
86250 78 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c xBacktrace)(int,
86260 20 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b int, void **)){
86270 0a 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 . mem.xBacktrac
86280 65 20 3d 20 78 42 61 63 6b 74 72 61 63 65 3b 0a e = xBacktrace;.
86290 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 }../*.** Set the
862a0 20 74 69 74 6c 65 20 73 74 72 69 6e 67 20 66 6f title string fo
862b0 72 20 73 75 62 73 65 71 75 65 6e 74 20 61 6c 6c r subsequent all
862c0 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c ocations..*/.SQL
862d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
862e0 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 sqlite3Memdebug
862f0 53 65 74 74 69 74 6c 65 28 63 6f 6e 73 74 20 63 Settitle(const c
86300 68 61 72 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 har *zTitle){.
86310 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 20 3d unsigned int n =
86320 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
86330 28 7a 54 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 (zTitle) + 1;.
86340 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
86350 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a ter(mem.mutex);.
86360 20 20 69 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 if( n>=sizeof(
86370 6d 65 6d 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20 mem.zTitle) ) n
86380 3d 20 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 = sizeof(mem.zTi
86390 74 6c 65 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 tle)-1;. memcpy
863a0 28 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 (mem.zTitle, zTi
863b0 74 6c 65 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a tle, n);. mem.z
863c0 54 69 74 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 Title[n] = 0;.
863d0 6d 65 6d 2e 6e 54 69 74 6c 65 20 3d 20 52 4f 55 mem.nTitle = ROU
863e0 4e 44 38 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 ND8(n);. sqlite
863f0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
86400 6d 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c m.mutex);.}..SQL
86410 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
86420 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 sqlite3Memdebug
86430 53 79 6e 63 28 29 7b 0a 20 20 73 74 72 75 63 74 Sync(){. struct
86440 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 MemBlockHdr *pH
86450 64 72 3b 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d dr;. for(pHdr=m
86460 65 6d 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b em.pFirst; pHdr;
86470 20 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 pHdr=pHdr->pNex
86480 74 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 2a 70 t){. void **p
86490 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 Bt = (void**)pHd
864a0 72 3b 0a 20 20 20 20 70 42 74 20 2d 3d 20 70 48 r;. pBt -= pH
864b0 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c dr->nBacktraceSl
864c0 6f 74 73 3b 0a 20 20 20 20 6d 65 6d 2e 78 42 61 ots;. mem.xBa
864d0 63 6b 74 72 61 63 65 28 70 48 64 72 2d 3e 69 53 cktrace(pHdr->iS
864e0 69 7a 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b ize, pHdr->nBack
864f0 74 72 61 63 65 2d 31 2c 20 26 70 42 74 5b 31 5d trace-1, &pBt[1]
86500 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
86510 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e Open the file in
86520 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 dicated and writ
86530 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 e a log of all u
86540 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a nfreed memory .*
86550 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e * allocations in
86560 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a to that log..*/.
86570 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
86580 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 oid sqlite3Memde
86590 62 75 67 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 bugDump(const ch
865a0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a ar *zFilename){.
865b0 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 FILE *out;. s
865c0 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 truct MemBlockHd
865d0 72 20 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 r *pHdr;. void
865e0 2a 2a 70 42 74 3b 0a 20 20 69 6e 74 20 69 3b 0a **pBt;. int i;.
865f0 20 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 out = fopen(zF
86600 69 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 ilename, "w");.
86610 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 if( out==0 ){.
86620 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 fprintf(stder
86630 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f r, "** Unable to
86640 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 output memory d
86650 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a ebug output log:
86660 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 %s **\n",.
86670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a z
86680 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 72 Filename);. r
86690 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 eturn;. }. for
866a0 28 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 (pHdr=mem.pFirst
866b0 3b 20 70 48 64 72 3b 20 70 48 64 72 3d 70 48 64 ; pHdr; pHdr=pHd
866c0 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 r->pNext){. c
866d0 68 61 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 har *z = (char*)
866e0 70 48 64 72 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 pHdr;. z -= p
866f0 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 Hdr->nBacktraceS
86700 6c 6f 74 73 2a 73 69 7a 65 6f 66 28 76 6f 69 64 lots*sizeof(void
86710 2a 29 20 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c *) + pHdr->nTitl
86720 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f e;. fprintf(o
86730 75 74 2c 20 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 ut, "**** %lld b
86740 79 74 65 73 20 61 74 20 25 70 20 66 72 6f 6d 20 ytes at %p from
86750 25 73 20 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 %s ****\n", .
86760 20 20 20 20 20 20 20 20 20 70 48 64 72 2d 3e 69 pHdr->i
86770 53 69 7a 65 2c 20 26 70 48 64 72 5b 31 5d 2c 20 Size, &pHdr[1],
86780 70 48 64 72 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a pHdr->nTitle ? z
86790 20 3a 20 22 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 : "???");. i
867a0 66 28 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 f( pHdr->nBacktr
867b0 61 63 65 20 29 7b 0a 20 20 20 20 20 20 66 66 6c ace ){. ffl
867c0 75 73 68 28 6f 75 74 29 3b 0a 20 20 20 20 20 20 ush(out);.
867d0 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 pBt = (void**)pH
867e0 64 72 3b 0a 20 20 20 20 20 20 70 42 74 20 2d 3d dr;. pBt -=
867f0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
86800 65 53 6c 6f 74 73 3b 0a 20 20 20 20 20 20 62 61 eSlots;. ba
86810 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f cktrace_symbols_
86820 66 64 28 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42 fd(pBt, pHdr->nB
86830 61 63 6b 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f acktrace, fileno
86840 28 6f 75 74 29 29 3b 0a 20 20 20 20 20 20 66 70 (out));. fp
86850 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 rintf(out, "\n")
86860 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 70 ;. }. }. fp
86870 72 69 6e 74 66 28 6f 75 74 2c 20 22 43 4f 55 4e rintf(out, "COUN
86880 54 53 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 TS:\n");. for(i
86890 3d 30 3b 20 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 =0; i<NCSIZE-1;
868a0 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 i++){. if( me
868b0 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 m.nAlloc[i] ){.
868c0 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
868d0 2c 20 22 20 20 20 25 35 64 3a 20 25 31 30 64 20 , " %5d: %10d
868e0 25 31 30 64 20 25 31 30 64 5c 6e 22 2c 20 0a 20 %10d %10d\n", .
868f0 20 20 20 20 20 20 20 20 20 20 20 69 2a 38 2c 20 i*8,
86900 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d mem.nAlloc[i], m
86910 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2c 20 em.nCurrent[i],
86920 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d mem.mxCurrent[i]
86930 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 );. }. }. i
86940 66 28 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 f( mem.nAlloc[NC
86950 53 49 5a 45 2d 31 5d 20 29 7b 0a 20 20 20 20 66 SIZE-1] ){. f
86960 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 printf(out, "
86970 25 35 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 %5d: %10d %10d %
86980 31 30 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 10d\n",.
86990 20 20 20 20 20 4e 43 53 49 5a 45 2a 38 2d 38 2c NCSIZE*8-8,
869a0 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 mem.nAlloc[NCSI
869b0 5a 45 2d 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 ZE-1],.
869c0 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 mem.nCurrent
869d0 5b 4e 43 53 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e [NCSIZE-1], mem.
869e0 6d 78 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 mxCurrent[NCSIZE
869f0 2d 31 5d 29 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f -1]);. }. fclo
86a00 73 65 28 6f 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a se(out);.}../*.*
86a10 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
86a20 62 65 72 20 6f 66 20 74 69 6d 65 73 20 73 71 6c ber of times sql
86a30 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 ite3MemMalloc()
86a40 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e has been called.
86a50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
86a60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 TE int sqlite3Me
86a70 6d 64 65 62 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e mdebugMallocCoun
86a80 74 28 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 t(){. int i;.
86a90 69 6e 74 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a int nTotal = 0;.
86aa0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 for(i=0; i<NCS
86ab0 49 5a 45 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e IZE; i++){. n
86ac0 54 6f 74 61 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c Total += mem.nAl
86ad0 6c 6f 63 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 loc[i];. }. re
86ae0 74 75 72 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a turn nTotal;.}..
86af0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
86b00 45 5f 4d 45 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f E_MEMDEBUG */../
86b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
86b20 6e 64 20 6f 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a nd of mem2.c ***
86b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
86b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
86b70 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 33 2e 63 egin file mem3.c
86b80 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
86b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
86bb0 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 *.** 2007 Octobe
86bc0 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 14.**.** The a
86bd0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
86be0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
86bf0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
86c00 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
86c10 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
86c20 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
86c30 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
86c40 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
86c50 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
86c60 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
86c70 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
86c80 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
86c90 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
86ca0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
86cb0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
86cc0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
86cd0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
86ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86d20 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
86d30 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 contains the C
86d40 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 functions that i
86d50 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 mplement a memor
86d60 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 y.** allocation
86d70 73 75 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 subsystem for us
86d80 65 20 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a e by SQLite. .**
86d90 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
86da0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
86db0 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
86dc0 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a tem omits all.**
86dd0 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 use of malloc()
86de0 2e 20 54 68 65 20 53 51 4c 69 74 65 20 75 73 65 . The SQLite use
86df0 72 20 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f r supplies a blo
86e00 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 ck of memory.**
86e10 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 before calling s
86e20 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
86e30 65 28 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 e() from which a
86e40 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 llocations.** ar
86e50 65 20 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 e made and retur
86e60 6e 65 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c ned by the xMall
86e70 6f 63 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f oc() and xReallo
86e80 63 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e c() .** implemen
86e90 74 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 tations. Once sq
86ea0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
86eb0 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c () has been call
86ec0 65 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e ed,.** the amoun
86ed0 74 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 t of memory avai
86ee0 6c 61 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 lable to SQLite
86ef0 69 73 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e is fixed and can
86f00 6e 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 not.** be change
86f10 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 d..**.** This ve
86f20 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d rsion of the mem
86f30 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 ory allocation s
86f40 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c ubsystem is incl
86f50 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 uded.** in the b
86f60 75 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c uild only if SQL
86f70 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 ITE_ENABLE_MEMSY
86f80 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a S3 is defined..*
86f90 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 33 2e 63 *.** $Id: mem3.c
86fa0 2c 76 20 31 2e 32 35 20 32 30 30 38 2f 31 31 2f ,v 1.25 2008/11/
86fb0 31 39 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 19 16:52:44 dani
86fc0 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f elk1977 Exp $.*/
86fd0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 ../*.** This ver
86fe0 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f sion of the memo
86ff0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 ry allocator is
87000 6f 6e 6c 79 20 62 75 69 6c 74 20 69 6e 74 6f 20 only built into
87010 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 53 the library.** S
87020 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
87030 53 59 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e SYS3 is defined.
87040 20 44 65 66 69 6e 69 6e 67 20 74 68 69 73 20 73 Defining this s
87050 79 6d 62 6f 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a ymbol does not.*
87060 2a 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 * mean that the
87070 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 75 73 65 library will use
87080 20 61 20 6d 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 a memory-pool b
87090 79 20 64 65 66 61 75 6c 74 2c 20 6a 75 73 74 20 y default, just
870a0 74 68 61 74 0a 2a 2a 20 69 74 20 69 73 20 61 76 that.** it is av
870b0 61 69 6c 61 62 6c 65 2e 20 54 68 65 20 6d 65 6d ailable. The mem
870c0 70 6f 6f 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 69 pool allocator i
870d0 73 20 61 63 74 69 76 61 74 65 64 20 62 79 20 63 s activated by c
870e0 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 alling.** sqlite
870f0 33 5f 63 6f 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 3_config()..*/.#
87100 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
87110 42 4c 45 5f 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a BLE_MEMSYS3../*.
87120 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 ** Maximum size
87130 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 (in Mem3Blocks)
87140 6f 66 20 61 20 22 73 6d 61 6c 6c 22 20 63 68 75 of a "small" chu
87150 6e 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d nk..*/.#define M
87160 58 5f 53 4d 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a X_SMALL 10.../*.
87170 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 ** Number of fre
87180 65 6c 69 73 74 20 68 61 73 68 20 73 6c 6f 74 73 elist hash slots
87190 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 48 41 .*/.#define N_HA
871a0 53 48 20 20 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 SH 61../*.** A
871b0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
871c0 6e 20 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 61 n (also called a
871d0 20 22 63 68 75 6e 6b 22 29 20 63 6f 6e 73 69 73 "chunk") consis
871e0 74 73 20 6f 66 20 74 77 6f 20 6f 72 20 0a 2a 2a ts of two or .**
871f0 20 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 77 68 65 more blocks whe
87200 72 65 20 65 61 63 68 20 62 6c 6f 63 6b 20 69 73 re each block is
87210 20 38 20 62 79 74 65 73 2e 20 20 54 68 65 20 66 8 bytes. The f
87220 69 72 73 74 20 38 20 62 79 74 65 73 20 61 72 65 irst 8 bytes are
87230 20 0a 2a 2a 20 61 20 68 65 61 64 65 72 20 74 68 .** a header th
87240 61 74 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e at is not return
87250 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a ed to the user..
87260 2a 2a 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 73 **.** A chunk is
87270 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 6c 6f two or more blo
87280 63 6b 73 20 74 68 61 74 20 69 73 20 65 69 74 68 cks that is eith
87290 65 72 20 63 68 65 63 6b 65 64 20 6f 75 74 20 6f er checked out o
872a0 72 0a 2a 2a 20 66 72 65 65 2e 20 20 54 68 65 20 r.** free. The
872b0 66 69 72 73 74 20 62 6c 6f 63 6b 20 68 61 73 20 first block has
872c0 66 6f 72 6d 61 74 20 75 2e 68 64 72 2e 20 20 75 format u.hdr. u
872d0 2e 68 64 72 2e 73 69 7a 65 34 78 20 69 73 20 34 .hdr.size4x is 4
872e0 20 74 69 6d 65 73 20 74 68 65 0a 2a 2a 20 73 69 times the.** si
872f0 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 ze of the alloca
87300 74 69 6f 6e 20 69 6e 20 62 6c 6f 63 6b 73 20 69 tion in blocks i
87310 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e f the allocation
87320 20 69 73 20 66 72 65 65 2e 0a 2a 2a 20 54 68 65 is free..** The
87330 20 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 20 u.hdr.size4x&1
87340 62 69 74 20 69 73 20 74 72 75 65 20 69 66 20 74 bit is true if t
87350 68 65 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 he chunk is chec
87360 6b 65 64 20 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 ked out and.** f
87370 61 6c 73 65 20 69 66 20 74 68 65 20 63 68 75 6e alse if the chun
87380 6b 20 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 k is on the free
87390 6c 69 73 74 2e 20 20 54 68 65 20 75 2e 68 64 72 list. The u.hdr
873a0 2e 73 69 7a 65 34 78 26 32 20 62 69 74 0a 2a 2a .size4x&2 bit.**
873b0 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 is true if the
873c0 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 previous chunk i
873d0 73 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e s checked out an
873e0 64 20 66 61 6c 73 65 20 69 66 20 74 68 65 0a 2a d false if the.*
873f0 2a 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b * previous chunk
87400 20 69 73 20 66 72 65 65 2e 20 20 54 68 65 20 75 is free. The u
87410 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 66 69 .hdr.prevSize fi
87420 65 6c 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 eld is the size
87430 6f 66 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f of.** the previo
87440 75 73 20 63 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 us chunk in bloc
87450 6b 73 20 69 66 20 74 68 65 20 70 72 65 76 69 6f ks if the previo
87460 75 73 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 us chunk is on t
87470 68 65 0a 2a 2a 20 66 72 65 65 6c 69 73 74 2e 20 he.** freelist.
87480 49 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 If the previous
87490 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 chunk is checked
874a0 20 6f 75 74 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e out, then.** u.
874b0 68 64 72 2e 70 72 65 76 53 69 7a 65 20 63 61 6e hdr.prevSize can
874c0 20 62 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 be part of the
874d0 64 61 74 61 20 66 6f 72 20 74 68 61 74 20 63 68 data for that ch
874e0 75 6e 6b 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a unk and should.*
874f0 2a 20 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 * not be read or
87500 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 written..**.**
87510 57 65 20 6f 66 74 65 6e 20 69 64 65 6e 74 69 66 We often identif
87520 79 20 61 20 63 68 75 6e 6b 20 62 79 20 69 74 73 y a chunk by its
87530 20 69 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 index in mem3.a
87540 50 6f 6f 6c 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a Pool[]. When.**
87550 20 74 68 69 73 20 69 73 20 64 6f 6e 65 2c 20 74 this is done, t
87560 68 65 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 72 he chunk index r
87570 65 66 65 72 73 20 74 6f 20 74 68 65 20 73 65 63 efers to the sec
87580 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 ond block of.**
87590 74 68 65 20 63 68 75 6e 6b 2e 20 20 49 6e 20 74 the chunk. In t
875a0 68 69 73 20 77 61 79 2c 20 74 68 65 20 66 69 72 his way, the fir
875b0 73 74 20 63 68 75 6e 6b 20 68 61 73 20 61 6e 20 st chunk has an
875c0 69 6e 64 65 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 index of 1..** A
875d0 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 6f 66 20 chunk index of
875e0 30 20 6d 65 61 6e 73 20 22 6e 6f 20 73 75 63 68 0 means "no such
875f0 20 63 68 75 6e 6b 22 20 61 6e 64 20 69 73 20 74 chunk" and is t
87600 68 65 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a he equivalent.**
87610 20 6f 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 of a NULL point
87620 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 er..**.** The se
87630 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 66 72 cond block of fr
87640 65 65 20 63 68 75 6e 6b 73 20 69 73 20 6f 66 20 ee chunks is of
87650 74 68 65 20 66 6f 72 6d 20 75 2e 6c 69 73 74 2e the form u.list.
87660 20 20 54 68 65 0a 2a 2a 20 74 77 6f 20 66 69 65 The.** two fie
87670 6c 64 73 20 66 6f 72 6d 20 61 20 64 6f 75 62 6c lds form a doubl
87680 65 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 e-linked list of
87690 20 63 68 75 6e 6b 73 20 6f 66 20 72 65 6c 61 74 chunks of relat
876a0 65 64 20 73 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 ed sizes..** Poi
876b0 6e 74 65 72 73 20 74 6f 20 74 68 65 20 68 65 61 nters to the hea
876c0 64 20 6f 66 20 74 68 65 20 6c 69 73 74 20 61 72 d of the list ar
876d0 65 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 33 e stored in mem3
876e0 2e 61 69 53 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 .aiSmall[] .** f
876f0 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b or smaller chunk
87700 73 20 61 6e 64 20 6d 65 6d 33 2e 61 69 48 61 73 s and mem3.aiHas
87710 68 5b 5d 20 66 6f 72 20 6c 61 72 67 65 72 20 63 h[] for larger c
87720 68 75 6e 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 hunks..**.** The
87730 20 73 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 second block of
87740 20 61 20 63 68 75 6e 6b 20 69 73 20 75 73 65 72 a chunk is user
87750 20 64 61 74 61 20 69 66 20 74 68 65 20 63 68 75 data if the chu
87760 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 0a 2a nk is checked .*
87770 2a 20 6f 75 74 2e 20 20 49 66 20 61 20 63 68 75 * out. If a chu
87780 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 nk is checked ou
87790 74 2c 20 74 68 65 20 75 73 65 72 20 64 61 74 61 t, the user data
877a0 20 6d 61 79 20 65 78 74 65 6e 64 20 69 6e 74 6f may extend into
877b0 0a 2a 2a 20 74 68 65 20 75 2e 68 64 72 2e 70 72 .** the u.hdr.pr
877c0 65 76 53 69 7a 65 20 76 61 6c 75 65 20 6f 66 20 evSize value of
877d0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 the following ch
877e0 75 6e 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 unk..*/.typedef
877f0 73 74 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b struct Mem3Block
87800 20 4d 65 6d 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 Mem3Block;.stru
87810 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 ct Mem3Block {.
87820 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 union {. str
87830 75 63 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 uct {. u32
87840 70 72 65 76 53 69 7a 65 3b 20 20 20 2f 2a 20 53 prevSize; /* S
87850 69 7a 65 20 6f 66 20 70 72 65 76 69 6f 75 73 20 ize of previous
87860 63 68 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f chunk in Mem3Blo
87870 63 6b 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 ck elements */.
87880 20 20 20 20 20 75 33 32 20 73 69 7a 65 34 78 3b u32 size4x;
87890 20 20 20 20 20 2f 2a 20 34 78 20 74 68 65 20 73 /* 4x the s
878a0 69 7a 65 20 6f 66 20 63 75 72 72 65 6e 74 20 63 ize of current c
878b0 68 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 hunk in Mem3Bloc
878c0 6b 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 k elements */.
878d0 20 20 7d 20 68 64 72 3b 0a 20 20 20 20 73 74 72 } hdr;. str
878e0 75 63 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 uct {. u32
878f0 6e 65 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 next; /* I
87900 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f ndex in mem3.aPo
87910 6f 6c 5b 5d 20 6f 66 20 6e 65 78 74 20 66 72 65 ol[] of next fre
87920 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 e chunk */.
87930 20 75 33 32 20 70 72 65 76 3b 20 20 20 20 20 20 u32 prev;
87940 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d /* Index in mem
87950 33 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 3.aPool[] of pre
87960 76 69 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b vious free chunk
87970 20 2a 2f 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0a */. } list;.
87980 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a } u;.};../*.**
87990 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 All of the stat
879a0 69 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 ic variables use
879b0 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 d by this module
879c0 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a are collected.*
879d0 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 * into a single
879e0 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 structure named
879f0 22 6d 65 6d 33 22 2e 20 20 54 68 69 73 20 69 73 "mem3". This is
87a00 20 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 to keep the.**
87a10 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 static variables
87a20 20 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 organized and t
87a30 6f 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 o reduce namespa
87a40 63 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 ce pollution.**
87a50 77 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 when this module
87a60 20 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 is combined wit
87a70 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 h other in the a
87a80 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a malgamation..*/.
87a90 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 static SQLITE_WS
87aa0 44 20 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f D struct Mem3Glo
87ab0 62 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 bal {. /*. **
87ac0 4d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 Memory available
87ad0 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e for allocation.
87ae0 20 6e 50 6f 6f 6c 20 69 73 20 74 68 65 20 73 69 nPool is the si
87af0 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 0a ze of the array.
87b00 20 20 2a 2a 20 28 69 6e 20 4d 65 6d 33 42 6c 6f ** (in Mem3Blo
87b10 63 6b 73 29 20 70 6f 69 6e 74 65 64 20 74 6f 20 cks) pointed to
87b20 62 79 20 61 50 6f 6f 6c 20 6c 65 73 73 20 32 2e by aPool less 2.
87b30 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6e 50 6f 6f . */. u32 nPoo
87b40 6c 3b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a l;. Mem3Block *
87b50 61 50 6f 6f 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a aPool;.. /*. *
87b60 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 72 65 * True if we are
87b70 20 65 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 6f evaluating an o
87b80 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 6c ut-of-memory cal
87b90 6c 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e lback.. */. in
87ba0 74 20 61 6c 61 72 6d 42 75 73 79 3b 0a 20 20 0a t alarmBusy;. .
87bb0 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 /*. ** Mutex
87bc0 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 to control acces
87bd0 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 s to the memory
87be0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
87bf0 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c stem.. */. sql
87c00 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
87c10 78 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 x;. . /*. **
87c20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 The minimum amou
87c30 6e 74 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 nt of free space
87c40 20 74 68 61 74 20 77 65 20 68 61 76 65 20 73 65 that we have se
87c50 65 6e 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6d en.. */. u32 m
87c60 6e 4d 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 nMaster;.. /*.
87c70 20 2a 2a 20 69 4d 61 73 74 65 72 20 69 73 20 74 ** iMaster is t
87c80 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 he index of the
87c90 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 4d master chunk. M
87ca0 6f 73 74 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 ost new allocati
87cb0 6f 6e 73 0a 20 20 2a 2a 20 6f 63 63 75 72 20 6f ons. ** occur o
87cc0 66 66 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b ff of this chunk
87cd0 2e 20 20 73 7a 4d 61 73 74 65 72 20 69 73 20 74 . szMaster is t
87ce0 68 65 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 he size (in Mem3
87cf0 42 6c 6f 63 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 Blocks). ** of
87d00 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 73 74 the current mast
87d10 65 72 2e 20 20 69 4d 61 73 74 65 72 20 69 73 20 er. iMaster is
87d20 30 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 0 if there is no
87d30 74 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 0a t master chunk..
87d40 20 20 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 ** The master
87d50 63 68 75 6e 6b 20 69 73 20 6e 6f 74 20 69 6e 20 chunk is not in
87d60 65 69 74 68 65 72 20 74 68 65 20 61 69 48 61 73 either the aiHas
87d70 68 5b 5d 20 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d h[] or aiSmall[]
87d80 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 69 4d 61 .. */. u32 iMa
87d90 73 74 65 72 3b 0a 20 20 75 33 32 20 73 7a 4d 61 ster;. u32 szMa
87da0 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a ster;.. /*. **
87db0 20 41 72 72 61 79 20 6f 66 20 6c 69 73 74 73 20 Array of lists
87dc0 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 of free blocks a
87dd0 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 ccording to the
87de0 62 6c 6f 63 6b 20 73 69 7a 65 20 0a 20 20 2a 2a block size . **
87df0 20 66 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 for smaller chu
87e00 6e 6b 73 2c 20 6f 72 20 61 20 68 61 73 68 20 6f nks, or a hash o
87e10 6e 20 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65 n the block size
87e20 20 66 6f 72 20 6c 61 72 67 65 72 0a 20 20 2a 2a for larger. **
87e30 20 63 68 75 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 chunks.. */.
87e40 75 33 32 20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 u32 aiSmall[MX_S
87e50 4d 41 4c 4c 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f MALL-1]; /* Fo
87e60 72 20 73 69 7a 65 73 20 32 20 74 68 72 6f 75 67 r sizes 2 throug
87e70 68 20 4d 58 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c h MX_SMALL, incl
87e80 75 73 69 76 65 20 2a 2f 0a 20 20 75 33 32 20 61 usive */. u32 a
87e90 69 48 61 73 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 iHash[N_HASH];
87ea0 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a /* For siz
87eb0 65 73 20 4d 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e es MX_SMALL+1 an
87ec0 64 20 6c 61 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 d larger */.} me
87ed0 6d 33 20 3d 20 7b 20 39 37 35 33 35 35 37 35 20 m3 = { 97535575
87ee0 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 33 };..#define mem3
87ef0 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d GLOBAL(struct M
87f00 65 6d 33 47 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 em3Global, mem3)
87f10 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 ../*.** Unlink t
87f20 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33 he chunk at mem3
87f30 2e 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c .aPool[i] from l
87f40 69 73 74 20 69 74 20 69 73 20 63 75 72 72 65 6e ist it is curren
87f50 74 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f tly.** on. *pRo
87f60 6f 74 20 69 73 20 74 68 65 20 6c 69 73 74 20 74 ot is the list t
87f70 68 61 74 20 69 20 69 73 20 61 20 6d 65 6d 62 65 hat i is a membe
87f80 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 r of..*/.static
87f90 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 void memsys3Unli
87fa0 6e 6b 46 72 6f 6d 4c 69 73 74 28 75 33 32 20 69 nkFromList(u32 i
87fb0 2c 20 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 , u32 *pRoot){.
87fc0 20 75 33 32 20 6e 65 78 74 20 3d 20 6d 65 6d 33 u32 next = mem3
87fd0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 .aPool[i].u.list
87fe0 2e 6e 65 78 74 3b 0a 20 20 75 33 32 20 70 72 65 .next;. u32 pre
87ff0 76 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 v = mem3.aPool[i
88000 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 3b 0a 20 ].u.list.prev;.
88010 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
88020 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 _mutex_held(mem3
88030 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 .mutex) );. if(
88040 20 70 72 65 76 3d 3d 30 20 29 7b 0a 20 20 20 20 prev==0 ){.
88050 2a 70 52 6f 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 *pRoot = next;.
88060 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 33 }else{. mem3
88070 2e 61 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c .aPool[prev].u.l
88080 69 73 74 2e 6e 65 78 74 20 3d 20 6e 65 78 74 3b ist.next = next;
88090 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 20 . }. if( next
880a0 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f ){. mem3.aPoo
880b0 6c 5b 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 l[next].u.list.p
880c0 72 65 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a rev = prev;. }.
880d0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e mem3.aPool[i].
880e0 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 30 3b u.list.next = 0;
880f0 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d . mem3.aPool[i]
88100 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 .u.list.prev = 0
88110 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e ;.}../*.** Unlin
88120 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 k the chunk at i
88130 6e 64 65 78 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 ndex i from .**
88140 77 68 61 74 65 76 65 72 20 6c 69 73 74 20 69 73 whatever list is
88150 20 63 75 72 72 65 6e 74 6c 79 20 61 20 6d 65 6d currently a mem
88160 62 65 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 ber of..*/.stati
88170 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e c void memsys3Un
88180 6c 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 link(u32 i){. u
88190 33 32 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 32 size, hash;.
881a0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
881b0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 _mutex_held(mem3
881c0 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 .mutex) );. ass
881d0 65 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c ert( (mem3.aPool
881e0 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 [i-1].u.hdr.size
881f0 34 78 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 4x & 1)==0 );.
88200 61 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a assert( i>=1 );.
88210 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 size = mem3.aP
88220 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 ool[i-1].u.hdr.s
88230 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 ize4x/4;. asser
88240 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 t( size==mem3.aP
88250 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e ool[i+size-1].u.
88260 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a hdr.prevSize );.
88270 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d assert( size>=
88280 32 20 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 2 );. if( size
88290 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 <= MX_SMALL ){.
882a0 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b memsys3Unlink
882b0 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d FromList(i, &mem
882c0 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 3.aiSmall[size-2
882d0 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ]);. }else{.
882e0 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e hash = size % N
882f0 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 _HASH;. memsy
88300 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 s3UnlinkFromList
88310 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 (i, &mem3.aiHash
88320 5b 68 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a [hash]);. }.}..
88330 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 /*.** Link the c
88340 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f hunk at mem3.aPo
88350 6f 6c 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 ol[i] so that is
88360 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 72 6f 6f on the list roo
88370 74 65 64 0a 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 ted.** at *pRoot
88380 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
88390 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f memsys3LinkInto
883a0 4c 69 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 List(u32 i, u32
883b0 2a 70 52 6f 6f 74 29 7b 0a 20 20 61 73 73 65 72 *pRoot){. asser
883c0 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
883d0 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 _held(mem3.mutex
883e0 29 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f ) );. mem3.aPoo
883f0 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 l[i].u.list.next
88400 20 3d 20 2a 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d = *pRoot;. mem
88410 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 3.aPool[i].u.lis
88420 74 2e 70 72 65 76 20 3d 20 30 3b 0a 20 20 69 66 t.prev = 0;. if
88430 28 20 2a 70 52 6f 6f 74 20 29 7b 0a 20 20 20 20 ( *pRoot ){.
88440 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f mem3.aPool[*pRoo
88450 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d t].u.list.prev =
88460 20 69 3b 0a 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 i;. }. *pRoot
88470 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c = i;.}../*.** L
88480 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 ink the chunk at
88490 20 69 6e 64 65 78 20 69 20 69 6e 74 6f 20 65 69 index i into ei
884a0 74 68 65 72 20 74 68 65 20 61 70 70 72 6f 70 72 ther the appropr
884b0 69 61 74 65 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 iate.** small ch
884c0 75 6e 6b 20 6c 69 73 74 2c 20 6f 72 20 69 6e 74 unk list, or int
884d0 6f 20 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e o the large chun
884e0 6b 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f k hash table..*/
884f0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
88500 73 79 73 33 4c 69 6e 6b 28 75 33 32 20 69 29 7b sys3Link(u32 i){
88510 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73 . u32 size, has
88520 68 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c h;. assert( sql
88530 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
88540 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 mem3.mutex) );.
88550 20 61 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b assert( i>=1 );
88560 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 . assert( (mem3
88570 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 .aPool[i-1].u.hd
88580 72 2e 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30 r.size4x & 1)==0
88590 20 29 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d );. size = mem
885a0 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
885b0 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 dr.size4x/4;. a
885c0 73 73 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d ssert( size==mem
885d0 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 3.aPool[i+size-1
885e0 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 ].u.hdr.prevSize
885f0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 );. assert( si
88600 7a 65 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 ze>=2 );. if( s
88610 69 7a 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 ize <= MX_SMALL
88620 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 ){. memsys3Li
88630 6e 6b 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d nkIntoList(i, &m
88640 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 em3.aiSmall[size
88650 2d 32 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 -2]);. }else{.
88660 20 20 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 hash = size %
88670 20 4e 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d N_HASH;. mem
88680 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 sys3LinkIntoList
88690 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 (i, &mem3.aiHash
886a0 5b 68 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a [hash]);. }.}..
886b0 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 /*.** If the STA
886c0 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 TIC_MEM mutex is
886d0 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c not already hel
886e0 64 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 d, obtain it now
886f0 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 . The mutex.** w
88700 69 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 ill already be h
88710 65 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 eld (obtained by
88720 20 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e code in malloc.
88730 63 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 c) if.** sqlite3
88740 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
88750 6d 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a mStat is true..*
88760 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 /.static void me
88770 6d 73 79 73 33 45 6e 74 65 72 28 76 6f 69 64 29 msys3Enter(void)
88780 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 {. if( sqlite3G
88790 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d lobalConfig.bMem
887a0 73 74 61 74 3d 3d 30 20 26 26 20 6d 65 6d 33 2e stat==0 && mem3.
887b0 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 mutex==0 ){.
887c0 6d 65 6d 33 2e 6d 75 74 65 78 20 3d 20 73 71 6c mem3.mutex = sql
887d0 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
887e0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
887f0 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 IC_MEM);. }. s
88800 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
88810 65 72 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a er(mem3.mutex);.
88820 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 }.static void me
88830 6d 73 79 73 33 4c 65 61 76 65 28 76 6f 69 64 29 msys3Leave(void)
88840 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 {. sqlite3_mute
88850 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 x_leave(mem3.mut
88860 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 ex);.}../*.** Ca
88870 6c 6c 65 64 20 77 68 65 6e 20 77 65 20 61 72 65 lled when we are
88880 20 75 6e 61 62 6c 65 20 74 6f 20 73 61 74 69 73 unable to satis
88890 66 79 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e fy an allocation
888a0 20 6f 66 20 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 of nBytes..*/.s
888b0 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 tatic void memsy
888c0 73 33 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 69 6e s3OutOfMemory(in
888d0 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 66 28 20 t nByte){. if(
888e0 21 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 !mem3.alarmBusy
888f0 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 ){. mem3.alar
88900 6d 42 75 73 79 20 3d 20 31 3b 0a 20 20 20 20 61 mBusy = 1;. a
88910 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
88920 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d utex_held(mem3.m
88930 75 74 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c utex) );. sql
88940 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
88950 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem3.mutex);.
88960 20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 sqlite3_releas
88970 65 5f 6d 65 6d 6f 72 79 28 6e 42 79 74 65 29 3b e_memory(nByte);
88980 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
88990 65 78 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 ex_enter(mem3.mu
889a0 74 65 78 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 tex);. mem3.a
889b0 6c 61 72 6d 42 75 73 79 20 3d 20 30 3b 0a 20 20 larmBusy = 0;.
889c0 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e }.}.../*.** Chun
889d0 6b 20 69 20 69 73 20 61 20 66 72 65 65 20 63 68 k i is a free ch
889e0 75 6e 6b 20 74 68 61 74 20 68 61 73 20 62 65 65 unk that has bee
889f0 6e 20 75 6e 6c 69 6e 6b 65 64 2e 20 20 41 64 6a n unlinked. Adj
88a00 75 73 74 20 69 74 73 20 0a 2a 2a 20 73 69 7a 65 ust its .** size
88a10 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 parameters for
88a20 63 68 65 63 6b 2d 6f 75 74 20 61 6e 64 20 72 65 check-out and re
88a30 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
88a40 6f 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 20 70 o the .** user p
88a50 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 ortion of the ch
88a60 75 6e 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 unk..*/.static v
88a70 6f 69 64 20 2a 6d 65 6d 73 79 73 33 43 68 65 63 oid *memsys3Chec
88a80 6b 6f 75 74 28 75 33 32 20 69 2c 20 75 33 32 20 kout(u32 i, u32
88a90 6e 42 6c 6f 63 6b 29 7b 0a 20 20 75 33 32 20 78 nBlock){. u32 x
88aa0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
88ab0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d te3_mutex_held(m
88ac0 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 em3.mutex) );.
88ad0 61 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a assert( i>=1 );.
88ae0 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 assert( mem3.a
88af0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
88b00 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b size4x/4==nBlock
88b10 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 );. assert( me
88b20 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 m3.aPool[i+nBloc
88b30 6b 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 k-1].u.hdr.prevS
88b40 69 7a 65 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 ize==nBlock );.
88b50 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b x = mem3.aPool[
88b60 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
88b70 78 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b x;. mem3.aPool[
88b80 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
88b90 78 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 x = nBlock*4 | 1
88ba0 20 7c 20 28 78 26 32 29 3b 0a 20 20 6d 65 6d 33 | (x&2);. mem3
88bb0 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d .aPool[i+nBlock-
88bc0 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 1].u.hdr.prevSiz
88bd0 65 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 e = nBlock;. me
88be0 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 m3.aPool[i+nBloc
88bf0 6b 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 k-1].u.hdr.size4
88c00 78 20 7c 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e x |= 2;. return
88c10 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b &mem3.aPool[i];
88c20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 .}../*.** Carve
88c30 61 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 a piece off of t
88c40 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 65 he end of the me
88c50 6d 33 2e 69 4d 61 73 74 65 72 20 66 72 65 65 20 m3.iMaster free
88c60 63 68 75 6e 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e chunk..** Return
88c70 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
88c80 65 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e e new allocation
88c90 2e 20 20 4f 72 2c 20 69 66 20 74 68 65 20 6d 61 . Or, if the ma
88ca0 73 74 65 72 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 ster chunk.** is
88cb0 20 6e 6f 74 20 6c 61 72 67 65 20 65 6e 6f 75 67 not large enoug
88cc0 68 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a h, return 0..*/.
88cd0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d static void *mem
88ce0 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 75 sys3FromMaster(u
88cf0 33 32 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 32 nBlock){. as
88d00 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
88d10 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 tex_held(mem3.mu
88d20 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
88d30 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e ( mem3.szMaster>
88d40 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 69 66 28 =nBlock );. if(
88d50 20 6e 42 6c 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a nBlock>=mem3.sz
88d60 4d 61 73 74 65 72 2d 31 20 29 7b 0a 20 20 20 20 Master-1 ){.
88d70 2f 2a 20 55 73 65 20 74 68 65 20 65 6e 74 69 72 /* Use the entir
88d80 65 20 6d 61 73 74 65 72 20 2a 2f 0a 20 20 20 20 e master */.
88d90 76 6f 69 64 20 2a 70 20 3d 20 6d 65 6d 73 79 73 void *p = memsys
88da0 33 43 68 65 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 3Checkout(mem3.i
88db0 4d 61 73 74 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d Master, mem3.szM
88dc0 61 73 74 65 72 29 3b 0a 20 20 20 20 6d 65 6d 33 aster);. mem3
88dd0 2e 69 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 .iMaster = 0;.
88de0 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 mem3.szMaster
88df0 3d 20 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e = 0;. mem3.mn
88e00 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 Master = 0;.
88e10 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 return p;. }els
88e20 65 7b 0a 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 e{. /* Split
88e30 74 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b the master block
88e40 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 74 61 . Return the ta
88e50 69 6c 2e 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e il. */. u32 n
88e60 65 77 69 2c 20 78 3b 0a 20 20 20 20 6e 65 77 69 ewi, x;. newi
88e70 20 3d 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 = mem3.iMaster
88e80 2b 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 + mem3.szMaster
88e90 2d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 - nBlock;. as
88ea0 73 65 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d sert( newi > mem
88eb0 33 2e 69 4d 61 73 74 65 72 2b 31 20 29 3b 0a 20 3.iMaster+1 );.
88ec0 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 mem3.aPool[me
88ed0 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e m3.iMaster+mem3.
88ee0 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 szMaster-1].u.hd
88ef0 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c r.prevSize = nBl
88f00 6f 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 ock;. mem3.aP
88f10 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ool[mem3.iMaster
88f20 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 +mem3.szMaster-1
88f30 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c ].u.hdr.size4x |
88f40 3d 20 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 = 2;. mem3.aP
88f50 6f 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 ool[newi-1].u.hd
88f60 72 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 r.size4x = nBloc
88f70 6b 2a 34 20 2b 20 31 3b 0a 20 20 20 20 6d 65 6d k*4 + 1;. mem
88f80 33 2e 73 7a 4d 61 73 74 65 72 20 2d 3d 20 6e 42 3.szMaster -= nB
88f90 6c 6f 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 lock;. mem3.a
88fa0 50 6f 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 Pool[newi-1].u.h
88fb0 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 dr.prevSize = me
88fc0 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 m3.szMaster;.
88fd0 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b x = mem3.aPool[
88fe0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e mem3.iMaster-1].
88ff0 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 u.hdr.size4x & 2
89000 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c ;. mem3.aPool
89010 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d [mem3.iMaster-1]
89020 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 .u.hdr.size4x =
89030 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 mem3.szMaster*4
89040 7c 20 78 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d | x;. if( mem
89050 33 2e 73 7a 4d 61 73 74 65 72 20 3c 20 6d 65 6d 3.szMaster < mem
89060 33 2e 6d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 3.mnMaster ){.
89070 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 mem3.mnMaste
89080 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 r = mem3.szMaste
89090 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 r;. }. ret
890a0 75 72 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 33 urn (void*)&mem3
890b0 2e 61 50 6f 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 .aPool[newi];.
890c0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f }.}../*.** *pRoo
890d0 74 20 69 73 20 74 68 65 20 68 65 61 64 20 6f 66 t is the head of
890e0 20 61 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 a list of free
890f0 63 68 75 6e 6b 73 20 6f 66 20 74 68 65 20 73 61 chunks of the sa
89100 6d 65 20 73 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 me size.** or sa
89110 6d 65 20 73 69 7a 65 20 68 61 73 68 2e 20 20 49 me size hash. I
89120 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 2a n other words, *
89130 70 52 6f 6f 74 20 69 73 20 61 6e 20 65 6e 74 72 pRoot is an entr
89140 79 20 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 6d y in either.** m
89150 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 em3.aiSmall[] or
89160 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 5d 2e 20 mem3.aiHash[].
89170 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 .**.** This rou
89180 74 69 6e 65 20 65 78 61 6d 69 6e 65 73 20 61 6c tine examines al
89190 6c 20 65 6e 74 72 69 65 73 20 6f 6e 20 74 68 65 l entries on the
891a0 20 67 69 76 65 6e 20 6c 69 73 74 20 61 6e 64 20 given list and
891b0 74 72 69 65 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c tries.** to coal
891c0 65 73 63 65 20 65 61 63 68 20 65 6e 74 72 69 65 esce each entrie
891d0 73 20 77 69 74 68 20 61 64 6a 61 63 65 6e 74 20 s with adjacent
891e0 66 72 65 65 20 63 68 75 6e 6b 73 2e 20 20 0a 2a free chunks. .*
891f0 2a 0a 2a 2a 20 49 66 20 69 74 20 73 65 65 73 20 *.** If it sees
89200 61 20 63 68 75 6e 6b 20 74 68 61 74 20 69 73 20 a chunk that is
89210 6c 61 72 67 65 72 20 74 68 61 6e 20 6d 65 6d 33 larger than mem3
89220 2e 69 4d 61 73 74 65 72 2c 20 69 74 20 72 65 70 .iMaster, it rep
89230 6c 61 63 65 73 20 0a 2a 2a 20 74 68 65 20 63 75 laces .** the cu
89240 72 72 65 6e 74 20 6d 65 6d 33 2e 69 4d 61 73 74 rrent mem3.iMast
89250 65 72 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 er with the new
89260 6c 61 72 67 65 72 20 63 68 75 6e 6b 2e 20 20 49 larger chunk. I
89270 6e 20 6f 72 64 65 72 20 66 6f 72 0a 2a 2a 20 74 n order for.** t
89280 68 69 73 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 his mem3.iMaster
89290 20 72 65 70 6c 61 63 65 6d 65 6e 74 20 74 6f 20 replacement to
892a0 77 6f 72 6b 2c 20 74 68 65 20 6d 61 73 74 65 72 work, the master
892b0 20 63 68 75 6e 6b 20 6d 75 73 74 20 62 65 0a 2a chunk must be.*
892c0 2a 20 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 68 * linked into th
892d0 65 20 68 61 73 68 20 74 61 62 6c 65 73 2e 20 20 e hash tables.
892e0 54 68 61 74 20 69 73 20 6e 6f 74 20 74 68 65 20 That is not the
892f0 6e 6f 72 6d 61 6c 20 73 74 61 74 65 20 6f 66 0a normal state of.
89300 2a 2a 20 61 66 66 61 69 72 73 2c 20 6f 66 20 63 ** affairs, of c
89310 6f 75 72 73 65 2e 20 20 54 68 65 20 63 61 6c 6c ourse. The call
89320 69 6e 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 ing routine must
89330 20 6c 69 6e 6b 20 74 68 65 20 6d 61 73 74 65 72 link the master
89340 0a 2a 2a 20 63 68 75 6e 6b 20 62 65 66 6f 72 65 .** chunk before
89350 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 invoking this r
89360 6f 75 74 69 6e 65 2c 20 74 68 65 6e 20 6d 75 73 outine, then mus
89370 74 20 75 6e 6c 69 6e 6b 20 74 68 65 20 28 70 6f t unlink the (po
89380 73 73 69 62 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 ssibly.** change
89390 64 29 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 d) master chunk
893a0 6f 6e 63 65 20 74 68 69 73 20 72 6f 75 74 69 6e once this routin
893b0 65 20 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a e has finished..
893c0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
893d0 65 6d 73 79 73 33 4d 65 72 67 65 28 75 33 32 20 emsys3Merge(u32
893e0 2a 70 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 69 *pRoot){. u32 i
893f0 4e 65 78 74 2c 20 70 72 65 76 2c 20 73 69 7a 65 Next, prev, size
89400 2c 20 69 2c 20 78 3b 0a 0a 20 20 61 73 73 65 72 , i, x;.. asser
89410 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
89420 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 _held(mem3.mutex
89430 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 2a 70 52 ) );. for(i=*pR
89440 6f 6f 74 3b 20 69 3e 30 3b 20 69 3d 69 4e 65 78 oot; i>0; i=iNex
89450 74 29 7b 0a 20 20 20 20 69 4e 65 78 74 20 3d 20 t){. iNext =
89460 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e mem3.aPool[i].u.
89470 6c 69 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 73 list.next;. s
89480 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c ize = mem3.aPool
89490 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 [i-1].u.hdr.size
894a0 34 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 4x;. assert(
894b0 28 73 69 7a 65 26 31 29 3d 3d 30 20 29 3b 0a 20 (size&1)==0 );.
894c0 20 20 20 69 66 28 20 28 73 69 7a 65 26 32 29 3d if( (size&2)=
894d0 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 =0 ){. mems
894e0 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 ys3UnlinkFromLis
894f0 74 28 69 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 t(i, pRoot);.
89500 20 20 20 61 73 73 65 72 74 28 20 69 20 3e 20 6d assert( i > m
89510 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 em3.aPool[i-1].u
89520 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b .hdr.prevSize );
89530 0a 20 20 20 20 20 20 70 72 65 76 20 3d 20 69 20 . prev = i
89540 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 - mem3.aPool[i-1
89550 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 ].u.hdr.prevSize
89560 3b 0a 20 20 20 20 20 20 69 66 28 20 70 72 65 76 ;. if( prev
89570 3d 3d 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 ==iNext ){.
89580 20 20 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e iNext = mem3.
89590 61 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 aPool[prev].u.li
895a0 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 20 20 7d st.next;. }
895b0 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e . memsys3Un
895c0 6c 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 link(prev);.
895d0 20 20 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a size = i + siz
895e0 65 2f 34 20 2d 20 70 72 65 76 3b 0a 20 20 20 20 e/4 - prev;.
895f0 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c x = mem3.aPool
89600 5b 70 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 [prev-1].u.hdr.s
89610 69 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 ize4x & 2;.
89620 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 mem3.aPool[prev
89630 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
89640 20 3d 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 = size*4 | x;.
89650 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b mem3.aPool[
89660 70 72 65 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 prev+size-1].u.h
89670 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 73 69 dr.prevSize = si
89680 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 ze;. memsys
89690 33 4c 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 3Link(prev);.
896a0 20 20 20 69 20 3d 20 70 72 65 76 3b 0a 20 20 20 i = prev;.
896b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 69 }else{. si
896c0 7a 65 20 2f 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 ze /= 4;. }.
896d0 20 20 20 69 66 28 20 73 69 7a 65 3e 6d 65 6d 33 if( size>mem3
896e0 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 .szMaster ){.
896f0 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 mem3.iMaster
89700 3d 20 69 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e = i;. mem3.
89710 73 7a 4d 61 73 74 65 72 20 3d 20 73 69 7a 65 3b szMaster = size;
89720 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
89730 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f .** Return a blo
89740 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 ck of memory of
89750 61 74 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 at least nBytes
89760 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 in size..** Retu
89770 72 6e 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c rn NULL if unabl
89780 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 e..**.** This fu
89790 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 nction assumes t
897a0 68 61 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 hat the necessar
897b0 79 20 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e y mutexes, if an
897c0 79 2c 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 y, are.** alread
897d0 79 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 y held by the ca
897e0 6c 6c 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 ller. Hence "Uns
897f0 61 66 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 afe"..*/.static
89800 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c void *memsys3Mal
89810 6c 6f 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 locUnsafe(int nB
89820 79 74 65 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 yte){. u32 i;.
89830 20 75 33 32 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75 u32 nBlock;. u
89840 33 32 20 74 6f 46 72 65 65 3b 0a 0a 20 20 61 73 32 toFree;.. as
89850 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
89860 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 tex_held(mem3.mu
89870 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
89880 28 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f ( sizeof(Mem3Blo
89890 63 6b 29 3d 3d 38 20 29 3b 0a 20 20 69 66 28 20 ck)==8 );. if(
898a0 6e 42 79 74 65 3c 3d 31 32 20 29 7b 0a 20 20 20 nByte<=12 ){.
898b0 20 6e 42 6c 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d nBlock = 2;. }
898c0 65 6c 73 65 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b else{. nBlock
898d0 20 3d 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f = (nByte + 11)/
898e0 38 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 8;. }. assert(
898f0 20 6e 42 6c 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 nBlock>=2 );..
89900 20 2f 2a 20 53 54 45 50 20 31 3a 0a 20 20 2a 2a /* STEP 1:. **
89910 20 4c 6f 6f 6b 20 66 6f 72 20 61 6e 20 65 6e 74 Look for an ent
89920 72 79 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 ry of the correc
89930 74 20 73 69 7a 65 20 69 6e 20 65 69 74 68 65 72 t size in either
89940 20 74 68 65 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 the small. **
89950 63 68 75 6e 6b 20 74 61 62 6c 65 20 6f 72 20 69 chunk table or i
89960 6e 20 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e n the large chun
89970 6b 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 k hash table. T
89980 68 69 73 20 69 73 0a 20 20 2a 2a 20 73 75 63 63 his is. ** succ
89990 65 73 73 66 75 6c 20 6d 6f 73 74 20 6f 66 20 74 essful most of t
899a0 68 65 20 74 69 6d 65 20 28 61 62 6f 75 74 20 39 he time (about 9
899b0 20 74 69 6d 65 73 20 6f 75 74 20 6f 66 20 31 30 times out of 10
899c0 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 ).. */. if( nB
899d0 6c 6f 63 6b 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c lock <= MX_SMALL
899e0 20 29 7b 0a 20 20 20 20 69 20 3d 20 6d 65 6d 33 ){. i = mem3
899f0 2e 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d .aiSmall[nBlock-
89a00 32 5d 3b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 2];. if( i>0
89a10 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 ){. memsys3
89a20 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 UnlinkFromList(i
89a30 2c 20 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b , &mem3.aiSmall[
89a40 6e 42 6c 6f 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 nBlock-2]);.
89a50 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 return memsys3
89a60 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f Checkout(i, nBlo
89a70 63 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c ck);. }. }el
89a80 73 65 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 68 se{. int hash
89a90 20 3d 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 = nBlock % N_HA
89aa0 53 48 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65 SH;. for(i=me
89ab0 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b m3.aiHash[hash];
89ac0 20 69 3e 30 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f i>0; i=mem3.aPo
89ad0 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 ol[i].u.list.nex
89ae0 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 t){. if( me
89af0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
89b00 68 64 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 hdr.size4x/4==nB
89b10 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 lock ){.
89b20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f memsys3UnlinkFro
89b30 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 mList(i, &mem3.a
89b40 69 48 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 iHash[hash]);.
89b50 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d return mem
89b60 73 79 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20 sys3Checkout(i,
89b70 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d nBlock);. }
89b80 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
89b90 20 53 54 45 50 20 32 3a 0a 20 20 2a 2a 20 54 72 STEP 2:. ** Tr
89ba0 79 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 y to satisfy the
89bb0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 allocation by c
89bc0 61 72 76 69 6e 67 20 61 20 70 69 65 63 65 20 6f arving a piece o
89bd0 66 66 20 6f 66 20 74 68 65 20 65 6e 64 0a 20 20 ff of the end.
89be0 2a 2a 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 ** of the master
89bf0 20 63 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 chunk. This st
89c00 65 70 20 75 73 75 61 6c 6c 79 20 77 6f 72 6b 73 ep usually works
89c10 20 69 66 20 73 74 65 70 20 31 20 66 61 69 6c 73 if step 1 fails
89c20 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d .. */. if( mem
89c30 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 3.szMaster>=nBlo
89c40 63 6b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ck ){. return
89c50 20 6d 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 memsys3FromMast
89c60 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a er(nBlock);. }.
89c70 0a 0a 20 20 2f 2a 20 53 54 45 50 20 33 3a 20 20 .. /* STEP 3:
89c80 0a 20 20 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 . ** Loop throu
89c90 67 68 20 74 68 65 20 65 6e 74 69 72 65 20 6d 65 gh the entire me
89ca0 6d 6f 72 79 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c mory pool. Coal
89cb0 65 73 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72 esce adjacent fr
89cc0 65 65 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 ee. ** chunks.
89cd0 20 52 65 63 6f 6d 70 75 74 65 20 74 68 65 20 6d Recompute the m
89ce0 61 73 74 65 72 20 63 68 75 6e 6b 20 61 73 20 74 aster chunk as t
89cf0 68 65 20 6c 61 72 67 65 73 74 20 66 72 65 65 20 he largest free
89d00 63 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e chunk.. ** Then
89d10 20 74 72 79 20 61 67 61 69 6e 20 74 6f 20 73 61 try again to sa
89d20 74 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 tisfy the alloca
89d30 74 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 tion by carving
89d40 61 20 70 69 65 63 65 20 6f 66 66 0a 20 20 2a 2a a piece off. **
89d50 20 6f 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 of the end of t
89d60 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e he master chunk.
89d70 20 20 54 68 69 73 20 73 74 65 70 20 68 61 70 70 This step happ
89d80 65 6e 73 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 ens very. ** ra
89d90 72 65 6c 79 20 28 77 65 20 68 6f 70 65 21 29 0a rely (we hope!).
89da0 20 20 2a 2f 0a 20 20 66 6f 72 28 74 6f 46 72 65 */. for(toFre
89db0 65 3d 6e 42 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 e=nBlock*16; toF
89dc0 72 65 65 3c 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a ree<(mem3.nPool*
89dd0 31 36 29 3b 20 74 6f 46 72 65 65 20 2a 3d 20 32 16); toFree *= 2
89de0 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4f 75 ){. memsys3Ou
89df0 74 4f 66 4d 65 6d 6f 72 79 28 74 6f 46 72 65 65 tOfMemory(toFree
89e00 29 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e );. if( mem3.
89e10 69 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 iMaster ){.
89e20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 6d 65 6d memsys3Link(mem
89e30 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 3.iMaster);.
89e40 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d mem3.iMaster =
89e50 20 30 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 0;. mem3.s
89e60 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 zMaster = 0;.
89e70 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 }. for(i=0;
89e80 69 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a i<N_HASH; i++){.
89e90 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72 memsys3Mer
89ea0 67 65 28 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b ge(&mem3.aiHash[
89eb0 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 i]);. }. f
89ec0 6f 72 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 or(i=0; i<MX_SMA
89ed0 4c 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 LL-1; i++){.
89ee0 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 memsys3Merge(&
89ef0 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 mem3.aiSmall[i])
89f00 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
89f10 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b mem3.szMaster ){
89f20 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e . memsys3Un
89f30 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 link(mem3.iMaste
89f40 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 r);. if( me
89f50 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c m3.szMaster>=nBl
89f60 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ock ){. r
89f70 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f eturn memsys3Fro
89f80 6d 4d 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b mMaster(nBlock);
89f90 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
89fa0 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 }.. /* If none
89fb0 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 77 6f of the above wo
89fc0 72 6b 65 64 2c 20 74 68 65 6e 20 77 65 20 66 61 rked, then we fa
89fd0 69 6c 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 il. */. return
89fe0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 0;.}../*.** Free
89ff0 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 an outstanding
8a000 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
8a010 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 n..**.** This fu
8a020 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 nction assumes t
8a030 68 61 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 hat the necessar
8a040 79 20 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e y mutexes, if an
8a050 79 2c 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 y, are.** alread
8a060 79 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 y held by the ca
8a070 6c 6c 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 ller. Hence "Uns
8a080 61 66 65 22 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 afe"..*/.void me
8a090 6d 73 79 73 33 46 72 65 65 55 6e 73 61 66 65 28 msys3FreeUnsafe(
8a0a0 76 6f 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d void *pOld){. M
8a0b0 65 6d 33 42 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d em3Block *p = (M
8a0c0 65 6d 33 42 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a em3Block*)pOld;.
8a0d0 20 20 69 6e 74 20 69 3b 0a 20 20 75 33 32 20 73 int i;. u32 s
8a0e0 69 7a 65 2c 20 78 3b 0a 20 20 61 73 73 65 72 74 ize, x;. assert
8a0f0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
8a100 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 held(mem3.mutex)
8a110 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 3e );. assert( p>
8a120 6d 65 6d 33 2e 61 50 6f 6f 6c 20 26 26 20 70 3c mem3.aPool && p<
8a130 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 &mem3.aPool[mem3
8a140 2e 6e 50 6f 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d .nPool] );. i =
8a150 20 70 20 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b p - mem3.aPool;
8a160 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 . assert( (mem3
8a170 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 .aPool[i-1].u.hd
8a180 72 2e 73 69 7a 65 34 78 26 31 29 3d 3d 31 20 29 r.size4x&1)==1 )
8a190 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e ;. size = mem3.
8a1a0 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 aPool[i-1].u.hdr
8a1b0 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 .size4x/4;. ass
8a1c0 65 72 74 28 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d ert( i+size<=mem
8a1d0 33 2e 6e 50 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d 3.nPool+1 );. m
8a1e0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 em3.aPool[i-1].u
8a1f0 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e .hdr.size4x &= ~
8a200 31 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 1;. mem3.aPool[
8a210 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e i+size-1].u.hdr.
8a220 70 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b prevSize = size;
8a230 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b . mem3.aPool[i+
8a240 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 size-1].u.hdr.si
8a250 7a 65 34 78 20 26 3d 20 7e 32 3b 0a 20 20 6d 65 ze4x &= ~2;. me
8a260 6d 73 79 73 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20 msys3Link(i);..
8a270 20 2f 2a 20 54 72 79 20 74 6f 20 65 78 70 61 6e /* Try to expan
8a280 64 20 74 68 65 20 6d 61 73 74 65 72 20 75 73 69 d the master usi
8a290 6e 67 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65 ng the newly fre
8a2a0 65 64 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 66 ed chunk */. if
8a2b0 28 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 29 ( mem3.iMaster )
8a2c0 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 {. while( (me
8a2d0 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d m3.aPool[mem3.iM
8a2e0 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 aster-1].u.hdr.s
8a2f0 69 7a 65 34 78 26 32 29 3d 3d 30 20 29 7b 0a 20 ize4x&2)==0 ){.
8a300 20 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 size = mem3
8a310 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 .aPool[mem3.iMas
8a320 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 ter-1].u.hdr.pre
8a330 76 53 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d vSize;. mem
8a340 33 2e 69 4d 61 73 74 65 72 20 2d 3d 20 73 69 7a 3.iMaster -= siz
8a350 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a e;. mem3.sz
8a360 4d 61 73 74 65 72 20 2b 3d 20 73 69 7a 65 3b 0a Master += size;.
8a370 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c memsys3Unl
8a380 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ink(mem3.iMaster
8a390 29 3b 0a 20 20 20 20 20 20 78 20 3d 20 6d 65 6d );. x = mem
8a3a0 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 3.aPool[mem3.iMa
8a3b0 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ster-1].u.hdr.si
8a3c0 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 ze4x & 2;.
8a3d0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e mem3.aPool[mem3.
8a3e0 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 iMaster-1].u.hdr
8a3f0 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 .size4x = mem3.s
8a400 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 zMaster*4 | x;.
8a410 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b mem3.aPool[
8a420 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d mem3.iMaster+mem
8a430 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 3.szMaster-1].u.
8a440 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d hdr.prevSize = m
8a450 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 em3.szMaster;.
8a460 20 20 7d 0a 20 20 20 20 78 20 3d 20 6d 65 6d 33 }. x = mem3
8a470 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 .aPool[mem3.iMas
8a480 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a ter-1].u.hdr.siz
8a490 65 34 78 20 26 20 32 3b 0a 20 20 20 20 77 68 69 e4x & 2;. whi
8a4a0 6c 65 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b le( (mem3.aPool[
8a4b0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d mem3.iMaster+mem
8a4c0 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 3.szMaster-1].u.
8a4d0 68 64 72 2e 73 69 7a 65 34 78 26 31 29 3d 3d 30 hdr.size4x&1)==0
8a4e0 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 ){. memsys
8a4f0 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 3Unlink(mem3.iMa
8a500 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 ster+mem3.szMast
8a510 65 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e er);. mem3.
8a520 73 7a 4d 61 73 74 65 72 20 2b 3d 20 6d 65 6d 33 szMaster += mem3
8a530 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 .aPool[mem3.iMas
8a540 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 ter+mem3.szMaste
8a550 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 r-1].u.hdr.size4
8a560 78 2f 34 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e x/4;. mem3.
8a570 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 aPool[mem3.iMast
8a580 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 er-1].u.hdr.size
8a590 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 4x = mem3.szMast
8a5a0 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 er*4 | x;.
8a5b0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e mem3.aPool[mem3.
8a5c0 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d iMaster+mem3.szM
8a5d0 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 aster-1].u.hdr.p
8a5e0 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 revSize = mem3.s
8a5f0 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 zMaster;. }.
8a600 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 }.}../*.** Retu
8a610 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 rn the size of a
8a620 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c n outstanding al
8a630 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74 location, in byt
8a640 65 73 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65 es. The.** size
8a650 20 72 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20 returned omits
8a660 74 68 65 20 38 2d 62 79 74 65 20 68 65 61 64 65 the 8-byte heade
8a670 72 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69 r overhead. Thi
8a680 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 s only.** works
8a690 66 6f 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20 for chunks that
8a6a0 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68 are currently ch
8a6b0 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 ecked out..*/.st
8a6c0 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 atic int memsys3
8a6d0 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 Size(void *p){.
8a6e0 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f Mem3Block *pBlo
8a6f0 63 6b 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 ck;. if( p==0 )
8a700 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 42 6c return 0;. pBl
8a710 6f 63 6b 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b ock = (Mem3Block
8a720 2a 29 70 3b 0a 20 20 61 73 73 65 72 74 28 20 28 *)p;. assert( (
8a730 70 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 pBlock[-1].u.hdr
8a740 2e 73 69 7a 65 34 78 26 31 29 21 3d 30 20 29 3b .size4x&1)!=0 );
8a750 0a 20 20 72 65 74 75 72 6e 20 28 70 42 6c 6f 63 . return (pBloc
8a760 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 k[-1].u.hdr.size
8a770 34 78 26 7e 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a 4x&~3)*2 - 4;.}.
8a780 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 ./*.** Round up
8a790 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 a request size t
8a7a0 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 o the next valid
8a7b0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 allocation size
8a7c0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
8a7d0 6d 65 6d 73 79 73 33 52 6f 75 6e 64 75 70 28 69 memsys3Roundup(i
8a7e0 6e 74 20 6e 29 7b 0a 20 20 69 66 28 20 6e 3c 3d nt n){. if( n<=
8a7f0 31 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 12 ){. return
8a800 20 31 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 12;. }else{.
8a810 20 20 72 65 74 75 72 6e 20 28 28 6e 2b 31 31 29 return ((n+11)
8a820 26 7e 37 29 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a &~7) - 4;. }.}.
8a830 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 ./*.** Allocate
8a840 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 nBytes of memory
8a850 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
8a860 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 *memsys3Malloc(
8a870 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 int nBytes){. s
8a880 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b qlite3_int64 *p;
8a890 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 . assert( nByte
8a8a0 73 3e 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 s>0 );
8a8b0 2f 2a 20 6d 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 /* malloc.c filt
8a8c0 65 72 73 20 6f 75 74 20 30 20 62 79 74 65 20 72 ers out 0 byte r
8a8d0 65 71 75 65 73 74 73 20 2a 2f 0a 20 20 6d 65 6d equests */. mem
8a8e0 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 sys3Enter();. p
8a8f0 20 3d 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 = memsys3Malloc
8a900 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a Unsafe(nBytes);.
8a910 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 memsys3Leave()
8a920 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 ;. return (void
8a930 2a 29 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 *)p; .}../*.** F
8a940 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76 ree memory..*/.v
8a950 6f 69 64 20 6d 65 6d 73 79 73 33 46 72 65 65 28 oid memsys3Free(
8a960 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 void *pPrior){.
8a970 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 20 assert( pPrior
8a980 29 3b 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 );. memsys3Ente
8a990 72 28 29 3b 0a 20 20 6d 65 6d 73 79 73 33 46 72 r();. memsys3Fr
8a9a0 65 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 eeUnsafe(pPrior)
8a9b0 3b 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 ;. memsys3Leave
8a9c0 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 ();.}../*.** Cha
8a9d0 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 nge the size of
8a9e0 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f an existing memo
8a9f0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f ry allocation.*/
8aa00 0a 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 52 65 .void *memsys3Re
8aa10 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 alloc(void *pPri
8aa20 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b or, int nBytes){
8aa30 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 . int nOld;. v
8aa40 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 oid *p;. if( pP
8aa50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 rior==0 ){. r
8aa60 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 61 eturn sqlite3_ma
8aa70 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 lloc(nBytes);.
8aa80 7d 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d }. if( nBytes<=
8aa90 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
8aaa0 5f 66 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 _free(pPrior);.
8aab0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
8aac0 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 . nOld = memsys
8aad0 33 53 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 3Size(pPrior);.
8aae0 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c if( nBytes<=nOl
8aaf0 64 20 26 26 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c d && nBytes>=nOl
8ab00 64 2d 31 32 38 20 29 7b 0a 20 20 20 20 72 65 74 d-128 ){. ret
8ab10 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a urn pPrior;. }.
8ab20 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 memsys3Enter()
8ab30 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d ;. p = memsys3M
8ab40 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 allocUnsafe(nByt
8ab50 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a es);. if( p ){.
8ab60 20 20 20 20 69 66 28 20 6e 4f 6c 64 3c 6e 42 79 if( nOld<nBy
8ab70 74 65 73 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d tes ){. mem
8ab80 63 70 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e cpy(p, pPrior, n
8ab90 4f 6c 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b Old);. }else{
8aba0 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c . memcpy(p,
8abb0 20 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 73 29 pPrior, nBytes)
8abc0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 ;. }. mems
8abd0 79 73 33 46 72 65 65 55 6e 73 61 66 65 28 70 50 ys3FreeUnsafe(pP
8abe0 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d rior);. }. mem
8abf0 73 79 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 sys3Leave();. r
8ac00 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
8ac10 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 * Initialize thi
8ac20 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 s module..*/.sta
8ac30 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 49 tic int memsys3I
8ac40 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 nit(void *NotUse
8ac50 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d){. UNUSED_PAR
8ac60 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
8ac70 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 . if( !sqlite3G
8ac80 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 lobalConfig.pHea
8ac90 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 p ){. return
8aca0 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
8acb0 7d 0a 0a 20 20 2f 2a 20 53 74 6f 72 65 20 61 20 }.. /* Store a
8acc0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d pointer to the m
8acd0 65 6d 6f 72 79 20 62 6c 6f 63 6b 20 69 6e 20 67 emory block in g
8ace0 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 lobal structure
8acf0 6d 65 6d 33 2e 20 2a 2f 0a 20 20 61 73 73 65 72 mem3. */. asser
8ad00 74 28 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c t( sizeof(Mem3Bl
8ad10 6f 63 6b 29 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d ock)==8 );. mem
8ad20 33 2e 61 50 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42 3.aPool = (Mem3B
8ad30 6c 6f 63 6b 20 2a 29 73 71 6c 69 74 65 33 47 6c lock *)sqlite3Gl
8ad40 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 obalConfig.pHeap
8ad50 3b 0a 20 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d ;. mem3.nPool =
8ad60 20 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 (sqlite3GlobalC
8ad70 6f 6e 66 69 67 2e 6e 48 65 61 70 20 2f 20 73 69 onfig.nHeap / si
8ad80 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 29 zeof(Mem3Block))
8ad90 20 2d 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 - 2;.. /* Init
8ada0 69 61 6c 69 7a 65 20 74 68 65 20 6d 61 73 74 65 ialize the maste
8adb0 72 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65 r block. */. me
8adc0 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 6d 65 m3.szMaster = me
8add0 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 m3.nPool;. mem3
8ade0 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 .mnMaster = mem3
8adf0 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 6d 65 6d .szMaster;. mem
8ae00 33 2e 69 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 3.iMaster = 1;.
8ae10 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 mem3.aPool[0].u
8ae20 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 28 6d .hdr.size4x = (m
8ae30 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3c 3c 32 29 em3.szMaster<<2)
8ae40 20 2b 20 32 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f + 2;. mem3.aPo
8ae50 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 ol[mem3.nPool].u
8ae60 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 .hdr.prevSize =
8ae70 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 mem3.nPool;. me
8ae80 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 m3.aPool[mem3.nP
8ae90 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 ool].u.hdr.size4
8aea0 78 20 3d 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e x = 1;.. return
8aeb0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
8aec0 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a *.** Deinitializ
8aed0 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a e this module..*
8aee0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 /.static void me
8aef0 6d 73 79 73 33 53 68 75 74 64 6f 77 6e 28 76 6f msys3Shutdown(vo
8af00 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 id *NotUsed){.
8af10 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
8af20 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 (NotUsed);. ret
8af30 75 72 6e 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 urn;.}..../*.**
8af40 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e Open the file in
8af50 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 dicated and writ
8af60 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 e a log of all u
8af70 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a nfreed memory .*
8af80 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e * allocations in
8af90 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a to that log..*/.
8afa0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
8afb0 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 oid sqlite3Memsy
8afc0 73 33 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 s3Dump(const cha
8afd0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 23 r *zFilename){.#
8afe0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
8aff0 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a UG. FILE *out;.
8b000 20 20 75 33 32 20 69 2c 20 6a 3b 0a 20 20 75 33 u32 i, j;. u3
8b010 32 20 73 69 7a 65 3b 0a 20 20 69 66 28 20 7a 46 2 size;. if( zF
8b020 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 ilename==0 || zF
8b030 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b ilename[0]==0 ){
8b040 0a 20 20 20 20 6f 75 74 20 3d 20 73 74 64 6f 75 . out = stdou
8b050 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 t;. }else{.
8b060 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c out = fopen(zFil
8b070 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 20 ename, "w");.
8b080 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 if( out==0 ){.
8b090 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 fprintf(std
8b0a0 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 err, "** Unable
8b0b0 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 to output memory
8b0c0 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f debug output lo
8b0d0 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 g: %s **\n",.
8b0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8b0f0 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 zFilename);.
8b100 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
8b110 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33 }. }. memsys3
8b120 45 6e 74 65 72 28 29 3b 0a 20 20 66 70 72 69 6e Enter();. fprin
8b130 74 66 28 6f 75 74 2c 20 22 43 48 55 4e 4b 53 3a tf(out, "CHUNKS:
8b140 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 31 3b \n");. for(i=1;
8b150 20 69 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 20 i<=mem3.nPool;
8b160 69 2b 3d 73 69 7a 65 2f 34 29 7b 0a 20 20 20 20 i+=size/4){.
8b170 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f size = mem3.aPoo
8b180 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[i-1].u.hdr.siz
8b190 65 34 78 3b 0a 20 20 20 20 69 66 28 20 73 69 7a e4x;. if( siz
8b1a0 65 2f 34 3c 3d 31 20 29 7b 0a 20 20 20 20 20 20 e/4<=1 ){.
8b1b0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 fprintf(out, "%p
8b1c0 20 73 69 7a 65 20 65 72 72 6f 72 5c 6e 22 2c 20 size error\n",
8b1d0 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b &mem3.aPool[i]);
8b1e0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 . assert( 0
8b1f0 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b );. break;
8b200 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 . }. if( (
8b210 73 69 7a 65 26 31 29 3d 3d 30 20 26 26 20 6d 65 size&1)==0 && me
8b220 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f m3.aPool[i+size/
8b230 34 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 4-1].u.hdr.prevS
8b240 69 7a 65 21 3d 73 69 7a 65 2f 34 20 29 7b 0a 20 ize!=size/4 ){.
8b250 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
8b260 2c 20 22 25 70 20 74 61 69 6c 20 73 69 7a 65 20 , "%p tail size
8b270 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 5c 6e does not match\n
8b280 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 ", &mem3.aPool[i
8b290 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ]);. assert
8b2a0 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 ( 0 );. bre
8b2b0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ak;. }. if
8b2c0 28 20 28 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 ( ((mem3.aPool[i
8b2d0 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64 72 +size/4-1].u.hdr
8b2e0 2e 73 69 7a 65 34 78 26 32 29 3e 3e 31 29 21 3d .size4x&2)>>1)!=
8b2f0 28 73 69 7a 65 26 31 29 20 29 7b 0a 20 20 20 20 (size&1) ){.
8b300 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
8b310 25 70 20 74 61 69 6c 20 63 68 65 63 6b 6f 75 74 %p tail checkout
8b320 20 62 69 74 20 69 73 20 69 6e 63 6f 72 72 65 63 bit is incorrec
8b330 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f t\n", &mem3.aPoo
8b340 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 l[i]);. ass
8b350 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 ert( 0 );.
8b360 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
8b370 20 69 66 28 20 73 69 7a 65 26 31 20 29 7b 0a 20 if( size&1 ){.
8b380 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
8b390 2c 20 22 25 70 20 25 36 64 20 62 79 74 65 73 20 , "%p %6d bytes
8b3a0 63 68 65 63 6b 65 64 20 6f 75 74 5c 6e 22 2c 20 checked out\n",
8b3b0 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 &mem3.aPool[i],
8b3c0 28 73 69 7a 65 2f 34 29 2a 38 2d 38 29 3b 0a 20 (size/4)*8-8);.
8b3d0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
8b3e0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 fprintf(out, "%p
8b3f0 20 25 36 64 20 62 79 74 65 73 20 66 72 65 65 25 %6d bytes free%
8b400 73 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f s\n", &mem3.aPoo
8b410 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a 38 l[i], (size/4)*8
8b420 2d 38 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 -8,.
8b430 20 20 20 20 20 20 69 3d 3d 6d 65 6d 33 2e 69 4d i==mem3.iM
8b440 61 73 74 65 72 20 3f 20 22 20 2a 2a 6d 61 73 74 aster ? " **mast
8b450 65 72 2a 2a 22 20 3a 20 22 22 29 3b 0a 20 20 20 er**" : "");.
8b460 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 }. }. for(i=0
8b470 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 ; i<MX_SMALL-1;
8b480 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 i++){. if( me
8b490 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3d 3d 30 m3.aiSmall[i]==0
8b4a0 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
8b4b0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 73 fprintf(out, "s
8b4c0 6d 61 6c 6c 28 25 32 64 29 3a 22 2c 20 69 29 3b mall(%2d):", i);
8b4d0 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65 6d . for(j = mem
8b4e0 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3b 20 6a 3e 3.aiSmall[i]; j>
8b4f0 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 0; j=mem3.aPool[
8b500 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b j].u.list.next){
8b510 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f . fprintf(o
8b520 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c 20 26 ut, " %p(%d)", &
8b530 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 mem3.aPool[j],.
8b540 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6d 65 (me
8b550 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e m3.aPool[j-1].u.
8b560 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 2d hdr.size4x/4)*8-
8b570 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 70 8);. }. fp
8b580 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 rintf(out, "\n")
8b590 3b 20 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 ; . }. for(i=0
8b5a0 3b 20 69 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 ; i<N_HASH; i++)
8b5b0 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 {. if( mem3.a
8b5c0 69 48 61 73 68 5b 69 5d 3d 3d 30 20 29 20 63 6f iHash[i]==0 ) co
8b5d0 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 69 ntinue;. fpri
8b5e0 6e 74 66 28 6f 75 74 2c 20 22 68 61 73 68 28 25 ntf(out, "hash(%
8b5f0 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66 2d):", i);. f
8b600 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 48 61 or(j = mem3.aiHa
8b610 73 68 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 sh[i]; j>0; j=me
8b620 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 m3.aPool[j].u.li
8b630 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20 st.next){.
8b640 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 25 fprintf(out, " %
8b650 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50 p(%d)", &mem3.aP
8b660 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20 ool[j],.
8b670 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f (mem3.aPoo
8b680 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[j-1].u.hdr.siz
8b690 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 e4x/4)*8-8);.
8b6a0 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f }. fprintf(o
8b6b0 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a ut, "\n"); . }.
8b6c0 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
8b6d0 6d 61 73 74 65 72 3d 25 64 5c 6e 22 2c 20 6d 65 master=%d\n", me
8b6e0 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 66 m3.iMaster);. f
8b6f0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6e 6f 77 printf(out, "now
8b700 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 Used=%d\n", mem3
8b710 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e .nPool*8 - mem3.
8b720 73 7a 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 66 szMaster*8);. f
8b730 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 78 55 printf(out, "mxU
8b740 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e sed=%d\n", mem3.
8b750 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 6d nPool*8 - mem3.m
8b760 6e 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 73 71 nMaster*8);. sq
8b770 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
8b780 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 e(mem3.mutex);.
8b790 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74 if( out==stdout
8b7a0 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73 ){. fflush(s
8b7b0 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b tdout);. }else{
8b7c0 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 . fclose(out)
8b7d0 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e ;. }.#else. UN
8b7e0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a USED_PARAMETER(z
8b7f0 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 Filename);.#endi
8b800 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 f.}../*.** This
8b810 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f routine is the o
8b820 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 nly routine in t
8b830 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 his file with ex
8b840 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 ternal .** linka
8b850 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 ge..**.** Popula
8b860 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c te the low-level
8b870 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
8b880 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e on function poin
8b890 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 ters in.** sqlit
8b8a0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
8b8b0 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 with pointers t
8b8c0 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 o the routines i
8b8d0 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 54 68 65 n this file. The
8b8e0 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 73 70 .** arguments sp
8b8f0 65 63 69 66 79 20 74 68 65 20 62 6c 6f 63 6b 20 ecify the block
8b900 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6d 61 6e of memory to man
8b910 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 age..**.** This
8b920 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 routine is only
8b930 63 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65 called by sqlite
8b940 33 5f 63 6f 6e 66 69 67 28 29 2c 20 61 6e 64 20 3_config(), and
8b950 74 68 65 72 65 66 6f 72 65 0a 2a 2a 20 69 73 20 therefore.** is
8b960 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 not required to
8b970 62 65 20 74 68 72 65 61 64 73 61 66 65 20 28 69 be threadsafe (i
8b980 74 20 69 73 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 51 t is not)..*/.SQ
8b990 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
8b9a0 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d st sqlite3_mem_m
8b9b0 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d ethods *sqlite3M
8b9c0 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 76 6f 69 emGetMemsys3(voi
8b9d0 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e d){. static con
8b9e0 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d st sqlite3_mem_m
8b9f0 65 74 68 6f 64 73 20 6d 65 6d 70 6f 6f 6c 4d 65 ethods mempoolMe
8ba00 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 6d thods = {. m
8ba10 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 2c 0a 20 20 emsys3Malloc,.
8ba20 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 2c 0a memsys3Free,.
8ba30 20 20 20 20 20 6d 65 6d 73 79 73 33 52 65 61 6c memsys3Real
8ba40 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 loc,. memsys
8ba50 33 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d 73 3Size,. mems
8ba60 79 73 33 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 ys3Roundup,.
8ba70 20 6d 65 6d 73 79 73 33 49 6e 69 74 2c 0a 20 20 memsys3Init,.
8ba80 20 20 20 6d 65 6d 73 79 73 33 53 68 75 74 64 6f memsys3Shutdo
8ba90 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a wn,. 0. };.
8baa0 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 70 6f 6f return &mempoo
8bab0 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e lMethods;.}..#en
8bac0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e dif /* SQLITE_EN
8bad0 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 2a 2f 0a ABLE_MEMSYS3 */.
8bae0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
8baf0 20 45 6e 64 20 6f 66 20 6d 65 6d 33 2e 63 20 2a End of mem3.c *
8bb00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8bb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8bb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
8bb30 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
8bb40 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 35 Begin file mem5
8bb50 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
8bb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8bb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
8bb80 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f ./*.** 2007 Octo
8bb90 62 65 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 14.**.** The
8bba0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
8bbb0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
8bbc0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
8bbd0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
8bbe0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
8bbf0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
8bc00 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
8bc10 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
8bc20 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
8bc30 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
8bc40 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
8bc50 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
8bc60 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
8bc70 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
8bc80 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
8bc90 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
8bca0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
8bcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8bcc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8bcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8bce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8bcf0 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
8bd00 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
8bd10 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 C functions that
8bd20 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d implement a mem
8bd30 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f ory.** allocatio
8bd40 6e 20 73 75 62 73 79 73 74 65 6d 20 66 6f 72 20 n subsystem for
8bd50 75 73 65 20 62 79 20 53 51 4c 69 74 65 2e 20 0a use by SQLite. .
8bd60 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 **.** This versi
8bd70 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 on of the memory
8bd80 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
8bd90 79 73 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a ystem omits all.
8bda0 2a 2a 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 ** use of malloc
8bdb0 28 29 2e 20 54 68 65 20 61 70 70 6c 69 63 61 74 (). The applicat
8bdc0 69 6f 6e 20 67 69 76 65 73 20 53 51 4c 69 74 65 ion gives SQLite
8bdd0 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f a block of memo
8bde0 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c ry.** before cal
8bdf0 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e 69 ling sqlite3_ini
8be00 74 69 61 6c 69 7a 65 28 29 20 66 72 6f 6d 20 77 tialize() from w
8be10 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 hich allocations
8be20 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 61 6e 64 .** are made and
8be30 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 returned by the
8be40 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 78 xMalloc() and x
8be50 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 69 6d Realloc() .** im
8be60 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 20 4f plementations. O
8be70 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 nce sqlite3_init
8be80 69 61 6c 69 7a 65 28 29 20 68 61 73 20 62 65 65 ialize() has bee
8be90 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65 n called,.** the
8bea0 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 amount of memor
8beb0 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 53 y available to S
8bec0 51 4c 69 74 65 20 69 73 20 66 69 78 65 64 20 61 QLite is fixed a
8bed0 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 nd cannot.** be
8bee0 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 changed..**.** T
8bef0 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 his version of t
8bf00 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
8bf10 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 69 tion subsystem i
8bf20 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69 6e s included.** in
8bf30 20 74 68 65 20 62 75 69 6c 64 20 6f 6e 6c 79 20 the build only
8bf40 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 if SQLITE_ENABLE
8bf50 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66 69 _MEMSYS5 is defi
8bf60 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ned..**.** This
8bf70 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
8bf80 20 75 73 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 uses the follow
8bf90 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 3a 0a 2a ing algorithm:.*
8bfa0 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 6c 6c 20 6d *.** 1. All m
8bfb0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
8bfc0 73 20 73 69 7a 65 73 20 61 72 65 20 72 6f 75 6e s sizes are roun
8bfd0 64 65 64 20 75 70 20 74 6f 20 61 20 70 6f 77 65 ded up to a powe
8bfe0 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 r of 2..**.**
8bff0 32 2e 20 20 49 66 20 74 77 6f 20 61 64 6a 61 63 2. If two adjac
8c000 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 ent free blocks
8c010 61 72 65 20 74 68 65 20 68 61 6c 76 65 73 20 6f are the halves o
8c020 66 20 61 20 6c 61 72 67 65 72 20 62 6c 6f 63 6b f a larger block
8c030 2c 0a 2a 2a 20 20 20 20 20 20 20 74 68 65 6e 20 ,.** then
8c040 74 68 65 20 74 77 6f 20 62 6c 6f 63 6b 73 20 61 the two blocks a
8c050 72 65 20 63 6f 61 6c 65 73 65 64 20 69 6e 74 6f re coalesed into
8c060 20 74 68 65 20 73 69 6e 67 6c 65 20 6c 61 72 67 the single larg
8c070 65 72 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 er block..**.**
8c080 20 20 33 2e 20 20 4e 65 77 20 6d 65 6d 6f 72 79 3. New memory
8c090 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 72 is allocated fr
8c0a0 6f 6d 20 74 68 65 20 66 69 72 73 74 20 61 76 61 om the first ava
8c0b0 69 6c 61 62 6c 65 20 66 72 65 65 20 62 6c 6f 63 ilable free bloc
8c0c0 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 6c k..**.** This al
8c0d0 67 6f 72 69 74 68 6d 20 69 73 20 64 65 73 63 72 gorithm is descr
8c0e0 69 62 65 64 20 69 6e 3a 20 4a 2e 20 4d 2e 20 52 ibed in: J. M. R
8c0f0 6f 62 73 6f 6e 2e 20 22 42 6f 75 6e 64 73 20 66 obson. "Bounds f
8c100 6f 72 20 53 6f 6d 65 20 46 75 6e 63 74 69 6f 6e or Some Function
8c110 73 0a 2a 2a 20 43 6f 6e 63 65 72 6e 69 6e 67 20 s.** Concerning
8c120 44 79 6e 61 6d 69 63 20 53 74 6f 72 61 67 65 20 Dynamic Storage
8c130 41 6c 6c 6f 63 61 74 69 6f 6e 22 2e 20 4a 6f 75 Allocation". Jou
8c140 72 6e 61 6c 20 6f 66 20 74 68 65 20 41 73 73 6f rnal of the Asso
8c150 63 69 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 43 ciation for.** C
8c160 6f 6d 70 75 74 69 6e 67 20 4d 61 63 68 69 6e 65 omputing Machine
8c170 72 79 2c 20 56 6f 6c 75 6d 65 20 32 31 2c 20 4e ry, Volume 21, N
8c180 75 6d 62 65 72 20 38 2c 20 4a 75 6c 79 20 31 39 umber 8, July 19
8c190 37 34 2c 20 70 61 67 65 73 20 34 39 31 2d 34 39 74, pages 491-49
8c1a0 39 2e 0a 2a 2a 20 0a 2a 2a 20 4c 65 74 20 6e 20 9..** .** Let n
8c1b0 62 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 be the size of t
8c1c0 68 65 20 6c 61 72 67 65 73 74 20 61 6c 6c 6f 63 he largest alloc
8c1d0 61 74 69 6f 6e 20 64 69 76 69 64 65 64 20 62 79 ation divided by
8c1e0 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 2a 2a 20 the minimum.**
8c1f0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 allocation size
8c200 28 61 66 74 65 72 20 72 6f 75 6e 64 69 6e 67 20 (after rounding
8c210 61 6c 6c 20 73 69 7a 65 73 20 75 70 20 74 6f 20 all sizes up to
8c220 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 29 20 20 a power of 2.)
8c230 4c 65 74 20 4d 0a 2a 2a 20 62 65 20 74 68 65 20 Let M.** be the
8c240 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f maximum amount o
8c250 66 20 6d 65 6d 6f 72 79 20 65 76 65 72 20 6f 75 f memory ever ou
8c260 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 tstanding at one
8c270 20 74 69 6d 65 2e 20 20 4c 65 74 0a 2a 2a 20 4e time. Let.** N
8c280 20 62 65 20 74 68 65 20 74 6f 74 61 6c 20 61 6d be the total am
8c290 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 61 ount of memory a
8c2a0 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c vailable for all
8c2b0 6f 63 61 74 69 6f 6e 2e 20 20 52 6f 62 73 6f 6e ocation. Robson
8c2c0 0a 2a 2a 20 70 72 6f 76 65 64 20 74 68 61 74 20 .** proved that
8c2d0 74 68 69 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f this memory allo
8c2e0 63 61 74 6f 72 20 77 69 6c 6c 20 6e 65 76 65 72 cator will never
8c2f0 20 62 72 65 61 6b 64 6f 77 6e 20 64 75 65 20 74 breakdown due t
8c300 6f 20 0a 2a 2a 20 66 72 61 67 6d 65 6e 74 61 74 o .** fragmentat
8c310 69 6f 6e 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 ion as long as t
8c320 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e he following con
8c330 73 74 72 61 69 6e 74 20 68 6f 6c 64 73 3a 0a 2a straint holds:.*
8c340 2a 0a 2a 2a 20 20 20 20 20 20 4e 20 3e 3d 20 20 *.** N >=
8c350 4d 2a 28 31 20 2b 20 6c 6f 67 32 28 6e 29 2f 32 M*(1 + log2(n)/2
8c360 29 20 2d 20 6e 20 2b 20 31 0a 2a 2a 0a 2a 2a 20 ) - n + 1.**.**
8c370 54 68 65 20 73 71 6c 69 74 65 33 5f 73 74 61 74 The sqlite3_stat
8c380 75 73 28 29 20 6c 6f 67 69 63 20 74 72 61 63 6b us() logic track
8c390 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 s the maximum va
8c3a0 6c 75 65 73 20 6f 66 20 6e 20 61 6e 64 20 4d 20 lues of n and M
8c3b0 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6e 20 61 70 so.** that an ap
8c3c0 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 2c 20 61 plication can, a
8c3d0 74 20 61 6e 79 20 74 69 6d 65 2c 20 76 65 72 69 t any time, veri
8c3e0 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 fy this constrai
8c3f0 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 nt..*/../*.** Th
8c400 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 is version of th
8c410 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
8c420 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 or is used only
8c430 77 68 65 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f when .** SQLITE_
8c440 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69 ENABLE_MEMSYS5 i
8c450 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 s defined..*/.#i
8c460 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
8c470 4c 45 5f 4d 45 4d 53 59 53 35 0a 0a 2f 2a 0a 2a LE_MEMSYS5../*.*
8c480 2a 20 41 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f * A minimum allo
8c490 63 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 73 cation is an ins
8c4a0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
8c4b0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
8c4c0 2e 0a 2a 2a 20 4c 61 72 67 65 72 20 61 6c 6c 6f ..** Larger allo
8c4d0 63 61 74 69 6f 6e 73 20 61 72 65 20 61 6e 20 61 cations are an a
8c4e0 72 72 61 79 20 6f 66 20 74 68 65 73 65 20 73 74 rray of these st
8c4f0 72 75 63 74 75 72 65 73 20 77 68 65 72 65 20 74 ructures where t
8c500 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 he.** size of th
8c510 65 20 61 72 72 61 79 20 69 73 20 61 20 70 6f 77 e array is a pow
8c520 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 54 er of 2..**.** T
8c530 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 20 he size of this
8c540 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65 20 61 object must be a
8c550 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 power of two.
8c560 54 68 61 74 20 66 61 63 74 20 69 73 0a 2a 2a 20 That fact is.**
8c570 76 65 72 69 66 69 65 64 20 69 6e 20 6d 65 6d 73 verified in mems
8c580 79 73 35 49 6e 69 74 28 29 2e 0a 2a 2f 0a 74 79 ys5Init()..*/.ty
8c590 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d pedef struct Mem
8c5a0 35 4c 69 6e 6b 20 4d 65 6d 35 4c 69 6e 6b 3b 0a 5Link Mem5Link;.
8c5b0 73 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 struct Mem5Link
8c5c0 7b 0a 20 20 69 6e 74 20 6e 65 78 74 3b 20 20 20 {. int next;
8c5d0 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
8c5e0 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b 20 next free chunk
8c5f0 2a 2f 0a 20 20 69 6e 74 20 70 72 65 76 3b 20 20 */. int prev;
8c600 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
8c610 20 70 72 65 76 69 6f 75 73 20 66 72 65 65 20 63 previous free c
8c620 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a hunk */.};../*.*
8c630 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f * Maximum size o
8c640 66 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f 6e f any allocation
8c650 20 69 73 20 28 28 31 3c 3c 4c 4f 47 4d 41 58 29 is ((1<<LOGMAX)
8c660 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 2e 20 53 *mem5.szAtom). S
8c670 69 6e 63 65 0a 2a 2a 20 6d 65 6d 35 2e 73 7a 41 ince.** mem5.szA
8c680 74 6f 6d 20 69 73 20 61 6c 77 61 79 73 20 61 74 tom is always at
8c690 20 6c 65 61 73 74 20 38 20 61 6e 64 20 33 32 2d least 8 and 32-
8c6a0 62 69 74 20 69 6e 74 65 67 65 72 73 20 61 72 65 bit integers are
8c6b0 20 75 73 65 64 2c 0a 2a 2a 20 69 74 20 69 73 20 used,.** it is
8c6c0 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 70 6f 73 not actually pos
8c6d0 73 69 62 6c 65 20 74 6f 20 72 65 61 63 68 20 74 sible to reach t
8c6e0 68 69 73 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64 his limit..*/.#d
8c6f0 65 66 69 6e 65 20 4c 4f 47 4d 41 58 20 33 30 0a efine LOGMAX 30.
8c700 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b 73 20 75 73 65 ./*.** Masks use
8c710 64 20 66 6f 72 20 6d 65 6d 35 2e 61 43 74 72 6c d for mem5.aCtrl
8c720 5b 5d 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a [] elements..*/.
8c730 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 4c 4f 47 #define CTRL_LOG
8c740 53 49 5a 45 20 20 30 78 31 66 20 20 20 20 2f 2a SIZE 0x1f /*
8c750 20 4c 6f 67 32 20 53 69 7a 65 20 6f 66 20 74 68 Log2 Size of th
8c760 69 73 20 62 6c 6f 63 6b 20 2a 2f 0a 23 64 65 66 is block */.#def
8c770 69 6e 65 20 43 54 52 4c 5f 46 52 45 45 20 20 20 ine CTRL_FREE
8c780 20 20 30 78 32 30 20 20 20 20 2f 2a 20 54 72 75 0x20 /* Tru
8c790 65 20 69 66 20 6e 6f 74 20 63 68 65 63 6b 65 64 e if not checked
8c7a0 20 6f 75 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 out */../*.** A
8c7b0 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 ll of the static
8c7c0 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 variables used
8c7d0 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 by this module a
8c7e0 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 re collected.**
8c7f0 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 into a single st
8c800 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d ructure named "m
8c810 65 6d 35 22 2e 20 20 54 68 69 73 20 69 73 20 74 em5". This is t
8c820 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 o keep the.** st
8c830 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f atic variables o
8c840 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 rganized and to
8c850 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 reduce namespace
8c860 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 pollution.** wh
8c870 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 en this module i
8c880 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 s combined with
8c890 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 other in the ama
8c8a0 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 lgamation..*/.st
8c8b0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 atic SQLITE_WSD
8c8c0 73 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 struct Mem5Globa
8c8d0 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 l {. /*. ** Me
8c8e0 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66 mory available f
8c8f0 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 20 20 or allocation.
8c900 2a 2f 0a 20 20 69 6e 74 20 73 7a 41 74 6f 6d 3b */. int szAtom;
8c910 20 20 20 20 20 20 2f 2a 20 53 6d 61 6c 6c 65 73 /* Smalles
8c920 74 20 70 6f 73 73 69 62 6c 65 20 61 6c 6c 6f 63 t possible alloc
8c930 61 74 69 6f 6e 20 69 6e 20 62 79 74 65 73 20 2a ation in bytes *
8c940 2f 0a 20 20 69 6e 74 20 6e 42 6c 6f 63 6b 3b 20 /. int nBlock;
8c950 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
8c960 66 20 73 7a 41 74 6f 6d 20 73 69 7a 65 64 20 62 f szAtom sized b
8c970 6c 6f 63 6b 73 20 69 6e 20 7a 50 6f 6f 6c 20 2a locks in zPool *
8c980 2f 0a 20 20 75 38 20 2a 7a 50 6f 6f 6c 3b 20 20 /. u8 *zPool;
8c990 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 61 /* Memory a
8c9a0 76 61 69 6c 61 62 6c 65 20 74 6f 20 62 65 20 61 vailable to be a
8c9b0 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 0a 20 llocated */. .
8c9c0 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 /*. ** Mutex t
8c9d0 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 o control access
8c9e0 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 to the memory a
8c9f0 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
8ca00 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 tem.. */. sqli
8ca10 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
8ca20 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 65 72 ;.. /*. ** Per
8ca30 66 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 73 74 formance statist
8ca40 69 63 73 0a 20 20 2a 2f 0a 20 20 75 36 34 20 6e ics. */. u64 n
8ca50 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 2f Alloc; /
8ca60 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f * Total number o
8ca70 66 20 63 61 6c 6c 73 20 74 6f 20 6d 61 6c 6c 6f f calls to mallo
8ca80 63 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 6c c */. u64 total
8ca90 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 54 6f Alloc; /* To
8caa0 74 61 6c 20 6f 66 20 61 6c 6c 20 6d 61 6c 6c 6f tal of all mallo
8cab0 63 20 63 61 6c 6c 73 20 2d 20 69 6e 63 6c 75 64 c calls - includ
8cac0 65 73 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 es internal frag
8cad0 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 6c 45 */. u64 totalE
8cae0 78 63 65 73 73 3b 20 20 20 20 2f 2a 20 54 6f 74 xcess; /* Tot
8caf0 61 6c 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 al internal frag
8cb00 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 mentation */. u
8cb10 33 32 20 63 75 72 72 65 6e 74 4f 75 74 3b 20 20 32 currentOut;
8cb20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 63 68 /* Current ch
8cb30 65 63 6b 6f 75 74 2c 20 69 6e 63 6c 75 64 69 6e eckout, includin
8cb40 67 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 6d g internal fragm
8cb50 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 33 entation */. u3
8cb60 32 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 3b 20 2 currentCount;
8cb70 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d /* Current num
8cb80 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63 74 20 ber of distinct
8cb90 63 68 65 63 6b 6f 75 74 73 20 2a 2f 0a 20 20 75 checkouts */. u
8cba0 33 32 20 6d 61 78 4f 75 74 3b 20 20 20 20 20 20 32 maxOut;
8cbb0 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 6e /* Maximum in
8cbc0 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75 72 72 stantaneous curr
8cbd0 65 6e 74 4f 75 74 20 2a 2f 0a 20 20 75 33 32 20 entOut */. u32
8cbe0 6d 61 78 43 6f 75 6e 74 3b 20 20 20 20 20 20 20 maxCount;
8cbf0 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61 /* Maximum insta
8cc00 6e 74 61 6e 65 6f 75 73 20 63 75 72 72 65 6e 74 ntaneous current
8cc10 43 6f 75 6e 74 20 2a 2f 0a 20 20 75 33 32 20 6d Count */. u32 m
8cc20 61 78 52 65 71 75 65 73 74 3b 20 20 20 20 20 2f axRequest; /
8cc30 2a 20 4c 61 72 67 65 73 74 20 61 6c 6c 6f 63 61 * Largest alloca
8cc40 74 69 6f 6e 20 28 65 78 63 6c 75 73 69 76 65 20 tion (exclusive
8cc50 6f 66 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 of internal frag
8cc60 29 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a ) */. . /*. *
8cc70 2a 20 4c 69 73 74 73 20 6f 66 20 66 72 65 65 20 * Lists of free
8cc80 62 6c 6f 63 6b 73 2e 20 20 61 69 46 72 65 65 6c blocks. aiFreel
8cc90 69 73 74 5b 30 5d 20 69 73 20 61 20 6c 69 73 74 ist[0] is a list
8cca0 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 of free blocks
8ccb0 6f 66 0a 20 20 2a 2a 20 73 69 7a 65 20 6d 65 6d of. ** size mem
8ccc0 35 2e 73 7a 41 74 6f 6d 2e 20 20 61 69 46 72 65 5.szAtom. aiFre
8ccd0 65 6c 69 73 74 5b 31 5d 20 68 6f 6c 64 73 20 62 elist[1] holds b
8cce0 6c 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 73 7a locks of size sz
8ccf0 41 74 6f 6d 2a 32 2e 0a 20 20 2a 2a 20 61 6e 64 Atom*2.. ** and
8cd00 20 73 6f 20 66 6f 72 74 68 2e 0a 20 20 2a 2f 0a so forth.. */.
8cd10 20 20 69 6e 74 20 61 69 46 72 65 65 6c 69 73 74 int aiFreelist
8cd20 5b 4c 4f 47 4d 41 58 2b 31 5d 3b 0a 0a 20 20 2f [LOGMAX+1];.. /
8cd30 2a 0a 20 20 2a 2a 20 53 70 61 63 65 20 66 6f 72 *. ** Space for
8cd40 20 74 72 61 63 6b 69 6e 67 20 77 68 69 63 68 20 tracking which
8cd50 62 6c 6f 63 6b 73 20 61 72 65 20 63 68 65 63 6b blocks are check
8cd60 65 64 20 6f 75 74 20 61 6e 64 20 74 68 65 20 73 ed out and the s
8cd70 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68 ize. ** of each
8cd80 20 62 6c 6f 63 6b 2e 20 20 4f 6e 65 20 62 79 74 block. One byt
8cd90 65 20 70 65 72 20 62 6c 6f 63 6b 2e 0a 20 20 2a e per block.. *
8cda0 2f 0a 20 20 75 38 20 2a 61 43 74 72 6c 3b 0a 0a /. u8 *aCtrl;..
8cdb0 7d 20 6d 65 6d 35 20 3d 20 7b 20 30 20 7d 3b 0a } mem5 = { 0 };.
8cdc0 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 68 ./*.** Access th
8cdd0 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c e static variabl
8cde0 65 20 74 68 72 6f 75 67 68 20 61 20 6d 61 63 72 e through a macr
8cdf0 6f 20 66 6f 72 20 53 51 4c 49 54 45 5f 4f 4d 49 o for SQLITE_OMI
8ce00 54 5f 57 53 44 0a 2a 2f 0a 23 64 65 66 69 6e 65 T_WSD.*/.#define
8ce10 20 6d 65 6d 35 20 47 4c 4f 42 41 4c 28 73 74 72 mem5 GLOBAL(str
8ce20 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c 2c 20 uct Mem5Global,
8ce30 6d 65 6d 35 29 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 mem5)../*.** Ass
8ce40 75 6d 69 6e 67 20 6d 65 6d 35 2e 7a 50 6f 6f 6c uming mem5.zPool
8ce50 20 69 73 20 64 69 76 69 64 65 64 20 75 70 20 69 is divided up i
8ce60 6e 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 nto an array of
8ce70 4d 65 6d 35 4c 69 6e 6b 0a 2a 2a 20 73 74 72 75 Mem5Link.** stru
8ce80 63 74 75 72 65 73 2c 20 72 65 74 75 72 6e 20 61 ctures, return a
8ce90 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
8cea0 69 64 78 2d 74 68 20 73 75 63 68 20 6c 69 6b 2e idx-th such lik.
8ceb0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 35 .*/.#define MEM5
8cec0 4c 49 4e 4b 28 69 64 78 29 20 28 28 4d 65 6d 35 LINK(idx) ((Mem5
8ced0 4c 69 6e 6b 20 2a 29 28 26 6d 65 6d 35 2e 7a 50 Link *)(&mem5.zP
8cee0 6f 6f 6c 5b 28 69 64 78 29 2a 6d 65 6d 35 2e 73 ool[(idx)*mem5.s
8cef0 7a 41 74 6f 6d 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20 zAtom]))../*.**
8cf00 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b Unlink the chunk
8cf10 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69 at mem5.aPool[i
8cf20 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20 69 ] from list it i
8cf30 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f s currently.** o
8cf40 6e 2e 20 20 49 74 20 73 68 6f 75 6c 64 20 62 65 n. It should be
8cf50 20 66 6f 75 6e 64 20 6f 6e 20 6d 65 6d 35 2e 61 found on mem5.a
8cf60 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 iFreelist[iLogsi
8cf70 7a 65 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ze]..*/.static v
8cf80 6f 69 64 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e oid memsys5Unlin
8cf90 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f k(int i, int iLo
8cfa0 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 6e 65 gsize){. int ne
8cfb0 78 74 2c 20 70 72 65 76 3b 0a 20 20 61 73 73 65 xt, prev;. asse
8cfc0 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65 rt( i>=0 && i<me
8cfd0 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 m5.nBlock );. a
8cfe0 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e ssert( iLogsize>
8cff0 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d =0 && iLogsize<=
8d000 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73 65 LOGMAX );. asse
8d010 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b rt( (mem5.aCtrl[
8d020 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a i] & CTRL_LOGSIZ
8d030 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b 0a E)==iLogsize );.
8d040 0a 20 20 6e 65 78 74 20 3d 20 4d 45 4d 35 4c 49 . next = MEM5LI
8d050 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 70 NK(i)->next;. p
8d060 72 65 76 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 rev = MEM5LINK(i
8d070 29 2d 3e 70 72 65 76 3b 0a 20 20 69 66 28 20 70 )->prev;. if( p
8d080 72 65 76 3c 30 20 29 7b 0a 20 20 20 20 6d 65 6d rev<0 ){. mem
8d090 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 5.aiFreelist[iLo
8d0a0 67 73 69 7a 65 5d 20 3d 20 6e 65 78 74 3b 0a 20 gsize] = next;.
8d0b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 45 4d 35 }else{. MEM5
8d0c0 4c 49 4e 4b 28 70 72 65 76 29 2d 3e 6e 65 78 74 LINK(prev)->next
8d0d0 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 = next;. }. i
8d0e0 66 28 20 6e 65 78 74 3e 3d 30 20 29 7b 0a 20 20 f( next>=0 ){.
8d0f0 20 20 4d 45 4d 35 4c 49 4e 4b 28 6e 65 78 74 29 MEM5LINK(next)
8d100 2d 3e 70 72 65 76 20 3d 20 70 72 65 76 3b 0a 20 ->prev = prev;.
8d110 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b }.}../*.** Link
8d120 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 the chunk at me
8d130 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f 20 74 m5.aPool[i] so t
8d140 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20 69 4c hat is on the iL
8d150 6f 67 73 69 7a 65 0a 2a 2a 20 66 72 65 65 20 6c ogsize.** free l
8d160 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ist..*/.static v
8d170 6f 69 64 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 oid memsys5Link(
8d180 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f 67 73 int i, int iLogs
8d190 69 7a 65 29 7b 0a 20 20 69 6e 74 20 78 3b 0a 20 ize){. int x;.
8d1a0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
8d1b0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 35 _mutex_held(mem5
8d1c0 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 .mutex) );. ass
8d1d0 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d ert( i>=0 && i<m
8d1e0 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 em5.nBlock );.
8d1f0 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 assert( iLogsize
8d200 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c >=0 && iLogsize<
8d210 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73 =LOGMAX );. ass
8d220 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c ert( (mem5.aCtrl
8d230 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 [i] & CTRL_LOGSI
8d240 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b ZE)==iLogsize );
8d250 0a 0a 20 20 78 20 3d 20 4d 45 4d 35 4c 49 4e 4b .. x = MEM5LINK
8d260 28 69 29 2d 3e 6e 65 78 74 20 3d 20 6d 65 6d 35 (i)->next = mem5
8d270 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 .aiFreelist[iLog
8d280 73 69 7a 65 5d 3b 0a 20 20 4d 45 4d 35 4c 49 4e size];. MEM5LIN
8d290 4b 28 69 29 2d 3e 70 72 65 76 20 3d 20 2d 31 3b K(i)->prev = -1;
8d2a0 0a 20 20 69 66 28 20 78 3e 3d 30 20 29 7b 0a 20 . if( x>=0 ){.
8d2b0 20 20 20 61 73 73 65 72 74 28 20 78 3c 6d 65 6d assert( x<mem
8d2c0 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 20 20 5.nBlock );.
8d2d0 4d 45 4d 35 4c 49 4e 4b 28 78 29 2d 3e 70 72 65 MEM5LINK(x)->pre
8d2e0 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 6d 65 6d v = i;. }. mem
8d2f0 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 5.aiFreelist[iLo
8d300 67 73 69 7a 65 5d 20 3d 20 69 3b 0a 7d 0a 0a 2f gsize] = i;.}../
8d310 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54 *.** If the STAT
8d320 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 IC_MEM mutex is
8d330 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 not already held
8d340 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e , obtain it now.
8d350 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 The mutex.** wi
8d360 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65 ll already be he
8d370 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20 ld (obtained by
8d380 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 code in malloc.c
8d390 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 ) if.** sqlite3G
8d3a0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d lobalConfig.bMem
8d3b0 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f Stat is true..*/
8d3c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
8d3d0 73 79 73 35 45 6e 74 65 72 28 76 6f 69 64 29 7b sys5Enter(void){
8d3e0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
8d3f0 5f 65 6e 74 65 72 28 6d 65 6d 35 2e 6d 75 74 65 _enter(mem5.mute
8d400 78 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 x);.}.static voi
8d410 64 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 76 d memsys5Leave(v
8d420 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f oid){. sqlite3_
8d430 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 35 mutex_leave(mem5
8d440 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a .mutex);.}../*.*
8d450 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a * Return the siz
8d460 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 e of an outstand
8d470 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 ing allocation,
8d480 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65 0a 2a in bytes. The.*
8d490 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65 64 20 * size returned
8d4a0 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79 74 65 omits the 8-byte
8d4b0 20 68 65 61 64 65 72 20 6f 76 65 72 68 65 61 64 header overhead
8d4c0 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 . This only.**
8d4d0 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73 works for chunks
8d4e0 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e that are curren
8d4f0 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e tly checked out.
8d500 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
8d510 65 6d 73 79 73 35 53 69 7a 65 28 76 6f 69 64 20 emsys5Size(void
8d520 2a 70 29 7b 0a 20 20 69 6e 74 20 69 53 69 7a 65 *p){. int iSize
8d530 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29 7b = 0;. if( p ){
8d540 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 28 28 75 . int i = ((u
8d550 38 20 2a 29 70 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 8 *)p-mem5.zPool
8d560 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 20 )/mem5.szAtom;.
8d570 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 assert( i>=0
8d580 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b && i<mem5.nBlock
8d590 20 29 3b 0a 20 20 20 20 69 53 69 7a 65 20 3d 20 );. iSize =
8d5a0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 2a 20 28 31 mem5.szAtom * (1
8d5b0 20 3c 3c 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b << (mem5.aCtrl[
8d5c0 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 i]&CTRL_LOGSIZE)
8d5d0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
8d5e0 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 iSize;.}../*.**
8d5f0 46 69 6e 64 20 74 68 65 20 66 69 72 73 74 20 65 Find the first e
8d600 6e 74 72 79 20 6f 6e 20 74 68 65 20 66 72 65 65 ntry on the free
8d610 6c 69 73 74 20 69 4c 6f 67 73 69 7a 65 2e 20 20 list iLogsize.
8d620 55 6e 6c 69 6e 6b 20 74 68 61 74 0a 2a 2a 20 65 Unlink that.** e
8d630 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e 20 ntry and return
8d640 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2f 0a 73 its index. .*/.s
8d650 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 tatic int memsys
8d660 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 6e 74 5UnlinkFirst(int
8d670 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e iLogsize){. in
8d680 74 20 69 3b 0a 20 20 69 6e 74 20 69 46 69 72 73 t i;. int iFirs
8d690 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 4c t;.. assert( iL
8d6a0 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f ogsize>=0 && iLo
8d6b0 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b gsize<=LOGMAX );
8d6c0 0a 20 20 69 20 3d 20 69 46 69 72 73 74 20 3d 20 . i = iFirst =
8d6d0 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b mem5.aiFreelist[
8d6e0 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 61 73 73 iLogsize];. ass
8d6f0 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30 20 29 ert( iFirst>=0 )
8d700 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29 ;. while( i>0 )
8d710 7b 0a 20 20 20 20 69 66 28 20 69 3c 69 46 69 72 {. if( i<iFir
8d720 73 74 20 29 20 69 46 69 72 73 74 20 3d 20 69 3b st ) iFirst = i;
8d730 0a 20 20 20 20 69 20 3d 20 4d 45 4d 35 4c 49 4e . i = MEM5LIN
8d740 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a K(i)->next;. }.
8d750 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 memsys5Unlink(
8d760 69 46 69 72 73 74 2c 20 69 4c 6f 67 73 69 7a 65 iFirst, iLogsize
8d770 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 69 72 );. return iFir
8d780 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 st;.}../*.** Ret
8d790 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d urn a block of m
8d7a0 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 73 emory of at leas
8d7b0 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a 65 t nBytes in size
8d7c0 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c ..** Return NULL
8d7d0 20 69 66 20 75 6e 61 62 6c 65 2e 20 20 52 65 74 if unable. Ret
8d7e0 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 42 79 74 urn NULL if nByt
8d7f0 65 73 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 es==0..**.** The
8d800 20 63 61 6c 6c 65 72 20 67 75 61 72 61 6e 74 65 caller guarante
8d810 65 73 20 74 68 61 74 20 6e 42 79 74 65 20 70 6f es that nByte po
8d820 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 sitive..**.** Th
8d830 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 62 74 e caller has obt
8d840 61 69 6e 65 64 20 61 20 6d 75 74 65 78 20 70 72 ained a mutex pr
8d850 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 ior to invoking
8d860 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 this.** routine
8d870 73 6f 20 74 68 65 72 65 20 69 73 20 6e 65 76 65 so there is neve
8d880 72 20 61 6e 79 20 63 68 61 6e 63 65 20 74 68 61 r any chance tha
8d890 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a t two or more.**
8d8a0 20 74 68 72 65 61 64 73 20 63 61 6e 20 62 65 20 threads can be
8d8b0 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 in this routine
8d8c0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
8d8d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
8d8e0 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 *memsys5MallocU
8d8f0 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 65 29 nsafe(int nByte)
8d900 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 {. int i;
8d910 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
8d920 20 61 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 5d 20 a mem5.aPool[]
8d930 73 6c 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 69 42 slot */. int iB
8d940 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 6e in; /* In
8d950 64 65 78 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 69 dex into mem5.ai
8d960 46 72 65 65 6c 69 73 74 5b 5d 20 2a 2f 0a 20 20 Freelist[] */.
8d970 69 6e 74 20 69 46 75 6c 6c 53 7a 3b 20 20 20 20 int iFullSz;
8d980 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c 6f /* Size of allo
8d990 63 61 74 69 6f 6e 20 72 6f 75 6e 64 65 64 20 75 cation rounded u
8d9a0 70 20 74 6f 20 70 6f 77 65 72 20 6f 66 20 32 20 p to power of 2
8d9b0 2a 2f 0a 20 20 69 6e 74 20 69 4c 6f 67 73 69 7a */. int iLogsiz
8d9c0 65 3b 20 20 20 20 2f 2a 20 4c 6f 67 32 20 6f 66 e; /* Log2 of
8d9d0 20 69 46 75 6c 6c 53 7a 2f 50 4f 57 32 5f 4d 49 iFullSz/POW2_MI
8d9e0 4e 20 2a 2f 0a 0a 20 20 2f 2a 20 6e 42 79 74 65 N */.. /* nByte
8d9f0 20 6d 75 73 74 20 62 65 20 61 20 70 6f 73 69 74 must be a posit
8da00 69 76 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ive */. assert(
8da10 20 6e 42 79 74 65 3e 30 20 29 3b 0a 0a 20 20 2f nByte>0 );.. /
8da20 2a 20 4b 65 65 70 20 74 72 61 63 6b 20 6f 66 20 * Keep track of
8da30 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f the maximum allo
8da40 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 2e 20 cation request.
8da50 20 45 76 65 6e 20 75 6e 66 75 6c 66 69 6c 6c 65 Even unfulfille
8da60 64 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 73 20 d. ** requests
8da70 61 72 65 20 63 6f 75 6e 74 65 64 20 2a 2f 0a 20 are counted */.
8da80 20 69 66 28 20 28 75 33 32 29 6e 42 79 74 65 3e if( (u32)nByte>
8da90 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 mem5.maxRequest
8daa0 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 61 78 52 ){. mem5.maxR
8dab0 65 71 75 65 73 74 20 3d 20 6e 42 79 74 65 3b 0a equest = nByte;.
8dac0 20 20 7d 0a 0a 20 20 2f 2a 20 41 62 6f 72 74 20 }.. /* Abort
8dad0 69 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 if the requested
8dae0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 allocation size
8daf0 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 is larger than
8db00 74 68 65 20 6c 61 72 67 65 73 74 0a 20 20 2a 2a the largest. **
8db10 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 74 68 power of two th
8db20 61 74 20 77 65 20 63 61 6e 20 72 65 70 72 65 73 at we can repres
8db30 65 6e 74 20 75 73 69 6e 67 20 33 32 2d 62 69 74 ent using 32-bit
8db40 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 signed integers
8db50 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 79 .. */. if( nBy
8db60 74 65 20 3e 20 30 78 34 30 30 30 30 30 30 30 20 te > 0x40000000
8db70 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
8db80 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 75 6e 64 . }.. /* Round
8db90 20 6e 42 79 74 65 20 75 70 20 74 6f 20 74 68 65 nByte up to the
8dba0 20 6e 65 78 74 20 76 61 6c 69 64 20 70 6f 77 65 next valid powe
8dbb0 72 20 6f 66 20 74 77 6f 20 2a 2f 0a 20 20 66 6f r of two */. fo
8dbc0 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e 73 r(iFullSz=mem5.s
8dbd0 7a 41 74 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65 3d zAtom, iLogsize=
8dbe0 30 3b 20 69 46 75 6c 6c 53 7a 3c 6e 42 79 74 65 0; iFullSz<nByte
8dbf0 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c 20 ; iFullSz *= 2,
8dc00 69 4c 6f 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a 20 iLogsize++){}..
8dc10 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 6d 65 /* Make sure me
8dc20 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c m5.aiFreelist[iL
8dc30 6f 67 73 69 7a 65 5d 20 63 6f 6e 74 61 69 6e 73 ogsize] contains
8dc40 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 66 72 at least one fr
8dc50 65 65 0a 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20 ee. ** block.
8dc60 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20 73 70 6c If not, then spl
8dc70 69 74 20 61 20 62 6c 6f 63 6b 20 6f 66 20 74 68 it a block of th
8dc80 65 20 6e 65 78 74 20 6c 61 72 67 65 72 20 70 6f e next larger po
8dc90 77 65 72 20 6f 66 0a 20 20 2a 2a 20 74 77 6f 20 wer of. ** two
8dca0 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61 in order to crea
8dcb0 74 65 20 61 20 6e 65 77 20 66 72 65 65 20 62 6c te a new free bl
8dcc0 6f 63 6b 20 6f 66 20 73 69 7a 65 20 69 4c 6f 67 ock of size iLog
8dcd0 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 size.. */. for
8dce0 28 69 42 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b 20 (iBin=iLogsize;
8dcf0 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b mem5.aiFreelist[
8dd00 69 42 69 6e 5d 3c 30 20 26 26 20 69 42 69 6e 3c iBin]<0 && iBin<
8dd10 3d 4c 4f 47 4d 41 58 3b 20 69 42 69 6e 2b 2b 29 =LOGMAX; iBin++)
8dd20 7b 7d 0a 20 20 69 66 28 20 69 42 69 6e 3e 4c 4f {}. if( iBin>LO
8dd30 47 4d 41 58 20 29 20 72 65 74 75 72 6e 20 30 3b GMAX ) return 0;
8dd40 0a 20 20 69 20 3d 20 6d 65 6d 73 79 73 35 55 6e . i = memsys5Un
8dd50 6c 69 6e 6b 46 69 72 73 74 28 69 42 69 6e 29 3b linkFirst(iBin);
8dd60 0a 20 20 77 68 69 6c 65 28 20 69 42 69 6e 3e 69 . while( iBin>i
8dd70 4c 6f 67 73 69 7a 65 20 29 7b 0a 20 20 20 20 69 Logsize ){. i
8dd80 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 0a 20 20 20 nt newSize;..
8dd90 20 69 42 69 6e 2d 2d 3b 0a 20 20 20 20 6e 65 77 iBin--;. new
8dda0 53 69 7a 65 20 3d 20 31 20 3c 3c 20 69 42 69 6e Size = 1 << iBin
8ddb0 3b 0a 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c ;. mem5.aCtrl
8ddc0 5b 69 2b 6e 65 77 53 69 7a 65 5d 20 3d 20 43 54 [i+newSize] = CT
8ddd0 52 4c 5f 46 52 45 45 20 7c 20 69 42 69 6e 3b 0a RL_FREE | iBin;.
8dde0 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 memsys5Link(
8ddf0 69 2b 6e 65 77 53 69 7a 65 2c 20 69 42 69 6e 29 i+newSize, iBin)
8de00 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 43 74 ;. }. mem5.aCt
8de10 72 6c 5b 69 5d 20 3d 20 69 4c 6f 67 73 69 7a 65 rl[i] = iLogsize
8de20 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 61 ;.. /* Update a
8de30 6c 6c 6f 63 61 74 6f 72 20 70 65 72 66 6f 72 6d llocator perform
8de40 61 6e 63 65 20 73 74 61 74 69 73 74 69 63 73 2e ance statistics.
8de50 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6e 41 6c 6c 6f */. mem5.nAllo
8de60 63 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 c++;. mem5.tota
8de70 6c 41 6c 6c 6f 63 20 2b 3d 20 69 46 75 6c 6c 53 lAlloc += iFullS
8de80 7a 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 z;. mem5.totalE
8de90 78 63 65 73 73 20 2b 3d 20 69 46 75 6c 6c 53 7a xcess += iFullSz
8dea0 20 2d 20 6e 42 79 74 65 3b 0a 20 20 6d 65 6d 35 - nByte;. mem5
8deb0 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2b 2b 3b .currentCount++;
8dec0 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f . mem5.currentO
8ded0 75 74 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 ut += iFullSz;.
8dee0 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 if( mem5.maxCou
8def0 6e 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 nt<mem5.currentC
8df00 6f 75 6e 74 20 29 20 6d 65 6d 35 2e 6d 61 78 43 ount ) mem5.maxC
8df10 6f 75 6e 74 20 3d 20 6d 65 6d 35 2e 63 75 72 72 ount = mem5.curr
8df20 65 6e 74 43 6f 75 6e 74 3b 0a 20 20 69 66 28 20 entCount;. if(
8df30 6d 65 6d 35 2e 6d 61 78 4f 75 74 3c 6d 65 6d 35 mem5.maxOut<mem5
8df40 2e 63 75 72 72 65 6e 74 4f 75 74 20 29 20 6d 65 .currentOut ) me
8df50 6d 35 2e 6d 61 78 4f 75 74 20 3d 20 6d 65 6d 35 m5.maxOut = mem5
8df60 2e 63 75 72 72 65 6e 74 4f 75 74 3b 0a 0a 20 20 .currentOut;..
8df70 2f 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e /* Return a poin
8df80 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 ter to the alloc
8df90 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 2a 2f 0a ated memory. */.
8dfa0 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 return (void*)
8dfb0 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d 65 &mem5.zPool[i*me
8dfc0 6d 35 2e 73 7a 41 74 6f 6d 5d 3b 0a 7d 0a 0a 2f m5.szAtom];.}../
8dfd0 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 74 *.** Free an out
8dfe0 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 standing memory
8dff0 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 allocation..*/.s
8e000 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 tatic void memsy
8e010 73 35 46 72 65 65 55 6e 73 61 66 65 28 76 6f 69 s5FreeUnsafe(voi
8e020 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 75 33 32 20 d *pOld){. u32
8e030 73 69 7a 65 2c 20 69 4c 6f 67 73 69 7a 65 3b 0a size, iLogsize;.
8e040 20 20 69 6e 74 20 69 42 6c 6f 63 6b 3b 0a 0a 20 int iBlock;..
8e050 20 2f 2a 20 53 65 74 20 69 42 6c 6f 63 6b 20 74 /* Set iBlock t
8e060 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 o the index of t
8e070 68 65 20 62 6c 6f 63 6b 20 70 6f 69 6e 74 65 64 he block pointed
8e080 20 74 6f 20 62 79 20 70 4f 6c 64 20 69 6e 20 0a to by pOld in .
8e090 20 20 2a 2a 20 74 68 65 20 61 72 72 61 79 20 6f ** the array o
8e0a0 66 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 62 79 f mem5.szAtom by
8e0b0 74 65 20 62 6c 6f 63 6b 73 20 70 6f 69 6e 74 65 te blocks pointe
8e0c0 64 20 74 6f 20 62 79 20 6d 65 6d 35 2e 7a 50 6f d to by mem5.zPo
8e0d0 6f 6c 2e 0a 20 20 2a 2f 0a 20 20 69 42 6c 6f 63 ol.. */. iBloc
8e0e0 6b 20 3d 20 28 28 75 38 20 2a 29 70 4f 6c 64 2d k = ((u8 *)pOld-
8e0f0 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35 mem5.zPool)/mem5
8e100 2e 73 7a 41 74 6f 6d 3b 0a 0a 20 20 2f 2a 20 43 .szAtom;.. /* C
8e110 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 6f heck that the po
8e120 69 6e 74 65 72 20 70 4f 6c 64 20 70 6f 69 6e 74 inter pOld point
8e130 73 20 74 6f 20 61 20 76 61 6c 69 64 2c 20 6e 6f s to a valid, no
8e140 6e 2d 66 72 65 65 20 62 6c 6f 63 6b 2e 20 2a 2f n-free block. */
8e150 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63 . assert( iBloc
8e160 6b 3e 3d 30 20 26 26 20 69 42 6c 6f 63 6b 3c 6d k>=0 && iBlock<m
8e170 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 em5.nBlock );.
8e180 61 73 73 65 72 74 28 20 28 28 75 38 20 2a 29 70 assert( ((u8 *)p
8e190 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 25 Old-mem5.zPool)%
8e1a0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3d 3d 30 20 29 mem5.szAtom==0 )
8e1b0 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d ;. assert( (mem
8e1c0 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 5.aCtrl[iBlock]
8e1d0 26 20 43 54 52 4c 5f 46 52 45 45 29 3d 3d 30 20 & CTRL_FREE)==0
8e1e0 29 3b 0a 0a 20 20 69 4c 6f 67 73 69 7a 65 20 3d );.. iLogsize =
8e1f0 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f mem5.aCtrl[iBlo
8e200 63 6b 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 ck] & CTRL_LOGSI
8e210 5a 45 3b 0a 20 20 73 69 7a 65 20 3d 20 31 3c 3c ZE;. size = 1<<
8e220 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 61 73 73 65 iLogsize;. asse
8e230 72 74 28 20 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d rt( iBlock+size-
8e240 31 3c 28 75 33 32 29 6d 65 6d 35 2e 6e 42 6c 6f 1<(u32)mem5.nBlo
8e250 63 6b 20 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 ck );.. mem5.aC
8e260 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 7c 3d 20 43 trl[iBlock] |= C
8e270 54 52 4c 5f 46 52 45 45 3b 0a 20 20 6d 65 6d 35 TRL_FREE;. mem5
8e280 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 2b 73 69 .aCtrl[iBlock+si
8e290 7a 65 2d 31 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 ze-1] |= CTRL_FR
8e2a0 45 45 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 EE;. assert( me
8e2b0 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3e m5.currentCount>
8e2c0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 0 );. assert( m
8e2d0 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e 3d em5.currentOut>=
8e2e0 28 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41 74 6f (size*mem5.szAto
8e2f0 6d 29 20 29 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 m) );. mem5.cur
8e300 72 65 6e 74 43 6f 75 6e 74 2d 2d 3b 0a 20 20 6d rentCount--;. m
8e310 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 2d em5.currentOut -
8e320 3d 20 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41 74 = size*mem5.szAt
8e330 6f 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 om;. assert( me
8e340 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e 30 20 m5.currentOut>0
8e350 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 || mem5.currentC
8e360 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 ount==0 );. ass
8e370 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e ert( mem5.curren
8e380 74 43 6f 75 6e 74 3e 30 20 7c 7c 20 6d 65 6d 35 tCount>0 || mem5
8e390 2e 63 75 72 72 65 6e 74 4f 75 74 3d 3d 30 20 29 .currentOut==0 )
8e3a0 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b ;.. mem5.aCtrl[
8e3b0 69 42 6c 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 iBlock] = CTRL_F
8e3c0 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a REE | iLogsize;.
8e3d0 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28 while( ALWAYS(
8e3e0 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d 41 58 29 iLogsize<LOGMAX)
8e3f0 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75 64 ){. int iBud
8e400 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69 42 6c dy;. if( (iBl
8e410 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29 20 26 ock>>iLogsize) &
8e420 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42 75 64 1 ){. iBud
8e430 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20 73 69 dy = iBlock - si
8e440 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ze;. }else{.
8e450 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 69 42 iBuddy = iB
8e460 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20 20 20 lock + size;.
8e470 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 }. assert( i
8e480 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20 20 20 Buddy>=0 );.
8e490 69 66 28 20 28 69 42 75 64 64 79 2b 28 31 3c 3c if( (iBuddy+(1<<
8e4a0 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d 35 2e iLogsize))>mem5.
8e4b0 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b 3b 0a nBlock ) break;.
8e4c0 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61 43 74 if( mem5.aCt
8e4d0 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43 54 52 rl[iBuddy]!=(CTR
8e4e0 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a L_FREE | iLogsiz
8e4f0 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 e) ) break;.
8e500 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 42 memsys5Unlink(iB
8e510 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65 29 3b uddy, iLogsize);
8e520 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b 2b 3b . iLogsize++;
8e530 0a 20 20 20 20 69 66 28 20 69 42 75 64 64 79 3c . if( iBuddy<
8e540 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 iBlock ){.
8e550 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 mem5.aCtrl[iBudd
8e560 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c y] = CTRL_FREE |
8e570 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20 iLogsize;.
8e580 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f mem5.aCtrl[iBlo
8e590 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 ck] = 0;. i
8e5a0 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79 3b 0a Block = iBuddy;.
8e5b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
8e5c0 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f mem5.aCtrl[iBlo
8e5d0 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 ck] = CTRL_FREE
8e5e0 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 | iLogsize;.
8e5f0 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 mem5.aCtrl[iBu
8e600 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a ddy] = 0;. }.
8e610 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b 0a 20 size *= 2;.
8e620 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b }. memsys5Link
8e630 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73 69 7a (iBlock, iLogsiz
8e640 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c e);.}../*.** All
8e650 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f 66 20 ocate nBytes of
8e660 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74 69 63 memory.*/.static
8e670 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d 61 void *memsys5Ma
8e680 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73 29 lloc(int nBytes)
8e690 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 {. sqlite3_int6
8e6a0 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 4 *p = 0;. if(
8e6b0 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20 20 20 nBytes>0 ){.
8e6c0 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a memsys5Enter();.
8e6d0 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d p = memsys5M
8e6e0 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 allocUnsafe(nByt
8e6f0 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 es);. memsys5
8e700 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20 20 72 Leave();. }. r
8e710 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 eturn (void*)p;
8e720 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d .}../*.** Free m
8e730 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 emory..**.** The
8e740 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d 65 6d outer layer mem
8e750 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 70 72 ory allocator pr
8e760 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f 75 74 events this rout
8e770 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65 69 6e ine from.** bein
8e780 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 50 g called with pP
8e790 72 69 6f 72 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 74 rior==0..*/.stat
8e7a0 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46 ic void memsys5F
8e7b0 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 ree(void *pPrior
8e7c0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 ){. assert( pPr
8e7d0 69 6f 72 21 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 ior!=0 );. mems
8e7e0 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 6d 65 ys5Enter();. me
8e7f0 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 msys5FreeUnsafe(
8e800 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73 79 pPrior);. memsy
8e810 73 35 4c 65 61 76 65 28 29 3b 20 20 0a 7d 0a 0a s5Leave(); .}..
8e820 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
8e830 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 size of an exis
8e840 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ting memory allo
8e850 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 cation..**.** Th
8e860 65 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d 65 e outer layer me
8e870 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 70 mory allocator p
8e880 72 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f 75 revents this rou
8e890 74 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65 69 tine from.** bei
8e8a0 6e 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 ng called with p
8e8b0 50 72 69 6f 72 3d 3d 30 2e 20 20 0a 2a 2a 0a 2a Prior==0. .**.*
8e8c0 2a 20 6e 42 79 74 65 73 20 69 73 20 61 6c 77 61 * nBytes is alwa
8e8d0 79 73 20 61 20 76 61 6c 75 65 20 6f 62 74 61 69 ys a value obtai
8e8e0 6e 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 ned from a prior
8e8f0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 6d 65 6d 73 call to.** mems
8e900 79 73 35 52 6f 75 6e 64 28 29 2e 20 20 48 65 6e ys5Round(). Hen
8e910 63 65 20 6e 42 79 74 65 73 20 69 73 20 61 6c 77 ce nBytes is alw
8e920 61 79 73 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 ays a non-negati
8e930 76 65 20 70 6f 77 65 72 0a 2a 2a 20 6f 66 20 74 ve power.** of t
8e940 77 6f 2e 20 20 49 66 20 6e 42 79 74 65 73 3d 3d wo. If nBytes==
8e950 30 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 0 that means tha
8e960 74 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 61 6c t an oversize al
8e970 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 28 61 6e 20 location.** (an
8e980 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 65 allocation large
8e990 72 20 74 68 61 6e 20 30 78 34 30 30 30 30 30 30 r than 0x4000000
8e9a0 30 29 20 77 61 73 20 72 65 71 75 65 73 74 65 64 0) was requested
8e9b0 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f 75 and this.** rou
8e9c0 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65 74 75 tine should retu
8e9d0 72 6e 20 30 20 77 69 74 68 6f 75 74 20 66 72 65 rn 0 without fre
8e9e0 65 69 6e 67 20 70 50 72 69 6f 72 2e 0a 2a 2f 0a eing pPrior..*/.
8e9f0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d static void *mem
8ea00 73 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69 64 sys5Realloc(void
8ea10 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 *pPrior, int nB
8ea20 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c ytes){. int nOl
8ea30 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 d;. void *p;.
8ea40 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d assert( pPrior!=
8ea50 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 0 );. assert( (
8ea60 6e 42 79 74 65 73 26 28 6e 42 79 74 65 73 2d 31 nBytes&(nBytes-1
8ea70 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ))==0 );. asser
8ea80 74 28 20 6e 42 79 74 65 73 3e 3d 30 20 29 3b 0a t( nBytes>=0 );.
8ea90 20 20 69 66 28 20 6e 42 79 74 65 73 3d 3d 30 20 if( nBytes==0
8eaa0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
8eab0 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65 . }. nOld = me
8eac0 6d 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f 72 msys5Size(pPrior
8ead0 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c );. if( nBytes<
8eae0 3d 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65 74 =nOld ){. ret
8eaf0 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a urn pPrior;. }.
8eb00 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 memsys5Enter()
8eb10 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d ;. p = memsys5M
8eb20 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 allocUnsafe(nByt
8eb30 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a es);. if( p ){.
8eb40 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 50 memcpy(p, pP
8eb50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20 rior, nOld);.
8eb60 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 memsys5FreeUnsa
8eb70 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a fe(pPrior);. }.
8eb80 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 memsys5Leave()
8eb90 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a ;. return p;.}.
8eba0 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 ./*.** Round up
8ebb0 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 a request size t
8ebc0 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 o the next valid
8ebd0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 allocation size
8ebe0 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 61 6c 6c . If.** the all
8ebf0 6f 63 61 74 69 6f 6e 20 69 73 20 74 6f 6f 20 6c ocation is too l
8ec00 61 72 67 65 20 74 6f 20 62 65 20 68 61 6e 64 6c arge to be handl
8ec10 65 64 20 62 79 20 74 68 69 73 20 61 6c 6c 6f 63 ed by this alloc
8ec20 61 74 69 6f 6e 20 73 79 73 74 65 6d 2c 0a 2a 2a ation system,.**
8ec30 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a return 0..**.**
8ec40 20 41 6c 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 All allocations
8ec50 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 must be a power
8ec60 20 6f 66 20 74 77 6f 20 61 6e 64 20 6d 75 73 74 of two and must
8ec70 20 62 65 20 65 78 70 72 65 73 73 65 64 20 62 79 be expressed by
8ec80 20 61 0a 2a 2a 20 33 32 2d 62 69 74 20 73 69 67 a.** 32-bit sig
8ec90 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 48 65 ned integer. He
8eca0 6e 63 65 20 74 68 65 20 6c 61 72 67 65 73 74 20 nce the largest
8ecb0 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 30 78 allocation is 0x
8ecc0 34 30 30 30 30 30 30 30 0a 2a 2a 20 6f 72 20 31 40000000.** or 1
8ecd0 30 37 33 37 34 31 38 32 34 20 62 79 74 65 73 2e 073741824 bytes.
8ece0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
8ecf0 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 69 6e emsys5Roundup(in
8ed00 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 75 6c t n){. int iFul
8ed10 6c 53 7a 3b 0a 20 20 69 66 28 20 6e 20 3e 20 30 lSz;. if( n > 0
8ed20 78 34 30 30 30 30 30 30 30 20 29 20 72 65 74 75 x40000000 ) retu
8ed30 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69 46 75 6c rn 0;. for(iFul
8ed40 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b lSz=mem5.szAtom;
8ed50 20 69 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75 6c iFullSz<n; iFul
8ed60 6c 53 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65 74 lSz *= 2);. ret
8ed70 75 72 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a urn iFullSz;.}..
8ed80 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
8ed90 20 63 65 69 6c 69 6e 67 20 6f 66 20 74 68 65 20 ceiling of the
8eda0 6c 6f 67 61 72 69 74 68 6d 20 62 61 73 65 20 32 logarithm base 2
8edb0 20 6f 66 20 69 56 61 6c 75 65 2e 0a 2a 2a 0a 2a of iValue..**.*
8edc0 2a 20 45 78 61 6d 70 6c 65 73 3a 20 20 20 6d 65 * Examples: me
8edd0 6d 73 79 73 35 4c 6f 67 28 31 29 20 2d 3e 20 30 msys5Log(1) -> 0
8ede0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
8edf0 6d 65 6d 73 79 73 35 4c 6f 67 28 32 29 20 2d 3e memsys5Log(2) ->
8ee00 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 1.**
8ee10 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 34 29 20 memsys5Log(4)
8ee20 2d 3e 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 20 -> 2.**
8ee30 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 35 memsys5Log(5
8ee40 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 20 20 20 ) -> 3.**
8ee50 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 memsys5Log
8ee60 28 38 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 20 (8) -> 3.**
8ee70 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c memsys5L
8ee80 6f 67 28 39 29 20 2d 3e 20 34 0a 2a 2f 0a 73 74 og(9) -> 4.*/.st
8ee90 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 atic int memsys5
8eea0 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 29 7b Log(int iValue){
8eeb0 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 20 66 . int iLog;. f
8eec0 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c 3c 69 or(iLog=0; (1<<i
8eed0 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69 4c 6f Log)<iValue; iLo
8eee0 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 g++);. return i
8eef0 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e Log;.}../*.** In
8ef00 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d itialize the mem
8ef10 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 0a 2a ory allocator..*
8ef20 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
8ef30 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 e is not threads
8ef40 61 66 65 2e 20 20 54 68 65 20 63 61 6c 6c 65 72 afe. The caller
8ef50 20 6d 75 73 74 20 62 65 20 68 6f 6c 64 69 6e 67 must be holding
8ef60 20 61 20 6d 75 74 65 78 0a 2a 2a 20 74 6f 20 70 a mutex.** to p
8ef70 72 65 76 65 6e 74 20 6d 75 6c 74 69 70 6c 65 20 revent multiple
8ef80 74 68 72 65 61 64 73 20 66 72 6f 6d 20 65 6e 74 threads from ent
8ef90 65 72 69 6e 67 20 61 74 20 74 68 65 20 73 61 6d ering at the sam
8efa0 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 e time..*/.stati
8efb0 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e 69 c int memsys5Ini
8efc0 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 t(void *NotUsed)
8efd0 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 {. int ii;
8efe0 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
8eff0 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 ounter */. int
8f000 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 2f nByte; /
8f010 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
8f020 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 s of memory avai
8f030 6c 61 62 6c 65 20 74 6f 20 74 68 69 73 20 61 6c lable to this al
8f040 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 locator */. u8
8f050 2a 7a 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 *zByte;
8f060 2f 2a 20 4d 65 6d 6f 72 79 20 75 73 61 62 6c 65 /* Memory usable
8f070 20 62 79 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 by this allocat
8f080 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 69 6e or */. int nMin
8f090 4c 6f 67 3b 20 20 20 20 20 20 20 2f 2a 20 4c 6f Log; /* Lo
8f0a0 67 20 62 61 73 65 20 32 20 6f 66 20 6d 69 6e 69 g base 2 of mini
8f0b0 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 mum allocation s
8f0c0 69 7a 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a ize in bytes */.
8f0d0 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 int iOffset;
8f0e0 20 20 20 20 20 2f 2a 20 41 6e 20 6f 66 66 73 65 /* An offse
8f0f0 74 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 43 74 72 t into mem5.aCtr
8f100 6c 5b 5d 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 44 l[] */.. UNUSED
8f110 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
8f120 65 64 29 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 74 ed);.. /* For t
8f130 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 he purposes of t
8f140 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 64 69 73 his routine, dis
8f150 61 62 6c 65 20 74 68 65 20 6d 75 74 65 78 20 2a able the mutex *
8f160 2f 0a 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d /. mem5.mutex =
8f170 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69 0;.. /* The si
8f180 7a 65 20 6f 66 20 61 20 4d 65 6d 35 4c 69 6e 6b ze of a Mem5Link
8f190 20 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65 20 object must be
8f1a0 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 a power of two.
8f1b0 20 56 65 72 69 66 79 20 74 68 61 74 0a 20 20 2a Verify that. *
8f1c0 2a 20 74 68 69 73 20 69 73 20 63 61 73 65 2e 0a * this is case..
8f1d0 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 */. assert( (
8f1e0 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29 sizeof(Mem5Link)
8f1f0 26 28 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e &(sizeof(Mem5Lin
8f200 6b 29 2d 31 29 29 3d 3d 30 20 29 3b 0a 0a 20 20 k)-1))==0 );..
8f210 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33 47 nByte = sqlite3G
8f220 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 lobalConfig.nHea
8f230 70 3b 0a 20 20 7a 42 79 74 65 20 3d 20 28 75 38 p;. zByte = (u8
8f240 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 *)sqlite3GlobalC
8f250 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20 20 61 onfig.pHeap;. a
8f260 73 73 65 72 74 28 20 7a 42 79 74 65 21 3d 30 20 ssert( zByte!=0
8f270 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 ); /* sqlite3_c
8f280 6f 6e 66 69 67 28 29 20 64 6f 65 73 20 6e 6f 74 onfig() does not
8f290 20 61 6c 6c 6f 77 20 6f 74 68 65 72 77 69 73 65 allow otherwise
8f2a0 20 2a 2f 0a 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d */.. nMinLog =
8f2b0 20 6d 65 6d 73 79 73 35 4c 6f 67 28 73 71 6c 69 memsys5Log(sqli
8f2c0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
8f2d0 6d 6e 52 65 71 29 3b 0a 20 20 6d 65 6d 35 2e 73 mnReq);. mem5.s
8f2e0 7a 41 74 6f 6d 20 3d 20 28 31 3c 3c 6e 4d 69 6e zAtom = (1<<nMin
8f2f0 4c 6f 67 29 3b 0a 20 20 77 68 69 6c 65 28 20 28 Log);. while( (
8f300 69 6e 74 29 73 69 7a 65 6f 66 28 4d 65 6d 35 4c int)sizeof(Mem5L
8f310 69 6e 6b 29 3e 6d 65 6d 35 2e 73 7a 41 74 6f 6d ink)>mem5.szAtom
8f320 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 73 7a 41 ){. mem5.szA
8f330 74 6f 6d 20 3d 20 6d 65 6d 35 2e 73 7a 41 74 6f tom = mem5.szAto
8f340 6d 20 3c 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20 6d m << 1;. }.. m
8f350 65 6d 35 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 em5.nBlock = (nB
8f360 79 74 65 20 2f 20 28 6d 65 6d 35 2e 73 7a 41 74 yte / (mem5.szAt
8f370 6f 6d 2b 73 69 7a 65 6f 66 28 75 38 29 29 29 3b om+sizeof(u8)));
8f380 0a 20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 3d 20 . mem5.zPool =
8f390 7a 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 61 43 zByte;. mem5.aC
8f3a0 74 72 6c 20 3d 20 28 75 38 20 2a 29 26 6d 65 6d trl = (u8 *)&mem
8f3b0 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 6c 5.zPool[mem5.nBl
8f3c0 6f 63 6b 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5d ock*mem5.szAtom]
8f3d0 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 ;.. for(ii=0; i
8f3e0 69 3c 3d 4c 4f 47 4d 41 58 3b 20 69 69 2b 2b 29 i<=LOGMAX; ii++)
8f3f0 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 65 {. mem5.aiFre
8f400 65 6c 69 73 74 5b 69 69 5d 20 3d 20 2d 31 3b 0a elist[ii] = -1;.
8f410 20 20 7d 0a 0a 20 20 69 4f 66 66 73 65 74 20 3d }.. iOffset =
8f420 20 30 3b 0a 20 20 66 6f 72 28 69 69 3d 4c 4f 47 0;. for(ii=LOG
8f430 4d 41 58 3b 20 69 69 3e 3d 30 3b 20 69 69 2d 2d MAX; ii>=0; ii--
8f440 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f ){. int nAllo
8f450 63 20 3d 20 28 31 3c 3c 69 69 29 3b 0a 20 20 20 c = (1<<ii);.
8f460 20 69 66 28 20 28 69 4f 66 66 73 65 74 2b 6e 41 if( (iOffset+nA
8f470 6c 6c 6f 63 29 3c 3d 6d 65 6d 35 2e 6e 42 6c 6f lloc)<=mem5.nBlo
8f480 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 35 ck ){. mem5
8f490 2e 61 43 74 72 6c 5b 69 4f 66 66 73 65 74 5d 20 .aCtrl[iOffset]
8f4a0 3d 20 69 69 20 7c 20 43 54 52 4c 5f 46 52 45 45 = ii | CTRL_FREE
8f4b0 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c ;. memsys5L
8f4c0 69 6e 6b 28 69 4f 66 66 73 65 74 2c 20 69 69 29 ink(iOffset, ii)
8f4d0 3b 0a 20 20 20 20 20 20 69 4f 66 66 73 65 74 20 ;. iOffset
8f4e0 2b 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20 7d += nAlloc;. }
8f4f0 0a 20 20 20 20 61 73 73 65 72 74 28 28 69 4f 66 . assert((iOf
8f500 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 6d fset+nAlloc)>mem
8f510 35 2e 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 5.nBlock);. }..
8f520 20 20 2f 2a 20 49 66 20 61 20 6d 75 74 65 78 20 /* If a mutex
8f530 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 is required for
8f540 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 69 6f 6e normal operation
8f550 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20 2a , allocate one *
8f560 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 /. if( sqlite3G
8f570 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d lobalConfig.bMem
8f580 73 74 61 74 3d 3d 30 20 29 7b 0a 20 20 20 20 6d stat==0 ){. m
8f590 65 6d 35 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 em5.mutex = sqli
8f5a0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
8f5b0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
8f5c0 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 0a 20 20 72 C_MEM);. }.. r
8f5d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
8f5e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 .}../*.** Deinit
8f5f0 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 ialize this modu
8f600 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f le..*/.static vo
8f610 69 64 20 6d 65 6d 73 79 73 35 53 68 75 74 64 6f id memsys5Shutdo
8f620 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 wn(void *NotUsed
8f630 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
8f640 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
8f650 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 30 mem5.mutex = 0
8f660 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 23 ;. return;.}..#
8f670 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
8f680 54 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 T./*.** Open the
8f690 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20 file indicated
8f6a0 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20 and write a log
8f6b0 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d of all unfreed m
8f6c0 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 emory .** alloca
8f6d0 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 tions into that
8f6e0 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 log..*/.SQLITE_P
8f6f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
8f700 74 65 33 4d 65 6d 73 79 73 35 44 75 6d 70 28 63 te3Memsys5Dump(c
8f710 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
8f720 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 2a 6f name){. FILE *o
8f730 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 ut;. int i, j,
8f740 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 n;. int nMinLog
8f750 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 ;.. if( zFilena
8f760 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 me==0 || zFilena
8f770 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 me[0]==0 ){.
8f780 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 out = stdout;.
8f790 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d }else{. out =
8f7a0 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 fopen(zFilename
8f7b0 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28 20 , "w");. if(
8f7c0 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 out==0 ){.
8f7d0 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 fprintf(stderr,
8f7e0 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 "** Unable to ou
8f7f0 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 tput memory debu
8f800 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 g output log: %s
8f810 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 **\n",.
8f820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 zF
8f830 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 ilename);.
8f840 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
8f850 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 }. memsys5Enter
8f860 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d 20 ();. nMinLog =
8f870 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65 6d 35 2e memsys5Log(mem5.
8f880 73 7a 41 74 6f 6d 29 3b 0a 20 20 66 6f 72 28 69 szAtom);. for(i
8f890 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20 26 26 =0; i<=LOGMAX &&
8f8a0 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b 20 69 i+nMinLog<32; i
8f8b0 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e 3d 30 ++){. for(n=0
8f8c0 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65 65 6c , j=mem5.aiFreel
8f8d0 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 6a 20 ist[i]; j>=0; j
8f8e0 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d 3e 6e = MEM5LINK(j)->n
8f8f0 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 ext, n++){}.
8f900 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 66 72 fprintf(out, "fr
8f910 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f 66 20 eelist items of
8f920 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22 2c 20 size %d: %d\n",
8f930 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3c 3c 20 69 mem5.szAtom << i
8f940 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72 69 , n);. }. fpri
8f950 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6e ntf(out, "mem5.n
8f960 41 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25 6c Alloc = %l
8f970 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c 6c lu\n", mem5.nAll
8f980 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f oc);. fprintf(o
8f990 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 41 ut, "mem5.totalA
8f9a0 6c 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 lloc = %llu\n"
8f9b0 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f , mem5.totalAllo
8f9c0 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 c);. fprintf(ou
8f9d0 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 t, "mem5.totalEx
8f9e0 63 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c cess = %llu\n",
8f9f0 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 mem5.totalExces
8fa00 73 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 s);. fprintf(ou
8fa10 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 t, "mem5.current
8fa20 4f 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d Out = %u\n", m
8fa30 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29 3b em5.currentOut);
8fa40 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 . fprintf(out,
8fa50 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 "mem5.currentCou
8fa60 6e 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 nt = %u\n", mem5
8fa70 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b 0a .currentCount);.
8fa80 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
8fa90 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20 20 mem5.maxOut
8faa0 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e = %u\n", mem5.
8fab0 6d 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69 6e maxOut);. fprin
8fac0 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 tf(out, "mem5.ma
8fad0 78 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75 5c xCount = %u\
8fae0 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e n", mem5.maxCoun
8faf0 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 t);. fprintf(ou
8fb00 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71 75 t, "mem5.maxRequ
8fb10 65 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d est = %u\n", m
8fb20 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29 3b em5.maxRequest);
8fb30 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 . memsys5Leave(
8fb40 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 74 );. if( out==st
8fb50 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c 75 dout ){. fflu
8fb60 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d 65 sh(stdout);. }e
8fb70 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 28 lse{. fclose(
8fb80 6f 75 74 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 out);. }.}.#end
8fb90 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 if../*.** This r
8fba0 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e outine is the on
8fbb0 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 ly routine in th
8fbc0 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 is file with ext
8fbd0 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 ernal .** linkag
8fbe0 65 2e 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 e. It returns a
8fbf0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 pointer to a sta
8fc00 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f tic sqlite3_mem_
8fc10 6d 65 74 68 6f 64 73 0a 2a 2a 20 73 74 72 75 63 methods.** struc
8fc20 74 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 t populated with
8fc30 20 74 68 65 20 6d 65 6d 73 79 73 35 20 6d 65 74 the memsys5 met
8fc40 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f hods..*/.SQLITE_
8fc50 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 PRIVATE const sq
8fc60 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
8fc70 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 s *sqlite3MemGet
8fc80 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 7b 0a 20 Memsys5(void){.
8fc90 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 static const sq
8fca0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
8fcb0 73 20 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64 73 s memsys5Methods
8fcc0 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73 = {. memsys
8fcd0 35 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 5Malloc,. me
8fce0 6d 73 79 73 35 46 72 65 65 2c 0a 20 20 20 20 20 msys5Free,.
8fcf0 6d 65 6d 73 79 73 35 52 65 61 6c 6c 6f 63 2c 0a memsys5Realloc,.
8fd00 20 20 20 20 20 6d 65 6d 73 79 73 35 53 69 7a 65 memsys5Size
8fd10 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 6f ,. memsys5Ro
8fd20 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73 undup,. mems
8fd30 79 73 35 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65 ys5Init,. me
8fd40 6d 73 79 73 35 53 68 75 74 64 6f 77 6e 2c 0a 20 msys5Shutdown,.
8fd50 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74 0. };. ret
8fd60 75 72 6e 20 26 6d 65 6d 73 79 73 35 4d 65 74 68 urn &memsys5Meth
8fd70 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f ods;.}..#endif /
8fd80 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f * SQLITE_ENABLE_
8fd90 4d 45 4d 53 59 53 35 20 2a 2f 0a 0a 2f 2a 2a 2a MEMSYS5 */../***
8fda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
8fdb0 6f 66 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a of mem5.c ******
8fdc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
8fdf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
8fe00 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 63 20 2a n file mutex.c *
8fe10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fe20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fe30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
8fe40 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 34 * 2007 August 14
8fe50 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
8fe60 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
8fe70 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
8fe80 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
8fe90 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
8fea0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
8feb0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
8fec0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
8fed0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
8fee0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
8fef0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
8ff00 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
8ff10 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
8ff20 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
8ff30 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
8ff40 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
8ff50 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
8ff60 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
8ff70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ff80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ff90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ffa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
8ffb0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
8ffc0 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 tains the C func
8ffd0 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 tions that imple
8ffe0 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a ment mutexes..**
8fff0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
90000 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 ntains code that
90010 20 69 73 20 63 6f 6d 6d 6f 6e 20 61 63 72 6f 73 is common acros
90020 73 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c s all mutex impl
90030 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 0a 2a 2a ementations...**
90040 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e 63 .** $Id: mutex.c
90050 2c 76 20 31 2e 33 31 20 32 30 30 39 2f 30 37 2f ,v 1.31 2009/07/
90060 31 36 20 31 38 3a 32 31 3a 31 38 20 64 72 68 20 16 18:21:18 drh
90070 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 Exp $.*/..#ifnde
90080 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f f SQLITE_MUTEX_O
90090 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 MIT./*.** Initia
900a0 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 lize the mutex s
900b0 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ystem..*/.SQLITE
900c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
900d0 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f ite3MutexInit(vo
900e0 69 64 29 7b 20 0a 20 20 69 6e 74 20 72 63 20 3d id){ . int rc =
900f0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
90100 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
90110 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 onfig.bCoreMutex
90120 20 29 7b 0a 20 20 20 20 69 66 28 20 21 73 71 6c ){. if( !sql
90130 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
90140 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c .mutex.xMutexAll
90150 6f 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 oc ){. /* I
90160 66 20 74 68 65 20 78 4d 75 74 65 78 41 6c 6c 6f f the xMutexAllo
90170 63 20 6d 65 74 68 6f 64 20 68 61 73 20 6e 6f 74 c method has not
90180 20 62 65 65 6e 20 73 65 74 2c 20 74 68 65 6e 20 been set, then
90190 74 68 65 20 75 73 65 72 20 64 69 64 20 6e 6f 74 the user did not
901a0 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 61 6c . ** instal
901b0 6c 20 61 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d l a mutex implem
901c0 65 6e 74 61 74 69 6f 6e 20 76 69 61 20 73 71 6c entation via sql
901d0 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 70 72 ite3_config() pr
901e0 69 6f 72 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a ior to . **
901f0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
90200 69 7a 65 28 29 20 62 65 69 6e 67 20 63 61 6c 6c ize() being call
90210 65 64 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 ed. This block c
90220 6f 70 69 65 73 20 70 6f 69 6e 74 65 72 73 20 74 opies pointers t
90230 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 o. ** the d
90240 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 efault implement
90250 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 73 ation into the s
90260 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
90270 69 67 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 ig structure..
90280 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c */. sql
90290 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
902a0 64 73 20 2a 70 46 72 6f 6d 20 3d 20 73 71 6c 69 ds *pFrom = sqli
902b0 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 te3DefaultMutex(
902c0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
902d0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a _mutex_methods *
902e0 70 54 6f 20 3d 20 26 73 71 6c 69 74 65 33 47 6c pTo = &sqlite3Gl
902f0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
90300 3b 0a 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ;.. memcpy(
90310 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 6f 66 66 73 pTo, pFrom, offs
90320 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 75 74 etof(sqlite3_mut
90330 65 78 5f 6d 65 74 68 6f 64 73 2c 20 78 4d 75 74 ex_methods, xMut
90340 65 78 41 6c 6c 6f 63 29 29 3b 0a 20 20 20 20 20 exAlloc));.
90350 20 6d 65 6d 63 70 79 28 26 70 54 6f 2d 3e 78 4d memcpy(&pTo->xM
90360 75 74 65 78 46 72 65 65 2c 20 26 70 46 72 6f 6d utexFree, &pFrom
90370 2d 3e 78 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 ->xMutexFree,.
90380 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f sizeo
90390 66 28 2a 70 54 6f 29 20 2d 20 6f 66 66 73 65 74 f(*pTo) - offset
903a0 6f 66 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 of(sqlite3_mutex
903b0 5f 6d 65 74 68 6f 64 73 2c 20 78 4d 75 74 65 78 _methods, xMutex
903c0 46 72 65 65 29 29 3b 0a 20 20 20 20 20 20 70 54 Free));. pT
903d0 6f 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 3d o->xMutexAlloc =
903e0 20 70 46 72 6f 6d 2d 3e 78 4d 75 74 65 78 41 6c pFrom->xMutexAl
903f0 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 loc;. }. r
90400 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 c = sqlite3Globa
90410 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d lConfig.mutex.xM
90420 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 20 7d 0a utexInit();. }.
90430 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
90440 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 ./*.** Shutdown
90450 74 68 65 20 6d 75 74 65 78 20 73 79 73 74 65 6d the mutex system
90460 2e 20 54 68 69 73 20 63 61 6c 6c 20 66 72 65 65 . This call free
90470 73 20 72 65 73 6f 75 72 63 65 73 20 61 6c 6c 6f s resources allo
90480 63 61 74 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 cated by.** sqli
90490 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 2e 0a te3MutexInit()..
904a0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
904b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 E int sqlite3Mut
904c0 65 78 45 6e 64 28 76 6f 69 64 29 7b 0a 20 20 69 exEnd(void){. i
904d0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
904e0 4b 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 K;. if( sqlite3
904f0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
90500 65 78 2e 78 4d 75 74 65 78 45 6e 64 20 29 7b 0a ex.xMutexEnd ){.
90510 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
90520 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
90530 65 78 2e 78 4d 75 74 65 78 45 6e 64 28 29 3b 0a ex.xMutexEnd();.
90540 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
90550 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72 69 65 .}../*.** Retrie
90560 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ve a pointer to
90570 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 6f a static mutex o
90580 72 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 r allocate a new
90590 20 64 79 6e 61 6d 69 63 20 6f 6e 65 2e 0a 2a 2f dynamic one..*/
905a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
905b0 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 te3_mutex *sqlit
905c0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 69 e3_mutex_alloc(i
905d0 6e 74 20 69 64 29 7b 0a 23 69 66 6e 64 65 66 20 nt id){.#ifndef
905e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
905f0 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 INIT. if( sqlit
90600 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
90610 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 ) return 0;.#end
90620 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 if. return sqli
90630 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
90640 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f mutex.xMutexAllo
90650 63 28 69 64 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 c(id);.}..SQLITE
90660 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
90670 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 4d _mutex *sqlite3M
90680 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 utexAlloc(int id
90690 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 ){. if( !sqlite
906a0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 3GlobalConfig.bC
906b0 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 oreMutex ){.
906c0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
906d0 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c return sqlite3Gl
906e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
906f0 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29 .xMutexAlloc(id)
90700 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
90710 61 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e a dynamic mutex.
90720 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
90730 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 oid sqlite3_mute
90740 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f 6d x_free(sqlite3_m
90750 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 utex *p){. if(
90760 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 p ){. sqlite3
90770 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
90780 65 78 2e 78 4d 75 74 65 78 46 72 65 65 28 70 29 ex.xMutexFree(p)
90790 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f ;. }.}../*.** O
907a0 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20 btain the mutex
907b0 70 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 p. If some other
907c0 20 74 68 72 65 61 64 20 61 6c 72 65 61 64 79 20 thread already
907d0 68 61 73 20 74 68 65 20 6d 75 74 65 78 2c 20 62 has the mutex, b
907e0 6c 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20 69 74 lock.** until it
907f0 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 can be obtained
90800 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
90810 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 void sqlite3_mut
90820 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 ex_enter(sqlite3
90830 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 _mutex *p){. if
90840 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ( p ){. sqlit
90850 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
90860 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 74 65 72 utex.xMutexEnter
90870 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
90880 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74 * Obtain the mut
90890 65 78 20 70 2e 20 49 66 20 73 75 63 63 65 73 73 ex p. If success
908a0 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 ful, return SQLI
908b0 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 TE_OK. Otherwise
908c0 2c 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 , if another.**
908d0 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65 thread holds the
908e0 20 6d 75 74 65 78 20 61 6e 64 20 69 74 20 63 61 mutex and it ca
908f0 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 nnot be obtained
90900 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
90910 42 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f BUSY..*/.SQLITE_
90920 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
90930 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65 mutex_try(sqlite
90940 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 3_mutex *p){. i
90950 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
90960 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 K;. if( p ){.
90970 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
90980 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
90990 65 78 2e 78 4d 75 74 65 78 54 72 79 28 70 29 3b ex.xMutexTry(p);
909a0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
909b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 ;.}../*.** The s
909c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
909d0 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 ve() routine exi
909e0 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 ts a mutex that
909f0 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a was previously.*
90a00 2a 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 * entered by the
90a10 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 same thread. T
90a20 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 he behavior is u
90a30 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 ndefined if the
90a40 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e 6f 74 mutex .** is not
90a50 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 currently enter
90a60 65 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f ed. If a NULL po
90a70 69 6e 74 65 72 20 69 73 20 70 61 73 73 65 64 20 inter is passed
90a80 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 0a 2a as an argument.*
90a90 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * this function
90aa0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 is a no-op..*/.S
90ab0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
90ac0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
90ad0 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ve(sqlite3_mutex
90ae0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b *p){. if( p ){
90af0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
90b00 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
90b10 4d 75 74 65 78 4c 65 61 76 65 28 70 29 3b 0a 20 MutexLeave(p);.
90b20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 }.}..#ifndef ND
90b30 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 EBUG./*.** The s
90b40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
90b50 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f d() and sqlite3_
90b60 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 mutex_notheld()
90b70 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 routine are.** i
90b80 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 ntended for use
90b90 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 inside assert()
90ba0 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 statements..*/.S
90bb0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
90bc0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
90bd0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
90be0 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d p){. return p==
90bf0 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 0 || sqlite3Glob
90c00 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
90c10 4d 75 74 65 78 48 65 6c 64 28 70 29 3b 0a 7d 0a MutexHeld(p);.}.
90c20 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
90c30 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
90c40 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 held(sqlite3_mut
90c50 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e ex *p){. return
90c60 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 p==0 || sqlite3
90c70 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
90c80 65 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 ex.xMutexNotheld
90c90 28 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 (p);.}.#endif..#
90ca0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
90cb0 4f 4d 49 54 5f 4d 55 54 45 58 20 2a 2f 0a 0a 2f OMIT_MUTEX */../
90cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
90cd0 6e 64 20 6f 66 20 6d 75 74 65 78 2e 63 20 2a 2a nd of mutex.c **
90ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
90d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
90d20 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f egin file mutex_
90d30 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a noop.c *********
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 2f 0a 2f *************/./
90d60 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 *.** 2008 Octobe
90d70 72 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 07.**.** The a
90d80 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
90d90 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
90da0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
90db0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
90dc0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
90dd0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
90de0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
90df0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
90e00 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
90e10 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
90e20 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
90e30 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
90e40 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
90e50 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
90e60 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
90e70 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
90e80 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
90e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90ed0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
90ee0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 contains the C
90ef0 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 functions that i
90f00 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 mplement mutexes
90f10 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 ..**.** This imp
90f20 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74 lementation in t
90f30 68 69 73 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f his file does no
90f40 74 20 70 72 6f 76 69 64 65 20 61 6e 79 20 6d 75 t provide any mu
90f50 74 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73 69 6f tual.** exclusio
90f60 6e 20 61 6e 64 20 69 73 20 74 68 75 73 20 73 75 n and is thus su
90f70 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 6f itable for use o
90f80 6e 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 74 69 nly in applicati
90f90 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73 65 20 ons.** that use
90fa0 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69 6e 67 SQLite in a sing
90fb0 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 le thread. The
90fc0 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64 routines defined
90fd0 0a 2a 2a 20 68 65 72 65 20 61 72 65 20 70 6c 61 .** here are pla
90fe0 63 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41 70 70 ce-holders. App
90ff0 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 73 75 lications can su
91000 62 73 74 69 74 75 74 65 20 77 6f 72 6b 69 6e 67 bstitute working
91010 0a 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74 69 6e .** mutex routin
91020 65 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 es at start-time
91030 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a 2a 2a using the.**.**
91040 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e sqlite3_con
91050 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 fig(SQLITE_CONFI
91060 47 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a 2a 0a G_MUTEX,...).**.
91070 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a ** interface..**
91080 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64 20 .** If compiled
91090 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55 with SQLITE_DEBU
910a0 47 2c 20 74 68 65 6e 20 61 64 64 69 74 69 6f 6e G, then addition
910b0 61 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e 73 65 al logic is inse
910c0 72 74 65 64 0a 2a 2a 20 74 68 61 74 20 64 6f 65 rted.** that doe
910d0 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 s error checking
910e0 20 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f 20 6d on mutexes to m
910f0 61 6b 65 20 73 75 72 65 20 74 68 65 79 20 61 72 ake sure they ar
91100 65 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c 6c 65 e being.** calle
91110 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a d correctly..**.
91120 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 6e 6f ** $Id: mutex_no
91130 6f 70 2e 63 2c 76 20 31 2e 33 20 32 30 30 38 2f op.c,v 1.3 2008/
91140 31 32 2f 30 35 20 31 37 3a 31 37 3a 30 38 20 64 12/05 17:17:08 d
91150 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69 rh Exp $.*/...#i
91160 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
91170 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 _MUTEX_NOOP) &&
91180 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
91190 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 74 75 DEBUG)./*.** Stu
911a0 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61 b routines for a
911b0 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73 ll mutex methods
911c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
911d0 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 6e 6f tines provide no
911e0 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f mutual exclusio
911f0 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65 63 6b n or error check
91200 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ing..*/.static i
91210 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64 nt noopMutexHeld
91220 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
91230 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a p){ return 1; }.
91240 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d static int noopM
91250 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 utexNotheld(sqli
91260 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 te3_mutex *p){ r
91270 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69 eturn 1; }.stati
91280 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 49 c int noopMutexI
91290 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 nit(void){ retur
912a0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 n SQLITE_OK; }.s
912b0 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 tatic int noopMu
912c0 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 texEnd(void){ re
912d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
912e0 7d 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 }.static sqlite3
912f0 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 65 _mutex *noopMute
91300 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 20 xAlloc(int id){
91310 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f return (sqlite3_
91320 6d 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74 61 74 mutex*)8; }.stat
91330 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 ic void noopMute
91340 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 xFree(sqlite3_mu
91350 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b tex *p){ return;
91360 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e }.static void n
91370 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 73 71 oopMutexEnter(sq
91380 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
91390 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 return; }.stati
913a0 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 54 c int noopMutexT
913b0 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ry(sqlite3_mutex
913c0 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 53 51 4c *p){ return SQL
913d0 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 ITE_OK; }.static
913e0 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 4c void noopMutexL
913f0 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 eave(sqlite3_mut
91400 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 ex *p){ return;
91410 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 }..SQLITE_PRIVAT
91420 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f E sqlite3_mutex_
91430 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
91440 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 DefaultMutex(voi
91450 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c d){. static sql
91460 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
91470 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 ds sMutex = {.
91480 20 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 2c noopMutexInit,
91490 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e . noopMutexEn
914a0 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 d,. noopMutex
914b0 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f 70 4d Alloc,. noopM
914c0 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6e 6f utexFree,. no
914d0 6f 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 opMutexEnter,.
914e0 20 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 2c 0a noopMutexTry,.
914f0 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 noopMutexLea
91500 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 ve,.. noopMut
91510 65 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f 6f 70 exHeld,. noop
91520 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d MutexNotheld. }
91530 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 ;.. return &sMu
91540 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a tex;.}.#endif /*
91550 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
91560 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21 MUTEX_NOOP) && !
91570 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
91580 45 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20 64 65 EBUG) */..#if de
91590 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 fined(SQLITE_MUT
915a0 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66 69 EX_NOOP) && defi
915b0 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
915c0 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 )./*.** In this
915d0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 implementation,
915e0 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 69 error checking i
915f0 73 20 70 72 6f 76 69 64 65 64 20 66 6f 72 20 74 s provided for t
91600 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 esting.** and de
91610 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73 bugging purposes
91620 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20 73 . The mutexes s
91630 74 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 6f 76 till do not prov
91640 69 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 75 61 ide any.** mutua
91650 6c 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a 2f 0a l exclusion..*/.
91660 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 ./*.** The mutex
91670 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 75 63 object.*/.struc
91680 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 t sqlite3_mutex
91690 7b 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20 {. int id;
916a0 2f 2a 20 54 68 65 20 6d 75 74 65 78 20 74 79 70 /* The mutex typ
916b0 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 e */. int cnt;
916c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
916d0 65 6e 74 72 69 65 73 20 77 69 74 68 6f 75 74 20 entries without
916e0 61 20 6d 61 74 63 68 69 6e 67 20 6c 65 61 76 65 a matching leave
916f0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 */.};../*.** Th
91700 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
91710 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 held() and sqlit
91720 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
91730 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a () routine are.*
91740 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 * intended for u
91750 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 se inside assert
91760 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a () statements..*
91770 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 /.static int deb
91780 75 67 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 ugMutexHeld(sqli
91790 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
917a0 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 return p==0 ||
917b0 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74 p->cnt>0;.}.stat
917c0 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 ic int debugMute
917d0 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 xNotheld(sqlite3
917e0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 _mutex *p){. re
917f0 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e turn p==0 || p->
91800 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a cnt==0;.}../*.**
91810 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 Initialize and
91820 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 deinitialize the
91830 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d mutex subsystem
91840 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
91850 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 28 76 debugMutexInit(v
91860 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c oid){ return SQL
91870 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 ITE_OK; }.static
91880 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 45 int debugMutexE
91890 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e nd(void){ return
918a0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f SQLITE_OK; }../
918b0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
918c0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 _mutex_alloc() r
918d0 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 outine allocates
918e0 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 a new.** mutex
918f0 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f and returns a po
91900 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 inter to it. If
91910 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c it returns NULL
91920 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 .** that means t
91930 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c hat a mutex coul
91940 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 d not be allocat
91950 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 73 ed. .*/.static s
91960 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 64 65 qlite3_mutex *de
91970 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e bugMutexAlloc(in
91980 74 20 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 t id){. static
91990 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 61 53 sqlite3_mutex aS
919a0 74 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71 6c 69 tatic[6];. sqli
919b0 74 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65 77 20 te3_mutex *pNew
919c0 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 69 = 0;. switch( i
919d0 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 d ){. case SQ
919e0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a LITE_MUTEX_FAST:
919f0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
91a00 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
91a10 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d : {. pNew =
91a20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73 sqlite3Malloc(s
91a30 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 izeof(*pNew));.
91a40 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b if( pNew ){
91a50 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 . pNew->i
91a60 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 20 20 d = id;.
91a70 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 pNew->cnt = 0;.
91a80 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
91a90 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
91aa0 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 fault: {. a
91ab0 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d 20 30 ssert( id-2 >= 0
91ac0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
91ad0 28 20 69 64 2d 32 20 3c 20 28 69 6e 74 29 28 73 ( id-2 < (int)(s
91ae0 69 7a 65 6f 66 28 61 53 74 61 74 69 63 29 2f 73 izeof(aStatic)/s
91af0 69 7a 65 6f 66 28 61 53 74 61 74 69 63 5b 30 5d izeof(aStatic[0]
91b00 29 29 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 )) );. pNew
91b10 20 3d 20 26 61 53 74 61 74 69 63 5b 69 64 2d 32 = &aStatic[id-2
91b20 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 ];. pNew->i
91b30 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 62 72 d = id;. br
91b40 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
91b50 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
91b60 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
91b70 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 ine deallocates
91b80 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c a previously all
91b90 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2f ocated mutex..*/
91ba0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 .static void deb
91bb0 75 67 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 ugMutexFree(sqli
91bc0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
91bd0 20 61 73 73 65 72 74 28 20 70 2d 3e 63 6e 74 3d assert( p->cnt=
91be0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
91bf0 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
91c00 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 TEX_FAST || p->i
91c10 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
91c20 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 73 RECURSIVE );. s
91c30 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
91c40 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
91c50 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
91c60 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d () and sqlite3_m
91c70 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 utex_try() routi
91c80 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 nes attempt.** t
91c90 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e o enter a mutex.
91ca0 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 If another thr
91cb0 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 ead is already w
91cc0 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c ithin the mutex,
91cd0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .** sqlite3_mute
91ce0 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 x_enter() will b
91cf0 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 lock and sqlite3
91d00 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c _mutex_try() wil
91d10 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 l return.** SQLI
91d20 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 TE_BUSY. The sq
91d30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
91d40 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 ) interface retu
91d50 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a rns SQLITE_OK.**
91d60 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c upon successful
91d70 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 entry. Mutexes
91d80 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 created using S
91d90 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
91da0 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 RSIVE can.** be
91db0 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 entered multiple
91dc0 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 times by the sa
91dd0 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 me thread. In s
91de0 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a uch cases the,.*
91df0 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 * mutex must be
91e00 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 exited an equal
91e10 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 number of times
91e20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 before another t
91e30 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 hread.** can ent
91e40 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 er. If the same
91e50 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f thread tries to
91e60 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 enter any other
91e70 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a kind of mutex.*
91e80 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 * more than once
91e90 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 , the behavior i
91ea0 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a s undefined..*/.
91eb0 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 75 static void debu
91ec0 67 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 gMutexEnter(sqli
91ed0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
91ee0 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d assert( p->id==
91ef0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
91f00 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d URSIVE || debugM
91f10 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 utexNotheld(p) )
91f20 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a ;. p->cnt++;.}.
91f30 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 static int debug
91f40 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 MutexTry(sqlite3
91f50 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 _mutex *p){. as
91f60 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c sert( p->id==SQL
91f70 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
91f80 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 IVE || debugMute
91f90 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 xNotheld(p) );.
91fa0 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72 65 74 p->cnt++;. ret
91fb0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
91fc0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ../*.** The sqli
91fd0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
91fe0 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 ) routine exits
91ff0 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 a mutex that was
92000 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 .** previously e
92010 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 ntered by the sa
92020 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 me thread. The
92030 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 behavior.** is u
92040 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 ndefined if the
92050 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 mutex is not cur
92060 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f rently entered o
92070 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 r.** is not curr
92080 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e ently allocated.
92090 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 SQLite will ne
920a0 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a ver do either..*
920b0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 /.static void de
920c0 62 75 67 4d 75 74 65 78 4c 65 61 76 65 28 73 71 bugMutexLeave(sq
920d0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
920e0 0a 20 20 61 73 73 65 72 74 28 20 64 65 62 75 67 . assert( debug
920f0 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a MutexHeld(p) );.
92100 20 20 70 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 61 73 p->cnt--;. as
92110 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c sert( p->id==SQL
92120 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
92130 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 IVE || debugMute
92140 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 7d xNotheld(p) );.}
92150 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
92160 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
92170 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 ethods *sqlite3D
92180 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 efaultMutex(void
92190 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 ){. static sqli
921a0 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
921b0 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 s sMutex = {.
921c0 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 2c debugMutexInit,
921d0 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 45 . debugMutexE
921e0 6e 64 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 nd,. debugMut
921f0 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 64 65 62 exAlloc,. deb
92200 75 67 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 ugMutexFree,.
92210 20 64 65 62 75 67 4d 75 74 65 78 45 6e 74 65 72 debugMutexEnter
92220 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 ,. debugMutex
92230 54 72 79 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 Try,. debugMu
92240 74 65 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20 64 texLeave,.. d
92250 65 62 75 67 4d 75 74 65 78 48 65 6c 64 2c 0a 20 ebugMutexHeld,.
92260 20 20 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 debugMutexNot
92270 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 held. };.. ret
92280 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 urn &sMutex;.}.#
92290 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 endif /* defined
922a0 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f (SQLITE_MUTEX_NO
922b0 4f 50 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 OP) && defined(S
922c0 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a QLITE_DEBUG) */.
922d0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
922e0 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 6e 6f End of mutex_no
922f0 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a op.c ***********
92300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
92320 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
92330 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 Begin file mute
92340 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a x_os2.c ********
92350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
92370 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 ./*.** 2007 Augu
92380 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 28.**.** The
92390 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
923a0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
923b0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
923c0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
923d0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
923e0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
923f0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
92400 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
92410 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
92420 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
92430 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
92440 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
92450 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
92460 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
92470 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
92480 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
92490 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
924a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
924b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
924c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
924d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
924e0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
924f0 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 e contains the C
92500 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
92510 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 implement mutexe
92520 73 20 66 6f 72 20 4f 53 2f 32 0a 2a 2a 0a 2a 2a s for OS/2.**.**
92530 20 24 49 64 3a 20 6d 75 74 65 78 5f 6f 73 32 2e $Id: mutex_os2.
92540 63 2c 76 20 31 2e 31 31 20 32 30 30 38 2f 31 31 c,v 1.11 2008/11
92550 2f 32 32 20 31 39 3a 35 30 3a 35 34 20 70 77 65 /22 19:50:54 pwe
92560 69 6c 62 61 63 68 65 72 20 45 78 70 20 24 0a 2a ilbacher Exp $.*
92570 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 /../*.** The cod
92580 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 e in this file i
92590 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 53 s only used if S
925a0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 QLITE_MUTEX_OS2
925b0 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 53 is defined..** S
925c0 65 65 20 74 68 65 20 6d 75 74 65 78 2e 68 20 66 ee the mutex.h f
925d0 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e ile for details.
925e0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
925f0 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 0a 2f 2a 2a E_MUTEX_OS2../**
92600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92610 2a 2a 2a 2a 20 4f 53 2f 32 20 4d 75 74 65 78 20 **** OS/2 Mutex
92620 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a Implementation *
92630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92640 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
92650 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
92660 6f 66 20 6d 75 74 65 78 65 73 20 69 73 20 62 75 of mutexes is bu
92670 69 6c 74 20 75 73 69 6e 67 20 74 68 65 20 4f 53 ilt using the OS
92680 2f 32 20 41 50 49 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a /2 API..*/../*.*
92690 2a 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a 65 * The mutex obje
926a0 63 74 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 ct.** Each recur
926b0 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e sive mutex is an
926c0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
926d0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
926e0 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ture..*/.struct
926f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a sqlite3_mutex {.
92700 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 20 20 20 HMTX mutex;
92710 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e /* Mutex con
92720 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63 trolling the loc
92730 6b 20 2a 2f 0a 20 20 69 6e 74 20 20 69 64 3b 20 k */. int id;
92740 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 /* Mute
92750 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 x type */. int
92760 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 2f 2a nRef; /*
92770 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 Number of refer
92780 65 6e 63 65 73 20 2a 2f 0a 20 20 54 49 44 20 20 ences */. TID
92790 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 owner; /*
927a0 54 68 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 Thread holding t
927b0 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a his mutex */.};.
927c0 0a 23 64 65 66 69 6e 65 20 4f 53 32 5f 4d 55 54 .#define OS2_MUT
927d0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 20 EX_INITIALIZER
927e0 20 30 2c 30 2c 30 2c 30 0a 0a 2f 2a 0a 2a 2a 20 0,0,0,0../*.**
927f0 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 Initialize and d
92800 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 einitialize the
92810 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e mutex subsystem.
92820 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
92830 73 32 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 s2MutexInit(void
92840 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ){ return SQLITE
92850 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e _OK; }.static in
92860 74 20 6f 73 32 4d 75 74 65 78 45 6e 64 28 76 6f t os2MutexEnd(vo
92870 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 id){ return SQLI
92880 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK; }../*.**
92890 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
928a0 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e x_alloc() routin
928b0 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 e allocates a ne
928c0 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 w.** mutex and r
928d0 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
928e0 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 to it. If it r
928f0 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 eturns NULL.** t
92900 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 hat means that a
92910 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 mutex could not
92920 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 0a be allocated. .
92930 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 ** SQLite will u
92940 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 nwind its stack
92950 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 and return an er
92960 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65 ror. The argume
92970 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 nt.** to sqlite3
92980 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 _mutex_alloc() i
92990 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 s one of these i
929a0 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 nteger constants
929b0 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 :.**.** <ul>.**
929c0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
929d0 45 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20 EX_FAST
929e0 20 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20 0.** <li>
929f0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
92a00 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20 20 CURSIVE
92a10 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 1.** <li> SQLI
92a20 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
92a30 4d 41 53 54 45 52 20 20 20 20 20 20 32 0a 2a 2a MASTER 2.**
92a40 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
92a50 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20 TEX_STATIC_MEM
92a60 20 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e 3.** <li>
92a70 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
92a80 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20 TATIC_PRNG
92a90 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 4.** </ul>.**.
92aa0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f ** The first two
92ab0 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65 constants cause
92ac0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
92ad0 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65 lloc() to create
92ae0 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e .** a new mutex.
92af0 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 The new mutex
92b00 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65 is recursive whe
92b10 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 n SQLITE_MUTEX_R
92b20 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 ECURSIVE.** is u
92b30 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 sed but not nece
92b40 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 ssarily so when
92b50 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
92b60 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68 T is used..** Th
92b70 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e e mutex implemen
92b80 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 tation does not
92b90 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 need to make a d
92ba0 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 istinction.** be
92bb0 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 tween SQLITE_MUT
92bc0 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64 EX_RECURSIVE and
92bd0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
92be0 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a ST if it does.**
92bf0 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 not want to. B
92c00 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f ut SQLite will o
92c10 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65 nly request a re
92c20 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e cursive mutex in
92c30 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 .** cases where
92c40 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 it really needs
92c50 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65 one. If a faste
92c60 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 r non-recursive
92c70 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 mutex.** impleme
92c80 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c ntation is avail
92c90 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 able on the host
92ca0 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d platform, the m
92cb0 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a utex subsystem.*
92cc0 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73 * might return s
92cd0 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72 uch a mutex in r
92ce0 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 esponse to SQLIT
92cf0 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a E_MUTEX_FAST..**
92d00 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c .** The other al
92d10 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73 lowed parameters
92d20 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 to sqlite3_mute
92d30 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 x_alloc() each r
92d40 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 eturn.** a point
92d50 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 er to a static p
92d60 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 reexisting mutex
92d70 2e 20 20 54 68 72 65 65 20 73 74 61 74 69 63 20 . Three static
92d80 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 mutexes are.** u
92d90 73 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65 sed by the curre
92da0 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 nt version of SQ
92db0 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 76 65 Lite. Future ve
92dc0 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 rsions of SQLite
92dd0 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69 .** may add addi
92de0 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 tional static mu
92df0 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d texes. Static m
92e00 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 69 utexes are for i
92e10 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 nternal.** use b
92e20 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 y SQLite only.
92e30 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 Applications tha
92e40 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74 t use SQLite mut
92e50 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 exes should.** u
92e60 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 se only the dyna
92e70 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 74 75 mic mutexes retu
92e80 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d rned by SQLITE_M
92e90 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 UTEX_FAST or.**
92ea0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
92eb0 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f URSIVE..**.** No
92ec0 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f te that if one o
92ed0 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 f the dynamic mu
92ee0 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 tex parameters (
92ef0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
92f00 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d T.** or SQLITE_M
92f10 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 UTEX_RECURSIVE)
92f20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c is used then sql
92f30 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
92f40 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 ().** returns a
92f50 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 different mutex
92f60 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 on every call.
92f70 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 But for the stat
92f80 69 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 ic.** mutex type
92f90 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 s, the same mute
92fa0 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e x is returned on
92fb0 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 every call that
92fc0 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 has.** the same
92fd0 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f type number..*/
92fe0 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f .static sqlite3_
92ff0 6d 75 74 65 78 20 2a 6f 73 32 4d 75 74 65 78 41 mutex *os2MutexA
93000 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b lloc(int iType){
93010 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
93020 20 2a 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 73 77 *p = NULL;. sw
93030 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 itch( iType ){.
93040 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d case SQLITE_M
93050 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 UTEX_FAST:. c
93060 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ase SQLITE_MUTEX
93070 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 _RECURSIVE: {.
93080 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d p = sqlite3M
93090 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f allocZero( sizeo
930a0 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 f(*p) );. i
930b0 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 f( p ){.
930c0 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 p->id = iType;.
930d0 20 20 20 20 20 20 20 69 66 28 20 44 6f 73 43 72 if( DosCr
930e0 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 30 2c eateMutexSem( 0,
930f0 20 26 70 2d 3e 6d 75 74 65 78 2c 20 30 2c 20 46 &p->mutex, 0, F
93100 41 4c 53 45 20 29 20 21 3d 20 4e 4f 5f 45 52 52 ALSE ) != NO_ERR
93110 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OR ){.
93120 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20 sqlite3_free( p
93130 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d );. p =
93140 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 7d NULL;. }
93150 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
93160 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
93170 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
93180 20 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c 65 static volatile
93190 20 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 30 3b int isInit = 0;
931a0 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 73 71 . static sq
931b0 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61 74 lite3_mutex stat
931c0 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20 7b 0a icMutexes[] = {.
931d0 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 { OS2_MU
931e0 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
931f0 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 },. { OS
93200 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 2_MUTEX_INITIALI
93210 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 ZER, },.
93220 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 { OS2_MUTEX_INIT
93230 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 IALIZER, },.
93240 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f { OS2_MUTEX_
93250 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a INITIALIZER, },.
93260 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 { OS2_MU
93270 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
93280 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 },. { OS
93290 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 2_MUTEX_INITIALI
932a0 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 7d 3b ZER, },. };
932b0 0a 20 20 20 20 20 20 69 66 20 28 20 21 69 73 49 . if ( !isI
932c0 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 41 nit ){. A
932d0 50 49 52 45 54 20 72 63 3b 0a 20 20 20 20 20 20 PIRET rc;.
932e0 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 20 PTIB ptib;.
932f0 20 20 20 20 20 50 50 49 42 20 70 70 69 62 3b 0a PPIB ppib;.
93300 20 20 20 20 20 20 20 20 48 4d 54 58 20 6d 75 74 HMTX mut
93310 65 78 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 ex;. char
93320 20 6e 61 6d 65 5b 33 32 5d 3b 0a 20 20 20 20 20 name[32];.
93330 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f DosGetInfoBlo
93340 63 6b 73 28 20 26 70 74 69 62 2c 20 26 70 70 69 cks( &ptib, &ppi
93350 62 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c b );. sql
93360 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 73 ite3_snprintf( s
93370 69 7a 65 6f 66 28 6e 61 6d 65 29 2c 20 6e 61 6d izeof(name), nam
93380 65 2c 20 22 5c 5c 53 45 4d 33 32 5c 5c 53 51 4c e, "\\SEM32\\SQL
93390 49 54 45 25 30 34 78 22 2c 0a 20 20 20 20 20 20 ITE%04x",.
933a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
933b0 20 20 20 20 70 70 69 62 2d 3e 70 69 62 5f 75 6c ppib->pib_ul
933c0 70 69 64 20 29 3b 0a 20 20 20 20 20 20 20 20 77 pid );. w
933d0 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20 29 7b hile( !isInit ){
933e0 0a 20 20 20 20 20 20 20 20 20 20 6d 75 74 65 78 . mutex
933f0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
93400 72 63 20 3d 20 44 6f 73 43 72 65 61 74 65 4d 75 rc = DosCreateMu
93410 74 65 78 53 65 6d 28 20 6e 61 6d 65 2c 20 26 6d texSem( name, &m
93420 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 29 3b utex, 0, FALSE);
93430 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
93440 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b c == NO_ERROR ){
93450 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 6e 73 . uns
93460 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 20 igned int i;.
93470 20 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 if( !is
93480 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Init ){.
93490 20 20 20 20 20 20 66 6f 72 28 20 69 20 3d 20 30 for( i = 0
934a0 3b 20 69 20 3c 20 73 69 7a 65 6f 66 28 73 74 61 ; i < sizeof(sta
934b0 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 ticMutexes)/size
934c0 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 of(staticMutexes
934d0 5b 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 [0]); i++ ){.
934e0 20 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 Dos
934f0 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 CreateMutexSem(
93500 30 2c 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 0, &staticMutexe
93510 73 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c 20 46 s[i].mutex, 0, F
93520 41 4c 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20 ALSE );.
93530 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
93540 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31 isInit = 1
93550 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
93560 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 43 DosC
93570 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 6d 75 loseMutexSem( mu
93580 74 65 78 20 29 3b 0a 20 20 20 20 20 20 20 20 20 tex );.
93590 20 7d 65 6c 73 65 20 69 66 28 20 72 63 20 3d 3d }else if( rc ==
935a0 20 45 52 52 4f 52 5f 44 55 50 4c 49 43 41 54 45 ERROR_DUPLICATE
935b0 5f 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20 20 20 _NAME ){.
935c0 20 20 20 20 20 44 6f 73 53 6c 65 65 70 28 20 31 DosSleep( 1
935d0 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 );. }e
935e0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
935f0 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 20 return p;.
93600 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
93610 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
93620 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e ssert( iType-2 >
93630 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 = 0 );. ass
93640 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c 20 73 ert( iType-2 < s
93650 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 izeof(staticMute
93660 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74 xes)/sizeof(stat
93670 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 20 29 3b icMutexes[0]) );
93680 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 . p = &stat
93690 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d icMutexes[iType-
936a0 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 2];. p->id
936b0 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 = iType;. b
936c0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
936d0 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a return p;.}...
936e0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
936f0 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 ne deallocates a
93700 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f previously allo
93710 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2a 20 cated mutex..**
93720 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 SQLite is carefu
93730 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 l to deallocate
93740 65 76 65 72 79 20 6d 75 74 65 78 20 74 68 61 74 every mutex that
93750 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a it allocates..*
93760 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 /.static void os
93770 32 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 2MutexFree(sqlit
93780 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
93790 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
937a0 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e n;. assert( p->
937b0 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 nRef==0 );. ass
937c0 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
937d0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c TE_MUTEX_FAST ||
937e0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
937f0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 UTEX_RECURSIVE )
93800 3b 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74 65 ;. DosCloseMute
93810 78 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78 20 29 xSem( p->mutex )
93820 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
93830 28 20 70 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ( p );.}../*.**
93840 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
93850 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 x_enter() and sq
93860 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
93870 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d ) routines attem
93880 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 pt.** to enter a
93890 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 mutex. If anot
938a0 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c her thread is al
938b0 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 ready within the
938c0 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 mutex,.** sqlit
938d0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
938e0 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 will block and
938f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
93900 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a y() will return.
93910 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 ** SQLITE_BUSY.
93920 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
93930 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 ex_try() interfa
93940 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 ce returns SQLIT
93950 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 E_OK.** upon suc
93960 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 cessful entry.
93970 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 Mutexes created
93980 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 using SQLITE_MUT
93990 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e EX_RECURSIVE can
939a0 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d .** be entered m
939b0 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 ultiple times by
939c0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
939d0 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 . In such cases
939e0 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d the,.** mutex m
939f0 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e ust be exited an
93a00 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 equal number of
93a10 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e times before an
93a20 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 other thread.**
93a30 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 can enter. If t
93a40 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 he same thread t
93a50 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e ries to enter an
93a60 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 y other kind of
93a70 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 mutex.** more th
93a80 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 an once, the beh
93a90 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
93aa0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ed..*/.static vo
93ab0 69 64 20 6f 73 32 4d 75 74 65 78 45 6e 74 65 72 id os2MutexEnter
93ac0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
93ad0 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 p){. TID tid;.
93ae0 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20 PID holder1;.
93af0 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20 ULONG holder2;.
93b00 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
93b10 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d rn;. assert( p-
93b20 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
93b30 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f X_RECURSIVE || o
93b40 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 s2MutexNotheld(p
93b50 29 20 29 3b 0a 20 20 44 6f 73 52 65 71 75 65 73 ) );. DosReques
93b60 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 tMutexSem(p->mut
93b70 65 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49 4e 49 ex, SEM_INDEFINI
93b80 54 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f 73 51 TE_WAIT);. DosQ
93b90 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e ueryMutexSem(p->
93ba0 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c mutex, &holder1,
93bb0 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 29 &tid, &holder2)
93bc0 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 ;. p->owner = t
93bd0 69 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b id;. p->nRef++;
93be0 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 .}.static int os
93bf0 32 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 2MutexTry(sqlite
93c00 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 3_mutex *p){. i
93c10 6e 74 20 72 63 3b 0a 20 20 54 49 44 20 74 69 64 nt rc;. TID tid
93c20 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b ;. PID holder1;
93c30 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 . ULONG holder2
93c40 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
93c50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
93c60 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 . assert( p->id
93c70 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
93c80 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d ECURSIVE || os2M
93c90 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 utexNotheld(p) )
93ca0 3b 0a 20 20 69 66 28 20 44 6f 73 52 65 71 75 65 ;. if( DosReque
93cb0 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 stMutexSem(p->mu
93cc0 74 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44 49 41 tex, SEM_IMMEDIA
93cd0 54 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20 4e 4f TE_RETURN) == NO
93ce0 5f 45 52 52 4f 52 29 20 7b 0a 20 20 20 20 44 6f _ERROR) {. Do
93cf0 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 sQueryMutexSem(p
93d00 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 ->mutex, &holder
93d10 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 1, &tid, &holder
93d20 32 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 2);. p->owner
93d30 20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d 3e 6e = tid;. p->n
93d40 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 Ref++;. rc =
93d50 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 65 SQLITE_OK;. } e
93d60 6c 73 65 20 7b 0a 20 20 20 20 72 63 20 3d 20 53 lse {. rc = S
93d70 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a QLITE_BUSY;. }.
93d80 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
93d90 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
93da0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 e3_mutex_leave()
93db0 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 routine exits a
93dc0 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a mutex that was.
93dd0 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e ** previously en
93de0 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d tered by the sam
93df0 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 e thread. The b
93e00 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e ehavior.** is un
93e10 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d defined if the m
93e20 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 utex is not curr
93e30 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 ently entered or
93e40 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 .** is not curre
93e50 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 ntly allocated.
93e60 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 SQLite will nev
93e70 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f er do either..*/
93e80 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 .static void os2
93e90 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 MutexLeave(sqlit
93ea0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
93eb0 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68 TID tid;. PID h
93ec0 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 older1;. ULONG
93ed0 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70 holder2;. if( p
93ee0 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
93ef0 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e assert( p->nRef>
93f00 30 20 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 4d 0 );. DosQueryM
93f10 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 utexSem(p->mutex
93f20 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64 , &holder1, &tid
93f30 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 61 , &holder2);. a
93f40 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d ssert( p->owner=
93f50 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 =tid );. p->nRe
93f60 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 f--;. assert( p
93f70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e ->nRef==0 || p->
93f80 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
93f90 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 _RECURSIVE );.
93fa0 44 6f 73 52 65 6c 65 61 73 65 4d 75 74 65 78 53 DosReleaseMutexS
93fb0 65 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a em(p->mutex);.}.
93fc0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
93fd0 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 EBUG./*.** The s
93fe0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
93ff0 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f d() and sqlite3_
94000 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 mutex_notheld()
94010 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 routine are.** i
94020 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 ntended for use
94030 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 inside assert()
94040 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 statements..*/.s
94050 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 tatic int os2Mut
94060 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d exHeld(sqlite3_m
94070 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 utex *p){. TID
94080 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 3b 0a tid;. PID pid;.
94090 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b ULONG ulCount;
940a0 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 . PTIB ptib;.
940b0 69 66 28 20 70 21 3d 30 20 29 20 7b 0a 20 20 20 if( p!=0 ) {.
940c0 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 DosQueryMutexSe
940d0 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64 m(p->mutex, &pid
940e0 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74 , &tid, &ulCount
940f0 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 );. } else {.
94100 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 DosGetInfoBloc
94110 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b ks(&ptib, NULL);
94120 0a 20 20 20 20 74 69 64 20 3d 20 70 74 69 62 2d . tid = ptib-
94130 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 >tib_ptib2->tib2
94140 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65 _ultid;. }. re
94150 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28 70 2d turn p==0 || (p-
94160 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f >nRef!=0 && p->o
94170 77 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d 0a 73 74 wner==tid);.}.st
94180 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 atic int os2Mute
94190 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 xNotheld(sqlite3
941a0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 _mutex *p){. TI
941b0 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 D tid;. PID pid
941c0 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e ;. ULONG ulCoun
941d0 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a t;. PTIB ptib;.
941e0 20 20 69 66 28 20 70 21 3d 20 30 20 29 20 7b 0a if( p!= 0 ) {.
941f0 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 DosQueryMute
94200 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 xSem(p->mutex, &
94210 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f pid, &tid, &ulCo
94220 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b unt);. } else {
94230 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 . DosGetInfoB
94240 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c locks(&ptib, NUL
94250 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74 L);. tid = pt
94260 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 ib->tib_ptib2->t
94270 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 ib2_ultid;. }.
94280 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 return p==0 ||
94290 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d p->nRef==0 || p-
942a0 3e 6f 77 6e 65 72 21 3d 74 69 64 3b 0a 7d 0a 23 >owner!=tid;.}.#
942b0 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
942c0 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 IVATE sqlite3_mu
942d0 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c tex_methods *sql
942e0 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 ite3DefaultMutex
942f0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 (void){. static
94300 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
94310 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 ethods sMutex =
94320 7b 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 49 6e {. os2MutexIn
94330 69 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 it,. os2Mutex
94340 45 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 End,. os2Mute
94350 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73 32 4d xAlloc,. os2M
94360 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6f 73 utexFree,. os
94370 32 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 2MutexEnter,.
94380 20 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a 20 20 os2MutexTry,.
94390 20 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 65 2c os2MutexLeave,
943a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
943b0 45 42 55 47 0a 20 20 20 20 6f 73 32 4d 75 74 65 EBUG. os2Mute
943c0 78 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32 4d 75 xHeld,. os2Mu
943d0 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64 69 texNotheld.#endi
943e0 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e f. };.. return
943f0 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 &sMutex;.}.#end
94400 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 if /* SQLITE_MUT
94410 45 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a EX_OS2 */../****
94420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
94430 66 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a 2a f mutex_os2.c **
94440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
94470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
94480 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e 69 78 file mutex_unix
94490 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
944a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
944b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
944c0 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a 2007 August 28.
944d0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
944e0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
944f0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
94500 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
94510 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
94520 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
94530 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
94540 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
94550 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
94560 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
94570 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
94580 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
94590 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
945a0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
945b0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
945c0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
945d0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
945e0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
945f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
94630 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
94640 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 ains the C funct
94650 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d ions that implem
94660 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 ent mutexes for
94670 70 74 68 72 65 61 64 73 0a 2a 2a 0a 2a 2a 20 24 pthreads.**.** $
94680 49 64 3a 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 Id: mutex_unix.c
94690 2c 76 20 31 2e 31 36 20 32 30 30 38 2f 31 32 2f ,v 1.16 2008/12/
946a0 30 38 20 31 38 3a 31 39 3a 31 38 20 64 72 68 20 08 18:19:18 drh
946b0 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Exp $.*/../*.**
946c0 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 The code in this
946d0 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 file is only us
946e0 65 64 20 69 66 20 77 65 20 61 72 65 20 63 6f 6d ed if we are com
946f0 70 69 6c 69 6e 67 20 74 68 72 65 61 64 73 61 66 piling threadsaf
94700 65 0a 2a 2a 20 75 6e 64 65 72 20 75 6e 69 78 20 e.** under unix
94710 77 69 74 68 20 70 74 68 72 65 61 64 73 2e 0a 2a with pthreads..*
94720 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 *.** Note that t
94730 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 his implementati
94740 6f 6e 20 72 65 71 75 69 72 65 73 20 61 20 76 65 on requires a ve
94750 72 73 69 6f 6e 20 6f 66 20 70 74 68 72 65 61 64 rsion of pthread
94760 73 20 74 68 61 74 0a 2a 2a 20 73 75 70 70 6f 72 s that.** suppor
94770 74 73 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 ts recursive mut
94780 65 78 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 exes..*/.#ifdef
94790 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 SQLITE_MUTEX_PTH
947a0 52 45 41 44 53 0a 0a 23 69 6e 63 6c 75 64 65 20 READS..#include
947b0 3c 70 74 68 72 65 61 64 2e 68 3e 0a 0a 0a 2f 2a <pthread.h>.../*
947c0 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69 .** Each recursi
947d0 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69 ve mutex is an i
947e0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
947f0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
94800 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 re..*/.struct sq
94810 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 lite3_mutex {.
94820 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 pthread_mutex_t
94830 6d 75 74 65 78 3b 20 20 20 20 20 2f 2a 20 4d 75 mutex; /* Mu
94840 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 tex controlling
94850 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e the lock */. in
94860 74 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 t id;
94870 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 /* Mute
94880 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 x type */. int
94890 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 nRef;
948a0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
948b0 20 6f 66 20 65 6e 74 72 61 6e 63 65 73 20 2a 2f of entrances */
948c0 0a 20 20 70 74 68 72 65 61 64 5f 74 20 6f 77 6e . pthread_t own
948d0 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a er; /*
948e0 20 54 68 72 65 61 64 20 74 68 61 74 20 69 73 20 Thread that is
948f0 77 69 74 68 69 6e 20 74 68 69 73 20 6d 75 74 65 within this mute
94900 78 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 x */.#ifdef SQLI
94910 54 45 5f 44 45 42 55 47 0a 20 20 69 6e 74 20 74 TE_DEBUG. int t
94920 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 race;
94930 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f /* True to
94940 20 74 72 61 63 65 20 63 68 61 6e 67 65 73 20 2a trace changes *
94950 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 69 66 64 /.#endif.};.#ifd
94960 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
94970 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f #define SQLITE3_
94980 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
94990 52 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45 R { PTHREAD_MUTE
949a0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 X_INITIALIZER, 0
949b0 2c 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29 , 0, (pthread_t)
949c0 30 2c 20 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65 0, 0 }.#else.#de
949d0 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 fine SQLITE3_MUT
949e0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b EX_INITIALIZER {
949f0 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 PTHREAD_MUTEX_I
94a00 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 NITIALIZER, 0, 0
94a10 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30 20 7d , (pthread_t)0 }
94a20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
94a30 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
94a40 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 _held() and sqli
94a50 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
94a60 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a d() routine are.
94a70 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 ** intended for
94a80 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20 use only inside
94a90 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
94aa0 6e 74 73 2e 20 20 4f 6e 20 73 6f 6d 65 20 70 6c nts. On some pl
94ab0 61 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74 68 65 72 atforms,.** ther
94ac0 65 20 6d 69 67 68 74 20 62 65 20 72 61 63 65 20 e might be race
94ad0 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20 conditions that
94ae0 63 61 6e 20 63 61 75 73 65 20 74 68 65 73 65 20 can cause these
94af0 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 64 routines to.** d
94b00 65 6c 69 76 65 72 20 69 6e 63 6f 72 72 65 63 74 eliver incorrect
94b10 20 72 65 73 75 6c 74 73 2e 20 20 49 6e 20 70 61 results. In pa
94b20 72 74 69 63 75 6c 61 72 2c 20 69 66 20 70 74 68 rticular, if pth
94b30 72 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 0a read_equal() is.
94b40 2a 2a 20 6e 6f 74 20 61 6e 20 61 74 6f 6d 69 63 ** not an atomic
94b50 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e operation, then
94b60 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
94b70 6d 69 67 68 74 20 64 65 6c 69 76 65 72 79 0a 2a might delivery.*
94b80 2a 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 * incorrect resu
94b90 6c 74 73 2e 20 20 4f 6e 20 6d 6f 73 74 20 70 6c lts. On most pl
94ba0 61 74 66 6f 72 6d 73 2c 20 70 74 68 72 65 61 64 atforms, pthread
94bb0 5f 65 71 75 61 6c 28 29 20 69 73 20 61 20 0a 2a _equal() is a .*
94bc0 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 * comparison of
94bd0 74 77 6f 20 69 6e 74 65 67 65 72 73 20 61 6e 64 two integers and
94be0 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 61 74 is therefore at
94bf0 6f 6d 69 63 2e 20 20 42 75 74 20 77 65 20 61 72 omic. But we ar
94c00 65 0a 2a 2a 20 74 6f 6c 64 20 74 68 61 74 20 48 e.** told that H
94c10 50 55 58 20 69 73 20 6e 6f 74 20 73 75 63 68 20 PUX is not such
94c20 61 20 70 6c 61 74 66 6f 72 6d 2e 20 20 49 66 20 a platform. If
94c30 73 6f 2c 20 74 68 65 6e 20 74 68 65 73 65 20 72 so, then these r
94c40 6f 75 74 69 6e 65 73 0a 2a 2a 20 77 69 6c 6c 20 outines.** will
94c50 6e 6f 74 20 61 6c 77 61 79 73 20 77 6f 72 6b 20 not always work
94c60 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 48 50 55 correctly on HPU
94c70 58 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 6f 73 X..**.** On thos
94c80 65 20 70 6c 61 74 66 6f 72 6d 73 20 77 68 65 72 e platforms wher
94c90 65 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 e pthread_equal(
94ca0 29 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2c ) is not atomic,
94cb0 20 53 51 4c 69 74 65 0a 2a 2a 20 73 68 6f 75 6c SQLite.** shoul
94cc0 64 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 d be compiled wi
94cd0 74 68 6f 75 74 20 2d 44 53 51 4c 49 54 45 5f 44 thout -DSQLITE_D
94ce0 45 42 55 47 20 61 6e 64 20 77 69 74 68 20 2d 44 EBUG and with -D
94cf0 4e 44 45 42 55 47 20 74 6f 0a 2a 2a 20 6d 61 6b NDEBUG to.** mak
94d00 65 20 73 75 72 65 20 6e 6f 20 61 73 73 65 72 74 e sure no assert
94d10 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 () statements ar
94d20 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 e evaluated and
94d30 68 65 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 72 hence these.** r
94d40 6f 75 74 69 6e 65 73 20 61 72 65 20 6e 65 76 65 outines are neve
94d50 72 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 r called..*/.#if
94d60 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
94d70 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
94d80 49 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69 ITE_DEBUG).stati
94d90 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 c int pthreadMut
94da0 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d exHeld(sqlite3_m
94db0 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 utex *p){. retu
94dc0 72 6e 20 28 70 2d 3e 6e 52 65 66 21 3d 30 20 26 rn (p->nRef!=0 &
94dd0 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 & pthread_equal(
94de0 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 p->owner, pthrea
94df0 64 5f 73 65 6c 66 28 29 29 29 3b 0a 7d 0a 73 74 d_self()));.}.st
94e00 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 atic int pthread
94e10 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c MutexNotheld(sql
94e20 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
94e30 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 return p->nRef
94e40 3d 3d 30 20 7c 7c 20 70 74 68 72 65 61 64 5f 65 ==0 || pthread_e
94e50 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 qual(p->owner, p
94e60 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 3d 3d thread_self())==
94e70 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 0;.}.#endif../*.
94e80 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e ** Initialize an
94e90 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 d deinitialize t
94ea0 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 he mutex subsyst
94eb0 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e em..*/.static in
94ec0 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 49 6e t pthreadMutexIn
94ed0 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e it(void){ return
94ee0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 SQLITE_OK; }.st
94ef0 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 atic int pthread
94f00 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 MutexEnd(void){
94f10 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
94f20 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 ; }../*.** The s
94f30 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
94f40 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c oc() routine all
94f50 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 ocates a new.**
94f60 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e mutex and return
94f70 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 s a pointer to i
94f80 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e t. If it return
94f90 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d s NULL.** that m
94fa0 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 eans that a mute
94fb0 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 x could not be a
94fc0 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 llocated. SQLit
94fd0 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64 e.** will unwind
94fe0 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 its stack and r
94ff0 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 eturn an error.
95000 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a The argument.**
95010 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 to sqlite3_mute
95020 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 x_alloc() is one
95030 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 of these intege
95040 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a r constants:.**.
95050 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 ** <ul>.** <li>
95060 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
95070 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 ST.** <li> SQLI
95080 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
95090 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 VE.** <li> SQLI
950a0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
950b0 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 MASTER.** <li>
950c0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
950d0 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 TIC_MEM.** <li>
950e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
950f0 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 ATIC_MEM2.** <li
95100 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
95110 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c STATIC_PRNG.** <
95120 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
95130 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 X_STATIC_LRU.**
95140 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
95150 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a EX_STATIC_LRU2.*
95160 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 * </ul>.**.** Th
95170 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 e first two cons
95180 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 tants cause sqli
95190 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 te3_mutex_alloc(
951a0 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 ) to create.** a
951b0 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 new mutex. The
951c0 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 new mutex is re
951d0 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c cursive when SQL
951e0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
951f0 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 IVE.** is used b
95200 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 ut not necessari
95210 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 ly so when SQLIT
95220 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 E_MUTEX_FAST is
95230 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 used..** The mut
95240 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ex implementatio
95250 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 n does not need
95260 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e to make a distin
95270 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e ction.** between
95280 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
95290 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 CURSIVE and SQLI
952a0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 TE_MUTEX_FAST if
952b0 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 it does.** not
952c0 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 want to. But SQ
952d0 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 Lite will only r
952e0 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 equest a recursi
952f0 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 ve mutex in.** c
95300 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 ases where it re
95310 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 ally needs one.
95320 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e If a faster non
95330 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 -recursive mutex
95340 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
95350 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 on is available
95360 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 on the host plat
95370 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 form, the mutex
95380 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 subsystem.** mig
95390 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 ht return such a
953a0 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e mutex in respon
953b0 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 se to SQLITE_MUT
953c0 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 EX_FAST..**.** T
953d0 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 he other allowed
953e0 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 parameters to s
953f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
95400 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e oc() each return
95410 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f .** a pointer to
95420 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 a static preexi
95430 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 53 69 sting mutex. Si
95440 78 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 x static mutexes
95450 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 are.** used by
95460 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 the current vers
95470 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 ion of SQLite.
95480 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 Future versions
95490 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 of SQLite.** may
954a0 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 add additional
954b0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 static mutexes.
954c0 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 Static mutexes
954d0 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c are for internal
954e0 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 .** use by SQLit
954f0 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 e only. Applica
95500 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 tions that use S
95510 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 QLite mutexes sh
95520 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 ould.** use only
95530 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 the dynamic mut
95540 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 exes returned by
95550 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
95560 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f ST or.** SQLITE_
95570 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e MUTEX_RECURSIVE.
95580 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
95590 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 if one of the d
955a0 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72 ynamic mutex par
955b0 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f ameters (SQLITE_
955c0 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 MUTEX_FAST.** or
955d0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
955e0 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64 CURSIVE) is used
955f0 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 then sqlite3_mu
95600 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 tex_alloc().** r
95610 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65 eturns a differe
95620 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 nt mutex on ever
95630 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72 y call. But for
95640 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a 20 the static .**
95650 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 mutex types, the
95660 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 same mutex is r
95670 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 eturned on every
95680 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a call that has.*
95690 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 * the same type
956a0 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 number..*/.stati
956b0 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 c sqlite3_mutex
956c0 2a 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c 6c *pthreadMutexAll
956d0 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 oc(int iType){.
956e0 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f static sqlite3_
956f0 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65 mutex staticMute
95700 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 53 51 xes[] = {. SQ
95710 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 LITE3_MUTEX_INIT
95720 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c IALIZER,. SQL
95730 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 ITE3_MUTEX_INITI
95740 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 ALIZER,. SQLI
95750 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 TE3_MUTEX_INITIA
95760 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 LIZER,. SQLIT
95770 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c E3_MUTEX_INITIAL
95780 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 IZER,. SQLITE
95790 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 3_MUTEX_INITIALI
957a0 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 ZER,. SQLITE3
957b0 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a _MUTEX_INITIALIZ
957c0 45 52 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 ER. };. sqlite
957d0 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 20 20 73 77 3_mutex *p;. sw
957e0 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 itch( iType ){.
957f0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d case SQLITE_M
95800 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20 UTEX_RECURSIVE:
95810 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 {. p = sqli
95820 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 te3MallocZero( s
95830 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 izeof(*p) );.
95840 20 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66 64 if( p ){.#ifd
95850 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 ef SQLITE_HOMEGR
95860 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 OWN_RECURSIVE_MU
95870 54 45 58 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 TEX. /* I
95880 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 f recursive mute
95890 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 xes are not avai
958a0 6c 61 62 6c 65 2c 20 77 65 20 77 69 6c 6c 20 68 lable, we will h
958b0 61 76 65 20 74 6f 0a 20 20 20 20 20 20 20 20 2a ave to. *
958c0 2a 20 62 75 69 6c 64 20 6f 75 72 20 6f 77 6e 2e * build our own.
958d0 20 20 53 65 65 20 62 65 6c 6f 77 2e 20 2a 2f 0a See below. */.
958e0 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f pthread_
958f0 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d mutex_init(&p->m
95900 75 74 65 78 2c 20 30 29 3b 0a 23 65 6c 73 65 0a utex, 0);.#else.
95910 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61 /* Use a
95920 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 recursive mutex
95930 20 69 66 20 69 74 20 69 73 20 61 76 61 69 6c 61 if it is availa
95940 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 ble */. p
95950 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 thread_mutexattr
95960 5f 74 20 72 65 63 75 72 73 69 76 65 41 74 74 72 _t recursiveAttr
95970 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 ;. pthrea
95980 64 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e 69 74 d_mutexattr_init
95990 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29 (&recursiveAttr)
959a0 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 ;. pthrea
959b0 64 5f 6d 75 74 65 78 61 74 74 72 5f 73 65 74 74 d_mutexattr_sett
959c0 79 70 65 28 26 72 65 63 75 72 73 69 76 65 41 74 ype(&recursiveAt
959d0 74 72 2c 20 50 54 48 52 45 41 44 5f 4d 55 54 45 tr, PTHREAD_MUTE
959e0 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20 20 X_RECURSIVE);.
959f0 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 pthread_mu
95a00 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 tex_init(&p->mut
95a10 65 78 2c 20 26 72 65 63 75 72 73 69 76 65 41 74 ex, &recursiveAt
95a20 74 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 tr);. pth
95a30 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 64 read_mutexattr_d
95a40 65 73 74 72 6f 79 28 26 72 65 63 75 72 73 69 76 estroy(&recursiv
95a50 65 41 74 74 72 29 3b 0a 23 65 6e 64 69 66 0a 20 eAttr);.#endif.
95a60 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 p->id = i
95a70 54 79 70 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Type;. }.
95a80 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
95a90 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
95aa0 5f 4d 55 54 45 58 5f 46 41 53 54 3a 20 7b 0a 20 _MUTEX_FAST: {.
95ab0 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 p = sqlite3
95ac0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 MallocZero( size
95ad0 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 of(*p) );.
95ae0 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 if( p ){.
95af0 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a p->id = iType;.
95b00 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f pthread_
95b10 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d mutex_init(&p->m
95b20 75 74 65 78 2c 20 30 29 3b 0a 20 20 20 20 20 20 utex, 0);.
95b30 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
95b40 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 }. default
95b50 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 : {. assert
95b60 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 ( iType-2 >= 0 )
95b70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
95b80 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61 79 53 iType-2 < ArrayS
95b90 69 7a 65 28 73 74 61 74 69 63 4d 75 74 65 78 65 ize(staticMutexe
95ba0 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 s) );. p =
95bb0 26 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 &staticMutexes[i
95bc0 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 Type-2];. p
95bd0 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 ->id = iType;.
95be0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
95bf0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
95c00 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 .}.../*.** This
95c10 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 routine dealloca
95c20 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 tes a previously
95c30 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 .** allocated mu
95c40 74 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20 tex. SQLite is
95c50 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c careful to deall
95c60 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d ocate every.** m
95c70 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c utex that it all
95c80 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 ocates..*/.stati
95c90 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75 c void pthreadMu
95ca0 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f texFree(sqlite3_
95cb0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 mutex *p){. ass
95cc0 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 ert( p->nRef==0
95cd0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
95ce0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
95cf0 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d _FAST || p->id==
95d00 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
95d10 55 52 53 49 56 45 20 29 3b 0a 20 20 70 74 68 72 URSIVE );. pthr
95d20 65 61 64 5f 6d 75 74 65 78 5f 64 65 73 74 72 6f ead_mutex_destro
95d30 79 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 y(&p->mutex);.
95d40 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
95d50 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 .}../*.** The sq
95d60 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
95d70 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f r() and sqlite3_
95d80 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 mutex_try() rout
95d90 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 ines attempt.**
95da0 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 to enter a mutex
95db0 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 . If another th
95dc0 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 read is already
95dd0 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 within the mutex
95de0 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 ,.** sqlite3_mut
95df0 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 ex_enter() will
95e00 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 block and sqlite
95e10 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 3_mutex_try() wi
95e20 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c ll return.** SQL
95e30 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 ITE_BUSY. The s
95e40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
95e50 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 () interface ret
95e60 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a urns SQLITE_OK.*
95e70 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 * upon successfu
95e80 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 l entry. Mutexe
95e90 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 s created using
95ea0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
95eb0 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 URSIVE can.** be
95ec0 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c entered multipl
95ed0 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 e times by the s
95ee0 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 ame thread. In
95ef0 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a such cases the,.
95f00 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 ** mutex must be
95f10 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c exited an equal
95f20 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 number of times
95f30 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 before another
95f40 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e thread.** can en
95f50 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d ter. If the sam
95f60 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 e thread tries t
95f70 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 o enter any othe
95f80 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a r kind of mutex.
95f90 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 ** more than onc
95fa0 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 e, the behavior
95fb0 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f is undefined..*/
95fc0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 68 .static void pth
95fd0 72 65 61 64 4d 75 74 65 78 45 6e 74 65 72 28 73 readMutexEnter(s
95fe0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
95ff0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 {. assert( p->i
96000 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
96010 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74 68 RECURSIVE || pth
96020 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 readMutexNotheld
96030 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 (p) );..#ifdef S
96040 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f QLITE_HOMEGROWN_
96050 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a RECURSIVE_MUTEX.
96060 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 76 /* If recursiv
96070 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f e mutexes are no
96080 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 t available, the
96090 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67 72 6f n we have to gro
960a0 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20 w. ** our own.
960b0 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 This implementa
960c0 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 tion assumes tha
960d0 74 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 t pthread_equal(
960e0 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63 ). ** is atomic
960f0 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f - that it canno
96100 74 20 62 65 20 64 65 63 65 69 76 65 64 20 69 6e t be deceived in
96110 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66 to thinking self
96120 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e . ** and p->own
96130 65 72 20 61 72 65 20 65 71 75 61 6c 20 69 66 20 er are equal if
96140 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65 73 p->owner changes
96150 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61 6c between two val
96160 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61 72 ues. ** that ar
96170 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 73 e not equal to s
96180 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63 6f elf while the co
96190 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b 69 mparison is taki
961a0 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54 ng place.. ** T
961b0 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 his implementati
961c0 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 on also assumes
961d0 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 65 a coherent cache
961e0 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 65 - that . ** se
961f0 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 parate processes
96200 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 66 cannot read dif
96210 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 72 ferent values fr
96220 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a om the same. **
96230 20 61 64 64 72 65 73 73 20 61 74 20 74 68 65 20 address at the
96240 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20 65 same time. If e
96250 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 ither of these t
96260 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20 wo conditions.
96270 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20 ** are not met,
96280 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65 73 then the mutexes
96290 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70 will fail and p
962a0 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73 roblems will res
962b0 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 ult.. */. {.
962c0 20 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c 66 pthread_t self
962d0 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 = pthread_self(
962e0 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 );. if( p->nR
962f0 65 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 5f ef>0 && pthread_
96300 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 equal(p->owner,
96310 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20 70 self) ){. p
96320 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 65 ->nRef++;. }e
96330 6c 73 65 7b 0a 20 20 20 20 20 20 70 74 68 72 65 lse{. pthre
96340 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70 ad_mutex_lock(&p
96350 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 ->mutex);.
96360 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d assert( p->nRef=
96370 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f =0 );. p->o
96380 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 wner = self;.
96390 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a p->nRef = 1;.
963a0 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a }. }.#else.
963b0 20 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69 /* Use the bui
963c0 6c 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20 lt-in recursive
963d0 6d 75 74 65 78 65 73 20 69 66 20 74 68 65 79 20 mutexes if they
963e0 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 are available..
963f0 20 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 */. pthread_mu
96400 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 tex_lock(&p->mut
96410 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 ex);. p->owner
96420 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 = pthread_self()
96430 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 ;. p->nRef++;.#
96440 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 endif..#ifdef SQ
96450 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 LITE_DEBUG. if(
96460 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 p->trace ){.
96470 20 70 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d printf("enter m
96480 75 74 65 78 20 25 70 20 28 25 64 29 20 77 69 74 utex %p (%d) wit
96490 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c h nRef=%d\n", p,
964a0 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 p->trace, p->nR
964b0 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ef);. }.#endif.
964c0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 }.static int pth
964d0 72 65 61 64 4d 75 74 65 78 54 72 79 28 73 71 6c readMutexTry(sql
964e0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
964f0 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
96500 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 rt( p->id==SQLIT
96510 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
96520 45 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 74 65 E || pthreadMute
96530 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a xNotheld(p) );..
96540 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f #ifdef SQLITE_HO
96550 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 MEGROWN_RECURSIV
96560 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66 20 E_MUTEX. /* If
96570 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 recursive mutexe
96580 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 s are not availa
96590 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61 76 ble, then we hav
965a0 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f e to grow. ** o
965b0 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d ur own. This im
965c0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 plementation ass
965d0 75 6d 65 73 20 74 68 61 74 20 70 74 68 72 65 61 umes that pthrea
965e0 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69 d_equal(). ** i
965f0 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 20 s atomic - that
96600 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 63 it cannot be dec
96610 65 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b eived into think
96620 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e ing self. ** an
96630 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65 d p->owner are e
96640 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72 qual if p->owner
96650 20 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 6e changes between
96660 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a two values. **
96670 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 71 that are not eq
96680 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 6c ual to self whil
96690 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e e the comparison
966a0 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65 is taking place
966b0 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c .. ** This impl
966c0 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20 ementation also
966d0 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72 65 assumes a cohere
966e0 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61 74 20 nt cache - that
966f0 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 70 . ** separate p
96700 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74 20 rocesses cannot
96710 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 76 read different v
96720 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 73 alues from the s
96730 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73 73 ame. ** address
96740 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
96750 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 66 e. If either of
96760 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64 69 these two condi
96770 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e tions. ** are n
96780 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68 65 ot met, then the
96790 20 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66 61 mutexes will fa
967a0 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20 il and problems
967b0 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a will result.. *
967c0 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65 61 /. {. pthrea
967d0 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72 65 d_t self = pthre
967e0 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 69 ad_self();. i
967f0 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20 f( p->nRef>0 &&
96800 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d pthread_equal(p-
96810 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b >owner, self) ){
96820 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b . p->nRef++
96830 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
96840 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 ITE_OK;. }els
96850 65 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75 e if( pthread_mu
96860 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e tex_trylock(&p->
96870 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 mutex)==0 ){.
96880 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 assert( p->nR
96890 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 ef==0 );. p
968a0 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a ->owner = self;.
968b0 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 p->nRef =
968c0 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 1;. rc = SQ
968d0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c LITE_OK;. }el
968e0 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 se{. rc = S
968f0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
96900 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a }. }.#else. /*
96910 20 55 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 Use the built-i
96920 6e 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 n recursive mute
96930 78 65 73 20 69 66 20 74 68 65 79 20 61 72 65 20 xes if they are
96940 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a available.. */.
96950 20 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75 if( pthread_mu
96960 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e tex_trylock(&p->
96970 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 mutex)==0 ){.
96980 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 p->owner = pthr
96990 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 ead_self();.
969a0 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 p->nRef++;. r
969b0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
969c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d }else{. rc =
969d0 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
969e0 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 }.#endif..#ifdef
969f0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
96a00 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
96a10 4b 20 26 26 20 70 2d 3e 74 72 61 63 65 20 29 7b K && p->trace ){
96a20 0a 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74 . printf("ent
96a30 65 72 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 er mutex %p (%d)
96a40 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 with nRef=%d\n"
96a50 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 , p, p->trace, p
96a60 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e ->nRef);. }.#en
96a70 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b dif. return rc;
96a80 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 .}../*.** The sq
96a90 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
96aa0 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 e() routine exit
96ab0 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 s a mutex that w
96ac0 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 as.** previously
96ad0 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 entered by the
96ae0 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 same thread. Th
96af0 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 e behavior.** is
96b00 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 undefined if th
96b10 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 e mutex is not c
96b20 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 urrently entered
96b30 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 or.** is not cu
96b40 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 rrently allocate
96b50 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 d. SQLite will
96b60 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e never do either.
96b70 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
96b80 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61 76 pthreadMutexLeav
96b90 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
96ba0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
96bb0 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 28 threadMutexHeld(
96bc0 70 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d p) );. p->nRef-
96bd0 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e -;. assert( p->
96be0 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 nRef==0 || p->id
96bf0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
96c00 45 43 55 52 53 49 56 45 20 29 3b 0a 0a 23 69 66 ECURSIVE );..#if
96c10 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 def SQLITE_HOMEG
96c20 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d ROWN_RECURSIVE_M
96c30 55 54 45 58 0a 20 20 69 66 28 20 70 2d 3e 6e 52 UTEX. if( p->nR
96c40 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70 74 68 ef==0 ){. pth
96c50 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 read_mutex_unloc
96c60 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 k(&p->mutex);.
96c70 7d 0a 23 65 6c 73 65 0a 20 20 70 74 68 72 65 61 }.#else. pthrea
96c80 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 d_mutex_unlock(&
96c90 70 2d 3e 6d 75 74 65 78 29 3b 0a 23 65 6e 64 69 p->mutex);.#endi
96ca0 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
96cb0 5f 44 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e _DEBUG. if( p->
96cc0 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 trace ){. pri
96cd0 6e 74 66 28 22 6c 65 61 76 65 20 6d 75 74 65 78 ntf("leave mutex
96ce0 20 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 %p (%d) with nR
96cf0 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e ef=%d\n", p, p->
96d00 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b trace, p->nRef);
96d10 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 53 . }.#endif.}..S
96d20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
96d30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
96d40 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 ods *sqlite3Defa
96d50 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a ultMutex(void){.
96d60 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 static sqlite3
96d70 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 _mutex_methods s
96d80 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 70 74 Mutex = {. pt
96d90 68 72 65 61 64 4d 75 74 65 78 49 6e 69 74 2c 0a hreadMutexInit,.
96da0 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 pthreadMutex
96db0 45 6e 64 2c 0a 20 20 20 20 70 74 68 72 65 61 64 End,. pthread
96dc0 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 MutexAlloc,.
96dd0 70 74 68 72 65 61 64 4d 75 74 65 78 46 72 65 65 pthreadMutexFree
96de0 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 ,. pthreadMut
96df0 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 70 74 68 exEnter,. pth
96e00 72 65 61 64 4d 75 74 65 78 54 72 79 2c 0a 20 20 readMutexTry,.
96e10 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 pthreadMutexLe
96e20 61 76 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 ave,.#ifdef SQLI
96e30 54 45 5f 44 45 42 55 47 0a 20 20 20 20 70 74 68 TE_DEBUG. pth
96e40 72 65 61 64 4d 75 74 65 78 48 65 6c 64 2c 0a 20 readMutexHeld,.
96e50 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e pthreadMutexN
96e60 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20 20 otheld.#else.
96e70 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69 66 0,. 0.#endif
96e80 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 . };.. return
96e90 26 73 4d 75 74 65 78 3b 0a 7d 0a 0a 23 65 6e 64 &sMutex;.}..#end
96ea0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 if /* SQLITE_MUT
96eb0 45 58 5f 50 54 48 52 45 41 44 20 2a 2f 0a 0a 2f EX_PTHREAD */../
96ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
96ed0 6e 64 20 6f 66 20 6d 75 74 65 78 5f 75 6e 69 78 nd of mutex_unix
96ee0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
96ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
96f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
96f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
96f20 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f egin file mutex_
96f30 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a w32.c **********
96f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
96f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
96f60 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 *.** 2007 August
96f70 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 14.**.** The au
96f80 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
96f90 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
96fa0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
96fb0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
96fc0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
96fd0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
96fe0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
96ff0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
97000 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
97010 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
97020 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
97030 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
97040 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
97050 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
97060 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
97070 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
97080 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
97090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
970a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
970b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
970c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
970d0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
970e0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 contains the C f
970f0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d unctions that im
97100 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 plement mutexes
97110 66 6f 72 20 77 69 6e 33 32 0a 2a 2a 0a 2a 2a 20 for win32.**.**
97120 24 49 64 3a 20 6d 75 74 65 78 5f 77 33 32 2e 63 $Id: mutex_w32.c
97130 2c 76 20 31 2e 31 38 20 32 30 30 39 2f 30 38 2f ,v 1.18 2009/08/
97140 31 30 20 30 33 3a 32 33 3a 32 31 20 73 68 61 6e 10 03:23:21 shan
97150 65 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a e Exp $.*/../*.*
97160 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 * The code in th
97170 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 is file is only
97180 75 73 65 64 20 69 66 20 77 65 20 61 72 65 20 63 used if we are c
97190 6f 6d 70 69 6c 69 6e 67 20 6d 75 6c 74 69 74 68 ompiling multith
971a0 72 65 61 64 65 64 0a 2a 2a 20 6f 6e 20 61 20 77 readed.** on a w
971b0 69 6e 33 32 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a in32 system..*/.
971c0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 #ifdef SQLITE_MU
971d0 54 45 58 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a 20 45 TEX_W32../*.** E
971e0 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 ach recursive mu
971f0 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e tex is an instan
97200 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
97210 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
97220 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 /.struct sqlite3
97230 5f 6d 75 74 65 78 20 7b 0a 20 20 43 52 49 54 49 _mutex {. CRITI
97240 43 41 4c 5f 53 45 43 54 49 4f 4e 20 6d 75 74 65 CAL_SECTION mute
97250 78 3b 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 x; /* Mutex c
97260 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c ontrolling the l
97270 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b ock */. int id;
97280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
97290 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 /* Mutex typ
972a0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b e */. int nRef;
972b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
972c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
972d0 6e 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20 20 44 nterances */. D
972e0 57 4f 52 44 20 6f 77 6e 65 72 3b 20 20 20 20 20 WORD owner;
972f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72 /* Thr
97300 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 ead holding this
97310 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a mutex */.};../*
97320 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
97330 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 65 (non-zero) if we
97340 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e 64 are running und
97350 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c er WinNT, Win2K,
97360 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 WinXP,.** or Wi
97370 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 6c nCE. Return fal
97380 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 69 se (zero) for Wi
97390 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 57 n95, Win98, or W
973a0 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 inME..**.** Here
973b0 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 69 is an interesti
973c0 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a 20 ng observation:
973d0 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 61 Win95, Win98, a
973e0 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a nd WinME lack.**
973f0 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 the LockFileEx(
97400 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 63 ) API. But we c
97410 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 61 an still statica
97420 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 lly link against
97430 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 20 that.** API as
97440 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 74 long as we don't
97450 20 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 75 6e call it win run
97460 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 ning Win95/98/ME
97470 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 . A call to.**
97480 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
97490 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e used to determin
974a0 65 20 69 66 20 74 68 65 20 68 6f 73 74 20 69 73 e if the host is
974b0 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a Win95/98/ME or.
974c0 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 ** WinNT/2K/XP s
974d0 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 6b o that we will k
974e0 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e now whether or n
974f0 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65 6c 79 ot we can safely
97500 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 call.** the Loc
97510 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a kFileEx() API..*
97520 2a 0a 2a 2a 20 6d 75 74 65 78 49 73 4e 54 28 29 *.** mutexIsNT()
97530 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f is only used fo
97540 72 20 74 68 65 20 54 72 79 45 6e 74 65 72 43 72 r the TryEnterCr
97550 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 iticalSection()
97560 41 50 49 20 63 61 6c 6c 2c 0a 2a 2a 20 77 68 69 API call,.** whi
97570 63 68 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c ch is only avail
97580 61 62 6c 65 20 69 66 20 79 6f 75 72 20 61 70 70 able if your app
97590 6c 69 63 61 74 69 6f 6e 20 77 61 73 20 63 6f 6d lication was com
975a0 70 69 6c 65 64 20 77 69 74 68 20 0a 2a 2a 20 5f piled with .** _
975b0 57 49 4e 33 32 5f 57 49 4e 4e 54 20 64 65 66 69 WIN32_WINNT defi
975c0 6e 65 64 20 74 6f 20 61 20 76 61 6c 75 65 20 3e ned to a value >
975d0 3d 20 30 78 30 34 30 30 2e 20 20 43 75 72 72 65 = 0x0400. Curre
975e0 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 0a 2a ntly, the only.*
975f0 2a 20 63 61 6c 6c 20 74 6f 20 54 72 79 45 6e 74 * call to TryEnt
97600 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f erCriticalSectio
97610 6e 28 29 20 69 73 20 23 69 66 64 65 66 27 65 64 n() is #ifdef'ed
97620 20 6f 75 74 2c 20 73 6f 20 23 69 66 64 65 66 20 out, so #ifdef
97630 0a 2a 2a 20 74 68 69 73 20 6f 75 74 20 61 73 20 .** this out as
97640 77 65 6c 6c 2e 0a 2a 2f 0a 23 69 66 20 30 0a 23 well..*/.#if 0.#
97650 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
97660 43 45 0a 23 20 64 65 66 69 6e 65 20 6d 75 74 65 CE.# define mute
97670 78 49 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c xIsNT() (1).#el
97680 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 se. static int
97690 6d 75 74 65 78 49 73 4e 54 28 76 6f 69 64 29 7b mutexIsNT(void){
976a0 0a 20 20 20 20 73 74 61 74 69 63 20 69 6e 74 20 . static int
976b0 6f 73 54 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 osType = 0;.
976c0 69 66 28 20 6f 73 54 79 70 65 3d 3d 30 20 29 7b if( osType==0 ){
976d0 0a 20 20 20 20 20 20 4f 53 56 45 52 53 49 4f 4e . OSVERSION
976e0 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20 INFO sInfo;.
976f0 20 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 sInfo.dwOSVers
97700 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 ionInfoSize = si
97710 7a 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20 zeof(sInfo);.
97720 20 20 20 47 65 74 56 65 72 73 69 6f 6e 45 78 28 GetVersionEx(
97730 26 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 6f &sInfo);. o
97740 73 54 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 sType = sInfo.dw
97750 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f PlatformId==VER_
97760 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e PLATFORM_WIN32_N
97770 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d T ? 2 : 1;. }
97780 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 73 54 79 . return osTy
97790 70 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 pe==2;. }.#endi
977a0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 f /* SQLITE_OS_W
977b0 49 4e 43 45 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a INCE */.#endif..
977c0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
977d0 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 BUG./*.** The sq
977e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
977f0 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d () and sqlite3_m
97800 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 utex_notheld() r
97810 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e outine are.** in
97820 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f tended for use o
97830 6e 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 72 nly inside asser
97840 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a t() statements..
97850 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
97860 6e 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 nMutexHeld(sqlit
97870 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
97880 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 21 3d return p->nRef!=
97890 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 0 && p->owner==G
978a0 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 etCurrentThreadI
978b0 64 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e d();.}.static in
978c0 74 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c t winMutexNothel
978d0 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 d(sqlite3_mutex
978e0 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d *p){. return p-
978f0 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f >nRef==0 || p->o
97900 77 6e 65 72 21 3d 47 65 74 43 75 72 72 65 6e 74 wner!=GetCurrent
97910 54 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 23 65 ThreadId();.}.#e
97920 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 ndif.../*.** Ini
97930 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e tialize and dein
97940 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 itialize the mut
97950 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f ex subsystem..*/
97960 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f .static sqlite3_
97970 6d 75 74 65 78 20 77 69 6e 4d 75 74 65 78 5f 73 mutex winMutex_s
97980 74 61 74 69 63 4d 75 74 65 78 65 73 5b 36 5d 3b taticMutexes[6];
97990 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d .static int winM
979a0 75 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 30 3b utex_isInit = 0;
979b0 0a 2f 2a 20 41 73 20 77 69 6e 4d 75 74 65 78 49 ./* As winMutexI
979c0 6e 69 74 28 29 20 61 6e 64 20 77 69 6e 4d 75 74 nit() and winMut
979d0 65 78 45 6e 64 28 29 20 61 72 65 20 63 61 6c 6c exEnd() are call
979e0 65 64 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f 66 ed as part.** of
979f0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 the sqlite3_ini
97a00 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 71 6c 69 tialize and sqli
97a10 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a 2a te3_shutdown().*
97a20 2a 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 74 68 * processing, th
97a30 65 20 22 69 6e 74 65 72 6c 6f 63 6b 65 64 22 20 e "interlocked"
97a40 6d 61 67 69 63 20 69 73 20 70 72 6f 62 61 62 6c magic is probabl
97a50 79 20 6e 6f 74 0a 2a 2a 20 73 74 72 69 63 74 6c y not.** strictl
97a60 79 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a y necessary..*/.
97a70 73 74 61 74 69 63 20 6c 6f 6e 67 20 77 69 6e 4d static long winM
97a80 75 74 65 78 5f 6c 6f 63 6b 20 3d 20 30 3b 0a 0a utex_lock = 0;..
97a90 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 static int winMu
97aa0 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 0a texInit(void){ .
97ab0 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 /* The first t
97ac0 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20 31 o increment to 1
97ad0 20 64 6f 65 73 20 61 63 74 75 61 6c 20 69 6e 69 does actual ini
97ae0 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20 tialization */.
97af0 20 69 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64 if( Interlocked
97b00 43 6f 6d 70 61 72 65 45 78 63 68 61 6e 67 65 28 CompareExchange(
97b10 26 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 2c 20 &winMutex_lock,
97b20 31 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 1, 0)==0 ){.
97b30 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 int i;. for(i
97b40 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 =0; i<ArraySize(
97b50 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d winMutex_staticM
97b60 75 74 65 78 65 73 29 3b 20 69 2b 2b 29 7b 0a 20 utexes); i++){.
97b70 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 43 InitializeC
97b80 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 riticalSection(&
97b90 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d winMutex_staticM
97ba0 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 29 utexes[i].mutex)
97bb0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 69 6e 4d ;. }. winM
97bc0 75 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 31 3b utex_isInit = 1;
97bd0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
97be0 20 53 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 69 73 Someone else is
97bf0 20 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 in the process
97c00 6f 66 20 69 6e 69 74 69 6e 67 20 74 68 65 20 73 of initing the s
97c10 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 2a 2f tatic mutexes */
97c20 0a 20 20 20 20 77 68 69 6c 65 28 20 21 77 69 6e . while( !win
97c30 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 29 7b 0a Mutex_isInit ){.
97c40 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a Sleep(1);.
97c50 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
97c60 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d rn SQLITE_OK; .}
97c70 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e ..static int win
97c80 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 MutexEnd(void){
97c90 0a 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 . /* The first
97ca0 74 6f 20 64 65 63 72 65 6d 65 6e 74 20 74 6f 20 to decrement to
97cb0 30 20 64 6f 65 73 20 61 63 74 75 61 6c 20 73 68 0 does actual sh
97cc0 75 74 64 6f 77 6e 20 0a 20 20 2a 2a 20 28 77 68 utdown . ** (wh
97cd0 69 63 68 20 73 68 6f 75 6c 64 20 62 65 20 74 68 ich should be th
97ce0 65 20 6c 61 73 74 20 74 6f 20 73 68 75 74 64 6f e last to shutdo
97cf0 77 6e 2e 29 20 2a 2f 0a 20 20 69 66 28 20 49 6e wn.) */. if( In
97d00 74 65 72 6c 6f 63 6b 65 64 43 6f 6d 70 61 72 65 terlockedCompare
97d10 45 78 63 68 61 6e 67 65 28 26 77 69 6e 4d 75 74 Exchange(&winMut
97d20 65 78 5f 6c 6f 63 6b 2c 20 30 2c 20 31 29 3d 3d ex_lock, 0, 1)==
97d30 31 20 29 7b 0a 20 20 20 20 69 66 28 20 77 69 6e 1 ){. if( win
97d40 4d 75 74 65 78 5f 69 73 49 6e 69 74 3d 3d 31 20 Mutex_isInit==1
97d50 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a ){. int i;.
97d60 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 for(i=0; i
97d70 3c 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d 75 <ArraySize(winMu
97d80 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 tex_staticMutexe
97d90 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 s); i++){.
97da0 20 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c DeleteCritical
97db0 53 65 63 74 69 6f 6e 28 26 77 69 6e 4d 75 74 65 Section(&winMute
97dc0 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b x_staticMutexes[
97dd0 69 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 i].mutex);.
97de0 20 7d 0a 20 20 20 20 20 20 77 69 6e 4d 75 74 65 }. winMute
97df0 78 5f 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 x_isInit = 0;.
97e00 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
97e10 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a SQLITE_OK; .}..
97e20 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 /*.** The sqlite
97e30 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 3_mutex_alloc()
97e40 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 routine allocate
97e50 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 s a new.** mutex
97e60 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 and returns a p
97e70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 ointer to it. I
97e80 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c f it returns NUL
97e90 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 L.** that means
97ea0 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 that a mutex cou
97eb0 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 ld not be alloca
97ec0 74 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 ted. SQLite.**
97ed0 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 will unwind its
97ee0 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e stack and return
97ef0 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 an error. The
97f00 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 argument.** to s
97f10 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
97f20 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 oc() is one of t
97f30 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e hese integer con
97f40 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 stants:.**.** <u
97f50 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 l>.** <li> SQLI
97f60 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a TE_MUTEX_FAST.**
97f70 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
97f80 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a TEX_RECURSIVE.**
97f90 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
97fa0 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
97fb0 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 R.** <li> SQLIT
97fc0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
97fd0 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 EM.** <li> SQLI
97fe0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
97ff0 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 MEM2.** <li> SQ
98000 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
98010 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 C_PRNG.** <li>
98020 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
98030 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 TIC_LRU.** <li>
98040 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
98050 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75 ATIC_LRU2.** </u
98060 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 l>.**.** The fir
98070 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 st two constants
98080 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d cause sqlite3_m
98090 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 utex_alloc() to
980a0 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 create.** a new
980b0 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 mutex. The new
980c0 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 mutex is recursi
980d0 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d ve when SQLITE_M
980e0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a UTEX_RECURSIVE.*
980f0 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f * is used but no
98100 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f t necessarily so
98110 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 when SQLITE_MUT
98120 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e EX_FAST is used.
98130 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d .** The mutex im
98140 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 plementation doe
98150 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 s not need to ma
98160 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e ke a distinction
98170 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 .** between SQLI
98180 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
98190 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 VE and SQLITE_MU
981a0 54 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 TEX_FAST if it d
981b0 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 oes.** not want
981c0 74 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 to. But SQLite
981d0 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 will only reques
981e0 74 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 t a recursive mu
981f0 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 tex in.** cases
98200 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 where it really
98210 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 needs one. If a
98220 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 faster non-recu
98230 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 rsive mutex.** i
98240 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 mplementation is
98250 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 available on th
98260 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c e host platform,
98270 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 the mutex subsy
98280 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 stem.** might re
98290 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 turn such a mute
982a0 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f x in response to
982b0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
982c0 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 ST..**.** The ot
982d0 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 her allowed para
982e0 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 meters to sqlite
982f0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 3_mutex_alloc()
98300 65 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 each return.** a
98310 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 pointer to a st
98320 61 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 atic preexisting
98330 20 6d 75 74 65 78 2e 20 20 53 69 78 20 73 74 61 mutex. Six sta
98340 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a tic mutexes are.
98350 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 ** used by the c
98360 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f urrent version o
98370 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 f SQLite. Futur
98380 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 e versions of SQ
98390 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 Lite.** may add
983a0 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 additional stati
983b0 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 c mutexes. Stat
983c0 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 ic mutexes are f
983d0 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 or internal.** u
983e0 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c se by SQLite onl
983f0 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 y. Applications
98400 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 that use SQLite
98410 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a mutexes should.
98420 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 ** use only the
98430 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 dynamic mutexes
98440 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 returned by SQLI
98450 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 TE_MUTEX_FAST or
98460 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 .** SQLITE_MUTEX
98470 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a _RECURSIVE..**.*
98480 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f * Note that if o
98490 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 ne of the dynami
984a0 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 c mutex paramete
984b0 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 rs (SQLITE_MUTEX
984c0 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 _FAST.** or SQLI
984d0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
984e0 56 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e VE) is used then
984f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
98500 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e lloc().** return
98510 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 s a different mu
98520 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c tex on every cal
98530 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 l. But for the
98540 73 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 static .** mutex
98550 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 types, the same
98560 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e mutex is return
98570 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c ed on every call
98580 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 that has.** the
98590 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 same type numbe
985a0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c r..*/.static sql
985b0 69 74 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e 4d ite3_mutex *winM
985c0 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54 utexAlloc(int iT
985d0 79 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f ype){. sqlite3_
985e0 6d 75 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77 69 mutex *p;.. swi
985f0 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20 tch( iType ){.
98600 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 case SQLITE_MU
98610 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 TEX_FAST:. ca
98620 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f se SQLITE_MUTEX_
98630 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 RECURSIVE: {.
98640 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 p = sqlite3Ma
98650 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 llocZero( sizeof
98660 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 (*p) );. if
98670 28 20 70 20 29 7b 20 20 0a 20 20 20 20 20 20 20 ( p ){ .
98680 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a p->id = iType;.
98690 20 20 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 Initiali
986a0 7a 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f zeCriticalSectio
986b0 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 n(&p->mutex);.
986c0 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
986d0 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 k;. }. def
986e0 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 ault: {. as
986f0 73 65 72 74 28 20 77 69 6e 4d 75 74 65 78 5f 69 sert( winMutex_i
98700 73 49 6e 69 74 3d 3d 31 20 29 3b 0a 20 20 20 20 sInit==1 );.
98710 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d assert( iType-
98720 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 2 >= 0 );.
98730 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 assert( iType-2
98740 3c 20 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d < ArraySize(winM
98750 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 utex_staticMutex
98760 65 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d es) );. p =
98770 20 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 &winMutex_stati
98780 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32 cMutexes[iType-2
98790 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d ];. p->id =
987a0 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72 iType;. br
987b0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
987c0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f return p;.}.../
987d0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
987e0 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 e deallocates a
987f0 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c previously.** al
98800 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20 located mutex.
98810 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 SQLite is carefu
98820 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 l to deallocate
98830 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 every.** mutex t
98840 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 hat it allocates
98850 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
98860 20 77 69 6e 4d 75 74 65 78 46 72 65 65 28 73 71 winMutexFree(sq
98870 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
98880 0a 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a . assert( p );.
98890 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
988a0 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 f==0 );. assert
988b0 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f ( p->id==SQLITE_
988c0 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d MUTEX_FAST || p-
988d0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
988e0 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 X_RECURSIVE );.
988f0 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c 53 DeleteCriticalS
98900 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 ection(&p->mutex
98910 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 );. sqlite3_fre
98920 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 e(p);.}../*.** T
98930 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
98940 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c _enter() and sql
98950 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
98960 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 routines attemp
98970 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 t.** to enter a
98980 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 mutex. If anoth
98990 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 er thread is alr
989a0 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 eady within the
989b0 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 mutex,.** sqlite
989c0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 3_mutex_enter()
989d0 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 will block and s
989e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
989f0 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a () will return.*
98a00 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 * SQLITE_BUSY.
98a10 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
98a20 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 x_try() interfac
98a30 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 e returns SQLITE
98a40 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 _OK.** upon succ
98a50 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d essful entry. M
98a60 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 utexes created u
98a70 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 sing SQLITE_MUTE
98a80 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a X_RECURSIVE can.
98a90 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 ** be entered mu
98aa0 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 ltiple times by
98ab0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e the same thread.
98ac0 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 In such cases
98ad0 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 the,.** mutex mu
98ae0 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 st be exited an
98af0 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 equal number of
98b00 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f times before ano
98b10 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 ther thread.** c
98b20 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 an enter. If th
98b30 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 e same thread tr
98b40 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 ies to enter any
98b50 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d other kind of m
98b60 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 utex.** more tha
98b70 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 n once, the beha
98b80 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 vior is undefine
98b90 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
98ba0 64 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 28 d winMutexEnter(
98bb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
98bc0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e ){. assert( p->
98bd0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
98be0 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77 69 _RECURSIVE || wi
98bf0 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 nMutexNotheld(p)
98c00 20 29 3b 0a 20 20 45 6e 74 65 72 43 72 69 74 69 );. EnterCriti
98c10 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d calSection(&p->m
98c20 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 utex);. p->owne
98c30 72 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 r = GetCurrentTh
98c40 72 65 61 64 49 64 28 29 3b 20 0a 20 20 70 2d 3e readId(); . p->
98c50 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 nRef++;.}.static
98c60 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 54 72 79 int winMutexTry
98c70 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
98c80 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 p){. int rc = S
98c90 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61 73 QLITE_BUSY;. as
98ca0 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c sert( p->id==SQL
98cb0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
98cc0 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 4e IVE || winMutexN
98cd0 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 2f otheld(p) );. /
98ce0 2a 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 *. ** The sqlit
98cf0 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 e3_mutex_try() r
98d00 6f 75 74 69 6e 65 20 69 73 20 76 65 72 79 20 72 outine is very r
98d10 61 72 65 6c 79 20 75 73 65 64 2c 20 61 6e 64 20 arely used, and
98d20 77 68 65 6e 20 69 74 0a 20 20 2a 2a 20 69 73 20 when it. ** is
98d30 75 73 65 64 20 69 74 20 69 73 20 6d 65 72 65 6c used it is merel
98d40 79 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f y an optimizatio
98d50 6e 2e 20 20 53 6f 20 69 74 20 69 73 20 4f 4b 20 n. So it is OK
98d60 66 6f 72 20 69 74 20 74 6f 20 61 6c 77 61 79 73 for it to always
98d70 0a 20 20 2a 2a 20 66 61 69 6c 2e 20 20 0a 20 20 . ** fail. .
98d80 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 54 72 79 45 **. ** The TryE
98d90 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 nterCriticalSect
98da0 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 ion() interface
98db0 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c is only availabl
98dc0 65 20 6f 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a 2a e on WinNT.. **
98dd0 20 41 6e 64 20 73 6f 6d 65 20 77 69 6e 64 6f 77 And some window
98de0 73 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 s compilers comp
98df0 6c 61 69 6e 20 69 66 20 79 6f 75 20 74 72 79 20 lain if you try
98e00 74 6f 20 75 73 65 20 69 74 20 77 69 74 68 6f 75 to use it withou
98e10 74 0a 20 20 2a 2a 20 66 69 72 73 74 20 64 6f 69 t. ** first doi
98e20 6e 67 20 73 6f 6d 65 20 23 64 65 66 69 6e 65 73 ng some #defines
98e30 20 74 68 61 74 20 70 72 65 76 65 6e 74 20 53 51 that prevent SQ
98e40 4c 69 74 65 20 66 72 6f 6d 20 62 75 69 6c 64 69 Lite from buildi
98e50 6e 67 20 6f 6e 20 57 69 6e 39 38 2e 0a 20 20 2a ng on Win98.. *
98e60 2a 20 46 6f 72 20 74 68 61 74 20 72 65 61 73 6f * For that reaso
98e70 6e 2c 20 77 65 20 77 69 6c 6c 20 6f 6d 69 74 20 n, we will omit
98e80 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f this optimizatio
98e90 6e 20 66 6f 72 20 6e 6f 77 2e 20 20 53 65 65 0a n for now. See.
98ea0 20 20 2a 2a 20 74 69 63 6b 65 74 20 23 32 36 38 ** ticket #268
98eb0 35 2e 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20 20 5.. */.#if 0.
98ec0 69 66 28 20 6d 75 74 65 78 49 73 4e 54 28 29 20 if( mutexIsNT()
98ed0 26 26 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 && TryEnterCriti
98ee0 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d calSection(&p->m
98ef0 75 74 65 78 29 20 29 7b 0a 20 20 20 20 70 2d 3e utex) ){. p->
98f00 6f 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 65 owner = GetCurre
98f10 6e 74 54 68 72 65 61 64 49 64 28 29 3b 0a 20 20 ntThreadId();.
98f20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 p->nRef++;.
98f30 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
98f40 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 . }.#else. UNU
98f50 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 29 SED_PARAMETER(p)
98f60 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 ;.#endif. retur
98f70 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
98f80 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
98f90 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 _leave() routine
98fa0 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 exits a mutex t
98fb0 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 hat was.** previ
98fc0 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 ously entered by
98fd0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
98fe0 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a . The behavior.
98ff0 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 ** is undefined
99000 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 if the mutex is
99010 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e not currently en
99020 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e tered or.** is n
99030 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c ot currently all
99040 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 ocated. SQLite
99050 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 will never do ei
99060 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ther..*/.static
99070 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c 65 61 void winMutexLea
99080 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ve(sqlite3_mutex
99090 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
990a0 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 61 p->nRef>0 );. a
990b0 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d ssert( p->owner=
990c0 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 =GetCurrentThrea
990d0 64 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 dId() );. p->nR
990e0 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 ef--;. assert(
990f0 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d p->nRef==0 || p-
99100 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
99110 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 X_RECURSIVE );.
99120 20 4c 65 61 76 65 43 72 69 74 69 63 61 6c 53 65 LeaveCriticalSe
99130 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 ction(&p->mutex)
99140 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 ;.}..SQLITE_PRIV
99150 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ATE sqlite3_mute
99160 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 x_methods *sqlit
99170 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 e3DefaultMutex(v
99180 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 oid){. static s
99190 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
991a0 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a hods sMutex = {.
991b0 20 20 20 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 winMutexInit
991c0 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e ,. winMutexEn
991d0 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 41 d,. winMutexA
991e0 6c 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d 75 74 lloc,. winMut
991f0 65 78 46 72 65 65 2c 0a 20 20 20 20 77 69 6e 4d exFree,. winM
99200 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 77 utexEnter,. w
99210 69 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 inMutexTry,.
99220 77 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 winMutexLeave,.#
99230 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
99240 55 47 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 48 UG. winMutexH
99250 65 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 eld,. winMute
99260 78 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 xNotheld.#else.
99270 20 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 0,. 0.#end
99280 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 if. };.. retur
99290 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e n &sMutex;.}.#en
992a0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 dif /* SQLITE_MU
992b0 54 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a 2a TEX_W32 */../***
992c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
992d0 6f 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a of mutex_w32.c *
992e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
992f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
99310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
99320 6e 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20 n file malloc.c
99330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
99360 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 * 2001 September
99370 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 15.**.** The au
99380 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
99390 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
993a0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
993b0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
993c0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
993d0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
993e0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
993f0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
99400 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
99410 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
99420 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
99430 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
99440 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
99450 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
99460 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
99470 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
99480 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
99490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
994a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
994b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
994c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
994d0 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 **.**.** Memory
994e0 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 allocation funct
994f0 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 67 ions used throug
99500 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a hout sqlite..**.
99510 2a 2a 20 24 49 64 3a 20 6d 61 6c 6c 6f 63 2e 63 ** $Id: malloc.c
99520 2c 76 20 31 2e 36 36 20 32 30 30 39 2f 30 37 2f ,v 1.66 2009/07/
99530 31 37 20 31 31 3a 34 34 3a 30 37 20 64 72 68 20 17 11:44:07 drh
99540 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Exp $.*/../*.**
99550 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e This routine run
99560 73 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f 72 s when the memor
99570 79 20 61 6c 6c 6f 63 61 74 6f 72 20 73 65 65 73 y allocator sees
99580 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 74 6f 74 that the.** tot
99590 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 al memory alloca
995a0 74 69 6f 6e 20 69 73 20 61 62 6f 75 74 20 74 6f tion is about to
995b0 20 65 78 63 65 65 64 20 74 68 65 20 73 6f 66 74 exceed the soft
995c0 20 68 65 61 70 0a 2a 2a 20 6c 69 6d 69 74 2e 0a heap.** limit..
995d0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 */.static void s
995e0 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f oftHeapLimitEnfo
995f0 72 63 65 72 28 0a 20 20 76 6f 69 64 20 2a 4e 6f rcer(. void *No
99600 74 55 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 65 tUsed, . sqlite
99610 33 5f 69 6e 74 36 34 20 4e 6f 74 55 73 65 64 32 3_int64 NotUsed2
99620 2c 0a 20 20 69 6e 74 20 61 6c 6c 6f 63 53 69 7a ,. int allocSiz
99630 65 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 e.){. UNUSED_PA
99640 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 RAMETER2(NotUsed
99650 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 , NotUsed2);. s
99660 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d qlite3_release_m
99670 65 6d 6f 72 79 28 61 6c 6c 6f 63 53 69 7a 65 29 emory(allocSize)
99680 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 ;.}../*.** Set t
99690 68 65 20 73 6f 66 74 20 68 65 61 70 2d 73 69 7a he soft heap-siz
996a0 65 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 65 20 e limit for the
996b0 6c 69 62 72 61 72 79 2e 20 50 61 73 73 69 6e 67 library. Passing
996c0 20 61 20 7a 65 72 6f 20 6f 72 20 0a 2a 2a 20 6e a zero or .** n
996d0 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 69 6e egative value in
996e0 64 69 63 61 74 65 73 20 6e 6f 20 6c 69 6d 69 74 dicates no limit
996f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
99700 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 6f 66 void sqlite3_sof
99710 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69 6e 74 t_heap_limit(int
99720 20 6e 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 75 n){. sqlite3_u
99730 69 6e 74 36 34 20 69 4c 69 6d 69 74 3b 0a 20 20 int64 iLimit;.
99740 69 6e 74 20 6f 76 65 72 61 67 65 3b 0a 20 20 69 int overage;. i
99750 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 69 4c f( n<0 ){. iL
99760 69 6d 69 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 imit = 0;. }els
99770 65 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 e{. iLimit =
99780 6e 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 n;. }.#ifndef S
99790 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
997a0 4e 49 54 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e NIT. sqlite3_in
997b0 69 74 69 61 6c 69 7a 65 28 29 3b 0a 23 65 6e 64 itialize();.#end
997c0 69 66 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3e if. if( iLimit>
997d0 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
997e0 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 73 6f 66 74 MemoryAlarm(soft
997f0 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65 HeapLimitEnforce
99800 72 2c 20 30 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 r, 0, iLimit);.
99810 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 }else{. sqli
99820 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 30 te3MemoryAlarm(0
99830 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 6f , 0, 0);. }. o
99840 76 65 72 61 67 65 20 3d 20 28 69 6e 74 29 28 73 verage = (int)(s
99850 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 qlite3_memory_us
99860 65 64 28 29 20 2d 20 28 69 36 34 29 6e 29 3b 0a ed() - (i64)n);.
99870 20 20 69 66 28 20 6f 76 65 72 61 67 65 3e 30 20 if( overage>0
99880 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 ){. sqlite3_r
99890 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 6f 76 elease_memory(ov
998a0 65 72 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f erage);. }.}../
998b0 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 *.** Attempt to
998c0 72 65 6c 65 61 73 65 20 75 70 20 74 6f 20 6e 20 release up to n
998d0 62 79 74 65 73 20 6f 66 20 6e 6f 6e 2d 65 73 73 bytes of non-ess
998e0 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 63 75 ential memory cu
998f0 72 72 65 6e 74 6c 79 0a 2a 2a 20 68 65 6c 64 20 rrently.** held
99900 62 79 20 53 51 4c 69 74 65 2e 20 41 6e 20 65 78 by SQLite. An ex
99910 61 6d 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 ample of non-ess
99920 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 69 73 ential memory is
99930 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 0a memory used to.
99940 2a 2a 20 63 61 63 68 65 20 64 61 74 61 62 61 73 ** cache databas
99950 65 20 70 61 67 65 73 20 74 68 61 74 20 61 72 65 e pages that are
99960 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 69 not currently i
99970 6e 20 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 n use..*/.SQLITE
99980 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
99990 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 _release_memory(
999a0 69 6e 74 20 6e 29 7b 0a 23 69 66 64 65 66 20 53 int n){.#ifdef S
999b0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
999c0 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 ORY_MANAGEMENT.
999d0 20 69 6e 74 20 6e 52 65 74 20 3d 20 30 3b 0a 23 int nRet = 0;.#
999e0 69 66 20 30 0a 20 20 6e 52 65 74 20 2b 3d 20 73 if 0. nRet += s
999f0 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 qlite3VdbeReleas
99a00 65 4d 65 6d 6f 72 79 28 6e 29 3b 0a 23 65 6e 64 eMemory(n);.#end
99a10 69 66 0a 20 20 6e 52 65 74 20 2b 3d 20 73 71 6c if. nRet += sql
99a20 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 ite3PcacheReleas
99a30 65 4d 65 6d 6f 72 79 28 6e 2d 6e 52 65 74 29 3b eMemory(n-nRet);
99a40 0a 20 20 72 65 74 75 72 6e 20 6e 52 65 74 3b 0a . return nRet;.
99a50 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 #else. UNUSED_P
99a60 41 52 41 4d 45 54 45 52 28 6e 29 3b 0a 20 20 72 ARAMETER(n);. r
99a70 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
99a80 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
99a90 20 53 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 State informati
99aa0 6f 6e 20 6c 6f 63 61 6c 20 74 6f 20 74 68 65 20 on local to the
99ab0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
99ac0 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a n subsystem..*/.
99ad0 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 static SQLITE_WS
99ae0 44 20 73 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f D struct Mem0Glo
99af0 62 61 6c 20 7b 0a 20 20 2f 2a 20 4e 75 6d 62 65 bal {. /* Numbe
99b00 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 r of free pages
99b10 66 6f 72 20 73 63 72 61 74 63 68 20 61 6e 64 20 for scratch and
99b20 70 61 67 65 2d 63 61 63 68 65 20 6d 65 6d 6f 72 page-cache memor
99b30 79 20 2a 2f 0a 20 20 75 33 32 20 6e 53 63 72 61 y */. u32 nScra
99b40 74 63 68 46 72 65 65 3b 0a 20 20 75 33 32 20 6e tchFree;. u32 n
99b50 50 61 67 65 46 72 65 65 3b 0a 0a 20 20 73 71 6c PageFree;.. sql
99b60 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
99b70 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 x; /* Mu
99b80 74 65 78 20 74 6f 20 73 65 72 69 61 6c 69 7a 65 tex to serialize
99b90 20 61 63 63 65 73 73 20 2a 2f 0a 0a 20 20 2f 2a access */.. /*
99ba0 0a 20 20 2a 2a 20 54 68 65 20 61 6c 61 72 6d 20 . ** The alarm
99bb0 63 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69 74 73 callback and its
99bc0 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 arguments. The
99bd0 20 6d 65 6d 30 2e 6d 75 74 65 78 20 6c 6f 63 6b mem0.mutex lock
99be0 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 68 65 will. ** be he
99bf0 6c 64 20 77 68 69 6c 65 20 74 68 65 20 63 61 6c ld while the cal
99c00 6c 62 61 63 6b 20 69 73 20 72 75 6e 6e 69 6e 67 lback is running
99c10 2e 20 20 52 65 63 75 72 73 69 76 65 20 63 61 6c . Recursive cal
99c20 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 ls into. ** the
99c30 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 memory subsyste
99c40 6d 20 61 72 65 20 61 6c 6c 6f 77 65 64 2c 20 62 m are allowed, b
99c50 75 74 20 6e 6f 20 6e 65 77 20 63 61 6c 6c 62 61 ut no new callba
99c60 63 6b 73 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a cks will be. **
99c70 20 69 73 73 75 65 64 2e 0a 20 20 2a 2f 0a 20 20 issued.. */.
99c80 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 61 6c sqlite3_int64 al
99c90 61 72 6d 54 68 72 65 73 68 6f 6c 64 3b 0a 20 20 armThreshold;.
99ca0 76 6f 69 64 20 28 2a 61 6c 61 72 6d 43 61 6c 6c void (*alarmCall
99cb0 62 61 63 6b 29 28 76 6f 69 64 2a 2c 20 73 71 6c back)(void*, sql
99cc0 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 29 3b ite3_int64,int);
99cd0 0a 20 20 76 6f 69 64 20 2a 61 6c 61 72 6d 41 72 . void *alarmAr
99ce0 67 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 6f g;.. /*. ** Po
99cf0 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 65 6e inters to the en
99d00 64 20 6f 66 20 73 71 6c 69 74 65 33 47 6c 6f 62 d of sqlite3Glob
99d10 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 alConfig.pScratc
99d20 68 20 61 6e 64 0a 20 20 2a 2a 20 73 71 6c 69 74 h and. ** sqlit
99d30 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
99d40 50 61 67 65 20 74 6f 20 61 20 62 6c 6f 63 6b 20 Page to a block
99d50 6f 66 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 72 of memory that r
99d60 65 63 6f 72 64 73 0a 20 20 2a 2a 20 77 68 69 63 ecords. ** whic
99d70 68 20 70 61 67 65 73 20 61 72 65 20 61 76 61 69 h pages are avai
99d80 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 75 33 lable.. */. u3
99d90 32 20 2a 61 53 63 72 61 74 63 68 46 72 65 65 3b 2 *aScratchFree;
99da0 0a 20 20 75 33 32 20 2a 61 50 61 67 65 46 72 65 . u32 *aPageFre
99db0 65 3b 0a 7d 20 6d 65 6d 30 20 3d 20 7b 20 30 2c e;.} mem0 = { 0,
99dc0 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
99dd0 30 2c 20 30 20 7d 3b 0a 0a 23 64 65 66 69 6e 65 0, 0 };..#define
99de0 20 6d 65 6d 30 20 47 4c 4f 42 41 4c 28 73 74 72 mem0 GLOBAL(str
99df0 75 63 74 20 4d 65 6d 30 47 6c 6f 62 61 6c 2c 20 uct Mem0Global,
99e00 6d 65 6d 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 mem0)../*.** Ini
99e10 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f tialize the memo
99e20 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ry allocation su
99e30 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 bsystem..*/.SQLI
99e40 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
99e50 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 qlite3MallocInit
99e60 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 (void){. if( sq
99e70 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
99e80 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29 g.m.xMalloc==0 )
99e90 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d {. sqlite3Mem
99ea0 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 SetDefault();.
99eb0 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d 65 6d 30 }. memset(&mem0
99ec0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65 6d 30 , 0, sizeof(mem0
99ed0 29 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 ));. if( sqlite
99ee0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 3GlobalConfig.bC
99ef0 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 oreMutex ){.
99f00 6d 65 6d 30 2e 6d 75 74 65 78 20 3d 20 73 71 6c mem0.mutex = sql
99f10 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
99f20 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
99f30 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 69 IC_MEM);. }. i
99f40 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c f( sqlite3Global
99f50 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 20 Config.pScratch
99f60 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c && sqlite3Global
99f70 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 Config.szScratch
99f80 3e 3d 31 30 30 0a 20 20 20 20 20 20 26 26 20 73 >=100. && s
99f90 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
99fa0 69 67 2e 6e 53 63 72 61 74 63 68 3e 3d 30 20 29 ig.nScratch>=0 )
99fb0 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 {. int i;.
99fc0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
99fd0 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 20 3d nfig.szScratch =
99fe0 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 71 6c 69 ROUNDDOWN8(sqli
99ff0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9a000 73 7a 53 63 72 61 74 63 68 2d 34 29 3b 0a 20 20 szScratch-4);.
9a010 20 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 mem0.aScratchF
9a020 72 65 65 20 3d 20 28 75 33 32 2a 29 26 28 28 63 ree = (u32*)&((c
9a030 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 har*)sqlite3Glob
9a040 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 alConfig.pScratc
9a050 68 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 h).
9a060 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c 6f [sqlite3Glo
9a070 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 balConfig.szScra
9a080 74 63 68 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 tch*sqlite3Globa
9a090 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 lConfig.nScratch
9a0a0 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 ];. for(i=0;
9a0b0 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 i<sqlite3GlobalC
9a0c0 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 3b 20 onfig.nScratch;
9a0d0 69 2b 2b 29 7b 20 6d 65 6d 30 2e 61 53 63 72 61 i++){ mem0.aScra
9a0e0 74 63 68 46 72 65 65 5b 69 5d 20 3d 20 69 3b 20 tchFree[i] = i;
9a0f0 7d 0a 20 20 20 20 6d 65 6d 30 2e 6e 53 63 72 61 }. mem0.nScra
9a100 74 63 68 46 72 65 65 20 3d 20 73 71 6c 69 74 65 tchFree = sqlite
9a110 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 3GlobalConfig.nS
9a120 63 72 61 74 63 68 3b 0a 20 20 7d 65 6c 73 65 7b cratch;. }else{
9a130 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
9a140 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 alConfig.pScratc
9a150 68 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 h = 0;. sqlit
9a160 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 e3GlobalConfig.s
9a170 7a 53 63 72 61 74 63 68 20 3d 20 30 3b 0a 20 20 zScratch = 0;.
9a180 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 }. if( sqlite3G
9a190 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 lobalConfig.pPag
9a1a0 65 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 e && sqlite3Glob
9a1b0 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3e alConfig.szPage>
9a1c0 3d 35 31 32 0a 20 20 20 20 20 20 26 26 20 73 71 =512. && sq
9a1d0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9a1e0 67 2e 6e 50 61 67 65 3e 3d 31 20 29 7b 0a 20 20 g.nPage>=1 ){.
9a1f0 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74 int i;. int
9a200 20 6f 76 65 72 68 65 61 64 3b 0a 20 20 20 20 69 overhead;. i
9a210 6e 74 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 nt sz = ROUNDDOW
9a220 4e 38 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c N8(sqlite3Global
9a230 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 29 3b 0a Config.szPage);.
9a240 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 int n = sqli
9a250 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9a260 6e 50 61 67 65 3b 0a 20 20 20 20 6f 76 65 72 68 nPage;. overh
9a270 65 61 64 20 3d 20 28 34 2a 6e 20 2b 20 73 7a 20 ead = (4*n + sz
9a280 2d 20 31 29 2f 73 7a 3b 0a 20 20 20 20 73 71 6c - 1)/sz;. sql
9a290 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9a2a0 2e 6e 50 61 67 65 20 2d 3d 20 6f 76 65 72 68 65 .nPage -= overhe
9a2b0 61 64 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 50 61 ad;. mem0.aPa
9a2c0 67 65 46 72 65 65 20 3d 20 28 75 33 32 2a 29 26 geFree = (u32*)&
9a2d0 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47 ((char*)sqlite3G
9a2e0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 lobalConfig.pPag
9a2f0 65 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e).
9a300 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c 6f [sqlite3Glo
9a310 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 balConfig.szPage
9a320 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f *sqlite3GlobalCo
9a330 6e 66 69 67 2e 6e 50 61 67 65 5d 3b 0a 20 20 20 nfig.nPage];.
9a340 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c 69 for(i=0; i<sqli
9a350 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9a360 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 6d 65 6d nPage; i++){ mem
9a370 30 2e 61 50 61 67 65 46 72 65 65 5b 69 5d 20 3d 0.aPageFree[i] =
9a380 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e 6e i; }. mem0.n
9a390 50 61 67 65 46 72 65 65 20 3d 20 73 71 6c 69 74 PageFree = sqlit
9a3a0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
9a3b0 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Page;. }else{.
9a3c0 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
9a3d0 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20 30 Config.pPage = 0
9a3e0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f ;. sqlite3Glo
9a3f0 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 balConfig.szPage
9a400 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 0;. }. retu
9a410 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c rn sqlite3Global
9a420 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69 74 28 73 Config.m.xInit(s
9a430 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9a440 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 29 3b 0a ig.m.pAppData);.
9a450 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 }../*.** Deiniti
9a460 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 alize the memory
9a470 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
9a480 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ystem..*/.SQLITE
9a490 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
9a4a0 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 76 lite3MallocEnd(v
9a4b0 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 oid){. if( sqli
9a4c0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9a4d0 6d 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20 m.xShutdown ){.
9a4e0 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
9a4f0 43 6f 6e 66 69 67 2e 6d 2e 78 53 68 75 74 64 6f Config.m.xShutdo
9a500 77 6e 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c wn(sqlite3Global
9a510 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 Config.m.pAppDat
9a520 61 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 a);. }. memset
9a530 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f (&mem0, 0, sizeo
9a540 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a f(mem0));.}../*.
9a550 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d ** Return the am
9a560 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 ount of memory c
9a570 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 urrently checked
9a580 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f out..*/.SQLITE_
9a590 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 API sqlite3_int6
9a5a0 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 4 sqlite3_memory
9a5b0 5f 75 73 65 64 28 76 6f 69 64 29 7b 0a 20 20 69 _used(void){. i
9a5c0 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 6c 69 nt n, mx;. sqli
9a5d0 74 65 33 5f 69 6e 74 36 34 20 72 65 73 3b 0a 20 te3_int64 res;.
9a5e0 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 sqlite3_status(
9a5f0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 SQLITE_STATUS_ME
9a600 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c 20 26 MORY_USED, &n, &
9a610 6d 78 2c 20 30 29 3b 0a 20 20 72 65 73 20 3d 20 mx, 0);. res =
9a620 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 6e (sqlite3_int64)n
9a630 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75 6e ; /* Work aroun
9a640 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e 64 d bug in Borland
9a650 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32 31 36 C. Ticket #3216
9a660 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 65 73 */. return res
9a670 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
9a680 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d n the maximum am
9a690 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 74 ount of memory t
9a6a0 68 61 74 20 68 61 73 20 65 76 65 72 20 62 65 65 hat has ever bee
9a6b0 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 n.** checked out
9a6c0 20 73 69 6e 63 65 20 65 69 74 68 65 72 20 74 68 since either th
9a6d0 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 e beginning of t
9a6e0 68 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a 20 6f his process.** o
9a6f0 72 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 r since the most
9a700 20 72 65 63 65 6e 74 20 72 65 73 65 74 2e 0a 2a recent reset..*
9a710 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c /.SQLITE_API sql
9a720 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 ite3_int64 sqlit
9a730 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 e3_memory_highwa
9a740 74 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61 ter(int resetFla
9a750 67 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 3b g){. int n, mx;
9a760 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
9a770 20 72 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f res;. sqlite3_
9a780 73 74 61 74 75 73 28 53 51 4c 49 54 45 5f 53 54 status(SQLITE_ST
9a790 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 ATUS_MEMORY_USED
9a7a0 2c 20 26 6e 2c 20 26 6d 78 2c 20 72 65 73 65 74 , &n, &mx, reset
9a7b0 46 6c 61 67 29 3b 0a 20 20 72 65 73 20 3d 20 28 Flag);. res = (
9a7c0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 6d 78 sqlite3_int64)mx
9a7d0 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75 6e ; /* Work aroun
9a7e0 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e 64 d bug in Borland
9a7f0 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32 31 36 C. Ticket #3216
9a800 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 65 73 */. return res
9a810 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
9a820 65 20 74 68 65 20 61 6c 61 72 6d 20 63 61 6c 6c e the alarm call
9a830 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 back.*/.SQLITE_P
9a840 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
9a850 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 0a 20 e3MemoryAlarm(.
9a860 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b void(*xCallback
9a870 29 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 )(void *pArg, sq
9a880 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 73 65 64 lite3_int64 used
9a890 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 ,int N),. void
9a8a0 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 *pArg,. sqlite3
9a8b0 5f 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f 6c _int64 iThreshol
9a8c0 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d d.){. sqlite3_m
9a8d0 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e utex_enter(mem0.
9a8e0 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30 2e 61 mutex);. mem0.a
9a8f0 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 78 larmCallback = x
9a900 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 6d 30 Callback;. mem0
9a910 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41 72 67 .alarmArg = pArg
9a920 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 ;. mem0.alarmTh
9a930 72 65 73 68 6f 6c 64 20 3d 20 69 54 68 72 65 73 reshold = iThres
9a940 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f hold;. sqlite3_
9a950 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 mutex_leave(mem0
9a960 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 .mutex);. retur
9a970 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
9a980 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
9a990 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f MIT_DEPRECATED./
9a9a0 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 64 20 *.** Deprecated
9a9b0 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61 external interfa
9a9c0 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f 63 6f ce. Internal/co
9a9d0 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65 0a 2a re SQLite code.*
9a9e0 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71 * should call sq
9a9f0 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d lite3MemoryAlarm
9aa00 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
9aa10 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f int sqlite3_memo
9aa20 72 79 5f 61 6c 61 72 6d 28 0a 20 20 76 6f 69 64 ry_alarm(. void
9aa30 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 (*xCallback)(voi
9aa40 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 65 33 d *pArg, sqlite3
9aa50 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e 74 20 _int64 used,int
9aa60 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 N),. void *pArg
9aa70 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 ,. sqlite3_int6
9aa80 34 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 7b 0a 4 iThreshold.){.
9aa90 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
9aaa0 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 78 43 61 6c MemoryAlarm(xCal
9aab0 6c 62 61 63 6b 2c 20 70 41 72 67 2c 20 69 54 68 lback, pArg, iTh
9aac0 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a 23 65 6e 64 reshold);.}.#end
9aad0 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67 67 65 if../*.** Trigge
9aae0 72 20 74 68 65 20 61 6c 61 72 6d 20 0a 2a 2f 0a r the alarm .*/.
9aaf0 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 static void sqli
9ab00 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 69 te3MallocAlarm(i
9ab10 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 6f 69 nt nByte){. voi
9ab20 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 d (*xCallback)(v
9ab30 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 oid*,sqlite3_int
9ab40 36 34 2c 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 64,int);. sqlit
9ab50 65 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73 65 64 e3_int64 nowUsed
9ab60 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a ;. void *pArg;.
9ab70 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61 72 6d if( mem0.alarm
9ab80 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 29 20 72 65 Callback==0 ) re
9ab90 74 75 72 6e 3b 0a 20 20 78 43 61 6c 6c 62 61 63 turn;. xCallbac
9aba0 6b 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 k = mem0.alarmCa
9abb0 6c 6c 62 61 63 6b 3b 0a 20 20 6e 6f 77 55 73 65 llback;. nowUse
9abc0 64 20 3d 20 73 71 6c 69 74 65 33 53 74 61 74 75 d = sqlite3Statu
9abd0 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 sValue(SQLITE_ST
9abe0 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 ATUS_MEMORY_USED
9abf0 29 3b 0a 20 20 70 41 72 67 20 3d 20 6d 65 6d 30 );. pArg = mem0
9ac00 2e 61 6c 61 72 6d 41 72 67 3b 0a 20 20 6d 65 6d .alarmArg;. mem
9ac10 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 0.alarmCallback
9ac20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d = 0;. sqlite3_m
9ac30 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e utex_leave(mem0.
9ac40 6d 75 74 65 78 29 3b 0a 20 20 78 43 61 6c 6c 62 mutex);. xCallb
9ac50 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55 73 65 ack(pArg, nowUse
9ac60 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73 71 6c d, nByte);. sql
9ac70 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
9ac80 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
9ac90 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 mem0.alarmCallba
9aca0 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a ck = xCallback;.
9acb0 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 20 mem0.alarmArg
9acc0 3d 20 70 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a = pArg;.}../*.**
9acd0 20 44 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c Do a memory all
9ace0 6f 63 61 74 69 6f 6e 20 77 69 74 68 20 73 74 61 ocation with sta
9acf0 74 69 73 74 69 63 73 20 61 6e 64 20 61 6c 61 72 tistics and alar
9ad00 6d 73 2e 20 20 41 73 73 75 6d 65 20 74 68 65 0a ms. Assume the.
9ad10 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 ** lock is alrea
9ad20 64 79 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 dy held..*/.stat
9ad30 69 63 20 69 6e 74 20 6d 61 6c 6c 6f 63 57 69 74 ic int mallocWit
9ad40 68 41 6c 61 72 6d 28 69 6e 74 20 6e 2c 20 76 6f hAlarm(int n, vo
9ad50 69 64 20 2a 2a 70 70 29 7b 0a 20 20 69 6e 74 20 id **pp){. int
9ad60 6e 46 75 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a 70 nFull;. void *p
9ad70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
9ad80 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d te3_mutex_held(m
9ad90 65 6d 30 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 em0.mutex) );.
9ada0 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 nFull = sqlite3G
9adb0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 lobalConfig.m.xR
9adc0 6f 75 6e 64 75 70 28 6e 29 3b 0a 20 20 73 71 6c oundup(n);. sql
9add0 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 ite3StatusSet(SQ
9ade0 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c LITE_STATUS_MALL
9adf0 4f 43 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 69 OC_SIZE, n);. i
9ae00 66 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c f( mem0.alarmCal
9ae10 6c 62 61 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 lback!=0 ){.
9ae20 69 6e 74 20 6e 55 73 65 64 20 3d 20 73 71 6c 69 int nUsed = sqli
9ae30 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 53 te3StatusValue(S
9ae40 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d QLITE_STATUS_MEM
9ae50 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 20 20 69 ORY_USED);. i
9ae60 66 28 20 6e 55 73 65 64 2b 6e 46 75 6c 6c 20 3e f( nUsed+nFull >
9ae70 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 = mem0.alarmThre
9ae80 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 73 shold ){. s
9ae90 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 qlite3MallocAlar
9aea0 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 7d 0a m(nFull);. }.
9aeb0 20 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 }. p = sqlite
9aec0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
9aed0 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a xMalloc(nFull);.
9aee0 20 20 69 66 28 20 70 3d 3d 30 20 26 26 20 6d 65 if( p==0 && me
9aef0 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b m0.alarmCallback
9af00 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d ){. sqlite3M
9af10 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c allocAlarm(nFull
9af20 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 );. p = sqlit
9af30 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9af40 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b .xMalloc(nFull);
9af50 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 29 7b 0a . }. if( p ){.
9af60 20 20 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 nFull = sqli
9af70 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 te3MallocSize(p)
9af80 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
9af90 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
9afa0 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 ATUS_MEMORY_USED
9afb0 2c 20 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 , nFull);. }.
9afc0 2a 70 70 20 3d 20 70 3b 0a 20 20 72 65 74 75 72 *pp = p;. retur
9afd0 6e 20 6e 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a n nFull;.}../*.*
9afe0 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 * Allocate memor
9aff0 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 y. This routine
9b000 20 69 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 is like sqlite3
9b010 5f 6d 61 6c 6c 6f 63 28 29 20 65 78 63 65 70 74 _malloc() except
9b020 20 74 68 61 74 20 69 74 0a 2a 2a 20 61 73 73 75 that it.** assu
9b030 6d 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 mes the memory s
9b040 75 62 73 79 73 74 65 6d 20 68 61 73 20 61 6c 72 ubsystem has alr
9b050 65 61 64 79 20 62 65 65 6e 20 69 6e 69 74 69 61 eady been initia
9b060 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 lized..*/.SQLITE
9b070 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
9b080 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 qlite3Malloc(int
9b090 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a n){. void *p;.
9b0a0 20 20 69 66 28 20 6e 3c 3d 30 20 7c 7c 20 6e 3e if( n<=0 || n>
9b0b0 3d 30 78 37 66 66 66 66 66 30 30 20 29 7b 0a 20 =0x7fffff00 ){.
9b0c0 20 20 20 2f 2a 20 41 20 6d 65 6d 6f 72 79 20 61 /* A memory a
9b0d0 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 20 6e llocation of a n
9b0e0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 umber of bytes w
9b0f0 68 69 63 68 20 69 73 20 6e 65 61 72 20 74 68 65 hich is near the
9b100 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 maximum. **
9b110 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 76 signed integer v
9b120 61 6c 75 65 20 6d 69 67 68 74 20 63 61 75 73 65 alue might cause
9b130 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 76 65 72 an integer over
9b140 66 6c 6f 77 20 69 6e 73 69 64 65 20 6f 66 20 74 flow inside of t
9b150 68 65 0a 20 20 20 20 2a 2a 20 78 4d 61 6c 6c 6f he. ** xMallo
9b160 63 28 29 2e 20 20 48 65 6e 63 65 20 77 65 20 6c c(). Hence we l
9b170 69 6d 69 74 20 74 68 65 20 6d 61 78 69 6d 75 6d imit the maximum
9b180 20 73 69 7a 65 20 74 6f 20 30 78 37 66 66 66 66 size to 0x7ffff
9b190 66 30 30 2c 20 67 69 76 69 6e 67 0a 20 20 20 20 f00, giving.
9b1a0 2a 2a 20 32 35 35 20 62 79 74 65 73 20 6f 66 20 ** 255 bytes of
9b1b0 6f 76 65 72 68 65 61 64 2e 20 20 53 51 4c 69 74 overhead. SQLit
9b1c0 65 20 69 74 73 65 6c 66 20 77 69 6c 6c 20 6e 65 e itself will ne
9b1d0 76 65 72 20 75 73 65 20 61 6e 79 74 68 69 6e 67 ver use anything
9b1e0 20 6e 65 61 72 0a 20 20 20 20 2a 2a 20 74 68 69 near. ** thi
9b1f0 73 20 61 6d 6f 75 6e 74 2e 20 20 54 68 65 20 6f s amount. The o
9b200 6e 6c 79 20 77 61 79 20 74 6f 20 72 65 61 63 68 nly way to reach
9b210 20 74 68 65 20 6c 69 6d 69 74 20 69 73 20 77 69 the limit is wi
9b220 74 68 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f th sqlite3_mallo
9b230 63 28 29 20 2a 2f 0a 20 20 20 20 70 20 3d 20 30 c() */. p = 0
9b240 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 ;. }else if( sq
9b250 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b260 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 g.bMemstat ){.
9b270 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
9b280 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 enter(mem0.mutex
9b290 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f 63 57 69 74 );. mallocWit
9b2a0 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20 hAlarm(n, &p);.
9b2b0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
9b2c0 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 _leave(mem0.mute
9b2d0 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 x);. }else{.
9b2e0 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 p = sqlite3Glob
9b2f0 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c alConfig.m.xMall
9b300 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 oc(n);. }. ret
9b310 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
9b320 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 This version of
9b330 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
9b340 61 74 69 6f 6e 20 69 73 20 66 6f 72 20 75 73 65 ation is for use
9b350 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 by the applicat
9b360 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73 74 20 6d 61 ion..** First ma
9b370 6b 65 20 73 75 72 65 20 74 68 65 20 6d 65 6d 6f ke sure the memo
9b380 72 79 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 ry subsystem is
9b390 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65 initialized, the
9b3a0 6e 20 64 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f n do the.** allo
9b3b0 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 cation..*/.SQLIT
9b3c0 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
9b3d0 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 20 6e te3_malloc(int n
9b3e0 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ){.#ifndef SQLIT
9b3f0 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a E_OMIT_AUTOINIT.
9b400 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e if( sqlite3_in
9b410 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 itialize() ) ret
9b420 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 urn 0;.#endif.
9b430 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4d 61 return sqlite3Ma
9b440 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a lloc(n);.}../*.*
9b450 2a 20 45 61 63 68 20 74 68 72 65 61 64 20 6d 61 * Each thread ma
9b460 79 20 6f 6e 6c 79 20 68 61 76 65 20 61 20 73 69 y only have a si
9b470 6e 67 6c 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 ngle outstanding
9b480 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 6f 6d allocation from
9b490 0a 2a 2a 20 78 53 63 72 61 74 63 68 4d 61 6c 6c .** xScratchMall
9b4a0 6f 63 28 29 2e 20 20 57 65 20 76 65 72 69 66 79 oc(). We verify
9b4b0 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 this constraint
9b4c0 20 69 6e 20 74 68 65 20 73 69 6e 67 6c 65 2d 74 in the single-t
9b4d0 68 72 65 61 64 65 64 0a 2a 2a 20 63 61 73 65 20 hreaded.** case
9b4e0 62 79 20 73 65 74 74 69 6e 67 20 73 63 72 61 74 by setting scrat
9b4f0 63 68 41 6c 6c 6f 63 4f 75 74 20 74 6f 20 31 20 chAllocOut to 1
9b500 77 68 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 when an allocati
9b510 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74 73 74 61 6e on.** is outstan
9b520 64 69 6e 67 20 63 6c 65 61 72 69 6e 67 20 69 74 ding clearing it
9b530 20 77 68 65 6e 20 74 68 65 20 61 6c 6c 6f 63 61 when the alloca
9b540 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e 0a 2a tion is freed..*
9b550 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 /.#if SQLITE_THR
9b560 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 EADSAFE==0 && !d
9b570 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 73 efined(NDEBUG).s
9b580 74 61 74 69 63 20 69 6e 74 20 73 63 72 61 74 63 tatic int scratc
9b590 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a 23 hAllocOut = 0;.#
9b5a0 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c endif.../*.** Al
9b5b0 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 68 locate memory th
9b5c0 61 74 20 69 73 20 74 6f 20 62 65 20 75 73 65 64 at is to be used
9b5d0 20 61 6e 64 20 72 65 6c 65 61 73 65 64 20 72 69 and released ri
9b5e0 67 68 74 20 61 77 61 79 2e 0a 2a 2a 20 54 68 69 ght away..** Thi
9b5f0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d s routine is sim
9b600 69 6c 61 72 20 74 6f 20 61 6c 6c 6f 63 61 28 29 ilar to alloca()
9b610 20 69 6e 20 74 68 61 74 20 69 74 20 69 73 20 6e in that it is n
9b620 6f 74 20 69 6e 74 65 6e 64 65 64 0a 2a 2a 20 66 ot intended.** f
9b630 6f 72 20 73 69 74 75 61 74 69 6f 6e 73 20 77 68 or situations wh
9b640 65 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 6d ere the memory m
9b650 69 67 68 74 20 62 65 20 68 65 6c 64 20 6c 6f 6e ight be held lon
9b660 67 2d 74 65 72 6d 2e 20 20 54 68 69 73 0a 2a 2a g-term. This.**
9b670 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 74 65 routine is inte
9b680 6e 64 65 64 20 74 6f 20 67 65 74 20 6d 65 6d 6f nded to get memo
9b690 72 79 20 74 6f 20 6f 6c 64 20 6c 61 72 67 65 20 ry to old large
9b6a0 74 72 61 6e 73 69 65 6e 74 20 64 61 74 61 0a 2a transient data.*
9b6b0 2a 20 73 74 72 75 63 74 75 72 65 73 20 74 68 61 * structures tha
9b6c0 74 20 77 6f 75 6c 64 20 6e 6f 74 20 6e 6f 72 6d t would not norm
9b6d0 61 6c 6c 79 20 66 69 74 20 6f 6e 20 74 68 65 20 ally fit on the
9b6e0 73 74 61 63 6b 20 6f 66 20 61 6e 0a 2a 2a 20 65 stack of an.** e
9b6f0 6d 62 65 64 64 65 64 20 70 72 6f 63 65 73 73 6f mbedded processo
9b700 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
9b710 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
9b720 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 e3ScratchMalloc(
9b730 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a int n){. void *
9b740 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30 p;. assert( n>0
9b750 20 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f );..#if SQLITE_
9b760 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 THREADSAFE==0 &&
9b770 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
9b780 29 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 ). /* Verify th
9b790 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 at no more than
9b7a0 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f one scratch allo
9b7b0 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72 65 61 cation per threa
9b7c0 64 0a 20 20 2a 2a 20 69 73 20 6f 75 74 73 74 61 d. ** is outsta
9b7d0 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 74 69 6d nding at one tim
9b7e0 65 2e 20 20 28 54 68 69 73 20 69 73 20 6f 6e 6c e. (This is onl
9b7f0 79 20 63 68 65 63 6b 65 64 20 69 6e 20 74 68 65 y checked in the
9b800 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 2d 74 68 72 . ** single-thr
9b810 65 61 64 65 64 20 63 61 73 65 20 73 69 6e 63 65 eaded case since
9b820 20 63 68 65 63 6b 69 6e 67 20 69 6e 20 74 68 65 checking in the
9b830 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 multi-threaded
9b840 63 61 73 65 0a 20 20 2a 2a 20 77 6f 75 6c 64 20 case. ** would
9b850 62 65 20 6d 75 63 68 20 6d 6f 72 65 20 63 6f 6d be much more com
9b860 70 6c 69 63 61 74 65 64 2e 29 20 2a 2f 0a 20 20 plicated.) */.
9b870 61 73 73 65 72 74 28 20 73 63 72 61 74 63 68 41 assert( scratchA
9b880 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29 3b 0a 23 65 llocOut==0 );.#e
9b890 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 ndif.. if( sqli
9b8a0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9b8b0 73 7a 53 63 72 61 74 63 68 3c 6e 20 29 7b 0a 20 szScratch<n ){.
9b8c0 20 20 20 67 6f 74 6f 20 73 63 72 61 74 63 68 5f goto scratch_
9b8d0 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 65 6c 73 overflow;. }els
9b8e0 65 7b 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 e{ . sqlite3
9b8f0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
9b900 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 69 66 0.mutex);. if
9b910 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 ( mem0.nScratchF
9b920 72 65 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ree==0 ){.
9b930 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
9b940 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b ave(mem0.mutex);
9b950 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 63 72 61 . goto scra
9b960 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 tch_overflow;.
9b970 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 }else{. i
9b980 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 20 3d 20 nt i;. i =
9b990 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 mem0.aScratchFre
9b9a0 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63 72 61 74 63 e[--mem0.nScratc
9b9b0 68 46 72 65 65 5d 3b 0a 20 20 20 20 20 20 69 20 hFree];. i
9b9c0 2a 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c *= sqlite3Global
9b9d0 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 Config.szScratch
9b9e0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 ;. sqlite3S
9b9f0 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f tatusAdd(SQLITE_
9ba00 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 STATUS_SCRATCH_U
9ba10 53 45 44 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 SED, 1);. s
9ba20 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 qlite3StatusSet(
9ba30 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 SQLITE_STATUS_SC
9ba40 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a RATCH_SIZE, n);.
9ba50 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
9ba60 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d tex_leave(mem0.m
9ba70 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 20 3d utex);. p =
9ba80 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a (void*)&((char*
9ba90 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f )sqlite3GlobalCo
9baa0 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29 5b 69 nfig.pScratch)[i
9bab0 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ];. assert(
9bac0 20 20 28 28 28 75 38 2a 29 70 20 2d 20 28 75 38 (((u8*)p - (u8
9bad0 2a 29 30 29 20 26 20 37 29 3d 3d 30 20 29 3b 0a *)0) & 7)==0 );.
9bae0 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 20 53 51 }. }.#if SQ
9baf0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d LITE_THREADSAFE=
9bb00 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e =0 && !defined(N
9bb10 44 45 42 55 47 29 0a 20 20 73 63 72 61 74 63 68 DEBUG). scratch
9bb20 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d 30 3b AllocOut = p!=0;
9bb30 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 .#endif.. retur
9bb40 6e 20 70 3b 0a 0a 73 63 72 61 74 63 68 5f 6f 76 n p;..scratch_ov
9bb50 65 72 66 6c 6f 77 3a 0a 20 20 69 66 28 20 73 71 erflow:. if( sq
9bb60 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9bb70 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 g.bMemstat ){.
9bb80 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
9bb90 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 enter(mem0.mutex
9bba0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 );. sqlite3St
9bbb0 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 atusSet(SQLITE_S
9bbc0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 TATUS_SCRATCH_SI
9bbd0 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 6e 20 3d 20 ZE, n);. n =
9bbe0 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 mallocWithAlarm(
9bbf0 6e 2c 20 26 70 29 3b 0a 20 20 20 20 69 66 28 20 n, &p);. if(
9bc00 70 20 29 20 73 71 6c 69 74 65 33 53 74 61 74 75 p ) sqlite3Statu
9bc10 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 sAdd(SQLITE_STAT
9bc20 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 US_SCRATCH_OVERF
9bc30 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c LOW, n);. sql
9bc40 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
9bc50 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
9bc60 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 73 }else{. p = s
9bc70 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9bc80 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b ig.m.xMalloc(n);
9bc90 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f . }.#if SQLITE_
9bca0 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 THREADSAFE==0 &&
9bcb0 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
9bcc0 29 0a 20 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 ). scratchAlloc
9bcd0 4f 75 74 20 3d 20 70 21 3d 30 3b 0a 23 65 6e 64 Out = p!=0;.#end
9bce0 69 66 0a 20 20 72 65 74 75 72 6e 20 70 3b 20 20 if. return p;
9bcf0 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 .}.SQLITE_PRIV
9bd00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
9bd10 53 63 72 61 74 63 68 46 72 65 65 28 76 6f 69 64 ScratchFree(void
9bd20 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b *p){. if( p ){
9bd30 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 ..#if SQLITE_THR
9bd40 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 EADSAFE==0 && !d
9bd50 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 efined(NDEBUG).
9bd60 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 /* Verify tha
9bd70 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f t no more than o
9bd80 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 ne scratch alloc
9bd90 61 74 69 6f 6e 20 70 65 72 20 74 68 72 65 61 64 ation per thread
9bda0 0a 20 20 20 20 2a 2a 20 69 73 20 6f 75 74 73 74 . ** is outst
9bdb0 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 74 69 anding at one ti
9bdc0 6d 65 2e 20 20 28 54 68 69 73 20 69 73 20 6f 6e me. (This is on
9bdd0 6c 79 20 63 68 65 63 6b 65 64 20 69 6e 20 74 68 ly checked in th
9bde0 65 0a 20 20 20 20 2a 2a 20 73 69 6e 67 6c 65 2d e. ** single-
9bdf0 74 68 72 65 61 64 65 64 20 63 61 73 65 20 73 69 threaded case si
9be00 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e 20 nce checking in
9be10 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 the multi-thread
9be20 65 64 20 63 61 73 65 0a 20 20 20 20 2a 2a 20 77 ed case. ** w
9be30 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f 72 ould be much mor
9be40 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20 e complicated.)
9be50 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 */. assert( s
9be60 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d cratchAllocOut==
9be70 31 20 29 3b 0a 20 20 20 20 73 63 72 61 74 63 68 1 );. scratch
9be80 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a 23 65 AllocOut = 0;.#e
9be90 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20 73 71 ndif.. if( sq
9bea0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9beb0 67 2e 70 53 63 72 61 74 63 68 3d 3d 30 0a 20 20 g.pScratch==0.
9bec0 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3c 73 71 || p<sq
9bed0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9bee0 67 2e 70 53 63 72 61 74 63 68 0a 20 20 20 20 20 g.pScratch.
9bef0 20 20 20 20 20 20 7c 7c 20 70 3e 3d 28 76 6f 69 || p>=(voi
9bf00 64 2a 29 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 d*)mem0.aScratch
9bf10 46 72 65 65 20 29 7b 0a 20 20 20 20 20 20 69 66 Free ){. if
9bf20 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
9bf30 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 onfig.bMemstat )
9bf40 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 53 {. int iS
9bf50 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c ize = sqlite3Mal
9bf60 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 locSize(p);.
9bf70 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
9bf80 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 x_enter(mem0.mut
9bf90 65 78 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c ex);. sql
9bfa0 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 ite3StatusAdd(SQ
9bfb0 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 LITE_STATUS_SCRA
9bfc0 54 43 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d 69 TCH_OVERFLOW, -i
9bfd0 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 73 Size);. s
9bfe0 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 qlite3StatusAdd(
9bff0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 SQLITE_STATUS_ME
9c000 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 69 53 69 7a MORY_USED, -iSiz
9c010 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 e);. sqli
9c020 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9c030 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20 20 m.xFree(p);.
9c040 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
9c050 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 x_leave(mem0.mut
9c060 65 78 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ex);. }else
9c070 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
9c080 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
9c090 78 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 xFree(p);.
9c0a0 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
9c0b0 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
9c0c0 69 20 3d 20 28 69 6e 74 29 28 28 75 38 2a 29 70 i = (int)((u8*)p
9c0d0 20 2d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 47 - (u8*)sqlite3G
9c0e0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 lobalConfig.pScr
9c0f0 61 74 63 68 29 3b 0a 20 20 20 20 20 20 69 20 2f atch);. i /
9c100 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
9c110 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3b onfig.szScratch;
9c120 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
9c130 3e 3d 30 20 26 26 20 69 3c 73 71 6c 69 74 65 33 >=0 && i<sqlite3
9c140 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 GlobalConfig.nSc
9c150 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20 20 73 ratch );. s
9c160 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
9c170 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a er(mem0.mutex);.
9c180 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 assert( me
9c190 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 3c m0.nScratchFree<
9c1a0 28 75 33 32 29 73 71 6c 69 74 65 33 47 6c 6f 62 (u32)sqlite3Glob
9c1b0 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 alConfig.nScratc
9c1c0 68 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 30 2e h );. mem0.
9c1d0 61 53 63 72 61 74 63 68 46 72 65 65 5b 6d 65 6d aScratchFree[mem
9c1e0 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 2b 2b 0.nScratchFree++
9c1f0 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 73 71 6c ] = i;. sql
9c200 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 ite3StatusAdd(SQ
9c210 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 LITE_STATUS_SCRA
9c220 54 43 48 5f 55 53 45 44 2c 20 2d 31 29 3b 0a 20 TCH_USED, -1);.
9c230 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
9c240 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 ex_leave(mem0.mu
9c250 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a tex);. }. }.
9c260 7d 0a 0a 2f 2a 0a 2a 2a 20 54 52 55 45 20 69 66 }../*.** TRUE if
9c270 20 70 20 69 73 20 61 20 6c 6f 6f 6b 61 73 69 64 p is a lookasid
9c280 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
9c290 69 6f 6e 20 66 72 6f 6d 20 64 62 0a 2a 2f 0a 23 ion from db.*/.#
9c2a0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
9c2b0 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 73 74 61 IT_LOOKASIDE.sta
9c2c0 74 69 63 20 69 6e 74 20 69 73 4c 6f 6f 6b 61 73 tic int isLookas
9c2d0 69 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ide(sqlite3 *db,
9c2e0 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 74 void *p){. ret
9c2f0 75 72 6e 20 64 62 20 26 26 20 70 20 26 26 20 70 urn db && p && p
9c300 3e 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e >=db->lookaside.
9c310 70 53 74 61 72 74 20 26 26 20 70 3c 64 62 2d 3e pStart && p<db->
9c320 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e 64 3b 0a lookaside.pEnd;.
9c330 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 }.#else.#define
9c340 69 73 4c 6f 6f 6b 61 73 69 64 65 28 41 2c 42 29 isLookaside(A,B)
9c350 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0.#endif../*.**
9c360 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 Return the size
9c370 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c of a memory all
9c380 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 6f 75 73 ocation previous
9c390 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ly obtained from
9c3a0 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f .** sqlite3Mallo
9c3b0 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 6d c() or sqlite3_m
9c3c0 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 alloc()..*/.SQLI
9c3d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
9c3e0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 qlite3MallocSize
9c3f0 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 74 (void *p){. ret
9c400 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 urn sqlite3Globa
9c410 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a 65 28 lConfig.m.xSize(
9c420 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 p);.}.SQLITE_PRI
9c430 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
9c440 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71 6c DbMallocSize(sql
9c450 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a ite3 *db, void *
9c460 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 64 62 p){. assert( db
9c470 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
9c480 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 utex_held(db->mu
9c490 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 69 73 tex) );. if( is
9c4a0 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 Lookaside(db, p)
9c4b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 ){. return d
9c4c0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 3b b->lookaside.sz;
9c4d0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
9c4e0 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 turn sqlite3Glob
9c4f0 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a 65 alConfig.m.xSize
9c500 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
9c510 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 70 72 * Free memory pr
9c520 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 eviously obtaine
9c530 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61 d from sqlite3Ma
9c540 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 lloc()..*/.SQLIT
9c550 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
9c560 65 33 5f 66 72 65 65 28 76 6f 69 64 20 2a 70 29 e3_free(void *p)
9c570 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 {. if( p==0 ) r
9c580 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c eturn;. if( sql
9c590 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9c5a0 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 .bMemstat ){.
9c5b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
9c5c0 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 nter(mem0.mutex)
9c5d0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
9c5e0 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
9c5f0 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 ATUS_MEMORY_USED
9c600 2c 20 2d 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 , -sqlite3Malloc
9c610 53 69 7a 65 28 70 29 29 3b 0a 20 20 20 20 73 71 Size(p));. sq
9c620 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9c630 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 g.m.xFree(p);.
9c640 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
9c650 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 leave(mem0.mutex
9c660 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
9c670 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9c680 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a fig.m.xFree(p);.
9c690 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 }.}../*.** Fre
9c6a0 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 69 e memory that mi
9c6b0 67 68 74 20 62 65 20 61 73 73 6f 63 69 61 74 65 ght be associate
9c6c0 64 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 d with a particu
9c6d0 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 lar database.**
9c6e0 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 connection..*/.S
9c6f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
9c700 69 64 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 id sqlite3DbFree
9c710 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f (sqlite3 *db, vo
9c720 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 id *p){. assert
9c730 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ( db==0 || sqlit
9c740 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 e3_mutex_held(db
9c750 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 ->mutex) );. if
9c760 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62 ( isLookaside(db
9c770 2c 20 70 29 20 29 7b 0a 20 20 20 20 4c 6f 6f 6b , p) ){. Look
9c780 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 66 20 asideSlot *pBuf
9c790 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 = (LookasideSlot
9c7a0 2a 29 70 3b 0a 20 20 20 20 70 42 75 66 2d 3e 70 *)p;. pBuf->p
9c7b0 4e 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 Next = db->looka
9c7c0 73 69 64 65 2e 70 46 72 65 65 3b 0a 20 20 20 20 side.pFree;.
9c7d0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 db->lookaside.pF
9c7e0 72 65 65 20 3d 20 70 42 75 66 3b 0a 20 20 20 20 ree = pBuf;.
9c7f0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f db->lookaside.nO
9c800 75 74 2d 2d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ut--;. }else{.
9c810 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
9c820 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a p);. }.}../*.**
9c830 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 Change the size
9c840 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 of an existing
9c850 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
9c860 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 n.*/.SQLITE_PRIV
9c870 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
9c880 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 3Realloc(void *p
9c890 4f 6c 64 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 Old, int nBytes)
9c8a0 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 2c 20 6e 4e {. int nOld, nN
9c8b0 65 77 3b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 ew;. void *pNew
9c8c0 3b 0a 20 20 69 66 28 20 70 4f 6c 64 3d 3d 30 20 ;. if( pOld==0
9c8d0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 ){. return sq
9c8e0 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74 lite3Malloc(nByt
9c8f0 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e es);. }. if( n
9c900 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20 20 Bytes<=0 ){.
9c910 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f 6c sqlite3_free(pOl
9c920 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 d);. return 0
9c930 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 74 ;. }. if( nByt
9c940 65 73 3e 3d 30 78 37 66 66 66 66 66 30 30 20 29 es>=0x7fffff00 )
9c950 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 30 78 37 {. /* The 0x7
9c960 66 66 66 66 30 30 20 6c 69 6d 69 74 20 74 65 72 ffff00 limit ter
9c970 6d 20 69 73 20 65 78 70 6c 61 69 6e 65 64 20 69 m is explained i
9c980 6e 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 73 71 n comments on sq
9c990 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f lite3Malloc() */
9c9a0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
9c9b0 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 73 71 6c 69 }. nOld = sqli
9c9c0 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4f te3MallocSize(pO
9c9d0 6c 64 29 3b 0a 20 20 6e 4e 65 77 20 3d 20 73 71 ld);. nNew = sq
9c9e0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9c9f0 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 28 6e 42 79 g.m.xRoundup(nBy
9ca00 74 65 73 29 3b 0a 20 20 69 66 28 20 6e 4f 6c 64 tes);. if( nOld
9ca10 3d 3d 6e 4e 65 77 20 29 7b 0a 20 20 20 20 70 4e ==nNew ){. pN
9ca20 65 77 20 3d 20 70 4f 6c 64 3b 0a 20 20 7d 65 6c ew = pOld;. }el
9ca30 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c se if( sqlite3Gl
9ca40 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 obalConfig.bMems
9ca50 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 tat ){. sqlit
9ca60 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
9ca70 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em0.mutex);.
9ca80 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 sqlite3StatusSet
9ca90 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d (SQLITE_STATUS_M
9caa0 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e 42 79 74 ALLOC_SIZE, nByt
9cab0 65 73 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c es);. if( sql
9cac0 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 ite3StatusValue(
9cad0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 SQLITE_STATUS_ME
9cae0 4d 4f 52 59 5f 55 53 45 44 29 2b 6e 4e 65 77 2d MORY_USED)+nNew-
9caf0 6e 4f 6c 64 20 3e 3d 20 0a 20 20 20 20 20 20 20 nOld >= .
9cb00 20 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 mem0.alarmThr
9cb10 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 eshold ){.
9cb20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 sqlite3MallocAla
9cb30 72 6d 28 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 rm(nNew-nOld);.
9cb40 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 20 3d 20 }. pNew =
9cb50 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9cb60 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 fig.m.xRealloc(p
9cb70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 Old, nNew);.
9cb80 69 66 28 20 70 4e 65 77 3d 3d 30 20 26 26 20 6d if( pNew==0 && m
9cb90 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 em0.alarmCallbac
9cba0 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 k ){. sqlit
9cbb0 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 42 e3MallocAlarm(nB
9cbc0 79 74 65 73 29 3b 0a 20 20 20 20 20 20 70 4e 65 ytes);. pNe
9cbd0 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 w = sqlite3Globa
9cbe0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c lConfig.m.xReall
9cbf0 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a oc(pOld, nNew);.
9cc00 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4e }. if( pN
9cc10 65 77 20 29 7b 0a 20 20 20 20 20 20 6e 4e 65 77 ew ){. nNew
9cc20 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
9cc30 53 69 7a 65 28 70 4e 65 77 29 3b 0a 20 20 20 20 Size(pNew);.
9cc40 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 sqlite3StatusA
9cc50 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dd(SQLITE_STATUS
9cc60 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e 4e _MEMORY_USED, nN
9cc70 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a ew-nOld);. }.
9cc80 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
9cc90 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 x_leave(mem0.mut
9cca0 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ex);. }else{.
9ccb0 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
9ccc0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9ccd0 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e Realloc(pOld, nN
9cce0 65 77 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ew);. }. retur
9ccf0 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n pNew;.}../*.**
9cd00 20 54 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65 The public inte
9cd10 72 66 61 63 65 20 74 6f 20 73 71 6c 69 74 65 33 rface to sqlite3
9cd20 52 65 61 6c 6c 6f 63 2e 20 20 4d 61 6b 65 20 73 Realloc. Make s
9cd30 75 72 65 20 74 68 61 74 20 74 68 65 20 6d 65 6d ure that the mem
9cd40 6f 72 79 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d ory.** subsystem
9cd50 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 is initialized
9cd60 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e prior to invokin
9cd70 67 20 73 71 6c 69 74 65 52 65 61 6c 6c 6f 63 2e g sqliteRealloc.
9cd80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
9cd90 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65 61 oid *sqlite3_rea
9cda0 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 4f 6c 64 2c lloc(void *pOld,
9cdb0 20 69 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66 int n){.#ifndef
9cdc0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
9cdd0 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 OINIT. if( sqli
9cde0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
9cdf0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e ) return 0;.#en
9ce00 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c dif. return sql
9ce10 69 74 65 33 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 ite3Realloc(pOld
9ce20 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 , n);.}.../*.**
9ce30 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 Allocate and zer
9ce40 6f 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 20 0a 53 51 o memory..*/ .SQ
9ce50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
9ce60 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 d *sqlite3Malloc
9ce70 5a 65 72 6f 28 69 6e 74 20 6e 29 7b 0a 20 20 76 Zero(int n){. v
9ce80 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 oid *p = sqlite3
9ce90 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28 Malloc(n);. if(
9cea0 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 p ){. memset
9ceb0 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20 (p, 0, n);. }.
9cec0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
9ced0 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 .** Allocate and
9cee0 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 20 49 zero memory. I
9cef0 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e f the allocation
9cf00 20 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a 20 fails, make.**
9cf10 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 the mallocFailed
9cf20 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e flag in the con
9cf30 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e nection pointer.
9cf40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
9cf50 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
9cf60 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c DbMallocZero(sql
9cf70 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 29 ite3 *db, int n)
9cf80 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 {. void *p = sq
9cf90 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
9cfa0 28 64 62 2c 20 6e 29 3b 0a 20 20 69 66 28 20 70 (db, n);. if( p
9cfb0 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 ){. memset(p
9cfc0 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 , 0, n);. }. r
9cfd0 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
9cfe0 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a * Allocate and z
9cff0 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 20 49 66 20 ero memory. If
9d000 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 the allocation f
9d010 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68 ails, make.** th
9d020 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 e mallocFailed f
9d030 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 lag in the conne
9d040 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a ction pointer..*
9d050 2a 0a 2a 2a 20 49 66 20 64 62 21 3d 30 20 61 6e *.** If db!=0 an
9d060 64 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c d db->mallocFail
9d070 65 64 20 69 73 20 74 72 75 65 20 28 69 6e 64 69 ed is true (indi
9d080 63 61 74 69 6e 67 20 61 20 70 72 69 6f 72 20 6d cating a prior m
9d090 61 6c 6c 6f 63 0a 2a 2a 20 66 61 69 6c 75 72 65 alloc.** failure
9d0a0 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 on the same dat
9d0b0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
9d0c0 29 20 74 68 65 6e 20 61 6c 77 61 79 73 20 72 65 ) then always re
9d0d0 74 75 72 6e 20 30 2e 0a 2a 2a 20 48 65 6e 63 65 turn 0..** Hence
9d0e0 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 for a particula
9d0f0 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
9d100 63 74 69 6f 6e 2c 20 6f 6e 63 65 20 6d 61 6c 6c ction, once mall
9d110 6f 63 20 73 74 61 72 74 73 0a 2a 2a 20 66 61 69 oc starts.** fai
9d120 6c 69 6e 67 2c 20 69 74 20 66 61 69 6c 73 20 63 ling, it fails c
9d130 6f 6e 73 69 73 74 65 6e 74 6c 79 20 75 6e 74 69 onsistently unti
9d140 6c 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69 l mallocFailed i
9d150 73 20 72 65 73 65 74 2e 0a 2a 2a 20 54 68 69 73 s reset..** This
9d160 20 69 73 20 61 6e 20 69 6d 70 6f 72 74 61 6e 74 is an important
9d170 20 61 73 73 75 6d 70 74 69 6f 6e 2e 20 20 54 68 assumption. Th
9d180 65 72 65 20 61 72 65 20 6d 61 6e 79 20 70 6c 61 ere are many pla
9d190 63 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 63 6f ces in the.** co
9d1a0 64 65 20 74 68 61 74 20 64 6f 20 74 68 69 6e 67 de that do thing
9d1b0 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a s like this:.**.
9d1c0 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74 20 2a ** int *
9d1d0 61 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 74 65 a = (int*)sqlite
9d1e0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
9d1f0 20 31 30 30 29 3b 0a 2a 2a 20 20 20 20 20 20 20 100);.**
9d200 20 20 69 6e 74 20 2a 62 20 3d 20 28 69 6e 74 2a int *b = (int*
9d210 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 )sqlite3DbMalloc
9d220 52 61 77 28 64 62 2c 20 32 30 30 29 3b 0a 2a 2a Raw(db, 200);.**
9d230 20 20 20 20 20 20 20 20 20 69 66 28 20 62 20 29 if( b )
9d240 20 61 5b 31 30 5d 20 3d 20 39 3b 0a 2a 2a 0a 2a a[10] = 9;.**.*
9d250 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 * In other words
9d260 2c 20 69 66 20 61 20 73 75 62 73 65 71 75 65 6e , if a subsequen
9d270 74 20 6d 61 6c 6c 6f 63 20 28 65 78 3a 20 22 62 t malloc (ex: "b
9d280 22 29 20 77 6f 72 6b 65 64 2c 20 69 74 20 69 73 ") worked, it is
9d290 20 61 73 73 75 6d 65 64 0a 2a 2a 20 74 68 61 74 assumed.** that
9d2a0 20 61 6c 6c 20 70 72 69 6f 72 20 6d 61 6c 6c 6f all prior mallo
9d2b0 63 73 20 28 65 78 3a 20 22 61 22 29 20 77 6f 72 cs (ex: "a") wor
9d2c0 6b 65 64 20 74 6f 6f 2e 0a 2a 2f 0a 53 51 4c 49 ked too..*/.SQLI
9d2d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
9d2e0 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 *sqlite3DbMalloc
9d2f0 52 61 77 28 73 71 6c 69 74 65 33 20 2a 64 62 2c Raw(sqlite3 *db,
9d300 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 int n){. void
9d310 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 *p;. assert( db
9d320 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
9d330 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 utex_held(db->mu
9d340 74 65 78 29 20 29 3b 0a 23 69 66 6e 64 65 66 20 tex) );.#ifndef
9d350 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b SQLITE_OMIT_LOOK
9d360 41 53 49 44 45 0a 20 20 69 66 28 20 64 62 20 29 ASIDE. if( db )
9d370 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65 53 {. LookasideS
9d380 6c 6f 74 20 2a 70 42 75 66 3b 0a 20 20 20 20 69 lot *pBuf;. i
9d390 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
9d3a0 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65 74 led ){. ret
9d3b0 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 0;. }.
9d3c0 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 if( db->lookasi
9d3d0 64 65 2e 62 45 6e 61 62 6c 65 64 20 26 26 20 6e de.bEnabled && n
9d3e0 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e <=db->lookaside.
9d3f0 73 7a 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 sz. && (
9d400 70 42 75 66 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 pBuf = db->looka
9d410 73 69 64 65 2e 70 46 72 65 65 29 21 3d 30 20 29 side.pFree)!=0 )
9d420 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b {. db->look
9d430 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20 70 42 aside.pFree = pB
9d440 75 66 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 uf->pNext;.
9d450 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e db->lookaside.n
9d460 4f 75 74 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 Out++;. if(
9d470 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e db->lookaside.n
9d480 4f 75 74 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 Out>db->lookasid
9d490 65 2e 6d 78 4f 75 74 20 29 7b 0a 20 20 20 20 20 e.mxOut ){.
9d4a0 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 db->lookaside
9d4b0 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f .mxOut = db->loo
9d4c0 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 kaside.nOut;.
9d4d0 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 }. retur
9d4e0 6e 20 28 76 6f 69 64 2a 29 70 42 75 66 3b 0a 20 n (void*)pBuf;.
9d4f0 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 }. }.#else.
9d500 20 69 66 28 20 64 62 20 26 26 20 64 62 2d 3e 6d if( db && db->m
9d510 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
9d520 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
9d530 0a 23 65 6e 64 69 66 0a 20 20 70 20 3d 20 73 71 .#endif. p = sq
9d540 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a lite3Malloc(n);.
9d550 20 20 69 66 28 20 21 70 20 26 26 20 64 62 20 29 if( !p && db )
9d560 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 {. db->malloc
9d570 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a Failed = 1;. }.
9d580 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
9d590 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 74 68 65 20 *.** Resize the
9d5a0 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 block of memory
9d5b0 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 20 pointed to by p
9d5c0 74 6f 20 6e 20 62 79 74 65 73 2e 20 49 66 20 74 to n bytes. If t
9d5d0 68 65 0a 2a 2a 20 72 65 73 69 7a 65 20 66 61 69 he.** resize fai
9d5e0 6c 73 2c 20 73 65 74 20 74 68 65 20 6d 61 6c 6c ls, set the mall
9d5f0 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e ocFailed flag in
9d600 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 the connection
9d610 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 object..*/.SQLIT
9d620 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
9d630 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 sqlite3DbRealloc
9d640 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f (sqlite3 *db, vo
9d650 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 id *p, int n){.
9d660 20 76 6f 69 64 20 2a 70 4e 65 77 20 3d 20 30 3b void *pNew = 0;
9d670 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 . assert( db!=0
9d680 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
9d690 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
9d6a0 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 (db->mutex) );.
9d6b0 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
9d6c0 61 69 6c 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 ailed==0 ){.
9d6d0 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 if( p==0 ){.
9d6e0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
9d6f0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
9d700 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 n);. }. if
9d710 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62 ( isLookaside(db
9d720 2c 20 70 29 20 29 7b 0a 20 20 20 20 20 20 69 66 , p) ){. if
9d730 28 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 ( n<=db->lookasi
9d740 64 65 2e 73 7a 20 29 7b 0a 20 20 20 20 20 20 20 de.sz ){.
9d750 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 20 return p;.
9d760 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 }. pNew =
9d770 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
9d780 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20 20 20 aw(db, n);.
9d790 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 if( pNew ){.
9d7a0 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 memcpy(pNew
9d7b0 2c 20 70 2c 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 , p, db->lookasi
9d7c0 64 65 2e 73 7a 29 3b 0a 20 20 20 20 20 20 20 20 de.sz);.
9d7d0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
9d7e0 2c 20 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 , p);. }.
9d7f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
9d800 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 New = sqlite3_re
9d810 61 6c 6c 6f 63 28 70 2c 20 6e 29 3b 0a 20 20 20 alloc(p, n);.
9d820 20 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a if( !pNew ){.
9d830 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c db->mall
9d840 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
9d850 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
9d860 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d return pNew;.}
9d870 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 ../*.** Attempt
9d880 74 6f 20 72 65 61 6c 6c 6f 63 61 74 65 20 70 2e to reallocate p.
9d890 20 20 49 66 20 74 68 65 20 72 65 61 6c 6c 6f 63 If the realloc
9d8a0 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 ation fails, the
9d8b0 6e 20 66 72 65 65 20 70 0a 2a 2a 20 61 6e 64 20 n free p.** and
9d8c0 73 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 set the mallocFa
9d8d0 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 iled flag in the
9d8e0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
9d8f0 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
9d900 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
9d910 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 lite3DbReallocOr
9d920 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62 Free(sqlite3 *db
9d930 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e , void *p, int n
9d940 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b ){. void *pNew;
9d950 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 . pNew = sqlite
9d960 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 3DbRealloc(db, p
9d970 2c 20 6e 29 3b 0a 20 20 69 66 28 20 21 70 4e 65 , n);. if( !pNe
9d980 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 w ){. sqlite3
9d990 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 DbFree(db, p);.
9d9a0 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 }. return pNew
9d9b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 ;.}../*.** Make
9d9c0 61 20 63 6f 70 79 20 6f 66 20 61 20 73 74 72 69 a copy of a stri
9d9d0 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 ng in memory obt
9d9e0 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
9d9f0 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 73 65 eMalloc(). These
9da00 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 63 .** functions c
9da10 61 6c 6c 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f all sqlite3Mallo
9da20 63 52 61 77 28 29 20 64 69 72 65 63 74 6c 79 20 cRaw() directly
9da30 69 6e 73 74 65 61 64 20 6f 66 20 73 71 6c 69 74 instead of sqlit
9da40 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 69 73 0a eMalloc(). This.
9da50 2a 2a 20 69 73 20 62 65 63 61 75 73 65 20 77 68 ** is because wh
9da60 65 6e 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 67 en memory debugg
9da70 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20 6f 6e ing is turned on
9da80 2c 20 74 68 65 73 65 20 74 77 6f 20 66 75 6e 63 , these two func
9da90 74 69 6f 6e 73 20 61 72 65 20 0a 2a 2a 20 63 61 tions are .** ca
9daa0 6c 6c 65 64 20 76 69 61 20 6d 61 63 72 6f 73 20 lled via macros
9dab0 74 68 61 74 20 72 65 63 6f 72 64 20 74 68 65 20 that record the
9dac0 63 75 72 72 65 6e 74 20 66 69 6c 65 20 61 6e 64 current file and
9dad0 20 6c 69 6e 65 20 6e 75 6d 62 65 72 20 69 6e 20 line number in
9dae0 74 68 65 0a 2a 2a 20 54 68 72 65 61 64 44 61 74 the.** ThreadDat
9daf0 61 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a a structure..*/.
9db00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
9db10 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 har *sqlite3DbSt
9db20 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 rDup(sqlite3 *db
9db30 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 , const char *z)
9db40 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a {. char *zNew;.
9db50 20 20 73 69 7a 65 5f 74 20 6e 3b 0a 20 20 69 66 size_t n;. if
9db60 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ( z==0 ){. re
9db70 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 20 turn 0;. }. n
9db80 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
9db90 30 28 7a 29 20 2b 20 31 3b 0a 20 20 61 73 73 65 0(z) + 1;. asse
9dba0 72 74 28 20 28 6e 26 30 78 37 66 66 66 66 66 66 rt( (n&0x7ffffff
9dbb0 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a 4e 65 77 20 f)==n );. zNew
9dbc0 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
9dbd0 63 52 61 77 28 64 62 2c 20 28 69 6e 74 29 6e 29 cRaw(db, (int)n)
9dbe0 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a ;. if( zNew ){.
9dbf0 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c memcpy(zNew,
9dc00 20 7a 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 z, n);. }. re
9dc10 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 53 51 4c turn zNew;.}.SQL
9dc20 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
9dc30 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 *sqlite3DbStrND
9dc40 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 up(sqlite3 *db,
9dc50 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 const char *z, i
9dc60 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a 7a nt n){. char *z
9dc70 4e 65 77 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 New;. if( z==0
9dc80 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
9dc90 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 . }. assert( (
9dca0 6e 26 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e n&0x7fffffff)==n
9dcb0 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c );. zNew = sql
9dcc0 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
9dcd0 64 62 2c 20 6e 2b 31 29 3b 0a 20 20 69 66 28 20 db, n+1);. if(
9dce0 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 zNew ){. memc
9dcf0 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a py(zNew, z, n);.
9dd00 20 20 20 20 7a 4e 65 77 5b 6e 5d 20 3d 20 30 3b zNew[n] = 0;
9dd10 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e . }. return zN
9dd20 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 ew;.}../*.** Cre
9dd30 61 74 65 20 61 20 73 74 72 69 6e 67 20 66 72 6f ate a string fro
9dd40 6d 20 74 68 65 20 7a 46 72 6f 6d 61 74 20 61 72 m the zFromat ar
9dd50 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 76 gument and the v
9dd60 61 5f 6c 69 73 74 20 74 68 61 74 20 66 6f 6c 6c a_list that foll
9dd70 6f 77 73 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 ows..** Store th
9dd80 65 20 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d 6f e string in memo
9dd90 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ry obtained from
9dda0 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 sqliteMalloc()
9ddb0 61 6e 64 20 6d 61 6b 65 20 2a 70 7a 0a 2a 2a 20 and make *pz.**
9ddc0 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 73 74 point to that st
9ddd0 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ring..*/.SQLITE_
9dde0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
9ddf0 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 63 68 ite3SetString(ch
9de00 61 72 20 2a 2a 70 7a 2c 20 73 71 6c 69 74 65 33 ar **pz, sqlite3
9de10 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 *db, const char
9de20 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
9de30 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 . va_list ap;.
9de40 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 76 61 5f char *z;.. va_
9de50 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 start(ap, zForma
9de60 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 t);. z = sqlite
9de70 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 3VMPrintf(db, zF
9de80 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 ormat, ap);. va
9de90 5f 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 _end(ap);. sqli
9dea0 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 2a 70 te3DbFree(db, *p
9deb0 7a 29 3b 0a 20 20 2a 70 7a 20 3d 20 7a 3b 0a 7d z);. *pz = z;.}
9dec0 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 .../*.** This fu
9ded0 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 63 nction must be c
9dee0 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 65 78 69 alled before exi
9def0 74 69 6e 67 20 61 6e 79 20 41 50 49 20 66 75 6e ting any API fun
9df00 63 74 69 6f 6e 20 28 69 2e 65 2e 20 0a 2a 2a 20 ction (i.e. .**
9df10 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e 74 72 6f returning contro
9df20 6c 20 74 6f 20 74 68 65 20 75 73 65 72 29 20 74 l to the user) t
9df30 68 61 74 20 68 61 73 20 63 61 6c 6c 65 64 20 73 hat has called s
9df40 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 6f 72 qlite3_malloc or
9df50 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 61 6c .** sqlite3_real
9df60 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 loc..**.** The r
9df70 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 eturned value is
9df80 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 63 6f 70 79 normally a copy
9df90 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 of the second a
9dfa0 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 0a rgument to this.
9dfb0 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 48 6f 77 ** function. How
9dfc0 65 76 65 72 2c 20 69 66 20 61 20 6d 61 6c 6c 6f ever, if a mallo
9dfd0 63 28 29 20 66 61 69 6c 75 72 65 20 68 61 73 20 c() failure has
9dfe0 6f 63 63 75 72 72 65 64 20 73 69 6e 63 65 20 74 occurred since t
9dff0 68 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 69 he previous.** i
9e000 6e 76 6f 63 61 74 69 6f 6e 20 53 51 4c 49 54 45 nvocation SQLITE
9e010 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e _NOMEM is return
9e020 65 64 20 69 6e 73 74 65 61 64 2e 20 0a 2a 2a 0a ed instead. .**.
9e030 2a 2a 20 49 66 20 74 68 65 20 66 69 72 73 74 20 ** If the first
9e040 61 72 67 75 6d 65 6e 74 2c 20 64 62 2c 20 69 73 argument, db, is
9e050 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20 61 20 not NULL and a
9e060 6d 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 68 malloc() error h
9e070 61 73 20 6f 63 63 75 72 72 65 64 2c 0a 2a 2a 20 as occurred,.**
9e080 74 68 65 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 then the connect
9e090 69 6f 6e 20 65 72 72 6f 72 2d 63 6f 64 65 20 28 ion error-code (
9e0a0 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
9e0b0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 65 72 ed by sqlite3_er
9e0c0 72 63 6f 64 65 28 29 29 0a 2a 2a 20 69 73 20 73 rcode()).** is s
9e0d0 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d et to SQLITE_NOM
9e0e0 45 4d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 EM..*/.SQLITE_PR
9e0f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
9e100 33 41 70 69 45 78 69 74 28 73 71 6c 69 74 65 33 3ApiExit(sqlite3
9e110 2a 20 64 62 2c 20 69 6e 74 20 72 63 29 7b 0a 20 * db, int rc){.
9e120 20 2f 2a 20 49 66 20 74 68 65 20 64 62 20 68 61 /* If the db ha
9e130 6e 64 6c 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c ndle is not NULL
9e140 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 68 , then we must h
9e150 6f 6c 64 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 old the connecti
9e160 6f 6e 20 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 6d on handle. ** m
9e170 75 74 65 78 20 68 65 72 65 2e 20 4f 74 68 65 72 utex here. Other
9e180 77 69 73 65 20 74 68 65 20 72 65 61 64 20 28 61 wise the read (a
9e190 6e 64 20 70 6f 73 73 69 62 6c 65 20 77 72 69 74 nd possible writ
9e1a0 65 29 20 6f 66 20 64 62 2d 3e 6d 61 6c 6c 6f 63 e) of db->malloc
9e1b0 46 61 69 6c 65 64 20 0a 20 20 2a 2a 20 69 73 20 Failed . ** is
9e1c0 75 6e 73 61 66 65 2c 20 61 73 20 69 73 20 74 68 unsafe, as is th
9e1d0 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 e call to sqlite
9e1e0 33 45 72 72 6f 72 28 29 2e 0a 20 20 2a 2f 0a 20 3Error().. */.
9e1f0 20 61 73 73 65 72 74 28 20 21 64 62 20 7c 7c 20 assert( !db ||
9e200 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
9e210 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b ld(db->mutex) );
9e220 0a 20 20 69 66 28 20 64 62 20 26 26 20 28 64 62 . if( db && (db
9e230 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c ->mallocFailed |
9e240 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 | rc==SQLITE_IOE
9e250 52 52 5f 4e 4f 4d 45 4d 29 20 29 7b 0a 20 20 20 RR_NOMEM) ){.
9e260 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 sqlite3Error(db
9e270 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 , SQLITE_NOMEM,
9e280 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 0);. db->mall
9e290 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 ocFailed = 0;.
9e2a0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
9e2b0 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 MEM;. }. retur
9e2c0 6e 20 72 63 20 26 20 28 64 62 20 3f 20 64 62 2d n rc & (db ? db-
9e2d0 3e 65 72 72 4d 61 73 6b 20 3a 20 30 78 66 66 29 >errMask : 0xff)
9e2e0 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;.}../**********
9e2f0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61 6c 6c **** End of mall
9e300 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a oc.c ***********
9e310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e330 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
9e340 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
9e350 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a printf.c *******
9e360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e380 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 ***/./*.** The "
9e390 70 72 69 6e 74 66 22 20 63 6f 64 65 20 74 68 61 printf" code tha
9e3a0 74 20 66 6f 6c 6c 6f 77 73 20 64 61 74 65 73 20 t follows dates
9e3b0 66 72 6f 6d 20 74 68 65 20 31 39 38 30 27 73 2e from the 1980's.
9e3c0 20 20 49 74 20 69 73 20 69 6e 0a 2a 2a 20 74 68 It is in.** th
9e3d0 65 20 70 75 62 6c 69 63 20 64 6f 6d 61 69 6e 2e e public domain.
9e3e0 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 63 The original c
9e3f0 6f 6d 6d 65 6e 74 73 20 61 72 65 20 69 6e 63 6c omments are incl
9e400 75 64 65 64 20 68 65 72 65 20 66 6f 72 0a 2a 2a uded here for.**
9e410 20 63 6f 6d 70 6c 65 74 65 6e 65 73 73 2e 20 20 completeness.
9e420 54 68 65 79 20 61 72 65 20 76 65 72 79 20 6f 75 They are very ou
9e430 74 2d 6f 66 2d 64 61 74 65 20 62 75 74 20 6d 69 t-of-date but mi
9e440 67 68 74 20 62 65 20 75 73 65 66 75 6c 20 61 73 ght be useful as
9e450 0a 2a 2a 20 61 6e 20 68 69 73 74 6f 72 69 63 61 .** an historica
9e460 6c 20 72 65 66 65 72 65 6e 63 65 2e 20 20 4d 6f l reference. Mo
9e470 73 74 20 6f 66 20 74 68 65 20 22 65 6e 68 61 6e st of the "enhan
9e480 63 65 6d 65 6e 74 73 22 20 68 61 76 65 20 62 65 cements" have be
9e490 65 6e 20 62 61 63 6b 65 64 0a 2a 2a 20 6f 75 74 en backed.** out
9e4a0 20 73 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e so that the fun
9e4b0 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 6e 6f ctionality is no
9e4c0 77 20 74 68 65 20 73 61 6d 65 20 61 73 20 73 74 w the same as st
9e4d0 61 6e 64 61 72 64 20 70 72 69 6e 74 66 28 29 2e andard printf().
9e4e0 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 70 72 69 6e .**.** $Id: prin
9e4f0 74 66 2e 63 2c 76 20 31 2e 31 30 34 20 32 30 30 tf.c,v 1.104 200
9e500 39 2f 30 36 2f 30 33 20 30 31 3a 32 34 3a 35 34 9/06/03 01:24:54
9e510 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a drh Exp $.**.**
9e520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e560 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
9e570 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 6f 64 he following mod
9e580 75 6c 65 73 20 69 73 20 61 6e 20 65 6e 68 61 6e ules is an enhan
9e590 63 65 64 20 72 65 70 6c 61 63 65 6d 65 6e 74 20 ced replacement
9e5a0 66 6f 72 20 74 68 65 20 22 70 72 69 6e 74 66 22 for the "printf"
9e5b0 20 73 75 62 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 subroutines.**
9e5c0 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 73 74 61 found in the sta
9e5d0 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e ndard C library.
9e5e0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
9e5f0 65 6e 68 61 6e 63 65 6d 65 6e 74 73 20 61 72 65 enhancements are
9e600 0a 2a 2a 20 73 75 70 70 6f 72 74 65 64 3a 0a 2a .** supported:.*
9e610 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 64 64 *.** + Add
9e620 69 74 69 6f 6e 61 6c 20 66 75 6e 63 74 69 6f 6e itional function
9e630 73 2e 20 20 54 68 65 20 73 74 61 6e 64 61 72 64 s. The standard
9e640 20 73 65 74 20 6f 66 20 22 70 72 69 6e 74 66 22 set of "printf"
9e650 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 20 functions.**
9e660 20 20 20 20 20 20 69 6e 63 6c 75 64 65 73 20 70 includes p
9e670 72 69 6e 74 66 2c 20 66 70 72 69 6e 74 66 2c 20 rintf, fprintf,
9e680 73 70 72 69 6e 74 66 2c 20 76 70 72 69 6e 74 66 sprintf, vprintf
9e690 2c 20 76 66 70 72 69 6e 74 66 2c 20 61 6e 64 0a , vfprintf, and.
9e6a0 2a 2a 20 20 20 20 20 20 20 20 20 76 73 70 72 69 ** vspri
9e6b0 6e 74 66 2e 20 20 54 68 69 73 20 6d 6f 64 75 6c ntf. This modul
9e6c0 65 20 61 64 64 73 20 74 68 65 20 66 6f 6c 6c 6f e adds the follo
9e6d0 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 wing:.**.**
9e6e0 20 20 20 20 20 20 2a 20 20 73 6e 70 72 69 6e 74 * snprint
9e6f0 66 20 2d 2d 20 57 6f 72 6b 73 20 6c 69 6b 65 20 f -- Works like
9e700 73 70 72 69 6e 74 66 2c 20 62 75 74 20 68 61 73 sprintf, but has
9e710 20 61 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65 an extra argume
9e720 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 nt.**
9e730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 w
9e740 68 69 63 68 20 69 73 20 74 68 65 20 73 69 7a 65 hich is the size
9e750 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 77 of the buffer w
9e760 72 69 74 74 65 6e 20 74 6f 2e 0a 2a 2a 0a 2a 2a ritten to..**.**
9e770 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 6d 70 * mp
9e780 72 69 6e 74 66 20 2d 2d 20 20 53 69 6d 69 6c 61 rintf -- Simila
9e790 72 20 74 6f 20 73 70 72 69 6e 74 66 2e 20 20 57 r to sprintf. W
9e7a0 72 69 74 65 73 20 6f 75 74 70 75 74 20 74 6f 20 rites output to
9e7b0 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 memory.**
9e7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9e7d0 20 20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d obtained from
9e7e0 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 20 malloc..**.**
9e7f0 20 20 20 20 20 20 20 20 20 2a 20 20 78 70 72 69 * xpri
9e800 6e 74 66 20 2d 2d 20 20 43 61 6c 6c 73 20 61 20 ntf -- Calls a
9e810 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 69 73 70 function to disp
9e820 6f 73 65 20 6f 66 20 6f 75 74 70 75 74 2e 0a 2a ose of output..*
9e830 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a *.** *
9e840 20 20 6e 70 72 69 6e 74 66 20 2d 2d 20 20 4e 6f nprintf -- No
9e850 20 6f 75 74 70 75 74 2c 20 62 75 74 20 72 65 74 output, but ret
9e860 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 urns the number
9e870 6f 66 20 63 68 61 72 61 63 74 65 72 73 0a 2a 2a of characters.**
9e880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9e890 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 77 that w
9e8a0 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 6f ould have been o
9e8b0 75 74 70 75 74 20 62 79 20 70 72 69 6e 74 66 2e utput by printf.
9e8c0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .**.**
9e8d0 20 2a 20 20 41 20 76 2d 20 76 65 72 73 69 6f 6e * A v- version
9e8e0 20 28 65 78 3a 20 76 73 6e 70 72 69 6e 74 66 29 (ex: vsnprintf)
9e8f0 20 6f 66 20 65 76 65 72 79 20 66 75 6e 63 74 69 of every functi
9e900 6f 6e 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 20 20 on is also.**
9e910 20 20 20 20 20 20 20 20 20 20 20 73 75 70 70 6c suppl
9e920 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 ied..**.**
9e930 2b 20 20 41 20 66 65 77 20 65 78 74 65 6e 73 69 + A few extensi
9e940 6f 6e 73 20 74 6f 20 74 68 65 20 66 6f 72 6d 61 ons to the forma
9e950 74 74 69 6e 67 20 6e 6f 74 61 74 69 6f 6e 20 61 tting notation a
9e960 72 65 20 73 75 70 70 6f 72 74 65 64 3a 0a 2a 2a re supported:.**
9e970 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 .** *
9e980 20 54 68 65 20 22 3d 22 20 66 6c 61 67 20 28 73 The "=" flag (s
9e990 69 6d 69 6c 61 72 20 74 6f 20 22 2d 22 29 20 63 imilar to "-") c
9e9a0 61 75 73 65 73 20 74 68 65 20 6f 75 74 70 75 74 auses the output
9e9b0 20 74 6f 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 to be.**
9e9c0 20 20 20 20 20 20 20 62 65 20 63 65 6e 74 65 72 be center
9e9d0 65 64 20 69 6e 20 74 68 65 20 61 70 70 72 6f 70 ed in the approp
9e9e0 72 69 61 74 65 6c 79 20 73 69 7a 65 64 20 66 69 riately sized fi
9e9f0 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 eld..**.**
9ea00 20 20 20 20 20 2a 20 20 54 68 65 20 25 62 20 66 * The %b f
9ea10 69 65 6c 64 20 6f 75 74 70 75 74 73 20 61 6e 20 ield outputs an
9ea20 69 6e 74 65 67 65 72 20 69 6e 20 62 69 6e 61 72 integer in binar
9ea30 79 20 6e 6f 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a y notation..**.*
9ea40 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 54 * * T
9ea50 68 65 20 25 63 20 66 69 65 6c 64 20 6e 6f 77 20 he %c field now
9ea60 61 63 63 65 70 74 73 20 61 20 70 72 65 63 69 73 accepts a precis
9ea70 69 6f 6e 2e 20 20 54 68 65 20 63 68 61 72 61 63 ion. The charac
9ea80 74 65 72 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 ter output.**
9ea90 20 20 20 20 20 20 20 20 20 20 20 69 73 20 72 65 is re
9eaa0 70 65 61 74 65 64 20 62 79 20 74 68 65 20 6e 75 peated by the nu
9eab0 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 mber of times th
9eac0 65 20 70 72 65 63 69 73 69 6f 6e 20 73 70 65 63 e precision spec
9ead0 69 66 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 ifies..**.**
9eae0 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 27 * The %'
9eaf0 20 66 69 65 6c 64 20 77 6f 72 6b 73 20 6c 69 6b field works lik
9eb00 65 20 25 63 2c 20 62 75 74 20 74 61 6b 65 73 20 e %c, but takes
9eb10 61 73 20 69 74 73 20 63 68 61 72 61 63 74 65 72 as its character
9eb20 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
9eb30 20 20 20 20 20 6e 65 78 74 20 63 68 61 72 61 63 next charac
9eb40 74 65 72 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 ter of the forma
9eb50 74 20 73 74 72 69 6e 67 2c 20 69 6e 73 74 65 61 t string, instea
9eb60 64 20 6f 66 20 74 68 65 20 6e 65 78 74 0a 2a 2a d of the next.**
9eb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 72 ar
9eb80 67 75 6d 65 6e 74 2e 20 20 46 6f 72 20 65 78 61 gument. For exa
9eb90 6d 70 6c 65 2c 20 20 70 72 69 6e 74 66 28 22 25 mple, printf("%
9eba0 2e 37 38 27 2d 22 29 20 20 70 72 69 6e 74 73 20 .78'-") prints
9ebb0 37 38 20 6d 69 6e 75 73 0a 2a 2a 20 20 20 20 20 78 minus.**
9ebc0 20 20 20 20 20 20 20 20 20 73 69 67 6e 73 2c 20 signs,
9ebd0 74 68 65 20 73 61 6d 65 20 61 73 20 20 70 72 69 the same as pri
9ebe0 6e 74 66 28 22 25 2e 37 38 63 22 2c 27 2d 27 29 ntf("%.78c",'-')
9ebf0 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 ..**.** +
9ec00 57 68 65 6e 20 63 6f 6d 70 69 6c 65 64 20 75 73 When compiled us
9ec10 69 6e 67 20 47 43 43 20 6f 6e 20 61 20 53 50 41 ing GCC on a SPA
9ec20 52 43 2c 20 74 68 69 73 20 76 65 72 73 69 6f 6e RC, this version
9ec30 20 6f 66 20 70 72 69 6e 74 66 20 69 73 0a 2a 2a of printf is.**
9ec40 20 20 20 20 20 20 20 20 20 66 61 73 74 65 72 20 faster
9ec50 74 68 61 6e 20 74 68 65 20 6c 69 62 72 61 72 79 than the library
9ec60 20 70 72 69 6e 74 66 20 66 6f 72 20 53 55 4e 20 printf for SUN
9ec70 4f 53 20 34 2e 31 2e 0a 2a 2a 0a 2a 2a 20 20 20 OS 4.1..**.**
9ec80 20 20 20 2b 20 20 41 6c 6c 20 66 75 6e 63 74 69 + All functi
9ec90 6f 6e 73 20 61 72 65 20 66 75 6c 6c 79 20 72 65 ons are fully re
9eca0 65 6e 74 72 61 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 0a entrant..**.*/..
9ecb0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e /*.** Conversion
9ecc0 20 74 79 70 65 73 20 66 61 6c 6c 20 69 6e 74 6f types fall into
9ecd0 20 76 61 72 69 6f 75 73 20 63 61 74 65 67 6f 72 various categor
9ece0 69 65 73 20 61 73 20 64 65 66 69 6e 65 64 20 62 ies as defined b
9ecf0 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 y the.** followi
9ed00 6e 67 20 65 6e 75 6d 65 72 61 74 69 6f 6e 2e 0a ng enumeration..
9ed10 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 52 41 44 */.#define etRAD
9ed20 49 58 20 20 20 20 20 20 20 31 20 2f 2a 20 49 6e IX 1 /* In
9ed30 74 65 67 65 72 20 74 79 70 65 73 2e 20 20 25 64 teger types. %d
9ed40 2c 20 25 78 2c 20 25 6f 2c 20 61 6e 64 20 73 6f , %x, %o, and so
9ed50 20 66 6f 72 74 68 20 2a 2f 0a 23 64 65 66 69 6e forth */.#defin
9ed60 65 20 65 74 46 4c 4f 41 54 20 20 20 20 20 20 20 e etFLOAT
9ed70 32 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 70 6f 2 /* Floating po
9ed80 69 6e 74 2e 20 20 25 66 20 2a 2f 0a 23 64 65 66 int. %f */.#def
9ed90 69 6e 65 20 65 74 45 58 50 20 20 20 20 20 20 20 ine etEXP
9eda0 20 20 33 20 2f 2a 20 45 78 70 6f 6e 65 6e 74 69 3 /* Exponenti
9edb0 6f 6e 61 6c 20 6e 6f 74 61 74 69 6f 6e 2e 20 25 onal notation. %
9edc0 65 20 61 6e 64 20 25 45 20 2a 2f 0a 23 64 65 66 e and %E */.#def
9edd0 69 6e 65 20 65 74 47 45 4e 45 52 49 43 20 20 20 ine etGENERIC
9ede0 20 20 34 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 4 /* Floating
9edf0 6f 72 20 65 78 70 6f 6e 65 6e 74 69 61 6c 2c 20 or exponential,
9ee00 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 65 78 70 depending on exp
9ee10 6f 6e 65 6e 74 2e 20 25 67 20 2a 2f 0a 23 64 65 onent. %g */.#de
9ee20 66 69 6e 65 20 65 74 53 49 5a 45 20 20 20 20 20 fine etSIZE
9ee30 20 20 20 35 20 2f 2a 20 52 65 74 75 72 6e 20 6e 5 /* Return n
9ee40 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
9ee50 65 72 73 20 70 72 6f 63 65 73 73 65 64 20 73 6f ers processed so
9ee60 20 66 61 72 2e 20 25 6e 20 2a 2f 0a 23 64 65 66 far. %n */.#def
9ee70 69 6e 65 20 65 74 53 54 52 49 4e 47 20 20 20 20 ine etSTRING
9ee80 20 20 36 20 2f 2a 20 53 74 72 69 6e 67 73 2e 20 6 /* Strings.
9ee90 25 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 %s */.#define et
9eea0 44 59 4e 53 54 52 49 4e 47 20 20 20 37 20 2f 2a DYNSTRING 7 /*
9eeb0 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c Dynamically all
9eec0 6f 63 61 74 65 64 20 73 74 72 69 6e 67 73 2e 20 ocated strings.
9eed0 25 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 %z */.#define et
9eee0 50 45 52 43 45 4e 54 20 20 20 20 20 38 20 2f 2a PERCENT 8 /*
9eef0 20 50 65 72 63 65 6e 74 20 73 79 6d 62 6f 6c 2e Percent symbol.
9ef00 20 25 25 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %% */.#define e
9ef10 74 43 48 41 52 58 20 20 20 20 20 20 20 39 20 2f tCHARX 9 /
9ef20 2a 20 43 68 61 72 61 63 74 65 72 73 2e 20 25 63 * Characters. %c
9ef30 20 2a 2f 0a 2f 2a 20 54 68 65 20 72 65 73 74 20 */./* The rest
9ef40 61 72 65 20 65 78 74 65 6e 73 69 6f 6e 73 2c 20 are extensions,
9ef50 6e 6f 74 20 6e 6f 72 6d 61 6c 6c 79 20 66 6f 75 not normally fou
9ef60 6e 64 20 69 6e 20 70 72 69 6e 74 66 28 29 20 2a nd in printf() *
9ef70 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45 /.#define etSQLE
9ef80 53 43 41 50 45 20 20 31 30 20 2f 2a 20 53 74 72 SCAPE 10 /* Str
9ef90 69 6e 67 73 20 77 69 74 68 20 27 5c 27 27 20 64 ings with '\'' d
9efa0 6f 75 62 6c 65 64 2e 20 20 25 71 20 2a 2f 0a 23 oubled. %q */.#
9efb0 64 65 66 69 6e 65 20 65 74 53 51 4c 45 53 43 41 define etSQLESCA
9efc0 50 45 32 20 31 31 20 2f 2a 20 53 74 72 69 6e 67 PE2 11 /* String
9efd0 73 20 77 69 74 68 20 27 5c 27 27 20 64 6f 75 62 s with '\'' doub
9efe0 6c 65 64 20 61 6e 64 20 65 6e 63 6c 6f 73 65 64 led and enclosed
9eff0 20 69 6e 20 27 27 2c 0a 20 20 20 20 20 20 20 20 in '',.
9f000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9f010 20 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20 NULL pointers
9f020 72 65 70 6c 61 63 65 64 20 62 79 20 53 51 4c 20 replaced by SQL
9f030 4e 55 4c 4c 2e 20 20 25 51 20 2a 2f 0a 23 64 65 NULL. %Q */.#de
9f040 66 69 6e 65 20 65 74 54 4f 4b 45 4e 20 20 20 20 fine etTOKEN
9f050 20 20 31 32 20 2f 2a 20 61 20 70 6f 69 6e 74 65 12 /* a pointe
9f060 72 20 74 6f 20 61 20 54 6f 6b 65 6e 20 73 74 72 r to a Token str
9f070 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e ucture */.#defin
9f080 65 20 65 74 53 52 43 4c 49 53 54 20 20 20 20 31 e etSRCLIST 1
9f090 33 20 2f 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 3 /* a pointer t
9f0a0 6f 20 61 20 53 72 63 4c 69 73 74 20 2a 2f 0a 23 o a SrcList */.#
9f0b0 64 65 66 69 6e 65 20 65 74 50 4f 49 4e 54 45 52 define etPOINTER
9f0c0 20 20 20 20 31 34 20 2f 2a 20 54 68 65 20 25 70 14 /* The %p
9f0d0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 23 conversion */.#
9f0e0 64 65 66 69 6e 65 20 65 74 53 51 4c 45 53 43 41 define etSQLESCA
9f0f0 50 45 33 20 31 35 20 2f 2a 20 25 77 20 2d 3e 20 PE3 15 /* %w ->
9f100 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 5c 22 Strings with '\"
9f110 27 20 64 6f 75 62 6c 65 64 20 2a 2f 0a 23 64 65 ' doubled */.#de
9f120 66 69 6e 65 20 65 74 4f 52 44 49 4e 41 4c 20 20 fine etORDINAL
9f130 20 20 31 36 20 2f 2a 20 25 72 20 2d 3e 20 31 73 16 /* %r -> 1s
9f140 74 2c 20 32 6e 64 2c 20 33 72 64 2c 20 34 74 68 t, 2nd, 3rd, 4th
9f150 2c 20 65 74 63 2e 20 20 45 6e 67 6c 69 73 68 20 , etc. English
9f160 6f 6e 6c 79 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 only */..#define
9f170 20 65 74 49 4e 56 41 4c 49 44 20 20 20 20 20 30 etINVALID 0
9f180 20 2f 2a 20 41 6e 79 20 75 6e 72 65 63 6f 67 6e /* Any unrecogn
9f190 69 7a 65 64 20 63 6f 6e 76 65 72 73 69 6f 6e 20 ized conversion
9f1a0 74 79 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 type */.../*.**
9f1b0 41 6e 20 22 65 74 42 79 74 65 22 20 69 73 20 61 An "etByte" is a
9f1c0 6e 20 38 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 n 8-bit unsigned
9f1d0 20 76 61 6c 75 65 2e 0a 2a 2f 0a 74 79 70 65 64 value..*/.typed
9f1e0 65 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 ef unsigned char
9f1f0 20 65 74 42 79 74 65 3b 0a 0a 2f 2a 0a 2a 2a 20 etByte;../*.**
9f200 45 61 63 68 20 62 75 69 6c 74 69 6e 20 63 6f 6e Each builtin con
9f210 76 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 version characte
9f220 72 20 28 65 78 3a 20 74 68 65 20 27 64 27 20 69 r (ex: the 'd' i
9f230 6e 20 22 25 64 22 29 20 69 73 20 64 65 73 63 72 n "%d") is descr
9f240 69 62 65 64 0a 2a 2a 20 62 79 20 61 6e 20 69 6e ibed.** by an in
9f250 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
9f260 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
9f270 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 e.*/.typedef str
9f280 75 63 74 20 65 74 5f 69 6e 66 6f 20 7b 20 20 20 uct et_info {
9f290 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 /* Information a
9f2a0 62 6f 75 74 20 65 61 63 68 20 66 6f 72 6d 61 74 bout each format
9f2b0 20 66 69 65 6c 64 20 2a 2f 0a 20 20 63 68 61 72 field */. char
9f2c0 20 66 6d 74 74 79 70 65 3b 20 20 20 20 20 20 20 fmttype;
9f2d0 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 72 6d /* The form
9f2e0 61 74 20 66 69 65 6c 64 20 63 6f 64 65 20 6c 65 at field code le
9f2f0 74 74 65 72 20 2a 2f 0a 20 20 65 74 42 79 74 65 tter */. etByte
9f300 20 62 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 base;
9f310 20 20 20 2f 2a 20 54 68 65 20 62 61 73 65 20 66 /* The base f
9f320 6f 72 20 72 61 64 69 78 20 63 6f 6e 76 65 72 73 or radix convers
9f330 69 6f 6e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 ion */. etByte
9f340 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
9f350 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 /* One or more
9f360 20 6f 66 20 46 4c 41 47 5f 20 63 6f 6e 73 74 61 of FLAG_ consta
9f370 6e 74 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 65 nts below */. e
9f380 74 42 79 74 65 20 74 79 70 65 3b 20 20 20 20 20 tByte type;
9f390 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 /* Conve
9f3a0 72 73 69 6f 6e 20 70 61 72 61 64 69 67 6d 20 2a rsion paradigm *
9f3b0 2f 0a 20 20 65 74 42 79 74 65 20 63 68 61 72 73 /. etByte chars
9f3c0 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 et; /*
9f3d0 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 44 69 67 Offset into aDig
9f3e0 69 74 73 5b 5d 20 6f 66 20 74 68 65 20 64 69 67 its[] of the dig
9f3f0 69 74 73 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 its string */.
9f400 65 74 42 79 74 65 20 70 72 65 66 69 78 3b 20 20 etByte prefix;
9f410 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
9f420 65 74 20 69 6e 74 6f 20 61 50 72 65 66 69 78 5b et into aPrefix[
9f430 5d 20 6f 66 20 74 68 65 20 70 72 65 66 69 78 20 ] of the prefix
9f440 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 65 74 5f 69 string */.} et_i
9f450 6e 66 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f nfo;../*.** Allo
9f460 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 65 wed values for e
9f470 74 5f 69 6e 66 6f 2e 66 6c 61 67 73 0a 2a 2f 0a t_info.flags.*/.
9f480 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 49 47 #define FLAG_SIG
9f490 4e 45 44 20 20 31 20 20 20 20 20 2f 2a 20 54 72 NED 1 /* Tr
9f4a0 75 65 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 ue if the value
9f4b0 74 6f 20 63 6f 6e 76 65 72 74 20 69 73 20 73 69 to convert is si
9f4c0 67 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 gned */.#define
9f4d0 46 4c 41 47 5f 49 4e 54 45 52 4e 20 20 32 20 20 FLAG_INTERN 2
9f4e0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66 6f /* True if fo
9f4f0 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f r internal use o
9f500 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 46 nly */.#define F
9f510 4c 41 47 5f 53 54 52 49 4e 47 20 20 34 20 20 20 LAG_STRING 4
9f520 20 20 2f 2a 20 41 6c 6c 6f 77 20 69 6e 66 69 6e /* Allow infin
9f530 69 74 79 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f ity precision */
9f540 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c .../*.** The fol
9f550 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 69 73 20 lowing table is
9f560 73 65 61 72 63 68 65 64 20 6c 69 6e 65 61 72 6c searched linearl
9f570 79 2c 20 73 6f 20 69 74 20 69 73 20 67 6f 6f 64 y, so it is good
9f580 20 74 6f 20 70 75 74 20 74 68 65 0a 2a 2a 20 6d to put the.** m
9f590 6f 73 74 20 66 72 65 71 75 65 6e 74 6c 79 20 75 ost frequently u
9f5a0 73 65 64 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 sed conversion t
9f5b0 79 70 65 73 20 66 69 72 73 74 2e 0a 2a 2f 0a 73 ypes first..*/.s
9f5c0 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 tatic const char
9f5d0 20 61 44 69 67 69 74 73 5b 5d 20 3d 20 22 30 31 aDigits[] = "01
9f5e0 32 33 34 35 36 37 38 39 41 42 43 44 45 46 30 31 23456789ABCDEF01
9f5f0 32 33 34 35 36 37 38 39 61 62 63 64 65 66 22 3b 23456789abcdef";
9f600 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 .static const ch
9f610 61 72 20 61 50 72 65 66 69 78 5b 5d 20 3d 20 22 ar aPrefix[] = "
9f620 2d 78 30 5c 30 30 30 58 30 22 3b 0a 73 74 61 74 -x0\000X0";.stat
9f630 69 63 20 63 6f 6e 73 74 20 65 74 5f 69 6e 66 6f ic const et_info
9f640 20 66 6d 74 69 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 fmtinfo[] = {.
9f650 20 7b 20 20 27 64 27 2c 20 31 30 2c 20 31 2c 20 { 'd', 10, 1,
9f660 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c etRADIX, 0,
9f670 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 73 27 2c 0 },. { 's',
9f680 20 20 30 2c 20 34 2c 20 65 74 53 54 52 49 4e 47 0, 4, etSTRING
9f690 2c 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 , 0, 0 },.
9f6a0 20 7b 20 20 27 67 27 2c 20 20 30 2c 20 31 2c 20 { 'g', 0, 1,
9f6b0 65 74 47 45 4e 45 52 49 43 2c 20 20 20 20 33 30 etGENERIC, 30
9f6c0 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 7a 27 2c , 0 },. { 'z',
9f6d0 20 20 30 2c 20 34 2c 20 65 74 44 59 4e 53 54 52 0, 4, etDYNSTR
9f6e0 49 4e 47 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20 ING, 0, 0 },.
9f6f0 20 7b 20 20 27 71 27 2c 20 20 30 2c 20 34 2c 20 { 'q', 0, 4,
9f700 65 74 53 51 4c 45 53 43 41 50 45 2c 20 20 30 2c etSQLESCAPE, 0,
9f710 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 51 27 2c 0 },. { 'Q',
9f720 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 0, 4, etSQLESC
9f730 41 50 45 32 2c 20 30 2c 20 20 30 20 7d 2c 0a 20 APE2, 0, 0 },.
9f740 20 7b 20 20 27 77 27 2c 20 20 30 2c 20 34 2c 20 { 'w', 0, 4,
9f750 65 74 53 51 4c 45 53 43 41 50 45 33 2c 20 30 2c etSQLESCAPE3, 0,
9f760 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 63 27 2c 0 },. { 'c',
9f770 20 20 30 2c 20 30 2c 20 65 74 43 48 41 52 58 2c 0, 0, etCHARX,
9f780 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 0, 0 },.
9f790 20 7b 20 20 27 6f 27 2c 20 20 38 2c 20 30 2c 20 { 'o', 8, 0,
9f7a0 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c etRADIX, 0,
9f7b0 20 20 32 20 7d 2c 0a 20 20 7b 20 20 27 75 27 2c 2 },. { 'u',
9f7c0 20 31 30 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 10, 0, etRADIX,
9f7d0 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 0, 0 },.
9f7e0 20 7b 20 20 27 78 27 2c 20 31 36 2c 20 30 2c 20 { 'x', 16, 0,
9f7f0 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 31 36 etRADIX, 16
9f800 2c 20 31 20 7d 2c 0a 20 20 7b 20 20 27 58 27 2c , 1 },. { 'X',
9f810 20 31 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 16, 0, etRADIX,
9f820 20 20 20 20 20 20 30 2c 20 20 34 20 7d 2c 0a 23 0, 4 },.#
9f830 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
9f840 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
9f850 54 0a 20 20 7b 20 20 27 66 27 2c 20 20 30 2c 20 T. { 'f', 0,
9f860 31 2c 20 65 74 46 4c 4f 41 54 2c 20 20 20 20 20 1, etFLOAT,
9f870 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
9f880 65 27 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50 e', 0, 1, etEXP
9f890 2c 20 20 20 20 20 20 20 20 33 30 2c 20 30 20 7d , 30, 0 }
9f8a0 2c 0a 20 20 7b 20 20 27 45 27 2c 20 20 30 2c 20 ,. { 'E', 0,
9f8b0 31 2c 20 65 74 45 58 50 2c 20 20 20 20 20 20 20 1, etEXP,
9f8c0 20 31 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 14, 0 },. { '
9f8d0 47 27 2c 20 20 30 2c 20 31 2c 20 65 74 47 45 4e G', 0, 1, etGEN
9f8e0 45 52 49 43 2c 20 20 20 20 31 34 2c 20 30 20 7d ERIC, 14, 0 }
9f8f0 2c 0a 23 65 6e 64 69 66 0a 20 20 7b 20 20 27 69 ,.#endif. { 'i
9f900 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41 44 49 ', 10, 1, etRADI
9f910 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c X, 0, 0 },
9f920 0a 20 20 7b 20 20 27 6e 27 2c 20 20 30 2c 20 30 . { 'n', 0, 0
9f930 2c 20 65 74 53 49 5a 45 2c 20 20 20 20 20 20 20 , etSIZE,
9f940 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 25 0, 0 },. { '%
9f950 27 2c 20 20 30 2c 20 30 2c 20 65 74 50 45 52 43 ', 0, 0, etPERC
9f960 45 4e 54 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c ENT, 0, 0 },
9f970 0a 20 20 7b 20 20 27 70 27 2c 20 31 36 2c 20 30 . { 'p', 16, 0
9f980 2c 20 65 74 50 4f 49 4e 54 45 52 2c 20 20 20 20 , etPOINTER,
9f990 30 2c 20 20 31 20 7d 2c 0a 0a 2f 2a 20 41 6c 6c 0, 1 },../* All
9f9a0 20 74 68 65 20 72 65 73 74 20 68 61 76 65 20 74 the rest have t
9f9b0 68 65 20 46 4c 41 47 5f 49 4e 54 45 52 4e 20 62 he FLAG_INTERN b
9f9c0 69 74 20 73 65 74 20 61 6e 64 20 61 72 65 20 74 it set and are t
9f9d0 68 75 73 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c hus for internal
9f9e0 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a .** use only */.
9f9f0 20 20 7b 20 20 27 54 27 2c 20 20 30 2c 20 32 2c { 'T', 0, 2,
9fa00 20 65 74 54 4f 4b 45 4e 2c 20 20 20 20 20 20 30 etTOKEN, 0
9fa10 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 53 27 , 0 },. { 'S'
9fa20 2c 20 20 30 2c 20 32 2c 20 65 74 53 52 43 4c 49 , 0, 2, etSRCLI
9fa30 53 54 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a ST, 0, 0 },.
9fa40 20 20 7b 20 20 27 72 27 2c 20 31 30 2c 20 33 2c { 'r', 10, 3,
9fa50 20 65 74 4f 52 44 49 4e 41 4c 2c 20 20 20 20 30 etORDINAL, 0
9fa60 2c 20 20 30 20 7d 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a , 0 },.};../*.*
9fa70 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 * If SQLITE_OMIT
9fa80 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 _FLOATING_POINT
9fa90 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e is defined, then
9faa0 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c 6f none of the flo
9fab0 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 63 ating point.** c
9fac0 6f 6e 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c 20 onversions will
9fad0 77 6f 72 6b 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 work..*/.#ifndef
9fae0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
9faf0 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 2f 2a 0a 2a ATING_POINT./*.*
9fb00 2a 20 22 2a 76 61 6c 22 20 69 73 20 61 20 64 6f * "*val" is a do
9fb10 75 62 6c 65 20 73 75 63 68 20 74 68 61 74 20 30 uble such that 0
9fb20 2e 31 20 3c 3d 20 2a 76 61 6c 20 3c 20 31 30 2e .1 <= *val < 10.
9fb30 30 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 0.** Return the
9fb40 61 73 63 69 69 20 63 6f 64 65 20 66 6f 72 20 74 ascii code for t
9fb50 68 65 20 6c 65 61 64 69 6e 67 20 64 69 67 69 74 he leading digit
9fb60 20 6f 66 20 2a 76 61 6c 2c 20 74 68 65 6e 0a 2a of *val, then.*
9fb70 2a 20 6d 75 6c 74 69 70 6c 79 20 22 2a 76 61 6c * multiply "*val
9fb80 22 20 62 79 20 31 30 2e 30 20 74 6f 20 72 65 6e " by 10.0 to ren
9fb90 6f 72 6d 61 6c 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 ormalize..**.**
9fba0 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 20 20 20 20 Example:.**
9fbb0 69 6e 70 75 74 3a 20 20 20 20 20 2a 76 61 6c 20 input: *val
9fbc0 3d 20 33 2e 31 34 31 35 39 0a 2a 2a 20 20 20 20 = 3.14159.**
9fbd0 20 6f 75 74 70 75 74 3a 20 20 20 20 2a 76 61 6c output: *val
9fbe0 20 3d 20 31 2e 34 31 35 39 20 20 20 20 66 75 6e = 1.4159 fun
9fbf0 63 74 69 6f 6e 20 72 65 74 75 72 6e 20 3d 20 27 ction return = '
9fc00 33 27 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 75 3'.**.** The cou
9fc10 6e 74 65 72 20 2a 63 6e 74 20 69 73 20 69 6e 63 nter *cnt is inc
9fc20 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74 69 remented each ti
9fc30 6d 65 2e 20 20 41 66 74 65 72 20 63 6f 75 6e 74 me. After count
9fc40 65 72 20 65 78 63 65 65 64 73 0a 2a 2a 20 31 36 er exceeds.** 16
9fc50 20 28 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 (the number of
9fc60 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 significant digi
9fc70 74 73 20 69 6e 20 61 20 36 34 2d 62 69 74 20 66 ts in a 64-bit f
9fc80 6c 6f 61 74 29 20 27 30 27 20 69 73 0a 2a 2a 20 loat) '0' is.**
9fc90 61 6c 77 61 79 73 20 72 65 74 75 72 6e 65 64 2e always returned.
9fca0 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 .*/.static char
9fcb0 65 74 5f 67 65 74 64 69 67 69 74 28 4c 4f 4e 47 et_getdigit(LONG
9fcc0 44 4f 55 42 4c 45 5f 54 59 50 45 20 2a 76 61 6c DOUBLE_TYPE *val
9fcd0 2c 20 69 6e 74 20 2a 63 6e 74 29 7b 0a 20 20 69 , int *cnt){. i
9fce0 6e 74 20 64 69 67 69 74 3b 0a 20 20 4c 4f 4e 47 nt digit;. LONG
9fcf0 44 4f 55 42 4c 45 5f 54 59 50 45 20 64 3b 0a 20 DOUBLE_TYPE d;.
9fd00 20 69 66 28 20 28 2a 63 6e 74 29 2b 2b 20 3e 3d if( (*cnt)++ >=
9fd10 20 31 36 20 29 20 72 65 74 75 72 6e 20 27 30 27 16 ) return '0'
9fd20 3b 0a 20 20 64 69 67 69 74 20 3d 20 28 69 6e 74 ;. digit = (int
9fd30 29 2a 76 61 6c 3b 0a 20 20 64 20 3d 20 64 69 67 )*val;. d = dig
9fd40 69 74 3b 0a 20 20 64 69 67 69 74 20 2b 3d 20 27 it;. digit += '
9fd50 30 27 3b 0a 20 20 2a 76 61 6c 20 3d 20 28 2a 76 0';. *val = (*v
9fd60 61 6c 20 2d 20 64 29 2a 31 30 2e 30 3b 0a 20 20 al - d)*10.0;.
9fd70 72 65 74 75 72 6e 20 28 63 68 61 72 29 64 69 67 return (char)dig
9fd80 69 74 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 it;.}.#endif /*
9fd90 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
9fda0 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a 2f TING_POINT */../
9fdb0 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 4e 20 73 70 *.** Append N sp
9fdc0 61 63 65 20 63 68 61 72 61 63 74 65 72 73 20 74 ace characters t
9fdd0 6f 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 o the given stri
9fde0 6e 67 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 ng buffer..*/.st
9fdf0 61 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e 64 atic void append
9fe00 53 70 61 63 65 28 53 74 72 41 63 63 75 6d 20 2a Space(StrAccum *
9fe10 70 41 63 63 75 6d 2c 20 69 6e 74 20 4e 29 7b 0a pAccum, int N){.
9fe20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 static const c
9fe30 68 61 72 20 7a 53 70 61 63 65 73 5b 5d 20 3d 20 har zSpaces[] =
9fe40 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
9fe50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 3b ";
9fe60 0a 20 20 77 68 69 6c 65 28 20 4e 3e 3d 28 69 6e . while( N>=(in
9fe70 74 29 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 t)sizeof(zSpaces
9fe80 29 2d 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 )-1 ){. sqlit
9fe90 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 e3StrAccumAppend
9fea0 28 70 41 63 63 75 6d 2c 20 7a 53 70 61 63 65 73 (pAccum, zSpaces
9feb0 2c 20 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 , sizeof(zSpaces
9fec0 29 2d 31 29 3b 0a 20 20 20 20 4e 20 2d 3d 20 73 )-1);. N -= s
9fed0 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 izeof(zSpaces)-1
9fee0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3e 30 20 ;. }. if( N>0
9fef0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 ){. sqlite3St
9ff00 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 rAccumAppend(pAc
9ff10 63 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20 4e 29 cum, zSpaces, N)
9ff20 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f ;. }.}../*.** O
9ff30 6e 20 6d 61 63 68 69 6e 65 73 20 77 69 74 68 20 n machines with
9ff40 61 20 73 6d 61 6c 6c 20 73 74 61 63 6b 20 73 69 a small stack si
9ff50 7a 65 2c 20 79 6f 75 20 63 61 6e 20 72 65 64 65 ze, you can rede
9ff60 66 69 6e 65 20 74 68 65 0a 2a 2a 20 53 51 4c 49 fine the.** SQLI
9ff70 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a TE_PRINT_BUF_SIZ
9ff80 45 20 74 6f 20 62 65 20 6c 65 73 73 20 74 68 61 E to be less tha
9ff90 6e 20 33 35 30 2e 0a 2a 2f 0a 23 69 66 6e 64 65 n 350..*/.#ifnde
9ffa0 66 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 f SQLITE_PRINT_B
9ffb0 55 46 5f 53 49 5a 45 0a 23 20 69 66 20 64 65 66 UF_SIZE.# if def
9ffc0 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 4d 41 4c ined(SQLITE_SMAL
9ffd0 4c 5f 53 54 41 43 4b 29 0a 23 20 20 20 64 65 66 L_STACK).# def
9ffe0 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 ine SQLITE_PRINT
9fff0 5f 42 55 46 5f 53 49 5a 45 20 35 30 0a 23 20 65 _BUF_SIZE 50.# e
a0000 6c 73 65 0a 23 20 20 20 64 65 66 69 6e 65 20 53 lse.# define S
a0010 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f QLITE_PRINT_BUF_
a0020 53 49 5a 45 20 33 35 30 0a 23 20 65 6e 64 69 66 SIZE 350.# endif
a0030 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 .#endif.#define
a0040 65 74 42 55 46 53 49 5a 45 20 53 51 4c 49 54 45 etBUFSIZE SQLITE
a0050 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 _PRINT_BUF_SIZE
a0060 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 /* Size of the
a0070 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f output buffer */
a0080 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 ../*.** The root
a0090 20 70 72 6f 67 72 61 6d 2e 20 20 41 6c 6c 20 76 program. All v
a00a0 61 72 69 61 74 69 6f 6e 73 20 63 61 6c 6c 20 74 ariations call t
a00b0 68 69 73 20 63 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 his core..**.**
a00c0 49 4e 50 55 54 53 3a 0a 2a 2a 20 20 20 66 75 6e INPUTS:.** fun
a00d0 63 20 20 20 54 68 69 73 20 69 73 20 61 20 70 6f c This is a po
a00e0 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 inter to a funct
a00f0 69 6f 6e 20 74 61 6b 69 6e 67 20 74 68 72 65 65 ion taking three
a0100 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 20 20 arguments.**
a0110 20 20 20 20 20 20 20 20 20 31 2e 20 41 20 70 6f 1. A po
a0120 69 6e 74 65 72 20 74 6f 20 61 6e 79 74 68 69 6e inter to anythin
a0130 67 2e 20 20 53 61 6d 65 20 61 73 20 74 68 65 20 g. Same as the
a0140 22 61 72 67 22 20 70 61 72 61 6d 65 74 65 72 2e "arg" parameter.
a0150 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 32 .** 2
a0160 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 . A pointer to t
a0170 68 65 20 6c 69 73 74 20 6f 66 20 63 68 61 72 61 he list of chara
a0180 63 74 65 72 73 20 74 6f 20 62 65 20 6f 75 74 70 cters to be outp
a0190 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ut.**
a01a0 20 20 20 20 28 4e 6f 74 65 2c 20 74 68 69 73 20 (Note, this
a01b0 6c 69 73 74 20 69 73 20 4e 4f 54 20 6e 75 6c 6c list is NOT null
a01c0 20 74 65 72 6d 69 6e 61 74 65 64 2e 29 0a 2a 2a terminated.).**
a01d0 20 20 20 20 20 20 20 20 20 20 20 20 33 2e 20 41 3. A
a01e0 6e 20 69 6e 74 65 67 65 72 20 6e 75 6d 62 65 72 n integer number
a01f0 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 74 of characters t
a0200 6f 20 62 65 20 6f 75 74 70 75 74 2e 0a 2a 2a 20 o be output..**
a0210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4e (N
a0220 6f 74 65 3a 20 54 68 69 73 20 6e 75 6d 62 65 72 ote: This number
a0230 20 6d 69 67 68 74 20 62 65 20 7a 65 72 6f 2e 29 might be zero.)
a0240 0a 2a 2a 0a 2a 2a 20 20 20 61 72 67 20 20 20 20 .**.** arg
a0250 54 68 69 73 20 69 73 20 74 68 65 20 70 6f 69 6e This is the poin
a0260 74 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 ter to anything
a0270 77 68 69 63 68 20 77 69 6c 6c 20 62 65 20 70 61 which will be pa
a0280 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 20 ssed as the.**
a0290 20 20 20 20 20 20 20 20 66 69 72 73 74 20 61 72 first ar
a02a0 67 75 6d 65 6e 74 20 74 6f 20 22 66 75 6e 63 22 gument to "func"
a02b0 2e 20 20 55 73 65 20 69 74 20 66 6f 72 20 77 68 . Use it for wh
a02c0 61 74 65 76 65 72 20 79 6f 75 20 6c 69 6b 65 2e atever you like.
a02d0 0a 2a 2a 0a 2a 2a 20 20 20 66 6d 74 20 20 20 20 .**.** fmt
a02e0 54 68 69 73 20 69 73 20 74 68 65 20 66 6f 72 6d This is the form
a02f0 61 74 20 73 74 72 69 6e 67 2c 20 61 73 20 69 6e at string, as in
a0300 20 74 68 65 20 75 73 75 61 6c 20 70 72 69 6e 74 the usual print
a0310 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 70 20 20 20 20 ..**.** ap
a0320 20 54 68 69 73 20 69 73 20 61 20 70 6f 69 6e 74 This is a point
a0330 65 72 20 74 6f 20 61 20 6c 69 73 74 20 6f 66 20 er to a list of
a0340 61 72 67 75 6d 65 6e 74 73 2e 20 20 53 61 6d 65 arguments. Same
a0350 20 61 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 as in.**
a0360 20 20 20 76 66 70 72 69 6e 74 2e 0a 2a 2a 0a 2a vfprint..**.*
a0370 2a 20 4f 55 54 50 55 54 53 3a 0a 2a 2a 20 20 20 * OUTPUTS:.**
a0380 20 20 20 20 20 20 20 54 68 65 20 72 65 74 75 72 The retur
a0390 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 74 n value is the t
a03a0 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 otal number of c
a03b0 68 61 72 61 63 74 65 72 73 20 73 65 6e 74 20 74 haracters sent t
a03c0 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 o.** th
a03d0 65 20 66 75 6e 63 74 69 6f 6e 20 22 66 75 6e 63 e function "func
a03e0 22 2e 20 20 52 65 74 75 72 6e 73 20 2d 31 20 6f ". Returns -1 o
a03f0 6e 20 61 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a n a error..**.**
a0400 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6f Note that the o
a0410 72 64 65 72 20 69 6e 20 77 68 69 63 68 20 61 75 rder in which au
a0420 74 6f 6d 61 74 69 63 20 76 61 72 69 61 62 6c 65 tomatic variable
a0430 73 20 61 72 65 20 64 65 63 6c 61 72 65 64 20 62 s are declared b
a0440 65 6c 6f 77 0a 2a 2a 20 73 65 65 6d 73 20 74 6f elow.** seems to
a0450 20 6d 61 6b 65 20 61 20 62 69 67 20 64 69 66 66 make a big diff
a0460 65 72 65 6e 63 65 20 69 6e 20 64 65 74 65 72 6d erence in determ
a0470 69 6e 69 6e 67 20 68 6f 77 20 66 61 73 74 20 74 ining how fast t
a0480 68 69 73 20 62 65 61 73 74 0a 2a 2a 20 77 69 6c his beast.** wil
a0490 6c 20 72 75 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 l run..*/.SQLITE
a04a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
a04b0 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 0a 20 lite3VXPrintf(.
a04c0 20 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 StrAccum *pAccu
a04d0 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m,
a04e0 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 /* Accumulat
a04f0 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a e results here *
a0500 2f 0a 20 20 69 6e 74 20 75 73 65 45 78 74 65 6e /. int useExten
a0510 64 65 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 ded,
a0520 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 20 /* Allow
a0530 65 78 74 65 6e 64 65 64 20 25 2d 63 6f 6e 76 65 extended %-conve
a0540 72 73 69 6f 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73 rsions */. cons
a0550 74 20 63 68 61 72 20 2a 66 6d 74 2c 20 20 20 20 t char *fmt,
a0560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
a0570 2a 20 46 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 * Format string
a0580 2a 2f 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 20 */. va_list ap
a0590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a05a0 20 20 20 20 20 20 20 20 2f 2a 20 61 72 67 75 6d /* argum
a05b0 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 ents */.){. int
a05c0 20 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
a05d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 /* Next
a05e0 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68 65 character in the
a05f0 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a format string *
a0600 2f 0a 20 20 63 68 61 72 20 2a 62 75 66 70 74 3b /. char *bufpt;
a0610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
a0620 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 * Pointer to the
a0630 20 63 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 66 conversion buff
a0640 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65 63 er */. int prec
a0650 69 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 ision;
a0660 20 20 20 2f 2a 20 50 72 65 63 69 73 69 6f 6e 20 /* Precision
a0670 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 of the current f
a0680 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 ield */. int le
a0690 6e 67 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 ngth;
a06a0 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f /* Length o
a06b0 66 20 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a 20 f the field */.
a06c0 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 int idx;
a06d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
a06e0 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 general purpose
a06f0 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f loop counter */
a0700 0a 20 20 69 6e 74 20 77 69 64 74 68 3b 20 20 20 . int width;
a0710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a0720 20 57 69 64 74 68 20 6f 66 20 74 68 65 20 63 75 Width of the cu
a0730 72 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a 20 rrent field */.
a0740 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c 65 66 etByte flag_lef
a0750 74 6a 75 73 74 69 66 79 3b 20 20 20 2f 2a 20 54 tjustify; /* T
a0760 72 75 65 20 69 66 20 22 2d 22 20 66 6c 61 67 20 rue if "-" flag
a0770 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 is present */.
a0780 65 74 42 79 74 65 20 66 6c 61 67 5f 70 6c 75 73 etByte flag_plus
a0790 73 69 67 6e 3b 20 20 20 20 20 20 2f 2a 20 54 72 sign; /* Tr
a07a0 75 65 20 69 66 20 22 2b 22 20 66 6c 61 67 20 69 ue if "+" flag i
a07b0 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 s present */. e
a07c0 74 42 79 74 65 20 66 6c 61 67 5f 62 6c 61 6e 6b tByte flag_blank
a07d0 73 69 67 6e 3b 20 20 20 20 20 2f 2a 20 54 72 75 sign; /* Tru
a07e0 65 20 69 66 20 22 20 22 20 66 6c 61 67 20 69 73 e if " " flag is
a07f0 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 present */. et
a0800 42 79 74 65 20 66 6c 61 67 5f 61 6c 74 65 72 6e Byte flag_altern
a0810 61 74 65 66 6f 72 6d 3b 20 2f 2a 20 54 72 75 65 ateform; /* True
a0820 20 69 66 20 22 23 22 20 66 6c 61 67 20 69 73 20 if "#" flag is
a0830 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 present */. etB
a0840 79 74 65 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d yte flag_altform
a0850 32 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 2; /* True
a0860 69 66 20 22 21 22 20 66 6c 61 67 20 69 73 20 70 if "!" flag is p
a0870 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 resent */. etBy
a0880 74 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 3b te flag_zeropad;
a0890 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
a08a0 66 20 66 69 65 6c 64 20 77 69 64 74 68 20 63 6f f field width co
a08b0 6e 73 74 61 6e 74 20 73 74 61 72 74 73 20 77 69 nstant starts wi
a08c0 74 68 20 7a 65 72 6f 20 2a 2f 0a 20 20 65 74 42 th zero */. etB
a08d0 79 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 3b 20 20 yte flag_long;
a08e0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
a08f0 69 66 20 22 6c 22 20 66 6c 61 67 20 69 73 20 70 if "l" flag is p
a0900 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 resent */. etBy
a0910 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 te flag_longlong
a0920 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 ; /* True i
a0930 66 20 74 68 65 20 22 6c 6c 22 20 66 6c 61 67 20 f the "ll" flag
a0940 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 is present */.
a0950 65 74 42 79 74 65 20 64 6f 6e 65 3b 20 20 20 20 etByte done;
a0960 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
a0970 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 66 op termination f
a0980 6c 61 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f lag */. sqlite_
a0990 75 69 6e 74 36 34 20 6c 6f 6e 67 76 61 6c 75 65 uint64 longvalue
a09a0 3b 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 ; /* Value for
a09b0 20 69 6e 74 65 67 65 72 20 74 79 70 65 73 20 2a integer types *
a09c0 2f 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 /. LONGDOUBLE_T
a09d0 59 50 45 20 72 65 61 6c 76 61 6c 75 65 3b 20 2f YPE realvalue; /
a09e0 2a 20 56 61 6c 75 65 20 66 6f 72 20 72 65 61 6c * Value for real
a09f0 20 74 79 70 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 types */. cons
a0a00 74 20 65 74 5f 69 6e 66 6f 20 2a 69 6e 66 6f 70 t et_info *infop
a0a10 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 ; /* Pointe
a0a20 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 r to the appropr
a0a30 69 61 74 65 20 69 6e 66 6f 20 73 74 72 75 63 74 iate info struct
a0a40 75 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 62 75 ure */. char bu
a0a50 66 5b 65 74 42 55 46 53 49 5a 45 5d 3b 20 20 20 f[etBUFSIZE];
a0a60 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f /* Conversio
a0a70 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 63 68 n buffer */. ch
a0a80 61 72 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 ar prefix;
a0a90 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 /* Pref
a0aa0 69 78 20 63 68 61 72 61 63 74 65 72 2e 20 20 22 ix character. "
a0ab0 2b 22 20 6f 72 20 22 2d 22 20 6f 72 20 22 20 22 +" or "-" or " "
a0ac0 20 6f 72 20 27 5c 30 27 2e 20 2a 2f 0a 20 20 65 or '\0'. */. e
a0ad0 74 42 79 74 65 20 78 74 79 70 65 20 3d 20 30 3b tByte xtype = 0;
a0ae0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e /* Con
a0af0 76 65 72 73 69 6f 6e 20 70 61 72 61 64 69 67 6d version paradigm
a0b00 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 78 74 */. char *zExt
a0b10 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ra;
a0b20 20 2f 2a 20 45 78 74 72 61 20 6d 65 6d 6f 72 79 /* Extra memory
a0b30 20 75 73 65 64 20 66 6f 72 20 65 74 54 43 4c 45 used for etTCLE
a0b40 53 43 41 50 45 20 63 6f 6e 76 65 72 73 69 6f 6e SCAPE conversion
a0b50 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c s */.#ifndef SQL
a0b60 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
a0b70 47 5f 50 4f 49 4e 54 0a 20 20 69 6e 74 20 20 65 G_POINT. int e
a0b80 78 70 2c 20 65 32 3b 20 20 20 20 20 20 20 20 20 xp, e2;
a0b90 20 20 20 20 20 2f 2a 20 65 78 70 6f 6e 65 6e 74 /* exponent
a0ba0 20 6f 66 20 72 65 61 6c 20 6e 75 6d 62 65 72 73 of real numbers
a0bb0 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 6f 75 */. double rou
a0bc0 6e 64 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 nder;
a0bd0 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 72 6f 75 /* Used for rou
a0be0 6e 64 69 6e 67 20 66 6c 6f 61 74 69 6e 67 20 70 nding floating p
a0bf0 6f 69 6e 74 20 76 61 6c 75 65 73 20 2a 2f 0a 20 oint values */.
a0c00 20 65 74 42 79 74 65 20 66 6c 61 67 5f 64 70 3b etByte flag_dp;
a0c10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
a0c20 72 75 65 20 69 66 20 64 65 63 69 6d 61 6c 20 70 rue if decimal p
a0c30 6f 69 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 73 oint should be s
a0c40 68 6f 77 6e 20 2a 2f 0a 20 20 65 74 42 79 74 65 hown */. etByte
a0c50 20 66 6c 61 67 5f 72 74 7a 3b 20 20 20 20 20 20 flag_rtz;
a0c60 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a0c70 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 73 trailing zeros s
a0c80 68 6f 75 6c 64 20 62 65 20 72 65 6d 6f 76 65 64 hould be removed
a0c90 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 */. etByte fla
a0ca0 67 5f 65 78 70 3b 20 20 20 20 20 20 20 20 20 20 g_exp;
a0cb0 20 2f 2a 20 54 72 75 65 20 74 6f 20 66 6f 72 63 /* True to forc
a0cc0 65 20 64 69 73 70 6c 61 79 20 6f 66 20 74 68 65 e display of the
a0cd0 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 69 exponent */. i
a0ce0 6e 74 20 6e 73 64 3b 20 20 20 20 20 20 20 20 20 nt nsd;
a0cf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
a0d00 62 65 72 20 6f 66 20 73 69 67 6e 69 66 69 63 61 ber of significa
a0d10 6e 74 20 64 69 67 69 74 73 20 72 65 74 75 72 6e nt digits return
a0d20 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 ed */.#endif..
a0d30 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 20 20 62 75 length = 0;. bu
a0d40 66 70 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 3b fpt = 0;. for(;
a0d50 20 28 63 3d 28 2a 66 6d 74 29 29 21 3d 30 3b 20 (c=(*fmt))!=0;
a0d60 2b 2b 66 6d 74 29 7b 0a 20 20 20 20 69 66 28 20 ++fmt){. if(
a0d70 63 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 c!='%' ){.
a0d80 69 6e 74 20 61 6d 74 3b 0a 20 20 20 20 20 20 62 int amt;. b
a0d90 75 66 70 74 20 3d 20 28 63 68 61 72 20 2a 29 66 ufpt = (char *)f
a0da0 6d 74 3b 0a 20 20 20 20 20 20 61 6d 74 20 3d 20 mt;. amt =
a0db0 31 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 1;. while(
a0dc0 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 27 25 (c=(*++fmt))!='%
a0dd0 27 20 26 26 20 63 21 3d 30 20 29 20 61 6d 74 2b ' && c!=0 ) amt+
a0de0 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 +;. sqlite3
a0df0 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 StrAccumAppend(p
a0e00 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 61 6d Accum, bufpt, am
a0e10 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d t);. if( c=
a0e20 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 =0 ) break;.
a0e30 7d 0a 20 20 20 20 69 66 28 20 28 63 3d 28 2a 2b }. if( (c=(*+
a0e40 2b 66 6d 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20 +fmt))==0 ){.
a0e50 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
a0e60 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c umAppend(pAccum,
a0e70 20 22 25 22 2c 20 31 29 3b 0a 20 20 20 20 20 20 "%", 1);.
a0e80 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
a0e90 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 61 /* Find out wha
a0ea0 74 20 66 6c 61 67 73 20 61 72 65 20 70 72 65 73 t flags are pres
a0eb0 65 6e 74 20 2a 2f 0a 20 20 20 20 66 6c 61 67 5f ent */. flag_
a0ec0 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 66 6c leftjustify = fl
a0ed0 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 66 6c ag_plussign = fl
a0ee0 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 0a ag_blanksign = .
a0ef0 20 20 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e flag_altern
a0f00 61 74 65 66 6f 72 6d 20 3d 20 66 6c 61 67 5f 61 ateform = flag_a
a0f10 6c 74 66 6f 72 6d 32 20 3d 20 66 6c 61 67 5f 7a ltform2 = flag_z
a0f20 65 72 6f 70 61 64 20 3d 20 30 3b 0a 20 20 20 20 eropad = 0;.
a0f30 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 64 6f done = 0;. do
a0f40 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20 {. switch(
a0f50 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 c ){. cas
a0f60 65 20 27 2d 27 3a 20 20 20 66 6c 61 67 5f 6c 65 e '-': flag_le
a0f70 66 74 6a 75 73 74 69 66 79 20 3d 20 31 3b 20 20 ftjustify = 1;
a0f80 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a0f90 20 20 63 61 73 65 20 27 2b 27 3a 20 20 20 66 6c case '+': fl
a0fa0 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 31 3b ag_plussign = 1;
a0fb0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
a0fc0 20 20 20 20 20 20 20 63 61 73 65 20 27 20 27 3a case ' ':
a0fd0 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 flag_blanksig
a0fe0 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 62 72 65 n = 1; bre
a0ff0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
a1000 20 27 23 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 '#': flag_alt
a1010 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 31 3b 20 ernateform = 1;
a1020 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
a1030 20 63 61 73 65 20 27 21 27 3a 20 20 20 66 6c 61 case '!': fla
a1040 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20 31 3b 20 g_altform2 = 1;
a1050 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a1060 20 20 20 20 20 20 63 61 73 65 20 27 30 27 3a 20 case '0':
a1070 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 3d flag_zeropad =
a1080 20 31 3b 20 20 20 20 20 20 20 20 20 62 72 65 61 1; brea
a1090 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 66 61 75 k;. defau
a10a0 6c 74 3a 20 20 20 20 64 6f 6e 65 20 3d 20 31 3b lt: done = 1;
a10b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a10c0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
a10d0 20 20 20 20 7d 77 68 69 6c 65 28 20 21 64 6f 6e }while( !don
a10e0 65 20 26 26 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 e && (c=(*++fmt)
a10f0 29 21 3d 30 20 29 3b 0a 20 20 20 20 2f 2a 20 47 )!=0 );. /* G
a1100 65 74 20 74 68 65 20 66 69 65 6c 64 20 77 69 64 et the field wid
a1110 74 68 20 2a 2f 0a 20 20 20 20 77 69 64 74 68 20 th */. width
a1120 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 63 3d 3d = 0;. if( c==
a1130 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 77 69 64 '*' ){. wid
a1140 74 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 th = va_arg(ap,i
a1150 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 77 nt);. if( w
a1160 69 64 74 68 3c 30 20 29 7b 0a 20 20 20 20 20 20 idth<0 ){.
a1170 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 flag_leftjusti
a1180 66 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 fy = 1;.
a1190 77 69 64 74 68 20 3d 20 2d 77 69 64 74 68 3b 0a width = -width;.
a11a0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 20 }. c
a11b0 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 7d 65 = *++fmt;. }e
a11c0 6c 73 65 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 lse{. while
a11d0 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 ( c>='0' && c<='
a11e0 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 77 69 9' ){. wi
a11f0 64 74 68 20 3d 20 77 69 64 74 68 2a 31 30 20 2b dth = width*10 +
a1200 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 c - '0';.
a1210 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 c = *++fmt;.
a1220 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
a1230 69 66 28 20 77 69 64 74 68 20 3e 20 65 74 42 55 if( width > etBU
a1240 46 53 49 5a 45 2d 31 30 20 29 7b 0a 20 20 20 20 FSIZE-10 ){.
a1250 20 20 77 69 64 74 68 20 3d 20 65 74 42 55 46 53 width = etBUFS
a1260 49 5a 45 2d 31 30 3b 0a 20 20 20 20 7d 0a 20 20 IZE-10;. }.
a1270 20 20 2f 2a 20 47 65 74 20 74 68 65 20 70 72 65 /* Get the pre
a1280 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 cision */. if
a1290 28 20 63 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 ( c=='.' ){.
a12a0 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 30 3b precision = 0;
a12b0 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d . c = *++fm
a12c0 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d t;. if( c==
a12d0 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 70 '*' ){. p
a12e0 72 65 63 69 73 69 6f 6e 20 3d 20 76 61 5f 61 72 recision = va_ar
a12f0 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 g(ap,int);.
a1300 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e if( precision
a1310 3c 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d <0 ) precision =
a1320 20 2d 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20 -precision;.
a1330 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b c = *++fmt;
a1340 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
a1350 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d while( c>=
a1360 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b '0' && c<='9' ){
a1370 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 63 69 . preci
a1380 73 69 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f 6e sion = precision
a1390 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 *10 + c - '0';.
a13a0 20 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b c = *++
a13b0 66 6d 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 fmt;. }.
a13c0 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
a13d0 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69 6f {. precisio
a13e0 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 n = -1;. }.
a13f0 20 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f 6e /* Get the con
a1400 76 65 72 73 69 6f 6e 20 74 79 70 65 20 6d 6f 64 version type mod
a1410 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 66 28 ifier */. if(
a1420 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20 20 c=='l' ){.
a1430 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b 0a flag_long = 1;.
a1440 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 c = *++fmt
a1450 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 ;. if( c=='
a1460 6c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c l' ){. fl
a1470 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31 3b ag_longlong = 1;
a1480 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b . c = *++
a1490 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 fmt;. }else
a14a0 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c {. flag_l
a14b0 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 20 onglong = 0;.
a14c0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
a14d0 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 flag_long
a14e0 3d 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 = flag_longlong
a14f0 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f = 0;. }. /
a1500 2a 20 46 65 74 63 68 20 74 68 65 20 69 6e 66 6f * Fetch the info
a1510 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66 entry for the f
a1520 69 65 6c 64 20 2a 2f 0a 20 20 20 20 69 6e 66 6f ield */. info
a1530 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 30 5d 3b p = &fmtinfo[0];
a1540 0a 20 20 20 20 78 74 79 70 65 20 3d 20 65 74 49 . xtype = etI
a1550 4e 56 41 4c 49 44 3b 0a 20 20 20 20 66 6f 72 28 NVALID;. for(
a1560 69 64 78 3d 30 3b 20 69 64 78 3c 41 72 72 61 79 idx=0; idx<Array
a1570 53 69 7a 65 28 66 6d 74 69 6e 66 6f 29 3b 20 69 Size(fmtinfo); i
a1580 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 dx++){. if(
a1590 20 63 3d 3d 66 6d 74 69 6e 66 6f 5b 69 64 78 5d c==fmtinfo[idx]
a15a0 2e 66 6d 74 74 79 70 65 20 29 7b 0a 20 20 20 20 .fmttype ){.
a15b0 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74 infop = &fmt
a15c0 69 6e 66 6f 5b 69 64 78 5d 3b 0a 20 20 20 20 20 info[idx];.
a15d0 20 20 20 69 66 28 20 75 73 65 45 78 74 65 6e 64 if( useExtend
a15e0 65 64 20 7c 7c 20 28 69 6e 66 6f 70 2d 3e 66 6c ed || (infop->fl
a15f0 61 67 73 20 26 20 46 4c 41 47 5f 49 4e 54 45 52 ags & FLAG_INTER
a1600 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 N)==0 ){.
a1610 20 20 20 78 74 79 70 65 20 3d 20 69 6e 66 6f 70 xtype = infop
a1620 2d 3e 74 79 70 65 3b 0a 20 20 20 20 20 20 20 20 ->type;.
a1630 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
a1640 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 return;.
a1650 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b }. break
a1660 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
a1670 20 20 20 20 7a 45 78 74 72 61 20 3d 20 30 3b 0a zExtra = 0;.
a1680 0a 0a 20 20 20 20 2f 2a 20 4c 69 6d 69 74 20 74 .. /* Limit t
a1690 68 65 20 70 72 65 63 69 73 69 6f 6e 20 74 6f 20 he precision to
a16a0 70 72 65 76 65 6e 74 20 6f 76 65 72 66 6c 6f 77 prevent overflow
a16b0 69 6e 67 20 62 75 66 5b 5d 20 64 75 72 69 6e 67 ing buf[] during
a16c0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 conversion */.
a16d0 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e if( precision
a16e0 3e 65 74 42 55 46 53 49 5a 45 2d 34 30 20 26 26 >etBUFSIZE-40 &&
a16f0 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 (infop->flags &
a1700 20 46 4c 41 47 5f 53 54 52 49 4e 47 29 3d 3d 30 FLAG_STRING)==0
a1710 20 29 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 ){. precis
a1720 69 6f 6e 20 3d 20 65 74 42 55 46 53 49 5a 45 2d ion = etBUFSIZE-
a1730 34 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 40;. }.. /
a1740 2a 0a 20 20 20 20 2a 2a 20 41 74 20 74 68 69 73 *. ** At this
a1750 20 70 6f 69 6e 74 2c 20 76 61 72 69 61 62 6c 65 point, variable
a1760 73 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 s are initialize
a1770 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 d as follows:.
a1780 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 66 6c **. ** fl
a1790 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d ag_alternateform
a17a0 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 TRUE i
a17b0 66 20 61 20 27 23 27 20 69 73 20 70 72 65 73 65 f a '#' is prese
a17c0 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 nt.. ** fla
a17d0 67 5f 61 6c 74 66 6f 72 6d 32 20 20 20 20 20 20 g_altform2
a17e0 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 TRUE if
a17f0 20 61 20 27 21 27 20 69 73 20 70 72 65 73 65 6e a '!' is presen
a1800 74 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 t.. ** flag
a1810 5f 70 6c 75 73 73 69 67 6e 20 20 20 20 20 20 20 _plussign
a1820 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 TRUE if
a1830 61 20 27 2b 27 20 69 73 20 70 72 65 73 65 6e 74 a '+' is present
a1840 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f .. ** flag_
a1850 6c 65 66 74 6a 75 73 74 69 66 79 20 20 20 20 20 leftjustify
a1860 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 61 TRUE if a
a1870 20 27 2d 27 20 69 73 20 70 72 65 73 65 6e 74 20 '-' is present
a1880 6f 72 20 69 66 20 74 68 65 0a 20 20 20 20 2a 2a or if the. **
a1890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a18a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 f
a18b0 69 65 6c 64 20 77 69 64 74 68 20 77 61 73 20 6e ield width was n
a18c0 65 67 61 74 69 76 65 2e 0a 20 20 20 20 2a 2a 20 egative.. **
a18d0 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 20 flag_zeropad
a18e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 TR
a18f0 55 45 20 69 66 20 74 68 65 20 77 69 64 74 68 20 UE if the width
a1900 62 65 67 61 6e 20 77 69 74 68 20 30 2e 0a 20 20 began with 0..
a1910 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 ** flag_long
a1920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1930 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20 6c TRUE if the l
a1940 65 74 74 65 72 20 27 6c 27 20 28 65 6c 6c 29 20 etter 'l' (ell)
a1950 70 72 65 66 69 78 65 64 0a 20 20 20 20 2a 2a 20 prefixed. **
a1960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
a1980 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68 61 e conversion cha
a1990 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20 20 racter.. **
a19a0 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 20 flag_longlong
a19b0 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 TRU
a19c0 45 20 69 66 20 74 68 65 20 6c 65 74 74 65 72 20 E if the letter
a19d0 27 6c 6c 27 20 28 65 6c 6c 20 65 6c 6c 29 20 70 'll' (ell ell) p
a19e0 72 65 66 69 78 65 64 0a 20 20 20 20 2a 2a 20 20 refixed. **
a19f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1a00 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 the
a1a10 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68 61 72 conversion char
a1a20 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20 20 20 acter.. **
a1a30 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 20 flag_blanksign
a1a40 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45 TRUE
a1a50 20 69 66 20 61 20 27 20 27 20 69 73 20 70 72 65 if a ' ' is pre
a1a60 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 77 sent.. ** w
a1a70 69 64 74 68 20 20 20 20 20 20 20 20 20 20 20 20 idth
a1a80 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 73 The s
a1a90 70 65 63 69 66 69 65 64 20 66 69 65 6c 64 20 77 pecified field w
a1aa0 69 64 74 68 2e 20 20 54 68 69 73 20 69 73 0a 20 idth. This is.
a1ab0 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **
a1ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1ad0 20 20 20 20 61 6c 77 61 79 73 20 6e 6f 6e 2d 6e always non-n
a1ae0 65 67 61 74 69 76 65 2e 20 20 5a 65 72 6f 20 69 egative. Zero i
a1af0 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 0a 20 s the default..
a1b00 20 20 20 2a 2a 20 20 20 70 72 65 63 69 73 69 6f ** precisio
a1b10 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
a1b20 20 20 20 20 54 68 65 20 73 70 65 63 69 66 69 65 The specifie
a1b30 64 20 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68 d precision. Th
a1b40 65 20 64 65 66 61 75 6c 74 0a 20 20 20 20 2a 2a e default. **
a1b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
a1b70 73 20 2d 31 2e 0a 20 20 20 20 2a 2a 20 20 20 78 s -1.. ** x
a1b80 74 79 70 65 20 20 20 20 20 20 20 20 20 20 20 20 type
a1b90 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 63 The c
a1ba0 6c 61 73 73 20 6f 66 20 74 68 65 20 63 6f 6e 76 lass of the conv
a1bb0 65 72 73 69 6f 6e 2e 0a 20 20 20 20 2a 2a 20 20 ersion.. **
a1bc0 20 69 6e 66 6f 70 20 20 20 20 20 20 20 20 20 20 infop
a1bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 50 6f 69 Poi
a1be0 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70 70 72 nter to the appr
a1bf0 6f 70 72 69 61 74 65 20 69 6e 66 6f 20 73 74 72 opriate info str
a1c00 75 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 uct.. */.
a1c10 73 77 69 74 63 68 28 20 78 74 79 70 65 20 29 7b switch( xtype ){
a1c20 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 50 4f . case etPO
a1c30 49 4e 54 45 52 3a 0a 20 20 20 20 20 20 20 20 66 INTER:. f
a1c40 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 73 lag_longlong = s
a1c50 69 7a 65 6f 66 28 63 68 61 72 2a 29 3d 3d 73 69 izeof(char*)==si
a1c60 7a 65 6f 66 28 69 36 34 29 3b 0a 20 20 20 20 20 zeof(i64);.
a1c70 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 73 flag_long = s
a1c80 69 7a 65 6f 66 28 63 68 61 72 2a 29 3d 3d 73 69 izeof(char*)==si
a1c90 7a 65 6f 66 28 6c 6f 6e 67 20 69 6e 74 29 3b 0a zeof(long int);.
a1ca0 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 /* Fall
a1cb0 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 74 68 65 through into the
a1cc0 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 next case */.
a1cd0 20 20 20 20 63 61 73 65 20 65 74 4f 52 44 49 4e case etORDIN
a1ce0 41 4c 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 AL:. case e
a1cf0 74 52 41 44 49 58 3a 0a 20 20 20 20 20 20 20 20 tRADIX:.
a1d00 69 66 28 20 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 if( infop->flags
a1d10 20 26 20 46 4c 41 47 5f 53 49 47 4e 45 44 20 29 & FLAG_SIGNED )
a1d20 7b 0a 20 20 20 20 20 20 20 20 20 20 69 36 34 20 {. i64
a1d30 76 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 v;. if(
a1d40 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 flag_longlong )
a1d50 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 20 {. v
a1d60 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 36 34 29 = va_arg(ap,i64)
a1d70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
a1d80 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 20 e if( flag_long
a1d90 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 ){. v
a1da0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 6c 6f 6e = va_arg(ap,lon
a1db0 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 g int);.
a1dc0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
a1dd0 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28 v = va_arg(
a1de0 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 ap,int);.
a1df0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 }. i
a1e00 66 28 20 76 3c 30 20 29 7b 0a 20 20 20 20 20 20 f( v<0 ){.
a1e10 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 longvalue
a1e20 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20 20 20 20 = -v;.
a1e30 20 20 70 72 65 66 69 78 20 3d 20 27 2d 27 3b 0a prefix = '-';.
a1e40 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
a1e50 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e . lon
a1e60 67 76 61 6c 75 65 20 3d 20 76 3b 0a 20 20 20 20 gvalue = v;.
a1e70 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 if( flag
a1e80 5f 70 6c 75 73 73 69 67 6e 20 29 20 20 20 20 20 _plussign )
a1e90 20 20 20 70 72 65 66 69 78 20 3d 20 27 2b 27 3b prefix = '+';
a1ea0 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 6c 73 . els
a1eb0 65 20 69 66 28 20 66 6c 61 67 5f 62 6c 61 6e 6b e if( flag_blank
a1ec0 73 69 67 6e 20 29 20 20 70 72 65 66 69 78 20 3d sign ) prefix =
a1ed0 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 20 20 ' ';.
a1ee0 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 else
a1ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65 pre
a1f00 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 fix = 0;.
a1f10 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c }. }el
a1f20 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 se{. if
a1f30 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 ( flag_longlong
a1f40 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c ){. l
a1f50 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 ongvalue = va_ar
a1f60 67 28 61 70 2c 75 36 34 29 3b 0a 20 20 20 20 20 g(ap,u64);.
a1f70 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 }else if( f
a1f80 6c 61 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 lag_long ){.
a1f90 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 longvalu
a1fa0 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 6e e = va_arg(ap,un
a1fb0 73 69 67 6e 65 64 20 6c 6f 6e 67 20 69 6e 74 29 signed long int)
a1fc0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
a1fd0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c e{. l
a1fe0 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 ongvalue = va_ar
a1ff0 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 69 6e g(ap,unsigned in
a2000 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a t);. }.
a2010 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 prefix
a2020 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 0;. }.
a2030 20 20 20 20 20 20 20 20 69 66 28 20 6c 6f 6e 67 if( long
a2040 76 61 6c 75 65 3d 3d 30 20 29 20 66 6c 61 67 5f value==0 ) flag_
a2050 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 alternateform =
a2060 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 0;. if( f
a2070 6c 61 67 5f 7a 65 72 6f 70 61 64 20 26 26 20 70 lag_zeropad && p
a2080 72 65 63 69 73 69 6f 6e 3c 77 69 64 74 68 2d 28 recision<width-(
a2090 70 72 65 66 69 78 21 3d 30 29 20 29 7b 0a 20 20 prefix!=0) ){.
a20a0 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69 6f precisio
a20b0 6e 20 3d 20 77 69 64 74 68 2d 28 70 72 65 66 69 n = width-(prefi
a20c0 78 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 7d x!=0);. }
a20d0 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d . bufpt =
a20e0 20 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d &buf[etBUFSIZE-
a20f0 31 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 1];. if(
a2100 78 74 79 70 65 3d 3d 65 74 4f 52 44 49 4e 41 4c xtype==etORDINAL
a2110 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74 ){. st
a2120 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 atic const char
a2130 7a 4f 72 64 5b 5d 20 3d 20 22 74 68 73 74 6e 64 zOrd[] = "thstnd
a2140 72 64 22 3b 0a 20 20 20 20 20 20 20 20 20 20 69 rd";. i
a2150 6e 74 20 78 20 3d 20 28 69 6e 74 29 28 6c 6f 6e nt x = (int)(lon
a2160 67 76 61 6c 75 65 20 25 20 31 30 29 3b 0a 20 20 gvalue % 10);.
a2170 20 20 20 20 20 20 20 20 69 66 28 20 78 3e 3d 34 if( x>=4
a2180 20 7c 7c 20 28 6c 6f 6e 67 76 61 6c 75 65 2f 31 || (longvalue/1
a2190 30 29 25 31 30 3d 3d 31 20 29 7b 0a 20 20 20 20 0)%10==1 ){.
a21a0 20 20 20 20 20 20 20 20 78 20 3d 20 30 3b 0a 20 x = 0;.
a21b0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
a21c0 20 20 20 20 20 62 75 66 5b 65 74 42 55 46 53 49 buf[etBUFSI
a21d0 5a 45 2d 33 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32 ZE-3] = zOrd[x*2
a21e0 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 ];. buf
a21f0 5b 65 74 42 55 46 53 49 5a 45 2d 32 5d 20 3d 20 [etBUFSIZE-2] =
a2200 7a 4f 72 64 5b 78 2a 32 2b 31 5d 3b 0a 20 20 20 zOrd[x*2+1];.
a2210 20 20 20 20 20 20 20 62 75 66 70 74 20 2d 3d 20 bufpt -=
a2220 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 2;. }.
a2230 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 {.
a2240 20 72 65 67 69 73 74 65 72 20 63 6f 6e 73 74 20 register const
a2250 63 68 61 72 20 2a 63 73 65 74 3b 20 20 20 20 20 char *cset;
a2260 20 2f 2a 20 55 73 65 20 72 65 67 69 73 74 65 72 /* Use register
a2270 73 20 66 6f 72 20 73 70 65 65 64 20 2a 2f 0a 20 s for speed */.
a2280 20 20 20 20 20 20 20 20 20 72 65 67 69 73 74 65 registe
a2290 72 20 69 6e 74 20 62 61 73 65 3b 0a 20 20 20 20 r int base;.
a22a0 20 20 20 20 20 20 63 73 65 74 20 3d 20 26 61 44 cset = &aD
a22b0 69 67 69 74 73 5b 69 6e 66 6f 70 2d 3e 63 68 61 igits[infop->cha
a22c0 72 73 65 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 rset];.
a22d0 20 62 61 73 65 20 3d 20 69 6e 66 6f 70 2d 3e 62 base = infop->b
a22e0 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 64 ase;. d
a22f0 6f 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 o{
a2300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2310 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a2320 43 6f 6e 76 65 72 74 20 74 6f 20 61 73 63 69 69 Convert to ascii
a2330 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
a2340 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 63 73 65 *(--bufpt) = cse
a2350 74 5b 6c 6f 6e 67 76 61 6c 75 65 25 62 61 73 65 t[longvalue%base
a2360 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c ];. l
a2370 6f 6e 67 76 61 6c 75 65 20 3d 20 6c 6f 6e 67 76 ongvalue = longv
a2380 61 6c 75 65 2f 62 61 73 65 3b 0a 20 20 20 20 20 alue/base;.
a2390 20 20 20 20 20 7d 77 68 69 6c 65 28 20 6c 6f 6e }while( lon
a23a0 67 76 61 6c 75 65 3e 30 20 29 3b 0a 20 20 20 20 gvalue>0 );.
a23b0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c 65 }. le
a23c0 6e 67 74 68 20 3d 20 28 69 6e 74 29 28 26 62 75 ngth = (int)(&bu
a23d0 66 5b 65 74 42 55 46 53 49 5a 45 2d 31 5d 2d 62 f[etBUFSIZE-1]-b
a23e0 75 66 70 74 29 3b 0a 20 20 20 20 20 20 20 20 66 ufpt);. f
a23f0 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f 6e or(idx=precision
a2400 2d 6c 65 6e 67 74 68 3b 20 69 64 78 3e 30 3b 20 -length; idx>0;
a2410 69 64 78 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 idx--){.
a2420 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 27 *(--bufpt) = '
a2430 30 27 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0';
a2440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2450 2f 2a 20 5a 65 72 6f 20 70 61 64 20 2a 2f 0a 20 /* Zero pad */.
a2460 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a2470 20 69 66 28 20 70 72 65 66 69 78 20 29 20 2a 28 if( prefix ) *(
a2480 2d 2d 62 75 66 70 74 29 20 3d 20 70 72 65 66 69 --bufpt) = prefi
a2490 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x;
a24a0 20 2f 2a 20 41 64 64 20 73 69 67 6e 20 2a 2f 0a /* Add sign */.
a24b0 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 if( flag
a24c0 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 26 _alternateform &
a24d0 26 20 69 6e 66 6f 70 2d 3e 70 72 65 66 69 78 20 & infop->prefix
a24e0 29 7b 20 20 20 20 20 20 2f 2a 20 41 64 64 20 22 ){ /* Add "
a24f0 30 22 20 6f 72 20 22 30 78 22 20 2a 2f 0a 20 20 0" or "0x" */.
a2500 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 const ch
a2510 61 72 20 2a 70 72 65 3b 0a 20 20 20 20 20 20 20 ar *pre;.
a2520 20 20 20 63 68 61 72 20 78 3b 0a 20 20 20 20 20 char x;.
a2530 20 20 20 20 20 70 72 65 20 3d 20 26 61 50 72 65 pre = &aPre
a2540 66 69 78 5b 69 6e 66 6f 70 2d 3e 70 72 65 66 69 fix[infop->prefi
a2550 78 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f x];. fo
a2560 72 28 3b 20 28 78 3d 28 2a 70 72 65 29 29 21 3d r(; (x=(*pre))!=
a2570 30 3b 20 70 72 65 2b 2b 29 20 2a 28 2d 2d 62 75 0; pre++) *(--bu
a2580 66 70 74 29 20 3d 20 78 3b 0a 20 20 20 20 20 20 fpt) = x;.
a2590 20 20 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 }. leng
a25a0 74 68 20 3d 20 28 69 6e 74 29 28 26 62 75 66 5b th = (int)(&buf[
a25b0 65 74 42 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66 etBUFSIZE-1]-buf
a25c0 70 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 pt);. bre
a25d0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 ak;. case e
a25e0 74 46 4c 4f 41 54 3a 0a 20 20 20 20 20 20 63 61 tFLOAT:. ca
a25f0 73 65 20 65 74 45 58 50 3a 0a 20 20 20 20 20 20 se etEXP:.
a2600 63 61 73 65 20 65 74 47 45 4e 45 52 49 43 3a 0a case etGENERIC:.
a2610 20 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 realvalu
a2620 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 64 6f e = va_arg(ap,do
a2630 75 62 6c 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 uble);.#ifndef S
a2640 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
a2650 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 ING_POINT.
a2660 20 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3c if( precision<
a2670 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 0 ) precision =
a2680 36 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 6; /* Se
a2690 74 20 64 65 66 61 75 6c 74 20 70 72 65 63 69 73 t default precis
a26a0 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 ion */. i
a26b0 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 f( precision>etB
a26c0 55 46 53 49 5a 45 2f 32 2d 31 30 20 29 20 70 72 UFSIZE/2-10 ) pr
a26d0 65 63 69 73 69 6f 6e 20 3d 20 65 74 42 55 46 53 ecision = etBUFS
a26e0 49 5a 45 2f 32 2d 31 30 3b 0a 20 20 20 20 20 20 IZE/2-10;.
a26f0 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65 3c if( realvalue<
a2700 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 0.0 ){.
a2710 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 2d 72 65 realvalue = -re
a2720 61 6c 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 alvalue;.
a2730 20 20 20 70 72 65 66 69 78 20 3d 20 27 2d 27 3b prefix = '-';
a2740 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
a2750 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c if( fl
a2760 61 67 5f 70 6c 75 73 73 69 67 6e 20 29 20 20 20 ag_plussign )
a2770 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 prefix =
a2780 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 65 '+';. e
a2790 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 62 6c 61 lse if( flag_bla
a27a0 6e 6b 73 69 67 6e 20 29 20 20 20 20 70 72 65 66 nksign ) pref
a27b0 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 ix = ' ';.
a27c0 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 else
a27d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a27e0 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 prefix = 0;.
a27f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
a2800 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 f( xtype==etGENE
a2810 52 49 43 20 26 26 20 70 72 65 63 69 73 69 6f 6e RIC && precision
a2820 3e 30 20 29 20 70 72 65 63 69 73 69 6f 6e 2d 2d >0 ) precision--
a2830 3b 0a 23 69 66 20 30 0a 20 20 20 20 20 20 20 20 ;.#if 0.
a2840 2f 2a 20 52 6f 75 6e 64 69 6e 67 20 77 6f 72 6b /* Rounding work
a2850 73 20 6c 69 6b 65 20 42 53 44 20 77 68 65 6e 20 s like BSD when
a2860 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 30 2e 34 the constant 0.4
a2870 39 39 39 20 69 73 20 75 73 65 64 2e 20 20 57 69 999 is used. Wi
a2880 65 72 64 21 20 2a 2f 0a 20 20 20 20 20 20 20 20 erd! */.
a2890 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f for(idx=precisio
a28a0 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 34 39 39 n, rounder=0.499
a28b0 39 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 9; idx>0; idx--,
a28c0 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 3b 0a rounder*=0.1);.
a28d0 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 2f 2a #else. /*
a28e0 20 49 74 20 6d 61 6b 65 73 20 6d 6f 72 65 20 73 It makes more s
a28f0 65 6e 73 65 20 74 6f 20 75 73 65 20 30 2e 35 20 ense to use 0.5
a2900 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 */. for(i
a2910 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f dx=precision, ro
a2920 75 6e 64 65 72 3d 30 2e 35 3b 20 69 64 78 3e 30 under=0.5; idx>0
a2930 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 ; idx--, rounder
a2940 2a 3d 30 2e 31 29 7b 7d 0a 23 65 6e 64 69 66 0a *=0.1){}.#endif.
a2950 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 if( xtyp
a2960 65 3d 3d 65 74 46 4c 4f 41 54 20 29 20 72 65 61 e==etFLOAT ) rea
a2970 6c 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 lvalue += rounde
a2980 72 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f r;. /* No
a2990 72 6d 61 6c 69 7a 65 20 72 65 61 6c 76 61 6c 75 rmalize realvalu
a29a0 65 20 74 6f 20 77 69 74 68 69 6e 20 31 30 2e 30 e to within 10.0
a29b0 20 3e 20 72 65 61 6c 76 61 6c 75 65 20 3e 3d 20 > realvalue >=
a29c0 31 2e 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 65 1.0 */. e
a29d0 78 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 xp = 0;.
a29e0 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e if( sqlite3IsNaN
a29f0 28 28 64 6f 75 62 6c 65 29 72 65 61 6c 76 61 6c ((double)realval
a2a00 75 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ue) ){.
a2a10 20 62 75 66 70 74 20 3d 20 22 4e 61 4e 22 3b 0a bufpt = "NaN";.
a2a20 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 length
a2a30 20 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 = 3;.
a2a40 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d break;. }
a2a50 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61 . if( rea
a2a60 6c 76 61 6c 75 65 3e 30 2e 30 20 29 7b 0a 20 20 lvalue>0.0 ){.
a2a70 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 while( r
a2a80 65 61 6c 76 61 6c 75 65 3e 3d 31 65 33 32 20 26 ealvalue>=1e32 &
a2a90 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 65 & exp<=350 ){ re
a2aa0 61 6c 76 61 6c 75 65 20 2a 3d 20 31 65 2d 33 32 alvalue *= 1e-32
a2ab0 3b 20 65 78 70 2b 3d 33 32 3b 20 7d 0a 20 20 20 ; exp+=32; }.
a2ac0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 while( re
a2ad0 61 6c 76 61 6c 75 65 3e 3d 31 65 38 20 26 26 20 alvalue>=1e8 &&
a2ae0 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c exp<=350 ){ real
a2af0 76 61 6c 75 65 20 2a 3d 20 31 65 2d 38 3b 20 65 value *= 1e-8; e
a2b00 78 70 2b 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20 xp+=8; }.
a2b10 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 while( realva
a2b20 6c 75 65 3e 3d 31 30 2e 30 20 26 26 20 65 78 70 lue>=10.0 && exp
a2b30 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c <=350 ){ realval
a2b40 75 65 20 2a 3d 20 30 2e 31 3b 20 65 78 70 2b 2b ue *= 0.1; exp++
a2b50 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 ; }. wh
a2b60 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3c 31 ile( realvalue<1
a2b70 65 2d 38 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 e-8 ){ realvalue
a2b80 20 2a 3d 20 31 65 38 3b 20 65 78 70 2d 3d 38 3b *= 1e8; exp-=8;
a2b90 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 }. whi
a2ba0 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3c 31 2e le( realvalue<1.
a2bb0 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 0 ){ realvalue *
a2bc0 3d 20 31 30 2e 30 3b 20 65 78 70 2d 2d 3b 20 7d = 10.0; exp--; }
a2bd0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 . if( e
a2be0 78 70 3e 33 35 30 20 29 7b 0a 20 20 20 20 20 20 xp>350 ){.
a2bf0 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69 78 if( prefix
a2c00 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 20 =='-' ){.
a2c10 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 bufpt = "
a2c20 2d 49 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 -Inf";.
a2c30 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 72 65 }else if( pre
a2c40 66 69 78 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 fix=='+' ){.
a2c50 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 bufpt
a2c60 3d 20 22 2b 49 6e 66 22 3b 0a 20 20 20 20 20 20 = "+Inf";.
a2c70 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a2c80 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 bufpt
a2c90 20 3d 20 22 49 6e 66 22 3b 0a 20 20 20 20 20 20 = "Inf";.
a2ca0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a2cb0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 71 6c length = sql
a2cc0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 62 75 66 ite3Strlen30(buf
a2cd0 70 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 pt);.
a2ce0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
a2cf0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
a2d00 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 bufpt = bu
a2d10 66 3b 0a 20 20 20 20 20 20 20 20 2f 2a 0a 20 20 f;. /*.
a2d20 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 ** If the
a2d30 66 69 65 6c 64 20 74 79 70 65 20 69 73 20 65 74 field type is et
a2d40 47 45 4e 45 52 49 43 2c 20 74 68 65 6e 20 63 6f GENERIC, then co
a2d50 6e 76 65 72 74 20 74 6f 20 65 69 74 68 65 72 20 nvert to either
a2d60 65 74 45 58 50 0a 20 20 20 20 20 20 20 20 2a 2a etEXP. **
a2d70 20 6f 72 20 65 74 46 4c 4f 41 54 2c 20 61 73 20 or etFLOAT, as
a2d80 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 20 20 20 appropriate..
a2d90 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
a2da0 66 6c 61 67 5f 65 78 70 20 3d 20 78 74 79 70 65 flag_exp = xtype
a2db0 3d 3d 65 74 45 58 50 3b 0a 20 20 20 20 20 20 20 ==etEXP;.
a2dc0 20 69 66 28 20 78 74 79 70 65 21 3d 65 74 46 4c if( xtype!=etFL
a2dd0 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 20 20 20 OAT ){.
a2de0 20 72 65 61 6c 76 61 6c 75 65 20 2b 3d 20 72 6f realvalue += ro
a2df0 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 20 20 20 under;.
a2e00 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d if( realvalue>=
a2e10 31 30 2e 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 10.0 ){ realvalu
a2e20 65 20 2a 3d 20 30 2e 31 3b 20 65 78 70 2b 2b 3b e *= 0.1; exp++;
a2e30 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
a2e40 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d if( xtype==
a2e50 65 74 47 45 4e 45 52 49 43 20 29 7b 0a 20 20 20 etGENERIC ){.
a2e60 20 20 20 20 20 20 20 66 6c 61 67 5f 72 74 7a 20 flag_rtz
a2e70 3d 20 21 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 = !flag_alternat
a2e80 65 66 6f 72 6d 3b 0a 20 20 20 20 20 20 20 20 20 eform;.
a2e90 20 69 66 28 20 65 78 70 3c 2d 34 20 7c 7c 20 65 if( exp<-4 || e
a2ea0 78 70 3e 70 72 65 63 69 73 69 6f 6e 20 29 7b 0a xp>precision ){.
a2eb0 20 20 20 20 20 20 20 20 20 20 20 20 78 74 79 70 xtyp
a2ec0 65 20 3d 20 65 74 45 58 50 3b 0a 20 20 20 20 20 e = etEXP;.
a2ed0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a2ee0 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69 6f precisio
a2ef0 6e 20 3d 20 70 72 65 63 69 73 69 6f 6e 20 2d 20 n = precision -
a2f00 65 78 70 3b 0a 20 20 20 20 20 20 20 20 20 20 20 exp;.
a2f10 20 78 74 79 70 65 20 3d 20 65 74 46 4c 4f 41 54 xtype = etFLOAT
a2f20 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
a2f30 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a2f40 20 20 20 20 20 20 20 66 6c 61 67 5f 72 74 7a 20 flag_rtz
a2f50 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = 0;. }.
a2f60 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 if( xtype
a2f70 3d 3d 65 74 45 58 50 20 29 7b 0a 20 20 20 20 20 ==etEXP ){.
a2f80 20 20 20 20 20 65 32 20 3d 20 30 3b 0a 20 20 20 e2 = 0;.
a2f90 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a2fa0 20 20 20 20 20 20 65 32 20 3d 20 65 78 70 3b 0a e2 = exp;.
a2fb0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a2fc0 20 20 6e 73 64 20 3d 20 30 3b 0a 20 20 20 20 20 nsd = 0;.
a2fd0 20 20 20 66 6c 61 67 5f 64 70 20 3d 20 28 70 72 flag_dp = (pr
a2fe0 65 63 69 73 69 6f 6e 3e 30 20 3f 31 3a 30 29 20 ecision>0 ?1:0)
a2ff0 7c 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 | flag_alternate
a3000 66 6f 72 6d 20 7c 20 66 6c 61 67 5f 61 6c 74 66 form | flag_altf
a3010 6f 72 6d 32 3b 0a 20 20 20 20 20 20 20 20 2f 2a orm2;. /*
a3020 20 54 68 65 20 73 69 67 6e 20 69 6e 20 66 72 6f The sign in fro
a3030 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 nt of the number
a3040 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
a3050 70 72 65 66 69 78 20 29 7b 0a 20 20 20 20 20 20 prefix ){.
a3060 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d *(bufpt++) =
a3070 20 70 72 65 66 69 78 3b 0a 20 20 20 20 20 20 20 prefix;.
a3080 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 44 69 }. /* Di
a3090 67 69 74 73 20 70 72 69 6f 72 20 74 6f 20 74 68 gits prior to th
a30a0 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 e decimal point
a30b0 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 */. if( e
a30c0 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 2<0 ){.
a30d0 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 *(bufpt++) = '0
a30e0 27 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 ';. }else
a30f0 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 {. for(
a3100 3b 20 65 32 3e 3d 30 3b 20 65 32 2d 2d 29 7b 0a ; e2>=0; e2--){.
a3110 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 *(bu
a3120 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 fpt++) = et_getd
a3130 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65 2c igit(&realvalue,
a3140 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 20 20 &nsd);.
a3150 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
a3160 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 63 69 /* The deci
a3170 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 mal point */.
a3180 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 64 70 if( flag_dp
a3190 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 ){. *(
a31a0 62 75 66 70 74 2b 2b 29 20 3d 20 27 2e 27 3b 0a bufpt++) = '.';.
a31b0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a31c0 20 20 2f 2a 20 22 30 22 20 64 69 67 69 74 73 20 /* "0" digits
a31d0 61 66 74 65 72 20 74 68 65 20 64 65 63 69 6d 61 after the decima
a31e0 6c 20 70 6f 69 6e 74 20 62 75 74 20 62 65 66 6f l point but befo
a31f0 72 65 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 re the first.
a3200 20 20 20 20 20 2a 2a 20 73 69 67 6e 69 66 69 63 ** signific
a3210 61 6e 74 20 64 69 67 69 74 20 6f 66 20 74 68 65 ant digit of the
a3220 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 number */.
a3230 20 20 20 66 6f 72 28 65 32 2b 2b 3b 20 65 32 3c for(e2++; e2<
a3240 30 3b 20 70 72 65 63 69 73 69 6f 6e 2d 2d 2c 20 0; precision--,
a3250 65 32 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 e2++){.
a3260 20 61 73 73 65 72 74 28 20 70 72 65 63 69 73 69 assert( precisi
a3270 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 on>0 );.
a3280 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 *(bufpt++) = '
a3290 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 0';. }.
a32a0 20 20 20 20 20 20 2f 2a 20 53 69 67 6e 69 66 69 /* Signifi
a32b0 63 61 6e 74 20 64 69 67 69 74 73 20 61 66 74 65 cant digits afte
a32c0 72 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f r the decimal po
a32d0 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 int */. w
a32e0 68 69 6c 65 28 20 28 70 72 65 63 69 73 69 6f 6e hile( (precision
a32f0 2d 2d 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 --)>0 ){.
a3300 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 *(bufpt++) =
a3310 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61 et_getdigit(&rea
a3320 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20 lvalue,&nsd);.
a3330 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a3340 2f 2a 20 52 65 6d 6f 76 65 20 74 72 61 69 6c 69 /* Remove traili
a3350 6e 67 20 7a 65 72 6f 73 20 61 6e 64 20 74 68 65 ng zeros and the
a3360 20 22 2e 22 20 69 66 20 6e 6f 20 64 69 67 69 74 "." if no digit
a3370 73 20 66 6f 6c 6c 6f 77 20 74 68 65 20 22 2e 22 s follow the "."
a3380 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
a3390 66 6c 61 67 5f 72 74 7a 20 26 26 20 66 6c 61 67 flag_rtz && flag
a33a0 5f 64 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _dp ){.
a33b0 20 77 68 69 6c 65 28 20 62 75 66 70 74 5b 2d 31 while( bufpt[-1
a33c0 5d 3d 3d 27 30 27 20 29 20 2a 28 2d 2d 62 75 66 ]=='0' ) *(--buf
a33d0 70 74 29 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 pt) = 0;.
a33e0 20 20 20 61 73 73 65 72 74 28 20 62 75 66 70 74 assert( bufpt
a33f0 3e 62 75 66 20 29 3b 0a 20 20 20 20 20 20 20 20 >buf );.
a3400 20 20 69 66 28 20 62 75 66 70 74 5b 2d 31 5d 3d if( bufpt[-1]=
a3410 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 20 20 ='.' ){.
a3420 20 20 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74 if( flag_alt
a3430 66 6f 72 6d 32 20 29 7b 0a 20 20 20 20 20 20 20 form2 ){.
a3440 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b *(bufpt++
a3450 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 ) = '0';.
a3460 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a3470 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 *(--bu
a3480 66 70 74 29 20 3d 20 30 3b 0a 20 20 20 20 20 20 fpt) = 0;.
a3490 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a34a0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
a34b0 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 /* Add the
a34c0 20 22 65 4e 4e 4e 22 20 73 75 66 66 69 78 20 2a "eNNN" suffix *
a34d0 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c /. if( fl
a34e0 61 67 5f 65 78 70 20 7c 7c 20 78 74 79 70 65 3d ag_exp || xtype=
a34f0 3d 65 74 45 58 50 20 29 7b 0a 20 20 20 20 20 20 =etEXP ){.
a3500 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d *(bufpt++) =
a3510 20 61 44 69 67 69 74 73 5b 69 6e 66 6f 70 2d 3e aDigits[infop->
a3520 63 68 61 72 73 65 74 5d 3b 0a 20 20 20 20 20 20 charset];.
a3530 20 20 20 20 69 66 28 20 65 78 70 3c 30 20 29 7b if( exp<0 ){
a3540 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 . *(b
a3550 75 66 70 74 2b 2b 29 20 3d 20 27 2d 27 3b 20 65 ufpt++) = '-'; e
a3560 78 70 20 3d 20 2d 65 78 70 3b 0a 20 20 20 20 20 xp = -exp;.
a3570 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a3580 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b *(bufpt+
a3590 2b 29 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 +) = '+';.
a35a0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
a35b0 69 66 28 20 65 78 70 3e 3d 31 30 30 20 29 7b 0a if( exp>=100 ){.
a35c0 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 *(bu
a35d0 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29 28 fpt++) = (char)(
a35e0 28 65 78 70 2f 31 30 30 29 2b 27 30 27 29 3b 20 (exp/100)+'0');
a35f0 20 20 20 20 20 20 20 2f 2a 20 31 30 30 27 73 20 /* 100's
a3600 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 20 20 20 digit */.
a3610 20 20 20 20 20 65 78 70 20 25 3d 20 31 30 30 3b exp %= 100;
a3620 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
a3630 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b *(bufpt++
a3640 29 20 3d 20 28 63 68 61 72 29 28 65 78 70 2f 31 ) = (char)(exp/1
a3650 30 2b 27 30 27 29 3b 20 20 20 20 20 20 20 20 20 0+'0');
a3660 20 20 20 20 2f 2a 20 31 30 27 73 20 64 69 67 69 /* 10's digi
a3670 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 2a t */. *
a3680 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 (bufpt++) = (cha
a3690 72 29 28 65 78 70 25 31 30 2b 27 30 27 29 3b 20 r)(exp%10+'0');
a36a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 /* 1
a36b0 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 's digit */.
a36c0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 62 }. *b
a36d0 75 66 70 74 20 3d 20 30 3b 0a 0a 20 20 20 20 20 ufpt = 0;..
a36e0 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 76 65 72 /* The conver
a36f0 74 65 64 20 6e 75 6d 62 65 72 20 69 73 20 69 6e ted number is in
a3700 20 62 75 66 5b 5d 20 61 6e 64 20 7a 65 72 6f 20 buf[] and zero
a3710 74 65 72 6d 69 6e 61 74 65 64 2e 20 4f 75 74 70 terminated. Outp
a3720 75 74 20 69 74 2e 0a 20 20 20 20 20 20 20 20 2a ut it.. *
a3730 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 * Note that the
a3740 6e 75 6d 62 65 72 20 69 73 20 69 6e 20 74 68 65 number is in the
a3750 20 75 73 75 61 6c 20 6f 72 64 65 72 2c 20 6e 6f usual order, no
a3760 74 20 72 65 76 65 72 73 65 64 20 61 73 20 77 69 t reversed as wi
a3770 74 68 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e th. ** in
a3780 74 65 67 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e teger conversion
a3790 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c 65 s. */. le
a37a0 6e 67 74 68 20 3d 20 28 69 6e 74 29 28 62 75 66 ngth = (int)(buf
a37b0 70 74 2d 62 75 66 29 3b 0a 20 20 20 20 20 20 20 pt-buf);.
a37c0 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 0a 20 bufpt = buf;..
a37d0 20 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 61 /* Specia
a37e0 6c 20 63 61 73 65 3a 20 20 41 64 64 20 6c 65 61 l case: Add lea
a37f0 64 69 6e 67 20 7a 65 72 6f 73 20 69 66 20 74 68 ding zeros if th
a3800 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 66 e flag_zeropad f
a3810 6c 61 67 20 69 73 0a 20 20 20 20 20 20 20 20 2a lag is. *
a3820 2a 20 73 65 74 20 61 6e 64 20 77 65 20 61 72 65 * set and we are
a3830 20 6e 6f 74 20 6c 65 66 74 20 6a 75 73 74 69 66 not left justif
a3840 69 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 ied */. i
a3850 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 f( flag_zeropad
a3860 26 26 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75 73 && !flag_leftjus
a3870 74 69 66 79 20 26 26 20 6c 65 6e 67 74 68 20 3c tify && length <
a3880 20 77 69 64 74 68 29 7b 0a 20 20 20 20 20 20 20 width){.
a3890 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
a38a0 20 20 20 20 69 6e 74 20 6e 50 61 64 20 3d 20 77 int nPad = w
a38b0 69 64 74 68 20 2d 20 6c 65 6e 67 74 68 3b 0a 20 idth - length;.
a38c0 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 77 for(i=w
a38d0 69 64 74 68 3b 20 69 3e 3d 6e 50 61 64 3b 20 69 idth; i>=nPad; i
a38e0 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 --){.
a38f0 20 62 75 66 70 74 5b 69 5d 20 3d 20 62 75 66 70 bufpt[i] = bufp
a3900 74 5b 69 2d 6e 50 61 64 5d 3b 0a 20 20 20 20 20 t[i-nPad];.
a3910 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
a3920 20 69 20 3d 20 70 72 65 66 69 78 21 3d 30 3b 0a i = prefix!=0;.
a3930 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 while(
a3940 20 6e 50 61 64 2d 2d 20 29 20 62 75 66 70 74 5b nPad-- ) bufpt[
a3950 69 2b 2b 5d 20 3d 20 27 30 27 3b 0a 20 20 20 20 i++] = '0';.
a3960 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 length = w
a3970 69 64 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a idth;. }.
a3980 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 62 #endif. b
a3990 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 reak;. case
a39a0 20 65 74 53 49 5a 45 3a 0a 20 20 20 20 20 20 20 etSIZE:.
a39b0 20 2a 28 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 *(va_arg(ap,int
a39c0 2a 29 29 20 3d 20 70 41 63 63 75 6d 2d 3e 6e 43 *)) = pAccum->nC
a39d0 68 61 72 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e har;. len
a39e0 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 3b gth = width = 0;
a39f0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
a3a00 20 20 20 20 20 20 63 61 73 65 20 65 74 50 45 52 case etPER
a3a10 43 45 4e 54 3a 0a 20 20 20 20 20 20 20 20 62 75 CENT:. bu
a3a20 66 5b 30 5d 20 3d 20 27 25 27 3b 0a 20 20 20 20 f[0] = '%';.
a3a30 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b bufpt = buf;
a3a40 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 . length
a3a50 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72 65 = 1;. bre
a3a60 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 ak;. case e
a3a70 74 43 48 41 52 58 3a 0a 20 20 20 20 20 20 20 20 tCHARX:.
a3a80 63 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e c = va_arg(ap,in
a3a90 74 29 3b 0a 20 20 20 20 20 20 20 20 62 75 66 5b t);. buf[
a3aa0 30 5d 20 3d 20 28 63 68 61 72 29 63 3b 0a 20 20 0] = (char)c;.
a3ab0 20 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 if( precis
a3ac0 69 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 ion>=0 ){.
a3ad0 20 20 20 20 66 6f 72 28 69 64 78 3d 31 3b 20 69 for(idx=1; i
a3ae0 64 78 3c 70 72 65 63 69 73 69 6f 6e 3b 20 69 64 dx<precision; id
a3af0 78 2b 2b 29 20 62 75 66 5b 69 64 78 5d 20 3d 20 x++) buf[idx] =
a3b00 28 63 68 61 72 29 63 3b 0a 20 20 20 20 20 20 20 (char)c;.
a3b10 20 20 20 6c 65 6e 67 74 68 20 3d 20 70 72 65 63 length = prec
a3b20 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 7d ision;. }
a3b30 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
a3b40 6c 65 6e 67 74 68 20 3d 31 3b 0a 20 20 20 20 20 length =1;.
a3b50 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66 }. buf
a3b60 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 pt = buf;.
a3b70 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
a3b80 61 73 65 20 65 74 53 54 52 49 4e 47 3a 0a 20 20 ase etSTRING:.
a3b90 20 20 20 20 63 61 73 65 20 65 74 44 59 4e 53 54 case etDYNST
a3ba0 52 49 4e 47 3a 0a 20 20 20 20 20 20 20 20 62 75 RING:. bu
a3bb0 66 70 74 20 3d 20 76 61 5f 61 72 67 28 61 70 2c fpt = va_arg(ap,
a3bc0 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 char*);.
a3bd0 69 66 28 20 62 75 66 70 74 3d 3d 30 20 29 7b 0a if( bufpt==0 ){.
a3be0 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 bufpt
a3bf0 3d 20 22 22 3b 0a 20 20 20 20 20 20 20 20 7d 65 = "";. }e
a3c00 6c 73 65 20 69 66 28 20 78 74 79 70 65 3d 3d 65 lse if( xtype==e
a3c10 74 44 59 4e 53 54 52 49 4e 47 20 29 7b 0a 20 20 tDYNSTRING ){.
a3c20 20 20 20 20 20 20 20 20 7a 45 78 74 72 61 20 3d zExtra =
a3c30 20 62 75 66 70 74 3b 0a 20 20 20 20 20 20 20 20 bufpt;.
a3c40 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 }. if( pr
a3c50 65 63 69 73 69 6f 6e 3e 3d 30 20 29 7b 0a 20 20 ecision>=0 ){.
a3c60 20 20 20 20 20 20 20 20 66 6f 72 28 6c 65 6e 67 for(leng
a3c70 74 68 3d 30 3b 20 6c 65 6e 67 74 68 3c 70 72 65 th=0; length<pre
a3c80 63 69 73 69 6f 6e 20 26 26 20 62 75 66 70 74 5b cision && bufpt[
a3c90 6c 65 6e 67 74 68 5d 3b 20 6c 65 6e 67 74 68 2b length]; length+
a3ca0 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c +){}. }el
a3cb0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 se{. le
a3cc0 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 33 53 74 ngth = sqlite3St
a3cd0 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3b 0a 20 rlen30(bufpt);.
a3ce0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a3cf0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 break;. ca
a3d00 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 3a 0a se etSQLESCAPE:.
a3d10 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c case etSQL
a3d20 45 53 43 41 50 45 32 3a 0a 20 20 20 20 20 20 63 ESCAPE2:. c
a3d30 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 33 ase etSQLESCAPE3
a3d40 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 : {. int
a3d50 69 2c 20 6a 2c 20 6e 2c 20 69 73 6e 75 6c 6c 3b i, j, n, isnull;
a3d60 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 65 65 . int nee
a3d70 64 51 75 6f 74 65 3b 0a 20 20 20 20 20 20 20 20 dQuote;.
a3d80 63 68 61 72 20 63 68 3b 0a 20 20 20 20 20 20 20 char ch;.
a3d90 20 63 68 61 72 20 71 20 3d 20 28 28 78 74 79 70 char q = ((xtyp
a3da0 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 33 29 e==etSQLESCAPE3)
a3db0 3f 27 22 27 3a 27 5c 27 27 29 3b 20 20 20 2f 2a ?'"':'\''); /*
a3dc0 20 51 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 Quote character
a3dd0 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 68 61 72 */. char
a3de0 20 2a 65 73 63 61 72 67 20 3d 20 76 61 5f 61 72 *escarg = va_ar
a3df0 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a 20 20 20 g(ap,char*);.
a3e00 20 20 20 20 20 69 73 6e 75 6c 6c 20 3d 20 65 73 isnull = es
a3e10 63 61 72 67 3d 3d 30 3b 0a 20 20 20 20 20 20 20 carg==0;.
a3e20 20 69 66 28 20 69 73 6e 75 6c 6c 20 29 20 65 73 if( isnull ) es
a3e30 63 61 72 67 20 3d 20 28 78 74 79 70 65 3d 3d 65 carg = (xtype==e
a3e40 74 53 51 4c 45 53 43 41 50 45 32 20 3f 20 22 4e tSQLESCAPE2 ? "N
a3e50 55 4c 4c 22 20 3a 20 22 28 4e 55 4c 4c 29 22 29 ULL" : "(NULL)")
a3e60 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d ;. for(i=
a3e70 6e 3d 30 3b 20 28 63 68 3d 65 73 63 61 72 67 5b n=0; (ch=escarg[
a3e80 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 i])!=0; i++){.
a3e90 20 20 20 20 20 20 20 20 69 66 28 20 63 68 3d 3d if( ch==
a3ea0 71 20 29 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 20 q ) n++;.
a3eb0 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 65 65 64 }. need
a3ec0 51 75 6f 74 65 20 3d 20 21 69 73 6e 75 6c 6c 20 Quote = !isnull
a3ed0 26 26 20 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 && xtype==etSQLE
a3ee0 53 43 41 50 45 32 3b 0a 20 20 20 20 20 20 20 20 SCAPE2;.
a3ef0 6e 20 2b 3d 20 69 20 2b 20 31 20 2b 20 6e 65 65 n += i + 1 + nee
a3f00 64 51 75 6f 74 65 2a 32 3b 0a 20 20 20 20 20 20 dQuote*2;.
a3f10 20 20 69 66 28 20 6e 3e 65 74 42 55 46 53 49 5a if( n>etBUFSIZ
a3f20 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 E ){. b
a3f30 75 66 70 74 20 3d 20 7a 45 78 74 72 61 20 3d 20 ufpt = zExtra =
a3f40 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e sqlite3Malloc( n
a3f50 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 );. if
a3f60 28 20 62 75 66 70 74 3d 3d 30 20 29 7b 0a 20 20 ( bufpt==0 ){.
a3f70 20 20 20 20 20 20 20 20 20 20 70 41 63 63 75 6d pAccum
a3f80 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
a3f90 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 1;.
a3fa0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 return;.
a3fb0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 }. }els
a3fc0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 e{. buf
a3fd0 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 pt = buf;.
a3fe0 20 20 7d 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 }. j =
a3ff0 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 0;. if( n
a4000 65 65 64 51 75 6f 74 65 20 29 20 62 75 66 70 74 eedQuote ) bufpt
a4010 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20 20 20 20 [j++] = q;.
a4020 20 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 68 3d for(i=0; (ch=
a4030 65 73 63 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69 escarg[i])!=0; i
a4040 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 ++){. b
a4050 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a ufpt[j++] = ch;.
a4060 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 68 if( ch
a4070 3d 3d 71 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d ==q ) bufpt[j++]
a4080 20 3d 20 63 68 3b 0a 20 20 20 20 20 20 20 20 7d = ch;. }
a4090 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 65 65 . if( nee
a40a0 64 51 75 6f 74 65 20 29 20 62 75 66 70 74 5b 6a dQuote ) bufpt[j
a40b0 2b 2b 5d 20 3d 20 71 3b 0a 20 20 20 20 20 20 20 ++] = q;.
a40c0 20 62 75 66 70 74 5b 6a 5d 20 3d 20 30 3b 0a 20 bufpt[j] = 0;.
a40d0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 length =
a40e0 6a 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 j;. /* Th
a40f0 65 20 70 72 65 63 69 73 69 6f 6e 20 69 73 20 69 e precision is i
a4100 67 6e 6f 72 65 64 20 6f 6e 20 25 71 20 61 6e 64 gnored on %q and
a4110 20 25 51 20 2a 2f 0a 20 20 20 20 20 20 20 20 2f %Q */. /
a4120 2a 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e * if( precision>
a4130 3d 30 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3c =0 && precision<
a4140 6c 65 6e 67 74 68 20 29 20 6c 65 6e 67 74 68 20 length ) length
a4150 3d 20 70 72 65 63 69 73 69 6f 6e 3b 20 2a 2f 0a = precision; */.
a4160 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
a4170 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 }. cas
a4180 65 20 65 74 54 4f 4b 45 4e 3a 20 7b 0a 20 20 20 e etTOKEN: {.
a4190 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b Token *pTok
a41a0 65 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 en = va_arg(ap,
a41b0 54 6f 6b 65 6e 2a 29 3b 0a 20 20 20 20 20 20 20 Token*);.
a41c0 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 if( pToken ){.
a41d0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
a41e0 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 StrAccumAppend(p
a41f0 41 63 63 75 6d 2c 20 28 63 6f 6e 73 74 20 63 68 Accum, (const ch
a4200 61 72 2a 29 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 ar*)pToken->z, p
a4210 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 20 20 20 Token->n);.
a4220 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e }. len
a4230 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 3b gth = width = 0;
a4240 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
a4250 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 }. ca
a4260 73 65 20 65 74 53 52 43 4c 49 53 54 3a 20 7b 0a se etSRCLIST: {.
a4270 20 20 20 20 20 20 20 20 53 72 63 4c 69 73 74 20 SrcList
a4280 2a 70 53 72 63 20 3d 20 76 61 5f 61 72 67 28 61 *pSrc = va_arg(a
a4290 70 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 20 20 p, SrcList*);.
a42a0 20 20 20 20 20 20 69 6e 74 20 6b 20 3d 20 76 61 int k = va
a42b0 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 _arg(ap, int);.
a42c0 20 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 struct Sr
a42d0 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 cList_item *pIte
a42e0 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b 6b 5d 3b m = &pSrc->a[k];
a42f0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
a4300 20 6b 3e 3d 30 20 26 26 20 6b 3c 70 53 72 63 2d k>=0 && k<pSrc-
a4310 3e 6e 53 72 63 20 29 3b 0a 20 20 20 20 20 20 20 >nSrc );.
a4320 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44 61 74 if( pItem->zDat
a4330 61 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 abase ){.
a4340 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
a4350 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c umAppend(pAccum,
a4360 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 pItem->zDatabas
a4370 65 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 e, -1);.
a4380 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 sqlite3StrAccu
a4390 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 mAppend(pAccum,
a43a0 22 2e 22 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 ".", 1);.
a43b0 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
a43c0 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 e3StrAccumAppend
a43d0 28 70 41 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e (pAccum, pItem->
a43e0 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 20 20 zName, -1);.
a43f0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 length = wid
a4400 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 th = 0;.
a4410 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
a4420 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a default: {.
a4430 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
a4440 78 74 79 70 65 3d 3d 65 74 49 4e 56 41 4c 49 44 xtype==etINVALID
a4450 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 );. retu
a4460 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rn;. }.
a4470 7d 2f 2a 20 45 6e 64 20 73 77 69 74 63 68 20 6f }/* End switch o
a4480 76 65 72 20 74 68 65 20 66 6f 72 6d 61 74 20 74 ver the format t
a4490 79 70 65 20 2a 2f 0a 20 20 20 20 2f 2a 0a 20 20 ype */. /*.
a44a0 20 20 2a 2a 20 54 68 65 20 74 65 78 74 20 6f 66 ** The text of
a44b0 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 the conversion
a44c0 69 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 is pointed to by
a44d0 20 22 62 75 66 70 74 22 20 61 6e 64 20 69 73 0a "bufpt" and is.
a44e0 20 20 20 20 2a 2a 20 22 6c 65 6e 67 74 68 22 20 ** "length"
a44f0 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e characters long.
a4500 20 20 54 68 65 20 66 69 65 6c 64 20 77 69 64 74 The field widt
a4510 68 20 69 73 20 22 77 69 64 74 68 22 2e 20 20 44 h is "width". D
a4520 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f 75 74 o. ** the out
a4530 70 75 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 put.. */.
a4540 69 66 28 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75 if( !flag_leftju
a4550 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 20 72 stify ){. r
a4560 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 70 61 egister int nspa
a4570 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 63 65 ce;. nspace
a4580 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 68 3b = width-length;
a4590 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 61 63 . if( nspac
a45a0 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 e>0 ){. a
a45b0 70 70 65 6e 64 53 70 61 63 65 28 70 41 63 63 75 ppendSpace(pAccu
a45c0 6d 2c 20 6e 73 70 61 63 65 29 3b 0a 20 20 20 20 m, nspace);.
a45d0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
a45e0 28 20 6c 65 6e 67 74 68 3e 30 20 29 7b 0a 20 20 ( length>0 ){.
a45f0 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
a4600 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d cumAppend(pAccum
a4610 2c 20 62 75 66 70 74 2c 20 6c 65 6e 67 74 68 29 , bufpt, length)
a4620 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
a4630 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 flag_leftjustify
a4640 20 29 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74 ){. regist
a4650 65 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20 er int nspace;.
a4660 20 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69 nspace = wi
a4670 64 74 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20 dth-length;.
a4680 20 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29 if( nspace>0 )
a4690 7b 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 {. append
a46a0 53 70 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 Space(pAccum, ns
a46b0 70 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 pace);. }.
a46c0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 45 78 }. if( zEx
a46d0 74 72 61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c tra ){. sql
a46e0 69 74 65 33 5f 66 72 65 65 28 7a 45 78 74 72 61 ite3_free(zExtra
a46f0 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 2f 2a 20 45 );. }. }/* E
a4700 6e 64 20 66 6f 72 20 6c 6f 6f 70 20 6f 76 65 72 nd for loop over
a4710 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 the format stri
a4720 6e 67 20 2a 2f 0a 7d 20 2f 2a 20 45 6e 64 20 6f ng */.} /* End o
a4730 66 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 2f f function */../
a4740 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 4e 20 62 79 *.** Append N by
a4750 74 65 73 20 6f 66 20 74 65 78 74 20 66 72 6f 6d tes of text from
a4760 20 7a 20 74 6f 20 74 68 65 20 53 74 72 41 63 63 z to the StrAcc
a4770 75 6d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 um object..*/.SQ
a4780 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
a4790 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 d sqlite3StrAccu
a47a0 6d 41 70 70 65 6e 64 28 53 74 72 41 63 63 75 6d mAppend(StrAccum
a47b0 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *p, const char
a47c0 2a 7a 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 61 73 *z, int N){. as
a47d0 73 65 72 74 28 20 7a 21 3d 30 20 7c 7c 20 4e 3d sert( z!=0 || N=
a47e0 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 74 =0 );. if( p->t
a47f0 6f 6f 42 69 67 20 7c 20 70 2d 3e 6d 61 6c 6c 6f ooBig | p->mallo
a4800 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 74 cFailed ){. t
a4810 65 73 74 63 61 73 65 28 70 2d 3e 74 6f 6f 42 69 estcase(p->tooBi
a4820 67 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 g);. testcase
a4830 28 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 (p->mallocFailed
a4840 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 );. return;.
a4850 20 7d 0a 20 20 69 66 28 20 4e 3c 30 20 29 7b 0a }. if( N<0 ){.
a4860 20 20 20 20 4e 20 3d 20 73 71 6c 69 74 65 33 53 N = sqlite3S
a4870 74 72 6c 65 6e 33 30 28 7a 29 3b 0a 20 20 7d 0a trlen30(z);. }.
a4880 20 20 69 66 28 20 4e 3d 3d 30 20 7c 7c 20 4e 45 if( N==0 || NE
a4890 56 45 52 28 7a 3d 3d 30 29 20 29 7b 0a 20 20 20 VER(z==0) ){.
a48a0 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 return;. }. i
a48b0 66 28 20 70 2d 3e 6e 43 68 61 72 2b 4e 20 3e 3d f( p->nChar+N >=
a48c0 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 p->nAlloc ){.
a48d0 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 char *zNew;.
a48e0 20 20 69 66 28 20 21 70 2d 3e 75 73 65 4d 61 6c if( !p->useMal
a48f0 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e loc ){. p->
a4900 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 tooBig = 1;.
a4910 20 20 4e 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 N = p->nAlloc
a4920 2d 20 70 2d 3e 6e 43 68 61 72 20 2d 20 31 3b 0a - p->nChar - 1;.
a4930 20 20 20 20 20 20 69 66 28 20 4e 3c 3d 30 20 29 if( N<=0 )
a4940 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
a4950 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
a4960 6c 73 65 7b 0a 20 20 20 20 20 20 69 36 34 20 73 lse{. i64 s
a4970 7a 4e 65 77 20 3d 20 70 2d 3e 6e 43 68 61 72 3b zNew = p->nChar;
a4980 0a 20 20 20 20 20 20 73 7a 4e 65 77 20 2b 3d 20 . szNew +=
a4990 4e 20 2b 20 31 3b 0a 20 20 20 20 20 20 69 66 28 N + 1;. if(
a49a0 20 73 7a 4e 65 77 20 3e 20 70 2d 3e 6d 78 41 6c szNew > p->mxAl
a49b0 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 loc ){. s
a49c0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 qlite3StrAccumRe
a49d0 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 set(p);.
a49e0 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 p->tooBig = 1;.
a49f0 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 return;.
a4a00 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a4a10 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 p->nAlloc =
a4a20 28 69 6e 74 29 73 7a 4e 65 77 3b 0a 20 20 20 20 (int)szNew;.
a4a30 20 20 7d 0a 20 20 20 20 20 20 7a 4e 65 77 20 3d }. zNew =
a4a40 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
a4a50 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e 41 Raw(p->db, p->nA
a4a60 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 20 20 69 66 lloc );. if
a4a70 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 ( zNew ){.
a4a80 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 70 memcpy(zNew, p
a4a90 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 6e 43 68 61 ->zText, p->nCha
a4aa0 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 r);. sqli
a4ab0 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 te3StrAccumReset
a4ac0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e (p);. p->
a4ad0 7a 54 65 78 74 20 3d 20 7a 4e 65 77 3b 0a 20 20 zText = zNew;.
a4ae0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a4af0 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c p->mallocFail
a4b00 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 ed = 1;.
a4b10 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 sqlite3StrAccumR
a4b20 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 eset(p);.
a4b30 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d return;. }
a4b40 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d . }. }. mem
a4b50 63 70 79 28 26 70 2d 3e 7a 54 65 78 74 5b 70 2d cpy(&p->zText[p-
a4b60 3e 6e 43 68 61 72 5d 2c 20 7a 2c 20 4e 29 3b 0a >nChar], z, N);.
a4b70 20 20 70 2d 3e 6e 43 68 61 72 20 2b 3d 20 4e 3b p->nChar += N;
a4b80 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 69 73 68 .}../*.** Finish
a4b90 20 6f 66 66 20 61 20 73 74 72 69 6e 67 20 62 79 off a string by
a4ba0 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 69 74 20 making sure it
a4bb0 69 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 is zero-terminat
a4bc0 65 64 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ed..** Return a
a4bd0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 pointer to the r
a4be0 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67 2e esulting string.
a4bf0 20 20 52 65 74 75 72 6e 20 61 20 4e 55 4c 4c 0a Return a NULL.
a4c00 2a 2a 20 70 6f 69 6e 74 65 72 20 69 66 20 61 6e ** pointer if an
a4c10 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 y kind of error
a4c20 77 61 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e was encountered.
a4c30 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a4c40 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
a4c50 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 53 StrAccumFinish(S
a4c60 74 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69 trAccum *p){. i
a4c70 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20 f( p->zText ){.
a4c80 20 20 20 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e p->zText[p->n
a4c90 43 68 61 72 5d 20 3d 20 30 3b 0a 20 20 20 20 69 Char] = 0;. i
a4ca0 66 28 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 f( p->useMalloc
a4cb0 26 26 20 70 2d 3e 7a 54 65 78 74 3d 3d 70 2d 3e && p->zText==p->
a4cc0 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 20 20 70 zBase ){. p
a4cd0 2d 3e 7a 54 65 78 74 20 3d 20 73 71 6c 69 74 65 ->zText = sqlite
a4ce0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 3DbMallocRaw(p->
a4cf0 64 62 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 20 29 db, p->nChar+1 )
a4d00 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 7a ;. if( p->z
a4d10 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Text ){.
a4d20 6d 65 6d 63 70 79 28 70 2d 3e 7a 54 65 78 74 2c memcpy(p->zText,
a4d30 20 70 2d 3e 7a 42 61 73 65 2c 20 70 2d 3e 6e 43 p->zBase, p->nC
a4d40 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20 7d 65 har+1);. }e
a4d50 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e lse{. p->
a4d60 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
a4d70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
a4d80 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e }. return p->
a4d90 7a 54 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 zText;.}../*.**
a4da0 52 65 73 65 74 20 61 6e 20 53 74 72 41 63 63 75 Reset an StrAccu
a4db0 6d 20 73 74 72 69 6e 67 2e 20 20 52 65 63 6c 61 m string. Recla
a4dc0 69 6d 20 61 6c 6c 20 6d 61 6c 6c 6f 63 65 64 20 im all malloced
a4dd0 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 memory..*/.SQLIT
a4de0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
a4df0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 qlite3StrAccumRe
a4e00 73 65 74 28 53 74 72 41 63 63 75 6d 20 2a 70 29 set(StrAccum *p)
a4e10 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 {. if( p->zText
a4e20 21 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a 20 20 !=p->zBase ){.
a4e30 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
a4e40 70 2d 3e 64 62 2c 20 70 2d 3e 7a 54 65 78 74 29 p->db, p->zText)
a4e50 3b 0a 20 20 7d 0a 20 20 70 2d 3e 7a 54 65 78 74 ;. }. p->zText
a4e60 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 = 0;.}../*.** I
a4e70 6e 69 74 69 61 6c 69 7a 65 20 61 20 73 74 72 69 nitialize a stri
a4e80 6e 67 20 61 63 63 75 6d 75 6c 61 74 6f 72 0a 2a ng accumulator.*
a4e90 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
a4ea0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 void sqlite3Str
a4eb0 41 63 63 75 6d 49 6e 69 74 28 53 74 72 41 63 63 AccumInit(StrAcc
a4ec0 75 6d 20 2a 70 2c 20 63 68 61 72 20 2a 7a 42 61 um *p, char *zBa
a4ed0 73 65 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 6d se, int n, int m
a4ee0 78 29 7b 0a 20 20 70 2d 3e 7a 54 65 78 74 20 3d x){. p->zText =
a4ef0 20 70 2d 3e 7a 42 61 73 65 20 3d 20 7a 42 61 73 p->zBase = zBas
a4f00 65 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 30 3b 0a e;. p->db = 0;.
a4f10 20 20 70 2d 3e 6e 43 68 61 72 20 3d 20 30 3b 0a p->nChar = 0;.
a4f20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 3b p->nAlloc = n;
a4f30 0a 20 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20 . p->mxAlloc =
a4f40 6d 78 3b 0a 20 20 70 2d 3e 75 73 65 4d 61 6c 6c mx;. p->useMall
a4f50 6f 63 20 3d 20 31 3b 0a 20 20 70 2d 3e 74 6f 6f oc = 1;. p->too
a4f60 42 69 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 6d 61 Big = 0;. p->ma
a4f70 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a llocFailed = 0;.
a4f80 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 }../*.** Print i
a4f90 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 nto memory obtai
a4fa0 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d ned from sqliteM
a4fb0 61 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74 68 alloc(). Use th
a4fc0 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d e internal.** %-
a4fd0 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e conversion exten
a4fe0 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 sions..*/.SQLITE
a4ff0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
a5000 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73 qlite3VMPrintf(s
a5010 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 qlite3 *db, cons
a5020 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c t char *zFormat,
a5030 20 76 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20 20 va_list ap){.
a5040 63 68 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72 20 char *z;. char
a5050 7a 42 61 73 65 5b 53 51 4c 49 54 45 5f 50 52 49 zBase[SQLITE_PRI
a5060 4e 54 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20 20 NT_BUF_SIZE];.
a5070 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20 StrAccum acc;.
a5080 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b assert( db!=0 );
a5090 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 . sqlite3StrAcc
a50a0 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 61 umInit(&acc, zBa
a50b0 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73 65 se, sizeof(zBase
a50c0 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ),.
a50d0 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c 69 db->aLi
a50e0 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
a50f0 5f 4c 45 4e 47 54 48 5d 29 3b 0a 20 20 61 63 63 _LENGTH]);. acc
a5100 2e 64 62 20 3d 20 64 62 3b 0a 20 20 73 71 6c 69 .db = db;. sqli
a5110 74 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 te3VXPrintf(&acc
a5120 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 , 1, zFormat, ap
a5130 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 );. z = sqlite3
a5140 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 StrAccumFinish(&
a5150 61 63 63 29 3b 0a 20 20 69 66 28 20 61 63 63 2e acc);. if( acc.
a5160 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
a5170 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 db->mallocFa
a5180 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 iled = 1;. }.
a5190 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a return z;.}../*.
a51a0 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 ** Print into me
a51b0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
a51c0 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 om sqliteMalloc(
a51d0 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74 65 ). Use the inte
a51e0 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 rnal.** %-conver
a51f0 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e sion extensions.
a5200 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a5210 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
a5220 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 20 MPrintf(sqlite3
a5230 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *db, const char
a5240 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a *zFormat, ...){.
a5250 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 va_list ap;.
a5260 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 char *z;. va_st
a5270 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 art(ap, zFormat)
a5280 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 ;. z = sqlite3V
a5290 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 MPrintf(db, zFor
a52a0 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 mat, ap);. va_e
a52b0 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e nd(ap);. return
a52c0 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b z;.}../*.** Lik
a52d0 65 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 e sqlite3MPrintf
a52e0 28 29 2c 20 62 75 74 20 63 61 6c 6c 20 73 71 6c (), but call sql
a52f0 69 74 65 33 44 62 46 72 65 65 28 29 20 6f 6e 20 ite3DbFree() on
a5300 7a 53 74 72 20 61 66 74 65 72 20 66 6f 72 6d 61 zStr after forma
a5310 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 73 74 72 tting.** the str
a5320 69 6e 67 20 61 6e 64 20 62 65 66 6f 72 65 20 72 ing and before r
a5330 65 74 75 72 6e 6e 69 6e 67 2e 20 20 54 68 69 73 eturnning. This
a5340 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 74 65 routine is inte
a5350 6e 64 65 64 20 74 6f 20 62 65 20 75 73 65 64 0a nded to be used.
a5360 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 61 6e 20 ** to modify an
a5370 65 78 69 73 74 69 6e 67 20 73 74 72 69 6e 67 2e existing string.
a5380 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a For example:.*
a5390 2a 0a 2a 2a 20 20 20 20 20 20 20 78 20 3d 20 73 *.** x = s
a53a0 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 qlite3MPrintf(db
a53b0 2c 20 78 2c 20 22 70 72 65 66 69 78 20 25 73 20 , x, "prefix %s
a53c0 73 75 66 66 69 78 22 2c 20 78 29 3b 0a 2a 2a 0a suffix", x);.**.
a53d0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
a53e0 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d E char *sqlite3M
a53f0 41 70 70 65 6e 64 66 28 73 71 6c 69 74 65 33 20 Appendf(sqlite3
a5400 2a 64 62 2c 20 63 68 61 72 20 2a 7a 53 74 72 2c *db, char *zStr,
a5410 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
a5420 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 rmat, ...){. va
a5430 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 _list ap;. char
a5440 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 *z;. va_start(
a5450 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 ap, zFormat);.
a5460 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 z = sqlite3VMPri
a5470 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c ntf(db, zFormat,
a5480 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 ap);. va_end(a
a5490 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 p);. sqlite3DbF
a54a0 72 65 65 28 64 62 2c 20 7a 53 74 72 29 3b 0a 20 ree(db, zStr);.
a54b0 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a return z;.}../*
a54c0 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d .** Print into m
a54d0 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
a54e0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c rom sqlite3_mall
a54f0 6f 63 28 29 2e 20 20 4f 6d 69 74 20 74 68 65 20 oc(). Omit the
a5500 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f internal.** %-co
a5510 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 nversion extensi
a5520 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ons..*/.SQLITE_A
a5530 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 PI char *sqlite3
a5540 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 _vmprintf(const
a5550 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 char *zFormat, v
a5560 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20 20 63 68 a_list ap){. ch
a5570 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72 20 7a 42 ar *z;. char zB
a5580 61 73 65 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54 ase[SQLITE_PRINT
a5590 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 _BUF_SIZE];. St
a55a0 72 41 63 63 75 6d 20 61 63 63 3b 0a 23 69 66 6e rAccum acc;.#ifn
a55b0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
a55c0 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 AUTOINIT. if( s
a55d0 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
a55e0 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a e() ) return 0;.
a55f0 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 #endif. sqlite3
a5600 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 StrAccumInit(&ac
a5610 63 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f 66 c, zBase, sizeof
a5620 28 7a 42 61 73 65 29 2c 20 53 51 4c 49 54 45 5f (zBase), SQLITE_
a5630 4d 41 58 5f 4c 45 4e 47 54 48 29 3b 0a 20 20 73 MAX_LENGTH);. s
a5640 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 qlite3VXPrintf(&
a5650 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c acc, 0, zFormat,
a5660 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 ap);. z = sqli
a5670 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 te3StrAccumFinis
a5680 68 28 26 61 63 63 29 3b 0a 20 20 72 65 74 75 72 h(&acc);. retur
a5690 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 n z;.}../*.** Pr
a56a0 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 int into memory
a56b0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
a56c0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 28 29 lite3_malloc()()
a56d0 2e 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 . Omit the inte
a56e0 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 rnal.** %-conver
a56f0 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e sion extensions.
a5700 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
a5710 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d 70 72 har *sqlite3_mpr
a5720 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 intf(const char
a5730 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a *zFormat, ...){.
a5740 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 va_list ap;.
a5750 63 68 61 72 20 2a 7a 3b 0a 23 69 66 6e 64 65 66 char *z;.#ifndef
a5760 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
a5770 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 OINIT. if( sqli
a5780 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
a5790 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e ) return 0;.#en
a57a0 64 69 66 0a 20 20 76 61 5f 73 74 61 72 74 28 61 dif. va_start(a
a57b0 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a p, zFormat);. z
a57c0 20 3d 20 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 = sqlite3_vmpri
a57d0 6e 74 66 28 7a 46 6f 72 6d 61 74 2c 20 61 70 29 ntf(zFormat, ap)
a57e0 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a ;. va_end(ap);.
a57f0 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f return z;.}../
a5800 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 6e 70 *.** sqlite3_snp
a5810 72 69 6e 74 66 28 29 20 77 6f 72 6b 73 20 6c 69 rintf() works li
a5820 6b 65 20 73 6e 70 72 69 6e 74 66 28 29 20 65 78 ke snprintf() ex
a5830 63 65 70 74 20 74 68 61 74 20 69 74 20 69 67 6e cept that it ign
a5840 6f 72 65 73 20 74 68 65 0a 2a 2a 20 63 75 72 72 ores the.** curr
a5850 65 6e 74 20 6c 6f 63 61 6c 65 20 73 65 74 74 69 ent locale setti
a5860 6e 67 73 2e 20 20 54 68 69 73 20 69 73 20 69 6d ngs. This is im
a5870 70 6f 72 74 61 6e 74 20 66 6f 72 20 53 51 4c 69 portant for SQLi
a5880 74 65 20 62 65 63 61 75 73 65 20 77 65 0a 2a 2a te because we.**
a5890 20 61 72 65 20 6e 6f 74 20 61 62 6c 65 20 74 6f are not able to
a58a0 20 75 73 65 20 61 20 22 2c 22 20 61 73 20 74 68 use a "," as th
a58b0 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 e decimal point
a58c0 69 6e 20 70 6c 61 63 65 20 6f 66 20 22 2e 22 20 in place of "."
a58d0 61 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 as.** specified
a58e0 62 79 20 73 6f 6d 65 20 6c 6f 63 61 6c 65 73 2e by some locales.
a58f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
a5900 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70 har *sqlite3_snp
a5910 72 69 6e 74 66 28 69 6e 74 20 6e 2c 20 63 68 61 rintf(int n, cha
a5920 72 20 2a 7a 42 75 66 2c 20 63 6f 6e 73 74 20 63 r *zBuf, const c
a5930 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e har *zFormat, ..
a5940 2e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 .){. char *z;.
a5950 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 53 va_list ap;. S
a5960 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 0a 20 20 trAccum acc;..
a5970 69 66 28 20 6e 3c 3d 30 20 29 7b 0a 20 20 20 20 if( n<=0 ){.
a5980 72 65 74 75 72 6e 20 7a 42 75 66 3b 0a 20 20 7d return zBuf;. }
a5990 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 . sqlite3StrAcc
a59a0 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75 umInit(&acc, zBu
a59b0 66 2c 20 6e 2c 20 30 29 3b 0a 20 20 61 63 63 2e f, n, 0);. acc.
a59c0 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 useMalloc = 0;.
a59d0 20 76 61 5f 73 74 61 72 74 28 61 70 2c 7a 46 6f va_start(ap,zFo
a59e0 72 6d 61 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 rmat);. sqlite3
a59f0 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 30 VXPrintf(&acc, 0
a5a00 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
a5a10 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
a5a20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63 z = sqlite3StrAc
a5a30 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b cumFinish(&acc);
a5a40 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a . return z;.}..
a5a50 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
a5a60 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 TE_DEBUG)./*.**
a5a70 41 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 A version of pri
a5a80 6e 74 66 28 29 20 74 68 61 74 20 75 6e 64 65 72 ntf() that under
a5a90 73 74 61 6e 64 73 20 25 6c 6c 64 2e 20 20 55 73 stands %lld. Us
a5aa0 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 ed for debugging
a5ab0 2e 0a 2a 2a 20 54 68 65 20 70 72 69 6e 74 66 28 ..** The printf(
a5ac0 29 20 62 75 69 6c 74 20 69 6e 74 6f 20 73 6f 6d ) built into som
a5ad0 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 77 69 e versions of wi
a5ae0 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 75 ndows does not u
a5af0 6e 64 65 72 73 74 61 6e 64 20 25 6c 6c 64 0a 2a nderstand %lld.*
a5b00 2a 20 61 6e 64 20 73 65 67 66 61 75 6c 74 73 20 * and segfaults
a5b10 69 66 20 79 6f 75 20 67 69 76 65 20 69 74 20 61 if you give it a
a5b20 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 2e 0a long long int..
a5b30 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
a5b40 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 E void sqlite3De
a5b50 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 73 74 20 bugPrintf(const
a5b60 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e char *zFormat, .
a5b70 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 ..){. va_list a
a5b80 70 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 p;. StrAccum ac
a5b90 63 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 35 c;. char zBuf[5
a5ba0 30 30 5d 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 00];. sqlite3St
a5bb0 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c rAccumInit(&acc,
a5bc0 20 7a 42 75 66 2c 20 73 69 7a 65 6f 66 28 7a 42 zBuf, sizeof(zB
a5bd0 75 66 29 2c 20 30 29 3b 0a 20 20 61 63 63 2e 75 uf), 0);. acc.u
a5be0 73 65 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 seMalloc = 0;.
a5bf0 76 61 5f 73 74 61 72 74 28 61 70 2c 7a 46 6f 72 va_start(ap,zFor
a5c00 6d 61 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 mat);. sqlite3V
a5c10 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 30 2c XPrintf(&acc, 0,
a5c20 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 zFormat, ap);.
a5c30 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 73 va_end(ap);. s
a5c40 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 qlite3StrAccumFi
a5c50 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 66 70 nish(&acc);. fp
a5c60 72 69 6e 74 66 28 73 74 64 6f 75 74 2c 22 25 73 rintf(stdout,"%s
a5c70 22 2c 20 7a 42 75 66 29 3b 0a 20 20 66 66 6c 75 ", zBuf);. fflu
a5c80 73 68 28 73 74 64 6f 75 74 29 3b 0a 7d 0a 23 65 sh(stdout);.}.#e
a5c90 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ndif../*********
a5ca0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72 69 ***** End of pri
a5cb0 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ntf.c **********
a5cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5ce0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
a5cf0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
a5d00 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a random.c ******
a5d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5d30 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 ****/./*.** 2001
a5d40 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a September 15.**
a5d50 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
a5d60 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
a5d70 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
a5d80 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
a5d90 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
a5da0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
a5db0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
a5dc0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
a5dd0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
a5de0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
a5df0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
a5e00 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
a5e10 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
a5e20 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
a5e30 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
a5e40 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
a5e50 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
a5e60 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
a5e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
a5eb0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
a5ec0 6e 73 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 ns code to imple
a5ed0 6d 65 6e 74 20 61 20 70 73 65 75 64 6f 2d 72 61 ment a pseudo-ra
a5ee0 6e 64 6f 6d 20 6e 75 6d 62 65 72 0a 2a 2a 20 67 ndom number.** g
a5ef0 65 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 29 20 enerator (PRNG)
a5f00 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a for SQLite..**.*
a5f10 2a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 * Random numbers
a5f20 20 61 72 65 20 75 73 65 64 20 62 79 20 73 6f 6d are used by som
a5f30 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
a5f40 65 20 62 61 63 6b 65 6e 64 73 20 69 6e 20 6f 72 e backends in or
a5f50 64 65 72 0a 2a 2a 20 74 6f 20 67 65 6e 65 72 61 der.** to genera
a5f60 74 65 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 65 te random intege
a5f70 72 20 6b 65 79 73 20 66 6f 72 20 74 61 62 6c 65 r keys for table
a5f80 73 20 6f 72 20 72 61 6e 64 6f 6d 20 66 69 6c 65 s or random file
a5f90 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 names..**.** $Id
a5fa0 3a 20 72 61 6e 64 6f 6d 2e 63 2c 76 20 31 2e 32 : random.c,v 1.2
a5fb0 39 20 32 30 30 38 2f 31 32 2f 31 30 20 31 39 3a 9 2008/12/10 19:
a5fc0 32 36 3a 32 34 20 64 72 68 20 45 78 70 20 24 0a 26:24 drh Exp $.
a5fd0 2a 2f 0a 0a 0a 2f 2a 20 41 6c 6c 20 74 68 72 65 */.../* All thre
a5fe0 61 64 73 20 73 68 61 72 65 20 61 20 73 69 6e 67 ads share a sing
a5ff0 6c 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 le random number
a6000 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2a 20 54 generator..** T
a6010 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 his structure is
a6020 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 the current sta
a6030 74 65 20 6f 66 20 74 68 65 20 67 65 6e 65 72 61 te of the genera
a6040 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 tor..*/.static S
a6050 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 QLITE_WSD struct
a6060 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 sqlite3PrngType
a6070 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 {. unsigned ch
a6080 61 72 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20 ar isInit;
a6090 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 /* True if i
a60a0 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 nitialized */.
a60b0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 2c unsigned char i,
a60c0 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f j; /
a60d0 2a 20 53 74 61 74 65 20 76 61 72 69 61 62 6c 65 * State variable
a60e0 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 s */. unsigned
a60f0 63 68 61 72 20 73 5b 32 35 36 5d 3b 20 20 20 20 char s[256];
a6100 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 20 76 /* State v
a6110 61 72 69 61 62 6c 65 73 20 2a 2f 0a 7d 20 73 71 ariables */.} sq
a6120 6c 69 74 65 33 50 72 6e 67 3b 0a 0a 2f 2a 0a 2a lite3Prng;../*.*
a6130 2a 20 47 65 74 20 61 20 73 69 6e 67 6c 65 20 38 * Get a single 8
a6140 2d 62 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 -bit random valu
a6150 65 20 66 72 6f 6d 20 74 68 65 20 52 43 34 20 50 e from the RC4 P
a6160 52 4e 47 2e 20 20 54 68 65 20 4d 75 74 65 78 0a RNG. The Mutex.
a6170 2a 2a 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 ** must be held
a6180 77 68 69 6c 65 20 65 78 65 63 75 74 69 6e 67 20 while executing
a6190 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a this routine..**
a61a0 0a 2a 2a 20 57 68 79 20 6e 6f 74 20 6a 75 73 74 .** Why not just
a61b0 20 75 73 65 20 61 20 6c 69 62 72 61 72 79 20 72 use a library r
a61c0 61 6e 64 6f 6d 20 67 65 6e 65 72 61 74 6f 72 20 andom generator
a61d0 6c 69 6b 65 20 6c 72 61 6e 64 34 38 28 29 20 66 like lrand48() f
a61e0 6f 72 20 74 68 69 73 3f 0a 2a 2a 20 42 65 63 61 or this?.** Beca
a61f0 75 73 65 20 74 68 65 20 4f 50 5f 4e 65 77 52 6f use the OP_NewRo
a6200 77 69 64 20 6f 70 63 6f 64 65 20 69 6e 20 74 68 wid opcode in th
a6210 65 20 56 44 42 45 20 64 65 70 65 6e 64 73 20 6f e VDBE depends o
a6220 6e 20 68 61 76 69 6e 67 20 61 20 76 65 72 79 0a n having a very.
a6230 2a 2a 20 67 6f 6f 64 20 73 6f 75 72 63 65 20 6f ** good source o
a6240 66 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 f random numbers
a6250 2e 20 20 54 68 65 20 6c 72 61 6e 64 34 38 28 29 . The lrand48()
a6260 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f library functio
a6270 6e 20 6d 61 79 0a 2a 2a 20 77 65 6c 6c 20 62 65 n may.** well be
a6280 20 67 6f 6f 64 20 65 6e 6f 75 67 68 2e 20 20 42 good enough. B
a6290 75 74 20 6d 61 79 62 65 20 6e 6f 74 2e 20 20 4f ut maybe not. O
a62a0 72 20 6d 61 79 62 65 20 6c 72 61 6e 64 34 38 28 r maybe lrand48(
a62b0 29 20 68 61 73 20 73 6f 6d 65 0a 2a 2a 20 73 75 ) has some.** su
a62c0 62 74 6c 65 20 70 72 6f 62 6c 65 6d 73 20 6f 6e btle problems on
a62d0 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 74 68 some systems th
a62e0 61 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 70 at could cause p
a62f0 72 6f 62 6c 65 6d 73 2e 20 20 49 74 20 69 73 20 roblems. It is
a6300 68 61 72 64 0a 2a 2a 20 74 6f 20 6b 6e 6f 77 2e hard.** to know.
a6310 20 20 54 6f 20 6d 69 6e 69 6d 69 7a 65 20 74 68 To minimize th
a6320 65 20 72 69 73 6b 20 6f 66 20 70 72 6f 62 6c 65 e risk of proble
a6330 6d 73 20 64 75 65 20 74 6f 20 62 61 64 20 6c 72 ms due to bad lr
a6340 61 6e 64 34 38 28 29 0a 2a 2a 20 69 6d 70 6c 65 and48().** imple
a6350 6d 65 6e 74 61 74 69 6f 6e 73 2c 20 53 51 4c 69 mentations, SQLi
a6360 74 65 20 75 73 65 73 20 74 68 69 73 20 72 61 6e te uses this ran
a6370 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 dom number gener
a6380 61 74 6f 72 20 62 61 73 65 64 0a 2a 2a 20 6f 6e ator based.** on
a6390 20 52 43 34 2c 20 77 68 69 63 68 20 77 65 20 6b RC4, which we k
a63a0 6e 6f 77 20 77 6f 72 6b 73 20 76 65 72 79 20 77 now works very w
a63b0 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 28 4c 61 74 65 ell..**.** (Late
a63c0 72 29 3a 20 20 41 63 74 75 61 6c 6c 79 2c 20 4f r): Actually, O
a63d0 50 5f 4e 65 77 52 6f 77 69 64 20 64 6f 65 73 20 P_NewRowid does
a63e0 6e 6f 74 20 64 65 70 65 6e 64 20 6f 6e 20 61 20 not depend on a
a63f0 67 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 0a 2a good source of.*
a6400 2a 20 72 61 6e 64 6f 6d 6e 65 73 73 20 61 6e 79 * randomness any
a6410 20 6d 6f 72 65 2e 20 20 42 75 74 20 77 65 20 77 more. But we w
a6420 69 6c 6c 20 6c 65 61 76 65 20 74 68 69 73 20 63 ill leave this c
a6430 6f 64 65 20 69 6e 20 61 6c 6c 20 74 68 65 20 73 ode in all the s
a6440 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 ame..*/.static u
a6450 38 20 72 61 6e 64 6f 6d 42 79 74 65 28 76 6f 69 8 randomByte(voi
a6460 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 d){. unsigned c
a6470 68 61 72 20 74 3b 0a 0a 0a 20 20 2f 2a 20 54 68 har t;... /* Th
a6480 65 20 22 77 73 64 50 72 6e 67 22 20 6d 61 63 72 e "wsdPrng" macr
a6490 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 o will resolve t
a64a0 6f 20 74 68 65 20 70 73 65 75 64 6f 2d 72 61 6e o the pseudo-ran
a64b0 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 dom number gener
a64c0 61 74 6f 72 0a 20 20 2a 2a 20 73 74 61 74 65 20 ator. ** state
a64d0 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74 vector. If writ
a64e0 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 able static data
a64f0 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 is unsupported
a6500 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a 20 on the target,.
a6510 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c ** we have to l
a6520 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65 20 ocate the state
a6530 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 vector at run-ti
a6540 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 me. In the more
a6550 20 63 6f 6d 6d 6f 6e 0a 20 20 2a 2a 20 63 61 73 common. ** cas
a6560 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 e where writable
a6570 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 static data is
a6580 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 50 72 supported, wsdPr
a6590 6e 67 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 ng can refer dir
a65a0 65 63 74 6c 79 0a 20 20 2a 2a 20 74 6f 20 74 68 ectly. ** to th
a65b0 65 20 22 73 71 6c 69 74 65 33 50 72 6e 67 22 20 e "sqlite3Prng"
a65c0 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 state vector dec
a65d0 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 20 20 2a lared above.. *
a65e0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
a65f0 4f 4d 49 54 5f 57 53 44 0a 20 20 73 74 72 75 63 OMIT_WSD. struc
a6600 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 t sqlite3PrngTyp
a6610 65 20 2a 70 20 3d 20 26 47 4c 4f 42 41 4c 28 73 e *p = &GLOBAL(s
a6620 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e truct sqlite3Prn
a6630 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 gType, sqlite3Pr
a6640 6e 67 29 3b 0a 23 20 64 65 66 69 6e 65 20 77 73 ng);.# define ws
a6650 64 50 72 6e 67 20 70 5b 30 5d 0a 23 65 6c 73 65 dPrng p[0].#else
a6660 0a 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e .# define wsdPrn
a6670 67 20 73 71 6c 69 74 65 33 50 72 6e 67 0a 23 65 g sqlite3Prng.#e
a6680 6e 64 69 66 0a 0a 0a 20 20 2f 2a 20 49 6e 69 74 ndif... /* Init
a6690 69 61 6c 69 7a 65 20 74 68 65 20 73 74 61 74 65 ialize the state
a66a0 20 6f 66 20 74 68 65 20 72 61 6e 64 6f 6d 20 6e of the random n
a66b0 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 umber generator
a66c0 6f 6e 63 65 2c 0a 20 20 2a 2a 20 74 68 65 20 66 once,. ** the f
a66d0 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 irst time this r
a66e0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
a66f0 2e 20 20 54 68 65 20 73 65 65 64 20 76 61 6c 75 . The seed valu
a6700 65 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 e does. ** not
a6710 6e 65 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 need to contain
a6720 61 20 6c 6f 74 20 6f 66 20 72 61 6e 64 6f 6d 6e a lot of randomn
a6730 65 73 73 20 73 69 6e 63 65 20 77 65 20 61 72 65 ess since we are
a6740 20 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 69 6e 67 not. ** trying
a6750 20 74 6f 20 64 6f 20 73 65 63 75 72 65 20 65 6e to do secure en
a6760 63 72 79 70 74 69 6f 6e 20 6f 72 20 61 6e 79 74 cryption or anyt
a6770 68 69 6e 67 20 6c 69 6b 65 20 74 68 61 74 2e 2e hing like that..
a6780 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 68 .. **. ** Noth
a6790 69 6e 67 20 69 6e 20 74 68 69 73 20 66 69 6c 65 ing in this file
a67a0 20 6f 72 20 61 6e 79 77 68 65 72 65 20 65 6c 73 or anywhere els
a67b0 65 20 69 6e 20 53 51 4c 69 74 65 20 64 6f 65 73 e in SQLite does
a67c0 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 20 20 2a any kind of. *
a67d0 2a 20 65 6e 63 72 79 70 74 69 6f 6e 2e 20 20 54 * encryption. T
a67e0 68 65 20 52 43 34 20 61 6c 67 6f 72 69 74 68 6d he RC4 algorithm
a67f0 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 61 is being used a
a6800 73 20 61 20 50 52 4e 47 20 28 70 73 65 75 64 6f s a PRNG (pseudo
a6810 2d 72 61 6e 64 6f 6d 0a 20 20 2a 2a 20 6e 75 6d -random. ** num
a6820 62 65 72 20 67 65 6e 65 72 61 74 6f 72 29 20 6e ber generator) n
a6830 6f 74 20 61 73 20 61 6e 20 65 6e 63 72 79 70 74 ot as an encrypt
a6840 69 6f 6e 20 64 65 76 69 63 65 2e 0a 20 20 2a 2f ion device.. */
a6850 0a 20 20 69 66 28 20 21 77 73 64 50 72 6e 67 2e . if( !wsdPrng.
a6860 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 69 6e isInit ){. in
a6870 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20 6b 5b t i;. char k[
a6880 32 35 36 5d 3b 0a 20 20 20 20 77 73 64 50 72 6e 256];. wsdPrn
a6890 67 2e 6a 20 3d 20 30 3b 0a 20 20 20 20 77 73 64 g.j = 0;. wsd
a68a0 50 72 6e 67 2e 69 20 3d 20 30 3b 0a 20 20 20 20 Prng.i = 0;.
a68b0 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e sqlite3OsRandomn
a68c0 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 5f ess(sqlite3_vfs_
a68d0 66 69 6e 64 28 30 29 2c 20 32 35 36 2c 20 6b 29 find(0), 256, k)
a68e0 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
a68f0 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 <256; i++){.
a6900 20 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 3d wsdPrng.s[i] =
a6910 20 28 75 38 29 69 3b 0a 20 20 20 20 7d 0a 20 20 (u8)i;. }.
a6920 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36 for(i=0; i<256
a6930 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 73 ; i++){. ws
a6940 64 50 72 6e 67 2e 6a 20 2b 3d 20 77 73 64 50 72 dPrng.j += wsdPr
a6950 6e 67 2e 73 5b 69 5d 20 2b 20 6b 5b 69 5d 3b 0a ng.s[i] + k[i];.
a6960 20 20 20 20 20 20 74 20 3d 20 77 73 64 50 72 6e t = wsdPrn
a6970 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a g.s[wsdPrng.j];.
a6980 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e 73 5b wsdPrng.s[
a6990 77 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 77 73 64 wsdPrng.j] = wsd
a69a0 50 72 6e 67 2e 73 5b 69 5d 3b 0a 20 20 20 20 20 Prng.s[i];.
a69b0 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 wsdPrng.s[i] =
a69c0 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 73 64 t;. }. wsd
a69d0 50 72 6e 67 2e 69 73 49 6e 69 74 20 3d 20 31 3b Prng.isInit = 1;
a69e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 . }.. /* Gener
a69f0 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 73 ate and return s
a6a00 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 62 79 74 ingle random byt
a6a10 65 0a 20 20 2a 2f 0a 20 20 77 73 64 50 72 6e 67 e. */. wsdPrng
a6a20 2e 69 2b 2b 3b 0a 20 20 74 20 3d 20 77 73 64 50 .i++;. t = wsdP
a6a30 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 69 5d rng.s[wsdPrng.i]
a6a40 3b 0a 20 20 77 73 64 50 72 6e 67 2e 6a 20 2b 3d ;. wsdPrng.j +=
a6a50 20 74 3b 0a 20 20 77 73 64 50 72 6e 67 2e 73 5b t;. wsdPrng.s[
a6a60 77 73 64 50 72 6e 67 2e 69 5d 20 3d 20 77 73 64 wsdPrng.i] = wsd
a6a70 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a Prng.s[wsdPrng.j
a6a80 5d 3b 0a 20 20 77 73 64 50 72 6e 67 2e 73 5b 77 ];. wsdPrng.s[w
a6a90 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 74 3b 0a 20 sdPrng.j] = t;.
a6aa0 20 74 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b t += wsdPrng.s[
a6ab0 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 72 65 wsdPrng.i];. re
a6ac0 74 75 72 6e 20 77 73 64 50 72 6e 67 2e 73 5b 74 turn wsdPrng.s[t
a6ad0 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ];.}../*.** Retu
a6ae0 72 6e 20 4e 20 72 61 6e 64 6f 6d 20 62 79 74 65 rn N random byte
a6af0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 s..*/.SQLITE_API
a6b00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 61 void sqlite3_ra
a6b10 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e 2c 20 ndomness(int N,
a6b20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 20 75 void *pBuf){. u
a6b30 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 42 nsigned char *zB
a6b40 75 66 20 3d 20 70 42 75 66 3b 0a 23 69 66 20 53 uf = pBuf;.#if S
a6b50 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
a6b60 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
a6b70 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 *mutex = sqlite
a6b80 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
a6b90 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
a6ba0 50 52 4e 47 29 3b 0a 23 65 6e 64 69 66 0a 20 20 PRNG);.#endif.
a6bb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
a6bc0 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 77 68 ter(mutex);. wh
a6bd0 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 ile( N-- ){.
a6be0 2a 28 7a 42 75 66 2b 2b 29 20 3d 20 72 61 6e 64 *(zBuf++) = rand
a6bf0 6f 6d 42 79 74 65 28 29 3b 0a 20 20 7d 0a 20 20 omByte();. }.
a6c00 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
a6c10 61 76 65 28 6d 75 74 65 78 29 3b 0a 7d 0a 0a 23 ave(mutex);.}..#
a6c20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
a6c30 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a IT_BUILTIN_TEST.
a6c40 2f 2a 0a 2a 2a 20 46 6f 72 20 74 65 73 74 69 6e /*.** For testin
a6c50 67 20 70 75 72 70 6f 73 65 73 2c 20 77 65 20 73 g purposes, we s
a6c60 6f 6d 65 74 69 6d 65 73 20 77 61 6e 74 20 74 6f ometimes want to
a6c70 20 70 72 65 73 65 72 76 65 20 74 68 65 20 73 74 preserve the st
a6c80 61 74 65 20 6f 66 0a 2a 2a 20 50 52 4e 47 20 61 ate of.** PRNG a
a6c90 6e 64 20 72 65 73 74 6f 72 65 20 74 68 65 20 50 nd restore the P
a6ca0 52 4e 47 20 74 6f 20 69 74 73 20 73 61 76 65 64 RNG to its saved
a6cb0 20 73 74 61 74 65 20 61 74 20 61 20 6c 61 74 65 state at a late
a6cc0 72 20 74 69 6d 65 2c 20 6f 72 0a 2a 2a 20 74 6f r time, or.** to
a6cd0 20 72 65 73 65 74 20 74 68 65 20 50 52 4e 47 20 reset the PRNG
a6ce0 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73 to its initial s
a6cf0 74 61 74 65 2e 20 20 54 68 65 73 65 20 72 6f 75 tate. These rou
a6d00 74 69 6e 65 73 20 61 63 63 6f 6d 70 6c 69 73 68 tines accomplish
a6d10 0a 2a 2a 20 74 68 6f 73 65 20 74 61 73 6b 73 2e .** those tasks.
a6d20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
a6d30 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 e3_test_control(
a6d40 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c ) interface call
a6d50 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 s these routines
a6d60 20 74 6f 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 20 74 to.** control t
a6d70 68 65 20 50 52 4e 47 2e 0a 2a 2f 0a 73 74 61 74 he PRNG..*/.stat
a6d80 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 ic SQLITE_WSD st
a6d90 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 ruct sqlite3Prng
a6da0 54 79 70 65 20 73 71 6c 69 74 65 33 53 61 76 65 Type sqlite3Save
a6db0 64 50 72 6e 67 3b 0a 53 51 4c 49 54 45 5f 50 52 dPrng;.SQLITE_PR
a6dc0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
a6dd0 65 33 50 72 6e 67 53 61 76 65 53 74 61 74 65 28 e3PrngSaveState(
a6de0 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79 28 void){. memcpy(
a6df0 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 . &GLOBAL(str
a6e00 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 uct sqlite3PrngT
a6e10 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61 76 65 ype, sqlite3Save
a6e20 64 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 4c 4f dPrng),. &GLO
a6e30 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 BAL(struct sqlit
a6e40 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 e3PrngType, sqli
a6e50 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 73 69 te3Prng),. si
a6e60 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e 67 zeof(sqlite3Prng
a6e70 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f ). );.}.SQLITE_
a6e80 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
a6e90 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 53 ite3PrngRestoreS
a6ea0 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 6d 65 tate(void){. me
a6eb0 6d 63 70 79 28 0a 20 20 20 20 26 47 4c 4f 42 41 mcpy(. &GLOBA
a6ec0 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 L(struct sqlite3
a6ed0 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 PrngType, sqlite
a6ee0 33 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 4c 4f 3Prng),. &GLO
a6ef0 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 BAL(struct sqlit
a6f00 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 e3PrngType, sqli
a6f10 74 65 33 53 61 76 65 64 50 72 6e 67 29 2c 0a 20 te3SavedPrng),.
a6f20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 sizeof(sqlite
a6f30 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a 53 51 3Prng). );.}.SQ
a6f40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
a6f50 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 d sqlite3PrngRes
a6f60 65 74 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 etState(void){.
a6f70 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 GLOBAL(struct s
a6f80 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 qlite3PrngType,
a6f90 73 71 6c 69 74 65 33 50 72 6e 67 29 2e 69 73 49 sqlite3Prng).isI
a6fa0 6e 69 74 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 nit = 0;.}.#endi
a6fb0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
a6fc0 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f _BUILTIN_TEST */
a6fd0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
a6fe0 2a 20 45 6e 64 20 6f 66 20 72 61 6e 64 6f 6d 2e * End of random.
a6ff0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
a7000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7020 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
a7030 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75 74 66 * Begin file utf
a7040 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
a7050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7070 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 /./*.** 2004 Apr
a7080 69 6c 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 il 13.**.** The
a7090 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
a70a0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
a70b0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
a70c0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
a70d0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
a70e0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
a70f0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
a7100 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
a7110 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
a7120 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
a7130 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
a7140 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
a7150 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
a7160 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
a7170 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
a7180 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
a7190 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
a71a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a71b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a71c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a71d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a71e0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
a71f0 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69 e contains routi
a7200 6e 65 73 20 75 73 65 64 20 74 6f 20 74 72 61 6e nes used to tran
a7210 73 6c 61 74 65 20 62 65 74 77 65 65 6e 20 55 54 slate between UT
a7220 46 2d 38 2c 20 0a 2a 2a 20 55 54 46 2d 31 36 2c F-8, .** UTF-16,
a7230 20 55 54 46 2d 31 36 42 45 2c 20 61 6e 64 20 55 UTF-16BE, and U
a7240 54 46 2d 31 36 4c 45 2e 0a 2a 2a 0a 2a 2a 20 24 TF-16LE..**.** $
a7250 49 64 3a 20 75 74 66 2e 63 2c 76 20 31 2e 37 33 Id: utf.c,v 1.73
a7260 20 32 30 30 39 2f 30 34 2f 30 31 20 31 38 3a 34 2009/04/01 18:4
a7270 30 3a 33 32 20 64 72 68 20 45 78 70 20 24 0a 2a 0:32 drh Exp $.*
a7280 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54 *.** Notes on UT
a7290 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 20 42 79 74 F-8:.**.** Byt
a72a0 65 2d 30 20 20 20 20 42 79 74 65 2d 31 20 20 20 e-0 Byte-1
a72b0 20 42 79 74 65 2d 32 20 20 20 20 42 79 74 65 2d Byte-2 Byte-
a72c0 33 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20 30 3 Value.** 0
a72d0 78 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20 xxxxxxx
a72e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a72f0 20 20 20 20 20 20 20 20 30 30 30 30 30 30 30 30 00000000
a7300 20 30 30 30 30 30 30 30 30 20 30 78 78 78 78 78 00000000 0xxxxx
a7310 78 78 0a 2a 2a 20 20 31 31 30 79 79 79 79 79 20 xx.** 110yyyyy
a7320 20 31 30 78 78 78 78 78 78 20 20 20 20 20 20 20 10xxxxxx
a7330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7340 30 30 30 30 30 30 30 30 20 30 30 30 30 30 79 79 00000000 00000yy
a7350 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 31 y yyxxxxxx.** 1
a7360 31 31 30 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 110zzzz 10yyyyy
a7370 79 20 20 31 30 78 78 78 78 78 78 20 20 20 20 20 y 10xxxxxx
a7380 20 20 20 20 20 20 20 20 30 30 30 30 30 30 30 30 00000000
a7390 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 zzzzyyyy yyxxxx
a73a0 78 78 0a 2a 2a 20 20 31 31 31 31 30 75 75 75 20 xx.** 11110uuu
a73b0 20 31 30 75 75 7a 7a 7a 7a 20 20 31 30 79 79 79 10uuzzzz 10yyy
a73c0 79 79 79 20 20 31 30 78 78 78 78 78 78 20 20 20 yyy 10xxxxxx
a73d0 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79 000uuuuu zzzzyyy
a73e0 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a 2a y yyxxxxxx.**.**
a73f0 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54 46 .** Notes on UTF
a7400 2d 31 36 3a 20 20 28 77 69 74 68 20 77 77 77 77 -16: (with wwww
a7410 2b 31 3d 3d 75 75 75 75 75 29 0a 2a 2a 0a 2a 2a +1==uuuuu).**.**
a7420 20 20 20 20 20 20 57 6f 72 64 2d 30 20 20 20 20 Word-0
a7430 20 20 20 20 20 20 20 20 20 20 20 57 6f 72 64 2d Word-
a7440 31 20 20 20 20 20 20 20 20 20 20 56 61 6c 75 65 1 Value
a7450 0a 2a 2a 20 20 31 31 30 31 31 30 77 77 20 77 77 .** 110110ww ww
a7460 7a 7a 7a 7a 79 79 20 20 20 31 31 30 31 31 31 79 zzzzyy 110111y
a7470 79 20 79 79 78 78 78 78 78 78 20 20 20 20 30 30 y yyxxxxxx 00
a7480 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 0uuuuu zzzzyyyy
a7490 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 7a 7a 7a yyxxxxxx.** zzz
a74a0 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 20 20 zyyyy yyxxxxxx
a74b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a74c0 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20 7a 00000000 z
a74d0 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 zzzyyyy yyxxxxxx
a74e0 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 42 4f 4d 20 6f 72 .**.**.** BOM or
a74f0 20 42 79 74 65 20 4f 72 64 65 72 20 4d 61 72 6b Byte Order Mark
a7500 3a 0a 2a 2a 20 20 20 20 20 30 78 66 66 20 30 78 :.** 0xff 0x
a7510 66 65 20 20 20 6c 69 74 74 6c 65 2d 65 6e 64 69 fe little-endi
a7520 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 an utf-16 follow
a7530 73 0a 2a 2a 20 20 20 20 20 30 78 66 65 20 30 78 s.** 0xfe 0x
a7540 66 66 20 20 20 62 69 67 2d 65 6e 64 69 61 6e 20 ff big-endian
a7550 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a utf-16 follows.*
a7560 2a 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.*/./**********
a7570 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 **** Include vdb
a7580 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 eInt.h in the mi
a7590 64 64 6c 65 20 6f 66 20 75 74 66 2e 63 20 2a 2a ddle of utf.c **
a75a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a75b0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
a75c0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
a75d0 76 64 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a vdbeInt.h ******
a75e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a75f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7600 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 ***/./*.** 2003
a7610 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a September 6.**.*
a7620 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
a7630 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
a7640 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
a7650 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
a7660 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
a7670 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
a7680 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
a7690 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
a76a0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
a76b0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
a76c0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
a76d0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
a76e0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
a76f0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
a7700 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
a7710 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
a7720 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
a7730 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
a7740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
a7780 69 73 20 69 73 20 74 68 65 20 68 65 61 64 65 72 is is the header
a7790 20 66 69 6c 65 20 66 6f 72 20 69 6e 66 6f 72 6d file for inform
a77a0 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 70 72 ation that is pr
a77b0 69 76 61 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 ivate to the.**
a77c0 56 44 42 45 2e 20 20 54 68 69 73 20 69 6e 66 6f VDBE. This info
a77d0 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 rmation used to
a77e0 61 6c 6c 20 62 65 20 61 74 20 74 68 65 20 74 6f all be at the to
a77f0 70 20 6f 66 20 74 68 65 20 73 69 6e 67 6c 65 0a p of the single.
a7800 2a 2a 20 73 6f 75 72 63 65 20 63 6f 64 65 20 66 ** source code f
a7810 69 6c 65 20 22 76 64 62 65 2e 63 22 2e 20 20 57 ile "vdbe.c". W
a7820 68 65 6e 20 74 68 61 74 20 66 69 6c 65 20 62 65 hen that file be
a7830 63 61 6d 65 20 74 6f 6f 20 62 69 67 20 28 6f 76 came too big (ov
a7840 65 72 0a 2a 2a 20 36 30 30 30 20 6c 69 6e 65 73 er.** 6000 lines
a7850 20 6c 6f 6e 67 29 20 69 74 20 77 61 73 20 73 70 long) it was sp
a7860 6c 69 74 20 75 70 20 69 6e 74 6f 20 73 65 76 65 lit up into seve
a7870 72 61 6c 20 73 6d 61 6c 6c 65 72 20 66 69 6c 65 ral smaller file
a7880 73 20 61 6e 64 0a 2a 2a 20 74 68 69 73 20 68 65 s and.** this he
a7890 61 64 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ader information
a78a0 20 77 61 73 20 66 61 63 74 6f 72 65 64 20 6f 75 was factored ou
a78b0 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 t..**.** $Id: vd
a78c0 62 65 49 6e 74 2e 68 2c 76 20 31 2e 31 37 34 20 beInt.h,v 1.174
a78d0 32 30 30 39 2f 30 36 2f 32 33 20 31 34 3a 31 35 2009/06/23 14:15
a78e0 3a 30 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :04 drh Exp $.*/
a78f0 0a 23 69 66 6e 64 65 66 20 5f 56 44 42 45 49 4e .#ifndef _VDBEIN
a7900 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 56 44 T_H_.#define _VD
a7910 42 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 BEINT_H_../*.**
a7920 53 51 4c 20 69 73 20 74 72 61 6e 73 6c 61 74 65 SQL is translate
a7930 64 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e 63 d into a sequenc
a7940 65 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e e of instruction
a7950 73 20 74 6f 20 62 65 0a 2a 2a 20 65 78 65 63 75 s to be.** execu
a7960 74 65 64 20 62 79 20 61 20 76 69 72 74 75 61 6c ted by a virtual
a7970 20 6d 61 63 68 69 6e 65 2e 20 20 45 61 63 68 20 machine. Each
a7980 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 instruction is a
a7990 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 n instance.** of
a79a0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
a79b0 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 tructure..*/.typ
a79c0 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 edef struct Vdbe
a79d0 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 42 6f Op Op;../*.** Bo
a79e0 6f 6c 65 61 6e 20 76 61 6c 75 65 73 0a 2a 2f 0a olean values.*/.
a79f0 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 typedef unsigned
a7a00 20 63 68 61 72 20 42 6f 6f 6c 3b 0a 0a 2f 2a 0a char Bool;../*.
a7a10 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 61 ** A cursor is a
a7a20 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61 20 pointer into a
a7a30 73 69 6e 67 6c 65 20 42 54 72 65 65 20 77 69 74 single BTree wit
a7a40 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 hin a database f
a7a50 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 ile..** The curs
a7a60 6f 72 20 63 61 6e 20 73 65 65 6b 20 74 6f 20 61 or can seek to a
a7a70 20 42 54 72 65 65 20 65 6e 74 72 79 20 77 69 74 BTree entry wit
a7a80 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 6b h a particular k
a7a90 65 79 2c 20 6f 72 0a 2a 2a 20 6c 6f 6f 70 20 6f ey, or.** loop o
a7aa0 76 65 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 ver all entries
a7ab0 6f 66 20 74 68 65 20 42 74 72 65 65 2e 20 20 59 of the Btree. Y
a7ac0 6f 75 20 63 61 6e 20 61 6c 73 6f 20 69 6e 73 65 ou can also inse
a7ad0 72 74 20 6e 65 77 20 42 54 72 65 65 0a 2a 2a 20 rt new BTree.**
a7ae0 65 6e 74 72 69 65 73 20 6f 72 20 72 65 74 72 69 entries or retri
a7af0 65 76 65 20 74 68 65 20 6b 65 79 20 6f 72 20 64 eve the key or d
a7b00 61 74 61 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 ata from the ent
a7b10 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 ry that the curs
a7b20 6f 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 or.** is current
a7b30 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a ly pointing to..
a7b40 2a 2a 20 0a 2a 2a 20 45 76 65 72 79 20 63 75 72 ** .** Every cur
a7b50 73 6f 72 20 74 68 61 74 20 74 68 65 20 76 69 72 sor that the vir
a7b60 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 73 tual machine has
a7b70 20 6f 70 65 6e 20 69 73 20 72 65 70 72 65 73 65 open is represe
a7b80 6e 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e nted by an.** in
a7b90 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
a7ba0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
a7bb0 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
a7bc0 56 64 62 65 43 75 72 73 6f 72 2e 69 73 54 72 69 VdbeCursor.isTri
a7bd0 67 67 65 72 52 6f 77 20 66 6c 61 67 20 69 73 20 ggerRow flag is
a7be0 73 65 74 20 69 74 20 6d 65 61 6e 73 20 74 68 61 set it means tha
a7bf0 74 20 74 68 69 73 20 63 75 72 73 6f 72 20 69 73 t this cursor is
a7c00 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 20 73 69 6e .** really a sin
a7c10 67 6c 65 20 72 6f 77 20 74 68 61 74 20 72 65 70 gle row that rep
a7c20 72 65 73 65 6e 74 73 20 74 68 65 20 4e 45 57 20 resents the NEW
a7c30 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 or OLD pseudo-ta
a7c40 62 6c 65 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 20 ble of.** a row
a7c50 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 64 61 trigger. The da
a7c60 74 61 20 66 6f 72 20 74 68 65 20 72 6f 77 20 69 ta for the row i
a7c70 73 20 73 74 6f 72 65 64 20 69 6e 20 56 64 62 65 s stored in Vdbe
a7c80 43 75 72 73 6f 72 2e 70 44 61 74 61 20 61 6e 64 Cursor.pData and
a7c90 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 69 73 .** the rowid is
a7ca0 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72 2e 69 in VdbeCursor.i
a7cb0 4b 65 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 Key..*/.struct V
a7cc0 64 62 65 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 dbeCursor {. Bt
a7cd0 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b Cursor *pCursor;
a7ce0 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f /* The curso
a7cf0 72 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 74 r structure of t
a7d00 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 20 20 he backend */.
a7d10 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 int iDb;
a7d20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
a7d30 66 20 63 75 72 73 6f 72 20 64 61 74 61 62 61 73 f cursor databas
a7d40 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 28 e in db->aDb[] (
a7d50 6f 72 20 2d 31 29 20 2a 2f 0a 20 20 69 36 34 20 or -1) */. i64
a7d60 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 20 lastRowid;
a7d70 20 20 2f 2a 20 4c 61 73 74 20 72 6f 77 69 64 20 /* Last rowid
a7d80 66 72 6f 6d 20 61 20 4e 65 78 74 20 6f 72 20 4e from a Next or N
a7d90 65 78 74 49 64 78 20 6f 70 65 72 61 74 69 6f 6e extIdx operation
a7da0 20 2a 2f 0a 20 20 42 6f 6f 6c 20 7a 65 72 6f 65 */. Bool zeroe
a7db0 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 d; /* T
a7dc0 72 75 65 20 69 66 20 7a 65 72 6f 65 64 20 6f 75 rue if zeroed ou
a7dd0 74 20 61 6e 64 20 72 65 61 64 79 20 66 6f 72 20 t and ready for
a7de0 72 65 75 73 65 20 2a 2f 0a 20 20 42 6f 6f 6c 20 reuse */. Bool
a7df0 72 6f 77 69 64 49 73 56 61 6c 69 64 3b 20 20 20 rowidIsValid;
a7e00 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 61 73 74 /* True if last
a7e10 52 6f 77 69 64 20 69 73 20 76 61 6c 69 64 20 2a Rowid is valid *
a7e20 2f 0a 20 20 42 6f 6f 6c 20 61 74 46 69 72 73 74 /. Bool atFirst
a7e30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ; /* Tru
a7e40 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f e if pointing to
a7e50 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a first entry */.
a7e60 20 20 42 6f 6f 6c 20 75 73 65 52 61 6e 64 6f 6d Bool useRandom
a7e70 52 6f 77 69 64 3b 20 20 2f 2a 20 47 65 6e 65 72 Rowid; /* Gener
a7e80 61 74 65 20 6e 65 77 20 72 65 63 6f 72 64 20 6e ate new record n
a7e90 75 6d 62 65 72 73 20 73 65 6d 69 2d 72 61 6e 64 umbers semi-rand
a7ea0 6f 6d 6c 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 6e omly */. Bool n
a7eb0 75 6c 6c 52 6f 77 3b 20 20 20 20 20 20 20 20 20 ullRow;
a7ec0 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e 74 /* True if point
a7ed0 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 77 69 74 ing to a row wit
a7ee0 68 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 42 h no data */. B
a7ef0 6f 6f 6c 20 64 65 66 65 72 72 65 64 4d 6f 76 65 ool deferredMove
a7f00 74 6f 3b 20 20 2f 2a 20 41 20 63 61 6c 6c 20 74 to; /* A call t
a7f10 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f o sqlite3BtreeMo
a7f20 76 65 74 6f 28 29 20 69 73 20 6e 65 65 64 65 64 veto() is needed
a7f30 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 54 61 62 */. Bool isTab
a7f40 6c 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 le; /* T
a7f50 72 75 65 20 69 66 20 61 20 74 61 62 6c 65 20 72 rue if a table r
a7f60 65 71 75 69 72 69 6e 67 20 69 6e 74 65 67 65 72 equiring integer
a7f70 20 6b 65 79 73 20 2a 2f 0a 20 20 42 6f 6f 6c 20 keys */. Bool
a7f80 69 73 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 isIndex;
a7f90 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 69 /* True if an i
a7fa0 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ndex containing
a7fb0 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64 keys only - no d
a7fc0 61 74 61 20 2a 2f 0a 20 20 69 36 34 20 6d 6f 76 ata */. i64 mov
a7fd0 65 74 6f 54 61 72 67 65 74 3b 20 20 20 20 20 2f etoTarget; /
a7fe0 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 * Argument to th
a7ff0 65 20 64 65 66 65 72 72 65 64 20 73 71 6c 69 74 e deferred sqlit
a8000 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 e3BtreeMoveto()
a8010 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b */. Btree *pBt;
a8020 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 /* Se
a8030 70 61 72 61 74 65 20 66 69 6c 65 20 68 6f 6c 64 parate file hold
a8040 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61 ing temporary ta
a8050 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 70 73 65 ble */. int pse
a8060 75 64 6f 54 61 62 6c 65 52 65 67 3b 20 20 20 2f udoTableReg; /
a8070 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 * Register holdi
a8080 6e 67 20 70 73 65 75 64 6f 74 61 62 6c 65 20 63 ng pseudotable c
a8090 6f 6e 74 65 6e 74 2e 20 2a 2f 0a 20 20 4b 65 79 ontent. */. Key
a80a0 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 Info *pKeyInfo;
a80b0 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74 /* Info about
a80c0 20 69 6e 64 65 78 20 6b 65 79 73 20 6e 65 65 64 index keys need
a80d0 65 64 20 62 79 20 69 6e 64 65 78 20 63 75 72 73 ed by index curs
a80e0 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 ors */. int nFi
a80f0 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f eld; /
a8100 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c * Number of fiel
a8110 64 73 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 ds in the header
a8120 20 2a 2f 0a 20 20 69 36 34 20 73 65 71 43 6f 75 */. i64 seqCou
a8130 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 nt; /* S
a8140 65 71 75 65 6e 63 65 20 63 6f 75 6e 74 65 72 20 equence counter
a8150 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 */. sqlite3_vta
a8160 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 b_cursor *pVtabC
a8170 75 72 73 6f 72 3b 20 20 2f 2a 20 54 68 65 20 63 ursor; /* The c
a8180 75 72 73 6f 72 20 66 6f 72 20 61 20 76 69 72 74 ursor for a virt
a8190 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 ual table */. c
a81a0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 onst sqlite3_mod
a81b0 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 ule *pModule;
a81c0 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 66 6f 72 20 /* Module for
a81d0 63 75 72 73 6f 72 20 70 56 74 61 62 43 75 72 73 cursor pVtabCurs
a81e0 6f 72 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 73 75 or */.. /* Resu
a81f0 6c 74 20 6f 66 20 6c 61 73 74 20 73 71 6c 69 74 lt of last sqlit
a8200 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 e3BtreeMoveto()
a8210 64 6f 6e 65 20 62 79 20 61 6e 20 4f 50 5f 4e 6f done by an OP_No
a8220 74 45 78 69 73 74 73 20 6f 72 20 0a 20 20 2a 2a tExists or . **
a8230 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 6f 70 63 OP_IsUnique opc
a8240 6f 64 65 20 6f 6e 20 74 68 69 73 20 63 75 72 73 ode on this curs
a8250 6f 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65 or. */. int see
a8260 6b 52 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a 20 43 kResult;.. /* C
a8270 61 63 68 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f ached informatio
a8280 6e 20 61 62 6f 75 74 20 74 68 65 20 68 65 61 64 n about the head
a8290 65 72 20 66 6f 72 20 74 68 65 20 64 61 74 61 20 er for the data
a82a0 72 65 63 6f 72 64 20 74 68 61 74 20 74 68 65 0a record that the.
a82b0 20 20 2a 2a 20 63 75 72 73 6f 72 20 69 73 20 63 ** cursor is c
a82c0 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e urrently pointin
a82d0 67 20 74 6f 2e 20 20 4f 6e 6c 79 20 76 61 6c 69 g to. Only vali
a82e0 64 20 69 66 20 63 61 63 68 65 53 74 61 74 75 73 d if cacheStatus
a82f0 20 6d 61 74 63 68 65 73 0a 20 20 2a 2a 20 56 64 matches. ** Vd
a8300 62 65 2e 63 61 63 68 65 43 74 72 2e 20 20 56 64 be.cacheCtr. Vd
a8310 62 65 2e 63 61 63 68 65 43 74 72 20 77 69 6c 6c be.cacheCtr will
a8320 20 6e 65 76 65 72 20 74 61 6b 65 20 6f 6e 20 74 never take on t
a8330 68 65 20 76 61 6c 75 65 20 6f 66 0a 20 20 2a 2a he value of. **
a8340 20 43 41 43 48 45 5f 53 54 41 4c 45 20 61 6e 64 CACHE_STALE and
a8350 20 73 6f 20 73 65 74 74 69 6e 67 20 63 61 63 68 so setting cach
a8360 65 53 74 61 74 75 73 3d 43 41 43 48 45 5f 53 54 eStatus=CACHE_ST
a8370 41 4c 45 20 67 75 61 72 61 6e 74 65 65 73 20 74 ALE guarantees t
a8380 68 61 74 0a 20 20 2a 2a 20 74 68 65 20 63 61 63 hat. ** the cac
a8390 68 65 20 69 73 20 6f 75 74 20 6f 66 20 64 61 74 he is out of dat
a83a0 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 52 6f e.. **. ** aRo
a83b0 77 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f w might point to
a83c0 20 28 65 70 68 65 6d 65 72 61 6c 29 20 64 61 74 (ephemeral) dat
a83d0 61 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e a for the curren
a83e0 74 20 72 6f 77 2c 20 6f 72 20 69 74 20 6d 69 67 t row, or it mig
a83f0 68 74 0a 20 20 2a 2a 20 62 65 20 4e 55 4c 4c 2e ht. ** be NULL.
a8400 0a 20 20 2a 2f 0a 20 20 75 33 32 20 63 61 63 68 . */. u32 cach
a8410 65 53 74 61 74 75 73 3b 20 20 20 20 20 20 2f 2a eStatus; /*
a8420 20 43 61 63 68 65 20 69 73 20 76 61 6c 69 64 20 Cache is valid
a8430 69 66 20 74 68 69 73 20 6d 61 74 63 68 65 73 20 if this matches
a8440 56 64 62 65 2e 63 61 63 68 65 43 74 72 20 2a 2f Vdbe.cacheCtr */
a8450 0a 20 20 69 6e 74 20 70 61 79 6c 6f 61 64 53 69 . int payloadSi
a8460 7a 65 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 ze; /* Tota
a8470 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 l number of byte
a8480 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 s in the record
a8490 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 70 65 3b */. u32 *aType;
a84a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79 /* Ty
a84b0 70 65 20 76 61 6c 75 65 73 20 66 6f 72 20 61 6c pe values for al
a84c0 6c 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 l entries in the
a84d0 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32 record */. u32
a84e0 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20 *aOffset;
a84f0 20 20 20 2f 2a 20 43 61 63 68 65 64 20 6f 66 66 /* Cached off
a8500 73 65 74 73 20 74 6f 20 74 68 65 20 73 74 61 72 sets to the star
a8510 74 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e t of each column
a8520 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 2a s data */. u8 *
a8530 61 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20 aRow;
a8540 20 20 2f 2a 20 44 61 74 61 20 66 6f 72 20 74 68 /* Data for th
a8550 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 69 e current row, i
a8560 66 20 61 6c 6c 20 6f 6e 20 6f 6e 65 20 70 61 67 f all on one pag
a8570 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 e */.};.typedef
a8580 73 74 72 75 63 74 20 56 64 62 65 43 75 72 73 6f struct VdbeCurso
a8590 72 20 56 64 62 65 43 75 72 73 6f 72 3b 0a 0a 2f r VdbeCursor;../
a85a0 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 75 62 2d *.** When a sub-
a85b0 70 72 6f 67 72 61 6d 20 69 73 20 65 78 65 63 75 program is execu
a85c0 74 65 64 20 28 4f 50 5f 50 72 6f 67 72 61 6d 29 ted (OP_Program)
a85d0 2c 20 61 20 73 74 72 75 63 74 75 72 65 20 6f 66 , a structure of
a85e0 20 74 68 69 73 20 74 79 70 65 0a 2a 2a 20 69 73 this type.** is
a85f0 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 73 74 allocated to st
a8600 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ore the current
a8610 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 72 6f value of the pro
a8620 67 72 61 6d 20 63 6f 75 6e 74 65 72 2c 20 61 73 gram counter, as
a8630 0a 2a 2a 20 77 65 6c 6c 20 61 73 20 74 68 65 20 .** well as the
a8640 63 75 72 72 65 6e 74 20 6d 65 6d 6f 72 79 20 63 current memory c
a8650 65 6c 6c 20 61 72 72 61 79 20 61 6e 64 20 76 61 ell array and va
a8660 72 69 6f 75 73 20 6f 74 68 65 72 20 66 72 61 6d rious other fram
a8670 65 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 76 61 e specific.** va
a8680 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 lues stored in t
a8690 68 65 20 56 64 62 65 20 73 74 72 75 63 74 2e 20 he Vdbe struct.
a86a0 57 68 65 6e 20 74 68 65 20 73 75 62 2d 70 72 6f When the sub-pro
a86b0 67 72 61 6d 20 69 73 20 66 69 6e 69 73 68 65 64 gram is finished
a86c0 2c 20 0a 2a 2a 20 74 68 65 73 65 20 76 61 6c 75 , .** these valu
a86d0 65 73 20 61 72 65 20 63 6f 70 69 65 64 20 62 61 es are copied ba
a86e0 63 6b 20 74 6f 20 74 68 65 20 56 64 62 65 20 66 ck to the Vdbe f
a86f0 72 6f 6d 20 74 68 65 20 56 64 62 65 46 72 61 6d rom the VdbeFram
a8700 65 20 73 74 72 75 63 74 75 72 65 2c 0a 2a 2a 20 e structure,.**
a8710 72 65 73 74 6f 72 69 6e 67 20 74 68 65 20 73 74 restoring the st
a8720 61 74 65 20 6f 66 20 74 68 65 20 56 4d 20 74 6f ate of the VM to
a8730 20 61 73 20 69 74 20 77 61 73 20 62 65 66 6f 72 as it was befor
a8740 65 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 e the sub-progra
a8750 6d 0a 2a 2a 20 62 65 67 61 6e 20 65 78 65 63 75 m.** began execu
a8760 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 72 61 6d ting..**.** Fram
a8770 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e es are stored in
a8780 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 a linked list h
a8790 65 61 64 65 64 20 61 74 20 56 64 62 65 2e 70 50 eaded at Vdbe.pP
a87a0 61 72 65 6e 74 2e 20 56 64 62 65 2e 70 50 61 72 arent. Vdbe.pPar
a87b0 65 6e 74 0a 2a 2a 20 69 73 20 74 68 65 20 70 61 ent.** is the pa
a87c0 72 65 6e 74 20 6f 66 20 74 68 65 20 63 75 72 72 rent of the curr
a87d0 65 6e 74 20 66 72 61 6d 65 2c 20 6f 72 20 7a 65 ent frame, or ze
a87e0 72 6f 20 69 66 20 74 68 65 20 63 75 72 72 65 6e ro if the curren
a87f0 74 20 66 72 61 6d 65 0a 2a 2a 20 69 73 20 74 68 t frame.** is th
a8800 65 20 6d 61 69 6e 20 56 64 62 65 20 70 72 6f 67 e main Vdbe prog
a8810 72 61 6d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ram..*/.typedef
a8820 73 74 72 75 63 74 20 56 64 62 65 46 72 61 6d 65 struct VdbeFrame
a8830 20 56 64 62 65 46 72 61 6d 65 3b 0a 73 74 72 75 VdbeFrame;.stru
a8840 63 74 20 56 64 62 65 46 72 61 6d 65 20 7b 0a 20 ct VdbeFrame {.
a8850 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 Vdbe *v;
a8860 20 20 20 20 20 20 20 20 20 2f 2a 20 56 4d 20 74 /* VM t
a8870 68 69 73 20 66 72 61 6d 65 20 62 65 6c 6f 6e 67 his frame belong
a8880 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 70 63 s to */. int pc
a8890 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a88a0 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 43 6f 75 /* Program Cou
a88b0 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 2a 61 4f nter */. Op *aO
a88c0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
a88d0 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 69 6e 73 /* Program ins
a88e0 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 tructions */. i
a88f0 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20 20 20 nt nOp;
a8900 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
a8910 66 20 61 4f 70 20 61 72 72 61 79 20 2a 2f 0a 20 f aOp array */.
a8920 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 Mem *aMem;
a8930 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 /* Arra
a8940 79 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c y of memory cell
a8950 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b s */. int nMem;
a8960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
a8970 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
a8980 69 65 73 20 69 6e 20 61 4d 65 6d 20 2a 2f 0a 20 ies in aMem */.
a8990 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 VdbeCursor **ap
a89a0 43 73 72 3b 20 20 20 20 20 2f 2a 20 45 6c 65 6d Csr; /* Elem
a89b0 65 6e 74 20 6f 66 20 56 64 62 65 20 63 75 72 73 ent of Vdbe curs
a89c0 6f 72 73 20 2a 2f 0a 20 20 75 31 36 20 6e 43 75 ors */. u16 nCu
a89d0 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 rsor;
a89e0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
a89f0 74 72 69 65 73 20 69 6e 20 61 70 43 73 72 20 2a tries in apCsr *
a8a00 2f 0a 20 20 76 6f 69 64 20 2a 74 6f 6b 65 6e 3b /. void *token;
a8a10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
a8a20 6f 70 79 20 6f 66 20 53 75 62 50 72 6f 67 72 61 opy of SubProgra
a8a30 6d 2e 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 m.token */. int
a8a40 20 6e 43 68 69 6c 64 4d 65 6d 3b 20 20 20 20 20 nChildMem;
a8a50 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
a8a60 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 f memory cells f
a8a70 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65 20 2a or child frame *
a8a80 2f 0a 20 20 69 6e 74 20 6e 43 68 69 6c 64 43 73 /. int nChildCs
a8a90 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e r; /* N
a8aa0 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 umber of cursors
a8ab0 20 66 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65 for child frame
a8ac0 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 6f */. i64 lastRo
a8ad0 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a wid; /*
a8ae0 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 Last insert row
a8af0 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73 74 id (sqlite3.last
a8b00 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e 74 20 Rowid) */. int
a8b10 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 nChange;
a8b20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 /* Statement
a8b30 20 63 68 61 6e 67 65 73 20 28 56 64 62 65 2e 6e changes (Vdbe.n
a8b40 43 68 61 6e 67 65 73 29 20 20 20 20 20 2a 2f 0a Changes) */.
a8b50 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 50 61 VdbeFrame *pPa
a8b60 72 65 6e 74 3b 20 20 20 20 20 2f 2a 20 50 61 72 rent; /* Par
a8b70 65 6e 74 20 6f 66 20 74 68 69 73 20 66 72 61 6d ent of this fram
a8b80 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 e */.};..#define
a8b90 20 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 70 29 VdbeFrameMem(p)
a8ba0 20 28 28 4d 65 6d 20 2a 29 26 28 28 75 38 20 2a ((Mem *)&((u8 *
a8bb0 29 70 29 5b 52 4f 55 4e 44 38 28 73 69 7a 65 6f )p)[ROUND8(sizeo
a8bc0 66 28 56 64 62 65 46 72 61 6d 65 29 29 5d 29 0a f(VdbeFrame))]).
a8bd0 0a 2f 2a 0a 2a 2a 20 41 20 76 61 6c 75 65 20 66 ./*.** A value f
a8be0 6f 72 20 56 64 62 65 43 75 72 73 6f 72 2e 63 61 or VdbeCursor.ca
a8bf0 63 68 65 56 61 6c 69 64 20 74 68 61 74 20 6d 65 cheValid that me
a8c00 61 6e 73 20 74 68 65 20 63 61 63 68 65 20 69 73 ans the cache is
a8c10 20 61 6c 77 61 79 73 20 69 6e 76 61 6c 69 64 2e always invalid.
a8c20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 41 43 48 .*/.#define CACH
a8c30 45 5f 53 54 41 4c 45 20 30 0a 0a 2f 2a 0a 2a 2a E_STALE 0../*.**
a8c40 20 49 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68 65 Internally, the
a8c50 20 76 64 62 65 20 6d 61 6e 69 70 75 6c 61 74 65 vdbe manipulate
a8c60 73 20 6e 65 61 72 6c 79 20 61 6c 6c 20 53 51 4c s nearly all SQL
a8c70 20 76 61 6c 75 65 73 20 61 73 20 4d 65 6d 0a 2a values as Mem.*
a8c80 2a 20 73 74 72 75 63 74 75 72 65 73 2e 20 45 61 * structures. Ea
a8c90 63 68 20 4d 65 6d 20 73 74 72 75 63 74 20 6d 61 ch Mem struct ma
a8ca0 79 20 63 61 63 68 65 20 6d 75 6c 74 69 70 6c 65 y cache multiple
a8cb0 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 representations
a8cc0 20 28 73 74 72 69 6e 67 2c 0a 2a 2a 20 69 6e 74 (string,.** int
a8cd0 65 67 65 72 20 65 74 63 2e 29 20 6f 66 20 74 68 eger etc.) of th
a8ce0 65 20 73 61 6d 65 20 76 61 6c 75 65 2e 20 20 41 e same value. A
a8cf0 20 76 61 6c 75 65 20 28 61 6e 64 20 74 68 65 72 value (and ther
a8d00 65 66 6f 72 65 20 4d 65 6d 20 73 74 72 75 63 74 efore Mem struct
a8d10 75 72 65 29 0a 2a 2a 20 68 61 73 20 74 68 65 20 ure).** has the
a8d20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 70 65 72 following proper
a8d30 74 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 45 61 63 68 ties:.**.** Each
a8d40 20 76 61 6c 75 65 20 68 61 73 20 61 20 6d 61 6e value has a man
a8d50 69 66 65 73 74 20 74 79 70 65 2e 20 54 68 65 20 ifest type. The
a8d60 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20 6f 66 manifest type of
a8d70 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 the value store
a8d80 64 0a 2a 2a 20 69 6e 20 61 20 4d 65 6d 20 73 74 d.** in a Mem st
a8d90 72 75 63 74 20 69 73 20 72 65 74 75 72 6e 65 64 ruct is returned
a8da0 20 62 79 20 74 68 65 20 4d 65 6d 54 79 70 65 28 by the MemType(
a8db0 4d 65 6d 2a 29 20 6d 61 63 72 6f 2e 20 54 68 65 Mem*) macro. The
a8dc0 20 74 79 70 65 20 69 73 0a 2a 2a 20 6f 6e 65 20 type is.** one
a8dd0 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 of SQLITE_NULL,
a8de0 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c 20 SQLITE_INTEGER,
a8df0 53 51 4c 49 54 45 5f 52 45 41 4c 2c 20 53 51 4c SQLITE_REAL, SQL
a8e00 49 54 45 5f 54 45 58 54 20 6f 72 0a 2a 2a 20 53 ITE_TEXT or.** S
a8e10 51 4c 49 54 45 5f 42 4c 4f 42 2e 0a 2a 2f 0a 73 QLITE_BLOB..*/.s
a8e20 74 72 75 63 74 20 4d 65 6d 20 7b 0a 20 20 75 6e truct Mem {. un
a8e30 69 6f 6e 20 7b 0a 20 20 20 20 69 36 34 20 69 3b ion {. i64 i;
a8e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a8e50 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 20 Integer value.
a8e60 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f */. int nZero
a8e70 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 ; /* Us
a8e80 65 64 20 77 68 65 6e 20 62 69 74 20 4d 45 4d 5f ed when bit MEM_
a8e90 5a 65 72 6f 20 69 73 20 73 65 74 20 69 6e 20 66 Zero is set in f
a8ea0 6c 61 67 73 20 2a 2f 0a 20 20 20 20 46 75 6e 63 lags */. Func
a8eb0 44 65 66 20 2a 70 44 65 66 3b 20 20 20 20 20 20 Def *pDef;
a8ec0 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68 65 /* Used only whe
a8ed0 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 41 67 67 n flags==MEM_Agg
a8ee0 20 2a 2f 0a 20 20 20 20 52 6f 77 53 65 74 20 2a */. RowSet *
a8ef0 70 52 6f 77 53 65 74 3b 20 20 20 20 2f 2a 20 55 pRowSet; /* U
a8f00 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c sed only when fl
a8f10 61 67 73 3d 3d 4d 45 4d 5f 52 6f 77 53 65 74 20 ags==MEM_RowSet
a8f20 2a 2f 0a 20 20 20 20 56 64 62 65 46 72 61 6d 65 */. VdbeFrame
a8f30 20 2a 70 46 72 61 6d 65 3b 20 20 2f 2a 20 55 73 *pFrame; /* Us
a8f40 65 64 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d ed when flags==M
a8f50 45 4d 5f 46 72 61 6d 65 20 2a 2f 0a 20 20 7d 20 EM_Frame */. }
a8f60 75 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20 20 u;. double r;
a8f70 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 6c /* Real
a8f80 20 76 61 6c 75 65 20 2a 2f 0a 20 20 73 71 6c 69 value */. sqli
a8f90 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
a8fa0 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 /* The associate
a8fb0 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 d database conne
a8fc0 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20 ction */. char
a8fd0 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f *z; /
a8fe0 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 * String or BLOB
a8ff0 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 value */. int
a9000 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
a9010 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 /* Number of cha
a9020 72 61 63 74 65 72 73 20 69 6e 20 73 74 72 69 6e racters in strin
a9030 67 20 76 61 6c 75 65 2c 20 65 78 63 6c 75 64 69 g value, excludi
a9040 6e 67 20 27 5c 30 27 20 2a 2f 0a 20 20 75 31 36 ng '\0' */. u16
a9050 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 flags;
a9060 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 /* Some combina
a9070 74 69 6f 6e 20 6f 66 20 4d 45 4d 5f 4e 75 6c 6c tion of MEM_Null
a9080 2c 20 4d 45 4d 5f 53 74 72 2c 20 4d 45 4d 5f 44 , MEM_Str, MEM_D
a9090 79 6e 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38 yn, etc. */. u8
a90a0 20 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 type;
a90b0 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 51 4c 49 /* One of SQLI
a90c0 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f TE_NULL, SQLITE_
a90d0 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 49 4e 54 TEXT, SQLITE_INT
a90e0 45 47 45 52 2c 20 65 74 63 20 2a 2f 0a 20 20 75 EGER, etc */. u
a90f0 38 20 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 8 enc;
a9100 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46 /* SQLITE_UTF
a9110 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 8, SQLITE_UTF16B
a9120 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c E, SQLITE_UTF16L
a9130 45 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 E */. void (*xD
a9140 65 6c 29 28 76 6f 69 64 20 2a 29 3b 20 20 2f 2a el)(void *); /*
a9150 20 49 66 20 6e 6f 74 20 6e 75 6c 6c 2c 20 63 61 If not null, ca
a9160 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ll this function
a9170 20 74 6f 20 64 65 6c 65 74 65 20 4d 65 6d 2e 7a to delete Mem.z
a9180 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c */. char *zMal
a9190 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 44 79 6e loc; /* Dyn
a91a0 61 6d 69 63 20 62 75 66 66 65 72 20 61 6c 6c 6f amic buffer allo
a91b0 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 cated by sqlite3
a91c0 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 0a _malloc() */.};.
a91d0 0a 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 ./* One or more
a91e0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
a91f0 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 74 flags are set t
a9200 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 76 o indicate the v
a9210 61 6c 69 64 4f 4b 0a 2a 2a 20 72 65 70 72 65 73 alidOK.** repres
a9220 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 entations of the
a9230 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e value stored in
a9240 20 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74 2e the Mem struct.
a9250 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 .**.** If the ME
a9260 4d 5f 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 73 M_Null flag is s
a9270 65 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c et, then the val
a9280 75 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c ue is an SQL NUL
a9290 4c 20 76 61 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 6f L value..** No o
a92a0 74 68 65 72 20 66 6c 61 67 73 20 6d 61 79 20 62 ther flags may b
a92b0 65 20 73 65 74 20 69 6e 20 74 68 69 73 20 63 61 e set in this ca
a92c0 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 se..**.** If the
a92d0 20 4d 45 4d 5f 53 74 72 20 66 6c 61 67 20 69 73 MEM_Str flag is
a92e0 20 73 65 74 20 74 68 65 6e 20 4d 65 6d 2e 7a 20 set then Mem.z
a92f0 70 6f 69 6e 74 73 20 61 74 20 61 20 73 74 72 69 points at a stri
a9300 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ng representatio
a9310 6e 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 74 68 n..** Usually th
a9320 69 73 20 69 73 20 65 6e 63 6f 64 65 64 20 69 6e is is encoded in
a9330 20 74 68 65 20 73 61 6d 65 20 75 6e 69 63 6f 64 the same unicod
a9340 65 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68 e encoding as th
a9350 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61 e main.** databa
a9360 73 65 20 28 73 65 65 20 62 65 6c 6f 77 20 66 6f se (see below fo
a9370 72 20 65 78 63 65 70 74 69 6f 6e 73 29 2e 20 49 r exceptions). I
a9380 66 20 74 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66 f the MEM_Term f
a9390 6c 61 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 73 lag is also.** s
a93a0 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72 et, then the str
a93b0 69 6e 67 20 69 73 20 6e 75 6c 20 74 65 72 6d 69 ing is nul termi
a93c0 6e 61 74 65 64 2e 20 54 68 65 20 4d 45 4d 5f 49 nated. The MEM_I
a93d0 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 nt and MEM_Real
a93e0 0a 2a 2a 20 66 6c 61 67 73 20 6d 61 79 20 63 6f .** flags may co
a93f0 65 78 69 73 74 20 77 69 74 68 20 74 68 65 20 4d exist with the M
a9400 45 4d 5f 53 74 72 20 66 6c 61 67 2e 0a 2a 2a 0a EM_Str flag..**.
a9410 2a 2a 20 4d 75 6c 74 69 70 6c 65 20 6f 66 20 74 ** Multiple of t
a9420 68 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e 20 hese values can
a9430 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 66 6c appear in Mem.fl
a9440 61 67 73 2e 20 20 42 75 74 20 6f 6e 6c 79 20 6f ags. But only o
a9450 6e 65 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 20 ne.** at a time
a9460 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20 4d 65 can appear in Me
a9470 6d 2e 74 79 70 65 2e 0a 2a 2f 0a 23 64 65 66 69 m.type..*/.#defi
a9480 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20 20 ne MEM_Null
a9490 20 30 78 30 30 30 31 20 20 20 2f 2a 20 56 61 6c 0x0001 /* Val
a94a0 75 65 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 ue is NULL */.#d
a94b0 65 66 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20 20 efine MEM_Str
a94c0 20 20 20 20 30 78 30 30 30 32 20 20 20 2f 2a 20 0x0002 /*
a94d0 56 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e Value is a strin
a94e0 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d g */.#define MEM
a94f0 5f 49 6e 74 20 20 20 20 20 20 20 30 78 30 30 30 _Int 0x000
a9500 34 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 4 /* Value is
a9510 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64 an integer */.#d
a9520 65 66 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20 20 efine MEM_Real
a9530 20 20 20 20 30 78 30 30 30 38 20 20 20 2f 2a 20 0x0008 /*
a9540 56 61 6c 75 65 20 69 73 20 61 20 72 65 61 6c 20 Value is a real
a9550 6e 75 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69 6e number */.#defin
a9560 65 20 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20 20 e MEM_Blob
a9570 30 78 30 30 31 30 20 20 20 2f 2a 20 56 61 6c 75 0x0010 /* Valu
a9580 65 20 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a 23 e is a BLOB */.#
a9590 64 65 66 69 6e 65 20 4d 45 4d 5f 52 6f 77 53 65 define MEM_RowSe
a95a0 74 20 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a t 0x0020 /*
a95b0 20 56 61 6c 75 65 20 69 73 20 61 20 52 6f 77 53 Value is a RowS
a95c0 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 65 et object */.#de
a95d0 66 69 6e 65 20 4d 45 4d 5f 46 72 61 6d 65 20 20 fine MEM_Frame
a95e0 20 20 20 30 78 30 30 34 30 20 20 20 2f 2a 20 56 0x0040 /* V
a95f0 61 6c 75 65 20 69 73 20 61 20 56 64 62 65 46 72 alue is a VdbeFr
a9600 61 6d 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 ame object */.#d
a9610 65 66 69 6e 65 20 4d 45 4d 5f 54 79 70 65 4d 61 efine MEM_TypeMa
a9620 73 6b 20 20 30 78 30 30 66 66 20 20 20 2f 2a 20 sk 0x00ff /*
a9630 4d 61 73 6b 20 6f 66 20 74 79 70 65 20 62 69 74 Mask of type bit
a9640 73 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 65 76 65 s */../* Wheneve
a9650 72 20 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20 61 r Mem contains a
a9660 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 6f 72 valid string or
a9670 20 62 6c 6f 62 20 72 65 70 72 65 73 65 6e 74 61 blob representa
a9680 74 69 6f 6e 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20 tion, one of.**
a9690 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c the following fl
a96a0 61 67 73 20 6d 75 73 74 20 62 65 20 73 65 74 20 ags must be set
a96b0 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
a96c0 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 memory manageme
a96d0 6e 74 0a 2a 2a 20 70 6f 6c 69 63 79 20 66 6f 72 nt.** policy for
a96e0 20 4d 65 6d 2e 7a 2e 20 20 54 68 65 20 4d 45 4d Mem.z. The MEM
a96f0 5f 54 65 72 6d 20 66 6c 61 67 20 74 65 6c 6c 73 _Term flag tells
a9700 20 75 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e us whether or n
a9710 6f 74 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 ot the.** string
a9720 20 69 73 20 5c 30 30 30 20 6f 72 20 5c 75 30 30 is \000 or \u00
a9730 30 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2f 00 terminated.*/
a9740 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 54 65 72 .#define MEM_Ter
a9750 6d 20 20 20 20 20 20 30 78 30 32 30 30 20 20 20 m 0x0200
a9760 2f 2a 20 53 74 72 69 6e 67 20 72 65 70 20 69 73 /* String rep is
a9770 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 20 nul terminated
a9780 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 44 */.#define MEM_D
a9790 79 6e 20 20 20 20 20 20 20 30 78 30 34 30 30 20 yn 0x0400
a97a0 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61 6c /* Need to cal
a97b0 6c 20 73 71 6c 69 74 65 46 72 65 65 28 29 20 6f l sqliteFree() o
a97c0 6e 20 4d 65 6d 2e 7a 20 2a 2f 0a 23 64 65 66 69 n Mem.z */.#defi
a97d0 6e 65 20 4d 45 4d 5f 53 74 61 74 69 63 20 20 20 ne MEM_Static
a97e0 20 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65 6d 0x0800 /* Mem
a97f0 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 .z points to a s
a9800 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a tatic string */.
a9810 23 64 65 66 69 6e 65 20 4d 45 4d 5f 45 70 68 65 #define MEM_Ephe
a9820 6d 20 20 20 20 20 30 78 31 30 30 30 20 20 20 2f m 0x1000 /
a9830 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 * Mem.z points t
a9840 6f 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 o an ephemeral s
a9850 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 tring */.#define
a9860 20 4d 45 4d 5f 41 67 67 20 20 20 20 20 20 20 30 MEM_Agg 0
a9870 78 32 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a x2000 /* Mem.z
a9880 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 67 points to an ag
a9890 67 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 g function conte
a98a0 78 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 xt */.#define ME
a98b0 4d 5f 5a 65 72 6f 20 20 20 20 20 20 30 78 34 30 M_Zero 0x40
a98c0 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 69 20 63 6f 00 /* Mem.i co
a98d0 6e 74 61 69 6e 73 20 63 6f 75 6e 74 20 6f 66 20 ntains count of
a98e0 30 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 62 0s appended to b
a98f0 6c 6f 62 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 lob */..#ifdef S
a9900 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
a9910 4c 4f 42 0a 20 20 23 75 6e 64 65 66 20 4d 45 4d LOB. #undef MEM
a9920 5f 5a 65 72 6f 0a 20 20 23 64 65 66 69 6e 65 20 _Zero. #define
a9930 4d 45 4d 5f 5a 65 72 6f 20 30 78 30 30 30 30 0a MEM_Zero 0x0000.
a9940 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 #endif.../*.** C
a9950 6c 65 61 72 20 61 6e 79 20 65 78 69 73 74 69 6e lear any existin
a9960 67 20 74 79 70 65 20 66 6c 61 67 73 20 66 72 6f g type flags fro
a9970 6d 20 61 20 4d 65 6d 20 61 6e 64 20 72 65 70 6c m a Mem and repl
a9980 61 63 65 20 74 68 65 6d 20 77 69 74 68 20 66 0a ace them with f.
a9990 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53 65 */.#define MemSe
a99a0 74 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29 20 tTypeFlag(p, f)
a99b0 5c 0a 20 20 20 28 28 70 29 2d 3e 66 6c 61 67 73 \. ((p)->flags
a99c0 20 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26 7e = ((p)->flags&~
a99d0 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 7c 4d 45 (MEM_TypeMask|ME
a99e0 4d 5f 5a 65 72 6f 29 29 7c 66 29 0a 0a 0a 2f 2a M_Zero))|f).../*
a99f0 20 41 20 56 64 62 65 46 75 6e 63 20 69 73 20 6a A VdbeFunc is j
a9a00 75 73 74 20 61 20 46 75 6e 63 44 65 66 20 28 64 ust a FuncDef (d
a9a10 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 efined in sqlite
a9a20 49 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e 74 Int.h) that cont
a9a30 61 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f 6e ains.** addition
a9a40 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 al information a
a9a50 62 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20 69 bout auxiliary i
a9a60 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e 64 nformation bound
a9a70 20 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a to arguments.**
a9a80 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e of the function
a9a90 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 . This is used
a9aa0 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
a9ab0 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 sqlite3_get_aux
a9ac0 64 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73 71 data().** and sq
a9ad0 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 lite3_set_auxdat
a9ae0 61 28 29 20 41 50 49 73 2e 20 20 54 68 65 20 22 a() APIs. The "
a9af0 61 75 78 64 61 74 61 22 20 69 73 20 73 6f 6d 65 auxdata" is some
a9b00 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 0a auxiliary data.
a9b10 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 61 ** that can be a
a9b20 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
a9b30 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d 65 constant argume
a9b40 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e nt to a function
a9b50 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77 . This.** allow
a9b60 73 20 66 75 6e 63 74 69 6f 6e 73 20 73 75 63 68 s functions such
a9b70 20 61 73 20 22 72 65 67 65 78 70 22 20 74 6f 20 as "regexp" to
a9b80 63 6f 6d 70 69 6c 65 20 74 68 65 69 72 20 63 6f compile their co
a9b90 6e 73 74 61 6e 74 20 72 65 67 75 6c 61 72 0a 2a nstant regular.*
a9ba0 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61 72 67 * expression arg
a9bb0 75 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20 72 ument once and r
a9bc0 65 75 73 65 64 20 74 68 65 20 63 6f 6d 70 69 6c eused the compil
a9bd0 65 64 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c 74 ed code for mult
a9be0 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 iple.** invocati
a9bf0 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 ons..*/.struct V
a9c00 64 62 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e 63 dbeFunc {. Func
a9c10 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 Def *pFunc;
a9c20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
a9c30 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 definition of t
a9c40 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 he function */.
a9c50 20 69 6e 74 20 6e 41 75 78 3b 20 20 20 20 20 20 int nAux;
a9c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
a9c70 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
a9c80 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f ies allocated fo
a9c90 72 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20 73 r apAux[] */. s
a9ca0 74 72 75 63 74 20 41 75 78 44 61 74 61 20 7b 0a truct AuxData {.
a9cb0 20 20 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 void *pAux;
a9cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a9cd0 20 20 2f 2a 20 41 75 78 20 64 61 74 61 20 66 6f /* Aux data fo
a9ce0 72 20 74 68 65 20 69 2d 74 68 20 61 72 67 75 6d r the i-th argum
a9cf0 65 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 ent */. void
a9d00 28 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 20 (*xDelete)(void
a9d10 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74 *); /* Dest
a9d20 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 61 ructor for the a
a9d30 75 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20 61 ux data */. } a
a9d40 70 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20 20 pAux[1];
a9d50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e /* On
a9d60 65 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68 20 e slot for each
a9d70 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e function argumen
a9d80 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 t */.};../*.** T
a9d90 68 65 20 22 63 6f 6e 74 65 78 74 22 20 61 72 67 he "context" arg
a9da0 75 6d 65 6e 74 20 66 6f 72 20 61 20 69 6e 73 74 ument for a inst
a9db0 61 6c 6c 61 62 6c 65 20 66 75 6e 63 74 69 6f 6e allable function
a9dc0 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 . A pointer to
a9dd0 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f an.** instance o
a9de0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
a9df0 20 69 73 20 74 68 65 20 66 69 72 73 74 20 61 72 is the first ar
a9e00 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 72 6f gument to the ro
a9e10 75 74 69 6e 65 73 20 75 73 65 64 0a 2a 2a 20 69 utines used.** i
a9e20 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c mplement the SQL
a9e30 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a functions..**.*
a9e40 2a 20 54 68 65 72 65 20 69 73 20 61 20 74 79 70 * There is a typ
a9e50 65 64 65 66 20 66 6f 72 20 74 68 69 73 20 73 74 edef for this st
a9e60 72 75 63 74 75 72 65 20 69 6e 20 73 71 6c 69 74 ructure in sqlit
a9e70 65 2e 68 2e 20 20 53 6f 20 61 6c 6c 20 72 6f 75 e.h. So all rou
a9e80 74 69 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 74 tines,.** even t
a9e90 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72 66 he public interf
a9ea0 61 63 65 20 74 6f 20 53 51 4c 69 74 65 2c 20 63 ace to SQLite, c
a9eb0 61 6e 20 75 73 65 20 61 20 70 6f 69 6e 74 65 72 an use a pointer
a9ec0 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 to this structu
a9ed0 72 65 2e 0a 2a 2a 20 42 75 74 20 74 68 69 73 20 re..** But this
a9ee0 66 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 file is the only
a9ef0 20 70 6c 61 63 65 20 77 68 65 72 65 20 74 68 65 place where the
a9f00 20 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69 6c internal detail
a9f10 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74 72 s of this.** str
a9f20 75 63 74 75 72 65 20 61 72 65 20 6b 6e 6f 77 6e ucture are known
a9f30 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 ..**.** This str
a9f40 75 63 74 75 72 65 20 69 73 20 64 65 66 69 6e 65 ucture is define
a9f50 64 20 69 6e 73 69 64 65 20 6f 66 20 76 64 62 65 d inside of vdbe
a9f60 49 6e 74 2e 68 20 62 65 63 61 75 73 65 20 69 74 Int.h because it
a9f70 20 75 73 65 73 20 73 75 62 73 74 72 75 63 74 75 uses substructu
a9f80 72 65 73 0a 2a 2a 20 28 4d 65 6d 29 20 77 68 69 res.** (Mem) whi
a9f90 63 68 20 61 72 65 20 6f 6e 6c 79 20 64 65 66 69 ch are only defi
a9fa0 6e 65 64 20 74 68 65 72 65 2e 0a 2a 2f 0a 73 74 ned there..*/.st
a9fb0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e ruct sqlite3_con
a9fc0 74 65 78 74 20 7b 0a 20 20 46 75 6e 63 44 65 66 text {. FuncDef
a9fd0 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 2f *pFunc; /
a9fe0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 75 6e * Pointer to fun
a9ff0 63 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f ction informatio
aa000 6e 2e 20 20 4d 55 53 54 20 42 45 20 46 49 52 53 n. MUST BE FIRS
aa010 54 20 2a 2f 0a 20 20 56 64 62 65 46 75 6e 63 20 T */. VdbeFunc
aa020 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a 20 *pVdbeFunc; /*
aa030 41 75 78 69 6c 61 72 79 20 64 61 74 61 2c 20 69 Auxilary data, i
aa040 66 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 20 20 f created. */.
aa050 4d 65 6d 20 73 3b 20 20 20 20 20 20 20 20 20 20 Mem s;
aa060 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 /* The ret
aa070 75 72 6e 20 76 61 6c 75 65 20 69 73 20 73 74 6f urn value is sto
aa080 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 4d 65 red here */. Me
aa090 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 m *pMem;
aa0a0 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 /* Memory ce
aa0b0 6c 6c 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 ll used to store
aa0c0 20 61 67 67 72 65 67 61 74 65 20 63 6f 6e 74 65 aggregate conte
aa0d0 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 xt */. int isEr
aa0e0 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ror; /*
aa0f0 20 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 Error code retu
aa100 72 6e 65 64 20 62 79 20 74 68 65 20 66 75 6e 63 rned by the func
aa110 74 69 6f 6e 2e 20 2a 2f 0a 20 20 43 6f 6c 6c 53 tion. */. CollS
aa120 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 eq *pColl;
aa130 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 /* Collating se
aa140 71 75 65 6e 63 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a quence */.};../*
aa150 0a 2a 2a 20 41 20 53 65 74 20 73 74 72 75 63 74 .** A Set struct
aa160 75 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 ure is used for
aa170 71 75 69 63 6b 20 74 65 73 74 69 6e 67 20 74 6f quick testing to
aa180 20 73 65 65 20 69 66 20 61 20 76 61 6c 75 65 0a see if a value.
aa190 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 ** is part of a
aa1a0 73 6d 61 6c 6c 20 73 65 74 2e 20 20 53 65 74 73 small set. Sets
aa1b0 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 are used to imp
aa1c0 6c 65 6d 65 6e 74 20 63 6f 64 65 20 6c 69 6b 65 lement code like
aa1d0 0a 2a 2a 20 74 68 69 73 3a 0a 2a 2a 20 20 20 20 .** this:.**
aa1e0 20 20 20 20 20 20 20 20 78 2e 79 20 49 4e 20 28 x.y IN (
aa1f0 27 68 69 27 2c 27 68 6f 6f 27 2c 27 68 75 6d 27 'hi','hoo','hum'
aa200 29 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ).*/.typedef str
aa210 75 63 74 20 53 65 74 20 53 65 74 3b 0a 73 74 72 uct Set Set;.str
aa220 75 63 74 20 53 65 74 20 7b 0a 20 20 48 61 73 68 uct Set {. Hash
aa230 20 68 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 hash;
aa240 20 20 20 2f 2a 20 41 20 73 65 74 20 69 73 20 6a /* A set is j
aa250 75 73 74 20 61 20 68 61 73 68 20 74 61 62 6c 65 ust a hash table
aa260 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a */. HashElem *
aa270 70 72 65 76 3b 20 20 20 20 20 20 20 20 2f 2a 20 prev; /*
aa280 50 72 65 76 69 6f 75 73 6c 79 20 61 63 63 65 73 Previously acces
aa290 73 65 64 20 68 61 73 68 20 65 6c 65 6d 65 6e 20 sed hash elemen
aa2a0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 */.};../*.** An
aa2b0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
aa2c0 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e virtual machine.
aa2d0 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 This structure
aa2e0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f contains the co
aa2f0 6d 70 6c 65 74 65 0a 2a 2a 20 73 74 61 74 65 20 mplete.** state
aa300 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d of the virtual m
aa310 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 achine..**.** Th
aa320 65 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 22 e "sqlite3_stmt"
aa330 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74 structure point
aa340 65 72 20 74 68 61 74 20 69 73 20 72 65 74 75 72 er that is retur
aa350 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 ned by sqlite3_c
aa360 6f 6d 70 69 6c 65 28 29 0a 2a 2a 20 69 73 20 72 ompile().** is r
aa370 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 eally a pointer
aa380 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f to an instance o
aa390 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
aa3a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 ..**.** The Vdbe
aa3b0 2e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 76 61 .inVtabMethod va
aa3c0 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f riable is set to
aa3d0 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20 74 68 non-zero for th
aa3e0 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 0a 2a 2a e duration of.**
aa3f0 20 61 6e 79 20 76 69 72 74 75 61 6c 20 74 61 62 any virtual tab
aa400 6c 65 20 6d 65 74 68 6f 64 20 69 6e 76 6f 63 61 le method invoca
aa410 74 69 6f 6e 73 20 6d 61 64 65 20 62 79 20 74 68 tions made by th
aa420 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 e vdbe program.
aa430 49 74 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 It is.** set to
aa440 32 20 66 6f 72 20 78 44 65 73 74 72 6f 79 20 6d 2 for xDestroy m
aa450 65 74 68 6f 64 20 63 61 6c 6c 73 20 61 6e 64 20 ethod calls and
aa460 31 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 1 for all other
aa470 6d 65 74 68 6f 64 73 2e 20 54 68 69 73 0a 2a 2a methods. This.**
aa480 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 variable is use
aa490 64 20 66 6f 72 20 74 77 6f 20 70 75 72 70 6f 73 d for two purpos
aa4a0 65 73 3a 20 74 6f 20 61 6c 6c 6f 77 20 78 44 65 es: to allow xDe
aa4b0 73 74 72 6f 79 20 6d 65 74 68 6f 64 73 20 74 6f stroy methods to
aa4c0 20 65 78 65 63 75 74 65 0a 2a 2a 20 22 44 52 4f execute.** "DRO
aa4d0 50 20 54 41 42 4c 45 22 20 73 74 61 74 65 6d 65 P TABLE" stateme
aa4e0 6e 74 73 20 61 6e 64 20 74 6f 20 70 72 65 76 65 nts and to preve
aa4f0 6e 74 20 73 6f 6d 65 20 6e 61 73 74 79 20 73 69 nt some nasty si
aa500 64 65 20 65 66 66 65 63 74 73 20 6f 66 0a 2a 2a de effects of.**
aa510 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 malloc failure
aa520 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 69 when SQLite is i
aa530 6e 76 6f 6b 65 64 20 72 65 63 75 72 73 69 76 65 nvoked recursive
aa540 6c 79 20 62 79 20 61 20 76 69 72 74 75 61 6c 20 ly by a virtual
aa550 74 61 62 6c 65 20 0a 2a 2a 20 6d 65 74 68 6f 64 table .** method
aa560 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 function..*/.st
aa570 72 75 63 74 20 56 64 62 65 20 7b 0a 20 20 73 71 ruct Vdbe {. sq
aa580 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 lite3 *db;
aa590 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 /* The dat
aa5a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
aa5b0 20 74 68 61 74 20 6f 77 6e 73 20 74 68 69 73 20 that owns this
aa5c0 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 56 statement */. V
aa5d0 64 62 65 20 2a 70 50 72 65 76 2c 2a 70 4e 65 78 dbe *pPrev,*pNex
aa5e0 74 3b 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 t; /* Linked
aa5f0 20 6c 69 73 74 20 6f 66 20 56 44 42 45 73 20 77 list of VDBEs w
aa600 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 64 62 ith the same Vdb
aa610 65 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f e.db */. int nO
aa620 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
aa630 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 /* Number of i
aa640 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 6e 20 74 nstructions in t
aa650 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 he program */.
aa660 69 6e 74 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20 20 int nOpAlloc;
aa670 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
aa680 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 r of slots alloc
aa690 61 74 65 64 20 66 6f 72 20 61 4f 70 5b 5d 20 2a ated for aOp[] *
aa6a0 2f 0a 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 20 /. Op *aOp;
aa6b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
aa6c0 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 pace to hold the
aa6d0 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
aa6e0 27 73 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 's program */.
aa6f0 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20 20 20 20 int nLabel;
aa700 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
aa710 72 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 65 64 r of labels used
aa720 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c */. int nLabel
aa730 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f 2a Alloc; /*
aa740 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 Number of slots
aa750 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 4c allocated in aL
aa760 61 62 65 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 abel[] */. int
aa770 2a 61 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 *aLabel;
aa780 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 /* Space to
aa790 68 6f 6c 64 20 74 68 65 20 6c 61 62 65 6c 73 20 hold the labels
aa7a0 2a 2f 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 */. Mem **apArg
aa7b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
aa7c0 41 72 67 75 6d 65 6e 74 73 20 74 6f 20 63 75 72 Arguments to cur
aa7d0 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 rently executing
aa7e0 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a user function *
aa7f0 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f 6c 4e 61 6d /. Mem *aColNam
aa800 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 e; /* C
aa810 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 72 olumn names to r
aa820 65 74 75 72 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a eturn */. Mem *
aa830 70 52 65 73 75 6c 74 53 65 74 3b 20 20 20 20 20 pResultSet;
aa840 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
aa850 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65 73 an array of res
aa860 75 6c 74 73 20 2a 2f 0a 20 20 75 31 36 20 6e 52 ults */. u16 nR
aa870 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 esColumn;
aa880 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
aa890 6f 6c 75 6d 6e 73 20 69 6e 20 6f 6e 65 20 72 6f olumns in one ro
aa8a0 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 w of the result
aa8b0 73 65 74 20 2a 2f 0a 20 20 75 31 36 20 6e 43 75 set */. u16 nCu
aa8c0 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 rsor;
aa8d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c /* Number of sl
aa8e0 6f 74 73 20 69 6e 20 61 70 43 73 72 5b 5d 20 2a ots in apCsr[] *
aa8f0 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
aa900 2a 61 70 43 73 72 3b 20 20 20 20 20 2f 2a 20 4f *apCsr; /* O
aa910 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 ne element of th
aa920 69 73 20 61 72 72 61 79 20 66 6f 72 20 65 61 63 is array for eac
aa930 68 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 2a 2f h open cursor */
aa940 0a 20 20 75 38 20 65 72 72 6f 72 41 63 74 69 6f . u8 errorActio
aa950 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 n; /* Re
aa960 63 6f 76 65 72 79 20 61 63 74 69 6f 6e 20 74 6f covery action to
aa970 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 61 do in case of a
aa980 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 75 38 20 n error */. u8
aa990 6f 6b 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 okVar;
aa9a0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
aa9b0 61 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65 6e azVar[] has been
aa9c0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
aa9d0 20 20 75 31 36 20 6e 56 61 72 3b 20 20 20 20 20 u16 nVar;
aa9e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
aa9f0 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
aaa00 6e 20 61 56 61 72 5b 5d 20 2a 2f 0a 20 20 4d 65 n aVar[] */. Me
aaa10 6d 20 2a 61 56 61 72 3b 20 20 20 20 20 20 20 20 m *aVar;
aaa20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 /* Values
aaa30 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72 69 61 for the OP_Varia
aaa40 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a 20 ble opcode. */.
aaa50 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b 20 20 char **azVar;
aaa60 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
aaa70 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 2a 2f of variables */
aaa80 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 . u32 magic;
aaa90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 /* Ma
aaaa0 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 73 gic number for s
aaab0 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a anity checking *
aaac0 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 /. int nMem;
aaad0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
aaae0 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 umber of memory
aaaf0 6c 6f 63 61 74 69 6f 6e 73 20 63 75 72 72 65 6e locations curren
aab00 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f tly allocated */
aab10 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 . Mem *aMem;
aab20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
aab30 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f e memory locatio
aab40 6e 73 20 2a 2f 0a 20 20 75 33 32 20 63 61 63 68 ns */. u32 cach
aab50 65 43 74 72 3b 20 20 20 20 20 20 20 20 20 20 20 eCtr;
aab60 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20 72 6f /* VdbeCursor ro
aab70 77 20 63 61 63 68 65 20 67 65 6e 65 72 61 74 69 w cache generati
aab80 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 on counter */.
aab90 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 int pc;
aaba0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 /* The p
aabb0 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a rogram counter *
aabc0 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
aabd0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
aabe0 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a alue to return *
aabf0 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 /. char *zErrMs
aac00 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 g; /* E
aac10 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72 69 rror message wri
aac20 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 75 tten here */. u
aac30 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20 8 explain;
aac40 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
aac50 66 20 45 58 50 4c 41 49 4e 20 70 72 65 73 65 6e f EXPLAIN presen
aac60 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 t on SQL command
aac70 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 43 */. u8 changeC
aac80 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a ntOn; /*
aac90 20 54 72 75 65 20 74 6f 20 75 70 64 61 74 65 20 True to update
aaca0 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
aacb0 65 72 20 2a 2f 0a 20 20 75 38 20 65 78 70 69 72 er */. u8 expir
aacc0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ed;
aacd0 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 56 /* True if the V
aace0 4d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 M needs to be re
aacf0 63 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20 20 75 38 compiled */. u8
aad00 20 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 minWriteFileFor
aad10 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d mat; /* Minimum
aad20 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f 72 file format for
aad30 20 77 72 69 74 61 62 6c 65 20 64 61 74 61 62 61 writable databa
aad40 73 65 20 66 69 6c 65 73 20 2a 2f 0a 20 20 75 38 se files */. u8
aad50 20 69 6e 56 74 61 62 4d 65 74 68 6f 64 3b 20 20 inVtabMethod;
aad60 20 20 20 20 20 20 2f 2a 20 53 65 65 20 63 6f 6d /* See com
aad70 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0a 20 ments above */.
aad80 20 75 38 20 75 73 65 73 53 74 6d 74 4a 6f 75 72 u8 usesStmtJour
aad90 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 nal; /* True
aada0 20 69 66 20 75 73 65 73 20 61 20 73 74 61 74 65 if uses a state
aadb0 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a ment journal */.
aadc0 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 u8 readOnly;
aadd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
aade0 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 e for read-only
aadf0 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 statements */.
aae00 75 38 20 69 73 50 72 65 70 61 72 65 56 32 3b 20 u8 isPrepareV2;
aae10 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
aae20 69 66 20 70 72 65 70 61 72 65 64 20 77 69 74 68 if prepared with
aae30 20 70 72 65 70 61 72 65 5f 76 32 28 29 20 2a 2f prepare_v2() */
aae40 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20 . int nChange;
aae50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
aae60 6d 62 65 72 20 6f 66 20 64 62 20 63 68 61 6e 67 mber of db chang
aae70 65 73 20 6d 61 64 65 20 73 69 6e 63 65 20 6c 61 es made since la
aae80 73 74 20 72 65 73 65 74 20 2a 2f 0a 20 20 69 6e st reset */. in
aae90 74 20 62 74 72 65 65 4d 61 73 6b 3b 20 20 20 20 t btreeMask;
aaea0 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b /* Bitmask
aaeb0 20 6f 66 20 64 62 2d 3e 61 44 62 5b 5d 20 65 6e of db->aDb[] en
aaec0 74 72 69 65 73 20 72 65 66 65 72 65 6e 63 65 64 tries referenced
aaed0 20 2a 2f 0a 20 20 69 36 34 20 73 74 61 72 74 54 */. i64 startT
aaee0 69 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ime; /*
aaef0 20 54 69 6d 65 20 77 68 65 6e 20 71 75 65 72 79 Time when query
aaf00 20 73 74 61 72 74 65 64 20 2d 20 75 73 65 64 20 started - used
aaf10 66 6f 72 20 70 72 6f 66 69 6c 69 6e 67 20 2a 2f for profiling */
aaf20 0a 20 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 . BtreeMutexArr
aaf30 61 79 20 61 4d 75 74 65 78 3b 20 2f 2a 20 41 6e ay aMutex; /* An
aaf40 20 61 72 72 61 79 20 6f 66 20 42 74 72 65 65 20 array of Btree
aaf50 75 73 65 64 20 68 65 72 65 20 61 6e 64 20 6e 65 used here and ne
aaf60 65 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 eding locks */.
aaf70 20 69 6e 74 20 61 43 6f 75 6e 74 65 72 5b 32 5d int aCounter[2]
aaf80 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e ; /* Coun
aaf90 74 65 72 73 20 75 73 65 64 20 62 79 20 73 71 6c ters used by sql
aafa0 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 ite3_stmt_status
aafb0 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 () */. char *zS
aafc0 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ql;
aafd0 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 53 /* Text of the S
aafe0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 QL statement tha
aaff0 74 20 67 65 6e 65 72 61 74 65 64 20 74 68 69 73 t generated this
ab000 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 46 72 65 */. void *pFre
ab010 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
ab020 20 46 72 65 65 20 74 68 69 73 20 77 68 65 6e 20 Free this when
ab030 64 65 6c 65 74 69 6e 67 20 74 68 65 20 76 64 62 deleting the vdb
ab040 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 74 e */. int iStat
ab050 65 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20 2f ement; /
ab060 2a 20 53 74 61 74 65 6d 65 6e 74 20 6e 75 6d 62 * Statement numb
ab070 65 72 20 28 6f 72 20 30 20 69 66 20 68 61 73 20 er (or 0 if has
ab080 6e 6f 74 20 6f 70 65 6e 65 64 20 73 74 6d 74 29 not opened stmt)
ab090 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
ab0a0 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a E_DEBUG. FILE *
ab0b0 74 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 trace;
ab0c0 20 20 2f 2a 20 57 72 69 74 65 20 61 6e 20 65 78 /* Write an ex
ab0d0 65 63 75 74 69 6f 6e 20 74 72 61 63 65 20 68 65 ecution trace he
ab0e0 72 65 2c 20 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 re, if not NULL
ab0f0 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 56 64 62 65 */.#endif. Vdbe
ab100 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 Frame *pFrame;
ab110 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 66 72 /* Parent fr
ab120 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 ame */. int nFr
ab130 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ame;
ab140 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 /* Number of fr
ab150 61 6d 65 73 20 69 6e 20 70 46 72 61 6d 65 20 6c ames in pFrame l
ab160 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ist */.};../*.**
ab170 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 The following a
ab180 72 65 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 re allowed value
ab190 73 20 66 6f 72 20 56 64 62 65 2e 6d 61 67 69 63 s for Vdbe.magic
ab1a0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 .*/.#define VDBE
ab1b0 5f 4d 41 47 49 43 5f 49 4e 49 54 20 20 20 20 20 _MAGIC_INIT
ab1c0 30 78 32 36 62 63 65 61 61 35 20 20 20 20 2f 2a 0x26bceaa5 /*
ab1d0 20 42 75 69 6c 64 69 6e 67 20 61 20 56 44 42 45 Building a VDBE
ab1e0 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 23 64 65 66 program */.#def
ab1f0 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 ine VDBE_MAGIC_R
ab200 55 4e 20 20 20 20 20 20 30 78 62 64 66 32 30 64 UN 0xbdf20d
ab210 61 33 20 20 20 20 2f 2a 20 56 44 42 45 20 69 73 a3 /* VDBE is
ab220 20 72 65 61 64 79 20 74 6f 20 65 78 65 63 75 74 ready to execut
ab230 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 e */.#define VDB
ab240 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 20 20 20 E_MAGIC_HALT
ab250 20 30 78 35 31 39 63 32 39 37 33 20 20 20 20 2f 0x519c2973 /
ab260 2a 20 56 44 42 45 20 68 61 73 20 63 6f 6d 70 6c * VDBE has compl
ab270 65 74 65 64 20 65 78 65 63 75 74 69 6f 6e 20 2a eted execution *
ab280 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d /.#define VDBE_M
ab290 41 47 49 43 5f 44 45 41 44 20 20 20 20 20 30 78 AGIC_DEAD 0x
ab2a0 62 36 30 36 63 33 63 38 20 20 20 20 2f 2a 20 54 b606c3c8 /* T
ab2b0 68 65 20 56 44 42 45 20 68 61 73 20 62 65 65 6e he VDBE has been
ab2c0 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a deallocated */.
ab2d0 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 ./*.** Function
ab2e0 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 prototypes.*/.SQ
ab2f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
ab300 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 d sqlite3VdbeFre
ab310 65 43 75 72 73 6f 72 28 56 64 62 65 20 2a 2c 20 eCursor(Vdbe *,
ab320 56 64 62 65 43 75 72 73 6f 72 2a 29 3b 0a 76 6f VdbeCursor*);.vo
ab330 69 64 20 73 71 6c 69 74 65 56 64 62 65 50 6f 70 id sqliteVdbePop
ab340 53 74 61 63 6b 28 56 64 62 65 2a 2c 69 6e 74 29 Stack(Vdbe*,int)
ab350 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
ab360 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
ab370 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 CursorMoveto(Vdb
ab380 65 43 75 72 73 6f 72 2a 29 3b 0a 23 69 66 20 64 eCursor*);.#if d
ab390 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
ab3a0 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 BUG) || defined(
ab3b0 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 0a 53 51 VDBE_PROFILE).SQ
ab3c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
ab3d0 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 d sqlite3VdbePri
ab3e0 6e 74 4f 70 28 46 49 4c 45 2a 2c 20 69 6e 74 2c ntOp(FILE*, int,
ab3f0 20 4f 70 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 Op*);.#endif.SQ
ab400 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 LITE_PRIVATE u32
ab410 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
ab420 61 6c 54 79 70 65 4c 65 6e 28 75 33 32 29 3b 0a alTypeLen(u32);.
ab430 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 SQLITE_PRIVATE u
ab440 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 32 sqlite3VdbeSe
ab450 72 69 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 69 rialType(Mem*, i
ab460 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
ab470 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 ATE u32 sqlite3V
ab480 64 62 65 53 65 72 69 61 6c 50 75 74 28 75 6e 73 dbeSerialPut(uns
ab490 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 igned char*, int
ab4a0 2c 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 , Mem*, int);.SQ
ab4b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 LITE_PRIVATE u32
ab4c0 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
ab4d0 61 6c 47 65 74 28 63 6f 6e 73 74 20 75 6e 73 69 alGet(const unsi
ab4e0 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 2c gned char*, u32,
ab4f0 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Mem*);.SQLITE_P
ab500 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
ab510 74 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 te3VdbeDeleteAux
ab520 44 61 74 61 28 56 64 62 65 46 75 6e 63 2a 2c 20 Data(VdbeFunc*,
ab530 69 6e 74 29 3b 0a 0a 69 6e 74 20 73 71 6c 69 74 int);..int sqlit
ab540 65 32 42 74 72 65 65 4b 65 79 43 6f 6d 70 61 72 e2BtreeKeyCompar
ab550 65 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 63 6f e(BtCursor *, co
ab560 6e 73 74 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c nst void *, int,
ab570 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 int, int *);.SQ
ab580 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
ab590 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b sqlite3VdbeIdxK
ab5a0 65 79 43 6f 6d 70 61 72 65 28 56 64 62 65 43 75 eyCompare(VdbeCu
ab5b0 72 73 6f 72 2a 2c 55 6e 70 61 63 6b 65 64 52 65 rsor*,UnpackedRe
ab5c0 63 6f 72 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c cord*,int*);.SQL
ab5d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
ab5e0 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f sqlite3VdbeIdxRo
ab5f0 77 69 64 28 73 71 6c 69 74 65 33 2a 2c 20 42 74 wid(sqlite3*, Bt
ab600 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29 Cursor *, i64 *)
ab610 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
ab620 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 43 int sqlite3MemC
ab630 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d ompare(const Mem
ab640 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 *, const Mem*, c
ab650 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 2a 29 3b 0a onst CollSeq*);.
ab660 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
ab670 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 nt sqlite3VdbeEx
ab680 65 63 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 ec(Vdbe*);.SQLIT
ab690 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
ab6a0 6c 69 74 65 33 56 64 62 65 4c 69 73 74 28 56 64 lite3VdbeList(Vd
ab6b0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
ab6c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
ab6d0 56 64 62 65 48 61 6c 74 28 56 64 62 65 2a 29 3b VdbeHalt(Vdbe*);
ab6e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ab6f0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 int sqlite3VdbeC
ab700 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 hangeEncoding(Me
ab710 6d 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 m *, int);.SQLIT
ab720 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
ab730 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 lite3VdbeMemTooB
ab740 69 67 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 ig(Mem*);.SQLITE
ab750 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
ab760 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 ite3VdbeMemCopy(
ab770 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a Mem*, const Mem*
ab780 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
ab790 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
ab7a0 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 beMemShallowCopy
ab7b0 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d (Mem*, const Mem
ab7c0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
ab7d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
ab7e0 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 ite3VdbeMemMove(
ab7f0 4d 65 6d 2a 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c Mem*, Mem*);.SQL
ab800 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
ab810 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 sqlite3VdbeMemNu
ab820 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d 2a 29 lTerminate(Mem*)
ab830 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
ab840 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
ab850 4d 65 6d 53 65 74 53 74 72 28 4d 65 6d 2a 2c 20 MemSetStr(Mem*,
ab860 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 const char*, int
ab870 2c 20 75 38 2c 20 76 6f 69 64 28 2a 29 28 76 6f , u8, void(*)(vo
ab880 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 id*));.SQLITE_PR
ab890 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
ab8a0 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 e3VdbeMemSetInt6
ab8b0 34 28 4d 65 6d 2a 2c 20 69 36 34 29 3b 0a 53 51 4(Mem*, i64);.SQ
ab8c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
ab8d0 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
ab8e0 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 2a 2c 20 SetDouble(Mem*,
ab8f0 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f double);.SQLITE_
ab900 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
ab910 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
ab920 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 ll(Mem*);.SQLITE
ab930 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
ab940 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a lite3VdbeMemSetZ
ab950 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e 74 eroBlob(Mem*,int
ab960 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
ab970 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
ab980 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 4d beMemSetRowSet(M
ab990 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
ab9a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
ab9b0 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 VdbeMemMakeWrite
ab9c0 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 able(Mem*);.SQLI
ab9d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
ab9e0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 qlite3VdbeMemStr
ab9f0 69 6e 67 69 66 79 28 4d 65 6d 2a 2c 20 69 6e 74 ingify(Mem*, int
aba00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
aba10 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 64 62 E i64 sqlite3Vdb
aba20 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 2a 29 3b eIntValue(Mem*);
aba30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
aba40 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
aba50 65 6d 49 6e 74 65 67 65 72 69 66 79 28 4d 65 6d emIntegerify(Mem
aba60 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
aba70 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 TE double sqlite
aba80 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 4d 3VdbeRealValue(M
aba90 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
abaa0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
abab0 33 56 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 3VdbeIntegerAffi
abac0 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 nity(Mem*);.SQLI
abad0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
abae0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 qlite3VdbeMemRea
abaf0 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 lify(Mem*);.SQLI
abb00 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
abb10 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d qlite3VdbeMemNum
abb20 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c erify(Mem*);.SQL
abb30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
abb40 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 sqlite3VdbeMemFr
abb50 6f 6d 42 74 72 65 65 28 42 74 43 75 72 73 6f 72 omBtree(BtCursor
abb60 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 4d 65 *,int,int,int,Me
abb70 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
abb80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
abb90 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d VdbeMemRelease(M
abba0 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 em *p);.SQLITE_P
abbb0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
abbc0 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
abbd0 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 eExternal(Mem *p
abbe0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
abbf0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
abc00 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 4d 65 6d eMemFinalize(Mem
abc10 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 *, FuncDef*);.SQ
abc20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
abc30 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
abc40 4f 70 63 6f 64 65 4e 61 6d 65 28 69 6e 74 29 3b OpcodeName(int);
abc50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
abc60 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4f int sqlite3VdbeO
abc70 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79 pcodeHasProperty
abc80 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 (int, int);.SQLI
abc90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
abca0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
abcb0 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 w(Mem *pMem, int
abcc0 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65 n, int preserve
abcd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
abce0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
abcf0 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 eCloseStatement(
abd00 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 Vdbe *, int);.SQ
abd10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
abd20 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 61 d sqlite3VdbeFra
abd30 6d 65 44 65 6c 65 74 65 28 56 64 62 65 46 72 61 meDelete(VdbeFra
abd40 6d 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 me*);.SQLITE_PRI
abd50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
abd60 56 64 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65 VdbeFrameRestore
abd70 28 56 64 62 65 46 72 61 6d 65 20 2a 29 3b 0a 23 (VdbeFrame *);.#
abd80 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
abd90 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 BLE_MEMORY_MANAG
abda0 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 EMENT.SQLITE_PRI
abdb0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
abdc0 56 64 62 65 52 65 6c 65 61 73 65 42 75 66 66 65 VdbeReleaseBuffe
abdd0 72 73 28 56 64 62 65 20 2a 70 29 3b 0a 23 65 6e rs(Vdbe *p);.#en
abde0 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
abdf0 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
abe00 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 CACHE.SQLITE_PRI
abe10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
abe20 33 56 64 62 65 4d 75 74 65 78 41 72 72 61 79 45 3VdbeMutexArrayE
abe30 6e 74 65 72 28 56 64 62 65 20 2a 70 29 3b 0a 23 nter(Vdbe *p);.#
abe40 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 else.# define sq
abe50 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 lite3VdbeMutexAr
abe60 72 61 79 45 6e 74 65 72 28 70 29 0a 23 65 6e 64 rayEnter(p).#end
abe70 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 if..SQLITE_PRIVA
abe80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
abe90 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 4d beMemTranslate(M
abea0 65 6d 2a 2c 20 75 38 29 3b 0a 23 69 66 64 65 66 em*, u8);.#ifdef
abeb0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 SQLITE_DEBUG.SQ
abec0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
abed0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 oid sqlite3VdbeP
abee0 72 69 6e 74 53 71 6c 28 56 64 62 65 2a 29 3b 0a rintSql(Vdbe*);.
abef0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
abf00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
abf10 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 eMemPrettyPrint(
abf20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 72 20 Mem *pMem, char
abf30 2a 7a 42 75 66 29 3b 0a 23 65 6e 64 69 66 0a 53 *zBuf);.#endif.S
abf40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
abf50 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
abf60 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 HandleBom(Mem *p
abf70 4d 65 6d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 Mem);..#ifndef S
abf80 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
abf90 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 LOB.SQLITE_PRIVA
abfa0 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 TE int sqlite3
abfb0 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f VdbeMemExpandBlo
abfc0 62 28 4d 65 6d 20 2a 29 3b 0a 23 65 6c 73 65 0a b(Mem *);.#else.
abfd0 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 #define sqlite
abfe0 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 3VdbeMemExpandBl
abff0 6f 62 28 78 29 20 53 51 4c 49 54 45 5f 4f 4b 0a ob(x) SQLITE_OK.
ac000 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f #endif..#endif /
ac010 2a 20 21 64 65 66 69 6e 65 64 28 5f 56 44 42 45 * !defined(_VDBE
ac020 49 4e 54 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a INT_H_) */../***
ac030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
ac040 6f 66 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a 2a of vdbeInt.h ***
ac050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ac060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ac070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
ac080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
ac090 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
ac0a0 6c 65 66 74 20 6f 66 66 20 69 6e 20 75 74 66 2e left off in utf.
ac0b0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
ac0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 **********/..#if
ac0d0 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c ndef SQLITE_AMAL
ac0e0 47 41 4d 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20 54 GAMATION./*.** T
ac0f0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e he following con
ac100 73 74 61 6e 74 20 76 61 6c 75 65 20 69 73 20 75 stant value is u
ac110 73 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 sed by the SQLIT
ac120 45 5f 42 49 47 45 4e 44 49 41 4e 20 61 6e 64 0a E_BIGENDIAN and.
ac130 2a 2a 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 ** SQLITE_LITTLE
ac140 45 4e 44 49 41 4e 20 6d 61 63 72 6f 73 2e 0a 2a ENDIAN macros..*
ac150 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
ac160 20 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 const int sqlit
ac170 65 33 6f 6e 65 20 3d 20 31 3b 0a 23 65 6e 64 69 e3one = 1;.#endi
ac180 66 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4d 41 4c f /* SQLITE_AMAL
ac190 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a GAMATION */../*.
ac1a0 2a 2a 20 54 68 69 73 20 6c 6f 6f 6b 75 70 20 74 ** This lookup t
ac1b0 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 able is used to
ac1c0 68 65 6c 70 20 64 65 63 6f 64 65 20 74 68 65 20 help decode the
ac1d0 66 69 72 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a first byte of.**
ac1e0 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 55 54 a multi-byte UT
ac1f0 46 38 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2f F8 character..*/
ac200 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e .static const un
ac210 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 signed char sqli
ac220 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b 5d 20 te3Utf8Trans1[]
ac230 3d 20 7b 0a 20 20 30 78 30 30 2c 20 30 78 30 31 = {. 0x00, 0x01
ac240 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 , 0x02, 0x03, 0x
ac250 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 04, 0x05, 0x06,
ac260 30 78 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 78 0x07,. 0x08, 0x
ac270 30 39 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20 09, 0x0a, 0x0b,
ac280 30 78 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65 0x0c, 0x0d, 0x0e
ac290 2c 20 30 78 30 66 2c 0a 20 20 30 78 31 30 2c 20 , 0x0f,. 0x10,
ac2a0 30 78 31 31 2c 20 30 78 31 32 2c 20 30 78 31 33 0x11, 0x12, 0x13
ac2b0 2c 20 30 78 31 34 2c 20 30 78 31 35 2c 20 30 78 , 0x14, 0x15, 0x
ac2c0 31 36 2c 20 30 78 31 37 2c 0a 20 20 30 78 31 38 16, 0x17,. 0x18
ac2d0 2c 20 30 78 31 39 2c 20 30 78 31 61 2c 20 30 78 , 0x19, 0x1a, 0x
ac2e0 31 62 2c 20 30 78 31 63 2c 20 30 78 31 64 2c 20 1b, 0x1c, 0x1d,
ac2f0 30 78 31 65 2c 20 30 78 31 66 2c 0a 20 20 30 78 0x1e, 0x1f,. 0x
ac300 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 00, 0x01, 0x02,
ac310 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35 0x03, 0x04, 0x05
ac320 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 , 0x06, 0x07,.
ac330 30 78 30 38 2c 20 30 78 30 39 2c 20 30 78 30 61 0x08, 0x09, 0x0a
ac340 2c 20 30 78 30 62 2c 20 30 78 30 63 2c 20 30 78 , 0x0b, 0x0c, 0x
ac350 30 64 2c 20 30 78 30 65 2c 20 30 78 30 66 2c 0a 0d, 0x0e, 0x0f,.
ac360 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 0x00, 0x01, 0x
ac370 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20 02, 0x03, 0x04,
ac380 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37 0x05, 0x06, 0x07
ac390 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 ,. 0x00, 0x01,
ac3a0 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 30 0x02, 0x03, 0x00
ac3b0 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 , 0x01, 0x00, 0x
ac3c0 30 30 2c 0a 7d 3b 0a 0a 0a 23 64 65 66 69 6e 65 00,.};...#define
ac3d0 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 74 WRITE_UTF8(zOut
ac3e0 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 20 , c) {
ac3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac400 5c 0a 20 20 69 66 28 20 63 3c 30 78 30 30 30 38 \. if( c<0x0008
ac410 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 0 ){
ac420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac430 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
ac440 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 zOut++ = (u8)(c&
ac450 30 78 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 0xFF);
ac460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac470 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 \. }
ac480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac4a0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 65 \. e
ac4b0 6c 73 65 20 69 66 28 20 63 3c 30 78 30 30 38 30 lse if( c<0x0080
ac4c0 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 0 ){
ac4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac4e0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
ac4f0 2b 20 3d 20 30 78 43 30 20 2b 20 28 75 38 29 28 + = 0xC0 + (u8)(
ac500 28 63 3e 3e 36 29 26 30 78 31 46 29 3b 20 20 20 (c>>6)&0x1F);
ac510 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ac520 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 *zOut++ = 0x8
ac530 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 33 0 + (u8)(c & 0x3
ac540 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 F);
ac550 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 \. }
ac560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ac590 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c 30 78 . else if( c<0x
ac5a0 31 30 30 30 30 20 29 7b 20 20 20 20 20 20 20 20 10000 ){
ac5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac5c0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
ac5d0 4f 75 74 2b 2b 20 3d 20 30 78 45 30 20 2b 20 28 Out++ = 0xE0 + (
ac5e0 75 38 29 28 28 63 3e 3e 31 32 29 26 30 78 30 46 u8)((c>>12)&0x0F
ac5f0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
ac600 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
ac610 20 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e 0x80 + (u8)((c>
ac620 3e 36 29 20 26 20 30 78 33 46 29 3b 20 20 20 20 >6) & 0x3F);
ac630 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
ac640 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b *zOut++ = 0x80 +
ac650 20 28 75 38 29 28 63 20 26 20 30 78 33 46 29 3b (u8)(c & 0x3F);
ac660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac670 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 \. }else{
ac680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac6a0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ac6b0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 46 30 *zOut++ = 0xF0
ac6c0 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 38 29 20 + (u8)((c>>18)
ac6d0 26 20 30 78 30 37 29 3b 20 20 20 20 20 20 20 20 & 0x07);
ac6e0 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
ac6f0 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 ++ = 0x80 + (u8)
ac700 28 28 63 3e 3e 31 32 29 20 26 20 30 78 33 46 29 ((c>>12) & 0x3F)
ac710 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a ; \.
ac720 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 *zOut++ = 0x
ac730 38 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 29 80 + (u8)((c>>6)
ac740 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 & 0x3F);
ac750 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f \. *zO
ac760 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 ut++ = 0x80 + (u
ac770 38 29 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 8)(c & 0x3F);
ac780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac790 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 \. }
ac7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac7c0 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 \.}..#d
ac7d0 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 31 efine WRITE_UTF1
ac7e0 36 4c 45 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 6LE(zOut, c) {
ac7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac810 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 46 \. if( c<=0xF
ac820 46 46 46 20 29 7b 20 20 20 20 20 20 20 20 20 20 FFF ){
ac830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac850 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
ac860 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 Out++ = (u8)(c&0
ac870 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 x00FF);
ac880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ac8a0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 *zOut++ = (u
ac8b0 38 29 28 28 63 3e 3e 38 29 26 30 78 30 30 46 46 8)((c>>8)&0x00FF
ac8c0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
ac8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac8e0 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 \. }else{
ac8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac920 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
ac930 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 *zOut++ = (u8)((
ac940 28 63 3e 3e 31 30 29 26 30 78 30 30 33 46 29 20 (c>>10)&0x003F)
ac950 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e + (((c-0x10000)>
ac960 3e 31 30 29 26 30 78 30 30 43 30 29 29 3b 20 20 >10)&0x00C0));
ac970 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
ac980 28 75 38 29 28 30 78 30 30 44 38 20 2b 20 28 28 (u8)(0x00D8 + ((
ac990 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 29 (c-0x10000)>>18)
ac9a0 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 &0x03));
ac9b0 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
ac9c0 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 30 t++ = (u8)(c&0x0
ac9d0 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 0FF);
ac9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac9f0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
aca00 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 *zOut++ = (u8)
aca10 28 30 78 30 30 44 43 20 2b 20 28 28 63 3e 3e 38 (0x00DC + ((c>>8
aca20 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 )&0x03));
aca30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aca40 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 \. }
aca50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aca60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aca70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aca80 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 \.}..#de
aca90 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 31 36 fine WRITE_UTF16
acaa0 42 45 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 BE(zOut, c) {
acab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acad0 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 46 46 \. if( c<=0xFF
acae0 46 46 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 FF ){
acaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acb10 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f \. *zO
acb20 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 63 3e 3e ut++ = (u8)((c>>
acb30 38 29 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 8)&0x00FF);
acb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acb50 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
acb60 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 *zOut++ = (u8
acb70 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 )(c&0x00FF);
acb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acba0 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 \. }else{
acbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acbe0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
acbf0 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 zOut++ = (u8)(0x
acc00 30 30 44 38 20 2b 20 28 28 28 63 2d 30 78 31 30 00D8 + (((c-0x10
acc10 30 30 30 29 3e 3e 31 38 29 26 30 78 30 33 29 29 000)>>18)&0x03))
acc20 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c ; \
acc30 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
acc40 75 38 29 28 28 28 63 3e 3e 31 30 29 26 30 78 30 u8)(((c>>10)&0x0
acc50 30 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31 30 03F) + (((c-0x10
acc60 30 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43 30 000)>>10)&0x00C0
acc70 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 )); \. *zOut
acc80 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 43 ++ = (u8)(0x00DC
acc90 20 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 33 29 + ((c>>8)&0x03)
acca0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
accb0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
accc0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 *zOut++ = (u8)(
accd0 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 c&0x00FF);
acce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
accf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acd00 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 \. }
acd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acd40 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 \.}..#def
acd50 69 6e 65 20 52 45 41 44 5f 55 54 46 31 36 4c 45 ine READ_UTF16LE
acd60 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20 20 20 (zIn, c){
acd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acd80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acd90 20 20 5c 0a 20 20 63 20 3d 20 28 2a 7a 49 6e 2b \. c = (*zIn+
acda0 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 +);
acdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acdd0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 \. c
acde0 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 += ((*zIn++)<<8)
acdf0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ace00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ace10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ace20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 44 \. if( c>=0xD
ace30 38 30 30 20 26 26 20 63 3c 30 78 45 30 30 30 20 800 && c<0xE000
ace40 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
ace50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ace60 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
ace70 69 6e 74 20 63 32 20 3d 20 28 2a 7a 49 6e 2b 2b int c2 = (*zIn++
ace80 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
ace90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aceb0 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28 28 \. c2 += ((
acec0 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 *zIn++)<<8);
aced0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acef0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
acf00 63 20 3d 20 28 63 32 26 30 78 30 33 46 46 29 20 c = (c2&0x03FF)
acf10 2b 20 28 28 63 26 30 78 30 30 33 46 29 3c 3c 31 + ((c&0x003F)<<1
acf20 30 29 20 2b 20 28 28 28 63 26 30 78 30 33 43 30 0) + (((c&0x03C0
acf30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 3b 20 )+0x0040)<<10);
acf40 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 \. }
acf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acf80 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 \.}..#
acf90 64 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 31 define READ_UTF1
acfa0 36 42 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 6BE(zIn, c){
acfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acfc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acfd0 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 28 2a \. c = ((*
acfe0 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 zIn++)<<8);
acff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad010 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ad020 20 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 c += (*zIn++);
ad030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad060 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d \. if( c>=
ad070 30 78 44 38 30 30 20 26 26 20 63 3c 30 78 45 30 0xD800 && c<0xE0
ad080 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 00 ){
ad090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ad0b0 20 20 20 69 6e 74 20 63 32 20 3d 20 28 28 2a 7a int c2 = ((*z
ad0c0 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 In++)<<8);
ad0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad0f0 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d \. c2 +=
ad100 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 (*zIn++);
ad110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad130 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ad140 20 20 20 63 20 3d 20 28 63 32 26 30 78 30 33 46 c = (c2&0x03F
ad150 46 29 20 2b 20 28 28 63 26 30 78 30 30 33 46 29 F) + ((c&0x003F)
ad160 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30 78 30 <<10) + (((c&0x0
ad170 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30 3C0)+0x0040)<<10
ad180 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 ); \. }
ad190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d \.}
ad1d0 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 ../*.** Translat
ad1e0 65 20 61 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 e a single UTF-8
ad1f0 20 63 68 61 72 61 63 74 65 72 2e 20 20 52 65 74 character. Ret
ad200 75 72 6e 20 74 68 65 20 75 6e 69 63 6f 64 65 20 urn the unicode
ad210 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 value..**.** Dur
ad220 69 6e 67 20 74 72 61 6e 73 6c 61 74 69 6f 6e 2c ing translation,
ad230 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 assume that the
ad240 20 62 79 74 65 20 74 68 61 74 20 7a 54 65 72 6d byte that zTerm
ad250 20 70 6f 69 6e 74 73 0a 2a 2a 20 69 73 20 61 20 points.** is a
ad260 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 0x00..**.** Writ
ad270 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 e a pointer to t
ad280 68 65 20 6e 65 78 74 20 75 6e 72 65 61 64 20 62 he next unread b
ad290 79 74 65 20 62 61 63 6b 20 69 6e 74 6f 20 2a 70 yte back into *p
ad2a0 7a 4e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 zNext..**.** Not
ad2b0 65 73 20 4f 6e 20 49 6e 76 61 6c 69 64 20 55 54 es On Invalid UT
ad2c0 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 F-8:.**.** * T
ad2d0 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 his routine neve
ad2e0 72 20 61 6c 6c 6f 77 73 20 61 20 37 2d 62 69 74 r allows a 7-bit
ad2f0 20 63 68 61 72 61 63 74 65 72 20 28 30 78 30 30 character (0x00
ad300 20 74 68 72 6f 75 67 68 20 30 78 37 66 29 20 74 through 0x7f) t
ad310 6f 0a 2a 2a 20 20 20 20 20 62 65 20 65 6e 63 6f o.** be enco
ad320 64 65 64 20 61 73 20 61 20 6d 75 6c 74 69 2d 62 ded as a multi-b
ad330 79 74 65 20 63 68 61 72 61 63 74 65 72 2e 20 20 yte character.
ad340 41 6e 79 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 Any multi-byte c
ad350 68 61 72 61 63 74 65 72 20 74 68 61 74 0a 2a 2a haracter that.**
ad360 20 20 20 20 20 61 74 74 65 6d 70 74 73 20 74 6f attempts to
ad370 20 65 6e 63 6f 64 65 20 61 20 76 61 6c 75 65 20 encode a value
ad380 62 65 74 77 65 65 6e 20 30 78 30 30 20 61 6e 64 between 0x00 and
ad390 20 30 78 37 66 20 69 73 20 72 65 6e 64 65 72 65 0x7f is rendere
ad3a0 64 20 61 73 20 30 78 66 66 66 64 2e 0a 2a 2a 0a d as 0xfffd..**.
ad3b0 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74 ** * This rout
ad3c0 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73 ine never allows
ad3d0 20 61 20 55 54 46 31 36 20 73 75 72 72 6f 67 61 a UTF16 surroga
ad3e0 74 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 65 te value to be e
ad3f0 6e 63 6f 64 65 64 2e 0a 2a 2a 20 20 20 20 20 49 ncoded..** I
ad400 66 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 f a multi-byte c
ad410 68 61 72 61 63 74 65 72 20 61 74 74 65 6d 70 74 haracter attempt
ad420 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 61 s to encode a va
ad430 6c 75 65 20 62 65 74 77 65 65 6e 0a 2a 2a 20 20 lue between.**
ad440 20 20 20 30 78 64 38 30 30 20 61 6e 64 20 30 78 0xd800 and 0x
ad450 65 30 30 30 20 74 68 65 6e 20 69 74 20 69 73 20 e000 then it is
ad460 72 65 6e 64 65 72 65 64 20 61 73 20 30 78 66 66 rendered as 0xff
ad470 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 42 79 fd..**.** * By
ad480 74 65 73 20 69 6e 20 74 68 65 20 72 61 6e 67 65 tes in the range
ad490 20 6f 66 20 30 78 38 30 20 74 68 72 6f 75 67 68 of 0x80 through
ad4a0 20 30 78 62 66 20 77 68 69 63 68 20 6f 63 63 75 0xbf which occu
ad4b0 72 20 61 73 20 74 68 65 20 66 69 72 73 74 0a 2a r as the first.*
ad4c0 2a 20 20 20 20 20 62 79 74 65 20 6f 66 20 61 20 * byte of a
ad4d0 63 68 61 72 61 63 74 65 72 20 61 72 65 20 69 6e character are in
ad4e0 74 65 72 70 72 65 74 65 64 20 61 73 20 73 69 6e terpreted as sin
ad4f0 67 6c 65 2d 62 79 74 65 20 63 68 61 72 61 63 74 gle-byte charact
ad500 65 72 73 0a 2a 2a 20 20 20 20 20 61 6e 64 20 72 ers.** and r
ad510 65 6e 64 65 72 65 64 20 61 73 20 74 68 65 6d 73 endered as thems
ad520 65 6c 76 65 73 20 65 76 65 6e 20 74 68 6f 75 67 elves even thoug
ad530 68 20 74 68 65 79 20 61 72 65 20 74 65 63 68 6e h they are techn
ad540 69 63 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 69 6e ically.** in
ad550 76 61 6c 69 64 20 63 68 61 72 61 63 74 65 72 73 valid characters
ad560 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 ..**.** * This
ad570 20 72 6f 75 74 69 6e 65 20 61 63 63 65 70 74 73 routine accepts
ad580 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 6e 75 6d an infinite num
ad590 62 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e 74 ber of different
ad5a0 20 55 54 46 38 20 65 6e 63 6f 64 69 6e 67 73 0a UTF8 encodings.
ad5b0 2a 2a 20 20 20 20 20 66 6f 72 20 75 6e 69 63 6f ** for unico
ad5c0 64 65 20 76 61 6c 75 65 73 20 30 78 38 30 20 61 de values 0x80 a
ad5d0 6e 64 20 67 72 65 61 74 65 72 2e 20 20 49 74 20 nd greater. It
ad5e0 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 6f 76 do not change ov
ad5f0 65 72 2d 6c 65 6e 67 74 68 0a 2a 2a 20 20 20 20 er-length.**
ad600 20 65 6e 63 6f 64 69 6e 67 73 20 74 6f 20 30 78 encodings to 0x
ad610 66 66 66 64 20 61 73 20 73 6f 6d 65 20 73 79 73 fffd as some sys
ad620 74 65 6d 73 20 72 65 63 6f 6d 6d 65 6e 64 2e 0a tems recommend..
ad630 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f */.#define READ_
ad640 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c UTF8(zIn, zTerm,
ad650 20 63 29 20 20 20 20 20 20 20 20 20 20 20 20 20 c)
ad660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ad670 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 3b 20 c = *(zIn++);
ad680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad6a0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 \. i
ad6b0 66 28 20 63 3e 3d 30 78 63 30 20 29 7b 20 20 20 f( c>=0xc0 ){
ad6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad6e0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 20 \. c
ad6f0 3d 20 73 71 6c 69 74 65 33 55 74 66 38 54 72 61 = sqlite3Utf8Tra
ad700 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 20 20 20 20 ns1[c-0xc0];
ad710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad720 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69 6c 65 \. while
ad730 28 20 7a 49 6e 21 3d 7a 54 65 72 6d 20 26 26 20 ( zIn!=zTerm &&
ad740 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d 30 (*zIn & 0xc0)==0
ad750 78 38 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 x80 ){
ad760 20 20 5c 0a 20 20 20 20 20 20 63 20 3d 20 28 63 \. c = (c
ad770 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26 20 2a <<6) + (0x3f & *
ad780 28 7a 49 6e 2b 2b 29 29 3b 20 20 20 20 20 20 20 (zIn++));
ad790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ad7a0 0a 20 20 20 20 7d 20 20 20 20 20 20 20 20 20 20 . }
ad7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad7d0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ad7e0 20 20 69 66 28 20 63 3c 30 78 38 30 20 20 20 20 if( c<0x80
ad7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad810 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 \.
ad820 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46 || (c&0xFFFFF
ad830 38 30 30 29 3d 3d 30 78 44 38 30 30 20 20 20 20 800)==0xD800
ad840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad850 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20 20 20 \.
ad860 7c 7c 20 28 63 26 30 78 46 46 46 46 46 46 46 45 || (c&0xFFFFFFFE
ad870 29 3d 3d 30 78 46 46 46 45 20 29 7b 20 20 63 20 )==0xFFFE ){ c
ad880 3d 20 30 78 46 46 46 44 3b 20 7d 20 20 20 20 20 = 0xFFFD; }
ad890 20 20 20 5c 0a 20 20 7d 0a 53 51 4c 49 54 45 5f \. }.SQLITE_
ad8a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
ad8b0 74 65 33 55 74 66 38 52 65 61 64 28 0a 20 20 63 te3Utf8Read(. c
ad8c0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
ad8d0 61 72 20 2a 7a 49 6e 2c 20 20 20 20 20 20 20 2f ar *zIn, /
ad8e0 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 * First byte of
ad8f0 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 20 UTF-8 character
ad900 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 */. const unsig
ad910 6e 65 64 20 63 68 61 72 20 2a 2a 70 7a 4e 65 78 ned char **pzNex
ad920 74 20 20 20 20 2f 2a 20 57 72 69 74 65 20 66 69 t /* Write fi
ad930 72 73 74 20 62 79 74 65 20 70 61 73 74 20 55 54 rst byte past UT
ad940 46 2d 38 20 63 68 61 72 20 68 65 72 65 20 2a 2f F-8 char here */
ad950 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 0a 20 20 .){. int c;..
ad960 2f 2a 20 53 61 6d 65 20 61 73 20 52 45 41 44 5f /* Same as READ_
ad970 55 54 46 38 28 29 20 61 62 6f 76 65 20 62 75 74 UTF8() above but
ad980 20 77 69 74 68 6f 75 74 20 74 68 65 20 7a 54 65 without the zTe
ad990 72 6d 20 70 61 72 61 6d 65 74 65 72 2e 0a 20 20 rm parameter..
ad9a0 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 6f 75 74 ** For this rout
ad9b0 69 6e 65 2c 20 77 65 20 61 73 73 75 6d 65 20 74 ine, we assume t
ad9c0 68 65 20 55 54 46 38 20 73 74 72 69 6e 67 20 69 he UTF8 string i
ad9d0 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 65 s always zero-te
ad9e0 72 6d 69 6e 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 rminated.. */.
ad9f0 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 3b 0a 20 c = *(zIn++);.
ada00 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29 7b 0a if( c>=0xc0 ){.
ada10 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 c = sqlite3U
ada20 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 63 30 tf8Trans1[c-0xc0
ada30 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a ];. while( (*
ada40 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d 30 78 38 zIn & 0xc0)==0x8
ada50 30 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 28 0 ){. c = (
ada60 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26 20 c<<6) + (0x3f &
ada70 2a 28 7a 49 6e 2b 2b 29 29 3b 0a 20 20 20 20 7d *(zIn++));. }
ada80 0a 20 20 20 20 69 66 28 20 63 3c 30 78 38 30 0a . if( c<0x80.
ada90 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 || (c&0x
adaa0 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38 30 FFFFF800)==0xD80
adab0 30 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 0. || (c&
adac0 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 0xFFFFFFFE)==0xF
adad0 46 46 45 20 29 7b 20 20 63 20 3d 20 30 78 46 46 FFE ){ c = 0xFF
adae0 46 44 3b 20 7d 0a 20 20 7d 0a 20 20 2a 70 7a 4e FD; }. }. *pzN
adaf0 65 78 74 20 3d 20 7a 49 6e 3b 0a 20 20 72 65 74 ext = zIn;. ret
adb00 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a 0a urn c;.}...../*.
adb10 2a 2a 20 49 66 20 74 68 65 20 54 52 41 4e 53 4c ** If the TRANSL
adb20 41 54 45 5f 54 52 41 43 45 20 6d 61 63 72 6f 20 ATE_TRACE macro
adb30 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 20 is defined, the
adb40 76 61 6c 75 65 20 6f 66 20 65 61 63 68 20 4d 65 value of each Me
adb50 6d 20 69 73 0a 2a 2a 20 70 72 69 6e 74 65 64 20 m is.** printed
adb60 6f 6e 20 73 74 64 65 72 72 20 6f 6e 20 74 68 65 on stderr on the
adb70 20 77 61 79 20 69 6e 74 6f 20 61 6e 64 20 6f 75 way into and ou
adb80 74 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65 t of sqlite3Vdbe
adb90 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 2e 0a MemTranslate()..
adba0 2a 2f 20 0a 2f 2a 20 23 64 65 66 69 6e 65 20 54 */ ./* #define T
adbb0 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 31 RANSLATE_TRACE 1
adbc0 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
adbd0 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f ITE_OMIT_UTF16./
adbe0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
adbf0 65 20 74 72 61 6e 73 66 6f 72 6d 73 20 74 68 65 e transforms the
adc00 20 69 6e 74 65 72 6e 61 6c 20 74 65 78 74 20 65 internal text e
adc10 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20 ncoding used by
adc20 70 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 73 69 72 pMem to.** desir
adc30 65 64 45 6e 63 2e 20 49 74 20 69 73 20 61 6e 20 edEnc. It is an
adc40 65 72 72 6f 72 20 69 66 20 74 68 65 20 73 74 72 error if the str
adc50 69 6e 67 20 69 73 20 61 6c 72 65 61 64 79 20 6f ing is already o
adc60 66 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a f the desired.**
adc70 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20 69 66 encoding, or if
adc80 20 2a 70 4d 65 6d 20 64 6f 65 73 20 6e 6f 74 20 *pMem does not
adc90 63 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 6e 67 contain a string
adca0 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 value..*/.SQLIT
adcb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
adcc0 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e lite3VdbeMemTran
adcd0 73 6c 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c slate(Mem *pMem,
adce0 20 75 38 20 64 65 73 69 72 65 64 45 6e 63 29 7b u8 desiredEnc){
adcf0 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 . int len;
add00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
add10 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 * Maximum length
add20 20 6f 66 20 6f 75 74 70 75 74 20 73 74 72 69 6e of output strin
add30 67 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 g in bytes */.
add40 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
add50 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 Out;
add60 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 /* Output
add70 62 75 66 66 65 72 20 2a 2f 0a 20 20 75 6e 73 69 buffer */. unsi
add80 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b 20 gned char *zIn;
add90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adda0 20 20 2f 2a 20 49 6e 70 75 74 20 69 74 65 72 61 /* Input itera
addb0 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 tor */. unsigne
addc0 64 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 20 d char *zTerm;
addd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
adde0 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75 74 20 2a * End of input *
addf0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
ade00 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 r *z;
ade10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 /* Out
ade20 70 75 74 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a put iterator */.
ade30 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 unsigned int c
ade40 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 ;.. assert( pMe
ade50 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 m->db==0 || sqli
ade60 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
ade70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 Mem->db->mutex)
ade80 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 );. assert( pMe
ade90 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 m->flags&MEM_Str
adea0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d );. assert( pM
adeb0 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 em->enc!=desired
adec0 45 6e 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28 Enc );. assert(
aded0 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 20 29 3b pMem->enc!=0 );
adee0 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d . assert( pMem-
adef0 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 20 64 65 >n>=0 );..#if de
adf00 66 69 6e 65 64 28 54 52 41 4e 53 4c 41 54 45 5f fined(TRANSLATE_
adf10 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e 65 TRACE) && define
adf20 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a d(SQLITE_DEBUG).
adf30 20 20 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 {. char zBu
adf40 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 f[100];. sqli
adf50 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 te3VdbeMemPretty
adf60 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 75 66 Print(pMem, zBuf
adf70 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 );. fprintf(s
adf80 74 64 65 72 72 2c 20 22 49 4e 50 55 54 3a 20 20 tderr, "INPUT:
adf90 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 20 %s\n", zBuf);.
adfa0 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 }.#endif.. /* I
adfb0 66 20 74 68 65 20 74 72 61 6e 73 6c 61 74 69 6f f the translatio
adfc0 6e 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 n is between UTF
adfd0 2d 31 36 20 6c 69 74 74 6c 65 20 61 6e 64 20 62 -16 little and b
adfe0 69 67 20 65 6e 64 69 61 6e 2c 20 74 68 65 6e 20 ig endian, then
adff0 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69 . ** all that i
ae000 73 20 72 65 71 75 69 72 65 64 20 69 73 20 74 6f s required is to
ae010 20 73 77 61 70 20 74 68 65 20 62 79 74 65 20 6f swap the byte o
ae020 72 64 65 72 2e 20 54 68 69 73 20 63 61 73 65 20 rder. This case
ae030 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 2a 2a 20 is handled. **
ae040 64 69 66 66 65 72 65 6e 74 6c 79 20 66 72 6f 6d differently from
ae050 20 74 68 65 20 6f 74 68 65 72 73 2e 0a 20 20 2a the others.. *
ae060 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e /. if( pMem->en
ae070 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 c!=SQLITE_UTF8 &
ae080 26 20 64 65 73 69 72 65 64 45 6e 63 21 3d 53 51 & desiredEnc!=SQ
ae090 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 LITE_UTF8 ){.
ae0a0 20 75 38 20 74 65 6d 70 3b 0a 20 20 20 20 69 6e u8 temp;. in
ae0b0 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 t rc;. rc = s
ae0c0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b qlite3VdbeMemMak
ae0d0 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d 29 eWriteable(pMem)
ae0e0 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
ae0f0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
ae100 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
ae110 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 ITE_NOMEM );.
ae120 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
ae130 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 _NOMEM;. }.
ae140 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65 zIn = (u8*)pMe
ae150 6d 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 72 6d 20 m->z;. zTerm
ae160 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 26 7e = &zIn[pMem->n&~
ae170 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 1];. while( z
ae180 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 In<zTerm ){.
ae190 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b 0a 20 temp = *zIn;.
ae1a0 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28 7a 49 *zIn = *(zI
ae1b0 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49 6e 2b n+1);. zIn+
ae1c0 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b 2b 20 +;. *zIn++
ae1d0 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 = temp;. }.
ae1e0 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 pMem->enc = de
ae1f0 73 69 72 65 64 45 6e 63 3b 0a 20 20 20 20 67 6f siredEnc;. go
ae200 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74 to translate_out
ae210 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 ;. }.. /* Set
ae220 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61 78 69 6d len to the maxim
ae230 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 um number of byt
ae240 65 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 74 es required in t
ae250 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 he output buffer
ae260 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 69 72 . */. if( desir
ae270 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 edEnc==SQLITE_UT
ae280 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65 F8 ){. /* Whe
ae290 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f n converting fro
ae2a0 6d 20 55 54 46 2d 31 36 2c 20 74 68 65 20 6d 61 m UTF-16, the ma
ae2b0 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 72 65 73 ximum growth res
ae2c0 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a ults from. **
ae2d0 20 74 72 61 6e 73 6c 61 74 69 6e 67 20 61 20 32 translating a 2
ae2e0 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 -byte character
ae2f0 74 6f 20 61 20 34 2d 62 79 74 65 20 55 54 46 2d to a 4-byte UTF-
ae300 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20 8 character..
ae310 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62 79 74 ** A single byt
ae320 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f e is required fo
ae330 72 20 74 68 65 20 6f 75 74 70 75 74 20 73 74 72 r the output str
ae340 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 ing. ** nul-t
ae350 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a erminator.. *
ae360 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 26 3d /. pMem->n &=
ae370 20 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70 ~1;. len = p
ae380 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 31 3b 0a Mem->n * 2 + 1;.
ae390 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
ae3a0 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 When converting
ae3b0 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 from UTF-8 to UT
ae3c0 46 2d 31 36 20 74 68 65 20 6d 61 78 69 6d 75 6d F-16 the maximum
ae3d0 20 67 72 6f 77 74 68 20 69 73 20 63 61 75 73 65 growth is cause
ae3e0 64 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 61 20 d. ** when a
ae3f0 31 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68 61 1-byte UTF-8 cha
ae400 72 61 63 74 65 72 20 69 73 20 74 72 61 6e 73 6c racter is transl
ae410 61 74 65 64 20 69 6e 74 6f 20 61 20 32 2d 62 79 ated into a 2-by
ae420 74 65 20 55 54 46 2d 31 36 0a 20 20 20 20 2a 2a te UTF-16. **
ae430 20 63 68 61 72 61 63 74 65 72 2e 20 54 77 6f 20 character. Two
ae440 62 79 74 65 73 20 61 72 65 20 72 65 71 75 69 72 bytes are requir
ae450 65 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 ed in the output
ae460 20 62 75 66 66 65 72 20 66 6f 72 20 74 68 65 0a buffer for the.
ae470 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 ** nul-termi
ae480 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 nator.. */.
ae490 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 len = pMem->n
ae4a0 2a 20 32 20 2b 20 32 3b 0a 20 20 7d 0a 0a 20 20 * 2 + 2;. }..
ae4b0 2f 2a 20 53 65 74 20 7a 49 6e 20 74 6f 20 70 6f /* Set zIn to po
ae4c0 69 6e 74 20 61 74 20 74 68 65 20 73 74 61 72 74 int at the start
ae4d0 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 62 75 of the input bu
ae4e0 66 66 65 72 20 61 6e 64 20 7a 54 65 72 6d 20 74 ffer and zTerm t
ae4f0 6f 20 70 6f 69 6e 74 20 31 0a 20 20 2a 2a 20 62 o point 1. ** b
ae500 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 yte past the end
ae510 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 69 .. **. ** Vari
ae520 61 62 6c 65 20 7a 4f 75 74 20 69 73 20 73 65 74 able zOut is set
ae530 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 to point at the
ae540 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c 20 output buffer,
ae550 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 0a 20 space obtained.
ae560 20 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ** from sqlite3
ae570 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 2a 2f 0a _malloc().. */.
ae580 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65 zIn = (u8*)pMe
ae590 6d 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20 3d 20 m->z;. zTerm =
ae5a0 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20 &zIn[pMem->n];.
ae5b0 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 44 zOut = sqlite3D
ae5c0 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d bMallocRaw(pMem-
ae5d0 3e 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 69 66 28 >db, len);. if(
ae5e0 20 21 7a 4f 75 74 20 29 7b 0a 20 20 20 20 72 65 !zOut ){. re
ae5f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
ae600 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 7a 4f 75 M;. }. z = zOu
ae610 74 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e t;.. if( pMem->
ae620 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 enc==SQLITE_UTF8
ae630 20 29 7b 0a 20 20 20 20 69 66 28 20 64 65 73 69 ){. if( desi
ae640 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 redEnc==SQLITE_U
ae650 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 20 TF16LE ){.
ae660 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 2d /* UTF-8 -> UTF-
ae670 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 16 Little-endian
ae680 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 */. while(
ae690 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 zIn<zTerm ){.
ae6a0 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 6c /* c = sql
ae6b0 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e ite3Utf8Read(zIn
ae6c0 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20 , zTerm, (const
ae6d0 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20 u8**)&zIn); */.
ae6e0 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 38 READ_UTF8
ae6f0 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b (zIn, zTerm, c);
ae700 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 . WRITE_U
ae710 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 TF16LE(z, c);.
ae720 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
ae730 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 . assert( d
ae740 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 esiredEnc==SQLIT
ae750 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 20 E_UTF16BE );.
ae760 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 /* UTF-8 -> U
ae770 54 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 6e TF-16 Big-endian
ae780 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 */. while(
ae790 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 zIn<zTerm ){.
ae7a0 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 6c /* c = sql
ae7b0 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e ite3Utf8Read(zIn
ae7c0 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20 , zTerm, (const
ae7d0 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20 u8**)&zIn); */.
ae7e0 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 38 READ_UTF8
ae7f0 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b (zIn, zTerm, c);
ae800 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 . WRITE_U
ae810 54 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 TF16BE(z, c);.
ae820 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
ae830 70 4d 65 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 pMem->n = (int)(
ae840 7a 20 2d 20 7a 4f 75 74 29 3b 0a 20 20 20 20 2a z - zOut);. *
ae850 7a 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 z++ = 0;. }else
ae860 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 65 {. assert( de
ae870 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 siredEnc==SQLITE
ae880 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 69 66 28 _UTF8 );. if(
ae890 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 pMem->enc==SQLI
ae8a0 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 TE_UTF16LE ){.
ae8b0 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 4c 69 /* UTF-16 Li
ae8c0 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 ttle-endian -> U
ae8d0 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 TF-8 */. wh
ae8e0 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 ile( zIn<zTerm )
ae8f0 7b 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 {. READ_U
ae900 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29 3b 20 TF16LE(zIn, c);
ae910 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 . WRITE_U
ae920 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 TF8(z, c);.
ae930 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
ae940 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42 69 /* UTF-16 Bi
ae950 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d g-endian -> UTF-
ae960 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 8 */. while
ae970 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 ( zIn<zTerm ){.
ae980 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 READ_UTF1
ae990 36 42 45 28 7a 49 6e 2c 20 63 29 3b 20 0a 20 20 6BE(zIn, c); .
ae9a0 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 38 WRITE_UTF8
ae9b0 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a (z, c);. }.
ae9c0 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e }. pMem->
ae9d0 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20 7a 4f n = (int)(z - zO
ae9e0 75 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a 20 3d 20 ut);. }. *z =
ae9f0 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 0;. assert( (pM
aea00 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 64 45 6e em->n+(desiredEn
aea10 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 c==SQLITE_UTF8?1
aea20 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 0a 20 20 :2))<=len );..
aea30 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
aea40 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 lease(pMem);. p
aea50 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 Mem->flags &= ~(
aea60 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 MEM_Static|MEM_D
aea70 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 yn|MEM_Ephem);.
aea80 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 73 pMem->enc = des
aea90 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d 65 6d 2d iredEnc;. pMem-
aeaa0 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45 4d 5f 54 >flags |= (MEM_T
aeab0 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 erm|MEM_Dyn);.
aeac0 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a pMem->z = (char*
aead0 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d 2d 3e 7a )zOut;. pMem->z
aeae0 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a Malloc = pMem->z
aeaf0 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74 ;..translate_out
aeb00 3a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 52 :.#if defined(TR
aeb10 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 20 26 ANSLATE_TRACE) &
aeb20 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 & defined(SQLITE
aeb30 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 20 20 _DEBUG). {.
aeb40 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a char zBuf[100];.
aeb50 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
aeb60 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70 4d emPrettyPrint(pM
aeb70 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 em, zBuf);. f
aeb80 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 printf(stderr, "
aeb90 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22 2c 20 7a OUTPUT: %s\n", z
aeba0 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 Buf);. }.#endif
aebb0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
aebc0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 _OK;.}../*.** Th
aebd0 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b is routine check
aebe0 73 20 66 6f 72 20 61 20 62 79 74 65 2d 6f 72 64 s for a byte-ord
aebf0 65 72 20 6d 61 72 6b 20 61 74 20 74 68 65 20 62 er mark at the b
aec00 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 eginning of the
aec10 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e .** UTF-16 strin
aec20 67 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 g stored in *pMe
aec30 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20 70 72 65 m. If one is pre
aec40 73 65 6e 74 2c 20 69 74 20 69 73 20 72 65 6d 6f sent, it is remo
aec50 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 20 65 ved and.** the e
aec60 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 4d ncoding of the M
aec70 65 6d 20 61 64 6a 75 73 74 65 64 2e 20 54 68 69 em adjusted. Thi
aec80 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e s routine does n
aec90 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 62 79 74 ot do any.** byt
aeca0 65 2d 73 77 61 70 70 69 6e 67 2c 20 69 74 20 6a e-swapping, it j
aecb0 75 73 74 20 73 65 74 73 20 4d 65 6d 2e 65 6e 63 ust sets Mem.enc
aecc0 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a appropriately..
aecd0 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61 **.** The alloca
aece0 74 69 6f 6e 20 28 73 74 61 74 69 63 2c 20 64 79 tion (static, dy
aecf0 6e 61 6d 69 63 20 65 74 63 2e 29 20 61 6e 64 20 namic etc.) and
aed00 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 encoding of the
aed10 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a 20 63 68 Mem may be.** ch
aed20 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 66 75 anged by this fu
aed30 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 nction..*/.SQLIT
aed40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
aed50 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e 64 lite3VdbeMemHand
aed60 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 leBom(Mem *pMem)
aed70 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
aed80 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 62 6f 6d ITE_OK;. u8 bom
aed90 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 = 0;.. assert(
aeda0 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 20 pMem->n>=0 );.
aedb0 20 69 66 28 20 70 4d 65 6d 2d 3e 6e 3e 31 20 29 if( pMem->n>1 )
aedc0 7b 0a 20 20 20 20 75 38 20 62 31 20 3d 20 2a 28 {. u8 b1 = *(
aedd0 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 u8 *)pMem->z;.
aede0 20 20 75 38 20 62 32 20 3d 20 2a 28 28 28 75 38 u8 b2 = *(((u8
aedf0 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20 2b 20 31 29 *)pMem->z) + 1)
aee00 3b 0a 20 20 20 20 69 66 28 20 62 31 3d 3d 30 78 ;. if( b1==0x
aee10 46 45 20 26 26 20 62 32 3d 3d 30 78 46 46 20 29 FE && b2==0xFF )
aee20 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20 53 51 {. bom = SQ
aee30 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20 20 LITE_UTF16BE;.
aee40 20 20 7d 0a 20 20 20 20 69 66 28 20 62 31 3d 3d }. if( b1==
aee50 30 78 46 46 20 26 26 20 62 32 3d 3d 30 78 46 45 0xFF && b2==0xFE
aee60 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20 ){. bom =
aee70 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3b 0a SQLITE_UTF16LE;.
aee80 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 }. }. . i
aee90 66 28 20 62 6f 6d 20 29 7b 0a 20 20 20 20 72 63 f( bom ){. rc
aeea0 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 = sqlite3VdbeMe
aeeb0 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 mMakeWriteable(p
aeec0 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 Mem);. if( rc
aeed0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
aeee0 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 2d 3d 20 pMem->n -=
aeef0 32 3b 0a 20 20 20 20 20 20 6d 65 6d 6d 6f 76 65 2;. memmove
aef00 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65 6d 2d (pMem->z, &pMem-
aef10 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e 29 3b >z[2], pMem->n);
aef20 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 . pMem->z[p
aef30 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c 30 27 3b 0a Mem->n] = '\0';.
aef40 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d pMem->z[pM
aef50 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 5c 30 27 3b em->n+1] = '\0';
aef60 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 . pMem->fla
aef70 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a gs |= MEM_Term;.
aef80 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 pMem->enc
aef90 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d = bom;. }. }
aefa0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
aefb0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
aefc0 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a _OMIT_UTF16 */..
aefd0 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 20 55 54 /*.** pZ is a UT
aefe0 46 2d 38 20 65 6e 63 6f 64 65 64 20 75 6e 69 63 F-8 encoded unic
aeff0 6f 64 65 20 73 74 72 69 6e 67 2e 20 49 66 20 6e ode string. If n
af000 42 79 74 65 20 69 73 20 6c 65 73 73 20 74 68 61 Byte is less tha
af010 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 75 72 n zero,.** retur
af020 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
af030 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 unicode characte
af040 72 73 20 69 6e 20 70 5a 20 75 70 20 74 6f 20 28 rs in pZ up to (
af050 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e but not includin
af060 67 29 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 g).** the first
af070 30 78 30 30 20 62 79 74 65 2e 20 49 66 20 6e 42 0x00 byte. If nB
af080 79 74 65 20 69 73 20 6e 6f 74 20 6c 65 73 73 20 yte is not less
af090 74 68 61 6e 20 7a 65 72 6f 2c 20 72 65 74 75 72 than zero, retur
af0a0 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 n the.** number
af0b0 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 of unicode chara
af0c0 63 74 65 72 73 20 69 6e 20 74 68 65 20 66 69 72 cters in the fir
af0d0 73 74 20 6e 42 79 74 65 20 6f 66 20 70 5a 20 28 st nByte of pZ (
af0e0 6f 72 20 75 70 20 74 6f 20 0a 2a 2a 20 74 68 65 or up to .** the
af0f0 20 66 69 72 73 74 20 30 78 30 30 2c 20 77 68 69 first 0x00, whi
af100 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 chever comes fir
af110 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 st)..*/.SQLITE_P
af120 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
af130 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63 6f e3Utf8CharLen(co
af140 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69 nst char *zIn, i
af150 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 nt nByte){. int
af160 20 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 r = 0;. const
af170 75 38 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 75 u8 *z = (const u
af180 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e 73 74 20 8*)zIn;. const
af190 75 38 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 66 28 u8 *zTerm;. if(
af1a0 20 6e 42 79 74 65 3e 3d 30 20 29 7b 0a 20 20 20 nByte>=0 ){.
af1b0 20 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e 42 79 74 zTerm = &z[nByt
af1c0 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 e];. }else{.
af1d0 20 7a 54 65 72 6d 20 3d 20 28 63 6f 6e 73 74 20 zTerm = (const
af1e0 75 38 2a 29 28 2d 31 29 3b 0a 20 20 7d 0a 20 20 u8*)(-1);. }.
af1f0 61 73 73 65 72 74 28 20 7a 3c 3d 7a 54 65 72 6d assert( z<=zTerm
af200 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 21 );. while( *z!
af210 3d 30 20 26 26 20 7a 3c 7a 54 65 72 6d 20 29 7b =0 && z<zTerm ){
af220 0a 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 . SQLITE_SKIP
af230 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 72 2b _UTF8(z);. r+
af240 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 +;. }. return
af250 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 74 65 r;.}../* This te
af260 73 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e st function is n
af270 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65 ot currently use
af280 64 20 62 79 20 74 68 65 20 61 75 74 6f 6d 61 74 d by the automat
af290 65 64 20 74 65 73 74 2d 73 75 69 74 65 2e 20 0a ed test-suite. .
af2a0 2a 2a 20 48 65 6e 63 65 20 69 74 20 69 73 20 6f ** Hence it is o
af2b0 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 6e nly available in
af2c0 20 64 65 62 75 67 20 62 75 69 6c 64 73 2e 0a 2a debug builds..*
af2d0 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 /.#if defined(SQ
af2e0 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 64 65 LITE_TEST) && de
af2f0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
af300 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c UG)./*.** Transl
af310 61 74 65 20 55 54 46 2d 38 20 74 6f 20 55 54 46 ate UTF-8 to UTF
af320 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 -8..**.** This h
af330 61 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 as the effect of
af340 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 61 making sure tha
af350 74 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 t the string is
af360 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 55 well-formed.** U
af370 54 46 2d 38 2e 20 20 4d 69 73 63 6f 64 65 64 20 TF-8. Miscoded
af380 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20 72 characters are r
af390 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 emoved..**.** Th
af3a0 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73 e translation is
af3b0 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 20 28 done in-place (
af3c0 73 69 6e 63 65 20 69 74 20 69 73 20 69 6d 70 6f since it is impo
af3d0 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 0a 2a ssible for the.*
af3e0 2a 20 63 6f 72 72 65 63 74 20 55 54 46 2d 38 20 * correct UTF-8
af3f0 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65 20 6c encoding to be l
af400 6f 6e 67 65 72 20 74 68 61 6e 20 61 20 6d 61 6c onger than a mal
af410 66 6f 72 6d 65 64 20 65 6e 63 6f 64 69 6e 67 29 formed encoding)
af420 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
af430 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 ATE int sqlite3U
af440 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 64 20 tf8To8(unsigned
af450 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 75 6e char *zIn){. un
af460 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f 75 signed char *zOu
af470 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73 69 67 t = zIn;. unsig
af480 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 61 72 74 ned char *zStart
af490 20 3d 20 7a 49 6e 3b 0a 20 20 75 33 32 20 63 3b = zIn;. u32 c;
af4a0 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e 5b 30 .. while( zIn[0
af4b0 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 6c ] ){. c = sql
af4c0 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e ite3Utf8Read(zIn
af4d0 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a , (const u8**)&z
af4e0 49 6e 29 3b 0a 20 20 20 20 69 66 28 20 63 21 3d In);. if( c!=
af4f0 30 78 66 66 66 64 20 29 7b 0a 20 20 20 20 20 20 0xfffd ){.
af500 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 74 2c WRITE_UTF8(zOut,
af510 20 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 c);. }. }.
af520 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20 20 72 65 *zOut = 0;. re
af530 74 75 72 6e 20 28 69 6e 74 29 28 7a 4f 75 74 20 turn (int)(zOut
af540 2d 20 7a 53 74 61 72 74 29 3b 0a 7d 0a 23 65 6e - zStart);.}.#en
af550 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
af560 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f ITE_OMIT_UTF16./
af570 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 *.** Convert a U
af580 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69 6e 20 TF-16 string in
af590 74 68 65 20 6e 61 74 69 76 65 20 65 6e 63 6f 64 the native encod
af5a0 69 6e 67 20 69 6e 74 6f 20 61 20 55 54 46 2d 38 ing into a UTF-8
af5b0 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d 65 6d 6f string..** Memo
af5c0 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 55 ry to hold the U
af5d0 54 46 2d 38 20 73 74 72 69 6e 67 20 69 73 20 6f TF-8 string is o
af5e0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c btained from sql
af5f0 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 6e 64 20 ite3_malloc and
af600 6d 75 73 74 0a 2a 2a 20 62 65 20 66 72 65 65 64 must.** be freed
af610 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 by the calling
af620 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
af630 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 NULL is returned
af640 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 if there is an
af650 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 allocation error
af660 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
af670 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
af680 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69 74 65 3Utf16to8(sqlite
af690 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 76 6f 69 3 *db, const voi
af6a0 64 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 74 65 29 d *z, int nByte)
af6b0 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d {. Mem m;. mem
af6c0 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f set(&m, 0, sizeo
af6d0 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 f(m));. m.db =
af6e0 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 db;. sqlite3Vdb
af6f0 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c 20 7a eMemSetStr(&m, z
af700 2c 20 6e 42 79 74 65 2c 20 53 51 4c 49 54 45 5f , nByte, SQLITE_
af710 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51 4c UTF16NATIVE, SQL
af720 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 73 ITE_STATIC);. s
af730 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
af740 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 53 51 4c Encoding(&m, SQL
af750 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 66 28 ITE_UTF8);. if(
af760 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
af770 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 d ){. sqlite3
af780 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 VdbeMemRelease(&
af790 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d 20 30 3b m);. m.z = 0;
af7a0 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 . }. assert( (
af7b0 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 m.flags & MEM_Te
af7c0 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 rm)!=0 || db->ma
af7d0 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
af7e0 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 67 73 assert( (m.flags
af7f0 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30 20 7c & MEM_Str)!=0 |
af800 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
af810 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 ed );. return (
af820 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 m.flags & MEM_Dy
af830 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20 73 71 n)!=0 ? m.z : sq
af840 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 lite3DbStrDup(db
af850 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a , m.z);.}../*.**
af860 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 38 Convert a UTF-8
af870 20 73 74 72 69 6e 67 20 74 6f 20 74 68 65 20 55 string to the U
af880 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20 73 TF-16 encoding s
af890 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 pecified by para
af8a0 6d 65 74 65 72 0a 2a 2a 20 65 6e 63 2e 20 41 20 meter.** enc. A
af8b0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e pointer to the n
af8c0 65 77 20 73 74 72 69 6e 67 20 69 73 20 72 65 74 ew string is ret
af8d0 75 72 6e 65 64 2c 20 61 6e 64 20 74 68 65 20 76 urned, and the v
af8e0 61 6c 75 65 20 6f 66 20 2a 70 6e 4f 75 74 0a 2a alue of *pnOut.*
af8f0 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 * is set to the
af900 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 72 65 length of the re
af910 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 6e turned string in
af920 20 62 79 74 65 73 2e 20 54 68 65 20 63 61 6c 6c bytes. The call
af930 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 72 72 61 6e should.** arran
af940 67 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 ge to call sqlit
af950 65 33 44 62 46 72 65 65 28 29 20 6f 6e 20 74 68 e3DbFree() on th
af960 65 20 72 65 74 75 72 6e 65 64 20 70 6f 69 6e 74 e returned point
af970 65 72 20 77 68 65 6e 20 69 74 20 69 73 0a 2a 2a er when it is.**
af980 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 71 75 69 no longer requi
af990 72 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 61 red..** .** If a
af9a0 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 malloc failure
af9b0 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20 occurs, NULL is
af9c0 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 returned and the
af9d0 20 64 62 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db.mallocFailed
af9e0 0a 2a 2a 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2f .** flag set..*/
af9f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
afa00 4e 41 42 4c 45 5f 53 54 41 54 32 0a 53 51 4c 49 NABLE_STAT2.SQLI
afa10 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
afa20 2a 73 71 6c 69 74 65 33 55 74 66 38 74 6f 31 36 *sqlite3Utf8to16
afa30 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 75 38 (sqlite3 *db, u8
afa40 20 65 6e 63 2c 20 63 68 61 72 20 2a 7a 2c 20 69 enc, char *z, i
afa50 6e 74 20 6e 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 nt n, int *pnOut
afa60 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65 ){. Mem m;. me
afa70 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 mset(&m, 0, size
afa80 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 3d of(m));. m.db =
afa90 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 db;. sqlite3Vd
afaa0 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c 20 beMemSetStr(&m,
afab0 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 z, n, SQLITE_UTF
afac0 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 8, SQLITE_STATIC
afad0 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 );. if( sqlite3
afae0 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 VdbeMemTranslate
afaf0 28 26 6d 2c 20 65 6e 63 29 20 29 7b 0a 20 20 20 (&m, enc) ){.
afb00 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c assert( db->mal
afb10 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 locFailed );.
afb20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
afb30 20 61 73 73 65 72 74 28 20 6d 2e 7a 3d 3d 6d 2e assert( m.z==m.
afb40 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 2a 70 6e zMalloc );. *pn
afb50 4f 75 74 20 3d 20 6d 2e 6e 3b 0a 20 20 72 65 74 Out = m.n;. ret
afb60 75 72 6e 20 6d 2e 7a 3b 0a 7d 0a 23 65 6e 64 69 urn m.z;.}.#endi
afb70 66 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 f../*.** pZ is a
afb80 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 UTF-16 encoded
afb90 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 20 61 unicode string a
afba0 74 20 6c 65 61 73 74 20 6e 43 68 61 72 20 63 68 t least nChar ch
afbb0 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 0a 2a aracters long..*
afbc0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
afbd0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
afbe0 74 68 65 20 66 69 72 73 74 20 6e 43 68 61 72 20 the first nChar
afbf0 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 unicode characte
afc00 72 73 0a 2a 2a 20 69 6e 20 70 5a 2e 20 20 6e 43 rs.** in pZ. nC
afc10 68 61 72 20 6d 75 73 74 20 62 65 20 6e 6f 6e 2d har must be non-
afc20 6e 65 67 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c negative..*/.SQL
afc30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
afc40 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 74 65 sqlite3Utf16Byte
afc50 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a Len(const void *
afc60 7a 49 6e 2c 20 69 6e 74 20 6e 43 68 61 72 29 7b zIn, int nChar){
afc70 0a 20 20 69 6e 74 20 63 3b 0a 20 20 75 6e 73 69 . int c;. unsi
afc80 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 gned char const
afc90 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 69 6e 74 20 *z = zIn;. int
afca0 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 53 51 4c n = 0;. if( SQL
afcb0 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d ITE_UTF16NATIVE=
afcc0 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 =SQLITE_UTF16BE
afcd0 29 7b 0a 20 20 20 20 2f 2a 20 55 73 69 6e 67 20 ){. /* Using
afce0 61 6e 20 22 69 66 20 28 53 51 4c 49 54 45 5f 55 an "if (SQLITE_U
afcf0 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51 4c 49 TF16NATIVE==SQLI
afd00 54 45 5f 55 54 46 31 36 42 45 29 22 20 63 6f 6e TE_UTF16BE)" con
afd10 73 74 72 75 63 74 20 68 65 72 65 0a 20 20 20 20 struct here.
afd20 2a 2a 20 61 6e 64 20 69 6e 20 6f 74 68 65 72 20 ** and in other
afd30 70 61 72 74 73 20 6f 66 20 74 68 69 73 20 66 69 parts of this fi
afd40 6c 65 20 6d 65 61 6e 73 20 74 68 61 74 20 61 74 le means that at
afd50 20 6f 6e 65 20 62 72 61 6e 63 68 20 77 69 6c 6c one branch will
afd60 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 62 65 20 63 . ** not be c
afd70 6f 76 65 72 65 64 20 62 79 20 63 6f 76 65 72 61 overed by covera
afd80 67 65 20 74 65 73 74 69 6e 67 20 6f 6e 20 61 6e ge testing on an
afd90 79 20 73 69 6e 67 6c 65 20 68 6f 73 74 2e 20 42 y single host. B
afda0 75 74 20 63 6f 76 65 72 61 67 65 0a 20 20 20 20 ut coverage.
afdb0 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70 6c ** will be compl
afdc0 65 74 65 20 69 66 20 74 68 65 20 74 65 73 74 73 ete if the tests
afdd0 20 61 72 65 20 72 75 6e 20 6f 6e 20 62 6f 74 68 are run on both
afde0 20 61 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e a little-endian
afdf0 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 62 69 67 and . ** big
afe00 2d 65 6e 64 69 61 6e 20 68 6f 73 74 2e 20 42 65 -endian host. Be
afe10 63 61 75 73 65 20 62 6f 74 68 20 74 68 65 20 55 cause both the U
afe20 54 46 31 36 4e 41 54 49 56 45 20 61 6e 64 20 53 TF16NATIVE and S
afe30 51 4c 49 54 45 5f 55 54 46 31 36 42 45 0a 20 20 QLITE_UTF16BE.
afe40 20 20 2a 2a 20 6d 61 63 72 6f 73 20 61 72 65 20 ** macros are
afe50 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 constant at comp
afe60 69 6c 65 20 74 69 6d 65 20 74 68 65 20 63 6f 6d ile time the com
afe70 70 69 6c 65 72 20 63 61 6e 20 64 65 74 65 72 6d piler can determ
afe80 69 6e 65 0a 20 20 20 20 2a 2a 20 77 68 69 63 68 ine. ** which
afe90 20 62 72 61 6e 63 68 20 77 69 6c 6c 20 62 65 20 branch will be
afea0 66 6f 6c 6c 6f 77 65 64 2e 20 49 74 20 69 73 20 followed. It is
afeb0 74 68 65 72 65 66 6f 72 65 20 61 73 73 75 6d 65 therefore assume
afec0 64 20 74 68 61 74 20 6e 6f 20 72 75 6e 74 69 6d d that no runtim
afed0 65 0a 20 20 20 20 2a 2a 20 70 65 6e 61 6c 74 79 e. ** penalty
afee0 20 69 73 20 70 61 69 64 20 66 6f 72 20 74 68 69 is paid for thi
afef0 73 20 22 69 66 22 20 73 74 61 74 65 6d 65 6e 74 s "if" statement
aff00 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 69 .. */. whi
aff10 6c 65 28 20 6e 3c 6e 43 68 61 72 20 29 7b 0a 20 le( n<nChar ){.
aff20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 42 READ_UTF16B
aff30 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e E(z, c);. n
aff40 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 ++;. }. }els
aff50 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 3c e{. while( n<
aff60 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 52 nChar ){. R
aff70 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63 EAD_UTF16LE(z, c
aff80 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 );. n++;.
aff90 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
affa0 20 28 69 6e 74 29 28 7a 2d 28 75 6e 73 69 67 6e (int)(z-(unsign
affb0 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 29 ed char const *)
affc0 7a 49 6e 29 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 zIn);.}..#if def
affd0 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
affe0 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 )./*.** This rou
afff0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 tine is called f
b0000 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 73 74 rom the TCL test
b0010 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 6e 73 function "trans
b0020 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22 2e 0a late_selftest"..
b0030 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74 68 61 ** It checks tha
b0040 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 73 t the primitives
b0050 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 for serializing
b0060 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 7a 69 and deserializi
b0070 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 ng.** characters
b0080 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 69 6e in each encodin
b0090 67 20 61 72 65 20 69 6e 76 65 72 73 65 73 20 6f g are inverses o
b00a0 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f f each other..*/
b00b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b00c0 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74 66 53 void sqlite3UtfS
b00d0 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b 0a 20 elfTest(void){.
b00e0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 2c unsigned int i,
b00f0 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 t;. unsigned c
b0100 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 20 20 har zBuf[20];.
b0110 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
b0120 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 ;. int n;. uns
b0130 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20 igned int c;..
b0140 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 for(i=0; i<0x001
b0150 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 10000; i++){.
b0160 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 z = zBuf;. W
b0170 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 29 3b RITE_UTF8(z, i);
b0180 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a . n = (int)(z
b0190 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65 -zBuf);. asse
b01a0 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 rt( n>0 && n<=4
b01b0 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b );. z[0] = 0;
b01c0 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 . z = zBuf;.
b01d0 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 c = sqlite3Ut
b01e0 66 38 52 65 61 64 28 7a 2c 20 28 63 6f 6e 73 74 f8Read(z, (const
b01f0 20 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74 u8**)&z);. t
b0200 20 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 69 3e = i;. if( i>
b0210 3d 30 78 44 38 30 30 20 26 26 20 69 3c 3d 30 78 =0xD800 && i<=0x
b0220 44 46 46 46 20 29 20 74 20 3d 20 30 78 46 46 46 DFFF ) t = 0xFFF
b0230 44 3b 0a 20 20 20 20 69 66 28 20 28 69 26 30 78 D;. if( (i&0x
b0240 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 FFFFFFFE)==0xFFF
b0250 45 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a E ) t = 0xFFFD;.
b0260 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d 74 assert( c==t
b0270 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
b0280 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 (z-zBuf)==n );.
b0290 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }. for(i=0; i<
b02a0 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 0x00110000; i++)
b02b0 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44 {. if( i>=0xD
b02c0 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 20 800 && i<0xE000
b02d0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
b02e0 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52 z = zBuf;. WR
b02f0 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69 ITE_UTF16LE(z, i
b0300 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 );. n = (int)
b0310 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 (z-zBuf);. as
b0320 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d sert( n>0 && n<=
b0330 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 4 );. z[0] =
b0340 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0;. z = zBuf;
b0350 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 4c . READ_UTF16L
b0360 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 61 73 73 E(z, c);. ass
b0370 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20 ert( c==i );.
b0380 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 assert( (z-zBuf
b0390 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 66 6f )==n );. }. fo
b03a0 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 r(i=0; i<0x00110
b03b0 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 000; i++){. i
b03c0 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26 20 f( i>=0xD800 &&
b03d0 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e 74 69 i<0xE000 ) conti
b03e0 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 nue;. z = zBu
b03f0 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 54 46 f;. WRITE_UTF
b0400 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 20 20 20 16BE(z, i);.
b0410 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66 n = (int)(z-zBuf
b0420 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e );. assert( n
b0430 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 >0 && n<=4 );.
b0440 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 z[0] = 0;.
b0450 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 52 45 z = zBuf;. RE
b0460 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29 AD_UTF16BE(z, c)
b0470 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 3d ;. assert( c=
b0480 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =i );. assert
b0490 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b ( (z-zBuf)==n );
b04a0 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a . }.}.#endif /*
b04b0 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a SQLITE_TEST */.
b04c0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
b04d0 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a _OMIT_UTF16 */..
b04e0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
b04f0 45 6e 64 20 6f 66 20 75 74 66 2e 63 20 2a 2a 2a End of utf.c ***
b0500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
b0530 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
b0540 42 65 67 69 6e 20 66 69 6c 65 20 75 74 69 6c 2e Begin file util.
b0550 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
b0560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
b0580 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
b0590 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
b05a0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
b05b0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
b05c0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
b05d0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
b05e0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
b05f0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
b0600 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
b0610 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
b0620 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
b0630 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
b0640 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
b0650 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
b0660 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
b0670 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
b0680 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
b0690 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
b06a0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
b06b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b06c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b06d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b06e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b06f0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 55 74 69 6c 69 74 ******.** Utilit
b0700 79 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 y functions used
b0710 20 74 68 72 6f 75 67 68 6f 75 74 20 73 71 6c 69 throughout sqli
b0720 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 te..**.** This f
b0730 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e ile contains fun
b0740 63 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 6f 63 ctions for alloc
b0750 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 63 6f ating memory, co
b0760 6d 70 61 72 69 6e 67 0a 2a 2a 20 73 74 72 69 6e mparing.** strin
b0770 67 73 2c 20 61 6e 64 20 73 74 75 66 66 20 6c 69 gs, and stuff li
b0780 6b 65 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2f 0a 23 ke that..**.*/.#
b0790 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 56 ifdef SQLITE_HAV
b07a0 45 5f 49 53 4e 41 4e 0a 23 20 69 6e 63 6c 75 64 E_ISNAN.# includ
b07b0 65 20 3c 6d 61 74 68 2e 68 3e 0a 23 65 6e 64 69 e <math.h>.#endi
b07c0 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 f../*.** Routine
b07d0 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70 70 6f needed to suppo
b07e0 72 74 20 74 68 65 20 74 65 73 74 63 61 73 65 28 rt the testcase(
b07f0 29 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 64 ) macro..*/.#ifd
b0800 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 ef SQLITE_COVERA
b0810 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 GE_TEST.SQLITE_P
b0820 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
b0830 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e 74 20 te3Coverage(int
b0840 78 29 7b 0a 20 20 73 74 61 74 69 63 20 69 6e 74 x){. static int
b0850 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20 20 64 75 dummy = 0;. du
b0860 6d 6d 79 20 2b 3d 20 78 3b 0a 7d 0a 23 65 6e 64 mmy += x;.}.#end
b0870 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e if../*.** Return
b0880 20 74 72 75 65 20 69 66 20 74 68 65 20 66 6c 6f true if the flo
b0890 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 ating point valu
b08a0 65 20 69 73 20 4e 6f 74 20 61 20 4e 75 6d 62 65 e is Not a Numbe
b08b0 72 20 28 4e 61 4e 29 2e 0a 2a 2a 0a 2a 2a 20 55 r (NaN)..**.** U
b08c0 73 65 20 74 68 65 20 6d 61 74 68 20 6c 69 62 72 se the math libr
b08d0 61 72 79 20 69 73 6e 61 6e 28 29 20 66 75 6e 63 ary isnan() func
b08e0 74 69 6f 6e 20 69 66 20 63 6f 6d 70 69 6c 65 64 tion if compiled
b08f0 20 77 69 74 68 20 53 51 4c 49 54 45 5f 48 41 56 with SQLITE_HAV
b0900 45 5f 49 53 4e 41 4e 2e 0a 2a 2a 20 4f 74 68 65 E_ISNAN..** Othe
b0910 72 77 69 73 65 2c 20 77 65 20 68 61 76 65 20 6f rwise, we have o
b0920 75 72 20 6f 77 6e 20 69 6d 70 6c 65 6d 65 6e 74 ur own implement
b0930 61 74 69 6f 6e 20 74 68 61 74 20 77 6f 72 6b 73 ation that works
b0940 20 6f 6e 20 6d 6f 73 74 20 73 79 73 74 65 6d 73 on most systems
b0950 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
b0960 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
b0970 73 4e 61 4e 28 64 6f 75 62 6c 65 20 78 29 7b 0a sNaN(double x){.
b0980 20 20 69 6e 74 20 72 63 3b 20 20 20 2f 2a 20 54 int rc; /* T
b0990 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 20 he value return
b09a0 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 */.#if !defined(
b09b0 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 SQLITE_HAVE_ISNA
b09c0 4e 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 79 73 N). /*. ** Sys
b09d0 74 65 6d 73 20 74 68 61 74 20 73 75 70 70 6f 72 tems that suppor
b09e0 74 20 74 68 65 20 69 73 6e 61 6e 28 29 20 6c 69 t the isnan() li
b09f0 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 73 brary function s
b0a00 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 0a 20 hould probably.
b0a10 20 2a 2a 20 6d 61 6b 65 20 75 73 65 20 6f 66 20 ** make use of
b0a20 69 74 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67 20 it by compiling
b0a30 77 69 74 68 20 2d 44 53 51 4c 49 54 45 5f 48 41 with -DSQLITE_HA
b0a40 56 45 5f 49 53 4e 41 4e 2e 20 20 42 75 74 20 77 VE_ISNAN. But w
b0a50 65 20 68 61 76 65 0a 20 20 2a 2a 20 66 6f 75 6e e have. ** foun
b0a60 64 20 74 68 61 74 20 6d 61 6e 79 20 73 79 73 74 d that many syst
b0a70 65 6d 73 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 ems do not have
b0a80 61 20 77 6f 72 6b 69 6e 67 20 69 73 6e 61 6e 28 a working isnan(
b0a90 29 20 66 75 6e 63 74 69 6f 6e 20 73 6f 0a 20 20 ) function so.
b0aa0 2a 2a 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e ** this implemen
b0ab0 74 61 74 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 tation is provid
b0ac0 65 64 20 61 73 20 61 6e 20 61 6c 74 65 72 6e 61 ed as an alterna
b0ad0 74 69 76 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 tive.. **. **
b0ae0 54 68 69 73 20 4e 61 4e 20 74 65 73 74 20 73 6f This NaN test so
b0af0 6d 65 74 69 6d 65 73 20 66 61 69 6c 73 20 69 66 metimes fails if
b0b00 20 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 47 43 43 compiled on GCC
b0b10 20 77 69 74 68 20 2d 66 66 61 73 74 2d 6d 61 74 with -ffast-mat
b0b20 68 2e 0a 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f h.. ** On the o
b0b30 74 68 65 72 20 68 61 6e 64 2c 20 74 68 65 20 75 ther hand, the u
b0b40 73 65 20 6f 66 20 2d 66 66 61 73 74 2d 6d 61 74 se of -ffast-mat
b0b50 68 20 63 6f 6d 65 73 20 77 69 74 68 20 74 68 65 h comes with the
b0b60 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 following. **
b0b70 77 61 72 6e 69 6e 67 3a 0a 20 20 2a 2a 0a 20 20 warning:. **.
b0b80 2a 2a 20 20 20 20 20 20 54 68 69 73 20 6f 70 74 ** This opt
b0b90 69 6f 6e 20 5b 2d 66 66 61 73 74 2d 6d 61 74 68 ion [-ffast-math
b0ba0 5d 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 ] should never b
b0bb0 65 20 74 75 72 6e 65 64 20 6f 6e 20 62 79 20 61 e turned on by a
b0bc0 6e 79 0a 20 20 2a 2a 20 20 20 20 20 20 2d 4f 20 ny. ** -O
b0bd0 6f 70 74 69 6f 6e 20 73 69 6e 63 65 20 69 74 20 option since it
b0be0 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 69 6e can result in in
b0bf0 63 6f 72 72 65 63 74 20 6f 75 74 70 75 74 20 66 correct output f
b0c00 6f 72 20 70 72 6f 67 72 61 6d 73 0a 20 20 2a 2a or programs. **
b0c10 20 20 20 20 20 20 77 68 69 63 68 20 64 65 70 65 which depe
b0c20 6e 64 20 6f 6e 20 61 6e 20 65 78 61 63 74 20 69 nd on an exact i
b0c30 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
b0c40 20 49 45 45 45 20 6f 72 20 49 53 4f 20 0a 20 20 IEEE or ISO .
b0c50 2a 2a 20 20 20 20 20 20 72 75 6c 65 73 2f 73 70 ** rules/sp
b0c60 65 63 69 66 69 63 61 74 69 6f 6e 73 20 66 6f 72 ecifications for
b0c70 20 6d 61 74 68 20 66 75 6e 63 74 69 6f 6e 73 2e math functions.
b0c80 0a 20 20 2a 2a 0a 20 20 2a 2a 20 55 6e 64 65 72 . **. ** Under
b0c90 20 4d 53 56 43 2c 20 74 68 69 73 20 4e 61 4e 20 MSVC, this NaN
b0ca0 74 65 73 74 20 6d 61 79 20 66 61 69 6c 20 69 66 test may fail if
b0cb0 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 61 compiled with a
b0cc0 20 66 6c 6f 61 74 69 6e 67 2d 0a 20 20 2a 2a 20 floating-. **
b0cd0 70 6f 69 6e 74 20 70 72 65 63 69 73 69 6f 6e 20 point precision
b0ce0 6d 6f 64 65 20 6f 74 68 65 72 20 74 68 61 6e 20 mode other than
b0cf0 2f 66 70 3a 70 72 65 63 69 73 65 2e 20 20 46 72 /fp:precise. Fr
b0d00 6f 6d 20 74 68 65 20 4d 53 44 4e 20 0a 20 20 2a om the MSDN . *
b0d10 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 3a * documentation:
b0d20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 . **. **
b0d30 54 68 65 20 63 6f 6d 70 69 6c 65 72 20 5b 77 69 The compiler [wi
b0d40 74 68 20 2f 66 70 3a 70 72 65 63 69 73 65 5d 20 th /fp:precise]
b0d50 77 69 6c 6c 20 70 72 6f 70 65 72 6c 79 20 68 61 will properly ha
b0d60 6e 64 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 73 ndle comparisons
b0d70 20 0a 20 20 2a 2a 20 20 20 20 20 20 69 6e 76 6f . ** invo
b0d80 6c 76 69 6e 67 20 4e 61 4e 2e 20 46 6f 72 20 65 lving NaN. For e
b0d90 78 61 6d 70 6c 65 2c 20 78 20 21 3d 20 78 20 65 xample, x != x e
b0da0 76 61 6c 75 61 74 65 73 20 74 6f 20 74 72 75 65 valuates to true
b0db0 20 69 66 20 78 20 69 73 20 4e 61 4e 20 0a 20 20 if x is NaN .
b0dc0 2a 2a 20 20 20 20 20 20 2e 2e 2e 0a 20 20 2a 2f ** .... */
b0dd0 0a 23 69 66 64 65 66 20 5f 5f 46 41 53 54 5f 4d .#ifdef __FAST_M
b0de0 41 54 48 5f 5f 0a 23 20 65 72 72 6f 72 20 53 51 ATH__.# error SQ
b0df0 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 77 6f Lite will not wo
b0e00 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 77 69 74 rk correctly wit
b0e10 68 20 74 68 65 20 2d 66 66 61 73 74 2d 6d 61 74 h the -ffast-mat
b0e20 68 20 6f 70 74 69 6f 6e 20 6f 66 20 47 43 43 2e h option of GCC.
b0e30 0a 23 65 6e 64 69 66 0a 20 20 76 6f 6c 61 74 69 .#endif. volati
b0e40 6c 65 20 64 6f 75 62 6c 65 20 79 20 3d 20 78 3b le double y = x;
b0e50 0a 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 . volatile doub
b0e60 6c 65 20 7a 20 3d 20 79 3b 0a 20 20 72 63 20 3d le z = y;. rc =
b0e70 20 28 79 21 3d 7a 29 3b 0a 23 65 6c 73 65 20 20 (y!=z);.#else
b0e80 2f 2a 20 69 66 20 64 65 66 69 6e 65 64 28 53 51 /* if defined(SQ
b0e90 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 29 LITE_HAVE_ISNAN)
b0ea0 20 2a 2f 0a 20 20 72 63 20 3d 20 69 73 6e 61 6e */. rc = isnan
b0eb0 28 78 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 (x);.#endif /* S
b0ec0 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e QLITE_HAVE_ISNAN
b0ed0 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28 20 */. testcase(
b0ee0 72 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 rc );. return r
b0ef0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 c;.}../*.** Comp
b0f00 75 74 65 20 61 20 73 74 72 69 6e 67 20 6c 65 6e ute a string len
b0f10 67 74 68 20 74 68 61 74 20 69 73 20 6c 69 6d 69 gth that is limi
b0f20 74 65 64 20 74 6f 20 77 68 61 74 20 63 61 6e 20 ted to what can
b0f30 62 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 be stored in.**
b0f40 6c 6f 77 65 72 20 33 30 20 62 69 74 73 20 6f 66 lower 30 bits of
b0f50 20 61 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 a 32-bit signed
b0f60 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 integer..**.**
b0f70 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e The value return
b0f80 65 64 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 ed will never be
b0f90 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 72 20 negative. Nor
b0fa0 77 69 6c 6c 20 69 74 20 65 76 65 72 20 62 65 20 will it ever be
b0fb0 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 greater.** than
b0fc0 74 68 65 20 61 63 74 75 61 6c 20 6c 65 6e 67 74 the actual lengt
b0fd0 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 2e h of the string.
b0fe0 20 20 46 6f 72 20 76 65 72 79 20 6c 6f 6e 67 20 For very long
b0ff0 73 74 72 69 6e 67 73 20 28 67 72 65 61 74 65 72 strings (greater
b1000 0a 2a 2a 20 74 68 61 6e 20 31 47 69 42 29 20 74 .** than 1GiB) t
b1010 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
b1020 64 20 6d 69 67 68 74 20 62 65 20 6c 65 73 73 20 d might be less
b1030 74 68 61 6e 20 74 68 65 20 74 72 75 65 20 73 74 than the true st
b1040 72 69 6e 67 20 6c 65 6e 67 74 68 2e 0a 2a 2f 0a ring length..*/.
b1050 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
b1060 6e 74 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e nt sqlite3Strlen
b1070 33 30 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 30(const char *z
b1080 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 ){. const char
b1090 2a 7a 32 20 3d 20 7a 3b 0a 20 20 69 66 28 20 7a *z2 = z;. if( z
b10a0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
b10b0 20 20 77 68 69 6c 65 28 20 2a 7a 32 20 29 7b 20 while( *z2 ){
b10c0 7a 32 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e z2++; }. return
b10d0 20 30 78 33 66 66 66 66 66 66 66 20 26 20 28 69 0x3fffffff & (i
b10e0 6e 74 29 28 7a 32 20 2d 20 7a 29 3b 0a 7d 0a 0a nt)(z2 - z);.}..
b10f0 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d 6f /*.** Set the mo
b1100 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 st recent error
b1110 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20 73 code and error s
b1120 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 73 71 tring for the sq
b1130 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 22 lite.** handle "
b1140 64 62 22 2e 20 54 68 65 20 65 72 72 6f 72 20 63 db". The error c
b1150 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 22 65 ode is set to "e
b1160 72 72 5f 63 6f 64 65 22 2e 0a 2a 2a 0a 2a 2a 20 rr_code"..**.**
b1170 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c If it is not NUL
b1180 4c 2c 20 73 74 72 69 6e 67 20 7a 46 6f 72 6d 61 L, string zForma
b1190 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 t specifies the
b11a0 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 0a 2a 2a format of the.**
b11b0 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 69 6e error string in
b11c0 20 74 68 65 20 73 74 79 6c 65 20 6f 66 20 74 68 the style of th
b11d0 65 20 70 72 69 6e 74 66 20 66 75 6e 63 74 69 6f e printf functio
b11e0 6e 73 3a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e ns: The followin
b11f0 67 0a 2a 2a 20 66 6f 72 6d 61 74 20 63 68 61 72 g.** format char
b1200 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 acters are allow
b1210 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 ed:.**.** %
b1220 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 s Insert a
b1230 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25 string.** %
b1240 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e 67 20 z A string
b1250 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66 that should be f
b1260 72 65 65 64 20 61 66 74 65 72 20 75 73 65 0a 2a reed after use.*
b1270 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 20 49 * %d I
b1280 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72 nsert an integer
b1290 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20 20 .** %T
b12a0 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a Insert a token.
b12b0 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 20 20 ** %S
b12c0 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 73 74 Insert the first
b12d0 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 72 element of a Sr
b12e0 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 7a 46 6f 72 cList.**.** zFor
b12f0 6d 61 74 20 61 6e 64 20 61 6e 79 20 73 74 72 69 mat and any stri
b1300 6e 67 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 66 ng tokens that f
b1310 6f 6c 6c 6f 77 20 69 74 20 61 72 65 20 61 73 73 ollow it are ass
b1320 75 6d 65 64 20 74 6f 20 62 65 0a 2a 2a 20 65 6e umed to be.** en
b1330 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a coded in UTF-8..
b1340 2a 2a 0a 2a 2a 20 54 6f 20 63 6c 65 61 72 20 74 **.** To clear t
b1350 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 he most recent e
b1360 72 72 6f 72 20 66 6f 72 20 73 71 6c 69 74 65 20 rror for sqlite
b1370 68 61 6e 64 6c 65 20 22 64 62 22 2c 20 73 71 6c handle "db", sql
b1380 69 74 65 33 45 72 72 6f 72 0a 2a 2a 20 73 68 6f ite3Error.** sho
b1390 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 69 uld be called wi
b13a0 74 68 20 65 72 72 5f 63 6f 64 65 20 73 65 74 20 th err_code set
b13b0 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 to SQLITE_OK and
b13c0 20 7a 46 6f 72 6d 61 74 20 73 65 74 0a 2a 2a 20 zFormat set.**
b13d0 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 to NULL..*/.SQLI
b13e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
b13f0 73 71 6c 69 74 65 33 45 72 72 6f 72 28 73 71 6c sqlite3Error(sql
b1400 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 65 72 ite3 *db, int er
b1410 72 5f 63 6f 64 65 2c 20 63 6f 6e 73 74 20 63 68 r_code, const ch
b1420 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e ar *zFormat, ...
b1430 29 7b 0a 20 20 69 66 28 20 64 62 20 26 26 20 28 ){. if( db && (
b1440 64 62 2d 3e 70 45 72 72 20 7c 7c 20 28 64 62 2d db->pErr || (db-
b1450 3e 70 45 72 72 20 3d 20 73 71 6c 69 74 65 33 56 >pErr = sqlite3V
b1460 61 6c 75 65 4e 65 77 28 64 62 29 29 21 3d 30 29 alueNew(db))!=0)
b1470 20 29 7b 0a 20 20 20 20 64 62 2d 3e 65 72 72 43 ){. db->errC
b1480 6f 64 65 20 3d 20 65 72 72 5f 63 6f 64 65 3b 0a ode = err_code;.
b1490 20 20 20 20 69 66 28 20 7a 46 6f 72 6d 61 74 20 if( zFormat
b14a0 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a ){. char *z
b14b0 3b 0a 20 20 20 20 20 20 76 61 5f 6c 69 73 74 20 ;. va_list
b14c0 61 70 3b 0a 20 20 20 20 20 20 76 61 5f 73 74 61 ap;. va_sta
b14d0 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b rt(ap, zFormat);
b14e0 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 . z = sqlit
b14f0 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a e3VMPrintf(db, z
b1500 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 20 Format, ap);.
b1510 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 va_end(ap);.
b1520 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 sqlite3Valu
b1530 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72 eSetStr(db->pErr
b1540 2c 20 2d 31 2c 20 7a 2c 20 53 51 4c 49 54 45 5f , -1, z, SQLITE_
b1550 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 59 4e UTF8, SQLITE_DYN
b1560 41 4d 49 43 29 3b 0a 20 20 20 20 7d 65 6c 73 65 AMIC);. }else
b1570 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
b1580 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 alueSetStr(db->p
b1590 45 72 72 2c 20 30 2c 20 30 2c 20 53 51 4c 49 54 Err, 0, 0, SQLIT
b15a0 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 E_UTF8, SQLITE_S
b15b0 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 TATIC);. }.
b15c0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 }.}../*.** Add a
b15d0 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 n error message
b15e0 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d to pParse->zErrM
b15f0 73 67 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 sg and increment
b1600 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2e 0a 2a pParse->nErr..*
b1610 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
b1620 66 6f 72 6d 61 74 74 69 6e 67 20 63 68 61 72 61 formatting chara
b1630 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 65 cters are allowe
b1640 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 73 d:.**.** %s
b1650 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 73 Insert a s
b1660 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25 7a tring.** %z
b1670 20 20 20 20 20 20 41 20 73 74 72 69 6e 67 20 74 A string t
b1680 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66 72 hat should be fr
b1690 65 65 64 20 61 66 74 65 72 20 75 73 65 0a 2a 2a eed after use.**
b16a0 20 20 20 20 20 20 25 64 20 20 20 20 20 20 49 6e %d In
b16b0 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72 0a sert an integer.
b16c0 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20 20 20 ** %T
b16d0 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a 2a Insert a token.*
b16e0 2a 20 20 20 20 20 20 25 53 20 20 20 20 20 20 49 * %S I
b16f0 6e 73 65 72 74 20 74 68 65 20 66 69 72 73 74 20 nsert the first
b1700 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 72 63 element of a Src
b1710 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 List.**.** This
b1720 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 function should
b1730 62 65 20 75 73 65 64 20 74 6f 20 72 65 70 6f 72 be used to repor
b1740 74 20 61 6e 79 20 65 72 72 6f 72 20 74 68 61 74 t any error that
b1750 20 6f 63 63 75 72 73 20 77 68 69 6c 73 74 0a 2a occurs whilst.*
b1760 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 61 6e 20 53 * compiling an S
b1770 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 28 69 2e QL statement (i.
b1780 65 2e 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 e. within sqlite
b1790 33 5f 70 72 65 70 61 72 65 28 29 29 2e 20 54 68 3_prepare()). Th
b17a0 65 0a 2a 2a 20 6c 61 73 74 20 74 68 69 6e 67 20 e.** last thing
b17b0 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 the sqlite3_prep
b17c0 61 72 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 64 are() function d
b17d0 6f 65 73 20 69 73 20 63 6f 70 79 20 74 68 65 20 oes is copy the
b17e0 65 72 72 6f 72 0a 2a 2a 20 73 74 6f 72 65 64 20 error.** stored
b17f0 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e by this function
b1800 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 into the databa
b1810 73 65 20 68 61 6e 64 6c 65 20 75 73 69 6e 67 20 se handle using
b1820 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 2e 0a sqlite3Error()..
b1830 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c 69 ** Function sqli
b1840 74 65 33 45 72 72 6f 72 28 29 20 73 68 6f 75 6c te3Error() shoul
b1850 64 20 62 65 20 75 73 65 64 20 64 75 72 69 6e 67 d be used during
b1860 20 73 74 61 74 65 6d 65 6e 74 20 65 78 65 63 75 statement execu
b1870 74 69 6f 6e 0a 2a 2a 20 28 73 71 6c 69 74 65 33 tion.** (sqlite3
b1880 5f 73 74 65 70 28 29 20 65 74 63 2e 29 2e 0a 2a _step() etc.)..*
b1890 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b18a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 void sqlite3Err
b18b0 6f 72 4d 73 67 28 50 61 72 73 65 20 2a 70 50 61 orMsg(Parse *pPa
b18c0 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 rse, const char
b18d0 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a *zFormat, ...){.
b18e0 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 va_list ap;.
b18f0 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
b1900 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50 61 72 arse->db;. pPar
b1910 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 73 71 se->nErr++;. sq
b1920 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
b1930 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29 pParse->zErrMsg)
b1940 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c ;. va_start(ap,
b1950 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 70 50 61 zFormat);. pPa
b1960 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 rse->zErrMsg = s
b1970 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 qlite3VMPrintf(d
b1980 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b b, zFormat, ap);
b1990 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 . va_end(ap);.
b19a0 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 pParse->rc = SQ
b19b0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f LITE_ERROR;.}../
b19c0 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 65 *.** Clear the e
b19d0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 rror message in
b19e0 70 50 61 72 73 65 2c 20 69 66 20 61 6e 79 0a 2a pParse, if any.*
b19f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b1a00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 void sqlite3Err
b1a10 6f 72 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70 orClear(Parse *p
b1a20 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 Parse){. sqlite
b1a30 33 44 62 46 72 65 65 28 70 50 61 72 73 65 2d 3e 3DbFree(pParse->
b1a40 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 db, pParse->zErr
b1a50 4d 73 67 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e Msg);. pParse->
b1a60 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 70 zErrMsg = 0;. p
b1a70 50 61 72 73 65 2d 3e 6e 45 72 72 20 3d 20 30 3b Parse->nErr = 0;
b1a80 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 .}../*.** Conver
b1a90 74 20 61 6e 20 53 51 4c 2d 73 74 79 6c 65 20 71 t an SQL-style q
b1aa0 75 6f 74 65 64 20 73 74 72 69 6e 67 20 69 6e 74 uoted string int
b1ab0 6f 20 61 20 6e 6f 72 6d 61 6c 20 73 74 72 69 6e o a normal strin
b1ac0 67 20 62 79 20 72 65 6d 6f 76 69 6e 67 0a 2a 2a g by removing.**
b1ad0 20 74 68 65 20 71 75 6f 74 65 20 63 68 61 72 61 the quote chara
b1ae0 63 74 65 72 73 2e 20 20 54 68 65 20 63 6f 6e 76 cters. The conv
b1af0 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e 65 20 69 ersion is done i
b1b00 6e 2d 70 6c 61 63 65 2e 20 20 49 66 20 74 68 65 n-place. If the
b1b10 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 65 73 20 6e .** input does n
b1b20 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20 ot begin with a
b1b30 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 2c quote character,
b1b40 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
b1b50 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 ne.** is a no-op
b1b60 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 ..**.** The inpu
b1b70 74 20 73 74 72 69 6e 67 20 6d 75 73 74 20 62 65 t string must be
b1b80 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
b1b90 2e 20 20 41 20 6e 65 77 20 7a 65 72 6f 2d 74 65 . A new zero-te
b1ba0 72 6d 69 6e 61 74 6f 72 0a 2a 2a 20 69 73 20 61 rminator.** is a
b1bb0 64 64 65 64 20 74 6f 20 74 68 65 20 64 65 71 75 dded to the dequ
b1bc0 6f 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a oted string..**.
b1bd0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 ** The return va
b1be0 6c 75 65 20 69 73 20 2d 31 20 69 66 20 6e 6f 20 lue is -1 if no
b1bf0 64 65 71 75 6f 74 69 6e 67 20 6f 63 63 75 72 73 dequoting occurs
b1c00 20 6f 72 20 74 68 65 20 6c 65 6e 67 74 68 20 6f or the length o
b1c10 66 20 74 68 65 0a 2a 2a 20 64 65 71 75 6f 74 65 f the.** dequote
b1c20 64 20 73 74 72 69 6e 67 2c 20 65 78 63 6c 75 73 d string, exclus
b1c30 69 76 65 20 6f 66 20 74 68 65 20 7a 65 72 6f 20 ive of the zero
b1c40 74 65 72 6d 69 6e 61 74 6f 72 2c 20 69 66 20 64 terminator, if d
b1c50 65 71 75 6f 74 69 6e 67 20 64 6f 65 73 0a 2a 2a equoting does.**
b1c60 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 32 30 occur..**.** 20
b1c70 30 32 2d 46 65 62 2d 31 34 3a 20 54 68 69 73 20 02-Feb-14: This
b1c80 72 6f 75 74 69 6e 65 20 69 73 20 65 78 74 65 6e routine is exten
b1c90 64 65 64 20 74 6f 20 72 65 6d 6f 76 65 20 4d 53 ded to remove MS
b1ca0 2d 41 63 63 65 73 73 20 73 74 79 6c 65 0a 2a 2a -Access style.**
b1cb0 20 62 72 61 63 6b 65 74 73 20 66 72 6f 6d 20 61 brackets from a
b1cc0 72 6f 75 6e 64 20 69 64 65 6e 74 69 66 65 72 73 round identifers
b1cd0 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 20 . For example:
b1ce0 20 22 5b 61 2d 62 2d 63 5d 22 20 62 65 63 6f 6d "[a-b-c]" becom
b1cf0 65 73 0a 2a 2a 20 22 61 2d 62 2d 63 22 2e 0a 2a es.** "a-b-c"..*
b1d00 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b1d10 20 69 6e 74 20 73 71 6c 69 74 65 33 44 65 71 75 int sqlite3Dequ
b1d20 6f 74 65 28 63 68 61 72 20 2a 7a 29 7b 0a 20 20 ote(char *z){.
b1d30 63 68 61 72 20 71 75 6f 74 65 3b 0a 20 20 69 6e char quote;. in
b1d40 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28 20 7a 3d t i, j;. if( z=
b1d50 3d 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a =0 ) return -1;.
b1d60 20 20 71 75 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0a quote = z[0];.
b1d70 20 20 73 77 69 74 63 68 28 20 71 75 6f 74 65 20 switch( quote
b1d80 29 7b 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27 ){. case '\''
b1d90 3a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 : break;. ca
b1da0 73 65 20 27 22 27 3a 20 20 20 62 72 65 61 6b 3b se '"': break;
b1db0 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a 20 20 . case '`':
b1dc0 20 62 72 65 61 6b 3b 20 20 20 20 20 20 20 20 20 break;
b1dd0 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 4d 79 /* For My
b1de0 53 51 4c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 SQL compatibilit
b1df0 79 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 27 5b y */. case '[
b1e00 27 3a 20 20 20 71 75 6f 74 65 20 3d 20 27 5d 27 ': quote = ']'
b1e10 3b 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 46 6f ; break; /* Fo
b1e20 72 20 4d 53 20 53 71 6c 53 65 72 76 65 72 20 63 r MS SqlServer c
b1e30 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a ompatibility */.
b1e40 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 default:
b1e50 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 return -1;. }.
b1e60 20 66 6f 72 28 69 3d 31 2c 20 6a 3d 30 3b 20 41 for(i=1, j=0; A
b1e70 4c 57 41 59 53 28 7a 5b 69 5d 29 3b 20 69 2b 2b LWAYS(z[i]); i++
b1e80 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d ){. if( z[i]=
b1e90 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 =quote ){.
b1ea0 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71 75 6f 74 if( z[i+1]==quot
b1eb0 65 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a e ){. z[j
b1ec0 2b 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a 20 20 20 ++] = quote;.
b1ed0 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 i++;.
b1ee0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62 }else{. b
b1ef0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
b1f00 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a }else{. z
b1f10 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 [j++] = z[i];.
b1f20 20 20 7d 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d }. }. z[j] =
b1f30 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 6a 3b 0a 0;. return j;.
b1f40 7d 0a 0a 2f 2a 20 43 6f 6e 76 65 6e 69 65 6e 74 }../* Convenient
b1f50 20 73 68 6f 72 74 2d 68 61 6e 64 20 2a 2f 0a 23 short-hand */.#
b1f60 64 65 66 69 6e 65 20 55 70 70 65 72 54 6f 4c 6f define UpperToLo
b1f70 77 65 72 20 73 71 6c 69 74 65 33 55 70 70 65 72 wer sqlite3Upper
b1f80 54 6f 4c 6f 77 65 72 0a 0a 2f 2a 0a 2a 2a 20 53 ToLower../*.** S
b1f90 6f 6d 65 20 73 79 73 74 65 6d 73 20 68 61 76 65 ome systems have
b1fa0 20 73 74 72 69 63 6d 70 28 29 2e 20 20 4f 74 68 stricmp(). Oth
b1fb0 65 72 73 20 68 61 76 65 20 73 74 72 63 61 73 65 ers have strcase
b1fc0 63 6d 70 28 29 2e 20 20 42 65 63 61 75 73 65 0a cmp(). Because.
b1fd0 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 ** there is no c
b1fe0 6f 6e 73 69 73 74 65 6e 63 79 2c 20 77 65 20 77 onsistency, we w
b1ff0 69 6c 6c 20 64 65 66 69 6e 65 20 6f 75 72 20 6f ill define our o
b2000 77 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 wn..*/.SQLITE_PR
b2010 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b2020 33 53 74 72 49 43 6d 70 28 63 6f 6e 73 74 20 63 3StrICmp(const c
b2030 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 har *zLeft, cons
b2040 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74 29 7b t char *zRight){
b2050 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e 73 69 . register unsi
b2060 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 2a 62 gned char *a, *b
b2070 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 6e 65 ;. a = (unsigne
b2080 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b 0a d char *)zLeft;.
b2090 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 b = (unsigned
b20a0 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a 20 char *)zRight;.
b20b0 20 77 68 69 6c 65 28 20 2a 61 21 3d 30 20 26 26 while( *a!=0 &&
b20c0 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 UpperToLower[*a
b20d0 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b ]==UpperToLower[
b20e0 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 *b]){ a++; b++;
b20f0 7d 0a 20 20 72 65 74 75 72 6e 20 55 70 70 65 72 }. return Upper
b2100 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55 70 ToLower[*a] - Up
b2110 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b 0a perToLower[*b];.
b2120 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 }.SQLITE_API int
b2130 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d sqlite3_strnicm
b2140 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c p(const char *zL
b2150 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 eft, const char
b2160 2a 7a 52 69 67 68 74 2c 20 69 6e 74 20 4e 29 7b *zRight, int N){
b2170 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e 73 69 . register unsi
b2180 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 2a 62 gned char *a, *b
b2190 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 6e 65 ;. a = (unsigne
b21a0 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b 0a d char *)zLeft;.
b21b0 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 b = (unsigned
b21c0 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a 20 char *)zRight;.
b21d0 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 30 20 while( N-- > 0
b21e0 26 26 20 2a 61 21 3d 30 20 26 26 20 55 70 70 65 && *a!=0 && Uppe
b21f0 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55 70 rToLower[*a]==Up
b2200 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29 7b perToLower[*b]){
b2210 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20 72 a++; b++; }. r
b2220 65 74 75 72 6e 20 4e 3c 30 20 3f 20 30 20 3a 20 eturn N<0 ? 0 :
b2230 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d UpperToLower[*a]
b2240 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b - UpperToLower[
b2250 2a 62 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 *b];.}../*.** Re
b2260 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a 20 69 turn TRUE if z i
b2270 73 20 61 20 70 75 72 65 20 6e 75 6d 65 72 69 63 s a pure numeric
b2280 20 73 74 72 69 6e 67 2e 20 20 52 65 74 75 72 6e string. Return
b2290 20 46 41 4c 53 45 20 61 6e 64 20 6c 65 61 76 65 FALSE and leave
b22a0 0a 2a 2a 20 2a 72 65 61 6c 6e 75 6d 20 75 6e 63 .** *realnum unc
b22b0 68 61 6e 67 65 64 20 69 66 20 74 68 65 20 73 74 hanged if the st
b22c0 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 61 6e ring contains an
b22d0 79 20 63 68 61 72 61 63 74 65 72 20 77 68 69 63 y character whic
b22e0 68 20 69 73 20 6e 6f 74 0a 2a 2a 20 70 61 72 74 h is not.** part
b22f0 20 6f 66 20 61 20 6e 75 6d 62 65 72 2e 0a 2a 2a of a number..**
b2300 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 72 69 6e .** If the strin
b2310 67 20 69 73 20 70 75 72 65 20 6e 75 6d 65 72 69 g is pure numeri
b2320 63 2c 20 73 65 74 20 2a 72 65 61 6c 6e 75 6d 20 c, set *realnum
b2330 74 6f 20 54 52 55 45 20 69 66 20 74 68 65 20 73 to TRUE if the s
b2340 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 74 61 69 6e tring.** contain
b2350 73 20 74 68 65 20 27 2e 27 20 63 68 61 72 61 63 s the '.' charac
b2360 74 65 72 20 6f 72 20 61 6e 20 22 45 2b 30 30 30 ter or an "E+000
b2370 22 20 73 74 79 6c 65 20 65 78 70 6f 6e 65 6e 74 " style exponent
b2380 69 61 74 69 6f 6e 20 73 75 66 66 69 78 2e 0a 2a iation suffix..*
b2390 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 74 20 * Otherwise set
b23a0 2a 72 65 61 6c 6e 75 6d 20 74 6f 20 46 41 4c 53 *realnum to FALS
b23b0 45 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 6a 75 E. Note that ju
b23c0 73 74 20 62 65 63 61 75 65 20 2a 72 65 61 6c 6e st becaue *realn
b23d0 75 6d 20 69 73 0a 2a 2a 20 66 61 6c 73 65 20 64 um is.** false d
b23e0 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74 68 61 oes not mean tha
b23f0 74 20 74 68 65 20 6e 75 6d 62 65 72 20 63 61 6e t the number can
b2400 20 62 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79 be successfully
b2410 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 0a converted into.
b2420 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 2d 20 ** an integer -
b2430 69 74 20 6d 69 67 68 74 20 62 65 20 74 6f 6f 20 it might be too
b2440 62 69 67 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 6d big..**.** An em
b2450 70 74 79 20 73 74 72 69 6e 67 20 69 73 20 63 6f pty string is co
b2460 6e 73 69 64 65 72 65 64 20 6e 6f 6e 2d 6e 75 6d nsidered non-num
b2470 65 72 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f eric..*/.SQLITE_
b2480 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
b2490 74 65 33 49 73 4e 75 6d 62 65 72 28 63 6f 6e 73 te3IsNumber(cons
b24a0 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a t char *z, int *
b24b0 72 65 61 6c 6e 75 6d 2c 20 75 38 20 65 6e 63 29 realnum, u8 enc)
b24c0 7b 0a 20 20 69 6e 74 20 69 6e 63 72 20 3d 20 28 {. int incr = (
b24d0 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 enc==SQLITE_UTF8
b24e0 3f 31 3a 32 29 3b 0a 20 20 69 66 28 20 65 6e 63 ?1:2);. if( enc
b24f0 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 ==SQLITE_UTF16BE
b2500 20 29 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a ) z++;. if( *z
b2510 3d 3d 27 2d 27 20 7c 7c 20 2a 7a 3d 3d 27 2b 27 =='-' || *z=='+'
b2520 20 29 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 ) z += incr;.
b2530 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 if( !sqlite3Isdi
b2540 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20 72 git(*z) ){. r
b2550 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a eturn 0;. }. z
b2560 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 2a 72 65 61 += incr;. *rea
b2570 6c 6e 75 6d 20 3d 20 30 3b 0a 20 20 77 68 69 6c lnum = 0;. whil
b2580 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 e( sqlite3Isdigi
b2590 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e t(*z) ){ z += in
b25a0 63 72 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d cr; }. if( *z==
b25b0 27 2e 27 20 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 '.' ){. z +=
b25c0 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 21 73 incr;. if( !s
b25d0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a qlite3Isdigit(*z
b25e0 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 ) ) return 0;.
b25f0 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 while( sqlite3
b2600 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a Isdigit(*z) ){ z
b2610 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20 += incr; }.
b2620 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 *realnum = 1;.
b2630 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20 }. if( *z=='e'
b2640 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 || *z=='E' ){.
b2650 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 z += incr;.
b2660 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 7c 7c 20 if( *z=='+' ||
b2670 2a 7a 3d 3d 27 2d 27 20 29 20 7a 20 2b 3d 20 69 *z=='-' ) z += i
b2680 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 21 73 71 ncr;. if( !sq
b2690 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 lite3Isdigit(*z)
b26a0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 ) return 0;.
b26b0 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 while( sqlite3I
b26c0 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20 sdigit(*z) ){ z
b26d0 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20 2a += incr; }. *
b26e0 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d realnum = 1;. }
b26f0 0a 20 20 72 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b . return *z==0;
b2700 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 .}../*.** The st
b2710 72 69 6e 67 20 7a 5b 5d 20 69 73 20 61 6e 20 41 ring z[] is an A
b2720 53 43 49 49 20 72 65 70 72 65 73 65 6e 74 61 74 SCII representat
b2730 69 6f 6e 20 6f 66 20 61 20 72 65 61 6c 20 6e 75 ion of a real nu
b2740 6d 62 65 72 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74 mber..** Convert
b2750 20 74 68 69 73 20 73 74 72 69 6e 67 20 74 6f 20 this string to
b2760 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 a double..**.**
b2770 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 73 73 This routine ass
b2780 75 6d 65 73 20 74 68 61 74 20 7a 5b 5d 20 72 65 umes that z[] re
b2790 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69 64 20 ally is a valid
b27a0 6e 75 6d 62 65 72 2e 20 20 49 66 20 69 74 0a 2a number. If it.*
b27b0 2a 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 72 65 * is not, the re
b27c0 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69 6e 65 sult is undefine
b27d0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f d..**.** This ro
b27e0 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e utine is used in
b27f0 73 74 65 61 64 20 6f 66 20 74 68 65 20 6c 69 62 stead of the lib
b2800 72 61 72 79 20 61 74 6f 66 28 29 20 66 75 6e 63 rary atof() func
b2810 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 tion because.**
b2820 74 68 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66 the library atof
b2830 28 29 20 6d 69 67 68 74 20 77 61 6e 74 20 74 6f () might want to
b2840 20 75 73 65 20 22 2c 22 20 61 73 20 74 68 65 20 use "," as the
b2850 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e decimal point in
b2860 73 74 65 61 64 0a 2a 2a 20 6f 66 20 22 2e 22 20 stead.** of "."
b2870 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 depending on how
b2880 20 6c 6f 63 61 6c 65 20 69 73 20 73 65 74 2e 20 locale is set.
b2890 20 42 75 74 20 74 68 61 74 20 77 6f 75 6c 64 20 But that would
b28a0 63 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 0a 2a cause problems.*
b28b0 2a 20 66 6f 72 20 53 51 4c 2e 20 20 53 6f 20 74 * for SQL. So t
b28c0 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 77 61 his routine alwa
b28d0 79 73 20 75 73 65 73 20 22 2e 22 20 72 65 67 61 ys uses "." rega
b28e0 72 64 6c 65 73 73 20 6f 66 20 6c 6f 63 61 6c 65 rdless of locale
b28f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
b2900 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 ATE int sqlite3A
b2910 74 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 20 2a toF(const char *
b2920 7a 2c 20 64 6f 75 62 6c 65 20 2a 70 52 65 73 75 z, double *pResu
b2930 6c 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c lt){.#ifndef SQL
b2940 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
b2950 47 5f 50 4f 49 4e 54 0a 20 20 63 6f 6e 73 74 20 G_POINT. const
b2960 63 68 61 72 20 2a 7a 42 65 67 69 6e 20 3d 20 7a char *zBegin = z
b2970 3b 0a 20 20 2f 2a 20 73 69 67 6e 20 2a 20 73 69 ;. /* sign * si
b2980 67 6e 69 66 69 63 61 6e 64 20 2a 20 28 31 30 20 gnificand * (10
b2990 5e 20 28 65 73 69 67 6e 20 2a 20 65 78 70 6f 6e ^ (esign * expon
b29a0 65 6e 74 29 29 20 2a 2f 0a 20 20 69 6e 74 20 73 ent)) */. int s
b29b0 69 67 6e 20 3d 20 31 3b 20 20 20 2f 2a 20 73 69 ign = 1; /* si
b29c0 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63 61 6e gn of significan
b29d0 64 20 2a 2f 0a 20 20 69 36 34 20 73 20 3d 20 30 d */. i64 s = 0
b29e0 3b 20 20 20 20 20 20 2f 2a 20 73 69 67 6e 69 66 ; /* signif
b29f0 69 63 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 64 icand */. int d
b2a00 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 61 64 = 0; /* ad
b2a10 6a 75 73 74 20 65 78 70 6f 6e 65 6e 74 20 66 6f just exponent fo
b2a20 72 20 73 68 69 66 74 69 6e 67 20 64 65 63 69 6d r shifting decim
b2a30 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 69 6e al point */. in
b2a40 74 20 65 73 69 67 6e 20 3d 20 31 3b 20 20 2f 2a t esign = 1; /*
b2a50 20 73 69 67 6e 20 6f 66 20 65 78 70 6f 6e 65 6e sign of exponen
b2a60 74 20 2a 2f 0a 20 20 69 6e 74 20 65 20 3d 20 30 t */. int e = 0
b2a70 3b 20 20 20 20 20 20 2f 2a 20 65 78 70 6f 6e 65 ; /* expone
b2a80 6e 74 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 nt */. double r
b2a90 65 73 75 6c 74 3b 0a 20 20 69 6e 74 20 6e 44 69 esult;. int nDi
b2aa0 67 69 74 73 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 gits = 0;.. /*
b2ab0 73 6b 69 70 20 6c 65 61 64 69 6e 67 20 73 70 61 skip leading spa
b2ac0 63 65 73 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 ces */. while(
b2ad0 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a sqlite3Isspace(*
b2ae0 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 2f 2a 20 67 z) ) z++;. /* g
b2af0 65 74 20 73 69 67 6e 20 6f 66 20 73 69 67 6e 69 et sign of signi
b2b00 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 66 28 20 ficand */. if(
b2b10 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 *z=='-' ){. s
b2b20 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7a 2b ign = -1;. z+
b2b30 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a +;. }else if( *
b2b40 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 7a 2b z=='+' ){. z+
b2b50 2b 3b 0a 20 20 7d 0a 20 20 2f 2a 20 73 6b 69 70 +;. }. /* skip
b2b60 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 65 73 20 leading zeroes
b2b70 2a 2f 0a 20 20 77 68 69 6c 65 28 20 7a 5b 30 5d */. while( z[0]
b2b80 3d 3d 27 30 27 20 29 20 7a 2b 2b 2c 20 6e 44 69 =='0' ) z++, nDi
b2b90 67 69 74 73 2b 2b 3b 0a 0a 20 20 2f 2a 20 63 6f gits++;.. /* co
b2ba0 70 79 20 6d 61 78 20 73 69 67 6e 69 66 69 63 61 py max significa
b2bb0 6e 74 20 64 69 67 69 74 73 20 74 6f 20 73 69 67 nt digits to sig
b2bc0 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 77 68 nificand */. wh
b2bd0 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 ile( sqlite3Isdi
b2be0 67 69 74 28 2a 7a 29 20 26 26 20 73 3c 28 28 4c git(*z) && s<((L
b2bf0 41 52 47 45 53 54 5f 49 4e 54 36 34 2d 39 29 2f ARGEST_INT64-9)/
b2c00 31 30 29 20 29 7b 0a 20 20 20 20 73 20 3d 20 73 10) ){. s = s
b2c10 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 *10 + (*z - '0')
b2c20 3b 0a 20 20 20 20 7a 2b 2b 2c 20 6e 44 69 67 69 ;. z++, nDigi
b2c30 74 73 2b 2b 3b 0a 20 20 7d 0a 20 20 2f 2a 20 73 ts++;. }. /* s
b2c40 6b 69 70 20 6e 6f 6e 2d 73 69 67 6e 69 66 69 63 kip non-signific
b2c50 61 6e 74 20 73 69 67 6e 69 66 69 63 61 6e 64 20 ant significand
b2c60 64 69 67 69 74 73 0a 20 20 2a 2a 20 28 69 6e 63 digits. ** (inc
b2c70 72 65 61 73 65 20 65 78 70 6f 6e 65 6e 74 20 62 rease exponent b
b2c80 79 20 64 20 74 6f 20 73 68 69 66 74 20 64 65 63 y d to shift dec
b2c90 69 6d 61 6c 20 6c 65 66 74 29 20 2a 2f 0a 20 20 imal left) */.
b2ca0 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 while( sqlite3Is
b2cb0 64 69 67 69 74 28 2a 7a 29 20 29 20 7a 2b 2b 2c digit(*z) ) z++,
b2cc0 20 6e 44 69 67 69 74 73 2b 2b 2c 20 64 2b 2b 3b nDigits++, d++;
b2cd0 0a 0a 20 20 2f 2a 20 69 66 20 64 65 63 69 6d 61 .. /* if decima
b2ce0 6c 20 70 6f 69 6e 74 20 69 73 20 70 72 65 73 65 l point is prese
b2cf0 6e 74 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d 3d nt */. if( *z==
b2d00 27 2e 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a '.' ){. z++;.
b2d10 20 20 20 20 2f 2a 20 63 6f 70 79 20 64 69 67 69 /* copy digi
b2d20 74 73 20 66 72 6f 6d 20 61 66 74 65 72 20 64 65 ts from after de
b2d30 63 69 6d 61 6c 20 74 6f 20 73 69 67 6e 69 66 69 cimal to signifi
b2d40 63 61 6e 64 0a 20 20 20 20 2a 2a 20 28 64 65 63 cand. ** (dec
b2d50 72 65 61 73 65 20 65 78 70 6f 6e 65 6e 74 20 62 rease exponent b
b2d60 79 20 64 20 74 6f 20 73 68 69 66 74 20 64 65 63 y d to shift dec
b2d70 69 6d 61 6c 20 72 69 67 68 74 29 20 2a 2f 0a 20 imal right) */.
b2d80 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 while( sqlite
b2d90 33 49 73 64 69 67 69 74 28 2a 7a 29 20 26 26 20 3Isdigit(*z) &&
b2da0 73 3c 28 28 4c 41 52 47 45 53 54 5f 49 4e 54 36 s<((LARGEST_INT6
b2db0 34 2d 39 29 2f 31 30 29 20 29 7b 0a 20 20 20 20 4-9)/10) ){.
b2dc0 20 20 73 20 3d 20 73 2a 31 30 20 2b 20 28 2a 7a s = s*10 + (*z
b2dd0 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20 7a - '0');. z
b2de0 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 2c 20 64 ++, nDigits++, d
b2df0 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a --;. }. /*
b2e00 20 73 6b 69 70 20 6e 6f 6e 2d 73 69 67 6e 69 66 skip non-signif
b2e10 69 63 61 6e 74 20 64 69 67 69 74 73 20 2a 2f 0a icant digits */.
b2e20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 while( sqlit
b2e30 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20 e3Isdigit(*z) )
b2e40 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 3b 0a z++, nDigits++;.
b2e50 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 65 78 70 }.. /* if exp
b2e60 6f 6e 65 6e 74 20 69 73 20 70 72 65 73 65 6e 74 onent is present
b2e70 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 65 */. if( *z=='e
b2e80 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a ' || *z=='E' ){.
b2e90 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 2f 2a 20 z++;. /*
b2ea0 67 65 74 20 73 69 67 6e 20 6f 66 20 65 78 70 6f get sign of expo
b2eb0 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 nent */. if(
b2ec0 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 *z=='-' ){.
b2ed0 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 esign = -1;.
b2ee0 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 z++;. }els
b2ef0 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b e if( *z=='+' ){
b2f00 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 . z++;.
b2f10 7d 0a 20 20 20 20 2f 2a 20 63 6f 70 79 20 64 69 }. /* copy di
b2f20 67 69 74 73 20 74 6f 20 65 78 70 6f 6e 65 6e 74 gits to exponent
b2f30 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 73 */. while( s
b2f40 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a qlite3Isdigit(*z
b2f50 29 20 29 7b 0a 20 20 20 20 20 20 65 20 3d 20 65 ) ){. e = e
b2f60 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 *10 + (*z - '0')
b2f70 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 ;. z++;.
b2f80 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 64 6a }. }.. /* adj
b2f90 75 73 74 20 65 78 70 6f 6e 65 6e 74 20 62 79 20 ust exponent by
b2fa0 64 2c 20 61 6e 64 20 75 70 64 61 74 65 20 73 69 d, and update si
b2fb0 67 6e 20 2a 2f 0a 20 20 65 20 3d 20 28 65 2a 65 gn */. e = (e*e
b2fc0 73 69 67 6e 29 20 2b 20 64 3b 0a 20 20 69 66 28 sign) + d;. if(
b2fd0 20 65 3c 30 20 29 20 7b 0a 20 20 20 20 65 73 69 e<0 ) {. esi
b2fe0 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 65 20 2a gn = -1;. e *
b2ff0 3d 20 2d 31 3b 0a 20 20 7d 20 65 6c 73 65 20 7b = -1;. } else {
b3000 0a 20 20 20 20 65 73 69 67 6e 20 3d 20 31 3b 0a . esign = 1;.
b3010 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 30 20 73 }.. /* if 0 s
b3020 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 ignificand */.
b3030 69 66 28 20 21 73 20 29 20 7b 0a 20 20 20 20 2f if( !s ) {. /
b3040 2a 20 49 6e 20 74 68 65 20 49 45 45 45 20 37 35 * In the IEEE 75
b3050 34 20 73 74 61 6e 64 61 72 64 2c 20 7a 65 72 6f 4 standard, zero
b3060 20 69 73 20 73 69 67 6e 65 64 2e 0a 20 20 20 20 is signed..
b3070 2a 2a 20 41 64 64 20 74 68 65 20 73 69 67 6e 20 ** Add the sign
b3080 69 66 20 77 65 27 76 65 20 73 65 65 6e 20 61 74 if we've seen at
b3090 20 6c 65 61 73 74 20 6f 6e 65 20 64 69 67 69 74 least one digit
b30a0 20 2a 2f 0a 20 20 20 20 72 65 73 75 6c 74 20 3d */. result =
b30b0 20 28 73 69 67 6e 3c 30 20 26 26 20 6e 44 69 67 (sign<0 && nDig
b30c0 69 74 73 29 20 3f 20 2d 28 64 6f 75 62 6c 65 29 its) ? -(double)
b30d0 30 20 3a 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 0 : (double)0;.
b30e0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a } else {. /*
b30f0 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 64 75 attempt to redu
b3100 63 65 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 ce exponent */.
b3110 20 20 20 69 66 28 20 65 73 69 67 6e 3e 30 20 29 if( esign>0 )
b3120 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 {. while( s
b3130 3c 28 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 2f <(LARGEST_INT64/
b3140 31 30 29 20 26 26 20 65 3e 30 20 29 20 65 2d 2d 10) && e>0 ) e--
b3150 2c 73 2a 3d 31 30 3b 0a 20 20 20 20 7d 65 6c 73 ,s*=10;. }els
b3160 65 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 e{. while(
b3170 21 28 73 25 31 30 29 20 26 26 20 65 3e 30 20 29 !(s%10) && e>0 )
b3180 20 65 2d 2d 2c 73 2f 3d 31 30 3b 0a 20 20 20 20 e--,s/=10;.
b3190 7d 0a 0a 20 20 20 20 2f 2a 20 61 64 6a 75 73 74 }.. /* adjust
b31a0 20 74 68 65 20 73 69 67 6e 20 6f 66 20 73 69 67 the sign of sig
b31b0 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 20 20 nificand */.
b31c0 73 20 3d 20 73 69 67 6e 3c 30 20 3f 20 2d 73 20 s = sign<0 ? -s
b31d0 3a 20 73 3b 0a 0a 20 20 20 20 2f 2a 20 69 66 20 : s;.. /* if
b31e0 65 78 70 6f 6e 65 6e 74 2c 20 73 63 61 6c 65 20 exponent, scale
b31f0 73 69 67 6e 69 66 69 63 61 6e 64 20 61 73 20 61 significand as a
b3200 70 70 72 6f 70 72 69 61 74 65 0a 20 20 20 20 2a ppropriate. *
b3210 2a 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 72 * and store in r
b3220 65 73 75 6c 74 2e 20 2a 2f 0a 20 20 20 20 69 66 esult. */. if
b3230 28 20 65 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 ( e ){. dou
b3240 62 6c 65 20 73 63 61 6c 65 20 3d 20 31 2e 30 3b ble scale = 1.0;
b3250 0a 20 20 20 20 20 20 2f 2a 20 61 74 74 65 6d 70 . /* attemp
b3260 74 20 74 6f 20 68 61 6e 64 6c 65 20 65 78 74 72 t to handle extr
b3270 65 6d 65 6c 79 20 73 6d 61 6c 6c 2f 6c 61 72 67 emely small/larg
b3280 65 20 6e 75 6d 62 65 72 73 20 62 65 74 74 65 72 e numbers better
b3290 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 65 3e */. if( e>
b32a0 33 30 37 20 26 26 20 65 3c 33 34 32 20 29 7b 0a 307 && e<342 ){.
b32b0 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 65 while( e
b32c0 25 33 30 38 20 29 20 7b 20 73 63 61 6c 65 20 2a %308 ) { scale *
b32d0 3d 20 31 2e 30 65 2b 31 3b 20 65 20 2d 3d 20 31 = 1.0e+1; e -= 1
b32e0 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 ; }. if(
b32f0 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20 20 20 20 esign<0 ){.
b3300 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20 result = s
b3310 2f 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 20 / scale;.
b3320 20 20 20 72 65 73 75 6c 74 20 2f 3d 20 31 2e 30 result /= 1.0
b3330 65 2b 33 30 38 3b 0a 20 20 20 20 20 20 20 20 7d e+308;. }
b3340 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
b3350 72 65 73 75 6c 74 20 3d 20 73 20 2a 20 73 63 61 result = s * sca
b3360 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 le;. re
b3370 73 75 6c 74 20 2a 3d 20 31 2e 30 65 2b 33 30 38 sult *= 1.0e+308
b3380 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
b3390 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
b33a0 20 2f 2a 20 31 2e 30 65 2b 32 32 20 69 73 20 74 /* 1.0e+22 is t
b33b0 68 65 20 6c 61 72 67 65 73 74 20 70 6f 77 65 72 he largest power
b33c0 20 6f 66 20 31 30 20 74 68 61 6e 20 63 61 6e 20 of 10 than can
b33d0 62 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 be . ** r
b33e0 65 70 72 65 73 65 6e 74 65 64 20 65 78 61 63 74 epresented exact
b33f0 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 ly. */. w
b3400 68 69 6c 65 28 20 65 25 32 32 20 29 20 7b 20 73 hile( e%22 ) { s
b3410 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 cale *= 1.0e+1;
b3420 65 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20 20 20 e -= 1; }.
b3430 20 20 77 68 69 6c 65 28 20 65 3e 30 20 29 20 7b while( e>0 ) {
b3440 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 32 scale *= 1.0e+2
b3450 32 3b 20 65 20 2d 3d 20 32 32 3b 20 7d 0a 20 20 2; e -= 22; }.
b3460 20 20 20 20 20 20 69 66 28 20 65 73 69 67 6e 3c if( esign<
b3470 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 0 ){. r
b3480 65 73 75 6c 74 20 3d 20 73 20 2f 20 73 63 61 6c esult = s / scal
b3490 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 e;. }else
b34a0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 75 {. resu
b34b0 6c 74 20 3d 20 73 20 2a 20 73 63 61 6c 65 3b 0a lt = s * scale;.
b34c0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
b34d0 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 }. } else {.
b34e0 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 28 64 result = (d
b34f0 6f 75 62 6c 65 29 73 3b 0a 20 20 20 20 7d 0a 20 ouble)s;. }.
b3500 20 7d 0a 0a 20 20 2f 2a 20 73 74 6f 72 65 20 74 }.. /* store t
b3510 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 2a he result */. *
b3520 70 52 65 73 75 6c 74 20 3d 20 72 65 73 75 6c 74 pResult = result
b3530 3b 0a 0a 20 20 2f 2a 20 72 65 74 75 72 6e 20 6e ;.. /* return n
b3540 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
b3550 65 72 73 20 75 73 65 64 20 2a 2f 0a 20 20 72 65 ers used */. re
b3560 74 75 72 6e 20 28 69 6e 74 29 28 7a 20 2d 20 7a turn (int)(z - z
b3570 42 65 67 69 6e 29 3b 0a 23 65 6c 73 65 0a 20 20 Begin);.#else.
b3580 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 74 return sqlite3At
b3590 6f 69 36 34 28 7a 2c 20 70 52 65 73 75 6c 74 29 oi64(z, pResult)
b35a0 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ;.#endif /* SQLI
b35b0 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 TE_OMIT_FLOATING
b35c0 5f 50 4f 49 4e 54 20 2a 2f 0a 7d 0a 0a 2f 2a 0a _POINT */.}../*.
b35d0 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 31 ** Compare the 1
b35e0 39 2d 63 68 61 72 61 63 74 65 72 20 73 74 72 69 9-character stri
b35f0 6e 67 20 7a 4e 75 6d 20 61 67 61 69 6e 73 74 20 ng zNum against
b3600 74 68 65 20 74 65 78 74 20 72 65 70 72 65 73 65 the text represe
b3610 6e 74 61 74 69 6f 6e 0a 2a 2a 20 76 61 6c 75 65 ntation.** value
b3620 20 32 5e 36 33 3a 20 20 39 32 32 33 33 37 32 30 2^63: 92233720
b3630 33 36 38 35 34 37 37 35 38 30 38 2e 20 20 52 65 36854775808. Re
b3640 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a turn negative, z
b3650 65 72 6f 2c 20 6f 72 20 70 6f 73 69 74 69 76 65 ero, or positive
b3660 0a 2a 2a 20 69 66 20 7a 4e 75 6d 20 69 73 20 6c .** if zNum is l
b3670 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 ess than, equal
b3680 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 20 74 to, or greater t
b3690 68 61 6e 20 74 68 65 20 73 74 72 69 6e 67 2e 0a han the string..
b36a0 2a 2a 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d 65 6d **.** Unlike mem
b36b0 63 6d 70 28 29 20 74 68 69 73 20 72 6f 75 74 69 cmp() this routi
b36c0 6e 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 ne is guaranteed
b36d0 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65 20 64 to return the d
b36e0 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 69 6e 20 ifference.** in
b36f0 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74 68 the values of th
b3700 65 20 6c 61 73 74 20 64 69 67 69 74 20 69 66 20 e last digit if
b3710 74 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 the only differe
b3720 6e 63 65 20 69 73 20 69 6e 20 74 68 65 0a 2a 2a nce is in the.**
b3730 20 6c 61 73 74 20 64 69 67 69 74 2e 20 20 53 6f last digit. So
b3740 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a , for example,.*
b3750 2a 0a 2a 2a 20 20 20 20 20 20 63 6f 6d 70 61 72 *.** compar
b3760 65 32 70 6f 77 36 33 28 22 39 32 32 33 33 37 32 e2pow63("9223372
b3770 30 33 36 38 35 34 37 37 35 38 30 30 22 29 0a 2a 036854775800").*
b3780 2a 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e *.** will return
b3790 20 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 -8..*/.static i
b37a0 6e 74 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 nt compare2pow63
b37b0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 (const char *zNu
b37c0 6d 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 63 m){. int c;. c
b37d0 20 3d 20 6d 65 6d 63 6d 70 28 7a 4e 75 6d 2c 22 = memcmp(zNum,"
b37e0 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 9223372036854775
b37f0 38 30 22 2c 31 38 29 2a 31 30 3b 0a 20 20 69 66 80",18)*10;. if
b3800 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 63 20 ( c==0 ){. c
b3810 3d 20 7a 4e 75 6d 5b 31 38 5d 20 2d 20 27 38 27 = zNum[18] - '8'
b3820 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 ;. }. return c
b3830 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ;.}.../*.** Retu
b3840 72 6e 20 54 52 55 45 20 69 66 20 7a 4e 75 6d 20 rn TRUE if zNum
b3850 69 73 20 61 20 36 34 2d 62 69 74 20 73 69 67 6e is a 64-bit sign
b3860 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64 20 77 ed integer and w
b3870 72 69 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75 rite.** the valu
b3880 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 e of the integer
b3890 20 69 6e 74 6f 20 2a 70 4e 75 6d 2e 20 20 49 66 into *pNum. If
b38a0 20 7a 4e 75 6d 20 69 73 20 6e 6f 74 20 61 6e 20 zNum is not an
b38b0 69 6e 74 65 67 65 72 0a 2a 2a 20 6f 72 20 69 73 integer.** or is
b38c0 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 an integer that
b38d0 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f is too large to
b38e0 20 62 65 20 65 78 70 72 65 73 73 65 64 20 77 69 be expressed wi
b38f0 74 68 20 36 34 20 62 69 74 73 2c 0a 2a 2a 20 74 th 64 bits,.** t
b3900 68 65 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 65 hen return false
b3910 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 ..**.** When thi
b3920 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 6f 72 s routine was or
b3930 69 67 69 6e 61 6c 6c 79 20 77 72 69 74 74 65 6e iginally written
b3940 20 69 74 20 64 65 61 6c 74 20 77 69 74 68 20 6f it dealt with o
b3950 6e 6c 79 0a 2a 2a 20 33 32 2d 62 69 74 20 6e 75 nly.** 32-bit nu
b3960 6d 62 65 72 73 2e 20 20 41 74 20 74 68 61 74 20 mbers. At that
b3970 74 69 6d 65 2c 20 69 74 20 77 61 73 20 6d 75 63 time, it was muc
b3980 68 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 68 h faster than th
b3990 65 0a 2a 2a 20 61 74 6f 69 28 29 20 6c 69 62 72 e.** atoi() libr
b39a0 61 72 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 52 ary routine in R
b39b0 65 64 48 61 74 20 37 2e 32 2e 0a 2a 2f 0a 53 51 edHat 7.2..*/.SQ
b39c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b39d0 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 63 sqlite3Atoi64(c
b39e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c onst char *zNum,
b39f0 20 69 36 34 20 2a 70 4e 75 6d 29 7b 0a 20 20 69 i64 *pNum){. i
b3a00 36 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 64 v = 0;. int
b3a10 6e 65 67 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b neg;. int i, c;
b3a20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
b3a30 53 74 61 72 74 3b 0a 20 20 77 68 69 6c 65 28 20 Start;. while(
b3a40 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a sqlite3Isspace(*
b3a50 7a 4e 75 6d 29 20 29 20 7a 4e 75 6d 2b 2b 3b 0a zNum) ) zNum++;.
b3a60 20 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 if( *zNum=='-'
b3a70 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b ){. neg = 1;
b3a80 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d . zNum++;. }
b3a90 65 6c 73 65 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d else if( *zNum==
b3aa0 27 2b 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d '+' ){. neg =
b3ab0 20 30 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 0;. zNum++;.
b3ac0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67 }else{. neg
b3ad0 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a 53 74 61 = 0;. }. zSta
b3ae0 72 74 20 3d 20 7a 4e 75 6d 3b 0a 20 20 77 68 69 rt = zNum;. whi
b3af0 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 le( zNum[0]=='0'
b3b00 20 29 7b 20 7a 4e 75 6d 2b 2b 3b 20 7d 20 2f 2a ){ zNum++; } /*
b3b10 20 53 6b 69 70 20 6f 76 65 72 20 6c 65 61 64 69 Skip over leadi
b3b20 6e 67 20 7a 65 72 6f 73 2e 20 54 69 63 6b 65 74 ng zeros. Ticket
b3b30 20 23 32 34 35 34 20 2a 2f 0a 20 20 66 6f 72 28 #2454 */. for(
b3b40 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b 69 5d 29 i=0; (c=zNum[i])
b3b50 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 3b >='0' && c<='9';
b3b60 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 76 i++){. v = v
b3b70 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 *10 + c - '0';.
b3b80 20 7d 0a 20 20 2a 70 4e 75 6d 20 3d 20 6e 65 67 }. *pNum = neg
b3b90 20 3f 20 2d 76 20 3a 20 76 3b 0a 20 20 69 66 28 ? -v : v;. if(
b3ba0 20 63 21 3d 30 20 7c 7c 20 28 69 3d 3d 30 20 26 c!=0 || (i==0 &
b3bb0 26 20 7a 53 74 61 72 74 3d 3d 7a 4e 75 6d 29 20 & zStart==zNum)
b3bc0 7c 7c 20 69 3e 31 39 20 29 7b 0a 20 20 20 20 2f || i>19 ){. /
b3bd0 2a 20 7a 4e 75 6d 20 69 73 20 65 6d 70 74 79 20 * zNum is empty
b3be0 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 6e 2d or contains non-
b3bf0 6e 75 6d 65 72 69 63 20 74 65 78 74 20 6f 72 20 numeric text or
b3c00 69 73 20 6c 6f 6e 67 65 72 0a 20 20 20 20 2a 2a is longer. **
b3c10 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 than 19 digits
b3c20 28 74 68 75 73 20 67 75 61 72 61 6e 74 69 6e 67 (thus guaranting
b3c30 20 74 68 61 74 20 69 74 20 69 73 20 74 6f 6f 20 that it is too
b3c40 6c 61 72 67 65 29 20 2a 2f 0a 20 20 20 20 72 65 large) */. re
b3c50 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 turn 0;. }else
b3c60 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20 20 20 if( i<19 ){.
b3c70 2f 2a 20 4c 65 73 73 20 74 68 61 6e 20 31 39 20 /* Less than 19
b3c80 64 69 67 69 74 73 2c 20 73 6f 20 77 65 20 6b 6e digits, so we kn
b3c90 6f 77 20 74 68 61 74 20 69 74 20 66 69 74 73 20 ow that it fits
b3ca0 69 6e 20 36 34 20 62 69 74 73 20 2a 2f 0a 20 20 in 64 bits */.
b3cb0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 return 1;. }e
b3cc0 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 31 39 2d 64 lse{. /* 19-d
b3cd0 69 67 69 74 20 6e 75 6d 62 65 72 73 20 6d 75 73 igit numbers mus
b3ce0 74 20 62 65 20 6e 6f 20 6c 61 72 67 65 72 20 74 t be no larger t
b3cf0 68 61 6e 20 39 32 32 33 33 37 32 30 33 36 38 35 han 922337203685
b3d00 34 37 37 35 38 30 37 20 69 66 20 70 6f 73 69 74 4775807 if posit
b3d10 69 76 65 0a 20 20 20 20 2a 2a 20 6f 72 20 39 32 ive. ** or 92
b3d20 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 2337203685477580
b3d30 38 20 69 66 20 6e 65 67 61 74 69 76 65 2e 20 20 8 if negative.
b3d40 4e 6f 74 65 20 74 68 61 74 20 39 32 32 33 33 37 Note that 922337
b3d50 32 30 33 36 38 35 34 36 36 35 38 30 38 0a 20 20 2036854665808.
b3d60 20 20 2a 2a 20 69 73 20 32 5e 36 33 2e 20 2a 2f ** is 2^63. */
b3d70 0a 20 20 20 20 72 65 74 75 72 6e 20 63 6f 6d 70 . return comp
b3d80 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d 29 3c are2pow63(zNum)<
b3d90 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a neg;. }.}../*.*
b3da0 2a 20 54 68 65 20 73 74 72 69 6e 67 20 7a 4e 75 * The string zNu
b3db0 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 m represents an
b3dc0 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 unsigned integer
b3dd0 2e 20 20 54 68 65 20 7a 4e 75 6d 20 73 74 72 69 . The zNum stri
b3de0 6e 67 0a 2a 2a 20 63 6f 6e 73 69 73 74 73 20 6f ng.** consists o
b3df0 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 64 69 f one or more di
b3e00 67 69 74 20 63 68 61 72 61 63 74 65 72 73 20 61 git characters a
b3e10 6e 64 20 69 73 20 74 65 72 6d 69 6e 61 74 65 64 nd is terminated
b3e20 20 62 79 0a 2a 2a 20 61 20 7a 65 72 6f 20 63 68 by.** a zero ch
b3e30 61 72 61 63 74 65 72 2e 20 20 41 6e 79 20 73 74 aracter. Any st
b3e40 72 61 79 20 63 68 61 72 61 63 74 65 72 73 20 69 ray characters i
b3e50 6e 20 7a 4e 75 6d 20 72 65 73 75 6c 74 20 69 6e n zNum result in
b3e60 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 62 65 undefined.** be
b3e70 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 havior..**.** If
b3e80 20 74 68 65 20 75 6e 73 69 67 6e 65 64 20 69 6e the unsigned in
b3e90 74 65 67 65 72 20 74 68 61 74 20 7a 4e 75 6d 20 teger that zNum
b3ea0 72 65 70 72 65 73 65 6e 74 73 20 77 69 6c 6c 20 represents will
b3eb0 66 69 74 20 69 6e 20 61 0a 2a 2a 20 36 34 2d 62 fit in a.** 64-b
b3ec0 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 it signed intege
b3ed0 72 2c 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 r, return TRUE.
b3ee0 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 Otherwise retur
b3ef0 6e 20 46 41 4c 53 45 2e 0a 2a 2a 0a 2a 2a 20 49 n FALSE..**.** I
b3f00 66 20 74 68 65 20 6e 65 67 46 6c 61 67 20 70 61 f the negFlag pa
b3f10 72 61 6d 65 74 65 72 20 69 73 20 74 72 75 65 2c rameter is true,
b3f20 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 that means that
b3f30 20 7a 4e 75 6d 20 72 65 61 6c 6c 79 20 72 65 70 zNum really rep
b3f40 72 65 73 65 6e 74 73 0a 2a 2a 20 61 20 6e 65 67 resents.** a neg
b3f50 61 74 69 76 65 20 6e 75 6d 62 65 72 2e 20 20 28 ative number. (
b3f60 54 68 65 20 6c 65 61 64 69 6e 67 20 22 2d 22 20 The leading "-"
b3f70 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 is omitted from
b3f80 7a 4e 75 6d 2e 29 20 20 54 68 69 73 0a 2a 2a 20 zNum.) This.**
b3f90 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 65 65 parameter is nee
b3fa0 64 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 ded to determine
b3fb0 20 61 20 62 6f 75 6e 64 61 72 79 20 63 61 73 65 a boundary case
b3fc0 2e 20 20 41 20 73 74 72 69 6e 67 0a 2a 2a 20 6f . A string.** o
b3fd0 66 20 22 39 32 32 33 33 37 33 30 33 36 38 35 34 f "9223373036854
b3fe0 37 37 35 38 30 38 22 20 72 65 74 75 72 6e 73 20 775808" returns
b3ff0 66 61 6c 73 65 20 69 66 20 6e 65 67 46 6c 61 67 false if negFlag
b4000 20 69 73 20 66 61 6c 73 65 20 6f 72 20 74 72 75 is false or tru
b4010 65 0a 2a 2a 20 69 66 20 6e 65 67 46 6c 61 67 20 e.** if negFlag
b4020 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 4c is true..**.** L
b4030 65 61 64 69 6e 67 20 7a 65 72 6f 73 20 61 72 65 eading zeros are
b4040 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c ignored..*/.SQL
b4050 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b4060 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 sqlite3FitsIn64B
b4070 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a its(const char *
b4080 7a 4e 75 6d 2c 20 69 6e 74 20 6e 65 67 46 6c 61 zNum, int negFla
b4090 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 g){. int i;. i
b40a0 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 0a 20 20 61 nt neg = 0;.. a
b40b0 73 73 65 72 74 28 20 7a 4e 75 6d 5b 30 5d 3e 3d ssert( zNum[0]>=
b40c0 27 30 27 20 26 26 20 7a 4e 75 6d 5b 30 5d 3c 3d '0' && zNum[0]<=
b40d0 27 39 27 20 29 3b 20 2f 2a 20 7a 4e 75 6d 20 69 '9' ); /* zNum i
b40e0 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 6e 75 s an unsigned nu
b40f0 6d 62 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20 6e mber */.. if( n
b4100 65 67 46 6c 61 67 20 29 20 6e 65 67 20 3d 20 31 egFlag ) neg = 1
b4110 2d 6e 65 67 3b 0a 20 20 77 68 69 6c 65 28 20 2a -neg;. while( *
b4120 7a 4e 75 6d 3d 3d 27 30 27 20 29 7b 0a 20 20 20 zNum=='0' ){.
b4130 20 7a 4e 75 6d 2b 2b 3b 20 20 20 2f 2a 20 53 6b zNum++; /* Sk
b4140 69 70 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 ip leading zeros
b4150 2e 20 20 54 69 63 6b 65 74 20 23 32 34 35 34 20 . Ticket #2454
b4160 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 */. }. for(i=0
b4170 3b 20 7a 4e 75 6d 5b 69 5d 3b 20 69 2b 2b 29 7b ; zNum[i]; i++){
b4180 20 61 73 73 65 72 74 28 20 7a 4e 75 6d 5b 69 5d assert( zNum[i]
b4190 3e 3d 27 30 27 20 26 26 20 7a 4e 75 6d 5b 69 5d >='0' && zNum[i]
b41a0 3c 3d 27 39 27 20 29 3b 20 7d 0a 20 20 69 66 28 <='9' ); }. if(
b41b0 20 69 3c 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 i<19 ){. /*
b41c0 47 75 61 72 61 6e 74 65 65 64 20 74 6f 20 66 69 Guaranteed to fi
b41d0 74 20 69 66 20 6c 65 73 73 20 74 68 61 6e 20 31 t if less than 1
b41e0 39 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 20 20 9 digits */.
b41f0 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 return 1;. }els
b4200 65 20 69 66 28 20 69 3e 31 39 20 29 7b 0a 20 20 e if( i>19 ){.
b4210 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65 64 20 /* Guaranteed
b4220 74 6f 20 62 65 20 74 6f 6f 20 62 69 67 20 69 66 to be too big if
b4230 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 39 greater than 19
b4240 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 20 20 72 digits */. r
b4250 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 eturn 0;. }else
b4260 7b 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 61 72 65 {. /* Compare
b4270 20 61 67 61 69 6e 73 74 20 32 5e 36 33 2e 20 2a against 2^63. *
b4280 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 63 6f 6d /. return com
b4290 70 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d 29 pare2pow63(zNum)
b42a0 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a <neg;. }.}../*.
b42b0 2a 2a 20 49 66 20 7a 4e 75 6d 20 72 65 70 72 65 ** If zNum repre
b42c0 73 65 6e 74 73 20 61 6e 20 69 6e 74 65 67 65 72 sents an integer
b42d0 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74 20 69 that will fit i
b42e0 6e 20 33 32 2d 62 69 74 73 2c 20 74 68 65 6e 20 n 32-bits, then
b42f0 73 65 74 0a 2a 2a 20 2a 70 56 61 6c 75 65 20 74 set.** *pValue t
b4300 6f 20 74 68 61 74 20 69 6e 74 65 67 65 72 20 61 o that integer a
b4310 6e 64 20 72 65 74 75 72 6e 20 74 72 75 65 2e 20 nd return true.
b4320 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 Otherwise retur
b4330 6e 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 41 n false..**.** A
b4340 6e 79 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 63 ny non-numeric c
b4350 68 61 72 61 63 74 65 72 73 20 74 68 61 74 20 66 haracters that f
b4360 6f 6c 6c 6f 77 69 6e 67 20 7a 4e 75 6d 20 61 72 ollowing zNum ar
b4370 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 54 68 e ignored..** Th
b4380 69 73 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 is is different
b4390 66 72 6f 6d 20 73 71 6c 69 74 65 33 41 74 6f 69 from sqlite3Atoi
b43a0 36 34 28 29 20 77 68 69 63 68 20 72 65 71 75 69 64() which requi
b43b0 72 65 73 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 res the.** input
b43c0 20 6e 75 6d 62 65 72 20 74 6f 20 62 65 20 7a 65 number to be ze
b43d0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a ro-terminated..*
b43e0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b43f0 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 49 int sqlite3GetI
b4400 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20 nt32(const char
b4410 2a 7a 4e 75 6d 2c 20 69 6e 74 20 2a 70 56 61 6c *zNum, int *pVal
b4420 75 65 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e ue){. sqlite_in
b4430 74 36 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e 74 t64 v = 0;. int
b4440 20 69 2c 20 63 3b 0a 20 20 69 6e 74 20 6e 65 67 i, c;. int neg
b4450 20 3d 20 30 3b 0a 20 20 69 66 28 20 7a 4e 75 6d = 0;. if( zNum
b4460 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 [0]=='-' ){.
b4470 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 neg = 1;. zNu
b4480 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 m++;. }else if(
b4490 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2b 27 20 29 7b zNum[0]=='+' ){
b44a0 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d . zNum++;. }
b44b0 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 . while( zNum[0
b44c0 5d 3d 3d 27 30 27 20 29 20 7a 4e 75 6d 2b 2b 3b ]=='0' ) zNum++;
b44d0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 31 . for(i=0; i<11
b44e0 20 26 26 20 28 63 20 3d 20 7a 4e 75 6d 5b 69 5d && (c = zNum[i]
b44f0 20 2d 20 27 30 27 29 3e 3d 30 20 26 26 20 63 3c - '0')>=0 && c<
b4500 3d 39 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 =9; i++){. v
b4510 3d 20 76 2a 31 30 20 2b 20 63 3b 0a 20 20 7d 0a = v*10 + c;. }.
b4520 0a 20 20 2f 2a 20 54 68 65 20 6c 6f 6e 67 65 73 . /* The longes
b4530 74 20 64 65 63 69 6d 61 6c 20 72 65 70 72 65 73 t decimal repres
b4540 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 33 32 entation of a 32
b4550 20 62 69 74 20 69 6e 74 65 67 65 72 20 69 73 20 bit integer is
b4560 31 30 20 64 69 67 69 74 73 3a 0a 20 20 2a 2a 0a 10 digits:. **.
b4570 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 **
b4580 20 31 32 33 34 35 36 37 38 39 30 0a 20 20 2a 2a 1234567890. **
b4590 20 20 20 20 20 32 5e 33 31 20 2d 3e 20 32 31 34 2^31 -> 214
b45a0 37 34 38 33 36 34 38 0a 20 20 2a 2f 0a 20 20 69 7483648. */. i
b45b0 66 28 20 69 3e 31 30 20 29 7b 0a 20 20 20 20 72 f( i>10 ){. r
b45c0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 eturn 0;. }. i
b45d0 66 28 20 76 2d 6e 65 67 3e 32 31 34 37 34 38 33 f( v-neg>2147483
b45e0 36 34 37 20 29 7b 0a 20 20 20 20 72 65 74 75 72 647 ){. retur
b45f0 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e n 0;. }. if( n
b4600 65 67 20 29 7b 0a 20 20 20 20 76 20 3d 20 2d 76 eg ){. v = -v
b4610 3b 0a 20 20 7d 0a 20 20 2a 70 56 61 6c 75 65 20 ;. }. *pValue
b4620 3d 20 28 69 6e 74 29 76 3b 0a 20 20 72 65 74 75 = (int)v;. retu
b4630 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 rn 1;.}../*.** T
b4640 68 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 he variable-leng
b4650 74 68 20 69 6e 74 65 67 65 72 20 65 6e 63 6f 64 th integer encod
b4660 69 6e 67 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 ing is as follow
b4670 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a s:.**.** KEY:.**
b4680 20 20 20 20 20 20 20 20 20 41 20 3d 20 30 78 78 A = 0xx
b4690 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73 20 xxxxx 7 bits
b46a0 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 of data and one
b46b0 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 20 flag bit.**
b46c0 20 20 20 20 42 20 3d 20 31 78 78 78 78 78 78 78 B = 1xxxxxxx
b46d0 20 20 20 20 37 20 62 69 74 73 20 6f 66 20 64 61 7 bits of da
b46e0 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 ta and one flag
b46f0 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 43 bit.** C
b4700 20 3d 20 78 78 78 78 78 78 78 78 20 20 20 20 38 = xxxxxxxx 8
b4710 20 62 69 74 73 20 6f 66 20 64 61 74 61 0a 2a 2a bits of data.**
b4720 0a 2a 2a 20 20 37 20 62 69 74 73 20 2d 20 41 0a .** 7 bits - A.
b4730 2a 2a 20 31 34 20 62 69 74 73 20 2d 20 42 41 0a ** 14 bits - BA.
b4740 2a 2a 20 32 31 20 62 69 74 73 20 2d 20 42 42 41 ** 21 bits - BBA
b4750 0a 2a 2a 20 32 38 20 62 69 74 73 20 2d 20 42 42 .** 28 bits - BB
b4760 42 41 0a 2a 2a 20 33 35 20 62 69 74 73 20 2d 20 BA.** 35 bits -
b4770 42 42 42 42 41 0a 2a 2a 20 34 32 20 62 69 74 73 BBBBA.** 42 bits
b4780 20 2d 20 42 42 42 42 42 41 0a 2a 2a 20 34 39 20 - BBBBBA.** 49
b4790 62 69 74 73 20 2d 20 42 42 42 42 42 42 41 0a 2a bits - BBBBBBA.*
b47a0 2a 20 35 36 20 62 69 74 73 20 2d 20 42 42 42 42 * 56 bits - BBBB
b47b0 42 42 42 41 0a 2a 2a 20 36 34 20 62 69 74 73 20 BBBA.** 64 bits
b47c0 2d 20 42 42 42 42 42 42 42 42 43 0a 2a 2f 0a 0a - BBBBBBBBC.*/..
b47d0 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 36 34 /*.** Write a 64
b47e0 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 -bit variable-le
b47f0 6e 67 74 68 20 69 6e 74 65 67 65 72 20 74 6f 20 ngth integer to
b4800 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 memory starting
b4810 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 54 68 65 20 at p[0]..** The
b4820 6c 65 6e 67 74 68 20 6f 66 20 64 61 74 61 20 77 length of data w
b4830 72 69 74 65 20 77 69 6c 6c 20 62 65 20 62 65 74 rite will be bet
b4840 77 65 65 6e 20 31 20 61 6e 64 20 39 20 62 79 74 ween 1 and 9 byt
b4850 65 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a es. The number.
b4860 2a 2a 20 6f 66 20 62 79 74 65 73 20 77 72 69 74 ** of bytes writ
b4870 74 65 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e ten is returned.
b4880 0a 2a 2a 0a 2a 2a 20 41 20 76 61 72 69 61 62 6c .**.** A variabl
b4890 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e-length integer
b48a0 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 consists of the
b48b0 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20 6f 66 lower 7 bits of
b48c0 20 65 61 63 68 20 62 79 74 65 0a 2a 2a 20 66 6f each byte.** fo
b48d0 72 20 61 6c 6c 20 62 79 74 65 73 20 74 68 61 74 r all bytes that
b48e0 20 68 61 76 65 20 74 68 65 20 38 74 68 20 62 69 have the 8th bi
b48f0 74 20 73 65 74 20 61 6e 64 20 6f 6e 65 20 62 79 t set and one by
b4900 74 65 20 77 69 74 68 20 74 68 65 20 38 74 68 0a te with the 8th.
b4910 2a 2a 20 62 69 74 20 63 6c 65 61 72 2e 20 20 45 ** bit clear. E
b4920 78 63 65 70 74 2c 20 69 66 20 77 65 20 67 65 74 xcept, if we get
b4930 20 74 6f 20 74 68 65 20 39 74 68 20 62 79 74 65 to the 9th byte
b4940 2c 20 69 74 20 73 74 6f 72 65 73 20 74 68 65 20 , it stores the
b4950 66 75 6c 6c 0a 2a 2a 20 38 20 62 69 74 73 20 61 full.** 8 bits a
b4960 6e 64 20 69 73 20 74 68 65 20 6c 61 73 74 20 62 nd is the last b
b4970 79 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 yte..*/.SQLITE_P
b4980 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b4990 65 33 50 75 74 56 61 72 69 6e 74 28 75 6e 73 69 e3PutVarint(unsi
b49a0 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 36 gned char *p, u6
b49b0 34 20 76 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 4 v){. int i, j
b49c0 2c 20 6e 3b 0a 20 20 75 38 20 62 75 66 5b 31 30 , n;. u8 buf[10
b49d0 5d 3b 0a 20 20 69 66 28 20 76 20 26 20 28 28 28 ];. if( v & (((
b49e0 75 36 34 29 30 78 66 66 30 30 30 30 30 30 29 3c u64)0xff000000)<
b49f0 3c 33 32 29 20 29 7b 0a 20 20 20 20 70 5b 38 5d <32) ){. p[8]
b4a00 20 3d 20 28 75 38 29 76 3b 0a 20 20 20 20 76 20 = (u8)v;. v
b4a10 3e 3e 3d 20 38 3b 0a 20 20 20 20 66 6f 72 28 69 >>= 8;. for(i
b4a20 3d 37 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a =7; i>=0; i--){.
b4a30 20 20 20 20 20 20 70 5b 69 5d 20 3d 20 28 75 38 p[i] = (u8
b4a40 29 28 28 76 20 26 20 30 78 37 66 29 20 7c 20 30 )((v & 0x7f) | 0
b4a50 78 38 30 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e x80);. v >>
b4a60 3d 20 37 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 = 7;. }. r
b4a70 65 74 75 72 6e 20 39 3b 0a 20 20 7d 20 20 20 20 eturn 9;. }
b4a80 0a 20 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a . n = 0;. do{.
b4a90 20 20 20 20 62 75 66 5b 6e 2b 2b 5d 20 3d 20 28 buf[n++] = (
b4aa0 75 38 29 28 28 76 20 26 20 30 78 37 66 29 20 7c u8)((v & 0x7f) |
b4ab0 20 30 78 38 30 29 3b 0a 20 20 20 20 76 20 3e 3e 0x80);. v >>
b4ac0 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 = 7;. }while( v
b4ad0 21 3d 30 20 29 3b 0a 20 20 62 75 66 5b 30 5d 20 !=0 );. buf[0]
b4ae0 26 3d 20 30 78 37 66 3b 0a 20 20 61 73 73 65 72 &= 0x7f;. asser
b4af0 74 28 20 6e 3c 3d 39 20 29 3b 0a 20 20 66 6f 72 t( n<=9 );. for
b4b00 28 69 3d 30 2c 20 6a 3d 6e 2d 31 3b 20 6a 3e 3d (i=0, j=n-1; j>=
b4b10 30 3b 20 6a 2d 2d 2c 20 69 2b 2b 29 7b 0a 20 20 0; j--, i++){.
b4b20 20 20 70 5b 69 5d 20 3d 20 62 75 66 5b 6a 5d 3b p[i] = buf[j];
b4b30 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b . }. return n;
b4b40 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
b4b50 6f 75 74 69 6e 65 20 69 73 20 61 20 66 61 73 74 outine is a fast
b4b60 65 72 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 71 er version of sq
b4b70 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 29 lite3PutVarint()
b4b80 20 74 68 61 74 20 6f 6e 6c 79 0a 2a 2a 20 77 6f that only.** wo
b4b90 72 6b 73 20 66 6f 72 20 33 32 2d 62 69 74 20 70 rks for 32-bit p
b4ba0 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 73 ositive integers
b4bb0 20 61 6e 64 20 77 68 69 63 68 20 69 73 20 6f 70 and which is op
b4bc0 74 69 6d 69 7a 65 64 20 66 6f 72 0a 2a 2a 20 74 timized for.** t
b4bd0 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 6f he common case o
b4be0 66 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73 f small integers
b4bf0 2e 20 20 41 20 4d 41 43 52 4f 20 76 65 72 73 69 . A MACRO versi
b4c00 6f 6e 2c 20 70 75 74 56 61 72 69 6e 74 33 32 2c on, putVarint32,
b4c10 0a 2a 2a 20 69 73 20 70 72 6f 76 69 64 65 64 20 .** is provided
b4c20 77 68 69 63 68 20 69 6e 6c 69 6e 65 73 20 74 68 which inlines th
b4c30 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 e single-byte ca
b4c40 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73 68 se. All code sh
b4c50 6f 75 6c 64 20 75 73 65 0a 2a 2a 20 74 68 65 20 ould use.** the
b4c60 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 MACRO version as
b4c70 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 this function a
b4c80 73 73 75 6d 65 73 20 74 68 65 20 73 69 6e 67 6c ssumes the singl
b4c90 65 2d 62 79 74 65 20 63 61 73 65 20 68 61 73 0a e-byte case has.
b4ca0 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ** already been
b4cb0 68 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 handled..*/.SQLI
b4cc0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
b4cd0 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 qlite3PutVarint3
b4ce0 32 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2(unsigned char
b4cf0 2a 70 2c 20 75 33 32 20 76 29 7b 0a 23 69 66 6e *p, u32 v){.#ifn
b4d00 64 65 66 20 70 75 74 56 61 72 69 6e 74 33 32 0a def putVarint32.
b4d10 20 20 69 66 28 20 28 76 20 26 20 7e 30 78 37 66 if( (v & ~0x7f
b4d20 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 5b 30 5d )==0 ){. p[0]
b4d30 20 3d 20 76 3b 0a 20 20 20 20 72 65 74 75 72 6e = v;. return
b4d40 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 1;. }.#endif.
b4d50 20 69 66 28 20 28 76 20 26 20 7e 30 78 33 66 66 if( (v & ~0x3ff
b4d60 66 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 5b 30 f)==0 ){. p[0
b4d70 5d 20 3d 20 28 75 38 29 28 28 76 3e 3e 37 29 20 ] = (u8)((v>>7)
b4d80 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 70 5b 31 | 0x80);. p[1
b4d90 5d 20 3d 20 28 75 38 29 28 76 20 26 20 30 78 37 ] = (u8)(v & 0x7
b4da0 66 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 32 f);. return 2
b4db0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 ;. }. return s
b4dc0 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 qlite3PutVarint(
b4dd0 70 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 p, v);.}../*.**
b4de0 52 65 61 64 20 61 20 36 34 2d 62 69 74 20 76 61 Read a 64-bit va
b4df0 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e riable-length in
b4e00 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 teger from memor
b4e10 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b y starting at p[
b4e20 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 0]..** Return th
b4e30 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
b4e40 73 20 72 65 61 64 2e 20 20 54 68 65 20 76 61 6c s read. The val
b4e50 75 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 ue is stored in
b4e60 2a 76 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 *v..*/.SQLITE_PR
b4e70 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 IVATE u8 sqlite3
b4e80 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 74 20 GetVarint(const
b4e90 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
b4ea0 2c 20 75 36 34 20 2a 76 29 7b 0a 20 20 75 33 32 , u64 *v){. u32
b4eb0 20 61 2c 62 2c 73 3b 0a 0a 20 20 61 20 3d 20 2a a,b,s;.. a = *
b4ec0 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 20 28 75 p;. /* a: p0 (u
b4ed0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 nmasked) */. if
b4ee0 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b (!(a&0x80)). {
b4ef0 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 . *v = a;.
b4f00 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a return 1;. }..
b4f10 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b p++;. b = *p;
b4f20 0a 20 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d . /* b: p1 (unm
b4f30 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 asked) */. if (
b4f40 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 !(b&0x80)). {.
b4f50 20 20 20 61 20 26 3d 20 30 78 37 66 3b 0a 20 20 a &= 0x7f;.
b4f60 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 a = a<<7;.
b4f70 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d a |= b;. *v =
b4f80 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 32 a;. return 2
b4f90 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 ;. }.. p++;.
b4fa0 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c a = a<<14;. a |
b4fb0 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 = *p;. /* a: p0
b4fc0 3c 3c 31 34 20 7c 20 70 32 20 28 75 6e 6d 61 73 <<14 | p2 (unmas
b4fd0 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 ked) */. if (!(
b4fe0 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 a&0x80)). {.
b4ff0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 a &= (0x7f<<14)
b5000 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26 |(0x7f);. b &
b5010 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20 3d 20 = 0x7f;. b =
b5020 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 b<<7;. a |= b
b5030 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 ;. *v = a;.
b5040 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a return 3;. }.
b5050 0a 20 20 2f 2a 20 43 53 45 31 20 66 72 6f 6d 20 . /* CSE1 from
b5060 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26 3d 20 below */. a &=
b5070 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
b5080 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 );. p++;. b =
b5090 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 b<<14;. b |= *p
b50a0 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 ;. /* b: p1<<14
b50b0 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 | p3 (unmasked)
b50c0 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 */. if (!(b&0x
b50d0 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62 20 26 80)). {. b &
b50e0 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 = (0x7f<<14)|(0x
b50f0 37 66 29 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 7f);. /* move
b5100 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20 20 20 d CSE1 up */.
b5110 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c /* a &= (0x7f<<
b5120 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 14)|(0x7f); */.
b5130 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 a = a<<7;.
b5140 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 a |= b;. *v
b5150 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = a;. return
b5160 34 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 3a 20 4;. }.. /* a:
b5170 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61 73 p0<<14 | p2 (mas
b5180 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 62 3a 20 ked) */. /* b:
b5190 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d p1<<14 | p3 (unm
b51a0 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 31 asked) */. /* 1
b51b0 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 32 31 :save off p0<<21
b51c0 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c | p1<<14 | p2<<
b51d0 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 20 7 | p3 (masked)
b51e0 2a 2f 0a 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 */. /* moved CS
b51f0 45 31 20 75 70 20 2a 2f 0a 20 20 2f 2a 20 61 20 E1 up */. /* a
b5200 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 &= (0x7f<<14)|(0
b5210 78 37 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 x7f); */. b &=
b5220 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
b5230 29 3b 0a 20 20 73 20 3d 20 61 3b 0a 20 20 2f 2a );. s = a;. /*
b5240 20 73 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 s: p0<<14 | p2
b5250 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 (masked) */.. p
b5260 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b ++;. a = a<<14;
b5270 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a . a |= *p;. /*
b5280 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c a: p0<<28 | p2<
b5290 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b <14 | p4 (unmask
b52a0 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 ed) */. if (!(a
b52b0 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 &0x80)). {.
b52c0 2f 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 20 74 /* we can skip t
b52d0 68 65 73 65 20 63 61 75 73 65 20 74 68 65 79 20 hese cause they
b52e0 77 65 72 65 20 28 65 66 66 65 63 74 69 76 65 6c were (effectivel
b52f0 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20 69 6e y) done above in
b5300 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f 0a 20 calc'ing s */.
b5310 20 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 /* a &= (0x7f
b5320 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 <<28)|(0x7f<<14)
b5330 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 |(0x7f); */.
b5340 2f 2a 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 /* b &= (0x7f<<1
b5350 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 4)|(0x7f); */.
b5360 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 b = b<<7;.
b5370 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 a |= b;. s =
b5380 73 3e 3e 31 38 3b 0a 20 20 20 20 2a 76 20 3d 20 s>>18;. *v =
b5390 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 ((u64)s)<<32 | a
b53a0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 35 3b 0a ;. return 5;.
b53b0 20 20 7d 0a 0a 20 20 2f 2a 20 32 3a 73 61 76 65 }.. /* 2:save
b53c0 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20 70 31 off p0<<21 | p1
b53d0 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 <<14 | p2<<7 | p
b53e0 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 3 (masked) */.
b53f0 73 20 3d 20 73 3c 3c 37 3b 0a 20 20 73 20 7c 3d s = s<<7;. s |=
b5400 20 62 3b 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c b;. /* s: p0<<
b5410 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 21 | p1<<14 | p2
b5420 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 <<7 | p3 (masked
b5430 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 ) */.. p++;. b
b5440 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d = b<<14;. b |=
b5450 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c *p;. /* b: p1<
b5460 3c 32 38 20 7c 20 70 33 3c 3c 31 34 20 7c 20 70 <28 | p3<<14 | p
b5470 35 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 5 (unmasked) */.
b5480 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 if (!(b&0x80))
b5490 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63 . {. /* we c
b54a0 61 6e 20 73 6b 69 70 20 74 68 69 73 20 63 61 75 an skip this cau
b54b0 73 65 20 69 74 20 77 61 73 20 28 65 66 66 65 63 se it was (effec
b54c0 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 61 62 6f tively) done abo
b54d0 76 65 20 69 6e 20 63 61 6c 63 27 69 6e 67 20 73 ve in calc'ing s
b54e0 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 */. /* b &=
b54f0 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 (0x7f<<28)|(0x7f
b5500 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f <<14)|(0x7f); */
b5510 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c . a &= (0x7f<
b5520 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 <14)|(0x7f);.
b5530 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 a = a<<7;. a
b5540 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 |= b;. s = s
b5550 3e 3e 31 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28 >>18;. *v = (
b5560 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b (u64)s)<<32 | a;
b5570 0a 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 . return 6;.
b5580 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d }.. p++;. a =
b5590 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a a<<14;. a |= *
b55a0 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 32 3c 3c 32 p;. /* a: p2<<2
b55b0 38 20 7c 20 70 34 3c 3c 31 34 20 7c 20 70 36 20 8 | p4<<14 | p6
b55c0 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 (unmasked) */.
b55d0 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 if (!(a&0x80)).
b55e0 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 31 {. a &= (0x1
b55f0 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 f<<28)|(0x7f<<14
b5600 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 )|(0x7f);. b
b5610 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 &= (0x7f<<14)|(0
b5620 78 37 66 29 3b 0a 20 20 20 20 62 20 3d 20 62 3c x7f);. b = b<
b5630 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a <7;. a |= b;.
b5640 20 20 20 20 73 20 3d 20 73 3e 3e 31 31 3b 0a 20 s = s>>11;.
b5650 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 *v = ((u64)s)
b5660 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 <<32 | a;. re
b5670 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 0a 20 20 2f turn 7;. }.. /
b5680 2a 20 43 53 45 32 20 66 72 6f 6d 20 62 65 6c 6f * CSE2 from belo
b5690 77 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30 78 37 w */. a &= (0x7
b56a0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 f<<14)|(0x7f);.
b56b0 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 p++;. b = b<<1
b56c0 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 4;. b |= *p;.
b56d0 2f 2a 20 62 3a 20 70 33 3c 3c 32 38 20 7c 20 70 /* b: p3<<28 | p
b56e0 35 3c 3c 31 34 20 7c 20 70 37 20 28 75 6e 6d 61 5<<14 | p7 (unma
b56f0 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 sked) */. if (!
b5700 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 (b&0x80)). {.
b5710 20 20 62 20 26 3d 20 28 30 78 31 66 3c 3c 32 38 b &= (0x1f<<28
b5720 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 )|(0x7f<<14)|(0x
b5730 37 66 29 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 7f);. /* move
b5740 64 20 43 53 45 32 20 75 70 20 2a 2f 0a 20 20 20 d CSE2 up */.
b5750 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c /* a &= (0x7f<<
b5760 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 14)|(0x7f); */.
b5770 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 a = a<<7;.
b5780 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d a |= b;. s =
b5790 20 73 3e 3e 34 3b 0a 20 20 20 20 2a 76 20 3d 20 s>>4;. *v =
b57a0 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 ((u64)s)<<32 | a
b57b0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 38 3b 0a ;. return 8;.
b57c0 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 }.. p++;. a
b57d0 3d 20 61 3c 3c 31 35 3b 0a 20 20 61 20 7c 3d 20 = a<<15;. a |=
b57e0 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 34 3c 3c *p;. /* a: p4<<
b57f0 32 39 20 7c 20 70 36 3c 3c 31 35 20 7c 20 70 38 29 | p6<<15 | p8
b5800 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a (unmasked) */..
b5810 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 32 20 /* moved CSE2
b5820 75 70 20 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 up */. /* a &=
b5830 28 30 78 37 66 3c 3c 32 39 29 7c 28 30 78 37 66 (0x7f<<29)|(0x7f
b5840 3c 3c 31 35 29 7c 28 30 78 66 66 29 3b 20 2a 2f <<15)|(0xff); */
b5850 0a 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 . b &= (0x7f<<1
b5860 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 62 20 3d 4)|(0x7f);. b =
b5870 20 62 3c 3c 38 3b 0a 20 20 61 20 7c 3d 20 62 3b b<<8;. a |= b;
b5880 0a 0a 20 20 73 20 3d 20 73 3c 3c 34 3b 0a 20 20 .. s = s<<4;.
b5890 62 20 3d 20 70 5b 2d 34 5d 3b 0a 20 20 62 20 26 b = p[-4];. b &
b58a0 3d 20 30 78 37 66 3b 0a 20 20 62 20 3d 20 62 3e = 0x7f;. b = b>
b58b0 3e 33 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a 0a 20 >3;. s |= b;..
b58c0 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c *v = ((u64)s)<<
b58d0 33 32 20 7c 20 61 3b 0a 0a 20 20 72 65 74 75 72 32 | a;.. retur
b58e0 6e 20 39 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 n 9;.}../*.** Re
b58f0 61 64 20 61 20 33 32 2d 62 69 74 20 76 61 72 69 ad a 32-bit vari
b5900 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 able-length inte
b5910 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 ger from memory
b5920 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d starting at p[0]
b5930 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 ..** Return the
b5940 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
b5950 72 65 61 64 2e 20 20 54 68 65 20 76 61 6c 75 65 read. The value
b5960 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 is stored in *v
b5970 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 ..**.** If the v
b5980 61 72 69 6e 74 20 73 74 6f 72 65 64 20 69 6e 20 arint stored in
b5990 70 5b 30 5d 20 69 73 20 6c 61 72 67 65 72 20 74 p[0] is larger t
b59a0 68 61 6e 20 63 61 6e 20 66 69 74 20 69 6e 20 61 han can fit in a
b59b0 20 33 32 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 32-bit unsigned
b59c0 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 74 68 65 .** integer, the
b59d0 6e 20 73 65 74 20 2a 76 20 74 6f 20 30 78 66 66 n set *v to 0xff
b59e0 66 66 66 66 66 66 2e 0a 2a 2a 0a 2a 2a 20 41 20 ffffff..**.** A
b59f0 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20 67 MACRO version, g
b5a00 65 74 56 61 72 69 6e 74 33 32 2c 20 69 73 20 70 etVarint32, is p
b5a10 72 6f 76 69 64 65 64 20 77 68 69 63 68 20 69 6e rovided which in
b5a20 6c 69 6e 65 73 20 74 68 65 20 0a 2a 2a 20 73 69 lines the .** si
b5a30 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e 20 ngle-byte case.
b5a40 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c 64 All code should
b5a50 20 75 73 65 20 74 68 65 20 4d 41 43 52 4f 20 76 use the MACRO v
b5a60 65 72 73 69 6f 6e 20 61 73 20 0a 2a 2a 20 74 68 ersion as .** th
b5a70 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 is function assu
b5a80 6d 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 mes the single-b
b5a90 79 74 65 20 63 61 73 65 20 68 61 73 20 61 6c 72 yte case has alr
b5aa0 65 61 64 79 20 62 65 65 6e 20 68 61 6e 64 6c 65 eady been handle
b5ab0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
b5ac0 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 VATE u8 sqlite3G
b5ad0 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 etVarint32(const
b5ae0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
b5af0 70 2c 20 75 33 32 20 2a 76 29 7b 0a 20 20 75 33 p, u32 *v){. u3
b5b00 32 20 61 2c 62 3b 0a 0a 20 20 2f 2a 20 54 68 65 2 a,b;.. /* The
b5b10 20 31 2d 62 79 74 65 20 63 61 73 65 2e 20 20 4f 1-byte case. O
b5b20 76 65 72 77 68 65 6c 6d 69 6e 67 6c 79 20 74 68 verwhelmingly th
b5b30 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 2e 20 20 e most common.
b5b40 48 61 6e 64 6c 65 64 20 69 6e 6c 69 6e 65 0a 20 Handled inline.
b5b50 20 2a 2a 20 62 79 20 74 68 65 20 67 65 74 56 61 ** by the getVa
b5b60 72 69 6e 33 32 28 29 20 6d 61 63 72 6f 20 2a 2f rin32() macro */
b5b70 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 . a = *p;. /*
b5b80 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 a: p0 (unmasked)
b5b90 20 2a 2f 0a 23 69 66 6e 64 65 66 20 67 65 74 56 */.#ifndef getV
b5ba0 61 72 69 6e 74 33 32 0a 20 20 69 66 20 28 21 28 arint32. if (!(
b5bb0 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 a&0x80)). {.
b5bc0 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 65 /* Values betwe
b5bd0 65 6e 20 30 20 61 6e 64 20 31 32 37 20 2a 2f 0a en 0 and 127 */.
b5be0 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 *v = a;.
b5bf0 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 return 1;. }.#e
b5c00 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68 65 20 32 ndif.. /* The 2
b5c10 2d 62 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 20 -byte case */.
b5c20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 p++;. b = *p;.
b5c30 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 /* b: p1 (unmas
b5c40 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 ked) */. if (!(
b5c50 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 b&0x80)). {.
b5c60 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 65 /* Values betwe
b5c70 65 6e 20 31 32 38 20 61 6e 64 20 31 36 33 38 33 en 128 and 16383
b5c80 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 30 78 37 */. a &= 0x7
b5c90 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b f;. a = a<<7;
b5ca0 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b . *v = a | b;
b5cb0 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 . return 2;.
b5cc0 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 33 2d 62 }.. /* The 3-b
b5cd0 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 20 70 2b yte case */. p+
b5ce0 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a +;. a = a<<14;.
b5cf0 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 a |= *p;. /*
b5d00 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 a: p0<<14 | p2 (
b5d10 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 unmasked) */. i
b5d20 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 f (!(a&0x80)).
b5d30 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 {. /* Values
b5d40 62 65 74 77 65 65 6e 20 31 36 33 38 34 20 61 6e between 16384 an
b5d50 64 20 32 30 39 37 31 35 31 20 2a 2f 0a 20 20 20 d 2097151 */.
b5d60 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 a &= (0x7f<<14)
b5d70 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26 |(0x7f);. b &
b5d80 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20 3d 20 = 0x7f;. b =
b5d90 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 b<<7;. *v = a
b5da0 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e | b;. return
b5db0 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 20 3;. }.. /* A
b5dc0 33 32 2d 62 69 74 20 76 61 72 69 6e 74 20 69 73 32-bit varint is
b5dd0 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 73 used to store s
b5de0 69 7a 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ize information
b5df0 69 6e 20 62 74 72 65 65 73 2e 0a 20 20 2a 2a 20 in btrees.. **
b5e00 4f 62 6a 65 63 74 73 20 61 72 65 20 72 61 72 65 Objects are rare
b5e10 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 32 ly larger than 2
b5e20 4d 69 42 20 6c 69 6d 69 74 20 6f 66 20 61 20 33 MiB limit of a 3
b5e30 2d 62 79 74 65 20 76 61 72 69 6e 74 2e 0a 20 20 -byte varint..
b5e40 2a 2a 20 41 20 33 2d 62 79 74 65 20 76 61 72 69 ** A 3-byte vari
b5e50 6e 74 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 nt is sufficient
b5e60 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 , for example, t
b5e70 6f 20 72 65 63 6f 72 64 20 74 68 65 20 73 69 7a o record the siz
b5e80 65 0a 20 20 2a 2a 20 6f 66 20 61 20 31 30 34 38 e. ** of a 1048
b5e90 35 36 39 2d 62 79 74 65 20 42 4c 4f 42 20 6f 72 569-byte BLOB or
b5ea0 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2a 0a 20 20 string.. **.
b5eb0 2a 2a 20 57 65 20 6f 6e 6c 79 20 75 6e 72 6f 6c ** We only unrol
b5ec0 6c 20 74 68 65 20 66 69 72 73 74 20 31 2d 2c 20 l the first 1-,
b5ed0 32 2d 2c 20 61 6e 64 20 33 2d 20 62 79 74 65 20 2-, and 3- byte
b5ee0 63 61 73 65 73 2e 20 20 54 68 65 20 76 65 72 79 cases. The very
b5ef0 0a 20 20 2a 2a 20 72 61 72 65 20 6c 61 72 67 65 . ** rare large
b5f00 72 20 63 61 73 65 73 20 63 61 6e 20 62 65 20 68 r cases can be h
b5f10 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 73 6c andled by the sl
b5f20 6f 77 65 72 20 36 34 2d 62 69 74 20 76 61 72 69 ower 64-bit vari
b5f30 6e 74 0a 20 20 2a 2a 20 72 6f 75 74 69 6e 65 2e nt. ** routine.
b5f40 0a 20 20 2a 2f 0a 23 69 66 20 31 0a 20 20 7b 0a . */.#if 1. {.
b5f50 20 20 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20 u64 v64;.
b5f60 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d u8 n;.. p -=
b5f70 20 32 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 2;. n = sqli
b5f80 74 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20 te3GetVarint(p,
b5f90 26 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72 &v64);. asser
b5fa0 74 28 20 6e 3e 33 20 26 26 20 6e 3c 3d 39 20 29 t( n>3 && n<=9 )
b5fb0 3b 0a 20 20 20 20 69 66 28 20 28 76 36 34 20 26 ;. if( (v64 &
b5fc0 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29 SQLITE_MAX_U32)
b5fd0 21 3d 76 36 34 20 29 7b 0a 20 20 20 20 20 20 2a !=v64 ){. *
b5fe0 76 20 3d 20 30 78 66 66 66 66 66 66 66 66 3b 0a v = 0xffffffff;.
b5ff0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
b6000 20 2a 76 20 3d 20 28 75 33 32 29 76 36 34 3b 0a *v = (u32)v64;.
b6010 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
b6020 20 6e 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 20 n;. }..#else.
b6030 20 2f 2a 20 46 6f 72 20 66 6f 6c 6c 6f 77 69 6e /* For followin
b6040 67 20 63 6f 64 65 20 28 6b 65 70 74 20 66 6f 72 g code (kept for
b6050 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 63 6f historical reco
b6060 72 64 20 6f 6e 6c 79 29 20 73 68 6f 77 73 20 61 rd only) shows a
b6070 6e 0a 20 20 2a 2a 20 75 6e 72 6f 6c 6c 69 6e 67 n. ** unrolling
b6080 20 66 6f 72 20 74 68 65 20 33 2d 20 61 6e 64 20 for the 3- and
b6090 34 2d 62 79 74 65 20 76 61 72 69 6e 74 20 63 61 4-byte varint ca
b60a0 73 65 73 2e 20 20 54 68 69 73 20 63 6f 64 65 20 ses. This code
b60b0 69 73 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79 is. ** slightly
b60c0 20 66 61 73 74 65 72 2c 20 62 75 74 20 69 74 20 faster, but it
b60d0 69 73 20 61 6c 73 6f 20 6c 61 72 67 65 72 20 61 is also larger a
b60e0 6e 64 20 6d 75 63 68 20 68 61 72 64 65 72 20 74 nd much harder t
b60f0 6f 20 74 65 73 74 2e 0a 20 20 2a 2f 0a 20 20 70 o test.. */. p
b6100 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b ++;. b = b<<14;
b6110 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a . b |= *p;. /*
b6120 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 b: p1<<14 | p3
b6130 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 (unmasked) */.
b6140 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 if (!(b&0x80)).
b6150 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 {. /* Values
b6160 20 62 65 74 77 65 65 6e 20 32 30 39 37 31 35 32 between 2097152
b6170 20 61 6e 64 20 32 36 38 34 33 35 34 35 35 20 2a and 268435455 *
b6180 2f 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 /. b &= (0x7f
b6190 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 <<14)|(0x7f);.
b61a0 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 a &= (0x7f<<14
b61b0 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 )|(0x7f);. a
b61c0 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d = a<<7;. *v =
b61d0 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 a | b;. retu
b61e0 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b rn 4;. }.. p++
b61f0 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 ;. a = a<<14;.
b6200 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 a |= *p;. /* a
b6210 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 : p0<<28 | p2<<1
b6220 34 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 4 | p4 (unmasked
b6230 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 ) */. if (!(a&0
b6240 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a x80)). {. /*
b6250 20 57 61 6c 75 65 73 20 20 62 65 74 77 65 65 6e Walues between
b6260 20 32 36 38 34 33 35 34 35 36 20 61 6e 64 20 33 268435456 and 3
b6270 34 33 35 39 37 33 38 33 36 37 20 2a 2f 0a 20 20 4359738367 */.
b6280 20 20 61 20 26 3d 20 28 30 78 31 66 3c 3c 32 38 a &= (0x1f<<28
b6290 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 )|(0x7f<<14)|(0x
b62a0 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 28 30 7f);. b &= (0
b62b0 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c x1f<<28)|(0x7f<<
b62c0 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 14)|(0x7f);.
b62d0 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 b = b<<7;. *v
b62e0 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 = a | b;. re
b62f0 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f turn 5;. }.. /
b6300 2a 20 57 65 20 63 61 6e 20 6f 6e 6c 79 20 72 65 * We can only re
b6310 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 20 77 ach this point w
b6320 68 65 6e 20 72 65 61 64 69 6e 67 20 61 20 63 6f hen reading a co
b6330 72 72 75 70 74 20 64 61 74 61 62 61 73 65 0a 20 rrupt database.
b6340 20 2a 2a 20 66 69 6c 65 2e 20 20 49 6e 20 74 68 ** file. In th
b6350 61 74 20 63 61 73 65 20 77 65 20 61 72 65 20 6e at case we are n
b6360 6f 74 20 69 6e 20 61 6e 79 20 68 75 72 72 79 2e ot in any hurry.
b6370 20 20 55 73 65 20 74 68 65 20 28 72 65 6c 61 74 Use the (relat
b6380 69 76 65 6c 79 0a 20 20 2a 2a 20 73 6c 6f 77 29 ively. ** slow)
b6390 20 67 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73 65 general-purpose
b63a0 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e sqlite3GetVarin
b63b0 74 28 29 20 72 6f 75 74 69 6e 65 20 74 6f 20 65 t() routine to e
b63c0 78 74 72 61 63 74 20 74 68 65 0a 20 20 2a 2a 20 xtract the. **
b63d0 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 7b 0a 20 20 value. */. {.
b63e0 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20 20 75 u64 v64;. u
b63f0 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d 20 34 8 n;.. p -= 4
b6400 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 ;. n = sqlite
b6410 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20 26 76 3GetVarint(p, &v
b6420 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 64);. assert(
b6430 20 6e 3e 35 20 26 26 20 6e 3c 3d 39 20 29 3b 0a n>5 && n<=9 );.
b6440 20 20 20 20 2a 76 20 3d 20 28 75 33 32 29 76 36 *v = (u32)v6
b6450 34 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 3b 4;. return n;
b6460 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f . }.#endif.}../
b6470 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
b6480 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
b6490 74 68 61 74 20 77 69 6c 6c 20 62 65 20 6e 65 65 that will be nee
b64a0 64 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 ded to store the
b64b0 20 67 69 76 65 6e 0a 2a 2a 20 36 34 2d 62 69 74 given.** 64-bit
b64c0 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c integer..*/.SQL
b64d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b64e0 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e sqlite3VarintLen
b64f0 28 75 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 69 (u64 v){. int i
b6500 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 = 0;. do{.
b6510 69 2b 2b 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37 i++;. v >>= 7
b6520 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30 ;. }while( v!=0
b6530 20 26 26 20 41 4c 57 41 59 53 28 69 3c 39 29 20 && ALWAYS(i<9)
b6540 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d );. return i;.}
b6550 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 .../*.** Read or
b6560 20 77 72 69 74 65 20 61 20 66 6f 75 72 2d 62 79 write a four-by
b6570 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e te big-endian in
b6580 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a teger value..*/.
b6590 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 SQLITE_PRIVATE u
b65a0 33 32 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 32 sqlite3Get4by
b65b0 74 65 28 63 6f 6e 73 74 20 75 38 20 2a 70 29 7b te(const u8 *p){
b65c0 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30 5d 3c . return (p[0]<
b65d0 3c 32 34 29 20 7c 20 28 70 5b 31 5d 3c 3c 31 36 <24) | (p[1]<<16
b65e0 29 20 7c 20 28 70 5b 32 5d 3c 3c 38 29 20 7c 20 ) | (p[2]<<8) |
b65f0 70 5b 33 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 p[3];.}.SQLITE_P
b6600 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
b6610 74 65 33 50 75 74 34 62 79 74 65 28 75 6e 73 69 te3Put4byte(unsi
b6620 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 33 gned char *p, u3
b6630 32 20 76 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 2 v){. p[0] = (
b6640 75 38 29 28 76 3e 3e 32 34 29 3b 0a 20 20 70 5b u8)(v>>24);. p[
b6650 31 5d 20 3d 20 28 75 38 29 28 76 3e 3e 31 36 29 1] = (u8)(v>>16)
b6660 3b 0a 20 20 70 5b 32 5d 20 3d 20 28 75 38 29 28 ;. p[2] = (u8)(
b6670 76 3e 3e 38 29 3b 0a 20 20 70 5b 33 5d 20 3d 20 v>>8);. p[3] =
b6680 28 75 38 29 76 3b 0a 7d 0a 0a 0a 0a 23 69 66 20 (u8)v;.}....#if
b6690 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
b66a0 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 OMIT_BLOB_LITERA
b66b0 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 L) || defined(SQ
b66c0 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 0a LITE_HAS_CODEC).
b66d0 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20 /*.** Translate
b66e0 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 6f 66 a single byte of
b66f0 20 48 65 78 20 69 6e 74 6f 20 61 6e 20 69 6e 74 Hex into an int
b6700 65 67 65 72 2e 0a 2a 2a 20 54 68 69 73 20 72 6f eger..** This ro
b6710 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 utine only works
b6720 20 69 66 20 68 20 72 65 61 6c 6c 79 20 69 73 20 if h really is
b6730 61 20 76 61 6c 69 64 20 68 65 78 61 64 65 63 69 a valid hexadeci
b6740 6d 61 6c 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 mal.** character
b6750 3a 20 20 30 2e 2e 39 61 2e 2e 66 41 2e 2e 46 0a : 0..9a..fA..F.
b6760 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 68 65 78 */.static u8 hex
b6770 54 6f 49 6e 74 28 69 6e 74 20 68 29 7b 0a 20 20 ToInt(int h){.
b6780 61 73 73 65 72 74 28 20 28 68 3e 3d 27 30 27 20 assert( (h>='0'
b6790 26 26 20 68 3c 3d 27 39 27 29 20 7c 7c 20 20 28 && h<='9') || (
b67a0 68 3e 3d 27 61 27 20 26 26 20 68 3c 3d 27 66 27 h>='a' && h<='f'
b67b0 29 20 7c 7c 20 20 28 68 3e 3d 27 41 27 20 26 26 ) || (h>='A' &&
b67c0 20 68 3c 3d 27 46 27 29 20 29 3b 0a 23 69 66 64 h<='F') );.#ifd
b67d0 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a ef SQLITE_ASCII.
b67e0 20 20 68 20 2b 3d 20 39 2a 28 31 26 28 68 3e 3e h += 9*(1&(h>>
b67f0 36 29 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 6));.#endif.#ifd
b6800 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 ef SQLITE_EBCDIC
b6810 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 7e 28 68 . h += 9*(1&~(h
b6820 3e 3e 34 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 >>4));.#endif.
b6830 72 65 74 75 72 6e 20 28 75 38 29 28 68 20 26 20 return (u8)(h &
b6840 30 78 66 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 0xf);.}.#endif /
b6850 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 * !SQLITE_OMIT_B
b6860 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 53 LOB_LITERAL || S
b6870 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 20 QLITE_HAS_CODEC
b6880 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 */..#if !defined
b6890 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f (SQLITE_OMIT_BLO
b68a0 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64 65 B_LITERAL) || de
b68b0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 53 fined(SQLITE_HAS
b68c0 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 43 6f _CODEC)./*.** Co
b68d0 6e 76 65 72 74 20 61 20 42 4c 4f 42 20 6c 69 74 nvert a BLOB lit
b68e0 65 72 61 6c 20 6f 66 20 74 68 65 20 66 6f 72 6d eral of the form
b68f0 20 22 78 27 68 68 68 68 68 68 27 22 20 69 6e 74 "x'hhhhhh'" int
b6900 6f 20 69 74 73 20 62 69 6e 61 72 79 0a 2a 2a 20 o its binary.**
b6910 76 61 6c 75 65 2e 20 20 52 65 74 75 72 6e 20 61 value. Return a
b6920 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20 pointer to its
b6930 62 69 6e 61 72 79 20 76 61 6c 75 65 2e 20 20 53 binary value. S
b6940 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 pace to hold the
b6950 0a 2a 2a 20 62 69 6e 61 72 79 20 76 61 6c 75 65 .** binary value
b6960 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e has been obtain
b6970 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 ed from malloc a
b6980 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 nd must be freed
b6990 20 62 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 by.** the calli
b69a0 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 ng routine..*/.S
b69b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
b69c0 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78 54 6f id *sqlite3HexTo
b69d0 42 6c 6f 62 28 73 71 6c 69 74 65 33 20 2a 64 62 Blob(sqlite3 *db
b69e0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c , const char *z,
b69f0 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20 int n){. char
b6a00 2a 7a 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 69 3b *zBlob;. int i;
b6a10 0a 0a 20 20 7a 42 6c 6f 62 20 3d 20 28 63 68 61 .. zBlob = (cha
b6a20 72 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c r *)sqlite3DbMal
b6a30 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2f 32 20 2b locRaw(db, n/2 +
b6a40 20 31 29 3b 0a 20 20 6e 2d 2d 3b 0a 20 20 69 66 1);. n--;. if
b6a50 28 20 7a 42 6c 6f 62 20 29 7b 0a 20 20 20 20 66 ( zBlob ){. f
b6a60 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 3d or(i=0; i<n; i+=
b6a70 32 29 7b 0a 20 20 20 20 20 20 7a 42 6c 6f 62 5b 2){. zBlob[
b6a80 69 2f 32 5d 20 3d 20 28 68 65 78 54 6f 49 6e 74 i/2] = (hexToInt
b6a90 28 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20 68 65 78 (z[i])<<4) | hex
b6aa0 54 6f 49 6e 74 28 7a 5b 69 2b 31 5d 29 3b 0a 20 ToInt(z[i+1]);.
b6ab0 20 20 20 7d 0a 20 20 20 20 7a 42 6c 6f 62 5b 69 }. zBlob[i
b6ac0 2f 32 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 /2] = 0;. }. r
b6ad0 65 74 75 72 6e 20 7a 42 6c 6f 62 3b 0a 7d 0a 23 eturn zBlob;.}.#
b6ae0 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 endif /* !SQLITE
b6af0 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 _OMIT_BLOB_LITER
b6b00 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41 53 AL || SQLITE_HAS
b6b10 5f 43 4f 44 45 43 20 2a 2f 0a 0a 0a 2f 2a 0a 2a _CODEC */.../*.*
b6b20 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 71 6c * Change the sql
b6b30 69 74 65 2e 6d 61 67 69 63 20 66 72 6f 6d 20 53 ite.magic from S
b6b40 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e QLITE_MAGIC_OPEN
b6b50 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 to SQLITE_MAGIC
b6b60 5f 42 55 53 59 2e 0a 2a 2a 20 52 65 74 75 72 6e _BUSY..** Return
b6b70 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e 2d 7a an error (non-z
b6b80 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61 67 69 ero) if the magi
b6b90 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 54 45 c was not SQLITE
b6ba0 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 77 _MAGIC_OPEN.** w
b6bb0 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
b6bc0 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a is called..**.*
b6bd0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
b6be0 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 65 6e s called when en
b6bf0 74 65 72 69 6e 67 20 61 6e 20 53 51 4c 69 74 65 tering an SQLite
b6c00 20 41 50 49 2e 20 20 54 68 65 20 53 51 4c 49 54 API. The SQLIT
b6c10 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 E_MAGIC_OPEN.**
b6c20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 value indicates
b6c30 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 that the databas
b6c40 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 61 73 e connection pas
b6c50 73 65 64 20 69 6e 74 6f 20 74 68 65 20 41 50 49 sed into the API
b6c60 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 61 6e 64 20 is.** open and
b6c70 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 75 73 65 is not being use
b6c80 64 20 62 79 20 61 6e 6f 74 68 65 72 20 74 68 72 d by another thr
b6c90 65 61 64 2e 20 20 42 79 20 63 68 61 6e 67 69 6e ead. By changin
b6ca0 67 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 74 g the value.** t
b6cb0 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 o SQLITE_MAGIC_B
b6cc0 55 53 59 20 77 65 20 69 6e 64 69 63 61 74 65 20 USY we indicate
b6cd0 74 68 61 74 20 74 68 65 20 63 6f 6e 6e 65 63 74 that the connect
b6ce0 69 6f 6e 20 69 73 20 69 6e 20 75 73 65 2e 0a 2a ion is in use..*
b6cf0 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f * sqlite3SafetyO
b6d00 66 66 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c 20 ff() below will
b6d10 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 change the value
b6d20 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f back to SQLITE_
b6d30 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 77 68 MAGIC_OPEN.** wh
b6d40 65 6e 20 74 68 65 20 41 50 49 20 65 78 69 74 73 en the API exits
b6d50 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f . .**.** This ro
b6d60 75 74 69 6e 65 20 69 73 20 61 20 61 74 74 65 6d utine is a attem
b6d70 70 74 20 74 6f 20 64 65 74 65 63 74 20 69 66 20 pt to detect if
b6d80 74 77 6f 20 74 68 72 65 61 64 73 20 75 73 65 20 two threads use
b6d90 74 68 65 0a 2a 2a 20 73 61 6d 65 20 73 71 6c 69 the.** same sqli
b6da0 74 65 2a 20 70 6f 69 6e 74 65 72 20 61 74 20 74 te* pointer at t
b6db0 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 54 he same time. T
b6dc0 68 65 72 65 20 69 73 20 61 20 72 61 63 65 20 0a here is a race .
b6dd0 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 20 73 6f 20 ** condition so
b6de0 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 it is possible t
b6df0 68 61 74 20 74 68 65 20 65 72 72 6f 72 20 69 73 hat the error is
b6e00 20 6e 6f 74 20 64 65 74 65 63 74 65 64 2e 0a 2a not detected..*
b6e10 2a 20 42 75 74 20 75 73 75 61 6c 6c 79 20 74 68 * But usually th
b6e20 65 20 70 72 6f 62 6c 65 6d 20 77 69 6c 6c 20 62 e problem will b
b6e30 65 20 73 65 65 6e 2e 20 20 54 68 65 20 72 65 73 e seen. The res
b6e40 75 6c 74 20 77 69 6c 6c 20 62 65 20 61 6e 0a 2a ult will be an.*
b6e50 2a 20 65 72 72 6f 72 20 77 68 69 63 68 20 63 61 * error which ca
b6e60 6e 20 62 65 20 75 73 65 64 20 74 6f 20 64 65 62 n be used to deb
b6e70 75 67 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 ug the applicati
b6e80 6f 6e 20 74 68 61 74 20 69 73 0a 2a 2a 20 75 73 on that is.** us
b6e90 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 63 6f 72 ing SQLite incor
b6ea0 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 69 rectly..**.** Ti
b6eb0 63 6b 65 74 20 23 32 30 32 3a 20 20 49 66 20 64 cket #202: If d
b6ec0 62 2d 3e 6d 61 67 69 63 20 69 73 20 6e 6f 74 20 b->magic is not
b6ed0 61 20 76 61 6c 69 64 20 6f 70 65 6e 20 76 61 6c a valid open val
b6ee0 75 65 2c 20 74 61 6b 65 20 63 61 72 65 20 6e 6f ue, take care no
b6ef0 74 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 t.** to modify t
b6f00 68 65 20 64 62 20 73 74 72 75 63 74 75 72 65 20 he db structure
b6f10 61 74 20 61 6c 6c 2e 20 20 49 74 20 63 6f 75 6c at all. It coul
b6f20 64 20 62 65 20 74 68 61 74 20 64 62 20 69 73 20 d be that db is
b6f30 61 20 73 74 61 6c 65 0a 2a 2a 20 70 6f 69 6e 74 a stale.** point
b6f40 65 72 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f er. In other wo
b6f50 72 64 73 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 rds, it could be
b6f60 20 74 68 61 74 20 74 68 65 72 65 20 68 61 73 20 that there has
b6f70 62 65 65 6e 20 61 20 70 72 69 6f 72 0a 2a 2a 20 been a prior.**
b6f80 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
b6f90 63 6c 6f 73 65 28 64 62 29 20 61 6e 64 20 64 62 close(db) and db
b6fa0 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f has been deallo
b6fb0 63 61 74 65 64 2e 20 20 41 6e 64 20 77 65 20 64 cated. And we d
b6fc0 6f 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f o.** not want to
b6fd0 20 77 72 69 74 65 20 69 6e 74 6f 20 64 65 61 6c write into deal
b6fe0 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 0a located memory..
b6ff0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
b7000 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 _DEBUG.SQLITE_PR
b7010 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b7020 33 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65 3SafetyOn(sqlite
b7030 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 3 *db){. if( db
b7040 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f ->magic==SQLITE_
b7050 4d 41 47 49 43 5f 4f 50 45 4e 20 29 7b 0a 20 20 MAGIC_OPEN ){.
b7060 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 db->magic = SQ
b7070 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 3b LITE_MAGIC_BUSY;
b7080 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
b7090 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
b70a0 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
b70b0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 return 0;. }e
b70c0 6c 73 65 20 69 66 28 20 64 62 2d 3e 6d 61 67 69 lse if( db->magi
b70d0 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f c==SQLITE_MAGIC_
b70e0 42 55 53 59 20 29 7b 0a 20 20 20 20 64 62 2d 3e BUSY ){. db->
b70f0 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d magic = SQLITE_M
b7100 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20 20 AGIC_ERROR;.
b7110 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 db->u1.isInterru
b7120 70 74 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 pted = 1;. }.
b7130 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 return 1;.}.#end
b7140 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 if../*.** Change
b7150 20 74 68 65 20 6d 61 67 69 63 20 66 72 6f 6d 20 the magic from
b7160 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 SQLITE_MAGIC_BUS
b7170 59 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 Y to SQLITE_MAGI
b7180 43 5f 4f 50 45 4e 2e 0a 2a 2a 20 52 65 74 75 72 C_OPEN..** Retur
b7190 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e 2d n an error (non-
b71a0 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61 67 zero) if the mag
b71b0 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 54 ic was not SQLIT
b71c0 45 5f 4d 41 47 49 43 5f 42 55 53 59 0a 2a 2a 20 E_MAGIC_BUSY.**
b71d0 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e when this routin
b71e0 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a e is called..*/.
b71f0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
b7200 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
b7210 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 TE int sqlite3Sa
b7220 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33 20 fetyOff(sqlite3
b7230 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 2d 3e *db){. if( db->
b7240 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 magic==SQLITE_MA
b7250 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 GIC_BUSY ){.
b7260 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 db->magic = SQLI
b7270 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b 0a 20 TE_MAGIC_OPEN;.
b7280 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
b7290 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 e3_mutex_held(db
b72a0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 ->mutex) );.
b72b0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 return 0;. }els
b72c0 65 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 e{. db->magic
b72d0 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f = SQLITE_MAGIC_
b72e0 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e 75 ERROR;. db->u
b72f0 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 1.isInterrupted
b7300 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = 1;. return
b7310 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 1;. }.}.#endif.
b7320 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 ./*.** Check to
b7330 6d 61 6b 65 20 73 75 72 65 20 77 65 20 68 61 76 make sure we hav
b7340 65 20 61 20 76 61 6c 69 64 20 64 62 20 70 6f 69 e a valid db poi
b7350 6e 74 65 72 2e 20 20 54 68 69 73 20 74 65 73 74 nter. This test
b7360 20 69 73 20 6e 6f 74 0a 2a 2a 20 66 6f 6f 6c 70 is not.** foolp
b7370 72 6f 6f 66 20 62 75 74 20 69 74 20 64 6f 65 73 roof but it does
b7380 20 70 72 6f 76 69 64 65 20 73 6f 6d 65 20 6d 65 provide some me
b7390 61 73 75 72 65 20 6f 66 20 70 72 6f 74 65 63 74 asure of protect
b73a0 69 6f 6e 20 61 67 61 69 6e 73 74 0a 2a 2a 20 6d ion against.** m
b73b0 69 73 75 73 65 20 6f 66 20 74 68 65 20 69 6e 74 isuse of the int
b73c0 65 72 66 61 63 65 20 73 75 63 68 20 61 73 20 70 erface such as p
b73d0 61 73 73 69 6e 67 20 69 6e 20 64 62 20 70 6f 69 assing in db poi
b73e0 6e 74 65 72 73 20 74 68 61 74 20 61 72 65 0a 2a nters that are.*
b73f0 2a 20 4e 55 4c 4c 20 6f 72 20 77 68 69 63 68 20 * NULL or which
b7400 68 61 76 65 20 62 65 65 6e 20 70 72 65 76 69 6f have been previo
b7410 75 73 6c 79 20 63 6c 6f 73 65 64 2e 20 20 49 66 usly closed. If
b7420 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
b7430 74 75 72 6e 73 0a 2a 2a 20 31 20 69 74 20 6d 65 turns.** 1 it me
b7440 61 6e 73 20 74 68 61 74 20 74 68 65 20 64 62 20 ans that the db
b7450 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69 64 pointer is valid
b7460 20 61 6e 64 20 30 20 69 66 20 69 74 20 73 68 6f and 0 if it sho
b7470 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 64 65 uld not be.** de
b7480 72 65 66 65 72 65 6e 63 65 64 20 66 6f 72 20 61 referenced for a
b7490 6e 79 20 72 65 61 73 6f 6e 2e 20 20 54 68 65 20 ny reason. The
b74a0 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e calling function
b74b0 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 0a 2a should invoke.*
b74c0 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 * SQLITE_MISUSE
b74d0 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a immediately..**.
b74e0 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 ** sqlite3Safety
b74f0 43 68 65 63 6b 4f 6b 28 29 20 72 65 71 75 69 72 CheckOk() requir
b7500 65 73 20 74 68 61 74 20 74 68 65 20 64 62 20 70 es that the db p
b7510 6f 69 6e 74 65 72 20 62 65 20 76 61 6c 69 64 20 ointer be valid
b7520 66 6f 72 0a 2a 2a 20 75 73 65 2e 20 20 73 71 6c for.** use. sql
b7530 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 ite3SafetyCheckS
b7540 69 63 6b 4f 72 4f 6b 28 29 20 61 6c 6c 6f 77 73 ickOrOk() allows
b7550 20 61 20 64 62 20 70 6f 69 6e 74 65 72 20 74 68 a db pointer th
b7560 61 74 20 66 61 69 6c 65 64 20 74 6f 0a 2a 2a 20 at failed to.**
b7570 6f 70 65 6e 20 70 72 6f 70 65 72 6c 79 20 61 6e open properly an
b7580 64 20 69 73 20 6e 6f 74 20 66 69 74 20 66 6f 72 d is not fit for
b7590 20 67 65 6e 65 72 61 6c 20 75 73 65 20 62 75 74 general use but
b75a0 20 77 68 69 63 68 20 63 61 6e 20 62 65 0a 2a 2a which can be.**
b75b0 20 75 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 used as an argu
b75c0 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f ment to sqlite3_
b75d0 65 72 72 6d 73 67 28 29 20 6f 72 20 73 71 6c 69 errmsg() or sqli
b75e0 74 65 33 5f 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a te3_close()..*/.
b75f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
b7600 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 nt sqlite3Safety
b7610 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 20 CheckOk(sqlite3
b7620 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d 61 67 69 *db){. u32 magi
b7630 63 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30 20 29 c;. if( db==0 )
b7640 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d 61 67 return 0;. mag
b7650 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b 0a ic = db->magic;.
b7660 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51 4c if( magic!=SQL
b7670 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 0a ITE_MAGIC_OPEN .
b7680 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
b7690 42 55 47 0a 20 20 20 20 20 26 26 20 6d 61 67 69 BUG. && magi
b76a0 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f c!=SQLITE_MAGIC_
b76b0 42 55 53 59 0a 23 65 6e 64 69 66 0a 20 20 29 7b BUSY.#endif. ){
b76c0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
b76d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 }else{. retu
b76e0 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 rn 1;. }.}.SQLI
b76f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
b7700 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 qlite3SafetyChec
b7710 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69 74 65 kSickOrOk(sqlite
b7720 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d 61 3 *db){. u32 ma
b7730 67 69 63 3b 0a 20 20 6d 61 67 69 63 20 3d 20 64 gic;. magic = d
b7740 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 b->magic;. if(
b7750 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 magic!=SQLITE_MA
b7760 47 49 43 5f 53 49 43 4b 20 26 26 0a 20 20 20 20 GIC_SICK &&.
b7770 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f magic!=SQLITE_
b7780 4d 41 47 49 43 5f 4f 50 45 4e 20 26 26 0a 20 20 MAGIC_OPEN &&.
b7790 20 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 magic!=SQLIT
b77a0 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 20 72 E_MAGIC_BUSY ) r
b77b0 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 eturn 0;. retur
b77c0 6e 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a n 1;.}../*******
b77d0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 ******* End of u
b77e0 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a til.c **********
b77f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7810 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
b7820 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
b7830 6c 65 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a le hash.c ******
b7840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7860 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
b7870 30 31 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a 01 September 22.
b7880 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
b7890 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
b78a0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
b78b0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
b78c0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
b78d0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
b78e0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
b78f0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
b7900 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
b7910 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
b7920 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
b7930 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
b7940 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
b7950 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
b7960 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
b7970 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
b7980 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
b7990 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
b79a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b79b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b79c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b79d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
b79e0 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d * This is the im
b79f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
b7a00 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 generic hash-tab
b7a10 6c 65 73 0a 2a 2a 20 75 73 65 64 20 69 6e 20 53 les.** used in S
b7a20 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 QLite..**.** $Id
b7a30 3a 20 68 61 73 68 2e 63 2c 76 20 31 2e 33 38 20 : hash.c,v 1.38
b7a40 32 30 30 39 2f 30 35 2f 30 39 20 32 33 3a 32 39 2009/05/09 23:29
b7a50 3a 31 32 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :12 drh Exp $.*/
b7a60 0a 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d ../* Turn bulk m
b7a70 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73 emory into a has
b7a80 68 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62 h table object b
b7a90 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 y initializing t
b7aa0 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 20 he.** fields of
b7ab0 74 68 65 20 48 61 73 68 20 73 74 72 75 63 74 75 the Hash structu
b7ac0 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22 re..**.** "pNew"
b7ad0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
b7ae0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
b7af0 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e that is to be in
b7b00 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 itialized..*/.SQ
b7b10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
b7b20 64 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 d sqlite3HashIni
b7b30 74 28 48 61 73 68 20 2a 70 4e 65 77 29 7b 0a 20 t(Hash *pNew){.
b7b40 20 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30 assert( pNew!=0
b7b50 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73 );. pNew->firs
b7b60 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 t = 0;. pNew->c
b7b70 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 ount = 0;. pNew
b7b80 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 ->htsize = 0;.
b7b90 70 4e 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a pNew->ht = 0;.}.
b7ba0 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 ./* Remove all e
b7bb0 6e 74 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61 ntries from a ha
b7bc0 73 68 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 sh table. Recla
b7bd0 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a im all memory..*
b7be0 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 * Call this rout
b7bf0 69 6e 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20 ine to delete a
b7c00 68 61 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f hash table or to
b7c10 20 72 65 73 65 74 20 61 20 68 61 73 68 20 74 61 reset a hash ta
b7c20 62 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d ble.** to the em
b7c30 70 74 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 pty state..*/.SQ
b7c40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
b7c50 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 d sqlite3HashCle
b7c60 61 72 28 48 61 73 68 20 2a 70 48 29 7b 0a 20 20 ar(Hash *pH){.
b7c70 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 HashElem *elem;
b7c80 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c /* For l
b7c90 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 ooping over all
b7ca0 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 elements of the
b7cb0 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 table */.. asse
b7cc0 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 65 rt( pH!=0 );. e
b7cd0 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b lem = pH->first;
b7ce0 0a 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 30 . pH->first = 0
b7cf0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
b7d00 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e (pH->ht);. pH->
b7d10 68 74 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68 74 ht = 0;. pH->ht
b7d20 73 69 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c size = 0;. whil
b7d30 65 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 48 e( elem ){. H
b7d40 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c ashElem *next_el
b7d50 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b em = elem->next;
b7d60 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
b7d70 65 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65 e(elem);. ele
b7d80 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 m = next_elem;.
b7d90 20 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d }. pH->count =
b7da0 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 0;.}../*.** The
b7db0 20 68 61 73 68 69 6e 67 20 66 75 6e 63 74 69 6f hashing functio
b7dc0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 6e 73 n..*/.static uns
b7dd0 69 67 6e 65 64 20 69 6e 74 20 73 74 72 48 61 73 igned int strHas
b7de0 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c h(const char *z,
b7df0 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e int nKey){. in
b7e00 74 20 68 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 t h = 0;. asser
b7e10 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 t( nKey>=0 );.
b7e20 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30 20 while( nKey > 0
b7e30 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c ){. h = (h<<
b7e40 33 29 20 5e 20 68 20 5e 20 73 71 6c 69 74 65 33 3) ^ h ^ sqlite3
b7e50 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e UpperToLower[(un
b7e60 73 69 67 6e 65 64 20 63 68 61 72 29 2a 7a 2b 2b signed char)*z++
b7e70 5d 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20 ];. nKey--;.
b7e80 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 3b 0a 7d }. return h;.}
b7e90 0a 0a 0a 2f 2a 20 4c 69 6e 6b 20 70 4e 65 77 20 .../* Link pNew
b7ea0 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 element into the
b7eb0 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 hash table pH.
b7ec0 20 49 66 20 70 45 6e 74 72 79 21 3d 30 20 74 68 If pEntry!=0 th
b7ed0 65 6e 20 61 6c 73 6f 0a 2a 2a 20 69 6e 73 65 72 en also.** inser
b7ee0 74 20 70 4e 65 77 20 69 6e 74 6f 20 74 68 65 20 t pNew into the
b7ef0 70 45 6e 74 72 79 20 68 61 73 68 20 62 75 63 6b pEntry hash buck
b7f00 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f et..*/.static vo
b7f10 69 64 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 id insertElement
b7f20 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20 (. Hash *pH,
b7f30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
b7f40 65 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 e complete hash
b7f50 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 table */. struc
b7f60 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 2c 20 20 t _ht *pEntry,
b7f70 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20 69 /* The entry i
b7f80 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20 69 nto which pNew i
b7f90 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 s inserted */.
b7fa0 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20 20 HashElem *pNew
b7fb0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 6c /* The el
b7fc0 65 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 65 ement to be inse
b7fd0 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 48 61 73 rted */.){. Has
b7fe0 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20 20 20 hElem *pHead;
b7ff0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c 65 /* First ele
b8000 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 69 6e 20 ment already in
b8010 70 45 6e 74 72 79 20 2a 2f 0a 20 20 69 66 28 20 pEntry */. if(
b8020 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 70 48 pEntry ){. pH
b8030 65 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f ead = pEntry->co
b8040 75 6e 74 20 3f 20 70 45 6e 74 72 79 2d 3e 63 68 unt ? pEntry->ch
b8050 61 69 6e 20 3a 20 30 3b 0a 20 20 20 20 70 45 6e ain : 0;. pEn
b8060 74 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 try->count++;.
b8070 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 pEntry->chain
b8080 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b = pNew;. }else{
b8090 0a 20 20 20 20 70 48 65 61 64 20 3d 20 30 3b 0a . pHead = 0;.
b80a0 20 20 7d 0a 20 20 69 66 28 20 70 48 65 61 64 20 }. if( pHead
b80b0 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 ){. pNew->nex
b80c0 74 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 20 70 t = pHead;. p
b80d0 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 61 New->prev = pHea
b80e0 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 66 28 d->prev;. if(
b80f0 20 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b 20 pHead->prev ){
b8100 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 78 pHead->prev->nex
b8110 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 t = pNew; }.
b8120 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 else
b8130 20 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 { pH->first = p
b8140 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 61 64 New; }. pHead
b8150 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a 20 ->prev = pNew;.
b8160 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 }else{. pNew
b8170 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69 72 ->next = pH->fir
b8180 73 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e st;. if( pH->
b8190 66 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 69 72 first ){ pH->fir
b81a0 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b st->prev = pNew;
b81b0 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 }. pNew->pre
b81c0 76 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e 66 v = 0;. pH->f
b81d0 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d irst = pNew;. }
b81e0 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 .}.../* Resize t
b81f0 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f he hash table so
b8200 20 74 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e that it cantain
b8210 73 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 s "new_size" buc
b8220 6b 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 kets..**.** The
b8230 68 61 73 68 20 74 61 62 6c 65 20 6d 69 67 68 74 hash table might
b8240 20 66 61 69 6c 20 74 6f 20 72 65 73 69 7a 65 20 fail to resize
b8250 69 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f if sqlite3_mallo
b8260 63 28 29 20 66 61 69 6c 73 20 6f 72 0a 2a 2a 20 c() fails or.**
b8270 69 66 20 74 68 65 20 6e 65 77 20 73 69 7a 65 20 if the new size
b8280 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 is the same as t
b8290 68 65 20 70 72 69 6f 72 20 73 69 7a 65 2e 0a 2a he prior size..*
b82a0 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 * Return TRUE if
b82b0 20 74 68 65 20 72 65 73 69 7a 65 20 6f 63 63 75 the resize occu
b82c0 72 73 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 rs and false if
b82d0 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 not..*/.static i
b82e0 6e 74 20 72 65 68 61 73 68 28 48 61 73 68 20 2a nt rehash(Hash *
b82f0 70 48 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 pH, unsigned int
b8300 20 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74 new_size){. st
b8310 72 75 63 74 20 5f 68 74 20 2a 6e 65 77 5f 68 74 ruct _ht *new_ht
b8320 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
b8330 54 68 65 20 6e 65 77 20 68 61 73 68 20 74 61 62 The new hash tab
b8340 6c 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d le */. HashElem
b8350 20 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c *elem, *next_el
b8360 65 6d 3b 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f em; /* For lo
b8370 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 oping over exist
b8380 69 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a ing elements */.
b8390 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c .#if SQLITE_MALL
b83a0 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 3e 30 0a OC_SOFT_LIMIT>0.
b83b0 20 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 2a 73 if( new_size*s
b83c0 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 izeof(struct _ht
b83d0 29 3e 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f )>SQLITE_MALLOC_
b83e0 53 4f 46 54 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 SOFT_LIMIT ){.
b83f0 20 20 6e 65 77 5f 73 69 7a 65 20 3d 20 53 51 4c new_size = SQL
b8400 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f ITE_MALLOC_SOFT_
b8410 4c 49 4d 49 54 2f 73 69 7a 65 6f 66 28 73 74 72 LIMIT/sizeof(str
b8420 75 63 74 20 5f 68 74 29 3b 0a 20 20 7d 0a 20 20 uct _ht);. }.
b8430 69 66 28 20 6e 65 77 5f 73 69 7a 65 3d 3d 70 48 if( new_size==pH
b8440 2d 3e 68 74 73 69 7a 65 20 29 20 72 65 74 75 72 ->htsize ) retur
b8450 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f n 0;.#endif.. /
b8460 2a 20 54 68 65 20 69 6e 61 62 69 6c 69 74 79 20 * The inability
b8470 74 6f 20 61 6c 6c 6f 63 61 74 65 73 20 73 70 61 to allocates spa
b8480 63 65 20 66 6f 72 20 61 20 6c 61 72 67 65 72 20 ce for a larger
b8490 68 61 73 68 20 74 61 62 6c 65 20 69 73 0a 20 20 hash table is.
b84a0 2a 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 ** a performance
b84b0 20 68 69 74 20 62 75 74 20 69 74 20 69 73 20 6e hit but it is n
b84c0 6f 74 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 ot a fatal error
b84d0 2e 20 20 53 6f 20 6d 61 72 6b 20 74 68 65 0a 20 . So mark the.
b84e0 20 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 ** allocation a
b84f0 73 20 61 20 62 65 6e 69 67 6e 2e 0a 20 20 2a 2f s a benign.. */
b8500 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 . sqlite3BeginB
b8510 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
b8520 20 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 75 63 new_ht = (struc
b8530 74 20 5f 68 74 20 2a 29 73 71 6c 69 74 65 33 4d t _ht *)sqlite3M
b8540 61 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 65 2a alloc( new_size*
b8550 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 sizeof(struct _h
b8560 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 t) );. sqlite3E
b8570 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 ndBenignMalloc()
b8580 3b 0a 0a 20 20 69 66 28 20 6e 65 77 5f 68 74 3d ;.. if( new_ht=
b8590 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
b85a0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 sqlite3_free(pH
b85b0 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 ->ht);. pH->ht
b85c0 3d 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e = new_ht;. pH->
b85d0 68 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a htsize = new_siz
b85e0 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f e = sqlite3Mallo
b85f0 63 53 69 7a 65 28 6e 65 77 5f 68 74 29 2f 73 69 cSize(new_ht)/si
b8600 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 zeof(struct _ht)
b8610 3b 0a 20 20 6d 65 6d 73 65 74 28 6e 65 77 5f 68 ;. memset(new_h
b8620 74 2c 20 30 2c 20 6e 65 77 5f 73 69 7a 65 2a 73 t, 0, new_size*s
b8630 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 izeof(struct _ht
b8640 29 29 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70 ));. for(elem=p
b8650 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66 69 H->first, pH->fi
b8660 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 rst=0; elem; ele
b8670 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a m = next_elem){.
b8680 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 unsigned int
b8690 20 68 20 3d 20 73 74 72 48 61 73 68 28 65 6c 65 h = strHash(ele
b86a0 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e m->pKey, elem->n
b86b0 4b 65 79 29 20 25 20 6e 65 77 5f 73 69 7a 65 3b Key) % new_size;
b86c0 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d . next_elem =
b86d0 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 elem->next;.
b86e0 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 insertElement(p
b86f0 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 65 H, &new_ht[h], e
b8700 6c 65 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 lem);. }. retu
b8710 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 rn 1;.}../* This
b8720 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69 function (for i
b8730 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 nternal use only
b8740 29 20 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c 65 ) locates an ele
b8750 6d 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61 ment in an.** ha
b8760 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 sh table that ma
b8770 74 63 68 65 73 20 74 68 65 20 67 69 76 65 6e 20 tches the given
b8780 6b 65 79 2e 20 20 54 68 65 20 68 61 73 68 20 66 key. The hash f
b8790 6f 72 20 74 68 69 73 20 6b 65 79 20 68 61 73 0a or this key has.
b87a0 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ** already been
b87b0 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 69 73 20 computed and is
b87c0 70 61 73 73 65 64 20 61 73 20 74 68 65 20 34 74 passed as the 4t
b87d0 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a h parameter..*/.
b87e0 73 74 61 74 69 63 20 48 61 73 68 45 6c 65 6d 20 static HashElem
b87f0 2a 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 *findElementGive
b8800 6e 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 48 nHash(. const H
b8810 61 73 68 20 2a 70 48 2c 20 20 20 20 20 2f 2a 20 ash *pH, /*
b8820 54 68 65 20 70 48 20 74 6f 20 62 65 20 73 65 61 The pH to be sea
b8830 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 rched */. const
b8840 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 20 20 2f char *pKey, /
b8850 2a 20 54 68 65 20 6b 65 79 20 77 65 20 61 72 65 * The key we are
b8860 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a searching for *
b8870 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 /. int nKey,
b8880 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 /* Bytes
b8890 20 69 6e 20 6b 65 79 20 28 6e 6f 74 20 63 6f 75 in key (not cou
b88a0 6e 74 69 6e 67 20 7a 65 72 6f 20 74 65 72 6d 69 nting zero termi
b88b0 6e 61 74 6f 72 29 20 2a 2f 0a 20 20 75 6e 73 69 nator) */. unsi
b88c0 67 6e 65 64 20 69 6e 74 20 68 20 20 20 20 20 20 gned int h
b88d0 2f 2a 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 /* The hash for
b88e0 74 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a this key. */.){.
b88f0 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d HashElem *elem
b8900 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
b8910 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 /* Used to loop
b8920 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e thru the elemen
b8930 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 t list */. int
b8940 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 count;
b8950 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
b8960 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 mber of elements
b8970 20 6c 65 66 74 20 74 6f 20 74 65 73 74 20 2a 2f left to test */
b8980 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 .. if( pH->ht )
b8990 7b 0a 20 20 20 20 73 74 72 75 63 74 20 5f 68 74 {. struct _ht
b89a0 20 2a 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e *pEntry = &pH->
b89b0 68 74 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 ht[h];. elem
b89c0 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b = pEntry->chain;
b89d0 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e . count = pEn
b89e0 74 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 7d 65 try->count;. }e
b89f0 6c 73 65 7b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 lse{. elem =
b8a00 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 63 pH->first;. c
b8a10 6f 75 6e 74 20 3d 20 70 48 2d 3e 63 6f 75 6e 74 ount = pH->count
b8a20 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 63 ;. }. while( c
b8a30 6f 75 6e 74 2d 2d 20 26 26 20 41 4c 57 41 59 53 ount-- && ALWAYS
b8a40 28 65 6c 65 6d 29 20 29 7b 0a 20 20 20 20 69 66 (elem) ){. if
b8a50 28 20 65 6c 65 6d 2d 3e 6e 4b 65 79 3d 3d 6e 4b ( elem->nKey==nK
b8a60 65 79 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 ey && sqlite3Str
b8a70 4e 49 43 6d 70 28 65 6c 65 6d 2d 3e 70 4b 65 79 NICmp(elem->pKey
b8a80 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 ,pKey,nKey)==0 )
b8a90 7b 20 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 { . return
b8aa0 65 6c 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 elem;. }.
b8ab0 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 elem = elem->nex
b8ac0 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 t;. }. return
b8ad0 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 0;.}../* Remove
b8ae0 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 a single entry f
b8af0 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 rom the hash tab
b8b00 6c 65 20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 le given a point
b8b10 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c er to that.** el
b8b20 65 6d 65 6e 74 20 61 6e 64 20 61 20 68 61 73 68 ement and a hash
b8b30 20 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 27 on the element'
b8b40 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 s key..*/.static
b8b50 20 76 6f 69 64 20 72 65 6d 6f 76 65 45 6c 65 6d void removeElem
b8b60 65 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 entGivenHash(.
b8b70 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 Hash *pH,
b8b80 20 20 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 /* The pH cont
b8b90 61 69 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f aining "elem" */
b8ba0 0a 20 20 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 . HashElem* ele
b8bb0 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d m, /* The elem
b8bc0 65 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 ent to be remove
b8bd0 64 20 66 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f d from the pH */
b8be0 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
b8bf0 68 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61 6c h /* Hash val
b8c00 75 65 20 66 6f 72 20 74 68 65 20 65 6c 65 6d 65 ue for the eleme
b8c10 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 nt */.){. struc
b8c20 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20 t _ht *pEntry;.
b8c30 20 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 if( elem->prev
b8c40 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65 ){. elem->pre
b8c50 76 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e v->next = elem->
b8c60 6e 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a next; . }else{.
b8c70 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 pH->first =
b8c80 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a elem->next;. }.
b8c90 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 if( elem->next
b8ca0 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 ){. elem->ne
b8cb0 78 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d xt->prev = elem-
b8cc0 3e 70 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 >prev;. }. if(
b8cd0 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 70 pH->ht ){. p
b8ce0 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b Entry = &pH->ht[
b8cf0 68 5d 3b 0a 20 20 20 20 69 66 28 20 70 45 6e 74 h];. if( pEnt
b8d00 72 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20 ry->chain==elem
b8d10 29 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 2d ){. pEntry-
b8d20 3e 63 68 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e >chain = elem->n
b8d30 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 ext;. }. p
b8d40 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a Entry->count--;.
b8d50 20 20 20 20 61 73 73 65 72 74 28 20 70 45 6e 74 assert( pEnt
b8d60 72 79 2d 3e 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a ry->count>=0 );.
b8d70 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 }. sqlite3_fr
b8d80 65 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20 70 48 ee( elem );. pH
b8d90 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 ->count--;. if(
b8da0 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b pH->count<=0 ){
b8db0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d . assert( pH-
b8dc0 3e 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 >first==0 );.
b8dd0 20 61 73 73 65 72 74 28 20 70 48 2d 3e 63 6f 75 assert( pH->cou
b8de0 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c nt==0 );. sql
b8df0 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 70 48 ite3HashClear(pH
b8e00 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 );. }.}../* Att
b8e10 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 empt to locate a
b8e20 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 n element of the
b8e30 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 20 77 hash table pH w
b8e40 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 ith a key.** tha
b8e50 74 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2c 6e t matches pKey,n
b8e60 4b 65 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 Key. Return the
b8e70 20 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 65 data for this e
b8e80 6c 65 6d 65 6e 74 20 69 66 20 69 74 20 69 73 0a lement if it is.
b8e90 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c ** found, or NUL
b8ea0 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f L if there is no
b8eb0 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 match..*/.SQLIT
b8ec0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
b8ed0 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 sqlite3HashFind(
b8ee0 63 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 const Hash *pH,
b8ef0 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 const char *pKey
b8f00 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 48 , int nKey){. H
b8f10 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 ashElem *elem;
b8f20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 /* The element
b8f30 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65 that matches ke
b8f40 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 y */. unsigned
b8f50 69 6e 74 20 68 3b 20 20 20 20 2f 2a 20 41 20 68 int h; /* A h
b8f60 61 73 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a 0a 20 ash on key */..
b8f70 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 assert( pH!=0 )
b8f80 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 ;. assert( pKey
b8f90 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
b8fa0 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69 66 nKey>=0 );. if
b8fb0 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 ( pH->ht ){.
b8fc0 68 20 3d 20 73 74 72 48 61 73 68 28 70 4b 65 79 h = strHash(pKey
b8fd0 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e 68 74 , nKey) % pH->ht
b8fe0 73 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 size;. }else{.
b8ff0 20 20 20 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 h = 0;. }.
b9000 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 elem = findEleme
b9010 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48 2c 20 ntGivenHash(pH,
b9020 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 68 29 3b 0a pKey, nKey, h);.
b9030 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20 return elem ?
b9040 65 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a elem->data : 0;.
b9050 7d 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 }../* Insert an
b9060 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 element into the
b9070 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 hash table pH.
b9080 20 54 68 65 20 6b 65 79 20 69 73 20 70 4b 65 79 The key is pKey
b9090 2c 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65 ,nKey.** and the
b90a0 20 64 61 74 61 20 69 73 20 22 64 61 74 61 22 2e data is "data".
b90b0 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 .**.** If no ele
b90c0 6d 65 6e 74 20 65 78 69 73 74 73 20 77 69 74 68 ment exists with
b90d0 20 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c a matching key,
b90e0 20 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65 then a new.** e
b90f0 6c 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74 65 lement is create
b9100 64 20 61 6e 64 20 4e 55 4c 4c 20 69 73 20 72 65 d and NULL is re
b9110 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 turned..**.** If
b9120 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74 another element
b9130 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 already exists
b9140 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6b 65 with the same ke
b9150 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6e y, then the.** n
b9160 65 77 20 64 61 74 61 20 72 65 70 6c 61 63 65 73 ew data replaces
b9170 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 61 6e the old data an
b9180 64 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69 d the old data i
b9190 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 s returned..** T
b91a0 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f he key is not co
b91b0 70 69 65 64 20 69 6e 20 74 68 69 73 20 69 6e 73 pied in this ins
b91c0 74 61 6e 63 65 2e 20 20 49 66 20 61 20 6d 61 6c tance. If a mal
b91d0 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68 65 6e 0a loc fails, then.
b91e0 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 ** the new data
b91f0 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 is returned and
b9200 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 the hash table i
b9210 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a s unchanged..**.
b9220 2a 2a 20 49 66 20 74 68 65 20 22 64 61 74 61 22 ** If the "data"
b9230 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 parameter to th
b9240 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e is function is N
b9250 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a ULL, then the.**
b9260 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73 70 element corresp
b9270 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 22 20 onding to "key"
b9280 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 is removed from
b9290 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a the hash table..
b92a0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b92b0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 E void *sqlite3H
b92c0 61 73 68 49 6e 73 65 72 74 28 48 61 73 68 20 2a ashInsert(Hash *
b92d0 70 48 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a pH, const char *
b92e0 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 pKey, int nKey,
b92f0 76 6f 69 64 20 2a 64 61 74 61 29 7b 0a 20 20 75 void *data){. u
b9300 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 20 20 nsigned int h;
b9310 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68 /* the hash
b9320 20 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f 64 75 of the key modu
b9330 6c 6f 20 68 61 73 68 20 74 61 62 6c 65 20 73 69 lo hash table si
b9340 7a 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d ze */. HashElem
b9350 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a *elem; /*
b9360 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 Used to loop th
b9370 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c ru the element l
b9380 69 73 74 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 ist */. HashEle
b9390 6d 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f m *new_elem; /
b93a0 2a 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 * New element ad
b93b0 64 65 64 20 74 6f 20 74 68 65 20 70 48 20 2a 2f ded to the pH */
b93c0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48 21 3d .. assert( pH!=
b93d0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
b93e0 4b 65 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 Key!=0 );. asse
b93f0 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 rt( nKey>=0 );.
b9400 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 20 if( pH->htsize
b9410 29 7b 0a 20 20 20 20 68 20 3d 20 73 74 72 48 61 ){. h = strHa
b9420 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 sh(pKey, nKey) %
b9430 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 7d pH->htsize;. }
b9440 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 30 3b else{. h = 0;
b9450 0a 20 20 7d 0a 20 20 65 6c 65 6d 20 3d 20 66 69 . }. elem = fi
b9460 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 ndElementGivenHa
b9470 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c sh(pH,pKey,nKey,
b9480 68 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d 20 29 h);. if( elem )
b9490 7b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f {. void *old_
b94a0 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 data = elem->dat
b94b0 61 3b 0a 20 20 20 20 69 66 28 20 64 61 74 61 3d a;. if( data=
b94c0 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 6d 6f =0 ){. remo
b94d0 76 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 veElementGivenHa
b94e0 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 sh(pH,elem,h);.
b94f0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
b9500 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 elem->data = dat
b9510 61 3b 0a 20 20 20 20 20 20 65 6c 65 6d 2d 3e 70 a;. elem->p
b9520 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 Key = pKey;.
b9530 20 20 61 73 73 65 72 74 28 6e 4b 65 79 3d 3d 65 assert(nKey==e
b9540 6c 65 6d 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 lem->nKey);.
b9550 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 6c 64 }. return old
b9560 5f 64 61 74 61 3b 0a 20 20 7d 0a 20 20 69 66 28 _data;. }. if(
b9570 20 64 61 74 61 3d 3d 30 20 29 20 72 65 74 75 72 data==0 ) retur
b9580 6e 20 30 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 20 n 0;. new_elem
b9590 3d 20 28 48 61 73 68 45 6c 65 6d 2a 29 73 71 6c = (HashElem*)sql
b95a0 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 ite3Malloc( size
b95b0 6f 66 28 48 61 73 68 45 6c 65 6d 29 20 29 3b 0a of(HashElem) );.
b95c0 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d if( new_elem==
b95d0 30 20 29 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0 ) return data;
b95e0 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 . new_elem->pKe
b95f0 79 20 3d 20 70 4b 65 79 3b 0a 20 20 6e 65 77 5f y = pKey;. new_
b9600 65 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65 elem->nKey = nKe
b9610 79 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64 y;. new_elem->d
b9620 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 70 48 ata = data;. pH
b9630 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 ->count++;. if(
b9640 20 70 48 2d 3e 63 6f 75 6e 74 3e 3d 31 30 20 26 pH->count>=10 &
b9650 26 20 70 48 2d 3e 63 6f 75 6e 74 20 3e 20 32 2a & pH->count > 2*
b9660 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20 pH->htsize ){.
b9670 20 20 69 66 28 20 72 65 68 61 73 68 28 70 48 2c if( rehash(pH,
b9680 20 70 48 2d 3e 63 6f 75 6e 74 2a 32 29 20 29 7b pH->count*2) ){
b9690 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
b96a0 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 3b 0a 20 H->htsize>0 );.
b96b0 20 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 68 h = strHash
b96c0 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 (pKey, nKey) % p
b96d0 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 20 20 7d H->htsize;. }
b96e0 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e 68 . }. if( pH->h
b96f0 74 20 29 7b 0a 20 20 20 20 69 6e 73 65 72 74 45 t ){. insertE
b9700 6c 65 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e lement(pH, &pH->
b9710 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 ht[h], new_elem)
b9720 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
b9730 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c nsertElement(pH,
b9740 20 30 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 0, new_elem);.
b9750 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d }. return 0;.}
b9760 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
b9770 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 63 20 * End of hash.c
b9780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b97a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b97b0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
b97c0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 * Begin file opc
b97d0 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a odes.c *********
b97e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b97f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9800 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c /./* Automatical
b9810 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 ly generated. D
b9820 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a o not edit */./*
b9830 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 See the mkopcod
b9840 65 63 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f ec.awk script fo
b9850 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 69 r details. */.#i
b9860 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
b9870 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 E_OMIT_EXPLAIN)
b9880 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 || !defined(NDEB
b9890 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 UG) || defined(V
b98a0 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 DBE_PROFILE) ||
b98b0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
b98c0 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 EBUG).SQLITE_PRI
b98d0 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 VATE const char
b98e0 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 *sqlite3OpcodeNa
b98f0 6d 65 28 69 6e 74 20 69 29 7b 0a 20 73 74 61 74 me(int i){. stat
b9900 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 ic const char *c
b9910 6f 6e 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20 onst azName[] =
b9920 7b 20 22 3f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 { "?",. /*
b9930 20 31 20 2a 2f 20 22 56 4e 65 78 74 22 2c 0a 20 1 */ "VNext",.
b9940 20 20 20 20 2f 2a 20 20 20 32 20 2a 2f 20 22 41 /* 2 */ "A
b9950 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20 2f ffinity",. /
b9960 2a 20 20 20 33 20 2a 2f 20 22 43 6f 6c 75 6d 6e * 3 */ "Column
b9970 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 34 20 2a ",. /* 4 *
b9980 2f 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0a 20 / "SetCookie",.
b9990 20 20 20 20 2f 2a 20 20 20 35 20 2a 2f 20 22 53 /* 5 */ "S
b99a0 65 65 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 eek",. /*
b99b0 36 20 2a 2f 20 22 53 65 71 75 65 6e 63 65 22 2c 6 */ "Sequence",
b99c0 0a 20 20 20 20 20 2f 2a 20 20 20 37 20 2a 2f 20 . /* 7 */
b99d0 22 53 61 76 65 70 6f 69 6e 74 22 2c 0a 20 20 20 "Savepoint",.
b99e0 20 20 2f 2a 20 20 20 38 20 2a 2f 20 22 52 6f 77 /* 8 */ "Row
b99f0 4b 65 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 Key",. /*
b9a00 39 20 2a 2f 20 22 53 43 6f 70 79 22 2c 0a 20 20 9 */ "SCopy",.
b9a10 20 20 20 2f 2a 20 20 31 30 20 2a 2f 20 22 4f 70 /* 10 */ "Op
b9a20 65 6e 57 72 69 74 65 22 2c 0a 20 20 20 20 20 2f enWrite",. /
b9a30 2a 20 20 31 31 20 2a 2f 20 22 49 66 22 2c 0a 20 * 11 */ "If",.
b9a40 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f 20 22 43 /* 12 */ "C
b9a50 6f 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20 2f 2a ollSeq",. /*
b9a60 20 20 31 33 20 2a 2f 20 22 4f 70 65 6e 52 65 61 13 */ "OpenRea
b9a70 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 34 20 d",. /* 14
b9a80 2a 2f 20 22 45 78 70 69 72 65 22 2c 0a 20 20 20 */ "Expire",.
b9a90 20 20 2f 2a 20 20 31 35 20 2a 2f 20 22 41 75 74 /* 15 */ "Aut
b9aa0 6f 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 20 20 2f oCommit",. /
b9ab0 2a 20 20 31 36 20 2a 2f 20 22 50 61 67 65 63 6f * 16 */ "Pageco
b9ac0 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 unt",. /* 1
b9ad0 37 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 79 43 7 */ "IntegrityC
b9ae0 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 38 20 k",. /* 18
b9af0 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 20 20 20 20 */ "Sort",.
b9b00 2f 2a 20 20 31 39 20 2a 2f 20 22 4e 6f 74 22 2c /* 19 */ "Not",
b9b10 0a 20 20 20 20 20 2f 2a 20 20 32 30 20 2a 2f 20 . /* 20 */
b9b20 22 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f 2a 20 "Copy",. /*
b9b30 20 32 31 20 2a 2f 20 22 54 72 61 63 65 22 2c 0a 21 */ "Trace",.
b9b40 20 20 20 20 20 2f 2a 20 20 32 32 20 2a 2f 20 22 /* 22 */ "
b9b50 46 75 6e 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 Function",.
b9b60 2f 2a 20 20 32 33 20 2a 2f 20 22 49 66 4e 65 67 /* 23 */ "IfNeg
b9b70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 34 20 2a ",. /* 24 *
b9b80 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20 20 2f / "Noop",. /
b9b90 2a 20 20 32 35 20 2a 2f 20 22 50 72 6f 67 72 61 * 25 */ "Progra
b9ba0 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 36 20 m",. /* 26
b9bb0 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0a 20 20 20 */ "Return",.
b9bc0 20 20 2f 2a 20 20 32 37 20 2a 2f 20 22 4e 65 77 /* 27 */ "New
b9bd0 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20 Rowid",. /*
b9be0 20 32 38 20 2a 2f 20 22 56 61 72 69 61 62 6c 65 28 */ "Variable
b9bf0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 39 20 2a ",. /* 29 *
b9c00 2f 20 22 53 74 72 69 6e 67 22 2c 0a 20 20 20 20 / "String",.
b9c10 20 2f 2a 20 20 33 30 20 2a 2f 20 22 52 65 61 6c /* 30 */ "Real
b9c20 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20 Affinity",.
b9c30 2f 2a 20 20 33 31 20 2a 2f 20 22 56 52 65 6e 61 /* 31 */ "VRena
b9c40 6d 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 32 me",. /* 32
b9c50 20 2a 2f 20 22 50 61 72 73 65 53 63 68 65 6d 61 */ "ParseSchema
b9c60 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 33 20 2a ",. /* 33 *
b9c70 2f 20 22 56 4f 70 65 6e 22 2c 0a 20 20 20 20 20 / "VOpen",.
b9c80 2f 2a 20 20 33 34 20 2a 2f 20 22 43 6c 6f 73 65 /* 34 */ "Close
b9c90 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 35 20 2a ",. /* 35 *
b9ca0 2f 20 22 43 72 65 61 74 65 49 6e 64 65 78 22 2c / "CreateIndex",
b9cb0 0a 20 20 20 20 20 2f 2a 20 20 33 36 20 2a 2f 20 . /* 36 */
b9cc0 22 49 73 55 6e 69 71 75 65 22 2c 0a 20 20 20 20 "IsUnique",.
b9cd0 20 2f 2a 20 20 33 37 20 2a 2f 20 22 4e 6f 74 46 /* 37 */ "NotF
b9ce0 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 ound",. /*
b9cf0 33 38 20 2a 2f 20 22 49 6e 74 36 34 22 2c 0a 20 38 */ "Int64",.
b9d00 20 20 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22 4d /* 39 */ "M
b9d10 75 73 74 42 65 49 6e 74 22 2c 0a 20 20 20 20 20 ustBeInt",.
b9d20 2f 2a 20 20 34 30 20 2a 2f 20 22 48 61 6c 74 22 /* 40 */ "Halt"
b9d30 2c 0a 20 20 20 20 20 2f 2a 20 20 34 31 20 2a 2f ,. /* 41 */
b9d40 20 22 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f "Rowid",. /
b9d50 2a 20 20 34 32 20 2a 2f 20 22 49 64 78 4c 54 22 * 42 */ "IdxLT"
b9d60 2c 0a 20 20 20 20 20 2f 2a 20 20 34 33 20 2a 2f ,. /* 43 */
b9d70 20 22 41 64 64 49 6d 6d 22 2c 0a 20 20 20 20 20 "AddImm",.
b9d80 2f 2a 20 20 34 34 20 2a 2f 20 22 52 6f 77 44 61 /* 44 */ "RowDa
b9d90 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 35 ta",. /* 45
b9da0 20 2a 2f 20 22 4d 65 6d 4d 61 78 22 2c 0a 20 20 */ "MemMax",.
b9db0 20 20 20 2f 2a 20 20 34 36 20 2a 2f 20 22 4e 6f /* 46 */ "No
b9dc0 74 45 78 69 73 74 73 22 2c 0a 20 20 20 20 20 2f tExists",. /
b9dd0 2a 20 20 34 37 20 2a 2f 20 22 47 6f 73 75 62 22 * 47 */ "Gosub"
b9de0 2c 0a 20 20 20 20 20 2f 2a 20 20 34 38 20 2a 2f ,. /* 48 */
b9df0 20 22 49 6e 74 65 67 65 72 22 2c 0a 20 20 20 20 "Integer",.
b9e00 20 2f 2a 20 20 34 39 20 2a 2f 20 22 50 72 65 76 /* 49 */ "Prev
b9e10 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 30 20 2a ",. /* 50 *
b9e20 2f 20 22 52 6f 77 53 65 74 52 65 61 64 22 2c 0a / "RowSetRead",.
b9e30 20 20 20 20 20 2f 2a 20 20 35 31 20 2a 2f 20 22 /* 51 */ "
b9e40 52 6f 77 53 65 74 41 64 64 22 2c 0a 20 20 20 20 RowSetAdd",.
b9e50 20 2f 2a 20 20 35 32 20 2a 2f 20 22 56 43 6f 6c /* 52 */ "VCol
b9e60 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 umn",. /* 5
b9e70 33 20 2a 2f 20 22 43 72 65 61 74 65 54 61 62 6c 3 */ "CreateTabl
b9e80 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 34 20 e",. /* 54
b9e90 2a 2f 20 22 4c 61 73 74 22 2c 0a 20 20 20 20 20 */ "Last",.
b9ea0 2f 2a 20 20 35 35 20 2a 2f 20 22 53 65 65 6b 4c /* 55 */ "SeekL
b9eb0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 36 20 e",. /* 56
b9ec0 2a 2f 20 22 49 6e 63 72 56 61 63 75 75 6d 22 2c */ "IncrVacuum",
b9ed0 0a 20 20 20 20 20 2f 2a 20 20 35 37 20 2a 2f 20 . /* 57 */
b9ee0 22 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 20 "IdxRowid",.
b9ef0 20 2f 2a 20 20 35 38 20 2a 2f 20 22 52 65 73 65 /* 58 */ "Rese
b9f00 74 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a tCount",. /*
b9f10 20 20 35 39 20 2a 2f 20 22 59 69 65 6c 64 22 2c 59 */ "Yield",
b9f20 0a 20 20 20 20 20 2f 2a 20 20 36 30 20 2a 2f 20 . /* 60 */
b9f30 22 44 72 6f 70 54 72 69 67 67 65 72 22 2c 0a 20 "DropTrigger",.
b9f40 20 20 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22 44 /* 61 */ "D
b9f50 72 6f 70 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 ropIndex",.
b9f60 2f 2a 20 20 36 32 20 2a 2f 20 22 50 61 72 61 6d /* 62 */ "Param
b9f70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 33 20 2a ",. /* 63 *
b9f80 2f 20 22 49 64 78 47 45 22 2c 0a 20 20 20 20 20 / "IdxGE",.
b9f90 2f 2a 20 20 36 34 20 2a 2f 20 22 49 64 78 44 65 /* 64 */ "IdxDe
b9fa0 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 lete",. /*
b9fb0 36 35 20 2a 2f 20 22 56 61 63 75 75 6d 22 2c 0a 65 */ "Vacuum",.
b9fc0 20 20 20 20 20 2f 2a 20 20 36 36 20 2a 2f 20 22 /* 66 */ "
b9fd0 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 37 Or",. /* 67
b9fe0 20 2a 2f 20 22 41 6e 64 22 2c 0a 20 20 20 20 20 */ "And",.
b9ff0 2f 2a 20 20 36 38 20 2a 2f 20 22 49 66 4e 6f 74 /* 68 */ "IfNot
ba000 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 39 20 2a ",. /* 69 *
ba010 2f 20 22 44 72 6f 70 54 61 62 6c 65 22 2c 0a 20 / "DropTable",.
ba020 20 20 20 20 2f 2a 20 20 37 30 20 2a 2f 20 22 53 /* 70 */ "S
ba030 65 65 6b 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 eekLt",. /*
ba040 20 37 31 20 2a 2f 20 22 49 73 4e 75 6c 6c 22 2c 71 */ "IsNull",
ba050 0a 20 20 20 20 20 2f 2a 20 20 37 32 20 2a 2f 20 . /* 72 */
ba060 22 4e 6f 74 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 "NotNull",.
ba070 2f 2a 20 20 37 33 20 2a 2f 20 22 4e 65 22 2c 0a /* 73 */ "Ne",.
ba080 20 20 20 20 20 2f 2a 20 20 37 34 20 2a 2f 20 22 /* 74 */ "
ba090 45 71 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 35 Eq",. /* 75
ba0a0 20 2a 2f 20 22 47 74 22 2c 0a 20 20 20 20 20 2f */ "Gt",. /
ba0b0 2a 20 20 37 36 20 2a 2f 20 22 4c 65 22 2c 0a 20 * 76 */ "Le",.
ba0c0 20 20 20 20 2f 2a 20 20 37 37 20 2a 2f 20 22 4c /* 77 */ "L
ba0d0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 38 20 t",. /* 78
ba0e0 2a 2f 20 22 47 65 22 2c 0a 20 20 20 20 20 2f 2a */ "Ge",. /*
ba0f0 20 20 37 39 20 2a 2f 20 22 4d 61 6b 65 52 65 63 79 */ "MakeRec
ba100 6f 72 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 ord",. /* 8
ba110 30 20 2a 2f 20 22 42 69 74 41 6e 64 22 2c 0a 20 0 */ "BitAnd",.
ba120 20 20 20 20 2f 2a 20 20 38 31 20 2a 2f 20 22 42 /* 81 */ "B
ba130 69 74 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 itOr",. /*
ba140 38 32 20 2a 2f 20 22 53 68 69 66 74 4c 65 66 74 82 */ "ShiftLeft
ba150 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 33 20 2a ",. /* 83 *
ba160 2f 20 22 53 68 69 66 74 52 69 67 68 74 22 2c 0a / "ShiftRight",.
ba170 20 20 20 20 20 2f 2a 20 20 38 34 20 2a 2f 20 22 /* 84 */ "
ba180 41 64 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 Add",. /* 8
ba190 35 20 2a 2f 20 22 53 75 62 74 72 61 63 74 22 2c 5 */ "Subtract",
ba1a0 0a 20 20 20 20 20 2f 2a 20 20 38 36 20 2a 2f 20 . /* 86 */
ba1b0 22 4d 75 6c 74 69 70 6c 79 22 2c 0a 20 20 20 20 "Multiply",.
ba1c0 20 2f 2a 20 20 38 37 20 2a 2f 20 22 44 69 76 69 /* 87 */ "Divi
ba1d0 64 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 38 de",. /* 88
ba1e0 20 2a 2f 20 22 52 65 6d 61 69 6e 64 65 72 22 2c */ "Remainder",
ba1f0 0a 20 20 20 20 20 2f 2a 20 20 38 39 20 2a 2f 20 . /* 89 */
ba200 22 43 6f 6e 63 61 74 22 2c 0a 20 20 20 20 20 2f "Concat",. /
ba210 2a 20 20 39 30 20 2a 2f 20 22 52 65 73 75 6c 74 * 90 */ "Result
ba220 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 Row",. /* 9
ba230 31 20 2a 2f 20 22 44 65 6c 65 74 65 22 2c 0a 20 1 */ "Delete",.
ba240 20 20 20 20 2f 2a 20 20 39 32 20 2a 2f 20 22 41 /* 92 */ "A
ba250 67 67 46 69 6e 61 6c 22 2c 0a 20 20 20 20 20 2f ggFinal",. /
ba260 2a 20 20 39 33 20 2a 2f 20 22 42 69 74 4e 6f 74 * 93 */ "BitNot
ba270 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 34 20 2a ",. /* 94 *
ba280 2f 20 22 53 74 72 69 6e 67 38 22 2c 0a 20 20 20 / "String8",.
ba290 20 20 2f 2a 20 20 39 35 20 2a 2f 20 22 43 6f 6d /* 95 */ "Com
ba2a0 70 61 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 pare",. /*
ba2b0 39 36 20 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 20 96 */ "Goto",.
ba2c0 20 20 20 2f 2a 20 20 39 37 20 2a 2f 20 22 54 61 /* 97 */ "Ta
ba2d0 62 6c 65 4c 6f 63 6b 22 2c 0a 20 20 20 20 20 2f bleLock",. /
ba2e0 2a 20 20 39 38 20 2a 2f 20 22 43 6c 65 61 72 22 * 98 */ "Clear"
ba2f0 2c 0a 20 20 20 20 20 2f 2a 20 20 39 39 20 2a 2f ,. /* 99 */
ba300 20 22 56 65 72 69 66 79 43 6f 6f 6b 69 65 22 2c "VerifyCookie",
ba310 0a 20 20 20 20 20 2f 2a 20 31 30 30 20 2a 2f 20 . /* 100 */
ba320 22 41 67 67 53 74 65 70 22 2c 0a 20 20 20 20 20 "AggStep",.
ba330 2f 2a 20 31 30 31 20 2a 2f 20 22 54 72 61 6e 73 /* 101 */ "Trans
ba340 61 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a action",. /*
ba350 20 31 30 32 20 2a 2f 20 22 56 46 69 6c 74 65 72 102 */ "VFilter
ba360 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 33 20 2a ",. /* 103 *
ba370 2f 20 22 56 44 65 73 74 72 6f 79 22 2c 0a 20 20 / "VDestroy",.
ba380 20 20 20 2f 2a 20 31 30 34 20 2a 2f 20 22 4e 65 /* 104 */ "Ne
ba390 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 35 xt",. /* 105
ba3a0 20 2a 2f 20 22 43 6f 75 6e 74 22 2c 0a 20 20 20 */ "Count",.
ba3b0 20 20 2f 2a 20 31 30 36 20 2a 2f 20 22 49 64 78 /* 106 */ "Idx
ba3c0 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 20 2f 2a Insert",. /*
ba3d0 20 31 30 37 20 2a 2f 20 22 53 65 65 6b 47 65 22 107 */ "SeekGe"
ba3e0 2c 0a 20 20 20 20 20 2f 2a 20 31 30 38 20 2a 2f ,. /* 108 */
ba3f0 20 22 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 20 "Insert",.
ba400 2f 2a 20 31 30 39 20 2a 2f 20 22 44 65 73 74 72 /* 109 */ "Destr
ba410 6f 79 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 30 oy",. /* 110
ba420 20 2a 2f 20 22 52 65 61 64 43 6f 6f 6b 69 65 22 */ "ReadCookie"
ba430 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20 2a 2f ,. /* 111 */
ba440 20 22 52 6f 77 53 65 74 54 65 73 74 22 2c 0a 20 "RowSetTest",.
ba450 20 20 20 20 2f 2a 20 31 31 32 20 2a 2f 20 22 4c /* 112 */ "L
ba460 6f 61 64 41 6e 61 6c 79 73 69 73 22 2c 0a 20 20 oadAnalysis",.
ba470 20 20 20 2f 2a 20 31 31 33 20 2a 2f 20 22 45 78 /* 113 */ "Ex
ba480 70 6c 61 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 plain",. /*
ba490 31 31 34 20 2a 2f 20 22 48 61 6c 74 49 66 4e 75 114 */ "HaltIfNu
ba4a0 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 35 ll",. /* 115
ba4b0 20 2a 2f 20 22 4f 70 65 6e 50 73 65 75 64 6f 22 */ "OpenPseudo"
ba4c0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 36 20 2a 2f ,. /* 116 */
ba4d0 20 22 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 22 "OpenEphemeral"
ba4e0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 37 20 2a 2f ,. /* 117 */
ba4f0 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a "Null",. /*
ba500 20 31 31 38 20 2a 2f 20 22 4d 6f 76 65 22 2c 0a 118 */ "Move",.
ba510 20 20 20 20 20 2f 2a 20 31 31 39 20 2a 2f 20 22 /* 119 */ "
ba520 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 Blob",. /* 1
ba530 32 30 20 2a 2f 20 22 52 65 77 69 6e 64 22 2c 0a 20 */ "Rewind",.
ba540 20 20 20 20 20 2f 2a 20 31 32 31 20 2a 2f 20 22 /* 121 */ "
ba550 53 65 65 6b 47 74 22 2c 0a 20 20 20 20 20 2f 2a SeekGt",. /*
ba560 20 31 32 32 20 2a 2f 20 22 56 42 65 67 69 6e 22 122 */ "VBegin"
ba570 2c 0a 20 20 20 20 20 2f 2a 20 31 32 33 20 2a 2f ,. /* 123 */
ba580 20 22 56 55 70 64 61 74 65 22 2c 0a 20 20 20 20 "VUpdate",.
ba590 20 2f 2a 20 31 32 34 20 2a 2f 20 22 49 66 5a 65 /* 124 */ "IfZe
ba5a0 72 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 35 ro",. /* 125
ba5b0 20 2a 2f 20 22 56 43 72 65 61 74 65 22 2c 0a 20 */ "VCreate",.
ba5c0 20 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20 22 46 /* 126 */ "F
ba5d0 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 31 ound",. /* 1
ba5e0 32 37 20 2a 2f 20 22 49 66 50 6f 73 22 2c 0a 20 27 */ "IfPos",.
ba5f0 20 20 20 20 2f 2a 20 31 32 38 20 2a 2f 20 22 4e /* 128 */ "N
ba600 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a ullRow",. /*
ba610 20 31 32 39 20 2a 2f 20 22 4a 75 6d 70 22 2c 0a 129 */ "Jump",.
ba620 20 20 20 20 20 2f 2a 20 31 33 30 20 2a 2f 20 22 /* 130 */ "
ba630 52 65 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 Real",. /* 1
ba640 33 31 20 2a 2f 20 22 50 65 72 6d 75 74 61 74 69 31 */ "Permutati
ba650 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 32 on",. /* 132
ba660 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 32 */ "NotUsed_132
ba670 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33 20 2a ",. /* 133 *
ba680 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 33 22 2c / "NotUsed_133",
ba690 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a 2f 20 . /* 134 */
ba6a0 22 4e 6f 74 55 73 65 64 5f 31 33 34 22 2c 0a 20 "NotUsed_134",.
ba6b0 20 20 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22 4e /* 135 */ "N
ba6c0 6f 74 55 73 65 64 5f 31 33 35 22 2c 0a 20 20 20 otUsed_135",.
ba6d0 20 20 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 6f 74 /* 136 */ "Not
ba6e0 55 73 65 64 5f 31 33 36 22 2c 0a 20 20 20 20 20 Used_136",.
ba6f0 2f 2a 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55 73 /* 137 */ "NotUs
ba700 65 64 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f 2a ed_137",. /*
ba710 20 31 33 38 20 2a 2f 20 22 4e 6f 74 55 73 65 64 138 */ "NotUsed
ba720 5f 31 33 38 22 2c 0a 20 20 20 20 20 2f 2a 20 31 _138",. /* 1
ba730 33 39 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 39 */ "NotUsed_1
ba740 33 39 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 30 39",. /* 140
ba750 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 34 30 */ "NotUsed_140
ba760 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 31 20 2a ",. /* 141 *
ba770 2f 20 22 54 6f 54 65 78 74 22 2c 0a 20 20 20 20 / "ToText",.
ba780 20 2f 2a 20 31 34 32 20 2a 2f 20 22 54 6f 42 6c /* 142 */ "ToBl
ba790 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 33 ob",. /* 143
ba7a0 20 2a 2f 20 22 54 6f 4e 75 6d 65 72 69 63 22 2c */ "ToNumeric",
ba7b0 0a 20 20 20 20 20 2f 2a 20 31 34 34 20 2a 2f 20 . /* 144 */
ba7c0 22 54 6f 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a "ToInt",. /*
ba7d0 20 31 34 35 20 2a 2f 20 22 54 6f 52 65 61 6c 22 145 */ "ToReal"
ba7e0 2c 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 ,. };. return
ba7f0 61 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 6e azName[i];.}.#en
ba800 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dif../**********
ba810 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f **** End of opco
ba820 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a des.c **********
ba830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba850 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
ba860 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
ba870 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a os_os2.c *******
ba880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba8a0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 ***/./*.** 2006
ba8b0 46 65 62 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 Feb 14.**.** The
ba8c0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
ba8d0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
ba8e0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
ba8f0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
ba900 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
ba910 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
ba920 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
ba930 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
ba940 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
ba950 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
ba960 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
ba970 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
ba980 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
ba990 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
ba9a0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
ba9b0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
ba9c0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
ba9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
baa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
baa10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
baa20 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
baa30 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 ins code that is
baa40 20 73 70 65 63 69 66 69 63 20 74 6f 20 4f 53 2f specific to OS/
baa50 32 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2..**.** $Id: os
baa60 5f 6f 73 32 2e 63 2c 76 20 31 2e 36 33 20 32 30 _os2.c,v 1.63 20
baa70 30 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 08/12/10 19:26:2
baa80 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 4 drh Exp $.*/..
baa90 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f .#if SQLITE_OS_O
baaa0 53 32 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 S2../*.** A Note
baab0 20 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c About Memory Al
baac0 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 location:.**.**
baad0 54 68 69 73 20 64 72 69 76 65 72 20 75 73 65 73 This driver uses
baae0 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 malloc()/free()
baaf0 20 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 72 directly rather
bab00 20 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f than going thro
bab10 75 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 ugh.** the SQLit
bab20 65 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69 74 e-wrappers sqlit
bab30 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 e3_malloc()/sqli
bab40 74 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68 6f te3_free(). Tho
bab50 73 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 se wrappers.** a
bab60 72 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 re designed for
bab70 75 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 use on embedded
bab80 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d 65 systems where me
bab90 6d 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 61 mory is scarce a
baba0 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 nd.** malloc fai
babb0 6c 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 65 lures happen fre
babc0 71 75 65 6e 74 6c 79 2e 20 20 4f 53 2f 32 20 64 quently. OS/2 d
babd0 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c oes not typicall
babe0 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 y run on.** embe
babf0 64 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e dded systems, an
bac00 64 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74 d when it does t
bac10 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f he developers no
bac20 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67 rmally have bigg
bac30 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74 er.** problems t
bac40 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68 o worry about th
bac50 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f an running out o
bac60 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68 f memory. So th
bac70 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 ere is not.** a
bac80 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20 compelling need
bac90 74 6f 20 75 73 65 20 74 68 65 20 77 72 61 70 70 to use the wrapp
baca0 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 ers..**.** But t
bacb0 68 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72 here is a good r
bacc0 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65 eason to not use
bacd0 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 20 20 the wrappers.
bace0 49 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a If we use the.**
bacf0 20 77 72 61 70 70 65 72 73 20 74 68 65 6e 20 77 wrappers then w
bad00 65 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c e will get simul
bad10 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61 ated malloc() fa
bad20 69 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74 68 ilures within th
bad30 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41 is.** driver. A
bad40 6e 64 20 74 68 61 74 20 63 61 75 73 65 73 20 61 nd that causes a
bad50 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62 ll kinds of prob
bad60 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73 lems for our tes
bad70 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 ts. We.** could
bad80 20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20 enhance SQLite
bad90 74 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d to deal with sim
bada0 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61 ulated malloc fa
badb0 69 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a ilures within.**
badc0 20 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20 the OS driver,
badd0 62 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20 but the code to
bade0 64 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20 deal with those
badf0 66 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f failure would no
bae00 74 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65 t.** be exercise
bae10 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63 d on Linux (whic
bae20 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 h does not need
bae30 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74 to malloc() in t
bae40 68 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e he driver).** an
bae50 64 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61 d so we would ha
bae60 76 65 20 64 69 66 66 69 63 75 6c 74 79 20 77 72 ve difficulty wr
bae70 69 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 iting coverage t
bae80 65 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a ests for that.**
bae90 20 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74 code. Better t
baea0 6f 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65 o leave the code
baeb0 20 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a out, we think..
baec0 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20 **.** The point
baed0 6f 66 20 74 68 69 73 20 64 69 73 63 75 73 73 69 of this discussi
baee0 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 on is as follows
baef0 3a 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67 : When creating
baf00 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 a new.** OS lay
baf10 65 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64 er for an embedd
baf20 65 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f ed system, if yo
baf30 75 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 20 u use this file
baf40 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a as an example,.*
baf50 2a 20 61 76 6f 69 64 20 74 68 65 20 75 73 65 20 * avoid the use
baf60 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 of malloc()/free
baf70 28 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69 (). Those routi
baf80 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 4f nes work ok on O
baf90 53 2f 32 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 S/2.** desktops
bafa0 62 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 but not so well
bafb0 69 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 in embedded syst
bafc0 65 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d ems..*/../*.** M
bafd0 61 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65 acros used to de
bafe0 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 termine whether
baff0 6f 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 or not to use th
bb000 72 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 reads..*/.#if de
bb010 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 52 fined(SQLITE_THR
bb020 45 41 44 53 41 46 45 29 20 26 26 20 53 51 4c 49 EADSAFE) && SQLI
bb030 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 TE_THREADSAFE.#
bb040 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
bb050 32 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 2_THREADS 1.#end
bb060 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 if../*.** Includ
bb070 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 e code that is c
bb080 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f ommon to all os_
bb090 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a *.c files.*/./**
bb0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
bb0b0 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 lude os_common.h
bb0c0 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
bb0d0 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a f os_os2.c *****
bb0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
bb0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
bb100 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f in file os_commo
bb110 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.h ************
bb120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
bb140 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a ** 2004 May 22.*
bb150 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
bb160 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
bb170 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
bb180 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
bb190 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
bb1a0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
bb1b0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
bb1c0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
bb1d0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
bb1e0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
bb1f0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
bb200 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
bb210 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
bb220 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
bb230 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
bb240 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
bb250 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
bb260 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
bb270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb2b0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
bb2c0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 le contains macr
bb2d0 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 os and a little
bb2e0 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 bit of code that
bb2f0 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a is common to.**
bb300 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 all of the plat
bb310 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 form-specific fi
bb320 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 les (os_*.c) and
bb330 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e is #included in
bb340 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 to those.** file
bb350 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 s..**.** This fi
bb360 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e le should be #in
bb370 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 cluded by the os
bb380 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e _*.c files only.
bb390 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a It is not a.**
bb3a0 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 general purpose
bb3b0 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a header file..**
bb3c0 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d .** $Id: os_comm
bb3d0 6f 6e 2e 68 2c 76 20 31 2e 33 38 20 32 30 30 39 on.h,v 1.38 2009
bb3e0 2f 30 32 2f 32 34 20 31 38 3a 34 30 3a 35 30 20 /02/24 18:40:50
bb3f0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
bb400 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53 $.*/.#ifndef _OS
bb410 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 _COMMON_H_.#defi
bb420 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f ne _OS_COMMON_H_
bb430 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 ../*.** At least
bb440 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 73 two bugs have s
bb450 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 73 lipped in becaus
bb460 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 65 e we changed the
bb470 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a MEMORY_DEBUG.**
bb480 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 macro to SQLITE
bb490 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 _DEBUG and some
bb4a0 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 older makefiles
bb4b0 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 have not yet mad
bb4c0 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e e the.** switch.
bb4d0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
bb4e0 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63 code should catc
bb4f0 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 h this problem a
bb500 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a t compile-time..
bb510 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 */.#ifdef MEMORY
bb520 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22 _DEBUG.# error "
bb530 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 The MEMORY_DEBUG
bb540 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 macro is obsole
bb550 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f te. Use SQLITE_
bb560 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a DEBUG instead.".
bb570 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
bb580 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 QLITE_DEBUG.SQLI
bb590 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
bb5a0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d 20 qlite3OSTrace =
bb5b0 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 0;.#define OSTRA
bb5c0 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20 69 CE1(X) i
bb5d0 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 f( sqlite3OSTrac
bb5e0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 e ) sqlite3Debug
bb5f0 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e Printf(X).#defin
bb600 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20 e OSTRACE2(X,Y)
bb610 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
bb620 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 3OSTrace ) sqlit
bb630 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
bb640 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 Y).#define OSTRA
bb650 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 CE3(X,Y,Z) i
bb660 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 f( sqlite3OSTrac
bb670 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 e ) sqlite3Debug
bb680 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 Printf(X,Y,Z).#d
bb690 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 efine OSTRACE4(X
bb6a0 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 71 ,Y,Z,A) if( sq
bb6b0 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 lite3OSTrace ) s
bb6c0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
bb6d0 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 f(X,Y,Z,A).#defi
bb6e0 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c ne OSTRACE5(X,Y,
bb6f0 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74 Z,A,B) if( sqlit
bb700 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
bb710 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
bb720 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e ,Y,Z,A,B).#defin
bb730 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a e OSTRACE6(X,Y,Z
bb740 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 ,A,B,C) \. if
bb750 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 (sqlite3OSTrace)
bb760 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
bb770 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 ntf(X,Y,Z,A,B,C)
bb780 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
bb790 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 7(X,Y,Z,A,B,C,D)
bb7a0 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 \. if(sqlite
bb7b0 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 3OSTrace) sqlite
bb7c0 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 3DebugPrintf(X,Y
bb7d0 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 ,Z,A,B,C,D).#els
bb7e0 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 e.#define OSTRAC
bb7f0 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 E1(X).#define OS
bb800 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 66 TRACE2(X,Y).#def
bb810 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 ine OSTRACE3(X,Y
bb820 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,Z).#define OSTR
bb830 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 ACE4(X,Y,Z,A).#d
bb840 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 efine OSTRACE5(X
bb850 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e ,Y,Z,A,B).#defin
bb860 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a e OSTRACE6(X,Y,Z
bb870 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 ,A,B,C).#define
bb880 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 OSTRACE7(X,Y,Z,A
bb890 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a ,B,C,D).#endif..
bb8a0 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 /*.** Macros for
bb8b0 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 performance tra
bb8c0 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 cing. Normally
bb8d0 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c turned off. Onl
bb8e0 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 y works.** on i4
bb8f0 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 86 hardware..*/.
bb900 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45 #ifdef SQLITE_PE
bb910 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a RFORMANCE_TRACE.
bb920 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 ./* .** hwtime.h
bb930 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 contains inline
bb940 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 assembler code
bb950 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 for implementing
bb960 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 .** high-perfor
bb970 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 mance timing rou
bb980 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a tines..*/./*****
bb990 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
bb9a0 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 e hwtime.h in th
bb9b0 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 e middle of os_c
bb9c0 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a ommon.h ********
bb9d0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
bb9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
bb9f0 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a file hwtime.h **
bba00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bba10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bba20 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
bba30 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2008 May 27.**.*
bba40 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
bba50 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
bba60 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
bba70 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
bba80 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
bba90 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
bbaa0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
bbab0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
bbac0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
bbad0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
bbae0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
bbaf0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
bbb00 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
bbb10 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
bbb20 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
bbb30 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
bbb40 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
bbb50 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
bbb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
bbba0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
bbbb0 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 contains inline
bbbc0 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74 asm code for ret
bbbd0 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65 rieving "high-pe
bbbe0 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f rformance".** co
bbbf0 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 63 unters for x86 c
bbc00 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a lass CPUs..**.**
bbc10 20 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 $Id: hwtime.h,v
bbc20 20 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 20 1.3 2008/08/01
bbc30 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 45 14:33:15 shane E
bbc40 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 xp $.*/.#ifndef
bbc50 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 _HWTIME_H_.#defi
bbc60 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f ne _HWTIME_H_../
bbc70 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
bbc80 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 ng routine only
bbc90 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d works on pentium
bbca0 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 72 -class (or newer
bbcb0 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a ) processors..**
bbcc0 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44 54 It uses the RDT
bbcd0 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 SC opcode to rea
bbce0 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 6e d the cycle coun
bbcf0 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 t value out of t
bbd00 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 he.** processor
bbd10 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 74 and returns that
bbd20 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 61 value. This ca
bbd30 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 69 n be used for hi
bbd40 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c gh-res.** profil
bbd50 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 ing..*/.#if (def
bbd60 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c ined(__GNUC__) |
bbd70 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 | defined(_MSC_V
bbd80 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 20 ER)) && \.
bbd90 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c (defined(i386) |
bbda0 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 | defined(__i386
bbdb0 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f __) || defined(_
bbdc0 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 20 M_IX86)).. #if
bbdd0 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
bbde0 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 ).. __inline__
bbdf0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 sqlite_uint64 sq
bbe00 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
bbe10 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 ){. unsigned
bbe20 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 int lo, hi;.
bbe30 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 __asm__ __vola
bbe40 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 tile__ ("rdtsc"
bbe50 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 : "=a" (lo), "=d
bbe60 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 65 " (hi));. re
bbe70 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e turn (sqlite_uin
bbe80 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c t64)hi << 32 | l
bbe90 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 o;. }.. #elif
bbea0 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 defined(_MSC_VER
bbeb0 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 ).. __declspec(
bbec0 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 naked) __inline
bbed0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f sqlite_uint64 __
bbee0 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 74 cdecl sqlite3Hwt
bbef0 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 ime(void){.
bbf00 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 __asm {.
bbf10 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 65 rdtsc. re
bbf20 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 6e t ; return
bbf30 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 41 value at EDX:EA
bbf40 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 X. }. }..
bbf50 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 #endif..#elif (d
bbf60 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 efined(__GNUC__)
bbf70 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 && defined(__x8
bbf80 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6_64__)).. __in
bbf90 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 line__ sqlite_ui
bbfa0 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 nt64 sqlite3Hwti
bbfb0 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 me(void){.
bbfc0 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 unsigned long va
bbfd0 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f l;. __asm__
bbfe0 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 __volatile__ ("
bbff0 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 76 rdtsc" : "=A" (v
bc000 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 al));. retu
bc010 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 rn val;. }. .#e
bc020 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 lif (defined(__G
bc030 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 NUC__) && define
bc040 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f d(__ppc__)).. _
bc050 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 _inline__ sqlite
bc060 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 _uint64 sqlite3H
bc070 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 wtime(void){.
bc080 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 unsigned long
bc090 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 long retval;.
bc0a0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e unsigned lon
bc0b0 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f g junk;. __
bc0c0 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 asm__ __volatile
bc0d0 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 __ ("\n\.
bc0e0 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75 1: mftbu
bc0f0 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 %1\n\.
bc100 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 20 mftb
bc110 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 %L0\n\.
bc120 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 mftb
bc130 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 u %0\n\.
bc140 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 77 cmpw
bc150 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 %0,%1\n\.
bc160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 b
bc170 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 20 ne 1b".
bc180 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 22 : "
bc190 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 3d =r" (retval), "=
bc1a0 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 r" (junk));.
bc1b0 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c 3b return retval;
bc1c0 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 . }..#else.. #
bc1d0 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 error Need imple
bc1e0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c mentation of sql
bc1f0 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 ite3Hwtime() for
bc200 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a your platform..
bc210 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f . /*. ** To co
bc220 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 6d mpile without im
bc230 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 plementing sqlit
bc240 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 e3Hwtime() for y
bc250 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 our platform,.
bc260 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 ** you can remov
bc270 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 72 e the above #err
bc280 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 66 or and use the f
bc290 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 ollowing. ** st
bc2a0 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f ub function. Yo
bc2b0 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 u will lose timi
bc2c0 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d ng support for m
bc2d0 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 any. ** of the
bc2e0 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65 debugging and te
bc2f0 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c sting utilities,
bc300 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61 but it should a
bc310 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d t. ** least com
bc320 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 pile and run..
bc330 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
bc340 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 E sqlite_uint6
bc350 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 4 sqlite3Hwtime(
bc360 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 28 void){ return ((
bc370 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 sqlite_uint64)0)
bc380 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e ; }..#endif..#en
bc390 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 dif /* !defined(
bc3a0 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a _HWTIME_H_) */..
bc3b0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
bc3c0 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 End of hwtime.h
bc3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
bc400 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
bc410 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
bc420 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
bc430 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a os_common.h ****
bc440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
bc450 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 .static sqlite_u
bc460 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 int64 g_start;.s
bc470 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e tatic sqlite_uin
bc480 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 t64 g_elapsed;.#
bc490 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 define TIMER_STA
bc4a0 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 74 RT g_start
bc4b0 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 =sqlite3Hwtime()
bc4c0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 .#define TIMER_E
bc4d0 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c 61 ND g_ela
bc4e0 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74 69 psed=sqlite3Hwti
bc4f0 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 me()-g_start.#de
bc500 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 fine TIMER_ELAPS
bc510 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 ED g_elapsed
bc520 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 .#else.#define T
bc530 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 69 IMER_START.#defi
bc540 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 ne TIMER_END.#de
bc550 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 fine TIMER_ELAPS
bc560 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 5f ED ((sqlite_
bc570 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 66 uint64)0).#endif
bc580 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f ../*.** If we co
bc590 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 53 mpile with the S
bc5a0 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 6f QLITE_TEST macro
bc5b0 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 66 set, then the f
bc5c0 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a ollowing block.*
bc5d0 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 * of code will g
bc5e0 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c 69 ive us the abili
bc5f0 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 ty to simulate a
bc600 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e disk I/O error.
bc610 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 This.** is use
bc620 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 68 d for testing th
bc630 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c e I/O recovery l
bc640 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 ogic..*/.#ifdef
bc650 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 SQLITE_TEST.SQLI
bc660 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
bc670 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 e3_io_error_hit
bc680 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
bc690 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 /* Total number
bc6a0 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f of I/O Errors */
bc6b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
bc6c0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
bc6d0 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 20 _hardhit = 0;
bc6e0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
bc6f0 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 f non-benign err
bc700 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ors */.SQLITE_AP
bc710 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f I int sqlite3_io
bc720 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d _error_pending =
bc730 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 0; /* Co
bc740 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 unt down to firs
bc750 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 t I/O error */.S
bc760 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
bc770 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
bc780 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20 ersist = 0;
bc790 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f /* True if I/
bc7a0 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 74 O errors persist
bc7b0 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
bc7c0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
bc7d0 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 ror_benign = 0;
bc7e0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
bc7f0 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 65 if errors are be
bc800 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 nign */.SQLITE_A
bc810 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 PI int sqlite3_d
bc820 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 iskfull_pending
bc830 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 = 0;.SQLITE_API
bc840 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b int sqlite3_disk
bc850 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e full = 0;.#defin
bc860 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f e SimulateIOErro
bc870 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 rBenign(X) sqlit
bc880 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 e3_io_error_beni
bc890 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 53 gn=(X).#define S
bc8a0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 imulateIOError(C
bc8b0 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 73 ODE) \. if( (s
bc8c0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
bc8d0 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 74 persist && sqlit
bc8e0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 e3_io_error_hit)
bc8f0 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c \. || sql
bc900 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 ite3_io_error_pe
bc910 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 nding-- == 1 )
bc920 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
bc930 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b { local_ioerr();
bc940 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 CODE; }.static
bc950 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 void local_ioerr
bc960 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 (){. IOTRACE(("
bc970 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 IOERR\n"));. sq
bc980 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 lite3_io_error_h
bc990 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c it++;. if( !sql
bc9a0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 ite3_io_error_be
bc9b0 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 nign ) sqlite3_i
bc9c0 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 2b o_error_hardhit+
bc9d0 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d +;.}.#define Sim
bc9e0 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 ulateDiskfullErr
bc9f0 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 or(CODE) \. if
bca00 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 ( sqlite3_diskfu
bca10 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a ll_pending ){ \.
bca20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
bca30 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e _diskfull_pendin
bca40 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 g == 1 ){ \.
bca50 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 local_ioerr()
bca60 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 ; \. sqlit
bca70 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b e3_diskfull = 1;
bca80 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 \. sqlite
bca90 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 3_io_error_hit =
bcaa0 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 1; \. COD
bcab0 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b E; \. }else{
bcac0 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 \. sqlite
bcad0 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 3_diskfull_pendi
bcae0 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c ng--; \. } \
bcaf0 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 . }.#else.#def
bcb00 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ine SimulateIOEr
bcb10 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 rorBenign(X).#de
bcb20 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 fine SimulateIOE
bcb30 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 20 rror(A).#define
bcb40 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c SimulateDiskfull
bcb50 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a Error(A).#endif.
bcb60 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 ./*.** When test
bcb70 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e ing, keep a coun
bcb80 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 t of the number
bcb90 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a of open files..*
bcba0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
bcbb0 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 TEST.SQLITE_API
bcbc0 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e int sqlite3_open
bcbd0 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b _file_count = 0;
bcbe0 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 .#define OpenCou
bcbf0 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 33 nter(X) sqlite3
bcc00 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 _open_file_count
bcc10 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 +=(X).#else.#def
bcc20 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 ine OpenCounter(
bcc30 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 X).#endif..#endi
bcc40 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f f /* !defined(_O
bcc50 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a S_COMMON_H_) */.
bcc60 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
bcc70 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f End of os_commo
bcc80 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.h ************
bcc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
bccb0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
bccc0 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
bccd0 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
bcce0 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a os_os2.c ******
bccf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
bcd00 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 73 32 46 ../*.** The os2F
bcd10 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 ile structure is
bcd20 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c subclass of sql
bcd30 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 66 ite3_file specif
bcd40 69 63 20 66 6f 72 20 74 68 65 20 4f 53 2f 32 0a ic for the OS/2.
bcd50 2a 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20 6c ** protability l
bcd60 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ayer..*/.typedef
bcd70 20 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 20 struct os2File
bcd80 6f 73 32 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 os2File;.struct
bcd90 6f 73 32 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 os2File {. cons
bcda0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
bcdb0 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 hods *pMethod;
bcdc0 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66 69 /* Always the fi
bcdd0 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 48 rst entry */. H
bcde0 46 49 4c 45 20 68 3b 20 20 20 20 20 20 20 20 20 FILE h;
bcdf0 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 /* Hand
bce00 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 le for accessing
bce10 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 the file */. c
bce20 68 61 72 2a 20 70 61 74 68 54 6f 44 65 6c 3b 20 har* pathToDel;
bce30 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
bce40 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 of file to dele
bce50 74 65 20 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55 4c te on close, NUL
bce60 4c 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 75 6e L if not */. un
bce70 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b signed char lock
bce80 74 79 70 65 3b 20 20 20 2f 2a 20 54 79 70 65 20 type; /* Type
bce90 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c of lock currentl
bcea0 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 y held on this f
bceb0 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 ile */.};..#defi
bcec0 6e 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 ne LOCK_TIMEOUT
bced0 31 30 4c 20 2f 2a 20 74 68 65 20 64 65 66 61 75 10L /* the defau
bcee0 6c 74 20 6c 6f 63 6b 69 6e 67 20 74 69 6d 65 6f lt locking timeo
bcef0 75 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ut */../********
bcf00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcf10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcf20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcf30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcf40 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 *****.** The nex
bcf50 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 t group of routi
bcf60 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 nes implement th
bcf70 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70 e I/O methods sp
bcf80 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 ecified.** by th
bcf90 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 e sqlite3_io_met
bcfa0 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a hods object..***
bcfb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
bd000 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 .** Close a file
bd010 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
bd020 6f 73 32 43 6c 6f 73 65 28 20 73 71 6c 69 74 65 os2Close( sqlite
bd030 33 5f 66 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20 3_file *id ){.
bd040 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 APIRET rc = NO_E
bd050 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65 20 RROR;. os2File
bd060 2a 70 46 69 6c 65 3b 0a 20 20 69 66 28 20 69 64 *pFile;. if( id
bd070 20 26 26 20 28 70 46 69 6c 65 20 3d 20 28 6f 73 && (pFile = (os
bd080 32 46 69 6c 65 2a 29 69 64 29 20 21 3d 20 30 20 2File*)id) != 0
bd090 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 ){. OSTRACE2(
bd0a0 20 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 "CLOSE %d\n", p
bd0b0 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 72 File->h );. r
bd0c0 63 20 3d 20 44 6f 73 43 6c 6f 73 65 28 20 70 46 c = DosClose( pF
bd0d0 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 70 46 ile->h );. pF
bd0e0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
bd0f0 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 69 66 28 NO_LOCK;. if(
bd100 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 pFile->pathToDe
bd110 6c 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20 20 l != NULL ){.
bd120 20 20 20 72 63 20 3d 20 44 6f 73 46 6f 72 63 65 rc = DosForce
bd130 44 65 6c 65 74 65 28 20 28 50 53 5a 29 70 46 69 Delete( (PSZ)pFi
bd140 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b le->pathToDel );
bd150 0a 20 20 20 20 20 20 66 72 65 65 28 20 70 46 69 . free( pFi
bd160 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b le->pathToDel );
bd170 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 . pFile->pa
bd180 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a thToDel = NULL;.
bd190 20 20 20 20 7d 0a 20 20 20 20 69 64 20 3d 20 30 }. id = 0
bd1a0 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 ;. OpenCounte
bd1b0 72 28 20 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20 20 r( -1 );. }..
bd1c0 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f return rc == NO_
bd1d0 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f ERROR ? SQLITE_O
bd1e0 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 K : SQLITE_IOERR
bd1f0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
bd200 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 data from a file
bd210 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 into a buffer.
bd220 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
bd230 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 K if all.** byte
bd240 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63 s were read succ
bd250 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c essfully and SQL
bd260 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 ITE_IOERR if any
bd270 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 thing goes.** wr
bd280 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ong..*/.static i
bd290 6e 74 20 6f 73 32 52 65 61 64 28 0a 20 20 73 71 nt os2Read(. sq
bd2a0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
bd2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
bd2c0 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 File to read fr
bd2d0 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 om */. void *pB
bd2e0 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 uf,
bd2f0 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 /* Write
bd300 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 content into th
bd310 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 is buffer */. i
bd320 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 nt amt,
bd330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
bd340 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
bd350 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 s to read */. s
bd360 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 qlite3_int64 off
bd370 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20 2f set /
bd380 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 * Begin reading
bd390 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a at this offset *
bd3a0 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c /.){. ULONG fil
bd3b0 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a eLocation = 0L;.
bd3c0 20 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20 6f ULONG got;. o
bd3d0 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 s2File *pFile =
bd3e0 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (os2File*)id;.
bd3f0 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b assert( id!=0 );
bd400 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
bd410 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 or( return SQLIT
bd420 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 29 3b 0a E_IOERR_READ );.
bd430 20 20 4f 53 54 52 41 43 45 33 28 20 22 52 45 41 OSTRACE3( "REA
bd440 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c D %d lock=%d\n",
bd450 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 pFile->h, pFile
bd460 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 ->locktype );.
bd470 69 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 74 if( DosSetFilePt
bd480 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 r(pFile->h, offs
bd490 65 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 et, FILE_BEGIN,
bd4a0 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21 &fileLocation) !
bd4b0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
bd4c0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
bd4d0 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 69 66 28 IOERR;. }. if(
bd4e0 20 44 6f 73 52 65 61 64 28 20 70 46 69 6c 65 2d DosRead( pFile-
bd4f0 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 >h, pBuf, amt, &
bd500 67 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f got ) != NO_ERRO
bd510 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 R ){. return
bd520 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 SQLITE_IOERR_REA
bd530 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 D;. }. if( got
bd540 20 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 20 29 == (ULONG)amt )
bd550 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
bd560 54 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b 0a TE_OK;. else {.
bd570 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 6f /* Unread po
bd580 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 69 6e rtions of the in
bd590 70 75 74 20 62 75 66 66 65 72 20 6d 75 73 74 20 put buffer must
bd5a0 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a be zero-filled *
bd5b0 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 /. memset(&((
bd5c0 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d char*)pBuf)[got]
bd5d0 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 , 0, amt-got);.
bd5e0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
bd5f0 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
bd600 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 D;. }.}../*.**
bd610 57 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d 20 Write data from
bd620 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 20 a buffer into a
bd630 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 file. Return SQ
bd640 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 LITE_OK on succe
bd650 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 ss.** or some ot
bd660 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 6f her error code o
bd670 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 n failure..*/.st
bd680 61 74 69 63 20 69 6e 74 20 6f 73 32 57 72 69 74 atic int os2Writ
bd690 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c e(. sqlite3_fil
bd6a0 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 e *id,
bd6b0 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 /* File to
bd6c0 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 write into */.
bd6d0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 const void *pBuf
bd6e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
bd6f0 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 /* The bytes to
bd700 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 be written */.
bd710 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 int amt,
bd720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bd730 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
bd740 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 es to write */.
bd750 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f sqlite3_int64 o
bd760 66 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20 ffset
bd770 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 /* Offset into
bd780 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67 69 the file to begi
bd790 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a n writing at */.
bd7a0 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 4c ){. ULONG fileL
bd7b0 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20 ocation = 0L;.
bd7c0 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 APIRET rc = NO_E
bd7d0 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 77 72 RROR;. ULONG wr
bd7e0 6f 74 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a ote;. os2File *
bd7f0 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 pFile = (os2File
bd800 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 *)id;. assert(
bd810 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c id!=0 );. Simul
bd820 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 ateIOError( retu
bd830 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
bd840 57 52 49 54 45 20 29 3b 0a 20 20 53 69 6d 75 6c WRITE );. Simul
bd850 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 ateDiskfullError
bd860 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
bd870 46 55 4c 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 FULL );. OSTRAC
bd880 45 33 28 20 22 57 52 49 54 45 20 25 64 20 6c 6f E3( "WRITE %d lo
bd890 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d ck=%d\n", pFile-
bd8a0 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 >h, pFile->lockt
bd8b0 79 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 ype );. if( Dos
bd8c0 53 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c 65 SetFilePtr(pFile
bd8d0 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c ->h, offset, FIL
bd8e0 45 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f E_BEGIN, &fileLo
bd8f0 63 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 cation) != NO_ER
bd900 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ROR ){. retur
bd910 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a n SQLITE_IOERR;.
bd920 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d }. assert( am
bd930 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 t>0 );. while(
bd940 61 6d 74 20 3e 20 30 20 26 26 0a 20 20 20 20 20 amt > 0 &&.
bd950 20 20 20 20 28 20 72 63 20 3d 20 44 6f 73 57 72 ( rc = DosWr
bd960 69 74 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20 28 ite( pFile->h, (
bd970 50 56 4f 49 44 29 70 42 75 66 2c 20 61 6d 74 2c PVOID)pBuf, amt,
bd980 20 26 77 72 6f 74 65 20 29 20 29 20 3d 3d 20 4e &wrote ) ) == N
bd990 4f 5f 45 52 52 4f 52 20 26 26 0a 20 20 20 20 20 O_ERROR &&.
bd9a0 20 20 20 20 77 72 6f 74 65 20 3e 20 30 0a 20 20 wrote > 0.
bd9b0 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 ){. amt -= wr
bd9c0 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 ote;. pBuf =
bd9d0 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 77 &((char*)pBuf)[w
bd9e0 72 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72 65 rote];. }.. re
bd9f0 74 75 72 6e 20 28 20 72 63 20 21 3d 20 4e 4f 5f turn ( rc != NO_
bda00 45 52 52 4f 52 20 7c 7c 20 61 6d 74 20 3e 20 28 ERROR || amt > (
bda10 69 6e 74 29 77 72 6f 74 65 20 29 20 3f 20 53 51 int)wrote ) ? SQ
bda20 4c 49 54 45 5f 46 55 4c 4c 20 3a 20 53 51 4c 49 LITE_FULL : SQLI
bda30 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
bda40 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e Truncate an open
bda50 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 file to a speci
bda60 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 fied size.*/.sta
bda70 74 69 63 20 69 6e 74 20 6f 73 32 54 72 75 6e 63 tic int os2Trunc
bda80 61 74 65 28 20 73 71 6c 69 74 65 33 5f 66 69 6c ate( sqlite3_fil
bda90 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65 e *id, i64 nByte
bdaa0 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 ){. APIRET rc
bdab0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 = NO_ERROR;. os
bdac0 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 2File *pFile = (
bdad0 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f os2File*)id;. O
bdae0 53 54 52 41 43 45 33 28 20 22 54 52 55 4e 43 41 STRACE3( "TRUNCA
bdaf0 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 TE %d %lld\n", p
bdb00 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 File->h, nByte )
bdb10 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
bdb20 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 ror( return SQLI
bdb30 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 TE_IOERR_TRUNCAT
bdb40 45 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 53 E );. rc = DosS
bdb50 65 74 46 69 6c 65 53 69 7a 65 28 20 70 46 69 6c etFileSize( pFil
bdb60 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20 e->h, nByte );.
bdb70 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f return rc == NO
bdb80 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f _ERROR ? SQLITE_
bdb90 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 OK : SQLITE_IOER
bdba0 52 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a 23 R_TRUNCATE;.}..#
bdbb0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
bdbc0 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 T./*.** Count th
bdbd0 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c e number of full
bdbe0 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c syncs and normal
bdbf0 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73 syncs. This is
bdc00 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a used to test.**
bdc10 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20 that syncs and
bdc20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 fullsyncs are oc
bdc30 63 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 69 curing at the ri
bdc40 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 ght times..*/.SQ
bdc50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
bdc60 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 ite3_sync_count
bdc70 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 = 0;.SQLITE_API
bdc80 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c int sqlite3_full
bdc90 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a sync_count = 0;.
bdca0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 #endif../*.** Ma
bdcb0 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 ke sure all writ
bdcc0 65 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c es to a particul
bdcd0 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d ar file are comm
bdce0 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a itted to disk..*
bdcf0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 /.static int os2
bdd00 53 79 6e 63 28 20 73 71 6c 69 74 65 33 5f 66 69 Sync( sqlite3_fi
bdd10 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 le *id, int flag
bdd20 73 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 2a s ){. os2File *
bdd30 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 pFile = (os2File
bdd40 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 *)id;. OSTRACE3
bdd50 28 20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d ( "SYNC %d lock=
bdd60 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
bdd70 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
bdd80 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );.#ifdef SQLIT
bdd90 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c 61 E_TEST. if( fla
bdda0 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 gs & SQLITE_SYNC
bddb0 5f 46 55 4c 4c 29 7b 0a 20 20 20 20 73 71 6c 69 _FULL){. sqli
bddc0 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 te3_fullsync_cou
bddd0 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 nt++;. }. sqli
bdde0 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b te3_sync_count++
bddf0 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 ;.#endif. /* If
bde00 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 we compiled wit
bde10 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f h the SQLITE_NO_
bde20 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 SYNC flag, then
bde30 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a syncing is a. *
bde40 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 * no-op. */.#if
bde50 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 def SQLITE_NO_SY
bde60 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 NC. UNUSED_PARA
bde70 4d 45 54 45 52 28 70 46 69 6c 65 29 3b 0a 20 20 METER(pFile);.
bde80 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
bde90 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e ;.#else. return
bdea0 20 44 6f 73 52 65 73 65 74 42 75 66 66 65 72 28 DosResetBuffer(
bdeb0 20 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d 20 4e pFile->h ) == N
bdec0 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 O_ERROR ? SQLITE
bded0 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 _OK : SQLITE_IOE
bdee0 52 52 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a RR;.#endif.}../*
bdef0 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 .** Determine th
bdf00 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f e current size o
bdf10 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 f a file in byte
bdf20 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 s.*/.static int
bdf30 6f 73 32 46 69 6c 65 53 69 7a 65 28 20 73 71 6c os2FileSize( sql
bdf40 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 ite3_file *id, s
bdf50 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 qlite3_int64 *pS
bdf60 69 7a 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20 ize ){. APIRET
bdf70 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 rc = NO_ERROR;.
bdf80 20 46 49 4c 45 53 54 41 54 55 53 33 20 66 73 74 FILESTATUS3 fst
bdf90 73 33 46 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d 65 s3FileInfo;. me
bdfa0 6d 73 65 74 28 26 66 73 74 73 33 46 69 6c 65 49 mset(&fsts3FileI
bdfb0 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 nfo, 0, sizeof(f
bdfc0 73 74 73 33 46 69 6c 65 49 6e 66 6f 29 29 3b 0a sts3FileInfo));.
bdfd0 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 assert( id!=0
bdfe0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
bdff0 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
be000 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 ITE_IOERR_FSTAT
be010 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 );. rc = DosQue
be020 72 79 46 69 6c 65 49 6e 66 6f 28 20 28 28 6f 73 ryFileInfo( ((os
be030 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 46 2File*)id)->h, F
be040 49 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26 66 73 IL_STANDARD, &fs
be050 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73 69 7a ts3FileInfo, siz
be060 65 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29 eof(FILESTATUS3)
be070 20 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 );. if( rc ==
be080 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
be090 2a 70 53 69 7a 65 20 3d 20 66 73 74 73 33 46 69 *pSize = fsts3Fi
be0a0 6c 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a 20 leInfo.cbFile;.
be0b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
be0c0 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
be0d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
be0e0 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d IOERR_FSTAT;. }
be0f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 .}../*.** Acquir
be100 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e e a reader lock.
be110 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 .*/.static int g
be120 65 74 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46 etReadLock( os2F
be130 69 6c 65 20 2a 70 46 69 6c 65 20 29 7b 0a 20 20 ile *pFile ){.
be140 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 FILELOCK LockAr
be150 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ea,.
be160 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50 UnlockArea;. AP
be170 49 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73 IRET res;. mems
be180 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c et(&LockArea, 0,
be190 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 sizeof(LockArea
be1a0 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e ));. memset(&Un
be1b0 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a lockArea, 0, siz
be1c0 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 eof(UnlockArea))
be1d0 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 ;. LockArea.lOf
be1e0 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 fset = SHARED_FI
be1f0 52 53 54 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e RST;. LockArea.
be200 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f lRange = SHARED_
be210 53 49 5a 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 SIZE;. UnlockAr
be220 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b ea.lOffset = 0L;
be230 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 . UnlockArea.lR
be240 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 72 65 73 ange = 0L;. res
be250 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 = DosSetFileLoc
be260 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 ks( pFile->h, &U
be270 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b nlockArea, &Lock
be280 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f Area, LOCK_TIMEO
be290 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52 UT, 1L );. OSTR
be2a0 41 43 45 33 28 20 22 47 45 54 52 45 41 44 4c 4f ACE3( "GETREADLO
be2b0 43 4b 20 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c CK %d res=%d\n",
be2c0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 pFile->h, res )
be2d0 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a ;. return res;.
be2e0 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 }../*.** Undo a
be2f0 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 readlock.*/.stat
be300 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 ic int unlockRea
be310 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a dLock( os2File *
be320 69 64 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b id ){. FILELOCK
be330 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 LockArea,.
be340 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 UnlockAr
be350 65 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73 ea;. APIRET res
be360 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b ;. memset(&Lock
be370 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Area, 0, sizeof(
be380 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 LockArea));. me
be390 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 mset(&UnlockArea
be3a0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f , 0, sizeof(Unlo
be3b0 63 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b ckArea));. Lock
be3c0 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 Area.lOffset = 0
be3d0 4c 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 L;. LockArea.lR
be3e0 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c ange = 0L;. Unl
be3f0 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
be400 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a = SHARED_FIRST;.
be410 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 UnlockArea.lRa
be420 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a nge = SHARED_SIZ
be430 45 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53 65 E;. res = DosSe
be440 74 46 69 6c 65 4c 6f 63 6b 73 28 20 69 64 2d 3e tFileLocks( id->
be450 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 h, &UnlockArea,
be460 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f &LockArea, LOCK_
be470 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 TIMEOUT, 1L );.
be480 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f OSTRACE3( "UNLO
be490 43 4b 2d 52 45 41 44 4c 4f 43 4b 20 66 69 6c 65 CK-READLOCK file
be4a0 20 68 61 6e 64 6c 65 3d 25 64 20 72 65 73 3d 25 handle=%d res=%
be4b0 64 3f 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 65 d?\n", id->h, re
be4c0 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 s );. return re
be4d0 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b s;.}../*.** Lock
be4e0 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 the file with t
be4f0 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 he lock specifie
be500 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c d by parameter l
be510 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a ocktype - one.**
be520 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
be530 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 g:.**.** (1)
be540 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 SHARED_LOCK.**
be550 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 (2) RESERVED
be560 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 _LOCK.** (3)
be570 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a PENDING_LOCK.**
be580 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 (4) EXCLUSI
be590 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f VE_LOCK.**.** So
be5a0 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 metimes when req
be5b0 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b uesting one lock
be5c0 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e state, addition
be5d0 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a al lock states.*
be5e0 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 * are inserted i
be5f0 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 n between. The
be600 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 locking might fa
be610 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 il on one of the
be620 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 later.** transi
be630 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 tions leaving th
be640 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 e lock state dif
be650 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 ferent from what
be660 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a it started but.
be670 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f ** still short o
be680 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 f its goal. The
be690 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 following chart
be6a0 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 shows the allow
be6b0 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e ed.** transition
be6c0 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 s and the insert
be6d0 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 ed intermediate
be6e0 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 states:.**.**
be6f0 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 UNLOCKED -> SHA
be700 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 RED.** SHARED
be710 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 -> RESERVED.**
be720 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 SHARED -> (PE
be730 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
be740 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 IVE.** RESERV
be750 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 ED -> (PENDING)
be760 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 -> EXCLUSIVE.**
be770 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 PENDING -> EX
be780 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 CLUSIVE.**.** Th
be790 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
be7a0 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 only increase a
be7b0 6c 6f 63 6b 2e 20 20 54 68 65 20 6f 73 32 55 6e lock. The os2Un
be7c0 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a lock() routine.*
be7d0 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 * erases all loc
be7e0 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 ks at once and r
be7f0 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 eturns us immedi
be800 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 ately to locking
be810 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 level 0..** It
be820 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
be830 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 to lower the loc
be840 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 king level one s
be850 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20 20 tep at a time.
be860 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 You.** must go s
be870 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 traight to locki
be880 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 ng level 0..*/.s
be890 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4c 6f 63 tatic int os2Loc
be8a0 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 k( sqlite3_file
be8b0 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 *id, int locktyp
be8c0 65 20 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 e ){. int rc =
be8d0 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
be8e0 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
be8f0 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65 73 from subroutines
be900 20 2a 2f 0a 20 20 41 50 49 52 45 54 20 72 65 73 */. APIRET res
be910 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 20 = NO_ERROR;
be920 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 /* Result of an
be930 4f 53 2f 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a OS/2 lock call *
be940 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 /. int newLockt
be950 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 ype; /* Se
be960 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 t pFile->locktyp
be970 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20 e to this value
be980 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a before exiting *
be990 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 69 /. int gotPendi
be9a0 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 ngLock = 0;/* Tr
be9b0 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72 65 ue if we acquire
be9c0 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b d a PENDING lock
be9d0 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20 this time */.
be9e0 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 FILELOCK LockAr
be9f0 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ea,.
bea00 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f 73 UnlockArea;. os
bea10 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 2File *pFile = (
bea20 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 6d os2File*)id;. m
bea30 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c emset(&LockArea,
bea40 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 0, sizeof(LockA
bea50 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 rea));. memset(
bea60 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 &UnlockArea, 0,
bea70 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 sizeof(UnlockAre
bea80 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 a));. assert( p
bea90 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53 54 File!=0 );. OST
beaa0 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 25 64 20 RACE4( "LOCK %d
beab0 25 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 %d was %d\n", pF
beac0 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 ile->h, locktype
bead0 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 , pFile->locktyp
beae0 65 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 e );.. /* If th
beaf0 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 ere is already a
beb00 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 lock of this ty
beb10 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 pe or more restr
beb20 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 ictive on the.
beb30 2a 2a 20 6f 73 32 46 69 6c 65 2c 20 64 6f 20 6e ** os2File, do n
beb40 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 othing. Don't us
beb50 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 e the end_lock:
beb60 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 exit path, as.
beb70 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ** sqlite3_mutex
beb80 5f 65 6e 74 65 72 28 29 20 68 61 73 6e 27 74 20 _enter() hasn't
beb90 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e been called yet.
beba0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c . */. if( pFil
bebb0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 e->locktype>=loc
bebc0 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 ktype ){. OST
bebd0 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 RACE3( "LOCK %d
bebe0 25 64 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 %d ok (already h
bebf0 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e eld)\n", pFile->
bec00 68 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 h, locktype );.
bec10 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
bec20 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d _OK;. }.. /* M
bec30 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 ake sure the loc
bec40 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 king sequence is
bec50 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 correct. */.
bec60 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
bec70 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b ocktype!=NO_LOCK
bec80 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 || locktype==SH
bec90 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 ARED_LOCK );. a
beca0 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 ssert( locktype!
becb0 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b =PENDING_LOCK );
becc0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
becd0 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f ype!=RESERVED_LO
bece0 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 CK || pFile->loc
becf0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
bed00 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b CK );.. /* Lock
bed10 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 the PENDING_LOC
bed20 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 65 K byte if we nee
bed30 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 50 d to acquire a P
bed40 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 ENDING lock or.
bed50 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63 ** a SHARED loc
bed60 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 63 k. If we are ac
bed70 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 quiring a SHARED
bed80 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69 lock, the acqui
bed90 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 sition of. ** t
beda0 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 he PENDING_LOCK
bedb0 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72 byte is temporar
bedc0 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 y.. */. newLoc
bedd0 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c ktype = pFile->l
bede0 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 70 ocktype;. if( p
bedf0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
bee00 4e 4f 5f 4c 4f 43 4b 0a 20 20 20 20 20 20 7c 7c NO_LOCK. ||
bee10 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c (locktype==EXCL
bee20 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 USIVE_LOCK && pF
bee30 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 ile->locktype==R
bee40 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 20 ESERVED_LOCK).
bee50 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e ){. LockArea.
bee60 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e lOffset = PENDIN
bee70 47 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b G_BYTE;. Lock
bee80 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c Area.lRange = 1L
bee90 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ;. UnlockArea
beea0 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 .lOffset = 0L;.
beeb0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 UnlockArea.lR
beec0 61 6e 67 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 20 ange = 0L;..
beed0 2f 2a 20 77 61 69 74 20 6c 6f 6e 67 65 72 20 74 /* wait longer t
beee0 68 61 6e 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 han LOCK_TIMEOUT
beef0 20 68 65 72 65 20 6e 6f 74 20 74 6f 20 68 61 76 here not to hav
bef00 65 20 74 6f 20 74 72 79 20 6d 75 6c 74 69 70 6c e to try multipl
bef10 65 20 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 72 e times */. r
bef20 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c es = DosSetFileL
bef30 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 ocks( pFile->h,
bef40 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f &UnlockArea, &Lo
bef50 63 6b 41 72 65 61 2c 20 31 30 30 4c 2c 20 30 4c ckArea, 100L, 0L
bef60 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 );. if( res
bef70 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 == NO_ERROR ){.
bef80 20 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c gotPendingL
bef90 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 4f ock = 1;. O
befa0 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 STRACE3( "LOCK %
befb0 64 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 d pending lock b
befc0 6f 6f 6c 65 61 6e 20 73 65 74 2e 20 20 72 65 73 oolean set. res
befd0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
befe0 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a 20 , res );. }.
beff0 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 }.. /* Acquire
bf000 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 a shared lock.
bf010 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 */. if( lockty
bf020 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
bf030 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 && res == NO_ERR
bf040 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 OR ){. assert
bf050 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
bf060 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 e==NO_LOCK );.
bf070 20 20 72 65 73 20 3d 20 67 65 74 52 65 61 64 4c res = getReadL
bf080 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 ock(pFile);.
bf090 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 if( res == NO_ER
bf0a0 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 ROR ){. new
bf0b0 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 Locktype = SHARE
bf0c0 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 D_LOCK;. }.
bf0d0 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 OSTRACE3( "LOC
bf0e0 4b 20 25 64 20 61 63 71 75 69 72 65 20 73 68 61 K %d acquire sha
bf0f0 72 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 red lock. res=%d
bf100 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
bf110 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 es );. }.. /*
bf120 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56 Acquire a RESERV
bf130 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 ED lock. */. i
bf140 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 f( locktype==RES
bf150 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 ERVED_LOCK && re
bf160 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b s == NO_ERROR ){
bf170 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 . assert( pFi
bf180 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 le->locktype==SH
bf190 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 ARED_LOCK );.
bf1a0 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 LockArea.lOffse
bf1b0 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 t = RESERVED_BYT
bf1c0 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e E;. LockArea.
bf1d0 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 lRange = 1L;.
bf1e0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 UnlockArea.lOff
bf1f0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e set = 0L;. Un
bf200 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 lockArea.lRange
bf210 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 = 0L;. res =
bf220 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
bf230 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
bf240 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
bf250 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
bf260 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 0L );. if( r
bf270 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 es == NO_ERROR )
bf280 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 {. newLockt
bf290 79 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f 4c ype = RESERVED_L
bf2a0 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f OCK;. }. O
bf2b0 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 STRACE3( "LOCK %
bf2c0 64 20 61 63 71 75 69 72 65 20 72 65 73 65 72 76 d acquire reserv
bf2d0 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c ed lock. res=%d\
bf2e0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 n", pFile->h, re
bf2f0 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 s );. }.. /* A
bf300 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 cquire a PENDING
bf310 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 lock. */. if(
bf320 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 locktype==EXCLU
bf330 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 SIVE_LOCK && res
bf340 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a == NO_ERROR ){.
bf350 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 newLocktype
bf360 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a = PENDING_LOCK;.
bf370 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f gotPendingLo
bf380 63 6b 20 3d 20 30 3b 0a 20 20 20 20 4f 53 54 52 ck = 0;. OSTR
bf390 41 43 45 32 28 20 22 4c 4f 43 4b 20 25 64 20 61 ACE2( "LOCK %d a
bf3a0 63 71 75 69 72 65 20 70 65 6e 64 69 6e 67 20 6c cquire pending l
bf3b0 6f 63 6b 2e 20 70 65 6e 64 69 6e 67 20 6c 6f 63 ock. pending loc
bf3c0 6b 20 62 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 2e k boolean unset.
bf3d0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b \n", pFile->h );
bf3e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 . }.. /* Acqui
bf3f0 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 re an EXCLUSIVE
bf400 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 lock. */. if(
bf410 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 locktype==EXCLUS
bf420 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 IVE_LOCK && res
bf430 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 == NO_ERROR ){.
bf440 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 assert( pFile
bf450 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 ->locktype>=SHAR
bf460 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 ED_LOCK );. r
bf470 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c es = unlockReadL
bf480 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 ock(pFile);.
bf490 4f 53 54 52 41 43 45 32 28 20 22 75 6e 72 65 61 OSTRACE2( "unrea
bf4a0 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 dlock = %d\n", r
bf4b0 65 73 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 es );. LockAr
bf4c0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 ea.lOffset = SHA
bf4d0 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 4c RED_FIRST;. L
bf4e0 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
bf4f0 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 SHARED_SIZE;.
bf500 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 UnlockArea.lOf
bf510 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 fset = 0L;. U
bf520 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
bf530 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d = 0L;. res =
bf540 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
bf550 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
bf560 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
bf570 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ea, LOCK_TIMEOUT
bf580 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 , 0L );. if(
bf590 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 res == NO_ERROR
bf5a0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b ){. newLock
bf5b0 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 type = EXCLUSIVE
bf5c0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 _LOCK;. }else
bf5d0 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 {. OSTRACE2
bf5e0 28 20 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63 6f ( "OS/2 error-co
bf5f0 64 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 de = %d\n", res
bf600 29 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61 64 );. getRead
bf610 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 Lock(pFile);.
bf620 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 }. OSTRACE3(
bf630 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72 "LOCK %d acquir
bf640 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b e exclusive lock
bf650 2e 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 . res=%d\n", pF
bf660 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 ile->h, res );.
bf670 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 }.. /* If we a
bf680 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e re holding a PEN
bf690 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f DING lock that o
bf6a0 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 61 ught to be relea
bf6b0 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 sed, then. ** r
bf6c0 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 elease it now..
bf6d0 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 6e */. if( gotPen
bf6e0 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b dingLock && lock
bf6f0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
bf700 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 3b 0a K ){. int r;.
bf710 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 LockArea.lOf
bf720 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c fset = 0L;. L
bf730 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
bf740 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 0L;. UnlockA
bf750 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 rea.lOffset = PE
bf760 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 NDING_BYTE;.
bf770 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 UnlockArea.lRang
bf780 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 20 3d 20 e = 1L;. r =
bf790 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
bf7a0 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
bf7b0 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
bf7c0 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
bf7d0 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 0L );. OSTRA
bf7e0 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 75 6e CE3( "LOCK %d un
bf7f0 6c 6f 63 6b 69 6e 67 20 70 65 6e 64 69 6e 67 2f locking pending/
bf800 69 73 20 73 68 61 72 65 64 2e 20 72 3d 25 64 5c is shared. r=%d\
bf810 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 n", pFile->h, r
bf820 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 );. }.. /* Upd
bf830 61 74 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 ate the state of
bf840 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 the lock has he
bf850 6c 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 ld in the file d
bf860 65 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 escriptor then.
bf870 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 ** return the a
bf880 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c ppropriate resul
bf890 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 t code.. */. i
bf8a0 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 f( res == NO_ERR
bf8b0 4f 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 OR ){. rc = S
bf8c0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 QLITE_OK;. }els
bf8d0 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 e{. OSTRACE4(
bf8e0 20 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 "LOCK FAILED %d
bf8f0 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 62 trying for %d b
bf900 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 ut got %d\n", pF
bf910 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 ile->h,.
bf920 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 locktype,
bf930 6e 65 77 4c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 newLocktype );.
bf940 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
bf950 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 USY;. }. pFile
bf960 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65 77 ->locktype = new
bf970 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 Locktype;. OSTR
bf980 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 6e ACE3( "LOCK %d n
bf990 6f 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d ow %d\n", pFile-
bf9a0 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 >h, pFile->lockt
bf9b0 79 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ype );. return
bf9c0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rc;.}../*.** Thi
bf9d0 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 s routine checks
bf9e0 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 if there is a R
bf9f0 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c ESERVED lock hel
bfa00 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 d on the specifi
bfa10 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 ed.** file by th
bfa20 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 is or any other
bfa30 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 process. If such
bfa40 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c a lock is held,
bfa50 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a return.** non-z
bfa60 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 7a ero, otherwise z
bfa70 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ero..*/.static i
bfa80 6e 74 20 6f 73 32 43 68 65 63 6b 52 65 73 65 72 nt os2CheckReser
bfa90 76 65 64 4c 6f 63 6b 28 20 73 71 6c 69 74 65 33 vedLock( sqlite3
bfaa0 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a _file *id, int *
bfab0 70 4f 75 74 20 29 7b 0a 20 20 69 6e 74 20 72 20 pOut ){. int r
bfac0 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a = 0;. os2File *
bfad0 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 pFile = (os2File
bfae0 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 *)id;. assert(
bfaf0 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66 pFile!=0 );. if
bfb00 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
bfb10 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e>=RESERVED_LOCK
bfb20 20 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a 20 ){. r = 1;.
bfb30 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 OSTRACE3( "TE
bfb40 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 ST WR-LOCK %d %d
bfb50 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 (local)\n", pFi
bfb60 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 65 le->h, r );. }e
bfb70 6c 73 65 7b 0a 20 20 20 20 46 49 4c 45 4c 4f 43 lse{. FILELOC
bfb80 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 K LockArea,.
bfb90 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 Unloc
bfba0 6b 41 72 65 61 3b 0a 20 20 20 20 41 50 49 52 45 kArea;. APIRE
bfbb0 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b T rc = NO_ERROR;
bfbc0 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 . memset(&Loc
bfbd0 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 kArea, 0, sizeof
bfbe0 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 (LockArea));.
bfbf0 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 memset(&UnlockA
bfc00 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 rea, 0, sizeof(U
bfc10 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 nlockArea));.
bfc20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 LockArea.lOffse
bfc30 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 t = RESERVED_BYT
bfc40 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e E;. LockArea.
bfc50 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 lRange = 1L;.
bfc60 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 UnlockArea.lOff
bfc70 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e set = 0L;. Un
bfc80 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 lockArea.lRange
bfc90 3d 20 30 4c 3b 0a 20 20 20 20 72 63 20 3d 20 44 = 0L;. rc = D
bfca0 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 osSetFileLocks(
bfcb0 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 pFile->h, &Unloc
bfcc0 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 kArea, &LockArea
bfcd0 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 , LOCK_TIMEOUT,
bfce0 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 0L );. OSTRAC
bfcf0 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 E3( "TEST WR-LOC
bfd00 4b 20 25 64 20 6c 6f 63 6b 20 72 65 73 65 72 76 K %d lock reserv
bfd10 65 64 20 62 79 74 65 20 72 63 3d 25 64 5c 6e 22 ed byte rc=%d\n"
bfd20 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20 29 , pFile->h, rc )
bfd30 3b 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20 ;. if( rc ==
bfd40 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
bfd50 20 20 41 50 49 52 45 54 20 72 63 75 20 3d 20 4e APIRET rcu = N
bfd60 4f 5f 45 52 52 4f 52 3b 20 2f 2a 20 72 65 74 75 O_ERROR; /* retu
bfd70 72 6e 20 63 6f 64 65 20 66 6f 72 20 75 6e 6c 6f rn code for unlo
bfd80 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 4c cking */. L
bfd90 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
bfda0 3d 20 30 4c 3b 0a 20 20 20 20 20 20 4c 6f 63 6b = 0L;. Lock
bfdb0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c Area.lRange = 0L
bfdc0 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 ;. UnlockAr
bfdd0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 ea.lOffset = RES
bfde0 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 ERVED_BYTE;.
bfdf0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 UnlockArea.lRa
bfe00 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 20 20 nge = 1L;.
bfe10 72 63 75 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 rcu = DosSetFile
bfe20 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c Locks( pFile->h,
bfe30 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c &UnlockArea, &L
bfe40 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 ockArea, LOCK_TI
bfe50 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 MEOUT, 0L );.
bfe60 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 OSTRACE3( "TE
bfe70 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 75 6e ST WR-LOCK %d un
bfe80 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62 79 lock reserved by
bfe90 74 65 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c te r=%d\n", pFil
bfea0 65 2d 3e 68 2c 20 72 63 75 20 29 3b 0a 20 20 20 e->h, rcu );.
bfeb0 20 7d 0a 20 20 20 20 72 20 3d 20 21 28 72 63 20 }. r = !(rc
bfec0 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 == NO_ERROR);.
bfed0 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 OSTRACE3( "TES
bfee0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 T WR-LOCK %d %d
bfef0 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 (remote)\n", pFi
bff00 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a le->h, r );. }.
bff10 20 20 2a 70 4f 75 74 20 3d 20 72 3b 0a 20 20 72 *pOut = r;. r
bff20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
bff30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 .}../*.** Lower
bff40 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
bff50 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 l on file descri
bff60 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 ptor id to lockt
bff70 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a ype. locktype.*
bff80 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 * must be either
bff90 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 NO_LOCK or SHAR
bffa0 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 ED_LOCK..**.** I
bffb0 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 f the locking le
bffc0 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 vel of the file
bffd0 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c descriptor is al
bffe0 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f ready at or belo
bfff0 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 w.** the request
c0000 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c ed locking level
c0010 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 , this routine i
c0020 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a s a no-op..**.**
c0030 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 It is not possi
c0040 62 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f 75 ble for this rou
c0050 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 20 tine to fail if
c0060 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
c0070 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 ent.** is NO_LOC
c0080 4b 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f 6e K. If the secon
c0090 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 48 d argument is SH
c00a0 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 ARED_LOCK then t
c00b0 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d his routine.** m
c00c0 69 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c 49 ight return SQLI
c00d0 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 TE_IOERR;.*/.sta
c00e0 74 69 63 20 69 6e 74 20 6f 73 32 55 6e 6c 6f 63 tic int os2Unloc
c00f0 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 k( sqlite3_file
c0100 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 *id, int locktyp
c0110 65 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b e ){. int type;
c0120 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c . os2File *pFil
c0130 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 e = (os2File*)id
c0140 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 ;. APIRET rc =
c0150 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 49 SQLITE_OK;. API
c0160 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52 RET res = NO_ERR
c0170 4f 52 3b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 OR;. FILELOCK
c0180 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 LockArea,.
c0190 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 UnlockArea
c01a0 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b ;. memset(&Lock
c01b0 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Area, 0, sizeof(
c01c0 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 LockArea));. me
c01d0 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 mset(&UnlockArea
c01e0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f , 0, sizeof(Unlo
c01f0 63 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73 65 ckArea));. asse
c0200 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a rt( pFile!=0 );.
c0210 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
c0220 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe<=SHARED_LOCK
c0230 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 );. OSTRACE4( "
c0240 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 UNLOCK %d to %d
c0250 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 was %d\n", pFile
c0260 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 ->h, locktype, p
c0270 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 File->locktype )
c0280 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c 65 ;. type = pFile
c0290 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 ->locktype;. if
c02a0 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 56 ( type>=EXCLUSIV
c02b0 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f E_LOCK ){. Lo
c02c0 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
c02d0 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 0L;. LockAre
c02e0 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 a.lRange = 0L;.
c02f0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f UnlockArea.lO
c0300 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 ffset = SHARED_F
c0310 49 52 53 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b IRST;. Unlock
c0320 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 Area.lRange = SH
c0330 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72 ARED_SIZE;. r
c0340 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c es = DosSetFileL
c0350 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 ocks( pFile->h,
c0360 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f &UnlockArea, &Lo
c0370 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d ckArea, LOCK_TIM
c0380 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 EOUT, 0L );.
c0390 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 OSTRACE3( "UNLOC
c03a0 4b 20 25 64 20 65 78 63 6c 75 73 69 76 65 20 6c K %d exclusive l
c03b0 6f 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 ock res=%d\n", p
c03c0 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a File->h, res );.
c03d0 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 if( locktype
c03e0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 ==SHARED_LOCK &&
c03f0 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 getReadLock(pFi
c0400 6c 65 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 le) != NO_ERROR
c0410 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 ){. /* This
c0420 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 should never ha
c0430 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 ppen. We should
c0440 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65 20 always be able
c0450 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 63 to. ** reac
c0460 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20 6c quire the read l
c0470 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 ock */. OST
c0480 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 RACE3( "UNLOCK %
c0490 64 20 74 6f 20 25 64 20 67 65 74 52 65 61 64 4c d to %d getReadL
c04a0 6f 63 6b 28 29 20 66 61 69 6c 65 64 5c 6e 22 2c ock() failed\n",
c04b0 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 pFile->h, lockt
c04c0 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20 ype );. rc
c04d0 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 = SQLITE_IOERR_U
c04e0 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d NLOCK;. }. }
c04f0 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 53 . if( type>=RES
c0500 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 ERVED_LOCK ){.
c0510 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 LockArea.lOffs
c0520 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 et = 0L;. Loc
c0530 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
c0540 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 L;. UnlockAre
c0550 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 a.lOffset = RESE
c0560 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 55 RVED_BYTE;. U
c0570 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
c0580 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d = 1L;. res =
c0590 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
c05a0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
c05b0 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
c05c0 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ea, LOCK_TIMEOUT
c05d0 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 , 0L );. OSTR
c05e0 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 ACE3( "UNLOCK %d
c05f0 20 72 65 73 65 72 76 65 64 20 72 65 73 3d 25 64 reserved res=%d
c0600 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
c0610 65 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 es );. }. if(
c0620 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 locktype==NO_LOC
c0630 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45 K && type>=SHARE
c0640 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 D_LOCK ){. re
c0650 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f s = unlockReadLo
c0660 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f ck(pFile);. O
c0670 53 54 52 41 43 45 35 28 20 22 55 4e 4c 4f 43 4b STRACE5( "UNLOCK
c0680 20 25 64 20 69 73 20 25 64 20 77 61 6e 74 20 25 %d is %d want %
c0690 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 d res=%d\n", pFi
c06a0 6c 65 2d 3e 68 2c 20 74 79 70 65 2c 20 6c 6f 63 le->h, type, loc
c06b0 6b 74 79 70 65 2c 20 72 65 73 20 29 3b 0a 20 20 ktype, res );.
c06c0 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45 }. if( type>=PE
c06d0 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 NDING_LOCK ){.
c06e0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 LockArea.lOffs
c06f0 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 et = 0L;. Loc
c0700 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
c0710 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 L;. UnlockAre
c0720 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 a.lOffset = PEND
c0730 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e ING_BYTE;. Un
c0740 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 lockArea.lRange
c0750 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 = 1L;. res =
c0760 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
c0770 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
c0780 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
c0790 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
c07a0 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 0L );. OSTRA
c07b0 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 CE3( "UNLOCK %d
c07c0 70 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c 6e pending res=%d\n
c07d0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 ", pFile->h, res
c07e0 20 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d );. }. pFile-
c07f0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b >locktype = lock
c0800 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 33 type;. OSTRACE3
c0810 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 77 ( "UNLOCK %d now
c0820 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 %d\n", pFile->h
c0830 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 , pFile->locktyp
c0840 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 e );. return rc
c0850 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 ;.}../*.** Contr
c0860 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20 ol and query of
c0870 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 the open file ha
c0880 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ndle..*/.static
c0890 69 6e 74 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 int os2FileContr
c08a0 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ol(sqlite3_file
c08b0 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 *id, int op, voi
c08c0 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 d *pArg){. swit
c08d0 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 ch( op ){. ca
c08e0 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f se SQLITE_FCNTL_
c08f0 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 LOCKSTATE: {.
c0900 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d *(int*)pArg =
c0910 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d ((os2File*)id)-
c0920 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 >locktype;.
c0930 20 4f 53 54 52 41 43 45 33 28 20 22 46 43 4e 54 OSTRACE3( "FCNT
c0940 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20 6c L_LOCKSTATE %d l
c0950 6f 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 32 ock=%d\n", ((os2
c0960 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28 File*)id)->h, ((
c0970 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f os2File*)id)->lo
c0980 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20 20 cktype );.
c0990 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
c09a0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
c09b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
c09c0 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 R;.}../*.** Retu
c09d0 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 rn the sector si
c09e0 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 ze in bytes of t
c09f0 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c he underlying bl
c0a00 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a ock device for.*
c0a10 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 * the specified
c0a20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c file. This is al
c0a30 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 most always 512
c0a40 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 bytes, but may b
c0a50 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 e.** larger for
c0a60 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a some devices..**
c0a70 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 .** SQLite code
c0a80 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e assumes this fun
c0a90 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 ction cannot fai
c0aa0 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d l. It also assum
c0ab0 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 es that.** if tw
c0ac0 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 o files are crea
c0ad0 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 ted in the same
c0ae0 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 file-system dire
c0af0 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 ctory (i.e..** a
c0b00 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 database and it
c0b10 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 s journal file)
c0b20 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 that the sector
c0b30 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 size will be the
c0b40 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 .** same for bot
c0b50 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 h..*/.static int
c0b60 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28 73 os2SectorSize(s
c0b70 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
c0b80 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 {. return SQLIT
c0b90 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 E_DEFAULT_SECTOR
c0ba0 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 _SIZE;.}../*.**
c0bb0 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 20 Return a vector
c0bc0 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61 63 of device charac
c0bd0 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 teristics..*/.st
c0be0 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65 76 69 atic int os2Devi
c0bf0 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
c0c00 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a s(sqlite3_file *
c0c10 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b id){. return 0;
c0c20 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 .}.../*.** Chara
c0c30 63 74 65 72 20 73 65 74 20 63 6f 6e 76 65 72 73 cter set convers
c0c40 69 6f 6e 20 6f 62 6a 65 63 74 73 20 75 73 65 64 ion objects used
c0c50 20 62 79 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72 by conversion r
c0c60 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 outines..*/.stat
c0c70 69 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75 ic UconvObject u
c0c80 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f 2a cUtf8 = NULL; /*
c0c90 20 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65 6e convert between
c0ca0 20 55 54 46 2d 38 20 61 6e 64 20 55 43 53 2d 32 UTF-8 and UCS-2
c0cb0 20 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e 76 */.static Uconv
c0cc0 4f 62 6a 65 63 74 20 75 63 6c 43 70 20 3d 20 4e Object uclCp = N
c0cd0 55 4c 4c 3b 20 20 2f 2a 20 63 6f 6e 76 65 72 74 ULL; /* convert
c0ce0 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 20 63 between local c
c0cf0 6f 64 65 70 61 67 65 20 61 6e 64 20 55 43 53 2d odepage and UCS-
c0d00 32 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 2 */../*.** Help
c0d10 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 er function to i
c0d20 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f nitialize the co
c0d30 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 nversion objects
c0d40 20 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46 from and to UTF
c0d50 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f -8..*/.static vo
c0d60 69 64 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 id initUconvObje
c0d70 63 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20 69 cts( void ){. i
c0d80 66 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e f( UniCreateUcon
c0d90 76 4f 62 6a 65 63 74 28 20 55 54 46 5f 38 2c 20 vObject( UTF_8,
c0da0 26 75 63 55 74 66 38 20 29 20 21 3d 20 55 4c 53 &ucUtf8 ) != ULS
c0db0 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 75 _SUCCESS ). u
c0dc0 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20 cUtf8 = NULL;.
c0dd0 69 66 20 28 20 55 6e 69 43 72 65 61 74 65 55 63 if ( UniCreateUc
c0de0 6f 6e 76 4f 62 6a 65 63 74 28 20 28 55 6e 69 43 onvObject( (UniC
c0df0 68 61 72 20 2a 29 4c 22 40 70 61 74 68 3d 79 65 har *)L"@path=ye
c0e00 73 22 2c 20 26 75 63 6c 43 70 20 29 20 21 3d 20 s", &uclCp ) !=
c0e10 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 ULS_SUCCESS ).
c0e20 20 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a uclCp = NULL;.
c0e30 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 }../*.** Helper
c0e40 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 72 65 65 function to free
c0e50 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 the conversion
c0e60 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e 64 objects from and
c0e70 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74 to UTF-8..*/.st
c0e80 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 55 63 atic void freeUc
c0e90 6f 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69 64 onvObjects( void
c0ea0 20 29 7b 0a 20 20 69 66 20 28 20 75 63 55 74 66 ){. if ( ucUtf
c0eb0 38 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55 8 ). UniFreeU
c0ec0 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 55 74 convObject( ucUt
c0ed0 66 38 20 29 3b 0a 20 20 69 66 20 28 20 75 63 6c f8 );. if ( ucl
c0ee0 43 70 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 Cp ). UniFree
c0ef0 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 6c UconvObject( ucl
c0f00 43 70 20 29 3b 0a 20 20 75 63 55 74 66 38 20 3d Cp );. ucUtf8 =
c0f10 20 4e 55 4c 4c 3b 0a 20 20 75 63 6c 43 70 20 3d NULL;. uclCp =
c0f20 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 NULL;.}../*.**
c0f30 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 Helper function
c0f40 74 6f 20 63 6f 6e 76 65 72 74 20 55 54 46 2d 38 to convert UTF-8
c0f50 20 66 69 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 6f filenames to lo
c0f60 63 61 6c 20 4f 53 2f 32 20 63 6f 64 65 70 61 67 cal OS/2 codepag
c0f70 65 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 e..** The two-st
c0f80 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 ep process: firs
c0f90 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e t convert the in
c0fa0 63 6f 6d 69 6e 67 20 55 54 46 2d 38 20 73 74 72 coming UTF-8 str
c0fb0 69 6e 67 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 2d ing.** into UCS-
c0fc0 32 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d 20 2 and then from
c0fd0 55 43 53 2d 32 20 74 6f 20 74 68 65 20 63 75 72 UCS-2 to the cur
c0fe0 72 65 6e 74 20 63 6f 64 65 70 61 67 65 2e 0a 2a rent codepage..*
c0ff0 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 63 * The returned c
c1000 68 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73 20 har pointer has
c1010 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f 0a to be freed..*/.
c1020 73 74 61 74 69 63 20 63 68 61 72 20 2a 63 6f 6e static char *con
c1030 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 vertUtf8PathToCp
c1040 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e ( const char *in
c1050 20 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74 65 ){. UniChar te
c1060 6d 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41 54 mpPath[CCHMAXPAT
c1070 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 H];. char *out
c1080 3d 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63 = (char *)calloc
c1090 28 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31 20 ( CCHMAXPATH, 1
c10a0 29 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20 29 );.. if( !out )
c10b0 0a 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c . return NULL
c10c0 3b 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66 38 ;.. if( !ucUtf8
c10d0 20 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20 20 || !uclCp ).
c10e0 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 initUconvObject
c10f0 73 28 29 3b 0a 0a 20 20 2f 2a 20 64 65 74 65 72 s();.. /* deter
c1100 6d 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 20 mine string for
c1110 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f the conversion o
c1120 66 20 55 54 46 2d 38 20 77 68 69 63 68 20 69 73 f UTF-8 which is
c1130 20 43 50 31 32 30 38 20 2a 2f 0a 20 20 69 66 28 CP1208 */. if(
c1140 20 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75 63 UniStrToUcs( uc
c1150 55 74 66 38 2c 20 74 65 6d 70 50 61 74 68 2c 20 Utf8, tempPath,
c1160 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d (char *)in, CCHM
c1170 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53 5f AXPATH ) != ULS_
c1180 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 72 65 SUCCESS ). re
c1190 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20 turn out; /* if
c11a0 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c 73 conversion fails
c11b0 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d 70 , return the emp
c11c0 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 ty string */..
c11d0 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f /* conversion fo
c11e0 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 r current codepa
c11f0 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 ge which can be
c1200 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a used for paths *
c1210 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55 63 /. UniStrFromUc
c1220 73 28 20 75 63 6c 43 70 2c 20 6f 75 74 2c 20 74 s( uclCp, out, t
c1230 65 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58 50 empPath, CCHMAXP
c1240 41 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e ATH );.. return
c1250 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 out;.}../*.** H
c1260 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 elper function t
c1270 6f 20 63 6f 6e 76 65 72 74 20 66 69 6c 65 6e 61 o convert filena
c1280 6d 65 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 63 mes from local c
c1290 6f 64 65 70 61 67 65 20 74 6f 20 55 54 46 2d 38 odepage to UTF-8
c12a0 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 ..** The two-ste
c12b0 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74 p process: first
c12c0 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63 convert the inc
c12d0 6f 6d 69 6e 67 20 63 6f 64 65 70 61 67 65 2d 73 oming codepage-s
c12e0 70 65 63 69 66 69 63 0a 2a 2a 20 73 74 72 69 6e pecific.** strin
c12f0 67 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64 g into UCS-2 and
c1300 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32 then from UCS-2
c1310 20 74 6f 20 74 68 65 20 63 6f 64 65 70 61 67 65 to the codepage
c1320 20 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 of UTF-8..** Th
c1330 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 20 e returned char
c1340 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 62 pointer has to b
c1350 65 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 e freed..**.** T
c1360 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
c1370 6e 6f 6e 2d 73 74 61 74 69 63 20 74 6f 20 62 65 non-static to be
c1380 20 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 69 able to use thi
c1390 73 20 69 6e 20 73 68 65 6c 6c 2e 63 20 61 6e 64 s in shell.c and
c13a0 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 61 70 70 6c .** similar appl
c13b0 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 74 61 ications that ta
c13c0 6b 65 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20 ke command line
c13d0 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 68 arguments..*/.ch
c13e0 61 72 20 2a 63 6f 6e 76 65 72 74 43 70 50 61 74 ar *convertCpPat
c13f0 68 54 6f 55 74 66 38 28 20 63 6f 6e 73 74 20 63 hToUtf8( const c
c1400 68 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69 har *in ){. Uni
c1410 43 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43 43 Char tempPath[CC
c1420 48 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68 61 HMAXPATH];. cha
c1430 72 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20 2a r *out = (char *
c1440 29 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50 )calloc( CCHMAXP
c1450 41 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28 ATH, 1 );.. if(
c1460 20 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74 75 !out ). retu
c1470 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20 rn NULL;.. if(
c1480 21 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c 43 !ucUtf8 || !uclC
c1490 70 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e p ). initUcon
c14a0 76 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f vObjects();.. /
c14b0 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 * conversion for
c14c0 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67 current codepag
c14d0 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 e which can be u
c14e0 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f sed for paths */
c14f0 0a 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f 55 . if( UniStrToU
c1500 63 73 28 20 75 63 6c 43 70 2c 20 74 65 6d 70 50 cs( uclCp, tempP
c1510 61 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e 2c ath, (char *)in,
c1520 20 43 43 48 4d 41 58 50 41 54 48 20 29 20 21 3d CCHMAXPATH ) !=
c1530 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 ULS_SUCCESS ).
c1540 20 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20 2f return out; /
c1550 2a 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 * if conversion
c1560 66 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74 68 fails, return th
c1570 65 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 2a e empty string *
c1580 2f 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e /.. /* determin
c1590 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 e string for the
c15a0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 conversion of U
c15b0 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 50 TF-8 which is CP
c15c0 31 32 30 38 20 2a 2f 0a 20 20 55 6e 69 53 74 72 1208 */. UniStr
c15d0 46 72 6f 6d 55 63 73 28 20 75 63 55 74 66 38 2c FromUcs( ucUtf8,
c15e0 20 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c 20 out, tempPath,
c15f0 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20 CCHMAXPATH );..
c1600 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a return out;.}..
c1610 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74 6f /*.** This vecto
c1620 72 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 r defines all th
c1630 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 63 e methods that c
c1640 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e an operate on an
c1650 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 .** sqlite3_file
c1660 20 66 6f 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 61 for os2..*/.sta
c1670 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
c1680 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 73 32 3_io_methods os2
c1690 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 IoMethod = {. 1
c16a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
c16b0 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 /* iVer
c16c0 73 69 6f 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c 6f sion */. os2Clo
c16d0 73 65 2c 0a 20 20 6f 73 32 52 65 61 64 2c 0a 20 se,. os2Read,.
c16e0 20 6f 73 32 57 72 69 74 65 2c 0a 20 20 6f 73 32 os2Write,. os2
c16f0 54 72 75 6e 63 61 74 65 2c 0a 20 20 6f 73 32 53 Truncate,. os2S
c1700 79 6e 63 2c 0a 20 20 6f 73 32 46 69 6c 65 53 69 ync,. os2FileSi
c1710 7a 65 2c 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a 20 ze,. os2Lock,.
c1720 20 6f 73 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f 73 os2Unlock,. os
c1730 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 2CheckReservedLo
c1740 63 6b 2c 0a 20 20 6f 73 32 46 69 6c 65 43 6f 6e ck,. os2FileCon
c1750 74 72 6f 6c 2c 0a 20 20 6f 73 32 53 65 63 74 6f trol,. os2Secto
c1760 72 53 69 7a 65 2c 0a 20 20 6f 73 32 44 65 76 69 rSize,. os2Devi
c1770 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
c1780 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a s.};../*********
c1790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c17a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c17b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c17c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c17d0 2a 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 **.** Here ends
c17e0 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 the I/O methods
c17f0 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 71 that form the sq
c1800 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
c1810 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 object..**.** T
c1820 68 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 he next block of
c1830 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 code implements
c1840 20 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 73 the VFS methods
c1850 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**************
c1860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
c18a0 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 ./*.** Create a
c18b0 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e temporary file n
c18c0 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 ame in zBuf. zB
c18d0 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65 uf must be big e
c18e0 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 nough to.** hold
c18f0 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 at pVfs->mxPath
c1900 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e name characters.
c1910 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 .*/.static int g
c1920 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e etTempname(int n
c1930 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 Buf, char *zBuf
c1940 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 ){. static cons
c1950 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
c1960 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 zChars[] =. "
c1970 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 abcdefghijklmnop
c1980 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 qrstuvwxyz".
c1990 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f "ABCDEFGHIJKLMNO
c19a0 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 PQRSTUVWXYZ".
c19b0 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 "0123456789";.
c19c0 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 int i, j;. cha
c19d0 72 20 7a 54 65 6d 70 50 61 74 68 42 75 66 5b 33 r zTempPathBuf[3
c19e0 5d 3b 0a 20 20 50 53 5a 20 7a 54 65 6d 70 50 61 ];. PSZ zTempPa
c19f0 74 68 20 3d 20 28 50 53 5a 29 26 7a 54 65 6d 70 th = (PSZ)&zTemp
c1a00 50 61 74 68 42 75 66 3b 0a 20 20 69 66 28 20 73 PathBuf;. if( s
c1a10 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 qlite3_temp_dire
c1a20 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 54 65 ctory ){. zTe
c1a30 6d 70 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 mpPath = sqlite3
c1a40 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b _temp_directory;
c1a50 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 . }else{. if
c1a60 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 ( DosScanEnv( (P
c1a70 53 5a 29 22 54 45 4d 50 22 2c 20 26 7a 54 65 6d SZ)"TEMP", &zTem
c1a80 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20 pPath ) ){.
c1a90 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 if( DosScanEnv(
c1aa0 20 28 50 53 5a 29 22 54 4d 50 22 2c 20 26 7a 54 (PSZ)"TMP", &zT
c1ab0 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 empPath ) ){.
c1ac0 20 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 6e if( DosScan
c1ad0 45 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 44 49 Env( (PSZ)"TMPDI
c1ae0 52 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 29 R", &zTempPath )
c1af0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 55 ){. U
c1b00 4c 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d 20 LONG ulDriveNum
c1b10 3d 20 30 2c 20 75 6c 44 72 69 76 65 4d 61 70 20 = 0, ulDriveMap
c1b20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 = 0;.
c1b30 44 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74 44 DosQueryCurrentD
c1b40 69 73 6b 28 20 26 75 6c 44 72 69 76 65 4e 75 6d isk( &ulDriveNum
c1b50 2c 20 26 75 6c 44 72 69 76 65 4d 61 70 20 29 3b , &ulDriveMap );
c1b60 0a 20 20 20 20 20 20 20 20 20 20 20 73 70 72 69 . spri
c1b70 6e 74 66 28 20 28 63 68 61 72 2a 29 7a 54 65 6d ntf( (char*)zTem
c1b80 70 50 61 74 68 2c 20 22 25 63 3a 22 2c 20 28 63 pPath, "%c:", (c
c1b90 68 61 72 29 28 20 27 41 27 20 2b 20 75 6c 44 72 har)( 'A' + ulDr
c1ba0 69 76 65 4e 75 6d 20 2d 20 31 20 29 20 29 3b 0a iveNum - 1 ) );.
c1bb0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
c1bc0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a }. }. }. /*
c1bd0 20 53 74 72 69 70 20 6f 66 66 20 61 20 74 72 61 Strip off a tra
c1be0 69 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f 72 iling slashes or
c1bf0 20 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f 74 backslashes, ot
c1c00 68 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c 64 herwise we would
c1c10 20 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c 74 get *. * mult
c1c20 69 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73 68 iple (back)slash
c1c30 65 73 20 77 68 69 63 68 20 63 61 75 73 65 73 20 es which causes
c1c40 44 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61 69 DosOpen() to fai
c1c50 6c 2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l.
c1c60 2a 0a 20 20 20 2a 20 54 72 61 69 6c 69 6e 67 20 *. * Trailing
c1c70 73 70 61 63 65 73 20 61 72 65 20 6e 6f 74 20 61 spaces are not a
c1c80 6c 6c 6f 77 65 64 2c 20 65 69 74 68 65 72 2e 20 llowed, either.
c1c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c1ca0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 */.
c1cb0 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 j = sqlite3Strle
c1cc0 6e 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b 0a n30(zTempPath);.
c1cd0 20 20 77 68 69 6c 65 28 20 6a 20 3e 20 30 20 26 while( j > 0 &
c1ce0 26 20 28 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d & ( zTempPath[j-
c1cf0 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a 54 1] == '\\' || zT
c1d00 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 empPath[j-1] ==
c1d10 27 2f 27 0a 20 20 20 20 20 20 20 20 20 20 20 20 '/'.
c1d20 20 20 20 20 20 20 20 20 7c 7c 20 7a 54 65 6d 70 || zTemp
c1d30 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20 27 Path[j-1] == ' '
c1d40 20 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a 20 ) ){. j--;.
c1d50 20 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b 6a }. zTempPath[j
c1d60 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 ] = '\0';. if(
c1d70 21 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 !sqlite3_temp_di
c1d80 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 63 rectory ){. c
c1d90 68 61 72 20 2a 7a 54 65 6d 70 50 61 74 68 55 54 har *zTempPathUT
c1da0 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 74 F = convertCpPat
c1db0 68 54 6f 55 74 66 38 28 20 7a 54 65 6d 70 50 61 hToUtf8( zTempPa
c1dc0 74 68 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 th );. sqlite
c1dd0 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 3_snprintf( nBuf
c1de0 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 -30, zBuf,.
c1df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c1e00 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 "%s\\"SQLITE_TE
c1e10 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 MP_FILE_PREFIX,
c1e20 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b 0a zTempPathUTF );.
c1e30 20 20 20 20 66 72 65 65 28 20 7a 54 65 6d 70 50 free( zTempP
c1e40 61 74 68 55 54 46 20 29 3b 0a 20 20 7d 65 6c 73 athUTF );. }els
c1e50 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 e{. sqlite3_s
c1e60 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33 30 nprintf( nBuf-30
c1e70 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 , zBuf,.
c1e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25 "%
c1e90 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f s\\"SQLITE_TEMP_
c1ea0 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 FILE_PREFIX, zTe
c1eb0 6d 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a 20 20 mpPath );. }.
c1ec0 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 j = sqlite3Strle
c1ed0 6e 33 30 28 20 7a 42 75 66 20 29 3b 0a 20 20 73 n30( zBuf );. s
c1ee0 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
c1ef0 73 28 20 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 20 s( 20, &zBuf[j]
c1f00 29 3b 0a 20 20 66 6f 72 28 20 69 20 3d 20 30 3b );. for( i = 0;
c1f10 20 69 20 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a 2b i < 20; i++, j+
c1f20 2b 20 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a 5d + ){. zBuf[j]
c1f30 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 5b = (char)zChars[
c1f40 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 ((unsigned char
c1f50 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f )zBuf[j])%(sizeo
c1f60 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a f(zChars)-1) ];.
c1f70 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 }. zBuf[j] =
c1f80 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22 0;. OSTRACE2( "
c1f90 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25 TEMP FILENAME: %
c1fa0 73 5c 6e 22 2c 20 7a 42 75 66 20 29 3b 0a 20 20 s\n", zBuf );.
c1fb0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
c1fc0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e ;.}.../*.** Turn
c1fd0 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 a relative path
c1fe0 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c name into a full
c1ff0 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 74 pathname. Writ
c2000 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 e the full.** pa
c2010 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 46 75 6c thname into zFul
c2020 6c 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77 69 l[]. zFull[] wi
c2030 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70 ll be at least p
c2040 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a Vfs->mxPathname.
c2050 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 ** bytes in size
c2060 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
c2070 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 os2FullPathname(
c2080 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
c2090 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 2f pVfs, /
c20a0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 * Pointer to vfs
c20b0 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e object */. con
c20c0 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 st char *zRelati
c20d0 76 65 2c 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 ve, /* Poss
c20e0 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e ibly relative in
c20f0 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e put path */. in
c2100 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 20 t nFull,
c2110 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
c2120 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 66 e of output buff
c2130 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 er in bytes */.
c2140 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20 char *zFull
c2150 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c2160 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f Output buffer */
c2170 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 52 65 6c .){. char *zRel
c2180 61 74 69 76 65 43 70 20 3d 20 63 6f 6e 76 65 72 ativeCp = conver
c2190 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a tUtf8PathToCp( z
c21a0 52 65 6c 61 74 69 76 65 20 29 3b 0a 20 20 63 68 Relative );. ch
c21b0 61 72 20 7a 46 75 6c 6c 43 70 5b 43 43 48 4d 41 ar zFullCp[CCHMA
c21c0 58 50 41 54 48 5d 20 3d 20 22 5c 30 22 3b 0a 20 XPATH] = "\0";.
c21d0 20 63 68 61 72 20 2a 7a 46 75 6c 6c 55 54 46 3b char *zFullUTF;
c21e0 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 44 . APIRET rc = D
c21f0 6f 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f 28 osQueryPathInfo(
c2200 20 7a 52 65 6c 61 74 69 76 65 43 70 2c 20 46 49 zRelativeCp, FI
c2210 4c 5f 51 55 45 52 59 46 55 4c 4c 4e 41 4d 45 2c L_QUERYFULLNAME,
c2220 20 7a 46 75 6c 6c 43 70 2c 0a 20 20 20 20 20 20 zFullCp,.
c2230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2240 20 20 20 20 20 20 20 20 20 20 43 43 48 4d 41 58 CCHMAX
c2250 50 41 54 48 20 29 3b 0a 20 20 66 72 65 65 28 20 PATH );. free(
c2260 7a 52 65 6c 61 74 69 76 65 43 70 20 29 3b 0a 20 zRelativeCp );.
c2270 20 7a 46 75 6c 6c 55 54 46 20 3d 20 63 6f 6e 76 zFullUTF = conv
c2280 65 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28 ertCpPathToUtf8(
c2290 20 7a 46 75 6c 6c 43 70 20 29 3b 0a 20 20 73 71 zFullCp );. sq
c22a0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 lite3_snprintf(
c22b0 6e 46 75 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a 46 nFull, zFull, zF
c22c0 75 6c 6c 55 54 46 20 29 3b 0a 20 20 66 72 65 65 ullUTF );. free
c22d0 28 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20 ( zFullUTF );.
c22e0 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f return rc == NO_
c22f0 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f ERROR ? SQLITE_O
c2300 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 K : SQLITE_IOERR
c2310 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e ;.}.../*.** Open
c2320 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 a file..*/.stat
c2330 69 63 20 69 6e 74 20 6f 73 32 4f 70 65 6e 28 0a ic int os2Open(.
c2340 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
c2350 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 Vfs,
c2360 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 /* Not used */.
c2370 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
c2380 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f me, /
c2390 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 * Name of the fi
c23a0 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f le */. sqlite3_
c23b0 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 file *id,
c23c0 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 /* Write t
c23d0 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68 he SQLite file h
c23e0 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 andle here */.
c23f0 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 int flags,
c2400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c2410 20 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 Open mode flags
c2420 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 */. int *pOutF
c2430 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 lags
c2440 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 65 /* Status re
c2450 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 7b turn flags */.){
c2460 0a 20 20 48 46 49 4c 45 20 68 3b 0a 20 20 55 4c . HFILE h;. UL
c2470 4f 4e 47 20 75 6c 46 69 6c 65 41 74 74 72 69 62 ONG ulFileAttrib
c2480 75 74 65 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d 41 ute = FILE_NORMA
c2490 4c 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 65 L;. ULONG ulOpe
c24a0 6e 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 55 4c nFlags = 0;. UL
c24b0 4f 4e 47 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 3d ONG ulOpenMode =
c24c0 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 0;. os2File *p
c24d0 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a File = (os2File*
c24e0 29 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 )id;. APIRET rc
c24f0 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 = NO_ERROR;. U
c2500 4c 4f 4e 47 20 75 6c 41 63 74 69 6f 6e 3b 0a 20 LONG ulAction;.
c2510 20 63 68 61 72 20 2a 7a 4e 61 6d 65 43 70 3b 0a char *zNameCp;.
c2520 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b char zTmpname[
c2530 43 43 48 4d 41 58 50 41 54 48 2b 31 5d 3b 20 20 CCHMAXPATH+1];
c2540 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 68 /* Buffer to h
c2550 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74 65 6d 70 old name of temp
c2560 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 file */.. /* I
c2570 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 f the second arg
c2580 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 ument to this fu
c2590 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 nction is NULL,
c25a0 67 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a 2a generate a . **
c25b0 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
c25c0 6e 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 2a name to use . *
c25d0 2f 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 /. if( !zName )
c25e0 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67 {. int rc = g
c25f0 65 74 54 65 6d 70 6e 61 6d 65 28 43 43 48 4d 41 etTempname(CCHMA
c2600 58 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d XPATH+1, zTmpnam
c2610 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
c2620 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
c2630 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
c2640 20 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 }. zName =
c2650 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 0a zTmpname;. }...
c2660 20 20 6d 65 6d 73 65 74 28 20 70 46 69 6c 65 2c memset( pFile,
c2670 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 0, sizeof(*pFil
c2680 65 29 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43 45 e) );.. OSTRACE
c2690 32 28 20 22 4f 50 45 4e 20 77 61 6e 74 20 25 64 2( "OPEN want %d
c26a0 5c 6e 22 2c 20 66 6c 61 67 73 20 29 3b 0a 0a 20 \n", flags );..
c26b0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c if( flags & SQL
c26c0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
c26d0 54 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e TE ){. ulOpen
c26e0 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43 Mode |= OPEN_ACC
c26f0 45 53 53 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 ESS_READWRITE;.
c2700 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 OSTRACE1( "OP
c2710 45 4e 20 72 65 61 64 2f 77 72 69 74 65 5c 6e 22 EN read/write\n"
c2720 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 );. }else{.
c2730 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f ulOpenMode |= O
c2740 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 4f PEN_ACCESS_READO
c2750 4e 4c 59 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 NLY;. OSTRACE
c2760 31 28 20 22 4f 50 45 4e 20 72 65 61 64 20 6f 6e 1( "OPEN read on
c2770 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 ly\n" );. }..
c2780 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 if( flags & SQLI
c2790 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 29 TE_OPEN_CREATE )
c27a0 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 {. ulOpenFlag
c27b0 73 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e s |= OPEN_ACTION
c27c0 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 _OPEN_IF_EXISTS
c27d0 7c 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 52 | OPEN_ACTION_CR
c27e0 45 41 54 45 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 EATE_IF_NEW;.
c27f0 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e OSTRACE1( "OPEN
c2800 20 6f 70 65 6e 20 6e 65 77 2f 63 72 65 61 74 65 open new/create
c2810 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a \n" );. }else{.
c2820 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 ulOpenFlags
c2830 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f |= OPEN_ACTION_O
c2840 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 PEN_IF_EXISTS |
c2850 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 46 41 49 4c OPEN_ACTION_FAIL
c2860 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54 _IF_NEW;. OST
c2870 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 65 RACE1( "OPEN ope
c2880 6e 20 65 78 69 73 74 69 6e 67 5c 6e 22 20 29 3b n existing\n" );
c2890 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 . }.. if( flag
c28a0 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
c28b0 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 75 MAIN_DB ){. u
c28c0 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 lOpenMode |= OPE
c28d0 4e 5f 53 48 41 52 45 5f 44 45 4e 59 4e 4f 4e 45 N_SHARE_DENYNONE
c28e0 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 ;. OSTRACE1(
c28f0 22 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61 64 "OPEN share read
c2900 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d /write\n" );. }
c2910 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e else{. ulOpen
c2920 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 Mode |= OPEN_SHA
c2930 52 45 5f 44 45 4e 59 57 52 49 54 45 3b 0a 20 20 RE_DENYWRITE;.
c2940 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 OSTRACE1( "OPE
c2950 4e 20 73 68 61 72 65 20 72 65 61 64 20 6f 6e 6c N share read onl
c2960 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 y\n" );. }.. i
c2970 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f( flags & SQLIT
c2980 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 E_OPEN_DELETEONC
c2990 4c 4f 53 45 20 29 7b 0a 20 20 20 20 63 68 61 72 LOSE ){. char
c29a0 20 70 61 74 68 55 74 66 38 5b 43 43 48 4d 41 58 pathUtf8[CCHMAX
c29b0 50 41 54 48 5d 3b 0a 23 69 66 64 65 66 20 4e 44 PATH];.#ifdef ND
c29c0 45 42 55 47 20 2f 2a 20 77 68 65 6e 20 64 65 62 EBUG /* when deb
c29d0 75 67 67 69 6e 67 20 77 65 20 77 61 6e 74 20 74 ugging we want t
c29e0 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 o make sure it i
c29f0 73 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 20 s deleted */.
c2a00 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 ulFileAttribute
c2a10 20 3d 20 46 49 4c 45 5f 48 49 44 44 45 4e 3b 0a = FILE_HIDDEN;.
c2a20 23 65 6e 64 69 66 0a 20 20 20 20 6f 73 32 46 75 #endif. os2Fu
c2a30 6c 6c 50 61 74 68 6e 61 6d 65 28 20 70 56 66 73 llPathname( pVfs
c2a40 2c 20 7a 4e 61 6d 65 2c 20 43 43 48 4d 41 58 50 , zName, CCHMAXP
c2a50 41 54 48 2c 20 70 61 74 68 55 74 66 38 20 29 3b ATH, pathUtf8 );
c2a60 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 . pFile->path
c2a70 54 6f 44 65 6c 20 3d 20 63 6f 6e 76 65 72 74 55 ToDel = convertU
c2a80 74 66 38 50 61 74 68 54 6f 43 70 28 20 70 61 74 tf8PathToCp( pat
c2a90 68 55 74 66 38 20 29 3b 0a 20 20 20 20 4f 53 54 hUtf8 );. OST
c2aa0 52 41 43 45 31 28 20 22 4f 50 45 4e 20 68 69 64 RACE1( "OPEN hid
c2ab0 64 65 6e 2f 64 65 6c 65 74 65 20 6f 6e 20 63 6c den/delete on cl
c2ac0 6f 73 65 20 66 69 6c 65 20 61 74 74 72 69 62 75 ose file attribu
c2ad0 74 65 73 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 tes\n" );. }els
c2ae0 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 e{. pFile->pa
c2af0 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a thToDel = NULL;.
c2b00 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f OSTRACE1( "O
c2b10 50 45 4e 20 6e 6f 72 6d 61 6c 20 66 69 6c 65 20 PEN normal file
c2b20 61 74 74 72 69 62 75 74 65 5c 6e 22 20 29 3b 0a attribute\n" );.
c2b30 20 20 7d 0a 0a 20 20 2f 2a 20 61 6c 77 61 79 73 }.. /* always
c2b40 20 6f 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d 20 open in random
c2b50 61 63 63 65 73 73 20 6d 6f 64 65 20 66 6f 72 20 access mode for
c2b60 70 6f 73 73 69 62 6c 79 20 62 65 74 74 65 72 20 possibly better
c2b70 73 70 65 65 64 20 2a 2f 0a 20 20 75 6c 4f 70 65 speed */. ulOpe
c2b80 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c nMode |= OPEN_FL
c2b90 41 47 53 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75 6c AGS_RANDOM;. ul
c2ba0 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e OpenMode |= OPEN
c2bb0 5f 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f 45 _FLAGS_FAIL_ON_E
c2bc0 52 52 4f 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f RROR;. ulOpenMo
c2bd0 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 de |= OPEN_FLAGS
c2be0 5f 4e 4f 49 4e 48 45 52 49 54 3b 0a 0a 20 20 7a _NOINHERIT;.. z
c2bf0 4e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 NameCp = convert
c2c00 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 4e Utf8PathToCp( zN
c2c10 61 6d 65 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f ame );. rc = Do
c2c20 73 4f 70 65 6e 28 20 28 50 53 5a 29 7a 4e 61 6d sOpen( (PSZ)zNam
c2c30 65 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 eCp,.
c2c40 20 20 20 20 20 26 68 2c 0a 20 20 20 20 20 20 20 &h,.
c2c50 20 20 20 20 20 20 20 20 20 26 75 6c 41 63 74 69 &ulActi
c2c60 6f 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 on,.
c2c70 20 20 20 20 30 4c 2c 0a 20 20 20 20 20 20 20 20 0L,.
c2c80 20 20 20 20 20 20 20 20 75 6c 46 69 6c 65 41 74 ulFileAt
c2c90 74 72 69 62 75 74 65 2c 0a 20 20 20 20 20 20 20 tribute,.
c2ca0 20 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e 46 ulOpenF
c2cb0 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 lags,.
c2cc0 20 20 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 ulOpenMode
c2cd0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
c2ce0 20 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20 29 (PEAOP2)NULL )
c2cf0 3b 0a 20 20 66 72 65 65 28 20 7a 4e 61 6d 65 43 ;. free( zNameC
c2d00 70 20 29 3b 0a 20 20 69 66 28 20 72 63 20 21 3d p );. if( rc !=
c2d10 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
c2d20 20 4f 53 54 52 41 43 45 37 28 20 22 4f 50 45 4e OSTRACE7( "OPEN
c2d30 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 20 Invalid handle
c2d40 72 63 3d 25 64 3a 20 7a 4e 61 6d 65 3d 25 73 2c rc=%d: zName=%s,
c2d50 20 75 6c 41 63 74 69 6f 6e 3d 25 23 6c 78 2c 20 ulAction=%#lx,
c2d60 75 6c 41 74 74 72 3d 25 23 6c 78 2c 20 75 6c 46 ulAttr=%#lx, ulF
c2d70 6c 61 67 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f 64 lags=%#lx, ulMod
c2d80 65 3d 25 23 6c 78 5c 6e 22 2c 0a 20 20 20 20 20 e=%#lx\n",.
c2d90 20 20 20 20 20 20 20 20 20 72 63 2c 20 7a 4e 61 rc, zNa
c2da0 6d 65 2c 20 75 6c 41 63 74 69 6f 6e 2c 20 75 6c me, ulAction, ul
c2db0 46 69 6c 65 41 74 74 72 69 62 75 74 65 2c 20 75 FileAttribute, u
c2dc0 6c 4f 70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f 70 lOpenFlags, ulOp
c2dd0 65 6e 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69 66 enMode );. if
c2de0 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 ( pFile->pathToD
c2df0 65 6c 20 29 0a 20 20 20 20 20 20 66 72 65 65 28 el ). free(
c2e00 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 pFile->pathToDe
c2e10 6c 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e l );. pFile->
c2e20 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c pathToDel = NULL
c2e30 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 ;. if( flags
c2e40 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 & SQLITE_OPEN_RE
c2e50 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 ADWRITE ){.
c2e60 20 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 4e OSTRACE2( "OPEN
c2e70 20 25 64 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 %d Invalid hand
c2e80 6c 65 5c 6e 22 2c 20 28 28 66 6c 61 67 73 20 7c le\n", ((flags |
c2e90 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
c2ea0 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54 45 DONLY) & ~SQLITE
c2eb0 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 _OPEN_READWRITE)
c2ec0 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e );. return
c2ed0 20 6f 73 32 4f 70 65 6e 28 20 70 56 66 73 2c 20 os2Open( pVfs,
c2ee0 7a 4e 61 6d 65 2c 20 69 64 2c 0a 20 20 20 20 20 zName, id,.
c2ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2f00 20 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 ((flags | SQLIT
c2f10 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 E_OPEN_READONLY)
c2f20 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f & ~SQLITE_OPEN_
c2f30 52 45 41 44 57 52 49 54 45 29 2c 0a 20 20 20 20 READWRITE),.
c2f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2f50 20 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20 pOutFlags );.
c2f60 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
c2f70 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 return SQLITE_CA
c2f80 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 NTOPEN;. }.
c2f90 7d 0a 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 }.. if( pOutFla
c2fa0 67 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46 gs ){. *pOutF
c2fb0 6c 61 67 73 20 3d 20 66 6c 61 67 73 20 26 20 53 lags = flags & S
c2fc0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
c2fd0 52 49 54 45 20 3f 20 53 51 4c 49 54 45 5f 4f 50 RITE ? SQLITE_OP
c2fe0 45 4e 5f 52 45 41 44 57 52 49 54 45 20 3a 20 53 EN_READWRITE : S
c2ff0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
c3000 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 70 46 69 6c NLY;. }.. pFil
c3010 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 6f 73 e->pMethod = &os
c3020 32 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 2IoMethod;. pFi
c3030 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 4f 70 65 le->h = h;. Ope
c3040 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 nCounter(+1);.
c3050 4f 53 54 52 41 43 45 33 28 20 22 4f 50 45 4e 20 OSTRACE3( "OPEN
c3060 25 64 20 70 4f 75 74 46 6c 61 67 73 3d 25 64 5c %d pOutFlags=%d\
c3070 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 4f n", pFile->h, pO
c3080 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 72 65 74 utFlags );. ret
c3090 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
c30a0 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 ../*.** Delete t
c30b0 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a he named file..*
c30c0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 /.static int os2
c30d0 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 Delete(. sqlite
c30e0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 3_vfs *pVfs,
c30f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3100 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 /* Not used on
c3110 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 os2 */. const c
c3120 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 har *zFilename,
c3130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3140 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 /* Name of file
c3150 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 to delete */. i
c3160 6e 74 20 73 79 6e 63 44 69 72 20 20 20 20 20 20 nt syncDir
c3170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3180 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 /* Not use
c3190 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a 20 d on os2 */.){.
c31a0 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f APIRET rc = NO_
c31b0 45 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a ERROR;. char *z
c31c0 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e FilenameCp = con
c31d0 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 vertUtf8PathToCp
c31e0 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 ( zFilename );.
c31f0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
c3200 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
c3210 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 29 3b 0a IOERR_DELETE );.
c3220 20 20 72 63 20 3d 20 44 6f 73 44 65 6c 65 74 65 rc = DosDelete
c3230 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 ( (PSZ)zFilename
c3240 43 70 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 Cp );. free( zF
c3250 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f ilenameCp );. O
c3260 53 54 52 41 43 45 32 28 20 22 44 45 4c 45 54 45 STRACE2( "DELETE
c3270 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c \"%s\"\n", zFil
c3280 65 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75 72 ename );. retur
c3290 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 n rc == NO_ERROR
c32a0 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 ? SQLITE_OK : S
c32b0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 QLITE_IOERR_DELE
c32c0 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 TE;.}../*.** Che
c32d0 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 ck the existance
c32e0 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 61 and status of a
c32f0 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
c3300 20 69 6e 74 20 6f 73 32 41 63 63 65 73 73 28 0a int os2Access(.
c3310 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
c3320 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e Vfs, /* N
c3330 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a ot used on os2 *
c3340 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
c3350 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 2f 2a zFilename, /*
c3360 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f Name of file to
c3370 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 check */. int
c3380 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 flags,
c3390 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 /* Type of
c33a0 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e test to make on
c33b0 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 this file */.
c33c0 69 6e 74 20 2a 70 4f 75 74 20 20 20 20 20 20 20 int *pOut
c33d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 /* Wri
c33e0 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 te results here
c33f0 2a 2f 0a 29 7b 0a 20 20 46 49 4c 45 53 54 41 54 */.){. FILESTAT
c3400 55 53 33 20 66 73 74 73 33 43 6f 6e 66 69 67 49 US3 fsts3ConfigI
c3410 6e 66 6f 3b 0a 20 20 41 50 49 52 45 54 20 72 63 nfo;. APIRET rc
c3420 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 = NO_ERROR;. c
c3430 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 har *zFilenameCp
c3440 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 = convertUtf8Pa
c3450 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61 6d thToCp( zFilenam
c3460 65 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 20 e );.. memset(
c3470 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f &fsts3ConfigInfo
c3480 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73 , 0, sizeof(fsts
c3490 33 43 6f 6e 66 69 67 49 6e 66 6f 29 20 29 3b 0a 3ConfigInfo) );.
c34a0 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 rc = DosQueryP
c34b0 61 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a 46 athInfo( (PSZ)zF
c34c0 69 6c 65 6e 61 6d 65 43 70 2c 20 46 49 4c 5f 53 ilenameCp, FIL_S
c34d0 54 41 4e 44 41 52 44 2c 0a 20 20 20 20 20 20 20 TANDARD,.
c34e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c34f0 20 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e &fsts3ConfigIn
c3500 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 53 fo, sizeof(FILES
c3510 54 41 54 55 53 33 29 20 29 3b 0a 20 20 66 72 65 TATUS3) );. fre
c3520 65 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 e( zFilenameCp )
c3530 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 41 ;. OSTRACE4( "A
c3540 43 43 45 53 53 20 66 73 74 73 33 43 6f 6e 66 69 CCESS fsts3Confi
c3550 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 3d 25 gInfo.attrFile=%
c3560 64 20 66 6c 61 67 73 3d 25 64 20 72 63 3d 25 64 d flags=%d rc=%d
c3570 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
c3580 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f fsts3ConfigInfo
c3590 2e 61 74 74 72 46 69 6c 65 2c 20 66 6c 61 67 73 .attrFile, flags
c35a0 2c 20 72 63 20 29 3b 0a 20 20 73 77 69 74 63 68 , rc );. switch
c35b0 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 ( flags ){. c
c35c0 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 ase SQLITE_ACCES
c35d0 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73 65 S_READ:. case
c35e0 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 SQLITE_ACCESS_E
c35f0 58 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63 20 XISTS:. rc
c3600 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f = (rc == NO_ERRO
c3610 52 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 R);. OSTRAC
c3620 45 33 28 20 22 41 43 43 45 53 53 20 25 73 20 61 E3( "ACCESS %s a
c3630 63 63 65 73 73 20 6f 66 20 72 65 61 64 20 61 6e ccess of read an
c3640 64 20 65 78 69 73 74 73 20 20 72 63 3d 25 64 5c d exists rc=%d\
c3650 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 n", zFilename, r
c3660 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b c );. break
c3670 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 ;. case SQLIT
c3680 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 E_ACCESS_READWRI
c3690 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 TE:. rc = (
c36a0 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 rc == NO_ERROR)
c36b0 26 26 20 28 20 28 66 73 74 73 33 43 6f 6e 66 69 && ( (fsts3Confi
c36c0 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 20 26 gInfo.attrFile &
c36d0 20 46 49 4c 45 5f 52 45 41 44 4f 4e 4c 59 29 20 FILE_READONLY)
c36e0 3d 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 4f 53 == 0 );. OS
c36f0 54 52 41 43 45 33 28 20 22 41 43 43 45 53 53 20 TRACE3( "ACCESS
c3700 25 73 20 61 63 63 65 73 73 20 6f 66 20 72 65 61 %s access of rea
c3710 64 2f 77 72 69 74 65 20 20 72 63 3d 25 64 5c 6e d/write rc=%d\n
c3720 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63 ", zFilename, rc
c3730 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b );. break;
c3740 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 . default:.
c3750 20 20 20 20 61 73 73 65 72 74 28 20 21 22 49 6e assert( !"In
c3760 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 valid flags argu
c3770 6d 65 6e 74 22 20 29 3b 0a 20 20 7d 0a 20 20 2a ment" );. }. *
c3780 70 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 pOut = rc;. ret
c3790 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
c37a0 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
c37b0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
c37c0 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 NSION./*.** Inte
c37d0 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 rfaces for openi
c37e0 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 ng a shared libr
c37f0 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 ary, finding ent
c3800 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 ry points.** wit
c3810 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c hin the shared l
c3820 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 ibrary, and clos
c3830 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c ing the shared l
c3840 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a ibrary..*/./*.**
c3850 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 Interfaces for
c3860 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 opening a shared
c3870 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e library, findin
c3880 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a g entry points.*
c3890 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 * within the sha
c38a0 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 red library, and
c38b0 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 closing the sha
c38c0 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a red library..*/.
c38d0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 32 static void *os2
c38e0 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 DlOpen(sqlite3_v
c38f0 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 fs *pVfs, const
c3900 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 char *zFilename)
c3910 7b 0a 20 20 55 43 48 41 52 20 6c 6f 61 64 45 72 {. UCHAR loadEr
c3920 72 5b 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55 4c r[256];. HMODUL
c3930 45 20 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45 54 E hmod;. APIRET
c3940 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 rc;. char *zFi
c3950 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 lenameCp = conve
c3960 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 7a rtUtf8PathToCp(z
c3970 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 63 20 Filename);. rc
c3980 3d 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 28 = DosLoadModule(
c3990 28 50 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73 69 (PSZ)loadErr, si
c39a0 7a 65 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20 7a zeof(loadErr), z
c39b0 46 69 6c 65 6e 61 6d 65 43 70 2c 20 26 68 6d 6f FilenameCp, &hmo
c39c0 64 29 3b 0a 20 20 66 72 65 65 28 7a 46 69 6c 65 d);. free(zFile
c39d0 6e 61 6d 65 43 70 29 3b 0a 20 20 72 65 74 75 72 nameCp);. retur
c39e0 6e 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 n rc != NO_ERROR
c39f0 20 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 68 6d ? 0 : (void*)hm
c3a00 6f 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e 6f od;.}./*.** A no
c3a10 2d 6f 70 20 73 69 6e 63 65 20 74 68 65 20 65 72 -op since the er
c3a20 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
c3a30 72 6e 65 64 20 6f 6e 20 74 68 65 20 44 6f 73 4c rned on the DosL
c3a40 6f 61 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e 0a oadModule call..
c3a50 2a 2a 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65 74 ** os2Dlopen ret
c3a60 75 72 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f 73 urns zero if Dos
c3a70 4c 6f 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e 6f LoadModule is no
c3a80 74 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2f t successful..*/
c3a90 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 .static void os2
c3aa0 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f DlError(sqlite3_
c3ab0 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e vfs *pVfs, int n
c3ac0 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f Buf, char *zBufO
c3ad0 75 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f ut){./* no-op */
c3ae0 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a .}.static void *
c3af0 6f 73 32 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 os2DlSym(sqlite3
c3b00 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 _vfs *pVfs, void
c3b10 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 *pHandle, const
c3b20 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b char *zSymbol){
c3b30 0a 20 20 50 46 4e 20 70 66 6e 3b 0a 20 20 41 50 . PFN pfn;. AP
c3b40 49 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d 20 IRET rc;. rc =
c3b50 44 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64 72 DosQueryProcAddr
c3b60 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c ((HMODULE)pHandl
c3b70 65 2c 20 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 e, 0L, zSymbol,
c3b80 26 70 66 6e 29 3b 0a 20 20 69 66 28 20 72 63 20 &pfn);. if( rc
c3b90 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 != NO_ERROR ){.
c3ba0 20 20 20 2f 2a 20 69 66 20 74 68 65 20 73 79 6d /* if the sym
c3bb0 62 6f 6c 20 69 74 73 65 6c 66 20 77 61 73 20 6e bol itself was n
c3bc0 6f 74 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 68 ot found, search
c3bd0 20 61 67 61 69 6e 20 66 6f 72 20 74 68 65 20 73 again for the s
c3be0 61 6d 65 0a 20 20 20 20 20 2a 20 73 79 6d 62 6f ame. * symbo
c3bf0 6c 20 77 69 74 68 20 61 6e 20 65 78 74 72 61 20 l with an extra
c3c00 75 6e 64 65 72 73 63 6f 72 65 2c 20 74 68 61 74 underscore, that
c3c10 20 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65 64 might be needed
c3c20 20 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20 20 depending.
c3c30 2a 20 6f 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 * on the calling
c3c40 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a 20 convention */.
c3c50 20 20 20 63 68 61 72 20 5f 7a 53 79 6d 62 6f 6c char _zSymbol
c3c60 5b 32 35 36 5d 20 3d 20 22 5f 22 3b 0a 20 20 20 [256] = "_";.
c3c70 20 73 74 72 6e 63 61 74 28 5f 7a 53 79 6d 62 6f strncat(_zSymbo
c3c80 6c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 32 35 35 29 l, zSymbol, 255)
c3c90 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 51 75 ;. rc = DosQu
c3ca0 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d 4f eryProcAddr((HMO
c3cb0 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c DULE)pHandle, 0L
c3cc0 2c 20 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e , _zSymbol, &pfn
c3cd0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
c3ce0 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f rc != NO_ERROR ?
c3cf0 20 30 20 3a 20 28 76 6f 69 64 2a 29 70 66 6e 3b 0 : (void*)pfn;
c3d00 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f .}.static void o
c3d10 73 32 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 s2DlClose(sqlite
c3d20 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 3_vfs *pVfs, voi
c3d30 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 44 d *pHandle){. D
c3d40 6f 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48 4d osFreeModule((HM
c3d50 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a ODULE)pHandle);.
c3d60 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 }.#else /* if SQ
c3d70 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 LITE_OMIT_LOAD_E
c3d80 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 XTENSION is defi
c3d90 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e ned: */. #defin
c3da0 65 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0a 20 20 e os2DlOpen 0.
c3db0 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72 72 #define os2DlErr
c3dc0 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 6f or 0. #define o
c3dd0 73 32 44 6c 53 79 6d 20 30 0a 20 20 23 64 65 66 s2DlSym 0. #def
c3de0 69 6e 65 20 6f 73 32 44 6c 43 6c 6f 73 65 20 30 ine os2DlClose 0
c3df0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
c3e00 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 66 Write up to nBuf
c3e10 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d bytes of random
c3e20 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a ness into zBuf..
c3e30 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
c3e40 32 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 2Randomness(sqli
c3e50 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
c3e60 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a nt nBuf, char *z
c3e70 42 75 66 20 29 7b 0a 20 20 69 6e 74 20 6e 20 3d Buf ){. int n =
c3e80 20 30 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 0;.#if defined(
c3e90 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 6e SQLITE_TEST). n
c3ea0 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 = nBuf;. memse
c3eb0 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 t(zBuf, 0, nBuf)
c3ec0 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73 69 ;.#else. int si
c3ed0 7a 65 6f 66 55 4c 6f 6e 67 20 3d 20 73 69 7a 65 zeofULong = size
c3ee0 6f 66 28 55 4c 4f 4e 47 29 3b 0a 20 20 69 66 28 of(ULONG);. if(
c3ef0 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 44 41 54 (int)sizeof(DAT
c3f00 45 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66 20 2d ETIME) <= nBuf -
c3f10 20 6e 20 29 7b 0a 20 20 20 20 44 41 54 45 54 49 n ){. DATETI
c3f20 4d 45 20 78 3b 0a 20 20 20 20 44 6f 73 47 65 74 ME x;. DosGet
c3f30 44 61 74 65 54 69 6d 65 28 26 78 29 3b 0a 20 20 DateTime(&x);.
c3f40 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e memcpy(&zBuf[n
c3f50 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 ], &x, sizeof(x)
c3f60 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 );. n += size
c3f70 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 of(x);. }.. if
c3f80 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d ( sizeofULong <=
c3f90 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 nBuf - n ){.
c3fa0 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 PPIB ppib;.
c3fb0 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 DosGetInfoBlocks
c3fc0 28 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b 0a 20 (NULL, &ppib);.
c3fd0 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b memcpy(&zBuf[
c3fe0 6e 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62 5f 75 n], &ppib->pib_u
c3ff0 6c 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e lpid, sizeofULon
c4000 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a g);. n += siz
c4010 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 eofULong;. }..
c4020 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 if( sizeofULong
c4030 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a <= nBuf - n ){.
c4040 20 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 PTIB ptib;.
c4050 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f DosGetInfoBlo
c4060 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 cks(&ptib, NULL)
c4070 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 ;. memcpy(&zB
c4080 75 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e 74 69 uf[n], &ptib->ti
c4090 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c b_ptib2->tib2_ul
c40a0 74 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 tid, sizeofULong
c40b0 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 );. n += size
c40c0 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 ofULong;. }..
c40d0 2f 2a 20 69 66 20 77 65 20 73 74 69 6c 6c 20 68 /* if we still h
c40e0 61 76 65 6e 27 74 20 66 69 6c 6c 65 64 20 74 68 aven't filled th
c40f0 65 20 62 75 66 66 65 72 20 79 65 74 20 74 68 65 e buffer yet the
c4100 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c 20 following will
c4110 2a 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65 76 65 */. /* grab eve
c4120 72 79 74 68 69 6e 67 20 6f 6e 63 65 20 69 6e 73 rything once ins
c4130 74 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67 20 73 tead of making s
c4140 65 76 65 72 61 6c 20 63 61 6c 6c 73 20 66 6f 72 everal calls for
c4150 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 2a a single item *
c4160 2f 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c /. if( sizeofUL
c4170 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 ong <= nBuf - n
c4180 29 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75 6c 53 ){. ULONG ulS
c4190 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d 3b ysInfo[QSV_MAX];
c41a0 0a 20 20 20 20 44 6f 73 51 75 65 72 79 53 79 73 . DosQuerySys
c41b0 49 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d 41 58 Info(1L, QSV_MAX
c41c0 2c 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73 69 7a , ulSysInfo, siz
c41d0 65 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56 5f 4d eofULong * QSV_M
c41e0 41 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 AX);.. memcpy
c41f0 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 (&zBuf[n], &ulSy
c4200 73 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f 55 sInfo[QSV_MS_COU
c4210 4e 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 NT - 1], sizeofU
c4220 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 Long);. n +=
c4230 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20 20 sizeofULong;..
c4240 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e if( sizeofULon
c4250 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b g <= nBuf - n ){
c4260 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a . memcpy(&z
c4270 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e Buf[n], &ulSysIn
c4280 66 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49 4e 54 fo[QSV_TIMER_INT
c4290 45 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69 7a 65 ERVAL - 1], size
c42a0 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 ofULong);.
c42b0 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 n += sizeofULong
c42c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
c42d0 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e sizeofULong <= n
c42e0 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 Buf - n ){.
c42f0 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d memcpy(&zBuf[n]
c4300 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 , &ulSysInfo[QSV
c4310 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c 20 _TIME_LOW - 1],
c4320 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 sizeofULong);.
c4330 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 n += sizeofU
c4340 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Long;. }.
c4350 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 if( sizeofULong
c4360 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 <= nBuf - n ){.
c4370 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 memcpy(&zBu
c4380 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f f[n], &ulSysInfo
c4390 5b 51 53 56 5f 54 49 4d 45 5f 48 49 47 48 20 2d [QSV_TIME_HIGH -
c43a0 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 1], sizeofULong
c43b0 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 );. n += si
c43c0 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d zeofULong;. }
c43d0 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 . if( sizeofU
c43e0 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e Long <= nBuf - n
c43f0 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 ){. memcpy
c4400 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 (&zBuf[n], &ulSy
c4410 73 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41 56 41 sInfo[QSV_TOTAVA
c4420 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69 7a 65 ILMEM - 1], size
c4430 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 ofULong);.
c4440 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 n += sizeofULong
c4450 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 ;. }. }.#end
c4460 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a if.. return n;.
c4470 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 }../*.** Sleep f
c4480 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c or a little whil
c4490 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 e. Return the a
c44a0 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c mount of time sl
c44b0 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 ept..** The argu
c44c0 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d 62 ment is the numb
c44d0 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e er of microsecon
c44e0 64 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73 6c ds we want to sl
c44f0 65 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 eep..** The retu
c4500 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 rn value is the
c4510 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 number of micros
c4520 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 econds of sleep
c4530 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 actually.** requ
c4540 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 75 ested from the u
c4550 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 nderlying operat
c4560 69 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 ing system, a nu
c4570 6d 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 mber which.** mi
c4580 67 68 74 20 62 65 20 67 72 65 61 74 65 72 20 74 ght be greater t
c4590 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
c45a0 74 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 the argument, bu
c45b0 74 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 t not less.** th
c45c0 61 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e an the argument.
c45d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
c45e0 73 32 53 6c 65 65 70 28 20 73 71 6c 69 74 65 33 s2Sleep( sqlite3
c45f0 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 _vfs *pVfs, int
c4600 6d 69 63 72 6f 73 65 63 20 29 7b 0a 20 20 44 6f microsec ){. Do
c4610 73 53 6c 65 65 70 28 20 28 6d 69 63 72 6f 73 65 sSleep( (microse
c4620 63 2f 31 30 30 30 29 20 29 3b 0a 20 20 72 65 74 c/1000) );. ret
c4630 75 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0a 7d 0a urn microsec;.}.
c4640 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
c4650 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69 wing variable, i
c4660 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a f set to a non-z
c4670 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d ero value, becom
c4680 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a es the result.**
c4690 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 returned from s
c46a0 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 qlite3OsCurrentT
c46b0 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 20 ime(). This is
c46c0 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 used for testing
c46d0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
c46e0 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
c46f0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
c4700 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b urrent_time = 0;
c4710 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 .#endif../*.** F
c4720 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 ind the current
c4730 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73 time (in Univers
c4740 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 al Coordinated T
c4750 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 65 ime). Write the
c4760 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d 65 .** current time
c4770 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a and date as a J
c4780 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 ulian Day number
c4790 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 into *prNow and
c47a0 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52 .** return 0. R
c47b0 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 74 eturn 1 if the t
c47c0 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e ime and date can
c47d0 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f not be found..*/
c47e0 0a 69 6e 74 20 6f 73 32 43 75 72 72 65 6e 74 54 .int os2CurrentT
c47f0 69 6d 65 28 20 73 71 6c 69 74 65 33 5f 76 66 73 ime( sqlite3_vfs
c4800 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a *pVfs, double *
c4810 70 72 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62 6c prNow ){. doubl
c4820 65 20 6e 6f 77 3b 0a 20 20 53 48 4f 52 54 20 6d e now;. SHORT m
c4830 69 6e 75 74 65 3b 20 2f 2a 20 6e 65 65 64 73 20 inute; /* needs
c4840 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 63 6f to be able to co
c4850 70 65 20 77 69 74 68 20 6e 65 67 61 74 69 76 65 pe with negative
c4860 20 74 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 timezone offset
c4870 20 2a 2f 0a 20 20 55 53 48 4f 52 54 20 73 65 63 */. USHORT sec
c4880 6f 6e 64 2c 20 68 6f 75 72 2c 0a 20 20 20 20 20 ond, hour,.
c4890 20 20 20 20 64 61 79 2c 20 6d 6f 6e 74 68 2c 20 day, month,
c48a0 79 65 61 72 3b 0a 20 20 44 41 54 45 54 49 4d 45 year;. DATETIME
c48b0 20 64 74 3b 0a 20 20 44 6f 73 47 65 74 44 61 74 dt;. DosGetDat
c48c0 65 54 69 6d 65 28 20 26 64 74 20 29 3b 0a 20 20 eTime( &dt );.
c48d0 73 65 63 6f 6e 64 20 3d 20 28 55 53 48 4f 52 54 second = (USHORT
c48e0 29 64 74 2e 73 65 63 6f 6e 64 73 3b 0a 20 20 6d )dt.seconds;. m
c48f0 69 6e 75 74 65 20 3d 20 28 53 48 4f 52 54 29 64 inute = (SHORT)d
c4900 74 2e 6d 69 6e 75 74 65 73 20 2b 20 64 74 2e 74 t.minutes + dt.t
c4910 69 6d 65 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72 20 imezone;. hour
c4920 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 68 6f 75 = (USHORT)dt.hou
c4930 72 73 3b 0a 20 20 64 61 79 20 3d 20 28 55 53 48 rs;. day = (USH
c4940 4f 52 54 29 64 74 2e 64 61 79 3b 0a 20 20 6d 6f ORT)dt.day;. mo
c4950 6e 74 68 20 3d 20 28 55 53 48 4f 52 54 29 64 74 nth = (USHORT)dt
c4960 2e 6d 6f 6e 74 68 3b 0a 20 20 79 65 61 72 20 3d .month;. year =
c4970 20 28 55 53 48 4f 52 54 29 64 74 2e 79 65 61 72 (USHORT)dt.year
c4980 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 ;.. /* Calculat
c4990 69 6f 6e 73 20 66 72 6f 6d 20 68 74 74 70 3a 2f ions from http:/
c49a0 2f 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 /www.astro.keele
c49b0 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 .ac.uk/~rno/Astr
c49c0 6f 6e 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a 20 onomy/hjd.html.
c49d0 20 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61 http://www.a
c49e0 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b stro.keele.ac.uk
c49f0 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f /~rno/Astronomy/
c4a00 68 6a 64 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20 2f hjd-0.1.c */. /
c4a10 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 20 * Calculate the
c4a20 4a 75 6c 69 61 6e 20 64 61 79 73 20 2a 2f 0a 20 Julian days */.
c4a30 20 6e 6f 77 20 3d 20 64 61 79 20 2d 20 33 32 30 now = day - 320
c4a40 37 36 20 2b 0a 20 20 20 20 31 34 36 31 2a 28 79 76 +. 1461*(y
c4a50 65 61 72 20 2b 20 34 38 30 30 20 2b 20 28 6d 6f ear + 4800 + (mo
c4a60 6e 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 34 20 nth - 14)/12)/4
c4a70 2b 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e 74 68 +. 367*(month
c4a80 20 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20 2d 20 - 2 - (month -
c4a90 31 34 29 2f 31 32 2a 31 32 29 2f 31 32 20 2d 0a 14)/12*12)/12 -.
c4aa0 20 20 20 20 33 2a 28 28 79 65 61 72 20 2b 20 34 3*((year + 4
c4ab0 39 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 900 + (month - 1
c4ac0 34 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b 0a 0a 4)/12)/100)/4;..
c4ad0 20 20 2f 2a 20 41 64 64 20 74 68 65 20 66 72 61 /* Add the fra
c4ae0 63 74 69 6f 6e 61 6c 20 68 6f 75 72 73 2c 20 6d ctional hours, m
c4af0 69 6e 73 20 61 6e 64 20 73 65 63 6f 6e 64 73 20 ins and seconds
c4b00 2a 2f 0a 20 20 6e 6f 77 20 2b 3d 20 28 68 6f 75 */. now += (hou
c4b10 72 20 2b 20 31 32 2e 30 29 2f 32 34 2e 30 3b 0a r + 12.0)/24.0;.
c4b20 20 20 6e 6f 77 20 2b 3d 20 6d 69 6e 75 74 65 2f now += minute/
c4b30 31 34 34 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 1440.0;. now +=
c4b40 20 73 65 63 6f 6e 64 2f 38 36 34 30 30 2e 30 3b second/86400.0;
c4b50 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77 3b . *prNow = now;
c4b60 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
c4b70 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 EST. if( sqlite
c4b80 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 3_current_time )
c4b90 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 {. *prNow = s
c4ba0 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 qlite3_current_t
c4bb0 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 ime/86400.0 + 24
c4bc0 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 40587.5;. }.#en
c4bd0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a dif. return 0;.
c4be0 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 }..static int os
c4bf0 32 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 2GetLastError(sq
c4c00 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
c4c10 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 int nBuf, char
c4c20 2a 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72 6e *zBuf){. return
c4c30 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 0;.}../*.** Ini
c4c40 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e tialize and dein
c4c50 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 itialize the ope
c4c60 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e rating system in
c4c70 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 terface..*/.SQLI
c4c80 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
c4c90 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 e3_os_init(void)
c4ca0 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 {. static sqlit
c4cb0 65 33 5f 76 66 73 20 6f 73 32 56 66 73 20 3d 20 e3_vfs os2Vfs =
c4cc0 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 {. 1,
c4cd0 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 /* iVer
c4ce0 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 sion */. size
c4cf0 6f 66 28 6f 73 32 46 69 6c 65 29 2c 20 20 20 2f of(os2File), /
c4d00 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 * szOsFile */.
c4d10 20 20 43 43 48 4d 41 58 50 41 54 48 2c 20 20 20 CCHMAXPATH,
c4d20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 /* mxPathna
c4d30 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 me */. 0,
c4d40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c4d50 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 6f 73 pNext */. "os
c4d60 32 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2",
c4d70 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 /* zName */.
c4d80 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
c4d90 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a /* pAppData *
c4da0 2f 0a 0a 20 20 20 20 6f 73 32 4f 70 65 6e 2c 20 /.. os2Open,
c4db0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 /* xOp
c4dc0 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 65 6c en */. os2Del
c4dd0 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ete, /*
c4de0 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 6f xDelete */. o
c4df0 73 32 41 63 63 65 73 73 2c 20 20 20 20 20 20 20 s2Access,
c4e00 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a /* xAccess */.
c4e10 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e os2FullPathn
c4e20 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 ame, /* xFullP
c4e30 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6f athname */. o
c4e40 73 32 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 s2DlOpen,
c4e50 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a /* xDlOpen */.
c4e60 20 20 20 20 6f 73 32 44 6c 45 72 72 6f 72 2c 20 os2DlError,
c4e70 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 /* xDlErr
c4e80 6f 72 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 53 or */. os2DlS
c4e90 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ym, /*
c4ea0 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 6f 73 xDlSym */. os
c4eb0 32 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 2DlClose,
c4ec0 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a /* xDlClose */.
c4ed0 20 20 20 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 73 os2Randomnes
c4ee0 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f s, /* xRando
c4ef0 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32 mness */. os2
c4f00 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 Sleep,
c4f10 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 /* xSleep */.
c4f20 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 2c os2CurrentTime,
c4f30 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 /* xCurrentT
c4f40 69 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 47 65 ime */. os2Ge
c4f50 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f 2a tLastError /*
c4f60 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a xGetLastError *
c4f70 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 /. };. sqlite3
c4f80 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 6f _vfs_register(&o
c4f90 73 32 56 66 73 2c 20 31 29 3b 0a 20 20 69 6e 69 s2Vfs, 1);. ini
c4fa0 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b tUconvObjects();
c4fb0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
c4fc0 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 _OK;.}.SQLITE_AP
c4fd0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 I int sqlite3_os
c4fe0 5f 65 6e 64 28 76 6f 69 64 29 7b 0a 20 20 66 72 _end(void){. fr
c4ff0 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 eeUconvObjects()
c5000 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
c5010 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 E_OK;.}..#endif
c5020 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 /* SQLITE_OS_OS2
c5030 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
c5040 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 6f **** End of os_o
c5050 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s2.c ***********
c5060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5080 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
c5090 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
c50a0 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a os_unix.c ******
c50b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c50c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c50d0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 ***/./*.** 2004
c50e0 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 May 22.**.** The
c50f0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
c5100 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
c5110 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
c5120 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
c5130 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
c5140 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
c5150 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
c5160 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
c5170 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
c5180 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
c5190 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
c51a0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
c51b0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
c51c0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
c51d0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
c51e0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
c51f0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
c5200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
c5250 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
c5260 69 6e 73 20 74 68 65 20 56 46 53 20 69 6d 70 6c ins the VFS impl
c5270 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 75 ementation for u
c5280 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74 69 nix-like operati
c5290 6e 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20 69 6e ng systems.** in
c52a0 63 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d 61 63 clude Linux, Mac
c52b0 4f 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58 2c 20 OSX, *BSD, QNX,
c52c0 56 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20 48 50 VxWorks, AIX, HP
c52d0 55 58 2c 20 61 6e 64 20 6f 74 68 65 72 73 2e 0a UX, and others..
c52e0 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 **.** There are
c52f0 61 63 74 75 61 6c 6c 79 20 73 65 76 65 72 61 6c actually several
c5300 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 20 69 different VFS i
c5310 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 69 mplementations i
c5320 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 n this file..**
c5330 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 20 The differences
c5340 61 72 65 20 69 6e 20 74 68 65 20 77 61 79 20 74 are in the way t
c5350 68 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 hat file locking
c5360 20 69 73 20 64 6f 6e 65 2e 20 20 54 68 65 20 64 is done. The d
c5370 65 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65 6d efault.** implem
c5380 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 50 6f entation uses Po
c5390 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 six Advisory Loc
c53a0 6b 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 76 65 ks. Alternative
c53b0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
c53c0 0a 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29 2c .** use flock(),
c53d0 20 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 72 69 dot-files, vari
c53e0 6f 75 73 20 70 72 6f 70 72 69 65 74 61 72 79 20 ous proprietary
c53f0 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 73 2c locking schemas,
c5400 20 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20 73 6b or simply.** sk
c5410 69 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 74 ip locking all t
c5420 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 ogether..**.** T
c5430 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20 his source file
c5440 69 73 20 6f 72 67 61 6e 69 7a 65 64 20 69 6e 74 is organized int
c5450 6f 20 64 69 76 69 73 69 6f 6e 73 20 77 68 65 72 o divisions wher
c5460 65 20 74 68 65 20 6c 6f 67 69 63 20 66 6f 72 20 e the logic for
c5470 76 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62 66 75 various.** subfu
c5480 6e 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e 74 61 nctions is conta
c5490 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 ined within the
c54a0 61 70 70 72 6f 70 72 69 61 74 65 20 64 69 76 69 appropriate divi
c54b0 73 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a 2a 2a sion. PLEASE.**
c54c0 20 4b 45 45 50 20 54 48 45 20 53 54 52 55 43 54 KEEP THE STRUCT
c54d0 55 52 45 20 4f 46 20 54 48 49 53 20 46 49 4c 45 URE OF THIS FILE
c54e0 20 49 4e 54 41 43 54 2e 20 20 4e 65 77 20 63 6f INTACT. New co
c54f0 64 65 20 73 68 6f 75 6c 64 20 62 65 20 70 6c 61 de should be pla
c5500 63 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f ced.** in the co
c5510 72 72 65 63 74 20 64 69 76 69 73 69 6f 6e 20 61 rrect division a
c5520 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 65 nd should be cle
c5530 61 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a 2a 2a arly labeled..**
c5540 0a 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74 20 6f .** The layout o
c5550 66 20 64 69 76 69 73 69 6f 6e 73 20 69 73 20 61 f divisions is a
c5560 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
c5570 20 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d 70 75 * General-pu
c5580 72 70 6f 73 65 20 64 65 63 6c 61 72 61 74 69 6f rpose declaratio
c5590 6e 73 20 61 6e 64 20 75 74 69 6c 69 74 79 20 66 ns and utility f
c55a0 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a unctions..** *
c55b0 20 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44 Unique file ID
c55c0 20 6c 6f 67 69 63 20 75 73 65 64 20 62 79 20 56 logic used by V
c55d0 78 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20 20 xWorks..** *
c55e0 56 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 Various locking
c55f0 70 72 69 6d 69 74 69 76 65 20 69 6d 70 6c 65 6d primitive implem
c5600 65 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c 20 65 entations (all e
c5610 78 63 65 70 74 20 70 72 6f 78 79 20 6c 6f 63 6b xcept proxy lock
c5620 69 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20 2b 20 ing):.** +
c5630 66 6f 72 20 50 6f 73 69 78 20 41 64 76 69 73 6f for Posix Adviso
c5640 72 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 ry Locks.**
c5650 20 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f 63 + for no-op loc
c5660 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 ks.** + for
c5670 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 0a dot-file locks.
c5680 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 66 6c ** + for fl
c5690 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a 2a ock() locking.**
c56a0 20 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 6d 65 + for name
c56b0 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b d semaphore lock
c56c0 73 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c 79 29 s (VxWorks only)
c56d0 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 41 .** + for A
c56e0 46 50 20 66 69 6c 65 73 79 73 74 65 6d 20 6c 6f FP filesystem lo
c56f0 63 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 cks (MacOSX only
c5700 29 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 ).** * sqlite
c5710 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 6e 3_file methods n
c5720 6f 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ot associated wi
c5730 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 th locking..**
c5740 20 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73 20 * Definitions
c5750 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 of sqlite3_io_me
c5760 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 6f thods objects fo
c5770 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a 2a r all locking.**
c5780 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 70 6c methods pl
c5790 75 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 us "finder" func
c57a0 74 69 6f 6e 73 20 66 6f 72 20 65 61 63 68 20 6c tions for each l
c57b0 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e 0a 2a ocking method..*
c57c0 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f 76 * * sqlite3_v
c57d0 66 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c 65 6d fs method implem
c57e0 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 entations..**
c57f0 2a 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 * Locking primi
c5800 74 69 76 65 73 20 66 6f 72 20 74 68 65 20 70 72 tives for the pr
c5810 6f 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 oxy uber-locking
c5820 2d 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f 53 58 -method. (MacOSX
c5830 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 44 only).** * D
c5840 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 efinitions of sq
c5850 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 lite3_vfs object
c5860 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e s for all lockin
c5870 67 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20 20 20 g methods.**
c5880 20 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65 6e 74 plus implement
c5890 61 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 ations of sqlite
c58a0 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 3_os_init() and
c58b0 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 sqlite3_os_end()
c58c0 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f ..*/.#if SQLITE_
c58d0 4f 53 5f 55 4e 49 58 20 20 20 20 20 20 20 20 20 OS_UNIX
c58e0 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c /* This fil
c58f0 65 20 69 73 20 75 73 65 64 20 6f 6e 20 75 6e 69 e is used on uni
c5900 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a x only */../*.**
c5910 20 54 68 65 72 65 20 61 72 65 20 76 61 72 69 6f There are vario
c5920 75 73 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 66 us methods for f
c5930 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 64 ile locking used
c5940 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 63 79 for concurrency
c5950 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 3a 0a 2a 2a 0a .** control:.**.
c5960 2a 2a 20 20 20 31 2e 20 50 4f 53 49 58 20 6c 6f ** 1. POSIX lo
c5970 63 6b 69 6e 67 20 28 74 68 65 20 64 65 66 61 75 cking (the defau
c5980 6c 74 29 2c 0a 2a 2a 20 20 20 32 2e 20 4e 6f 20 lt),.** 2. No
c5990 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 33 2e locking,.** 3.
c59a0 20 44 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e Dot-file lockin
c59b0 67 2c 0a 2a 2a 20 20 20 34 2e 20 66 6c 6f 63 6b g,.** 4. flock
c59c0 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 () locking,.**
c59d0 20 35 2e 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20 5. AFP locking
c59e0 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 (OSX only),.**
c59f0 20 36 2e 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 6. Named POSIX
c5a00 73 65 6d 61 70 68 6f 72 65 73 20 28 56 58 57 6f semaphores (VXWo
c5a10 72 6b 73 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20 rks only),.**
c5a20 37 2e 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 7. proxy locking
c5a30 2e 20 28 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a 0a . (OSX only).**.
c5a40 2a 2a 20 53 74 79 6c 65 73 20 34 2c 20 35 2c 20 ** Styles 4, 5,
c5a50 61 6e 64 20 37 20 61 72 65 20 6f 6e 6c 79 20 61 and 7 are only a
c5a60 76 61 69 6c 61 62 6c 65 20 6f 66 20 53 51 4c 49 vailable of SQLI
c5a70 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
c5a80 47 5f 53 54 59 4c 45 0a 2a 2a 20 69 73 20 64 65 G_STYLE.** is de
c5a90 66 69 6e 65 64 20 74 6f 20 31 2e 20 20 54 68 65 fined to 1. The
c5aa0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
c5ab0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 61 6c 73 OCKING_STYLE als
c5ac0 6f 20 65 6e 61 62 6c 65 73 20 61 75 74 6f 6d 61 o enables automa
c5ad0 74 69 63 0a 2a 2a 20 73 65 6c 65 63 74 69 6f 6e tic.** selection
c5ae0 20 6f 66 20 74 68 65 20 61 70 70 72 6f 70 72 69 of the appropri
c5af0 61 74 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c ate locking styl
c5b00 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 66 e based on the f
c5b10 69 6c 65 73 79 73 74 65 6d 0a 2a 2a 20 77 68 65 ilesystem.** whe
c5b20 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 re the database
c5b30 69 73 20 6c 6f 63 61 74 65 64 2e 20 20 0a 2a 2f is located. .*/
c5b40 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
c5b50 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
c5b60 49 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20 69 66 ING_STYLE).# if
c5b70 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 defined(__APPLE
c5b80 5f 5f 29 0a 23 20 20 20 20 64 65 66 69 6e 65 20 __).# define
c5b90 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
c5ba0 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a 23 20 CKING_STYLE 1.#
c5bb0 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e else.# defin
c5bc0 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f e SQLITE_ENABLE_
c5bd0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 30 0a LOCKING_STYLE 0.
c5be0 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a # endif.#endif.
c5bf0 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 ./*.** Define th
c5c00 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 70 72 65 e OS_VXWORKS pre
c5c10 2d 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f -processor macro
c5c20 20 74 6f 20 31 20 69 66 20 62 75 69 6c 64 69 6e to 1 if buildin
c5c30 67 20 6f 6e 20 0a 2a 2a 20 76 78 77 6f 72 6b 73 g on .** vxworks
c5c40 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69 73 65 , or 0 otherwise
c5c50 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4f 53 5f ..*/.#ifndef OS_
c5c60 56 58 57 4f 52 4b 53 0a 23 20 20 69 66 20 64 65 VXWORKS.# if de
c5c70 66 69 6e 65 64 28 5f 5f 52 54 50 5f 5f 29 20 7c fined(__RTP__) |
c5c80 7c 20 64 65 66 69 6e 65 64 28 5f 57 52 53 5f 4b | defined(_WRS_K
c5c90 45 52 4e 45 4c 29 0a 23 20 20 20 20 64 65 66 69 ERNEL).# defi
c5ca0 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 31 0a ne OS_VXWORKS 1.
c5cb0 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 # else.# def
c5cc0 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 30 ine OS_VXWORKS 0
c5cd0 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 .# endif.#endif
c5ce0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 ../*.** These #d
c5cf0 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e efines should en
c5d00 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 able >2GB file s
c5d10 75 70 70 6f 72 74 20 6f 6e 20 50 6f 73 69 78 20 upport on Posix
c5d20 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c if the.** underl
c5d30 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 ying operating s
c5d40 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 ystem supports i
c5d50 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 t. If the OS la
c5d60 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c cks.** large fil
c5d70 65 20 73 75 70 70 6f 72 74 2c 20 74 68 65 73 65 e support, these
c5d80 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f 70 should be no-op
c5d90 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 66 s..**.** Large f
c5da0 69 6c 65 20 73 75 70 70 6f 72 74 20 63 61 6e 20 ile support can
c5db0 62 65 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e be disabled usin
c5dc0 67 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 44 g the -DSQLITE_D
c5dd0 49 53 41 42 4c 45 5f 4c 46 53 20 73 77 69 74 63 ISABLE_LFS switc
c5de0 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70 h.** on the comp
c5df0 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e iler command lin
c5e00 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63 65 e. This is nece
c5e10 73 73 61 72 79 20 69 66 20 79 6f 75 20 61 72 65 ssary if you are
c5e20 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e compiling.** on
c5e30 20 61 20 72 65 63 65 6e 74 20 6d 61 63 68 69 6e a recent machin
c5e40 65 20 28 65 78 3a 20 52 65 64 48 61 74 20 37 2e e (ex: RedHat 7.
c5e50 32 29 20 62 75 74 20 79 6f 75 20 77 61 6e 74 20 2) but you want
c5e60 79 6f 75 72 20 63 6f 64 65 20 74 6f 20 77 6f 72 your code to wor
c5e70 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 72 k.** on an older
c5e80 20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 machine (ex: Re
c5e90 64 48 61 74 20 36 2e 30 29 2e 20 20 49 66 20 79 dHat 6.0). If y
c5ea0 6f 75 20 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65 ou compile on Re
c5eb0 64 48 61 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68 dHat 7.2.** with
c5ec0 6f 75 74 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c out this option,
c5ed0 20 4c 46 53 20 69 73 20 65 6e 61 62 6c 65 2e 20 LFS is enable.
c5ee0 20 42 75 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f But LFS does no
c5ef0 74 20 65 78 69 73 74 20 69 6e 20 74 68 65 20 6b t exist in the k
c5f00 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 48 ernel.** in RedH
c5f10 61 74 20 36 2e 30 2c 20 73 6f 20 74 68 65 20 63 at 6.0, so the c
c5f20 6f 64 65 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20 ode won't work.
c5f30 20 48 65 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69 Hence, for maxi
c5f40 6d 75 6d 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f mum binary.** po
c5f50 72 74 61 62 69 6c 69 74 79 20 79 6f 75 20 73 68 rtability you sh
c5f60 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a ould omit LFS..*
c5f70 2a 0a 2a 2a 20 54 68 65 20 70 72 65 76 69 6f 75 *.** The previou
c5f80 73 20 70 61 72 61 67 72 61 70 68 20 77 61 73 20 s paragraph was
c5f90 77 72 69 74 74 65 6e 20 69 6e 20 32 30 30 35 2e written in 2005.
c5fa0 20 20 28 54 68 69 73 20 70 61 72 61 67 72 61 70 (This paragrap
c5fb0 68 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 h is written.**
c5fc0 6f 6e 20 32 30 30 38 2d 31 31 2d 32 38 2e 29 20 on 2008-11-28.)
c5fd0 54 68 65 73 65 20 64 61 79 73 2c 20 61 6c 6c 20 These days, all
c5fe0 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 73 75 Linux kernels su
c5ff0 70 70 6f 72 74 20 6c 61 72 67 65 20 66 69 6c 65 pport large file
c6000 73 2c 20 73 6f 0a 2a 2a 20 79 6f 75 20 73 68 6f s, so.** you sho
c6010 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 6c 65 61 uld probably lea
c6020 76 65 20 4c 46 53 20 65 6e 61 62 6c 65 64 2e 20 ve LFS enabled.
c6030 20 42 75 74 20 73 6f 6d 65 20 65 6d 62 65 64 64 But some embedd
c6040 65 64 20 70 6c 61 74 66 6f 72 6d 73 20 6d 69 67 ed platforms mig
c6050 68 74 0a 2a 2a 20 6c 61 63 6b 20 4c 46 53 20 69 ht.** lack LFS i
c6060 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 n which case the
c6070 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f SQLITE_DISABLE_
c6080 4c 46 53 20 6d 61 63 72 6f 20 6d 69 67 68 74 20 LFS macro might
c6090 73 74 69 6c 6c 20 62 65 20 75 73 65 66 75 6c 2e still be useful.
c60a0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
c60b0 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23 TE_DISABLE_LFS.#
c60c0 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f 46 define _LARGE_F
c60d0 49 4c 45 20 20 20 20 20 20 20 31 0a 23 20 69 66 ILE 1.# if
c60e0 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53 45 ndef _FILE_OFFSE
c60f0 54 5f 42 49 54 53 0a 23 20 20 20 64 65 66 69 6e T_BITS.# defin
c6100 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 e _FILE_OFFSET_B
c6110 49 54 53 20 36 34 0a 23 20 65 6e 64 69 66 0a 23 ITS 64.# endif.#
c6120 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 46 49 define _LARGEFI
c6130 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e 64 LE_SOURCE 1.#end
c6140 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 6e 64 61 if../*.** standa
c6150 72 64 20 69 6e 63 6c 75 64 65 20 66 69 6c 65 73 rd include files
c6160 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 ..*/.#include <s
c6170 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 ys/types.h>.#inc
c6180 6c 75 64 65 20 3c 73 79 73 2f 73 74 61 74 2e 68 lude <sys/stat.h
c6190 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e 74 >.#include <fcnt
c61a0 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 l.h>.#include <u
c61b0 6e 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 nistd.h>.#includ
c61c0 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68 3e 0a 23 e <sys/time.h>.#
c61d0 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e 6f 2e 68 include <errno.h
c61e0 3e 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e >..#if SQLITE_EN
c61f0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
c6200 4c 45 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 LE.# include <sy
c6210 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 20 69 66 20 s/ioctl.h>.# if
c6220 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 6e OS_VXWORKS.# in
c6230 63 6c 75 64 65 20 3c 73 65 6d 61 70 68 6f 72 65 clude <semaphore
c6240 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c .h>.# include <
c6250 6c 69 6d 69 74 73 2e 68 3e 0a 23 20 65 6c 73 65 limits.h>.# else
c6260 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 .# include <sys
c6270 2f 66 69 6c 65 2e 68 3e 0a 23 20 20 69 6e 63 6c /file.h>.# incl
c6280 75 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68 ude <sys/param.h
c6290 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 >.# include <sy
c62a0 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 20 65 6e 64 s/mount.h>.# end
c62b0 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c if.#endif /* SQL
c62c0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
c62d0 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a NG_STYLE */../*.
c62e0 2a 2a 20 49 66 20 77 65 20 61 72 65 20 74 6f 20 ** If we are to
c62f0 62 65 20 74 68 72 65 61 64 2d 73 61 66 65 2c 20 be thread-safe,
c6300 69 6e 63 6c 75 64 65 20 74 68 65 20 70 74 68 72 include the pthr
c6310 65 61 64 73 20 68 65 61 64 65 72 20 61 6e 64 20 eads header and
c6320 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51 define.** the SQ
c6330 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 LITE_UNIX_THREAD
c6340 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20 S macro..*/.#if
c6350 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
c6360 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 E.# define SQLIT
c6370 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 53 20 31 E_UNIX_THREADS 1
c6380 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 .#endif../*.** D
c6390 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69 6f efault permissio
c63a0 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 ns when creating
c63b0 20 61 20 6e 65 77 20 66 69 6c 65 0a 2a 2f 0a 23 a new file.*/.#
c63c0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 ifndef SQLITE_DE
c63d0 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 FAULT_FILE_PERMI
c63e0 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 SSIONS.# define
c63f0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 SQLITE_DEFAULT_F
c6400 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 20 ILE_PERMISSIONS
c6410 30 36 34 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 0644.#endif../*.
c6420 20 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72 6d ** Default perm
c6430 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 65 issions when cre
c6440 61 74 69 6e 67 20 61 75 74 6f 20 70 72 6f 78 79 ating auto proxy
c6450 20 64 69 72 0a 20 2a 2f 0a 23 69 66 6e 64 65 66 dir. */.#ifndef
c6460 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
c6470 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 PROXYDIR_PERMISS
c6480 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 53 51 IONS.# define SQ
c6490 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f LITE_DEFAULT_PRO
c64a0 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e XYDIR_PERMISSION
c64b0 53 20 30 37 35 35 0a 23 65 6e 64 69 66 0a 0a 2f S 0755.#endif../
c64c0 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 75 70 *.** Maximum sup
c64d0 70 6f 72 74 65 64 20 70 61 74 68 2d 6c 65 6e 67 ported path-leng
c64e0 74 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d th..*/.#define M
c64f0 41 58 5f 50 41 54 48 4e 41 4d 45 20 35 31 32 0a AX_PATHNAME 512.
c6500 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 73 65 74 20 ./*.** Only set
c6510 74 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 69 66 the lastErrno if
c6520 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 the error code
c6530 69 73 20 61 20 72 65 61 6c 20 65 72 72 6f 72 20 is a real error
c6540 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20 61 20 6e 6f and not .** a no
c6550 72 6d 61 6c 20 65 78 70 65 63 74 65 64 20 72 65 rmal expected re
c6560 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c turn code of SQL
c6570 49 54 45 5f 42 55 53 59 20 6f 72 20 53 51 4c 49 ITE_BUSY or SQLI
c6580 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 66 69 6e 65 TE_OK.*/.#define
c6590 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 78 IS_LOCK_ERROR(x
c65a0 29 20 20 28 28 78 20 21 3d 20 53 51 4c 49 54 45 ) ((x != SQLITE
c65b0 5f 4f 4b 29 20 26 26 20 28 78 20 21 3d 20 53 51 _OK) && (x != SQ
c65c0 4c 49 54 45 5f 42 55 53 59 29 29 0a 0a 0a 2f 2a LITE_BUSY)).../*
c65d0 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 2c 20 61 .** Sometimes, a
c65e0 66 74 65 72 20 61 20 66 69 6c 65 20 68 61 6e 64 fter a file hand
c65f0 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 62 79 20 le is closed by
c6600 53 51 4c 69 74 65 2c 20 74 68 65 20 66 69 6c 65 SQLite, the file
c6610 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20 63 descriptor.** c
c6620 61 6e 6e 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 annot be closed
c6630 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e 20 immediately. In
c6640 74 68 65 73 65 20 63 61 73 65 73 2c 20 69 6e 73 these cases, ins
c6650 74 61 6e 63 65 73 20 6f 66 20 74 68 65 20 66 6f tances of the fo
c6660 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 llowing.** struc
c6670 74 75 72 65 20 61 72 65 20 75 73 65 64 20 74 6f ture are used to
c6680 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c 65 20 store the file
c6690 64 65 73 63 72 69 70 74 6f 72 20 77 68 69 6c 65 descriptor while
c66a0 20 77 61 69 74 69 6e 67 20 66 6f 72 20 61 6e 0a waiting for an.
c66b0 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 ** opportunity t
c66c0 6f 20 65 69 74 68 65 72 20 63 6c 6f 73 65 20 6f o either close o
c66d0 72 20 72 65 75 73 65 20 69 74 2e 0a 2a 2f 0a 74 r reuse it..*/.t
c66e0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 55 6e ypedef struct Un
c66f0 69 78 55 6e 75 73 65 64 46 64 20 55 6e 69 78 55 ixUnusedFd UnixU
c6700 6e 75 73 65 64 46 64 3b 0a 73 74 72 75 63 74 20 nusedFd;.struct
c6710 55 6e 69 78 55 6e 75 73 65 64 46 64 20 7b 0a 20 UnixUnusedFd {.
c6720 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 int fd;
c6730 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
c6740 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f le descriptor to
c6750 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 69 6e 74 20 close */. int
c6760 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
c6770 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 /* Flags t
c6780 68 69 73 20 66 69 6c 65 20 64 65 73 63 72 69 70 his file descrip
c6790 74 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 77 tor was opened w
c67a0 69 74 68 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 ith */. UnixUnu
c67b0 73 65 64 46 64 20 2a 70 4e 65 78 74 3b 20 20 20 sedFd *pNext;
c67c0 20 20 20 2f 2a 20 4e 65 78 74 20 75 6e 75 73 65 /* Next unuse
c67d0 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f d file descripto
c67e0 72 20 6f 6e 20 73 61 6d 65 20 66 69 6c 65 20 2a r on same file *
c67f0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 /.};../*.** The
c6800 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 unixFile structu
c6810 72 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 6f re is subclass o
c6820 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 f sqlite3_file s
c6830 70 65 63 69 66 69 63 20 74 6f 20 74 68 65 20 75 pecific to the u
c6840 6e 69 78 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 nix.** VFS imple
c6850 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 mentations..*/.t
c6860 79 70 65 64 65 66 20 73 74 72 75 63 74 20 75 6e ypedef struct un
c6870 69 78 46 69 6c 65 20 75 6e 69 78 46 69 6c 65 3b ixFile unixFile;
c6880 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 .struct unixFile
c6890 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f {. sqlite3_io_
c68a0 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 methods const *p
c68b0 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 Method; /* Alwa
c68c0 79 73 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 ys the first ent
c68d0 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 ry */. struct u
c68e0 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 nixOpenCnt *pOpe
c68f0 6e 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f n; /* Info
c6900 20 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65 6e 20 about all open
c6910 66 64 27 73 20 6f 6e 20 74 68 69 73 20 69 6e 6f fd's on this ino
c6920 64 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 de */. struct u
c6930 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f nixLockInfo *pLo
c6940 63 6b 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f ck; /* Info
c6950 20 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e 20 about locks on
c6960 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 this inode */.
c6970 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 int h;
c6980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c6990 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 /* The file des
c69a0 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 criptor */. int
c69b0 20 64 69 72 66 64 3b 20 20 20 20 20 20 20 20 20 dirfd;
c69c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c69d0 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 File descriptor
c69e0 20 66 6f 72 20 74 68 65 20 64 69 72 65 63 74 6f for the directo
c69f0 72 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 ry */. unsigned
c6a00 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20 char locktype;
c6a10 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
c6a20 74 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68 65 6c type of lock hel
c6a30 64 20 6f 6e 20 74 68 69 73 20 66 64 20 2a 2f 0a d on this fd */.
c6a40 20 20 69 6e 74 20 6c 61 73 74 45 72 72 6e 6f 3b int lastErrno;
c6a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c6a60 20 20 20 2f 2a 20 54 68 65 20 75 6e 69 78 20 65 /* The unix e
c6a70 72 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c 61 rrno from the la
c6a80 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a st I/O error */.
c6a90 20 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 6e 67 43 void *lockingC
c6aa0 6f 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20 20 ontext;
c6ab0 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73 74 /* Locking st
c6ac0 79 6c 65 20 73 70 65 63 69 66 69 63 20 73 74 61 yle specific sta
c6ad0 74 65 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73 te */. UnixUnus
c6ae0 65 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20 20 edFd *pUnused;
c6af0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 2d /* Pre-
c6b00 61 6c 6c 6f 63 61 74 65 64 20 55 6e 69 78 55 6e allocated UnixUn
c6b10 75 73 65 64 46 64 20 2a 2f 0a 20 20 69 6e 74 20 usedFd */. int
c6b20 66 69 6c 65 46 6c 61 67 73 3b 20 20 20 20 20 20 fileFlags;
c6b30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c6b40 4d 69 73 63 65 6c 6c 61 6e 6f 75 73 20 66 6c 61 Miscellanous fla
c6b50 67 73 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 gs */.#if SQLITE
c6b60 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
c6b70 53 54 59 4c 45 0a 20 20 69 6e 74 20 6f 70 65 6e STYLE. int open
c6b80 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 Flags;
c6b90 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
c6ba0 66 6c 61 67 73 20 73 70 65 63 69 66 69 65 64 20 flags specified
c6bb0 61 74 20 6f 70 65 6e 28 29 20 2a 2f 0a 23 65 6e at open() */.#en
c6bc0 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 dif.#if SQLITE_T
c6bd0 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 HREADSAFE && def
c6be0 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a ined(__linux__).
c6bf0 20 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 3b pthread_t tid;
c6c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c6c10 20 20 20 2f 2a 20 54 68 65 20 74 68 72 65 61 64 /* The thread
c6c20 20 74 68 61 74 20 22 6f 77 6e 73 22 20 74 68 69 that "owns" thi
c6c30 73 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 23 65 s unixFile */.#e
c6c40 6e 64 69 66 0a 23 69 66 20 4f 53 5f 56 58 57 4f ndif.#if OS_VXWO
c6c50 52 4b 53 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 RKS. int isDele
c6c60 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 te;
c6c70 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 /* Delete
c6c80 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 72 75 on close if tru
c6c90 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 78 e */. struct vx
c6ca0 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 worksFileId *pId
c6cb0 3b 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 71 75 ; /* Uniqu
c6cc0 65 20 66 69 6c 65 20 49 44 20 2a 2f 0a 23 65 6e e file ID */.#en
c6cd0 64 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 dif.#ifndef NDEB
c6ce0 55 47 0a 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 UG. /* The next
c6cf0 20 67 72 6f 75 70 20 6f 66 20 76 61 72 69 61 62 group of variab
c6d00 6c 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 les are used to
c6d10 74 72 61 63 6b 20 77 68 65 74 68 65 72 20 6f 72 track whether or
c6d20 20 6e 6f 74 20 74 68 65 0a 20 20 2a 2a 20 74 72 not the. ** tr
c6d30 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 ansaction counte
c6d40 72 20 69 6e 20 62 79 74 65 73 20 32 34 2d 32 37 r in bytes 24-27
c6d50 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c of database fil
c6d60 65 73 20 61 72 65 20 75 70 64 61 74 65 64 0a 20 es are updated.
c6d70 20 2a 2a 20 77 68 65 6e 65 76 65 72 20 61 6e 79 ** whenever any
c6d80 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 part of the dat
c6d90 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20 20 abase changes.
c6da0 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 An assertion fau
c6db0 6c 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 6f 63 63 lt will. ** occ
c6dc0 75 72 20 69 66 20 61 20 66 69 6c 65 20 69 73 20 ur if a file is
c6dd0 75 70 64 61 74 65 64 20 77 69 74 68 6f 75 74 20 updated without
c6de0 61 6c 73 6f 20 75 70 64 61 74 69 6e 67 20 74 68 also updating th
c6df0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 e transaction.
c6e00 2a 2a 20 63 6f 75 6e 74 65 72 2e 20 20 54 68 69 ** counter. Thi
c6e10 73 20 74 65 73 74 20 69 73 20 6d 61 64 65 20 74 s test is made t
c6e20 6f 20 61 76 6f 69 64 20 6e 65 77 20 70 72 6f 62 o avoid new prob
c6e30 6c 65 6d 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 lems similar to
c6e40 74 68 65 0a 20 20 2a 2a 20 6f 6e 65 20 64 65 73 the. ** one des
c6e50 63 72 69 62 65 64 20 62 79 20 74 69 63 6b 65 74 cribed by ticket
c6e60 20 23 33 35 38 34 2e 20 0a 20 20 2a 2f 0a 20 20 #3584. . */.
c6e70 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 72 unsigned char tr
c6e80 61 6e 73 43 6e 74 72 43 68 6e 67 3b 20 20 20 2f ansCntrChng; /
c6e90 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 74 72 * True if the tr
c6ea0 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 ansaction counte
c6eb0 72 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 r changed */. u
c6ec0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64 62 55 nsigned char dbU
c6ed0 70 64 61 74 65 3b 20 20 20 20 20 20 20 20 2f 2a pdate; /*
c6ee0 20 54 72 75 65 20 69 66 20 61 6e 79 20 70 61 72 True if any par
c6ef0 74 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 t of database fi
c6f00 6c 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 le changed */.
c6f10 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 6e unsigned char in
c6f20 4e 6f 72 6d 61 6c 57 72 69 74 65 3b 20 20 20 2f NormalWrite; /
c6f30 2a 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 6e * True if in a n
c6f40 6f 72 6d 61 6c 20 77 72 69 74 65 20 6f 70 65 72 ormal write oper
c6f50 61 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a ation */.#endif.
c6f60 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
c6f70 53 54 0a 20 20 2f 2a 20 49 6e 20 74 65 73 74 20 ST. /* In test
c6f80 6d 6f 64 65 2c 20 69 6e 63 72 65 61 73 65 20 74 mode, increase t
c6f90 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 20 he size of this
c6fa0 73 74 72 75 63 74 75 72 65 20 61 20 62 69 74 20 structure a bit
c6fb0 73 6f 20 74 68 61 74 20 0a 20 20 2a 2a 20 69 74 so that . ** it
c6fc0 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 is larger than
c6fd0 74 68 65 20 73 74 72 75 63 74 20 43 72 61 73 68 the struct Crash
c6fe0 46 69 6c 65 20 64 65 66 69 6e 65 64 20 69 6e 20 File defined in
c6ff0 74 65 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 20 test6.c.. */.
c7000 63 68 61 72 20 61 50 61 64 64 69 6e 67 5b 33 32 char aPadding[32
c7010 5d 3b 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a ];.#endif.};../*
c7020 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
c7030 67 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 g macros define
c7040 62 69 74 73 20 69 6e 20 75 6e 69 78 46 69 6c 65 bits in unixFile
c7050 2e 66 69 6c 65 46 6c 61 67 73 0a 2a 2f 0a 23 64 .fileFlags.*/.#d
c7060 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 48 4f efine SQLITE_WHO
c7070 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 LE_FILE_LOCKING
c7080 20 30 78 30 30 30 31 20 20 20 2f 2a 20 55 73 65 0x0001 /* Use
c7090 20 77 68 6f 6c 65 2d 66 69 6c 65 20 6c 6f 63 6b whole-file lock
c70a0 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e ing */../*.** In
c70b0 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20 clude code that
c70c0 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c is common to all
c70d0 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f os_*.c files.*/
c70e0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
c70f0 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d Include os_comm
c7100 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 on.h in the midd
c7110 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20 le of os_unix.c
c7120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
c7130 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
c7140 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 Begin file os_c
c7150 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a ommon.h ********
c7160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
c7180 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 ./*.** 2004 May
c7190 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 22.**.** The aut
c71a0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
c71b0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
c71c0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
c71d0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
c71e0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
c71f0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
c7200 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
c7210 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
c7220 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
c7230 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
c7240 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
c7250 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
c7260 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
c7270 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
c7280 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
c7290 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
c72a0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
c72b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c72c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c72d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c72e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c72f0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
c7300 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
c7310 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 macros and a lit
c7320 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20 tle bit of code
c7330 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 that is common t
c7340 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 o.** all of the
c7350 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 platform-specifi
c7360 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 c files (os_*.c)
c7370 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65 and is #include
c7380 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 d into those.**
c7390 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 files..**.** Thi
c73a0 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 s file should be
c73b0 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68 #included by th
c73c0 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f e os_*.c files o
c73d0 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 nly. It is not
c73e0 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 a.** general pur
c73f0 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65 pose header file
c7400 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f ..**.** $Id: os_
c7410 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20 common.h,v 1.38
c7420 32 30 30 39 2f 30 32 2f 32 34 20 31 38 3a 34 30 2009/02/24 18:40
c7430 3a 35 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :50 danielk1977
c7440 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 Exp $.*/.#ifndef
c7450 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 _OS_COMMON_H_.#
c7460 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f define _OS_COMMO
c7470 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c N_H_../*.** At l
c7480 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61 east two bugs ha
c7490 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65 ve slipped in be
c74a0 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64 cause we changed
c74b0 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 the MEMORY_DEBU
c74c0 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 G.** macro to SQ
c74d0 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73 LITE_DEBUG and s
c74e0 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 ome older makefi
c74f0 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74 les have not yet
c7500 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 made the.** swi
c7510 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 tch. The follow
c7520 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 ing code should
c7530 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c catch this probl
c7540 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 em at compile-ti
c7550 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 me..*/.#ifdef ME
c7560 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 72 MORY_DEBUG.# err
c7570 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 or "The MEMORY_D
c7580 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 EBUG macro is ob
c7590 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 4c solete. Use SQL
c75a0 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 61 ITE_DEBUG instea
c75b0 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 d.".#endif..#ifd
c75c0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
c75d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
c75e0 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 nt sqlite3OSTrac
c75f0 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f e = 0;.#define O
c7600 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20 20 STRACE1(X)
c7610 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 if( sqlite3OS
c7620 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 Trace ) sqlite3D
c7630 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 64 ebugPrintf(X).#d
c7640 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 efine OSTRACE2(X
c7650 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73 71 ,Y) if( sq
c7660 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 lite3OSTrace ) s
c7670 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
c7680 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f f(X,Y).#define O
c7690 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 STRACE3(X,Y,Z)
c76a0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 if( sqlite3OS
c76b0 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 Trace ) sqlite3D
c76c0 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a ebugPrintf(X,Y,Z
c76d0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
c76e0 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 E4(X,Y,Z,A) if
c76f0 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
c7700 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
c7710 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 rintf(X,Y,Z,A).#
c7720 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 define OSTRACE5(
c7730 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 X,Y,Z,A,B) if( s
c7740 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
c7750 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
c7760 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 tf(X,Y,Z,A,B).#d
c7770 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 efine OSTRACE6(X
c7780 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 ,Y,Z,A,B,C) \.
c7790 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 if(sqlite3OSTr
c77a0 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 ace) sqlite3Debu
c77b0 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c gPrintf(X,Y,Z,A,
c77c0 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 B,C).#define OST
c77d0 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c RACE7(X,Y,Z,A,B,
c77e0 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 71 C,D) \. if(sq
c77f0 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 lite3OSTrace) sq
c7800 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
c7810 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a (X,Y,Z,A,B,C,D).
c7820 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 #else.#define OS
c7830 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69 6e TRACE1(X).#defin
c7840 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 0a e OSTRACE2(X,Y).
c7850 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 #define OSTRACE3
c7860 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 (X,Y,Z).#define
c7870 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 OSTRACE4(X,Y,Z,A
c7880 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
c7890 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 E5(X,Y,Z,A,B).#d
c78a0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 efine OSTRACE6(X
c78b0 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 ,Y,Z,A,B,C).#def
c78c0 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 ine OSTRACE7(X,Y
c78d0 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 ,Z,A,B,C,D).#end
c78e0 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 if../*.** Macros
c78f0 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 for performance
c7900 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 tracing. Norma
c7910 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 lly turned off.
c7920 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f Only works.** o
c7930 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65 2e n i486 hardware.
c7940 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
c7950 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 E_PERFORMANCE_TR
c7960 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 ACE../* .** hwti
c7970 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e me.h contains in
c7980 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63 line assembler c
c7990 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e ode for implemen
c79a0 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 ting .** high-pe
c79b0 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 rformance timing
c79c0 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a routines..*/./*
c79d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
c79e0 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69 clude hwtime.h i
c79f0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
c7a00 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a os_common.h ****
c7a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
c7a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
c7a30 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e gin file hwtime.
c7a40 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
c7a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
c7a70 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a .** 2008 May 27.
c7a80 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
c7a90 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
c7aa0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
c7ab0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
c7ac0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
c7ad0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
c7ae0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
c7af0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
c7b00 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
c7b10 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
c7b20 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
c7b30 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
c7b40 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
c7b50 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
c7b60 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
c7b70 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
c7b80 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
c7b90 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
c7ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7be0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
c7bf0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c ile contains inl
c7c00 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 ine asm code for
c7c10 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 67 retrieving "hig
c7c20 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a h-performance".*
c7c30 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 * counters for x
c7c40 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 86 class CPUs..*
c7c50 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 *.** $Id: hwtime
c7c60 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 38 .h,v 1.3 2008/08
c7c70 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 61 /01 14:33:15 sha
c7c80 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e ne Exp $.*/.#ifn
c7c90 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 def _HWTIME_H_.#
c7ca0 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 define _HWTIME_H
c7cb0 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c _../*.** The fol
c7cc0 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f lowing routine o
c7cd0 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e nly works on pen
c7ce0 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e tium-class (or n
c7cf0 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 73 ewer) processors
c7d00 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 65 ..** It uses the
c7d10 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 6f RDTSC opcode to
c7d20 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 20 read the cycle
c7d30 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 count value out
c7d40 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 of the.** proces
c7d50 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20 sor and returns
c7d60 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 69 that value. Thi
c7d70 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f s can be used fo
c7d80 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 r high-res.** pr
c7d90 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 ofiling..*/.#if
c7da0 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f (defined(__GNUC_
c7db0 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d _) || defined(_M
c7dc0 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 20 SC_VER)) && \.
c7dd0 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 38 (defined(i38
c7de0 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 6) || defined(__
c7df0 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e i386__) || defin
c7e00 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 ed(_M_IX86))..
c7e10 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e #if defined(__GN
c7e20 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e UC__).. __inlin
c7e30 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 e__ sqlite_uint6
c7e40 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 4 sqlite3Hwtime(
c7e50 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 void){. unsi
c7e60 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b gned int lo, hi;
c7e70 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f . __asm__ __
c7e80 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 volatile__ ("rdt
c7e90 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c sc" : "=a" (lo),
c7ea0 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 20 "=d" (hi));.
c7eb0 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 return (sqlite
c7ec0 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 _uint64)hi << 32
c7ed0 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 | lo;. }.. #e
c7ee0 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 lif defined(_MSC
c7ef0 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 _VER).. __decls
c7f00 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c pec(naked) __inl
c7f10 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 ine sqlite_uint6
c7f20 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 4 __cdecl sqlite
c7f30 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 3Hwtime(void){.
c7f40 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 __asm {.
c7f50 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 20 rdtsc.
c7f60 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 65 ret ; re
c7f70 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 44 turn value at ED
c7f80 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d X:EAX. }. }
c7f90 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 .. #endif..#eli
c7fa0 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 f (defined(__GNU
c7fb0 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 C__) && defined(
c7fc0 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 __x86_64__))..
c7fd0 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 __inline__ sqlit
c7fe0 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e_uint64 sqlite3
c7ff0 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 Hwtime(void){.
c8000 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e unsigned lon
c8010 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 g val;. __a
c8020 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f sm__ __volatile_
c8030 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41 _ ("rdtsc" : "=A
c8040 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 " (val));.
c8050 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a return val;. }.
c8060 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 .#elif (defined
c8070 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 (__GNUC__) && de
c8080 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a fined(__ppc__)).
c8090 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 . __inline__ sq
c80a0 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 lite_uint64 sqli
c80b0 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
c80c0 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 . unsigned
c80d0 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c long long retval
c80e0 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 ;. unsigned
c80f0 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 long junk;.
c8100 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 __asm__ __vola
c8110 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 tile__ ("\n\.
c8120 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 6d 1: m
c8130 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 ftbu %1\n\.
c8140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d m
c8150 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 ftb %L0\n\.
c8160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c8170 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 mftbu %0\n\.
c8180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c8190 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c cmpw %0,%1\n\
c81a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
c81b0 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a 20 bne 1b".
c81c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c81d0 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29 : "=r" (retval)
c81e0 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a , "=r" (junk));.
c81f0 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 74 return ret
c8200 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a val;. }..#else.
c8210 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 69 . #error Need i
c8220 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
c8230 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 sqlite3Hwtime()
c8240 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f for your platfo
c8250 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 rm... /*. ** T
c8260 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 o compile withou
c8270 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 t implementing s
c8280 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 qlite3Hwtime() f
c8290 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d or your platform
c82a0 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 ,. ** you can r
c82b0 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 20 emove the above
c82c0 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 74 #error and use t
c82d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a he following. *
c82e0 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e * stub function.
c82f0 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 You will lose
c8300 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 66 timing support f
c8310 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 or many. ** of
c8320 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 6e the debugging an
c8330 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 74 d testing utilit
c8340 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f 75 ies, but it shou
c8350 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 ld at. ** least
c8360 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e compile and run
c8370 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 .. */.SQLITE_PR
c8380 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75 IVATE sqlite_u
c8390 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
c83a0 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 ime(void){ retur
c83b0 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 n ((sqlite_uint6
c83c0 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 4)0); }..#endif.
c83d0 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 .#endif /* !defi
c83e0 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 ned(_HWTIME_H_)
c83f0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
c8400 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 6d *** End of hwtim
c8410 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
c8420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8440 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
c8450 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
c8460 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
c8470 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 in os_common.h
c8480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8490 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 **/..static sqli
c84a0 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 72 te_uint64 g_star
c84b0 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 t;.static sqlite
c84c0 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 _uint64 g_elapse
c84d0 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 d;.#define TIMER
c84e0 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f 73 _START g_s
c84f0 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74 69 tart=sqlite3Hwti
c8500 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49 4d me().#define TIM
c8510 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20 67 ER_END g
c8520 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 33 _elapsed=sqlite3
c8530 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 Hwtime()-g_start
c8540 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 .#define TIMER_E
c8550 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c 61 LAPSED g_ela
c8560 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 psed.#else.#defi
c8570 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 ne TIMER_START.#
c8580 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 define TIMER_END
c8590 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 .#define TIMER_E
c85a0 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 71 6c LAPSED ((sql
c85b0 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 ite_uint64)0).#e
c85c0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 ndif../*.** If w
c85d0 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 e compile with t
c85e0 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6d he SQLITE_TEST m
c85f0 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 acro set, then t
c8600 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f he following blo
c8610 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 ck.** of code wi
c8620 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 20 61 ll give us the a
c8630 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 bility to simula
c8640 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 te a disk I/O er
c8650 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 ror. This.** is
c8660 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e used for testin
c8670 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 g the I/O recove
c8680 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 ry logic..*/.#if
c8690 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
c86a0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
c86b0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
c86c0 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 hit = 0;
c86d0 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d /* Total num
c86e0 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 ber of I/O Error
c86f0 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 s */.SQLITE_API
c8700 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
c8710 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 30 rror_hardhit = 0
c8720 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ; /* Numb
c8730 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e er of non-benign
c8740 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 errors */.SQLIT
c8750 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
c8760 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 3_io_error_pendi
c8770 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f ng = 0; /
c8780 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 * Count down to
c8790 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 first I/O error
c87a0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
c87b0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 t sqlite3_io_err
c87c0 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 20 or_persist = 0;
c87d0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
c87e0 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 f I/O errors per
c87f0 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 sist */.SQLITE_A
c8800 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
c8810 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d o_error_benign =
c8820 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 0; /* T
c8830 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 61 72 rue if errors ar
c8840 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 e benign */.SQLI
c8850 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
c8860 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 e3_diskfull_pend
c8870 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f ing = 0;.SQLITE_
c8880 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
c8890 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 diskfull = 0;.#d
c88a0 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f efine SimulateIO
c88b0 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 ErrorBenign(X) s
c88c0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
c88d0 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 benign=(X).#defi
c88e0 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 ne SimulateIOErr
c88f0 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 or(CODE) \. if
c8900 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 ( (sqlite3_io_er
c8910 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20 73 ror_persist && s
c8920 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
c8930 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c hit) \. ||
c8940 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
c8950 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 r_pending-- == 1
c8960 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20 ) \.
c8970 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 { local_ioer
c8980 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 r(); CODE; }.sta
c8990 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 tic void local_i
c89a0 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 oerr(){. IOTRAC
c89b0 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a E(("IOERR\n"));.
c89c0 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 sqlite3_io_err
c89d0 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 or_hit++;. if(
c89e0 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f !sqlite3_io_erro
c89f0 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 r_benign ) sqlit
c8a00 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 e3_io_error_hard
c8a10 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 hit++;.}.#define
c8a20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c SimulateDiskful
c8a30 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 lError(CODE) \.
c8a40 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 if( sqlite3_di
c8a50 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 skfull_pending )
c8a60 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71 6c { \. if( sql
c8a70 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 ite3_diskfull_pe
c8a80 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a nding == 1 ){ \.
c8a90 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 local_ioe
c8aa0 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 rr(); \. s
c8ab0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 qlite3_diskfull
c8ac0 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 = 1; \. sq
c8ad0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 lite3_io_error_h
c8ae0 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 it = 1; \.
c8af0 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 CODE; \. }e
c8b00 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 lse{ \. sq
c8b10 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 lite3_diskfull_p
c8b20 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 ending--; \.
c8b30 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a } \. }.#else.
c8b40 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
c8b50 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 IOErrorBenign(X)
c8b60 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
c8b70 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 66 eIOError(A).#def
c8b80 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b ine SimulateDisk
c8b90 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e fullError(A).#en
c8ba0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 dif../*.** When
c8bb0 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 testing, keep a
c8bc0 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d count of the num
c8bd0 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 ber of open file
c8be0 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c s..*/.#ifdef SQL
c8bf0 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
c8c00 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
c8c10 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 open_file_count
c8c20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 = 0;.#define Ope
c8c30 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 6c nCounter(X) sql
c8c40 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 ite3_open_file_c
c8c50 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a ount+=(X).#else.
c8c60 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e #define OpenCoun
c8c70 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 ter(X).#endif..#
c8c80 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 endif /* !define
c8c90 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 d(_OS_COMMON_H_)
c8ca0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
c8cb0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 **** End of os_c
c8cc0 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a ommon.h ********
c8cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8cf0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
c8d00 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
c8d10 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
c8d20 66 20 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 20 2a f in os_unix.c *
c8d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8d40 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 ***/../*.** Defi
c8d50 6e 65 20 76 61 72 69 6f 75 73 20 6d 61 63 72 6f ne various macro
c8d60 73 20 74 68 61 74 20 61 72 65 20 6d 69 73 73 69 s that are missi
c8d70 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 20 73 79 73 ng from some sys
c8d80 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 tems..*/.#ifndef
c8d90 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 O_LARGEFILE.# d
c8da0 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c efine O_LARGEFIL
c8db0 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 E 0.#endif.#ifde
c8dc0 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 f SQLITE_DISABLE
c8dd0 5f 4c 46 53 0a 23 20 75 6e 64 65 66 20 4f 5f 4c _LFS.# undef O_L
c8de0 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66 69 6e ARGEFILE.# defin
c8df0 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30 0a e O_LARGEFILE 0.
c8e00 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f #endif.#ifndef O
c8e10 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 66 69 _NOFOLLOW.# defi
c8e20 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30 0a ne O_NOFOLLOW 0.
c8e30 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f #endif.#ifndef O
c8e40 5f 42 49 4e 41 52 59 0a 23 20 64 65 66 69 6e 65 _BINARY.# define
c8e50 20 4f 5f 42 49 4e 41 52 59 20 30 0a 23 65 6e 64 O_BINARY 0.#end
c8e60 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44 4a if../*.** The DJ
c8e70 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e 76 GPP compiler env
c8e80 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20 6d ironment looks m
c8e90 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78 2c ostly like Unix,
c8ea0 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61 63 6b 73 but it.** lacks
c8eb0 20 74 68 65 20 66 63 6e 74 6c 28 29 20 73 79 73 the fcntl() sys
c8ec0 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72 65 tem call. So re
c8ed0 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 29 20 74 define fcntl() t
c8ee0 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a 2a o be something.*
c8ef0 2a 20 74 68 61 74 20 61 6c 77 61 79 73 20 73 75 * that always su
c8f00 63 63 65 65 64 73 2e 20 20 54 68 69 73 20 6d 65 cceeds. This me
c8f10 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69 6e 67 ans that locking
c8f20 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 20 does not occur
c8f30 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 50 2e 20 under.** DJGPP.
c8f40 20 42 75 74 20 69 74 20 69 73 20 44 4f 53 20 2d But it is DOS -
c8f50 20 77 68 61 74 20 64 69 64 20 79 6f 75 20 65 78 what did you ex
c8f60 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 66 20 pect?.*/.#ifdef
c8f70 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 65 66 69 __DJGPP__.# defi
c8f80 6e 65 20 66 63 6e 74 6c 28 41 2c 42 2c 43 29 20 ne fcntl(A,B,C)
c8f90 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
c8fa0 54 68 65 20 74 68 72 65 61 64 69 64 20 6d 61 63 The threadid mac
c8fb0 72 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 74 ro resolves to t
c8fc0 68 65 20 74 68 72 65 61 64 2d 69 64 20 6f 72 20 he thread-id or
c8fd0 74 6f 20 30 2e 20 20 55 73 65 64 20 66 6f 72 0a to 0. Used for.
c8fe0 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 ** testing and d
c8ff0 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a ebugging only..*
c9000 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 /.#if SQLITE_THR
c9010 45 41 44 53 41 46 45 0a 23 64 65 66 69 6e 65 20 EADSAFE.#define
c9020 74 68 72 65 61 64 69 64 20 70 74 68 72 65 61 64 threadid pthread
c9030 5f 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23 64 _self().#else.#d
c9040 65 66 69 6e 65 20 74 68 72 65 61 64 69 64 20 30 efine threadid 0
c9050 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
c9060 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 73 Helper functions
c9070 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 to obtain and r
c9080 65 6c 69 6e 71 75 69 73 68 20 74 68 65 20 67 6c elinquish the gl
c9090 6f 62 61 6c 20 6d 75 74 65 78 2e 20 54 68 65 0a obal mutex. The.
c90a0 2a 2a 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 ** global mutex
c90b0 69 73 20 75 73 65 64 20 74 6f 20 70 72 6f 74 65 is used to prote
c90c0 63 74 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 ct the unixOpenC
c90d0 6e 74 2c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f nt, unixLockInfo
c90e0 20 61 6e 64 0a 2a 2a 20 76 78 77 6f 72 6b 73 46 and.** vxworksF
c90f0 69 6c 65 49 64 20 6f 62 6a 65 63 74 73 20 75 73 ileId objects us
c9100 65 64 20 62 79 20 74 68 69 73 20 66 69 6c 65 2c ed by this file,
c9110 20 61 6c 6c 20 6f 66 20 77 68 69 63 68 20 6d 61 all of which ma
c9120 79 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 64 20 y be .** shared
c9130 62 79 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 by multiple thre
c9140 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 ads..**.** Funct
c9150 69 6f 6e 20 75 6e 69 78 4d 75 74 65 78 48 65 6c ion unixMutexHel
c9160 64 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61 d() is used to a
c9170 73 73 65 72 74 28 29 20 74 68 61 74 20 74 68 65 ssert() that the
c9180 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 0a 2a global mutex .*
c9190 2a 20 69 73 20 68 65 6c 64 20 77 68 65 6e 20 72 * is held when r
c91a0 65 71 75 69 72 65 64 2e 20 54 68 69 73 20 66 75 equired. This fu
c91b0 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 nction is only u
c91c0 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 sed as part of a
c91d0 73 73 65 72 74 28 29 20 0a 2a 2a 20 73 74 61 74 ssert() .** stat
c91e0 65 6d 65 6e 74 73 2e 20 65 2e 67 2e 0a 2a 2a 0a ements. e.g..**.
c91f0 2a 2a 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 ** unixEnterMu
c9200 74 65 78 28 29 0a 2a 2a 20 20 20 20 20 61 73 73 tex().** ass
c9210 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 65 ert( unixMutexHe
c9220 6c 64 28 29 20 29 3b 0a 2a 2a 20 20 20 75 6e 69 ld() );.** uni
c9230 78 45 6e 74 65 72 4c 65 61 76 65 28 29 0a 2a 2f xEnterLeave().*/
c9240 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 .static void uni
c9250 78 45 6e 74 65 72 4d 75 74 65 78 28 76 6f 69 64 xEnterMutex(void
c9260 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 ){. sqlite3_mut
c9270 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 ex_enter(sqlite3
c9280 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
c9290 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
c92a0 41 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61 74 69 ASTER));.}.stati
c92b0 63 20 76 6f 69 64 20 75 6e 69 78 4c 65 61 76 65 c void unixLeave
c92c0 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 Mutex(void){. s
c92d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
c92e0 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 ve(sqlite3MutexA
c92f0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
c9300 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
c9310 29 3b 0a 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 );.}.#ifdef SQLI
c9320 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20 TE_DEBUG.static
c9330 69 6e 74 20 75 6e 69 78 4d 75 74 65 78 48 65 6c int unixMutexHel
c9340 64 28 76 6f 69 64 29 20 7b 0a 20 20 72 65 74 75 d(void) {. retu
c9350 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 rn sqlite3_mutex
c9360 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d 75 74 _held(sqlite3Mut
c9370 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
c9380 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
c9390 45 52 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a ER));.}.#endif..
c93a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
c93b0 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 EBUG./*.** Helpe
c93c0 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 70 r function for p
c93d0 72 69 6e 74 69 6e 67 20 6f 75 74 20 74 72 61 63 rinting out trac
c93e0 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 e information fr
c93f0 6f 6d 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 om debugging.**
c9400 62 69 6e 61 72 69 65 73 2e 20 54 68 69 73 20 72 binaries. This r
c9410 65 74 75 72 6e 73 20 74 68 65 20 73 74 72 69 6e eturns the strin
c9420 67 20 72 65 70 72 65 73 65 74 61 74 69 6f 6e 20 g represetation
c9430 6f 66 20 74 68 65 20 73 75 70 70 6c 69 65 64 0a of the supplied.
c9440 2a 2a 20 69 6e 74 65 67 65 72 20 6c 6f 63 6b 2d ** integer lock-
c9450 74 79 70 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 type..*/.static
c9460 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 6f 63 6b const char *lock
c9470 74 79 70 65 4e 61 6d 65 28 69 6e 74 20 6c 6f 63 typeName(int loc
c9480 6b 74 79 70 65 29 7b 0a 20 20 73 77 69 74 63 68 ktype){. switch
c9490 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 ( locktype ){.
c94a0 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b 3a 20 case NO_LOCK:
c94b0 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b 0a 20 return "NONE";.
c94c0 20 20 20 63 61 73 65 20 53 48 41 52 45 44 5f 4c case SHARED_L
c94d0 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 41 OCK: return "SHA
c94e0 52 45 44 22 3b 0a 20 20 20 20 63 61 73 65 20 52 RED";. case R
c94f0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 ESERVED_LOCK: re
c9500 74 75 72 6e 20 22 52 45 53 45 52 56 45 44 22 3b turn "RESERVED";
c9510 0a 20 20 20 20 63 61 73 65 20 50 45 4e 44 49 4e . case PENDIN
c9520 47 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 G_LOCK: return "
c9530 50 45 4e 44 49 4e 47 22 3b 0a 20 20 20 20 63 61 PENDING";. ca
c9540 73 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 se EXCLUSIVE_LOC
c9550 4b 3a 20 72 65 74 75 72 6e 20 22 45 58 43 4c 55 K: return "EXCLU
c9560 53 49 56 45 22 3b 0a 20 20 7d 0a 20 20 72 65 74 SIVE";. }. ret
c9570 75 72 6e 20 22 45 52 52 4f 52 22 3b 0a 7d 0a 23 urn "ERROR";.}.#
c9580 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 endif..#ifdef SQ
c9590 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a LITE_LOCK_TRACE.
c95a0 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 6f 75 74 20 /*.** Print out
c95b0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 information abou
c95c0 74 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6f 70 t all locking op
c95d0 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 erations..**.**
c95e0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
c95f0 75 73 65 64 20 66 6f 72 20 74 72 6f 75 62 6c 65 used for trouble
c9600 73 68 6f 6f 74 69 6e 67 20 6c 6f 63 6b 73 20 6f shooting locks o
c9610 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 65 64 0a n multithreaded.
c9620 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20 45 ** platforms. E
c9630 6e 61 62 6c 65 20 62 79 20 63 6f 6d 70 69 6c 69 nable by compili
c9640 6e 67 20 77 69 74 68 20 74 68 65 20 2d 44 53 51 ng with the -DSQ
c9650 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a LITE_LOCK_TRACE.
c9660 2a 2a 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 ** command-line
c9670 6f 70 74 69 6f 6e 20 6f 6e 20 74 68 65 20 63 6f option on the co
c9680 6d 70 69 6c 65 72 2e 20 20 54 68 69 73 20 63 6f mpiler. This co
c9690 64 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a de is normally.*
c96a0 2a 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2f * turned off..*/
c96b0 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b .static int lock
c96c0 54 72 61 63 65 28 69 6e 74 20 66 64 2c 20 69 6e Trace(int fd, in
c96d0 74 20 6f 70 2c 20 73 74 72 75 63 74 20 66 6c 6f t op, struct flo
c96e0 63 6b 20 2a 70 29 7b 0a 20 20 63 68 61 72 20 2a ck *p){. char *
c96f0 7a 4f 70 4e 61 6d 65 2c 20 2a 7a 54 79 70 65 3b zOpName, *zType;
c9700 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 6e 74 20 . int s;. int
c9710 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20 69 66 savedErrno;. if
c9720 28 20 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20 29 7b ( op==F_GETLK ){
c9730 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 . zOpName = "
c9740 47 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 GETLK";. }else
c9750 69 66 28 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 if( op==F_SETLK
c9760 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d ){. zOpName =
c9770 20 22 53 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 "SETLK";. }els
c9780 65 7b 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c e{. s = fcntl
c9790 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 20 (fd, op, p);.
c97a0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
c97b0 6e 74 66 28 22 66 63 6e 74 6c 20 75 6e 6b 6e 6f ntf("fcntl unkno
c97c0 77 6e 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 wn %d %d %d\n",
c97d0 66 64 2c 20 6f 70 2c 20 73 29 3b 0a 20 20 20 20 fd, op, s);.
c97e0 72 65 74 75 72 6e 20 73 3b 0a 20 20 7d 0a 20 20 return s;. }.
c97f0 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 if( p->l_type==F
c9800 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 _RDLCK ){. zT
c9810 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 ype = "RDLCK";.
c9820 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f }else if( p->l_
c9830 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b type==F_WRLCK ){
c9840 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52 . zType = "WR
c9850 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 LCK";. }else if
c9860 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 55 ( p->l_type==F_U
c9870 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 NLCK ){. zTyp
c9880 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 7d e = "UNLCK";. }
c9890 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
c98a0 28 20 30 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 ( 0 );. }. ass
c98b0 65 72 74 28 20 70 2d 3e 6c 5f 77 68 65 6e 63 65 ert( p->l_whence
c98c0 3d 3d 53 45 45 4b 5f 53 45 54 20 29 3b 0a 20 20 ==SEEK_SET );.
c98d0 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f 70 s = fcntl(fd, op
c98e0 2c 20 70 29 3b 0a 20 20 73 61 76 65 64 45 72 72 , p);. savedErr
c98f0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 73 71 no = errno;. sq
c9900 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
c9910 28 22 66 63 6e 74 6c 20 25 64 20 25 64 20 25 73 ("fcntl %d %d %s
c9920 20 25 73 20 25 64 20 25 64 20 25 64 20 25 64 5c %s %d %d %d %d\
c9930 6e 22 2c 0a 20 20 20 20 20 74 68 72 65 61 64 69 n",. threadi
c9940 64 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d 65 2c 20 d, fd, zOpName,
c9950 7a 54 79 70 65 2c 20 28 69 6e 74 29 70 2d 3e 6c zType, (int)p->l
c9960 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 70 2d 3e _start, (int)p->
c9970 6c 5f 6c 65 6e 2c 0a 20 20 20 20 20 28 69 6e 74 l_len,. (int
c9980 29 70 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b 0a 20 )p->l_pid, s);.
c9990 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 26 26 20 if( s==(-1) &&
c99a0 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 26 26 20 28 op==F_SETLK && (
c99b0 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c p->l_type==F_RDL
c99c0 43 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70 65 3d CK || p->l_type=
c99d0 3d 46 5f 57 52 4c 43 4b 29 20 29 7b 0a 20 20 20 =F_WRLCK) ){.
c99e0 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 32 struct flock l2
c99f0 3b 0a 20 20 20 20 6c 32 20 3d 20 2a 70 3b 0a 20 ;. l2 = *p;.
c9a00 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 fcntl(fd, F_G
c9a10 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a 20 20 20 20 ETLK, &l2);.
c9a20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 if( l2.l_type==F
c9a30 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 _RDLCK ){.
c9a40 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b zType = "RDLCK";
c9a50 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c . }else if( l
c9a60 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 2.l_type==F_WRLC
c9a70 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 K ){. zType
c9a80 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20 20 20 = "WRLCK";.
c9a90 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 }else if( l2.l_t
c9aa0 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a ype==F_UNLCK ){.
c9ab0 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 55 zType = "U
c9ac0 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 NLCK";. }else
c9ad0 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
c9ae0 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 0 );. }. s
c9af0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
c9b00 66 28 22 66 63 6e 74 6c 2d 66 61 69 6c 75 72 65 f("fcntl-failure
c9b10 2d 72 65 61 73 6f 6e 3a 20 25 73 20 25 64 20 25 -reason: %s %d %
c9b20 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 d %d\n",.
c9b30 7a 54 79 70 65 2c 20 28 69 6e 74 29 6c 32 2e 6c zType, (int)l2.l
c9b40 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 6c 32 2e _start, (int)l2.
c9b50 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29 6c 32 2e 6c l_len, (int)l2.l
c9b60 5f 70 69 64 29 3b 0a 20 20 7d 0a 20 20 65 72 72 _pid);. }. err
c9b70 6e 6f 20 3d 20 73 61 76 65 64 45 72 72 6e 6f 3b no = savedErrno;
c9b80 0a 20 20 72 65 74 75 72 6e 20 73 3b 0a 7d 0a 23 . return s;.}.#
c9b90 64 65 66 69 6e 65 20 66 63 6e 74 6c 20 6c 6f 63 define fcntl loc
c9ba0 6b 54 72 61 63 65 0a 23 65 6e 64 69 66 20 2f 2a kTrace.#endif /*
c9bb0 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 SQLITE_LOCK_TRA
c9bc0 43 45 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 CE */..../*.** T
c9bd0 68 69 73 20 72 6f 75 74 69 6e 65 20 74 72 61 6e his routine tran
c9be0 73 6c 61 74 65 73 20 61 20 73 74 61 6e 64 61 72 slates a standar
c9bf0 64 20 50 4f 53 49 58 20 65 72 72 6e 6f 20 63 6f d POSIX errno co
c9c00 64 65 20 69 6e 74 6f 20 73 6f 6d 65 74 68 69 6e de into somethin
c9c10 67 0a 2a 2a 20 75 73 65 66 75 6c 20 74 6f 20 74 g.** useful to t
c9c20 68 65 20 63 6c 69 65 6e 74 73 20 6f 66 20 74 68 he clients of th
c9c30 65 20 73 71 6c 69 74 65 33 20 66 75 6e 63 74 69 e sqlite3 functi
c9c40 6f 6e 73 2e 20 20 53 70 65 63 69 66 69 63 61 6c ons. Specifical
c9c50 6c 79 2c 20 69 74 20 69 73 0a 2a 2a 20 69 6e 74 ly, it is.** int
c9c60 65 6e 64 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 ended to transla
c9c70 74 65 20 61 20 76 61 72 69 65 74 79 20 6f 66 20 te a variety of
c9c80 22 74 72 79 20 61 67 61 69 6e 22 20 65 72 72 6f "try again" erro
c9c90 72 73 20 69 6e 74 6f 20 53 51 4c 49 54 45 5f 42 rs into SQLITE_B
c9ca0 55 53 59 0a 2a 2a 20 61 6e 64 20 61 20 76 61 72 USY.** and a var
c9cb0 69 65 74 79 20 6f 66 20 22 70 6c 65 61 73 65 20 iety of "please
c9cc0 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 64 close the file d
c9cd0 65 73 63 72 69 70 74 6f 72 20 4e 4f 57 22 20 65 escriptor NOW" e
c9ce0 72 72 6f 72 73 20 69 6e 74 6f 20 0a 2a 2a 20 53 rrors into .** S
c9cf0 51 4c 49 54 45 5f 49 4f 45 52 52 0a 2a 2a 20 0a QLITE_IOERR.** .
c9d00 2a 2a 20 45 72 72 6f 72 73 20 64 75 72 69 6e 67 ** Errors during
c9d10 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
c9d20 6f 66 20 6c 6f 63 6b 73 2c 20 6f 72 20 66 69 6c of locks, or fil
c9d30 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 e system support
c9d40 20 66 6f 72 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 73 for locks,.** s
c9d50 68 6f 75 6c 64 20 68 61 6e 64 6c 65 20 45 4e 4f hould handle ENO
c9d60 4c 43 4b 2c 20 45 4e 4f 54 53 55 50 2c 20 45 4f LCK, ENOTSUP, EO
c9d70 50 4e 4f 54 53 55 50 50 20 73 65 70 61 72 61 74 PNOTSUPP separat
c9d80 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ely..*/.static i
c9d90 6e 74 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 nt sqliteErrorFr
c9da0 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 69 6e 74 omPosixError(int
c9db0 20 70 6f 73 69 78 45 72 72 6f 72 2c 20 69 6e 74 posixError, int
c9dc0 20 73 71 6c 69 74 65 49 4f 45 72 72 29 20 7b 0a sqliteIOErr) {.
c9dd0 20 20 73 77 69 74 63 68 20 28 70 6f 73 69 78 45 switch (posixE
c9de0 72 72 6f 72 29 20 7b 0a 20 20 63 61 73 65 20 30 rror) {. case 0
c9df0 3a 20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 : . return SQ
c9e00 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20 LITE_OK;. .
c9e10 63 61 73 65 20 45 41 47 41 49 4e 3a 0a 20 20 63 case EAGAIN:. c
c9e20 61 73 65 20 45 54 49 4d 45 44 4f 55 54 3a 0a 20 ase ETIMEDOUT:.
c9e30 20 63 61 73 65 20 45 42 55 53 59 3a 0a 20 20 63 case EBUSY:. c
c9e40 61 73 65 20 45 49 4e 54 52 3a 0a 20 20 63 61 73 ase EINTR:. cas
c9e50 65 20 45 4e 4f 4c 43 4b 3a 20 20 0a 20 20 20 20 e ENOLCK: .
c9e60 2f 2a 20 72 61 6e 64 6f 6d 20 4e 46 53 20 72 65 /* random NFS re
c9e70 74 72 79 20 65 72 72 6f 72 2c 20 75 6e 6c 65 73 try error, unles
c9e80 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 s during file sy
c9e90 73 74 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 20 stem support .
c9ea0 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 69 * introspecti
c9eb0 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 74 20 on, in which it
c9ec0 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 actually means w
c9ed0 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 20 hat it says */.
c9ee0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
c9ef0 5f 42 55 53 59 3b 0a 20 20 20 20 0a 20 20 63 61 _BUSY;. . ca
c9f00 73 65 20 45 41 43 43 45 53 3a 20 0a 20 20 20 20 se EACCES: .
c9f10 2f 2a 20 45 41 43 43 45 53 20 69 73 20 6c 69 6b /* EACCES is lik
c9f20 65 20 45 41 47 41 49 4e 20 64 75 72 69 6e 67 20 e EAGAIN during
c9f30 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f locking operatio
c9f40 6e 73 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 ns, but not any
c9f50 6f 74 68 65 72 20 74 69 6d 65 2a 2f 0a 20 20 20 other time*/.
c9f60 20 69 66 28 20 28 73 71 6c 69 74 65 49 4f 45 72 if( (sqliteIOEr
c9f70 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 r == SQLITE_IOER
c9f80 52 5f 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71 R_LOCK) || ..(sq
c9f90 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c liteIOErr == SQL
c9fa0 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b ITE_IOERR_UNLOCK
c9fb0 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f ) || ..(sqliteIO
c9fc0 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f Err == SQLITE_IO
c9fd0 45 52 52 5f 52 44 4c 4f 43 4b 29 20 7c 7c 0a 09 ERR_RDLOCK) ||..
c9fe0 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 (sqliteIOErr ==
c9ff0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 SQLITE_IOERR_CHE
ca000 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 20 CKRESERVEDLOCK)
ca010 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
ca020 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 SQLITE_BUSY;.
ca030 20 7d 0a 20 20 20 20 2f 2a 20 65 6c 73 65 20 66 }. /* else f
ca040 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 all through */.
ca050 20 63 61 73 65 20 45 50 45 52 4d 3a 20 0a 20 20 case EPERM: .
ca060 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
ca070 50 45 52 4d 3b 0a 20 20 20 20 0a 20 20 63 61 73 PERM;. . cas
ca080 65 20 45 44 45 41 44 4c 4b 3a 0a 20 20 20 20 72 e EDEADLK:. r
ca090 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
ca0a0 52 52 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 RR_BLOCKED;.
ca0b0 0a 23 69 66 20 45 4f 50 4e 4f 54 53 55 50 50 21 .#if EOPNOTSUPP!
ca0c0 3d 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 20 =ENOTSUP. case
ca0d0 45 4f 50 4e 4f 54 53 55 50 50 3a 20 0a 20 20 20 EOPNOTSUPP: .
ca0e0 20 2f 2a 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 /* something we
ca0f0 6e 74 20 74 65 72 72 69 62 6c 79 20 61 77 72 79 nt terribly awry
ca100 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 , unless during
ca110 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 file system supp
ca120 6f 72 74 20 0a 20 20 20 20 20 2a 20 69 6e 74 72 ort . * intr
ca130 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 ospection, in wh
ca140 69 63 68 20 69 74 20 61 63 74 75 61 6c 6c 79 20 ich it actually
ca150 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 73 61 means what it sa
ca160 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 ys */.#endif.#if
ca170 64 65 66 20 45 4e 4f 54 53 55 50 0a 20 20 63 61 def ENOTSUP. ca
ca180 73 65 20 45 4e 4f 54 53 55 50 3a 20 0a 20 20 20 se ENOTSUP: .
ca190 20 2f 2a 20 69 6e 76 61 6c 69 64 20 66 64 2c 20 /* invalid fd,
ca1a0 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 unless during fi
ca1b0 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 le system suppor
ca1c0 74 20 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c t introspection,
ca1d0 20 69 6e 20 77 68 69 63 68 20 0a 20 20 20 20 20 in which .
ca1e0 2a 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 * it actually me
ca1f0 61 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 73 ans what it says
ca200 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 61 73 */.#endif. cas
ca210 65 20 45 49 4f 3a 0a 20 20 63 61 73 65 20 45 42 e EIO:. case EB
ca220 41 44 46 3a 0a 20 20 63 61 73 65 20 45 49 4e 56 ADF:. case EINV
ca230 41 4c 3a 0a 20 20 63 61 73 65 20 45 4e 4f 54 43 AL:. case ENOTC
ca240 4f 4e 4e 3a 0a 20 20 63 61 73 65 20 45 4e 4f 44 ONN:. case ENOD
ca250 45 56 3a 0a 20 20 63 61 73 65 20 45 4e 58 49 4f EV:. case ENXIO
ca260 3a 0a 20 20 63 61 73 65 20 45 4e 4f 45 4e 54 3a :. case ENOENT:
ca270 0a 20 20 63 61 73 65 20 45 53 54 41 4c 45 3a 0a . case ESTALE:.
ca280 20 20 63 61 73 65 20 45 4e 4f 53 59 53 3a 0a 20 case ENOSYS:.
ca290 20 20 20 2f 2a 20 74 68 65 73 65 20 73 68 6f 75 /* these shou
ca2a0 6c 64 20 66 6f 72 63 65 20 74 68 65 20 63 6c 69 ld force the cli
ca2b0 65 6e 74 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 ent to close the
ca2c0 20 66 69 6c 65 20 61 6e 64 20 72 65 63 6f 6e 6e file and reconn
ca2d0 65 63 74 20 2a 2f 0a 20 20 20 20 0a 20 20 64 65 ect */. . de
ca2e0 66 61 75 6c 74 3a 20 0a 20 20 20 20 72 65 74 75 fault: . retu
ca2f0 72 6e 20 73 71 6c 69 74 65 49 4f 45 72 72 3b 0a rn sqliteIOErr;.
ca300 20 20 7d 0a 7d 0a 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a }.}..../******
ca310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ca320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ca330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ca340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ca350 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a ********.*******
ca360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
ca370 6e 20 55 6e 69 71 75 65 20 46 69 6c 65 20 49 44 n Unique File ID
ca380 20 55 74 69 6c 69 74 79 20 55 73 65 64 20 42 79 Utility Used By
ca390 20 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a VxWorks *******
ca3a0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f ********.**.** O
ca3b0 6e 20 6d 6f 73 74 20 76 65 72 73 69 6f 6e 73 20 n most versions
ca3c0 6f 66 20 75 6e 69 78 2c 20 77 65 20 63 61 6e 20 of unix, we can
ca3d0 67 65 74 20 61 20 75 6e 69 71 75 65 20 49 44 20 get a unique ID
ca3e0 66 6f 72 20 61 20 66 69 6c 65 20 62 79 20 63 6f for a file by co
ca3f0 6e 63 61 74 65 6e 61 74 69 6e 67 0a 2a 2a 20 74 ncatenating.** t
ca400 68 65 20 64 65 76 69 63 65 20 6e 75 6d 62 65 72 he device number
ca410 20 61 6e 64 20 74 68 65 20 69 6e 6f 64 65 20 6e and the inode n
ca420 75 6d 62 65 72 2e 20 20 42 75 74 20 74 68 69 73 umber. But this
ca430 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f does not work o
ca440 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 4f 6e n VxWorks..** On
ca450 20 56 78 57 6f 72 6b 73 2c 20 61 20 75 6e 69 71 VxWorks, a uniq
ca460 75 65 20 66 69 6c 65 20 69 64 20 6d 75 73 74 20 ue file id must
ca470 62 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 be based on the
ca480 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 canonical filena
ca490 6d 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e me..**.** A poin
ca4a0 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e ter to an instan
ca4b0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
ca4c0 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 61 ing structure ca
ca4d0 6e 20 62 65 20 75 73 65 64 20 61 73 20 61 0a 2a n be used as a.*
ca4e0 2a 20 75 6e 69 71 75 65 20 66 69 6c 65 20 49 44 * unique file ID
ca4f0 20 69 6e 20 56 78 57 6f 72 6b 73 2e 20 20 45 61 in VxWorks. Ea
ca500 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 ch instance of t
ca510 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63 6f his structure co
ca520 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 63 6f 70 79 ntains.** a copy
ca530 20 6f 66 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 of the canonica
ca540 6c 20 66 69 6c 65 6e 61 6d 65 2e 20 20 54 68 65 l filename. The
ca550 72 65 20 69 73 20 61 6c 73 6f 20 61 20 72 65 66 re is also a ref
ca560 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e 20 20 0a erence count. .
ca570 2a 2a 20 54 68 65 20 73 74 72 75 63 74 75 72 65 ** The structure
ca580 20 69 73 20 72 65 63 6c 61 69 6d 65 64 20 77 68 is reclaimed wh
ca590 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 en the number of
ca5a0 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 69 74 20 pointers to it
ca5b0 64 72 6f 70 73 20 74 6f 0a 2a 2a 20 7a 65 72 6f drops to.** zero
ca5c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 ..**.** There ar
ca5d0 65 20 6e 65 76 65 72 20 76 65 72 79 20 6d 61 6e e never very man
ca5e0 79 20 66 69 6c 65 73 20 6f 70 65 6e 20 61 74 20 y files open at
ca5f0 6f 6e 65 20 74 69 6d 65 20 61 6e 64 20 6c 6f 6f one time and loo
ca600 6b 75 70 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 kups are not.**
ca610 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 2d 63 72 a performance-cr
ca620 69 74 69 63 61 6c 20 70 61 74 68 2c 20 73 6f 20 itical path, so
ca630 69 74 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 it is sufficient
ca640 20 74 6f 20 70 75 74 20 74 68 65 73 65 0a 2a 2a to put these.**
ca650 20 73 74 72 75 63 74 75 72 65 73 20 6f 6e 20 61 structures on a
ca660 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2f linked list..*/
ca670 0a 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 .struct vxworksF
ca680 69 6c 65 49 64 20 7b 0a 20 20 73 74 72 75 63 74 ileId {. struct
ca690 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a vxworksFileId *
ca6a0 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 pNext; /* Next
ca6b0 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 74 68 65 in a list of the
ca6c0 6d 20 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e m all */. int n
ca6d0 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Ref;
ca6e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
ca6f0 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 er of references
ca700 20 74 6f 20 74 68 69 73 20 6f 6e 65 20 2a 2f 0a to this one */.
ca710 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20 int nName;
ca720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca730 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 /* Length of the
ca740 20 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 5b zCanonicalName[
ca750 5d 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68 ] string */. ch
ca760 61 72 20 2a 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 ar *zCanonicalNa
ca770 6d 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 me; /* C
ca780 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d anonical filenam
ca790 65 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 4f 53 5f e */.};..#if OS_
ca7a0 56 58 57 4f 52 4b 53 0a 2f 2a 20 0a 2a 2a 20 41 VXWORKS./* .** A
ca7b0 6c 6c 20 75 6e 69 71 75 65 20 66 69 6c 65 6e 61 ll unique filena
ca7c0 6d 65 73 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 mes are held on
ca7d0 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 a linked list he
ca7e0 61 64 65 64 20 62 79 20 74 68 69 73 0a 2a 2a 20 aded by this.**
ca7f0 76 61 72 69 61 62 6c 65 3a 0a 2a 2f 0a 73 74 61 variable:.*/.sta
ca800 74 69 63 20 73 74 72 75 63 74 20 76 78 77 6f 72 tic struct vxwor
ca810 6b 73 46 69 6c 65 49 64 20 2a 76 78 77 6f 72 6b ksFileId *vxwork
ca820 73 46 69 6c 65 4c 69 73 74 20 3d 20 30 3b 0a 0a sFileList = 0;..
ca830 2f 2a 0a 2a 2a 20 53 69 6d 70 6c 69 66 79 20 61 /*.** Simplify a
ca840 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 69 filename into i
ca850 74 73 20 63 61 6e 6f 6e 69 63 61 6c 20 66 6f 72 ts canonical for
ca860 6d 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e 67 20 74 m.** by making t
ca870 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 he following cha
ca880 6e 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 72 nges:.**.** * r
ca890 65 6d 6f 76 69 6e 67 20 61 6e 79 20 74 72 61 69 emoving any trai
ca8a0 6c 69 6e 67 20 61 6e 64 20 64 75 70 6c 69 63 61 ling and duplica
ca8b0 74 65 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 te /.** * conve
ca8c0 72 74 20 2f 2e 2f 20 69 6e 74 6f 20 6a 75 73 74 rt /./ into just
ca8d0 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74 /.** * convert
ca8e0 20 2f 41 2f 2e 2e 2f 20 77 68 65 72 65 20 41 20 /A/../ where A
ca8f0 69 73 20 61 6e 79 20 73 69 6d 70 6c 65 20 6e 61 is any simple na
ca900 6d 65 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a me into just /.*
ca910 2a 0a 2a 2a 20 43 68 61 6e 67 65 73 20 61 72 65 *.** Changes are
ca920 20 6d 61 64 65 20 69 6e 2d 70 6c 61 63 65 2e 20 made in-place.
ca930 20 52 65 74 75 72 6e 20 74 68 65 20 6e 65 77 20 Return the new
ca940 6e 61 6d 65 20 6c 65 6e 67 74 68 2e 0a 2a 2a 0a name length..**.
ca950 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 ** The original
ca960 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 20 7a filename is in z
ca970 5b 30 2e 2e 6e 2d 31 5d 2e 20 20 52 65 74 75 72 [0..n-1]. Retur
ca980 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a n the number of.
ca990 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 69 6e ** characters in
ca9a0 20 74 68 65 20 73 69 6d 70 6c 69 66 69 65 64 20 the simplified
ca9b0 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 name..*/.static
ca9c0 69 6e 74 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c int vxworksSimpl
ca9d0 69 66 79 4e 61 6d 65 28 63 68 61 72 20 2a 7a 2c ifyName(char *z,
ca9e0 20 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 int n){. int i
ca9f0 2c 20 6a 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e , j;. while( n>
caa00 31 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 2f 27 1 && z[n-1]=='/'
caa10 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 66 6f 72 ){ n--; }. for
caa20 28 69 3d 6a 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b (i=j=0; i<n; i++
caa30 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d ){. if( z[i]=
caa40 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 69 66 ='/' ){. if
caa50 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2f 27 20 29 20 ( z[i+1]=='/' )
caa60 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 continue;.
caa70 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 if( z[i+1]=='.'
caa80 26 26 20 69 2b 32 3c 6e 20 26 26 20 7a 5b 69 2b && i+2<n && z[i+
caa90 32 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 2]=='/' ){.
caaa0 20 20 20 69 20 2b 3d 20 31 3b 0a 20 20 20 20 20 i += 1;.
caab0 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 continue;.
caac0 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 7a }. if( z
caad0 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b [i+1]=='.' && i+
caae0 33 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 3<n && z[i+2]=='
caaf0 2e 27 20 26 26 20 7a 5b 69 2b 33 5d 3d 3d 27 2f .' && z[i+3]=='/
cab00 27 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68 69 ' ){. whi
cab10 6c 65 28 20 6a 3e 30 20 26 26 20 7a 5b 6a 2d 31 le( j>0 && z[j-1
cab20 5d 21 3d 27 2f 27 20 29 7b 20 6a 2d 2d 3b 20 7d ]!='/' ){ j--; }
cab30 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3e 30 . if( j>0
cab40 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 ){ j--; }.
cab50 20 20 20 69 20 2b 3d 20 32 3b 0a 20 20 20 20 20 i += 2;.
cab60 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 continue;.
cab70 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a }. }. z
cab80 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 [j++] = z[i];.
cab90 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 }. z[j] = 0;.
caba0 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 0a return j;.}../*.
cabb0 2a 2a 20 46 69 6e 64 20 61 20 75 6e 69 71 75 65 ** Find a unique
cabc0 20 66 69 6c 65 20 49 44 20 66 6f 72 20 74 68 65 file ID for the
cabd0 20 67 69 76 65 6e 20 61 62 73 6f 6c 75 74 65 20 given absolute
cabe0 70 61 74 68 6e 61 6d 65 2e 20 20 52 65 74 75 72 pathname. Retur
cabf0 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 n.** a pointer t
cac00 6f 20 74 68 65 20 76 78 77 6f 72 6b 73 46 69 6c o the vxworksFil
cac10 65 49 64 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 eId object. Thi
cac20 73 20 70 6f 69 6e 74 65 72 20 69 73 20 74 68 65 s pointer is the
cac30 20 75 6e 69 71 75 65 0a 2a 2a 20 66 69 6c 65 20 unique.** file
cac40 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 52 ID..**.** The nR
cac50 65 66 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 ef field of the
cac60 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 vxworksFileId ob
cac70 6a 65 63 74 20 69 73 20 69 6e 63 72 65 6d 65 6e ject is incremen
cac80 74 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 ted before.** th
cac90 65 20 6f 62 6a 65 63 74 20 69 73 20 72 65 74 75 e object is retu
caca0 72 6e 65 64 2e 20 20 41 20 6e 65 77 20 76 78 77 rned. A new vxw
cacb0 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 orksFileId objec
cacc0 74 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 t is created.**
cacd0 61 6e 64 20 61 64 64 65 64 20 74 6f 20 74 68 65 and added to the
cace0 20 67 6c 6f 62 61 6c 20 6c 69 73 74 20 69 66 20 global list if
cacf0 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a necessary..**.**
cad00 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c If a memory all
cad10 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 ocation error oc
cad20 63 75 72 73 2c 20 72 65 74 75 72 6e 20 4e 55 4c curs, return NUL
cad30 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 L..*/.static str
cad40 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 uct vxworksFileI
cad50 64 20 2a 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 d *vxworksFindFi
cad60 6c 65 49 64 28 63 6f 6e 73 74 20 63 68 61 72 20 leId(const char
cad70 2a 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 29 7b *zAbsoluteName){
cad80 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b . struct vxwork
cad90 73 46 69 6c 65 49 64 20 2a 70 4e 65 77 3b 20 20 sFileId *pNew;
cada0 20 20 20 20 20 20 20 2f 2a 20 73 65 61 72 63 68 /* search
cadb0 20 6b 65 79 20 61 6e 64 20 6e 65 77 20 66 69 6c key and new fil
cadc0 65 20 49 44 20 2a 2f 0a 20 20 73 74 72 75 63 74 e ID */. struct
cadd0 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a vxworksFileId *
cade0 70 43 61 6e 64 69 64 61 74 65 3b 20 20 20 2f 2a pCandidate; /*
cadf0 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 For looping ove
cae00 72 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 20 r existing file
cae10 49 44 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 IDs */. int n;
cae20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cae30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
cae40 4c 65 6e 67 74 68 20 6f 66 20 7a 41 62 73 6f 6c Length of zAbsol
cae50 75 74 65 4e 61 6d 65 20 73 74 72 69 6e 67 20 2a uteName string *
cae60 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 41 62 /.. assert( zAb
cae70 73 6f 6c 75 74 65 4e 61 6d 65 5b 30 5d 3d 3d 27 soluteName[0]=='
cae80 2f 27 20 29 3b 0a 20 20 6e 20 3d 20 28 69 6e 74 /' );. n = (int
cae90 29 73 74 72 6c 65 6e 28 7a 41 62 73 6f 6c 75 74 )strlen(zAbsolut
caea0 65 4e 61 6d 65 29 3b 0a 20 20 70 4e 65 77 20 3d eName);. pNew =
caeb0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
caec0 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 2b sizeof(*pNew) +
caed0 20 28 6e 2b 31 29 20 29 3b 0a 20 20 69 66 28 20 (n+1) );. if(
caee0 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e pNew==0 ) return
caef0 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 7a 43 61 6e 0;. pNew->zCan
caf00 6f 6e 69 63 61 6c 4e 61 6d 65 20 3d 20 28 63 68 onicalName = (ch
caf10 61 72 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 ar*)&pNew[1];.
caf20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 7a 43 61 memcpy(pNew->zCa
caf30 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 7a 41 62 nonicalName, zAb
caf40 73 6f 6c 75 74 65 4e 61 6d 65 2c 20 6e 2b 31 29 soluteName, n+1)
caf50 3b 0a 20 20 6e 20 3d 20 76 78 77 6f 72 6b 73 53 ;. n = vxworksS
caf60 69 6d 70 6c 69 66 79 4e 61 6d 65 28 70 4e 65 77 implifyName(pNew
caf70 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 ->zCanonicalName
caf80 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 72 , n);.. /* Sear
caf90 63 68 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69 ch for an existi
cafa0 6e 67 20 65 6e 74 72 79 20 74 68 61 74 20 6d 61 ng entry that ma
cafb0 74 63 68 69 6e 67 20 74 68 65 20 63 61 6e 6f 6e tching the canon
cafc0 69 63 61 6c 20 6e 61 6d 65 2e 0a 20 20 2a 2a 20 ical name.. **
cafd0 49 66 20 66 6f 75 6e 64 2c 20 69 6e 63 72 65 6d If found, increm
cafe0 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 ent the referenc
caff0 65 20 63 6f 75 6e 74 20 61 6e 64 20 72 65 74 75 e count and retu
cb000 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a rn a pointer to.
cb010 20 20 2a 2a 20 74 68 65 20 65 78 69 73 74 69 6e ** the existin
cb020 67 20 66 69 6c 65 20 49 44 2e 0a 20 20 2a 2f 0a g file ID.. */.
cb030 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 unixEnterMutex
cb040 28 29 3b 0a 20 20 66 6f 72 28 70 43 61 6e 64 69 ();. for(pCandi
cb050 64 61 74 65 3d 76 78 77 6f 72 6b 73 46 69 6c 65 date=vxworksFile
cb060 4c 69 73 74 3b 20 70 43 61 6e 64 69 64 61 74 65 List; pCandidate
cb070 3b 20 70 43 61 6e 64 69 64 61 74 65 3d 70 43 61 ; pCandidate=pCa
cb080 6e 64 69 64 61 74 65 2d 3e 70 4e 65 78 74 29 7b ndidate->pNext){
cb090 0a 20 20 20 20 69 66 28 20 70 43 61 6e 64 69 64 . if( pCandid
cb0a0 61 74 65 2d 3e 6e 4e 61 6d 65 3d 3d 6e 20 0a 20 ate->nName==n .
cb0b0 20 20 20 20 26 26 20 6d 65 6d 63 6d 70 28 70 43 && memcmp(pC
cb0c0 61 6e 64 69 64 61 74 65 2d 3e 7a 43 61 6e 6f 6e andidate->zCanon
cb0d0 69 63 61 6c 4e 61 6d 65 2c 20 70 4e 65 77 2d 3e icalName, pNew->
cb0e0 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 zCanonicalName,
cb0f0 6e 29 3d 3d 30 0a 20 20 20 20 29 7b 0a 20 20 20 n)==0. ){.
cb100 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
cb110 28 70 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 70 (pNew);. p
cb120 43 61 6e 64 69 64 61 74 65 2d 3e 6e 52 65 66 2b Candidate->nRef+
cb130 2b 3b 0a 20 20 20 20 20 20 20 75 6e 69 78 4c 65 +;. unixLe
cb140 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 aveMutex();.
cb150 20 20 20 72 65 74 75 72 6e 20 70 43 61 6e 64 69 return pCandi
cb160 64 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a date;. }. }.
cb170 0a 20 20 2f 2a 20 4e 6f 20 6d 61 74 63 68 20 77 . /* No match w
cb180 61 73 20 66 6f 75 6e 64 2e 20 20 57 65 20 77 69 as found. We wi
cb190 6c 6c 20 6d 61 6b 65 20 61 20 6e 65 77 20 66 69 ll make a new fi
cb1a0 6c 65 20 49 44 20 2a 2f 0a 20 20 70 4e 65 77 2d le ID */. pNew-
cb1b0 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 65 >nRef = 1;. pNe
cb1c0 77 2d 3e 6e 4e 61 6d 65 20 3d 20 6e 3b 0a 20 20 w->nName = n;.
cb1d0 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 76 78 pNew->pNext = vx
cb1e0 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 0a 20 worksFileList;.
cb1f0 20 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 vxworksFileList
cb200 20 3d 20 70 4e 65 77 3b 0a 20 20 75 6e 69 78 4c = pNew;. unixL
cb210 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 eaveMutex();. r
cb220 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f eturn pNew;.}../
cb230 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 *.** Decrement t
cb240 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 he reference cou
cb250 6e 74 20 6f 6e 20 61 20 76 78 77 6f 72 6b 73 46 nt on a vxworksF
cb260 69 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 20 46 ileId object. F
cb270 72 65 65 0a 2a 2a 20 74 68 65 20 6f 62 6a 65 63 ree.** the objec
cb280 74 20 77 68 65 6e 20 74 68 65 20 72 65 66 65 72 t when the refer
cb290 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68 ence count reach
cb2a0 65 73 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 es zero..*/.stat
cb2b0 69 63 20 76 6f 69 64 20 76 78 77 6f 72 6b 73 52 ic void vxworksR
cb2c0 65 6c 65 61 73 65 46 69 6c 65 49 64 28 73 74 72 eleaseFileId(str
cb2d0 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 uct vxworksFileI
cb2e0 64 20 2a 70 49 64 29 7b 0a 20 20 75 6e 69 78 45 d *pId){. unixE
cb2f0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 61 nterMutex();. a
cb300 73 73 65 72 74 28 20 70 49 64 2d 3e 6e 52 65 66 ssert( pId->nRef
cb310 3e 30 20 29 3b 0a 20 20 70 49 64 2d 3e 6e 52 65 >0 );. pId->nRe
cb320 66 2d 2d 3b 0a 20 20 69 66 28 20 70 49 64 2d 3e f--;. if( pId->
cb330 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 73 nRef==0 ){. s
cb340 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c truct vxworksFil
cb350 65 49 64 20 2a 2a 70 70 3b 0a 20 20 20 20 66 6f eId **pp;. fo
cb360 72 28 70 70 3d 26 76 78 77 6f 72 6b 73 46 69 6c r(pp=&vxworksFil
cb370 65 4c 69 73 74 3b 20 2a 70 70 20 26 26 20 2a 70 eList; *pp && *p
cb380 70 21 3d 70 49 64 3b 20 70 70 20 3d 20 26 28 28 p!=pId; pp = &((
cb390 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 7b 7d 0a *pp)->pNext)){}.
cb3a0 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 70 3d assert( *pp=
cb3b0 3d 70 49 64 20 29 3b 0a 20 20 20 20 2a 70 70 20 =pId );. *pp
cb3c0 3d 20 70 49 64 2d 3e 70 4e 65 78 74 3b 0a 20 20 = pId->pNext;.
cb3d0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
cb3e0 49 64 29 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 4c Id);. }. unixL
cb3f0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 eaveMutex();.}.#
cb400 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f endif /* OS_VXWO
cb410 52 4b 53 20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a RKS */./********
cb420 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 55 ******* End of U
cb430 6e 69 71 75 65 20 46 69 6c 65 20 49 44 20 55 74 nique File ID Ut
cb440 69 6c 69 74 79 20 55 73 65 64 20 42 79 20 56 78 ility Used By Vx
cb450 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Works **********
cb460 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
cb470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb4b0 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a *****/.../******
cb4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb500 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a ********.*******
cb510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb520 2a 2a 2a 2a 20 50 6f 73 69 78 20 41 64 76 69 73 **** Posix Advis
cb530 6f 72 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a ory Locking ****
cb540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb550 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 ********.**.** P
cb560 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f OSIX advisory lo
cb570 63 6b 73 20 61 72 65 20 62 72 6f 6b 65 6e 20 62 cks are broken b
cb580 79 20 64 65 73 69 67 6e 2e 20 20 41 4e 53 49 20 y design. ANSI
cb590 53 54 44 20 31 30 30 33 2e 31 20 28 31 39 39 36 STD 1003.1 (1996
cb5a0 29 0a 2a 2a 20 73 65 63 74 69 6f 6e 20 36 2e 35 ).** section 6.5
cb5b0 2e 32 2e 32 20 6c 69 6e 65 73 20 34 38 33 20 74 .2.2 lines 483 t
cb5c0 68 72 6f 75 67 68 20 34 39 30 20 73 70 65 63 69 hrough 490 speci
cb5d0 66 79 20 74 68 61 74 20 77 68 65 6e 20 61 20 70 fy that when a p
cb5e0 72 6f 63 65 73 73 0a 2a 2a 20 73 65 74 73 20 6f rocess.** sets o
cb5f0 72 20 63 6c 65 61 72 73 20 61 20 6c 6f 63 6b 2c r clears a lock,
cb600 20 74 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 that operation
cb610 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20 70 72 overrides any pr
cb620 69 6f 72 20 6c 6f 63 6b 73 20 73 65 74 0a 2a 2a ior locks set.**
cb630 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f by the same pro
cb640 63 65 73 73 2e 20 20 49 74 20 64 6f 65 73 20 6e cess. It does n
cb650 6f 74 20 65 78 70 6c 69 63 69 74 6c 79 20 73 61 ot explicitly sa
cb660 79 20 73 6f 2c 20 62 75 74 20 74 68 69 73 20 69 y so, but this i
cb670 6d 70 6c 69 65 73 0a 2a 2a 20 74 68 61 74 20 69 mplies.** that i
cb680 74 20 6f 76 65 72 72 69 64 65 73 20 6c 6f 63 6b t overrides lock
cb690 73 20 73 65 74 20 62 79 20 74 68 65 20 73 61 6d s set by the sam
cb6a0 65 20 70 72 6f 63 65 73 73 20 75 73 69 6e 67 20 e process using
cb6b0 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 a different.** f
cb6c0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 ile descriptor.
cb6d0 20 43 6f 6e 73 69 64 65 72 20 74 68 69 73 20 74 Consider this t
cb6e0 65 73 74 20 63 61 73 65 3a 0a 2a 2a 0a 2a 2a 20 est case:.**.**
cb6f0 20 20 20 20 20 20 69 6e 74 20 66 64 31 20 3d 20 int fd1 =
cb700 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 31 22 2c 20 open("./file1",
cb710 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20 O_RDWR|O_CREAT,
cb720 30 36 34 34 29 3b 0a 2a 2a 20 20 20 20 20 20 20 0644);.**
cb730 69 6e 74 20 66 64 32 20 3d 20 6f 70 65 6e 28 22 int fd2 = open("
cb740 2e 2f 66 69 6c 65 32 22 2c 20 4f 5f 52 44 57 52 ./file2", O_RDWR
cb750 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 29 3b |O_CREAT, 0644);
cb760 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 2e .**.** Suppose .
cb770 2f 66 69 6c 65 31 20 61 6e 64 20 2e 2f 66 69 6c /file1 and ./fil
cb780 65 32 20 61 72 65 20 72 65 61 6c 6c 79 20 74 68 e2 are really th
cb790 65 20 73 61 6d 65 20 66 69 6c 65 20 28 62 65 63 e same file (bec
cb7a0 61 75 73 65 0a 2a 2a 20 6f 6e 65 20 69 73 20 61 ause.** one is a
cb7b0 20 68 61 72 64 20 6f 72 20 73 79 6d 62 6f 6c 69 hard or symboli
cb7c0 63 20 6c 69 6e 6b 20 74 6f 20 74 68 65 20 6f 74 c link to the ot
cb7d0 68 65 72 29 20 74 68 65 6e 20 69 66 20 79 6f 75 her) then if you
cb7e0 20 73 65 74 0a 2a 2a 20 61 6e 20 65 78 63 6c 75 set.** an exclu
cb7f0 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 66 64 31 sive lock on fd1
cb800 2c 20 74 68 65 6e 20 74 72 79 20 74 6f 20 67 65 , then try to ge
cb810 74 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c t an exclusive l
cb820 6f 63 6b 0a 2a 2a 20 6f 6e 20 66 64 32 2c 20 69 ock.** on fd2, i
cb830 74 20 77 6f 72 6b 73 2e 20 20 49 20 77 6f 75 6c t works. I woul
cb840 64 20 68 61 76 65 20 65 78 70 65 63 74 65 64 20 d have expected
cb850 74 68 65 20 73 65 63 6f 6e 64 20 6c 6f 63 6b 20 the second lock
cb860 74 6f 0a 2a 2a 20 66 61 69 6c 20 73 69 6e 63 65 to.** fail since
cb870 20 74 68 65 72 65 20 77 61 73 20 61 6c 72 65 61 there was alrea
cb880 64 79 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 dy a lock on the
cb890 20 66 69 6c 65 20 64 75 65 20 74 6f 20 66 64 31 file due to fd1
cb8a0 2e 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 73 6f 2e ..** But not so.
cb8b0 20 20 53 69 6e 63 65 20 62 6f 74 68 20 6c 6f 63 Since both loc
cb8c0 6b 73 20 63 61 6d 65 20 66 72 6f 6d 20 74 68 65 ks came from the
cb8d0 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2c 20 74 same process, t
cb8e0 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 6f 76 65 he.** second ove
cb8f0 72 72 69 64 65 73 20 74 68 65 20 66 69 72 73 74 rrides the first
cb900 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 , even though th
cb910 65 79 20 77 65 72 65 20 6f 6e 20 64 69 66 66 65 ey were on diffe
cb920 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65 73 rent.** file des
cb930 63 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 criptors opened
cb940 6f 6e 20 64 69 66 66 65 72 65 6e 74 20 66 69 6c on different fil
cb950 65 20 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 e names..**.** T
cb960 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 77 his means that w
cb970 65 20 63 61 6e 6e 6f 74 20 75 73 65 20 50 4f 53 e cannot use POS
cb980 49 58 20 6c 6f 63 6b 73 20 74 6f 20 73 79 6e 63 IX locks to sync
cb990 68 72 6f 6e 69 7a 65 20 66 69 6c 65 20 61 63 63 hronize file acc
cb9a0 65 73 73 0a 2a 2a 20 61 6d 6f 6e 67 20 63 6f 6d ess.** among com
cb9b0 70 65 74 69 6e 67 20 74 68 72 65 61 64 73 20 6f peting threads o
cb9c0 66 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 f the same proce
cb9d0 73 73 2e 20 20 50 4f 53 49 58 20 6c 6f 63 6b 73 ss. POSIX locks
cb9e0 20 77 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 0a will work fine.
cb9f0 2a 2a 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a ** to synchroniz
cba00 65 20 61 63 63 65 73 73 20 66 6f 72 20 74 68 72 e access for thr
cba10 65 61 64 73 20 69 6e 20 73 65 70 61 72 61 74 65 eads in separate
cba20 20 70 72 6f 63 65 73 73 65 73 2c 20 62 75 74 20 processes, but
cba30 6e 6f 74 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 not.** threads w
cba40 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 70 ithin the same p
cba50 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f rocess..**.** To
cba60 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 work around the
cba70 20 70 72 6f 62 6c 65 6d 2c 20 53 51 4c 69 74 65 problem, SQLite
cba80 20 68 61 73 20 74 6f 20 6d 61 6e 61 67 65 20 66 has to manage f
cba90 69 6c 65 20 6c 6f 63 6b 73 20 69 6e 74 65 72 6e ile locks intern
cbaa0 61 6c 6c 79 0a 2a 2a 20 6f 6e 20 69 74 73 20 6f ally.** on its o
cbab0 77 6e 2e 20 20 57 68 65 6e 65 76 65 72 20 61 20 wn. Whenever a
cbac0 6e 65 77 20 64 61 74 61 62 61 73 65 20 69 73 20 new database is
cbad0 6f 70 65 6e 65 64 2c 20 77 65 20 68 61 76 65 20 opened, we have
cbae0 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20 73 to find the.** s
cbaf0 70 65 63 69 66 69 63 20 69 6e 6f 64 65 20 6f 66 pecific inode of
cbb00 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
cbb10 6c 65 20 28 74 68 65 20 69 6e 6f 64 65 20 69 73 le (the inode is
cbb20 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 determined by t
cbb30 68 65 0a 2a 2a 20 73 74 5f 64 65 76 20 61 6e 64 he.** st_dev and
cbb40 20 73 74 5f 69 6e 6f 20 66 69 65 6c 64 73 20 6f st_ino fields o
cbb50 66 20 74 68 65 20 73 74 61 74 20 73 74 72 75 63 f the stat struc
cbb60 74 75 72 65 20 74 68 61 74 20 66 73 74 61 74 28 ture that fstat(
cbb70 29 20 66 69 6c 6c 73 20 69 6e 29 0a 2a 2a 20 61 ) fills in).** a
cbb80 6e 64 20 63 68 65 63 6b 20 66 6f 72 20 6c 6f 63 nd check for loc
cbb90 6b 73 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 ks already exist
cbba0 69 6e 67 20 6f 6e 20 74 68 61 74 20 69 6e 6f 64 ing on that inod
cbbb0 65 2e 20 20 57 68 65 6e 20 6c 6f 63 6b 73 20 61 e. When locks a
cbbc0 72 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 6f 72 re.** created or
cbbd0 20 72 65 6d 6f 76 65 64 2c 20 77 65 20 68 61 76 removed, we hav
cbbe0 65 20 74 6f 20 6c 6f 6f 6b 20 61 74 20 6f 75 72 e to look at our
cbbf0 20 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 72 65 own internal re
cbc00 63 6f 72 64 20 6f 66 20 74 68 65 0a 2a 2a 20 6c cord of the.** l
cbc10 6f 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 61 ocks to see if a
cbc20 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 68 61 nother thread ha
cbc30 73 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 s previously set
cbc40 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20 a lock on that
cbc50 73 61 6d 65 0a 2a 2a 20 69 6e 6f 64 65 2e 0a 2a same.** inode..*
cbc60 2a 0a 2a 2a 20 28 41 73 69 64 65 3a 20 54 68 65 *.** (Aside: The
cbc70 20 75 73 65 20 6f 66 20 69 6e 6f 64 65 20 6e 75 use of inode nu
cbc80 6d 62 65 72 73 20 61 73 20 75 6e 69 71 75 65 20 mbers as unique
cbc90 49 44 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 IDs does not wor
cbca0 6b 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a k on VxWorks..**
cbcb0 20 46 6f 72 20 56 78 57 6f 72 6b 73 2c 20 77 65 For VxWorks, we
cbcc0 20 68 61 76 65 20 74 6f 20 75 73 65 20 74 68 65 have to use the
cbcd0 20 61 6c 74 65 72 6e 61 74 69 76 65 20 75 6e 69 alternative uni
cbce0 71 75 65 20 49 44 20 73 79 73 74 65 6d 20 62 61 que ID system ba
cbcf0 73 65 64 20 6f 6e 0a 2a 2a 20 63 61 6e 6f 6e 69 sed on.** canoni
cbd00 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 cal filename and
cbd10 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 implemented in
cbd20 74 68 65 20 70 72 65 76 69 6f 75 73 20 64 69 76 the previous div
cbd30 69 73 69 6f 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 ision.).**.** Th
cbd40 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 e sqlite3_file s
cbd50 74 72 75 63 74 75 72 65 20 66 6f 72 20 50 4f 53 tructure for POS
cbd60 49 58 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 IX is no longer
cbd70 6a 75 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20 just an integer
cbd80 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 file.** descript
cbd90 6f 72 2e 20 20 49 74 20 69 73 20 6e 6f 77 20 61 or. It is now a
cbda0 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 structure that
cbdb0 68 6f 6c 64 73 20 74 68 65 20 69 6e 74 65 67 65 holds the intege
cbdc0 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 r file.** descri
cbdd0 70 74 6f 72 20 61 6e 64 20 61 20 70 6f 69 6e 74 ptor and a point
cbde0 65 72 20 74 6f 20 61 20 73 74 72 75 63 74 75 72 er to a structur
cbdf0 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 e that describes
cbe00 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a the internal.**
cbe10 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 63 6f locks on the co
cbe20 72 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e 6f 64 rresponding inod
cbe30 65 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 e. There is one
cbe40 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 locking structu
cbe50 72 65 0a 2a 2a 20 70 65 72 20 69 6e 6f 64 65 2c re.** per inode,
cbe60 20 73 6f 20 69 66 20 74 68 65 20 73 61 6d 65 20 so if the same
cbe70 69 6e 6f 64 65 20 69 73 20 6f 70 65 6e 65 64 20 inode is opened
cbe80 74 77 69 63 65 2c 20 62 6f 74 68 20 75 6e 69 78 twice, both unix
cbe90 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 73 0a File structures.
cbea0 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 ** point to the
cbeb0 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 same locking str
cbec0 75 63 74 75 72 65 2e 20 20 54 68 65 20 6c 6f 63 ucture. The loc
cbed0 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b king structure k
cbee0 65 65 70 73 0a 2a 2a 20 61 20 72 65 66 65 72 65 eeps.** a refere
cbef0 6e 63 65 20 63 6f 75 6e 74 20 28 73 6f 20 77 65 nce count (so we
cbf00 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 6e 20 will know when
cbf10 74 6f 20 64 65 6c 65 74 65 20 69 74 29 20 61 6e to delete it) an
cbf20 64 20 61 20 22 63 6e 74 22 0a 2a 2a 20 66 69 65 d a "cnt".** fie
cbf30 6c 64 20 74 68 61 74 20 74 65 6c 6c 73 20 75 73 ld that tells us
cbf40 20 69 74 73 20 69 6e 74 65 72 6e 61 6c 20 6c 6f its internal lo
cbf50 63 6b 20 73 74 61 74 75 73 2e 20 20 63 6e 74 3d ck status. cnt=
cbf60 3d 30 20 6d 65 61 6e 73 20 74 68 65 0a 2a 2a 20 =0 means the.**
cbf70 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 file is unlocked
cbf80 2e 20 20 63 6e 74 3d 3d 2d 31 20 6d 65 61 6e 73 . cnt==-1 means
cbf90 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 61 6e the file has an
cbfa0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e exclusive lock.
cbfb0 0a 2a 2a 20 63 6e 74 3e 30 20 6d 65 61 6e 73 20 .** cnt>0 means
cbfc0 74 68 65 72 65 20 61 72 65 20 63 6e 74 20 73 68 there are cnt sh
cbfd0 61 72 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 ared locks on th
cbfe0 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e e file..**.** An
cbff0 79 20 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 y attempt to loc
cc000 6b 20 6f 72 20 75 6e 6c 6f 63 6b 20 61 20 66 69 k or unlock a fi
cc010 6c 65 20 66 69 72 73 74 20 63 68 65 63 6b 73 20 le first checks
cc020 74 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 the locking.** s
cc030 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 66 tructure. The f
cc040 63 6e 74 6c 28 29 20 73 79 73 74 65 6d 20 63 61 cntl() system ca
cc050 6c 6c 20 69 73 20 6f 6e 6c 79 20 69 6e 76 6f 6b ll is only invok
cc060 65 64 20 74 6f 20 73 65 74 20 61 20 0a 2a 2a 20 ed to set a .**
cc070 50 4f 53 49 58 20 6c 6f 63 6b 20 69 66 20 74 68 POSIX lock if th
cc080 65 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 e internal lock
cc090 73 74 72 75 63 74 75 72 65 20 74 72 61 6e 73 69 structure transi
cc0a0 74 69 6f 6e 73 20 62 65 74 77 65 65 6e 0a 2a 2a tions between.**
cc0b0 20 61 20 6c 6f 63 6b 65 64 20 61 6e 64 20 61 6e a locked and an
cc0c0 20 75 6e 6c 6f 63 6b 65 64 20 73 74 61 74 65 2e unlocked state.
cc0d0 0a 2a 2a 0a 2a 2a 20 42 75 74 20 77 61 69 74 3a .**.** But wait:
cc0e0 20 20 74 68 65 72 65 20 61 72 65 20 79 65 74 20 there are yet
cc0f0 6d 6f 72 65 20 70 72 6f 62 6c 65 6d 73 20 77 69 more problems wi
cc100 74 68 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 th POSIX advisor
cc110 79 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 y locks..**.** I
cc120 66 20 79 6f 75 20 63 6c 6f 73 65 20 61 20 66 69 f you close a fi
cc130 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 le descriptor th
cc140 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66 at points to a f
cc150 69 6c 65 20 74 68 61 74 20 68 61 73 20 6c 6f 63 ile that has loc
cc160 6b 73 2c 0a 2a 2a 20 61 6c 6c 20 6c 6f 63 6b 73 ks,.** all locks
cc170 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 20 74 68 on that file th
cc180 61 74 20 61 72 65 20 6f 77 6e 65 64 20 62 79 20 at are owned by
cc190 74 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f 63 the current proc
cc1a0 65 73 73 20 61 72 65 0a 2a 2a 20 72 65 6c 65 61 ess are.** relea
cc1b0 73 65 64 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 sed. To work ar
cc1c0 6f 75 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65 ound this proble
cc1d0 6d 2c 20 65 61 63 68 20 75 6e 69 78 46 69 6c 65 m, each unixFile
cc1e0 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 structure conta
cc1f0 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 ins.** a pointer
cc200 20 74 6f 20 61 6e 20 75 6e 69 78 4f 70 65 6e 43 to an unixOpenC
cc210 6e 74 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 nt structure. T
cc220 68 65 72 65 20 69 73 20 6f 6e 65 20 75 6e 69 78 here is one unix
cc230 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 OpenCnt structur
cc240 65 0a 2a 2a 20 70 65 72 20 6f 70 65 6e 20 69 6e e.** per open in
cc250 6f 64 65 2c 20 77 68 69 63 68 20 6d 65 61 6e 73 ode, which means
cc260 20 74 68 61 74 20 6d 75 6c 74 69 70 6c 65 20 75 that multiple u
cc270 6e 69 78 46 69 6c 65 20 63 61 6e 20 70 6f 69 6e nixFile can poin
cc280 74 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a t to a single.**
cc290 20 75 6e 69 78 4f 70 65 6e 43 6e 74 2e 20 20 57 unixOpenCnt. W
cc2a0 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 hen an attempt i
cc2b0 73 20 6d 61 64 65 20 74 6f 20 63 6c 6f 73 65 20 s made to close
cc2c0 61 6e 20 75 6e 69 78 46 69 6c 65 2c 20 69 66 20 an unixFile, if
cc2d0 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 6f 74 68 there are.** oth
cc2e0 65 72 20 75 6e 69 78 46 69 6c 65 20 6f 70 65 6e er unixFile open
cc2f0 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 69 6e 6f on the same ino
cc300 64 65 20 74 68 61 74 20 61 72 65 20 68 6f 6c 64 de that are hold
cc310 69 6e 67 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 ing locks, the c
cc320 61 6c 6c 0a 2a 2a 20 74 6f 20 63 6c 6f 73 65 28 all.** to close(
cc330 29 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 ) the file descr
cc340 69 70 74 6f 72 20 69 73 20 64 65 66 65 72 72 65 iptor is deferre
cc350 64 20 75 6e 74 69 6c 20 61 6c 6c 20 6f 66 20 74 d until all of t
cc360 68 65 20 6c 6f 63 6b 73 20 63 6c 65 61 72 2e 0a he locks clear..
cc370 2a 2a 20 54 68 65 20 75 6e 69 78 4f 70 65 6e 43 ** The unixOpenC
cc380 6e 74 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 nt structure kee
cc390 70 73 20 61 20 6c 69 73 74 20 6f 66 20 66 69 6c ps a list of fil
cc3a0 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 74 68 e descriptors th
cc3b0 61 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 62 65 at need to.** be
cc3c0 20 63 6c 6f 73 65 64 20 61 6e 64 20 74 68 61 74 closed and that
cc3d0 20 6c 69 73 74 20 69 73 20 77 61 6c 6b 65 64 20 list is walked
cc3e0 28 61 6e 64 20 63 6c 65 61 72 65 64 29 20 77 68 (and cleared) wh
cc3f0 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b en the last lock
cc400 0a 2a 2a 20 63 6c 65 61 72 73 2e 0a 2a 2a 0a 2a .** clears..**.*
cc410 2a 20 59 65 74 20 61 6e 6f 74 68 65 72 20 70 72 * Yet another pr
cc420 6f 62 6c 65 6d 3a 20 20 4c 69 6e 75 78 54 68 72 oblem: LinuxThr
cc430 65 61 64 73 20 64 6f 20 6e 6f 74 20 70 6c 61 79 eads do not play
cc440 20 77 65 6c 6c 20 77 69 74 68 20 70 6f 73 69 78 well with posix
cc450 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4d 61 locks..**.** Ma
cc460 6e 79 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e ny older version
cc470 73 20 6f 66 20 6c 69 6e 75 78 20 75 73 65 20 74 s of linux use t
cc480 68 65 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 he LinuxThreads
cc490 6c 69 62 72 61 72 79 20 77 68 69 63 68 20 69 73 library which is
cc4a0 0a 2a 2a 20 6e 6f 74 20 70 6f 73 69 78 20 63 6f .** not posix co
cc4b0 6d 70 6c 69 61 6e 74 2e 20 20 55 6e 64 65 72 20 mpliant. Under
cc4c0 4c 69 6e 75 78 54 68 72 65 61 64 73 2c 20 61 20 LinuxThreads, a
cc4d0 6c 6f 63 6b 20 63 72 65 61 74 65 64 20 62 79 20 lock created by
cc4e0 74 68 72 65 61 64 0a 2a 2a 20 41 20 63 61 6e 6e thread.** A cann
cc4f0 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6f ot be modified o
cc500 72 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 r overridden by
cc510 61 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 a different thre
cc520 61 64 20 42 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 68 ad B..** Only th
cc530 72 65 61 64 20 41 20 63 61 6e 20 6d 6f 64 69 66 read A can modif
cc540 79 20 74 68 65 20 6c 6f 63 6b 2e 20 20 4c 6f 63 y the lock. Loc
cc550 6b 69 6e 67 20 62 65 68 61 76 69 6f 72 20 69 73 king behavior is
cc560 20 63 6f 72 72 65 63 74 0a 2a 2a 20 69 66 20 74 correct.** if t
cc570 68 65 20 61 70 70 6c 69 61 74 69 6f 6e 20 75 73 he appliation us
cc580 65 73 20 74 68 65 20 6e 65 77 65 72 20 4e 61 74 es the newer Nat
cc590 69 76 65 20 50 6f 73 69 78 20 54 68 72 65 61 64 ive Posix Thread
cc5a0 20 4c 69 62 72 61 72 79 20 28 4e 50 54 4c 29 0a Library (NPTL).
cc5b0 2a 2a 20 6f 6e 20 6c 69 6e 75 78 20 2d 20 77 69 ** on linux - wi
cc5c0 74 68 20 4e 50 54 4c 20 61 20 6c 6f 63 6b 20 63 th NPTL a lock c
cc5d0 72 65 61 74 65 64 20 62 79 20 74 68 72 65 61 64 reated by thread
cc5e0 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 A can override
cc5f0 6c 6f 63 6b 73 0a 2a 2a 20 69 6e 20 74 68 72 65 locks.** in thre
cc600 61 64 20 42 2e 20 20 42 75 74 20 74 68 65 72 65 ad B. But there
cc610 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e is no way to kn
cc620 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 ow at compile-ti
cc630 6d 65 20 77 68 69 63 68 0a 2a 2a 20 74 68 72 65 me which.** thre
cc640 61 64 69 6e 67 20 6c 69 62 72 61 72 79 20 69 73 ading library is
cc650 20 62 65 69 6e 67 20 75 73 65 64 2e 20 20 53 6f being used. So
cc660 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 there is no way
cc670 20 74 6f 20 6b 6e 6f 77 20 61 74 0a 2a 2a 20 63 to know at.** c
cc680 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 65 74 ompile-time whet
cc690 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 her or not threa
cc6a0 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65 d A can override
cc6b0 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 72 65 61 64 locks on thread
cc6c0 20 42 2e 0a 2a 2a 20 57 65 20 68 61 76 65 20 74 B..** We have t
cc6d0 6f 20 64 6f 20 61 20 72 75 6e 2d 74 69 6d 65 20 o do a run-time
cc6e0 63 68 65 63 6b 20 74 6f 20 64 69 73 63 6f 76 65 check to discove
cc6f0 72 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f r the behavior o
cc700 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 f the.** current
cc710 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 process..**.**
cc720 4f 6e 20 73 79 73 74 65 6d 73 20 77 68 65 72 65 On systems where
cc730 20 74 68 72 65 61 64 20 41 20 69 73 20 75 6e 61 thread A is una
cc740 62 6c 65 20 74 6f 20 6d 6f 64 69 66 79 20 6c 6f ble to modify lo
cc750 63 6b 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a cks created by.*
cc760 2a 20 74 68 72 65 61 64 20 42 2c 20 77 65 20 68 * thread B, we h
cc770 61 76 65 20 74 6f 20 6b 65 65 70 20 74 72 61 63 ave to keep trac
cc780 6b 20 6f 66 20 77 68 69 63 68 20 74 68 72 65 61 k of which threa
cc790 64 20 63 72 65 61 74 65 64 20 65 61 63 68 0a 2a d created each.*
cc7a0 2a 20 6c 6f 63 6b 2e 20 20 48 65 6e 63 65 20 74 * lock. Hence t
cc7b0 68 65 72 65 20 69 73 20 61 6e 20 65 78 74 72 61 here is an extra
cc7c0 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20 6b 65 field in the ke
cc7d0 79 20 74 6f 20 74 68 65 20 75 6e 69 78 4c 6f 63 y to the unixLoc
cc7e0 6b 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 kInfo.** structu
cc7f0 72 65 20 74 6f 20 72 65 63 6f 72 64 20 74 68 69 re to record thi
cc800 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 s information.
cc810 41 6e 64 20 6f 6e 20 74 68 6f 73 65 20 73 79 73 And on those sys
cc820 74 65 6d 73 20 69 74 0a 2a 2a 20 69 73 20 69 6c tems it.** is il
cc830 6c 65 67 61 6c 20 74 6f 20 62 65 67 69 6e 20 61 legal to begin a
cc840 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 transaction in
cc850 6f 6e 65 20 74 68 72 65 61 64 20 61 6e 64 20 66 one thread and f
cc860 69 6e 69 73 68 20 69 74 0a 2a 2a 20 69 6e 20 61 inish it.** in a
cc870 6e 6f 74 68 65 72 2e 20 20 46 6f 72 20 74 68 69 nother. For thi
cc880 73 20 6c 61 74 74 65 72 20 72 65 73 74 72 69 63 s latter restric
cc890 74 69 6f 6e 2c 20 74 68 65 72 65 20 69 73 20 6e tion, there is n
cc8a0 6f 20 77 6f 72 6b 2d 61 72 6f 75 6e 64 2e 0a 2a o work-around..*
cc8b0 2a 20 49 74 20 69 73 20 61 20 6c 69 6d 69 74 61 * It is a limita
cc8c0 74 69 6f 6e 20 6f 66 20 4c 69 6e 75 78 54 68 72 tion of LinuxThr
cc8d0 65 61 64 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 eads..*/../*.**
cc8e0 53 65 74 20 6f 72 20 63 68 65 63 6b 20 74 68 65 Set or check the
cc8f0 20 75 6e 69 78 46 69 6c 65 2e 74 69 64 20 66 69 unixFile.tid fi
cc900 65 6c 64 2e 20 20 54 68 69 73 20 66 69 65 6c 64 eld. This field
cc910 20 69 73 20 73 65 74 20 77 68 65 6e 20 61 6e 20 is set when an
cc920 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 69 73 20 66 unixFile.** is f
cc930 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 20 41 6c irst opened. Al
cc940 6c 20 73 75 62 73 65 71 75 65 6e 74 20 75 73 65 l subsequent use
cc950 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c s of the unixFil
cc960 65 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68 e verify that th
cc970 65 0a 2a 2a 20 73 61 6d 65 20 74 68 72 65 61 64 e.** same thread
cc980 20 69 73 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e is operating on
cc990 20 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 20 20 the unixFile.
cc9a0 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73 Some operating s
cc9b0 79 73 74 65 6d 73 20 64 6f 0a 2a 2a 20 6e 6f 74 ystems do.** not
cc9c0 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73 20 74 6f 20 allow locks to
cc9d0 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 be overridden by
cc9e0 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 61 other threads a
cc9f0 6e 64 20 74 68 61 74 20 72 65 73 74 72 69 63 74 nd that restrict
cca00 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 ion.** means tha
cca10 74 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 t sqlite3* datab
cca20 61 73 65 20 68 61 6e 64 6c 65 73 20 63 61 6e 6e ase handles cann
cca30 6f 74 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d ot be moved from
cca40 20 6f 6e 65 20 74 68 72 65 61 64 0a 2a 2a 20 74 one thread.** t
cca50 6f 20 61 6e 6f 74 68 65 72 20 77 68 69 6c 65 20 o another while
cca60 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e 0a locks are held..
cca70 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 33 2e **.** Version 3.
cca80 33 2e 31 20 28 32 30 30 36 2d 30 31 2d 31 35 29 3.1 (2006-01-15)
cca90 3a 20 20 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 : unixFile can
ccaa0 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e be moved from on
ccab0 65 20 74 68 72 65 61 64 20 74 6f 0a 2a 2a 20 61 e thread to.** a
ccac0 6e 6f 74 68 65 72 20 61 73 20 6c 6f 6e 67 20 61 nother as long a
ccad0 73 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 s we are running
ccae0 20 6f 6e 20 61 20 73 79 73 74 65 6d 20 74 68 61 on a system tha
ccaf0 74 20 73 75 70 70 6f 72 74 73 20 74 68 72 65 61 t supports threa
ccb00 64 73 0a 2a 2a 20 6f 76 65 72 72 69 64 69 6e 67 ds.** overriding
ccb10 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 each others loc
ccb20 6b 73 20 28 77 68 69 63 68 20 69 73 20 6e 6f 77 ks (which is now
ccb30 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e the most common
ccb40 20 62 65 68 61 76 69 6f 72 29 0a 2a 2a 20 6f 72 behavior).** or
ccb50 20 69 66 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65 if no locks are
ccb60 20 68 65 6c 64 2e 20 20 42 75 74 20 74 68 65 20 held. But the
ccb70 75 6e 69 78 46 69 6c 65 2e 70 4c 6f 63 6b 20 66 unixFile.pLock f
ccb80 69 65 6c 64 20 6e 65 65 64 73 20 74 6f 20 62 65 ield needs to be
ccb90 0a 2a 2a 20 72 65 63 6f 6d 70 75 74 65 64 20 62 .** recomputed b
ccba0 65 63 61 75 73 65 20 69 74 73 20 6b 65 79 20 69 ecause its key i
ccbb0 6e 63 6c 75 64 65 73 20 74 68 65 20 74 68 72 65 ncludes the thre
ccbc0 61 64 2d 69 64 2e 20 20 53 65 65 20 74 68 65 20 ad-id. See the
ccbd0 0a 2a 2a 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 .** transferOwne
ccbe0 72 73 68 69 70 28 29 20 66 75 6e 63 74 69 6f 6e rship() function
ccbf0 20 62 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 below for addit
ccc00 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
ccc10 6e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f n.*/.#if SQLITE_
ccc20 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 THREADSAFE && de
ccc30 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 fined(__linux__)
ccc40 0a 23 20 64 65 66 69 6e 65 20 53 45 54 5f 54 48 .# define SET_TH
ccc50 52 45 41 44 49 44 28 58 29 20 20 20 28 58 29 2d READID(X) (X)-
ccc60 3e 74 69 64 20 3d 20 70 74 68 72 65 61 64 5f 73 >tid = pthread_s
ccc70 65 6c 66 28 29 0a 23 20 64 65 66 69 6e 65 20 43 elf().# define C
ccc80 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 58 29 HECK_THREADID(X)
ccc90 20 28 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 (threadsOverrid
ccca0 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 eEachOthersLocks
cccb0 3d 3d 30 20 26 26 20 5c 0a 20 20 20 20 20 20 20 ==0 && \.
cccc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cccd0 20 20 20 20 20 21 70 74 68 72 65 61 64 5f 65 71 !pthread_eq
ccce0 75 61 6c 28 28 58 29 2d 3e 74 69 64 2c 20 70 74 ual((X)->tid, pt
cccf0 68 72 65 61 64 5f 73 65 6c 66 28 29 29 29 0a 23 hread_self())).#
ccd00 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 45 else.# define SE
ccd10 54 5f 54 48 52 45 41 44 49 44 28 58 29 0a 23 20 T_THREADID(X).#
ccd20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 54 48 52 define CHECK_THR
ccd30 45 41 44 49 44 28 58 29 20 30 0a 23 65 6e 64 69 EADID(X) 0.#endi
ccd40 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 f../*.** An inst
ccd50 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
ccd60 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
ccd70 73 65 72 76 65 73 20 61 73 20 74 68 65 20 6b 65 serves as the ke
ccd80 79 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 y used.** to loc
ccd90 61 74 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 ate a particular
ccda0 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 unixOpenCnt str
ccdb0 75 63 74 75 72 65 20 67 69 76 65 6e 20 69 74 73 ucture given its
ccdc0 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 0a 2a 2a inode. This.**
ccdd0 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 is the same as
ccde0 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 the unixLockKey
ccdf0 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 except that the
cce00 74 68 72 65 61 64 20 49 44 20 69 73 20 6f 6d 69 thread ID is omi
cce10 74 74 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 tted..*/.struct
cce20 75 6e 69 78 46 69 6c 65 49 64 20 7b 0a 20 20 64 unixFileId {. d
cce30 65 76 5f 74 20 64 65 76 3b 20 20 20 20 20 20 20 ev_t dev;
cce40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 /* De
cce50 76 69 63 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 vice number */.#
cce60 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if OS_VXWORKS.
cce70 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 struct vxworksFi
cce80 6c 65 49 64 20 2a 70 49 64 3b 20 20 2f 2a 20 55 leId *pId; /* U
cce90 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 66 6f nique file ID fo
ccea0 72 20 76 78 77 6f 72 6b 73 2e 20 2a 2f 0a 23 65 r vxworks. */.#e
cceb0 6c 73 65 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b lse. ino_t ino;
ccec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cced0 20 20 2f 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65 /* Inode numbe
ccee0 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a r */.#endif.};..
ccef0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
ccf00 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
ccf10 6e 67 20 73 74 72 75 63 74 75 72 65 20 73 65 72 ng structure ser
ccf20 76 65 73 20 61 73 20 74 68 65 20 6b 65 79 20 75 ves as the key u
ccf30 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 sed.** to locate
ccf40 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 75 6e a particular un
ccf50 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 ixLockInfo struc
ccf60 74 75 72 65 20 67 69 76 65 6e 20 69 74 73 20 69 ture given its i
ccf70 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 node..**.** If t
ccf80 68 72 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 hreads cannot ov
ccf90 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 erride each othe
ccfa0 72 73 20 6c 6f 63 6b 73 20 28 4c 69 6e 75 78 54 rs locks (LinuxT
ccfb0 68 72 65 61 64 73 29 2c 20 74 68 65 6e 20 77 65 hreads), then we
ccfc0 0a 2a 2a 20 73 65 74 20 74 68 65 20 75 6e 69 78 .** set the unix
ccfd0 4c 6f 63 6b 4b 65 79 2e 74 69 64 20 66 69 65 6c LockKey.tid fiel
ccfe0 64 20 74 6f 20 74 68 65 20 74 68 72 65 61 64 20 d to the thread
ccff0 49 44 2e 20 20 49 66 20 74 68 72 65 61 64 73 20 ID. If threads
cd000 63 61 6e 20 6f 76 65 72 72 69 64 65 0a 2a 2a 20 can override.**
cd010 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b each others lock
cd020 73 20 28 50 6f 73 69 78 20 61 6e 64 20 4e 50 54 s (Posix and NPT
cd030 4c 29 20 74 68 65 6e 20 74 69 64 20 69 73 20 61 L) then tid is a
cd040 6c 77 61 79 73 20 73 65 74 20 74 6f 20 7a 65 72 lways set to zer
cd050 6f 2e 0a 2a 2a 20 74 69 64 20 69 73 20 6f 6d 69 o..** tid is omi
cd060 74 74 65 64 20 69 66 20 77 65 20 63 6f 6d 70 69 tted if we compi
cd070 6c 65 20 77 69 74 68 6f 75 74 20 74 68 72 65 61 le without threa
cd080 64 69 6e 67 20 73 75 70 70 6f 72 74 20 6f 72 20 ding support or
cd090 6f 6e 20 61 6e 20 4f 53 0a 2a 2a 20 6f 74 68 65 on an OS.** othe
cd0a0 72 20 74 68 61 6e 20 6c 69 6e 75 78 2e 0a 2a 2f r than linux..*/
cd0b0 0a 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b .struct unixLock
cd0c0 4b 65 79 20 7b 0a 20 20 73 74 72 75 63 74 20 75 Key {. struct u
cd0d0 6e 69 78 46 69 6c 65 49 64 20 66 69 64 3b 20 20 nixFileId fid;
cd0e0 2f 2a 20 55 6e 69 71 75 65 20 69 64 65 6e 74 69 /* Unique identi
cd0f0 66 69 65 72 20 66 6f 72 20 74 68 65 20 66 69 6c fier for the fil
cd100 65 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f e */.#if SQLITE_
cd110 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 THREADSAFE && de
cd120 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 fined(__linux__)
cd130 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 . pthread_t tid
cd140 3b 20 20 2f 2a 20 54 68 72 65 61 64 20 49 44 20 ; /* Thread ID
cd150 6f 66 20 6c 6f 63 6b 20 6f 77 6e 65 72 2e 20 5a of lock owner. Z
cd160 65 72 6f 20 69 66 20 6e 6f 74 20 75 73 69 6e 67 ero if not using
cd170 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 2a 2f LinuxThreads */
cd180 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a .#endif.};../*.*
cd190 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
cd1a0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
cd1b0 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f tructure is allo
cd1c0 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 6f cated for each o
cd1d0 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 4f pen.** inode. O
cd1e0 72 2c 20 6f 6e 20 4c 69 6e 75 78 54 68 72 65 61 r, on LinuxThrea
cd1f0 64 73 2c 20 74 68 65 72 65 20 69 73 20 6f 6e 65 ds, there is one
cd200 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74 of these struct
cd210 75 72 65 73 20 66 6f 72 0a 2a 2a 20 65 61 63 68 ures for.** each
cd220 20 69 6e 6f 64 65 20 6f 70 65 6e 65 64 20 62 79 inode opened by
cd230 20 65 61 63 68 20 74 68 72 65 61 64 2e 0a 2a 2a each thread..**
cd240 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 6f .** A single ino
cd250 64 65 20 63 61 6e 20 68 61 76 65 20 6d 75 6c 74 de can have mult
cd260 69 70 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 iple file descri
cd270 70 74 6f 72 73 2c 20 73 6f 20 65 61 63 68 20 75 ptors, so each u
cd280 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75 63 nixFile.** struc
cd290 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 ture contains a
cd2a0 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e pointer to an in
cd2b0 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f stance of this o
cd2c0 62 6a 65 63 74 20 61 6e 64 20 74 68 69 73 0a 2a bject and this.*
cd2d0 2a 20 6f 62 6a 65 63 74 20 6b 65 65 70 73 20 61 * object keeps a
cd2e0 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 count of the nu
cd2f0 6d 62 65 72 20 6f 66 20 75 6e 69 78 46 69 6c 65 mber of unixFile
cd300 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 74 2e pointing to it.
cd310 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4c .*/.struct unixL
cd320 6f 63 6b 49 6e 66 6f 20 7b 0a 20 20 73 74 72 75 ockInfo {. stru
cd330 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c ct unixLockKey l
cd340 6f 63 6b 4b 65 79 3b 20 20 20 20 20 2f 2a 20 54 ockKey; /* T
cd350 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f he lookup key */
cd360 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 . int cnt;
cd370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cd380 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
cd390 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 68 65 6c SHARED locks hel
cd3a0 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 63 6b 74 d */. int lockt
cd3b0 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ype;
cd3c0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 /* One of
cd3d0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 SHARED_LOCK, RE
cd3e0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 65 74 63 2e SERVED_LOCK etc.
cd3f0 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 */. int nRef;
cd400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cd410 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
cd420 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 of pointers to t
cd430 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f his structure */
cd440 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f . struct unixLo
cd450 63 6b 49 6e 66 6f 20 2a 70 4e 65 78 74 3b 20 20 ckInfo *pNext;
cd460 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c /* List of al
cd470 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 6f l unixLockInfo o
cd480 62 6a 65 63 74 73 20 2a 2f 0a 20 20 73 74 72 75 bjects */. stru
cd490 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 ct unixLockInfo
cd4a0 2a 70 50 72 65 76 3b 20 20 20 20 20 2f 2a 20 20 *pPrev; /*
cd4b0 20 20 2e 2e 2e 2e 20 64 6f 75 62 6c 79 20 6c 69 .... doubly li
cd4c0 6e 6b 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a nked */.};../*.*
cd4d0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
cd4e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
cd4f0 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f tructure is allo
cd500 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 6f cated for each o
cd510 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 54 pen.** inode. T
cd520 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6b 65 his structure ke
cd530 65 70 73 20 74 72 61 63 6b 20 6f 66 20 74 68 65 eps track of the
cd540 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 number of locks
cd550 20 6f 6e 20 74 68 61 74 0a 2a 2a 20 69 6e 6f 64 on that.** inod
cd560 65 2e 20 20 49 66 20 61 20 63 6c 6f 73 65 20 69 e. If a close i
cd570 73 20 61 74 74 65 6d 70 74 65 64 20 61 67 61 69 s attempted agai
cd580 6e 73 74 20 61 6e 20 69 6e 6f 64 65 20 74 68 61 nst an inode tha
cd590 74 20 69 73 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 t is holding.**
cd5a0 6c 6f 63 6b 73 2c 20 74 68 65 20 63 6c 6f 73 65 locks, the close
cd5b0 20 69 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 is deferred unt
cd5c0 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 63 6c 65 il all locks cle
cd5d0 61 72 20 62 79 20 61 64 64 69 6e 67 20 74 68 65 ar by adding the
cd5e0 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 .** file descrip
cd5f0 74 6f 72 20 74 6f 20 62 65 20 63 6c 6f 73 65 64 tor to be closed
cd600 20 74 6f 20 74 68 65 20 70 65 6e 64 69 6e 67 20 to the pending
cd610 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f list..**.** TODO
cd620 3a 20 20 43 6f 6e 73 69 64 65 72 20 63 68 61 6e : Consider chan
cd630 67 69 6e 67 20 74 68 69 73 20 73 6f 20 74 68 61 ging this so tha
cd640 74 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 t there is only
cd650 61 20 73 69 6e 67 6c 65 20 66 69 6c 65 0a 2a 2a a single file.**
cd660 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 descriptor for
cd670 65 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 2c 20 each open file,
cd680 65 76 65 6e 20 77 68 65 6e 20 69 74 20 69 73 20 even when it is
cd690 6f 70 65 6e 65 64 20 6d 75 6c 74 69 70 6c 65 20 opened multiple
cd6a0 74 69 6d 65 73 2e 0a 2a 2a 20 54 68 65 20 63 6c times..** The cl
cd6b0 6f 73 65 28 29 20 73 79 73 74 65 6d 20 63 61 6c ose() system cal
cd6c0 6c 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 6f 63 63 l would only occ
cd6d0 75 72 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 ur when the last
cd6e0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 75 73 69 database.** usi
cd6f0 6e 67 20 74 68 65 20 66 69 6c 65 20 63 6c 6f 73 ng the file clos
cd700 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e es..*/.struct un
cd710 69 78 4f 70 65 6e 43 6e 74 20 7b 0a 20 20 73 74 ixOpenCnt {. st
cd720 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 ruct unixFileId
cd730 66 69 6c 65 49 64 3b 20 20 20 2f 2a 20 54 68 65 fileId; /* The
cd740 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 lookup key */.
cd750 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 int nRef;
cd760 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
cd770 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 Number of pointe
cd780 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 rs to this struc
cd790 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c ture */. int nL
cd7a0 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 ock;
cd7b0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
cd7c0 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c of outstanding l
cd7d0 6f 63 6b 73 20 2a 2f 0a 20 20 55 6e 69 78 55 6e ocks */. UnixUn
cd7e0 75 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64 3b usedFd *pUnused;
cd7f0 20 20 20 20 20 20 2f 2a 20 55 6e 75 73 65 64 20 /* Unused
cd800 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 file descriptors
cd810 20 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a 23 69 66 to close */.#if
cd820 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 65 OS_VXWORKS. se
cd830 6d 5f 74 20 2a 70 53 65 6d 3b 20 20 20 20 20 20 m_t *pSem;
cd840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
cd850 2a 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 73 65 * Named POSIX se
cd860 6d 61 70 68 6f 72 65 20 2a 2f 0a 20 20 63 68 61 maphore */. cha
cd870 72 20 61 53 65 6d 4e 61 6d 65 5b 4d 41 58 5f 50 r aSemName[MAX_P
cd880 41 54 48 4e 41 4d 45 2b 32 5d 3b 20 20 20 2f 2a ATHNAME+2]; /*
cd890 20 4e 61 6d 65 20 6f 66 20 74 68 61 74 20 73 65 Name of that se
cd8a0 6d 61 70 68 6f 72 65 20 2a 2f 0a 23 65 6e 64 69 maphore */.#endi
cd8b0 66 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f f. struct unixO
cd8c0 70 65 6e 43 6e 74 20 2a 70 4e 65 78 74 2c 20 2a penCnt *pNext, *
cd8d0 70 50 72 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74 pPrev; /* List
cd8e0 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4f 70 65 6e of all unixOpen
cd8f0 43 6e 74 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d Cnt objects */.}
cd900 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74 73 20 6f ;../*.** Lists o
cd910 66 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e f all unixLockIn
cd920 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43 fo and unixOpenC
cd930 6e 74 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 nt objects. The
cd940 73 65 20 75 73 65 64 20 74 6f 20 62 65 20 68 61 se used to be ha
cd950 73 68 0a 2a 2a 20 74 61 62 6c 65 73 2e 20 20 42 sh.** tables. B
cd960 75 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ut the number of
cd970 20 6f 62 6a 65 63 74 73 20 69 73 20 72 61 72 65 objects is rare
cd980 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 64 ly more than a d
cd990 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e 65 76 65 ozen and.** neve
cd9a0 72 20 65 78 63 65 65 64 73 20 61 20 66 65 77 20 r exceeds a few
cd9b0 74 68 6f 75 73 61 6e 64 2e 20 20 41 6e 64 20 6c thousand. And l
cd9c0 6f 6f 6b 75 70 20 69 73 20 6e 6f 74 20 6f 6e 20 ookup is not on
cd9d0 61 20 63 72 69 74 69 63 61 6c 0a 2a 2a 20 70 61 a critical.** pa
cd9e0 74 68 20 73 6f 20 61 20 73 69 6d 70 6c 65 20 6c th so a simple l
cd9f0 69 6e 6b 65 64 20 6c 69 73 74 20 77 69 6c 6c 20 inked list will
cda00 73 75 66 66 69 63 65 2e 0a 2a 2f 0a 73 74 61 74 suffice..*/.stat
cda10 69 63 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f ic struct unixLo
cda20 63 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73 74 ckInfo *lockList
cda30 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 73 74 72 = 0;.static str
cda40 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 uct unixOpenCnt
cda50 2a 6f 70 65 6e 4c 69 73 74 20 3d 20 30 3b 0a 0a *openList = 0;..
cda60 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 61 72 69 61 /*.** This varia
cda70 62 6c 65 20 72 65 6d 65 6d 62 65 72 73 20 77 68 ble remembers wh
cda80 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 ether or not thr
cda90 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 eads can overrid
cdaa0 65 20 65 61 63 68 20 6f 74 68 65 72 73 0a 2a 2a e each others.**
cdab0 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 locks..**.**
cdac0 20 30 3a 20 20 4e 6f 2e 20 20 54 68 72 65 61 64 0: No. Thread
cdad0 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 s cannot overrid
cdae0 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f e each others lo
cdaf0 63 6b 73 2e 20 20 28 4c 69 6e 75 78 54 68 72 65 cks. (LinuxThre
cdb00 61 64 73 29 0a 2a 2a 20 20 20 20 31 3a 20 20 59 ads).** 1: Y
cdb10 65 73 2e 20 20 54 68 72 65 61 64 73 20 63 61 6e es. Threads can
cdb20 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f override each o
cdb30 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 28 50 thers locks. (P
cdb40 6f 73 69 78 20 26 20 4e 4c 50 54 29 0a 2a 2a 20 osix & NLPT).**
cdb50 20 20 2d 31 3a 20 20 57 65 20 64 6f 6e 27 74 20 -1: We don't
cdb60 6b 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a 2a 2a 20 know yet..**.**
cdb70 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c On some systems,
cdb80 20 77 65 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 we know at comp
cdb90 69 6c 65 2d 74 69 6d 65 20 69 66 20 74 68 72 65 ile-time if thre
cdba0 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 ads can override
cdbb0 20 65 61 63 68 0a 2a 2a 20 6f 74 68 65 72 73 20 each.** others
cdbc0 6c 6f 63 6b 73 2e 20 20 4f 6e 20 74 68 6f 73 65 locks. On those
cdbd0 20 73 79 73 74 65 6d 73 2c 20 74 68 65 20 53 51 systems, the SQ
cdbe0 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 LITE_THREAD_OVER
cdbf0 52 49 44 45 5f 4c 4f 43 4b 20 6d 61 63 72 6f 0a RIDE_LOCK macro.
cdc00 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65 74 20 61 ** will be set a
cdc10 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 20 4f ppropriately. O
cdc20 6e 20 6f 74 68 65 72 20 73 79 73 74 65 6d 73 2c n other systems,
cdc30 20 77 65 20 68 61 76 65 20 74 6f 20 63 68 65 63 we have to chec
cdc40 6b 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2e k at.** runtime.
cdc50 20 20 4f 6e 20 74 68 65 73 65 20 6c 61 74 74 65 On these latte
cdc60 72 20 73 79 73 74 65 6d 73 2c 20 53 51 4c 54 49 r systems, SQLTI
cdc70 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 E_THREAD_OVERRID
cdc80 45 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20 75 6e 64 E_LOCK is.** und
cdc90 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 efined..**.** Th
cdca0 69 73 20 76 61 72 69 61 62 6c 65 20 6e 6f 72 6d is variable norm
cdcb0 61 6c 6c 79 20 68 61 73 20 66 69 6c 65 20 73 63 ally has file sc
cdcc0 6f 70 65 20 6f 6e 6c 79 2e 20 20 42 75 74 20 64 ope only. But d
cdcd0 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 77 uring testing, w
cdce0 65 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 61 20 67 e make.** it a g
cdcf0 6c 6f 62 61 6c 20 73 6f 20 74 68 61 74 20 74 68 lobal so that th
cdd00 65 20 74 65 73 74 20 63 6f 64 65 20 63 61 6e 20 e test code can
cdd10 63 68 61 6e 67 65 20 69 74 73 20 76 61 6c 75 65 change its value
cdd20 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 76 65 72 in order to ver
cdd30 69 66 79 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 ify.** that the
cdd40 72 69 67 68 74 20 73 74 75 66 66 20 68 61 70 70 right stuff happ
cdd50 65 6e 73 20 69 6e 20 65 69 74 68 65 72 20 63 61 ens in either ca
cdd60 73 65 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 se..*/.#if SQLIT
cdd70 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
cdd80 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f defined(__linux_
cdd90 5f 29 0a 23 20 20 69 66 6e 64 65 66 20 53 51 4c _).# ifndef SQL
cdda0 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 ITE_THREAD_OVERR
cddb0 49 44 45 5f 4c 4f 43 4b 0a 23 20 20 20 20 64 65 IDE_LOCK.# de
cddc0 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 fine SQLITE_THRE
cddd0 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b AD_OVERRIDE_LOCK
cdde0 20 2d 31 0a 23 20 20 65 6e 64 69 66 0a 23 20 20 -1.# endif.#
cddf0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
cde00 54 0a 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 T.int threadsOve
cde10 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c rrideEachOthersL
cde20 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 ocks = SQLITE_TH
cde30 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f READ_OVERRIDE_LO
cde40 43 4b 3b 0a 23 20 20 65 6c 73 65 0a 73 74 61 74 CK;.# else.stat
cde50 69 63 20 69 6e 74 20 74 68 72 65 61 64 73 4f 76 ic int threadsOv
cde60 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 errideEachOthers
cde70 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 Locks = SQLITE_T
cde80 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c HREAD_OVERRIDE_L
cde90 4f 43 4b 3b 0a 23 20 20 65 6e 64 69 66 0a 23 65 OCK;.# endif.#e
cdea0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ndif../*.** This
cdeb0 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 structure holds
cdec0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 information pas
cded0 73 65 64 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 sed into individ
cdee0 75 61 6c 20 74 65 73 74 0a 2a 2a 20 74 68 72 65 ual test.** thre
cdef0 61 64 73 20 62 79 20 74 68 65 20 74 65 73 74 54 ads by the testT
cdf00 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 hreadLockingBeha
cdf10 76 69 6f 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a vior() routine..
cdf20 2a 2f 0a 73 74 72 75 63 74 20 74 68 72 65 61 64 */.struct thread
cdf30 54 65 73 74 44 61 74 61 20 7b 0a 20 20 69 6e 74 TestData {. int
cdf40 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 fd;
cdf50 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 62 /* File to b
cdf60 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 73 74 e locked */. st
cdf70 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b ruct flock lock;
cdf80 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b /* The lock
cdf90 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f ing operation */
cdfa0 0a 20 20 69 6e 74 20 72 65 73 75 6c 74 3b 20 20 . int result;
cdfb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
cdfc0 75 6c 74 20 6f 66 20 74 68 65 20 6c 6f 63 6b 69 ult of the locki
cdfd0 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a ng operation */.
cdfe0 7d 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 };..#if SQLITE_T
cdff0 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 HREADSAFE && def
ce000 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a ined(__linux__).
ce010 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
ce020 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73 20 74 ion is used as t
ce030 68 65 20 6d 61 69 6e 20 72 6f 75 74 69 6e 65 20 he main routine
ce040 66 6f 72 20 61 20 74 68 72 65 61 64 20 6c 61 75 for a thread lau
ce050 6e 63 68 65 64 20 62 79 0a 2a 2a 20 74 65 73 74 nched by.** test
ce060 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 ThreadLockingBeh
ce070 61 76 69 6f 72 28 29 2e 20 49 74 20 74 65 73 74 avior(). It test
ce080 73 20 77 68 65 74 68 65 72 20 74 68 65 20 73 68 s whether the sh
ce090 61 72 65 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e ared-lock obtain
ce0a0 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 6d 61 69 ed.** by the mai
ce0b0 6e 20 74 68 72 65 61 64 20 69 6e 20 74 65 73 74 n thread in test
ce0c0 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 ThreadLockingBeh
ce0d0 61 76 69 6f 72 28 29 20 63 6f 6e 66 6c 69 63 74 avior() conflict
ce0e0 73 20 77 69 74 68 20 61 0a 2a 2a 20 68 79 70 6f s with a.** hypo
ce0f0 74 68 65 74 69 63 61 6c 20 77 72 69 74 65 2d 6c thetical write-l
ce100 6f 63 6b 20 6f 62 74 61 69 6e 65 64 20 62 79 20 ock obtained by
ce110 74 68 69 73 20 74 68 72 65 61 64 20 6f 6e 20 74 this thread on t
ce120 68 65 20 73 61 6d 65 20 66 69 6c 65 2e 0a 2a 2a he same file..**
ce130 0a 2a 2a 20 54 68 65 20 77 72 69 74 65 2d 6c 6f .** The write-lo
ce140 63 6b 20 69 73 20 6e 6f 74 20 61 63 74 75 61 6c ck is not actual
ce150 6c 79 20 61 63 71 75 69 72 65 64 2c 20 61 73 20 ly acquired, as
ce160 74 68 69 73 20 69 73 20 6e 6f 74 20 70 6f 73 73 this is not poss
ce170 69 62 6c 65 20 69 66 20 0a 2a 2a 20 74 68 65 20 ible if .** the
ce180 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 69 6e 20 file is open in
ce190 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 28 read-only mode (
ce1a0 73 65 65 20 74 69 63 6b 65 74 20 23 33 34 37 32 see ticket #3472
ce1b0 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f )..*/ .static vo
ce1c0 69 64 20 2a 74 68 72 65 61 64 4c 6f 63 6b 69 6e id *threadLockin
ce1d0 67 54 65 73 74 28 76 6f 69 64 20 2a 70 41 72 67 gTest(void *pArg
ce1e0 29 7b 0a 20 20 73 74 72 75 63 74 20 74 68 72 65 ){. struct thre
ce1f0 61 64 54 65 73 74 44 61 74 61 20 2a 70 44 61 74 adTestData *pDat
ce200 61 20 3d 20 28 73 74 72 75 63 74 20 74 68 72 65 a = (struct thre
ce210 61 64 54 65 73 74 44 61 74 61 2a 29 70 41 72 67 adTestData*)pArg
ce220 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65 73 75 6c ;. pData->resul
ce230 74 20 3d 20 66 63 6e 74 6c 28 70 44 61 74 61 2d t = fcntl(pData-
ce240 3e 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 70 >fd, F_GETLK, &p
ce250 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20 72 Data->lock);. r
ce260 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d 0a 23 65 eturn pArg;.}.#e
ce270 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 ndif /* SQLITE_T
ce280 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 HREADSAFE && def
ce290 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 20 ined(__linux__)
ce2a0 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f */...#if SQLITE_
ce2b0 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 THREADSAFE && de
ce2c0 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 fined(__linux__)
ce2d0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 ./*.** This proc
ce2e0 65 64 75 72 65 20 61 74 74 65 6d 70 74 73 20 74 edure attempts t
ce2f0 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 o determine whet
ce300 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 her or not threa
ce310 64 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 72 72 69 ds.** can overri
ce320 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c de each others l
ce330 6f 63 6b 73 20 74 68 65 6e 20 73 65 74 73 20 74 ocks then sets t
ce340 68 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 4f 76 he .** threadsOv
ce350 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 errideEachOthers
ce360 4c 6f 63 6b 73 20 76 61 72 69 61 62 6c 65 20 61 Locks variable a
ce370 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f ppropriately..*/
ce380 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 65 73 .static void tes
ce390 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 tThreadLockingBe
ce3a0 68 61 76 69 6f 72 28 69 6e 74 20 66 64 5f 6f 72 havior(int fd_or
ce3b0 69 67 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 ig){. int fd;.
ce3c0 20 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63 int rc;. struc
ce3d0 74 20 74 68 72 65 61 64 54 65 73 74 44 61 74 61 t threadTestData
ce3e0 20 64 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f d;. struct flo
ce3f0 63 6b 20 6c 3b 0a 20 20 70 74 68 72 65 61 64 5f ck l;. pthread_
ce400 74 20 74 3b 0a 0a 20 20 66 64 20 3d 20 64 75 70 t t;.. fd = dup
ce410 28 66 64 5f 6f 72 69 67 29 3b 0a 20 20 69 66 28 (fd_orig);. if(
ce420 20 66 64 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a fd<0 ) return;.
ce430 20 20 6d 65 6d 73 65 74 28 26 6c 2c 20 30 2c 20 memset(&l, 0,
ce440 73 69 7a 65 6f 66 28 6c 29 29 3b 0a 20 20 6c 2e sizeof(l));. l.
ce450 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b l_type = F_RDLCK
ce460 3b 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d 20 31 3b ;. l.l_len = 1;
ce470 0a 20 20 6c 2e 6c 5f 73 74 61 72 74 20 3d 20 30 . l.l_start = 0
ce480 3b 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63 65 20 3d ;. l.l_whence =
ce490 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 72 63 20 SEEK_SET;. rc
ce4a0 3d 20 66 63 6e 74 6c 28 66 64 5f 6f 72 69 67 2c = fcntl(fd_orig,
ce4b0 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 29 3b 0a 20 F_SETLK, &l);.
ce4c0 20 69 66 28 20 72 63 21 3d 30 20 29 20 72 65 74 if( rc!=0 ) ret
ce4d0 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 urn;. memset(&d
ce4e0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 29 29 3b , 0, sizeof(d));
ce4f0 0a 20 20 64 2e 66 64 20 3d 20 66 64 3b 0a 20 20 . d.fd = fd;.
ce500 64 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20 20 64 2e d.lock = l;. d.
ce510 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f lock.l_type = F_
ce520 57 52 4c 43 4b 3b 0a 20 20 69 66 28 20 70 74 68 WRLCK;. if( pth
ce530 72 65 61 64 5f 63 72 65 61 74 65 28 26 74 2c 20 read_create(&t,
ce540 30 2c 20 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 0, threadLocking
ce550 54 65 73 74 2c 20 26 64 29 3d 3d 30 20 29 7b 0a Test, &d)==0 ){.
ce560 20 20 20 20 70 74 68 72 65 61 64 5f 6a 6f 69 6e pthread_join
ce570 28 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 63 6c (t, 0);. }. cl
ce580 6f 73 65 28 66 64 29 3b 0a 20 20 69 66 28 20 64 ose(fd);. if( d
ce590 2e 72 65 73 75 6c 74 21 3d 30 20 29 20 72 65 74 .result!=0 ) ret
ce5a0 75 72 6e 3b 0a 20 20 74 68 72 65 61 64 73 4f 76 urn;. threadsOv
ce5b0 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 errideEachOthers
ce5c0 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c 6f 63 6b 2e Locks = (d.lock.
ce5d0 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 29 l_type==F_UNLCK)
ce5e0 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
ce5f0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 LITE_THREADSAFE
ce600 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e && defined(__lin
ce610 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 ux__) */../*.**
ce620 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4c 6f Release a unixLo
ce630 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 ckInfo structure
ce640 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f previously allo
ce650 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f 63 cated by findLoc
ce660 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 kInfo()..**.** T
ce670 68 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65 64 he mutex entered
ce680 20 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78 45 using the unixE
ce690 6e 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e 63 nterMutex() func
ce6a0 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 6c tion must be hel
ce6b0 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 66 d.** when this f
ce6c0 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
ce6d0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
ce6e0 64 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 d releaseLockInf
ce6f0 6f 28 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 o(struct unixLoc
ce700 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 29 7b 0a 20 kInfo *pLock){.
ce710 20 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 assert( unixMut
ce720 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 20 69 66 exHeld() );. if
ce730 28 20 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 ( pLock ){. p
ce740 4c 6f 63 6b 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 Lock->nRef--;.
ce750 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 6e 52 65 if( pLock->nRe
ce760 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 f==0 ){. if
ce770 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 29 ( pLock->pPrev )
ce780 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
ce790 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e ( pLock->pPrev->
ce7a0 70 4e 65 78 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a pNext==pLock );.
ce7b0 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 pLock->p
ce7c0 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4c Prev->pNext = pL
ce7d0 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 ock->pNext;.
ce7e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
ce7f0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 4c 69 73 assert( lockLis
ce800 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 t==pLock );.
ce810 20 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 lockList = p
ce820 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 Lock->pNext;.
ce830 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 }. if( p
ce840 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 Lock->pNext ){.
ce850 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
ce860 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 Lock->pNext->pPr
ce870 65 76 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 ev==pLock );.
ce880 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 pLock->pNex
ce890 74 2d 3e 70 50 72 65 76 20 3d 20 70 4c 6f 63 6b t->pPrev = pLock
ce8a0 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d ->pPrev;. }
ce8b0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 . sqlite3_f
ce8c0 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 ree(pLock);.
ce8d0 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 }. }.}../*.** R
ce8e0 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4f 70 65 elease a unixOpe
ce8f0 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 70 nCnt structure p
ce900 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 reviously alloca
ce910 74 65 64 20 62 79 20 66 69 6e 64 4c 6f 63 6b 49 ted by findLockI
ce920 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 nfo()..**.** The
ce930 20 6d 75 74 65 78 20 65 6e 74 65 72 65 64 20 75 mutex entered u
ce940 73 69 6e 67 20 74 68 65 20 75 6e 69 78 45 6e 74 sing the unixEnt
ce950 65 72 4d 75 74 65 78 28 29 20 66 75 6e 63 74 69 erMutex() functi
ce960 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 6c 64 0a on must be held.
ce970 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e ** when this fun
ce980 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e ction is called.
ce990 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
ce9a0 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 73 releaseOpenCnt(s
ce9b0 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e truct unixOpenCn
ce9c0 74 20 2a 70 4f 70 65 6e 29 7b 0a 20 20 61 73 73 t *pOpen){. ass
ce9d0 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 65 ert( unixMutexHe
ce9e0 6c 64 28 29 20 29 3b 0a 20 20 69 66 28 20 70 4f ld() );. if( pO
ce9f0 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e pen ){. pOpen
cea00 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 ->nRef--;. if
cea10 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d 3d 30 ( pOpen->nRef==0
cea20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f ){. if( pO
cea30 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 pen->pPrev ){.
cea40 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
cea50 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 pen->pPrev->pNex
cea60 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 t==pOpen );.
cea70 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 pOpen->pPrev
cea80 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 6e 2d ->pNext = pOpen-
cea90 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 65 >pNext;. }e
ceaa0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 lse{. ass
ceab0 65 72 74 28 20 6f 70 65 6e 4c 69 73 74 3d 3d 70 ert( openList==p
ceac0 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 Open );.
cead0 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e openList = pOpen
ceae0 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d ->pNext;. }
ceaf0 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e . if( pOpen
ceb00 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 ->pNext ){.
ceb10 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 65 6e assert( pOpen
ceb20 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d ->pNext->pPrev==
ceb30 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 pOpen );.
ceb40 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 pOpen->pNext->p
ceb50 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d 3e 70 50 Prev = pOpen->pP
ceb60 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rev;. }.
ceb70 20 20 20 61 73 73 65 72 74 28 20 21 70 4f 70 65 assert( !pOpe
ceb80 6e 2d 3e 70 55 6e 75 73 65 64 20 29 3b 0a 20 20 n->pUnused );.
ceb90 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
ceba0 28 70 4f 70 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 (pOpen);. }.
cebb0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 }.}../*.** Give
cebc0 6e 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 n a file descrip
cebd0 74 6f 72 2c 20 6c 6f 63 61 74 65 20 75 6e 69 78 tor, locate unix
cebe0 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 75 6e 69 LockInfo and uni
cebf0 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 xOpenCnt structu
cec00 72 65 73 20 74 68 61 74 0a 2a 2a 20 64 65 73 63 res that.** desc
cec10 72 69 62 65 73 20 74 68 61 74 20 66 69 6c 65 20 ribes that file
cec20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43 72 65 descriptor. Cre
cec30 61 74 65 20 6e 65 77 20 6f 6e 65 73 20 69 66 20 ate new ones if
cec40 6e 65 63 65 73 73 61 72 79 2e 20 20 54 68 65 0a necessary. The.
cec50 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 ** return values
cec60 20 6d 69 67 68 74 20 62 65 20 75 6e 69 6e 69 74 might be uninit
cec70 69 61 6c 69 7a 65 64 20 69 66 20 61 6e 20 65 72 ialized if an er
cec80 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a ror occurs..**.*
cec90 2a 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65 * The mutex ente
ceca0 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e red using the un
cecb0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66 ixEnterMutex() f
cecc0 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 unction must be
cecd0 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 held.** when thi
cece0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
cecf0 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 lled..**.** Retu
ced00 72 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 rn an appropriat
ced10 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f e error code..*/
ced20 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6e 64 .static int find
ced30 4c 6f 63 6b 49 6e 66 6f 28 0a 20 20 75 6e 69 78 LockInfo(. unix
ced40 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 File *pFile,
ced50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e /* Un
ced60 69 78 20 66 69 6c 65 20 77 69 74 68 20 66 69 6c ix file with fil
ced70 65 20 64 65 73 63 20 75 73 65 64 20 69 6e 20 74 e desc used in t
ced80 68 65 20 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75 he key */. stru
ced90 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 ct unixLockInfo
ceda0 2a 2a 70 70 4c 6f 63 6b 2c 20 20 2f 2a 20 52 65 **ppLock, /* Re
cedb0 74 75 72 6e 20 74 68 65 20 75 6e 69 78 4c 6f 63 turn the unixLoc
cedc0 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 kInfo structure
cedd0 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 here */. struct
cede0 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 2a 70 unixOpenCnt **p
cedf0 70 4f 70 65 6e 20 20 20 20 2f 2a 20 52 65 74 75 pOpen /* Retu
cee00 72 6e 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 rn the unixOpenC
cee10 6e 74 20 73 74 72 75 63 74 75 72 65 20 68 65 72 nt structure her
cee20 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
cee30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
cee40 20 20 20 20 20 20 20 20 20 2f 2a 20 53 79 73 74 /* Syst
cee50 65 6d 20 63 61 6c 6c 20 72 65 74 75 72 6e 20 63 em call return c
cee60 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b ode */. int fd;
cee70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cee80 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 /* The f
cee90 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 ile descriptor f
ceea0 6f 72 20 70 46 69 6c 65 20 2a 2f 0a 20 20 73 74 or pFile */. st
ceeb0 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 ruct unixLockKey
ceec0 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 2f 2a 20 lockKey; /*
ceed0 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 74 Lookup key for t
ceee0 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 he unixLockInfo
ceef0 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 73 structure */. s
cef00 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 truct unixFileId
cef10 20 66 69 6c 65 49 64 3b 20 20 20 20 20 20 2f 2a fileId; /*
cef20 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 Lookup key for
cef30 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 the unixOpenCnt
cef40 73 74 72 75 63 74 20 2a 2f 0a 20 20 73 74 72 75 struct */. stru
cef50 63 74 20 73 74 61 74 20 73 74 61 74 62 75 66 3b ct stat statbuf;
cef60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
cef70 77 2d 6c 65 76 65 6c 20 66 69 6c 65 20 69 6e 66 w-level file inf
cef80 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 ormation */. st
cef90 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 ruct unixLockInf
cefa0 6f 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 o *pLock = 0;/*
cefb0 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 4c 6f Candidate unixLo
cefc0 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a 2f ckInfo object */
cefd0 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 . struct unixOp
cefe0 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 20 20 enCnt *pOpen;
ceff0 20 20 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 75 /* Candidate u
cf000 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62 6a 65 63 nixOpenCnt objec
cf010 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 t */.. assert(
cf020 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 29 20 unixMutexHeld()
cf030 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 6c 6f 77 );.. /* Get low
cf040 2d 6c 65 76 65 6c 20 69 6e 66 6f 72 6d 61 74 69 -level informati
cf050 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 69 6c on about the fil
cf060 65 20 74 68 61 74 20 77 65 20 63 61 6e 20 75 73 e that we can us
cf070 65 64 20 74 6f 0a 20 20 2a 2a 20 63 72 65 61 74 ed to. ** creat
cf080 65 20 61 20 75 6e 69 71 75 65 20 6e 61 6d 65 20 e a unique name
cf090 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 for the file..
cf0a0 2a 2f 0a 20 20 66 64 20 3d 20 70 46 69 6c 65 2d */. fd = pFile-
cf0b0 3e 68 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 74 >h;. rc = fstat
cf0c0 28 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a (fd, &statbuf);.
cf0d0 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 if( rc!=0 ){.
cf0e0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
cf0f0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 69 66 rno = errno;.#if
cf100 64 65 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20 def EOVERFLOW.
cf110 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 61 73 if( pFile->las
cf120 74 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f tErrno==EOVERFLO
cf130 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 W ) return SQLIT
cf140 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 66 0a E_NOLFS;.#endif.
cf150 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
cf160 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a 23 69 E_IOERR;. }..#i
cf170 66 64 65 66 20 5f 5f 41 50 50 4c 45 5f 5f 0a 20 fdef __APPLE__.
cf180 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20 61 /* On OS X on a
cf190 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74 n msdos filesyst
cf1a0 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 em, the inode nu
cf1b0 6d 62 65 72 20 69 73 20 72 65 70 6f 72 74 65 64 mber is reported
cf1c0 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74 6c . ** incorrectl
cf1d0 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65 20 y for zero-size
cf1e0 66 69 6c 65 73 2e 20 20 53 65 65 20 74 69 63 6b files. See tick
cf1f0 65 74 20 23 33 32 36 30 2e 20 20 54 6f 20 77 6f et #3260. To wo
cf200 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 74 rk. ** around t
cf210 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 77 65 20 his problem (we
cf220 63 6f 6e 73 69 64 65 72 20 69 74 20 61 20 62 75 consider it a bu
cf230 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20 53 g in OS X, not S
cf240 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65 20 61 QLite). ** we a
cf250 6c 77 61 79 73 20 69 6e 63 72 65 61 73 65 20 74 lways increase t
cf260 68 65 20 66 69 6c 65 20 73 69 7a 65 20 74 6f 20 he file size to
cf270 31 20 62 79 20 77 72 69 74 69 6e 67 20 61 20 73 1 by writing a s
cf280 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a 20 ingle byte. **
cf290 70 72 69 6f 72 20 74 6f 20 61 63 63 65 73 73 69 prior to accessi
cf2a0 6e 67 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d ng the inode num
cf2b0 62 65 72 2e 20 20 54 68 65 20 6f 6e 65 20 62 79 ber. The one by
cf2c0 74 65 20 77 72 69 74 74 65 6e 20 69 73 0a 20 20 te written is.
cf2d0 2a 2a 20 61 6e 20 41 53 43 49 49 20 27 53 27 20 ** an ASCII 'S'
cf2e0 63 68 61 72 61 63 74 65 72 20 77 68 69 63 68 20 character which
cf2f0 61 6c 73 6f 20 68 61 70 70 65 6e 73 20 74 6f 20 also happens to
cf300 62 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74 be the first byt
cf310 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 68 65 e. ** in the he
cf320 61 64 65 72 20 6f 66 20 65 76 65 72 79 20 53 51 ader of every SQ
cf330 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e 20 20 Lite database.
cf340 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69 66 20 In this way, if
cf350 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20 there. ** is a
cf360 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73 race condition s
cf370 75 63 68 20 74 68 61 74 20 61 6e 6f 74 68 65 72 uch that another
cf380 20 74 68 72 65 61 64 20 68 61 73 20 61 6c 72 65 thread has alre
cf390 61 64 79 20 70 6f 70 75 6c 61 74 65 64 0a 20 20 ady populated.
cf3a0 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70 61 67 ** the first pag
cf3b0 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
cf3c0 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69 73 20 e, no damage is
cf3d0 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 done.. */. if(
cf3e0 20 73 74 61 74 62 75 66 2e 73 74 5f 73 69 7a 65 statbuf.st_size
cf3f0 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ==0 ){. rc =
cf400 77 72 69 74 65 28 66 64 2c 20 22 53 22 2c 20 31 write(fd, "S", 1
cf410 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 31 );. if( rc!=1
cf420 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
cf430 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 SQLITE_IOERR;.
cf440 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 73 }. rc = fs
cf450 74 61 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 tat(fd, &statbuf
cf460 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 30 );. if( rc!=0
cf470 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d ){. pFile-
cf480 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
cf490 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e no;. return
cf4a0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 SQLITE_IOERR;.
cf4b0 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
cf4c0 0a 20 20 6d 65 6d 73 65 74 28 26 6c 6f 63 6b 4b . memset(&lockK
cf4d0 65 79 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6c 6f ey, 0, sizeof(lo
cf4e0 63 6b 4b 65 79 29 29 3b 0a 20 20 6c 6f 63 6b 4b ckKey));. lockK
cf4f0 65 79 2e 66 69 64 2e 64 65 76 20 3d 20 73 74 61 ey.fid.dev = sta
cf500 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23 69 66 tbuf.st_dev;.#if
cf510 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 6c 6f OS_VXWORKS. lo
cf520 63 6b 4b 65 79 2e 66 69 64 2e 70 49 64 20 3d 20 ckKey.fid.pId =
cf530 70 46 69 6c 65 2d 3e 70 49 64 3b 0a 23 65 6c 73 pFile->pId;.#els
cf540 65 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e e. lockKey.fid.
cf550 69 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e 73 74 ino = statbuf.st
cf560 5f 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23 69 66 _ino;.#endif.#if
cf570 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
cf580 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f FE && defined(__
cf590 6c 69 6e 75 78 5f 5f 29 0a 20 20 69 66 28 20 74 linux__). if( t
cf5a0 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 hreadsOverrideEa
cf5b0 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20 chOthersLocks<0
cf5c0 29 7b 0a 20 20 20 20 74 65 73 74 54 68 72 65 61 ){. testThrea
cf5d0 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 dLockingBehavior
cf5e0 28 66 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f 63 6b (fd);. }. lock
cf5f0 4b 65 79 2e 74 69 64 20 3d 20 74 68 72 65 61 64 Key.tid = thread
cf600 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 sOverrideEachOth
cf610 65 72 73 4c 6f 63 6b 73 20 3f 20 30 20 3a 20 70 ersLocks ? 0 : p
cf620 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 23 thread_self();.#
cf630 65 6e 64 69 66 0a 20 20 66 69 6c 65 49 64 20 3d endif. fileId =
cf640 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a 20 20 lockKey.fid;.
cf650 69 66 28 20 70 70 4c 6f 63 6b 21 3d 30 20 29 7b if( ppLock!=0 ){
cf660 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 6c 6f 63 . pLock = loc
cf670 6b 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 kList;. while
cf680 28 20 70 4c 6f 63 6b 20 26 26 20 6d 65 6d 63 6d ( pLock && memcm
cf690 70 28 26 6c 6f 63 6b 4b 65 79 2c 20 26 70 4c 6f p(&lockKey, &pLo
cf6a0 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73 69 7a ck->lockKey, siz
cf6b0 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 20 29 7b eof(lockKey)) ){
cf6c0 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 . pLock = p
cf6d0 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 Lock->pNext;.
cf6e0 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b }. if( pLock
cf6f0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f ==0 ){. pLo
cf700 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c ck = sqlite3_mal
cf710 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4c 6f loc( sizeof(*pLo
cf720 63 6b 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 ck) );. if(
cf730 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 pLock==0 ){.
cf740 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
cf750 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 _NOMEM;.
cf760 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f goto exit_findlo
cf770 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a ckinfo;. }.
cf780 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 pLock->loc
cf790 6b 4b 65 79 20 3d 20 6c 6f 63 6b 4b 65 79 3b 0a kKey = lockKey;.
cf7a0 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 pLock->nRe
cf7b0 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4c 6f f = 1;. pLo
cf7c0 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 ck->cnt = 0;.
cf7d0 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 pLock->lockty
cf7e0 70 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c pe = 0;. pL
cf7f0 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 6c 6f 63 ock->pNext = loc
cf800 6b 4c 69 73 74 3b 0a 20 20 20 20 20 20 70 4c 6f kList;. pLo
cf810 63 6b 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 ck->pPrev = 0;.
cf820 20 20 20 20 20 69 66 28 20 6c 6f 63 6b 4c 69 73 if( lockLis
cf830 74 20 29 20 6c 6f 63 6b 4c 69 73 74 2d 3e 70 50 t ) lockList->pP
cf840 72 65 76 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20 rev = pLock;.
cf850 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c lockList = pL
cf860 6f 63 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ock;. }else{.
cf870 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 pLock->nRe
cf880 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a f++;. }. *
cf890 70 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a ppLock = pLock;.
cf8a0 20 20 7d 0a 20 20 69 66 28 20 70 70 4f 70 65 6e }. if( ppOpen
cf8b0 21 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e !=0 ){. pOpen
cf8c0 20 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20 20 20 = openList;.
cf8d0 20 77 68 69 6c 65 28 20 70 4f 70 65 6e 20 26 26 while( pOpen &&
cf8e0 20 6d 65 6d 63 6d 70 28 26 66 69 6c 65 49 64 2c memcmp(&fileId,
cf8f0 20 26 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 2c &pOpen->fileId,
cf900 20 73 69 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 sizeof(fileId))
cf910 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 ){. pOpen
cf920 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a = pOpen->pNext;.
cf930 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4f }. if( pO
cf940 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 pen==0 ){.
cf950 70 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f pOpen = sqlite3_
cf960 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a malloc( sizeof(*
cf970 70 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 20 20 pOpen) );.
cf980 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a if( pOpen==0 ){.
cf990 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 4c releaseL
cf9a0 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63 6b 29 3b 0a ockInfo(pLock);.
cf9b0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
cf9c0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ITE_NOMEM;.
cf9d0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e goto exit_fin
cf9e0 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 dlockinfo;.
cf9f0 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 }. memset(
cfa00 70 4f 70 65 6e 2c 20 30 2c 20 73 69 7a 65 6f 66 pOpen, 0, sizeof
cfa10 28 2a 70 4f 70 65 6e 29 29 3b 0a 20 20 20 20 20 (*pOpen));.
cfa20 20 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 20 3d pOpen->fileId =
cfa30 20 66 69 6c 65 49 64 3b 0a 20 20 20 20 20 20 70 fileId;. p
cfa40 4f 70 65 6e 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a Open->nRef = 1;.
cfa50 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 pOpen->pNe
cfa60 78 74 20 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20 xt = openList;.
cfa70 20 20 20 20 20 69 66 28 20 6f 70 65 6e 4c 69 73 if( openLis
cfa80 74 20 29 20 6f 70 65 6e 4c 69 73 74 2d 3e 70 50 t ) openList->pP
cfa90 72 65 76 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 rev = pOpen;.
cfaa0 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f openList = pO
cfab0 70 65 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a pen;. }else{.
cfac0 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 pOpen->nRe
cfad0 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a f++;. }. *
cfae0 70 70 4f 70 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a ppOpen = pOpen;.
cfaf0 20 20 7d 0a 0a 65 78 69 74 5f 66 69 6e 64 6c 6f }..exit_findlo
cfb00 63 6b 69 6e 66 6f 3a 0a 20 20 72 65 74 75 72 6e ckinfo:. return
cfb10 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 rc;.}../*.** If
cfb20 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c we are currentl
cfb30 79 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 y in a different
cfb40 20 74 68 72 65 61 64 20 74 68 61 6e 20 74 68 65 thread than the
cfb50 20 74 68 72 65 61 64 20 74 68 61 74 20 74 68 65 thread that the
cfb60 0a 2a 2a 20 75 6e 69 78 46 69 6c 65 20 61 72 67 .** unixFile arg
cfb70 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 20 74 6f ument belongs to
cfb80 2c 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 , then transfer
cfb90 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 74 68 65 ownership of the
cfba0 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 6f 76 65 unixFile.** ove
cfbb0 72 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 r to the current
cfbc0 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 thread..**.** A
cfbd0 20 75 6e 69 78 46 69 6c 65 20 69 73 20 6f 6e 6c unixFile is onl
cfbe0 79 20 6f 77 6e 65 64 20 62 79 20 61 20 74 68 72 y owned by a thr
cfbf0 65 61 64 20 6f 6e 20 73 79 73 74 65 6d 73 20 74 ead on systems t
cfc00 68 61 74 20 75 73 65 20 4c 69 6e 75 78 54 68 72 hat use LinuxThr
cfc10 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 eads..**.** Owne
cfc20 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 20 69 rship transfer i
cfc30 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 69 s only allowed i
cfc40 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 69 f the unixFile i
cfc50 73 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f s currently unlo
cfc60 63 6b 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 cked..** If the
cfc70 75 6e 69 78 46 69 6c 65 20 69 73 20 6c 6f 63 6b unixFile is lock
cfc80 65 64 20 61 6e 64 20 61 6e 20 6f 77 6e 65 72 73 ed and an owners
cfc90 68 69 70 20 69 73 20 77 72 6f 6e 67 2c 20 74 68 hip is wrong, th
cfca0 65 6e 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c en return.** SQL
cfcb0 49 54 45 5f 4d 49 53 55 53 45 2e 20 20 53 51 4c ITE_MISUSE. SQL
cfcc0 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
cfcd0 65 64 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 ed if everything
cfce0 20 77 6f 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53 works..*/.#if S
cfcf0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
cfd00 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 && defined(__li
cfd10 6e 75 78 5f 5f 29 0a 73 74 61 74 69 63 20 69 6e nux__).static in
cfd20 74 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 t transferOwners
cfd30 68 69 70 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 hip(unixFile *pF
cfd40 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ile){. int rc;.
cfd50 20 20 70 74 68 72 65 61 64 5f 74 20 68 53 65 6c pthread_t hSel
cfd60 66 3b 0a 20 20 69 66 28 20 74 68 72 65 61 64 73 f;. if( threads
cfd70 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 OverrideEachOthe
cfd80 72 73 4c 6f 63 6b 73 20 29 7b 0a 20 20 20 20 2f rsLocks ){. /
cfd90 2a 20 4f 77 6e 65 72 73 68 69 70 20 74 72 61 6e * Ownership tran
cfda0 73 66 65 72 73 20 6e 6f 74 20 6e 65 65 64 65 64 sfers not needed
cfdb0 20 6f 6e 20 74 68 69 73 20 73 79 73 74 65 6d 20 on this system
cfdc0 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 */. return SQ
cfdd0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 LITE_OK;. }. h
cfde0 53 65 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 Self = pthread_s
cfdf0 65 6c 66 28 29 3b 0a 20 20 69 66 28 20 70 74 68 elf();. if( pth
cfe00 72 65 61 64 5f 65 71 75 61 6c 28 70 46 69 6c 65 read_equal(pFile
cfe10 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 20 29 7b ->tid, hSelf) ){
cfe20 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 73 . /* We are s
cfe30 74 69 6c 6c 20 69 6e 20 74 68 65 20 73 61 6d 65 till in the same
cfe40 20 74 68 72 65 61 64 20 2a 2f 0a 20 20 20 20 4f thread */. O
cfe50 53 54 52 41 43 45 31 28 22 4e 6f 2d 74 72 61 6e STRACE1("No-tran
cfe60 73 66 65 72 2c 20 73 61 6d 65 20 74 68 72 65 61 sfer, same threa
cfe70 64 5c 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 d\n");. retur
cfe80 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
cfe90 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
cfea0 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 cktype!=NO_LOCK
cfeb0 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 63 61 6e ){. /* We can
cfec0 6e 6f 74 20 63 68 61 6e 67 65 20 6f 77 6e 65 72 not change owner
cfed0 73 68 69 70 20 77 68 69 6c 65 20 77 65 20 61 72 ship while we ar
cfee0 65 20 68 6f 6c 64 69 6e 67 20 61 20 6c 6f 63 6b e holding a lock
cfef0 21 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 ! */. return
cff00 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
cff10 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 }. OSTRACE4("T
cff20 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73 68 69 ransfer ownershi
cff30 70 20 6f 66 20 25 64 20 66 72 6f 6d 20 25 64 20 p of %d from %d
cff40 74 6f 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 to %d\n",.
cff50 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 pFile->h,
cff60 70 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53 65 6c pFile->tid, hSel
cff70 66 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 74 69 64 f);. pFile->tid
cff80 20 3d 20 68 53 65 6c 66 3b 0a 20 20 69 66 20 28 = hSelf;. if (
cff90 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 21 3d 20 pFile->pLock !=
cffa0 4e 55 4c 4c 29 20 7b 0a 20 20 20 20 72 65 6c 65 NULL) {. rele
cffb0 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c aseLockInfo(pFil
cffc0 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 e->pLock);. r
cffd0 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f c = findLockInfo
cffe0 28 70 46 69 6c 65 2c 20 26 70 46 69 6c 65 2d 3e (pFile, &pFile->
cfff0 70 4c 6f 63 6b 2c 20 30 29 3b 0a 20 20 20 20 4f pLock, 0);. O
d0000 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20 STRACE5("LOCK
d0010 20 25 64 20 69 73 20 6e 6f 77 20 25 73 28 25 73 %d is now %s(%s
d0020 2c 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e ,%d)\n", pFile->
d0030 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f h,. lo
d0040 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 cktypeName(pFile
d0050 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 ->locktype),.
d0060 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 locktype
d0070 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 Name(pFile->pLoc
d0080 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 46 k->locktype), pF
d0090 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 29 ile->pLock->cnt)
d00a0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b ;. return rc;
d00b0 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 . } else {.
d00c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d00d0 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 20 20 2f ;. }.}.#else /
d00e0 2a 20 69 66 20 6e 6f 74 20 53 51 4c 49 54 45 5f * if not SQLITE_
d00f0 54 48 52 45 41 44 53 41 46 45 20 2a 2f 0a 20 20 THREADSAFE */.
d0100 2f 2a 20 4f 6e 20 73 69 6e 67 6c 65 2d 74 68 72 /* On single-thr
d0110 65 61 64 65 64 20 62 75 69 6c 64 73 2c 20 6f 77 eaded builds, ow
d0120 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 nership transfer
d0130 20 69 73 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 is a no-op */.#
d0140 20 64 65 66 69 6e 65 20 74 72 61 6e 73 66 65 72 define transfer
d0150 4f 77 6e 65 72 73 68 69 70 28 58 29 20 53 51 4c Ownership(X) SQL
d0160 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 20 2f 2a ITE_OK.#endif /*
d0170 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
d0180 46 45 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 FE */.../*.** Th
d0190 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b is routine check
d01a0 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 s if there is a
d01b0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 RESERVED lock he
d01c0 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 ld on the specif
d01d0 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 ied.** file by t
d01e0 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 his or any other
d01f0 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 process. If suc
d0200 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 h a lock is held
d0210 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a , set *pResOut.*
d0220 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 * to a non-zero
d0230 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 value otherwise
d0240 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 *pResOut is set
d0250 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 to zero. The re
d0260 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 turn value.** is
d0270 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f set to SQLITE_O
d0280 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 K unless an I/O
d0290 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 error occurs dur
d02a0 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e ing lock checkin
d02b0 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
d02c0 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 unixCheckReserv
d02d0 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 edLock(sqlite3_f
d02e0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 ile *id, int *pR
d02f0 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 esOut){. int rc
d0300 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
d0310 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 int reserved = 0
d0320 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 ;. unixFile *pF
d0330 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
d0340 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 )id;.. Simulate
d0350 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 IOError( return
d0360 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 SQLITE_IOERR_CHE
d0370 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 CKRESERVEDLOCK;
d0380 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 );.. assert( pF
d0390 69 6c 65 20 29 3b 0a 20 20 75 6e 69 78 45 6e 74 ile );. unixEnt
d03a0 65 72 4d 75 74 65 78 28 29 3b 20 2f 2a 20 42 65 erMutex(); /* Be
d03b0 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f cause pFile->pLo
d03c0 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 72 ck is shared acr
d03d0 6f 73 73 20 74 68 72 65 61 64 73 20 2a 2f 0a 0a oss threads */..
d03e0 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 /* Check if a
d03f0 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 thread in this p
d0400 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 rocess holds suc
d0410 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 h a lock */. if
d0420 28 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e ( pFile->pLock->
d0430 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f locktype>SHARED_
d0440 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 LOCK ){. rese
d0450 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 rved = 1;. }..
d0460 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 /* Otherwise se
d0470 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 e if some other
d0480 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74 process holds it
d0490 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f .. */.#ifndef _
d04a0 5f 44 4a 47 50 50 5f 5f 0a 20 20 69 66 28 20 21 _DJGPP__. if( !
d04b0 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 reserved ){.
d04c0 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 struct flock loc
d04d0 6b 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 k;. lock.l_wh
d04e0 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b ence = SEEK_SET;
d04f0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 . lock.l_star
d0500 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 t = RESERVED_BYT
d0510 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 E;. lock.l_le
d0520 6e 20 3d 20 31 3b 0a 20 20 20 20 6c 6f 63 6b 2e n = 1;. lock.
d0530 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b l_type = F_WRLCK
d0540 3b 0a 20 20 20 20 69 66 20 28 2d 31 20 3d 3d 20 ;. if (-1 ==
d0550 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 fcntl(pFile->h,
d0560 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29 F_GETLK, &lock))
d0570 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 {. int tEr
d0580 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
d0590 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 rc = sqliteEr
d05a0 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f rorFromPosixErro
d05b0 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 r(tErrno, SQLITE
d05c0 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 _IOERR_CHECKRESE
d05d0 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 RVEDLOCK);.
d05e0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
d05f0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
d0600 7d 20 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 2e } else if( lock.
d0610 6c 5f 74 79 70 65 21 3d 46 5f 55 4e 4c 43 4b 20 l_type!=F_UNLCK
d0620 29 7b 0a 20 20 20 20 20 20 72 65 73 65 72 76 65 ){. reserve
d0630 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d d = 1;. }. }
d0640 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 75 6e 69 .#endif. . uni
d0650 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 xLeaveMutex();.
d0660 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 20 OSTRACE4("TEST
d0670 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 WR-LOCK %d %d %d
d0680 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
d0690 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 0a 20 c, reserved);..
d06a0 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 *pResOut = rese
d06b0 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 rved;. return r
d06c0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 c;.}../*.** Perf
d06d0 6f 72 6d 20 61 20 66 69 6c 65 20 6c 6f 63 6b 69 orm a file locki
d06e0 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 ng operation on
d06f0 61 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 a range of bytes
d0700 20 69 6e 20 61 20 66 69 6c 65 2e 0a 2a 2a 20 54 in a file..** T
d0710 68 65 20 22 6f 70 22 20 70 61 72 61 6d 65 74 65 he "op" paramete
d0720 72 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 r should be one
d0730 6f 66 20 46 5f 52 44 4c 43 4b 2c 20 46 5f 57 52 of F_RDLCK, F_WR
d0740 4c 43 4b 2c 20 6f 72 20 46 5f 55 4e 4c 43 4b 2e LCK, or F_UNLCK.
d0750 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 6f 6e 20 .** Return 0 on
d0760 73 75 63 63 65 73 73 20 6f 72 20 2d 31 20 66 6f success or -1 fo
d0770 72 20 66 61 69 6c 75 72 65 2e 20 20 4f 6e 20 66 r failure. On f
d0780 61 69 6c 75 72 65 2c 20 77 72 69 74 65 20 74 68 ailure, write th
d0790 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 e error.** code
d07a0 69 6e 74 6f 20 2a 70 45 72 72 63 6f 64 65 2e 0a into *pErrcode..
d07b0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c **.** If the SQL
d07c0 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c ITE_WHOLE_FILE_L
d07d0 4f 43 4b 49 4e 47 20 62 69 74 20 69 73 20 63 6c OCKING bit is cl
d07e0 65 61 72 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 6c ear, then only l
d07f0 6f 63 6b 0a 2a 2a 20 74 68 65 20 72 61 6e 67 65 ock.** the range
d0800 20 6f 66 20 62 79 74 65 73 20 6f 6e 20 74 68 65 of bytes on the
d0810 20 6c 6f 63 6b 69 6e 67 20 70 61 67 65 20 62 65 locking page be
d0820 74 77 65 65 6e 20 53 48 41 52 45 44 5f 46 49 52 tween SHARED_FIR
d0830 53 54 20 61 6e 64 0a 2a 2a 20 53 48 41 52 45 44 ST and.** SHARED
d0840 5f 53 49 5a 45 2e 20 20 49 66 20 53 51 4c 49 54 _SIZE. If SQLIT
d0850 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 E_WHOLE_FILE_LOC
d0860 4b 49 4e 47 20 69 73 20 73 65 74 2c 20 74 68 65 KING is set, the
d0870 6e 20 6c 6f 63 6b 20 61 6c 6c 0a 2a 2a 20 62 79 n lock all.** by
d0880 74 65 73 20 66 72 6f 6d 20 30 20 75 70 20 74 6f tes from 0 up to
d0890 20 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69 but not includi
d08a0 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c ng PENDING_BYTE,
d08b0 20 61 6e 64 20 61 6c 6c 20 62 79 74 65 73 0a 2a and all bytes.*
d08c0 2a 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 53 48 * that follow SH
d08d0 41 52 45 44 5f 46 49 52 53 54 2e 0a 2a 2a 0a 2a ARED_FIRST..**.*
d08e0 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 * In other words
d08f0 2c 20 6f 66 20 53 51 4c 49 54 45 5f 57 48 4f 4c , of SQLITE_WHOL
d0900 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 69 E_FILE_LOCKING i
d0910 66 20 66 61 6c 73 65 20 28 74 68 65 20 68 69 73 f false (the his
d0920 74 6f 72 69 63 61 6c 0a 2a 2a 20 64 65 66 61 75 torical.** defau
d0930 6c 74 20 63 61 73 65 29 20 74 68 65 6e 20 6f 6e lt case) then on
d0940 6c 79 20 6c 6f 63 6b 20 61 20 73 6d 61 6c 6c 20 ly lock a small
d0950 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 66 range of bytes f
d0960 72 6f 6d 20 53 48 41 52 45 44 5f 46 49 52 53 54 rom SHARED_FIRST
d0970 0a 2a 2a 20 74 68 72 6f 75 67 68 20 53 48 41 52 .** through SHAR
d0980 45 44 5f 46 49 52 53 54 2b 53 48 41 52 45 44 5f ED_FIRST+SHARED_
d0990 53 49 5a 45 2d 31 2e 20 20 42 75 74 20 69 66 20 SIZE-1. But if
d09a0 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c SQLITE_WHOLE_FIL
d09b0 45 5f 4c 4f 43 4b 49 4e 47 20 69 73 0a 2a 2a 20 E_LOCKING is.**
d09c0 74 72 75 65 20 74 68 65 6e 20 6c 6f 63 6b 20 65 true then lock e
d09d0 76 65 72 79 20 62 79 74 65 20 69 6e 20 74 68 65 very byte in the
d09e0 20 66 69 6c 65 20 65 78 63 65 70 74 20 66 6f 72 file except for
d09f0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 61 6e PENDING_BYTE an
d0a00 64 0a 2a 2a 20 52 45 53 45 52 56 45 44 5f 42 59 d.** RESERVED_BY
d0a10 54 45 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 TE..**.** SQLITE
d0a20 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b _WHOLE_FILE_LOCK
d0a30 49 4e 47 3d 74 72 75 65 20 6f 76 65 72 6c 61 70 ING=true overlap
d0a40 73 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 s SQLITE_WHOLE_F
d0a50 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 66 61 6c 73 ILE_LOCKING=fals
d0a60 65 0a 2a 2a 20 61 6e 64 20 73 6f 20 74 68 65 20 e.** and so the
d0a70 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 73 20 locking schemes
d0a80 61 72 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e 20 are compatible.
d0a90 20 4f 6e 65 20 74 79 70 65 20 6f 66 20 6c 6f 63 One type of loc
d0aa0 6b 20 77 69 6c 6c 0a 2a 2a 20 65 66 66 65 63 74 k will.** effect
d0ab0 69 76 65 6c 79 20 65 78 63 6c 75 64 65 20 74 68 ively exclude th
d0ac0 65 20 6f 74 68 65 72 20 74 79 70 65 2e 20 20 54 e other type. T
d0ad0 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 75 73 he reason for us
d0ae0 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 ing the.** SQLIT
d0af0 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 E_WHOLE_FILE_LOC
d0b00 4b 49 4e 47 3d 74 72 75 65 20 69 73 20 74 68 61 KING=true is tha
d0b10 74 20 62 79 20 69 6e 64 69 63 61 74 69 6e 67 20 t by indicating
d0b20 74 68 65 20 66 75 6c 6c 20 72 61 6e 67 65 0a 2a the full range.*
d0b30 2a 20 6f 66 20 62 79 74 65 73 20 74 6f 20 62 65 * of bytes to be
d0b40 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e read or written
d0b50 2c 20 77 65 20 67 69 76 65 20 68 69 6e 74 73 20 , we give hints
d0b60 74 6f 20 4e 46 53 20 74 6f 20 68 65 6c 70 20 69 to NFS to help i
d0b70 74 0a 2a 2a 20 6d 61 69 6e 74 61 69 6e 20 63 61 t.** maintain ca
d0b80 63 68 65 20 63 6f 68 65 72 65 6e 63 79 2e 20 20 che coherency.
d0b90 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e On the other han
d0ba0 64 2c 20 77 68 6f 6c 65 20 66 69 6c 65 20 6c 6f d, whole file lo
d0bb0 63 6b 69 6e 67 0a 2a 2a 20 69 73 20 73 6c 6f 77 cking.** is slow
d0bc0 65 72 2c 20 73 6f 20 77 65 20 64 6f 6e 27 74 20 er, so we don't
d0bd0 77 61 6e 74 20 74 6f 20 75 73 65 20 69 74 20 65 want to use it e
d0be0 78 63 65 70 74 20 66 6f 72 20 4e 46 53 2e 0a 2a xcept for NFS..*
d0bf0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 61 6e /.static int ran
d0c00 67 65 4c 6f 63 6b 28 75 6e 69 78 46 69 6c 65 20 geLock(unixFile
d0c10 2a 70 46 69 6c 65 2c 20 69 6e 74 20 6f 70 2c 20 *pFile, int op,
d0c20 69 6e 74 20 2a 70 45 72 72 63 6f 64 65 29 7b 0a int *pErrcode){.
d0c30 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c struct flock l
d0c40 6f 63 6b 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ock;. int rc;.
d0c50 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 6f lock.l_type = o
d0c60 70 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 p;. lock.l_star
d0c70 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 t = SHARED_FIRST
d0c80 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 ;. lock.l_whenc
d0c90 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 e = SEEK_SET;.
d0ca0 69 66 28 20 28 70 46 69 6c 65 2d 3e 66 69 6c 65 if( (pFile->file
d0cb0 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 57 Flags & SQLITE_W
d0cc0 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e HOLE_FILE_LOCKIN
d0cd0 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 6c 6f 63 G)==0 ){. loc
d0ce0 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44 k.l_len = SHARED
d0cf0 5f 53 49 5a 45 3b 0a 20 20 20 20 72 63 20 3d 20 _SIZE;. rc =
d0d00 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 fcntl(pFile->h,
d0d10 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b F_SETLK, &lock);
d0d20 0a 20 20 20 20 2a 70 45 72 72 63 6f 64 65 20 3d . *pErrcode =
d0d30 20 65 72 72 6e 6f 3b 0a 20 20 7d 65 6c 73 65 7b errno;. }else{
d0d40 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 . lock.l_len
d0d50 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 66 63 = 0;. rc = fc
d0d60 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f ntl(pFile->h, F_
d0d70 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 SETLK, &lock);.
d0d80 20 20 20 2a 70 45 72 72 63 6f 64 65 20 3d 20 65 *pErrcode = e
d0d90 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 4e 45 rrno;. if( NE
d0da0 56 45 52 28 6f 70 3d 3d 46 5f 55 4e 4c 43 4b 29 VER(op==F_UNLCK)
d0db0 20 7c 7c 20 72 63 21 3d 28 2d 31 29 20 29 7b 0a || rc!=(-1) ){.
d0dc0 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 lock.l_sta
d0dd0 72 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 6c 6f rt = 0;. lo
d0de0 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 50 45 4e 44 49 ck.l_len = PENDI
d0df0 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 72 NG_BYTE;. r
d0e00 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d c = fcntl(pFile-
d0e10 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f >h, F_SETLK, &lo
d0e20 63 6b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 41 ck);. if( A
d0e30 4c 57 41 59 53 28 6f 70 21 3d 46 5f 55 4e 4c 43 LWAYS(op!=F_UNLC
d0e40 4b 29 20 26 26 20 72 63 3d 3d 28 2d 31 29 20 29 K) && rc==(-1) )
d0e50 7b 0a 20 20 20 20 20 20 20 20 2a 70 45 72 72 63 {. *pErrc
d0e60 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 ode = errno;.
d0e70 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 lock.l_type
d0e80 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 = F_UNLCK;.
d0e90 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 lock.l_start
d0ea0 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b = SHARED_FIRST;
d0eb0 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f . lock.l_
d0ec0 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 len = 0;.
d0ed0 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c fcntl(pFile->h,
d0ee0 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 F_SETLK, &lock)
d0ef0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
d0f00 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
d0f10 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 .}../*.** Lock t
d0f20 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 he file with the
d0f30 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 lock specified
d0f40 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 by parameter loc
d0f50 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f ktype - one.** o
d0f60 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a f the following:
d0f70 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 .**.** (1) S
d0f80 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 HARED_LOCK.**
d0f90 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c (2) RESERVED_L
d0fa0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 OCK.** (3) P
d0fb0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 ENDING_LOCK.**
d0fc0 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 (4) EXCLUSIVE
d0fd0 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 _LOCK.**.** Some
d0fe0 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 times when reque
d0ff0 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 sting one lock s
d1000 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c tate, additional
d1010 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 lock states.**
d1020 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 are inserted in
d1030 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f between. The lo
d1040 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c cking might fail
d1050 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c on one of the l
d1060 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 ater.** transiti
d1070 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 ons leaving the
d1080 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 lock state diffe
d1090 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 rent from what i
d10a0 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a t started but.**
d10b0 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 still short of
d10c0 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 its goal. The f
d10d0 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 ollowing chart s
d10e0 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 hows the allowed
d10f0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
d1100 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 and the inserted
d1110 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 intermediate st
d1120 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 ates:.**.** U
d1130 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 NLOCKED -> SHARE
d1140 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
d1150 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 > RESERVED.**
d1160 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 SHARED -> (PEND
d1170 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
d1180 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 E.** RESERVED
d1190 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
d11a0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
d11b0 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c PENDING -> EXCL
d11c0 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 USIVE.**.** This
d11d0 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e routine will on
d11e0 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f ly increase a lo
d11f0 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c ck. Use the sql
d1200 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a ite3OsUnlock().*
d1210 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 * routine to low
d1220 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 er a locking lev
d1230 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e el..*/.static in
d1240 74 20 75 6e 69 78 4c 6f 63 6b 28 73 71 6c 69 74 t unixLock(sqlit
d1250 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
d1260 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 2f 2a locktype){. /*
d1270 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 The following d
d1280 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6d 70 escribes the imp
d1290 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
d12a0 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 he various locks
d12b0 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b 20 74 and. ** lock t
d12c0 72 61 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74 65 ransitions in te
d12d0 72 6d 73 20 6f 66 20 74 68 65 20 50 4f 53 49 58 rms of the POSIX
d12e0 20 61 64 76 69 73 6f 72 79 20 73 68 61 72 65 64 advisory shared
d12f0 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 20 and exclusive.
d1300 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d 69 74 69 ** lock primiti
d1310 76 65 73 20 28 63 61 6c 6c 65 64 20 72 65 61 64 ves (called read
d1320 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 72 69 74 65 -locks and write
d1330 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c 20 74 6f -locks below, to
d1340 20 61 76 6f 69 64 0a 20 20 2a 2a 20 63 6f 6e 66 avoid. ** conf
d1350 75 73 69 6f 6e 20 77 69 74 68 20 53 51 4c 69 74 usion with SQLit
d1360 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e 20 54 e lock names). T
d1370 68 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 he algorithms ar
d1380 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 20 e complicated.
d1390 2a 2a 20 73 6c 69 67 68 74 6c 79 20 69 6e 20 6f ** slightly in o
d13a0 72 64 65 72 20 74 6f 20 62 65 20 63 6f 6d 70 61 rder to be compa
d13b0 74 69 62 6c 65 20 77 69 74 68 20 77 69 6e 64 6f tible with windo
d13c0 77 73 20 73 79 73 74 65 6d 73 20 73 69 6d 75 6c ws systems simul
d13d0 74 61 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a 20 61 taneously. ** a
d13e0 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d ccessing the sam
d13f0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
d1400 20 69 6e 20 63 61 73 65 20 74 68 61 74 20 69 73 in case that is
d1410 20 65 76 65 72 20 72 65 71 75 69 72 65 64 2e 0a ever required..
d1420 20 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c **. ** Symbol
d1430 73 20 64 65 66 69 6e 65 64 20 69 6e 20 6f 73 2e s defined in os.
d1440 68 20 69 6e 64 65 6e 74 69 66 79 20 74 68 65 20 h indentify the
d1450 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 20 61 'pending byte' a
d1460 6e 64 20 74 68 65 20 27 72 65 73 65 72 76 65 64 nd the 'reserved
d1470 0a 20 20 2a 2a 20 62 79 74 65 27 2c 20 65 61 63 . ** byte', eac
d1480 68 20 73 69 6e 67 6c 65 20 62 79 74 65 73 20 61 h single bytes a
d1490 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 t well known off
d14a0 73 65 74 73 2c 20 61 6e 64 20 74 68 65 20 27 73 sets, and the 's
d14b0 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 hared byte. **
d14c0 72 61 6e 67 65 27 2c 20 61 20 72 61 6e 67 65 20 range', a range
d14d0 6f 66 20 35 31 30 20 62 79 74 65 73 20 61 74 20 of 510 bytes at
d14e0 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 a well known off
d14f0 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 set.. **. ** T
d1500 6f 20 6f 62 74 61 69 6e 20 61 20 53 48 41 52 45 o obtain a SHARE
d1510 44 20 6c 6f 63 6b 2c 20 61 20 72 65 61 64 2d 6c D lock, a read-l
d1520 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 ock is obtained
d1530 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 0a on the 'pending.
d1540 20 20 2a 2a 20 62 79 74 65 27 2e 20 20 49 66 20 ** byte'. If
d1550 74 68 69 73 20 69 73 20 73 75 63 63 65 73 73 66 this is successf
d1560 75 6c 2c 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 ul, a random byt
d1570 65 20 66 72 6f 6d 20 74 68 65 20 27 73 68 61 72 e from the 'shar
d1580 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e ed byte. ** ran
d1590 67 65 27 20 69 73 20 72 65 61 64 2d 6c 6f 63 6b ge' is read-lock
d15a0 65 64 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 ed and the lock
d15b0 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 on the 'pending
d15c0 62 79 74 65 27 20 72 65 6c 65 61 73 65 64 2e 0a byte' released..
d15d0 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63 **. ** A proc
d15e0 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 ess may only obt
d15f0 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c ain a RESERVED l
d1600 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73 ock after it has
d1610 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 0a a SHARED lock..
d1620 20 20 2a 2a 20 41 20 52 45 53 45 52 56 45 44 20 ** A RESERVED
d1630 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e lock is implemen
d1640 74 65 64 20 62 79 20 67 72 61 62 62 69 6e 67 20 ted by grabbing
d1650 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 a write-lock on
d1660 74 68 65 0a 20 20 2a 2a 20 27 72 65 73 65 72 76 the. ** 'reserv
d1670 65 64 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a 0a ed byte'. . **.
d1680 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d ** A process m
d1690 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61 ay only obtain a
d16a0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 66 PENDING lock af
d16b0 74 65 72 20 69 74 20 68 61 73 20 6f 62 74 61 69 ter it has obtai
d16c0 6e 65 64 20 61 0a 20 20 2a 2a 20 53 48 41 52 45 ned a. ** SHARE
d16d0 44 20 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49 4e D lock. A PENDIN
d16e0 47 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d G lock is implem
d16f0 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69 ented by obtaini
d1700 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 0a ng a write-lock.
d1710 20 20 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65 6e ** on the 'pen
d1720 64 69 6e 67 20 62 79 74 65 27 2e 20 54 68 69 73 ding byte'. This
d1730 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f ensures that no
d1740 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b new SHARED lock
d1750 73 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f 62 s can be. ** ob
d1760 74 61 69 6e 65 64 2c 20 62 75 74 20 65 78 69 73 tained, but exis
d1770 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b ting SHARED lock
d1780 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f s are allowed to
d1790 20 70 65 72 73 69 73 74 2e 20 41 20 70 72 6f 63 persist. A proc
d17a0 65 73 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e 6f ess. ** does no
d17b0 74 20 68 61 76 65 20 74 6f 20 6f 62 74 61 69 6e t have to obtain
d17c0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
d17d0 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 61 on the way to a
d17e0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a 20 PENDING lock..
d17f0 20 2a 2a 20 54 68 69 73 20 70 72 6f 70 65 72 74 ** This propert
d1800 79 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 y is used by the
d1810 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 72 algorithm for r
d1820 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f olling back a jo
d1830 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 urnal file. **
d1840 61 66 74 65 72 20 61 20 63 72 61 73 68 2e 0a 20 after a crash..
d1850 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43 4c **. ** An EXCL
d1860 55 53 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74 61 USIVE lock, obta
d1870 69 6e 65 64 20 61 66 74 65 72 20 61 20 50 45 4e ined after a PEN
d1880 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65 6c DING lock is hel
d1890 64 2c 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c 65 d, is. ** imple
d18a0 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e mented by obtain
d18b0 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b ing a write-lock
d18c0 20 6f 6e 20 74 68 65 20 65 6e 74 69 72 65 20 27 on the entire '
d18d0 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a shared byte. **
d18e0 20 72 61 6e 67 65 27 2e 20 53 69 6e 63 65 20 61 range'. Since a
d18f0 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 72 ll other locks r
d1900 65 71 75 69 72 65 20 61 20 72 65 61 64 2d 6c 6f equire a read-lo
d1910 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 ck on one of the
d1920 20 62 79 74 65 73 0a 20 20 2a 2a 20 77 69 74 68 bytes. ** with
d1930 69 6e 20 74 68 69 73 20 72 61 6e 67 65 2c 20 74 in this range, t
d1940 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 his ensures that
d1950 20 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 no other locks
d1960 61 72 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 0a are held on the.
d1970 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a ** database. .
d1980 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72 65 **. ** The re
d1990 61 73 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62 79 ason a single by
d19a0 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 te cannot be use
d19b0 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 d instead of the
d19c0 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 'shared byte.
d19d0 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 74 68 61 ** range' is tha
d19e0 74 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 t some versions
d19f0 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 20 6e 6f of windows do no
d1a00 74 20 73 75 70 70 6f 72 74 20 72 65 61 64 2d 6c t support read-l
d1a10 6f 63 6b 73 2e 20 42 79 0a 20 20 2a 2a 20 6c 6f ocks. By. ** lo
d1a20 63 6b 69 6e 67 20 61 20 72 61 6e 64 6f 6d 20 62 cking a random b
d1a30 79 74 65 20 66 72 6f 6d 20 61 20 72 61 6e 67 65 yte from a range
d1a40 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 53 48 41 , concurrent SHA
d1a50 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 65 78 RED locks may ex
d1a60 69 73 74 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66 ist. ** even if
d1a70 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72 69 the locking pri
d1a80 6d 69 74 69 76 65 20 75 73 65 64 20 69 73 20 61 mitive used is a
d1a90 6c 77 61 79 73 20 61 20 77 72 69 74 65 2d 6c 6f lways a write-lo
d1aa0 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 72 ck.. */. int r
d1ab0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
d1ac0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d1ad0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d1ae0 3b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c ;. struct unixL
d1af0 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d ockInfo *pLock =
d1b00 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 pFile->pLock;.
d1b10 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f struct flock lo
d1b20 63 6b 3b 0a 20 20 69 6e 74 20 73 20 3d 20 30 3b ck;. int s = 0;
d1b30 0a 20 20 69 6e 74 20 74 45 72 72 6e 6f 3b 0a 0a . int tErrno;..
d1b40 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
d1b50 29 3b 0a 20 20 4f 53 54 52 41 43 45 37 28 22 4c );. OSTRACE7("L
d1b60 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 OCK %d %s was
d1b70 20 25 73 28 25 73 2c 25 64 29 20 70 69 64 3d 25 %s(%s,%d) pid=%
d1b80 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a d\n", pFile->h,.
d1b90 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 locktypeNa
d1ba0 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f me(locktype), lo
d1bb0 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 cktypeName(pFile
d1bc0 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 ->locktype),.
d1bd0 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 locktypeName(
d1be0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 pLock->locktype)
d1bf0 2c 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 2c 20 67 , pLock->cnt , g
d1c00 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 2f 2a 20 etpid());.. /*
d1c10 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 If there is alre
d1c20 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 ady a lock of th
d1c30 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 is type or more
d1c40 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 restrictive on t
d1c50 68 65 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65 he. ** unixFile
d1c60 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f , do nothing. Do
d1c70 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f n't use the end_
d1c80 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c lock: exit path,
d1c90 20 61 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 as. ** unixEnt
d1ca0 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 erMutex() hasn't
d1cb0 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 been called yet
d1cc0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 .. */. if( pFi
d1cd0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f le->locktype>=lo
d1ce0 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 cktype ){. OS
d1cf0 54 52 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 TRACE3("LOCK
d1d00 25 64 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 %d %s ok (alread
d1d10 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c y held)\n", pFil
d1d20 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 e->h,.
d1d30 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c locktypeName(l
d1d40 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20 20 20 72 ocktype));. r
d1d50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d1d60 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 . }.. /* Make
d1d70 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 sure the locking
d1d80 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 sequence is cor
d1d90 72 65 63 74 2e 0a 20 20 2a 2a 20 20 28 31 29 20 rect.. ** (1)
d1da0 57 65 20 6e 65 76 65 72 20 6d 6f 76 65 20 66 72 We never move fr
d1db0 6f 6d 20 75 6e 6c 6f 63 6b 65 64 20 74 6f 20 61 om unlocked to a
d1dc0 6e 79 74 68 69 6e 67 20 68 69 67 68 65 72 20 74 nything higher t
d1dd0 68 61 6e 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e han shared lock.
d1de0 0a 20 20 2a 2a 20 20 28 32 29 20 53 51 4c 69 74 . ** (2) SQLit
d1df0 65 20 6e 65 76 65 72 20 65 78 70 6c 69 63 69 74 e never explicit
d1e00 6c 79 20 72 65 71 75 65 73 74 73 20 61 20 70 65 ly requests a pe
d1e10 6e 64 69 67 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 ndig lock.. **
d1e20 20 28 33 29 20 41 20 73 68 61 72 65 64 20 6c 6f (3) A shared lo
d1e30 63 6b 20 69 73 20 61 6c 77 61 79 73 20 68 65 6c ck is always hel
d1e40 64 20 77 68 65 6e 20 61 20 72 65 73 65 72 76 65 d when a reserve
d1e50 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 lock is request
d1e60 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ed.. */. asser
d1e70 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 t( pFile->lockty
d1e80 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c pe!=NO_LOCK || l
d1e90 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
d1ea0 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
d1eb0 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 ( locktype!=PEND
d1ec0 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 ING_LOCK );. as
d1ed0 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d sert( locktype!=
d1ee0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c RESERVED_LOCK ||
d1ef0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d1f00 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b ==SHARED_LOCK );
d1f10 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 .. /* This mute
d1f20 78 20 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 x is needed beca
d1f30 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b use pFile->pLock
d1f40 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 is shared acros
d1f50 73 20 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 s threads. */.
d1f60 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
d1f70 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 );.. /* Make su
d1f80 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 re the current t
d1f90 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 hread owns the p
d1fa0 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 File.. */. rc
d1fb0 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 = transferOwners
d1fc0 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 hip(pFile);. if
d1fd0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
d1fe0 29 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 ){. unixLeave
d1ff0 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 Mutex();. ret
d2000 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 4c urn rc;. }. pL
d2010 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f ock = pFile->pLo
d2020 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d ck;.. /* If som
d2030 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 e thread using t
d2040 68 69 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f his PID has a lo
d2050 63 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 65 ck via a differe
d2060 6e 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a nt unixFile*. *
d2070 2a 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72 * handle that pr
d2080 65 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 75 ecludes the requ
d2090 65 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 ested lock, retu
d20a0 72 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 rn BUSY.. */.
d20b0 69 66 28 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( (pFile->lock
d20c0 74 79 70 65 21 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63 type!=pLock->loc
d20d0 6b 74 79 70 65 20 26 26 20 0a 20 20 20 20 20 20 ktype && .
d20e0 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b (pLock->lock
d20f0 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f type>=PENDING_LO
d2100 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3e 53 CK || locktype>S
d2110 48 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 HARED_LOCK)). )
d2120 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
d2130 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f E_BUSY;. goto
d2140 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a end_lock;. }..
d2150 20 20 2f 2a 20 49 66 20 61 20 53 48 41 52 45 44 /* If a SHARED
d2160 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 lock is request
d2170 65 64 2c 20 61 6e 64 20 73 6f 6d 65 20 74 68 72 ed, and some thr
d2180 65 61 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 ead using this P
d2190 49 44 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 ID already. **
d21a0 68 61 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 has a SHARED or
d21b0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74 RESERVED lock, t
d21c0 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 72 65 hen increment re
d21d0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 73 20 61 ference counts a
d21e0 6e 64 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 nd. ** return S
d21f0 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 QLITE_OK.. */.
d2200 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 if( locktype==S
d2210 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 HARED_LOCK && .
d2220 20 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 (pLock->loc
d2230 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
d2240 43 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 CK || pLock->loc
d2250 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f ktype==RESERVED_
d2260 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61 73 73 LOCK) ){. ass
d2270 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 ert( locktype==S
d2280 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
d2290 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
d22a0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a >locktype==0 );.
d22b0 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 assert( pLoc
d22c0 6b 2d 3e 63 6e 74 3e 30 20 29 3b 0a 20 20 20 20 k->cnt>0 );.
d22d0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
d22e0 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 = SHARED_LOCK;.
d22f0 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b pLock->cnt++;
d2300 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 . pFile->pOpe
d2310 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 n->nLock++;.
d2320 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 goto end_lock;.
d2330 20 7d 0a 0a 0a 20 20 2f 2a 20 41 20 50 45 4e 44 }... /* A PEND
d2340 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 ING lock is need
d2350 65 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 ed before acquir
d2360 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 ing a SHARED loc
d2370 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a k and before. *
d2380 2a 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 * acquiring an E
d2390 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 XCLUSIVE lock.
d23a0 46 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 6c For the SHARED l
d23b0 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 ock, the PENDING
d23c0 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 will. ** be re
d23d0 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 6c leased.. */. l
d23e0 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a ock.l_len = 1L;.
d23f0 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 lock.l_whence
d2400 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 69 66 = SEEK_SET;. if
d2410 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ( locktype==SHAR
d2420 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c ED_LOCK . |
d2430 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 | (locktype==EXC
d2440 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 LUSIVE_LOCK && p
d2450 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 File->locktype<P
d2460 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 ENDING_LOCK). )
d2470 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 {. lock.l_typ
d2480 65 20 3d 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 e = (locktype==S
d2490 48 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52 44 4c HARED_LOCK?F_RDL
d24a0 43 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20 20 20 CK:F_WRLCK);.
d24b0 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 lock.l_start =
d24c0 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 PENDING_BYTE;.
d24d0 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c s = fcntl(pFil
d24e0 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 e->h, F_SETLK, &
d24f0 6c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73 lock);. if( s
d2500 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 ==(-1) ){.
d2510 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
d2520 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
d2530 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 eErrorFromPosixE
d2540 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c rror(tErrno, SQL
d2550 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b ITE_IOERR_LOCK);
d2560 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f . if( IS_LO
d2570 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a CK_ERROR(rc) ){.
d2580 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
d2590 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e astErrno = tErrn
d25a0 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 o;. }.
d25b0 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a goto end_lock;.
d25c0 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20 20 2f 2a }. }... /*
d25d0 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73 If control gets
d25e0 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 to this point,
d25f0 74 68 65 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f then actually go
d2600 20 61 68 65 61 64 20 61 6e 64 20 6d 61 6b 65 0a ahead and make.
d2610 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 ** operating s
d2620 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f 72 20 ystem calls for
d2630 74 68 65 20 73 70 65 63 69 66 69 65 64 20 6c 6f the specified lo
d2640 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c ck.. */. if( l
d2650 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
d2660 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 LOCK ){. asse
d2670 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d rt( pLock->cnt==
d2680 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
d2690 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 pLock->locktype
d26a0 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4e ==0 );.. /* N
d26b0 6f 77 20 67 65 74 20 74 68 65 20 72 65 61 64 2d ow get the read-
d26c0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 73 20 3d 20 lock */. s =
d26d0 72 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c rangeLock(pFile,
d26e0 20 46 5f 52 44 4c 43 4b 2c 20 26 74 45 72 72 6e F_RDLCK, &tErrn
d26f0 6f 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 o);.. /* Drop
d2700 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50 the temporary P
d2710 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 ENDING lock */.
d2720 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 lock.l_start
d2730 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a = PENDING_BYTE;.
d2740 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d lock.l_len =
d2750 20 31 4c 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 1L;. lock.l_
d2760 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a type = F_UNLCK;.
d2770 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 70 46 if( fcntl(pF
d2780 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c ile->h, F_SETLK,
d2790 20 26 6c 6f 63 6b 29 21 3d 30 20 29 7b 0a 20 20 &lock)!=0 ){.
d27a0 20 20 20 20 69 66 28 20 73 20 21 3d 20 2d 31 20 if( s != -1
d27b0 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
d27c0 69 73 20 63 6f 75 6c 64 20 68 61 70 70 65 6e 20 is could happen
d27d0 77 69 74 68 20 61 20 6e 65 74 77 6f 72 6b 20 6d with a network m
d27e0 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 ount */.
d27f0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 tErrno = errno;
d2800 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
d2810 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
d2820 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
d2830 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c SQLITE_IOERR_UNL
d2840 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20 69 OCK); . i
d2850 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
d2860 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 (rc) ){.
d2870 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d2880 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
d2890 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67 }. g
d28a0 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 oto end_lock;.
d28b0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
d28c0 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 if( s==(-1) ){.
d28d0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
d28e0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
d28f0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
d2900 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a TE_IOERR_LOCK);.
d2910 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 if( IS_LOC
d2920 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 K_ERROR(rc) ){.
d2930 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
d2940 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
d2950 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
d2960 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 lse{. pFile
d2970 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 ->locktype = SHA
d2980 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 RED_LOCK;.
d2990 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c pFile->pOpen->nL
d29a0 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 70 4c 6f ock++;. pLo
d29b0 63 6b 2d 3e 63 6e 74 20 3d 20 31 3b 0a 20 20 20 ck->cnt = 1;.
d29c0 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c }. }else if( l
d29d0 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 ocktype==EXCLUSI
d29e0 56 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b VE_LOCK && pLock
d29f0 2d 3e 63 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f ->cnt>1 ){. /
d2a00 2a 20 57 65 20 61 72 65 20 74 72 79 69 6e 67 20 * We are trying
d2a10 66 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65 for an exclusive
d2a20 20 6c 6f 63 6b 20 62 75 74 20 61 6e 6f 74 68 65 lock but anothe
d2a30 72 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 r thread in this
d2a40 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 70 72 6f . ** same pro
d2a50 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 68 6f cess is still ho
d2a60 6c 64 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c lding a shared l
d2a70 6f 63 6b 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d ock. */. rc =
d2a80 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
d2a90 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 }else{. /* Th
d2aa0 65 20 72 65 71 75 65 73 74 20 77 61 73 20 66 6f e request was fo
d2ab0 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 r a RESERVED or
d2ac0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 EXCLUSIVE lock.
d2ad0 20 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 73 It is. ** as
d2ae0 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 65 sumed that there
d2af0 20 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 is a SHARED or
d2b00 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 greater lock on
d2b10 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 the file. **
d2b20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a already.. */.
d2b30 20 20 20 20 61 73 73 65 72 74 28 20 30 21 3d 70 assert( 0!=p
d2b40 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 File->locktype )
d2b50 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 ;. lock.l_typ
d2b60 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 e = F_WRLCK;.
d2b70 20 73 77 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 switch( locktyp
d2b80 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 e ){. case
d2b90 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 0a 20 RESERVED_LOCK:.
d2ba0 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 lock.l_st
d2bb0 61 72 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 art = RESERVED_B
d2bc0 59 54 45 3b 0a 20 20 20 20 20 20 20 20 73 20 3d YTE;. s =
d2bd0 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c fcntl(pFile->h,
d2be0 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 F_SETLK, &lock)
d2bf0 3b 0a 20 20 20 20 20 20 20 20 74 45 72 72 6e 6f ;. tErrno
d2c00 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
d2c10 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
d2c20 61 73 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f ase EXCLUSIVE_LO
d2c30 43 4b 3a 0a 20 20 20 20 20 20 20 20 73 20 3d 20 CK:. s =
d2c40 72 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c rangeLock(pFile,
d2c50 20 46 5f 57 52 4c 43 4b 2c 20 26 74 45 72 72 6e F_WRLCK, &tErrn
d2c60 6f 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 o);. brea
d2c70 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 k;. default
d2c80 3a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 :. assert
d2c90 28 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 (0);. }. i
d2ca0 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 f( s==(-1) ){.
d2cb0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
d2cc0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
d2cd0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
d2ce0 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 E_IOERR_LOCK);.
d2cf0 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b if( IS_LOCK
d2d00 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 _ERROR(rc) ){.
d2d10 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
d2d20 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
d2d30 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
d2d40 20 7d 0a 20 20 0a 0a 23 69 66 6e 64 65 66 20 4e }. ..#ifndef N
d2d50 44 45 42 55 47 0a 20 20 2f 2a 20 53 65 74 20 75 DEBUG. /* Set u
d2d60 70 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f p the transactio
d2d70 6e 2d 63 6f 75 6e 74 65 72 20 63 68 61 6e 67 65 n-counter change
d2d80 20 63 68 65 63 6b 69 6e 67 20 66 6c 61 67 73 20 checking flags
d2d90 77 68 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 69 when. ** transi
d2da0 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 61 20 53 tioning from a S
d2db0 48 41 52 45 44 20 74 6f 20 61 20 52 45 53 45 52 HARED to a RESER
d2dc0 56 45 44 20 6c 6f 63 6b 2e 20 20 54 68 65 20 63 VED lock. The c
d2dd0 68 61 6e 67 65 0a 20 20 2a 2a 20 66 72 6f 6d 20 hange. ** from
d2de0 53 48 41 52 45 44 20 74 6f 20 52 45 53 45 52 56 SHARED to RESERV
d2df0 45 44 20 6d 61 72 6b 73 20 74 68 65 20 62 65 67 ED marks the beg
d2e00 69 6e 6e 69 6e 67 20 6f 66 20 61 20 6e 6f 72 6d inning of a norm
d2e10 61 6c 0a 20 20 2a 2a 20 77 72 69 74 65 20 6f 70 al. ** write op
d2e20 65 72 61 74 69 6f 6e 20 28 6e 6f 74 20 61 20 68 eration (not a h
d2e30 6f 74 20 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 ot journal rollb
d2e40 61 63 6b 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ack).. */. if(
d2e50 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 rc==SQLITE_OK.
d2e60 20 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b && pFile->lock
d2e70 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 type<=SHARED_LOC
d2e80 4b 0a 20 20 20 26 26 20 6c 6f 63 6b 74 79 70 65 K. && locktype
d2e90 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a ==RESERVED_LOCK.
d2ea0 20 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
d2eb0 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 20 3d 20 transCntrChng =
d2ec0 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 62 0;. pFile->db
d2ed0 55 70 64 61 74 65 20 3d 20 30 3b 0a 20 20 20 20 Update = 0;.
d2ee0 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 pFile->inNormalW
d2ef0 72 69 74 65 20 3d 20 31 3b 0a 20 20 7d 0a 23 65 rite = 1;. }.#e
d2f00 6e 64 69 66 0a 0a 0a 20 20 69 66 28 20 72 63 3d ndif... if( rc=
d2f10 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
d2f20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
d2f30 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 e = locktype;.
d2f40 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 pLock->locktyp
d2f50 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 e = locktype;.
d2f60 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 }else if( lockty
d2f70 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f pe==EXCLUSIVE_LO
d2f80 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d CK ){. pFile-
d2f90 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 >locktype = PEND
d2fa0 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 4c ING_LOCK;. pL
d2fb0 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ock->locktype =
d2fc0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 PENDING_LOCK;.
d2fd0 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 }..end_lock:. u
d2fe0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
d2ff0 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 . OSTRACE4("LOC
d3000 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c 6e 22 K %d %s %s\n"
d3010 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
d3020 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 typeName(locktyp
d3030 65 29 2c 20 0a 20 20 20 20 20 20 72 63 3d 3d 53 e), . rc==S
d3040 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 QLITE_OK ? "ok"
d3050 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 72 : "failed");. r
d3060 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
d3070 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 66 69 6c ** Close all fil
d3080 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 61 63 e descriptors ac
d3090 63 75 6d 75 61 74 65 64 20 69 6e 20 74 68 65 20 cumuated in the
d30a0 75 6e 69 78 4f 70 65 6e 43 6e 74 2d 3e 70 55 6e unixOpenCnt->pUn
d30b0 75 73 65 64 20 6c 69 73 74 2e 0a 2a 2a 20 49 66 used list..** If
d30c0 20 61 6c 6c 20 73 75 63 68 20 66 69 6c 65 20 64 all such file d
d30d0 65 73 63 72 69 70 74 6f 72 73 20 61 72 65 20 63 escriptors are c
d30e0 6c 6f 73 65 64 20 77 69 74 68 6f 75 74 20 65 72 losed without er
d30f0 72 6f 72 2c 20 74 68 65 20 6c 69 73 74 20 69 73 ror, the list is
d3100 0a 2a 2a 20 63 6c 65 61 72 65 64 20 61 6e 64 20 .** cleared and
d3110 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e SQLITE_OK return
d3120 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 ed..**.** Otherw
d3130 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 ise, if an error
d3140 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 73 75 occurs, then su
d3150 63 63 65 73 73 66 75 6c 6c 79 20 63 6c 6f 73 65 ccessfully close
d3160 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f d file descripto
d3170 72 0a 2a 2a 20 65 6e 74 72 69 65 73 20 61 72 65 r.** entries are
d3180 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 removed from th
d3190 65 20 6c 69 73 74 2c 20 61 6e 64 20 53 51 4c 49 e list, and SQLI
d31a0 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20 72 TE_IOERR_CLOSE r
d31b0 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 6e 6f 74 eturned. .** not
d31c0 20 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51 4c deleted and SQL
d31d0 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20 ITE_IOERR_CLOSE
d31e0 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 20 0a 73 74 returned..*/ .st
d31f0 61 74 69 63 20 69 6e 74 20 63 6c 6f 73 65 50 65 atic int closePe
d3200 6e 64 69 6e 67 46 64 73 28 75 6e 69 78 46 69 6c ndingFds(unixFil
d3210 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 e *pFile){. int
d3220 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
d3230 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 . struct unixOp
d3240 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 enCnt *pOpen = p
d3250 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 55 File->pOpen;. U
d3260 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 45 72 nixUnusedFd *pEr
d3270 72 6f 72 20 3d 20 30 3b 0a 20 20 55 6e 69 78 55 ror = 0;. UnixU
d3280 6e 75 73 65 64 46 64 20 2a 70 3b 0a 20 20 55 6e nusedFd *p;. Un
d3290 69 78 55 6e 75 73 65 64 46 64 20 2a 70 4e 65 78 ixUnusedFd *pNex
d32a0 74 3b 0a 20 20 66 6f 72 28 70 3d 70 4f 70 65 6e t;. for(p=pOpen
d32b0 2d 3e 70 55 6e 75 73 65 64 3b 20 70 3b 20 70 3d ->pUnused; p; p=
d32c0 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e 65 78 pNext){. pNex
d32d0 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 t = p->pNext;.
d32e0 20 20 69 66 28 20 63 6c 6f 73 65 28 70 2d 3e 66 if( close(p->f
d32f0 64 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c d) ){. pFil
d3300 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 e->lastErrno = e
d3310 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d rrno;. rc =
d3320 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c SQLITE_IOERR_CL
d3330 4f 53 45 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4e OSE;. p->pN
d3340 65 78 74 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20 ext = pError;.
d3350 20 20 20 20 70 45 72 72 6f 72 20 3d 20 70 3b 0a pError = p;.
d3360 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
d3370 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 sqlite3_free(p)
d3380 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4f ;. }. }. pO
d3390 70 65 6e 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70 pen->pUnused = p
d33a0 45 72 72 6f 72 3b 0a 20 20 72 65 74 75 72 6e 20 Error;. return
d33b0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 rc;.}../*.** Add
d33c0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
d33d0 70 74 6f 72 20 75 73 65 64 20 62 79 20 66 69 6c ptor used by fil
d33e0 65 20 68 61 6e 64 6c 65 20 70 46 69 6c 65 20 74 e handle pFile t
d33f0 6f 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 o the correspond
d3400 69 6e 67 0a 2a 2a 20 70 55 6e 75 73 65 64 20 6c ing.** pUnused l
d3410 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ist..*/.static v
d3420 6f 69 64 20 73 65 74 50 65 6e 64 69 6e 67 46 64 oid setPendingFd
d3430 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 (unixFile *pFile
d3440 29 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 ){. struct unix
d3450 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d OpenCnt *pOpen =
d3460 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 pFile->pOpen;.
d3470 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 UnixUnusedFd *p
d3480 20 3d 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 = pFile->pUnuse
d3490 64 3b 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 d;. p->pNext =
d34a0 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 3b 0a pOpen->pUnused;.
d34b0 20 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 pOpen->pUnused
d34c0 20 3d 20 70 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 = p;. pFile->h
d34d0 20 3d 20 2d 31 3b 0a 20 20 70 46 69 6c 65 2d 3e = -1;. pFile->
d34e0 70 55 6e 75 73 65 64 20 3d 20 30 3b 0a 7d 0a 0a pUnused = 0;.}..
d34f0 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 /*.** Lower the
d3500 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e locking level on
d3510 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
d3520 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 pFile to lockty
d3530 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a pe. locktype.**
d3540 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
d3550 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 NO_LOCK or SHARE
d3560 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 D_LOCK..**.** If
d3570 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
d3580 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 el of the file d
d3590 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 escriptor is alr
d35a0 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 eady at or below
d35b0 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 .** the requeste
d35c0 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c d locking level,
d35d0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
d35e0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 a no-op..*/.sta
d35f0 74 69 63 20 69 6e 74 20 75 6e 69 78 55 6e 6c 6f tic int unixUnlo
d3600 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
d3610 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 *id, int locktyp
d3620 65 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a e){. unixFile *
d3630 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
d3640 65 2a 29 69 64 3b 20 2f 2a 20 54 68 65 20 6f 70 e*)id; /* The op
d3650 65 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 73 74 72 en file */. str
d3660 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f uct unixLockInfo
d3670 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a *pLock; /*
d3680 20 53 74 72 75 63 74 75 72 65 20 64 65 73 63 72 Structure descr
d3690 69 62 69 6e 67 20 63 75 72 72 65 6e 74 20 6c 6f ibing current lo
d36a0 63 6b 20 73 74 61 74 65 20 2a 2f 0a 20 20 73 74 ck state */. st
d36b0 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b ruct flock lock;
d36c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
d36d0 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 * Information pa
d36e0 73 73 65 64 20 69 6e 74 6f 20 66 63 6e 74 6c 28 ssed into fcntl(
d36f0 29 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 ) */. int rc =
d3700 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
d3710 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
d3720 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 73 n code from this
d3730 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 interface */.
d3740 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 int h;
d3750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d3760 20 2f 2a 20 54 68 65 20 75 6e 64 65 72 6c 79 69 /* The underlyi
d3770 6e 67 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ng file descript
d3780 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 74 45 72 72 or */. int tErr
d3790 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 no;
d37a0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f /* Erro
d37b0 72 20 63 6f 64 65 20 66 72 6f 6d 20 73 79 73 74 r code from syst
d37c0 65 6d 20 63 61 6c 6c 20 65 72 72 6f 72 73 20 2a em call errors *
d37d0 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 /.. assert( pFi
d37e0 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 37 le );. OSTRACE7
d37f0 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 ("UNLOCK %d %d
d3800 77 61 73 20 25 64 28 25 64 2c 25 64 29 20 70 69 was %d(%d,%d) pi
d3810 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e d=%d\n", pFile->
d3820 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 h, locktype,.
d3830 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
d3840 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b pe, pFile->pLock
d3850 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c ->locktype, pFil
d3860 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 2c 20 67 e->pLock->cnt, g
d3870 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 61 73 73 etpid());.. ass
d3880 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 ert( locktype<=S
d3890 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
d38a0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
d38b0 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b ype<=locktype ){
d38c0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d38d0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 TE_OK;. }. if(
d38e0 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 CHECK_THREADID(
d38f0 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 pFile) ){. re
d3900 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 turn SQLITE_MISU
d3910 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e SE;. }. unixEn
d3920 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 68 20 terMutex();. h
d3930 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 70 4c = pFile->h;. pL
d3940 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f ock = pFile->pLo
d3950 63 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c ck;. assert( pL
d3960 6f 63 6b 2d 3e 63 6e 74 21 3d 30 20 29 3b 0a 20 ock->cnt!=0 );.
d3970 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
d3980 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b type>SHARED_LOCK
d3990 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
d39a0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d pLock->locktype=
d39b0 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 =pFile->locktype
d39c0 20 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 );. Simulate
d39d0 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 IOErrorBenign(1)
d39e0 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f ;. SimulateIO
d39f0 45 72 72 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a Error( h=(-1) ).
d3a00 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 SimulateIOEr
d3a10 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 0a 23 rorBenign(0);..#
d3a20 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 ifndef NDEBUG.
d3a30 20 20 2f 2a 20 57 68 65 6e 20 72 65 64 75 63 69 /* When reduci
d3a40 6e 67 20 61 20 6c 6f 63 6b 20 73 75 63 68 20 74 ng a lock such t
d3a50 68 61 74 20 6f 74 68 65 72 20 70 72 6f 63 65 73 hat other proces
d3a60 73 65 73 20 63 61 6e 20 73 74 61 72 74 0a 20 20 ses can start.
d3a70 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65 ** reading the
d3a80 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
d3a90 67 61 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20 gain, make sure
d3aa0 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 that the. **
d3ab0 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e transaction coun
d3ac0 74 65 72 20 77 61 73 20 75 70 64 61 74 65 64 20 ter was updated
d3ad0 69 66 20 61 6e 79 20 70 61 72 74 20 6f 66 20 74 if any part of t
d3ae0 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 he database.
d3af0 2a 2a 20 66 69 6c 65 20 63 68 61 6e 67 65 64 2e ** file changed.
d3b00 20 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 If the transac
d3b10 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73 20 tion counter is
d3b20 6e 6f 74 20 75 70 64 61 74 65 64 2c 0a 20 20 20 not updated,.
d3b30 20 2a 2a 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 ** other connec
d3b40 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 73 61 6d tions to the sam
d3b50 65 20 66 69 6c 65 20 6d 69 67 68 74 20 6e 6f 74 e file might not
d3b60 20 72 65 61 6c 69 7a 65 20 74 68 61 74 0a 20 20 realize that.
d3b70 20 20 2a 2a 20 74 68 65 20 66 69 6c 65 20 68 61 ** the file ha
d3b80 73 20 63 68 61 6e 67 65 64 20 61 6e 64 20 68 65 s changed and he
d3b90 6e 63 65 20 6d 69 67 68 74 20 6e 6f 74 20 6b 6e nce might not kn
d3ba0 6f 77 20 74 6f 20 66 6c 75 73 68 20 74 68 65 69 ow to flush thei
d3bb0 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 20 r. ** cache.
d3bc0 20 54 68 65 20 75 73 65 20 6f 66 20 61 20 73 74 The use of a st
d3bd0 61 6c 65 20 63 61 63 68 65 20 63 61 6e 20 6c 65 ale cache can le
d3be0 61 64 20 74 6f 20 64 61 74 61 62 61 73 65 20 63 ad to database c
d3bf0 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 2a orruption.. *
d3c00 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 /. assert( pF
d3c10 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 ile->inNormalWri
d3c20 74 65 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 7c te==0. |
d3c30 7c 20 70 46 69 6c 65 2d 3e 64 62 55 70 64 61 74 | pFile->dbUpdat
d3c40 65 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 7c 7c e==0. ||
d3c50 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 pFile->transCnt
d3c60 72 43 68 6e 67 3d 3d 31 20 29 3b 0a 20 20 20 20 rChng==1 );.
d3c70 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 pFile->inNormalW
d3c80 72 69 74 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 rite = 0;.#endif
d3c90 0a 0a 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 ... if( lockt
d3ca0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
d3cb0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 61 ){. if( ra
d3cc0 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 46 ngeLock(pFile, F
d3cd0 5f 52 44 4c 43 4b 2c 20 26 74 45 72 72 6e 6f 29 _RDLCK, &tErrno)
d3ce0 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 ==(-1) ){.
d3cf0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 rc = sqliteErr
d3d00 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
d3d10 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
d3d20 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 29 3b 0a 20 IOERR_RDLOCK);.
d3d30 20 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f if( IS_LO
d3d40 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a CK_ERROR(rc) ){.
d3d50 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d pFile-
d3d60 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
d3d70 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rno;. }.
d3d80 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
d3d90 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 7d 0a unlock;. }.
d3da0 20 20 20 20 7d 0a 20 20 20 20 6c 6f 63 6b 2e 6c }. lock.l
d3db0 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b _type = F_UNLCK;
d3dc0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e . lock.l_when
d3dd0 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 ce = SEEK_SET;.
d3de0 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 lock.l_start
d3df0 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a = PENDING_BYTE;.
d3e00 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d lock.l_len =
d3e10 20 32 4c 3b 20 20 61 73 73 65 72 74 28 20 50 45 2L; assert( PE
d3e20 4e 44 49 4e 47 5f 42 59 54 45 2b 31 3d 3d 52 45 NDING_BYTE+1==RE
d3e30 53 45 52 56 45 44 5f 42 59 54 45 20 29 3b 0a 20 SERVED_BYTE );.
d3e40 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68 2c 20 if( fcntl(h,
d3e50 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 F_SETLK, &lock)!
d3e60 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 70 =(-1) ){. p
d3e70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d Lock->locktype =
d3e80 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 SHARED_LOCK;.
d3e90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 74 }else{. t
d3ea0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d3eb0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
d3ec0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
d3ed0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
d3ee0 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 TE_IOERR_UNLOCK)
d3ef0 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c ;. if( IS_L
d3f00 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b OCK_ERROR(rc) ){
d3f10 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e . pFile->
d3f20 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 lastErrno = tErr
d3f30 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 no;. }.
d3f40 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 goto end_unloc
d3f50 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 k;. }. }. i
d3f60 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f f( locktype==NO_
d3f70 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 73 74 72 75 LOCK ){. stru
d3f80 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a ct unixOpenCnt *
d3f90 70 4f 70 65 6e 3b 0a 0a 20 20 20 20 2f 2a 20 44 pOpen;.. /* D
d3fa0 65 63 72 65 6d 65 6e 74 20 74 68 65 20 73 68 61 ecrement the sha
d3fb0 72 65 64 20 6c 6f 63 6b 20 63 6f 75 6e 74 65 72 red lock counter
d3fc0 2e 20 20 52 65 6c 65 61 73 65 20 74 68 65 20 6c . Release the l
d3fd0 6f 63 6b 20 75 73 69 6e 67 20 61 6e 0a 20 20 20 ock using an.
d3fe0 20 2a 2a 20 4f 53 20 63 61 6c 6c 20 6f 6e 6c 79 ** OS call only
d3ff0 20 77 68 65 6e 20 61 6c 6c 20 74 68 72 65 61 64 when all thread
d4000 73 20 69 6e 20 74 68 69 73 20 73 61 6d 65 20 70 s in this same p
d4010 72 6f 63 65 73 73 20 68 61 76 65 20 72 65 6c 65 rocess have rele
d4020 61 73 65 64 0a 20 20 20 20 2a 2a 20 74 68 65 20 ased. ** the
d4030 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 lock.. */.
d4040 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2d 2d 3b 0a 20 pLock->cnt--;.
d4050 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 63 6e if( pLock->cn
d4060 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6c 6f t==0 ){. lo
d4070 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e ck.l_type = F_UN
d4080 4c 43 4b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e LCK;. lock.
d4090 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f l_whence = SEEK_
d40a0 53 45 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e SET;. lock.
d40b0 6c 5f 73 74 61 72 74 20 3d 20 6c 6f 63 6b 2e 6c l_start = lock.l
d40c0 5f 6c 65 6e 20 3d 20 30 4c 3b 0a 20 20 20 20 20 _len = 0L;.
d40d0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d40e0 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 20 Benign(1);.
d40f0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d4100 28 20 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 20 ( h=(-1) ).
d4110 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d4120 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20 20 20 20 Benign(0);.
d4130 20 69 66 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f if( fcntl(h, F_
d4140 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28 SETLK, &lock)!=(
d4150 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 -1) ){. p
d4160 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d Lock->locktype =
d4170 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 NO_LOCK;.
d4180 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 74 }else{. t
d4190 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d41a0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
d41b0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 teErrorFromPosix
d41c0 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 Error(tErrno, SQ
d41d0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 LITE_IOERR_UNLOC
d41e0 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 K);. if(
d41f0 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 IS_LOCK_ERROR(rc
d4200 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
d4210 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
d4220 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = tErrno;.
d4230 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 }. pLoc
d4240 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f k->locktype = NO
d4250 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 70 _LOCK;. p
d4260 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
d4270 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 NO_LOCK;.
d4280 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
d4290 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 63 6f Decrement the co
d42a0 75 6e 74 20 6f 66 20 6c 6f 63 6b 73 20 61 67 61 unt of locks aga
d42b0 69 6e 73 74 20 74 68 69 73 20 73 61 6d 65 20 66 inst this same f
d42c0 69 6c 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 20 ile. When the.
d42d0 20 20 20 2a 2a 20 63 6f 75 6e 74 20 72 65 61 63 ** count reac
d42e0 68 65 73 20 7a 65 72 6f 2c 20 63 6c 6f 73 65 20 hes zero, close
d42f0 61 6e 79 20 6f 74 68 65 72 20 66 69 6c 65 20 64 any other file d
d4300 65 73 63 72 69 70 74 6f 72 73 20 77 68 6f 73 65 escriptors whose
d4310 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 77 61 close. ** wa
d4320 73 20 64 65 66 65 72 72 65 64 20 62 65 63 61 75 s deferred becau
d4330 73 65 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e se of outstandin
d4340 67 20 6c 6f 63 6b 73 2e 0a 20 20 20 20 2a 2f 0a g locks.. */.
d4350 20 20 20 20 70 4f 70 65 6e 20 3d 20 70 46 69 6c pOpen = pFil
d4360 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 70 4f e->pOpen;. pO
d4370 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20 pen->nLock--;.
d4380 20 20 61 73 73 65 72 74 28 20 70 4f 70 65 6e 2d assert( pOpen-
d4390 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 >nLock>=0 );.
d43a0 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 if( pOpen->nLoc
d43b0 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e k==0 ){. in
d43c0 74 20 72 63 32 20 3d 20 63 6c 6f 73 65 50 65 6e t rc2 = closePen
d43d0 64 69 6e 67 46 64 73 28 70 46 69 6c 65 29 3b 0a dingFds(pFile);.
d43e0 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
d43f0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
d4400 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 rc = rc2;.
d4410 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 09 }. }. }..
d4420 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a 0a 20 20 75 .end_unlock:. u
d4430 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
d4440 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
d4450 45 5f 4f 4b 20 29 20 70 46 69 6c 65 2d 3e 6c 6f E_OK ) pFile->lo
d4460 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 cktype = locktyp
d4470 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a e;. return rc;.
d4480 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
d4490 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20 nction performs
d44a0 74 68 65 20 70 61 72 74 73 20 6f 66 20 74 68 65 the parts of the
d44b0 20 22 63 6c 6f 73 65 20 66 69 6c 65 22 20 6f 70 "close file" op
d44c0 65 72 61 74 69 6f 6e 20 0a 2a 2a 20 63 6f 6d 6d eration .** comm
d44d0 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e on to all lockin
d44e0 67 20 73 63 68 65 6d 65 73 2e 20 49 74 20 63 6c g schemes. It cl
d44f0 6f 73 65 73 20 74 68 65 20 64 69 72 65 63 74 6f oses the directo
d4500 72 79 20 61 6e 64 20 66 69 6c 65 0a 2a 2a 20 68 ry and file.** h
d4510 61 6e 64 6c 65 73 2c 20 69 66 20 74 68 65 79 20 andles, if they
d4520 61 72 65 20 76 61 6c 69 64 2c 20 61 6e 64 20 73 are valid, and s
d4530 65 74 73 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f ets all fields o
d4540 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 0a 2a f the unixFile.*
d4550 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 30 * structure to 0
d4560 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 2a 6e ..**.** It is *n
d4570 6f 74 2a 20 6e 65 63 65 73 73 61 72 79 20 74 6f ot* necessary to
d4580 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 20 hold the mutex
d4590 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e when this routin
d45a0 65 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 e is called,.**
d45b0 65 76 65 6e 20 6f 6e 20 56 78 57 6f 72 6b 73 2e even on VxWorks.
d45c0 20 20 41 20 6d 75 74 65 78 20 77 69 6c 6c 20 62 A mutex will b
d45d0 65 20 61 63 71 75 69 72 65 64 20 6f 6e 20 56 78 e acquired on Vx
d45e0 57 6f 72 6b 73 20 62 79 20 74 68 65 0a 2a 2a 20 Works by the.**
d45f0 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46 69 vxworksReleaseFi
d4600 6c 65 49 64 28 29 20 72 6f 75 74 69 6e 65 2e 0a leId() routine..
d4610 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c */.static int cl
d4620 6f 73 65 55 6e 69 78 46 69 6c 65 28 73 71 6c 69 oseUnixFile(sqli
d4630 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 te3_file *id){.
d4640 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d4650 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d4660 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 20 29 7b ;. if( pFile ){
d4670 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e . if( pFile->
d4680 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 dirfd>=0 ){.
d4690 20 20 69 6e 74 20 65 72 72 20 3d 20 63 6c 6f 73 int err = clos
d46a0 65 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b e(pFile->dirfd);
d46b0 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 20 29 . if( err )
d46c0 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d {. pFile-
d46d0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
d46e0 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 no;. retu
d46f0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
d4700 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 DIR_CLOSE;.
d4710 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
d4720 70 46 69 6c 65 2d 3e 64 69 72 66 64 3d 2d 31 3b pFile->dirfd=-1;
d4730 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
d4740 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e if( pFile->h>
d4750 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 =0 ){. int
d4760 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c err = close(pFil
d4770 65 2d 3e 68 29 3b 0a 20 20 20 20 20 20 69 66 28 e->h);. if(
d4780 20 65 72 72 20 29 7b 0a 20 20 20 20 20 20 20 20 err ){.
d4790 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
d47a0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
d47b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d47c0 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 IOERR_CLOSE;.
d47d0 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 20 4f }. }.#if O
d47e0 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 69 66 S_VXWORKS. if
d47f0 28 20 70 46 69 6c 65 2d 3e 70 49 64 20 29 7b 0a ( pFile->pId ){.
d4800 20 20 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d if( pFile-
d4810 3e 69 73 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 >isDelete ){.
d4820 20 20 20 20 20 75 6e 6c 69 6e 6b 28 70 46 69 6c unlink(pFil
d4830 65 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69 63 e->pId->zCanonic
d4840 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d alName);. }
d4850 0a 20 20 20 20 20 20 76 78 77 6f 72 6b 73 52 65 . vxworksRe
d4860 6c 65 61 73 65 46 69 6c 65 49 64 28 70 46 69 6c leaseFileId(pFil
d4870 65 2d 3e 70 49 64 29 3b 0a 20 20 20 20 20 20 70 e->pId);. p
d4880 46 69 6c 65 2d 3e 70 49 64 20 3d 20 30 3b 0a 20 File->pId = 0;.
d4890 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
d48a0 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53 45 20 OSTRACE2("CLOSE
d48b0 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65 %-3d\n", pFile
d48c0 2d 3e 68 29 3b 0a 20 20 20 20 4f 70 65 6e 43 6f ->h);. OpenCo
d48d0 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 20 20 73 unter(-1);. s
d48e0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c qlite3_free(pFil
d48f0 65 2d 3e 70 55 6e 75 73 65 64 29 3b 0a 20 20 20 e->pUnused);.
d4900 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 memset(pFile, 0
d4910 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c , sizeof(unixFil
d4920 65 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 e));. }. retur
d4930 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
d4940 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 /*.** Close a fi
d4950 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
d4960 74 20 75 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69 t unixClose(sqli
d4970 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 te3_file *id){.
d4980 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
d4990 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 64 20 29 7b _OK;. if( id ){
d49a0 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 . unixFile *p
d49b0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
d49c0 20 2a 29 69 64 3b 0a 20 20 20 20 75 6e 69 78 55 *)id;. unixU
d49d0 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 nlock(id, NO_LOC
d49e0 4b 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 K);. unixEnte
d49f0 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66 rMutex();. if
d4a00 28 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 20 26 ( pFile->pOpen &
d4a10 26 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e & pFile->pOpen->
d4a20 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 2f nLock ){. /
d4a30 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6f * If there are o
d4a40 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 utstanding locks
d4a50 2c 20 64 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c , do not actuall
d4a60 79 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 y close the file
d4a70 20 6a 75 73 74 0a 20 20 20 20 20 20 2a 2a 20 79 just. ** y
d4a80 65 74 20 62 65 63 61 75 73 65 20 74 68 61 74 20 et because that
d4a90 77 6f 75 6c 64 20 63 6c 65 61 72 20 74 68 6f 73 would clear thos
d4aa0 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 74 65 61 e locks. Instea
d4ab0 64 2c 20 61 64 64 20 74 68 65 20 66 69 6c 65 0a d, add the file.
d4ac0 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70 ** descrip
d4ad0 74 6f 72 20 74 6f 20 70 4f 70 65 6e 2d 3e 70 55 tor to pOpen->pU
d4ae0 6e 75 73 65 64 20 6c 69 73 74 2e 20 20 49 74 20 nused list. It
d4af0 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 will be automati
d4b00 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20 0a 20 20 cally closed .
d4b10 20 20 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 ** when the
d4b20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65 last lock is cle
d4b30 61 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ared.. */.
d4b40 20 20 20 20 20 73 65 74 50 65 6e 64 69 6e 67 46 setPendingF
d4b50 64 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a d(pFile);. }.
d4b60 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 releaseLockI
d4b70 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b nfo(pFile->pLock
d4b80 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70 );. releaseOp
d4b90 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 enCnt(pFile->pOp
d4ba0 65 6e 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c en);. rc = cl
d4bb0 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b oseUnixFile(id);
d4bc0 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 . unixLeaveMu
d4bd0 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 tex();. }. ret
d4be0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a urn rc;.}../****
d4bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
d4c00 66 20 74 68 65 20 70 6f 73 69 78 20 61 64 76 69 f the posix advi
d4c10 73 6f 72 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d sory lock implem
d4c20 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a entation *******
d4c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a **********.*****
d4c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a *********/../***
d4c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
d4ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 2d 6f 70 ********** No-op
d4d00 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a Locking *******
d4d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
d4d30 2a 20 4f 66 20 74 68 65 20 76 61 72 69 6f 75 73 * Of the various
d4d40 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 locking impleme
d4d50 6e 74 61 74 69 6f 6e 73 20 61 76 61 69 6c 61 62 ntations availab
d4d60 6c 65 2c 20 74 68 69 73 20 69 73 20 62 79 20 66 le, this is by f
d4d70 61 72 20 74 68 65 0a 2a 2a 20 73 69 6d 70 6c 65 ar the.** simple
d4d80 73 74 3a 20 20 6c 6f 63 6b 69 6e 67 20 69 73 20 st: locking is
d4d90 69 67 6e 6f 72 65 64 2e 20 20 4e 6f 20 61 74 74 ignored. No att
d4da0 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 empt is made to
d4db0 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 lock the databas
d4dc0 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 72 65 e.** file for re
d4dd0 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 ading or writing
d4de0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 63 ..**.** This loc
d4df0 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 61 70 70 king mode is app
d4e00 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75 73 65 ropriate for use
d4e10 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 on read-only da
d4e20 74 61 62 61 73 65 73 0a 2a 2a 20 28 65 78 3a 20 tabases.** (ex:
d4e30 64 61 74 61 62 61 73 65 73 20 74 68 61 74 20 61 databases that a
d4e40 72 65 20 62 75 72 6e 65 64 20 69 6e 74 6f 20 43 re burned into C
d4e50 44 2d 52 4f 4d 2c 20 66 6f 72 20 65 78 61 6d 70 D-ROM, for examp
d4e60 6c 65 2e 29 20 20 49 74 20 63 61 6e 0a 2a 2a 20 le.) It can.**
d4e70 61 6c 73 6f 20 62 65 20 75 73 65 64 20 69 66 20 also be used if
d4e80 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
d4e90 65 6d 70 6c 6f 79 73 20 73 6f 6d 65 20 65 78 74 employs some ext
d4ea0 65 72 6e 61 6c 20 6d 65 63 68 61 6e 69 73 6d 20 ernal mechanism
d4eb0 74 6f 0a 2a 2a 20 70 72 65 76 65 6e 74 20 73 69 to.** prevent si
d4ec0 6d 75 6c 74 61 6e 65 6f 75 73 20 61 63 63 65 73 multaneous acces
d4ed0 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 64 61 s of the same da
d4ee0 74 61 62 61 73 65 20 62 79 20 74 77 6f 20 6f 72 tabase by two or
d4ef0 20 6d 6f 72 65 0a 2a 2a 20 64 61 74 61 62 61 73 more.** databas
d4f00 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 e connections.
d4f10 42 75 74 20 74 68 65 72 65 20 69 73 20 61 20 73 But there is a s
d4f20 65 72 69 6f 75 73 20 72 69 73 6b 20 6f 66 20 64 erious risk of d
d4f30 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 72 72 75 atabase.** corru
d4f40 70 74 69 6f 6e 20 69 66 20 74 68 69 73 20 6c 6f ption if this lo
d4f50 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 75 73 cking mode is us
d4f60 65 64 20 69 6e 20 73 69 74 75 61 74 69 6f 6e 73 ed in situations
d4f70 20 77 68 65 72 65 20 6d 75 6c 74 69 70 6c 65 0a where multiple.
d4f80 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ** database conn
d4f90 65 63 74 69 6f 6e 73 20 61 72 65 20 61 63 63 65 ections are acce
d4fa0 73 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 ssing the same d
d4fb0 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 atabase file at
d4fc0 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 69 6d 65 the same.** time
d4fd0 20 61 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 and one or more
d4fe0 20 6f 66 20 74 68 6f 73 65 20 63 6f 6e 6e 65 63 of those connec
d4ff0 74 69 6f 6e 73 20 61 72 65 20 77 72 69 74 69 6e tions are writin
d5000 67 2e 0a 2a 2f 0a 0a 73 74 61 74 69 63 20 69 6e g..*/..static in
d5010 74 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73 t nolockCheckRes
d5020 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
d5030 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 3_file *NotUsed,
d5040 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a int *pResOut){.
d5050 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
d5060 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 2a ER(NotUsed);. *
d5070 70 52 65 73 4f 75 74 20 3d 20 30 3b 0a 20 20 72 pResOut = 0;. r
d5080 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d5090 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f .}.static int no
d50a0 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 lockLock(sqlite3
d50b0 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 _file *NotUsed,
d50c0 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 int NotUsed2){.
d50d0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
d50e0 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 R2(NotUsed, NotU
d50f0 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 sed2);. return
d5100 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 SQLITE_OK;.}.sta
d5110 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 55 6e tic int nolockUn
d5120 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c lock(sqlite3_fil
d5130 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 e *NotUsed, int
d5140 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 NotUsed2){. UNU
d5150 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e SED_PARAMETER2(N
d5160 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 otUsed, NotUsed2
d5170 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
d5180 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
d5190 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a Close the file..
d51a0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f */.static int no
d51b0 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 lockClose(sqlite
d51c0 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 3_file *id) {.
d51d0 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 return closeUnix
d51e0 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 2a File(id);.}../**
d51f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5200 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 6f 2d * End of the no-
d5210 6f 70 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e op lock implemen
d5220 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a tation *********
d5230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
d5240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
d5290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d52a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d52b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d52c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d52d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
d52e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d52f0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 64 6f ******* Begin do
d5300 74 2d 66 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 2a t-file Locking *
d5310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
d5330 0a 2a 2a 20 54 68 65 20 64 6f 74 66 69 6c 65 20 .** The dotfile
d5340 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e locking implemen
d5350 74 61 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20 tation uses the
d5360 65 78 69 73 74 61 6e 63 65 20 6f 66 20 73 65 70 existance of sep
d5370 61 72 61 74 65 20 6c 6f 63 6b 0a 2a 2a 20 66 69 arate lock.** fi
d5380 6c 65 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 les in order to
d5390 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 control access t
d53a0 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 o the database.
d53b0 20 54 68 69 73 20 77 6f 72 6b 73 20 6f 6e 20 6a This works on j
d53c0 75 73 74 0a 2a 2a 20 61 62 6f 75 74 20 65 76 65 ust.** about eve
d53d0 72 79 20 66 69 6c 65 73 79 73 74 65 6d 20 69 6d ry filesystem im
d53e0 61 67 69 6e 61 62 6c 65 2e 20 20 42 75 74 20 74 aginable. But t
d53f0 68 65 72 65 20 61 72 65 20 73 65 72 69 6f 75 73 here are serious
d5400 20 64 6f 77 6e 73 69 64 65 73 3a 0a 2a 2a 0a 2a downsides:.**.*
d5410 2a 20 20 20 20 28 31 29 20 20 54 68 65 72 65 20 * (1) There
d5420 69 73 20 7a 65 72 6f 20 63 6f 6e 63 75 72 72 65 is zero concurre
d5430 6e 63 79 2e 20 20 41 20 73 69 6e 67 6c 65 20 72 ncy. A single r
d5440 65 61 64 65 72 20 62 6c 6f 63 6b 73 20 61 6c 6c eader blocks all
d5450 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 other.**
d5460 20 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 72 connections fr
d5470 6f 6d 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 om reading or wr
d5480 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 iting the databa
d5490 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 se..**.** (2)
d54a0 20 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e An application
d54b0 20 63 72 61 73 68 20 6f 72 20 70 6f 77 65 72 20 crash or power
d54c0 6c 6f 73 73 20 63 61 6e 20 6c 65 61 76 65 20 73 loss can leave s
d54d0 74 61 6c 65 20 6c 6f 63 6b 20 66 69 6c 65 73 0a tale lock files.
d54e0 2a 2a 20 20 20 20 20 20 20 20 20 73 69 74 74 69 ** sitti
d54f0 6e 67 20 61 72 6f 75 6e 64 20 74 68 61 74 20 6e ng around that n
d5500 65 65 64 20 74 6f 20 62 65 20 63 6c 65 61 72 65 eed to be cleare
d5510 64 20 6d 61 6e 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a d manually..**.*
d5520 2a 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20 * Nevertheless,
d5530 61 20 64 6f 74 6c 6f 63 6b 20 69 73 20 61 6e 20 a dotlock is an
d5540 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b appropriate lock
d5550 69 6e 67 20 6d 6f 64 65 20 66 6f 72 20 75 73 65 ing mode for use
d5560 20 69 66 20 6e 6f 0a 2a 2a 20 6f 74 68 65 72 20 if no.** other
d5570 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 locking strategy
d5580 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a is available..*
d5590 2a 0a 2a 2a 20 44 6f 74 66 69 6c 65 20 6c 6f 63 *.** Dotfile loc
d55a0 6b 69 6e 67 20 77 6f 72 6b 73 20 62 79 20 63 72 king works by cr
d55b0 65 61 74 69 6e 67 20 61 20 66 69 6c 65 20 69 6e eating a file in
d55c0 20 74 68 65 20 73 61 6d 65 20 64 69 72 65 63 74 the same direct
d55d0 6f 72 79 20 61 73 20 74 68 65 0a 2a 2a 20 64 61 ory as the.** da
d55e0 74 61 62 61 73 65 20 61 6e 64 20 77 69 74 68 20 tabase and with
d55f0 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 the same name bu
d5600 74 20 77 69 74 68 20 61 20 22 2e 6c 6f 63 6b 22 t with a ".lock"
d5610 20 65 78 74 65 6e 73 69 6f 6e 20 61 64 64 65 64 extension added
d5620 2e 0a 2a 2a 20 54 68 65 20 65 78 69 73 74 61 6e ..** The existan
d5630 63 65 20 6f 66 20 61 20 6c 6f 63 6b 20 66 69 6c ce of a lock fil
d5640 65 20 69 6d 70 6c 69 65 73 20 61 6e 20 45 58 43 e implies an EXC
d5650 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 41 6c LUSIVE lock. Al
d5660 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 0a 2a 2a 20 l other lock.**
d5670 74 79 70 65 73 20 28 53 48 41 52 45 44 2c 20 52 types (SHARED, R
d5680 45 53 45 52 56 45 44 2c 20 50 45 4e 44 49 4e 47 ESERVED, PENDING
d5690 29 20 61 72 65 20 6d 61 70 70 65 64 20 69 6e 74 ) are mapped int
d56a0 6f 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2f 0a o EXCLUSIVE..*/.
d56b0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 ./*.** The file
d56c0 73 75 66 66 69 78 20 61 64 64 65 64 20 74 6f 20 suffix added to
d56d0 74 68 65 20 64 61 74 61 20 62 61 73 65 20 66 69 the data base fi
d56e0 6c 65 6e 61 6d 65 20 69 6e 20 6f 72 64 65 72 20 lename in order
d56f0 74 6f 20 63 72 65 61 74 65 20 74 68 65 0a 2a 2a to create the.**
d5700 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 23 lock file..*/.#
d5710 64 65 66 69 6e 65 20 44 4f 54 4c 4f 43 4b 5f 53 define DOTLOCK_S
d5720 55 46 46 49 58 20 22 2e 6c 6f 63 6b 22 0a 0a 2f UFFIX ".lock"../
d5730 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
d5740 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 e checks if ther
d5750 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 e is a RESERVED
d5760 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 lock held on the
d5770 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 specified.** fi
d5780 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e le by this or an
d5790 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e y other process.
d57a0 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 If such a lock
d57b0 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 is held, set *pR
d57c0 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f esOut.** to a no
d57d0 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 n-zero value oth
d57e0 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 erwise *pResOut
d57f0 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 is set to zero.
d5800 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
d5810 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 e.** is set to S
d5820 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 QLITE_OK unless
d5830 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 an I/O error occ
d5840 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 urs during lock
d5850 63 68 65 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 checking..**.**
d5860 49 6e 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 In dotfile locki
d5870 6e 67 2c 20 65 69 74 68 65 72 20 61 20 6c 6f 63 ng, either a loc
d5880 6b 20 65 78 69 73 74 73 20 6f 72 20 69 74 20 64 k exists or it d
d5890 6f 65 73 20 6e 6f 74 2e 20 20 53 6f 20 69 6e 20 oes not. So in
d58a0 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 74 69 6f this.** variatio
d58b0 6e 20 6f 66 20 43 68 65 63 6b 52 65 73 65 72 76 n of CheckReserv
d58c0 65 64 4c 6f 63 6b 28 29 2c 20 2a 70 52 65 73 4f edLock(), *pResO
d58d0 75 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 ut is set to tru
d58e0 65 20 69 66 20 61 6e 79 20 6c 6f 63 6b 0a 2a 2a e if any lock.**
d58f0 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 is held on the
d5900 66 69 6c 65 20 61 6e 64 20 66 61 6c 73 65 20 69 file and false i
d5910 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 75 6e f the file is un
d5920 6c 6f 63 6b 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 locked..*/.stati
d5930 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 68 65 c int dotlockChe
d5940 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 ckReservedLock(s
d5950 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
d5960 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b int *pResOut) {
d5970 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
d5980 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 TE_OK;. int res
d5990 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 erved = 0;. uni
d59a0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
d59b0 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 unixFile*)id;..
d59c0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d59d0 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
d59e0 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 IOERR_CHECKRESER
d59f0 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 VEDLOCK; );. .
d5a00 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
d5a10 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 ;.. /* Check if
d5a20 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 a thread in thi
d5a30 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 s process holds
d5a40 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 such a lock */.
d5a50 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
d5a60 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b type>SHARED_LOCK
d5a70 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 ){. /* Eithe
d5a80 72 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f r this connectio
d5a90 6e 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 n or some other
d5aa0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 74 68 connection in th
d5ab0 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 0a 20 e same process.
d5ac0 20 20 20 2a 2a 20 68 6f 6c 64 73 20 61 20 6c 6f ** holds a lo
d5ad0 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 ck on the file.
d5ae0 20 4e 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63 No need to chec
d5af0 6b 20 66 75 72 74 68 65 72 2e 20 2a 2f 0a 20 20 k further. */.
d5b00 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a reserved = 1;.
d5b10 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
d5b20 54 68 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 The lock is held
d5b30 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20 if and only if
d5b40 74 68 65 20 6c 6f 63 6b 66 69 6c 65 20 65 78 69 the lockfile exi
d5b50 73 74 73 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 sts */. const
d5b60 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 char *zLockFile
d5b70 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 = (const char*)
d5b80 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
d5b90 6e 74 65 78 74 3b 0a 20 20 20 20 72 65 73 65 72 ntext;. reser
d5ba0 76 65 64 20 3d 20 61 63 63 65 73 73 28 7a 4c 6f ved = access(zLo
d5bb0 63 6b 46 69 6c 65 2c 20 30 29 3d 3d 30 3b 0a 20 ckFile, 0)==0;.
d5bc0 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 }. OSTRACE4("T
d5bd0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 EST WR-LOCK %d %
d5be0 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e d %d\n", pFile->
d5bf0 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 h, rc, reserved)
d5c00 3b 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 ;. *pResOut = r
d5c10 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 eserved;. retur
d5c20 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c n rc;.}../*.** L
d5c30 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 ock the file wit
d5c40 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 h the lock speci
d5c50 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 fied by paramete
d5c60 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 r locktype - one
d5c70 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f .** of the follo
d5c80 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 wing:.**.**
d5c90 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a (1) SHARED_LOCK.
d5ca0 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 ** (2) RESER
d5cb0 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 VED_LOCK.**
d5cc0 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b (3) PENDING_LOCK
d5cd0 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c .** (4) EXCL
d5ce0 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a USIVE_LOCK.**.**
d5cf0 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 Sometimes when
d5d00 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c requesting one l
d5d10 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 ock state, addit
d5d20 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 ional lock state
d5d30 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 s.** are inserte
d5d40 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 d in between. T
d5d50 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 he locking might
d5d60 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 fail on one of
d5d70 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 the later.** tra
d5d80 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 nsitions leaving
d5d90 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 the lock state
d5da0 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 different from w
d5db0 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 hat it started b
d5dc0 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 ut.** still shor
d5dd0 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 t of its goal.
d5de0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 The following ch
d5df0 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c art shows the al
d5e00 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 lowed.** transit
d5e10 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 ions and the ins
d5e20 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 erted intermedia
d5e30 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a te states:.**.**
d5e40 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 UNLOCKED ->
d5e50 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 SHARED.** SHA
d5e60 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a RED -> RESERVED.
d5e70 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 ** SHARED ->
d5e80 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 (PENDING) -> EXC
d5e90 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 LUSIVE.** RES
d5ea0 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e ERVED -> (PENDIN
d5eb0 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a G) -> EXCLUSIVE.
d5ec0 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e ** PENDING ->
d5ed0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a EXCLUSIVE.**.**
d5ee0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 This routine wi
d5ef0 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 ll only increase
d5f00 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 a lock. Use th
d5f10 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 e sqlite3OsUnloc
d5f20 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 k().** routine t
d5f30 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e o lower a lockin
d5f40 67 20 6c 65 76 65 6c 2e 0a 2a 2a 0a 2a 2a 20 57 g level..**.** W
d5f50 69 74 68 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b ith dotfile lock
d5f60 69 6e 67 2c 20 77 65 20 72 65 61 6c 6c 79 20 6f ing, we really o
d5f70 6e 6c 79 20 73 75 70 70 6f 72 74 20 73 74 61 74 nly support stat
d5f80 65 20 28 34 29 3a 20 45 58 43 4c 55 53 49 56 45 e (4): EXCLUSIVE
d5f90 2e 0a 2a 2a 20 42 75 74 20 77 65 20 74 72 61 63 ..** But we trac
d5fa0 6b 20 74 68 65 20 6f 74 68 65 72 20 6c 6f 63 6b k the other lock
d5fb0 69 6e 67 20 6c 65 76 65 6c 73 20 69 6e 74 65 72 ing levels inter
d5fc0 6e 61 6c 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 nally..*/.static
d5fd0 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b int dotlockLock
d5fe0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
d5ff0 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 d, int locktype)
d6000 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 {. unixFile *p
d6010 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
d6020 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a *)id;. int fd;.
d6030 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c char *zLockFil
d6040 65 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69 6c e = (char *)pFil
d6050 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
d6060 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 t;. int rc = SQ
d6070 4c 49 54 45 5f 4f 4b 3b 0a 0a 0a 20 20 2f 2a 20 LITE_OK;... /*
d6080 49 66 20 77 65 20 68 61 76 65 20 61 6e 79 20 6c If we have any l
d6090 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 6c 6f ock, then the lo
d60a0 63 6b 20 66 69 6c 65 20 61 6c 72 65 61 64 79 20 ck file already
d60b0 65 78 69 73 74 73 2e 20 20 41 6c 6c 20 77 65 20 exists. All we
d60c0 68 61 76 65 0a 20 20 2a 2a 20 74 6f 20 64 6f 20 have. ** to do
d60d0 69 73 20 61 64 6a 75 73 74 20 6f 75 72 20 69 6e is adjust our in
d60e0 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f 66 ternal record of
d60f0 20 74 68 65 20 6c 6f 63 6b 20 6c 65 76 65 6c 2e the lock level.
d6100 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c . */. if( pFil
d6110 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f e->locktype > NO
d6120 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 _LOCK ){. pFi
d6130 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c le->locktype = l
d6140 6f 63 6b 74 79 70 65 3b 0a 23 69 66 20 21 4f 53 ocktype;.#if !OS
d6150 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 2f 2a 20 _VXWORKS. /*
d6160 41 6c 77 61 79 73 20 75 70 64 61 74 65 20 74 68 Always update th
d6170 65 20 74 69 6d 65 73 74 61 6d 70 20 6f 6e 20 74 e timestamp on t
d6180 68 65 20 6f 6c 64 20 66 69 6c 65 20 2a 2f 0a 20 he old file */.
d6190 20 20 20 75 74 69 6d 65 73 28 7a 4c 6f 63 6b 46 utimes(zLockF
d61a0 69 6c 65 2c 20 4e 55 4c 4c 29 3b 0a 23 65 6e 64 ile, NULL);.#end
d61b0 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 if. return SQ
d61c0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a LITE_OK;. }. .
d61d0 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63 /* grab an exc
d61e0 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 lusive lock */.
d61f0 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4c 6f 63 6b fd = open(zLock
d6200 46 69 6c 65 2c 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f File,O_RDONLY|O_
d6210 43 52 45 41 54 7c 4f 5f 45 58 43 4c 2c 30 36 30 CREAT|O_EXCL,060
d6220 30 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29 0);. if( fd<0 )
d6230 7b 0a 20 20 20 20 2f 2a 20 66 61 69 6c 65 64 20 {. /* failed
d6240 74 6f 20 6f 70 65 6e 2f 63 72 65 61 74 65 20 74 to open/create t
d6250 68 65 20 66 69 6c 65 2c 20 73 6f 6d 65 6f 6e 65 he file, someone
d6260 20 65 6c 73 65 20 6d 61 79 20 68 61 76 65 20 73 else may have s
d6270 74 6f 6c 65 6e 20 74 68 65 20 6c 6f 63 6b 20 2a tolen the lock *
d6280 2f 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f /. int tErrno
d6290 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 = errno;. if
d62a0 28 20 45 45 58 49 53 54 20 3d 3d 20 74 45 72 72 ( EEXIST == tErr
d62b0 6e 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d no ){. rc =
d62c0 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
d62d0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
d62e0 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f rc = sqliteErro
d62f0 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
d6300 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 tErrno, SQLITE_I
d6310 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 OERR_LOCK);.
d6320 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
d6330 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 ROR(rc) ){.
d6340 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
d6350 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 rno = tErrno;.
d6360 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
d6370 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 0a return rc;. } .
d6380 20 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 20 if( close(fd)
d6390 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 ){. pFile->la
d63a0 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b stErrno = errno;
d63b0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
d63c0 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 _IOERR_CLOSE;.
d63d0 7d 0a 20 20 0a 20 20 2f 2a 20 67 6f 74 20 69 74 }. . /* got it
d63e0 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61 , set the type a
d63f0 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a nd return ok */.
d6400 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
d6410 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 e = locktype;.
d6420 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
d6430 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f .** Lower the lo
d6440 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 cking level on f
d6450 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 ile descriptor p
d6460 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 File to locktype
d6470 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d . locktype.** m
d6480 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f ust be either NO
d6490 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f _LOCK or SHARED_
d64a0 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 LOCK..**.** If t
d64b0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
d64c0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 of the file des
d64d0 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 criptor is alrea
d64e0 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a dy at or below.*
d64f0 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 * the requested
d6500 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 locking level, t
d6510 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
d6520 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 57 68 no-op..**.** Wh
d6530 65 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c en the locking l
d6540 65 76 65 6c 20 72 65 61 63 68 65 73 20 4e 4f 5f evel reaches NO_
d6550 4c 4f 43 4b 2c 20 64 65 6c 65 74 65 20 74 68 65 LOCK, delete the
d6560 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 73 lock file..*/.s
d6570 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 tatic int dotloc
d6580 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f kUnlock(sqlite3_
d6590 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f file *id, int lo
d65a0 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 cktype) {. unix
d65b0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
d65c0 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 63 nixFile*)id;. c
d65d0 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d har *zLockFile =
d65e0 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e (char *)pFile->
d65f0 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a lockingContext;.
d6600 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
d6610 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 );. OSTRACE5("
d6620 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 UNLOCK %d %d wa
d6630 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 s %d pid=%d\n",
d6640 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 pFile->h, lockty
d6650 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e 6c pe,.. pFile->l
d6660 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28 ocktype, getpid(
d6670 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f ));. assert( lo
d6680 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c cktype<=SHARED_L
d6690 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e OCK );. . /* n
d66a0 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65 o-op if possible
d66b0 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d */. if( pFile-
d66c0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 >locktype==lockt
d66d0 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ype ){. retur
d66e0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
d66f0 0a 0a 20 20 2f 2a 20 54 6f 20 64 6f 77 6e 67 72 .. /* To downgr
d6700 61 64 65 20 74 6f 20 73 68 61 72 65 64 2c 20 73 ade to shared, s
d6710 69 6d 70 6c 79 20 75 70 64 61 74 65 20 6f 75 72 imply update our
d6720 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 74 69 6f 6e internal notion
d6730 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6c 6f 63 of the. ** loc
d6740 6b 20 73 74 61 74 65 2e 20 20 4e 6f 20 6e 65 65 k state. No nee
d6750 64 20 74 6f 20 6d 65 73 73 20 77 69 74 68 20 74 d to mess with t
d6760 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e he file on disk.
d6770 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b . */. if( lock
d6780 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
d6790 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K ){. pFile->
d67a0 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 locktype = SHARE
d67b0 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75 D_LOCK;. retu
d67c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
d67d0 7d 0a 20 20 0a 20 20 2f 2a 20 54 6f 20 66 75 6c }. . /* To ful
d67e0 6c 79 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 ly unlock the da
d67f0 74 61 62 61 73 65 2c 20 64 65 6c 65 74 65 20 74 tabase, delete t
d6800 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a he lock file */.
d6810 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
d6820 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 pe==NO_LOCK );.
d6830 20 69 66 28 20 75 6e 6c 69 6e 6b 28 7a 4c 6f 63 if( unlink(zLoc
d6840 6b 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 69 6e kFile) ){. in
d6850 74 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 69 6e t rc = 0;. in
d6860 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f t tErrno = errno
d6870 3b 0a 20 20 20 20 69 66 28 20 45 4e 4f 45 4e 54 ;. if( ENOENT
d6880 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 != tErrno ){.
d6890 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
d68a0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
d68b0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
d68c0 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b E_IOERR_UNLOCK);
d68d0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 49 . }. if( I
d68e0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
d68f0 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d ){. pFile-
d6900 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
d6910 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 rno;. }. r
d6920 65 74 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 eturn rc; . }.
d6930 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d6940 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 = NO_LOCK;. re
d6950 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d6960 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 }../*.** Close a
d6970 20 66 69 6c 65 2e 20 20 4d 61 6b 65 20 73 75 72 file. Make sur
d6980 65 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 62 e the lock has b
d6990 65 65 6e 20 72 65 6c 65 61 73 65 64 20 62 65 66 een released bef
d69a0 6f 72 65 20 63 6c 6f 73 69 6e 67 2e 0a 2a 2f 0a ore closing..*/.
d69b0 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f static int dotlo
d69c0 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f ckClose(sqlite3_
d69d0 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 6e file *id) {. in
d69e0 74 20 72 63 3b 0a 20 20 69 66 28 20 69 64 20 29 t rc;. if( id )
d69f0 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a {. unixFile *
d6a00 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
d6a10 65 2a 29 69 64 3b 0a 20 20 20 20 64 6f 74 6c 6f e*)id;. dotlo
d6a20 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f ckUnlock(id, NO_
d6a30 4c 4f 43 4b 29 3b 0a 20 20 20 20 73 71 6c 69 74 LOCK);. sqlit
d6a40 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e 6c e3_free(pFile->l
d6a50 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a ockingContext);.
d6a60 20 20 7d 0a 20 20 72 63 20 3d 20 63 6c 6f 73 65 }. rc = close
d6a70 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 UnixFile(id);.
d6a80 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 2f 2a 2a return rc;.}./**
d6a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6aa0 20 45 6e 64 20 6f 66 20 74 68 65 20 64 6f 74 2d End of the dot-
d6ab0 66 69 6c 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d file lock implem
d6ac0 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a entation *******
d6ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
d6ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
d6b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
d6b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6b90 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
d6ba0 6c 6f 63 6b 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a lock Locking ***
d6bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
d6bd0 0a 2a 2a 20 55 73 65 20 74 68 65 20 66 6c 6f 63 .** Use the floc
d6be0 6b 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 k() system call
d6bf0 74 6f 20 64 6f 20 66 69 6c 65 20 6c 6f 63 6b 69 to do file locki
d6c00 6e 67 2e 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 ng..**.** flock(
d6c10 29 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69 6b ) locking is lik
d6c20 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 e dot-file locki
d6c30 6e 67 20 69 6e 20 74 68 61 74 20 74 68 65 20 76 ng in that the v
d6c40 61 72 69 6f 75 73 0a 2a 2a 20 66 69 6e 65 2d 67 arious.** fine-g
d6c50 72 61 69 6e 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 rain locking lev
d6c60 65 6c 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 els supported by
d6c70 20 53 51 4c 69 74 65 20 61 72 65 20 63 6f 6c 6c SQLite are coll
d6c80 61 70 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 61 20 apsed into.** a
d6c90 73 69 6e 67 6c 65 20 65 78 63 6c 75 73 69 76 65 single exclusive
d6ca0 20 6c 6f 63 6b 2e 20 20 49 6e 20 6f 74 68 65 72 lock. In other
d6cb0 20 77 6f 72 64 73 2c 20 53 48 41 52 45 44 2c 20 words, SHARED,
d6cc0 52 45 53 45 52 56 45 44 2c 20 61 6e 64 0a 2a 2a RESERVED, and.**
d6cd0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 73 20 61 PENDING locks a
d6ce0 72 65 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e re the same thin
d6cf0 67 20 61 73 20 61 6e 20 45 58 43 4c 55 53 49 56 g as an EXCLUSIV
d6d00 45 20 6c 6f 63 6b 2e 20 20 53 51 4c 69 74 65 0a E lock. SQLite.
d6d10 2a 2a 20 73 74 69 6c 6c 20 77 6f 72 6b 73 20 77 ** still works w
d6d20 68 65 6e 20 79 6f 75 20 64 6f 20 74 68 69 73 2c hen you do this,
d6d30 20 62 75 74 20 63 6f 6e 63 75 72 72 65 6e 63 79 but concurrency
d6d40 20 69 73 20 72 65 64 75 63 65 64 20 73 69 6e 63 is reduced sinc
d6d50 65 0a 2a 2a 20 6f 6e 6c 79 20 61 20 73 69 6e 67 e.** only a sing
d6d60 6c 65 20 70 72 6f 63 65 73 73 20 63 61 6e 20 62 le process can b
d6d70 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 e reading the da
d6d80 74 61 62 61 73 65 20 61 74 20 61 20 74 69 6d 65 tabase at a time
d6d90 2e 0a 2a 2a 0a 2a 2a 20 4f 6d 69 74 20 74 68 69 ..**.** Omit thi
d6da0 73 20 73 65 63 74 69 6f 6e 20 69 66 20 53 51 4c s section if SQL
d6db0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
d6dc0 4e 47 5f 53 54 59 4c 45 20 69 73 20 74 75 72 6e NG_STYLE is turn
d6dd0 65 64 20 6f 66 66 20 6f 72 20 69 66 0a 2a 2a 20 ed off or if.**
d6de0 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 56 58 compiling for VX
d6df0 57 4f 52 4b 53 2e 0a 2a 2f 0a 23 69 66 20 53 51 WORKS..*/.#if SQ
d6e00 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
d6e10 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 ING_STYLE && !OS
d6e20 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20 _VXWORKS../*.**
d6e30 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 This routine che
d6e40 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 cks if there is
d6e50 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
d6e60 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 held on the spec
d6e70 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 ified.** file by
d6e80 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 this or any oth
d6e90 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 er process. If s
d6ea0 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 uch a lock is he
d6eb0 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 ld, set *pResOut
d6ec0 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 .** to a non-zer
d6ed0 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 o value otherwis
d6ee0 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 e *pResOut is se
d6ef0 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 t to zero. The
d6f00 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 return value.**
d6f10 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 is set to SQLITE
d6f20 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f _OK unless an I/
d6f30 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 O error occurs d
d6f40 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b uring lock check
d6f50 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ing..*/.static i
d6f60 6e 74 20 66 6c 6f 63 6b 43 68 65 63 6b 52 65 73 nt flockCheckRes
d6f70 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
d6f80 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
d6f90 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 *pResOut){. int
d6fa0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
d6fb0 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 . int reserved
d6fc0 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 = 0;. unixFile
d6fd0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
d6fe0 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69 6d le*)id;. . Sim
d6ff0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 ulateIOError( re
d7000 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
d7010 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c R_CHECKRESERVEDL
d7020 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 OCK; );. . ass
d7030 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 ert( pFile );.
d7040 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 . /* Check if a
d7050 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 thread in this
d7060 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 process holds su
d7070 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 ch a lock */. i
d7080 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
d7090 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 pe>SHARED_LOCK )
d70a0 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d {. reserved =
d70b0 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 1;. }. . /*
d70c0 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66 Otherwise see if
d70d0 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 some other proc
d70e0 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 2a 2f ess holds it. */
d70f0 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64 . if( !reserved
d7100 20 29 7b 0a 20 20 20 20 2f 2a 20 61 74 74 65 6d ){. /* attem
d7110 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f pt to get the lo
d7120 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 ck */. int lr
d7130 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d c = flock(pFile-
d7140 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c 4f >h, LOCK_EX | LO
d7150 43 4b 5f 4e 42 29 3b 0a 20 20 20 20 69 66 28 20 CK_NB);. if(
d7160 21 6c 72 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a !lrc ){. /*
d7170 20 67 6f 74 20 74 68 65 20 6c 6f 63 6b 2c 20 75 got the lock, u
d7180 6e 6c 6f 63 6b 20 69 74 20 2a 2f 0a 20 20 20 20 nlock it */.
d7190 20 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 lrc = flock(pF
d71a0 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 ile->h, LOCK_UN)
d71b0 3b 0a 20 20 20 20 20 20 69 66 20 28 20 6c 72 63 ;. if ( lrc
d71c0 20 29 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 ) {. int
d71d0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b tErrno = errno;
d71e0 0a 20 20 20 20 20 20 20 20 2f 2a 20 75 6e 6c 6f . /* unlo
d71f0 63 6b 20 66 61 69 6c 65 64 20 77 69 74 68 20 61 ck failed with a
d7200 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 n error */.
d7210 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 74 65 45 lrc = sqliteE
d7220 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
d7230 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
d7240 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b E_IOERR_UNLOCK);
d7250 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53 . if( IS
d7260 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 _LOCK_ERROR(lrc)
d7270 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 ){. pF
d7280 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d7290 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 tErrno;.
d72a0 20 20 20 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20 rc = lrc;.
d72b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
d72c0 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 } else {.
d72d0 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 int tErrno = e
d72e0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 73 65 rrno;. rese
d72f0 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 rved = 1;.
d7300 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 /* someone else
d7310 6d 69 67 68 74 20 68 61 76 65 20 69 74 20 72 65 might have it re
d7320 73 65 72 76 65 64 20 2a 2f 0a 20 20 20 20 20 20 served */.
d7330 6c 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f lrc = sqliteErro
d7340 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
d7350 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 tErrno, SQLITE_I
d7360 4f 45 52 52 5f 4c 4f 43 4b 29 3b 20 0a 20 20 20 OERR_LOCK); .
d7370 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
d7380 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 RROR(lrc) ){.
d7390 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
d73a0 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a Errno = tErrno;.
d73b0 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 rc = lrc
d73c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
d73d0 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 }. OSTRACE4("
d73e0 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 TEST WR-LOCK %d
d73f0 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d %d %d\n", pFile-
d7400 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 >h, rc, reserved
d7410 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
d7420 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c E_IGNORE_FLOCK_L
d7430 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 69 66 28 OCK_ERRORS. if(
d7440 20 28 72 63 20 26 20 53 51 4c 49 54 45 5f 49 4f (rc & SQLITE_IO
d7450 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 ERR) == SQLITE_I
d7460 4f 45 52 52 20 29 7b 0a 20 20 20 20 72 63 20 3d OERR ){. rc =
d7470 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
d7480 72 65 73 65 72 76 65 64 3d 31 3b 0a 20 20 7d 0a reserved=1;. }.
d7490 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
d74a0 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f _IGNORE_FLOCK_LO
d74b0 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 2a CK_ERRORS */. *
d74c0 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 pResOut = reserv
d74d0 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ed;. return rc;
d74e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 .}../*.** Lock t
d74f0 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 he file with the
d7500 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 lock specified
d7510 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 by parameter loc
d7520 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f ktype - one.** o
d7530 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a f the following:
d7540 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 .**.** (1) S
d7550 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 HARED_LOCK.**
d7560 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c (2) RESERVED_L
d7570 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 OCK.** (3) P
d7580 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 ENDING_LOCK.**
d7590 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 (4) EXCLUSIVE
d75a0 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 _LOCK.**.** Some
d75b0 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 times when reque
d75c0 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 sting one lock s
d75d0 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c tate, additional
d75e0 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 lock states.**
d75f0 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 are inserted in
d7600 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f between. The lo
d7610 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c cking might fail
d7620 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c on one of the l
d7630 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 ater.** transiti
d7640 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 ons leaving the
d7650 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 lock state diffe
d7660 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 rent from what i
d7670 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a t started but.**
d7680 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 still short of
d7690 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 its goal. The f
d76a0 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 ollowing chart s
d76b0 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 hows the allowed
d76c0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
d76d0 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 and the inserted
d76e0 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 intermediate st
d76f0 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 ates:.**.** U
d7700 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 NLOCKED -> SHARE
d7710 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
d7720 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 > RESERVED.**
d7730 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 SHARED -> (PEND
d7740 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
d7750 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 E.** RESERVED
d7760 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
d7770 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
d7780 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c PENDING -> EXCL
d7790 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 USIVE.**.** floc
d77a0 6b 28 29 20 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 k() only really
d77b0 73 75 70 70 6f 72 74 20 45 58 43 4c 55 53 49 56 support EXCLUSIV
d77c0 45 20 6c 6f 63 6b 73 2e 20 20 57 65 20 74 72 61 E locks. We tra
d77d0 63 6b 20 69 6e 74 65 72 6d 65 64 69 61 74 65 0a ck intermediate.
d77e0 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 73 20 69 ** lock states i
d77f0 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 n the sqlite3_fi
d7800 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20 62 75 le structure, bu
d7810 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 53 48 41 52 t all locks SHAR
d7820 45 44 20 6f 72 0a 2a 2a 20 61 62 6f 76 65 20 61 ED or.** above a
d7830 72 65 20 72 65 61 6c 6c 79 20 45 58 43 4c 55 53 re really EXCLUS
d7840 49 56 45 20 6c 6f 63 6b 73 20 61 6e 64 20 65 78 IVE locks and ex
d7850 63 6c 75 64 65 20 61 6c 6c 20 6f 74 68 65 72 20 clude all other
d7860 70 72 6f 63 65 73 73 65 73 20 66 72 6f 6d 0a 2a processes from.*
d7870 2a 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c * access the fil
d7880 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f e..**.** This ro
d7890 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 utine will only
d78a0 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e increase a lock.
d78b0 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 Use the sqlite
d78c0 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 3OsUnlock().** r
d78d0 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 outine to lower
d78e0 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e a locking level.
d78f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 .*/.static int f
d7900 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 lockLock(sqlite3
d7910 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c _file *id, int l
d7920 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 69 6e 74 ocktype) {. int
d7930 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
d7940 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
d7950 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
d7960 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 id;.. assert( p
d7970 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 69 66 File );.. /* if
d7980 20 77 65 20 61 6c 72 65 61 64 79 20 68 61 76 65 we already have
d7990 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 a lock, it is e
d79a0 78 63 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a xclusive. . **
d79b0 20 4a 75 73 74 20 61 64 6a 75 73 74 20 6c 65 76 Just adjust lev
d79c0 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f el and punt on o
d79d0 75 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 utta here. */.
d79e0 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if (pFile->lockt
d79f0 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b ype > NO_LOCK) {
d7a00 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b . pFile->lock
d7a10 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b type = locktype;
d7a20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d7a30 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 TE_OK;. }. .
d7a40 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63 6c 75 /* grab an exclu
d7a50 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 0a sive lock */. .
d7a60 20 20 69 66 20 28 66 6c 6f 63 6b 28 70 46 69 6c if (flock(pFil
d7a70 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 e->h, LOCK_EX |
d7a80 4c 4f 43 4b 5f 4e 42 29 29 20 7b 0a 20 20 20 20 LOCK_NB)) {.
d7a90 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 int tErrno = err
d7aa0 6e 6f 3b 0a 20 20 20 20 2f 2a 20 64 69 64 6e 27 no;. /* didn'
d7ab0 74 20 67 65 74 2c 20 6d 75 73 74 20 62 65 20 62 t get, must be b
d7ac0 75 73 79 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 usy */. rc =
d7ad0 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
d7ae0 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
d7af0 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c , SQLITE_IOERR_L
d7b00 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 OCK);. if( IS
d7b10 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 _LOCK_ERROR(rc)
d7b20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
d7b30 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 lastErrno = tErr
d7b40 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c no;. }. } el
d7b50 73 65 20 7b 0a 20 20 20 20 2f 2a 20 67 6f 74 20 se {. /* got
d7b60 69 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 it, set the type
d7b70 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a and return ok *
d7b80 2f 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 /. pFile->loc
d7b90 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 ktype = locktype
d7ba0 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 ;. }. OSTRACE4
d7bb0 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 ("LOCK %d %s
d7bc0 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %s\n", pFile->h,
d7bd0 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f locktypeName(lo
d7be0 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 20 cktype), .
d7bf0 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f rc==SQLITE_
d7c00 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 OK ? "ok" : "fai
d7c10 6c 65 64 22 29 3b 0a 23 69 66 64 65 66 20 53 51 led");.#ifdef SQ
d7c20 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 LITE_IGNORE_FLOC
d7c30 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 K_LOCK_ERRORS.
d7c40 69 66 28 20 28 72 63 20 26 20 53 51 4c 49 54 45 if( (rc & SQLITE
d7c50 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 _IOERR) == SQLIT
d7c60 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 72 E_IOERR ){. r
d7c70 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
d7c80 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 . }.#endif /* S
d7c90 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f QLITE_IGNORE_FLO
d7ca0 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a CK_LOCK_ERRORS *
d7cb0 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d /. return rc;.}
d7cc0 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 .../*.** Lower t
d7cd0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
d7ce0 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 on file descrip
d7cf0 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 tor pFile to loc
d7d00 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 ktype. locktype
d7d10 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 .** must be eith
d7d20 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 er NO_LOCK or SH
d7d30 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a ARED_LOCK..**.**
d7d40 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 If the locking
d7d50 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c level of the fil
d7d60 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 e descriptor is
d7d70 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 already at or be
d7d80 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 low.** the reque
d7d90 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 sted locking lev
d7da0 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 el, this routine
d7db0 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
d7dc0 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b static int flock
d7dd0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 Unlock(sqlite3_f
d7de0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
d7df0 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 ktype) {. unixF
d7e00 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
d7e10 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 ixFile*)id;. .
d7e20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
d7e30 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e ;. OSTRACE5("UN
d7e40 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 LOCK %d %d was
d7e50 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 %d pid=%d\n", pF
d7e60 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 ile->h, locktype
d7e70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 46 69 ,. pFi
d7e80 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 le->locktype, ge
d7e90 74 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 72 tpid());. asser
d7ea0 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 t( locktype<=SHA
d7eb0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 RED_LOCK );. .
d7ec0 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 /* no-op if pos
d7ed0 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 sible */. if( p
d7ee0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
d7ef0 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 locktype ){.
d7f00 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d7f10 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 ;. }. . /* sh
d7f20 61 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65 ared can just be
d7f30 20 73 65 74 20 62 65 63 61 75 73 65 20 77 65 20 set because we
d7f40 61 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 65 always have an e
d7f50 78 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 xclusive */. if
d7f60 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 (locktype==SHAR
d7f70 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 ED_LOCK) {. p
d7f80 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
d7f90 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 locktype;. r
d7fa0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d7fb0 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c . }. . /* no,
d7fc0 20 72 65 61 6c 6c 79 2c 20 75 6e 6c 6f 63 6b 2e really, unlock.
d7fd0 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 66 */. int rc = f
d7fe0 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c lock(pFile->h, L
d7ff0 4f 43 4b 5f 55 4e 29 3b 0a 20 20 69 66 20 28 72 OCK_UN);. if (r
d8000 63 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 2c 20 c) {. int r,
d8010 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
d8020 20 20 20 20 72 20 3d 20 73 71 6c 69 74 65 45 72 r = sqliteEr
d8030 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f rorFromPosixErro
d8040 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 r(tErrno, SQLITE
d8050 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a _IOERR_UNLOCK);.
d8060 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f if( IS_LOCK_
d8070 45 52 52 4f 52 28 72 29 20 29 7b 0a 20 20 20 20 ERROR(r) ){.
d8080 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d8090 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
d80a0 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }.#ifdef SQLITE
d80b0 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f _IGNORE_FLOCK_LO
d80c0 43 4b 5f 45 52 52 4f 52 53 0a 20 20 20 20 69 66 CK_ERRORS. if
d80d0 28 20 28 72 20 26 20 53 51 4c 49 54 45 5f 49 4f ( (r & SQLITE_IO
d80e0 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 ERR) == SQLITE_I
d80f0 4f 45 52 52 20 29 7b 0a 20 20 20 20 20 20 72 20 OERR ){. r
d8100 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 = SQLITE_BUSY;.
d8110 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 }.#endif /* S
d8120 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f QLITE_IGNORE_FLO
d8130 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a CK_LOCK_ERRORS *
d8140 2f 0a 20 20 20 20 0a 20 20 20 20 72 65 74 75 72 /. . retur
d8150 6e 20 72 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a n r;. } else {.
d8160 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
d8170 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 ype = NO_LOCK;.
d8180 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
d8190 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a _OK;. }.}../*.*
d81a0 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a * Close a file..
d81b0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c */.static int fl
d81c0 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 ockClose(sqlite3
d81d0 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 _file *id) {. i
d81e0 66 28 20 69 64 20 29 7b 0a 20 20 20 20 66 6c 6f f( id ){. flo
d81f0 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f ckUnlock(id, NO_
d8200 4c 4f 43 4b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 LOCK);. }. ret
d8210 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c urn closeUnixFil
d8220 65 28 69 64 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 e(id);.}..#endif
d8230 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c /* SQLITE_ENABL
d8240 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
d8250 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 20 2a 2f && !OS_VXWORK */
d8260 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
d8270 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 ****** End of th
d8280 65 20 66 6c 6f 63 6b 20 6c 6f 63 6b 20 69 6d 70 e flock lock imp
d8290 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a lementation ****
d82a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d82b0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
d82c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d82d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d82e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d82f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8300 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
d8310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8350 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
d8360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
d8370 6e 20 4e 61 6d 65 64 20 53 65 6d 61 70 68 6f 72 n Named Semaphor
d8380 65 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a e Locking ******
d8390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d83a0 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 64 20 73 **.**.** Named s
d83b0 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 emaphore locking
d83c0 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 is only support
d83d0 65 64 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a ed on VxWorks..*
d83e0 2a 0a 2a 2a 20 53 65 6d 61 70 68 6f 72 65 20 6c *.** Semaphore l
d83f0 6f 63 6b 69 6e 67 20 69 73 20 6c 69 6b 65 20 64 ocking is like d
d8400 6f 74 2d 6c 6f 63 6b 20 61 6e 64 20 66 6c 6f 63 ot-lock and floc
d8410 6b 20 69 6e 20 74 68 61 74 20 69 74 20 72 65 61 k in that it rea
d8420 6c 6c 79 20 6f 6e 6c 79 0a 2a 2a 20 73 75 70 70 lly only.** supp
d8430 6f 72 74 73 20 45 58 43 4c 55 53 49 56 45 20 6c orts EXCLUSIVE l
d8440 6f 63 6b 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 20 ocking. Only a
d8450 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 63 single process c
d8460 61 6e 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 an read or write
d8470 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
d8480 20 66 69 6c 65 20 61 74 20 61 20 74 69 6d 65 2e file at a time.
d8490 20 20 54 68 69 73 20 72 65 64 75 63 65 73 20 70 This reduces p
d84a0 6f 74 65 6e 74 69 61 6c 20 63 6f 6e 63 75 72 72 otential concurr
d84b0 65 6e 63 79 2c 20 62 75 74 0a 2a 2a 20 6d 61 6b ency, but.** mak
d84c0 65 73 20 74 68 65 20 6c 6f 63 6b 20 69 6d 70 6c es the lock impl
d84d0 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 63 68 20 ementation much
d84e0 65 61 73 69 65 72 2e 0a 2a 2f 0a 23 69 66 20 4f easier..*/.#if O
d84f0 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a S_VXWORKS../*.**
d8500 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
d8510 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 ecks if there is
d8520 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
d8530 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 held on the spe
d8540 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 cified.** file b
d8550 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 y this or any ot
d8560 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 her process. If
d8570 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 such a lock is h
d8580 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 eld, set *pResOu
d8590 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 t.** to a non-ze
d85a0 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 ro value otherwi
d85b0 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 se *pResOut is s
d85c0 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 et to zero. The
d85d0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a return value.**
d85e0 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 is set to SQLIT
d85f0 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 E_OK unless an I
d8600 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 /O error occurs
d8610 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 during lock chec
d8620 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 king..*/.static
d8630 69 6e 74 20 73 65 6d 43 68 65 63 6b 52 65 73 65 int semCheckRese
d8640 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 rvedLock(sqlite3
d8650 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a _file *id, int *
d8660 70 52 65 73 4f 75 74 29 20 7b 0a 20 20 69 6e 74 pResOut) {. int
d8670 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
d8680 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 . int reserved
d8690 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 = 0;. unixFile
d86a0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
d86b0 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c le*)id;.. Simul
d86c0 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 ateIOError( retu
d86d0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
d86e0 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 CHECKRESERVEDLOC
d86f0 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 K; );. . asser
d8700 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f t( pFile );.. /
d8710 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 * Check if a thr
d8720 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 ead in this proc
d8730 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 ess holds such a
d8740 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 lock */. if( p
d8750 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 File->locktype>S
d8760 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 HARED_LOCK ){.
d8770 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a reserved = 1;.
d8780 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 }. . /* Othe
d8790 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d rwise see if som
d87a0 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 e other process
d87b0 68 6f 6c 64 73 20 69 74 2e 20 2a 2f 0a 20 20 69 holds it. */. i
d87c0 66 28 20 21 72 65 73 65 72 76 65 64 20 29 7b 0a f( !reserved ){.
d87d0 20 20 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 sem_t *pSem
d87e0 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e = pFile->pOpen->
d87f0 70 53 65 6d 3b 0a 20 20 20 20 73 74 72 75 63 74 pSem;. struct
d8800 20 73 74 61 74 20 73 74 61 74 42 75 66 3b 0a 0a stat statBuf;..
d8810 20 20 20 20 69 66 28 20 73 65 6d 5f 74 72 79 77 if( sem_tryw
d8820 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 7b ait(pSem)==-1 ){
d8830 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e . int tErrn
d8840 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 o = errno;.
d8850 20 69 66 28 20 45 41 47 41 49 4e 20 21 3d 20 74 if( EAGAIN != t
d8860 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 Errno ){.
d8870 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f rc = sqliteErro
d8880 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
d8890 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 tErrno, SQLITE_I
d88a0 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 OERR_CHECKRESERV
d88b0 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 EDLOCK);.
d88c0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
d88d0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
d88e0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
d88f0 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c /* someone el
d8900 73 65 20 68 61 73 20 74 68 65 20 6c 6f 63 6b 20 se has the lock
d8910 77 68 65 6e 20 77 65 20 61 72 65 20 69 6e 20 4e when we are in N
d8920 4f 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 20 20 O_LOCK */.
d8930 20 20 72 65 73 65 72 76 65 64 20 3d 20 28 70 46 reserved = (pF
d8940 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 ile->locktype <
d8950 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 SHARED_LOCK);.
d8960 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
d8970 0a 20 20 20 20 20 20 2f 2a 20 77 65 20 63 6f 75 . /* we cou
d8980 6c 64 20 68 61 76 65 20 69 74 20 69 66 20 77 65 ld have it if we
d8990 20 77 61 6e 74 20 69 74 20 2a 2f 0a 20 20 20 20 want it */.
d89a0 20 20 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 sem_post(pSem)
d89b0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 ;. }. }. OS
d89c0 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d TRACE4("TEST WR-
d89d0 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 LOCK %d %d %d\n"
d89e0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 , pFile->h, rc,
d89f0 72 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70 reserved);.. *p
d8a00 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 ResOut = reserve
d8a10 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a d;. return rc;.
d8a20 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 }../*.** Lock th
d8a30 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 e file with the
d8a40 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 lock specified b
d8a50 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b y parameter lock
d8a60 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 type - one.** of
d8a70 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
d8a80 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 **.** (1) SH
d8a90 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 ARED_LOCK.**
d8aa0 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f (2) RESERVED_LO
d8ab0 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 CK.** (3) PE
d8ac0 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 NDING_LOCK.**
d8ad0 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f (4) EXCLUSIVE_
d8ae0 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 LOCK.**.** Somet
d8af0 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 imes when reques
d8b00 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 ting one lock st
d8b10 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 ate, additional
d8b20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 lock states.** a
d8b30 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 re inserted in b
d8b40 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 etween. The loc
d8b50 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 king might fail
d8b60 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 on one of the la
d8b70 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f ter.** transitio
d8b80 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c ns leaving the l
d8b90 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 ock state differ
d8ba0 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 ent from what it
d8bb0 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 started but.**
d8bc0 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 still short of i
d8bd0 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f ts goal. The fo
d8be0 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 llowing chart sh
d8bf0 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a ows the allowed.
d8c00 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 ** transitions a
d8c10 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 nd the inserted
d8c20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 intermediate sta
d8c30 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e tes:.**.** UN
d8c40 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 LOCKED -> SHARED
d8c50 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
d8c60 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 RESERVED.**
d8c70 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 SHARED -> (PENDI
d8c80 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
d8c90 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 .** RESERVED
d8ca0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
d8cb0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
d8cc0 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 PENDING -> EXCLU
d8cd0 53 49 56 45 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70 SIVE.**.** Semap
d8ce0 68 6f 72 65 20 6c 6f 63 6b 73 20 6f 6e 6c 79 20 hore locks only
d8cf0 72 65 61 6c 6c 79 20 73 75 70 70 6f 72 74 20 45 really support E
d8d00 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20 XCLUSIVE locks.
d8d10 20 57 65 20 74 72 61 63 6b 20 69 6e 74 65 72 6d We track interm
d8d20 65 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73 ediate.** lock s
d8d30 74 61 74 65 73 20 69 6e 20 74 68 65 20 73 71 6c tates in the sql
d8d40 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74 ite3_file struct
d8d50 75 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63 ure, but all loc
d8d60 6b 73 20 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20 ks SHARED or.**
d8d70 61 62 6f 76 65 20 61 72 65 20 72 65 61 6c 6c 79 above are really
d8d80 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 EXCLUSIVE locks
d8d90 20 61 6e 64 20 65 78 63 6c 75 64 65 20 61 6c 6c and exclude all
d8da0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 other processes
d8db0 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20 from.** access
d8dc0 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 the file..**.**
d8dd0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c This routine wil
d8de0 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 l only increase
d8df0 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 a lock. Use the
d8e00 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b sqlite3OsUnlock
d8e10 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f ().** routine to
d8e20 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 lower a locking
d8e30 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 level..*/.stati
d8e40 63 20 69 6e 74 20 73 65 6d 4c 6f 63 6b 28 73 71 c int semLock(sq
d8e50 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
d8e60 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a int locktype) {.
d8e70 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d8e80 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
d8e90 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 73 d;. int fd;. s
d8ea0 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 em_t *pSem = pFi
d8eb0 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b le->pOpen->pSem;
d8ec0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
d8ed0 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 69 66 20 TE_OK;.. /* if
d8ee0 77 65 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 we already have
d8ef0 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 78 a lock, it is ex
d8f00 63 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20 clusive. . **
d8f10 4a 75 73 74 20 61 64 6a 75 73 74 20 6c 65 76 65 Just adjust leve
d8f20 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75 l and punt on ou
d8f30 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69 tta here. */. i
d8f40 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f (pFile->lockty
d8f50 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a pe > NO_LOCK) {.
d8f60 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
d8f70 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a ype = locktype;.
d8f80 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
d8f90 4f 4b 3b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d OK;. goto sem
d8fa0 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 _end_lock;. }.
d8fb0 20 0a 20 20 2f 2a 20 6c 6f 63 6b 20 73 65 6d 61 . /* lock sema
d8fc0 70 68 6f 72 65 20 6e 6f 77 20 62 75 74 20 62 61 phore now but ba
d8fd0 69 6c 20 6f 75 74 20 77 68 65 6e 20 61 6c 72 65 il out when alre
d8fe0 61 64 79 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 ady locked. */.
d8ff0 20 69 66 28 20 73 65 6d 5f 74 72 79 77 61 69 74 if( sem_trywait
d9000 28 70 53 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 (pSem)==-1 ){.
d9010 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 rc = SQLITE_BU
d9020 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d SY;. goto sem
d9030 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a _end_lock;. }..
d9040 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 /* got it, set
d9050 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65 the type and re
d9060 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 turn ok */. pFi
d9070 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c le->locktype = l
d9080 6f 63 6b 74 79 70 65 3b 0a 0a 20 73 65 6d 5f 65 ocktype;.. sem_e
d9090 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 72 65 74 75 72 nd_lock:. retur
d90a0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c n rc;.}../*.** L
d90b0 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 ower the locking
d90c0 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 level on file d
d90d0 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 escriptor pFile
d90e0 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f to locktype. lo
d90f0 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 cktype.** must b
d9100 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b e either NO_LOCK
d9110 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e or SHARED_LOCK.
d9120 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f .**.** If the lo
d9130 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 cking level of t
d9140 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
d9150 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 or is already at
d9160 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 or below.** the
d9170 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 requested locki
d9180 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 ng level, this r
d9190 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
d91a0 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 p..*/.static int
d91b0 20 73 65 6d 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 semUnlock(sqlit
d91c0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
d91d0 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 locktype) {. u
d91e0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
d91f0 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
d9200 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 sem_t *pSem =
d9210 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 pFile->pOpen->pS
d9220 65 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 em;.. assert( p
d9230 46 69 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 File );. assert
d9240 28 20 70 53 65 6d 20 29 3b 0a 20 20 4f 53 54 52 ( pSem );. OSTR
d9250 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 ACE5("UNLOCK %d
d9260 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 %d was %d pid=%
d9270 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
d9280 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 20 20 70 46 locktype,.. pF
d9290 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 ile->locktype, g
d92a0 65 74 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 etpid());. asse
d92b0 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 rt( locktype<=SH
d92c0 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a ARED_LOCK );. .
d92d0 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f /* no-op if po
d92e0 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 ssible */. if(
d92f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
d9300 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 =locktype ){.
d9310 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
d9320 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 K;. }. . /* s
d9330 68 61 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 hared can just b
d9340 65 20 73 65 74 20 62 65 63 61 75 73 65 20 77 65 e set because we
d9350 20 61 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 always have an
d9360 65 78 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 exclusive */. i
d9370 66 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 f (locktype==SHA
d9380 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 RED_LOCK) {.
d9390 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
d93a0 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 = locktype;.
d93b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d93c0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f ;. }. . /* no
d93d0 2c 20 72 65 61 6c 6c 79 20 75 6e 6c 6f 63 6b 2e , really unlock.
d93e0 20 2a 2f 0a 20 20 69 66 20 28 20 73 65 6d 5f 70 */. if ( sem_p
d93f0 6f 73 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 20 ost(pSem)==-1 )
d9400 7b 0a 20 20 20 20 69 6e 74 20 72 63 2c 20 74 45 {. int rc, tE
d9410 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
d9420 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 rc = sqliteErr
d9430 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
d9440 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
d9450 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 IOERR_UNLOCK);.
d9460 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
d9470 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 RROR(rc) ){.
d9480 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d9490 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
d94a0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 }. return rc
d94b0 3b 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e ; . }. pFile->
d94c0 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f locktype = NO_LO
d94d0 43 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c CK;. return SQL
d94e0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a ITE_OK;.}../*. *
d94f0 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a * Close a file..
d9500 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 */.static int s
d9510 65 6d 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f emClose(sqlite3_
d9520 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 file *id) {. if
d9530 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 ( id ){. unix
d9540 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
d9550 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 nixFile*)id;.
d9560 20 73 65 6d 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e semUnlock(id, N
d9570 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 61 73 73 O_LOCK);. ass
d9580 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 ert( pFile );.
d9590 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 unixEnterMutex
d95a0 28 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4c ();. releaseL
d95b0 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 ockInfo(pFile->p
d95c0 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61 Lock);. relea
d95d0 73 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d seOpenCnt(pFile-
d95e0 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 75 6e 69 >pOpen);. uni
d95f0 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 xLeaveMutex();.
d9600 20 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 closeUnixFile
d9610 28 69 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 (id);. }. retu
d9620 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
d9630 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 .#endif /* OS_VX
d9640 57 4f 52 4b 53 20 2a 2f 0a 2f 2a 0a 2a 2a 20 4e WORKS */./*.** N
d9650 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c amed semaphore l
d9660 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 61 ocking is only a
d9670 76 61 69 6c 61 62 6c 65 20 6f 6e 20 56 78 57 6f vailable on VxWo
d9680 72 6b 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a rks..**.********
d9690 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 ******* End of t
d96a0 68 65 20 6e 61 6d 65 64 20 73 65 6d 61 70 68 6f he named semapho
d96b0 72 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e re lock implemen
d96c0 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a tation *********
d96d0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
d96e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d96f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9720 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a ******/.../*****
d9730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9770 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
d9780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9790 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 41 46 50 20 ***** Begin AFP
d97a0 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a Locking ********
d97b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d97c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
d97d0 41 46 50 20 69 73 20 74 68 65 20 41 70 70 6c 65 AFP is the Apple
d97e0 20 46 69 6c 69 6e 67 20 50 72 6f 74 6f 63 6f 6c Filing Protocol
d97f0 2e 20 20 41 46 50 20 69 73 20 61 20 6e 65 74 77 . AFP is a netw
d9800 6f 72 6b 20 66 69 6c 65 73 79 73 74 65 6d 20 66 ork filesystem f
d9810 6f 75 6e 64 0a 2a 2a 20 6f 6e 20 41 70 70 6c 65 ound.** on Apple
d9820 20 4d 61 63 69 6e 74 6f 73 68 20 63 6f 6d 70 75 Macintosh compu
d9830 74 65 72 73 20 2d 20 62 6f 74 68 20 4f 53 39 20 ters - both OS9
d9840 61 6e 64 20 4f 53 58 2e 0a 2a 2a 0a 2a 2a 20 54 and OSX..**.** T
d9850 68 69 72 64 2d 70 61 72 74 79 20 69 6d 70 6c 65 hird-party imple
d9860 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 41 46 mentations of AF
d9870 50 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e P are available.
d9880 20 20 42 75 74 20 74 68 69 73 20 63 6f 64 65 20 But this code
d9890 68 65 72 65 0a 2a 2a 20 6f 6e 6c 79 20 77 6f 72 here.** only wor
d98a0 6b 73 20 6f 6e 20 4f 53 58 2e 0a 2a 2f 0a 0a 23 ks on OSX..*/..#
d98b0 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 if defined(__APP
d98c0 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f LE__) && SQLITE_
d98d0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
d98e0 54 59 4c 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 TYLE./*.** The a
d98f0 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 fpLockingContext
d9900 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 structure conta
d9910 69 6e 73 20 61 6c 6c 20 61 66 70 20 6c 6f 63 6b ins all afp lock
d9920 20 73 70 65 63 69 66 69 63 20 73 74 61 74 65 0a specific state.
d9930 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
d9940 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 t afpLockingCont
d9950 65 78 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f ext afpLockingCo
d9960 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20 61 66 ntext;.struct af
d9970 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 pLockingContext
d9980 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e {. unsigned lon
d9990 67 20 6c 6f 6e 67 20 73 68 61 72 65 64 42 79 74 g long sharedByt
d99a0 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 e;. const char
d99b0 2a 64 62 50 61 74 68 3b 20 20 20 20 20 20 20 20 *dbPath;
d99c0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
d99d0 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f the open file */
d99e0 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 42 79 74 65 .};..struct Byte
d99f0 52 61 6e 67 65 4c 6f 63 6b 50 42 32 0a 7b 0a 20 RangeLockPB2.{.
d9a00 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c unsigned long l
d9a10 6f 6e 67 20 6f 66 66 73 65 74 3b 20 20 20 20 20 ong offset;
d9a20 20 20 20 2f 2a 20 6f 66 66 73 65 74 20 74 6f 20 /* offset to
d9a30 66 69 72 73 74 20 62 79 74 65 20 74 6f 20 6c 6f first byte to lo
d9a40 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 ck */. unsigned
d9a50 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67 74 long long lengt
d9a60 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e 62 72 h; /* nbr
d9a70 20 6f 66 20 62 79 74 65 73 20 74 6f 20 6c 6f 63 of bytes to loc
d9a80 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 k */. unsigned
d9a90 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 52 61 6e long long retRan
d9aa0 67 65 53 74 61 72 74 3b 20 2f 2a 20 6e 62 72 20 geStart; /* nbr
d9ab0 6f 66 20 31 73 74 20 62 79 74 65 20 6c 6f 63 6b of 1st byte lock
d9ac0 65 64 20 69 66 20 73 75 63 63 65 73 73 66 75 6c ed if successful
d9ad0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
d9ae0 68 61 72 20 75 6e 4c 6f 63 6b 46 6c 61 67 3b 20 har unLockFlag;
d9af0 20 20 20 20 20 20 20 20 2f 2a 20 31 20 3d 20 75 /* 1 = u
d9b00 6e 6c 6f 63 6b 2c 20 30 20 3d 20 6c 6f 63 6b 20 nlock, 0 = lock
d9b10 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
d9b20 61 72 20 73 74 61 72 74 45 6e 64 46 6c 61 67 3b ar startEndFlag;
d9b30 20 20 20 20 20 20 20 2f 2a 20 31 3d 72 65 6c 20 /* 1=rel
d9b40 74 6f 20 65 6e 64 20 6f 66 20 66 6f 72 6b 2c 20 to end of fork,
d9b50 30 3d 72 65 6c 20 74 6f 20 73 74 61 72 74 20 2a 0=rel to start *
d9b60 2f 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20 /. int fd;
d9b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9b80 20 20 20 20 20 20 2f 2a 20 66 69 6c 65 20 64 65 /* file de
d9b90 73 63 20 74 6f 20 61 73 73 6f 63 20 74 68 69 73 sc to assoc this
d9ba0 20 6c 6f 63 6b 20 77 69 74 68 20 2a 2f 0a 7d 3b lock with */.};
d9bb0 0a 0a 23 64 65 66 69 6e 65 20 61 66 70 66 73 42 ..#define afpfsB
d9bc0 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43 yteRangeLock2FSC
d9bd0 54 4c 20 20 20 20 20 20 20 20 5f 49 4f 57 52 28 TL _IOWR(
d9be0 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74 20 'z', 23, struct
d9bf0 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 ByteRangeLockPB2
d9c00 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 )../*.** This is
d9c10 20 61 20 75 74 69 6c 69 74 79 20 66 6f 72 20 73 a utility for s
d9c20 65 74 74 69 6e 67 20 6f 72 20 63 6c 65 61 72 69 etting or cleari
d9c30 6e 67 20 61 20 62 69 74 2d 72 61 6e 67 65 20 6c ng a bit-range l
d9c40 6f 63 6b 20 6f 6e 20 61 6e 0a 2a 2a 20 41 46 50 ock on an.** AFP
d9c50 20 66 69 6c 65 73 79 73 74 65 6d 2e 0a 2a 2a 20 filesystem..**
d9c60 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
d9c70 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2c E_OK on success,
d9c80 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6f 6e 20 SQLITE_BUSY on
d9c90 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 failure..*/.stat
d9ca0 69 63 20 69 6e 74 20 61 66 70 53 65 74 4c 6f 63 ic int afpSetLoc
d9cb0 6b 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 k(. const char
d9cc0 2a 70 61 74 68 2c 20 20 20 20 20 20 20 20 20 20 *path,
d9cd0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
d9ce0 68 65 20 66 69 6c 65 20 74 6f 20 62 65 20 6c 6f he file to be lo
d9cf0 63 6b 65 64 20 6f 72 20 75 6e 6c 6f 63 6b 65 64 cked or unlocked
d9d00 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a */. unixFile *
d9d10 70 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20 20 pFile,
d9d20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c /* Open fil
d9d30 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 e descriptor on
d9d40 70 61 74 68 20 2a 2f 0a 20 20 75 6e 73 69 67 6e path */. unsign
d9d50 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 ed long long off
d9d60 73 65 74 2c 20 20 20 20 20 2f 2a 20 46 69 72 73 set, /* Firs
d9d70 74 20 62 79 74 65 20 74 6f 20 62 65 20 6c 6f 63 t byte to be loc
d9d80 6b 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 ked */. unsigne
d9d90 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67 d long long leng
d9da0 74 68 2c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 th, /* Numbe
d9db0 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 6c 6f r of bytes to lo
d9dc0 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 73 65 74 4c ck */. int setL
d9dd0 6f 63 6b 46 6c 61 67 20 20 20 20 20 20 20 20 20 ockFlag
d9de0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 /* True t
d9df0 6f 20 73 65 74 20 6c 6f 63 6b 2e 20 20 46 61 6c o set lock. Fal
d9e00 73 65 20 74 6f 20 63 6c 65 61 72 20 6c 6f 63 6b se to clear lock
d9e10 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 */.){. struct
d9e20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 ByteRangeLockPB2
d9e30 20 70 62 3b 0a 20 20 69 6e 74 20 65 72 72 3b 0a pb;. int err;.
d9e40 20 20 0a 20 20 70 62 2e 75 6e 4c 6f 63 6b 46 6c . pb.unLockFl
d9e50 61 67 20 3d 20 73 65 74 4c 6f 63 6b 46 6c 61 67 ag = setLockFlag
d9e60 20 3f 20 30 20 3a 20 31 3b 0a 20 20 70 62 2e 73 ? 0 : 1;. pb.s
d9e70 74 61 72 74 45 6e 64 46 6c 61 67 20 3d 20 30 3b tartEndFlag = 0;
d9e80 0a 20 20 70 62 2e 6f 66 66 73 65 74 20 3d 20 6f . pb.offset = o
d9e90 66 66 73 65 74 3b 0a 20 20 70 62 2e 6c 65 6e 67 ffset;. pb.leng
d9ea0 74 68 20 3d 20 6c 65 6e 67 74 68 3b 20 0a 20 20 th = length; .
d9eb0 70 62 2e 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68 pb.fd = pFile->h
d9ec0 3b 0a 20 20 0a 20 20 4f 53 54 52 41 43 45 36 28 ;. . OSTRACE6(
d9ed0 22 41 46 50 53 45 54 4c 4f 43 4b 20 5b 25 73 5d "AFPSETLOCK [%s]
d9ee0 20 66 6f 72 20 25 64 25 73 20 69 6e 20 72 61 6e for %d%s in ran
d9ef0 67 65 20 25 6c 6c 78 3a 25 6c 6c 78 5c 6e 22 2c ge %llx:%llx\n",
d9f00 20 0a 20 20 20 20 28 73 65 74 4c 6f 63 6b 46 6c . (setLockFl
d9f10 61 67 3f 22 4f 4e 22 3a 22 4f 46 46 22 29 2c 20 ag?"ON":"OFF"),
d9f20 70 46 69 6c 65 2d 3e 68 2c 20 28 70 62 2e 66 64 pFile->h, (pb.fd
d9f30 3d 3d 2d 31 3f 22 5b 74 65 73 74 76 61 6c 2d 31 ==-1?"[testval-1
d9f40 5d 22 3a 22 22 29 2c 0a 20 20 20 20 6f 66 66 73 ]":""),. offs
d9f50 65 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 65 et, length);. e
d9f60 72 72 20 3d 20 66 73 63 74 6c 28 70 61 74 68 2c rr = fsctl(path,
d9f70 20 61 66 70 66 73 42 79 74 65 52 61 6e 67 65 4c afpfsByteRangeL
d9f80 6f 63 6b 32 46 53 43 54 4c 2c 20 26 70 62 2c 20 ock2FSCTL, &pb,
d9f90 30 29 3b 0a 20 20 69 66 20 28 20 65 72 72 3d 3d 0);. if ( err==
d9fa0 2d 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 -1 ) {. int r
d9fb0 63 3b 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e c;. int tErrn
d9fc0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 4f o = errno;. O
d9fd0 53 54 52 41 43 45 34 28 22 41 46 50 53 45 54 4c STRACE4("AFPSETL
d9fe0 4f 43 4b 20 66 61 69 6c 65 64 20 74 6f 20 66 73 OCK failed to fs
d9ff0 63 74 6c 28 29 20 27 25 73 27 20 25 64 20 25 73 ctl() '%s' %d %s
da000 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
da010 20 20 70 61 74 68 2c 20 74 45 72 72 6e 6f 2c 20 path, tErrno,
da020 73 74 72 65 72 72 6f 72 28 74 45 72 72 6e 6f 29 strerror(tErrno)
da030 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 );.#ifdef SQLITE
da040 5f 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b _IGNORE_AFP_LOCK
da050 5f 45 52 52 4f 52 53 0a 20 20 20 20 72 63 20 3d _ERRORS. rc =
da060 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 23 65 SQLITE_BUSY;.#e
da070 6c 73 65 0a 20 20 20 20 72 63 20 3d 20 73 71 6c lse. rc = sql
da080 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
da090 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 0a 20 xError(tErrno,.
da0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
da0b0 20 20 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f setLockFlag ?
da0c0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f SQLITE_IOERR_LO
da0d0 43 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 CK : SQLITE_IOER
da0e0 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69 R_UNLOCK);.#endi
da0f0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f f /* SQLITE_IGNO
da100 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f RE_AFP_LOCK_ERRO
da110 52 53 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 RS */. if( IS
da120 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 _LOCK_ERROR(rc)
da130 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
da140 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 lastErrno = tErr
da150 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 no;. }. re
da160 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 65 6c 73 turn rc;. } els
da170 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 e {. return S
da180 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a QLITE_OK;. }.}.
da190 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
da1a0 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 ine checks if th
da1b0 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45 ere is a RESERVE
da1c0 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 D lock held on t
da1d0 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 he specified.**
da1e0 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 file by this or
da1f0 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 any other proces
da200 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 s. If such a loc
da210 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a k is held, set *
da220 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 pResOut.** to a
da230 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f non-zero value o
da240 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 therwise *pResOu
da250 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f t is set to zero
da260 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 . The return va
da270 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f lue.** is set to
da280 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 SQLITE_OK unles
da290 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f s an I/O error o
da2a0 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 ccurs during loc
da2b0 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 k checking..*/.s
da2c0 74 61 74 69 63 20 69 6e 74 20 61 66 70 43 68 65 tatic int afpChe
da2d0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 ckReservedLock(s
da2e0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
da2f0 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a int *pResOut){.
da300 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
da310 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 E_OK;. int rese
da320 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 rved = 0;. unix
da330 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
da340 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a nixFile*)id;. .
da350 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
da360 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
da370 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 _IOERR_CHECKRESE
da380 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a RVEDLOCK; );. .
da390 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
da3a0 29 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 );. afpLockingC
da3b0 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 ontext *context
da3c0 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e = (afpLockingCon
da3d0 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c text *) pFile->l
da3e0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 ockingContext;.
da3f0 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 . /* Check if
da400 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 a thread in this
da410 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 process holds s
da420 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 uch a lock */.
da430 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
da440 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ype>SHARED_LOCK
da450 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 ){. reserved
da460 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a = 1;. }. . /*
da470 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 Otherwise see i
da480 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f f some other pro
da490 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 cess holds it..
da4a0 20 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65 */. if( !rese
da4b0 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 6c rved ){. /* l
da4c0 6f 63 6b 20 74 68 65 20 52 45 53 45 52 56 45 44 ock the RESERVED
da4d0 20 62 79 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74 byte */. int
da4e0 20 6c 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 lrc = afpSetLoc
da4f0 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 k(context->dbPat
da500 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 h, pFile, RESERV
da510 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 20 20 ED_BYTE, 1,1);
da520 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f . if( SQLITE_
da530 4f 4b 3d 3d 6c 72 63 20 29 7b 0a 20 20 20 20 20 OK==lrc ){.
da540 20 2f 2a 20 69 66 20 77 65 20 73 75 63 63 65 65 /* if we succee
da550 64 65 64 20 69 6e 20 74 61 6b 69 6e 67 20 74 68 ded in taking th
da560 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2c e reserved lock,
da570 20 75 6e 6c 6f 63 6b 20 69 74 20 74 6f 20 72 65 unlock it to re
da580 73 74 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 74 store. ** t
da590 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 he original stat
da5a0 65 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d e */. lrc =
da5b0 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 afpSetLock(cont
da5c0 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 ext->dbPath, pFi
da5d0 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 le, RESERVED_BYT
da5e0 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 E, 1, 0);. }
da5f0 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 2f 2a 20 else {. /*
da600 69 66 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20 if we failed to
da610 67 65 74 20 74 68 65 20 6c 6f 63 6b 20 74 68 65 get the lock the
da620 6e 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d n someone else m
da630 75 73 74 20 68 61 76 65 20 69 74 20 2a 2f 0a 20 ust have it */.
da640 20 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 reserved =
da650 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 1;. }. if(
da660 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c IS_LOCK_ERROR(l
da670 72 63 29 20 29 7b 0a 20 20 20 20 20 20 72 63 3d rc) ){. rc=
da680 6c 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 lrc;. }. }.
da690 20 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 . OSTRACE4("TE
da6a0 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 ST WR-LOCK %d %d
da6b0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 %d\n", pFile->h
da6c0 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b , rc, reserved);
da6d0 0a 20 20 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d . . *pResOut =
da6e0 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 reserved;. ret
da6f0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
da700 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 Lock the file w
da710 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 ith the lock spe
da720 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 cified by parame
da730 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f ter locktype - o
da740 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c ne.** of the fol
da750 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 lowing:.**.**
da760 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 (1) SHARED_LOC
da770 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 K.** (2) RES
da780 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 ERVED_LOCK.**
da790 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f (3) PENDING_LO
da7a0 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 CK.** (4) EX
da7b0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a CLUSIVE_LOCK.**.
da7c0 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 ** Sometimes whe
da7d0 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 n requesting one
da7e0 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 lock state, add
da7f0 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 itional lock sta
da800 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 tes.** are inser
da810 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 ted in between.
da820 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 The locking mig
da830 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f ht fail on one o
da840 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 f the later.** t
da850 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 ransitions leavi
da860 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 ng the lock stat
da870 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d e different from
da880 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 what it started
da890 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 but.** still sh
da8a0 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e ort of its goal.
da8b0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
da8c0 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 chart shows the
da8d0 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 allowed.** trans
da8e0 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 itions and the i
da8f0 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 nserted intermed
da900 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a iate states:.**.
da910 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d ** UNLOCKED -
da920 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 > SHARED.** S
da930 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 HARED -> RESERVE
da940 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
da950 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 > (PENDING) -> E
da960 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 XCLUSIVE.** R
da970 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 ESERVED -> (PEND
da980 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
da990 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 E.** PENDING
da9a0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a -> EXCLUSIVE.**.
da9b0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
da9c0 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 will only increa
da9d0 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 se a lock. Use
da9e0 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c the sqlite3OsUnl
da9f0 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 ock().** routine
daa00 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b to lower a lock
daa10 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 ing level..*/.st
daa20 61 74 69 63 20 69 6e 74 20 61 66 70 4c 6f 63 6b atic int afpLock
daa30 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
daa40 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 d, int locktype)
daa50 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
daa60 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 ITE_OK;. unixFi
daa70 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
daa80 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 xFile*)id;. afp
daa90 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a LockingContext *
daaa0 63 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f context = (afpLo
daab0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 ckingContext *)
daac0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
daad0 6e 74 65 78 74 3b 0a 20 20 0a 20 20 61 73 73 65 ntext;. . asse
daae0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f rt( pFile );. O
daaf0 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20 STRACE5("LOCK
dab00 20 25 64 20 25 73 20 77 61 73 20 25 73 20 70 69 %d %s was %s pi
dab10 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e d=%d\n", pFile->
dab20 68 2c 0a 20 20 20 20 20 20 20 20 20 6c 6f 63 6b h,. lock
dab30 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 typeName(locktyp
dab40 65 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 e), locktypeName
dab50 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 (pFile->locktype
dab60 29 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 ), getpid());..
dab70 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 /* If there is
dab80 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f already a lock o
dab90 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d f this type or m
daba0 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 ore restrictive
dabb0 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78 on the. ** unix
dabc0 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 File, do nothing
dabd0 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20 . Don't use the
dabe0 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 afp_end_lock: ex
dabf0 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a it path, as. **
dac00 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
dac10 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 ) hasn't been ca
dac20 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 lled yet.. */.
dac30 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
dac40 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 type>=locktype )
dac50 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 {. OSTRACE3("
dac60 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 6f 6b LOCK %d %s ok
dac70 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29 5c (already held)\
dac80 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 n", pFile->h,.
dac90 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 locktyp
daca0 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 eName(locktype))
dacb0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
dacc0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f ITE_OK;. }.. /
dacd0 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 * Make sure the
dace0 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 locking sequence
dacf0 20 69 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f is correct. */
dad00 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
dad10 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c ->locktype!=NO_L
dad20 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d OCK || locktype=
dad30 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
dad40 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
dad50 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b pe!=PENDING_LOCK
dad60 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f );. assert( lo
dad70 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 cktype!=RESERVED
dad80 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e _LOCK || pFile->
dad90 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
dada0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a _LOCK );. . /*
dadb0 20 54 68 69 73 20 6d 75 74 65 78 20 69 73 20 6e This mutex is n
dadc0 65 65 64 65 64 20 62 65 63 61 75 73 65 20 70 46 eeded because pF
dadd0 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68 ile->pLock is sh
dade0 61 72 65 64 20 61 63 72 6f 73 73 20 74 68 72 65 ared across thre
dadf0 61 64 73 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45 ads. */. unixE
dae00 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 nterMutex();..
dae10 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 /* Make sure the
dae20 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 current thread
dae30 6f 77 6e 73 20 74 68 65 20 70 46 69 6c 65 2e 0a owns the pFile..
dae40 20 20 2a 2f 0a 20 20 72 63 20 3d 20 74 72 61 6e */. rc = tran
dae50 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 70 46 sferOwnership(pF
dae60 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d ile);. if( rc!=
dae70 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
dae80 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 unixLeaveMutex(
dae90 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 );. return rc
daea0 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 20 2f 2a 20 ;. }. . /*
daeb0 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 A PENDING lock i
daec0 73 20 6e 65 65 64 65 64 20 62 65 66 6f 72 65 20 s needed before
daed0 61 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 acquiring a SHAR
daee0 45 44 20 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f ED lock and befo
daef0 72 65 0a 20 20 2a 2a 20 61 63 71 75 69 72 69 6e re. ** acquirin
daf00 67 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c g an EXCLUSIVE l
daf10 6f 63 6b 2e 20 20 46 6f 72 20 74 68 65 20 53 48 ock. For the SH
daf20 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50 ARED lock, the P
daf30 45 4e 44 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a ENDING will. **
daf40 20 62 65 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 be released..
daf50 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 */. if( locktyp
daf60 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a e==SHARED_LOCK .
daf70 20 20 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 || (lockty
daf80 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f pe==EXCLUSIVE_LO
daf90 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 CK && pFile->loc
dafa0 6b 74 79 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f ktype<PENDING_LO
dafb0 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 CK). ){. int
dafc0 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 66 61 69 failed;. fai
dafd0 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b led = afpSetLock
dafe0 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 (context->dbPath
daff0 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e 47 , pFile, PENDING
db000 5f 42 59 54 45 2c 20 31 2c 20 31 29 3b 0a 20 20 _BYTE, 1, 1);.
db010 20 20 69 66 20 28 66 61 69 6c 65 64 29 20 7b 0a if (failed) {.
db020 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 rc = faile
db030 64 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66 d;. goto af
db040 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 p_end_lock;.
db050 7d 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 66 }. }. . /* If
db060 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f control gets to
db070 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 this point, the
db080 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f 20 61 68 n actually go ah
db090 65 61 64 20 61 6e 64 20 6d 61 6b 65 0a 20 20 2a ead and make. *
db0a0 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 * operating syst
db0b0 65 6d 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 em calls for the
db0c0 20 73 70 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e specified lock.
db0d0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b . */. if( lock
db0e0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
db0f0 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 2c K ){. int lk,
db100 20 6c 72 63 31 2c 20 6c 72 63 32 2c 20 6c 72 63 lrc1, lrc2, lrc
db110 31 45 72 72 6e 6f 3b 0a 20 20 20 20 0a 20 20 20 1Errno;. .
db120 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 65 20 /* Now get the
db130 72 65 61 64 2d 6c 6f 63 6b 20 53 48 41 52 45 44 read-lock SHARED
db140 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 2f 2a 20 _LOCK */. /*
db150 6e 6f 74 65 20 74 68 61 74 20 74 68 65 20 71 75 note that the qu
db160 61 6c 69 74 79 20 6f 66 20 74 68 65 20 72 61 6e ality of the ran
db170 64 6f 6d 6e 65 73 73 20 64 6f 65 73 6e 27 74 20 domness doesn't
db180 6d 61 74 74 65 72 20 74 68 61 74 20 6d 75 63 68 matter that much
db190 20 2a 2f 0a 20 20 20 20 6c 6b 20 3d 20 72 61 6e */. lk = ran
db1a0 64 6f 6d 28 29 3b 20 0a 20 20 20 20 63 6f 6e 74 dom(); . cont
db1b0 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65 20 ext->sharedByte
db1c0 3d 20 28 6c 6b 20 26 20 30 78 37 66 66 66 66 66 = (lk & 0x7fffff
db1d0 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 5a 45 ff)%(SHARED_SIZE
db1e0 20 2d 20 31 29 3b 0a 20 20 20 20 6c 72 63 31 20 - 1);. lrc1
db1f0 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e = afpSetLock(con
db200 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 text->dbPath, pF
db210 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 ile, .
db220 53 48 41 52 45 44 5f 46 49 52 53 54 2b 63 6f 6e SHARED_FIRST+con
db230 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65 text->sharedByte
db240 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 , 1, 1);. if(
db250 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c IS_LOCK_ERROR(l
db260 72 63 31 29 20 29 7b 0a 20 20 20 20 20 20 6c 72 rc1) ){. lr
db270 63 31 45 72 72 6e 6f 20 3d 20 70 46 69 6c 65 2d c1Errno = pFile-
db280 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 >lastErrno;.
db290 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 }. /* Drop th
db2a0 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44 e temporary PEND
db2b0 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 ING lock */.
db2c0 6c 72 63 32 20 3d 20 61 66 70 53 65 74 4c 6f 63 lrc2 = afpSetLoc
db2d0 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 k(context->dbPat
db2e0 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e h, pFile, PENDIN
db2f0 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 G_BYTE, 1, 0);.
db300 20 20 20 0a 20 20 20 20 69 66 28 20 49 53 5f 4c . if( IS_L
db310 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31 29 20 OCK_ERROR(lrc1)
db320 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d ) {. pFile-
db330 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 72 63 >lastErrno = lrc
db340 31 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 1Errno;. rc
db350 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 20 20 67 = lrc1;. g
db360 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b oto afp_end_lock
db370 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28 ;. } else if(
db380 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c IS_LOCK_ERROR(l
db390 72 63 32 29 20 29 7b 0a 20 20 20 20 20 20 72 63 rc2) ){. rc
db3a0 20 3d 20 6c 72 63 32 3b 0a 20 20 20 20 20 20 67 = lrc2;. g
db3b0 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b oto afp_end_lock
db3c0 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28 ;. } else if(
db3d0 20 6c 72 63 31 20 21 3d 20 53 51 4c 49 54 45 5f lrc1 != SQLITE_
db3e0 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 72 63 20 OK ) {. rc
db3f0 3d 20 6c 72 63 31 3b 0a 20 20 20 20 7d 20 65 6c = lrc1;. } el
db400 73 65 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 se {. pFile
db410 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 ->locktype = SHA
db420 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 RED_LOCK;.
db430 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c pFile->pOpen->nL
db440 6f 63 6b 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d ock++;. }. }
db450 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 else{. /* The
db460 20 72 65 71 75 65 73 74 20 77 61 73 20 66 6f 72 request was for
db470 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 a RESERVED or E
db480 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 XCLUSIVE lock.
db490 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 73 73 It is. ** ass
db4a0 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 65 20 umed that there
db4b0 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 67 is a SHARED or g
db4c0 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 reater lock on t
db4d0 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61 he file. ** a
db4e0 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 lready.. */.
db4f0 20 20 20 69 6e 74 20 66 61 69 6c 65 64 20 3d 20 int failed =
db500 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 0;. assert( 0
db510 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 !=pFile->locktyp
db520 65 20 29 3b 0a 20 20 20 20 69 66 20 28 6c 6f 63 e );. if (loc
db530 6b 74 79 70 65 20 3e 3d 20 52 45 53 45 52 56 45 ktype >= RESERVE
db540 44 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d D_LOCK && pFile-
db550 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 52 45 53 45 >locktype < RESE
db560 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 RVED_LOCK) {.
db570 20 20 20 20 20 2f 2a 20 41 63 71 75 69 72 65 20 /* Acquire
db580 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
db590 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69 6c 65 */. faile
db5a0 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 d = afpSetLock(c
db5b0 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 ontext->dbPath,
db5c0 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44 5f pFile, RESERVED_
db5d0 42 59 54 45 2c 20 31 2c 31 29 3b 0a 20 20 20 20 BYTE, 1,1);.
db5e0 7d 0a 20 20 20 20 69 66 20 28 21 66 61 69 6c 65 }. if (!faile
db5f0 64 20 26 26 20 6c 6f 63 6b 74 79 70 65 20 3d 3d d && locktype ==
db600 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 EXCLUSIVE_LOCK)
db610 20 7b 0a 20 20 20 20 20 20 2f 2a 20 41 63 71 75 {. /* Acqu
db620 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 ire an EXCLUSIVE
db630 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 lock */.
db640 20 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 . /* Remov
db650 65 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 e the shared loc
db660 6b 20 62 65 66 6f 72 65 20 74 72 79 69 6e 67 20 k before trying
db670 74 68 65 20 72 61 6e 67 65 2e 20 20 77 65 27 6c the range. we'l
db680 6c 20 6e 65 65 64 20 74 6f 20 0a 20 20 20 20 20 l need to .
db690 20 2a 2a 20 72 65 65 73 74 61 62 6c 69 73 68 20 ** reestablish
db6a0 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 the shared lock
db6b0 69 66 20 77 65 20 63 61 6e 27 74 20 67 65 74 20 if we can't get
db6c0 74 68 65 20 20 61 66 70 55 6e 6c 6f 63 6b 0a 20 the afpUnlock.
db6d0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
db6e0 28 20 21 28 66 61 69 6c 65 64 20 3d 20 61 66 70 ( !(failed = afp
db6f0 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d SetLock(context-
db700 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 >dbPath, pFile,
db710 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b 0a 20 SHARED_FIRST +.
db720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
db730 20 20 20 20 20 20 20 20 63 6f 6e 74 65 78 74 2d context-
db740 3e 73 68 61 72 65 64 42 79 74 65 2c 20 31 2c 20 >sharedByte, 1,
db750 30 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 0)) ){. i
db760 6e 74 20 66 61 69 6c 65 64 32 20 3d 20 53 51 4c nt failed2 = SQL
db770 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 ITE_OK;.
db780 2f 2a 20 6e 6f 77 20 61 74 74 65 6d 6d 70 74 20 /* now attemmpt
db790 74 6f 20 67 65 74 20 74 68 65 20 65 78 63 6c 75 to get the exclu
db7a0 73 69 76 65 20 6c 6f 63 6b 20 72 61 6e 67 65 20 sive lock range
db7b0 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69 6c 65 */. faile
db7c0 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 d = afpSetLock(c
db7d0 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 ontext->dbPath,
db7e0 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 pFile, SHARED_FI
db7f0 52 53 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20 RST, .
db800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
db810 20 20 20 20 20 53 48 41 52 45 44 5f 53 49 5a 45 SHARED_SIZE
db820 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 , 1);. if
db830 28 20 66 61 69 6c 65 64 20 26 26 20 28 66 61 69 ( failed && (fai
db840 6c 65 64 32 20 3d 20 61 66 70 53 65 74 4c 6f 63 led2 = afpSetLoc
db850 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 k(context->dbPat
db860 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 h, pFile, .
db870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
db880 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b SHARED_FIRST +
db890 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 context->shared
db8a0 42 79 74 65 2c 20 31 2c 20 31 29 29 20 29 7b 0a Byte, 1, 1)) ){.
db8b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6e /* Can
db8c0 27 74 20 72 65 65 73 74 61 62 6c 69 73 68 20 74 't reestablish t
db8d0 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 he shared lock.
db8e0 20 53 71 6c 69 74 65 20 63 61 6e 27 74 20 64 65 Sqlite can't de
db8f0 61 6c 2c 20 74 68 69 73 20 69 73 0a 20 20 20 20 al, this is.
db900 20 20 20 20 20 20 2a 2a 20 61 20 63 72 69 74 69 ** a criti
db910 63 61 6c 20 49 2f 4f 20 65 72 72 6f 72 0a 20 20 cal I/O error.
db920 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
db930 20 20 20 20 20 72 63 20 3d 20 28 28 66 61 69 6c rc = ((fail
db940 65 64 20 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 ed & SQLITE_IOER
db950 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 R) == SQLITE_IOE
db960 52 52 29 20 3f 20 66 61 69 6c 65 64 32 20 3a 20 RR) ? failed2 :
db970 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
db980 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 SQLITE_IOERR_LOC
db990 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 K;. got
db9a0 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a o afp_end_lock;.
db9b0 20 20 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20 } .
db9c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
db9d0 72 63 20 3d 20 66 61 69 6c 65 64 3b 20 0a 20 20 rc = failed; .
db9e0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
db9f0 69 66 28 20 66 61 69 6c 65 64 20 29 7b 0a 20 20 if( failed ){.
dba00 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b rc = failed;
dba10 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 . }. }. .
dba20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
dba30 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K ){. pFile->
dba40 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
dba50 79 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ype;. }else if(
dba60 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 locktype==EXCLU
dba70 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 SIVE_LOCK ){.
dba80 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
dba90 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b = PENDING_LOCK;
dbaa0 0a 20 20 7d 0a 20 20 0a 61 66 70 5f 65 6e 64 5f . }. .afp_end_
dbab0 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76 lock:. unixLeav
dbac0 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52 eMutex();. OSTR
dbad0 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 25 64 ACE4("LOCK %d
dbae0 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65 %s %s\n", pFile
dbaf0 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d ->h, locktypeNam
dbb00 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 e(locktype), .
dbb10 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 rc==SQLIT
dbb20 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 E_OK ? "ok" : "f
dbb30 61 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 75 72 ailed");. retur
dbb40 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c n rc;.}../*.** L
dbb50 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 ower the locking
dbb60 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 level on file d
dbb70 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 escriptor pFile
dbb80 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f to locktype. lo
dbb90 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 cktype.** must b
dbba0 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b e either NO_LOCK
dbbb0 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e or SHARED_LOCK.
dbbc0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f .**.** If the lo
dbbd0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 cking level of t
dbbe0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
dbbf0 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 or is already at
dbc00 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 or below.** the
dbc10 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 requested locki
dbc20 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 ng level, this r
dbc30 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
dbc40 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 p..*/.static int
dbc50 20 61 66 70 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 afpUnlock(sqlit
dbc60 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
dbc70 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 69 locktype) {. i
dbc80 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
dbc90 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 K;. unixFile *p
dbca0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
dbcb0 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 *)id;. afpLocki
dbcc0 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 ngContext *pCtx
dbcd0 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e = (afpLockingCon
dbce0 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c text *) pFile->l
dbcf0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a ockingContext;..
dbd00 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
dbd10 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 );. OSTRACE5("U
dbd20 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 NLOCK %d %d was
dbd30 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 %d pid=%d\n", p
dbd40 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
dbd50 65 2c 0a 20 20 20 20 20 20 20 20 20 70 46 69 6c e,. pFil
dbd60 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 e->locktype, get
dbd70 70 69 64 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 pid());.. asser
dbd80 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 t( locktype<=SHA
dbd90 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 RED_LOCK );. if
dbda0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
dbdb0 65 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 e<=locktype ){.
dbdc0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
dbdd0 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 _OK;. }. if( C
dbde0 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 70 46 HECK_THREADID(pF
dbdf0 69 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 ile) ){. retu
dbe00 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 rn SQLITE_MISUSE
dbe10 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65 ;. }. unixEnte
dbe20 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 rMutex();. if(
dbe30 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
dbe40 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SHARED_LOCK ){.
dbe50 20 20 20 0a 20 20 20 20 69 66 28 20 70 46 69 6c . if( pFil
dbe60 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 e->locktype==EXC
dbe70 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 LUSIVE_LOCK ){.
dbe80 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 rc = afpSet
dbe90 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 Lock(pCtx->dbPat
dbea0 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44 h, pFile, SHARED
dbeb0 5f 46 49 52 53 54 2c 20 53 48 41 52 45 44 5f 53 _FIRST, SHARED_S
dbec0 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 IZE, 0);. i
dbed0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
dbee0 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 && locktype==SH
dbef0 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
dbf00 20 20 20 20 20 2f 2a 20 6f 6e 6c 79 20 72 65 2d /* only re-
dbf10 65 73 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 establish the sh
dbf20 61 72 65 64 20 6c 6f 63 6b 20 69 66 20 6e 65 63 ared lock if nec
dbf30 65 73 73 61 72 79 20 2a 2f 0a 20 20 20 20 20 20 essary */.
dbf40 20 20 69 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b int sharedLock
dbf50 42 79 74 65 20 3d 20 53 48 41 52 45 44 5f 46 49 Byte = SHARED_FI
dbf60 52 53 54 2b 70 43 74 78 2d 3e 73 68 61 72 65 64 RST+pCtx->shared
dbf70 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 72 63 Byte;. rc
dbf80 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 = afpSetLock(pC
dbf90 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c tx->dbPath, pFil
dbfa0 65 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 e, sharedLockByt
dbfb0 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 e, 1, 1);.
dbfc0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
dbfd0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
dbfe0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
dbff0 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 >=PENDING_LOCK )
dc000 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66 70 {. rc = afp
dc010 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 SetLock(pCtx->db
dc020 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e Path, pFile, PEN
dc030 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 DING_BYTE, 1, 0)
dc040 3b 0a 20 20 20 20 7d 20 0a 20 20 20 20 69 66 28 ;. } . if(
dc050 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
dc060 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 & pFile->locktyp
dc070 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e>=RESERVED_LOCK
dc080 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 ){. rc = a
dc090 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e fpSetLock(pCtx->
dc0a0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 dbPath, pFile, R
dc0b0 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c ESERVED_BYTE, 1,
dc0c0 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 0);. }. }el
dc0d0 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d se if( locktype=
dc0e0 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 =NO_LOCK ){.
dc0f0 2f 2a 20 63 6c 65 61 72 20 74 68 65 20 73 68 61 /* clear the sha
dc100 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 red lock */.
dc110 69 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 int sharedLockBy
dc120 74 65 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 te = SHARED_FIRS
dc130 54 2b 70 43 74 78 2d 3e 73 68 61 72 65 64 42 79 T+pCtx->sharedBy
dc140 74 65 3b 0a 20 20 20 20 72 63 20 3d 20 61 66 70 te;. rc = afp
dc150 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 SetLock(pCtx->db
dc160 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 73 68 61 Path, pFile, sha
dc170 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 redLockByte, 1,
dc180 30 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 0);. }.. if( r
dc190 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
dc1a0 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 if( locktype
dc1b0 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ==NO_LOCK ){.
dc1c0 20 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 struct unixOp
dc1d0 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 enCnt *pOpen = p
dc1e0 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 File->pOpen;.
dc1f0 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d pOpen->nLock-
dc200 2d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 -;. assert(
dc210 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30 pOpen->nLock>=0
dc220 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f );. if( pO
dc230 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b pen->nLock==0 ){
dc240 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6c . rc = cl
dc250 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 70 46 osePendingFds(pF
dc260 69 6c 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ile);. }.
dc270 20 20 7d 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 }. }. unixLe
dc280 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 aveMutex();. if
dc290 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
dc2a0 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f ){. pFile->lo
dc2b0 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 cktype = locktyp
dc2c0 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 e;. }. return
dc2d0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f rc;.}../*.** Clo
dc2e0 73 65 20 61 20 66 69 6c 65 20 26 20 63 6c 65 61 se a file & clea
dc2f0 6e 75 70 20 41 46 50 20 73 70 65 63 69 66 69 63 nup AFP specific
dc300 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 locking context
dc310 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 .*/.static int
dc320 61 66 70 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 afpClose(sqlite3
dc330 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 _file *id) {. i
dc340 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 f( id ){. uni
dc350 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
dc360 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 unixFile*)id;.
dc370 20 20 61 66 70 55 6e 6c 6f 63 6b 28 69 64 2c 20 afpUnlock(id,
dc380 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e NO_LOCK);. un
dc390 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a ixEnterMutex();.
dc3a0 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 if( pFile->p
dc3b0 4f 70 65 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70 Open && pFile->p
dc3c0 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 Open->nLock ){.
dc3d0 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 /* If there
dc3e0 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 are outstanding
dc3f0 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 locks, do not a
dc400 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68 ctually close th
dc410 65 20 66 69 6c 65 20 6a 75 73 74 0a 20 20 20 20 e file just.
dc420 20 20 2a 2a 20 79 65 74 20 62 65 63 61 75 73 65 ** yet because
dc430 20 74 68 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 that would clea
dc440 72 20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 r those locks.
dc450 49 6e 73 74 65 61 64 2c 20 61 64 64 20 74 68 65 Instead, add the
dc460 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 file. ** d
dc470 65 73 63 72 69 70 74 6f 72 20 74 6f 20 70 4f 70 escriptor to pOp
dc480 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 2e 20 20 49 en->aPending. I
dc490 74 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 t will be automa
dc4a0 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20 77 tically closed w
dc4b0 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 hen. ** the
dc4c0 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c last lock is cl
dc4d0 65 61 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a eared.. */.
dc4e0 20 20 20 20 20 20 73 65 74 50 65 6e 64 69 6e 67 setPending
dc4f0 46 64 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d Fd(pFile);. }
dc500 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 6e . releaseOpen
dc510 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e Cnt(pFile->pOpen
dc520 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 );. sqlite3_f
dc530 72 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 ree(pFile->locki
dc540 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 ngContext);.
dc550 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 closeUnixFile(id
dc560 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 );. unixLeave
dc570 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 Mutex();. }. r
dc580 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
dc590 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 .}..#endif /* de
dc5a0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
dc5b0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
dc5c0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
dc5d0 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 */./*.** The cod
dc5e0 65 20 61 62 6f 76 65 20 69 73 20 74 68 65 20 41 e above is the A
dc5f0 46 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e FP lock implemen
dc600 74 61 74 69 6f 6e 2e 20 20 54 68 65 20 63 6f 64 tation. The cod
dc610 65 20 69 73 20 73 70 65 63 69 66 69 63 0a 2a 2a e is specific.**
dc620 20 74 6f 20 4d 61 63 4f 53 58 20 61 6e 64 20 64 to MacOSX and d
dc630 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 oes not work on
dc640 6f 74 68 65 72 20 75 6e 69 78 20 70 6c 61 74 66 other unix platf
dc650 6f 72 6d 73 2e 20 20 4e 6f 20 61 6c 74 65 72 6e orms. No altern
dc660 61 74 69 76 65 0a 2a 2a 20 69 73 20 61 76 61 69 ative.** is avai
dc670 6c 61 62 6c 65 2e 20 20 49 66 20 79 6f 75 20 64 lable. If you d
dc680 6f 6e 27 74 20 63 6f 6d 70 69 6c 65 20 66 6f 72 on't compile for
dc690 20 61 20 6d 61 63 2c 20 74 68 65 6e 20 74 68 65 a mac, then the
dc6a0 20 22 75 6e 69 78 2d 61 66 70 22 0a 2a 2a 20 56 "unix-afp".** V
dc6b0 46 53 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 FS is not availa
dc6c0 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ble..**.********
dc6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
dc6e0 64 20 6f 66 20 74 68 65 20 41 46 50 20 6c 6f 63 d of the AFP loc
dc6f0 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e k implementation
dc700 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
dc710 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
dc720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dc730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dc740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dc750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dc760 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a ******/.../*****
dc770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dc780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dc790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dc7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dc7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
dc7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 6e 2d 6c ********** Non-l
dc7d0 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 ocking sqlite3_f
dc7e0 69 6c 65 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a ile methods ****
dc7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dc800 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
dc810 54 68 65 20 6e 65 78 74 20 64 69 76 69 73 69 6f The next divisio
dc820 6e 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 n contains imple
dc830 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 61 mentations for a
dc840 6c 6c 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 ll methods of th
dc850 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 e .** sqlite3_fi
dc860 6c 65 20 6f 62 6a 65 63 74 20 6f 74 68 65 72 20 le object other
dc870 74 68 61 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 than the locking
dc880 20 6d 65 74 68 6f 64 73 2e 20 20 54 68 65 20 6c methods. The l
dc890 6f 63 6b 69 6e 67 0a 2a 2a 20 6d 65 74 68 6f 64 ocking.** method
dc8a0 73 20 77 65 72 65 20 64 65 66 69 6e 65 64 20 69 s were defined i
dc8b0 6e 20 64 69 76 69 73 69 6f 6e 73 20 61 62 6f 76 n divisions abov
dc8c0 65 20 28 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 6d e (one locking m
dc8d0 65 74 68 6f 64 20 70 65 72 0a 2a 2a 20 64 69 76 ethod per.** div
dc8e0 69 73 69 6f 6e 29 2e 20 20 54 68 6f 73 65 20 6d ision). Those m
dc8f0 65 74 68 6f 64 73 20 74 68 61 74 20 61 72 65 20 ethods that are
dc900 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f common to all lo
dc910 63 6b 69 6e 67 20 6d 6f 64 65 73 0a 2a 2a 20 61 cking modes.** a
dc920 72 65 20 67 61 74 68 65 72 20 74 6f 67 65 74 68 re gather togeth
dc930 65 72 20 69 6e 74 6f 20 74 68 69 73 20 64 69 76 er into this div
dc940 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a ision..*/../*.**
dc950 20 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 Seek to the off
dc960 73 65 74 20 70 61 73 73 65 64 20 61 73 20 74 68 set passed as th
dc970 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
dc980 74 2c 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 t, then read cnt
dc990 20 0a 2a 2a 20 62 79 74 65 73 20 69 6e 74 6f 20 .** bytes into
dc9a0 70 42 75 66 2e 20 52 65 74 75 72 6e 20 74 68 65 pBuf. Return the
dc9b0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
dc9c0 20 61 63 74 75 61 6c 6c 79 20 72 65 61 64 2e 0a actually read..
dc9d0 2a 2a 0a 2a 2a 20 4e 42 3a 20 20 49 66 20 79 6f **.** NB: If yo
dc9e0 75 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 u define USE_PRE
dc9f0 41 44 20 6f 72 20 55 53 45 5f 50 52 45 41 44 36 AD or USE_PREAD6
dca00 34 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 4, then it might
dca10 20 61 6c 73 6f 0a 2a 2a 20 62 65 20 6e 65 63 65 also.** be nece
dca20 73 73 61 72 79 20 74 6f 20 64 65 66 69 6e 65 20 ssary to define
dca30 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 74 6f _XOPEN_SOURCE to
dca40 20 62 65 20 35 30 30 2e 20 20 54 68 69 73 20 76 be 500. This v
dca50 61 72 69 65 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e aries from.** on
dca60 65 20 73 79 73 74 65 6d 20 74 6f 20 61 6e 6f 74 e system to anot
dca70 68 65 72 2e 20 20 53 69 6e 63 65 20 53 51 4c 69 her. Since SQLi
dca80 74 65 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 te does not defi
dca90 6e 65 20 55 53 45 5f 50 52 45 41 44 0a 2a 2a 20 ne USE_PREAD.**
dcaa0 61 6e 79 20 61 6e 79 20 66 6f 72 6d 20 62 79 20 any any form by
dcab0 64 65 66 61 75 6c 74 2c 20 77 65 20 77 69 6c 6c default, we will
dcac0 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 not attempt to
dcad0 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f define _XOPEN_SO
dcae0 55 52 43 45 2e 0a 2a 2a 20 53 65 65 20 74 69 63 URCE..** See tic
dcaf0 6b 65 74 73 20 23 32 37 34 31 20 61 6e 64 20 23 kets #2741 and #
dcb00 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 2681..**.** To a
dcb10 76 6f 69 64 20 73 74 6f 6d 70 69 6e 67 20 74 68 void stomping th
dcb20 65 20 65 72 72 6e 6f 20 76 61 6c 75 65 20 6f 6e e errno value on
dcb30 20 61 20 66 61 69 6c 65 64 20 72 65 61 64 20 74 a failed read t
dcb40 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 76 61 6c he lastErrno val
dcb50 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 62 65 66 ue.** is set bef
dcb60 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
dcb70 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 65 /.static int see
dcb80 6b 41 6e 64 52 65 61 64 28 75 6e 69 78 46 69 6c kAndRead(unixFil
dcb90 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 e *id, sqlite3_i
dcba0 6e 74 36 34 20 6f 66 66 73 65 74 2c 20 76 6f 69 nt64 offset, voi
dcbb0 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 6e 74 d *pBuf, int cnt
dcbc0 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 ){. int got;.
dcbd0 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b 0a 20 i64 newOffset;.
dcbe0 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a 23 69 TIMER_START;.#i
dcbf0 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 f defined(USE_PR
dcc00 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70 72 65 EAD). got = pre
dcc10 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 ad(id->h, pBuf,
dcc20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 cnt, offset);.
dcc30 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
dcc40 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c got = -1 );.#el
dcc50 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 if defined(USE_P
dcc60 52 45 41 44 36 34 29 0a 20 20 67 6f 74 20 3d 20 READ64). got =
dcc70 70 72 65 61 64 36 34 28 69 64 2d 3e 68 2c 20 70 pread64(id->h, p
dcc80 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 Buf, cnt, offset
dcc90 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
dcca0 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 rror( got = -1 )
dccb0 3b 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66 ;.#else. newOff
dccc0 73 65 74 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e set = lseek(id->
dccd0 68 2c 20 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f h, offset, SEEK_
dcce0 53 45 54 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 SET);. Simulate
dccf0 49 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66 66 73 IOError( newOffs
dcd00 65 74 2d 2d 20 29 3b 0a 20 20 69 66 28 20 6e 65 et-- );. if( ne
dcd10 77 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74 20 wOffset!=offset
dcd20 29 7b 0a 20 20 20 20 69 66 28 20 6e 65 77 4f 66 ){. if( newOf
dcd30 66 73 65 74 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 fset == -1 ){.
dcd40 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 ((unixFile*)
dcd50 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d id)->lastErrno =
dcd60 20 65 72 72 6e 6f 3b 0a 20 20 20 20 7d 65 6c 73 errno;. }els
dcd70 65 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 e{. ((unixF
dcd80 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 ile*)id)->lastEr
dcd90 72 6e 6f 20 3d 20 30 3b 09 09 09 0a 20 20 20 20 rno = 0;....
dcda0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b }. return -1;
dcdb0 0a 20 20 7d 0a 20 20 67 6f 74 20 3d 20 72 65 61 . }. got = rea
dcdc0 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 d(id->h, pBuf, c
dcdd0 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 54 49 nt);.#endif. TI
dcde0 4d 45 52 5f 45 4e 44 3b 0a 20 20 69 66 28 20 67 MER_END;. if( g
dcdf0 6f 74 3c 30 20 29 7b 0a 20 20 20 20 28 28 75 6e ot<0 ){. ((un
dce00 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 ixFile*)id)->las
dce10 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
dce20 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 35 28 22 }. OSTRACE5("
dce30 52 45 41 44 20 20 20 20 25 2d 33 64 20 25 35 64 READ %-3d %5d
dce40 20 25 37 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 %7lld %llu\n",
dce50 69 64 2d 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 id->h, got, offs
dce60 65 74 2c 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 et, TIMER_ELAPSE
dce70 44 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 D);. return got
dce80 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
dce90 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 data from a file
dcea0 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 into a buffer.
dceb0 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
dcec0 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 K if all.** byte
dced0 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63 s were read succ
dcee0 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c essfully and SQL
dcef0 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 ITE_IOERR if any
dcf00 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 thing goes.** wr
dcf10 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ong..*/.static i
dcf20 6e 74 20 75 6e 69 78 52 65 61 64 28 0a 20 20 73 nt unixRead(. s
dcf30 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
dcf40 20 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 . void *pBuf,
dcf50 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 . int amt,. sq
dcf60 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 lite3_int64 offs
dcf70 65 74 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 et.){. unixFile
dcf80 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
dcf90 69 6c 65 20 2a 29 69 64 3b 0a 20 20 69 6e 74 20 ile *)id;. int
dcfa0 67 6f 74 3b 0a 20 20 61 73 73 65 72 74 28 20 69 got;. assert( i
dcfb0 64 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 d );.. /* If th
dcfc0 69 73 20 69 73 20 61 20 64 61 74 61 62 61 73 65 is is a database
dcfd0 20 66 69 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75 file (not a jou
dcfe0 72 6e 61 6c 2c 20 6d 61 73 74 65 72 2d 6a 6f 75 rnal, master-jou
dcff0 72 6e 61 6c 20 6f 72 20 74 65 6d 70 0a 20 20 2a rnal or temp. *
dd000 2a 20 66 69 6c 65 29 2c 20 74 68 65 20 62 79 74 * file), the byt
dd010 65 73 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e es in the lockin
dd020 67 20 72 61 6e 67 65 20 73 68 6f 75 6c 64 20 6e g range should n
dd030 65 76 65 72 20 62 65 20 72 65 61 64 20 6f 72 20 ever be read or
dd040 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 61 73 written. */. as
dd050 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e sert( pFile->pUn
dd060 75 73 65 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c used==0. |
dd070 7c 20 6f 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e | offset>=PENDIN
dd080 47 5f 42 59 54 45 2b 35 31 32 0a 20 20 20 20 20 G_BYTE+512.
dd090 20 20 7c 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c || offset+amt<
dd0a0 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20 =PENDING_BYTE .
dd0b0 20 29 3b 0a 0a 20 20 67 6f 74 20 3d 20 73 65 65 );.. got = see
dd0c0 6b 41 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20 kAndRead(pFile,
dd0d0 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d offset, pBuf, am
dd0e0 74 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d 3d 61 t);. if( got==a
dd0f0 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e mt ){. return
dd100 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
dd110 6c 73 65 20 69 66 28 20 67 6f 74 3c 30 20 29 7b lse if( got<0 ){
dd120 0a 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 72 6e . /* lastErrn
dd130 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41 6e 64 o set by seekAnd
dd140 52 65 61 64 20 2a 2f 0a 20 20 20 20 72 65 74 75 Read */. retu
dd150 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
dd160 52 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 READ;. }else{.
dd170 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
dd180 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 6e 6f 74 20 rno = 0; /* not
dd190 61 20 73 79 73 74 65 6d 20 65 72 72 6f 72 20 2a a system error *
dd1a0 2f 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 /. /* Unread
dd1b0 70 61 72 74 73 20 6f 66 20 74 68 65 20 62 75 66 parts of the buf
dd1c0 66 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 6f fer must be zero
dd1d0 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d -filled */. m
dd1e0 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 emset(&((char*)p
dd1f0 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d Buf)[got], 0, am
dd200 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 t-got);. retu
dd210 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
dd220 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a SHORT_READ;. }.
dd230 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f }../*.** Seek to
dd240 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e 20 69 the offset in i
dd250 64 2d 3e 6f 66 66 73 65 74 20 74 68 65 6e 20 72 d->offset then r
dd260 65 61 64 20 63 6e 74 20 62 79 74 65 73 20 69 6e ead cnt bytes in
dd270 74 6f 20 70 42 75 66 2e 0a 2a 2a 20 52 65 74 75 to pBuf..** Retu
dd280 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
dd290 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 bytes actually
dd2a0 72 65 61 64 2e 20 20 55 70 64 61 74 65 20 74 68 read. Update th
dd2b0 65 20 6f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 e offset..**.**
dd2c0 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 69 6e To avoid stompin
dd2d0 67 20 74 68 65 20 65 72 72 6e 6f 20 76 61 6c 75 g the errno valu
dd2e0 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 77 72 e on a failed wr
dd2f0 69 74 65 20 74 68 65 20 6c 61 73 74 45 72 72 6e ite the lastErrn
dd300 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 o value.** is se
dd310 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 t before returni
dd320 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
dd330 74 20 73 65 65 6b 41 6e 64 57 72 69 74 65 28 75 t seekAndWrite(u
dd340 6e 69 78 46 69 6c 65 20 2a 69 64 2c 20 69 36 34 nixFile *id, i64
dd350 20 6f 66 66 73 65 74 2c 20 63 6f 6e 73 74 20 76 offset, const v
dd360 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 oid *pBuf, int c
dd370 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a nt){. int got;.
dd380 20 20 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b i64 newOffset;
dd390 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a . TIMER_START;.
dd3a0 23 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f #if defined(USE_
dd3b0 50 52 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70 PREAD). got = p
dd3c0 77 72 69 74 65 28 69 64 2d 3e 68 2c 20 70 42 75 write(id->h, pBu
dd3d0 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b f, cnt, offset);
dd3e0 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 55 .#elif defined(U
dd3f0 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67 6f SE_PREAD64). go
dd400 74 20 3d 20 70 77 72 69 74 65 36 34 28 69 64 2d t = pwrite64(id-
dd410 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f >h, pBuf, cnt, o
dd420 66 66 73 65 74 29 3b 0a 23 65 6c 73 65 0a 20 20 ffset);.#else.
dd430 6e 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65 newOffset = lsee
dd440 6b 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c k(id->h, offset,
dd450 20 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 69 66 SEEK_SET);. if
dd460 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66 66 ( newOffset!=off
dd470 73 65 74 20 29 7b 0a 20 20 20 20 69 66 28 20 6e set ){. if( n
dd480 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 29 ewOffset == -1 )
dd490 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 {. ((unixFi
dd4a0 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 le*)id)->lastErr
dd4b0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
dd4c0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 75 }else{. ((u
dd4d0 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 nixFile*)id)->la
dd4e0 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09 0a stErrno = 0;....
dd4f0 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
dd500 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 3d -1;. }. got =
dd510 20 77 72 69 74 65 28 69 64 2d 3e 68 2c 20 70 42 write(id->h, pB
dd520 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66 uf, cnt);.#endif
dd530 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 . TIMER_END;.
dd540 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 if( got<0 ){.
dd550 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 ((unixFile*)id)
dd560 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
dd570 72 6e 6f 3b 0a 20 20 7d 0a 0a 20 20 4f 53 54 52 rno;. }.. OSTR
dd580 41 43 45 35 28 22 57 52 49 54 45 20 20 20 25 2d ACE5("WRITE %-
dd590 33 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 3d %5d %7lld %ll
dd5a0 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 u\n", id->h, got
dd5b0 2c 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f , offset, TIMER_
dd5c0 45 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75 ELAPSED);. retu
dd5d0 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a rn got;.}.../*.*
dd5e0 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f * Write data fro
dd5f0 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 m a buffer into
dd600 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 a file. Return
dd610 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 SQLITE_OK on suc
dd620 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 cess.** or some
dd630 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 other error code
dd640 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a on failure..*/.
dd650 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 57 static int unixW
dd660 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f rite(. sqlite3_
dd670 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 63 6f 6e file *id, . con
dd680 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a st void *pBuf, .
dd690 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c int amt,. sql
dd6a0 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 ite3_int64 offse
dd6b0 74 20 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 t .){. unixFile
dd6c0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
dd6d0 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 77 ile*)id;. int w
dd6e0 72 6f 74 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 rote = 0;. asse
dd6f0 72 74 28 20 69 64 20 29 3b 0a 20 20 61 73 73 65 rt( id );. asse
dd700 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 0a 20 20 rt( amt>0 );..
dd710 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 /* If this is a
dd720 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e database file (n
dd730 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61 ot a journal, ma
dd740 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20 ster-journal or
dd750 74 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65 29 2c temp. ** file),
dd760 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68 the bytes in th
dd770 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 e locking range
dd780 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 should never be
dd790 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e read or written.
dd7a0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 */. assert( pF
dd7b0 69 6c 65 2d 3e 70 55 6e 75 73 65 64 3d 3d 30 0a ile->pUnused==0.
dd7c0 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 || offset
dd7d0 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 >=PENDING_BYTE+5
dd7e0 31 32 0a 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 12. || off
dd7f0 73 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 set+amt<=PENDING
dd800 5f 42 59 54 45 20 0a 20 20 29 3b 0a 0a 23 69 66 _BYTE . );..#if
dd810 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a ndef NDEBUG. /*
dd820 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67 If we are doing
dd830 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20 a normal write
dd840 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 69 to a database fi
dd850 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74 le (as opposed t
dd860 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 20 68 o. ** doing a h
dd870 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 ot-journal rollb
dd880 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65 20 74 ack or a write t
dd890 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 68 65 o some file othe
dd8a0 72 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20 6e 6f r than a. ** no
dd8b0 72 6d 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 rmal database fi
dd8c0 6c 65 29 20 74 68 65 6e 20 72 65 63 6f 72 64 20 le) then record
dd8d0 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 the fact that th
dd8e0 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 e database. **
dd8f0 68 61 73 20 63 68 61 6e 67 65 64 2e 20 20 49 66 has changed. If
dd900 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
dd910 20 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f 64 69 counter is modi
dd920 66 69 65 64 2c 20 72 65 63 6f 72 64 20 74 68 61 fied, record tha
dd930 74 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f 6f 2e t. ** fact too.
dd940 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c . */. if( pFil
dd950 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 e->inNormalWrite
dd960 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 ){. pFile->d
dd970 62 55 70 64 61 74 65 20 3d 20 31 3b 20 20 2f 2a bUpdate = 1; /*
dd980 20 54 68 65 20 64 61 74 61 62 61 73 65 20 68 61 The database ha
dd990 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 s been modified
dd9a0 2a 2f 0a 20 20 20 20 69 66 28 20 6f 66 66 73 65 */. if( offse
dd9b0 74 3c 3d 32 34 20 26 26 20 6f 66 66 73 65 74 2b t<=24 && offset+
dd9c0 61 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20 20 20 amt>=27 ){.
dd9d0 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 63 int rc;. c
dd9e0 68 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d 3b 0a har oldCntr[4];.
dd9f0 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f SimulateIO
dda00 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a ErrorBenign(1);.
dda10 20 20 20 20 20 20 72 63 20 3d 20 73 65 65 6b 41 rc = seekA
dda20 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20 32 34 ndRead(pFile, 24
dda30 2c 20 6f 6c 64 43 6e 74 72 2c 20 34 29 3b 0a 20 , oldCntr, 4);.
dda40 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 SimulateIOE
dda50 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 rrorBenign(0);.
dda60 20 20 20 20 20 69 66 28 20 72 63 21 3d 34 20 7c if( rc!=4 |
dda70 7c 20 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e 74 72 | memcmp(oldCntr
dda80 2c 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 , &((char*)pBuf)
dda90 5b 32 34 2d 6f 66 66 73 65 74 5d 2c 20 34 29 21 [24-offset], 4)!
ddaa0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 =0 ){. pF
ddab0 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 ile->transCntrCh
ddac0 6e 67 20 3d 20 31 3b 20 20 2f 2a 20 54 68 65 20 ng = 1; /* The
ddad0 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e transaction coun
ddae0 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 20 ter has changed
ddaf0 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d */. }. }
ddb00 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 77 . }.#endif.. w
ddb10 68 69 6c 65 28 20 61 6d 74 3e 30 20 26 26 20 28 hile( amt>0 && (
ddb20 77 72 6f 74 65 20 3d 20 73 65 65 6b 41 6e 64 57 wrote = seekAndW
ddb30 72 69 74 65 28 70 46 69 6c 65 2c 20 6f 66 66 73 rite(pFile, offs
ddb40 65 74 2c 20 70 42 75 66 2c 20 61 6d 74 29 29 3e et, pBuf, amt))>
ddb50 30 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 0 ){. amt -=
ddb60 77 72 6f 74 65 3b 0a 20 20 20 20 6f 66 66 73 65 wrote;. offse
ddb70 74 20 2b 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 t += wrote;.
ddb80 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29 pBuf = &((char*)
ddb90 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 pBuf)[wrote];.
ddba0 7d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 }. SimulateIOEr
ddbb0 72 6f 72 28 28 20 77 72 6f 74 65 3d 28 2d 31 29 ror(( wrote=(-1)
ddbc0 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 53 69 , amt=1 ));. Si
ddbd0 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 mulateDiskfullEr
ddbe0 72 6f 72 28 28 20 77 72 6f 74 65 3d 30 2c 20 61 ror(( wrote=0, a
ddbf0 6d 74 3d 31 20 29 29 3b 0a 20 20 69 66 28 20 61 mt=1 ));. if( a
ddc00 6d 74 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 mt>0 ){. if(
ddc10 77 72 6f 74 65 3c 30 20 29 7b 0a 20 20 20 20 20 wrote<0 ){.
ddc20 20 2f 2a 20 6c 61 73 74 45 72 72 6e 6f 20 73 65 /* lastErrno se
ddc30 74 20 62 79 20 73 65 65 6b 41 6e 64 57 72 69 74 t by seekAndWrit
ddc40 65 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 e */. retur
ddc50 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 n SQLITE_IOERR_W
ddc60 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b RITE;. }else{
ddc70 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 . pFile->la
ddc80 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 stErrno = 0; /*
ddc90 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 72 not a system err
ddca0 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 or */. retu
ddcb0 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a rn SQLITE_FULL;.
ddcc0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
ddcd0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
ddce0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
ddcf0 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 EST./*.** Count
ddd00 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 the number of fu
ddd10 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d llsyncs and norm
ddd20 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 al syncs. This
ddd30 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a is used to test.
ddd40 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e ** that syncs an
ddd50 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 d fullsyncs are
ddd60 6f 63 63 75 72 72 69 6e 67 20 61 74 20 74 68 65 occurring at the
ddd70 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f right times..*/
ddd80 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
ddd90 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 sqlite3_sync_cou
ddda0 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 nt = 0;.SQLITE_A
dddb0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 PI int sqlite3_f
dddc0 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 ullsync_count =
dddd0 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0;.#endif../*.**
ddde0 20 57 65 20 64 6f 20 6e 6f 74 20 74 72 75 73 74 We do not trust
dddf0 20 73 79 73 74 65 6d 73 20 74 6f 20 70 72 6f 76 systems to prov
dde00 69 64 65 20 61 20 77 6f 72 6b 69 6e 67 20 66 64 ide a working fd
dde10 61 74 61 73 79 6e 63 28 29 2e 20 20 53 6f 6d 65 atasync(). Some
dde20 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65 72 73 20 64 do..** Others d
dde30 6f 20 6e 6f 2e 20 20 54 6f 20 62 65 20 73 61 66 o no. To be saf
dde40 65 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 63 6b e, we will stick
dde50 20 77 69 74 68 20 74 68 65 20 28 73 6c 6f 77 65 with the (slowe
dde60 72 29 20 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 49 r) fsync()..** I
dde70 66 20 79 6f 75 20 6b 6e 6f 77 20 74 68 61 74 20 f you know that
dde80 79 6f 75 72 20 73 79 73 74 65 6d 20 64 6f 65 73 your system does
dde90 20 73 75 70 70 6f 72 74 20 66 64 61 74 61 73 79 support fdatasy
ddea0 6e 63 28 29 20 63 6f 72 72 65 63 74 6c 79 2c 0a nc() correctly,.
ddeb0 2a 2a 20 74 68 65 6e 20 73 69 6d 70 6c 79 20 63 ** then simply c
ddec0 6f 6d 70 69 6c 65 20 77 69 74 68 20 2d 44 66 64 ompile with -Dfd
dded0 61 74 61 73 79 6e 63 3d 66 64 61 74 61 73 79 6e atasync=fdatasyn
ddee0 63 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 c.*/.#if !define
ddef0 64 28 66 64 61 74 61 73 79 6e 63 29 20 26 26 20 d(fdatasync) &&
ddf00 21 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 !defined(__linux
ddf10 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 66 64 61 __).# define fda
ddf20 74 61 73 79 6e 63 20 66 73 79 6e 63 0a 23 65 6e tasync fsync.#en
ddf30 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e dif../*.** Defin
ddf40 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 e HAVE_FULLFSYNC
ddf50 20 74 6f 20 30 20 6f 72 20 31 20 64 65 70 65 6e to 0 or 1 depen
ddf60 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20 ding on whether
ddf70 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 46 5f or not.** the F_
ddf80 46 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72 6f 20 FULLFSYNC macro
ddf90 69 73 20 64 65 66 69 6e 65 64 2e 20 20 46 5f 46 is defined. F_F
ddfa0 55 4c 4c 46 53 59 4e 43 20 69 73 20 63 75 72 72 ULLFSYNC is curr
ddfb0 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20 61 76 ently.** only av
ddfc0 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 20 4f ailable on Mac O
ddfd0 53 20 58 2e 20 20 42 75 74 20 74 68 61 74 20 63 S X. But that c
ddfe0 6f 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a ould change..*/.
ddff0 23 69 66 64 65 66 20 46 5f 46 55 4c 4c 46 53 59 #ifdef F_FULLFSY
de000 4e 43 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45 NC.# define HAVE
de010 5f 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23 65 6c _FULLFSYNC 1.#el
de020 73 65 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45 se.# define HAVE
de030 5f 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23 65 6e _FULLFSYNC 0.#en
de040 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 dif.../*.** The
de050 66 73 79 6e 63 28 29 20 73 79 73 74 65 6d 20 63 fsync() system c
de060 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 all does not wor
de070 6b 20 61 73 20 61 64 76 65 72 74 69 73 65 64 20 k as advertised
de080 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69 78 20 on many.** unix
de090 73 79 73 74 65 6d 73 2e 20 20 54 68 65 20 66 6f systems. The fo
de0a0 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64 75 72 llowing procedur
de0b0 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20 e is an attempt
de0c0 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 77 6f to make.** it wo
de0d0 72 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a 2a 2a rk better..**.**
de0e0 20 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 The SQLITE_NO_S
de0f0 59 4e 43 20 6d 61 63 72 6f 20 64 69 73 61 62 6c YNC macro disabl
de100 65 73 20 61 6c 6c 20 66 73 79 6e 63 28 29 73 2e es all fsync()s.
de110 20 20 54 68 69 73 20 69 73 20 75 73 65 66 75 6c This is useful
de120 0a 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e 67 20 .** for testing
de130 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74 6f 20 when we want to
de140 72 75 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20 run through the
de150 74 65 73 74 20 73 75 69 74 65 20 71 75 69 63 6b test suite quick
de160 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65 20 73 ly..** You are s
de170 74 72 6f 6e 67 6c 79 20 61 64 76 69 73 65 64 20 trongly advised
de180 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f 79 20 *not* to deploy
de190 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 with SQLITE_NO_S
de1a0 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 YNC.** enabled,
de1b0 68 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 77 however, since w
de1c0 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 ith SQLITE_NO_SY
de1d0 4e 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e 20 4f NC enabled, an O
de1e0 53 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20 70 6f S crash.** or po
de1f0 77 65 72 20 66 61 69 6c 75 72 65 20 77 69 6c 6c wer failure will
de200 20 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70 74 20 likely corrupt
de210 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
de220 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 e..**.** SQLite
de230 73 65 74 73 20 74 68 65 20 64 61 74 61 4f 6e 6c sets the dataOnl
de240 79 20 66 6c 61 67 20 69 66 20 74 68 65 20 73 69 y flag if the si
de250 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 ze of the file i
de260 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 s unchanged..**
de270 54 68 65 20 69 64 65 61 20 62 65 68 69 6e 64 20 The idea behind
de280 64 61 74 61 4f 6e 6c 79 20 69 73 20 74 68 61 74 dataOnly is that
de290 20 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 it should only
de2a0 77 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 63 write the file c
de2b0 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f 20 64 69 73 ontent.** to dis
de2c0 6b 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f 64 65 k, not the inode
de2d0 2e 20 20 57 65 20 6f 6e 6c 79 20 73 65 74 20 64 . We only set d
de2e0 61 74 61 4f 6e 6c 79 20 69 66 20 74 68 65 20 66 ataOnly if the f
de2f0 69 6c 65 20 73 69 7a 65 20 69 73 20 0a 2a 2a 20 ile size is .**
de300 75 6e 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20 unchanged since
de310 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73 the file size is
de320 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e 6f part of the ino
de330 64 65 2e 20 20 48 6f 77 65 76 65 72 2c 20 0a 2a de. However, .*
de340 2a 20 54 65 64 20 54 73 27 6f 20 74 65 6c 6c 73 * Ted Ts'o tells
de350 20 75 73 20 74 68 61 74 20 66 64 61 74 61 73 79 us that fdatasy
de360 6e 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f 20 77 nc() will also w
de370 72 69 74 65 20 74 68 65 20 69 6e 6f 64 65 20 69 rite the inode i
de380 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 73 69 f the.** file si
de390 7a 65 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20 ze has changed.
de3a0 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 6c 20 64 The only real d
de3b0 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 ifference betwee
de3c0 6e 20 66 64 61 74 61 73 79 6e 63 28 29 0a 2a 2a n fdatasync().**
de3d0 20 61 6e 64 20 66 73 79 6e 63 28 29 2c 20 54 65 and fsync(), Te
de3e0 64 20 74 65 6c 6c 73 20 75 73 2c 20 69 73 20 74 d tells us, is t
de3f0 68 61 74 20 66 64 61 74 61 73 79 6e 63 28 29 20 hat fdatasync()
de400 77 69 6c 6c 20 6e 6f 74 20 66 6c 75 73 68 20 74 will not flush t
de410 68 65 0a 2a 2a 20 69 6e 6f 64 65 20 69 66 20 74 he.** inode if t
de420 68 65 20 6d 74 69 6d 65 20 6f 72 20 6f 77 6e 65 he mtime or owne
de430 72 20 6f 72 20 6f 74 68 65 72 20 69 6e 6f 64 65 r or other inode
de440 20 61 74 74 72 69 62 75 74 65 73 20 68 61 76 65 attributes have
de450 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 57 65 20 changed..** We
de460 6f 6e 6c 79 20 63 61 72 65 20 61 62 6f 75 74 20 only care about
de470 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2c 20 6e the file size, n
de480 6f 74 20 74 68 65 20 6f 74 68 65 72 20 66 69 6c ot the other fil
de490 65 20 61 74 74 72 69 62 75 74 65 73 2c 20 73 6f e attributes, so
de4a0 0a 2a 2a 20 61 73 20 66 61 72 20 61 73 20 53 51 .** as far as SQ
de4b0 4c 69 74 65 20 69 73 20 63 6f 6e 63 65 72 6e 65 Lite is concerne
de4c0 64 2c 20 61 6e 20 66 64 61 74 61 73 79 6e 63 28 d, an fdatasync(
de4d0 29 20 69 73 20 61 6c 77 61 79 73 20 61 64 65 71 ) is always adeq
de4e0 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c 20 77 65 20 uate..** So, we
de4f0 61 6c 77 61 79 73 20 75 73 65 20 66 64 61 74 61 always use fdata
de500 73 79 6e 63 28 29 20 69 66 20 69 74 20 69 73 20 sync() if it is
de510 61 76 61 69 6c 61 62 6c 65 2c 20 72 65 67 61 72 available, regar
de520 64 6c 65 73 73 20 6f 66 0a 2a 2a 20 74 68 65 20 dless of.** the
de530 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 value of the dat
de540 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a 2a 2f 0a 73 aOnly flag..*/.s
de550 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f 66 tatic int full_f
de560 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e 74 sync(int fd, int
de570 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 64 fullSync, int d
de580 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 20 ataOnly){. int
de590 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f rc;.. /* The fo
de5a0 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 2f 65 llowing "ifdef/e
de5b0 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f 63 6b lif/else/" block
de5c0 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 73 74 has the same st
de5d0 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a 2a 20 ructure as. **
de5e0 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e 20 49 the one below. I
de5f0 74 20 69 73 20 72 65 70 6c 69 63 61 74 65 64 20 t is replicated
de600 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f 20 61 here solely to a
de610 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e 67 20 void cluttering
de620 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 65 61 . ** up the rea
de630 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 65 20 l code with the
de640 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
de650 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a 2f 0a () macros.. */.
de660 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f #ifdef SQLITE_NO
de670 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 _SYNC. UNUSED_P
de680 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a 20 20 ARAMETER(fd);.
de690 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
de6a0 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 55 4e (fullSync);. UN
de6b0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 USED_PARAMETER(d
de6c0 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 66 20 ataOnly);.#elif
de6d0 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0a 20 HAVE_FULLFSYNC.
de6e0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
de6f0 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c R(dataOnly);.#el
de700 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 se. UNUSED_PARA
de710 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b METER(fullSync);
de720 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
de730 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 TER(dataOnly);.#
de740 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 63 6f endif.. /* Reco
de750 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rd the number of
de760 20 74 69 6d 65 73 20 74 68 61 74 20 77 65 20 64 times that we d
de770 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 o a normal fsync
de780 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46 55 4c () and . ** FUL
de790 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69 73 20 LSYNC. This is
de7a0 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 73 74 used during test
de7b0 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20 74 68 ing to verify th
de7c0 61 74 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 at this procedur
de7d0 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 6c 6c e. ** gets call
de7e0 65 64 20 77 69 74 68 20 74 68 65 20 63 6f 72 72 ed with the corr
de7f0 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e 0a 20 ect arguments..
de800 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
de810 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 75 6c E_TEST. if( ful
de820 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65 33 5f lSync ) sqlite3_
de830 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b fullsync_count++
de840 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 ;. sqlite3_sync
de850 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 _count++;.#endif
de860 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d .. /* If we com
de870 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 piled with the S
de880 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c QLITE_NO_SYNC fl
de890 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 ag, then syncing
de8a0 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 is a. ** no-op
de8b0 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c . */.#ifdef SQL
de8c0 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 72 63 ITE_NO_SYNC. rc
de8d0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 = SQLITE_OK;.#e
de8e0 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 lif HAVE_FULLFSY
de8f0 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53 79 6e NC. if( fullSyn
de900 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 63 c ){. rc = fc
de910 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c 46 53 ntl(fd, F_FULLFS
de920 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 YNC, 0);. }else
de930 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 {. rc = 1;.
de940 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 46 55 }. /* If the FU
de950 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64 2c 20 LLFSYNC failed,
de960 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61 74 74 fall back to att
de970 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79 6e 63 empting an fsync
de980 28 29 2e 0a 20 20 2a 2a 20 49 74 20 73 68 6f 75 ().. ** It shou
de990 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69 62 6c ldn't be possibl
de9a0 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e 63 20 e for fullfsync
de9b0 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65 20 6c to fail on the l
de9c0 6f 63 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 ocal . ** file
de9d0 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58 29 2c system (on OSX),
de9e0 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e 64 69 so failure indi
de9f0 63 61 74 65 73 20 74 68 61 74 20 46 55 4c 4c 46 cates that FULLF
dea00 53 59 4e 43 0a 20 20 2a 2a 20 69 73 6e 27 74 20 SYNC. ** isn't
dea10 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 74 68 supported for th
dea20 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d 2e 20 is file system.
dea30 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e 20 66 So, attempt an f
dea40 73 79 6e 63 20 0a 20 20 2a 2a 20 61 6e 64 20 28 sync . ** and (
dea50 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72 65 20 for now) ignore
dea60 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f 66 20 the overhead of
dea70 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20 66 63 a superfluous fc
dea80 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20 2a 2a ntl call. . **
dea90 20 49 74 27 64 20 62 65 20 62 65 74 74 65 72 20 It'd be better
deaa0 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c 66 73 to detect fullfs
deab0 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e 63 65 ync support once
deac0 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20 2a 2a and avoid . **
dead0 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c 6c 20 the fcntl call
deae0 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e 63 20 every time sync
deaf0 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2f 0a is called.. */.
deb00 20 20 69 66 28 20 72 63 20 29 20 72 63 20 3d 20 if( rc ) rc =
deb10 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23 65 6c 73 fsync(fd);..#els
deb20 65 20 0a 20 20 72 63 20 3d 20 66 64 61 74 61 73 e . rc = fdatas
deb30 79 6e 63 28 66 64 29 3b 0a 23 69 66 20 4f 53 5f ync(fd);.#if OS_
deb40 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63 VXWORKS. if( rc
deb50 3d 3d 2d 31 20 26 26 20 65 72 72 6e 6f 3d 3d 45 ==-1 && errno==E
deb60 4e 4f 54 53 55 50 20 29 7b 0a 20 20 20 20 72 63 NOTSUP ){. rc
deb70 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 20 20 = fsync(fd);.
deb80 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 }.#endif /* OS_V
deb90 58 57 4f 52 4b 53 20 2a 2f 0a 23 65 6e 64 69 66 XWORKS */.#endif
deba0 20 2f 2a 20 69 66 64 65 66 20 53 51 4c 49 54 45 /* ifdef SQLITE
debb0 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69 66 20 48 41 _NO_SYNC elif HA
debc0 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 2a 2f 0a VE_FULLFSYNC */.
debd0 0a 20 20 69 66 28 20 4f 53 5f 56 58 57 4f 52 4b . if( OS_VXWORK
debe0 53 20 26 26 20 72 63 21 3d 20 2d 31 20 29 7b 0a S && rc!= -1 ){.
debf0 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 7d 0a rc = 0;. }.
dec00 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
dec10 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 /*.** Make sure
dec20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20 all writes to a
dec30 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 particular file
dec40 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f are committed to
dec50 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 disk..**.** If
dec60 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 74 68 65 6e dataOnly==0 then
dec70 20 62 6f 74 68 20 74 68 65 20 66 69 6c 65 20 69 both the file i
dec80 74 73 65 6c 66 20 61 6e 64 20 69 74 73 20 6d 65 tself and its me
dec90 74 61 64 61 74 61 20 28 66 69 6c 65 0a 2a 2a 20 tadata (file.**
deca0 73 69 7a 65 2c 20 61 63 63 65 73 73 20 74 69 6d size, access tim
decb0 65 2c 20 65 74 63 29 20 61 72 65 20 73 79 6e 63 e, etc) are sync
decc0 65 64 2e 20 20 49 66 20 64 61 74 61 4f 6e 6c 79 ed. If dataOnly
decd0 21 3d 30 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 !=0 then only th
dece0 65 0a 2a 2a 20 66 69 6c 65 20 64 61 74 61 20 69 e.** file data i
decf0 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 s synced..**.**
ded00 55 6e 64 65 72 20 55 6e 69 78 2c 20 61 6c 73 6f Under Unix, also
ded10 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 make sure that
ded20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 65 6e the directory en
ded30 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65 try for the file
ded40 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 72 65 .** has been cre
ded50 61 74 65 64 20 62 79 20 66 73 79 6e 63 2d 69 6e ated by fsync-in
ded60 67 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 g the directory
ded70 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
ded80 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 66 20 77 65 e file..** If we
ded90 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 20 do not do this
deda0 61 6e 64 20 77 65 20 65 6e 63 6f 75 6e 74 65 72 and we encounter
dedb0 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 a power failure
dedc0 2c 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 0a , the directory.
dedd0 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 ** entry for the
dede0 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 6e journal might n
dedf0 6f 74 20 65 78 69 73 74 20 61 66 74 65 72 20 77 ot exist after w
dee00 65 20 72 65 62 6f 6f 74 2e 20 20 54 68 65 20 6e e reboot. The n
dee10 65 78 74 0a 2a 2a 20 53 51 4c 69 74 65 20 74 6f ext.** SQLite to
dee20 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c 65 access the file
dee30 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f 77 20 74 will not know t
dee40 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hat the journal
dee50 65 78 69 73 74 73 20 28 62 65 63 61 75 73 65 0a exists (because.
dee60 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 ** the directory
dee70 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6a entry for the j
dee80 6f 75 72 6e 61 6c 20 77 61 73 20 6e 65 76 65 72 ournal was never
dee90 20 63 72 65 61 74 65 64 29 20 61 6e 64 20 74 68 created) and th
deea0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a e transaction.**
deeb0 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c 20 62 will not roll b
deec0 61 63 6b 20 2d 20 70 6f 73 73 69 62 6c 79 20 6c ack - possibly l
deed0 65 61 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61 eading to databa
deee0 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a se corruption..*
deef0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
def00 78 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 xSync(sqlite3_fi
def10 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 le *id, int flag
def20 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 s){. int rc;.
def30 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
def40 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
def50 0a 0a 20 20 69 6e 74 20 69 73 44 61 74 61 4f 6e .. int isDataOn
def60 6c 79 20 3d 20 28 66 6c 61 67 73 26 53 51 4c 49 ly = (flags&SQLI
def70 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 TE_SYNC_DATAONLY
def80 29 3b 0a 20 20 69 6e 74 20 69 73 46 75 6c 6c 73 );. int isFulls
def90 79 6e 63 20 3d 20 28 66 6c 61 67 73 26 30 78 30 ync = (flags&0x0
defa0 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f F)==SQLITE_SYNC_
defb0 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 FULL;.. /* Chec
defc0 6b 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 53 51 k that one of SQ
defd0 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
defe0 20 6f 72 20 46 55 4c 4c 20 77 61 73 20 70 61 73 or FULL was pas
deff0 73 65 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 sed */. assert(
df000 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 (flags&0x0F)==SQ
df010 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
df020 0a 20 20 20 20 20 20 7c 7c 20 28 66 6c 61 67 73 . || (flags
df030 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 &0x0F)==SQLITE_S
df040 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 3b 0a 0a 20 YNC_FULL. );..
df050 20 2f 2a 20 55 6e 69 78 20 63 61 6e 6e 6f 74 2c /* Unix cannot,
df060 20 62 75 74 20 73 6f 6d 65 20 73 79 73 74 65 6d but some system
df070 73 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c s may return SQL
df080 49 54 45 5f 46 55 4c 4c 20 66 72 6f 6d 20 68 65 ITE_FULL from he
df090 72 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6c 69 re. This. ** li
df0a0 6e 65 20 69 73 20 74 6f 20 74 65 73 74 20 74 68 ne is to test th
df0b0 61 74 20 64 6f 69 6e 67 20 73 6f 20 64 6f 65 73 at doing so does
df0c0 20 6e 6f 74 20 63 61 75 73 65 20 61 6e 79 20 70 not cause any p
df0d0 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a 2f 0a 20 20 roblems.. */.
df0e0 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c SimulateDiskfull
df0f0 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
df100 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 0a 20 20 LITE_FULL );..
df110 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
df120 0a 20 20 4f 53 54 52 41 43 45 32 28 22 53 59 4e . OSTRACE2("SYN
df130 43 20 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 C %-3d\n", pF
df140 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 63 20 3d 20 ile->h);. rc =
df150 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69 6c 65 full_fsync(pFile
df160 2d 3e 68 2c 20 69 73 46 75 6c 6c 73 79 6e 63 2c ->h, isFullsync,
df170 20 69 73 44 61 74 61 4f 6e 6c 79 29 3b 0a 20 20 isDataOnly);.
df180 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
df190 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72 rc=1 );. if( r
df1a0 63 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e c ){. pFile->
df1b0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e lastErrno = errn
df1c0 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 o;. return SQ
df1d0 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 LITE_IOERR_FSYNC
df1e0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69 6c ;. }. if( pFil
df1f0 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 e->dirfd>=0 ){.
df200 20 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 20 20 int err;.
df210 4f 53 54 52 41 43 45 34 28 22 44 49 52 53 59 4e OSTRACE4("DIRSYN
df220 43 20 25 2d 33 64 20 28 68 61 76 65 5f 66 75 6c C %-3d (have_ful
df230 6c 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c 73 79 lfsync=%d fullsy
df240 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 nc=%d)\n", pFile
df250 2d 3e 64 69 72 66 64 2c 0a 20 20 20 20 20 20 20 ->dirfd,.
df260 20 20 20 20 20 48 41 56 45 5f 46 55 4c 4c 46 53 HAVE_FULLFS
df270 59 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e 63 29 YNC, isFullsync)
df280 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
df290 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 _DISABLE_DIRSYNC
df2a0 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72 65 . /* The dire
df2b0 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6f 6e ctory sync is on
df2c0 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 66 20 ly attempted if
df2d0 66 75 6c 6c 5f 66 73 79 6e 63 20 69 73 0a 20 20 full_fsync is.
df2e0 20 20 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 20 ** turned off
df2f0 6f 72 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e 20 or unavailable.
df300 20 49 66 20 61 20 66 75 6c 6c 5f 66 73 79 6e 63 If a full_fsync
df310 20 6f 63 63 75 72 72 65 64 20 61 62 6f 76 65 2c occurred above,
df320 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 . ** then the
df330 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20 directory sync
df340 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e 0a is superfluous..
df350 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 */. if( (
df360 21 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 !HAVE_FULLFSYNC
df370 7c 7c 20 21 69 73 46 75 6c 6c 73 79 6e 63 29 20 || !isFullsync)
df380 26 26 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 && full_fsync(pF
df390 69 6c 65 2d 3e 64 69 72 66 64 2c 30 2c 30 29 20 ile->dirfd,0,0)
df3a0 29 7b 0a 20 20 20 20 20 20 20 2f 2a 0a 20 20 20 ){. /*.
df3b0 20 20 20 20 2a 2a 20 57 65 20 68 61 76 65 20 72 ** We have r
df3c0 65 63 65 69 76 65 64 20 6d 75 6c 74 69 70 6c 65 eceived multiple
df3d0 20 72 65 70 6f 72 74 73 20 6f 66 20 66 73 79 6e reports of fsyn
df3e0 63 28 29 20 72 65 74 75 72 6e 69 6e 67 0a 20 20 c() returning.
df3f0 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 73 20 77 ** errors w
df400 68 65 6e 20 61 70 70 6c 69 65 64 20 74 6f 20 64 hen applied to d
df410 69 72 65 63 74 6f 72 69 65 73 20 6f 6e 20 63 65 irectories on ce
df420 72 74 61 69 6e 20 66 69 6c 65 20 73 79 73 74 65 rtain file syste
df430 6d 73 2e 0a 20 20 20 20 20 20 20 2a 2a 20 41 20 ms.. ** A
df440 66 61 69 6c 65 64 20 64 69 72 65 63 74 6f 72 79 failed directory
df450 20 73 79 6e 63 20 69 73 20 6e 6f 74 20 61 20 62 sync is not a b
df460 69 67 20 64 65 61 6c 2e 20 20 53 6f 20 69 74 20 ig deal. So it
df470 73 65 65 6d 73 0a 20 20 20 20 20 20 20 2a 2a 20 seems. **
df480 62 65 74 74 65 72 20 74 6f 20 69 67 6e 6f 72 65 better to ignore
df490 20 74 68 65 20 65 72 72 6f 72 2e 20 20 54 69 63 the error. Tic
df4a0 6b 65 74 20 23 31 36 35 37 0a 20 20 20 20 20 20 ket #1657.
df4b0 20 2a 2f 0a 20 20 20 20 20 20 20 2f 2a 20 70 46 */. /* pF
df4c0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
df4d0 20 65 72 72 6e 6f 3b 20 2a 2f 0a 20 20 20 20 20 errno; */.
df4e0 20 20 2f 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 /* return SQLI
df4f0 54 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a 20 20 20 TE_IOERR; */.
df500 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 65 72 }.#endif. er
df510 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d r = close(pFile-
df520 3e 64 69 72 66 64 29 3b 20 2f 2a 20 4f 6e 6c 79 >dirfd); /* Only
df530 20 6e 65 65 64 20 74 6f 20 73 79 6e 63 20 6f 6e need to sync on
df540 63 65 2c 20 73 6f 20 63 6c 6f 73 65 20 74 68 65 ce, so close the
df550 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 72 72 3d */. if( err=
df560 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 =0 ){
df570 20 20 20 2f 2a 20 64 69 72 65 63 74 6f 72 79 20 /* directory
df580 77 68 65 6e 20 77 65 20 61 72 65 20 64 6f 6e 65 when we are done
df590 20 2a 2f 0a 20 20 20 20 20 20 70 46 69 6c 65 2d */. pFile-
df5a0 3e 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20 20 >dirfd = -1;.
df5b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 }else{. pF
df5c0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
df5d0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 errno;. rc
df5e0 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
df5f0 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 7d DIR_CLOSE;. }
df600 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
df610 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 ;.}../*.** Trunc
df620 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 ate an open file
df630 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20 to a specified
df640 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 size.*/.static i
df650 6e 74 20 75 6e 69 78 54 72 75 6e 63 61 74 65 28 nt unixTruncate(
df660 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
df670 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 , i64 nByte){.
df680 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 int rc;. assert
df690 28 20 69 64 20 29 3b 0a 20 20 53 69 6d 75 6c 61 ( id );. Simula
df6a0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
df6b0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 n SQLITE_IOERR_T
df6c0 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 63 20 RUNCATE );. rc
df6d0 3d 20 66 74 72 75 6e 63 61 74 65 28 28 28 75 6e = ftruncate(((un
df6e0 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 ixFile*)id)->h,
df6f0 28 6f 66 66 5f 74 29 6e 42 79 74 65 29 3b 0a 20 (off_t)nByte);.
df700 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 28 if( rc ){. (
df710 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e (unixFile*)id)->
df720 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e lastErrno = errn
df730 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 o;. return SQ
df740 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 LITE_IOERR_TRUNC
df750 41 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ATE;. }else{.
df760 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
df770 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a OK;. }.}../*.**
df780 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63 Determine the c
df790 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 urrent size of a
df7a0 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a file in bytes.*
df7b0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
df7c0 78 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 xFileSize(sqlite
df7d0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 3_file *id, i64
df7e0 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 *pSize){. int r
df7f0 63 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 c;. struct stat
df800 20 62 75 66 3b 0a 20 20 61 73 73 65 72 74 28 20 buf;. assert(
df810 69 64 20 29 3b 0a 20 20 72 63 20 3d 20 66 73 74 id );. rc = fst
df820 61 74 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 at(((unixFile*)i
df830 64 29 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20 20 d)->h, &buf);.
df840 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
df850 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72 rc=1 );. if( r
df860 63 21 3d 30 20 29 7b 0a 20 20 20 20 28 28 75 6e c!=0 ){. ((un
df870 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 ixFile*)id)->las
df880 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
df890 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
df8a0 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 E_IOERR_FSTAT;.
df8b0 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 62 75 }. *pSize = bu
df8c0 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20 2f 2a f.st_size;.. /*
df8d0 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20 When opening a
df8e0 7a 65 72 6f 2d 73 69 7a 65 20 64 61 74 61 62 61 zero-size databa
df8f0 73 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f 63 6b se, the findLock
df900 49 6e 66 6f 28 29 20 70 72 6f 63 65 64 75 72 65 Info() procedure
df910 0a 20 20 2a 2a 20 77 72 69 74 65 73 20 61 20 73 . ** writes a s
df920 69 6e 67 6c 65 20 62 79 74 65 20 69 6e 74 6f 20 ingle byte into
df930 74 68 61 74 20 66 69 6c 65 20 69 6e 20 6f 72 64 that file in ord
df940 65 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e er to work aroun
df950 64 20 61 20 62 75 67 0a 20 20 2a 2a 20 69 6e 20 d a bug. ** in
df960 74 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73 20 66 the OS-X msdos f
df970 69 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e 20 6f ilesystem. In o
df980 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 70 72 rder to avoid pr
df990 6f 62 6c 65 6d 73 20 77 69 74 68 20 75 70 70 65 oblems with uppe
df9a0 72 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c 20 77 r. ** layers, w
df9b0 65 20 6e 65 65 64 20 74 6f 20 72 65 70 6f 72 74 e need to report
df9c0 20 74 68 69 73 20 66 69 6c 65 20 73 69 7a 65 20 this file size
df9d0 61 73 20 7a 65 72 6f 20 65 76 65 6e 20 74 68 6f as zero even tho
df9e0 75 67 68 20 69 74 20 69 73 0a 20 20 2a 2a 20 72 ugh it is. ** r
df9f0 65 61 6c 6c 79 20 31 2e 20 20 20 54 69 63 6b 65 eally 1. Ticke
dfa00 74 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a 20 20 t #3260.. */.
dfa10 69 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20 29 20 if( *pSize==1 )
dfa20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a 20 20 *pSize = 0;...
dfa30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
dfa40 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f ;.}..#if SQLITE_
dfa50 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
dfa60 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 TYLE && defined(
dfa70 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a __APPLE__)./*.**
dfa80 20 48 61 6e 64 6c 65 72 20 66 6f 72 20 70 72 6f Handler for pro
dfa90 78 79 2d 6c 6f 63 6b 69 6e 67 20 66 69 6c 65 2d xy-locking file-
dfaa0 63 6f 6e 74 72 6f 6c 20 76 65 72 62 73 2e 20 20 control verbs.
dfab0 44 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 6e Defined below in
dfac0 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 69 6e 67 the.** proxying
dfad0 20 6c 6f 63 6b 69 6e 67 20 64 69 76 69 73 69 6f locking divisio
dfae0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 n..*/.static int
dfaf0 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f proxyFileContro
dfb00 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c l(sqlite3_file*,
dfb10 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 int,void*);.#end
dfb20 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72 if.../*.** Infor
dfb30 6d 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e 74 72 mation and contr
dfb40 6f 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20 66 69 ol of an open fi
dfb50 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 le handle..*/.st
dfb60 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c atic int unixFil
dfb70 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 eControl(sqlite3
dfb80 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f _file *id, int o
dfb90 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a p, void *pArg){.
dfba0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a switch( op ){.
dfbb0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
dfbc0 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a FCNTL_LOCKSTATE:
dfbd0 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 {. *(int*)
dfbe0 70 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69 6c pArg = ((unixFil
dfbf0 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 e*)id)->locktype
dfc00 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
dfc10 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
dfc20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
dfc30 4c 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 LAST_ERRNO: {.
dfc40 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 *(int*)pArg
dfc50 3d 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = ((unixFile*)id
dfc60 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 )->lastErrno;.
dfc70 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
dfc80 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e E_OK;. }.#ifn
dfc90 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f def NDEBUG. /
dfca0 2a 20 54 68 65 20 70 61 67 65 72 20 63 61 6c 6c * The pager call
dfcb0 73 20 74 68 69 73 20 6d 65 74 68 6f 64 20 74 6f s this method to
dfcc0 20 73 69 67 6e 61 6c 20 74 68 61 74 20 69 74 20 signal that it
dfcd0 68 61 73 20 64 6f 6e 65 0a 20 20 20 20 2a 2a 20 has done. **
dfce0 61 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 74 a rollback and t
dfcf0 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 hat the database
dfd00 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 75 6e is therefore un
dfd10 63 68 61 6e 67 65 64 20 61 6e 64 0a 20 20 20 20 changed and.
dfd20 2a 2a 20 69 74 20 68 65 6e 63 65 20 69 74 20 69 ** it hence it i
dfd30 73 20 4f 4b 20 66 6f 72 20 74 68 65 20 74 72 61 s OK for the tra
dfd40 6e 73 61 63 74 69 6f 6e 20 63 68 61 6e 67 65 20 nsaction change
dfd50 63 6f 75 6e 74 65 72 20 74 6f 20 62 65 0a 20 20 counter to be.
dfd60 20 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 0a ** unchanged..
dfd70 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 */. case
dfd80 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f SQLITE_FCNTL_DB_
dfd90 55 4e 43 48 41 4e 47 45 44 3a 20 7b 0a 20 20 20 UNCHANGED: {.
dfda0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 ((unixFile*)i
dfdb0 64 29 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30 d)->dbUpdate = 0
dfdc0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
dfdd0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
dfde0 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 #endif.#if SQLIT
dfdf0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
dfe00 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 _STYLE && define
dfe10 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20 d(__APPLE__).
dfe20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54 case SQLITE_SET
dfe30 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 0a _LOCKPROXYFILE:.
dfe40 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
dfe50 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c GET_LOCKPROXYFIL
dfe60 45 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 E: {. retur
dfe70 6e 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 n proxyFileContr
dfe80 6f 6c 28 69 64 2c 6f 70 2c 70 41 72 67 29 3b 0a ol(id,op,pArg);.
dfe90 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 }.#endif /*
dfea0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
dfeb0 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 CKING_STYLE && d
dfec0 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f efined(__APPLE__
dfed0 29 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75 72 ) */. }. retur
dfee0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
dfef0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
dff00 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 the sector size
dff10 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 in bytes of the
dff20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b underlying block
dff30 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 device for.** t
dff40 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c he specified fil
dff50 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 e. This is almos
dff60 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 t always 512 byt
dff70 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a es, but may be.*
dff80 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d * larger for som
dff90 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a e devices..**.**
dffa0 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 SQLite code ass
dffb0 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 umes this functi
dffc0 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 on cannot fail.
dffd0 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 It also assumes
dffe0 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 that.** if two f
dfff0 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64 iles are created
e0000 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c in the same fil
e0010 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f e-system directo
e0020 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 ry (i.e..** a da
e0030 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a tabase and its j
e0040 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 ournal file) tha
e0050 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a t the sector siz
e0060 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a e will be the.**
e0070 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a same for both..
e0080 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
e0090 69 78 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c ixSectorSize(sql
e00a0 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 ite3_file *NotUs
e00b0 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 ed){. UNUSED_PA
e00c0 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
e00d0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
e00e0 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 E_DEFAULT_SECTOR
e00f0 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 _SIZE;.}../*.**
e0100 52 65 74 75 72 6e 20 74 68 65 20 64 65 76 69 63 Return the devic
e0110 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 e characteristic
e0120 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20 s for the file.
e0130 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 30 This is always 0
e0140 20 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 73 74 for unix..*/.st
e0150 61 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 76 atic int unixDev
e0160 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
e0170 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 cs(sqlite3_file
e0180 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 *NotUsed){. UNU
e0190 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
e01a0 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e tUsed);. return
e01b0 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 0;.}../*.** Her
e01c0 65 20 65 6e 64 73 20 74 68 65 20 69 6d 70 6c 65 e ends the imple
e01d0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6c 6c mentation of all
e01e0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 sqlite3_file me
e01f0 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a thods..**.******
e0200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0210 20 45 6e 64 20 73 71 6c 69 74 65 33 5f 66 69 6c End sqlite3_fil
e0220 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a e Methods ******
e0230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0240 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
e0250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0290 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a ********/../*.**
e02a0 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 63 This division c
e02b0 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 ontains definiti
e02c0 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 ons of sqlite3_i
e02d0 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 o_methods object
e02e0 73 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d s that.** implem
e02f0 65 6e 74 20 76 61 72 69 6f 75 73 20 66 69 6c 65 ent various file
e0300 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 locking strateg
e0310 69 65 73 2e 20 20 49 74 20 61 6c 73 6f 20 63 6f ies. It also co
e0320 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f ntains definitio
e0330 6e 73 0a 2a 2a 20 6f 66 20 22 66 69 6e 64 65 72 ns.** of "finder
e0340 22 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 41 20 " functions. A
e0350 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 finder-function
e0360 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 is used to locat
e0370 65 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 e the appropriat
e0380 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f e.** sqlite3_io_
e0390 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 66 methods object f
e03a0 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 or a particular
e03b0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
e03c0 54 68 65 20 70 41 70 70 44 61 74 61 0a 2a 2a 20 The pAppData.**
e03d0 66 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 6c field of the sql
e03e0 69 74 65 33 5f 76 66 73 20 56 46 53 20 6f 62 6a ite3_vfs VFS obj
e03f0 65 63 74 73 20 61 72 65 20 69 6e 69 74 69 61 6c ects are initial
e0400 69 7a 65 64 20 74 6f 20 62 65 20 70 6f 69 6e 74 ized to be point
e0410 65 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 63 6f ers to.** the co
e0420 72 72 65 63 74 20 66 69 6e 64 65 72 2d 66 75 6e rrect finder-fun
e0430 63 74 69 6f 6e 20 66 6f 72 20 74 68 61 74 20 56 ction for that V
e0440 46 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 66 FS..**.** Most f
e0450 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 inder functions
e0460 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 return a pointer
e0470 20 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 to a fixed sqli
e0480 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a te3_io_methods.*
e0490 2a 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 6f * object. The o
e04a0 6e 6c 79 20 69 6e 74 65 72 65 73 74 69 6e 67 20 nly interesting
e04b0 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 finder-function
e04c0 69 73 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e is autolockIoFin
e04d0 64 65 72 2c 20 77 68 69 63 68 0a 2a 2a 20 6c 6f der, which.** lo
e04e0 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65 73 oks at the files
e04f0 79 73 74 65 6d 20 74 79 70 65 20 61 6e 64 20 74 ystem type and t
e0500 72 69 65 73 20 74 6f 20 67 75 65 73 73 20 74 68 ries to guess th
e0510 65 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 0a 2a e best locking.*
e0520 2a 20 73 74 72 61 74 65 67 79 20 66 72 6f 6d 20 * strategy from
e0530 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 that..**.** For
e0540 66 69 6e 64 65 72 2d 66 75 6e 74 69 6f 6e 20 46 finder-funtion F
e0550 2c 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 61 72 , two objects ar
e0560 65 20 63 72 65 61 74 65 64 3a 0a 2a 2a 0a 2a 2a e created:.**.**
e0570 20 20 20 20 28 31 29 20 54 68 65 20 72 65 61 6c (1) The real
e0580 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e finder-function
e0590 20 6e 61 6d 65 64 20 22 46 49 6d 70 74 28 29 22 named "FImpt()"
e05a0 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 41 ..**.** (2) A
e05b0 20 63 6f 6e 73 74 61 6e 74 20 70 6f 69 6e 74 65 constant pointe
e05c0 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 r to this functi
e05d0 6f 6e 20 6e 61 6d 65 64 20 6a 75 73 74 20 22 46 on named just "F
e05e0 22 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 20 70 6f "..**.**.** A po
e05f0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 20 70 inter to the F p
e0600 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 ointer is used a
e0610 73 20 74 68 65 20 70 41 70 70 44 61 74 61 20 76 s the pAppData v
e0620 61 6c 75 65 20 66 6f 72 20 56 46 53 0a 2a 2a 20 alue for VFS.**
e0630 6f 62 6a 65 63 74 73 2e 20 20 57 65 20 68 61 76 objects. We hav
e0640 65 20 74 6f 20 64 6f 20 74 68 69 73 20 69 6e 73 e to do this ins
e0650 74 65 61 64 20 6f 66 20 6c 65 74 74 69 6e 67 20 tead of letting
e0660 70 41 70 70 44 61 74 61 20 70 6f 69 6e 74 0a 2a pAppData point.*
e0670 2a 20 64 69 72 65 63 74 6c 79 20 61 74 20 74 68 * directly at th
e0680 65 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f e finder-functio
e0690 6e 20 73 69 6e 63 65 20 43 39 30 20 72 75 6c 65 n since C90 rule
e06a0 73 20 70 72 65 76 65 6e 74 20 61 20 76 6f 69 64 s prevent a void
e06b0 2a 0a 2a 2a 20 66 72 6f 6d 20 62 65 20 63 61 73 *.** from be cas
e06c0 74 20 69 6e 74 6f 20 61 20 66 75 6e 63 74 69 6f t into a functio
e06d0 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a n pointer..**.**
e06e0 0a 2a 2a 20 45 61 63 68 20 69 6e 73 74 61 6e 63 .** Each instanc
e06f0 65 20 6f 66 20 74 68 69 73 20 6d 61 63 72 6f 20 e of this macro
e0700 67 65 6e 65 72 61 74 65 73 20 74 77 6f 20 6f 62 generates two ob
e0710 6a 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a jects:.**.** *
e0720 20 20 41 20 63 6f 6e 73 74 61 6e 74 20 73 71 6c A constant sql
e0730 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
e0740 6f 62 6a 65 63 74 20 63 61 6c 6c 20 4d 45 54 48 object call METH
e0750 4f 44 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b OD that has lock
e0760 69 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68 ing.** meth
e0770 6f 64 73 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c ods CLOSE, LOCK,
e0780 20 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45 53 4c 4f UNLOCK, CKRESLO
e0790 43 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 CK..**.** * A
e07a0 6e 20 49 2f 4f 20 6d 65 74 68 6f 64 20 66 69 6e n I/O method fin
e07b0 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c der function cal
e07c0 6c 65 64 20 46 49 4e 44 45 52 20 74 68 61 74 20 led FINDER that
e07d0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
e07e0 72 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 r.** to the
e07f0 20 4d 45 54 48 4f 44 20 6f 62 6a 65 63 74 20 69 METHOD object i
e0800 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 n the previous b
e0810 75 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e ullet..*/.#defin
e0820 65 20 49 4f 4d 45 54 48 4f 44 53 28 46 49 4e 44 e IOMETHODS(FIND
e0830 45 52 2c 20 4d 45 54 48 4f 44 2c 20 43 4c 4f 53 ER, METHOD, CLOS
e0840 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c E, LOCK, UNLOCK,
e0850 20 43 4b 4c 4f 43 4b 29 20 20 20 20 20 20 20 20 CKLOCK)
e0860 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20 \.static
e0870 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f const sqlite3_io
e0880 5f 6d 65 74 68 6f 64 73 20 4d 45 54 48 4f 44 20 _methods METHOD
e0890 3d 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 = {
e08a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e08b0 20 20 20 20 20 20 5c 0a 20 20 20 31 2c 20 20 20 \. 1,
e08c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e08d0 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 /* iVersi
e08e0 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 on */
e08f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0900 20 20 20 20 20 5c 0a 20 20 20 43 4c 4f 53 45 2c \. CLOSE,
e0910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0920 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 /* xClose
e0930 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e0940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0950 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 52 65 61 \. unixRea
e0960 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d,
e0970 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f /* xRead */
e0980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e09a0 20 20 20 5c 0a 20 20 20 75 6e 69 78 57 72 69 74 \. unixWrit
e09b0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
e09c0 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f /* xWrite */
e09d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e09e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e09f0 20 20 5c 0a 20 20 20 75 6e 69 78 54 72 75 6e 63 \. unixTrunc
e0a00 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ate,
e0a10 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 /* xTruncate
e0a20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e0a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0a40 20 5c 0a 20 20 20 75 6e 69 78 53 79 6e 63 2c 20 \. unixSync,
e0a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0a60 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20 20 20 /* xSync */
e0a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0a90 5c 0a 20 20 20 75 6e 69 78 46 69 6c 65 53 69 7a \. unixFileSiz
e0aa0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
e0ab0 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f /* xFileSize */
e0ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e0ae0 0a 20 20 20 4c 4f 43 4b 2c 20 20 20 20 20 20 20 . LOCK,
e0af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0b00 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 /* xLock */
e0b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
e0b30 20 20 20 55 4e 4c 4f 43 4b 2c 20 20 20 20 20 20 UNLOCK,
e0b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e0b50 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20 20 20 * xUnlock */
e0b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0b70 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
e0b80 20 20 43 4b 4c 4f 43 4b 2c 20 20 20 20 20 20 20 CKLOCK,
e0b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e0ba0 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
e0bb0 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ock */
e0bc0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
e0bd0 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c unixFileControl
e0be0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
e0bf0 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 20 xFileControl */
e0c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0c10 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
e0c20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 2c 20 unixSectorSize,
e0c30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e0c40 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 20 20 20 SectorSize */
e0c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0c60 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 \. u
e0c70 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 63 74 nixDeviceCharact
e0c80 65 72 69 73 74 69 63 73 20 20 20 2f 2a 20 78 44 eristics /* xD
e0c90 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65 eviceCapabilitie
e0ca0 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 s */
e0cb0 20 20 20 20 20 20 20 20 20 5c 0a 7d 3b 20 20 20 \.};
e0cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0d00 20 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 \.static
e0d10 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
e0d20 6f 5f 6d 65 74 68 6f 64 73 20 2a 46 49 4e 44 45 o_methods *FINDE
e0d30 52 23 23 49 6d 70 6c 28 63 6f 6e 73 74 20 63 68 R##Impl(const ch
e0d40 61 72 20 2a 7a 2c 20 75 6e 69 78 46 69 6c 65 20 ar *z, unixFile
e0d50 2a 70 29 7b 20 20 20 5c 0a 20 20 55 4e 55 53 45 *p){ \. UNUSE
e0d60 44 5f 50 41 52 41 4d 45 54 45 52 28 7a 29 3b 20 D_PARAMETER(z);
e0d70 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
e0d80 28 70 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 (p);
e0d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0da0 20 20 20 20 20 20 5c 0a 20 20 72 65 74 75 72 6e \. return
e0db0 20 26 4d 45 54 48 4f 44 3b 20 20 20 20 20 20 20 &METHOD;
e0dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0df0 20 20 20 20 20 5c 0a 7d 20 20 20 20 20 20 20 20 \.}
e0e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0e40 20 20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e \.static con
e0e50 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
e0e60 74 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 46 thods *(*const F
e0e70 49 4e 44 45 52 29 28 63 6f 6e 73 74 20 63 68 61 INDER)(const cha
e0e80 72 2a 2c 75 6e 69 78 46 69 6c 65 20 2a 70 29 20 r*,unixFile *p)
e0e90 20 20 20 5c 0a 20 20 20 20 3d 20 46 49 4e 44 45 \. = FINDE
e0ea0 52 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 R##Impl;../*.**
e0eb0 48 65 72 65 20 61 72 65 20 61 6c 6c 20 6f 66 20 Here are all of
e0ec0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d the sqlite3_io_m
e0ed0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 ethods objects f
e0ee0 6f 72 20 65 61 63 68 20 6f 66 20 74 68 65 0a 2a or each of the.*
e0ef0 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 * locking strate
e0f00 67 69 65 73 2e 20 20 46 75 6e 63 74 69 6f 6e 73 gies. Functions
e0f10 20 74 68 61 74 20 72 65 74 75 72 6e 20 70 6f 69 that return poi
e0f20 6e 74 65 72 73 20 74 6f 20 74 68 65 73 65 20 6d nters to these m
e0f30 65 74 68 6f 64 73 0a 2a 2a 20 61 72 65 20 61 6c ethods.** are al
e0f40 73 6f 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 49 so created..*/.I
e0f50 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 6f 73 69 OMETHODS(. posi
e0f60 78 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 xIoFinder,
e0f70 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 /* Finder
e0f80 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f function name */
e0f90 0a 20 20 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 . posixIoMethod
e0fa0 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s, /*
e0fb0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
e0fc0 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a ds object name *
e0fd0 2f 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20 /. unixClose,
e0fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e0ff0 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a xClose method *
e1000 2f 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20 /. unixLock,
e1010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e1020 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f xLock method */
e1030 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c 20 20 . unixUnlock,
e1040 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e1050 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a xUnlock method *
e1060 2f 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73 /. unixCheckRes
e1070 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 2f 2a ervedLock /*
e1080 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
e1090 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a ock method */.).
e10a0 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e 6f 6c IOMETHODS(. nol
e10b0 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 ockIoFinder,
e10c0 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 /* Finder
e10d0 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a function name *
e10e0 2f 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 /. nolockIoMeth
e10f0 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a ods, /*
e1100 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
e1110 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 ods object name
e1120 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 */. nolockClose
e1130 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
e1140 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 * xClose method
e1150 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c */. nolockLock,
e1160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e1170 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a * xLock method *
e1180 2f 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b /. nolockUnlock
e1190 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
e11a0 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 xUnlock method
e11b0 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b */. nolockCheck
e11c0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 2f ReservedLock /
e11d0 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 * xCheckReserved
e11e0 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 Lock method */.)
e11f0 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 64 6f .IOMETHODS(. do
e1200 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 tlockIoFinder,
e1210 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 /* Finde
e1220 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 r function name
e1230 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 4d 65 */. dotlockIoMe
e1240 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 2f thods, /
e1250 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 * sqlite3_io_met
e1260 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 hods object name
e1270 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 6c 6f */. dotlockClo
e1280 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 se,
e1290 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 /* xClose method
e12a0 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63 */. dotlockLoc
e12b0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k,
e12c0 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 /* xLock method
e12d0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f */. dotlockUnlo
e12e0 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f ck, /
e12f0 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 * xUnlock method
e1300 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 68 65 */. dotlockChe
e1310 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 ckReservedLock
e1320 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 /* xCheckReserve
e1330 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a dLock method */.
e1340 29 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e )..#if SQLITE_EN
e1350 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
e1360 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b LE && !OS_VXWORK
e1370 53 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 66 S.IOMETHODS(. f
e1380 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 lockIoFinder,
e1390 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 /* Find
e13a0 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 er function name
e13b0 20 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f 4d 65 74 */. flockIoMet
e13c0 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 hods,
e13d0 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 /* sqlite3_io_me
e13e0 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d thods object nam
e13f0 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 6c 6f 73 e */. flockClos
e1400 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
e1410 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f /* xClose metho
e1420 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c 6f 63 6b d */. flockLock
e1430 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e1440 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 /* xLock method
e1450 20 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 */. flockUnloc
e1460 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k,
e1470 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f /* xUnlock metho
e1480 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 68 65 63 d */. flockChec
e1490 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 kReservedLock
e14a0 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 /* xCheckReserv
e14b0 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f edLock method */
e14c0 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 4f .).#endif..#if O
e14d0 53 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54 48 S_VXWORKS.IOMETH
e14e0 4f 44 53 28 0a 20 20 73 65 6d 49 6f 46 69 6e 64 ODS(. semIoFind
e14f0 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 er,
e1500 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 /* Finder funct
e1510 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65 ion name */. se
e1520 6d 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 mIoMethods,
e1530 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 /* sqlit
e1540 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 e3_io_methods ob
e1550 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 ject name */. s
e1560 65 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 emClose,
e1570 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f /* xClo
e1580 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 se method */. s
e1590 65 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 emLock,
e15a0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 /* xLoc
e15b0 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 k method */. se
e15c0 6d 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 mUnlock,
e15d0 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f /* xUnlo
e15e0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 ck method */. s
e15f0 65 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c emCheckReservedL
e1600 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 ock /* xChe
e1610 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d ckReservedLock m
e1620 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 ethod */.).#endi
e1630 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f f..#if defined(_
e1640 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c _APPLE__) && SQL
e1650 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
e1660 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 48 4f NG_STYLE.IOMETHO
e1670 44 53 28 0a 20 20 61 66 70 49 6f 46 69 6e 64 65 DS(. afpIoFinde
e1680 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r,
e1690 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 /* Finder functi
e16a0 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70 on name */. afp
e16b0 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 IoMethods,
e16c0 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 /* sqlite
e16d0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 3_io_methods obj
e16e0 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 ect name */. af
e16f0 70 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 pClose,
e1700 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 /* xClos
e1710 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 e method */. af
e1720 70 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 pLock,
e1730 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b /* xLock
e1740 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 method */. afp
e1750 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 Unlock,
e1760 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 /* xUnloc
e1770 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 k method */. af
e1780 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f pCheckReservedLo
e1790 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 ck /* xChec
e17a0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 kReservedLock me
e17b0 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 thod */.).#endif
e17c0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 57 68 6f ../*.** The "Who
e17d0 6c 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 22 le File Locking"
e17e0 20 66 69 6e 64 65 72 20 72 65 74 75 72 6e 73 20 finder returns
e17f0 74 68 65 20 73 61 6d 65 20 73 65 74 20 6f 66 20 the same set of
e1800 6d 65 74 68 6f 64 73 20 61 73 0a 2a 2a 20 74 68 methods as.** th
e1810 65 20 70 6f 73 69 78 20 6c 6f 63 6b 69 6e 67 20 e posix locking
e1820 66 69 6e 64 65 72 2e 20 20 42 75 74 20 69 74 20 finder. But it
e1830 61 6c 73 6f 20 73 65 74 73 20 74 68 65 20 53 51 also sets the SQ
e1840 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f LITE_WHOLE_FILE_
e1850 4c 4f 43 4b 49 4e 47 0a 2a 2a 20 66 6c 61 67 20 LOCKING.** flag
e1860 74 6f 20 66 6f 72 63 65 20 74 68 65 20 70 6f 73 to force the pos
e1870 69 78 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b ix advisory lock
e1880 73 20 74 6f 20 63 6f 76 65 72 20 74 68 65 20 77 s to cover the w
e1890 68 6f 6c 65 20 66 69 6c 65 20 69 6e 73 74 65 61 hole file instea
e18a0 64 0a 2a 2a 20 6f 66 20 6a 75 73 74 20 61 20 73 d.** of just a s
e18b0 6d 61 6c 6c 20 73 70 61 6e 20 6f 66 20 62 79 74 mall span of byt
e18c0 65 73 20 6e 65 61 72 20 74 68 65 20 31 47 69 42 es near the 1GiB
e18d0 20 62 6f 75 6e 64 61 72 79 2e 20 20 57 68 6f 6c boundary. Whol
e18e0 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 0a 2a e File Locking.*
e18f0 2a 20 69 73 20 75 73 65 66 75 6c 20 6f 6e 20 4e * is useful on N
e1900 46 53 2d 6d 6f 75 6e 74 65 64 20 66 69 6c 65 73 FS-mounted files
e1910 20 73 69 6e 63 65 20 69 74 20 68 65 6c 70 73 20 since it helps
e1920 4e 46 53 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20 NFS to maintain
e1930 63 61 63 68 65 0a 2a 2a 20 63 6f 68 65 72 65 6e cache.** coheren
e1940 63 79 2e 20 20 42 75 74 20 69 74 20 69 73 20 61 cy. But it is a
e1950 20 64 65 74 72 69 6d 65 6e 74 20 74 6f 20 6f 74 detriment to ot
e1960 68 65 72 20 66 69 6c 65 73 79 73 74 65 6d 73 20 her filesystems
e1970 73 69 6e 63 65 20 69 74 20 72 75 6e 73 0a 2a 2a since it runs.**
e1980 20 73 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74 61 74 slower..*/.stat
e1990 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
e19a0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 6f 73 _io_methods *pos
e19b0 69 78 57 66 6c 49 6f 46 69 6e 64 65 72 49 6d 70 ixWflIoFinderImp
e19c0 6c 28 63 6f 6e 73 74 20 63 68 61 72 2a 7a 2c 20 l(const char*z,
e19d0 75 6e 69 78 46 69 6c 65 2a 70 29 7b 0a 20 20 55 unixFile*p){. U
e19e0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
e19f0 7a 29 3b 0a 20 20 70 2d 3e 66 69 6c 65 46 6c 61 z);. p->fileFla
e1a00 67 73 20 3d 20 53 51 4c 49 54 45 5f 57 48 4f 4c gs = SQLITE_WHOL
e1a10 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a E_FILE_LOCKING;.
e1a20 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 return &posixI
e1a30 6f 4d 65 74 68 6f 64 73 3b 0a 7d 0a 73 74 61 74 oMethods;.}.stat
e1a40 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
e1a50 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20 2a _io_methods . *
e1a60 28 2a 63 6f 6e 73 74 20 70 6f 73 69 78 57 66 6c (*const posixWfl
e1a70 49 6f 46 69 6e 64 65 72 29 28 63 6f 6e 73 74 20 IoFinder)(const
e1a80 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 20 2a char*,unixFile *
e1a90 70 29 20 3d 20 70 6f 73 69 78 57 66 6c 49 6f 46 p) = posixWflIoF
e1aa0 69 6e 64 65 72 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a inderImpl;../*.*
e1ab0 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b * The proxy lock
e1ac0 69 6e 67 20 6d 65 74 68 6f 64 20 69 73 20 61 20 ing method is a
e1ad0 22 73 75 70 65 72 2d 6d 65 74 68 6f 64 22 20 69 "super-method" i
e1ae0 6e 20 74 68 65 20 73 65 6e 73 65 20 74 68 61 74 n the sense that
e1af0 20 69 74 0a 2a 2a 20 6f 70 65 6e 73 20 73 65 63 it.** opens sec
e1b00 6f 6e 64 61 72 79 20 66 69 6c 65 20 64 65 73 63 ondary file desc
e1b10 72 69 70 74 6f 72 73 20 66 6f 72 20 74 68 65 20 riptors for the
e1b20 63 6f 6e 63 68 20 61 6e 64 20 6c 6f 63 6b 20 66 conch and lock f
e1b30 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 69 74 20 75 iles and.** it u
e1b40 73 65 73 20 70 72 6f 78 79 2c 20 64 6f 74 2d 66 ses proxy, dot-f
e1b50 69 6c 65 2c 20 41 46 50 2c 20 61 6e 64 20 66 6c ile, AFP, and fl
e1b60 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 6d 65 ock() locking me
e1b70 74 68 6f 64 73 20 6f 6e 20 74 68 6f 73 65 0a 2a thods on those.*
e1b80 2a 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 * secondary file
e1b90 73 2e 20 20 46 6f 72 20 74 68 69 73 20 72 65 61 s. For this rea
e1ba0 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 6f son, the divisio
e1bb0 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 n that implement
e1bc0 73 0a 2a 2a 20 70 72 6f 78 79 20 6c 6f 63 6b 69 s.** proxy locki
e1bd0 6e 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6d 75 ng is located mu
e1be0 63 68 20 66 75 72 74 68 65 72 20 64 6f 77 6e 20 ch further down
e1bf0 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 42 75 in the file. Bu
e1c00 74 20 77 65 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 t we need.** to
e1c10 67 6f 20 61 68 65 61 64 20 61 6e 64 20 64 65 66 go ahead and def
e1c20 69 6e 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f ine the sqlite3_
e1c30 69 6f 5f 6d 65 74 68 6f 64 73 20 61 6e 64 20 66 io_methods and f
e1c40 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 0a 2a inder function.*
e1c50 2a 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f 63 6b * for proxy lock
e1c60 69 6e 67 20 68 65 72 65 2e 20 20 53 6f 20 77 65 ing here. So we
e1c70 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72 65 forward declare
e1c80 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 the I/O methods
e1c90 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 ..*/.#if defined
e1ca0 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 (__APPLE__) && S
e1cb0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
e1cc0 4b 49 4e 47 5f 53 54 59 4c 45 0a 73 74 61 74 69 KING_STYLE.stati
e1cd0 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 c int proxyClose
e1ce0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b (sqlite3_file*);
e1cf0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 .static int prox
e1d00 79 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 yLock(sqlite3_fi
e1d10 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69 le*, int);.stati
e1d20 63 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63 c int proxyUnloc
e1d30 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c k(sqlite3_file*,
e1d40 20 69 6e 74 29 3b 0a 73 74 61 74 69 63 20 69 6e int);.static in
e1d50 74 20 70 72 6f 78 79 43 68 65 63 6b 52 65 73 65 t proxyCheckRese
e1d60 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 rvedLock(sqlite3
e1d70 5f 66 69 6c 65 2a 2c 20 69 6e 74 2a 29 3b 0a 49 _file*, int*);.I
e1d80 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 72 6f 78 OMETHODS(. prox
e1d90 79 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 yIoFinder,
e1da0 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 /* Finder
e1db0 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f function name */
e1dc0 0a 20 20 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 . proxyIoMethod
e1dd0 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s, /*
e1de0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
e1df0 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a ds object name *
e1e00 2f 0a 20 20 70 72 6f 78 79 43 6c 6f 73 65 2c 20 /. proxyClose,
e1e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e1e20 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a xClose method *
e1e30 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 2c 20 20 /. proxyLock,
e1e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e1e50 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f xLock method */
e1e60 0a 20 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 2c 20 . proxyUnlock,
e1e70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e1e80 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a xUnlock method *
e1e90 2f 0a 20 20 70 72 6f 78 79 43 68 65 63 6b 52 65 /. proxyCheckRe
e1ea0 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 2f 2a servedLock /*
e1eb0 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
e1ec0 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a ock method */.).
e1ed0 23 65 6e 64 69 66 0a 0a 0a 23 69 66 20 64 65 66 #endif...#if def
e1ee0 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 ined(__APPLE__)
e1ef0 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 && SQLITE_ENABLE
e1f00 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f _LOCKING_STYLE./
e1f10 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 6e 64 * .** This "find
e1f20 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 er" function att
e1f30 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d 69 empts to determi
e1f40 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f 63 6b ne the best lock
e1f50 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a 2a 2a ing strategy .**
e1f60 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 for the databas
e1f70 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61 74 68 e file "filePath
e1f80 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65 74 75 ". It then retu
e1f90 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f rns the sqlite3_
e1fa0 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62 io_methods.** ob
e1fb0 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65 6d ject that implem
e1fc0 65 6e 74 73 20 74 68 61 74 20 73 74 72 61 74 65 ents that strate
e1fd0 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 gy..**.** This i
e1fe0 73 20 66 6f 72 20 4d 61 63 4f 53 58 20 6f 6e 6c s for MacOSX onl
e1ff0 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e y..*/.static con
e2000 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
e2010 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63 6b 49 thods *autolockI
e2020 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20 20 63 oFinderImpl(. c
e2030 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 50 onst char *fileP
e2040 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d 65 20 ath, /* name
e2050 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
e2060 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69 file */. unixFi
e2070 6c 65 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 le *pNew
e2080 20 20 20 2f 2a 20 6f 70 65 6e 20 66 69 6c 65 20 /* open file
e2090 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 64 object for the d
e20a0 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
e20b0 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 ){. static cons
e20c0 74 20 73 74 72 75 63 74 20 4d 61 70 70 69 6e 67 t struct Mapping
e20d0 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 {. const cha
e20e0 72 20 2a 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20 r *zFilesystem;
e20f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e2100 46 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20 Filesystem type
e2110 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 name */. cons
e2120 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
e2130 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 3b 20 hods *pMethods;
e2140 20 20 2f 2a 20 41 70 70 72 6f 70 72 69 61 74 65 /* Appropriate
e2150 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 locking method
e2160 2a 2f 0a 20 20 7d 20 61 4d 61 70 5b 5d 20 3d 20 */. } aMap[] =
e2170 7b 0a 20 20 20 20 7b 20 22 68 66 73 22 2c 20 20 {. { "hfs",
e2180 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 &posixIoMethod
e2190 73 20 7d 2c 0a 20 20 20 20 7b 20 22 75 66 73 22 s },. { "ufs"
e21a0 2c 20 20 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 , &posixIoMet
e21b0 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22 61 hods },. { "a
e21c0 66 70 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65 fpfs", &afpIoMe
e21d0 74 68 6f 64 73 20 7d 2c 0a 23 69 66 64 65 66 20 thods },.#ifdef
e21e0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 46 SQLITE_ENABLE_AF
e21f0 50 5f 4c 4f 43 4b 49 4e 47 5f 53 4d 42 0a 20 20 P_LOCKING_SMB.
e2200 20 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 26 61 { "smbfs", &a
e2210 66 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 fpIoMethods },.#
e2220 65 6c 73 65 0a 20 20 20 20 7b 20 22 73 6d 62 66 else. { "smbf
e2230 73 22 2c 20 20 26 66 6c 6f 63 6b 49 6f 4d 65 74 s", &flockIoMet
e2240 68 6f 64 73 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 hods },.#endif.
e2250 20 20 20 7b 20 22 77 65 62 64 61 76 22 2c 20 26 { "webdav", &
e2260 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 nolockIoMethods
e2270 7d 2c 0a 20 20 20 20 7b 20 30 2c 20 30 20 7d 0a },. { 0, 0 }.
e2280 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 };. int i;.
e2290 73 74 72 75 63 74 20 73 74 61 74 66 73 20 66 73 struct statfs fs
e22a0 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74 20 66 Info;. struct f
e22b0 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a lock lockInfo;..
e22c0 20 20 69 66 28 20 21 66 69 6c 65 50 61 74 68 20 if( !filePath
e22d0 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c ){. /* If fil
e22e0 65 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 ePath==NULL that
e22f0 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 means we are de
e2300 61 6c 69 6e 67 20 77 69 74 68 20 61 20 74 72 61 aling with a tra
e2310 6e 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 nsient file.
e2320 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 ** that does not
e2330 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b need to be lock
e2340 65 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 ed. */. retur
e2350 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f n &nolockIoMetho
e2360 64 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 74 ds;. }. if( st
e2370 61 74 66 73 28 66 69 6c 65 50 61 74 68 2c 20 26 atfs(filePath, &
e2380 66 73 49 6e 66 6f 29 20 21 3d 20 2d 31 20 29 7b fsInfo) != -1 ){
e2390 0a 20 20 20 20 69 66 28 20 66 73 49 6e 66 6f 2e . if( fsInfo.
e23a0 66 5f 66 6c 61 67 73 20 26 20 4d 4e 54 5f 52 44 f_flags & MNT_RD
e23b0 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 72 65 ONLY ){. re
e23c0 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 turn &nolockIoMe
e23d0 74 68 6f 64 73 3b 0a 20 20 20 20 7d 0a 20 20 20 thods;. }.
e23e0 20 66 6f 72 28 69 3d 30 3b 20 61 4d 61 70 5b 69 for(i=0; aMap[i
e23f0 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20 69 ].zFilesystem; i
e2400 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 ++){. if( s
e2410 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66 trcmp(fsInfo.f_f
e2420 73 74 79 70 65 6e 61 6d 65 2c 20 61 4d 61 70 5b stypename, aMap[
e2430 69 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d 29 3d i].zFilesystem)=
e2440 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 =0 ){. re
e2450 74 75 72 6e 20 61 4d 61 70 5b 69 5d 2e 70 4d 65 turn aMap[i].pMe
e2460 74 68 6f 64 73 3b 0a 20 20 20 20 20 20 7d 0a 20 thods;. }.
e2470 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 }. }.. /* D
e2480 65 66 61 75 6c 74 20 63 61 73 65 2e 20 48 61 6e efault case. Han
e2490 64 6c 65 73 2c 20 61 6d 6f 6e 67 73 74 20 6f 74 dles, amongst ot
e24a0 68 65 72 73 2c 20 22 6e 66 73 22 2e 0a 20 20 2a hers, "nfs".. *
e24b0 2a 20 54 65 73 74 20 62 79 74 65 2d 72 61 6e 67 * Test byte-rang
e24c0 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 66 63 6e e lock using fcn
e24d0 74 6c 28 29 2e 20 49 66 20 74 68 65 20 63 61 6c tl(). If the cal
e24e0 6c 20 73 75 63 63 65 65 64 73 2c 20 0a 20 20 2a l succeeds, . *
e24f0 2a 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 * assume that th
e2500 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 e file-system su
e2510 70 70 6f 72 74 73 20 50 4f 53 49 58 20 73 74 79 pports POSIX sty
e2520 6c 65 20 6c 6f 63 6b 73 2e 20 0a 20 20 2a 2f 0a le locks. . */.
e2530 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e lockInfo.l_len
e2540 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f = 1;. lockInfo
e2550 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 .l_start = 0;.
e2560 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63 lockInfo.l_whenc
e2570 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 e = SEEK_SET;.
e2580 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65 20 lockInfo.l_type
e2590 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 66 28 = F_RDLCK;. if(
e25a0 20 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c 20 fcntl(pNew->h,
e25b0 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e F_GETLK, &lockIn
e25c0 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 20 20 fo)!=-1 ) {.
e25d0 70 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20 pNew->fileFlags
e25e0 3d 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 = SQLITE_WHOLE_F
e25f0 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a 20 20 20 ILE_LOCKING;.
e2600 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f return &posixIo
e2610 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 Methods;. }else
e2620 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64 6f {. return &do
e2630 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a tlockIoMethods;.
e2640 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e }.}.static con
e2650 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
e2660 74 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 thods . *(*cons
e2670 74 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 t autolockIoFind
e2680 65 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c er)(const char*,
e2690 75 6e 69 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 unixFile*) = aut
e26a0 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 olockIoFinderImp
e26b0 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 l;..#endif /* de
e26c0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
e26d0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
e26e0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
e26f0 2a 2f 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 */..#if OS_VXWOR
e2700 4b 53 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 KS && SQLITE_ENA
e2710 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
e2720 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 E./* .** This "f
e2730 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 inder" function
e2740 61 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 attempts to dete
e2750 72 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c rmine the best l
e2760 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 ocking strategy
e2770 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61 .** for the data
e2780 62 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50 base file "fileP
e2790 61 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72 ath". It then r
e27a0 65 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74 eturns the sqlit
e27b0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a e3_io_methods.**
e27c0 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 object that imp
e27d0 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72 lements that str
e27e0 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ategy..**.** Thi
e27f0 73 20 69 73 20 66 6f 72 20 56 58 57 6f 72 6b 73 s is for VXWorks
e2800 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 only..*/.static
e2810 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
e2820 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c o_methods *autol
e2830 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28 ockIoFinderImpl(
e2840 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 . const char *f
e2850 69 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e ilePath, /* n
e2860 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
e2870 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e ase file */. un
e2880 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 20 ixFile *pNew
e2890 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 6f 70 /* the op
e28a0 65 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74 20 2a en file object *
e28b0 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 66 6c /.){. struct fl
e28c0 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 ock lockInfo;..
e28d0 20 69 66 28 20 21 66 69 6c 65 50 61 74 68 20 29 if( !filePath )
e28e0 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c 65 {. /* If file
e28f0 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 Path==NULL that
e2900 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61 means we are dea
e2910 6c 69 6e 67 20 77 69 74 68 20 61 20 74 72 61 6e ling with a tran
e2920 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a sient file. *
e2930 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 * that does not
e2940 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 need to be locke
e2950 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e d. */. return
e2960 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 &nolockIoMethod
e2970 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 s;. }.. /* Tes
e2980 74 20 69 66 20 66 63 6e 74 6c 28 29 20 69 73 20 t if fcntl() is
e2990 73 75 70 70 6f 72 74 65 64 20 61 6e 64 20 75 73 supported and us
e29a0 65 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f e POSIX style lo
e29b0 63 6b 73 2e 0a 20 20 2a 2a 20 4f 74 68 65 72 77 cks.. ** Otherw
e29c0 69 73 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f ise fall back to
e29d0 20 74 68 65 20 6e 61 6d 65 64 20 73 65 6d 61 70 the named semap
e29e0 68 6f 72 65 20 6d 65 74 68 6f 64 2e 0a 20 20 2a hore method.. *
e29f0 2f 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c /. lockInfo.l_l
e2a00 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e en = 1;. lockIn
e2a10 66 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a fo.l_start = 0;.
e2a20 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 lockInfo.l_whe
e2a30 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a nce = SEEK_SET;.
e2a40 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 lockInfo.l_typ
e2a50 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 e = F_RDLCK;. i
e2a60 66 28 20 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 f( fcntl(pNew->h
e2a70 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b , F_GETLK, &lock
e2a80 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 Info)!=-1 ) {.
e2a90 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 return &posixI
e2aa0 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 oMethods;. }els
e2ab0 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 73 e{. return &s
e2ac0 65 6d 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d emIoMethods;. }
e2ad0 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 .}.static const
e2ae0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
e2af0 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 20 61 ds . *(*const a
e2b00 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 29 utolockIoFinder)
e2b10 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 (const char*,uni
e2b20 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f 6c 6f xFile*) = autolo
e2b30 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a ckIoFinderImpl;.
e2b40 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 .#endif /* OS_VX
e2b50 57 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f WORKS && SQLITE_
e2b60 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
e2b70 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 TYLE */../*.** A
e2b80 6e 20 61 62 73 74 72 61 63 74 20 74 79 70 65 20 n abstract type
e2b90 66 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f for a pointer to
e2ba0 20 61 20 49 4f 20 6d 65 74 68 6f 64 20 66 69 6e a IO method fin
e2bb0 64 65 72 20 66 75 6e 63 74 69 6f 6e 3a 0a 2a 2f der function:.*/
e2bc0 0a 74 79 70 65 64 65 66 20 63 6f 6e 73 74 20 73 .typedef const s
e2bd0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e2be0 73 20 2a 28 2a 66 69 6e 64 65 72 5f 74 79 70 65 s *(*finder_type
e2bf0 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e )(const char*,un
e2c00 69 78 46 69 6c 65 2a 29 3b 0a 0a 0a 2f 2a 2a 2a ixFile*);.../***
e2c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e2c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e2c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e2c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e2c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
e2c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e2c70 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 ****** sqlite3_v
e2c80 66 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a fs methods *****
e2c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e2ca0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
e2cb0 69 73 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 is division cont
e2cc0 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 ains the impleme
e2cd0 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f ntation of metho
e2ce0 64 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 71 6c ds on the.** sql
e2cf0 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e ite3_vfs object.
e2d00 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 .*/../*.** Initi
e2d10 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 74 65 6e alize the conten
e2d20 74 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 ts of the unixFi
e2d30 6c 65 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 le structure poi
e2d40 6e 74 65 64 20 74 6f 20 62 79 20 70 49 64 2e 0a nted to by pId..
e2d50 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 */.static int fi
e2d60 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 0a 20 20 llInUnixFile(.
e2d70 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
e2d80 73 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 s, /* Point
e2d90 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 er to vfs object
e2da0 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20 20 20 */. int h,
e2db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e2dc0 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 Open file descr
e2dd0 69 70 74 6f 72 20 6f 66 20 66 69 6c 65 20 62 65 iptor of file be
e2de0 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 ing opened */.
e2df0 69 6e 74 20 64 69 72 66 64 2c 20 20 20 20 20 20 int dirfd,
e2e00 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 /* Direc
e2e10 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 tory file descri
e2e20 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ptor */. sqlite
e2e30 33 5f 66 69 6c 65 20 2a 70 49 64 2c 20 20 20 20 3_file *pId,
e2e40 20 20 2f 2a 20 57 72 69 74 65 20 74 6f 20 74 68 /* Write to th
e2e50 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 e unixFile struc
e2e60 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 ture here */. c
e2e70 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
e2e80 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f name, /* Name o
e2e90 66 20 74 68 65 20 66 69 6c 65 20 62 65 69 6e 67 f the file being
e2ea0 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 opened */. int
e2eb0 20 6e 6f 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 noLock,
e2ec0 20 20 20 20 20 2f 2a 20 4f 6d 69 74 20 6c 6f 63 /* Omit loc
e2ed0 6b 69 6e 67 20 69 66 20 74 72 75 65 20 2a 2f 0a king if true */.
e2ee0 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65 20 20 int isDelete
e2ef0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c /* Del
e2f00 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 ete on close if
e2f10 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e true */.){. con
e2f20 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
e2f30 74 68 6f 64 73 20 2a 70 4c 6f 63 6b 69 6e 67 53 thods *pLockingS
e2f40 74 79 6c 65 3b 0a 20 20 75 6e 69 78 46 69 6c 65 tyle;. unixFile
e2f50 20 2a 70 4e 65 77 20 3d 20 28 75 6e 69 78 46 69 *pNew = (unixFi
e2f60 6c 65 20 2a 29 70 49 64 3b 0a 20 20 69 6e 74 20 le *)pId;. int
e2f70 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
e2f80 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d . assert( pNew-
e2f90 3e 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20 29 3b 0a >pLock==NULL );.
e2fa0 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e assert( pNew->
e2fb0 70 4f 70 65 6e 3d 3d 4e 55 4c 4c 20 29 3b 0a 0a pOpen==NULL );..
e2fc0 20 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 20 69 /* Parameter i
e2fd0 73 44 65 6c 65 74 65 20 69 73 20 6f 6e 6c 79 20 sDelete is only
e2fe0 75 73 65 64 20 6f 6e 20 76 78 77 6f 72 6b 73 2e used on vxworks.
e2ff0 20 45 78 70 72 65 73 73 20 74 68 69 73 20 65 78 Express this ex
e3000 70 6c 69 63 69 74 6c 79 20 0a 20 20 2a 2a 20 68 plicitly . ** h
e3010 65 72 65 20 74 6f 20 70 72 65 76 65 6e 74 20 63 ere to prevent c
e3020 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 ompiler warnings
e3030 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 70 61 about unused pa
e3040 72 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a 20 rameters.. */.
e3050 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
e3060 52 28 69 73 44 65 6c 65 74 65 29 3b 0a 0a 20 20 R(isDelete);..
e3070 4f 53 54 52 41 43 45 33 28 22 4f 50 45 4e 20 20 OSTRACE3("OPEN
e3080 20 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 68 2c %-3d %s\n", h,
e3090 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 20 20 20 zFilename);
e30a0 0a 20 20 70 4e 65 77 2d 3e 68 20 3d 20 68 3b 0a . pNew->h = h;.
e30b0 20 20 70 4e 65 77 2d 3e 64 69 72 66 64 20 3d 20 pNew->dirfd =
e30c0 64 69 72 66 64 3b 0a 20 20 53 45 54 5f 54 48 52 dirfd;. SET_THR
e30d0 45 41 44 49 44 28 70 4e 65 77 29 3b 0a 20 20 70 EADID(pNew);. p
e30e0 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20 3d New->fileFlags =
e30f0 20 30 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 0;..#if OS_VXWO
e3100 52 4b 53 0a 20 20 70 4e 65 77 2d 3e 70 49 64 20 RKS. pNew->pId
e3110 3d 20 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c = vxworksFindFil
e3120 65 49 64 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a eId(zFilename);.
e3130 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 49 64 3d if( pNew->pId=
e3140 3d 30 20 29 7b 0a 20 20 20 20 6e 6f 4c 6f 63 6b =0 ){. noLock
e3150 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 53 = 1;. rc = S
e3160 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
e3170 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e .#endif.. if( n
e3180 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f oLock ){. pLo
e3190 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 26 6e 6f ckingStyle = &no
e31a0 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 lockIoMethods;.
e31b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4c 6f 63 }else{. pLoc
e31c0 6b 69 6e 67 53 74 79 6c 65 20 3d 20 28 2a 2a 28 kingStyle = (**(
e31d0 66 69 6e 64 65 72 5f 74 79 70 65 2a 29 70 56 66 finder_type*)pVf
e31e0 73 2d 3e 70 41 70 70 44 61 74 61 29 28 7a 46 69 s->pAppData)(zFi
e31f0 6c 65 6e 61 6d 65 2c 20 70 4e 65 77 29 3b 0a 23 lename, pNew);.#
e3200 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 if SQLITE_ENABLE
e3210 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 _LOCKING_STYLE.
e3220 20 20 20 2f 2a 20 43 61 63 68 65 20 7a 46 69 6c /* Cache zFil
e3230 65 6e 61 6d 65 20 69 6e 20 74 68 65 20 6c 6f 63 ename in the loc
e3240 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 28 41 46 king context (AF
e3250 50 20 61 6e 64 20 64 6f 74 6c 6f 63 6b 20 6f 76 P and dotlock ov
e3260 65 72 72 69 64 65 29 20 66 6f 72 0a 20 20 20 20 erride) for.
e3270 2a 2a 20 70 72 6f 78 79 4c 6f 63 6b 20 61 63 74 ** proxyLock act
e3280 69 76 61 74 69 6f 6e 20 69 73 20 70 6f 73 73 69 ivation is possi
e3290 62 6c 65 20 28 72 65 6d 6f 74 65 20 70 72 6f 78 ble (remote prox
e32a0 79 20 69 73 20 62 61 73 65 64 20 6f 6e 20 64 62 y is based on db
e32b0 20 6e 61 6d 65 29 0a 20 20 20 20 2a 2a 20 7a 46 name). ** zF
e32c0 69 6c 65 6e 61 6d 65 20 72 65 6d 61 69 6e 73 20 ilename remains
e32d0 76 61 6c 69 64 20 75 6e 74 69 6c 20 66 69 6c 65 valid until file
e32e0 20 69 73 20 63 6c 6f 73 65 64 2c 20 74 6f 20 73 is closed, to s
e32f0 75 70 70 6f 72 74 20 2a 2f 0a 20 20 20 20 70 4e upport */. pN
e3300 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 ew->lockingConte
e3310 78 74 20 3d 20 28 76 6f 69 64 2a 29 7a 46 69 6c xt = (void*)zFil
e3320 65 6e 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 ename;.#endif.
e3330 7d 0a 0a 20 20 69 66 28 20 70 4c 6f 63 6b 69 6e }.. if( pLockin
e3340 67 53 74 79 6c 65 20 3d 3d 20 26 70 6f 73 69 78 gStyle == &posix
e3350 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 IoMethods ){.
e3360 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
e3370 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 );. rc = find
e3380 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 LockInfo(pNew, &
e3390 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e pNew->pLock, &pN
e33a0 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 ew->pOpen);.
e33b0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
e33c0 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 K ){. /* If
e33d0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 65 an error occure
e33e0 64 20 69 6e 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 d in findLockInf
e33f0 6f 28 29 2c 20 63 6c 6f 73 65 20 74 68 65 20 66 o(), close the f
e3400 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 20 ile descriptor.
e3410 20 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 ** immediat
e3420 65 6c 79 2c 20 62 65 66 6f 72 65 20 72 65 6c 65 ely, before rele
e3430 61 73 69 6e 67 20 74 68 65 20 6d 75 74 65 78 2e asing the mutex.
e3440 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 20 findLockInfo()
e3450 6d 61 79 20 66 61 69 6c 0a 20 20 20 20 20 20 2a may fail. *
e3460 2a 20 69 6e 20 74 77 6f 20 73 63 65 6e 61 72 69 * in two scenari
e3470 6f 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 os:. **.
e3480 20 20 20 2a 2a 20 20 20 28 61 29 20 41 20 63 61 ** (a) A ca
e3490 6c 6c 20 74 6f 20 66 73 74 61 74 28 29 20 66 61 ll to fstat() fa
e34a0 69 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 20 iled.. **
e34b0 20 28 62 29 20 41 20 6d 61 6c 6c 6f 63 20 66 61 (b) A malloc fa
e34c0 69 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 iled.. **.
e34d0 20 20 20 20 20 2a 2a 20 53 63 65 6e 61 72 69 6f ** Scenario
e34e0 20 28 62 29 20 6d 61 79 20 6f 6e 6c 79 20 6f 63 (b) may only oc
e34f0 63 75 72 20 69 66 20 74 68 65 20 70 72 6f 63 65 cur if the proce
e3500 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 6e 6f ss is holding no
e3510 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 other. **
e3520 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 file descriptors
e3530 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d open on the sam
e3540 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 e file. If there
e3550 20 77 65 72 65 20 6f 74 68 65 72 20 66 69 6c 65 were other file
e3560 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 . ** descri
e3570 70 74 6f 72 73 20 6f 6e 20 74 68 69 73 20 66 69 ptors on this fi
e3580 6c 65 2c 20 74 68 65 6e 20 6e 6f 20 6d 61 6c 6c le, then no mall
e3590 6f 63 20 77 6f 75 6c 64 20 62 65 20 72 65 71 75 oc would be requ
e35a0 69 72 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a ired by. **
e35b0 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e findLockInfo().
e35c0 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 If this is the
e35d0 63 61 73 65 2c 20 69 74 20 69 73 20 71 75 69 74 case, it is quit
e35e0 65 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 65 0a e safe to close.
e35f0 20 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 20 ** handle
e3600 68 20 2d 20 61 73 20 69 74 20 69 73 20 67 75 61 h - as it is gua
e3610 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e 6f 20 ranteed that no
e3620 70 6f 73 69 78 20 6c 6f 63 6b 73 20 77 69 6c 6c posix locks will
e3630 20 62 65 20 72 65 6c 65 61 73 65 64 0a 20 20 20 be released.
e3640 20 20 20 2a 2a 20 62 79 20 64 6f 69 6e 67 20 73 ** by doing s
e3650 6f 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 o.. **.
e3660 20 20 2a 2a 20 49 66 20 73 63 65 6e 61 72 69 6f ** If scenario
e3670 20 28 61 29 20 63 61 75 73 65 64 20 74 68 65 20 (a) caused the
e3680 65 72 72 6f 72 20 74 68 65 6e 20 74 68 69 6e 67 error then thing
e3690 73 20 61 72 65 20 6e 6f 74 20 73 6f 20 73 61 66 s are not so saf
e36a0 65 2e 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 e. The. **
e36b0 69 6d 70 6c 69 63 69 74 20 61 73 73 75 6d 70 74 implicit assumpt
e36c0 69 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61 74 ion here is that
e36d0 20 69 66 20 66 73 74 61 74 28 29 20 66 61 69 6c if fstat() fail
e36e0 73 2c 20 74 68 69 6e 67 73 20 61 72 65 20 69 6e s, things are in
e36f0 0a 20 20 20 20 20 20 2a 2a 20 73 75 63 68 20 62 . ** such b
e3700 61 64 20 73 68 61 70 65 20 74 68 61 74 20 64 72 ad shape that dr
e3710 6f 70 70 69 6e 67 20 61 20 6c 6f 63 6b 20 6f 72 opping a lock or
e3720 20 74 77 6f 20 64 6f 65 73 6e 27 74 20 6d 61 74 two doesn't mat
e3730 74 65 72 20 6d 75 63 68 2e 0a 20 20 20 20 20 20 ter much..
e3740 2a 2f 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 68 */. close(h
e3750 29 3b 0a 20 20 20 20 20 20 68 20 3d 20 2d 31 3b );. h = -1;
e3760 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c . }. unixL
e3770 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d eaveMutex();. }
e3780 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 ..#if SQLITE_ENA
e3790 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
e37a0 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 E && defined(__A
e37b0 50 50 4c 45 5f 5f 29 0a 20 20 65 6c 73 65 20 69 PPLE__). else i
e37c0 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 f( pLockingStyle
e37d0 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 == &afpIoMethod
e37e0 73 20 29 7b 0a 20 20 20 20 2f 2a 20 41 46 50 20 s ){. /* AFP
e37f0 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 locking uses the
e3800 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 file path so it
e3810 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 needs to be inc
e3820 6c 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20 luded in. **
e3830 74 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f the afpLockingCo
e3840 6e 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 ntext.. */.
e3850 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 afpLockingCont
e3860 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 70 ext *pCtx;. p
e3870 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 New->lockingCont
e3880 65 78 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c ext = pCtx = sql
e3890 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a ite3_malloc( siz
e38a0 65 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 eof(*pCtx) );.
e38b0 20 20 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b if( pCtx==0 ){
e38c0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
e38d0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 TE_NOMEM;. }e
e38e0 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 42 lse{. /* NB
e38f0 3a 20 7a 46 69 6c 65 6e 61 6d 65 20 65 78 69 73 : zFilename exis
e3900 74 73 20 61 6e 64 20 72 65 6d 61 69 6e 73 20 76 ts and remains v
e3910 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 66 alid until the f
e3920 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 0a 20 20 ile is closed.
e3930 20 20 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 ** according
e3940 20 74 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20 to requirement
e3950 46 31 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64 F11141. So we d
e3960 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 o not need to ma
e3970 6b 65 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f ke a. ** co
e3980 70 79 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61 py of the filena
e3990 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 70 43 74 me. */. pCt
e39a0 78 2d 3e 64 62 50 61 74 68 20 3d 20 7a 46 69 6c x->dbPath = zFil
e39b0 65 6e 61 6d 65 3b 0a 20 20 20 20 20 20 73 72 61 ename;. sra
e39c0 6e 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20 20 20 ndomdev();.
e39d0 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
e39e0 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 );. rc = fi
e39f0 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c ndLockInfo(pNew,
e3a00 20 4e 55 4c 4c 2c 20 26 70 4e 65 77 2d 3e 70 4f NULL, &pNew->pO
e3a10 70 65 6e 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 pen);. unix
e3a20 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 20 20 20 LeaveMutex();
e3a30 20 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a . }. }.
e3a40 23 65 6e 64 69 66 0a 0a 20 20 65 6c 73 65 20 69 #endif.. else i
e3a50 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 f( pLockingStyle
e3a60 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 == &dotlockIoMe
e3a70 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 thods ){. /*
e3a80 44 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 Dotfile locking
e3a90 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 uses the file pa
e3aa0 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 th so it needs t
e3ab0 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e o be included in
e3ac0 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 6f 74 6c . ** the dotl
e3ad0 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 ockLockingContex
e3ae0 74 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68 t . */. ch
e3af0 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 ar *zLockFile;.
e3b00 20 20 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65 int nFilename
e3b10 3b 0a 20 20 20 20 6e 46 69 6c 65 6e 61 6d 65 20 ;. nFilename
e3b20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 46 = (int)strlen(zF
e3b30 69 6c 65 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20 ilename) + 6;.
e3b40 20 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 zLockFile = (c
e3b50 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 har *)sqlite3_ma
e3b60 6c 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 29 3b lloc(nFilename);
e3b70 0a 20 20 20 20 69 66 28 20 7a 4c 6f 63 6b 46 69 . if( zLockFi
e3b80 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 le==0 ){. r
e3b90 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
e3ba0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
e3bb0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
e3bc0 6e 74 66 28 6e 46 69 6c 65 6e 61 6d 65 2c 20 7a ntf(nFilename, z
e3bd0 4c 6f 63 6b 46 69 6c 65 2c 20 22 25 73 22 20 44 LockFile, "%s" D
e3be0 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 2c 20 7a OTLOCK_SUFFIX, z
e3bf0 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7d Filename);. }
e3c00 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 . pNew->locki
e3c10 6e 67 43 6f 6e 74 65 78 74 20 3d 20 7a 4c 6f 63 ngContext = zLoc
e3c20 6b 46 69 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 20 kFile;. }..#if
e3c30 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 65 6c 73 OS_VXWORKS. els
e3c40 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 e if( pLockingSt
e3c50 79 6c 65 20 3d 3d 20 26 73 65 6d 49 6f 4d 65 74 yle == &semIoMet
e3c60 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4e hods ){. /* N
e3c70 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c amed semaphore l
e3c80 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 ocking uses the
e3c90 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 file path so it
e3ca0 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 needs to be.
e3cb0 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 ** included in t
e3cc0 68 65 20 73 65 6d 4c 6f 63 6b 69 6e 67 43 6f 6e he semLockingCon
e3cd0 74 65 78 74 0a 20 20 20 20 2a 2f 0a 20 20 20 20 text. */.
e3ce0 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
e3cf0 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c ;. rc = findL
e3d00 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70 ockInfo(pNew, &p
e3d10 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 New->pLock, &pNe
e3d20 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69 w->pOpen);. i
e3d30 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f f( (rc==SQLITE_O
e3d40 4b 29 20 26 26 20 28 70 4e 65 77 2d 3e 70 4f 70 K) && (pNew->pOp
e3d50 65 6e 2d 3e 70 53 65 6d 3d 3d 4e 55 4c 4c 29 20 en->pSem==NULL)
e3d60 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a ){. char *z
e3d70 53 65 6d 4e 61 6d 65 20 3d 20 70 4e 65 77 2d 3e SemName = pNew->
e3d80 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 3b pOpen->aSemName;
e3d90 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 . int n;.
e3da0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
e3db0 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d intf(MAX_PATHNAM
e3dc0 45 2c 20 7a 53 65 6d 4e 61 6d 65 2c 20 22 2f 25 E, zSemName, "/%
e3dd0 73 2e 73 65 6d 22 2c 0a 20 20 20 20 20 20 20 20 s.sem",.
e3de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
e3df0 4e 65 77 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e New->pId->zCanon
e3e00 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 icalName);.
e3e10 20 66 6f 72 28 20 6e 3d 31 3b 20 7a 53 65 6d 4e for( n=1; zSemN
e3e20 61 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a 20 20 ame[n]; n++ ).
e3e30 20 20 20 20 20 20 69 66 28 20 7a 53 65 6d 4e 61 if( zSemNa
e3e40 6d 65 5b 6e 5d 3d 3d 27 2f 27 20 29 20 7a 53 65 me[n]=='/' ) zSe
e3e50 6d 4e 61 6d 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a mName[n] = '_';.
e3e60 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 pNew->pOpe
e3e70 6e 2d 3e 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70 n->pSem = sem_op
e3e80 65 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43 en(zSemName, O_C
e3e90 52 45 41 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a REAT, 0666, 1);.
e3ea0 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e if( pNew->
e3eb0 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 3d 20 53 pOpen->pSem == S
e3ec0 45 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20 20 20 EM_FAILED ){.
e3ed0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
e3ee0 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 _NOMEM;.
e3ef0 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 pNew->pOpen->aSe
e3f00 6d 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b mName[0] = '\0';
e3f10 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
e3f20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 unixLeaveMute
e3f30 78 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a x();. }.#endif.
e3f40 20 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73 74 45 . pNew->lastE
e3f50 72 72 6e 6f 20 3d 20 30 3b 0a 23 69 66 20 4f 53 rrno = 0;.#if OS
e3f60 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 _VXWORKS. if( r
e3f70 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
e3f80 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 46 69 6c 65 unlink(zFile
e3f90 6e 61 6d 65 29 3b 0a 20 20 20 20 69 73 44 65 6c name);. isDel
e3fa0 65 74 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 ete = 0;. }. p
e3fb0 4e 65 77 2d 3e 69 73 44 65 6c 65 74 65 20 3d 20 New->isDelete =
e3fc0 69 73 44 65 6c 65 74 65 3b 0a 23 65 6e 64 69 66 isDelete;.#endif
e3fd0 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
e3fe0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 E_OK ){. if(
e3ff0 64 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 dirfd>=0 ) close
e4000 28 64 69 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 (dirfd); /* sile
e4010 6e 74 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c nt leak if fail,
e4020 20 61 6c 72 65 61 64 79 20 69 6e 20 65 72 72 6f already in erro
e4030 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 68 3e 3d r */. if( h>=
e4040 30 20 29 20 63 6c 6f 73 65 28 68 29 3b 0a 20 20 0 ) close(h);.
e4050 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d }else{. pNew-
e4060 3e 70 4d 65 74 68 6f 64 20 3d 20 70 4c 6f 63 6b >pMethod = pLock
e4070 69 6e 67 53 74 79 6c 65 3b 0a 20 20 20 20 4f 70 ingStyle;. Op
e4080 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 enCounter(+1);.
e4090 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
e40a0 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 }../*.** Open a
e40b0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
e40c0 74 6f 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 to the directory
e40d0 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 6c 65 containing file
e40e0 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20 49 zFilename..** I
e40f0 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 f successful, *p
e4100 46 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 Fd is set to the
e4110 20 6f 70 65 6e 65 64 20 66 69 6c 65 20 64 65 73 opened file des
e4120 63 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a 20 53 criptor and.** S
e4130 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
e4140 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f rned. If an erro
e4150 72 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72 r occurs, either
e4160 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a SQLITE_NOMEM.**
e4170 20 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f or SQLITE_CANTO
e4180 50 45 4e 20 69 73 20 72 65 74 75 72 6e 65 64 20 PEN is returned
e4190 61 6e 64 20 2a 70 46 64 20 69 73 20 73 65 74 20 and *pFd is set
e41a0 74 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a to an undefined.
e41b0 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 ** value..**.**
e41c0 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 If SQLITE_OK is
e41d0 72 65 74 75 72 6e 65 64 2c 20 74 68 65 20 63 61 returned, the ca
e41e0 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 ller is responsi
e41f0 62 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e 67 0a ble for closing.
e4200 2a 2a 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 ** the file desc
e4210 72 69 70 74 6f 72 20 2a 70 46 64 20 75 73 69 6e riptor *pFd usin
e4220 67 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 73 74 g close()..*/.st
e4230 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 69 72 atic int openDir
e4240 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 68 61 ectory(const cha
e4250 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 69 6e r *zFilename, in
e4260 74 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74 20 69 t *pFd){. int i
e4270 69 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d 31 i;. int fd = -1
e4280 3b 0a 20 20 63 68 61 72 20 7a 44 69 72 6e 61 6d ;. char zDirnam
e4290 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 e[MAX_PATHNAME+1
e42a0 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e ];.. sqlite3_sn
e42b0 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e printf(MAX_PATHN
e42c0 41 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c 20 22 AME, zDirname, "
e42d0 25 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b %s", zFilename);
e42e0 0a 20 20 66 6f 72 28 69 69 3d 28 69 6e 74 29 73 . for(ii=(int)s
e42f0 74 72 6c 65 6e 28 7a 44 69 72 6e 61 6d 65 29 3b trlen(zDirname);
e4300 20 69 69 3e 31 20 26 26 20 7a 44 69 72 6e 61 6d ii>1 && zDirnam
e4310 65 5b 69 69 5d 21 3d 27 2f 27 3b 20 69 69 2d 2d e[ii]!='/'; ii--
e4320 29 3b 0a 20 20 69 66 28 20 69 69 3e 30 20 29 7b );. if( ii>0 ){
e4330 0a 20 20 20 20 7a 44 69 72 6e 61 6d 65 5b 69 69 . zDirname[ii
e4340 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 66 64 ] = '\0';. fd
e4350 20 3d 20 6f 70 65 6e 28 7a 44 69 72 6e 61 6d 65 = open(zDirname
e4360 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e , O_RDONLY|O_BIN
e4370 41 52 59 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 ARY, 0);. if(
e4380 20 66 64 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 fd>=0 ){.#ifdef
e4390 20 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 20 20 FD_CLOEXEC.
e43a0 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 fcntl(fd, F_SE
e43b0 54 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46 TFD, fcntl(fd, F
e43c0 5f 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f _GETFD, 0) | FD_
e43d0 43 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 CLOEXEC);.#endif
e43e0 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 . OSTRACE3(
e43f0 22 4f 50 45 4e 44 49 52 20 25 2d 33 64 20 25 73 "OPENDIR %-3d %s
e4400 5c 6e 22 2c 20 66 64 2c 20 7a 44 69 72 6e 61 6d \n", fd, zDirnam
e4410 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 e);. }. }.
e4420 2a 70 46 64 20 3d 20 66 64 3b 0a 20 20 72 65 74 *pFd = fd;. ret
e4430 75 72 6e 20 28 66 64 3e 3d 30 3f 53 51 4c 49 54 urn (fd>=0?SQLIT
e4440 45 5f 4f 4b 3a 53 51 4c 49 54 45 5f 43 41 4e 54 E_OK:SQLITE_CANT
e4450 4f 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 OPEN);.}../*.**
e4460 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 Create a tempora
e4470 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 ry file name in
e4480 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 zBuf. zBuf must
e4490 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a be allocated.**
e44a0 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 by the calling
e44b0 70 72 6f 63 65 73 73 20 61 6e 64 20 6d 75 73 74 process and must
e44c0 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 be big enough t
e44d0 6f 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 0a o hold at least.
e44e0 2a 2a 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e ** pVfs->mxPathn
e44f0 61 6d 65 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 ame bytes..*/.st
e4500 61 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 atic int getTemp
e4510 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 name(int nBuf, c
e4520 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 har *zBuf){. st
e4530 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 atic const char
e4540 2a 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 20 *azDirs[] = {.
e4550 20 20 20 30 2c 0a 20 20 20 20 20 30 2c 0a 20 20 0,. 0,.
e4560 20 20 20 22 2f 76 61 72 2f 74 6d 70 22 2c 0a 20 "/var/tmp",.
e4570 20 20 20 20 22 2f 75 73 72 2f 74 6d 70 22 2c 0a "/usr/tmp",.
e4580 20 20 20 20 20 22 2f 74 6d 70 22 2c 0a 20 20 20 "/tmp",.
e4590 20 20 22 2e 22 2c 0a 20 20 7d 3b 0a 20 20 73 74 ".",. };. st
e45a0 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 atic const unsig
e45b0 6e 65 64 20 63 68 61 72 20 7a 43 68 61 72 73 5b ned char zChars[
e45c0 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67 ] =. "abcdefg
e45d0 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 hijklmnopqrstuvw
e45e0 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46 xyz". "ABCDEF
e45f0 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 GHIJKLMNOPQRSTUV
e4600 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34 WXYZ". "01234
e4610 35 36 37 38 39 22 3b 0a 20 20 75 6e 73 69 67 6e 56789";. unsign
e4620 65 64 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 ed int i, j;. s
e4630 74 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a truct stat buf;.
e4640 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
e4650 69 72 20 3d 20 22 2e 22 3b 0a 0a 20 20 2f 2a 20 ir = ".";.. /*
e4660 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 It's odd to simu
e4670 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 late an io-error
e4680 20 68 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c here, but reall
e4690 79 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20 y this is just.
e46a0 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f ** using the io
e46b0 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75 -error infrastru
e46c0 63 74 75 72 65 20 74 6f 20 74 65 73 74 20 74 68 cture to test th
e46d0 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 at SQLite handle
e46e0 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 s this. ** func
e46f0 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 0a 20 tion failing. .
e4700 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f */. SimulateIO
e4710 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
e4720 4c 49 54 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 LITE_IOERR );..
e4730 20 61 7a 44 69 72 73 5b 30 5d 20 3d 20 73 71 6c azDirs[0] = sql
e4740 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 ite3_temp_direct
e4750 6f 72 79 3b 0a 20 20 69 66 20 28 4e 55 4c 4c 20 ory;. if (NULL
e4760 3d 3d 20 61 7a 44 69 72 73 5b 31 5d 29 20 7b 0a == azDirs[1]) {.
e4770 20 20 20 20 61 7a 44 69 72 73 5b 31 5d 20 3d 20 azDirs[1] =
e4780 67 65 74 65 6e 76 28 22 54 4d 50 44 49 52 22 29 getenv("TMPDIR")
e4790 3b 0a 20 20 7d 0a 20 20 0a 20 20 66 6f 72 28 69 ;. }. . for(i
e47a0 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 7a 44 =0; i<sizeof(azD
e47b0 69 72 73 29 2f 73 69 7a 65 6f 66 28 61 7a 44 69 irs)/sizeof(azDi
e47c0 72 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 rs[0]); i++){.
e47d0 20 20 69 66 28 20 61 7a 44 69 72 73 5b 69 5d 3d if( azDirs[i]=
e47e0 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 =0 ) continue;.
e47f0 20 20 20 69 66 28 20 73 74 61 74 28 61 7a 44 69 if( stat(azDi
e4800 72 73 5b 69 5d 2c 20 26 62 75 66 29 20 29 20 63 rs[i], &buf) ) c
e4810 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 ontinue;. if(
e4820 20 21 53 5f 49 53 44 49 52 28 62 75 66 2e 73 74 !S_ISDIR(buf.st
e4830 5f 6d 6f 64 65 29 20 29 20 63 6f 6e 74 69 6e 75 _mode) ) continu
e4840 65 3b 0a 20 20 20 20 69 66 28 20 61 63 63 65 73 e;. if( acces
e4850 73 28 61 7a 44 69 72 73 5b 69 5d 2c 20 30 37 29 s(azDirs[i], 07)
e4860 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
e4870 20 7a 44 69 72 20 3d 20 61 7a 44 69 72 73 5b 69 zDir = azDirs[i
e4880 5d 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 ];. break;.
e4890 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 }.. /* Check th
e48a0 61 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 at the output bu
e48b0 66 66 65 72 20 69 73 20 6c 61 72 67 65 20 65 6e ffer is large en
e48c0 6f 75 67 68 20 66 6f 72 20 74 68 65 20 74 65 6d ough for the tem
e48d0 70 6f 72 61 72 79 20 66 69 6c 65 20 0a 20 20 2a porary file . *
e48e0 2a 20 6e 61 6d 65 2e 20 49 66 20 69 74 20 69 73 * name. If it is
e48f0 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 53 51 4c not, return SQL
e4900 49 54 45 5f 45 52 52 4f 52 2e 0a 20 20 2a 2f 0a ITE_ERROR.. */.
e4910 20 20 69 66 28 20 28 73 74 72 6c 65 6e 28 7a 44 if( (strlen(zD
e4920 69 72 29 20 2b 20 73 74 72 6c 65 6e 28 53 51 4c ir) + strlen(SQL
e4930 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 ITE_TEMP_FILE_PR
e4940 45 46 49 58 29 20 2b 20 31 37 29 20 3e 3d 20 28 EFIX) + 17) >= (
e4950 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 7b 0a 20 size_t)nBuf ){.
e4960 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
e4970 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64 _ERROR;. }.. d
e4980 6f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 o{. sqlite3_s
e4990 6e 70 72 69 6e 74 66 28 6e 42 75 66 2d 31 37 2c nprintf(nBuf-17,
e49a0 20 7a 42 75 66 2c 20 22 25 73 2f 22 53 51 4c 49 zBuf, "%s/"SQLI
e49b0 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 TE_TEMP_FILE_PRE
e49c0 46 49 58 2c 20 7a 44 69 72 29 3b 0a 20 20 20 20 FIX, zDir);.
e49d0 6a 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 j = (int)strlen(
e49e0 7a 42 75 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 zBuf);. sqlit
e49f0 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 31 35 e3_randomness(15
e4a00 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 20 , &zBuf[j]);.
e4a10 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 35 3b 20 for(i=0; i<15;
e4a20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 i++, j++){.
e4a30 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 zBuf[j] = (char
e4a40 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67 )zChars[ ((unsig
e4a50 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d ned char)zBuf[j]
e4a60 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 )%(sizeof(zChars
e4a70 29 2d 31 29 20 5d 3b 0a 20 20 20 20 7d 0a 20 20 )-1) ];. }.
e4a80 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 zBuf[j] = 0;.
e4a90 20 7d 77 68 69 6c 65 28 20 61 63 63 65 73 73 28 }while( access(
e4aa0 7a 42 75 66 2c 30 29 3d 3d 30 20 29 3b 0a 20 20 zBuf,0)==0 );.
e4ab0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
e4ac0 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f ;.}..#if SQLITE_
e4ad0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
e4ae0 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 TYLE && defined(
e4af0 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a __APPLE__)./*.**
e4b00 20 52 6f 75 74 69 6e 65 20 74 6f 20 74 72 61 6e Routine to tran
e4b10 73 66 6f 72 6d 20 61 20 75 6e 69 78 46 69 6c 65 sform a unixFile
e4b20 20 69 6e 74 6f 20 61 20 70 72 6f 78 79 2d 6c 6f into a proxy-lo
e4b30 63 6b 69 6e 67 20 75 6e 69 78 46 69 6c 65 2e 0a cking unixFile..
e4b40 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
e4b50 6e 20 69 6e 20 74 68 65 20 70 72 6f 78 79 2d 6c n in the proxy-l
e4b60 6f 63 6b 20 64 69 76 69 73 69 6f 6e 2c 20 62 75 ock division, bu
e4b70 74 20 75 73 65 64 20 62 79 20 75 6e 69 78 4f 70 t used by unixOp
e4b80 65 6e 28 29 0a 2a 2a 20 69 66 20 53 51 4c 49 54 en().** if SQLIT
e4b90 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c E_PREFER_PROXY_L
e4ba0 4f 43 4b 49 4e 47 20 69 73 20 64 65 66 69 6e 65 OCKING is define
e4bb0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
e4bc0 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 proxyTransformU
e4bd0 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 nixFile(unixFile
e4be0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b *, const char*);
e4bf0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 .#endif../*.** S
e4c00 65 61 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 earch for an unu
e4c10 73 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 sed file descrip
e4c20 74 6f 72 20 74 68 61 74 20 77 61 73 20 6f 70 65 tor that was ope
e4c30 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ned on the datab
e4c40 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20 28 6e 6f ase .** file (no
e4c50 74 20 61 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 6d t a journal or m
e4c60 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 aster-journal fi
e4c70 6c 65 29 20 69 64 65 6e 74 69 66 69 65 64 20 62 le) identified b
e4c80 79 20 70 61 74 68 6e 61 6d 65 0a 2a 2a 20 7a 50 y pathname.** zP
e4c90 61 74 68 20 77 69 74 68 20 53 51 4c 49 54 45 5f ath with SQLITE_
e4ca0 4f 50 45 4e 5f 58 58 58 20 66 6c 61 67 73 20 6d OPEN_XXX flags m
e4cb0 61 74 63 68 69 6e 67 20 74 68 6f 73 65 20 70 61 atching those pa
e4cc0 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f ssed as the seco
e4cd0 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 nd.** argument t
e4ce0 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e o this function.
e4cf0 0a 2a 2a 0a 2a 2a 20 53 75 63 68 20 61 20 66 69 .**.** Such a fi
e4d00 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6d 61 le descriptor ma
e4d10 79 20 65 78 69 73 74 20 69 66 20 61 20 64 61 74 y exist if a dat
e4d20 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
e4d30 20 77 61 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 62 was closed.** b
e4d40 75 74 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 ut the associate
e4d50 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f d file descripto
e4d60 72 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 r could not be c
e4d70 6c 6f 73 65 64 20 62 65 63 61 75 73 65 20 73 6f losed because so
e4d80 6d 65 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65 me.** other file
e4d90 20 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e descriptor open
e4da0 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c on the same fil
e4db0 65 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 66 e is holding a f
e4dc0 69 6c 65 2d 6c 6f 63 6b 2e 0a 2a 2a 20 52 65 66 ile-lock..** Ref
e4dd0 65 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 20 69 er to comments i
e4de0 6e 20 74 68 65 20 75 6e 69 78 43 6c 6f 73 65 28 n the unixClose(
e4df0 29 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 74 ) function and t
e4e00 68 65 20 6c 65 6e 67 74 68 79 20 63 6f 6d 6d 65 he lengthy comme
e4e10 6e 74 0a 2a 2a 20 64 65 73 63 72 69 62 69 6e 67 nt.** describing
e4e20 20 22 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 "Posix Advisory
e4e30 20 4c 6f 63 6b 69 6e 67 22 20 61 74 20 74 68 65 Locking" at the
e4e40 20 73 74 61 72 74 20 6f 66 20 74 68 69 73 20 66 start of this f
e4e50 69 6c 65 20 66 6f 72 20 0a 2a 2a 20 66 75 72 74 ile for .** furt
e4e60 68 65 72 20 64 65 74 61 69 6c 73 2e 20 41 6c 73 her details. Als
e4e70 6f 2c 20 74 69 63 6b 65 74 20 23 34 30 31 38 2e o, ticket #4018.
e4e80 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 75 69 74 .**.** If a suit
e4e90 61 62 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 able file descri
e4ea0 70 74 6f 72 20 69 73 20 66 6f 75 6e 64 2c 20 74 ptor is found, t
e4eb0 68 65 6e 20 69 74 20 69 73 20 72 65 74 75 72 6e hen it is return
e4ec0 65 64 2e 20 49 66 20 6e 6f 0a 2a 2a 20 73 75 63 ed. If no.** suc
e4ed0 68 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f h file descripto
e4ee0 72 20 69 73 20 6c 6f 63 61 74 65 64 2c 20 2d 31 r is located, -1
e4ef0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f is returned..*/
e4f00 0a 73 74 61 74 69 63 20 55 6e 69 78 55 6e 75 73 .static UnixUnus
e4f10 65 64 46 64 20 2a 66 69 6e 64 52 65 75 73 61 62 edFd *findReusab
e4f20 6c 65 46 64 28 63 6f 6e 73 74 20 63 68 61 72 20 leFd(const char
e4f30 2a 7a 50 61 74 68 2c 20 69 6e 74 20 66 6c 61 67 *zPath, int flag
e4f40 73 29 7b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 s){. UnixUnused
e4f50 46 64 20 2a 70 55 6e 75 73 65 64 20 3d 20 30 3b Fd *pUnused = 0;
e4f60 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 65 .. /* Do not se
e4f70 61 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73 arch for an unus
e4f80 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ed file descript
e4f90 6f 72 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20 4e or on vxworks. N
e4fa0 6f 74 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 ot because. **
e4fb0 76 78 77 6f 72 6b 73 20 77 6f 75 6c 64 20 6e 6f vxworks would no
e4fc0 74 20 62 65 6e 65 66 69 74 20 66 72 6f 6d 20 74 t benefit from t
e4fd0 68 65 20 63 68 61 6e 67 65 20 28 69 74 20 6d 69 he change (it mi
e4fe0 67 68 74 2c 20 77 65 27 72 65 20 6e 6f 74 20 73 ght, we're not s
e4ff0 75 72 65 29 2c 0a 20 20 2a 2a 20 62 75 74 20 62 ure),. ** but b
e5000 65 63 61 75 73 65 20 6e 6f 20 77 61 79 20 74 6f ecause no way to
e5010 20 74 65 73 74 20 69 74 20 69 73 20 63 75 72 72 test it is curr
e5020 65 6e 74 6c 79 20 61 76 61 69 6c 61 62 6c 65 2e ently available.
e5030 20 49 74 20 69 73 20 62 65 74 74 65 72 20 0a 20 It is better .
e5040 20 2a 2a 20 6e 6f 74 20 74 6f 20 72 69 73 6b 20 ** not to risk
e5050 62 72 65 61 6b 69 6e 67 20 76 78 77 6f 72 6b 73 breaking vxworks
e5060 20 73 75 70 70 6f 72 74 20 66 6f 72 20 74 68 65 support for the
e5070 20 73 61 6b 65 20 6f 66 20 73 75 63 68 20 61 6e sake of such an
e5080 20 6f 62 73 63 75 72 65 20 0a 20 20 2a 2a 20 66 obscure . ** f
e5090 65 61 74 75 72 65 2e 20 20 2a 2f 0a 23 69 66 20 eature. */.#if
e50a0 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 !OS_VXWORKS. st
e50b0 72 75 63 74 20 73 74 61 74 20 73 53 74 61 74 3b ruct stat sStat;
e50c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e50d0 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 /* Results of
e50e0 20 73 74 61 74 28 29 20 63 61 6c 6c 20 2a 2f 0a stat() call */.
e50f0 0a 20 20 2f 2a 20 41 20 73 74 61 74 28 29 20 63 . /* A stat() c
e5100 61 6c 6c 20 6d 61 79 20 66 61 69 6c 20 66 6f 72 all may fail for
e5110 20 76 61 72 69 6f 75 73 20 72 65 61 73 6f 6e 73 various reasons
e5120 2e 20 49 66 20 74 68 69 73 20 68 61 70 70 65 6e . If this happen
e5130 73 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 61 6c s, it is. ** al
e5140 6d 6f 73 74 20 63 65 72 74 61 69 6e 20 74 68 61 most certain tha
e5150 74 20 61 6e 20 6f 70 65 6e 28 29 20 63 61 6c 6c t an open() call
e5160 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74 on the same pat
e5170 68 20 77 69 6c 6c 20 61 6c 73 6f 20 66 61 69 6c h will also fail
e5180 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69 73 20 .. ** For this
e5190 72 65 61 73 6f 6e 2c 20 69 66 20 61 6e 20 65 72 reason, if an er
e51a0 72 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 74 68 ror occurs in th
e51b0 65 20 73 74 61 74 28 29 20 63 61 6c 6c 20 68 65 e stat() call he
e51c0 72 65 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 69 re, it is. ** i
e51d0 67 6e 6f 72 65 64 20 61 6e 64 20 2d 31 20 69 73 gnored and -1 is
e51e0 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20 63 returned. The c
e51f0 61 6c 6c 65 72 20 77 69 6c 6c 20 74 72 79 20 74 aller will try t
e5200 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20 66 69 6c o open a new fil
e5210 65 0a 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f e. ** descripto
e5220 72 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 r on the same pa
e5230 74 68 2c 20 66 61 69 6c 2c 20 61 6e 64 20 72 65 th, fail, and re
e5240 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 74 6f turn an error to
e5250 20 53 51 4c 69 74 65 2e 0a 20 20 2a 2a 0a 20 20 SQLite.. **.
e5260 2a 2a 20 45 76 65 6e 20 69 66 20 61 20 73 75 62 ** Even if a sub
e5270 73 65 71 75 65 6e 74 20 6f 70 65 6e 28 29 20 63 sequent open() c
e5280 61 6c 6c 20 64 6f 65 73 20 73 75 63 63 65 65 64 all does succeed
e5290 2c 20 74 68 65 20 63 6f 6e 73 65 71 75 65 6e 63 , the consequenc
e52a0 65 73 20 6f 66 0a 20 20 2a 2a 20 6e 6f 74 20 73 es of. ** not s
e52b0 65 61 72 63 68 69 6e 67 20 66 6f 72 20 61 20 72 earching for a r
e52c0 65 73 75 73 61 62 6c 65 20 66 69 6c 65 20 64 65 esusable file de
e52d0 73 63 72 69 70 74 6f 72 20 61 72 65 20 6e 6f 74 scriptor are not
e52e0 20 64 69 72 65 2e 20 20 2a 2f 0a 20 20 69 66 28 dire. */. if(
e52f0 20 30 3d 3d 73 74 61 74 28 7a 50 61 74 68 2c 20 0==stat(zPath,
e5300 26 73 53 74 61 74 29 20 29 7b 0a 20 20 20 20 73 &sStat) ){. s
e5310 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e truct unixOpenCn
e5320 74 20 2a 70 4f 3b 0a 20 20 20 20 73 74 72 75 63 t *pO;. struc
e5330 74 20 75 6e 69 78 46 69 6c 65 49 64 20 69 64 3b t unixFileId id;
e5340 0a 20 20 20 20 69 64 2e 64 65 76 20 3d 20 73 53 . id.dev = sS
e5350 74 61 74 2e 73 74 5f 64 65 76 3b 0a 20 20 20 20 tat.st_dev;.
e5360 69 64 2e 69 6e 6f 20 3d 20 73 53 74 61 74 2e 73 id.ino = sStat.s
e5370 74 5f 69 6e 6f 3b 0a 0a 20 20 20 20 75 6e 69 78 t_ino;.. unix
e5380 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
e5390 20 20 66 6f 72 28 70 4f 3d 6f 70 65 6e 4c 69 73 for(pO=openLis
e53a0 74 3b 20 70 4f 20 26 26 20 6d 65 6d 63 6d 70 28 t; pO && memcmp(
e53b0 26 69 64 2c 20 26 70 4f 2d 3e 66 69 6c 65 49 64 &id, &pO->fileId
e53c0 2c 20 73 69 7a 65 6f 66 28 69 64 29 29 3b 20 70 , sizeof(id)); p
e53d0 4f 3d 70 4f 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 O=pO->pNext);.
e53e0 20 20 69 66 28 20 70 4f 20 29 7b 0a 20 20 20 20 if( pO ){.
e53f0 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a UnixUnusedFd *
e5400 2a 70 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 *pp;. for(p
e5410 70 3d 26 70 4f 2d 3e 70 55 6e 75 73 65 64 3b 20 p=&pO->pUnused;
e5420 2a 70 70 20 26 26 20 28 2a 70 70 29 2d 3e 66 6c *pp && (*pp)->fl
e5430 61 67 73 21 3d 66 6c 61 67 73 3b 20 70 70 3d 26 ags!=flags; pp=&
e5440 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 3b ((*pp)->pNext));
e5450 0a 20 20 20 20 20 20 70 55 6e 75 73 65 64 20 3d . pUnused =
e5460 20 2a 70 70 3b 0a 20 20 20 20 20 20 69 66 28 20 *pp;. if(
e5470 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 20 pUnused ){.
e5480 20 20 20 2a 70 70 20 3d 20 70 55 6e 75 73 65 64 *pp = pUnused
e5490 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d ->pNext;. }
e54a0 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c . }. unixL
e54b0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d eaveMutex();. }
e54c0 0a 23 65 6e 64 69 66 20 20 20 20 2f 2a 20 69 66 .#endif /* if
e54d0 20 21 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a !OS_VXWORKS */.
e54e0 20 20 72 65 74 75 72 6e 20 70 55 6e 75 73 65 64 return pUnused
e54f0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 ;.}../*.** Open
e5500 74 68 65 20 66 69 6c 65 20 7a 50 61 74 68 2e 0a the file zPath..
e5510 2a 2a 20 0a 2a 2a 20 50 72 65 76 69 6f 75 73 6c ** .** Previousl
e5520 79 2c 20 74 68 65 20 53 51 4c 69 74 65 20 4f 53 y, the SQLite OS
e5530 20 6c 61 79 65 72 20 75 73 65 64 20 74 68 72 65 layer used thre
e5540 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 70 e functions in p
e5550 6c 61 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 lace of this.**
e5560 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 one:.**.** s
e5570 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 qlite3OsOpenRead
e5580 57 72 69 74 65 28 29 3b 0a 2a 2a 20 20 20 20 20 Write();.**
e5590 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 sqlite3OsOpenRea
e55a0 64 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 20 20 20 20 dOnly();.**
e55b0 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78 63 sqlite3OsOpenExc
e55c0 6c 75 73 69 76 65 28 29 3b 0a 2a 2a 0a 2a 2a 20 lusive();.**.**
e55d0 54 68 65 73 65 20 63 61 6c 6c 73 20 63 6f 72 72 These calls corr
e55e0 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 66 6f espond to the fo
e55f0 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 llowing combinat
e5600 69 6f 6e 73 20 6f 66 20 66 6c 61 67 73 3a 0a 2a ions of flags:.*
e5610 2a 0a 2a 2a 20 20 20 20 20 52 65 61 64 57 72 69 *.** ReadWri
e5620 74 65 28 29 20 2d 3e 20 20 20 20 20 28 52 45 41 te() -> (REA
e5630 44 57 52 49 54 45 20 7c 20 43 52 45 41 54 45 29 DWRITE | CREATE)
e5640 0a 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e 6c 79 .** ReadOnly
e5650 28 29 20 20 2d 3e 20 20 20 20 20 28 52 45 41 44 () -> (READ
e5660 4f 4e 4c 59 29 20 0a 2a 2a 20 20 20 20 20 4f 70 ONLY) .** Op
e5670 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 2d 3e enExclusive() ->
e5680 20 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 52 (READWRITE | CR
e5690 45 41 54 45 20 7c 20 45 58 43 4c 55 53 49 56 45 EATE | EXCLUSIVE
e56a0 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c 64 20 ).**.** The old
e56b0 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 OpenExclusive()
e56c0 61 63 63 65 70 74 65 64 20 61 20 62 6f 6f 6c 65 accepted a boole
e56d0 61 6e 20 61 72 67 75 6d 65 6e 74 20 2d 20 22 64 an argument - "d
e56e0 65 6c 46 6c 61 67 22 2e 20 49 66 0a 2a 2a 20 74 elFlag". If.** t
e56f0 72 75 65 2c 20 74 68 65 20 66 69 6c 65 20 77 61 rue, the file wa
e5700 73 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 s configured to
e5710 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 be automatically
e5720 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 deleted when th
e5730 65 0a 2a 2a 20 66 69 6c 65 20 68 61 6e 64 6c 65 e.** file handle
e5740 20 63 6c 6f 73 65 64 2e 20 54 6f 20 61 63 68 69 closed. To achi
e5750 65 76 65 20 74 68 65 20 73 61 6d 65 20 65 66 66 eve the same eff
e5760 65 63 74 20 75 73 69 6e 67 20 74 68 69 73 20 6e ect using this n
e5770 65 77 20 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 ew .** interface
e5780 2c 20 61 64 64 20 74 68 65 20 44 45 4c 45 54 45 , add the DELETE
e5790 4f 4e 43 4c 4f 53 45 20 66 6c 61 67 20 74 6f 20 ONCLOSE flag to
e57a0 74 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 20 those specified
e57b0 61 62 6f 76 65 20 66 6f 72 20 0a 2a 2a 20 4f 70 above for .** Op
e57c0 65 6e 45 78 63 6c 75 73 69 76 65 28 29 2e 0a 2a enExclusive()..*
e57d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
e57e0 78 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 xOpen(. sqlite3
e57f0 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
e5800 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 /* The VFS
e5810 20 66 6f 72 20 77 68 69 63 68 20 74 68 69 73 20 for which this
e5820 69 73 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 is the xOpen met
e5830 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 hod */. const c
e5840 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 har *zPath,
e5850 20 20 20 20 20 20 2f 2a 20 50 61 74 68 6e 61 6d /* Pathnam
e5860 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20 e of file to be
e5870 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 opened */. sqli
e5880 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c te3_file *pFile,
e5890 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
e58a0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
e58b0 74 6f 20 62 65 20 66 69 6c 6c 65 64 20 69 6e 20 to be filled in
e58c0 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 */. int flags,
e58d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e58e0 20 20 2f 2a 20 49 6e 70 75 74 20 66 6c 61 67 73 /* Input flags
e58f0 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 to control the
e5900 6f 70 65 6e 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 opening */. int
e5910 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20 *pOutFlags
e5920 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 /* Out
e5930 70 75 74 20 66 6c 61 67 73 20 72 65 74 75 72 6e put flags return
e5940 65 64 20 74 6f 20 53 51 4c 69 74 65 20 63 6f 72 ed to SQLite cor
e5950 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69 78 46 69 e */.){. unixFi
e5960 6c 65 20 2a 70 20 3d 20 28 75 6e 69 78 46 69 6c le *p = (unixFil
e5970 65 20 2a 29 70 46 69 6c 65 3b 0a 20 20 69 6e 74 e *)pFile;. int
e5980 20 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 fd = -1;
e5990 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
e59a0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 72 ile descriptor r
e59b0 65 74 75 72 6e 65 64 20 62 79 20 6f 70 65 6e 28 eturned by open(
e59c0 29 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 ) */. int dirfd
e59d0 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20 = -1;
e59e0 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f /* Directo
e59f0 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ry file descript
e5a00 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e or */. int open
e5a10 46 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20 Flags = 0;
e5a20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
e5a30 74 6f 20 70 61 73 73 20 74 6f 20 6f 70 65 6e 28 to pass to open(
e5a40 29 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 65 ) */. int eType
e5a50 20 3d 20 66 6c 61 67 73 26 30 78 46 46 46 46 46 = flags&0xFFFFF
e5a60 46 30 30 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66 F00; /* Type of
e5a70 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f file to open */
e5a80 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 3b 20 20 . int noLock;
e5a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e5aa0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 /* True to omi
e5ab0 74 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 t locking primit
e5ac0 69 76 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 ives */. int rc
e5ad0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
e5ae0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 /* Func
e5af0 74 69 6f 6e 20 52 65 74 75 72 6e 20 43 6f 64 65 tion Return Code
e5b00 20 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 45 78 63 */.. int isExc
e5b10 6c 75 73 69 76 65 20 20 3d 20 28 66 6c 61 67 73 lusive = (flags
e5b20 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 & SQLITE_OPEN_E
e5b30 58 43 4c 55 53 49 56 45 29 3b 0a 20 20 69 6e 74 XCLUSIVE);. int
e5b40 20 69 73 44 65 6c 65 74 65 20 20 20 20 20 3d 20 isDelete =
e5b50 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f (flags & SQLITE_
e5b60 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f OPEN_DELETEONCLO
e5b70 53 45 29 3b 0a 20 20 69 6e 74 20 69 73 43 72 65 SE);. int isCre
e5b80 61 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 ate = (flags
e5b90 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 & SQLITE_OPEN_C
e5ba0 52 45 41 54 45 29 3b 0a 20 20 69 6e 74 20 69 73 REATE);. int is
e5bb0 52 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28 66 6c Readonly = (fl
e5bc0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
e5bd0 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 69 N_READONLY);. i
e5be0 6e 74 20 69 73 52 65 61 64 57 72 69 74 65 20 20 nt isReadWrite
e5bf0 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 = (flags & SQLIT
e5c00 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
e5c10 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 63 72 65 61 );.. /* If crea
e5c20 74 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6f 72 ting a master or
e5c30 20 6d 61 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 6e main-file journ
e5c40 61 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f al, this functio
e5c50 6e 20 77 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a 2a n will open. **
e5c60 20 61 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 a file-descript
e5c70 6f 72 20 6f 6e 20 74 68 65 20 64 69 72 65 63 74 or on the direct
e5c80 6f 72 79 20 74 6f 6f 2e 20 54 68 65 20 66 69 72 ory too. The fir
e5c90 73 74 20 74 69 6d 65 20 75 6e 69 78 53 79 6e 63 st time unixSync
e5ca0 28 29 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 (). ** is calle
e5cb0 64 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 d the directory
e5cc0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
e5cd0 77 69 6c 6c 20 62 65 20 66 73 79 6e 63 28 29 65 will be fsync()e
e5ce0 64 20 61 6e 64 20 63 6c 6f 73 65 28 29 64 2e 0a d and close()d..
e5cf0 20 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 70 65 */. int isOpe
e5d00 6e 44 69 72 65 63 74 6f 72 79 20 3d 20 28 69 73 nDirectory = (is
e5d10 43 72 65 61 74 65 20 26 26 20 0a 20 20 20 20 20 Create && .
e5d20 20 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f (eType==SQLITE_
e5d30 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 OPEN_MASTER_JOUR
e5d40 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 NAL || eType==SQ
e5d50 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a LITE_OPEN_MAIN_J
e5d60 4f 55 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a 20 20 OURNAL). );..
e5d70 2f 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a /* If argument z
e5d80 50 61 74 68 20 69 73 20 61 20 4e 55 4c 4c 20 70 Path is a NULL p
e5d90 6f 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e ointer, this fun
e5da0 63 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 ction is require
e5db0 64 20 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 61 d to open. ** a
e5dc0 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e temporary file.
e5dd0 20 55 73 65 20 74 68 69 73 20 62 75 66 66 65 72 Use this buffer
e5de0 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 to store the fi
e5df0 6c 65 20 6e 61 6d 65 20 69 6e 2e 0a 20 20 2a 2f le name in.. */
e5e00 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 . char zTmpname
e5e10 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d [MAX_PATHNAME+1]
e5e20 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ;. const char *
e5e30 7a 4e 61 6d 65 20 3d 20 7a 50 61 74 68 3b 0a 0a zName = zPath;..
e5e40 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 66 /* Check the f
e5e50 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 ollowing stateme
e5e60 6e 74 73 20 61 72 65 20 74 72 75 65 3a 20 0a 20 nts are true: .
e5e70 20 2a 2a 0a 20 20 2a 2a 20 20 20 28 61 29 20 45 **. ** (a) E
e5e80 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 xactly one of th
e5e90 65 20 52 45 41 44 57 52 49 54 45 20 61 6e 64 20 e READWRITE and
e5ea0 52 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20 6d READONLY flags m
e5eb0 75 73 74 20 62 65 20 73 65 74 2c 20 61 6e 64 20 ust be set, and
e5ec0 0a 20 20 2a 2a 20 20 20 28 62 29 20 69 66 20 43 . ** (b) if C
e5ed0 52 45 41 54 45 20 69 73 20 73 65 74 2c 20 74 68 REATE is set, th
e5ee0 65 6e 20 52 45 41 44 57 52 49 54 45 20 6d 75 73 en READWRITE mus
e5ef0 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2c 20 61 t also be set, a
e5f00 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 69 66 nd. ** (c) if
e5f10 20 45 58 43 4c 55 53 49 56 45 20 69 73 20 73 65 EXCLUSIVE is se
e5f20 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d t, then CREATE m
e5f30 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e ust also be set.
e5f40 0a 20 20 2a 2a 20 20 20 28 64 29 20 69 66 20 44 . ** (d) if D
e5f50 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73 20 ELETEONCLOSE is
e5f60 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 set, then CREATE
e5f70 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 must also be se
e5f80 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 t.. */. assert
e5f90 28 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20 ((isReadonly==0
e5fa0 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 3d 3d || isReadWrite==
e5fb0 30 29 20 26 26 20 28 69 73 52 65 61 64 57 72 69 0) && (isReadWri
e5fc0 74 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79 te || isReadonly
e5fd0 29 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 43 ));. assert(isC
e5fe0 72 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52 65 reate==0 || isRe
e5ff0 61 64 57 72 69 74 65 29 3b 0a 20 20 61 73 73 65 adWrite);. asse
e6000 72 74 28 69 73 45 78 63 6c 75 73 69 76 65 3d 3d rt(isExclusive==
e6010 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0a 0 || isCreate);.
e6020 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74 assert(isDelet
e6030 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65 e==0 || isCreate
e6040 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61 69 );.. /* The mai
e6050 6e 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 72 6e n DB, main journ
e6060 61 6c 2c 20 61 6e 64 20 6d 61 73 74 65 72 20 6a al, and master j
e6070 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 72 ournal are never
e6080 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 20 automatically.
e6090 20 2a 2a 20 64 65 6c 65 74 65 64 2e 20 4e 6f 72 ** deleted. Nor
e60a0 20 61 72 65 20 74 68 65 79 20 65 76 65 72 20 74 are they ever t
e60b0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20 emporary files.
e60c0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 21 */. assert( (!
e60d0 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d isDelete && zNam
e60e0 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c e) || eType!=SQL
e60f0 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 ITE_OPEN_MAIN_DB
e6100 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 21 );. assert( (!
e6110 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d isDelete && zNam
e6120 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c e) || eType!=SQL
e6130 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f ITE_OPEN_MAIN_JO
e6140 55 52 4e 41 4c 20 29 3b 0a 20 20 61 73 73 65 72 URNAL );. asser
e6150 74 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 t( (!isDelete &&
e6160 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 zName) || eType
e6170 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 !=SQLITE_OPEN_MA
e6180 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0a STER_JOURNAL );.
e6190 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 . /* Assert tha
e61a0 74 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 t the upper laye
e61b0 72 20 68 61 73 20 73 65 74 20 6f 6e 65 20 6f 66 r has set one of
e61c0 20 74 68 65 20 22 66 69 6c 65 2d 74 79 70 65 22 the "file-type"
e61d0 20 66 6c 61 67 73 2e 20 2a 2f 0a 20 20 61 73 73 flags. */. ass
e61e0 65 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 ert( eType==SQLI
e61f0 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 TE_OPEN_MAIN_DB
e6200 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 || eType==S
e6210 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f QLITE_OPEN_TEMP_
e6220 44 42 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 DB . || eT
e6230 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
e6240 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c _MAIN_JOURNAL ||
e6250 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f eType==SQLITE_O
e6260 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c PEN_TEMP_JOURNAL
e6270 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 . || eTyp
e6280 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 e==SQLITE_OPEN_S
e6290 55 42 4a 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 65 UBJOURNAL || e
e62a0 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 Type==SQLITE_OPE
e62b0 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c N_MASTER_JOURNAL
e62c0 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 . || eTyp
e62d0 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 e==SQLITE_OPEN_T
e62e0 52 41 4e 53 49 45 4e 54 5f 44 42 0a 20 20 29 3b RANSIENT_DB. );
e62f0 0a 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c .. memset(p, 0,
e6300 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 sizeof(unixFile
e6310 29 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65 ));.. if( eType
e6320 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 ==SQLITE_OPEN_MA
e6330 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 55 6e 69 IN_DB ){. Uni
e6340 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73 xUnusedFd *pUnus
e6350 65 64 3b 0a 20 20 20 20 70 55 6e 75 73 65 64 20 ed;. pUnused
e6360 3d 20 66 69 6e 64 52 65 75 73 61 62 6c 65 46 64 = findReusableFd
e6370 28 7a 4e 61 6d 65 2c 20 66 6c 61 67 73 29 3b 0a (zName, flags);.
e6380 20 20 20 20 69 66 28 20 70 55 6e 75 73 65 64 20 if( pUnused
e6390 29 7b 0a 20 20 20 20 20 20 66 64 20 3d 20 70 55 ){. fd = pU
e63a0 6e 75 73 65 64 2d 3e 66 64 3b 0a 20 20 20 20 7d nused->fd;. }
e63b0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 55 6e 75 else{. pUnu
e63c0 73 65 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 sed = sqlite3_ma
e63d0 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 55 6e lloc(sizeof(*pUn
e63e0 75 73 65 64 29 29 3b 0a 20 20 20 20 20 20 69 66 used));. if
e63f0 28 20 21 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 ( !pUnused ){.
e6400 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
e6410 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ITE_NOMEM;.
e6420 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e }. }. p->
e6430 70 55 6e 75 73 65 64 20 3d 20 70 55 6e 75 73 65 pUnused = pUnuse
e6440 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 d;. }else if( !
e6450 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20 zName ){. /*
e6460 49 66 20 7a 4e 61 6d 65 20 69 73 20 4e 55 4c 4c If zName is NULL
e6470 2c 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 , the upper laye
e6480 72 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 20 r is requesting
e6490 61 20 74 65 6d 70 20 66 69 6c 65 2e 20 2a 2f 0a a temp file. */.
e64a0 20 20 20 20 61 73 73 65 72 74 28 69 73 44 65 6c assert(isDel
e64b0 65 74 65 20 26 26 20 21 69 73 4f 70 65 6e 44 69 ete && !isOpenDi
e64c0 72 65 63 74 6f 72 79 29 3b 0a 20 20 20 20 72 63 rectory);. rc
e64d0 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d = getTempname(M
e64e0 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 2c 20 7a AX_PATHNAME+1, z
e64f0 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 Tmpname);. if
e6500 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
e6510 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
e6520 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 4e rc;. }. zN
e6530 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a ame = zTmpname;.
e6540 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d }.. /* Determ
e6550 69 6e 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 ine the value of
e6560 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d the flags param
e6570 65 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 50 eter passed to P
e6580 4f 53 49 58 20 66 75 6e 63 74 69 6f 6e 0a 20 20 OSIX function.
e6590 2a 2a 20 6f 70 65 6e 28 29 2e 20 54 68 65 73 65 ** open(). These
e65a0 20 6d 75 73 74 20 62 65 20 63 61 6c 63 75 6c 61 must be calcula
e65b0 74 65 64 20 65 76 65 6e 20 69 66 20 6f 70 65 6e ted even if open
e65c0 28 29 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 () is not called
e65d0 2c 20 61 73 0a 20 20 2a 2a 20 74 68 65 79 20 6d , as. ** they m
e65e0 61 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20 ay be stored as
e65f0 70 61 72 74 20 6f 66 20 74 68 65 20 66 69 6c 65 part of the file
e6600 20 68 61 6e 64 6c 65 20 61 6e 64 20 75 73 65 64 handle and used
e6610 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 27 63 by the . ** 'c
e6620 6f 6e 63 68 20 66 69 6c 65 27 20 6c 6f 63 6b 69 onch file' locki
e6630 6e 67 20 66 75 6e 63 74 69 6f 6e 73 20 6c 61 74 ng functions lat
e6640 65 72 20 6f 6e 2e 20 20 2a 2f 0a 20 20 69 66 28 er on. */. if(
e6650 20 69 73 52 65 61 64 6f 6e 6c 79 20 29 20 20 6f isReadonly ) o
e6660 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 penFlags |= O_RD
e6670 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 69 73 52 65 ONLY;. if( isRe
e6680 61 64 57 72 69 74 65 20 29 20 6f 70 65 6e 46 6c adWrite ) openFl
e6690 61 67 73 20 7c 3d 20 4f 5f 52 44 57 52 3b 0a 20 ags |= O_RDWR;.
e66a0 20 69 66 28 20 69 73 43 72 65 61 74 65 20 29 20 if( isCreate )
e66b0 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 openFlags |=
e66c0 4f 5f 43 52 45 41 54 3b 0a 20 20 69 66 28 20 69 O_CREAT;. if( i
e66d0 73 45 78 63 6c 75 73 69 76 65 20 29 20 6f 70 65 sExclusive ) ope
e66e0 6e 46 6c 61 67 73 20 7c 3d 20 28 4f 5f 45 58 43 nFlags |= (O_EXC
e66f0 4c 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b 0a 20 L|O_NOFOLLOW);.
e6700 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f openFlags |= (O
e6710 5f 4c 41 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e _LARGEFILE|O_BIN
e6720 41 52 59 29 3b 0a 0a 20 20 69 66 28 20 66 64 3c ARY);.. if( fd<
e6730 30 20 29 7b 0a 20 20 20 20 6d 6f 64 65 5f 74 20 0 ){. mode_t
e6740 6f 70 65 6e 4d 6f 64 65 20 3d 20 28 69 73 44 65 openMode = (isDe
e6750 6c 65 74 65 3f 30 36 30 30 3a 53 51 4c 49 54 45 lete?0600:SQLITE
e6760 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 _DEFAULT_FILE_PE
e6770 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20 20 RMISSIONS);.
e6780 66 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c fd = open(zName,
e6790 20 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 65 6e openFlags, open
e67a0 4d 6f 64 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 Mode);. OSTRA
e67b0 43 45 34 28 22 4f 50 45 4e 58 20 20 20 25 2d 33 CE4("OPENX %-3
e67c0 64 20 25 73 20 30 25 6f 5c 6e 22 2c 20 66 64 2c d %s 0%o\n", fd,
e67d0 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 zName, openFlag
e67e0 73 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30 s);. if( fd<0
e67f0 20 26 26 20 65 72 72 6e 6f 21 3d 45 49 53 44 49 && errno!=EISDI
e6800 52 20 26 26 20 69 73 52 65 61 64 57 72 69 74 65 R && isReadWrite
e6810 20 26 26 20 21 69 73 45 78 63 6c 75 73 69 76 65 && !isExclusive
e6820 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 61 69 ){. /* Fai
e6830 6c 65 64 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 led to open the
e6840 66 69 6c 65 20 66 6f 72 20 72 65 61 64 2f 77 72 file for read/wr
e6850 69 74 65 20 61 63 63 65 73 73 2e 20 54 72 79 20 ite access. Try
e6860 72 65 61 64 2d 6f 6e 6c 79 2e 20 2a 2f 0a 20 20 read-only. */.
e6870 20 20 20 20 66 6c 61 67 73 20 26 3d 20 7e 28 53 flags &= ~(S
e6880 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
e6890 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e RITE|SQLITE_OPEN
e68a0 5f 43 52 45 41 54 45 29 3b 0a 20 20 20 20 20 20 _CREATE);.
e68b0 6f 70 65 6e 46 6c 61 67 73 20 26 3d 20 7e 28 4f openFlags &= ~(O
e68c0 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 29 3b 0a _RDWR|O_CREAT);.
e68d0 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 53 flags |= S
e68e0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
e68f0 4e 4c 59 3b 0a 20 20 20 20 20 20 6f 70 65 6e 46 NLY;. openF
e6900 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 lags |= O_RDONLY
e6910 3b 0a 20 20 20 20 20 20 66 64 20 3d 20 6f 70 65 ;. fd = ope
e6920 6e 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 n(zName, openFla
e6930 67 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b 0a 20 gs, openMode);.
e6940 20 20 20 7d 0a 20 20 20 20 69 66 28 20 66 64 3c }. if( fd<
e6950 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
e6960 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b SQLITE_CANTOPEN;
e6970 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e . goto open
e6980 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d _finished;. }
e6990 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 66 . }. assert( f
e69a0 64 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f d>=0 );. if( pO
e69b0 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a utFlags ){. *
e69c0 70 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 pOutFlags = flag
e69d0 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d s;. }.. if( p-
e69e0 3e 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 >pUnused ){.
e69f0 70 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 64 20 3d p->pUnused->fd =
e6a00 20 66 64 3b 0a 20 20 20 20 70 2d 3e 70 55 6e 75 fd;. p->pUnu
e6a10 73 65 64 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 sed->flags = fla
e6a20 67 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 gs;. }.. if( i
e6a30 73 44 65 6c 65 74 65 20 29 7b 0a 23 69 66 20 4f sDelete ){.#if O
e6a40 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 7a 50 S_VXWORKS. zP
e6a50 61 74 68 20 3d 20 7a 4e 61 6d 65 3b 0a 23 65 6c ath = zName;.#el
e6a60 73 65 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 4e se. unlink(zN
e6a70 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d ame);.#endif. }
e6a80 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 .#if SQLITE_ENAB
e6a90 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
e6aa0 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e . else{. p->
e6ab0 6f 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70 65 6e openFlags = open
e6ac0 46 6c 61 67 73 3b 0a 20 20 7d 0a 23 65 6e 64 69 Flags;. }.#endi
e6ad0 66 0a 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 44 f.. if( isOpenD
e6ae0 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 irectory ){.
e6af0 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 6f rc = openDirecto
e6b00 72 79 28 7a 50 61 74 68 2c 20 26 64 69 72 66 64 ry(zPath, &dirfd
e6b10 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
e6b20 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
e6b30 20 20 2f 2a 20 49 74 20 69 73 20 73 61 66 65 20 /* It is safe
e6b40 74 6f 20 63 6c 6f 73 65 20 66 64 20 61 74 20 74 to close fd at t
e6b50 68 69 73 20 70 6f 69 6e 74 2c 20 62 65 63 61 75 his point, becau
e6b60 73 65 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 se it is guarant
e6b70 65 65 64 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a eed not. **
e6b80 20 74 6f 20 62 65 20 6f 70 65 6e 20 6f 6e 20 61 to be open on a
e6b90 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
e6ba0 49 66 20 69 74 20 77 65 72 65 20 6f 70 65 6e 20 If it were open
e6bb0 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 on a database fi
e6bc0 6c 65 2c 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 le,. ** it
e6bd0 77 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 61 66 would not be saf
e6be0 65 20 74 6f 20 63 6c 6f 73 65 20 61 73 20 74 68 e to close as th
e6bf0 69 73 20 77 6f 75 6c 64 20 72 65 6c 65 61 73 65 is would release
e6c00 20 61 6e 79 20 6c 6f 63 6b 73 20 68 65 6c 64 0a any locks held.
e6c10 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 ** on the
e6c20 66 69 6c 65 20 62 79 20 74 68 69 73 20 70 72 6f file by this pro
e6c30 63 65 73 73 2e 20 20 2a 2f 0a 20 20 20 20 20 20 cess. */.
e6c40 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 53 assert( eType!=S
e6c50 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
e6c60 44 42 20 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 DB );. clos
e6c70 65 28 66 64 29 3b 20 20 20 20 20 20 20 20 20 20 e(fd);
e6c80 20 20 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c /* silently l
e6c90 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 61 6c 72 eak if fail, alr
e6ca0 65 61 64 79 20 69 6e 20 65 72 72 6f 72 20 2a 2f eady in error */
e6cb0 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e . goto open
e6cc0 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d _finished;. }
e6cd0 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 46 44 5f . }..#ifdef FD_
e6ce0 43 4c 4f 45 58 45 43 0a 20 20 66 63 6e 74 6c 28 CLOEXEC. fcntl(
e6cf0 66 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 6e fd, F_SETFD, fcn
e6d00 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c 20 tl(fd, F_GETFD,
e6d10 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29 0) | FD_CLOEXEC)
e6d20 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 6e 6f 4c 6f ;.#endif.. noLo
e6d30 63 6b 20 3d 20 65 54 79 70 65 21 3d 53 51 4c 49 ck = eType!=SQLI
e6d40 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b TE_OPEN_MAIN_DB;
e6d50 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 50 52 45 ..#if SQLITE_PRE
e6d60 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e FER_PROXY_LOCKIN
e6d70 47 0a 20 20 69 66 28 20 7a 50 61 74 68 21 3d 4e G. if( zPath!=N
e6d80 55 4c 4c 20 26 26 20 21 6e 6f 4c 6f 63 6b 20 26 ULL && !noLock &
e6d90 26 20 70 56 66 73 2d 3e 78 4f 70 65 6e 20 29 7b & pVfs->xOpen ){
e6da0 0a 20 20 20 20 63 68 61 72 20 2a 65 6e 76 66 6f . char *envfo
e6db0 72 63 65 20 3d 20 67 65 74 65 6e 76 28 22 53 51 rce = getenv("SQ
e6dc0 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 LITE_FORCE_PROXY
e6dd0 5f 4c 4f 43 4b 49 4e 47 22 29 3b 0a 20 20 20 20 _LOCKING");.
e6de0 69 6e 74 20 75 73 65 50 72 6f 78 79 20 3d 20 30 int useProxy = 0
e6df0 3b 0a 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 ;.. /* SQLITE
e6e00 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 _FORCE_PROXY_LOC
e6e10 4b 49 4e 47 3d 3d 31 20 6d 65 61 6e 73 20 66 6f KING==1 means fo
e6e20 72 63 65 20 61 6c 77 61 79 73 20 75 73 65 20 70 rce always use p
e6e30 72 6f 78 79 2c 20 30 20 6d 65 61 6e 73 20 0a 20 roxy, 0 means .
e6e40 20 20 20 2a 2a 20 6e 65 76 65 72 20 75 73 65 20 ** never use
e6e50 70 72 6f 78 79 2c 20 4e 55 4c 4c 20 6d 65 61 6e proxy, NULL mean
e6e60 73 20 75 73 65 20 70 72 6f 78 79 20 66 6f 72 20 s use proxy for
e6e70 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 73 20 non-local files
e6e80 6f 6e 6c 79 2e 20 20 2a 2f 0a 20 20 20 20 69 66 only. */. if
e6e90 28 20 65 6e 76 66 6f 72 63 65 21 3d 4e 55 4c 4c ( envforce!=NULL
e6ea0 20 29 7b 0a 20 20 20 20 20 20 75 73 65 50 72 6f ){. usePro
e6eb0 78 79 20 3d 20 61 74 6f 69 28 65 6e 76 66 6f 72 xy = atoi(envfor
e6ec0 63 65 29 3e 30 3b 0a 20 20 20 20 7d 65 6c 73 65 ce)>0;. }else
e6ed0 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 73 {. struct s
e6ee0 74 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20 tatfs fsInfo;.
e6ef0 20 20 20 20 69 66 28 20 73 74 61 74 66 73 28 7a if( statfs(z
e6f00 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 3d Path, &fsInfo) =
e6f10 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 = -1 ){.
e6f20 2f 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 74 68 /* In theory, th
e6f30 65 20 63 6c 6f 73 65 28 66 64 29 20 63 61 6c 6c e close(fd) call
e6f40 20 69 73 20 73 75 62 2d 6f 70 74 69 6d 61 6c 2e is sub-optimal.
e6f50 20 49 66 20 74 68 65 20 66 69 6c 65 20 6f 70 65 If the file ope
e6f60 6e 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 ned. ** w
e6f70 69 74 68 20 66 64 20 69 73 20 61 20 64 61 74 61 ith fd is a data
e6f80 62 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 20 74 base file, and t
e6f90 68 65 72 65 20 61 72 65 20 6f 74 68 65 72 20 63 here are other c
e6fa0 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 70 65 6e 0a onnections open.
e6fb0 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 ** on th
e6fc0 61 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 at file that are
e6fd0 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69 currently holdi
e6fe0 6e 67 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b ng advisory lock
e6ff0 73 20 6f 6e 20 69 74 2c 0a 20 20 20 20 20 20 20 s on it,.
e7000 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 61 6c ** then the cal
e7010 6c 20 74 6f 20 63 6c 6f 73 65 28 29 20 77 69 6c l to close() wil
e7020 6c 20 63 61 6e 63 65 6c 20 74 68 6f 73 65 20 6c l cancel those l
e7030 6f 63 6b 73 2e 20 49 6e 20 70 72 61 63 74 69 63 ocks. In practic
e7040 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65 e,. ** we
e7050 27 72 65 20 61 73 73 75 6d 69 6e 67 20 74 68 61 're assuming tha
e7060 74 20 73 74 61 74 66 73 28 29 20 64 6f 65 73 6e t statfs() doesn
e7070 27 74 20 66 61 69 6c 20 76 65 72 79 20 6f 66 74 't fail very oft
e7080 65 6e 2e 20 41 74 20 6c 65 61 73 74 0a 20 20 20 en. At least.
e7090 20 20 20 20 20 2a 2a 20 6e 6f 74 20 77 68 69 6c ** not whil
e70a0 65 20 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73 e other file des
e70b0 63 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 criptors opened
e70c0 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 by the same proc
e70d0 65 73 73 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a ess on. *
e70e0 2a 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 * the same file
e70f0 61 72 65 20 77 6f 72 6b 69 6e 67 2e 20 20 2a 2f are working. */
e7100 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c 61 73 74 . p->last
e7110 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
e7120 20 20 20 20 20 20 20 69 66 28 20 64 69 72 66 64 if( dirfd
e7130 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 >=0 ){.
e7140 20 63 6c 6f 73 65 28 64 69 72 66 64 29 3b 20 2f close(dirfd); /
e7150 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 * silently leak
e7160 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 72 72 6f if fail, in erro
e7170 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 r */. }.
e7180 20 20 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29 close(fd)
e7190 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 ; /* silently le
e71a0 61 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 ak if fail, in e
e71b0 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 rror */.
e71c0 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 rc = SQLITE_IOER
e71d0 52 5f 41 43 43 45 53 53 3b 0a 20 20 20 20 20 20 R_ACCESS;.
e71e0 20 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 goto open_fini
e71f0 73 68 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 shed;. }.
e7200 20 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20 21 useProxy = !
e7210 28 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 26 (fsInfo.f_flags&
e7220 4d 4e 54 5f 4c 4f 43 41 4c 29 3b 0a 20 20 20 20 MNT_LOCAL);.
e7230 7d 0a 20 20 20 20 69 66 28 20 75 73 65 50 72 6f }. if( usePro
e7240 78 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d xy ){. rc =
e7250 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 fillInUnixFile(
e7260 70 56 66 73 2c 20 66 64 2c 20 64 69 72 66 64 2c pVfs, fd, dirfd,
e7270 20 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 6e pFile, zPath, n
e7280 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65 29 oLock, isDelete)
e7290 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
e72a0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
e72b0 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 54 rc = proxyT
e72c0 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 ransformUnixFile
e72d0 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69 6c ((unixFile*)pFil
e72e0 65 2c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 20 20 e, ":auto:");.
e72f0 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f }. goto
e7300 20 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a open_finished;.
e7310 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
e7320 0a 20 20 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 . . rc = fillI
e7330 6e 55 6e 69 78 46 69 6c 65 28 70 56 66 73 2c 20 nUnixFile(pVfs,
e7340 66 64 2c 20 64 69 72 66 64 2c 20 70 46 69 6c 65 fd, dirfd, pFile
e7350 2c 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c , zPath, noLock,
e7360 20 69 73 44 65 6c 65 74 65 29 3b 0a 6f 70 65 6e isDelete);.open
e7370 5f 66 69 6e 69 73 68 65 64 3a 0a 20 20 69 66 28 _finished:. if(
e7380 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
e7390 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 {. sqlite3_fr
e73a0 65 65 28 70 2d 3e 70 55 6e 75 73 65 64 29 3b 0a ee(p->pUnused);.
e73b0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
e73c0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 .}.../*.** Delet
e73d0 65 20 74 68 65 20 66 69 6c 65 20 61 74 20 7a 50 e the file at zP
e73e0 61 74 68 2e 20 49 66 20 74 68 65 20 64 69 72 53 ath. If the dirS
e73f0 79 6e 63 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ync argument is
e7400 74 72 75 65 2c 20 66 73 79 6e 63 28 29 0a 2a 2a true, fsync().**
e7410 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 61 the directory a
e7420 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20 74 68 fter deleting th
e7430 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 e file..*/.stati
e7440 63 20 69 6e 74 20 75 6e 69 78 44 65 6c 65 74 65 c int unixDelete
e7450 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
e7460 2a 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 2f 2a *NotUsed, /*
e7470 20 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20 VFS containing
e7480 74 68 69 73 20 61 73 20 74 68 65 20 78 44 65 6c this as the xDel
e7490 65 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ete method */.
e74a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 const char *zPat
e74b0 68 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d h, /* Nam
e74c0 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20 e of file to be
e74d0 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 deleted */. int
e74e0 20 64 69 72 53 79 6e 63 20 20 20 20 20 20 20 20 dirSync
e74f0 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 /* If tru
e7500 65 2c 20 66 73 79 6e 63 28 29 20 64 69 72 65 63 e, fsync() direc
e7510 74 6f 72 79 20 61 66 74 65 72 20 64 65 6c 65 74 tory after delet
e7520 69 6e 67 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 ing file */.){.
e7530 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
e7540 5f 4f 4b 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 _OK;. UNUSED_PA
e7550 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
e7560 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
e7570 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 ror(return SQLIT
e7580 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b E_IOERR_DELETE);
e7590 0a 20 20 75 6e 6c 69 6e 6b 28 7a 50 61 74 68 29 . unlink(zPath)
e75a0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
e75b0 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 _DISABLE_DIRSYNC
e75c0 0a 20 20 69 66 28 20 64 69 72 53 79 6e 63 20 29 . if( dirSync )
e75d0 7b 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a 20 20 {. int fd;.
e75e0 20 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 rc = openDirec
e75f0 74 6f 72 79 28 7a 50 61 74 68 2c 20 26 66 64 29 tory(zPath, &fd)
e7600 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
e7610 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 20 4f LITE_OK ){.#if O
e7620 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 20 20 S_VXWORKS.
e7630 69 66 28 20 66 73 79 6e 63 28 66 64 29 3d 3d 2d if( fsync(fd)==-
e7640 31 20 29 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 1 ).#else.
e7650 69 66 28 20 66 73 79 6e 63 28 66 64 29 20 29 0a if( fsync(fd) ).
e7660 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 #endif. {.
e7670 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
e7680 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 TE_IOERR_DIR_FSY
e7690 4e 43 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 NC;. }.
e76a0 20 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 26 if( close(fd)&
e76b0 26 21 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 &!rc ){.
e76c0 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 rc = SQLITE_IOER
e76d0 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 R_DIR_CLOSE;.
e76e0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 }. }. }.#
e76f0 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 endif. return r
e7700 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 c;.}../*.** Test
e7710 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f the existance o
e7720 66 20 6f 72 20 61 63 63 65 73 73 20 70 65 72 6d f or access perm
e7730 69 73 73 69 6f 6e 73 20 6f 66 20 66 69 6c 65 20 issions of file
e7740 7a 50 61 74 68 2e 20 54 68 65 0a 2a 2a 20 74 65 zPath. The.** te
e7750 73 74 20 70 65 72 66 6f 72 6d 65 64 20 64 65 70 st performed dep
e7760 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 ends on the valu
e7770 65 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a e of flags:.**.*
e7780 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 * SQLITE_ACC
e7790 45 53 53 5f 45 58 49 53 54 53 3a 20 52 65 74 75 ESS_EXISTS: Retu
e77a0 72 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 rn 1 if the file
e77b0 20 65 78 69 73 74 73 0a 2a 2a 20 20 20 20 20 53 exists.** S
e77c0 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 QLITE_ACCESS_REA
e77d0 44 57 52 49 54 45 3a 20 52 65 74 75 72 6e 20 31 DWRITE: Return 1
e77e0 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 if the file is
e77f0 72 65 61 64 20 61 6e 64 20 77 72 69 74 61 62 6c read and writabl
e7800 65 2e 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 e..** SQLITE
e7810 5f 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 _ACCESS_READONLY
e7820 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 : Return 1 if th
e7830 65 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62 e file is readab
e7840 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 le..**.** Otherw
e7850 69 73 65 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f ise return 0..*/
e7860 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
e7870 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 Access(. sqlite
e7880 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 3_vfs *NotUsed,
e7890 20 20 2f 2a 20 54 68 65 20 56 46 53 20 63 6f 6e /* The VFS con
e78a0 74 61 69 6e 69 6e 67 20 74 68 69 73 20 78 41 63 taining this xAc
e78b0 63 65 73 73 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 cess method */.
e78c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 const char *zPa
e78d0 74 68 2c 20 20 20 20 20 20 2f 2a 20 50 61 74 68 th, /* Path
e78e0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 of the file to
e78f0 65 78 61 6d 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 examine */. int
e7900 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
e7910 20 20 20 20 20 2f 2a 20 57 68 61 74 20 64 6f 20 /* What do
e7920 77 65 20 77 61 6e 74 20 74 6f 20 6c 65 61 72 6e we want to learn
e7930 20 61 62 6f 75 74 20 74 68 65 20 7a 50 61 74 68 about the zPath
e7940 20 66 69 6c 65 3f 20 2a 2f 0a 20 20 69 6e 74 20 file? */. int
e7950 2a 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20 *pResOut
e7960 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73 /* Write res
e7970 75 6c 74 20 62 6f 6f 6c 65 61 6e 20 68 65 72 65 ult boolean here
e7980 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 6d 6f */.){. int amo
e7990 64 65 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 de = 0;. UNUSED
e79a0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
e79b0 65 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 ed);. SimulateI
e79c0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 OError( return S
e79d0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 QLITE_IOERR_ACCE
e79e0 53 53 3b 20 29 3b 0a 20 20 73 77 69 74 63 68 28 SS; );. switch(
e79f0 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 flags ){. ca
e7a00 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 se SQLITE_ACCESS
e7a10 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 61 _EXISTS:. a
e7a20 6d 6f 64 65 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20 mode = F_OK;.
e7a30 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 break;. ca
e7a40 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 se SQLITE_ACCESS
e7a50 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20 _READWRITE:.
e7a60 20 20 61 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52 amode = W_OK|R
e7a70 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b _OK;. break
e7a80 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 ;. case SQLIT
e7a90 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 E_ACCESS_READ:.
e7aa0 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f amode = R_O
e7ab0 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a K;. break;.
e7ac0 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 . default:.
e7ad0 20 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 assert(!"Inv
e7ae0 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d alid flags argum
e7af0 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 ent");. }. *pR
e7b00 65 73 4f 75 74 20 3d 20 28 61 63 63 65 73 73 28 esOut = (access(
e7b10 7a 50 61 74 68 2c 20 61 6d 6f 64 65 29 3d 3d 30 zPath, amode)==0
e7b20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
e7b30 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a TE_OK;.}.../*.**
e7b40 20 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 Turn a relative
e7b50 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 pathname into a
e7b60 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 full pathname.
e7b70 54 68 65 20 72 65 6c 61 74 69 76 65 20 70 61 74 The relative pat
e7b80 68 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 61 h.** is stored a
e7b90 73 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 s a nul-terminat
e7ba0 65 64 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 ed string in the
e7bb0 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 buffer pointed
e7bc0 74 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68 2e 20 to by.** zPath.
e7bd0 0a 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e .**.** zOut poin
e7be0 74 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f ts to a buffer o
e7bf0 66 20 61 74 20 6c 65 61 73 74 20 73 71 6c 69 74 f at least sqlit
e7c00 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d e3_vfs.mxPathnam
e7c10 65 20 62 79 74 65 73 20 0a 2a 2a 20 28 69 6e 20 e bytes .** (in
e7c20 74 68 69 73 20 63 61 73 65 2c 20 4d 41 58 5f 50 this case, MAX_P
e7c30 41 54 48 4e 41 4d 45 20 62 79 74 65 73 29 2e 20 ATHNAME bytes).
e7c40 54 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 69 73 The full-path is
e7c50 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 74 written to.** t
e7c60 68 69 73 20 62 75 66 66 65 72 20 62 65 66 6f 72 his buffer befor
e7c70 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a e returning..*/.
e7c80 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 static int unixF
e7c90 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 ullPathname(. s
e7ca0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
e7cb0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
e7cc0 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f Pointer to vfs o
e7cd0 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 bject */. const
e7ce0 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 char *zPath,
e7cf0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 /* Poss
e7d00 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e ibly relative in
e7d10 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e put path */. in
e7d20 74 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 20 t nOut,
e7d30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
e7d40 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 ize of output bu
e7d50 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f ffer in bytes */
e7d60 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 20 20 . char *zOut
e7d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e7d80 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 /* Output buffe
e7d90 72 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 49 74 r */.){.. /* It
e7da0 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 's odd to simula
e7db0 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 te an io-error h
e7dc0 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 ere, but really
e7dd0 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a this is just. *
e7de0 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 * using the io-e
e7df0 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 rror infrastruct
e7e00 75 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 ure to test that
e7e10 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 SQLite handles
e7e20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 this. ** functi
e7e30 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68 69 73 on failing. This
e7e40 20 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20 function could
e7e50 66 61 69 6c 20 69 66 2c 20 66 6f 72 20 65 78 61 fail if, for exa
e7e60 6d 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a 20 63 mple, the. ** c
e7e70 75 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64 urrent working d
e7e80 69 72 65 63 74 6f 72 79 20 68 61 73 20 62 65 65 irectory has bee
e7e90 6e 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a 2f n unlinked.. */
e7ea0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
e7eb0 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 or( return SQLIT
e7ec0 45 5f 45 52 52 4f 52 20 29 3b 0a 0a 20 20 61 73 E_ERROR );.. as
e7ed0 73 65 72 74 28 20 70 56 66 73 2d 3e 6d 78 50 61 sert( pVfs->mxPa
e7ee0 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41 54 48 thname==MAX_PATH
e7ef0 4e 41 4d 45 20 29 3b 0a 20 20 55 4e 55 53 45 44 NAME );. UNUSED
e7f00 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 _PARAMETER(pVfs)
e7f10 3b 0a 0a 20 20 7a 4f 75 74 5b 6e 4f 75 74 2d 31 ;.. zOut[nOut-1
e7f20 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 ] = '\0';. if(
e7f30 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 20 29 7b zPath[0]=='/' ){
e7f40 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
e7f50 72 69 6e 74 66 28 6e 4f 75 74 2c 20 7a 4f 75 74 rintf(nOut, zOut
e7f60 2c 20 22 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a , "%s", zPath);.
e7f70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 }else{. int
e7f80 20 6e 43 77 64 3b 0a 20 20 20 20 69 66 28 20 67 nCwd;. if( g
e7f90 65 74 63 77 64 28 7a 4f 75 74 2c 20 6e 4f 75 74 etcwd(zOut, nOut
e7fa0 2d 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 -1)==0 ){.
e7fb0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 return SQLITE_CA
e7fc0 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 NTOPEN;. }.
e7fd0 20 20 6e 43 77 64 20 3d 20 28 69 6e 74 29 73 74 nCwd = (int)st
e7fe0 72 6c 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20 rlen(zOut);.
e7ff0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
e8000 28 6e 4f 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75 (nOut-nCwd, &zOu
e8010 74 5b 6e 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20 t[nCwd], "/%s",
e8020 7a 50 61 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 zPath);. }. re
e8030 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
e8040 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }...#ifndef SQLI
e8050 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
e8060 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 ENSION./*.** Int
e8070 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e erfaces for open
e8080 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 ing a shared lib
e8090 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e rary, finding en
e80a0 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 try points.** wi
e80b0 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 thin the shared
e80c0 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f library, and clo
e80d0 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 sing the shared
e80e0 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 6e 63 library..*/.#inc
e80f0 6c 75 64 65 20 3c 64 6c 66 63 6e 2e 68 3e 0a 73 lude <dlfcn.h>.s
e8100 74 61 74 69 63 20 76 6f 69 64 20 2a 75 6e 69 78 tatic void *unix
e8110 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 DlOpen(sqlite3_v
e8120 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 63 6f 6e fs *NotUsed, con
e8130 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
e8140 6d 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 me){. UNUSED_PA
e8150 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
e8160 3b 0a 20 20 72 65 74 75 72 6e 20 64 6c 6f 70 65 ;. return dlope
e8170 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 52 54 4c n(zFilename, RTL
e8180 44 5f 4e 4f 57 20 7c 20 52 54 4c 44 5f 47 4c 4f D_NOW | RTLD_GLO
e8190 42 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 BAL);.}../*.** S
e81a0 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73 QLite calls this
e81b0 20 66 75 6e 63 74 69 6f 6e 20 69 6d 6d 65 64 69 function immedi
e81c0 61 74 65 6c 79 20 61 66 74 65 72 20 61 20 63 61 ately after a ca
e81d0 6c 6c 20 74 6f 20 75 6e 69 78 44 6c 53 79 6d 28 ll to unixDlSym(
e81e0 29 20 6f 72 0a 2a 2a 20 75 6e 69 78 44 6c 4f 70 ) or.** unixDlOp
e81f0 65 6e 28 29 20 66 61 69 6c 73 20 28 72 65 74 75 en() fails (retu
e8200 72 6e 73 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 rns a null point
e8210 65 72 29 2e 20 49 66 20 61 20 6d 6f 72 65 20 64 er). If a more d
e8220 65 74 61 69 6c 65 64 20 65 72 72 6f 72 0a 2a 2a etailed error.**
e8230 20 6d 65 73 73 61 67 65 20 69 73 20 61 76 61 69 message is avai
e8240 6c 61 62 6c 65 2c 20 69 74 20 69 73 20 77 72 69 lable, it is wri
e8250 74 74 65 6e 20 74 6f 20 7a 42 75 66 4f 75 74 2e tten to zBufOut.
e8260 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 If no error mes
e8270 73 61 67 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c sage.** is avail
e8280 61 62 6c 65 2c 20 7a 42 75 66 4f 75 74 20 69 73 able, zBufOut is
e8290 20 6c 65 66 74 20 75 6e 6d 6f 64 69 66 69 65 64 left unmodified
e82a0 20 61 6e 64 20 53 51 4c 69 74 65 20 75 73 65 73 and SQLite uses
e82b0 20 61 20 64 65 66 61 75 6c 74 0a 2a 2a 20 65 72 a default.** er
e82c0 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a ror message..*/.
e82d0 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 static void unix
e82e0 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f DlError(sqlite3_
e82f0 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e vfs *NotUsed, in
e8300 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
e8310 75 66 4f 75 74 29 7b 0a 20 20 63 68 61 72 20 2a ufOut){. char *
e8320 7a 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 zErr;. UNUSED_P
e8330 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
e8340 29 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 );. unixEnterMu
e8350 74 65 78 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20 tex();. zErr =
e8360 64 6c 65 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 dlerror();. if(
e8370 20 7a 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c zErr ){. sql
e8380 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 ite3_snprintf(nB
e8390 75 66 2c 20 7a 42 75 66 4f 75 74 2c 20 22 25 73 uf, zBufOut, "%s
e83a0 22 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 ", zErr);. }.
e83b0 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 unixLeaveMutex()
e83c0 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ;.}.static void
e83d0 28 2a 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69 (*unixDlSym(sqli
e83e0 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 te3_vfs *NotUsed
e83f0 2c 20 76 6f 69 64 20 2a 70 2c 20 63 6f 6e 73 74 , void *p, const
e8400 20 63 68 61 72 2a 7a 53 79 6d 29 29 28 76 6f 69 char*zSym))(voi
e8410 64 29 7b 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 47 d){. /* . ** G
e8420 43 43 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 CC with -pedanti
e8430 63 2d 65 72 72 6f 72 73 20 73 61 79 73 20 74 68 c-errors says th
e8440 61 74 20 43 39 30 20 64 6f 65 73 20 6e 6f 74 20 at C90 does not
e8450 61 6c 6c 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f allow a void* to
e8460 20 62 65 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e be. ** cast in
e8470 74 6f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 to a pointer to
e8480 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64 a function. And
e8490 20 79 65 74 20 74 68 65 20 6c 69 62 72 61 72 79 yet the library
e84a0 20 64 6c 73 79 6d 28 29 20 72 6f 75 74 69 6e 65 dlsym() routine
e84b0 0a 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 . ** returns a
e84c0 76 6f 69 64 2a 20 77 68 69 63 68 20 69 73 20 72 void* which is r
e84d0 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 eally a pointer
e84e0 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 to a function.
e84f0 53 6f 20 68 6f 77 20 64 6f 20 77 65 0a 20 20 2a So how do we. *
e8500 2a 20 75 73 65 20 64 6c 73 79 6d 28 29 20 77 69 * use dlsym() wi
e8510 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 th -pedantic-err
e8520 6f 72 73 3f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 ors?. **. ** V
e8530 61 72 69 61 62 6c 65 20 78 20 62 65 6c 6f 77 20 ariable x below
e8540 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 is defined to be
e8550 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
e8560 66 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67 0a function taking.
e8570 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 ** parameters
e8580 76 6f 69 64 2a 20 61 6e 64 20 63 6f 6e 73 74 20 void* and const
e8590 63 68 61 72 2a 20 61 6e 64 20 72 65 74 75 72 6e char* and return
e85a0 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ing a pointer to
e85b0 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a a function.. *
e85c0 2a 20 57 65 20 69 6e 69 74 69 61 6c 69 7a 65 20 * We initialize
e85d0 78 20 62 79 20 61 73 73 69 67 6e 69 6e 67 20 69 x by assigning i
e85e0 74 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 t a pointer to t
e85f0 68 65 20 64 6c 73 79 6d 28 29 20 66 75 6e 63 74 he dlsym() funct
e8600 69 6f 6e 2e 0a 20 20 2a 2a 20 28 54 68 61 74 20 ion.. ** (That
e8610 61 73 73 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 assignment requi
e8620 72 65 73 20 61 20 63 61 73 74 2e 29 20 20 54 68 res a cast.) Th
e8630 65 6e 20 77 65 20 63 61 6c 6c 20 74 68 65 20 66 en we call the f
e8640 75 6e 63 74 69 6f 6e 20 74 68 61 74 0a 20 20 2a unction that. *
e8650 2a 20 78 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 * x points to.
e8660 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 . **. ** This
e8670 77 6f 72 6b 2d 61 72 6f 75 6e 64 20 69 73 20 75 work-around is u
e8680 6e 6c 69 6b 65 6c 79 20 74 6f 20 77 6f 72 6b 20 nlikely to work
e8690 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 61 6e 79 correctly on any
e86a0 20 73 79 73 74 65 6d 20 77 68 65 72 65 0a 20 20 system where.
e86b0 2a 2a 20 79 6f 75 20 72 65 61 6c 6c 79 20 63 61 ** you really ca
e86c0 6e 6e 6f 74 20 63 61 73 74 20 61 20 66 75 6e 63 nnot cast a func
e86d0 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 69 6e 74 tion pointer int
e86e0 6f 20 76 6f 69 64 2a 2e 20 20 42 75 74 20 74 68 o void*. But th
e86f0 65 6e 2c 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 en, on the. **
e8700 6f 74 68 65 72 20 68 61 6e 64 2c 20 64 6c 73 79 other hand, dlsy
e8710 6d 28 29 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 m() will not wor
e8720 6b 20 6f 6e 20 73 75 63 68 20 61 20 73 79 73 74 k on such a syst
e8730 65 6d 20 65 69 74 68 65 72 2c 20 73 6f 20 77 65 em either, so we
e8740 20 68 61 76 65 0a 20 20 2a 2a 20 6e 6f 74 20 72 have. ** not r
e8750 65 61 6c 6c 79 20 6c 6f 73 74 20 61 6e 79 74 68 eally lost anyth
e8760 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64 ing.. */. void
e8770 20 28 2a 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f (*(*x)(void*,co
e8780 6e 73 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64 nst char*))(void
e8790 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
e87a0 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
e87b0 20 20 78 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29 x = (void(*(*)
e87c0 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 (void*,const cha
e87d0 72 2a 29 29 28 76 6f 69 64 29 29 64 6c 73 79 6d r*))(void))dlsym
e87e0 3b 0a 20 20 72 65 74 75 72 6e 20 28 2a 78 29 28 ;. return (*x)(
e87f0 70 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 73 74 61 74 p, zSym);.}.stat
e8800 69 63 20 76 6f 69 64 20 75 6e 69 78 44 6c 43 6c ic void unixDlCl
e8810 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ose(sqlite3_vfs
e8820 2a 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a *NotUsed, void *
e8830 70 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 pHandle){. UNUS
e8840 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
e8850 55 73 65 64 29 3b 0a 20 20 64 6c 63 6c 6f 73 65 Used);. dlclose
e8860 28 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c (pHandle);.}.#el
e8870 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f se /* if SQLITE_
e8880 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 OMIT_LOAD_EXTENS
e8890 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 ION is defined:
e88a0 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 */. #define uni
e88b0 78 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 xDlOpen 0. #de
e88c0 66 69 6e 65 20 75 6e 69 78 44 6c 45 72 72 6f 72 fine unixDlError
e88d0 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 0. #define uni
e88e0 78 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 xDlSym 0. #de
e88f0 66 69 6e 65 20 75 6e 69 78 44 6c 43 6c 6f 73 65 fine unixDlClose
e8900 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0.#endif../*.**
e8910 20 57 72 69 74 65 20 6e 42 75 66 20 62 79 74 65 Write nBuf byte
e8920 73 20 6f 66 20 72 61 6e 64 6f 6d 20 64 61 74 61 s of random data
e8930 20 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 to the supplied
e8940 20 62 75 66 66 65 72 20 7a 42 75 66 2e 0a 2a 2f buffer zBuf..*/
e8950 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
e8960 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 Randomness(sqlit
e8970 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c e3_vfs *NotUsed,
e8980 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 int nBuf, char
e8990 2a 7a 42 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 *zBuf){. UNUSED
e89a0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
e89b0 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 28 73 ed);. assert((s
e89c0 69 7a 65 5f 74 29 6e 42 75 66 3e 3d 28 73 69 7a ize_t)nBuf>=(siz
e89d0 65 6f 66 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65 eof(time_t)+size
e89e0 6f 66 28 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a of(int)));.. /*
e89f0 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 69 74 We have to init
e8a00 69 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f 20 70 ialize zBuf to p
e8a10 72 65 76 65 6e 74 20 76 61 6c 67 72 69 6e 64 20 revent valgrind
e8a20 66 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20 from reporting.
e8a30 20 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54 68 65 ** errors. The
e8a40 20 72 65 70 6f 72 74 73 20 69 73 73 75 65 64 20 reports issued
e8a50 62 79 20 76 61 6c 67 72 69 6e 64 20 61 72 65 20 by valgrind are
e8a60 69 6e 63 6f 72 72 65 63 74 20 2d 20 77 65 20 77 incorrect - we w
e8a70 6f 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66 65 72 ould. ** prefer
e8a80 20 74 68 61 74 20 74 68 65 20 72 61 6e 64 6f 6d that the random
e8a90 6e 65 73 73 20 62 65 20 69 6e 63 72 65 61 73 65 ness be increase
e8aa0 64 20 62 79 20 6d 61 6b 69 6e 67 20 75 73 65 20 d by making use
e8ab0 6f 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e of the. ** unin
e8ac0 69 74 69 61 6c 69 7a 65 64 20 73 70 61 63 65 20 itialized space
e8ad0 69 6e 20 7a 42 75 66 20 2d 20 62 75 74 20 76 61 in zBuf - but va
e8ae0 6c 67 72 69 6e 64 20 65 72 72 6f 72 73 20 74 65 lgrind errors te
e8af0 6e 64 20 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a nd to worry. **
e8b00 20 73 6f 6d 65 20 75 73 65 72 73 2e 20 20 52 61 some users. Ra
e8b10 74 68 65 72 20 74 68 61 6e 20 61 72 67 75 65 2c ther than argue,
e8b20 20 69 74 20 73 65 65 6d 73 20 65 61 73 69 65 72 it seems easier
e8b30 20 6a 75 73 74 20 74 6f 20 69 6e 69 74 69 61 6c just to initial
e8b40 69 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f ize. ** the who
e8b50 6c 65 20 61 72 72 61 79 20 61 6e 64 20 73 69 6c le array and sil
e8b60 65 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c 20 65 ence valgrind, e
e8b70 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e ven if that mean
e8b80 73 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73 s less randomnes
e8b90 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 61 s. ** in the ra
e8ba0 6e 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a ndom seed.. **.
e8bb0 20 20 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e ** When testin
e8bc0 67 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 g, initializing
e8bd0 7a 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 zBuf[] to zero i
e8be0 73 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 s all we do. Th
e8bf0 61 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 at means. ** th
e8c00 61 74 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 at we always use
e8c10 20 74 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d the same random
e8c20 20 6e 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65 number sequence
e8c30 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 . This makes th
e8c40 65 0a 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70 e. ** tests rep
e8c50 65 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 eatable.. */.
e8c60 6d 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 memset(zBuf, 0,
e8c70 6e 42 75 66 29 3b 0a 23 69 66 20 21 64 65 66 69 nBuf);.#if !defi
e8c80 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 ned(SQLITE_TEST)
e8c90 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 70 69 64 . {. int pid
e8ca0 2c 20 66 64 3b 0a 20 20 20 20 66 64 20 3d 20 6f , fd;. fd = o
e8cb0 70 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f pen("/dev/urando
e8cc0 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 m", O_RDONLY);.
e8cd0 20 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 if( fd<0 ){.
e8ce0 20 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 time_t t;.
e8cf0 20 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 time(&t);.
e8d00 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 memcpy(zBuf
e8d10 2c 20 26 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 , &t, sizeof(t))
e8d20 3b 0a 20 20 20 20 20 20 70 69 64 20 3d 20 67 65 ;. pid = ge
e8d30 74 70 69 64 28 29 3b 0a 20 20 20 20 20 20 6d 65 tpid();. me
e8d40 6d 63 70 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f mcpy(&zBuf[sizeo
e8d50 66 28 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a f(t)], &pid, siz
e8d60 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 20 eof(pid));.
e8d70 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 assert( sizeof(
e8d80 74 29 2b 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d t)+sizeof(pid)<=
e8d90 28 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 3b 0a (size_t)nBuf );.
e8da0 20 20 20 20 20 20 6e 42 75 66 20 3d 20 73 69 7a nBuf = siz
e8db0 65 6f 66 28 74 29 20 2b 20 73 69 7a 65 6f 66 28 eof(t) + sizeof(
e8dc0 70 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b pid);. }else{
e8dd0 0a 20 20 20 20 20 20 6e 42 75 66 20 3d 20 72 65 . nBuf = re
e8de0 61 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e 42 75 ad(fd, zBuf, nBu
e8df0 66 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 f);. close(
e8e00 66 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 fd);. }. }.#
e8e10 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 6e endif. return n
e8e20 42 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 Buf;.}.../*.** S
e8e30 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c leep for a littl
e8e40 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e e while. Return
e8e50 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 the amount of t
e8e60 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 ime slept..** Th
e8e70 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 e argument is th
e8e80 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 e number of micr
e8e90 6f 73 65 63 6f 6e 64 73 20 77 65 20 77 61 6e 74 oseconds we want
e8ea0 20 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 54 68 to sleep..** Th
e8eb0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 e return value i
e8ec0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
e8ed0 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f 66 20 microseconds of
e8ee0 73 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a sleep actually.*
e8ef0 2a 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d * requested from
e8f00 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 the underlying
e8f10 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
e8f20 2c 20 61 20 6e 75 6d 62 65 72 20 77 68 69 63 68 , a number which
e8f30 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 67 72 65 .** might be gre
e8f40 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 ater than or equ
e8f50 61 6c 20 74 6f 20 74 68 65 20 61 72 67 75 6d 65 al to the argume
e8f60 6e 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65 73 73 nt, but not less
e8f70 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 72 67 .** than the arg
e8f80 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 ument..*/.static
e8f90 20 69 6e 74 20 75 6e 69 78 53 6c 65 65 70 28 73 int unixSleep(s
e8fa0 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 qlite3_vfs *NotU
e8fb0 73 65 64 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 sed, int microse
e8fc0 63 6f 6e 64 73 29 7b 0a 23 69 66 20 4f 53 5f 56 conds){.#if OS_V
e8fd0 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 XWORKS. struct
e8fe0 74 69 6d 65 73 70 65 63 20 73 70 3b 0a 0a 20 20 timespec sp;..
e8ff0 73 70 2e 74 76 5f 73 65 63 20 3d 20 6d 69 63 72 sp.tv_sec = micr
e9000 6f 73 65 63 6f 6e 64 73 20 2f 20 31 30 30 30 30 oseconds / 10000
e9010 30 30 3b 0a 20 20 73 70 2e 74 76 5f 6e 73 65 63 00;. sp.tv_nsec
e9020 20 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 = (microseconds
e9030 20 25 20 31 30 30 30 30 30 30 29 20 2a 20 31 30 % 1000000) * 10
e9040 30 30 3b 0a 20 20 6e 61 6e 6f 73 6c 65 65 70 28 00;. nanosleep(
e9050 26 73 70 2c 20 4e 55 4c 4c 29 3b 0a 20 20 55 4e &sp, NULL);. UN
e9060 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
e9070 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 otUsed);. retur
e9080 6e 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a n microseconds;.
e9090 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 #elif defined(HA
e90a0 56 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41 VE_USLEEP) && HA
e90b0 56 45 5f 55 53 4c 45 45 50 0a 20 20 75 73 6c 65 VE_USLEEP. usle
e90c0 65 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 ep(microseconds)
e90d0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
e90e0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
e90f0 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 return microsec
e9100 6f 6e 64 73 3b 0a 23 65 6c 73 65 0a 20 20 69 6e onds;.#else. in
e9110 74 20 73 65 63 6f 6e 64 73 20 3d 20 28 6d 69 63 t seconds = (mic
e9120 72 6f 73 65 63 6f 6e 64 73 2b 39 39 39 39 39 39 roseconds+999999
e9130 29 2f 31 30 30 30 30 30 30 3b 0a 20 20 73 6c 65 )/1000000;. sle
e9140 65 70 28 73 65 63 6f 6e 64 73 29 3b 0a 20 20 55 ep(seconds);. U
e9150 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
e9160 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 NotUsed);. retu
e9170 72 6e 20 73 65 63 6f 6e 64 73 2a 31 30 30 30 30 rn seconds*10000
e9180 30 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 00;.#endif.}../*
e9190 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
e91a0 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 g variable, if s
e91b0 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f et to a non-zero
e91c0 20 76 61 6c 75 65 2c 20 69 73 20 69 6e 74 65 72 value, is inter
e91d0 70 72 65 74 65 64 20 61 73 0a 2a 2a 20 74 68 65 preted as.** the
e91e0 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 63 6f 6e number of secon
e91f0 64 73 20 73 69 6e 63 65 20 31 39 37 30 20 61 6e ds since 1970 an
e9200 64 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 d is used to set
e9210 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a the result of.*
e9220 2a 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 * sqlite3OsCurre
e9230 6e 74 54 69 6d 65 28 29 20 64 75 72 69 6e 67 20 ntTime() during
e9240 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 testing..*/.#ifd
e9250 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
e9260 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
e9270 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 lite3_current_ti
e9280 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 46 61 6b 65 me = 0; /* Fake
e9290 20 73 79 73 74 65 6d 20 74 69 6d 65 20 69 6e 20 system time in
e92a0 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 seconds since 19
e92b0 37 30 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 70. */.#endif../
e92c0 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 *.** Find the cu
e92d0 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 rrent time (in U
e92e0 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e niversal Coordin
e92f0 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 ated Time). Wri
e9300 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e te the.** curren
e9310 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 t time and date
e9320 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 as a Julian Day
e9330 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e number into *prN
e9340 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e ow and.** return
e9350 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 0. Return 1 if
e9360 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 the time and da
e9370 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 te cannot be fou
e9380 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e nd..*/.static in
e9390 74 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 6d t unixCurrentTim
e93a0 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e e(sqlite3_vfs *N
e93b0 6f 74 55 73 65 64 2c 20 64 6f 75 62 6c 65 20 2a otUsed, double *
e93c0 70 72 4e 6f 77 29 7b 0a 23 69 66 20 64 65 66 69 prNow){.#if defi
e93d0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
e93e0 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a FLOATING_POINT).
e93f0 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69 time_t t;. ti
e9400 6d 65 28 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 me(&t);. *prNow
e9410 20 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e = (((sqlite3_in
e9420 74 36 34 29 74 29 2f 38 36 34 30 20 2b 20 32 34 t64)t)/8640 + 24
e9430 34 30 35 38 37 35 29 2f 31 30 3b 0a 23 65 6c 69 405875)/10;.#eli
e9440 66 20 64 65 66 69 6e 65 64 28 4e 4f 5f 47 45 54 f defined(NO_GET
e9450 54 4f 44 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b TOD). time_t t;
e9460 0a 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a . time(&t);. *
e9470 70 72 4e 6f 77 20 3d 20 74 2f 38 36 34 30 30 2e prNow = t/86400.
e9480 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 23 0 + 2440587.5;.#
e9490 65 6c 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a elif OS_VXWORKS.
e94a0 20 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 struct timespe
e94b0 63 20 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63 6b 5f c sNow;. clock_
e94c0 67 65 74 74 69 6d 65 28 43 4c 4f 43 4b 5f 52 45 gettime(CLOCK_RE
e94d0 41 4c 54 49 4d 45 2c 20 26 73 4e 6f 77 29 3b 0a ALTIME, &sNow);.
e94e0 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 *prNow = 24405
e94f0 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 87.5 + sNow.tv_s
e9500 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f ec/86400.0 + sNo
e9510 77 2e 74 76 5f 6e 73 65 63 2f 38 36 34 30 30 30 w.tv_nsec/864000
e9520 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6c 73 00000000.0;.#els
e9530 65 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65 76 e. struct timev
e9540 61 6c 20 73 4e 6f 77 3b 0a 20 20 67 65 74 74 69 al sNow;. getti
e9550 6d 65 6f 66 64 61 79 28 26 73 4e 6f 77 2c 20 30 meofday(&sNow, 0
e9560 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 );. *prNow = 24
e9570 34 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 40587.5 + sNow.t
e9580 76 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 v_sec/86400.0 +
e9590 73 4e 6f 77 2e 74 76 5f 75 73 65 63 2f 38 36 34 sNow.tv_usec/864
e95a0 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6e 64 00000000.0;.#end
e95b0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
e95c0 45 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c E_TEST. if( sql
e95d0 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d ite3_current_tim
e95e0 65 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 e ){. *prNow
e95f0 3d 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e = sqlite3_curren
e9600 74 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b t_time/86400.0 +
e9610 20 32 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 2440587.5;. }.
e9620 23 65 6e 64 69 66 0a 20 20 55 4e 55 53 45 44 5f #endif. UNUSED_
e9630 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
e9640 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a d);. return 0;.
e9650 7d 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64 64 65 }../*.** We adde
e9660 64 20 74 68 65 20 78 47 65 74 4c 61 73 74 45 72 d the xGetLastEr
e9670 72 6f 72 28 29 20 6d 65 74 68 6f 64 20 77 69 74 ror() method wit
e9680 68 20 74 68 65 20 69 6e 74 65 6e 74 69 6f 6e 20 h the intention
e9690 6f 66 20 70 72 6f 76 69 64 69 6e 67 0a 2a 2a 20 of providing.**
e96a0 62 65 74 74 65 72 20 6c 6f 77 2d 6c 65 76 65 6c better low-level
e96b0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 error messages
e96c0 77 68 65 6e 20 6f 70 65 72 61 74 69 6e 67 2d 73 when operating-s
e96d0 79 73 74 65 6d 20 70 72 6f 62 6c 65 6d 73 20 63 ystem problems c
e96e0 6f 6d 65 20 75 70 0a 2a 2a 20 64 75 72 69 6e 67 ome up.** during
e96f0 20 53 51 4c 69 74 65 20 6f 70 65 72 61 74 69 6f SQLite operatio
e9700 6e 2e 20 20 42 75 74 20 73 6f 20 66 61 72 2c 20 n. But so far,
e9710 6e 6f 6e 65 20 6f 66 20 74 68 61 74 20 68 61 73 none of that has
e9720 20 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 been implemente
e9730 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 65 d.** in the core
e9740 2e 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 . So this routi
e9750 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c ne is never call
e9760 65 64 2e 20 20 46 6f 72 20 6e 6f 77 2c 20 69 74 ed. For now, it
e9770 20 69 73 20 6d 65 72 65 6c 79 0a 2a 2a 20 61 20 is merely.** a
e9780 70 6c 61 63 65 2d 68 6f 6c 64 65 72 2e 0a 2a 2f place-holder..*/
e9790 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
e97a0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c GetLastError(sql
e97b0 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 ite3_vfs *NotUse
e97c0 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 2c d, int NotUsed2,
e97d0 20 63 68 61 72 20 2a 4e 6f 74 55 73 65 64 33 29 char *NotUsed3)
e97e0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
e97f0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
e9800 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
e9810 52 28 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 55 R(NotUsed2);. U
e9820 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
e9830 4e 6f 74 55 73 65 64 33 29 3b 0a 20 20 72 65 74 NotUsed3);. ret
e9840 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 2a urn 0;.}../*.***
e9850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9860 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c ***** End of sql
e9870 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 ite3_vfs methods
e9880 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
e9890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
e98a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e98b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e98c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e98d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e98e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
e98f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
e9940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9950 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 50 ******** Begin P
e9960 72 6f 78 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a roxy Locking ***
e9970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
e9990 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e .** Proxy lockin
e99a0 67 20 69 73 20 61 20 22 75 62 65 72 2d 6c 6f 63 g is a "uber-loc
e99b0 6b 69 6e 67 2d 6d 65 74 68 6f 64 22 20 69 6e 20 king-method" in
e99c0 74 68 69 73 20 73 65 6e 73 65 3a 20 20 49 74 20 this sense: It
e99d0 75 73 65 73 20 74 68 65 0a 2a 2a 20 6f 74 68 65 uses the.** othe
e99e0 72 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 r locking method
e99f0 73 20 6f 6e 20 73 65 63 6f 6e 64 61 72 79 20 6c s on secondary l
e9a00 6f 63 6b 20 66 69 6c 65 73 2e 20 20 50 72 6f 78 ock files. Prox
e9a10 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 0a 2a y locking is a.*
e9a20 2a 20 6d 65 74 61 2d 6c 61 79 65 72 20 6f 76 65 * meta-layer ove
e9a30 72 20 74 6f 70 20 6f 66 20 74 68 65 20 70 72 69 r top of the pri
e9a40 6d 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67 20 69 mitive locking i
e9a50 6d 70 6c 65 6d 65 6e 74 65 64 20 61 62 6f 76 65 mplemented above
e9a60 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 69 73 20 72 . For.** this r
e9a70 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 eason, the divis
e9a80 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ion that impleme
e9a90 6e 74 73 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 nts of proxy loc
e9aa0 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 king is deferred
e9ab0 0a 2a 2a 20 75 6e 74 69 6c 20 6c 61 74 65 20 69 .** until late i
e9ac0 6e 20 74 68 65 20 66 69 6c 65 20 28 68 65 72 65 n the file (here
e9ad0 29 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 ) after all of t
e9ae0 68 65 20 6f 74 68 65 72 20 49 2f 4f 20 6d 65 74 he other I/O met
e9af0 68 6f 64 73 20 68 61 76 65 0a 2a 2a 20 62 65 65 hods have.** bee
e9b00 6e 20 64 65 66 69 6e 65 64 20 2d 20 73 6f 20 74 n defined - so t
e9b10 68 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 hat the primitiv
e9b20 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 e locking method
e9b30 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 0a s are available.
e9b40 2a 2a 20 61 73 20 73 65 72 76 69 63 65 73 20 74 ** as services t
e9b50 6f 20 68 65 6c 70 20 77 69 74 68 20 74 68 65 20 o help with the
e9b60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
e9b70 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e f proxy locking.
e9b80 0a 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 .**.****.**.** T
e9b90 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 he default locki
e9ba0 6e 67 20 73 63 68 65 6d 65 73 20 69 6e 20 53 51 ng schemes in SQ
e9bb0 4c 69 74 65 20 75 73 65 20 62 79 74 65 2d 72 61 Lite use byte-ra
e9bc0 6e 67 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 nge locks on the
e9bd0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c .** database fil
e9be0 65 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 e to coordinate
e9bf0 73 61 66 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 safe, concurrent
e9c00 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c 74 69 access by multi
e9c10 70 6c 65 20 72 65 61 64 65 72 73 0a 2a 2a 20 61 ple readers.** a
e9c20 6e 64 20 77 72 69 74 65 72 73 20 5b 68 74 74 70 nd writers [http
e9c30 3a 2f 2f 73 71 6c 69 74 65 2e 6f 72 67 2f 6c 6f ://sqlite.org/lo
e9c40 63 6b 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e 20 20 ckingv3.html].
e9c50 54 68 65 20 66 69 76 65 20 66 69 6c 65 20 6c 6f The five file lo
e9c60 63 6b 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 20 cking.** states
e9c70 28 55 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e 44 49 (UNLOCKED, PENDI
e9c80 4e 47 2c 20 53 48 41 52 45 44 2c 20 52 45 53 45 NG, SHARED, RESE
e9c90 52 56 45 44 2c 20 45 58 43 4c 55 53 49 56 45 29 RVED, EXCLUSIVE)
e9ca0 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 are implemented
e9cb0 0a 2a 2a 20 61 73 20 50 4f 53 49 58 20 72 65 61 .** as POSIX rea
e9cc0 64 20 26 20 77 72 69 74 65 20 6c 6f 63 6b 73 20 d & write locks
e9cd0 6f 76 65 72 20 66 69 78 65 64 20 73 65 74 20 6f over fixed set o
e9ce0 66 20 6c 6f 63 61 74 69 6f 6e 73 20 28 76 69 61 f locations (via
e9cf0 20 66 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e 20 41 fsctl),.** on A
e9d00 46 50 20 61 6e 64 20 53 4d 42 20 6f 6e 6c 79 20 FP and SMB only
e9d10 65 78 63 6c 75 73 69 76 65 20 62 79 74 65 2d 72 exclusive byte-r
e9d20 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 61 ange locks are a
e9d30 76 61 69 6c 61 62 6c 65 20 76 69 61 20 66 73 63 vailable via fsc
e9d40 74 6c 0a 2a 2a 20 77 69 74 68 20 5f 49 4f 57 52 tl.** with _IOWR
e9d50 28 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74 ('z', 23, struct
e9d60 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 ByteRangeLockPB
e9d70 32 29 20 74 6f 20 74 72 61 63 6b 20 74 68 65 20 2) to track the
e9d80 73 61 6d 65 20 35 20 73 74 61 74 65 73 2e 0a 2a same 5 states..*
e9d90 2a 20 54 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 * To simulate a
e9da0 46 5f 52 44 4c 43 4b 20 6f 6e 20 74 68 65 20 73 F_RDLCK on the s
e9db0 68 61 72 65 64 20 72 61 6e 67 65 2c 20 6f 6e 20 hared range, on
e9dc0 41 46 50 20 61 20 72 61 6e 64 6f 6d 6c 79 20 73 AFP a randomly s
e9dd0 65 6c 65 63 74 65 64 0a 2a 2a 20 61 64 64 72 65 elected.** addre
e9de0 73 73 20 69 6e 20 74 68 65 20 73 68 61 72 65 64 ss in the shared
e9df0 20 72 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20 range is taken
e9e00 66 6f 72 20 61 20 53 48 41 52 45 44 20 6c 6f 63 for a SHARED loc
e9e10 6b 2c 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a k, the entire.**
e9e20 20 73 68 61 72 65 64 20 72 61 6e 67 65 20 69 73 shared range is
e9e30 20 74 61 6b 65 6e 20 66 6f 72 20 61 6e 20 45 58 taken for an EX
e9e40 43 4c 55 53 49 56 45 20 6c 6f 63 6b 29 3a 0a 2a CLUSIVE lock):.*
e9e50 2a 0a 2a 2a 20 20 20 20 20 20 50 45 4e 44 49 4e *.** PENDIN
e9e60 47 5f 42 59 54 45 20 20 20 20 20 20 20 20 30 78 G_BYTE 0x
e9e70 34 30 30 30 30 30 30 30 09 09 20 20 20 09 0a 2a 40000000.. ..*
e9e80 2a 20 20 20 20 20 20 52 45 53 45 52 56 45 44 5f * RESERVED_
e9e90 42 59 54 45 20 20 20 20 20 20 20 30 78 34 30 30 BYTE 0x400
e9ea0 30 30 30 30 31 0a 2a 2a 20 20 20 20 20 20 53 48 00001.** SH
e9eb0 41 52 45 44 5f 52 41 4e 47 45 20 20 20 20 20 20 ARED_RANGE
e9ec0 20 20 30 78 34 30 30 30 30 30 30 32 20 2d 3e 20 0x40000002 ->
e9ed0 30 78 34 30 30 30 30 32 30 30 0a 2a 2a 0a 2a 2a 0x40000200.**.**
e9ee0 20 54 68 69 73 20 77 6f 72 6b 73 20 77 65 6c 6c This works well
e9ef0 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69 on the local fi
e9f00 6c 65 20 73 79 73 74 65 6d 2c 20 62 75 74 20 73 le system, but s
e9f10 68 6f 77 73 20 61 20 6e 65 61 72 6c 79 20 31 30 hows a nearly 10
e9f20 30 78 0a 2a 2a 20 73 6c 6f 77 64 6f 77 6e 20 69 0x.** slowdown i
e9f30 6e 20 72 65 61 64 20 70 65 72 66 6f 72 6d 61 6e n read performan
e9f40 63 65 20 6f 6e 20 41 46 50 20 62 65 63 61 75 73 ce on AFP becaus
e9f50 65 20 74 68 65 20 41 46 50 20 63 6c 69 65 6e 74 e the AFP client
e9f60 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 74 68 65 disables.** the
e9f70 20 72 65 61 64 20 63 61 63 68 65 20 77 68 65 6e read cache when
e9f80 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b byte-range lock
e9f90 73 20 61 72 65 20 70 72 65 73 65 6e 74 2e 20 20 s are present.
e9fa0 45 6e 61 62 6c 69 6e 67 20 74 68 65 20 72 65 61 Enabling the rea
e9fb0 64 0a 2a 2a 20 63 61 63 68 65 20 65 78 70 6f 73 d.** cache expos
e9fc0 65 73 20 61 20 63 61 63 68 65 20 63 6f 68 65 72 es a cache coher
e9fd0 65 6e 63 79 20 70 72 6f 62 6c 65 6d 20 74 68 61 ency problem tha
e9fe0 74 20 69 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 t is present on
e9ff0 61 6c 6c 20 4f 53 20 58 0a 2a 2a 20 73 75 70 70 all OS X.** supp
ea000 6f 72 74 65 64 20 6e 65 74 77 6f 72 6b 20 66 69 orted network fi
ea010 6c 65 20 73 79 73 74 65 6d 73 2e 20 20 4e 46 53 le systems. NFS
ea020 20 61 6e 64 20 41 46 50 20 62 6f 74 68 20 6f 62 and AFP both ob
ea030 73 65 72 76 65 20 74 68 65 0a 2a 2a 20 63 6c 6f serve the.** clo
ea040 73 65 2d 74 6f 2d 6f 70 65 6e 20 73 65 6d 61 6e se-to-open seman
ea050 74 69 63 73 20 66 6f 72 20 65 6e 73 75 72 69 6e tics for ensurin
ea060 67 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 g cache coherenc
ea070 79 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 6e 66 73 y.** [http://nfs
ea080 2e 73 6f 75 72 63 65 66 6f 72 67 65 2e 6e 65 74 .sourceforge.net
ea090 2f 23 66 61 71 5f 61 38 5d 2c 20 77 68 69 63 68 /#faq_a8], which
ea0a0 20 64 6f 65 73 20 6e 6f 74 20 65 66 66 65 63 74 does not effect
ea0b0 69 76 65 6c 79 0a 2a 2a 20 61 64 64 72 65 73 73 ively.** address
ea0c0 20 74 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74 the requirement
ea0d0 73 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 74 s for concurrent
ea0e0 20 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 database access
ea0f0 20 62 79 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 by multiple.**
ea100 72 65 61 64 65 72 73 20 61 6e 64 20 77 72 69 74 readers and writ
ea110 65 72 73 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 77 ers.** [http://w
ea120 77 77 2e 6e 61 62 62 6c 65 2e 63 6f 6d 2f 53 51 ww.nabble.com/SQ
ea130 4c 69 74 65 2d 6f 6e 2d 4e 46 53 2d 63 61 63 68 Lite-on-NFS-cach
ea140 65 2d 63 6f 68 65 72 65 6e 63 79 2d 74 64 31 35 e-coherency-td15
ea150 36 35 35 37 30 31 2e 68 74 6d 6c 5d 2e 0a 2a 2a 655701.html]..**
ea160 0a 2a 2a 20 54 6f 20 61 64 64 72 65 73 73 20 74 .** To address t
ea170 68 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61 he performance a
ea180 6e 64 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e nd cache coheren
ea190 63 79 20 69 73 73 75 65 73 2c 20 70 72 6f 78 79 cy issues, proxy
ea1a0 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a file locking.**
ea1b0 20 63 68 61 6e 67 65 73 20 74 68 65 20 77 61 79 changes the way
ea1c0 20 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 database access
ea1d0 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 is controlled b
ea1e0 79 20 6c 69 6d 69 74 69 6e 67 20 61 63 63 65 73 y limiting acces
ea1f0 73 20 74 6f 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 s to a.** single
ea200 20 68 6f 73 74 20 61 74 20 61 20 74 69 6d 65 20 host at a time
ea210 61 6e 64 20 6d 6f 76 69 6e 67 20 66 69 6c 65 20 and moving file
ea220 6c 6f 63 6b 73 20 6f 66 66 20 6f 66 20 74 68 65 locks off of the
ea230 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a database file.*
ea240 2a 20 61 6e 64 20 6f 6e 74 6f 20 61 20 70 72 6f * and onto a pro
ea250 78 79 20 66 69 6c 65 20 6f 6e 20 74 68 65 20 6c xy file on the l
ea260 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d ocal file system
ea270 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 69 . .**.**.** Usi
ea280 6e 67 20 70 72 6f 78 79 20 6c 6f 63 6b 73 0a 2a ng proxy locks.*
ea290 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * --------------
ea2a0 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 43 20 41 50 49 73 ---.**.** C APIs
ea2b0 0a 2a 2a 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f .**.** sqlite3_
ea2c0 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c file_control(db,
ea2d0 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f dbname, SQLITE_
ea2e0 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c SET_LOCKPROXYFIL
ea2f0 45 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 E,.**
ea300 20 20 20 20 20 20 20 20 20 20 20 20 3c 70 72 6f <pro
ea310 78 79 5f 70 61 74 68 3e 20 7c 20 22 3a 61 75 74 xy_path> | ":aut
ea320 6f 3a 22 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 o:");.** sqlite
ea330 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 3_file_control(d
ea340 62 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 b, dbname, SQLIT
ea350 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 E_GET_LOCKPROXYF
ea360 49 4c 45 2c 20 26 3c 70 72 6f 78 79 5f 70 61 74 ILE, &<proxy_pat
ea370 68 3e 29 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 53 51 h>);.**.**.** SQ
ea380 4c 20 70 72 61 67 6d 61 73 0a 2a 2a 0a 2a 2a 20 L pragmas.**.**
ea390 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 PRAGMA [databas
ea3a0 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 e.]lock_proxy_fi
ea3b0 6c 65 3d 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20 le=<proxy_path>
ea3c0 7c 20 3a 61 75 74 6f 3a 0a 2a 2a 20 20 50 52 41 | :auto:.** PRA
ea3d0 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c GMA [database.]l
ea3e0 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 0a 2a ock_proxy_file.*
ea3f0 2a 0a 2a 2a 20 53 70 65 63 69 66 79 69 6e 67 20 *.** Specifying
ea400 22 3a 61 75 74 6f 3a 22 20 6d 65 61 6e 73 20 74 ":auto:" means t
ea410 68 61 74 20 69 66 20 74 68 65 72 65 20 69 73 20 hat if there is
ea420 61 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 74 a conch file wit
ea430 68 20 61 20 6d 61 74 63 68 69 6e 67 0a 2a 2a 20 h a matching.**
ea440 68 6f 73 74 20 49 44 20 69 6e 20 69 74 2c 20 74 host ID in it, t
ea450 68 65 20 70 72 6f 78 79 20 70 61 74 68 20 69 6e he proxy path in
ea460 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 the conch file
ea470 77 69 6c 6c 20 62 65 20 75 73 65 64 2c 20 6f 74 will be used, ot
ea480 68 65 72 77 69 73 65 0a 2a 2a 20 61 20 70 72 6f herwise.** a pro
ea490 78 79 20 70 61 74 68 20 62 61 73 65 64 20 6f 6e xy path based on
ea4a0 20 74 68 65 20 75 73 65 72 27 73 20 74 65 6d 70 the user's temp
ea4b0 20 64 69 72 0a 2a 2a 20 28 76 69 61 20 63 6f 6e dir.** (via con
ea4c0 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49 4e 5f fstr(_CS_DARWIN_
ea4d0 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c 2e 2e USER_TEMP_DIR,..
ea4e0 2e 29 29 20 77 69 6c 6c 20 62 65 20 75 73 65 64 .)) will be used
ea4f0 20 61 6e 64 20 74 68 65 0a 2a 2a 20 61 63 74 75 and the.** actu
ea500 61 6c 20 70 72 6f 78 79 20 66 69 6c 65 20 6e 61 al proxy file na
ea510 6d 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 me is generated
ea520 66 72 6f 6d 20 74 68 65 20 6e 61 6d 65 20 61 6e from the name an
ea530 64 20 70 61 74 68 20 6f 66 20 74 68 65 0a 2a 2a d path of the.**
ea540 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
ea550 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a For example:.**
ea560 0a 2a 2a 20 20 20 20 20 20 20 46 6f 72 20 64 61 .** For da
ea570 74 61 62 61 73 65 20 70 61 74 68 20 22 2f 55 73 tabase path "/Us
ea580 65 72 73 2f 6d 65 2f 66 6f 6f 2e 64 62 22 20 0a ers/me/foo.db" .
ea590 2a 2a 20 20 20 20 20 20 20 54 68 65 20 6c 6f 63 ** The loc
ea5a0 6b 20 70 61 74 68 20 77 69 6c 6c 20 62 65 20 22 k path will be "
ea5b0 3c 74 6d 70 64 69 72 3e 2f 73 71 6c 69 74 65 70 <tmpdir>/sqlitep
ea5c0 6c 6f 63 6b 73 2f 5f 55 73 65 72 73 5f 6d 65 5f locks/_Users_me_
ea5d0 66 6f 6f 2e 64 62 3a 61 75 74 6f 3a 22 29 0a 2a foo.db:auto:").*
ea5e0 2a 0a 2a 2a 20 4f 6e 63 65 20 61 20 6c 6f 63 6b *.** Once a lock
ea5f0 20 70 72 6f 78 79 20 69 73 20 63 6f 6e 66 69 67 proxy is config
ea600 75 72 65 64 20 66 6f 72 20 61 20 64 61 74 61 62 ured for a datab
ea610 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 ase connection,
ea620 69 74 20 63 61 6e 20 6e 6f 74 0a 2a 2a 20 62 65 it can not.** be
ea630 20 72 65 6d 6f 76 65 64 2c 20 68 6f 77 65 76 65 removed, howeve
ea640 72 20 69 74 20 6d 61 79 20 62 65 20 73 77 69 74 r it may be swit
ea650 63 68 65 64 20 74 6f 20 61 20 64 69 66 66 65 72 ched to a differ
ea660 65 6e 74 20 70 72 6f 78 79 20 70 61 74 68 20 76 ent proxy path v
ea670 69 61 0a 2a 2a 20 74 68 65 20 61 62 6f 76 65 20 ia.** the above
ea680 41 50 49 73 20 28 61 73 73 75 6d 69 6e 67 20 74 APIs (assuming t
ea690 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 he conch file is
ea6a0 20 6e 6f 74 20 62 65 69 6e 67 20 68 65 6c 64 20 not being held
ea6b0 62 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f by another.** co
ea6c0 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70 72 6f 63 nnection or proc
ea6d0 65 73 73 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 ess). .**.**.**
ea6e0 48 6f 77 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e How proxy lockin
ea6f0 67 20 77 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d g works.** -----
ea700 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
ea710 2d 2d 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 66 --.**.** Proxy f
ea720 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 72 65 6c 69 ile locking reli
ea730 65 73 20 70 72 69 6d 61 72 69 6c 79 20 6f 6e 20 es primarily on
ea740 74 77 6f 20 6e 65 77 20 73 75 70 70 6f 72 74 69 two new supporti
ea750 6e 67 20 66 69 6c 65 73 3a 20 0a 2a 2a 0a 2a 2a ng files: .**.**
ea760 20 20 20 2a 20 20 63 6f 6e 63 68 20 66 69 6c 65 * conch file
ea770 20 74 6f 20 6c 69 6d 69 74 20 61 63 63 65 73 73 to limit access
ea780 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
ea790 20 66 69 6c 65 20 74 6f 20 61 20 73 69 6e 67 6c file to a singl
ea7a0 65 20 68 6f 73 74 0a 2a 2a 20 20 20 20 20 20 61 e host.** a
ea7b0 74 20 61 20 74 69 6d 65 0a 2a 2a 0a 2a 2a 20 20 t a time.**.**
ea7c0 20 2a 20 20 70 72 6f 78 79 20 66 69 6c 65 20 74 * proxy file t
ea7d0 6f 20 61 63 74 20 61 73 20 61 20 70 72 6f 78 79 o act as a proxy
ea7e0 20 66 6f 72 20 74 68 65 20 61 64 76 69 73 6f 72 for the advisor
ea7f0 79 20 6c 6f 63 6b 73 20 6e 6f 72 6d 61 6c 6c 79 y locks normally
ea800 0a 2a 2a 20 20 20 20 20 20 74 61 6b 65 6e 20 6f .** taken o
ea810 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a n the database.*
ea820 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 63 68 20 66 *.** The conch f
ea830 69 6c 65 20 2d 20 74 6f 20 75 73 65 20 61 20 70 ile - to use a p
ea840 72 6f 78 79 20 66 69 6c 65 2c 20 73 71 6c 69 74 roxy file, sqlit
ea850 65 20 6d 75 73 74 20 66 69 72 73 74 20 22 68 6f e must first "ho
ea860 6c 64 20 74 68 65 20 63 6f 6e 63 68 22 0a 2a 2a ld the conch".**
ea870 20 62 79 20 74 61 6b 69 6e 67 20 61 6e 20 73 71 by taking an sq
ea880 6c 69 74 65 2d 73 74 79 6c 65 20 73 68 61 72 65 lite-style share
ea890 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 63 6f d lock on the co
ea8a0 6e 63 68 20 66 69 6c 65 2c 20 72 65 61 64 69 6e nch file, readin
ea8b0 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 g the.** content
ea8c0 73 20 61 6e 64 20 63 6f 6d 70 61 72 69 6e 67 20 s and comparing
ea8d0 74 68 65 20 68 6f 73 74 27 73 20 75 6e 69 71 75 the host's uniqu
ea8e0 65 20 68 6f 73 74 20 49 44 20 28 73 65 65 20 62 e host ID (see b
ea8f0 65 6c 6f 77 29 20 61 6e 64 20 6c 6f 63 6b 0a 2a elow) and lock.*
ea900 2a 20 70 72 6f 78 79 20 70 61 74 68 20 61 67 61 * proxy path aga
ea910 69 6e 73 74 20 74 68 65 20 76 61 6c 75 65 73 20 inst the values
ea920 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 6f stored in the co
ea930 6e 63 68 2e 20 20 54 68 65 20 63 6f 6e 63 68 20 nch. The conch
ea940 66 69 6c 65 20 69 73 0a 2a 2a 20 73 74 6f 72 65 file is.** store
ea950 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69 d in the same di
ea960 72 65 63 74 6f 72 79 20 61 73 20 74 68 65 20 64 rectory as the d
ea970 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
ea980 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 0a 2a the file name.*
ea990 2a 20 69 73 20 70 61 74 74 65 72 6e 65 64 20 61 * is patterned a
ea9a0 66 74 65 72 20 74 68 65 20 64 61 74 61 62 61 73 fter the databas
ea9b0 65 20 66 69 6c 65 20 6e 61 6d 65 20 61 73 20 22 e file name as "
ea9c0 2e 3c 64 61 74 61 62 61 73 65 6e 61 6d 65 3e 2d .<databasename>-
ea9d0 63 6f 6e 63 68 22 2e 0a 2a 2a 20 49 66 20 74 68 conch"..** If th
ea9e0 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 64 6f 65 e conch file doe
ea9f0 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 6f 72 20 s not exist, or
eaa00 69 74 27 73 20 63 6f 6e 74 65 6e 74 73 20 64 6f it's contents do
eaa10 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 0a 2a not match the.*
eaa20 2a 20 68 6f 73 74 20 49 44 20 61 6e 64 2f 6f 72 * host ID and/or
eaa30 20 70 72 6f 78 79 20 70 61 74 68 2c 20 74 68 65 proxy path, the
eaa40 6e 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 65 73 n the lock is es
eaa50 63 61 6c 61 74 65 64 20 74 6f 20 61 6e 20 65 78 calated to an ex
eaa60 63 6c 75 73 69 76 65 0a 2a 2a 20 6c 6f 63 6b 20 clusive.** lock
eaa70 61 6e 64 20 74 68 65 20 63 6f 6e 63 68 20 66 69 and the conch fi
eaa80 6c 65 20 63 6f 6e 74 65 6e 74 73 20 69 73 20 75 le contents is u
eaa90 70 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20 pdated with the
eaaa0 68 6f 73 74 20 49 44 20 61 6e 64 20 70 72 6f 78 host ID and prox
eaab0 79 0a 2a 2a 20 70 61 74 68 20 61 6e 64 20 74 68 y.** path and th
eaac0 65 20 6c 6f 63 6b 20 69 73 20 64 6f 77 6e 67 72 e lock is downgr
eaad0 61 64 65 64 20 74 6f 20 61 20 73 68 61 72 65 64 aded to a shared
eaae0 20 6c 6f 63 6b 20 61 67 61 69 6e 2e 20 20 49 66 lock again. If
eaaf0 20 74 68 65 20 63 6f 6e 63 68 0a 2a 2a 20 69 73 the conch.** is
eab00 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 held by another
eab10 20 70 72 6f 63 65 73 73 20 28 77 69 74 68 20 61 process (with a
eab20 20 73 68 61 72 65 64 20 6c 6f 63 6b 29 2c 20 74 shared lock), t
eab30 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 he exclusive loc
eab40 6b 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c 20 61 k.** will fail a
eab50 6e 64 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 nd SQLITE_BUSY i
eab60 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
eab70 2a 20 54 68 65 20 70 72 6f 78 79 20 66 69 6c 65 * The proxy file
eab80 20 2d 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 - a single-byte
eab90 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 61 file used for a
eaba0 6c 6c 20 61 64 76 69 73 6f 72 79 20 66 69 6c 65 ll advisory file
eabb0 20 6c 6f 63 6b 73 0a 2a 2a 20 6e 6f 72 6d 61 6c locks.** normal
eabc0 6c 79 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 ly taken on the
eabd0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
eabe0 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 66 6f 72 This allows for
eabf0 20 73 61 66 65 20 73 68 61 72 69 6e 67 0a 2a 2a safe sharing.**
eac00 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
eac10 20 66 69 6c 65 20 66 6f 72 20 6d 75 6c 74 69 70 file for multip
eac20 6c 65 20 72 65 61 64 65 72 73 20 61 6e 64 20 77 le readers and w
eac30 72 69 74 65 72 73 20 6f 6e 20 74 68 65 20 73 61 riters on the sa
eac40 6d 65 0a 2a 2a 20 68 6f 73 74 20 28 74 68 65 20 me.** host (the
eac50 63 6f 6e 63 68 20 65 6e 73 75 72 65 73 20 74 68 conch ensures th
eac60 61 74 20 74 68 65 79 20 61 6c 6c 20 75 73 65 20 at they all use
eac70 74 68 65 20 73 61 6d 65 20 6c 6f 63 61 6c 20 6c the same local l
eac80 6f 63 6b 20 66 69 6c 65 29 2e 0a 2a 2a 0a 2a 2a ock file)..**.**
eac90 20 54 68 65 72 65 20 69 73 20 61 20 74 68 69 72 There is a thir
eaca0 64 20 66 69 6c 65 20 2d 20 74 68 65 20 68 6f 73 d file - the hos
eacb0 74 20 49 44 20 66 69 6c 65 20 2d 20 75 73 65 64 t ID file - used
eacc0 20 61 73 20 61 20 70 65 72 73 69 73 74 65 6e 74 as a persistent
eacd0 20 72 65 63 6f 72 64 0a 2a 2a 20 6f 66 20 61 20 record.** of a
eace0 75 6e 69 71 75 65 20 69 64 65 6e 74 69 66 69 65 unique identifie
eacf0 72 20 66 6f 72 20 74 68 65 20 68 6f 73 74 2c 20 r for the host,
ead00 61 20 31 32 38 2d 62 79 74 65 20 75 6e 69 71 75 a 128-byte uniqu
ead10 65 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a e host id file.*
ead20 2a 20 69 6e 20 74 68 65 20 70 61 74 68 20 64 65 * in the path de
ead30 66 69 6e 65 64 20 62 79 20 74 68 65 20 48 4f 53 fined by the HOS
ead40 54 49 44 50 41 54 48 20 6d 61 63 72 6f 20 28 64 TIDPATH macro (d
ead50 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 0a efault value is.
ead60 2a 2a 20 2f 4c 69 62 72 61 72 79 2f 43 61 63 68 ** /Library/Cach
ead70 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71 es/.com.apple.sq
ead80 6c 69 74 65 43 6f 6e 63 68 48 6f 73 74 49 64 29 liteConchHostId)
ead90 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 65 73 74 69 ..**.** Requesti
eada0 6e 67 20 74 68 65 20 6c 6f 63 6b 20 70 72 6f 78 ng the lock prox
eadb0 79 20 64 6f 65 73 20 6e 6f 74 20 69 6d 6d 65 64 y does not immed
eadc0 69 61 74 65 6c 79 20 74 61 6b 65 20 74 68 65 20 iately take the
eadd0 63 6f 6e 63 68 2c 20 69 74 20 69 73 0a 2a 2a 20 conch, it is.**
eade0 6f 6e 6c 79 20 74 61 6b 65 6e 20 77 68 65 6e 20 only taken when
eadf0 74 68 65 20 66 69 72 73 74 20 72 65 71 75 65 73 the first reques
eae00 74 20 74 6f 20 6c 6f 63 6b 20 64 61 74 61 62 61 t to lock databa
eae10 73 65 20 66 69 6c 65 20 69 73 20 6d 61 64 65 2e se file is made.
eae20 20 20 0a 2a 2a 20 54 68 69 73 20 6d 61 74 63 68 .** This match
eae30 65 73 20 74 68 65 20 73 65 6d 61 6e 74 69 63 73 es the semantics
eae40 20 6f 66 20 74 68 65 20 74 72 61 64 69 74 69 6f of the traditio
eae50 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 62 65 68 61 nal locking beha
eae60 76 69 6f 72 2c 20 77 68 65 72 65 0a 2a 2a 20 6f vior, where.** o
eae70 70 65 6e 69 6e 67 20 61 20 63 6f 6e 6e 65 63 74 pening a connect
eae80 69 6f 6e 20 74 6f 20 61 20 64 61 74 61 62 61 73 ion to a databas
eae90 65 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 e file does not
eaea0 74 61 6b 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 69 take a lock on i
eaeb0 74 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 65 64 t..** The shared
eaec0 20 6c 6f 63 6b 20 61 6e 64 20 61 6e 20 6f 70 65 lock and an ope
eaed0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f n file descripto
eaee0 72 20 61 72 65 20 6d 61 69 6e 74 61 69 6e 65 64 r are maintained
eaef0 20 75 6e 74 69 6c 20 0a 2a 2a 20 74 68 65 20 63 until .** the c
eaf00 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68 65 onnection to the
eaf10 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f database is clo
eaf20 73 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 sed. .**.** The
eaf30 70 72 6f 78 79 20 66 69 6c 65 20 61 6e 64 20 74 proxy file and t
eaf40 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 72 65 he lock file are
eaf50 20 6e 65 76 65 72 20 64 65 6c 65 74 65 64 20 73 never deleted s
eaf60 6f 20 74 68 65 79 20 6f 6e 6c 79 20 6e 65 65 64 o they only need
eaf70 0a 2a 2a 20 74 6f 20 62 65 20 63 72 65 61 74 65 .** to be create
eaf80 64 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 d the first time
eaf90 20 74 68 65 79 20 61 72 65 20 75 73 65 64 2e 0a they are used..
eafa0 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74 **.** Configurat
eafb0 69 6f 6e 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 2d ion options.** -
eafc0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
eafd0 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49 ----.**.** SQLI
eafe0 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f TE_PREFER_PROXY_
eaff0 4c 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20 20 20 LOCKING.**.**
eb000 20 20 20 20 44 61 74 61 62 61 73 65 20 66 69 6c Database fil
eb010 65 73 20 61 63 63 65 73 73 65 64 20 6f 6e 20 6e es accessed on n
eb020 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 on-local file sy
eb030 73 74 65 6d 73 20 61 72 65 0a 2a 2a 20 20 20 20 stems are.**
eb040 20 20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 automatically
eb050 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 configured for
eb060 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2c 20 6c proxy locking, l
eb070 6f 63 6b 20 66 69 6c 65 73 20 61 72 65 0a 2a 2a ock files are.**
eb080 20 20 20 20 20 20 20 6e 61 6d 65 64 20 61 75 74 named aut
eb090 6f 6d 61 74 69 63 61 6c 6c 79 20 75 73 69 6e 67 omatically using
eb0a0 20 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 63 20 the same logic
eb0b0 61 73 0a 2a 2a 20 20 20 20 20 20 20 50 52 41 47 as.** PRAG
eb0c0 4d 41 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 MA lock_proxy_fi
eb0d0 6c 65 3d 22 3a 61 75 74 6f 3a 22 0a 2a 2a 20 20 le=":auto:".**
eb0e0 20 20 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52 .** SQLITE_PR
eb0f0 4f 58 59 5f 44 45 42 55 47 0a 2a 2a 0a 2a 2a 20 OXY_DEBUG.**.**
eb100 20 20 20 20 20 20 45 6e 61 62 6c 65 73 20 74 68 Enables th
eb110 65 20 6c 6f 67 67 69 6e 67 20 6f 66 20 65 72 72 e logging of err
eb120 6f 72 20 6d 65 73 73 61 67 65 73 20 64 75 72 69 or messages duri
eb130 6e 67 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a ng host id file.
eb140 2a 2a 20 20 20 20 20 20 20 72 65 74 72 69 65 76 ** retriev
eb150 61 6c 20 61 6e 64 20 63 72 65 61 74 69 6f 6e 0a al and creation.
eb160 2a 2a 0a 2a 2a 20 20 48 4f 53 54 49 44 50 41 54 **.** HOSTIDPAT
eb170 48 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 H.**.** Ov
eb180 65 72 72 69 64 65 73 20 74 68 65 20 64 65 66 61 errides the defa
eb190 75 6c 74 20 68 6f 73 74 20 49 44 20 66 69 6c 65 ult host ID file
eb1a0 20 70 61 74 68 20 6c 6f 63 61 74 69 6f 6e 0a 2a path location.*
eb1b0 2a 0a 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58 59 44 *.** LOCKPROXYD
eb1c0 49 52 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f IR.**.** O
eb1d0 76 65 72 72 69 64 65 73 20 74 68 65 20 64 65 66 verrides the def
eb1e0 61 75 6c 74 20 64 69 72 65 63 74 6f 72 79 20 75 ault directory u
eb1f0 73 65 64 20 66 6f 72 20 6c 6f 63 6b 20 70 72 6f sed for lock pro
eb200 78 79 20 66 69 6c 65 73 20 74 68 61 74 0a 2a 2a xy files that.**
eb210 20 20 20 20 20 20 20 61 72 65 20 6e 61 6d 65 64 are named
eb220 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 76 automatically v
eb230 69 61 20 74 68 65 20 22 3a 61 75 74 6f 3a 22 20 ia the ":auto:"
eb240 73 65 74 74 69 6e 67 0a 2a 2a 0a 2a 2a 20 20 53 setting.**.** S
eb250 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 QLITE_DEFAULT_PR
eb260 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f OXYDIR_PERMISSIO
eb270 4e 53 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 50 NS.**.** P
eb280 65 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20 75 73 ermissions to us
eb290 65 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20 e when creating
eb2a0 61 20 64 69 72 65 63 74 6f 72 79 20 66 6f 72 20 a directory for
eb2b0 73 74 6f 72 69 6e 67 20 74 68 65 0a 2a 2a 20 20 storing the.**
eb2c0 20 20 20 20 20 6c 6f 63 6b 20 70 72 6f 78 79 20 lock proxy
eb2d0 66 69 6c 65 73 2c 20 6f 6e 6c 79 20 75 73 65 64 files, only used
eb2e0 20 77 68 65 6e 20 4c 4f 43 4b 50 52 4f 58 59 44 when LOCKPROXYD
eb2f0 49 52 20 69 73 20 6e 6f 74 20 73 65 74 2e 0a 2a IR is not set..*
eb300 2a 20 20 20 20 0a 2a 2a 20 20 20 20 0a 2a 2a 20 * .** .**
eb310 41 73 20 6d 65 6e 74 69 6f 6e 65 64 20 61 62 6f As mentioned abo
eb320 76 65 2c 20 77 68 65 6e 20 63 6f 6d 70 69 6c 65 ve, when compile
eb330 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 50 52 d with SQLITE_PR
eb340 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 EFER_PROXY_LOCKI
eb350 4e 47 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 20 74 NG,.** setting t
eb360 68 65 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 he environment v
eb370 61 72 69 61 62 6c 65 20 53 51 4c 49 54 45 5f 46 ariable SQLITE_F
eb380 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 ORCE_PROXY_LOCKI
eb390 4e 47 20 74 6f 20 31 20 77 69 6c 6c 0a 2a 2a 20 NG to 1 will.**
eb3a0 66 6f 72 63 65 20 70 72 6f 78 79 20 6c 6f 63 6b force proxy lock
eb3b0 69 6e 67 20 74 6f 20 62 65 20 75 73 65 64 20 66 ing to be used f
eb3c0 6f 72 20 65 76 65 72 79 20 64 61 74 61 62 61 73 or every databas
eb3d0 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c 20 61 e file opened, a
eb3e0 6e 64 20 30 0a 2a 2a 20 77 69 6c 6c 20 66 6f 72 nd 0.** will for
eb3f0 63 65 20 61 75 74 6f 6d 61 74 69 63 20 70 72 6f ce automatic pro
eb400 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65 xy locking to be
eb410 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 61 6c disabled for al
eb420 6c 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 l database.** fi
eb430 6c 65 73 20 28 65 78 70 6c 69 63 69 74 79 20 63 les (explicity c
eb440 61 6c 6c 69 6e 67 20 74 68 65 20 53 51 4c 49 54 alling the SQLIT
eb450 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 E_SET_LOCKPROXYF
eb460 49 4c 45 20 70 72 61 67 6d 61 20 6f 72 0a 2a 2a ILE pragma or.**
eb470 20 73 71 6c 69 74 65 5f 66 69 6c 65 5f 63 6f 6e sqlite_file_con
eb480 74 72 6f 6c 20 41 50 49 20 69 73 20 6e 6f 74 20 trol API is not
eb490 61 66 66 65 63 74 65 64 20 62 79 20 53 51 4c 49 affected by SQLI
eb4a0 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c TE_FORCE_PROXY_L
eb4b0 4f 43 4b 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f 2a 0a OCKING)..*/../*.
eb4c0 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 ** Proxy locking
eb4d0 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 is only availab
eb4e0 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 20 0a 2a 2f le on MacOSX .*/
eb4f0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 .#if defined(__A
eb500 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 PPLE__) && SQLIT
eb510 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
eb520 5f 53 54 59 4c 45 0a 0a 23 69 66 64 65 66 20 53 _STYLE..#ifdef S
eb530 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20 73 69 QLITE_TEST./* si
eb540 6d 75 6c 61 74 65 20 6d 75 6c 74 69 70 6c 65 20 mulate multiple
eb550 68 6f 73 74 73 20 62 79 20 63 72 65 61 74 69 6e hosts by creatin
eb560 67 20 75 6e 69 71 75 65 20 68 6f 73 74 69 64 20 g unique hostid
eb570 66 69 6c 65 20 70 61 74 68 73 20 2a 2f 0a 53 51 file paths */.SQ
eb580 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
eb590 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 20 ite3_hostid_num
eb5a0 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a = 0;.#endif../*.
eb5b0 2a 2a 20 54 68 65 20 70 72 6f 78 79 4c 6f 63 6b ** The proxyLock
eb5c0 69 6e 67 43 6f 6e 74 65 78 74 20 68 61 73 20 74 ingContext has t
eb5d0 68 65 20 70 61 74 68 20 61 6e 64 20 66 69 6c 65 he path and file
eb5e0 20 73 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 structures for
eb5f0 74 68 65 20 72 65 6d 6f 74 65 20 0a 2a 2a 20 61 the remote .** a
eb600 6e 64 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 66 nd local proxy f
eb610 69 6c 65 73 20 69 6e 20 69 74 0a 2a 2f 0a 74 79 iles in it.*/.ty
eb620 70 65 64 65 66 20 73 74 72 75 63 74 20 70 72 6f pedef struct pro
eb630 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
eb640 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
eb650 74 65 78 74 3b 0a 73 74 72 75 63 74 20 70 72 6f text;.struct pro
eb660 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
eb670 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 {. unixFile *c
eb680 6f 6e 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20 onchFile;
eb690 20 20 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63 68 20 /* Open conch
eb6a0 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a file */. char *
eb6b0 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 3b 20 20 conchFilePath;
eb6c0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
eb6d0 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 f the conch file
eb6e0 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a */. unixFile *
eb6f0 6c 6f 63 6b 50 72 6f 78 79 3b 20 20 20 20 20 20 lockProxy;
eb700 20 20 20 2f 2a 20 4f 70 65 6e 20 70 72 6f 78 79 /* Open proxy
eb710 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 lock file */.
eb720 63 68 61 72 20 2a 6c 6f 63 6b 50 72 6f 78 79 50 char *lockProxyP
eb730 61 74 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ath; /*
eb740 4e 61 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 78 Name of the prox
eb750 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 y lock file */.
eb760 20 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 char *dbPath;
eb770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
eb780 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 Name of the ope
eb790 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 n file */. int
eb7a0 63 6f 6e 63 68 48 65 6c 64 3b 20 20 20 20 20 20 conchHeld;
eb7b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
eb7c0 20 69 66 20 74 68 65 20 63 6f 6e 63 68 20 69 73 if the conch is
eb7d0 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 currently held
eb7e0 2a 2f 0a 20 20 76 6f 69 64 20 2a 6f 6c 64 4c 6f */. void *oldLo
eb7f0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 ckingContext;
eb800 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6c 6f /* Original lo
eb810 63 6b 69 6e 67 63 6f 6e 74 65 78 74 20 74 6f 20 ckingcontext to
eb820 72 65 73 74 6f 72 65 20 6f 6e 20 63 6c 6f 73 65 restore on close
eb830 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f */. sqlite3_io
eb840 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a _methods const *
eb850 70 4f 6c 64 4d 65 74 68 6f 64 3b 20 20 20 20 20 pOldMethod;
eb860 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 49 2f 4f 20 /* Original I/O
eb870 6d 65 74 68 6f 64 73 20 66 6f 72 20 63 6c 6f 73 methods for clos
eb880 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f 53 54 e */.};../* HOST
eb890 49 44 4c 45 4e 20 61 6e 64 20 43 4f 4e 43 48 4c IDLEN and CONCHL
eb8a0 45 4e 20 62 6f 74 68 20 69 6e 63 6c 75 64 65 20 EN both include
eb8b0 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 73 74 space for the st
eb8c0 72 69 6e 67 20 0a 2a 2a 20 74 65 72 6d 69 6e 61 ring .** termina
eb8d0 74 69 6e 67 20 6e 75 6c 20 0a 2a 2f 0a 23 64 65 ting nul .*/.#de
eb8e0 66 69 6e 65 20 48 4f 53 54 49 44 4c 45 4e 20 20 fine HOSTIDLEN
eb8f0 20 20 20 20 20 20 20 31 32 38 0a 23 64 65 66 69 128.#defi
eb900 6e 65 20 43 4f 4e 43 48 4c 45 4e 20 20 20 20 20 ne CONCHLEN
eb910 20 20 20 20 20 28 4d 41 58 50 41 54 48 4c 45 4e (MAXPATHLEN
eb920 2b 48 4f 53 54 49 44 4c 45 4e 2b 31 29 0a 23 69 +HOSTIDLEN+1).#i
eb930 66 6e 64 65 66 20 48 4f 53 54 49 44 50 41 54 48 fndef HOSTIDPATH
eb940 0a 23 20 64 65 66 69 6e 65 20 48 4f 53 54 49 44 .# define HOSTID
eb950 50 41 54 48 20 20 20 20 20 20 20 22 2f 4c 69 62 PATH "/Lib
eb960 72 61 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d rary/Caches/.com
eb970 2e 61 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e .apple.sqliteCon
eb980 63 68 48 6f 73 74 49 64 22 0a 23 65 6e 64 69 66 chHostId".#endif
eb990 0a 0a 2f 2a 20 62 61 73 69 63 61 6c 6c 79 20 61 ../* basically a
eb9a0 20 63 6f 70 79 20 6f 66 20 75 6e 69 78 52 61 6e copy of unixRan
eb9b0 64 6f 6d 6e 65 73 73 20 77 69 74 68 20 64 69 66 domness with dif
eb9c0 66 65 72 65 6e 74 0a 2a 2a 20 74 65 73 74 20 62 ferent.** test b
eb9d0 65 68 61 76 69 6f 72 20 62 75 69 6c 74 20 69 6e ehavior built in
eb9e0 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 */.static int p
eb9f0 72 6f 78 79 47 65 6e 65 72 61 74 65 48 6f 73 74 roxyGenerateHost
eba00 49 44 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44 ID(char *pHostID
eba10 29 7b 0a 20 20 69 6e 74 20 70 69 64 2c 20 66 64 ){. int pid, fd
eba20 2c 20 6c 65 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 , len;. unsigne
eba30 64 20 63 68 61 72 20 2a 6b 65 79 20 3d 20 28 75 d char *key = (u
eba40 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 nsigned char *)p
eba50 48 6f 73 74 49 44 3b 0a 20 20 0a 20 20 6d 65 6d HostID;. . mem
eba60 73 65 74 28 6b 65 79 2c 20 30 2c 20 48 4f 53 54 set(key, 0, HOST
eba70 49 44 4c 45 4e 29 3b 0a 20 20 6c 65 6e 20 3d 20 IDLEN);. len =
eba80 30 3b 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 22 0;. fd = open("
eba90 2f 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f /dev/urandom", O
ebaa0 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 69 66 28 20 _RDONLY);. if(
ebab0 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 6c 65 6e fd>=0 ){. len
ebac0 20 3d 20 72 65 61 64 28 66 64 2c 20 6b 65 79 2c = read(fd, key,
ebad0 20 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20 20 HOSTIDLEN);.
ebae0 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 close(fd); /* s
ebaf0 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 ilently leak the
ebb00 20 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20 fd if it fails
ebb10 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 6c 65 6e */. }. if( len
ebb20 20 3c 20 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a < HOSTIDLEN ){.
ebb30 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 time_t t;.
ebb40 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 time(&t);.
ebb50 6d 65 6d 63 70 79 28 6b 65 79 2c 20 26 74 2c 20 memcpy(key, &t,
ebb60 73 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 sizeof(t));.
ebb70 70 69 64 20 3d 20 67 65 74 70 69 64 28 29 3b 0a pid = getpid();.
ebb80 20 20 20 20 6d 65 6d 63 70 79 28 26 6b 65 79 5b memcpy(&key[
ebb90 73 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 sizeof(t)], &pid
ebba0 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a , sizeof(pid));.
ebbb0 20 20 7d 0a 20 20 0a 23 69 66 64 65 66 20 4d 41 }. .#ifdef MA
ebbc0 4b 45 5f 50 52 45 54 54 59 5f 48 4f 53 54 49 44 KE_PRETTY_HOSTID
ebbd0 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a . {. int i;.
ebbe0 20 20 20 20 2f 2a 20 66 69 6c 74 65 72 20 74 68 /* filter th
ebbf0 65 20 62 79 74 65 73 20 69 6e 74 6f 20 70 72 69 e bytes into pri
ebc00 6e 74 61 62 6c 65 20 61 73 63 69 69 20 63 68 61 ntable ascii cha
ebc10 72 61 63 74 65 72 73 20 61 6e 64 20 4e 55 4c 20 racters and NUL
ebc20 74 65 72 6d 69 6e 61 74 65 20 2a 2f 0a 20 20 20 terminate */.
ebc30 20 6b 65 79 5b 28 48 4f 53 54 49 44 4c 45 4e 2d key[(HOSTIDLEN-
ebc40 31 29 5d 20 3d 20 30 78 30 30 3b 0a 20 20 20 20 1)] = 0x00;.
ebc50 66 6f 72 28 20 69 3d 30 3b 20 69 3c 28 48 4f 53 for( i=0; i<(HOS
ebc60 54 49 44 4c 45 4e 2d 31 29 3b 20 69 2b 2b 20 29 TIDLEN-1); i++ )
ebc70 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 {. unsigned
ebc80 20 63 68 61 72 20 70 61 20 3d 20 6b 65 79 5b 69 char pa = key[i
ebc90 5d 26 30 78 37 46 3b 0a 20 20 20 20 20 20 69 66 ]&0x7F;. if
ebca0 28 20 70 61 3c 30 78 32 30 20 29 7b 0a 20 20 20 ( pa<0x20 ){.
ebcb0 20 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28 6b key[i] = (k
ebcc0 65 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30 78 ey[i]&0x80 == 0x
ebcd0 38 30 29 20 3f 20 70 61 2b 30 78 34 30 20 3a 20 80) ? pa+0x40 :
ebce0 70 61 2b 30 78 32 30 3b 0a 20 20 20 20 20 20 7d pa+0x20;. }
ebcf0 65 6c 73 65 20 69 66 28 20 70 61 3d 3d 30 78 37 else if( pa==0x7
ebd00 46 20 29 7b 0a 20 20 20 20 20 20 20 20 6b 65 79 F ){. key
ebd10 5b 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30 78 [i] = (key[i]&0x
ebd20 38 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 70 61 80 == 0x80) ? pa
ebd30 3d 30 78 32 30 20 3a 20 70 61 2b 30 78 37 45 3b =0x20 : pa+0x7E;
ebd40 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
ebd50 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 }.#endif. retu
ebd60 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
ebd70 0a 2f 2a 20 77 72 69 74 65 73 20 74 68 65 20 68 ./* writes the h
ebd80 6f 73 74 20 69 64 20 70 61 74 68 20 74 6f 20 70 ost id path to p
ebd90 61 74 68 2c 20 70 61 74 68 20 73 68 6f 75 6c 64 ath, path should
ebda0 20 62 65 20 61 6e 20 70 72 65 2d 61 6c 6c 6f 63 be an pre-alloc
ebdb0 61 74 65 64 20 62 75 66 66 65 72 0a 2a 2a 20 77 ated buffer.** w
ebdc0 69 74 68 20 65 6e 6f 75 67 68 20 73 70 61 63 65 ith enough space
ebdd0 20 66 6f 72 20 61 20 70 61 74 68 20 0a 2a 2f 0a for a path .*/.
ebde0 73 74 61 74 69 63 20 76 6f 69 64 20 70 72 6f 78 static void prox
ebdf0 79 47 65 74 48 6f 73 74 49 44 50 61 74 68 28 63 yGetHostIDPath(c
ebe00 68 61 72 20 2a 70 61 74 68 2c 20 73 69 7a 65 5f har *path, size_
ebe10 74 20 6c 65 6e 29 7b 0a 20 20 73 74 72 6c 63 70 t len){. strlcp
ebe20 79 28 70 61 74 68 2c 20 48 4f 53 54 49 44 50 41 y(path, HOSTIDPA
ebe30 54 48 2c 20 6c 65 6e 29 3b 0a 23 69 66 64 65 66 TH, len);.#ifdef
ebe40 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 SQLITE_TEST. i
ebe50 66 28 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 f( sqlite3_hosti
ebe60 64 5f 6e 75 6d 3e 30 20 29 7b 0a 20 20 20 20 63 d_num>0 ){. c
ebe70 68 61 72 20 73 75 66 66 69 78 5b 32 5d 20 3d 20 har suffix[2] =
ebe80 22 31 22 3b 0a 20 20 20 20 73 75 66 66 69 78 5b "1";. suffix[
ebe90 30 5d 20 3d 20 73 75 66 66 69 78 5b 30 5d 20 2b 0] = suffix[0] +
ebea0 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f sqlite3_hostid_
ebeb0 6e 75 6d 3b 0a 20 20 20 20 73 74 72 6c 63 61 74 num;. strlcat
ebec0 28 70 61 74 68 2c 20 73 75 66 66 69 78 2c 20 6c (path, suffix, l
ebed0 65 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a en);. }.#endif.
ebee0 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 48 OSTRACE3("GETH
ebef0 4f 53 54 49 44 50 41 54 48 20 20 25 73 20 70 69 OSTIDPATH %s pi
ebf00 64 3d 25 64 5c 6e 22 2c 20 70 61 74 68 2c 20 67 d=%d\n", path, g
ebf10 65 74 70 69 64 28 29 29 3b 0a 7d 0a 0a 2f 2a 20 etpid());.}../*
ebf20 67 65 74 20 74 68 65 20 68 6f 73 74 20 49 44 20 get the host ID
ebf30 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 20 68 6f from a sqlite ho
ebf40 73 74 69 64 20 66 69 6c 65 20 73 74 6f 72 65 64 stid file stored
ebf50 20 69 6e 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 in the .** user
ebf60 2d 73 70 65 63 69 66 69 63 20 74 6d 70 20 64 69 -specific tmp di
ebf70 72 65 63 74 6f 72 79 2c 20 63 72 65 61 74 65 20 rectory, create
ebf80 74 68 65 20 49 44 20 69 66 20 69 74 27 73 20 6e the ID if it's n
ebf90 6f 74 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 ot there already
ebfa0 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 .*/.static int
ebfb0 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44 28 63 proxyGetHostID(c
ebfc0 68 61 72 20 2a 70 48 6f 73 74 49 44 2c 20 69 6e har *pHostID, in
ebfd0 74 20 2a 70 45 72 72 6f 72 29 7b 0a 20 20 69 6e t *pError){. in
ebfe0 74 20 66 64 3b 0a 20 20 63 68 61 72 20 70 61 74 t fd;. char pat
ebff0 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 20 0a h[MAXPATHLEN]; .
ec000 20 20 73 69 7a 65 5f 74 20 6c 65 6e 3b 0a 20 20 size_t len;.
ec010 69 6e 74 20 72 63 3d 53 51 4c 49 54 45 5f 4f 4b int rc=SQLITE_OK
ec020 3b 0a 0a 20 20 70 72 6f 78 79 47 65 74 48 6f 73 ;.. proxyGetHos
ec030 74 49 44 50 61 74 68 28 70 61 74 68 2c 20 4d 41 tIDPath(path, MA
ec040 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 2f 2a 20 XPATHLEN);. /*
ec050 74 72 79 20 74 6f 20 63 72 65 61 74 65 20 74 68 try to create th
ec060 65 20 68 6f 73 74 20 49 44 20 66 69 6c 65 2c 20 e host ID file,
ec070 69 66 20 69 74 20 61 6c 72 65 61 64 79 20 65 78 if it already ex
ec080 69 73 74 73 20 72 65 61 64 20 74 68 65 20 63 6f ists read the co
ec090 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 66 64 20 3d ntents */. fd =
ec0a0 20 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f 43 52 open(path, O_CR
ec0b0 45 41 54 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f 5f 45 EAT|O_WRONLY|O_E
ec0c0 58 43 4c 2c 20 30 36 34 34 29 3b 0a 20 20 69 66 XCL, 0644);. if
ec0d0 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 69 6e ( fd<0 ){. in
ec0e0 74 20 65 72 72 3d 65 72 72 6e 6f 3b 0a 09 09 0a t err=errno;....
ec0f0 20 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 58 if( err!=EEX
ec100 49 53 54 20 29 7b 0a 23 69 66 64 65 66 20 53 51 IST ){.#ifdef SQ
ec110 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 LITE_PROXY_DEBUG
ec120 20 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 /* set the sqli
ec130 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 te error message
ec140 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 instead */.
ec150 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 fprintf(stderr
ec160 2c 20 22 73 71 6c 69 74 65 20 65 72 72 6f 72 20 , "sqlite error
ec170 63 72 65 61 74 69 6e 67 20 68 6f 73 74 20 49 44 creating host ID
ec180 20 66 69 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c file %s: %s\n",
ec190 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 . p
ec1a0 61 74 68 2c 20 73 74 72 65 72 72 6f 72 28 65 72 ath, strerror(er
ec1b0 72 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 r));.#endif.
ec1c0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
ec1d0 50 45 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 PERM;. }.
ec1e0 2f 2a 20 63 6f 75 6c 64 6e 27 74 20 63 72 65 61 /* couldn't crea
ec1f0 74 65 20 74 68 65 20 66 69 6c 65 2c 20 72 65 61 te the file, rea
ec200 64 20 69 74 20 69 6e 73 74 65 61 64 20 2a 2f 0a d it instead */.
ec210 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 fd = open(pa
ec220 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 45 th, O_RDONLY|O_E
ec230 58 43 4c 29 3b 0a 20 20 20 20 69 66 28 20 66 64 XCL);. if( fd
ec240 3c 30 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c <0 ){.#ifdef SQL
ec250 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 ITE_PROXY_DEBUG
ec260 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 /* set the sqlit
ec270 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
ec280 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 instead */.
ec290 20 69 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f int err = errno
ec2a0 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 ;. fprintf(
ec2b0 73 74 64 65 72 72 2c 20 22 73 71 6c 69 74 65 20 stderr, "sqlite
ec2c0 65 72 72 6f 72 20 6f 70 65 6e 69 6e 67 20 68 6f error opening ho
ec2d0 73 74 20 49 44 20 66 69 6c 65 20 25 73 3a 20 25 st ID file %s: %
ec2e0 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 s\n",.
ec2f0 20 20 20 20 70 61 74 68 2c 20 73 74 72 65 72 72 path, strerr
ec300 6f 72 28 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 or(err));.#endif
ec310 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
ec320 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d LITE_PERM;. }
ec330 0a 20 20 20 20 6c 65 6e 20 3d 20 70 72 65 61 64 . len = pread
ec340 28 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 48 4f (fd, pHostID, HO
ec350 53 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 STIDLEN, 0);.
ec360 20 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 if( len<0 ){.
ec370 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 65 72 *pError = er
ec380 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 rno;. rc =
ec390 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 SQLITE_IOERR_REA
ec3a0 44 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 D;. }else if(
ec3b0 20 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29 len<HOSTIDLEN )
ec3c0 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20 {. *pError
ec3d0 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 0;. rc =
ec3e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f SQLITE_IOERR_SHO
ec3f0 52 54 5f 52 45 41 44 3b 0a 20 20 20 20 7d 0a 20 RT_READ;. }.
ec400 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a close(fd); /*
ec410 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 silently leak t
ec420 68 65 20 66 64 20 69 66 20 69 74 20 66 61 69 6c he fd if it fail
ec430 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 s */. OSTRACE
ec440 33 28 22 47 45 54 48 4f 53 54 49 44 20 20 72 65 3("GETHOSTID re
ec450 61 64 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c ad %s pid=%d\n",
ec460 20 70 48 6f 73 74 49 44 2c 20 67 65 74 70 69 64 pHostID, getpid
ec470 28 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ());. return
ec480 72 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 rc;. }else{.
ec490 20 2f 2a 20 77 65 27 72 65 20 63 72 65 61 74 69 /* we're creati
ec4a0 6e 67 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 ng the host ID f
ec4b0 69 6c 65 20 28 75 73 65 20 61 20 72 61 6e 64 6f ile (use a rando
ec4c0 6d 20 73 74 72 69 6e 67 20 6f 66 20 62 79 74 65 m string of byte
ec4d0 73 29 20 2a 2f 0a 20 20 20 20 70 72 6f 78 79 47 s) */. proxyG
ec4e0 65 6e 65 72 61 74 65 48 6f 73 74 49 44 28 70 48 enerateHostID(pH
ec4f0 6f 73 74 49 44 29 3b 0a 20 20 20 20 6c 65 6e 20 ostID);. len
ec500 3d 20 70 77 72 69 74 65 28 66 64 2c 20 70 48 6f = pwrite(fd, pHo
ec510 73 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e 2c stID, HOSTIDLEN,
ec520 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 65 6e 0);. if( len
ec530 3c 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 <0 ){. *pEr
ec540 72 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 ror = errno;.
ec550 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 rc = SQLITE_I
ec560 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 OERR_WRITE;.
ec570 7d 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f }else if( len<HO
ec580 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20 STIDLEN ){.
ec590 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 *pError = 0;.
ec5a0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
ec5b0 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 IOERR_WRITE;.
ec5c0 20 7d 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29 }. close(fd)
ec5d0 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 ; /* silently le
ec5e0 61 6b 20 74 68 65 20 66 64 20 69 66 20 69 74 20 ak the fd if it
ec5f0 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54 fails */. OST
ec600 52 41 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 RACE3("GETHOSTID
ec610 20 20 77 72 6f 74 65 20 25 73 20 70 69 64 3d 25 wrote %s pid=%
ec620 64 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 d\n", pHostID, g
ec630 65 74 70 69 64 28 29 29 3b 0a 20 20 20 20 72 65 etpid());. re
ec640 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a turn rc;. }.}..
ec650 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
ec660 47 65 74 4c 6f 63 6b 50 61 74 68 28 63 6f 6e 73 GetLockPath(cons
ec670 74 20 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20 t char *dbPath,
ec680 63 68 61 72 20 2a 6c 50 61 74 68 2c 20 73 69 7a char *lPath, siz
ec690 65 5f 74 20 6d 61 78 4c 65 6e 29 7b 0a 20 20 69 e_t maxLen){. i
ec6a0 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 64 62 nt len;. int db
ec6b0 4c 65 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 23 Len;. int i;..#
ec6c0 69 66 64 65 66 20 4c 4f 43 4b 50 52 4f 58 59 44 ifdef LOCKPROXYD
ec6d0 49 52 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 IR. len = strlc
ec6e0 70 79 28 6c 50 61 74 68 2c 20 4c 4f 43 4b 50 52 py(lPath, LOCKPR
ec6f0 4f 58 59 44 49 52 2c 20 6d 61 78 4c 65 6e 29 3b OXYDIR, maxLen);
ec700 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 5f .#else.# ifdef _
ec710 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 CS_DARWIN_USER_T
ec720 45 4d 50 5f 44 49 52 0a 20 20 7b 0a 20 20 20 20 EMP_DIR. {.
ec730 63 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 57 confstr(_CS_DARW
ec740 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 IN_USER_TEMP_DIR
ec750 2c 20 6c 50 61 74 68 2c 20 6d 61 78 4c 65 6e 29 , lPath, maxLen)
ec760 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c ;. len = strl
ec770 63 61 74 28 6c 50 61 74 68 2c 20 22 73 71 6c 69 cat(lPath, "sqli
ec780 74 65 70 6c 6f 63 6b 73 22 2c 20 6d 61 78 4c 65 teplocks", maxLe
ec790 6e 29 3b 0a 20 20 20 20 69 66 28 20 6d 6b 64 69 n);. if( mkdi
ec7a0 72 28 6c 50 61 74 68 2c 20 53 51 4c 49 54 45 5f r(lPath, SQLITE_
ec7b0 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 DEFAULT_PROXYDIR
ec7c0 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 20 29 7b _PERMISSIONS) ){
ec7d0 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 6d 6b 64 . /* if mkd
ec7e0 69 72 20 66 61 69 6c 73 2c 20 68 61 6e 64 6c 65 ir fails, handle
ec7f0 20 61 73 20 6c 6f 63 6b 20 66 69 6c 65 20 63 72 as lock file cr
ec800 65 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 2a eation failure *
ec810 2f 0a 23 20 20 69 66 64 65 66 20 53 51 4c 49 54 /.# ifdef SQLIT
ec820 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 6e E_DEBUG. in
ec830 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 t err = errno;.
ec840 20 20 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 if( err!=EE
ec850 58 49 53 54 20 29 7b 0a 20 20 20 20 20 20 20 20 XIST ){.
ec860 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 fprintf(stderr,
ec870 22 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74 "proxyGetLockPat
ec880 68 3a 20 6d 6b 64 69 72 28 25 73 2c 30 25 6f 29 h: mkdir(%s,0%o)
ec890 20 65 72 72 6f 72 20 25 64 20 25 73 5c 6e 22 2c error %d %s\n",
ec8a0 20 6c 50 61 74 68 2c 0a 20 20 20 20 20 20 20 20 lPath,.
ec8b0 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 SQLITE_D
ec8c0 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f EFAULT_PROXYDIR_
ec8d0 50 45 52 4d 49 53 53 49 4f 4e 53 2c 20 65 72 72 PERMISSIONS, err
ec8e0 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 29 29 , strerror(err))
ec8f0 3b 0a 20 20 20 20 20 20 7d 0a 23 20 20 65 6e 64 ;. }.# end
ec900 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 if. }else{.
ec910 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 OSTRACE3("GE
ec920 54 4c 4f 43 4b 50 41 54 48 20 20 6d 6b 64 69 72 TLOCKPATH mkdir
ec930 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 6c %s pid=%d\n", l
ec940 50 61 74 68 2c 20 67 65 74 70 69 64 28 29 29 3b Path, getpid());
ec950 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 7d 0a . }. . }.
ec960 23 20 65 6c 73 65 0a 20 20 6c 65 6e 20 3d 20 73 # else. len = s
ec970 74 72 6c 63 70 79 28 6c 50 61 74 68 2c 20 22 2f trlcpy(lPath, "/
ec980 74 6d 70 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a tmp/", maxLen);.
ec990 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a # endif.#endif..
ec9a0 20 20 69 66 28 20 6c 50 61 74 68 5b 6c 65 6e 2d if( lPath[len-
ec9b0 31 5d 21 3d 27 2f 27 20 29 7b 0a 20 20 20 20 6c 1]!='/' ){. l
ec9c0 65 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 en = strlcat(lPa
ec9d0 74 68 2c 20 22 2f 22 2c 20 6d 61 78 4c 65 6e 29 th, "/", maxLen)
ec9e0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 74 72 ;. }. . /* tr
ec9f0 61 6e 73 66 6f 72 6d 20 74 68 65 20 64 62 20 70 ansform the db p
eca00 61 74 68 20 74 6f 20 61 20 75 6e 69 71 75 65 20 ath to a unique
eca10 63 61 63 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 cache name */.
eca20 64 62 4c 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 dbLen = (int)str
eca30 6c 65 6e 28 64 62 50 61 74 68 29 3b 0a 20 20 66 len(dbPath);. f
eca40 6f 72 28 20 69 3d 30 3b 20 69 3c 64 62 4c 65 6e or( i=0; i<dbLen
eca50 20 26 26 20 28 69 2b 6c 65 6e 2b 37 29 3c 6d 61 && (i+len+7)<ma
eca60 78 4c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 xLen; i++){.
eca70 63 68 61 72 20 63 20 3d 20 64 62 50 61 74 68 5b char c = dbPath[
eca80 69 5d 3b 0a 20 20 20 20 6c 50 61 74 68 5b 69 2b i];. lPath[i+
eca90 6c 65 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27 29 3f len] = (c=='/')?
ecaa0 27 5f 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c 50 61 '_':c;. }. lPa
ecab0 74 68 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27 3b 0a th[i+len]='\0';.
ecac0 20 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c strlcat(lPath,
ecad0 20 22 3a 61 75 74 6f 3a 22 2c 20 6d 61 78 4c 65 ":auto:", maxLe
ecae0 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c n);. return SQL
ecaf0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
ecb00 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 56 46 Create a new VF
ecb10 53 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f S file descripto
ecb20 72 20 28 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d r (stored in mem
ecb30 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
ecb40 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c m.** sqlite3_mal
ecb50 6c 6f 63 29 20 61 6e 64 20 6f 70 65 6e 20 74 68 loc) and open th
ecb60 65 20 66 69 6c 65 20 6e 61 6d 65 64 20 22 70 61 e file named "pa
ecb70 74 68 22 20 69 6e 20 74 68 65 20 66 69 6c 65 20 th" in the file
ecb80 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2a 0a 2a descriptor..**.*
ecb90 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 * The caller is
ecba0 72 65 73 70 6f 6e 73 69 62 6c 65 20 6e 6f 74 20 responsible not
ecbb0 6f 6e 6c 79 20 66 6f 72 20 63 6c 6f 73 69 6e 67 only for closing
ecbc0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
ecbd0 70 74 6f 72 0a 2a 2a 20 62 75 74 20 61 6c 73 6f ptor.** but also
ecbe0 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65 for freeing the
ecbf0 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 memory associat
ecc00 65 64 20 77 69 74 68 20 74 68 65 20 66 69 6c 65 ed with the file
ecc10 20 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2f 0a descriptor..*/.
ecc20 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
ecc30 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28 63 CreateUnixFile(c
ecc40 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c onst char *path,
ecc50 20 75 6e 69 78 46 69 6c 65 20 2a 2a 70 70 46 69 unixFile **ppFi
ecc60 6c 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 le) {. unixFile
ecc70 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 66 6c *pNew;. int fl
ecc80 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 ags = SQLITE_OPE
ecc90 4e 5f 4d 41 49 4e 5f 44 42 7c 53 51 4c 49 54 45 N_MAIN_DB|SQLITE
ecca0 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c _OPEN_CREATE|SQL
eccb0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
eccc0 54 45 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 TE;. int rc = S
eccd0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 QLITE_OK;. sqli
ecce0 74 65 33 5f 76 66 73 20 64 75 6d 6d 79 56 66 73 te3_vfs dummyVfs
eccf0 3b 0a 0a 20 20 70 4e 65 77 20 3d 20 28 75 6e 69 ;.. pNew = (uni
ecd00 78 46 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 5f xFile *)sqlite3_
ecd10 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 75 6e malloc(sizeof(un
ecd20 69 78 46 69 6c 65 29 29 3b 0a 20 20 69 66 28 20 ixFile));. if(
ecd30 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 72 65 74 !pNew ){. ret
ecd40 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
ecd50 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 ;. }. memset(p
ecd60 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 New, 0, sizeof(u
ecd70 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20 2f 2a nixFile));.. /*
ecd80 20 43 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 28 29 Call unixOpen()
ecd90 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 70 72 6f to open the pro
ecda0 78 79 20 66 69 6c 65 2e 20 54 68 65 20 66 6c 61 xy file. The fla
ecdb0 67 73 20 70 61 73 73 65 64 20 74 6f 20 75 6e 69 gs passed to uni
ecdc0 78 4f 70 65 6e 28 29 0a 20 20 2a 2a 20 73 75 67 xOpen(). ** sug
ecdd0 67 65 73 74 20 74 68 61 74 20 74 68 65 20 66 69 gest that the fi
ecde0 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 le being opened
ecdf0 69 73 20 61 20 22 6d 61 69 6e 20 64 61 74 61 62 is a "main datab
ece00 61 73 65 22 2e 20 54 68 69 73 20 69 73 0a 20 20 ase". This is.
ece10 2a 2a 20 6e 65 63 65 73 73 61 72 79 20 61 73 20 ** necessary as
ece20 6f 74 68 65 72 20 66 69 6c 65 20 74 79 70 65 73 other file types
ece30 20 64 6f 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 do not necessar
ece40 69 6c 79 20 73 75 70 70 6f 72 74 20 6c 6f 63 6b ily support lock
ece50 69 6e 67 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20 ing. It. ** is
ece60 62 65 74 74 65 72 20 74 6f 20 75 73 65 20 75 6e better to use un
ece70 69 78 4f 70 65 6e 28 29 20 69 6e 73 74 65 61 64 ixOpen() instead
ece80 20 6f 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 of opening the
ece90 66 69 6c 65 20 64 69 72 65 63 74 6c 79 20 77 69 file directly wi
ecea0 74 68 0a 20 20 2a 2a 20 6f 70 65 6e 28 29 2c 20 th. ** open(),
eceb0 61 73 20 75 6e 69 78 4f 70 65 6e 28 29 20 73 65 as unixOpen() se
ecec0 74 73 20 75 70 20 74 68 65 20 76 61 72 69 6f 75 ts up the variou
eced0 73 20 6d 65 63 68 61 6e 69 73 6d 73 20 72 65 71 s mechanisms req
ecee0 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a 20 6d 61 uired to. ** ma
ecef0 6b 65 20 73 75 72 65 20 61 20 63 61 6c 6c 20 74 ke sure a call t
ecf00 6f 20 63 6c 6f 73 65 28 29 20 64 6f 65 73 20 6e o close() does n
ecf10 6f 74 20 63 61 75 73 65 20 74 68 65 20 73 79 73 ot cause the sys
ecf20 74 65 6d 20 74 6f 20 64 69 73 63 61 72 64 0a 20 tem to discard.
ecf30 20 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 ** POSIX locks
ecf40 70 72 65 6d 61 74 75 72 65 6c 79 2e 0a 20 20 2a prematurely.. *
ecf50 2a 0a 20 20 2a 2a 20 49 74 20 69 73 20 69 6d 70 *. ** It is imp
ecf60 6f 72 74 61 6e 74 20 74 68 61 74 20 74 68 65 20 ortant that the
ecf70 78 4f 70 65 6e 20 6d 65 6d 62 65 72 20 6f 66 20 xOpen member of
ecf80 74 68 65 20 56 46 53 20 6f 62 6a 65 63 74 20 70 the VFS object p
ecf90 61 73 73 65 64 20 74 6f 20 0a 20 20 2a 2a 20 75 assed to . ** u
ecfa0 6e 69 78 4f 70 65 6e 28 29 20 69 73 20 4e 55 4c nixOpen() is NUL
ecfb0 4c 2e 20 54 68 69 73 20 74 65 6c 6c 73 20 75 6e L. This tells un
ecfc0 69 78 4f 70 65 6e 28 29 20 6d 61 79 20 74 72 79 ixOpen() may try
ecfd0 20 74 6f 20 6f 70 65 6e 20 61 20 70 72 6f 78 79 to open a proxy
ecfe0 2d 66 69 6c 65 20 0a 20 20 2a 2a 20 66 6f 72 20 -file . ** for
ecff0 74 68 65 20 70 72 6f 78 79 2d 66 69 6c 65 20 28 the proxy-file (
ed000 63 72 65 61 74 69 6e 67 20 61 20 70 6f 74 65 6e creating a poten
ed010 74 69 61 6c 20 69 6e 66 69 6e 69 74 65 20 6c 6f tial infinite lo
ed020 6f 70 29 2e 0a 20 20 2a 2f 0a 20 20 64 75 6d 6d op).. */. dumm
ed030 79 56 66 73 2e 70 41 70 70 44 61 74 61 20 3d 20 yVfs.pAppData =
ed040 28 76 6f 69 64 2a 29 26 61 75 74 6f 6c 6f 63 6b (void*)&autolock
ed050 49 6f 46 69 6e 64 65 72 3b 0a 20 20 64 75 6d 6d IoFinder;. dumm
ed060 79 56 66 73 2e 78 4f 70 65 6e 20 3d 20 30 3b 0a yVfs.xOpen = 0;.
ed070 20 20 72 63 20 3d 20 75 6e 69 78 4f 70 65 6e 28 rc = unixOpen(
ed080 26 64 75 6d 6d 79 56 66 73 2c 20 70 61 74 68 2c &dummyVfs, path,
ed090 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a (sqlite3_file *
ed0a0 29 70 4e 65 77 2c 20 66 6c 61 67 73 2c 20 26 66 )pNew, flags, &f
ed0b0 6c 61 67 73 29 3b 0a 20 20 69 66 28 20 72 63 3d lags);. if( rc=
ed0c0 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 66 =SQLITE_OK && (f
ed0d0 6c 61 67 73 26 53 51 4c 49 54 45 5f 4f 50 45 4e lags&SQLITE_OPEN
ed0e0 5f 52 45 41 44 4f 4e 4c 59 29 20 29 7b 0a 20 20 _READONLY) ){.
ed0f0 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 2d pNew->pMethod-
ed100 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 >xClose((sqlite3
ed110 5f 66 69 6c 65 20 2a 29 70 4e 65 77 29 3b 0a 20 _file *)pNew);.
ed120 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
ed130 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 0a 20 20 ANTOPEN;. }..
ed140 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
ed150 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 K ){. sqlite3
ed160 5f 66 72 65 65 28 70 4e 65 77 29 3b 0a 20 20 20 _free(pNew);.
ed170 20 70 4e 65 77 20 3d 20 30 3b 0a 20 20 7d 0a 0a pNew = 0;. }..
ed180 20 20 2a 70 70 46 69 6c 65 20 3d 20 70 4e 65 77 *ppFile = pNew
ed190 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
ed1a0 0a 0a 2f 2a 20 74 61 6b 65 73 20 74 68 65 20 63 ../* takes the c
ed1b0 6f 6e 63 68 20 62 79 20 74 61 6b 69 6e 67 20 61 onch by taking a
ed1c0 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64 shared lock and
ed1d0 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e read the conten
ed1e0 74 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a 2a 2a ts conch, if .**
ed1f0 20 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e 6f 6e lockPath is non
ed200 2d 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73 74 20 -NULL, the host
ed210 49 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c 65 ID and lock file
ed220 20 70 61 74 68 20 6d 75 73 74 20 6d 61 74 63 68 path must match
ed230 2e 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20 6c 6f . A NULL .** lo
ed240 63 6b 50 61 74 68 20 6d 65 61 6e 73 20 74 68 61 ckPath means tha
ed250 74 20 74 68 65 20 6c 6f 63 6b 50 61 74 68 20 69 t the lockPath i
ed260 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 n the conch file
ed270 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69 66 will be used if
ed280 20 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 49 44 the .** host ID
ed290 73 20 6d 61 74 63 68 2c 20 6f 72 20 61 20 6e 65 s match, or a ne
ed2a0 77 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c w lock path will
ed2b0 20 62 65 20 67 65 6e 65 72 61 74 65 64 20 61 75 be generated au
ed2c0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 tomatically .**
ed2d0 61 6e 64 20 77 72 69 74 74 65 6e 20 74 6f 20 74 and written to t
ed2e0 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a he conch file..*
ed2f0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f /.static int pro
ed300 78 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e 69 78 xyTakeConch(unix
ed310 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 File *pFile){.
ed320 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
ed330 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f ext *pCtx = (pro
ed340 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
ed350 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *)pFile->lockin
ed360 67 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a 20 20 gContext; . .
ed370 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 if( pCtx->conchH
ed380 65 6c 64 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 eld>0 ){. ret
ed390 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
ed3a0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 }else{. unix
ed3b0 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20 File *conchFile
ed3c0 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c = pCtx->conchFil
ed3d0 65 3b 0a 20 20 20 20 63 68 61 72 20 74 65 73 74 e;. char test
ed3e0 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b Value[CONCHLEN];
ed3f0 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 63 68 56 . char conchV
ed400 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a alue[CONCHLEN];.
ed410 20 20 20 20 63 68 61 72 20 6c 6f 63 6b 50 61 74 char lockPat
ed420 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a 20 h[MAXPATHLEN];.
ed430 20 20 20 63 68 61 72 20 2a 74 4c 6f 63 6b 50 61 char *tLockPa
ed440 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 th = NULL;. i
ed450 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
ed460 4b 3b 0a 20 20 20 20 69 6e 74 20 72 65 61 64 52 K;. int readR
ed470 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
ed480 20 20 20 69 6e 74 20 73 79 6e 63 50 65 72 6d 73 int syncPerms
ed490 20 3d 20 30 3b 0a 0a 20 20 20 20 4f 53 54 52 41 = 0;.. OSTRA
ed4a0 43 45 34 28 22 54 41 4b 45 43 4f 4e 43 48 20 20 CE4("TAKECONCH
ed4b0 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25 64 %d for %s pid=%d
ed4c0 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e \n", conchFile->
ed4d0 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 h,.
ed4e0 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 (pCtx->lockProxy
ed4f0 50 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c 6f 63 Path ? pCtx->loc
ed500 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 22 3a 61 kProxyPath : ":a
ed510 75 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 29 uto:"), getpid()
ed520 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e );.. rc = con
ed530 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d chFile->pMethod-
ed540 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f >xLock((sqlite3_
ed550 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c file*)conchFile,
ed560 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 SHARED_LOCK);.
ed570 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
ed580 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e E_OK ){. in
ed590 74 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 t pError = 0;.
ed5a0 20 20 20 20 6d 65 6d 73 65 74 28 74 65 73 74 56 memset(testV
ed5b0 61 6c 75 65 2c 20 30 2c 20 43 4f 4e 43 48 4c 45 alue, 0, CONCHLE
ed5c0 4e 29 3b 20 2f 2a 20 63 6f 6e 63 68 20 69 73 20 N); /* conch is
ed5d0 66 69 78 65 64 20 73 69 7a 65 20 2a 2f 0a 20 20 fixed size */.
ed5e0 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 47 65 rc = proxyGe
ed5f0 74 48 6f 73 74 49 44 28 74 65 73 74 56 61 6c 75 tHostID(testValu
ed600 65 2c 20 26 70 45 72 72 6f 72 29 3b 0a 20 20 20 e, &pError);.
ed610 20 20 20 69 66 28 20 28 72 63 26 30 78 66 66 29 if( (rc&0xff)
ed620 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 ==SQLITE_IOERR )
ed630 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d {. pFile-
ed640 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 70 45 72 >lastErrno = pEr
ed650 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ror;. }.
ed660 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 if( pCtx->loc
ed670 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 kProxyPath ){.
ed680 20 20 20 20 20 20 73 74 72 6c 63 70 79 28 26 74 strlcpy(&t
ed690 65 73 74 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c estValue[HOSTIDL
ed6a0 45 4e 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 EN], pCtx->lockP
ed6b0 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 roxyPath, MAXPAT
ed6c0 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 HLEN);. }.
ed6d0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 }. if( rc!
ed6e0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
ed6f0 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b goto end_tak
ed700 65 63 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20 20 econch;. }.
ed710 20 20 0a 20 20 20 20 72 65 61 64 52 63 20 3d 20 . readRc =
ed720 75 6e 69 78 52 65 61 64 28 28 73 71 6c 69 74 65 unixRead((sqlite
ed730 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 3_file *)conchFi
ed740 6c 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 le, conchValue,
ed750 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 CONCHLEN, 0);.
ed760 20 20 69 66 28 20 72 65 61 64 52 63 21 3d 53 51 if( readRc!=SQ
ed770 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 LITE_IOERR_SHORT
ed780 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 69 _READ ){. i
ed790 66 28 20 72 65 61 64 52 63 21 3d 53 51 4c 49 54 f( readRc!=SQLIT
ed7a0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
ed7b0 69 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53 if( (rc&0xff)==S
ed7c0 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 QLITE_IOERR ){.
ed7d0 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e pFile->
ed7e0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 63 6f 6e 63 lastErrno = conc
ed7f0 68 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f hFile->lastErrno
ed800 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
ed810 20 20 20 20 72 63 20 3d 20 72 65 61 64 52 63 3b rc = readRc;
ed820 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e . goto en
ed830 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 d_takeconch;.
ed840 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 69 66 }. /* if
ed850 20 74 68 65 20 63 6f 6e 63 68 20 68 61 73 20 64 the conch has d
ed860 61 74 61 20 63 6f 6d 70 61 72 65 20 74 68 65 20 ata compare the
ed870 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 20 20 contents */.
ed880 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63 if( !pCtx->loc
ed890 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 kProxyPath ){.
ed8a0 20 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 75 74 /* for aut
ed8b0 6f 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 6c 6f o-named local lo
ed8c0 63 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 63 68 ck file, just ch
ed8d0 65 63 6b 20 74 68 65 20 68 6f 73 74 20 49 44 20 eck the host ID
ed8e0 61 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20 20 20 and we'll.
ed8f0 20 20 20 2a 2a 20 75 73 65 20 74 68 65 20 6c 6f ** use the lo
ed900 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 cal lock file pa
ed910 74 68 20 74 68 61 74 27 73 20 61 6c 72 65 61 64 th that's alread
ed920 79 20 69 6e 20 74 68 65 72 65 20 2a 2f 0a 20 20 y in there */.
ed930 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 6d if( !memcm
ed940 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e p(testValue, con
ed950 63 68 56 61 6c 75 65 2c 20 48 4f 53 54 49 44 4c chValue, HOSTIDL
ed960 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 EN) ){.
ed970 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 28 63 68 tLockPath = (ch
ed980 61 72 20 2a 29 26 63 6f 6e 63 68 56 61 6c 75 65 ar *)&conchValue
ed990 5b 48 4f 53 54 49 44 4c 45 4e 5d 3b 0a 20 20 20 [HOSTIDLEN];.
ed9a0 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
ed9b0 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20 takeconch;.
ed9c0 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
ed9d0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 77 65 27 {. /* we'
ed9e0 76 65 20 67 6f 74 20 74 68 65 20 63 6f 6e 63 68 ve got the conch
ed9f0 20 69 66 20 63 6f 6e 63 68 56 61 6c 75 65 20 6d if conchValue m
eda00 61 74 63 68 65 73 20 6f 75 72 20 70 61 74 68 20 atches our path
eda10 61 6e 64 20 68 6f 73 74 20 49 44 20 2a 2f 0a 20 and host ID */.
eda20 20 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 if( !memc
eda30 6d 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f mp(testValue, co
eda40 6e 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c nchValue, CONCHL
eda50 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 EN) ){.
eda60 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f goto end_takeco
eda70 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 nch;. }.
eda80 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
eda90 7b 0a 20 20 20 20 20 20 2f 2a 20 61 20 73 68 6f {. /* a sho
edaa0 72 74 20 72 65 61 64 20 6d 65 61 6e 73 20 77 65 rt read means we
edab0 27 72 65 20 22 63 72 65 61 74 69 6e 67 22 20 74 're "creating" t
edac0 68 65 20 63 6f 6e 63 68 20 28 65 76 65 6e 20 74 he conch (even t
edad0 68 6f 75 67 68 20 69 74 20 63 6f 75 6c 64 20 0a hough it could .
edae0 20 20 20 20 20 20 2a 2a 20 68 61 76 65 20 62 65 ** have be
edaf0 65 6e 20 75 73 65 72 2d 69 6e 74 65 72 76 65 6e en user-interven
edb00 74 69 6f 6e 29 2c 20 69 66 20 77 65 20 61 63 71 tion), if we acq
edb10 75 69 72 65 20 74 68 65 20 65 78 63 6c 75 73 69 uire the exclusi
edb20 76 65 20 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 2a ve lock,. *
edb30 2a 20 77 65 27 6c 6c 20 74 72 79 20 74 6f 20 6d * we'll try to m
edb40 61 74 63 68 20 74 68 65 20 63 75 72 72 65 6e 74 atch the current
edb50 20 6f 6e 2d 64 69 73 6b 20 70 65 72 6d 69 73 73 on-disk permiss
edb60 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 61 74 61 ions of the data
edb70 62 61 73 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 base. */.
edb80 20 20 20 20 73 79 6e 63 50 65 72 6d 73 20 3d 20 syncPerms =
edb90 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 1;. }. .
edba0 20 20 2f 2a 20 65 69 74 68 65 72 20 63 6f 6e 63 /* either conc
edbb0 68 20 77 61 73 20 65 6d 74 70 79 20 6f 72 20 64 h was emtpy or d
edbc0 69 64 6e 27 74 20 6d 61 74 63 68 20 2a 2f 0a 20 idn't match */.
edbd0 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f if( !pCtx->lo
edbe0 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 ckProxyPath ){.
edbf0 20 20 20 20 20 70 72 6f 78 79 47 65 74 4c 6f 63 proxyGetLoc
edc00 6b 50 61 74 68 28 70 43 74 78 2d 3e 64 62 50 61 kPath(pCtx->dbPa
edc10 74 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d 41 th, lockPath, MA
edc20 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20 XPATHLEN);.
edc30 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 6c 6f 63 tLockPath = loc
edc40 6b 50 61 74 68 3b 0a 20 20 20 20 20 20 73 74 72 kPath;. str
edc50 6c 63 70 79 28 26 74 65 73 74 56 61 6c 75 65 5b lcpy(&testValue[
edc60 48 4f 53 54 49 44 4c 45 4e 5d 2c 20 6c 6f 63 6b HOSTIDLEN], lock
edc70 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e Path, MAXPATHLEN
edc80 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 );. }. .
edc90 20 20 2f 2a 20 75 70 64 61 74 65 20 63 6f 6e 63 /* update conc
edca0 68 20 77 69 74 68 20 68 6f 73 74 20 61 6e 64 20 h with host and
edcb0 70 61 74 68 20 28 74 68 69 73 20 77 69 6c 6c 20 path (this will
edcc0 66 61 69 6c 20 69 66 20 6f 74 68 65 72 20 70 72 fail if other pr
edcd0 6f 63 65 73 73 0a 20 20 20 20 20 2a 2a 20 68 61 ocess. ** ha
edce0 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 s a shared lock
edcf0 61 6c 72 65 61 64 79 29 20 2a 2f 0a 20 20 20 20 already) */.
edd00 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e rc = conchFile->
edd10 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 pMethod->xLock((
edd20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f sqlite3_file*)co
edd30 6e 63 68 46 69 6c 65 2c 20 45 58 43 4c 55 53 49 nchFile, EXCLUSI
edd40 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 VE_LOCK);. if
edd50 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
edd60 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 6e ){. rc = un
edd70 69 78 57 72 69 74 65 28 28 73 71 6c 69 74 65 33 ixWrite((sqlite3
edd80 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c _file *)conchFil
edd90 65 2c 20 74 65 73 74 56 61 6c 75 65 2c 20 43 4f e, testValue, CO
edda0 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 NCHLEN, 0);.
eddb0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
eddc0 5f 4f 4b 20 26 26 20 73 79 6e 63 50 65 72 6d 73 _OK && syncPerms
eddd0 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75 ){. stru
edde0 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 20 ct stat buf;.
eddf0 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 66 int err = f
ede00 73 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c 20 26 stat(pFile->h, &
ede10 62 75 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66 buf);. if
ede20 28 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 ( err==0 ){.
ede30 20 20 20 20 20 20 2f 2a 20 74 72 79 20 74 6f 20 /* try to
ede40 6d 61 74 63 68 20 74 68 65 20 64 61 74 61 62 61 match the databa
ede50 73 65 20 66 69 6c 65 20 70 65 72 6d 69 73 73 69 se file permissi
ede60 6f 6e 73 2c 20 69 67 6e 6f 72 65 20 66 61 69 6c ons, ignore fail
ede70 75 72 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ure */.#ifndef S
ede80 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 QLITE_PROXY_DEBU
ede90 47 0a 20 20 20 20 20 20 20 20 20 20 66 63 68 6d G. fchm
edea0 6f 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c od(conchFile->h,
edeb0 20 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 23 buf.st_mode);.#
edec0 65 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20 69 else. i
eded0 66 28 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46 f( fchmod(conchF
edee0 69 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d ile->h, buf.st_m
edef0 6f 64 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 ode)!=0 ){.
edf00 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20 int code
edf10 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 = errno;.
edf20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 fprintf(std
edf30 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20 err, "fchmod %o
edf40 46 41 49 4c 45 44 20 77 69 74 68 20 25 64 20 25 FAILED with %d %
edf50 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 s\n",.
edf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
edf70 20 20 20 62 75 66 2e 73 74 5f 6d 6f 64 65 2c 20 buf.st_mode,
edf80 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28 63 code, strerror(c
edf90 6f 64 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20 ode));.
edfa0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 } else {.
edfb0 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 fprintf(st
edfc0 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f derr, "fchmod %o
edfd0 20 53 55 43 43 45 44 45 44 5c 6e 22 2c 62 75 66 SUCCEDED\n",buf
edfe0 2e 73 74 5f 6d 6f 64 65 29 3b 0a 20 20 20 20 20 .st_mode);.
edff0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
ee000 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
ee010 69 6e 74 20 63 6f 64 65 20 3d 20 65 72 72 6e 6f int code = errno
ee020 3b 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69 ;. fpri
ee030 6e 74 66 28 73 74 64 65 72 72 2c 20 22 53 54 41 ntf(stderr, "STA
ee040 54 20 46 41 49 4c 45 44 5b 25 64 5d 20 77 69 74 T FAILED[%d] wit
ee050 68 20 25 64 20 25 73 5c 6e 22 2c 20 0a 20 20 20 h %d %s\n", .
ee060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ee070 20 20 20 20 20 20 20 65 72 72 2c 20 63 6f 64 65 err, code
ee080 2c 20 73 74 72 65 72 72 6f 72 28 63 6f 64 65 29 , strerror(code)
ee090 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 );.#endif.
ee0a0 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
ee0b0 7d 0a 20 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d }. conchFile-
ee0c0 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 >pMethod->xUnloc
ee0d0 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a k((sqlite3_file*
ee0e0 29 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 52 )conchFile, SHAR
ee0f0 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 0a 65 6e 64 ED_LOCK);. .end
ee100 5f 74 61 6b 65 63 6f 6e 63 68 3a 0a 20 20 20 20 _takeconch:.
ee110 4f 53 54 52 41 43 45 32 28 22 54 52 41 4e 53 50 OSTRACE2("TRANSP
ee120 52 4f 58 59 3a 20 43 4c 4f 53 45 20 20 25 64 5c ROXY: CLOSE %d\
ee130 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 n", pFile->h);.
ee140 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
ee150 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6f E_OK && pFile->o
ee160 70 65 6e 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 penFlags ){.
ee170 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d if( pFile->h>=
ee180 30 20 29 7b 0a 23 69 66 64 65 66 20 53 54 52 49 0 ){.#ifdef STRI
ee190 43 54 5f 43 4c 4f 53 45 5f 45 52 52 4f 52 0a 20 CT_CLOSE_ERROR.
ee1a0 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 if( close
ee1b0 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 (pFile->h) ){.
ee1c0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
ee1d0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f astErrno = errno
ee1e0 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ;. retu
ee1f0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
ee200 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 20 20 7d CLOSE;. }
ee210 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 63 .#else. c
ee220 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 20 lose(pFile->h);
ee230 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b /* silently leak
ee240 20 66 64 20 69 66 20 66 61 69 6c 20 2a 2f 0a 23 fd if fail */.#
ee250 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20 endif. }.
ee260 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d pFile->h = -
ee270 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 64 20 1;. int fd
ee280 3d 20 6f 70 65 6e 28 70 43 74 78 2d 3e 64 62 50 = open(pCtx->dbP
ee290 61 74 68 2c 20 70 46 69 6c 65 2d 3e 6f 70 65 6e ath, pFile->open
ee2a0 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 Flags,.
ee2b0 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 SQLIT
ee2c0 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 E_DEFAULT_FILE_P
ee2d0 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20 ERMISSIONS);.
ee2e0 20 20 20 4f 53 54 52 41 43 45 32 28 22 54 52 41 OSTRACE2("TRA
ee2f0 4e 53 50 52 4f 58 59 3a 20 4f 50 45 4e 20 20 25 NSPROXY: OPEN %
ee300 64 5c 6e 22 2c 20 66 64 29 3b 0a 20 20 20 20 20 d\n", fd);.
ee310 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20 if( fd>=0 ){.
ee320 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d pFile->h =
ee330 20 66 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 fd;. }else
ee340 7b 0a 20 20 20 20 20 20 20 20 72 63 3d 53 51 4c {. rc=SQL
ee350 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 20 2f 2a ITE_CANTOPEN; /*
ee360 20 53 51 4c 49 54 45 5f 42 55 53 59 3f 20 70 72 SQLITE_BUSY? pr
ee370 6f 78 79 54 61 6b 65 43 6f 6e 63 68 20 63 61 6c oxyTakeConch cal
ee380 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 led.
ee390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ee3a0 20 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e during lockin
ee3b0 67 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 g */. }.
ee3c0 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 }. if( rc==S
ee3d0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 43 74 QLITE_OK && !pCt
ee3e0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a x->lockProxy ){.
ee3f0 20 20 20 20 20 20 63 68 61 72 20 2a 70 61 74 68 char *path
ee400 20 3d 20 74 4c 6f 63 6b 50 61 74 68 20 3f 20 74 = tLockPath ? t
ee410 4c 6f 63 6b 50 61 74 68 20 3a 20 70 43 74 78 2d LockPath : pCtx-
ee420 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a >lockProxyPath;.
ee430 20 20 20 20 20 20 2f 2a 20 41 43 53 3a 20 4e 65 /* ACS: Ne
ee440 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f 70 ed to make a cop
ee450 79 20 6f 66 20 70 61 74 68 20 73 6f 6d 65 74 69 y of path someti
ee460 6d 65 73 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 mes */. rc
ee470 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e 69 = proxyCreateUni
ee480 78 46 69 6c 65 28 70 61 74 68 2c 20 26 70 43 74 xFile(path, &pCt
ee490 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 x->lockProxy);.
ee4a0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d }. if( rc=
ee4b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
ee4c0 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 pCtx->conchH
ee4d0 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 eld = 1;..
ee4e0 69 66 28 20 74 4c 6f 63 6b 50 61 74 68 20 29 7b if( tLockPath ){
ee4f0 0a 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c . pCtx->l
ee500 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73 ockProxyPath = s
ee510 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 qlite3DbStrDup(0
ee520 2c 20 74 4c 6f 63 6b 50 61 74 68 29 3b 0a 20 20 , tLockPath);.
ee530 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e if( pCtx->
ee540 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 lockProxy->pMeth
ee550 6f 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 od == &afpIoMeth
ee560 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ods ){.
ee570 20 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e ((afpLockingCon
ee580 74 65 78 74 20 2a 29 70 43 74 78 2d 3e 6c 6f 63 text *)pCtx->loc
ee590 6b 50 72 6f 78 79 2d 3e 6c 6f 63 6b 69 6e 67 43 kProxy->lockingC
ee5a0 6f 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 20 ontext)->dbPath
ee5b0 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 =.
ee5c0 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 pCtx->loc
ee5d0 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20 kProxyPath;.
ee5e0 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
ee5f0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
ee600 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 conchFile->pMet
ee610 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 hod->xUnlock((sq
ee620 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 lite3_file*)conc
ee630 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b hFile, NO_LOCK);
ee640 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 . }. OSTRA
ee650 43 45 33 28 22 54 41 4b 45 43 4f 4e 43 48 20 20 CE3("TAKECONCH
ee660 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 %d %s\n", conchF
ee670 69 6c 65 2d 3e 68 2c 20 72 63 3d 3d 53 51 4c 49 ile->h, rc==SQLI
ee680 54 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 69 6c TE_OK?"ok":"fail
ee690 65 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ed");. return
ee6a0 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a rc;. }.}../*.*
ee6b0 2a 20 49 66 20 70 46 69 6c 65 20 68 6f 6c 64 73 * If pFile holds
ee6c0 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 63 6f 6e a lock on a con
ee6d0 63 68 20 66 69 6c 65 2c 20 74 68 65 6e 20 72 65 ch file, then re
ee6e0 6c 65 61 73 65 20 74 68 61 74 20 6c 6f 63 6b 2e lease that lock.
ee6f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
ee700 72 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 68 roxyReleaseConch
ee710 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 (unixFile *pFile
ee720 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
ee730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ee740 20 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 72 /* Subroutine r
ee750 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
ee760 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
ee770 65 78 74 20 2a 70 43 74 78 3b 20 20 2f 2a 20 54 ext *pCtx; /* T
ee780 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 he locking conte
ee790 78 74 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79 xt for the proxy
ee7a0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 69 78 46 lock */. unixF
ee7b0 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 ile *conchFile;
ee7c0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
ee7d0 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 f the conch file
ee7e0 20 2a 2f 0a 0a 20 20 70 43 74 78 20 3d 20 28 70 */.. pCtx = (p
ee7f0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
ee800 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b xt *)pFile->lock
ee810 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63 6f ingContext;. co
ee820 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e nchFile = pCtx->
ee830 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 4f 53 54 conchFile;. OST
ee840 52 41 43 45 34 28 22 52 45 4c 45 41 53 45 43 4f RACE4("RELEASECO
ee850 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73 20 70 NCH %d for %s p
ee860 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 id=%d\n", conchF
ee870 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 ile->h,.
ee880 20 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 (pCtx->lockPr
ee890 6f 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d 3e oxyPath ? pCtx->
ee8a0 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 lockProxyPath :
ee8b0 22 3a 61 75 74 6f 3a 22 29 2c 20 0a 20 20 20 20 ":auto:"), .
ee8c0 20 20 20 20 20 20 20 67 65 74 70 69 64 28 29 29 getpid())
ee8d0 3b 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 ;. pCtx->conchH
ee8e0 65 6c 64 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 eld = 0;. rc =
ee8f0 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 conchFile->pMeth
ee900 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c od->xUnlock((sql
ee910 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 ite3_file*)conch
ee920 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a File, NO_LOCK);.
ee930 20 20 4f 53 54 52 41 43 45 33 28 22 52 45 4c 45 OSTRACE3("RELE
ee940 41 53 45 43 4f 4e 43 48 20 20 25 64 20 25 73 5c ASECONCH %d %s\
ee950 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 n", conchFile->h
ee960 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 72 63 ,. (rc
ee970 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f ==SQLITE_OK ? "o
ee980 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b k" : "failed"));
ee990 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
ee9a0 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 ./*.** Given the
ee9b0 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62 name of a datab
ee9c0 61 73 65 20 66 69 6c 65 2c 20 63 6f 6d 70 75 74 ase file, comput
ee9d0 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 69 74 e the name of it
ee9e0 73 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2a s conch file..**
ee9f0 20 53 74 6f 72 65 20 74 68 65 20 63 6f 6e 63 68 Store the conch
eea00 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6d 65 6d filename in mem
eea10 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
eea20 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 m sqlite3_malloc
eea30 28 29 2e 0a 2a 2a 20 4d 61 6b 65 20 2a 70 43 6f ()..** Make *pCo
eea40 6e 63 68 50 61 74 68 20 70 6f 69 6e 74 20 74 6f nchPath point to
eea50 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 2e 20 20 the new name.
eea60 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
eea70 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f on success.** o
eea80 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 r SQLITE_NOMEM i
eea90 66 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 f unable to obta
eeaa0 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a in memory..**.**
eeab0 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 The caller is r
eeac0 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 65 esponsible for e
eead0 6e 73 75 72 69 6e 67 20 74 68 61 74 20 74 68 65 nsuring that the
eeae0 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 allocated memor
eeaf0 79 0a 2a 2a 20 73 70 61 63 65 20 69 73 20 65 76 y.** space is ev
eeb00 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e 0a entually freed..
eeb10 2a 2a 0a 2a 2a 20 2a 70 43 6f 6e 63 68 50 61 74 **.** *pConchPat
eeb20 68 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c h is set to NULL
eeb30 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c if a memory all
eeb40 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 ocation error oc
eeb50 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 curs..*/.static
eeb60 69 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65 43 int proxyCreateC
eeb70 6f 6e 63 68 50 61 74 68 6e 61 6d 65 28 63 68 61 onchPathname(cha
eeb80 72 20 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20 r *dbPath, char
eeb90 2a 2a 70 43 6f 6e 63 68 50 61 74 68 29 7b 0a 20 **pConchPath){.
eeba0 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
eebb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
eebc0 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
eebd0 2f 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28 69 /. int len = (i
eebe0 6e 74 29 73 74 72 6c 65 6e 28 64 62 50 61 74 68 nt)strlen(dbPath
eebf0 29 3b 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 ); /* Length of
eec00 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d database filenam
eec10 65 20 2d 20 64 62 50 61 74 68 20 2a 2f 0a 20 20 e - dbPath */.
eec20 63 68 61 72 20 2a 63 6f 6e 63 68 50 61 74 68 3b char *conchPath;
eec30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
eec40 20 62 75 66 66 65 72 20 69 6e 20 77 68 69 63 68 buffer in which
eec50 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 63 6f to construct co
eec60 6e 63 68 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 2f nch name */.. /
eec70 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 * Allocate space
eec80 20 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 66 for the conch f
eec90 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6e 69 74 ilename and init
eeca0 69 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d 65 20 ialize the name
eecb0 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 to. ** the name
eecc0 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c of the original
eecd0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
eece0 2a 2f 20 20 0a 20 20 2a 70 43 6f 6e 63 68 50 61 */ . *pConchPa
eecf0 74 68 20 3d 20 63 6f 6e 63 68 50 61 74 68 20 3d th = conchPath =
eed00 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 (char *)sqlite3
eed10 5f 6d 61 6c 6c 6f 63 28 6c 65 6e 20 2b 20 38 29 _malloc(len + 8)
eed20 3b 0a 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74 ;. if( conchPat
eed30 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 h==0 ){. retu
eed40 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
eed50 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 63 6f . }. memcpy(co
eed60 6e 63 68 50 61 74 68 2c 20 64 62 50 61 74 68 2c nchPath, dbPath,
eed70 20 6c 65 6e 2b 31 29 3b 0a 20 20 0a 20 20 2f 2a len+1);. . /*
eed80 20 6e 6f 77 20 69 6e 73 65 72 74 20 61 20 22 2e now insert a ".
eed90 22 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 73 " before the las
eeda0 74 20 2f 20 63 68 61 72 61 63 74 65 72 20 2a 2f t / character */
eedb0 0a 20 20 66 6f 72 28 20 69 3d 28 6c 65 6e 2d 31 . for( i=(len-1
eedc0 29 3b 20 69 3e 3d 30 3b 20 69 2d 2d 20 29 7b 0a ); i>=0; i-- ){.
eedd0 20 20 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74 if( conchPat
eede0 68 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 h[i]=='/' ){.
eedf0 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 62 72 i++;. br
eee00 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
eee10 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 27 2e conchPath[i]='.
eee20 27 3b 0a 20 20 77 68 69 6c 65 20 28 20 69 3c 6c ';. while ( i<l
eee30 65 6e 20 29 7b 0a 20 20 20 20 63 6f 6e 63 68 50 en ){. conchP
eee40 61 74 68 5b 69 2b 31 5d 3d 64 62 50 61 74 68 5b ath[i+1]=dbPath[
eee50 69 5d 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d i];. i++;. }
eee60 0a 0a 20 20 2f 2a 20 61 70 70 65 6e 64 20 74 68 .. /* append th
eee70 65 20 22 2d 63 6f 6e 63 68 22 20 73 75 66 66 69 e "-conch" suffi
eee80 78 20 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f x to the file */
eee90 0a 20 20 6d 65 6d 63 70 79 28 26 63 6f 6e 63 68 . memcpy(&conch
eeea0 50 61 74 68 5b 69 2b 31 5d 2c 20 22 2d 63 6f 6e Path[i+1], "-con
eeeb0 63 68 22 2c 20 37 29 3b 0a 20 20 61 73 73 65 72 ch", 7);. asser
eeec0 74 28 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 63 t( (int)strlen(c
eeed0 6f 6e 63 68 50 61 74 68 29 20 3d 3d 20 6c 65 6e onchPath) == len
eeee0 2b 37 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 +7 );.. return
eeef0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f SQLITE_OK;.}.../
eef00 2a 20 54 61 6b 65 73 20 61 20 66 75 6c 6c 79 20 * Takes a fully
eef10 63 6f 6e 66 69 67 75 72 65 64 20 70 72 6f 78 79 configured proxy
eef20 20 6c 6f 63 6b 69 6e 67 2d 73 74 79 6c 65 20 75 locking-style u
eef30 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 73 77 69 nix file and swi
eef40 74 63 68 65 73 0a 2a 2a 20 74 68 65 20 6c 6f 63 tches.** the loc
eef50 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 al lock file pat
eef60 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 h .*/.static int
eef70 20 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 switchLockProxy
eef80 50 61 74 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 Path(unixFile *p
eef90 46 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 File, const char
eefa0 20 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 *path) {. prox
eefb0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
eefc0 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f *pCtx = (proxyLo
eefd0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 ckingContext*)pF
eefe0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
eeff0 65 78 74 3b 0a 20 20 63 68 61 72 20 2a 6f 6c 64 ext;. char *old
ef000 50 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 Path = pCtx->loc
ef010 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 69 6e kProxyPath;. in
ef020 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
ef030 3b 0a 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e ;.. if( pFile->
ef040 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 locktype!=NO_LOC
ef050 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 K ){. return
ef060 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
ef070 20 20 0a 0a 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 .. /* nothing
ef080 20 74 6f 20 64 6f 20 69 66 20 74 68 65 20 70 61 to do if the pa
ef090 74 68 20 69 73 20 4e 55 4c 4c 2c 20 3a 61 75 74 th is NULL, :aut
ef0a0 6f 3a 20 6f 72 20 6d 61 74 63 68 65 73 20 74 68 o: or matches th
ef0b0 65 20 65 78 69 73 74 69 6e 67 20 70 61 74 68 20 e existing path
ef0c0 2a 2f 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c */. if( !path |
ef0d0 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 | path[0]=='\0'
ef0e0 7c 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c || !strcmp(path,
ef0f0 20 22 3a 61 75 74 6f 3a 22 29 20 7c 7c 0a 20 20 ":auto:") ||.
ef100 20 20 28 6f 6c 64 50 61 74 68 20 26 26 20 21 73 (oldPath && !s
ef110 74 72 6e 63 6d 70 28 6f 6c 64 50 61 74 68 2c 20 trncmp(oldPath,
ef120 70 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e path, MAXPATHLEN
ef130 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e )) ){. return
ef140 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
ef150 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c lse{. unixFil
ef160 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 e *lockProxy = p
ef170 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a Ctx->lockProxy;.
ef180 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 pCtx->lockPr
ef190 6f 78 79 3d 4e 55 4c 4c 3b 0a 20 20 20 20 70 43 oxy=NULL;. pC
ef1a0 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 tx->conchHeld =
ef1b0 30 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 50 0;. if( lockP
ef1c0 72 6f 78 79 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 roxy!=NULL ){.
ef1d0 20 20 20 20 72 63 3d 6c 6f 63 6b 50 72 6f 78 79 rc=lockProxy
ef1e0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 ->pMethod->xClos
ef1f0 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 e((sqlite3_file
ef200 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 *)lockProxy);.
ef210 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
ef220 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71 urn rc;. sq
ef230 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50 lite3_free(lockP
ef240 72 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 roxy);. }.
ef250 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6f 6c sqlite3_free(ol
ef260 64 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74 78 dPath);. pCtx
ef270 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 ->lockProxyPath
ef280 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
ef290 70 28 30 2c 20 70 61 74 68 29 3b 0a 20 20 7d 0a p(0, path);. }.
ef2a0 20 20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
ef2b0 7d 0a 0a 2f 2a 0a 2a 2a 20 70 46 69 6c 65 20 69 }../*.** pFile i
ef2c0 73 20 61 20 66 69 6c 65 20 74 68 61 74 20 68 61 s a file that ha
ef2d0 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79 s been opened by
ef2e0 20 61 20 70 72 69 6f 72 20 78 4f 70 65 6e 20 63 a prior xOpen c
ef2f0 61 6c 6c 2e 20 20 64 62 50 61 74 68 0a 2a 2a 20 all. dbPath.**
ef300 69 73 20 61 20 73 74 72 69 6e 67 20 62 75 66 66 is a string buff
ef310 65 72 20 61 74 20 6c 65 61 73 74 20 4d 41 58 50 er at least MAXP
ef320 41 54 48 4c 45 4e 2b 31 20 63 68 61 72 61 63 74 ATHLEN+1 charact
ef330 65 72 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a ers in size..**.
ef340 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
ef350 66 69 6e 64 20 74 68 65 20 66 69 6c 65 6e 61 6d find the filenam
ef360 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
ef370 68 20 70 46 69 6c 65 20 61 6e 64 20 77 72 69 74 h pFile and writ
ef380 65 73 20 69 74 0a 2a 2a 20 69 6e 74 20 64 62 50 es it.** int dbP
ef390 61 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ath..*/.static i
ef3a0 6e 74 20 70 72 6f 78 79 47 65 74 44 62 50 61 74 nt proxyGetDbPat
ef3b0 68 46 6f 72 55 6e 69 78 46 69 6c 65 28 75 6e 69 hForUnixFile(uni
ef3c0 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 68 xFile *pFile, ch
ef3d0 61 72 20 2a 64 62 50 61 74 68 29 7b 0a 23 69 66 ar *dbPath){.#if
ef3e0 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 defined(__APPLE
ef3f0 5f 5f 29 0a 20 20 69 66 28 20 70 46 69 6c 65 2d __). if( pFile-
ef400 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 70 >pMethod == &afp
ef410 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 IoMethods ){.
ef420 20 2f 2a 20 61 66 70 20 73 74 79 6c 65 20 6b 65 /* afp style ke
ef430 65 70 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 eps a reference
ef440 74 6f 20 74 68 65 20 64 62 20 70 61 74 68 20 69 to the db path i
ef450 6e 20 74 68 65 20 66 69 6c 65 50 61 74 68 20 66 n the filePath f
ef460 69 65 6c 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20 ield . ** of
ef470 74 68 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 the struct */.
ef480 20 20 61 73 73 65 72 74 28 20 28 69 6e 74 29 73 assert( (int)s
ef490 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 trlen((char*)pFi
ef4a0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
ef4b0 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 xt)<=MAXPATHLEN
ef4c0 29 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 );. strcpy(db
ef4d0 50 61 74 68 2c 20 28 28 61 66 70 4c 6f 63 6b 69 Path, ((afpLocki
ef4e0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c ngContext *)pFil
ef4f0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
ef500 74 29 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 7d t)->dbPath);. }
ef510 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 69 66 else.#endif. if
ef520 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 ( pFile->pMethod
ef530 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 == &dotlockIoMe
ef540 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 thods ){. /*
ef550 64 6f 74 20 6c 6f 63 6b 20 73 74 79 6c 65 20 75 dot lock style u
ef560 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 ses the locking
ef570 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65 context to store
ef580 20 74 68 65 20 64 6f 74 20 6c 6f 63 6b 0a 20 20 the dot lock.
ef590 20 20 2a 2a 20 66 69 6c 65 20 70 61 74 68 20 2a ** file path *
ef5a0 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 /. int len =
ef5b0 73 74 72 6c 65 6e 28 28 63 68 61 72 20 2a 29 70 strlen((char *)p
ef5c0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
ef5d0 74 65 78 74 29 20 2d 20 73 74 72 6c 65 6e 28 44 text) - strlen(D
ef5e0 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 29 3b 0a OTLOCK_SUFFIX);.
ef5f0 20 20 20 20 6d 65 6d 63 70 79 28 64 62 50 61 74 memcpy(dbPat
ef600 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 h, (char *)pFile
ef610 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
ef620 2c 20 6c 65 6e 20 2b 20 31 29 3b 0a 20 20 7d 65 , len + 1);. }e
ef630 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c 20 lse{. /* all
ef640 6f 74 68 65 72 20 73 74 79 6c 65 73 20 75 73 65 other styles use
ef650 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e the locking con
ef660 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20 74 68 text to store th
ef670 65 20 64 62 20 66 69 6c 65 20 70 61 74 68 20 2a e db file path *
ef680 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 74 /. assert( st
ef690 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 6c rlen((char*)pFil
ef6a0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
ef6b0 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29 t)<=MAXPATHLEN )
ef6c0 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 50 ;. strcpy(dbP
ef6d0 61 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 ath, (char *)pFi
ef6e0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
ef6f0 78 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 xt);. }. retur
ef700 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
ef710 2f 2a 0a 2a 2a 20 54 61 6b 65 73 20 61 6e 20 61 /*.** Takes an a
ef720 6c 72 65 61 64 79 20 66 69 6c 6c 65 64 20 69 6e lready filled in
ef730 20 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 61 unix file and a
ef740 6c 74 65 72 73 20 69 74 20 73 6f 20 61 6c 6c 20 lters it so all
ef750 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a file locking .**
ef760 20 77 69 6c 6c 20 62 65 20 70 65 72 66 6f 72 6d will be perform
ef770 65 64 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 ed on the local
ef780 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 2e proxy lock file.
ef790 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
ef7a0 66 69 65 6c 64 73 0a 2a 2a 20 61 72 65 20 70 72 fields.** are pr
ef7b0 65 73 65 72 76 65 64 20 69 6e 20 74 68 65 20 6c eserved in the l
ef7c0 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 73 ocking context s
ef7d0 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 6e 20 o that they can
ef7e0 62 65 20 72 65 73 74 6f 72 65 64 20 61 6e 64 20 be restored and
ef7f0 0a 2a 2a 20 74 68 65 20 75 6e 69 78 20 73 74 72 .** the unix str
ef800 75 63 74 75 72 65 20 70 72 6f 70 65 72 6c 79 20 ucture properly
ef810 63 6c 65 61 6e 65 64 20 75 70 20 61 74 20 63 6c cleaned up at cl
ef820 6f 73 65 20 74 69 6d 65 3a 0a 2a 2a 20 20 2d 3e ose time:.** ->
ef830 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 2a lockingContext.*
ef840 2a 20 20 2d 3e 70 4d 65 74 68 6f 64 0a 2a 2f 0a * ->pMethod.*/.
ef850 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
ef860 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c TransformUnixFil
ef870 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c e(unixFile *pFil
ef880 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 e, const char *p
ef890 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c 6f ath) {. proxyLo
ef8a0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
ef8b0 74 78 3b 0a 20 20 63 68 61 72 20 64 62 50 61 74 tx;. char dbPat
ef8c0 68 5b 4d 41 58 50 41 54 48 4c 45 4e 2b 31 5d 3b h[MAXPATHLEN+1];
ef8d0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
ef8e0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
ef8f0 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c ile */. char *l
ef900 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20 ockPath=NULL;.
ef910 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
ef920 4f 4b 3b 0a 20 20 0a 20 20 69 66 28 20 70 46 69 OK;. . if( pFi
ef930 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f le->locktype!=NO
ef940 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 _LOCK ){. ret
ef950 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b urn SQLITE_BUSY;
ef960 0a 20 20 7d 0a 20 20 70 72 6f 78 79 47 65 74 44 . }. proxyGetD
ef970 62 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c 65 bPathForUnixFile
ef980 28 70 46 69 6c 65 2c 20 64 62 50 61 74 68 29 3b (pFile, dbPath);
ef990 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c 20 . if( !path ||
ef9a0 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c path[0]=='\0' ||
ef9b0 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20 22 !strcmp(path, "
ef9c0 3a 61 75 74 6f 3a 22 29 20 29 7b 0a 20 20 20 20 :auto:") ){.
ef9d0 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 lockPath=NULL;.
ef9e0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b }else{. lock
ef9f0 50 61 74 68 3d 28 63 68 61 72 20 2a 29 70 61 74 Path=(char *)pat
efa00 68 3b 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 h;. }. . OSTR
efa10 41 43 45 34 28 22 54 52 41 4e 53 50 52 4f 58 59 ACE4("TRANSPROXY
efa20 20 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d %d for %s pid=
efa30 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
efa40 0a 20 20 20 20 20 20 20 20 20 20 20 28 6c 6f 63 . (loc
efa50 6b 50 61 74 68 20 3f 20 6c 6f 63 6b 50 61 74 68 kPath ? lockPath
efa60 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 65 : ":auto:"), ge
efa70 74 70 69 64 28 29 29 3b 0a 0a 20 20 70 43 74 78 tpid());.. pCtx
efa80 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f = sqlite3_mallo
efa90 63 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 c( sizeof(*pCtx)
efaa0 20 29 3b 0a 20 20 69 66 28 20 70 43 74 78 3d 3d );. if( pCtx==
efab0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
efac0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
efad0 7d 0a 20 20 6d 65 6d 73 65 74 28 70 43 74 78 2c }. memset(pCtx,
efae0 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 0, sizeof(*pCtx
efaf0 29 29 3b 0a 0a 20 20 72 63 20 3d 20 70 72 6f 78 ));.. rc = prox
efb00 79 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74 68 yCreateConchPath
efb10 6e 61 6d 65 28 64 62 50 61 74 68 2c 20 26 70 43 name(dbPath, &pC
efb20 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 tx->conchFilePat
efb30 68 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 h);. if( rc==SQ
efb40 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
efb50 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 c = proxyCreateU
efb60 6e 69 78 46 69 6c 65 28 70 43 74 78 2d 3e 63 6f nixFile(pCtx->co
efb70 6e 63 68 46 69 6c 65 50 61 74 68 2c 20 26 70 43 nchFilePath, &pC
efb80 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a tx->conchFile);.
efb90 20 20 7d 20 20 0a 20 20 69 66 28 20 72 63 3d 3d } . if( rc==
efba0 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 SQLITE_OK && loc
efbb0 6b 50 61 74 68 20 29 7b 0a 20 20 20 20 70 43 74 kPath ){. pCt
efbc0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 x->lockProxyPath
efbd0 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 = sqlite3DbStrD
efbe0 75 70 28 30 2c 20 6c 6f 63 6b 50 61 74 68 29 3b up(0, lockPath);
efbf0 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d . }.. if( rc==
efc00 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
efc10 20 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 69 /* all memory i
efc20 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70 72 6f s allocated, pro
efc30 78 79 73 20 61 72 65 20 63 72 65 61 74 65 64 20 xys are created
efc40 61 6e 64 20 61 73 73 69 67 6e 65 64 2c 20 0a 20 and assigned, .
efc50 20 20 20 2a 2a 20 73 77 69 74 63 68 20 74 68 65 ** switch the
efc60 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 locking context
efc70 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 65 and pMethod the
efc80 6e 20 72 65 74 75 72 6e 2e 0a 20 20 20 20 2a 2f n return.. */
efc90 0a 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61 74 . pCtx->dbPat
efca0 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 h = sqlite3DbStr
efcb0 44 75 70 28 30 2c 20 64 62 50 61 74 68 29 3b 0a Dup(0, dbPath);.
efcc0 20 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 pCtx->oldLoc
efcd0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 46 kingContext = pF
efce0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
efcf0 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e ext;. pFile->
efd00 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d lockingContext =
efd10 20 70 43 74 78 3b 0a 20 20 20 20 70 43 74 78 2d pCtx;. pCtx-
efd20 3e 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 70 46 >pOldMethod = pF
efd30 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a 20 20 ile->pMethod;.
efd40 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 pFile->pMethod
efd50 20 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f = &proxyIoMetho
efd60 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ds;. }else{.
efd70 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 if( pCtx->conch
efd80 46 69 6c 65 20 29 7b 20 0a 20 20 20 20 20 20 72 File ){ . r
efd90 63 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 c = pCtx->conchF
efda0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 ile->pMethod->xC
efdb0 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 lose((sqlite3_fi
efdc0 6c 65 20 2a 29 70 43 74 78 2d 3e 63 6f 6e 63 68 le *)pCtx->conch
efdd0 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66 28 File);. if(
efde0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
efdf0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 . sqlite3_f
efe00 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 ree(pCtx->conchF
efe10 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ile);. }.
efe20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 sqlite3_free(pCt
efe30 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 x->conchFilePath
efe40 29 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ); . sqlite3_
efe50 66 72 65 65 28 70 43 74 78 29 3b 0a 20 20 7d 0a free(pCtx);. }.
efe60 20 20 4f 53 54 52 41 43 45 33 28 22 54 52 41 4e OSTRACE3("TRAN
efe70 53 50 52 4f 58 59 20 20 25 64 20 25 73 5c 6e 22 SPROXY %d %s\n"
efe80 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 , pFile->h,.
efe90 20 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49 (rc==SQLI
efea0 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 TE_OK ? "ok" : "
efeb0 66 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 65 74 failed"));. ret
efec0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a urn rc;.}.../*.*
efed0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 * This routine h
efee0 61 6e 64 6c 65 73 20 73 71 6c 69 74 65 33 5f 66 andles sqlite3_f
efef0 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 20 63 61 ile_control() ca
eff00 6c 6c 73 20 74 68 61 74 20 61 72 65 20 73 70 65 lls that are spe
eff10 63 69 66 69 63 0a 2a 2a 20 74 6f 20 70 72 6f 78 cific.** to prox
eff20 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 y locking..*/.st
eff30 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46 69 atic int proxyFi
eff40 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 leControl(sqlite
eff50 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
eff60 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b op, void *pArg){
eff70 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b . switch( op ){
eff80 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
eff90 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 _GET_LOCKPROXYFI
effa0 4c 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 LE: {. unix
effb0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
effc0 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 nixFile*)id;.
effd0 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d if( pFile->pM
effe0 65 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49 ethod == &proxyI
efff0 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 oMethods ){.
f0000 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 proxyLocking
f0010 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 Context *pCtx =
f0020 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e (proxyLockingCon
f0030 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 text*)pFile->loc
f0040 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 kingContext;.
f0050 20 20 20 20 20 70 72 6f 78 79 54 61 6b 65 43 6f proxyTakeCo
f0060 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 nch(pFile);.
f0070 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f if( pCtx->lo
f0080 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 ckProxyPath ){.
f0090 20 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74 *(const
f00a0 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20 char **)pArg =
f00b0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 pCtx->lockProxyP
f00c0 61 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c ath;. }el
f00d0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 se{. *(
f00e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41 const char **)pA
f00f0 72 67 20 3d 20 22 3a 61 75 74 6f 3a 20 28 6e 6f rg = ":auto: (no
f0100 74 20 68 65 6c 64 29 22 3b 0a 20 20 20 20 20 20 t held)";.
f0110 20 20 7d 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 }. } else
f0120 20 7b 0a 20 20 20 20 20 20 20 20 2a 28 63 6f 6e {. *(con
f0130 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 st char **)pArg
f0140 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a = NULL;. }.
f0150 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
f0160 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 ITE_OK;. }.
f0170 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 case SQLITE_SE
f0180 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a T_LOCKPROXYFILE:
f0190 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 6c {. unixFil
f01a0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
f01b0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20 20 File*)id;.
f01c0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
f01d0 4f 4b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 OK;. int is
f01e0 50 72 6f 78 79 53 74 79 6c 65 20 3d 20 28 70 46 ProxyStyle = (pF
f01f0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 ile->pMethod ==
f0200 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 29 &proxyIoMethods)
f0210 3b 0a 20 20 20 20 20 20 69 66 28 20 70 41 72 67 ;. if( pArg
f0220 3d 3d 4e 55 4c 4c 20 7c 7c 20 28 63 6f 6e 73 74 ==NULL || (const
f0230 20 63 68 61 72 20 2a 29 70 41 72 67 3d 3d 30 20 char *)pArg==0
f0240 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 ){. if( i
f0250 73 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20 sProxyStyle ){.
f0260 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e /* turn
f0270 20 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 off proxy locki
f0280 6e 67 20 2d 20 6e 6f 74 20 73 75 70 70 6f 72 74 ng - not support
f0290 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 ed */.
f02a0 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
f02b0 52 20 2f 2a 53 51 4c 49 54 45 5f 50 52 4f 54 4f R /*SQLITE_PROTO
f02c0 43 4f 4c 3f 20 53 51 4c 49 54 45 5f 4d 49 53 55 COL? SQLITE_MISU
f02d0 53 45 3f 2a 2f 3b 0a 20 20 20 20 20 20 20 20 7d SE?*/;. }
f02e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
f02f0 2f 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 6f 78 /* turn off prox
f0300 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 61 6c 72 65 y locking - alre
f0310 61 64 79 20 6f 66 66 20 2d 20 4e 4f 4f 50 20 2a ady off - NOOP *
f0320 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d /. rc =
f0330 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
f0340 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
f0350 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 e{. const
f0360 20 63 68 61 72 20 2a 70 72 6f 78 79 50 61 74 68 char *proxyPath
f0370 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a = (const char *
f0380 29 70 41 72 67 3b 0a 20 20 20 20 20 20 20 20 69 )pArg;. i
f0390 66 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20 f( isProxyStyle
f03a0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 6f ){. pro
f03b0 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
f03c0 20 2a 70 43 74 78 20 3d 20 0a 20 20 20 20 20 20 *pCtx = .
f03d0 20 20 20 20 20 20 28 70 72 6f 78 79 4c 6f 63 6b (proxyLock
f03e0 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c ingContext*)pFil
f03f0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
f0400 74 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 t;. if(
f0410 20 21 73 74 72 63 6d 70 28 70 41 72 67 2c 20 22 !strcmp(pArg, "
f0420 3a 61 75 74 6f 3a 22 29 20 0a 20 20 20 20 20 20 :auto:") .
f0430 20 20 20 20 20 7c 7c 20 28 70 43 74 78 2d 3e 6c || (pCtx->l
f0440 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 26 26 0a ockProxyPath &&.
f0450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 !
f0460 73 74 72 6e 63 6d 70 28 70 43 74 78 2d 3e 6c 6f strncmp(pCtx->lo
f0470 63 6b 50 72 6f 78 79 50 61 74 68 2c 20 70 72 6f ckProxyPath, pro
f0480 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c xyPath, MAXPATHL
f0490 45 4e 29 29 0a 20 20 20 20 20 20 20 20 20 20 29 EN)). )
f04a0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 {. rc
f04b0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
f04c0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
f04d0 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
f04e0 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50 switchLockProxyP
f04f0 61 74 68 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 ath(pFile, proxy
f0500 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 20 Path);.
f0510 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
f0520 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 {. /* t
f0530 75 72 6e 20 6f 6e 20 70 72 6f 78 79 20 66 69 6c urn on proxy fil
f0540 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 e locking */.
f0550 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 rc = prox
f0560 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 yTransformUnixFi
f0570 6c 65 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 le(pFile, proxyP
f0580 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a ath);. }.
f0590 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
f05a0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
f05b0 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 default: {.
f05c0 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b assert( 0 );
f05d0 20 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20 61 73 /* The call as
f05e0 73 75 72 65 73 20 74 68 61 74 20 6f 6e 6c 79 20 sures that only
f05f0 76 61 6c 69 64 20 6f 70 63 6f 64 65 73 20 61 72 valid opcodes ar
f0600 65 20 73 65 6e 74 20 2a 2f 0a 20 20 20 20 7d 0a e sent */. }.
f0610 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 }. /*NOTREACH
f0620 45 44 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53 51 ED*/. return SQ
f0630 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f LITE_ERROR;.}../
f0640 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73 *.** Within this
f0650 20 64 69 76 69 73 69 6f 6e 20 28 74 68 65 20 70 division (the p
f0660 72 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 roxying locking
f0670 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 20 implementation)
f0680 74 68 65 20 70 72 6f 63 65 64 75 72 65 73 0a 2a the procedures.*
f0690 2a 20 61 62 6f 76 65 20 74 68 69 73 20 70 6f 69 * above this poi
f06a0 6e 74 20 61 72 65 20 61 6c 6c 20 75 74 69 6c 69 nt are all utili
f06b0 74 69 65 73 2e 20 20 54 68 65 20 6c 6f 63 6b 2d ties. The lock-
f06c0 72 65 6c 61 74 65 64 20 6d 65 74 68 6f 64 73 20 related methods
f06d0 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 2d of the.** proxy-
f06e0 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f locking sqlite3_
f06f0 69 6f 5f 6d 65 74 68 6f 64 20 6f 62 6a 65 63 74 io_method object
f0700 20 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f 2a follow..*/.../*
f0710 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
f0720 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 checks if there
f0730 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c is a RESERVED l
f0740 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 ock held on the
f0750 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c specified.** fil
f0760 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 e by this or any
f0770 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 other process.
f0780 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 If such a lock i
f0790 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 s held, set *pRe
f07a0 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e sOut.** to a non
f07b0 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 -zero value othe
f07c0 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 rwise *pResOut i
f07d0 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 s set to zero.
f07e0 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
f07f0 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 .** is set to SQ
f0800 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 LITE_OK unless a
f0810 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 n I/O error occu
f0820 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 rs during lock c
f0830 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 hecking..*/.stat
f0840 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63 ic int proxyChec
f0850 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 kReservedLock(sq
f0860 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
f0870 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a int *pResOut) {.
f0880 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
f0890 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
f08a0 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 d;. int rc = pr
f08b0 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 oxyTakeConch(pFi
f08c0 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 le);. if( rc==S
f08d0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
f08e0 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
f08f0 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f ext *pCtx = (pro
f0900 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
f0910 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *)pFile->lockin
f0920 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e gContext;. un
f0930 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 ixFile *proxy =
f0940 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b pCtx->lockProxy;
f0950 0a 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f 78 . return prox
f0960 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 68 65 y->pMethod->xChe
f0970 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 28 ckReservedLock((
f0980 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 sqlite3_file*)pr
f0990 6f 78 79 2c 20 70 52 65 73 4f 75 74 29 3b 0a 20 oxy, pResOut);.
f09a0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
f09b0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 }../*.** Lock th
f09c0 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 e file with the
f09d0 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 lock specified b
f09e0 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b y parameter lock
f09f0 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 type - one.** of
f0a00 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
f0a10 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 **.** (1) SH
f0a20 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 ARED_LOCK.**
f0a30 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f (2) RESERVED_LO
f0a40 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 CK.** (3) PE
f0a50 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 NDING_LOCK.**
f0a60 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f (4) EXCLUSIVE_
f0a70 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 LOCK.**.** Somet
f0a80 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 imes when reques
f0a90 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 ting one lock st
f0aa0 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 ate, additional
f0ab0 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 lock states.** a
f0ac0 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 re inserted in b
f0ad0 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 etween. The loc
f0ae0 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 king might fail
f0af0 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 on one of the la
f0b00 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f ter.** transitio
f0b10 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c ns leaving the l
f0b20 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 ock state differ
f0b30 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 ent from what it
f0b40 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 started but.**
f0b50 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 still short of i
f0b60 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f ts goal. The fo
f0b70 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 llowing chart sh
f0b80 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a ows the allowed.
f0b90 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 ** transitions a
f0ba0 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 nd the inserted
f0bb0 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 intermediate sta
f0bc0 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e tes:.**.** UN
f0bd0 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 LOCKED -> SHARED
f0be0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
f0bf0 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 RESERVED.**
f0c00 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 SHARED -> (PENDI
f0c10 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
f0c20 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 .** RESERVED
f0c30 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
f0c40 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
f0c50 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 PENDING -> EXCLU
f0c60 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 SIVE.**.** This
f0c70 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c routine will onl
f0c80 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 y increase a loc
f0c90 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 k. Use the sqli
f0ca0 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a te3OsUnlock().**
f0cb0 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 routine to lowe
f0cc0 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 r a locking leve
f0cd0 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 l..*/.static int
f0ce0 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 proxyLock(sqlit
f0cf0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
f0d00 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 locktype) {. u
f0d10 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
f0d20 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
f0d30 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79 int rc = proxy
f0d40 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 TakeConch(pFile)
f0d50 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
f0d60 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f TE_OK ){. pro
f0d70 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
f0d80 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c *pCtx = (proxyL
f0d90 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 ockingContext *)
f0da0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
f0db0 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 ntext;. unixF
f0dc0 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74 ile *proxy = pCt
f0dd0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 x->lockProxy;.
f0de0 20 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e 70 4d rc = proxy->pM
f0df0 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 ethod->xLock((sq
f0e00 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78 lite3_file*)prox
f0e10 79 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 y, locktype);.
f0e20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
f0e30 65 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74 e = proxy->lockt
f0e40 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ype;. }. retur
f0e50 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 n rc;.}.../*.**
f0e60 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e Lower the lockin
f0e70 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 g level on file
f0e80 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 descriptor pFile
f0e90 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c to locktype. l
f0ea0 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 ocktype.** must
f0eb0 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 be either NO_LOC
f0ec0 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b K or SHARED_LOCK
f0ed0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c ..**.** If the l
f0ee0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 ocking level of
f0ef0 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
f0f00 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 tor is already a
f0f10 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 t or below.** th
f0f20 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b e requested lock
f0f30 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 ing level, this
f0f40 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d routine is a no-
f0f50 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e op..*/.static in
f0f60 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 71 t proxyUnlock(sq
f0f70 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
f0f80 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a int locktype) {.
f0f90 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
f0fa0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
f0fb0 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 d;. int rc = pr
f0fc0 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 oxyTakeConch(pFi
f0fd0 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 le);. if( rc==S
f0fe0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
f0ff0 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
f1000 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f ext *pCtx = (pro
f1010 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
f1020 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *)pFile->lockin
f1030 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e gContext;. un
f1040 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 ixFile *proxy =
f1050 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b pCtx->lockProxy;
f1060 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d . rc = proxy-
f1070 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 >pMethod->xUnloc
f1080 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a k((sqlite3_file*
f1090 29 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65 )proxy, locktype
f10a0 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f );. pFile->lo
f10b0 63 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e cktype = proxy->
f10c0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 locktype;. }.
f10d0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
f10e0 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 .** Close a file
f10f0 20 74 68 61 74 20 75 73 65 73 20 70 72 6f 78 79 that uses proxy
f1100 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 locks..*/.stati
f1110 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 c int proxyClose
f1120 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
f1130 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b d) {. if( id ){
f1140 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 . unixFile *p
f1150 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
f1160 2a 29 69 64 3b 0a 20 20 20 20 70 72 6f 78 79 4c *)id;. proxyL
f1170 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 ockingContext *p
f1180 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b Ctx = (proxyLock
f1190 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 ingContext *)pFi
f11a0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
f11b0 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 xt;. unixFile
f11c0 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43 *lockProxy = pC
f11d0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 tx->lockProxy;.
f11e0 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e unixFile *con
f11f0 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 chFile = pCtx->c
f1200 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 69 6e onchFile;. in
f1210 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
f1220 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 6c ;. . if( l
f1230 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 ockProxy ){.
f1240 20 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 rc = lockProxy
f1250 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f ->pMethod->xUnlo
f1260 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck((sqlite3_file
f1270 2a 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e 4f 5f *)lockProxy, NO_
f1280 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 LOCK);. if(
f1290 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
f12a0 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b . rc = lock
f12b0 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e Proxy->pMethod->
f12c0 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f xClose((sqlite3_
f12d0 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 file*)lockProxy)
f12e0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
f12f0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
f1300 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c sqlite3_free(l
f1310 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 ockProxy);.
f1320 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 pCtx->lockProxy
f1330 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
f1340 69 66 28 20 63 6f 6e 63 68 46 69 6c 65 20 29 7b if( conchFile ){
f1350 0a 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d . if( pCtx-
f1360 3e 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a 20 20 >conchHeld ){.
f1370 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 rc = proxy
f1380 52 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 46 69 ReleaseConch(pFi
f1390 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 le);. if(
f13a0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
f13b0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
f13c0 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 c = conchFile->p
f13d0 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 Method->xClose((
f13e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f sqlite3_file*)co
f13f0 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 nchFile);.
f1400 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
f1410 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 rc;. sqlite
f1420 33 5f 66 72 65 65 28 63 6f 6e 63 68 46 69 6c 65 3_free(conchFile
f1430 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
f1440 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e ite3_free(pCtx->
f1450 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 29 3b 0a lockProxyPath);.
f1460 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
f1470 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 (pCtx->conchFile
f1480 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 Path);. sqlit
f1490 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 64 62 e3_free(pCtx->db
f14a0 50 61 74 68 29 3b 0a 20 20 20 20 2f 2a 20 72 65 Path);. /* re
f14b0 73 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69 6e store the origin
f14c0 61 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 al locking conte
f14d0 78 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 xt and pMethod t
f14e0 68 65 6e 20 63 6c 6f 73 65 20 69 74 20 2a 2f 0a hen close it */.
f14f0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 pFile->locki
f1500 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 ngContext = pCtx
f1510 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 ->oldLockingCont
f1520 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e ext;. pFile->
f1530 70 4d 65 74 68 6f 64 20 3d 20 70 43 74 78 2d 3e pMethod = pCtx->
f1540 70 4f 6c 64 4d 65 74 68 6f 64 3b 0a 20 20 20 20 pOldMethod;.
f1550 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 sqlite3_free(pCt
f1560 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 x);. return p
f1570 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 File->pMethod->x
f1580 43 6c 6f 73 65 28 69 64 29 3b 0a 20 20 7d 0a 20 Close(id);. }.
f1590 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
f15a0 4b 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64 69 66 20 2f K;.}....#endif /
f15b0 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c * defined(__APPL
f15c0 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 E__) && SQLITE_E
f15d0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
f15e0 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 YLE */./*.** The
f15f0 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 73 proxy locking s
f1600 74 79 6c 65 20 69 73 20 69 6e 74 65 6e 64 65 64 tyle is intended
f1610 20 66 6f 72 20 75 73 65 20 77 69 74 68 20 41 46 for use with AF
f1620 50 20 66 69 6c 65 73 79 73 74 65 6d 73 2e 0a 2a P filesystems..*
f1630 2a 20 41 6e 64 20 73 69 6e 63 65 20 41 46 50 20 * And since AFP
f1640 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 is only supporte
f1650 64 20 6f 6e 20 4d 61 63 4f 53 58 2c 20 74 68 65 d on MacOSX, the
f1660 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 proxy locking i
f1670 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 74 72 69 s also.** restri
f1680 63 74 65 64 20 74 6f 20 4d 61 63 4f 53 58 2e 0a cted to MacOSX..
f1690 2a 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ** .**.*********
f16a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
f16b0 66 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b f the proxy lock
f16c0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
f16d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f16e0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
f16f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1730 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e *****/../*.** In
f1740 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 itialize the ope
f1750 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e rating system in
f1760 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 terface..**.** T
f1770 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 69 his routine regi
f1780 73 74 65 72 73 20 61 6c 6c 20 56 46 53 20 69 6d sters all VFS im
f1790 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f plementations fo
f17a0 72 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 r unix-like oper
f17b0 61 74 69 6e 67 0a 2a 2a 20 73 79 73 74 65 6d 73 ating.** systems
f17c0 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 2c . This routine,
f17d0 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 and the sqlite3
f17e0 5f 6f 73 5f 65 6e 64 28 29 20 72 6f 75 74 69 6e _os_end() routin
f17f0 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2c 0a e that follows,.
f1800 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 ** should be the
f1810 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 20 69 only routines i
f1820 6e 20 74 68 69 73 20 66 69 6c 65 20 74 68 61 74 n this file that
f1830 20 61 72 65 20 76 69 73 69 62 6c 65 20 66 72 6f are visible fro
f1840 6d 20 6f 74 68 65 72 0a 2a 2a 20 66 69 6c 65 73 m other.** files
f1850 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
f1860 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f tine is called o
f1870 6e 63 65 20 64 75 72 69 6e 67 20 53 51 4c 69 74 nce during SQLit
f1880 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e e initialization
f1890 20 61 6e 64 20 62 79 20 61 0a 2a 2a 20 73 69 6e and by a.** sin
f18a0 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 gle thread. The
f18b0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
f18c0 6f 6e 20 61 6e 64 20 6d 75 74 65 78 20 73 75 62 on and mutex sub
f18d0 73 79 73 74 65 6d 73 20 68 61 76 65 20 6e 6f 74 systems have not
f18e0 0a 2a 2a 20 6e 65 63 65 73 73 61 72 69 6c 79 20 .** necessarily
f18f0 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 been initialized
f1900 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 when this routi
f1910 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 6e ne is called, an
f1920 64 20 73 6f 20 74 68 65 79 0a 2a 2a 20 73 68 6f d so they.** sho
f1930 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 2e uld not be used.
f1940 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
f1950 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e nt sqlite3_os_in
f1960 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 it(void){ . /*
f1970 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 . ** The follow
f1980 69 6e 67 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 ing macro define
f1990 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 72 s an initializer
f19a0 20 66 6f 72 20 61 6e 20 73 71 6c 69 74 65 33 5f for an sqlite3_
f19b0 76 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a vfs object.. **
f19c0 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 The name of the
f19d0 20 56 46 53 20 69 73 20 4e 41 4d 45 2e 20 20 54 VFS is NAME. T
f19e0 68 65 20 70 41 70 70 44 61 74 61 20 69 73 20 61 he pAppData is a
f19f0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 6f pointer to a po
f1a00 69 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 inter. ** to th
f1a10 65 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 e "finder" funct
f1a20 69 6f 6e 2e 20 20 28 70 41 70 70 44 61 74 61 20 ion. (pAppData
f1a30 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
f1a40 61 20 70 6f 69 6e 74 65 72 20 62 65 63 61 75 73 a pointer becaus
f1a50 65 0a 20 20 2a 2a 20 73 69 6c 6c 79 20 43 39 30 e. ** silly C90
f1a60 20 72 75 6c 65 73 20 70 72 6f 68 69 62 69 74 20 rules prohibit
f1a70 61 20 76 6f 69 64 2a 20 66 72 6f 6d 20 62 65 69 a void* from bei
f1a80 6e 67 20 63 61 73 74 20 74 6f 20 61 20 66 75 6e ng cast to a fun
f1a90 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 0a 20 20 ction pointer.
f1aa0 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 68 61 76 ** and so we hav
f1ab0 65 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 20 e to go through
f1ac0 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 the intermediate
f1ad0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 76 6f 69 pointer to avoi
f1ae0 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 d problems. **
f1af0 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 77 when compiling w
f1b00 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 ith -pedantic-er
f1b10 72 6f 72 73 20 6f 6e 20 47 43 43 2e 29 0a 20 20 rors on GCC.).
f1b20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 46 49 4e 44 **. ** The FIND
f1b30 45 52 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 ER parameter to
f1b40 74 68 69 73 20 6d 61 63 72 6f 20 69 73 20 74 68 this macro is th
f1b50 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 70 6f e name of the po
f1b60 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 20 20 2a inter to the. *
f1b70 2a 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f * finder-functio
f1b80 6e 2e 20 20 54 68 65 20 66 69 6e 64 65 72 2d 66 n. The finder-f
f1b90 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
f1ba0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
f1bb0 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 69 6f 5f . ** sqlite_io_
f1bc0 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 74 methods object t
f1bd0 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 hat implements t
f1be0 68 65 20 64 65 73 69 72 65 64 20 6c 6f 63 6b 69 he desired locki
f1bf0 6e 67 0a 20 20 2a 2a 20 62 65 68 61 76 69 6f 72 ng. ** behavior
f1c00 73 2e 20 20 53 65 65 20 74 68 65 20 64 69 76 69 s. See the divi
f1c10 73 69 6f 6e 20 61 62 6f 76 65 20 74 68 61 74 20 sion above that
f1c20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4f 4d contains the IOM
f1c30 45 54 48 4f 44 53 0a 20 20 2a 2a 20 6d 61 63 72 ETHODS. ** macr
f1c40 6f 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 20 69 o for addition i
f1c50 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 66 69 nformation on fi
f1c60 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 73 2e 0a nder-functions..
f1c70 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 73 74 20 66 **. ** Most f
f1c80 69 6e 64 65 72 73 20 73 69 6d 70 6c 79 20 72 65 inders simply re
f1c90 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
f1ca0 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 74 65 o a fixed sqlite
f1cb0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 20 20 2a 3_io_methods. *
f1cc0 2a 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 74 * object. But t
f1cd0 68 65 20 22 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 he "autolockIoFi
f1ce0 6e 64 65 72 22 20 61 76 61 69 6c 61 62 6c 65 20 nder" available
f1cf0 6f 6e 20 4d 61 63 4f 53 58 20 64 6f 65 73 20 61 on MacOSX does a
f1d00 20 6c 69 74 74 6c 65 0a 20 20 2a 2a 20 6d 6f 72 little. ** mor
f1d10 65 20 74 68 61 6e 20 74 68 61 74 3b 20 69 74 20 e than that; it
f1d20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c looks at the fil
f1d30 65 73 79 73 74 65 6d 20 74 79 70 65 20 74 68 61 esystem type tha
f1d40 74 20 68 6f 73 74 73 20 74 68 65 20 0a 20 20 2a t hosts the . *
f1d50 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 * database file
f1d60 61 6e 64 20 74 72 69 65 73 20 74 6f 20 63 68 6f and tries to cho
f1d70 6f 73 65 20 61 6e 20 6c 6f 63 6b 69 6e 67 20 6d ose an locking m
f1d80 65 74 68 6f 64 20 61 70 70 72 6f 70 72 69 61 74 ethod appropriat
f1d90 65 20 66 6f 72 0a 20 20 2a 2a 20 74 68 61 74 20 e for. ** that
f1da0 66 69 6c 65 73 79 73 74 65 6d 20 74 69 6d 65 2e filesystem time.
f1db0 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 . */. #define
f1dc0 55 4e 49 58 56 46 53 28 56 46 53 4e 41 4d 45 2c UNIXVFS(VFSNAME,
f1dd0 20 46 49 4e 44 45 52 29 20 7b 20 20 20 20 20 20 FINDER) {
f1de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f1df0 20 20 5c 0a 20 20 20 20 31 2c 20 20 20 20 20 20 \. 1,
f1e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f1e10 20 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20 iVersion */
f1e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f1e30 5c 0a 20 20 20 20 73 69 7a 65 6f 66 28 75 6e 69 \. sizeof(uni
f1e40 78 46 69 6c 65 29 2c 20 20 20 20 20 2f 2a 20 73 xFile), /* s
f1e50 7a 4f 73 46 69 6c 65 20 2a 2f 20 20 20 20 20 20 zOsFile */
f1e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
f1e70 20 20 20 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 MAX_PATHNAME
f1e80 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 , /* mxP
f1e90 61 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20 20 athname */
f1ea0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
f1eb0 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
f1ec0 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 /* pNext
f1ed0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f1ee0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
f1ef0 56 46 53 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 VFSNAME,
f1f00 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a /* zName *
f1f10 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
f1f20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 28 76 \. (v
f1f30 6f 69 64 2a 29 26 46 49 4e 44 45 52 2c 20 20 20 oid*)&FINDER,
f1f40 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 /* pAppData
f1f50 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f1f60 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 \. unix
f1f70 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 Open,
f1f80 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 20 20 20 /* xOpen */
f1f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f1fa0 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 65 \. unixDe
f1fb0 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 lete,
f1fc0 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f 20 20 20 /* xDelete */
f1fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f1fe0 20 20 5c 0a 20 20 20 20 75 6e 69 78 41 63 63 65 \. unixAcce
f1ff0 73 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ss, /*
f2000 20 78 41 63 63 65 73 73 20 2a 2f 20 20 20 20 20 xAccess */
f2010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2020 5c 0a 20 20 20 20 75 6e 69 78 46 75 6c 6c 50 61 \. unixFullPa
f2030 74 68 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 78 thname, /* x
f2040 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 20 FullPathname */
f2050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
f2060 20 20 20 20 75 6e 69 78 44 6c 4f 70 65 6e 2c 20 unixDlOpen,
f2070 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c /* xDl
f2080 4f 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 Open */
f2090 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
f20a0 20 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c 20 20 unixDlError,
f20b0 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 /* xDlEr
f20c0 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ror */
f20d0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
f20e0 75 6e 69 78 44 6c 53 79 6d 2c 20 20 20 20 20 20 unixDlSym,
f20f0 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 /* xDlSym
f2100 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f2110 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e \. un
f2120 69 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 ixDlClose,
f2130 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 /* xDlClose
f2140 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f2150 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 \. unix
f2160 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 Randomness,
f2170 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 /* xRandomness
f2180 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f2190 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53 6c \. unixSl
f21a0 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 eep,
f21b0 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 20 20 20 20 /* xSleep */
f21c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f21d0 20 20 5c 0a 20 20 20 20 75 6e 69 78 43 75 72 72 \. unixCurr
f21e0 65 6e 74 54 69 6d 65 2c 20 20 20 20 20 20 2f 2a entTime, /*
f21f0 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f xCurrentTime */
f2200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2210 5c 0a 20 20 20 20 75 6e 69 78 47 65 74 4c 61 73 \. unixGetLas
f2220 74 45 72 72 6f 72 20 20 20 20 20 20 2f 2a 20 78 tError /* x
f2230 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 20 GetLastError */
f2240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
f2250 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 }.. /*. ** A
f2260 6c 6c 20 64 65 66 61 75 6c 74 20 56 46 53 65 73 ll default VFSes
f2270 20 66 6f 72 20 75 6e 69 78 20 61 72 65 20 63 6f for unix are co
f2280 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 ntained in the f
f2290 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 2e 0a ollowing array..
f22a0 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 **. ** Note t
f22b0 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f hat the sqlite3_
f22c0 76 66 73 2e 70 4e 65 78 74 20 66 69 65 6c 64 20 vfs.pNext field
f22d0 6f 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 63 of the VFS objec
f22e0 74 20 69 73 20 6d 6f 64 69 66 69 65 64 0a 20 20 t is modified.
f22f0 2a 2a 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 ** by the SQLite
f2300 20 63 6f 72 65 20 77 68 65 6e 20 74 68 65 20 56 core when the V
f2310 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64 FS is registered
f2320 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 . So the follow
f2330 69 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79 20 63 ing. ** array c
f2340 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 2e 0a annot be const..
f2350 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 73 71 */. static sq
f2360 6c 69 74 65 33 5f 76 66 73 20 61 56 66 73 5b 5d lite3_vfs aVfs[]
f2370 20 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f = {.#if SQLITE_
f2380 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
f2390 54 59 4c 45 20 26 26 20 28 4f 53 5f 56 58 57 4f TYLE && (OS_VXWO
f23a0 52 4b 53 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f RKS || defined(_
f23b0 5f 41 50 50 4c 45 5f 5f 29 29 0a 20 20 20 20 55 _APPLE__)). U
f23c0 4e 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20 NIXVFS("unix",
f23d0 20 20 20 20 20 20 20 20 61 75 74 6f 6c 6f 63 6b autolock
f23e0 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6c 73 IoFinder ),.#els
f23f0 65 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 e. UNIXVFS("u
f2400 6e 69 78 22 2c 20 20 20 20 20 20 20 20 20 20 70 nix", p
f2410 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a osixIoFinder ),.
f2420 23 65 6e 64 69 66 0a 20 20 20 20 55 4e 49 58 56 #endif. UNIXV
f2430 46 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20 FS("unix-none",
f2440 20 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 nolockIoFind
f2450 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 er ),. UNIXVF
f2460 53 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65 22 S("unix-dotfile"
f2470 2c 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 , dotlockIoFind
f2480 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 er ),. UNIXVF
f2490 53 28 22 75 6e 69 78 2d 77 66 6c 22 2c 20 20 20 S("unix-wfl",
f24a0 20 20 20 70 6f 73 69 78 57 66 6c 49 6f 46 69 6e posixWflIoFin
f24b0 64 65 72 20 29 2c 0a 23 69 66 20 4f 53 5f 56 58 der ),.#if OS_VX
f24c0 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46 WORKS. UNIXVF
f24d0 53 28 22 75 6e 69 78 2d 6e 61 6d 65 64 73 65 6d S("unix-namedsem
f24e0 22 2c 20 73 65 6d 49 6f 46 69 6e 64 65 72 20 29 ", semIoFinder )
f24f0 2c 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c ,.#endif.#if SQL
f2500 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
f2510 4e 47 5f 53 54 59 4c 45 0a 20 20 20 20 55 4e 49 NG_STYLE. UNI
f2520 58 56 46 53 28 22 75 6e 69 78 2d 70 6f 73 69 78 XVFS("unix-posix
f2530 22 2c 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e ", posixIoFin
f2540 64 65 72 20 29 2c 0a 23 69 66 20 21 4f 53 5f 56 der ),.#if !OS_V
f2550 58 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 XWORKS. UNIXV
f2560 46 53 28 22 75 6e 69 78 2d 66 6c 6f 63 6b 22 2c FS("unix-flock",
f2570 20 20 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 flockIoFinde
f2580 72 20 29 2c 0a 23 65 6e 64 69 66 0a 23 65 6e 64 r ),.#endif.#end
f2590 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e if.#if SQLITE_EN
f25a0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
f25b0 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f LE && defined(__
f25c0 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20 55 4e 49 APPLE__). UNI
f25d0 58 56 46 53 28 22 75 6e 69 78 2d 61 66 70 22 2c XVFS("unix-afp",
f25e0 20 20 20 20 20 20 61 66 70 49 6f 46 69 6e 64 65 afpIoFinde
f25f0 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 r ),. UNIXVFS
f2600 28 22 75 6e 69 78 2d 70 72 6f 78 79 22 2c 20 20 ("unix-proxy",
f2610 20 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 20 proxyIoFinder
f2620 29 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20 ),.#endif. };.
f2630 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b unsigned int i;
f2640 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
f2650 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 p counter */..
f2660 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 20 /* Register all
f2670 56 46 53 65 73 20 64 65 66 69 6e 65 64 20 69 6e VFSes defined in
f2680 20 74 68 65 20 61 56 66 73 5b 5d 20 61 72 72 61 the aVfs[] arra
f2690 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 y */. for(i=0;
f26a0 69 3c 28 73 69 7a 65 6f 66 28 61 56 66 73 29 2f i<(sizeof(aVfs)/
f26b0 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 sizeof(sqlite3_v
f26c0 66 73 29 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 fs)); i++){.
f26d0 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 sqlite3_vfs_regi
f26e0 73 74 65 72 28 26 61 56 66 73 5b 69 5d 2c 20 69 ster(&aVfs[i], i
f26f0 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ==0);. }. retu
f2700 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d rn SQLITE_OK; .}
f2710 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e ../*.** Shutdown
f2720 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 the operating s
f2730 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e ystem interface.
f2740 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 70 65 72 .**.** Some oper
f2750 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20 6d 69 ating systems mi
f2760 67 68 74 20 6e 65 65 64 20 74 6f 20 64 6f 20 73 ght need to do s
f2770 6f 6d 65 20 63 6c 65 61 6e 75 70 20 69 6e 20 74 ome cleanup in t
f2780 68 69 73 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20 his routine,.**
f2790 74 6f 20 72 65 6c 65 61 73 65 20 64 79 6e 61 6d to release dynam
f27a0 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 ically allocated
f27b0 20 6f 62 6a 65 63 74 73 2e 20 20 42 75 74 20 6e objects. But n
f27c0 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a 2a 2a 20 54 ot on unix..** T
f27d0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
f27e0 20 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e 69 78 2e no-op for unix.
f27f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
f2800 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e nt sqlite3_os_en
f2810 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75 d(void){ . retu
f2820 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d rn SQLITE_OK; .}
f2830 0a 20 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c . .#endif /* SQL
f2840 49 54 45 5f 4f 53 5f 55 4e 49 58 20 2a 2f 0a 0a ITE_OS_UNIX */..
f2850 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
f2860 45 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 End of os_unix.c
f2870 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
f2880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
f28a0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
f28b0 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 77 69 Begin file os_wi
f28c0 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.c ************
f28d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f28e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
f28f0 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 /*.** 2004 May 2
f2900 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 2.**.** The auth
f2910 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
f2920 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
f2930 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
f2940 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
f2950 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
f2960 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
f2970 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
f2980 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
f2990 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
f29a0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
f29b0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
f29c0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
f29d0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
f29e0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
f29f0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
f2a00 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
f2a10 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
f2a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2a60 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
f2a70 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
f2a80 6f 64 65 20 74 68 61 74 20 69 73 20 73 70 65 63 ode that is spec
f2a90 69 66 69 63 20 74 6f 20 77 69 6e 64 6f 77 73 2e ific to windows.
f2aa0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
f2ab0 53 5f 57 49 4e 20 20 20 20 20 20 20 20 20 20 20 S_WIN
f2ac0 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65 /* This file
f2ad0 20 69 73 20 75 73 65 64 20 66 6f 72 20 77 69 6e is used for win
f2ae0 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f dows only */.../
f2af0 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75 *.** A Note Abou
f2b00 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 t Memory Allocat
f2b10 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ion:.**.** This
f2b20 64 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c 6c driver uses mall
f2b30 6f 63 28 29 2f 66 72 65 65 28 29 20 64 69 72 65 oc()/free() dire
f2b40 63 74 6c 79 20 72 61 74 68 65 72 20 74 68 61 6e ctly rather than
f2b50 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a going through.*
f2b60 2a 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72 61 * the SQLite-wra
f2b70 70 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d 61 ppers sqlite3_ma
f2b80 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f 66 lloc()/sqlite3_f
f2b90 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77 72 ree(). Those wr
f2ba0 61 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64 65 appers.** are de
f2bb0 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 6f signed for use o
f2bc0 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 n embedded syste
f2bd0 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79 20 ms where memory
f2be0 69 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a is scarce and.**
f2bf0 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 malloc failures
f2c00 20 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e 74 happen frequent
f2c10 6c 79 2e 20 20 57 69 6e 33 32 20 64 6f 65 73 20 ly. Win32 does
f2c20 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75 not typically ru
f2c30 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 n on.** embedded
f2c40 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68 systems, and wh
f2c50 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64 en it does the d
f2c60 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c evelopers normal
f2c70 6c 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a ly have bigger.*
f2c80 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f * problems to wo
f2c90 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72 rry about than r
f2ca0 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65 unning out of me
f2cb0 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20 mory. So there
f2cc0 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70 is not.** a comp
f2cd0 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75 elling need to u
f2ce0 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e se the wrappers.
f2cf0 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65 .**.** But there
f2d00 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f is a good reaso
f2d10 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 n to not use the
f2d20 20 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77 wrappers. If w
f2d30 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61 e use the.** wra
f2d40 70 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69 ppers then we wi
f2d50 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64 ll get simulated
f2d60 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 malloc() failur
f2d70 65 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a es within this.*
f2d80 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74 * driver. And t
f2d90 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b hat causes all k
f2da0 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73 inds of problems
f2db0 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20 for our tests.
f2dc0 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 We.** could enh
f2dd0 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64 ance SQLite to d
f2de0 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74 eal with simulat
f2df0 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 ed malloc failur
f2e00 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 es within.** the
f2e10 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20 OS driver, but
f2e20 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c the code to deal
f2e30 20 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c with those fail
f2e40 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a ure would not.**
f2e50 20 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e be exercised on
f2e60 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f Linux (which do
f2e70 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d es not need to m
f2e80 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64 alloc() in the d
f2e90 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f river).** and so
f2ea0 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64 we would have d
f2eb0 69 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e ifficulty writin
f2ec0 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73 g coverage tests
f2ed0 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64 for that.** cod
f2ee0 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65 e. Better to le
f2ef0 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74 ave the code out
f2f00 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a , we think..**.*
f2f10 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 * The point of t
f2f20 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69 his discussion i
f2f30 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57 s as follows: W
f2f40 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e hen creating a n
f2f50 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66 ew.** OS layer f
f2f60 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73 or an embedded s
f2f70 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73 ystem, if you us
f2f80 65 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61 e this file as a
f2f90 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76 n example,.** av
f2fa0 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d oid the use of m
f2fb0 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20 alloc()/free().
f2fc0 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 Those routines
f2fd0 77 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f work ok on windo
f2fe0 77 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 ws.** desktops b
f2ff0 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 ut not so well i
f3000 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 n embedded syste
f3010 6d 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 ms..*/..#include
f3020 20 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a 23 69 <winbase.h>..#i
f3030 66 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a fdef __CYGWIN__.
f3040 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 63 # include <sys/c
f3050 79 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a ygwin.h>.#endif.
f3060 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 ./*.** Macros us
f3070 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ed to determine
f3080 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
f3090 6f 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a o use threads..*
f30a0 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 /.#if defined(TH
f30b0 52 45 41 44 53 41 46 45 29 20 26 26 20 54 48 52 READSAFE) && THR
f30c0 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 EADSAFE.# define
f30d0 20 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45 SQLITE_W32_THRE
f30e0 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a ADS 1.#endif../*
f30f0 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 .** Include code
f3100 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 that is common
f3110 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 to all os_*.c fi
f3120 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a les.*/./********
f3130 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f ****** Include o
f3140 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 s_common.h in th
f3150 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 77 e middle of os_w
f3160 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a in.c ***********
f3170 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
f3180 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
f3190 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a e os_common.h **
f31a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f31b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f31c0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
f31d0 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 4 May 22.**.** T
f31e0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
f31f0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
f3200 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
f3210 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
f3220 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
f3230 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
f3240 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
f3250 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
f3260 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
f3270 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
f3280 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
f3290 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
f32a0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
f32b0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
f32c0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
f32d0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
f32e0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
f32f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
f3340 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
f3350 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 tains macros and
f3360 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 a little bit of
f3370 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f code that is co
f3380 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f mmon to.** all o
f3390 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 f the platform-s
f33a0 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f pecific files (o
f33b0 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 s_*.c) and is #i
f33c0 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f ncluded into tho
f33d0 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a se.** files..**.
f33e0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f ** This file sho
f33f0 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 uld be #included
f3400 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 by the os_*.c f
f3410 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 iles only. It i
f3420 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 s not a.** gener
f3430 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 al purpose heade
f3440 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 r file..**.** $I
f3450 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 d: os_common.h,v
f3460 20 31 2e 33 38 20 32 30 30 39 2f 30 32 2f 32 34 1.38 2009/02/24
f3470 20 31 38 3a 34 30 3a 35 30 20 64 61 6e 69 65 6c 18:40:50 daniel
f3480 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 k1977 Exp $.*/.#
f3490 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f ifndef _OS_COMMO
f34a0 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 N_H_.#define _OS
f34b0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a _COMMON_H_../*.*
f34c0 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62 * At least two b
f34d0 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 64 ugs have slipped
f34e0 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 in because we c
f34f0 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 hanged the MEMOR
f3500 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f Y_DEBUG.** macro
f3510 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 to SQLITE_DEBUG
f3520 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 and some older
f3530 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e makefiles have n
f3540 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a ot yet made the.
f3550 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 20 ** switch. The
f3560 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 following code s
f3570 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 73 hould catch this
f3580 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 problem at comp
f3590 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 ile-time..*/.#if
f35a0 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 def MEMORY_DEBUG
f35b0 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 .# error "The ME
f35c0 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f MORY_DEBUG macro
f35d0 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 is obsolete. U
f35e0 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 se SQLITE_DEBUG
f35f0 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 instead.".#endif
f3600 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
f3610 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 DEBUG.SQLITE_PRI
f3620 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
f3630 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 OSTrace = 0;.#de
f3640 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 fine OSTRACE1(X)
f3650 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c if( sql
f3660 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
f3670 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
f3680 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 (X).#define OSTR
f3690 41 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 ACE2(X,Y)
f36a0 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
f36b0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
f36c0 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 gPrintf(X,Y).#de
f36d0 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c fine OSTRACE3(X,
f36e0 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c Y,Z) if( sql
f36f0 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
f3700 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
f3710 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 (X,Y,Z).#define
f3720 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 OSTRACE4(X,Y,Z,A
f3730 29 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f ) if( sqlite3O
f3740 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
f3750 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
f3760 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 Z,A).#define OST
f3770 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 RACE5(X,Y,Z,A,B)
f3780 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 if( sqlite3OSTr
f3790 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 ace ) sqlite3Deb
f37a0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 ugPrintf(X,Y,Z,A
f37b0 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,B).#define OSTR
f37c0 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 ACE6(X,Y,Z,A,B,C
f37d0 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 ) \. if(sqlit
f37e0 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 e3OSTrace) sqlit
f37f0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
f3800 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 Y,Z,A,B,C).#defi
f3810 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c ne OSTRACE7(X,Y,
f3820 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 Z,A,B,C,D) \.
f3830 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 if(sqlite3OSTra
f3840 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 ce) sqlite3Debug
f3850 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 Printf(X,Y,Z,A,B
f3860 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 ,C,D).#else.#def
f3870 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a ine OSTRACE1(X).
f3880 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 #define OSTRACE2
f3890 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 (X,Y).#define OS
f38a0 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 TRACE3(X,Y,Z).#d
f38b0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 efine OSTRACE4(X
f38c0 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 ,Y,Z,A).#define
f38d0 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 OSTRACE5(X,Y,Z,A
f38e0 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,B).#define OSTR
f38f0 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 ACE6(X,Y,Z,A,B,C
f3900 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
f3910 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 E7(X,Y,Z,A,B,C,D
f3920 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
f3930 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f Macros for perfo
f3940 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 rmance tracing.
f3950 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 Normally turned
f3960 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b off. Only work
f3970 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 s.** on i486 har
f3980 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 dware..*/.#ifdef
f3990 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 SQLITE_PERFORMA
f39a0 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a NCE_TRACE../* .*
f39b0 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 * hwtime.h conta
f39c0 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d ins inline assem
f39d0 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d bler code for im
f39e0 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 plementing .** h
f39f0 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 igh-performance
f3a00 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e timing routines.
f3a10 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .*/./***********
f3a20 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 *** Include hwti
f3a30 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 me.h in the midd
f3a40 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e le of os_common.
f3a50 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
f3a60 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
f3a70 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 *** Begin file h
f3a80 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a wtime.h ********
f3a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3ab0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d **/./*.** 2008 M
f3ac0 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ay 27.**.** The
f3ad0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
f3ae0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
f3af0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
f3b00 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
f3b10 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
f3b20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
f3b30 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
f3b40 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
f3b50 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
f3b60 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
f3b70 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
f3b80 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
f3b90 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
f3ba0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
f3bb0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
f3bc0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
f3bd0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
f3be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
f3c30 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
f3c40 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f ns inline asm co
f3c50 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e de for retrievin
f3c60 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 g "high-performa
f3c70 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 nce".** counters
f3c80 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 for x86 class C
f3c90 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 PUs..**.** $Id:
f3ca0 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 hwtime.h,v 1.3 2
f3cb0 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 008/08/01 14:33:
f3cc0 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 15 shane Exp $.*
f3cd0 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d /.#ifndef _HWTIM
f3ce0 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 E_H_.#define _HW
f3cf0 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 TIME_H_../*.** T
f3d00 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 he following rou
f3d10 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 tine only works
f3d20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 on pentium-class
f3d30 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 (or newer) proc
f3d40 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 essors..** It us
f3d50 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 es the RDTSC opc
f3d60 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 ode to read the
f3d70 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 cycle count valu
f3d80 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 e out of the.**
f3d90 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 processor and re
f3da0 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 turns that value
f3db0 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 . This can be u
f3dc0 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 sed for high-res
f3dd0 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a .** profiling..*
f3de0 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f /.#if (defined(_
f3df0 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 _GNUC__) || defi
f3e00 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 ned(_MSC_VER)) &
f3e10 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e & \. (defin
f3e20 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 ed(i386) || defi
f3e30 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c ned(__i386__) ||
f3e40 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 defined(_M_IX86
f3e50 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 )).. #if define
f3e60 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f d(__GNUC__).. _
f3e70 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 _inline__ sqlite
f3e80 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 _uint64 sqlite3H
f3e90 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 wtime(void){.
f3ea0 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c unsigned int l
f3eb0 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 o, hi;. __as
f3ec0 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f m__ __volatile__
f3ed0 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 ("rdtsc" : "=a"
f3ee0 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 (lo), "=d" (hi)
f3ef0 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 );. return (
f3f00 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 sqlite_uint64)hi
f3f10 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d << 32 | lo;. }
f3f20 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 .. #elif define
f3f30 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f d(_MSC_VER).. _
f3f40 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 _declspec(naked)
f3f50 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 __inline sqlite
f3f60 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 _uint64 __cdecl
f3f70 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
f3f80 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 id){. __asm
f3f90 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a {. rdtsc.
f3fa0 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20 ret
f3fb0 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 ; return value
f3fc0 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 at EDX:EAX.
f3fd0 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 }. }.. #endif
f3fe0 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 ..#elif (defined
f3ff0 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 (__GNUC__) && de
f4000 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f fined(__x86_64__
f4010 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f )).. __inline__
f4020 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
f4030 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
f4040 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e d){. unsign
f4050 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 ed long val;.
f4060 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
f4070 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 atile__ ("rdtsc"
f4080 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a : "=A" (val));.
f4090 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c return val
f40a0 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 ;. }. .#elif (d
f40b0 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 efined(__GNUC__)
f40c0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 && defined(__pp
f40d0 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e c__)).. __inlin
f40e0 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 e__ sqlite_uint6
f40f0 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 4 sqlite3Hwtime(
f4100 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 void){. uns
f4110 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
f4120 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e retval;. un
f4130 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b signed long junk
f4140 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 ;. __asm__
f4150 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c __volatile__ ("\
f4160 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 n\. 1:
f4170 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c mftbu %1\
f4180 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n\.
f4190 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 mftb %L0
f41a0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
f41b0 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 mftbu %0
f41c0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
f41d0 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 cmpw %0
f41e0 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 ,%1\n\.
f41f0 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 bne
f4200 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 1b".
f4210 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 : "=r" (r
f4220 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 etval), "=r" (ju
f4230 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 nk));. retu
f4240 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a rn retval;. }..
f4250 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 #else.. #error
f4260 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 Need implementat
f4270 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 ion of sqlite3Hw
f4280 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 time() for your
f4290 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a platform... /*.
f42a0 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 ** To compile
f42b0 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e without implemen
f42c0 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 ting sqlite3Hwti
f42d0 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c me() for your pl
f42e0 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 atform,. ** you
f42f0 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 can remove the
f4300 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 above #error and
f4310 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 use the followi
f4320 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e ng. ** stub fun
f4330 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c ction. You will
f4340 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 lose timing sup
f4350 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 port for many.
f4360 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 ** of the debugg
f4370 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 ing and testing
f4380 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 utilities, but i
f4390 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a t should at. **
f43a0 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 least compile a
f43b0 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c nd run.. */.SQL
f43c0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 ITE_PRIVATE sq
f43d0 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 lite_uint64 sqli
f43e0 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
f43f0 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 return ((sqlite
f4400 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 _uint64)0); }..#
f4410 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a endif..#endif /*
f4420 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d !defined(_HWTIM
f4430 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a E_H_) */../*****
f4440 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
f4450 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a hwtime.h ******
f4460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4480 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
f4490 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
f44a0 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
f44b0 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d ft off in os_com
f44c0 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a mon.h **********
f44d0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 ********/..stati
f44e0 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 c sqlite_uint64
f44f0 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 g_start;.static
f4500 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f sqlite_uint64 g_
f4510 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 elapsed;.#define
f4520 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 TIMER_START
f4530 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 g_start=sqlit
f4540 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 e3Hwtime().#defi
f4550 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 ne TIMER_END
f4560 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 g_elapsed=s
f4570 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 qlite3Hwtime()-g
f4580 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 _start.#define T
f4590 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 IMER_ELAPSED
f45a0 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 g_elapsed.#else
f45b0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 .#define TIMER_S
f45c0 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d TART.#define TIM
f45d0 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 ER_END.#define T
f45e0 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 IMER_ELAPSED
f45f0 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 ((sqlite_uint64
f4600 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a )0).#endif../*.*
f4610 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 * If we compile
f4620 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f with the SQLITE_
f4630 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 TEST macro set,
f4640 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 then the followi
f4650 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 ng block.** of c
f4660 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 ode will give us
f4670 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 the ability to
f4680 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 simulate a disk
f4690 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 I/O error. This
f46a0 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 .** is used for
f46b0 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 testing the I/O
f46c0 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a recovery logic..
f46d0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
f46e0 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
f46f0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
f4700 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 error_hit = 0;
f4710 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 /* Tot
f4720 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f al number of I/O
f4730 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 Errors */.SQLIT
f4740 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
f4750 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 3_io_error_hardh
f4760 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f it = 0; /
f4770 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d * Number of non-
f4780 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f benign errors */
f4790 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
f47a0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
f47b0 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 _pending = 0;
f47c0 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f /* Count do
f47d0 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 wn to first I/O
f47e0 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f error */.SQLITE_
f47f0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
f4800 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 io_error_persist
f4810 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
f4820 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f True if I/O erro
f4830 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 rs persist */.SQ
f4840 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
f4850 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 ite3_io_error_be
f4860 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 nign = 0;
f4870 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 /* True if err
f4880 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a ors are benign *
f4890 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
f48a0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
f48b0 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 l_pending = 0;.S
f48c0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
f48d0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d lite3_diskfull =
f48e0 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 0;.#define Simu
f48f0 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 lateIOErrorBenig
f4900 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f n(X) sqlite3_io_
f4910 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 error_benign=(X)
f4920 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
f4930 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 eIOError(CODE)
f4940 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 \. if( (sqlite3
f4950 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 _io_error_persis
f4960 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f t && sqlite3_io_
f4970 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 error_hit) \.
f4980 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 || sqlite3_i
f4990 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d o_error_pending-
f49a0 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 - == 1 ) \.
f49b0 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 { loca
f49c0 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b l_ioerr(); CODE;
f49d0 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c }.static void l
f49e0 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 ocal_ioerr(){.
f49f0 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c IOTRACE(("IOERR\
f4a00 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f n"));. sqlite3_
f4a10 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a io_error_hit++;.
f4a20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 if( !sqlite3_i
f4a30 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 o_error_benign )
f4a40 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
f4a50 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 r_hardhit++;.}.#
f4a60 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 define SimulateD
f4a70 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 iskfullError(COD
f4a80 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 E) \. if( sqli
f4a90 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e te3_diskfull_pen
f4aa0 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 ding ){ \. i
f4ab0 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 f( sqlite3_diskf
f4ac0 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 ull_pending == 1
f4ad0 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 ){ \. loc
f4ae0 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 al_ioerr(); \.
f4af0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 sqlite3_dis
f4b00 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 kfull = 1; \.
f4b10 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 sqlite3_io_e
f4b20 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a rror_hit = 1; \.
f4b30 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 CODE; \.
f4b40 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 }else{ \.
f4b50 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b sqlite3_disk
f4b60 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 full_pending--;
f4b70 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a \. } \. }.
f4b80 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 #else.#define Si
f4b90 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e mulateIOErrorBen
f4ba0 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 ign(X).#define S
f4bb0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 imulateIOError(A
f4bc0 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 ).#define Simula
f4bd0 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 teDiskfullError(
f4be0 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a A).#endif../*.**
f4bf0 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b When testing, k
f4c00 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 eep a count of t
f4c10 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 he number of ope
f4c20 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 n files..*/.#ifd
f4c30 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
f4c40 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
f4c50 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f lite3_open_file_
f4c60 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 count = 0;.#defi
f4c70 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 ne OpenCounter(X
f4c80 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f ) sqlite3_open_
f4c90 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a file_count+=(X).
f4ca0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 #else.#define Op
f4cb0 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e enCounter(X).#en
f4cc0 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 dif..#endif /* !
f4cd0 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d defined(_OS_COMM
f4ce0 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a ON_H_) */../****
f4cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
f4d00 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a f os_common.h **
f4d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
f4d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
f4d50 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
f4d60 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77 69 eft off in os_wi
f4d70 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.c ************
f4d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a *********/../*.*
f4d90 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74 * Some microsoft
f4da0 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 compilers lack
f4db0 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e this definition.
f4dc0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 .*/.#ifndef INVA
f4dd0 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 LID_FILE_ATTRIBU
f4de0 54 45 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 TES.# define INV
f4df0 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 ALID_FILE_ATTRIB
f4e00 55 54 45 53 20 28 28 44 57 4f 52 44 29 2d 31 29 UTES ((DWORD)-1)
f4e10 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 .#endif../*.**
f4e20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20 Determine if we
f4e30 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 are dealing with
f4e40 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69 WindowsCE - whi
f4e50 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a ch has a much.**
f4e60 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f reduced API..*/
f4e70 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
f4e80 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 41 72 INCE.# define Ar
f4e90 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 eFileApisANSI()
f4ea0 31 0a 23 20 64 65 66 69 6e 65 20 47 65 74 44 69 1.# define GetDi
f4eb0 73 6b 46 72 65 65 53 70 61 63 65 57 28 29 20 30 skFreeSpaceW() 0
f4ec0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 .#endif../*.** W
f4ed0 69 6e 43 45 20 6c 61 63 6b 73 20 6e 61 74 69 76 inCE lacks nativ
f4ee0 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 66 69 e support for fi
f4ef0 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 6f 20 77 65 le locking so we
f4f00 20 68 61 76 65 20 74 6f 20 66 61 6b 65 20 69 74 have to fake it
f4f10 0a 2a 2a 20 77 69 74 68 20 73 6f 6d 65 20 63 6f .** with some co
f4f20 64 65 20 6f 66 20 6f 75 72 20 6f 77 6e 2e 0a 2a de of our own..*
f4f30 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
f4f40 57 49 4e 43 45 0a 74 79 70 65 64 65 66 20 73 74 WINCE.typedef st
f4f50 72 75 63 74 20 77 69 6e 63 65 4c 6f 63 6b 20 7b ruct winceLock {
f4f60 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b . int nReaders;
f4f70 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
f4f80 20 6f 66 20 72 65 61 64 65 72 20 6c 6f 63 6b 73 of reader locks
f4f90 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 obtained */. B
f4fa0 4f 4f 4c 20 62 50 65 6e 64 69 6e 67 3b 20 20 20 OOL bPending;
f4fb0 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 /* Indicates
f4fc0 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 a pending lock h
f4fd0 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 as been obtained
f4fe0 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 52 65 73 65 */. BOOL bRese
f4ff0 72 76 65 64 3b 20 20 20 20 20 2f 2a 20 49 6e 64 rved; /* Ind
f5000 69 63 61 74 65 73 20 61 20 72 65 73 65 72 76 65 icates a reserve
f5010 64 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 d lock has been
f5020 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f obtained */. BO
f5030 4f 4c 20 62 45 78 63 6c 75 73 69 76 65 3b 20 20 OL bExclusive;
f5040 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 61 /* Indicates a
f5050 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
f5060 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e has been obtain
f5070 65 64 20 2a 2f 0a 7d 20 77 69 6e 63 65 4c 6f 63 ed */.} winceLoc
f5080 6b 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a k;.#endif../*.**
f5090 20 54 68 65 20 77 69 6e 46 69 6c 65 20 73 74 72 The winFile str
f50a0 75 63 74 75 72 65 20 69 73 20 61 20 73 75 62 63 ucture is a subc
f50b0 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f lass of sqlite3_
f50c0 66 69 6c 65 2a 20 73 70 65 63 69 66 69 63 20 74 file* specific t
f50d0 6f 20 74 68 65 20 77 69 6e 33 32 0a 2a 2a 20 70 o the win32.** p
f50e0 6f 72 74 61 62 69 6c 69 74 79 20 6c 61 79 65 72 ortability layer
f50f0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
f5100 75 63 74 20 77 69 6e 46 69 6c 65 20 77 69 6e 46 uct winFile winF
f5110 69 6c 65 3b 0a 73 74 72 75 63 74 20 77 69 6e 46 ile;.struct winF
f5120 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 ile {. const sq
f5130 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
f5140 20 2a 70 4d 65 74 68 6f 64 3b 2f 2a 20 4d 75 73 *pMethod;/* Mus
f5150 74 20 62 65 20 66 69 72 73 74 20 2a 2f 0a 20 20 t be first */.
f5160 48 41 4e 44 4c 45 20 68 3b 20 20 20 20 20 20 20 HANDLE h;
f5170 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c /* Handl
f5180 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 e for accessing
f5190 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e the file */. un
f51a0 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b signed char lock
f51b0 74 79 70 65 3b 20 2f 2a 20 54 79 70 65 20 6f 66 type; /* Type of
f51c0 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20 lock currently
f51d0 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c held on this fil
f51e0 65 20 2a 2f 0a 20 20 73 68 6f 72 74 20 73 68 61 e */. short sha
f51f0 72 65 64 4c 6f 63 6b 42 79 74 65 3b 20 20 20 2f redLockByte; /
f5200 2a 20 52 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 * Randomly chose
f5210 6e 20 62 79 74 65 20 75 73 65 64 20 61 73 20 61 n byte used as a
f5220 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a shared lock */.
f5230 20 20 44 57 4f 52 44 20 6c 61 73 74 45 72 72 6e DWORD lastErrn
f5240 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 o; /* The
f5250 20 57 69 6e 64 6f 77 73 20 65 72 72 6e 6f 20 66 Windows errno f
f5260 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49 2f 4f rom the last I/O
f5270 20 65 72 72 6f 72 20 2a 2f 0a 20 20 44 57 4f 52 error */. DWOR
f5280 44 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 D sectorSize;
f5290 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 /* Sector si
f52a0 7a 65 20 6f 66 20 74 68 65 20 64 65 76 69 63 65 ze of the device
f52b0 20 66 69 6c 65 20 69 73 20 6f 6e 20 2a 2f 0a 23 file is on */.#
f52c0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
f52d0 43 45 0a 20 20 57 43 48 41 52 20 2a 7a 44 65 6c CE. WCHAR *zDel
f52e0 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20 eteOnClose; /*
f52f0 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 Name of file to
f5300 64 65 6c 65 74 65 20 77 68 65 6e 20 63 6c 6f 73 delete when clos
f5310 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20 ing */. HANDLE
f5320 68 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 hMutex;
f5330 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 74 /* Mutex used t
f5340 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 o control access
f5350 20 74 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 to shared lock
f5360 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45 20 68 53 */ . HANDLE hS
f5370 68 61 72 65 64 3b 20 20 20 20 20 20 20 20 20 2f hared; /
f5380 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 * Shared memory
f5390 73 65 67 6d 65 6e 74 20 75 73 65 64 20 66 6f 72 segment used for
f53a0 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 77 69 locking */. wi
f53b0 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20 nceLock local;
f53c0 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f /* Locks o
f53d0 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 73 20 btained by this
f53e0 69 6e 73 74 61 6e 63 65 20 6f 66 20 77 69 6e 46 instance of winF
f53f0 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f ile */. winceLo
f5400 63 6b 20 2a 73 68 61 72 65 64 3b 20 20 20 20 20 ck *shared;
f5410 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68 61 72 65 /* Global share
f5420 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79 20 66 6f d lock memory fo
f5430 72 20 74 68 65 20 66 69 6c 65 20 20 2a 2f 0a 23 r the file */.#
f5440 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
f5450 46 6f 72 77 61 72 64 20 70 72 6f 74 6f 74 79 70 Forward prototyp
f5460 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e es..*/.static in
f5470 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28 t getSectorSize(
f5480 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 . sqlite3_vfs
f5490 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73 *pVfs,. cons
f54a0 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 t char *zRelativ
f54b0 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 66 e /* UTF-8 f
f54c0 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 0a ile name */.);..
f54d0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
f54e0 69 6e 67 20 76 61 72 69 61 62 6c 65 20 69 73 20 ing variable is
f54f0 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20 6f (normally) set o
f5500 6e 63 65 20 61 6e 64 20 6e 65 76 65 72 20 63 68 nce and never ch
f5510 61 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 61 66 anges.** thereaf
f5520 74 65 72 2e 20 20 49 74 20 72 65 63 6f 72 64 73 ter. It records
f5530 20 77 68 65 74 68 65 72 20 74 68 65 20 6f 70 65 whether the ope
f5540 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 rating system is
f5550 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 20 57 69 6e Win95.** or Win
f5560 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f NT..**.** 0: O
f5570 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
f5580 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20 unknown..** 1:
f5590 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 Operating syste
f55a0 6d 20 69 73 20 57 69 6e 39 35 2e 0a 2a 2a 20 32 m is Win95..** 2
f55b0 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 : Operating sy
f55c0 73 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a 2a stem is WinNT..*
f55d0 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 6f *.** In order to
f55e0 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73 74 facilitate test
f55f0 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20 73 ing on a WinNT s
f5600 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74 20 ystem, the test
f5610 66 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20 6d fixture.** can m
f5620 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69 73 anually set this
f5630 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20 65 value to 1 to e
f5640 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65 68 mulate Win98 beh
f5650 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 avior..*/.#ifdef
f5660 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
f5670 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
f5680 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 3b te3_os_type = 0;
f5690 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 69 6e .#else.static in
f56a0 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 t sqlite3_os_typ
f56b0 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f e = 0;.#endif../
f56c0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
f56d0 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 (non-zero) if w
f56e0 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e e are running un
f56f0 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b der WinNT, Win2K
f5700 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 , WinXP,.** or W
f5710 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 inCE. Return fa
f5720 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 lse (zero) for W
f5730 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 in95, Win98, or
f5740 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 WinME..**.** Her
f5750 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 e is an interest
f5760 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a ing observation:
f5770 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 Win95, Win98,
f5780 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a and WinME lack.*
f5790 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 * the LockFileEx
f57a0 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 () API. But we
f57b0 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 can still static
f57c0 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 ally link agains
f57d0 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 t that.** API as
f57e0 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 long as we don'
f57f0 74 20 63 61 6c 6c 20 69 74 20 77 68 65 6e 20 72 t call it when r
f5800 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f unning Win95/98/
f5810 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a ME. A call to.*
f5820 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * this routine i
f5830 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d s used to determ
f5840 69 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20 ine if the host
f5850 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f is Win95/98/ME o
f5860 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 r.** WinNT/2K/XP
f5870 20 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c so that we will
f5880 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 know whether or
f5890 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65 not we can safe
f58a0 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c ly call.** the L
f58b0 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e ockFileEx() API.
f58c0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
f58d0 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 S_WINCE.# define
f58e0 20 69 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c isNT() (1).#el
f58f0 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 se. static int
f5900 69 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 20 isNT(void){.
f5910 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 if( sqlite3_os_t
f5920 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ype==0 ){.
f5930 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 49 OSVERSIONINFO sI
f5940 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 6f nfo;. sInfo
f5950 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 6f .dwOSVersionInfo
f5960 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 49 Size = sizeof(sI
f5970 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 56 nfo);. GetV
f5980 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f 29 ersionEx(&sInfo)
f5990 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
f59a0 6f 73 5f 74 79 70 65 20 3d 20 73 49 6e 66 6f 2e os_type = sInfo.
f59b0 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 dwPlatformId==VE
f59c0 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 R_PLATFORM_WIN32
f59d0 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 _NT ? 2 : 1;.
f59e0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 }. return sq
f59f0 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d 32 lite3_os_type==2
f5a00 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ;. }.#endif /*
f5a10 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 SQLITE_OS_WINCE
f5a20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 */../*.** Conver
f5a30 74 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 t a UTF-8 string
f5a40 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e to microsoft un
f5a50 69 63 6f 64 65 20 28 55 54 46 2d 31 36 3f 29 2e icode (UTF-16?).
f5a60 20 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f .**.** Space to
f5a70 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e hold the return
f5a80 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 ed string is obt
f5a90 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f ained from mallo
f5aa0 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 c..*/.static WCH
f5ab0 41 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f 64 AR *utf8ToUnicod
f5ac0 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 e(const char *zF
f5ad0 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 ilename){. int
f5ae0 6e 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20 2a nChar;. WCHAR *
f5af0 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a zWideFilename;..
f5b00 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42 nChar = MultiB
f5b10 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50 yteToWideChar(CP
f5b20 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e _UTF8, 0, zFilen
f5b30 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30 ame, -1, NULL, 0
f5b40 29 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e 61 );. zWideFilena
f5b50 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 43 68 me = malloc( nCh
f5b60 61 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64 65 46 ar*sizeof(zWideF
f5b70 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 ilename[0]) );.
f5b80 20 69 66 28 20 7a 57 69 64 65 46 69 6c 65 6e 61 if( zWideFilena
f5b90 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 me==0 ){. ret
f5ba0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 43 68 urn 0;. }. nCh
f5bb0 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f ar = MultiByteTo
f5bc0 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38 WideChar(CP_UTF8
f5bd0 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 , 0, zFilename,
f5be0 2d 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d -1, zWideFilenam
f5bf0 65 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69 66 28 e, nChar);. if(
f5c00 20 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20 nChar==0 ){.
f5c10 20 66 72 65 65 28 7a 57 69 64 65 46 69 6c 65 6e free(zWideFilen
f5c20 61 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64 65 46 ame);. zWideF
f5c30 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d ilename = 0;. }
f5c40 0a 20 20 72 65 74 75 72 6e 20 7a 57 69 64 65 46 . return zWideF
f5c50 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a ilename;.}../*.*
f5c60 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73 * Convert micros
f5c70 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 55 oft unicode to U
f5c80 54 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20 TF-8. Space to
f5c90 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 hold the returne
f5ca0 64 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 6f d string is.** o
f5cb0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c btained from mal
f5cc0 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 loc()..*/.static
f5cd0 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f char *unicodeTo
f5ce0 55 74 66 38 28 63 6f 6e 73 74 20 57 43 48 41 52 Utf8(const WCHAR
f5cf0 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 *zWideFilename)
f5d00 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 {. int nByte;.
f5d10 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
f5d20 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 ;.. nByte = Wid
f5d30 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 eCharToMultiByte
f5d40 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69 (CP_UTF8, 0, zWi
f5d50 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 deFilename, -1,
f5d60 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 0, 0, 0, 0);. z
f5d70 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f Filename = mallo
f5d80 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 c( nByte );. if
f5d90 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 ( zFilename==0 )
f5da0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
f5db0 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 }. nByte = Wi
f5dc0 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 deCharToMultiByt
f5dd0 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 e(CP_UTF8, 0, zW
f5de0 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c ideFilename, -1,
f5df0 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 zFilename, nByt
f5e00 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e,.
f5e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f5e20 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 0, 0);. if( nB
f5e30 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 yte == 0 ){.
f5e40 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b free(zFilename);
f5e50 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d . zFilename =
f5e60 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0;. }. return
f5e70 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f zFilename;.}../
f5e80 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 *.** Convert an
f5e90 61 6e 73 69 20 73 74 72 69 6e 67 20 74 6f 20 6d ansi string to m
f5ea0 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 icrosoft unicode
f5eb0 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a , based on the.*
f5ec0 2a 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 * current codepa
f5ed0 67 65 20 73 65 74 74 69 6e 67 73 20 66 6f 72 20 ge settings for
f5ee0 66 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a file apis..** .*
f5ef0 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 * Space to hold
f5f00 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 the returned str
f5f10 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 0a ing is obtained.
f5f20 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a ** from malloc..
f5f30 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 */.static WCHAR
f5f40 2a 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 63 *mbcsToUnicode(c
f5f50 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
f5f60 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 name){. int nBy
f5f70 74 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4d 62 te;. WCHAR *zMb
f5f80 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e csFilename;. in
f5f90 74 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65 t codepage = Are
f5fa0 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f FileApisANSI() ?
f5fb0 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d CP_ACP : CP_OEM
f5fc0 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 4d CP;.. nByte = M
f5fd0 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 ultiByteToWideCh
f5fe0 61 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 ar(codepage, 0,
f5ff0 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e zFilename, -1, N
f6000 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28 57 43 ULL,0)*sizeof(WC
f6010 48 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46 69 6c HAR);. zMbcsFil
f6020 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 ename = malloc(
f6030 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 4d 62 nByte*sizeof(zMb
f6040 63 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 csFilename[0]) )
f6050 3b 0a 20 20 69 66 28 20 7a 4d 62 63 73 46 69 6c ;. if( zMbcsFil
f6060 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 ename==0 ){.
f6070 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
f6080 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79 74 nByte = MultiByt
f6090 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64 65 eToWideChar(code
f60a0 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 page, 0, zFilena
f60b0 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46 69 6c me, -1, zMbcsFil
f60c0 65 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b 0a 20 ename, nByte);.
f60d0 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b if( nByte==0 ){
f60e0 0a 20 20 20 20 66 72 65 65 28 7a 4d 62 63 73 46 . free(zMbcsF
f60f0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 4d ilename);. zM
f6100 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b bcsFilename = 0;
f6110 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4d . }. return zM
f6120 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a bcsFilename;.}..
f6130 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69 /*.** Convert mi
f6140 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20 crosoft unicode
f6150 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61 to multibyte cha
f6160 72 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 62 racter string, b
f6170 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 75 ased on the.** u
f6180 73 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65 70 ser's Ansi codep
f6190 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 age..**.** Space
f61a0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 to hold the ret
f61b0 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 urned string is
f61c0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a obtained from.**
f61d0 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 malloc()..*/.st
f61e0 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 6f atic char *unico
f61f0 64 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 57 deToMbcs(const W
f6200 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e CHAR *zWideFilen
f6210 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 ame){. int nByt
f6220 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 e;. char *zFile
f6230 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 65 name;. int code
f6240 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 70 page = AreFileAp
f6250 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 43 isANSI() ? CP_AC
f6260 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20 P : CP_OEMCP;..
f6270 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 nByte = WideCha
f6280 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 rToMultiByte(cod
f6290 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 epage, 0, zWideF
f62a0 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 ilename, -1, 0,
f62b0 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 0, 0, 0);. zFil
f62c0 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 ename = malloc(
f62d0 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a nByte );. if( z
f62e0 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 Filename==0 ){.
f62f0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
f6300 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 . nByte = WideC
f6310 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 harToMultiByte(c
f6320 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 odepage, 0, zWid
f6330 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a eFilename, -1, z
f6340 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c Filename, nByte,
f6350 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
f6360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
f6370 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 , 0);. if( nByt
f6380 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 72 e == 0 ){. fr
f6390 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 ee(zFilename);.
f63a0 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 zFilename = 0
f63b0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a ;. }. return z
f63c0 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a Filename;.}../*.
f63d0 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c 74 69 ** Convert multi
f63e0 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73 byte character s
f63f0 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2e 20 tring to UTF-8.
f6400 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 Space to hold t
f6410 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 he.** returned s
f6420 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 tring is obtaine
f6430 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e d from malloc().
f6440 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
f6450 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 77 69 6e har *sqlite3_win
f6460 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 32_mbcs_to_utf8(
f6470 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
f6480 65 6e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a ename){. char *
f6490 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a 20 zFilenameUtf8;.
f64a0 20 57 43 48 41 52 20 2a 7a 54 6d 70 57 69 64 65 WCHAR *zTmpWide
f64b0 3b 0a 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d 20 ;.. zTmpWide =
f64c0 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 7a 46 mbcsToUnicode(zF
f64d0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 ilename);. if(
f64e0 7a 54 6d 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20 zTmpWide==0 ){.
f64f0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
f6500 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 . zFilenameUtf8
f6510 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 = unicodeToUtf8
f6520 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 66 72 (zTmpWide);. fr
f6530 65 65 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 ee(zTmpWide);.
f6540 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 return zFilename
f6550 55 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 Utf8;.}../*.** C
f6560 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 74 6f 20 onvert UTF-8 to
f6570 6d 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 multibyte charac
f6580 74 65 72 20 73 74 72 69 6e 67 2e 20 20 53 70 61 ter string. Spa
f6590 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 0a ce to hold the .
f65a0 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 ** returned stri
f65b0 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 ng is obtained f
f65c0 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f rom malloc()..*/
f65d0 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 75 74 .static char *ut
f65e0 66 38 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 63 f8ToMbcs(const c
f65f0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b har *zFilename){
f6600 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 . char *zFilena
f6610 6d 65 4d 62 63 73 3b 0a 20 20 57 43 48 41 52 20 meMbcs;. WCHAR
f6620 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54 *zTmpWide;.. zT
f6630 6d 70 57 69 64 65 20 3d 20 75 74 66 38 54 6f 55 mpWide = utf8ToU
f6640 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 nicode(zFilename
f6650 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 64 );. if( zTmpWid
f6660 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 e==0 ){. retu
f6670 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c rn 0;. }. zFil
f6680 65 6e 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63 enameMbcs = unic
f6690 6f 64 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69 odeToMbcs(zTmpWi
f66a0 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 6d 70 de);. free(zTmp
f66b0 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 Wide);. return
f66c0 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d zFilenameMbcs;.}
f66d0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ..#if SQLITE_OS_
f66e0 57 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a WINCE./*********
f66f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6730 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e .** This section
f6740 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 contains code f
f6750 6f 72 20 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a or WinCE only..*
f6760 2f 0a 2f 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 43 /./*.** WindowsC
f6770 45 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 E does not have
f6780 61 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 66 75 a localtime() fu
f6790 6e 63 74 69 6f 6e 2e 20 20 53 6f 20 63 72 65 61 nction. So crea
f67a0 74 65 20 61 0a 2a 2a 20 73 75 62 73 74 69 74 75 te a.** substitu
f67b0 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 6d te..*/.struct tm
f67c0 20 2a 5f 5f 63 64 65 63 6c 20 6c 6f 63 61 6c 74 *__cdecl localt
f67d0 69 6d 65 28 63 6f 6e 73 74 20 74 69 6d 65 5f 74 ime(const time_t
f67e0 20 2a 74 29 0a 7b 0a 20 20 73 74 61 74 69 63 20 *t).{. static
f67f0 73 74 72 75 63 74 20 74 6d 20 79 3b 0a 20 20 46 struct tm y;. F
f6800 49 4c 45 54 49 4d 45 20 75 54 6d 2c 20 6c 54 6d ILETIME uTm, lTm
f6810 3b 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 70 ;. SYSTEMTIME p
f6820 54 6d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e Tm;. sqlite3_in
f6830 74 36 34 20 74 36 34 3b 0a 20 20 74 36 34 20 3d t64 t64;. t64 =
f6840 20 2a 74 3b 0a 20 20 74 36 34 20 3d 20 28 74 36 *t;. t64 = (t6
f6850 34 20 2b 20 31 31 36 34 34 34 37 33 36 30 30 29 4 + 11644473600)
f6860 2a 31 30 30 30 30 30 30 30 3b 0a 20 20 75 54 6d *10000000;. uTm
f6870 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d 65 20 3d .dwLowDateTime =
f6880 20 28 44 57 4f 52 44 29 28 74 36 34 20 26 20 30 (DWORD)(t64 & 0
f6890 78 46 46 46 46 46 46 46 46 29 3b 0a 20 20 75 54 xFFFFFFFF);. uT
f68a0 6d 2e 64 77 48 69 67 68 44 61 74 65 54 69 6d 65 m.dwHighDateTime
f68b0 3d 20 28 44 57 4f 52 44 29 28 74 36 34 20 3e 3e = (DWORD)(t64 >>
f68c0 20 33 32 29 3b 0a 20 20 46 69 6c 65 54 69 6d 65 32);. FileTime
f68d0 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 69 6d 65 28 ToLocalFileTime(
f68e0 26 75 54 6d 2c 26 6c 54 6d 29 3b 0a 20 20 46 69 &uTm,&lTm);. Fi
f68f0 6c 65 54 69 6d 65 54 6f 53 79 73 74 65 6d 54 69 leTimeToSystemTi
f6900 6d 65 28 26 6c 54 6d 2c 26 70 54 6d 29 3b 0a 20 me(&lTm,&pTm);.
f6910 20 79 2e 74 6d 5f 79 65 61 72 20 3d 20 70 54 6d y.tm_year = pTm
f6920 2e 77 59 65 61 72 20 2d 20 31 39 30 30 3b 0a 20 .wYear - 1900;.
f6930 20 79 2e 74 6d 5f 6d 6f 6e 20 3d 20 70 54 6d 2e y.tm_mon = pTm.
f6940 77 4d 6f 6e 74 68 20 2d 20 31 3b 0a 20 20 79 2e wMonth - 1;. y.
f6950 74 6d 5f 77 64 61 79 20 3d 20 70 54 6d 2e 77 44 tm_wday = pTm.wD
f6960 61 79 4f 66 57 65 65 6b 3b 0a 20 20 79 2e 74 6d ayOfWeek;. y.tm
f6970 5f 6d 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 _mday = pTm.wDay
f6980 3b 0a 20 20 79 2e 74 6d 5f 68 6f 75 72 20 3d 20 ;. y.tm_hour =
f6990 70 54 6d 2e 77 48 6f 75 72 3b 0a 20 20 79 2e 74 pTm.wHour;. y.t
f69a0 6d 5f 6d 69 6e 20 3d 20 70 54 6d 2e 77 4d 69 6e m_min = pTm.wMin
f69b0 75 74 65 3b 0a 20 20 79 2e 74 6d 5f 73 65 63 20 ute;. y.tm_sec
f69c0 3d 20 70 54 6d 2e 77 53 65 63 6f 6e 64 3b 0a 20 = pTm.wSecond;.
f69d0 20 72 65 74 75 72 6e 20 26 79 3b 0a 7d 0a 0a 2f return &y;.}../
f69e0 2a 20 54 68 69 73 20 77 69 6c 6c 20 6e 65 76 65 * This will neve
f69f0 72 20 62 65 20 63 61 6c 6c 65 64 2c 20 62 75 74 r be called, but
f6a00 20 64 65 66 69 6e 65 64 20 74 6f 20 6d 61 6b 65 defined to make
f6a10 20 74 68 65 20 63 6f 64 65 20 63 6f 6d 70 69 6c the code compil
f6a20 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 47 65 74 e */.#define Get
f6a30 54 65 6d 70 50 61 74 68 41 28 61 2c 62 29 0a 0a TempPathA(a,b)..
f6a40 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 #define LockFile
f6a50 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 (a,b,c,d,e)
f6a60 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 winceLockFile(
f6a70 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a &a, b, c, d, e).
f6a80 23 64 65 66 69 6e 65 20 55 6e 6c 6f 63 6b 46 69 #define UnlockFi
f6a90 6c 65 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 le(a,b,c,d,e)
f6aa0 20 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c winceUnlockFil
f6ab0 65 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 e(&a, b, c, d, e
f6ac0 29 0a 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69 ).#define LockFi
f6ad0 6c 65 45 78 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 leEx(a,b,c,d,e,f
f6ae0 29 20 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c ) winceLockFil
f6af0 65 45 78 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c eEx(&a, b, c, d,
f6b00 20 65 2c 20 66 29 0a 0a 23 64 65 66 69 6e 65 20 e, f)..#define
f6b10 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c HANDLE_TO_WINFIL
f6b20 45 28 61 29 20 28 77 69 6e 46 69 6c 65 2a 29 26 E(a) (winFile*)&
f6b30 28 28 63 68 61 72 2a 29 61 29 5b 2d 28 69 6e 74 ((char*)a)[-(int
f6b40 29 6f 66 66 73 65 74 6f 66 28 77 69 6e 46 69 6c )offsetof(winFil
f6b50 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 e,h)]../*.** Acq
f6b60 75 69 72 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 uire a lock on t
f6b70 68 65 20 68 61 6e 64 6c 65 20 68 0a 2a 2f 0a 73 he handle h.*/.s
f6b80 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 63 65 tatic void wince
f6b90 4d 75 74 65 78 41 63 71 75 69 72 65 28 48 41 4e MutexAcquire(HAN
f6ba0 44 4c 45 20 68 29 7b 0a 20 20 20 44 57 4f 52 44 DLE h){. DWORD
f6bb0 20 64 77 45 72 72 3b 0a 20 20 20 64 6f 20 7b 0a dwErr;. do {.
f6bc0 20 20 20 20 20 64 77 45 72 72 20 3d 20 57 61 69 dwErr = Wai
f6bd0 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63 74 tForSingleObject
f6be0 28 68 2c 20 49 4e 46 49 4e 49 54 45 29 3b 0a 20 (h, INFINITE);.
f6bf0 20 20 7d 20 77 68 69 6c 65 20 28 64 77 45 72 72 } while (dwErr
f6c00 20 21 3d 20 57 41 49 54 5f 4f 42 4a 45 43 54 5f != WAIT_OBJECT_
f6c10 30 20 26 26 20 64 77 45 72 72 20 21 3d 20 57 41 0 && dwErr != WA
f6c20 49 54 5f 41 42 41 4e 44 4f 4e 45 44 29 3b 0a 7d IT_ABANDONED);.}
f6c30 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 ./*.** Release a
f6c40 20 6c 6f 63 6b 20 61 63 71 75 69 72 65 64 20 62 lock acquired b
f6c50 79 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 y winceMutexAcqu
f6c60 69 72 65 28 29 0a 2a 2f 0a 23 64 65 66 69 6e 65 ire().*/.#define
f6c70 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 winceMutexRelea
f6c80 73 65 28 68 29 20 52 65 6c 65 61 73 65 4d 75 74 se(h) ReleaseMut
f6c90 65 78 28 68 29 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 ex(h)../*.** Cre
f6ca0 61 74 65 20 74 68 65 20 6d 75 74 65 78 20 61 6e ate the mutex an
f6cb0 64 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 d shared memory
f6cc0 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 used for locking
f6cd0 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 in the file.**
f6ce0 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 descriptor pFile
f6cf0 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 .*/.static BOOL
f6d00 77 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b 28 winceCreateLock(
f6d10 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
f6d20 65 6e 61 6d 65 2c 20 77 69 6e 46 69 6c 65 20 2a ename, winFile *
f6d30 70 46 69 6c 65 29 7b 0a 20 20 57 43 48 41 52 20 pFile){. WCHAR
f6d40 2a 7a 54 6f 6b 3b 0a 20 20 57 43 48 41 52 20 2a *zTok;. WCHAR *
f6d50 7a 4e 61 6d 65 20 3d 20 75 74 66 38 54 6f 55 6e zName = utf8ToUn
f6d60 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 icode(zFilename)
f6d70 3b 0a 20 20 42 4f 4f 4c 20 62 49 6e 69 74 20 3d ;. BOOL bInit =
f6d80 20 54 52 55 45 3b 0a 0a 20 20 2f 2a 20 49 6e 69 TRUE;.. /* Ini
f6d90 74 69 61 6c 69 7a 65 20 74 68 65 20 6c 6f 63 61 tialize the loca
f6da0 6c 20 6c 6f 63 6b 64 61 74 61 20 2a 2f 0a 20 20 l lockdata */.
f6db0 5a 65 72 6f 4d 65 6d 6f 72 79 28 26 70 46 69 6c ZeroMemory(&pFil
f6dc0 65 2d 3e 6c 6f 63 61 6c 2c 20 73 69 7a 65 6f 66 e->local, sizeof
f6dd0 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 29 29 3b (pFile->local));
f6de0 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 74 .. /* Replace t
f6df0 68 65 20 62 61 63 6b 73 6c 61 73 68 65 73 20 66 he backslashes f
f6e00 72 6f 6d 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 rom the filename
f6e10 20 61 6e 64 20 6c 6f 77 65 72 63 61 73 65 20 69 and lowercase i
f6e20 74 0a 20 20 2a 2a 20 74 6f 20 64 65 72 69 76 65 t. ** to derive
f6e30 20 61 20 6d 75 74 65 78 20 6e 61 6d 65 2e 20 2a a mutex name. *
f6e40 2f 0a 20 20 7a 54 6f 6b 20 3d 20 43 68 61 72 4c /. zTok = CharL
f6e50 6f 77 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 owerW(zName);.
f6e60 66 6f 72 20 28 3b 2a 7a 54 6f 6b 3b 7a 54 6f 6b for (;*zTok;zTok
f6e70 2b 2b 29 7b 0a 20 20 20 20 69 66 20 28 2a 7a 54 ++){. if (*zT
f6e80 6f 6b 20 3d 3d 20 27 5c 5c 27 29 20 2a 7a 54 6f ok == '\\') *zTo
f6e90 6b 20 3d 20 27 5f 27 3b 0a 20 20 7d 0a 0a 20 20 k = '_';. }..
f6ea0 2f 2a 20 43 72 65 61 74 65 2f 6f 70 65 6e 20 74 /* Create/open t
f6eb0 68 65 20 6e 61 6d 65 64 20 6d 75 74 65 78 20 2a he named mutex *
f6ec0 2f 0a 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 /. pFile->hMute
f6ed0 78 20 3d 20 43 72 65 61 74 65 4d 75 74 65 78 57 x = CreateMutexW
f6ee0 28 4e 55 4c 4c 2c 20 46 41 4c 53 45 2c 20 7a 4e (NULL, FALSE, zN
f6ef0 61 6d 65 29 3b 0a 20 20 69 66 20 28 21 70 46 69 ame);. if (!pFi
f6f00 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20 le->hMutex){.
f6f10 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
f6f20 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 o = GetLastError
f6f30 28 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 4e 61 ();. free(zNa
f6f40 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 me);. return
f6f50 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a FALSE;. }.. /*
f6f60 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 74 Acquire the mut
f6f70 65 78 20 62 65 66 6f 72 65 20 63 6f 6e 74 69 6e ex before contin
f6f80 75 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 4d uing */. winceM
f6f90 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c utexAcquire(pFil
f6fa0 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 0a 20 e->hMutex);. .
f6fb0 20 2f 2a 20 53 69 6e 63 65 20 74 68 65 20 6e 61 /* Since the na
f6fc0 6d 65 73 20 6f 66 20 6e 61 6d 65 64 20 6d 75 74 mes of named mut
f6fd0 65 78 65 73 2c 20 73 65 6d 61 70 68 6f 72 65 73 exes, semaphores
f6fe0 2c 20 66 69 6c 65 20 6d 61 70 70 69 6e 67 73 20 , file mappings
f6ff0 65 74 63 20 61 72 65 20 0a 20 20 2a 2a 20 63 61 etc are . ** ca
f7000 73 65 2d 73 65 6e 73 69 74 69 76 65 2c 20 74 61 se-sensitive, ta
f7010 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20 ke advantage of
f7020 74 68 61 74 20 62 79 20 75 70 70 65 72 63 61 73 that by uppercas
f7030 69 6e 67 20 74 68 65 20 6d 75 74 65 78 20 6e 61 ing the mutex na
f7040 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 75 73 69 6e me. ** and usin
f7050 67 20 74 68 61 74 20 61 73 20 74 68 65 20 73 68 g that as the sh
f7060 61 72 65 64 20 66 69 6c 65 6d 61 70 70 69 6e 67 ared filemapping
f7070 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 43 68 name.. */. Ch
f7080 61 72 55 70 70 65 72 57 28 7a 4e 61 6d 65 29 3b arUpperW(zName);
f7090 0a 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 . pFile->hShare
f70a0 64 20 3d 20 43 72 65 61 74 65 46 69 6c 65 4d 61 d = CreateFileMa
f70b0 70 70 69 6e 67 57 28 49 4e 56 41 4c 49 44 5f 48 ppingW(INVALID_H
f70c0 41 4e 44 4c 45 5f 56 41 4c 55 45 2c 20 4e 55 4c ANDLE_VALUE, NUL
f70d0 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 L,.
f70e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f70f0 20 20 20 20 20 20 20 20 20 20 50 41 47 45 5f 52 PAGE_R
f7100 45 41 44 57 52 49 54 45 2c 20 30 2c 20 73 69 7a EADWRITE, 0, siz
f7110 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 2c 0a eof(winceLock),.
f7120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f7130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f7140 20 20 20 20 20 20 20 7a 4e 61 6d 65 29 3b 20 20 zName);
f7150 0a 0a 20 20 2f 2a 20 53 65 74 20 61 20 66 6c 61 .. /* Set a fla
f7160 67 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73 g that indicates
f7170 20 77 65 27 72 65 20 74 68 65 20 66 69 72 73 74 we're the first
f7180 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 6d to create the m
f7190 65 6d 6f 72 79 20 73 6f 20 69 74 20 0a 20 20 2a emory so it . *
f71a0 2a 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d 69 * must be zero-i
f71b0 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 nitialized */.
f71c0 69 66 20 28 47 65 74 4c 61 73 74 45 72 72 6f 72 if (GetLastError
f71d0 28 29 20 3d 3d 20 45 52 52 4f 52 5f 41 4c 52 45 () == ERROR_ALRE
f71e0 41 44 59 5f 45 58 49 53 54 53 29 7b 0a 20 20 20 ADY_EXISTS){.
f71f0 20 62 49 6e 69 74 20 3d 20 46 41 4c 53 45 3b 0a bInit = FALSE;.
f7200 20 20 7d 0a 0a 20 20 66 72 65 65 28 7a 4e 61 6d }.. free(zNam
f7210 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 e);.. /* If we
f7220 73 75 63 63 65 65 64 65 64 20 69 6e 20 6d 61 6b succeeded in mak
f7230 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6d ing the shared m
f7240 65 6d 6f 72 79 20 68 61 6e 64 6c 65 2c 20 6d 61 emory handle, ma
f7250 70 20 69 74 2e 20 2a 2f 0a 20 20 69 66 20 28 70 p it. */. if (p
f7260 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29 7b 0a File->hShared){.
f7270 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 pFile->share
f7280 64 20 3d 20 28 77 69 6e 63 65 4c 6f 63 6b 2a 29 d = (winceLock*)
f7290 4d 61 70 56 69 65 77 4f 66 46 69 6c 65 28 70 46 MapViewOfFile(pF
f72a0 69 6c 65 2d 3e 68 53 68 61 72 65 64 2c 20 0a 20 ile->hShared, .
f72b0 20 20 20 20 20 20 20 20 20 20 20 20 46 49 4c 45 FILE
f72c0 5f 4d 41 50 5f 52 45 41 44 7c 46 49 4c 45 5f 4d _MAP_READ|FILE_M
f72d0 41 50 5f 57 52 49 54 45 2c 20 30 2c 20 30 2c 20 AP_WRITE, 0, 0,
f72e0 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b sizeof(winceLock
f72f0 29 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 6d 61 ));. /* If ma
f7300 70 70 69 6e 67 20 66 61 69 6c 65 64 2c 20 63 6c pping failed, cl
f7310 6f 73 65 20 74 68 65 20 73 68 61 72 65 64 20 6d ose the shared m
f7320 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 61 6e 64 emory handle and
f7330 20 65 72 61 73 65 20 69 74 20 2a 2f 0a 20 20 20 erase it */.
f7340 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 73 68 61 if (!pFile->sha
f7350 72 65 64 29 7b 0a 20 20 20 20 20 20 70 46 69 6c red){. pFil
f7360 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 e->lastErrno = G
f7370 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
f7380 20 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 CloseHandle
f7390 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29 (pFile->hShared)
f73a0 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 ;. pFile->h
f73b0 53 68 61 72 65 64 20 3d 20 4e 55 4c 4c 3b 0a 20 Shared = NULL;.
f73c0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
f73d0 66 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 f shared memory
f73e0 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 72 65 could not be cre
f73f0 61 74 65 64 2c 20 74 68 65 6e 20 63 6c 6f 73 65 ated, then close
f7400 20 74 68 65 20 6d 75 74 65 78 20 61 6e 64 20 66 the mutex and f
f7410 61 69 6c 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 ail */. if (pFi
f7420 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 3d 20 4e le->hShared == N
f7430 55 4c 4c 29 7b 0a 20 20 20 20 77 69 6e 63 65 4d ULL){. winceM
f7440 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c utexRelease(pFil
f7450 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20 e->hMutex);.
f7460 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c CloseHandle(pFil
f7470 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20 e->hMutex);.
f7480 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 pFile->hMutex =
f7490 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 74 75 72 6e NULL;. return
f74a0 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 20 20 0a 20 FALSE;. }. .
f74b0 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 /* Initialize t
f74c0 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 he shared memory
f74d0 20 69 66 20 77 65 27 72 65 20 73 75 70 70 6f 73 if we're suppos
f74e0 65 64 20 74 6f 20 2a 2f 0a 20 20 69 66 20 28 62 ed to */. if (b
f74f0 49 6e 69 74 29 20 7b 0a 20 20 20 20 5a 65 72 6f Init) {. Zero
f7500 4d 65 6d 6f 72 79 28 70 46 69 6c 65 2d 3e 73 68 Memory(pFile->sh
f7510 61 72 65 64 2c 20 73 69 7a 65 6f 66 28 77 69 6e ared, sizeof(win
f7520 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 7d 0a 0a 20 ceLock));. }..
f7530 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 winceMutexRelea
f7540 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 se(pFile->hMutex
f7550 29 3b 0a 20 20 72 65 74 75 72 6e 20 54 52 55 45 );. return TRUE
f7560 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 ;.}../*.** Destr
f7570 6f 79 20 74 68 65 20 70 61 72 74 20 6f 66 20 77 oy the part of w
f7580 69 6e 46 69 6c 65 20 74 68 61 74 20 64 65 61 6c inFile that deal
f7590 73 20 77 69 74 68 20 77 69 6e 63 65 20 6c 6f 63 s with wince loc
f75a0 6b 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 ks.*/.static voi
f75b0 64 20 77 69 6e 63 65 44 65 73 74 72 6f 79 4c 6f d winceDestroyLo
f75c0 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c ck(winFile *pFil
f75d0 65 29 7b 0a 20 20 69 66 20 28 70 46 69 6c 65 2d e){. if (pFile-
f75e0 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20 20 2f 2a >hMutex){. /*
f75f0 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 74 Acquire the mut
f7600 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65 4d ex */. winceM
f7610 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c utexAcquire(pFil
f7620 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 20 e->hMutex);..
f7630 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e /* The followin
f7640 67 20 62 6c 6f 63 6b 73 20 73 68 6f 75 6c 64 20 g blocks should
f7650 70 72 6f 62 61 62 6c 79 20 61 73 73 65 72 74 20 probably assert
f7660 69 6e 20 64 65 62 75 67 20 6d 6f 64 65 2c 20 62 in debug mode, b
f7670 75 74 20 74 68 65 79 0a 20 20 20 20 20 20 20 61 ut they. a
f7680 72 65 20 74 6f 20 63 6c 65 61 6e 75 70 20 69 6e re to cleanup in
f7690 20 63 61 73 65 20 61 6e 79 20 6c 6f 63 6b 73 20 case any locks
f76a0 72 65 6d 61 69 6e 65 64 20 6f 70 65 6e 20 2a 2f remained open */
f76b0 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
f76c0 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 29 7b local.nReaders){
f76d0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 . pFile->sh
f76e0 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2d ared->nReaders -
f76f0 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 -;. }. if
f7700 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 (pFile->local.bR
f7710 65 73 65 72 76 65 64 29 7b 0a 20 20 20 20 20 20 eserved){.
f7720 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 pFile->shared->b
f7730 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45 Reserved = FALSE
f7740 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 ;. }. if (
f7750 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 pFile->local.bPe
f7760 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46 nding){. pF
f7770 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 ile->shared->bPe
f7780 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 nding = FALSE;.
f7790 20 20 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69 }. if (pFi
f77a0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 le->local.bExclu
f77b0 73 69 76 65 29 7b 0a 20 20 20 20 20 20 70 46 69 sive){. pFi
f77c0 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 le->shared->bExc
f77d0 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a lusive = FALSE;.
f77e0 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 }.. /* De
f77f0 2d 72 65 66 65 72 65 6e 63 65 20 61 6e 64 20 63 -reference and c
f7800 6c 6f 73 65 20 6f 75 72 20 63 6f 70 79 20 6f 66 lose our copy of
f7810 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f the shared memo
f7820 72 79 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 20 ry handle */.
f7830 20 55 6e 6d 61 70 56 69 65 77 4f 66 46 69 6c 65 UnmapViewOfFile
f7840 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 3b (pFile->shared);
f7850 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 . CloseHandle
f7860 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29 (pFile->hShared)
f7870 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 6e 65 20 77 ;.. /* Done w
f7880 69 74 68 20 74 68 65 20 6d 75 74 65 78 20 2a 2f ith the mutex */
f7890 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 52 . winceMutexR
f78a0 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d elease(pFile->hM
f78b0 75 74 65 78 29 3b 20 20 20 20 0a 20 20 20 20 43 utex); . C
f78c0 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 loseHandle(pFile
f78d0 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20 70 ->hMutex);. p
f78e0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 4e File->hMutex = N
f78f0 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a ULL;. }.}../* .
f7900 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 ** An implementa
f7910 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f 63 6b tion of the Lock
f7920 46 69 6c 65 28 29 20 41 50 49 20 6f 66 20 77 69 File() API of wi
f7930 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a ndows for wince.
f7940 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 */.static BOOL w
f7950 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 0a 20 20 inceLockFile(.
f7960 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 2c 0a HANDLE *phFile,.
f7970 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 DWORD dwFileOf
f7980 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 fsetLow,. DWORD
f7990 20 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69 67 dwFileOffsetHig
f79a0 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 h,. DWORD nNumb
f79b0 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c erOfBytesToLockL
f79c0 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d ow,. DWORD nNum
f79d0 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b berOfBytesToLock
f79e0 48 69 67 68 0a 29 7b 0a 20 20 77 69 6e 46 69 6c High.){. winFil
f79f0 65 20 2a 70 46 69 6c 65 20 3d 20 48 41 4e 44 4c e *pFile = HANDL
f7a00 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 70 68 46 E_TO_WINFILE(phF
f7a10 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 52 65 ile);. BOOL bRe
f7a20 74 75 72 6e 20 3d 20 46 41 4c 53 45 3b 0a 0a 20 turn = FALSE;..
f7a30 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
f7a40 52 28 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69 R(dwFileOffsetHi
f7a50 67 68 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 gh);. UNUSED_PA
f7a60 52 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 4f RAMETER(nNumberO
f7a70 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 fBytesToLockHigh
f7a80 29 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c 65 );.. if (!pFile
f7a90 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72 6e ->hMutex) return
f7aa0 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d 75 TRUE;. winceMu
f7ab0 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c 65 texAcquire(pFile
f7ac0 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a ->hMutex);.. /*
f7ad0 20 57 61 6e 74 69 6e 67 20 61 6e 20 65 78 63 6c Wanting an excl
f7ae0 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 usive lock? */.
f7af0 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 if (dwFileOffse
f7b00 74 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f 46 tLow == SHARED_F
f7b10 49 52 53 54 0a 20 20 20 20 20 20 20 26 26 20 6e IRST. && n
f7b20 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c NumberOfBytesToL
f7b30 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 ockLow == SHARED
f7b40 5f 53 49 5a 45 29 7b 0a 20 20 20 20 69 66 20 28 _SIZE){. if (
f7b50 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e pFile->shared->n
f7b60 52 65 61 64 65 72 73 20 3d 3d 20 30 20 26 26 20 Readers == 0 &&
f7b70 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 pFile->shared->b
f7b80 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 7b Exclusive == 0){
f7b90 0a 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 . pFile->s
f7ba0 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 hared->bExclusiv
f7bb0 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 e = TRUE;.
f7bc0 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 pFile->local.bE
f7bd0 78 63 6c 75 73 69 76 65 20 3d 20 54 52 55 45 3b xclusive = TRUE;
f7be0 0a 20 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 . bReturn
f7bf0 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 = TRUE;. }.
f7c00 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 }.. /* Want a r
f7c10 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63 6b 3f 20 2a ead-only lock? *
f7c20 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 /. else if (dwF
f7c30 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 ileOffsetLow ==
f7c40 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a SHARED_FIRST &&.
f7c50 20 20 20 20 20 20 20 20 20 20 20 6e 4e 75 6d 62 nNumb
f7c60 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c erOfBytesToLockL
f7c70 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 ow == 1){. if
f7c80 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d (pFile->shared-
f7c90 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 >bExclusive == 0
f7ca0 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
f7cb0 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 2b local.nReaders +
f7cc0 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46 69 +;. if (pFi
f7cd0 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 le->local.nReade
f7ce0 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20 20 20 20 rs == 1){.
f7cf0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
f7d00 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20 >nReaders ++;.
f7d10 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74 }. bRet
f7d20 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
f7d30 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 }. }.. /* Want
f7d40 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f a pending lock?
f7d50 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 */. else if (d
f7d60 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d wFileOffsetLow =
f7d70 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 = PENDING_BYTE &
f7d80 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 & nNumberOfBytes
f7d90 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b ToLockLow == 1){
f7da0 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20 70 65 . /* If no pe
f7db0 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 nding lock has b
f7dc0 65 65 6e 20 61 63 71 75 69 72 65 64 2c 20 74 68 een acquired, th
f7dd0 65 6e 20 61 63 71 75 69 72 65 20 69 74 20 2a 2f en acquire it */
f7de0 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
f7df0 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 shared->bPending
f7e00 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 == 0) {. p
f7e10 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 File->shared->bP
f7e20 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20 ending = TRUE;.
f7e30 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 pFile->loca
f7e40 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 l.bPending = TRU
f7e50 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e E;. bReturn
f7e60 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 = TRUE;. }.
f7e70 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 }.. /* Want a
f7e80 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f 20 2a reserved lock? *
f7e90 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 /. else if (dwF
f7ea0 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 ileOffsetLow ==
f7eb0 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 RESERVED_BYTE &&
f7ec0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 nNumberOfBytesT
f7ed0 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a oLockLow == 1){.
f7ee0 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 if (pFile->s
f7ef0 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 hared->bReserved
f7f00 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 == 0) {. p
f7f10 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 File->shared->bR
f7f20 65 73 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a eserved = TRUE;.
f7f30 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 pFile->loc
f7f40 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d 20 54 al.bReserved = T
f7f50 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 RUE;. bRetu
f7f60 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d rn = TRUE;. }
f7f70 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 . }.. winceMut
f7f80 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d exRelease(pFile-
f7f90 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 >hMutex);. retu
f7fa0 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f rn bReturn;.}../
f7fb0 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e *.** An implemen
f7fc0 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 55 6e tation of the Un
f7fd0 6c 6f 63 6b 46 69 6c 65 20 41 50 49 20 6f 66 20 lockFile API of
f7fe0 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 windows for winc
f7ff0 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c e.*/.static BOOL
f8000 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 winceUnlockFile
f8010 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 (. HANDLE *phFi
f8020 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 le,. DWORD dwFi
f8030 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 leOffsetLow,. D
f8040 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 WORD dwFileOffse
f8050 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e tHigh,. DWORD n
f8060 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 NumberOfBytesToU
f8070 6e 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 nlockLow,. DWOR
f8080 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 D nNumberOfBytes
f8090 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 7b 0a ToUnlockHigh.){.
f80a0 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 winFile *pFile
f80b0 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e = HANDLE_TO_WIN
f80c0 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 FILE(phFile);.
f80d0 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 BOOL bReturn = F
f80e0 41 4c 53 45 3b 0a 0a 20 20 55 4e 55 53 45 44 5f ALSE;.. UNUSED_
f80f0 50 41 52 41 4d 45 54 45 52 28 64 77 46 69 6c 65 PARAMETER(dwFile
f8100 4f 66 66 73 65 74 48 69 67 68 29 3b 0a 20 20 55 OffsetHigh);. U
f8110 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
f8120 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
f8130 55 6e 6c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20 UnlockHigh);..
f8140 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 if (!pFile->hMut
f8150 65 78 29 20 72 65 74 75 72 6e 20 54 52 55 45 3b ex) return TRUE;
f8160 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 . winceMutexAcq
f8170 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 uire(pFile->hMut
f8180 65 78 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 ex);.. /* Relea
f8190 73 69 6e 67 20 61 20 72 65 61 64 65 72 20 6c 6f sing a reader lo
f81a0 63 6b 20 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 ck or an exclusi
f81b0 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 20 ve lock */. if
f81c0 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 (dwFileOffsetLow
f81d0 20 3d 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 == SHARED_FIRST
f81e0 29 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 65 ){. /* Did we
f81f0 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 have an exclusi
f8200 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 ve lock? */.
f8210 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c if (pFile->local
f8220 2e 62 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 .bExclusive){.
f8230 20 20 20 20 61 73 73 65 72 74 28 6e 4e 75 6d 62 assert(nNumb
f8240 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 erOfBytesToUnloc
f8250 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f 53 kLow == SHARED_S
f8260 49 5a 45 29 3b 0a 20 20 20 20 20 20 70 46 69 6c IZE);. pFil
f8270 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 e->local.bExclus
f8280 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 ive = FALSE;.
f8290 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 pFile->shared
f82a0 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 46 ->bExclusive = F
f82b0 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 74 ALSE;. bRet
f82c0 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
f82d0 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 65 }.. /* Did we
f82e0 20 6a 75 73 74 20 68 61 76 65 20 61 20 72 65 61 just have a rea
f82f0 64 65 72 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 der lock? */.
f8300 20 65 6c 73 65 20 69 66 20 28 70 46 69 6c 65 2d else if (pFile-
f8310 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 29 >local.nReaders)
f8320 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e {. assert(n
f8330 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 NumberOfBytesToU
f8340 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 3b 0a nlockLow == 1);.
f8350 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 pFile->loc
f8360 61 6c 2e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a al.nReaders --;.
f8370 20 20 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d if (pFile-
f8380 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 >local.nReaders
f8390 3d 3d 20 30 29 0a 20 20 20 20 20 20 7b 0a 20 20 == 0). {.
f83a0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 pFile->sha
f83b0 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2d 2d red->nReaders --
f83c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
f83d0 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a bReturn = TRUE;.
f83e0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
f83f0 52 65 6c 65 61 73 69 6e 67 20 61 20 70 65 6e 64 Releasing a pend
f8400 69 6e 67 20 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c ing lock */. el
f8410 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 se if (dwFileOff
f8420 73 65 74 4c 6f 77 20 3d 3d 20 50 45 4e 44 49 4e setLow == PENDIN
f8430 47 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 G_BYTE && nNumbe
f8440 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b rOfBytesToUnlock
f8450 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 Low == 1){. i
f8460 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e f (pFile->local.
f8470 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20 bPending){.
f8480 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 pFile->local.bP
f8490 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a ending = FALSE;.
f84a0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 pFile->sha
f84b0 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20 red->bPending =
f84c0 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 FALSE;. bRe
f84d0 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 turn = TRUE;.
f84e0 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 6c 65 }. }. /* Rele
f84f0 61 73 69 6e 67 20 61 20 72 65 73 65 72 76 65 64 asing a reserved
f8500 20 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 lock */. else
f8510 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 if (dwFileOffset
f8520 4c 6f 77 20 3d 3d 20 52 45 53 45 52 56 45 44 5f Low == RESERVED_
f8530 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f BYTE && nNumberO
f8540 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f fBytesToUnlockLo
f8550 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 w == 1){. if
f8560 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 (pFile->local.bR
f8570 65 73 65 72 76 65 64 29 20 7b 0a 20 20 20 20 20 eserved) {.
f8580 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 pFile->local.bR
f8590 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45 3b eserved = FALSE;
f85a0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 . pFile->sh
f85b0 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 ared->bReserved
f85c0 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 = FALSE;. b
f85d0 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 Return = TRUE;.
f85e0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 }. }.. winc
f85f0 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 eMutexRelease(pF
f8600 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 ile->hMutex);.
f8610 72 65 74 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a return bReturn;.
f8620 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c }../*.** An impl
f8630 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
f8640 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 e LockFileEx() A
f8650 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f PI of windows fo
f8660 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 r wince.*/.stati
f8670 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b c BOOL winceLock
f8680 46 69 6c 65 45 78 28 0a 20 20 48 41 4e 44 4c 45 FileEx(. HANDLE
f8690 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 *phFile,. DWOR
f86a0 44 20 64 77 46 6c 61 67 73 2c 0a 20 20 44 57 4f D dwFlags,. DWO
f86b0 52 44 20 64 77 52 65 73 65 72 76 65 64 2c 0a 20 RD dwReserved,.
f86c0 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 DWORD nNumberOf
f86d0 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a BytesToLockLow,.
f86e0 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f DWORD nNumberO
f86f0 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 fBytesToLockHigh
f8700 2c 0a 20 20 4c 50 4f 56 45 52 4c 41 50 50 45 44 ,. LPOVERLAPPED
f8710 20 6c 70 4f 76 65 72 6c 61 70 70 65 64 0a 29 7b lpOverlapped.){
f8720 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
f8730 54 45 52 28 64 77 52 65 73 65 72 76 65 64 29 3b TER(dwReserved);
f8740 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
f8750 54 45 52 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 TER(nNumberOfByt
f8760 65 73 54 6f 4c 6f 63 6b 48 69 67 68 29 3b 0a 0a esToLockHigh);..
f8770 20 20 2f 2a 20 49 66 20 74 68 65 20 63 61 6c 6c /* If the call
f8780 65 72 20 77 61 6e 74 73 20 61 20 73 68 61 72 65 er wants a share
f8790 64 20 72 65 61 64 20 6c 6f 63 6b 2c 20 66 6f 72 d read lock, for
f87a0 77 61 72 64 20 74 68 69 73 20 63 61 6c 6c 0a 20 ward this call.
f87b0 20 2a 2a 20 74 6f 20 77 69 6e 63 65 4c 6f 63 6b ** to winceLock
f87c0 46 69 6c 65 20 2a 2f 0a 20 20 69 66 20 28 6c 70 File */. if (lp
f87d0 4f 76 65 72 6c 61 70 70 65 64 2d 3e 4f 66 66 73 Overlapped->Offs
f87e0 65 74 20 3d 3d 20 53 48 41 52 45 44 5f 46 49 52 et == SHARED_FIR
f87f0 53 54 20 26 26 0a 20 20 20 20 20 20 64 77 46 6c ST &&. dwFl
f8800 61 67 73 20 3d 3d 20 31 20 26 26 0a 20 20 20 20 ags == 1 &&.
f8810 20 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 nNumberOfBytes
f8820 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 ToLockLow == SHA
f8830 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 72 RED_SIZE){. r
f8840 65 74 75 72 6e 20 77 69 6e 63 65 4c 6f 63 6b 46 eturn winceLockF
f8850 69 6c 65 28 70 68 46 69 6c 65 2c 20 53 48 41 52 ile(phFile, SHAR
f8860 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 31 2c 20 ED_FIRST, 0, 1,
f8870 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0);. }. return
f8880 20 46 41 4c 53 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 FALSE;.}./*.**
f8890 45 6e 64 20 6f 66 20 74 68 65 20 73 70 65 63 69 End of the speci
f88a0 61 6c 20 63 6f 64 65 20 66 6f 72 20 77 69 6e 63 al code for winc
f88b0 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.**************
f88c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f88d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f88e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f88f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
f8900 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
f8910 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f E_OS_WINCE */../
f8920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
f8970 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20 The next group
f8980 6f 66 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c of routines impl
f8990 65 6d 65 6e 74 20 74 68 65 20 49 2f 4f 20 6d 65 ement the I/O me
f89a0 74 68 6f 64 73 20 73 70 65 63 69 66 69 65 64 0a thods specified.
f89b0 2a 2a 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 ** by the sqlite
f89c0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 3_io_methods obj
f89d0 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ect..***********
f89e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f89f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8a20 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 ***/../*.** Clos
f8a30 65 20 61 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 e a file..**.**
f8a40 49 74 20 69 73 20 72 65 70 6f 72 74 65 64 20 74 It is reported t
f8a50 68 61 74 20 61 6e 20 61 74 74 65 6d 70 74 20 74 hat an attempt t
f8a60 6f 20 63 6c 6f 73 65 20 61 20 68 61 6e 64 6c 65 o close a handle
f8a70 20 6d 69 67 68 74 20 73 6f 6d 65 74 69 6d 65 73 might sometimes
f8a80 0a 2a 2a 20 66 61 69 6c 2e 20 20 54 68 69 73 20 .** fail. This
f8a90 69 73 20 61 20 76 65 72 79 20 75 6e 72 65 61 73 is a very unreas
f8aa0 6f 6e 61 62 6c 65 20 72 65 73 75 6c 74 2c 20 62 onable result, b
f8ab0 75 74 20 77 69 6e 64 6f 77 73 20 69 73 20 6e 6f ut windows is no
f8ac0 74 6f 72 69 6f 75 73 0a 2a 2a 20 66 6f 72 20 62 torious.** for b
f8ad0 65 69 6e 67 20 75 6e 72 65 61 73 6f 6e 61 62 6c eing unreasonabl
f8ae0 65 20 73 6f 20 49 20 64 6f 20 6e 6f 74 20 64 6f e so I do not do
f8af0 75 62 74 20 74 68 61 74 20 69 74 20 6d 69 67 68 ubt that it migh
f8b00 74 20 68 61 70 70 65 6e 2e 20 20 49 66 0a 2a 2a t happen. If.**
f8b10 20 74 68 65 20 63 6c 6f 73 65 20 66 61 69 6c 73 the close fails
f8b20 2c 20 77 65 20 70 61 75 73 65 20 66 6f 72 20 31 , we pause for 1
f8b30 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 00 milliseconds
f8b40 61 6e 64 20 74 72 79 20 61 67 61 69 6e 2e 20 20 and try again.
f8b50 41 73 0a 2a 2a 20 6d 61 6e 79 20 61 73 20 4d 58 As.** many as MX
f8b60 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 61 _CLOSE_ATTEMPT a
f8b70 74 74 65 6d 70 74 73 20 74 6f 20 63 6c 6f 73 65 ttempts to close
f8b80 20 74 68 65 20 68 61 6e 64 6c 65 20 61 72 65 20 the handle are
f8b90 6d 61 64 65 20 62 65 66 6f 72 65 0a 2a 2a 20 67 made before.** g
f8ba0 69 76 69 6e 67 20 75 70 20 61 6e 64 20 72 65 74 iving up and ret
f8bb0 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e urning an error.
f8bc0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 .*/.#define MX_C
f8bd0 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 33 0a 73 LOSE_ATTEMPT 3.s
f8be0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 43 6c 6f tatic int winClo
f8bf0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 se(sqlite3_file
f8c00 2a 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20 *id){. int rc,
f8c10 63 6e 74 20 3d 20 30 3b 0a 20 20 77 69 6e 46 69 cnt = 0;. winFi
f8c20 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
f8c30 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 File*)id;.. ass
f8c40 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 ert( id!=0 );.
f8c50 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53 45 20 OSTRACE2("CLOSE
f8c60 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 %d\n", pFile->h)
f8c70 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 72 63 20 3d ;. do{. rc =
f8c80 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 CloseHandle(pFi
f8c90 6c 65 2d 3e 68 29 3b 0a 20 20 7d 77 68 69 6c 65 le->h);. }while
f8ca0 28 20 72 63 3d 3d 30 20 26 26 20 2b 2b 63 6e 74 ( rc==0 && ++cnt
f8cb0 20 3c 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 < MX_CLOSE_ATTE
f8cc0 4d 50 54 20 26 26 20 28 53 6c 65 65 70 28 31 30 MPT && (Sleep(10
f8cd0 30 29 2c 20 31 29 20 29 3b 0a 23 69 66 20 53 51 0), 1) );.#if SQ
f8ce0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 64 LITE_OS_WINCE.#d
f8cf0 65 66 69 6e 65 20 57 49 4e 43 45 5f 44 45 4c 45 efine WINCE_DELE
f8d00 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 33 0a TION_ATTEMPTS 3.
f8d10 20 20 77 69 6e 63 65 44 65 73 74 72 6f 79 4c 6f winceDestroyLo
f8d20 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 ck(pFile);. if(
f8d30 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f pFile->zDeleteO
f8d40 6e 43 6c 6f 73 65 20 29 7b 0a 20 20 20 20 69 6e nClose ){. in
f8d50 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 77 t cnt = 0;. w
f8d60 68 69 6c 65 28 0a 20 20 20 20 20 20 20 20 20 20 hile(.
f8d70 20 44 65 6c 65 74 65 46 69 6c 65 57 28 70 46 69 DeleteFileW(pFi
f8d80 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f le->zDeleteOnClo
f8d90 73 65 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 26 se)==0. &
f8da0 26 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 & GetFileAttribu
f8db0 74 65 73 57 28 70 46 69 6c 65 2d 3e 7a 44 65 6c tesW(pFile->zDel
f8dc0 65 74 65 4f 6e 43 6c 6f 73 65 29 21 3d 30 78 66 eteOnClose)!=0xf
f8dd0 66 66 66 66 66 66 66 20 0a 20 20 20 20 20 20 20 fffffff .
f8de0 20 26 26 20 63 6e 74 2b 2b 20 3c 20 57 49 4e 43 && cnt++ < WINC
f8df0 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d E_DELETION_ATTEM
f8e00 50 54 53 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 PTS. ){.
f8e10 20 20 53 6c 65 65 70 28 31 30 30 29 3b 20 20 2f Sleep(100); /
f8e20 2a 20 57 61 69 74 20 61 20 6c 69 74 74 6c 65 20 * Wait a little
f8e30 62 65 66 6f 72 65 20 74 72 79 69 6e 67 20 61 67 before trying ag
f8e40 61 69 6e 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 ain */. }.
f8e50 20 66 72 65 65 28 70 46 69 6c 65 2d 3e 7a 44 65 free(pFile->zDe
f8e60 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3b 0a 20 20 leteOnClose);.
f8e70 7d 0a 23 65 6e 64 69 66 0a 20 20 4f 70 65 6e 43 }.#endif. OpenC
f8e80 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 72 65 ounter(-1);. re
f8e90 74 75 72 6e 20 72 63 20 3f 20 53 51 4c 49 54 45 turn rc ? SQLITE
f8ea0 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 _OK : SQLITE_IOE
f8eb0 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d RR;.}../*.** Som
f8ec0 65 20 6d 69 63 72 6f 73 6f 66 74 20 63 6f 6d 70 e microsoft comp
f8ed0 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 69 73 20 ilers lack this
f8ee0 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 definition..*/.#
f8ef0 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 44 5f 53 ifndef INVALID_S
f8f00 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 0a ET_FILE_POINTER.
f8f10 23 20 64 65 66 69 6e 65 20 49 4e 56 41 4c 49 44 # define INVALID
f8f20 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 _SET_FILE_POINTE
f8f30 52 20 28 28 44 57 4f 52 44 29 2d 31 29 0a 23 65 R ((DWORD)-1).#e
f8f40 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 ndif../*.** Read
f8f50 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c data from a fil
f8f60 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e e into a buffer.
f8f70 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f Return SQLITE_
f8f80 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 OK if all.** byt
f8f90 65 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 es were read suc
f8fa0 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 cessfully and SQ
f8fb0 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e LITE_IOERR if an
f8fc0 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 ything goes.** w
f8fd0 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rong..*/.static
f8fe0 69 6e 74 20 77 69 6e 52 65 61 64 28 0a 20 20 73 int winRead(. s
f8ff0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
f9000 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c /* Fil
f9010 65 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a e to read from *
f9020 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 /. void *pBuf,
f9030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
f9040 2a 20 57 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 * Write content
f9050 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 72 into this buffer
f9060 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 */. int amt,
f9070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f9080 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
f9090 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 tes to read */.
f90a0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f sqlite3_int64 o
f90b0 66 66 73 65 74 20 20 20 20 20 20 20 2f 2a 20 42 ffset /* B
f90c0 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 egin reading at
f90d0 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 this offset */.)
f90e0 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 {. LONG upperBi
f90f0 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 6f 66 66 ts = (LONG)((off
f9100 73 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66 66 set>>32) & 0x7ff
f9110 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c fffff);. LONG l
f9120 6f 77 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 owerBits = (LONG
f9130 29 28 6f 66 66 73 65 74 20 26 20 30 78 66 66 66 )(offset & 0xfff
f9140 66 66 66 66 66 29 3b 0a 20 20 44 57 4f 52 44 20 fffff);. DWORD
f9150 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 rc;. winFile *p
f9160 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a File = (winFile*
f9170 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 )id;. DWORD err
f9180 6f 72 3b 0a 20 20 44 57 4f 52 44 20 67 6f 74 3b or;. DWORD got;
f9190 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d .. assert( id!=
f91a0 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 0 );. SimulateI
f91b0 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 OError(return SQ
f91c0 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 29 LITE_IOERR_READ)
f91d0 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 52 45 ;. OSTRACE3("RE
f91e0 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 AD %d lock=%d\n"
f91f0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c , pFile->h, pFil
f9200 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 e->locktype);.
f9210 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e rc = SetFilePoin
f9220 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f ter(pFile->h, lo
f9230 77 65 72 42 69 74 73 2c 20 26 75 70 70 65 72 42 werBits, &upperB
f9240 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 its, FILE_BEGIN)
f9250 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 ;. if( rc==INVA
f9260 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 LID_SET_FILE_POI
f9270 4e 54 45 52 20 26 26 20 28 65 72 72 6f 72 3d 47 NTER && (error=G
f9280 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 21 3d etLastError())!=
f9290 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
f92a0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
f92b0 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 = error;. re
f92c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c turn SQLITE_FULL
f92d0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 52 65 61 ;. }. if( !Rea
f92e0 64 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 dFile(pFile->h,
f92f0 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f 74 2c pBuf, amt, &got,
f9300 20 30 29 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 0) ){. pFile
f9310 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 ->lastErrno = Ge
f9320 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 tLastError();.
f9330 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
f9340 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d 0a IOERR_READ;. }.
f9350 20 20 69 66 28 20 67 6f 74 3d 3d 28 44 57 4f 52 if( got==(DWOR
f9360 44 29 61 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 D)amt ){. ret
f9370 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
f9380 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 55 }else{. /* U
f9390 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74 nread parts of t
f93a0 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 he buffer must b
f93b0 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f e zero-filled */
f93c0 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 . memset(&((c
f93d0 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c har*)pBuf)[got],
f93e0 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 0, amt-got);.
f93f0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
f9400 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 IOERR_SHORT_READ
f9410 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 ;. }.}../*.** W
f9420 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d 20 61 rite data from a
f9430 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 20 66 buffer into a f
f9440 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c ile. Return SQL
f9450 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 ITE_OK on succes
f9460 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 s.** or some oth
f9470 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e er error code on
f9480 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 failure..*/.sta
f9490 74 69 63 20 69 6e 74 20 77 69 6e 57 72 69 74 65 tic int winWrite
f94a0 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 (. sqlite3_file
f94b0 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 2f 2a *id, /*
f94c0 20 46 69 6c 65 20 74 6f 20 77 72 69 74 65 20 69 File to write i
f94d0 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 nto */. const v
f94e0 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 oid *pBuf,
f94f0 20 20 20 2f 2a 20 54 68 65 20 62 79 74 65 73 20 /* The bytes
f9500 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 2a 2f to be written */
f9510 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 . int amt,
f9520 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f9530 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
f9540 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 to write */. sq
f9550 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 lite3_int64 offs
f9560 65 74 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 et /* Offse
f9570 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 t into the file
f9580 74 6f 20 62 65 67 69 6e 20 77 72 69 74 69 6e 67 to begin writing
f9590 20 61 74 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 at */.){. LONG
f95a0 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c 4f upperBits = (LO
f95b0 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e 33 32 29 NG)((offset>>32)
f95c0 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a & 0x7fffffff);.
f95d0 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 LONG lowerBits
f95e0 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66 73 65 74 = (LONG)(offset
f95f0 20 26 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a & 0xffffffff);.
f9600 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 DWORD rc;. wi
f9610 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 nFile *pFile = (
f9620 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 winFile*)id;. D
f9630 57 4f 52 44 20 65 72 72 6f 72 3b 0a 20 20 44 57 WORD error;. DW
f9640 4f 52 44 20 77 72 6f 74 65 20 3d 20 30 3b 0a 0a ORD wrote = 0;..
f9650 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 assert( id!=0
f9660 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
f9670 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 rror(return SQLI
f9680 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 29 3b TE_IOERR_WRITE);
f9690 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 . SimulateDiskf
f96a0 75 6c 6c 45 72 72 6f 72 28 72 65 74 75 72 6e 20 ullError(return
f96b0 53 51 4c 49 54 45 5f 46 55 4c 4c 29 3b 0a 20 20 SQLITE_FULL);.
f96c0 4f 53 54 52 41 43 45 33 28 22 57 52 49 54 45 20 OSTRACE3("WRITE
f96d0 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 %d lock=%d\n", p
f96e0 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e File->h, pFile->
f96f0 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 72 63 20 locktype);. rc
f9700 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 = SetFilePointer
f9710 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 (pFile->h, lower
f9720 42 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73 Bits, &upperBits
f9730 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 , FILE_BEGIN);.
f9740 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 if( rc==INVALID
f9750 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 _SET_FILE_POINTE
f9760 52 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c R && (error=GetL
f9770 61 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f astError())!=NO_
f9780 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 ERROR ){. pFi
f9790 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
f97a0 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 error;. retur
f97b0 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 n SQLITE_FULL;.
f97c0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74 }. assert( amt
f97d0 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 0a 20 >0 );. while(.
f97e0 20 20 20 20 61 6d 74 3e 30 0a 20 20 20 20 20 26 amt>0. &
f97f0 26 20 28 72 63 20 3d 20 57 72 69 74 65 46 69 6c & (rc = WriteFil
f9800 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 e(pFile->h, pBuf
f9810 2c 20 61 6d 74 2c 20 26 77 72 6f 74 65 2c 20 30 , amt, &wrote, 0
f9820 29 29 21 3d 30 0a 20 20 20 20 20 26 26 20 77 72 ))!=0. && wr
f9830 6f 74 65 3e 30 0a 20 20 29 7b 0a 20 20 20 20 61 ote>0. ){. a
f9840 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 mt -= wrote;.
f9850 20 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a pBuf = &((char*
f9860 29 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 )pBuf)[wrote];.
f9870 20 7d 0a 20 20 69 66 28 20 21 72 63 20 7c 7c 20 }. if( !rc ||
f9880 61 6d 74 3e 28 69 6e 74 29 77 72 6f 74 65 20 29 amt>(int)wrote )
f9890 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 {. pFile->las
f98a0 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 tErrno = GetLast
f98b0 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 Error();. ret
f98c0 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b urn SQLITE_FULL;
f98d0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
f98e0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
f98f0 2a 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 * Truncate an op
f9900 65 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 en file to a spe
f9910 63 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 cified size.*/.s
f9920 74 61 74 69 63 20 69 6e 74 20 77 69 6e 54 72 75 tatic int winTru
f9930 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 ncate(sqlite3_fi
f9940 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f le *id, sqlite3_
f9950 69 6e 74 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 int64 nByte){.
f9960 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 3d LONG upperBits =
f9970 20 28 4c 4f 4e 47 29 28 28 6e 42 79 74 65 3e 3e (LONG)((nByte>>
f9980 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66 66 32) & 0x7fffffff
f9990 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 );. LONG lowerB
f99a0 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6e 42 79 its = (LONG)(nBy
f99b0 74 65 20 26 20 30 78 66 66 66 66 66 66 66 66 29 te & 0xffffffff)
f99c0 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 ;. DWORD rc;.
f99d0 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d winFile *pFile =
f99e0 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 (winFile*)id;.
f99f0 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20 DWORD error;..
f9a00 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 assert( id!=0 )
f9a10 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 54 52 ;. OSTRACE3("TR
f9a20 55 4e 43 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e UNCATE %d %lld\n
f9a30 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 ", pFile->h, nBy
f9a40 74 65 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 te);. SimulateI
f9a50 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 OError(return SQ
f9a60 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 LITE_IOERR_TRUNC
f9a70 41 54 45 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 ATE);. rc = Set
f9a80 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c FilePointer(pFil
f9a90 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c e->h, lowerBits,
f9aa0 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c &upperBits, FIL
f9ab0 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 E_BEGIN);. if(
f9ac0 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f rc==INVALID_SET_
f9ad0 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 FILE_POINTER &&
f9ae0 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 45 72 (error=GetLastEr
f9af0 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 ror())!=NO_ERROR
f9b00 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ){. pFile->l
f9b10 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 astErrno = error
f9b20 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
f9b30 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 ITE_IOERR_TRUNCA
f9b40 54 45 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 65 74 TE;. }. /* Set
f9b50 45 6e 64 4f 66 46 69 6c 65 20 77 69 6c 6c 20 66 EndOfFile will f
f9b60 61 69 6c 20 69 66 20 6e 42 79 74 65 20 69 73 20 ail if nByte is
f9b70 6e 65 67 61 74 69 76 65 20 2a 2f 0a 20 20 69 66 negative */. if
f9b80 28 20 21 53 65 74 45 6e 64 4f 66 46 69 6c 65 28 ( !SetEndOfFile(
f9b90 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20 pFile->h) ){.
f9ba0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
f9bb0 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 o = GetLastError
f9bc0 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ();. return S
f9bd0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e QLITE_IOERR_TRUN
f9be0 43 41 54 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 CATE;. }. retu
f9bf0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
f9c00 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
f9c10 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 EST./*.** Count
f9c20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 the number of fu
f9c30 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d llsyncs and norm
f9c40 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 al syncs. This
f9c50 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a is used to test.
f9c60 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e ** that syncs an
f9c70 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 d fullsyncs are
f9c80 6f 63 63 75 72 69 6e 67 20 61 74 20 74 68 65 20 occuring at the
f9c90 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a right times..*/.
f9ca0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
f9cb0 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e qlite3_sync_coun
f9cc0 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 t = 0;.SQLITE_AP
f9cd0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 I int sqlite3_fu
f9ce0 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 llsync_count = 0
f9cf0 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
f9d00 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72 Make sure all wr
f9d10 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 63 ites to a partic
f9d20 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f ular file are co
f9d30 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e mmitted to disk.
f9d40 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
f9d50 69 6e 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 inSync(sqlite3_f
f9d60 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 ile *id, int fla
f9d70 67 73 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c gs){.#ifndef SQL
f9d80 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 77 69 ITE_NO_SYNC. wi
f9d90 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 nFile *pFile = (
f9da0 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 winFile*)id;..
f9db0 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b assert( id!=0 );
f9dc0 0a 20 20 4f 53 54 52 41 43 45 33 28 22 53 59 4e . OSTRACE3("SYN
f9dd0 43 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c C %d lock=%d\n",
f9de0 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 pFile->h, pFile
f9df0 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 23 65 6c ->locktype);.#el
f9e00 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 se. UNUSED_PARA
f9e10 4d 45 54 45 52 28 69 64 29 3b 0a 23 65 6e 64 69 METER(id);.#endi
f9e20 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 f.#ifndef SQLITE
f9e30 5f 54 45 53 54 0a 20 20 55 4e 55 53 45 44 5f 50 _TEST. UNUSED_P
f9e40 41 52 41 4d 45 54 45 52 28 66 6c 61 67 73 29 3b ARAMETER(flags);
f9e50 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 66 6c 61 .#else. if( fla
f9e60 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 gs & SQLITE_SYNC
f9e70 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 73 71 6c _FULL ){. sql
f9e80 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f ite3_fullsync_co
f9e90 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c unt++;. }. sql
f9ea0 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b ite3_sync_count+
f9eb0 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 +;.#endif. /* I
f9ec0 66 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 f we compiled wi
f9ed0 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f th the SQLITE_NO
f9ee0 5f 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e _SYNC flag, then
f9ef0 20 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 syncing is a.
f9f00 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 ** no-op. */.#i
f9f10 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 fdef SQLITE_NO_S
f9f20 59 4e 43 0a 20 20 20 20 72 65 74 75 72 6e 20 53 YNC. return S
f9f30 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a QLITE_OK;.#else.
f9f40 20 20 69 66 28 20 46 6c 75 73 68 46 69 6c 65 42 if( FlushFileB
f9f50 75 66 66 65 72 73 28 70 46 69 6c 65 2d 3e 68 29 uffers(pFile->h)
f9f60 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
f9f70 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 QLITE_OK;. }els
f9f80 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 e{. pFile->la
f9f90 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 stErrno = GetLas
f9fa0 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 tError();. re
f9fb0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
f9fc0 52 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a R;. }.#endif.}.
f9fd0 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 ./*.** Determine
f9fe0 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a the current siz
f9ff0 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62 e of a file in b
fa000 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ytes.*/.static i
fa010 6e 74 20 77 69 6e 46 69 6c 65 53 69 7a 65 28 73 nt winFileSize(s
fa020 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
fa030 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a sqlite3_int64 *
fa040 70 53 69 7a 65 29 7b 0a 20 20 44 57 4f 52 44 20 pSize){. DWORD
fa050 75 70 70 65 72 42 69 74 73 3b 0a 20 20 44 57 4f upperBits;. DWO
fa060 52 44 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 RD lowerBits;.
fa070 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d winFile *pFile =
fa080 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 (winFile*)id;.
fa090 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20 DWORD error;..
fa0a0 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 assert( id!=0 )
fa0b0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
fa0c0 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 ror(return SQLIT
fa0d0 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 29 3b 0a E_IOERR_FSTAT);.
fa0e0 20 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 47 65 lowerBits = Ge
fa0f0 74 46 69 6c 65 53 69 7a 65 28 70 46 69 6c 65 2d tFileSize(pFile-
fa100 3e 68 2c 20 26 75 70 70 65 72 42 69 74 73 29 3b >h, &upperBits);
fa110 0a 20 20 69 66 28 20 20 20 28 6c 6f 77 65 72 42 . if( (lowerB
fa120 69 74 73 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 its == INVALID_F
fa130 49 4c 45 5f 53 49 5a 45 29 0a 20 20 20 20 20 26 ILE_SIZE). &
fa140 26 20 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c & ((error = GetL
fa150 61 73 74 45 72 72 6f 72 28 29 29 20 21 3d 20 4e astError()) != N
fa160 4f 5f 45 52 52 4f 52 29 20 29 0a 20 20 7b 0a 20 O_ERROR) ). {.
fa170 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
fa180 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 rno = error;.
fa190 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
fa1a0 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a OERR_FSTAT;. }.
fa1b0 20 20 2a 70 53 69 7a 65 20 3d 20 28 28 28 73 71 *pSize = (((sq
fa1c0 6c 69 74 65 33 5f 69 6e 74 36 34 29 75 70 70 65 lite3_int64)uppe
fa1d0 72 42 69 74 73 29 3c 3c 33 32 29 20 2b 20 6c 6f rBits)<<32) + lo
fa1e0 77 65 72 42 69 74 73 3b 0a 20 20 72 65 74 75 72 werBits;. retur
fa1f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
fa200 2f 2a 0a 2a 2a 20 4c 4f 43 4b 46 49 4c 45 5f 46 /*.** LOCKFILE_F
fa210 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 AIL_IMMEDIATELY
fa220 69 73 20 75 6e 64 65 66 69 6e 65 64 20 6f 6e 20 is undefined on
fa230 73 6f 6d 65 20 57 69 6e 64 6f 77 73 20 73 79 73 some Windows sys
fa240 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 tems..*/.#ifndef
fa250 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 LOCKFILE_FAIL_I
fa260 4d 4d 45 44 49 41 54 45 4c 59 0a 23 20 64 65 66 MMEDIATELY.# def
fa270 69 6e 65 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 ine LOCKFILE_FAI
fa280 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 31 0a L_IMMEDIATELY 1.
fa290 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 #endif../*.** Ac
fa2a0 71 75 69 72 65 20 61 20 72 65 61 64 65 72 20 6c quire a reader l
fa2b0 6f 63 6b 2e 0a 2a 2a 20 44 69 66 66 65 72 65 6e ock..** Differen
fa2c0 74 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 61 t API routines a
fa2d0 72 65 20 63 61 6c 6c 65 64 20 64 65 70 65 6e 64 re called depend
fa2e0 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20 6f ing on whether o
fa2f0 72 20 6e 6f 74 20 74 68 69 73 0a 2a 2a 20 69 73 r not this.** is
fa300 20 57 69 6e 39 35 20 6f 72 20 57 69 6e 4e 54 2e Win95 or WinNT.
fa310 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 .*/.static int g
fa320 65 74 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 etReadLock(winFi
fa330 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e le *pFile){. in
fa340 74 20 72 65 73 3b 0a 20 20 69 66 28 20 69 73 4e t res;. if( isN
fa350 54 28 29 20 29 7b 0a 20 20 20 20 4f 56 45 52 4c T() ){. OVERL
fa360 41 50 50 45 44 20 6f 76 6c 70 3b 0a 20 20 20 20 APPED ovlp;.
fa370 6f 76 6c 70 2e 4f 66 66 73 65 74 20 3d 20 53 48 ovlp.Offset = SH
fa380 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 ARED_FIRST;.
fa390 6f 76 6c 70 2e 4f 66 66 73 65 74 48 69 67 68 20 ovlp.OffsetHigh
fa3a0 3d 20 30 3b 0a 20 20 20 20 6f 76 6c 70 2e 68 45 = 0;. ovlp.hE
fa3b0 76 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 72 65 vent = 0;. re
fa3c0 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 45 78 28 70 s = LockFileEx(p
fa3d0 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 46 49 4c File->h, LOCKFIL
fa3e0 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 E_FAIL_IMMEDIATE
fa3f0 4c 59 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 LY,.
fa400 20 20 20 20 20 20 20 20 20 30 2c 20 53 48 41 52 0, SHAR
fa410 45 44 5f 53 49 5a 45 2c 20 30 2c 20 26 6f 76 6c ED_SIZE, 0, &ovl
fa420 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 p);./* isNT() is
fa430 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 1 if SQLITE_OS_
fa440 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 WINCE==1, so thi
fa450 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 s else is never
fa460 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 executed. .*/.#i
fa470 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
fa480 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 E==0. }else{.
fa490 20 20 69 6e 74 20 6c 6b 3b 0a 20 20 20 20 73 71 int lk;. sq
fa4a0 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 lite3_randomness
fa4b0 28 73 69 7a 65 6f 66 28 6c 6b 29 2c 20 26 6c 6b (sizeof(lk), &lk
fa4c0 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 );. pFile->sh
fa4d0 61 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 28 aredLockByte = (
fa4e0 73 68 6f 72 74 29 28 28 6c 6b 20 26 20 30 78 37 short)((lk & 0x7
fa4f0 66 66 66 66 66 66 66 29 25 28 53 48 41 52 45 44 fffffff)%(SHARED
fa500 5f 53 49 5a 45 20 2d 20 31 29 29 3b 0a 20 20 20 _SIZE - 1));.
fa510 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 res = LockFile(
fa520 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 pFile->h, SHARED
fa530 5f 46 49 52 53 54 2b 70 46 69 6c 65 2d 3e 73 68 _FIRST+pFile->sh
fa540 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c aredLockByte, 0,
fa550 20 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 1, 0);.#endif.
fa560 20 7d 0a 20 20 69 66 28 20 72 65 73 20 3d 3d 20 }. if( res ==
fa570 30 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 0 ){. pFile->
fa580 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c lastErrno = GetL
fa590 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a astError();. }.
fa5a0 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a return res;.}.
fa5b0 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 ./*.** Undo a re
fa5c0 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 adlock.*/.static
fa5d0 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c int unlockReadL
fa5e0 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 ock(winFile *pFi
fa5f0 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 3b 0a le){. int res;.
fa600 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a if( isNT() ){.
fa610 20 20 20 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b res = Unlock
fa620 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 File(pFile->h, S
fa630 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 HARED_FIRST, 0,
fa640 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b SHARED_SIZE, 0);
fa650 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 ./* isNT() is 1
fa660 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
fa670 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 CE==1, so this e
fa680 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 lse is never exe
fa690 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 cuted. .*/.#if S
fa6a0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
fa6b0 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 0. }else{. r
fa6c0 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 es = UnlockFile(
fa6d0 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 pFile->h, SHARED
fa6e0 5f 46 49 52 53 54 20 2b 20 70 46 69 6c 65 2d 3e _FIRST + pFile->
fa6f0 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 sharedLockByte,
fa700 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0, 1, 0);.#endif
fa710 0a 20 20 7d 0a 20 20 69 66 28 20 72 65 73 20 3d . }. if( res =
fa720 3d 20 30 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 = 0 ){. pFile
fa730 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 ->lastErrno = Ge
fa740 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 tLastError();.
fa750 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a }. return res;.
fa760 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 }../*.** Lock th
fa770 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 e file with the
fa780 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 lock specified b
fa790 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b y parameter lock
fa7a0 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 type - one.** of
fa7b0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
fa7c0 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 **.** (1) SH
fa7d0 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 ARED_LOCK.**
fa7e0 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f (2) RESERVED_LO
fa7f0 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 CK.** (3) PE
fa800 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 NDING_LOCK.**
fa810 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f (4) EXCLUSIVE_
fa820 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 LOCK.**.** Somet
fa830 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 imes when reques
fa840 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 ting one lock st
fa850 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 ate, additional
fa860 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 lock states.** a
fa870 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 re inserted in b
fa880 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 etween. The loc
fa890 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 king might fail
fa8a0 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 on one of the la
fa8b0 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f ter.** transitio
fa8c0 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c ns leaving the l
fa8d0 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 ock state differ
fa8e0 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 ent from what it
fa8f0 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 started but.**
fa900 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 still short of i
fa910 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f ts goal. The fo
fa920 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 llowing chart sh
fa930 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a ows the allowed.
fa940 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 ** transitions a
fa950 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 nd the inserted
fa960 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 intermediate sta
fa970 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e tes:.**.** UN
fa980 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 LOCKED -> SHARED
fa990 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
fa9a0 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 RESERVED.**
fa9b0 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 SHARED -> (PENDI
fa9c0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
fa9d0 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 .** RESERVED
fa9e0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
fa9f0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
faa00 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 PENDING -> EXCLU
faa10 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 SIVE.**.** This
faa20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c routine will onl
faa30 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 y increase a loc
faa40 6b 2e 20 20 54 68 65 20 77 69 6e 55 6e 6c 6f 63 k. The winUnloc
faa50 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 k() routine.** e
faa60 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 rases all locks
faa70 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 75 at once and retu
faa80 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74 65 rns us immediate
faa90 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 ly to locking le
faaa0 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20 vel 0..** It is
faab0 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 not possible to
faac0 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e lower the lockin
faad0 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70 g level one step
faae0 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75 at a time. You
faaf0 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61 .** must go stra
fab00 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 ight to locking
fab10 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 level 0..*/.stat
fab20 69 63 20 69 6e 74 20 77 69 6e 4c 6f 63 6b 28 73 ic int winLock(s
fab30 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
fab40 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a int locktype){.
fab50 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
fab60 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 52 65 74 75 E_OK; /* Retu
fab70 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 rn code from sub
fab80 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 69 6e routines */. in
fab90 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20 20 20 t res = 1;
faba0 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f /* Result o
fabb0 66 20 61 20 77 69 6e 64 6f 77 73 20 6c 6f 63 6b f a windows lock
fabc0 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e call */. int n
fabd0 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 ewLocktype;
fabe0 20 20 2f 2a 20 53 65 74 20 70 46 69 6c 65 2d 3e /* Set pFile->
fabf0 6c 6f 63 6b 74 79 70 65 20 74 6f 20 74 68 69 73 locktype to this
fac00 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 65 78 value before ex
fac10 69 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67 iting */. int g
fac20 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 otPendingLock =
fac30 30 3b 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 0;/* True if we
fac40 61 63 71 75 69 72 65 64 20 61 20 50 45 4e 44 49 acquired a PENDI
fac50 4e 47 20 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d NG lock this tim
fac60 65 20 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20 2a e */. winFile *
fac70 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 pFile = (winFile
fac80 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 *)id;. DWORD er
fac90 72 6f 72 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a ror = NO_ERROR;.
faca0 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 . assert( id!=0
facb0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 );. OSTRACE5("
facc0 4c 4f 43 4b 20 25 64 20 25 64 20 77 61 73 20 25 LOCK %d %d was %
facd0 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20 20 20 d(%d)\n",.
face0 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f pFile->h, lo
facf0 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c cktype, pFile->l
fad00 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e ocktype, pFile->
fad10 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b sharedLockByte);
fad20 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 .. /* If there
fad30 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 is already a loc
fad40 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 6f k of this type o
fad50 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 69 r more restricti
fad60 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 4f ve on the. ** O
fad70 73 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e sFile, do nothin
fad80 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 g. Don't use the
fad90 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 end_lock: exit
fada0 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 73 71 path, as. ** sq
fadb0 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d 75 74 65 lite3OsEnterMute
fadc0 78 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 x() hasn't been
fadd0 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f called yet.. */
fade0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
fadf0 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 cktype>=locktype
fae00 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
fae10 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 QLITE_OK;. }..
fae20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 /* Make sure th
fae30 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e e locking sequen
fae40 63 65 20 69 73 20 63 6f 72 72 65 63 74 0a 20 20 ce is correct.
fae50 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 */. assert( pFi
fae60 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f le->locktype!=NO
fae70 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 _LOCK || locktyp
fae80 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
fae90 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b ;. assert( lock
faea0 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f type!=PENDING_LO
faeb0 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 CK );. assert(
faec0 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56 locktype!=RESERV
faed0 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 ED_LOCK || pFile
faee0 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ->locktype==SHAR
faef0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a ED_LOCK );.. /*
faf00 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e 44 49 4e Lock the PENDIN
faf10 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 66 20 77 G_LOCK byte if w
faf20 65 20 6e 65 65 64 20 74 6f 20 61 63 71 75 69 72 e need to acquir
faf30 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b e a PENDING lock
faf40 20 6f 72 0a 20 20 2a 2a 20 61 20 53 48 41 52 45 or. ** a SHARE
faf50 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77 65 20 61 D lock. If we a
faf60 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20 53 re acquiring a S
faf70 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 HARED lock, the
faf80 61 63 71 75 69 73 69 74 69 6f 6e 20 6f 66 0a 20 acquisition of.
faf90 20 2a 2a 20 74 68 65 20 50 45 4e 44 49 4e 47 5f ** the PENDING_
fafa0 4c 4f 43 4b 20 62 79 74 65 20 69 73 20 74 65 6d LOCK byte is tem
fafb0 70 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e porary.. */. n
fafc0 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 70 46 69 ewLocktype = pFi
fafd0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 le->locktype;.
fafe0 69 66 28 20 20 20 28 70 46 69 6c 65 2d 3e 6c 6f if( (pFile->lo
faff0 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 29 cktype==NO_LOCK)
fb000 0a 20 20 20 20 20 7c 7c 20 28 20 20 20 28 6c 6f . || ( (lo
fb010 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 cktype==EXCLUSIV
fb020 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 20 20 20 20 E_LOCK).
fb030 20 26 26 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b && (pFile->lock
fb040 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c type==RESERVED_L
fb050 4f 43 4b 29 29 0a 20 20 29 7b 0a 20 20 20 20 69 OCK)). ){. i
fb060 6e 74 20 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 nt cnt = 3;.
fb070 77 68 69 6c 65 28 20 63 6e 74 2d 2d 3e 30 20 26 while( cnt-->0 &
fb080 26 20 28 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c & (res = LockFil
fb090 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 e(pFile->h, PEND
fb0a0 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 ING_BYTE, 0, 1,
fb0b0 30 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 0))==0 ){.
fb0c0 2f 2a 20 54 72 79 20 33 20 74 69 6d 65 73 20 74 /* Try 3 times t
fb0d0 6f 20 67 65 74 20 74 68 65 20 70 65 6e 64 69 6e o get the pendin
fb0e0 67 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70 65 6e g lock. The pen
fb0f0 64 69 6e 67 20 6c 6f 63 6b 20 6d 69 67 68 74 20 ding lock might
fb100 62 65 0a 20 20 20 20 20 20 2a 2a 20 68 65 6c 64 be. ** held
fb110 20 62 79 20 61 6e 6f 74 68 65 72 20 72 65 61 64 by another read
fb120 65 72 20 70 72 6f 63 65 73 73 20 77 68 6f 20 77 er process who w
fb130 69 6c 6c 20 72 65 6c 65 61 73 65 20 69 74 20 6d ill release it m
fb140 6f 6d 65 6e 74 61 72 69 6c 79 2e 0a 20 20 20 20 omentarily..
fb150 20 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 */. OSTRA
fb160 43 45 32 28 22 63 6f 75 6c 64 20 6e 6f 74 20 67 CE2("could not g
fb170 65 74 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 et a PENDING loc
fb180 6b 2e 20 63 6e 74 3d 25 64 5c 6e 22 2c 20 63 6e k. cnt=%d\n", cn
fb190 74 29 3b 0a 20 20 20 20 20 20 53 6c 65 65 70 28 t);. Sleep(
fb1a0 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 1);. }. go
fb1b0 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 72 tPendingLock = r
fb1c0 65 73 3b 0a 20 20 20 20 69 66 28 20 21 72 65 73 es;. if( !res
fb1d0 20 29 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20 ){. error
fb1e0 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 = GetLastError()
fb1f0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
fb200 2a 20 41 63 71 75 69 72 65 20 61 20 73 68 61 72 * Acquire a shar
fb210 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 ed lock. */. i
fb220 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 f( locktype==SHA
fb230 52 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 RED_LOCK && res
fb240 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
fb250 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
fb260 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 NO_LOCK );. r
fb270 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b es = getReadLock
fb280 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 (pFile);. if(
fb290 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 res ){. ne
fb2a0 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 wLocktype = SHAR
fb2b0 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c ED_LOCK;. }el
fb2c0 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20 se{. error
fb2d0 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 = GetLastError()
fb2e0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
fb2f0 2a 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45 * Acquire a RESE
fb300 52 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 RVED lock. */.
fb310 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 if( locktype==R
fb320 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 ESERVED_LOCK &&
fb330 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65 72 res ){. asser
fb340 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 t( pFile->lockty
fb350 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
fb360 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 );. res = Loc
fb370 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 kFile(pFile->h,
fb380 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 RESERVED_BYTE, 0
fb390 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 , 1, 0);. if(
fb3a0 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 res ){. ne
fb3b0 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45 53 45 wLocktype = RESE
fb3c0 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d RVED_LOCK;. }
fb3d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f else{. erro
fb3e0 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 r = GetLastError
fb3f0 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 ();. }. }..
fb400 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 50 45 /* Acquire a PE
fb410 4e 44 49 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a NDING lock. */.
fb420 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d if( locktype==
fb430 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 EXCLUSIVE_LOCK &
fb440 26 20 72 65 73 20 29 7b 0a 20 20 20 20 6e 65 77 & res ){. new
fb450 4c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 Locktype = PENDI
fb460 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 NG_LOCK;. got
fb470 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b PendingLock = 0;
fb480 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 . }.. /* Acqui
fb490 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 re an EXCLUSIVE
fb4a0 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 lock. */. if(
fb4b0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 locktype==EXCLUS
fb4c0 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 IVE_LOCK && res
fb4d0 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
fb4e0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d File->locktype>=
fb4f0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SHARED_LOCK );.
fb500 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 res = unlockR
fb510 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a eadLock(pFile);.
fb520 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 75 6e OSTRACE2("un
fb530 72 65 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 readlock = %d\n"
fb540 2c 20 72 65 73 29 3b 0a 20 20 20 20 72 65 73 20 , res);. res
fb550 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 = LockFile(pFile
fb560 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 ->h, SHARED_FIRS
fb570 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a T, 0, SHARED_SIZ
fb580 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 E, 0);. if( r
fb590 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c es ){. newL
fb5a0 6f 63 6b 74 79 70 65 20 3d 20 45 58 43 4c 55 53 ocktype = EXCLUS
fb5b0 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 IVE_LOCK;. }e
fb5c0 6c 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 lse{. error
fb5d0 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 = GetLastError(
fb5e0 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 );. OSTRACE
fb5f0 32 28 22 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20 2("error-code =
fb600 25 64 5c 6e 22 2c 20 65 72 72 6f 72 29 3b 0a 20 %d\n", error);.
fb610 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f 63 6b getReadLock
fb620 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 (pFile);. }.
fb630 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 }.. /* If we a
fb640 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e re holding a PEN
fb650 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f DING lock that o
fb660 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 61 ught to be relea
fb670 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 sed, then. ** r
fb680 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 elease it now..
fb690 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 6e */. if( gotPen
fb6a0 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b dingLock && lock
fb6b0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
fb6c0 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 K ){. UnlockF
fb6d0 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 ile(pFile->h, PE
fb6e0 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 NDING_BYTE, 0, 1
fb6f0 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 , 0);. }.. /*
fb700 55 70 64 61 74 65 20 74 68 65 20 73 74 61 74 65 Update the state
fb710 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 of the lock has
fb720 20 68 65 6c 64 20 69 6e 20 74 68 65 20 66 69 6c held in the fil
fb730 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 65 e descriptor the
fb740 6e 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 n. ** return th
fb750 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 e appropriate re
fb760 73 75 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a sult code.. */.
fb770 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 if( res ){.
fb780 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
fb790 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4f 53 . }else{. OS
fb7a0 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 46 41 49 TRACE4("LOCK FAI
fb7b0 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20 66 6f LED %d trying fo
fb7c0 72 20 25 64 20 62 75 74 20 67 6f 74 20 25 64 5c r %d but got %d\
fb7d0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 n", pFile->h,.
fb7e0 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 locktyp
fb7f0 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 70 65 29 3b e, newLocktype);
fb800 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 . pFile->last
fb810 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 Errno = error;.
fb820 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
fb830 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 USY;. }. pFile
fb840 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 28 75 38 ->locktype = (u8
fb850 29 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 )newLocktype;.
fb860 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
fb870 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
fb880 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 checks if there
fb890 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c is a RESERVED l
fb8a0 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 ock held on the
fb8b0 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c specified.** fil
fb8c0 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 e by this or any
fb8d0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 other process.
fb8e0 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 If such a lock i
fb8f0 73 20 68 65 6c 64 2c 20 72 65 74 75 72 6e 0a 2a s held, return.*
fb900 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65 * non-zero, othe
fb910 72 77 69 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 rwise zero..*/.s
fb920 74 61 74 69 63 20 69 6e 74 20 77 69 6e 43 68 65 tatic int winChe
fb930 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 ckReservedLock(s
fb940 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
fb950 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a int *pResOut){.
fb960 20 20 69 6e 74 20 72 63 3b 0a 20 20 77 69 6e 46 int rc;. winF
fb970 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 ile *pFile = (wi
fb980 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 nFile*)id;.. as
fb990 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 sert( id!=0 );.
fb9a0 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
fb9b0 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c type>=RESERVED_L
fb9c0 4f 43 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 OCK ){. rc =
fb9d0 31 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 1;. OSTRACE3(
fb9e0 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 "TEST WR-LOCK %d
fb9f0 20 25 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 %d (local)\n",
fba00 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 pFile->h, rc);.
fba10 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d }else{. rc =
fba20 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d LockFile(pFile-
fba30 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 >h, RESERVED_BYT
fba40 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 E, 0, 1, 0);.
fba50 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
fba60 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c UnlockFile(pFil
fba70 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 e->h, RESERVED_B
fba80 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 YTE, 0, 1, 0);.
fba90 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 21 72 }. rc = !r
fbaa0 63 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 c;. OSTRACE3(
fbab0 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 "TEST WR-LOCK %d
fbac0 20 25 64 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c %d (remote)\n",
fbad0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a pFile->h, rc);.
fbae0 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d }. *pResOut =
fbaf0 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 rc;. return SQ
fbb00 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
fbb10 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b * Lower the lock
fbb20 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c ing level on fil
fbb30 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 64 20 e descriptor id
fbb40 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f to locktype. lo
fbb50 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 cktype.** must b
fbb60 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b e either NO_LOCK
fbb70 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e or SHARED_LOCK.
fbb80 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f .**.** If the lo
fbb90 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 cking level of t
fbba0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
fbbb0 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 or is already at
fbbc0 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 or below.** the
fbbd0 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 requested locki
fbbe0 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 ng level, this r
fbbf0 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
fbc00 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e p..**.** It is n
fbc10 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 ot possible for
fbc20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 this routine to
fbc30 66 61 69 6c 20 69 66 20 74 68 65 20 73 65 63 6f fail if the seco
fbc40 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 nd argument.** i
fbc50 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 74 s NO_LOCK. If t
fbc60 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
fbc70 6e 74 20 69 73 20 53 48 41 52 45 44 5f 4c 4f 43 nt is SHARED_LOC
fbc80 4b 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 K then this rout
fbc90 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 ine.** might ret
fbca0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
fbcb0 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ;.*/.static int
fbcc0 77 69 6e 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 winUnlock(sqlite
fbcd0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
fbce0 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 locktype){. int
fbcf0 20 74 79 70 65 3b 0a 20 20 77 69 6e 46 69 6c 65 type;. winFile
fbd00 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 *pFile = (winFi
fbd10 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 le*)id;. int rc
fbd20 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
fbd30 61 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30 assert( pFile!=0
fbd40 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f );. assert( lo
fbd50 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c cktype<=SHARED_L
fbd60 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 OCK );. OSTRACE
fbd70 35 28 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 5("UNLOCK %d to
fbd80 25 64 20 77 61 73 20 25 64 28 25 64 29 5c 6e 22 %d was %d(%d)\n"
fbd90 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
fbda0 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 20 type,.
fbdb0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c pFile->locktype,
fbdc0 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f pFile->sharedLo
fbdd0 63 6b 42 79 74 65 29 3b 0a 20 20 74 79 70 65 20 ckByte);. type
fbde0 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 = pFile->locktyp
fbdf0 65 3b 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 45 e;. if( type>=E
fbe00 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b XCLUSIVE_LOCK ){
fbe10 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 . UnlockFile(
fbe20 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 pFile->h, SHARED
fbe30 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45 _FIRST, 0, SHARE
fbe40 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20 D_SIZE, 0);.
fbe50 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 if( locktype==SH
fbe60 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 21 67 65 ARED_LOCK && !ge
fbe70 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 tReadLock(pFile)
fbe80 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 ){. /* Thi
fbe90 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 s should never h
fbea0 61 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c appen. We shoul
fbeb0 64 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65 d always be able
fbec0 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 to. ** rea
fbed0 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20 cquire the read
fbee0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 72 63 lock */. rc
fbef0 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
fbf00 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 UNLOCK;. }.
fbf10 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 }. if( type>=RE
fbf20 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SERVED_LOCK ){.
fbf30 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 UnlockFile(pF
fbf40 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 ile->h, RESERVED
fbf50 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b _BYTE, 0, 1, 0);
fbf60 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 . }. if( lockt
fbf70 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 ype==NO_LOCK &&
fbf80 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 type>=SHARED_LOC
fbf90 4b 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 52 K ){. unlockR
fbfa0 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a eadLock(pFile);.
fbfb0 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d }. if( type>=
fbfc0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a PENDING_LOCK ){.
fbfd0 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 UnlockFile(p
fbfe0 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 File->h, PENDING
fbff0 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b _BYTE, 0, 1, 0);
fc000 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f . }. pFile->lo
fc010 63 6b 74 79 70 65 20 3d 20 28 75 38 29 6c 6f 63 cktype = (u8)loc
fc020 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 ktype;. return
fc030 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e rc;.}../*.** Con
fc040 74 72 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f trol and query o
fc050 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 f the open file
fc060 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 handle..*/.stati
fc070 63 20 69 6e 74 20 77 69 6e 46 69 6c 65 43 6f 6e c int winFileCon
fc080 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c trol(sqlite3_fil
fc090 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 e *id, int op, v
fc0a0 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 oid *pArg){. sw
fc0b0 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 itch( op ){.
fc0c0 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 case SQLITE_FCNT
fc0d0 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 L_LOCKSTATE: {.
fc0e0 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 *(int*)pArg
fc0f0 20 3d 20 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 = ((winFile*)id
fc100 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 )->locktype;.
fc110 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fc120 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 _OK;. }. c
fc130 61 73 65 20 53 51 4c 49 54 45 5f 4c 41 53 54 5f ase SQLITE_LAST_
fc140 45 52 52 4e 4f 3a 20 7b 0a 20 20 20 20 20 20 2a ERRNO: {. *
fc150 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 69 6e (int*)pArg = (in
fc160 74 29 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 t)((winFile*)id)
fc170 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 ->lastErrno;.
fc180 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fc190 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 _OK;. }. }.
fc1a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
fc1b0 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 RROR;.}../*.** R
fc1c0 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72 eturn the sector
fc1d0 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f size in bytes o
fc1e0 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 f the underlying
fc1f0 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f block device fo
fc200 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 r.** the specifi
fc210 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 ed file. This is
fc220 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 almost always 5
fc230 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 12 bytes, but ma
fc240 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 y be.** larger f
fc250 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e or some devices.
fc260 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f .**.** SQLite co
fc270 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 73 20 de assumes this
fc280 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 function cannot
fc290 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 fail. It also as
fc2a0 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 sumes that.** if
fc2b0 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63 two files are c
fc2c0 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 73 61 reated in the sa
fc2d0 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 me file-system d
fc2e0 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a irectory (i.e..*
fc2f0 2a 20 61 20 64 61 74 61 62 61 73 65 20 61 6e 64 * a database and
fc300 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c its journal fil
fc310 65 29 20 74 68 61 74 20 74 68 65 20 73 65 63 74 e) that the sect
fc320 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 or size will be
fc330 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 the.** same for
fc340 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 both..*/.static
fc350 69 6e 74 20 77 69 6e 53 65 63 74 6f 72 53 69 7a int winSectorSiz
fc360 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
fc370 69 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 id){. assert( i
fc380 64 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e d!=0 );. return
fc390 20 28 69 6e 74 29 28 28 28 77 69 6e 46 69 6c 65 (int)(((winFile
fc3a0 2a 29 69 64 29 2d 3e 73 65 63 74 6f 72 53 69 7a *)id)->sectorSiz
fc3b0 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 e);.}../*.** Ret
fc3c0 75 72 6e 20 61 20 76 65 63 74 6f 72 20 6f 66 20 urn a vector of
fc3d0 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 device character
fc3e0 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69 istics..*/.stati
fc3f0 63 20 69 6e 74 20 77 69 6e 44 65 76 69 63 65 43 c int winDeviceC
fc400 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73 haracteristics(s
fc410 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
fc420 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
fc430 45 54 45 52 28 69 64 29 3b 0a 20 20 72 65 74 75 ETER(id);. retu
fc440 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 rn 0;.}../*.** T
fc450 68 69 73 20 76 65 63 74 6f 72 20 64 65 66 69 6e his vector defin
fc460 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f es all the metho
fc470 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70 65 72 ds that can oper
fc480 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c ate on an.** sql
fc490 69 74 65 33 5f 66 69 6c 65 20 66 6f 72 20 77 69 ite3_file for wi
fc4a0 6e 33 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 n32..*/.static c
fc4b0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
fc4c0 6d 65 74 68 6f 64 73 20 77 69 6e 49 6f 4d 65 74 methods winIoMet
fc4d0 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 hod = {. 1,
fc4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fc4f0 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 /* iVersion
fc500 2a 2f 0a 20 20 77 69 6e 43 6c 6f 73 65 2c 0a 20 */. winClose,.
fc510 20 77 69 6e 52 65 61 64 2c 0a 20 20 77 69 6e 57 winRead,. winW
fc520 72 69 74 65 2c 0a 20 20 77 69 6e 54 72 75 6e 63 rite,. winTrunc
fc530 61 74 65 2c 0a 20 20 77 69 6e 53 79 6e 63 2c 0a ate,. winSync,.
fc540 20 20 77 69 6e 46 69 6c 65 53 69 7a 65 2c 0a 20 winFileSize,.
fc550 20 77 69 6e 4c 6f 63 6b 2c 0a 20 20 77 69 6e 55 winLock,. winU
fc560 6e 6c 6f 63 6b 2c 0a 20 20 77 69 6e 43 68 65 63 nlock,. winChec
fc570 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 kReservedLock,.
fc580 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c winFileControl,
fc590 0a 20 20 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 . winSectorSize
fc5a0 2c 0a 20 20 77 69 6e 44 65 76 69 63 65 43 68 61 ,. winDeviceCha
fc5b0 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d 3b 0a racteristics.};.
fc5c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
fc5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fc5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fc5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fc600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
fc610 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20 49 Here ends the I
fc620 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 /O methods that
fc630 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74 65 33 form the sqlite3
fc640 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
fc650 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 ct..**.** The ne
fc660 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 xt block of code
fc670 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 implements the
fc680 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a VFS methods..***
fc690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fc6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fc6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fc6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fc6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a *********/../*.*
fc6e0 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d * Convert a UTF-
fc6f0 38 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 8 filename into
fc700 77 68 61 74 65 76 65 72 20 66 6f 72 6d 20 74 68 whatever form th
fc710 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 e underlying.**
fc720 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
fc730 20 77 61 6e 74 73 20 66 69 6c 65 6e 61 6d 65 73 wants filenames
fc740 20 69 6e 2e 20 20 53 70 61 63 65 20 74 6f 20 68 in. Space to h
fc750 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 0a 2a old the result.*
fc760 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 * is obtained fr
fc770 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 om malloc and mu
fc780 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74 st be freed by t
fc790 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 he calling.** fu
fc7a0 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 nction..*/.stati
fc7b0 63 20 76 6f 69 64 20 2a 63 6f 6e 76 65 72 74 55 c void *convertU
fc7c0 74 66 38 46 69 6c 65 6e 61 6d 65 28 63 6f 6e 73 tf8Filename(cons
fc7d0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
fc7e0 65 29 7b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e e){. void *zCon
fc7f0 76 65 72 74 65 64 20 3d 20 30 3b 0a 20 20 69 66 verted = 0;. if
fc800 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 ( isNT() ){.
fc810 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 66 zConverted = utf
fc820 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 8ToUnicode(zFile
fc830 6e 61 6d 65 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 name);./* isNT()
fc840 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f is 1 if SQLITE_
fc850 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 OS_WINCE==1, so
fc860 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 this else is nev
fc870 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f er executed. .*/
fc880 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
fc890 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b INCE==0. }else{
fc8a0 0a 20 20 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 . zConverted
fc8b0 3d 20 75 74 66 38 54 6f 4d 62 63 73 28 7a 46 69 = utf8ToMbcs(zFi
fc8c0 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a lename);.#endif.
fc8d0 20 20 7d 0a 20 20 2f 2a 20 63 61 6c 6c 65 72 20 }. /* caller
fc8e0 77 69 6c 6c 20 68 61 6e 64 6c 65 20 6f 75 74 20 will handle out
fc8f0 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 72 of memory */. r
fc900 65 74 75 72 6e 20 7a 43 6f 6e 76 65 72 74 65 64 eturn zConverted
fc910 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 ;.}../*.** Creat
fc920 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 e a temporary fi
fc930 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e le name in zBuf.
fc940 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65 20 62 zBuf must be b
fc950 69 67 20 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 ig enough to.**
fc960 68 6f 6c 64 20 61 74 20 70 56 66 73 2d 3e 6d 78 hold at pVfs->mx
fc970 50 61 74 68 6e 61 6d 65 20 63 68 61 72 61 63 74 Pathname charact
fc980 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ers..*/.static i
fc990 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 nt getTempname(i
fc9a0 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a nt nBuf, char *z
fc9b0 42 75 66 29 7b 0a 20 20 73 74 61 74 69 63 20 63 Buf){. static c
fc9c0 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 har zChars[] =.
fc9d0 20 20 20 22 61 62 63 64 65 66 67 68 69 6a 6b 6c "abcdefghijkl
fc9e0 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 22 0a mnopqrstuvwxyz".
fc9f0 20 20 20 20 22 41 42 43 44 45 46 47 48 49 4a 4b "ABCDEFGHIJK
fca00 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 22 LMNOPQRSTUVWXYZ"
fca10 0a 20 20 20 20 22 30 31 32 33 34 35 36 37 38 39 . "0123456789
fca20 22 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c 20 6a ";. size_t i, j
fca30 3b 0a 20 20 63 68 61 72 20 7a 54 65 6d 70 50 61 ;. char zTempPa
fca40 74 68 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b 0a th[MAX_PATH+1];.
fca50 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74 65 if( sqlite3_te
fca60 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a mp_directory ){.
fca70 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
fca80 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 intf(MAX_PATH-30
fca90 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 73 , zTempPath, "%s
fcaa0 22 2c 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f ", sqlite3_temp_
fcab0 64 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 7d 65 directory);. }e
fcac0 6c 73 65 20 69 66 28 20 69 73 4e 54 28 29 20 29 lse if( isNT() )
fcad0 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 75 6c {. char *zMul
fcae0 74 69 3b 0a 20 20 20 20 57 43 48 41 52 20 7a 57 ti;. WCHAR zW
fcaf0 69 64 65 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 idePath[MAX_PATH
fcb00 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70 50 61 ];. GetTempPa
fcb10 74 68 57 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c thW(MAX_PATH-30,
fcb20 20 7a 57 69 64 65 50 61 74 68 29 3b 0a 20 20 20 zWidePath);.
fcb30 20 7a 4d 75 6c 74 69 20 3d 20 75 6e 69 63 6f 64 zMulti = unicod
fcb40 65 54 6f 55 74 66 38 28 7a 57 69 64 65 50 61 74 eToUtf8(zWidePat
fcb50 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 4d 75 6c h);. if( zMul
fcb60 74 69 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ti ){. sqli
fcb70 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 te3_snprintf(MAX
fcb80 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50 _PATH-30, zTempP
fcb90 61 74 68 2c 20 22 25 73 22 2c 20 7a 4d 75 6c 74 ath, "%s", zMult
fcba0 69 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28 7a i);. free(z
fcbb0 4d 75 6c 74 69 29 3b 0a 20 20 20 20 7d 65 6c 73 Multi);. }els
fcbc0 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 e{. return
fcbd0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
fcbe0 20 20 7d 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 }./* isNT() is
fcbf0 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 1 if SQLITE_OS_
fcc00 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 WINCE==1, so thi
fcc10 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 s else is never
fcc20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 executed. .** Si
fcc30 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 nce the ASCII ve
fcc40 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 rsion of these W
fcc50 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f indows API do no
fcc60 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 t exist for WINC
fcc70 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 E,.** it's impor
fcc80 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 tant to not refe
fcc90 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 rence them for W
fcca0 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a INCE builds..*/.
fccb0 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
fccc0 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a NCE==0. }else{.
fccd0 20 20 20 20 63 68 61 72 20 2a 7a 55 74 66 38 3b char *zUtf8;
fcce0 0a 20 20 20 20 63 68 61 72 20 7a 4d 62 63 73 50 . char zMbcsP
fccf0 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 ath[MAX_PATH];.
fcd00 20 20 20 47 65 74 54 65 6d 70 50 61 74 68 41 28 GetTempPathA(
fcd10 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 4d 62 MAX_PATH-30, zMb
fcd20 63 73 50 61 74 68 29 3b 0a 20 20 20 20 7a 55 74 csPath);. zUt
fcd30 66 38 20 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e f8 = sqlite3_win
fcd40 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 32_mbcs_to_utf8(
fcd50 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 20 20 zMbcsPath);.
fcd60 69 66 28 20 7a 55 74 66 38 20 29 7b 0a 20 20 20 if( zUtf8 ){.
fcd70 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
fcd80 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c ntf(MAX_PATH-30,
fcd90 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 73 22 zTempPath, "%s"
fcda0 2c 20 7a 55 74 66 38 29 3b 0a 20 20 20 20 20 20 , zUtf8);.
fcdb0 66 72 65 65 28 7a 55 74 66 38 29 3b 0a 20 20 20 free(zUtf8);.
fcdc0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 }else{. re
fcdd0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
fcde0 4d 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a M;. }.#endif.
fcdf0 20 20 7d 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 }. for(i=sqli
fce00 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d te3Strlen30(zTem
fce10 70 50 61 74 68 29 3b 20 69 3e 30 20 26 26 20 7a pPath); i>0 && z
fce20 54 65 6d 70 50 61 74 68 5b 69 2d 31 5d 3d 3d 27 TempPath[i-1]=='
fce30 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a 20 20 7a 54 \\'; i--){}. zT
fce40 65 6d 70 50 61 74 68 5b 69 5d 20 3d 20 30 3b 0a empPath[i] = 0;.
fce50 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
fce60 74 66 28 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 tf(nBuf-30, zBuf
fce70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
fce80 20 20 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 "%s\\"SQLIT
fce90 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 E_TEMP_FILE_PREF
fcea0 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 29 3b 0a IX, zTempPath);.
fceb0 20 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 j = sqlite3Str
fcec0 6c 65 6e 33 30 28 7a 42 75 66 29 3b 0a 20 20 73 len30(zBuf);. s
fced0 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
fcee0 73 28 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b s(20, &zBuf[j]);
fcef0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 30 . for(i=0; i<20
fcf00 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 ; i++, j++){.
fcf10 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 zBuf[j] = (char
fcf20 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67 )zChars[ ((unsig
fcf30 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d ned char)zBuf[j]
fcf40 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 )%(sizeof(zChars
fcf50 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42 )-1) ];. }. zB
fcf60 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 54 uf[j] = 0;. OST
fcf70 52 41 43 45 32 28 22 54 45 4d 50 20 46 49 4c 45 RACE2("TEMP FILE
fcf80 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 NAME: %s\n", zBu
fcf90 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c f);. return SQL
fcfa0 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a ITE_OK; .}../*.*
fcfb0 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c * The return val
fcfc0 75 65 20 6f 66 20 67 65 74 4c 61 73 74 45 72 72 ue of getLastErr
fcfd0 6f 72 4d 73 67 0a 2a 2a 20 69 73 20 7a 65 72 6f orMsg.** is zero
fcfe0 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 if the error me
fcff0 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 74 68 ssage fits in th
fd000 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e e buffer, or non
fd010 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 -zero.** otherwi
fd020 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 73 61 se (if the messa
fd030 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 65 64 ge was truncated
fd040 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
fd050 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 getLastErrorMsg
fd060 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 (int nBuf, char
fd070 2a 7a 42 75 66 29 7b 0a 20 20 44 57 4f 52 44 20 *zBuf){. DWORD
fd080 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 error = GetLastE
fd090 72 72 6f 72 28 29 3b 0a 0a 23 69 66 20 53 51 4c rror();..#if SQL
fd0a0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 73 ITE_OS_WINCE. s
fd0b0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
fd0c0 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45 nBuf, zBuf, "OsE
fd0d0 72 72 6f 72 20 30 78 25 78 20 28 25 75 29 22 2c rror 0x%x (%u)",
fd0e0 20 65 72 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a error, error);.
fd0f0 23 65 6c 73 65 0a 20 20 2f 2a 20 46 6f 72 6d 61 #else. /* Forma
fd100 74 4d 65 73 73 61 67 65 20 72 65 74 75 72 6e 73 tMessage returns
fd110 20 30 20 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20 0 on failure.
fd120 4f 74 68 65 72 77 69 73 65 20 69 74 0a 20 20 2a Otherwise it. *
fd130 2a 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 * returns the nu
fd140 6d 62 65 72 20 6f 66 20 54 43 48 41 52 73 20 77 mber of TCHARs w
fd150 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6f 75 ritten to the ou
fd160 74 70 75 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 tput. ** buffer
fd170 2c 20 65 78 63 6c 75 64 69 6e 67 20 74 68 65 20 , excluding the
fd180 74 65 72 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c terminating null
fd190 20 63 68 61 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 char.. */. if
fd1a0 20 28 21 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 (!FormatMessage
fd1b0 41 28 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 A(FORMAT_MESSAGE
fd1c0 5f 46 52 4f 4d 5f 53 59 53 54 45 4d 2c 0a 20 20 _FROM_SYSTEM,.
fd1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd1e0 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 NULL,.
fd1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd200 65 72 72 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 error,.
fd210 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0a 0,.
fd220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd230 20 20 20 20 20 20 7a 42 75 66 2c 0a 20 20 20 20 zBuf,.
fd240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd250 20 20 6e 42 75 66 2d 31 2c 0a 20 20 20 20 20 20 nBuf-1,.
fd260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd270 30 29 29 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69 0)). {. sqli
fd280 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 te3_snprintf(nBu
fd290 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f f, zBuf, "OsErro
fd2a0 72 20 30 78 25 78 20 28 25 75 29 22 2c 20 65 72 r 0x%x (%u)", er
fd2b0 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a 20 20 7d ror, error);. }
fd2c0 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 .#endif.. retur
fd2d0 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 n 0;.}../*.** Op
fd2e0 65 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 en a file..*/.st
fd2f0 61 74 69 63 20 69 6e 74 20 77 69 6e 4f 70 65 6e atic int winOpen
fd300 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
fd310 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a *pVfs, /*
fd320 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 63 Not used */. c
fd330 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
fd340 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 , /* Name
fd350 20 6f 66 20 74 68 65 20 66 69 6c 65 20 28 55 54 of the file (UT
fd360 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 F-8) */. sqlite
fd370 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 3_file *id,
fd380 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 /* Write the
fd390 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61 6e SQLite file han
fd3a0 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e dle here */. in
fd3b0 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 t flags,
fd3c0 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 /* Open
fd3d0 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20 mode flags */.
fd3e0 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 int *pOutFlags
fd3f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 /* Sta
fd400 74 75 73 20 72 65 74 75 72 6e 20 66 6c 61 67 73 tus return flags
fd410 20 2a 2f 0a 29 7b 0a 20 20 48 41 4e 44 4c 45 20 */.){. HANDLE
fd420 68 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 65 73 h;. DWORD dwDes
fd430 69 72 65 64 41 63 63 65 73 73 3b 0a 20 20 44 57 iredAccess;. DW
fd440 4f 52 44 20 64 77 53 68 61 72 65 4d 6f 64 65 3b ORD dwShareMode;
fd450 0a 20 20 44 57 4f 52 44 20 64 77 43 72 65 61 74 . DWORD dwCreat
fd460 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 3b 0a ionDisposition;.
fd470 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73 41 DWORD dwFlagsA
fd480 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 30 ndAttributes = 0
fd490 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ;.#if SQLITE_OS_
fd4a0 57 49 4e 43 45 0a 20 20 69 6e 74 20 69 73 54 65 WINCE. int isTe
fd4b0 6d 70 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 mp = 0;.#endif.
fd4c0 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 winFile *pFile
fd4d0 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a = (winFile*)id;.
fd4e0 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 void *zConvert
fd4f0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ed;
fd500 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 /* Filename
fd510 69 6e 20 4f 53 20 65 6e 63 6f 64 69 6e 67 20 2a in OS encoding *
fd520 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
fd530 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 4e 61 6d zUtf8Name = zNam
fd540 65 3b 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d e; /* Filenam
fd550 65 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 e in UTF-8 encod
fd560 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 ing */. char zT
fd570 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 2b mpname[MAX_PATH+
fd580 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 75 1]; /* Bu
fd590 66 66 65 72 20 75 73 65 64 20 74 6f 20 63 72 65 ffer used to cre
fd5a0 61 74 65 20 74 65 6d 70 20 66 69 6c 65 6e 61 6d ate temp filenam
fd5b0 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 e */.. assert(
fd5c0 69 64 21 3d 30 20 29 3b 0a 20 20 55 4e 55 53 45 id!=0 );. UNUSE
fd5d0 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 D_PARAMETER(pVfs
fd5e0 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
fd5f0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
fd600 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
fd610 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 is NULL, genera
fd620 74 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f te a . ** tempo
fd630 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 rary file name t
fd640 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 o use . */. if
fd650 28 20 21 7a 55 74 66 38 4e 61 6d 65 20 29 7b 0a ( !zUtf8Name ){.
fd660 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67 65 74 int rc = get
fd670 54 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 Tempname(MAX_PAT
fd680 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a H+1, zTmpname);.
fd690 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
fd6a0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
fd6b0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
fd6c0 20 20 20 20 7a 55 74 66 38 4e 61 6d 65 20 3d 20 zUtf8Name =
fd6d0 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 zTmpname;. }..
fd6e0 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 /* Convert the
fd6f0 66 69 6c 65 6e 61 6d 65 20 74 6f 20 74 68 65 20 filename to the
fd700 73 79 73 74 65 6d 20 65 6e 63 6f 64 69 6e 67 2e system encoding.
fd710 20 2a 2f 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 */. zConverted
fd720 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 = convertUtf8Fi
fd730 6c 65 6e 61 6d 65 28 7a 55 74 66 38 4e 61 6d 65 lename(zUtf8Name
fd740 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 );. if( zConver
fd750 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ted==0 ){. re
fd760 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
fd770 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c M;. }.. if( fl
fd780 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
fd790 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 N_READWRITE ){.
fd7a0 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 dwDesiredAcce
fd7b0 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 ss = GENERIC_REA
fd7c0 44 20 7c 20 47 45 4e 45 52 49 43 5f 57 52 49 54 D | GENERIC_WRIT
fd7d0 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 E;. }else{.
fd7e0 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 20 dwDesiredAccess
fd7f0 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44 3b 0a = GENERIC_READ;.
fd800 20 20 7d 0a 20 20 2f 2a 20 53 51 4c 49 54 45 5f }. /* SQLITE_
fd810 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 69 OPEN_EXCLUSIVE i
fd820 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 s used to make s
fd830 75 72 65 20 74 68 61 74 20 61 20 6e 65 77 20 66 ure that a new f
fd840 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20 63 72 65 ile is . ** cre
fd850 61 74 65 64 2e 20 53 51 4c 69 74 65 20 64 6f 65 ated. SQLite doe
fd860 73 6e 27 74 20 75 73 65 20 69 74 20 74 6f 20 69 sn't use it to i
fd870 6e 64 69 63 61 74 65 20 22 65 78 63 6c 75 73 69 ndicate "exclusi
fd880 76 65 20 61 63 63 65 73 73 22 20 0a 20 20 2a 2a ve access" . **
fd890 20 61 73 20 69 74 20 69 73 20 75 73 75 61 6c 6c as it is usuall
fd8a0 79 20 75 6e 64 65 72 73 74 6f 6f 64 2e 0a 20 20 y understood..
fd8b0 2a 2f 0a 20 20 61 73 73 65 72 74 28 21 28 66 6c */. assert(!(fl
fd8c0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
fd8d0 4e 5f 45 58 43 4c 55 53 49 56 45 29 20 7c 7c 20 N_EXCLUSIVE) ||
fd8e0 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f (flags & SQLITE_
fd8f0 4f 50 45 4e 5f 43 52 45 41 54 45 29 29 3b 0a 20 OPEN_CREATE));.
fd900 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c if( flags & SQL
fd910 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 ITE_OPEN_EXCLUSI
fd920 56 45 20 29 7b 0a 20 20 20 20 2f 2a 20 43 72 65 VE ){. /* Cre
fd930 61 74 65 73 20 61 20 6e 65 77 20 66 69 6c 65 2c ates a new file,
fd940 20 6f 6e 6c 79 20 69 66 20 69 74 20 64 6f 65 73 only if it does
fd950 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 not already exi
fd960 73 74 2e 20 2a 2f 0a 20 20 20 20 2f 2a 20 49 66 st. */. /* If
fd970 20 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 the file exists
fd980 2c 20 69 74 20 66 61 69 6c 73 2e 20 2a 2f 0a 20 , it fails. */.
fd990 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 dwCreationDis
fd9a0 70 6f 73 69 74 69 6f 6e 20 3d 20 43 52 45 41 54 position = CREAT
fd9b0 45 5f 4e 45 57 3b 0a 20 20 7d 65 6c 73 65 20 69 E_NEW;. }else i
fd9c0 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f( flags & SQLIT
fd9d0 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 29 7b E_OPEN_CREATE ){
fd9e0 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 65 78 69 . /* Open exi
fd9f0 73 74 69 6e 67 20 66 69 6c 65 2c 20 6f 72 20 63 sting file, or c
fda00 72 65 61 74 65 20 69 66 20 69 74 20 64 6f 65 73 reate if it does
fda10 6e 27 74 20 65 78 69 73 74 20 2a 2f 0a 20 20 20 n't exist */.
fda20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f dwCreationDispo
fda30 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 41 4c sition = OPEN_AL
fda40 57 41 59 53 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 WAYS;. }else{.
fda50 20 20 20 2f 2a 20 4f 70 65 6e 73 20 61 20 66 69 /* Opens a fi
fda60 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74 20 65 le, only if it e
fda70 78 69 73 74 73 2e 20 2a 2f 0a 20 20 20 20 64 77 xists. */. dw
fda80 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 CreationDisposit
fda90 69 6f 6e 20 3d 20 4f 50 45 4e 5f 45 58 49 53 54 ion = OPEN_EXIST
fdaa0 49 4e 47 3b 0a 20 20 7d 0a 20 20 64 77 53 68 61 ING;. }. dwSha
fdab0 72 65 4d 6f 64 65 20 3d 20 46 49 4c 45 5f 53 48 reMode = FILE_SH
fdac0 41 52 45 5f 52 45 41 44 20 7c 20 46 49 4c 45 5f ARE_READ | FILE_
fdad0 53 48 41 52 45 5f 57 52 49 54 45 3b 0a 20 20 69 SHARE_WRITE;. i
fdae0 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f( flags & SQLIT
fdaf0 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 E_OPEN_DELETEONC
fdb00 4c 4f 53 45 20 29 7b 0a 23 69 66 20 53 51 4c 49 LOSE ){.#if SQLI
fdb10 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 TE_OS_WINCE.
fdb20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 dwFlagsAndAttrib
fdb30 75 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 utes = FILE_ATTR
fdb40 49 42 55 54 45 5f 48 49 44 44 45 4e 3b 0a 20 20 IBUTE_HIDDEN;.
fdb50 20 20 69 73 54 65 6d 70 20 3d 20 31 3b 0a 23 65 isTemp = 1;.#e
fdb60 6c 73 65 0a 20 20 20 20 64 77 46 6c 61 67 73 41 lse. dwFlagsA
fdb70 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 46 ndAttributes = F
fdb80 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 54 45 ILE_ATTRIBUTE_TE
fdb90 4d 50 4f 52 41 52 59 0a 20 20 20 20 20 20 20 20 MPORARY.
fdba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fdbb0 20 20 20 20 20 20 20 7c 20 46 49 4c 45 5f 41 54 | FILE_AT
fdbc0 54 52 49 42 55 54 45 5f 48 49 44 44 45 4e 0a 20 TRIBUTE_HIDDEN.
fdbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fdbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 |
fdbf0 46 49 4c 45 5f 46 4c 41 47 5f 44 45 4c 45 54 45 FILE_FLAG_DELETE
fdc00 5f 4f 4e 5f 43 4c 4f 53 45 3b 0a 23 65 6e 64 69 _ON_CLOSE;.#endi
fdc10 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 f. }else{. d
fdc20 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 wFlagsAndAttribu
fdc30 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 tes = FILE_ATTRI
fdc40 42 55 54 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d BUTE_NORMAL;. }
fdc50 0a 20 20 2f 2a 20 52 65 70 6f 72 74 73 20 66 72 . /* Reports fr
fdc60 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 65 74 20 om the internet
fdc70 61 72 65 20 74 68 61 74 20 70 65 72 66 6f 72 6d are that perform
fdc80 61 6e 63 65 20 69 73 20 61 6c 77 61 79 73 0a 20 ance is always.
fdc90 20 2a 2a 20 62 65 74 74 65 72 20 69 66 20 46 49 ** better if FI
fdca0 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 LE_FLAG_RANDOM_A
fdcb0 43 43 45 53 53 20 69 73 20 75 73 65 64 2e 20 20 CCESS is used.
fdcc0 54 69 63 6b 65 74 20 23 32 36 39 39 2e 20 2a 2f Ticket #2699. */
fdcd0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
fdce0 49 4e 43 45 0a 20 20 64 77 46 6c 61 67 73 41 6e INCE. dwFlagsAn
fdcf0 64 41 74 74 72 69 62 75 74 65 73 20 7c 3d 20 46 dAttributes |= F
fdd00 49 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f ILE_FLAG_RANDOM_
fdd10 41 43 43 45 53 53 3b 0a 23 65 6e 64 69 66 0a 20 ACCESS;.#endif.
fdd20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 if( isNT() ){.
fdd30 20 20 20 68 20 3d 20 43 72 65 61 74 65 46 69 6c h = CreateFil
fdd40 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 eW((WCHAR*)zConv
fdd50 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77 erted,. dw
fdd60 44 65 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20 DesiredAccess,.
fdd70 20 20 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 dwShareMod
fdd80 65 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a e,. NULL,.
fdd90 20 20 20 20 20 20 20 64 77 43 72 65 61 74 69 6f dwCreatio
fdda0 6e 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 nDisposition,.
fddb0 20 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 dwFlagsAndA
fddc0 74 74 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20 ttributes,.
fddd0 20 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 2f 2a NULL. );./*
fdde0 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 isNT() is 1 if
fddf0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
fde00 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 =1, so this else
fde10 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 is never execut
fde20 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 ed. .** Since th
fde30 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 e ASCII version
fde40 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 of these Windows
fde50 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 API do not exis
fde60 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 t for WINCE,.**
fde70 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 it's important t
fde80 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 o not reference
fde90 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 them for WINCE b
fdea0 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 uilds..*/.#if SQ
fdeb0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 LITE_OS_WINCE==0
fdec0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 . }else{. h
fded0 3d 20 43 72 65 61 74 65 46 69 6c 65 41 28 28 63 = CreateFileA((c
fdee0 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c har*)zConverted,
fdef0 0a 20 20 20 20 20 20 20 64 77 44 65 73 69 72 65 . dwDesire
fdf00 64 41 63 63 65 73 73 2c 0a 20 20 20 20 20 20 20 dAccess,.
fdf10 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20 20 dwShareMode,.
fdf20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 NULL,.
fdf30 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f dwCreationDispo
fdf40 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20 64 sition,. d
fdf50 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 wFlagsAndAttribu
fdf60 74 65 73 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c tes,. NULL
fdf70 0a 20 20 20 20 29 3b 0a 23 65 6e 64 69 66 0a 20 . );.#endif.
fdf80 20 7d 0a 20 20 69 66 28 20 68 3d 3d 49 4e 56 41 }. if( h==INVA
fdf90 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 LID_HANDLE_VALUE
fdfa0 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f ){. free(zCo
fdfb0 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 69 66 nverted);. if
fdfc0 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ( flags & SQLITE
fdfd0 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 _OPEN_READWRITE
fdfe0 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
fdff0 77 69 6e 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e winOpen(pVfs, zN
fe000 61 6d 65 2c 20 69 64 2c 20 0a 20 20 20 20 20 20 ame, id, .
fe010 20 20 20 20 20 20 20 28 28 66 6c 61 67 73 7c 53 ((flags|S
fe020 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
fe030 4e 4c 59 29 26 7e 53 51 4c 49 54 45 5f 4f 50 45 NLY)&~SQLITE_OPE
fe040 4e 5f 52 45 41 44 57 52 49 54 45 29 2c 20 70 4f N_READWRITE), pO
fe050 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 7d 65 utFlags);. }e
fe060 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 lse{. retur
fe070 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 n SQLITE_CANTOPE
fe080 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 N;. }. }. i
fe090 66 28 20 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a f( pOutFlags ){.
fe0a0 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 if( flags &
fe0b0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
fe0c0 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 2a WRITE ){. *
fe0d0 70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51 4c 49 pOutFlags = SQLI
fe0e0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
fe0f0 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 E;. }else{.
fe100 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d *pOutFlags =
fe110 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
fe120 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 7d DONLY;. }. }
fe130 0a 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c . memset(pFile,
fe140 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 0, sizeof(*pFil
fe150 65 29 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 4d e));. pFile->pM
fe160 65 74 68 6f 64 20 3d 20 26 77 69 6e 49 6f 4d 65 ethod = &winIoMe
fe170 74 68 6f 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 thod;. pFile->h
fe180 20 3d 20 68 3b 0a 20 20 70 46 69 6c 65 2d 3e 6c = h;. pFile->l
fe190 61 73 74 45 72 72 6e 6f 20 3d 20 4e 4f 5f 45 52 astErrno = NO_ER
fe1a0 52 4f 52 3b 0a 20 20 70 46 69 6c 65 2d 3e 73 65 ROR;. pFile->se
fe1b0 63 74 6f 72 53 69 7a 65 20 3d 20 67 65 74 53 65 ctorSize = getSe
fe1c0 63 74 6f 72 53 69 7a 65 28 70 56 66 73 2c 20 7a ctorSize(pVfs, z
fe1d0 55 74 66 38 4e 61 6d 65 29 3b 0a 23 69 66 20 53 Utf8Name);.#if S
fe1e0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 QLITE_OS_WINCE.
fe1f0 20 69 66 28 20 28 66 6c 61 67 73 20 26 20 28 53 if( (flags & (S
fe200 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
fe210 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e RITE|SQLITE_OPEN
fe220 5f 4d 41 49 4e 5f 44 42 29 29 20 3d 3d 0a 20 20 _MAIN_DB)) ==.
fe230 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 (SQ
fe240 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
fe250 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ITE|SQLITE_OPEN_
fe260 4d 41 49 4e 5f 44 42 29 0a 20 20 20 20 20 20 20 MAIN_DB).
fe270 26 26 20 21 77 69 6e 63 65 43 72 65 61 74 65 4c && !winceCreateL
fe280 6f 63 6b 28 7a 4e 61 6d 65 2c 20 70 46 69 6c 65 ock(zName, pFile
fe290 29 0a 20 20 29 7b 0a 20 20 20 20 43 6c 6f 73 65 ). ){. Close
fe2a0 48 61 6e 64 6c 65 28 68 29 3b 0a 20 20 20 20 66 Handle(h);. f
fe2b0 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b ree(zConverted);
fe2c0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
fe2d0 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d TE_CANTOPEN;. }
fe2e0 0a 20 20 69 66 28 20 69 73 54 65 6d 70 20 29 7b . if( isTemp ){
fe2f0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 7a 44 65 6c . pFile->zDel
fe300 65 74 65 4f 6e 43 6c 6f 73 65 20 3d 20 7a 43 6f eteOnClose = zCo
fe310 6e 76 65 72 74 65 64 3b 0a 20 20 7d 65 6c 73 65 nverted;. }else
fe320 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 .#endif. {.
fe330 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 free(zConverted)
fe340 3b 0a 20 20 7d 0a 20 20 4f 70 65 6e 43 6f 75 6e ;. }. OpenCoun
fe350 74 65 72 28 2b 31 29 3b 0a 20 20 72 65 74 75 72 ter(+1);. retur
fe360 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
fe370 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 /*.** Delete the
fe380 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a named file..**.
fe390 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 69 6e ** Note that win
fe3a0 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 61 6c dows does not al
fe3b0 6c 6f 77 20 61 20 66 69 6c 65 20 74 6f 20 62 65 low a file to be
fe3c0 20 64 65 6c 65 74 65 64 20 69 66 20 73 6f 6d 65 deleted if some
fe3d0 20 6f 74 68 65 72 0a 2a 2a 20 70 72 6f 63 65 73 other.** proces
fe3e0 73 20 68 61 73 20 69 74 20 6f 70 65 6e 2e 20 20 s has it open.
fe3f0 53 6f 6d 65 74 69 6d 65 73 20 61 20 76 69 72 75 Sometimes a viru
fe400 73 20 73 63 61 6e 6e 65 72 20 6f 72 20 69 6e 64 s scanner or ind
fe410 65 78 69 6e 67 20 70 72 6f 67 72 61 6d 0a 2a 2a exing program.**
fe420 20 77 69 6c 6c 20 6f 70 65 6e 20 61 20 6a 6f 75 will open a jou
fe430 72 6e 61 6c 20 66 69 6c 65 20 73 68 6f 72 74 6c rnal file shortl
fe440 79 20 61 66 74 65 72 20 69 74 20 69 73 20 63 72 y after it is cr
fe450 65 61 74 65 64 20 69 6e 20 6f 72 64 65 72 20 74 eated in order t
fe460 6f 20 64 6f 0a 2a 2a 20 77 68 61 74 65 76 65 72 o do.** whatever
fe470 20 69 74 20 64 6f 65 73 2e 20 20 57 68 69 6c 65 it does. While
fe480 20 74 68 69 73 20 6f 74 68 65 72 20 70 72 6f 63 this other proc
fe490 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 ess is holding t
fe4a0 68 65 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 6e 2c he.** file open,
fe4b0 20 77 65 20 77 69 6c 6c 20 62 65 20 75 6e 61 62 we will be unab
fe4c0 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 69 74 2e le to delete it.
fe4d0 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 To work around
fe4e0 20 74 68 69 73 0a 2a 2a 20 70 72 6f 62 6c 65 6d this.** problem
fe4f0 2c 20 77 65 20 64 65 6c 61 79 20 31 30 30 20 6d , we delay 100 m
fe500 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 illiseconds and
fe510 74 72 79 20 74 6f 20 64 65 6c 65 74 65 20 61 67 try to delete ag
fe520 61 69 6e 2e 20 20 55 70 0a 2a 2a 20 74 6f 20 4d ain. Up.** to M
fe530 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d X_DELETION_ATTEM
fe540 50 54 73 20 64 65 6c 65 74 69 6f 6e 20 61 74 74 PTs deletion att
fe550 65 6d 70 74 73 20 61 72 65 20 72 75 6e 20 62 65 empts are run be
fe560 66 6f 72 65 20 67 69 76 69 6e 67 0a 2a 2a 20 75 fore giving.** u
fe570 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 p and returning
fe580 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 an error..*/.#de
fe590 66 69 6e 65 20 4d 58 5f 44 45 4c 45 54 49 4f 4e fine MX_DELETION
fe5a0 5f 41 54 54 45 4d 50 54 53 20 35 0a 73 74 61 74 _ATTEMPTS 5.stat
fe5b0 69 63 20 69 6e 74 20 77 69 6e 44 65 6c 65 74 65 ic int winDelete
fe5c0 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
fe5d0 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 *pVfs,
fe5e0 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 /* Not used on w
fe5f0 69 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 in32 */. const
fe600 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c char *zFilename,
fe610 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
fe620 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 file to delete
fe630 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44 69 72 */. int syncDir
fe640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe650 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 /* Not used on
fe660 77 69 6e 33 32 20 2a 2f 0a 29 7b 0a 20 20 69 6e win32 */.){. in
fe670 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 44 57 4f t cnt = 0;. DWO
fe680 52 44 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 65 RD rc;. DWORD e
fe690 72 72 6f 72 20 3d 20 30 3b 0a 20 20 76 6f 69 64 rror = 0;. void
fe6a0 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 *zConverted = c
fe6b0 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 onvertUtf8Filena
fe6c0 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 me(zFilename);.
fe6d0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
fe6e0 52 28 70 56 66 73 29 3b 0a 20 20 55 4e 55 53 45 R(pVfs);. UNUSE
fe6f0 44 5f 50 41 52 41 4d 45 54 45 52 28 73 79 6e 63 D_PARAMETER(sync
fe700 44 69 72 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e Dir);. if( zCon
fe710 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 verted==0 ){.
fe720 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
fe730 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 OMEM;. }. Simu
fe740 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 lateIOError(retu
fe750 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
fe760 44 45 4c 45 54 45 29 3b 0a 20 20 69 66 28 20 69 DELETE);. if( i
fe770 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 64 6f 7b sNT() ){. do{
fe780 0a 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c . DeleteFil
fe790 65 57 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a eW(zConverted);.
fe7a0 20 20 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20 }while( (
fe7b0 20 20 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65 ((rc = GetFile
fe7c0 41 74 74 72 69 62 75 74 65 73 57 28 7a 43 6f 6e AttributesW(zCon
fe7d0 76 65 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41 verted)) != INVA
fe7e0 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 LID_FILE_ATTRIBU
fe7f0 54 45 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 TES).
fe800 20 20 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d || ((error =
fe810 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 GetLastError())
fe820 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53 == ERROR_ACCESS
fe830 5f 44 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20 _DENIED)).
fe840 20 20 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c && (++cnt <
fe850 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 MX_DELETION_ATT
fe860 45 4d 50 54 53 29 0a 20 20 20 20 20 20 20 20 20 EMPTS).
fe870 20 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 && (Sleep(100)
fe880 2c 20 31 29 20 29 3b 0a 2f 2a 20 69 73 4e 54 28 , 1) );./* isNT(
fe890 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 ) is 1 if SQLITE
fe8a0 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f _OS_WINCE==1, so
fe8b0 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 this else is ne
fe8c0 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a ver executed. .*
fe8d0 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 * Since the ASCI
fe8e0 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 I version of the
fe8f0 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 se Windows API d
fe900 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 o not exist for
fe910 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 WINCE,.** it's i
fe920 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 mportant to not
fe930 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 reference them f
fe940 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e or WINCE builds.
fe950 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
fe960 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c S_WINCE==0. }el
fe970 73 65 7b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 se{. do{.
fe980 20 20 44 65 6c 65 74 65 46 69 6c 65 41 28 7a 43 DeleteFileA(zC
fe990 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7d onverted);. }
fe9a0 77 68 69 6c 65 28 20 20 20 28 20 20 20 28 28 72 while( ( ((r
fe9b0 63 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 c = GetFileAttri
fe9c0 62 75 74 65 73 41 28 7a 43 6f 6e 76 65 72 74 65 butesA(zConverte
fe9d0 64 29 29 20 21 3d 20 49 4e 56 41 4c 49 44 5f 46 d)) != INVALID_F
fe9e0 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 29 0a ILE_ATTRIBUTES).
fe9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
fea00 7c 20 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c | ((error = GetL
fea10 61 73 74 45 72 72 6f 72 28 29 29 20 3d 3d 20 45 astError()) == E
fea20 52 52 4f 52 5f 41 43 43 45 53 53 5f 44 45 4e 49 RROR_ACCESS_DENI
fea30 45 44 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 ED)).
fea40 26 26 20 28 2b 2b 63 6e 74 20 3c 20 4d 58 5f 44 && (++cnt < MX_D
fea50 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 ELETION_ATTEMPTS
fea60 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 ). &&
fea70 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20 (Sleep(100), 1)
fea80 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 );.#endif. }.
fea90 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 free(zConverted)
feaa0 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 44 45 ;. OSTRACE2("DE
feab0 4c 45 54 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 LETE \"%s\"\n",
feac0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 65 zFilename);. re
fead0 74 75 72 6e 20 28 20 20 20 28 72 63 20 3d 3d 20 turn ( (rc ==
feae0 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 INVALID_FILE_ATT
feaf0 52 49 42 55 54 45 53 29 20 0a 20 20 20 20 20 20 RIBUTES) .
feb00 20 20 20 20 26 26 20 28 65 72 72 6f 72 20 3d 3d && (error ==
feb10 20 45 52 52 4f 52 5f 46 49 4c 45 5f 4e 4f 54 5f ERROR_FILE_NOT_
feb20 46 4f 55 4e 44 29 29 20 3f 20 53 51 4c 49 54 45 FOUND)) ? SQLITE
feb30 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 _OK : SQLITE_IOE
feb40 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a 0a 2f 2a RR_DELETE;.}../*
feb50 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 65 78 .** Check the ex
feb60 69 73 74 61 6e 63 65 20 61 6e 64 20 73 74 61 74 istance and stat
feb70 75 73 20 6f 66 20 61 20 66 69 6c 65 2e 0a 2a 2f us of a file..*/
feb80 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 41 .static int winA
feb90 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 ccess(. sqlite3
feba0 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
febb0 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 /* Not used
febc0 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f on win32 */. co
febd0 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
febe0 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 ame, /* Name
febf0 20 6f 66 20 66 69 6c 65 20 74 6f 20 63 68 65 63 of file to chec
fec00 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 k */. int flags
fec10 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
fec20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 65 73 /* Type of tes
fec30 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 69 t to make on thi
fec40 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 s file */. int
fec50 2a 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20 *pResOut
fec60 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 /* OUT: R
fec70 65 73 75 6c 74 20 2a 2f 0a 29 7b 0a 20 20 44 57 esult */.){. DW
fec80 4f 52 44 20 61 74 74 72 3b 0a 20 20 69 6e 74 20 ORD attr;. int
fec90 72 63 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a rc = 0;. void *
feca0 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e zConverted = con
fecb0 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 vertUtf8Filename
fecc0 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 (zFilename);. U
fecd0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
fece0 70 56 66 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f pVfs);. if( zCo
fecf0 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 nverted==0 ){.
fed00 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
fed10 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 NOMEM;. }. if(
fed20 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 61 isNT() ){. a
fed30 74 74 72 20 3d 20 47 65 74 46 69 6c 65 41 74 74 ttr = GetFileAtt
fed40 72 69 62 75 74 65 73 57 28 28 57 43 48 41 52 2a ributesW((WCHAR*
fed50 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a )zConverted);./*
fed60 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 isNT() is 1 if
fed70 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
fed80 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 =1, so this else
fed90 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 is never execut
feda0 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 ed. .** Since th
fedb0 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 e ASCII version
fedc0 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 of these Windows
fedd0 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 API do not exis
fede0 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 t for WINCE,.**
fedf0 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 it's important t
fee00 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 o not reference
fee10 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 them for WINCE b
fee20 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 uilds..*/.#if SQ
fee30 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 LITE_OS_WINCE==0
fee40 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 74 . }else{. at
fee50 74 72 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 tr = GetFileAttr
fee60 69 62 75 74 65 73 41 28 28 63 68 61 72 2a 29 7a ibutesA((char*)z
fee70 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e 64 Converted);.#end
fee80 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43 if. }. free(zC
fee90 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 73 77 69 onverted);. swi
feea0 74 63 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 tch( flags ){.
feeb0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 case SQLITE_AC
feec0 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 CESS_READ:. c
feed0 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 ase SQLITE_ACCES
feee0 53 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 S_EXISTS:.
feef0 72 63 20 3d 20 61 74 74 72 21 3d 49 4e 56 41 4c rc = attr!=INVAL
fef00 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 ID_FILE_ATTRIBUT
fef10 45 53 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b ES;. break;
fef20 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
fef30 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 _ACCESS_READWRIT
fef40 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 61 E:. rc = (a
fef50 74 74 72 20 26 20 46 49 4c 45 5f 41 54 54 52 49 ttr & FILE_ATTRI
fef60 42 55 54 45 5f 52 45 41 44 4f 4e 4c 59 29 3d 3d BUTE_READONLY)==
fef70 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0;. break;.
fef80 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 default:.
fef90 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 61 assert(!"Inva
fefa0 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65 lid flags argume
fefb0 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 nt");. }. *pRe
fefc0 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 sOut = rc;. ret
fefd0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
fefe0 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 .../*.** Turn a
feff0 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d relative pathnam
ff000 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 e into a full pa
ff010 74 68 6e 61 6d 65 2e 20 20 57 72 69 74 65 20 74 thname. Write t
ff020 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e he full.** pathn
ff030 61 6d 65 20 69 6e 74 6f 20 7a 4f 75 74 5b 5d 2e ame into zOut[].
ff040 20 20 7a 4f 75 74 5b 5d 20 77 69 6c 6c 20 62 65 zOut[] will be
ff050 20 61 74 20 6c 65 61 73 74 20 70 56 66 73 2d 3e at least pVfs->
ff060 6d 78 50 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 mxPathname.** by
ff070 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a tes in size..*/.
ff080 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 75 static int winFu
ff090 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 llPathname(. sq
ff0a0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
ff0b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
ff0c0 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 ointer to vfs ob
ff0d0 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ject */. const
ff0e0 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 2c char *zRelative,
ff0f0 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 /* Possi
ff100 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70 bly relative inp
ff110 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 ut path */. int
ff120 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 nFull,
ff130 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
ff140 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 ze of output buf
ff150 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a fer in bytes */.
ff160 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 char *zFull
ff170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff180 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 /* Output buffer
ff190 20 2a 2f 0a 29 7b 0a 20 20 0a 23 69 66 20 64 65 */.){. .#if de
ff1a0 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f fined(__CYGWIN__
ff1b0 29 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ). UNUSED_PARAM
ff1c0 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 63 ETER(nFull);. c
ff1d0 79 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 66 75 ygwin_conv_to_fu
ff1e0 6c 6c 5f 77 69 6e 33 32 5f 70 61 74 68 28 7a 52 ll_win32_path(zR
ff1f0 65 6c 61 74 69 76 65 2c 20 7a 46 75 6c 6c 29 3b elative, zFull);
ff200 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
ff210 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 _OK;.#endif..#if
ff220 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
ff230 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
ff240 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 2f 2a TER(nFull);. /*
ff250 20 57 69 6e 43 45 20 68 61 73 20 6e 6f 20 63 6f WinCE has no co
ff260 6e 63 65 70 74 20 6f 66 20 61 20 72 65 6c 61 74 ncept of a relat
ff270 69 76 65 20 70 61 74 68 6e 61 6d 65 2c 20 6f 72 ive pathname, or
ff280 20 73 6f 20 49 20 61 6d 20 74 6f 6c 64 2e 20 2a so I am told. *
ff290 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 /. sqlite3_snpr
ff2a0 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 intf(pVfs->mxPat
ff2b0 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 hname, zFull, "%
ff2c0 73 22 2c 20 7a 52 65 6c 61 74 69 76 65 29 3b 0a s", zRelative);.
ff2d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
ff2e0 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 OK;.#endif..#if
ff2f0 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 !SQLITE_OS_WINCE
ff300 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 43 && !defined(__C
ff310 59 47 57 49 4e 5f 5f 29 0a 20 20 69 6e 74 20 6e YGWIN__). int n
ff320 42 79 74 65 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 Byte;. void *zC
ff330 6f 6e 76 65 72 74 65 64 3b 0a 20 20 63 68 61 72 onverted;. char
ff340 20 2a 7a 4f 75 74 3b 0a 20 20 55 4e 55 53 45 44 *zOut;. UNUSED
ff350 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c _PARAMETER(nFull
ff360 29 3b 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 );. zConverted
ff370 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c = convertUtf8Fil
ff380 65 6e 61 6d 65 28 7a 52 65 6c 61 74 69 76 65 29 ename(zRelative)
ff390 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 ;. if( isNT() )
ff3a0 7b 0a 20 20 20 20 57 43 48 41 52 20 2a 7a 54 65 {. WCHAR *zTe
ff3b0 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 mp;. nByte =
ff3c0 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57 GetFullPathNameW
ff3d0 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 ((WCHAR*)zConver
ff3e0 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20 ted, 0, 0, 0) +
ff3f0 33 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d 3;. zTemp = m
ff400 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a alloc( nByte*siz
ff410 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b eof(zTemp[0]) );
ff420 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d . if( zTemp==
ff430 30 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65 28 0 ){. free(
ff440 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 zConverted);.
ff450 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
ff460 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 _NOMEM;. }.
ff470 20 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d GetFullPathNam
ff480 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 eW((WCHAR*)zConv
ff490 65 72 74 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54 erted, nByte, zT
ff4a0 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 emp, 0);. fre
ff4b0 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 e(zConverted);.
ff4c0 20 20 20 7a 4f 75 74 20 3d 20 75 6e 69 63 6f 64 zOut = unicod
ff4d0 65 54 6f 55 74 66 38 28 7a 54 65 6d 70 29 3b 0a eToUtf8(zTemp);.
ff4e0 20 20 20 20 66 72 65 65 28 7a 54 65 6d 70 29 3b free(zTemp);
ff4f0 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 ./* isNT() is 1
ff500 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
ff510 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 CE==1, so this e
ff520 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 lse is never exe
ff530 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 cuted. .** Since
ff540 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 the ASCII versi
ff550 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 on of these Wind
ff560 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 ows API do not e
ff570 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a xist for WINCE,.
ff580 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e ** it's importan
ff590 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e t to not referen
ff5a0 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 ce them for WINC
ff5b0 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 E builds..*/.#if
ff5c0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
ff5d0 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ==0. }else{.
ff5e0 20 63 68 61 72 20 2a 7a 54 65 6d 70 3b 0a 20 20 char *zTemp;.
ff5f0 20 20 6e 42 79 74 65 20 3d 20 47 65 74 46 75 6c nByte = GetFul
ff600 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68 61 72 lPathNameA((char
ff610 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c *)zConverted, 0,
ff620 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 20 20 0, 0) + 3;.
ff630 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 zTemp = malloc(
ff640 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 54 65 nByte*sizeof(zTe
ff650 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 mp[0]) );. if
ff660 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 ( zTemp==0 ){.
ff670 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 free(zConver
ff680 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ted);. retu
ff690 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
ff6a0 0a 20 20 20 20 7d 0a 20 20 20 20 47 65 74 46 75 . }. GetFu
ff6b0 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68 61 llPathNameA((cha
ff6c0 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e r*)zConverted, n
ff6d0 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b Byte, zTemp, 0);
ff6e0 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 . free(zConve
ff6f0 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20 rted);. zOut
ff700 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f = sqlite3_win32_
ff710 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 54 65 mbcs_to_utf8(zTe
ff720 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 mp);. free(zT
ff730 65 6d 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d emp);.#endif. }
ff740 0a 20 20 69 66 28 20 7a 4f 75 74 20 29 7b 0a 20 . if( zOut ){.
ff750 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
ff760 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 68 ntf(pVfs->mxPath
ff770 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 name, zFull, "%s
ff780 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 66 72 ", zOut);. fr
ff790 65 65 28 7a 4f 75 74 29 3b 0a 20 20 20 20 72 65 ee(zOut);. re
ff7a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
ff7b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
ff7c0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
ff7d0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a ;. }.#endif.}..
ff7e0 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 65 /*.** Get the se
ff7f0 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 65 ctor size of the
ff800 20 64 65 76 69 63 65 20 75 73 65 64 20 74 6f 20 device used to
ff810 73 74 6f 72 65 0a 2a 2a 20 66 69 6c 65 2e 0a 2a store.** file..*
ff820 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 /.static int get
ff830 53 65 63 74 6f 72 53 69 7a 65 28 0a 20 20 20 20 SectorSize(.
ff840 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
ff850 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 s,. const cha
ff860 72 20 2a 7a 52 65 6c 61 74 69 76 65 20 20 20 20 r *zRelative
ff870 20 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65 20 6e /* UTF-8 file n
ff880 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 44 57 4f 52 ame */.){. DWOR
ff890 44 20 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 D bytesPerSector
ff8a0 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c = SQLITE_DEFAUL
ff8b0 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 T_SECTOR_SIZE;.
ff8c0 20 2f 2a 20 47 65 74 44 69 73 6b 46 72 65 65 53 /* GetDiskFreeS
ff8d0 70 61 63 65 20 69 73 20 6e 6f 74 20 73 75 70 70 pace is not supp
ff8e0 6f 72 74 65 64 20 75 6e 64 65 72 20 57 49 4e 43 orted under WINC
ff8f0 45 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f E */.#if SQLITE_
ff900 4f 53 5f 57 49 4e 43 45 0a 20 20 55 4e 55 53 45 OS_WINCE. UNUSE
ff910 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 D_PARAMETER(pVfs
ff920 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
ff930 4d 45 54 45 52 28 7a 52 65 6c 61 74 69 76 65 29 METER(zRelative)
ff940 3b 0a 23 65 6c 73 65 0a 20 20 63 68 61 72 20 7a ;.#else. char z
ff950 46 75 6c 6c 70 61 74 68 5b 4d 41 58 5f 50 41 54 Fullpath[MAX_PAT
ff960 48 2b 31 5d 3b 0a 20 20 69 6e 74 20 72 63 3b 0a H+1];. int rc;.
ff970 20 20 44 57 4f 52 44 20 64 77 52 65 74 20 3d 20 DWORD dwRet =
ff980 30 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 75 6d 0;. DWORD dwDum
ff990 6d 79 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 57 my;.. /*. ** W
ff9a0 65 20 6e 65 65 64 20 74 6f 20 67 65 74 20 74 68 e need to get th
ff9b0 65 20 66 75 6c 6c 20 70 61 74 68 20 6e 61 6d 65 e full path name
ff9c0 20 6f 66 20 74 68 65 20 66 69 6c 65 0a 20 20 2a of the file. *
ff9d0 2a 20 74 6f 20 67 65 74 20 74 68 65 20 64 72 69 * to get the dri
ff9e0 76 65 20 6c 65 74 74 65 72 20 74 6f 20 6c 6f 6f ve letter to loo
ff9f0 6b 20 75 70 20 74 68 65 20 73 65 63 74 6f 72 0a k up the sector.
ffa00 20 20 2a 2a 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a ** size.. */.
ffa10 20 20 72 63 20 3d 20 77 69 6e 46 75 6c 6c 50 61 rc = winFullPa
ffa20 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 52 65 thname(pVfs, zRe
ffa30 6c 61 74 69 76 65 2c 20 4d 41 58 5f 50 41 54 48 lative, MAX_PATH
ffa40 2c 20 7a 46 75 6c 6c 70 61 74 68 29 3b 0a 20 20 , zFullpath);.
ffa50 69 66 28 20 72 63 20 3d 3d 20 53 51 4c 49 54 45 if( rc == SQLITE
ffa60 5f 4f 4b 20 29 0a 20 20 7b 0a 20 20 20 20 76 6f _OK ). {. vo
ffa70 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d id *zConverted =
ffa80 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 convertUtf8File
ffa90 6e 61 6d 65 28 7a 46 75 6c 6c 70 61 74 68 29 3b name(zFullpath);
ffaa0 0a 20 20 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 . if( zConver
ffab0 74 65 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ted ){. if(
ffac0 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 20 isNT() ){.
ffad0 20 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68 20 /* trim path
ffae0 74 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72 65 to just drive re
ffaf0 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20 20 ference */.
ffb00 20 20 20 57 43 48 41 52 20 2a 70 20 3d 20 7a 43 WCHAR *p = zC
ffb10 6f 6e 76 65 72 74 65 64 3b 0a 20 20 20 20 20 20 onverted;.
ffb20 20 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b 29 7b 0a for(;*p;p++){.
ffb30 20 20 20 20 20 20 20 20 20 20 69 66 28 20 2a 70 if( *p
ffb40 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20 20 20 20 == '\\' ){.
ffb50 20 20 20 20 20 20 20 20 2a 70 20 3d 20 27 5c 30 *p = '\0
ffb60 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 ';. b
ffb70 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 reak;.
ffb80 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
ffb90 20 20 20 20 64 77 52 65 74 20 3d 20 47 65 74 44 dwRet = GetD
ffba0 69 73 6b 46 72 65 65 53 70 61 63 65 57 28 28 57 iskFreeSpaceW((W
ffbb0 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 CHAR*)zConverted
ffbc0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
ffbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ffbe0 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20 &dwDummy,.
ffbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ffc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ffc10 26 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 2c &bytesPerSector,
ffc20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
ffc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ffc40 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20 20 &dwDummy,.
ffc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ffc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
ffc70 64 77 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 dwDummy);.
ffc80 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f }else{. /
ffc90 2a 20 74 72 69 6d 20 70 61 74 68 20 74 6f 20 6a * trim path to j
ffca0 75 73 74 20 64 72 69 76 65 20 72 65 66 65 72 65 ust drive refere
ffcb0 6e 63 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 nce */. C
ffcc0 48 41 52 20 2a 70 20 3d 20 28 43 48 41 52 20 2a HAR *p = (CHAR *
ffcd0 29 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 20 )zConverted;.
ffce0 20 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b for(;*p;p++
ffcf0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
ffd00 20 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20 *p == '\\' ){.
ffd10 20 20 20 20 20 20 20 20 20 20 20 2a 70 20 3d 20 *p =
ffd20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 '\0';.
ffd30 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
ffd40 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
ffd50 20 20 20 20 20 20 20 64 77 52 65 74 20 3d 20 47 dwRet = G
ffd60 65 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 41 etDiskFreeSpaceA
ffd70 28 28 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 ((CHAR*)zConvert
ffd80 65 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ed,.
ffd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ffda0 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a &dwDummy,.
ffdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ffdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ffdd0 20 20 26 62 79 74 65 73 50 65 72 53 65 63 74 6f &bytesPerSecto
ffde0 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 r,.
ffdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ffe00 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 &dwDummy,.
ffe10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ffe20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ffe30 20 26 64 77 44 75 6d 6d 79 29 3b 0a 20 20 20 20 &dwDummy);.
ffe40 20 20 7d 0a 20 20 20 20 20 20 66 72 65 65 28 7a }. free(z
ffe50 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 Converted);.
ffe60 7d 0a 20 20 20 20 69 66 28 20 21 64 77 52 65 74 }. if( !dwRet
ffe70 20 29 7b 0a 20 20 20 20 20 20 62 79 74 65 73 50 ){. bytesP
ffe80 65 72 53 65 63 74 6f 72 20 3d 20 53 51 4c 49 54 erSector = SQLIT
ffe90 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 E_DEFAULT_SECTOR
ffea0 5f 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d _SIZE;. }. }
ffeb0 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
ffec0 20 28 69 6e 74 29 20 62 79 74 65 73 50 65 72 53 (int) bytesPerS
ffed0 65 63 74 6f 72 3b 20 0a 7d 0a 0a 23 69 66 6e 64 ector; .}..#ifnd
ffee0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c ef SQLITE_OMIT_L
ffef0 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a OAD_EXTENSION./*
fff00 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 .** Interfaces f
fff10 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 or opening a sha
fff20 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e red library, fin
fff30 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 ding entry point
fff40 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 s.** within the
fff50 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 shared library,
fff60 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 and closing the
fff70 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a shared library..
fff80 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 */./*.** Interfa
fff90 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 ces for opening
fffa0 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 a shared library
fffb0 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 , finding entry
fffc0 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e points.** within
fffd0 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 the shared libr
fffe0 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 ary, and closing
ffff0 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 the shared libr
10000 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ary..*/.static v
10001 6f 69 64 20 2a 77 69 6e 44 6c 4f 70 65 6e 28 73 oid *winDlOpen(s
10002 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
10003 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 , const char *zF
10004 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 48 41 4e 44 ilename){. HAND
10005 4c 45 20 68 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 LE h;. void *zC
10006 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 onverted = conve
10007 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a rtUtf8Filename(z
10008 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55 Filename);. UNU
10009 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 SED_PARAMETER(pV
1000a 66 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 fs);. if( zConv
1000b 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 erted==0 ){.
1000c 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
1000d 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 if( isNT() ){.
1000e 20 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72 h = LoadLibrar
1000f 79 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 yW((WCHAR*)zConv
10010 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e 54 28 erted);./* isNT(
10011 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 ) is 1 if SQLITE
10012 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f _OS_WINCE==1, so
10013 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 this else is ne
10014 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a ver executed. .*
10015 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 * Since the ASCI
10016 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 I version of the
10017 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 se Windows API d
10018 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 o not exist for
10019 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 WINCE,.** it's i
1001a 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 mportant to not
1001b 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 reference them f
1001c 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e or WINCE builds.
1001d 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
1001e 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c S_WINCE==0. }el
1001f 73 65 7b 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 se{. h = Load
10020 4c 69 62 72 61 72 79 41 28 28 63 68 61 72 2a 29 LibraryA((char*)
10021 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e zConverted);.#en
10022 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a dif. }. free(z
10023 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 72 65 Converted);. re
10024 74 75 72 6e 20 28 76 6f 69 64 2a 29 68 3b 0a 7d turn (void*)h;.}
10025 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e .static void win
10026 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f DlError(sqlite3_
10027 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e vfs *pVfs, int n
10028 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f Buf, char *zBufO
10029 75 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 ut){. UNUSED_PA
1002a 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 RAMETER(pVfs);.
1002b 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 getLastErrorMsg
1002c 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 29 3b (nBuf, zBufOut);
1002d 0a 7d 0a 76 6f 69 64 20 28 2a 77 69 6e 44 6c 53 .}.void (*winDlS
1002e 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ym(sqlite3_vfs *
1002f 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e pVfs, void *pHan
10030 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 dle, const char
10031 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 *zSymbol))(void)
10032 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
10033 45 54 45 52 28 70 56 66 73 29 3b 0a 23 69 66 20 ETER(pVfs);.#if
10034 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a SQLITE_OS_WINCE.
10035 20 20 2f 2a 20 54 68 65 20 47 65 74 50 72 6f 63 /* The GetProc
10036 41 64 64 72 65 73 73 41 28 29 20 72 6f 75 74 69 AddressA() routi
10037 6e 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c ne is only avail
10038 61 62 6c 65 20 6f 6e 20 77 69 6e 63 65 2e 20 2a able on wince. *
10039 2f 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 /. return (void
1003a 28 2a 29 28 76 6f 69 64 29 29 47 65 74 50 72 6f (*)(void))GetPro
1003b 63 41 64 64 72 65 73 73 41 28 28 48 41 4e 44 4c cAddressA((HANDL
1003c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d 62 E)pHandle, zSymb
1003d 6f 6c 29 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 ol);.#else. /*
1003e 41 6c 6c 20 6f 74 68 65 72 20 77 69 6e 64 6f 77 All other window
1003f 73 20 70 6c 61 74 66 6f 72 6d 73 20 65 78 70 65 s platforms expe
10040 63 74 20 47 65 74 50 72 6f 63 41 64 64 72 65 73 ct GetProcAddres
10041 73 28 29 20 74 6f 20 74 61 6b 65 0a 20 20 2a 2a s() to take. **
10042 20 61 6e 20 41 6e 73 69 20 73 74 72 69 6e 67 20 an Ansi string
10043 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 regardless of th
10044 65 20 5f 55 4e 49 43 4f 44 45 20 73 65 74 74 69 e _UNICODE setti
10045 6e 67 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 ng */. return (
10046 76 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47 65 void(*)(void))Ge
10047 74 50 72 6f 63 41 64 64 72 65 73 73 28 28 48 41 tProcAddress((HA
10048 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 NDLE)pHandle, zS
10049 79 6d 62 6f 6c 29 3b 0a 23 65 6e 64 69 66 0a 7d ymbol);.#endif.}
1004a 0a 76 6f 69 64 20 77 69 6e 44 6c 43 6c 6f 73 65 .void winDlClose
1004b 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
1004c 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c fs, void *pHandl
1004d 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 e){. UNUSED_PAR
1004e 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 AMETER(pVfs);.
1004f 46 72 65 65 4c 69 62 72 61 72 79 28 28 48 41 4e FreeLibrary((HAN
10050 44 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a DLE)pHandle);.}.
10051 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 #else /* if SQLI
10052 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
10053 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 ENSION is define
10054 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 d: */. #define
10055 77 69 6e 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 winDlOpen 0. #
10056 64 65 66 69 6e 65 20 77 69 6e 44 6c 45 72 72 6f define winDlErro
10057 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 r 0. #define wi
10058 6e 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 nDlSym 0. #de
10059 66 69 6e 65 20 77 69 6e 44 6c 43 6c 6f 73 65 20 fine winDlClose
1005a 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 0.#endif.../*.**
1005b 20 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 Write up to nBu
1005c 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f f bytes of rando
1005d 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e mness into zBuf.
1005e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
1005f 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c inRandomness(sql
10060 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
10061 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a int nBuf, char *
10062 7a 42 75 66 29 7b 0a 20 20 69 6e 74 20 6e 20 3d zBuf){. int n =
10063 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 0;. UNUSED_PAR
10064 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 23 69 AMETER(pVfs);.#i
10065 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
10066 5f 54 45 53 54 29 0a 20 20 6e 20 3d 20 6e 42 75 _TEST). n = nBu
10067 66 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 f;. memset(zBuf
10068 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23 65 6c 73 , 0, nBuf);.#els
10069 65 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 53 e. if( sizeof(S
1006a 59 53 54 45 4d 54 49 4d 45 29 3c 3d 6e 42 75 66 YSTEMTIME)<=nBuf
1006b 2d 6e 20 29 7b 0a 20 20 20 20 53 59 53 54 45 4d -n ){. SYSTEM
1006c 54 49 4d 45 20 78 3b 0a 20 20 20 20 47 65 74 53 TIME x;. GetS
1006d 79 73 74 65 6d 54 69 6d 65 28 26 78 29 3b 0a 20 ystemTime(&x);.
1006e 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b memcpy(&zBuf[
1006f 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 n], &x, sizeof(x
10070 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a ));. n += siz
10071 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 20 20 69 66 eof(x);. }. if
10072 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c ( sizeof(DWORD)<
10073 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 =nBuf-n ){. D
10074 57 4f 52 44 20 70 69 64 20 3d 20 47 65 74 43 75 WORD pid = GetCu
10075 72 72 65 6e 74 50 72 6f 63 65 73 73 49 64 28 29 rrentProcessId()
10076 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 ;. memcpy(&zB
10077 75 66 5b 6e 5d 2c 20 26 70 69 64 2c 20 73 69 7a uf[n], &pid, siz
10078 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 6e eof(pid));. n
10079 20 2b 3d 20 73 69 7a 65 6f 66 28 70 69 64 29 3b += sizeof(pid);
1007a 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f . }. if( sizeo
1007b 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e f(DWORD)<=nBuf-n
1007c 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 63 6e ){. DWORD cn
1007d 74 20 3d 20 47 65 74 54 69 63 6b 43 6f 75 6e 74 t = GetTickCount
1007e 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 ();. memcpy(&
1007f 7a 42 75 66 5b 6e 5d 2c 20 26 63 6e 74 2c 20 73 zBuf[n], &cnt, s
10080 69 7a 65 6f 66 28 63 6e 74 29 29 3b 0a 20 20 20 izeof(cnt));.
10081 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 63 6e 74 n += sizeof(cnt
10082 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a );. }. if( siz
10083 65 6f 66 28 4c 41 52 47 45 5f 49 4e 54 45 47 45 eof(LARGE_INTEGE
10084 52 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 R)<=nBuf-n ){.
10085 20 20 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 20 LARGE_INTEGER
10086 69 3b 0a 20 20 20 20 51 75 65 72 79 50 65 72 66 i;. QueryPerf
10087 6f 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72 28 26 ormanceCounter(&
10088 69 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 i);. memcpy(&
10089 7a 42 75 66 5b 6e 5d 2c 20 26 69 2c 20 73 69 7a zBuf[n], &i, siz
1008a 65 6f 66 28 69 29 29 3b 0a 20 20 20 20 6e 20 2b eof(i));. n +
1008b 3d 20 73 69 7a 65 6f 66 28 69 29 3b 0a 20 20 7d = sizeof(i);. }
1008c 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
1008d 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c n;.}.../*.** Sl
1008e 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 eep for a little
1008f 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 while. Return
10090 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 the amount of ti
10091 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 73 74 61 me slept..*/.sta
10092 74 69 63 20 69 6e 74 20 77 69 6e 53 6c 65 65 70 tic int winSleep
10093 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
10094 66 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 fs, int microsec
10095 29 7b 0a 20 20 53 6c 65 65 70 28 28 6d 69 63 72 ){. Sleep((micr
10096 6f 73 65 63 2b 39 39 39 29 2f 31 30 30 30 29 3b osec+999)/1000);
10097 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
10098 54 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 TER(pVfs);. ret
10099 75 72 6e 20 28 28 6d 69 63 72 6f 73 65 63 2b 39 urn ((microsec+9
1009a 39 39 29 2f 31 30 30 30 29 2a 31 30 30 30 3b 0a 99)/1000)*1000;.
1009b 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c }../*.** The fol
1009c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c lowing variable,
1009d 20 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e if set to a non
1009e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 -zero value, bec
1009f 6f 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a omes the result.
100a0 2a 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d ** returned from
100a1 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e sqlite3OsCurren
100a2 74 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 tTime(). This i
100a3 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 s used for testi
100a4 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ng..*/.#ifdef SQ
100a5 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
100a6 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
100a7 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 _current_time =
100a8 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0;.#endif../*.**
100a9 20 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e Find the curren
100aa 74 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 t time (in Unive
100ab 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 rsal Coordinated
100ac 20 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 Time). Write t
100ad 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 he.** current ti
100ae 6d 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 me and date as a
100af 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 Julian Day numb
100b0 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 er into *prNow a
100b1 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 nd.** return 0.
100b2 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 Return 1 if the
100b3 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 time and date c
100b4 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a annot be found..
100b5 2a 2f 0a 69 6e 74 20 77 69 6e 43 75 72 72 65 6e */.int winCurren
100b6 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 tTime(sqlite3_vf
100b7 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 s *pVfs, double
100b8 2a 70 72 4e 6f 77 29 7b 0a 20 20 46 49 4c 45 54 *prNow){. FILET
100b9 49 4d 45 20 66 74 3b 0a 20 20 2f 2a 20 46 49 4c IME ft;. /* FIL
100ba 45 54 49 4d 45 20 73 74 72 75 63 74 75 72 65 20 ETIME structure
100bb 69 73 20 61 20 36 34 2d 62 69 74 20 76 61 6c 75 is a 64-bit valu
100bc 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 e representing t
100bd 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 20 20 he number of .
100be 20 20 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 100-nanosecon
100bf 64 20 69 6e 74 65 72 76 61 6c 73 20 73 69 6e 63 d intervals sinc
100c0 65 20 4a 61 6e 75 61 72 79 20 31 2c 20 31 36 30 e January 1, 160
100c1 31 20 28 3d 20 4a 44 20 32 33 30 35 38 31 33 2e 1 (= JD 2305813.
100c2 35 29 2e 20 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 5). . */. sqli
100c3 74 65 33 5f 69 6e 74 36 34 20 74 69 6d 65 57 3b te3_int64 timeW;
100c4 20 20 20 2f 2a 20 57 68 6f 6c 65 20 64 61 79 73 /* Whole days
100c5 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e */. sqlite3_in
100c6 74 36 34 20 74 69 6d 65 46 3b 20 20 20 2f 2a 20 t64 timeF; /*
100c7 46 72 61 63 74 69 6f 6e 61 6c 20 44 61 79 73 20 Fractional Days
100c8 2a 2f 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 */.. /* Number
100c9 6f 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e of 100-nanosecon
100ca 64 20 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 61 d intervals in a
100cb 20 73 69 6e 67 6c 65 20 64 61 79 20 2a 2f 0a 20 single day */.
100cc 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 static const sq
100cd 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 50 lite3_int64 ntuP
100ce 65 72 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31 erDay = . 1
100cf 30 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33 0000000*(sqlite3
100d0 5f 69 6e 74 36 34 29 38 36 34 30 30 3b 0a 0a 20 _int64)86400;..
100d1 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 31 30 /* Number of 10
100d2 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 0-nanosecond int
100d3 65 72 76 61 6c 73 20 69 6e 20 68 61 6c 66 20 6f ervals in half o
100d4 66 20 61 20 64 61 79 20 2a 2f 0a 20 20 73 74 61 f a day */. sta
100d5 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
100d6 33 5f 69 6e 74 36 34 20 6e 74 75 50 65 72 48 61 3_int64 ntuPerHa
100d7 6c 66 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31 lfDay = . 1
100d8 30 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33 0000000*(sqlite3
100d9 5f 69 6e 74 36 34 29 34 33 32 30 30 3b 0a 0a 20 _int64)43200;..
100da 20 2f 2a 20 32 5e 33 32 20 2d 20 74 6f 20 61 76 /* 2^32 - to av
100db 6f 69 64 20 75 73 65 20 6f 66 20 4c 4c 20 61 6e oid use of LL an
100dc 64 20 77 61 72 6e 69 6e 67 73 20 69 6e 20 67 63 d warnings in gc
100dd 63 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f c */. static co
100de 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 nst sqlite3_int6
100df 34 20 6d 61 78 33 32 42 69 74 56 61 6c 75 65 20 4 max32BitValue
100e0 3d 20 0a 20 20 20 20 20 20 28 73 71 6c 69 74 65 = . (sqlite
100e1 33 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30 30 3_int64)20000000
100e2 30 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 00 + (sqlite3_in
100e3 74 36 34 29 32 30 30 30 30 30 30 30 30 30 20 2b t64)2000000000 +
100e4 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 (sqlite3_int64)
100e5 32 39 34 39 36 37 32 39 36 3b 0a 0a 23 69 66 20 294967296;..#if
100e6 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a SQLITE_OS_WINCE.
100e7 20 20 53 59 53 54 45 4d 54 49 4d 45 20 74 69 6d SYSTEMTIME tim
100e8 65 3b 0a 20 20 47 65 74 53 79 73 74 65 6d 54 69 e;. GetSystemTi
100e9 6d 65 28 26 74 69 6d 65 29 3b 0a 20 20 2f 2a 20 me(&time);. /*
100ea 69 66 20 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 if SystemTimeToF
100eb 69 6c 65 54 69 6d 65 28 29 20 66 61 69 6c 73 2c ileTime() fails,
100ec 20 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72 6f it returns zero
100ed 2e 20 2a 2f 0a 20 20 69 66 20 28 21 53 79 73 74 . */. if (!Syst
100ee 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 6d 65 emTimeToFileTime
100ef 28 26 74 69 6d 65 2c 26 66 74 29 29 7b 0a 20 20 (&time,&ft)){.
100f0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
100f1 23 65 6c 73 65 0a 20 20 47 65 74 53 79 73 74 65 #else. GetSyste
100f2 6d 54 69 6d 65 41 73 46 69 6c 65 54 69 6d 65 28 mTimeAsFileTime(
100f3 20 26 66 74 20 29 3b 0a 23 65 6e 64 69 66 0a 20 &ft );.#endif.
100f4 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
100f5 52 28 70 56 66 73 29 3b 0a 20 20 74 69 6d 65 57 R(pVfs);. timeW
100f6 20 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e = (((sqlite3_in
100f7 74 36 34 29 66 74 2e 64 77 48 69 67 68 44 61 74 t64)ft.dwHighDat
100f8 65 54 69 6d 65 29 2a 6d 61 78 33 32 42 69 74 56 eTime)*max32BitV
100f9 61 6c 75 65 29 20 2b 20 28 73 71 6c 69 74 65 33 alue) + (sqlite3
100fa 5f 69 6e 74 36 34 29 66 74 2e 64 77 4c 6f 77 44 _int64)ft.dwLowD
100fb 61 74 65 54 69 6d 65 3b 0a 20 20 74 69 6d 65 46 ateTime;. timeF
100fc 20 3d 20 74 69 6d 65 57 20 25 20 6e 74 75 50 65 = timeW % ntuPe
100fd 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f rDay; /
100fe 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 61 79 * fractional day
100ff 73 20 28 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e s (100-nanosecon
10100 64 73 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d ds) */. timeW =
10101 20 74 69 6d 65 57 20 2f 20 6e 74 75 50 65 72 44 timeW / ntuPerD
10102 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ay; /*
10103 77 68 6f 6c 65 20 64 61 79 73 20 2a 2f 0a 20 20 whole days */.
10104 74 69 6d 65 57 20 3d 20 74 69 6d 65 57 20 2b 20 timeW = timeW +
10105 32 33 30 35 38 31 33 3b 20 20 20 20 20 20 20 20 2305813;
10106 20 20 20 20 2f 2a 20 61 64 64 20 77 68 6f 6c 65 /* add whole
10107 20 64 61 79 73 20 28 66 72 6f 6d 20 32 33 30 35 days (from 2305
10108 38 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 65 813.5) */. time
10109 46 20 3d 20 74 69 6d 65 46 20 2b 20 6e 74 75 50 F = timeF + ntuP
1010a 65 72 48 61 6c 66 44 61 79 3b 20 20 20 20 20 20 erHalfDay;
1010b 2f 2a 20 61 64 64 20 68 61 6c 66 20 61 20 64 61 /* add half a da
1010c 79 20 28 66 72 6f 6d 20 32 33 30 35 38 31 33 2e y (from 2305813.
1010d 35 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20 5) */. timeW =
1010e 74 69 6d 65 57 20 2b 20 28 74 69 6d 65 46 2f 6e timeW + (timeF/n
1010f 74 75 50 65 72 44 61 79 29 3b 20 20 2f 2a 20 61 tuPerDay); /* a
10110 64 64 20 77 68 6f 6c 65 20 64 61 79 20 69 66 20 dd whole day if
10111 68 61 6c 66 20 64 61 79 20 6d 61 64 65 20 6f 6e half day made on
10112 65 20 2a 2f 0a 20 20 74 69 6d 65 46 20 3d 20 74 e */. timeF = t
10113 69 6d 65 46 20 25 20 6e 74 75 50 65 72 44 61 79 imeF % ntuPerDay
10114 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f ; /* co
10115 6d 70 75 74 65 20 6e 65 77 20 66 72 61 63 74 69 mpute new fracti
10116 6f 6e 61 6c 20 64 61 79 73 20 2a 2f 0a 20 20 2a onal days */. *
10117 70 72 4e 6f 77 20 3d 20 28 64 6f 75 62 6c 65 29 prNow = (double)
10118 74 69 6d 65 57 20 2b 20 28 28 64 6f 75 62 6c 65 timeW + ((double
10119 29 74 69 6d 65 46 20 2f 20 28 64 6f 75 62 6c 65 )timeF / (double
1011a 29 6e 74 75 50 65 72 44 61 79 29 3b 0a 23 69 66 )ntuPerDay);.#if
1011b 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
1011c 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 if( sqlite3_cu
1011d 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 rrent_time ){.
1011e 20 20 2a 70 72 4e 6f 77 20 3d 20 28 28 64 6f 75 *prNow = ((dou
1011f 62 6c 65 29 73 71 6c 69 74 65 33 5f 63 75 72 72 ble)sqlite3_curr
10120 65 6e 74 5f 74 69 6d 65 20 2b 20 28 64 6f 75 62 ent_time + (doub
10121 6c 65 29 34 33 32 30 30 29 20 2f 20 28 64 6f 75 le)43200) / (dou
10122 62 6c 65 29 38 36 34 30 30 20 2b 20 28 64 6f 75 ble)86400 + (dou
10123 62 6c 65 29 32 34 34 30 35 38 37 3b 0a 20 20 7d ble)2440587;. }
10124 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
10125 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 0;.}../*.** The
10126 20 69 64 65 61 20 69 73 20 74 68 61 74 20 74 68 idea is that th
10127 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b is function work
10128 73 20 6c 69 6b 65 20 61 20 63 6f 6d 62 69 6e 61 s like a combina
10129 74 69 6f 6e 20 6f 66 0a 2a 2a 20 47 65 74 4c 61 tion of.** GetLa
1012a 73 74 45 72 72 6f 72 28 29 20 61 6e 64 20 46 6f stError() and Fo
1012b 72 6d 61 74 4d 65 73 73 61 67 65 28 29 20 6f 6e rmatMessage() on
1012c 20 77 69 6e 64 6f 77 73 20 28 6f 72 20 65 72 72 windows (or err
1012d 6e 6f 20 61 6e 64 0a 2a 2a 20 73 74 72 65 72 72 no and.** strerr
1012e 6f 72 5f 72 28 29 20 6f 6e 20 75 6e 69 78 29 2e or_r() on unix).
1012f 20 41 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 After an error
10130 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 is returned by a
10131 6e 20 4f 53 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e n OS.** function
10132 2c 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 , SQLite calls t
10133 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 his function wit
10134 68 20 7a 42 75 66 20 70 6f 69 6e 74 69 6e 67 20 h zBuf pointing
10135 74 6f 0a 2a 2a 20 61 20 62 75 66 66 65 72 20 6f to.** a buffer o
10136 66 20 6e 42 75 66 20 62 79 74 65 73 2e 20 54 68 f nBuf bytes. Th
10137 65 20 4f 53 20 6c 61 79 65 72 20 73 68 6f 75 6c e OS layer shoul
10138 64 20 70 6f 70 75 6c 61 74 65 20 74 68 65 0a 2a d populate the.*
10139 2a 20 62 75 66 66 65 72 20 77 69 74 68 20 61 20 * buffer with a
1013a 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 nul-terminated U
1013b 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 65 72 72 TF-8 encoded err
1013c 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 64 65 or message.** de
1013d 73 63 72 69 62 69 6e 67 20 74 68 65 20 6c 61 73 scribing the las
1013e 74 20 49 4f 20 65 72 72 6f 72 20 74 6f 20 68 61 t IO error to ha
1013f 76 65 20 6f 63 63 75 72 72 65 64 20 77 69 74 68 ve occurred with
10140 69 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a in the calling.*
10141 2a 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 * thread..**.**
10142 49 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 If the error mes
10143 73 61 67 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 sage is too larg
10144 65 20 66 6f 72 20 74 68 65 20 73 75 70 70 6c 69 e for the suppli
10145 65 64 20 62 75 66 66 65 72 2c 0a 2a 2a 20 69 74 ed buffer,.** it
10146 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 should be trunc
10147 61 74 65 64 2e 20 54 68 65 20 72 65 74 75 72 6e ated. The return
10148 20 76 61 6c 75 65 20 6f 66 20 78 47 65 74 4c 61 value of xGetLa
10149 73 74 45 72 72 6f 72 0a 2a 2a 20 69 73 20 7a 65 stError.** is ze
1014a 72 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 ro if the error
1014b 6d 65 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 message fits in
1014c 74 68 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e the buffer, or n
1014d 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 on-zero.** other
1014e 77 69 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 wise (if the mes
1014f 73 61 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 sage was truncat
10150 65 64 29 2e 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f ed). If non-zero
10151 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a is returned,.**
10152 20 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 20 then it is not
10153 6e 65 63 65 73 73 61 72 79 20 74 6f 20 69 6e 63 necessary to inc
10154 6c 75 64 65 20 74 68 65 20 6e 75 6c 2d 74 65 72 lude the nul-ter
10155 6d 69 6e 61 74 6f 72 20 63 68 61 72 61 63 74 65 minator characte
10156 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 75 74 70 r.** in the outp
10157 75 74 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a ut buffer..**.**
10158 20 4e 6f 74 20 73 75 70 70 6c 79 69 6e 67 20 61 Not supplying a
10159 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 n error message
1015a 77 69 6c 6c 20 68 61 76 65 20 6e 6f 20 61 64 76 will have no adv
1015b 65 72 73 65 20 65 66 66 65 63 74 0a 2a 2a 20 6f erse effect.** o
1015c 6e 20 53 51 4c 69 74 65 2e 20 49 74 20 69 73 20 n SQLite. It is
1015d 66 69 6e 65 20 74 6f 20 68 61 76 65 20 61 6e 20 fine to have an
1015e 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 implementation t
1015f 68 61 74 20 6e 65 76 65 72 0a 2a 2a 20 72 65 74 hat never.** ret
10160 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 urns an error me
10161 73 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 ssage:.**.** i
10162 6e 74 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 nt xGetLastError
10163 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
10164 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 fs, int nBuf, ch
10165 61 72 20 2a 7a 42 75 66 29 7b 0a 2a 2a 20 20 20 ar *zBuf){.**
10166 20 20 61 73 73 65 72 74 28 7a 42 75 66 5b 30 5d assert(zBuf[0]
10167 3d 3d 27 5c 30 27 29 3b 0a 2a 2a 20 20 20 20 20 =='\0');.**
10168 72 65 74 75 72 6e 20 30 3b 0a 2a 2a 20 20 20 7d return 0;.** }
10169 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 20 69 .**.** However i
1016a 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 f an error messa
1016b 67 65 20 69 73 20 73 75 70 70 6c 69 65 64 2c 20 ge is supplied,
1016c 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 63 6f 72 it will be incor
1016d 70 6f 72 61 74 65 64 0a 2a 2a 20 62 79 20 73 71 porated.** by sq
1016e 6c 69 74 65 20 69 6e 74 6f 20 74 68 65 20 65 72 lite into the er
1016f 72 6f 72 20 6d 65 73 73 61 67 65 20 61 76 61 69 ror message avai
10170 6c 61 62 6c 65 20 74 6f 20 74 68 65 20 75 73 65 lable to the use
10171 72 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 r using.** sqlit
10172 65 33 5f 65 72 72 6d 73 67 28 29 2c 20 70 6f 73 e3_errmsg(), pos
10173 73 69 62 6c 79 20 6d 61 6b 69 6e 67 20 49 4f 20 sibly making IO
10174 65 72 72 6f 72 73 20 65 61 73 69 65 72 20 74 6f errors easier to
10175 20 64 65 62 75 67 2e 0a 2a 2f 0a 73 74 61 74 69 debug..*/.stati
10176 63 20 69 6e 74 20 77 69 6e 47 65 74 4c 61 73 74 c int winGetLast
10177 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 Error(sqlite3_vf
10178 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 s *pVfs, int nBu
10179 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a f, char *zBuf){.
1017a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
1017b 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75 ER(pVfs);. retu
1017c 72 6e 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d rn getLastErrorM
1017d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 29 3b 0a sg(nBuf, zBuf);.
1017e 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c }../*.** Initial
1017f 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 ize and deinitia
10180 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69 lize the operati
10181 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 ng system interf
10182 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ace..*/.SQLITE_A
10183 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
10184 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 s_init(void){.
10185 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 static sqlite3_v
10186 66 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a 20 20 fs winVfs = {.
10187 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 1,
10188 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e /* iVersion
10189 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 77 */. sizeof(w
1018a 69 6e 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a inFile), /* sz
1018b 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 4d 41 OsFile */. MA
1018c 58 5f 50 41 54 48 2c 20 20 20 20 20 20 20 20 20 X_PATH,
1018d 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a /* mxPathname *
1018e 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 /. 0,
1018f 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 /* pNex
10190 74 20 2a 2f 0a 20 20 20 20 22 77 69 6e 33 32 22 t */. "win32"
10191 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a , /* z
10192 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 Name */. 0,
10193 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10194 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 20 0a * pAppData */. .
10195 20 20 20 20 77 69 6e 4f 70 65 6e 2c 20 20 20 20 winOpen,
10196 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 /* xOpen
10197 2a 2f 0a 20 20 20 20 77 69 6e 44 65 6c 65 74 65 */. winDelete
10198 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 , /* xDe
10199 6c 65 74 65 20 2a 2f 0a 20 20 20 20 77 69 6e 41 lete */. winA
1019a 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f ccess, /
1019b 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 * xAccess */.
1019c 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 winFullPathname
1019d 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 , /* xFullPath
1019e 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 44 name */. winD
1019f 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f lOpen, /
101a0 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 * xDlOpen */.
101a1 20 77 69 6e 44 6c 45 72 72 6f 72 2c 20 20 20 20 winDlError,
101a2 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 /* xDlError
101a3 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 53 79 6d 2c */. winDlSym,
101a4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c /* xDl
101a5 53 79 6d 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c Sym */. winDl
101a6 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a Close, /*
101a7 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 xDlClose */.
101a8 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 winRandomness,
101a9 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 /* xRandomne
101aa 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e 53 6c 65 ss */. winSle
101ab 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ep, /*
101ac 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 77 69 xSleep */. wi
101ad 6e 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 nCurrentTime,
101ae 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 /* xCurrentTime
101af 20 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74 4c 61 */. winGetLa
101b0 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47 stError /* xG
101b1 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20 etLastError */.
101b2 20 7d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 76 };.. sqlite3_v
101b3 66 73 5f 72 65 67 69 73 74 65 72 28 26 77 69 6e fs_register(&win
101b4 56 66 73 2c 20 31 29 3b 0a 20 20 72 65 74 75 72 Vfs, 1);. retur
101b5 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a n SQLITE_OK; .}.
101b6 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
101b7 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f qlite3_os_end(vo
101b8 69 64 29 7b 20 0a 20 20 72 65 74 75 72 6e 20 53 id){ . return S
101b9 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e QLITE_OK;.}..#en
101ba 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 dif /* SQLITE_OS
101bb 5f 57 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a _WIN */../******
101bc 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
101bd 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a os_win.c *******
101be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101c0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
101c1 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
101c2 69 6c 65 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a ile bitvec.c ***
101c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101c5 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
101c6 30 30 38 20 46 65 62 72 75 61 72 79 20 31 36 0a 008 February 16.
101c7 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
101c8 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
101c9 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
101ca 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
101cb 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
101cc 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
101cd 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
101ce 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
101cf 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
101d0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
101d1 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
101d2 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
101d3 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
101d4 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
101d5 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
101d6 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
101d7 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
101d8 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
101d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
101dd 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c * This file impl
101de 65 6d 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74 ements an object
101df 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 that represents
101e0 20 61 20 66 69 78 65 64 2d 6c 65 6e 67 74 68 0a a fixed-length.
101e1 2a 2a 20 62 69 74 6d 61 70 2e 20 20 42 69 74 73 ** bitmap. Bits
101e2 20 61 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 are numbered st
101e3 61 72 74 69 6e 67 20 77 69 74 68 20 31 2e 0a 2a arting with 1..*
101e4 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73 *.** A bitmap is
101e5 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 used to record
101e6 77 68 69 63 68 20 70 61 67 65 73 20 6f 66 20 61 which pages of a
101e7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 database file h
101e8 61 76 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f 75 72 ave been.** jour
101e9 6e 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 nalled during a
101ea 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 transaction, or
101eb 77 68 69 63 68 20 70 61 67 65 73 20 68 61 76 65 which pages have
101ec 20 74 68 65 20 22 64 6f 6e 74 2d 77 72 69 74 65 the "dont-write
101ed 22 0a 2a 2a 20 70 72 6f 70 65 72 74 79 2e 20 20 ".** property.
101ee 55 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 61 20 66 Usually only a f
101ef 65 77 20 70 61 67 65 73 20 61 72 65 20 6d 65 65 ew pages are mee
101f0 74 20 65 69 74 68 65 72 20 63 6f 6e 64 69 74 69 t either conditi
101f1 6f 6e 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 62 69 on..** So the bi
101f2 74 6d 61 70 20 69 73 20 75 73 75 61 6c 6c 79 20 tmap is usually
101f3 73 70 61 72 73 65 20 61 6e 64 20 68 61 73 20 6c sparse and has l
101f4 6f 77 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 0a ow cardinality..
101f5 2a 2a 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 73 ** But sometimes
101f6 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 68 (for example wh
101f7 65 6e 20 64 75 72 69 6e 67 20 61 20 44 52 4f 50 en during a DROP
101f8 20 6f 66 20 61 20 6c 61 72 67 65 20 74 61 62 6c of a large tabl
101f9 65 29 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20 61 6c e) most.** or al
101fa 6c 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 69 l of the pages i
101fb 6e 20 61 20 64 61 74 61 62 61 73 65 20 63 61 6e n a database can
101fc 20 67 65 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 2e get journalled.
101fd 20 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 In those cases
101fe 2c 20 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 70 , .** the bitmap
101ff 20 62 65 63 6f 6d 65 73 20 64 65 6e 73 65 20 77 becomes dense w
10200 69 74 68 20 68 69 67 68 20 63 61 72 64 69 6e 61 ith high cardina
10201 6c 69 74 79 2e 20 20 54 68 65 20 61 6c 67 6f 72 lity. The algor
10202 69 74 68 6d 20 6e 65 65 64 73 20 0a 2a 2a 20 74 ithm needs .** t
10203 6f 20 68 61 6e 64 6c 65 20 62 6f 74 68 20 63 61 o handle both ca
10204 73 65 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 ses well..**.**
10205 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 The size of the
10206 62 69 74 6d 61 70 20 69 73 20 66 69 78 65 64 20 bitmap is fixed
10207 77 68 65 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 when the object
10208 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a is created..**.*
10209 2a 20 41 6c 6c 20 62 69 74 73 20 61 72 65 20 63 * All bits are c
1020a 6c 65 61 72 20 77 68 65 6e 20 74 68 65 20 62 69 lear when the bi
1020b 74 6d 61 70 20 69 73 20 63 72 65 61 74 65 64 2e tmap is created.
1020c 20 20 49 6e 64 69 76 69 64 75 61 6c 20 62 69 74 Individual bit
1020d 73 0a 2a 2a 20 6d 61 79 20 62 65 20 73 65 74 20 s.** may be set
1020e 6f 72 20 63 6c 65 61 72 65 64 20 6f 6e 65 20 61 or cleared one a
1020f 74 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 t a time..**.**
10210 54 65 73 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 Test operations
10211 61 72 65 20 61 62 6f 75 74 20 31 30 30 20 74 69 are about 100 ti
10212 6d 65 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 20 mes more common
10213 74 68 61 74 20 73 65 74 20 6f 70 65 72 61 74 69 that set operati
10214 6f 6e 73 2e 0a 2a 2a 20 43 6c 65 61 72 20 6f 70 ons..** Clear op
10215 65 72 61 74 69 6f 6e 73 20 61 72 65 20 65 78 63 erations are exc
10216 65 65 64 69 6e 67 6c 79 20 72 61 72 65 2e 20 20 eedingly rare.
10217 54 68 65 72 65 20 61 72 65 20 75 73 75 61 6c 6c There are usuall
10218 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 35 20 61 y between.** 5 a
10219 6e 64 20 35 30 30 20 73 65 74 20 6f 70 65 72 61 nd 500 set opera
1021a 74 69 6f 6e 73 20 70 65 72 20 42 69 74 76 65 63 tions per Bitvec
1021b 20 6f 62 6a 65 63 74 2c 20 74 68 6f 75 67 68 20 object, though
1021c 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 the number of se
1021d 74 73 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65 74 69 ts can.** someti
1021e 6d 65 73 20 67 72 6f 77 20 69 6e 74 6f 20 74 65 mes grow into te
1021f 6e 73 20 6f 66 20 74 68 6f 75 73 61 6e 64 73 20 ns of thousands
10220 6f 72 20 6c 61 72 67 65 72 2e 20 20 54 68 65 20 or larger. The
10221 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 42 size of the.** B
10222 69 74 76 65 63 20 6f 62 6a 65 63 74 20 69 73 20 itvec object is
10223 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
10224 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 ges in the datab
10225 61 73 65 20 66 69 6c 65 20 61 74 20 74 68 65 0a ase file at the.
10226 2a 2a 20 73 74 61 72 74 20 6f 66 20 61 20 74 72 ** start of a tr
10227 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 69 ansaction, and i
10228 73 20 74 68 75 73 20 75 73 75 61 6c 6c 79 20 6c s thus usually l
10229 65 73 73 20 74 68 61 6e 20 61 20 66 65 77 20 74 ess than a few t
1022a 68 6f 75 73 61 6e 64 2c 0a 2a 2a 20 62 75 74 20 housand,.** but
1022b 63 61 6e 20 62 65 20 61 73 20 6c 61 72 67 65 20 can be as large
1022c 61 73 20 32 20 62 69 6c 6c 69 6f 6e 20 66 6f 72 as 2 billion for
1022d 20 61 20 72 65 61 6c 6c 79 20 62 69 67 20 64 61 a really big da
1022e 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 tabase..**.** @(
1022f 23 29 20 24 49 64 3a 20 62 69 74 76 65 63 2e 63 #) $Id: bitvec.c
10230 2c 76 20 31 2e 31 37 20 32 30 30 39 2f 30 37 2f ,v 1.17 2009/07/
10231 32 35 20 31 37 3a 33 33 3a 32 36 20 64 72 68 20 25 17:33:26 drh
10232 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 53 69 7a Exp $.*/../* Siz
10233 65 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 20 e of the Bitvec
10234 73 74 72 75 63 74 75 72 65 20 69 6e 20 62 79 74 structure in byt
10235 65 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 es. */.#define B
10236 49 54 56 45 43 5f 53 5a 20 20 20 20 20 20 20 20 ITVEC_SZ
10237 28 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 2a 31 (sizeof(void*)*1
10238 32 38 29 20 20 2f 2a 20 35 31 32 20 6f 6e 20 33 28) /* 512 on 3
10239 32 62 69 74 2e 20 20 31 30 32 34 20 6f 6e 20 36 2bit. 1024 on 6
1023a 34 62 69 74 20 2a 2f 0a 0a 2f 2a 20 52 6f 75 6e 4bit */../* Roun
1023b 64 20 74 68 65 20 75 6e 69 6f 6e 20 73 69 7a 65 d the union size
1023c 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65 61 down to the nea
1023d 72 65 73 74 20 70 6f 69 6e 74 65 72 20 62 6f 75 rest pointer bou
1023e 6e 64 61 72 79 2c 20 73 69 6e 63 65 20 74 68 61 ndary, since tha
1023f 74 27 73 20 68 6f 77 20 0a 2a 2a 20 69 74 20 77 t's how .** it w
10240 69 6c 6c 20 62 65 20 61 6c 69 67 6e 65 64 20 77 ill be aligned w
10241 69 74 68 69 6e 20 74 68 65 20 42 69 74 76 65 63 ithin the Bitvec
10242 20 73 74 72 75 63 74 2e 20 2a 2f 0a 23 64 65 66 struct. */.#def
10243 69 6e 65 20 42 49 54 56 45 43 5f 55 53 49 5a 45 ine BITVEC_USIZE
10244 20 20 20 20 20 28 28 28 42 49 54 56 45 43 5f 53 (((BITVEC_S
10245 5a 2d 28 33 2a 73 69 7a 65 6f 66 28 75 33 32 29 Z-(3*sizeof(u32)
10246 29 29 2f 73 69 7a 65 6f 66 28 42 69 74 76 65 63 ))/sizeof(Bitvec
10247 2a 29 29 2a 73 69 7a 65 6f 66 28 42 69 74 76 65 *))*sizeof(Bitve
10248 63 2a 29 29 0a 0a 2f 2a 20 54 79 70 65 20 6f 66 c*))../* Type of
10249 20 74 68 65 20 61 72 72 61 79 20 22 65 6c 65 6d the array "elem
1024a 65 6e 74 22 20 66 6f 72 20 74 68 65 20 62 69 74 ent" for the bit
1024b 6d 61 70 20 72 65 70 72 65 73 65 6e 74 61 74 69 map representati
1024c 6f 6e 2e 20 0a 2a 2a 20 53 68 6f 75 6c 64 20 62 on. .** Should b
1024d 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2c 20 e a power of 2,
1024e 61 6e 64 20 69 64 65 61 6c 6c 79 2c 20 65 76 65 and ideally, eve
1024f 6e 6c 79 20 64 69 76 69 64 65 20 69 6e 74 6f 20 nly divide into
10250 42 49 54 56 45 43 5f 55 53 49 5a 45 2e 20 0a 2a BITVEC_USIZE. .*
10251 2a 20 53 65 74 74 69 6e 67 20 74 68 69 73 20 74 * Setting this t
10252 6f 20 74 68 65 20 22 6e 61 74 75 72 61 6c 20 77 o the "natural w
10253 6f 72 64 22 20 73 69 7a 65 20 6f 66 20 79 6f 75 ord" size of you
10254 72 20 43 50 55 20 6d 61 79 20 69 6d 70 72 6f 76 r CPU may improv
10255 65 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 e.** performance
10256 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 . */.#define BIT
10257 56 45 43 5f 54 45 4c 45 4d 20 20 20 20 20 75 38 VEC_TELEM u8
10258 0a 2f 2a 20 53 69 7a 65 2c 20 69 6e 20 62 69 74 ./* Size, in bit
10259 73 2c 20 6f 66 20 74 68 65 20 62 69 74 6d 61 70 s, of the bitmap
1025a 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 23 64 65 element. */.#de
1025b 66 69 6e 65 20 42 49 54 56 45 43 5f 53 5a 45 4c fine BITVEC_SZEL
1025c 45 4d 20 20 20 20 38 0a 2f 2a 20 4e 75 6d 62 65 EM 8./* Numbe
1025d 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e r of elements in
1025e 20 61 20 62 69 74 6d 61 70 20 61 72 72 61 79 2e a bitmap array.
1025f 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 */.#define BITV
10260 45 43 5f 4e 45 4c 45 4d 20 20 20 20 20 28 42 49 EC_NELEM (BI
10261 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f TVEC_USIZE/sizeo
10262 66 28 42 49 54 56 45 43 5f 54 45 4c 45 4d 29 29 f(BITVEC_TELEM))
10263 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 ./* Number of bi
10264 74 73 20 69 6e 20 74 68 65 20 62 69 74 6d 61 70 ts in the bitmap
10265 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69 array. */.#defi
10266 6e 65 20 42 49 54 56 45 43 5f 4e 42 49 54 20 20 ne BITVEC_NBIT
10267 20 20 20 20 28 42 49 54 56 45 43 5f 4e 45 4c 45 (BITVEC_NELE
10268 4d 2a 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 29 M*BITVEC_SZELEM)
10269 0a 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 ../* Number of u
1026a 33 32 20 76 61 6c 75 65 73 20 69 6e 20 68 61 73 32 values in has
1026b 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 23 64 65 66 h table. */.#def
1026c 69 6e 65 20 42 49 54 56 45 43 5f 4e 49 4e 54 20 ine BITVEC_NINT
1026d 20 20 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 (BITVEC_USI
1026e 5a 45 2f 73 69 7a 65 6f 66 28 75 33 32 29 29 0a ZE/sizeof(u32)).
1026f 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 /* Maximum numbe
10270 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 r of entries in
10271 68 61 73 68 20 74 61 62 6c 65 20 62 65 66 6f 72 hash table befor
10272 65 20 0a 2a 2a 20 73 75 62 2d 64 69 76 69 64 69 e .** sub-dividi
10273 6e 67 20 61 6e 64 20 72 65 2d 68 61 73 68 69 6e ng and re-hashin
10274 67 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 g. */.#define BI
10275 54 56 45 43 5f 4d 58 48 41 53 48 20 20 20 20 28 TVEC_MXHASH (
10276 42 49 54 56 45 43 5f 4e 49 4e 54 2f 32 29 0a 2f BITVEC_NINT/2)./
10277 2a 20 48 61 73 68 69 6e 67 20 66 75 6e 63 74 69 * Hashing functi
10278 6f 6e 20 66 6f 72 20 74 68 65 20 61 48 61 73 68 on for the aHash
10279 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e representation.
1027a 0a 2a 2a 20 45 6d 70 69 72 69 63 61 6c 20 74 65 .** Empirical te
1027b 73 74 69 6e 67 20 73 68 6f 77 65 64 20 74 68 61 sting showed tha
1027c 74 20 74 68 65 20 2a 33 37 20 6d 75 6c 74 69 70 t the *37 multip
1027d 6c 69 65 72 20 0a 2a 2a 20 28 61 6e 20 61 72 62 lier .** (an arb
1027e 69 74 72 61 72 79 20 70 72 69 6d 65 29 69 6e 20 itrary prime)in
1027f 74 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f the hash functio
10280 6e 20 70 72 6f 76 69 64 65 64 20 0a 2a 2a 20 6e n provided .** n
10281 6f 20 66 65 77 65 72 20 63 6f 6c 6c 69 73 69 6f o fewer collisio
10282 6e 73 20 74 68 61 6e 20 74 68 65 20 6e 6f 2d 6f ns than the no-o
10283 70 20 2a 31 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 p *1. */.#define
10284 20 42 49 54 56 45 43 5f 48 41 53 48 28 58 29 20 BITVEC_HASH(X)
10285 20 20 28 28 28 58 29 2a 31 29 25 42 49 54 56 45 (((X)*1)%BITVE
10286 43 5f 4e 49 4e 54 29 0a 0a 23 64 65 66 69 6e 65 C_NINT)..#define
10287 20 42 49 54 56 45 43 5f 4e 50 54 52 20 20 20 20 BITVEC_NPTR
10288 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f (BITVEC_USIZE/
10289 73 69 7a 65 6f 66 28 42 69 74 76 65 63 20 2a 29 sizeof(Bitvec *)
1028a 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 62 69 74 6d ).../*.** A bitm
1028b 61 70 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 ap is an instanc
1028c 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
1028d 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ng structure..**
1028e 0a 2a 2a 20 54 68 69 73 20 62 69 74 6d 61 70 20 .** This bitmap
1028f 72 65 63 6f 72 64 73 20 74 68 65 20 65 78 69 73 records the exis
10290 74 61 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72 tance of zero or
10291 20 6d 6f 72 65 20 62 69 74 73 0a 2a 2a 20 77 69 more bits.** wi
10292 74 68 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 th values betwee
10293 6e 20 31 20 61 6e 64 20 69 53 69 7a 65 2c 20 69 n 1 and iSize, i
10294 6e 63 6c 75 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 nclusive..**.**
10295 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20 There are three
10296 70 6f 73 73 69 62 6c 65 20 72 65 70 72 65 73 65 possible represe
10297 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 ntations of the
10298 62 69 74 6d 61 70 2e 0a 2a 2a 20 49 66 20 69 53 bitmap..** If iS
10299 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 ize<=BITVEC_NBIT
1029a 2c 20 74 68 65 6e 20 42 69 74 76 65 63 2e 75 2e , then Bitvec.u.
1029b 61 42 69 74 6d 61 70 5b 5d 20 69 73 20 61 20 73 aBitmap[] is a s
1029c 74 72 61 69 67 68 74 0a 2a 2a 20 62 69 74 6d 61 traight.** bitma
1029d 70 2e 20 20 54 68 65 20 6c 65 61 73 74 20 73 69 p. The least si
1029e 67 6e 69 66 69 63 61 6e 74 20 62 69 74 20 69 73 gnificant bit is
1029f 20 62 69 74 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 bit 1..**.** If
102a0 20 69 53 69 7a 65 3e 42 49 54 56 45 43 5f 4e 42 iSize>BITVEC_NB
102a1 49 54 20 61 6e 64 20 69 44 69 76 69 73 6f 72 3d IT and iDivisor=
102a2 3d 30 20 74 68 65 6e 20 42 69 74 76 65 63 2e 75 =0 then Bitvec.u
102a3 2e 61 48 61 73 68 5b 5d 20 69 73 0a 2a 2a 20 61 .aHash[] is.** a
102a4 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 hash table that
102a5 20 77 69 6c 6c 20 68 6f 6c 64 20 75 70 20 74 6f will hold up to
102a6 20 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20 64 BITVEC_MXHASH d
102a7 69 73 74 69 6e 63 74 20 76 61 6c 75 65 73 2e 0a istinct values..
102a8 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c **.** Otherwise,
102a9 20 74 68 65 20 76 61 6c 75 65 20 69 20 69 73 20 the value i is
102aa 72 65 64 69 72 65 63 74 65 64 20 69 6e 74 6f 20 redirected into
102ab 6f 6e 65 20 6f 66 20 42 49 54 56 45 43 5f 4e 50 one of BITVEC_NP
102ac 54 52 0a 2a 2a 20 73 75 62 2d 62 69 74 6d 61 70 TR.** sub-bitmap
102ad 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 s pointed to by
102ae 42 69 74 76 65 63 2e 75 2e 61 70 53 75 62 5b 5d Bitvec.u.apSub[]
102af 2e 20 20 45 61 63 68 20 73 75 62 62 69 74 6d 61 . Each subbitma
102b0 70 0a 2a 2a 20 68 61 6e 64 6c 65 73 20 75 70 20 p.** handles up
102b1 74 6f 20 69 44 69 76 69 73 6f 72 20 73 65 70 61 to iDivisor sepa
102b2 72 61 74 65 20 76 61 6c 75 65 73 20 6f 66 20 69 rate values of i
102b3 2e 20 20 61 70 53 75 62 5b 30 5d 20 68 6f 6c 64 . apSub[0] hold
102b4 73 0a 2a 2a 20 76 61 6c 75 65 73 20 62 65 74 77 s.** values betw
102b5 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76 69 73 een 1 and iDivis
102b6 6f 72 2e 20 20 61 70 53 75 62 5b 31 5d 20 68 6f or. apSub[1] ho
102b7 6c 64 73 20 76 61 6c 75 65 73 20 62 65 74 77 65 lds values betwe
102b8 65 6e 0a 2a 2a 20 69 44 69 76 69 73 6f 72 2b 31 en.** iDivisor+1
102b9 20 61 6e 64 20 32 2a 69 44 69 76 69 73 6f 72 2e and 2*iDivisor.
102ba 20 20 61 70 53 75 62 5b 4e 5d 20 68 6f 6c 64 73 apSub[N] holds
102bb 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 0a values between.
102bc 2a 2a 20 4e 2a 69 44 69 76 69 73 6f 72 2b 31 20 ** N*iDivisor+1
102bd 61 6e 64 20 28 4e 2b 31 29 2a 69 44 69 76 69 73 and (N+1)*iDivis
102be 6f 72 2e 20 20 45 61 63 68 20 73 75 62 62 69 74 or. Each subbit
102bf 6d 61 70 20 69 73 20 6e 6f 72 6d 61 6c 69 7a 65 map is normalize
102c0 64 0a 2a 2a 20 74 6f 20 68 6f 6c 64 20 64 65 61 d.** to hold dea
102c1 6c 20 77 69 74 68 20 76 61 6c 75 65 73 20 62 65 l with values be
102c2 74 77 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76 tween 1 and iDiv
102c3 69 73 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 isor..*/.struct
102c4 42 69 74 76 65 63 20 7b 0a 20 20 75 33 32 20 69 Bitvec {. u32 i
102c5 53 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4d 61 Size; /* Ma
102c6 78 69 6d 75 6d 20 62 69 74 20 69 6e 64 65 78 2e ximum bit index.
102c7 20 20 4d 61 78 20 69 53 69 7a 65 20 69 73 20 34 Max iSize is 4
102c8 2c 32 39 34 2c 39 36 37 2c 32 39 36 2e 20 2a 2f ,294,967,296. */
102c9 0a 20 20 75 33 32 20 6e 53 65 74 3b 20 20 20 20 . u32 nSet;
102ca 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
102cb 62 69 74 73 20 74 68 61 74 20 61 72 65 20 73 65 bits that are se
102cc 74 20 2d 20 6f 6e 6c 79 20 76 61 6c 69 64 20 66 t - only valid f
102cd 6f 72 20 61 48 61 73 68 0a 20 20 20 20 20 20 20 or aHash.
102ce 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65 6c ** el
102cf 65 6d 65 6e 74 2e 20 20 4d 61 78 20 69 73 20 42 ement. Max is B
102d0 49 54 56 45 43 5f 4e 49 4e 54 2e 20 20 46 6f 72 ITVEC_NINT. For
102d1 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35 31 BITVEC_SZ of 51
102d2 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 2,.
102d3 20 20 20 20 20 2a 2a 20 74 68 69 73 20 77 6f 75 ** this wou
102d4 6c 64 20 62 65 20 31 32 35 2e 20 2a 2f 0a 20 20 ld be 125. */.
102d5 75 33 32 20 69 44 69 76 69 73 6f 72 3b 20 20 20 u32 iDivisor;
102d6 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 /* Number of bit
102d7 73 20 68 61 6e 64 6c 65 64 20 62 79 20 65 61 63 s handled by eac
102d8 68 20 61 70 53 75 62 5b 5d 20 65 6e 74 72 79 2e h apSub[] entry.
102d9 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
102da 20 20 20 20 20 20 2f 2a 20 53 68 6f 75 6c 64 20 /* Should
102db 3e 3d 30 20 66 6f 72 20 61 70 53 75 62 20 65 6c >=0 for apSub el
102dc 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 ement. */.
102dd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
102de 61 78 20 69 44 69 76 69 73 6f 72 20 69 73 20 6d ax iDivisor is m
102df 61 78 28 75 33 32 29 20 2f 20 42 49 54 56 45 43 ax(u32) / BITVEC
102e0 5f 4e 50 54 52 20 2b 20 31 2e 20 20 2a 2f 0a 20 _NPTR + 1. */.
102e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
102e2 20 2f 2a 20 46 6f 72 20 61 20 42 49 54 56 45 43 /* For a BITVEC
102e3 5f 53 5a 20 6f 66 20 35 31 32 2c 20 74 68 69 73 _SZ of 512, this
102e4 20 77 6f 75 6c 64 20 62 65 20 33 34 2c 33 35 39 would be 34,359
102e5 2c 37 33 39 2e 20 2a 2f 0a 20 20 75 6e 69 6f 6e ,739. */. union
102e6 20 7b 0a 20 20 20 20 42 49 54 56 45 43 5f 54 45 {. BITVEC_TE
102e7 4c 45 4d 20 61 42 69 74 6d 61 70 5b 42 49 54 56 LEM aBitmap[BITV
102e8 45 43 5f 4e 45 4c 45 4d 5d 3b 20 20 20 20 2f 2a EC_NELEM]; /*
102e9 20 42 69 74 6d 61 70 20 72 65 70 72 65 73 65 6e Bitmap represen
102ea 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 75 33 tation */. u3
102eb 32 20 61 48 61 73 68 5b 42 49 54 56 45 43 5f 4e 2 aHash[BITVEC_N
102ec 49 4e 54 5d 3b 20 20 20 20 20 20 2f 2a 20 48 61 INT]; /* Ha
102ed 73 68 20 74 61 62 6c 65 20 72 65 70 72 65 73 65 sh table represe
102ee 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 42 ntation */. B
102ef 69 74 76 65 63 20 2a 61 70 53 75 62 5b 42 49 54 itvec *apSub[BIT
102f0 56 45 43 5f 4e 50 54 52 5d 3b 20 20 2f 2a 20 52 VEC_NPTR]; /* R
102f1 65 63 75 72 73 69 76 65 20 72 65 70 72 65 73 65 ecursive represe
102f2 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 7d 20 75 ntation */. } u
102f3 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 ;.};../*.** Crea
102f4 74 65 20 61 20 6e 65 77 20 62 69 74 6d 61 70 20 te a new bitmap
102f5 6f 62 6a 65 63 74 20 61 62 6c 65 20 74 6f 20 68 object able to h
102f6 61 6e 64 6c 65 20 62 69 74 73 20 62 65 74 77 65 andle bits betwe
102f7 65 6e 20 30 20 61 6e 64 20 69 53 69 7a 65 2c 0a en 0 and iSize,.
102f8 2a 2a 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 52 ** inclusive. R
102f9 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
102fa 74 6f 20 74 68 65 20 6e 65 77 20 6f 62 6a 65 63 to the new objec
102fb 74 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 t. Return NULL
102fc 69 66 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 if .** malloc fa
102fd 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ils..*/.SQLITE_P
102fe 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a 73 RIVATE Bitvec *s
102ff 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 qlite3BitvecCrea
10300 74 65 28 75 33 32 20 69 53 69 7a 65 29 7b 0a 20 te(u32 iSize){.
10301 20 42 69 74 76 65 63 20 2a 70 3b 0a 20 20 61 73 Bitvec *p;. as
10302 73 65 72 74 28 20 73 69 7a 65 6f 66 28 2a 70 29 sert( sizeof(*p)
10303 3d 3d 42 49 54 56 45 43 5f 53 5a 20 29 3b 0a 20 ==BITVEC_SZ );.
10304 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c p = sqlite3Mall
10305 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a ocZero( sizeof(*
10306 70 29 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b p) );. if( p ){
10307 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 . p->iSize =
10308 69 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 iSize;. }. ret
10309 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
1030a 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 Check to see if
1030b 74 68 65 20 69 2d 74 68 20 62 69 74 20 69 73 20 the i-th bit is
1030c 73 65 74 2e 20 20 52 65 74 75 72 6e 20 74 72 75 set. Return tru
1030d 65 20 6f 72 20 66 61 6c 73 65 2e 0a 2a 2a 20 49 e or false..** I
1030e 66 20 70 20 69 73 20 4e 55 4c 4c 20 28 69 66 20 f p is NULL (if
1030f 74 68 65 20 62 69 74 6d 61 70 20 68 61 73 20 6e the bitmap has n
10310 6f 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 29 ot been created)
10311 20 6f 72 20 69 66 0a 2a 2a 20 69 20 69 73 20 6f or if.** i is o
10312 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 ut of range, the
10313 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a n return false..
10314 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
10315 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 E int sqlite3Bit
10316 76 65 63 54 65 73 74 28 42 69 74 76 65 63 20 2a vecTest(Bitvec *
10317 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 69 66 28 p, u32 i){. if(
10318 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 p==0 ) return 0
10319 3b 0a 20 20 69 66 28 20 69 3e 70 2d 3e 69 53 69 ;. if( i>p->iSi
1031a 7a 65 20 7c 7c 20 69 3d 3d 30 20 29 20 72 65 74 ze || i==0 ) ret
1031b 75 72 6e 20 30 3b 0a 20 20 69 2d 2d 3b 0a 20 20 urn 0;. i--;.
1031c 77 68 69 6c 65 28 20 70 2d 3e 69 44 69 76 69 73 while( p->iDivis
1031d 6f 72 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69 or ){. u32 bi
1031e 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f n = i/p->iDiviso
1031f 72 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e r;. i = i%p->
10320 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 70 20 iDivisor;. p
10321 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e = p->u.apSub[bin
10322 5d 3b 0a 20 20 20 20 69 66 20 28 21 70 29 20 7b ];. if (!p) {
10323 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b . return 0;
10324 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
10325 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 p->iSize<=BITVE
10326 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 72 65 C_NBIT ){. re
10327 74 75 72 6e 20 28 70 2d 3e 75 2e 61 42 69 74 6d turn (p->u.aBitm
10328 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c ap[i/BITVEC_SZEL
10329 45 4d 5d 20 26 20 28 31 3c 3c 28 69 26 28 42 49 EM] & (1<<(i&(BI
1032a 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 TVEC_SZELEM-1)))
1032b 29 21 3d 30 3b 0a 20 20 7d 20 65 6c 73 65 7b 0a )!=0;. } else{.
1032c 20 20 20 20 75 33 32 20 68 20 3d 20 42 49 54 56 u32 h = BITV
1032d 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b 0a 20 20 EC_HASH(i++);.
1032e 20 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 while( p->u.aH
1032f 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20 ash[h] ){.
10330 69 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 if( p->u.aHash[h
10331 5d 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 31 3b ]==i ) return 1;
10332 0a 20 20 20 20 20 20 68 20 3d 20 28 68 2b 31 29 . h = (h+1)
10333 20 25 20 42 49 54 56 45 43 5f 4e 49 4e 54 3b 0a % BITVEC_NINT;.
10334 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
10335 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 0;. }.}../*.**
10336 20 53 65 74 20 74 68 65 20 69 2d 74 68 20 62 69 Set the i-th bi
10337 74 2e 20 20 52 65 74 75 72 6e 20 30 20 6f 6e 20 t. Return 0 on
10338 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e 20 65 success and an e
10339 72 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20 rror code if.**
1033a 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 anything goes wr
1033b 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ong..**.** This
1033c 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 63 61 routine might ca
1033d 75 73 65 20 73 75 62 2d 62 69 74 6d 61 70 73 20 use sub-bitmaps
1033e 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e to be allocated.
1033f 20 20 46 61 69 6c 69 6e 67 0a 2a 2a 20 74 6f 20 Failing.** to
10340 67 65 74 20 74 68 65 20 6d 65 6d 6f 72 79 20 6e get the memory n
10341 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 eeded to hold th
10342 65 20 73 75 62 2d 62 69 74 6d 61 70 20 69 73 20 e sub-bitmap is
10343 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 74 68 61 74 the only.** that
10344 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67 20 77 69 can go wrong wi
10345 74 68 20 61 6e 20 69 6e 73 65 72 74 2c 20 61 73 th an insert, as
10346 73 75 6d 69 6e 67 20 70 20 61 6e 64 20 69 20 61 suming p and i a
10347 72 65 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 re valid..**.**
10348 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 The calling func
10349 74 69 6f 6e 20 6d 75 73 74 20 65 6e 73 75 72 65 tion must ensure
1034a 20 74 68 61 74 20 70 20 69 73 20 61 20 76 61 6c that p is a val
1034b 69 64 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 id Bitvec object
1034c 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68 65 .** and that the
1034d 20 76 61 6c 75 65 20 66 6f 72 20 22 69 22 20 69 value for "i" i
1034e 73 20 77 69 74 68 69 6e 20 72 61 6e 67 65 20 6f s within range o
1034f 66 20 74 68 65 20 42 69 74 76 65 63 20 6f 62 6a f the Bitvec obj
10350 65 63 74 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ect..** Otherwis
10351 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 e the behavior i
10352 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a s undefined..*/.
10353 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
10354 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 nt sqlite3Bitvec
10355 53 65 74 28 42 69 74 76 65 63 20 2a 70 2c 20 75 Set(Bitvec *p, u
10356 33 32 20 69 29 7b 0a 20 20 75 33 32 20 68 3b 0a 32 i){. u32 h;.
10357 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
10358 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
10359 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a assert( i>0 );.
1035a 20 20 61 73 73 65 72 74 28 20 69 3c 3d 70 2d 3e assert( i<=p->
1035b 69 53 69 7a 65 20 29 3b 0a 20 20 69 2d 2d 3b 0a iSize );. i--;.
1035c 20 20 77 68 69 6c 65 28 28 70 2d 3e 69 53 69 7a while((p->iSiz
1035d 65 20 3e 20 42 49 54 56 45 43 5f 4e 42 49 54 29 e > BITVEC_NBIT)
1035e 20 26 26 20 70 2d 3e 69 44 69 76 69 73 6f 72 29 && p->iDivisor)
1035f 20 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d {. u32 bin =
10360 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a i/p->iDivisor;.
10361 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 i = i%p->iDi
10362 76 69 73 6f 72 3b 0a 20 20 20 20 69 66 28 20 70 visor;. if( p
10363 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d ->u.apSub[bin]==
10364 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 75 2e 0 ){. p->u.
10365 61 70 53 75 62 5b 62 69 6e 5d 20 3d 20 73 71 6c apSub[bin] = sql
10366 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 ite3BitvecCreate
10367 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 3b ( p->iDivisor );
10368 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e . if( p->u.
10369 61 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 20 apSub[bin]==0 )
1036a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
1036b 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 MEM;. }. p
1036c 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 = p->u.apSub[bi
1036d 6e 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d n];. }. if( p-
1036e 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e >iSize<=BITVEC_N
1036f 42 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e BIT ){. p->u.
10370 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 aBitmap[i/BITVEC
10371 5f 53 5a 45 4c 45 4d 5d 20 7c 3d 20 31 20 3c 3c _SZELEM] |= 1 <<
10372 20 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c (i&(BITVEC_SZEL
10373 45 4d 2d 31 29 29 3b 0a 20 20 20 20 72 65 74 75 EM-1));. retu
10374 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
10375 7d 0a 20 20 68 20 3d 20 42 49 54 56 45 43 5f 48 }. h = BITVEC_H
10376 41 53 48 28 69 2b 2b 29 3b 0a 20 20 2f 2a 20 69 ASH(i++);. /* i
10377 66 20 74 68 65 72 65 20 77 61 73 6e 27 74 20 61 f there wasn't a
10378 20 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 2c hash collision,
10379 20 61 6e 64 20 74 68 69 73 20 64 6f 65 73 6e 27 and this doesn'
1037a 74 20 2a 2f 0a 20 20 2f 2a 20 63 6f 6d 70 6c 65 t */. /* comple
1037b 74 65 6c 79 20 66 69 6c 6c 20 74 68 65 20 68 61 tely fill the ha
1037c 73 68 2c 20 74 68 65 6e 20 6a 75 73 74 20 61 64 sh, then just ad
1037d 64 20 69 74 20 77 69 74 68 6f 75 74 20 2a 2f 0a d it without */.
1037e 20 20 2f 2a 20 77 6f 72 72 69 6e 67 20 61 62 6f /* worring abo
1037f 75 74 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20 ut sub-dividing
10380 61 6e 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 and re-hashing.
10381 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 75 2e 61 */. if( !p->u.a
10382 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 69 Hash[h] ){. i
10383 66 20 28 70 2d 3e 6e 53 65 74 3c 28 42 49 54 56 f (p->nSet<(BITV
10384 45 43 5f 4e 49 4e 54 2d 31 29 29 20 7b 0a 20 20 EC_NINT-1)) {.
10385 20 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f goto bitvec_
10386 73 65 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 20 65 set_end;. } e
10387 6c 73 65 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f lse {. goto
10388 20 62 69 74 76 65 63 5f 73 65 74 5f 72 65 68 61 bitvec_set_reha
10389 73 68 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 sh;. }. }.
1038a 2f 2a 20 74 68 65 72 65 20 77 61 73 20 61 20 63 /* there was a c
1038b 6f 6c 6c 69 73 69 6f 6e 2c 20 63 68 65 63 6b 20 ollision, check
1038c 74 6f 20 73 65 65 20 69 66 20 69 74 27 73 20 61 to see if it's a
1038d 6c 72 65 61 64 79 20 2a 2f 0a 20 20 2f 2a 20 69 lready */. /* i
1038e 6e 20 68 61 73 68 2c 20 69 66 20 6e 6f 74 2c 20 n hash, if not,
1038f 74 72 79 20 74 6f 20 66 69 6e 64 20 61 20 73 70 try to find a sp
10390 6f 74 20 66 6f 72 20 69 74 20 2a 2f 0a 20 20 64 ot for it */. d
10391 6f 20 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 o {. if( p->u
10392 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 .aHash[h]==i ) r
10393 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
10394 0a 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 69 66 . h++;. if
10395 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 ( h>=BITVEC_NINT
10396 20 29 20 68 20 3d 20 30 3b 0a 20 20 7d 20 77 68 ) h = 0;. } wh
10397 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b ile( p->u.aHash[
10398 68 5d 20 29 3b 0a 20 20 2f 2a 20 77 65 20 64 69 h] );. /* we di
10399 64 6e 27 74 20 66 69 6e 64 20 69 74 20 69 6e 20 dn't find it in
1039a 74 68 65 20 68 61 73 68 2e 20 20 68 20 70 6f 69 the hash. h poi
1039b 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 nts to the first
1039c 20 2a 2f 0a 20 20 2f 2a 20 61 76 61 69 6c 61 62 */. /* availab
1039d 6c 65 20 66 72 65 65 20 73 70 6f 74 2e 20 63 68 le free spot. ch
1039e 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 eck to see if th
1039f 69 73 20 69 73 20 67 6f 69 6e 67 20 74 6f 20 2a is is going to *
103a0 2f 0a 20 20 2f 2a 20 6d 61 6b 65 20 6f 75 72 20 /. /* make our
103a1 68 61 73 68 20 74 6f 6f 20 22 66 75 6c 6c 22 2e hash too "full".
103a2 20 20 2a 2f 0a 62 69 74 76 65 63 5f 73 65 74 5f */.bitvec_set_
103a3 72 65 68 61 73 68 3a 0a 20 20 69 66 28 20 70 2d rehash:. if( p-
103a4 3e 6e 53 65 74 3e 3d 42 49 54 56 45 43 5f 4d 58 >nSet>=BITVEC_MX
103a5 48 41 53 48 20 29 7b 0a 20 20 20 20 75 6e 73 69 HASH ){. unsi
103a6 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20 20 gned int j;.
103a7 69 6e 74 20 72 63 3b 0a 20 20 20 20 75 33 32 20 int rc;. u32
103a8 2a 61 69 56 61 6c 75 65 73 20 3d 20 73 71 6c 69 *aiValues = sqli
103a9 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 te3StackAllocRaw
103aa 28 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e (0, sizeof(p->u.
103ab 61 48 61 73 68 29 29 3b 0a 20 20 20 20 69 66 28 aHash));. if(
103ac 20 61 69 56 61 6c 75 65 73 3d 3d 30 20 29 7b 0a aiValues==0 ){.
103ad 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
103ae 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
103af 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 else{. memc
103b0 70 79 28 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e py(aiValues, p->
103b1 75 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 28 u.aHash, sizeof(
103b2 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 p->u.aHash));.
103b3 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e 75 2e memset(p->u.
103b4 61 70 53 75 62 2c 20 30 2c 20 73 69 7a 65 6f 66 apSub, 0, sizeof
103b5 28 70 2d 3e 75 2e 61 70 53 75 62 29 29 3b 0a 20 (p->u.apSub));.
103b6 20 20 20 20 20 70 2d 3e 69 44 69 76 69 73 6f 72 p->iDivisor
103b7 20 3d 20 28 70 2d 3e 69 53 69 7a 65 20 2b 20 42 = (p->iSize + B
103b8 49 54 56 45 43 5f 4e 50 54 52 20 2d 20 31 29 2f ITVEC_NPTR - 1)/
103b9 42 49 54 56 45 43 5f 4e 50 54 52 3b 0a 20 20 20 BITVEC_NPTR;.
103ba 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
103bb 69 74 76 65 63 53 65 74 28 70 2c 20 69 29 3b 0a itvecSet(p, i);.
103bc 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a for(j=0; j
103bd 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b <BITVEC_NINT; j+
103be 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 +){. if(
103bf 61 69 56 61 6c 75 65 73 5b 6a 5d 20 29 20 72 63 aiValues[j] ) rc
103c0 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 |= sqlite3Bitve
103c1 63 53 65 74 28 70 2c 20 61 69 56 61 6c 75 65 73 cSet(p, aiValues
103c2 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 [j]);. }.
103c3 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b sqlite3Stack
103c4 46 72 65 65 28 30 2c 20 61 69 56 61 6c 75 65 73 Free(0, aiValues
103c5 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
103c6 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 62 69 rc;. }. }.bi
103c7 74 76 65 63 5f 73 65 74 5f 65 6e 64 3a 0a 20 20 tvec_set_end:.
103c8 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 70 2d 3e p->nSet++;. p->
103c9 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 3b 0a u.aHash[h] = i;.
103ca 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
103cb 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 OK;.}../*.** Cle
103cc 61 72 20 74 68 65 20 69 2d 74 68 20 62 69 74 2e ar the i-th bit.
103cd 0a 2a 2a 0a 2a 2a 20 70 42 75 66 20 6d 75 73 74 .**.** pBuf must
103ce 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f be a pointer to
103cf 20 61 74 20 6c 65 61 73 74 20 42 49 54 56 45 43 at least BITVEC
103d0 5f 53 5a 20 62 79 74 65 73 20 6f 66 20 74 65 6d _SZ bytes of tem
103d1 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 0a 2a porary storage.*
103d2 2a 20 74 68 61 74 20 42 69 74 76 65 63 43 6c 65 * that BitvecCle
103d3 61 72 20 63 61 6e 20 75 73 65 20 74 6f 20 72 65 ar can use to re
103d4 62 75 69 6c 74 20 69 74 73 20 68 61 73 68 20 74 built its hash t
103d5 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f able..*/.SQLITE_
103d6 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
103d7 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 28 ite3BitvecClear(
103d8 42 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 Bitvec *p, u32 i
103d9 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 , void *pBuf){.
103da 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
103db 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e rn;. assert( i>
103dc 30 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 0 );. i--;. wh
103dd 69 6c 65 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 ile( p->iDivisor
103de 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20 ){. u32 bin
103df 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b = i/p->iDivisor;
103e0 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 . i = i%p->iD
103e1 69 76 69 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20 ivisor;. p =
103e2 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b p->u.apSub[bin];
103e3 0a 20 20 20 20 69 66 20 28 21 70 29 20 7b 0a 20 . if (!p) {.
103e4 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
103e5 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e }. }. if( p->
103e6 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 iSize<=BITVEC_NB
103e7 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 IT ){. p->u.a
103e8 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f Bitmap[i/BITVEC_
103e9 53 5a 45 4c 45 4d 5d 20 26 3d 20 7e 28 31 20 3c SZELEM] &= ~(1 <
103ea 3c 20 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 < (i&(BITVEC_SZE
103eb 4c 45 4d 2d 31 29 29 29 3b 0a 20 20 7d 65 6c 73 LEM-1)));. }els
103ec 65 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 e{. unsigned
103ed 69 6e 74 20 6a 3b 0a 20 20 20 20 75 33 32 20 2a int j;. u32 *
103ee 61 69 56 61 6c 75 65 73 20 3d 20 70 42 75 66 3b aiValues = pBuf;
103ef 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61 . memcpy(aiVa
103f0 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 lues, p->u.aHash
103f1 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48 , sizeof(p->u.aH
103f2 61 73 68 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 ash));. memse
103f3 74 28 70 2d 3e 75 2e 61 48 61 73 68 2c 20 30 2c t(p->u.aHash, 0,
103f4 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48 61 sizeof(p->u.aHa
103f5 73 68 29 29 3b 0a 20 20 20 20 70 2d 3e 6e 53 65 sh));. p->nSe
103f6 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 6a t = 0;. for(j
103f7 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e =0; j<BITVEC_NIN
103f8 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 T; j++){. i
103f9 66 28 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 26 f( aiValues[j] &
103fa 26 20 61 69 56 61 6c 75 65 73 5b 6a 5d 21 3d 28 & aiValues[j]!=(
103fb 69 2b 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 i+1) ){.
103fc 75 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48 u32 h = BITVEC_H
103fd 41 53 48 28 61 69 56 61 6c 75 65 73 5b 6a 5d 2d ASH(aiValues[j]-
103fe 31 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 1);. p->n
103ff 53 65 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 Set++;. w
10400 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 hile( p->u.aHash
10401 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 [h] ){.
10402 20 68 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 h++;.
10403 69 66 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 if( h>=BITVEC_NI
10404 4e 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 20 NT ) h = 0;.
10405 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 2d }. p-
10406 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 61 69 >u.aHash[h] = ai
10407 56 61 6c 75 65 73 5b 6a 5d 3b 0a 20 20 20 20 20 Values[j];.
10408 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a }. }. }.}..
10409 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 /*.** Destroy a
1040a 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 2e 20 20 bitmap object.
1040b 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f Reclaim all memo
1040c 72 79 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 ry used..*/.SQLI
1040d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1040e 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 sqlite3BitvecDes
1040f 74 72 6f 79 28 42 69 74 76 65 63 20 2a 70 29 7b troy(Bitvec *p){
10410 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
10411 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 69 turn;. if( p->i
10412 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 Divisor ){. u
10413 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 nsigned int i;.
10414 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 42 49 for(i=0; i<BI
10415 54 56 45 43 5f 4e 50 54 52 3b 20 69 2b 2b 29 7b TVEC_NPTR; i++){
10416 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 . sqlite3Bi
10417 74 76 65 63 44 65 73 74 72 6f 79 28 70 2d 3e 75 tvecDestroy(p->u
10418 2e 61 70 53 75 62 5b 69 5d 29 3b 0a 20 20 20 20 .apSub[i]);.
10419 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f }. }. sqlite3_
1041a 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a free(p);.}../*.*
1041b 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c * Return the val
1041c 75 65 20 6f 66 20 74 68 65 20 69 53 69 7a 65 20 ue of the iSize
1041d 70 61 72 61 6d 65 74 65 72 20 73 70 65 63 69 66 parameter specif
1041e 69 65 64 20 77 68 65 6e 20 42 69 74 76 65 63 20 ied when Bitvec
1041f 2a 70 0a 2a 2a 20 77 61 73 20 63 72 65 61 74 65 *p.** was create
10420 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
10421 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 VATE u32 sqlite3
10422 42 69 74 76 65 63 53 69 7a 65 28 42 69 74 76 65 BitvecSize(Bitve
10423 63 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 c *p){. return
10424 70 2d 3e 69 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 p->iSize;.}..#if
10425 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
10426 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a _BUILTIN_TEST./*
10427 0a 2a 2a 20 4c 65 74 20 56 5b 5d 20 62 65 20 61 .** Let V[] be a
10428 6e 20 61 72 72 61 79 20 6f 66 20 75 6e 73 69 67 n array of unsig
10429 6e 65 64 20 63 68 61 72 61 63 74 65 72 73 20 73 ned characters s
1042a 75 66 66 69 63 69 65 6e 74 20 74 6f 20 68 6f 6c ufficient to hol
1042b 64 0a 2a 2a 20 75 70 20 74 6f 20 4e 20 62 69 74 d.** up to N bit
1042c 73 2e 20 20 4c 65 74 20 49 20 62 65 20 61 6e 20 s. Let I be an
1042d 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 integer between
1042e 30 20 61 6e 64 20 4e 2e 20 20 30 3c 3d 49 3c 4e 0 and N. 0<=I<N
1042f 2e 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20 66 6f ..** Then the fo
10430 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 63 llowing macros c
10431 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 an be used to se
10432 74 2c 20 63 6c 65 61 72 2c 20 6f 72 20 74 65 73 t, clear, or tes
10433 74 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 t.** individual
10434 62 69 74 73 20 77 69 74 68 69 6e 20 56 2e 0a 2a bits within V..*
10435 2f 0a 23 64 65 66 69 6e 65 20 53 45 54 42 49 54 /.#define SETBIT
10436 28 56 2c 49 29 20 20 20 20 20 20 56 5b 49 3e 3e (V,I) V[I>>
10437 33 5d 20 7c 3d 20 28 31 3c 3c 28 49 26 37 29 29 3] |= (1<<(I&7))
10438 0a 23 64 65 66 69 6e 65 20 43 4c 45 41 52 42 49 .#define CLEARBI
10439 54 28 56 2c 49 29 20 20 20 20 56 5b 49 3e 3e 33 T(V,I) V[I>>3
1043a 5d 20 26 3d 20 7e 28 31 3c 3c 28 49 26 37 29 29 ] &= ~(1<<(I&7))
1043b 0a 23 64 65 66 69 6e 65 20 54 45 53 54 42 49 54 .#define TESTBIT
1043c 28 56 2c 49 29 20 20 20 20 20 28 56 5b 49 3e 3e (V,I) (V[I>>
1043d 33 5d 26 28 31 3c 3c 28 49 26 37 29 29 29 21 3d 3]&(1<<(I&7)))!=
1043e 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 0../*.** This ro
1043f 75 74 69 6e 65 20 72 75 6e 73 20 61 6e 20 65 78 utine runs an ex
10440 74 65 6e 73 69 76 65 20 74 65 73 74 20 6f 66 20 tensive test of
10441 74 68 65 20 42 69 74 76 65 63 20 63 6f 64 65 2e the Bitvec code.
10442 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 .**.** The input
10443 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 is an array of
10444 69 6e 74 65 67 65 72 73 20 74 68 61 74 20 61 63 integers that ac
10445 74 73 20 61 73 20 61 20 70 72 6f 67 72 61 6d 0a ts as a program.
10446 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20 42 ** to test the B
10447 69 74 76 65 63 2e 20 20 54 68 65 20 69 6e 74 65 itvec. The inte
10448 67 65 72 73 20 61 72 65 20 6f 70 63 6f 64 65 73 gers are opcodes
10449 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20 followed.** by
1044a 30 2c 20 31 2c 20 6f 72 20 33 20 6f 70 65 72 61 0, 1, or 3 opera
1044b 6e 64 73 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f nds, depending o
1044c 6e 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 20 41 n the opcode. A
1044d 6e 6f 74 68 65 72 0a 2a 2a 20 6f 70 63 6f 64 65 nother.** opcode
1044e 20 66 6f 6c 6c 6f 77 73 20 69 6d 6d 65 64 69 61 follows immedia
1044f 74 65 6c 79 20 61 66 74 65 72 20 74 68 65 20 6c tely after the l
10450 61 73 74 20 6f 70 65 72 61 6e 64 2e 0a 2a 2a 0a ast operand..**.
10451 2a 2a 20 54 68 65 72 65 20 61 72 65 20 36 20 6f ** There are 6 o
10452 70 63 6f 64 65 73 20 6e 75 6d 62 65 72 65 64 20 pcodes numbered
10453 66 72 6f 6d 20 30 20 74 68 72 6f 75 67 68 20 35 from 0 through 5
10454 2e 20 20 30 20 69 73 20 74 68 65 0a 2a 2a 20 22 . 0 is the.** "
10455 68 61 6c 74 22 20 6f 70 63 6f 64 65 20 61 6e 64 halt" opcode and
10456 20 63 61 75 73 65 73 20 74 68 65 20 74 65 73 74 causes the test
10457 20 74 6f 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 to end..**.**
10458 20 20 30 20 20 20 20 20 20 20 20 20 20 48 61 6c 0 Hal
10459 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 t and return the
1045a 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 number of error
1045b 73 0a 2a 2a 20 20 20 20 31 20 4e 20 53 20 58 20 s.** 1 N S X
1045c 20 20 20 53 65 74 20 4e 20 62 69 74 73 20 62 65 Set N bits be
1045d 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 20 61 ginning with S a
1045e 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 nd incrementing
1045f 62 79 20 58 0a 2a 2a 20 20 20 20 32 20 4e 20 53 by X.** 2 N S
10460 20 58 20 20 20 20 43 6c 65 61 72 20 4e 20 62 69 X Clear N bi
10461 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 ts beginning wit
10462 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e h S and incremen
10463 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 20 20 ting by X.**
10464 33 20 4e 20 20 20 20 20 20 20 20 53 65 74 20 4e 3 N Set N
10465 20 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e randomly chosen
10466 20 62 69 74 73 0a 2a 2a 20 20 20 20 34 20 4e 20 bits.** 4 N
10467 20 20 20 20 20 20 20 43 6c 65 61 72 20 4e 20 72 Clear N r
10468 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 andomly chosen b
10469 69 74 73 0a 2a 2a 20 20 20 20 35 20 4e 20 53 20 its.** 5 N S
1046a 58 20 20 20 20 53 65 74 20 4e 20 62 69 74 73 20 X Set N bits
1046b 66 72 6f 6d 20 53 20 69 6e 63 72 65 6d 65 6e 74 from S increment
1046c 20 58 20 69 6e 20 61 72 72 61 79 20 6f 6e 6c 79 X in array only
1046d 2c 20 6e 6f 74 20 69 6e 20 62 69 74 76 65 63 0a , not in bitvec.
1046e 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 63 6f 64 65 **.** The opcode
1046f 73 20 31 20 74 68 72 6f 75 67 68 20 34 20 70 65 s 1 through 4 pe
10470 72 66 6f 72 6d 20 73 65 74 20 61 6e 64 20 63 6c rform set and cl
10471 65 61 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 ear operations a
10472 72 65 20 70 65 72 66 6f 72 6d 65 64 0a 2a 2a 20 re performed.**
10473 6f 6e 20 62 6f 74 68 20 61 20 42 69 74 76 65 63 on both a Bitvec
10474 20 6f 62 6a 65 63 74 20 61 6e 64 20 6f 6e 20 61 object and on a
10475 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f 66 linear array of
10476 20 62 69 74 73 20 6f 62 74 61 69 6e 65 64 20 66 bits obtained f
10477 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 4f rom malloc..** O
10478 70 63 6f 64 65 20 35 20 77 6f 72 6b 73 20 6f 6e pcode 5 works on
10479 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61 the linear arra
1047a 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 6f 6e 20 74 y only, not on t
1047b 68 65 20 42 69 74 76 65 63 2e 0a 2a 2a 20 4f 70 he Bitvec..** Op
1047c 63 6f 64 65 20 35 20 69 73 20 75 73 65 64 20 74 code 5 is used t
1047d 6f 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20 69 o deliberately i
1047e 6e 64 75 63 65 20 61 20 66 61 75 6c 74 20 69 6e nduce a fault in
1047f 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 6f 6e order to.** con
10480 66 69 72 6d 20 74 68 61 74 20 65 72 72 6f 72 20 firm that error
10481 64 65 74 65 63 74 69 6f 6e 20 77 6f 72 6b 73 2e detection works.
10482 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 63 6f .**.** At the co
10483 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 74 68 65 20 nclusion of the
10484 74 65 73 74 20 74 68 65 20 6c 69 6e 65 61 72 20 test the linear
10485 61 72 72 61 79 20 69 73 20 63 6f 6d 70 61 72 65 array is compare
10486 64 0a 2a 2a 20 61 67 61 69 6e 73 74 20 74 68 65 d.** against the
10487 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 Bitvec object.
10488 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e If there are an
10489 79 20 64 69 66 66 65 72 65 6e 63 65 73 2c 0a 2a y differences,.*
1048a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 * an error is re
1048b 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 65 79 turned. If they
1048c 20 61 72 65 20 74 68 65 20 73 61 6d 65 2c 20 7a are the same, z
1048d 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e ero is returned.
1048e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f .**.** If a memo
1048f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 ry allocation er
10490 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 ror occurs, retu
10491 72 6e 20 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 rn -1..*/.SQLITE
10492 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
10493 69 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 69 ite3BitvecBuilti
10494 6e 54 65 73 74 28 69 6e 74 20 73 7a 2c 20 69 6e nTest(int sz, in
10495 74 20 2a 61 4f 70 29 7b 0a 20 20 42 69 74 76 65 t *aOp){. Bitve
10496 63 20 2a 70 42 69 74 76 65 63 20 3d 20 30 3b 0a c *pBitvec = 0;.
10497 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
10498 2a 70 56 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 *pV = 0;. int r
10499 63 20 3d 20 2d 31 3b 0a 20 20 69 6e 74 20 69 2c c = -1;. int i,
1049a 20 6e 78 2c 20 70 63 2c 20 6f 70 3b 0a 20 20 76 nx, pc, op;. v
1049b 6f 69 64 20 2a 70 54 6d 70 53 70 61 63 65 3b 0a oid *pTmpSpace;.
1049c 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 . /* Allocate t
1049d 68 65 20 42 69 74 76 65 63 20 74 6f 20 62 65 20 he Bitvec to be
1049e 74 65 73 74 65 64 20 61 6e 64 20 61 20 6c 69 6e tested and a lin
1049f 65 61 72 20 61 72 72 61 79 20 6f 66 0a 20 20 2a ear array of. *
104a0 2a 20 62 69 74 73 20 74 6f 20 61 63 74 20 61 73 * bits to act as
104a1 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 2a the reference *
104a2 2f 0a 20 20 70 42 69 74 76 65 63 20 3d 20 73 71 /. pBitvec = sq
104a3 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 lite3BitvecCreat
104a4 65 28 20 73 7a 20 29 3b 0a 20 20 70 56 20 3d 20 e( sz );. pV =
104a5 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 sqlite3_malloc(
104a6 28 73 7a 2b 37 29 2f 38 20 2b 20 31 20 29 3b 0a (sz+7)/8 + 1 );.
104a7 20 20 70 54 6d 70 53 70 61 63 65 20 3d 20 73 71 pTmpSpace = sq
104a8 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 42 49 54 lite3_malloc(BIT
104a9 56 45 43 5f 53 5a 29 3b 0a 20 20 69 66 28 20 70 VEC_SZ);. if( p
104aa 42 69 74 76 65 63 3d 3d 30 20 7c 7c 20 70 56 3d Bitvec==0 || pV=
104ab 3d 30 20 7c 7c 20 70 54 6d 70 53 70 61 63 65 3d =0 || pTmpSpace=
104ac 3d 30 20 20 29 20 67 6f 74 6f 20 62 69 74 76 65 =0 ) goto bitve
104ad 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d 73 65 74 28 c_end;. memset(
104ae 70 56 2c 20 30 2c 20 28 73 7a 2b 37 29 2f 38 20 pV, 0, (sz+7)/8
104af 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 4e 55 4c 4c + 1);.. /* NULL
104b0 20 70 42 69 74 76 65 63 20 74 65 73 74 73 20 2a pBitvec tests *
104b1 2f 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 /. sqlite3Bitve
104b2 63 53 65 74 28 30 2c 20 31 29 3b 0a 20 20 73 71 cSet(0, 1);. sq
104b3 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 lite3BitvecClear
104b4 28 30 2c 20 31 2c 20 70 54 6d 70 53 70 61 63 65 (0, 1, pTmpSpace
104b5 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65 );.. /* Run the
104b6 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 70 63 program */. pc
104b7 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 28 = 0;. while( (
104b8 6f 70 20 3d 20 61 4f 70 5b 70 63 5d 29 21 3d 30 op = aOp[pc])!=0
104b9 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20 ){. switch(
104ba 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 op ){. case
104bb 20 31 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 1:. case 2
104bc 3a 0a 20 20 20 20 20 20 63 61 73 65 20 35 3a 20 :. case 5:
104bd 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20 34 {. nx = 4
104be 3b 0a 20 20 20 20 20 20 20 20 69 20 3d 20 61 4f ;. i = aO
104bf 70 5b 70 63 2b 32 5d 20 2d 20 31 3b 0a 20 20 20 p[pc+2] - 1;.
104c0 20 20 20 20 20 61 4f 70 5b 70 63 2b 32 5d 20 2b aOp[pc+2] +
104c1 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 0a 20 20 20 = aOp[pc+3];.
104c2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
104c3 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 33 }. case 3
104c4 3a 0a 20 20 20 20 20 20 63 61 73 65 20 34 3a 20 :. case 4:
104c5 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 . default:
104c6 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20 32 {. nx = 2
104c7 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
104c8 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 3_randomness(siz
104c9 65 6f 66 28 69 29 2c 20 26 69 29 3b 0a 20 20 20 eof(i), &i);.
104ca 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
104cb 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
104cc 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31 5d 29 20 ( (--aOp[pc+1])
104cd 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b 0a 20 20 > 0 ) nx = 0;.
104ce 20 20 70 63 20 2b 3d 20 6e 78 3b 0a 20 20 20 20 pc += nx;.
104cf 69 20 3d 20 28 69 20 26 20 30 78 37 66 66 66 66 i = (i & 0x7ffff
104d0 66 66 66 29 25 73 7a 3b 0a 20 20 20 20 69 66 28 fff)%sz;. if(
104d1 20 28 6f 70 20 26 20 31 29 21 3d 30 20 29 7b 0a (op & 1)!=0 ){.
104d2 20 20 20 20 20 20 53 45 54 42 49 54 28 70 56 2c SETBIT(pV,
104d3 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 69 (i+1));. i
104d4 66 28 20 6f 70 21 3d 35 20 29 7b 0a 20 20 20 20 f( op!=5 ){.
104d5 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 if( sqlite3B
104d6 69 74 76 65 63 53 65 74 28 70 42 69 74 76 65 63 itvecSet(pBitvec
104d7 2c 20 69 2b 31 29 20 29 20 67 6f 74 6f 20 62 69 , i+1) ) goto bi
104d8 74 76 65 63 5f 65 6e 64 3b 0a 20 20 20 20 20 20 tvec_end;.
104d9 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
104da 20 20 20 43 4c 45 41 52 42 49 54 28 70 56 2c 20 CLEARBIT(pV,
104db 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 73 71 (i+1));. sq
104dc 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 lite3BitvecClear
104dd 28 70 42 69 74 76 65 63 2c 20 69 2b 31 2c 20 70 (pBitvec, i+1, p
104de 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 7d TmpSpace);. }
104df 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20 . }.. /* Test
104e0 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 to make sure the
104e1 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 65 78 linear array ex
104e2 61 63 74 6c 79 20 6d 61 74 63 68 65 73 20 74 68 actly matches th
104e3 65 0a 20 20 2a 2a 20 42 69 74 76 65 63 20 6f 62 e. ** Bitvec ob
104e4 6a 65 63 74 2e 20 20 53 74 61 72 74 20 77 69 74 ject. Start wit
104e5 68 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e h the assumption
104e6 20 74 68 61 74 20 74 68 65 79 20 64 6f 0a 20 20 that they do.
104e7 2a 2a 20 6d 61 74 63 68 20 28 72 63 3d 3d 30 29 ** match (rc==0)
104e8 2e 20 20 43 68 61 6e 67 65 20 72 63 20 74 6f 20 . Change rc to
104e9 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 64 69 non-zero if a di
104ea 73 63 72 65 70 61 6e 63 79 0a 20 20 2a 2a 20 69 screpancy. ** i
104eb 73 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20 s found.. */.
104ec 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 rc = sqlite3Bitv
104ed 65 63 54 65 73 74 28 30 2c 30 29 20 2b 20 73 71 ecTest(0,0) + sq
104ee 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 lite3BitvecTest(
104ef 70 42 69 74 76 65 63 2c 20 73 7a 2b 31 29 0a 20 pBitvec, sz+1).
104f0 20 20 20 20 20 20 20 20 20 2b 20 73 71 6c 69 74 + sqlit
104f1 65 33 42 69 74 76 65 63 54 65 73 74 28 70 42 69 e3BitvecTest(pBi
104f2 74 76 65 63 2c 20 30 29 0a 20 20 20 20 20 20 20 tvec, 0).
104f3 20 20 20 2b 20 28 73 71 6c 69 74 65 33 42 69 74 + (sqlite3Bit
104f4 76 65 63 53 69 7a 65 28 70 42 69 74 76 65 63 29 vecSize(pBitvec)
104f5 20 2d 20 73 7a 29 3b 0a 20 20 66 6f 72 28 69 3d - sz);. for(i=
104f6 31 3b 20 69 3c 3d 73 7a 3b 20 69 2b 2b 29 7b 0a 1; i<=sz; i++){.
104f7 20 20 20 20 69 66 28 20 20 28 54 45 53 54 42 49 if( (TESTBI
104f8 54 28 70 56 2c 69 29 29 21 3d 73 71 6c 69 74 65 T(pV,i))!=sqlite
104f9 33 42 69 74 76 65 63 54 65 73 74 28 70 42 69 74 3BitvecTest(pBit
104fa 76 65 63 2c 69 29 20 29 7b 0a 20 20 20 20 20 20 vec,i) ){.
104fb 72 63 20 3d 20 69 3b 0a 20 20 20 20 20 20 62 72 rc = i;. br
104fc 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a eak;. }. }..
104fd 20 20 2f 2a 20 46 72 65 65 20 61 6c 6c 6f 63 61 /* Free alloca
104fe 74 65 64 20 73 74 72 75 63 74 75 72 65 20 2a 2f ted structure */
104ff 0a 62 69 74 76 65 63 5f 65 6e 64 3a 0a 20 20 73 .bitvec_end:. s
10500 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6d 70 qlite3_free(pTmp
10501 53 70 61 63 65 29 3b 0a 20 20 73 71 6c 69 74 65 Space);. sqlite
10502 33 5f 66 72 65 65 28 70 56 29 3b 0a 20 20 73 71 3_free(pV);. sq
10503 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 lite3BitvecDestr
10504 6f 79 28 70 42 69 74 76 65 63 29 3b 0a 20 20 72 oy(pBitvec);. r
10505 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 eturn rc;.}.#end
10506 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
10507 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a T_BUILTIN_TEST *
10508 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
10509 2a 2a 20 45 6e 64 20 6f 66 20 62 69 74 76 65 63 ** End of bitvec
1050a 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1050b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1050c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1050d 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
1050e 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 63 ** Begin file pc
1050f 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ache.c *********
10510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10511 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10512 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 */./*.** 2008 Au
10513 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54 68 gust 05.**.** Th
10514 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
10515 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
10516 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
10517 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
10518 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
10519 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
1051a 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
1051b 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
1051c 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1051d 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
1051e 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
1051f 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
10520 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
10521 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
10522 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
10523 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
10524 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
10525 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10526 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10527 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10528 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10529 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
1052a 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 ile implements t
1052b 68 61 74 20 70 61 67 65 20 63 61 63 68 65 2e 0a hat page cache..
1052c 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 **.** @(#) $Id:
1052d 70 63 61 63 68 65 2e 63 2c 76 20 31 2e 34 37 20 pcache.c,v 1.47
1052e 32 30 30 39 2f 30 37 2f 32 35 20 31 31 3a 34 36 2009/07/25 11:46
1052f 3a 34 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :49 danielk1977
10530 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Exp $.*/../*.**
10531 41 20 63 6f 6d 70 6c 65 74 65 20 70 61 67 65 20 A complete page
10532 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e 73 74 cache is an inst
10533 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
10534 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 ucture..*/.struc
10535 74 20 50 43 61 63 68 65 20 7b 0a 20 20 50 67 48 t PCache {. PgH
10536 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 44 69 dr *pDirty, *pDi
10537 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20 20 20 rtyTail;
10538 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72 74 /* List of dirt
10539 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55 20 6f y pages in LRU o
1053a 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 72 20 rder */. PgHdr
1053b 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20 20 20 *pSynced;
1053c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1053d 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70 61 67 Last synced pag
1053e 65 20 69 6e 20 64 69 72 74 79 20 70 61 67 65 20 e in dirty page
1053f 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 list */. int nR
10540 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ef;
10541 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10542 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 Number of refer
10543 65 6e 63 65 64 20 70 61 67 65 73 20 2a 2f 0a 20 enced pages */.
10544 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 20 20 int nMax;
10545 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10546 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 /* Configur
10547 65 64 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f ed cache size */
10548 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 . int szPage;
10549 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1054a 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
1054b 66 20 65 76 65 72 79 20 70 61 67 65 20 69 6e 20 f every page in
1054c 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20 20 this cache */.
1054d 69 6e 74 20 73 7a 45 78 74 72 61 3b 20 20 20 20 int szExtra;
1054e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1054f 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 /* Size of e
10550 78 74 72 61 20 73 70 61 63 65 20 66 6f 72 20 65 xtra space for e
10551 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e ach page */. in
10552 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 20 t bPurgeable;
10553 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10554 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67 /* True if pag
10555 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e es are on backin
10556 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 g store */. int
10557 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 64 (*xStress)(void
10558 2a 2c 50 67 48 64 72 2a 29 3b 20 20 20 20 20 20 *,PgHdr*);
10559 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20 /* Call to try
1055a 6d 61 6b 65 20 61 20 70 61 67 65 20 63 6c 65 61 make a page clea
1055b 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 n */. void *pSt
1055c 72 65 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 ress;
1055d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 /* Ar
1055e 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73 gument to xStres
1055f 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70 s */. sqlite3_p
10560 63 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20 20 cache *pCache;
10561 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6c /* Pl
10562 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 6d 6f uggable cache mo
10563 64 75 6c 65 20 2a 2f 0a 20 20 50 67 48 64 72 20 dule */. PgHdr
10564 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 20 20 *pPage1;
10565 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10566 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 Reference to pa
10567 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a ge 1 */.};../*.*
10568 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61 73 * Some of the as
10569 73 65 72 74 28 29 20 6d 61 63 72 6f 73 20 69 6e sert() macros in
1056a 20 74 68 69 73 20 63 6f 64 65 20 61 72 65 20 74 this code are t
1056b 6f 6f 20 65 78 70 65 6e 73 69 76 65 20 74 6f 20 oo expensive to
1056c 72 75 6e 0a 2a 2a 20 65 76 65 6e 20 64 75 72 69 run.** even duri
1056d 6e 67 20 6e 6f 72 6d 61 6c 20 64 65 62 75 67 67 ng normal debugg
1056e 69 6e 67 2e 20 20 55 73 65 20 74 68 65 6d 20 6f ing. Use them o
1056f 6e 6c 79 20 72 61 72 65 6c 79 20 6f 6e 20 6c 6f nly rarely on lo
10570 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a 2a 20 74 65 ng-running.** te
10571 73 74 73 2e 20 20 45 6e 61 62 6c 65 20 74 68 65 sts. Enable the
10572 20 65 78 70 65 6e 73 69 76 65 20 61 73 73 65 72 expensive asser
10573 74 73 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 ts using the.**
10574 2d 44 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f -DSQLITE_ENABLE_
10575 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 EXPENSIVE_ASSERT
10576 3d 31 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 =1 compile-time
10577 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 64 65 option..*/.#ifde
10578 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
10579 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 EXPENSIVE_ASSERT
1057a 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73 .# define expens
1057b 69 76 65 5f 61 73 73 65 72 74 28 58 29 20 20 61 ive_assert(X) a
1057c 73 73 65 72 74 28 58 29 0a 23 65 6c 73 65 0a 23 ssert(X).#else.#
1057d 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73 69 76 define expensiv
1057e 65 5f 61 73 73 65 72 74 28 58 29 0a 23 65 6e 64 e_assert(X).#end
1057f 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a if../***********
10580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10581 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b 65 64 20 4c ******* Linked L
10582 69 73 74 20 4d 61 6e 61 67 65 6d 65 6e 74 20 2a ist Management *
10583 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10584 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e ***/..#if !defin
10585 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 64 65 ed(NDEBUG) && de
10586 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 fined(SQLITE_ENA
10587 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 BLE_EXPENSIVE_AS
10588 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20 43 68 65 63 SERT)./*.** Chec
10589 6b 20 74 68 61 74 20 74 68 65 20 70 43 61 63 68 k that the pCach
1058a 65 2d 3e 70 53 79 6e 63 65 64 20 76 61 72 69 61 e->pSynced varia
1058b 62 6c 65 20 69 73 20 73 65 74 20 63 6f 72 72 65 ble is set corre
1058c 63 74 6c 79 2e 20 49 66 20 69 74 0a 2a 2a 20 69 ctly. If it.** i
1058d 73 20 6e 6f 74 2c 20 65 69 74 68 65 72 20 66 61 s not, either fa
1058e 69 6c 20 61 6e 20 61 73 73 65 72 74 20 6f 72 20 il an assert or
1058f 72 65 74 75 72 6e 20 7a 65 72 6f 2e 20 4f 74 68 return zero. Oth
10590 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 0a 2a erwise, return.*
10591 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 54 68 69 73 * non-zero. This
10592 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 6e is only used in
10593 20 64 65 62 75 67 67 69 6e 67 20 62 75 69 6c 64 debugging build
10594 73 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a s, as follows:.*
10595 2a 0a 2a 2a 20 20 20 65 78 70 65 6e 73 69 76 65 *.** expensive
10596 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43 _assert( pcacheC
10597 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63 68 heckSynced(pCach
10598 65 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 e) );.*/.static
10599 69 6e 74 20 70 63 61 63 68 65 43 68 65 63 6b 53 int pcacheCheckS
1059a 79 6e 63 65 64 28 50 43 61 63 68 65 20 2a 70 43 ynced(PCache *pC
1059b 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72 20 2a ache){. PgHdr *
1059c 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68 p;. for(p=pCach
1059d 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b 20 70 e->pDirtyTail; p
1059e 21 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 !=pCache->pSynce
1059f 64 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 50 72 d; p=p->pDirtyPr
105a0 65 76 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 ev){. assert(
105a1 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d 3e p->nRef || (p->
105a2 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
105a3 5f 53 59 4e 43 29 20 29 3b 0a 20 20 7d 0a 20 20 _SYNC) );. }.
105a4 72 65 74 75 72 6e 20 28 70 3d 3d 30 20 7c 7c 20 return (p==0 ||
105a5 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d 3e 66 p->nRef || (p->f
105a6 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f lags&PGHDR_NEED_
105a7 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d 0a 23 65 6e SYNC)==0);.}.#en
105a8 64 69 66 20 2f 2a 20 21 4e 44 45 42 55 47 20 26 dif /* !NDEBUG &
105a9 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f & SQLITE_ENABLE_
105aa 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 EXPENSIVE_ASSERT
105ab 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 */../*.** Remov
105ac 65 20 70 61 67 65 20 70 50 61 67 65 20 66 72 6f e page pPage fro
105ad 6d 20 74 68 65 20 6c 69 73 74 20 6f 66 20 64 69 m the list of di
105ae 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 73 74 rty pages..*/.st
105af 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
105b0 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c RemoveFromDirtyL
105b1 69 73 74 28 50 67 48 64 72 20 2a 70 50 61 67 65 ist(PgHdr *pPage
105b2 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 20 3d ){. PCache *p =
105b3 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a pPage->pCache;.
105b4 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
105b5 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 7c 7c 20 ->pDirtyNext ||
105b6 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 pPage==p->pDirty
105b7 54 61 69 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 Tail );. assert
105b8 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 ( pPage->pDirtyP
105b9 72 65 76 20 7c 7c 20 70 50 61 67 65 3d 3d 70 2d rev || pPage==p-
105ba 3e 70 44 69 72 74 79 20 29 3b 0a 0a 20 20 2f 2a >pDirty );.. /*
105bb 20 55 70 64 61 74 65 20 74 68 65 20 50 43 61 63 Update the PCac
105bc 68 65 31 2e 70 53 79 6e 63 65 64 20 76 61 72 69 he1.pSynced vari
105bd 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61 72 able if necessar
105be 79 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 y. */. if( p->p
105bf 53 79 6e 63 65 64 3d 3d 70 50 61 67 65 20 29 7b Synced==pPage ){
105c0 0a 20 20 20 20 50 67 48 64 72 20 2a 70 53 79 6e . PgHdr *pSyn
105c1 63 65 64 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 ced = pPage->pDi
105c2 72 74 79 50 72 65 76 3b 0a 20 20 20 20 77 68 69 rtyPrev;. whi
105c3 6c 65 28 20 70 53 79 6e 63 65 64 20 26 26 20 28 le( pSynced && (
105c4 70 53 79 6e 63 65 64 2d 3e 66 6c 61 67 73 26 50 pSynced->flags&P
105c5 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 GHDR_NEED_SYNC)
105c6 29 7b 0a 20 20 20 20 20 20 70 53 79 6e 63 65 64 ){. pSynced
105c7 20 3d 20 70 53 79 6e 63 65 64 2d 3e 70 44 69 72 = pSynced->pDir
105c8 74 79 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 tyPrev;. }.
105c9 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70 p->pSynced = p
105ca 53 79 6e 63 65 64 3b 0a 20 20 7d 0a 0a 20 20 69 Synced;. }.. i
105cb 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 f( pPage->pDirty
105cc 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 50 61 67 Next ){. pPag
105cd 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70 e->pDirtyNext->p
105ce 44 69 72 74 79 50 72 65 76 20 3d 20 70 50 61 67 DirtyPrev = pPag
105cf 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 e->pDirtyPrev;.
105d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
105d1 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 rt( pPage==p->pD
105d2 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20 20 20 irtyTail );.
105d3 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d 20 p->pDirtyTail =
105d4 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 pPage->pDirtyPre
105d5 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 v;. }. if( pPa
105d6 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20 29 ge->pDirtyPrev )
105d7 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69 {. pPage->pDi
105d8 72 74 79 50 72 65 76 2d 3e 70 44 69 72 74 79 4e rtyPrev->pDirtyN
105d9 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 ext = pPage->pDi
105da 72 74 79 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 rtyNext;. }else
105db 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
105dc 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 20 29 age==p->pDirty )
105dd 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 20 ;. p->pDirty
105de 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e = pPage->pDirtyN
105df 65 78 74 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 ext;. }. pPage
105e0 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20 30 ->pDirtyNext = 0
105e1 3b 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 ;. pPage->pDirt
105e2 79 50 72 65 76 20 3d 20 30 3b 0a 0a 20 20 65 78 yPrev = 0;.. ex
105e3 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 pensive_assert(
105e4 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 pcacheCheckSynce
105e5 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a d(p) );.}../*.**
105e6 20 41 64 64 20 70 61 67 65 20 70 50 61 67 65 20 Add page pPage
105e7 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 to the head of t
105e8 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 28 50 he dirty list (P
105e9 43 61 63 68 65 31 2e 70 44 69 72 74 79 20 69 73 Cache1.pDirty is
105ea 20 73 65 74 20 74 6f 0a 2a 2a 20 70 50 61 67 65 set to.** pPage
105eb 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
105ec 64 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 d pcacheAddToDir
105ed 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70 50 tyList(PgHdr *pP
105ee 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20 2a age){. PCache *
105ef 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 68 p = pPage->pCach
105f0 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 e;.. assert( pP
105f1 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3d age->pDirtyNext=
105f2 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 70 44 69 =0 && pPage->pDi
105f3 72 74 79 50 72 65 76 3d 3d 30 20 26 26 20 70 2d rtyPrev==0 && p-
105f4 3e 70 44 69 72 74 79 21 3d 70 50 61 67 65 20 29 >pDirty!=pPage )
105f5 3b 0a 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72 ;.. pPage->pDir
105f6 74 79 4e 65 78 74 20 3d 20 70 2d 3e 70 44 69 72 tyNext = p->pDir
105f7 74 79 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d ty;. if( pPage-
105f8 3e 70 44 69 72 74 79 4e 65 78 74 20 29 7b 0a 20 >pDirtyNext ){.
105f9 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
105fa 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70 44 ->pDirtyNext->pD
105fb 69 72 74 79 50 72 65 76 3d 3d 30 20 29 3b 0a 20 irtyPrev==0 );.
105fc 20 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 pPage->pDirty
105fd 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72 65 76 Next->pDirtyPrev
105fe 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 = pPage;. }.
105ff 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 61 67 p->pDirty = pPag
10600 65 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 44 69 e;. if( !p->pDi
10601 72 74 79 54 61 69 6c 20 29 7b 0a 20 20 20 20 70 rtyTail ){. p
10602 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d 20 70 ->pDirtyTail = p
10603 50 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 Page;. }. if(
10604 21 70 2d 3e 70 53 79 6e 63 65 64 20 26 26 20 30 !p->pSynced && 0
10605 3d 3d 28 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 ==(pPage->flags&
10606 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
10607 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 79 6e 63 ){. p->pSync
10608 65 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a ed = pPage;. }.
10609 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 expensive_asse
1060a 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53 rt( pcacheCheckS
1060b 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f ynced(p) );.}../
1060c 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20 61 72 6f *.** Wrapper aro
1060d 75 6e 64 20 74 68 65 20 70 6c 75 67 67 61 62 6c und the pluggabl
1060e 65 20 63 61 63 68 65 73 20 78 55 6e 70 69 6e 20 e caches xUnpin
1060f 6d 65 74 68 6f 64 2e 20 49 66 20 74 68 65 20 63 method. If the c
10610 61 63 68 65 20 69 73 0a 2a 2a 20 62 65 69 6e 67 ache is.** being
10611 20 75 73 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d used for an in-
10612 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c memory database,
10613 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
10614 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 s a no-op..*/.st
10615 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
10616 55 6e 70 69 6e 28 50 67 48 64 72 20 2a 70 29 7b Unpin(PgHdr *p){
10617 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 . PCache *pCach
10618 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 e = p->pCache;.
10619 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 if( pCache->bPu
1061a 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 rgeable ){. i
1061b 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b f( p->pgno==1 ){
1061c 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 70 . pCache->p
1061d 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 20 7d Page1 = 0;. }
1061e 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
1061f 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e alConfig.pcache.
10620 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 3e 70 xUnpin(pCache->p
10621 43 61 63 68 65 2c 20 70 2c 20 30 29 3b 0a 20 20 Cache, p, 0);.
10622 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }.}../**********
10623 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10624 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10625 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 ********* Genera
10626 6c 20 49 6e 74 65 72 66 61 63 65 73 20 2a 2a 2a l Interfaces ***
10627 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6e 69 74 69 61 ***.**.** Initia
10628 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64 6f 77 lize and shutdow
10629 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 n the page cache
1062a 20 73 75 62 73 79 73 74 65 6d 2e 20 4e 65 69 74 subsystem. Neit
1062b 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a 2a 2a her of these .**
1062c 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 74 functions are t
1062d 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a 53 51 hreadsafe..*/.SQ
1062e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1062f 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 6e sqlite3PcacheIn
10630 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 7b 0a itialize(void){.
10631 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
10632 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
10633 2e 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 .xInit==0 ){.
10634 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 sqlite3PCacheSe
10635 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d 0a tDefault();. }.
10636 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
10637 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 GlobalConfig.pca
10638 63 68 65 2e 78 49 6e 69 74 28 73 71 6c 69 74 65 che.xInit(sqlite
10639 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
1063a 61 63 68 65 2e 70 41 72 67 29 3b 0a 7d 0a 53 51 ache.pArg);.}.SQ
1063b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1063c 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 d sqlite3PcacheS
1063d 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b 0a 20 hutdown(void){.
1063e 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 if( sqlite3Glob
1063f 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e alConfig.pcache.
10640 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20 20 20 xShutdown ){.
10641 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
10642 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 53 68 75 nfig.pcache.xShu
10643 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c 6f tdown(sqlite3Glo
10644 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
10645 2e 70 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f .pArg);. }.}../
10646 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
10647 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 size in bytes of
10648 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 a PCache object
10649 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1064a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
1064b 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64 29 7b cacheSize(void){
1064c 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28 50 return sizeof(P
1064d 43 61 63 68 65 29 3b 20 7d 0a 0a 2f 2a 0a 2a 2a Cache); }../*.**
1064e 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 50 43 Create a new PC
1064f 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 53 74 6f ache object. Sto
10650 72 61 67 65 20 73 70 61 63 65 20 74 6f 20 68 6f rage space to ho
10651 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 0a 2a 2a ld the object.**
10652 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
10653 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 n allocated and
10654 69 73 20 70 61 73 73 65 64 20 69 6e 20 61 73 20 is passed in as
10655 74 68 65 20 70 20 70 6f 69 6e 74 65 72 2e 20 0a the p pointer. .
10656 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 64 69 ** The caller di
10657 73 63 6f 76 65 72 73 20 68 6f 77 20 6d 75 63 68 scovers how much
10658 20 73 70 61 63 65 20 6e 65 65 64 73 20 74 6f 20 space needs to
10659 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 be allocated by
1065a 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 .** calling sqli
1065b 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 2e te3PcacheSize().
1065c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1065d 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
1065e 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e 74 cacheOpen(. int
1065f 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20 20 20 szPage,
10660 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
10661 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 e of every page
10662 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61 */. int szExtra
10663 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
10664 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61 63 65 /* Extra space
10665 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
10666 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 each page */.
10667 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 2c 20 int bPurgeable,
10668 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10669 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72 True if pages ar
1066a 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f e on backing sto
1066b 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 re */. int (*xS
1066c 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48 tress)(void*,PgH
1066d 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74 6f 20 dr*),/* Call to
1066e 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 try to make page
1066f 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 s clean */. voi
10670 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20 d *pStress,
10671 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 /* Arg
10672 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73 73 ument to xStress
10673 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 20 */. PCache *p
10674 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10675 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 /* Preallocat
10676 65 64 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 ed space for the
10677 20 50 43 61 63 68 65 20 2a 2f 0a 29 7b 0a 20 20 PCache */.){.
10678 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a memset(p, 0, siz
10679 65 6f 66 28 50 43 61 63 68 65 29 29 3b 0a 20 20 eof(PCache));.
1067a 70 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 p->szPage = szPa
1067b 67 65 3b 0a 20 20 70 2d 3e 73 7a 45 78 74 72 61 ge;. p->szExtra
1067c 20 3d 20 73 7a 45 78 74 72 61 3b 0a 20 20 70 2d = szExtra;. p-
1067d 3e 62 50 75 72 67 65 61 62 6c 65 20 3d 20 62 50 >bPurgeable = bP
1067e 75 72 67 65 61 62 6c 65 3b 0a 20 20 70 2d 3e 78 urgeable;. p->x
1067f 53 74 72 65 73 73 20 3d 20 78 53 74 72 65 73 73 Stress = xStress
10680 3b 0a 20 20 70 2d 3e 70 53 74 72 65 73 73 20 3d ;. p->pStress =
10681 20 70 53 74 72 65 73 73 3b 0a 20 20 70 2d 3e 6e pStress;. p->n
10682 4d 61 78 20 3d 20 31 30 30 3b 0a 7d 0a 0a 2f 2a Max = 100;.}../*
10683 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70 .** Change the p
10684 61 67 65 20 73 69 7a 65 20 66 6f 72 20 50 43 61 age size for PCa
10685 63 68 65 20 6f 62 6a 65 63 74 2e 20 54 68 65 20 che object. The
10686 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 caller must ensu
10687 72 65 20 74 68 61 74 20 74 68 65 72 65 0a 2a 2a re that there.**
10688 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 are no outstand
10689 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 65 6e ing page referen
1068a 63 65 73 20 77 68 65 6e 20 74 68 69 73 20 66 75 ces when this fu
1068b 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
1068c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1068d 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1068e 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69 7a PcacheSetPageSiz
1068f 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 e(PCache *pCache
10690 2c 20 69 6e 74 20 73 7a 50 61 67 65 29 7b 0a 20 , int szPage){.
10691 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 2d assert( pCache-
10692 3e 6e 52 65 66 3d 3d 30 20 26 26 20 70 43 61 63 >nRef==0 && pCac
10693 68 65 2d 3e 70 44 69 72 74 79 3d 3d 30 20 29 3b he->pDirty==0 );
10694 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 . if( pCache->p
10695 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c Cache ){. sql
10696 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
10697 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 .pcache.xDestroy
10698 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 (pCache->pCache)
10699 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 43 ;. pCache->pC
1069a 61 63 68 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 ache = 0;. }.
1069b 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 3d pCache->szPage =
1069c 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a szPage;.}../*.*
1069d 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20 * Try to obtain
1069e 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 a page from the
1069f 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 cache..*/.SQLITE
106a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
106a1 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28 ite3PcacheFetch(
106a2 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 . PCache *pCach
106a3 65 2c 20 20 20 20 20 20 20 2f 2a 20 4f 62 74 61 e, /* Obta
106a4 69 6e 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d in the page from
106a5 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20 this cache */.
106a6 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 Pgno pgno,
106a7 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e /* Page n
106a8 75 6d 62 65 72 20 74 6f 20 6f 62 74 61 69 6e 20 umber to obtain
106a9 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74 65 46 */. int createF
106aa 6c 61 67 2c 20 20 20 20 20 20 20 2f 2a 20 49 66 lag, /* If
106ab 20 74 72 75 65 2c 20 63 72 65 61 74 65 20 70 61 true, create pa
106ac 67 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f ge if it does no
106ad 74 20 65 78 69 73 74 20 61 6c 72 65 61 64 79 20 t exist already
106ae 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 70 70 50 */. PgHdr **ppP
106af 61 67 65 20 20 20 20 20 20 20 20 2f 2a 20 57 72 age /* Wr
106b0 69 74 65 20 74 68 65 20 70 61 67 65 20 68 65 72 ite the page her
106b1 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72 20 e */.){. PgHdr
106b2 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e *pPage = 0;. in
106b3 74 20 65 43 72 65 61 74 65 3b 0a 0a 20 20 61 73 t eCreate;.. as
106b4 73 65 72 74 28 20 70 43 61 63 68 65 21 3d 30 20 sert( pCache!=0
106b5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 72 65 );. assert( cre
106b6 61 74 65 46 6c 61 67 3d 3d 31 20 7c 7c 20 63 72 ateFlag==1 || cr
106b7 65 61 74 65 46 6c 61 67 3d 3d 30 20 29 3b 0a 20 eateFlag==0 );.
106b8 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3e 30 20 assert( pgno>0
106b9 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
106ba 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 pluggable cache
106bb 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a (sqlite3_pcache*
106bc 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61 ) has not been a
106bd 6c 6c 6f 63 61 74 65 64 2c 0a 20 20 2a 2a 20 61 llocated,. ** a
106be 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e 0a llocate it now..
106bf 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 61 63 */. if( !pCac
106c0 68 65 2d 3e 70 43 61 63 68 65 20 26 26 20 63 72 he->pCache && cr
106c1 65 61 74 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 eateFlag ){.
106c2 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a sqlite3_pcache *
106c3 70 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 p;. int nByte
106c4 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 70 43 ;. nByte = pC
106c5 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 2b 20 70 ache->szPage + p
106c6 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20 2b Cache->szExtra +
106c7 20 73 69 7a 65 6f 66 28 50 67 48 64 72 29 3b 0a sizeof(PgHdr);.
106c8 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 p = sqlite3G
106c9 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
106ca 68 65 2e 78 43 72 65 61 74 65 28 6e 42 79 74 65 he.xCreate(nByte
106cb 2c 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 , pCache->bPurge
106cc 61 62 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 21 able);. if( !
106cd 70 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 p ){. retur
106ce 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
106cf 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
106d0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
106d1 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28 ache.xCachesize(
106d2 70 2c 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 p, pCache->nMax)
106d3 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 43 ;. pCache->pC
106d4 61 63 68 65 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20 ache = p;. }..
106d5 20 65 43 72 65 61 74 65 20 3d 20 63 72 65 61 74 eCreate = creat
106d6 65 46 6c 61 67 20 2a 20 28 31 20 2b 20 28 21 70 eFlag * (1 + (!p
106d7 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c Cache->bPurgeabl
106d8 65 20 7c 7c 20 21 70 43 61 63 68 65 2d 3e 70 44 e || !pCache->pD
106d9 69 72 74 79 29 29 3b 0a 20 20 69 66 28 20 70 43 irty));. if( pC
106da 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a ache->pCache ){.
106db 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69 pPage = sqli
106dc 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
106dd 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 43 pcache.xFetch(pC
106de 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 ache->pCache, pg
106df 6e 6f 2c 20 65 43 72 65 61 74 65 29 3b 0a 20 20 no, eCreate);.
106e0 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 20 }.. if( !pPage
106e1 26 26 20 65 43 72 65 61 74 65 3d 3d 31 20 29 7b && eCreate==1 ){
106e2 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 3b . PgHdr *pPg;
106e3 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61 20 .. /* Find a
106e4 64 69 72 74 79 20 70 61 67 65 20 74 6f 20 77 72 dirty page to wr
106e5 69 74 65 2d 6f 75 74 20 61 6e 64 20 72 65 63 79 ite-out and recy
106e6 63 6c 65 2e 20 46 69 72 73 74 20 74 72 79 20 74 cle. First try t
106e7 6f 20 66 69 6e 64 20 61 20 0a 20 20 20 20 2a 2a o find a . **
106e8 20 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 page that does
106e9 6e 6f 74 20 72 65 71 75 69 72 65 20 61 20 6a 6f not require a jo
106ea 75 72 6e 61 6c 2d 73 79 6e 63 20 28 6f 6e 65 20 urnal-sync (one
106eb 77 69 74 68 20 50 47 48 44 52 5f 4e 45 45 44 5f with PGHDR_NEED_
106ec 53 59 4e 43 0a 20 20 20 20 2a 2a 20 63 6c 65 61 SYNC. ** clea
106ed 72 65 64 29 2c 20 62 75 74 20 69 66 20 74 68 61 red), but if tha
106ee 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
106ef 65 20 73 65 74 74 6c 65 20 66 6f 72 20 61 6e 79 e settle for any
106f0 20 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a 20 75 other . ** u
106f1 6e 72 65 66 65 72 65 6e 63 65 64 20 64 69 72 74 nreferenced dirt
106f2 79 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 y page.. */.
106f3 20 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 expensive_ass
106f4 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b ert( pcacheCheck
106f5 53 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 29 Synced(pCache) )
106f6 3b 0a 20 20 20 20 66 6f 72 28 70 50 67 3d 70 43 ;. for(pPg=pC
106f7 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 0a ache->pSynced; .
106f8 20 20 20 20 20 20 20 20 70 50 67 20 26 26 20 28 pPg && (
106f9 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 50 pPg->nRef || (pP
106fa 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
106fb 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a 20 20 20 EED_SYNC)); .
106fc 20 20 20 20 20 70 50 67 3d 70 50 67 2d 3e 70 44 pPg=pPg->pD
106fd 69 72 74 79 50 72 65 76 0a 20 20 20 20 29 3b 0a irtyPrev. );.
106fe 20 20 20 20 69 66 28 20 21 70 50 67 20 29 7b 0a if( !pPg ){.
106ff 20 20 20 20 20 20 66 6f 72 28 70 50 67 3d 70 43 for(pPg=pC
10700 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c ache->pDirtyTail
10701 3b 20 70 50 67 20 26 26 20 70 50 67 2d 3e 6e 52 ; pPg && pPg->nR
10702 65 66 3b 20 70 50 67 3d 70 50 67 2d 3e 70 44 69 ef; pPg=pPg->pDi
10703 72 74 79 50 72 65 76 29 3b 0a 20 20 20 20 7d 0a rtyPrev);. }.
10704 20 20 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 if( pPg ){.
10705 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 int rc;.
10706 20 20 20 72 63 20 3d 20 70 43 61 63 68 65 2d 3e rc = pCache->
10707 78 53 74 72 65 73 73 28 70 43 61 63 68 65 2d 3e xStress(pCache->
10708 70 53 74 72 65 73 73 2c 20 70 50 67 29 3b 0a 20 pStress, pPg);.
10709 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1070a 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 ITE_OK && rc!=SQ
1070b 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 LITE_BUSY ){.
1070c 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1070d 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
1070e 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69 74 pPage = sqlit
1070f 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
10710 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 43 61 cache.xFetch(pCa
10711 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 6e che->pCache, pgn
10712 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 o, 2);. }.. if
10713 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 69 ( pPage ){. i
10714 66 28 20 21 70 50 61 67 65 2d 3e 70 44 61 74 61 f( !pPage->pData
10715 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 ){. memset
10716 28 70 50 61 67 65 2c 20 30 2c 20 73 69 7a 65 6f (pPage, 0, sizeo
10717 66 28 50 67 48 64 72 29 20 2b 20 70 43 61 63 68 f(PgHdr) + pCach
10718 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20 20 20 e->szExtra);.
10719 20 20 20 70 50 61 67 65 2d 3e 70 45 78 74 72 61 pPage->pExtra
1071a 20 3d 20 28 76 6f 69 64 2a 29 26 70 50 61 67 65 = (void*)&pPage
1071b 5b 31 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 65 [1];. pPage
1071c 2d 3e 70 44 61 74 61 20 3d 20 28 76 6f 69 64 20 ->pData = (void
1071d 2a 29 26 28 28 63 68 61 72 20 2a 29 70 50 61 67 *)&((char *)pPag
1071e 65 29 5b 73 69 7a 65 6f 66 28 50 67 48 64 72 29 e)[sizeof(PgHdr)
1071f 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 + pCache->szExt
10720 72 61 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 65 ra];. pPage
10721 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 ->pCache = pCach
10722 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e e;. pPage->
10723 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 pgno = pgno;.
10724 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
10725 50 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d 70 43 Page->pCache==pC
10726 61 63 68 65 20 29 3b 0a 20 20 20 20 61 73 73 65 ache );. asse
10727 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d rt( pPage->pgno=
10728 3d 70 67 6e 6f 20 29 3b 0a 20 20 20 20 61 73 73 =pgno );. ass
10729 65 72 74 28 20 70 50 61 67 65 2d 3e 70 45 78 74 ert( pPage->pExt
1072a 72 61 3d 3d 28 76 6f 69 64 20 2a 29 26 70 50 61 ra==(void *)&pPa
1072b 67 65 5b 31 5d 20 29 3b 0a 0a 20 20 20 20 69 66 ge[1] );.. if
1072c 28 20 30 3d 3d 70 50 61 67 65 2d 3e 6e 52 65 66 ( 0==pPage->nRef
1072d 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65 ){. pCache
1072e 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a ->nRef++;. }.
1072f 20 20 20 20 70 50 61 67 65 2d 3e 6e 52 65 66 2b pPage->nRef+
10730 2b 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3d +;. if( pgno=
10731 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 =1 ){. pCac
10732 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61 he->pPage1 = pPa
10733 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ge;. }. }.
10734 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 3b *ppPage = pPage;
10735 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 67 65 . return (pPage
10736 3d 3d 30 20 26 26 20 65 43 72 65 61 74 65 29 20 ==0 && eCreate)
10737 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 3a ? SQLITE_NOMEM :
10738 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
10739 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 *.** Decrement t
1073a 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 he reference cou
1073b 6e 74 20 6f 6e 20 61 20 70 61 67 65 2e 20 49 66 nt on a page. If
1073c 20 74 68 65 20 70 61 67 65 20 69 73 20 63 6c 65 the page is cle
1073d 61 6e 20 61 6e 64 20 74 68 65 0a 2a 2a 20 72 65 an and the.** re
1073e 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 64 72 ference count dr
1073f 6f 70 73 20 74 6f 20 30 2c 20 74 68 65 6e 20 69 ops to 0, then i
10740 74 20 69 73 20 6d 61 64 65 20 65 6c 69 62 6c 65 t is made elible
10741 20 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 2e 0a for recycling..
10742 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
10743 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
10744 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 64 acheRelease(PgHd
10745 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 r *p){. assert(
10746 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 p->nRef>0 );.
10747 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 p->nRef--;. if(
10748 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 p->nRef==0 ){.
10749 20 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 PCache *pCach
1074a 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 e = p->pCache;.
1074b 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 2d pCache->nRef-
1074c 2d 3b 0a 20 20 20 20 69 66 28 20 28 70 2d 3e 66 -;. if( (p->f
1074d 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 lags&PGHDR_DIRTY
1074e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 )==0 ){. pc
1074f 61 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a 20 20 acheUnpin(p);.
10750 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f }else{. /
10751 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 * Move the page
10752 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 to the head of t
10753 68 65 20 64 69 72 74 79 20 6c 69 73 74 2e 20 2a he dirty list. *
10754 2f 0a 20 20 20 20 20 20 70 63 61 63 68 65 52 65 /. pcacheRe
10755 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 moveFromDirtyLis
10756 74 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63 t(p);. pcac
10757 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 74 heAddToDirtyList
10758 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d (p);. }. }.}
10759 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 61 73 65 ../*.** Increase
1075a 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 the reference c
1075b 6f 75 6e 74 20 6f 66 20 61 20 73 75 70 70 6c 69 ount of a suppli
1075c 65 64 20 70 61 67 65 20 62 79 20 31 2e 0a 2a 2f ed page by 1..*/
1075d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1075e 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
1075f 68 65 52 65 66 28 50 67 48 64 72 20 2a 70 29 7b heRef(PgHdr *p){
10760 0a 20 20 61 73 73 65 72 74 28 70 2d 3e 6e 52 65 . assert(p->nRe
10761 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b f>0);. p->nRef+
10762 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 +;.}../*.** Drop
10763 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 a page from the
10764 20 63 61 63 68 65 2e 20 54 68 65 72 65 20 6d 75 cache. There mu
10765 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 6f 6e st be exactly on
10766 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 e reference to t
10767 68 65 0a 2a 2a 20 70 61 67 65 2e 20 54 68 69 73 he.** page. This
10768 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74 65 function delete
10769 73 20 74 68 61 74 20 72 65 66 65 72 65 6e 63 65 s that reference
1076a 2c 20 73 6f 20 61 66 74 65 72 20 69 74 20 72 65 , so after it re
1076b 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 70 61 67 turns the.** pag
1076c 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 e pointed to by
1076d 70 20 69 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f p is invalid..*/
1076e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1076f 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
10770 68 65 44 72 6f 70 28 50 67 48 64 72 20 2a 70 29 heDrop(PgHdr *p)
10771 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 {. PCache *pCac
10772 68 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d he;. assert( p-
10773 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69 66 >nRef==1 );. if
10774 28 20 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 ( p->flags&PGHDR
10775 5f 44 49 52 54 59 20 29 7b 0a 20 20 20 20 70 63 _DIRTY ){. pc
10776 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 acheRemoveFromDi
10777 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d 0a rtyList(p);. }.
10778 20 20 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 pCache = p->pC
10779 61 63 68 65 3b 0a 20 20 70 43 61 63 68 65 2d 3e ache;. pCache->
1077a 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 2d nRef--;. if( p-
1077b 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 >pgno==1 ){.
1077c 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d pCache->pPage1 =
1077d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 0;. }. sqlite
1077e 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
1077f 61 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 61 63 ache.xUnpin(pCac
10780 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 31 he->pCache, p, 1
10781 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 );.}../*.** Make
10782 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69 sure the page i
10783 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 s marked as dirt
10784 79 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 64 y. If it isn't d
10785 69 72 74 79 20 61 6c 72 65 61 64 79 2c 0a 2a 2a irty already,.**
10786 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a make it so..*/.
10787 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
10788 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
10789 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64 72 eMakeDirty(PgHdr
1078a 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c 61 67 73 *p){. p->flags
1078b 20 26 3d 20 7e 50 47 48 44 52 5f 44 4f 4e 54 5f &= ~PGHDR_DONT_
1078c 57 52 49 54 45 3b 0a 20 20 61 73 73 65 72 74 28 WRITE;. assert(
1078d 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 p->nRef>0 );.
1078e 69 66 28 20 30 3d 3d 28 70 2d 3e 66 6c 61 67 73 if( 0==(p->flags
1078f 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 29 20 & PGHDR_DIRTY)
10790 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 ){. p->flags
10791 7c 3d 20 50 47 48 44 52 5f 44 49 52 54 59 3b 0a |= PGHDR_DIRTY;.
10792 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f 44 pcacheAddToD
10793 69 72 74 79 4c 69 73 74 28 20 70 29 3b 0a 20 20 irtyList( p);.
10794 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 }.}../*.** Make
10795 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69 73 sure the page is
10796 20 6d 61 72 6b 65 64 20 61 73 20 63 6c 65 61 6e marked as clean
10797 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 63 6c . If it isn't cl
10798 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a 20 ean already,.**
10799 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53 make it so..*/.S
1079a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1079b 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
1079c 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 20 MakeClean(PgHdr
1079d 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66 *p){. if( (p->f
1079e 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 lags & PGHDR_DIR
1079f 54 59 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68 TY) ){. pcach
107a0 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 eRemoveFromDirty
107a1 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 2d 3e List(p);. p->
107a2 66 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 44 52 flags &= ~(PGHDR
107a3 5f 44 49 52 54 59 7c 50 47 48 44 52 5f 4e 45 45 _DIRTY|PGHDR_NEE
107a4 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 28 D_SYNC);. if(
107a5 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 p->nRef==0 ){.
107a6 20 20 20 20 20 70 63 61 63 68 65 55 6e 70 69 6e pcacheUnpin
107a7 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d (p);. }. }.}
107a8 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76 65 ../*.** Make eve
107a9 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 ry page in the c
107aa 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a 53 ache clean..*/.S
107ab 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
107ac 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
107ad 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 20 CleanAll(PCache
107ae 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 *pCache){. PgHd
107af 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20 28 r *p;. while( (
107b0 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 p = pCache->pDir
107b1 74 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 ty)!=0 ){. sq
107b2 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 lite3PcacheMakeC
107b3 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a lean(p);. }.}..
107b4 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 /*.** Clear the
107b5 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 PGHDR_NEED_SYNC
107b6 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64 69 flag from all di
107b7 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53 51 rty pages..*/.SQ
107b8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
107b9 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 d sqlite3PcacheC
107ba 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43 learSyncFlags(PC
107bb 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 ache *pCache){.
107bc 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 PgHdr *p;. for
107bd 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 (p=pCache->pDirt
107be 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 y; p; p=p->pDirt
107bf 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 66 yNext){. p->f
107c0 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e lags &= ~PGHDR_N
107c1 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 EED_SYNC;. }.
107c2 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 pCache->pSynced
107c3 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 = pCache->pDirty
107c4 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 Tail;.}../*.** C
107c5 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 6e hange the page n
107c6 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70 20 umber of page p
107c7 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f 0a to newPgno. .*/.
107c8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
107c9 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
107ca 65 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c 20 eMove(PgHdr *p,
107cb 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 Pgno newPgno){.
107cc 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 20 PCache *pCache
107cd 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 61 = p->pCache;. a
107ce 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 ssert( p->nRef>0
107cf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 65 );. assert( ne
107d0 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71 6c wPgno>0 );. sql
107d1 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
107d2 2e 70 63 61 63 68 65 2e 78 52 65 6b 65 79 28 70 .pcache.xRekey(p
107d3 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 Cache->pCache, p
107d4 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77 50 67 , p->pgno, newPg
107d5 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20 3d no);. p->pgno =
107d6 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28 20 newPgno;. if(
107d7 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f (p->flags&PGHDR_
107d8 44 49 52 54 59 29 20 26 26 20 28 70 2d 3e 66 6c DIRTY) && (p->fl
107d9 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
107da 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 63 61 63 YNC) ){. pcac
107db 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 heRemoveFromDirt
107dc 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 63 yList(p);. pc
107dd 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 acheAddToDirtyLi
107de 73 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a st(p);. }.}../*
107df 0a 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 20 63 .** Drop every c
107e0 61 63 68 65 20 65 6e 74 72 79 20 77 68 6f 73 65 ache entry whose
107e1 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 page number is
107e2 67 72 65 61 74 65 72 20 74 68 61 6e 20 22 70 67 greater than "pg
107e3 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c no". The.** call
107e4 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 er must ensure t
107e5 68 61 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f hat there are no
107e6 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 outstanding ref
107e7 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 70 erences to any p
107e8 61 67 65 73 0a 2a 2a 20 6f 74 68 65 72 20 74 68 ages.** other th
107e9 61 6e 20 70 61 67 65 20 31 20 77 69 74 68 20 61 an page 1 with a
107ea 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72 65 page number gre
107eb 61 74 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e 0a ater than pgno..
107ec 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 **.** If there i
107ed 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f s a reference to
107ee 20 70 61 67 65 20 31 20 61 6e 64 20 74 68 65 20 page 1 and the
107ef 70 67 6e 6f 20 70 61 72 61 6d 65 74 65 72 20 70 pgno parameter p
107f0 61 73 73 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a assed to this.**
107f1 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 30 2c 20 function is 0,
107f2 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 61 72 then the data ar
107f3 65 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ea associated wi
107f4 74 68 20 70 61 67 65 20 31 20 69 73 20 7a 65 72 th page 1 is zer
107f5 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 20 oed, but.** the
107f6 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 6e page object is n
107f7 6f 74 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a 53 ot dropped..*/.S
107f8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
107f9 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
107fa 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 65 20 Truncate(PCache
107fb 2a 70 43 61 63 68 65 2c 20 50 67 6e 6f 20 70 67 *pCache, Pgno pg
107fc 6e 6f 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68 no){. if( pCach
107fd 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 e->pCache ){.
107fe 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 50 PgHdr *p;. P
107ff 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20 20 20 gHdr *pNext;.
10800 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 for(p=pCache->p
10801 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 4e 65 78 Dirty; p; p=pNex
10802 74 29 7b 0a 20 20 20 20 20 20 70 4e 65 78 74 20 t){. pNext
10803 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b = p->pDirtyNext;
10804 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 67 . if( p->pg
10805 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 no>pgno ){.
10806 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c assert( p->fl
10807 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 ags&PGHDR_DIRTY
10808 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
10809 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 e3PcacheMakeClea
1080a 6e 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n(p);. }.
1080b 20 20 7d 0a 20 20 20 20 69 66 28 20 70 67 6e 6f }. if( pgno
1080c 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70 ==0 && pCache->p
1080d 50 61 67 65 31 20 29 7b 0a 20 20 20 20 20 20 6d Page1 ){. m
1080e 65 6d 73 65 74 28 70 43 61 63 68 65 2d 3e 70 50 emset(pCache->pP
1080f 61 67 65 31 2d 3e 70 44 61 74 61 2c 20 30 2c 20 age1->pData, 0,
10810 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 3b pCache->szPage);
10811 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 31 3b . pgno = 1;
10812 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
10813 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
10814 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65 28 cache.xTruncate(
10815 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 pCache->pCache,
10816 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a pgno+1);. }.}..
10817 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 61 /*.** Close a ca
10818 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 che..*/.SQLITE_P
10819 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1081a 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 50 te3PcacheClose(P
1081b 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a Cache *pCache){.
1081c 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 if( pCache->pC
1081d 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 ache ){. sqli
1081e 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
1081f 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 28 pcache.xDestroy(
10820 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b pCache->pCache);
10821 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 . }.}../* .** D
10822 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 iscard the conte
10823 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65 nts of the cache
10824 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
10825 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
10826 50 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 63 PcacheClear(PCac
10827 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 73 he *pCache){. s
10828 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e qlite3PcacheTrun
10829 63 61 74 65 28 70 43 61 63 68 65 2c 20 30 29 3b cate(pCache, 0);
1082a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 .}../*.** Merge
1082b 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 61 67 two lists of pag
1082c 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 es connected by
1082d 70 44 69 72 74 79 20 61 6e 64 20 69 6e 20 70 67 pDirty and in pg
1082e 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 6f 20 no order..** Do
1082f 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 6e 67 20 not both fixing
10830 74 68 65 20 70 44 69 72 74 79 50 72 65 76 20 70 the pDirtyPrev p
10831 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 ointers..*/.stat
10832 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68 65 ic PgHdr *pcache
10833 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74 28 50 MergeDirtyList(P
10834 67 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72 20 gHdr *pA, PgHdr
10835 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72 65 *pB){. PgHdr re
10836 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20 20 sult, *pTail;.
10837 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74 3b pTail = &result;
10838 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20 . while( pA &&
10839 70 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70 41 pB ){. if( pA
1083a 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f 20 ->pgno<pB->pgno
1083b 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e ){. pTail->
1083c 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 20 pDirty = pA;.
1083d 20 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a 20 pTail = pA;.
1083e 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 44 pA = pA->pD
1083f 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65 7b irty;. }else{
10840 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 . pTail->pD
10841 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20 20 irty = pB;.
10842 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20 20 pTail = pB;.
10843 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69 72 pB = pB->pDir
10844 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ty;. }. }.
10845 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70 54 if( pA ){. pT
10846 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 ail->pDirty = pA
10847 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 ;. }else if( pB
10848 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70 ){. pTail->p
10849 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d 65 Dirty = pB;. }e
1084a 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e lse{. pTail->
1084b 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d 0a pDirty = 0;. }.
1084c 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74 2e return result.
1084d 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pDirty;.}../*.**
1084e 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20 6f Sort the list o
1084f 66 20 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e f pages in accen
10850 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 67 ding order by pg
10851 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a 2a no. Pages are.*
10852 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 * connected by p
10853 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e 20 Dirty pointers.
10854 20 54 68 65 20 70 44 69 72 74 79 50 72 65 76 20 The pDirtyPrev
10855 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a 20 pointers are.**
10856 63 6f 72 72 75 70 74 65 64 20 62 79 20 74 68 69 corrupted by thi
10857 73 20 73 6f 72 74 2e 0a 2a 2a 0a 2a 2a 20 53 69 s sort..**.** Si
10858 6e 63 65 20 74 68 65 72 65 20 63 61 6e 6e 6f 74 nce there cannot
10859 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 32 5e be more than 2^
1085a 33 31 20 64 69 73 74 69 6e 63 74 20 70 61 67 65 31 distinct page
1085b 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 2c s in a database,
1085c 0a 2a 2a 20 74 68 65 72 65 20 63 61 6e 6e 6f 74 .** there cannot
1085d 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 33 31 be more than 31
1085e 20 62 75 63 6b 65 74 73 20 72 65 71 75 69 72 65 buckets require
1085f 64 20 62 79 20 74 68 65 20 6d 65 72 67 65 20 73 d by the merge s
10860 6f 72 74 65 72 2e 0a 2a 2a 20 4f 6e 65 20 65 78 orter..** One ex
10861 74 72 61 20 62 75 63 6b 65 74 20 69 73 20 61 64 tra bucket is ad
10862 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f 76 65 ded to catch ove
10863 72 66 6c 6f 77 20 69 6e 20 63 61 73 65 20 73 6f rflow in case so
10864 6d 65 74 68 69 6e 67 0a 2a 2a 20 65 76 65 72 20 mething.** ever
10865 63 68 61 6e 67 65 73 20 74 6f 20 6d 61 6b 65 20 changes to make
10866 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e the previous sen
10867 74 65 6e 63 65 20 69 6e 63 6f 72 72 65 63 74 2e tence incorrect.
10868 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 53 4f .*/.#define N_SO
10869 52 54 5f 42 55 43 4b 45 54 20 20 33 32 0a 73 74 RT_BUCKET 32.st
1086a 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 atic PgHdr *pcac
1086b 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74 28 heSortDirtyList(
1086c 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20 50 PgHdr *pIn){. P
1086d 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f 42 gHdr *a[N_SORT_B
1086e 55 43 4b 45 54 5d 2c 20 2a 70 3b 0a 20 20 69 6e UCKET], *p;. in
1086f 74 20 69 3b 0a 20 20 6d 65 6d 73 65 74 28 61 2c t i;. memset(a,
10870 20 30 2c 20 73 69 7a 65 6f 66 28 61 29 29 3b 0a 0, sizeof(a));.
10871 20 20 77 68 69 6c 65 28 20 70 49 6e 20 29 7b 0a while( pIn ){.
10872 20 20 20 20 70 20 3d 20 70 49 6e 3b 0a 20 20 20 p = pIn;.
10873 20 70 49 6e 20 3d 20 70 2d 3e 70 44 69 72 74 79 pIn = p->pDirty
10874 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 20 ;. p->pDirty
10875 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 = 0;. for(i=0
10876 3b 20 41 4c 57 41 59 53 28 69 3c 4e 5f 53 4f 52 ; ALWAYS(i<N_SOR
10877 54 5f 42 55 43 4b 45 54 2d 31 29 3b 20 69 2b 2b T_BUCKET-1); i++
10878 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 5b 69 ){. if( a[i
10879 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 ]==0 ){.
1087a 61 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 20 20 a[i] = p;.
1087b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
1087c 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 20 else{. p
1087d 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 = pcacheMergeDir
1087e 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b tyList(a[i], p);
1087f 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 . a[i] =
10880 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0;. }. }
10881 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 69 . if( NEVER(i
10882 3d 3d 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 2d ==N_SORT_BUCKET-
10883 31 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 1) ){. /* T
10884 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 65 72 o get here, ther
10885 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 5e 28 e need to be 2^(
10886 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 20 65 N_SORT_BUCKET) e
10887 6c 65 6d 65 6e 74 73 20 69 6e 0a 20 20 20 20 20 lements in.
10888 20 2a 2a 20 74 68 65 20 69 6e 70 75 74 20 6c 69 ** the input li
10889 73 74 2e 20 20 42 75 74 20 74 68 61 74 20 69 73 st. But that is
1088a 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0a 20 20 20 impossible..
1088b 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69 5d */. a[i]
1088c 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 = pcacheMergeDi
1088d 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 rtyList(a[i], p)
1088e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 20 ;. }. }. p
1088f 3d 20 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d = a[0];. for(i=
10890 31 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 1; i<N_SORT_BUCK
10891 45 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 ET; i++){. p
10892 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 = pcacheMergeDir
10893 74 79 4c 69 73 74 28 70 2c 20 61 5b 69 5d 29 3b tyList(p, a[i]);
10894 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
10895 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
10896 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64 a list of all d
10897 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 irty pages in th
10898 65 20 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20 e cache, sorted
10899 62 79 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a by page number..
1089a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1089b 45 20 50 67 48 64 72 20 2a 73 71 6c 69 74 65 33 E PgHdr *sqlite3
1089c 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 PcacheDirtyList(
1089d 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
1089e 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 . PgHdr *p;. f
1089f 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 or(p=pCache->pDi
108a0 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 rty; p; p=p->pDi
108a1 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d rtyNext){. p-
108a2 3e 70 44 69 72 74 79 20 3d 20 70 2d 3e 70 44 69 >pDirty = p->pDi
108a3 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 rtyNext;. }. r
108a4 65 74 75 72 6e 20 70 63 61 63 68 65 53 6f 72 74 eturn pcacheSort
108a5 44 69 72 74 79 4c 69 73 74 28 70 43 61 63 68 65 DirtyList(pCache
108a6 2d 3e 70 44 69 72 74 79 29 3b 0a 7d 0a 0a 2f 2a ->pDirty);.}../*
108a7 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 .** Return the
108a8 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
108a9 72 65 66 65 72 65 6e 63 65 64 20 70 61 67 65 73 referenced pages
108aa 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 63 held by the cac
108ab 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 he..*/.SQLITE_PR
108ac 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
108ad 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 3PcacheRefCount(
108ae 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
108af 0a 20 20 72 65 74 75 72 6e 20 70 43 61 63 68 65 . return pCache
108b0 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ->nRef;.}../*.**
108b1 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
108b2 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 er of references
108b3 20 74 6f 20 74 68 65 20 70 61 67 65 20 73 75 70 to the page sup
108b4 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 plied as an argu
108b5 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ment..*/.SQLITE_
108b6 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
108b7 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66 te3PcachePageRef
108b8 63 6f 75 6e 74 28 50 67 48 64 72 20 2a 70 29 7b count(PgHdr *p){
108b9 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 . return p->nRe
108ba 66 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 f;.}../* .** Ret
108bb 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 urn the total nu
108bc 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
108bd 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 the cache..*/.S
108be 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
108bf 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 t sqlite3PcacheP
108c0 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65 20 agecount(PCache
108c1 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20 *pCache){. int
108c2 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 66 28 nPage = 0;. if(
108c3 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 pCache->pCache
108c4 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 73 ){. nPage = s
108c5 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
108c6 69 67 2e 70 63 61 63 68 65 2e 78 50 61 67 65 63 ig.pcache.xPagec
108c7 6f 75 6e 74 28 70 43 61 63 68 65 2d 3e 70 43 61 ount(pCache->pCa
108c8 63 68 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 che);. }. retu
108c9 72 6e 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23 69 66 rn nPage;.}..#if
108ca 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
108cb 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 75 /*.** Get the su
108cc 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 ggested cache-si
108cd 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c ze value..*/.SQL
108ce 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
108cf 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 sqlite3PcacheGet
108d0 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 Cachesize(PCache
108d1 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65 74 *pCache){. ret
108d2 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 urn pCache->nMax
108d3 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
108d4 2a 20 53 65 74 20 74 68 65 20 73 75 67 67 65 73 * Set the sugges
108d5 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 ted cache-size v
108d6 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f alue..*/.SQLITE_
108d7 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
108d8 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61 63 ite3PcacheSetCac
108d9 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70 hesize(PCache *p
108da 43 61 63 68 65 2c 20 69 6e 74 20 6d 78 50 61 67 Cache, int mxPag
108db 65 29 7b 0a 20 20 70 43 61 63 68 65 2d 3e 6e 4d e){. pCache->nM
108dc 61 78 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 69 ax = mxPage;. i
108dd 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 f( pCache->pCach
108de 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 e ){. sqlite3
108df 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 GlobalConfig.pca
108e0 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28 70 che.xCachesize(p
108e1 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 6d Cache->pCache, m
108e2 78 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 xPage);. }.}..#
108e3 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
108e4 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 20 7c E_CHECK_PAGES) |
108e5 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 | defined(SQLITE
108e6 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 46 6f _DEBUG)./*.** Fo
108e7 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 r all dirty page
108e8 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 s currently in t
108e9 68 65 20 63 61 63 68 65 2c 20 69 6e 76 6f 6b 65 he cache, invoke
108ea 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a the specified.*
108eb 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 69 73 * callback. This
108ec 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 is only used if
108ed 20 74 68 65 20 53 51 4c 49 54 45 5f 43 48 45 43 the SQLITE_CHEC
108ee 4b 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 69 73 K_PAGES macro is
108ef 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a .** defined..*/.
108f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
108f1 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
108f2 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50 43 eIterateDirty(PC
108f3 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 6f ache *pCache, vo
108f4 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 64 id (*xIter)(PgHd
108f5 72 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20 2a r *)){. PgHdr *
108f6 70 44 69 72 74 79 3b 0a 20 20 66 6f 72 28 70 44 pDirty;. for(pD
108f7 69 72 74 79 3d 70 43 61 63 68 65 2d 3e 70 44 69 irty=pCache->pDi
108f8 72 74 79 3b 20 70 44 69 72 74 79 3b 20 70 44 69 rty; pDirty; pDi
108f9 72 74 79 3d 70 44 69 72 74 79 2d 3e 70 44 69 72 rty=pDirty->pDir
108fa 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 78 49 74 tyNext){. xIt
108fb 65 72 28 70 44 69 72 74 79 29 3b 0a 20 20 7d 0a er(pDirty);. }.
108fc 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a }.#endif../*****
108fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
108fe 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a pcache.c ******
108ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10901 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
10902 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
10903 66 69 6c 65 20 70 63 61 63 68 65 31 2e 63 20 2a file pcache1.c *
10904 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10905 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10906 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
10907 32 30 30 38 20 4e 6f 76 65 6d 62 65 72 20 30 35 2008 November 05
10908 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
10909 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1090a 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1090b 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
1090c 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
1090d 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
1090e 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
1090f 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
10910 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
10911 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
10912 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
10913 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
10914 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
10915 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
10916 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
10917 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
10918 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
10919 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
1091a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1091b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1091c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1091d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1091e 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
1091f 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 64 implements the d
10920 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 efault page cach
10921 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
10922 20 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 (the.** sqlite3
10923 5f 70 63 61 63 68 65 20 69 6e 74 65 72 66 61 63 _pcache interfac
10924 65 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74 e). It also cont
10925 61 69 6e 73 20 70 61 72 74 20 6f 66 20 74 68 65 ains part of the
10926 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a implementation.
10927 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 ** of the SQLITE
10928 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 _CONFIG_PAGECACH
10929 45 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 E and sqlite3_re
1092a 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 66 lease_memory() f
1092b 65 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20 74 eatures..** If t
1092c 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 he default page
1092d 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 cache implementa
1092e 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 65 tion is override
1092f 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 n, then neither
10930 6f 66 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f 20 of.** these two
10931 66 65 61 74 75 72 65 73 20 61 72 65 20 61 76 61 features are ava
10932 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 ilable..**.** @(
10933 23 29 20 24 49 64 3a 20 70 63 61 63 68 65 31 2e #) $Id: pcache1.
10934 63 2c 76 20 31 2e 31 39 20 32 30 30 39 2f 30 37 c,v 1.19 2009/07
10935 2f 31 37 20 31 31 3a 34 34 3a 30 37 20 64 72 68 /17 11:44:07 drh
10936 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 74 79 70 65 Exp $.*/...type
10937 64 65 66 20 73 74 72 75 63 74 20 50 43 61 63 68 def struct PCach
10938 65 31 20 50 43 61 63 68 65 31 3b 0a 74 79 70 65 e1 PCache1;.type
10939 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72 def struct PgHdr
1093a 31 20 50 67 48 64 72 31 3b 0a 74 79 70 65 64 65 1 PgHdr1;.typede
1093b 66 20 73 74 72 75 63 74 20 50 67 46 72 65 65 73 f struct PgFrees
1093c 6c 6f 74 20 50 67 46 72 65 65 73 6c 6f 74 3b 0a lot PgFreeslot;.
1093d 0a 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 ./* Pointers to
1093e 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 74 68 structures of th
1093f 69 73 20 74 79 70 65 20 61 72 65 20 63 61 73 74 is type are cast
10940 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 61 73 and returned as
10941 20 0a 2a 2a 20 6f 70 61 71 75 65 20 73 71 6c 69 .** opaque sqli
10942 74 65 33 5f 70 63 61 63 68 65 2a 20 68 61 6e 64 te3_pcache* hand
10943 6c 65 73 0a 2a 2f 0a 73 74 72 75 63 74 20 50 43 les.*/.struct PC
10944 61 63 68 65 31 20 7b 0a 20 20 2f 2a 20 43 61 63 ache1 {. /* Cac
10945 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e he configuration
10946 20 70 61 72 61 6d 65 74 65 72 73 2e 20 50 61 67 parameters. Pag
10947 65 20 73 69 7a 65 20 28 73 7a 50 61 67 65 29 20 e size (szPage)
10948 61 6e 64 20 74 68 65 20 70 75 72 67 65 61 62 6c and the purgeabl
10949 65 0a 20 20 2a 2a 20 66 6c 61 67 20 28 62 50 75 e. ** flag (bPu
1094a 72 67 65 61 62 6c 65 29 20 61 72 65 20 73 65 74 rgeable) are set
1094b 20 77 68 65 6e 20 74 68 65 20 63 61 63 68 65 20 when the cache
1094c 69 73 20 63 72 65 61 74 65 64 2e 20 6e 4d 61 78 is created. nMax
1094d 20 6d 61 79 20 62 65 20 0a 20 20 2a 2a 20 6d 6f may be . ** mo
1094e 64 69 66 69 65 64 20 61 74 20 61 6e 79 20 74 69 dified at any ti
1094f 6d 65 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 me by a call to
10950 74 68 65 20 70 63 61 63 68 65 31 43 61 63 68 65 the pcache1Cache
10951 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 2e 0a 20 Size() method..
10952 20 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d ** The global m
10953 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c utex must be hel
10954 64 20 77 68 65 6e 20 61 63 63 65 73 73 69 6e 67 d when accessing
10955 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a 20 20 69 6e nMax.. */. in
10956 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 t szPage;
10957 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10958 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c /* Size of all
10959 6f 63 61 74 65 64 20 70 61 67 65 73 20 69 6e 20 ocated pages in
1095a 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 bytes */. int b
1095b 50 75 72 67 65 61 62 6c 65 3b 20 20 20 20 20 20 Purgeable;
1095c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1095d 2a 20 54 72 75 65 20 69 66 20 63 61 63 68 65 20 * True if cache
1095e 69 73 20 70 75 72 67 65 61 62 6c 65 20 2a 2f 0a is purgeable */.
1095f 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e unsigned int n
10960 4d 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 Min;
10961 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d /* Minimum
10962 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
10963 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20 20 75 reserved */. u
10964 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d 61 78 nsigned int nMax
10965 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10966 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 64 /* Configured
10967 20 22 63 61 63 68 65 5f 73 69 7a 65 22 20 76 61 "cache_size" va
10968 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 48 61 73 lue */.. /* Has
10969 68 20 74 61 62 6c 65 20 6f 66 20 61 6c 6c 20 70 h table of all p
1096a 61 67 65 73 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 ages. The follow
1096b 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 6d 61 ing variables ma
1096c 79 20 6f 6e 6c 79 20 62 65 20 61 63 63 65 73 73 y only be access
1096d 65 64 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 ed. ** when the
1096e 20 61 63 63 65 73 73 6f 72 20 69 73 20 68 6f 6c accessor is hol
1096f 64 69 6e 67 20 74 68 65 20 67 6c 6f 62 61 6c 20 ding the global
10970 6d 75 74 65 78 20 28 73 65 65 20 70 63 61 63 68 mutex (see pcach
10971 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 20 0a e1EnterMutex() .
10972 20 20 2a 2a 20 61 6e 64 20 70 63 61 63 68 65 31 ** and pcache1
10973 4c 65 61 76 65 4d 75 74 65 78 28 29 29 2e 0a 20 LeaveMutex())..
10974 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 */. unsigned i
10975 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b 20 nt nRecyclable;
10976 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
10977 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
10978 74 68 65 20 4c 52 55 20 6c 69 73 74 20 2a 2f 0a the LRU list */.
10979 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e unsigned int n
1097a 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
1097b 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e /* Total n
1097c 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
1097d 6e 20 61 70 48 61 73 68 20 2a 2f 0a 20 20 75 6e n apHash */. un
1097e 73 69 67 6e 65 64 20 69 6e 74 20 6e 48 61 73 68 signed int nHash
1097f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10980 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 /* Number of s
10981 6c 6f 74 73 20 69 6e 20 61 70 48 61 73 68 5b 5d lots in apHash[]
10982 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 2a 61 */. PgHdr1 **a
10983 70 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 pHash;
10984 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 /* Has
10985 68 20 74 61 62 6c 65 20 66 6f 72 20 66 61 73 74 h table for fast
10986 20 6c 6f 6f 6b 75 70 20 62 79 20 6b 65 79 20 2a lookup by key *
10987 2f 0a 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e /.. unsigned in
10988 74 20 69 4d 61 78 4b 65 79 3b 20 20 20 20 20 20 t iMaxKey;
10989 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 /* Larg
1098a 65 73 74 20 6b 65 79 20 73 65 65 6e 20 73 69 6e est key seen sin
1098b 63 65 20 78 54 72 75 6e 63 61 74 65 28 29 20 2a ce xTruncate() *
1098c 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 /.};../*.** Each
1098d 20 63 61 63 68 65 20 65 6e 74 72 79 20 69 73 20 cache entry is
1098e 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 represented by a
1098f 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
10990 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 e following .**
10991 73 74 72 75 63 74 75 72 65 2e 20 41 20 62 75 66 structure. A buf
10992 66 65 72 20 6f 66 20 50 67 48 64 72 31 2e 70 43 fer of PgHdr1.pC
10993 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 62 79 74 ache->szPage byt
10994 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 es is allocated
10995 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 62 65 66 .** directly bef
10996 6f 72 65 20 74 68 69 73 20 73 74 72 75 63 74 75 ore this structu
10997 72 65 20 69 6e 20 6d 65 6d 6f 72 79 20 28 73 65 re in memory (se
10998 65 20 74 68 65 20 50 47 48 44 52 31 5f 54 4f 5f e the PGHDR1_TO_
10999 50 41 47 45 28 29 20 0a 2a 2a 20 6d 61 63 72 6f PAGE() .** macro
1099a 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a 73 74 72 75 below)..*/.stru
1099b 63 74 20 50 67 48 64 72 31 20 7b 0a 20 20 75 6e ct PgHdr1 {. un
1099c 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79 3b signed int iKey;
1099d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1099e 4b 65 79 20 76 61 6c 75 65 20 28 70 61 67 65 20 Key value (page
1099f 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 20 50 67 48 number) */. PgH
109a0 64 72 31 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 dr1 *pNext;
109a1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
109a2 65 78 74 20 69 6e 20 68 61 73 68 20 74 61 62 6c ext in hash tabl
109a3 65 20 63 68 61 69 6e 20 2a 2f 0a 20 20 50 43 61 e chain */. PCa
109a4 63 68 65 31 20 2a 70 43 61 63 68 65 3b 20 20 20 che1 *pCache;
109a5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
109a6 61 63 68 65 20 74 68 61 74 20 63 75 72 72 65 6e ache that curren
109a7 74 6c 79 20 6f 77 6e 73 20 74 68 69 73 20 70 61 tly owns this pa
109a8 67 65 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a ge */. PgHdr1 *
109a9 70 4c 72 75 4e 65 78 74 3b 20 20 20 20 20 20 20 pLruNext;
109aa 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 /* Next i
109ab 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e n LRU list of un
109ac 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a pinned pages */.
109ad 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 50 72 PgHdr1 *pLruPr
109ae 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ev;
109af 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 69 6e 20 /* Previous in
109b0 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e 70 69 LRU list of unpi
109b1 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a 7d 3b nned pages */.};
109b2 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 73 6c 6f ../*.** Free slo
109b3 74 73 20 69 6e 20 74 68 65 20 61 6c 6c 6f 63 61 ts in the alloca
109b4 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69 76 69 tor used to divi
109b5 64 65 20 75 70 20 74 68 65 20 62 75 66 66 65 72 de up the buffer
109b6 20 70 72 6f 76 69 64 65 64 20 75 73 69 6e 67 0a provided using.
109b7 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f ** the SQLITE_CO
109b8 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 6d NFIG_PAGECACHE m
109b9 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 73 74 72 echanism..*/.str
109ba 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74 20 7b uct PgFreeslot {
109bb 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 . PgFreeslot *p
109bc 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 66 Next; /* Next f
109bd 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 7d 3b 0a 0a ree slot */.};..
109be 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 64 61 74 /*.** Global dat
109bf 61 20 75 73 65 64 20 62 79 20 74 68 69 73 20 63 a used by this c
109c0 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ache..*/.static
109c1 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 SQLITE_WSD struc
109c2 74 20 50 43 61 63 68 65 47 6c 6f 62 61 6c 20 7b t PCacheGlobal {
109c3 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
109c4 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 *mutex;
109c5 20 20 20 20 20 20 20 2f 2a 20 73 74 61 74 69 63 /* static
109c6 20 6d 75 74 65 78 20 4d 55 54 45 58 5f 53 54 41 mutex MUTEX_STA
109c7 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a 20 20 69 6e TIC_LRU */.. in
109c8 74 20 6e 4d 61 78 50 61 67 65 3b 20 20 20 20 20 t nMaxPage;
109c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
109ca 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d 61 78 /* Sum of nMax
109cb 50 61 67 65 20 66 6f 72 20 70 75 72 67 65 61 62 Page for purgeab
109cc 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 69 le caches */. i
109cd 6e 74 20 6e 4d 69 6e 50 61 67 65 3b 20 20 20 20 nt nMinPage;
109ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
109cf 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d 69 /* Sum of nMi
109d0 6e 50 61 67 65 20 66 6f 72 20 70 75 72 67 65 61 nPage for purgea
109d1 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 ble caches */.
109d2 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 61 67 65 int nCurrentPage
109d3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
109d4 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
109d5 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 65 73 purgeable pages
109d6 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 allocated */.
109d7 50 67 48 64 72 31 20 2a 70 4c 72 75 48 65 61 64 PgHdr1 *pLruHead
109d8 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20 20 20 20 , *pLruTail;
109d9 20 20 20 20 2f 2a 20 4c 52 55 20 6c 69 73 74 20 /* LRU list
109da 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 of unpinned page
109db 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 61 72 69 61 s */.. /* Varia
109dc 62 6c 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20 bles related to
109dd 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 SQLITE_CONFIG_PA
109de 47 45 43 41 43 48 45 20 73 65 74 74 69 6e 67 73 GECACHE settings
109df 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 6c 6f . */. int szSlo
109e0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
109e1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
109e2 7a 65 20 6f 66 20 65 61 63 68 20 66 72 65 65 20 ze of each free
109e3 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a slot */. void *
109e4 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b 20 20 pStart, *pEnd;
109e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
109e6 20 42 6f 75 6e 64 73 20 6f 66 20 70 61 67 65 63 Bounds of pagec
109e7 61 63 68 65 20 6d 61 6c 6c 6f 63 20 72 61 6e 67 ache malloc rang
109e8 65 20 2a 2f 0a 20 20 50 67 46 72 65 65 73 6c 6f e */. PgFreeslo
109e9 74 20 2a 70 46 72 65 65 3b 20 20 20 20 20 20 20 t *pFree;
109ea 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 /* Fr
109eb 65 65 20 70 61 67 65 20 62 6c 6f 63 6b 73 20 2a ee page blocks *
109ec 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20 /. int isInit;
109ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
109ee 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
109ef 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a if initialized *
109f0 2f 0a 7d 20 70 63 61 63 68 65 31 5f 67 3b 0a 0a /.} pcache1_g;..
109f1 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 64 65 20 69 /*.** All code i
109f2 6e 20 74 68 69 73 20 66 69 6c 65 20 73 68 6f 75 n this file shou
109f3 6c 64 20 61 63 63 65 73 73 20 74 68 65 20 67 6c ld access the gl
109f4 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 61 obal structure a
109f5 62 6f 76 65 20 76 69 61 20 74 68 65 0a 2a 2a 20 bove via the.**
109f6 61 6c 69 61 73 20 22 70 63 61 63 68 65 31 22 2e alias "pcache1".
109f7 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 This ensures th
109f8 61 74 20 74 68 65 20 57 53 44 20 65 6d 75 6c 61 at the WSD emula
109f9 74 69 6f 6e 20 69 73 20 75 73 65 64 20 77 68 65 tion is used whe
109fa 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 66 n.** compiling f
109fb 6f 72 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 or systems that
109fc 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 72 do not support r
109fd 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23 64 65 66 eal WSD..*/.#def
109fe 69 6e 65 20 70 63 61 63 68 65 31 20 28 47 4c 4f ine pcache1 (GLO
109ff 42 41 4c 28 73 74 72 75 63 74 20 50 43 61 63 68 BAL(struct PCach
10a00 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63 68 65 31 eGlobal, pcache1
10a01 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e _g))../*.** When
10a02 20 61 20 50 67 48 64 72 31 20 73 74 72 75 63 74 a PgHdr1 struct
10a03 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ure is allocated
10a04 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 , the associated
10a05 20 50 43 61 63 68 65 31 2e 73 7a 50 61 67 65 0a PCache1.szPage.
10a06 2a 2a 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 ** bytes of data
10a07 20 61 72 65 20 6c 6f 63 61 74 65 64 20 64 69 72 are located dir
10a08 65 63 74 6c 79 20 62 65 66 6f 72 65 20 69 74 20 ectly before it
10a09 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e 65 2e 20 in memory (i.e.
10a0a 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 73 69 7a the total.** siz
10a0b 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 e of the allocat
10a0c 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66 28 50 67 ion is sizeof(Pg
10a0d 48 64 72 31 29 2b 50 43 61 63 68 65 31 2e 73 7a Hdr1)+PCache1.sz
10a0e 50 61 67 65 20 62 79 74 65 29 2e 20 54 68 65 0a Page byte). The.
10a0f 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 ** PGHDR1_TO_PAG
10a10 45 28 29 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 E() macro takes
10a11 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 50 a pointer to a P
10a12 67 48 64 72 31 20 73 74 72 75 63 74 75 72 65 20 gHdr1 structure
10a13 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 6d 65 6e as.** an argumen
10a14 74 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 t and returns a
10a15 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 pointer to the a
10a16 73 73 6f 63 69 61 74 65 64 20 62 6c 6f 63 6b 20 ssociated block
10a17 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20 62 79 74 of szPage.** byt
10a18 65 73 2e 20 54 68 65 20 50 41 47 45 5f 54 4f 5f es. The PAGE_TO_
10a19 50 47 48 44 52 31 28 29 20 6d 61 63 72 6f 20 64 PGHDR1() macro d
10a1a 6f 65 73 20 74 68 65 20 6f 70 70 6f 73 69 74 65 oes the opposite
10a1b 3a 20 69 74 73 20 61 72 67 75 6d 65 6e 74 20 69 : its argument i
10a1c 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 s.** a pointer t
10a1d 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 73 7a 50 o a block of szP
10a1e 61 67 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 age bytes of dat
10a1f 61 20 61 6e 64 20 74 68 65 20 72 65 74 75 72 6e a and the return
10a20 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 20 70 value is.** a p
10a21 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 73 ointer to the as
10a22 73 6f 63 69 61 74 65 64 20 50 67 48 64 72 31 20 sociated PgHdr1
10a23 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a structure..**.**
10a24 20 20 20 61 73 73 65 72 74 28 20 50 47 48 44 52 assert( PGHDR
10a25 31 5f 54 4f 5f 50 41 47 45 28 50 41 47 45 5f 54 1_TO_PAGE(PAGE_T
10a26 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c O_PGHDR1(pCache,
10a27 20 58 29 29 3d 3d 58 20 29 3b 0a 2a 2f 0a 23 64 X))==X );.*/.#d
10a28 65 66 69 6e 65 20 50 47 48 44 52 31 5f 54 4f 5f efine PGHDR1_TO_
10a29 50 41 47 45 28 70 29 20 20 20 20 28 76 6f 69 64 PAGE(p) (void
10a2a 2a 29 28 28 28 63 68 61 72 2a 29 70 29 20 2d 20 *)(((char*)p) -
10a2b 70 2d 3e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 p->pCache->szPag
10a2c 65 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 5f e).#define PAGE_
10a2d 54 4f 5f 50 47 48 44 52 31 28 63 2c 20 70 29 20 TO_PGHDR1(c, p)
10a2e 28 50 67 48 64 72 31 2a 29 28 28 28 63 68 61 72 (PgHdr1*)(((char
10a2f 2a 29 70 29 20 2b 20 63 2d 3e 73 7a 50 61 67 65 *)p) + c->szPage
10a30 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 )../*.** Macros
10a31 74 6f 20 65 6e 74 65 72 20 61 6e 64 20 6c 65 61 to enter and lea
10a32 76 65 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 ve the global LR
10a33 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 23 64 65 66 U mutex..*/.#def
10a34 69 6e 65 20 70 63 61 63 68 65 31 45 6e 74 65 72 ine pcache1Enter
10a35 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 33 5f Mutex() sqlite3_
10a36 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 63 61 63 mutex_enter(pcac
10a37 68 65 31 2e 6d 75 74 65 78 29 0a 23 64 65 66 69 he1.mutex).#defi
10a38 6e 65 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d ne pcache1LeaveM
10a39 75 74 65 78 28 29 20 73 71 6c 69 74 65 33 5f 6d utex() sqlite3_m
10a3a 75 74 65 78 5f 6c 65 61 76 65 28 70 63 61 63 68 utex_leave(pcach
10a3b 65 31 2e 6d 75 74 65 78 29 0a 0a 2f 2a 2a 2a 2a e1.mutex)../****
10a3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
10a41 2a 2a 2a 2a 2a 20 50 61 67 65 20 41 6c 6c 6f 63 ***** Page Alloc
10a42 61 74 69 6f 6e 2f 53 51 4c 49 54 45 5f 43 4f 4e ation/SQLITE_CON
10a43 46 49 47 5f 50 43 41 43 48 45 20 52 65 6c 61 74 FIG_PCACHE Relat
10a44 65 64 20 46 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a ed Functions ***
10a45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
10a46 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
10a47 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75 72 69 n is called duri
10a48 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f ng initializatio
10a49 6e 20 69 66 20 61 20 73 74 61 74 69 63 20 62 75 n if a static bu
10a4a 66 66 65 72 20 69 73 20 0a 2a 2a 20 73 75 70 70 ffer is .** supp
10a4b 6c 69 65 64 20 74 6f 20 75 73 65 20 66 6f 72 20 lied to use for
10a4c 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 62 the page-cache b
10a4d 79 20 70 61 73 73 69 6e 67 20 74 68 65 20 53 51 y passing the SQ
10a4e 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 LITE_CONFIG_PAGE
10a4f 43 41 43 48 45 0a 2a 2a 20 76 65 72 62 20 74 6f CACHE.** verb to
10a50 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
10a51 29 2e 20 50 61 72 61 6d 65 74 65 72 20 70 42 75 ). Parameter pBu
10a52 66 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 f points to an a
10a53 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 65 0a llocation large.
10a54 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 63 6f 6e ** enough to con
10a55 74 61 69 6e 20 27 6e 27 20 62 75 66 66 65 72 73 tain 'n' buffers
10a56 20 6f 66 20 27 73 7a 27 20 62 79 74 65 73 20 65 of 'sz' bytes e
10a57 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ach..*/.SQLITE_P
10a58 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
10a59 74 65 33 50 43 61 63 68 65 42 75 66 66 65 72 53 te3PCacheBufferS
10a5a 65 74 75 70 28 76 6f 69 64 20 2a 70 42 75 66 2c etup(void *pBuf,
10a5b 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e 29 7b int sz, int n){
10a5c 0a 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 69 . if( pcache1.i
10a5d 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 50 67 46 sInit ){. PgF
10a5e 72 65 65 73 6c 6f 74 20 2a 70 3b 0a 20 20 20 20 reeslot *p;.
10a5f 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 sz = ROUNDDOWN8(
10a60 73 7a 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 sz);. pcache1
10a61 2e 73 7a 53 6c 6f 74 20 3d 20 73 7a 3b 0a 20 20 .szSlot = sz;.
10a62 20 20 70 63 61 63 68 65 31 2e 70 53 74 61 72 74 pcache1.pStart
10a63 20 3d 20 70 42 75 66 3b 0a 20 20 20 20 70 63 61 = pBuf;. pca
10a64 63 68 65 31 2e 70 46 72 65 65 20 3d 20 30 3b 0a che1.pFree = 0;.
10a65 20 20 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 29 while( n-- )
10a66 7b 0a 20 20 20 20 20 20 70 20 3d 20 28 50 67 46 {. p = (PgF
10a67 72 65 65 73 6c 6f 74 2a 29 70 42 75 66 3b 0a 20 reeslot*)pBuf;.
10a68 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 p->pNext =
10a69 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b 0a 20 pcache1.pFree;.
10a6a 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 46 72 pcache1.pFr
10a6b 65 65 20 3d 20 70 3b 0a 20 20 20 20 20 20 70 42 ee = p;. pB
10a6c 75 66 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 uf = (void*)&((c
10a6d 68 61 72 2a 29 70 42 75 66 29 5b 73 7a 5d 3b 0a har*)pBuf)[sz];.
10a6e 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65 }. pcache
10a6f 31 2e 70 45 6e 64 20 3d 20 70 42 75 66 3b 0a 20 1.pEnd = pBuf;.
10a70 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c }.}../*.** Mall
10a71 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 oc function used
10a72 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c within this fil
10a73 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 73 70 e to allocate sp
10a74 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 75 66 ace from the buf
10a75 66 65 72 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 65 fer.** configure
10a76 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f d using sqlite3_
10a77 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f config(SQLITE_CO
10a78 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 29 20 NFIG_PAGECACHE)
10a79 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 0a 2a option. If no .*
10a7a 2a 20 73 75 63 68 20 62 75 66 66 65 72 20 65 78 * such buffer ex
10a7b 69 73 74 73 20 6f 72 20 74 68 65 72 65 20 69 73 ists or there is
10a7c 20 6e 6f 20 73 70 61 63 65 20 6c 65 66 74 20 69 no space left i
10a7d 6e 20 69 74 2c 20 74 68 69 73 20 66 75 6e 63 74 n it, this funct
10a7e 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a 2a 20 62 61 ion falls .** ba
10a7f 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c ck to sqlite3Mal
10a80 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 loc()..*/.static
10a81 20 76 6f 69 64 20 2a 70 63 61 63 68 65 31 41 6c void *pcache1Al
10a82 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a loc(int nByte){.
10a83 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 void *p;. ass
10a84 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
10a85 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e ex_held(pcache1.
10a86 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
10a87 6e 42 79 74 65 3c 3d 70 63 61 63 68 65 31 2e 73 nByte<=pcache1.s
10a88 7a 53 6c 6f 74 20 26 26 20 70 63 61 63 68 65 31 zSlot && pcache1
10a89 2e 70 46 72 65 65 20 29 7b 0a 20 20 20 20 61 73 .pFree ){. as
10a8a 73 65 72 74 28 20 70 63 61 63 68 65 31 2e 69 73 sert( pcache1.is
10a8b 49 6e 69 74 20 29 3b 0a 20 20 20 20 70 20 3d 20 Init );. p =
10a8c 28 50 67 48 64 72 31 20 2a 29 70 63 61 63 68 65 (PgHdr1 *)pcache
10a8d 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61 1.pFree;. pca
10a8e 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 63 61 che1.pFree = pca
10a8f 63 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e 65 78 che1.pFree->pNex
10a90 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 t;. sqlite3St
10a91 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 atusSet(SQLITE_S
10a92 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f TATUS_PAGECACHE_
10a93 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b 0a 20 20 SIZE, nByte);.
10a94 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 sqlite3StatusA
10a95 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dd(SQLITE_STATUS
10a96 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 2c _PAGECACHE_USED,
10a97 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20 1);. }else{..
10a98 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 /* Allocate a
10a99 20 6e 65 77 20 62 75 66 66 65 72 20 75 73 69 6e new buffer usin
10a9a 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 2e g sqlite3Malloc.
10a9b 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 73 6f Before doing so
10a9c 2c 20 65 78 69 74 20 74 68 65 0a 20 20 20 20 2a , exit the. *
10a9d 2a 20 67 6c 6f 62 61 6c 20 70 63 61 63 68 65 20 * global pcache
10a9e 6d 75 74 65 78 20 61 6e 64 20 75 6e 6c 6f 63 6b mutex and unlock
10a9f 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 the pager-cache
10aa0 20 6f 62 6a 65 63 74 20 70 43 61 63 68 65 2e 20 object pCache.
10aa1 54 68 69 73 20 69 73 20 0a 20 20 20 20 2a 2a 20 This is . **
10aa2 73 6f 20 74 68 61 74 20 69 66 20 74 68 65 20 61 so that if the a
10aa3 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 ttempt to alloca
10aa4 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 72 20 te a new buffer
10aa5 63 61 75 73 65 73 20 74 68 65 20 74 68 65 20 0a causes the the .
10aa6 20 20 20 20 2a 2a 20 63 6f 6e 66 69 67 75 72 65 ** configure
10aa7 64 20 73 6f 66 74 2d 68 65 61 70 2d 6c 69 6d 69 d soft-heap-limi
10aa8 74 20 74 6f 20 62 65 20 62 72 65 61 63 68 65 64 t to be breached
10aa9 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70 6f 73 , it will be pos
10aaa 73 69 62 6c 65 20 74 6f 0a 20 20 20 20 2a 2a 20 sible to. **
10aab 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 20 66 reclaim memory f
10aac 72 6f 6d 20 74 68 69 73 20 70 61 67 65 72 2d 63 rom this pager-c
10aad 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ache.. */.
10aae 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 pcache1LeaveMut
10aaf 65 78 28 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 ex();. p = sq
10ab0 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74 lite3Malloc(nByt
10ab1 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45 e);. pcache1E
10ab2 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 nterMutex();.
10ab3 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 if( p ){.
10ab4 69 6e 74 20 73 7a 20 3d 20 73 71 6c 69 74 65 33 int sz = sqlite3
10ab5 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 MallocSize(p);.
10ab6 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 sqlite3Stat
10ab7 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
10ab8 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 TUS_PAGECACHE_OV
10ab9 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20 20 20 ERFLOW, sz);.
10aba 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
10abb 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 p;.}../*.** Free
10abc 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 an allocated bu
10abd 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 72 ffer obtained fr
10abe 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 om pcache1Alloc(
10abf 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
10ac0 64 20 70 63 61 63 68 65 31 46 72 65 65 28 76 6f d pcache1Free(vo
10ac1 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 id *p){. assert
10ac2 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
10ac3 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 held(pcache1.mut
10ac4 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d ex) );. if( p==
10ac5 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 0 ) return;. if
10ac6 28 20 70 3e 3d 70 63 61 63 68 65 31 2e 70 53 74 ( p>=pcache1.pSt
10ac7 61 72 74 20 26 26 20 70 3c 70 63 61 63 68 65 31 art && p<pcache1
10ac8 2e 70 45 6e 64 20 29 7b 0a 20 20 20 20 50 67 46 .pEnd ){. PgF
10ac9 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74 3b 0a reeslot *pSlot;.
10aca 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
10acb 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 sAdd(SQLITE_STAT
10acc 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 US_PAGECACHE_USE
10acd 44 2c 20 2d 31 29 3b 0a 20 20 20 20 70 53 6c 6f D, -1);. pSlo
10ace 74 20 3d 20 28 50 67 46 72 65 65 73 6c 6f 74 2a t = (PgFreeslot*
10acf 29 70 3b 0a 20 20 20 20 70 53 6c 6f 74 2d 3e 70 )p;. pSlot->p
10ad0 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31 2e 70 Next = pcache1.p
10ad1 46 72 65 65 3b 0a 20 20 20 20 70 63 61 63 68 65 Free;. pcache
10ad2 31 2e 70 46 72 65 65 20 3d 20 70 53 6c 6f 74 3b 1.pFree = pSlot;
10ad3 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
10ad4 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 t iSize = sqlite
10ad5 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 3MallocSize(p);.
10ad6 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
10ad7 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 sAdd(SQLITE_STAT
10ad8 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 45 US_PAGECACHE_OVE
10ad9 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a RFLOW, -iSize);.
10ada 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
10adb 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
10adc 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 * Allocate a new
10add 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 6e 69 page object ini
10ade 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61 74 65 tially associate
10adf 64 20 77 69 74 68 20 63 61 63 68 65 20 70 43 61 d with cache pCa
10ae0 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 che..*/.static P
10ae1 67 48 64 72 31 20 2a 70 63 61 63 68 65 31 41 6c gHdr1 *pcache1Al
10ae2 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65 31 20 locPage(PCache1
10ae3 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20 *pCache){. int
10ae4 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 50 nByte = sizeof(P
10ae5 67 48 64 72 31 29 20 2b 20 70 43 61 63 68 65 2d gHdr1) + pCache-
10ae6 3e 73 7a 50 61 67 65 3b 0a 20 20 76 6f 69 64 20 >szPage;. void
10ae7 2a 70 50 67 20 3d 20 70 63 61 63 68 65 31 41 6c *pPg = pcache1Al
10ae8 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 50 67 loc(nByte);. Pg
10ae9 48 64 72 31 20 2a 70 3b 0a 20 20 69 66 28 20 70 Hdr1 *p;. if( p
10aea 50 67 20 29 7b 0a 20 20 20 20 70 20 3d 20 50 41 Pg ){. p = PA
10aeb 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61 GE_TO_PGHDR1(pCa
10aec 63 68 65 2c 20 70 50 67 29 3b 0a 20 20 20 20 69 che, pPg);. i
10aed 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 f( pCache->bPurg
10aee 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 eable ){. p
10aef 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 cache1.nCurrentP
10af0 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d age++;. }. }
10af1 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 30 3b else{. p = 0;
10af2 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
10af3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 .}../*.** Free a
10af4 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61 6c 6c page object all
10af5 6f 63 61 74 65 64 20 62 79 20 70 63 61 63 68 65 ocated by pcache
10af6 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e 0a 2a 2a 1AllocPage()..**
10af7 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 .** The pointer
10af8 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 is allowed to be
10af9 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 20 NULL, which is
10afa 70 72 75 64 65 6e 74 2e 20 20 42 75 74 20 69 74 prudent. But it
10afb 20 74 75 72 6e 73 20 6f 75 74 0a 2a 2a 20 74 68 turns out.** th
10afc 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 69 at the current i
10afd 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 68 61 mplementation ha
10afe 70 70 65 6e 73 20 74 6f 20 6e 65 76 65 72 20 63 ppens to never c
10aff 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 all this routine
10b00 0a 2a 2a 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 .** with a NULL
10b01 70 6f 69 6e 74 65 72 2c 20 73 6f 20 77 65 20 6d pointer, so we m
10b02 61 72 6b 20 74 68 65 20 4e 55 4c 4c 20 74 65 73 ark the NULL tes
10b03 74 20 77 69 74 68 20 41 4c 57 41 59 53 28 29 2e t with ALWAYS().
10b04 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
10b05 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 pcache1FreePage(
10b06 50 67 48 64 72 31 20 2a 70 29 7b 0a 20 20 69 66 PgHdr1 *p){. if
10b07 28 20 41 4c 57 41 59 53 28 70 29 20 29 7b 0a 20 ( ALWAYS(p) ){.
10b08 20 20 20 69 66 28 20 70 2d 3e 70 43 61 63 68 65 if( p->pCache
10b09 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a ->bPurgeable ){.
10b0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 6e 43 pcache1.nC
10b0b 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b 0a 20 20 urrentPage--;.
10b0c 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65 31 46 }. pcache1F
10b0d 72 65 65 28 50 47 48 44 52 31 5f 54 4f 5f 50 41 ree(PGHDR1_TO_PA
10b0e 47 45 28 70 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f GE(p));. }.}../
10b0f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75 6e 63 *.** Malloc func
10b10 74 69 6f 6e 20 75 73 65 64 20 62 79 20 53 51 4c tion used by SQL
10b11 69 74 65 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 ite to obtain sp
10b12 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 75 66 ace from the buf
10b13 66 65 72 20 63 6f 6e 66 69 67 75 72 65 64 0a 2a fer configured.*
10b14 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f * using sqlite3_
10b15 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f config(SQLITE_CO
10b16 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 29 20 NFIG_PAGECACHE)
10b17 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 73 75 option. If no su
10b18 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 65 78 69 ch buffer.** exi
10b19 73 74 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69 sts, this functi
10b1a 6f 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 6f on falls back to
10b1b 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 sqlite3Malloc()
10b1c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
10b1d 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
10b1e 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 20 3PageMalloc(int
10b1f 73 7a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a sz){. void *p;.
10b20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 pcache1EnterMu
10b21 74 65 78 28 29 3b 0a 20 20 70 20 3d 20 70 63 61 tex();. p = pca
10b22 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b 0a 20 che1Alloc(sz);.
10b23 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 pcache1LeaveMut
10b24 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 ex();. return p
10b25 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
10b26 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 an allocated buf
10b27 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 72 6f fer obtained fro
10b28 6d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c m sqlite3PageMal
10b29 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 loc()..*/.SQLITE
10b2a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
10b2b 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 76 6f lite3PageFree(vo
10b2c 69 64 20 2a 70 29 7b 0a 20 20 70 63 61 63 68 65 id *p){. pcache
10b2d 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 1EnterMutex();.
10b2e 20 70 63 61 63 68 65 31 46 72 65 65 28 70 29 3b pcache1Free(p);
10b2f 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d . pcache1LeaveM
10b30 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a utex();.}../****
10b31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b32 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
10b36 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 49 6d ***** General Im
10b37 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 46 75 6e plementation Fun
10b38 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ctions *********
10b39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
10b3b 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
10b3c 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 73 n is used to res
10b3d 69 7a 65 20 74 68 65 20 68 61 73 68 20 74 61 62 ize the hash tab
10b3e 6c 65 20 75 73 65 64 20 62 79 20 74 68 65 20 63 le used by the c
10b3f 61 63 68 65 20 70 61 73 73 65 64 0a 2a 2a 20 61 ache passed.** a
10b40 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 s the first argu
10b41 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ment..**.** The
10b42 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 global mutex mus
10b43 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 74 t be held when t
10b44 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
10b45 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 called..*/.stati
10b46 63 20 69 6e 74 20 70 63 61 63 68 65 31 52 65 73 c int pcache1Res
10b47 69 7a 65 48 61 73 68 28 50 43 61 63 68 65 31 20 izeHash(PCache1
10b48 2a 70 29 7b 0a 20 20 50 67 48 64 72 31 20 2a 2a *p){. PgHdr1 **
10b49 61 70 4e 65 77 3b 0a 20 20 75 6e 73 69 67 6e 65 apNew;. unsigne
10b4a 64 20 69 6e 74 20 6e 4e 65 77 3b 0a 20 20 75 6e d int nNew;. un
10b4b 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 0a 20 signed int i;..
10b4c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
10b4d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 _mutex_held(pcac
10b4e 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 he1.mutex) );..
10b4f 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 48 61 73 68 nNew = p->nHash
10b50 2a 32 3b 0a 20 20 69 66 28 20 6e 4e 65 77 3c 32 *2;. if( nNew<2
10b51 35 36 20 29 7b 0a 20 20 20 20 6e 4e 65 77 20 3d 56 ){. nNew =
10b52 20 32 35 36 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 256;. }.. pca
10b53 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 che1LeaveMutex()
10b54 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 61 73 68 ;. if( p->nHash
10b55 20 29 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e ){ sqlite3Begin
10b56 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 BenignMalloc();
10b57 7d 0a 20 20 61 70 4e 65 77 20 3d 20 28 50 67 48 }. apNew = (PgH
10b58 64 72 31 20 2a 2a 29 73 71 6c 69 74 65 33 5f 6d dr1 **)sqlite3_m
10b59 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50 67 48 alloc(sizeof(PgH
10b5a 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a 20 20 dr1 *)*nNew);.
10b5b 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29 7b 20 if( p->nHash ){
10b5c 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
10b5d 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20 70 63 Malloc(); }. pc
10b5e 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 ache1EnterMutex(
10b5f 29 3b 0a 20 20 69 66 28 20 61 70 4e 65 77 20 29 );. if( apNew )
10b60 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 61 70 4e {. memset(apN
10b61 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 67 ew, 0, sizeof(Pg
10b62 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a 20 Hdr1 *)*nNew);.
10b63 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d for(i=0; i<p-
10b64 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 7b 0a 20 20 >nHash; i++){.
10b65 20 20 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 PgHdr1 *pPag
10b66 65 3b 0a 20 20 20 20 20 20 50 67 48 64 72 31 20 e;. PgHdr1
10b67 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 61 70 48 61 *pNext = p->apHa
10b68 73 68 5b 69 5d 3b 0a 20 20 20 20 20 20 77 68 69 sh[i];. whi
10b69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 70 4e 65 le( (pPage = pNe
10b6a 78 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 xt)!=0 ){.
10b6b 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
10b6c 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 25 = pPage->iKey %
10b6d 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 70 nNew;. p
10b6e 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 4e Next = pPage->pN
10b6f 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 50 61 ext;. pPa
10b70 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 70 4e 65 ge->pNext = apNe
10b71 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20 20 61 70 w[h];. ap
10b72 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a New[h] = pPage;.
10b73 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
10b74 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
10b75 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 20 20 70 ->apHash);. p
10b76 2d 3e 61 70 48 61 73 68 20 3d 20 61 70 4e 65 77 ->apHash = apNew
10b77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73 68 20 3d ;. p->nHash =
10b78 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 72 65 nNew;. }.. re
10b79 74 75 72 6e 20 28 70 2d 3e 61 70 48 61 73 68 20 turn (p->apHash
10b7a 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 ? SQLITE_OK : SQ
10b7b 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a LITE_NOMEM);.}..
10b7c 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
10b7d 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 74 65 ion is used inte
10b7e 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d 6f 76 65 rnally to remove
10b7f 20 74 68 65 20 70 61 67 65 20 70 50 61 67 65 20 the page pPage
10b80 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 67 6c 6f from the .** glo
10b81 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 20 69 66 bal LRU list, if
10b82 20 69 73 20 70 61 72 74 20 6f 66 20 69 74 2e 20 is part of it.
10b83 49 66 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20 If pPage is not
10b84 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f 62 part of the glob
10b85 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69 73 74 2c 20 al.** LRU list,
10b86 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 then this functi
10b87 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a on is a no-op..*
10b88 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 *.** The global
10b89 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 mutex must be he
10b8a 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e ld when this fun
10b8b 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e ction is called.
10b8c 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
10b8d 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 50 pcache1PinPage(P
10b8e 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b 0a 20 gHdr1 *pPage){.
10b8f 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
10b90 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 _mutex_held(pcac
10b91 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 he1.mutex) );.
10b92 69 66 28 20 70 50 61 67 65 20 26 26 20 28 70 50 if( pPage && (pP
10b93 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 7c 7c age->pLruNext ||
10b94 20 70 50 61 67 65 3d 3d 70 63 61 63 68 65 31 2e pPage==pcache1.
10b95 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20 20 20 pLruTail) ){.
10b96 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c 72 75 if( pPage->pLru
10b97 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70 50 Prev ){. pP
10b98 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d 3e 70 age->pLruPrev->p
10b99 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67 65 2d LruNext = pPage-
10b9a 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d >pLruNext;. }
10b9b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e . if( pPage->
10b9c 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 20 20 pLruNext ){.
10b9d 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 pPage->pLruNex
10b9e 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 50 t->pLruPrev = pP
10b9f 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b 0a 20 age->pLruPrev;.
10ba0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 63 61 }. if( pca
10ba1 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3d 3d 70 che1.pLruHead==p
10ba2 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70 63 Page ){. pc
10ba3 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 3d ache1.pLruHead =
10ba4 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 pPage->pLruNext
10ba5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
10ba6 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c pcache1.pLruTail
10ba7 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 ==pPage ){.
10ba8 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 pcache1.pLruTai
10ba9 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 l = pPage->pLruP
10baa 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 rev;. }. p
10bab 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d Page->pLruNext =
10bac 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 0;. pPage->p
10bad 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 LruPrev = 0;.
10bae 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e pPage->pCache->
10baf 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b 0a 20 nRecyclable--;.
10bb0 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d }.}.../*.** Rem
10bb1 6f 76 65 20 74 68 65 20 70 61 67 65 20 73 75 70 ove the page sup
10bb2 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 plied as an argu
10bb3 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 68 61 ment from the ha
10bb4 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28 50 43 sh table .** (PC
10bb5 61 63 68 65 31 2e 61 70 48 61 73 68 20 73 74 72 ache1.apHash str
10bb6 75 63 74 75 72 65 29 20 74 68 61 74 20 69 74 20 ucture) that it
10bb7 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f is currently sto
10bb8 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 red in..**.** Th
10bb9 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d e global mutex m
10bba 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e ust be held when
10bbb 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
10bbc 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 s called..*/.sta
10bbd 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 tic void pcache1
10bbe 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 50 RemoveFromHash(P
10bbf 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b 0a 20 gHdr1 *pPage){.
10bc0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b unsigned int h;
10bc1 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 . PCache1 *pCac
10bc2 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 he = pPage->pCac
10bc3 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a 70 he;. PgHdr1 **p
10bc4 70 3b 0a 0a 20 20 68 20 3d 20 70 50 61 67 65 2d p;.. h = pPage-
10bc5 3e 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e >iKey % pCache->
10bc6 6e 48 61 73 68 3b 0a 20 20 66 6f 72 28 70 70 3d nHash;. for(pp=
10bc7 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b &pCache->apHash[
10bc8 68 5d 3b 20 28 2a 70 70 29 21 3d 70 50 61 67 65 h]; (*pp)!=pPage
10bc9 3b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70 4e 65 ; pp=&(*pp)->pNe
10bca 78 74 29 3b 0a 20 20 2a 70 70 20 3d 20 28 2a 70 xt);. *pp = (*p
10bcb 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 70 43 p)->pNext;.. pC
10bcc 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b 0a 7d ache->nPage--;.}
10bcd 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 ../*.** If there
10bce 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 6d are currently m
10bcf 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68 65 2e ore than pcache.
10bd0 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73 20 61 nMaxPage pages a
10bd1 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a 2a 2a llocated, try.**
10bd2 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61 67 65 to recycle page
10bd3 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 s to reduce the
10bd4 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74 65 64 number allocated
10bd5 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61 78 50 to pcache.nMaxP
10bd6 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 age..*/.static v
10bd7 6f 69 64 20 70 63 61 63 68 65 31 45 6e 66 6f 72 oid pcache1Enfor
10bd8 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64 29 7b ceMaxPage(void){
10bd9 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
10bda 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 e3_mutex_held(pc
10bdb 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a ache1.mutex) );.
10bdc 20 20 77 68 69 6c 65 28 20 70 63 61 63 68 65 31 while( pcache1
10bdd 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e 70 63 .nCurrentPage>pc
10bde 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 26 ache1.nMaxPage &
10bdf 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 & pcache1.pLruTa
10be0 69 6c 20 29 7b 0a 20 20 20 20 50 67 48 64 72 31 il ){. PgHdr1
10be1 20 2a 70 20 3d 20 70 63 61 63 68 65 31 2e 70 4c *p = pcache1.pL
10be2 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63 61 63 ruTail;. pcac
10be3 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b 0a 20 he1PinPage(p);.
10be4 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76 65 pcache1Remove
10be5 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 20 20 FromHash(p);.
10be6 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 pcache1FreePage
10be7 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
10be8 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 70 61 * Discard all pa
10be9 67 65 73 20 66 72 6f 6d 20 63 61 63 68 65 20 70 ges from cache p
10bea 43 61 63 68 65 20 77 69 74 68 20 61 20 70 61 67 Cache with a pag
10beb 65 20 6e 75 6d 62 65 72 20 28 6b 65 79 20 76 61 e number (key va
10bec 6c 75 65 29 20 0a 2a 2a 20 67 72 65 61 74 65 72 lue) .** greater
10bed 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
10bee 6f 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 70 69 o iLimit. Any pi
10bef 6e 6e 65 64 20 70 61 67 65 73 20 74 68 61 74 20 nned pages that
10bf0 6d 65 65 74 20 74 68 69 73 20 0a 2a 2a 20 63 72 meet this .** cr
10bf1 69 74 65 72 69 61 20 61 72 65 20 75 6e 70 69 6e iteria are unpin
10bf2 6e 65 64 20 62 65 66 6f 72 65 20 74 68 65 79 20 ned before they
10bf3 61 72 65 20 64 69 73 63 61 72 64 65 64 2e 0a 2a are discarded..*
10bf4 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 *.** The global
10bf5 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 mutex must be he
10bf6 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e ld when this fun
10bf7 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e ction is called.
10bf8 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
10bf9 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 55 pcache1TruncateU
10bfa 6e 73 61 66 65 28 0a 20 20 50 43 61 63 68 65 31 nsafe(. PCache1
10bfb 20 2a 70 43 61 63 68 65 2c 20 0a 20 20 75 6e 73 *pCache, . uns
10bfc 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d 69 74 igned int iLimit
10bfd 20 0a 29 7b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 .){. TESTONLY(
10bfe 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 50 unsigned int nP
10bff 61 67 65 20 3d 20 30 3b 20 29 20 20 20 20 20 20 age = 0; )
10c00 2f 2a 20 55 73 65 64 20 74 6f 20 61 73 73 65 72 /* Used to asser
10c01 74 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 20 t pCache->nPage
10c02 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a 20 20 is correct */.
10c03 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 0a unsigned int h;.
10c04 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
10c05 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 3_mutex_held(pca
10c06 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 che1.mutex) );.
10c07 20 66 6f 72 28 68 3d 30 3b 20 68 3c 70 43 61 63 for(h=0; h<pCac
10c08 68 65 2d 3e 6e 48 61 73 68 3b 20 68 2b 2b 29 7b he->nHash; h++){
10c09 0a 20 20 20 20 50 67 48 64 72 31 20 2a 2a 70 70 . PgHdr1 **pp
10c0a 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 48 61 = &pCache->apHa
10c0b 73 68 5b 68 5d 3b 20 0a 20 20 20 20 50 67 48 64 sh[h]; . PgHd
10c0c 72 31 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 77 r1 *pPage;. w
10c0d 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 2a hile( (pPage = *
10c0e 70 70 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 pp)!=0 ){.
10c0f 69 66 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3e if( pPage->iKey>
10c10 3d 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 =iLimit ){.
10c11 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 pCache->nPage
10c12 2d 2d 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 20 --;. *pp
10c13 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a = pPage->pNext;.
10c14 20 20 20 20 20 20 20 20 70 63 61 63 68 65 31 50 pcache1P
10c15 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 inPage(pPage);.
10c16 20 20 20 20 20 20 20 70 63 61 63 68 65 31 46 72 pcache1Fr
10c17 65 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 eePage(pPage);.
10c18 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
10c19 20 20 20 20 70 70 20 3d 20 26 70 50 61 67 65 2d pp = &pPage-
10c1a 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 >pNext;.
10c1b 54 45 53 54 4f 4e 4c 59 28 20 6e 50 61 67 65 2b TESTONLY( nPage+
10c1c 2b 3b 20 29 0a 20 20 20 20 20 20 7d 0a 20 20 20 +; ). }.
10c1d 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 }. }. assert(
10c1e 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 3d 3d pCache->nPage==
10c1f 6e 50 61 67 65 20 29 3b 0a 7d 0a 0a 2f 2a 2a 2a nPage );.}../***
10c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
10c25 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 ****** sqlite3_p
10c26 63 61 63 68 65 20 4d 65 74 68 6f 64 73 20 2a 2a cache Methods **
10c27 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c28 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
10c2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
10c2b 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ion of the sqlit
10c2c 65 33 5f 70 63 61 63 68 65 2e 78 49 6e 69 74 20 e3_pcache.xInit
10c2d 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 method..*/.stati
10c2e 63 20 69 6e 74 20 70 63 61 63 68 65 31 49 6e 69 c int pcache1Ini
10c2f 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 t(void *NotUsed)
10c30 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
10c31 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
10c32 20 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31 assert( pcache1
10c33 2e 69 73 49 6e 69 74 3d 3d 30 20 29 3b 0a 20 20 .isInit==0 );.
10c34 6d 65 6d 73 65 74 28 26 70 63 61 63 68 65 31 2c memset(&pcache1,
10c35 20 30 2c 20 73 69 7a 65 6f 66 28 70 63 61 63 68 0, sizeof(pcach
10c36 65 31 29 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 e1));. if( sqli
10c37 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
10c38 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 bCoreMutex ){.
10c39 20 20 70 63 61 63 68 65 31 2e 6d 75 74 65 78 20 pcache1.mutex
10c3a 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f = sqlite3_mutex_
10c3b 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 alloc(SQLITE_MUT
10c3c 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 29 3b 0a EX_STATIC_LRU);.
10c3d 20 20 7d 0a 20 20 70 63 61 63 68 65 31 2e 69 73 }. pcache1.is
10c3e 49 6e 69 74 20 3d 20 31 3b 0a 20 20 72 65 74 75 Init = 1;. retu
10c3f 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
10c40 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
10c41 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c ation of the sql
10c42 69 74 65 33 5f 70 63 61 63 68 65 2e 78 53 68 75 ite3_pcache.xShu
10c43 74 64 6f 77 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2a tdown method..**
10c44 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 73 Note that the s
10c45 74 61 74 69 63 20 6d 75 74 65 78 20 61 6c 6c 6f tatic mutex allo
10c46 63 61 74 65 64 20 69 6e 20 78 49 6e 69 74 20 64 cated in xInit d
10c47 6f 65 73 20 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 oes .** not need
10c48 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f to be freed..*/
10c49 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 .static void pca
10c4a 63 68 65 31 53 68 75 74 64 6f 77 6e 28 76 6f 69 che1Shutdown(voi
10c4b 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 d *NotUsed){. U
10c4c 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
10c4d 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 73 65 NotUsed);. asse
10c4e 72 74 28 20 70 63 61 63 68 65 31 2e 69 73 49 6e rt( pcache1.isIn
10c4f 69 74 21 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65 it!=0 );. memse
10c50 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c 20 73 t(&pcache1, 0, s
10c51 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29 29 3b izeof(pcache1));
10c52 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d .}../*.** Implem
10c53 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
10c54 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 sqlite3_pcache.x
10c55 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2e 0a 2a Create method..*
10c56 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 *.** Allocate a
10c57 6e 65 77 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 new cache..*/.st
10c58 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70 63 61 atic sqlite3_pca
10c59 63 68 65 20 2a 70 63 61 63 68 65 31 43 72 65 61 che *pcache1Crea
10c5a 74 65 28 69 6e 74 20 73 7a 50 61 67 65 2c 20 69 te(int szPage, i
10c5b 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 7b 0a nt bPurgeable){.
10c5c 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 PCache1 *pCach
10c5d 65 3b 0a 0a 20 20 70 43 61 63 68 65 20 3d 20 28 e;.. pCache = (
10c5e 50 43 61 63 68 65 31 20 2a 29 73 71 6c 69 74 65 PCache1 *)sqlite
10c5f 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 3_malloc(sizeof(
10c60 50 43 61 63 68 65 31 29 29 3b 0a 20 20 69 66 28 PCache1));. if(
10c61 20 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 6d pCache ){. m
10c62 65 6d 73 65 74 28 70 43 61 63 68 65 2c 20 30 2c emset(pCache, 0,
10c63 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 31 29 sizeof(PCache1)
10c64 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 73 );. pCache->s
10c65 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a zPage = szPage;.
10c66 20 20 20 20 70 43 61 63 68 65 2d 3e 62 50 75 72 pCache->bPur
10c67 67 65 61 62 6c 65 20 3d 20 28 62 50 75 72 67 65 geable = (bPurge
10c68 61 62 6c 65 20 3f 20 31 20 3a 20 30 29 3b 0a 20 able ? 1 : 0);.
10c69 20 20 20 69 66 28 20 62 50 75 72 67 65 61 62 6c if( bPurgeabl
10c6a 65 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 e ){. pCach
10c6b 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b 0a 20 20 e->nMin = 10;.
10c6c 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 pcache1Enter
10c6d 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 70 Mutex();. p
10c6e 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20 cache1.nMinPage
10c6f 2b 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b += pCache->nMin;
10c70 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 4c 65 . pcache1Le
10c71 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 aveMutex();.
10c72 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 }. }. return (
10c73 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a sqlite3_pcache *
10c74 29 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f 2a 0a 2a )pCache;.}../*.*
10c75 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
10c76 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
10c77 70 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a pcache.xCachesiz
10c78 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a e method. .**.**
10c79 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 63 Configure the c
10c7a 61 63 68 65 5f 73 69 7a 65 20 6c 69 6d 69 74 20 ache_size limit
10c7b 66 6f 72 20 61 20 63 61 63 68 65 2e 0a 2a 2f 0a for a cache..*/.
10c7c 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
10c7d 68 65 31 43 61 63 68 65 73 69 7a 65 28 73 71 6c he1Cachesize(sql
10c7e 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 ite3_pcache *p,
10c7f 69 6e 74 20 6e 4d 61 78 29 7b 0a 20 20 50 43 61 int nMax){. PCa
10c80 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 che1 *pCache = (
10c81 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 69 PCache1 *)p;. i
10c82 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 f( pCache->bPurg
10c83 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 70 63 61 eable ){. pca
10c84 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
10c85 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 6e 4d ;. pcache1.nM
10c86 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d 61 78 20 axPage += (nMax
10c87 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 3b - pCache->nMax);
10c88 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61 . pCache->nMa
10c89 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 20 20 70 63 x = nMax;. pc
10c8a 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 78 50 ache1EnforceMaxP
10c8b 61 67 65 28 29 3b 0a 20 20 20 20 70 63 61 63 68 age();. pcach
10c8c 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a e1LeaveMutex();.
10c8d 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 }.}../*.** Imp
10c8e 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
10c8f 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 he sqlite3_pcach
10c90 65 2e 78 50 61 67 65 63 6f 75 6e 74 20 6d 65 74 e.xPagecount met
10c91 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 hod. .*/.static
10c92 69 6e 74 20 70 63 61 63 68 65 31 50 61 67 65 63 int pcache1Pagec
10c93 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 70 63 61 ount(sqlite3_pca
10c94 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e che *p){. int n
10c95 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
10c96 4d 75 74 65 78 28 29 3b 0a 20 20 6e 20 3d 20 28 Mutex();. n = (
10c97 28 50 43 61 63 68 65 31 20 2a 29 70 29 2d 3e 6e (PCache1 *)p)->n
10c98 50 61 67 65 3b 0a 20 20 70 63 61 63 68 65 31 4c Page;. pcache1L
10c99 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 eaveMutex();. r
10c9a 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn n;.}../*.*
10c9b 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
10c9c 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
10c9d 70 63 61 63 68 65 2e 78 46 65 74 63 68 20 6d 65 pcache.xFetch me
10c9e 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 46 65 74 thod. .**.** Fet
10c9f 63 68 20 61 20 70 61 67 65 20 62 79 20 6b 65 79 ch a page by key
10ca0 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 value..**.** Wh
10ca1 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 6e ether or not a n
10ca2 65 77 20 70 61 67 65 20 6d 61 79 20 62 65 20 61 ew page may be a
10ca3 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 llocated by this
10ca4 20 66 75 6e 63 74 69 6f 6e 20 64 65 70 65 6e 64 function depend
10ca5 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 76 61 6c 75 s on.** the valu
10ca6 65 20 6f 66 20 74 68 65 20 63 72 65 61 74 65 46 e of the createF
10ca7 6c 61 67 20 61 72 67 75 6d 65 6e 74 2e 20 20 30 lag argument. 0
10ca8 20 6d 65 61 6e 73 20 64 6f 20 6e 6f 74 20 61 6c means do not al
10ca9 6c 6f 63 61 74 65 20 61 20 6e 65 77 0a 2a 2a 20 locate a new.**
10caa 70 61 67 65 2e 20 20 31 20 6d 65 61 6e 73 20 61 page. 1 means a
10cab 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 llocate a new pa
10cac 67 65 20 69 66 20 73 70 61 63 65 20 69 73 20 65 ge if space is e
10cad 61 73 69 6c 79 20 61 76 61 69 6c 61 62 6c 65 2e asily available.
10cae 20 20 32 20 0a 2a 2a 20 6d 65 61 6e 73 20 74 6f 2 .** means to
10caf 20 74 72 79 20 72 65 61 6c 6c 79 20 68 61 72 64 try really hard
10cb0 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e to allocate a n
10cb1 65 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 46 ew page..**.** F
10cb2 6f 72 20 61 20 6e 6f 6e 2d 70 75 72 67 65 61 62 or a non-purgeab
10cb3 6c 65 20 63 61 63 68 65 20 28 61 20 63 61 63 68 le cache (a cach
10cb4 65 20 75 73 65 64 20 61 73 20 74 68 65 20 73 74 e used as the st
10cb5 6f 72 61 67 65 20 66 6f 72 20 61 6e 20 69 6e 2d orage for an in-
10cb6 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 61 memory.** databa
10cb7 73 65 29 20 74 68 65 72 65 20 69 73 20 72 65 61 se) there is rea
10cb8 6c 6c 79 20 6e 6f 20 64 69 66 66 65 72 65 6e 63 lly no differenc
10cb9 65 20 62 65 74 77 65 65 6e 20 63 72 65 61 74 65 e between create
10cba 46 6c 61 67 20 31 20 61 6e 64 20 32 2e 20 20 53 Flag 1 and 2. S
10cbb 6f 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 o.** the calling
10cbc 20 66 75 6e 63 74 69 6f 6e 20 28 70 63 61 63 68 function (pcach
10cbd 65 2e 63 29 20 77 69 6c 6c 20 6e 65 76 65 72 20 e.c) will never
10cbe 68 61 76 65 20 61 20 63 72 65 61 74 65 46 6c 61 have a createFla
10cbf 67 20 6f 66 20 31 20 6f 6e 0a 2a 2a 20 61 20 6e g of 1 on.** a n
10cc0 6f 6e 2d 70 75 72 67 61 62 6c 65 20 63 61 63 68 on-purgable cach
10cc1 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 e..**.** There a
10cc2 72 65 20 74 68 72 65 65 20 64 69 66 66 65 72 65 re three differe
10cc3 6e 74 20 61 70 70 72 6f 61 63 68 65 73 20 74 6f nt approaches to
10cc4 20 6f 62 74 61 69 6e 69 6e 67 20 73 70 61 63 65 obtaining space
10cc5 20 66 6f 72 20 61 20 70 61 67 65 2c 0a 2a 2a 20 for a page,.**
10cc6 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 depending on the
10cc7 20 76 61 6c 75 65 20 6f 66 20 70 61 72 61 6d 65 value of parame
10cc8 74 65 72 20 63 72 65 61 74 65 46 6c 61 67 20 28 ter createFlag (
10cc9 77 68 69 63 68 20 6d 61 79 20 62 65 20 30 2c 20 which may be 0,
10cca 31 20 6f 72 20 32 29 2e 0a 2a 2a 0a 2a 2a 20 20 1 or 2)..**.**
10ccb 20 31 2e 20 52 65 67 61 72 64 6c 65 73 73 20 6f 1. Regardless o
10ccc 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 63 f the value of c
10ccd 72 65 61 74 65 46 6c 61 67 2c 20 74 68 65 20 63 reateFlag, the c
10cce 61 63 68 65 20 69 73 20 73 65 61 72 63 68 65 64 ache is searched
10ccf 20 66 6f 72 20 61 20 0a 2a 2a 20 20 20 20 20 20 for a .**
10cd0 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 71 75 copy of the requ
10cd1 65 73 74 65 64 20 70 61 67 65 2e 20 49 66 20 6f ested page. If o
10cd2 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 69 74 20 ne is found, it
10cd3 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
10cd4 2a 2a 20 20 20 32 2e 20 49 66 20 63 72 65 61 74 ** 2. If creat
10cd5 65 46 6c 61 67 3d 3d 30 20 61 6e 64 20 74 68 65 eFlag==0 and the
10cd6 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 page is not alr
10cd7 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 eady in the cach
10cd8 65 2c 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 20 20 e, NULL is.**
10cd9 20 20 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a returned..**.
10cda 2a 2a 20 20 20 33 2e 20 49 66 20 63 72 65 61 74 ** 3. If creat
10cdb 65 46 6c 61 67 20 69 73 20 31 2c 20 61 6e 64 20 eFlag is 1, and
10cdc 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 the page is not
10cdd 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 already in the c
10cde 61 63 68 65 2c 0a 2a 2a 20 20 20 20 20 20 61 6e ache,.** an
10cdf 64 20 69 66 20 65 69 74 68 65 72 20 6f 66 20 74 d if either of t
10ce0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 he following are
10ce1 20 74 72 75 65 2c 20 72 65 74 75 72 6e 20 4e 55 true, return NU
10ce2 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 LL:.**.**
10ce3 28 61 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f (a) the number o
10ce4 66 20 70 61 67 65 73 20 70 69 6e 6e 65 64 20 62 f pages pinned b
10ce5 79 20 74 68 65 20 63 61 63 68 65 20 69 73 20 67 y the cache is g
10ce6 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 20 reater than.**
10ce7 20 20 20 20 20 20 20 20 20 50 43 61 63 68 65 31 PCache1
10ce8 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 20 20 20 20 .nMax, or.**
10ce9 20 20 20 28 62 29 20 74 68 65 20 6e 75 6d 62 65 (b) the numbe
10cea 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e 65 r of pages pinne
10ceb 64 20 62 79 20 74 68 65 20 63 61 63 68 65 20 69 d by the cache i
10cec 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a s greater than.*
10ced 2a 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 * the
10cee 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f 72 20 sum of nMax for
10cef 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61 all purgeable ca
10cf0 63 68 65 73 2c 20 6c 65 73 73 20 74 68 65 20 73 ches, less the s
10cf1 75 6d 20 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20 um of .**
10cf2 20 20 20 20 6e 4d 69 6e 20 66 6f 72 20 61 6c 6c nMin for all
10cf3 20 6f 74 68 65 72 20 70 75 72 67 65 61 62 6c 65 other purgeable
10cf4 20 63 61 63 68 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 caches. .**.**
10cf5 20 20 34 2e 20 49 66 20 6e 6f 6e 65 20 6f 66 20 4. If none of
10cf6 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20 the first three
10cf7 63 6f 6e 64 69 74 69 6f 6e 73 20 61 70 70 6c 79 conditions apply
10cf8 20 61 6e 64 20 74 68 65 20 63 61 63 68 65 20 69 and the cache i
10cf9 73 20 6d 61 72 6b 65 64 0a 2a 2a 20 20 20 20 20 s marked.**
10cfa 20 61 73 20 70 75 72 67 65 61 62 6c 65 2c 20 61 as purgeable, a
10cfb 6e 64 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 nd if one of the
10cfc 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 following is tr
10cfd 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 ue:.**.**
10cfe 28 61 29 20 54 68 65 20 6e 75 6d 62 65 72 20 6f (a) The number o
10cff 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 f pages allocate
10d00 64 20 66 6f 72 20 74 68 65 20 63 61 63 68 65 20 d for the cache
10d01 69 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 20 is already .**
10d02 20 20 20 20 20 20 20 20 20 50 43 61 63 68 65 31 PCache1
10d03 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 .nMax, or.**.**
10d04 20 20 20 20 20 20 28 62 29 20 54 68 65 20 6e 75 (b) The nu
10d05 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c mber of pages al
10d06 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 6c 6c 20 located for all
10d07 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 purgeable caches
10d08 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 is.**
10d09 20 61 6c 72 65 61 64 79 20 65 71 75 61 6c 20 74 already equal t
10d0a 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 o or greater tha
10d0b 6e 20 74 68 65 20 73 75 6d 20 6f 66 20 6e 4d 61 n the sum of nMa
10d0c 78 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 20 20 20 x for all.**
10d0d 20 20 20 20 20 20 20 70 75 72 67 65 61 62 6c 65 purgeable
10d0e 20 63 61 63 68 65 73 2c 0a 2a 2a 0a 2a 2a 20 20 caches,.**.**
10d0f 20 20 20 20 74 68 65 6e 20 61 74 74 65 6d 70 74 then attempt
10d10 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 to recycle a pa
10d11 67 65 20 66 72 6f 6d 20 74 68 65 20 4c 52 55 20 ge from the LRU
10d12 6c 69 73 74 2e 20 49 66 20 69 74 20 69 73 20 74 list. If it is t
10d13 68 65 20 72 69 67 68 74 0a 2a 2a 20 20 20 20 20 he right.**
10d14 20 73 69 7a 65 2c 20 72 65 74 75 72 6e 20 74 68 size, return th
10d15 65 20 72 65 63 79 63 6c 65 64 20 62 75 66 66 65 e recycled buffe
10d16 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 72 r. Otherwise, fr
10d17 65 65 20 74 68 65 20 62 75 66 66 65 72 20 61 6e ee the buffer an
10d18 64 0a 2a 2a 20 20 20 20 20 20 70 72 6f 63 65 65 d.** procee
10d19 64 20 74 6f 20 73 74 65 70 20 35 2e 20 0a 2a 2a d to step 5. .**
10d1a 0a 2a 2a 20 20 20 35 2e 20 4f 74 68 65 72 77 69 .** 5. Otherwi
10d1b 73 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 se, allocate and
10d1c 20 72 65 74 75 72 6e 20 61 20 6e 65 77 20 70 61 return a new pa
10d1d 67 65 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 ge buffer..*/.st
10d1e 61 74 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68 atic void *pcach
10d1f 65 31 46 65 74 63 68 28 73 71 6c 69 74 65 33 5f e1Fetch(sqlite3_
10d20 70 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 69 67 pcache *p, unsig
10d21 6e 65 64 20 69 6e 74 20 69 4b 65 79 2c 20 69 6e ned int iKey, in
10d22 74 20 63 72 65 61 74 65 46 6c 61 67 29 7b 0a 20 t createFlag){.
10d23 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 50 unsigned int nP
10d24 69 6e 6e 65 64 3b 0a 20 20 50 43 61 63 68 65 31 inned;. PCache1
10d25 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 *pCache = (PCac
10d26 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 he1 *)p;. PgHdr
10d27 31 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 1 *pPage = 0;..
10d28 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 2d assert( pCache-
10d29 3e 62 50 75 72 67 65 61 62 6c 65 20 7c 7c 20 63 >bPurgeable || c
10d2a 72 65 61 74 65 46 6c 61 67 21 3d 31 20 29 3b 0a reateFlag!=1 );.
10d2b 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 pcache1EnterMu
10d2c 74 65 78 28 29 3b 0a 20 20 69 66 28 20 63 72 65 tex();. if( cre
10d2d 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c ateFlag==1 ) sql
10d2e 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
10d2f 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 2f 2a 20 53 alloc();.. /* S
10d30 65 61 72 63 68 20 74 68 65 20 68 61 73 68 20 74 earch the hash t
10d31 61 62 6c 65 20 66 6f 72 20 61 6e 20 65 78 69 73 able for an exis
10d32 74 69 6e 67 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 ting entry. */.
10d33 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e 48 61 if( pCache->nHa
10d34 73 68 3e 30 20 29 7b 0a 20 20 20 20 75 6e 73 69 sh>0 ){. unsi
10d35 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b 65 gned int h = iKe
10d36 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 y % pCache->nHas
10d37 68 3b 0a 20 20 20 20 66 6f 72 28 70 50 61 67 65 h;. for(pPage
10d38 3d 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b =pCache->apHash[
10d39 68 5d 3b 20 70 50 61 67 65 26 26 70 50 61 67 65 h]; pPage&&pPage
10d3a 2d 3e 69 4b 65 79 21 3d 69 4b 65 79 3b 20 70 50 ->iKey!=iKey; pP
10d3b 61 67 65 3d 70 50 61 67 65 2d 3e 70 4e 65 78 74 age=pPage->pNext
10d3c 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 );. }.. if( pP
10d3d 61 67 65 20 7c 7c 20 63 72 65 61 74 65 46 6c 61 age || createFla
10d3e 67 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 61 63 g==0 ){. pcac
10d3f 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 67 65 he1PinPage(pPage
10d40 29 3b 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 63 );. goto fetc
10d41 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a h_out;. }.. /*
10d42 20 53 74 65 70 20 33 20 6f 66 20 68 65 61 64 65 Step 3 of heade
10d43 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a 2f 0a 20 20 r comment. */.
10d44 6e 50 69 6e 6e 65 64 20 3d 20 70 43 61 63 68 65 nPinned = pCache
10d45 2d 3e 6e 50 61 67 65 20 2d 20 70 43 61 63 68 65 ->nPage - pCache
10d46 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 3b 0a 20 ->nRecyclable;.
10d47 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d if( createFlag=
10d48 3d 31 20 26 26 20 28 0a 20 20 20 20 20 20 20 20 =1 && (.
10d49 6e 50 69 6e 6e 65 64 3e 3d 28 70 63 61 63 68 65 nPinned>=(pcache
10d4a 31 2e 6e 4d 61 78 50 61 67 65 2b 70 43 61 63 68 1.nMaxPage+pCach
10d4b 65 2d 3e 6e 4d 69 6e 2d 70 63 61 63 68 65 31 2e e->nMin-pcache1.
10d4c 6e 4d 69 6e 50 61 67 65 29 0a 20 20 20 20 20 7c nMinPage). |
10d4d 7c 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 43 61 63 | nPinned>=(pCac
10d4e 68 65 2d 3e 6e 4d 61 78 20 2a 20 39 20 2f 20 31 he->nMax * 9 / 1
10d4f 30 29 0a 20 20 29 29 7b 0a 20 20 20 20 67 6f 74 0). )){. got
10d50 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d o fetch_out;. }
10d51 0a 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e .. if( pCache->
10d52 6e 50 61 67 65 3e 3d 70 43 61 63 68 65 2d 3e 6e nPage>=pCache->n
10d53 48 61 73 68 20 26 26 20 70 63 61 63 68 65 31 52 Hash && pcache1R
10d54 65 73 69 7a 65 48 61 73 68 28 70 43 61 63 68 65 esizeHash(pCache
10d55 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 65 ) ){. goto fe
10d56 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 tch_out;. }..
10d57 2f 2a 20 53 74 65 70 20 34 2e 20 54 72 79 20 74 /* Step 4. Try t
10d58 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 67 65 o recycle a page
10d59 20 62 75 66 66 65 72 20 69 66 20 61 70 70 72 6f buffer if appro
10d5a 70 72 69 61 74 65 2e 20 2a 2f 0a 20 20 69 66 28 priate. */. if(
10d5b 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 pCache->bPurgea
10d5c 62 6c 65 20 26 26 20 70 63 61 63 68 65 31 2e 70 ble && pcache1.p
10d5d 4c 72 75 54 61 69 6c 20 26 26 20 28 0a 20 20 20 LruTail && (.
10d5e 20 20 28 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 (pCache->nPage
10d5f 2b 31 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d 61 78 +1>=pCache->nMax
10d60 29 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e 43 75 ) || pcache1.nCu
10d61 72 72 65 6e 74 50 61 67 65 3e 3d 70 63 61 63 68 rrentPage>=pcach
10d62 65 31 2e 6e 4d 61 78 50 61 67 65 0a 20 20 29 29 e1.nMaxPage. ))
10d63 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 63 {. pPage = pc
10d64 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 3b 0a ache1.pLruTail;.
10d65 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76 pcache1Remov
10d66 65 46 72 6f 6d 48 61 73 68 28 70 50 61 67 65 29 eFromHash(pPage)
10d67 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50 69 6e ;. pcache1Pin
10d68 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
10d69 20 69 66 28 20 70 50 61 67 65 2d 3e 70 43 61 63 if( pPage->pCac
10d6a 68 65 2d 3e 73 7a 50 61 67 65 21 3d 70 43 61 63 he->szPage!=pCac
10d6b 68 65 2d 3e 73 7a 50 61 67 65 20 29 7b 0a 20 20 he->szPage ){.
10d6c 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 pcache1FreeP
10d6d 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 age(pPage);.
10d6e 20 20 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 pPage = 0;.
10d6f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 63 }else{. pc
10d70 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 ache1.nCurrentPa
10d71 67 65 20 2d 3d 20 28 70 50 61 67 65 2d 3e 70 43 ge -= (pPage->pC
10d72 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 ache->bPurgeable
10d73 20 2d 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 - pCache->bPurg
10d74 65 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 eable);. }.
10d75 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35 2e 20 }.. /* Step 5.
10d76 49 66 20 61 20 75 73 61 62 6c 65 20 70 61 67 65 If a usable page
10d77 20 62 75 66 66 65 72 20 68 61 73 20 73 74 69 6c buffer has stil
10d78 6c 20 6e 6f 74 20 62 65 65 6e 20 66 6f 75 6e 64 l not been found
10d79 2c 20 0a 20 20 2a 2a 20 61 74 74 65 6d 70 74 20 , . ** attempt
10d7a 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 to allocate a ne
10d7b 77 20 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20 20 69 w one. . */. i
10d7c 66 28 20 21 70 50 61 67 65 20 29 7b 0a 20 20 20 f( !pPage ){.
10d7d 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 65 31 pPage = pcache1
10d7e 41 6c 6c 6f 63 50 61 67 65 28 70 43 61 63 68 65 AllocPage(pCache
10d7f 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 );. }.. if( pP
10d80 61 67 65 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 age ){. unsig
10d81 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b 65 79 ned int h = iKey
10d82 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 % pCache->nHash
10d83 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 ;. pCache->nP
10d84 61 67 65 2b 2b 3b 0a 20 20 20 20 70 50 61 67 65 age++;. pPage
10d85 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b 0a 20 ->iKey = iKey;.
10d86 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 pPage->pNext
10d87 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 = pCache->apHash
10d88 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e [h];. pPage->
10d89 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 65 3b pCache = pCache;
10d8a 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 . pPage->pLru
10d8b 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 50 Prev = 0;. pP
10d8c 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 age->pLruNext =
10d8d 30 3b 0a 20 20 20 20 2a 28 76 6f 69 64 20 2a 2a 0;. *(void **
10d8e 29 28 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 )(PGHDR1_TO_PAGE
10d8f 28 70 50 61 67 65 29 29 20 3d 20 30 3b 0a 20 20 (pPage)) = 0;.
10d90 20 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 pCache->apHash
10d91 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d [h] = pPage;. }
10d92 0a 0a 66 65 74 63 68 5f 6f 75 74 3a 0a 20 20 69 ..fetch_out:. i
10d93 66 28 20 70 50 61 67 65 20 26 26 20 69 4b 65 79 f( pPage && iKey
10d94 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 >pCache->iMaxKey
10d95 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e ){. pCache->
10d96 69 4d 61 78 4b 65 79 20 3d 20 69 4b 65 79 3b 0a iMaxKey = iKey;.
10d97 20 20 7d 0a 20 20 69 66 28 20 63 72 65 61 74 65 }. if( create
10d98 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c 69 74 65 Flag==1 ) sqlite
10d99 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 3EndBenignMalloc
10d9a 28 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 ();. pcache1Lea
10d9b 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 veMutex();. ret
10d9c 75 72 6e 20 28 70 50 61 67 65 20 3f 20 50 47 48 urn (pPage ? PGH
10d9d 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50 61 67 DR1_TO_PAGE(pPag
10d9e 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a e) : 0);.}.../*.
10d9f 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
10da0 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 n of the sqlite3
10da1 5f 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 20 6d _pcache.xUnpin m
10da2 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 4d 61 72 ethod..**.** Mar
10da3 6b 20 61 20 70 61 67 65 20 61 73 20 75 6e 70 69 k a page as unpi
10da4 6e 6e 65 64 20 28 65 6c 69 67 69 62 6c 65 20 66 nned (eligible f
10da5 6f 72 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 20 or asynchronous
10da6 72 65 63 79 63 6c 69 6e 67 29 2e 0a 2a 2f 0a 73 recycling)..*/.s
10da7 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
10da8 65 31 55 6e 70 69 6e 28 73 71 6c 69 74 65 33 5f e1Unpin(sqlite3_
10da9 70 63 61 63 68 65 20 2a 70 2c 20 76 6f 69 64 20 pcache *p, void
10daa 2a 70 50 67 2c 20 69 6e 74 20 72 65 75 73 65 55 *pPg, int reuseU
10dab 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20 50 43 61 63 nlikely){. PCac
10dac 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 he1 *pCache = (P
10dad 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 Cache1 *)p;. Pg
10dae 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 50 41 Hdr1 *pPage = PA
10daf 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61 GE_TO_PGHDR1(pCa
10db0 63 68 65 2c 20 70 50 67 29 3b 0a 20 0a 20 20 61 che, pPg);. . a
10db1 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 43 ssert( pPage->pC
10db2 61 63 68 65 3d 3d 70 43 61 63 68 65 20 29 3b 0a ache==pCache );.
10db3 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 pcache1EnterMu
10db4 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 49 74 20 tex();.. /* It
10db5 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 63 is an error to c
10db6 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f all this functio
10db7 6e 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 n if the page is
10db8 20 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a 20 70 already . ** p
10db9 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 art of the globa
10dba 6c 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20 2a 2f l LRU list.. */
10dbb 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
10dbc 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20 26 26 ->pLruPrev==0 &&
10dbd 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 pPage->pLruNext
10dbe 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
10dbf 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 pcache1.pLruHea
10dc0 64 21 3d 70 50 61 67 65 20 26 26 20 70 63 61 63 d!=pPage && pcac
10dc1 68 65 31 2e 70 4c 72 75 54 61 69 6c 21 3d 70 50 he1.pLruTail!=pP
10dc2 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20 72 65 age );.. if( re
10dc3 75 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c 20 70 useUnlikely || p
10dc4 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 cache1.nCurrentP
10dc5 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d 61 78 age>pcache1.nMax
10dc6 50 61 67 65 20 29 7b 0a 20 20 20 20 70 63 61 63 Page ){. pcac
10dc7 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 he1RemoveFromHas
10dc8 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20 70 63 h(pPage);. pc
10dc9 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70 50 ache1FreePage(pP
10dca 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 age);. }else{.
10dcb 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 70 61 /* Add the pa
10dcc 67 65 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c ge to the global
10dcd 20 4c 52 55 20 6c 69 73 74 2e 20 4e 6f 72 6d 61 LRU list. Norma
10dce 6c 6c 79 2c 20 74 68 65 20 70 61 67 65 20 69 73 lly, the page is
10dcf 20 61 64 64 65 64 20 74 6f 0a 20 20 20 20 2a 2a added to. **
10dd0 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 65 the head of the
10dd1 20 6c 69 73 74 20 28 6c 61 73 74 20 70 61 67 65 list (last page
10dd2 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65 64 29 to be recycled)
10dd3 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 . However, if th
10dd4 65 20 0a 20 20 20 20 2a 2a 20 72 65 75 73 65 55 e . ** reuseU
10dd5 6e 6c 69 6b 65 6c 79 20 66 6c 61 67 20 70 61 73 nlikely flag pas
10dd6 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 sed to this func
10dd7 74 69 6f 6e 20 69 73 20 74 72 75 65 2c 20 74 68 tion is true, th
10dd8 65 20 70 61 67 65 20 69 73 20 61 64 64 65 64 0a e page is added.
10dd9 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 61 ** to the ta
10dda 69 6c 20 6f 66 20 74 68 65 20 6c 69 73 74 20 28 il of the list (
10ddb 66 69 72 73 74 20 70 61 67 65 20 74 6f 20 62 65 first page to be
10ddc 20 72 65 63 79 63 6c 65 64 29 2e 0a 20 20 20 20 recycled)..
10ddd 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 61 63 68 */. if( pcach
10dde 65 31 2e 70 4c 72 75 48 65 61 64 20 29 7b 0a 20 e1.pLruHead ){.
10ddf 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 pcache1.pLr
10de0 75 48 65 61 64 2d 3e 70 4c 72 75 50 72 65 76 20 uHead->pLruPrev
10de1 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20 70 = pPage;. p
10de2 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d Page->pLruNext =
10de3 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 pcache1.pLruHea
10de4 64 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 d;. pcache1
10de5 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50 61 67 .pLruHead = pPag
10de6 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 e;. }else{.
10de7 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 pcache1.pLru
10de8 54 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a 20 20 Tail = pPage;.
10de9 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 pcache1.pLru
10dea 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 Head = pPage;.
10deb 20 20 7d 0a 20 20 20 20 70 43 61 63 68 65 2d 3e }. pCache->
10dec 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20 nRecyclable++;.
10ded 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 61 }.. pcache1Lea
10dee 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a veMutex();.}../*
10def 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
10df0 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 on of the sqlite
10df1 33 5f 70 63 61 63 68 65 2e 78 52 65 6b 65 79 20 3_pcache.xRekey
10df2 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 method. .*/.stat
10df3 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 52 ic void pcache1R
10df4 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33 5f ekey(. sqlite3_
10df5 70 63 61 63 68 65 20 2a 70 2c 0a 20 20 76 6f 69 pcache *p,. voi
10df6 64 20 2a 70 50 67 2c 0a 20 20 75 6e 73 69 67 6e d *pPg,. unsign
10df7 65 64 20 69 6e 74 20 69 4f 6c 64 2c 0a 20 20 75 ed int iOld,. u
10df8 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4e 65 77 nsigned int iNew
10df9 0a 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 .){. PCache1 *p
10dfa 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 Cache = (PCache1
10dfb 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 20 2a *)p;. PgHdr1 *
10dfc 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54 4f 5f pPage = PAGE_TO_
10dfd 50 47 48 44 52 31 28 70 43 61 63 68 65 2c 20 70 PGHDR1(pCache, p
10dfe 50 67 29 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a Pg);. PgHdr1 **
10dff 70 70 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 pp;. unsigned i
10e00 6e 74 20 68 3b 20 0a 20 20 61 73 73 65 72 74 28 nt h; . assert(
10e01 20 70 50 61 67 65 2d 3e 69 4b 65 79 3d 3d 69 4f pPage->iKey==iO
10e02 6c 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ld );. assert(
10e03 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d 70 pPage->pCache==p
10e04 43 61 63 68 65 20 29 3b 0a 0a 20 20 70 63 61 63 Cache );.. pcac
10e05 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b he1EnterMutex();
10e06 0a 0a 20 20 68 20 3d 20 69 4f 6c 64 25 70 43 61 .. h = iOld%pCa
10e07 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 70 che->nHash;. pp
10e08 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 48 61 = &pCache->apHa
10e09 73 68 5b 68 5d 3b 0a 20 20 77 68 69 6c 65 28 20 sh[h];. while(
10e0a 28 2a 70 70 29 21 3d 70 50 61 67 65 20 29 7b 0a (*pp)!=pPage ){.
10e0b 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 29 2d pp = &(*pp)-
10e0c 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 2a 70 >pNext;. }. *p
10e0d 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 p = pPage->pNext
10e0e 3b 0a 0a 20 20 68 20 3d 20 69 4e 65 77 25 70 43 ;.. h = iNew%pC
10e0f 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 ache->nHash;. p
10e10 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4e 65 Page->iKey = iNe
10e11 77 3b 0a 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 w;. pPage->pNex
10e12 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61 t = pCache->apHa
10e13 73 68 5b 68 5d 3b 0a 20 20 70 43 61 63 68 65 2d sh[h];. pCache-
10e14 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20 70 50 61 >apHash[h] = pPa
10e15 67 65 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 78 52 ge;.. /* The xR
10e16 65 6b 65 79 28 29 20 69 6e 74 65 72 66 61 63 65 ekey() interface
10e17 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 74 6f is only used to
10e18 20 6d 6f 76 65 20 70 61 67 65 73 20 65 61 72 6c move pages earl
10e19 69 65 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 ier in the. **
10e1a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 69 database file (i
10e1b 6e 20 6f 72 64 65 72 20 74 6f 20 6d 6f 76 65 20 n order to move
10e1c 61 6c 6c 20 66 72 65 65 20 70 61 67 65 73 20 74 all free pages t
10e1d 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 o the end of the
10e1e 0a 20 20 2a 2a 20 66 69 6c 65 20 77 68 65 72 65 . ** file where
10e1f 20 74 68 65 79 20 63 61 6e 20 62 65 20 74 72 75 they can be tru
10e20 6e 63 61 74 65 64 20 6f 66 66 2e 29 20 20 48 65 ncated off.) He
10e21 6e 63 65 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 nce, it is not p
10e22 6f 73 73 69 62 6c 65 0a 20 20 2a 2a 20 66 6f 72 ossible. ** for
10e23 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 6e 75 the new page nu
10e24 6d 62 65 72 20 74 6f 20 62 65 20 67 72 65 61 74 mber to be great
10e25 65 72 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67 er than the larg
10e26 65 73 74 20 70 72 65 76 69 6f 75 73 6c 79 0a 20 est previously.
10e27 20 2a 2a 20 66 65 74 63 68 65 64 20 70 61 67 65 ** fetched page
10e28 2e 20 20 42 75 74 20 77 65 20 72 65 74 61 69 6e . But we retain
10e29 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 the following t
10e2a 65 73 74 20 69 6e 20 63 61 73 65 20 78 52 65 6b est in case xRek
10e2b 65 79 28 29 0a 20 20 2a 2a 20 62 65 67 69 6e 73 ey(). ** begins
10e2c 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e 20 64 to be used in d
10e2d 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 69 6e ifferent ways in
10e2e 20 74 68 65 20 66 75 74 75 72 65 2e 0a 20 20 2a the future.. *
10e2f 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69 4e /. if( NEVER(iN
10e30 65 77 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b ew>pCache->iMaxK
10e31 65 79 29 20 29 7b 0a 20 20 20 20 70 43 61 63 68 ey) ){. pCach
10e32 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4e 65 e->iMaxKey = iNe
10e33 77 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 w;. }.. pcache
10e34 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 1LeaveMutex();.}
10e35 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
10e36 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 tation of the sq
10e37 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 54 72 lite3_pcache.xTr
10e38 75 6e 63 61 74 65 20 6d 65 74 68 6f 64 2e 20 0a uncate method. .
10e39 2a 2a 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c **.** Discard al
10e3a 6c 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 l unpinned pages
10e3b 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 77 69 in the cache wi
10e3c 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 th a page number
10e3d 20 65 71 75 61 6c 20 74 6f 0a 2a 2a 20 6f 72 20 equal to.** or
10e3e 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 61 72 greater than par
10e3f 61 6d 65 74 65 72 20 69 4c 69 6d 69 74 2e 20 41 ameter iLimit. A
10e40 6e 79 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20 ny pinned pages
10e41 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 with a page numb
10e42 65 72 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6f er.** equal to o
10e43 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 69 r greater than i
10e44 4c 69 6d 69 74 20 61 72 65 20 69 6d 70 6c 69 63 Limit are implic
10e45 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64 2e 0a 2a itly unpinned..*
10e46 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 /.static void pc
10e47 61 63 68 65 31 54 72 75 6e 63 61 74 65 28 73 71 ache1Truncate(sq
10e48 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c lite3_pcache *p,
10e49 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c unsigned int iL
10e4a 69 6d 69 74 29 7b 0a 20 20 50 43 61 63 68 65 31 imit){. PCache1
10e4b 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 *pCache = (PCac
10e4c 68 65 31 20 2a 29 70 3b 0a 20 20 70 63 61 63 68 he1 *)p;. pcach
10e4d 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a e1EnterMutex();.
10e4e 20 20 69 66 28 20 69 4c 69 6d 69 74 3c 3d 70 43 if( iLimit<=pC
10e4f 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b ache->iMaxKey ){
10e50 0a 20 20 20 20 70 63 61 63 68 65 31 54 72 75 6e . pcache1Trun
10e51 63 61 74 65 55 6e 73 61 66 65 28 70 43 61 63 68 cateUnsafe(pCach
10e52 65 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 e, iLimit);.
10e53 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 pCache->iMaxKey
10e54 3d 20 69 4c 69 6d 69 74 2d 31 3b 0a 20 20 7d 0a = iLimit-1;. }.
10e55 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 pcache1LeaveMu
10e56 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 tex();.}../*.**
10e57 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
10e58 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 f the sqlite3_pc
10e59 61 63 68 65 2e 78 44 65 73 74 72 6f 79 20 6d 65 ache.xDestroy me
10e5a 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 65 73 thod. .**.** Des
10e5b 74 72 6f 79 20 61 20 63 61 63 68 65 20 61 6c 6c troy a cache all
10e5c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 70 63 61 ocated using pca
10e5d 63 68 65 31 43 72 65 61 74 65 28 29 2e 0a 2a 2f che1Create()..*/
10e5e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 .static void pca
10e5f 63 68 65 31 44 65 73 74 72 6f 79 28 73 71 6c 69 che1Destroy(sqli
10e60 74 65 33 5f 70 63 61 63 68 65 20 2a 70 29 7b 0a te3_pcache *p){.
10e61 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 PCache1 *pCach
10e62 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 e = (PCache1 *)p
10e63 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
10e64 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 63 68 Mutex();. pcach
10e65 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66 65 e1TruncateUnsafe
10e66 28 70 43 61 63 68 65 2c 20 30 29 3b 0a 20 20 70 (pCache, 0);. p
10e67 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 cache1.nMaxPage
10e68 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b -= pCache->nMax;
10e69 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 . pcache1.nMinP
10e6a 61 67 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e age -= pCache->n
10e6b 4d 69 6e 3b 0a 20 20 70 63 61 63 68 65 31 45 6e Min;. pcache1En
10e6c 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29 3b 0a forceMaxPage();.
10e6d 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 pcache1LeaveMu
10e6e 74 65 78 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 tex();. sqlite3
10e6f 5f 66 72 65 65 28 70 43 61 63 68 65 2d 3e 61 70 _free(pCache->ap
10e70 48 61 73 68 29 3b 0a 20 20 73 71 6c 69 74 65 33 Hash);. sqlite3
10e71 5f 66 72 65 65 28 70 43 61 63 68 65 29 3b 0a 7d _free(pCache);.}
10e72 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
10e73 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
10e74 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a during initializ
10e75 61 74 69 6f 6e 20 28 73 71 6c 69 74 65 33 5f 69 ation (sqlite3_i
10e76 6e 69 74 69 61 6c 69 7a 65 28 29 29 20 74 6f 0a nitialize()) to.
10e77 2a 2a 20 69 6e 73 74 61 6c 6c 20 74 68 65 20 64 ** install the d
10e78 65 66 61 75 6c 74 20 70 6c 75 67 67 61 62 6c 65 efault pluggable
10e79 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 2c 20 61 cache module, a
10e7a 73 73 75 6d 69 6e 67 20 74 68 65 20 75 73 65 72 ssuming the user
10e7b 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 has not.** alre
10e7c 61 64 79 20 70 72 6f 76 69 64 65 64 20 61 6e 20 ady provided an
10e7d 61 6c 74 65 72 6e 61 74 69 76 65 2e 0a 2a 2f 0a alternative..*/.
10e7e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
10e7f 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 oid sqlite3PCach
10e80 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 eSetDefault(void
10e81 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 ){. static sqli
10e82 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
10e83 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 ds defaultMethod
10e84 73 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20 20 20 s = {. 0,
10e85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10e86 20 20 20 2f 2a 20 70 41 72 67 20 2a 2f 0a 20 20 /* pArg */.
10e87 20 20 70 63 61 63 68 65 31 49 6e 69 74 2c 20 20 pcache1Init,
10e88 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 49 /* xI
10e89 6e 69 74 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 nit */. pcach
10e8a 65 31 53 68 75 74 64 6f 77 6e 2c 20 20 20 20 20 e1Shutdown,
10e8b 20 20 20 20 2f 2a 20 78 53 68 75 74 64 6f 77 6e /* xShutdown
10e8c 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 43 */. pcache1C
10e8d 72 65 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 reate,
10e8e 20 2f 2a 20 78 43 72 65 61 74 65 20 2a 2f 0a 20 /* xCreate */.
10e8f 20 20 20 70 63 61 63 68 65 31 43 61 63 68 65 73 pcache1Caches
10e90 69 7a 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 ize, /* x
10e91 43 61 63 68 65 73 69 7a 65 20 2a 2f 0a 20 20 20 Cachesize */.
10e92 20 70 63 61 63 68 65 31 50 61 67 65 63 6f 75 6e pcache1Pagecoun
10e93 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 50 61 t, /* xPa
10e94 67 65 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 70 gecount */. p
10e95 63 61 63 68 65 31 46 65 74 63 68 2c 20 20 20 20 cache1Fetch,
10e96 20 20 20 20 20 20 20 20 2f 2a 20 78 46 65 74 63 /* xFetc
10e97 68 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 h */. pcache1
10e98 55 6e 70 69 6e 2c 20 20 20 20 20 20 20 20 20 20 Unpin,
10e99 20 20 2f 2a 20 78 55 6e 70 69 6e 20 2a 2f 0a 20 /* xUnpin */.
10e9a 20 20 20 70 63 61 63 68 65 31 52 65 6b 65 79 2c pcache1Rekey,
10e9b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
10e9c 52 65 6b 65 79 20 2a 2f 0a 20 20 20 20 70 63 61 Rekey */. pca
10e9d 63 68 65 31 54 72 75 6e 63 61 74 65 2c 20 20 20 che1Truncate,
10e9e 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 /* xTrunca
10e9f 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 te */. pcache
10ea0 31 44 65 73 74 72 6f 79 20 20 20 20 20 20 20 20 1Destroy
10ea1 20 20 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 2a /* xDestroy *
10ea2 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 /. };. sqlite3
10ea3 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 _config(SQLITE_C
10ea4 4f 4e 46 49 47 5f 50 43 41 43 48 45 2c 20 26 64 ONFIG_PCACHE, &d
10ea5 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a efaultMethods);.
10ea6 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
10ea7 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d _ENABLE_MEMORY_M
10ea8 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 ANAGEMENT./*.**
10ea9 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
10eaa 20 63 61 6c 6c 65 64 20 74 6f 20 66 72 65 65 20 called to free
10eab 73 75 70 65 72 66 6c 75 6f 75 73 20 64 79 6e 61 superfluous dyna
10eac 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 mically allocate
10ead 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 65 6c 64 d memory.** held
10eae 20 62 79 20 74 68 65 20 70 61 67 65 72 20 73 79 by the pager sy
10eaf 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 20 69 6e 20 stem. Memory in
10eb0 75 73 65 20 62 79 20 61 6e 79 20 53 51 4c 69 74 use by any SQLit
10eb1 65 20 70 61 67 65 72 20 61 6c 6c 6f 63 61 74 65 e pager allocate
10eb2 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 75 72 72 d.** by the curr
10eb3 65 6e 74 20 74 68 72 65 61 64 20 6d 61 79 20 62 ent thread may b
10eb4 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 e sqlite3_free()
10eb5 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 65 71 20 69 ed..**.** nReq i
10eb6 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
10eb7 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 bytes of memory
10eb8 72 65 71 75 69 72 65 64 2e 20 4f 6e 63 65 20 74 required. Once t
10eb9 68 69 73 20 6d 75 63 68 20 68 61 73 0a 2a 2a 20 his much has.**
10eba 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2c 20 74 been released, t
10ebb 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 he function retu
10ebc 72 6e 73 2e 20 54 68 65 20 72 65 74 75 72 6e 20 rns. The return
10ebd 76 61 6c 75 65 20 69 73 20 74 68 65 20 74 6f 74 value is the tot
10ebe 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a 20 6f 66 al number .** of
10ebf 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 bytes of memory
10ec0 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f 0a 53 51 released..*/.SQ
10ec1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
10ec2 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
10ec3 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20 leaseMemory(int
10ec4 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20 6e 46 72 nReq){. int nFr
10ec5 65 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 63 ee = 0;. if( pc
10ec6 61 63 68 65 31 2e 70 53 74 61 72 74 3d 3d 30 20 ache1.pStart==0
10ec7 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70 ){. PgHdr1 *p
10ec8 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 ;. pcache1Ent
10ec9 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 77 erMutex();. w
10eca 68 69 6c 65 28 20 28 6e 52 65 71 3c 30 20 7c 7c hile( (nReq<0 ||
10ecb 20 6e 46 72 65 65 3c 6e 52 65 71 29 20 26 26 20 nFree<nReq) &&
10ecc 28 70 3d 70 63 61 63 68 65 31 2e 70 4c 72 75 54 (p=pcache1.pLruT
10ecd 61 69 6c 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 ail) ){. nF
10ece 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 4d 61 ree += sqlite3Ma
10ecf 6c 6c 6f 63 53 69 7a 65 28 50 47 48 44 52 31 5f llocSize(PGHDR1_
10ed0 54 4f 5f 50 41 47 45 28 70 29 29 3b 0a 20 20 20 TO_PAGE(p));.
10ed1 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 pcache1PinPag
10ed2 65 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63 e(p);. pcac
10ed3 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 he1RemoveFromHas
10ed4 68 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63 h(p);. pcac
10ed5 68 65 31 46 72 65 65 50 61 67 65 28 70 29 3b 0a he1FreePage(p);.
10ed6 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65 }. pcache
10ed7 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 1LeaveMutex();.
10ed8 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65 }. return nFre
10ed9 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 e;.}.#endif /* S
10eda 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
10edb 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 20 2a ORY_MANAGEMENT *
10edc 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 /..#ifdef SQLITE
10edd 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 _TEST./*.** This
10ede 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
10edf 64 20 62 79 20 74 65 73 74 20 70 72 6f 63 65 64 d by test proced
10ee0 75 72 65 73 20 74 6f 20 69 6e 73 70 65 63 74 20 ures to inspect
10ee1 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 the internal sta
10ee2 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 67 6c 6f te.** of the glo
10ee3 62 61 6c 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 bal cache..*/.SQ
10ee4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
10ee5 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 d sqlite3PcacheS
10ee6 74 61 74 73 28 0a 20 20 69 6e 74 20 2a 70 6e 43 tats(. int *pnC
10ee7 75 72 72 65 6e 74 2c 20 20 20 20 20 20 2f 2a 20 urrent, /*
10ee8 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 OUT: Total numbe
10ee9 72 20 6f 66 20 70 61 67 65 73 20 63 61 63 68 65 r of pages cache
10eea 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 61 d */. int *pnMa
10eeb 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f x, /* O
10eec 55 54 3a 20 47 6c 6f 62 61 6c 20 6d 61 78 69 6d UT: Global maxim
10eed 75 6d 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f um cache size */
10eee 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 6e 2c 20 20 . int *pnMin,
10eef 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 /* OUT:
10ef0 53 75 6d 20 6f 66 20 50 43 61 63 68 65 31 2e 6e Sum of PCache1.n
10ef1 4d 69 6e 20 66 6f 72 20 70 75 72 67 65 61 62 6c Min for purgeabl
10ef2 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e e caches */. in
10ef3 74 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c 65 20 t *pnRecyclable
10ef4 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c /* OUT: Total
10ef5 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
10ef6 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 72 available for r
10ef7 65 63 79 63 6c 69 6e 67 20 2a 2f 0a 29 7b 0a 20 ecycling */.){.
10ef8 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 69 6e PgHdr1 *p;. in
10ef9 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 20 3d 20 t nRecyclable =
10efa 30 3b 0a 20 20 66 6f 72 28 70 3d 70 63 61 63 68 0;. for(p=pcach
10efb 65 31 2e 70 4c 72 75 48 65 61 64 3b 20 70 3b 20 e1.pLruHead; p;
10efc 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74 29 7b 0a p=p->pLruNext){.
10efd 20 20 20 20 6e 52 65 63 79 63 6c 61 62 6c 65 2b nRecyclable+
10efe 2b 3b 0a 20 20 7d 0a 20 20 2a 70 6e 43 75 72 72 +;. }. *pnCurr
10eff 65 6e 74 20 3d 20 70 63 61 63 68 65 31 2e 6e 43 ent = pcache1.nC
10f00 75 72 72 65 6e 74 50 61 67 65 3b 0a 20 20 2a 70 urrentPage;. *p
10f01 6e 4d 61 78 20 3d 20 70 63 61 63 68 65 31 2e 6e nMax = pcache1.n
10f02 4d 61 78 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 69 MaxPage;. *pnMi
10f03 6e 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e n = pcache1.nMin
10f04 50 61 67 65 3b 0a 20 20 2a 70 6e 52 65 63 79 63 Page;. *pnRecyc
10f05 6c 61 62 6c 65 20 3d 20 6e 52 65 63 79 63 6c 61 lable = nRecycla
10f06 62 6c 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f ble;.}.#endif../
10f07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
10f08 6e 64 20 6f 66 20 70 63 61 63 68 65 31 2e 63 20 nd of pcache1.c
10f09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10f0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10f0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
10f0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
10f0d 65 67 69 6e 20 66 69 6c 65 20 72 6f 77 73 65 74 egin file rowset
10f0e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
10f0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
10f11 2a 0a 2a 2a 20 32 30 30 38 20 44 65 63 65 6d 62 *.** 2008 Decemb
10f12 65 72 20 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 er 3.**.** The a
10f13 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
10f14 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
10f15 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
10f16 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
10f17 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
10f18 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
10f19 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
10f1a 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
10f1b 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
10f1c 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
10f1d 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
10f1e 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
10f1f 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
10f20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
10f21 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
10f22 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
10f23 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
10f24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10f25 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10f26 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10f27 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10f28 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d ***.**.** This m
10f29 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 odule implements
10f2a 20 61 6e 20 6f 62 6a 65 63 74 20 77 65 20 63 61 an object we ca
10f2b 6c 6c 20 61 20 22 52 6f 77 53 65 74 22 2e 0a 2a ll a "RowSet"..*
10f2c 2a 0a 2a 2a 20 54 68 65 20 52 6f 77 53 65 74 20 *.** The RowSet
10f2d 6f 62 6a 65 63 74 20 69 73 20 61 20 63 6f 6c 6c object is a coll
10f2e 65 63 74 69 6f 6e 20 6f 66 20 72 6f 77 69 64 73 ection of rowids
10f2f 2e 20 20 52 6f 77 69 64 73 0a 2a 2a 20 61 72 65 . Rowids.** are
10f30 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 inserted into t
10f31 68 65 20 52 6f 77 53 65 74 20 69 6e 20 61 6e 20 he RowSet in an
10f32 61 72 62 69 74 72 61 72 79 20 6f 72 64 65 72 2e arbitrary order.
10f33 20 20 49 6e 73 65 72 74 73 0a 2a 2a 20 63 61 6e Inserts.** can
10f34 20 62 65 20 69 6e 74 65 72 6d 69 78 65 64 20 77 be intermixed w
10f35 69 74 68 20 74 65 73 74 73 20 74 6f 20 73 65 65 ith tests to see
10f36 20 69 66 20 61 20 67 69 76 65 6e 20 72 6f 77 69 if a given rowi
10f37 64 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 d has been.** pr
10f38 65 76 69 6f 75 73 6c 79 20 69 6e 73 65 72 74 65 eviously inserte
10f39 64 20 69 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 d into the RowSe
10f3a 74 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 t..**.** After a
10f3b 6c 6c 20 69 6e 73 65 72 74 73 20 61 72 65 20 66 ll inserts are f
10f3c 69 6e 69 73 68 65 64 2c 20 69 74 20 69 73 20 70 inished, it is p
10f3d 6f 73 73 69 62 6c 65 20 74 6f 20 65 78 74 72 61 ossible to extra
10f3e 63 74 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e ct the.** elemen
10f3f 74 73 20 6f 66 20 74 68 65 20 52 6f 77 53 65 74 ts of the RowSet
10f40 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 in sorted order
10f41 2e 20 20 4f 6e 63 65 20 74 68 69 73 20 65 78 74 . Once this ext
10f42 72 61 63 74 69 6f 6e 0a 2a 2a 20 70 72 6f 63 65 raction.** proce
10f43 73 73 20 68 61 73 20 73 74 61 72 74 65 64 2c 20 ss has started,
10f44 6e 6f 20 6e 65 77 20 65 6c 65 6d 65 6e 74 73 20 no new elements
10f45 6d 61 79 20 62 65 20 69 6e 73 65 72 74 65 64 2e may be inserted.
10f46 0a 2a 2a 0a 2a 2a 20 48 65 6e 63 65 2c 20 74 68 .**.** Hence, th
10f47 65 20 70 72 69 6d 69 74 69 76 65 20 6f 70 65 72 e primitive oper
10f48 61 74 69 6f 6e 73 20 66 6f 72 20 61 20 52 6f 77 ations for a Row
10f49 53 65 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 Set are:.**.**
10f4a 20 20 43 52 45 41 54 45 0a 2a 2a 20 20 20 20 49 CREATE.** I
10f4b 4e 53 45 52 54 0a 2a 2a 20 20 20 20 54 45 53 54 NSERT.** TEST
10f4c 0a 2a 2a 20 20 20 20 53 4d 41 4c 4c 45 53 54 0a .** SMALLEST.
10f4d 2a 2a 20 20 20 20 44 45 53 54 52 4f 59 0a 2a 2a ** DESTROY.**
10f4e 0a 2a 2a 20 54 68 65 20 43 52 45 41 54 45 20 61 .** The CREATE a
10f4f 6e 64 20 44 45 53 54 52 4f 59 20 70 72 69 6d 69 nd DESTROY primi
10f50 74 69 76 65 73 20 61 72 65 20 74 68 65 20 63 6f tives are the co
10f51 6e 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 65 nstructor and de
10f52 73 74 72 75 63 74 6f 72 2c 0a 2a 2a 20 6f 62 76 structor,.** obv
10f53 69 6f 75 73 6c 79 2e 20 20 54 68 65 20 49 4e 53 iously. The INS
10f54 45 52 54 20 70 72 69 6d 69 74 69 76 65 20 61 64 ERT primitive ad
10f55 64 73 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 ds a new element
10f56 20 74 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a to the RowSet..
10f57 2a 2a 20 54 45 53 54 20 63 68 65 63 6b 73 20 74 ** TEST checks t
10f58 6f 20 73 65 65 20 69 66 20 61 6e 20 65 6c 65 6d o see if an elem
10f59 65 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 ent is already i
10f5a 6e 20 74 68 65 20 52 6f 77 53 65 74 2e 20 20 53 n the RowSet. S
10f5b 4d 41 4c 4c 45 53 54 0a 2a 2a 20 65 78 74 72 61 MALLEST.** extra
10f5c 63 74 73 20 74 68 65 20 6c 65 61 73 74 20 76 61 cts the least va
10f5d 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 lue from the Row
10f5e 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 Set..**.** The I
10f5f 4e 53 45 52 54 20 70 72 69 6d 69 74 69 76 65 20 NSERT primitive
10f60 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 might allocate a
10f61 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 dditional memory
10f62 2e 20 20 4d 65 6d 6f 72 79 20 69 73 0a 2a 2a 20 . Memory is.**
10f63 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 63 68 75 allocated in chu
10f64 6e 6b 73 20 73 6f 20 6d 6f 73 74 20 49 4e 53 45 nks so most INSE
10f65 52 54 73 20 64 6f 20 6e 6f 20 61 6c 6c 6f 63 61 RTs do no alloca
10f66 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 69 73 20 tion. There is
10f67 61 6e 20 0a 2a 2a 20 75 70 70 65 72 20 62 6f 75 an .** upper bou
10f68 6e 64 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f nd on the size o
10f69 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f f allocated memo
10f6a 72 79 2e 20 20 4e 6f 20 6d 65 6d 6f 72 79 20 69 ry. No memory i
10f6b 73 20 66 72 65 65 64 0a 2a 2a 20 75 6e 74 69 6c s freed.** until
10f6c 20 44 45 53 54 52 4f 59 2e 0a 2a 2a 0a 2a 2a 20 DESTROY..**.**
10f6d 54 68 65 20 54 45 53 54 20 70 72 69 6d 69 74 69 The TEST primiti
10f6e 76 65 20 69 6e 63 6c 75 64 65 73 20 61 20 22 62 ve includes a "b
10f6f 61 74 63 68 22 20 6e 75 6d 62 65 72 2e 20 20 54 atch" number. T
10f70 68 65 20 54 45 53 54 20 70 72 69 6d 69 74 69 76 he TEST primitiv
10f71 65 0a 2a 2a 20 77 69 6c 6c 20 6f 6e 6c 79 20 73 e.** will only s
10f72 65 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 ee elements that
10f73 20 77 65 72 65 20 69 6e 73 65 72 74 65 64 20 62 were inserted b
10f74 65 66 6f 72 65 20 74 68 65 20 6c 61 73 74 20 63 efore the last c
10f75 68 61 6e 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20 hange.** in the
10f76 62 61 74 63 68 20 6e 75 6d 62 65 72 2e 20 20 49 batch number. I
10f77 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 n other words, i
10f78 66 20 61 6e 20 49 4e 53 45 52 54 20 6f 63 63 75 f an INSERT occu
10f79 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 77 rs between.** tw
10f7a 6f 20 54 45 53 54 73 20 77 68 65 72 65 20 74 68 o TESTs where th
10f7b 65 20 54 45 53 54 73 20 68 61 76 65 20 74 68 65 e TESTs have the
10f7c 20 73 61 6d 65 20 62 61 74 63 68 20 6e 75 62 6d same batch nubm
10f7d 65 72 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 er, then the.**
10f7e 76 61 6c 75 65 20 61 64 64 65 64 20 62 79 20 74 value added by t
10f7f 68 65 20 49 4e 53 45 52 54 20 77 69 6c 6c 20 6e he INSERT will n
10f80 6f 74 20 62 65 20 76 69 73 69 62 6c 65 20 74 6f ot be visible to
10f81 20 74 68 65 20 73 65 63 6f 6e 64 20 54 45 53 54 the second TEST
10f82 2e 0a 2a 2a 20 54 68 65 20 69 6e 69 74 69 61 6c ..** The initial
10f83 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73 batch number is
10f84 20 7a 65 72 6f 2c 20 73 6f 20 69 66 20 74 68 65 zero, so if the
10f85 20 76 65 72 79 20 66 69 72 73 74 20 54 45 53 54 very first TEST
10f86 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 6e contains.** a n
10f87 6f 6e 2d 7a 65 72 6f 20 62 61 74 63 68 20 6e 75 on-zero batch nu
10f88 6d 62 65 72 2c 20 69 74 20 77 69 6c 6c 20 73 65 mber, it will se
10f89 65 20 61 6c 6c 20 70 72 69 6f 72 20 49 4e 53 45 e all prior INSE
10f8a 52 54 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 49 4e RTs..**.** No IN
10f8b 53 45 52 54 73 20 6d 61 79 20 6f 63 63 75 72 73 SERTs may occurs
10f8c 20 61 66 74 65 72 20 61 20 53 4d 41 4c 4c 45 53 after a SMALLES
10f8d 54 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e T. An assertion
10f8e 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 0a 2a 2a will fail if.**
10f8f 20 74 68 61 74 20 69 73 20 61 74 74 65 6d 70 74 that is attempt
10f90 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f ed..**.** The co
10f91 73 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 st of an INSERT
10f92 69 73 20 72 6f 75 67 68 6c 79 20 63 6f 6e 73 74 is roughly const
10f93 61 6e 74 2e 20 20 28 53 6f 6d 65 74 69 6d 65 20 ant. (Sometime
10f94 6e 65 77 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 61 new memory.** ha
10f95 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 s to be allocate
10f96 64 20 6f 6e 20 61 6e 20 49 4e 53 45 52 54 2e 29 d on an INSERT.)
10f97 20 20 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20 The cost of a
10f98 54 45 53 54 20 77 69 74 68 20 61 20 6e 65 77 0a TEST with a new.
10f99 2a 2a 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 ** batch number
10f9a 69 73 20 4f 28 4e 6c 6f 67 4e 29 20 77 68 65 72 is O(NlogN) wher
10f9b 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 e N is the numbe
10f9c 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e r of elements in
10f9d 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 the RowSet..**
10f9e 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20 54 45 The cost of a TE
10f9f 53 54 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d ST using the sam
10fa0 65 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 e batch number i
10fa1 73 20 4f 28 6c 6f 67 4e 29 2e 20 20 54 68 65 20 s O(logN). The
10fa2 63 6f 73 74 0a 2a 2a 20 6f 66 20 74 68 65 20 66 cost.** of the f
10fa3 69 72 73 74 20 53 4d 41 4c 4c 45 53 54 20 69 73 irst SMALLEST is
10fa4 20 4f 28 4e 6c 6f 67 4e 29 2e 20 20 53 65 63 6f O(NlogN). Seco
10fa5 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e nd and subsequen
10fa6 74 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 70 72 t SMALLEST.** pr
10fa7 69 6d 69 74 69 76 65 73 20 61 72 65 20 63 6f 6e imitives are con
10fa8 73 74 61 6e 74 20 74 69 6d 65 2e 20 20 54 68 65 stant time. The
10fa9 20 63 6f 73 74 20 6f 66 20 44 45 53 54 52 4f 59 cost of DESTROY
10faa 20 69 73 20 4f 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20 is O(N)..**.**
10fab 54 68 65 72 65 20 69 73 20 61 6e 20 61 64 64 65 There is an adde
10fac 64 20 63 6f 73 74 20 6f 66 20 4f 28 4e 29 20 77 d cost of O(N) w
10fad 68 65 6e 20 73 77 69 74 63 68 69 6e 67 20 62 65 hen switching be
10fae 74 77 65 65 6e 20 54 45 53 54 20 61 6e 64 0a 2a tween TEST and.*
10faf 2a 20 53 4d 41 4c 4c 45 53 54 20 70 72 69 6d 69 * SMALLEST primi
10fb0 74 69 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 tives..**.** $Id
10fb1 3a 20 72 6f 77 73 65 74 2e 63 2c 76 20 31 2e 37 : rowset.c,v 1.7
10fb2 20 32 30 30 39 2f 30 35 2f 32 32 20 30 31 3a 30 2009/05/22 01:0
10fb3 30 3a 31 33 20 64 72 68 20 45 78 70 20 24 0a 2a 0:13 drh Exp $.*
10fb4 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 61 72 67 65 74 /.../*.** Target
10fb5 20 73 69 7a 65 20 66 6f 72 20 61 6c 6c 6f 63 61 size for alloca
10fb6 74 69 6f 6e 20 63 68 75 6e 6b 73 2e 0a 2a 2f 0a tion chunks..*/.
10fb7 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f 41 #define ROWSET_A
10fb8 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a 45 20 31 LLOCATION_SIZE 1
10fb9 30 32 34 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 024../*.** The n
10fba 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 65 74 20 umber of rowset
10fbb 65 6e 74 72 69 65 73 20 70 65 72 20 61 6c 6c 6f entries per allo
10fbc 63 61 74 69 6f 6e 20 63 68 75 6e 6b 2e 0a 2a 2f cation chunk..*/
10fbd 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f .#define ROWSET_
10fbe 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 20 ENTRY_PER_CHUNK
10fbf 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
10fc0 20 20 20 20 20 20 20 20 20 20 28 28 52 4f 57 53 ((ROWS
10fc1 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 ET_ALLOCATION_SI
10fc2 5a 45 2d 38 29 2f 73 69 7a 65 6f 66 28 73 74 72 ZE-8)/sizeof(str
10fc3 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 29 uct RowSetEntry)
10fc4 29 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 65 6e )../*.** Each en
10fc5 74 72 79 20 69 6e 20 61 20 52 6f 77 53 65 74 20 try in a RowSet
10fc6 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
10fc7 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
10fc8 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75 63 object..*/.struc
10fc9 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 7b 20 t RowSetEntry {
10fca 20 20 20 20 20 20 20 20 20 20 20 0a 20 20 69 36 . i6
10fcb 34 20 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 4 v;
10fcc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
10fcd 4f 57 49 44 20 76 61 6c 75 65 20 66 6f 72 20 74 OWID value for t
10fce 68 69 73 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 his entry */. s
10fcf 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
10fd0 79 20 2a 70 52 69 67 68 74 3b 20 20 20 2f 2a 20 y *pRight; /*
10fd1 52 69 67 68 74 20 73 75 62 74 72 65 65 20 28 6c Right subtree (l
10fd2 61 72 67 65 72 20 65 6e 74 72 69 65 73 29 20 6f arger entries) o
10fd3 72 20 6c 69 73 74 20 2a 2f 0a 20 20 73 74 72 75 r list */. stru
10fd4 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
10fd5 70 4c 65 66 74 3b 20 20 20 20 2f 2a 20 4c 65 66 pLeft; /* Lef
10fd6 74 20 73 75 62 74 72 65 65 20 28 73 6d 61 6c 6c t subtree (small
10fd7 65 72 20 65 6e 74 72 69 65 73 29 20 2a 2f 0a 7d er entries) */.}
10fd8 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 77 53 65 74 45 ;../*.** RowSetE
10fd9 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 61 72 65 ntry objects are
10fda 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 6c 61 allocated in la
10fdb 72 67 65 20 63 68 75 6e 6b 73 20 28 69 6e 73 74 rge chunks (inst
10fdc 61 6e 63 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 ances of the.**
10fdd 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
10fde 75 72 65 29 20 74 6f 20 72 65 64 75 63 65 20 6d ure) to reduce m
10fdf 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
10fe0 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 65 0a overhead. The.
10fe1 2a 2a 20 63 68 75 6e 6b 73 20 61 72 65 20 6b 65 ** chunks are ke
10fe2 70 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c pt on a linked l
10fe3 69 73 74 20 73 6f 20 74 68 61 74 20 74 68 65 79 ist so that they
10fe4 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61 can be dealloca
10fe5 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 ted.** when the
10fe6 52 6f 77 53 65 74 20 69 73 20 64 65 73 74 72 6f RowSet is destro
10fe7 79 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 yed..*/.struct R
10fe8 6f 77 53 65 74 43 68 75 6e 6b 20 7b 0a 20 20 73 owSetChunk {. s
10fe9 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e truct RowSetChun
10fea 6b 20 2a 70 4e 65 78 74 43 68 75 6e 6b 3b 20 20 k *pNextChunk;
10feb 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 /* Next ch
10fec 75 6e 6b 20 6f 6e 20 6c 69 73 74 20 6f 66 20 74 unk on list of t
10fed 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 73 74 72 hem all */. str
10fee 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
10fef 61 45 6e 74 72 79 5b 52 4f 57 53 45 54 5f 45 4e aEntry[ROWSET_EN
10ff0 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 5d 3b 20 TRY_PER_CHUNK];
10ff1 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 65 6e 74 /* Allocated ent
10ff2 72 69 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a ries */.};../*.*
10ff3 2a 20 41 20 52 6f 77 53 65 74 20 69 6e 20 61 6e * A RowSet in an
10ff4 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
10ff5 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
10ff6 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 79 ture..**.** A ty
10ff7 70 65 64 65 66 20 6f 66 20 74 68 69 73 20 73 74 pedef of this st
10ff8 72 75 63 74 75 72 65 20 69 66 20 66 6f 75 6e 64 ructure if found
10ff9 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e in sqliteInt.h.
10ffa 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53 65 .*/.struct RowSe
10ffb 74 20 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 t {. struct Row
10ffc 53 65 74 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b SetChunk *pChunk
10ffd 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 ; /* List of
10ffe 61 6c 6c 20 63 68 75 6e 6b 20 61 6c 6c 6f 63 61 all chunk alloca
10fff 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 71 6c 69 74 tions */. sqlit
11000 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 e3 *db;
11001 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
11002 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
11003 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 tion */. struct
11004 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45 RowSetEntry *pE
11005 6e 74 72 79 3b 20 20 20 20 2f 2a 20 4c 69 73 74 ntry; /* List
11006 20 6f 66 20 65 6e 74 72 69 65 73 20 75 73 69 6e of entries usin
11007 67 20 70 52 69 67 68 74 20 2a 2f 0a 20 20 73 74 g pRight */. st
11008 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
11009 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 *pLast; /*
1100a 4c 61 73 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 Last entry on th
1100b 65 20 70 45 6e 74 72 79 20 6c 69 73 74 20 2a 2f e pEntry list */
1100c 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
1100d 45 6e 74 72 79 20 2a 70 46 72 65 73 68 3b 20 20 Entry *pFresh;
1100e 20 20 2f 2a 20 53 6f 75 72 63 65 20 6f 66 20 6e /* Source of n
1100f 65 77 20 65 6e 74 72 79 20 6f 62 6a 65 63 74 73 ew entry objects
11010 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 */. struct Row
11011 53 65 74 45 6e 74 72 79 20 2a 70 54 72 65 65 3b SetEntry *pTree;
11012 20 20 20 20 20 2f 2a 20 42 69 6e 61 72 79 20 74 /* Binary t
11013 72 65 65 20 6f 66 20 65 6e 74 72 69 65 73 20 2a ree of entries *
11014 2f 0a 20 20 75 31 36 20 6e 46 72 65 73 68 3b 20 /. u16 nFresh;
11015 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11016 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
11017 6f 62 6a 65 63 74 73 20 6f 6e 20 70 46 72 65 73 objects on pFres
11018 68 20 2a 2f 0a 20 20 75 38 20 69 73 53 6f 72 74 h */. u8 isSort
11019 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ed;
1101a 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
1101b 20 70 45 6e 74 72 79 20 69 73 20 73 6f 72 74 65 pEntry is sorte
1101c 64 20 2a 2f 0a 20 20 75 38 20 69 42 61 74 63 68 d */. u8 iBatch
1101d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1101e 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 /* Current
1101f 20 69 6e 73 65 72 74 20 62 61 74 63 68 20 2a 2f insert batch */
11020 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 .};../*.** Turn
11021 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f bulk memory into
11022 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 a RowSet object
11023 2e 20 20 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 . N bytes of me
11024 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 76 61 69 mory.** are avai
11025 6c 61 62 6c 65 20 61 74 20 70 53 70 61 63 65 2e lable at pSpace.
11026 20 20 54 68 65 20 64 62 20 70 6f 69 6e 74 65 72 The db pointer
11027 20 69 73 20 75 73 65 64 20 61 73 20 61 20 6d 65 is used as a me
11028 6d 6f 72 79 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 mory context.**
11029 66 6f 72 20 61 6e 79 20 73 75 62 73 65 71 75 65 for any subseque
1102a 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 nt allocations t
1102b 68 61 74 20 6e 65 65 64 20 74 6f 20 6f 63 63 75 hat need to occu
1102c 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 r..** Return a p
1102d 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
1102e 77 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e w RowSet object.
1102f 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73 74 20 62 .**.** It must b
11030 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 e the case that
11031 4e 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 N is sufficient
11032 74 6f 20 6d 61 6b 65 20 61 20 52 6f 77 73 65 74 to make a Rowset
11033 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 61 6e 20 . If not.** an
11034 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 assertion fault
11035 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 occurs..** .** I
11036 66 20 4e 20 69 73 20 6c 61 72 67 65 72 20 74 68 f N is larger th
11037 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d 2c 20 an the minimum,
11038 75 73 65 20 74 68 65 20 73 75 72 70 6c 75 73 20 use the surplus
11039 61 73 20 61 6e 20 69 6e 69 74 69 61 6c 0a 2a 2a as an initial.**
1103a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 65 allocation of e
1103b 6e 74 72 69 65 73 20 61 76 61 69 6c 61 62 6c 65 ntries available
1103c 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 2e 0a 2a to be filled..*
1103d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1103e 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33 RowSet *sqlite3
1103f 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74 RowSetInit(sqlit
11040 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 53 e3 *db, void *pS
11041 70 61 63 65 2c 20 75 6e 73 69 67 6e 65 64 20 69 pace, unsigned i
11042 6e 74 20 4e 29 7b 0a 20 20 52 6f 77 53 65 74 20 nt N){. RowSet
11043 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4e 20 *p;. assert( N
11044 3e 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 >= ROUND8(sizeof
11045 28 2a 70 29 29 20 29 3b 0a 20 20 70 20 3d 20 70 (*p)) );. p = p
11046 53 70 61 63 65 3b 0a 20 20 70 2d 3e 70 43 68 75 Space;. p->pChu
11047 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 64 62 20 nk = 0;. p->db
11048 3d 20 64 62 3b 0a 20 20 70 2d 3e 70 45 6e 74 72 = db;. p->pEntr
11049 79 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61 73 y = 0;. p->pLas
1104a 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 54 72 65 t = 0;. p->pTre
1104b 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 46 72 65 e = 0;. p->pFre
1104c 73 68 20 3d 20 28 73 74 72 75 63 74 20 52 6f 77 sh = (struct Row
1104d 53 65 74 45 6e 74 72 79 2a 29 28 52 4f 55 4e 44 SetEntry*)(ROUND
1104e 38 28 73 69 7a 65 6f 66 28 2a 70 29 29 20 2b 20 8(sizeof(*p)) +
1104f 28 63 68 61 72 2a 29 70 29 3b 0a 20 20 70 2d 3e (char*)p);. p->
11050 6e 46 72 65 73 68 20 3d 20 28 75 31 36 29 28 28 nFresh = (u16)((
11051 4e 20 2d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f N - ROUND8(sizeo
11052 66 28 2a 70 29 29 29 2f 73 69 7a 65 6f 66 28 73 f(*p)))/sizeof(s
11053 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
11054 79 29 29 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74 y));. p->isSort
11055 65 64 20 3d 20 31 3b 0a 20 20 70 2d 3e 69 42 61 ed = 1;. p->iBa
11056 74 63 68 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 tch = 0;. retur
11057 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 n p;.}../*.** De
11058 61 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 63 68 75 allocate all chu
11059 6e 6b 73 20 66 72 6f 6d 20 61 20 52 6f 77 53 65 nks from a RowSe
1105a 74 2e 20 20 54 68 69 73 20 66 72 65 65 73 20 61 t. This frees a
1105b 6c 6c 20 6d 65 6d 6f 72 79 20 74 68 61 74 0a 2a ll memory that.*
1105c 2a 20 74 68 65 20 52 6f 77 53 65 74 20 68 61 73 * the RowSet has
1105d 20 61 6c 6c 6f 63 61 74 65 64 20 6f 76 65 72 20 allocated over
1105e 69 74 73 20 6c 69 66 65 74 69 6d 65 2e 20 20 54 its lifetime. T
1105f 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a his routine is.*
11060 2a 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 * the destructor
11061 20 66 6f 72 20 74 68 65 20 52 6f 77 53 65 74 2e for the RowSet.
11062 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
11063 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
11064 6f 77 53 65 74 43 6c 65 61 72 28 52 6f 77 53 65 owSetClear(RowSe
11065 74 20 2a 70 29 7b 0a 20 20 73 74 72 75 63 74 20 t *p){. struct
11066 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 43 68 RowSetChunk *pCh
11067 75 6e 6b 2c 20 2a 70 4e 65 78 74 43 68 75 6e 6b unk, *pNextChunk
11068 3b 0a 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d 70 ;. for(pChunk=p
11069 2d 3e 70 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b ->pChunk; pChunk
1106a 3b 20 70 43 68 75 6e 6b 20 3d 20 70 4e 65 78 74 ; pChunk = pNext
1106b 43 68 75 6e 6b 29 7b 0a 20 20 20 20 70 4e 65 78 Chunk){. pNex
1106c 74 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 2d tChunk = pChunk-
1106d 3e 70 4e 65 78 74 43 68 75 6e 6b 3b 0a 20 20 20 >pNextChunk;.
1106e 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 sqlite3DbFree(p
1106f 2d 3e 64 62 2c 20 70 43 68 75 6e 6b 29 3b 0a 20 ->db, pChunk);.
11070 20 7d 0a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d }. p->pChunk =
11071 20 30 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 20 0;. p->nFresh
11072 3d 20 30 3b 0a 20 20 70 2d 3e 70 45 6e 74 72 79 = 0;. p->pEntry
11073 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 = 0;. p->pLast
11074 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 54 72 65 65 = 0;. p->pTree
11075 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 = 0;. p->isSor
11076 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a ted = 1;.}../*.*
11077 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 77 20 76 * Insert a new v
11078 61 6c 75 65 20 69 6e 74 6f 20 61 20 52 6f 77 53 alue into a RowS
11079 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 et..**.** The ma
1107a 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 llocFailed flag
1107b 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1107c 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 73 65 connection is se
1107d 74 20 69 66 20 61 0a 2a 2a 20 6d 65 6d 6f 72 79 t if a.** memory
1107e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c allocation fail
1107f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
11080 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
11081 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 52 6f 3RowSetInsert(Ro
11082 77 53 65 74 20 2a 70 2c 20 69 36 34 20 72 6f 77 wSet *p, i64 row
11083 69 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f id){. struct Ro
11084 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 wSetEntry *pEntr
11085 79 3b 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 65 y; /* The new e
11086 6e 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 ntry */. struct
11087 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c RowSetEntry *pL
11088 61 73 74 3b 20 20 20 2f 2a 20 54 68 65 20 6c 61 ast; /* The la
11089 73 74 20 70 72 69 6f 72 20 65 6e 74 72 79 20 2a st prior entry *
1108a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 /. assert( p!=0
1108b 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 46 72 );. if( p->nFr
1108c 65 73 68 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 esh==0 ){. st
1108d 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b ruct RowSetChunk
1108e 20 2a 70 4e 65 77 3b 0a 20 20 20 20 70 4e 65 77 *pNew;. pNew
1108f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
11090 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 73 69 7a ocRaw(p->db, siz
11091 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 20 eof(*pNew));.
11092 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a if( pNew==0 ){.
11093 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
11094 20 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4e }. pNew->pN
11095 65 78 74 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 43 extChunk = p->pC
11096 68 75 6e 6b 3b 0a 20 20 20 20 70 2d 3e 70 43 68 hunk;. p->pCh
11097 75 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 unk = pNew;.
11098 70 2d 3e 70 46 72 65 73 68 20 3d 20 70 4e 65 77 p->pFresh = pNew
11099 2d 3e 61 45 6e 74 72 79 3b 0a 20 20 20 20 70 2d ->aEntry;. p-
1109a 3e 6e 46 72 65 73 68 20 3d 20 52 4f 57 53 45 54 >nFresh = ROWSET
1109b 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b _ENTRY_PER_CHUNK
1109c 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d ;. }. pEntry =
1109d 20 70 2d 3e 70 46 72 65 73 68 2b 2b 3b 0a 20 20 p->pFresh++;.
1109e 70 2d 3e 6e 46 72 65 73 68 2d 2d 3b 0a 20 20 70 p->nFresh--;. p
1109f 45 6e 74 72 79 2d 3e 76 20 3d 20 72 6f 77 69 64 Entry->v = rowid
110a0 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 70 52 69 67 ;. pEntry->pRig
110a1 68 74 20 3d 20 30 3b 0a 20 20 70 4c 61 73 74 20 ht = 0;. pLast
110a2 3d 20 70 2d 3e 70 4c 61 73 74 3b 0a 20 20 69 66 = p->pLast;. if
110a3 28 20 70 4c 61 73 74 20 29 7b 0a 20 20 20 20 69 ( pLast ){. i
110a4 66 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 26 f( p->isSorted &
110a5 26 20 72 6f 77 69 64 3c 3d 70 4c 61 73 74 2d 3e & rowid<=pLast->
110a6 76 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 73 v ){. p->is
110a7 53 6f 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 Sorted = 0;.
110a8 7d 0a 20 20 20 20 70 4c 61 73 74 2d 3e 70 52 69 }. pLast->pRi
110a9 67 68 74 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20 ght = pEntry;.
110aa 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
110ab 74 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d 30 20 t( p->pEntry==0
110ac 29 3b 20 2f 2a 20 46 69 72 65 73 20 69 66 20 49 ); /* Fires if I
110ad 4e 53 45 52 54 20 61 66 74 65 72 20 53 4d 41 4c NSERT after SMAL
110ae 4c 45 53 54 20 2a 2f 0a 20 20 20 20 70 2d 3e 70 LEST */. p->p
110af 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79 3b 0a Entry = pEntry;.
110b0 20 20 7d 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d }. p->pLast =
110b1 20 70 45 6e 74 72 79 3b 0a 7d 0a 0a 2f 2a 0a 2a pEntry;.}../*.*
110b2 2a 20 4d 65 72 67 65 20 74 77 6f 20 6c 69 73 74 * Merge two list
110b3 73 20 6f 66 20 52 6f 77 53 65 74 45 6e 74 72 79 s of RowSetEntry
110b4 20 6f 62 6a 65 63 74 73 2e 20 20 52 65 6d 6f 76 objects. Remov
110b5 65 20 64 75 70 6c 69 63 61 74 65 73 2e 0a 2a 2a e duplicates..**
110b6 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 6c 69 .** The input li
110b7 73 74 73 20 61 72 65 20 63 6f 6e 6e 65 63 74 65 sts are connecte
110b8 64 20 76 69 61 20 70 52 69 67 68 74 20 70 6f 69 d via pRight poi
110b9 6e 74 65 72 73 20 61 6e 64 20 61 72 65 20 0a 2a nters and are .*
110ba 2a 20 61 73 73 75 6d 65 64 20 74 6f 20 65 61 63 * assumed to eac
110bb 68 20 61 6c 72 65 61 64 79 20 62 65 20 69 6e 20 h already be in
110bc 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2f sorted order..*/
110bd 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 52 .static struct R
110be 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53 owSetEntry *rowS
110bf 65 74 4d 65 72 67 65 28 0a 20 20 73 74 72 75 63 etMerge(. struc
110c0 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 t RowSetEntry *p
110c1 41 2c 20 20 20 20 2f 2a 20 46 69 72 73 74 20 73 A, /* First s
110c2 6f 72 74 65 64 20 6c 69 73 74 20 74 6f 20 62 65 orted list to be
110c3 20 6d 65 72 67 65 64 20 2a 2f 0a 20 20 73 74 72 merged */. str
110c4 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
110c5 2a 70 42 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e *pB /* Secon
110c6 64 20 73 6f 72 74 65 64 20 6c 69 73 74 20 74 6f d sorted list to
110c7 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a 29 7b be merged */.){
110c8 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
110c9 45 6e 74 72 79 20 68 65 61 64 3b 0a 20 20 73 74 Entry head;. st
110ca 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
110cb 20 2a 70 54 61 69 6c 3b 0a 0a 20 20 70 54 61 69 *pTail;.. pTai
110cc 6c 20 3d 20 26 68 65 61 64 3b 0a 20 20 77 68 69 l = &head;. whi
110cd 6c 65 28 20 70 41 20 26 26 20 70 42 20 29 7b 0a le( pA && pB ){.
110ce 20 20 20 20 61 73 73 65 72 74 28 20 70 41 2d 3e assert( pA->
110cf 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 41 2d pRight==0 || pA-
110d0 3e 76 3c 3d 70 41 2d 3e 70 52 69 67 68 74 2d 3e >v<=pA->pRight->
110d1 76 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 v );. assert(
110d2 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c pB->pRight==0 |
110d3 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 52 69 | pB->v<=pB->pRi
110d4 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 69 66 ght->v );. if
110d5 28 20 70 41 2d 3e 76 3c 70 42 2d 3e 76 20 29 7b ( pA->v<pB->v ){
110d6 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 . pTail->pR
110d7 69 67 68 74 20 3d 20 70 41 3b 0a 20 20 20 20 20 ight = pA;.
110d8 20 70 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 74 pA = pA->pRight
110d9 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 ;. pTail =
110da 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b 0a 20 pTail->pRight;.
110db 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 2d }else if( pB-
110dc 3e 76 3c 70 41 2d 3e 76 20 29 7b 0a 20 20 20 20 >v<pA->v ){.
110dd 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 pTail->pRight
110de 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 42 20 3d = pB;. pB =
110df 20 70 42 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 pB->pRight;.
110e0 20 20 20 70 54 61 69 6c 20 3d 20 70 54 61 69 6c pTail = pTail
110e1 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65 ->pRight;. }e
110e2 6c 73 65 7b 0a 20 20 20 20 20 20 70 41 20 3d 20 lse{. pA =
110e3 70 41 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 pA->pRight;.
110e4 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 41 20 29 }. }. if( pA )
110e5 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 41 {. assert( pA
110e6 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 ->pRight==0 || p
110e7 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69 67 68 74 A->v<=pA->pRight
110e8 2d 3e 76 20 29 3b 0a 20 20 20 20 70 54 61 69 6c ->v );. pTail
110e9 2d 3e 70 52 69 67 68 74 20 3d 20 70 41 3b 0a 20 ->pRight = pA;.
110ea 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
110eb 72 74 28 20 70 42 3d 3d 30 20 7c 7c 20 70 42 2d rt( pB==0 || pB-
110ec 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 42 >pRight==0 || pB
110ed 2d 3e 76 3c 3d 70 42 2d 3e 70 52 69 67 68 74 2d ->v<=pB->pRight-
110ee 3e 76 20 29 3b 0a 20 20 20 20 70 54 61 69 6c 2d >v );. pTail-
110ef 3e 70 52 69 67 68 74 20 3d 20 70 42 3b 0a 20 20 >pRight = pB;.
110f0 7d 0a 20 20 72 65 74 75 72 6e 20 68 65 61 64 2e }. return head.
110f1 70 52 69 67 68 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pRight;.}../*.**
110f2 20 53 6f 72 74 20 61 6c 6c 20 65 6c 65 6d 65 6e Sort all elemen
110f3 74 73 20 6f 6e 20 74 68 65 20 70 45 6e 74 72 79 ts on the pEntry
110f4 20 6c 69 73 74 20 6f 66 20 74 68 65 20 52 6f 77 list of the Row
110f5 53 65 74 20 69 6e 74 6f 20 61 73 63 65 6e 64 69 Set into ascendi
110f6 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 20 0a 73 74 ng order..*/ .st
110f7 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 74 atic void rowSet
110f8 53 6f 72 74 28 52 6f 77 53 65 74 20 2a 70 29 7b Sort(RowSet *p){
110f9 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
110fa 69 3b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 i;. struct RowS
110fb 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 79 3b etEntry *pEntry;
110fc 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
110fd 45 6e 74 72 79 20 2a 61 42 75 63 6b 65 74 5b 34 Entry *aBucket[4
110fe 30 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 0];.. assert( p
110ff 2d 3e 69 73 53 6f 72 74 65 64 3d 3d 30 20 29 3b ->isSorted==0 );
11100 0a 20 20 6d 65 6d 73 65 74 28 61 42 75 63 6b 65 . memset(aBucke
11101 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 42 75 t, 0, sizeof(aBu
11102 63 6b 65 74 29 29 3b 0a 20 20 77 68 69 6c 65 28 cket));. while(
11103 20 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20 p->pEntry ){.
11104 20 20 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 45 pEntry = p->pE
11105 6e 74 72 79 3b 0a 20 20 20 20 70 2d 3e 70 45 6e ntry;. p->pEn
11106 74 72 79 20 3d 20 70 45 6e 74 72 79 2d 3e 70 52 try = pEntry->pR
11107 69 67 68 74 3b 0a 20 20 20 20 70 45 6e 74 72 79 ight;. pEntry
11108 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 ->pRight = 0;.
11109 20 20 66 6f 72 28 69 3d 30 3b 20 61 42 75 63 6b for(i=0; aBuck
1110a 65 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 et[i]; i++){.
1110b 20 20 20 70 45 6e 74 72 79 20 3d 20 72 6f 77 53 pEntry = rowS
1110c 65 74 4d 65 72 67 65 28 61 42 75 63 6b 65 74 5b etMerge(aBucket[
1110d 69 5d 2c 20 70 45 6e 74 72 79 29 3b 0a 20 20 20 i], pEntry);.
1110e 20 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20 aBucket[i] =
1110f 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 42 75 0;. }. aBu
11110 63 6b 65 74 5b 69 5d 20 3d 20 70 45 6e 74 72 79 cket[i] = pEntry
11111 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d ;. }. pEntry =
11112 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 0;. for(i=0; i
11113 3c 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 29 <sizeof(aBucket)
11114 2f 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 5b /sizeof(aBucket[
11115 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 0]); i++){. p
11116 45 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 4d 65 Entry = rowSetMe
11117 72 67 65 28 70 45 6e 74 72 79 2c 20 61 42 75 63 rge(pEntry, aBuc
11118 6b 65 74 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 ket[i]);. }. p
11119 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 ->pEntry = pEntr
1111a 79 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 y;. p->pLast =
1111b 30 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74 65 64 0;. p->isSorted
1111c 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 = 1;.}.../*.**
1111d 54 68 65 20 69 6e 70 75 74 2c 20 70 49 6e 2c 20 The input, pIn,
1111e 69 73 20 61 20 62 69 6e 61 72 79 20 74 72 65 65 is a binary tree
1111f 20 28 6f 72 20 73 75 62 74 72 65 65 29 20 6f 66 (or subtree) of
11120 20 52 6f 77 53 65 74 45 6e 74 72 79 20 6f 62 6a RowSetEntry obj
11121 65 63 74 73 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74 ects..** Convert
11122 20 74 68 69 73 20 74 72 65 65 20 69 6e 74 6f 20 this tree into
11123 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 63 6f a linked list co
11124 6e 6e 65 63 74 65 64 20 62 79 20 74 68 65 20 70 nnected by the p
11125 52 69 67 68 74 20 70 6f 69 6e 74 65 72 73 0a 2a Right pointers.*
11126 2a 20 61 6e 64 20 72 65 74 75 72 6e 20 70 6f 69 * and return poi
11127 6e 74 65 72 73 20 74 6f 20 74 68 65 20 66 69 72 nters to the fir
11128 73 74 20 61 6e 64 20 6c 61 73 74 20 65 6c 65 6d st and last elem
11129 65 6e 74 73 20 6f 66 20 74 68 65 20 6e 65 77 20 ents of the new
1112a 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 list..*/.static
1112b 76 6f 69 64 20 72 6f 77 53 65 74 54 72 65 65 54 void rowSetTreeT
1112c 6f 4c 69 73 74 28 0a 20 20 73 74 72 75 63 74 20 oList(. struct
1112d 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 49 6e RowSetEntry *pIn
1112e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f , /* Roo
1112f 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 74 t of the input t
11130 72 65 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ree */. struct
11131 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70 RowSetEntry **pp
11132 46 69 72 73 74 2c 20 20 20 20 2f 2a 20 57 72 69 First, /* Wri
11133 74 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 6f te head of the o
11134 75 74 70 75 74 20 6c 69 73 74 20 68 65 72 65 20 utput list here
11135 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 */. struct RowS
11136 65 74 45 6e 74 72 79 20 2a 2a 70 70 4c 61 73 74 etEntry **ppLast
11137 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 /* Write t
11138 61 69 6c 20 6f 66 20 74 68 65 20 6f 75 74 70 75 ail of the outpu
11139 74 20 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29 t list here */.)
1113a 7b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 21 {. assert( pIn!
1113b 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 2d =0 );. if( pIn-
1113c 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 73 74 >pLeft ){. st
1113d 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
1113e 20 2a 70 3b 0a 20 20 20 20 72 6f 77 53 65 74 54 *p;. rowSetT
1113f 72 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d 3e 70 reeToList(pIn->p
11140 4c 65 66 74 2c 20 70 70 46 69 72 73 74 2c 20 26 Left, ppFirst, &
11141 70 29 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67 68 p);. p->pRigh
11142 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 65 6c 73 65 t = pIn;. }else
11143 7b 0a 20 20 20 20 2a 70 70 46 69 72 73 74 20 3d {. *ppFirst =
11144 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 pIn;. }. if(
11145 70 49 6e 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 pIn->pRight ){.
11146 20 20 20 72 6f 77 53 65 74 54 72 65 65 54 6f 4c rowSetTreeToL
11147 69 73 74 28 70 49 6e 2d 3e 70 52 69 67 68 74 2c ist(pIn->pRight,
11148 20 26 70 49 6e 2d 3e 70 52 69 67 68 74 2c 20 70 &pIn->pRight, p
11149 70 4c 61 73 74 29 3b 0a 20 20 7d 65 6c 73 65 7b pLast);. }else{
1114a 0a 20 20 20 20 2a 70 70 4c 61 73 74 20 3d 20 70 . *ppLast = p
1114b 49 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 In;. }. assert
1114c 28 20 28 2a 70 70 4c 61 73 74 29 2d 3e 70 52 69 ( (*ppLast)->pRi
1114d 67 68 74 3d 3d 30 20 29 3b 0a 7d 0a 0a 0a 2f 2a ght==0 );.}.../*
1114e 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 73 6f .** Convert a so
1114f 72 74 65 64 20 6c 69 73 74 20 6f 66 20 65 6c 65 rted list of ele
11150 6d 65 6e 74 73 20 28 63 6f 6e 6e 65 63 74 65 64 ments (connected
11151 20 62 79 20 70 52 69 67 68 74 29 20 69 6e 74 6f by pRight) into
11152 20 61 20 62 69 6e 61 72 79 0a 2a 2a 20 74 72 65 a binary.** tre
11153 65 20 77 69 74 68 20 64 65 70 74 68 20 6f 66 20 e with depth of
11154 69 44 65 70 74 68 2e 20 20 41 20 64 65 70 74 68 iDepth. A depth
11155 20 6f 66 20 31 20 6d 65 61 6e 73 20 74 68 65 20 of 1 means the
11156 74 72 65 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 tree contains a
11157 73 69 6e 67 6c 65 0a 2a 2a 20 6e 6f 64 65 20 74 single.** node t
11158 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 68 65 aken from the he
11159 61 64 20 6f 66 20 2a 70 70 4c 69 73 74 2e 20 20 ad of *ppList.
1115a 41 20 64 65 70 74 68 20 6f 66 20 32 20 6d 65 61 A depth of 2 mea
1115b 6e 73 20 61 20 74 72 65 65 20 77 69 74 68 0a 2a ns a tree with.*
1115c 2a 20 74 68 72 65 65 20 6e 6f 64 65 73 2e 20 20 * three nodes.
1115d 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a And so forth..**
1115e 0a 2a 2a 20 55 73 65 20 61 73 20 6d 61 6e 79 20 .** Use as many
1115f 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68 65 entries from the
11160 20 69 6e 70 75 74 20 6c 69 73 74 20 61 73 20 72 input list as r
11161 65 71 75 69 72 65 64 20 61 6e 64 20 75 70 64 61 equired and upda
11162 74 65 20 74 68 65 0a 2a 2a 20 2a 70 70 4c 69 73 te the.** *ppLis
11163 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 t to point to th
11164 65 20 75 6e 75 73 65 64 20 65 6c 65 6d 65 6e 74 e unused element
11165 73 20 6f 66 20 74 68 65 20 6c 69 73 74 2e 20 20 s of the list.
11166 49 66 20 74 68 65 20 69 6e 70 75 74 0a 2a 2a 20 If the input.**
11167 6c 69 73 74 20 63 6f 6e 74 61 69 6e 73 20 74 6f list contains to
11168 6f 20 66 65 77 20 65 6c 65 6d 65 6e 74 73 2c 20 o few elements,
11169 74 68 65 6e 20 63 6f 6e 73 74 72 75 63 74 20 61 then construct a
1116a 6e 20 69 6e 63 6f 6d 70 6c 65 74 65 20 74 72 65 n incomplete tre
1116b 65 0a 2a 2a 20 61 6e 64 20 6c 65 61 76 65 20 2a e.** and leave *
1116c 70 70 4c 69 73 74 20 73 65 74 20 74 6f 20 4e 55 ppList set to NU
1116d 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e LL..**.** Return
1116e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
1116f 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 63 6f e root of the co
11170 6e 73 74 72 75 63 74 65 64 20 62 69 6e 61 72 79 nstructed binary
11171 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 tree..*/.static
11172 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
11173 74 72 79 20 2a 72 6f 77 53 65 74 4e 44 65 65 70 try *rowSetNDeep
11174 54 72 65 65 28 0a 20 20 73 74 72 75 63 74 20 52 Tree(. struct R
11175 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70 4c owSetEntry **ppL
11176 69 73 74 2c 0a 20 20 69 6e 74 20 69 44 65 70 74 ist,. int iDept
11177 68 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f h.){. struct Ro
11178 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 20 20 20 wSetEntry *p;
11179 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 6f 66 /* Root of
1117a 20 74 68 65 20 6e 65 77 20 74 72 65 65 20 2a 2f the new tree */
1117b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
1117c 45 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 Entry *pLeft;
1117d 20 20 2f 2a 20 4c 65 66 74 20 73 75 62 74 72 65 /* Left subtre
1117e 65 20 2a 2f 0a 20 20 69 66 28 20 2a 70 70 4c 69 e */. if( *ppLi
1117f 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 st==0 ){. ret
11180 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 urn 0;. }. if(
11181 20 69 44 65 70 74 68 3d 3d 31 20 29 7b 0a 20 20 iDepth==1 ){.
11182 20 20 70 20 3d 20 2a 70 70 4c 69 73 74 3b 0a 20 p = *ppList;.
11183 20 20 20 2a 70 70 4c 69 73 74 20 3d 20 70 2d 3e *ppList = p->
11184 70 52 69 67 68 74 3b 0a 20 20 20 20 70 2d 3e 70 pRight;. p->p
11185 4c 65 66 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 Left = p->pRight
11186 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e = 0;. return
11187 20 70 3b 0a 20 20 7d 0a 20 20 70 4c 65 66 74 20 p;. }. pLeft
11188 3d 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 = rowSetNDeepTre
11189 65 28 70 70 4c 69 73 74 2c 20 69 44 65 70 74 68 e(ppList, iDepth
1118a 2d 31 29 3b 0a 20 20 70 20 3d 20 2a 70 70 4c 69 -1);. p = *ppLi
1118b 73 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 st;. if( p==0 )
1118c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65 {. return pLe
1118d 66 74 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c 65 ft;. }. p->pLe
1118e 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 2a 70 ft = pLeft;. *p
1118f 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68 pList = p->pRigh
11190 74 3b 0a 20 20 70 2d 3e 70 52 69 67 68 74 20 3d t;. p->pRight =
11191 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65 rowSetNDeepTree
11192 28 70 70 4c 69 73 74 2c 20 69 44 65 70 74 68 2d (ppList, iDepth-
11193 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 1);. return p;.
11194 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 }../*.** Convert
11195 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20 6f a sorted list o
11196 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 74 6f 20 f elements into
11197 61 20 62 69 6e 61 72 79 20 74 72 65 65 2e 20 4d a binary tree. M
11198 61 6b 65 20 74 68 65 20 74 72 65 65 0a 2a 2a 20 ake the tree.**
11199 61 73 20 64 65 65 70 20 61 73 20 69 74 20 6e 65 as deep as it ne
1119a 65 64 73 20 74 6f 20 62 65 20 69 6e 20 6f 72 64 eds to be in ord
1119b 65 72 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 er to contain th
1119c 65 20 65 6e 74 69 72 65 20 6c 69 73 74 2e 0a 2a e entire list..*
1119d 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 /.static struct
1119e 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 RowSetEntry *row
1119f 53 65 74 4c 69 73 74 54 6f 54 72 65 65 28 73 74 SetListToTree(st
111a0 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
111a1 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 *pList){. int
111a2 69 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 iDepth;
111a3 20 20 2f 2a 20 44 65 70 74 68 20 6f 66 20 74 68 /* Depth of th
111a4 65 20 74 72 65 65 20 73 6f 20 66 61 72 20 2a 2f e tree so far */
111a5 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
111a6 45 6e 74 72 79 20 2a 70 3b 20 20 20 20 20 20 20 Entry *p;
111a7 2f 2a 20 43 75 72 72 65 6e 74 20 74 72 65 65 20 /* Current tree
111a8 72 6f 6f 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 root */. struct
111a9 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c RowSetEntry *pL
111aa 65 66 74 3b 20 20 20 2f 2a 20 4c 65 66 74 20 73 eft; /* Left s
111ab 75 62 74 72 65 65 20 2a 2f 0a 0a 20 20 61 73 73 ubtree */.. ass
111ac 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b ert( pList!=0 );
111ad 0a 20 20 70 20 3d 20 70 4c 69 73 74 3b 0a 20 20 . p = pList;.
111ae 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68 pList = p->pRigh
111af 74 3b 0a 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 t;. p->pLeft =
111b0 70 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 p->pRight = 0;.
111b1 20 66 6f 72 28 69 44 65 70 74 68 3d 31 3b 20 70 for(iDepth=1; p
111b2 4c 69 73 74 3b 20 69 44 65 70 74 68 2b 2b 29 7b List; iDepth++){
111b3 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 70 3b 0a . pLeft = p;.
111b4 20 20 20 20 70 20 3d 20 70 4c 69 73 74 3b 0a 20 p = pList;.
111b5 20 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 pList = p->pR
111b6 69 67 68 74 3b 0a 20 20 20 20 70 2d 3e 70 4c 65 ight;. p->pLe
111b7 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 20 20 ft = pLeft;.
111b8 70 2d 3e 70 52 69 67 68 74 20 3d 20 72 6f 77 53 p->pRight = rowS
111b9 65 74 4e 44 65 65 70 54 72 65 65 28 26 70 4c 69 etNDeepTree(&pLi
111ba 73 74 2c 20 69 44 65 70 74 68 29 3b 0a 20 20 7d st, iDepth);. }
111bb 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
111bc 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 /*.** Convert th
111bd 65 20 6c 69 73 74 20 69 6e 20 70 2d 3e 70 45 6e e list in p->pEn
111be 74 72 79 20 69 6e 74 6f 20 61 20 73 6f 72 74 65 try into a sorte
111bf 64 20 6c 69 73 74 20 69 66 20 69 74 20 69 73 20 d list if it is
111c0 6e 6f 74 0a 2a 2a 20 73 6f 72 74 65 64 20 61 6c not.** sorted al
111c1 72 65 61 64 79 2e 20 20 49 66 20 74 68 65 72 65 ready. If there
111c2 20 69 73 20 61 20 62 69 6e 61 72 79 20 74 72 65 is a binary tre
111c3 65 20 6f 6e 20 70 2d 3e 70 54 72 65 65 2c 20 74 e on p->pTree, t
111c4 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 69 hen.** convert i
111c5 74 20 69 6e 74 6f 20 61 20 6c 69 73 74 20 74 6f t into a list to
111c6 6f 20 61 6e 64 20 6d 65 72 67 65 20 69 74 20 69 o and merge it i
111c7 6e 74 6f 20 74 68 65 20 70 2d 3e 70 45 6e 74 72 nto the p->pEntr
111c8 79 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 y list..*/.stati
111c9 63 20 76 6f 69 64 20 72 6f 77 53 65 74 54 6f 4c c void rowSetToL
111ca 69 73 74 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a ist(RowSet *p){.
111cb 20 20 69 66 28 20 21 70 2d 3e 69 73 53 6f 72 74 if( !p->isSort
111cc 65 64 20 29 7b 0a 20 20 20 20 72 6f 77 53 65 74 ed ){. rowSet
111cd 53 6f 72 74 28 70 29 3b 0a 20 20 7d 0a 20 20 69 Sort(p);. }. i
111ce 66 28 20 70 2d 3e 70 54 72 65 65 20 29 7b 0a 20 f( p->pTree ){.
111cf 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 struct RowSet
111d0 45 6e 74 72 79 20 2a 70 48 65 61 64 2c 20 2a 70 Entry *pHead, *p
111d1 54 61 69 6c 3b 0a 20 20 20 20 72 6f 77 53 65 74 Tail;. rowSet
111d2 54 72 65 65 54 6f 4c 69 73 74 28 70 2d 3e 70 54 TreeToList(p->pT
111d3 72 65 65 2c 20 26 70 48 65 61 64 2c 20 26 70 54 ree, &pHead, &pT
111d4 61 69 6c 29 3b 0a 20 20 20 20 70 2d 3e 70 54 72 ail);. p->pTr
111d5 65 65 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 ee = 0;. p->p
111d6 45 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 4d 65 Entry = rowSetMe
111d7 72 67 65 28 70 2d 3e 70 45 6e 74 72 79 2c 20 70 rge(p->pEntry, p
111d8 48 65 61 64 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a Head);. }.}../*
111d9 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 .** Extract the
111da 73 6d 61 6c 6c 65 73 74 20 65 6c 65 6d 65 6e 74 smallest element
111db 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 53 65 74 from the RowSet
111dc 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 65 ..** Write the e
111dd 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 2a 70 52 6f lement into *pRo
111de 77 69 64 2e 20 20 52 65 74 75 72 6e 20 31 20 6f wid. Return 1 o
111df 6e 20 73 75 63 63 65 73 73 2e 20 20 52 65 74 75 n success. Retu
111e0 72 6e 0a 2a 2a 20 30 20 69 66 20 74 68 65 20 52 rn.** 0 if the R
111e1 6f 77 53 65 74 20 69 73 20 61 6c 72 65 61 64 79 owSet is already
111e2 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 66 empty..**.** Af
111e3 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ter this routine
111e4 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 has been called
111e5 2c 20 74 68 65 20 73 71 6c 69 74 65 33 52 6f 77 , the sqlite3Row
111e6 53 65 74 49 6e 73 65 72 74 28 29 0a 2a 2a 20 72 SetInsert().** r
111e7 6f 75 74 69 6e 65 20 6d 61 79 20 6e 6f 74 20 62 outine may not b
111e8 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 2e 20 e called again.
111e9 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/.SQLITE_PRIV
111ea 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
111eb 6f 77 53 65 74 4e 65 78 74 28 52 6f 77 53 65 74 owSetNext(RowSet
111ec 20 2a 70 2c 20 69 36 34 20 2a 70 52 6f 77 69 64 *p, i64 *pRowid
111ed 29 7b 0a 20 20 72 6f 77 53 65 74 54 6f 4c 69 73 ){. rowSetToLis
111ee 74 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 t(p);. if( p->p
111ef 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 2a 70 52 Entry ){. *pR
111f0 6f 77 69 64 20 3d 20 70 2d 3e 70 45 6e 74 72 79 owid = p->pEntry
111f1 2d 3e 76 3b 0a 20 20 20 20 70 2d 3e 70 45 6e 74 ->v;. p->pEnt
111f2 72 79 20 3d 20 70 2d 3e 70 45 6e 74 72 79 2d 3e ry = p->pEntry->
111f3 70 52 69 67 68 74 3b 0a 20 20 20 20 69 66 28 20 pRight;. if(
111f4 70 2d 3e 70 45 6e 74 72 79 3d 3d 30 20 29 7b 0a p->pEntry==0 ){.
111f5 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 sqlite3Row
111f6 53 65 74 43 6c 65 61 72 28 70 29 3b 0a 20 20 20 SetClear(p);.
111f7 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b }. return 1;
111f8 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
111f9 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f turn 0;. }.}../
111fa 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 *.** Check to se
111fb 65 20 69 66 20 65 6c 65 6d 65 6e 74 20 69 52 6f e if element iRo
111fc 77 69 64 20 77 61 73 20 69 6e 73 65 72 74 65 64 wid was inserted
111fd 20 69 6e 74 6f 20 74 68 65 20 74 68 65 20 72 6f into the the ro
111fe 77 73 65 74 20 61 73 0a 2a 2a 20 70 61 72 74 20 wset as.** part
111ff 6f 66 20 61 6e 79 20 69 6e 73 65 72 74 20 62 61 of any insert ba
11200 74 63 68 20 70 72 69 6f 72 20 74 6f 20 69 42 61 tch prior to iBa
11201 74 63 68 2e 20 20 52 65 74 75 72 6e 20 31 20 6f tch. Return 1 o
11202 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 r 0..*/.SQLITE_P
11203 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
11204 65 33 52 6f 77 53 65 74 54 65 73 74 28 52 6f 77 e3RowSetTest(Row
11205 53 65 74 20 2a 70 52 6f 77 53 65 74 2c 20 75 38 Set *pRowSet, u8
11206 20 69 42 61 74 63 68 2c 20 73 71 6c 69 74 65 33 iBatch, sqlite3
11207 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29 7b 0a _int64 iRowid){.
11208 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
11209 6e 74 72 79 20 2a 70 3b 0a 20 20 69 66 28 20 69 ntry *p;. if( i
1120a 42 61 74 63 68 21 3d 70 52 6f 77 53 65 74 2d 3e Batch!=pRowSet->
1120b 69 42 61 74 63 68 20 29 7b 0a 20 20 20 20 69 66 iBatch ){. if
1120c 28 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72 ( pRowSet->pEntr
1120d 79 20 29 7b 0a 20 20 20 20 20 20 72 6f 77 53 65 y ){. rowSe
1120e 74 54 6f 4c 69 73 74 28 70 52 6f 77 53 65 74 29 tToList(pRowSet)
1120f 3b 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d ;. pRowSet-
11210 3e 70 54 72 65 65 20 3d 20 72 6f 77 53 65 74 4c >pTree = rowSetL
11211 69 73 74 54 6f 54 72 65 65 28 70 52 6f 77 53 65 istToTree(pRowSe
11212 74 2d 3e 70 45 6e 74 72 79 29 3b 0a 20 20 20 20 t->pEntry);.
11213 20 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72 pRowSet->pEntr
11214 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 52 6f y = 0;. pRo
11215 77 53 65 74 2d 3e 70 4c 61 73 74 20 3d 20 30 3b wSet->pLast = 0;
11216 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 6f 77 53 . }. pRowS
11217 65 74 2d 3e 69 42 61 74 63 68 20 3d 20 69 42 61 et->iBatch = iBa
11218 74 63 68 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70 tch;. }. p = p
11219 52 6f 77 53 65 74 2d 3e 70 54 72 65 65 3b 0a 20 RowSet->pTree;.
1121a 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 while( p ){.
1121b 20 69 66 28 20 70 2d 3e 76 3c 69 52 6f 77 69 64 if( p->v<iRowid
1121c 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d ){. p = p-
1121d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65 6c >pRight;. }el
1121e 73 65 20 69 66 28 20 70 2d 3e 76 3e 69 52 6f 77 se if( p->v>iRow
1121f 69 64 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 id ){. p =
11220 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 7d 65 p->pLeft;. }e
11221 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 lse{. retur
11222 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 n 1;. }. }.
11223 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
11224 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
11225 64 20 6f 66 20 72 6f 77 73 65 74 2e 63 20 2a 2a d of rowset.c **
11226 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11227 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11228 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
11229 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
1122a 67 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 63 gin file pager.c
1122b 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1122c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1122d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1122e 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 .** 2001 Septemb
1122f 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 15.**.** The
11230 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
11231 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
11232 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
11233 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
11234 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
11235 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
11236 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
11237 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
11238 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
11239 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
1123a 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
1123b 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
1123c 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
1123d 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
1123e 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
1123f 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
11240 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
11241 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11242 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11243 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11244 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11245 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ****.** This is
11246 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
11247 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 on of the page c
11248 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 6f ache subsystem o
11249 72 20 22 70 61 67 65 72 22 2e 0a 2a 2a 20 0a 2a r "pager"..** .*
1124a 2a 20 54 68 65 20 70 61 67 65 72 20 69 73 20 75 * The pager is u
1124b 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 61 20 sed to access a
1124c 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 66 69 database disk fi
1124d 6c 65 2e 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e le. It implemen
1124e 74 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 63 6f 6d ts.** atomic com
1124f 6d 69 74 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b mit and rollback
11250 20 74 68 72 6f 75 67 68 20 74 68 65 20 75 73 65 through the use
11251 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 of a journal fi
11252 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 73 65 le that.** is se
11253 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68 65 20 parate from the
11254 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
11255 54 68 65 20 70 61 67 65 72 20 61 6c 73 6f 20 69 The pager also i
11256 6d 70 6c 65 6d 65 6e 74 73 20 66 69 6c 65 0a 2a mplements file.*
11257 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 70 72 65 * locking to pre
11258 76 65 6e 74 20 74 77 6f 20 70 72 6f 63 65 73 73 vent two process
11259 65 73 20 66 72 6f 6d 20 77 72 69 74 69 6e 67 20 es from writing
1125a 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
1125b 65 0a 2a 2a 20 66 69 6c 65 20 73 69 6d 75 6c 74 e.** file simult
1125c 61 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 6f 6e 65 aneously, or one
1125d 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 72 65 process from re
1125e 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 ading the databa
1125f 73 65 20 77 68 69 6c 65 0a 2a 2a 20 61 6e 6f 74 se while.** anot
11260 68 65 72 20 69 73 20 77 72 69 74 69 6e 67 2e 0a her is writing..
11261 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 **.** @(#) $Id:
11262 70 61 67 65 72 2e 63 2c 76 20 31 2e 36 32 39 20 pager.c,v 1.629
11263 32 30 30 39 2f 30 38 2f 31 30 20 31 37 3a 34 38 2009/08/10 17:48
11264 3a 35 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :57 drh Exp $.*/
11265 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
11266 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 0a 2f 2a 0a OMIT_DISKIO../*.
11267 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 74 72 ** Macros for tr
11268 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67 2e 20 20 oubleshooting.
11269 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 Normally turned
1126a 6f 66 66 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 off.*/.#if 0.int
1126b 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 61 sqlite3PagerTra
1126c 63 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20 74 ce=1; /* True t
1126d 6f 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 o enable tracing
1126e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 */.#define sqli
1126f 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 20 70 te3DebugPrintf p
11270 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 50 41 rintf.#define PA
11271 47 45 52 54 52 41 43 45 28 58 29 20 20 20 20 20 GERTRACE(X)
11272 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 if( sqlite3Pager
11273 54 72 61 63 65 20 29 7b 20 73 71 6c 69 74 65 33 Trace ){ sqlite3
11274 44 65 62 75 67 50 72 69 6e 74 66 20 58 3b 20 7d DebugPrintf X; }
11275 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 50 .#else.#define P
11276 41 47 45 52 54 52 41 43 45 28 58 29 0a 23 65 6e AGERTRACE(X).#en
11277 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 dif../*.** The f
11278 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 6d 61 63 ollowing two mac
11279 72 6f 73 20 61 72 65 20 75 73 65 64 20 77 69 74 ros are used wit
1127a 68 69 6e 20 74 68 65 20 50 41 47 45 52 54 52 41 hin the PAGERTRA
1127b 43 45 28 29 20 6d 61 63 72 6f 73 20 61 62 6f 76 CE() macros abov
1127c 65 0a 2a 2a 20 74 6f 20 70 72 69 6e 74 20 6f 75 e.** to print ou
1127d 74 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f t file-descripto
1127e 72 73 2e 20 0a 2a 2a 0a 2a 2a 20 50 41 47 45 52 rs. .**.** PAGER
1127f 49 44 28 29 20 74 61 6b 65 73 20 61 20 70 6f 69 ID() takes a poi
11280 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 72 20 nter to a Pager
11281 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61 72 struct as its ar
11282 67 75 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 61 gument. The.** a
11283 73 73 6f 63 69 61 74 65 64 20 66 69 6c 65 2d 64 ssociated file-d
11284 65 73 63 72 69 70 74 6f 72 20 69 73 20 72 65 74 escriptor is ret
11285 75 72 6e 65 64 2e 20 46 49 4c 45 48 41 4e 44 4c urned. FILEHANDL
11286 45 49 44 28 29 20 74 61 6b 65 73 20 61 6e 20 73 EID() takes an s
11287 71 6c 69 74 65 33 5f 66 69 6c 65 0a 2a 2a 20 73 qlite3_file.** s
11288 74 72 75 63 74 20 61 73 20 69 74 73 20 61 72 67 truct as its arg
11289 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e ument..*/.#defin
1128a 65 20 50 41 47 45 52 49 44 28 70 29 20 28 28 69 e PAGERID(p) ((i
1128b 6e 74 29 28 70 2d 3e 66 64 29 29 0a 23 64 65 66 nt)(p->fd)).#def
1128c 69 6e 65 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 ine FILEHANDLEID
1128d 28 66 64 29 20 28 28 69 6e 74 29 66 64 29 0a 0a (fd) ((int)fd)..
1128e 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 63 /*.** The page c
1128f 61 63 68 65 20 61 73 20 61 20 77 68 6f 6c 65 20 ache as a whole
11290 69 73 20 61 6c 77 61 79 73 20 69 6e 20 6f 6e 65 is always in one
11291 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
11292 67 0a 2a 2a 20 73 74 61 74 65 73 3a 0a 2a 2a 0a g.** states:.**.
11293 2a 2a 20 20 20 50 41 47 45 52 5f 55 4e 4c 4f 43 ** PAGER_UNLOC
11294 4b 20 20 20 20 20 20 20 20 54 68 65 20 70 61 67 K The pag
11295 65 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20 63 e cache is not c
11296 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69 6e 67 urrently reading
11297 20 6f 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 or .**
11298 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 72 wr
11299 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 iting the databa
1129a 73 65 20 66 69 6c 65 2e 20 20 54 68 65 72 65 20 se file. There
1129b 69 73 20 6e 6f 0a 2a 2a 20 20 20 20 20 20 20 20 is no.**
1129c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
1129d 61 74 61 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f ata held in memo
1129e 72 79 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 ry. This is the
1129f 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 20 20 20 20 initial.**
112a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
112a1 20 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 state..**.**
112a2 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 20 20 PAGER_SHARED
112a3 20 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61 The page ca
112a4 63 68 65 20 69 73 20 72 65 61 64 69 6e 67 20 74 che is reading t
112a5 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 he database..**
112a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
112a7 20 20 20 20 20 20 57 72 69 74 69 6e 67 20 69 73 Writing is
112a8 20 6e 6f 74 20 70 65 72 6d 69 74 74 65 64 2e 20 not permitted.
112a9 20 54 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a There can be.**
112aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
112ab 20 20 20 20 20 20 20 6d 75 6c 74 69 70 6c 65 20 multiple
112ac 72 65 61 64 65 72 73 20 61 63 63 65 73 73 69 6e readers accessin
112ad 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 g the same datab
112ae 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ase.**
112af 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69 6c fil
112b0 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 e at the same ti
112b1 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 me..**.** PAGE
112b2 52 5f 52 45 53 45 52 56 45 44 20 20 20 20 20 20 R_RESERVED
112b3 54 68 69 73 20 70 72 6f 63 65 73 73 20 68 61 73 This process has
112b4 20 72 65 73 65 72 76 65 64 20 74 68 65 20 64 61 reserved the da
112b5 74 61 62 61 73 65 20 66 6f 72 20 77 72 69 74 69 tabase for writi
112b6 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ng.**
112b7 20 20 20 20 20 20 20 20 20 20 20 20 62 75 74 20 but
112b8 68 61 73 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 has not yet made
112b9 20 61 6e 79 20 63 68 61 6e 67 65 73 2e 20 20 4f any changes. O
112ba 6e 6c 79 20 6f 6e 65 20 70 72 6f 63 65 73 73 0a nly one process.
112bb 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
112bc 20 20 20 20 20 20 20 20 20 61 74 20 61 20 74 69 at a ti
112bd 6d 65 20 63 61 6e 20 72 65 73 65 72 76 65 20 74 me can reserve t
112be 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 he database. Th
112bf 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 20 20 e original.**
112c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
112c1 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69 6c database fil
112c2 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 6d e has not been m
112c3 6f 64 69 66 69 65 64 20 73 6f 20 6f 74 68 65 72 odified so other
112c4 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
112c5 20 20 20 20 20 20 20 20 20 20 70 72 6f 63 65 73 proces
112c6 73 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 ses may still be
112c7 20 72 65 61 64 69 6e 67 20 74 68 65 20 6f 6e 2d reading the on-
112c8 64 69 73 6b 0a 2a 2a 20 20 20 20 20 20 20 20 20 disk.**
112c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 da
112ca 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
112cb 2a 2a 20 20 20 50 41 47 45 52 5f 45 58 43 4c 55 ** PAGER_EXCLU
112cc 53 49 56 45 20 20 20 20 20 54 68 65 20 70 61 67 SIVE The pag
112cd 65 20 63 61 63 68 65 20 69 73 20 77 72 69 74 69 e cache is writi
112ce 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ng the database.
112cf 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
112d0 20 20 20 20 20 20 20 20 20 20 41 63 63 65 73 73 Access
112d1 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 is exclusive.
112d2 4e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 No other process
112d3 65 73 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 es or.**
112d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 t
112d5 68 72 65 61 64 73 20 63 61 6e 20 62 65 20 72 65 hreads can be re
112d6 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 ading or writing
112d7 20 77 68 69 6c 65 20 6f 6e 65 0a 2a 2a 20 20 20 while one.**
112d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
112d9 20 20 20 20 70 72 6f 63 65 73 73 20 69 73 20 77 process is w
112da 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 riting..**.**
112db 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20 20 PAGER_SYNCED
112dc 20 20 20 20 54 68 65 20 70 61 67 65 72 20 6d 6f The pager mo
112dd 76 65 73 20 74 6f 20 74 68 69 73 20 73 74 61 74 ves to this stat
112de 65 20 66 72 6f 6d 20 50 41 47 45 52 5f 45 58 43 e from PAGER_EXC
112df 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 20 20 LUSIVE.**
112e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
112e1 61 66 74 65 72 20 61 6c 6c 20 64 69 72 74 79 20 after all dirty
112e2 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e 20 pages have been
112e3 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a written to the.*
112e4 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
112e5 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 database
112e6 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66 69 file and the fi
112e7 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79 6e 63 le has been sync
112e8 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 ed to.**
112e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
112ea 69 73 6b 2e 20 41 6c 6c 20 74 68 61 74 20 72 65 isk. All that re
112eb 6d 61 69 6e 73 20 74 6f 20 64 6f 20 69 73 20 74 mains to do is t
112ec 6f 20 72 65 6d 6f 76 65 20 6f 72 0a 2a 2a 20 20 o remove or.**
112ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
112ee 20 20 20 20 20 74 72 75 6e 63 61 74 65 20 74 68 truncate th
112ef 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 e journal file a
112f0 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 nd the transacti
112f1 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 on .**
112f2 20 20 20 20 20 20 20 20 20 20 20 20 20 77 69 6c wil
112f3 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 2e 0a l be committed..
112f4 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 63 **.** The page c
112f5 61 63 68 65 20 63 6f 6d 65 73 20 75 70 20 69 6e ache comes up in
112f6 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 PAGER_UNLOCK.
112f7 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 61 The first time a
112f8 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 .** sqlite3Pager
112f9 47 65 74 28 29 20 6f 63 63 75 72 73 2c 20 74 68 Get() occurs, th
112fa 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 e state transiti
112fb 6f 6e 73 20 74 6f 20 50 41 47 45 52 5f 53 48 41 ons to PAGER_SHA
112fc 52 45 44 2e 0a 2a 2a 20 41 66 74 65 72 20 61 6c RED..** After al
112fd 6c 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 l pages have bee
112fe 6e 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 n released using
112ff 20 73 71 6c 69 74 65 5f 70 61 67 65 5f 75 6e 72 sqlite_page_unr
11300 65 66 28 29 2c 0a 2a 2a 20 74 68 65 20 73 74 61 ef(),.** the sta
11301 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 62 te transitions b
11302 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 55 4e 4c ack to PAGER_UNL
11303 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73 74 20 OCK. The first
11304 74 69 6d 65 0a 2a 2a 20 74 68 61 74 20 73 71 6c time.** that sql
11305 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 ite3PagerWrite()
11306 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 is called, the
11307 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e state transition
11308 73 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 52 45 s to.** PAGER_RE
11309 53 45 52 56 45 44 2e 20 20 28 4e 6f 74 65 20 74 SERVED. (Note t
1130a 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 hat sqlite3Pager
1130b 57 72 69 74 65 28 29 20 63 61 6e 20 6f 6e 6c 79 Write() can only
1130c 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 6f 6e be.** called on
1130d 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 an outstanding
1130e 70 61 67 65 20 77 68 69 63 68 20 6d 65 61 6e 73 page which means
1130f 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20 that the pager
11310 6d 75 73 74 0a 2a 2a 20 62 65 20 69 6e 20 50 41 must.** be in PA
11311 47 45 52 5f 53 48 41 52 45 44 20 62 65 66 6f 72 GER_SHARED befor
11312 65 20 69 74 20 74 72 61 6e 73 69 74 69 6f 6e 73 e it transitions
11313 20 74 6f 20 50 41 47 45 52 5f 52 45 53 45 52 56 to PAGER_RESERV
11314 45 44 2e 29 0a 2a 2a 20 50 41 47 45 52 5f 52 45 ED.).** PAGER_RE
11315 53 45 52 56 45 44 20 6d 65 61 6e 73 20 74 68 61 SERVED means tha
11316 74 20 74 68 65 72 65 20 69 73 20 61 6e 20 6f 70 t there is an op
11317 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 en rollback jour
11318 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 74 72 61 6e nal..** The tran
11319 73 69 74 69 6f 6e 20 74 6f 20 50 41 47 45 52 5f sition to PAGER_
1131a 45 58 43 4c 55 53 49 56 45 20 6f 63 63 75 72 73 EXCLUSIVE occurs
1131b 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61 6e before any chan
1131c 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 ges.** are made
1131d 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
1131e 66 69 6c 65 2c 20 74 68 6f 75 67 68 20 77 72 69 file, though wri
1131f 74 65 73 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 tes to the rollb
11320 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6f ack.** journal o
11321 63 63 75 72 73 20 77 69 74 68 20 6a 75 73 74 20 ccurs with just
11322 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 20 PAGER_RESERVED.
11323 20 41 66 74 65 72 20 61 6e 20 73 71 6c 69 74 65 After an sqlite
11324 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 3PagerRollback()
11325 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 50 61 .** or sqlite3Pa
11326 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 gerCommitPhaseTw
11327 6f 28 29 2c 20 74 68 65 20 73 74 61 74 65 20 63 o(), the state c
11328 61 6e 20 67 6f 20 62 61 63 6b 20 74 6f 20 50 41 an go back to PA
11329 47 45 52 5f 53 48 41 52 45 44 2c 0a 2a 2a 20 6f GER_SHARED,.** o
1132a 72 20 69 74 20 63 61 6e 20 73 74 61 79 20 61 74 r it can stay at
1132b 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 PAGER_EXCLUSIVE
1132c 20 69 66 20 77 65 20 61 72 65 20 69 6e 20 65 78 if we are in ex
1132d 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 6d clusive access m
1132e 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ode..*/.#define
1132f 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 PAGER_UNLOCK
11330 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41 47 45 0.#define PAGE
11331 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 31 20 R_SHARED 1
11332 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 53 48 41 /* same as SHA
11333 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 RED_LOCK */.#def
11334 69 6e 65 20 50 41 47 45 52 5f 52 45 53 45 52 56 ine PAGER_RESERV
11335 45 44 20 20 20 20 32 20 20 20 2f 2a 20 73 61 6d ED 2 /* sam
11336 65 20 61 73 20 52 45 53 45 52 56 45 44 5f 4c 4f e as RESERVED_LO
11337 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 CK */.#define PA
11338 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20 20 GER_EXCLUSIVE
11339 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 45 4 /* same as E
1133a 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 2a 2f XCLUSIVE_LOCK */
1133b 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53 .#define PAGER_S
1133c 59 4e 43 45 44 20 20 20 20 20 20 35 0a 0a 2f 2a YNCED 5../*
1133d 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 75 73 65 64 .** A macro used
1133e 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68 for invoking th
1133f 65 20 63 6f 64 65 63 20 69 66 20 74 68 65 72 65 e codec if there
11340 20 69 73 20 6f 6e 65 0a 2a 2f 0a 23 69 66 64 65 is one.*/.#ifde
11341 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 f SQLITE_HAS_COD
11342 45 43 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 EC.# define CODE
11343 43 31 28 50 2c 44 2c 4e 2c 58 2c 45 29 20 5c 0a C1(P,D,N,X,E) \.
11344 20 20 20 20 69 66 28 20 50 2d 3e 78 43 6f 64 65 if( P->xCode
11345 63 20 26 26 20 50 2d 3e 78 43 6f 64 65 63 28 50 c && P->xCodec(P
11346 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e 2c 58 29 3d ->pCodec,D,N,X)=
11347 3d 30 20 29 7b 20 45 3b 20 7d 0a 23 20 64 65 66 =0 ){ E; }.# def
11348 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e ine CODEC2(P,D,N
11349 2c 58 2c 45 2c 4f 29 20 5c 0a 20 20 20 20 69 66 ,X,E,O) \. if
1134a 28 20 50 2d 3e 78 43 6f 64 65 63 3d 3d 30 20 29 ( P->xCodec==0 )
1134b 7b 20 4f 3d 28 63 68 61 72 2a 29 44 3b 20 7d 65 { O=(char*)D; }e
1134c 6c 73 65 20 5c 0a 20 20 20 20 69 66 28 20 28 4f lse \. if( (O
1134d 3d 28 63 68 61 72 2a 29 28 50 2d 3e 78 43 6f 64 =(char*)(P->xCod
1134e 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e ec(P->pCodec,D,N
1134f 2c 58 29 29 29 3d 3d 30 20 29 7b 20 45 3b 20 7d ,X)))==0 ){ E; }
11350 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
11351 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 2c 45 CODEC1(P,D,N,X,E
11352 29 20 20 20 2f 2a 20 4e 4f 2d 4f 50 20 2a 2f 0a ) /* NO-OP */.
11353 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32 28 # define CODEC2(
11354 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 20 4f 3d 28 P,D,N,X,E,O) O=(
11355 63 68 61 72 2a 29 44 0a 23 65 6e 64 69 66 0a 0a char*)D.#endif..
11356 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 /*.** The maximu
11357 6d 20 61 6c 6c 6f 77 65 64 20 73 65 63 74 6f 72 m allowed sector
11358 20 73 69 7a 65 2e 20 36 34 4b 69 42 2e 20 49 66 size. 64KiB. If
11359 20 74 68 65 20 78 53 65 63 74 6f 72 73 69 7a 65 the xSectorsize
1135a 28 29 20 6d 65 74 68 6f 64 20 0a 2a 2a 20 72 65 () method .** re
1135b 74 75 72 6e 73 20 61 20 76 61 6c 75 65 20 6c 61 turns a value la
1135c 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 2c 20 rger than this,
1135d 74 68 65 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f then MAX_SECTOR_
1135e 53 49 5a 45 20 69 73 20 75 73 65 64 20 69 6e 73 SIZE is used ins
1135f 74 65 61 64 2e 0a 2a 2a 20 54 68 69 73 20 63 6f tead..** This co
11360 75 6c 64 20 63 6f 6e 63 65 69 76 61 62 6c 79 20 uld conceivably
11361 63 61 75 73 65 20 63 6f 72 72 75 70 74 69 6f 6e cause corruption
11362 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 70 6f 77 following a pow
11363 65 72 20 66 61 69 6c 75 72 65 20 6f 6e 0a 2a 2a er failure on.**
11364 20 73 75 63 68 20 61 20 73 79 73 74 65 6d 2e 20 such a system.
11365 54 68 69 73 20 69 73 20 63 75 72 72 65 6e 74 6c This is currentl
11366 79 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 65 y an undocumente
11367 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64 65 66 d limit..*/.#def
11368 69 6e 65 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 ine MAX_SECTOR_S
11369 49 5a 45 20 30 78 31 30 30 30 30 0a 0a 2f 2a 0a IZE 0x10000../*.
1136a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
1136b 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
1136c 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c structure is all
1136d 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 ocated for each
1136e 61 63 74 69 76 65 0a 2a 2a 20 73 61 76 65 70 6f active.** savepo
1136f 69 6e 74 20 61 6e 64 20 73 74 61 74 65 6d 65 6e int and statemen
11370 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e t transaction in
11371 20 74 68 65 20 73 79 73 74 65 6d 2e 20 41 6c 6c the system. All
11372 20 73 75 63 68 20 73 74 72 75 63 74 75 72 65 73 such structures
11373 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 20 69 .** are stored i
11374 6e 20 74 68 65 20 50 61 67 65 72 2e 61 53 61 76 n the Pager.aSav
11375 65 70 6f 69 6e 74 5b 5d 20 61 72 72 61 79 2c 20 epoint[] array,
11376 77 68 69 63 68 20 69 73 20 61 6c 6c 6f 63 61 74 which is allocat
11377 65 64 20 61 6e 64 0a 2a 2a 20 72 65 73 69 7a 65 ed and.** resize
11378 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 52 d using sqlite3R
11379 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 ealloc()..**.**
1137a 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 When a savepoint
1137b 20 69 73 20 63 72 65 61 74 65 64 2c 20 74 68 65 is created, the
1137c 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e PagerSavepoint.
1137d 69 48 64 72 4f 66 66 73 65 74 20 66 69 65 6c 64 iHdrOffset field
1137e 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 2e is.** set to 0.
1137f 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 2d 68 65 If a journal-he
11380 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 ader is written
11381 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 6a 6f into the main jo
11382 75 72 6e 61 6c 20 77 68 69 6c 65 0a 2a 2a 20 74 urnal while.** t
11383 68 65 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 he savepoint is
11384 61 63 74 69 76 65 2c 20 74 68 65 6e 20 69 48 64 active, then iHd
11385 72 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 74 rOffset is set t
11386 6f 20 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 o the byte offse
11387 74 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c t .** immediatel
11388 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 y following the
11389 6c 61 73 74 20 6a 6f 75 72 6e 61 6c 20 72 65 63 last journal rec
1138a 6f 72 64 20 77 72 69 74 74 65 6e 20 69 6e 74 6f ord written into
1138b 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 the main.** jou
1138c 72 6e 61 6c 20 62 65 66 6f 72 65 20 74 68 65 20 rnal before the
1138d 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 2e 20 journal-header.
1138e 54 68 69 73 20 69 73 20 72 65 71 75 69 72 65 64 This is required
1138f 20 64 75 72 69 6e 67 20 73 61 76 65 70 6f 69 6e during savepoin
11390 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 28 73 t.** rollback (s
11391 65 65 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b ee pagerPlayback
11392 53 61 76 65 70 6f 69 6e 74 28 29 29 2e 0a 2a 2f Savepoint())..*/
11393 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
11394 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 50 PagerSavepoint P
11395 61 67 65 72 53 61 76 65 70 6f 69 6e 74 3b 0a 73 agerSavepoint;.s
11396 74 72 75 63 74 20 50 61 67 65 72 53 61 76 65 70 truct PagerSavep
11397 6f 69 6e 74 20 7b 0a 20 20 69 36 34 20 69 4f 66 oint {. i64 iOf
11398 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 fset;
11399 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 69 6e /* Startin
1139a 67 20 6f 66 66 73 65 74 20 69 6e 20 6d 61 69 6e g offset in main
1139b 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 journal */. i6
1139c 34 20 69 48 64 72 4f 66 66 73 65 74 3b 20 20 20 4 iHdrOffset;
1139d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 /* Se
1139e 65 20 61 62 6f 76 65 20 2a 2f 0a 20 20 42 69 74 e above */. Bit
1139f 76 65 63 20 2a 70 49 6e 53 61 76 65 70 6f 69 6e vec *pInSavepoin
113a0 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 t; /* Set
113a1 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 69 of pages in thi
113a2 73 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20 s savepoint */.
113a3 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20 20 Pgno nOrig;
113a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
113a5 20 4f 72 69 67 69 6e 61 6c 20 6e 75 6d 62 65 72 Original number
113a6 20 6f 66 20 70 61 67 65 73 20 69 6e 20 66 69 6c of pages in fil
113a7 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 53 75 62 e */. Pgno iSub
113a8 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 Rec;
113a9 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
113aa 66 69 72 73 74 20 72 65 63 6f 72 64 20 69 6e 20 first record in
113ab 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 7d sub-journal */.}
113ac 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f 70 65 6e 20 ;../*.** A open
113ad 70 61 67 65 20 63 61 63 68 65 20 69 73 20 61 6e page cache is an
113ae 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
113af 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
113b0 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 65 72 72 43 ture..**.** errC
113b1 6f 64 65 0a 2a 2a 0a 2a 2a 20 20 20 50 61 67 65 ode.**.** Page
113b2 72 2e 65 72 72 43 6f 64 65 20 6d 61 79 20 62 65 r.errCode may be
113b3 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 49 set to SQLITE_I
113b4 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52 OERR, SQLITE_COR
113b5 52 55 50 54 2c 20 6f 72 0a 2a 2a 20 20 20 6f 72 RUPT, or.** or
113b6 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 20 4f 6e SQLITE_FULL. On
113b7 63 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 69 ce one of the fi
113b8 72 73 74 20 74 68 72 65 65 20 65 72 72 6f 72 73 rst three errors
113b9 20 6f 63 63 75 72 73 2c 20 69 74 20 70 65 72 73 occurs, it pers
113ba 69 73 74 73 0a 2a 2a 20 20 20 61 6e 64 20 69 73 ists.** and is
113bb 20 72 65 74 75 72 6e 65 64 20 61 73 20 74 68 65 returned as the
113bc 20 72 65 73 75 6c 74 20 6f 66 20 65 76 65 72 79 result of every
113bd 20 6d 61 6a 6f 72 20 70 61 67 65 72 20 41 50 49 major pager API
113be 20 63 61 6c 6c 2e 20 20 54 68 65 0a 2a 2a 20 20 call. The.**
113bf 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 72 65 74 SQLITE_FULL ret
113c0 75 72 6e 20 63 6f 64 65 20 69 73 20 73 6c 69 67 urn code is slig
113c1 68 74 6c 79 20 64 69 66 66 65 72 65 6e 74 2e 20 htly different.
113c2 49 74 20 70 65 72 73 69 73 74 73 20 6f 6e 6c 79 It persists only
113c3 20 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 20 20 until the.**
113c4 6e 65 78 74 20 73 75 63 63 65 73 73 66 75 6c 20 next successful
113c5 72 6f 6c 6c 62 61 63 6b 20 69 73 20 70 65 72 66 rollback is perf
113c6 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 70 61 67 ormed on the pag
113c7 65 72 20 63 61 63 68 65 2e 20 41 6c 73 6f 2c 0a er cache. Also,.
113c8 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c ** SQLITE_FULL
113c9 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 63 74 does not affect
113ca 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 the sqlite3Page
113cb 72 47 65 74 28 29 20 61 6e 64 20 73 71 6c 69 74 rGet() and sqlit
113cc 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 0a e3PagerLookup().
113cd 2a 2a 20 20 20 41 50 49 73 2c 20 74 68 65 79 20 ** APIs, they
113ce 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 75 73 65 may still be use
113cf 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a d successfully..
113d0 2a 2a 0a 2a 2a 20 64 62 53 69 7a 65 56 61 6c 69 **.** dbSizeVali
113d1 64 2c 20 64 62 53 69 7a 65 2c 20 64 62 4f 72 69 d, dbSize, dbOri
113d2 67 53 69 7a 65 2c 20 64 62 46 69 6c 65 53 69 7a gSize, dbFileSiz
113d3 65 0a 2a 2a 0a 2a 2a 20 20 20 4d 61 6e 61 67 69 e.**.** Managi
113d4 6e 67 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 ng the size of t
113d5 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
113d6 20 69 6e 20 70 61 67 65 73 20 69 73 20 61 20 6c in pages is a l
113d7 69 74 74 6c 65 20 63 6f 6d 70 6c 69 63 61 74 65 ittle complicate
113d8 64 2e 0a 2a 2a 20 20 20 54 68 65 20 76 61 72 69 d..** The vari
113d9 61 62 6c 65 20 50 61 67 65 72 2e 64 62 53 69 7a able Pager.dbSiz
113da 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e e contains the n
113db 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 umber of pages t
113dc 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 hat the database
113dd 0a 2a 2a 20 20 20 69 6d 61 67 65 20 63 75 72 72 .** image curr
113de 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 2e 20 ently contains.
113df 41 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 As the database
113e0 69 6d 61 67 65 20 67 72 6f 77 73 20 6f 72 20 73 image grows or s
113e1 68 72 69 6e 6b 73 20 74 68 69 73 0a 2a 2a 20 20 hrinks this.**
113e2 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 70 64 variable is upd
113e3 61 74 65 64 2e 20 54 68 65 20 76 61 72 69 61 62 ated. The variab
113e4 6c 65 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53 le Pager.dbFileS
113e5 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ize contains the
113e6 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 6f 66 20 number.** of
113e7 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 pages in the dat
113e8 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 73 abase file. This
113e9 20 6d 61 79 20 62 65 20 64 69 66 66 65 72 65 6e may be differen
113ea 74 20 66 72 6f 6d 20 50 61 67 65 72 2e 64 62 53 t from Pager.dbS
113eb 69 7a 65 0a 2a 2a 20 20 20 69 66 20 73 6f 6d 65 ize.** if some
113ec 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e pages have been
113ed 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 appended to the
113ee 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 database image
113ef 62 75 74 20 6e 6f 74 20 79 65 74 20 77 72 69 74 but not yet writ
113f0 74 65 6e 0a 2a 2a 20 20 20 6f 75 74 20 66 72 6f ten.** out fro
113f1 6d 20 74 68 65 20 63 61 63 68 65 20 74 6f 20 74 m the cache to t
113f2 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20 6f he actual file o
113f3 6e 20 64 69 73 6b 2e 20 4f 72 20 69 66 20 74 68 n disk. Or if th
113f4 65 20 69 6d 61 67 65 20 68 61 73 20 62 65 65 6e e image has been
113f5 0a 2a 2a 20 20 20 74 72 75 6e 63 61 74 65 64 20 .** truncated
113f6 62 79 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 by an incrementa
113f7 6c 2d 76 61 63 75 75 6d 20 6f 70 65 72 61 74 69 l-vacuum operati
113f8 6f 6e 2e 20 54 68 65 20 50 61 67 65 72 2e 64 62 on. The Pager.db
113f9 4f 72 69 67 53 69 7a 65 20 76 61 72 69 61 62 6c OrigSize variabl
113fa 65 0a 2a 2a 20 20 20 63 6f 6e 74 61 69 6e 73 20 e.** contains
113fb 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
113fc 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 ges in the datab
113fd 61 73 65 20 69 6d 61 67 65 20 77 68 65 6e 20 74 ase image when t
113fe 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 20 20 he current.**
113ff 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 transaction was
11400 6f 70 65 6e 65 64 2e 20 54 68 65 20 63 6f 6e 74 opened. The cont
11401 65 6e 74 73 20 6f 66 20 61 6c 6c 20 74 68 72 65 ents of all thre
11402 65 20 6f 66 20 74 68 65 73 65 20 76 61 72 69 61 e of these varia
11403 62 6c 65 73 20 69 73 0a 2a 2a 20 20 20 6f 6e 6c bles is.** onl
11404 79 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 y guaranteed to
11405 62 65 20 63 6f 72 72 65 63 74 20 69 66 20 74 68 be correct if th
11406 65 20 62 6f 6f 6c 65 61 6e 20 50 61 67 65 72 2e e boolean Pager.
11407 64 62 53 69 7a 65 56 61 6c 69 64 20 69 73 20 74 dbSizeValid is t
11408 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 4f 44 rue..**.** TOD
11409 4f 3a 20 55 6e 64 65 72 20 77 68 61 74 20 63 6f O: Under what co
1140a 6e 64 69 74 69 6f 6e 73 20 69 73 20 64 62 53 69 nditions is dbSi
1140b 7a 65 56 61 6c 69 64 20 73 65 74 3f 20 43 6c 65 zeValid set? Cle
1140c 61 72 65 64 3f 0a 2a 2a 0a 2a 2a 20 63 68 61 6e ared?.**.** chan
1140d 67 65 43 6f 75 6e 74 44 6f 6e 65 0a 2a 2a 0a 2a geCountDone.**.*
1140e 2a 20 20 20 54 68 69 73 20 62 6f 6f 6c 65 61 6e * This boolean
1140f 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 variable is use
11410 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 d to make sure t
11411 68 61 74 20 74 68 65 20 63 68 61 6e 67 65 2d 63 hat the change-c
11412 6f 75 6e 74 65 72 20 0a 2a 2a 20 20 20 28 74 68 ounter .** (th
11413 65 20 34 2d 62 79 74 65 20 68 65 61 64 65 72 20 e 4-byte header
11414 66 69 65 6c 64 20 61 74 20 62 79 74 65 20 6f 66 field at byte of
11415 66 73 65 74 20 32 34 20 6f 66 20 74 68 65 20 64 fset 24 of the d
11416 61 74 61 62 61 73 65 20 66 69 6c 65 29 20 69 73 atabase file) is
11417 20 0a 2a 2a 20 20 20 6e 6f 74 20 75 70 64 61 74 .** not updat
11418 65 64 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 ed more often th
11419 61 6e 20 6e 65 63 65 73 73 61 72 79 2e 20 0a 2a an necessary. .*
1141a 2a 0a 2a 2a 20 20 20 49 74 20 69 73 20 73 65 74 *.** It is set
1141b 20 74 6f 20 74 72 75 65 20 77 68 65 6e 20 74 68 to true when th
1141c 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
1141d 20 66 69 65 6c 64 20 69 73 20 75 70 64 61 74 65 field is update
1141e 64 2c 20 77 68 69 63 68 20 0a 2a 2a 20 20 20 63 d, which .** c
1141f 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 an only happen i
11420 66 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c f an exclusive l
11421 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 ock is held on t
11422 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
11423 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20 63 6c 65 ..** It is cle
11424 61 72 65 64 20 28 73 65 74 20 74 6f 20 66 61 6c ared (set to fal
11425 73 65 29 20 77 68 65 6e 65 76 65 72 20 61 6e 20 se) whenever an
11426 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 exclusive lock i
11427 73 20 0a 2a 2a 20 20 20 72 65 6c 69 6e 71 75 69 s .** relinqui
11428 73 68 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 shed on the data
11429 62 61 73 65 20 66 69 6c 65 2e 20 45 61 63 68 20 base file. Each
1142a 74 69 6d 65 20 61 20 74 72 61 6e 73 61 63 74 69 time a transacti
1142b 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2c on is committed,
1142c 0a 2a 2a 20 20 20 54 68 65 20 63 68 61 6e 67 65 .** The change
1142d 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 69 CountDone flag i
1142e 73 20 69 6e 73 70 65 63 74 65 64 2e 20 49 66 20 s inspected. If
1142f 69 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 20 it is true, the
11430 77 6f 72 6b 20 6f 66 0a 2a 2a 20 20 20 75 70 64 work of.** upd
11431 61 74 69 6e 67 20 74 68 65 20 63 68 61 6e 67 65 ating the change
11432 2d 63 6f 75 6e 74 65 72 20 69 73 20 6f 6d 69 74 -counter is omit
11433 74 65 64 20 66 6f 72 20 74 68 65 20 63 75 72 72 ted for the curr
11434 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ent transaction.
11435 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 6d 65 .**.** This me
11436 63 68 61 6e 69 73 6d 20 6d 65 61 6e 73 20 74 68 chanism means th
11437 61 74 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 at when running
11438 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 in exclusive mod
11439 65 2c 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 e, a connection
1143a 0a 2a 2a 20 20 20 6e 65 65 64 20 6f 6e 6c 79 20 .** need only
1143b 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 update the chang
1143c 65 2d 63 6f 75 6e 74 65 72 20 6f 6e 63 65 2c 20 e-counter once,
1143d 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 72 for the first tr
1143e 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 63 ansaction.** c
1143f 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 ommitted..**.**
11440 64 62 4d 6f 64 69 66 69 65 64 0a 2a 2a 0a 2a 2a dbModified.**.**
11441 20 20 20 54 68 65 20 64 62 4d 6f 64 69 66 69 65 The dbModifie
11442 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 77 68 d flag is set wh
11443 65 6e 65 76 65 72 20 61 20 64 61 74 61 62 61 73 enever a databas
11444 65 20 70 61 67 65 20 69 73 20 64 69 72 74 69 65 e page is dirtie
11445 64 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20 63 6c d..** It is cl
11446 65 61 72 65 64 20 61 74 20 74 68 65 20 65 6e 64 eared at the end
11447 20 6f 66 20 65 61 63 68 20 74 72 61 6e 73 61 63 of each transac
11448 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 74 tion..**.** It
11449 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 63 6f is used when co
1144a 6d 6d 69 74 74 69 6e 67 20 6f 72 20 6f 74 68 65 mmitting or othe
1144b 72 77 69 73 65 20 65 6e 64 69 6e 67 20 61 20 74 rwise ending a t
1144c 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 0a 2a ransaction. If.*
1144d 2a 20 20 20 74 68 65 20 64 62 4d 6f 64 69 66 69 * the dbModifi
1144e 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 ed flag is clear
1144f 20 74 68 65 6e 20 6c 65 73 73 20 77 6f 72 6b 20 then less work
11450 68 61 73 20 74 6f 20 62 65 20 64 6f 6e 65 2e 0a has to be done..
11451 2a 2a 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 53 74 61 **.** journalSta
11452 72 74 65 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 rted.**.** Thi
11453 73 20 66 6c 61 67 20 69 73 20 73 65 74 20 77 68 s flag is set wh
11454 65 6e 65 76 65 72 20 74 68 65 20 74 68 65 20 6d enever the the m
11455 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 ain journal is s
11456 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 ynced. .**.**
11457 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 69 The point of thi
11458 73 20 66 6c 61 67 20 69 73 20 74 68 61 74 20 69 s flag is that i
11459 74 20 6d 75 73 74 20 62 65 20 73 65 74 20 61 66 t must be set af
1145a 74 65 72 20 74 68 65 20 0a 2a 2a 20 20 20 66 69 ter the .** fi
1145b 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 rst journal head
1145c 65 72 20 69 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 er in a journal
1145d 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79 file has been sy
1145e 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a nced to disk..**
1145f 20 20 20 41 66 74 65 72 20 74 68 69 73 20 68 61 After this ha
11460 73 20 68 61 70 70 65 6e 65 64 2c 20 6e 65 77 20 s happened, new
11461 70 61 67 65 73 20 61 70 70 65 6e 64 65 64 20 74 pages appended t
11462 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a o the database .
11463 2a 2a 20 20 20 64 6f 20 6e 6f 74 20 6e 65 65 64 ** do not need
11464 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f the PGHDR_NEED_
11465 53 59 4e 43 20 66 6c 61 67 20 73 65 74 2c 20 61 SYNC flag set, a
11466 73 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 6e 65 s they do not ne
11467 65 64 0a 2a 2a 20 20 20 74 6f 20 77 61 69 74 20 ed.** to wait
11468 66 6f 72 20 61 20 6a 6f 75 72 6e 61 6c 20 73 79 for a journal sy
11469 6e 63 20 62 65 66 6f 72 65 20 74 68 65 79 20 63 nc before they c
1146a 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75 an be written ou
1146b 74 20 74 6f 0a 2a 2a 20 20 20 74 68 65 20 64 61 t to.** the da
1146c 74 61 62 61 73 65 20 66 69 6c 65 20 28 73 65 65 tabase file (see
1146d 20 66 75 6e 63 74 69 6f 6e 20 70 61 67 65 72 5f function pager_
1146e 77 72 69 74 65 28 29 29 2e 0a 2a 2a 20 20 20 0a write())..** .
1146f 2a 2a 20 73 65 74 4d 61 73 74 65 72 0a 2a 2a 0a ** setMaster.**.
11470 2a 2a 20 20 20 54 68 69 73 20 76 61 72 69 61 62 ** This variab
11471 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e le is used to en
11472 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6d 61 sure that the ma
11473 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
11474 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 28 69 66 20 e name.** (if
11475 61 6e 79 29 20 69 73 20 6f 6e 6c 79 20 77 72 69 any) is only wri
11476 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f tten into the jo
11477 75 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 63 65 2e urnal file once.
11478 0a 2a 2a 0a 2a 2a 20 20 20 57 68 65 6e 20 63 6f .**.** When co
11479 6d 6d 69 74 74 69 6e 67 20 61 20 74 72 61 6e 73 mmitting a trans
1147a 61 63 74 69 6f 6e 2c 20 74 68 65 20 6d 61 73 74 action, the mast
1147b 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
1147c 6e 61 6d 65 20 28 69 66 20 61 6e 79 29 0a 2a 2a name (if any).**
1147d 20 20 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 may be writte
1147e 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e n into the journ
1147f 61 6c 20 66 69 6c 65 20 77 68 69 6c 65 20 74 68 al file while th
11480 65 20 70 61 67 65 72 20 69 73 20 73 74 69 6c 6c e pager is still
11481 20 69 6e 0a 2a 2a 20 20 20 50 41 47 45 52 5f 52 in.** PAGER_R
11482 45 53 45 52 56 45 44 20 73 74 61 74 65 20 28 73 ESERVED state (s
11483 65 65 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e ee CommitPhaseOn
11484 65 28 29 20 66 6f 72 20 74 68 65 20 61 63 74 69 e() for the acti
11485 6f 6e 29 2e 20 49 74 0a 2a 2a 20 20 20 74 68 65 on). It.** the
11486 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 75 70 n attempts to up
11487 67 72 61 64 65 20 74 6f 20 61 6e 20 65 78 63 6c grade to an excl
11488 75 73 69 76 65 20 6c 6f 63 6b 2e 20 49 66 20 74 usive lock. If t
11489 68 69 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 20 his attempt.**
1148a 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 53 51 4c fails, then SQL
1148b 49 54 45 5f 42 55 53 59 20 6d 61 79 20 62 65 20 ITE_BUSY may be
1148c 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 returned to the
1148d 75 73 65 72 20 61 6e 64 20 74 68 65 20 75 73 65 user and the use
1148e 72 0a 2a 2a 20 20 20 6d 61 79 20 61 74 74 65 6d r.** may attem
1148f 70 74 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 pt to commit the
11490 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 67 61 transaction aga
11491 69 6e 20 6c 61 74 65 72 20 28 63 61 6c 6c 69 6e in later (callin
11492 67 0a 2a 2a 20 20 20 43 6f 6d 6d 69 74 50 68 61 g.** CommitPha
11493 73 65 4f 6e 65 28 29 20 61 67 61 69 6e 29 2e 20 seOne() again).
11494 54 68 69 73 20 66 6c 61 67 20 69 73 20 75 73 65 This flag is use
11495 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 d to ensure that
11496 20 74 68 65 20 0a 2a 2a 20 20 20 6d 61 73 74 65 the .** maste
11497 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 r journal name i
11498 73 20 6f 6e 6c 79 20 77 72 69 74 74 65 6e 20 74 s only written t
11499 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 o the journal fi
1149a 6c 65 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 le the first.**
1149b 20 20 74 69 6d 65 20 43 6f 6d 6d 69 74 50 68 61 time CommitPha
1149c 73 65 4f 6e 65 28 29 20 69 73 20 63 61 6c 6c 65 seOne() is calle
1149d 64 2e 0a 2a 2a 0a 2a 2a 20 64 6f 4e 6f 74 53 79 d..**.** doNotSy
1149e 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 nc.**.** This
1149f 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 variable is set
114a0 61 6e 64 20 63 6c 65 61 72 65 64 20 62 79 20 73 and cleared by s
114a1 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
114a2 28 29 2e 0a 2a 2a 0a 2a 2a 20 6e 65 65 64 53 79 ()..**.** needSy
114a3 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 4f 44 4f 3a nc.**.** TODO:
114a4 20 49 74 20 6d 69 67 68 74 20 62 65 20 65 61 73 It might be eas
114a5 69 65 72 20 74 6f 20 73 65 74 20 74 68 69 73 20 ier to set this
114a6 76 61 72 69 61 62 6c 65 20 69 6e 20 77 72 69 74 variable in writ
114a7 65 4a 6f 75 72 6e 61 6c 48 64 72 28 29 0a 2a 2a eJournalHdr().**
114a8 20 20 20 61 6e 64 20 77 72 69 74 65 4d 61 73 74 and writeMast
114a9 65 72 4a 6f 75 72 6e 61 6c 28 29 20 6f 6e 6c 79 erJournal() only
114aa 2e 20 43 68 61 6e 67 65 20 69 74 73 20 6d 65 61 . Change its mea
114ab 6e 69 6e 67 20 74 6f 20 22 75 6e 73 79 6e 63 65 ning to "unsynce
114ac 64 20 64 61 74 61 0a 2a 2a 20 20 20 68 61 73 20 d data.** has
114ad 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 been written to
114ae 74 68 65 20 6a 6f 75 72 6e 61 6c 22 2e 0a 2a 2a the journal"..**
114af 0a 2a 2a 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 .** subjInMemory
114b0 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 69 73 .**.** This is
114b1 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 72 69 61 a boolean varia
114b2 62 6c 65 2e 20 49 66 20 74 72 75 65 2c 20 74 68 ble. If true, th
114b3 65 6e 20 61 6e 79 20 72 65 71 75 69 72 65 64 20 en any required
114b4 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 sub-journal.**
114b5 20 69 73 20 6f 70 65 6e 65 64 20 61 73 20 61 6e is opened as an
114b6 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e in-memory journ
114b7 61 6c 20 66 69 6c 65 2e 20 49 66 20 66 61 6c 73 al file. If fals
114b8 65 2c 20 74 68 65 6e 20 69 6e 2d 6d 65 6d 6f 72 e, then in-memor
114b9 79 0a 2a 2a 20 20 20 73 75 62 2d 6a 6f 75 72 6e y.** sub-journ
114ba 61 6c 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65 als are only use
114bb 64 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 d for in-memory
114bc 70 61 67 65 72 20 66 69 6c 65 73 2e 0a 2a 2f 0a pager files..*/.
114bd 73 74 72 75 63 74 20 50 61 67 65 72 20 7b 0a 20 struct Pager {.
114be 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
114bf 66 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 fs; /*
114c0 4f 53 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 OS functions to
114c1 75 73 65 20 66 6f 72 20 49 4f 20 2a 2f 0a 20 20 use for IO */.
114c2 75 38 20 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 u8 exclusiveMode
114c3 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 ; /* B
114c4 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20 69 66 20 oolean. True if
114c5 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 3d 45 58 locking_mode==EX
114c6 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20 75 38 20 CLUSIVE */. u8
114c7 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 20 20 20 journalMode;
114c8 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 20 6f /* On o
114c9 66 20 74 68 65 20 50 41 47 45 52 5f 4a 4f 55 52 f the PAGER_JOUR
114ca 4e 41 4c 4d 4f 44 45 5f 2a 20 76 61 6c 75 65 73 NALMODE_* values
114cb 20 2a 2f 0a 20 20 75 38 20 75 73 65 4a 6f 75 72 */. u8 useJour
114cc 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 nal;
114cd 20 20 2f 2a 20 55 73 65 20 61 20 72 6f 6c 6c 62 /* Use a rollb
114ce 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6f 6e 20 74 ack journal on t
114cf 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 his file */. u8
114d0 20 6e 6f 52 65 61 64 6c 6f 63 6b 3b 20 20 20 20 noReadlock;
114d1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 /* Do
114d2 6e 6f 74 20 62 6f 74 68 65 72 20 74 6f 20 6f 62 not bother to ob
114d3 74 61 69 6e 20 72 65 61 64 6c 6f 63 6b 73 20 2a tain readlocks *
114d4 2f 0a 20 20 75 38 20 6e 6f 53 79 6e 63 3b 20 20 /. u8 noSync;
114d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
114d6 2f 2a 20 44 6f 20 6e 6f 74 20 73 79 6e 63 20 74 /* Do not sync t
114d7 68 65 20 6a 6f 75 72 6e 61 6c 20 69 66 20 74 72 he journal if tr
114d8 75 65 20 2a 2f 0a 20 20 75 38 20 66 75 6c 6c 53 ue */. u8 fullS
114d9 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 ync;
114da 20 20 20 20 2f 2a 20 44 6f 20 65 78 74 72 61 20 /* Do extra
114db 73 79 6e 63 73 20 6f 66 20 74 68 65 20 6a 6f 75 syncs of the jou
114dc 72 6e 61 6c 20 66 6f 72 20 72 6f 62 75 73 74 6e rnal for robustn
114dd 65 73 73 20 2a 2f 0a 20 20 75 38 20 73 79 6e 63 ess */. u8 sync
114de 5f 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 _flags;
114df 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 /* One of S
114e0 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 53 59 YNC_NORMAL or SY
114e1 4e 43 5f 46 55 4c 4c 20 2a 2f 0a 20 20 75 38 20 NC_FULL */. u8
114e2 74 65 6d 70 46 69 6c 65 3b 20 20 20 20 20 20 20 tempFile;
114e3 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c /* zFil
114e4 65 6e 61 6d 65 20 69 73 20 61 20 74 65 6d 70 6f ename is a tempo
114e5 72 61 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 75 rary file */. u
114e6 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 8 readOnly;
114e7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
114e8 75 65 20 66 6f 72 20 61 20 72 65 61 64 2d 6f 6e ue for a read-on
114e9 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 ly database */.
114ea 20 75 38 20 6d 65 6d 44 62 3b 20 20 20 20 20 20 u8 memDb;
114eb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
114ec 54 72 75 65 20 74 6f 20 69 6e 68 69 62 69 74 20 True to inhibit
114ed 61 6c 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a all file I/O */.
114ee 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 . /* The follow
114ef 69 6e 67 20 62 6c 6f 63 6b 20 63 6f 6e 74 61 69 ing block contai
114f0 6e 73 20 74 68 6f 73 65 20 63 6c 61 73 73 20 6d ns those class m
114f1 65 6d 62 65 72 73 20 74 68 61 74 20 61 72 65 20 embers that are
114f2 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 20 2a 2a dynamically. **
114f3 20 6d 6f 64 69 66 69 65 64 20 64 75 72 69 6e 67 modified during
114f4 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 69 6f normal operatio
114f5 6e 73 2e 20 54 68 65 20 6f 74 68 65 72 20 76 61 ns. The other va
114f6 72 69 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20 riables in this
114f7 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a 20 61 structure. ** a
114f8 72 65 20 65 69 74 68 65 72 20 63 6f 6e 73 74 61 re either consta
114f9 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68 nt throughout th
114fa 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 e lifetime of th
114fb 65 20 70 61 67 65 72 2c 20 6f 72 20 65 6c 73 65 e pager, or else
114fc 0a 20 20 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 . ** used to st
114fd 6f 72 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f ore configuratio
114fe 6e 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 n parameters tha
114ff 74 20 61 66 66 65 63 74 20 74 68 65 20 77 61 79 t affect the way
11500 20 74 68 65 20 70 61 67 65 72 20 0a 20 20 2a 2a the pager . **
11501 20 6f 70 65 72 61 74 65 73 2e 0a 20 20 2a 2a 0a operates.. **.
11502 20 20 2a 2a 20 54 68 65 20 27 73 74 61 74 65 27 ** The 'state'
11503 20 76 61 72 69 61 62 6c 65 20 69 73 20 64 65 73 variable is des
11504 63 72 69 62 65 64 20 69 6e 20 6d 6f 72 65 20 64 cribed in more d
11505 65 74 61 69 6c 20 61 6c 6f 6e 67 20 77 69 74 68 etail along with
11506 20 74 68 65 0a 20 20 2a 2a 20 64 65 73 63 72 69 the. ** descri
11507 70 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 61 ptions of the va
11508 6c 75 65 73 20 69 74 20 6d 61 79 20 74 61 6b 65 lues it may take
11509 20 2d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 - PAGER_UNLOCK
1150a 65 74 63 2e 20 4d 61 6e 79 20 6f 66 20 74 68 65 etc. Many of the
1150b 0a 20 20 2a 2a 20 6f 74 68 65 72 20 76 61 72 69 . ** other vari
1150c 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20 62 6c ables in this bl
1150d 6f 63 6b 20 61 72 65 20 64 65 73 63 72 69 62 65 ock are describe
1150e 64 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74 d in the comment
1150f 20 64 69 72 65 63 74 6c 79 20 0a 20 20 2a 2a 20 directly . **
11510 61 62 6f 76 65 20 74 68 69 73 20 63 6c 61 73 73 above this class
11511 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 20 20 2a definition.. *
11512 2f 0a 20 20 75 38 20 73 74 61 74 65 3b 20 20 20 /. u8 state;
11513 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11514 2f 2a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2c /* PAGER_UNLOCK,
11515 20 5f 53 48 41 52 45 44 2c 20 5f 52 45 53 45 52 _SHARED, _RESER
11516 56 45 44 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 VED, etc. */. u
11517 38 20 64 62 4d 6f 64 69 66 69 65 64 3b 20 20 20 8 dbModified;
11518 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
11519 75 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20 ue if there are
1151a 61 6e 79 20 63 68 61 6e 67 65 73 20 74 6f 20 74 any changes to t
1151b 68 65 20 44 62 20 2a 2f 0a 20 20 75 38 20 6e 65 he Db */. u8 ne
1151c 65 64 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 edSync;
1151d 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
1151e 66 20 61 6e 20 66 73 79 6e 63 28 29 20 69 73 20 f an fsync() is
1151f 6e 65 65 64 65 64 20 6f 6e 20 74 68 65 20 6a 6f needed on the jo
11520 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 6a 6f urnal */. u8 jo
11521 75 72 6e 61 6c 53 74 61 72 74 65 64 3b 20 20 20 urnalStarted;
11522 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
11523 66 20 68 65 61 64 65 72 20 6f 66 20 6a 6f 75 72 f header of jour
11524 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 2a 2f nal is synced */
11525 0a 20 20 75 38 20 63 68 61 6e 67 65 43 6f 75 6e . u8 changeCoun
11526 74 44 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 2f tDone; /
11527 2a 20 53 65 74 20 61 66 74 65 72 20 69 6e 63 72 * Set after incr
11528 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 63 68 61 ementing the cha
11529 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 nge-counter */.
1152a 20 75 38 20 73 65 74 4d 61 73 74 65 72 3b 20 20 u8 setMaster;
1152b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1152c 54 72 75 65 20 69 66 20 61 20 6d 2d 6a 20 6e 61 True if a m-j na
1152d 6d 65 20 68 61 73 20 62 65 65 6e 20 77 72 69 74 me has been writ
1152e 74 65 6e 20 74 6f 20 6a 72 6e 6c 20 2a 2f 0a 20 ten to jrnl */.
1152f 20 75 38 20 64 6f 4e 6f 74 53 79 6e 63 3b 20 20 u8 doNotSync;
11530 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11531 42 6f 6f 6c 65 61 6e 2e 20 57 68 69 6c 65 20 74 Boolean. While t
11532 72 75 65 2c 20 64 6f 20 6e 6f 74 20 73 70 69 6c rue, do not spil
11533 6c 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 20 l the cache */.
11534 20 75 38 20 64 62 53 69 7a 65 56 61 6c 69 64 3b u8 dbSizeValid;
11535 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11536 53 65 74 20 77 68 65 6e 20 64 62 53 69 7a 65 20 Set when dbSize
11537 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a 20 20 is correct */.
11538 75 38 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 3b u8 subjInMemory;
11539 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1153a 72 75 65 20 74 6f 20 75 73 65 20 69 6e 2d 6d 65 rue to use in-me
1153b 6d 6f 72 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c mory sub-journal
1153c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 53 69 s */. Pgno dbSi
1153d 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ze;
1153e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1153f 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 pages in the dat
11540 61 62 61 73 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 abase */. Pgno
11541 64 62 4f 72 69 67 53 69 7a 65 3b 20 20 20 20 20 dbOrigSize;
11542 20 20 20 20 20 20 20 2f 2a 20 64 62 53 69 7a 65 /* dbSize
11543 20 62 65 66 6f 72 65 20 74 68 65 20 63 75 72 72 before the curr
11544 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
11545 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 46 69 6c 65 */. Pgno dbFile
11546 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
11547 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
11548 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 ges in the datab
11549 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e ase file */. in
1154a 74 20 65 72 72 43 6f 64 65 3b 20 20 20 20 20 20 t errCode;
1154b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
1154c 20 6f 66 20 73 65 76 65 72 61 6c 20 6b 69 6e 64 of several kind
1154d 73 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a 20 s of errors */.
1154e 20 69 6e 74 20 6e 52 65 63 3b 20 20 20 20 20 20 int nRec;
1154f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11550 50 61 67 65 73 20 6a 6f 75 72 6e 61 6c 6c 65 64 Pages journalled
11551 20 73 69 6e 63 65 20 6c 61 73 74 20 6a 2d 68 65 since last j-he
11552 61 64 65 72 20 77 72 69 74 74 65 6e 20 2a 2f 0a ader written */.
11553 20 20 75 33 32 20 63 6b 73 75 6d 49 6e 69 74 3b u32 cksumInit;
11554 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11555 20 51 75 61 73 69 2d 72 61 6e 64 6f 6d 20 76 61 Quasi-random va
11556 6c 75 65 20 61 64 64 65 64 20 74 6f 20 65 76 65 lue added to eve
11557 72 79 20 63 68 65 63 6b 73 75 6d 20 2a 2f 0a 20 ry checksum */.
11558 20 75 33 32 20 6e 53 75 62 52 65 63 3b 20 20 20 u32 nSubRec;
11559 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1155a 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64 Number of record
1155b 73 20 77 72 69 74 74 65 6e 20 74 6f 20 73 75 62 s written to sub
1155c 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 42 69 -journal */. Bi
1155d 74 76 65 63 20 2a 70 49 6e 4a 6f 75 72 6e 61 6c tvec *pInJournal
1155e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 ; /* One
1155f 20 62 69 74 20 66 6f 72 20 65 61 63 68 20 70 61 bit for each pa
11560 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ge in the databa
11561 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c se file */. sql
11562 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b 20 20 ite3_file *fd;
11563 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 /* File
11564 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 descriptor for
11565 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73 71 database */. sq
11566 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 64 3b lite3_file *jfd;
11567 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c /* Fil
11568 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 e descriptor for
11569 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f main journal */
1156a 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
1156b 2a 73 6a 66 64 3b 20 20 20 20 20 20 20 20 20 2f *sjfd; /
1156c 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f * File descripto
1156d 72 20 66 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61 r for sub-journa
1156e 6c 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e l */. i64 journ
1156f 61 6c 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 alOff;
11570 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 77 72 /* Current wr
11571 69 74 65 20 6f 66 66 73 65 74 20 69 6e 20 74 68 ite offset in th
11572 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a e journal file *
11573 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 48 /. i64 journalH
11574 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 dr;
11575 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 74 /* Byte offset t
11576 6f 20 70 72 65 76 69 6f 75 73 20 6a 6f 75 72 6e o previous journ
11577 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 50 al header */. P
11578 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 61 agerSavepoint *a
11579 53 61 76 65 70 6f 69 6e 74 3b 20 2f 2a 20 41 72 Savepoint; /* Ar
1157a 72 61 79 20 6f 66 20 61 63 74 69 76 65 20 73 61 ray of active sa
1157b 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e vepoints */. in
1157c 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 t nSavepoint;
1157d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1157e 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 ber of elements
1157f 69 6e 20 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 in aSavepoint[]
11580 2a 2f 0a 20 20 63 68 61 72 20 64 62 46 69 6c 65 */. char dbFile
11581 56 65 72 73 5b 31 36 5d 3b 20 20 20 20 20 20 20 Vers[16];
11582 20 2f 2a 20 43 68 61 6e 67 65 73 20 77 68 65 6e /* Changes when
11583 65 76 65 72 20 64 61 74 61 62 61 73 65 20 66 69 ever database fi
11584 6c 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20 le changes */.
11585 75 33 32 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 u32 sectorSize;
11586 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
11587 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 73 69 ssumed sector si
11588 7a 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 ze during rollba
11589 63 6b 20 2a 2f 0a 0a 20 20 75 31 36 20 6e 45 78 ck */.. u16 nEx
1158a 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 tra;
1158b 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 69 73 /* Add this
1158c 20 6d 61 6e 79 20 62 79 74 65 73 20 74 6f 20 65 many bytes to e
1158d 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 ach in-memory pa
1158e 67 65 20 2a 2f 0a 20 20 69 31 36 20 6e 52 65 73 ge */. i16 nRes
1158f 65 72 76 65 3b 20 20 20 20 20 20 20 20 20 20 20 erve;
11590 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
11591 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 61 74 unused bytes at
11592 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 end of each pag
11593 65 20 2a 2f 0a 20 20 75 33 32 20 76 66 73 46 6c e */. u32 vfsFl
11594 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
11595 20 20 20 2f 2a 20 46 6c 61 67 73 20 66 6f 72 20 /* Flags for
11596 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 sqlite3_vfs.xOpe
11597 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 70 61 67 n() */. int pag
11598 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 eSize;
11599 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1159a 66 20 62 79 74 65 73 20 69 6e 20 61 20 70 61 67 f bytes in a pag
1159b 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67 e */. Pgno mxPg
1159c 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 no;
1159d 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c /* Maximum al
1159e 6c 6f 77 65 64 20 73 69 7a 65 20 6f 66 20 74 68 lowed size of th
1159f 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 e database */.
115a0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b char *zFilename;
115a1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
115a2 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
115a3 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 ase file */. ch
115a4 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 ar *zJournal;
115a5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
115a6 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c e of the journal
115a7 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 28 file */. int (
115a8 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29 28 76 *xBusyHandler)(v
115a9 6f 69 64 2a 29 3b 20 2f 2a 20 46 75 6e 63 74 69 oid*); /* Functi
115aa 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20 on to call when
115ab 62 75 73 79 20 2a 2f 0a 20 20 76 6f 69 64 20 2a busy */. void *
115ac 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 3b pBusyHandlerArg;
115ad 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 /* Context
115ae 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 78 42 argument for xB
115af 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 23 69 usyHandler */.#i
115b0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
115b1 0a 20 20 69 6e 74 20 6e 48 69 74 2c 20 6e 4d 69 . int nHit, nMi
115b2 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ss; /
115b3 2a 20 43 61 63 68 65 20 68 69 74 73 20 61 6e 64 * Cache hits and
115b4 20 6d 69 73 73 69 6e 67 20 2a 2f 0a 20 20 69 6e missing */. in
115b5 74 20 6e 52 65 61 64 2c 20 6e 57 72 69 74 65 3b t nRead, nWrite;
115b6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
115b7 61 62 61 73 65 20 70 61 67 65 73 20 72 65 61 64 abase pages read
115b8 2f 77 72 69 74 74 65 6e 20 2a 2f 0a 23 65 6e 64 /written */.#end
115b9 69 66 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 69 if. void (*xRei
115ba 6e 69 74 65 72 29 28 44 62 50 61 67 65 2a 29 3b niter)(DbPage*);
115bb 20 2f 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f /* Call this ro
115bc 75 74 69 6e 65 20 77 68 65 6e 20 72 65 6c 6f 61 utine when reloa
115bd 64 69 6e 67 20 70 61 67 65 73 20 2a 2f 0a 23 69 ding pages */.#i
115be 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f fdef SQLITE_HAS_
115bf 43 4f 44 45 43 0a 20 20 76 6f 69 64 20 2a 28 2a CODEC. void *(*
115c0 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f xCodec)(void*,vo
115c1 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 20 2f id*,Pgno,int); /
115c2 2a 20 52 6f 75 74 69 6e 65 20 66 6f 72 20 65 6e * Routine for en
115c3 2f 64 65 63 6f 64 69 6e 67 20 64 61 74 61 20 2a /decoding data *
115c4 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 /. void (*xCode
115c5 63 53 69 7a 65 43 68 6e 67 29 28 76 6f 69 64 2a cSizeChng)(void*
115c6 2c 69 6e 74 2c 69 6e 74 29 3b 20 2f 2a 20 4e 6f ,int,int); /* No
115c7 74 69 66 79 20 6f 66 20 70 61 67 65 20 73 69 7a tify of page siz
115c8 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20 76 e changes */. v
115c9 6f 69 64 20 28 2a 78 43 6f 64 65 63 46 72 65 65 oid (*xCodecFree
115ca 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 )(void*);
115cb 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 /* Destruc
115cc 74 6f 72 20 66 6f 72 20 74 68 65 20 63 6f 64 65 tor for the code
115cd 63 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6f c */. void *pCo
115ce 64 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 dec;
115cf 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 /* First argu
115d0 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63 2e 2e ment to xCodec..
115d1 2e 20 6d 65 74 68 6f 64 73 20 2a 2f 0a 23 65 6e . methods */.#en
115d2 64 69 66 0a 20 20 63 68 61 72 20 2a 70 54 6d 70 dif. char *pTmp
115d3 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 Space;
115d4 20 20 2f 2a 20 50 61 67 65 72 2e 70 61 67 65 53 /* Pager.pageS
115d5 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 ize bytes of spa
115d6 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 20 2a ce for tmp use *
115d7 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 53 /. i64 journalS
115d8 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 izeLimit;
115d9 2f 2a 20 53 69 7a 65 20 6c 69 6d 69 74 20 66 6f /* Size limit fo
115da 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 r persistent jou
115db 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 rnal files */.
115dc 50 43 61 63 68 65 20 2a 70 50 43 61 63 68 65 3b PCache *pPCache;
115dd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
115de 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 63 ointer to page c
115df 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 ache object */.
115e0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 sqlite3_backup
115e1 2a 70 42 61 63 6b 75 70 3b 20 20 20 20 2f 2a 20 *pBackup; /*
115e2 50 6f 69 6e 74 65 72 20 74 6f 20 6c 69 73 74 20 Pointer to list
115e3 6f 66 20 6f 6e 67 6f 69 6e 67 20 62 61 63 6b 75 of ongoing backu
115e4 70 20 70 72 6f 63 65 73 73 65 73 20 2a 2f 0a 7d p processes */.}
115e5 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c ;../*.** The fol
115e6 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 lowing global va
115e7 72 69 61 62 6c 65 73 20 68 6f 6c 64 20 63 6f 75 riables hold cou
115e8 6e 74 65 72 73 20 75 73 65 64 20 66 6f 72 0a 2a nters used for.*
115e9 2a 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 * testing purpos
115ea 65 73 20 6f 6e 6c 79 2e 20 20 54 68 65 73 65 20 es only. These
115eb 76 61 72 69 61 62 6c 65 73 20 64 6f 20 6e 6f 74 variables do not
115ec 20 65 78 69 73 74 20 69 6e 0a 2a 2a 20 61 20 6e exist in.** a n
115ed 6f 6e 2d 74 65 73 74 69 6e 67 20 62 75 69 6c 64 on-testing build
115ee 2e 20 20 54 68 65 73 65 20 76 61 72 69 61 62 6c . These variabl
115ef 65 73 20 61 72 65 20 6e 6f 74 20 74 68 72 65 61 es are not threa
115f0 64 2d 73 61 66 65 2e 0a 2a 2f 0a 23 69 66 64 65 d-safe..*/.#ifde
115f1 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
115f2 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
115f3 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61 64 64 ite3_pager_readd
115f4 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 b_count = 0;
115f5 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6c /* Number of ful
115f6 6c 20 70 61 67 65 73 20 72 65 61 64 20 66 72 6f l pages read fro
115f7 6d 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 m DB */.SQLITE_A
115f8 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 PI int sqlite3_p
115f9 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 6f 75 ager_writedb_cou
115fa 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75 6d nt = 0; /* Num
115fb 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 67 65 ber of full page
115fc 73 20 77 72 69 74 74 65 6e 20 74 6f 20 44 42 20 s written to DB
115fd 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
115fe 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f t sqlite3_pager_
115ff 77 72 69 74 65 6a 5f 63 6f 75 6e 74 20 3d 20 30 writej_count = 0
11600 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f ; /* Number o
11601 66 20 70 61 67 65 73 20 77 72 69 74 74 65 6e 20 f pages written
11602 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 20 to journal */.#
11603 64 65 66 69 6e 65 20 50 41 47 45 52 5f 49 4e 43 define PAGER_INC
11604 52 28 76 29 20 20 76 2b 2b 0a 23 65 6c 73 65 0a R(v) v++.#else.
11605 23 20 64 65 66 69 6e 65 20 50 41 47 45 52 5f 49 # define PAGER_I
11606 4e 43 52 28 76 29 0a 23 65 6e 64 69 66 0a 0a 0a NCR(v).#endif...
11607 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72 6e 61 6c 20 66 ./*.** Journal f
11608 69 6c 65 73 20 62 65 67 69 6e 20 77 69 74 68 20 iles begin with
11609 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 the following ma
1160a 67 69 63 20 73 74 72 69 6e 67 2e 20 20 54 68 65 gic string. The
1160b 20 64 61 74 61 0a 2a 2a 20 77 61 73 20 6f 62 74 data.** was obt
1160c 61 69 6e 65 64 20 66 72 6f 6d 20 2f 64 65 76 2f ained from /dev/
1160d 72 61 6e 64 6f 6d 2e 20 20 49 74 20 69 73 20 75 random. It is u
1160e 73 65 64 20 6f 6e 6c 79 20 61 73 20 61 20 73 61 sed only as a sa
1160f 6e 69 74 79 20 63 68 65 63 6b 2e 0a 2a 2a 0a 2a nity check..**.*
11610 2a 20 53 69 6e 63 65 20 76 65 72 73 69 6f 6e 20 * Since version
11611 32 2e 38 2e 30 2c 20 74 68 65 20 6a 6f 75 72 6e 2.8.0, the journ
11612 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 74 61 69 al format contai
11613 6e 73 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 61 ns additional sa
11614 6e 69 74 79 0a 2a 2a 20 63 68 65 63 6b 69 6e 67 nity.** checking
11615 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49 information. I
11616 66 20 74 68 65 20 70 6f 77 65 72 20 66 61 69 6c f the power fail
11617 73 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 s while the jour
11618 6e 61 6c 20 69 73 20 62 65 69 6e 67 0a 2a 2a 20 nal is being.**
11619 77 72 69 74 74 65 6e 2c 20 73 65 6d 69 2d 72 61 written, semi-ra
1161a 6e 64 6f 6d 20 67 61 72 62 61 67 65 20 64 61 74 ndom garbage dat
1161b 61 20 6d 69 67 68 74 20 61 70 70 65 61 72 20 69 a might appear i
1161c 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a n the journal.**
1161d 20 66 69 6c 65 20 61 66 74 65 72 20 70 6f 77 65 file after powe
1161e 72 20 69 73 20 72 65 73 74 6f 72 65 64 2e 20 20 r is restored.
1161f 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 If an attempt is
11620 20 74 68 65 6e 20 6d 61 64 65 0a 2a 2a 20 74 6f then made.** to
11621 20 72 6f 6c 6c 20 74 68 65 20 6a 6f 75 72 6e 61 roll the journa
11622 6c 20 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61 l back, the data
11623 62 61 73 65 20 63 6f 75 6c 64 20 62 65 20 63 6f base could be co
11624 72 72 75 70 74 65 64 2e 20 20 54 68 65 20 61 64 rrupted. The ad
11625 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 73 61 6e 69 ditional.** sani
11626 74 79 20 63 68 65 63 6b 69 6e 67 20 64 61 74 61 ty checking data
11627 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20 74 is an attempt t
11628 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 67 o discover the g
11629 61 72 62 61 67 65 20 69 6e 20 74 68 65 0a 2a 2a arbage in the.**
1162a 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 69 67 6e journal and ign
1162b 6f 72 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 ore it..**.** Th
1162c 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e e sanity checkin
1162d 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f g information fo
1162e 72 20 74 68 65 20 6e 65 77 20 6a 6f 75 72 6e 61 r the new journa
1162f 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 73 69 73 74 l format consist
11630 73 0a 2a 2a 20 6f 66 20 61 20 33 32 2d 62 69 74 s.** of a 32-bit
11631 20 63 68 65 63 6b 73 75 6d 20 6f 6e 20 65 61 63 checksum on eac
11632 68 20 70 61 67 65 20 6f 66 20 64 61 74 61 2e 20 h page of data.
11633 20 54 68 65 20 63 68 65 63 6b 73 75 6d 20 63 6f The checksum co
11634 76 65 72 73 20 62 6f 74 68 0a 2a 2a 20 74 68 65 vers both.** the
11635 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 page number and
11636 20 74 68 65 20 70 50 61 67 65 72 2d 3e 70 61 67 the pPager->pag
11637 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64 eSize bytes of d
11638 61 74 61 20 66 6f 72 20 74 68 65 20 70 61 67 65 ata for the page
11639 2e 0a 2a 2a 20 54 68 69 73 20 63 6b 73 75 6d 20 ..** This cksum
1163a 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 is initialized t
1163b 6f 20 61 20 33 32 2d 62 69 74 20 72 61 6e 64 6f o a 32-bit rando
1163c 6d 20 76 61 6c 75 65 20 74 68 61 74 20 61 70 70 m value that app
1163d 65 61 72 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6a ears in the.** j
1163e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 72 69 67 68 ournal file righ
1163f 74 20 61 66 74 65 72 20 74 68 65 20 68 65 61 64 t after the head
11640 65 72 2e 20 20 54 68 65 20 72 61 6e 64 6f 6d 20 er. The random
11641 69 6e 69 74 69 61 6c 69 7a 65 72 20 69 73 20 69 initializer is i
11642 6d 70 6f 72 74 61 6e 74 2c 0a 2a 2a 20 62 65 63 mportant,.** bec
11643 61 75 73 65 20 67 61 72 62 61 67 65 20 64 61 74 ause garbage dat
11644 61 20 74 68 61 74 20 61 70 70 65 61 72 73 20 61 a that appears a
11645 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 6a t the end of a j
11646 6f 75 72 6e 61 6c 20 69 73 20 6c 69 6b 65 6c 79 ournal is likely
11647 0a 2a 2a 20 64 61 74 61 20 74 68 61 74 20 77 61 .** data that wa
11648 73 20 6f 6e 63 65 20 69 6e 20 6f 74 68 65 72 20 s once in other
11649 66 69 6c 65 73 20 74 68 61 74 20 68 61 76 65 20 files that have
1164a 6e 6f 77 20 62 65 65 6e 20 64 65 6c 65 74 65 64 now been deleted
1164b 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 67 61 72 . If the.** gar
1164c 62 61 67 65 20 64 61 74 61 20 63 61 6d 65 20 66 bage data came f
1164d 72 6f 6d 20 61 6e 20 6f 62 73 6f 6c 65 74 65 20 rom an obsolete
1164e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 journal file, th
1164f 65 20 63 68 65 63 6b 73 75 6d 73 20 6d 69 67 68 e checksums migh
11650 74 0a 2a 2a 20 62 65 20 63 6f 72 72 65 63 74 2e t.** be correct.
11651 20 20 42 75 74 20 62 79 20 69 6e 69 74 69 61 6c But by initial
11652 69 7a 69 6e 67 20 74 68 65 20 63 68 65 63 6b 73 izing the checks
11653 75 6d 20 74 6f 20 72 61 6e 64 6f 6d 20 76 61 6c um to random val
11654 75 65 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 64 ue which.** is d
11655 69 66 66 65 72 65 6e 74 20 66 6f 72 20 65 76 65 ifferent for eve
11656 72 79 20 6a 6f 75 72 6e 61 6c 2c 20 77 65 20 6d ry journal, we m
11657 69 6e 69 6d 69 7a 65 20 74 68 61 74 20 72 69 73 inimize that ris
11658 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e k..*/.static con
11659 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
1165a 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d aJournalMagic[]
1165b 20 3d 20 7b 0a 20 20 30 78 64 39 2c 20 30 78 64 = {. 0xd9, 0xd
1165c 35 2c 20 30 78 30 35 2c 20 30 78 66 39 2c 20 30 5, 0x05, 0xf9, 0
1165d 78 32 30 2c 20 30 78 61 31 2c 20 30 78 36 33 2c x20, 0xa1, 0x63,
1165e 20 30 78 64 37 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 0xd7,.};../*.**
1165f 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 The size of the
11660 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 72 65 of each page re
11661 63 6f 72 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 cord in the jour
11662 6e 61 6c 20 69 73 20 67 69 76 65 6e 20 62 79 0a nal is given by.
11663 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** the following
11664 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 64 65 66 69 macro..*/.#defi
11665 6e 65 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a ne JOURNAL_PG_SZ
11666 28 70 50 61 67 65 72 29 20 20 28 28 70 50 61 67 (pPager) ((pPag
11667 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 2b 20 er->pageSize) +
11668 38 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 8)../*.** The jo
11669 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 69 7a urnal header siz
1166a 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 e for this pager
1166b 2e 20 54 68 69 73 20 69 73 20 75 73 75 61 6c 6c . This is usuall
1166c 79 20 74 68 65 20 73 61 6d 65 20 0a 2a 2a 20 73 y the same .** s
1166d 69 7a 65 20 61 73 20 61 20 73 69 6e 67 6c 65 20 ize as a single
1166e 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 53 65 65 disk sector. See
1166f 20 61 6c 73 6f 20 73 65 74 53 65 63 74 6f 72 53 also setSectorS
11670 69 7a 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e ize()..*/.#defin
11671 65 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a e JOURNAL_HDR_SZ
11672 28 70 50 61 67 65 72 29 20 28 70 50 61 67 65 72 (pPager) (pPager
11673 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 0a 0a 2f ->sectorSize)../
11674 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20 4d *.** The macro M
11675 45 4d 44 42 20 69 73 20 74 72 75 65 20 69 66 20 EMDB is true if
11676 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 we are dealing w
11677 69 74 68 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 ith an in-memory
11678 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 57 65 database..** We
11679 20 64 6f 20 74 68 69 73 20 61 73 20 61 20 6d 61 do this as a ma
1167a 63 72 6f 20 73 6f 20 74 68 61 74 20 69 66 20 74 cro so that if t
1167b 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d he SQLITE_OMIT_M
1167c 45 4d 4f 52 59 44 42 20 6d 61 63 72 6f 20 69 73 EMORYDB macro is
1167d 20 73 65 74 2c 0a 2a 2a 20 74 68 65 20 76 61 6c set,.** the val
1167e 75 65 20 6f 66 20 4d 45 4d 44 42 20 77 69 6c 6c ue of MEMDB will
1167f 20 62 65 20 61 20 63 6f 6e 73 74 61 6e 74 20 61 be a constant a
11680 6e 64 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 nd the compiler
11681 77 69 6c 6c 20 6f 70 74 69 6d 69 7a 65 0a 2a 2a will optimize.**
11682 20 6f 75 74 20 63 6f 64 65 20 74 68 61 74 20 77 out code that w
11683 6f 75 6c 64 20 6e 65 76 65 72 20 65 78 65 63 75 ould never execu
11684 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 te..*/.#ifdef SQ
11685 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 LITE_OMIT_MEMORY
11686 44 42 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 DB.# define MEMD
11687 42 20 30 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 B 0.#else.# defi
11688 6e 65 20 4d 45 4d 44 42 20 70 50 61 67 65 72 2d ne MEMDB pPager-
11689 3e 6d 65 6d 44 62 0a 23 65 6e 64 69 66 0a 0a 2f >memDb.#endif../
1168a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d *.** The maximum
1168b 20 6c 65 67 61 6c 20 70 61 67 65 20 6e 75 6d 62 legal page numb
1168c 65 72 20 69 73 20 28 32 5e 33 31 20 2d 20 31 29 er is (2^31 - 1)
1168d 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 ..*/.#define PAG
1168e 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 32 31 34 37 ER_MAX_PGNO 2147
1168f 34 38 33 36 34 37 0a 0a 23 69 66 6e 64 65 66 20 483647..#ifndef
11690 4e 44 45 42 55 47 20 0a 2f 2a 0a 2a 2a 20 55 73 NDEBUG ./*.** Us
11691 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 73 73 age:.**.** ass
11692 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 67 65 ert( assert_page
11693 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29 20 r_state(pPager)
11694 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 );.*/.static int
11695 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 assert_pager_st
11696 61 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ate(Pager *pPage
11697 72 29 7b 0a 0a 20 20 2f 2a 20 41 20 74 65 6d 70 r){.. /* A temp
11698 2d 66 69 6c 65 20 69 73 20 61 6c 77 61 79 73 20 -file is always
11699 69 6e 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 in PAGER_EXCLUSI
1169a 56 45 20 6f 72 20 50 41 47 45 52 5f 53 59 4e 43 VE or PAGER_SYNC
1169b 45 44 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20 61 ED state. */. a
1169c 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 74 ssert( pPager->t
1169d 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c 20 70 50 empFile==0 || pP
1169e 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
1169f 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a ER_EXCLUSIVE );.
116a0 0a 20 20 2f 2a 20 54 68 65 20 63 68 61 6e 67 65 . /* The change
116a1 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 69 CountDone flag i
116a2 73 20 61 6c 77 61 79 73 20 73 65 74 20 66 6f 72 s always set for
116a3 20 74 65 6d 70 2d 66 69 6c 65 73 20 2a 2f 0a 20 temp-files */.
116a4 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
116a5 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c 20 >tempFile==0 ||
116a6 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f pPager->changeCo
116a7 75 6e 74 44 6f 6e 65 20 29 3b 0a 0a 20 20 72 65 untDone );.. re
116a8 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 turn 1;.}.#endif
116a9 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
116aa 72 75 65 20 69 66 20 69 74 20 69 73 20 6e 65 63 rue if it is nec
116ab 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 20 essary to write
116ac 70 61 67 65 20 2a 70 50 67 20 69 6e 74 6f 20 74 page *pPg into t
116ad 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0a he sub-journal..
116ae 2a 2a 20 41 20 70 61 67 65 20 6e 65 65 64 73 20 ** A page needs
116af 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e to be written in
116b0 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e to the sub-journ
116b1 61 6c 20 69 66 20 74 68 65 72 65 20 65 78 69 73 al if there exis
116b2 74 73 20 6f 6e 65 0a 2a 2a 20 6f 72 20 6d 6f 72 ts one.** or mor
116b3 65 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 e open savepoint
116b4 73 20 66 6f 72 20 77 68 69 63 68 3a 0a 2a 2a 0a s for which:.**.
116b5 2a 2a 20 20 20 2a 20 54 68 65 20 70 61 67 65 2d ** * The page-
116b6 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 20 74 number is less t
116b7 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
116b8 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 6e PagerSavepoint.n
116b9 4f 72 69 67 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a Orig, and.** *
116ba 20 54 68 65 20 62 69 74 20 63 6f 72 72 65 73 70 The bit corresp
116bb 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 70 61 onding to the pa
116bc 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 ge-number is not
116bd 20 73 65 74 20 69 6e 0a 2a 2a 20 20 20 20 20 50 set in.** P
116be 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 agerSavepoint.pI
116bf 6e 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 nSavepoint..*/.s
116c0 74 61 74 69 63 20 69 6e 74 20 73 75 62 6a 52 65 tatic int subjRe
116c1 71 75 69 72 65 73 50 61 67 65 28 50 67 48 64 72 quiresPage(PgHdr
116c2 20 2a 70 50 67 29 7b 0a 20 20 50 67 6e 6f 20 70 *pPg){. Pgno p
116c3 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b gno = pPg->pgno;
116c4 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
116c5 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
116c6 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 int i;. for(i
116c7 3d 30 3b 20 69 3c 70 50 61 67 65 72 2d 3e 6e 53 =0; i<pPager->nS
116c8 61 76 65 70 6f 69 6e 74 3b 20 69 2b 2b 29 7b 0a avepoint; i++){.
116c9 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 PagerSavepoi
116ca 6e 74 20 2a 70 20 3d 20 26 70 50 61 67 65 72 2d nt *p = &pPager-
116cb 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 5d 3b 0a >aSavepoint[i];.
116cc 20 20 20 20 69 66 28 20 70 2d 3e 6e 4f 72 69 67 if( p->nOrig
116cd 3e 3d 70 67 6e 6f 20 26 26 20 30 3d 3d 73 71 6c >=pgno && 0==sql
116ce 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
116cf 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20 ->pInSavepoint,
116d0 70 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 72 pgno) ){. r
116d1 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 eturn 1;. }.
116d2 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d }. return 0;.}
116d3 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
116d4 72 75 65 20 69 66 20 74 68 65 20 70 61 67 65 20 rue if the page
116d5 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 is already in th
116d6 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a e journal file..
116d7 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
116d8 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 50 67 48 64 geInJournal(PgHd
116d9 72 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72 r *pPg){. retur
116da 6e 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 n sqlite3BitvecT
116db 65 73 74 28 70 50 67 2d 3e 70 50 61 67 65 72 2d est(pPg->pPager-
116dc 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 >pInJournal, pPg
116dd 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ->pgno);.}../*.*
116de 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69 74 20 * Read a 32-bit
116df 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 74 68 65 integer from the
116e0 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73 63 given file desc
116e1 72 69 70 74 6f 72 2e 20 20 53 74 6f 72 65 20 74 riptor. Store t
116e2 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74 68 he integer.** th
116e3 61 74 20 69 73 20 72 65 61 64 20 69 6e 20 2a 70 at is read in *p
116e4 52 65 73 2e 20 20 52 65 74 75 72 6e 20 53 51 4c Res. Return SQL
116e5 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 ITE_OK if everyt
116e6 68 69 6e 67 20 77 6f 72 6b 65 64 2c 20 6f 72 20 hing worked, or
116e7 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 an.** error code
116e8 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f is something go
116e9 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 es wrong..**.**
116ea 41 6c 6c 20 76 61 6c 75 65 73 20 61 72 65 20 73 All values are s
116eb 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 20 61 73 tored on disk as
116ec 20 62 69 67 2d 65 6e 64 69 61 6e 2e 0a 2a 2f 0a big-endian..*/.
116ed 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 33 static int read3
116ee 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69 2bits(sqlite3_fi
116ef 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 le *fd, i64 offs
116f0 65 74 2c 20 75 33 32 20 2a 70 52 65 73 29 7b 0a et, u32 *pRes){.
116f1 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
116f2 61 63 5b 34 5d 3b 0a 20 20 69 6e 74 20 72 63 20 ac[4];. int rc
116f3 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 = sqlite3OsRead(
116f4 66 64 2c 20 61 63 2c 20 73 69 7a 65 6f 66 28 61 fd, ac, sizeof(a
116f5 63 29 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 69 c), offset);. i
116f6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
116f7 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 ){. *pRes =
116f8 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 sqlite3Get4byte(
116f9 61 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ac);. }. retur
116fa 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 n rc;.}../*.** W
116fb 72 69 74 65 20 61 20 33 32 2d 62 69 74 20 69 6e rite a 32-bit in
116fc 74 65 67 65 72 20 69 6e 74 6f 20 61 20 73 74 72 teger into a str
116fd 69 6e 67 20 62 75 66 66 65 72 20 69 6e 20 62 69 ing buffer in bi
116fe 67 2d 65 6e 64 69 61 6e 20 62 79 74 65 20 6f 72 g-endian byte or
116ff 64 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 der..*/.#define
11700 70 75 74 33 32 62 69 74 73 28 41 2c 42 29 20 20 put32bits(A,B)
11701 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 sqlite3Put4byte(
11702 28 75 38 2a 29 41 2c 42 29 0a 0a 2f 2a 0a 2a 2a (u8*)A,B)../*.**
11703 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 74 20 Write a 32-bit
11704 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 74 68 65 integer into the
11705 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73 63 given file desc
11706 72 69 70 74 6f 72 2e 20 20 52 65 74 75 72 6e 20 riptor. Return
11707 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 SQLITE_OK.** on
11708 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 success or an er
11709 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d 65 ror code is some
1170a 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 thing goes wrong
1170b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1170c 77 72 69 74 65 33 32 62 69 74 73 28 73 71 6c 69 write32bits(sqli
1170d 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69 36 te3_file *fd, i6
1170e 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 76 61 4 offset, u32 va
1170f 6c 29 7b 0a 20 20 63 68 61 72 20 61 63 5b 34 5d l){. char ac[4]
11710 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28 61 63 ;. put32bits(ac
11711 2c 20 76 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e , val);. return
11712 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
11713 66 64 2c 20 61 63 2c 20 34 2c 20 6f 66 66 73 65 fd, ac, 4, offse
11714 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 t);.}../*.** The
11715 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 argument to thi
11716 73 20 6d 61 63 72 6f 20 69 73 20 61 20 66 69 6c s macro is a fil
11717 65 20 64 65 73 63 72 69 70 74 6f 72 20 28 74 79 e descriptor (ty
11718 70 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a pe sqlite3_file*
11719 29 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 69 )..** Return 0 i
1171a 66 20 69 74 20 69 73 20 6e 6f 74 20 6f 70 65 6e f it is not open
1171b 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 20 28 62 , or non-zero (b
1171c 75 74 20 6e 6f 74 20 31 29 20 69 66 20 69 74 20 ut not 1) if it
1171d 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 is..**.** This i
1171e 73 20 73 6f 20 74 68 61 74 20 65 78 70 72 65 73 s so that expres
1171f 73 69 6f 6e 73 20 63 61 6e 20 62 65 20 77 72 69 sions can be wri
11720 74 74 65 6e 20 61 73 3a 0a 2a 2a 0a 2a 2a 20 20 tten as:.**.**
11721 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 if( isOpen(pPag
11722 65 72 2d 3e 6a 66 64 29 20 29 7b 20 2e 2e 2e 0a er->jfd) ){ ....
11723 2a 2a 0a 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66 **.** instead of
11724 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 70 50 61 .**.** if( pPa
11725 67 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68 6f ger->jfd->pMetho
11726 64 73 20 29 7b 20 2e 2e 2e 0a 2a 2f 0a 23 64 65 ds ){ ....*/.#de
11727 66 69 6e 65 20 69 73 4f 70 65 6e 28 70 46 64 29 fine isOpen(pFd)
11728 20 28 28 70 46 64 29 2d 3e 70 4d 65 74 68 6f 64 ((pFd)->pMethod
11729 73 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 66 69 6c s)../*.** If fil
1172a 65 20 70 46 64 20 69 73 20 6f 70 65 6e 2c 20 63 e pFd is open, c
1172b 61 6c 6c 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c all sqlite3OsUnl
1172c 6f 63 6b 28 29 20 6f 6e 20 69 74 2e 0a 2a 2f 0a ock() on it..*/.
1172d 73 74 61 74 69 63 20 69 6e 74 20 6f 73 55 6e 6c static int osUnl
1172e 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
1172f 20 2a 70 46 64 2c 20 69 6e 74 20 65 4c 6f 63 6b *pFd, int eLock
11730 29 7b 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e ){. if( !isOpen
11731 28 70 46 64 29 20 29 7b 0a 20 20 20 20 72 65 74 (pFd) ){. ret
11732 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
11733 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 }. return sqli
11734 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 46 64 2c te3OsUnlock(pFd,
11735 20 65 4c 6f 63 6b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a eLock);.}../*.*
11736 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
11737 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 determines wheth
11738 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74 er or not the at
11739 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d omic-write optim
1173a 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6e 20 62 ization.** can b
1173b 65 20 75 73 65 64 20 77 69 74 68 20 74 68 69 73 e used with this
1173c 20 70 61 67 65 72 2e 20 54 68 65 20 6f 70 74 69 pager. The opti
1173d 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 mization can be
1173e 75 73 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 used if:.**.**
1173f 28 61 29 20 74 68 65 20 76 61 6c 75 65 20 72 65 (a) the value re
11740 74 75 72 6e 65 64 20 62 79 20 4f 73 44 65 76 69 turned by OsDevi
11741 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
11742 73 28 29 20 69 6e 64 69 63 61 74 65 73 20 74 68 s() indicates th
11743 61 74 0a 2a 2a 20 20 20 20 20 20 61 20 64 61 74 at.** a dat
11744 61 62 61 73 65 20 70 61 67 65 20 6d 61 79 20 62 abase page may b
11745 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 63 e written atomic
11746 61 6c 6c 79 2c 20 61 6e 64 0a 2a 2a 20 20 28 62 ally, and.** (b
11747 29 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 ) the value retu
11748 72 6e 65 64 20 62 79 20 4f 73 53 65 63 74 6f 72 rned by OsSector
11749 53 69 7a 65 28 29 20 69 73 20 6c 65 73 73 20 74 Size() is less t
1174a 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20 han or equal.**
1174b 20 20 20 20 20 74 6f 20 74 68 65 20 70 61 67 65 to the page
1174c 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 size..**.** The
1174d 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 optimization is
1174e 20 61 6c 73 6f 20 61 6c 77 61 79 73 20 65 6e 61 also always ena
1174f 62 6c 65 64 20 66 6f 72 20 74 65 6d 70 6f 72 61 bled for tempora
11750 72 79 20 66 69 6c 65 73 2e 20 49 74 20 69 73 0a ry files. It is.
11751 2a 2a 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 63 ** an error to c
11752 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f all this functio
11753 6e 20 69 66 20 70 50 61 67 65 72 20 69 73 20 6f n if pPager is o
11754 70 65 6e 65 64 20 6f 6e 20 61 6e 20 69 6e 2d 6d pened on an in-m
11755 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 61 73 emory.** databas
11756 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
11757 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63 61 6e optimization can
11758 6e 6f 74 20 62 65 20 75 73 65 64 2c 20 30 20 69 not be used, 0 i
11759 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 69 s returned. If i
1175a 74 20 63 61 6e 20 62 65 20 75 73 65 64 2c 0a 2a t can be used,.*
1175b 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 * then the value
1175c 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 returned is the
1175d 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 size of the jou
1175e 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 69 rnal file when i
1175f 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 72 6f t.** contains ro
11760 6c 6c 62 61 63 6b 20 64 61 74 61 20 66 6f 72 20 llback data for
11761 65 78 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 65 exactly one page
11762 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
11763 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 TE_ENABLE_ATOMIC
11764 5f 57 52 49 54 45 0a 73 74 61 74 69 63 20 69 6e _WRITE.static in
11765 74 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65 t jrnlBufferSize
11766 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
11767 0a 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 . assert( !MEMD
11768 42 20 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67 B );. if( !pPag
11769 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a er->tempFile ){.
1176a 20 20 20 20 69 6e 74 20 64 63 3b 20 20 20 20 20 int dc;
1176b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1176c 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20 /* Device
1176d 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 characteristics
1176e 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 65 63 74 */. int nSect
1176f 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 or;
11770 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 74 /* Sect
11771 6f 72 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 69 or size */. i
11772 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 nt szPage;
11773 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11774 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 2a 2f /* Page size */
11775 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 .. assert( is
11776 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
11777 20 29 3b 0a 20 20 20 20 64 63 20 3d 20 73 71 6c );. dc = sql
11778 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 ite3OsDeviceChar
11779 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 acteristics(pPag
1177a 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 6e 53 65 er->fd);. nSe
1177b 63 74 6f 72 20 3d 20 70 50 61 67 65 72 2d 3e 73 ctor = pPager->s
1177c 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 20 20 73 ectorSize;. s
1177d 7a 50 61 67 65 20 3d 20 70 50 61 67 65 72 2d 3e zPage = pPager->
1177e 70 61 67 65 53 69 7a 65 3b 0a 0a 20 20 20 20 61 pageSize;.. a
1177f 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 ssert(SQLITE_IOC
11780 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28 35 AP_ATOMIC512==(5
11781 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 61 73 73 12>>8));. ass
11782 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 ert(SQLITE_IOCAP
11783 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 35 35 _ATOMIC64K==(655
11784 33 36 3e 3e 38 29 29 3b 0a 20 20 20 20 69 66 28 36>>8));. if(
11785 20 30 3d 3d 28 64 63 26 28 53 51 4c 49 54 45 5f 0==(dc&(SQLITE_
11786 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 73 7a IOCAP_ATOMIC|(sz
11787 50 61 67 65 3e 3e 38 29 29 20 7c 7c 20 6e 53 65 Page>>8)) || nSe
11788 63 74 6f 72 3e 73 7a 50 61 67 65 29 20 29 7b 0a ctor>szPage) ){.
11789 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
1178a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 }. }.. ret
1178b 75 72 6e 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f urn JOURNAL_HDR_
1178c 53 5a 28 70 50 61 67 65 72 29 20 2b 20 4a 4f 55 SZ(pPager) + JOU
1178d 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 RNAL_PG_SZ(pPage
1178e 72 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a r);.}.#endif../*
1178f 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 43 48 .** If SQLITE_CH
11790 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 65 66 ECK_PAGES is def
11791 69 6e 65 64 20 74 68 65 6e 20 77 65 20 64 6f 20 ined then we do
11792 73 6f 6d 65 20 73 61 6e 69 74 79 20 63 68 65 63 some sanity chec
11793 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 king.** on the c
11794 61 63 68 65 20 75 73 69 6e 67 20 61 20 68 61 73 ache using a has
11795 68 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 h function. Thi
11796 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 s is used for te
11797 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 62 sting.** and deb
11798 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a ugging only..*/.
11799 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 #ifdef SQLITE_CH
1179a 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a 20 ECK_PAGES./*.**
1179b 52 65 74 75 72 6e 20 61 20 33 32 2d 62 69 74 20 Return a 32-bit
1179c 68 61 73 68 20 6f 66 20 74 68 65 20 70 61 67 65 hash of the page
1179d 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 65 2e data for pPage.
1179e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70 .*/.static u32 p
1179f 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 69 6e ager_datahash(in
117a0 74 20 6e 42 79 74 65 2c 20 75 6e 73 69 67 6e 65 t nByte, unsigne
117a1 64 20 63 68 61 72 20 2a 70 44 61 74 61 29 7b 0a d char *pData){.
117a2 20 20 75 33 32 20 68 61 73 68 20 3d 20 30 3b 0a u32 hash = 0;.
117a3 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 int i;. for(i
117a4 3d 30 3b 20 69 3c 6e 42 79 74 65 3b 20 69 2b 2b =0; i<nByte; i++
117a5 29 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 28 68 ){. hash = (h
117a6 61 73 68 2a 31 30 33 39 29 20 2b 20 70 44 61 74 ash*1039) + pDat
117a7 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 a[i];. }. retu
117a8 72 6e 20 68 61 73 68 3b 0a 7d 0a 73 74 61 74 69 rn hash;.}.stati
117a9 63 20 75 33 32 20 70 61 67 65 72 5f 70 61 67 65 c u32 pager_page
117aa 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 61 67 hash(PgHdr *pPag
117ab 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 61 67 e){. return pag
117ac 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 61 67 er_datahash(pPag
117ad 65 2d 3e 70 50 61 67 65 72 2d 3e 70 61 67 65 53 e->pPager->pageS
117ae 69 7a 65 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 ize, (unsigned c
117af 68 61 72 20 2a 29 70 50 61 67 65 2d 3e 70 44 61 har *)pPage->pDa
117b0 74 61 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f ta);.}.static vo
117b1 69 64 20 70 61 67 65 72 5f 73 65 74 5f 70 61 67 id pager_set_pag
117b2 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 61 ehash(PgHdr *pPa
117b3 67 65 29 7b 0a 20 20 70 50 61 67 65 2d 3e 70 61 ge){. pPage->pa
117b4 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 geHash = pager_p
117b5 61 67 65 68 61 73 68 28 70 50 61 67 65 29 3b 0a agehash(pPage);.
117b6 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45 }../*.** The CHE
117b7 43 4b 5f 50 41 47 45 20 6d 61 63 72 6f 20 74 61 CK_PAGE macro ta
117b8 6b 65 73 20 61 20 50 67 48 64 72 2a 20 61 73 20 kes a PgHdr* as
117b9 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 an argument. If
117ba 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
117bb 45 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 ES.** is defined
117bc 2c 20 61 6e 64 20 4e 44 45 42 55 47 20 69 73 20 , and NDEBUG is
117bd 6e 6f 74 20 64 65 66 69 6e 65 64 2c 20 61 6e 20 not defined, an
117be 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
117bf 6e 74 20 63 68 65 63 6b 73 0a 2a 2a 20 74 68 61 nt checks.** tha
117c0 74 20 74 68 65 20 70 61 67 65 20 69 73 20 65 69 t the page is ei
117c1 74 68 65 72 20 64 69 72 74 79 20 6f 72 20 73 74 ther dirty or st
117c2 69 6c 6c 20 6d 61 74 63 68 65 73 20 74 68 65 20 ill matches the
117c3 63 61 6c 63 75 6c 61 74 65 64 20 70 61 67 65 2d calculated page-
117c4 68 61 73 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 hash..*/.#define
117c5 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 20 63 CHECK_PAGE(x) c
117c6 68 65 63 6b 50 61 67 65 28 78 29 0a 73 74 61 74 heckPage(x).stat
117c7 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 61 67 ic void checkPag
117c8 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 e(PgHdr *pPg){.
117c9 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
117ca 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 pPg->pPager;.
117cb 61 73 73 65 72 74 28 20 21 70 50 67 2d 3e 70 61 assert( !pPg->pa
117cc 67 65 48 61 73 68 20 7c 7c 20 70 50 61 67 65 72 geHash || pPager
117cd 2d 3e 65 72 72 43 6f 64 65 0a 20 20 20 20 20 20 ->errCode.
117ce 7c 7c 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 || (pPg->flags&P
117cf 47 48 44 52 5f 44 49 52 54 59 29 20 7c 7c 20 70 GHDR_DIRTY) || p
117d0 50 67 2d 3e 70 61 67 65 48 61 73 68 3d 3d 70 61 Pg->pageHash==pa
117d1 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 ger_pagehash(pPg
117d2 29 20 29 3b 0a 7d 0a 0a 23 65 6c 73 65 0a 23 64 ) );.}..#else.#d
117d3 65 66 69 6e 65 20 70 61 67 65 72 5f 64 61 74 61 efine pager_data
117d4 68 61 73 68 28 58 2c 59 29 20 20 30 0a 23 64 65 hash(X,Y) 0.#de
117d5 66 69 6e 65 20 70 61 67 65 72 5f 70 61 67 65 68 fine pager_pageh
117d6 61 73 68 28 58 29 20 20 30 0a 23 64 65 66 69 6e ash(X) 0.#defin
117d7 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 0a e CHECK_PAGE(x).
117d8 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 #endif /* SQLIT
117d9 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 2a 2f E_CHECK_PAGES */
117da 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 ../*.** When thi
117db 73 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 s is called the
117dc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 journal file for
117dd 20 70 61 67 65 72 20 70 50 61 67 65 72 20 6d 75 pager pPager mu
117de 73 74 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a 20 54 st be open..** T
117df 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 his function att
117e0 65 6d 70 74 73 20 74 6f 20 72 65 61 64 20 61 20 empts to read a
117e1 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
117e2 69 6c 65 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68 ile name from th
117e3 65 20 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 e .** end of the
117e4 20 66 69 6c 65 20 61 6e 64 2c 20 69 66 20 73 75 file and, if su
117e5 63 63 65 73 73 66 75 6c 2c 20 63 6f 70 69 65 73 ccessful, copies
117e6 20 69 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 it into memory
117e7 73 75 70 70 6c 69 65 64 20 0a 2a 2a 20 62 79 20 supplied .** by
117e8 74 68 65 20 63 61 6c 6c 65 72 2e 20 53 65 65 20 the caller. See
117e9 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 77 comments above w
117ea 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 riteMasterJourna
117eb 6c 28 29 20 66 6f 72 20 74 68 65 20 66 6f 72 6d l() for the form
117ec 61 74 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 at.** used to st
117ed 6f 72 65 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 ore a master jou
117ee 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 61 rnal file name a
117ef 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 6a t the end of a j
117f0 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a ournal file..**.
117f1 2a 2a 20 7a 4d 61 73 74 65 72 20 6d 75 73 74 20 ** zMaster must
117f2 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 point to a buffe
117f3 72 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 4d r of at least nM
117f4 61 73 74 65 72 20 62 79 74 65 73 20 61 6c 6c 6f aster bytes allo
117f5 63 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 cated by.** the
117f6 63 61 6c 6c 65 72 2e 20 54 68 69 73 20 73 68 6f caller. This sho
117f7 75 6c 64 20 62 65 20 73 71 6c 69 74 65 33 5f 76 uld be sqlite3_v
117f8 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 20 fs.mxPathname+1
117f9 28 74 6f 20 65 6e 73 75 72 65 20 74 68 65 72 65 (to ensure there
117fa 20 69 73 0a 2a 2a 20 65 6e 6f 75 67 68 20 73 70 is.** enough sp
117fb 61 63 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 ace to write the
117fc 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
117fd 6e 61 6d 65 29 2e 20 49 66 20 74 68 65 20 6d 61 name). If the ma
117fe 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 ster journal.**
117ff 6e 61 6d 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 name in the jour
11800 6e 61 6c 20 69 73 20 6c 6f 6e 67 65 72 20 74 68 nal is longer th
11801 61 6e 20 6e 4d 61 73 74 65 72 20 62 79 74 65 73 an nMaster bytes
11802 20 28 69 6e 63 6c 75 64 69 6e 67 20 61 0a 2a 2a (including a.**
11803 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 29 nul-terminator)
11804 2c 20 74 68 65 6e 20 74 68 69 73 20 69 73 20 68 , then this is h
11805 61 6e 64 6c 65 64 20 61 73 20 69 66 20 6e 6f 20 andled as if no
11806 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
11807 61 6d 65 0a 2a 2a 20 77 65 72 65 20 70 72 65 73 ame.** were pres
11808 65 6e 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e ent in the journ
11809 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d al..**.** If a m
1180a 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1180b 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 73 65 le name is prese
1180c 6e 74 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 nt at the end of
1180d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 the journal.**
1180e 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 file, then it is
1180f 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 copied into the
11810 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 buffer pointed
11811 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 2e 20 41 to by zMaster. A
11812 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 .** nul-terminat
11813 6f 72 20 62 79 74 65 20 69 73 20 61 70 70 65 6e or byte is appen
11814 64 65 64 20 74 6f 20 74 68 65 20 62 75 66 66 65 ded to the buffe
11815 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 r following the
11816 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 master.** journa
11817 6c 20 66 69 6c 65 20 6e 61 6d 65 2e 0a 2a 2a 0a l file name..**.
11818 2a 2a 20 49 66 20 69 74 20 69 73 20 64 65 74 65 ** If it is dete
11819 72 6d 69 6e 65 64 20 74 68 61 74 20 6e 6f 20 6d rmined that no m
1181a 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1181b 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 73 65 le name is prese
1181c 6e 74 20 0a 2a 2a 20 7a 4d 61 73 74 65 72 5b 30 nt .** zMaster[0
1181d 5d 20 69 73 20 73 65 74 20 74 6f 20 30 20 61 6e ] is set to 0 an
1181e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 d SQLITE_OK retu
1181f 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 rned..**.** If a
11820 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 n error occurs w
11821 68 69 6c 65 20 72 65 61 64 69 6e 67 20 66 72 6f hile reading fro
11822 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 m the journal fi
11823 6c 65 2c 20 61 6e 20 53 51 4c 69 74 65 0a 2a 2a le, an SQLite.**
11824 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
11825 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 eturned..*/.stat
11826 69 63 20 69 6e 74 20 72 65 61 64 4d 61 73 74 65 ic int readMaste
11827 72 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 33 rJournal(sqlite3
11828 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c 2c 20 63 68 _file *pJrnl, ch
11829 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 75 33 32 ar *zMaster, u32
1182a 20 6e 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 nMaster){. int
1182b 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
1182c 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
1182d 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 33 32 20 n code */. u32
1182e 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 len;
1182f 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 /* Length
11830 20 69 6e 20 62 79 74 65 73 20 6f 66 20 6d 61 73 in bytes of mas
11831 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ter journal name
11832 20 2a 2f 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 */. i64 szJ;
11833 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11834 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 69 /* Total size i
11835 6e 20 62 79 74 65 73 20 6f 66 20 6a 6f 75 72 6e n bytes of journ
11836 61 6c 20 66 69 6c 65 20 70 4a 72 6e 6c 20 2a 2f al file pJrnl */
11837 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20 20 20 . u32 cksum;
11838 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11839 20 4d 4a 20 63 68 65 63 6b 73 75 6d 20 76 61 6c MJ checksum val
1183a 75 65 20 72 65 61 64 20 66 72 6f 6d 20 6a 6f 75 ue read from jou
1183b 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 75 3b rnal */. u32 u;
1183c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1183d 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 /* Unsigned
1183e 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f loop counter */
1183f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
11840 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 2f 2a aMagic[8]; /*
11841 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c A buffer to hol
11842 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 61 64 d the magic head
11843 65 72 20 2a 2f 0a 20 20 7a 4d 61 73 74 65 72 5b er */. zMaster[
11844 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 69 66 0] = '\0';.. if
11845 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 ( SQLITE_OK!=(rc
11846 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 = sqlite3OsFile
11847 53 69 7a 65 28 70 4a 72 6e 6c 2c 20 26 73 7a 4a Size(pJrnl, &szJ
11848 29 29 0a 20 20 20 7c 7c 20 73 7a 4a 3c 31 36 0a )). || szJ<16.
11849 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 || SQLITE_OK!
1184a 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 =(rc = read32bit
1184b 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 36 2c s(pJrnl, szJ-16,
1184c 20 26 6c 65 6e 29 29 0a 20 20 20 7c 7c 20 6c 65 &len)). || le
1184d 6e 3e 3d 6e 4d 61 73 74 65 72 20 0a 20 20 20 7c n>=nMaster . |
1184e 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 | SQLITE_OK!=(rc
1184f 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 4a = read32bits(pJ
11850 72 6e 6c 2c 20 73 7a 4a 2d 31 32 2c 20 26 63 6b rnl, szJ-12, &ck
11851 73 75 6d 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 sum)). || SQLI
11852 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c TE_OK!=(rc = sql
11853 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c ite3OsRead(pJrnl
11854 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 73 7a 4a , aMagic, 8, szJ
11855 2d 38 29 29 0a 20 20 20 7c 7c 20 6d 65 6d 63 6d -8)). || memcm
11856 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e p(aMagic, aJourn
11857 61 6c 4d 61 67 69 63 2c 20 38 29 0a 20 20 20 7c alMagic, 8). |
11858 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 | SQLITE_OK!=(rc
11859 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
1185a 28 70 4a 72 6e 6c 2c 20 7a 4d 61 73 74 65 72 2c (pJrnl, zMaster,
1185b 20 6c 65 6e 2c 20 73 7a 4a 2d 31 36 2d 6c 65 6e len, szJ-16-len
1185c 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 )). ){. retu
1185d 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a rn rc;. }.. /*
1185e 20 53 65 65 20 69 66 20 74 68 65 20 63 68 65 63 See if the chec
1185f 6b 73 75 6d 20 6d 61 74 63 68 65 73 20 74 68 65 ksum matches the
11860 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
11861 6e 61 6d 65 20 2a 2f 0a 20 20 66 6f 72 28 75 3d name */. for(u=
11862 30 3b 20 75 3c 6c 65 6e 3b 20 75 2b 2b 29 7b 0a 0; u<len; u++){.
11863 20 20 20 20 63 6b 73 75 6d 20 2d 3d 20 7a 4d 61 cksum -= zMa
11864 73 74 65 72 5b 75 5d 3b 0a 20 20 7d 0a 20 20 69 ster[u];. }. i
11865 66 28 20 63 6b 73 75 6d 20 29 7b 0a 20 20 20 20 f( cksum ){.
11866 2f 2a 20 49 66 20 74 68 65 20 63 68 65 63 6b 73 /* If the checks
11867 75 6d 20 64 6f 65 73 6e 27 74 20 61 64 64 20 75 um doesn't add u
11868 70 2c 20 74 68 65 6e 20 6f 6e 65 20 6f 72 20 6d p, then one or m
11869 6f 72 65 20 6f 66 20 74 68 65 20 64 69 73 6b 20 ore of the disk
1186a 73 65 63 74 6f 72 73 0a 20 20 20 20 2a 2a 20 63 sectors. ** c
1186b 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6d 61 ontaining the ma
1186c 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
1186d 65 6e 61 6d 65 20 69 73 20 63 6f 72 72 75 70 74 ename is corrupt
1186e 65 64 2e 20 54 68 69 73 20 6d 65 61 6e 73 0a 20 ed. This means.
1186f 20 20 20 2a 2a 20 64 65 66 69 6e 69 74 65 6c 79 ** definitely
11870 20 72 6f 6c 6c 20 62 61 63 6b 2c 20 73 6f 20 6a roll back, so j
11871 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ust return SQLIT
11872 45 5f 4f 4b 20 61 6e 64 20 72 65 70 6f 72 74 20 E_OK and report
11873 61 20 28 6e 75 6c 29 0a 20 20 20 20 2a 2a 20 6d a (nul). ** m
11874 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 aster-journal fi
11875 6c 65 6e 61 6d 65 2e 0a 20 20 20 20 2a 2f 0a 20 lename.. */.
11876 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 7d 0a len = 0;. }.
11877 20 20 7a 4d 61 73 74 65 72 5b 6c 65 6e 5d 20 3d zMaster[len] =
11878 20 27 5c 30 27 3b 0a 20 20 20 0a 20 20 72 65 74 '\0';. . ret
11879 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1187a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1187b 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65 he offset of the
1187c 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 sector boundary
1187d 20 61 74 20 6f 72 20 69 6d 6d 65 64 69 61 74 65 at or immediate
1187e 6c 79 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 ly .** following
1187f 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 70 50 the value in pP
11880 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11881 2c 20 61 73 73 75 6d 69 6e 67 20 61 20 73 65 63 , assuming a sec
11882 74 6f 72 20 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 tor .** size of
11883 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
11884 7a 65 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 ze bytes..**.**
11885 69 2e 65 20 66 6f 72 20 61 20 73 65 63 74 6f 72 i.e for a sector
11886 20 73 69 7a 65 20 6f 66 20 35 31 32 3a 0a 2a 2a size of 512:.**
11887 0a 2a 2a 20 20 20 50 61 67 65 72 2e 6a 6f 75 72 .** Pager.jour
11888 6e 61 6c 4f 66 66 20 20 20 20 20 20 20 20 20 20 nalOff
11889 52 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 Return value.**
1188a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d --------------
1188b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1188c 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 30 ---------.** 0
1188d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1188e 20 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 20 20 0.**
1188f 35 31 32 20 20 20 20 20 20 20 20 20 20 20 20 20 512
11890 20 20 20 20 20 20 20 20 20 20 35 31 32 0a 2a 2a 512.**
11891 20 20 20 31 30 30 20 20 20 20 20 20 20 20 20 20 100
11892 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31 32 512
11893 0a 2a 2a 20 20 20 32 30 30 30 20 20 20 20 20 20 .** 2000
11894 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11895 32 30 34 38 0a 2a 2a 20 0a 2a 2f 0a 73 74 61 74 2048.** .*/.stat
11896 69 63 20 69 36 34 20 6a 6f 75 72 6e 61 6c 48 64 ic i64 journalHd
11897 72 4f 66 66 73 65 74 28 50 61 67 65 72 20 2a 70 rOffset(Pager *p
11898 50 61 67 65 72 29 7b 0a 20 20 69 36 34 20 6f 66 Pager){. i64 of
11899 66 73 65 74 20 3d 20 30 3b 0a 20 20 69 36 34 20 fset = 0;. i64
1189a 63 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 c = pPager->jour
1189b 6e 61 6c 4f 66 66 3b 0a 20 20 69 66 28 20 63 20 nalOff;. if( c
1189c 29 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20 ){. offset =
1189d 28 28 63 2d 31 29 2f 4a 4f 55 52 4e 41 4c 5f 48 ((c-1)/JOURNAL_H
1189e 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b 20 DR_SZ(pPager) +
1189f 31 29 20 2a 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 1) * JOURNAL_HDR
118a0 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d _SZ(pPager);. }
118a1 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 . assert( offse
118a2 74 25 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a t%JOURNAL_HDR_SZ
118a3 28 70 50 61 67 65 72 29 3d 3d 30 20 29 3b 0a 20 (pPager)==0 );.
118a4 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 3e assert( offset>
118a5 3d 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =c );. assert(
118a6 28 6f 66 66 73 65 74 2d 63 29 3c 4a 4f 55 52 4e (offset-c)<JOURN
118a7 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
118a8 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6f 66 ) );. return of
118a9 66 73 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 fset;.}../*.** T
118aa 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
118ab 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 must be open whe
118ac 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
118ad 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a is called..**.**
118ae 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
118af 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 s a no-op if the
118b0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 journal file ha
118b1 73 20 6e 6f 74 20 62 65 65 6e 20 77 72 69 74 74 s not been writt
118b2 65 6e 20 74 6f 0a 2a 2a 20 77 69 74 68 69 6e 20 en to.** within
118b3 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e the current tran
118b4 73 61 63 74 69 6f 6e 20 28 69 2e 65 2e 20 69 66 saction (i.e. if
118b5 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 Pager.journalOf
118b6 66 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 f==0)..**.** If
118b7 64 6f 54 72 75 6e 63 61 74 65 20 69 73 20 6e 6f doTruncate is no
118b8 6e 2d 7a 65 72 6f 20 6f 72 20 74 68 65 20 50 61 n-zero or the Pa
118b9 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c ger.journalSizeL
118ba 69 6d 69 74 20 76 61 72 69 61 62 6c 65 20 69 73 imit variable is
118bb 0a 2a 2a 20 73 65 74 20 74 6f 20 30 2c 20 74 68 .** set to 0, th
118bc 65 6e 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 en truncate the
118bd 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 journal file to
118be 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73 69 zero bytes in si
118bf 7a 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a ze. Otherwise,.*
118c0 2a 20 7a 65 72 6f 20 74 68 65 20 32 38 2d 62 79 * zero the 28-by
118c1 74 65 20 68 65 61 64 65 72 20 61 74 20 74 68 65 te header at the
118c2 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a 6f start of the jo
118c3 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 6e 20 65 urnal file. In e
118c4 69 74 68 65 72 20 63 61 73 65 2c 20 0a 2a 2a 20 ither case, .**
118c5 69 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 if the pager is
118c6 6e 6f 74 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d not in no-sync m
118c7 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f ode, sync the jo
118c8 75 72 6e 61 6c 20 66 69 6c 65 20 69 6d 6d 65 64 urnal file immed
118c9 69 61 74 65 6c 79 20 0a 2a 2a 20 61 66 74 65 72 iately .** after
118ca 20 77 72 69 74 69 6e 67 20 6f 72 20 74 72 75 6e writing or trun
118cb 63 61 74 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a cating it..**.**
118cc 20 49 66 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 If Pager.journa
118cd 6c 53 69 7a 65 4c 69 6d 69 74 20 69 73 20 73 65 lSizeLimit is se
118ce 74 20 74 6f 20 61 20 70 6f 73 69 74 69 76 65 2c t to a positive,
118cf 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c non-zero value,
118d0 20 61 6e 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e and.** followin
118d1 67 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f 6e g the truncation
118d2 20 6f 72 20 7a 65 72 6f 69 6e 67 20 64 65 73 63 or zeroing desc
118d3 72 69 62 65 64 20 61 62 6f 76 65 20 74 68 65 20 ribed above the
118d4 73 69 7a 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 size of the .**
118d5 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 journal file in
118d6 62 79 74 65 73 20 69 73 20 6c 61 72 67 65 72 20 bytes is larger
118d7 74 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 2c than this value,
118d8 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20 74 then truncate t
118d9 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 he.** journal fi
118da 6c 65 20 74 6f 20 50 61 67 65 72 2e 6a 6f 75 72 le to Pager.jour
118db 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 62 79 74 nalSizeLimit byt
118dc 65 73 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 es. The journal
118dd 66 69 6c 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 file does.** not
118de 20 6e 65 65 64 20 74 6f 20 62 65 20 73 79 6e 63 need to be sync
118df 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 ed following thi
118e0 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a s operation..**.
118e1 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f ** If an IO erro
118e2 72 20 6f 63 63 75 72 73 2c 20 61 62 61 6e 64 6f r occurs, abando
118e3 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 n processing and
118e4 20 72 65 74 75 72 6e 20 74 68 65 20 49 4f 20 65 return the IO e
118e5 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 20 4f 74 rror code..** Ot
118e6 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20 herwise, return
118e7 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 SQLITE_OK..*/.st
118e8 61 74 69 63 20 69 6e 74 20 7a 65 72 6f 4a 6f 75 atic int zeroJou
118e9 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 rnalHdr(Pager *p
118ea 50 61 67 65 72 2c 20 69 6e 74 20 64 6f 54 72 75 Pager, int doTru
118eb 6e 63 61 74 65 29 7b 0a 20 20 69 6e 74 20 72 63 ncate){. int rc
118ec 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
118ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
118ee 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
118ef 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
118f0 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
118f1 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 Pager->jfd) );.
118f2 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 if( pPager->jou
118f3 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 63 rnalOff ){. c
118f4 6f 6e 73 74 20 69 36 34 20 69 4c 69 6d 69 74 20 onst i64 iLimit
118f5 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 = pPager->journa
118f6 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 2f lSizeLimit; /
118f7 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 * Local cache of
118f8 20 6a 73 6c 20 2a 2f 0a 0a 20 20 20 20 49 4f 54 jsl */.. IOT
118f9 52 41 43 45 28 28 22 4a 5a 45 52 4f 48 44 52 20 RACE(("JZEROHDR
118fa 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a %p\n", pPager)).
118fb 20 20 20 20 69 66 28 20 64 6f 54 72 75 6e 63 61 if( doTrunca
118fc 74 65 20 7c 7c 20 69 4c 69 6d 69 74 3d 3d 30 20 te || iLimit==0
118fd 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
118fe 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 lite3OsTruncate(
118ff 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 30 29 3b pPager->jfd, 0);
11900 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
11901 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 static const c
11902 68 61 72 20 7a 65 72 6f 48 64 72 5b 32 38 5d 20 har zeroHdr[28]
11903 3d 20 7b 30 7d 3b 0a 20 20 20 20 20 20 72 63 20 = {0};. rc
11904 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
11905 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 65 (pPager->jfd, ze
11906 72 6f 48 64 72 2c 20 73 69 7a 65 6f 66 28 7a 65 roHdr, sizeof(ze
11907 72 6f 48 64 72 29 2c 20 30 29 3b 0a 20 20 20 20 roHdr), 0);.
11908 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
11909 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 50 61 67 LITE_OK && !pPag
1190a 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 er->noSync ){.
1190b 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1190c 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a OsSync(pPager->j
1190d 66 64 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f fd, SQLITE_SYNC_
1190e 44 41 54 41 4f 4e 4c 59 7c 70 50 61 67 65 72 2d DATAONLY|pPager-
1190f 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 >sync_flags);.
11910 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 }.. /* At t
11911 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 74 72 his point the tr
11912 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d ansaction is com
11913 6d 69 74 74 65 64 20 62 75 74 20 74 68 65 20 77 mitted but the w
11914 72 69 74 65 20 6c 6f 63 6b 20 0a 20 20 20 20 2a rite lock . *
11915 2a 20 69 73 20 73 74 69 6c 6c 20 68 65 6c 64 20 * is still held
11916 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20 on the file. If
11917 74 68 65 72 65 20 69 73 20 61 20 73 69 7a 65 20 there is a size
11918 6c 69 6d 69 74 20 63 6f 6e 66 69 67 75 72 65 64 limit configured
11919 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20 74 68 65 for . ** the
1191a 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 persistent jour
1191b 6e 61 6c 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 nal and the jour
1191c 6e 61 6c 20 66 69 6c 65 20 63 75 72 72 65 6e 74 nal file current
1191d 6c 79 20 63 6f 6e 73 75 6d 65 73 20 6d 6f 72 65 ly consumes more
1191e 0a 20 20 20 20 2a 2a 20 73 70 61 63 65 20 74 68 . ** space th
1191f 61 6e 20 74 68 61 74 20 6c 69 6d 69 74 20 61 6c an that limit al
11920 6c 6f 77 73 20 66 6f 72 2c 20 74 72 75 6e 63 61 lows for, trunca
11921 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 72 65 te it now. There
11922 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20 20 is no need.
11923 2a 2a 20 74 6f 20 73 79 6e 63 20 74 68 65 20 66 ** to sync the f
11924 69 6c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 ile following th
11925 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20 is operation..
11926 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d */. if( rc=
11927 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4c =SQLITE_OK && iL
11928 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 imit>0 ){.
11929 69 36 34 20 73 7a 3b 0a 20 20 20 20 20 20 72 63 i64 sz;. rc
1192a 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 = sqlite3OsFile
1192b 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 Size(pPager->jfd
1192c 2c 20 26 73 7a 29 3b 0a 20 20 20 20 20 20 69 66 , &sz);. if
1192d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1192e 26 26 20 73 7a 3e 69 4c 69 6d 69 74 20 29 7b 0a && sz>iLimit ){.
1192f 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
11930 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 ite3OsTruncate(p
11931 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 4c 69 6d Pager->jfd, iLim
11932 69 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 it);. }.
11933 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
11934 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 rc;.}../*.** The
11935 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 journal file mu
11936 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 st be open when
11937 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
11938 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61 called. A journa
11939 6c 0a 2a 2a 20 68 65 61 64 65 72 20 28 4a 4f 55 l.** header (JOU
1193a 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 RNAL_HDR_SZ byte
1193b 73 29 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e s) is written in
1193c 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
1193d 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20 63 75 ile at the.** cu
1193e 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 0a rrent location..
1193f 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 **.** The format
11940 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c for the journal
11941 20 68 65 61 64 65 72 20 69 73 20 61 73 20 66 6f header is as fo
11942 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d 20 38 20 62 79 llows:.** - 8 by
11943 74 65 73 3a 20 4d 61 67 69 63 20 69 64 65 6e 74 tes: Magic ident
11944 69 66 79 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 ifying journal f
11945 6f 72 6d 61 74 2e 0a 2a 2a 20 2d 20 34 20 62 79 ormat..** - 4 by
11946 74 65 73 3a 20 4e 75 6d 62 65 72 20 6f 66 20 72 tes: Number of r
11947 65 63 6f 72 64 73 20 69 6e 20 6a 6f 75 72 6e 61 ecords in journa
11948 6c 2c 20 6f 72 20 2d 31 20 6e 6f 2d 73 79 6e 63 l, or -1 no-sync
11949 20 6d 6f 64 65 20 69 73 20 6f 6e 2e 0a 2a 2a 20 mode is on..**
1194a 2d 20 34 20 62 79 74 65 73 3a 20 52 61 6e 64 6f - 4 bytes: Rando
1194b 6d 20 6e 75 6d 62 65 72 20 75 73 65 64 20 66 6f m number used fo
1194c 72 20 70 61 67 65 20 68 61 73 68 2e 0a 2a 2a 20 r page hash..**
1194d 2d 20 34 20 62 79 74 65 73 3a 20 49 6e 69 74 69 - 4 bytes: Initi
1194e 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 67 65 al database page
1194f 20 63 6f 75 6e 74 2e 0a 2a 2a 20 2d 20 34 20 62 count..** - 4 b
11950 79 74 65 73 3a 20 53 65 63 74 6f 72 20 73 69 7a ytes: Sector siz
11951 65 20 75 73 65 64 20 62 79 20 74 68 65 20 70 72 e used by the pr
11952 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f 74 65 ocess that wrote
11953 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 0a 2a this journal..*
11954 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 44 61 74 * - 4 bytes: Dat
11955 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 2e abase page size.
11956 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c 6c 6f 77 65 64 .** .** Followed
11957 20 62 79 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 by (JOURNAL_HDR
11958 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73 20 _SZ - 28) bytes
11959 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 65 2e of unused space.
1195a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
1195b 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 50 riteJournalHdr(P
1195c 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
1195d 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1195e 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 _OK;
1195f 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
11960 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ode */. char *z
11961 48 65 61 64 65 72 20 3d 20 70 50 61 67 65 72 2d Header = pPager-
11962 3e 70 54 6d 70 53 70 61 63 65 3b 20 20 2f 2a 20 >pTmpSpace; /*
11963 54 65 6d 70 6f 72 61 72 79 20 73 70 61 63 65 20 Temporary space
11964 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20 68 65 used to build he
11965 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 48 ader */. u32 nH
11966 65 61 64 65 72 20 3d 20 70 50 61 67 65 72 2d 3e eader = pPager->
11967 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 2f 2a pageSize; /*
11968 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20 Size of buffer
11969 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 48 pointed to by zH
1196a 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e eader */. u32 n
1196b 57 72 69 74 65 3b 20 20 20 20 20 20 20 20 20 20 Write;
1196c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1196d 2a 20 42 79 74 65 73 20 6f 66 20 68 65 61 64 65 * Bytes of heade
1196e 72 20 73 65 63 74 6f 72 20 77 72 69 74 74 65 6e r sector written
1196f 20 2a 2f 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 */. int ii;
11970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11971 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
11972 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 p counter */..
11973 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
11974 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20 20 Pager->jfd) );
11975 20 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 66 /* Journal f
11976 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e ile must be open
11977 2e 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 48 65 61 . */.. if( nHea
11978 64 65 72 3e 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f der>JOURNAL_HDR_
11979 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 SZ(pPager) ){.
1197a 20 20 6e 48 65 61 64 65 72 20 3d 20 4a 4f 55 52 nHeader = JOUR
1197b 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
1197c 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 r);. }.. /* If
1197d 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76 there are activ
1197e 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e 64 e savepoints and
1197f 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 77 65 72 any of them wer
11980 65 20 63 72 65 61 74 65 64 20 0a 20 20 2a 2a 20 e created . **
11981 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 20 72 since the most r
11982 65 63 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 68 65 ecent journal he
11983 61 64 65 72 20 77 61 73 20 77 72 69 74 74 65 6e ader was written
11984 2c 20 75 70 64 61 74 65 20 74 68 65 20 0a 20 20 , update the .
11985 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e ** PagerSavepoin
11986 74 2e 69 48 64 72 4f 66 66 73 65 74 20 66 69 65 t.iHdrOffset fie
11987 6c 64 73 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 lds now.. */.
11988 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 for(ii=0; ii<pPa
11989 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b ger->nSavepoint;
1198a 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ii++){. if(
1198b 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 pPager->aSavepoi
1198c 6e 74 5b 69 69 5d 2e 69 48 64 72 4f 66 66 73 65 nt[ii].iHdrOffse
1198d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 50 t==0 ){. pP
1198e 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 ager->aSavepoint
1198f 5b 69 69 5d 2e 69 48 64 72 4f 66 66 73 65 74 20 [ii].iHdrOffset
11990 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 = pPager->journa
11991 6c 4f 66 66 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a lOff;. }. }.
11992 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e . pPager->journ
11993 61 6c 48 64 72 20 3d 20 70 50 61 67 65 72 2d 3e alHdr = pPager->
11994 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 journalOff = jou
11995 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70 50 rnalHdrOffset(pP
11996 61 67 65 72 29 3b 0a 0a 20 20 2f 2a 20 0a 20 20 ager);.. /* .
11997 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52 65 ** Write the nRe
11998 63 20 46 69 65 6c 64 20 2d 20 74 68 65 20 6e 75 c Field - the nu
11999 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72 65 63 mber of page rec
1199a 6f 72 64 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 ords that follow
1199b 20 74 68 69 73 0a 20 20 2a 2a 20 6a 6f 75 72 6e this. ** journ
1199c 61 6c 20 68 65 61 64 65 72 2e 20 4e 6f 72 6d 61 al header. Norma
1199d 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 20 77 72 69 lly, zero is wri
1199e 74 74 65 6e 20 74 6f 20 74 68 69 73 20 76 61 6c tten to this val
1199f 75 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e ue at this time.
119a0 0a 20 20 2a 2a 20 41 66 74 65 72 20 74 68 65 20 . ** After the
119a1 72 65 63 6f 72 64 73 20 61 72 65 20 61 64 64 65 records are adde
119a2 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c d to the journal
119a3 20 28 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 (and the journa
119a4 6c 20 73 79 6e 63 65 64 2c 20 0a 20 20 2a 2a 20 l synced, . **
119a5 69 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 if in full-sync
119a6 6d 6f 64 65 29 2c 20 74 68 65 20 7a 65 72 6f 20 mode), the zero
119a7 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 is overwritten w
119a8 69 74 68 20 74 68 65 20 74 72 75 65 20 6e 75 6d ith the true num
119a9 62 65 72 0a 20 20 2a 2a 20 6f 66 20 72 65 63 6f ber. ** of reco
119aa 72 64 73 20 28 73 65 65 20 73 79 6e 63 4a 6f 75 rds (see syncJou
119ab 72 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a 0a 20 20 rnal()).. **.
119ac 2a 2a 20 41 20 66 61 73 74 65 72 20 61 6c 74 65 ** A faster alte
119ad 72 6e 61 74 69 76 65 20 69 73 20 74 6f 20 77 72 rnative is to wr
119ae 69 74 65 20 30 78 46 46 46 46 46 46 46 46 20 74 ite 0xFFFFFFFF t
119af 6f 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 o the nRec field
119b0 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 72 65 61 64 . When. ** read
119b1 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ing the journal
119b2 74 68 69 73 20 76 61 6c 75 65 20 74 65 6c 6c 73 this value tells
119b3 20 53 51 4c 69 74 65 20 74 6f 20 61 73 73 75 6d SQLite to assum
119b4 65 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 e that the. **
119b5 72 65 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 rest of the jour
119b6 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e nal file contain
119b7 73 20 76 61 6c 69 64 20 70 61 67 65 20 72 65 63 s valid page rec
119b8 6f 72 64 73 2e 20 54 68 69 73 20 61 73 73 75 6d ords. This assum
119b9 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 64 61 ption. ** is da
119ba 6e 67 65 72 6f 75 73 2c 20 61 73 20 69 66 20 61 ngerous, as if a
119bb 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72 65 failure occurre
119bc 64 20 77 68 69 6c 73 74 20 77 72 69 74 69 6e 67 d whilst writing
119bd 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a to the journal.
119be 20 20 2a 2a 20 66 69 6c 65 20 69 74 20 6d 61 79 ** file it may
119bf 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65 20 67 61 contain some ga
119c0 72 62 61 67 65 20 64 61 74 61 2e 20 54 68 65 72 rbage data. Ther
119c1 65 20 61 72 65 20 74 77 6f 20 73 63 65 6e 61 72 e are two scenar
119c2 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72 65 20 74 ios. ** where t
119c3 68 69 73 20 72 69 73 6b 20 63 61 6e 20 62 65 20 his risk can be
119c4 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a 0a 20 20 ignored:. **.
119c5 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68 65 20 ** * When the
119c6 70 61 67 65 72 20 69 73 20 69 6e 20 6e 6f 2d 73 pager is in no-s
119c7 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 72 75 70 ync mode. Corrup
119c8 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c 6f 77 20 tion can follow
119c9 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f 77 65 72 a. ** power
119ca 20 66 61 69 6c 75 72 65 20 69 6e 20 74 68 69 73 failure in this
119cb 20 63 61 73 65 20 61 6e 79 77 61 79 2e 0a 20 20 case anyway..
119cc 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e **. ** * When
119cd 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 the SQLITE_IOCA
119ce 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 66 6c P_SAFE_APPEND fl
119cf 61 67 20 69 73 20 73 65 74 2e 20 54 68 69 73 20 ag is set. This
119d0 67 75 61 72 61 6e 74 65 65 73 0a 20 20 2a 2a 20 guarantees. **
119d1 20 20 20 20 74 68 61 74 20 67 61 72 62 61 67 65 that garbage
119d2 20 64 61 74 61 20 69 73 20 6e 65 76 65 72 20 61 data is never a
119d3 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 6a ppended to the j
119d4 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 2a ournal file.. *
119d5 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 /. assert( isOp
119d6 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c en(pPager->fd) |
119d7 7c 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 | pPager->noSync
119d8 20 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65 );. if( (pPage
119d9 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20 28 70 r->noSync) || (p
119da 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
119db 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
119dc 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a 20 20 LMODE_MEMORY).
119dd 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73 44 65 || (sqlite3OsDe
119de 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
119df 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 26 ics(pPager->fd)&
119e0 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 SQLITE_IOCAP_SAF
119e1 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29 7b 0a E_APPEND) . ){.
119e2 20 20 20 20 6d 65 6d 63 70 79 28 7a 48 65 61 64 memcpy(zHead
119e3 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 er, aJournalMagi
119e4 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e c, sizeof(aJourn
119e5 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20 20 20 70 alMagic));. p
119e6 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 ut32bits(&zHeade
119e7 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 r[sizeof(aJourna
119e8 6c 4d 61 67 69 63 29 5d 2c 20 30 78 66 66 66 66 lMagic)], 0xffff
119e9 66 66 66 66 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a ffff);. }else{.
119ea 20 20 20 20 6d 65 6d 73 65 74 28 7a 48 65 61 64 memset(zHead
119eb 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 4a er, 0, sizeof(aJ
119ec 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 34 29 3b ournalMagic)+4);
119ed 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 72 . }.. /* The r
119ee 61 6e 64 6f 6d 20 63 68 65 63 6b 2d 68 61 73 68 andom check-hash
119ef 20 69 6e 69 74 69 61 6c 69 73 65 72 20 2a 2f 20 initialiser */
119f0 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f . sqlite3_rando
119f1 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 70 50 61 mness(sizeof(pPa
119f2 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 2c ger->cksumInit),
119f3 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 &pPager->cksumI
119f4 6e 69 74 29 3b 0a 20 20 70 75 74 33 32 62 69 74 nit);. put32bit
119f5 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f s(&zHeader[sizeo
119f6 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 f(aJournalMagic)
119f7 2b 34 5d 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73 +4], pPager->cks
119f8 75 6d 49 6e 69 74 29 3b 0a 20 20 2f 2a 20 54 68 umInit);. /* Th
119f9 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61 62 61 e initial databa
119fa 73 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70 75 74 se size */. put
119fb 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 32bits(&zHeader[
119fc 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d sizeof(aJournalM
119fd 61 67 69 63 29 2b 38 5d 2c 20 70 50 61 67 65 72 agic)+8], pPager
119fe 2d 3e 64 62 4f 72 69 67 53 69 7a 65 29 3b 0a 20 ->dbOrigSize);.
119ff 20 2f 2a 20 54 68 65 20 61 73 73 75 6d 65 64 20 /* The assumed
11a00 73 65 63 74 6f 72 20 73 69 7a 65 20 66 6f 72 20 sector size for
11a01 74 68 69 73 20 70 72 6f 63 65 73 73 20 2a 2f 0a this process */.
11a02 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 put32bits(&zHe
11a03 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 ader[sizeof(aJou
11a04 72 6e 61 6c 4d 61 67 69 63 29 2b 31 32 5d 2c 20 rnalMagic)+12],
11a05 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
11a06 7a 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 70 ze);.. /* The p
11a07 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70 75 age size */. pu
11a08 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 t32bits(&zHeader
11a09 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c [sizeof(aJournal
11a0a 4d 61 67 69 63 29 2b 31 36 5d 2c 20 70 50 61 67 Magic)+16], pPag
11a0b 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a er->pageSize);..
11a0c 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 69 6e /* Initializin
11a0d 67 20 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68 g the tail of th
11a0e 65 20 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 e buffer is not
11a0f 6e 65 63 65 73 73 61 72 79 2e 20 20 45 76 65 72 necessary. Ever
11a10 79 74 68 69 6e 67 0a 20 20 2a 2a 20 77 6f 72 6b ything. ** work
11a11 73 20 66 69 6e 64 20 69 66 20 74 68 65 20 66 6f s find if the fo
11a12 6c 6c 6f 77 69 6e 67 20 6d 65 6d 73 65 74 28 29 llowing memset()
11a13 20 69 73 20 6f 6d 69 74 74 65 64 2e 20 20 42 75 is omitted. Bu
11a14 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 0a 20 t initializing.
11a15 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 70 ** the memory p
11a16 72 65 76 65 6e 74 73 20 76 61 6c 67 72 69 6e 64 revents valgrind
11a17 20 66 72 6f 6d 20 63 6f 6d 70 6c 61 69 6e 69 6e from complainin
11a18 67 2c 20 73 6f 20 77 65 20 61 72 65 20 77 69 6c g, so we are wil
11a19 6c 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 61 6b ling to. ** tak
11a1a 65 20 74 68 65 20 70 65 72 66 6f 72 6d 61 6e 63 e the performanc
11a1b 65 20 68 69 74 2e 0a 20 20 2a 2f 0a 20 20 6d 65 e hit.. */. me
11a1c 6d 73 65 74 28 26 7a 48 65 61 64 65 72 5b 73 69 mset(&zHeader[si
11a1d 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
11a1e 69 63 29 2b 32 30 5d 2c 20 30 2c 0a 20 20 20 20 ic)+20], 0,.
11a1f 20 20 20 20 20 6e 48 65 61 64 65 72 2d 28 73 69 nHeader-(si
11a20 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
11a21 69 63 29 2b 32 30 29 29 3b 0a 0a 20 20 2f 2a 20 ic)+20));.. /*
11a22 49 6e 20 74 68 65 6f 72 79 2c 20 69 74 20 69 73 In theory, it is
11a23 20 6f 6e 6c 79 20 6e 65 63 65 73 73 61 72 79 20 only necessary
11a24 74 6f 20 77 72 69 74 65 20 74 68 65 20 32 38 20 to write the 28
11a25 62 79 74 65 73 20 74 68 61 74 20 74 68 65 20 0a bytes that the .
11a26 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 ** journal hea
11a27 64 65 72 20 63 6f 6e 73 75 6d 65 73 20 74 6f 20 der consumes to
11a28 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11a29 20 68 65 72 65 2e 20 54 68 65 6e 20 69 6e 63 72 here. Then incr
11a2a 65 6d 65 6e 74 20 74 68 65 20 0a 20 20 2a 2a 20 ement the . **
11a2b 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 Pager.journalOff
11a2c 20 76 61 72 69 61 62 6c 65 20 62 79 20 4a 4f 55 variable by JOU
11a2d 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 73 6f 20 74 RNAL_HDR_SZ so t
11a2e 68 61 74 20 74 68 65 20 6e 65 78 74 20 0a 20 20 hat the next .
11a2f 2a 2a 20 72 65 63 6f 72 64 20 69 73 20 77 72 69 ** record is wri
11a30 74 74 65 6e 20 74 6f 20 74 68 65 20 66 6f 6c 6c tten to the foll
11a31 6f 77 69 6e 67 20 73 65 63 74 6f 72 20 28 6c 65 owing sector (le
11a32 61 76 69 6e 67 20 61 20 67 61 70 20 69 6e 20 74 aving a gap in t
11a33 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 74 68 61 he file. ** tha
11a34 74 20 77 69 6c 6c 20 62 65 20 69 6d 70 6c 69 63 t will be implic
11a35 69 74 6c 79 20 66 69 6c 6c 65 64 20 69 6e 20 62 itly filled in b
11a36 79 20 74 68 65 20 4f 53 29 2e 0a 20 20 2a 2a 0a y the OS).. **.
11a37 20 20 2a 2a 20 48 6f 77 65 76 65 72 20 69 74 20 ** However it
11a38 68 61 73 20 62 65 65 6e 20 64 69 73 63 6f 76 65 has been discove
11a39 72 65 64 20 74 68 61 74 20 6f 6e 20 73 6f 6d 65 red that on some
11a3a 20 73 79 73 74 65 6d 73 20 74 68 69 73 20 70 61 systems this pa
11a3b 74 74 65 72 6e 20 63 61 6e 20 0a 20 20 2a 2a 20 ttern can . **
11a3c 62 65 20 73 69 67 6e 69 66 69 63 61 6e 74 6c 79 be significantly
11a3d 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 63 6f 6e slower than con
11a3e 74 69 67 75 6f 75 73 6c 79 20 77 72 69 74 69 6e tiguously writin
11a3f 67 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 g data to the fi
11a40 6c 65 2c 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66 le,. ** even if
11a41 20 74 68 61 74 20 6d 65 61 6e 73 20 65 78 70 6c that means expl
11a42 69 63 69 74 6c 79 20 77 72 69 74 69 6e 67 20 64 icitly writing d
11a43 61 74 61 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b ata to the block
11a44 20 6f 66 20 0a 20 20 2a 2a 20 28 4a 4f 55 52 4e of . ** (JOURN
11a45 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 29 20 AL_HDR_SZ - 28)
11a46 62 79 74 65 73 20 74 68 61 74 20 77 69 6c 6c 20 bytes that will
11a47 6e 6f 74 20 62 65 20 75 73 65 64 2e 20 53 6f 20 not be used. So
11a48 74 68 61 74 20 69 73 20 77 68 61 74 0a 20 20 2a that is what. *
11a49 2a 20 69 73 20 64 6f 6e 65 2e 20 0a 20 20 2a 2a * is done. . **
11a4a 0a 20 20 2a 2a 20 54 68 65 20 6c 6f 6f 70 20 69 . ** The loop i
11a4b 73 20 72 65 71 75 69 72 65 64 20 68 65 72 65 20 s required here
11a4c 69 6e 20 63 61 73 65 20 74 68 65 20 73 65 63 74 in case the sect
11a4d 6f 72 2d 73 69 7a 65 20 69 73 20 6c 61 72 67 65 or-size is large
11a4e 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 2a 2a r than the . **
11a4f 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 73 database page s
11a50 69 7a 65 2e 20 53 69 6e 63 65 20 74 68 65 20 7a ize. Since the z
11a51 48 65 61 64 65 72 20 62 75 66 66 65 72 20 69 73 Header buffer is
11a52 20 6f 6e 6c 79 20 50 61 67 65 72 2e 70 61 67 65 only Pager.page
11a53 53 69 7a 65 0a 20 20 2a 2a 20 62 79 74 65 73 20 Size. ** bytes
11a54 69 6e 20 73 69 7a 65 2c 20 6d 6f 72 65 20 74 68 in size, more th
11a55 61 6e 20 6f 6e 65 20 63 61 6c 6c 20 74 6f 20 73 an one call to s
11a56 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 29 20 qlite3OsWrite()
11a57 6d 61 79 20 62 65 20 72 65 71 75 69 72 65 64 0a may be required.
11a58 20 20 2a 2a 20 74 6f 20 70 6f 70 75 6c 61 74 65 ** to populate
11a59 20 74 68 65 20 65 6e 74 69 72 65 20 6a 6f 75 72 the entire jour
11a5a 6e 61 6c 20 68 65 61 64 65 72 20 73 65 63 74 6f nal header secto
11a5b 72 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 6e r.. */ . for(n
11a5c 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d 53 51 4c Write=0; rc==SQL
11a5d 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 65 3c 4a ITE_OK&&nWrite<J
11a5e 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
11a5f 61 67 65 72 29 3b 20 6e 57 72 69 74 65 2b 3d 6e ager); nWrite+=n
11a60 48 65 61 64 65 72 29 7b 0a 20 20 20 20 49 4f 54 Header){. IOT
11a61 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 20 25 RACE(("JHDR %p %
11a62 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 lld %d\n", pPage
11a63 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e r, pPager->journ
11a64 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 72 29 29 alHdr, nHeader))
11a65 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
11a66 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3OsWrite(pPager-
11a67 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c 20 6e >jfd, zHeader, n
11a68 48 65 61 64 65 72 2c 20 70 50 61 67 65 72 2d 3e Header, pPager->
11a69 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 20 journalOff);.
11a6a 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11a6b 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 72 3b 0a Off += nHeader;.
11a6c 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 }.. return rc
11a6d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a ;.}../*.** The j
11a6e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 ournal file must
11a6f 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 be open when th
11a70 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20 41 20 is is called. A
11a71 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 journal header f
11a72 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f ile.** (JOURNAL_
11a73 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69 73 HDR_SZ bytes) is
11a74 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 63 read from the c
11a75 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 urrent location
11a76 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a in the journal.*
11a77 2a 20 66 69 6c 65 2e 20 54 68 65 20 63 75 72 72 * file. The curr
11a78 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 ent location in
11a79 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11a7a 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 is given by.**
11a7b 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11a7c 66 66 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 ff. See comments
11a7d 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20 above function
11a7e 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 writeJournalHdr(
11a7f 29 20 66 6f 72 0a 2a 2a 20 61 20 64 65 73 63 72 ) for.** a descr
11a80 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f iption of the jo
11a81 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 6f 72 urnal header for
11a82 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 mat..**.** If th
11a83 65 20 68 65 61 64 65 72 20 69 73 20 72 65 61 64 e header is read
11a84 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 2a successfully, *
11a85 70 4e 52 65 63 20 69 73 20 73 65 74 20 74 6f 20 pNRec is set to
11a86 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a the number of.**
11a87 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 6f page records fo
11a88 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 68 65 61 llowing this hea
11a89 64 65 72 20 61 6e 64 20 2a 70 44 62 53 69 7a 65 der and *pDbSize
11a8a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 73 is set to the s
11a8b 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 ize of the.** da
11a8c 74 61 62 61 73 65 20 62 65 66 6f 72 65 20 74 68 tabase before th
11a8d 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 e transaction be
11a8e 67 61 6e 2c 20 69 6e 20 70 61 67 65 73 2e 20 41 gan, in pages. A
11a8f 6c 73 6f 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73 lso, pPager->cks
11a90 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 20 73 65 74 umInit.** is set
11a91 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 72 65 to the value re
11a92 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 ad from the jour
11a93 6e 61 6c 20 68 65 61 64 65 72 2e 20 53 51 4c 49 nal header. SQLI
11a94 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
11a95 64 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73 d.** in this cas
11a96 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
11a97 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 journal header f
11a98 69 6c 65 20 61 70 70 65 61 72 73 20 74 6f 20 62 ile appears to b
11a99 65 20 63 6f 72 72 75 70 74 65 64 2c 20 53 51 4c e corrupted, SQL
11a9a 49 54 45 5f 44 4f 4e 45 20 69 73 0a 2a 2a 20 72 ITE_DONE is.** r
11a9b 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 4e 52 eturned and *pNR
11a9c 65 63 20 61 6e 64 20 2a 50 44 62 53 69 7a 65 20 ec and *PDbSize
11a9d 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 are undefined.
11a9e 49 66 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 If JOURNAL_HDR_S
11a9f 5a 20 62 79 74 65 73 0a 2a 2a 20 63 61 6e 6e 6f Z bytes.** canno
11aa0 74 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74 t be read from t
11aa1 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11aa2 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 an error code is
11aa3 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 returned..*/.st
11aa4 61 74 69 63 20 69 6e 74 20 72 65 61 64 4a 6f 75 atic int readJou
11aa5 72 6e 61 6c 48 64 72 28 0a 20 20 50 61 67 65 72 rnalHdr(. Pager
11aa6 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 *pPager,
11aa7 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 /* Pager
11aa8 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 object */. int
11aa9 20 69 73 48 6f 74 2c 0a 20 20 69 36 34 20 6a 6f isHot,. i64 jo
11aaa 75 72 6e 61 6c 53 69 7a 65 2c 20 20 20 20 20 20 urnalSize,
11aab 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
11aac 66 20 74 68 65 20 6f 70 65 6e 20 6a 6f 75 72 6e f the open journ
11aad 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 al file in bytes
11aae 20 2a 2f 0a 20 20 75 33 32 20 2a 70 4e 52 65 63 */. u32 *pNRec
11aaf 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
11ab0 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 6c 75 65 /* OUT: Value
11ab1 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6e read from the n
11ab2 52 65 63 20 66 69 65 6c 64 20 2a 2f 0a 20 20 75 Rec field */. u
11ab3 33 32 20 2a 70 44 62 53 69 7a 65 20 20 20 20 20 32 *pDbSize
11ab4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
11ab5 55 54 3a 20 56 61 6c 75 65 20 6f 66 20 6f 72 69 UT: Value of ori
11ab6 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 73 ginal database s
11ab7 69 7a 65 20 66 69 65 6c 64 20 2a 2f 0a 29 7b 0a ize field */.){.
11ab8 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
11ab9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11aba 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
11abb 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
11abc 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 20 20 aMagic[8];
11abd 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 /* A buffer to h
11abe 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 old the magic he
11abf 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 69 48 ader */. i64 iH
11ac0 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 drOff;
11ac1 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
11ac2 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 of journal head
11ac3 65 72 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f er being read */
11ac4 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 .. assert( isOp
11ac5 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
11ac6 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f 75 72 6e ); /* Journ
11ac7 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 al file must be
11ac8 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 2f 2a 20 41 open. */.. /* A
11ac9 64 76 61 6e 63 65 20 50 61 67 65 72 2e 6a 6f 75 dvance Pager.jou
11aca 72 6e 61 6c 4f 66 66 20 74 6f 20 74 68 65 20 73 rnalOff to the s
11acb 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65 78 74 tart of the next
11acc 20 73 65 63 74 6f 72 2e 20 49 66 20 74 68 65 0a sector. If the.
11acd 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ** journal fil
11ace 65 20 69 73 20 74 6f 6f 20 73 6d 61 6c 6c 20 66 e is too small f
11acf 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65 20 61 or there to be a
11ad0 20 68 65 61 64 65 72 20 73 74 6f 72 65 64 20 61 header stored a
11ad1 74 20 74 68 69 73 0a 20 20 2a 2a 20 70 6f 69 6e t this. ** poin
11ad2 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 t, return SQLITE
11ad3 5f 44 4f 4e 45 2e 0a 20 20 2a 2f 0a 20 20 70 50 _DONE.. */. pP
11ad4 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11ad5 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 = journalHdrOff
11ad6 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 69 set(pPager);. i
11ad7 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
11ad8 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 48 44 alOff+JOURNAL_HD
11ad9 52 5f 53 5a 28 70 50 61 67 65 72 29 20 3e 20 6a R_SZ(pPager) > j
11ada 6f 75 72 6e 61 6c 53 69 7a 65 20 29 7b 0a 20 20 ournalSize ){.
11adb 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
11adc 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 48 64 72 DONE;. }. iHdr
11add 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f Off = pPager->jo
11ade 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 urnalOff;.. /*
11adf 52 65 61 64 20 69 6e 20 74 68 65 20 66 69 72 73 Read in the firs
11ae0 74 20 38 20 62 79 74 65 73 20 6f 66 20 74 68 65 t 8 bytes of the
11ae1 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e journal header.
11ae2 20 49 66 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 If they do not
11ae3 6d 61 74 63 68 0a 20 20 2a 2a 20 74 68 65 20 20 match. ** the
11ae4 6d 61 67 69 63 20 73 74 72 69 6e 67 20 66 6f 75 magic string fou
11ae5 6e 64 20 61 74 20 74 68 65 20 73 74 61 72 74 20 nd at the start
11ae6 6f 66 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 of each journal
11ae7 68 65 61 64 65 72 2c 20 72 65 74 75 72 6e 0a 20 header, return.
11ae8 20 2a 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e ** SQLITE_DONE.
11ae9 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 If an IO error
11aea 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 61 occurs, return a
11aeb 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 4f 74 n error code. Ot
11aec 68 65 72 77 69 73 65 2c 0a 20 20 2a 2a 20 70 72 herwise,. ** pr
11aed 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 oceed.. */. if
11aee 28 20 69 73 48 6f 74 20 7c 7c 20 69 48 64 72 4f ( isHot || iHdrO
11aef 66 66 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 ff!=pPager->jour
11af0 6e 61 6c 48 64 72 20 29 7b 0a 20 20 20 20 72 63 nalHdr ){. rc
11af1 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
11af2 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4d (pPager->jfd, aM
11af3 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4d 61 agic, sizeof(aMa
11af4 67 69 63 29 2c 20 69 48 64 72 4f 66 66 29 3b 0a gic), iHdrOff);.
11af5 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
11af6 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
11af7 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d }. if( mem
11af8 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 cmp(aMagic, aJou
11af9 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f rnalMagic, sizeo
11afa 66 28 61 4d 61 67 69 63 29 29 21 3d 30 20 29 7b f(aMagic))!=0 ){
11afb 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
11afc 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d LITE_DONE;. }
11afd 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 . }.. /* Read
11afe 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20 the first three
11aff 33 32 2d 62 69 74 20 66 69 65 6c 64 73 20 6f 66 32-bit fields of
11b00 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 the journal hea
11b01 64 65 72 3a 20 54 68 65 20 6e 52 65 63 0a 20 20 der: The nRec.
11b02 2a 2a 20 66 69 65 6c 64 2c 20 74 68 65 20 63 68 ** field, the ch
11b03 65 63 6b 73 75 6d 2d 69 6e 69 74 69 61 6c 69 7a ecksum-initializ
11b04 65 72 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 er and the datab
11b05 61 73 65 20 73 69 7a 65 20 61 74 20 74 68 65 20 ase size at the
11b06 73 74 61 72 74 0a 20 20 2a 2a 20 6f 66 20 74 68 start. ** of th
11b07 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 52 e transaction. R
11b08 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 eturn an error c
11b09 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 ode if anything
11b0a 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 20 20 2a 2f goes wrong.. */
11b0b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b . if( SQLITE_OK
11b0c 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 !=(rc = read32bi
11b0d 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ts(pPager->jfd,
11b0e 69 48 64 72 4f 66 66 2b 38 2c 20 70 4e 52 65 63 iHdrOff+8, pNRec
11b0f 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f )). || SQLITE_
11b10 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 OK!=(rc = read32
11b11 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
11b12 2c 20 69 48 64 72 4f 66 66 2b 31 32 2c 20 26 70 , iHdrOff+12, &p
11b13 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 Pager->cksumInit
11b14 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f )). || SQLITE_
11b15 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 OK!=(rc = read32
11b16 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
11b17 2c 20 69 48 64 72 4f 66 66 2b 31 36 2c 20 70 44 , iHdrOff+16, pD
11b18 62 53 69 7a 65 29 29 0a 20 20 29 7b 0a 20 20 20 bSize)). ){.
11b19 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
11b1a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a . if( pPager->j
11b1b 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a ournalOff==0 ){.
11b1c 20 20 20 20 75 33 32 20 69 50 61 67 65 53 69 7a u32 iPageSiz
11b1d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
11b1e 20 2f 2a 20 50 61 67 65 2d 73 69 7a 65 20 66 69 /* Page-size fi
11b1f 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 eld of journal h
11b20 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 33 32 eader */. u32
11b21 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 iSectorSize;
11b22 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 /* Sec
11b23 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f tor-size field o
11b24 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 f journal header
11b25 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 50 61 67 */. u16 iPag
11b26 65 53 69 7a 65 31 36 3b 20 20 20 20 20 20 20 20 eSize16;
11b27 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 /* Copy of
11b28 69 50 61 67 65 53 69 7a 65 20 69 6e 20 31 36 2d iPageSize in 16-
11b29 62 69 74 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a bit variable */.
11b2a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 . /* Read the
11b2b 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 page-size and s
11b2c 65 63 74 6f 72 2d 73 69 7a 65 20 6a 6f 75 72 6e ector-size journ
11b2d 61 6c 20 68 65 61 64 65 72 20 66 69 65 6c 64 73 al header fields
11b2e 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c . */. if( SQL
11b2f 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 ITE_OK!=(rc = re
11b30 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d ad32bits(pPager-
11b31 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 32 30 >jfd, iHdrOff+20
11b32 2c 20 26 69 53 65 63 74 6f 72 53 69 7a 65 29 29 , &iSectorSize))
11b33 0a 20 20 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f . || SQLITE_
11b34 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 OK!=(rc = read32
11b35 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
11b36 2c 20 69 48 64 72 4f 66 66 2b 32 34 2c 20 26 69 , iHdrOff+24, &i
11b37 50 61 67 65 53 69 7a 65 29 29 0a 20 20 20 20 29 PageSize)). )
11b38 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
11b39 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a c;. }.. /*
11b3a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 Check that the
11b3b 76 61 6c 75 65 73 20 72 65 61 64 20 66 72 6f 6d values read from
11b3c 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 the page-size a
11b3d 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 66 nd sector-size f
11b3e 69 65 6c 64 73 0a 20 20 20 20 2a 2a 20 61 72 65 ields. ** are
11b3f 20 77 69 74 68 69 6e 20 72 61 6e 67 65 2e 20 54 within range. T
11b40 6f 20 62 65 20 27 69 6e 20 72 61 6e 67 65 27 2c o be 'in range',
11b41 20 62 6f 74 68 20 76 61 6c 75 65 73 20 6e 65 65 both values nee
11b42 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72 0a d to be a power.
11b43 20 20 20 20 2a 2a 20 6f 66 20 74 77 6f 20 67 72 ** of two gr
11b44 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 eater than or eq
11b45 75 61 6c 20 74 6f 20 35 31 32 2c 20 61 6e 64 20 ual to 512, and
11b46 6e 6f 74 20 67 72 65 61 74 65 72 20 74 68 61 6e not greater than
11b47 20 74 68 65 69 72 20 0a 20 20 20 20 2a 2a 20 72 their . ** r
11b48 65 73 70 65 63 74 69 76 65 20 63 6f 6d 70 69 6c espective compil
11b49 65 20 74 69 6d 65 20 6d 61 78 69 6d 75 6d 20 6c e time maximum l
11b4a 69 6d 69 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 imits.. */.
11b4b 20 20 69 66 28 20 69 50 61 67 65 53 69 7a 65 3c if( iPageSize<
11b4c 35 31 32 20 20 20 20 20 20 20 20 20 20 20 20 20 512
11b4d 20 20 20 20 20 7c 7c 20 69 53 65 63 74 6f 72 53 || iSectorS
11b4e 69 7a 65 3c 35 31 32 0a 20 20 20 20 20 7c 7c 20 ize<512. ||
11b4f 69 50 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 iPageSize>SQLITE
11b50 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 7c _MAX_PAGE_SIZE |
11b51 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 3e 4d 41 | iSectorSize>MA
11b52 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 0a 20 20 X_SECTOR_SIZE.
11b53 20 20 20 7c 7c 20 28 28 69 50 61 67 65 53 69 7a || ((iPageSiz
11b54 65 2d 31 29 26 69 50 61 67 65 53 69 7a 65 29 21 e-1)&iPageSize)!
11b55 3d 30 20 20 20 7c 7c 20 28 28 69 53 65 63 74 6f =0 || ((iSecto
11b56 72 53 69 7a 65 2d 31 29 26 69 53 65 63 74 6f 72 rSize-1)&iSector
11b57 53 69 7a 65 29 21 3d 30 20 0a 20 20 20 20 29 7b Size)!=0 . ){
11b58 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
11b59 20 65 69 74 68 65 72 20 74 68 65 20 70 61 67 65 either the page
11b5a 2d 73 69 7a 65 20 6f 72 20 73 65 63 74 6f 72 2d -size or sector-
11b5b 73 69 7a 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 size in the jour
11b5c 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 0a 20 nal-header is .
11b5d 20 20 20 20 20 2a 2a 20 69 6e 76 61 6c 69 64 2c ** invalid,
11b5e 20 74 68 65 6e 20 74 68 65 20 70 72 6f 63 65 73 then the proces
11b5f 73 20 74 68 61 74 20 77 72 6f 74 65 20 74 68 65 s that wrote the
11b60 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 journal-header
11b61 6d 75 73 74 20 68 61 76 65 20 0a 20 20 20 20 20 must have .
11b62 20 2a 2a 20 63 72 61 73 68 65 64 20 62 65 66 6f ** crashed befo
11b63 72 65 20 74 68 65 20 68 65 61 64 65 72 20 77 61 re the header wa
11b64 73 20 73 79 6e 63 65 64 2e 20 49 6e 20 74 68 69 s synced. In thi
11b65 73 20 63 61 73 65 20 73 74 6f 70 20 72 65 61 64 s case stop read
11b66 69 6e 67 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 ing . ** th
11b67 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 e journal file h
11b68 65 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ere.. */.
11b69 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
11b6a 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 E_DONE;. }..
11b6b 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 /* Update the
11b6c 20 70 61 67 65 2d 73 69 7a 65 20 74 6f 20 6d 61 page-size to ma
11b6d 74 63 68 20 74 68 65 20 76 61 6c 75 65 20 72 65 tch the value re
11b6e 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 ad from the jour
11b6f 6e 61 6c 2e 20 0a 20 20 20 20 2a 2a 20 55 73 65 nal. . ** Use
11b70 20 61 20 74 65 73 74 63 61 73 65 28 29 20 6d 61 a testcase() ma
11b71 63 72 6f 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 cro to make sure
11b72 20 74 68 61 74 20 6d 61 6c 6c 6f 63 20 66 61 69 that malloc fai
11b73 6c 75 72 65 20 77 69 74 68 69 6e 20 0a 20 20 20 lure within .
11b74 20 2a 2a 20 50 61 67 65 72 53 65 74 50 61 67 65 ** PagerSetPage
11b75 73 69 7a 65 28 29 20 69 73 20 74 65 73 74 65 64 size() is tested
11b76 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 50 61 .. */. iPa
11b77 67 65 53 69 7a 65 31 36 20 3d 20 28 75 31 36 29 geSize16 = (u16)
11b78 69 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 72 iPageSize;. r
11b79 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
11b7a 53 65 74 50 61 67 65 73 69 7a 65 28 70 50 61 67 SetPagesize(pPag
11b7b 65 72 2c 20 26 69 50 61 67 65 53 69 7a 65 31 36 er, &iPageSize16
11b7c 2c 20 2d 31 29 3b 0a 20 20 20 20 74 65 73 74 63 , -1);. testc
11b7d 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ase( rc!=SQLITE_
11b7e 4f 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 OK );. assert
11b7f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
11b80 7c 7c 20 69 50 61 67 65 53 69 7a 65 31 36 3d 3d || iPageSize16==
11b81 28 75 31 36 29 69 50 61 67 65 53 69 7a 65 20 29 (u16)iPageSize )
11b82 3b 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65 ;.. /* Update
11b83 20 74 68 65 20 61 73 73 75 6d 65 64 20 73 65 63 the assumed sec
11b84 74 6f 72 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 tor-size to matc
11b85 68 20 74 68 65 20 76 61 6c 75 65 20 75 73 65 64 h the value used
11b86 20 62 79 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 by . ** the
11b87 70 72 6f 63 65 73 73 20 74 68 61 74 20 63 72 65 process that cre
11b88 61 74 65 64 20 74 68 69 73 20 6a 6f 75 72 6e 61 ated this journa
11b89 6c 2e 20 49 66 20 74 68 69 73 20 6a 6f 75 72 6e l. If this journ
11b8a 61 6c 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 72 al was. ** cr
11b8b 65 61 74 65 64 20 62 79 20 61 20 70 72 6f 63 65 eated by a proce
11b8c 73 73 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 ss other than th
11b8d 69 73 20 6f 6e 65 2c 20 74 68 65 6e 20 74 68 69 is one, then thi
11b8e 73 20 72 6f 75 74 69 6e 65 0a 20 20 20 20 2a 2a s routine. **
11b8f 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 is being called
11b90 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 61 67 from within pag
11b91 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 2e 20 54 er_playback(). T
11b92 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 0a 20 he local value.
11b93 20 20 20 2a 2a 20 6f 66 20 50 61 67 65 72 2e 73 ** of Pager.s
11b94 65 63 74 6f 72 53 69 7a 65 20 69 73 20 72 65 73 ectorSize is res
11b95 74 6f 72 65 64 20 61 74 20 74 68 65 20 65 6e 64 tored at the end
11b96 20 6f 66 20 74 68 61 74 20 72 6f 75 74 69 6e 65 of that routine
11b97 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 .. */. pPa
11b98 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 ger->sectorSize
11b99 3d 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 0a 20 = iSectorSize;.
11b9a 20 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f }.. pPager->jo
11b9b 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 4a 4f 55 52 urnalOff += JOUR
11b9c 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
11b9d 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b r);. return rc;
11b9e 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 .}.../*.** Write
11b9f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 6d 61 the supplied ma
11ba0 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster journal nam
11ba1 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e e into the journ
11ba2 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 al file for page
11ba3 72 0a 2a 2a 20 70 50 61 67 65 72 20 61 74 20 74 r.** pPager at t
11ba4 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 he current locat
11ba5 69 6f 6e 2e 20 54 68 65 20 6d 61 73 74 65 72 20 ion. The master
11ba6 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 6d 75 73 journal name mus
11ba7 74 20 62 65 20 74 68 65 20 6c 61 73 74 0a 2a 2a t be the last.**
11ba8 20 74 68 69 6e 67 20 77 72 69 74 74 65 6e 20 74 thing written t
11ba9 6f 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 o a journal file
11baa 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 . If the pager i
11bab 73 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d s in full-sync m
11bac 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 ode, the.** jour
11bad 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 nal file descrip
11bae 74 6f 72 20 69 73 20 61 64 76 61 6e 63 65 64 20 tor is advanced
11baf 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65 63 74 to the next sect
11bb0 6f 72 20 62 6f 75 6e 64 61 72 79 20 62 65 66 6f or boundary befo
11bb1 72 65 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 69 re.** anything i
11bb2 73 20 77 72 69 74 74 65 6e 2e 20 54 68 65 20 66 s written. The f
11bb3 6f 72 6d 61 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 ormat is:.**.**
11bb4 20 20 2b 20 34 20 62 79 74 65 73 3a 20 50 41 47 + 4 bytes: PAG
11bb5 45 52 5f 4d 4a 5f 50 47 4e 4f 2e 0a 2a 2a 20 20 ER_MJ_PGNO..**
11bb6 20 2b 20 4e 20 62 79 74 65 73 3a 20 4d 61 73 74 + N bytes: Mast
11bb7 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e er journal filen
11bb8 61 6d 65 20 69 6e 20 75 74 66 2d 38 2e 0a 2a 2a ame in utf-8..**
11bb9 20 20 20 2b 20 34 20 62 79 74 65 73 3a 20 4e 20 + 4 bytes: N
11bba 28 6c 65 6e 67 74 68 20 6f 66 20 6d 61 73 74 65 (length of maste
11bbb 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 r journal name i
11bbc 6e 20 62 79 74 65 73 2c 20 6e 6f 20 6e 75 6c 2d n bytes, no nul-
11bbd 74 65 72 6d 69 6e 61 74 6f 72 29 2e 0a 2a 2a 20 terminator)..**
11bbe 20 20 2b 20 34 20 62 79 74 65 73 3a 20 4d 61 73 + 4 bytes: Mas
11bbf 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ter journal name
11bc0 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20 20 20 checksum..**
11bc1 2b 20 38 20 62 79 74 65 73 3a 20 61 4a 6f 75 72 + 8 bytes: aJour
11bc2 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 0a 2a nalMagic[]..**.*
11bc3 2a 20 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 * The master jou
11bc4 72 6e 61 6c 20 70 61 67 65 20 63 68 65 63 6b 73 rnal page checks
11bc5 75 6d 20 69 73 20 74 68 65 20 73 75 6d 20 6f 66 um is the sum of
11bc6 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68 the bytes in th
11bc7 65 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 e master.** jour
11bc8 6e 61 6c 20 6e 61 6d 65 2c 20 77 68 65 72 65 20 nal name, where
11bc9 65 61 63 68 20 62 79 74 65 20 69 73 20 69 6e 74 each byte is int
11bca 65 72 70 72 65 74 65 64 20 61 73 20 61 20 73 69 erpreted as a si
11bcb 67 6e 65 64 20 38 2d 62 69 74 20 69 6e 74 65 67 gned 8-bit integ
11bcc 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 4d 61 er..**.** If zMa
11bcd 73 74 65 72 20 69 73 20 61 20 4e 55 4c 4c 20 70 ster is a NULL p
11bce 6f 69 6e 74 65 72 20 28 6f 63 63 75 72 73 20 66 ointer (occurs f
11bcf 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 or a single data
11bd0 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e base transaction
11bd1 29 2c 20 0a 2a 2a 20 74 68 69 73 20 63 61 6c 6c ), .** this call
11bd2 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
11bd3 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 static int write
11bd4 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 50 61 MasterJournal(Pa
11bd5 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e ger *pPager, con
11bd6 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 st char *zMaster
11bd7 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
11bd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11bd9 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
11bda 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d code */. int nM
11bdb 61 73 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 aster;
11bdc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 /* Le
11bdd 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 7a ngth of string z
11bde 4d 61 73 74 65 72 20 2a 2f 0a 20 20 69 36 34 20 Master */. i64
11bdf 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 iHdrOff;
11be0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11be1 4f 66 66 73 65 74 20 6f 66 20 68 65 61 64 65 72 Offset of header
11be2 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 in journal file
11be3 20 2a 2f 0a 20 20 69 36 34 20 6a 72 6e 6c 53 69 */. i64 jrnlSi
11be4 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ze;
11be5 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
11be6 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f f journal file o
11be7 6e 20 64 69 73 6b 20 2a 2f 0a 20 20 75 33 32 20 n disk */. u32
11be8 63 6b 73 75 6d 20 3d 20 30 3b 20 20 20 20 20 20 cksum = 0;
11be9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11bea 43 68 65 63 6b 73 75 6d 20 6f 66 20 73 74 72 69 Checksum of stri
11beb 6e 67 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 0a 20 ng zMaster */..
11bec 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 7c 7c if( !zMaster ||
11bed 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
11bee 65 72 0a 20 20 20 7c 7c 20 70 50 61 67 65 72 2d er. || pPager-
11bef 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 >journalMode==PA
11bf0 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
11bf1 4d 45 4d 4f 52 59 20 0a 20 20 20 7c 7c 20 70 50 MEMORY . || pP
11bf2 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
11bf3 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
11bf4 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 29 7b 0a 20 MODE_OFF . ){.
11bf5 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
11bf6 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 _OK;. }. pPage
11bf7 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 31 r->setMaster = 1
11bf8 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 ;. assert( isOp
11bf9 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
11bfa 29 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 );.. /* Calcula
11bfb 74 65 20 74 68 65 20 6c 65 6e 67 74 68 20 69 6e te the length in
11bfc 20 62 79 74 65 73 20 61 6e 64 20 74 68 65 20 63 bytes and the c
11bfd 68 65 63 6b 73 75 6d 20 6f 66 20 7a 4d 61 73 74 hecksum of zMast
11bfe 65 72 20 2a 2f 0a 20 20 66 6f 72 28 6e 4d 61 73 er */. for(nMas
11bff 74 65 72 3d 30 3b 20 7a 4d 61 73 74 65 72 5b 6e ter=0; zMaster[n
11c00 4d 61 73 74 65 72 5d 3b 20 6e 4d 61 73 74 65 72 Master]; nMaster
11c01 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2b ++){. cksum +
11c02 3d 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 74 65 = zMaster[nMaste
11c03 72 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 r];. }.. /* If
11c04 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f in full-sync mo
11c05 64 65 2c 20 61 64 76 61 6e 63 65 20 74 6f 20 74 de, advance to t
11c06 68 65 20 6e 65 78 74 20 64 69 73 6b 20 73 65 63 he next disk sec
11c07 74 6f 72 20 62 65 66 6f 72 65 20 77 72 69 74 69 tor before writi
11c08 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d 61 73 74 ng. ** the mast
11c09 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e er journal name.
11c0a 20 54 68 69 73 20 69 73 20 69 6e 20 63 61 73 65 This is in case
11c0b 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 the previous pa
11c0c 67 65 20 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 ge written to.
11c0d 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 ** the journal h
11c0e 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
11c0f 73 79 6e 63 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 synced.. */. i
11c10 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 f( pPager->fullS
11c11 79 6e 63 20 29 7b 0a 20 20 20 20 70 50 61 67 65 ync ){. pPage
11c12 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
11c13 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 journalHdrOffset
11c14 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 (pPager);. }.
11c15 69 48 64 72 4f 66 66 20 3d 20 70 50 61 67 65 72 iHdrOff = pPager
11c16 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 ->journalOff;..
11c17 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6d 61 /* Write the ma
11c18 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 64 61 74 ster journal dat
11c19 61 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 a to the end of
11c1a 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11c1b 2e 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65 72 72 . If. ** an err
11c1c 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 or occurs, retur
11c1d 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 n the error code
11c1e 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a to the caller..
11c1f 20 20 2a 2f 0a 20 20 69 66 28 20 28 30 20 21 3d */. if( (0 !=
11c20 20 28 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 (rc = write32bi
11c21 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ts(pPager->jfd,
11c22 69 48 64 72 4f 66 66 2c 20 50 41 47 45 52 5f 4d iHdrOff, PAGER_M
11c23 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 29 29 J_PGNO(pPager)))
11c24 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 ). || (0 != (r
11c25 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
11c26 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 te(pPager->jfd,
11c27 7a 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74 65 72 zMaster, nMaster
11c28 2c 20 69 48 64 72 4f 66 66 2b 34 29 29 29 0a 20 , iHdrOff+4))).
11c29 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d || (0 != (rc =
11c2a 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 write32bits(pPa
11c2b 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 ger->jfd, iHdrOf
11c2c 66 2b 34 2b 6e 4d 61 73 74 65 72 2c 20 6e 4d 61 f+4+nMaster, nMa
11c2d 73 74 65 72 29 29 29 0a 20 20 20 7c 7c 20 28 30 ster))). || (0
11c2e 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33 != (rc = write3
11c2f 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 2bits(pPager->jf
11c30 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 d, iHdrOff+4+nMa
11c31 73 74 65 72 2b 34 2c 20 63 6b 73 75 6d 29 29 29 ster+4, cksum)))
11c32 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 . || (0 != (rc
11c33 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
11c34 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61 e(pPager->jfd, a
11c35 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38 2c JournalMagic, 8,
11c36 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 iHdrOff+4+nMast
11c37 65 72 2b 38 29 29 29 0a 20 20 29 7b 0a 20 20 20 er+8))). ){.
11c38 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
11c39 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
11c3a 6c 4f 66 66 20 2b 3d 20 28 6e 4d 61 73 74 65 72 lOff += (nMaster
11c3b 2b 32 30 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e +20);. pPager->
11c3c 6e 65 65 64 53 79 6e 63 20 3d 20 21 70 50 61 67 needSync = !pPag
11c3d 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 0a 20 20 2f er->noSync;.. /
11c3e 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 * If the pager i
11c3f 73 20 69 6e 20 70 65 72 69 73 74 65 6e 74 2d 6a s in peristent-j
11c40 6f 75 72 6e 61 6c 20 6d 6f 64 65 2c 20 74 68 65 ournal mode, the
11c41 6e 20 74 68 65 20 70 68 79 73 69 63 61 6c 20 0a n the physical .
11c42 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c ** journal-fil
11c43 65 20 6d 61 79 20 65 78 74 65 6e 64 20 70 61 73 e may extend pas
11c44 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
11c45 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 master-journal
11c46 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 38 20 name. ** and 8
11c47 62 79 74 65 73 20 6f 66 20 6d 61 67 69 63 20 64 bytes of magic d
11c48 61 74 61 20 6a 75 73 74 20 77 72 69 74 74 65 6e ata just written
11c49 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 54 68 to the file. Th
11c4a 69 73 20 69 73 20 0a 20 20 2a 2a 20 64 61 6e 67 is is . ** dang
11c4b 65 72 6f 75 73 20 62 65 63 61 75 73 65 20 74 68 erous because th
11c4c 65 20 63 6f 64 65 20 74 6f 20 72 6f 6c 6c 62 61 e code to rollba
11c4d 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c ck a hot-journal
11c4e 20 66 69 6c 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 file. ** will
11c4f 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 66 not be able to f
11c50 69 6e 64 20 74 68 65 20 6d 61 73 74 65 72 2d 6a ind the master-j
11c51 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 74 6f 20 64 ournal name to d
11c52 65 74 65 72 6d 69 6e 65 20 0a 20 20 2a 2a 20 77 etermine . ** w
11c53 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
11c54 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 68 6f 74 e journal is hot
11c55 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 73 . . **. ** Eas
11c56 69 65 73 74 20 74 68 69 6e 67 20 74 6f 20 64 6f iest thing to do
11c57 20 69 6e 20 74 68 69 73 20 73 63 65 6e 61 72 69 in this scenari
11c58 6f 20 69 73 20 74 6f 20 74 72 75 6e 63 61 74 65 o is to truncate
11c59 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 the journal .
11c5a 2a 2a 20 66 69 6c 65 20 74 6f 20 74 68 65 20 72 ** file to the r
11c5b 65 71 75 69 72 65 64 20 73 69 7a 65 2e 0a 20 20 equired size..
11c5c 2a 2f 20 0a 20 20 69 66 28 20 53 51 4c 49 54 45 */ . if( SQLITE
11c5d 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 _OK==(rc = sqlit
11c5e 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 e3OsFileSize(pPa
11c5f 67 65 72 2d 3e 6a 66 64 2c 20 26 6a 72 6e 6c 53 ger->jfd, &jrnlS
11c60 69 7a 65 29 29 0a 20 20 20 26 26 20 6a 72 6e 6c ize)). && jrnl
11c61 53 69 7a 65 3e 70 50 61 67 65 72 2d 3e 6a 6f 75 Size>pPager->jou
11c62 72 6e 61 6c 4f 66 66 0a 20 20 29 7b 0a 20 20 20 rnalOff. ){.
11c63 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 rc = sqlite3OsT
11c64 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e runcate(pPager->
11c65 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 jfd, pPager->jou
11c66 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 7d 0a 20 20 rnalOff);. }.
11c67 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
11c68 0a 2a 2a 20 46 69 6e 64 20 61 20 70 61 67 65 20 .** Find a page
11c69 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c in the hash tabl
11c6a 65 20 67 69 76 65 6e 20 69 74 73 20 70 61 67 65 e given its page
11c6b 20 6e 75 6d 62 65 72 2e 20 52 65 74 75 72 6e 0a number. Return.
11c6c 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** a pointer to
11c6d 74 68 65 20 70 61 67 65 20 6f 72 20 4e 55 4c 4c the page or NULL
11c6e 20 69 66 20 74 68 65 20 72 65 71 75 65 73 74 65 if the requeste
11c6f 64 20 70 61 67 65 20 69 73 20 6e 6f 74 20 0a 2a d page is not .*
11c70 2a 20 61 6c 72 65 61 64 79 20 69 6e 20 6d 65 6d * already in mem
11c71 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 ory..*/.static P
11c72 67 48 64 72 20 2a 70 61 67 65 72 5f 6c 6f 6f 6b gHdr *pager_look
11c73 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 up(Pager *pPager
11c74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 , Pgno pgno){.
11c75 50 67 48 64 72 20 2a 70 3b 20 20 20 20 20 20 20 PgHdr *p;
11c76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11c77 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 /* Return valu
11c78 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 74 20 69 73 e */.. /* It is
11c79 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f not possible fo
11c7a 72 20 61 20 63 61 6c 6c 20 74 6f 20 50 63 61 63 r a call to Pcac
11c7b 68 65 46 65 74 63 68 28 29 20 77 69 74 68 20 63 heFetch() with c
11c7c 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 74 6f 0a reateFlag==0 to.
11c7d 20 20 2a 2a 20 66 61 69 6c 2c 20 73 69 6e 63 65 ** fail, since
11c7e 20 6e 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 61 no attempt to a
11c7f 6c 6c 6f 63 61 74 65 20 64 79 6e 61 6d 69 63 20 llocate dynamic
11c80 6d 65 6d 6f 72 79 20 77 69 6c 6c 20 62 65 20 6d memory will be m
11c81 61 64 65 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 ade.. */. (voi
11c82 64 29 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 d)sqlite3PcacheF
11c83 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 etch(pPager->pPC
11c84 61 63 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 ache, pgno, 0, &
11c85 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a p);. return p;.
11c86 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 }../*.** Unless
11c87 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
11c88 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 64 69 73 error-state, dis
11c89 63 61 72 64 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f card all in-memo
11c8a 72 79 20 70 61 67 65 73 2e 20 49 66 0a 2a 2a 20 ry pages. If.**
11c8b 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
11c8c 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 74 68 65 error-state, the
11c8d 6e 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 n this call is a
11c8e 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 4f no-op..**.** TO
11c8f 44 4f 3a 20 57 68 79 20 63 61 6e 20 77 65 20 6e DO: Why can we n
11c90 6f 74 20 72 65 73 65 74 20 74 68 65 20 70 61 67 ot reset the pag
11c91 65 72 20 77 68 69 6c 65 20 69 6e 20 65 72 72 6f er while in erro
11c92 72 20 73 74 61 74 65 3f 0a 2a 2f 0a 73 74 61 74 r state?.*/.stat
11c93 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 72 65 ic void pager_re
11c94 73 65 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 set(Pager *pPage
11c95 72 29 7b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 r){. if( SQLITE
11c96 5f 4f 4b 3d 3d 70 50 61 67 65 72 2d 3e 65 72 72 _OK==pPager->err
11c97 43 6f 64 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 Code ){. sqli
11c98 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 te3BackupRestart
11c99 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 (pPager->pBackup
11c9a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 );. sqlite3Pc
11c9b 61 63 68 65 43 6c 65 61 72 28 70 50 61 67 65 72 acheClear(pPager
11c9c 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 20 20 ->pPCache);.
11c9d 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 pPager->dbSizeVa
11c9e 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a lid = 0;. }.}..
11c9f 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20 73 /*.** Free all s
11ca0 74 72 75 63 74 75 72 65 73 20 69 6e 20 74 68 65 tructures in the
11ca1 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e Pager.aSavepoin
11ca2 74 5b 5d 20 61 72 72 61 79 20 61 6e 64 20 73 65 t[] array and se
11ca3 74 20 62 6f 74 68 0a 2a 2a 20 50 61 67 65 72 2e t both.** Pager.
11ca4 61 53 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 50 aSavepoint and P
11ca5 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74 20 ager.nSavepoint
11ca6 74 6f 20 7a 65 72 6f 2e 20 43 6c 6f 73 65 20 74 to zero. Close t
11ca7 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a he sub-journal.*
11ca8 2a 20 69 66 20 69 74 20 69 73 20 6f 70 65 6e 20 * if it is open
11ca9 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 69 73 and the pager is
11caa 20 6e 6f 74 20 69 6e 20 65 78 63 6c 75 73 69 76 not in exclusiv
11cab 65 20 6d 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 e mode..*/.stati
11cac 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 41 6c c void releaseAl
11cad 6c 53 61 76 65 70 6f 69 6e 74 73 28 50 61 67 65 lSavepoints(Page
11cae 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e r *pPager){. in
11caf 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 t ii;
11cb0 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 /* Iterator
11cb1 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f for looping thro
11cb2 75 67 68 20 50 61 67 65 72 2e 61 53 61 76 65 70 ugh Pager.aSavep
11cb3 6f 69 6e 74 20 2a 2f 0a 20 20 66 6f 72 28 69 69 oint */. for(ii
11cb4 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e =0; ii<pPager->n
11cb5 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 Savepoint; ii++)
11cb6 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 {. sqlite3Bit
11cb7 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 vecDestroy(pPage
11cb8 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 r->aSavepoint[ii
11cb9 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 29 3b ].pInSavepoint);
11cba 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 . }. if( !pPag
11cbb 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 er->exclusiveMod
11cbc 65 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4d 65 e || sqlite3IsMe
11cbd 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d mJournal(pPager-
11cbe 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 73 71 >sjfd) ){. sq
11cbf 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 lite3OsClose(pPa
11cc0 67 65 72 2d 3e 73 6a 66 64 29 3b 0a 20 20 7d 0a ger->sjfd);. }.
11cc1 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
11cc2 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e Pager->aSavepoin
11cc3 74 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 61 53 t);. pPager->aS
11cc4 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 avepoint = 0;.
11cc5 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 pPager->nSavepoi
11cc6 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 nt = 0;. pPager
11cc7 2d 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b 0a 7d ->nSubRec = 0;.}
11cc8 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
11cc9 62 69 74 20 6e 75 6d 62 65 72 20 70 67 6e 6f 20 bit number pgno
11cca 69 6e 20 74 68 65 20 50 61 67 65 72 53 61 76 65 in the PagerSave
11ccb 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 point.pInSavepoi
11ccc 6e 74 20 0a 2a 2a 20 62 69 74 76 65 63 73 20 6f nt .** bitvecs o
11ccd 66 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76 65 70 f all open savep
11cce 6f 69 6e 74 73 2e 20 52 65 74 75 72 6e 20 53 51 oints. Return SQ
11ccf 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63 65 LITE_OK if succe
11cd0 73 73 66 75 6c 0a 2a 2a 20 6f 72 20 53 51 4c 49 ssful.** or SQLI
11cd1 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 61 TE_NOMEM if a ma
11cd2 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 lloc failure occ
11cd3 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 urs..*/.static i
11cd4 6e 74 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e nt addToSavepoin
11cd5 74 42 69 74 76 65 63 73 28 50 61 67 65 72 20 2a tBitvecs(Pager *
11cd6 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e pPager, Pgno pgn
11cd7 6f 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 o){. int ii;
11cd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11cd9 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
11cda 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 */. int rc = SQ
11cdb 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 2f LITE_OK; /
11cdc 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f * Result code */
11cdd 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 .. for(ii=0; ii
11cde 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f <pPager->nSavepo
11cdf 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 int; ii++){.
11ce0 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a PagerSavepoint *
11ce1 70 20 3d 20 26 70 50 61 67 65 72 2d 3e 61 53 61 p = &pPager->aSa
11ce2 76 65 70 6f 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 vepoint[ii];.
11ce3 20 69 66 28 20 70 67 6e 6f 3c 3d 70 2d 3e 6e 4f if( pgno<=p->nO
11ce4 72 69 67 20 29 7b 0a 20 20 20 20 20 20 72 63 20 rig ){. rc
11ce5 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 |= sqlite3Bitvec
11ce6 53 65 74 28 70 2d 3e 70 49 6e 53 61 76 65 70 6f Set(p->pInSavepo
11ce7 69 6e 74 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 int, pgno);.
11ce8 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d testcase( rc==
11ce9 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a SQLITE_NOMEM );.
11cea 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
11ceb 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 ==SQLITE_OK || r
11cec 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
11ced 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 );. }. }. r
11cee 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
11cef 2a 2a 20 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61 ** Unlock the da
11cf0 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 tabase file. Thi
11cf1 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 s function is a
11cf2 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 70 61 67 no-op if the pag
11cf3 65 72 0a 2a 2a 20 69 73 20 69 6e 20 65 78 63 6c er.** is in excl
11cf4 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a usive mode..**.*
11cf5 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 * If the pager i
11cf6 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 65 s currently in e
11cf7 72 72 6f 72 20 73 74 61 74 65 2c 20 64 69 73 63 rror state, disc
11cf8 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ard the contents
11cf9 20 6f 66 20 0a 2a 2a 20 74 68 65 20 63 61 63 68 of .** the cach
11cfa 65 20 61 6e 64 20 72 65 73 65 74 20 74 68 65 20 e and reset the
11cfb 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 Pager structure
11cfc 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 2e 20 internal state.
11cfd 49 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 61 If there is.** a
11cfe 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 2d 66 n open journal-f
11cff 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 6e 65 ile, then the ne
11d00 78 74 20 74 69 6d 65 20 61 20 73 68 61 72 65 64 xt time a shared
11d01 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 -lock is obtaine
11d02 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 70 61 67 65 d.** on the page
11d03 72 20 66 69 6c 65 20 28 62 79 20 74 68 69 73 20 r file (by this
11d04 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f or any other pro
11d05 63 65 73 73 29 2c 20 69 74 20 77 69 6c 6c 20 62 cess), it will b
11d06 65 0a 2a 2a 20 74 72 65 61 74 65 64 20 61 73 20 e.** treated as
11d07 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e a hot-journal an
11d08 64 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a d rolled back..*
11d09 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 /.static void pa
11d0a 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 61 67 65 72 ger_unlock(Pager
11d0b 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 *pPager){. if(
11d0c 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 !pPager->exclus
11d0d 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 69 iveMode ){. i
11d0e 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
11d0f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
11d10 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 eturn code */..
11d11 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 63 6c 6f /* Always clo
11d12 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 se the journal f
11d13 69 6c 65 20 77 68 65 6e 20 64 72 6f 70 70 69 6e ile when droppin
11d14 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c g the database l
11d15 6f 63 6b 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 ock.. ** Othe
11d16 72 77 69 73 65 2c 20 61 6e 6f 74 68 65 72 20 63 rwise, another c
11d17 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 6a onnection with j
11d18 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 64 65 6c 65 ournal_mode=dele
11d19 74 65 20 6d 69 67 68 74 0a 20 20 20 20 2a 2a 20 te might. **
11d1a 64 65 6c 65 74 65 20 74 68 65 20 66 69 6c 65 20 delete the file
11d1b 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 75 out from under u
11d1c 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 s.. */. sq
11d1d 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 lite3OsClose(pPa
11d1e 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 73 ger->jfd);. s
11d1f 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 qlite3BitvecDest
11d20 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a roy(pPager->pInJ
11d21 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 70 50 61 ournal);. pPa
11d22 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 ger->pInJournal
11d23 3d 20 30 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 = 0;. release
11d24 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50 AllSavepoints(pP
11d25 61 67 65 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 ager);.. /* I
11d26 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 75 6e f the file is un
11d27 6c 6f 63 6b 65 64 2c 20 73 6f 6d 65 62 6f 64 79 locked, somebody
11d28 20 65 6c 73 65 20 6d 69 67 68 74 20 63 68 61 6e else might chan
11d29 67 65 20 69 74 2e 20 54 68 65 0a 20 20 20 20 2a ge it. The. *
11d2a 2a 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20 * values stored
11d2b 69 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 20 in Pager.dbSize
11d2c 65 74 63 2e 20 6d 69 67 68 74 20 62 65 63 6f 6d etc. might becom
11d2d 65 20 69 6e 76 61 6c 69 64 20 69 66 0a 20 20 20 e invalid if.
11d2e 20 2a 2a 20 74 68 69 73 20 68 61 70 70 65 6e 73 ** this happens
11d2f 2e 20 54 4f 44 4f 3a 20 52 65 61 6c 6c 79 2c 20 . TODO: Really,
11d30 74 68 69 73 20 64 6f 65 73 6e 27 74 20 6e 65 65 this doesn't nee
11d31 64 20 74 6f 20 62 65 20 63 6c 65 61 72 65 64 0a d to be cleared.
11d32 20 20 20 20 2a 2a 20 75 6e 74 69 6c 20 74 68 65 ** until the
11d33 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 change-counter
11d34 63 68 65 63 6b 20 66 61 69 6c 73 20 69 6e 20 50 check fails in P
11d35 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 29 agerSharedLock()
11d36 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 .. */. pPa
11d37 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
11d38 20 3d 20 30 3b 0a 0a 20 20 20 20 72 63 20 3d 20 = 0;.. rc =
11d39 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d osUnlock(pPager-
11d3a 3e 66 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 >fd, NO_LOCK);.
11d3b 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
11d3c 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f pPager->errCo
11d3d 64 65 20 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20 de = rc;. }.
11d3e 20 20 20 49 4f 54 52 41 43 45 28 28 22 55 4e 4c IOTRACE(("UNL
11d3f 4f 43 4b 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 OCK %p\n", pPage
11d40 72 29 29 0a 0a 20 20 20 20 2f 2a 20 49 66 20 50 r)).. /* If P
11d41 61 67 65 72 2e 65 72 72 43 6f 64 65 20 69 73 20 ager.errCode is
11d42 73 65 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 set, the content
11d43 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 63 s of the pager c
11d44 61 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 0a 20 ache cannot be.
11d45 20 20 20 2a 2a 20 74 72 75 73 74 65 64 2e 20 4e ** trusted. N
11d46 6f 77 20 74 68 61 74 20 74 68 65 20 70 61 67 65 ow that the page
11d47 72 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b r file is unlock
11d48 65 64 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ed, the contents
11d49 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 of the. ** c
11d4a 61 63 68 65 20 63 61 6e 20 62 65 20 64 69 73 63 ache can be disc
11d4b 61 72 64 65 64 20 61 6e 64 20 74 68 65 20 65 72 arded and the er
11d4c 72 6f 72 20 63 6f 64 65 20 73 61 66 65 6c 79 20 ror code safely
11d4d 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a cleared.. */.
11d4e 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
11d4f 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 errCode ){.
11d50 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
11d51 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 OK ){. pP
11d52 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 ager->errCode =
11d53 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
11d54 20 7d 0a 20 20 20 20 20 20 70 61 67 65 72 5f 72 }. pager_r
11d55 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 eset(pPager);.
11d56 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 72 2d }.. pPager-
11d57 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 >changeCountDone
11d58 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 = 0;. pPager
11d59 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f ->state = PAGER_
11d5a 55 4e 4c 4f 43 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f UNLOCK;. }.}../
11d5b 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
11d5c 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c on should be cal
11d5d 6c 65 64 20 77 68 65 6e 20 61 6e 20 49 4f 45 52 led when an IOER
11d5e 52 2c 20 43 4f 52 52 55 50 54 20 6f 72 20 46 55 R, CORRUPT or FU
11d5f 4c 4c 20 65 72 72 6f 72 0a 2a 2a 20 6d 61 79 20 LL error.** may
11d60 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e 20 54 have occurred. T
11d61 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
11d62 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 t is a pointer t
11d63 6f 20 74 68 65 20 70 61 67 65 72 20 0a 2a 2a 20 o the pager .**
11d64 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 20 73 structure, the s
11d65 65 63 6f 6e 64 20 74 68 65 20 65 72 72 6f 72 2d econd the error-
11d66 63 6f 64 65 20 61 62 6f 75 74 20 74 6f 20 62 65 code about to be
11d67 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 20 70 returned by a p
11d68 61 67 65 72 20 0a 2a 2a 20 41 50 49 20 66 75 6e ager .** API fun
11d69 63 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c 75 65 ction. The value
11d6a 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 20 63 returned is a c
11d6b 6f 70 79 20 6f 66 20 74 68 65 20 73 65 63 6f 6e opy of the secon
11d6c 64 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 74 d argument .** t
11d6d 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e o this function.
11d6e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 .**.** If the s
11d6f 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
11d70 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 s SQLITE_IOERR,
11d71 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2c 20 SQLITE_CORRUPT,
11d72 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 2a or SQLITE_FULL.*
11d73 2a 20 74 68 65 20 65 72 72 6f 72 20 62 65 63 6f * the error beco
11d74 6d 65 73 20 70 65 72 73 69 73 74 65 6e 74 2e 20 mes persistent.
11d75 55 6e 74 69 6c 20 74 68 65 20 70 65 72 73 69 73 Until the persis
11d76 74 65 6e 20 65 72 72 6f 72 20 69 73 20 63 6c 65 ten error is cle
11d77 61 72 65 64 2c 0a 2a 2a 20 73 75 62 73 65 71 75 ared,.** subsequ
11d78 65 6e 74 20 41 50 49 20 63 61 6c 6c 73 20 6f 6e ent API calls on
11d79 20 74 68 69 73 20 50 61 67 65 72 20 77 69 6c 6c this Pager will
11d7a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 72 65 74 immediately ret
11d7b 75 72 6e 20 74 68 65 20 73 61 6d 65 20 0a 2a 2a urn the same .**
11d7c 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a error code..**.
11d7d 2a 2a 20 41 20 70 65 72 73 69 73 74 65 6e 74 20 ** A persistent
11d7e 65 72 72 6f 72 20 69 6e 64 69 63 61 74 65 73 20 error indicates
11d7f 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 that the content
11d80 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 s of the pager-c
11d81 61 63 68 65 20 0a 2a 2a 20 63 61 6e 6e 6f 74 20 ache .** cannot
11d82 62 65 20 74 72 75 73 74 65 64 2e 20 54 68 69 73 be trusted. This
11d83 20 73 74 61 74 65 20 63 61 6e 20 62 65 20 63 6c state can be cl
11d84 65 61 72 65 64 20 62 79 20 63 6f 6d 70 6c 65 74 eared by complet
11d85 65 6c 79 20 64 69 73 63 61 72 64 69 6e 67 20 0a ely discarding .
11d86 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 ** the contents
11d87 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 of the pager-cac
11d88 68 65 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 he. If a transac
11d89 74 69 6f 6e 20 77 61 73 20 61 63 74 69 76 65 20 tion was active
11d8a 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 65 72 73 when.** the pers
11d8b 69 73 74 65 6e 74 20 65 72 72 6f 72 20 6f 63 63 istent error occ
11d8c 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20 urred, then the
11d8d 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
11d8e 20 6d 61 79 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 may need.** to
11d8f 62 65 20 72 65 70 6c 61 79 65 64 20 74 6f 20 72 be replayed to r
11d90 65 73 74 6f 72 65 20 74 68 65 20 63 6f 6e 74 65 estore the conte
11d91 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 nts of the datab
11d92 61 73 65 20 66 69 6c 65 20 28 61 73 20 69 66 0a ase file (as if.
11d93 2a 2a 20 69 74 20 77 65 72 65 20 61 20 68 6f 74 ** it were a hot
11d94 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2f 0a 73 74 -journal)..*/.st
11d95 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 65 atic int pager_e
11d96 72 72 6f 72 28 50 61 67 65 72 20 2a 70 50 61 67 rror(Pager *pPag
11d97 65 72 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20 69 er, int rc){. i
11d98 6e 74 20 72 63 32 20 3d 20 72 63 20 26 20 30 78 nt rc2 = rc & 0x
11d99 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 ff;. assert( rc
11d9a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 ==SQLITE_OK || !
11d9b 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73 73 65 72 MEMDB );. asser
11d9c 74 28 0a 20 20 20 20 20 20 20 70 50 61 67 65 72 t(. pPager
11d9d 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 ->errCode==SQLIT
11d9e 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20 20 20 20 E_FULL ||.
11d9f 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
11da0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 0a 20 ==SQLITE_OK ||.
11da1 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 65 (pPager->e
11da2 72 72 43 6f 64 65 20 26 20 30 78 66 66 29 3d 3d rrCode & 0xff)==
11da3 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 29 SQLITE_IOERR. )
11da4 3b 0a 20 20 69 66 28 20 72 63 32 3d 3d 53 51 4c ;. if( rc2==SQL
11da5 49 54 45 5f 46 55 4c 4c 20 7c 7c 20 72 63 32 3d ITE_FULL || rc2=
11da6 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b =SQLITE_IOERR ){
11da7 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 . pPager->err
11da8 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 7d 0a 20 Code = rc;. }.
11da9 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
11daa 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 20 72 *.** Execute a r
11dab 6f 6c 6c 62 61 63 6b 20 69 66 20 61 20 74 72 61 ollback if a tra
11dac 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 nsaction is acti
11dad 76 65 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 ve and unlock th
11dae 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 e .** database f
11daf 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ile. .**.** If t
11db0 68 65 20 70 61 67 65 72 20 68 61 73 20 61 6c 72 he pager has alr
11db1 65 61 64 79 20 65 6e 74 65 72 65 64 20 74 68 65 eady entered the
11db2 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 6f error state, do
11db3 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 0a 2a 2a not attempt .**
11db4 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 61 74 the rollback at
11db5 20 74 68 69 73 20 74 69 6d 65 2e 20 49 6e 73 74 this time. Inst
11db6 65 61 64 2c 20 70 61 67 65 72 5f 75 6e 6c 6f 63 ead, pager_unloc
11db7 6b 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 54 k() is called. T
11db8 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 70 61 he.** call to pa
11db9 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 77 69 6c ger_unlock() wil
11dba 6c 20 64 69 73 63 61 72 64 20 61 6c 6c 20 69 6e l discard all in
11dbb 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 2c 20 75 -memory pages, u
11dbc 6e 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20 64 61 74 nlock.** the dat
11dbd 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 63 abase file and c
11dbe 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 73 lear the error s
11dbf 74 61 74 65 2e 20 49 66 20 74 68 69 73 20 6d 65 tate. If this me
11dc0 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 72 ans that.** ther
11dc1 65 20 69 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e e is a hot-journ
11dc2 61 6c 20 6c 65 66 74 20 69 6e 20 74 68 65 20 66 al left in the f
11dc3 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 65 20 ile-system, the
11dc4 6e 65 78 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a next connection.
11dc5 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 73 ** to obtain a s
11dc6 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 hared lock on th
11dc7 65 20 70 61 67 65 72 20 28 77 68 69 63 68 20 6d e pager (which m
11dc8 61 79 20 62 65 20 74 68 69 73 20 6f 6e 65 29 20 ay be this one)
11dc9 77 69 6c 6c 0a 2a 2a 20 72 6f 6c 6c 20 69 74 20 will.** roll it
11dca 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 back..**.** If t
11dcb 68 65 20 70 61 67 65 72 20 68 61 73 20 6e 6f 74 he pager has not
11dcc 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64 already entered
11dcd 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
11dce 2c 20 62 75 74 20 61 6e 20 49 4f 20 6f 72 0a 2a , but an IO or.*
11dcf 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 6f * malloc error o
11dd0 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 20 72 ccurs during a r
11dd1 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20 74 68 ollback, then th
11dd2 69 73 20 77 69 6c 6c 20 69 74 73 65 6c 66 20 63 is will itself c
11dd3 61 75 73 65 20 0a 2a 2a 20 74 68 65 20 70 61 67 ause .** the pag
11dd4 65 72 20 74 6f 20 65 6e 74 65 72 20 74 68 65 20 er to enter the
11dd5 65 72 72 6f 72 20 73 74 61 74 65 2e 20 57 68 69 error state. Whi
11dd6 63 68 20 77 69 6c 6c 20 62 65 20 63 6c 65 61 72 ch will be clear
11dd7 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c ed by the.** cal
11dd8 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c 6f 63 l to pager_unloc
11dd9 6b 28 29 2c 20 61 73 20 64 65 73 63 72 69 62 65 k(), as describe
11dda 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74 d above..*/.stat
11ddb 69 63 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c ic void pagerUnl
11ddc 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 50 ockAndRollback(P
11ddd 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
11dde 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 if( pPager->err
11ddf 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 Code==SQLITE_OK
11de0 26 26 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 && pPager->state
11de1 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 >=PAGER_RESERVED
11de2 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 ){. sqlite3B
11de3 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 eginBenignMalloc
11de4 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 ();. sqlite3P
11de5 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 50 61 agerRollback(pPa
11de6 67 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ger);. sqlite
11de7 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 3EndBenignMalloc
11de8 28 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65 72 5f ();. }. pager_
11de9 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a unlock(pPager);.
11dea 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
11deb 75 74 69 6e 65 20 65 6e 64 73 20 61 20 74 72 61 utine ends a tra
11dec 6e 73 61 63 74 69 6f 6e 2e 20 41 20 74 72 61 6e nsaction. A tran
11ded 73 61 63 74 69 6f 6e 20 69 73 20 75 73 75 61 6c saction is usual
11dee 6c 79 20 65 6e 64 65 64 20 62 79 20 0a 2a 2a 20 ly ended by .**
11def 65 69 74 68 65 72 20 61 20 43 4f 4d 4d 49 54 20 either a COMMIT
11df0 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b 20 6f 70 or a ROLLBACK op
11df1 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 20 72 6f eration. This ro
11df2 75 74 69 6e 65 20 6d 61 79 20 62 65 20 63 61 6c utine may be cal
11df3 6c 65 64 20 0a 2a 2a 20 61 66 74 65 72 20 72 6f led .** after ro
11df4 6c 6c 62 61 63 6b 20 6f 66 20 61 20 68 6f 74 2d llback of a hot-
11df5 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 69 66 20 61 journal, or if a
11df6 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 n error occurs w
11df7 68 69 6c 65 20 6f 70 65 6e 69 6e 67 0a 2a 2a 20 hile opening.**
11df8 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11df9 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68 65 20 or writing the
11dfa 76 65 72 79 20 66 69 72 73 74 20 6a 6f 75 72 6e very first journ
11dfb 61 6c 2d 68 65 61 64 65 72 20 6f 66 20 61 0a 2a al-header of a.*
11dfc 2a 20 64 61 74 61 62 61 73 65 20 74 72 61 6e 73 * database trans
11dfd 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a 2a 2a 20 49 action..** .** I
11dfe 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 f the pager is i
11dff 6e 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 6f n PAGER_SHARED o
11e00 72 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 r PAGER_UNLOCK s
11e01 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 0a 2a tate when this.*
11e02 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c * routine is cal
11e03 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d led, it is a no-
11e04 6f 70 20 28 72 65 74 75 72 6e 73 20 53 51 4c 49 op (returns SQLI
11e05 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 TE_OK)..**.** Ot
11e06 68 65 72 77 69 73 65 2c 20 61 6e 79 20 61 63 74 herwise, any act
11e07 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 ive savepoints a
11e08 72 65 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a re released..**.
11e09 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 ** If the journa
11e0a 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 2c 20 l file is open,
11e0b 74 68 65 6e 20 69 74 20 69 73 20 22 66 69 6e 61 then it is "fina
11e0c 6c 69 7a 65 64 22 2e 20 4f 6e 63 65 20 61 20 6a lized". Once a j
11e0d 6f 75 72 6e 61 6c 20 0a 2a 2a 20 66 69 6c 65 20 ournal .** file
11e0e 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a has been finaliz
11e0f 65 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 ed it is not pos
11e10 73 69 62 6c 65 20 74 6f 20 75 73 65 20 69 74 20 sible to use it
11e11 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 61 20 0a to roll back a .
11e12 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 ** transaction.
11e13 4e 6f 72 20 77 69 6c 6c 20 69 74 20 62 65 20 63 Nor will it be c
11e14 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 onsidered to be
11e15 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 79 a hot-journal by
11e16 20 74 68 69 73 0a 2a 2a 20 6f 72 20 61 6e 79 20 this.** or any
11e17 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 other database c
11e18 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45 78 61 63 74 onnection. Exact
11e19 6c 79 20 68 6f 77 20 61 20 6a 6f 75 72 6e 61 6c ly how a journal
11e1a 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 0a 2a 2a is finalized.**
11e1b 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 65 74 depends on whet
11e1c 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 70 her or not the p
11e1d 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 ager is running
11e1e 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 in exclusive mod
11e1f 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63 75 72 e and.** the cur
11e20 72 65 6e 74 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 rent journal-mod
11e21 65 20 28 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c e (Pager.journal
11e22 4d 6f 64 65 20 76 61 6c 75 65 29 2c 20 61 73 20 Mode value), as
11e23 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 follows:.**.**
11e24 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 journalMode==ME
11e25 4d 4f 52 59 0a 2a 2a 20 20 20 20 20 4a 6f 75 72 MORY.** Jour
11e26 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 nal file descrip
11e27 74 6f 72 20 69 73 20 73 69 6d 70 6c 79 20 63 6c tor is simply cl
11e28 6f 73 65 64 2e 20 54 68 69 73 20 64 65 73 74 72 osed. This destr
11e29 6f 79 73 20 61 6e 20 0a 2a 2a 20 20 20 20 20 69 oys an .** i
11e2a 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c n-memory journal
11e2b 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 ..**.** journa
11e2c 6c 4d 6f 64 65 3d 3d 54 52 55 4e 43 41 54 45 0a lMode==TRUNCATE.
11e2d 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20 66 ** Journal f
11e2e 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65 64 ile is truncated
11e2f 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69 to zero bytes i
11e30 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 n size..**.**
11e31 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 45 52 journalMode==PER
11e32 53 49 53 54 0a 2a 2a 20 20 20 20 20 54 68 65 20 SIST.** The
11e33 66 69 72 73 74 20 32 38 20 62 79 74 65 73 20 6f first 28 bytes o
11e34 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
11e35 6c 65 20 61 72 65 20 7a 65 72 6f 65 64 2e 20 54 le are zeroed. T
11e36 68 69 73 20 69 6e 76 61 6c 69 64 61 74 65 73 0a his invalidates.
11e37 2a 2a 20 20 20 20 20 74 68 65 20 66 69 72 73 74 ** the first
11e38 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
11e39 69 6e 20 74 68 65 20 66 69 6c 65 2c 20 61 6e 64 in the file, and
11e3a 20 68 65 6e 63 65 20 74 68 65 20 65 6e 74 69 72 hence the entir
11e3b 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 e journal.**
11e3c 20 66 69 6c 65 2e 20 41 6e 20 69 6e 76 61 6c 69 file. An invali
11e3d 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 d journal file c
11e3e 61 6e 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 20 annot be rolled
11e3f 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f back..**.** jo
11e40 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44 45 4c 45 54 urnalMode==DELET
11e41 45 0a 2a 2a 20 20 20 20 20 54 68 65 20 6a 6f 75 E.** The jou
11e42 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 6c 6f rnal file is clo
11e43 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65 64 20 sed and deleted
11e44 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44 using sqlite3OsD
11e45 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 elete()..**.**
11e46 20 20 20 49 66 20 74 68 65 20 70 61 67 65 72 20 If the pager
11e47 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 is running in ex
11e48 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 clusive mode, th
11e49 69 73 20 6d 65 74 68 6f 64 20 6f 66 20 66 69 6e is method of fin
11e4a 61 6c 69 7a 69 6e 67 0a 2a 2a 20 20 20 20 20 74 alizing.** t
11e4b 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11e4c 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e 20 49 is never used. I
11e4d 6e 73 74 65 61 64 2c 20 69 66 20 74 68 65 20 6a nstead, if the j
11e4e 6f 75 72 6e 61 6c 4d 6f 64 65 20 69 73 0a 2a 2a ournalMode is.**
11e4f 20 20 20 20 20 44 45 4c 45 54 45 20 61 6e 64 20 DELETE and
11e50 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
11e51 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 exclusive mode,
11e52 74 68 65 20 6d 65 74 68 6f 64 20 64 65 73 63 72 the method descr
11e53 69 62 65 64 20 75 6e 64 65 72 0a 2a 2a 20 20 20 ibed under.**
11e54 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 journalMode==P
11e55 45 52 53 49 53 54 20 69 73 20 75 73 65 64 20 69 ERSIST is used i
11e56 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 66 nstead..**.** Af
11e57 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ter the journal
11e58 69 73 20 66 69 6e 61 6c 69 7a 65 64 2c 20 69 66 is finalized, if
11e59 20 72 75 6e 6e 69 6e 67 20 69 6e 20 6e 6f 6e 2d running in non-
11e5a 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 exclusive mode,
11e5b 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 6d 6f 76 the.** pager mov
11e5c 65 73 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 es to PAGER_SHAR
11e5d 45 44 20 73 74 61 74 65 20 28 61 6e 64 20 64 6f ED state (and do
11e5e 77 6e 67 72 61 64 65 73 20 74 68 65 20 6c 6f 63 wngrades the loc
11e5f 6b 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61 k on the.** data
11e60 62 61 73 65 20 66 69 6c 65 20 61 63 63 6f 72 64 base file accord
11e61 69 6e 67 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 ingly)..**.** If
11e62 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75 the pager is ru
11e63 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 nning in exclusi
11e64 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 73 20 69 ve mode and is i
11e65 6e 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 73 n PAGER_SYNCED s
11e66 74 61 74 65 2c 0a 2a 2a 20 69 74 20 6d 6f 76 65 tate,.** it move
11e67 73 20 74 6f 20 50 41 47 45 52 5f 45 58 43 4c 55 s to PAGER_EXCLU
11e68 53 49 56 45 2e 20 4e 6f 20 6c 6f 63 6b 73 20 61 SIVE. No locks a
11e69 72 65 20 64 6f 77 6e 67 72 61 64 65 64 20 77 68 re downgraded wh
11e6a 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 0a 2a 2a en running in.**
11e6b 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e exclusive mode.
11e6c 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
11e6d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
11e6e 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e no error occurs.
11e6f 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
11e70 75 72 73 20 64 75 72 69 6e 67 0a 2a 2a 20 61 6e urs during.** an
11e71 79 20 6f 66 20 74 68 65 20 49 4f 20 6f 70 65 72 y of the IO oper
11e72 61 74 69 6f 6e 73 20 74 6f 20 66 69 6e 61 6c 69 ations to finali
11e73 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ze the journal f
11e74 69 6c 65 20 6f 72 20 75 6e 6c 6f 63 6b 20 74 68 ile or unlock th
11e75 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 68 e.** database th
11e76 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 en the IO error
11e77 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
11e78 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 49 66 to the user. If
11e79 20 74 68 65 20 0a 2a 2a 20 6f 70 65 72 61 74 69 the .** operati
11e7a 6f 6e 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 on to finalize t
11e7b 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11e7c 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68 65 20 fails, then the
11e7d 63 6f 64 65 20 73 74 69 6c 6c 0a 2a 2a 20 74 72 code still.** tr
11e7e 69 65 73 20 74 6f 20 75 6e 6c 6f 63 6b 20 74 68 ies to unlock th
11e7f 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
11e80 69 66 20 6e 6f 74 20 69 6e 20 65 78 63 6c 75 73 if not in exclus
11e81 69 76 65 20 6d 6f 64 65 2e 20 49 66 20 74 68 65 ive mode. If the
11e82 0a 2a 2a 20 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 .** unlock opera
11e83 74 69 6f 6e 20 66 61 69 6c 73 20 61 73 20 77 65 tion fails as we
11e84 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 ll, then the fir
11e85 73 74 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65 st error code re
11e86 6c 61 74 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 lated.** to the
11e87 66 69 72 73 74 20 65 72 72 6f 72 20 65 6e 63 6f first error enco
11e88 75 6e 74 65 72 65 64 20 28 74 68 65 20 6a 6f 75 untered (the jou
11e89 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74 69 6f rnal finalizatio
11e8a 6e 20 6f 6e 65 29 20 69 73 0a 2a 2a 20 72 65 74 n one) is.** ret
11e8b 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 urned..*/.static
11e8c 20 69 6e 74 20 70 61 67 65 72 5f 65 6e 64 5f 74 int pager_end_t
11e8d 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 67 65 72 ransaction(Pager
11e8e 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 68 61 *pPager, int ha
11e8f 73 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 sMaster){. int
11e90 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
11e91 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f /* Error co
11e92 64 65 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 de from journal
11e93 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f 70 65 finalization ope
11e94 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 ration */. int
11e95 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc2 = SQLITE_OK;
11e96 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f /* Error co
11e97 64 65 20 66 72 6f 6d 20 64 62 20 66 69 6c 65 20 de from db file
11e98 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69 6f 6e unlock operation
11e99 20 2a 2f 0a 0a 20 20 69 66 28 20 70 50 61 67 65 */.. if( pPage
11e9a 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52 5f 52 r->state<PAGER_R
11e9b 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 72 ESERVED ){. r
11e9c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
11e9d 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 41 6c . }. releaseAl
11e9e 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50 61 67 lSavepoints(pPag
11e9f 65 72 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 er);.. assert(
11ea0 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
11ea1 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 70 fd) || pPager->p
11ea2 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a InJournal==0 );.
11ea3 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 if( isOpen(pPa
11ea4 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 0a 20 20 ger->jfd) ){..
11ea5 20 20 2f 2a 20 46 69 6e 61 6c 69 7a 65 20 74 68 /* Finalize th
11ea6 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 e journal file.
11ea7 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 */. if( sqlit
11ea8 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 e3IsMemJournal(p
11ea9 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 Pager->jfd) ){.
11eaa 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
11eab 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
11eac 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
11ead 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 20 20 ODE_MEMORY );.
11eae 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
11eaf 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b se(pPager->jfd);
11eb0 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 . }else if( p
11eb1 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
11eb2 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
11eb3 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 29 LMODE_TRUNCATE )
11eb4 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 {. if( pPag
11eb5 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d er->journalOff==
11eb6 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 0 ){. rc
11eb7 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
11eb8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
11eb9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
11eba 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d Truncate(pPager-
11ebb 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 >jfd, 0);.
11ebc 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e }. pPager->
11ebd 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a journalOff = 0;.
11ebe 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
11ebf 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30 urnalStarted = 0
11ec0 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
11ec1 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
11ec2 65 4d 6f 64 65 20 0a 20 20 20 20 20 7c 7c 20 70 eMode . || p
11ec3 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
11ec4 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
11ec5 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20 LMODE_PERSIST.
11ec6 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
11ec7 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 70 zeroJournalHdr(p
11ec8 50 61 67 65 72 2c 20 68 61 73 4d 61 73 74 65 72 Pager, hasMaster
11ec9 29 3b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 65 );. pager_e
11eca 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 rror(pPager, rc)
11ecb 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e ;. pPager->
11ecc 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a journalOff = 0;.
11ecd 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
11ece 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30 urnalStarted = 0
11ecf 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
11ed0 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 /* This branc
11ed1 68 20 6d 61 79 20 62 65 20 65 78 65 63 75 74 65 h may be execute
11ed2 64 20 77 69 74 68 20 50 61 67 65 72 2e 6a 6f 75 d with Pager.jou
11ed3 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 4d 4f 52 59 rnalMode==MEMORY
11ed4 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 61 20 68 if. ** a h
11ed5 6f 74 2d 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6a ot-journal was j
11ed6 75 73 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e ust rolled back.
11ed7 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
11ed8 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 e journal.
11ed9 2a 2a 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 ** file should b
11eda 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 64 65 6c e closed and del
11edb 65 74 65 64 2e 20 49 66 20 74 68 69 73 20 63 6f eted. If this co
11edc 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 73 20 nnection writes
11edd 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 to. ** the
11ede 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69 database file, i
11edf 74 20 77 69 6c 6c 20 64 6f 20 73 6f 20 75 73 69 t will do so usi
11ee0 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 ng an in-memory
11ee1 6a 6f 75 72 6e 61 6c 2e 20 20 2a 2f 0a 20 20 20 journal. */.
11ee2 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
11ee3 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d r->journalMode==
11ee4 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
11ee5 45 5f 44 45 4c 45 54 45 20 0a 20 20 20 20 20 20 E_DELETE .
11ee6 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e || pPager->
11ee7 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 journalMode==PAG
11ee8 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d ER_JOURNALMODE_M
11ee9 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 29 3b 0a EMORY . );.
11eea 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 sqlite3OsC
11eeb 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 lose(pPager->jfd
11eec 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 50 );. if( !pP
11eed 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 ager->tempFile )
11eee 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
11eef 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 qlite3OsDelete(p
11ef0 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 61 Pager->pVfs, pPa
11ef1 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 ger->zJournal, 0
11ef2 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
11ef3 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
11ef4 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 CHECK_PAGES.
11ef5 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 sqlite3PcacheIte
11ef6 72 61 74 65 44 69 72 74 79 28 70 50 61 67 65 72 rateDirty(pPager
11ef7 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 65 72 ->pPCache, pager
11ef8 5f 73 65 74 5f 70 61 67 65 68 61 73 68 29 3b 0a _set_pagehash);.
11ef9 23 65 6e 64 69 66 0a 0a 20 20 20 20 73 71 6c 69 #endif.. sqli
11efa 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c te3PcacheCleanAl
11efb 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 l(pPager->pPCach
11efc 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 e);. sqlite3B
11efd 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 itvecDestroy(pPa
11efe 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 ger->pInJournal)
11eff 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 ;. pPager->pI
11f00 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 nJournal = 0;.
11f01 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d pPager->nRec =
11f02 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 0;. }.. if( !
11f03 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
11f04 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 32 eMode ){. rc2
11f05 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 = osUnlock(pPag
11f06 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c er->fd, SHARED_L
11f07 4f 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65 72 OCK);. pPager
11f08 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f ->state = PAGER_
11f09 53 48 41 52 45 44 3b 0a 20 20 20 20 70 50 61 67 SHARED;. pPag
11f0a 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 er->changeCountD
11f0b 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 one = 0;. }else
11f0c 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 if( pPager->sta
11f0d 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 te==PAGER_SYNCED
11f0e 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
11f0f 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 state = PAGER_EX
11f10 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 20 70 CLUSIVE;. }. p
11f11 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 Pager->setMaster
11f12 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e = 0;. pPager->
11f13 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 needSync = 0;.
11f14 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 pPager->dbModifi
11f15 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 4f ed = 0;.. /* TO
11f16 44 4f 3a 20 49 73 20 74 68 69 73 20 6f 70 74 69 DO: Is this opti
11f17 6d 61 6c 3f 20 57 68 79 20 69 73 20 74 68 65 20 mal? Why is the
11f18 64 62 20 73 69 7a 65 20 69 6e 76 61 6c 69 64 61 db size invalida
11f19 74 65 64 20 68 65 72 65 20 0a 20 20 2a 2a 20 77 ted here . ** w
11f1a 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 hen the database
11f1b 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 75 6e 6c file is not unl
11f1c 6f 63 6b 65 64 3f 20 2a 2f 0a 20 20 70 50 61 67 ocked? */. pPag
11f1d 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d er->dbOrigSize =
11f1e 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 0;. sqlite3Pca
11f1f 63 68 65 54 72 75 6e 63 61 74 65 28 70 50 61 67 cheTruncate(pPag
11f20 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 50 61 er->pPCache, pPa
11f21 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 ger->dbSize);.
11f22 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 if( !MEMDB ){.
11f23 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
11f24 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a Valid = 0;. }..
11f25 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51 return (rc==SQ
11f26 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 29 3b LITE_OK?rc2:rc);
11f27 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 .}../*.** Parame
11f28 74 65 72 20 61 44 61 74 61 20 6d 75 73 74 20 70 ter aData must p
11f29 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 oint to a buffer
11f2a 20 6f 66 20 70 50 61 67 65 72 2d 3e 70 61 67 65 of pPager->page
11f2b 53 69 7a 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 Size bytes.** of
11f2c 20 64 61 74 61 2e 20 43 6f 6d 70 75 74 65 20 61 data. Compute a
11f2d 6e 64 20 72 65 74 75 72 6e 20 61 20 63 68 65 63 nd return a chec
11f2e 6b 73 75 6d 20 62 61 73 65 64 20 6f 6e 74 20 74 ksum based ont t
11f2f 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
11f30 68 65 20 0a 2a 2a 20 70 61 67 65 20 6f 66 20 64 he .** page of d
11f31 61 74 61 20 61 6e 64 20 74 68 65 20 63 75 72 72 ata and the curr
11f32 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 50 61 ent value of pPa
11f33 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 2e 0a ger->cksumInit..
11f34 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f **.** This is no
11f35 74 20 61 20 72 65 61 6c 20 63 68 65 63 6b 73 75 t a real checksu
11f36 6d 2e 20 49 74 20 69 73 20 72 65 61 6c 6c 79 20 m. It is really
11f37 6a 75 73 74 20 74 68 65 20 73 75 6d 20 6f 66 20 just the sum of
11f38 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 69 the .** random i
11f39 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 28 70 50 nitial value (pP
11f3a 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 ager->cksumInit)
11f3b 20 61 6e 64 20 65 76 65 72 79 20 32 30 30 74 68 and every 200th
11f3c 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 byte.** of the
11f3d 70 61 67 65 20 64 61 74 61 2c 20 73 74 61 72 74 page data, start
11f3e 69 6e 67 20 77 69 74 68 20 62 79 74 65 20 6f 66 ing with byte of
11f3f 66 73 65 74 20 28 70 50 61 67 65 72 2d 3e 70 61 fset (pPager->pa
11f40 67 65 53 69 7a 65 25 32 30 30 29 2e 0a 2a 2a 20 geSize%200)..**
11f41 45 61 63 68 20 62 79 74 65 20 69 73 20 69 6e 74 Each byte is int
11f42 65 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 38 erpreted as an 8
11f43 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 20 69 6e -bit unsigned in
11f44 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 teger..**.** Cha
11f45 6e 67 69 6e 67 20 74 68 65 20 66 6f 72 6d 75 6c nging the formul
11f46 61 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74 a used to comput
11f47 65 20 74 68 69 73 20 63 68 65 63 6b 73 75 6d 20 e this checksum
11f48 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a 2a 2a results in an.**
11f49 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 6a 6f incompatible jo
11f4a 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61 urnal file forma
11f4b 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6a 6f 75 72 t..**.** If jour
11f4c 6e 61 6c 20 63 6f 72 72 75 70 74 69 6f 6e 20 6f nal corruption o
11f4d 63 63 75 72 73 20 64 75 65 20 74 6f 20 61 20 70 ccurs due to a p
11f4e 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20 74 68 ower failure, th
11f4f 65 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 20 0a 2a e most likely .*
11f50 2a 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 68 * scenario is th
11f51 61 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20 74 68 at one end or th
11f52 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 72 e other of the r
11f53 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 20 63 68 ecord will be ch
11f54 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 74 20 69 73 anged. .** It is
11f55 20 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b 65 6c much less likel
11f56 79 20 74 68 61 74 20 74 68 65 20 74 77 6f 20 65 y that the two e
11f57 6e 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e nds of the journ
11f58 61 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62 al record will b
11f59 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 6e 64 e.** correct and
11f5a 20 74 68 65 20 6d 69 64 64 6c 65 20 62 65 20 63 the middle be c
11f5b 6f 72 72 75 70 74 2e 20 20 54 68 75 73 2c 20 74 orrupt. Thus, t
11f5c 68 69 73 20 22 63 68 65 63 6b 73 75 6d 22 20 73 his "checksum" s
11f5d 63 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75 67 68 cheme,.** though
11f5e 20 66 61 73 74 20 61 6e 64 20 73 69 6d 70 6c 65 fast and simple
11f5f 2c 20 63 61 74 63 68 65 73 20 74 68 65 20 6d 6f , catches the mo
11f60 73 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69 6e 64 stly likely kind
11f61 20 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a of corruption..
11f62 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70 61 */.static u32 pa
11f63 67 65 72 5f 63 6b 73 75 6d 28 50 61 67 65 72 20 ger_cksum(Pager
11f64 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 75 *pPager, const u
11f65 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 75 33 32 8 *aData){. u32
11f66 20 63 6b 73 75 6d 20 3d 20 70 50 61 67 65 72 2d cksum = pPager-
11f67 3e 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20 >cksumInit;
11f68 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 /* Checksum
11f69 76 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 value to return
11f6a 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 50 61 */. int i = pPa
11f6b 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30 ger->pageSize-20
11f6c 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 0; /* L
11f6d 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
11f6e 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 while( i>0 ){.
11f6f 20 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44 61 74 cksum += aDat
11f70 61 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d 20 32 a[i];. i -= 2
11f71 30 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 00;. }. return
11f72 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a cksum;.}../*.**
11f73 20 52 65 61 64 20 61 20 73 69 6e 67 6c 65 20 70 Read a single p
11f74 61 67 65 20 66 72 6f 6d 20 65 69 74 68 65 72 20 age from either
11f75 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11f76 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d (if isMainJrnl=
11f77 3d 31 29 20 6f 72 0a 2a 2a 20 66 72 6f 6d 20 74 =1) or.** from t
11f78 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 28 he sub-journal (
11f79 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 30 if isMainJrnl==0
11f7a 29 20 61 6e 64 20 70 6c 61 79 62 61 63 6b 20 74 ) and playback t
11f7b 68 61 74 20 70 61 67 65 2e 0a 2a 2a 20 54 68 65 hat page..** The
11f7c 20 70 61 67 65 20 62 65 67 69 6e 73 20 61 74 20 page begins at
11f7d 6f 66 66 73 65 74 20 2a 70 4f 66 66 73 65 74 20 offset *pOffset
11f7e 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 54 into the file. T
11f7f 68 65 20 2a 70 4f 66 66 73 65 74 0a 2a 2a 20 76 he *pOffset.** v
11f80 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65 alue is increase
11f81 64 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f d to the start o
11f82 66 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 f the next page
11f83 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a in the journal..
11f84 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61 69 6e **.** The isMain
11f85 4a 72 6e 6c 20 66 6c 61 67 20 69 73 20 74 72 75 Jrnl flag is tru
11f86 65 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 e if this is the
11f87 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a main rollback j
11f88 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 66 61 ournal and.** fa
11f89 6c 73 65 20 66 6f 72 20 74 68 65 20 73 74 61 74 lse for the stat
11f8a 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 ement journal.
11f8b 54 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 The main rollbac
11f8c 6b 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73 0a 2a k journal uses.*
11f8d 2a 20 63 68 65 63 6b 73 75 6d 73 20 2d 20 74 68 * checksums - th
11f8e 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 e statement jour
11f8f 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a nal does not..**
11f90 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 .** If the page
11f91 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 number of the pa
11f92 67 65 20 72 65 63 6f 72 64 20 72 65 61 64 20 66 ge record read f
11f93 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f rom the (sub-)jo
11f94 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 urnal file.** is
11f95 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
11f96 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 e current value
11f97 6f 66 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c of Pager.dbSize,
11f98 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 20 69 then playback i
11f99 73 0a 2a 2a 20 73 6b 69 70 70 65 64 20 61 6e 64 s.** skipped and
11f9a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
11f9b 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 turned..**.** If
11f9c 20 70 44 6f 6e 65 20 69 73 20 6e 6f 74 20 4e 55 pDone is not NU
11f9d 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 61 LL, then it is a
11f9e 20 72 65 63 6f 72 64 20 6f 66 20 70 61 67 65 73 record of pages
11f9f 20 74 68 61 74 20 68 61 76 65 20 61 6c 72 65 61 that have alrea
11fa0 64 79 0a 2a 2a 20 62 65 65 6e 20 70 6c 61 79 65 dy.** been playe
11fa1 64 20 62 61 63 6b 2e 20 20 49 66 20 74 68 65 20 d back. If the
11fa2 70 61 67 65 20 61 74 20 2a 70 4f 66 66 73 65 74 page at *pOffset
11fa3 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
11fa4 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 0a 2a 2a n played back.**
11fa5 20 28 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 (if the corresp
11fa6 6f 6e 64 69 6e 67 20 70 44 6f 6e 65 20 62 69 74 onding pDone bit
11fa7 20 69 73 20 73 65 74 29 20 74 68 65 6e 20 73 6b is set) then sk
11fa8 69 70 20 74 68 65 20 70 6c 61 79 62 61 63 6b 2e ip the playback.
11fa9 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 .** Make sure th
11faa 65 20 70 44 6f 6e 65 20 62 69 74 20 63 6f 72 72 e pDone bit corr
11fab 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 esponding to the
11fac 20 2a 70 4f 66 66 73 65 74 20 70 61 67 65 20 69 *pOffset page i
11fad 73 20 73 65 74 0a 2a 2a 20 70 72 69 6f 72 20 74 s set.** prior t
11fae 6f 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a o returning..**.
11faf 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 72 ** If the page r
11fb0 65 63 6f 72 64 20 69 73 20 73 75 63 63 65 73 73 ecord is success
11fb1 66 75 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20 fully read from
11fb2 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 the (sub-)journa
11fb3 6c 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 70 6c l file.** and pl
11fb4 61 79 65 64 20 62 61 63 6b 2c 20 74 68 65 6e 20 ayed back, then
11fb5 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
11fb6 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 urned. If an IO
11fb7 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a 20 error occurs.**
11fb8 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 74 68 while reading th
11fb9 65 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 74 68 e record from th
11fba 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 e (sub-)journal
11fbb 66 69 6c 65 20 6f 72 20 77 68 69 6c 65 20 77 72 file or while wr
11fbc 69 74 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65 20 iting.** to the
11fbd 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 database file, t
11fbe 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 hen the IO error
11fbf 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
11fc0 64 2e 20 49 66 20 64 61 74 61 0a 2a 2a 20 69 73 d. If data.** is
11fc1 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65 successfully re
11fc2 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 ad from the (sub
11fc3 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 -)journal file b
11fc4 75 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 ut appears to be
11fc5 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 2c 20 53 .** corrupted, S
11fc6 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 QLITE_DONE is re
11fc7 74 75 72 6e 65 64 2e 20 44 61 74 61 20 69 73 20 turned. Data is
11fc8 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 72 72 75 considered corru
11fc9 70 74 65 64 20 69 6e 0a 2a 2a 20 74 77 6f 20 63 pted in.** two c
11fca 69 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a 2a 2a ircumstances:.**
11fcb 20 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 .** * If the
11fcc 72 65 63 6f 72 64 20 70 61 67 65 2d 6e 75 6d 62 record page-numb
11fcd 65 72 20 69 73 20 69 6c 6c 65 67 61 6c 20 28 30 er is illegal (0
11fce 20 6f 72 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e or PAGER_MJ_PGN
11fcf 4f 29 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20 49 66 O), or.** * If
11fd0 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 62 the record is b
11fd1 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b eing rolled back
11fd2 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a from the main j
11fd3 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 ournal file.**
11fd4 20 20 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b and the check
11fd5 73 75 6d 20 66 69 65 6c 64 20 64 6f 65 73 20 6e sum field does n
11fd6 6f 74 20 6d 61 74 63 68 20 74 68 65 20 72 65 63 ot match the rec
11fd7 6f 72 64 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a ord content..**.
11fd8 2a 2a 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68 ** Neither of th
11fd9 65 73 65 20 74 77 6f 20 73 63 65 6e 61 72 69 6f ese two scenario
11fda 73 20 61 72 65 20 70 6f 73 73 69 62 6c 65 20 64 s are possible d
11fdb 75 72 69 6e 67 20 61 20 73 61 76 65 70 6f 69 6e uring a savepoin
11fdc 74 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a t rollback..**.*
11fdd 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 73 * If this is a s
11fde 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 avepoint rollbac
11fdf 6b 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 6d k, then memory m
11fe0 61 79 20 68 61 76 65 20 74 6f 20 62 65 20 64 79 ay have to be dy
11fe1 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c namically.** all
11fe2 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20 66 ocated by this f
11fe3 75 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68 69 73 unction. If this
11fe4 20 69 73 20 74 68 65 20 63 61 73 65 20 61 6e 64 is the case and
11fe5 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 an allocation f
11fe6 61 69 6c 73 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f ails,.** SQLITE_
11fe7 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 NOMEM is returne
11fe8 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
11fe9 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f pager_playback_
11fea 6f 6e 65 5f 70 61 67 65 28 0a 20 20 50 61 67 65 one_page(. Page
11feb 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 r *pPager,
11fec 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
11fed 20 70 61 67 65 72 20 62 65 69 6e 67 20 70 6c 61 pager being pla
11fee 79 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 6e yed back */. in
11fef 74 20 69 73 4d 61 69 6e 4a 72 6e 6c 2c 20 20 20 t isMainJrnl,
11ff0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 /* 1
11ff1 20 2d 3e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c -> main journal
11ff2 2e 20 30 20 2d 3e 20 73 75 62 2d 6a 6f 75 72 6e . 0 -> sub-journ
11ff3 61 6c 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55 al. */. int isU
11ff4 6e 73 79 6e 63 2c 20 20 20 20 20 20 20 20 20 20 nsync,
11ff5 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
11ff6 66 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 75 f reading from u
11ff7 6e 73 79 6e 63 65 64 20 6d 61 69 6e 20 6a 6f 75 nsynced main jou
11ff8 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 2a 70 rnal */. i64 *p
11ff9 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 Offset,
11ffa 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
11ffb 74 20 6f 66 20 72 65 63 6f 72 64 20 74 6f 20 70 t of record to p
11ffc 6c 61 79 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 layback */. int
11ffd 20 69 73 53 61 76 65 70 6e 74 2c 20 20 20 20 20 isSavepnt,
11ffe 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
11fff 75 65 20 66 6f 72 20 61 20 73 61 76 65 70 6f 69 ue for a savepoi
12000 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20 nt rollback */.
12001 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 20 Bitvec *pDone
12002 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
12003 2a 20 42 69 74 76 65 63 20 6f 66 20 70 61 67 65 * Bitvec of page
12004 73 20 61 6c 72 65 61 64 79 20 70 6c 61 79 65 64 s already played
12005 20 62 61 63 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e back */.){. in
12006 74 20 72 63 3b 0a 20 20 50 67 48 64 72 20 2a 70 t rc;. PgHdr *p
12007 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Pg;
12008 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 69 73 /* An exis
12009 74 69 6e 67 20 70 61 67 65 20 69 6e 20 74 68 65 ting page in the
1200a 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f cache */. Pgno
1200b 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 pgno;
1200c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1200d 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
1200e 61 20 70 61 67 65 20 69 6e 20 6a 6f 75 72 6e 61 a page in journa
1200f 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d l */. u32 cksum
12010 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
12011 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d /* Checksum
12012 20 75 73 65 64 20 66 6f 72 20 73 61 6e 69 74 79 used for sanity
12013 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 75 checking */. u
12014 38 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 8 *aData;
12015 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12016 54 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 Temporary storag
12017 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 20 2a e for the page *
12018 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 /. sqlite3_file
12019 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20 *jfd;
1201a 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 /* The file de
1201b 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65 scriptor for the
1201c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f journal file */
1201d 0a 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 4d .. assert( (isM
1201e 61 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d 30 20 29 ainJrnl&~1)==0 )
1201f 3b 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 69 6e ; /* isMain
12020 4a 72 6e 6c 20 69 73 20 30 20 6f 72 20 31 20 2a Jrnl is 0 or 1 *
12021 2f 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 53 /. assert( (isS
12022 61 76 65 70 6e 74 26 7e 31 29 3d 3d 30 20 29 3b avepnt&~1)==0 );
12023 20 20 20 20 20 20 20 2f 2a 20 69 73 53 61 76 65 /* isSave
12024 70 6e 74 20 69 73 20 30 20 6f 72 20 31 20 2a 2f pnt is 0 or 1 */
12025 0a 20 20 61 73 73 65 72 74 28 20 69 73 4d 61 69 . assert( isMai
12026 6e 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e 65 20 29 nJrnl || pDone )
12027 3b 20 20 20 20 20 2f 2a 20 70 44 6f 6e 65 20 61 ; /* pDone a
12028 6c 77 61 79 73 20 75 73 65 64 20 6f 6e 20 73 75 lways used on su
12029 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20 b-journals */.
1202a 61 73 73 65 72 74 28 20 69 73 53 61 76 65 70 6e assert( isSavepn
1202b 74 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 20 29 3b t || pDone==0 );
1202c 20 20 20 2f 2a 20 70 44 6f 6e 65 20 6e 65 76 65 /* pDone neve
1202d 72 20 75 73 65 64 20 6f 6e 20 6e 6f 6e 2d 73 61 r used on non-sa
1202e 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 61 44 vepoint */.. aD
1202f 61 74 61 20 3d 20 28 75 38 2a 29 70 50 61 67 65 ata = (u8*)pPage
12030 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 r->pTmpSpace;.
12031 61 73 73 65 72 74 28 20 61 44 61 74 61 20 29 3b assert( aData );
12032 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 /* Temp
12033 20 73 74 6f 72 61 67 65 20 6d 75 73 74 20 68 61 storage must ha
12034 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ve already been
12035 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 20 20 allocated */..
12036 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65 /* Read the page
12037 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67 65 number and page
12038 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 6a data from the j
12039 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d 6a 6f ournal or sub-jo
1203a 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 2e urnal. ** file.
1203b 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 Return an error
1203c 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c code to the cal
1203d 6c 65 72 20 69 66 20 61 6e 20 49 4f 20 65 72 72 ler if an IO err
1203e 6f 72 20 6f 63 63 75 72 73 2e 0a 20 20 2a 2f 0a or occurs.. */.
1203f 20 20 6a 66 64 20 3d 20 69 73 4d 61 69 6e 4a 72 jfd = isMainJr
12040 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e 6a 66 64 nl ? pPager->jfd
12041 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 3b : pPager->sjfd;
12042 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 . rc = read32bi
12043 74 73 28 6a 66 64 2c 20 2a 70 4f 66 66 73 65 74 ts(jfd, *pOffset
12044 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 , &pgno);. if(
12045 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
12046 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 return rc;. rc
12047 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 = sqlite3OsRead(
12048 6a 66 64 2c 20 61 44 61 74 61 2c 20 70 50 61 67 jfd, aData, pPag
12049 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 2a er->pageSize, (*
1204a 70 4f 66 66 73 65 74 29 2b 34 29 3b 0a 20 20 69 pOffset)+4);. i
1204b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1204c 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
1204d 2a 70 4f 66 66 73 65 74 20 2b 3d 20 70 50 61 67 *pOffset += pPag
1204e 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 34 er->pageSize + 4
1204f 20 2b 20 69 73 4d 61 69 6e 4a 72 6e 6c 2a 34 3b + isMainJrnl*4;
12050 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 .. /* Sanity ch
12051 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 70 61 ecking on the pa
12052 67 65 2e 20 20 54 68 69 73 20 69 73 20 6d 6f 72 ge. This is mor
12053 65 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 e important that
12054 20 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 I originally.
12055 2a 2a 20 74 68 6f 75 67 68 74 2e 20 20 49 66 20 ** thought. If
12056 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 a power failure
12057 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 68 65 occurs while the
12058 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 6e journal is bein
12059 67 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a 2a 20 g written,. **
1205a 69 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 69 it could cause i
1205b 6e 76 61 6c 69 64 20 64 61 74 61 20 74 6f 20 62 nvalid data to b
1205c 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 e written into t
1205d 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57 65 20 he journal. We
1205e 6e 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64 65 74 need to. ** det
1205f 65 63 74 20 74 68 69 73 20 69 6e 76 61 6c 69 64 ect this invalid
12060 20 64 61 74 61 20 28 77 69 74 68 20 68 69 67 68 data (with high
12061 20 70 72 6f 62 61 62 69 6c 69 74 79 29 20 61 6e probability) an
12062 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 2a d ignore it.. *
12063 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 /. if( pgno==0
12064 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d || pgno==PAGER_M
12065 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 J_PGNO(pPager) )
12066 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69 {. assert( !i
12067 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 20 sSavepnt );.
12068 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f return SQLITE_DO
12069 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 67 NE;. }. if( pg
1206a 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 2d no>(Pgno)pPager-
1206b 3e 64 62 53 69 7a 65 20 7c 7c 20 73 71 6c 69 74 >dbSize || sqlit
1206c 65 33 42 69 74 76 65 63 54 65 73 74 28 70 44 6f e3BitvecTest(pDo
1206d 6e 65 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20 ne, pgno) ){.
1206e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1206f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4d K;. }. if( isM
12070 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20 20 72 ainJrnl ){. r
12071 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a c = read32bits(j
12072 66 64 2c 20 28 2a 70 4f 66 66 73 65 74 29 2d 34 fd, (*pOffset)-4
12073 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 20 20 20 69 , &cksum);. i
12074 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
12075 63 3b 0a 20 20 20 20 69 66 28 20 21 69 73 53 61 c;. if( !isSa
12076 76 65 70 6e 74 20 26 26 20 70 61 67 65 72 5f 63 vepnt && pager_c
12077 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 61 ksum(pPager, aDa
12078 74 61 29 21 3d 63 6b 73 75 6d 20 29 7b 0a 20 20 ta)!=cksum ){.
12079 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1207a 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 E_DONE;. }.
1207b 7d 0a 0a 20 20 69 66 28 20 70 44 6f 6e 65 20 26 }.. if( pDone &
1207c 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 & (rc = sqlite3B
1207d 69 74 76 65 63 53 65 74 28 70 44 6f 6e 65 2c 20 itvecSet(pDone,
1207e 70 67 6e 6f 29 29 21 3d 53 51 4c 49 54 45 5f 4f pgno))!=SQLITE_O
1207f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 K ){. return
12080 72 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 rc;. }.. asser
12081 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 t( pPager->state
12082 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 ==PAGER_RESERVED
12083 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 || pPager->stat
12084 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 e>=PAGER_EXCLUSI
12085 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 VE );.. /* If t
12086 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 52 he pager is in R
12087 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 ESERVED state, t
12088 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62 hen there must b
12089 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 69 73 e a copy of this
1208a 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 . ** page in th
1208b 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 49 e pager cache. I
1208c 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75 73 74 n this case just
1208d 20 75 70 64 61 74 65 20 74 68 65 20 70 61 67 65 update the page
1208e 72 20 63 61 63 68 65 2c 0a 20 20 2a 2a 20 6e 6f r cache,. ** no
1208f 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 t the database f
12090 69 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73 ile. The page is
12091 20 6c 65 66 74 20 6d 61 72 6b 65 64 20 64 69 72 left marked dir
12092 74 79 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e ty in this case.
12093 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 78 . **. ** An ex
12094 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 65 20 61 ception to the a
12095 62 6f 76 65 20 72 75 6c 65 3a 20 49 66 20 74 68 bove rule: If th
12096 65 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e e database is in
12097 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 20 no-sync mode.
12098 2a 2a 20 61 6e 64 20 61 20 70 61 67 65 20 69 73 ** and a page is
12099 20 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 6e moved during an
1209a 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 incremental vac
1209b 75 75 6d 20 74 68 65 6e 20 74 68 65 20 70 61 67 uum then the pag
1209c 65 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 62 e may. ** not b
1209d 65 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 e in the pager c
1209e 61 63 68 65 2e 20 4c 61 74 65 72 3a 20 69 66 20 ache. Later: if
1209f 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f a malloc() or IO
120a0 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 20 20 error occurs.
120a1 2a 2a 20 64 75 72 69 6e 67 20 61 20 4d 6f 76 65 ** during a Move
120a2 70 61 67 65 28 29 20 63 61 6c 6c 2c 20 74 68 65 page() call, the
120a3 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79 20 6e n the page may n
120a4 6f 74 20 62 65 20 69 6e 20 74 68 65 20 63 61 63 ot be in the cac
120a5 68 65 0a 20 20 2a 2a 20 65 69 74 68 65 72 2e 20 he. ** either.
120a6 53 6f 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e So the condition
120a7 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 74 68 described in th
120a8 65 20 61 62 6f 76 65 20 70 61 72 61 67 72 61 70 e above paragrap
120a9 68 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 73 h is not. ** as
120aa 73 65 72 74 28 29 61 62 6c 65 2e 0a 20 20 2a 2a sert()able.. **
120ab 0a 20 20 2a 2a 20 49 66 20 69 6e 20 45 58 43 4c . ** If in EXCL
120ac 55 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 65 USIVE state, the
120ad 6e 20 77 65 20 75 70 64 61 74 65 20 74 68 65 20 n we update the
120ae 70 61 67 65 72 20 63 61 63 68 65 20 69 66 20 69 pager cache if i
120af 74 20 65 78 69 73 74 73 0a 20 20 2a 2a 20 61 6e t exists. ** an
120b0 64 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e d the main file.
120b1 20 54 68 65 20 70 61 67 65 20 69 73 20 74 68 65 The page is the
120b2 6e 20 6d 61 72 6b 65 64 20 6e 6f 74 20 64 69 72 n marked not dir
120b3 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69 ty.. **. ** Ti
120b4 63 6b 65 74 20 23 31 31 37 31 3a 20 20 54 68 65 cket #1171: The
120b5 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e statement journ
120b6 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e al might contain
120b7 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 68 page content th
120b8 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 66 66 65 at is. ** diffe
120b9 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 61 rent from the pa
120ba 67 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 74 68 ge content at th
120bb 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 e start of the t
120bc 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a ransaction.. **
120bd 20 54 68 69 73 20 6f 63 63 75 72 73 20 77 68 65 This occurs whe
120be 6e 20 61 20 70 61 67 65 20 69 73 20 63 68 61 6e n a page is chan
120bf 67 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 ged prior to the
120c0 20 73 74 61 72 74 20 6f 66 20 61 20 73 74 61 74 start of a stat
120c1 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20 ement. ** then
120c2 63 68 61 6e 67 65 64 20 61 67 61 69 6e 20 77 69 changed again wi
120c3 74 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 thin the stateme
120c4 6e 74 2e 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e nt. When rollin
120c5 67 20 62 61 63 6b 20 73 75 63 68 20 61 0a 20 20 g back such a.
120c6 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 20 ** statement we
120c7 6d 75 73 74 20 6e 6f 74 20 77 72 69 74 65 20 74 must not write t
120c8 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 o the original d
120c9 61 74 61 62 61 73 65 20 75 6e 6c 65 73 73 20 77 atabase unless w
120ca 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20 e know. ** for
120cb 63 65 72 74 61 69 6e 20 74 68 61 74 20 6f 72 69 certain that ori
120cc 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 ginal page conte
120cd 6e 74 73 20 61 72 65 20 73 79 6e 63 65 64 20 69 nts are synced i
120ce 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c nto the main rol
120cf 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e lback. ** journ
120d0 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 al. Otherwise,
120d1 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67 a power loss mig
120d2 68 74 20 6c 65 61 76 65 20 6d 6f 64 69 66 69 65 ht leave modifie
120d3 64 20 64 61 74 61 20 69 6e 20 74 68 65 0a 20 20 d data in the.
120d4 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
120d5 20 77 69 74 68 6f 75 74 20 61 6e 20 65 6e 74 72 without an entr
120d6 79 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 y in the rollbac
120d7 6b 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 63 k journal that c
120d8 61 6e 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 20 an. ** restore
120d9 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 the database to
120da 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72 its original for
120db 6d 2e 20 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f m. Two conditio
120dc 6e 73 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 ns must be. **
120dd 6d 65 74 20 62 65 66 6f 72 65 20 77 72 69 74 69 met before writi
120de 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ng to the databa
120df 73 65 20 66 69 6c 65 73 2e 20 28 31 29 20 74 68 se files. (1) th
120e0 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 e database must
120e1 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20 be. ** locked.
120e2 20 28 32 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 (2) we know tha
120e3 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 t the original p
120e4 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 66 age content is f
120e5 75 6c 6c 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a ully synced. **
120e6 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 in the main jou
120e7 72 6e 61 6c 20 65 69 74 68 65 72 20 62 65 63 61 rnal either beca
120e8 75 73 65 20 74 68 65 20 70 61 67 65 20 69 73 20 use the page is
120e9 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 not in cache or
120ea 65 6c 73 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 else. ** the pa
120eb 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 ge is marked as
120ec 6e 65 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a needSync==0.. *
120ed 2a 0a 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31 *. ** 2008-04-1
120ee 34 3a 20 20 57 68 65 6e 20 61 74 74 65 6d 70 74 4: When attempt
120ef 69 6e 67 20 74 6f 20 76 61 63 75 75 6d 20 61 20 ing to vacuum a
120f0 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 corrupt database
120f1 20 66 69 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69 file, it. ** i
120f2 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 61 s possible to fa
120f3 69 6c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f il a statement o
120f4 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 68 61 n a database tha
120f5 74 20 64 6f 65 73 20 6e 6f 74 20 79 65 74 20 65 t does not yet e
120f6 78 69 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f xist.. ** Do no
120f7 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 t attempt to wri
120f8 74 65 20 69 66 20 64 61 74 61 62 61 73 65 20 66 te if database f
120f9 69 6c 65 20 68 61 73 20 6e 65 76 65 72 20 62 65 ile has never be
120fa 65 6e 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a en opened.. */.
120fb 20 20 70 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f pPg = pager_lo
120fc 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e okup(pPager, pgn
120fd 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 o);. assert( pP
120fe 67 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a 20 g || !MEMDB );.
120ff 20 50 41 47 45 52 54 52 41 43 45 28 28 22 50 4c PAGERTRACE(("PL
12100 41 59 42 41 43 4b 20 25 64 20 70 61 67 65 20 25 AYBACK %d page %
12101 64 20 68 61 73 68 28 25 30 38 78 29 20 25 73 5c d hash(%08x) %s\
12102 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
12103 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 PAGERID(pPage
12104 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f r), pgno, pager_
12105 64 61 74 61 68 61 73 68 28 70 50 61 67 65 72 2d datahash(pPager-
12106 3e 70 61 67 65 53 69 7a 65 2c 20 61 44 61 74 61 >pageSize, aData
12107 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ),.
12108 20 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 3f 22 6d (isMainJrnl?"m
12109 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22 3a 22 73 75 ain-journal":"su
1210a 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a 20 20 29 29 b-journal"). ))
1210b 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65 72 2d ;. if( (pPager-
1210c 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 >state>=PAGER_EX
1210d 43 4c 55 53 49 56 45 29 0a 20 20 20 26 26 20 28 CLUSIVE). && (
1210e 70 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28 70 50 pPg==0 || 0==(pP
1210f 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
12110 45 45 44 5f 53 59 4e 43 29 29 0a 20 20 20 26 26 EED_SYNC)). &&
12111 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
12112 66 64 29 0a 20 20 20 26 26 20 21 69 73 55 6e 73 fd). && !isUns
12113 79 6e 63 0a 20 20 29 7b 0a 20 20 20 20 69 36 34 ync. ){. i64
12114 20 6f 66 73 74 20 3d 20 28 70 67 6e 6f 2d 31 29 ofst = (pgno-1)
12115 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 *(i64)pPager->pa
12116 67 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d geSize;. rc =
12117 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
12118 70 50 61 67 65 72 2d 3e 66 64 2c 20 61 44 61 74 pPager->fd, aDat
12119 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 a, pPager->pageS
1211a 69 7a 65 2c 20 6f 66 73 74 29 3b 0a 20 20 20 20 ize, ofst);.
1211b 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d if( pgno>pPager-
1211c 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 >dbFileSize ){.
1211d 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 pPager->dbF
1211e 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a ileSize = pgno;.
1211f 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50 }. if( pP
12120 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 29 7b ager->pBackup ){
12121 0a 20 20 20 20 20 20 43 4f 44 45 43 31 28 70 50 . CODEC1(pP
12122 61 67 65 72 2c 20 61 44 61 74 61 2c 20 70 67 6e ager, aData, pgn
12123 6f 2c 20 33 2c 20 72 63 3d 53 51 4c 49 54 45 5f o, 3, rc=SQLITE_
12124 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 20 20 73 71 NOMEM);. sq
12125 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 lite3BackupUpdat
12126 65 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 e(pPager->pBacku
12127 70 2c 20 70 67 6e 6f 2c 20 61 44 61 74 61 29 3b p, pgno, aData);
12128 0a 20 20 20 20 20 20 43 4f 44 45 43 31 28 70 50 . CODEC1(pP
12129 61 67 65 72 2c 20 61 44 61 74 61 2c 20 70 67 6e ager, aData, pgn
1212a 6f 2c 20 30 2c 20 72 63 3d 53 51 4c 49 54 45 5f o, 0, rc=SQLITE_
1212b 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 7d 0a 20 20 NOMEM);. }.
1212c 7d 65 6c 73 65 20 69 66 28 20 21 69 73 4d 61 69 }else if( !isMai
1212d 6e 4a 72 6e 6c 20 26 26 20 70 50 67 3d 3d 30 20 nJrnl && pPg==0
1212e 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 ){. /* If thi
1212f 73 20 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 s is a rollback
12130 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 61 of a savepoint a
12131 6e 64 20 64 61 74 61 20 77 61 73 20 6e 6f 74 20 nd data was not
12132 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 2a written to. *
12133 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 * the database a
12134 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e nd the page is n
12135 6f 74 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 68 ot in-memory, th
12136 65 72 65 20 69 73 20 61 20 70 6f 74 65 6e 74 69 ere is a potenti
12137 61 6c 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 al. ** proble
12138 6d 2e 20 57 68 65 6e 20 74 68 65 20 70 61 67 65 m. When the page
12139 20 69 73 20 6e 65 78 74 20 66 65 74 63 68 65 64 is next fetched
1213a 20 62 79 20 74 68 65 20 62 2d 74 72 65 65 20 6c by the b-tree l
1213b 61 79 65 72 2c 20 69 74 20 0a 20 20 20 20 2a 2a ayer, it . **
1213c 20 77 69 6c 6c 20 62 65 20 72 65 61 64 20 66 72 will be read fr
1213d 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
1213e 66 69 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20 file, which may
1213f 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 0a 20 or may not be .
12140 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 2e 20 0a ** current. .
12141 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 **. ** Th
12142 65 72 65 20 61 72 65 20 61 20 63 6f 75 70 6c 65 ere are a couple
12143 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 77 61 of different wa
12144 79 73 20 74 68 69 73 20 63 61 6e 20 68 61 70 70 ys this can happ
12145 65 6e 2e 20 41 6c 6c 20 61 72 65 20 71 75 69 74 en. All are quit
12146 65 0a 20 20 20 20 2a 2a 20 6f 62 73 63 75 72 65 e. ** obscure
12147 2e 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 . When running i
12148 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f n synchronous mo
12149 64 65 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c de, this can onl
1214a 79 20 68 61 70 70 65 6e 20 0a 20 20 20 20 2a 2a y happen . **
1214b 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 if the page is
1214c 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 on the free-list
1214d 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
1214e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
1214f 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 70 6f , then. ** po
12150 70 75 6c 61 74 65 64 2c 20 74 68 65 6e 20 6d 6f pulated, then mo
12151 76 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 ved using sqlite
12152 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 29 3PagerMovepage()
12153 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
12154 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 The solution is
12155 74 6f 20 61 64 64 20 61 6e 20 69 6e 2d 6d 65 6d to add an in-mem
12156 6f 72 79 20 70 61 67 65 20 74 6f 20 74 68 65 20 ory page to the
12157 63 61 63 68 65 20 63 6f 6e 74 61 69 6e 69 6e 67 cache containing
12158 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 . ** the data
12159 20 6a 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 just read from
1215a 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e the sub-journal.
1215b 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 Mark the page a
1215c 73 20 64 69 72 74 79 20 0a 20 20 20 20 2a 2a 20 s dirty . **
1215d 61 6e 64 20 69 66 20 74 68 65 20 70 61 67 65 72 and if the pager
1215e 20 72 65 71 75 69 72 65 73 20 61 20 6a 6f 75 72 requires a jour
1215f 6e 61 6c 2d 73 79 6e 63 2c 20 74 68 65 6e 20 6d nal-sync, then m
12160 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 ark the page as
12161 0a 20 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e . ** requirin
12162 67 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 g a journal-sync
12163 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 77 72 before it is wr
12164 69 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 itten.. */.
12165 20 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65 assert( isSave
12166 70 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 20 28 pnt );. if( (
12167 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
12168 72 41 63 71 75 69 72 65 28 70 50 61 67 65 72 2c rAcquire(pPager,
12169 20 70 67 6e 6f 2c 20 26 70 50 67 2c 20 31 29 29 pgno, &pPg, 1))
1216a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1216b 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1216c 20 20 20 20 7d 0a 20 20 20 20 70 50 67 2d 3e 66 }. pPg->f
1216d 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e lags &= ~PGHDR_N
1216e 45 45 44 5f 52 45 41 44 3b 0a 20 20 20 20 73 71 EED_READ;. sq
1216f 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 lite3PcacheMakeD
12170 69 72 74 79 28 70 50 67 29 3b 0a 20 20 7d 0a 20 irty(pPg);. }.
12171 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 if( pPg ){.
12172 2f 2a 20 4e 6f 20 70 61 67 65 20 73 68 6f 75 6c /* No page shoul
12173 64 20 65 76 65 72 20 62 65 20 65 78 70 6c 69 63 d ever be explic
12174 69 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b itly rolled back
12175 20 74 68 61 74 20 69 73 20 69 6e 20 75 73 65 2c that is in use,
12176 20 65 78 63 65 70 74 0a 20 20 20 20 2a 2a 20 66 except. ** f
12177 6f 72 20 70 61 67 65 20 31 20 77 68 69 63 68 20 or page 1 which
12178 69 73 20 68 65 6c 64 20 69 6e 20 75 73 65 20 69 is held in use i
12179 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 n order to keep
1217a 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a the lock on the.
1217b 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 ** database
1217c 61 63 74 69 76 65 2e 20 48 6f 77 65 76 65 72 20 active. However
1217d 73 75 63 68 20 61 20 70 61 67 65 20 6d 61 79 20 such a page may
1217e 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 be rolled back a
1217f 73 20 61 20 72 65 73 75 6c 74 0a 20 20 20 20 2a s a result. *
12180 2a 20 6f 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c * of an internal
12181 20 65 72 72 6f 72 20 72 65 73 75 6c 74 69 6e 67 error resulting
12182 20 69 6e 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 in an automatic
12183 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 call to. **
12184 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c sqlite3PagerRoll
12185 62 61 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 back().. */.
12186 20 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b 0a void *pData;.
12187 20 20 20 20 70 44 61 74 61 20 3d 20 70 50 67 2d pData = pPg-
12188 3e 70 44 61 74 61 3b 0a 20 20 20 20 6d 65 6d 63 >pData;. memc
12189 70 79 28 70 44 61 74 61 2c 20 61 44 61 74 61 2c py(pData, aData,
1218a 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
1218b 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e e);. pPager->
1218c 78 52 65 69 6e 69 74 65 72 28 70 50 67 29 3b 0a xReiniter(pPg);.
1218d 20 20 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72 if( isMainJr
1218e 6e 6c 20 26 26 20 28 21 69 73 53 61 76 65 70 6e nl && (!isSavepn
1218f 74 20 7c 7c 20 2a 70 4f 66 66 73 65 74 3c 3d 70 t || *pOffset<=p
12190 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
12191 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 r) ){. /* I
12192 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f f the contents o
12193 66 20 74 68 69 73 20 70 61 67 65 20 77 65 72 65 f this page were
12194 20 6a 75 73 74 20 72 65 73 74 6f 72 65 64 20 66 just restored f
12195 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 0a 20 20 rom the main .
12196 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 ** journal f
12197 69 6c 65 2c 20 74 68 65 6e 20 69 74 73 20 63 6f ile, then its co
12198 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 61 73 ntent must be as
12199 20 74 68 65 79 20 77 65 72 65 20 77 68 65 6e 20 they were when
1219a 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 72 the . ** tr
1219b 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 66 69 ansaction was fi
1219c 72 73 74 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74 rst opened. In t
1219d 68 69 73 20 63 61 73 65 20 77 65 20 63 61 6e 20 his case we can
1219e 6d 61 72 6b 20 74 68 65 20 70 61 67 65 0a 20 20 mark the page.
1219f 20 20 20 20 2a 2a 20 61 73 20 63 6c 65 61 6e 2c ** as clean,
121a0 20 73 69 6e 63 65 20 74 68 65 72 65 20 77 69 6c since there wil
121a1 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20 74 6f 20 l be no need to
121a2 77 72 69 74 65 20 69 74 20 6f 75 74 20 74 6f 20 write it out to
121a3 74 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 the.. **.
121a4 20 20 20 20 2a 2a 20 54 68 65 72 65 20 69 73 20 ** There is
121a5 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e 20 74 6f one exception to
121a6 20 74 68 69 73 20 72 75 6c 65 2e 20 49 66 20 74 this rule. If t
121a7 68 65 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 he page is being
121a8 20 72 6f 6c 6c 65 64 0a 20 20 20 20 20 20 2a 2a rolled. **
121a9 20 62 61 63 6b 20 61 73 20 70 61 72 74 20 6f 66 back as part of
121aa 20 61 20 73 61 76 65 70 6f 69 6e 74 20 28 6f 72 a savepoint (or
121ab 20 73 74 61 74 65 6d 65 6e 74 29 20 72 6f 6c 6c statement) roll
121ac 62 61 63 6b 20 66 72 6f 6d 20 61 6e 20 0a 20 20 back from an .
121ad 20 20 20 20 2a 2a 20 75 6e 73 79 6e 63 65 64 20 ** unsynced
121ae 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d portion of the m
121af 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ain journal file
121b0 2c 20 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 , then it is not
121b1 20 73 61 66 65 0a 20 20 20 20 20 20 2a 2a 20 74 safe. ** t
121b2 6f 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 20 o mark the page
121b3 61 73 20 63 6c 65 61 6e 2e 20 54 68 69 73 20 69 as clean. This i
121b4 73 20 62 65 63 61 75 73 65 20 6d 61 72 6b 69 6e s because markin
121b5 67 20 74 68 65 20 70 61 67 65 20 61 73 0a 20 20 g the page as.
121b6 20 20 20 20 2a 2a 20 63 6c 65 61 6e 20 77 69 6c ** clean wil
121b7 6c 20 63 6c 65 61 72 20 74 68 65 20 50 47 48 44 l clear the PGHD
121b8 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 R_NEED_SYNC flag
121b9 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61 67 65 . Since the page
121ba 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 is. ** alr
121bb 65 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 eady in the jour
121bc 6e 61 6c 20 66 69 6c 65 20 28 72 65 63 6f 72 64 nal file (record
121bd 65 64 20 69 6e 20 50 61 67 65 72 2e 70 49 6e 4a ed in Pager.pInJ
121be 6f 75 72 6e 61 6c 29 20 61 6e 64 0a 20 20 20 20 ournal) and.
121bf 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e ** the PGHDR_N
121c0 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 EED_SYNC flag is
121c1 20 63 6c 65 61 72 65 64 2c 20 69 66 20 74 68 65 cleared, if the
121c2 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e page is written
121c3 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 61 67 61 to. ** aga
121c4 69 6e 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 in within this t
121c5 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 77 ransaction, it w
121c6 69 6c 6c 20 62 65 20 6d 61 72 6b 65 64 20 61 73 ill be marked as
121c7 20 64 69 72 74 79 20 62 75 74 0a 20 20 20 20 20 dirty but.
121c8 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e 45 ** the PGHDR_NE
121c9 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 77 69 6c ED_SYNC flag wil
121ca 6c 20 6e 6f 74 20 62 65 20 73 65 74 2e 20 49 74 l not be set. It
121cb 20 63 6f 75 6c 64 20 74 68 65 6e 20 70 6f 74 65 could then pote
121cc 6e 74 69 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a ntially. **
121cd 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20 be written out
121ce 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
121cf 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20 69 74 e file before it
121d0 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 s journal file.
121d1 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74 20 ** segment
121d2 69 73 20 73 79 6e 63 65 64 2e 20 49 66 20 61 20 is synced. If a
121d3 63 72 61 73 68 20 6f 63 63 75 72 73 20 64 75 72 crash occurs dur
121d4 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67 ing or following
121d5 20 74 68 69 73 2c 0a 20 20 20 20 20 20 2a 2a 20 this,. **
121d6 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 database corrupt
121d7 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65 2e 0a 20 ion may ensue..
121d8 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 */. sq
121d9 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 lite3PcacheMakeC
121da 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 20 20 7d lean(pPg);. }
121db 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 .#ifdef SQLITE_C
121dc 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 HECK_PAGES. p
121dd 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 Pg->pageHash = p
121de 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 ager_pagehash(pP
121df 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f g);.#endif. /
121e0 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 70 61 * If this was pa
121e1 67 65 20 31 2c 20 74 68 65 6e 20 72 65 73 74 6f ge 1, then resto
121e2 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 re the value of
121e3 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 Pager.dbFileVers
121e4 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 74 68 69 73 .. ** Do this
121e5 20 62 65 66 6f 72 65 20 61 6e 79 20 64 65 63 6f before any deco
121e6 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 69 66 28 ding. */. if(
121e7 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 pgno==1 ){.
121e8 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 memcpy(&pPager
121e9 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 26 28 ->dbFileVers, &(
121ea 28 75 38 2a 29 70 44 61 74 61 29 5b 32 34 5d 2c (u8*)pData)[24],
121eb 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 sizeof(pPager->d
121ec 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 bFileVers));.
121ed 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 6f 64 }.. /* Decod
121ee 65 20 74 68 65 20 70 61 67 65 20 6a 75 73 74 20 e the page just
121ef 72 65 61 64 20 66 72 6f 6d 20 64 69 73 6b 20 2a read from disk *
121f0 2f 0a 20 20 20 20 43 4f 44 45 43 31 28 70 50 61 /. CODEC1(pPa
121f1 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d ger, pData, pPg-
121f2 3e 70 67 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c >pgno, 3, rc=SQL
121f3 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 ITE_NOMEM);.
121f4 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c sqlite3PcacheRel
121f5 65 61 73 65 28 70 50 67 29 3b 0a 20 20 7d 0a 20 ease(pPg);. }.
121f6 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
121f7 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a *.** Parameter z
121f8 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 6e 61 Master is the na
121f9 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a me of a master j
121fa 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 20 73 ournal file. A s
121fb 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a ingle journal.**
121fc 20 66 69 6c 65 20 74 68 61 74 20 72 65 66 65 72 file that refer
121fd 72 65 64 20 74 6f 20 74 68 65 20 6d 61 73 74 65 red to the maste
121fe 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 r journal file h
121ff 61 73 20 6a 75 73 74 20 62 65 65 6e 20 72 6f 6c as just been rol
12200 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 20 54 68 69 led back..** Thi
12201 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 s routine checks
12202 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 if it is possib
12203 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 le to delete the
12204 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
12205 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64 20 64 6f 65 file,.** and doe
12206 73 20 73 6f 20 69 66 20 69 74 20 69 73 2e 0a 2a s so if it is..*
12207 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4d *.** Argument zM
12208 61 73 74 65 72 20 6d 61 79 20 70 6f 69 6e 74 20 aster may point
12209 74 6f 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 to Pager.pTmpSpa
1220a 63 65 2e 20 53 6f 20 74 68 61 74 20 62 75 66 66 ce. So that buff
1220b 65 72 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 76 er is not .** av
1220c 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 ailable for use
1220d 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 within this func
1220e 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e tion..**.** When
1220f 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
12210 6c 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 l file is create
12211 64 2c 20 69 74 20 69 73 20 70 6f 70 75 6c 61 74 d, it is populat
12212 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 ed with the name
12213 73 20 0a 2a 2a 20 6f 66 20 61 6c 6c 20 6f 66 20 s .** of all of
12214 69 74 73 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 its child journa
12215 6c 73 2c 20 6f 6e 65 20 61 66 74 65 72 20 61 6e ls, one after an
12216 6f 74 68 65 72 2c 20 66 6f 72 6d 61 74 74 65 64 other, formatted
12217 20 61 73 20 75 74 66 2d 38 20 0a 2a 2a 20 65 6e as utf-8 .** en
12218 63 6f 64 65 64 20 74 65 78 74 2e 20 54 68 65 20 coded text. The
12219 65 6e 64 20 6f 66 20 65 61 63 68 20 63 68 69 6c end of each chil
1221a 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 d journal file i
1221b 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 61 20 s marked with a
1221c 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 .** nul-terminat
1221d 6f 72 20 62 79 74 65 20 28 30 78 30 30 29 2e 20 or byte (0x00).
1221e 69 2e 65 2e 20 74 68 65 20 65 6e 74 69 72 65 20 i.e. the entire
1221f 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20 6d 61 contents of a ma
12220 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 ster journal.**
12221 66 69 6c 65 20 66 6f 72 20 61 20 74 72 61 6e 73 file for a trans
12222 61 63 74 69 6f 6e 20 69 6e 76 6f 6c 76 69 6e 67 action involving
12223 20 74 77 6f 20 64 61 74 61 62 61 73 65 73 20 6d two databases m
12224 69 67 68 74 20 62 65 3a 0a 2a 2a 0a 2a 2a 20 20 ight be:.**.**
12225 20 22 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 61 2e 64 "/home/bill/a.d
12226 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 2f 68 6f b-journal\x00/ho
12227 6d 65 2f 62 69 6c 6c 2f 62 2e 64 62 2d 6a 6f 75 me/bill/b.db-jou
12228 72 6e 61 6c 5c 78 30 30 22 0a 2a 2a 0a 2a 2a 20 rnal\x00".**.**
12229 41 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c A master journal
1222a 20 66 69 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 62 file may only b
1222b 65 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 61 e deleted once a
1222c 6c 6c 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20 ll of its child
1222d 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73 20 68 61 76 .** journals hav
1222e 65 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 e been rolled ba
1222f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ck..**.** This f
12230 75 6e 63 74 69 6f 6e 20 72 65 61 64 73 20 74 68 unction reads th
12231 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
12232 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c e master-journal
12233 20 66 69 6c 65 20 69 6e 74 6f 20 0a 2a 2a 20 6d file into .** m
12234 65 6d 6f 72 79 20 61 6e 64 20 6c 6f 6f 70 73 20 emory and loops
12235 74 68 72 6f 75 67 68 20 65 61 63 68 20 6f 66 20 through each of
12236 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 the child journa
12237 6c 20 6e 61 6d 65 73 2e 20 46 6f 72 0a 2a 2a 20 l names. For.**
12238 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e each child journ
12239 61 6c 2c 20 69 74 20 63 68 65 63 6b 73 20 69 66 al, it checks if
1223a 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 69 66 20 74 :.**.** * if t
1223b 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c he child journal
1223c 20 65 78 69 73 74 73 2c 20 61 6e 64 20 69 66 20 exists, and if
1223d 73 6f 0a 2a 2a 20 20 20 2a 20 69 66 20 74 68 65 so.** * if the
1223e 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 child journal c
1223f 6f 6e 74 61 69 6e 73 20 61 20 72 65 66 65 72 65 ontains a refere
12240 6e 63 65 20 74 6f 20 6d 61 73 74 65 72 20 6a 6f nce to master jo
12241 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 66 69 urnal .** fi
12242 6c 65 20 7a 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a le zMaster.**.**
12243 20 49 66 20 61 20 63 68 69 6c 64 20 6a 6f 75 72 If a child jour
12244 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 nal can be found
12245 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 62 6f that matches bo
12246 74 68 20 6f 66 20 74 68 65 20 63 72 69 74 65 72 th of the criter
12247 69 61 0a 2a 2a 20 61 62 6f 76 65 2c 20 74 68 69 ia.** above, thi
12248 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
12249 6e 73 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 ns without doing
1224a 20 61 6e 79 74 68 69 6e 67 2e 20 4f 74 68 65 72 anything. Other
1224b 77 69 73 65 2c 20 69 66 0a 2a 2a 20 6e 6f 20 73 wise, if.** no s
1224c 75 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 uch child journa
1224d 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20 l can be found,
1224e 66 69 6c 65 20 7a 4d 61 73 74 65 72 20 69 73 20 file zMaster is
1224f 64 65 6c 65 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 deleted from.**
12250 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 the file-system
12251 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44 using sqlite3OsD
12252 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 elete()..**.** I
12253 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 77 69 f an IO error wi
12254 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 74 69 thin this functi
12255 6f 6e 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 on, an error cod
12256 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 54 e is returned. T
12257 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 his.** function
12258 61 6c 6c 6f 63 61 74 65 73 20 6d 65 6d 6f 72 79 allocates memory
12259 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 by calling sqli
1225a 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 20 49 66 20 te3Malloc(). If
1225b 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a an allocation.**
1225c 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e fails, SQLITE_N
1225d 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 OMEM is returned
1225e 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 . Otherwise, if
1225f 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 20 no IO or malloc
12260 65 72 72 6f 72 73 20 0a 2a 2a 20 6f 63 63 75 72 errors .** occur
12261 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 , SQLITE_OK is r
12262 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eturned..**.** T
12263 4f 44 4f 3a 20 54 68 69 73 20 66 75 6e 63 74 69 ODO: This functi
12264 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 73 on allocates a s
12265 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 6f 66 20 6d ingle block of m
12266 65 6d 6f 72 79 20 74 6f 20 6c 6f 61 64 0a 2a 2a emory to load.**
12267 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 the entire cont
12268 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61 73 74 ents of the mast
12269 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e er journal file.
1226a 20 54 68 69 73 20 63 6f 75 6c 64 20 62 65 0a 2a This could be.*
1226b 2a 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 6b 69 * a couple of ki
1226c 6c 6f 62 79 74 65 73 20 6f 72 20 73 6f 20 2d 20 lobytes or so -
1226d 70 6f 74 65 6e 74 69 61 6c 6c 79 20 6c 61 72 67 potentially larg
1226e 65 72 20 74 68 61 6e 20 74 68 65 20 70 61 67 65 er than the page
1226f 20 0a 2a 2a 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 .** size..*/.st
12270 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 64 atic int pager_d
12271 65 6c 6d 61 73 74 65 72 28 50 61 67 65 72 20 2a elmaster(Pager *
12272 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 pPager, const ch
12273 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 ar *zMaster){.
12274 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
12275 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 s = pPager->pVfs
12276 3b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 ;. int rc;
12277 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12278 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
12279 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
1227a 70 4d 61 73 74 65 72 3b 20 20 20 20 2f 2a 20 4d pMaster; /* M
1227b 61 6c 6c 6f 63 27 64 20 6d 61 73 74 65 72 2d 6a alloc'd master-j
1227c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 ournal file desc
1227d 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 riptor */. sqli
1227e 74 65 33 5f 66 69 6c 65 20 2a 70 4a 6f 75 72 6e te3_file *pJourn
1227f 61 6c 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 al; /* Malloc'
12280 64 20 63 68 69 6c 64 2d 6a 6f 75 72 6e 61 6c 20 d child-journal
12281 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
12282 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 */. char *zMast
12283 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 2f erJournal = 0; /
12284 2a 20 43 6f 6e 74 65 6e 74 73 20 6f 66 20 6d 61 * Contents of ma
12285 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
12286 65 20 2a 2f 0a 20 20 69 36 34 20 6e 4d 61 73 74 e */. i64 nMast
12287 65 72 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 erJournal;
12288 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6d 61 73 74 /* Size of mast
12289 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
1228a 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 */.. /* Allocat
1228b 65 20 73 70 61 63 65 20 66 6f 72 20 62 6f 74 68 e space for both
1228c 20 74 68 65 20 70 4a 6f 75 72 6e 61 6c 20 61 6e the pJournal an
1228d 64 20 70 4d 61 73 74 65 72 20 66 69 6c 65 20 64 d pMaster file d
1228e 65 73 63 72 69 70 74 6f 72 73 2e 0a 20 20 2a 2a escriptors.. **
1228f 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
12290 6f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20 open the master
12291 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 journal file for
12292 20 72 65 61 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 reading.. */.
12293 20 70 4d 61 73 74 65 72 20 3d 20 28 73 71 6c 69 pMaster = (sqli
12294 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 te3_file *)sqlit
12295 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 56 66 e3MallocZero(pVf
12296 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20 32 29 s->szOsFile * 2)
12297 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 20 28 ;. pJournal = (
12298 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 28 sqlite3_file *)(
12299 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72 29 20 ((u8 *)pMaster)
1229a 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 + pVfs->szOsFile
1229b 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73 74 65 );. if( !pMaste
1229c 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 r ){. rc = SQ
1229d 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 LITE_NOMEM;. }e
1229e 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 lse{. const i
1229f 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 nt flags = (SQLI
122a0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
122a1 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 |SQLITE_OPEN_MAS
122a2 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 TER_JOURNAL);.
122a3 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
122a4 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 73 74 Open(pVfs, zMast
122a5 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 6c 61 er, pMaster, fla
122a6 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 gs, 0);. }. if
122a7 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
122a8 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 ) goto delmaster
122a9 5f 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20 73 71 _out;.. rc = sq
122aa 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
122ab 70 4d 61 73 74 65 72 2c 20 26 6e 4d 61 73 74 65 pMaster, &nMaste
122ac 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 69 66 28 rJournal);. if(
122ad 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
122ae 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f goto delmaster_
122af 6f 75 74 3b 0a 0a 20 20 69 66 28 20 6e 4d 61 73 out;.. if( nMas
122b0 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 20 29 7b 0a terJournal>0 ){.
122b1 20 20 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e char *zJourn
122b2 61 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d al;. char *zM
122b3 61 73 74 65 72 50 74 72 20 3d 20 30 3b 0a 20 20 asterPtr = 0;.
122b4 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 50 74 72 int nMasterPtr
122b5 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e = pVfs->mxPathn
122b6 61 6d 65 2b 31 3b 0a 0a 20 20 20 20 2f 2a 20 4c ame+1;.. /* L
122b7 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65 20 6d oad the entire m
122b8 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
122b9 6c 65 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 le into space ob
122ba 74 61 69 6e 65 64 20 66 72 6f 6d 0a 20 20 20 20 tained from.
122bb 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f ** sqlite3_mallo
122bc 63 28 29 20 61 6e 64 20 70 6f 69 6e 74 65 64 20 c() and pointed
122bd 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 4a 6f 75 to by zMasterJou
122be 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 rnal. . */.
122bf 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c zMasterJournal
122c0 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
122c1 28 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 ((int)nMasterJou
122c2 72 6e 61 6c 20 2b 20 6e 4d 61 73 74 65 72 50 74 rnal + nMasterPt
122c3 72 20 2b 20 31 29 3b 0a 20 20 20 20 69 66 28 20 r + 1);. if(
122c4 21 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 !zMasterJournal
122c5 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
122c6 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
122c7 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 goto delmaster
122c8 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _out;. }.
122c9 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 26 7a 4d zMasterPtr = &zM
122ca 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 asterJournal[nMa
122cb 73 74 65 72 4a 6f 75 72 6e 61 6c 2b 31 5d 3b 0a sterJournal+1];.
122cc 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
122cd 4f 73 52 65 61 64 28 70 4d 61 73 74 65 72 2c 20 OsRead(pMaster,
122ce 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 zMasterJournal,
122cf 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 72 (int)nMasterJour
122d0 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 nal, 0);. if(
122d1 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
122d2 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f goto delmaster_
122d3 6f 75 74 3b 0a 20 20 20 20 7a 4d 61 73 74 65 72 out;. zMaster
122d4 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a Journal[nMasterJ
122d5 6f 75 72 6e 61 6c 5d 20 3d 20 30 3b 0a 0a 20 20 ournal] = 0;..
122d6 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4d 61 zJournal = zMa
122d7 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 sterJournal;.
122d8 20 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72 6e 61 while( (zJourna
122d9 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c l-zMasterJournal
122da 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c )<nMasterJournal
122db 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 78 ){. int ex
122dc 69 73 74 73 3b 0a 20 20 20 20 20 20 72 63 20 3d ists;. rc =
122dd 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 sqlite3OsAccess
122de 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c (pVfs, zJournal,
122df 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 SQLITE_ACCESS_E
122e0 58 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b XISTS, &exists);
122e1 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
122e2 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
122e3 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 goto delmast
122e4 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a er_out;. }.
122e5 20 20 20 20 20 20 69 66 28 20 65 78 69 73 74 73 if( exists
122e6 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f ){. /* O
122e7 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 ne of the journa
122e8 6c 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ls pointed to by
122e9 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
122ea 6e 61 6c 20 65 78 69 73 74 73 2e 0a 20 20 20 20 nal exists..
122eb 20 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74 20 61 ** Open it a
122ec 6e 64 20 63 68 65 63 6b 20 69 66 20 69 74 20 70 nd check if it p
122ed 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6d 61 73 oints at the mas
122ee 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a ter journal. If.
122ef 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 72 ** so, r
122f0 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 65 eturn without de
122f1 6c 65 74 69 6e 67 20 74 68 65 20 6d 61 73 74 65 leting the maste
122f2 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a r journal file..
122f3 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
122f4 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20 int c;.
122f5 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 int flags = (S
122f6 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
122f7 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f NLY|SQLITE_OPEN_
122f8 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 MAIN_JOURNAL);.
122f9 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
122fa 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 te3OsOpen(pVfs,
122fb 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a 6f 75 72 6e zJournal, pJourn
122fc 61 6c 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 al, flags, 0);.
122fd 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
122fe 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
122ff 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 goto delma
12300 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 ster_out;.
12301 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 72 63 20 }.. rc
12302 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 = readMasterJour
12303 6e 61 6c 28 70 4a 6f 75 72 6e 61 6c 2c 20 7a 4d nal(pJournal, zM
12304 61 73 74 65 72 50 74 72 2c 20 6e 4d 61 73 74 65 asterPtr, nMaste
12305 72 50 74 72 29 3b 0a 20 20 20 20 20 20 20 20 73 rPtr);. s
12306 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4a qlite3OsClose(pJ
12307 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 20 ournal);.
12308 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
12309 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
1230a 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f goto delmaster_o
1230b 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 ut;. }..
1230c 20 20 20 20 20 20 20 63 20 3d 20 7a 4d 61 73 74 c = zMast
1230d 65 72 50 74 72 5b 30 5d 21 3d 30 20 26 26 20 73 erPtr[0]!=0 && s
1230e 74 72 63 6d 70 28 7a 4d 61 73 74 65 72 50 74 72 trcmp(zMasterPtr
1230f 2c 20 7a 4d 61 73 74 65 72 29 3d 3d 30 3b 0a 20 , zMaster)==0;.
12310 20 20 20 20 20 20 20 69 66 28 20 63 20 29 7b 0a if( c ){.
12311 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 /* We
12312 68 61 76 65 20 61 20 6d 61 74 63 68 2e 20 44 6f have a match. Do
12313 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 not delete the
12314 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
12315 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 ile. */.
12316 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 goto delmaster
12317 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a _out;. }.
12318 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 4a }. zJ
12319 6f 75 72 6e 61 6c 20 2b 3d 20 28 73 71 6c 69 74 ournal += (sqlit
1231a 65 33 53 74 72 6c 65 6e 33 30 28 7a 4a 6f 75 72 e3Strlen30(zJour
1231b 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d 0a 20 nal)+1);. }.
1231c 20 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73 71 6c }. . rc = sql
1231d 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 ite3OsDelete(pVf
1231e 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a s, zMaster, 0);.
1231f 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3a 0a .delmaster_out:.
12320 20 20 69 66 28 20 7a 4d 61 73 74 65 72 4a 6f 75 if( zMasterJou
12321 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 rnal ){. sqli
12322 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72 te3_free(zMaster
12323 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20 20 0a Journal);. } .
12324 20 20 69 66 28 20 70 4d 61 73 74 65 72 20 29 7b if( pMaster ){
12325 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c . sqlite3OsCl
12326 6f 73 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 ose(pMaster);.
12327 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 assert( !isOpe
12328 6e 28 70 4a 6f 75 72 6e 61 6c 29 20 29 3b 0a 20 n(pJournal) );.
12329 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 }. sqlite3_fre
1232a 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 e(pMaster);. re
1232b 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
1232c 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
1232d 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e is used to chan
1232e 67 65 20 74 68 65 20 61 63 74 75 61 6c 20 73 69 ge the actual si
1232f 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
12330 73 65 20 0a 2a 2a 20 66 69 6c 65 20 69 6e 20 74 se .** file in t
12331 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e 20 he file-system.
12332 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e This only happen
12333 73 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e s when committin
12334 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c g a transaction,
12335 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 .** or rolling b
12336 61 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f ack a transactio
12337 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20 72 6f 6c n (including rol
12338 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 2d ling back a hot-
12339 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20 journal)..**.**
1233a 49 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 If the main data
1233b 62 61 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 base file is not
1233c 20 6f 70 65 6e 2c 20 6f 72 20 61 6e 20 65 78 63 open, or an exc
1233d 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 6e lusive lock is n
1233e 6f 74 0a 2a 2a 20 68 65 6c 64 2c 20 74 68 69 73 ot.** held, this
1233f 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
12340 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c o-op. Otherwise,
12341 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
12342 20 66 69 6c 65 20 69 73 0a 2a 2a 20 63 68 61 6e file is.** chan
12343 67 65 64 20 74 6f 20 6e 50 61 67 65 20 70 61 67 ged to nPage pag
12344 65 73 20 28 6e 50 61 67 65 2a 70 50 61 67 65 72 es (nPage*pPager
12345 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 ->pageSize bytes
12346 29 2e 20 49 66 20 74 68 65 20 66 69 6c 65 0a 2a ). If the file.*
12347 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 63 75 72 * on disk is cur
12348 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68 rently larger th
12349 61 6e 20 6e 50 61 67 65 20 70 61 67 65 73 2c 20 an nPage pages,
1234a 74 68 65 6e 20 75 73 65 20 74 68 65 20 56 46 53 then use the VFS
1234b 0a 2a 2a 20 78 54 72 75 6e 63 61 74 65 28 29 20 .** xTruncate()
1234c 6d 65 74 68 6f 64 20 74 6f 20 74 72 75 6e 63 61 method to trunca
1234d 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c te it..**.** Or,
1234e 20 69 74 20 6d 69 67 68 74 20 6d 69 67 68 74 20 it might might
1234f 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 be the case that
12350 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 the file on dis
12351 6b 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 k is smaller tha
12352 6e 20 0a 2a 2a 20 6e 50 61 67 65 20 70 61 67 65 n .** nPage page
12353 73 2e 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e s. Some operatin
12354 67 20 73 79 73 74 65 6d 20 69 6d 70 6c 65 6d 65 g system impleme
12355 6e 74 61 74 69 6f 6e 73 20 63 61 6e 20 67 65 74 ntations can get
12356 20 63 6f 6e 66 75 73 65 64 20 69 66 20 0a 2a 2a confused if .**
12357 20 79 6f 75 20 74 72 79 20 74 6f 20 74 72 75 6e you try to trun
12358 63 61 74 65 20 61 20 66 69 6c 65 20 74 6f 20 73 cate a file to s
12359 6f 6d 65 20 73 69 7a 65 20 74 68 61 74 20 69 73 ome size that is
1235a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 74 20 larger than it
1235b 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 69 73 .** currently is
1235c 2c 20 73 6f 20 64 65 74 65 63 74 20 74 68 69 73 , so detect this
1235d 20 63 61 73 65 20 61 6e 64 20 77 72 69 74 65 20 case and write
1235e 61 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20 62 79 a single zero by
1235f 74 65 20 74 6f 20 0a 2a 2a 20 74 68 65 20 65 6e te to .** the en
12360 64 20 6f 66 20 74 68 65 20 6e 65 77 20 66 69 6c d of the new fil
12361 65 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a e instead..**.**
12362 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
12363 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
12364 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 . If an IO error
12365 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6d 6f occurs while mo
12366 64 69 66 79 69 6e 67 0a 2a 2a 20 74 68 65 20 64 difying.** the d
12367 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 72 65 atabase file, re
12368 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 turn the error c
12369 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 ode to the calle
1236a 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
1236b 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 pager_truncate(
1236c 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 Pager *pPager, P
1236d 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e gno nPage){. in
1236e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
1236f 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ;. if( pPager->
12370 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 state>=PAGER_EXC
12371 4c 55 53 49 56 45 20 26 26 20 69 73 4f 70 65 6e LUSIVE && isOpen
12372 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a (pPager->fd) ){.
12373 20 20 20 20 69 36 34 20 63 75 72 72 65 6e 74 53 i64 currentS
12374 69 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a 20 20 ize, newSize;.
12375 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 69 74 /* TODO: Is it
12376 20 73 61 66 65 20 74 6f 20 75 73 65 20 50 61 67 safe to use Pag
12377 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 68 65 er.dbFileSize he
12378 72 65 3f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 re? */. rc =
12379 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
1237a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 63 e(pPager->fd, &c
1237b 75 72 72 65 6e 74 53 69 7a 65 29 3b 0a 20 20 20 urrentSize);.
1237c 20 6e 65 77 53 69 7a 65 20 3d 20 70 50 61 67 65 newSize = pPage
1237d 72 2d 3e 70 61 67 65 53 69 7a 65 2a 28 69 36 34 r->pageSize*(i64
1237e 29 6e 50 61 67 65 3b 0a 20 20 20 20 69 66 28 20 )nPage;. if(
1237f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
12380 20 63 75 72 72 65 6e 74 53 69 7a 65 21 3d 6e 65 currentSize!=ne
12381 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 69 wSize ){. i
12382 66 28 20 63 75 72 72 65 6e 74 53 69 7a 65 3e 6e f( currentSize>n
12383 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 ewSize ){.
12384 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
12385 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d Truncate(pPager-
12386 3e 66 64 2c 20 6e 65 77 53 69 7a 65 29 3b 0a 20 >fd, newSize);.
12387 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
12388 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12389 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e OsWrite(pPager->
1238a 66 64 2c 20 22 22 2c 20 31 2c 20 6e 65 77 53 69 fd, "", 1, newSi
1238b 7a 65 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ze-1);. }.
1238c 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
1238d 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1238e 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 pPager->dbFile
1238f 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 Size = nPage;.
12390 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
12391 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
12392 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 /*.** Set the va
12393 6c 75 65 20 6f 66 20 74 68 65 20 50 61 67 65 72 lue of the Pager
12394 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72 69 .sectorSize vari
12395 61 62 6c 65 20 66 6f 72 20 74 68 65 20 67 69 76 able for the giv
12396 65 6e 0a 2a 2a 20 70 61 67 65 72 20 62 61 73 65 en.** pager base
12397 64 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 72 d on the value r
12398 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 78 eturned by the x
12399 53 65 63 74 6f 72 53 69 7a 65 20 6d 65 74 68 6f SectorSize metho
1239a 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6f 70 65 6e d.** of the open
1239b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
1239c 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 The sector size
1239d 77 69 6c 6c 20 62 65 20 75 73 65 64 20 75 73 65 will be used use
1239e 64 20 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69 d .** to determi
1239f 6e 65 20 74 68 65 20 73 69 7a 65 20 61 6e 64 20 ne the size and
123a0 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 6a 6f 75 alignment of jou
123a1 72 6e 61 6c 20 68 65 61 64 65 72 20 61 6e 64 20 rnal header and
123a2 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e .** master journ
123a3 61 6c 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68 al pointers with
123a4 69 6e 20 63 72 65 61 74 65 64 20 6a 6f 75 72 6e in created journ
123a5 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 al files..**.**
123a6 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 For temporary fi
123a7 6c 65 73 20 74 68 65 20 65 66 66 65 63 74 69 76 les the effectiv
123a8 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73 e sector size is
123a9 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 always 512 byte
123aa 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 s..**.** Otherwi
123ab 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d 74 65 6d 70 se, for non-temp
123ac 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 74 68 65 orary files, the
123ad 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 6f effective secto
123ae 72 20 73 69 7a 65 20 69 73 0a 2a 2a 20 74 68 65 r size is.** the
123af 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
123b0 62 79 20 74 68 65 20 78 53 65 63 74 6f 72 53 69 by the xSectorSi
123b1 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 6f 75 6e ze() method roun
123b2 64 65 64 20 75 70 20 74 6f 20 35 31 32 20 69 66 ded up to 512 if
123b3 0a 2a 2a 20 69 74 20 69 73 20 6c 65 73 73 20 74 .** it is less t
123b4 68 61 6e 20 35 31 32 2c 20 6f 72 20 72 6f 75 6e han 512, or roun
123b5 64 65 64 20 64 6f 77 6e 20 74 6f 20 4d 41 58 5f ded down to MAX_
123b6 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 66 20 69 SECTOR_SIZE if i
123b7 74 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 t.** is greater
123b8 74 68 61 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f than MAX_SECTOR_
123b9 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 SIZE..*/.static
123ba 76 6f 69 64 20 73 65 74 53 65 63 74 6f 72 53 69 void setSectorSi
123bb 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ze(Pager *pPager
123bc 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f ){. assert( isO
123bd 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
123be 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 || pPager->tempF
123bf 69 6c 65 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 ile );.. if( !p
123c0 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
123c1 29 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 ){. /* Sector
123c2 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d 61 size doesn't ma
123c3 74 74 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 61 tter for tempora
123c4 72 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c 20 ry files. Also,
123c5 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 the file. **
123c6 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 62 65 65 may not have bee
123c7 6e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 6e n opened yet, in
123c8 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20 which case the
123c9 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20 OsSectorSize().
123ca 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20 ** call will
123cb 73 65 67 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2f segfault.. */
123cc 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 . pPager->sec
123cd 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 torSize = sqlite
123ce 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 70 50 3OsSectorSize(pP
123cf 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d 0a 20 ager->fd);. }.
123d0 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 if( pPager->sec
123d1 74 6f 72 53 69 7a 65 3c 35 31 32 20 29 7b 0a 20 torSize<512 ){.
123d2 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f pPager->secto
123d3 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20 7d rSize = 512;. }
123d4 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 . if( pPager->s
123d5 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 ectorSize>MAX_SE
123d6 43 54 4f 52 5f 53 49 5a 45 20 29 7b 0a 20 20 20 CTOR_SIZE ){.
123d7 20 61 73 73 65 72 74 28 20 4d 41 58 5f 53 45 43 assert( MAX_SEC
123d8 54 4f 52 5f 53 49 5a 45 3e 3d 35 31 32 20 29 3b TOR_SIZE>=512 );
123d9 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 . pPager->sec
123da 74 6f 72 53 69 7a 65 20 3d 20 4d 41 58 5f 53 45 torSize = MAX_SE
123db 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 7d 0a 7d CTOR_SIZE;. }.}
123dc 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b ../*.** Playback
123dd 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 the journal and
123de 20 74 68 75 73 20 72 65 73 74 6f 72 65 20 74 68 thus restore th
123df 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
123e0 74 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20 to.** the state
123e1 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f 72 65 it was in before
123e2 20 77 65 20 73 74 61 72 74 65 64 20 6d 61 6b 69 we started maki
123e3 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0a 2a 2a ng changes. .**
123e4 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 .** The journal
123e5 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73 20 61 file format is a
123e6 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a 0a 2a s follows: .**.*
123e7 2a 20 20 28 31 29 20 20 38 20 62 79 74 65 20 70 * (1) 8 byte p
123e8 72 65 66 69 78 2e 20 20 41 20 63 6f 70 79 20 6f refix. A copy o
123e9 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b f aJournalMagic[
123ea 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20 62 79 ]..** (2) 4 by
123eb 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e te big-endian in
123ec 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 teger which is t
123ed 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c he number of val
123ee 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 73 0a id page records.
123ef 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68 65 20 ** in the
123f0 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74 68 69 journal. If thi
123f1 73 20 76 61 6c 75 65 20 69 73 20 30 78 66 66 66 s value is 0xfff
123f2 66 66 66 66 66 2c 20 74 68 65 6e 20 63 6f 6d 70 fffff, then comp
123f3 75 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 ute the.**
123f4 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 number of page
123f5 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 records from the
123f6 20 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e 0a 2a journal size..*
123f7 2a 20 20 28 33 29 20 20 34 20 62 79 74 65 20 62 * (3) 4 byte b
123f8 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 ig-endian intege
123f9 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69 r which is the i
123fa 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 66 6f 72 nitial value for
123fb 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 20 73 the .** s
123fc 61 6e 69 74 79 20 63 68 65 63 6b 73 75 6d 2e 0a anity checksum..
123fd 2a 2a 20 20 28 34 29 20 20 34 20 62 79 74 65 20 ** (4) 4 byte
123fe 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 integer which is
123ff 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
12400 61 67 65 73 20 74 6f 20 74 72 75 6e 63 61 74 65 ages to truncate
12401 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 64 61 the.** da
12402 74 61 62 61 73 65 20 74 6f 20 64 75 72 69 6e 67 tabase to during
12403 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 a rollback..**
12404 20 28 35 29 20 20 34 20 62 79 74 65 20 62 69 67 (5) 4 byte big
12405 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 -endian integer
12406 77 68 69 63 68 20 69 73 20 74 68 65 20 73 65 63 which is the sec
12407 74 6f 72 20 73 69 7a 65 2e 20 20 54 68 65 20 68 tor size. The h
12408 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 20 20 69 eader.** i
12409 73 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 s this many byte
1240a 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 s in size..** (
1240b 36 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6) 4 byte big-e
1240c 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 ndian integer wh
1240d 69 63 68 20 69 73 20 74 68 65 20 70 61 67 65 20 ich is the page
1240e 63 61 73 65 2e 0a 2a 2a 20 20 28 37 29 20 20 34 case..** (7) 4
1240f 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 68 byte integer wh
12410 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 ich is the numbe
12411 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
12412 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
12413 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 2e 20 .** name.
12414 20 54 68 65 20 76 61 6c 75 65 20 6d 61 79 20 62 The value may b
12415 65 20 7a 65 72 6f 20 28 69 6e 64 69 63 61 74 65 e zero (indicate
12416 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e that there is n
12417 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20 20 o master.**
12418 20 20 6a 6f 75 72 6e 61 6c 2e 29 0a 2a 2a 20 20 journal.).**
12419 28 38 29 20 20 4e 20 62 79 74 65 73 20 6f 66 20 (8) N bytes of
1241a 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
1241b 61 6c 20 6e 61 6d 65 2e 20 20 54 68 65 20 6e 61 al name. The na
1241c 6d 65 20 77 69 6c 6c 20 62 65 20 6e 75 6c 2d 74 me will be nul-t
1241d 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20 erminated.**
1241e 20 20 20 61 6e 64 20 6d 69 67 68 74 20 62 65 20 and might be
1241f 73 68 6f 72 74 65 72 20 74 68 61 6e 20 74 68 65 shorter than the
12420 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d value read from
12421 20 28 35 29 2e 20 20 49 66 20 74 68 65 20 66 69 (5). If the fi
12422 72 73 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20 rst byte.**
12423 20 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73 of the name is
12424 20 5c 30 30 30 20 74 68 65 6e 20 74 68 65 72 65 \000 then there
12425 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f is no master jo
12426 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61 73 74 urnal. The mast
12427 65 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f 75 72 er.** jour
12428 6e 61 6c 20 6e 61 6d 65 20 69 73 20 73 74 6f 72 nal name is stor
12429 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a 2a 20 ed in UTF-8..**
1242a 20 28 39 29 20 20 5a 65 72 6f 20 6f 72 20 6d 6f (9) Zero or mo
1242b 72 65 20 70 61 67 65 73 20 69 6e 73 74 61 6e 63 re pages instanc
1242c 65 73 2c 20 65 61 63 68 20 61 73 20 66 6f 6c 6c es, each as foll
1242d 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 2b ows:.** +
1242e 20 20 34 20 62 79 74 65 20 70 61 67 65 20 6e 75 4 byte page nu
1242f 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20 mber..**
12430 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 + pPager->pageS
12431 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 ize bytes of dat
12432 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 a..** +
12433 34 20 62 79 74 65 20 63 68 65 63 6b 73 75 6d 0a 4 byte checksum.
12434 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20 73 70 **.** When we sp
12435 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e eak of the journ
12436 61 6c 20 68 65 61 64 65 72 2c 20 77 65 20 6d 65 al header, we me
12437 61 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 69 an the first 8 i
12438 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 45 tems above..** E
12439 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 ach entry in the
1243a 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e 20 69 journal is an i
1243b 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 39 nstance of the 9
1243c 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 43 th item..**.** C
1243d 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 20 66 72 all the value fr
1243e 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 20 62 75 om the second bu
1243f 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 20 6e 52 llet "nRec". nR
12440 65 63 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ec is the number
12441 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70 61 67 of.** valid pag
12442 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 e entries in the
12443 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20 6d 6f journal. In mo
12444 73 74 20 63 61 73 65 73 2c 20 79 6f 75 20 63 61 st cases, you ca
12445 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a n compute the.**
12446 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 66 value of nRec f
12447 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f 66 20 rom the size of
12448 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
12449 2e 20 20 42 75 74 20 69 66 20 61 20 70 6f 77 65 . But if a powe
1244a 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 63 63 r.** failure occ
1244b 75 72 72 65 64 20 77 68 69 6c 65 20 74 68 65 20 urred while the
1244c 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 69 6e journal was bein
1244d 67 20 77 72 69 74 74 65 6e 2c 20 69 74 20 63 6f g written, it co
1244e 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20 63 61 uld be the.** ca
1244f 73 65 20 74 68 61 74 20 74 68 65 20 73 69 7a 65 se that the size
12450 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
12451 66 69 6c 65 20 68 61 64 20 61 6c 72 65 61 64 79 file had already
12452 20 62 65 65 6e 20 69 6e 63 72 65 61 73 65 64 20 been increased
12453 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 74 72 61 but.** the extra
12454 20 65 6e 74 72 69 65 73 20 68 61 64 20 6e 6f 74 entries had not
12455 20 79 65 74 20 6d 61 64 65 20 69 74 20 73 61 66 yet made it saf
12456 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20 49 6e ely to disk. In
12457 20 73 75 63 68 20 61 20 63 61 73 65 2c 0a 2a 2a such a case,.**
12458 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 the value of nR
12459 65 63 20 63 6f 6d 70 75 74 65 64 20 66 72 6f 6d ec computed from
1245a 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 77 the file size w
1245b 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61 72 67 ould be too larg
1245c 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61 74 20 e. For.** that
1245d 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77 61 79 reason, we alway
1245e 73 20 75 73 65 20 74 68 65 20 6e 52 65 63 20 76 s use the nRec v
1245f 61 6c 75 65 20 69 6e 20 74 68 65 20 68 65 61 64 alue in the head
12460 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 er..**.** If the
12461 20 6e 52 65 63 20 76 61 6c 75 65 20 69 73 20 30 nRec value is 0
12462 78 66 66 66 66 66 66 66 66 20 69 74 20 6d 65 61 xffffffff it mea
12463 6e 73 20 74 68 61 74 20 6e 52 65 63 20 73 68 6f ns that nRec sho
12464 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 0a uld be computed.
12465 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 ** from the file
12466 20 73 69 7a 65 2e 20 20 54 68 69 73 20 76 61 6c size. This val
12467 75 65 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 ue is used when
12468 74 68 65 20 75 73 65 72 20 73 65 6c 65 63 74 73 the user selects
12469 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e 63 20 the.** no-sync
1246a 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6a option for the j
1246b 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77 65 72 ournal. A power
1246c 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c failure could l
1246d 65 61 64 20 74 6f 20 63 6f 72 72 75 70 74 69 6f ead to corruptio
1246e 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73 n.** in this cas
1246f 65 2e 20 20 42 75 74 20 66 6f 72 20 74 68 69 6e e. But for thin
12470 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72 61 72 gs like temporar
12471 79 20 74 61 62 6c 65 20 28 77 68 69 63 68 20 77 y table (which w
12472 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65 74 65 ill be.** delete
12473 64 20 77 68 65 6e 20 74 68 65 20 70 6f 77 65 72 d when the power
12474 20 69 73 20 72 65 73 74 6f 72 65 64 29 20 77 65 is restored) we
12475 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 20 0a 2a don't care. .*
12476 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65 *.** If the file
12477 20 6f 70 65 6e 65 64 20 61 73 20 74 68 65 20 6a opened as the j
12478 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e ournal file is n
12479 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 ot a well-formed
1247a 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 .** journal file
1247b 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 20 then all pages
1247c 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 up to the first
1247d 63 6f 72 72 75 70 74 65 64 20 70 61 67 65 20 61 corrupted page a
1247e 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 re rolled.** bac
1247f 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 73 20 69 k (or no pages i
12480 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 f the journal he
12481 61 64 65 72 20 69 73 20 63 6f 72 72 75 70 74 65 ader is corrupte
12482 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 d). The journal
12483 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65 6e 20 file.** is then
12484 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51 4c 49 deleted and SQLI
12485 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2c 20 TE_OK returned,
12486 6a 75 73 74 20 61 73 20 69 66 20 6e 6f 20 63 6f just as if no co
12487 72 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a 2a 20 rruption had.**
12488 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72 65 64 been encountered
12489 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 2f ..**.** If an I/
1248a 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65 72 O or malloc() er
1248b 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 ror occurs, the
1248c 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20 journal-file is
1248d 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a 20 61 not deleted.** a
1248e 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 nd an error code
1248f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
12490 0a 2a 2a 20 54 68 65 20 69 73 48 6f 74 20 70 61 .** The isHot pa
12491 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74 65 rameter indicate
12492 73 20 74 68 61 74 20 77 65 20 61 72 65 20 74 72 s that we are tr
12493 79 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b ying to rollback
12494 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 74 68 a journal.** th
12495 61 74 20 6d 69 67 68 74 20 62 65 20 61 20 68 6f at might be a ho
12496 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 72 2c 20 t journal. Or,
12497 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 it could be that
12498 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
12499 0a 2a 2a 20 70 72 65 73 65 72 76 65 64 20 62 65 .** preserved be
1249a 63 61 75 73 65 20 6f 66 20 4a 4f 55 52 4e 41 4c cause of JOURNAL
1249b 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 6f 72 20 MODE_PERSIST or
1249c 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e JOURNALMODE_TRUN
1249d 43 41 54 45 2e 0a 2a 2a 20 49 66 20 74 68 65 20 CATE..** If the
1249e 6a 6f 75 72 6e 61 6c 20 72 65 61 6c 6c 79 20 69 journal really i
1249f 73 20 68 6f 74 2c 20 72 65 73 65 74 20 74 68 65 s hot, reset the
124a0 20 70 61 67 65 72 20 63 61 63 68 65 20 70 72 69 pager cache pri
124a1 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a 20 62 61 or rolling.** ba
124a2 63 6b 20 61 6e 79 20 63 6f 6e 74 65 6e 74 2e 20 ck any content.
124a3 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 If the journal
124a4 69 73 20 6d 65 72 65 6c 79 20 70 65 72 73 69 73 is merely persis
124a5 74 65 6e 74 2c 20 6e 6f 20 72 65 73 65 74 20 69 tent, no reset i
124a6 73 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a s.** needed..*/.
124a7 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
124a8 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 65 72 20 _playback(Pager
124a9 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69 73 48 *pPager, int isH
124aa 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 ot){. sqlite3_v
124ab 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 fs *pVfs = pPage
124ac 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36 34 20 73 r->pVfs;. i64 s
124ad 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 zJ;
124ae 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 /* Size of t
124af 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
124b0 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 33 in bytes */. u3
124b1 32 20 6e 52 65 63 3b 20 20 20 20 20 20 20 20 20 2 nRec;
124b2 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
124b3 20 6f 66 20 52 65 63 6f 72 64 73 20 69 6e 20 74 of Records in t
124b4 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 he journal */.
124b5 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 20 20 u32 u;
124b6 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 /* Unsi
124b7 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 gned loop counte
124b8 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67 r */. Pgno mxPg
124b9 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
124ba 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f /* Size of the o
124bb 72 69 67 69 6e 61 6c 20 66 69 6c 65 20 69 6e 20 riginal file in
124bc 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 pages */. int r
124bd 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
124be 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f /* Result co
124bf 64 65 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69 de of a subrouti
124c0 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20 ne */. int res
124c1 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 1;
124c2 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e /* Value return
124c3 65 64 20 62 79 20 73 71 6c 69 74 65 33 4f 73 41 ed by sqlite3OsA
124c4 63 63 65 73 73 28 29 20 2a 2f 0a 20 20 63 68 61 ccess() */. cha
124c5 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30 3b 20 r *zMaster = 0;
124c6 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
124c7 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
124c8 66 69 6c 65 20 69 66 20 61 6e 79 20 2a 2f 0a 20 file if any */.
124c9 20 69 6e 74 20 6e 65 65 64 50 61 67 65 72 52 65 int needPagerRe
124ca 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 set; /* Tru
124cb 65 20 74 6f 20 72 65 73 65 74 20 70 61 67 65 20 e to reset page
124cc 70 72 69 6f 72 20 74 6f 20 66 69 72 73 74 20 70 prior to first p
124cd 61 67 65 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a age rollback */.
124ce 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 . /* Figure out
124cf 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f 72 64 how many record
124d0 73 20 61 72 65 20 69 6e 20 74 68 65 20 6a 6f 75 s are in the jou
124d1 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20 65 61 72 rnal. Abort ear
124d2 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 6a ly if. ** the j
124d3 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70 74 79 2e ournal is empty.
124d4 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
124d5 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
124d6 66 64 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 fd) );. rc = sq
124d7 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
124d8 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a pPager->jfd, &sz
124d9 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 J);. if( rc!=SQ
124da 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3d 3d LITE_OK || szJ==
124db 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 0 ){. goto en
124dc 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a d_playback;. }.
124dd 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6d . /* Read the m
124de 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
124df 6d 65 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 me from the jour
124e0 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20 70 72 nal, if it is pr
124e1 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 49 66 20 61 esent.. ** If a
124e2 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
124e3 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 73 70 65 file name is spe
124e4 63 69 66 69 65 64 2c 20 62 75 74 20 74 68 65 20 cified, but the
124e5 66 69 6c 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a file is not. **
124e6 20 70 72 65 73 65 6e 74 20 6f 6e 20 64 69 73 6b present on disk
124e7 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e , then the journ
124e8 61 6c 20 69 73 20 6e 6f 74 20 68 6f 74 20 61 6e al is not hot an
124e9 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 d does not need
124ea 74 6f 20 62 65 0a 20 20 2a 2a 20 70 6c 61 79 65 to be. ** playe
124eb 64 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a d back.. **. *
124ec 2a 20 54 4f 44 4f 3a 20 54 65 63 68 6e 69 63 61 * TODO: Technica
124ed 6c 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e lly the followin
124ee 67 20 69 73 20 61 6e 20 65 72 72 6f 72 20 62 65 g is an error be
124ef 63 61 75 73 65 20 69 74 20 61 73 73 75 6d 65 73 cause it assumes
124f0 20 74 68 61 74 0a 20 20 2a 2a 20 62 75 66 66 65 that. ** buffe
124f1 72 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 r Pager.pTmpSpac
124f2 65 20 69 73 20 28 6d 78 50 61 74 68 6e 61 6d 65 e is (mxPathname
124f3 2b 31 29 20 62 79 74 65 73 20 6f 72 20 6c 61 72 +1) bytes or lar
124f4 67 65 72 2e 20 69 2e 65 2e 20 74 68 61 74 0a 20 ger. i.e. that.
124f5 20 2a 2a 20 28 70 50 61 67 65 72 2d 3e 70 61 67 ** (pPager->pag
124f6 65 53 69 7a 65 20 3e 3d 20 70 50 61 67 65 72 2d eSize >= pPager-
124f7 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d >pVfs->mxPathnam
124f8 65 2b 31 29 2e 20 55 73 69 6e 67 20 6f 73 5f 75 e+1). Using os_u
124f9 6e 69 78 2e 63 2c 0a 20 20 2a 2a 20 20 6d 78 50 nix.c,. ** mxP
124fa 61 74 68 6e 61 6d 65 20 69 73 20 35 31 32 2c 20 athname is 512,
124fb 77 68 69 63 68 20 69 73 20 74 68 65 20 73 61 6d which is the sam
124fc 65 20 61 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d e as the minimum
124fd 20 61 6c 6c 6f 77 61 62 6c 65 20 76 61 6c 75 65 allowable value
124fe 0a 20 20 2a 2a 20 66 6f 72 20 70 61 67 65 53 69 . ** for pageSi
124ff 7a 65 2e 0a 20 20 2a 2f 0a 20 20 7a 4d 61 73 74 ze.. */. zMast
12500 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d er = pPager->pTm
12501 70 53 70 61 63 65 3b 0a 20 20 72 63 20 3d 20 72 pSpace;. rc = r
12502 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c eadMasterJournal
12503 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d (pPager->jfd, zM
12504 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 aster, pPager->p
12505 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b Vfs->mxPathname+
12506 31 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 1);. if( rc==SQ
12507 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 LITE_OK && zMast
12508 65 72 5b 30 5d 20 29 7b 0a 20 20 20 20 72 63 20 er[0] ){. rc
12509 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 = sqlite3OsAcces
1250a 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c s(pVfs, zMaster,
1250b 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 SQLITE_ACCESS_E
1250c 58 49 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 XISTS, &res);.
1250d 7d 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b }. zMaster = 0;
1250e 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
1250f 45 5f 4f 4b 20 7c 7c 20 21 72 65 73 20 29 7b 0a E_OK || !res ){.
12510 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 goto end_pla
12511 79 62 61 63 6b 3b 0a 20 20 7d 0a 20 20 70 50 61 yback;. }. pPa
12512 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
12513 3d 20 30 3b 0a 20 20 6e 65 65 64 50 61 67 65 72 = 0;. needPager
12514 52 65 73 65 74 20 3d 20 69 73 48 6f 74 3b 0a 0a Reset = isHot;..
12515 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 74 /* This loop t
12516 65 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65 72 erminates either
12517 20 77 68 65 6e 20 61 20 72 65 61 64 4a 6f 75 72 when a readJour
12518 6e 61 6c 48 64 72 28 29 20 6f 72 20 0a 20 20 2a nalHdr() or . *
12519 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b * pager_playback
1251a 5f 6f 6e 65 5f 70 61 67 65 28 29 20 63 61 6c 6c _one_page() call
1251b 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
1251c 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f 20 65 72 DONE or an IO er
1251d 72 6f 72 20 0a 20 20 2a 2a 20 6f 63 63 75 72 73 ror . ** occurs
1251e 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 . . */. while(
1251f 20 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 73 1 ){. int is
12520 55 6e 73 79 6e 63 20 3d 20 30 3b 0a 0a 20 20 20 Unsync = 0;..
12521 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6e 65 78 /* Read the nex
12522 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 t journal header
12523 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 from the journa
12524 6c 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 l file. If ther
12525 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e 6f 74 e are. ** not
12526 20 65 6e 6f 75 67 68 20 62 79 74 65 73 20 6c 65 enough bytes le
12527 66 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 ft in the journa
12528 6c 20 66 69 6c 65 20 66 6f 72 20 61 20 63 6f 6d l file for a com
12529 70 6c 65 74 65 20 68 65 61 64 65 72 2c 20 6f 72 plete header, or
1252a 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 63 6f . ** it is co
1252b 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20 61 20 rrupted, then a
1252c 70 72 6f 63 65 73 73 20 6d 75 73 74 20 6f 66 20 process must of
1252d 66 61 69 6c 65 64 20 77 68 69 6c 65 20 77 72 69 failed while wri
1252e 74 69 6e 67 20 69 74 2e 0a 20 20 20 20 2a 2a 20 ting it.. **
1252f 54 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 6e This indicates n
12530 6f 74 68 69 6e 67 20 6d 6f 72 65 20 6e 65 65 64 othing more need
12531 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 s to be rolled b
12532 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ack.. */.
12533 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c rc = readJournal
12534 48 64 72 28 70 50 61 67 65 72 2c 20 69 73 48 6f Hdr(pPager, isHo
12535 74 2c 20 73 7a 4a 2c 20 26 6e 52 65 63 2c 20 26 t, szJ, &nRec, &
12536 6d 78 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72 mxPg);. if( r
12537 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 c!=SQLITE_OK ){
12538 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
12539 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 QLITE_DONE ){.
1253a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1253b 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 E_OK;. }.
1253c 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 goto end_pla
1253d 79 62 61 63 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 yback;. }..
1253e 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20 /* If nRec is
1253f 30 78 66 66 66 66 66 66 66 66 2c 20 74 68 65 6e 0xffffffff, then
12540 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 this journal wa
12541 73 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70 s created by a p
12542 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a 20 77 6f rocess. ** wo
12543 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 rking in no-sync
12544 20 6d 6f 64 65 2e 20 54 68 69 73 20 6d 65 61 6e mode. This mean
12545 73 20 74 68 61 74 20 74 68 65 20 72 65 73 74 20 s that the rest
12546 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 of the journal.
12547 20 20 20 2a 2a 20 66 69 6c 65 20 63 6f 6e 73 69 ** file consi
12548 73 74 73 20 6f 66 20 70 61 67 65 73 2c 20 74 68 sts of pages, th
12549 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 ere are no more
1254a 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 73 2e journal headers.
1254b 20 43 6f 6d 70 75 74 65 0a 20 20 20 20 2a 2a 20 Compute. **
1254c 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 the value of nRe
1254d 63 20 62 61 73 65 64 20 6f 6e 20 74 68 69 73 20 c based on this
1254e 61 73 73 75 6d 70 74 69 6f 6e 2e 0a 20 20 20 20 assumption..
1254f 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 63 3d */. if( nRec=
12550 3d 30 78 66 66 66 66 66 66 66 66 20 29 7b 0a 20 =0xffffffff ){.
12551 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
12552 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d ger->journalOff=
12553 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 =JOURNAL_HDR_SZ(
12554 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 20 pPager) );.
12555 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 28 73 nRec = (int)((s
12556 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 zJ - JOURNAL_HDR
12557 5f 53 5a 28 70 50 61 67 65 72 29 29 2f 4a 4f 55 _SZ(pPager))/JOU
12558 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 RNAL_PG_SZ(pPage
12559 72 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 r));. }..
1255a 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30 20 /* If nRec is 0
1255b 61 6e 64 20 74 68 69 73 20 72 6f 6c 6c 62 61 63 and this rollbac
1255c 6b 20 69 73 20 6f 66 20 61 20 74 72 61 6e 73 61 k is of a transa
1255d 63 74 69 6f 6e 20 63 72 65 61 74 65 64 20 62 79 ction created by
1255e 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 70 72 6f this. ** pro
1255f 63 65 73 73 20 61 6e 64 20 69 66 20 74 68 69 73 cess and if this
12560 20 69 73 20 74 68 65 20 66 69 6e 61 6c 20 68 65 is the final he
12561 61 64 65 72 20 69 6e 20 74 68 65 20 6a 6f 75 72 ader in the jour
12562 6e 61 6c 2c 20 74 68 65 6e 20 69 74 20 6d 65 61 nal, then it mea
12563 6e 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 ns. ** that t
12564 68 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 his part of the
12565 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 69 6e journal was bein
12566 67 20 66 69 6c 6c 65 64 20 62 75 74 20 68 61 73 g filled but has
12567 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 0a 20 20 not yet been.
12568 20 20 2a 2a 20 73 79 6e 63 65 64 20 74 6f 20 64 ** synced to d
12569 69 73 6b 2e 20 20 43 6f 6d 70 75 74 65 20 74 68 isk. Compute th
1256a 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
1256b 73 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 72 s based on the r
1256c 65 6d 61 69 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 emaining. **
1256d 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 size of the file
1256e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
1256f 54 68 65 20 74 68 69 72 64 20 74 65 72 6d 20 6f The third term o
12570 66 20 74 68 65 20 74 65 73 74 20 77 61 73 20 61 f the test was a
12571 64 64 65 64 20 74 6f 20 66 69 78 20 74 69 63 6b dded to fix tick
12572 65 74 20 23 32 35 36 35 2e 0a 20 20 20 20 2a 2a et #2565.. **
12573 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 When rolling ba
12574 63 6b 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c ck a hot journal
12575 2c 20 6e 52 65 63 3d 3d 30 20 61 6c 77 61 79 73 , nRec==0 always
12576 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
12577 6e 65 78 74 0a 20 20 20 20 2a 2a 20 63 68 75 6e next. ** chun
12578 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c k of the journal
12579 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72 6f 20 70 contains zero p
1257a 61 67 65 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 ages to be rolle
1257b 64 20 62 61 63 6b 2e 20 20 42 75 74 0a 20 20 20 d back. But.
1257c 20 2a 2a 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 ** when doing a
1257d 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 20 74 68 ROLLBACK and th
1257e 65 20 6e 52 65 63 3d 3d 30 20 63 68 75 6e 6b 20 e nRec==0 chunk
1257f 69 73 20 74 68 65 20 6c 61 73 74 20 63 68 75 6e is the last chun
12580 6b 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 k in. ** the
12581 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 6d 65 61 6e journal, it mean
12582 73 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e s that the journ
12583 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e al might contain
12584 20 61 64 64 69 74 69 6f 6e 61 6c 0a 20 20 20 20 additional.
12585 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20 6e 65 ** pages that ne
12586 65 64 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 ed to be rolled
12587 62 61 63 6b 20 61 6e 64 20 74 68 61 74 20 74 68 back and that th
12588 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
12589 73 20 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 s . ** should
1258a 20 62 65 20 63 6f 6d 70 75 74 65 64 20 62 61 73 be computed bas
1258b 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 ed on the journa
1258c 6c 20 66 69 6c 65 20 73 69 7a 65 2e 0a 20 20 20 l file size..
1258d 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 63 */. if( nRec
1258e 3d 3d 30 20 26 26 20 21 69 73 48 6f 74 20 26 26 ==0 && !isHot &&
1258f 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
12590 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 >journalHdr+JOUR
12591 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
12592 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 r)==pPager->jour
12593 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 20 20 nalOff ){.
12594 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 28 73 7a nRec = (int)((sz
12595 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 J - pPager->jour
12596 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41 nalOff) / JOURNA
12597 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 L_PG_SZ(pPager))
12598 3b 0a 20 20 20 20 20 20 69 73 55 6e 73 79 6e 63 ;. isUnsync
12599 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 = 1;. }..
1259a 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 /* If this is t
1259b 68 65 20 66 69 72 73 74 20 68 65 61 64 65 72 20 he first header
1259c 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f read from the jo
1259d 75 72 6e 61 6c 2c 20 74 72 75 6e 63 61 74 65 20 urnal, truncate
1259e 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 the. ** datab
1259f 61 73 65 20 66 69 6c 65 20 62 61 63 6b 20 74 6f ase file back to
125a0 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 its original si
125a1 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ze.. */. i
125a2 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
125a3 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 alOff==JOURNAL_H
125a4 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 7b DR_SZ(pPager) ){
125a5 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
125a6 72 5f 74 72 75 6e 63 61 74 65 28 70 50 61 67 65 r_truncate(pPage
125a7 72 2c 20 6d 78 50 67 29 3b 0a 20 20 20 20 20 20 r, mxPg);.
125a8 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
125a9 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 K ){. got
125aa 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a o end_playback;.
125ab 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 }. pP
125ac 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6d ager->dbSize = m
125ad 78 50 67 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 xPg;. }..
125ae 2f 2a 20 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c /* Copy original
125af 20 70 61 67 65 73 20 6f 75 74 20 6f 66 20 74 68 pages out of th
125b0 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61 e journal and ba
125b1 63 6b 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 20 ck into the .
125b2 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c ** database fil
125b3 65 20 61 6e 64 2f 6f 72 20 70 61 67 65 20 63 61 e and/or page ca
125b4 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 che.. */.
125b5 66 6f 72 28 75 3d 30 3b 20 75 3c 6e 52 65 63 3b for(u=0; u<nRec;
125b6 20 75 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 u++){. if(
125b7 20 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 20 needPagerReset
125b8 29 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 ){. pager
125b9 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a _reset(pPager);.
125ba 20 20 20 20 20 20 20 20 6e 65 65 64 50 61 67 65 needPage
125bb 72 52 65 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 rReset = 0;.
125bc 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 70 }. rc = p
125bd 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e ager_playback_on
125be 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c 31 2c e_page(pPager,1,
125bf 69 73 55 6e 73 79 6e 63 2c 26 70 50 61 67 65 72 isUnsync,&pPager
125c0 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 30 2c 30 ->journalOff,0,0
125c1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
125c2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
125c3 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
125c4 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 LITE_DONE ){.
125c5 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
125c6 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 TE_OK;.
125c7 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
125c8 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 20 20 20 Off = szJ;.
125c9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
125ca 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
125cb 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72 /* If we ar
125cc 65 20 75 6e 61 62 6c 65 20 74 6f 20 72 6f 6c 6c e unable to roll
125cd 62 61 63 6b 2c 20 71 75 69 74 20 61 6e 64 20 72 back, quit and r
125ce 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 0a eturn the error.
125cf 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 64 ** cod
125d0 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 e. This will ca
125d1 75 73 65 20 74 68 65 20 70 61 67 65 72 20 74 6f use the pager to
125d2 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 enter the error
125d3 20 73 74 61 74 65 0a 20 20 20 20 20 20 20 20 20 state.
125d4 20 2a 2a 20 73 6f 20 74 68 61 74 20 6e 6f 20 66 ** so that no f
125d5 75 72 74 68 65 72 20 68 61 72 6d 20 77 69 6c 6c urther harm will
125d6 20 62 65 20 64 6f 6e 65 2e 20 20 50 65 72 68 61 be done. Perha
125d7 70 73 20 74 68 65 20 6e 65 78 74 0a 20 20 20 20 ps the next.
125d8 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 ** process
125d9 20 74 6f 20 63 6f 6d 65 20 61 6c 6f 6e 67 20 77 to come along w
125da 69 6c 6c 20 62 65 20 61 62 6c 65 20 74 6f 20 72 ill be able to r
125db 6f 6c 6c 62 61 63 6b 20 74 68 65 20 64 61 74 61 ollback the data
125dc 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20 20 base..
125dd 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 */. got
125de 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a o end_playback;.
125df 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
125e0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a }. }. }. /*
125e1 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 61 NOTREACHED*/. a
125e2 73 73 65 72 74 28 20 30 20 29 3b 0a 0a 65 6e 64 ssert( 0 );..end
125e3 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 2f 2a 20 _playback:. /*
125e4 46 6f 6c 6c 6f 77 69 6e 67 20 61 20 72 6f 6c 6c Following a roll
125e5 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61 62 61 back, the databa
125e6 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 se file should b
125e7 65 20 62 61 63 6b 20 69 6e 20 69 74 73 20 6f 72 e back in its or
125e8 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 73 74 61 74 iginal. ** stat
125e9 65 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73 e prior to the s
125ea 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e tart of the tran
125eb 73 61 63 74 69 6f 6e 2c 20 73 6f 20 69 6e 76 6f saction, so invo
125ec 6b 65 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49 ke the. ** SQLI
125ed 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 TE_FCNTL_DB_UNCH
125ee 41 4e 47 45 44 20 66 69 6c 65 2d 63 6f 6e 74 72 ANGED file-contr
125ef 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 64 69 73 ol method to dis
125f0 61 62 6c 65 20 74 68 65 0a 20 20 2a 2a 20 61 73 able the. ** as
125f1 73 65 72 74 69 6f 6e 20 74 68 61 74 20 74 68 65 sertion that the
125f2 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 transaction cou
125f3 6e 74 65 72 20 77 61 73 20 6d 6f 64 69 66 69 65 nter was modifie
125f4 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 d.. */. assert
125f5 28 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 66 64 (. pPager->fd
125f6 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 20 7c 7c ->pMethods==0 ||
125f7 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69 . sqlite3OsFi
125f8 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61 67 65 72 leControl(pPager
125f9 2d 3e 66 64 2c 53 51 4c 49 54 45 5f 46 43 4e 54 ->fd,SQLITE_FCNT
125fa 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 2c 30 L_DB_UNCHANGED,0
125fb 29 3e 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 29 )>=SQLITE_OK. )
125fc 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 ;.. /* If this
125fd 70 6c 61 79 62 61 63 6b 20 69 73 20 68 61 70 70 playback is happ
125fe 65 6e 69 6e 67 20 61 75 74 6f 6d 61 74 69 63 61 ening automatica
125ff 6c 6c 79 20 61 73 20 61 20 72 65 73 75 6c 74 20 lly as a result
12600 6f 66 20 61 6e 20 49 4f 20 6f 72 20 0a 20 20 2a of an IO or . *
12601 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 74 * malloc error t
12602 68 61 74 20 6f 63 63 75 72 72 65 64 20 61 66 74 hat occurred aft
12603 65 72 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f er the change-co
12604 75 6e 74 65 72 20 77 61 73 20 75 70 64 61 74 65 unter was update
12605 64 20 62 75 74 20 0a 20 20 2a 2a 20 62 65 66 6f d but . ** befo
12606 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 re the transacti
12607 6f 6e 20 77 61 73 20 63 6f 6d 6d 69 74 74 65 64 on was committed
12608 2c 20 74 68 65 6e 20 74 68 65 20 63 68 61 6e 67 , then the chang
12609 65 2d 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2a 20 e-counter . **
1260a 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 6d 61 79 modification may
1260b 20 6a 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 just have been
1260c 72 65 76 65 72 74 65 64 2e 20 49 66 20 74 68 69 reverted. If thi
1260d 73 20 68 61 70 70 65 6e 73 20 69 6e 20 65 78 63 s happens in exc
1260e 6c 75 73 69 76 65 20 0a 20 20 2a 2a 20 6d 6f 64 lusive . ** mod
1260f 65 2c 20 74 68 65 6e 20 73 75 62 73 65 71 75 65 e, then subseque
12610 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 nt transactions
12611 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 65 performed by the
12612 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c connection will
12613 20 6e 6f 74 0a 20 20 2a 2a 20 75 70 64 61 74 65 not. ** update
12614 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e the change-coun
12615 74 65 72 20 61 74 20 61 6c 6c 2e 20 54 68 69 73 ter at all. This
12616 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 63 61 63 may lead to cac
12617 68 65 20 69 6e 63 6f 6e 73 69 73 74 65 6e 63 79 he inconsistency
12618 0a 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 66 . ** problems f
12619 6f 72 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 or other process
1261a 65 73 20 61 74 20 73 6f 6d 65 20 70 6f 69 6e 74 es at some point
1261b 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e 20 in the future.
1261c 53 6f 2c 20 6a 75 73 74 0a 20 20 2a 2a 20 69 6e So, just. ** in
1261d 20 63 61 73 65 20 74 68 69 73 20 68 61 73 20 68 case this has h
1261e 61 70 70 65 6e 65 64 2c 20 63 6c 65 61 72 20 74 appened, clear t
1261f 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f he changeCountDo
12620 6e 65 20 66 6c 61 67 20 6e 6f 77 2e 0a 20 20 2a ne flag now.. *
12621 2f 0a 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e /. pPager->chan
12622 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50 geCountDone = pP
12623 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a ager->tempFile;.
12624 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
12625 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 7a 4d 61 73 E_OK ){. zMas
12626 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 ter = pPager->pT
12627 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 72 63 20 mpSpace;. rc
12628 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 = readMasterJour
12629 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c nal(pPager->jfd,
1262a 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72 zMaster, pPager
1262b 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 ->pVfs->mxPathna
1262c 6d 65 2b 31 29 3b 0a 20 20 20 20 74 65 73 74 63 me+1);. testc
1262d 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ase( rc!=SQLITE_
1262e 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 OK );. }. if(
1262f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
12630 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f . rc = pager_
12631 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 end_transaction(
12632 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 5b pPager, zMaster[
12633 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 20 20 20 74 0]!='\0');. t
12634 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c estcase( rc!=SQL
12635 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 ITE_OK );. }.
12636 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
12637 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 K && zMaster[0]
12638 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a && res ){. /*
12639 20 49 66 20 74 68 65 72 65 20 77 61 73 20 61 20 If there was a
1263a 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 master journal a
1263b 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 nd this routine
1263c 77 69 6c 6c 20 72 65 74 75 72 6e 20 73 75 63 63 will return succ
1263d 65 73 73 2c 0a 20 20 20 20 2a 2a 20 73 65 65 20 ess,. ** see
1263e 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c if it is possibl
1263f 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 e to delete the
12640 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a master journal..
12641 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 */. rc =
12642 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 pager_delmaster(
12643 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 pPager, zMaster)
12644 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
12645 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc!=SQLITE_OK );
12646 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 50 . }.. /* The P
12647 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 ager.sectorSize
12648 76 61 72 69 61 62 6c 65 20 6d 61 79 20 68 61 76 variable may hav
12649 65 20 62 65 65 6e 20 75 70 64 61 74 65 64 20 77 e been updated w
1264a 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 0a 20 20 2a hile rolling. *
1264b 2a 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c * back a journal
1264c 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 created by a pr
1264d 6f 63 65 73 73 20 77 69 74 68 20 61 20 64 69 66 ocess with a dif
1264e 66 65 72 65 6e 74 20 73 65 63 74 6f 72 20 73 69 ferent sector si
1264f 7a 65 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 52 ze. ** value. R
12650 65 73 65 74 20 69 74 20 74 6f 20 74 68 65 20 63 eset it to the c
12651 6f 72 72 65 63 74 20 76 61 6c 75 65 20 66 6f 72 orrect value for
12652 20 74 68 69 73 20 70 72 6f 63 65 73 73 2e 0a 20 this process..
12653 20 2a 2f 0a 20 20 73 65 74 53 65 63 74 6f 72 53 */. setSectorS
12654 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72 ize(pPager);. r
12655 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
12656 2a 2a 20 50 6c 61 79 62 61 63 6b 20 73 61 76 65 ** Playback save
12657 70 6f 69 6e 74 20 70 53 61 76 65 70 6f 69 6e 74 point pSavepoint
12658 2e 20 4f 72 2c 20 69 66 20 70 53 61 76 65 70 6f . Or, if pSavepo
12659 69 6e 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 int==NULL, then
1265a 70 6c 61 79 62 61 63 6b 0a 2a 2a 20 74 68 65 20 playback.** the
1265b 65 6e 74 69 72 65 20 6d 61 73 74 65 72 20 6a 6f entire master jo
1265c 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 urnal file. The
1265d 63 61 73 65 20 70 53 61 76 65 70 6f 69 6e 74 3d case pSavepoint=
1265e 3d 4e 55 4c 4c 20 6f 63 63 75 72 73 20 77 68 65 =NULL occurs whe
1265f 6e 20 0a 2a 2a 20 61 20 52 4f 4c 4c 42 41 43 4b n .** a ROLLBACK
12660 20 54 4f 20 63 6f 6d 6d 61 6e 64 20 69 73 20 69 TO command is i
12661 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 53 41 56 45 nvoked on a SAVE
12662 50 4f 49 4e 54 20 74 68 61 74 20 69 73 20 61 20 POINT that is a
12663 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 transaction .**
12664 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a savepoint..**.**
12665 20 57 68 65 6e 20 70 53 61 76 65 70 6f 69 6e 74 When pSavepoint
12666 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 28 6d 65 is not NULL (me
12667 61 6e 69 6e 67 20 61 20 6e 6f 6e 2d 74 72 61 6e aning a non-tran
12668 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e saction savepoin
12669 74 20 69 73 20 0a 2a 2a 20 62 65 69 6e 67 20 72 t is .** being r
1266a 6f 6c 6c 65 64 20 62 61 63 6b 29 2c 20 74 68 65 olled back), the
1266b 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 n the rollback c
1266c 6f 6e 73 69 73 74 73 20 6f 66 20 75 70 20 74 6f onsists of up to
1266d 20 74 68 72 65 65 20 73 74 61 67 65 73 2c 0a 2a three stages,.*
1266e 2a 20 70 65 72 66 6f 72 6d 65 64 20 69 6e 20 74 * performed in t
1266f 68 65 20 6f 72 64 65 72 20 73 70 65 63 69 66 69 he order specifi
12670 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 ed:.**.** * Pa
12671 67 65 73 20 61 72 65 20 70 6c 61 79 65 64 20 62 ges are played b
12672 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 ack from the mai
12673 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 n journal starti
12674 6e 67 20 61 74 20 62 79 74 65 0a 2a 2a 20 20 20 ng at byte.**
12675 20 20 6f 66 66 73 65 74 20 50 61 67 65 72 53 61 offset PagerSa
12676 76 65 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 vepoint.iOffset
12677 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 and continuing t
12678 6f 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 o .** PagerS
12679 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 avepoint.iHdrOff
1267a 73 65 74 2c 20 6f 72 20 74 6f 20 74 68 65 20 65 set, or to the e
1267b 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a nd of the main j
1267c 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 69 ournal.** fi
1267d 6c 65 20 69 66 20 50 61 67 65 72 53 61 76 65 70 le if PagerSavep
1267e 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 oint.iHdrOffset
1267f 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 is zero..**.**
12680 20 2a 20 49 66 20 50 61 67 65 72 53 61 76 65 70 * If PagerSavep
12681 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 oint.iHdrOffset
12682 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 is not zero, the
12683 6e 20 70 61 67 65 73 20 61 72 65 20 70 6c 61 79 n pages are play
12684 65 64 0a 2a 2a 20 20 20 20 20 62 61 63 6b 20 73 ed.** back s
12685 74 61 72 74 69 6e 67 20 66 72 6f 6d 20 74 68 65 tarting from the
12686 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
12687 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c immediately foll
12688 6f 77 69 6e 67 20 0a 2a 2a 20 20 20 20 20 50 61 owing .** Pa
12689 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 gerSavepoint.iHd
1268a 72 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 65 rOffset to the e
1268b 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a nd of the main j
1268c 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a ournal file..**.
1268d 2a 2a 20 20 20 2a 20 50 61 67 65 73 20 61 72 65 ** * Pages are
1268e 20 74 68 65 6e 20 70 6c 61 79 65 64 20 62 61 63 then played bac
1268f 6b 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a k from the sub-j
12690 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73 74 61 ournal file, sta
12691 72 74 69 6e 67 0a 2a 2a 20 20 20 20 20 77 69 74 rting.** wit
12692 68 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 h the PagerSavep
12693 6f 69 6e 74 2e 69 53 75 62 52 65 63 20 61 6e 64 oint.iSubRec and
12694 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 continuing to t
12695 68 65 20 65 6e 64 20 6f 66 0a 2a 2a 20 20 20 20 he end of.**
12696 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
12697 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 6f 75 67 68 e..**.** Through
12698 6f 75 74 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b out the rollback
12699 20 70 72 6f 63 65 73 73 2c 20 65 61 63 68 20 74 process, each t
1269a 69 6d 65 20 61 20 70 61 67 65 20 69 73 20 72 6f ime a page is ro
1269b 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 0a 2a lled back, the.*
1269c 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 * corresponding
1269d 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 61 20 bit is set in a
1269e 62 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65 bitvec structure
1269f 20 28 76 61 72 69 61 62 6c 65 20 70 44 6f 6e 65 (variable pDone
126a0 20 69 6e 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 in the.** imple
126a1 6d 65 6e 74 61 74 69 6f 6e 20 62 65 6c 6f 77 29 mentation below)
126a2 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 . This is used t
126a3 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 61 20 o ensure that a
126a4 70 61 67 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 page is only.**
126a5 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20 rolled back the
126a6 66 69 72 73 74 20 74 69 6d 65 20 69 74 20 69 73 first time it is
126a7 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 69 6e 20 encountered in
126a8 65 69 74 68 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a either journal..
126a9 2a 2a 0a 2a 2a 20 49 66 20 70 53 61 76 65 70 6f **.** If pSavepo
126aa 69 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 int is NULL, the
126ab 6e 20 70 61 67 65 73 20 61 72 65 20 6f 6e 6c 79 n pages are only
126ac 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66 72 6f played back fro
126ad 6d 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f m the main.** jo
126ae 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 urnal file. Ther
126af 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 e is no need for
126b0 20 61 20 62 69 74 76 65 63 20 69 6e 20 74 68 69 a bitvec in thi
126b1 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e s case..**.** In
126b2 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 62 65 either case, be
126b3 66 6f 72 65 20 70 6c 61 79 62 61 63 6b 20 63 6f fore playback co
126b4 6d 6d 65 6e 63 65 73 20 74 68 65 20 50 61 67 65 mmences the Page
126b5 72 2e 64 62 53 69 7a 65 20 76 61 72 69 61 62 6c r.dbSize variabl
126b6 65 0a 2a 2a 20 69 73 20 72 65 73 65 74 20 74 6f e.** is reset to
126b7 20 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 the value that
126b8 69 74 20 68 65 6c 64 20 61 74 20 74 68 65 20 73 it held at the s
126b9 74 61 72 74 20 6f 66 20 74 68 65 20 73 61 76 65 tart of the save
126ba 70 6f 69 6e 74 20 0a 2a 2a 20 28 6f 72 20 74 72 point .** (or tr
126bb 61 6e 73 61 63 74 69 6f 6e 29 2e 20 4e 6f 20 70 ansaction). No p
126bc 61 67 65 20 77 69 74 68 20 61 20 70 61 67 65 2d age with a page-
126bd 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 74 number greater t
126be 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 0a 2a han this value.*
126bf 2a 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b * is played back
126c0 2e 20 49 66 20 6f 6e 65 20 69 73 20 65 6e 63 6f . If one is enco
126c1 75 6e 74 65 72 65 64 20 69 74 20 69 73 20 73 69 untered it is si
126c2 6d 70 6c 79 20 73 6b 69 70 70 65 64 2e 0a 2a 2f mply skipped..*/
126c3 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
126c4 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 rPlaybackSavepoi
126c5 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nt(Pager *pPager
126c6 2c 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 , PagerSavepoint
126c7 20 2a 70 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 *pSavepoint){.
126c8 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 i64 szJ;
126c9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 66 66 /* Eff
126ca 65 63 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74 ective size of t
126cb 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
126cc 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 */. i64 iHdrOff
126cd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
126ce 20 45 6e 64 20 6f 66 20 66 69 72 73 74 20 73 65 End of first se
126cf 67 6d 65 6e 74 20 6f 66 20 6d 61 69 6e 2d 6a 6f gment of main-jo
126d0 75 72 6e 61 6c 20 72 65 63 6f 72 64 73 20 2a 2f urnal records */
126d1 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
126d2 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52 TE_OK; /* R
126d3 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
126d4 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 3d 20 Bitvec *pDone =
126d5 30 3b 20 20 20 20 20 20 20 2f 2a 20 42 69 74 76 0; /* Bitv
126d6 65 63 20 74 6f 20 65 6e 73 75 72 65 20 70 61 67 ec to ensure pag
126d7 65 73 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f es played back o
126d8 6e 6c 79 20 6f 6e 63 65 20 2a 2f 0a 0a 20 20 61 nly once */.. a
126d9 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
126da 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 tate>=PAGER_SHAR
126db 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f ED );.. /* Allo
126dc 63 61 74 65 20 61 20 62 69 74 76 65 63 20 74 6f cate a bitvec to
126dd 20 75 73 65 20 74 6f 20 73 74 6f 72 65 20 74 68 use to store th
126de 65 20 73 65 74 20 6f 66 20 70 61 67 65 73 20 72 e set of pages r
126df 6f 6c 6c 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 olled back */.
126e0 69 66 28 20 70 53 61 76 65 70 6f 69 6e 74 20 29 if( pSavepoint )
126e1 7b 0a 20 20 20 20 70 44 6f 6e 65 20 3d 20 73 71 {. pDone = sq
126e2 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 lite3BitvecCreat
126e3 65 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f e(pSavepoint->nO
126e4 72 69 67 29 3b 0a 20 20 20 20 69 66 28 20 21 70 rig);. if( !p
126e5 44 6f 6e 65 20 29 7b 0a 20 20 20 20 20 20 72 65 Done ){. re
126e6 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
126e7 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 M;. }. }..
126e8 2f 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62 /* Set the datab
126e9 61 73 65 20 73 69 7a 65 20 62 61 63 6b 20 74 6f ase size back to
126ea 20 74 68 65 20 76 61 6c 75 65 20 69 74 20 77 61 the value it wa
126eb 73 20 62 65 66 6f 72 65 20 74 68 65 20 73 61 76 s before the sav
126ec 65 70 6f 69 6e 74 20 0a 20 20 2a 2a 20 62 65 69 epoint . ** bei
126ed 6e 67 20 72 65 76 65 72 74 65 64 20 77 61 73 20 ng reverted was
126ee 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 opened.. */. p
126ef 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 Pager->dbSize =
126f0 70 53 61 76 65 70 6f 69 6e 74 20 3f 20 70 53 61 pSavepoint ? pSa
126f1 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 20 3a vepoint->nOrig :
126f2 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 pPager->dbOrigS
126f3 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 73 65 20 70 ize;.. /* Use p
126f4 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
126f5 66 20 61 73 20 74 68 65 20 65 66 66 65 63 74 69 f as the effecti
126f6 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6d ve size of the m
126f7 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a ain rollback. *
126f8 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 * journal. The
126f9 61 63 74 75 61 6c 20 66 69 6c 65 20 6d 69 67 68 actual file migh
126fa 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 6e t be larger than
126fb 20 74 68 69 73 20 69 6e 0a 20 20 2a 2a 20 50 41 this in. ** PA
126fc 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
126fd 54 52 55 4e 43 41 54 45 20 6f 72 20 50 41 47 45 TRUNCATE or PAGE
126fe 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 R_JOURNALMODE_PE
126ff 52 53 49 53 54 2e 20 20 42 75 74 20 61 6e 79 74 RSIST. But anyt
12700 68 69 6e 67 0a 20 20 2a 2a 20 70 61 73 74 20 70 hing. ** past p
12701 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
12702 66 20 69 73 20 6f 66 66 2d 6c 69 6d 69 74 73 20 f is off-limits
12703 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a 20 20 73 7a to us.. */. sz
12704 4a 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 J = pPager->jour
12705 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 42 65 nalOff;.. /* Be
12706 67 69 6e 20 62 79 20 72 6f 6c 6c 69 6e 67 20 62 gin by rolling b
12707 61 63 6b 20 72 65 63 6f 72 64 73 20 66 72 6f 6d ack records from
12708 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
12709 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20 20 l starting at.
1270a 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e ** PagerSavepoin
1270b 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 63 6f t.iOffset and co
1270c 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 20 ntinuing to the
1270d 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 next journal hea
1270e 64 65 72 2e 0a 20 20 2a 2a 20 54 68 65 72 65 20 der.. ** There
1270f 6d 69 67 68 74 20 62 65 20 72 65 63 6f 72 64 73 might be records
12710 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 in the main jou
12711 72 6e 61 6c 20 74 68 61 74 20 68 61 76 65 20 61 rnal that have a
12712 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a page number. *
12713 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 * greater than t
12714 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 62 he current datab
12715 61 73 65 20 73 69 7a 65 20 28 70 50 61 67 65 72 ase size (pPager
12716 2d 3e 64 62 53 69 7a 65 29 20 62 75 74 20 74 68 ->dbSize) but th
12717 6f 73 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 ose. ** will be
12718 20 73 6b 69 70 70 65 64 20 61 75 74 6f 6d 61 74 skipped automat
12719 69 63 61 6c 6c 79 2e 20 20 50 61 67 65 73 20 61 ically. Pages a
1271a 72 65 20 61 64 64 65 64 20 74 6f 20 70 44 6f 6e re added to pDon
1271b 65 20 61 73 20 74 68 65 79 0a 20 20 2a 2a 20 61 e as they. ** a
1271c 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a re played back..
1271d 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65 */. if( pSave
1271e 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 48 64 point ){. iHd
1271f 72 4f 66 66 20 3d 20 70 53 61 76 65 70 6f 69 6e rOff = pSavepoin
12720 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 20 3f 20 t->iHdrOffset ?
12721 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72 pSavepoint->iHdr
12722 4f 66 66 73 65 74 20 3a 20 73 7a 4a 3b 0a 20 20 Offset : szJ;.
12723 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
12724 6c 4f 66 66 20 3d 20 70 53 61 76 65 70 6f 69 6e lOff = pSavepoin
12725 74 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 t->iOffset;.
12726 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 while( rc==SQLIT
12727 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e E_OK && pPager->
12728 6a 6f 75 72 6e 61 6c 4f 66 66 3c 69 48 64 72 4f journalOff<iHdrO
12729 66 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d ff ){. rc =
1272a 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f pager_playback_
1272b 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c one_page(pPager,
1272c 20 31 2c 20 30 2c 20 26 70 50 61 67 65 72 2d 3e 1, 0, &pPager->
1272d 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 journalOff, 1, p
1272e 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Done);. }.
1272f 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c assert( rc!=SQL
12730 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 65 ITE_DONE );. }e
12731 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 72 2d lse{. pPager-
12732 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b >journalOff = 0;
12733 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 74 69 . }.. /* Conti
12734 6e 75 65 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b nue rolling back
12735 20 72 65 63 6f 72 64 73 20 6f 75 74 20 6f 66 20 records out of
12736 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c the main journal
12737 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20 20 2a starting at. *
12738 2a 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72 * the first jour
12739 6e 61 6c 20 68 65 61 64 65 72 20 73 65 65 6e 20 nal header seen
1273a 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 75 and continuing u
1273b 6e 74 69 6c 20 74 68 65 20 65 66 66 65 63 74 69 ntil the effecti
1273c 76 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 ve end. ** of t
1273d 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
1273e 66 69 6c 65 2e 20 20 43 6f 6e 74 69 6e 75 65 20 file. Continue
1273f 74 6f 20 73 6b 69 70 20 6f 75 74 2d 6f 66 2d 72 to skip out-of-r
12740 61 6e 67 65 20 70 61 67 65 73 20 61 6e 64 0a 20 ange pages and.
12741 20 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 61 64 64 ** continue add
12742 69 6e 67 20 70 61 67 65 73 20 72 6f 6c 6c 65 64 ing pages rolled
12743 20 62 61 63 6b 20 74 6f 20 70 44 6f 6e 65 2e 0a back to pDone..
12744 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 72 63 */. while( rc
12745 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
12746 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
12747 66 3c 73 7a 4a 20 29 7b 0a 20 20 20 20 75 33 32 f<szJ ){. u32
12748 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 ii;
12749 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
1274a 2a 2f 0a 20 20 20 20 75 33 32 20 6e 4a 52 65 63 */. u32 nJRec
1274b 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d = 0; /* Num
1274c 62 65 72 20 6f 66 20 4a 6f 75 72 6e 61 6c 20 52 ber of Journal R
1274d 65 63 6f 72 64 73 20 2a 2f 0a 20 20 20 20 75 33 ecords */. u3
1274e 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 72 63 20 2 dummy;. rc
1274f 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 = readJournalHdr
12750 28 70 50 61 67 65 72 2c 20 30 2c 20 73 7a 4a 2c (pPager, 0, szJ,
12751 20 26 6e 4a 52 65 63 2c 20 26 64 75 6d 6d 79 29 &nJRec, &dummy)
12752 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 ;. assert( rc
12753 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b !=SQLITE_DONE );
12754 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 .. /*. **
12755 54 68 65 20 22 70 50 61 67 65 72 2d 3e 6a 6f 75 The "pPager->jou
12756 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f rnalHdr+JOURNAL_
12757 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d HDR_SZ(pPager)==
12758 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
12759 66 66 22 0a 20 20 20 20 2a 2a 20 74 65 73 74 20 ff". ** test
1275a 69 73 20 72 65 6c 61 74 65 64 20 74 6f 20 74 69 is related to ti
1275b 63 6b 65 74 20 23 32 35 36 35 2e 20 20 53 65 65 cket #2565. See
1275c 20 74 68 65 20 64 69 73 63 75 73 73 69 6f 6e 20 the discussion
1275d 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 61 in the. ** pa
1275e 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 66 ger_playback() f
1275f 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 unction for addi
12760 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
12761 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 on.. */. i
12762 66 28 20 6e 4a 52 65 63 3d 3d 30 20 0a 20 20 20 f( nJRec==0 .
12763 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 && pPager->jou
12764 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f rnalHdr+JOURNAL_
12765 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d HDR_SZ(pPager)==
12766 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
12767 66 66 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 ff. ){.
12768 6e 4a 52 65 63 20 3d 20 28 75 33 32 29 28 28 73 nJRec = (u32)((s
12769 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 zJ - pPager->jou
1276a 72 6e 61 6c 4f 66 66 29 2f 4a 4f 55 52 4e 41 4c rnalOff)/JOURNAL
1276b 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3b _PG_SZ(pPager));
1276c 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 . }. for(i
1276d 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f i=0; rc==SQLITE_
1276e 4f 4b 20 26 26 20 69 69 3c 6e 4a 52 65 63 20 26 OK && ii<nJRec &
1276f 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 & pPager->journa
12770 6c 4f 66 66 3c 73 7a 4a 3b 20 69 69 2b 2b 29 7b lOff<szJ; ii++){
12771 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
12772 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 r_playback_one_p
12773 61 67 65 28 70 50 61 67 65 72 2c 20 31 2c 20 30 age(pPager, 1, 0
12774 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e , &pPager->journ
12775 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e 65 29 alOff, 1, pDone)
12776 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
12777 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 rt( rc!=SQLITE_D
12778 4f 4e 45 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 ONE );. }. ass
12779 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ert( rc!=SQLITE_
1277a 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f OK || pPager->jo
1277b 75 72 6e 61 6c 4f 66 66 3d 3d 73 7a 4a 20 29 3b urnalOff==szJ );
1277c 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 .. /* Finally,
1277d 20 72 6f 6c 6c 62 61 63 6b 20 70 61 67 65 73 20 rollback pages
1277e 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 from the sub-jou
1277f 72 6e 61 6c 2e 20 20 50 61 67 65 20 74 68 61 74 rnal. Page that
12780 20 77 65 72 65 0a 20 20 2a 2a 20 70 72 65 76 69 were. ** previ
12781 6f 75 73 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 ously rolled bac
12782 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 69 k out of the mai
12783 6e 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 61 n journal (and a
12784 72 65 20 68 65 6e 63 65 20 69 6e 20 70 44 6f 6e re hence in pDon
12785 65 29 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 e). ** will be
12786 73 6b 69 70 70 65 64 2e 20 20 4f 75 74 2d 6f 66 skipped. Out-of
12787 2d 72 61 6e 67 65 20 70 61 67 65 73 20 61 72 65 -range pages are
12788 20 61 6c 73 6f 20 73 6b 69 70 70 65 64 2e 0a 20 also skipped..
12789 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65 70 */. if( pSavep
1278a 6f 69 6e 74 20 29 7b 0a 20 20 20 20 75 33 32 20 oint ){. u32
1278b 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ii; /
1278c 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
1278d 2f 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 /. i64 offset
1278e 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 = pSavepoint->i
1278f 53 75 62 52 65 63 2a 28 34 2b 70 50 61 67 65 72 SubRec*(4+pPager
12790 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 ->pageSize);.
12791 20 66 6f 72 28 69 69 3d 70 53 61 76 65 70 6f 69 for(ii=pSavepoi
12792 6e 74 2d 3e 69 53 75 62 52 65 63 3b 20 72 63 3d nt->iSubRec; rc=
12793 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 =SQLITE_OK && ii
12794 3c 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 <pPager->nSubRec
12795 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 ; ii++){. a
12796 73 73 65 72 74 28 20 6f 66 66 73 65 74 3d 3d 69 ssert( offset==i
12797 69 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 i*(4+pPager->pag
12798 65 53 69 7a 65 29 20 29 3b 0a 20 20 20 20 20 20 eSize) );.
12799 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 rc = pager_playb
1279a 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 ack_one_page(pPa
1279b 67 65 72 2c 20 30 2c 20 30 2c 20 26 6f 66 66 73 ger, 0, 0, &offs
1279c 65 74 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 et, 1, pDone);.
1279d 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1279e 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 rc!=SQLITE_DONE
1279f 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 );. }.. sqlit
127a0 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
127a1 70 44 6f 6e 65 29 3b 0a 20 20 69 66 28 20 72 63 pDone);. if( rc
127a2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
127a3 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
127a4 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 7d alOff = szJ;. }
127a5 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
127a6 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
127a7 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
127a8 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 of in-memory pa
127a9 67 65 73 20 74 68 61 74 20 61 72 65 20 61 6c 6c ges that are all
127aa 6f 77 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f owed..*/.SQLITE_
127ab 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
127ac 69 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68 ite3PagerSetCach
127ad 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 esize(Pager *pPa
127ae 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 ger, int mxPage)
127af 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 {. sqlite3Pcach
127b0 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 70 50 eSetCachesize(pP
127b1 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 6d ager->pPCache, m
127b2 78 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a xPage);.}../*.**
127b3 20 41 64 6a 75 73 74 20 74 68 65 20 72 6f 62 75 Adjust the robu
127b4 73 74 6e 65 73 73 20 6f 66 20 74 68 65 20 64 61 stness of the da
127b5 74 61 62 61 73 65 20 74 6f 20 64 61 6d 61 67 65 tabase to damage
127b6 20 64 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68 due to OS crash
127b7 65 73 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 es.** or power f
127b8 61 69 6c 75 72 65 73 20 62 79 20 63 68 61 6e 67 ailures by chang
127b9 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ing the number o
127ba 66 20 73 79 6e 63 73 28 29 73 20 77 68 65 6e 20 f syncs()s when
127bb 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 72 writing.** the r
127bc 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e ollback journal.
127bd 20 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 There are thre
127be 65 20 6c 65 76 65 6c 73 3a 0a 2a 2a 0a 2a 2a 20 e levels:.**.**
127bf 20 20 20 4f 46 46 20 20 20 20 20 20 20 73 71 6c OFF sql
127c0 69 74 65 33 4f 73 53 79 6e 63 28 29 20 69 73 20 ite3OsSync() is
127c1 6e 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 20 54 never called. T
127c2 68 69 73 20 69 73 20 74 68 65 20 64 65 66 61 75 his is the defau
127c3 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 lt.**
127c4 20 20 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 for temporary
127c5 20 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 20 66 and transient f
127c6 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e iles..**.** N
127c7 4f 52 4d 41 4c 20 20 20 20 54 68 65 20 6a 6f 75 ORMAL The jou
127c8 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 6f rnal is synced o
127c9 6e 63 65 20 62 65 66 6f 72 65 20 77 72 69 74 65 nce before write
127ca 73 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a s begin on the.*
127cb 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 * d
127cc 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 69 atabase. This i
127cd 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 65 71 75 s normally adequ
127ce 61 74 65 20 70 72 6f 74 65 63 74 69 6f 6e 2c 20 ate protection,
127cf 62 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 but.**
127d0 20 20 20 20 69 74 20 69 73 20 74 68 65 6f 72 65 it is theore
127d1 74 69 63 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 tically possible
127d2 2c 20 74 68 6f 75 67 68 20 76 65 72 79 20 75 6e , though very un
127d3 6c 69 6b 65 6c 79 2c 0a 2a 2a 20 20 20 20 20 20 likely,.**
127d4 20 20 20 20 20 20 20 20 74 68 61 74 20 61 6e 20 that an
127d5 69 6e 6f 70 65 72 74 75 6e 65 20 70 6f 77 65 72 inopertune power
127d6 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c failure could l
127d7 65 61 76 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c eave the journal
127d8 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
127d9 20 69 6e 20 61 20 73 74 61 74 65 20 77 68 69 63 in a state whic
127da 68 20 77 6f 75 6c 64 20 63 61 75 73 65 20 64 61 h would cause da
127db 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 mage to the data
127dc 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 base.**
127dd 20 20 20 20 20 77 68 65 6e 20 69 74 20 69 73 20 when it is
127de 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a rolled back..**.
127df 2a 2a 20 20 20 20 46 55 4c 4c 20 20 20 20 20 20 ** FULL
127e0 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 The journal is s
127e1 79 6e 63 65 64 20 74 77 69 63 65 20 62 65 66 6f ynced twice befo
127e2 72 65 20 77 72 69 74 65 73 20 62 65 67 69 6e 20 re writes begin
127e3 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 on the.**
127e4 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 database
127e5 28 77 69 74 68 20 73 6f 6d 65 20 61 64 64 69 74 (with some addit
127e6 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
127e7 6e 20 2d 20 74 68 65 20 6e 52 65 63 20 66 69 65 n - the nRec fie
127e8 6c 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ld.**
127e9 20 20 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 of the journa
127ea 6c 20 68 65 61 64 65 72 20 2d 20 62 65 69 6e 67 l header - being
127eb 20 77 72 69 74 74 65 6e 20 69 6e 20 62 65 74 77 written in betw
127ec 65 65 6e 20 74 68 65 20 74 77 6f 0a 2a 2a 20 20 een the two.**
127ed 20 20 20 20 20 20 20 20 20 20 20 20 73 79 6e 63 sync
127ee 73 29 2e 20 20 49 66 20 77 65 20 61 73 73 75 6d s). If we assum
127ef 65 20 74 68 61 74 20 77 72 69 74 69 6e 67 20 61 e that writing a
127f0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
127f1 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 single disk sec
127f2 74 6f 72 20 69 73 20 61 74 6f 6d 69 63 2c 20 74 tor is atomic, t
127f3 68 65 6e 20 74 68 69 73 20 6d 6f 64 65 20 70 72 hen this mode pr
127f4 6f 76 69 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 ovides.**
127f5 20 20 20 20 20 20 20 61 73 73 75 72 61 6e 63 65 assurance
127f6 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 that the journa
127f7 6c 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f l will not be co
127f8 72 72 75 70 74 65 64 20 74 6f 20 74 68 65 0a 2a rrupted to the.*
127f9 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 * p
127fa 6f 69 6e 74 20 6f 66 20 63 61 75 73 69 6e 67 20 oint of causing
127fb 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 damage to the da
127fc 74 61 62 61 73 65 20 64 75 72 69 6e 67 20 72 6f tabase during ro
127fd 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4e 75 llback..**.** Nu
127fe 6d 65 72 69 63 20 76 61 6c 75 65 73 20 61 73 73 meric values ass
127ff 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
12800 73 65 20 73 74 61 74 65 73 20 61 72 65 20 4f 46 se states are OF
12801 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c 0a F==1, NORMAL=2,.
12802 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a 2a ** and FULL=3..*
12803 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
12804 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 _OMIT_PAGER_PRAG
12805 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 MAS.SQLITE_PRIVA
12806 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
12807 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 65 76 agerSetSafetyLev
12808 65 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 el(Pager *pPager
12809 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 , int level, int
1280a 20 62 46 75 6c 6c 46 73 79 6e 63 29 7b 0a 20 20 bFullFsync){.
1280b 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d pPager->noSync =
1280c 20 20 28 6c 65 76 65 6c 3d 3d 31 20 7c 7c 20 70 (level==1 || p
1280d 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 Pager->tempFile)
1280e 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d ?1:0;. pPager-
1280f 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28 6c 65 76 >fullSync = (lev
12810 65 6c 3d 3d 33 20 26 26 20 21 70 50 61 67 65 72 el==3 && !pPager
12811 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f 31 3a 30 ->tempFile) ?1:0
12812 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 ;. pPager->sync
12813 5f 66 6c 61 67 73 20 3d 20 28 62 46 75 6c 6c 46 _flags = (bFullF
12814 73 79 6e 63 3f 53 51 4c 49 54 45 5f 53 59 4e 43 sync?SQLITE_SYNC
12815 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 5f 53 59 4e _FULL:SQLITE_SYN
12816 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 69 66 28 C_NORMAL);. if(
12817 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 pPager->noSync
12818 29 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 ) pPager->needSy
12819 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 nc = 0;.}.#endif
1281a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
1281b 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 owing global var
1281c 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 iable is increme
1281d 6e 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 nted whenever th
1281e 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74 74 e library.** att
1281f 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20 61 20 empts to open a
12820 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 temporary file.
12821 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f This informatio
12822 6e 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a n is used for.**
12823 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 testing and ana
12824 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a 2f lysis only. .*/
12825 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
12826 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
12827 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 nt sqlite3_opent
12828 65 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 emp_count = 0;.#
12829 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 endif../*.** Ope
1282a 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 n a temporary fi
1282b 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 le..**.** Write
1282c 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
1282d 74 6f 72 20 69 6e 74 6f 20 2a 70 46 69 6c 65 2e tor into *pFile.
1282e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
1282f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 0a 2a 2a K on success .**
12830 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 or some other e
12831 72 72 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 rror code if we
12832 66 61 69 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c fail. The OS wil
12833 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 l automatically
12834 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 74 .** delete the t
12835 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 77 68 emporary file wh
12836 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e en it is closed.
12837 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 .**.** The flags
12838 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 56 passed to the V
12839 46 53 20 6c 61 79 65 72 20 78 4f 70 65 6e 28 29 FS layer xOpen()
1283a 20 63 61 6c 6c 20 61 72 65 20 74 68 6f 73 65 20 call are those
1283b 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 specified.** by
1283c 70 61 72 61 6d 65 74 65 72 20 76 66 73 46 6c 61 parameter vfsFla
1283d 67 73 20 4f 52 65 64 20 77 69 74 68 20 74 68 65 gs ORed with the
1283e 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
1283f 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 * SQLITE_OPE
12840 4e 5f 52 45 41 44 57 52 49 54 45 0a 2a 2a 20 20 N_READWRITE.**
12841 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 SQLITE_OPEN_C
12842 52 45 41 54 45 0a 2a 2a 20 20 20 20 20 53 51 4c REATE.** SQL
12843 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 ITE_OPEN_EXCLUSI
12844 56 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 VE.** SQLITE
12845 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
12846 4f 53 45 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e OSE.*/.static in
12847 74 20 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 t pagerOpentemp(
12848 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
12849 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 , /* The
1284a 70 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a pager object */.
1284b 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
1284c 70 46 69 6c 65 2c 20 20 2f 2a 20 57 72 69 74 65 pFile, /* Write
1284d 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
1284e 70 74 6f 72 20 68 65 72 65 20 2a 2f 0a 20 20 69 ptor here */. i
1284f 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 20 20 nt vfsFlags
12850 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 /* Flags pa
12851 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 ssed through to
12852 74 68 65 20 56 46 53 20 2a 2f 0a 29 7b 0a 20 20 the VFS */.){.
12853 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
12854 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
12855 63 6f 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20 code */..#ifdef
12856 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71 SQLITE_TEST. sq
12857 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 lite3_opentemp_c
12858 6f 75 6e 74 2b 2b 3b 20 20 2f 2a 20 55 73 65 64 ount++; /* Used
12859 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 for testing and
1285a 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 20 2a analysis only *
1285b 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 76 66 73 46 /.#endif.. vfsF
1285c 6c 61 67 73 20 7c 3d 20 20 53 51 4c 49 54 45 5f lags |= SQLITE_
1285d 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c OPEN_READWRITE |
1285e 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 SQLITE_OPEN_CRE
1285f 41 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 ATE |.
12860 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 SQLITE_OPEN_EX
12861 43 4c 55 53 49 56 45 20 7c 20 53 51 4c 49 54 45 CLUSIVE | SQLITE
12862 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
12863 4f 53 45 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 OSE;. rc = sqli
12864 74 65 33 4f 73 4f 70 65 6e 28 70 50 61 67 65 72 te3OsOpen(pPager
12865 2d 3e 70 56 66 73 2c 20 30 2c 20 70 46 69 6c 65 ->pVfs, 0, pFile
12866 2c 20 76 66 73 46 6c 61 67 73 2c 20 30 29 3b 0a , vfsFlags, 0);.
12867 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 assert( rc!=SQ
12868 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 LITE_OK || isOpe
12869 6e 28 70 46 69 6c 65 29 20 29 3b 0a 20 20 72 65 n(pFile) );. re
1286a 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1286b 2a 20 53 65 74 20 74 68 65 20 62 75 73 79 20 68 * Set the busy h
1286c 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 2e andler function.
1286d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 .**.** The pager
1286e 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 62 75 73 invokes the bus
1286f 79 2d 68 61 6e 64 6c 65 72 20 69 66 20 73 71 6c y-handler if sql
12870 69 74 65 33 4f 73 4c 6f 63 6b 28 29 20 72 65 74 ite3OsLock() ret
12871 75 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f urns .** SQLITE_
12872 42 55 53 59 20 77 68 65 6e 20 74 72 79 69 6e 67 BUSY when trying
12873 20 74 6f 20 75 70 67 72 61 64 65 20 66 72 6f 6d to upgrade from
12874 20 6e 6f 2d 6c 6f 63 6b 20 74 6f 20 61 20 53 48 no-lock to a SH
12875 41 52 45 44 20 6c 6f 63 6b 2c 0a 2a 2a 20 6f 72 ARED lock,.** or
12876 20 77 68 65 6e 20 74 72 79 69 6e 67 20 74 6f 20 when trying to
12877 75 70 67 72 61 64 65 20 66 72 6f 6d 20 61 20 52 upgrade from a R
12878 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20 ESERVED lock to
12879 61 6e 20 45 58 43 4c 55 53 49 56 45 20 0a 2a 2a an EXCLUSIVE .**
1287a 20 6c 6f 63 6b 2e 20 49 74 20 64 6f 65 73 20 2a lock. It does *
1287b 6e 6f 74 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 not* invoke the
1287c 62 75 73 79 20 68 61 6e 64 6c 65 72 20 77 68 65 busy handler whe
1287d 6e 20 75 70 67 72 61 64 69 6e 67 20 66 72 6f 6d n upgrading from
1287e 0a 2a 2a 20 53 48 41 52 45 44 20 74 6f 20 52 45 .** SHARED to RE
1287f 53 45 52 56 45 44 2c 20 6f 72 20 77 68 65 6e 20 SERVED, or when
12880 75 70 67 72 61 64 69 6e 67 20 66 72 6f 6d 20 53 upgrading from S
12881 48 41 52 45 44 20 74 6f 20 45 58 43 4c 55 53 49 HARED to EXCLUSI
12882 56 45 0a 2a 2a 20 28 77 68 69 63 68 20 6f 63 63 VE.** (which occ
12883 75 72 73 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a urs during hot-j
12884 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 ournal rollback)
12885 2e 20 53 75 6d 6d 61 72 79 3a 0a 2a 2a 0a 2a 2a . Summary:.**.**
12886 20 20 20 54 72 61 6e 73 69 74 69 6f 6e 20 20 20 Transition
12887 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12888 20 20 20 20 20 7c 20 49 6e 76 6f 6b 65 73 20 78 | Invokes x
12889 42 75 73 79 48 61 6e 64 6c 65 72 0a 2a 2a 20 20 BusyHandler.**
1288a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
1288b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1288c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1288d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 4e ---------.** N
1288e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20 2d 3e 20 O_LOCK ->
1288f 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 SHARED_LOCK
12890 20 7c 20 59 65 73 0a 2a 2a 20 20 20 53 48 41 52 | Yes.** SHAR
12891 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 52 45 53 ED_LOCK -> RES
12892 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 20 7c 20 ERVED_LOCK |
12893 4e 6f 0a 2a 2a 20 20 20 53 48 41 52 45 44 5f 4c No.** SHARED_L
12894 4f 43 4b 20 20 20 2d 3e 20 45 58 43 4c 55 53 49 OCK -> EXCLUSI
12895 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 4e 6f 0a 2a VE_LOCK | No.*
12896 2a 20 20 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 * RESERVED_LOC
12897 4b 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 5f 4c K -> EXCLUSIVE_L
12898 4f 43 4b 20 20 20 7c 20 59 65 73 0a 2a 2a 0a 2a OCK | Yes.**.*
12899 2a 20 49 66 20 74 68 65 20 62 75 73 79 2d 68 61 * If the busy-ha
1289a 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 ndler callback r
1289b 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c eturns non-zero,
1289c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a the lock is .**
1289d 20 72 65 74 72 69 65 64 2e 20 49 66 20 69 74 20 retried. If it
1289e 72 65 74 75 72 6e 73 20 7a 65 72 6f 2c 20 74 68 returns zero, th
1289f 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 42 55 en the SQLITE_BU
128a0 53 59 20 65 72 72 6f 72 20 69 73 0a 2a 2a 20 72 SY error is.** r
128a1 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 eturned to the c
128a2 61 6c 6c 65 72 20 6f 66 20 74 68 65 20 70 61 67 aller of the pag
128a3 65 72 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e er API function.
128a4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
128a5 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
128a6 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c agerSetBusyhandl
128a7 65 72 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 er(. Pager *pPa
128a8 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 ger,
128a9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
128aa 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 ger object */.
128ab 69 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c int (*xBusyHandl
128ac 65 72 29 28 76 6f 69 64 20 2a 29 2c 20 20 20 20 er)(void *),
128ad 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
128ae 74 6f 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 to busy-handler
128af 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f function */. vo
128b0 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 id *pBusyHandler
128b1 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20 Arg
128b2 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 /* Argument t
128b3 6f 20 70 61 73 73 20 74 6f 20 78 42 75 73 79 48 o pass to xBusyH
128b4 61 6e 64 6c 65 72 20 2a 2f 0a 29 7b 20 20 0a 20 andler */.){ .
128b5 20 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 pPager->xBusyHa
128b6 6e 64 6c 65 72 20 3d 20 78 42 75 73 79 48 61 6e ndler = xBusyHan
128b7 64 6c 65 72 3b 0a 20 20 70 50 61 67 65 72 2d 3e dler;. pPager->
128b8 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 20 pBusyHandlerArg
128b9 3d 20 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 = pBusyHandlerAr
128ba 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f g;.}../*.** Repo
128bb 72 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 rt the current p
128bc 61 67 65 20 73 69 7a 65 20 61 6e 64 20 6e 75 6d age size and num
128bd 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20 ber of reserved
128be 62 79 74 65 73 20 62 61 63 6b 0a 2a 2a 20 74 6f bytes back.** to
128bf 20 74 68 65 20 63 6f 64 65 63 2e 0a 2a 2f 0a 23 the codec..*/.#
128c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 ifdef SQLITE_HAS
128c1 5f 43 4f 44 45 43 0a 73 74 61 74 69 63 20 76 6f _CODEC.static vo
128c2 69 64 20 70 61 67 65 72 52 65 70 6f 72 74 53 69 id pagerReportSi
128c3 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ze(Pager *pPager
128c4 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d ){. if( pPager-
128c5 3e 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 20 >xCodecSizeChng
128c6 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 78 ){. pPager->x
128c7 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 28 70 50 CodecSizeChng(pP
128c8 61 67 65 72 2d 3e 70 43 6f 64 65 63 2c 20 70 50 ager->pCodec, pP
128c9 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a ager->pageSize,.
128ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
128cb 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29 (int)
128cc 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 pPager->nReserve
128cd 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 );. }.}.#else.#
128ce 20 64 65 66 69 6e 65 20 70 61 67 65 72 52 65 70 define pagerRep
128cf 6f 72 74 53 69 7a 65 28 58 29 20 20 20 20 20 2f ortSize(X) /
128d0 2a 20 4e 6f 2d 6f 70 20 69 66 20 77 65 20 64 6f * No-op if we do
128d1 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 61 20 63 not support a c
128d2 6f 64 65 63 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a odec */.#endif..
128d3 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
128d4 20 70 61 67 65 20 73 69 7a 65 20 75 73 65 64 20 page size used
128d5 62 79 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a by the Pager obj
128d6 65 63 74 2e 20 54 68 65 20 6e 65 77 20 70 61 67 ect. The new pag
128d7 65 20 73 69 7a 65 20 0a 2a 2a 20 69 73 20 70 61 e size .** is pa
128d8 73 73 65 64 20 69 6e 20 2a 70 50 61 67 65 53 69 ssed in *pPageSi
128d9 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ze..**.** If the
128da 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65 pager is in the
128db 20 65 72 72 6f 72 20 73 74 61 74 65 20 77 68 65 error state whe
128dc 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
128dd 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 0a 2a 2a is called, it.**
128de 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 54 68 65 is a no-op. The
128df 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
128e0 69 73 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 is the error sta
128e1 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 28 69 te error code (i
128e2 2e 65 2e 20 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 .e. .** one of S
128e3 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c QLITE_IOERR, SQL
128e4 49 54 45 5f 43 4f 52 52 55 50 54 20 6f 72 20 53 ITE_CORRUPT or S
128e5 51 4c 49 54 45 5f 46 55 4c 4c 29 2e 0a 2a 2a 0a QLITE_FULL)..**.
128e6 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 ** Otherwise, if
128e7 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c all of the foll
128e8 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0a owing are true:.
128e9 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 20 6e 65 **.** * the ne
128ea 77 20 70 61 67 65 20 73 69 7a 65 20 28 76 61 6c w page size (val
128eb 75 65 20 6f 66 20 2a 70 50 61 67 65 53 69 7a 65 ue of *pPageSize
128ec 29 20 69 73 20 76 61 6c 69 64 20 28 61 20 70 6f ) is valid (a po
128ed 77 65 72 20 0a 2a 2a 20 20 20 20 20 6f 66 20 74 wer .** of t
128ee 77 6f 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 wo between 512 a
128ef 6e 64 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 nd SQLITE_MAX_PA
128f0 47 45 5f 53 49 5a 45 2c 20 69 6e 63 6c 75 73 69 GE_SIZE, inclusi
128f1 76 65 29 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 ve), and.**.**
128f2 20 2a 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 * there are no
128f3 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 outstanding page
128f4 20 72 65 66 65 72 65 6e 63 65 73 2c 20 61 6e 64 references, and
128f5 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 .**.** * the d
128f6 61 74 61 62 61 73 65 20 69 73 20 65 69 74 68 65 atabase is eithe
128f7 72 20 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f r not an in-memo
128f8 72 79 20 64 61 74 61 62 61 73 65 20 6f 72 20 69 ry database or i
128f9 74 20 69 73 0a 2a 2a 20 20 20 20 20 61 6e 20 69 t is.** an i
128fa 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
128fb 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 e that currently
128fc 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 7a 65 72 consists of zer
128fd 6f 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 74 o pages..**.** t
128fe 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 6f 62 hen the pager ob
128ff 6a 65 63 74 20 70 61 67 65 20 73 69 7a 65 20 69 ject page size i
12900 73 20 73 65 74 20 74 6f 20 2a 70 50 61 67 65 53 s set to *pPageS
12901 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ize..**.** If th
12902 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 63 e page size is c
12903 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 74 68 69 hanged, then thi
12904 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 73 20 s function uses
12905 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61 6c 6c sqlite3PagerMall
12906 6f 63 28 29 20 0a 2a 2a 20 74 6f 20 6f 62 74 61 oc() .** to obta
12907 69 6e 20 61 20 6e 65 77 20 50 61 67 65 72 2e 70 in a new Pager.p
12908 54 6d 70 53 70 61 63 65 20 62 75 66 66 65 72 2e TmpSpace buffer.
12909 20 49 66 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 If this allocat
1290a 69 6f 6e 20 61 74 74 65 6d 70 74 20 0a 2a 2a 20 ion attempt .**
1290b 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f fails, SQLITE_NO
1290c 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20 MEM is returned
1290d 61 6e 64 20 74 68 65 20 70 61 67 65 20 73 69 7a and the page siz
1290e 65 20 72 65 6d 61 69 6e 73 20 75 6e 63 68 61 6e e remains unchan
1290f 67 65 64 2e 20 0a 2a 2a 20 49 6e 20 61 6c 6c 20 ged. .** In all
12910 6f 74 68 65 72 20 63 61 73 65 73 2c 20 53 51 4c other cases, SQL
12911 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
12912 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
12913 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f page size is no
12914 74 20 63 68 61 6e 67 65 64 2c 20 65 69 74 68 65 t changed, eithe
12915 72 20 62 65 63 61 75 73 65 20 6f 6e 65 20 6f 66 r because one of
12916 20 74 68 65 20 65 6e 75 6d 65 72 61 74 65 64 0a the enumerated.
12917 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62 ** conditions ab
12918 6f 76 65 20 69 73 20 6e 6f 74 20 74 72 75 65 2c ove is not true,
12919 20 74 68 65 20 70 61 67 65 72 20 77 61 73 20 69 the pager was i
1291a 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 77 68 n error state wh
1291b 65 6e 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 en this.** funct
1291c 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 ion was called,
1291d 6f 72 20 62 65 63 61 75 73 65 20 74 68 65 20 6d or because the m
1291e 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
1291f 20 61 74 74 65 6d 70 74 20 66 61 69 6c 65 64 2c attempt failed,
12920 20 0a 2a 2a 20 74 68 65 6e 20 2a 70 50 61 67 65 .** then *pPage
12921 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 74 Size is set to t
12922 68 65 20 6f 6c 64 2c 20 72 65 74 61 69 6e 65 64 he old, retained
12923 20 70 61 67 65 20 73 69 7a 65 20 62 65 66 6f 72 page size befor
12924 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a e returning..*/.
12925 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
12926 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 nt sqlite3PagerS
12927 65 74 50 61 67 65 73 69 7a 65 28 50 61 67 65 72 etPagesize(Pager
12928 20 2a 70 50 61 67 65 72 2c 20 75 31 36 20 2a 70 *pPager, u16 *p
12929 50 61 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 PageSize, int nR
1292a 65 73 65 72 76 65 29 7b 0a 20 20 69 6e 74 20 72 eserve){. int r
1292b 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 c = pPager->errC
1292c 6f 64 65 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d ode;.. if( rc==
1292d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1292e 20 75 31 36 20 70 61 67 65 53 69 7a 65 20 3d 20 u16 pageSize =
1292f 2a 70 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 *pPageSize;.
12930 61 73 73 65 72 74 28 20 70 61 67 65 53 69 7a 65 assert( pageSize
12931 3d 3d 30 20 7c 7c 20 28 70 61 67 65 53 69 7a 65 ==0 || (pageSize
12932 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53 69 7a >=512 && pageSiz
12933 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 e<=SQLITE_MAX_PA
12934 47 45 5f 53 49 5a 45 29 20 29 3b 0a 20 20 20 20 GE_SIZE) );.
12935 69 66 28 20 28 70 50 61 67 65 72 2d 3e 6d 65 6d if( (pPager->mem
12936 44 62 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d Db==0 || pPager-
12937 3e 64 62 53 69 7a 65 3d 3d 30 29 0a 20 20 20 20 >dbSize==0).
12938 20 26 26 20 73 71 6c 69 74 65 33 50 63 61 63 68 && sqlite3Pcach
12939 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 eRefCount(pPager
1293a 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 0a 20 ->pPCache)==0 .
1293b 20 20 20 20 26 26 20 70 61 67 65 53 69 7a 65 20 && pageSize
1293c 26 26 20 70 61 67 65 53 69 7a 65 21 3d 70 50 61 && pageSize!=pPa
1293d 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 0a 20 ger->pageSize .
1293e 20 20 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 ){. char
1293f 20 2a 70 4e 65 77 20 3d 20 28 63 68 61 72 20 2a *pNew = (char *
12940 29 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c )sqlite3PageMall
12941 6f 63 28 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 oc(pageSize);.
12942 20 20 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b if( !pNew ){
12943 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
12944 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
12945 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
12946 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 pager_reset(pPa
12947 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 70 50 ger);. pP
12948 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 3d ager->pageSize =
12949 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 pageSize;.
1294a 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 sqlite3PageFr
1294b 65 65 28 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 ee(pPager->pTmpS
1294c 70 61 63 65 29 3b 0a 20 20 20 20 20 20 20 20 70 pace);. p
1294d 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 Pager->pTmpSpace
1294e 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 20 = pNew;.
1294f 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 sqlite3PcacheSe
12950 74 50 61 67 65 53 69 7a 65 28 70 50 61 67 65 72 tPageSize(pPager
12951 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 65 53 ->pPCache, pageS
12952 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ize);. }.
12953 20 20 7d 0a 20 20 20 20 2a 70 50 61 67 65 53 69 }. *pPageSi
12954 7a 65 20 3d 20 28 75 31 36 29 70 50 61 67 65 72 ze = (u16)pPager
12955 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 ->pageSize;.
12956 69 66 28 20 6e 52 65 73 65 72 76 65 3c 30 20 29 if( nReserve<0 )
12957 20 6e 52 65 73 65 72 76 65 20 3d 20 70 50 61 67 nReserve = pPag
12958 65 72 2d 3e 6e 52 65 73 65 72 76 65 3b 0a 20 20 er->nReserve;.
12959 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 65 72 assert( nReser
1295a 76 65 3e 3d 30 20 26 26 20 6e 52 65 73 65 72 76 ve>=0 && nReserv
1295b 65 3c 31 30 30 30 20 29 3b 0a 20 20 20 20 70 50 e<1000 );. pP
1295c 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 20 3d ager->nReserve =
1295d 20 28 69 31 36 29 6e 52 65 73 65 72 76 65 3b 0a (i16)nReserve;.
1295e 20 20 20 20 70 61 67 65 72 52 65 70 6f 72 74 53 pagerReportS
1295f 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 7d ize(pPager);. }
12960 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
12961 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
12962 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 22 pointer to the "
12963 74 65 6d 70 6f 72 61 72 79 20 70 61 67 65 22 20 temporary page"
12964 62 75 66 66 65 72 20 68 65 6c 64 20 69 6e 74 65 buffer held inte
12965 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79 20 74 68 65 rnally.** by the
12966 20 70 61 67 65 72 2e 20 20 54 68 69 73 20 69 73 pager. This is
12967 20 61 20 62 75 66 66 65 72 20 74 68 61 74 20 69 a buffer that i
12968 73 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 s big enough to
12969 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 65 6e 74 69 hold the.** enti
1296a 72 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 re content of a
1296b 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 20 database page.
1296c 54 68 69 73 20 62 75 66 66 65 72 20 69 73 20 75 This buffer is u
1296d 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a sed internally.*
1296e 2a 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 * during rollbac
1296f 6b 20 61 6e 64 20 77 69 6c 6c 20 62 65 20 6f 76 k and will be ov
12970 65 72 77 72 69 74 74 65 6e 20 77 68 65 6e 65 76 erwritten whenev
12971 65 72 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a er a rollback.**
12972 20 6f 63 63 75 72 73 2e 20 20 42 75 74 20 6f 74 occurs. But ot
12973 68 65 72 20 6d 6f 64 75 6c 65 73 20 61 72 65 20 her modules are
12974 66 72 65 65 20 74 6f 20 75 73 65 20 69 74 20 74 free to use it t
12975 6f 6f 2c 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a oo, as long as.*
12976 2a 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 61 * no rollbacks a
12977 72 65 20 68 61 70 70 65 6e 69 6e 67 2e 0a 2a 2f re happening..*/
12978 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
12979 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 void *sqlite3Pag
1297a 65 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 erTempSpace(Page
1297b 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 r *pPager){. re
1297c 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 70 54 6d turn pPager->pTm
1297d 70 53 70 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pSpace;.}../*.**
1297e 20 41 74 74 65 6d 70 74 20 74 6f 20 73 65 74 20 Attempt to set
1297f 74 68 65 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 the maximum data
12980 62 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 20 base page count
12981 69 66 20 6d 78 50 61 67 65 20 69 73 20 70 6f 73 if mxPage is pos
12982 69 74 69 76 65 2e 20 0a 2a 2a 20 4d 61 6b 65 20 itive. .** Make
12983 6e 6f 20 63 68 61 6e 67 65 73 20 69 66 20 6d 78 no changes if mx
12984 50 61 67 65 20 69 73 20 7a 65 72 6f 20 6f 72 20 Page is zero or
12985 6e 65 67 61 74 69 76 65 2e 20 20 41 6e 64 20 6e negative. And n
12986 65 76 65 72 20 72 65 64 75 63 65 20 74 68 65 0a ever reduce the.
12987 2a 2a 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 ** maximum page
12988 63 6f 75 6e 74 20 62 65 6c 6f 77 20 74 68 65 20 count below the
12989 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 current size of
1298a 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a the database..**
1298b 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f .** Regardless o
1298c 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 6e f mxPage, return
1298d 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 78 the current max
1298e 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e imum page count.
1298f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
12990 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
12991 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 gerMaxPageCount(
12992 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
12993 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 66 nt mxPage){. if
12994 28 20 6d 78 50 61 67 65 3e 30 20 29 7b 0a 20 20 ( mxPage>0 ){.
12995 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f pPager->mxPgno
12996 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 7d 0a 20 = mxPage;. }.
12997 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
12998 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30 ecount(pPager, 0
12999 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 );. return pPag
1299a 65 72 2d 3e 6d 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f er->mxPgno;.}../
1299b 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
1299c 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e ng set of routin
1299d 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 64 es are used to d
1299e 69 73 61 62 6c 65 20 74 68 65 20 73 69 6d 75 6c isable the simul
1299f 61 74 65 64 0a 2a 2a 20 49 2f 4f 20 65 72 72 6f ated.** I/O erro
129a0 72 20 6d 65 63 68 61 6e 69 73 6d 2e 20 20 54 68 r mechanism. Th
129a1 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ese routines are
129a2 20 75 73 65 64 20 74 6f 20 61 76 6f 69 64 20 73 used to avoid s
129a3 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 65 72 72 6f imulated.** erro
129a4 72 73 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65 rs in places whe
129a5 72 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 re we do not car
129a6 65 20 61 62 6f 75 74 20 65 72 72 6f 72 73 2e 0a e about errors..
129a7 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 2d 44 53 **.** Unless -DS
129a8 51 4c 49 54 45 5f 54 45 53 54 3d 31 20 69 73 20 QLITE_TEST=1 is
129a9 75 73 65 64 2c 20 74 68 65 73 65 20 72 6f 75 74 used, these rout
129aa 69 6e 65 73 20 61 72 65 20 61 6c 6c 20 6e 6f 2d ines are all no-
129ab 6f 70 73 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72 ops.** and gener
129ac 61 74 65 20 6e 6f 20 63 6f 64 65 2e 0a 2a 2f 0a ate no code..*/.
129ad 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
129ae 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78 ST.SQLITE_API ex
129af 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 tern int sqlite3
129b0 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e _io_error_pendin
129b1 67 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78 g;.SQLITE_API ex
129b2 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 tern int sqlite3
129b3 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 3b 0a 73 _io_error_hit;.s
129b4 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 64 5f tatic int saved_
129b5 63 6e 74 3b 0a 76 6f 69 64 20 64 69 73 61 62 6c cnt;.void disabl
129b6 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 e_simulated_io_e
129b7 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 rrors(void){. s
129b8 61 76 65 64 5f 63 6e 74 20 3d 20 73 71 6c 69 74 aved_cnt = sqlit
129b9 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 e3_io_error_pend
129ba 69 6e 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 ing;. sqlite3_i
129bb 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 o_error_pending
129bc 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 64 20 65 6e 61 = -1;.}.void ena
129bd 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f ble_simulated_io
129be 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20 _errors(void){.
129bf 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
129c0 72 5f 70 65 6e 64 69 6e 67 20 3d 20 73 61 76 65 r_pending = save
129c1 64 5f 63 6e 74 3b 0a 7d 0a 23 65 6c 73 65 0a 23 d_cnt;.}.#else.#
129c2 20 64 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f define disable_
129c3 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 simulated_io_err
129c4 6f 72 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65 ors().# define e
129c5 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f nable_simulated_
129c6 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64 io_errors().#end
129c7 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 if../*.** Read t
129c8 68 65 20 66 69 72 73 74 20 4e 20 62 79 74 65 73 he first N bytes
129c9 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e from the beginn
129ca 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ing of the file
129cb 69 6e 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20 74 into memory.** t
129cc 68 61 74 20 70 44 65 73 74 20 70 6f 69 6e 74 73 hat pDest points
129cd 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 to. .**.** If t
129ce 68 65 20 70 61 67 65 72 20 77 61 73 20 6f 70 65 he pager was ope
129cf 6e 65 64 20 6f 6e 20 61 20 74 72 61 6e 73 69 65 ned on a transie
129d0 6e 74 20 66 69 6c 65 20 28 7a 46 69 6c 65 6e 61 nt file (zFilena
129d1 6d 65 3d 3d 22 22 29 2c 20 6f 72 0a 2a 2a 20 6f me==""), or.** o
129d2 70 65 6e 65 64 20 6f 6e 20 61 20 66 69 6c 65 20 pened on a file
129d3 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65 less than N byte
129d4 73 20 69 6e 20 73 69 7a 65 2c 20 74 68 65 20 6f s in size, the o
129d5 75 74 70 75 74 20 62 75 66 66 65 72 20 69 73 0a utput buffer is.
129d6 2a 2a 20 7a 65 72 6f 65 64 20 61 6e 64 20 53 51 ** zeroed and SQ
129d7 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 LITE_OK returned
129d8 2e 20 54 68 65 20 72 61 74 69 6f 6e 61 6c 65 20 . The rationale
129d9 66 6f 72 20 74 68 69 73 20 69 73 20 74 68 61 74 for this is that
129da 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 this .** functi
129db 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 on is used to re
129dc 61 64 20 64 61 74 61 62 61 73 65 20 68 65 61 64 ad database head
129dd 65 72 73 2c 20 61 6e 64 20 61 20 6e 65 77 20 74 ers, and a new t
129de 72 61 6e 73 69 65 6e 74 20 6f 72 0a 2a 2a 20 7a ransient or.** z
129df 65 72 6f 20 73 69 7a 65 64 20 64 61 74 61 62 61 ero sized databa
129e0 73 65 20 68 61 73 20 61 20 68 65 61 64 65 72 20 se has a header
129e1 74 68 61 6e 20 63 6f 6e 73 69 73 74 73 20 65 6e than consists en
129e2 74 69 72 65 6c 79 20 6f 66 20 7a 65 72 6f 65 73 tirely of zeroes
129e3 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 49 ..**.** If any I
129e4 4f 20 65 72 72 6f 72 20 61 70 61 72 74 20 66 72 O error apart fr
129e5 6f 6d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f om SQLITE_IOERR_
129e6 53 48 4f 52 54 5f 52 45 41 44 20 69 73 20 65 6e SHORT_READ is en
129e7 63 6f 75 6e 74 65 72 65 64 2c 0a 2a 2a 20 74 68 countered,.** th
129e8 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 e error code is
129e9 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 returned to the
129ea 63 61 6c 6c 65 72 20 61 6e 64 20 74 68 65 20 63 caller and the c
129eb 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a ontents of the.*
129ec 2a 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 * output buffer
129ed 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 undefined..*/.SQ
129ee 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
129ef 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 61 sqlite3PagerRea
129f0 64 46 69 6c 65 68 65 61 64 65 72 28 50 61 67 65 dFileheader(Page
129f1 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 4e r *pPager, int N
129f2 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 , unsigned char
129f3 2a 70 44 65 73 74 29 7b 0a 20 20 69 6e 74 20 72 *pDest){. int r
129f4 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
129f5 20 6d 65 6d 73 65 74 28 70 44 65 73 74 2c 20 30 memset(pDest, 0
129f6 2c 20 4e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 , N);. assert(
129f7 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
129f8 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 d) || pPager->te
129f9 6d 70 46 69 6c 65 20 29 3b 0a 20 20 69 66 28 20 mpFile );. if(
129fa 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
129fb 64 29 20 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 d) ){. IOTRAC
129fc 45 28 28 22 44 42 48 44 52 20 25 70 20 30 20 25 E(("DBHDR %p 0 %
129fd 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 4e 29 d\n", pPager, N)
129fe 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ). rc = sqlit
129ff 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d e3OsRead(pPager-
12a00 3e 66 64 2c 20 70 44 65 73 74 2c 20 4e 2c 20 30 >fd, pDest, N, 0
12a01 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
12a02 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 QLITE_IOERR_SHOR
12a03 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 T_READ ){.
12a04 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
12a05 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
12a06 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
12a07 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c Return the total
12a08 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
12a09 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
12a0a 20 66 69 6c 65 20 61 73 73 6f 63 69 61 74 65 64 file associated
12a0b 20 0a 2a 2a 20 77 69 74 68 20 70 50 61 67 65 72 .** with pPager
12a0c 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74 68 69 73 . Normally, this
12a0d 20 69 73 20 63 61 6c 63 75 6c 61 74 65 64 20 61 is calculated a
12a0e 73 20 28 3c 64 62 20 66 69 6c 65 20 73 69 7a 65 s (<db file size
12a0f 3e 2f 3c 70 61 67 65 2d 73 69 7a 65 3e 29 2e 0a >/<page-size>)..
12a10 2a 2a 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 ** However, if t
12a11 68 65 20 66 69 6c 65 20 69 73 20 62 65 74 77 65 he file is betwe
12a12 65 6e 20 31 20 61 6e 64 20 3c 70 61 67 65 2d 73 en 1 and <page-s
12a13 69 7a 65 3e 20 62 79 74 65 73 20 69 6e 20 73 69 ize> bytes in si
12a14 7a 65 2c 20 74 68 65 6e 20 0a 2a 2a 20 74 68 69 ze, then .** thi
12a15 73 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 s is considered
12a16 61 20 31 20 70 61 67 65 20 66 69 6c 65 2e 0a 2a a 1 page file..*
12a17 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 *.** If the page
12a18 72 20 69 73 20 69 6e 20 65 72 72 6f 72 20 73 74 r is in error st
12a19 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 ate when this fu
12a1a 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
12a1b 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65 72 , then the.** er
12a1c 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f 72 20 ror state error
12a1d 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
12a1e 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 6c 65 66 and *pnPage lef
12a1f 74 20 75 6e 63 68 61 6e 67 65 64 2e 20 4f 72 2c t unchanged. Or,
12a20 0a 2a 2a 20 69 66 20 74 68 65 20 66 69 6c 65 20 .** if the file
12a21 73 79 73 74 65 6d 20 68 61 73 20 74 6f 20 62 65 system has to be
12a22 20 71 75 65 72 69 65 64 20 66 6f 72 20 74 68 65 queried for the
12a23 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c size of the fil
12a24 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 71 75 65 e and.** the que
12a25 72 79 20 61 74 74 65 6d 70 74 20 72 65 74 75 72 ry attempt retur
12a26 6e 73 20 61 6e 20 49 4f 20 65 72 72 6f 72 2c 20 ns an IO error,
12a27 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 the IO error cod
12a28 65 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a e is returned.**
12a29 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 69 73 20 and *pnPage is
12a2a 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a left unchanged..
12a2b 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c **.** Otherwise,
12a2c 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 if everything i
12a2d 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 s successful, th
12a2e 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 en SQLITE_OK is
12a2f 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 returned.** and
12a30 2a 70 6e 50 61 67 65 20 69 73 20 73 65 74 20 74 *pnPage is set t
12a31 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 o the number of
12a32 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 pages in the dat
12a33 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 abase..*/.SQLITE
12a34 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
12a35 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 ite3PagerPagecou
12a36 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nt(Pager *pPager
12a37 2c 20 69 6e 74 20 2a 70 6e 50 61 67 65 29 7b 0a , int *pnPage){.
12a38 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20 Pgno nPage;
12a39 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
12a3a 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 76 alue to return v
12a3b 69 61 20 2a 70 6e 50 61 67 65 20 2a 2f 0a 0a 20 ia *pnPage */..
12a3c 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 /* If the pager
12a3d 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 is already in t
12a3e 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 he error state,
12a3f 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 return the error
12a40 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 69 66 28 20 code. */. if(
12a41 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
12a42 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 ){. return pP
12a43 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 ager->errCode;.
12a44 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69 }.. /* Determi
12a45 6e 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ne the number of
12a46 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 66 69 pages in the fi
12a47 6c 65 2e 20 53 74 6f 72 65 20 74 68 69 73 20 69 le. Store this i
12a48 6e 20 6e 50 61 67 65 2e 20 2a 2f 0a 20 20 69 66 n nPage. */. if
12a49 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 ( pPager->dbSize
12a4a 56 61 6c 69 64 20 29 7b 0a 20 20 20 20 6e 50 61 Valid ){. nPa
12a4b 67 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 ge = pPager->dbS
12a4c 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ize;. }else{.
12a4d 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
12a4e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 /* Err
12a4f 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f or returned by O
12a50 73 46 69 6c 65 53 69 7a 65 28 29 20 2a 2f 0a 20 sFileSize() */.
12a51 20 20 20 69 36 34 20 6e 20 3d 20 30 3b 20 20 20 i64 n = 0;
12a52 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
12a53 6c 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 le size in bytes
12a54 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73 46 returned by OsF
12a55 69 6c 65 53 69 7a 65 28 29 20 2a 2f 0a 0a 20 20 ileSize() */..
12a56 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e assert( isOpen
12a57 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 (pPager->fd) ||
12a58 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
12a59 20 29 3b 0a 20 20 20 20 69 66 28 20 69 73 4f 70 );. if( isOp
12a5a 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 26 en(pPager->fd) &
12a5b 26 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 & (0 != (rc = sq
12a5c 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
12a5d 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 6e 29 29 pPager->fd, &n))
12a5e 29 20 29 7b 0a 20 20 20 20 20 20 70 61 67 65 72 ) ){. pager
12a5f 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 _error(pPager, r
12a60 63 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e c);. return
12a61 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 rc;. }. i
12a62 66 28 20 6e 3e 30 20 26 26 20 6e 3c 70 50 61 67 f( n>0 && n<pPag
12a63 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a er->pageSize ){.
12a64 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 31 3b nPage = 1;
12a65 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
12a66 20 20 6e 50 61 67 65 20 3d 20 28 50 67 6e 6f 29 nPage = (Pgno)
12a67 28 6e 20 2f 20 70 50 61 67 65 72 2d 3e 70 61 67 (n / pPager->pag
12a68 65 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 eSize);. }.
12a69 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 if( pPager->st
12a6a 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 ate!=PAGER_UNLOC
12a6b 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 K ){. pPage
12a6c 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61 67 r->dbSize = nPag
12a6d 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d e;. pPager-
12a6e 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 6e 50 >dbFileSize = nP
12a6f 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 age;. pPage
12a70 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d r->dbSizeValid =
12a71 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 1;. }. }..
12a72 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 /* If the curre
12a73 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 nt number of pag
12a74 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 20 69 es in the file i
12a75 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 s greater than t
12a76 68 65 20 0a 20 20 2a 2a 20 63 6f 6e 66 69 67 75 he . ** configu
12a77 72 65 64 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 red maximum page
12a78 72 20 6e 75 6d 62 65 72 2c 20 69 6e 63 72 65 61 r number, increa
12a79 73 65 20 74 68 65 20 61 6c 6c 6f 77 65 64 20 6c se the allowed l
12a7a 69 6d 69 74 20 73 6f 0a 20 20 2a 2a 20 74 68 61 imit so. ** tha
12a7b 74 20 74 68 65 20 66 69 6c 65 20 63 61 6e 20 62 t the file can b
12a7c 65 20 72 65 61 64 2e 0a 20 20 2a 2f 0a 20 20 69 e read.. */. i
12a7d 66 28 20 6e 50 61 67 65 3e 70 50 61 67 65 72 2d f( nPage>pPager-
12a7e 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 20 20 70 >mxPgno ){. p
12a7f 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 Pager->mxPgno =
12a80 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a 20 20 7d (Pgno)nPage;. }
12a81 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6f .. /* Set the o
12a82 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 61 utput variable a
12a83 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 nd return SQLITE
12a84 5f 4f 4b 20 2a 2f 0a 20 20 69 66 28 20 70 6e 50 _OK */. if( pnP
12a85 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 6e 50 61 age ){. *pnPa
12a86 67 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 7d 0a ge = nPage;. }.
12a87 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
12a88 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72 OK;.}.../*.** Tr
12a89 79 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 6c 6f y to obtain a lo
12a8a 63 6b 20 6f 66 20 74 79 70 65 20 6c 6f 63 6b 74 ck of type lockt
12a8b 79 70 65 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ype on the datab
12a8c 61 73 65 20 66 69 6c 65 2e 20 49 66 0a 2a 2a 20 ase file. If.**
12a8d 61 20 73 69 6d 69 6c 61 72 20 6f 72 20 67 72 65 a similar or gre
12a8e 61 74 65 72 20 6c 6f 63 6b 20 69 73 20 61 6c 72 ater lock is alr
12a8f 65 61 64 79 20 68 65 6c 64 2c 20 74 68 69 73 20 eady held, this
12a90 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f function is a no
12a91 2d 6f 70 0a 2a 2a 20 28 72 65 74 75 72 6e 69 6e -op.** (returnin
12a92 67 20 53 51 4c 49 54 45 5f 4f 4b 20 69 6d 6d 65 g SQLITE_OK imme
12a93 64 69 61 74 65 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 diately)..**.**
12a94 4f 74 68 65 72 77 69 73 65 2c 20 61 74 74 65 6d Otherwise, attem
12a95 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 pt to obtain the
12a96 20 6c 6f 63 6b 20 75 73 69 6e 67 20 73 71 6c 69 lock using sqli
12a97 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e 76 te3OsLock(). Inv
12a98 6f 6b 65 20 0a 2a 2a 20 74 68 65 20 62 75 73 79 oke .** the busy
12a99 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 callback if the
12a9a 20 6c 6f 63 6b 20 69 73 20 63 75 72 72 65 6e 74 lock is current
12a9b 6c 79 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 ly not available
12a9c 2e 20 52 65 70 65 61 74 20 0a 2a 2a 20 75 6e 74 . Repeat .** unt
12a9d 69 6c 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c il the busy call
12a9e 62 61 63 6b 20 72 65 74 75 72 6e 73 20 66 61 6c back returns fal
12a9f 73 65 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 se or until the
12aa0 61 74 74 65 6d 70 74 20 74 6f 20 0a 2a 2a 20 6f attempt to .** o
12aa1 62 74 61 69 6e 20 74 68 65 20 6c 6f 63 6b 20 73 btain the lock s
12aa2 75 63 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 52 ucceeds..**.** R
12aa3 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
12aa4 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 61 on success and a
12aa5 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 n error code if
12aa6 77 65 20 63 61 6e 6e 6f 74 20 6f 62 74 61 69 6e we cannot obtain
12aa7 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 20 49 66 .** the lock. If
12aa8 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 6f 62 74 the lock is obt
12aa9 61 69 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c ained successful
12aaa 6c 79 2c 20 73 65 74 20 74 68 65 20 50 61 67 65 ly, set the Page
12aab 72 2e 73 74 61 74 65 20 0a 2a 2a 20 76 61 72 69 r.state .** vari
12aac 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 able to locktype
12aad 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
12aae 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
12aaf 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c pager_wait_on_l
12ab0 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 ock(Pager *pPage
12ab1 72 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 r, int locktype)
12ab2 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
12ab3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12ab4 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
12ab5 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a rn code */.. /*
12ab6 20 54 68 65 20 4f 53 20 6c 6f 63 6b 20 76 61 6c The OS lock val
12ab7 75 65 73 20 6d 75 73 74 20 62 65 20 74 68 65 20 ues must be the
12ab8 73 61 6d 65 20 61 73 20 74 68 65 20 50 61 67 65 same as the Page
12ab9 72 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20 2a 2f r lock values */
12aba 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 . assert( PAGER
12abb 5f 53 48 41 52 45 44 3d 3d 53 48 41 52 45 44 5f _SHARED==SHARED_
12abc 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
12abd 28 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 ( PAGER_RESERVED
12abe 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 ==RESERVED_LOCK
12abf 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 );. assert( PAG
12ac0 45 52 5f 45 58 43 4c 55 53 49 56 45 3d 3d 45 58 ER_EXCLUSIVE==EX
12ac1 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a CLUSIVE_LOCK );.
12ac2 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c . /* If the fil
12ac3 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 75 e is currently u
12ac4 6e 6c 6f 63 6b 65 64 20 74 68 65 6e 20 74 68 65 nlocked then the
12ac5 20 73 69 7a 65 20 6d 75 73 74 20 62 65 20 75 6e size must be un
12ac6 6b 6e 6f 77 6e 20 2a 2f 0a 20 20 61 73 73 65 72 known */. asser
12ac7 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 t( pPager->state
12ac8 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 7c >=PAGER_SHARED |
12ac9 7c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 | pPager->dbSize
12aca 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 0a 20 20 2f Valid==0 );.. /
12acb 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 69 * Check that thi
12acc 73 20 69 73 20 65 69 74 68 65 72 20 61 20 6e 6f s is either a no
12acd 2d 6f 70 20 28 62 65 63 61 75 73 65 20 74 68 65 -op (because the
12ace 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 requested lock
12acf 69 73 20 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 is . ** already
12ad0 20 68 65 6c 64 2c 20 6f 72 20 6f 6e 65 20 6f 66 held, or one of
12ad1 20 74 68 65 20 74 72 61 6e 73 69 73 74 69 6f 6e the transistion
12ad2 73 20 74 68 61 74 20 74 68 65 20 62 75 73 79 2d s that the busy-
12ad3 68 61 6e 64 6c 65 72 0a 20 20 2a 2a 20 6d 61 79 handler. ** may
12ad4 20 62 65 20 69 6e 76 6f 6b 65 64 20 64 75 72 69 be invoked duri
12ad5 6e 67 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f ng, according to
12ad6 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f the comment abo
12ad7 76 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 ve. ** sqlite3P
12ad8 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c agerSetBusyhandl
12ad9 65 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 er().. */. ass
12ada 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e 73 74 ert( (pPager->st
12adb 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 29 0a 20 ate>=locktype).
12adc 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65 72 || (pPager
12add 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 ->state==PAGER_U
12ade 4e 4c 4f 43 4b 20 26 26 20 6c 6f 63 6b 74 79 70 NLOCK && locktyp
12adf 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 29 e==PAGER_SHARED)
12ae0 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 . || (pPag
12ae1 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 er->state==PAGER
12ae2 5f 52 45 53 45 52 56 45 44 20 26 26 20 6c 6f 63 _RESERVED && loc
12ae3 6b 74 79 70 65 3d 3d 50 41 47 45 52 5f 45 58 43 ktype==PAGER_EXC
12ae4 4c 55 53 49 56 45 29 0a 20 20 29 3b 0a 0a 20 20 LUSIVE). );..
12ae5 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 if( pPager->stat
12ae6 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 e>=locktype ){.
12ae7 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
12ae8 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 K;. }else{.
12ae9 64 6f 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 do {. rc =
12aea 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 sqlite3OsLock(pP
12aeb 61 67 65 72 2d 3e 66 64 2c 20 6c 6f 63 6b 74 79 ager->fd, lockty
12aec 70 65 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 pe);. }while(
12aed 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 rc==SQLITE_BUSY
12aee 20 26 26 20 70 50 61 67 65 72 2d 3e 78 42 75 73 && pPager->xBus
12aef 79 48 61 6e 64 6c 65 72 28 70 50 61 67 65 72 2d yHandler(pPager-
12af0 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 >pBusyHandlerArg
12af1 29 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d ) );. if( rc=
12af2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
12af3 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 pPager->stat
12af4 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 65 e = (u8)locktype
12af5 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 ;. IOTRACE(
12af6 28 22 4c 4f 43 4b 20 25 70 20 25 64 5c 6e 22 2c ("LOCK %p %d\n",
12af7 20 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74 79 70 pPager, locktyp
12af8 65 29 29 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 e)). }. }.
12af9 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
12afa 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 61 73 73 .** Function ass
12afb 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 ertTruncateConst
12afc 72 61 69 6e 74 28 70 50 61 67 65 72 29 20 63 68 raint(pPager) ch
12afd 65 63 6b 73 20 74 68 61 74 20 6f 6e 65 20 6f 66 ecks that one of
12afe 20 74 68 65 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 the .** followi
12aff 6e 67 20 69 73 20 74 72 75 65 20 66 6f 72 20 61 ng is true for a
12b00 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 63 ll dirty pages c
12b01 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 urrently in the
12b02 70 61 67 65 2d 63 61 63 68 65 3a 0a 2a 2a 0a 2a page-cache:.**.*
12b03 2a 20 20 20 61 29 20 54 68 65 20 70 61 67 65 20 * a) The page
12b04 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 20 74 number is less t
12b05 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
12b06 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
12b07 0a 2a 2a 20 20 20 20 20 20 63 75 72 72 65 6e 74 .** current
12b08 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 2c database image,
12b09 20 69 6e 20 70 61 67 65 73 2c 20 4f 52 0a 2a 2a in pages, OR.**
12b0a 0a 2a 2a 20 20 20 62 29 20 69 66 20 74 68 65 20 .** b) if the
12b0b 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 77 65 72 page content wer
12b0c 65 20 77 72 69 74 74 65 6e 20 61 74 20 74 68 69 e written at thi
12b0d 73 20 74 69 6d 65 2c 20 69 74 20 77 6f 75 6c 64 s time, it would
12b0e 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 62 65 20 not.** be
12b0f 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 72 69 necessary to wri
12b10 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 te the current c
12b11 6f 6e 74 65 6e 74 20 6f 75 74 20 74 6f 20 74 68 ontent out to th
12b12 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a e sub-journal.**
12b13 20 20 20 20 20 20 28 61 73 20 64 65 74 65 72 6d (as determ
12b14 69 6e 65 64 20 62 79 20 66 75 6e 63 74 69 6f 6e ined by function
12b15 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 subjRequiresPag
12b16 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 e())..**.** If t
12b17 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 61 73 73 he condition ass
12b18 65 72 74 65 64 20 62 79 20 74 68 69 73 20 66 75 erted by this fu
12b19 6e 63 74 69 6f 6e 20 77 65 72 65 20 6e 6f 74 20 nction were not
12b1a 74 72 75 65 2c 20 61 6e 64 20 74 68 65 0a 2a 2a true, and the.**
12b1b 20 64 69 72 74 79 20 70 61 67 65 20 77 65 72 65 dirty page were
12b1c 20 74 6f 20 62 65 20 64 69 73 63 61 72 64 65 64 to be discarded
12b1d 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 from the cache
12b1e 76 69 61 20 74 68 65 20 70 61 67 65 72 53 74 72 via the pagerStr
12b1f 65 73 73 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 ess().** routine
12b20 2c 20 70 61 67 65 72 53 74 72 65 73 73 28 29 20 , pagerStress()
12b21 77 6f 75 6c 64 20 6e 6f 74 20 77 72 69 74 65 20 would not write
12b22 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 the current page
12b23 20 63 6f 6e 74 65 6e 74 20 74 6f 0a 2a 2a 20 74 content to.** t
12b24 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12b25 2e 20 49 66 20 61 20 73 61 76 65 70 6f 69 6e 74 . If a savepoint
12b26 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 65 72 transaction wer
12b27 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 66 e rolled back af
12b28 74 65 72 0a 2a 2a 20 74 68 69 73 20 68 61 70 70 ter.** this happ
12b29 65 6e 65 64 2c 20 74 68 65 20 63 6f 72 72 65 63 ened, the correc
12b2a 74 20 62 65 68 61 76 69 6f 75 72 20 77 6f 75 6c t behaviour woul
12b2b 64 20 62 65 20 74 6f 20 72 65 73 74 6f 72 65 20 d be to restore
12b2c 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63 the current.** c
12b2d 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 ontent of the pa
12b2e 67 65 2e 20 48 6f 77 65 76 65 72 2c 20 73 69 6e ge. However, sin
12b2f 63 65 20 74 68 69 73 20 63 6f 6e 74 65 6e 74 20 ce this content
12b30 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 is not present i
12b31 6e 20 65 69 74 68 65 72 0a 2a 2a 20 74 68 65 20 n either.** the
12b32 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 72 database file or
12b33 20 74 68 65 20 70 6f 72 74 69 6f 6e 20 6f 66 20 the portion of
12b34 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
12b35 72 6e 61 6c 20 61 6e 64 20 0a 2a 2a 20 73 75 62 rnal and .** sub
12b36 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 65 64 20 -journal rolled
12b37 62 61 63 6b 20 74 68 65 20 63 6f 6e 74 65 6e 74 back the content
12b38 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72 65 could not be re
12b39 73 74 6f 72 65 64 20 61 6e 64 20 74 68 65 0a 2a stored and the.*
12b3a 2a 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 * database image
12b3b 20 77 6f 75 6c 64 20 62 65 63 6f 6d 65 20 63 6f would become co
12b3c 72 72 75 70 74 2e 20 49 74 20 69 73 20 74 68 65 rrupt. It is the
12b3d 72 65 66 6f 72 65 20 66 6f 72 74 75 6e 61 74 65 refore fortunate
12b3e 20 74 68 61 74 20 0a 2a 2a 20 74 68 69 73 20 63 that .** this c
12b3f 69 72 63 75 6d 73 74 61 6e 63 65 20 63 61 6e 6e ircumstance cann
12b40 6f 74 20 61 72 69 73 65 2e 0a 2a 2f 0a 23 69 66 ot arise..*/.#if
12b41 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
12b42 44 45 42 55 47 29 0a 73 74 61 74 69 63 20 76 6f DEBUG).static vo
12b43 69 64 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 id assertTruncat
12b44 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 28 50 67 eConstraintCb(Pg
12b45 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 61 73 73 Hdr *pPg){. ass
12b46 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 ert( pPg->flags&
12b47 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 20 PGHDR_DIRTY );.
12b48 20 61 73 73 65 72 74 28 20 21 73 75 62 6a 52 65 assert( !subjRe
12b49 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 20 quiresPage(pPg)
12b4a 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 || pPg->pgno<=pP
12b4b 67 2d 3e 70 50 61 67 65 72 2d 3e 64 62 53 69 7a g->pPager->dbSiz
12b4c 65 20 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f e );.}.static vo
12b4d 69 64 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 id assertTruncat
12b4e 65 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 67 65 eConstraint(Page
12b4f 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 71 r *pPager){. sq
12b50 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72 61 lite3PcacheItera
12b51 74 65 44 69 72 74 79 28 70 50 61 67 65 72 2d 3e teDirty(pPager->
12b52 70 50 43 61 63 68 65 2c 20 61 73 73 65 72 74 54 pPCache, assertT
12b53 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e runcateConstrain
12b54 74 43 62 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 tCb);.}.#else.#
12b55 64 65 66 69 6e 65 20 61 73 73 65 72 74 54 72 75 define assertTru
12b56 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 ncateConstraint(
12b57 70 50 61 67 65 72 29 0a 23 65 6e 64 69 66 0a 0a pPager).#endif..
12b58 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 /*.** Truncate t
12b59 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 he in-memory dat
12b5a 61 62 61 73 65 20 66 69 6c 65 20 69 6d 61 67 65 abase file image
12b5b 20 74 6f 20 6e 50 61 67 65 20 70 61 67 65 73 2e to nPage pages.
12b5c 20 54 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 This .** functi
12b5d 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 63 74 75 on does not actu
12b5e 61 6c 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 ally modify the
12b5f 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e database file on
12b60 20 64 69 73 6b 2e 20 49 74 20 0a 2a 2a 20 6a 75 disk. It .** ju
12b61 73 74 20 73 65 74 73 20 74 68 65 20 69 6e 74 65 st sets the inte
12b62 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 20 74 68 rnal state of th
12b63 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 73 e pager object s
12b64 6f 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 74 o that the .** t
12b65 72 75 6e 63 61 74 69 6f 6e 20 77 69 6c 6c 20 62 runcation will b
12b66 65 20 64 6f 6e 65 20 77 68 65 6e 20 74 68 65 20 e done when the
12b67 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
12b68 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 ion is committed
12b69 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
12b6a 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
12b6b 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 PagerTruncateIma
12b6c 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ge(Pager *pPager
12b6d 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 , Pgno nPage){.
12b6e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
12b6f 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a >dbSizeValid );.
12b70 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
12b71 2d 3e 64 62 53 69 7a 65 3e 3d 6e 50 61 67 65 20 ->dbSize>=nPage
12b72 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
12b73 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
12b74 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 R_RESERVED );.
12b75 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d pPager->dbSize =
12b76 20 6e 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74 nPage;. assert
12b77 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 TruncateConstrai
12b78 6e 74 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f nt(pPager);.}../
12b79 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 *.** Shutdown th
12b7a 65 20 70 61 67 65 20 63 61 63 68 65 2e 20 20 46 e page cache. F
12b7b 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 ree all memory a
12b7c 6e 64 20 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c nd close all fil
12b7d 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 es..**.** If a t
12b7e 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 69 ransaction was i
12b7f 6e 20 70 72 6f 67 72 65 73 73 20 77 68 65 6e 20 n progress when
12b80 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
12b81 63 61 6c 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20 called, that.**
12b82 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 transaction is r
12b83 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c olled back. All
12b84 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 outstanding pag
12b85 65 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 es are invalidat
12b86 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65 69 72 20 ed.** and their
12b87 6d 65 6d 6f 72 79 20 69 73 20 66 72 65 65 64 2e memory is freed.
12b88 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f Any attempt to
12b89 20 75 73 65 20 61 20 70 61 67 65 20 61 73 73 6f use a page asso
12b8a 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 ciated.** with t
12b8b 68 69 73 20 70 61 67 65 20 63 61 63 68 65 20 61 his page cache a
12b8c 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 fter this functi
12b8d 6f 6e 20 72 65 74 75 72 6e 73 20 77 69 6c 6c 20 on returns will
12b8e 6c 69 6b 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74 likely.** result
12b8f 20 69 6e 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a in a coredump..
12b90 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
12b91 69 6f 6e 20 61 6c 77 61 79 73 20 73 75 63 63 65 ion always succe
12b92 65 64 73 2e 20 49 66 20 61 20 74 72 61 6e 73 61 eds. If a transa
12b93 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 ction is active
12b94 61 6e 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 an attempt.** is
12b95 20 6d 61 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74 made to roll it
12b96 20 62 61 63 6b 2e 20 49 66 20 61 6e 20 65 72 72 back. If an err
12b97 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
12b98 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a the rollback .*
12b99 2a 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 * a hot journal
12b9a 6d 61 79 20 62 65 20 6c 65 66 74 20 69 6e 20 74 may be left in t
12b9b 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 62 75 he filesystem bu
12b9c 74 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 72 65 t no error is re
12b9d 74 75 72 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65 turned.** to the
12b9e 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 caller..*/.SQLI
12b9f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
12ba0 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 qlite3PagerClose
12ba1 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
12ba2 0a 20 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c . disable_simul
12ba3 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 ated_io_errors()
12ba4 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e ;. sqlite3Begin
12ba5 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
12ba6 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 pPager->errCod
12ba7 65 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d e = 0;. pPager-
12ba8 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d >exclusiveMode =
12ba9 20 30 3b 0a 20 20 70 61 67 65 72 5f 72 65 73 65 0;. pager_rese
12baa 74 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 t(pPager);. if(
12bab 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 61 MEMDB ){. pa
12bac 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 ger_unlock(pPage
12bad 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 r);. }else{.
12bae 20 2f 2a 20 53 65 74 20 50 61 67 65 72 2e 6a 6f /* Set Pager.jo
12baf 75 72 6e 61 6c 48 64 72 20 74 6f 20 2d 31 20 66 urnalHdr to -1 f
12bb0 6f 72 20 74 68 65 20 62 65 6e 65 66 69 74 20 6f or the benefit o
12bb1 66 20 74 68 65 20 70 61 67 65 72 5f 70 6c 61 79 f the pager_play
12bb2 62 61 63 6b 28 29 20 0a 20 20 20 20 2a 2a 20 63 back() . ** c
12bb3 61 6c 6c 20 77 68 69 63 68 20 6d 61 79 20 62 65 all which may be
12bb4 20 6d 61 64 65 20 66 72 6f 6d 20 77 69 74 68 69 made from withi
12bb5 6e 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 n pagerUnlockAnd
12bb6 52 6f 6c 6c 62 61 63 6b 28 29 2e 20 49 66 20 69 Rollback(). If i
12bb7 74 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 t. ** is not
12bb8 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 75 6e 73 -1, then the uns
12bb9 79 6e 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 ynced portion of
12bba 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c an open journal
12bbb 20 66 69 6c 65 20 6d 61 79 0a 20 20 20 20 2a 2a file may. **
12bbc 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20 be played back
12bbd 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
12bbe 65 2e 20 49 66 20 61 20 70 6f 77 65 72 20 66 61 e. If a power fa
12bbf 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 ilure occurs whi
12bc0 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69 le. ** this i
12bc1 73 20 68 61 70 70 65 6e 69 6e 67 2c 20 74 68 65 s happening, the
12bc2 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 62 65 database may be
12bc3 63 6f 6d 65 20 63 6f 72 72 75 70 74 2e 0a 20 20 come corrupt..
12bc4 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d */. pPager-
12bc5 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 2d 31 >journalHdr = -1
12bc6 3b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 ;. pagerUnloc
12bc7 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 kAndRollback(pPa
12bc8 67 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 ger);. }. sqli
12bc9 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
12bca 6f 63 28 29 3b 0a 20 20 65 6e 61 62 6c 65 5f 73 oc();. enable_s
12bcb 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f imulated_io_erro
12bcc 72 73 28 29 3b 0a 20 20 50 41 47 45 52 54 52 41 rs();. PAGERTRA
12bcd 43 45 28 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22 CE(("CLOSE %d\n"
12bce 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 , PAGERID(pPager
12bcf 29 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 )));. IOTRACE((
12bd0 22 43 4c 4f 53 45 20 25 70 5c 6e 22 2c 20 70 50 "CLOSE %p\n", pP
12bd1 61 67 65 72 29 29 0a 20 20 73 71 6c 69 74 65 33 ager)). sqlite3
12bd2 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e OsClose(pPager->
12bd3 66 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 fd);. sqlite3Pa
12bd4 67 65 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 geFree(pPager->p
12bd5 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 73 71 6c TmpSpace);. sql
12bd6 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 ite3PcacheClose(
12bd7 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
12bd8 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
12bd9 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 66 28 _HAS_CODEC. if(
12bda 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 pPager->xCodecF
12bdb 72 65 65 20 29 20 70 50 61 67 65 72 2d 3e 78 43 ree ) pPager->xC
12bdc 6f 64 65 63 46 72 65 65 28 70 50 61 67 65 72 2d odecFree(pPager-
12bdd 3e 70 43 6f 64 65 63 29 3b 0a 23 65 6e 64 69 66 >pCodec);.#endif
12bde 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61 .. assert( !pPa
12bdf 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 ger->aSavepoint
12be0 26 26 20 21 70 50 61 67 65 72 2d 3e 70 49 6e 4a && !pPager->pInJ
12be1 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 ournal );. asse
12be2 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 rt( !isOpen(pPag
12be3 65 72 2d 3e 6a 66 64 29 20 26 26 20 21 69 73 4f er->jfd) && !isO
12be4 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 pen(pPager->sjfd
12be5 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f ) );.. sqlite3_
12be6 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 20 free(pPager);.
12be7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
12be8 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 ;.}..#if !define
12be9 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 d(NDEBUG) || def
12bea 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
12beb 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 )./*.** Return t
12bec 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 he page number f
12bed 6f 72 20 70 61 67 65 20 70 50 67 2e 0a 2a 2f 0a or page pPg..*/.
12bee 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 SQLITE_PRIVATE P
12bef 67 6e 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 gno sqlite3Pager
12bf0 50 61 67 65 6e 75 6d 62 65 72 28 44 62 50 61 67 Pagenumber(DbPag
12bf1 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72 e *pPg){. retur
12bf2 6e 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 7d 0a 23 n pPg->pgno;.}.#
12bf3 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 endif../*.** Inc
12bf4 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 rement the refer
12bf5 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20 70 ence count for p
12bf6 61 67 65 20 70 50 67 2e 0a 2a 2f 0a 53 51 4c 49 age pPg..*/.SQLI
12bf7 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
12bf8 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 sqlite3PagerRef(
12bf9 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 DbPage *pPg){.
12bfa 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 sqlite3PcacheRef
12bfb 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 (pPg);.}../*.**
12bfc 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c Sync the journal
12bfd 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 . In other words
12bfe 2c 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 , make sure all
12bff 74 68 65 20 70 61 67 65 73 20 74 68 61 74 20 68 the pages that h
12c00 61 76 65 0a 2a 2a 20 62 65 65 6e 20 77 72 69 74 ave.** been writ
12c01 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ten to the journ
12c02 61 6c 20 68 61 76 65 20 61 63 74 75 61 6c 6c 79 al have actually
12c03 20 72 65 61 63 68 65 64 20 74 68 65 20 73 75 72 reached the sur
12c04 66 61 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 face of the.** d
12c05 69 73 6b 20 61 6e 64 20 63 61 6e 20 62 65 20 72 isk and can be r
12c06 65 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 65 estored in the e
12c07 76 65 6e 74 20 6f 66 20 61 20 68 6f 74 2d 6a 6f vent of a hot-jo
12c08 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 0a urnal rollback..
12c09 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 61 67 **.** If the Pag
12c0a 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 er.needSync flag
12c0b 20 69 73 20 6e 6f 74 20 73 65 74 2c 20 74 68 65 is not set, the
12c0c 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
12c0d 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 4f is a.** no-op. O
12c0e 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 61 63 therwise, the ac
12c0f 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20 64 tions required d
12c10 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 6a 6f 75 epend on the jou
12c11 72 6e 61 6c 2d 6d 6f 64 65 0a 2a 2a 20 61 6e 64 rnal-mode.** and
12c12 20 74 68 65 20 64 65 76 69 63 65 20 63 68 61 72 the device char
12c13 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 acteristics of t
12c14 68 65 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 he the file-syst
12c15 65 6d 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a em, as follows:.
12c16 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 **.** * If the
12c17 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
12c18 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f an in-memory jo
12c19 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6e 6f 20 61 urnal file, no a
12c1a 63 74 69 6f 6e 20 6e 65 65 64 0a 2a 2a 20 20 20 ction need.**
12c1b 20 20 62 65 20 74 61 6b 65 6e 2e 0a 2a 2a 0a 2a be taken..**.*
12c1c 2a 20 20 20 2a 20 4f 74 68 65 72 77 69 73 65 2c * * Otherwise,
12c1d 20 69 66 20 74 68 65 20 64 65 76 69 63 65 20 64 if the device d
12c1e 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 oes not support
12c1f 74 68 65 20 53 41 46 45 5f 41 50 50 45 4e 44 20 the SAFE_APPEND
12c20 70 72 6f 70 65 72 74 79 2c 0a 2a 2a 20 20 20 20 property,.**
12c21 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63 20 66 then the nRec f
12c22 69 65 6c 64 20 6f 66 20 74 68 65 20 6d 6f 73 74 ield of the most
12c23 20 72 65 63 65 6e 74 6c 79 20 77 72 69 74 74 65 recently writte
12c24 6e 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 n journal header
12c25 0a 2a 2a 20 20 20 20 20 69 73 20 75 70 64 61 74 .** is updat
12c26 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 ed to contain th
12c27 65 20 6e 75 6d 62 65 72 20 6f 66 20 6a 6f 75 72 e number of jour
12c28 6e 61 6c 20 72 65 63 6f 72 64 73 20 74 68 61 74 nal records that
12c29 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 62 65 65 have.** bee
12c2a 6e 20 77 72 69 74 74 65 6e 20 66 6f 6c 6c 6f 77 n written follow
12c2b 69 6e 67 20 69 74 2e 20 49 66 20 74 68 65 20 70 ing it. If the p
12c2c 61 67 65 72 20 69 73 20 6f 70 65 72 61 74 69 6e ager is operatin
12c2d 67 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 0a 2a g in full-sync.*
12c2e 2a 20 20 20 20 20 6d 6f 64 65 2c 20 74 68 65 6e * mode, then
12c2f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
12c30 65 20 69 73 20 73 79 6e 63 65 64 20 62 65 66 6f e is synced befo
12c31 72 65 20 74 68 69 73 20 66 69 65 6c 64 20 69 73 re this field is
12c32 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 updated..**.**
12c33 20 20 2a 20 49 66 20 74 68 65 20 64 65 76 69 63 * If the devic
12c34 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f e does not suppo
12c35 72 74 20 74 68 65 20 53 45 51 55 45 4e 54 49 41 rt the SEQUENTIA
12c36 4c 20 70 72 6f 70 65 72 74 79 2c 20 74 68 65 6e L property, then
12c37 20 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c .** journal
12c38 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65 64 2e file is synced.
12c39 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 6e 20 70 73 .**.** Or, in ps
12c3a 65 75 64 6f 2d 63 6f 64 65 3a 0a 2a 2a 0a 2a 2a eudo-code:.**.**
12c3b 20 20 20 69 66 28 20 4e 4f 54 20 3c 69 6e 2d 6d if( NOT <in-m
12c3c 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 3e 20 29 emory journal> )
12c3d 7b 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f 54 {.** if( NOT
12c3e 20 53 41 46 45 5f 41 50 50 45 4e 44 20 29 7b 0a SAFE_APPEND ){.
12c3f 2a 2a 20 20 20 20 20 20 20 69 66 28 20 3c 66 75 ** if( <fu
12c40 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 3e 20 29 20 ll-sync mode> )
12c41 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 66 xSync(<journal f
12c42 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 20 20 20 20 ile>);.**
12c43 3c 75 70 64 61 74 65 20 6e 52 65 63 20 66 69 65 <update nRec fie
12c44 6c 64 3e 0a 2a 2a 20 20 20 20 20 7d 20 0a 2a 2a ld>.** } .**
12c45 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53 45 51 if( NOT SEQ
12c46 55 45 4e 54 49 41 4c 20 29 20 78 53 79 6e 63 28 UENTIAL ) xSync(
12c47 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 3e 29 3b <journal file>);
12c48 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 54 68 .** }.**.** Th
12c49 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 e Pager.needSync
12c4a 20 66 6c 61 67 20 69 73 20 6e 65 76 65 72 20 62 flag is never b
12c4b 65 20 73 65 74 20 66 6f 72 20 74 65 6d 70 6f 72 e set for tempor
12c4c 61 72 79 20 66 69 6c 65 73 2c 20 6f 72 20 61 6e ary files, or an
12c4d 79 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 72 61 74 y.** file operat
12c4e 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d ing in no-sync m
12c4f 6f 64 65 20 28 50 61 67 65 72 2e 6e 6f 53 79 6e ode (Pager.noSyn
12c50 63 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 c set to non-zer
12c51 6f 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 o)..**.** If suc
12c52 63 65 73 73 66 75 6c 2c 20 74 68 69 73 20 72 6f cessful, this ro
12c53 75 74 69 6e 65 20 63 6c 65 61 72 73 20 74 68 65 utine clears the
12c54 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
12c55 20 66 6c 61 67 20 6f 66 20 65 76 65 72 79 20 0a flag of every .
12c56 2a 2a 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c ** page currentl
12c57 79 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 y held in memory
12c58 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
12c59 67 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 g SQLITE_OK. If
12c5a 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 69 an IO.** error i
12c5b 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 s encountered, t
12c5c 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 hen the IO error
12c5d 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
12c5e 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e d to the caller.
12c5f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
12c60 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 yncJournal(Pager
12c61 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 *pPager){. if(
12c62 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
12c63 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 c ){. assert(
12c64 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 !pPager->tempFi
12c65 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 le );. if( pP
12c66 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
12c67 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e!=PAGER_JOURNAL
12c68 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 MODE_MEMORY ){.
12c69 20 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20 int rc;
12c6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12c6b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
12c6c 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 20 20 urn code */.
12c6d 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 44 63 20 const int iDc
12c6e 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 = sqlite3OsDevic
12c6f 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
12c70 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 (pPager->fd);.
12c71 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 assert( isOp
12c72 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
12c73 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d );.. if( 0=
12c74 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 =(iDc&SQLITE_IOC
12c75 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 20 AP_SAFE_APPEND)
12c76 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
12c77 69 73 20 62 6c 6f 63 6b 20 64 65 61 6c 73 20 77 is block deals w
12c78 69 74 68 20 61 6e 20 6f 62 73 63 75 72 65 20 70 ith an obscure p
12c79 72 6f 62 6c 65 6d 2e 20 49 66 20 74 68 65 20 6c roblem. If the l
12c7a 61 73 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 ast connection.
12c7b 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 77 ** that w
12c7c 72 6f 74 65 20 74 6f 20 74 68 69 73 20 64 61 74 rote to this dat
12c7d 61 62 61 73 65 20 77 61 73 20 6f 70 65 72 61 74 abase was operat
12c7e 69 6e 67 20 69 6e 20 70 65 72 73 69 73 74 65 6e ing in persisten
12c7f 74 2d 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 t-journal.
12c80 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 6e 20 ** mode, then
12c81 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
12c82 20 6d 61 79 20 61 74 20 74 68 69 73 20 70 6f 69 may at this poi
12c83 6e 74 20 61 63 74 75 61 6c 6c 79 20 62 65 20 6c nt actually be l
12c84 61 72 67 65 72 0a 20 20 20 20 20 20 20 20 2a 2a arger. **
12c85 20 74 68 61 6e 20 50 61 67 65 72 2e 6a 6f 75 72 than Pager.jour
12c86 6e 61 6c 4f 66 66 20 62 79 74 65 73 2e 20 49 66 nalOff bytes. If
12c87 20 74 68 65 20 6e 65 78 74 20 74 68 69 6e 67 20 the next thing
12c88 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 in the journal.
12c89 20 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 20 68 ** file h
12c8a 61 70 70 65 6e 73 20 74 6f 20 62 65 20 61 20 6a appens to be a j
12c8b 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 28 77 ournal-header (w
12c8c 72 69 74 74 65 6e 20 61 73 20 70 61 72 74 20 6f ritten as part o
12c8d 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a f the. **
12c8e 20 70 72 65 76 69 6f 75 73 20 63 6f 6e 6e 65 63 previous connec
12c8f 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 74 69 6f tions transactio
12c90 6e 29 2c 20 61 6e 64 20 61 20 63 72 61 73 68 20 n), and a crash
12c91 6f 72 20 70 6f 77 65 72 2d 66 61 69 6c 75 72 65 or power-failure
12c92 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 63 63 . ** occ
12c93 75 72 73 20 61 66 74 65 72 20 6e 52 65 63 20 69 urs after nRec i
12c94 73 20 75 70 64 61 74 65 64 20 62 75 74 20 62 65 s updated but be
12c95 66 6f 72 65 20 74 68 69 73 20 63 6f 6e 6e 65 63 fore this connec
12c96 74 69 6f 6e 20 77 72 69 74 65 73 20 0a 20 20 20 tion writes .
12c97 20 20 20 20 20 2a 2a 20 61 6e 79 74 68 69 6e 67 ** anything
12c98 20 65 6c 73 65 20 74 6f 20 74 68 65 20 6a 6f 75 else to the jou
12c99 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20 63 6f rnal file (or co
12c9a 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20 62 61 63 6b mmits/rolls back
12c9b 20 69 74 73 20 0a 20 20 20 20 20 20 20 20 2a 2a its . **
12c9c 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 74 transaction), t
12c9d 68 65 6e 20 53 51 4c 69 74 65 20 6d 61 79 20 62 hen SQLite may b
12c9e 65 63 6f 6d 65 20 63 6f 6e 66 75 73 65 64 20 77 ecome confused w
12c9f 68 65 6e 20 64 6f 69 6e 67 20 74 68 65 20 0a 20 hen doing the .
12ca0 20 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d 6a 6f ** hot-jo
12ca1 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 20 66 urnal rollback f
12ca2 6f 6c 6c 6f 77 69 6e 67 20 72 65 63 6f 76 65 72 ollowing recover
12ca3 79 2e 20 49 74 20 6d 61 79 20 72 6f 6c 6c 20 62 y. It may roll b
12ca4 61 63 6b 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 ack all.
12ca5 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f 6e 6e 65 ** of this conne
12ca6 63 74 69 6f 6e 73 20 64 61 74 61 2c 20 74 68 65 ctions data, the
12ca7 6e 20 70 72 6f 63 65 65 64 20 74 6f 20 72 6f 6c n proceed to rol
12ca8 6c 69 6e 67 20 62 61 63 6b 20 74 68 65 20 6f 6c ling back the ol
12ca9 64 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 75 d,. ** ou
12caa 74 2d 6f 66 2d 64 61 74 65 20 64 61 74 61 20 74 t-of-date data t
12cab 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 74 2e 20 hat follows it.
12cac 44 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 Database corrupt
12cad 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a ion.. **.
12cae 20 20 20 20 20 20 20 20 2a 2a 20 54 6f 20 77 6f ** To wo
12caf 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 2c 20 rk around this,
12cb0 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 if the journal f
12cb1 69 6c 65 20 64 6f 65 73 20 61 70 70 65 61 72 20 ile does appear
12cb2 74 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20 20 to contain.
12cb3 20 20 20 2a 2a 20 61 20 76 61 6c 69 64 20 68 65 ** a valid he
12cb4 61 64 65 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 50 ader following P
12cb5 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 2c ager.journalOff,
12cb6 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 30 78 then write a 0x
12cb7 30 30 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 79 00. ** by
12cb8 74 65 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 te to the start
12cb9 6f 66 20 69 74 20 74 6f 20 70 72 65 76 65 6e 74 of it to prevent
12cba 20 69 74 20 66 72 6f 6d 20 62 65 69 6e 67 20 72 it from being r
12cbb 65 63 6f 67 6e 69 7a 65 64 2e 0a 20 20 20 20 20 ecognized..
12cbc 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a **. **
12cbd 20 56 61 72 69 61 62 6c 65 20 69 4e 65 78 74 48 Variable iNextH
12cbe 64 72 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 drOffset is set
12cbf 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 61 74 to the offset at
12cc0 20 77 68 69 63 68 20 74 68 69 73 0a 20 20 20 20 which this.
12cc1 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 61 74 ** problemat
12cc2 69 63 20 68 65 61 64 65 72 20 77 69 6c 6c 20 6f ic header will o
12cc3 63 63 75 72 2c 20 69 66 20 69 74 20 65 78 69 73 ccur, if it exis
12cc4 74 73 2e 20 61 4d 61 67 69 63 20 69 73 20 75 73 ts. aMagic is us
12cc5 65 64 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 ed . ** a
12cc6 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 62 75 s a temporary bu
12cc7 66 66 65 72 20 74 6f 20 69 6e 73 70 65 63 74 20 ffer to inspect
12cc8 74 68 65 20 66 69 72 73 74 20 63 6f 75 70 6c 65 the first couple
12cc9 20 6f 66 20 62 79 74 65 73 20 6f 66 0a 20 20 20 of bytes of.
12cca 20 20 20 20 20 2a 2a 20 74 68 65 20 70 6f 74 65 ** the pote
12ccb 6e 74 69 61 6c 20 6a 6f 75 72 6e 61 6c 20 68 65 ntial journal he
12ccc 61 64 65 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f ader.. */
12ccd 0a 20 20 20 20 20 20 20 20 69 36 34 20 69 4e 65 . i64 iNe
12cce 78 74 48 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 xtHdrOffset;.
12ccf 20 20 20 20 20 75 38 20 61 4d 61 67 69 63 5b 38 u8 aMagic[8
12cd0 5d 3b 0a 09 75 38 20 7a 48 65 61 64 65 72 5b 73 ];..u8 zHeader[s
12cd1 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 izeof(aJournalMa
12cd2 67 69 63 29 2b 34 5d 3b 0a 0a 09 6d 65 6d 63 70 gic)+4];...memcp
12cd3 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f 75 72 y(zHeader, aJour
12cd4 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 nalMagic, sizeof
12cd5 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 29 (aJournalMagic))
12cd6 3b 0a 09 70 75 74 33 32 62 69 74 73 28 26 7a 48 ;..put32bits(&zH
12cd7 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f eader[sizeof(aJo
12cd8 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 70 50 urnalMagic)], pP
12cd9 61 67 65 72 2d 3e 6e 52 65 63 29 3b 0a 0a 20 20 ager->nRec);..
12cda 20 20 20 20 20 20 69 4e 65 78 74 48 64 72 4f 66 iNextHdrOf
12cdb 66 73 65 74 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 fset = journalHd
12cdc 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b rOffset(pPager);
12cdd 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
12cde 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 lite3OsRead(pPag
12cdf 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c er->jfd, aMagic,
12ce0 20 38 2c 20 69 4e 65 78 74 48 64 72 4f 66 66 73 8, iNextHdrOffs
12ce1 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 et);. if(
12ce2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
12ce3 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 61 4d 61 67 & 0==memcmp(aMag
12ce4 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 ic, aJournalMagi
12ce5 63 2c 20 38 29 20 29 7b 0a 20 20 20 20 20 20 20 c, 8) ){.
12ce6 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 static const
12ce7 75 38 20 7a 65 72 6f 62 79 74 65 20 3d 20 30 3b u8 zerobyte = 0;
12ce8 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
12ce9 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
12cea 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 7a 65 72 Pager->jfd, &zer
12ceb 6f 62 79 74 65 2c 20 31 2c 20 69 4e 65 78 74 48 obyte, 1, iNextH
12cec 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 drOffset);.
12ced 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
12cee 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc!=SQLITE_OK &
12cef 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 & rc!=SQLITE_IOE
12cf0 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b RR_SHORT_READ ){
12cf1 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
12cf2 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a n rc;. }.
12cf3 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 . /* Writ
12cf4 65 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 e the nRec value
12cf5 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 into the journa
12cf6 6c 20 66 69 6c 65 20 68 65 61 64 65 72 2e 20 49 l file header. I
12cf7 66 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 f in. **
12cf8 66 75 6c 6c 2d 73 79 6e 63 68 72 6f 6e 6f 75 73 full-synchronous
12cf9 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20 mode, sync the
12cfa 6a 6f 75 72 6e 61 6c 20 66 69 72 73 74 2e 20 54 journal first. T
12cfb 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 his ensures that
12cfc 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 20 . ** all
12cfd 64 61 74 61 20 68 61 73 20 72 65 61 6c 6c 79 20 data has really
12cfe 68 69 74 20 74 68 65 20 64 69 73 6b 20 62 65 66 hit the disk bef
12cff 6f 72 65 20 6e 52 65 63 20 69 73 20 75 70 64 61 ore nRec is upda
12d00 74 65 64 20 74 6f 20 6d 61 72 6b 0a 20 20 20 20 ted to mark.
12d01 20 20 20 20 2a 2a 20 69 74 20 61 73 20 61 20 63 ** it as a c
12d02 61 6e 64 69 64 61 74 65 20 66 6f 72 20 72 6f 6c andidate for rol
12d03 6c 62 61 63 6b 2e 0a 20 20 20 20 20 20 20 20 2a lback.. *
12d04 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69 *. ** Thi
12d05 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 s is not require
12d06 64 20 69 66 20 74 68 65 20 70 65 72 73 69 73 74 d if the persist
12d07 65 6e 74 20 6d 65 64 69 61 20 73 75 70 70 6f 72 ent media suppor
12d08 74 73 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a ts the. *
12d09 2a 20 53 41 46 45 5f 41 50 50 45 4e 44 20 70 72 * SAFE_APPEND pr
12d0a 6f 70 65 72 74 79 2e 20 42 65 63 61 75 73 65 20 operty. Because
12d0b 69 6e 20 74 68 69 73 20 63 61 73 65 20 69 74 20 in this case it
12d0c 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
12d0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 . ** for
12d0e 67 61 72 62 61 67 65 20 64 61 74 61 20 74 6f 20 garbage data to
12d0f 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 be appended to t
12d10 68 65 20 66 69 6c 65 2c 20 74 68 65 20 6e 52 65 he file, the nRe
12d11 63 20 66 69 65 6c 64 0a 20 20 20 20 20 20 20 20 c field.
12d12 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 ** is populated
12d13 77 69 74 68 20 30 78 46 46 46 46 46 46 46 46 20 with 0xFFFFFFFF
12d14 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c when the journal
12d15 20 68 65 61 64 65 72 20 69 73 20 77 72 69 74 74 header is writt
12d16 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e en. ** an
12d17 64 20 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f d never needs to
12d18 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20 20 20 be updated..
12d19 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
12d1a 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c if( pPager->full
12d1b 53 79 6e 63 20 26 26 20 30 3d 3d 28 69 44 63 26 Sync && 0==(iDc&
12d1c 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 SQLITE_IOCAP_SEQ
12d1d 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 20 20 UENTIAL) ){.
12d1e 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 PAGERTRACE
12d1f 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 (("SYNC journal
12d20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 of %d\n", PAGERI
12d21 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 D(pPager)));.
12d22 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 IOTRACE((
12d23 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 "JSYNC %p\n", pP
12d24 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 20 ager)).
12d25 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 rc = sqlite3OsS
12d26 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ync(pPager->jfd,
12d27 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c pPager->sync_fl
12d28 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ags);.
12d29 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
12d2a 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 K ) return rc;.
12d2b 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
12d2c 20 49 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20 IOTRACE(("JHDR
12d2d 25 70 20 25 6c 6c 64 5c 6e 22 2c 20 70 50 61 67 %p %lld\n", pPag
12d2e 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 er, pPager->jour
12d2f 6e 61 6c 48 64 72 29 29 3b 0a 20 20 20 20 20 20 nalHdr));.
12d30 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
12d31 57 72 69 74 65 28 0a 20 20 20 20 20 20 20 20 20 Write(.
12d32 20 20 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 pPager->jfd,
12d33 7a 48 65 61 64 65 72 2c 20 73 69 7a 65 6f 66 28 zHeader, sizeof(
12d34 7a 48 65 61 64 65 72 29 2c 20 70 50 61 67 65 72 zHeader), pPager
12d35 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 0a 09 29 3b ->journalHdr..);
12d36 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
12d37 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
12d38 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
12d39 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44 if( 0==(iD
12d3a 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 c&SQLITE_IOCAP_S
12d3b 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 EQUENTIAL) ){.
12d3c 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 PAGERTRACE
12d3d 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 (("SYNC journal
12d3e 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 of %d\n", PAGERI
12d3f 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 D(pPager)));.
12d40 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a IOTRACE(("J
12d41 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 SYNC %p\n", pPag
12d42 65 72 29 29 0a 20 20 20 20 20 20 20 20 72 63 20 er)). rc
12d43 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 = sqlite3OsSync(
12d44 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 pPager->jfd, pPa
12d45 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 7c ger->sync_flags|
12d46 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 50 61 . (pPa
12d47 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 3d ger->sync_flags=
12d48 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c =SQLITE_SYNC_FUL
12d49 4c 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 L?SQLITE_SYNC_DA
12d4a 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20 20 TAONLY:0).
12d4b 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 );. if(
12d4c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
12d4d 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
12d4e 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f }. }.. /
12d4f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * The journal fi
12d50 6c 65 20 77 61 73 20 6a 75 73 74 20 73 75 63 63 le was just succ
12d51 65 73 73 66 75 6c 6c 79 20 73 79 6e 63 65 64 2e essfully synced.
12d52 20 53 65 74 20 50 61 67 65 72 2e 6e 65 65 64 53 Set Pager.needS
12d53 79 6e 63 20 0a 20 20 20 20 2a 2a 20 74 6f 20 7a ync . ** to z
12d54 65 72 6f 20 61 6e 64 20 63 6c 65 61 72 20 74 68 ero and clear th
12d55 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e e PGHDR_NEED_SYN
12d56 43 20 66 6c 61 67 20 6f 6e 20 61 6c 6c 20 70 61 C flag on all pa
12d57 67 65 73 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 gess.. */.
12d58 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
12d59 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 c = 0;. pPage
12d5a 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 r->journalStarte
12d5b 64 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 d = 1;. sqlit
12d5c 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 79 6e e3PcacheClearSyn
12d5d 63 46 6c 61 67 73 28 70 50 61 67 65 72 2d 3e 70 cFlags(pPager->p
12d5e 50 43 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 PCache);. }..
12d5f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
12d60 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 ;.}../*.** The a
12d61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 66 rgument is the f
12d62 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64 irst in a linked
12d63 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 list of dirty p
12d64 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 0a 2a ages connected.*
12d65 2a 20 62 79 20 74 68 65 20 50 67 48 64 72 2e 70 * by the PgHdr.p
12d66 44 69 72 74 79 20 70 6f 69 6e 74 65 72 2e 20 54 Dirty pointer. T
12d67 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 his function wri
12d68 74 65 73 20 65 61 63 68 20 6f 6e 65 20 6f 66 20 tes each one of
12d69 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 the.** in-memory
12d6a 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c 69 pages in the li
12d6b 73 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 st to the databa
12d6c 73 65 20 66 69 6c 65 2e 20 54 68 65 20 61 72 67 se file. The arg
12d6d 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a 20 62 65 20 ument may.** be
12d6e 4e 55 4c 4c 2c 20 72 65 70 72 65 73 65 6e 74 69 NULL, representi
12d6f 6e 67 20 61 6e 20 65 6d 70 74 79 20 6c 69 73 74 ng an empty list
12d70 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
12d71 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a his function is.
12d72 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a ** a no-op..**.*
12d73 2a 20 54 68 65 20 70 61 67 65 72 20 6d 75 73 74 * The pager must
12d74 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 20 61 hold at least a
12d75 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 77 RESERVED lock w
12d76 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
12d77 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 n.** is called.
12d78 42 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 61 Before writing a
12d79 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20 64 nything to the d
12d7a 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 atabase file, th
12d7b 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 75 70 is lock.** is up
12d7c 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 58 43 graded to an EXC
12d7d 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 LUSIVE lock. If
12d7e 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 the lock cannot
12d7f 62 65 20 6f 62 74 61 69 6e 65 64 2c 0a 2a 2a 20 be obtained,.**
12d80 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 SQLITE_BUSY is r
12d81 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 eturned and no d
12d82 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 ata is written t
12d83 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
12d84 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 ile..** .** If t
12d85 68 65 20 70 61 67 65 72 20 69 73 20 61 20 74 65 he pager is a te
12d86 6d 70 2d 66 69 6c 65 20 70 61 67 65 72 20 61 6e mp-file pager an
12d87 64 20 74 68 65 20 61 63 74 75 61 6c 20 66 69 6c d the actual fil
12d88 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 0a 2a 2a e-system file.**
12d89 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e is not yet open
12d8a 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 64 20 , it is created
12d8b 61 6e 64 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 and opened befor
12d8c 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 0a 2a e any data is .*
12d8d 2a 20 77 72 69 74 74 65 6e 20 6f 75 74 2e 0a 2a * written out..*
12d8e 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 65 20 6c 6f *.** Once the lo
12d8f 63 6b 20 68 61 73 20 62 65 65 6e 20 75 70 67 72 ck has been upgr
12d90 61 64 65 64 20 61 6e 64 2c 20 69 66 20 6e 65 63 aded and, if nec
12d91 65 73 73 61 72 79 2c 20 74 68 65 20 66 69 6c 65 essary, the file
12d92 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 74 68 65 20 opened,.** the
12d93 70 61 67 65 73 20 61 72 65 20 77 72 69 74 74 65 pages are writte
12d94 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 n out to the dat
12d95 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 6c 69 abase file in li
12d96 73 74 20 6f 72 64 65 72 2e 20 57 72 69 74 69 6e st order. Writin
12d97 67 0a 2a 2a 20 61 20 70 61 67 65 20 69 73 20 73 g.** a page is s
12d98 6b 69 70 70 65 64 20 69 66 20 69 74 20 6d 65 65 kipped if it mee
12d99 74 73 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 ts either of the
12d9a 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 65 following crite
12d9b 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 ria:.**.** * T
12d9c 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 he page number i
12d9d 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 50 s greater than P
12d9e 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 6f 72 0a ager.dbSize, or.
12d9f 2a 2a 20 20 20 2a 20 54 68 65 20 50 47 48 44 52 ** * The PGHDR
12da0 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c 61 67 _DONT_WRITE flag
12da1 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20 70 is set on the p
12da2 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 age..**.** If wr
12da3 69 74 69 6e 67 20 6f 75 74 20 61 20 70 61 67 65 iting out a page
12da4 20 63 61 75 73 65 73 20 74 68 65 20 64 61 74 61 causes the data
12da5 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67 72 6f base file to gro
12da6 77 2c 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53 w, Pager.dbFileS
12da7 69 7a 65 0a 2a 2a 20 69 73 20 75 70 64 61 74 65 ize.** is update
12da8 64 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 20 49 d accordingly. I
12da9 66 20 70 61 67 65 20 31 20 69 73 20 77 72 69 74 f page 1 is writ
12daa 74 65 6e 20 6f 75 74 2c 20 74 68 65 6e 20 74 68 ten out, then th
12dab 65 20 76 61 6c 75 65 20 63 61 63 68 65 64 0a 2a e value cached.*
12dac 2a 20 69 6e 20 50 61 67 65 72 2e 64 62 46 69 6c * in Pager.dbFil
12dad 65 56 65 72 73 5b 5d 20 69 73 20 75 70 64 61 74 eVers[] is updat
12dae 65 64 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 ed to match the
12daf 6e 65 77 20 76 61 6c 75 65 20 73 74 6f 72 65 64 new value stored
12db0 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 in.** the datab
12db1 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ase file..**.**
12db2 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 If everything is
12db3 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c successful, SQL
12db4 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
12db5 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 ed. If an IO err
12db6 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 61 or .** occurs, a
12db7 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 n IO error code
12db8 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 72 2c is returned. Or,
12db9 20 69 66 20 74 68 65 20 45 58 43 4c 55 53 49 56 if the EXCLUSIV
12dba 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 0a 2a 2a E lock cannot.**
12dbb 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 53 51 be obtained, SQ
12dbc 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 LITE_BUSY is ret
12dbd 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 urned..*/.static
12dbe 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69 74 65 int pager_write
12dbf 5f 70 61 67 65 6c 69 73 74 28 50 67 48 64 72 20 _pagelist(PgHdr
12dc0 2a 70 4c 69 73 74 29 7b 0a 20 20 50 61 67 65 72 *pList){. Pager
12dc1 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20 *pPager;
12dc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12dc3 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 /* Pager object
12dc4 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
12dc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12dc6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
12dc7 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 69 urn code */.. i
12dc8 66 28 20 4e 45 56 45 52 28 70 4c 69 73 74 3d 3d f( NEVER(pList==
12dc9 30 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 0) ) return SQLI
12dca 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67 65 72 20 TE_OK;. pPager
12dcb 3d 20 70 4c 69 73 74 2d 3e 70 50 61 67 65 72 3b = pList->pPager;
12dcc 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 .. /* At this p
12dcd 6f 69 6e 74 20 74 68 65 72 65 20 6d 61 79 20 62 oint there may b
12dce 65 20 65 69 74 68 65 72 20 61 20 52 45 53 45 52 e either a RESER
12dcf 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 VED or EXCLUSIVE
12dd0 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a lock on the. *
12dd1 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e * database file.
12dd2 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 If there is alr
12dd3 65 61 64 79 20 61 6e 20 45 58 43 4c 55 53 49 56 eady an EXCLUSIV
12dd4 45 20 6c 6f 63 6b 2c 20 74 68 65 20 66 6f 6c 6c E lock, the foll
12dd5 6f 77 69 6e 67 0a 20 20 2a 2a 20 63 61 6c 6c 20 owing. ** call
12dd6 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a is a no-op.. **
12dd7 0a 20 20 2a 2a 20 4d 6f 76 69 6e 67 20 74 68 65 . ** Moving the
12dd8 20 6c 6f 63 6b 20 66 72 6f 6d 20 52 45 53 45 52 lock from RESER
12dd9 56 45 44 20 74 6f 20 45 58 43 4c 55 53 49 56 45 VED to EXCLUSIVE
12dda 20 61 63 74 75 61 6c 6c 79 20 69 6e 76 6f 6c 76 actually involv
12ddb 65 73 20 67 6f 69 6e 67 0a 20 20 2a 2a 20 74 68 es going. ** th
12ddc 72 6f 75 67 68 20 61 6e 20 69 6e 74 65 72 6d 65 rough an interme
12ddd 64 69 61 74 65 20 73 74 61 74 65 20 50 45 4e 44 diate state PEND
12dde 49 4e 47 2e 20 20 20 41 20 50 45 4e 44 49 4e 47 ING. A PENDING
12ddf 20 6c 6f 63 6b 20 70 72 65 76 65 6e 74 73 20 6e lock prevents n
12de0 65 77 0a 20 20 2a 2a 20 72 65 61 64 65 72 73 20 ew. ** readers
12de1 66 72 6f 6d 20 61 74 74 61 63 68 69 6e 67 20 74 from attaching t
12de2 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 o the database b
12de3 75 74 20 69 73 20 75 6e 73 75 66 66 69 63 69 65 ut is unsufficie
12de4 6e 74 20 66 6f 72 20 75 73 20 74 6f 0a 20 20 2a nt for us to. *
12de5 2a 20 77 72 69 74 65 2e 20 20 54 68 65 20 69 64 * write. The id
12de6 65 61 20 6f 66 20 61 20 50 45 4e 44 49 4e 47 20 ea of a PENDING
12de7 6c 6f 63 6b 20 69 73 20 74 6f 20 70 72 65 76 65 lock is to preve
12de8 6e 74 20 6e 65 77 20 72 65 61 64 65 72 73 20 66 nt new readers f
12de9 72 6f 6d 0a 20 20 2a 2a 20 63 6f 6d 69 6e 67 20 rom. ** coming
12dea 69 6e 20 77 68 69 6c 65 20 77 65 20 77 61 69 74 in while we wait
12deb 20 66 6f 72 20 65 78 69 73 74 69 6e 67 20 72 65 for existing re
12dec 61 64 65 72 73 20 74 6f 20 63 6c 65 61 72 2e 0a aders to clear..
12ded 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 69 6c 65 20 **. ** While
12dee 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
12def 74 68 65 20 52 45 53 45 52 56 45 44 20 73 74 61 the RESERVED sta
12df0 74 65 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c te, the original
12df1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 database file.
12df2 20 2a 2a 20 69 73 20 75 6e 63 68 61 6e 67 65 64 ** is unchanged
12df3 20 61 6e 64 20 77 65 20 63 61 6e 20 72 6f 6c 6c and we can roll
12df4 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68 61 76 back without hav
12df5 69 6e 67 20 74 6f 20 70 6c 61 79 62 61 63 6b 20 ing to playback
12df6 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c the. ** journal
12df7 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 67 69 6e into the origin
12df8 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 al database file
12df9 2e 20 20 4f 6e 63 65 20 77 65 20 74 72 61 6e 73 . Once we trans
12dfa 69 74 69 6f 6e 20 74 6f 0a 20 20 2a 2a 20 45 58 ition to. ** EX
12dfb 43 4c 55 53 49 56 45 2c 20 69 74 20 6d 65 61 6e CLUSIVE, it mean
12dfc 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 s the database f
12dfd 69 6c 65 20 68 61 73 20 62 65 65 6e 20 63 68 61 ile has been cha
12dfe 6e 67 65 64 20 61 6e 64 20 61 6e 79 20 72 6f 6c nged and any rol
12dff 6c 62 61 63 6b 0a 20 20 2a 2a 20 77 69 6c 6c 20 lback. ** will
12e00 72 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 require a journa
12e01 6c 20 70 6c 61 79 62 61 63 6b 2e 0a 20 20 2a 2f l playback.. */
12e02 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
12e03 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f r->state>=PAGER_
12e04 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 72 63 RESERVED );. rc
12e05 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e = pager_wait_on
12e06 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 45 58 _lock(pPager, EX
12e07 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 0a CLUSIVE_LOCK);..
12e08 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 /* If the file
12e09 20 69 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 20 is a temp-file
12e0a 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e has not yet been
12e0b 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e 20 69 74 opened, open it
12e0c 20 6e 6f 77 2e 20 49 74 0a 20 20 2a 2a 20 69 73 now. It. ** is
12e0d 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f not possible fo
12e0e 72 20 72 63 20 74 6f 20 62 65 20 6f 74 68 65 72 r rc to be other
12e0f 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 than SQLITE_OK
12e10 69 66 20 74 68 69 73 20 62 72 61 6e 63 68 0a 20 if this branch.
12e11 20 2a 2a 20 69 73 20 74 61 6b 65 6e 2c 20 61 73 ** is taken, as
12e12 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c pager_wait_on_l
12e13 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 ock() is a no-op
12e14 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c 65 73 2e for temp-files.
12e15 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 4f . */. if( !isO
12e16 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
12e17 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
12e18 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
12e19 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b && rc==SQLITE_OK
12e1a 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 );. rc = pag
12e1b 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65 erOpentemp(pPage
12e1c 72 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 r, pPager->fd, p
12e1d 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 73 29 Pager->vfsFlags)
12e1e 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 ;. }.. while(
12e1f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
12e20 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 50 67 pList ){. Pg
12e21 6e 6f 20 70 67 6e 6f 20 3d 20 70 4c 69 73 74 2d no pgno = pList-
12e22 3e 70 67 6e 6f 3b 0a 0a 20 20 20 20 2f 2a 20 49 >pgno;.. /* I
12e23 66 20 74 68 65 72 65 20 61 72 65 20 64 69 72 74 f there are dirt
12e24 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 70 y pages in the p
12e25 61 67 65 20 63 61 63 68 65 20 77 69 74 68 20 70 age cache with p
12e26 61 67 65 20 6e 75 6d 62 65 72 73 20 67 72 65 61 age numbers grea
12e27 74 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 ter. ** than
12e28 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 74 68 Pager.dbSize, th
12e29 69 73 20 6d 65 61 6e 73 20 73 71 6c 69 74 65 33 is means sqlite3
12e2a 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 PagerTruncateIma
12e2b 67 65 28 29 20 77 61 73 20 63 61 6c 6c 65 64 20 ge() was called
12e2c 74 6f 0a 20 20 20 20 2a 2a 20 6d 61 6b 65 20 74 to. ** make t
12e2d 68 65 20 66 69 6c 65 20 73 6d 61 6c 6c 65 72 20 he file smaller
12e2e 28 70 72 65 73 75 6d 61 62 6c 79 20 62 79 20 61 (presumably by a
12e2f 75 74 6f 2d 76 61 63 75 75 6d 20 63 6f 64 65 29 uto-vacuum code)
12e30 2e 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 0a 20 . Do not write.
12e31 20 20 20 2a 2a 20 61 6e 79 20 73 75 63 68 20 70 ** any such p
12e32 61 67 65 73 20 74 6f 20 74 68 65 20 66 69 6c 65 ages to the file
12e33 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
12e34 41 6c 73 6f 2c 20 64 6f 20 6e 6f 74 20 77 72 69 Also, do not wri
12e35 74 65 20 6f 75 74 20 61 6e 79 20 70 61 67 65 20 te out any page
12e36 74 68 61 74 20 68 61 73 20 74 68 65 20 50 47 48 that has the PGH
12e37 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c DR_DONT_WRITE fl
12e38 61 67 0a 20 20 20 20 2a 2a 20 73 65 74 20 28 73 ag. ** set (s
12e39 65 74 20 62 79 20 73 71 6c 69 74 65 33 50 61 67 et by sqlite3Pag
12e3a 65 72 44 6f 6e 74 57 72 69 74 65 28 29 29 2e 0a erDontWrite())..
12e3b 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
12e3c 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 53 gno<=pPager->dbS
12e3d 69 7a 65 20 26 26 20 30 3d 3d 28 70 4c 69 73 74 ize && 0==(pList
12e3e 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 4f ->flags&PGHDR_DO
12e3f 4e 54 5f 57 52 49 54 45 29 20 29 7b 0a 20 20 20 NT_WRITE) ){.
12e40 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 i64 offset =
12e41 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 (pgno-1)*(i64)pP
12e42 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 20 ager->pageSize;
12e43 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 77 /* Offset to w
12e44 72 69 74 65 20 2a 2f 0a 20 20 20 20 20 20 63 68 rite */. ch
12e45 61 72 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20 ar *pData;
12e46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12e47 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12e48 44 61 74 61 20 74 6f 20 77 72 69 74 65 20 2a 2f Data to write */
12e49 20 20 20 20 0a 0a 20 20 20 20 20 20 2f 2a 20 45 .. /* E
12e4a 6e 63 6f 64 65 20 74 68 65 20 64 61 74 61 62 61 ncode the databa
12e4b 73 65 20 2a 2f 0a 20 20 20 20 20 20 43 4f 44 45 se */. CODE
12e4c 43 32 28 70 50 61 67 65 72 2c 20 70 4c 69 73 74 C2(pPager, pList
12e4d 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 36 ->pData, pgno, 6
12e4e 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
12e4f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 29 3b 0a 0a NOMEM, pData);..
12e50 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6f /* Write o
12e51 75 74 20 74 68 65 20 70 61 67 65 20 64 61 74 61 ut the page data
12e52 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 . */. rc =
12e53 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
12e54 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 61 74 61 Pager->fd, pData
12e55 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 , pPager->pageSi
12e56 7a 65 2c 20 6f 66 66 73 65 74 29 3b 0a 0a 20 20 ze, offset);..
12e57 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65 20 31 /* If page 1
12e58 20 77 61 73 20 6a 75 73 74 20 77 72 69 74 74 65 was just writte
12e59 6e 2c 20 75 70 64 61 74 65 20 50 61 67 65 72 2e n, update Pager.
12e5a 64 62 46 69 6c 65 56 65 72 73 20 74 6f 20 6d 61 dbFileVers to ma
12e5b 74 63 68 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 tch. ** the
12e5c 20 76 61 6c 75 65 20 6e 6f 77 20 73 74 6f 72 65 value now store
12e5d 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 d in the databas
12e5e 65 20 66 69 6c 65 2e 20 49 66 20 77 72 69 74 69 e file. If writi
12e5f 6e 67 20 74 68 69 73 20 0a 20 20 20 20 20 20 2a ng this . *
12e60 2a 20 70 61 67 65 20 63 61 75 73 65 64 20 74 68 * page caused th
12e61 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
12e62 74 6f 20 67 72 6f 77 2c 20 75 70 64 61 74 65 20 to grow, update
12e63 64 62 46 69 6c 65 53 69 7a 65 2e 20 0a 20 20 20 dbFileSize. .
12e64 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
12e65 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 pgno==1 ){.
12e66 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 memcpy(&pPage
12e67 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 26 r->dbFileVers, &
12e68 70 44 61 74 61 5b 32 34 5d 2c 20 73 69 7a 65 6f pData[24], sizeo
12e69 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 f(pPager->dbFile
12e6a 56 65 72 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a Vers));. }.
12e6b 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 if( pgno>p
12e6c 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a Pager->dbFileSiz
12e6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 e ){. pPa
12e6e 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 ger->dbFileSize
12e6f 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a = pgno;. }.
12e70 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65 . /* Update
12e71 20 61 6e 79 20 62 61 63 6b 75 70 20 6f 62 6a 65 any backup obje
12e72 63 74 73 20 63 6f 70 79 69 6e 67 20 74 68 65 20 cts copying the
12e73 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69 73 contents of this
12e74 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20 20 20 20 pager. */.
12e75 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 sqlite3BackupUp
12e76 64 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42 61 date(pPager->pBa
12e77 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 28 75 38 2a ckup, pgno, (u8*
12e78 29 70 4c 69 73 74 2d 3e 70 44 61 74 61 29 3b 0a )pList->pData);.
12e79 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 . PAGERTRAC
12e7a 45 28 28 22 53 54 4f 52 45 20 25 64 20 70 61 67 E(("STORE %d pag
12e7b 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29 5c e %d hash(%08x)\
12e7c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
12e7d 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 PAGERID(p
12e7e 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 Pager), pgno, pa
12e7f 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69 ger_pagehash(pLi
12e80 73 74 29 29 29 3b 0a 20 20 20 20 20 20 49 4f 54 st)));. IOT
12e81 52 41 43 45 28 28 22 50 47 4f 55 54 20 25 70 20 RACE(("PGOUT %p
12e82 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 %d\n", pPager, p
12e83 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 50 41 47 gno));. PAG
12e84 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f ER_INCR(sqlite3_
12e85 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 6f pager_writedb_co
12e86 75 6e 74 29 3b 0a 20 20 20 20 20 20 50 41 47 45 unt);. PAGE
12e87 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e R_INCR(pPager->n
12e88 57 72 69 74 65 29 3b 0a 20 20 20 20 7d 65 6c 73 Write);. }els
12e89 65 7b 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 e{. PAGERTR
12e8a 41 43 45 28 28 22 4e 4f 53 54 4f 52 45 20 25 64 ACE(("NOSTORE %d
12e8b 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 page %d\n", PAG
12e8c 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 67 ERID(pPager), pg
12e8d 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 no));. }.#ifd
12e8e 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f ef SQLITE_CHECK_
12e8f 50 41 47 45 53 0a 20 20 20 20 70 4c 69 73 74 2d PAGES. pList-
12e90 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 >pageHash = page
12e91 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69 73 74 r_pagehash(pList
12e92 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 4c );.#endif. pL
12e93 69 73 74 20 3d 20 70 4c 69 73 74 2d 3e 70 44 69 ist = pList->pDi
12e94 72 74 79 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 rty;. }.. retu
12e95 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
12e96 41 70 70 65 6e 64 20 61 20 72 65 63 6f 72 64 20 Append a record
12e97 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 of the current s
12e98 74 61 74 65 20 6f 66 20 70 61 67 65 20 70 50 67 tate of page pPg
12e99 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 to the sub-jour
12e9a 6e 61 6c 2e 20 0a 2a 2a 20 49 74 20 69 73 20 74 nal. .** It is t
12e9b 68 65 20 63 61 6c 6c 65 72 73 20 72 65 73 70 6f he callers respo
12e9c 6e 73 69 62 69 6c 69 74 79 20 74 6f 20 75 73 65 nsibility to use
12e9d 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 subjRequiresPag
12e9e 65 28 29 20 74 6f 20 63 68 65 63 6b 20 0a 2a 2a e() to check .**
12e9f 20 74 68 61 74 20 69 74 20 69 73 20 72 65 61 6c that it is real
12ea0 6c 79 20 72 65 71 75 69 72 65 64 20 62 65 66 6f ly required befo
12ea1 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 re calling this
12ea2 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
12ea3 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 If successful, s
12ea4 65 74 20 74 68 65 20 62 69 74 20 63 6f 72 72 65 et the bit corre
12ea5 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 50 67 2d sponding to pPg-
12ea6 3e 70 67 6e 6f 20 69 6e 20 74 68 65 20 62 69 74 >pgno in the bit
12ea7 76 65 63 73 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 vecs.** for all
12ea8 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 open savepoints
12ea9 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 before returning
12eaa 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
12eab 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 ction returns SQ
12eac 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 LITE_OK if every
12ead 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 thing is success
12eae 66 75 6c 2c 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 ful, an IO.** er
12eaf 72 6f 72 20 63 6f 64 65 20 69 66 20 74 68 65 20 ror code if the
12eb0 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 attempt to write
12eb1 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 to the sub-jour
12eb2 6e 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a nal fails, or .*
12eb3 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 * SQLITE_NOMEM i
12eb4 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 f a malloc fails
12eb5 20 77 68 69 6c 65 20 73 65 74 74 69 6e 67 20 61 while setting a
12eb6 20 62 69 74 20 69 6e 20 61 20 73 61 76 65 70 6f bit in a savepo
12eb7 69 6e 74 0a 2a 2a 20 62 69 74 76 65 63 2e 0a 2a int.** bitvec..*
12eb8 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 75 62 /.static int sub
12eb9 6a 6f 75 72 6e 61 6c 50 61 67 65 28 50 67 48 64 journalPage(PgHd
12eba 72 20 2a 70 50 67 29 7b 0a 20 20 69 6e 74 20 72 r *pPg){. int r
12ebb 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
12ebc 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
12ebd 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 pPg->pPager;.
12ebe 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 if( isOpen(pPage
12ebf 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 r->sjfd) ){.
12ec0 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20 70 50 void *pData = pP
12ec1 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 69 36 g->pData;. i6
12ec2 34 20 6f 66 66 73 65 74 20 3d 20 70 50 61 67 65 4 offset = pPage
12ec3 72 2d 3e 6e 53 75 62 52 65 63 2a 28 34 2b 70 50 r->nSubRec*(4+pP
12ec4 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b ager->pageSize);
12ec5 0a 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 . char *pData
12ec6 32 3b 0a 0a 20 20 20 20 43 4f 44 45 43 32 28 70 2;.. CODEC2(p
12ec7 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 Pager, pData, pP
12ec8 67 2d 3e 70 67 6e 6f 2c 20 37 2c 20 72 65 74 75 g->pgno, 7, retu
12ec9 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c rn SQLITE_NOMEM,
12eca 20 70 44 61 74 61 32 29 3b 0a 20 20 20 20 50 41 pData2);. PA
12ecb 47 45 52 54 52 41 43 45 28 28 22 53 54 4d 54 2d GERTRACE(("STMT-
12ecc 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20 JOURNAL %d page
12ecd 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 %d\n", PAGERID(p
12ece 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e Pager), pPg->pgn
12ecf 6f 29 29 3b 0a 20 20 0a 20 20 20 20 61 73 73 65 o));. . asse
12ed0 72 74 28 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 rt( pageInJourna
12ed1 6c 28 70 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70 l(pPg) || pPg->p
12ed2 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72 gno>pPager->dbOr
12ed3 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 20 72 63 igSize );. rc
12ed4 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 = write32bits(p
12ed5 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 6f 66 66 Pager->sjfd, off
12ed6 73 65 74 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b set, pPg->pgno);
12ed7 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
12ed8 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
12ed9 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
12eda 69 74 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 ite(pPager->sjfd
12edb 2c 20 70 44 61 74 61 32 2c 20 70 50 61 67 65 72 , pData2, pPager
12edc 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 ->pageSize, offs
12edd 65 74 2b 34 29 3b 0a 20 20 20 20 7d 0a 20 20 7d et+4);. }. }
12ede 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
12edf 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 E_OK ){. pPag
12ee0 65 72 2d 3e 6e 53 75 62 52 65 63 2b 2b 3b 0a 20 er->nSubRec++;.
12ee1 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
12ee2 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3e 30 20 r->nSavepoint>0
12ee3 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 64 64 54 );. rc = addT
12ee4 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63 oSavepointBitvec
12ee5 73 28 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 s(pPager, pPg->p
12ee6 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 gno);. }. retu
12ee7 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
12ee8 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
12ee9 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 s called by the
12eea 70 63 61 63 68 65 20 6c 61 79 65 72 20 77 68 65 pcache layer whe
12eeb 6e 20 69 74 20 68 61 73 20 72 65 61 63 68 65 64 n it has reached
12eec 20 73 6f 6d 65 0a 2a 2a 20 73 6f 66 74 20 6d 65 some.** soft me
12eed 6d 6f 72 79 20 6c 69 6d 69 74 2e 20 54 68 65 20 mory limit. The
12eee 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 first argument i
12eef 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
12ef0 20 50 61 67 65 72 20 6f 62 6a 65 63 74 0a 2a 2a Pager object.**
12ef1 20 28 63 61 73 74 20 61 73 20 61 20 76 6f 69 64 (cast as a void
12ef2 2a 29 2e 20 54 68 65 20 70 61 67 65 72 20 69 73 *). The pager is
12ef3 20 61 6c 77 61 79 73 20 27 70 75 72 67 65 61 62 always 'purgeab
12ef4 6c 65 27 20 28 6e 6f 74 20 61 6e 20 69 6e 2d 6d le' (not an in-m
12ef5 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 61 73 emory.** databas
12ef6 65 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61 e). The second a
12ef7 72 67 75 6d 65 6e 74 20 69 73 20 61 20 72 65 66 rgument is a ref
12ef8 65 72 65 6e 63 65 20 74 6f 20 61 20 70 61 67 65 erence to a page
12ef9 20 74 68 61 74 20 69 73 20 0a 2a 2a 20 63 75 72 that is .** cur
12efa 72 65 6e 74 6c 79 20 64 69 72 74 79 20 62 75 74 rently dirty but
12efb 20 68 61 73 20 6e 6f 20 6f 75 74 73 74 61 6e 64 has no outstand
12efc 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 2e 20 ing references.
12efd 54 68 65 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 The page.** is a
12efe 6c 77 61 79 73 20 61 73 73 6f 63 69 61 74 65 64 lways associated
12eff 20 77 69 74 68 20 74 68 65 20 50 61 67 65 72 20 with the Pager
12f00 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20 61 73 object passed as
12f01 20 74 68 65 20 66 69 72 73 74 20 0a 2a 2a 20 61 the first .** a
12f02 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 rgument..**.** T
12f03 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73 20 66 he job of this f
12f04 75 6e 63 74 69 6f 6e 20 69 73 20 74 6f 20 6d 61 unction is to ma
12f05 6b 65 20 70 50 67 20 63 6c 65 61 6e 20 62 79 20 ke pPg clean by
12f06 77 72 69 74 69 6e 67 20 69 74 73 20 63 6f 6e 74 writing its cont
12f07 65 6e 74 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 74 ents.** out to t
12f08 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12f09 2c 20 69 66 20 70 6f 73 73 69 62 6c 65 2e 20 54 , if possible. T
12f0a 68 69 73 20 6d 61 79 20 69 6e 76 6f 6c 76 65 20 his may involve
12f0b 73 79 6e 63 69 6e 67 20 74 68 65 0a 2a 2a 20 6a syncing the.** j
12f0c 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 0a 2a 2a ournal file. .**
12f0d 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 .** If successfu
12f0e 6c 2c 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 l, sqlite3Pcache
12f0f 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69 73 20 63 MakeClean() is c
12f10 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 70 61 67 alled on the pag
12f11 65 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f e and.** SQLITE_
12f12 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 OK returned. If
12f13 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 an IO error occu
12f14 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67 20 rs while trying
12f15 74 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 70 to make the.** p
12f16 61 67 65 20 63 6c 65 61 6e 2c 20 74 68 65 20 49 age clean, the I
12f17 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 O error code is
12f18 72 65 74 75 72 6e 65 64 2e 20 49 66 20 74 68 65 returned. If the
12f19 20 70 61 67 65 20 63 61 6e 6e 6f 74 20 62 65 0a page cannot be.
12f1a 2a 2a 20 6d 61 64 65 20 63 6c 65 61 6e 20 66 6f ** made clean fo
12f1b 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 72 65 61 r some other rea
12f1c 73 6f 6e 2c 20 62 75 74 20 6e 6f 20 65 72 72 6f son, but no erro
12f1d 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 53 r occurs, then S
12f1e 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 QLITE_OK.** is r
12f1f 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
12f20 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 e3PcacheMakeClea
12f21 6e 28 29 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 n() is not calle
12f22 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
12f23 20 70 61 67 65 72 53 74 72 65 73 73 28 76 6f 69 pagerStress(voi
12f24 64 20 2a 70 2c 20 50 67 48 64 72 20 2a 70 50 67 d *p, PgHdr *pPg
12f25 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 ){. Pager *pPag
12f26 65 72 20 3d 20 28 50 61 67 65 72 20 2a 29 70 3b er = (Pager *)p;
12f27 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
12f28 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 TE_OK;.. assert
12f29 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 ( pPg->pPager==p
12f2a 50 61 67 65 72 20 29 3b 0a 20 20 61 73 73 65 72 Pager );. asser
12f2b 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 t( pPg->flags&PG
12f2c 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 0a 20 20 HDR_DIRTY );..
12f2d 2f 2a 20 54 68 65 20 64 6f 4e 6f 74 53 79 6e 63 /* The doNotSync
12f2e 20 66 6c 61 67 20 69 73 20 73 65 74 20 62 79 20 flag is set by
12f2f 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 the sqlite3Pager
12f30 57 72 69 74 65 28 29 20 66 75 6e 63 74 69 6f 6e Write() function
12f31 20 77 68 69 6c 65 20 69 74 0a 20 20 2a 2a 20 69 while it. ** i
12f32 73 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 61 20 s journalling a
12f33 73 65 74 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f set of two or mo
12f34 72 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 re database page
12f35 73 20 74 68 61 74 20 61 72 65 20 73 74 6f 72 65 s that are store
12f36 64 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 73 61 d. ** on the sa
12f37 6d 65 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 me disk sector.
12f38 53 79 6e 63 69 6e 67 20 74 68 65 20 6a 6f 75 72 Syncing the jour
12f39 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 nal is not allow
12f3a 65 64 20 77 68 69 6c 65 0a 20 20 2a 2a 20 74 68 ed while. ** th
12f3b 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20 is is happening
12f3c 61 73 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 as it is importa
12f3d 6e 74 20 74 68 61 74 20 61 6c 6c 20 6d 65 6d 62 nt that all memb
12f3e 65 72 73 20 6f 66 20 73 75 63 68 20 61 0a 20 20 ers of such a.
12f3f 2a 2a 20 73 65 74 20 6f 66 20 70 61 67 65 73 20 ** set of pages
12f40 61 72 65 20 73 79 6e 63 65 64 20 74 6f 20 64 69 are synced to di
12f41 73 6b 20 74 6f 67 65 74 68 65 72 2e 20 53 6f 2c sk together. So,
12f42 20 69 66 20 74 68 65 20 70 61 67 65 20 74 68 69 if the page thi
12f43 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 s function. **
12f44 69 73 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 6b is trying to mak
12f45 65 20 63 6c 65 61 6e 20 77 69 6c 6c 20 72 65 71 e clean will req
12f46 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 20 73 uire a journal s
12f47 79 6e 63 20 61 6e 64 20 74 68 65 20 64 6f 4e 6f ync and the doNo
12f48 74 53 79 6e 63 0a 20 20 2a 2a 20 66 6c 61 67 20 tSync. ** flag
12f49 69 73 20 73 65 74 2c 20 72 65 74 75 72 6e 20 77 is set, return w
12f4a 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 ithout doing any
12f4b 74 68 69 6e 67 2e 20 54 68 65 20 70 63 61 63 68 thing. The pcach
12f4c 65 20 6c 61 79 65 72 20 77 69 6c 6c 0a 20 20 2a e layer will. *
12f4d 2a 20 6a 75 73 74 20 68 61 76 65 20 74 6f 20 67 * just have to g
12f4e 6f 20 61 68 65 61 64 20 61 6e 64 20 61 6c 6c 6f o ahead and allo
12f4f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 cate a new page
12f50 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f buffer instead o
12f51 66 0a 20 20 2a 2a 20 72 65 75 73 69 6e 67 20 70 f. ** reusing p
12f52 50 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 69 Pg.. **. ** Si
12f53 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74 68 65 20 milarly, if the
12f54 70 61 67 65 72 20 68 61 73 20 61 6c 72 65 61 64 pager has alread
12f55 79 20 65 6e 74 65 72 65 64 20 74 68 65 20 65 72 y entered the er
12f56 72 6f 72 20 73 74 61 74 65 2c 20 64 6f 20 6e 6f ror state, do no
12f57 74 0a 20 20 2a 2a 20 74 72 79 20 74 6f 20 77 72 t. ** try to wr
12f58 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ite the contents
12f59 20 6f 66 20 70 50 67 20 74 6f 20 64 69 73 6b 2e of pPg to disk.
12f5a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 . */. if( NEVE
12f5b 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 R(pPager->errCod
12f5c 65 29 0a 20 20 20 7c 7c 20 28 70 50 61 67 65 72 e). || (pPager
12f5d 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 26 26 20 70 ->doNotSync && p
12f5e 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
12f5f 4e 45 45 44 5f 53 59 4e 43 29 0a 20 20 29 7b 0a NEED_SYNC). ){.
12f60 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
12f61 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 E_OK;. }.. /*
12f62 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c Sync the journal
12f63 20 66 69 6c 65 20 69 66 20 72 65 71 75 69 72 65 file if require
12f64 64 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d d. */. if( pPg-
12f65 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
12f66 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 72 63 D_SYNC ){. rc
12f67 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 = syncJournal(p
12f68 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 Pager);. if(
12f69 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
12f6a 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e pPager->fullSyn
12f6b 63 20 26 26 20 0a 20 20 20 20 20 20 21 28 70 50 c && . !(pP
12f6c 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
12f6d 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
12f6e 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 20 26 26 0a MODE_MEMORY) &&.
12f6f 20 20 20 20 20 20 21 28 73 71 6c 69 74 65 33 4f !(sqlite3O
12f70 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 sDeviceCharacter
12f71 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 istics(pPager->f
12f72 64 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f d)&SQLITE_IOCAP_
12f73 53 41 46 45 5f 41 50 50 45 4e 44 29 0a 20 20 20 SAFE_APPEND).
12f74 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 ){. pPager
12f75 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 ->nRec = 0;.
12f76 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 rc = writeJour
12f77 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a nalHdr(pPager);.
12f78 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
12f79 49 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 If the page numb
12f7a 65 72 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 er of this page
12f7b 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 is larger than t
12f7c 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 he current size
12f7d 6f 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61 of. ** the data
12f7e 62 61 73 65 20 69 6d 61 67 65 2c 20 69 74 20 6d base image, it m
12f7f 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 77 72 ay need to be wr
12f80 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62 itten to the sub
12f81 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 20 54 -journal.. ** T
12f82 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 74 his is because t
12f83 68 65 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 he call to pager
12f84 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28 _write_pagelist(
12f85 29 20 62 65 6c 6f 77 20 77 69 6c 6c 20 6e 6f 74 ) below will not
12f86 0a 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79 20 77 . ** actually w
12f87 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65 rite data to the
12f88 20 66 69 6c 65 20 69 6e 20 74 68 69 73 20 63 61 file in this ca
12f89 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 6f se.. **. ** Co
12f8a 6e 73 69 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f nsider the follo
12f8b 77 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66 wing sequence of
12f8c 20 65 76 65 6e 74 73 3a 0a 20 20 2a 2a 0a 20 20 events:. **.
12f8d 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a 2a ** BEGIN;. **
12f8e 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61 <journal pa
12f8f 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 20 3c ge X>. ** <
12f90 6d 6f 64 69 66 79 20 70 61 67 65 20 58 3e 0a 20 modify page X>.
12f91 20 2a 2a 20 20 20 20 20 53 41 56 45 50 4f 49 4e ** SAVEPOIN
12f92 54 20 73 70 3b 0a 20 20 2a 2a 20 20 20 20 20 20 T sp;. **
12f93 20 3c 73 68 72 69 6e 6b 20 64 61 74 61 62 61 73 <shrink databas
12f94 65 20 66 69 6c 65 20 74 6f 20 59 20 70 61 67 65 e file to Y page
12f95 73 3e 0a 20 20 2a 2a 20 20 20 20 20 20 20 70 61 s>. ** pa
12f96 67 65 72 53 74 72 65 73 73 28 70 61 67 65 20 58 gerStress(page X
12f97 29 0a 20 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 ). ** ROLLB
12f98 41 43 4b 20 54 4f 20 73 70 3b 0a 20 20 2a 2a 0a ACK TO sp;. **.
12f99 20 20 2a 2a 20 49 66 20 28 58 3e 59 29 2c 20 74 ** If (X>Y), t
12f9a 68 65 6e 20 77 68 65 6e 20 70 61 67 65 72 53 74 hen when pagerSt
12f9b 72 65 73 73 20 69 73 20 63 61 6c 6c 65 64 20 70 ress is called p
12f9c 61 67 65 20 58 20 77 69 6c 6c 20 6e 6f 74 20 62 age X will not b
12f9d 65 20 77 72 69 74 74 65 6e 0a 20 20 2a 2a 20 6f e written. ** o
12f9e 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ut to the databa
12f9f 73 65 20 66 69 6c 65 2c 20 62 75 74 20 77 69 6c se file, but wil
12fa0 6c 20 62 65 20 64 72 6f 70 70 65 64 20 66 72 6f l be dropped fro
12fa1 6d 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65 m the cache. The
12fa2 6e 2c 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e n,. ** followin
12fa3 67 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 g the "ROLLBACK
12fa4 54 4f 20 73 70 22 20 73 74 61 74 65 6d 65 6e 74 TO sp" statement
12fa5 2c 20 72 65 61 64 69 6e 67 20 70 61 67 65 20 58 , reading page X
12fa6 20 77 69 6c 6c 20 72 65 61 64 0a 20 20 2a 2a 20 will read. **
12fa7 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61 data from the da
12fa8 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 tabase file. Thi
12fa9 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20 63 6f s will be the co
12faa 70 79 20 6f 66 20 70 61 67 65 20 58 20 61 73 20 py of page X as
12fab 69 74 0a 20 20 2a 2a 20 77 61 73 20 77 68 65 6e it. ** was when
12fac 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
12fad 20 73 74 61 72 74 65 64 2c 20 6e 6f 74 20 61 73 started, not as
12fae 20 69 74 20 77 61 73 20 77 68 65 6e 20 22 53 41 it was when "SA
12faf 56 45 50 4f 49 4e 54 20 73 70 22 0a 20 20 2a 2a VEPOINT sp". **
12fb0 20 77 61 73 20 65 78 65 63 75 74 65 64 2e 0a 20 was executed..
12fb1 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 6f 6c **. ** The sol
12fb2 75 74 69 6f 6e 20 69 73 20 74 6f 20 77 72 69 74 ution is to writ
12fb3 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 e the current da
12fb4 74 61 20 66 6f 72 20 70 61 67 65 20 58 20 69 6e ta for page X in
12fb5 74 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 to the . ** sub
12fb6 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 6f -journal file no
12fb7 77 20 28 69 66 20 69 74 20 69 73 20 6e 6f 74 20 w (if it is not
12fb8 61 6c 72 65 61 64 79 20 74 68 65 72 65 29 2c 20 already there),
12fb9 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 0a so that it will.
12fba 20 20 2a 2a 20 62 65 20 72 65 73 74 6f 72 65 64 ** be restored
12fbb 20 74 6f 20 69 74 73 20 63 75 72 72 65 6e 74 20 to its current
12fbc 76 61 6c 75 65 20 77 68 65 6e 20 74 68 65 20 22 value when the "
12fbd 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 ROLLBACK TO sp"
12fbe 69 73 20 0a 20 20 2a 2a 20 65 78 65 63 75 74 65 is . ** execute
12fbf 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 d.. */. if( NE
12fc0 56 45 52 28 0a 20 20 20 20 20 20 72 63 3d 3d 53 VER(. rc==S
12fc1 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 67 2d QLITE_OK && pPg-
12fc2 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 >pgno>pPager->db
12fc3 53 69 7a 65 20 26 26 20 73 75 62 6a 52 65 71 75 Size && subjRequ
12fc4 69 72 65 73 50 61 67 65 28 70 50 67 29 0a 20 20 iresPage(pPg).
12fc5 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 75 ) ){. rc = su
12fc6 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67 bjournalPage(pPg
12fc7 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 );. }.. /* Wri
12fc8 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 te the contents
12fc9 6f 66 20 74 68 65 20 70 61 67 65 20 6f 75 74 20 of the page out
12fca 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
12fcb 66 69 6c 65 2e 20 2a 2f 0a 20 20 69 66 28 20 72 file. */. if( r
12fcc 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
12fcd 20 20 20 20 70 50 67 2d 3e 70 44 69 72 74 79 20 pPg->pDirty
12fce 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 = 0;. rc = pa
12fcf 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 ger_write_pageli
12fd0 73 74 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 st(pPg);. }..
12fd1 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 /* Mark the page
12fd2 20 61 73 20 63 6c 65 61 6e 2e 20 2a 2f 0a 20 20 as clean. */.
12fd3 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
12fd4 4b 20 29 7b 0a 20 20 20 20 50 41 47 45 52 54 52 K ){. PAGERTR
12fd5 41 43 45 28 28 22 53 54 52 45 53 53 20 25 64 20 ACE(("STRESS %d
12fd6 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 page %d\n", PAGE
12fd7 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 RID(pPager), pPg
12fd8 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 73 71 ->pgno));. sq
12fd9 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 lite3PcacheMakeC
12fda 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 7d 0a 0a lean(pPg);. }..
12fdb 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 return pager_e
12fdc 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 rror(pPager, rc)
12fdd 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f ;.}.../*.** Allo
12fde 63 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c cate and initial
12fdf 69 7a 65 20 61 20 6e 65 77 20 50 61 67 65 72 20 ize a new Pager
12fe0 6f 62 6a 65 63 74 20 61 6e 64 20 70 75 74 20 61 object and put a
12fe1 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 0a 2a pointer to it.*
12fe2 2a 20 69 6e 20 2a 70 70 50 61 67 65 72 2e 20 54 * in *ppPager. T
12fe3 68 65 20 70 61 67 65 72 20 73 68 6f 75 6c 64 20 he pager should
12fe4 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 66 72 eventually be fr
12fe5 65 65 64 20 62 79 20 70 61 73 73 69 6e 67 20 69 eed by passing i
12fe6 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 t.** to sqlite3P
12fe7 61 67 65 72 43 6c 6f 73 65 28 29 2e 0a 2a 2a 0a agerClose()..**.
12fe8 2a 2a 20 54 68 65 20 7a 46 69 6c 65 6e 61 6d 65 ** The zFilename
12fe9 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
12fea 20 70 61 74 68 20 74 6f 20 74 68 65 20 64 61 74 path to the dat
12feb 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 abase file to op
12fec 65 6e 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 6e en..** If zFilen
12fed 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e ame is NULL then
12fee 20 61 20 72 61 6e 64 6f 6d 6c 79 2d 6e 61 6d 65 a randomly-name
12fef 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 d temporary file
12ff0 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 is created.** a
12ff1 6e 64 20 75 73 65 64 20 61 73 20 74 68 65 20 66 nd used as the f
12ff2 69 6c 65 20 74 6f 20 62 65 20 63 61 63 68 65 64 ile to be cached
12ff3 2e 20 54 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 . Temporary file
12ff4 73 20 61 72 65 20 62 65 20 64 65 6c 65 74 65 64 s are be deleted
12ff5 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c .** automaticall
12ff6 79 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 y when they are
12ff7 63 6c 6f 73 65 64 2e 20 49 66 20 7a 46 69 6c 65 closed. If zFile
12ff8 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 name is ":memory
12ff9 3a 22 20 74 68 65 6e 20 0a 2a 2a 20 61 6c 6c 20 :" then .** all
12ffa 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 68 information is h
12ffb 65 6c 64 20 69 6e 20 63 61 63 68 65 2e 20 49 74 eld in cache. It
12ffc 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 65 is never writte
12ffd 6e 20 74 6f 20 64 69 73 6b 2e 20 0a 2a 2a 20 54 n to disk. .** T
12ffe 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 his can be used
12fff 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 to implement an
13000 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
13001 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 45 se..**.** The nE
13002 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 73 xtra parameter s
13003 70 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 6d pecifies the num
13004 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
13005 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 0a space allocated.
13006 2a 2a 20 61 6c 6f 6e 67 20 77 69 74 68 20 65 61 ** along with ea
13007 63 68 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 ch page referenc
13008 65 2e 20 54 68 69 73 20 73 70 61 63 65 20 69 73 e. This space is
13009 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 available to th
1300a 65 20 75 73 65 72 0a 2a 2a 20 76 69 61 20 74 68 e user.** via th
1300b 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 e sqlite3PagerGe
1300c 74 45 78 74 72 61 28 29 20 41 50 49 2e 0a 2a 2a tExtra() API..**
1300d 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 .** The flags ar
1300e 67 75 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74 gument is used t
1300f 6f 20 73 70 65 63 69 66 79 20 70 72 6f 70 65 72 o specify proper
13010 74 69 65 73 20 74 68 61 74 20 61 66 66 65 63 74 ties that affect
13011 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 69 6f the.** operatio
13012 6e 20 6f 66 20 74 68 65 20 70 61 67 65 72 2e 20 n of the pager.
13013 49 74 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 It should be pas
13014 73 65 64 20 73 6f 6d 65 20 62 69 74 77 69 73 65 sed some bitwise
13015 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 0a 2a 2a 20 combination.**
13016 6f 66 20 74 68 65 20 50 41 47 45 52 5f 4f 4d 49 of the PAGER_OMI
13017 54 5f 4a 4f 55 52 4e 41 4c 20 61 6e 64 20 50 41 T_JOURNAL and PA
13018 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 GER_NO_READLOCK
13019 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 flags..**.** The
1301a 20 76 66 73 46 6c 61 67 73 20 70 61 72 61 6d 65 vfsFlags parame
1301b 74 65 72 20 69 73 20 61 20 62 69 74 6d 61 73 6b ter is a bitmask
1301c 20 74 6f 20 70 61 73 73 20 74 6f 20 74 68 65 20 to pass to the
1301d 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 0a flags parameter.
1301e 2a 2a 20 6f 66 20 74 68 65 20 78 4f 70 65 6e 28 ** of the xOpen(
1301f 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 ) method of the
13020 73 75 70 70 6c 69 65 64 20 56 46 53 20 77 68 65 supplied VFS whe
13021 6e 20 6f 70 65 6e 69 6e 67 20 66 69 6c 65 73 2e n opening files.
13022 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 .**.** If the p
13023 61 67 65 72 20 6f 62 6a 65 63 74 20 69 73 20 61 ager object is a
13024 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 llocated and the
13025 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 20 specified file
13026 6f 70 65 6e 65 64 20 0a 2a 2a 20 73 75 63 63 65 opened .** succe
13027 73 73 66 75 6c 6c 79 2c 20 53 51 4c 49 54 45 5f ssfully, SQLITE_
13028 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 OK is returned a
13029 6e 64 20 2a 70 70 50 61 67 65 72 20 73 65 74 20 nd *ppPager set
1302a 74 6f 20 70 6f 69 6e 74 20 74 6f 0a 2a 2a 20 74 to point to.** t
1302b 68 65 20 6e 65 77 20 70 61 67 65 72 20 6f 62 6a he new pager obj
1302c 65 63 74 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 ect. If an error
1302d 20 6f 63 63 75 72 73 2c 20 2a 70 70 50 61 67 65 occurs, *ppPage
1302e 72 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c r is set to NULL
1302f 0a 2a 2a 20 61 6e 64 20 65 72 72 6f 72 20 63 6f .** and error co
13030 64 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 de returned. Thi
13031 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 s function may r
13032 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
13033 45 4d 0a 2a 2a 20 28 73 71 6c 69 74 65 33 4d 61 EM.** (sqlite3Ma
13034 6c 6c 6f 63 28 29 20 69 73 20 75 73 65 64 20 74 lloc() is used t
13035 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 o allocate memor
13036 79 29 2c 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f y), SQLITE_CANTO
13037 50 45 4e 20 6f 72 20 0a 2a 2a 20 76 61 72 69 6f PEN or .** vario
13038 75 73 20 53 51 4c 49 54 45 5f 49 4f 5f 58 58 58 us SQLITE_IO_XXX
13039 20 65 72 72 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49 errors..*/.SQLI
1303a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1303b 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 qlite3PagerOpen(
1303c 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
1303d 70 56 66 73 2c 20 20 20 20 20 20 20 2f 2a 20 54 pVfs, /* T
1303e 68 65 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20 he virtual file
1303f 73 79 73 74 65 6d 20 74 6f 20 75 73 65 20 2a 2f system to use */
13040 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67 . Pager **ppPag
13041 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f er, /* O
13042 55 54 3a 20 52 65 74 75 72 6e 20 74 68 65 20 50 UT: Return the P
13043 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 68 ager structure h
13044 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ere */. const c
13045 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 har *zFilename,
13046 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
13047 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
13048 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 o open */. int
13049 6e 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20 nExtra,
1304a 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 62 79 /* Extra by
1304b 74 65 73 20 61 70 70 65 6e 64 20 74 6f 20 65 61 tes append to ea
1304c 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 ch in-memory pag
1304d 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 e */. int flags
1304e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1304f 2f 2a 20 66 6c 61 67 73 20 63 6f 6e 74 72 6f 6c /* flags control
13050 6c 69 6e 67 20 74 68 69 73 20 66 69 6c 65 20 2a ling this file *
13051 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 /. int vfsFlags
13052 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
13053 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 flags passed thr
13054 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f ough to sqlite3_
13055 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 vfs.xOpen() */.
13056 20 76 6f 69 64 20 28 2a 78 52 65 69 6e 69 74 29 void (*xReinit)
13057 28 44 62 50 61 67 65 2a 29 20 2f 2a 20 46 75 6e (DbPage*) /* Fun
13058 63 74 69 6f 6e 20 74 6f 20 72 65 69 6e 69 74 69 ction to reiniti
13059 61 6c 69 7a 65 20 70 61 67 65 73 20 2a 2f 0a 29 alize pages */.)
1305a 7b 0a 20 20 75 38 20 2a 70 50 74 72 3b 0a 20 20 {. u8 *pPtr;.
1305b 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
1305c 30 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 0; /* Page
1305d 72 20 6f 62 6a 65 63 74 20 74 6f 20 61 6c 6c 6f r object to allo
1305e 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 cate and return
1305f 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 */. int rc = SQ
13060 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a LITE_OK; /*
13061 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
13062 20 20 69 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d int tempFile =
13063 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 0; /* Tr
13064 75 65 20 66 6f 72 20 74 65 6d 70 20 66 69 6c 65 ue for temp file
13065 73 20 28 69 6e 63 6c 2e 20 69 6e 2d 6d 65 6d 6f s (incl. in-memo
13066 72 79 20 66 69 6c 65 73 29 20 2a 2f 0a 20 20 69 ry files) */. i
13067 6e 74 20 6d 65 6d 44 62 20 3d 20 30 3b 20 20 20 nt memDb = 0;
13068 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
13069 69 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e if this is an in
1306a 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0a -memory file */.
1306b 20 20 69 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d int readOnly =
1306c 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 0; /* Tr
1306d 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20 ue if this is a
1306e 72 65 61 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 2a read-only file *
1306f 2f 0a 20 20 69 6e 74 20 6a 6f 75 72 6e 61 6c 46 /. int journalF
13070 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 ileSize; /*
13071 42 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 Bytes to allocat
13072 65 20 66 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e e for each journ
13073 61 6c 20 66 64 20 2a 2f 0a 20 20 63 68 61 72 20 al fd */. char
13074 2a 7a 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20 *zPathname = 0;
13075 20 20 20 20 2f 2a 20 46 75 6c 6c 20 70 61 74 68 /* Full path
13076 20 74 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c to database fil
13077 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 68 e */. int nPath
13078 6e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 name = 0;
13079 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1307a 65 73 20 69 6e 20 7a 50 61 74 68 6e 61 6d 65 20 es in zPathname
1307b 2a 2f 0a 20 20 69 6e 74 20 75 73 65 4a 6f 75 72 */. int useJour
1307c 6e 61 6c 20 3d 20 28 66 6c 61 67 73 20 26 20 50 nal = (flags & P
1307d 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 AGER_OMIT_JOURNA
1307e 4c 29 3d 3d 30 3b 20 2f 2a 20 46 61 6c 73 65 20 L)==0; /* False
1307f 74 6f 20 6f 6d 69 74 20 6a 6f 75 72 6e 61 6c 20 to omit journal
13080 2a 2f 0a 20 20 69 6e 74 20 6e 6f 52 65 61 64 6c */. int noReadl
13081 6f 63 6b 20 3d 20 28 66 6c 61 67 73 20 26 20 50 ock = (flags & P
13082 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b AGER_NO_READLOCK
13083 29 21 3d 30 3b 20 20 2f 2a 20 54 72 75 65 20 74 )!=0; /* True t
13084 6f 20 6f 6d 69 74 20 72 65 61 64 2d 6c 6f 63 6b o omit read-lock
13085 20 2a 2f 0a 20 20 69 6e 74 20 70 63 61 63 68 65 */. int pcache
13086 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 50 63 Size = sqlite3Pc
13087 61 63 68 65 53 69 7a 65 28 29 3b 20 20 20 20 20 acheSize();
13088 20 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 6c /* Bytes to al
13089 6c 6f 63 61 74 65 20 66 6f 72 20 50 43 61 63 68 locate for PCach
1308a 65 20 2a 2f 0a 20 20 75 31 36 20 73 7a 50 61 67 e */. u16 szPag
1308b 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f 44 eDflt = SQLITE_D
1308c 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
1308d 3b 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 70 61 ; /* Default pa
1308e 67 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 2f 2a ge size */.. /*
1308f 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 Figure out how
13090 6d 75 63 68 20 73 70 61 63 65 20 69 73 20 72 65 much space is re
13091 71 75 69 72 65 64 20 66 6f 72 20 65 61 63 68 20 quired for each
13092 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e journal file-han
13093 64 6c 65 0a 20 20 2a 2a 20 28 74 68 65 72 65 20 dle. ** (there
13094 61 72 65 20 74 77 6f 20 6f 66 20 74 68 65 6d 2c are two of them,
13095 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
13096 6c 20 61 6e 64 20 74 68 65 20 73 75 62 2d 6a 6f l and the sub-jo
13097 75 72 6e 61 6c 29 2e 20 54 68 69 73 0a 20 20 2a urnal). This. *
13098 2a 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d * is the maximum
13099 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 space required
1309a 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 for an in-memory
1309b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 journal file ha
1309c 6e 64 6c 65 20 0a 20 20 2a 2a 20 61 6e 64 20 61 ndle . ** and a
1309d 20 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c regular journal
1309e 20 66 69 6c 65 2d 68 61 6e 64 6c 65 2e 20 4e 6f file-handle. No
1309f 74 65 20 74 68 61 74 20 61 20 22 72 65 67 75 6c te that a "regul
130a0 61 72 20 6a 6f 75 72 6e 61 6c 2d 68 61 6e 64 6c ar journal-handl
130a1 65 22 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 61 e". ** may be a
130a2 20 77 72 61 70 70 65 72 20 63 61 70 61 62 6c 65 wrapper capable
130a3 20 6f 66 20 63 61 63 68 69 6e 67 20 74 68 65 20 of caching the
130a4 66 69 72 73 74 20 70 6f 72 74 69 6f 6e 20 6f 66 first portion of
130a5 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a the journal. *
130a6 2a 20 66 69 6c 65 20 69 6e 20 6d 65 6d 6f 72 79 * file in memory
130a7 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 to implement th
130a8 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f e atomic-write o
130a9 70 74 69 6d 69 7a 61 74 69 6f 6e 20 28 73 65 65 ptimization (see
130aa 20 0a 20 20 2a 2a 20 73 6f 75 72 63 65 20 66 69 . ** source fi
130ab 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 29 2e 0a 20 le journal.c)..
130ac 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 */. if( sqlite
130ad 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 3JournalSize(pVf
130ae 73 29 3e 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 s)>sqlite3MemJou
130af 72 6e 61 6c 53 69 7a 65 28 29 20 29 7b 0a 20 20 rnalSize() ){.
130b0 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a journalFileSiz
130b1 65 20 3d 20 52 4f 55 4e 44 38 28 73 71 6c 69 74 e = ROUND8(sqlit
130b2 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 e3JournalSize(pV
130b3 66 73 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 fs));. }else{.
130b4 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 journalFileSi
130b5 7a 65 20 3d 20 52 4f 55 4e 44 38 28 73 71 6c 69 ze = ROUND8(sqli
130b6 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a te3MemJournalSiz
130b7 65 28 29 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 e());. }.. /*
130b8 53 65 74 20 74 68 65 20 6f 75 74 70 75 74 20 76 Set the output v
130b9 61 72 69 61 62 6c 65 20 74 6f 20 4e 55 4c 4c 20 ariable to NULL
130ba 69 6e 20 63 61 73 65 20 61 6e 20 65 72 72 6f 72 in case an error
130bb 20 6f 63 63 75 72 73 2e 20 2a 2f 0a 20 20 2a 70 occurs. */. *p
130bc 70 50 61 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f pPager = 0;.. /
130bd 2a 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 73 74 * Compute and st
130be 6f 72 65 20 74 68 65 20 66 75 6c 6c 20 70 61 74 ore the full pat
130bf 68 6e 61 6d 65 20 69 6e 20 61 6e 20 61 6c 6c 6f hname in an allo
130c0 63 61 74 65 64 20 62 75 66 66 65 72 20 70 6f 69 cated buffer poi
130c1 6e 74 65 64 0a 20 20 2a 2a 20 74 6f 20 62 79 20 nted. ** to by
130c2 7a 50 61 74 68 6e 61 6d 65 2c 20 6c 65 6e 67 74 zPathname, lengt
130c3 68 20 6e 50 61 74 68 6e 61 6d 65 2e 20 4f 72 2c h nPathname. Or,
130c4 20 69 66 20 74 68 69 73 20 69 73 20 61 20 74 65 if this is a te
130c5 6d 70 6f 72 61 72 79 20 66 69 6c 65 2c 0a 20 20 mporary file,.
130c6 2a 2a 20 6c 65 61 76 65 20 62 6f 74 68 20 6e 50 ** leave both nP
130c7 61 74 68 6e 61 6d 65 20 61 6e 64 20 7a 50 61 74 athname and zPat
130c8 68 6e 61 6d 65 20 73 65 74 20 74 6f 20 30 2e 0a hname set to 0..
130c9 20 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 */. if( zFile
130ca 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d name && zFilenam
130cb 65 5b 30 5d 20 29 7b 0a 20 20 20 20 6e 50 61 74 e[0] ){. nPat
130cc 68 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 hname = pVfs->mx
130cd 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 Pathname+1;.
130ce 7a 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 zPathname = sqli
130cf 74 65 33 4d 61 6c 6c 6f 63 28 6e 50 61 74 68 6e te3Malloc(nPathn
130d0 61 6d 65 2a 32 29 3b 0a 20 20 20 20 69 66 28 20 ame*2);. if(
130d1 7a 50 61 74 68 6e 61 6d 65 3d 3d 30 20 29 7b 0a zPathname==0 ){.
130d2 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
130d3 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
130d4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
130d5 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 OMIT_MEMORYDB.
130d6 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a 46 69 if( strcmp(zFi
130d7 6c 65 6e 61 6d 65 2c 22 3a 6d 65 6d 6f 72 79 3a lename,":memory:
130d8 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d ")==0 ){. m
130d9 65 6d 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 emDb = 1;.
130da 7a 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 zPathname[0] = 0
130db 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 ;. }else.#end
130dc 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 7a if. {. z
130dd 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b Pathname[0] = 0;
130de 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 69 6e /* Make sure in
130df 69 74 69 61 6c 69 7a 65 64 20 65 76 65 6e 20 69 itialized even i
130e0 66 20 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 f FullPathname()
130e1 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 20 20 fails */.
130e2 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 75 rc = sqlite3OsFu
130e3 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c llPathname(pVfs,
130e4 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 zFilename, nPat
130e5 68 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 hname, zPathname
130e6 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 50 );. }.. nP
130e7 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 athname = sqlite
130e8 33 53 74 72 6c 65 6e 33 30 28 7a 50 61 74 68 6e 3Strlen30(zPathn
130e9 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ame);. if( rc
130ea 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e ==SQLITE_OK && n
130eb 50 61 74 68 6e 61 6d 65 2b 38 3e 70 56 66 73 2d Pathname+8>pVfs-
130ec 3e 6d 78 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 >mxPathname ){.
130ed 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 /* This bra
130ee 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 77 68 65 nch is taken whe
130ef 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 70 61 n the journal pa
130f0 74 68 20 72 65 71 75 69 72 65 64 20 62 79 0a 20 th required by.
130f1 20 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 ** the data
130f2 62 61 73 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 base being opene
130f3 64 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 74 d will be more t
130f4 68 61 6e 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 han pVfs->mxPath
130f5 6e 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 name. ** by
130f6 74 65 73 20 69 6e 20 6c 65 6e 67 74 68 2e 20 54 tes in length. T
130f7 68 69 73 20 6d 65 61 6e 73 20 74 68 65 20 64 61 his means the da
130f8 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 tabase cannot be
130f9 20 6f 70 65 6e 65 64 2c 0a 20 20 20 20 20 20 2a opened,. *
130fa 2a 20 61 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 * as it will not
130fb 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 be possible to
130fc 6f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c open the journal
130fd 20 66 69 6c 65 20 6f 72 20 65 76 65 6e 0a 20 20 file or even.
130fe 20 20 20 20 2a 2a 20 63 68 65 63 6b 20 66 6f 72 ** check for
130ff 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 a hot-journal b
13100 65 66 6f 72 65 20 72 65 61 64 69 6e 67 2e 0a 20 efore reading..
13101 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 */. rc
13102 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 = SQLITE_CANTOP
13103 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 EN;. }. if
13104 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
13105 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
13106 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 _free(zPathname)
13107 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
13108 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 c;. }. }..
13109 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f /* Allocate memo
1310a 72 79 20 66 6f 72 20 74 68 65 20 50 61 67 65 72 ry for the Pager
1310b 20 73 74 72 75 63 74 75 72 65 2c 20 50 43 61 63 structure, PCac
1310c 68 65 20 6f 62 6a 65 63 74 2c 20 74 68 65 0a 20 he object, the.
1310d 20 2a 2a 20 74 68 72 65 65 20 66 69 6c 65 20 64 ** three file d
1310e 65 73 63 72 69 70 74 6f 72 73 2c 20 74 68 65 20 escriptors, the
1310f 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6e 61 database file na
13110 6d 65 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e me and the journ
13111 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 6e 61 al . ** file na
13112 6d 65 2e 20 54 68 65 20 6c 61 79 6f 75 74 20 69 me. The layout i
13113 6e 20 6d 65 6d 6f 72 79 20 69 73 20 61 73 20 66 n memory is as f
13114 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a ollows:. **. *
13115 2a 20 20 20 20 20 50 61 67 65 72 20 6f 62 6a 65 * Pager obje
13116 63 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ct
13117 20 20 20 20 20 20 28 73 69 7a 65 6f 66 28 50 61 (sizeof(Pa
13118 67 65 72 29 20 62 79 74 65 73 29 0a 20 20 2a 2a ger) bytes). **
13119 20 20 20 20 20 50 43 61 63 68 65 20 6f 62 6a 65 PCache obje
1311a 63 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ct
1311b 20 20 20 20 20 28 73 71 6c 69 74 65 33 50 63 61 (sqlite3Pca
1311c 63 68 65 53 69 7a 65 28 29 20 62 79 74 65 73 29 cheSize() bytes)
1311d 0a 20 20 2a 2a 20 20 20 20 20 44 61 74 61 62 61 . ** Databa
1311e 73 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 20 se file handle
1311f 20 20 20 20 20 20 20 20 20 20 28 70 56 66 73 2d (pVfs-
13120 3e 73 7a 4f 73 46 69 6c 65 20 62 79 74 65 73 29 >szOsFile bytes)
13121 0a 20 20 2a 2a 20 20 20 20 20 53 75 62 2d 6a 6f . ** Sub-jo
13122 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c urnal file handl
13123 65 20 20 20 20 20 20 20 20 20 28 6a 6f 75 72 6e e (journ
13124 61 6c 46 69 6c 65 53 69 7a 65 20 62 79 74 65 73 alFileSize bytes
13125 29 0a 20 20 2a 2a 20 20 20 20 20 4d 61 69 6e 20 ). ** Main
13126 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e journal file han
13127 64 6c 65 20 20 20 20 20 20 20 20 28 6a 6f 75 72 dle (jour
13128 6e 61 6c 46 69 6c 65 53 69 7a 65 20 62 79 74 65 nalFileSize byte
13129 73 29 0a 20 20 2a 2a 20 20 20 20 20 44 61 74 61 s). ** Data
1312a 62 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20 20 base file name
1312b 20 20 20 20 20 20 20 20 20 20 20 20 28 6e 50 61 (nPa
1312c 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 29 0a thname+1 bytes).
1312d 20 20 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c ** Journal
1312e 20 66 69 6c 65 20 6e 61 6d 65 20 20 20 20 20 20 file name
1312f 20 20 20 20 20 20 20 20 20 28 6e 50 61 74 68 6e (nPathn
13130 61 6d 65 2b 38 2b 31 20 62 79 74 65 73 29 0a 20 ame+8+1 bytes).
13131 20 2a 2f 0a 20 20 70 50 74 72 20 3d 20 28 75 38 */. pPtr = (u8
13132 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 *)sqlite3Malloc
13133 5a 65 72 6f 28 0a 20 20 20 20 52 4f 55 4e 44 38 Zero(. ROUND8
13134 28 73 69 7a 65 6f 66 28 2a 70 50 61 67 65 72 29 (sizeof(*pPager)
13135 29 20 2b 20 20 20 20 20 20 2f 2a 20 50 61 67 65 ) + /* Page
13136 72 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 r structure */.
13137 20 20 20 52 4f 55 4e 44 38 28 70 63 61 63 68 65 ROUND8(pcache
13138 53 69 7a 65 29 20 2b 20 20 20 20 20 20 20 20 20 Size) +
13139 20 20 2f 2a 20 50 43 61 63 68 65 20 6f 62 6a 65 /* PCache obje
1313a 63 74 20 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38 ct */. ROUND8
1313b 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 (pVfs->szOsFile)
1313c 20 2b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 + /* The
1313d 6d 61 69 6e 20 64 62 20 66 69 6c 65 20 2a 2f 0a main db file */.
1313e 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 journalFileS
1313f 69 7a 65 20 2a 20 32 20 2b 20 20 20 20 20 20 20 ize * 2 +
13140 20 20 20 2f 2a 20 54 68 65 20 74 77 6f 20 6a 6f /* The two jo
13141 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 20 0a urnal files */ .
13142 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20 nPathname +
13143 31 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 1 +
13144 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 /* zFilename
13145 2a 2f 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 */. nPathname
13146 20 2b 20 38 20 2b 20 31 20 20 20 20 20 20 20 20 + 8 + 1
13147 20 20 20 20 20 20 2f 2a 20 7a 4a 6f 75 72 6e 61 /* zJourna
13148 6c 20 2a 2f 0a 20 20 29 3b 0a 20 20 61 73 73 65 l */. );. asse
13149 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
1314a 4c 49 47 4e 4d 45 4e 54 28 53 51 4c 49 54 45 5f LIGNMENT(SQLITE_
1314b 49 4e 54 5f 54 4f 5f 50 54 52 28 6a 6f 75 72 6e INT_TO_PTR(journ
1314c 61 6c 46 69 6c 65 53 69 7a 65 29 29 20 29 3b 0a alFileSize)) );.
1314d 20 20 69 66 28 20 21 70 50 74 72 20 29 7b 0a 20 if( !pPtr ){.
1314e 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
1314f 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 zPathname);.
13150 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
13151 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 MEM;. }. pPage
13152 72 20 3d 20 20 20 20 20 20 20 20 20 20 20 20 20 r =
13153 20 28 50 61 67 65 72 2a 29 28 70 50 74 72 29 3b (Pager*)(pPtr);
13154 0a 20 20 70 50 61 67 65 72 2d 3e 70 50 43 61 63 . pPager->pPCac
13155 68 65 20 3d 20 20 20 20 28 50 43 61 63 68 65 2a he = (PCache*
13156 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 4e 44 38 )(pPtr += ROUND8
13157 28 73 69 7a 65 6f 66 28 2a 70 50 61 67 65 72 29 (sizeof(*pPager)
13158 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 64 ));. pPager->fd
13159 20 3d 20 20 20 28 73 71 6c 69 74 65 33 5f 66 69 = (sqlite3_fi
1315a 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 le*)(pPtr += ROU
1315b 4e 44 38 28 70 63 61 63 68 65 53 69 7a 65 29 29 ND8(pcacheSize))
1315c 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 ;. pPager->sjfd
1315d 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 = (sqlite3_file
1315e 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 4e 44 *)(pPtr += ROUND
1315f 38 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 8(pVfs->szOsFile
13160 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 66 ));. pPager->jf
13161 64 20 3d 20 20 28 73 71 6c 69 74 65 33 5f 66 69 d = (sqlite3_fi
13162 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75 le*)(pPtr += jou
13163 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20 rnalFileSize);.
13164 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 pPager->zFilena
13165 6d 65 20 3d 20 20 20 20 28 63 68 61 72 2a 29 28 me = (char*)(
13166 70 50 74 72 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46 pPtr += journalF
13167 69 6c 65 53 69 7a 65 29 3b 0a 20 20 61 73 73 65 ileSize);. asse
13168 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
13169 4c 49 47 4e 4d 45 4e 54 28 70 50 61 67 65 72 2d LIGNMENT(pPager-
1316a 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 >jfd) );.. /* F
1316b 69 6c 6c 20 69 6e 20 74 68 65 20 50 61 67 65 72 ill in the Pager
1316c 2e 7a 46 69 6c 65 6e 61 6d 65 20 61 6e 64 20 50 .zFilename and P
1316d 61 67 65 72 2e 7a 4a 6f 75 72 6e 61 6c 20 62 75 ager.zJournal bu
1316e 66 66 65 72 73 2c 20 69 66 20 72 65 71 75 69 72 ffers, if requir
1316f 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 7a 50 61 ed. */. if( zPa
13170 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 70 50 thname ){. pP
13171 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d ager->zJournal =
13172 20 20 20 28 63 68 61 72 2a 29 28 70 50 74 72 20 (char*)(pPtr
13173 2b 3d 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20 31 += nPathname + 1
13174 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 );. memcpy(pP
13175 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c ager->zFilename,
13176 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74 zPathname, nPat
13177 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63 hname);. memc
13178 70 79 28 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 py(pPager->zJour
13179 6e 61 6c 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20 nal, zPathname,
1317a 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 nPathname);.
1317b 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e memcpy(&pPager->
1317c 7a 4a 6f 75 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 zJournal[nPathna
1317d 6d 65 5d 2c 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c me], "-journal",
1317e 20 38 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 8);. if( pPa
1317f 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 5b 30 ger->zFilename[0
13180 5d 3d 3d 30 20 29 20 70 50 61 67 65 72 2d 3e 7a ]==0 ) pPager->z
13181 4a 6f 75 72 6e 61 6c 5b 30 5d 20 3d 20 30 3b 0a Journal[0] = 0;.
13182 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
13183 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 7d (zPathname);. }
13184 0a 20 20 70 50 61 67 65 72 2d 3e 70 56 66 73 20 . pPager->pVfs
13185 3d 20 70 56 66 73 3b 0a 20 20 70 50 61 67 65 72 = pVfs;. pPager
13186 2d 3e 76 66 73 46 6c 61 67 73 20 3d 20 76 66 73 ->vfsFlags = vfs
13187 46 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 4f 70 65 Flags;.. /* Ope
13188 6e 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 n the pager file
13189 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 .. */. if( zFi
1318a 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e lename && zFilen
1318b 61 6d 65 5b 30 5d 20 26 26 20 21 6d 65 6d 44 62 ame[0] && !memDb
1318c 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 6f 75 74 ){. int fout
1318d 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1318e 20 20 20 20 20 20 20 20 20 2f 2a 20 56 46 53 20 /* VFS
1318f 66 6c 61 67 73 20 72 65 74 75 72 6e 65 64 20 62 flags returned b
13190 79 20 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 20 y xOpen() */.
13191 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f rc = sqlite3OsO
13192 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 pen(pVfs, pPager
13193 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 50 61 ->zFilename, pPa
13194 67 65 72 2d 3e 66 64 2c 20 76 66 73 46 6c 61 67 ger->fd, vfsFlag
13195 73 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20 20 72 s, &fout);. r
13196 65 61 64 4f 6e 6c 79 20 3d 20 28 66 6f 75 74 26 eadOnly = (fout&
13197 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
13198 4f 4e 4c 59 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 ONLY);.. /* I
13199 66 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 73 f the file was s
1319a 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e uccessfully open
1319b 65 64 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 ed for read/writ
1319c 65 20 61 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a e access,. **
1319d 20 63 68 6f 6f 73 65 20 61 20 64 65 66 61 75 6c choose a defaul
1319e 74 20 70 61 67 65 20 73 69 7a 65 20 69 6e 20 63 t page size in c
1319f 61 73 65 20 77 65 20 68 61 76 65 20 74 6f 20 63 ase we have to c
131a0 72 65 61 74 65 20 74 68 65 0a 20 20 20 20 2a 2a reate the. **
131a1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
131a2 54 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 The default page
131a3 20 73 69 7a 65 20 69 73 20 74 68 65 20 6d 61 78 size is the max
131a4 69 6d 75 6d 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a imum of:. **.
131a5 20 20 20 20 2a 2a 20 20 20 20 2b 20 53 51 4c 49 ** + SQLI
131a6 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f TE_DEFAULT_PAGE_
131a7 53 49 5a 45 2c 0a 20 20 20 20 2a 2a 20 20 20 20 SIZE,. **
131a8 2b 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 + The value retu
131a9 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 4f rned by sqlite3O
131aa 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20 20 sSectorSize().
131ab 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 6c 61 ** + The la
131ac 72 67 65 73 74 20 70 61 67 65 20 73 69 7a 65 20 rgest page size
131ad 74 68 61 74 20 63 61 6e 20 62 65 20 77 72 69 74 that can be writ
131ae 74 65 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2e 0a ten atomically..
131af 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 */. if( r
131b0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
131b1 21 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 !readOnly ){.
131b2 20 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 setSectorSize
131b3 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 (pPager);.
131b4 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 44 45 assert(SQLITE_DE
131b5 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c FAULT_PAGE_SIZE<
131b6 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 =SQLITE_MAX_DEFA
131b7 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 29 3b 0a ULT_PAGE_SIZE);.
131b8 20 20 20 20 20 20 69 66 28 20 73 7a 50 61 67 65 if( szPage
131b9 44 66 6c 74 3c 70 50 61 67 65 72 2d 3e 73 65 63 Dflt<pPager->sec
131ba 74 6f 72 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 torSize ){.
131bb 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 if( pPager->s
131bc 65 63 74 6f 72 53 69 7a 65 3e 53 51 4c 49 54 45 ectorSize>SQLITE
131bd 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 _MAX_DEFAULT_PAG
131be 45 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 20 E_SIZE ){.
131bf 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 3d szPageDflt =
131c0 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 SQLITE_MAX_DEFA
131c1 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 0a 20 ULT_PAGE_SIZE;.
131c2 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
131c3 20 20 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 szPageDf
131c4 6c 74 20 3d 20 28 75 31 36 29 70 50 61 67 65 72 lt = (u16)pPager
131c5 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 ->sectorSize;.
131c6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
131c7 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
131c8 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 ABLE_ATOMIC_WRIT
131c9 45 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 E. {.
131ca 20 20 69 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 int iDc = sqli
131cb 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 te3OsDeviceChara
131cc 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 65 cteristics(pPage
131cd 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20 20 20 r->fd);.
131ce 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20 int ii;.
131cf 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f assert(SQLITE_IO
131d0 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28 CAP_ATOMIC512==(
131d1 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 20 20 512>>8));.
131d2 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f assert(SQLITE_
131d3 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 3d IOCAP_ATOMIC64K=
131d4 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a 20 20 =(65536>>8));.
131d5 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c assert(SQL
131d6 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f ITE_MAX_DEFAULT_
131d7 50 41 47 45 5f 53 49 5a 45 3c 3d 36 35 35 33 36 PAGE_SIZE<=65536
131d8 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 );. for(i
131d9 69 3d 73 7a 50 61 67 65 44 66 6c 74 3b 20 69 69 i=szPageDflt; ii
131da 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 <=SQLITE_MAX_DEF
131db 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 20 AULT_PAGE_SIZE;
131dc 69 69 3d 69 69 2a 32 29 7b 0a 20 20 20 20 20 20 ii=ii*2){.
131dd 20 20 20 20 69 66 28 20 69 44 63 26 28 53 51 4c if( iDc&(SQL
131de 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
131df 7c 28 69 69 3e 3e 38 29 29 20 29 7b 0a 20 20 20 |(ii>>8)) ){.
131e0 20 20 20 20 20 20 20 20 20 73 7a 50 61 67 65 44 szPageD
131e1 66 6c 74 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 flt = ii;.
131e2 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
131e3 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
131e4 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
131e5 20 20 2f 2a 20 49 66 20 61 20 74 65 6d 70 6f 72 /* If a tempor
131e6 61 72 79 20 66 69 6c 65 20 69 73 20 72 65 71 75 ary file is requ
131e7 65 73 74 65 64 2c 20 69 74 20 69 73 20 6e 6f 74 ested, it is not
131e8 20 6f 70 65 6e 65 64 20 69 6d 6d 65 64 69 61 74 opened immediat
131e9 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 49 6e 20 74 ely.. ** In t
131ea 68 69 73 20 63 61 73 65 20 77 65 20 61 63 63 65 his case we acce
131eb 70 74 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 pt the default p
131ec 61 67 65 20 73 69 7a 65 20 61 6e 64 20 64 65 6c age size and del
131ed 61 79 20 61 63 74 75 61 6c 6c 79 0a 20 20 20 20 ay actually.
131ee 2a 2a 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 ** opening the f
131ef 69 6c 65 20 75 6e 74 69 6c 20 74 68 65 20 66 69 ile until the fi
131f0 72 73 74 20 63 61 6c 6c 20 74 6f 20 4f 73 57 72 rst call to OsWr
131f1 69 74 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 ite().. **.
131f2 20 20 2a 2a 20 54 68 69 73 20 62 72 61 6e 63 68 ** This branch
131f3 20 69 73 20 61 6c 73 6f 20 72 75 6e 20 66 6f 72 is also run for
131f4 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
131f5 74 61 62 61 73 65 2e 20 41 6e 20 69 6e 2d 6d 65 tabase. An in-me
131f6 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 64 61 74 61 mory. ** data
131f7 62 61 73 65 20 69 73 20 74 68 65 20 73 61 6d 65 base is the same
131f8 20 61 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 20 as a temp-file
131f9 74 68 61 74 20 69 73 20 6e 65 76 65 72 20 77 72 that is never wr
131fa 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a 20 20 20 itten out to.
131fb 20 2a 2a 20 64 69 73 6b 20 61 6e 64 20 75 73 65 ** disk and use
131fc 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 s an in-memory r
131fd 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e ollback journal.
131fe 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 74 65 6d . */ . tem
131ff 70 46 69 6c 65 20 3d 20 31 3b 0a 20 20 20 20 70 pFile = 1;. p
13200 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 Pager->state = P
13201 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a AGER_EXCLUSIVE;.
13202 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28 readOnly = (
13203 76 66 73 46 6c 61 67 73 26 53 51 4c 49 54 45 5f vfsFlags&SQLITE_
13204 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a OPEN_READONLY);.
13205 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f }.. /* The fo
13206 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74 6f 20 llowing call to
13207 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
13208 28 29 20 73 65 72 76 65 73 20 74 6f 20 73 65 74 () serves to set
13209 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 0a 20 the value of .
1320a 20 2a 2a 20 50 61 67 65 72 2e 70 61 67 65 53 69 ** Pager.pageSi
1320b 7a 65 20 61 6e 64 20 74 6f 20 61 6c 6c 6f 63 61 ze and to alloca
1320c 74 65 20 74 68 65 20 50 61 67 65 72 2e 70 54 6d te the Pager.pTm
1320d 70 53 70 61 63 65 20 62 75 66 66 65 72 2e 0a 20 pSpace buffer..
1320e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 */. if( rc==SQ
1320f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 LITE_OK ){. a
13210 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6d ssert( pPager->m
13211 65 6d 44 62 3d 3d 30 20 29 3b 0a 20 20 20 20 72 emDb==0 );. r
13212 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
13213 53 65 74 50 61 67 65 73 69 7a 65 28 70 50 61 67 SetPagesize(pPag
13214 65 72 2c 20 26 73 7a 50 61 67 65 44 66 6c 74 2c er, &szPageDflt,
13215 20 2d 31 29 3b 0a 20 20 20 20 74 65 73 74 63 61 -1);. testca
13216 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f se( rc!=SQLITE_O
13217 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 K );. }.. /* I
13218 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
13219 72 65 64 20 69 6e 20 65 69 74 68 65 72 20 6f 66 red in either of
1321a 20 74 68 65 20 62 6c 6f 63 6b 73 20 61 62 6f 76 the blocks abov
1321b 65 2c 20 66 72 65 65 20 74 68 65 20 0a 20 20 2a e, free the . *
1321c 2a 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 * Pager structur
1321d 65 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 65 20 e and close the
1321e 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 file.. */. if(
1321f 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
13220 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 {. assert( !p
13221 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 Pager->pTmpSpace
13222 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f );. sqlite3O
13223 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 66 sClose(pPager->f
13224 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f d);. sqlite3_
13225 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 20 free(pPager);.
13226 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
13227 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a .. /* Initializ
13228 65 20 74 68 65 20 50 43 61 63 68 65 20 6f 62 6a e the PCache obj
13229 65 63 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 ect. */. assert
1322a 28 20 6e 45 78 74 72 61 3c 31 30 30 30 20 29 3b ( nExtra<1000 );
1322b 0a 20 20 6e 45 78 74 72 61 20 3d 20 52 4f 55 4e . nExtra = ROUN
1322c 44 38 28 6e 45 78 74 72 61 29 3b 0a 20 20 73 71 D8(nExtra);. sq
1322d 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28 lite3PcacheOpen(
1322e 73 7a 50 61 67 65 44 66 6c 74 2c 20 6e 45 78 74 szPageDflt, nExt
1322f 72 61 2c 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20 ra, !memDb,.
13230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13231 21 6d 65 6d 44 62 3f 70 61 67 65 72 53 74 72 65 !memDb?pagerStre
13232 73 73 3a 30 2c 20 28 76 6f 69 64 20 2a 29 70 50 ss:0, (void *)pP
13233 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 50 ager, pPager->pP
13234 43 61 63 68 65 29 3b 0a 0a 20 20 50 41 47 45 52 Cache);.. PAGER
13235 54 52 41 43 45 28 28 22 4f 50 45 4e 20 25 64 20 TRACE(("OPEN %d
13236 25 73 5c 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c %s\n", FILEHANDL
13237 45 49 44 28 70 50 61 67 65 72 2d 3e 66 64 29 2c EID(pPager->fd),
13238 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 pPager->zFilena
13239 6d 65 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 me));. IOTRACE(
1323a 28 22 4f 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c ("OPEN %p %s\n",
1323b 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d pPager, pPager-
1323c 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20 >zFilename))..
1323d 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e pPager->useJourn
1323e 61 6c 20 3d 20 28 75 38 29 75 73 65 4a 6f 75 72 al = (u8)useJour
1323f 6e 61 6c 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e nal;. pPager->n
13240 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 6e 6f 52 oReadlock = (noR
13241 65 61 64 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f eadlock && readO
13242 6e 6c 79 29 20 3f 31 3a 30 3b 0a 20 20 2f 2a 20 nly) ?1:0;. /*
13243 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e pPager->stmtOpen
13244 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 = 0; */. /* pP
13245 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 ager->stmtInUse
13246 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 = 0; */. /* pPa
13247 67 65 72 2d 3e 6e 52 65 66 20 3d 20 30 3b 20 2a ger->nRef = 0; *
13248 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 /. pPager->dbSi
13249 7a 65 56 61 6c 69 64 20 3d 20 28 75 38 29 6d 65 zeValid = (u8)me
1324a 6d 44 62 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 mDb;. /* pPager
1324b 2d 3e 73 74 6d 74 53 69 7a 65 20 3d 20 30 3b 20 ->stmtSize = 0;
1324c 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e */. /* pPager->
1324d 73 74 6d 74 4a 53 69 7a 65 20 3d 20 30 3b 20 2a stmtJSize = 0; *
1324e 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e /. /* pPager->n
1324f 50 61 67 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 Page = 0; */. p
13250 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 Pager->mxPgno =
13251 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
13252 43 4f 55 4e 54 3b 0a 20 20 2f 2a 20 70 50 61 67 COUNT;. /* pPag
13253 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 er->state = PAGE
13254 52 5f 55 4e 4c 4f 43 4b 3b 20 2a 2f 0a 20 20 61 R_UNLOCK; */. a
13255 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
13256 74 61 74 65 20 3d 3d 20 28 74 65 6d 70 46 69 6c tate == (tempFil
13257 65 20 3f 20 50 41 47 45 52 5f 45 58 43 4c 55 53 e ? PAGER_EXCLUS
13258 49 56 45 20 3a 20 50 41 47 45 52 5f 55 4e 4c 4f IVE : PAGER_UNLO
13259 43 4b 29 20 29 3b 0a 20 20 2f 2a 20 70 50 61 67 CK) );. /* pPag
1325a 65 72 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 30 3b er->errMask = 0;
1325b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 74 65 */. pPager->te
1325c 6d 70 46 69 6c 65 20 3d 20 28 75 38 29 74 65 6d mpFile = (u8)tem
1325d 70 46 69 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 pFile;. assert(
1325e 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 tempFile==PAGER
1325f 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 _LOCKINGMODE_NOR
13260 4d 41 4c 20 0a 20 20 20 20 20 20 20 20 20 20 7c MAL . |
13261 7c 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 | tempFile==PAGE
13262 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 R_LOCKINGMODE_EX
13263 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 61 73 73 CLUSIVE );. ass
13264 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 ert( PAGER_LOCKI
13265 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 NGMODE_EXCLUSIVE
13266 3d 3d 31 20 29 3b 0a 20 20 70 50 61 67 65 72 2d ==1 );. pPager-
13267 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d >exclusiveMode =
13268 20 28 75 38 29 74 65 6d 70 46 69 6c 65 3b 20 0a (u8)tempFile; .
13269 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 pPager->change
1326a 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 CountDone = pPag
1326b 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 er->tempFile;.
1326c 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20 pPager->memDb =
1326d 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20 70 50 61 (u8)memDb;. pPa
1326e 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 ger->readOnly =
1326f 28 75 38 29 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 (u8)readOnly;.
13270 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 /* pPager->needS
13271 79 6e 63 20 3d 20 30 3b 20 2a 2f 0a 20 20 61 73 ync = 0; */. as
13272 73 65 72 74 28 20 75 73 65 4a 6f 75 72 6e 61 6c sert( useJournal
13273 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 || pPager->temp
13274 46 69 6c 65 20 29 3b 0a 20 20 70 50 61 67 65 72 File );. pPager
13275 2d 3e 6e 6f 53 79 6e 63 20 3d 20 70 50 61 67 65 ->noSync = pPage
13276 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 r->tempFile;. p
13277 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 Pager->fullSync
13278 3d 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 = pPager->noSync
13279 20 3f 30 3a 31 3b 0a 20 20 70 50 61 67 65 72 2d ?0:1;. pPager-
1327a 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20 53 51 >sync_flags = SQ
1327b 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
1327c 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 ;. /* pPager->p
1327d 46 69 72 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 First = 0; */.
1327e 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 69 72 73 /* pPager->pFirs
1327f 74 53 79 6e 63 65 64 20 3d 20 30 3b 20 2a 2f 0a tSynced = 0; */.
13280 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 4c 61 /* pPager->pLa
13281 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 st = 0; */. pPa
13282 67 65 72 2d 3e 6e 45 78 74 72 61 20 3d 20 28 75 ger->nExtra = (u
13283 31 36 29 6e 45 78 74 72 61 3b 0a 20 20 70 50 61 16)nExtra;. pPa
13284 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 ger->journalSize
13285 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 44 Limit = SQLITE_D
13286 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 EFAULT_JOURNAL_S
13287 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20 20 61 73 73 IZE_LIMIT;. ass
13288 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 ert( isOpen(pPag
13289 65 72 2d 3e 66 64 29 20 7c 7c 20 74 65 6d 70 46 er->fd) || tempF
1328a 69 6c 65 20 29 3b 0a 20 20 73 65 74 53 65 63 74 ile );. setSect
1328b 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a orSize(pPager);.
1328c 20 20 69 66 28 20 21 75 73 65 4a 6f 75 72 6e 61 if( !useJourna
1328d 6c 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d l ){. pPager-
1328e 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 >journalMode = P
1328f 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
13290 5f 4f 46 46 3b 0a 20 20 7d 65 6c 73 65 20 69 66 _OFF;. }else if
13291 28 20 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 70 ( memDb ){. p
13292 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
13293 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e de = PAGER_JOURN
13294 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 3b 0a 20 ALMODE_MEMORY;.
13295 20 7d 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e }. /* pPager->
13296 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 30 xBusyHandler = 0
13297 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 ; */. /* pPager
13298 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 ->pBusyHandlerAr
13299 67 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 g = 0; */. pPag
1329a 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 3d 20 er->xReiniter =
1329b 78 52 65 69 6e 69 74 3b 0a 20 20 2f 2a 20 6d 65 xReinit;. /* me
1329c 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 61 48 61 mset(pPager->aHa
1329d 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 50 sh, 0, sizeof(pP
1329e 61 67 65 72 2d 3e 61 48 61 73 68 29 29 3b 20 2a ager->aHash)); *
1329f 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20 70 /. *ppPager = p
132a0 50 61 67 65 72 3b 0a 20 20 72 65 74 75 72 6e 20 Pager;. return
132a1 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a SQLITE_OK;.}....
132a2 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
132a3 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 ion is called af
132a4 74 65 72 20 74 72 61 6e 73 69 74 69 6f 6e 69 6e ter transitionin
132a5 67 20 66 72 6f 6d 20 50 41 47 45 52 5f 55 4e 4c g from PAGER_UNL
132a6 4f 43 4b 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f OCK to.** PAGER_
132a7 53 48 41 52 45 44 20 73 74 61 74 65 2e 20 49 74 SHARED state. It
132a8 20 74 65 73 74 73 20 69 66 20 74 68 65 72 65 20 tests if there
132a9 69 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c is a hot journal
132aa 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 74 present in.** t
132ab 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 he file-system f
132ac 6f 72 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 or the given pag
132ad 65 72 2e 20 41 20 68 6f 74 20 6a 6f 75 72 6e 61 er. A hot journa
132ae 6c 20 69 73 20 6f 6e 65 20 74 68 61 74 20 0a 2a l is one that .*
132af 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70 6c * needs to be pl
132b0 61 79 65 64 20 62 61 63 6b 2e 20 41 63 63 6f 72 ayed back. Accor
132b1 64 69 6e 67 20 74 6f 20 74 68 69 73 20 66 75 6e ding to this fun
132b2 63 74 69 6f 6e 2c 20 61 20 68 6f 74 2d 6a 6f 75 ction, a hot-jou
132b3 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 65 78 69 rnal.** file exi
132b4 73 74 73 20 69 66 20 74 68 65 20 66 6f 6c 6c 6f sts if the follo
132b5 77 69 6e 67 20 63 72 69 74 65 72 69 61 20 61 72 wing criteria ar
132b6 65 20 6d 65 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a e met:.**.** *
132b7 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c The journal fil
132b8 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 e exists in the
132b9 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 61 6e 64 file system, and
132ba 0a 2a 2a 20 20 20 2a 20 4e 6f 20 70 72 6f 63 65 .** * No proce
132bb 73 73 20 68 6f 6c 64 73 20 61 20 52 45 53 45 52 ss holds a RESER
132bc 56 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c VED or greater l
132bd 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
132be 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 0a 2a 2a ase file, and.**
132bf 20 20 20 2a 20 54 68 65 20 64 61 74 61 62 61 73 * The databas
132c0 65 20 66 69 6c 65 20 69 74 73 65 6c 66 20 69 73 e file itself is
132c1 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 30 20 greater than 0
132c2 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 61 bytes in size, a
132c3 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 66 69 nd.** * The fi
132c4 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 rst byte of the
132c5 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 journal file exi
132c6 73 74 73 20 61 6e 64 20 69 73 20 6e 6f 74 20 30 sts and is not 0
132c7 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 x00..**.** If th
132c8 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f e current size o
132c9 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
132ca 69 6c 65 20 69 73 20 30 20 62 75 74 20 61 20 6a ile is 0 but a j
132cb 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 65 ournal file.** e
132cc 78 69 73 74 73 2c 20 74 68 61 74 20 69 73 20 70 xists, that is p
132cd 72 6f 62 61 62 6c 79 20 61 6e 20 6f 6c 64 20 6a robably an old j
132ce 6f 75 72 6e 61 6c 20 6c 65 66 74 20 6f 76 65 72 ournal left over
132cf 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a 2a 2a from a prior.**
132d0 20 64 61 74 61 62 61 73 65 20 77 69 74 68 20 74 database with t
132d1 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20 49 6e he same name. In
132d2 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6a this case the j
132d3 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 0a 2a ournal file is.*
132d4 2a 20 6a 75 73 74 20 64 65 6c 65 74 65 64 20 75 * just deleted u
132d5 73 69 6e 67 20 4f 73 44 65 6c 65 74 65 2c 20 2a sing OsDelete, *
132d6 70 45 78 69 73 74 73 20 69 73 20 73 65 74 20 74 pExists is set t
132d7 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f o 0 and SQLITE_O
132d8 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 K.** is returned
132d9 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
132da 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 63 68 tine does not ch
132db 65 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 20 eck if there is
132dc 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
132dd 20 66 69 6c 65 6e 61 6d 65 0a 2a 2a 20 61 74 20 filename.** at
132de 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 the end of the f
132df 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 ile. If there is
132e0 2c 20 61 6e 64 20 74 68 61 74 20 6d 61 73 74 65 , and that maste
132e1 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a r journal file.*
132e2 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 * does not exist
132e3 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e , then the journ
132e4 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 72 al file is not r
132e5 65 61 6c 6c 79 20 68 6f 74 2e 20 49 6e 20 74 68 eally hot. In th
132e6 69 73 0a 2a 2a 20 63 61 73 65 20 74 68 69 73 20 is.** case this
132e7 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 routine will ret
132e8 75 72 6e 20 61 20 66 61 6c 73 65 2d 70 6f 73 69 urn a false-posi
132e9 74 69 76 65 2e 20 54 68 65 20 70 61 67 65 72 5f tive. The pager_
132ea 70 6c 61 79 62 61 63 6b 28 29 0a 2a 2a 20 72 6f playback().** ro
132eb 75 74 69 6e 65 20 77 69 6c 6c 20 64 69 73 63 6f utine will disco
132ec 76 65 72 20 74 68 61 74 20 74 68 65 20 6a 6f 75 ver that the jou
132ed 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 rnal file is not
132ee 20 72 65 61 6c 6c 79 20 68 6f 74 20 61 6e 64 20 really hot and
132ef 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c .** will not rol
132f0 6c 20 69 74 20 62 61 63 6b 2e 20 0a 2a 2a 0a 2a l it back. .**.*
132f1 2a 20 49 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e * If a hot-journ
132f2 61 6c 20 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 al file is found
132f3 20 74 6f 20 65 78 69 73 74 2c 20 2a 70 45 78 69 to exist, *pExi
132f4 73 74 73 20 69 73 20 73 65 74 20 74 6f 20 31 20 sts is set to 1
132f5 61 6e 64 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f and .** SQLITE_O
132f6 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 6e K returned. If n
132f7 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 o hot-journal fi
132f8 6c 65 20 69 73 20 70 72 65 73 65 6e 74 2c 20 2a le is present, *
132f9 70 45 78 69 73 74 73 20 69 73 0a 2a 2a 20 73 65 pExists is.** se
132fa 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 t to 0 and SQLIT
132fb 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 E_OK returned. I
132fc 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 f an IO error oc
132fd 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e curs while tryin
132fe 67 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69 6e g.** to determin
132ff 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 e whether or not
13300 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 a hot-journal f
13301 69 6c 65 20 65 78 69 73 74 73 2c 20 74 68 65 20 ile exists, the
13302 49 4f 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 IO error.** code
13303 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
13304 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 2a 70 the value of *p
13305 45 78 69 73 74 73 20 69 73 20 75 6e 64 65 66 69 Exists is undefi
13306 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ned..*/.static i
13307 6e 74 20 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c nt hasHotJournal
13308 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
13309 69 6e 74 20 2a 70 45 78 69 73 74 73 29 7b 0a 20 int *pExists){.
1330a 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 sqlite3_vfs * c
1330b 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 67 onst pVfs = pPag
1330c 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e 74 20 er->pVfs;. int
1330d 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
1330e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
1330f 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e urn code */. in
13310 74 20 65 78 69 73 74 73 3b 20 20 20 20 20 20 20 t exists;
13311 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
13312 72 75 65 20 69 66 20 61 20 6a 6f 75 72 6e 61 6c rue if a journal
13313 20 66 69 6c 65 20 69 73 20 70 72 65 73 65 6e 74 file is present
13314 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
13315 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 Pager!=0 );. as
13316 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73 sert( pPager->us
13317 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 eJournal );. as
13318 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 sert( isOpen(pPa
13319 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 61 73 ger->fd) );. as
1331a 73 65 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50 sert( !isOpen(pP
1331b 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 ager->jfd) );.
1331c 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
1331d 73 74 61 74 65 20 3c 3d 20 50 41 47 45 52 5f 53 state <= PAGER_S
1331e 48 41 52 45 44 20 29 3b 0a 0a 20 20 2a 70 45 78 HARED );.. *pEx
1331f 69 73 74 73 20 3d 20 30 3b 0a 20 20 72 63 20 3d ists = 0;. rc =
13320 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 sqlite3OsAccess
13321 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a (pVfs, pPager->z
13322 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f Journal, SQLITE_
13323 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 ACCESS_EXISTS, &
13324 65 78 69 73 74 73 29 3b 0a 20 20 69 66 28 20 72 exists);. if( r
13325 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
13326 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 69 6e exists ){. in
13327 74 20 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 t locked;
13328 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
13329 65 20 69 66 20 73 6f 6d 65 20 70 72 6f 63 65 73 e if some proces
1332a 73 20 68 6f 6c 64 73 20 61 20 52 45 53 45 52 56 s holds a RESERV
1332b 45 44 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20 20 ED lock */..
1332c 2f 2a 20 52 61 63 65 20 63 6f 6e 64 69 74 69 6f /* Race conditio
1332d 6e 20 68 65 72 65 3a 20 20 41 6e 6f 74 68 65 72 n here: Another
1332e 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 20 68 process might h
1332f 61 76 65 20 62 65 65 6e 20 68 6f 6c 64 69 6e 67 ave been holding
13330 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 the. ** the
13331 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 61 6e RESERVED lock an
13332 64 20 68 61 76 65 20 61 20 6a 6f 75 72 6e 61 6c d have a journal
13333 20 6f 70 65 6e 20 61 74 20 74 68 65 20 73 71 6c open at the sql
13334 69 74 65 33 4f 73 41 63 63 65 73 73 28 29 20 0a ite3OsAccess() .
13335 20 20 20 20 2a 2a 20 63 61 6c 6c 20 61 62 6f 76 ** call abov
13336 65 2c 20 62 75 74 20 74 68 65 6e 20 64 65 6c 65 e, but then dele
13337 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 te the journal a
13338 6e 64 20 64 72 6f 70 20 74 68 65 20 6c 6f 63 6b nd drop the lock
13339 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 77 before. ** w
1333a 65 20 67 65 74 20 74 6f 20 74 68 65 20 66 6f 6c e get to the fol
1333b 6c 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 lowing sqlite3Os
1333c 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
1333d 6b 28 29 20 63 61 6c 6c 2e 20 20 49 66 20 74 68 k() call. If th
1333e 61 74 0a 20 20 20 20 2a 2a 20 69 73 20 74 68 65 at. ** is the
1333f 20 63 61 73 65 2c 20 74 68 69 73 20 72 6f 75 74 case, this rout
13340 69 6e 65 20 6d 69 67 68 74 20 74 68 69 6e 6b 20 ine might think
13341 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a there is a hot j
13342 6f 75 72 6e 61 6c 20 77 68 65 6e 0a 20 20 20 20 ournal when.
13343 2a 2a 20 69 6e 20 66 61 63 74 20 74 68 65 72 65 ** in fact there
13344 20 69 73 20 6e 6f 6e 65 2e 20 20 54 68 69 73 20 is none. This
13345 72 65 73 75 6c 74 73 20 69 6e 20 61 20 66 61 6c results in a fal
13346 73 65 2d 70 6f 73 69 74 69 76 65 20 77 68 69 63 se-positive whic
13347 68 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65 h will. ** be
13348 20 64 65 61 6c 74 20 77 69 74 68 20 62 79 20 74 dealt with by t
13349 68 65 20 70 6c 61 79 62 61 63 6b 20 72 6f 75 74 he playback rout
1334a 69 6e 65 2e 20 20 54 69 63 6b 65 74 20 23 33 38 ine. Ticket #38
1334b 38 33 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 83.. */. r
1334c 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 68 65 c = sqlite3OsChe
1334d 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 70 ckReservedLock(p
1334e 50 61 67 65 72 2d 3e 66 64 2c 20 26 6c 6f 63 6b Pager->fd, &lock
1334f 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d ed);. if( rc=
13350 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c =SQLITE_OK && !l
13351 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 69 ocked ){. i
13352 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 20 20 20 nt nPage;..
13353 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 73 69 /* Check the si
13354 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
13355 73 65 20 66 69 6c 65 2e 20 49 66 20 69 74 20 63 se file. If it c
13356 6f 6e 73 69 73 74 73 20 6f 66 20 30 20 70 61 67 onsists of 0 pag
13357 65 73 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 es,. ** the
13358 6e 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 n delete the jou
13359 72 6e 61 6c 20 66 69 6c 65 2e 20 53 65 65 20 74 rnal file. See t
1335a 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e he header commen
1335b 74 20 61 62 6f 76 65 20 66 6f 72 20 0a 20 20 20 t above for .
1335c 20 20 20 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e ** the reason
1335d 69 6e 67 20 68 65 72 65 2e 20 20 44 65 6c 65 74 ing here. Delet
1335e 65 20 74 68 65 20 6f 62 73 6f 6c 65 74 65 20 6a e the obsolete j
1335f 6f 75 72 6e 61 6c 20 66 69 6c 65 20 75 6e 64 65 ournal file unde
13360 72 0a 20 20 20 20 20 20 2a 2a 20 61 20 52 45 53 r. ** a RES
13361 45 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20 61 76 ERVED lock to av
13362 6f 69 64 20 72 61 63 65 20 63 6f 6e 64 69 74 69 oid race conditi
13363 6f 6e 73 20 61 6e 64 20 74 6f 20 61 76 6f 69 64 ons and to avoid
13364 20 76 69 6f 6c 61 74 69 6e 67 0a 20 20 20 20 20 violating.
13365 20 2a 2a 20 5b 48 33 33 30 32 30 5d 2e 0a 20 20 ** [H33020]..
13366 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 */. rc
13367 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 = sqlite3PagerPa
13368 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 gecount(pPager,
13369 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 &nPage);. i
1336a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1336b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
1336c 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 nPage==0 ){.
1336d 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 sqlite3Beg
1336e 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 inBenignMalloc()
1336f 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
13370 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 sqlite3OsLock(pP
13371 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 52 56 ager->fd, RESERV
13372 45 44 5f 4c 4f 43 4b 29 3d 3d 53 51 4c 49 54 45 ED_LOCK)==SQLITE
13373 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
13374 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 sqlite3OsDele
13375 74 65 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d te(pVfs, pPager-
13376 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 >zJournal, 0);.
13377 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
13378 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 e3OsUnlock(pPage
13379 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f r->fd, SHARED_LO
1337a 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d CK);. }
1337b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1337c 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f e3EndBenignMallo
1337d 63 28 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c c();. }el
1337e 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a se{. /*
1337f 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c The journal fil
13380 65 20 65 78 69 73 74 73 20 61 6e 64 20 6e 6f 20 e exists and no
13381 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e other connection
13382 20 68 61 73 20 61 20 72 65 73 65 72 76 65 64 0a has a reserved.
13383 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 ** or
13384 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 greater lock on
13385 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
13386 65 2e 20 4e 6f 77 20 63 68 65 63 6b 20 74 68 61 e. Now check tha
13387 74 20 74 68 65 72 65 20 69 73 0a 20 20 20 20 20 t there is.
13388 20 20 20 20 20 2a 2a 20 61 74 20 6c 65 61 73 74 ** at least
13389 20 6f 6e 65 20 6e 6f 6e 2d 7a 65 72 6f 20 62 79 one non-zero by
1338a 74 65 73 20 61 74 20 74 68 65 20 73 74 61 72 74 tes at the start
1338b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
1338c 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 20 20 file..
1338d 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 2c 20 ** If there is,
1338e 74 68 65 6e 20 77 65 20 63 6f 6e 73 69 64 65 72 then we consider
1338f 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 74 6f this journal to
13390 20 62 65 20 68 6f 74 2e 20 49 66 20 6e 6f 74 2c be hot. If not,
13391 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 . ** i
13392 74 20 63 61 6e 20 62 65 20 69 67 6e 6f 72 65 64 t can be ignored
13393 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 .. */.
13394 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d int f =
13395 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
13396 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 DONLY|SQLITE_OPE
13397 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a N_MAIN_JOURNAL;.
13398 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
13399 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 qlite3OsOpen(pVf
1339a 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 s, pPager->zJour
1339b 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 nal, pPager->jfd
1339c 2c 20 66 2c 20 26 66 29 3b 0a 20 20 20 20 20 20 , f, &f);.
1339d 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1339e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1339f 20 20 20 20 20 75 38 20 66 69 72 73 74 20 3d 20 u8 first =
133a0 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 0;. r
133a1 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 c = sqlite3OsRea
133a2 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 28 d(pPager->jfd, (
133a3 76 6f 69 64 20 2a 29 26 66 69 72 73 74 2c 20 31 void *)&first, 1
133a4 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 0);.
133a5 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
133a6 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
133a7 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 D ){.
133a8 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
133a9 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d K;. }
133aa 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
133ab 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 ite3OsClose(pPag
133ac 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 er->jfd);.
133ad 20 20 20 20 20 20 2a 70 45 78 69 73 74 73 20 3d *pExists =
133ae 20 28 66 69 72 73 74 21 3d 30 29 3b 0a 20 20 20 (first!=0);.
133af 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 }else if(
133b0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 41 4e 54 rc==SQLITE_CANT
133b1 4f 50 45 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 OPEN ){.
133b2 20 20 20 20 2f 2a 20 49 66 20 77 65 20 63 61 6e /* If we can
133b3 6e 6f 74 20 6f 70 65 6e 20 74 68 65 20 72 6f 6c not open the rol
133b4 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 lback journal fi
133b5 6c 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 le in order to s
133b6 65 65 20 69 66 0a 20 20 20 20 20 20 20 20 20 20 ee if.
133b7 20 20 2a 2a 20 69 74 73 20 68 61 73 20 61 20 7a ** its has a z
133b8 65 72 6f 20 68 65 61 64 65 72 2c 20 74 68 61 74 ero header, that
133b9 20 6d 69 67 68 74 20 62 65 20 64 75 65 20 74 6f might be due to
133ba 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 2c 20 6f an I/O error, o
133bb 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a r. **
133bc 20 69 74 20 6d 69 67 68 74 20 62 65 20 64 75 65 it might be due
133bd 20 74 6f 20 74 68 65 20 72 61 63 65 20 63 6f 6e to the race con
133be 64 69 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64 dition described
133bf 20 61 62 6f 76 65 20 61 6e 64 20 69 6e 0a 20 20 above and in.
133c0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 69 63 ** tic
133c1 6b 65 74 20 23 33 38 38 33 2e 20 20 45 69 74 68 ket #3883. Eith
133c2 65 72 20 77 61 79 2c 20 61 73 73 75 6d 65 20 74 er way, assume t
133c3 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hat the journal
133c4 69 73 20 68 6f 74 2e 0a 20 20 20 20 20 20 20 20 is hot..
133c5 20 20 20 20 2a 2a 20 54 68 69 73 20 6d 69 67 68 ** This migh
133c6 74 20 62 65 20 61 20 66 61 6c 73 65 20 70 6f 73 t be a false pos
133c7 69 74 69 76 65 2e 20 20 42 75 74 20 69 66 20 69 itive. But if i
133c8 74 20 69 73 2c 20 74 68 65 6e 20 74 68 65 0a 20 t is, then the.
133c9 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 75 ** au
133ca 74 6f 6d 61 74 69 63 20 6a 6f 75 72 6e 61 6c 20 tomatic journal
133cb 70 6c 61 79 62 61 63 6b 20 61 6e 64 20 72 65 63 playback and rec
133cc 6f 76 65 72 79 20 6d 65 63 68 61 6e 69 73 6d 20 overy mechanism
133cd 77 69 6c 6c 20 64 65 61 6c 0a 20 20 20 20 20 20 will deal.
133ce 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 69 74 ** with it
133cf 20 75 6e 64 65 72 20 61 6e 20 45 58 43 4c 55 53 under an EXCLUS
133d0 49 56 45 20 6c 6f 63 6b 20 77 68 65 72 65 20 77 IVE lock where w
133d1 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f e do not need to
133d2 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
133d3 77 6f 72 72 79 20 73 6f 20 6d 75 63 68 20 77 69 worry so much wi
133d4 74 68 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f th race conditio
133d5 6e 73 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 ns..
133d6 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a */. *
133d7 70 45 78 69 73 74 73 20 3d 20 31 3b 0a 20 20 20 pExists = 1;.
133d8 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
133d9 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 LITE_OK;.
133da 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
133db 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
133dc 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d .. return rc;.}
133dd 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 ../*.** Read the
133de 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 70 61 67 content for pag
133df 65 20 70 50 67 20 6f 75 74 20 6f 66 20 74 68 65 e pPg out of the
133e0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
133e1 6e 64 20 69 6e 74 6f 20 0a 2a 2a 20 70 50 67 2d nd into .** pPg-
133e2 3e 70 44 61 74 61 2e 20 41 20 73 68 61 72 65 64 >pData. A shared
133e3 20 6c 6f 63 6b 20 6f 72 20 67 72 65 61 74 65 72 lock or greater
133e4 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 6f 6e must be held on
133e5 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a the database.**
133e6 20 66 69 6c 65 20 62 65 66 6f 72 65 20 74 68 69 file before thi
133e7 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
133e8 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 lled..**.** If p
133e9 61 67 65 20 31 20 69 73 20 72 65 61 64 2c 20 74 age 1 is read, t
133ea 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 hen the value of
133eb 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 Pager.dbFileVer
133ec 73 5b 5d 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a s[] is set to.**
133ed 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 the value read
133ee 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
133ef 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 e file..**.** If
133f0 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 an IO error occ
133f1 75 72 73 2c 20 74 68 65 6e 20 74 68 65 20 49 4f urs, then the IO
133f2 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e error is return
133f3 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 ed to the caller
133f4 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 ..** Otherwise,
133f5 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
133f6 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 urned..*/.static
133f7 20 69 6e 74 20 72 65 61 64 44 62 50 61 67 65 28 int readDbPage(
133f8 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 PgHdr *pPg){. P
133f9 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 ager *pPager = p
133fa 50 67 2d 3e 70 50 61 67 65 72 3b 20 2f 2a 20 50 Pg->pPager; /* P
133fb 61 67 65 72 20 6f 62 6a 65 63 74 20 61 73 73 6f ager object asso
133fc 63 69 61 74 65 64 20 77 69 74 68 20 70 61 67 65 ciated with page
133fd 20 70 50 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 pPg */. Pgno p
133fe 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b gno = pPg->pgno;
133ff 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e /* Page n
13400 75 6d 62 65 72 20 74 6f 20 72 65 61 64 20 2a 2f umber to read */
13401 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
13402 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13403 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
13404 2f 0a 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b /. i64 iOffset;
13405 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13406 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 /* Byte offset
13407 6f 66 20 66 69 6c 65 20 74 6f 20 72 65 61 64 20 of file to read
13408 66 72 6f 6d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 from */.. asser
13409 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 t( pPager->state
1340a 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 26 >=PAGER_SHARED &
1340b 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73 & !MEMDB );. as
1340c 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 sert( isOpen(pPa
1340d 67 65 72 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 69 ger->fd) );.. i
1340e 66 28 20 4e 45 56 45 52 28 21 69 73 4f 70 65 6e f( NEVER(!isOpen
1340f 28 70 50 61 67 65 72 2d 3e 66 64 29 29 20 29 7b (pPager->fd)) ){
13410 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
13411 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b ger->tempFile );
13412 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67 2d . memset(pPg-
13413 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67 65 >pData, 0, pPage
13414 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 r->pageSize);.
13415 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
13416 4f 4b 3b 0a 20 20 7d 0a 20 20 69 4f 66 66 73 65 OK;. }. iOffse
13417 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 t = (pgno-1)*(i6
13418 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 4)pPager->pageSi
13419 7a 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 ze;. rc = sqlit
1341a 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d e3OsRead(pPager-
1341b 3e 66 64 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c >fd, pPg->pData,
1341c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
1341d 65 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 69 e, iOffset);. i
1341e 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f f( rc==SQLITE_IO
1341f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 ERR_SHORT_READ )
13420 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
13421 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 E_OK;. }. if(
13422 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 75 pgno==1 ){. u
13423 38 20 2a 64 62 46 69 6c 65 56 65 72 73 20 3d 20 8 *dbFileVers =
13424 26 28 28 75 38 2a 29 70 50 67 2d 3e 70 44 61 74 &((u8*)pPg->pDat
13425 61 29 5b 32 34 5d 3b 0a 20 20 20 20 6d 65 6d 63 a)[24];. memc
13426 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 py(&pPager->dbFi
13427 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65 56 65 leVers, dbFileVe
13428 72 73 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 rs, sizeof(pPage
13429 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b r->dbFileVers));
1342a 0a 20 20 7d 0a 20 20 43 4f 44 45 43 31 28 70 50 . }. CODEC1(pP
1342b 61 67 65 72 2c 20 70 50 67 2d 3e 70 44 61 74 61 ager, pPg->pData
1342c 2c 20 70 67 6e 6f 2c 20 33 2c 20 72 63 20 3d 20 , pgno, 3, rc =
1342d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 0a SQLITE_NOMEM);..
1342e 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c PAGER_INCR(sql
1342f 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61 64 64 ite3_pager_readd
13430 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 50 41 47 45 b_count);. PAGE
13431 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e R_INCR(pPager->n
13432 52 65 61 64 29 3b 0a 20 20 49 4f 54 52 41 43 45 Read);. IOTRACE
13433 28 28 22 50 47 49 4e 20 25 70 20 25 64 5c 6e 22 (("PGIN %p %d\n"
13434 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 , pPager, pgno))
13435 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 ;. PAGERTRACE((
13436 22 46 45 54 43 48 20 25 64 20 70 61 67 65 20 25 "FETCH %d page %
13437 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c d hash(%08x)\n",
13438 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
13439 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c PAGERID(pPager),
1343a 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67 pgno, pager_pag
1343b 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a 0a 20 ehash(pPg)));..
1343c 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1343d 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
1343e 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 on is called to
1343f 6f 62 74 61 69 6e 20 61 20 73 68 61 72 65 64 20 obtain a shared
13440 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
13441 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 74 base file..** It
13442 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 63 is illegal to c
13443 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 all sqlite3Pager
13444 41 63 71 75 69 72 65 28 29 20 75 6e 74 69 6c 20 Acquire() until
13445 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 after this funct
13446 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 ion.** has been
13447 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 61 6c successfully cal
13448 6c 65 64 2e 20 49 66 20 61 20 73 68 61 72 65 64 led. If a shared
13449 2d 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 -lock is already
1344a 20 68 65 6c 64 20 77 68 65 6e 0a 2a 2a 20 74 68 held when.** th
1344b 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
1344c 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e alled, it is a n
1344d 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 o-op..**.** The
1344e 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 65 72 61 74 following operat
1344f 69 6f 6e 73 20 61 72 65 20 61 6c 73 6f 20 70 65 ions are also pe
13450 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20 rformed by this
13451 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
13452 20 20 31 29 20 49 66 20 74 68 65 20 70 61 67 65 1) If the page
13453 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 r is currently i
13454 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 n PAGER_UNLOCK s
13455 74 61 74 65 20 28 6e 6f 20 6c 6f 63 6b 20 68 65 tate (no lock he
13456 6c 64 0a 2a 2a 20 20 20 20 20 20 6f 6e 20 74 68 ld.** on th
13457 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 29 e database file)
13458 2c 20 74 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 , then an attemp
13459 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 62 74 t is made to obt
1345a 61 69 6e 20 61 0a 2a 2a 20 20 20 20 20 20 53 48 ain a.** SH
1345b 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 ARED lock on the
1345c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
1345d 49 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 Immediately afte
1345e 72 20 6f 62 74 61 69 6e 69 6e 67 0a 2a 2a 20 20 r obtaining.**
1345f 20 20 20 20 74 68 65 20 53 48 41 52 45 44 20 6c the SHARED l
13460 6f 63 6b 2c 20 74 68 65 20 66 69 6c 65 2d 73 79 ock, the file-sy
13461 73 74 65 6d 20 69 73 20 63 68 65 63 6b 65 64 20 stem is checked
13462 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 for a hot-journa
13463 6c 2c 0a 2a 2a 20 20 20 20 20 20 77 68 69 63 68 l,.** which
13464 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b 20 is played back
13465 69 66 20 70 72 65 73 65 6e 74 2e 20 46 6f 6c 6c if present. Foll
13466 6f 77 69 6e 67 20 61 6e 79 20 68 6f 74 2d 6a 6f owing any hot-jo
13467 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 20 72 urnal .** r
13468 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 63 6f 6e ollback, the con
13469 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 tents of the cac
1346a 68 65 20 61 72 65 20 76 61 6c 69 64 61 74 65 64 he are validated
1346b 20 62 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 by checking.**
1346c 20 20 20 20 20 74 68 65 20 27 63 68 61 6e 67 65 the 'change
1346d 2d 63 6f 75 6e 74 65 72 27 20 66 69 65 6c 64 20 -counter' field
1346e 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1346f 66 69 6c 65 20 68 65 61 64 65 72 20 61 6e 64 0a file header and.
13470 2a 2a 20 20 20 20 20 20 64 69 73 63 61 72 64 65 ** discarde
13471 64 20 69 66 20 74 68 65 79 20 61 72 65 20 66 6f d if they are fo
13472 75 6e 64 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 und to be invali
13473 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 66 d..**.** 2) If
13474 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75 the pager is ru
13475 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 nning in exclusi
13476 76 65 2d 6d 6f 64 65 2c 20 61 6e 64 20 74 68 65 ve-mode, and the
13477 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 re are currently
13478 0a 2a 2a 20 20 20 20 20 20 6e 6f 20 6f 75 74 73 .** no outs
13479 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 tanding referenc
1347a 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65 73 2c es to any pages,
1347b 20 61 6e 64 20 69 73 20 69 6e 20 74 68 65 20 65 and is in the e
1347c 72 72 6f 72 20 73 74 61 74 65 2c 0a 2a 2a 20 20 rror state,.**
1347d 20 20 20 20 74 68 65 6e 20 61 6e 20 61 74 74 65 then an atte
1347e 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 63 mpt is made to c
1347f 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 73 lear the error s
13480 74 61 74 65 20 62 79 20 64 69 73 63 61 72 64 69 tate by discardi
13481 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 63 ng.** the c
13482 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 ontents of the p
13483 61 67 65 20 63 61 63 68 65 20 61 6e 64 20 72 6f age cache and ro
13484 6c 6c 69 6e 67 20 62 61 63 6b 20 61 6e 79 20 6f lling back any o
13485 70 65 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 pen journal.**
13486 20 20 20 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 file..**.**
13487 49 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e If the operation
13488 20 64 65 73 63 72 69 62 65 64 20 62 79 20 28 32 described by (2
13489 29 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 20 61 ) above is not a
1348a 74 74 65 6d 70 74 65 64 2c 20 61 6e 64 20 69 66 ttempted, and if
1348b 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 69 73 the.** pager is
1348c 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 in an error sta
1348d 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 te other than SQ
1348e 4c 49 54 45 5f 46 55 4c 4c 20 77 68 65 6e 20 74 LITE_FULL when t
1348f 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a his is called,.*
13490 2a 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 * the error stat
13491 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 e error code is
13492 72 65 74 75 72 6e 65 64 2e 20 49 74 20 69 73 20 returned. It is
13493 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 61 permitted to rea
13494 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 d the.** databas
13495 65 20 77 68 65 6e 20 69 6e 20 53 51 4c 49 54 45 e when in SQLITE
13496 5f 46 55 4c 4c 20 65 72 72 6f 72 20 73 74 61 74 _FULL error stat
13497 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 e..**.** Otherwi
13498 73 65 2c 20 69 66 20 65 76 65 72 79 74 68 69 6e se, if everythin
13499 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c g is successful,
1349a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
1349b 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 0a 2a 2a turned. If an.**
1349c 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 IO error occurs
1349d 20 77 68 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 74 while locking t
1349e 68 65 20 64 61 74 61 62 61 73 65 2c 20 63 68 65 he database, che
1349f 63 6b 69 6e 67 20 66 6f 72 20 61 20 68 6f 74 2d cking for a hot-
134a0 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 journal.** file
134a1 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 or rolling back
134a2 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 a journal file,
134a3 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 the IO error cod
134a4 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a e is returned..*
134a5 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
134a6 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
134a7 72 53 68 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 rSharedLock(Page
134a8 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e r *pPager){. in
134a9 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
134aa 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
134ab 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
134ac 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 72 6f 72 */. int isError
134ad 52 65 73 65 74 20 3d 20 30 3b 20 20 20 20 20 20 Reset = 0;
134ae 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
134af 69 66 20 72 65 63 6f 76 65 72 69 6e 67 20 66 72 if recovering fr
134b0 6f 6d 20 65 72 72 6f 72 20 73 74 61 74 65 20 2a om error state *
134b1 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 /.. /* This rou
134b2 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c tine is only cal
134b3 6c 65 64 20 66 72 6f 6d 20 62 2d 74 72 65 65 20 led from b-tree
134b4 61 6e 64 20 6f 6e 6c 79 20 77 68 65 6e 20 74 68 and only when th
134b5 65 72 65 20 61 72 65 20 6e 6f 0a 20 20 2a 2a 20 ere are no. **
134b6 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 outstanding page
134b7 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 s */. assert( s
134b8 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 qlite3PcacheRefC
134b9 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 ount(pPager->pPC
134ba 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 69 66 ache)==0 );. if
134bb 28 20 4e 45 56 45 52 28 4d 45 4d 44 42 20 26 26 ( NEVER(MEMDB &&
134bc 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
134bd 29 20 29 7b 20 72 65 74 75 72 6e 20 70 50 61 67 ) ){ return pPag
134be 65 72 2d 3e 65 72 72 43 6f 64 65 3b 20 7d 0a 0a er->errCode; }..
134bf 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 /* If this dat
134c0 61 62 61 73 65 20 69 73 20 69 6e 20 61 6e 20 65 abase is in an e
134c1 72 72 6f 72 2d 73 74 61 74 65 2c 20 6e 6f 77 20 rror-state, now
134c2 69 73 20 61 20 63 68 61 6e 63 65 20 74 6f 20 63 is a chance to c
134c3 6c 65 61 72 0a 20 20 2a 2a 20 74 68 65 20 65 72 lear. ** the er
134c4 72 6f 72 2e 20 44 69 73 63 61 72 64 20 74 68 65 ror. Discard the
134c5 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
134c6 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e 64 pager-cache and
134c7 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 61 rollback. ** a
134c8 6e 79 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 69 ny hot journal i
134c9 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 n the file-syste
134ca 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 m.. */. if( pP
134cb 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b ager->errCode ){
134cc 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 . if( isOpen(
134cd 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 pPager->jfd) ||
134ce 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
134cf 20 29 7b 0a 20 20 20 20 20 20 69 73 45 72 72 6f ){. isErro
134d0 72 52 65 73 65 74 20 3d 20 31 3b 0a 20 20 20 20 rReset = 1;.
134d1 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 }. pPager->er
134d2 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f rCode = SQLITE_O
134d3 4b 3b 0a 20 20 20 20 70 61 67 65 72 5f 72 65 73 K;. pager_res
134d4 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a et(pPager);. }.
134d5 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 . if( pPager->s
134d6 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f tate==PAGER_UNLO
134d7 43 4b 20 7c 7c 20 69 73 45 72 72 6f 72 52 65 73 CK || isErrorRes
134d8 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 et ){. sqlite
134d9 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 3_vfs * const pV
134da 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 fs = pPager->pVf
134db 73 3b 0a 20 20 20 20 69 6e 74 20 69 73 48 6f 74 s;. int isHot
134dc 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 Journal = 0;.
134dd 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 assert( !MEMDB
134de 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
134df 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 qlite3PcacheRefC
134e0 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 ount(pPager->pPC
134e1 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 ache)==0 );.
134e2 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 52 65 if( pPager->noRe
134e3 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 adlock ){.
134e4 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
134e5 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 20 20 readOnly );.
134e6 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 pPager->state
134e7 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 3b 0a = PAGER_SHARED;.
134e8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
134e9 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 rc = pager_wait
134ea 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c _on_lock(pPager,
134eb 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 SHARED_LOCK);.
134ec 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
134ed 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
134ee 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
134ef 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 ->state==PAGER_U
134f0 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 20 NLOCK );.
134f1 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 return pager_er
134f2 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b ror(pPager, rc);
134f3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
134f4 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
134f5 72 2d 3e 73 74 61 74 65 3e 3d 53 48 41 52 45 44 r->state>=SHARED
134f6 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a _LOCK );.. /*
134f7 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 If a journal fi
134f8 6c 65 20 65 78 69 73 74 73 2c 20 61 6e 64 20 74 le exists, and t
134f9 68 65 72 65 20 69 73 20 6e 6f 20 52 45 53 45 52 here is no RESER
134fa 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a VED lock on the.
134fb 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 ** database
134fc 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 65 69 file, then it ei
134fd 74 68 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65 ther needs to be
134fe 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f 72 20 played back or
134ff 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a deleted.. */.
13500 20 20 20 20 69 66 28 20 21 69 73 45 72 72 6f 72 if( !isError
13501 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20 20 61 Reset ){. a
13502 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
13503 74 61 74 65 20 3c 3d 20 50 41 47 45 52 5f 53 48 tate <= PAGER_SH
13504 41 52 45 44 20 29 3b 0a 20 20 20 20 20 20 72 63 ARED );. rc
13505 20 3d 20 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c = hasHotJournal
13506 28 70 50 61 67 65 72 2c 20 26 69 73 48 6f 74 4a (pPager, &isHotJ
13507 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 69 ournal);. i
13508 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
13509 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f ){. goto
1350a 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d failed;. }
1350b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 . }. if( i
1350c 73 45 72 72 6f 72 52 65 73 65 74 20 7c 7c 20 69 sErrorReset || i
1350d 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 sHotJournal ){.
1350e 20 20 20 20 20 2f 2a 20 47 65 74 20 61 6e 20 45 /* Get an E
1350f 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e XCLUSIVE lock on
13510 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
13511 6c 65 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e le. At this poin
13512 74 20 69 74 20 69 73 0a 20 20 20 20 20 20 2a 2a t it is. **
13513 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 important that
13514 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
13515 69 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 is not obtained
13516 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 74 68 on the way to th
13517 65 0a 20 20 20 20 20 20 2a 2a 20 45 58 43 4c 55 e. ** EXCLU
13518 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 69 74 SIVE lock. If it
13519 20 77 65 72 65 2c 20 61 6e 6f 74 68 65 72 20 70 were, another p
1351a 72 6f 63 65 73 73 20 6d 69 67 68 74 20 6f 70 65 rocess might ope
1351b 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 n the. ** d
1351c 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 64 65 atabase file, de
1351d 74 65 63 74 20 74 68 65 20 52 45 53 45 52 56 45 tect the RESERVE
1351e 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63 6f 6e 63 D lock, and conc
1351f 6c 75 64 65 20 74 68 61 74 20 74 68 65 0a 20 20 lude that the.
13520 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 ** database
13521 69 73 20 73 61 66 65 20 74 6f 20 72 65 61 64 20 is safe to read
13522 77 68 69 6c 65 20 74 68 69 73 20 70 72 6f 63 65 while this proce
13523 73 73 20 69 73 20 73 74 69 6c 6c 20 72 6f 6c 6c ss is still roll
13524 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 2a ing the . *
13525 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 61 * hot-journal ba
13526 63 6b 2e 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20 ck.. ** .
13527 20 20 20 20 2a 2a 20 42 65 63 61 75 73 65 20 74 ** Because t
13528 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 he intermediate
13529 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 RESERVED lock is
1352a 20 6e 6f 74 20 72 65 71 75 65 73 74 65 64 2c 20 not requested,
1352b 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68 any. ** oth
1352c 65 72 20 70 72 6f 63 65 73 73 20 61 74 74 65 6d er process attem
1352d 70 74 69 6e 67 20 74 6f 20 61 63 63 65 73 73 20 pting to access
1352e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1352f 65 20 77 69 6c 6c 20 67 65 74 20 74 6f 20 0a 20 e will get to .
13530 20 20 20 20 20 2a 2a 20 74 68 69 73 20 70 6f 69 ** this poi
13531 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 61 nt in the code a
13532 6e 64 20 66 61 69 6c 20 74 6f 20 6f 62 74 61 69 nd fail to obtai
13533 6e 20 69 74 73 20 6f 77 6e 20 45 58 43 4c 55 53 n its own EXCLUS
13534 49 56 45 20 6c 6f 63 6b 20 0a 20 20 20 20 20 20 IVE lock .
13535 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ** on the databa
13536 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 2a se file.. *
13537 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 /. if( pPag
13538 65 72 2d 3e 73 74 61 74 65 3c 45 58 43 4c 55 53 er->state<EXCLUS
13539 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 IVE_LOCK ){.
1353a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1353b 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 OsLock(pPager->f
1353c 64 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 d, EXCLUSIVE_LOC
1353d 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 K);. if(
1353e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1353f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
13540 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 pager_error(pPag
13541 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 er, rc);.
13542 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a goto failed;.
13543 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
13544 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 pPager->state
13545 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 = PAGER_EXCLUSIV
13546 45 3b 0a 20 20 20 20 20 20 7d 0a 20 0a 20 20 20 E;. }. .
13547 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a /* Open the j
13548 6f 75 72 6e 61 6c 20 66 6f 72 20 72 65 61 64 2f ournal for read/
13549 77 72 69 74 65 20 61 63 63 65 73 73 2e 20 54 68 write access. Th
1354a 69 73 20 69 73 20 62 65 63 61 75 73 65 20 69 6e is is because in
1354b 20 0a 20 20 20 20 20 20 2a 2a 20 65 78 63 6c 75 . ** exclu
1354c 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 sive-access mode
1354d 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
1354e 70 74 6f 72 20 77 69 6c 6c 20 62 65 20 6b 65 70 ptor will be kep
1354f 74 20 6f 70 65 6e 20 61 6e 64 0a 20 20 20 20 20 t open and.
13550 20 2a 2a 20 70 6f 73 73 69 62 6c 79 20 75 73 65 ** possibly use
13551 64 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63 74 d for a transact
13552 69 6f 6e 20 6c 61 74 65 72 20 6f 6e 2e 20 4f 6e ion later on. On
13553 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 74 some systems, t
13554 68 65 0a 20 20 20 20 20 20 2a 2a 20 4f 73 54 72 he. ** OsTr
13555 75 6e 63 61 74 65 28 29 20 63 61 6c 6c 20 75 73 uncate() call us
13556 65 64 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d ed in exclusive-
13557 61 63 63 65 73 73 20 6d 6f 64 65 20 61 6c 73 6f access mode also
13558 20 72 65 71 75 69 72 65 73 0a 20 20 20 20 20 20 requires.
13559 2a 2a 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 ** a read/write
1355a 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 20 20 20 file handle..
1355b 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
1355c 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e !isOpen(pPager->
1355d 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 jfd) ){.
1355e 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 20 int res;.
1355f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 rc = sqlite3OsA
13560 63 63 65 73 73 28 70 56 66 73 2c 70 50 61 67 65 ccess(pVfs,pPage
13561 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 53 51 4c 49 r->zJournal,SQLI
13562 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 TE_ACCESS_EXISTS
13563 2c 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 ,&res);.
13564 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
13565 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 K ){. i
13566 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 f( res ){.
13567 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74 20 3d int fout =
13568 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 0;.
13569 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f int f = SQLITE_O
1356a 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 PEN_READWRITE|SQ
1356b 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a LITE_OPEN_MAIN_J
1356c 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 OURNAL;.
1356d 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 assert( !pPa
1356e 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b ger->tempFile );
1356f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 . rc
13570 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 = sqlite3OsOpen(
13571 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a pVfs, pPager->zJ
13572 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e ournal, pPager->
13573 6a 66 64 2c 20 66 2c 20 26 66 6f 75 74 29 3b 0a jfd, f, &fout);.
13574 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 asse
13575 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f rt( rc!=SQLITE_O
13576 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 K || isOpen(pPag
13577 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 20 20 er->jfd) );.
13578 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
13579 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 66 6f 75 SQLITE_OK && fou
1357a 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 t&SQLITE_OPEN_RE
1357b 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 ADONLY ){.
1357c 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
1357d 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 ITE_CANTOPEN;.
1357e 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
1357f 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 te3OsClose(pPage
13580 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 20 r->jfd);.
13581 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
13582 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
13583 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f /* If the jo
13584 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 20 65 urnal does not e
13585 78 69 73 74 2c 20 69 74 20 75 73 75 61 6c 6c 79 xist, it usually
13586 20 6d 65 61 6e 73 20 74 68 61 74 20 73 6f 6d 65 means that some
13587 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a . **
13588 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f other connectio
13589 6e 20 6d 61 6e 61 67 65 64 20 74 6f 20 67 65 74 n managed to get
1358a 20 69 6e 20 61 6e 64 20 72 6f 6c 6c 20 69 74 20 in and roll it
1358b 62 61 63 6b 20 62 65 66 6f 72 65 20 0a 20 20 20 back before .
1358c 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73 ** this
1358d 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 74 61 connection obta
1358e 69 6e 65 64 20 74 68 65 20 65 78 63 6c 75 73 69 ined the exclusi
1358f 76 65 20 6c 6f 63 6b 20 61 62 6f 76 65 2e 20 4f ve lock above. O
13590 72 2c 20 69 74 20 0a 20 20 20 20 20 20 20 20 20 r, it .
13591 20 20 20 2a 2a 20 6d 61 79 20 6d 65 61 6e 20 74 ** may mean t
13592 68 61 74 20 74 68 65 20 70 61 67 65 72 20 77 61 hat the pager wa
13593 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 2d 73 s in the error-s
13594 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 0a 20 tate when this.
13595 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 75 ** fu
13596 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 nction was calle
13597 64 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 d and the journa
13598 6c 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 l file does not
13599 65 78 69 73 74 2e 20 20 2a 2f 0a 20 20 20 20 20 exist. */.
1359a 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 rc = page
1359b 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f r_end_transactio
1359c 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 n(pPager, 0);.
1359d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1359e 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1359f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
135a0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 _OK ){. g
135a1 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 oto failed;.
135a2 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 54 4f }.. /* TO
135a3 44 4f 3a 20 57 68 79 20 61 72 65 20 74 68 65 73 DO: Why are thes
135a4 65 20 63 6c 65 61 72 65 64 20 68 65 72 65 3f 20 e cleared here?
135a5 49 73 20 69 74 20 6e 65 63 65 73 73 61 72 79 3f Is it necessary?
135a6 20 2a 2f 0a 20 20 20 20 20 20 70 50 61 67 65 72 */. pPager
135a7 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 ->journalStarted
135a8 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 = 0;. pPag
135a9 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d er->journalOff =
135aa 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 0;. pPager
135ab 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b ->setMaster = 0;
135ac 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a . pPager->j
135ad 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20 ournalHdr = 0;.
135ae 0a 20 20 20 20 20 20 2f 2a 20 50 6c 61 79 62 61 . /* Playba
135af 63 6b 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 ck and delete th
135b0 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 72 6f 70 e journal. Drop
135b1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 72 the database wr
135b2 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 63 ite. ** loc
135b3 6b 20 61 6e 64 20 72 65 61 63 71 75 69 72 65 20 k and reacquire
135b4 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 20 50 the read lock. P
135b5 75 72 67 65 20 74 68 65 20 63 61 63 68 65 20 62 urge the cache b
135b6 65 66 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 70 efore. ** p
135b7 6c 61 79 69 6e 67 20 62 61 63 6b 20 74 68 65 20 laying back the
135b8 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 73 6f 20 74 hot-journal so t
135b9 68 61 74 20 77 65 20 64 6f 6e 27 74 20 65 6e 64 hat we don't end
135ba 20 75 70 20 77 69 74 68 0a 20 20 20 20 20 20 2a up with. *
135bb 2a 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e * an inconsisten
135bc 74 20 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a t cache.. *
135bd 2f 0a 20 20 20 20 20 20 69 66 28 20 69 73 4f 70 /. if( isOp
135be 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
135bf 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
135c0 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 pager_playback(p
135c1 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 20 Pager, 1);.
135c2 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
135c3 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
135c4 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72 rc = pager_err
135c5 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a or(pPager, rc);.
135c6 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 goto f
135c7 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d ailed;. }
135c8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
135c9 73 73 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e ssert( (pPager->
135ca 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 41 state==PAGER_SHA
135cb 52 45 44 29 0a 20 20 20 20 20 20 20 20 20 20 20 RED).
135cc 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 78 63 6c || (pPager->excl
135cd 75 73 69 76 65 4d 6f 64 65 20 26 26 20 70 50 61 usiveMode && pPa
135ce 67 65 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52 ger->state>PAGER
135cf 5f 53 48 41 52 45 44 29 0a 20 20 20 20 20 20 29 _SHARED). )
135d0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 ;. }.. if(
135d1 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 pPager->pBackup
135d2 20 7c 7c 20 73 71 6c 69 74 65 33 50 63 61 63 68 || sqlite3Pcach
135d3 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 ePagecount(pPage
135d4 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 29 7b r->pPCache)>0 ){
135d5 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 68 . /* The sh
135d6 61 72 65 64 2d 6c 6f 63 6b 20 68 61 73 20 6a 75 ared-lock has ju
135d7 73 74 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 st been acquired
135d8 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
135d9 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 file. ** a
135da 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 6c 72 nd there are alr
135db 65 61 64 79 20 70 61 67 65 73 20 69 6e 20 74 68 eady pages in th
135dc 65 20 63 61 63 68 65 20 28 66 72 6f 6d 20 61 20 e cache (from a
135dd 70 72 65 76 69 6f 75 73 0a 20 20 20 20 20 20 2a previous. *
135de 2a 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 * read or write
135df 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 20 43 transaction). C
135e0 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 heck to see if t
135e1 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 he database.
135e2 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f ** has been mo
135e3 64 69 66 69 65 64 2e 20 20 49 66 20 74 68 65 20 dified. If the
135e4 64 61 74 61 62 61 73 65 20 68 61 73 20 63 68 61 database has cha
135e5 6e 67 65 64 2c 20 66 6c 75 73 68 20 74 68 65 0a nged, flush the.
135e6 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 0a ** cache..
135e7 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
135e8 2a 20 44 61 74 61 62 61 73 65 20 63 68 61 6e 67 * Database chang
135e9 65 73 20 69 73 20 64 65 74 65 63 74 65 64 20 62 es is detected b
135ea 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 20 y looking at 15
135eb 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67 0a bytes beginning.
135ec 20 20 20 20 20 20 2a 2a 20 61 74 20 6f 66 66 73 ** at offs
135ed 65 74 20 32 34 20 69 6e 74 6f 20 74 68 65 20 66 et 24 into the f
135ee 69 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 ile. The first
135ef 34 20 6f 66 20 74 68 65 73 65 20 31 36 20 62 79 4 of these 16 by
135f0 74 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a 2a tes are. **
135f1 20 61 20 33 32 2d 62 69 74 20 63 6f 75 6e 74 65 a 32-bit counte
135f2 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d r that is increm
135f3 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 68 20 ented with each
135f4 63 68 61 6e 67 65 2e 20 20 54 68 65 0a 20 20 20 change. The.
135f5 20 20 20 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 ** other byte
135f6 73 20 63 68 61 6e 67 65 20 72 61 6e 64 6f 6d 6c s change randoml
135f7 79 20 77 69 74 68 20 65 61 63 68 20 66 69 6c 65 y with each file
135f8 20 63 68 61 6e 67 65 20 77 68 65 6e 0a 20 20 20 change when.
135f9 20 20 20 2a 2a 20 61 20 63 6f 64 65 63 20 69 73 ** a codec is
135fa 20 69 6e 20 75 73 65 2e 0a 20 20 20 20 20 20 2a in use.. *
135fb 2a 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 * . ** Ther
135fc 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e 67 e is a vanishing
135fd 6c 79 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65 20 ly small chance
135fe 74 68 61 74 20 61 20 63 68 61 6e 67 65 20 77 69 that a change wi
135ff 6c 6c 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 20 ll not be .
13600 20 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 20 54 ** detected. T
13601 68 65 20 63 68 61 6e 63 65 20 6f 66 20 61 6e 20 he chance of an
13602 75 6e 64 65 74 65 63 74 65 64 20 63 68 61 6e 67 undetected chang
13603 65 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68 e is so small th
13604 61 74 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 63 at. ** it c
13605 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65 64 2e an be neglected.
13606 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
13607 63 68 61 72 20 64 62 46 69 6c 65 56 65 72 73 5b char dbFileVers[
13608 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 sizeof(pPager->d
13609 62 46 69 6c 65 56 65 72 73 29 5d 3b 0a 20 20 20 bFileVers)];.
1360a 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 sqlite3PagerP
1360b 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c agecount(pPager,
1360c 20 30 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 0);.. if(
1360d 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
1360e 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
1360f 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b pPager->errCode;
13610 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 . goto fa
13611 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 iled;. }..
13612 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
13613 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
13614 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 );. if( pP
13615 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29 ager->dbSize>0 )
13616 7b 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 {. IOTRAC
13617 45 28 28 22 43 4b 56 45 52 53 20 25 70 20 25 64 E(("CKVERS %p %d
13618 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 73 69 7a \n", pPager, siz
13619 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 eof(dbFileVers))
1361a 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
1361b 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 sqlite3OsRead(pP
1361c 61 67 65 72 2d 3e 66 64 2c 20 26 64 62 46 69 6c ager->fd, &dbFil
1361d 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 eVers, sizeof(db
1361e 46 69 6c 65 56 65 72 73 29 2c 20 32 34 29 3b 0a FileVers), 24);.
1361f 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
13620 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13621 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c goto fail
13622 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ed;. }.
13623 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
13624 20 20 20 6d 65 6d 73 65 74 28 64 62 46 69 6c 65 memset(dbFile
13625 56 65 72 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Vers, 0, sizeof(
13626 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 dbFileVers));.
13627 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 }.. if(
13628 20 6d 65 6d 63 6d 70 28 70 50 61 67 65 72 2d 3e memcmp(pPager->
13629 64 62 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69 dbFileVers, dbFi
1362a 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 leVers, sizeof(d
1362b 62 46 69 6c 65 56 65 72 73 29 29 21 3d 30 20 29 bFileVers))!=0 )
1362c 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 5f {. pager_
1362d 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 reset(pPager);.
1362e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1362f 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13630 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c >exclusiveMode |
13631 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d | pPager->state=
13632 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b =PAGER_SHARED );
13633 0a 20 20 7d 0a 0a 20 66 61 69 6c 65 64 3a 0a 20 . }.. failed:.
13634 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
13635 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67 OK ){. /* pag
13636 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 61 er_unlock() is a
13637 20 6e 6f 2d 6f 70 20 66 6f 72 20 65 78 63 6c 75 no-op for exclu
13638 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 6e sive mode and in
13639 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
1363a 73 2e 20 2a 2f 0a 20 20 20 20 70 61 67 65 72 5f s. */. pager_
1363b 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a unlock(pPager);.
1363c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1363d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
1363e 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
1363f 20 68 61 73 20 72 65 61 63 68 65 64 20 7a 65 72 has reached zer
13640 6f 2c 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 o, rollback any
13641 61 63 74 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61 active.** transa
13642 63 74 69 6f 6e 20 61 6e 64 20 75 6e 6c 6f 63 6b ction and unlock
13643 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a the pager..**.*
13644 2a 20 45 78 63 65 70 74 2c 20 69 6e 20 6c 6f 63 * Except, in loc
13645 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53 king_mode=EXCLUS
13646 49 56 45 20 77 68 65 6e 20 74 68 65 72 65 20 69 IVE when there i
13647 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 69 6e 0a s nothing to in.
13648 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 ** the rollback
13649 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 20 75 6e 6c journal, the unl
1364a 6f 63 6b 20 69 73 20 6e 6f 74 20 70 65 72 66 6f ock is not perfo
1364b 72 6d 65 64 20 61 6e 64 20 74 68 65 72 65 20 69 rmed and there i
1364c 73 0a 2a 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 s.** nothing to
1364d 72 6f 6c 6c 62 61 63 6b 2c 20 73 6f 20 74 68 69 rollback, so thi
1364e 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e s routine is a n
1364f 6f 2d 6f 70 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 o-op..*/ .static
13650 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 void pagerUnloc
13651 6b 49 66 55 6e 75 73 65 64 28 50 61 67 65 72 20 kIfUnused(Pager
13652 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 *pPager){. if(
13653 28 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 (sqlite3PcacheRe
13654 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 fCount(pPager->p
13655 50 43 61 63 68 65 29 3d 3d 30 29 0a 20 20 20 26 PCache)==0). &
13656 26 20 28 21 70 50 61 67 65 72 2d 3e 65 78 63 6c & (!pPager->excl
13657 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61 usiveMode || pPa
13658 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e ger->journalOff>
13659 30 29 20 0a 20 20 29 7b 0a 20 20 20 20 70 61 67 0) . ){. pag
1365a 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 erUnlockAndRollb
1365b 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d ack(pPager);. }
1365c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 .}../*.** Acquir
1365d 65 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f e a reference to
1365e 20 70 61 67 65 20 6e 75 6d 62 65 72 20 70 67 6e page number pgn
1365f 6f 20 69 6e 20 70 61 67 65 72 20 70 50 61 67 65 o in pager pPage
13660 72 20 28 61 20 70 61 67 65 0a 2a 2a 20 72 65 66 r (a page.** ref
13661 65 72 65 6e 63 65 20 68 61 73 20 74 79 70 65 20 erence has type
13662 44 62 50 61 67 65 2a 29 2e 20 49 66 20 74 68 65 DbPage*). If the
13663 20 72 65 71 75 65 73 74 65 64 20 72 65 66 65 72 requested refer
13664 65 6e 63 65 20 69 73 20 0a 2a 2a 20 73 75 63 63 ence is .** succ
13665 65 73 73 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65 essfully obtaine
13666 64 2c 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 d, it is copied
13667 74 6f 20 2a 70 70 50 61 67 65 20 61 6e 64 20 53 to *ppPage and S
13668 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 QLITE_OK returne
13669 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
1366a 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 requested page i
1366b 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 s already in the
1366c 20 63 61 63 68 65 2c 20 69 74 20 69 73 20 72 65 cache, it is re
1366d 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 turned. .** Othe
1366e 72 77 69 73 65 2c 20 61 20 6e 65 77 20 70 61 67 rwise, a new pag
1366f 65 20 6f 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f e object is allo
13670 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61 cated and popula
13671 74 65 64 20 77 69 74 68 20 64 61 74 61 0a 2a 2a ted with data.**
13672 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 read from the d
13673 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 6e atabase file. In
13674 20 73 6f 6d 65 20 63 61 73 65 73 2c 20 74 68 65 some cases, the
13675 20 70 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 6d pcache module m
13676 61 79 0a 2a 2a 20 63 68 6f 6f 73 65 20 6e 6f 74 ay.** choose not
13677 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e to allocate a n
13678 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61 ew page object a
13679 6e 64 20 6d 61 79 20 72 65 75 73 65 20 61 6e 20 nd may reuse an
1367a 65 78 69 73 74 69 6e 67 0a 2a 2a 20 6f 62 6a 65 existing.** obje
1367b 63 74 20 77 69 74 68 20 6e 6f 20 6f 75 74 73 74 ct with no outst
1367c 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 anding reference
1367d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 74 s..**.** The ext
1367e 72 61 20 64 61 74 61 20 61 70 70 65 6e 64 65 64 ra data appended
1367f 20 74 6f 20 61 20 70 61 67 65 20 69 73 20 61 6c to a page is al
13680 77 61 79 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 ways initialized
13681 20 74 6f 20 7a 65 72 6f 73 20 74 68 65 20 0a 2a to zeros the .*
13682 2a 20 66 69 72 73 74 20 74 69 6d 65 20 61 20 70 * first time a p
13683 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 69 6e age is loaded in
13684 74 6f 20 6d 65 6d 6f 72 79 2e 20 49 66 20 74 68 to memory. If th
13685 65 20 70 61 67 65 20 72 65 71 75 65 73 74 65 64 e page requested
13686 20 69 73 20 0a 2a 2a 20 61 6c 72 65 61 64 79 20 is .** already
13687 69 6e 20 74 68 65 20 63 61 63 68 65 20 77 68 65 in the cache whe
13688 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
13689 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 is called, then
1368a 74 68 65 20 65 78 74 72 61 0a 2a 2a 20 64 61 74 the extra.** dat
1368b 61 20 69 73 20 6c 65 66 74 20 61 73 20 69 74 20 a is left as it
1368c 77 61 73 20 77 68 65 6e 20 74 68 65 20 70 61 67 was when the pag
1368d 65 20 6f 62 6a 65 63 74 20 77 61 73 20 6c 61 73 e object was las
1368e 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 t used..**.** If
1368f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d the database im
13690 61 67 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 age is smaller t
13691 68 61 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 han the requeste
13692 64 20 70 61 67 65 20 6f 72 20 69 66 20 61 20 0a d page or if a .
13693 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 ** non-zero valu
13694 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 e is passed as t
13695 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 70 61 72 he noContent par
13696 61 6d 65 74 65 72 20 61 6e 64 20 74 68 65 20 0a ameter and the .
13697 2a 2a 20 72 65 71 75 65 73 74 65 64 20 70 61 67 ** requested pag
13698 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 e is not already
13699 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 stored in the c
1369a 61 63 68 65 2c 20 74 68 65 6e 20 6e 6f 20 0a 2a ache, then no .*
1369b 2a 20 61 63 74 75 61 6c 20 64 69 73 6b 20 72 65 * actual disk re
1369c 61 64 20 6f 63 63 75 72 73 2e 20 49 6e 20 74 68 ad occurs. In th
1369d 69 73 20 63 61 73 65 20 74 68 65 20 6d 65 6d 6f is case the memo
1369e 72 79 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20 ry image of the
1369f 0a 2a 2a 20 70 61 67 65 20 69 73 20 69 6e 69 74 .** page is init
136a0 69 61 6c 69 7a 65 64 20 74 6f 20 61 6c 6c 20 7a ialized to all z
136a1 65 72 6f 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 eros. .**.** If
136a2 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74 72 75 noContent is tru
136a3 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 e, it means that
136a4 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 we do not care
136a5 61 62 6f 75 74 20 74 68 65 20 63 6f 6e 74 65 6e about the conten
136a6 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 ts.** of the pag
136a7 65 2e 20 54 68 69 73 20 6f 63 63 75 72 73 20 69 e. This occurs i
136a8 6e 20 74 77 6f 20 73 65 70 65 72 61 74 65 20 73 n two seperate s
136a9 63 65 6e 61 72 69 6f 73 3a 0a 2a 2a 0a 2a 2a 20 cenarios:.**.**
136aa 20 20 61 29 20 57 68 65 6e 20 72 65 61 64 69 6e a) When readin
136ab 67 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 g a free-list le
136ac 61 66 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 af page from the
136ad 20 64 61 74 61 62 61 73 65 2c 20 61 6e 64 0a 2a database, and.*
136ae 2a 0a 2a 2a 20 20 20 62 29 20 57 68 65 6e 20 61 *.** b) When a
136af 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 62 65 savepoint is be
136b0 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 ing rolled back
136b1 61 6e 64 20 77 65 20 6e 65 65 64 20 74 6f 20 6c and we need to l
136b2 6f 61 64 0a 2a 2a 20 20 20 20 20 20 61 20 6e 65 oad.** a ne
136b3 77 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 w page into the
136b4 63 61 63 68 65 20 74 6f 20 70 6f 70 75 6c 61 74 cache to populat
136b5 65 20 77 69 74 68 20 74 68 65 20 64 61 74 61 20 e with the data
136b6 72 65 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f read.** fro
136b7 6d 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 m the savepoint
136b8 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 journal..**.** I
136b9 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74 f noContent is t
136ba 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 64 61 rue, then the da
136bb 74 61 20 72 65 74 75 72 6e 65 64 20 69 73 20 7a ta returned is z
136bc 65 72 6f 65 64 20 69 6e 73 74 65 61 64 20 6f 66 eroed instead of
136bd 0a 2a 2a 20 62 65 69 6e 67 20 72 65 61 64 20 66 .** being read f
136be 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
136bf 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 . Additionally,
136c0 74 68 65 20 62 69 74 73 20 63 6f 72 72 65 73 70 the bits corresp
136c1 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 70 67 6e onding.** to pgn
136c2 6f 20 69 6e 20 50 61 67 65 72 2e 70 49 6e 4a 6f o in Pager.pInJo
136c3 75 72 6e 61 6c 20 28 62 69 74 76 65 63 20 6f 66 urnal (bitvec of
136c4 20 70 61 67 65 73 20 61 6c 72 65 61 64 79 20 77 pages already w
136c5 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a ritten to the.**
136c6 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 61 journal file) a
136c7 6e 64 20 74 68 65 20 50 61 67 65 72 53 61 76 65 nd the PagerSave
136c8 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 point.pInSavepoi
136c9 6e 74 20 62 69 74 76 65 63 73 20 6f 66 20 61 6e nt bitvecs of an
136ca 79 20 6f 70 65 6e 0a 2a 2a 20 73 61 76 65 70 6f y open.** savepo
136cb 69 6e 74 73 20 61 72 65 20 73 65 74 2e 20 54 68 ints are set. Th
136cc 69 73 20 6d 65 61 6e 73 20 69 66 20 74 68 65 20 is means if the
136cd 70 61 67 65 20 69 73 20 6d 61 64 65 20 77 72 69 page is made wri
136ce 74 61 62 6c 65 20 61 74 20 61 6e 79 0a 2a 2a 20 table at any.**
136cf 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 point in the fut
136d0 75 72 65 2c 20 75 73 69 6e 67 20 61 20 63 61 6c ure, using a cal
136d1 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 l to sqlite3Page
136d2 72 57 72 69 74 65 28 29 2c 20 69 74 73 20 63 6f rWrite(), its co
136d3 6e 74 65 6e 74 73 0a 2a 2a 20 77 69 6c 6c 20 6e ntents.** will n
136d4 6f 74 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e ot be journaled.
136d5 20 54 68 69 73 20 73 61 76 65 73 20 49 4f 2e 0a This saves IO..
136d6 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 71 75 69 73 **.** The acquis
136d7 69 74 69 6f 6e 20 6d 69 67 68 74 20 66 61 69 6c ition might fail
136d8 20 66 6f 72 20 73 65 76 65 72 61 6c 20 72 65 61 for several rea
136d9 73 6f 6e 73 2e 20 20 49 6e 20 61 6c 6c 20 63 61 sons. In all ca
136da 73 65 73 2c 0a 2a 2a 20 61 6e 20 61 70 70 72 6f ses,.** an appro
136db 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64 priate error cod
136dc 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e e is returned an
136dd 64 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74 d *ppPage is set
136de 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 to NULL..**.**
136df 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 See also sqlite3
136e0 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 2e 20 20 PagerLookup().
136e1 42 6f 74 68 20 74 68 69 73 20 72 6f 75 74 69 6e Both this routin
136e2 65 20 61 6e 64 20 4c 6f 6f 6b 75 70 28 29 20 61 e and Lookup() a
136e3 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 66 69 6e ttempt.** to fin
136e4 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 65 20 d a page in the
136e5 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 in-memory cache
136e6 66 69 72 73 74 2e 20 20 49 66 20 74 68 65 20 70 first. If the p
136e7 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 age is not alrea
136e8 64 79 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 2c dy.** in memory,
136e9 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 67 6f this routine go
136ea 65 73 20 74 6f 20 64 69 73 6b 20 74 6f 20 72 65 es to disk to re
136eb 61 64 20 69 74 20 69 6e 20 77 68 65 72 65 61 73 ad it in whereas
136ec 20 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 6a 75 73 Lookup().** jus
136ed 74 20 72 65 74 75 72 6e 73 20 30 2e 20 20 54 68 t returns 0. Th
136ee 69 73 20 72 6f 75 74 69 6e 65 20 61 63 71 75 69 is routine acqui
136ef 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 res a read-lock
136f0 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69 the first time i
136f1 74 0a 2a 2a 20 68 61 73 20 74 6f 20 67 6f 20 74 t.** has to go t
136f2 6f 20 64 69 73 6b 2c 20 61 6e 64 20 63 6f 75 6c o disk, and coul
136f3 64 20 61 6c 73 6f 20 70 6c 61 79 62 61 63 6b 20 d also playback
136f4 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 69 an old journal i
136f5 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 f necessary..**
136f6 53 69 6e 63 65 20 4c 6f 6f 6b 75 70 28 29 20 6e Since Lookup() n
136f7 65 76 65 72 20 67 6f 65 73 20 74 6f 20 64 69 73 ever goes to dis
136f8 6b 2c 20 69 74 20 6e 65 76 65 72 20 68 61 73 20 k, it never has
136f9 74 6f 20 64 65 61 6c 20 77 69 74 68 20 6c 6f 63 to deal with loc
136fa 6b 73 0a 2a 2a 20 6f 72 20 6a 6f 75 72 6e 61 6c ks.** or journal
136fb 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 files..*/.SQLIT
136fc 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
136fd 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 lite3PagerAcquir
136fe 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 e(. Pager *pPag
136ff 65 72 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 er, /* The
13700 70 61 67 65 72 20 6f 70 65 6e 20 6f 6e 20 74 68 pager open on th
13701 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
13702 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 */. Pgno pgno,
13703 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
13704 20 6e 75 6d 62 65 72 20 74 6f 20 66 65 74 63 68 number to fetch
13705 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 2a 70 */. DbPage **p
13706 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 57 72 69 pPage, /* Wri
13707 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 te a pointer to
13708 74 68 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f the page here */
13709 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 . int noContent
1370a 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 /* Do not
1370b 20 62 6f 74 68 65 72 20 72 65 61 64 69 6e 67 20 bother reading
1370c 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 64 69 73 content from dis
1370d 6b 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a k if true */.){.
1370e 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 48 64 int rc;. PgHd
1370f 72 20 2a 70 50 67 3b 0a 0a 20 20 61 73 73 65 72 r *pPg;.. asser
13710 74 28 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f t( assert_pager_
13711 73 74 61 74 65 28 70 50 61 67 65 72 29 20 29 3b state(pPager) );
13712 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
13713 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52 5f 55 r->state>PAGER_U
13714 4e 4c 4f 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20 NLOCK );.. if(
13715 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 72 pgno==0 ){. r
13716 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
13717 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a RUPT_BKPT;. }..
13718 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 /* If the page
13719 72 20 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f r is in the erro
1371a 72 20 73 74 61 74 65 2c 20 72 65 74 75 72 6e 20 r state, return
1371b 61 6e 20 65 72 72 6f 72 20 69 6d 6d 65 64 69 61 an error immedia
1371c 74 65 6c 79 2e 20 0a 20 20 2a 2a 20 4f 74 68 65 tely. . ** Othe
1371d 72 77 69 73 65 2c 20 72 65 71 75 65 73 74 20 74 rwise, request t
1371e 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 he page from the
1371f 20 50 43 61 63 68 65 20 6c 61 79 65 72 2e 20 2a PCache layer. *
13720 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e /. if( pPager->
13721 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f errCode!=SQLITE_
13722 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 OK && pPager->er
13723 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 rCode!=SQLITE_FU
13724 4c 4c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 LL ){. rc = p
13725 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a Pager->errCode;.
13726 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 }else{. rc
13727 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 = sqlite3PcacheF
13728 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 etch(pPager->pPC
13729 61 63 68 65 2c 20 70 67 6e 6f 2c 20 31 2c 20 70 ache, pgno, 1, p
1372a 70 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 pPage);. }.. i
1372b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1372c 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 ){. /* Eithe
1372d 72 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 r the call to sq
1372e 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 lite3PcacheFetch
1372f 28 29 20 72 65 74 75 72 6e 65 64 20 61 6e 20 65 () returned an e
13730 72 72 6f 72 20 6f 72 20 74 68 65 0a 20 20 20 20 rror or the.
13731 2a 2a 20 70 61 67 65 72 20 77 61 73 20 61 6c 72 ** pager was alr
13732 65 61 64 79 20 69 6e 20 74 68 65 20 65 72 72 6f eady in the erro
13733 72 2d 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 r-state when thi
13734 73 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 s function was c
13735 61 6c 6c 65 64 2e 0a 20 20 20 20 2a 2a 20 53 65 alled.. ** Se
13736 74 20 70 50 67 20 74 6f 20 30 20 61 6e 64 20 6a t pPg to 0 and j
13737 75 6d 70 20 74 6f 20 74 68 65 20 65 78 63 65 70 ump to the excep
13738 74 69 6f 6e 20 68 61 6e 64 6c 65 72 2e 20 20 2a tion handler. *
13739 2f 0a 20 20 20 20 70 50 67 20 3d 20 30 3b 0a 20 /. pPg = 0;.
1373a 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 goto pager_ac
1373b 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 7d 0a 20 quire_err;. }.
1373c 20 61 73 73 65 72 74 28 20 28 2a 70 70 50 61 67 assert( (*ppPag
1373d 65 29 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 20 29 e)->pgno==pgno )
1373e 3b 0a 20 20 61 73 73 65 72 74 28 20 28 2a 70 70 ;. assert( (*pp
1373f 50 61 67 65 29 2d 3e 70 50 61 67 65 72 3d 3d 70 Page)->pPager==p
13740 50 61 67 65 72 20 7c 7c 20 28 2a 70 70 50 61 67 Pager || (*ppPag
13741 65 29 2d 3e 70 50 61 67 65 72 3d 3d 30 20 29 3b e)->pPager==0 );
13742 0a 0a 20 20 69 66 28 20 28 2a 70 70 50 61 67 65 .. if( (*ppPage
13743 29 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20 20 20 )->pPager ){.
13744 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 /* In this case
13745 20 74 68 65 20 70 63 61 63 68 65 20 61 6c 72 65 the pcache alre
13746 61 64 79 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 ady contains an
13747 69 6e 69 74 69 61 6c 69 7a 65 64 20 63 6f 70 79 initialized copy
13748 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 of. ** the p
13749 61 67 65 2e 20 52 65 74 75 72 6e 20 77 69 74 68 age. Return with
1374a 6f 75 74 20 66 75 72 74 68 65 72 20 61 64 6f 2e out further ado.
1374b 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
1374c 20 70 67 6e 6f 3c 3d 50 41 47 45 52 5f 4d 41 58 pgno<=PAGER_MAX
1374d 5f 50 47 4e 4f 20 26 26 20 70 67 6e 6f 21 3d 50 _PGNO && pgno!=P
1374e 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 AGER_MJ_PGNO(pPa
1374f 67 65 72 29 20 29 3b 0a 20 20 20 20 50 41 47 45 ger) );. PAGE
13750 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e R_INCR(pPager->n
13751 48 69 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e Hit);. return
13752 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 7d SQLITE_OK;.. }
13753 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 else{. /* The
13754 20 70 61 67 65 72 20 63 61 63 68 65 20 68 61 73 pager cache has
13755 20 63 72 65 61 74 65 64 20 61 20 6e 65 77 20 70 created a new p
13756 61 67 65 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 age. Its content
13757 20 6e 65 65 64 73 20 74 6f 20 0a 20 20 20 20 2a needs to . *
13758 2a 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 * be initialized
13759 2e 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 4d . */. int nM
1375a 61 78 3b 0a 0a 20 20 20 20 50 41 47 45 52 5f 49 ax;.. PAGER_I
1375b 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 4d 69 73 NCR(pPager->nMis
1375c 73 29 3b 0a 20 20 20 20 70 50 67 20 3d 20 2a 70 s);. pPg = *p
1375d 70 50 61 67 65 3b 0a 20 20 20 20 70 50 67 2d 3e pPage;. pPg->
1375e 70 50 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b pPager = pPager;
1375f 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 .. /* The max
13760 69 6d 75 6d 20 70 61 67 65 20 6e 75 6d 62 65 72 imum page number
13761 20 69 73 20 32 5e 33 31 2e 20 52 65 74 75 72 6e is 2^31. Return
13762 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 SQLITE_CORRUPT
13763 69 66 20 61 20 70 61 67 65 0a 20 20 20 20 2a 2a if a page. **
13764 20 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 number greater
13765 74 68 61 6e 20 74 68 69 73 2c 20 6f 72 20 74 68 than this, or th
13766 65 20 75 6e 75 73 65 64 20 6c 6f 63 6b 69 6e 67 e unused locking
13767 2d 70 61 67 65 2c 20 69 73 20 72 65 71 75 65 73 -page, is reques
13768 74 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 ted. */. if(
13769 70 67 6e 6f 3e 50 41 47 45 52 5f 4d 41 58 5f 50 pgno>PAGER_MAX_P
1376a 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 GNO || pgno==PAG
1376b 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 ER_MJ_PGNO(pPage
1376c 72 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d r) ){. rc =
1376d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1376e 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f BKPT;. goto
1376f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 pager_acquire_e
13770 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 rr;. }.. r
13771 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
13772 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 Pagecount(pPager
13773 2c 20 26 6e 4d 61 78 29 3b 0a 20 20 20 20 69 66 , &nMax);. if
13774 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
13775 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 ){. goto pa
13776 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b ger_acquire_err;
13777 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
13778 6e 4d 61 78 3c 28 69 6e 74 29 70 67 6e 6f 20 7c nMax<(int)pgno |
13779 7c 20 4d 45 4d 44 42 20 7c 7c 20 6e 6f 43 6f 6e | MEMDB || noCon
1377a 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 69 66 tent ){. if
1377b 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6d ( pgno>pPager->m
1377c 78 50 67 6e 6f 20 29 7b 0a 09 72 63 20 3d 20 53 xPgno ){..rc = S
1377d 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 09 67 6f 74 QLITE_FULL;..got
1377e 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f o pager_acquire_
1377f 65 72 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 err;. }.
13780 20 20 20 69 66 28 20 6e 6f 43 6f 6e 74 65 6e 74 if( noContent
13781 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 ){. /* F
13782 61 69 6c 75 72 65 20 74 6f 20 73 65 74 20 74 68 ailure to set th
13783 65 20 62 69 74 73 20 69 6e 20 74 68 65 20 49 6e e bits in the In
13784 4a 6f 75 72 6e 61 6c 20 62 69 74 2d 76 65 63 74 Journal bit-vect
13785 6f 72 73 20 69 73 20 62 65 6e 69 67 6e 2e 0a 20 ors is benign..
13786 20 20 20 20 20 20 20 2a 2a 20 49 74 20 6d 65 72 ** It mer
13787 65 6c 79 20 6d 65 61 6e 73 20 74 68 61 74 20 77 ely means that w
13788 65 20 6d 69 67 68 74 20 64 6f 20 73 6f 6d 65 20 e might do some
13789 65 78 74 72 61 20 77 6f 72 6b 20 74 6f 20 6a 6f extra work to jo
1378a 75 72 6e 61 6c 20 61 20 0a 20 20 20 20 20 20 20 urnal a .
1378b 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64 6f ** page that do
1378c 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 es not need to b
1378d 65 20 6a 6f 75 72 6e 61 6c 65 64 2e 20 20 4e 65 e journaled. Ne
1378e 76 65 72 74 68 65 6c 65 73 73 2c 20 62 65 20 73 vertheless, be s
1378f 75 72 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 ure . **
13790 74 6f 20 74 65 73 74 20 74 68 65 20 63 61 73 65 to test the case
13791 20 77 68 65 72 65 20 61 20 6d 61 6c 6c 6f 63 20 where a malloc
13792 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 error occurs whi
13793 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 73 65 74 le trying to set
13794 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 62 . ** a b
13795 69 74 20 69 6e 20 61 20 62 69 74 20 76 65 63 74 it in a bit vect
13796 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 or.. */.
13797 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 sqlite3Be
13798 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
13799 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
1379a 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f gno<=pPager->dbO
1379b 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 rigSize ){.
1379c 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 TESTONLY( r
1379d 63 20 3d 20 29 20 73 71 6c 69 74 65 33 42 69 74 c = ) sqlite3Bit
1379e 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70 vecSet(pPager->p
1379f 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 6e 6f 29 InJournal, pgno)
137a0 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65 73 74 ;. test
137a1 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 case( rc==SQLITE
137a2 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 _NOMEM );.
137a3 20 20 7d 0a 20 20 20 20 20 20 20 20 54 45 53 54 }. TEST
137a4 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 61 64 64 ONLY( rc = ) add
137a5 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 ToSavepointBitve
137a6 63 73 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 cs(pPager, pgno)
137a7 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
137a8 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e se( rc==SQLITE_N
137a9 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 OMEM );.
137aa 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
137ab 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 Malloc();.
137ac 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d }else{. m
137ad 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 61 74 61 emset(pPg->pData
137ae 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 , 0, pPager->pag
137af 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a eSize);. }.
137b0 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 IOTRACE(("
137b1 5a 45 52 4f 20 25 70 20 25 64 5c 6e 22 2c 20 70 ZERO %p %d\n", p
137b2 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 Pager, pgno));.
137b3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
137b4 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 50 61 assert( pPg->pPa
137b5 67 65 72 3d 3d 70 50 61 67 65 72 20 29 3b 0a 20 ger==pPager );.
137b6 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 44 62 rc = readDb
137b7 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20 Page(pPg);.
137b8 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
137b9 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
137ba 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 to pager_acquire
137bb 5f 65 72 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 _err;. }.
137bc 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }.#ifdef SQLIT
137bd 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 E_CHECK_PAGES.
137be 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 pPg->pageHash
137bf 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 = pager_pagehash
137c0 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 (pPg);.#endif.
137c1 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }.. return SQLI
137c2 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 72 5f 61 63 TE_OK;..pager_ac
137c3 71 75 69 72 65 5f 65 72 72 3a 0a 20 20 61 73 73 quire_err:. ass
137c4 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ert( rc!=SQLITE_
137c5 4f 4b 20 29 3b 0a 20 20 69 66 28 20 70 50 67 20 OK );. if( pPg
137c6 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 ){. sqlite3Pc
137c7 61 63 68 65 44 72 6f 70 28 70 50 67 29 3b 0a 20 acheDrop(pPg);.
137c8 20 7d 0a 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b }. pagerUnlock
137c9 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 72 29 IfUnused(pPager)
137ca 3b 0a 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 30 ;.. *ppPage = 0
137cb 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
137cc 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 ../*.** Acquire
137cd 61 20 70 61 67 65 20 69 66 20 69 74 20 69 73 20 a page if it is
137ce 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 69 already in the i
137cf 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20 n-memory cache.
137d0 20 44 6f 0a 2a 2a 20 6e 6f 74 20 72 65 61 64 20 Do.** not read
137d1 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 64 69 the page from di
137d2 73 6b 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f sk. Return a po
137d3 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 inter to the pag
137d4 65 2c 0a 2a 2a 20 6f 72 20 30 20 69 66 20 74 68 e,.** or 0 if th
137d5 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e e page is not in
137d6 20 63 61 63 68 65 2e 20 41 6c 73 6f 2c 20 72 65 cache. Also, re
137d7 74 75 72 6e 20 30 20 69 66 20 74 68 65 20 0a 2a turn 0 if the .*
137d8 2a 20 70 61 67 65 72 20 69 73 20 69 6e 20 50 41 * pager is in PA
137d9 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 65 GER_UNLOCK state
137da 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
137db 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a ion is called,.*
137dc 2a 20 6f 72 20 69 66 20 74 68 65 20 70 61 67 65 * or if the page
137dd 72 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 r is in an error
137de 20 73 74 61 74 65 20 6f 74 68 65 72 20 74 68 61 state other tha
137df 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 0a 2a n SQLITE_FULL..*
137e0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 *.** See also sq
137e1 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 2e lite3PagerGet().
137e2 20 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 The difference
137e3 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 72 6f between this ro
137e4 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 20 73 71 6c utine.** and sql
137e5 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 69 ite3PagerGet() i
137e6 73 20 74 68 61 74 20 5f 67 65 74 28 29 20 77 69 s that _get() wi
137e7 6c 6c 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73 ll go to the dis
137e8 6b 20 61 6e 64 20 72 65 61 64 0a 2a 2a 20 69 6e k and read.** in
137e9 20 74 68 65 20 70 61 67 65 20 69 66 20 74 68 65 the page if the
137ea 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 page is not alr
137eb 65 61 64 79 20 69 6e 20 63 61 63 68 65 2e 20 20 eady in cache.
137ec 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 This routine.**
137ed 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 returns NULL if
137ee 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 the page is not
137ef 69 6e 20 63 61 63 68 65 20 6f 72 20 69 66 20 61 in cache or if a
137f0 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20 disk I/O error
137f1 0a 2a 2a 20 68 61 73 20 65 76 65 72 20 68 61 70 .** has ever hap
137f2 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 pened..*/.SQLITE
137f3 5f 50 52 49 56 41 54 45 20 44 62 50 61 67 65 20 _PRIVATE DbPage
137f4 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f *sqlite3PagerLoo
137f5 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 65 kup(Pager *pPage
137f6 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 r, Pgno pgno){.
137f7 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 30 3b PgHdr *pPg = 0;
137f8 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
137f9 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 r!=0 );. assert
137fa 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 20 20 61 ( pgno!=0 );. a
137fb 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 ssert( pPager->p
137fc 50 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 20 61 PCache!=0 );. a
137fd 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
137fe 74 61 74 65 20 3e 20 50 41 47 45 52 5f 55 4e 4c tate > PAGER_UNL
137ff 4f 43 4b 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 OCK );. sqlite3
13800 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67 PcacheFetch(pPag
13801 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e er->pPCache, pgn
13802 6f 2c 20 30 2c 20 26 70 50 67 29 3b 0a 20 20 72 o, 0, &pPg);. r
13803 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a 0a 2f 2a eturn pPg;.}../*
13804 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 70 61 .** Release a pa
13805 67 65 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a 2a ge reference..**
13806 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62 65 .** If the numbe
13807 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 r of references
13808 74 6f 20 74 68 65 20 70 61 67 65 20 64 72 6f 70 to the page drop
13809 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 to zero, then t
1380a 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 64 he.** page is ad
1380b 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55 20 6c ded to the LRU l
1380c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20 72 ist. When all r
1380d 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6c 6c eferences to all
1380e 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 72 65 pages.** are re
1380f 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62 61 leased, a rollba
13810 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68 ck occurs and th
13811 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 e lock on the da
13812 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 65 6d tabase is.** rem
13813 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f oved..*/.SQLITE_
13814 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
13815 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44 ite3PagerUnref(D
13816 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 69 bPage *pPg){. i
13817 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 50 61 f( pPg ){. Pa
13818 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 ger *pPager = pP
13819 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20 73 g->pPager;. s
1381a 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 qlite3PcacheRele
1381b 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20 70 61 ase(pPg);. pa
1381c 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 gerUnlockIfUnuse
1381d 64 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 7d d(pPager);. }.}
1381e 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d ../*.** If the m
1381f 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ain journal file
13820 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
13821 6e 20 6f 70 65 6e 65 64 2c 20 65 6e 73 75 72 65 n opened, ensure
13822 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 75 62 that the.** sub
13823 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 -journal file is
13824 20 6f 70 65 6e 20 74 6f 6f 2e 20 49 66 20 74 68 open too. If th
13825 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 e main journal i
13826 73 20 6e 6f 74 20 6f 70 65 6e 2c 0a 2a 2a 20 74 s not open,.** t
13827 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
13828 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 a no-op..**.** S
13829 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
1382a 72 6e 65 64 20 69 66 20 65 76 65 72 79 74 68 69 rned if everythi
1382b 6e 67 20 67 6f 65 73 20 61 63 63 6f 72 64 69 6e ng goes accordin
1382c 67 20 74 6f 20 70 6c 61 6e 2e 20 0a 2a 2a 20 41 g to plan. .** A
1382d 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 n SQLITE_IOERR_X
1382e 58 58 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 XX error code is
1382f 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 63 returned if a c
13830 61 6c 6c 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 all to .** sqlit
13831 65 33 4f 73 4f 70 65 6e 28 29 20 66 61 69 6c 73 e3OsOpen() fails
13832 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
13833 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 50 openSubJournal(P
13834 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
13835 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
13836 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 _OK;. if( isOpe
13837 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 n(pPager->jfd) &
13838 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 & !isOpen(pPager
13839 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 69 ->sjfd) ){. i
1383a 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
1383b 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
1383c 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 URNALMODE_MEMORY
1383d 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 75 62 6a || pPager->subj
1383e 49 6e 4d 65 6d 6f 72 79 20 29 7b 0a 20 20 20 20 InMemory ){.
1383f 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 sqlite3MemJour
13840 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e nalOpen(pPager->
13841 73 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 sjfd);. }else
13842 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 {. rc = pag
13843 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65 erOpentemp(pPage
13844 72 2c 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c r, pPager->sjfd,
13845 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 SQLITE_OPEN_SUB
13846 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 7d 0a JOURNAL);. }.
13847 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
13848 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
13849 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
1384a 64 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f d at the start o
1384b 66 20 65 76 65 72 79 20 77 72 69 74 65 20 74 72 f every write tr
1384c 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 ansaction..** Th
1384d 65 72 65 20 6d 75 73 74 20 61 6c 72 65 61 64 79 ere must already
1384e 20 62 65 20 61 20 52 45 53 45 52 56 45 44 20 6f be a RESERVED o
1384f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b r EXCLUSIVE lock
13850 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
13851 20 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e 20 74 .** file when t
13852 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
13853 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 70 65 alled..**.** Ope
13854 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
13855 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 50 61 le for pager pPa
13856 67 65 72 20 61 6e 64 20 77 72 69 74 65 20 61 20 ger and write a
13857 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a journal header.*
13858 2a 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f * to the start o
13859 66 20 69 74 2e 20 49 66 20 74 68 65 72 65 20 61 f it. If there a
1385a 72 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f re active savepo
1385b 69 6e 74 73 2c 20 6f 70 65 6e 20 74 68 65 20 73 ints, open the s
1385c 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 61 73 ub-journal.** as
1385d 20 77 65 6c 6c 2e 20 54 68 69 73 20 66 75 6e 63 well. This func
1385e 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 tion is only use
1385f 64 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e d when the journ
13860 61 6c 20 66 69 6c 65 20 69 73 20 62 65 69 6e 67 al file is being
13861 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 74 6f 20 77 .** opened to w
13862 72 69 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 rite a rollback
13863 6c 6f 67 20 66 6f 72 20 61 20 74 72 61 6e 73 61 log for a transa
13864 63 74 69 6f 6e 2e 20 49 74 20 69 73 20 6e 6f 74 ction. It is not
13865 20 75 73 65 64 20 0a 2a 2a 20 77 68 65 6e 20 6f used .** when o
13866 70 65 6e 69 6e 67 20 61 20 68 6f 74 20 6a 6f 75 pening a hot jou
13867 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 72 6f 6c rnal file to rol
13868 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a l it back..**.**
13869 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 If the journal
1386a 66 69 6c 65 20 69 73 20 61 6c 72 65 61 64 79 20 file is already
1386b 6f 70 65 6e 20 28 61 73 20 69 74 20 6d 61 79 20 open (as it may
1386c 62 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 be in exclusive
1386d 6d 6f 64 65 29 2c 0a 2a 2a 20 74 68 65 6e 20 74 mode),.** then t
1386e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6a 75 73 his function jus
1386f 74 20 77 72 69 74 65 73 20 61 20 6a 6f 75 72 6e t writes a journ
13870 61 6c 20 68 65 61 64 65 72 20 74 6f 20 74 68 65 al header to the
13871 20 73 74 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a start of the.**
13872 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 66 69 already open fi
13873 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 le. .**.** Wheth
13874 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f er or not the jo
13875 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 urnal file is op
13876 65 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 6e ened by this fun
13877 63 74 69 6f 6e 2c 20 74 68 65 0a 2a 2a 20 50 61 ction, the.** Pa
13878 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62 ger.pInJournal b
13879 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65 20 itvec structure
1387a 69 73 20 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a is allocated..**
1387b 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
1387c 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 E_OK if everythi
1387d 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c ng is successful
1387e 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 . Otherwise, ret
1387f 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e urn .** SQLITE_N
13880 4f 4d 45 4d 20 69 66 20 74 68 65 20 61 74 74 65 OMEM if the atte
13881 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 mpt to allocate
13882 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c Pager.pInJournal
13883 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 61 fails, or .** a
13884 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 n IO error code
13885 69 66 20 6f 70 65 6e 69 6e 67 20 6f 72 20 77 72 if opening or wr
13886 69 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 iting the journa
13887 6c 20 66 69 6c 65 20 66 61 69 6c 73 2e 0a 2a 2f l file fails..*/
13888 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
13889 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 50 r_open_journal(P
1388a 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
1388b 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1388c 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 _OK;
1388d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1388e 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
1388f 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f sqlite3_vfs * co
13890 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 67 65 nst pVfs = pPage
13891 72 2d 3e 70 56 66 73 3b 20 20 20 2f 2a 20 4c 6f r->pVfs; /* Lo
13892 63 61 6c 20 63 61 63 68 65 20 6f 66 20 76 66 73 cal cache of vfs
13893 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 0a 20 20 61 pointer */.. a
13894 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
13895 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 tate>=PAGER_RESE
13896 52 56 45 44 20 29 3b 0a 20 20 61 73 73 65 72 74 RVED );. assert
13897 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 ( pPager->useJou
13898 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 rnal );. assert
13899 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
1389a 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 lMode!=PAGER_JOU
1389b 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a RNALMODE_OFF );.
1389c 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
1389d 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 ->pInJournal==0
1389e 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20 61 6c );. . /* If al
1389f 72 65 61 64 79 20 69 6e 20 74 68 65 20 65 72 72 ready in the err
138a0 6f 72 20 73 74 61 74 65 2c 20 74 68 69 73 20 66 or state, this f
138a1 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d unction is a no-
138a2 6f 70 2e 20 20 42 75 74 20 6f 6e 0a 20 20 2a 2a op. But on. **
138a3 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c the other hand,
138a4 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
138a5 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 69 66 never called if
138a6 20 77 65 20 61 72 65 20 61 6c 72 65 61 64 79 20 we are already
138a7 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 in. ** an error
138a8 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20 69 66 28 state. */. if(
138a9 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 NEVER(pPager->e
138aa 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e rrCode) ) return
138ab 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
138ac 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 ;.. /* TODO: Is
138ad 20 69 74 20 72 65 61 6c 6c 79 20 70 6f 73 73 69 it really possi
138ae 62 6c 65 20 74 6f 20 67 65 74 20 68 65 72 65 20 ble to get here
138af 77 69 74 68 20 64 62 53 69 7a 65 56 61 6c 69 64 with dbSizeValid
138b0 3d 3d 30 3f 20 49 66 20 6e 6f 74 2c 0a 20 20 2a ==0? If not,. *
138b1 2a 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 50 61 * the call to Pa
138b2 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 63 gerPagecount() c
138b3 61 6e 20 62 65 20 72 65 6d 6f 76 65 64 2e 0a 20 an be removed..
138b4 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28 20 */. testcase(
138b5 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 pPager->dbSizeVa
138b6 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 lid==0 );. sqli
138b7 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e te3PagerPagecoun
138b8 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20 t(pPager, 0);..
138b9 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 pPager->pInJour
138ba 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 74 nal = sqlite3Bit
138bb 76 65 63 43 72 65 61 74 65 28 70 50 61 67 65 72 vecCreate(pPager
138bc 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 ->dbSize);. if(
138bd 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 pPager->pInJour
138be 6e 61 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 nal==0 ){. re
138bf 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
138c0 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 M;. }.. /* Ope
138c1 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
138c2 6c 65 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 le if it is not
138c3 61 6c 72 65 61 64 79 20 6f 70 65 6e 2e 20 2a 2f already open. */
138c4 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 . if( !isOpen(p
138c5 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 Pager->jfd) ){.
138c6 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a if( pPager->j
138c7 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 ournalMode==PAGE
138c8 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
138c9 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 20 73 71 MORY ){. sq
138ca 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f lite3MemJournalO
138cb 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
138cc 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
138cd 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61 const int fla
138ce 67 73 20 3d 20 20 20 20 20 20 20 20 20 20 20 20 gs =
138cf 20 20 20 20 20 20 20 2f 2a 20 56 46 53 20 66 6c /* VFS fl
138d0 61 67 73 20 74 6f 20 6f 70 65 6e 20 6a 6f 75 72 ags to open jour
138d1 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 nal file */.
138d2 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f SQLITE_OPEN_
138d3 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 READWRITE|SQLITE
138d4 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 0a 20 20 _OPEN_CREATE|.
138d5 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 74 (pPager->t
138d6 65 6d 70 46 69 6c 65 20 3f 20 0a 20 20 20 20 20 empFile ? .
138d7 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50 45 (SQLITE_OPE
138d8 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 7c N_DELETEONCLOSE|
138d9 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 SQLITE_OPEN_TEMP
138da 5f 4a 4f 55 52 4e 41 4c 29 3a 0a 20 20 20 20 20 _JOURNAL):.
138db 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50 45 (SQLITE_OPE
138dc 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 0a N_MAIN_JOURNAL).
138dd 20 20 20 20 20 20 20 20 29 3b 0a 23 69 66 64 65 );.#ifde
138de 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
138df 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 ATOMIC_WRITE.
138e0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a rc = sqlite3J
138e1 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 20 20 ournalOpen(.
138e2 20 20 20 20 20 20 70 56 66 73 2c 20 70 50 61 67 pVfs, pPag
138e3 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 er->zJournal, pP
138e4 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 ager->jfd, flags
138e5 2c 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65 , jrnlBufferSize
138e6 28 70 50 61 67 65 72 29 0a 20 20 20 20 20 20 29 (pPager). )
138e7 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 72 63 ;.#else. rc
138e8 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e = sqlite3OsOpen
138e9 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a (pVfs, pPager->z
138ea 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d Journal, pPager-
138eb 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 30 29 3b >jfd, flags, 0);
138ec 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 .#endif. }.
138ed 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 assert( rc!=SQ
138ee 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 LITE_OK || isOpe
138ef 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 n(pPager->jfd) )
138f0 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 57 72 69 ;. }... /* Wri
138f1 74 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 te the first jou
138f2 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20 74 rnal header to t
138f3 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
138f4 61 6e 64 20 6f 70 65 6e 20 0a 20 20 2a 2a 20 74 and open . ** t
138f5 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 he sub-journal i
138f6 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20 2a f necessary.. *
138f7 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 /. if( rc==SQLI
138f8 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 TE_OK ){. /*
138f9 54 4f 44 4f 3a 20 43 68 65 63 6b 20 69 66 20 61 TODO: Check if a
138fa 6c 6c 20 6f 66 20 74 68 65 73 65 20 61 72 65 20 ll of these are
138fb 72 65 61 6c 6c 79 20 72 65 71 75 69 72 65 64 2e really required.
138fc 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e */. pPager->
138fd 64 62 4f 72 69 67 53 69 7a 65 20 3d 20 70 50 61 dbOrigSize = pPa
138fe 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 ger->dbSize;.
138ff 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13900 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 Started = 0;.
13901 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
13902 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 c = 0;. pPage
13903 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 20 r->nRec = 0;.
13904 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13905 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 Off = 0;. pPa
13906 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d ger->setMaster =
13907 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 0;. pPager->
13908 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a journalHdr = 0;.
13909 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f rc = writeJo
1390a 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 urnalHdr(pPager)
1390b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d ;. }. if( rc==
1390c 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 SQLITE_OK && pPa
1390d 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 ger->nSavepoint
1390e 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e ){. rc = open
1390f 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 SubJournal(pPage
13910 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 r);. }.. if( r
13911 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
13912 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 sqlite3Bitve
13913 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d cDestroy(pPager-
13914 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 >pInJournal);.
13915 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 pPager->pInJou
13916 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 rnal = 0;. }.
13917 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
13918 0a 2a 2a 20 42 65 67 69 6e 20 61 20 77 72 69 74 .** Begin a writ
13919 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e e-transaction on
1391a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70 the specified p
1391b 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 66 20 ager object. If
1391c 61 20 0a 2a 2a 20 77 72 69 74 65 2d 74 72 61 6e a .** write-tran
1391d 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 saction has alre
1391e 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c ady been opened,
1391f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
13920 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a s a no-op..**.**
13921 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 20 61 If the exFlag a
13922 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65 rgument is false
13923 2c 20 74 68 65 6e 20 61 63 71 75 69 72 65 20 61 , then acquire a
13924 74 20 6c 65 61 73 74 20 61 20 52 45 53 45 52 56 t least a RESERV
13925 45 44 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 74 68 ED.** lock on th
13926 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
13927 20 49 66 20 65 78 46 6c 61 67 20 69 73 20 74 72 If exFlag is tr
13928 75 65 2c 20 74 68 65 6e 20 61 63 71 75 69 72 65 ue, then acquire
13929 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 61 6e 20 at least.** an
1392a 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 EXCLUSIVE lock.
1392b 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 If such a lock i
1392c 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 s already held,
1392d 6e 6f 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a 20 66 no locking .** f
1392e 75 6e 63 74 69 6f 6e 73 20 6e 65 65 64 20 62 65 unctions need be
1392f 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 called..**.** I
13930 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20 f this is not a
13931 74 65 6d 70 6f 72 61 72 79 20 6f 72 20 69 6e 2d temporary or in-
13932 6d 65 6d 6f 72 79 20 66 69 6c 65 20 61 6e 64 2c memory file and,
13933 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
13934 65 20 69 73 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 e is .** opened
13935 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62 65 if it has not be
13936 65 6e 20 61 6c 72 65 61 64 79 2e 20 46 6f 72 20 en already. For
13937 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 a temporary file
13938 2c 20 74 68 65 20 6f 70 65 6e 69 6e 67 20 0a 2a , the opening .*
13939 2a 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c * of the journal
1393a 20 66 69 6c 65 20 69 73 20 64 65 66 65 72 72 65 file is deferre
1393b 64 20 75 6e 74 69 6c 20 74 68 65 72 65 20 69 73 d until there is
1393c 20 61 6e 20 61 63 74 75 61 6c 20 6e 65 65 64 20 an actual need
1393d 74 6f 20 0a 2a 2a 20 77 72 69 74 65 20 74 6f 20 to .** write to
1393e 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 54 4f 44 the journal. TOD
1393f 4f 3a 20 57 68 79 20 68 61 6e 64 6c 65 20 74 65 O: Why handle te
13940 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 64 69 mporary files di
13941 66 66 65 72 65 6e 74 6c 79 3f 0a 2a 2a 0a 2a 2a fferently?.**.**
13942 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 If the journal
13943 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 28 file is opened (
13944 6f 72 20 69 66 20 69 74 20 69 73 20 61 6c 72 65 or if it is alre
13945 61 64 79 20 6f 70 65 6e 29 2c 20 74 68 65 6e 20 ady open), then
13946 61 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 a.** journal-hea
13947 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 74 der is written t
13948 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 69 o the start of i
13949 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 t..**.** If the
1394a 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 67 subjInMemory arg
1394b 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 ument is non-zer
1394c 6f 2c 20 74 68 65 6e 20 61 6e 79 20 73 75 62 2d o, then any sub-
1394d 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 65 64 0a 2a journal opened.*
1394e 2a 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 72 * within this tr
1394f 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 ansaction will b
13950 65 20 6f 70 65 6e 65 64 20 61 73 20 61 6e 20 69 e opened as an i
13951 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 2e 20 54 n-memory file. T
13952 68 69 73 0a 2a 2a 20 68 61 73 20 6e 6f 20 65 66 his.** has no ef
13953 66 65 63 74 20 69 66 20 74 68 65 20 73 75 62 2d fect if the sub-
13954 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 72 65 61 journal is alrea
13955 64 79 20 6f 70 65 6e 65 64 20 28 61 73 20 69 74 dy opened (as it
13956 20 6d 61 79 20 62 65 20 77 68 65 6e 0a 2a 2a 20 may be when.**
13957 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 running in exclu
13958 73 69 76 65 20 6d 6f 64 65 29 20 6f 72 20 69 66 sive mode) or if
13959 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
1395a 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 75 69 72 does not requir
1395b 65 20 61 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e e a.** sub-journ
1395c 61 6c 2e 20 49 66 20 74 68 65 20 73 75 62 6a 49 al. If the subjI
1395d 6e 4d 65 6d 6f 72 79 20 61 72 67 75 6d 65 6e 74 nMemory argument
1395e 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 61 is zero, then a
1395f 6e 79 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 73 ny required.** s
13960 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 69 6d ub-journal is im
13961 70 6c 65 6d 65 6e 74 65 64 20 69 6e 2d 6d 65 6d plemented in-mem
13962 6f 72 79 20 69 66 20 70 50 61 67 65 72 20 69 73 ory if pPager is
13963 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
13964 74 61 62 61 73 65 2c 20 0a 2a 2a 20 6f 72 20 75 tabase, .** or u
13965 73 69 6e 67 20 61 20 74 65 6d 70 6f 72 61 72 79 sing a temporary
13966 20 66 69 6c 65 20 6f 74 68 65 72 77 69 73 65 2e file otherwise.
13967 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
13968 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
13969 67 65 72 42 65 67 69 6e 28 50 61 67 65 72 20 2a gerBegin(Pager *
1396a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 78 46 6c pPager, int exFl
1396b 61 67 2c 20 69 6e 74 20 73 75 62 6a 49 6e 4d 65 ag, int subjInMe
1396c 6d 6f 72 79 29 7b 0a 20 20 69 6e 74 20 72 63 20 mory){. int rc
1396d 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 = SQLITE_OK;. a
1396e 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
1396f 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f tate!=PAGER_UNLO
13970 43 4b 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e CK );. pPager->
13971 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 3d 20 28 subjInMemory = (
13972 75 38 29 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 3b u8)subjInMemory;
13973 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 . if( pPager->s
13974 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 tate==PAGER_SHAR
13975 45 44 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 ED ){. assert
13976 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 ( pPager->pInJou
13977 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 61 rnal==0 );. a
13978 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 26 26 ssert( !MEMDB &&
13979 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 !pPager->tempFi
1397a 6c 65 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 62 le );.. /* Ob
1397b 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 tain a RESERVED
1397c 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
1397d 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 base file. If th
1397e 65 20 65 78 46 6c 61 67 20 70 61 72 61 6d 65 74 e exFlag paramet
1397f 65 72 0a 20 20 20 20 2a 2a 20 69 73 20 74 72 75 er. ** is tru
13980 65 2c 20 74 68 65 6e 20 69 6d 6d 65 64 69 61 74 e, then immediat
13981 65 6c 79 20 75 70 67 72 61 64 65 20 74 68 69 73 ely upgrade this
13982 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45 to an EXCLUSIVE
13983 20 6c 6f 63 6b 2e 20 54 68 65 0a 20 20 20 20 2a lock. The. *
13984 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 * busy-handler c
13985 61 6c 6c 62 61 63 6b 20 63 61 6e 20 62 65 20 75 allback can be u
13986 73 65 64 20 77 68 65 6e 20 75 70 67 72 61 64 69 sed when upgradi
13987 6e 67 20 74 6f 20 74 68 65 20 45 58 43 4c 55 53 ng to the EXCLUS
13988 49 56 45 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 2c IVE. ** lock,
13989 20 62 75 74 20 6e 6f 74 20 77 68 65 6e 20 6f 62 but not when ob
1398a 74 61 69 6e 69 6e 67 20 74 68 65 20 52 45 53 45 taining the RESE
1398b 52 56 45 44 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a RVED lock.. *
1398c 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
1398d 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d e3OsLock(pPager-
1398e 3e 66 64 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f >fd, RESERVED_LO
1398f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d CK);. if( rc=
13990 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
13991 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 pPager->stat
13992 65 20 3d 20 50 41 47 45 52 5f 52 45 53 45 52 56 e = PAGER_RESERV
13993 45 44 3b 0a 20 20 20 20 20 20 69 66 28 20 65 78 ED;. if( ex
13994 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 Flag ){.
13995 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f rc = pager_wait_
13996 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 on_lock(pPager,
13997 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b EXCLUSIVE_LOCK);
13998 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
13999 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 /* If the re
1399a 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 77 65 72 quired locks wer
1399b 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f e successfully o
1399c 62 74 61 69 6e 65 64 2c 20 6f 70 65 6e 20 74 68 btained, open th
1399d 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a e journal. **
1399e 20 66 69 6c 65 20 61 6e 64 20 77 72 69 74 65 20 file and write
1399f 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 the first journa
139a0 6c 2d 68 65 61 64 65 72 20 74 6f 20 69 74 2e 0a l-header to it..
139a1 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 */. if( r
139a2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
139a3 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
139a4 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode!=PAGER_JOURN
139a5 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 ALMODE_OFF ){.
139a6 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 6f rc = pager_o
139a7 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67 pen_journal(pPag
139a8 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c er);. }. }el
139a9 73 65 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 se if( isOpen(pP
139aa 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 70 50 ager->jfd) && pP
139ab 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
139ac 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 ==0 ){. /* Th
139ad 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 is happens when
139ae 74 68 65 20 70 61 67 65 72 20 77 61 73 20 69 6e the pager was in
139af 20 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 exclusive-acces
139b0 73 20 6d 6f 64 65 20 74 68 65 20 6c 61 73 74 0a s mode the last.
139b1 20 20 20 20 2a 2a 20 74 69 6d 65 20 61 20 28 72 ** time a (r
139b2 65 61 64 20 6f 72 20 77 72 69 74 65 29 20 74 72 ead or write) tr
139b3 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 73 75 ansaction was su
139b4 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6e 63 6c ccessfully concl
139b5 75 64 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 74 uded. ** by t
139b6 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 his connection.
139b7 49 6e 73 74 65 61 64 20 6f 66 20 64 65 6c 65 74 Instead of delet
139b8 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ing the journal
139b9 66 69 6c 65 20 69 74 20 77 61 73 20 0a 20 20 20 file it was .
139ba 20 2a 2a 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e ** kept open an
139bb 64 20 65 69 74 68 65 72 20 77 61 73 20 74 72 75 d either was tru
139bc 6e 63 61 74 65 64 20 74 6f 20 30 20 62 79 74 65 ncated to 0 byte
139bd 73 20 6f 72 20 69 74 73 20 68 65 61 64 65 72 20 s or its header
139be 77 61 73 0a 20 20 20 20 2a 2a 20 6f 76 65 72 77 was. ** overw
139bf 72 69 74 74 65 6e 20 77 69 74 68 20 7a 65 72 6f ritten with zero
139c0 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 s.. */. as
139c1 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 52 sert( pPager->nR
139c2 65 63 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 ec==0 );. ass
139c3 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 4f ert( pPager->dbO
139c4 72 69 67 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 20 rigSize==0 );.
139c5 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
139c6 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 ->pInJournal==0
139c7 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 );. rc = page
139c8 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 r_open_journal(p
139c9 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 50 Pager);. }.. P
139ca 41 47 45 52 54 52 41 43 45 28 28 22 54 52 41 4e AGERTRACE(("TRAN
139cb 53 41 43 54 49 4f 4e 20 25 64 5c 6e 22 2c 20 50 SACTION %d\n", P
139cc 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 AGERID(pPager)))
139cd 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 4f ;. assert( !isO
139ce 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
139cf 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 || pPager->jour
139d0 6e 61 6c 4f 66 66 3e 30 20 7c 7c 20 72 63 21 3d nalOff>0 || rc!=
139d1 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 SQLITE_OK );. i
139d2 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
139d3 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
139d4 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 !pPager->dbModif
139d5 69 65 64 20 29 3b 0a 20 20 20 20 2f 2a 20 49 67 ied );. /* Ig
139d6 6e 6f 72 65 20 61 6e 79 20 49 4f 20 65 72 72 6f nore any IO erro
139d7 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 77 69 r that occurs wi
139d8 74 68 69 6e 20 70 61 67 65 72 5f 65 6e 64 5f 74 thin pager_end_t
139d9 72 61 6e 73 61 63 74 69 6f 6e 28 29 2e 20 54 68 ransaction(). Th
139da 65 0a 20 20 20 20 2a 2a 20 70 75 72 70 6f 73 65 e. ** purpose
139db 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 20 69 73 of this call is
139dc 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 69 6e to reset the in
139dd 74 65 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 20 ternal state of
139de 74 68 65 20 70 61 67 65 72 0a 20 20 20 20 2a 2a the pager. **
139df 20 73 75 62 2d 73 79 73 74 65 6d 2e 20 49 74 20 sub-system. It
139e0 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 69 doesn't matter i
139e1 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 f the journal-fi
139e2 6c 65 20 69 73 20 6e 6f 74 20 70 72 6f 70 65 72 le is not proper
139e3 6c 79 0a 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69 ly. ** finali
139e4 7a 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e zed at this poin
139e5 74 20 28 73 69 6e 63 65 20 69 74 20 69 73 20 6e t (since it is n
139e6 6f 74 20 61 20 76 61 6c 69 64 20 6a 6f 75 72 6e ot a valid journ
139e7 61 6c 20 66 69 6c 65 20 61 6e 79 77 61 79 29 2e al file anyway).
139e8 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 61 67 65 . */. page
139e9 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f r_end_transactio
139ea 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 n(pPager, 0);.
139eb 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
139ec 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 73 ../*.** Mark a s
139ed 69 6e 67 6c 65 20 64 61 74 61 20 70 61 67 65 20 ingle data page
139ee 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68 as writeable. Th
139ef 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 e page is writte
139f0 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 6d n into the .** m
139f1 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 73 ain journal or s
139f2 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20 72 65 ub-journal as re
139f3 71 75 69 72 65 64 2e 20 49 66 20 74 68 65 20 70 quired. If the p
139f4 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69 age is written i
139f5 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 nto.** one of th
139f6 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 20 e journals, the
139f7 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 corresponding bi
139f8 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 t is set in the
139f9 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 .** Pager.pInJou
139fa 72 6e 61 6c 20 62 69 74 76 65 63 20 61 6e 64 20 rnal bitvec and
139fb 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 the PagerSavepoi
139fc 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 nt.pInSavepoint
139fd 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20 61 6e bitvecs.** of an
139fe 79 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 y open savepoint
139ff 73 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 s as appropriate
13a00 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
13a01 70 61 67 65 72 5f 77 72 69 74 65 28 50 67 48 64 pager_write(PgHd
13a02 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69 64 20 r *pPg){. void
13a03 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 *pData = pPg->pD
13a04 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 ata;. Pager *pP
13a05 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 ager = pPg->pPag
13a06 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 er;. int rc = S
13a07 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 QLITE_OK;.. /*
13a08 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
13a09 6e 6f 74 20 63 61 6c 6c 65 64 20 75 6e 6c 65 73 not called unles
13a0a 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 s a transaction
13a0b 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
13a0c 0a 20 20 2a 2a 20 73 74 61 72 74 65 64 2e 0a 20 . ** started..
13a0d 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
13a0e 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
13a0f 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 0a ER_RESERVED );..
13a10 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 /* If an error
13a11 20 68 61 73 20 62 65 65 6e 20 70 72 65 76 69 6f has been previo
13a12 75 73 6c 79 20 64 65 74 65 63 74 65 64 2c 20 77 usly detected, w
13a13 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a e should not be.
13a14 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69 ** calling thi
13a15 73 20 72 6f 75 74 69 6e 65 2e 20 20 52 65 70 65 s routine. Repe
13a16 61 74 20 74 68 65 20 65 72 72 6f 72 20 66 6f 72 at the error for
13a17 20 72 6f 62 75 73 74 6e 65 73 73 2e 0a 20 20 2a robustness.. *
13a18 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 /. if( NEVER(pP
13a19 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 ager->errCode) )
13a1a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
13a1b 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20 >errCode;.. /*
13a1c 48 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75 Higher-level rou
13a1d 74 69 6e 65 73 20 6e 65 76 65 72 20 63 61 6c 6c tines never call
13a1e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
13a1f 66 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f f database is no
13a20 74 0a 20 20 2a 2a 20 77 72 69 74 61 62 6c 65 2e t. ** writable.
13a21 20 20 42 75 74 20 63 68 65 63 6b 20 61 6e 79 77 But check anyw
13a22 61 79 2c 20 6a 75 73 74 20 66 6f 72 20 72 6f 62 ay, just for rob
13a23 75 73 74 6e 65 73 73 2e 20 2a 2f 0a 20 20 69 66 ustness. */. if
13a24 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e ( NEVER(pPager->
13a25 72 65 61 64 4f 6e 6c 79 29 20 29 20 72 65 74 75 readOnly) ) retu
13a26 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a rn SQLITE_PERM;.
13a27 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67 . assert( !pPag
13a28 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 29 3b er->setMaster );
13a29 0a 0a 20 20 43 48 45 43 4b 5f 50 41 47 45 28 70 .. CHECK_PAGE(p
13a2a 50 67 29 3b 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 Pg);.. /* Mark
13a2b 74 68 65 20 70 61 67 65 20 61 73 20 64 69 72 74 the page as dirt
13a2c 79 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20 y. If the page
13a2d 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
13a2e 20 77 72 69 74 74 65 6e 0a 20 20 2a 2a 20 74 6f written. ** to
13a2f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 68 65 the journal the
13a30 6e 20 77 65 20 63 61 6e 20 72 65 74 75 72 6e 20 n we can return
13a31 72 69 67 68 74 20 61 77 61 79 2e 0a 20 20 2a 2f right away.. */
13a32 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 . sqlite3Pcache
13a33 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a MakeDirty(pPg);.
13a34 20 20 69 66 28 20 70 61 67 65 49 6e 4a 6f 75 72 if( pageInJour
13a35 6e 61 6c 28 70 50 67 29 20 26 26 20 21 73 75 62 nal(pPg) && !sub
13a36 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50 jRequiresPage(pP
13a37 67 29 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 g) ){. pPager
13a38 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 31 ->dbModified = 1
13a39 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 20 20 ;. }else{..
13a3a 2f 2a 20 49 66 20 77 65 20 67 65 74 20 74 68 69 /* If we get thi
13a3b 73 20 66 61 72 2c 20 69 74 20 6d 65 61 6e 73 20 s far, it means
13a3c 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 65 that the page ne
13a3d 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 2a 2a eds to be. **
13a3e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
13a3f 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 transaction jour
13a40 6e 61 6c 20 6f 72 20 74 68 65 20 63 6b 65 63 6b nal or the ckeck
13a41 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 0a 20 20 point journal.
13a42 20 20 2a 2a 20 6f 72 20 62 6f 74 68 2e 0a 20 20 ** or both..
13a43 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 48 69 67 68 **. ** High
13a44 65 72 20 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 er level routine
13a45 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c s should have al
13a46 72 65 61 64 79 20 73 74 61 72 74 65 64 20 61 20 ready started a
13a47 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 20 transaction,.
13a48 20 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e 73 20 ** which means
13a49 74 68 65 79 20 68 61 76 65 20 61 63 71 75 69 72 they have acquir
13a4a 65 64 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 ed the necessary
13a4b 20 6c 6f 63 6b 73 20 61 6e 64 20 6f 70 65 6e 65 locks and opene
13a4c 64 0a 20 20 20 20 2a 2a 20 61 20 72 6f 6c 6c 62 d. ** a rollb
13a4d 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 6f ack journal. Do
13a4e 75 62 6c 65 2d 63 68 65 63 6b 20 74 6f 20 6d 61 uble-check to ma
13a4f 6b 65 73 20 73 75 72 65 20 74 68 69 73 20 69 73 kes sure this is
13a50 20 74 68 65 20 63 61 73 65 2e 0a 20 20 20 20 2a the case.. *
13a51 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
13a52 65 33 50 61 67 65 72 42 65 67 69 6e 28 70 50 61 e3PagerBegin(pPa
13a53 67 65 72 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e ger, 0, pPager->
13a54 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 3b 0a 20 subjInMemory);.
13a55 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 21 if( NEVER(rc!
13a56 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 =SQLITE_OK) ){.
13a57 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
13a58 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 69 }. if( !i
13a59 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
13a5a 64 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f d) && pPager->jo
13a5b 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 urnalMode!=PAGER
13a5c 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 _JOURNALMODE_OFF
13a5d 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
13a5e 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 ( pPager->useJou
13a5f 72 6e 61 6c 20 29 3b 0a 20 20 20 20 20 20 72 63 rnal );. rc
13a60 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f = pager_open_jo
13a61 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 urnal(pPager);.
13a62 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
13a63 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 ITE_OK ) return
13a64 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 rc;. }. pP
13a65 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
13a66 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 = 1;. . /*
13a67 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 The transaction
13a68 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78 69 73 journal now exis
13a69 74 73 20 61 6e 64 20 77 65 20 68 61 76 65 20 61 ts and we have a
13a6a 20 52 45 53 45 52 56 45 44 20 6f 72 20 61 6e 0a RESERVED or an.
13a6b 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45 ** EXCLUSIVE
13a6c 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d 61 69 lock on the mai
13a6d 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e n database file.
13a6e 20 20 57 72 69 74 65 20 74 68 65 20 63 75 72 72 Write the curr
13a6f 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 20 20 20 ent page to.
13a70 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 ** the transacti
13a71 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 20 69 74 on journal if it
13a72 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 61 6c is not there al
13a73 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 ready.. */.
13a74 20 20 69 66 28 20 21 70 61 67 65 49 6e 4a 6f 75 if( !pageInJou
13a75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 69 73 4f rnal(pPg) && isO
13a76 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
13a77 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 ){. if( pP
13a78 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d g->pgno<=pPager-
13a79 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 >dbOrigSize ){.
13a7a 20 20 20 20 20 20 20 75 33 32 20 63 6b 73 75 6d u32 cksum
13a7b 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a ;. char *
13a7c 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 20 20 20 pData2;..
13a7d 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 6e 65 /* We should ne
13a7e 76 65 72 20 77 72 69 74 65 20 74 6f 20 74 68 65 ver write to the
13a7f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 journal file th
13a80 65 20 70 61 67 65 20 74 68 61 74 0a 20 20 20 20 e page that.
13a81 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 ** contains
13a82 74 68 65 20 64 61 74 61 62 61 73 65 20 6c 6f 63 the database loc
13a83 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ks. The followi
13a84 6e 67 20 61 73 73 65 72 74 20 76 65 72 69 66 69 ng assert verifi
13a85 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 es. ** th
13a86 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e 20 2a 2f at we do not. */
13a87 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
13a88 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 41 47 45 pPg->pgno!=PAGE
13a89 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 R_MJ_PGNO(pPager
13a8a 29 20 29 3b 0a 20 20 20 20 20 20 20 20 43 4f 44 ) );. COD
13a8b 45 43 32 28 70 50 61 67 65 72 2c 20 70 44 61 74 EC2(pPager, pDat
13a8c 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 37 2c a, pPg->pgno, 7,
13a8d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
13a8e 4f 4d 45 4d 2c 20 70 44 61 74 61 32 29 3b 0a 20 OMEM, pData2);.
13a8f 20 20 20 20 20 20 20 63 6b 73 75 6d 20 3d 20 70 cksum = p
13a90 61 67 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 ager_cksum(pPage
13a91 72 2c 20 28 75 38 2a 29 70 44 61 74 61 32 29 3b r, (u8*)pData2);
13a92 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 . rc = wr
13a93 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 ite32bits(pPager
13a94 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a ->jfd, pPager->j
13a95 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e ournalOff, pPg->
13a96 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 pgno);. i
13a97 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
13a98 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
13a99 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
13a9a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 e(pPager->jfd, p
13a9b 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 Data2, pPager->p
13a9c 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 ageSize,.
13a9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13a9e 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a pPager->j
13a9f 6f 75 72 6e 61 6c 4f 66 66 20 2b 20 34 29 3b 0a ournalOff + 4);.
13aa0 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 pPager
13aa1 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 ->journalOff +=
13aa2 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
13aa3 2b 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 +4;. }.
13aa4 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
13aa5 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
13aa6 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 rc = write3
13aa7 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 2bits(pPager->jf
13aa8 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e d, pPager->journ
13aa9 61 6c 4f 66 66 2c 20 63 6b 73 75 6d 29 3b 0a 20 alOff, cksum);.
13aaa 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d pPager-
13aab 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 >journalOff += 4
13aac 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
13aad 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 4f IOTRACE(("JO
13aae 55 54 20 25 70 20 25 64 20 25 6c 6c 64 20 25 64 UT %p %d %lld %d
13aaf 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 \n", pPager, pPg
13ab0 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 ->pgno, .
13ab1 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 pPager
13ab2 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 ->journalOff, pP
13ab3 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 29 ager->pageSize))
13ab4 3b 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 5f ;. PAGER_
13ab5 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 INCR(sqlite3_pag
13ab6 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 29 er_writej_count)
13ab7 3b 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54 ;. PAGERT
13ab8 52 41 43 45 28 28 22 4a 4f 55 52 4e 41 4c 20 25 RACE(("JOURNAL %
13ab9 64 20 70 61 67 65 20 25 64 20 6e 65 65 64 53 79 d page %d needSy
13aba 6e 63 3d 25 64 20 68 61 73 68 28 25 30 38 78 29 nc=%d hash(%08x)
13abb 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
13abc 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 PAGERID(pPager
13abd 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 ), pPg->pgno, .
13abe 20 20 20 20 20 20 20 20 20 20 20 20 28 28 70 50 ((pP
13abf 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
13ac0 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20 EED_SYNC)?1:0),
13ac1 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 pager_pagehash(p
13ac2 50 67 29 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 Pg)));..
13ac3 2f 2a 20 45 76 65 6e 20 69 66 20 61 6e 20 49 4f /* Even if an IO
13ac4 20 6f 72 20 64 69 73 6b 66 75 6c 6c 20 65 72 72 or diskfull err
13ac5 6f 72 20 6f 63 63 75 72 72 65 64 20 77 68 69 6c or occurred whil
13ac6 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 74 68 e journalling th
13ac7 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 e. ** pag
13ac8 65 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b 20 61 e in the block a
13ac9 62 6f 76 65 2c 20 73 65 74 20 74 68 65 20 6e 65 bove, set the ne
13aca 65 64 2d 73 79 6e 63 20 66 6c 61 67 20 66 6f 72 ed-sync flag for
13acb 20 74 68 65 20 70 61 67 65 2e 0a 20 20 20 20 20 the page..
13acc 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c ** Otherwise,
13acd 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 when the transa
13ace 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 ction is rolled
13acf 62 61 63 6b 2c 20 74 68 65 20 6c 6f 67 69 63 20 back, the logic
13ad0 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6c in. ** pl
13ad1 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 ayback_one_page(
13ad2 29 20 77 69 6c 6c 20 74 68 69 6e 6b 20 74 68 61 ) will think tha
13ad3 74 20 74 68 65 20 70 61 67 65 20 6e 65 65 64 73 t the page needs
13ad4 20 74 6f 20 62 65 20 72 65 73 74 6f 72 65 64 0a to be restored.
13ad5 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 ** in th
13ad6 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
13ad7 20 41 6e 64 20 69 66 20 61 6e 20 49 4f 20 65 72 And if an IO er
13ad8 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 ror occurs while
13ad9 20 64 6f 69 6e 67 20 73 6f 2c 0a 20 20 20 20 20 doing so,.
13ada 20 20 20 2a 2a 20 74 68 65 6e 20 63 6f 72 72 75 ** then corru
13adb 70 74 69 6f 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77 ption may follow
13adc 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
13add 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 if( !pPager
13ade 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 ->noSync ){.
13adf 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 pPg->flags
13ae0 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 |= PGHDR_NEED_S
13ae1 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 20 70 YNC;. p
13ae2 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 Pager->needSync
13ae3 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a = 1;. }..
13ae4 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 72 /* An er
13ae5 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 ror has occurred
13ae6 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 writing to the
13ae7 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 journal file. Th
13ae8 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 e . ** tr
13ae9 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 ansaction will b
13aea 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 79 e rolled back by
13aeb 20 74 68 65 20 6c 61 79 65 72 20 61 62 6f 76 65 the layer above
13aec 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
13aed 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
13aee 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
13aef 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
13af0 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 }..
13af1 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 2b 2b pPager->nRec++
13af2 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
13af3 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 ( pPager->pInJou
13af4 72 6e 61 6c 21 3d 30 20 29 3b 0a 20 20 20 20 20 rnal!=0 );.
13af5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
13af6 69 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d itvecSet(pPager-
13af7 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 >pInJournal, pPg
13af8 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 ->pgno);.
13af9 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 testcase( rc==S
13afa 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 QLITE_NOMEM );.
13afb 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 assert( r
13afc 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c==SQLITE_OK ||
13afd 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
13afe 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 7c );. rc |
13aff 3d 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 = addToSavepoint
13b00 42 69 74 76 65 63 73 28 70 50 61 67 65 72 2c 20 Bitvecs(pPager,
13b01 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 pPg->pgno);.
13b02 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
13b03 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
13b04 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
13b05 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 QLITE_NOMEM );.
13b06 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
13b07 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rc;. }.
13b08 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
13b09 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e if( !pPager->
13b0a 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 26 journalStarted &
13b0b 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e & !pPager->noSyn
13b0c 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 c ){. p
13b0d 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 Pg->flags |= PGH
13b0e 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 DR_NEED_SYNC;.
13b0f 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
13b10 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 needSync = 1;.
13b11 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
13b12 50 41 47 45 52 54 52 41 43 45 28 28 22 41 50 50 PAGERTRACE(("APP
13b13 45 4e 44 20 25 64 20 70 61 67 65 20 25 64 20 6e END %d page %d n
13b14 65 65 64 53 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20 eedSync=%d\n",.
13b15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 P
13b16 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 AGERID(pPager),
13b17 70 50 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 pPg->pgno,.
13b18 20 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d ((pPg-
13b19 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
13b1a 44 5f 53 59 4e 43 29 3f 31 3a 30 29 29 29 3b 0a D_SYNC)?1:0)));.
13b1b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
13b1c 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 . /* If the s
13b1d 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c tatement journal
13b1e 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 is open and the
13b1f 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 page is not in
13b20 69 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 it,. ** then
13b21 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e write the curren
13b22 74 20 70 61 67 65 20 74 6f 20 74 68 65 20 73 74 t page to the st
13b23 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e atement journal.
13b24 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 20 20 Note that.
13b25 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 ** the statement
13b26 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 journal format
13b27 64 69 66 66 65 72 73 20 66 72 6f 6d 20 74 68 65 differs from the
13b28 20 73 74 61 6e 64 61 72 64 20 6a 6f 75 72 6e 61 standard journa
13b29 6c 20 66 6f 72 6d 61 74 0a 20 20 20 20 2a 2a 20 l format. **
13b2a 69 6e 20 74 68 61 74 20 69 74 20 6f 6d 69 74 73 in that it omits
13b2b 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20 61 the checksums a
13b2c 6e 64 20 74 68 65 20 68 65 61 64 65 72 2e 0a 20 nd the header..
13b2d 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 75 */. if( su
13b2e 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70 bjRequiresPage(p
13b2f 50 67 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 Pg) ){. rc
13b30 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 = subjournalPage
13b31 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d (pPg);. }. }
13b32 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 .. /* Update th
13b33 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 e database size
13b34 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f and return.. */
13b35 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
13b36 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f r->state>=PAGER_
13b37 53 48 41 52 45 44 20 29 3b 0a 20 20 69 66 28 20 SHARED );. if(
13b38 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3c 70 pPager->dbSize<p
13b39 50 67 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 Pg->pgno ){.
13b3a 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d pPager->dbSize =
13b3b 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 7d 0a pPg->pgno;. }.
13b3c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
13b3d 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 64 61 74 /*.** Mark a dat
13b3e 61 20 70 61 67 65 20 61 73 20 77 72 69 74 65 61 a page as writea
13b3f 62 6c 65 2e 20 54 68 69 73 20 72 6f 75 74 69 6e ble. This routin
13b40 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 e must be called
13b41 20 62 65 66 6f 72 65 20 0a 2a 2a 20 6d 61 6b 69 before .** maki
13b42 6e 67 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20 ng changes to a
13b43 70 61 67 65 2e 20 54 68 65 20 63 61 6c 6c 65 72 page. The caller
13b44 20 6d 75 73 74 20 63 68 65 63 6b 20 74 68 65 20 must check the
13b45 72 65 74 75 72 6e 20 76 61 6c 75 65 20 0a 2a 2a return value .**
13b46 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f of this functio
13b47 6e 20 61 6e 64 20 62 65 20 63 61 72 65 66 75 6c n and be careful
13b48 20 6e 6f 74 20 74 6f 20 63 68 61 6e 67 65 20 61 not to change a
13b49 6e 79 20 70 61 67 65 20 64 61 74 61 20 75 6e 6c ny page data unl
13b4a 65 73 73 20 0a 2a 2a 20 74 68 69 73 20 72 6f 75 ess .** this rou
13b4b 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c tine returns SQL
13b4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 ITE_OK..**.** Th
13b4d 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 e difference bet
13b4e 77 65 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 ween this functi
13b4f 6f 6e 20 61 6e 64 20 70 61 67 65 72 5f 77 72 69 on and pager_wri
13b50 74 65 28 29 20 69 73 20 74 68 61 74 20 74 68 69 te() is that thi
13b51 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c s.** function al
13b52 73 6f 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 so deals with th
13b53 65 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 77 e special case w
13b54 68 65 72 65 20 32 20 6f 72 20 6d 6f 72 65 20 70 here 2 or more p
13b55 61 67 65 73 0a 2a 2a 20 66 69 74 20 6f 6e 20 61 ages.** fit on a
13b56 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 single disk sec
13b57 74 6f 72 2e 20 49 6e 20 74 68 69 73 20 63 61 73 tor. In this cas
13b58 65 20 61 6c 6c 20 63 6f 2d 72 65 73 69 64 65 6e e all co-residen
13b59 74 20 70 61 67 65 73 0a 2a 2a 20 6d 75 73 74 20 t pages.** must
13b5a 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 have been writte
13b5b 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c n to the journal
13b5c 20 66 69 6c 65 20 62 65 66 6f 72 65 20 72 65 74 file before ret
13b5d 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 urning..**.** If
13b5e 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
13b5f 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f , SQLITE_NOMEM o
13b60 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f r an IO error co
13b61 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a de is returned.*
13b62 2a 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 * as appropriate
13b63 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c . Otherwise, SQL
13b64 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 ITE_OK..*/.SQLIT
13b65 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
13b66 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
13b67 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 29 DbPage *pDbPage)
13b68 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
13b69 49 54 45 5f 4f 4b 3b 0a 0a 20 20 50 67 48 64 72 ITE_OK;.. PgHdr
13b6a 20 2a 70 50 67 20 3d 20 70 44 62 50 61 67 65 3b *pPg = pDbPage;
13b6b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
13b6c 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
13b6d 20 20 50 67 6e 6f 20 6e 50 61 67 65 50 65 72 53 Pgno nPagePerS
13b6e 65 63 74 6f 72 20 3d 20 28 70 50 61 67 65 72 2d ector = (pPager-
13b6f 3e 73 65 63 74 6f 72 53 69 7a 65 2f 70 50 61 67 >sectorSize/pPag
13b70 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a er->pageSize);..
13b71 20 20 69 66 28 20 6e 50 61 67 65 50 65 72 53 65 if( nPagePerSe
13b72 63 74 6f 72 3e 31 20 29 7b 0a 20 20 20 20 50 67 ctor>1 ){. Pg
13b73 6e 6f 20 6e 50 61 67 65 43 6f 75 6e 74 3b 20 20 no nPageCount;
13b74 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
13b75 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
13b76 20 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c in database fil
13b77 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 70 67 e */. Pgno pg
13b78 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
13b79 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 /* First page
13b7a 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 of the sector p
13b7b 50 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e Pg is located on
13b7c 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61 . */. int nPa
13b7d 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
13b7e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
13b7f 70 61 67 65 73 20 73 74 61 72 74 69 6e 67 20 61 pages starting a
13b80 74 20 70 67 31 20 74 6f 20 6a 6f 75 72 6e 61 6c t pg1 to journal
13b81 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 */. int ii;
13b82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13b83 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
13b84 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 65 r */. int nee
13b85 64 53 79 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 dSync = 0;
13b86 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e /* True if an
13b87 79 20 70 61 67 65 20 68 61 73 20 50 47 48 44 52 y page has PGHDR
13b88 5f 4e 45 45 44 5f 53 59 4e 43 20 2a 2f 0a 0a 20 _NEED_SYNC */..
13b89 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 64 6f /* Set the do
13b8a 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20 74 6f 20 NotSync flag to
13b8b 31 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 1. This is becau
13b8c 73 65 20 77 65 20 63 61 6e 6e 6f 74 20 61 6c 6c se we cannot all
13b8d 6f 77 20 61 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 ow a journal.
13b8e 20 2a 2a 20 68 65 61 64 65 72 20 74 6f 20 62 65 ** header to be
13b8f 20 77 72 69 74 74 65 6e 20 62 65 74 77 65 65 6e written between
13b90 20 74 68 65 20 70 61 67 65 73 20 6a 6f 75 72 6e the pages journ
13b91 61 6c 65 64 20 62 79 20 74 68 69 73 20 66 75 6e aled by this fun
13b92 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 ction.. */.
13b93 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 assert( !MEMDB
13b94 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
13b95 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e pPager->doNotSyn
13b96 63 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 67 c==0 );. pPag
13b97 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d 20 er->doNotSync =
13b98 31 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 1;.. /* This
13b99 74 72 69 63 6b 20 61 73 73 75 6d 65 73 20 74 68 trick assumes th
13b9a 61 74 20 62 6f 74 68 20 74 68 65 20 70 61 67 65 at both the page
13b9b 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 -size and sector
13b9c 2d 73 69 7a 65 20 61 72 65 0a 20 20 20 20 2a 2a -size are. **
13b9d 20 61 6e 20 69 6e 74 65 67 65 72 20 70 6f 77 65 an integer powe
13b9e 72 20 6f 66 20 32 2e 20 49 74 20 73 65 74 73 20 r of 2. It sets
13b9f 76 61 72 69 61 62 6c 65 20 70 67 31 20 74 6f 20 variable pg1 to
13ba0 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 0a 20 the identifier.
13ba1 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 ** of the fir
13ba2 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 73 st page of the s
13ba3 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63 ector pPg is loc
13ba4 61 74 65 64 20 6f 6e 2e 0a 20 20 20 20 2a 2f 0a ated on.. */.
13ba5 20 20 20 20 70 67 31 20 3d 20 28 28 70 50 67 2d pg1 = ((pPg-
13ba6 3e 70 67 6e 6f 2d 31 29 20 26 20 7e 28 6e 50 61 >pgno-1) & ~(nPa
13ba7 67 65 50 65 72 53 65 63 74 6f 72 2d 31 29 29 20 gePerSector-1))
13ba8 2b 20 31 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 + 1;.. sqlite
13ba9 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
13baa 70 50 61 67 65 72 2c 20 28 69 6e 74 20 2a 29 26 pPager, (int *)&
13bab 6e 50 61 67 65 43 6f 75 6e 74 29 3b 0a 20 20 20 nPageCount);.
13bac 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3e 6e if( pPg->pgno>n
13bad 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 PageCount ){.
13bae 20 20 20 6e 50 61 67 65 20 3d 20 28 70 50 67 2d nPage = (pPg-
13baf 3e 70 67 6e 6f 20 2d 20 70 67 31 29 2b 31 3b 0a >pgno - pg1)+1;.
13bb0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 }else if( (p
13bb1 67 31 2b 6e 50 61 67 65 50 65 72 53 65 63 74 6f g1+nPagePerSecto
13bb2 72 2d 31 29 3e 6e 50 61 67 65 43 6f 75 6e 74 20 r-1)>nPageCount
13bb3 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d ){. nPage =
13bb4 20 6e 50 61 67 65 43 6f 75 6e 74 2b 31 2d 70 67 nPageCount+1-pg
13bb5 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 1;. }else{.
13bb6 20 20 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 nPage = nPag
13bb7 65 50 65 72 53 65 63 74 6f 72 3b 0a 20 20 20 20 ePerSector;.
13bb8 7d 0a 20 20 20 20 61 73 73 65 72 74 28 6e 50 61 }. assert(nPa
13bb9 67 65 3e 30 29 3b 0a 20 20 20 20 61 73 73 65 72 ge>0);. asser
13bba 74 28 70 67 31 3c 3d 70 50 67 2d 3e 70 67 6e 6f t(pg1<=pPg->pgno
13bbb 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 28 70 );. assert((p
13bbc 67 31 2b 6e 50 61 67 65 29 3e 70 50 67 2d 3e 70 g1+nPage)>pPg->p
13bbd 67 6e 6f 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 gno);.. for(i
13bbe 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65 20 26 26 i=0; ii<nPage &&
13bbf 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc==SQLITE_OK;
13bc0 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 50 67 6e ii++){. Pgn
13bc1 6f 20 70 67 20 3d 20 70 67 31 2b 69 69 3b 0a 20 o pg = pg1+ii;.
13bc2 20 20 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 PgHdr *pPag
13bc3 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 67 3d e;. if( pg=
13bc4 3d 70 50 67 2d 3e 70 67 6e 6f 20 7c 7c 20 21 73 =pPg->pgno || !s
13bc5 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 qlite3BitvecTest
13bc6 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 (pPager->pInJour
13bc7 6e 61 6c 2c 20 70 67 29 20 29 7b 0a 20 20 20 20 nal, pg) ){.
13bc8 20 20 20 20 69 66 28 20 70 67 21 3d 50 41 47 45 if( pg!=PAGE
13bc9 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 R_MJ_PGNO(pPager
13bca 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 ) ){. r
13bcb 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
13bcc 47 65 74 28 70 50 61 67 65 72 2c 20 70 67 2c 20 Get(pPager, pg,
13bcd 26 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 &pPage);.
13bce 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
13bcf 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
13bd0 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 rc = pager_w
13bd1 72 69 74 65 28 70 50 61 67 65 29 3b 0a 20 20 20 rite(pPage);.
13bd2 20 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 if( pPa
13bd3 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f ge->flags&PGHDR_
13bd4 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 NEED_SYNC ){.
13bd5 20 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 needS
13bd6 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 ync = 1;.
13bd7 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50 assert(pP
13bd8 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b ager->needSync);
13bd9 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 . }.
13bda 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
13bdb 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 e3PagerUnref(pPa
13bdc 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d ge);. }
13bdd 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
13bde 20 7d 65 6c 73 65 20 69 66 28 20 28 70 50 61 67 }else if( (pPag
13bdf 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 e = pager_lookup
13be0 28 70 50 61 67 65 72 2c 20 70 67 29 29 21 3d 30 (pPager, pg))!=0
13be1 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
13be2 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 pPage->flags&PGH
13be3 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a DR_NEED_SYNC ){.
13be4 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 needSy
13be5 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 nc = 1;.
13be6 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
13be7 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 3PagerUnref(pPag
13be8 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 e);. }.
13be9 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
13bea 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
13beb 20 66 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72 flag is set for
13bec 20 61 6e 79 20 6f 66 20 74 68 65 20 6e 50 61 67 any of the nPag
13bed 65 20 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 e pages . **
13bee 73 74 61 72 74 69 6e 67 20 61 74 20 70 67 31 2c starting at pg1,
13bef 20 74 68 65 6e 20 69 74 20 6e 65 65 64 73 20 74 then it needs t
13bf0 6f 20 62 65 20 73 65 74 20 66 6f 72 20 61 6c 6c o be set for all
13bf1 20 6f 66 20 74 68 65 6d 2e 20 42 65 63 61 75 73 of them. Becaus
13bf2 65 0a 20 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 e. ** writing
13bf3 20 74 6f 20 61 6e 79 20 6f 66 20 74 68 65 73 65 to any of these
13bf4 20 6e 50 61 67 65 20 70 61 67 65 73 20 6d 61 79 nPage pages may
13bf5 20 64 61 6d 61 67 65 20 74 68 65 20 6f 74 68 65 damage the othe
13bf6 72 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 6a rs, the. ** j
13bf7 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 ournal file must
13bf8 20 63 6f 6e 74 61 69 6e 20 73 79 6e 63 28 29 65 contain sync()e
13bf9 64 20 63 6f 70 69 65 73 20 6f 66 20 61 6c 6c 20 d copies of all
13bfa 6f 66 20 74 68 65 6d 0a 20 20 20 20 2a 2a 20 62 of them. ** b
13bfb 65 66 6f 72 65 20 61 6e 79 20 6f 66 20 74 68 65 efore any of the
13bfc 6d 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e m can be written
13bfd 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 out to the data
13bfe 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a base file.. *
13bff 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 /. if( rc==SQ
13c00 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 65 65 64 53 LITE_OK && needS
13c01 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ync ){. ass
13c02 65 72 74 28 20 21 4d 45 4d 44 42 20 26 26 20 70 ert( !MEMDB && p
13c03 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 Pager->noSync==0
13c04 20 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 69 );. for(ii
13c05 3d 30 3b 20 69 69 3c 6e 50 61 67 65 3b 20 69 69 =0; ii<nPage; ii
13c06 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 50 67 48 ++){. PgH
13c07 64 72 20 2a 70 50 61 67 65 20 3d 20 70 61 67 65 dr *pPage = page
13c08 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c r_lookup(pPager,
13c09 20 70 67 31 2b 69 69 29 3b 0a 20 20 20 20 20 20 pg1+ii);.
13c0a 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 if( pPage ){.
13c0b 20 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e pPage->
13c0c 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e flags |= PGHDR_N
13c0d 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 EED_SYNC;.
13c0e 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
13c0f 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 Unref(pPage);.
13c10 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
13c11 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50 61 assert(pPa
13c12 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a ger->needSync);.
13c13 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 }.. asser
13c14 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 t( pPager->doNot
13c15 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70 Sync==1 );. p
13c16 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 Pager->doNotSync
13c17 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 0;. }else{.
13c18 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 rc = pager_wr
13c19 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20 ite(pDbPage);.
13c1a 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
13c1b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 ../*.** Return T
13c1c 52 55 45 20 69 66 20 74 68 65 20 70 61 67 65 20 RUE if the page
13c1d 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 given in the arg
13c1e 75 6d 65 6e 74 20 77 61 73 20 70 72 65 76 69 6f ument was previo
13c1f 75 73 6c 79 20 70 61 73 73 65 64 0a 2a 2a 20 74 usly passed.** t
13c20 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 o sqlite3PagerWr
13c21 69 74 65 28 29 2e 20 20 49 6e 20 6f 74 68 65 72 ite(). In other
13c22 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54 words, return T
13c23 52 55 45 20 69 66 20 69 74 20 69 73 20 6f 6b 0a RUE if it is ok.
13c24 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 ** to change the
13c25 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 content of the
13c26 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 page..*/.#ifndef
13c27 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 NDEBUG.SQLITE_P
13c28 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
13c29 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
13c2a 6c 65 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b le(DbPage *pPg){
13c2b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 66 . return pPg->f
13c2c 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 lags&PGHDR_DIRTY
13c2d 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
13c2e 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 * A call to this
13c2f 20 72 6f 75 74 69 6e 65 20 74 65 6c 6c 73 20 74 routine tells t
13c30 68 65 20 70 61 67 65 72 20 74 68 61 74 20 69 74 he pager that it
13c31 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 is not necessar
13c32 79 20 74 6f 0a 2a 2a 20 77 72 69 74 65 20 74 68 y to.** write th
13c33 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e e information on
13c34 20 70 61 67 65 20 70 50 67 20 62 61 63 6b 20 74 page pPg back t
13c35 6f 20 74 68 65 20 64 69 73 6b 2c 20 65 76 65 6e o the disk, even
13c36 20 74 68 6f 75 67 68 0a 2a 2a 20 74 68 61 74 20 though.** that
13c37 70 61 67 65 20 6d 69 67 68 74 20 62 65 20 6d 61 page might be ma
13c38 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 20 rked as dirty.
13c39 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f This happens, fo
13c3a 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a r example, when.
13c3b 2a 2a 20 74 68 65 20 70 61 67 65 20 68 61 73 20 ** the page has
13c3c 62 65 65 6e 20 61 64 64 65 64 20 61 73 20 61 20 been added as a
13c3d 6c 65 61 66 20 6f 66 20 74 68 65 20 66 72 65 65 leaf of the free
13c3e 6c 69 73 74 20 61 6e 64 20 73 6f 20 69 74 73 0a list and so its.
13c3f 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e 6f 20 6c 6f ** content no lo
13c40 6e 67 65 72 20 6d 61 74 74 65 72 73 2e 0a 2a 2a nger matters..**
13c41 0a 2a 2a 20 54 68 65 20 6f 76 65 72 6c 79 69 6e .** The overlyin
13c42 67 20 73 6f 66 74 77 61 72 65 20 6c 61 79 65 72 g software layer
13c43 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 calls this rout
13c44 69 6e 65 20 77 68 65 6e 20 61 6c 6c 20 6f 66 20 ine when all of
13c45 74 68 65 20 64 61 74 61 0a 2a 2a 20 6f 6e 20 74 the data.** on t
13c46 68 65 20 67 69 76 65 6e 20 70 61 67 65 20 69 73 he given page is
13c47 20 75 6e 75 73 65 64 2e 20 54 68 65 20 70 61 67 unused. The pag
13c48 65 72 20 6d 61 72 6b 73 20 74 68 65 20 70 61 67 er marks the pag
13c49 65 20 61 73 20 63 6c 65 61 6e 20 73 6f 0a 2a 2a e as clean so.**
13c4a 20 74 68 61 74 20 69 74 20 64 6f 65 73 20 6e 6f that it does no
13c4b 74 20 67 65 74 20 77 72 69 74 74 65 6e 20 74 6f t get written to
13c4c 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 54 65 73 disk..**.** Tes
13c4d 74 73 20 73 68 6f 77 20 74 68 61 74 20 74 68 69 ts show that thi
13c4e 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63 s optimization c
13c4f 61 6e 20 71 75 61 64 72 75 70 6c 65 20 74 68 65 an quadruple the
13c50 20 73 70 65 65 64 20 6f 66 20 6c 61 72 67 65 20 speed of large
13c51 0a 2a 2a 20 44 45 4c 45 54 45 20 6f 70 65 72 61 .** DELETE opera
13c52 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 tions..*/.SQLITE
13c53 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
13c54 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 lite3PagerDontWr
13c55 69 74 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b ite(PgHdr *pPg){
13c56 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
13c57 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
13c58 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67 if( (pPg->flag
13c59 73 26 50 47 48 44 52 5f 44 49 52 54 59 29 20 26 s&PGHDR_DIRTY) &
13c5a 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 & pPager->nSavep
13c5b 6f 69 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 50 oint==0 ){. P
13c5c 41 47 45 52 54 52 41 43 45 28 28 22 44 4f 4e 54 AGERTRACE(("DONT
13c5d 5f 57 52 49 54 45 20 70 61 67 65 20 25 64 20 6f _WRITE page %d o
13c5e 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d 3e 70 67 f %d\n", pPg->pg
13c5f 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 no, PAGERID(pPag
13c60 65 72 29 29 29 3b 0a 20 20 20 20 49 4f 54 52 41 er)));. IOTRA
13c61 43 45 28 28 22 43 4c 45 41 4e 20 25 70 20 25 64 CE(("CLEAN %p %d
13c62 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 \n", pPager, pPg
13c63 2d 3e 70 67 6e 6f 29 29 0a 20 20 20 20 70 50 67 ->pgno)). pPg
13c64 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 ->flags |= PGHDR
13c65 5f 44 4f 4e 54 5f 57 52 49 54 45 3b 0a 23 69 66 _DONT_WRITE;.#if
13c66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b def SQLITE_CHECK
13c67 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e _PAGES. pPg->
13c68 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 pageHash = pager
13c69 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a _pagehash(pPg);.
13c6a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a #endif. }.}../*
13c6b 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
13c6c 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 69 6e is called to in
13c6d 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 crement the valu
13c6e 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
13c6f 65 20 66 69 6c 65 20 0a 2a 2a 20 63 68 61 6e 67 e file .** chang
13c70 65 2d 63 6f 75 6e 74 65 72 2c 20 73 74 6f 72 65 e-counter, store
13c71 64 20 61 73 20 61 20 34 2d 62 79 74 65 20 62 69 d as a 4-byte bi
13c72 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 g-endian integer
13c73 20 73 74 61 72 74 69 6e 67 20 61 74 20 0a 2a 2a starting at .**
13c74 20 62 79 74 65 20 6f 66 66 73 65 74 20 32 34 20 byte offset 24
13c75 6f 66 20 74 68 65 20 70 61 67 65 72 20 66 69 6c of the pager fil
13c76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
13c77 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66 6c 61 isDirectMode fla
13c78 67 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 g is zero, then
13c79 74 68 69 73 20 69 73 20 64 6f 6e 65 20 62 79 20 this is done by
13c7a 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 calling .** sqli
13c7b 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 te3PagerWrite()
13c7c 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 on page 1, then
13c7d 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 63 6f modifying the co
13c7e 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a ntents of the.**
13c7f 20 70 61 67 65 20 64 61 74 61 2e 20 49 6e 20 74 page data. In t
13c80 68 69 73 20 63 61 73 65 20 74 68 65 20 66 69 6c his case the fil
13c81 65 20 77 69 6c 6c 20 62 65 20 75 70 64 61 74 65 e will be update
13c82 64 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 d when the curre
13c83 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f nt.** transactio
13c84 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a n is committed..
13c85 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 44 69 72 65 **.** The isDire
13c86 63 74 4d 6f 64 65 20 66 6c 61 67 20 6d 61 79 20 ctMode flag may
13c87 6f 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f only be non-zero
13c88 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 if the library
13c89 77 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 was compiled.**
13c8a 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f with the SQLITE_
13c8b 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 ENABLE_ATOMIC_WR
13c8c 49 54 45 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 ITE macro define
13c8d 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c d. In this case,
13c8e 0a 2a 2a 20 69 66 20 69 73 44 69 72 65 63 74 20 .** if isDirect
13c8f 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
13c90 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
13c91 69 6c 65 20 69 73 20 75 70 64 61 74 65 64 20 64 ile is updated d
13c92 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 77 72 irectly.** by wr
13c93 69 74 69 6e 67 20 61 6e 20 75 70 64 61 74 65 64 iting an updated
13c94 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 67 65 version of page
13c95 20 31 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20 1 using a call
13c96 74 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 to the .** sqlit
13c97 65 33 4f 73 57 72 69 74 65 28 29 20 66 75 6e 63 e3OsWrite() func
13c98 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
13c99 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 int pager_incr_c
13c9a 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61 67 hangecounter(Pag
13c9b 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
13c9c 69 73 44 69 72 65 63 74 4d 6f 64 65 29 7b 0a 20 isDirectMode){.
13c9d 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
13c9e 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 65 63 6c 61 _OK;.. /* Decla
13c9f 72 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a re and initializ
13ca0 65 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 e constant integ
13ca1 65 72 20 27 69 73 44 69 72 65 63 74 27 2e 20 49 er 'isDirect'. I
13ca2 66 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f 6d 69 f the. ** atomi
13ca3 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 c-write optimiza
13ca4 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20 tion is enabled
13ca5 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2c 20 74 in this build, t
13ca6 68 65 6e 20 69 73 44 69 72 65 63 74 0a 20 20 2a hen isDirect. *
13ca7 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 * is initialized
13ca8 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61 to the value pa
13ca9 73 73 65 64 20 61 73 20 74 68 65 20 69 73 44 69 ssed as the isDi
13caa 72 65 63 74 4d 6f 64 65 20 70 61 72 61 6d 65 74 rectMode paramet
13cab 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 er. ** to this
13cac 66 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 function. Otherw
13cad 69 73 65 2c 20 69 74 20 69 73 20 61 6c 77 61 79 ise, it is alway
13cae 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 20 s set to zero..
13caf 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69 64 65 **. ** The ide
13cb0 61 20 69 73 20 74 68 61 74 20 69 66 20 74 68 65 a is that if the
13cb1 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 atomic-write op
13cb2 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 6e 6f timization is no
13cb3 74 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64 20 61 t. ** enabled a
13cb4 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 t compile time,
13cb5 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e the compiler can
13cb6 20 6f 6d 69 74 20 74 68 65 20 74 65 73 74 73 20 omit the tests
13cb7 6f 66 0a 20 20 2a 2a 20 27 69 73 44 69 72 65 63 of. ** 'isDirec
13cb8 74 27 20 62 65 6c 6f 77 2c 20 61 73 20 77 65 6c t' below, as wel
13cb9 6c 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 20 65 l as the block e
13cba 6e 63 6c 6f 73 65 64 20 69 6e 20 74 68 65 0a 20 nclosed in the.
13cbb 20 2a 2a 20 22 69 66 28 20 69 73 44 69 72 65 63 ** "if( isDirec
13cbc 74 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a t )" condition..
13cbd 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c */.#ifndef SQL
13cbe 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 ITE_ENABLE_ATOMI
13cbf 43 5f 57 52 49 54 45 0a 23 20 64 65 66 69 6e 65 C_WRITE.# define
13cc0 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 30 0a 20 DIRECT_MODE 0.
13cc1 20 61 73 73 65 72 74 28 20 69 73 44 69 72 65 63 assert( isDirec
13cc2 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 55 4e tMode==0 );. UN
13cc3 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 USED_PARAMETER(i
13cc4 73 44 69 72 65 63 74 4d 6f 64 65 29 3b 0a 23 65 sDirectMode);.#e
13cc5 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 44 49 52 lse.# define DIR
13cc6 45 43 54 5f 4d 4f 44 45 20 69 73 44 69 72 65 63 ECT_MODE isDirec
13cc7 74 4d 6f 64 65 0a 23 65 6e 64 69 66 0a 0a 20 20 tMode.#endif..
13cc8 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
13cc9 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 state>=PAGER_RES
13cca 45 52 56 45 44 20 29 3b 0a 20 20 69 66 28 20 21 ERVED );. if( !
13ccb 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f pPager->changeCo
13ccc 75 6e 74 44 6f 6e 65 20 26 26 20 41 4c 57 41 59 untDone && ALWAY
13ccd 53 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 S(pPager->dbSize
13cce 3e 30 29 20 29 7b 0a 20 20 20 20 50 67 48 64 72 >0) ){. PgHdr
13ccf 20 2a 70 50 67 48 64 72 3b 20 20 20 20 20 20 20 *pPgHdr;
13cd0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65 /* Refe
13cd1 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 rence to page 1
13cd2 2a 2f 0a 20 20 20 20 75 33 32 20 63 68 61 6e 67 */. u32 chang
13cd3 65 5f 63 6f 75 6e 74 65 72 3b 20 20 20 20 20 20 e_counter;
13cd4 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 /* Initial
13cd5 76 61 6c 75 65 20 6f 66 20 63 68 61 6e 67 65 2d value of change-
13cd6 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 20 2a 2f counter field */
13cd7 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 .. assert( !p
13cd8 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
13cd9 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 && isOpen(pPager
13cda 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 20 20 2f 2a ->fd) );.. /*
13cdb 20 4f 70 65 6e 20 70 61 67 65 20 31 20 6f 66 20 Open page 1 of
13cdc 74 68 65 20 66 69 6c 65 20 66 6f 72 20 77 72 69 the file for wri
13cdd 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20 ting. */. rc
13cde 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
13cdf 74 28 70 50 61 67 65 72 2c 20 31 2c 20 26 70 50 t(pPager, 1, &pP
13ce0 67 48 64 72 29 3b 0a 20 20 20 20 61 73 73 65 72 gHdr);. asser
13ce1 74 28 20 70 50 67 48 64 72 3d 3d 30 20 7c 7c 20 t( pPgHdr==0 ||
13ce2 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc==SQLITE_OK );
13ce3 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65 .. /* If page
13ce4 20 6f 6e 65 20 77 61 73 20 66 65 74 63 68 65 64 one was fetched
13ce5 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 61 successfully, a
13ce6 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e nd this function
13ce7 20 69 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 6f is not. ** o
13ce8 70 65 72 61 74 69 6e 67 20 69 6e 20 64 69 72 65 perating in dire
13ce9 63 74 2d 6d 6f 64 65 2c 20 6d 61 6b 65 20 70 61 ct-mode, make pa
13cea 67 65 20 31 20 77 72 69 74 61 62 6c 65 2e 20 20 ge 1 writable.
13ceb 57 68 65 6e 20 6e 6f 74 20 69 6e 20 0a 20 20 20 When not in .
13cec 20 2a 2a 20 64 69 72 65 63 74 20 6d 6f 64 65 2c ** direct mode,
13ced 20 70 61 67 65 20 31 20 69 73 20 61 6c 77 61 79 page 1 is alway
13cee 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 20 s held in cache
13cef 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20 50 61 and hence the Pa
13cf0 67 65 72 47 65 74 28 29 0a 20 20 20 20 2a 2a 20 gerGet(). **
13cf1 61 62 6f 76 65 20 69 73 20 61 6c 77 61 79 73 20 above is always
13cf2 73 75 63 63 65 73 73 66 75 6c 20 2d 20 68 65 6e successful - hen
13cf3 63 65 20 74 68 65 20 41 4c 57 41 59 53 20 6f 6e ce the ALWAYS on
13cf4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 2e 0a rc==SQLITE_OK..
13cf5 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 */. if( !
13cf6 44 49 52 45 43 54 5f 4d 4f 44 45 20 26 26 20 41 DIRECT_MODE && A
13cf7 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c 49 54 45 LWAYS(rc==SQLITE
13cf8 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 63 _OK) ){. rc
13cf9 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
13cfa 72 69 74 65 28 70 50 67 48 64 72 29 3b 0a 20 20 rite(pPgHdr);.
13cfb 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d }.. if( rc=
13cfc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
13cfd 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 /* Increment
13cfe 20 74 68 65 20 76 61 6c 75 65 20 6a 75 73 74 20 the value just
13cff 72 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 69 read and write i
13d00 74 20 62 61 63 6b 20 74 6f 20 62 79 74 65 20 32 t back to byte 2
13d01 34 2e 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 6e 4. */. chan
13d02 67 65 5f 63 6f 75 6e 74 65 72 20 3d 20 73 71 6c ge_counter = sql
13d03 69 74 65 33 47 65 74 34 62 79 74 65 28 28 75 38 ite3Get4byte((u8
13d04 2a 29 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 *)pPager->dbFile
13d05 56 65 72 73 29 3b 0a 20 20 20 20 20 20 63 68 61 Vers);. cha
13d06 6e 67 65 5f 63 6f 75 6e 74 65 72 2b 2b 3b 0a 20 nge_counter++;.
13d07 20 20 20 20 20 70 75 74 33 32 62 69 74 73 28 28 put32bits((
13d08 28 63 68 61 72 2a 29 70 50 67 48 64 72 2d 3e 70 (char*)pPgHdr->p
13d09 44 61 74 61 29 2b 32 34 2c 20 63 68 61 6e 67 65 Data)+24, change
13d0a 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a 20 20 20 20 _counter);..
13d0b 20 20 2f 2a 20 49 66 20 72 75 6e 6e 69 6e 67 20 /* If running
13d0c 69 6e 20 64 69 72 65 63 74 20 6d 6f 64 65 2c 20 in direct mode,
13d0d 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e write the conten
13d0e 74 73 20 6f 66 20 70 61 67 65 20 31 20 74 6f 20 ts of page 1 to
13d0f 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 the file. */.
13d10 20 20 20 69 66 28 20 44 49 52 45 43 54 5f 4d 4f if( DIRECT_MO
13d11 44 45 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f DE ){. co
13d12 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 20 3d nst void *zBuf =
13d13 20 70 50 67 48 64 72 2d 3e 70 44 61 74 61 3b 0a pPgHdr->pData;.
13d14 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
13d15 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 pPager->dbFileSi
13d16 7a 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 ze>0 );.
13d17 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
13d18 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ite(pPager->fd,
13d19 7a 42 75 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 zBuf, pPager->pa
13d1a 67 65 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 geSize, 0);.
13d1b 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
13d1c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
13d1d 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 pPager->chang
13d1e 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a eCountDone = 1;.
13d1f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
13d20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
13d21 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 Pager->changeCou
13d22 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20 ntDone = 1;.
13d23 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f }. }.. /
13d24 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 70 61 * Release the pa
13d25 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20 2a 2f ge reference. */
13d26 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 . sqlite3Page
13d27 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a rUnref(pPgHdr);.
13d28 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
13d29 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 .}../*.** Sync t
13d2a 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 74 6f he pager file to
13d2b 20 64 69 73 6b 2e 20 54 68 69 73 20 69 73 20 61 disk. This is a
13d2c 20 6e 6f 2d 6f 70 20 66 6f 72 20 69 6e 2d 6d 65 no-op for in-me
13d2d 6d 6f 72 79 20 66 69 6c 65 73 0a 2a 2a 20 6f 72 mory files.** or
13d2e 20 70 61 67 65 73 20 77 69 74 68 20 74 68 65 20 pages with the
13d2f 50 61 67 65 72 2e 6e 6f 53 79 6e 63 20 66 6c 61 Pager.noSync fla
13d30 67 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 g set..**.** If
13d31 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 63 successful, or c
13d32 61 6c 6c 65 64 20 6f 6e 20 61 20 70 61 67 65 72 alled on a pager
13d33 20 66 6f 72 20 77 68 69 63 68 20 69 74 20 69 73 for which it is
13d34 20 61 20 6e 6f 2d 6f 70 2c 20 74 68 69 73 0a 2a a no-op, this.*
13d35 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 * function retur
13d36 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 ns SQLITE_OK. Ot
13d37 68 65 72 77 69 73 65 2c 20 61 6e 20 49 4f 20 65 herwise, an IO e
13d38 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
13d39 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 urned..*/.SQLITE
13d3a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
13d3b 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 50 61 ite3PagerSync(Pa
13d3c 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
13d3d 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
13d3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13d3f 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
13d40 6f 64 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ode */. assert(
13d41 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 69 66 28 !MEMDB );. if(
13d42 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 pPager->noSync
13d43 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
13d44 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a TE_OK;. }else{.
13d45 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
13d46 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 OsSync(pPager->f
13d47 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f d, pPager->sync_
13d48 66 6c 61 67 73 29 3b 0a 20 20 7d 0a 20 20 72 65 flags);. }. re
13d49 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
13d4a 2a 20 53 79 6e 63 20 74 68 65 20 64 61 74 61 62 * Sync the datab
13d4b 61 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65 ase file for the
13d4c 20 70 61 67 65 72 20 70 50 61 67 65 72 2e 20 7a pager pPager. z
13d4d 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f Master points to
13d4e 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 the name.** of
13d4f 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
13d50 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c file that shoul
13d51 64 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 d be written int
13d52 6f 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c o the individual
13d53 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 .** journal file
13d54 2e 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 62 65 . zMaster may be
13d55 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 20 NULL, which is
13d56 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 6e interpreted as n
13d57 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 o master.** jour
13d58 6e 61 6c 20 28 61 20 73 69 6e 67 6c 65 20 64 61 nal (a single da
13d59 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 tabase transacti
13d5a 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 on)..**.** This
13d5b 72 6f 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20 routine ensures
13d5c 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 that:.**.** *
13d5d 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c The database fil
13d5e 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
13d5f 20 69 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a 20 is updated,.**
13d60 20 20 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 * the journal
13d61 69 73 20 73 79 6e 63 65 64 20 28 75 6e 6c 65 73 is synced (unles
13d62 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 s the atomic-wri
13d63 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 te optimization
13d64 69 73 20 75 73 65 64 29 2c 0a 2a 2a 20 20 20 2a is used),.** *
13d65 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 all dirty pages
13d66 20 61 72 65 20 77 72 69 74 74 65 6e 20 74 6f 20 are written to
13d67 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
13d68 65 2c 20 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 e, .** * the d
13d69 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 atabase file is
13d6a 74 72 75 6e 63 61 74 65 64 20 28 69 66 20 72 65 truncated (if re
13d6b 71 75 69 72 65 64 29 2c 20 61 6e 64 0a 2a 2a 20 quired), and.**
13d6c 20 20 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 * the database
13d6d 20 66 69 6c 65 20 73 79 6e 63 65 64 2e 20 0a 2a file synced. .*
13d6e 2a 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 74 68 *.** The only th
13d6f 69 6e 67 20 74 68 61 74 20 72 65 6d 61 69 6e 73 ing that remains
13d70 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 to commit the t
13d71 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 74 6f ransaction is to
13d72 20 66 69 6e 61 6c 69 7a 65 20 0a 2a 2a 20 28 64 finalize .** (d
13d73 65 6c 65 74 65 2c 20 74 72 75 6e 63 61 74 65 20 elete, truncate
13d74 6f 72 20 7a 65 72 6f 20 74 68 65 20 66 69 72 73 or zero the firs
13d75 74 20 70 61 72 74 20 6f 66 29 20 74 68 65 20 6a t part of) the j
13d76 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20 ournal file (or
13d77 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 6d .** delete the m
13d78 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
13d79 6c 65 20 69 66 20 73 70 65 63 69 66 69 65 64 29 le if specified)
13d7a 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
13d7b 74 20 69 66 20 7a 4d 61 73 74 65 72 3d 3d 4e 55 t if zMaster==NU
13d7c 4c 4c 2c 20 74 68 69 73 20 64 6f 65 73 20 6e 6f LL, this does no
13d7d 74 20 6f 76 65 72 77 72 69 74 65 20 61 20 70 72 t overwrite a pr
13d7e 65 76 69 6f 75 73 20 76 61 6c 75 65 0a 2a 2a 20 evious value.**
13d7f 70 61 73 73 65 64 20 74 6f 20 61 6e 20 73 71 6c passed to an sql
13d80 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 ite3PagerCommitP
13d81 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 2e 0a haseOne() call..
13d82 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6e **.** If the fin
13d83 61 6c 20 70 61 72 61 6d 65 74 65 72 20 2d 20 6e al parameter - n
13d84 6f 53 79 6e 63 20 2d 20 69 73 20 74 72 75 65 2c oSync - is true,
13d85 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 then the databa
13d86 73 65 20 66 69 6c 65 20 69 74 73 65 6c 66 0a 2a se file itself.*
13d87 2a 20 69 73 20 6e 6f 74 20 73 79 6e 63 65 64 2e * is not synced.
13d88 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 The caller must
13d89 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 call sqlite3Pag
13d8a 65 72 53 79 6e 63 28 29 20 64 69 72 65 63 74 6c erSync() directl
13d8b 79 20 74 6f 0a 2a 2a 20 73 79 6e 63 20 74 68 65 y to.** sync the
13d8c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 database file b
13d8d 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 43 6f efore calling Co
13d8e 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 74 mmitPhaseTwo() t
13d8f 6f 20 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20 o delete the.**
13d90 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 journal file in
13d91 74 68 69 73 20 63 61 73 65 2e 0a 2a 2f 0a 53 51 this case..*/.SQ
13d92 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
13d93 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d sqlite3PagerCom
13d94 6d 69 74 50 68 61 73 65 4f 6e 65 28 0a 20 20 50 mitPhaseOne(. P
13d95 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 ager *pPager,
13d96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
13d97 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a * Pager object *
13d98 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
13d99 7a 4d 61 73 74 65 72 2c 20 20 20 20 20 20 20 20 zMaster,
13d9a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e 55 /* If not NU
13d9b 4c 4c 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a LL, the master j
13d9c 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 ournal name */.
13d9d 20 69 6e 74 20 6e 6f 53 79 6e 63 20 20 20 20 20 int noSync
13d9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13d9f 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 /* True to omit
13da0 20 74 68 65 20 78 53 79 6e 63 20 6f 6e 20 74 68 the xSync on th
13da1 65 20 64 62 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a e db file */.){.
13da2 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
13da3 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 E_OK;
13da4 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
13da5 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 64 62 */.. /* The db
13da6 4f 72 69 67 53 69 7a 65 20 69 73 20 6e 65 76 65 OrigSize is neve
13da7 72 20 73 65 74 20 69 66 20 6a 6f 75 72 6e 61 6c r set if journal
13da8 5f 6d 6f 64 65 3d 4f 46 46 20 2a 2f 0a 20 20 61 _mode=OFF */. a
13da9 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a ssert( pPager->j
13daa 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 ournalMode!=PAGE
13dab 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 R_JOURNALMODE_OF
13dac 46 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 4f F || pPager->dbO
13dad 72 69 67 53 69 7a 65 3d 3d 30 20 29 3b 0a 0a 20 rigSize==0 );..
13dae 20 2f 2a 20 49 66 20 61 20 70 72 69 6f 72 20 65 /* If a prior e
13daf 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 74 rror occurred, t
13db0 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 his routine shou
13db1 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 ld not be called
13db2 2e 20 20 52 4f 4c 4c 42 41 43 4b 0a 20 20 2a 2a . ROLLBACK. **
13db3 20 69 73 20 74 68 65 20 61 70 70 72 6f 70 72 69 is the appropri
13db4 61 74 65 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 ate response to
13db5 61 6e 20 65 72 72 6f 72 2c 20 6e 6f 74 20 43 4f an error, not CO
13db6 4d 4d 49 54 2e 20 20 47 75 61 72 64 20 61 67 61 MMIT. Guard aga
13db7 69 6e 73 74 0a 20 20 2a 2a 20 63 6f 64 69 6e 67 inst. ** coding
13db8 20 65 72 72 6f 72 73 20 62 79 20 72 65 70 65 61 errors by repea
13db9 74 69 6e 67 20 74 68 65 20 70 72 69 6f 72 20 65 ting the prior e
13dba 72 72 6f 72 2e 20 2a 2f 0a 20 20 69 66 28 20 4e rror. */. if( N
13dbb 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72 EVER(pPager->err
13dbc 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e 20 70 Code) ) return p
13dbd 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a Pager->errCode;.
13dbe 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 . PAGERTRACE(("
13dbf 44 41 54 41 42 41 53 45 20 53 59 4e 43 3a 20 46 DATABASE SYNC: F
13dc0 69 6c 65 3d 25 73 20 7a 4d 61 73 74 65 72 3d 25 ile=%s zMaster=%
13dc1 73 20 6e 53 69 7a 65 3d 25 64 5c 6e 22 2c 20 0a s nSize=%d\n", .
13dc2 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 7a 46 pPager->zF
13dc3 69 6c 65 6e 61 6d 65 2c 20 7a 4d 61 73 74 65 72 ilename, zMaster
13dc4 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 , pPager->dbSize
13dc5 29 29 3b 0a 0a 20 20 69 66 28 20 4d 45 4d 44 42 ));.. if( MEMDB
13dc6 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f && pPager->dbMo
13dc7 64 69 66 69 65 64 20 29 7b 0a 20 20 20 20 2f 2a dified ){. /*
13dc8 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 If this is an i
13dc9 6e 2d 6d 65 6d 6f 72 79 20 64 62 2c 20 6f 72 20 n-memory db, or
13dca 6e 6f 20 70 61 67 65 73 20 68 61 76 65 20 62 65 no pages have be
13dcb 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 6f en written to, o
13dcc 72 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 66 75 r this. ** fu
13dcd 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 nction has alrea
13dce 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20 dy been called,
13dcf 69 74 20 69 73 20 6d 6f 73 74 6c 79 20 61 20 6e it is mostly a n
13dd0 6f 2d 6f 70 2e 20 20 48 6f 77 65 76 65 72 2c 20 o-op. However,
13dd1 61 6e 79 0a 20 20 20 20 2a 2a 20 62 61 63 6b 75 any. ** backu
13dd2 70 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6e 65 p in progress ne
13dd3 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 61 72 eds to be restar
13dd4 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ted.. */.
13dd5 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 sqlite3BackupRes
13dd6 74 61 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61 tart(pPager->pBa
13dd7 63 6b 75 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 ckup);. }else i
13dd8 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 f( pPager->state
13dd9 21 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 26 !=PAGER_SYNCED &
13dda 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 & pPager->dbModi
13ddb 66 69 65 64 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 fied ){.. /*
13ddc 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c The following bl
13ddd 6f 63 6b 20 75 70 64 61 74 65 73 20 74 68 65 20 ock updates the
13dde 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2e 20 change-counter.
13ddf 45 78 61 63 74 6c 79 20 68 6f 77 20 69 74 0a 20 Exactly how it.
13de0 20 20 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 20 ** does this
13de1 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 65 74 68 depends on wheth
13de2 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74 er or not the at
13de3 6f 6d 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69 omic-update opti
13de4 6d 69 7a 61 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 mization. **
13de5 77 61 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63 was enabled at c
13de6 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 61 6e 64 ompile time, and
13de7 20 69 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 if this transac
13de8 74 69 6f 6e 20 6d 65 65 74 73 20 74 68 65 20 0a tion meets the .
13de9 20 20 20 20 2a 2a 20 72 75 6e 74 69 6d 65 20 63 ** runtime c
13dea 72 69 74 65 72 69 61 20 74 6f 20 75 73 65 20 74 riteria to use t
13deb 68 65 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 20 he operation: .
13dec 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 **. **
13ded 2a 20 54 68 65 20 66 69 6c 65 2d 73 79 73 74 65 * The file-syste
13dee 6d 20 73 75 70 70 6f 72 74 73 20 74 68 65 20 61 m supports the a
13def 74 6f 6d 69 63 2d 77 72 69 74 65 20 70 72 6f 70 tomic-write prop
13df0 65 72 74 79 20 66 6f 72 0a 20 20 20 20 2a 2a 20 erty for. **
13df1 20 20 20 20 20 62 6c 6f 63 6b 73 20 6f 66 20 73 blocks of s
13df2 69 7a 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 61 ize page-size, a
13df3 6e 64 20 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 nd . ** *
13df4 54 68 69 73 20 63 6f 6d 6d 69 74 20 69 73 20 6e This commit is n
13df5 6f 74 20 70 61 72 74 20 6f 66 20 61 20 6d 75 6c ot part of a mul
13df6 74 69 2d 66 69 6c 65 20 74 72 61 6e 73 61 63 74 ti-file transact
13df7 69 6f 6e 2c 20 61 6e 64 0a 20 20 20 20 2a 2a 20 ion, and. **
13df8 20 20 20 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 * Exactly one
13df9 20 70 61 67 65 20 68 61 73 20 62 65 65 6e 20 6d page has been m
13dfa 6f 64 69 66 69 65 64 20 61 6e 64 20 73 74 6f 72 odified and stor
13dfb 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c e in the journal
13dfc 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 file.. **.
13dfd 20 20 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 ** If the opti
13dfe 6d 69 7a 61 74 69 6f 6e 20 77 61 73 20 6e 6f 74 mization was not
13dff 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 enabled at comp
13e00 69 6c 65 20 74 69 6d 65 2c 20 74 68 65 6e 20 74 ile time, then t
13e01 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f he. ** pager_
13e02 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 incr_changecount
13e03 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 er() function is
13e04 20 63 61 6c 6c 65 64 20 74 6f 20 75 70 64 61 74 called to updat
13e05 65 20 74 68 65 20 63 68 61 6e 67 65 0a 20 20 20 e the change.
13e06 20 2a 2a 20 63 6f 75 6e 74 65 72 20 69 6e 20 27 ** counter in '
13e07 69 6e 64 69 72 65 63 74 2d 6d 6f 64 65 27 2e 20 indirect-mode'.
13e08 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 If the optimizat
13e09 69 6f 6e 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 ion is compiled
13e0a 69 6e 20 62 75 74 0a 20 20 20 20 2a 2a 20 69 73 in but. ** is
13e0b 20 6e 6f 74 20 61 70 70 6c 69 63 61 62 6c 65 20 not applicable
13e0c 74 6f 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 to this transact
13e0d 69 6f 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 ion, call sqlite
13e0e 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 29 3JournalCreate()
13e0f 0a 20 20 20 20 2a 2a 20 74 6f 20 6d 61 6b 65 20 . ** to make
13e10 73 75 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c sure the journal
13e11 20 66 69 6c 65 20 68 61 73 20 61 63 74 75 61 6c file has actual
13e12 6c 79 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c ly been created,
13e13 20 74 68 65 6e 20 63 61 6c 6c 0a 20 20 20 20 2a then call. *
13e14 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 * pager_incr_cha
13e15 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 ngecounter() to
13e16 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 update the chang
13e17 65 2d 63 6f 75 6e 74 65 72 20 69 6e 20 69 6e 64 e-counter in ind
13e18 69 72 65 63 74 0a 20 20 20 20 2a 2a 20 6d 6f 64 irect. ** mod
13e19 65 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a e. . **. *
13e1a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 * Otherwise, if
13e1b 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e the optimization
13e1c 20 69 73 20 62 6f 74 68 20 65 6e 61 62 6c 65 64 is both enabled
13e1d 20 61 6e 64 20 61 70 70 6c 69 63 61 62 6c 65 2c and applicable,
13e1e 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 61 6c . ** then cal
13e1f 6c 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 l pager_incr_cha
13e20 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 ngecounter() to
13e21 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 update the chang
13e22 65 2d 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a e-counter. **
13e23 20 69 6e 20 27 64 69 72 65 63 74 27 20 6d 6f 64 in 'direct' mod
13e24 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 e. In this case
13e25 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
13e26 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 0a 20 will never be.
13e27 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 66 6f ** created fo
13e28 72 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 r this transacti
13e29 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 on.. */.#ifde
13e2a 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
13e2b 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 ATOMIC_WRITE.
13e2c 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 20 20 20 PgHdr *pPg;.
13e2d 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 assert( isOpen(
13e2e 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 pPager->jfd) ||
13e2f 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
13e30 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
13e31 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20 ALMODE_OFF );.
13e32 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 26 if( !zMaster &
13e33 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d & isOpen(pPager-
13e34 3e 6a 66 64 29 20 0a 20 20 20 20 20 26 26 20 70 >jfd) . && p
13e35 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
13e36 66 3d 3d 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a f==jrnlBufferSiz
13e37 65 28 70 50 61 67 65 72 29 20 0a 20 20 20 20 20 e(pPager) .
13e38 26 26 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a && pPager->dbSiz
13e39 65 3e 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 6c e>=pPager->dbFil
13e3a 65 53 69 7a 65 0a 20 20 20 20 20 26 26 20 28 30 eSize. && (0
13e3b 3d 3d 28 70 50 67 20 3d 20 73 71 6c 69 74 65 33 ==(pPg = sqlite3
13e3c 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 PcacheDirtyList(
13e3d 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
13e3e 29 20 7c 7c 20 30 3d 3d 70 50 67 2d 3e 70 44 69 ) || 0==pPg->pDi
13e3f 72 74 79 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 rty). ){.
13e40 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 /* Update the
13e41 64 62 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63 db file change c
13e42 6f 75 6e 74 65 72 20 76 69 61 20 74 68 65 20 64 ounter via the d
13e43 69 72 65 63 74 2d 77 72 69 74 65 20 6d 65 74 68 irect-write meth
13e44 6f 64 2e 20 54 68 65 20 0a 20 20 20 20 20 20 2a od. The . *
13e45 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c * following call
13e46 20 77 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 will modify the
13e47 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 in-memory repre
13e48 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70 61 67 sentation of pag
13e49 65 20 31 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f e 1 . ** to
13e4a 20 69 6e 63 6c 75 64 65 20 74 68 65 20 75 70 64 include the upd
13e4b 61 74 65 64 20 63 68 61 6e 67 65 20 63 6f 75 6e ated change coun
13e4c 74 65 72 20 61 6e 64 20 74 68 65 6e 20 77 72 69 ter and then wri
13e4d 74 65 20 70 61 67 65 20 31 20 0a 20 20 20 20 20 te page 1 .
13e4e 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20 ** directly to
13e4f 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
13e50 65 2e 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 e. Because of th
13e51 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 0a e atomic-write .
13e52 20 20 20 20 20 20 2a 2a 20 70 72 6f 70 65 72 74 ** propert
13e53 79 20 6f 66 20 74 68 65 20 68 6f 73 74 20 66 69 y of the host fi
13e54 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 69 73 20 le-system, this
13e55 69 73 20 73 61 66 65 2e 0a 20 20 20 20 20 20 2a is safe.. *
13e56 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 /. rc = pag
13e57 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f er_incr_changeco
13e58 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 31 29 unter(pPager, 1)
13e59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
13e5a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a rc = sqlite3J
13e5b 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 70 50 61 ournalCreate(pPa
13e5c 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 ger->jfd);.
13e5d 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
13e5e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
13e5f 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 = pager_incr_ch
13e60 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 angecounter(pPag
13e61 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a er, 0);. }.
13e62 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 }.#else.
13e63 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f rc = pager_incr_
13e64 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 changecounter(pP
13e65 61 67 65 72 2c 20 30 29 3b 0a 23 65 6e 64 69 66 ager, 0);.#endif
13e66 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
13e67 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f ITE_OK ) goto co
13e68 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 mmit_phase_one_e
13e69 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 xit;.. /* If
13e6a 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e this transaction
13e6b 20 68 61 73 20 6d 61 64 65 20 74 68 65 20 64 61 has made the da
13e6c 74 61 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20 tabase smaller,
13e6d 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20 then all pages.
13e6e 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 69 73 63 ** being disc
13e6f 61 72 64 65 64 20 62 79 20 74 68 65 20 74 72 75 arded by the tru
13e70 6e 63 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 ncation must be
13e71 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a written to the j
13e72 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 ournal. ** fi
13e73 6c 65 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c le. This can onl
13e74 79 20 68 61 70 70 65 6e 20 69 6e 20 61 75 74 6f y happen in auto
13e75 2d 76 61 63 75 75 6d 20 6d 6f 64 65 2e 0a 20 20 -vacuum mode..
13e76 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 42 65 66 6f **. ** Befo
13e77 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 70 re reading the p
13e78 61 67 65 73 20 77 69 74 68 20 70 61 67 65 20 6e ages with page n
13e79 75 6d 62 65 72 73 20 6c 61 72 67 65 72 20 74 68 umbers larger th
13e7a 61 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 an the . ** c
13e7b 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 urrent value of
13e7c 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 73 65 Pager.dbSize, se
13e7d 74 20 64 62 53 69 7a 65 20 62 61 63 6b 20 74 6f t dbSize back to
13e7e 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20 20 2a the value. *
13e7f 2a 20 74 68 61 74 20 69 74 20 74 6f 6f 6b 20 61 * that it took a
13e80 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 t the start of t
13e81 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 he transaction.
13e82 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 0a 20 Otherwise, the.
13e83 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 ** calls to s
13e84 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 qlite3PagerGet()
13e85 20 72 65 74 75 72 6e 20 7a 65 72 6f 65 64 20 70 return zeroed p
13e86 61 67 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20 ages instead of
13e87 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 . ** reading
13e88 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61 data from the da
13e89 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 tabase file..
13e8a 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20 **. ** When
13e8b 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 3d 4f 46 journal_mode==OF
13e8c 46 20 74 68 65 20 64 62 4f 72 69 67 53 69 7a 65 F the dbOrigSize
13e8d 20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2c is always zero,
13e8e 20 73 6f 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 so this. **
13e8f 62 6c 6f 63 6b 20 6e 65 76 65 72 20 72 75 6e 73 block never runs
13e90 20 69 66 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 if journal_mode
13e91 3d 4f 46 46 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 =OFF.. */.#if
13e92 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
13e93 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
13e94 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 if( pPager->dbSi
13e95 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 ze<pPager->dbOri
13e96 67 53 69 7a 65 20 0a 20 20 20 20 20 26 26 20 41 gSize . && A
13e97 4c 57 41 59 53 28 70 50 61 67 65 72 2d 3e 6a 6f LWAYS(pPager->jo
13e98 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 urnalMode!=PAGER
13e99 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 _JOURNALMODE_OFF
13e9a 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 50 ). ){. P
13e9b 67 6e 6f 20 69 3b 20 20 20 20 20 20 20 20 20 20 gno i;
13e9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13e9d 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 /* Iter
13e9e 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f ator variable */
13e9f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e . const Pgn
13ea0 6f 20 69 53 6b 69 70 20 3d 20 50 41 47 45 52 5f o iSkip = PAGER_
13ea1 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 3b MJ_PGNO(pPager);
13ea2 20 2f 2a 20 50 65 6e 64 69 6e 67 20 6c 6f 63 6b /* Pending lock
13ea3 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 63 page */. c
13ea4 6f 6e 73 74 20 50 67 6e 6f 20 64 62 53 69 7a 65 onst Pgno dbSize
13ea5 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a = pPager->dbSiz
13ea6 65 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 e; /* Data
13ea7 62 61 73 65 20 69 6d 61 67 65 20 73 69 7a 65 20 base image size
13ea8 2a 2f 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 */ . pPager
13ea9 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65 ->dbSize = pPage
13eaa 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b 0a 20 r->dbOrigSize;.
13eab 20 20 20 20 20 66 6f 72 28 20 69 3d 64 62 53 69 for( i=dbSi
13eac 7a 65 2b 31 3b 20 69 3c 3d 70 50 61 67 65 72 2d ze+1; i<=pPager-
13ead 3e 64 62 4f 72 69 67 53 69 7a 65 3b 20 69 2b 2b >dbOrigSize; i++
13eae 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
13eaf 21 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 !sqlite3BitvecTe
13eb0 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f st(pPager->pInJo
13eb1 75 72 6e 61 6c 2c 20 69 29 20 26 26 20 69 21 3d urnal, i) && i!=
13eb2 69 53 6b 69 70 20 29 7b 0a 20 20 20 20 20 20 20 iSkip ){.
13eb3 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b PgHdr *pPage;
13eb4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13eb5 50 61 67 65 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 Page to journal
13eb6 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 */. rc
13eb7 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
13eb8 74 28 70 50 61 67 65 72 2c 20 69 2c 20 26 70 50 t(pPager, i, &pP
13eb9 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
13eba 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
13ebb 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f K ) goto commit_
13ebc 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a phase_one_exit;.
13ebd 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
13ebe 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
13ebf 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 (pPage);.
13ec0 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 sqlite3PagerU
13ec1 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 nref(pPage);.
13ec2 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
13ec3 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 QLITE_OK ) goto
13ec4 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 commit_phase_one
13ec5 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 7d _exit;. }
13ec6 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20 20 . } .
13ec7 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d pPager->dbSize =
13ec8 20 64 62 53 69 7a 65 3b 0a 20 20 20 20 7d 0a 23 dbSize;. }.#
13ec9 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 57 72 endif.. /* Wr
13eca 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a ite the master j
13ecb 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f ournal name into
13ecc 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
13ecd 65 2e 20 49 66 20 61 20 6d 61 73 74 65 72 20 0a e. If a master .
13ece 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 ** journal f
13ecf 69 6c 65 20 6e 61 6d 65 20 68 61 73 20 61 6c 72 ile name has alr
13ed0 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 eady been writte
13ed1 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c n to the journal
13ed2 20 66 69 6c 65 2c 20 0a 20 20 20 20 2a 2a 20 6f file, . ** o
13ed3 72 20 69 66 20 7a 4d 61 73 74 65 72 20 69 73 20 r if zMaster is
13ed4 4e 55 4c 4c 20 28 6e 6f 20 6d 61 73 74 65 72 20 NULL (no master
13ed5 6a 6f 75 72 6e 61 6c 29 2c 20 74 68 65 6e 20 74 journal), then t
13ed6 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f his call is a no
13ed7 2d 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 -op.. */.
13ed8 72 63 20 3d 20 77 72 69 74 65 4d 61 73 74 65 72 rc = writeMaster
13ed9 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 Journal(pPager,
13eda 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66 zMaster);. if
13edb 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
13edc 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 ) goto commit_ph
13edd 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 ase_one_exit;..
13ede 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a /* Sync the j
13edf 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 ournal file. If
13ee0 74 68 65 20 61 74 6f 6d 69 63 2d 75 70 64 61 74 the atomic-updat
13ee1 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 e optimization i
13ee2 73 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 75 s being. ** u
13ee3 73 65 64 2c 20 74 68 69 73 20 63 61 6c 6c 20 77 sed, this call w
13ee4 69 6c 6c 20 6e 6f 74 20 63 72 65 61 74 65 20 74 ill not create t
13ee5 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
13ee6 6f 72 20 70 65 72 66 6f 72 6d 20 61 6e 79 0a 20 or perform any.
13ee7 20 20 20 2a 2a 20 72 65 61 6c 20 49 4f 2e 0a 20 ** real IO..
13ee8 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 */. rc = s
13ee9 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 yncJournal(pPage
13eea 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d r);. if( rc!=
13eeb 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
13eec 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e commit_phase_on
13eed 65 5f 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 e_exit;.. /*
13eee 57 72 69 74 65 20 61 6c 6c 20 64 69 72 74 79 20 Write all dirty
13eef 70 61 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 pages to the dat
13ef0 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 abase file. */.
13ef1 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 rc = pager_wr
13ef2 69 74 65 5f 70 61 67 65 6c 69 73 74 28 73 71 6c ite_pagelist(sql
13ef3 69 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c ite3PcacheDirtyL
13ef4 69 73 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 ist(pPager->pPCa
13ef5 63 68 65 29 29 3b 0a 20 20 20 20 69 66 28 20 72 che));. if( r
13ef6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
13ef7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
13ef8 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 !=SQLITE_IOERR_B
13ef9 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 20 20 20 20 LOCKED );.
13efa 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 goto commit_phas
13efb 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 e_one_exit;.
13efc 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 }. sqlite3Pca
13efd 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 cheCleanAll(pPag
13efe 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a 20 er->pPCache);..
13eff 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c /* If the fil
13f00 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f 74 e on disk is not
13f01 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 20 61 the same size a
13f02 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 s the database i
13f03 6d 61 67 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 mage,. ** the
13f04 6e 20 75 73 65 20 70 61 67 65 72 5f 74 72 75 6e n use pager_trun
13f05 63 61 74 65 20 74 6f 20 67 72 6f 77 20 6f 72 20 cate to grow or
13f06 73 68 72 69 6e 6b 20 74 68 65 20 66 69 6c 65 20 shrink the file
13f07 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 here.. */.
13f08 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 if( pPager->dbS
13f09 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 64 62 46 ize!=pPager->dbF
13f0a 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 ileSize ){.
13f0b 20 50 67 6e 6f 20 6e 4e 65 77 20 3d 20 70 50 61 Pgno nNew = pPa
13f0c 67 65 72 2d 3e 64 62 53 69 7a 65 20 2d 20 28 70 ger->dbSize - (p
13f0d 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 50 Pager->dbSize==P
13f0e 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 AGER_MJ_PGNO(pPa
13f0f 67 65 72 29 29 3b 0a 20 20 20 20 20 20 61 73 73 ger));. ass
13f10 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
13f11 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 te>=PAGER_EXCLUS
13f12 49 56 45 20 29 3b 0a 20 20 20 20 20 20 72 63 20 IVE );. rc
13f13 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 = pager_truncate
13f14 28 70 50 61 67 65 72 2c 20 6e 4e 65 77 29 3b 0a (pPager, nNew);.
13f15 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
13f16 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 LITE_OK ) goto c
13f17 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f ommit_phase_one_
13f18 65 78 69 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 exit;. }..
13f19 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 73 79 6e /* Finally, syn
13f1a 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 c the database f
13f1b 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 ile. */. if(
13f1c 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 !pPager->noSync
13f1d 26 26 20 21 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 && !noSync ){.
13f1e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
13f1f 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 OsSync(pPager->f
13f20 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f d, pPager->sync_
13f21 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20 flags);. }.
13f22 20 20 49 4f 54 52 41 43 45 28 28 22 44 42 53 59 IOTRACE(("DBSY
13f23 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 NC %p\n", pPager
13f24 29 29 0a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e )).. pPager->
13f25 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53 59 state = PAGER_SY
13f26 4e 43 45 44 3b 0a 20 20 7d 0a 0a 63 6f 6d 6d 69 NCED;. }..commi
13f27 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 t_phase_one_exit
13f28 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d :. return rc;.}
13f29 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 .../*.** When th
13f2a 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
13f2b 61 6c 6c 65 64 2c 20 74 68 65 20 64 61 74 61 62 alled, the datab
13f2c 61 73 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 ase file has bee
13f2d 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 n completely.**
13f2e 75 70 64 61 74 65 64 20 74 6f 20 72 65 66 6c 65 updated to refle
13f2f 63 74 20 74 68 65 20 63 68 61 6e 67 65 73 20 6d ct the changes m
13f30 61 64 65 20 62 79 20 74 68 65 20 63 75 72 72 65 ade by the curre
13f31 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 nt transaction a
13f32 6e 64 0a 2a 2a 20 73 79 6e 63 65 64 20 74 6f 20 nd.** synced to
13f33 64 69 73 6b 2e 20 54 68 65 20 6a 6f 75 72 6e 61 disk. The journa
13f34 6c 20 66 69 6c 65 20 73 74 69 6c 6c 20 65 78 69 l file still exi
13f35 73 74 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2d sts in the file-
13f36 73 79 73 74 65 6d 20 0a 2a 2a 20 74 68 6f 75 67 system .** thoug
13f37 68 2c 20 61 6e 64 20 69 66 20 61 20 66 61 69 6c h, and if a fail
13f38 75 72 65 20 6f 63 63 75 72 73 20 61 74 20 74 68 ure occurs at th
13f39 69 73 20 70 6f 69 6e 74 20 69 74 20 77 69 6c 6c is point it will
13f3a 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 62 eventually.** b
13f3b 65 20 75 73 65 64 20 61 73 20 61 20 68 6f 74 2d e used as a hot-
13f3c 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 journal and the
13f3d 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
13f3e 69 6f 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e ion rolled back.
13f3f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
13f40 74 69 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 20 74 tion finalizes t
13f41 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c he journal file,
13f42 20 65 69 74 68 65 72 20 62 79 20 64 65 6c 65 74 either by delet
13f43 69 6e 67 2c 20 0a 2a 2a 20 74 72 75 6e 63 61 74 ing, .** truncat
13f44 69 6e 67 20 6f 72 20 70 61 72 74 69 61 6c 6c 79 ing or partially
13f45 20 7a 65 72 6f 69 6e 67 20 69 74 2c 20 73 6f 20 zeroing it, so
13f46 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 that it cannot b
13f47 65 20 75 73 65 64 20 0a 2a 2a 20 66 6f 72 20 68 e used .** for h
13f48 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 ot-journal rollb
13f49 61 63 6b 2e 20 4f 6e 63 65 20 74 68 69 73 20 69 ack. Once this i
13f4a 73 20 64 6f 6e 65 20 74 68 65 20 74 72 61 6e 73 s done the trans
13f4b 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 72 72 action is.** irr
13f4c 65 76 6f 63 61 62 6c 79 20 63 6f 6d 6d 69 74 74 evocably committ
13f4d 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ed..**.** If an
13f4e 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e error occurs, an
13f4f 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 IO error code i
13f50 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 s returned and t
13f51 68 65 20 70 61 67 65 72 0a 2a 2a 20 6d 6f 76 65 he pager.** move
13f52 73 20 69 6e 74 6f 20 74 68 65 20 65 72 72 6f 72 s into the error
13f53 20 73 74 61 74 65 2e 20 4f 74 68 65 72 77 69 73 state. Otherwis
13f54 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 e, SQLITE_OK is
13f55 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c returned..*/.SQL
13f56 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
13f57 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d sqlite3PagerComm
13f58 69 74 50 68 61 73 65 54 77 6f 28 50 61 67 65 72 itPhaseTwo(Pager
13f59 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 *pPager){. int
13f5a 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
13f5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13f5c 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
13f5d 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 */.. /* This r
13f5e 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6e 6f outine should no
13f5f 74 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 61 t be called if a
13f60 20 70 72 69 6f 72 20 65 72 72 6f 72 20 68 61 73 prior error has
13f61 20 6f 63 63 75 72 72 65 64 2e 0a 20 20 2a 2a 20 occurred.. **
13f62 42 75 74 20 69 66 20 28 64 75 65 20 74 6f 20 61 But if (due to a
13f63 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 65 6c coding error el
13f64 73 65 77 68 65 72 65 20 69 6e 20 74 68 65 20 73 sewhere in the s
13f65 79 73 74 65 6d 29 20 69 74 20 64 6f 65 73 20 67 ystem) it does g
13f66 65 74 0a 20 20 2a 2a 20 63 61 6c 6c 65 64 2c 20 et. ** called,
13f67 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20 just return the
13f68 73 61 6d 65 20 65 72 72 6f 72 20 63 6f 64 65 20 same error code
13f69 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e without doing an
13f6a 79 74 68 69 6e 67 2e 20 2a 2f 0a 20 20 69 66 28 ything. */. if(
13f6b 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 NEVER(pPager->e
13f6c 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e rrCode) ) return
13f6d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
13f6e 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e ;.. /* This fun
13f6f 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 ction should not
13f70 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 74 68 be called if th
13f71 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 e pager is not i
13f72 6e 20 61 74 20 6c 65 61 73 74 0a 20 20 2a 2a 20 n at least. **
13f73 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 73 PAGER_RESERVED s
13f74 74 61 74 65 2e 20 41 6e 64 20 69 6e 64 65 65 64 tate. And indeed
13f75 20 53 51 4c 69 74 65 20 6e 65 76 65 72 20 64 6f SQLite never do
13f76 65 73 20 74 68 69 73 2e 20 42 75 74 20 69 74 20 es this. But it
13f77 69 73 0a 20 20 2a 2a 20 6e 69 63 65 20 74 6f 20 is. ** nice to
13f78 68 61 76 65 20 74 68 69 73 20 64 65 66 65 6e 73 have this defens
13f79 69 76 65 20 74 65 73 74 20 68 65 72 65 20 61 6e ive test here an
13f7a 79 77 61 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 yway.. */. if(
13f7b 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 73 NEVER(pPager->s
13f7c 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53 45 52 tate<PAGER_RESER
13f7d 56 45 44 29 20 29 20 72 65 74 75 72 6e 20 53 51 VED) ) return SQ
13f7e 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f LITE_ERROR;.. /
13f7f 2a 20 41 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f * An optimizatio
13f80 6e 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 61 n. If the databa
13f81 73 65 20 77 61 73 20 6e 6f 74 20 61 63 74 75 61 se was not actua
13f82 6c 6c 79 20 6d 6f 64 69 66 69 65 64 20 64 75 72 lly modified dur
13f83 69 6e 67 0a 20 20 2a 2a 20 74 68 69 73 20 74 72 ing. ** this tr
13f84 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 20 70 ansaction, the p
13f85 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 ager is running
13f86 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 in exclusive-mod
13f87 65 20 61 6e 64 20 69 73 0a 20 20 2a 2a 20 75 73 e and is. ** us
13f88 69 6e 67 20 70 65 72 73 69 73 74 65 6e 74 20 6a ing persistent j
13f89 6f 75 72 6e 61 6c 73 2c 20 74 68 65 6e 20 74 68 ournals, then th
13f8a 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
13f8b 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a no-op.. **. *
13f8c 2a 20 54 68 65 20 73 74 61 72 74 20 6f 66 20 74 * The start of t
13f8d 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
13f8e 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 currently contai
13f8f 6e 73 20 61 20 73 69 6e 67 6c 65 20 6a 6f 75 72 ns a single jour
13f90 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64 65 72 nal . ** header
13f91 20 77 69 74 68 20 74 68 65 20 6e 52 65 63 20 66 with the nRec f
13f92 69 65 6c 64 20 73 65 74 20 74 6f 20 30 2e 20 49 ield set to 0. I
13f93 66 20 73 75 63 68 20 61 20 6a 6f 75 72 6e 61 6c f such a journal
13f94 20 69 73 20 75 73 65 64 20 61 73 0a 20 20 2a 2a is used as. **
13f95 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 64 a hot-journal d
13f96 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 6e 61 uring hot-journa
13f97 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 30 20 63 68 l rollback, 0 ch
13f98 61 6e 67 65 73 20 77 69 6c 6c 20 62 65 20 6d 61 anges will be ma
13f99 64 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 64 de. ** to the d
13f9a 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 53 6f atabase file. So
13f9b 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 there is no nee
13f9c 64 20 74 6f 20 7a 65 72 6f 20 74 68 65 20 6a 6f d to zero the jo
13f9d 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64 urnal . ** head
13f9e 65 72 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61 er. Since the pa
13f9f 67 65 72 20 69 73 20 69 6e 20 65 78 63 6c 75 73 ger is in exclus
13fa0 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 72 65 20 ive mode, there
13fa1 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 2a 2a 20 is no need. **
13fa2 74 6f 20 64 72 6f 70 20 61 6e 79 20 6c 6f 63 6b to drop any lock
13fa3 73 20 65 69 74 68 65 72 2e 0a 20 20 2a 2f 0a 20 s either.. */.
13fa4 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 4d if( pPager->dbM
13fa5 6f 64 69 66 69 65 64 3d 3d 30 20 26 26 20 70 50 odified==0 && pP
13fa6 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d ager->exclusiveM
13fa7 6f 64 65 20 0a 20 20 20 26 26 20 70 50 61 67 65 ode . && pPage
13fa8 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d r->journalMode==
13fa9 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
13faa 45 5f 50 45 52 53 49 53 54 0a 20 20 29 7b 0a 20 E_PERSIST. ){.
13fab 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
13fac 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a r->journalOff==J
13fad 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
13fae 61 67 65 72 29 20 29 3b 0a 20 20 20 20 72 65 74 ager) );. ret
13faf 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
13fb0 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 }.. PAGERTRACE
13fb1 28 28 22 43 4f 4d 4d 49 54 20 25 64 5c 6e 22 2c (("COMMIT %d\n",
13fb2 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
13fb3 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 ));. assert( pP
13fb4 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 ager->state==PAG
13fb5 45 52 5f 53 59 4e 43 45 44 20 7c 7c 20 4d 45 4d ER_SYNCED || MEM
13fb6 44 42 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e 64 DB || !pPager->d
13fb7 62 4d 6f 64 69 66 69 65 64 20 29 3b 0a 20 20 72 bModified );. r
13fb8 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 c = pager_end_tr
13fb9 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 ansaction(pPager
13fba 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 , pPager->setMas
13fbb 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 ter);. return p
13fbc 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 ager_error(pPage
13fbd 72 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a r, rc);.}../*.**
13fbe 20 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 68 Rollback all ch
13fbf 61 6e 67 65 73 2e 20 54 68 65 20 64 61 74 61 62 anges. The datab
13fc0 61 73 65 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 ase falls back t
13fc1 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 6d o PAGER_SHARED m
13fc2 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ode..**.** This
13fc3 66 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d function perform
13fc4 73 20 74 77 6f 20 74 61 73 6b 73 3a 0a 2a 2a 0a s two tasks:.**.
13fc5 2a 2a 20 20 20 31 29 20 49 74 20 72 6f 6c 6c 73 ** 1) It rolls
13fc6 20 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 back the journa
13fc7 6c 20 66 69 6c 65 2c 20 72 65 73 74 6f 72 69 6e l file, restorin
13fc8 67 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 g all database f
13fc9 69 6c 65 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 ile and .**
13fca 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 in-memory cache
13fcb 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 73 74 pages to the st
13fcc 61 74 65 20 74 68 65 79 20 77 65 72 65 20 69 6e ate they were in
13fcd 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 when the transa
13fce 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 77 61 ction.** wa
13fcf 73 20 6f 70 65 6e 65 64 2c 20 61 6e 64 0a 2a 2a s opened, and.**
13fd0 20 20 20 32 29 20 49 74 20 66 69 6e 61 6c 69 7a 2) It finaliz
13fd1 65 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 es the journal f
13fd2 69 6c 65 2c 20 73 6f 20 74 68 61 74 20 69 74 20 ile, so that it
13fd3 69 73 20 6e 6f 74 20 75 73 65 64 20 66 6f 72 20 is not used for
13fd4 68 6f 74 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c hot.** roll
13fd5 62 61 63 6b 20 61 74 20 61 6e 79 20 70 6f 69 6e back at any poin
13fd6 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e t in the future.
13fd7 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 65 63 74 20 74 .**.** subject t
13fd8 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 o the following
13fd9 71 75 61 6c 69 66 69 63 61 74 69 6f 6e 73 3a 0a qualifications:.
13fda 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74 68 65 20 6a **.** * If the j
13fdb 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e ournal file is n
13fdc 6f 74 20 79 65 74 20 6f 70 65 6e 20 77 68 65 6e ot yet open when
13fdd 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
13fde 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 20 20 74 s called,.** t
13fdf 68 65 6e 20 6f 6e 6c 79 20 28 32 29 20 69 73 20 hen only (2) is
13fe0 70 65 72 66 6f 72 6d 65 64 2e 20 49 6e 20 74 68 performed. In th
13fe1 69 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73 is case there is
13fe2 20 6e 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 no journal file
13fe3 0a 2a 2a 20 20 20 74 6f 20 72 6f 6c 6c 20 62 61 .** to roll ba
13fe4 63 6b 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 69 ck..**.** * If i
13fe5 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 n an error state
13fe6 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 other than SQLI
13fe7 54 45 5f 46 55 4c 4c 2c 20 74 68 65 6e 20 74 61 TE_FULL, then ta
13fe8 73 6b 20 28 31 29 20 69 73 20 0a 2a 2a 20 20 20 sk (1) is .**
13fe9 70 65 72 66 6f 72 6d 65 64 2e 20 49 66 20 73 75 performed. If su
13fea 63 63 65 73 73 66 75 6c 2c 20 74 61 73 6b 20 28 ccessful, task (
13feb 32 29 2e 20 52 65 67 61 72 64 6c 65 73 73 20 6f 2). Regardless o
13fec 66 20 74 68 65 20 6f 75 74 63 6f 6d 65 0a 2a 2a f the outcome.**
13fed 20 20 20 6f 66 20 65 69 74 68 65 72 2c 20 74 68 of either, th
13fee 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 e error state er
13fef 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
13ff0 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c rned to the call
13ff1 65 72 0a 2a 2a 20 20 20 28 69 2e 65 2e 20 65 69 er.** (i.e. ei
13ff2 74 68 65 72 20 53 51 4c 49 54 45 5f 49 4f 45 52 ther SQLITE_IOER
13ff3 52 20 6f 72 20 53 51 4c 49 54 45 5f 43 4f 52 52 R or SQLITE_CORR
13ff4 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 UPT)..**.** * If
13ff5 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
13ff6 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 PAGER_RESERVED
13ff7 73 74 61 74 65 2c 20 74 68 65 6e 20 61 74 74 65 state, then atte
13ff8 6d 70 74 20 28 31 29 2e 20 57 68 65 74 68 65 72 mpt (1). Whether
13ff9 0a 2a 2a 20 20 20 6f 72 20 6e 6f 74 20 28 31 29 .** or not (1)
13ffa 20 69 73 20 73 75 63 63 75 73 73 66 75 6c 2c 20 is succussful,
13ffb 61 6c 73 6f 20 61 74 74 65 6d 70 74 20 28 32 29 also attempt (2)
13ffc 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c . If successful,
13ffd 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 53 51 4c return.** SQL
13ffe 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 ITE_OK. Otherwis
13fff 65 2c 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 e, enter the err
14000 6f 72 20 73 74 61 74 65 20 61 6e 64 20 72 65 74 or state and ret
14001 75 72 6e 20 74 68 65 20 66 69 72 73 74 20 0a 2a urn the first .*
14002 2a 20 20 20 65 72 72 6f 72 20 63 6f 64 65 20 65 * error code e
14003 6e 63 6f 75 6e 74 65 72 65 64 2e 20 0a 2a 2a 0a ncountered. .**.
14004 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 63 61 73 ** In this cas
14005 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 68 e there is no ch
14006 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 64 61 ance that the da
14007 74 61 62 61 73 65 20 77 61 73 20 77 72 69 74 74 tabase was writt
14008 65 6e 20 74 6f 2e 20 0a 2a 2a 20 20 20 53 6f 20 en to. .** So
14009 69 73 20 73 61 66 65 20 74 6f 20 66 69 6e 61 6c is safe to final
1400a 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ize the journal
1400b 66 69 6c 65 20 65 76 65 6e 20 69 66 20 74 68 65 file even if the
1400c 20 70 6c 61 79 62 61 63 6b 20 0a 2a 2a 20 20 20 playback .**
1400d 28 6f 70 65 72 61 74 69 6f 6e 20 31 29 20 66 61 (operation 1) fa
1400e 69 6c 65 64 2e 20 48 6f 77 65 76 65 72 20 74 68 iled. However th
1400f 65 20 70 61 67 65 72 20 6d 75 73 74 20 65 6e 74 e pager must ent
14010 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 er the error sta
14011 74 65 0a 2a 2a 20 20 20 61 73 20 74 68 65 20 63 te.** as the c
14012 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 69 ontents of the i
14013 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 61 n-memory cache a
14014 72 65 20 6e 6f 77 20 73 75 73 70 65 63 74 2e 0a re now suspect..
14015 2a 2a 0a 2a 2a 20 2a 20 46 69 6e 61 6c 6c 79 2c **.** * Finally,
14016 20 69 66 20 69 6e 20 50 41 47 45 52 5f 45 58 43 if in PAGER_EXC
14017 4c 55 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 LUSIVE state, th
14018 65 6e 20 61 74 74 65 6d 70 74 20 28 31 29 2e 20 en attempt (1).
14019 4f 6e 6c 79 0a 2a 2a 20 20 20 61 74 74 65 6d 70 Only.** attemp
1401a 74 20 28 32 29 20 69 66 20 28 31 29 20 69 73 20 t (2) if (1) is
1401b 73 75 63 63 65 73 73 66 75 6c 2e 20 52 65 74 75 successful. Retu
1401c 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 rn SQLITE_OK if
1401d 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 20 successful,.**
1401e 20 6f 74 68 65 72 77 69 73 65 20 65 6e 74 65 72 otherwise enter
1401f 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
14020 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 and return the
14021 65 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 error code from
14022 74 68 65 20 0a 2a 2a 20 20 20 66 61 69 6c 69 6e the .** failin
14023 67 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a g operation..**.
14024 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 63 61 73 ** In this cas
14025 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 e the database f
14026 69 6c 65 20 6d 61 79 20 68 61 76 65 20 62 65 65 ile may have bee
14027 6e 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 53 6f n written to. So
14028 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 70 6c 61 if the.** pla
14029 79 62 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20 yback operation
1402a 64 69 64 20 6e 6f 74 20 73 75 63 63 65 65 64 20 did not succeed
1402b 69 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 20 it would not be
1402c 73 61 66 65 20 74 6f 20 66 69 6e 61 6c 69 7a 65 safe to finalize
1402d 0a 2a 2a 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 .** the journa
1402e 6c 20 66 69 6c 65 2e 20 49 74 20 6e 65 65 64 73 l file. It needs
1402f 20 74 6f 20 62 65 20 6c 65 66 74 20 69 6e 20 74 to be left in t
14030 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 he file-system s
14031 6f 20 74 68 61 74 0a 2a 2a 20 20 20 73 6f 6d 65 o that.** some
14032 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 63 other process c
14033 61 6e 20 75 73 65 20 69 74 20 74 6f 20 72 65 73 an use it to res
14034 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 tore the databas
14035 65 20 73 74 61 74 65 20 28 62 79 0a 2a 2a 20 20 e state (by.**
14036 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c hot-journal rol
14037 6c 62 61 63 6b 29 2e 0a 2a 2f 0a 53 51 4c 49 54 lback)..*/.SQLIT
14038 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
14039 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 lite3PagerRollba
1403a 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ck(Pager *pPager
1403b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1403c 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 LITE_OK;
1403d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
1403e 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50 41 urn code */. PA
1403f 47 45 52 54 52 41 43 45 28 28 22 52 4f 4c 4c 42 GERTRACE(("ROLLB
14040 41 43 4b 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 ACK %d\n", PAGER
14041 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 ID(pPager)));.
14042 69 66 28 20 21 70 50 61 67 65 72 2d 3e 64 62 4d if( !pPager->dbM
14043 6f 64 69 66 69 65 64 20 7c 7c 20 21 69 73 4f 70 odified || !isOp
14044 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
14045 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 ){. rc = page
14046 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f r_end_transactio
14047 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 n(pPager, pPager
14048 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 ->setMaster);.
14049 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 }else if( pPager
1404a 2d 3e 65 72 72 43 6f 64 65 20 26 26 20 70 50 61 ->errCode && pPa
1404b 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 ger->errCode!=SQ
1404c 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 LITE_FULL ){.
1404d 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 if( pPager->sta
1404e 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 te>=PAGER_EXCLUS
1404f 49 56 45 20 29 7b 0a 20 20 20 20 20 20 70 61 67 IVE ){. pag
14050 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 er_playback(pPag
14051 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 er, 0);. }.
14052 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 rc = pPager->e
14053 72 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b rrCode;. }else{
14054 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d . if( pPager-
14055 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 >state==PAGER_RE
14056 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 20 20 SERVED ){.
14057 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20 20 72 int rc2;. r
14058 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 c = pager_playba
14059 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 ck(pPager, 0);.
1405a 20 20 20 20 20 72 63 32 20 3d 20 70 61 67 65 72 rc2 = pager
1405b 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e _end_transaction
1405c 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d (pPager, pPager-
1405d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 20 >setMaster);.
1405e 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1405f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
14060 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 rc = rc2;.
14061 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
14062 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c rc = pager_pl
14063 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30 ayback(pPager, 0
14064 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 );. }.. if
14065 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 ( !MEMDB ){.
14066 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
14067 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d Valid = 0;. }
14068 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 65 .. /* If an e
14069 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
1406a 6e 67 20 61 20 52 4f 4c 4c 42 41 43 4b 2c 20 77 ng a ROLLBACK, w
1406b 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 e can no longer
1406c 74 72 75 73 74 20 74 68 65 20 70 61 67 65 72 0a trust the pager.
1406d 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 20 53 6f ** cache. So
1406e 20 63 61 6c 6c 20 70 61 67 65 72 5f 65 72 72 6f call pager_erro
1406f 72 28 29 20 6f 6e 20 74 68 65 20 77 61 79 20 6f r() on the way o
14070 75 74 20 74 6f 20 6d 61 6b 65 20 61 6e 79 20 65 ut to make any e
14071 72 72 6f 72 20 0a 20 20 20 20 2a 2a 20 70 65 72 rror . ** per
14072 73 69 73 74 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a sistent.. */.
14073 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 rc = pager_e
14074 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 rror(pPager, rc)
14075 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
14076 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 c;.}../*.** Retu
14077 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 64 rn TRUE if the d
14078 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 atabase file is
14079 6f 70 65 6e 65 64 20 72 65 61 64 2d 6f 6e 6c 79 opened read-only
1407a 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a . Return FALSE.
1407b 2a 2a 20 69 66 20 74 68 65 20 64 61 74 61 62 61 ** if the databa
1407c 73 65 20 69 73 20 28 69 6e 20 74 68 65 6f 72 79 se is (in theory
1407d 29 20 77 72 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53 ) writable..*/.S
1407e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 QLITE_PRIVATE u8
1407f 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 72 sqlite3PagerIsr
14080 65 61 64 6f 6e 6c 79 28 50 61 67 65 72 20 2a 70 eadonly(Pager *p
14081 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e Pager){. return
14082 20 70 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c pPager->readOnl
14083 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 y;.}../*.** Retu
14084 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
14085 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 references to t
14086 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c he pager..*/.SQL
14087 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
14088 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 sqlite3PagerRefc
14089 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 ount(Pager *pPag
1408a 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 er){. return sq
1408b 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f lite3PcacheRefCo
1408c 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 unt(pPager->pPCa
1408d 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 che);.}../*.** R
1408e 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
1408f 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 of references t
14090 6f 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 o the specified
14091 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f page..*/.SQLITE_
14092 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
14093 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 te3PagerPageRefc
14094 6f 75 6e 74 28 44 62 50 61 67 65 20 2a 70 50 61 ount(DbPage *pPa
14095 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 ge){. return sq
14096 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 lite3PcachePageR
14097 65 66 63 6f 75 6e 74 28 70 50 61 67 65 29 3b 0a efcount(pPage);.
14098 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
14099 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 _TEST./*.** This
1409a 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 routine is used
1409b 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 for testing and
1409c 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a analysis only..
1409d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1409e 45 20 69 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 E int *sqlite3Pa
1409f 67 65 72 53 74 61 74 73 28 50 61 67 65 72 20 2a gerStats(Pager *
140a0 70 50 61 67 65 72 29 7b 0a 20 20 73 74 61 74 69 pPager){. stati
140a1 63 20 69 6e 74 20 61 5b 31 31 5d 3b 0a 20 20 61 c int a[11];. a
140a2 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61 [0] = sqlite3Pca
140a3 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 cheRefCount(pPag
140a4 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 er->pPCache);.
140a5 61 5b 31 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 a[1] = sqlite3Pc
140a6 61 63 68 65 50 61 67 65 63 6f 75 6e 74 28 70 50 achePagecount(pP
140a7 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a ager->pPCache);.
140a8 20 20 61 5b 32 5d 20 3d 20 73 71 6c 69 74 65 33 a[2] = sqlite3
140a9 50 63 61 63 68 65 47 65 74 43 61 63 68 65 73 69 PcacheGetCachesi
140aa 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ze(pPager->pPCac
140ab 68 65 29 3b 0a 20 20 61 5b 33 5d 20 3d 20 70 50 he);. a[3] = pP
140ac 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 ager->dbSizeVali
140ad 64 20 3f 20 28 69 6e 74 29 20 70 50 61 67 65 72 d ? (int) pPager
140ae 2d 3e 64 62 53 69 7a 65 20 3a 20 2d 31 3b 0a 20 ->dbSize : -1;.
140af 20 61 5b 34 5d 20 3d 20 70 50 61 67 65 72 2d 3e a[4] = pPager->
140b0 73 74 61 74 65 3b 0a 20 20 61 5b 35 5d 20 3d 20 state;. a[5] =
140b1 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b pPager->errCode;
140b2 0a 20 20 61 5b 36 5d 20 3d 20 70 50 61 67 65 72 . a[6] = pPager
140b3 2d 3e 6e 48 69 74 3b 0a 20 20 61 5b 37 5d 20 3d ->nHit;. a[7] =
140b4 20 70 50 61 67 65 72 2d 3e 6e 4d 69 73 73 3b 0a pPager->nMiss;.
140b5 20 20 61 5b 38 5d 20 3d 20 30 3b 20 20 2f 2a 20 a[8] = 0; /*
140b6 55 73 65 64 20 74 6f 20 62 65 20 70 50 61 67 65 Used to be pPage
140b7 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a 20 20 61 5b r->nOvfl */. a[
140b8 39 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 9] = pPager->nRe
140b9 61 64 3b 0a 20 20 61 5b 31 30 5d 20 3d 20 70 50 ad;. a[10] = pP
140ba 61 67 65 72 2d 3e 6e 57 72 69 74 65 3b 0a 20 20 ager->nWrite;.
140bb 72 65 74 75 72 6e 20 61 3b 0a 7d 0a 23 65 6e 64 return a;.}.#end
140bc 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e if../*.** Return
140bd 20 74 72 75 65 20 69 66 20 74 68 69 73 20 69 73 true if this is
140be 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 an in-memory pa
140bf 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ger..*/.SQLITE_P
140c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
140c1 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28 50 e3PagerIsMemdb(P
140c2 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
140c3 20 72 65 74 75 72 6e 20 4d 45 4d 44 42 3b 0a 7d return MEMDB;.}
140c4 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 ../*.** Check th
140c5 61 74 20 74 68 65 72 65 20 61 72 65 20 61 74 20 at there are at
140c6 6c 65 61 73 74 20 6e 53 61 76 65 70 6f 69 6e 74 least nSavepoint
140c7 20 73 61 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e savepoints open
140c8 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 0a 2a . If there are.*
140c9 2a 20 63 75 72 72 65 6e 74 6c 79 20 6c 65 73 73 * currently less
140ca 20 74 68 61 6e 20 6e 53 61 76 65 70 6f 69 6e 74 than nSavepoint
140cb 73 20 6f 70 65 6e 2c 20 74 68 65 6e 20 6f 70 65 s open, then ope
140cc 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 61 n one or more sa
140cd 76 65 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 6d vepoints.** to m
140ce 61 6b 65 20 75 70 20 74 68 65 20 64 69 66 66 65 ake up the diffe
140cf 72 65 6e 63 65 2e 20 49 66 20 74 68 65 20 6e 75 rence. If the nu
140d0 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e mber of savepoin
140d1 74 73 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a ts is already.**
140d2 20 65 71 75 61 6c 20 74 6f 20 6e 53 61 76 65 70 equal to nSavep
140d3 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20 oint, then this
140d4 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f function is a no
140d5 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 -op..**.** If a
140d6 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
140d7 6e 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f n fails, SQLITE_
140d8 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 NOMEM is returne
140d9 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 0a d. If an error .
140da 2a 2a 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 ** occurs while
140db 6f 70 65 6e 69 6e 67 20 74 68 65 20 73 75 62 2d opening the sub-
140dc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 journal file, th
140dd 65 6e 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 en an IO error c
140de 6f 64 65 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e ode is.** return
140df 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 ed. Otherwise, S
140e0 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c QLITE_OK..*/.SQL
140e1 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
140e2 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e sqlite3PagerOpen
140e3 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 Savepoint(Pager
140e4 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6e 53 61 *pPager, int nSa
140e5 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 vepoint){. int
140e6 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
140e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140e8 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
140e9 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 code */. int nC
140ea 75 72 72 65 6e 74 20 3d 20 70 50 61 67 65 72 2d urrent = pPager-
140eb 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 >nSavepoint;
140ec 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e /* Current n
140ed 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69 umber of savepoi
140ee 6e 74 73 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 53 nts */.. if( nS
140ef 61 76 65 70 6f 69 6e 74 3e 6e 43 75 72 72 65 6e avepoint>nCurren
140f0 74 20 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65 t && pPager->use
140f1 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 69 Journal ){. i
140f2 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 nt ii;
140f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140f4 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 /* Iterat
140f5 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 or variable */.
140f6 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e PagerSavepoin
140f7 74 20 2a 61 4e 65 77 3b 20 20 20 20 20 20 20 20 t *aNew;
140f8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 /* Ne
140f9 77 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 w Pager.aSavepoi
140fa 6e 74 20 61 72 72 61 79 20 2a 2f 0a 0a 20 20 20 nt array */..
140fb 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65 72 65 /* Either there
140fc 20 69 73 20 6e 6f 20 61 63 74 69 76 65 20 6a 6f is no active jo
140fd 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 73 75 62 urnal or the sub
140fe 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e -journal is open
140ff 20 6f 72 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 or . ** the
14100 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 77 61 79 journal is alway
14101 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f s stored in memo
14102 72 79 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 ry */. assert
14103 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 ( pPager->nSavep
14104 6f 69 6e 74 3d 3d 30 20 7c 7c 20 69 73 4f 70 65 oint==0 || isOpe
14105 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 n(pPager->sjfd)
14106 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ||. p
14107 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
14108 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
14109 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a LMODE_MEMORY );.
1410a 0a 20 20 20 20 2f 2a 20 47 72 6f 77 20 74 68 65 . /* Grow the
1410b 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e Pager.aSavepoin
1410c 74 20 61 72 72 61 79 20 75 73 69 6e 67 20 72 65 t array using re
1410d 61 6c 6c 6f 63 28 29 2e 20 52 65 74 75 72 6e 20 alloc(). Return
1410e 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 20 SQLITE_NOMEM.
1410f 20 2a 2a 20 69 66 20 74 68 65 20 61 6c 6c 6f 63 ** if the alloc
14110 61 74 69 6f 6e 20 66 61 69 6c 73 2e 20 4f 74 68 ation fails. Oth
14111 65 72 77 69 73 65 2c 20 7a 65 72 6f 20 74 68 65 erwise, zero the
14112 20 6e 65 77 20 70 6f 72 74 69 6f 6e 20 69 6e 20 new portion in
14113 63 61 73 65 20 61 20 0a 20 20 20 20 2a 2a 20 6d case a . ** m
14114 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 alloc failure oc
14115 63 75 72 73 20 77 68 69 6c 65 20 70 6f 70 75 6c curs while popul
14116 61 74 69 6e 67 20 69 74 20 69 6e 20 74 68 65 20 ating it in the
14117 66 6f 72 28 2e 2e 2e 29 20 6c 6f 6f 70 20 62 65 for(...) loop be
14118 6c 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 low.. */.
14119 61 4e 65 77 20 3d 20 28 50 61 67 65 72 53 61 76 aNew = (PagerSav
1411a 65 70 6f 69 6e 74 20 2a 29 73 71 6c 69 74 65 33 epoint *)sqlite3
1411b 52 65 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20 20 Realloc(.
1411c 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f pPager->aSavepo
1411d 69 6e 74 2c 20 73 69 7a 65 6f 66 28 50 61 67 65 int, sizeof(Page
1411e 72 53 61 76 65 70 6f 69 6e 74 29 2a 6e 53 61 76 rSavepoint)*nSav
1411f 65 70 6f 69 6e 74 0a 20 20 20 20 29 3b 0a 20 20 epoint. );.
14120 20 20 69 66 28 20 21 61 4e 65 77 20 29 7b 0a 20 if( !aNew ){.
14121 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
14122 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
14123 20 20 20 20 6d 65 6d 73 65 74 28 26 61 4e 65 77 memset(&aNew
14124 5b 6e 43 75 72 72 65 6e 74 5d 2c 20 30 2c 20 28 [nCurrent], 0, (
14125 6e 53 61 76 65 70 6f 69 6e 74 2d 6e 43 75 72 72 nSavepoint-nCurr
14126 65 6e 74 29 20 2a 20 73 69 7a 65 6f 66 28 50 61 ent) * sizeof(Pa
14127 67 65 72 53 61 76 65 70 6f 69 6e 74 29 29 3b 0a gerSavepoint));.
14128 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 pPager->aSav
14129 65 70 6f 69 6e 74 20 3d 20 61 4e 65 77 3b 0a 20 epoint = aNew;.
1412a 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 pPager->nSave
1412b 70 6f 69 6e 74 20 3d 20 6e 53 61 76 65 70 6f 69 point = nSavepoi
1412c 6e 74 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 nt;.. /* Popu
1412d 6c 61 74 65 20 74 68 65 20 50 61 67 65 72 53 61 late the PagerSa
1412e 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 vepoint structur
1412f 65 73 20 6a 75 73 74 20 61 6c 6c 6f 63 61 74 65 es just allocate
14130 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 69 d. */. for(ii
14131 3d 6e 43 75 72 72 65 6e 74 3b 20 69 69 3c 6e 53 =nCurrent; ii<nS
14132 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b avepoint; ii++){
14133 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
14134 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c Pager->dbSizeVal
14135 69 64 20 29 3b 0a 20 20 20 20 20 20 61 4e 65 77 id );. aNew
14136 5b 69 69 5d 2e 6e 4f 72 69 67 20 3d 20 70 50 61 [ii].nOrig = pPa
14137 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 ger->dbSize;.
14138 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 if( isOpen(pP
14139 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 41 4c ager->jfd) && AL
1413a 57 41 59 53 28 70 50 61 67 65 72 2d 3e 6a 6f 75 WAYS(pPager->jou
1413b 72 6e 61 6c 4f 66 66 3e 30 29 20 29 7b 0a 20 20 rnalOff>0) ){.
1413c 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 aNew[ii].i
1413d 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d Offset = pPager-
1413e 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 >journalOff;.
1413f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
14140 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73 aNew[ii].iOffs
14141 65 74 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 et = JOURNAL_HDR
14142 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20 _SZ(pPager);.
14143 20 20 20 7d 0a 20 20 20 20 20 20 61 4e 65 77 5b }. aNew[
14144 69 69 5d 2e 69 53 75 62 52 65 63 20 3d 20 70 50 ii].iSubRec = pP
14145 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3b 0a 20 ager->nSubRec;.
14146 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 70 49 aNew[ii].pI
14147 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 73 71 6c nSavepoint = sql
14148 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 ite3BitvecCreate
14149 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 (pPager->dbSize)
1414a 3b 0a 20 20 20 20 20 20 69 66 28 20 21 61 4e 65 ;. if( !aNe
1414b 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 w[ii].pInSavepoi
1414c 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 nt ){. re
1414d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
1414e 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d M;. }. }
1414f 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 .. /* Open th
14150 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2c 20 69 e sub-journal, i
14151 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 f it is not alre
14152 61 64 79 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 ady opened. */.
14153 20 20 20 72 63 20 3d 20 6f 70 65 6e 53 75 62 4a rc = openSubJ
14154 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a ournal(pPager);.
14155 20 20 20 20 61 73 73 65 72 74 54 72 75 6e 63 61 assertTrunca
14156 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 teConstraint(pPa
14157 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 ger);. }.. ret
14158 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
14159 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
1415a 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 6f 6c 6c s called to roll
1415b 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 73 65 20 back or release
1415c 28 63 6f 6d 6d 69 74 29 20 61 20 73 61 76 65 70 (commit) a savep
1415d 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 61 76 oint..** The sav
1415e 65 70 6f 69 6e 74 20 74 6f 20 72 65 6c 65 61 73 epoint to releas
1415f 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 e or rollback ne
14160 65 64 20 6e 6f 74 20 62 65 20 74 68 65 20 6d 6f ed not be the mo
14161 73 74 20 72 65 63 65 6e 74 6c 79 20 0a 2a 2a 20 st recently .**
14162 63 72 65 61 74 65 64 20 73 61 76 65 70 6f 69 6e created savepoin
14163 74 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 t..**.** Paramet
14164 65 72 20 6f 70 20 69 73 20 61 6c 77 61 79 73 20 er op is always
14165 65 69 74 68 65 72 20 53 41 56 45 50 4f 49 4e 54 either SAVEPOINT
14166 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72 20 53 41 56 _ROLLBACK or SAV
14167 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 0a EPOINT_RELEASE..
14168 2a 2a 20 49 66 20 69 74 20 69 73 20 53 41 56 45 ** If it is SAVE
14169 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74 POINT_RELEASE, t
1416a 68 65 6e 20 72 65 6c 65 61 73 65 20 61 6e 64 20 hen release and
1416b 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 76 65 destroy the save
1416c 70 6f 69 6e 74 20 77 69 74 68 0a 2a 2a 20 69 6e point with.** in
1416d 64 65 78 20 69 53 61 76 65 70 6f 69 6e 74 2e 20 dex iSavepoint.
1416e 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f 49 If it is SAVEPOI
1416f 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 NT_ROLLBACK, the
14170 6e 20 72 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 n rollback all c
14171 68 61 6e 67 65 73 0a 2a 2a 20 74 68 61 74 20 68 hanges.** that h
14172 61 76 65 20 6f 63 63 75 72 72 65 64 20 73 69 6e ave occurred sin
14173 63 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64 ce the specified
14174 20 73 61 76 65 70 6f 69 6e 74 20 77 61 73 20 63 savepoint was c
14175 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 reated..**.** Th
14176 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f 20 72 e savepoint to r
14177 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 ollback or relea
14178 73 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 se is identified
14179 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 0a 2a by parameter .*
1417a 2a 20 69 53 61 76 65 70 6f 69 6e 74 2e 20 41 20 * iSavepoint. A
1417b 76 61 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e 73 value of 0 means
1417c 20 74 6f 20 6f 70 65 72 61 74 65 20 6f 6e 20 74 to operate on t
1417d 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 he outermost sav
1417e 65 70 6f 69 6e 74 0a 2a 2a 20 28 74 68 65 20 66 epoint.** (the f
1417f 69 72 73 74 20 63 72 65 61 74 65 64 29 2e 20 41 irst created). A
14180 20 76 61 6c 75 65 20 6f 66 20 28 50 61 67 65 72 value of (Pager
14181 2e 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 20 6d .nSavepoint-1) m
14182 65 61 6e 73 20 6f 70 65 72 61 74 65 0a 2a 2a 20 eans operate.**
14183 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 on the most rece
14184 6e 74 6c 79 20 63 72 65 61 74 65 64 20 73 61 76 ntly created sav
14185 65 70 6f 69 6e 74 2e 20 49 66 20 69 53 61 76 65 epoint. If iSave
14186 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65 72 point is greater
14187 20 74 68 61 6e 0a 2a 2a 20 28 50 61 67 65 72 2e than.** (Pager.
14188 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 2c 20 74 nSavepoint-1), t
14189 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
1418a 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a n is a no-op..**
1418b 0a 2a 2a 20 49 66 20 61 20 6e 65 67 61 74 69 76 .** If a negativ
1418c 65 20 76 61 6c 75 65 20 69 73 20 70 61 73 73 65 e value is passe
1418d 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 d to this functi
1418e 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 on, then the cur
1418f 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 rent.** transact
14190 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 ion is rolled ba
14191 63 6b 2e 20 54 68 69 73 20 69 73 20 64 69 66 66 ck. This is diff
14192 65 72 65 6e 74 20 74 6f 20 63 61 6c 6c 69 6e 67 erent to calling
14193 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 .** sqlite3Page
14194 72 52 6f 6c 6c 62 61 63 6b 28 29 20 62 65 63 61 rRollback() beca
14195 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f use this functio
14196 6e 20 64 6f 65 73 20 6e 6f 74 20 74 65 72 6d 69 n does not termi
14197 6e 61 74 65 0a 2a 2a 20 74 68 65 20 74 72 61 6e nate.** the tran
14198 73 61 63 74 69 6f 6e 20 6f 72 20 75 6e 6c 6f 63 saction or unloc
14199 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 k the database,
1419a 69 74 20 6a 75 73 74 20 72 65 73 74 6f 72 65 73 it just restores
1419b 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 the .** content
1419c 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 s of the databas
1419d 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 e to its origina
1419e 6c 20 73 74 61 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 l state. .**.**
1419f 49 6e 20 61 6e 79 20 63 61 73 65 2c 20 61 6c 6c In any case, all
141a0 20 73 61 76 65 70 6f 69 6e 74 73 20 77 69 74 68 savepoints with
141a1 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65 an index greate
141a2 72 20 74 68 61 6e 20 69 53 61 76 65 70 6f 69 6e r than iSavepoin
141a3 74 20 0a 2a 2a 20 61 72 65 20 64 65 73 74 72 6f t .** are destro
141a4 79 65 64 2e 20 49 66 20 74 68 69 73 20 69 73 20 yed. If this is
141a5 61 20 72 65 6c 65 61 73 65 20 6f 70 65 72 61 74 a release operat
141a6 69 6f 6e 20 28 6f 70 3d 3d 53 41 56 45 50 4f 49 ion (op==SAVEPOI
141a7 4e 54 5f 52 45 4c 45 41 53 45 29 2c 0a 2a 2a 20 NT_RELEASE),.**
141a8 74 68 65 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 then savepoint i
141a9 53 61 76 65 70 6f 69 6e 74 20 69 73 20 61 6c 73 Savepoint is als
141aa 6f 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2a 0a o destroyed..**.
141ab 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
141ac 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 may return SQLI
141ad 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 65 TE_NOMEM if a me
141ae 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
141af 66 61 69 6c 73 2c 0a 2a 2a 20 6f 72 20 61 6e 20 fails,.** or an
141b0 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 IO error code if
141b1 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 an IO error occ
141b2 75 72 73 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e urs while rollin
141b3 67 20 62 61 63 6b 20 61 20 0a 2a 2a 20 73 61 76 g back a .** sav
141b4 65 70 6f 69 6e 74 2e 20 49 66 20 6e 6f 20 65 72 epoint. If no er
141b5 72 6f 72 73 20 6f 63 63 75 72 2c 20 53 51 4c 49 rors occur, SQLI
141b6 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
141b7 64 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 d..*/ .SQLITE_PR
141b8 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
141b9 33 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 3PagerSavepoint(
141ba 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
141bb 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 nt op, int iSave
141bc 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 point){. int rc
141bd 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
141be 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56 assert( op==SAV
141bf 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c EPOINT_RELEASE |
141c0 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f | op==SAVEPOINT_
141c1 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 61 73 ROLLBACK );. as
141c2 73 65 72 74 28 20 69 53 61 76 65 70 6f 69 6e 74 sert( iSavepoint
141c3 3e 3d 30 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 >=0 || op==SAVEP
141c4 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b OINT_ROLLBACK );
141c5 0a 0a 20 20 69 66 28 20 69 53 61 76 65 70 6f 69 .. if( iSavepoi
141c6 6e 74 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 nt<pPager->nSave
141c7 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 point ){. int
141c8 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 ii;
141c9 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 /* Iterator vari
141ca 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 able */. int
141cb 6e 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 2f nNew; /
141cc 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 6d 61 * Number of rema
141cd 69 6e 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 73 ining savepoints
141ce 20 61 66 74 65 72 20 74 68 69 73 20 6f 70 2e 20 after this op.
141cf 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 */.. /* Figur
141d0 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 73 e out how many s
141d1 61 76 65 70 6f 69 6e 74 73 20 77 69 6c 6c 20 73 avepoints will s
141d2 74 69 6c 6c 20 62 65 20 61 63 74 69 76 65 20 61 till be active a
141d3 66 74 65 72 20 74 68 69 73 0a 20 20 20 20 2a 2a fter this. **
141d4 20 6f 70 65 72 61 74 69 6f 6e 2e 20 53 74 6f 72 operation. Stor
141d5 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 6e 20 e this value in
141d6 6e 4e 65 77 2e 20 54 68 65 6e 20 66 72 65 65 20 nNew. Then free
141d7 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 resources associ
141d8 61 74 65 64 20 0a 20 20 20 20 2a 2a 20 77 69 74 ated . ** wit
141d9 68 20 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 h any savepoints
141da 20 74 68 61 74 20 61 72 65 20 64 65 73 74 72 6f that are destro
141db 79 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72 yed by this oper
141dc 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 ation.. */.
141dd 20 20 6e 4e 65 77 20 3d 20 69 53 61 76 65 70 6f nNew = iSavepo
141de 69 6e 74 20 2b 20 28 6f 70 3d 3d 53 41 56 45 50 int + (op==SAVEP
141df 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 3b 0a OINT_ROLLBACK);.
141e0 20 20 20 20 66 6f 72 28 69 69 3d 6e 4e 65 77 3b for(ii=nNew;
141e1 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 ii<pPager->nSav
141e2 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 epoint; ii++){.
141e3 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 sqlite3Bitv
141e4 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 ecDestroy(pPager
141e5 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d ->aSavepoint[ii]
141e6 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a .pInSavepoint);.
141e7 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 }. pPager
141e8 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e ->nSavepoint = n
141e9 4e 65 77 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 New;.. /* If
141ea 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c 62 61 this is a rollba
141eb 63 6b 20 6f 70 65 72 61 74 69 6f 6e 2c 20 70 6c ck operation, pl
141ec 61 79 62 61 63 6b 20 74 68 65 20 73 70 65 63 69 ayback the speci
141ed 66 69 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 0a fied savepoint..
141ee 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 ** If this i
141ef 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 2c 20 69 s a temp-file, i
141f0 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68 t is possible th
141f1 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 at the journal f
141f2 69 6c 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e ile has. ** n
141f3 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e ot yet been open
141f4 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
141f5 20 74 68 65 72 65 20 68 61 76 65 20 62 65 65 6e there have been
141f6 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 0a 20 no changes to.
141f7 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 ** the databa
141f8 73 65 20 66 69 6c 65 2c 20 73 6f 20 74 68 65 20 se file, so the
141f9 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74 69 playback operati
141fa 6f 6e 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65 on can be skippe
141fb 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 d.. */. if
141fc 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f ( op==SAVEPOINT_
141fd 52 4f 4c 4c 42 41 43 4b 20 26 26 20 69 73 4f 70 ROLLBACK && isOp
141fe 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
141ff 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72 53 61 ){. PagerSa
14200 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f vepoint *pSavepo
14201 69 6e 74 20 3d 20 28 6e 4e 65 77 3d 3d 30 29 3f int = (nNew==0)?
14202 30 3a 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 0:&pPager->aSave
14203 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b 0a 20 point[nNew-1];.
14204 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 50 rc = pagerP
14205 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 laybackSavepoint
14206 28 70 50 61 67 65 72 2c 20 70 53 61 76 65 70 6f (pPager, pSavepo
14207 69 6e 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 int);. asse
14208 72 74 28 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f rt(rc!=SQLITE_DO
14209 4e 45 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 NE);. }. .
1420a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
1420b 61 20 72 65 6c 65 61 73 65 20 6f 66 20 74 68 65 a release of the
1420c 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 65 70 outermost savep
1420d 6f 69 6e 74 2c 20 74 72 75 6e 63 61 74 65 20 0a oint, truncate .
1420e 20 20 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a ** the sub-j
1420f 6f 75 72 6e 61 6c 20 74 6f 20 7a 65 72 6f 20 62 ournal to zero b
14210 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 2a 2f ytes in size. */
14211 0a 20 20 20 20 69 66 28 20 6e 4e 65 77 3d 3d 30 . if( nNew==0
14212 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e && op==SAVEPOIN
14213 54 5f 52 45 4c 45 41 53 45 20 26 26 20 69 73 4f T_RELEASE && isO
14214 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 pen(pPager->sjfd
14215 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 ) ){. asser
14216 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
14217 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 );. rc = s
14218 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 qlite3OsTruncate
14219 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 30 (pPager->sjfd, 0
1421a 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d );. pPager-
1421b 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b 0a 20 20 >nSubRec = 0;.
1421c 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1421d 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
1421e 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 turn the full pa
1421f 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 thname of the da
14220 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a tabase file..*/.
14221 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
14222 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
14223 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 e3PagerFilename(
14224 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
14225 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
14226 3e 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f >zFilename;.}../
14227 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
14228 56 46 53 20 73 74 72 75 63 74 75 72 65 20 66 6f VFS structure fo
14229 72 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a r the pager..*/.
1422a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
1422b 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73 onst sqlite3_vfs
1422c 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 56 66 *sqlite3PagerVf
1422d 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 s(Pager *pPager)
1422e 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 {. return pPage
1422f 72 2d 3e 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a r->pVfs;.}../*.*
14230 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 69 6c * Return the fil
14231 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 e handle for the
14232 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
14233 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 ssociated.** wit
14234 68 20 74 68 65 20 70 61 67 65 72 2e 20 20 54 68 h the pager. Th
14235 69 73 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 is might return
14236 4e 55 4c 4c 20 69 66 20 74 68 65 20 66 69 6c 65 NULL if the file
14237 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 79 65 74 20 has.** not yet
14238 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a been opened..*/.
14239 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 SQLITE_PRIVATE s
1423a 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c qlite3_file *sql
1423b 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 ite3PagerFile(Pa
1423c 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
1423d 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 66 return pPager->f
1423e 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 d;.}../*.** Retu
1423f 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 rn the full path
14240 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 name of the jour
14241 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c nal file..*/.SQL
14242 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
14243 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 t char *sqlite3P
14244 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 agerJournalname(
14245 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
14246 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
14247 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a 7d 0a 0a 2f 2a >zJournal;.}../*
14248 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
14249 69 66 20 66 73 79 6e 63 28 29 20 63 61 6c 6c 73 if fsync() calls
1424a 20 61 72 65 20 64 69 73 61 62 6c 65 64 20 66 6f are disabled fo
1424b 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 20 52 r this pager. R
1424c 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 eturn FALSE.** i
1424d 66 20 66 73 79 6e 63 28 29 73 20 61 72 65 20 65 f fsync()s are e
1424e 78 65 63 75 74 65 64 20 6e 6f 72 6d 61 6c 6c 79 xecuted normally
1424f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
14250 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
14251 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 agerNosync(Pager
14252 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 *pPager){. ret
14253 75 72 6e 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 urn pPager->noSy
14254 6e 63 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 nc;.}..#ifdef SQ
14255 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 2f LITE_HAS_CODEC./
14256 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 72 65 74 72 *.** Set or retr
14257 69 65 76 65 20 74 68 65 20 63 6f 64 65 63 20 66 ieve the codec f
14258 6f 72 20 74 68 69 73 20 70 61 67 65 72 0a 2a 2f or this pager.*/
14259 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c .static void sql
1425a 69 74 65 33 50 61 67 65 72 53 65 74 43 6f 64 65 ite3PagerSetCode
1425b 63 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 c(. Pager *pPag
1425c 65 72 2c 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43 er,. void *(*xC
1425d 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64 odec)(void*,void
1425e 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c 0a 20 20 76 *,Pgno,int),. v
1425f 6f 69 64 20 28 2a 78 43 6f 64 65 63 53 69 7a 65 oid (*xCodecSize
14260 43 68 6e 67 29 28 76 6f 69 64 2a 2c 69 6e 74 2c Chng)(void*,int,
14261 69 6e 74 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 int),. void (*x
14262 43 6f 64 65 63 46 72 65 65 29 28 76 6f 69 64 2a CodecFree)(void*
14263 29 2c 0a 20 20 76 6f 69 64 20 2a 70 43 6f 64 65 ),. void *pCode
14264 63 0a 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 c.){. if( pPage
14265 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 20 29 20 r->xCodecFree )
14266 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 pPager->xCodecFr
14267 65 65 28 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 ee(pPager->pCode
14268 63 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 c);. pPager->xC
14269 6f 64 65 63 20 3d 20 78 43 6f 64 65 63 3b 0a 20 odec = xCodec;.
1426a 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 53 pPager->xCodecS
1426b 69 7a 65 43 68 6e 67 20 3d 20 78 43 6f 64 65 63 izeChng = xCodec
1426c 53 69 7a 65 43 68 6e 67 3b 0a 20 20 70 50 61 67 SizeChng;. pPag
1426d 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 20 3d er->xCodecFree =
1426e 20 78 43 6f 64 65 63 46 72 65 65 3b 0a 20 20 70 xCodecFree;. p
1426f 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 20 3d 20 Pager->pCodec =
14270 70 43 6f 64 65 63 3b 0a 20 20 70 61 67 65 72 52 pCodec;. pagerR
14271 65 70 6f 72 74 53 69 7a 65 28 70 50 61 67 65 72 eportSize(pPager
14272 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 );.}.static void
14273 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *sqlite3PagerGe
14274 74 43 6f 64 65 63 28 50 61 67 65 72 20 2a 70 50 tCodec(Pager *pP
14275 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
14276 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 3b 0a pPager->pCodec;.
14277 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 }.#endif..#ifnde
14278 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
14279 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 4d TOVACUUM./*.** M
1427a 6f 76 65 20 74 68 65 20 70 61 67 65 20 70 50 67 ove the page pPg
1427b 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 70 67 6e to location pgn
1427c 6f 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a o in the file..*
1427d 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 *.** There must
1427e 62 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 73 be no references
1427f 20 74 6f 20 74 68 65 20 70 61 67 65 20 70 72 65 to the page pre
14280 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 20 viously located
14281 61 74 0a 2a 2a 20 70 67 6e 6f 20 28 77 68 69 63 at.** pgno (whic
14282 68 20 77 65 20 63 61 6c 6c 20 70 50 67 4f 6c 64 h we call pPgOld
14283 29 20 74 68 6f 75 67 68 20 74 68 61 74 20 70 61 ) though that pa
14284 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f ge is allowed to
14285 20 62 65 0a 2a 2a 20 69 6e 20 63 61 63 68 65 2e be.** in cache.
14286 20 20 49 66 20 74 68 65 20 70 61 67 65 20 70 72 If the page pr
14287 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 eviously located
14288 20 61 74 20 70 67 6e 6f 20 69 73 20 6e 6f 74 20 at pgno is not
14289 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 74 68 already.** in th
1428a 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
1428b 61 6c 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 75 al, it is not pu
1428c 74 20 74 68 65 72 65 20 62 79 20 62 79 20 74 68 t there by by th
1428d 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a is routine..**.*
1428e 2a 20 52 65 66 65 72 65 6e 63 65 73 20 74 6f 20 * References to
1428f 74 68 65 20 70 61 67 65 20 70 50 67 20 72 65 6d the page pPg rem
14290 61 69 6e 20 76 61 6c 69 64 2e 20 55 70 64 61 74 ain valid. Updat
14291 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65 74 61 2d ing any.** meta-
14292 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 data associated
14293 77 69 74 68 20 70 50 67 20 28 69 2e 65 2e 20 64 with pPg (i.e. d
14294 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 ata stored in th
14295 65 20 6e 45 78 74 72 61 20 62 79 74 65 73 0a 2a e nExtra bytes.*
14296 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e * allocated alon
14297 67 20 77 69 74 68 20 74 68 65 20 70 61 67 65 29 g with the page)
14298 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69 is the responsi
14299 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 bility of the ca
1429a 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 ller..**.** A tr
1429b 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 ansaction must b
1429c 65 20 61 63 74 69 76 65 20 77 68 65 6e 20 74 68 e active when th
1429d 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
1429e 6c 6c 65 64 2e 20 49 74 20 75 73 65 64 20 74 6f lled. It used to
1429f 20 62 65 0a 2a 2a 20 72 65 71 75 69 72 65 64 20 be.** required
142a0 74 68 61 74 20 61 20 73 74 61 74 65 6d 65 6e 74 that a statement
142a1 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 transaction was
142a2 20 6e 6f 74 20 61 63 74 69 76 65 2c 20 62 75 74 not active, but
142a3 20 74 68 69 73 20 72 65 73 74 72 69 63 74 69 6f this restrictio
142a4 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 72 65 n.** has been re
142a5 6d 6f 76 65 64 20 28 43 52 45 41 54 45 20 49 4e moved (CREATE IN
142a6 44 45 58 20 6e 65 65 64 73 20 74 6f 20 6d 6f 76 DEX needs to mov
142a7 65 20 61 20 70 61 67 65 20 77 68 65 6e 20 61 20 e a page when a
142a8 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 statement.** tra
142a9 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 nsaction is acti
142aa 76 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ve)..**.** If th
142ab 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e e fourth argumen
142ac 74 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 69 73 20 t, isCommit, is
142ad 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 non-zero, then t
142ae 68 69 73 20 70 61 67 65 20 69 73 20 62 65 69 6e his page is bein
142af 67 0a 2a 2a 20 6d 6f 76 65 64 20 61 73 20 70 61 g.** moved as pa
142b0 72 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 rt of a database
142b1 20 72 65 6f 72 67 61 6e 69 7a 61 74 69 6f 6e 20 reorganization
142b2 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68 65 20 just before the
142b3 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 transaction .**
142b4 69 73 20 62 65 69 6e 67 20 63 6f 6d 6d 69 74 74 is being committ
142b5 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
142b6 2c 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 , it is guarante
142b7 65 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61 ed that the data
142b8 62 61 73 65 20 70 61 67 65 20 0a 2a 2a 20 70 50 base page .** pP
142b9 67 20 72 65 66 65 72 73 20 74 6f 20 77 69 6c 6c g refers to will
142ba 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e 20 not be written
142bb 74 6f 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 to again within
142bc 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e this transaction
142bd 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
142be 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e ction may return
142bf 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72 SQLITE_NOMEM or
142c0 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 an IO error cod
142c1 65 20 69 66 20 61 6e 20 65 72 72 6f 72 0a 2a 2a e if an error.**
142c2 20 6f 63 63 75 72 73 2e 20 4f 74 68 65 72 77 69 occurs. Otherwi
142c3 73 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 53 se, it returns S
142c4 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c QLITE_OK..*/.SQL
142c5 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
142c6 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 sqlite3PagerMove
142c7 70 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 page(Pager *pPag
142c8 65 72 2c 20 44 62 50 61 67 65 20 2a 70 50 67 2c er, DbPage *pPg,
142c9 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e 74 20 Pgno pgno, int
142ca 69 73 43 6f 6d 6d 69 74 29 7b 0a 20 20 50 67 48 isCommit){. PgH
142cb 64 72 20 2a 70 50 67 4f 6c 64 3b 20 20 20 20 20 dr *pPgOld;
142cc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
142cd 20 70 61 67 65 20 62 65 69 6e 67 20 6f 76 65 72 page being over
142ce 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 50 67 written. */. Pg
142cf 6e 6f 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 no needSyncPgno
142d0 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6c = 0; /* Ol
142d1 64 20 76 61 6c 75 65 20 6f 66 20 70 50 67 2d 3e d value of pPg->
142d2 70 67 6e 6f 2c 20 69 66 20 73 79 6e 63 20 69 73 pgno, if sync is
142d3 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 required */. i
142d4 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
142d5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
142d6 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
142d7 50 67 6e 6f 20 6f 72 69 67 50 67 6e 6f 3b 20 20 Pgno origPgno;
142d8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
142d9 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 The original pag
142da 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 61 e number */.. a
142db 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 ssert( pPg->nRef
142dc 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 >0 );.. /* If t
142dd 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 6d 6f he page being mo
142de 76 65 64 20 69 73 20 64 69 72 74 79 20 61 6e 64 ved is dirty and
142df 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 61 has not been sa
142e0 76 65 64 20 62 79 20 74 68 65 20 6c 61 74 65 73 ved by the lates
142e1 74 0a 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 t. ** savepoint
142e2 2c 20 74 68 65 6e 20 73 61 76 65 20 74 68 65 20 , then save the
142e3 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 current contents
142e4 20 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e 74 of the page int
142e5 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 2d o the . ** sub-
142e6 6a 6f 75 72 6e 61 6c 20 6e 6f 77 2e 20 54 68 69 journal now. Thi
142e7 73 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f s is required to
142e8 20 68 61 6e 64 6c 65 20 74 68 65 20 66 6f 6c 6c handle the foll
142e9 6f 77 69 6e 67 20 73 63 65 6e 61 72 69 6f 3a 0a owing scenario:.
142ea 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49 **. ** BEGI
142eb 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 N;. ** <jou
142ec 72 6e 61 6c 20 70 61 67 65 20 58 2c 20 74 68 65 rnal page X, the
142ed 6e 20 6d 6f 64 69 66 79 20 69 74 20 69 6e 20 6d n modify it in m
142ee 65 6d 6f 72 79 3e 0a 20 20 2a 2a 20 20 20 20 20 emory>. **
142ef 53 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20 SAVEPOINT one;.
142f0 20 2a 2a 20 20 20 20 20 20 20 3c 4d 6f 76 65 20 ** <Move
142f1 70 61 67 65 20 58 20 74 6f 20 6c 6f 63 61 74 69 page X to locati
142f2 6f 6e 20 59 3e 0a 20 20 2a 2a 20 20 20 20 20 52 on Y>. ** R
142f3 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 3b 0a OLLBACK TO one;.
142f4 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 61 67 **. ** If pag
142f5 65 20 58 20 77 65 72 65 20 6e 6f 74 20 77 72 69 e X were not wri
142f6 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62 2d tten to the sub-
142f7 6a 6f 75 72 6e 61 6c 20 68 65 72 65 2c 20 69 74 journal here, it
142f8 20 77 6f 75 6c 64 20 6e 6f 74 0a 20 20 2a 2a 20 would not. **
142f9 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 be possible to r
142fa 65 73 74 6f 72 65 20 69 74 73 20 63 6f 6e 74 65 estore its conte
142fb 6e 74 73 20 77 68 65 6e 20 74 68 65 20 22 52 4f nts when the "RO
142fc 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 22 0a 20 LLBACK TO one".
142fd 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 ** statement we
142fe 72 65 20 69 73 20 70 72 6f 63 65 73 73 65 64 2e re is processed.
142ff 0a 20 20 2a 2a 0a 20 20 2a 2a 20 73 75 62 6a 6f . **. ** subjo
14300 75 72 6e 61 6c 50 61 67 65 28 29 20 6d 61 79 20 urnalPage() may
14301 6e 65 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 need to allocate
14302 20 73 70 61 63 65 20 74 6f 20 73 74 6f 72 65 20 space to store
14303 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 74 6f 0a 20 pPg->pgno into.
14304 20 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 ** one or more
14305 73 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 savepoint bitvec
14306 73 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 72 s. This is the r
14307 65 61 73 6f 6e 20 74 68 69 73 20 66 75 6e 63 74 eason this funct
14308 69 6f 6e 0a 20 20 2a 2a 20 6d 61 79 20 72 65 74 ion. ** may ret
14309 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1430a 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 .. */. if( pPg
1430b 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
1430c 52 54 59 20 0a 20 20 20 26 26 20 73 75 62 6a 52 RTY . && subjR
1430d 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 equiresPage(pPg)
1430e 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b . && SQLITE_OK
1430f 21 3d 28 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e !=(rc = subjourn
14310 61 6c 50 61 67 65 28 70 50 67 29 29 0a 20 20 29 alPage(pPg)). )
14311 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
14312 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 . }.. PAGERTRA
14313 43 45 28 28 22 4d 4f 56 45 20 25 64 20 70 61 67 CE(("MOVE %d pag
14314 65 20 25 64 20 28 6e 65 65 64 53 79 6e 63 3d 25 e %d (needSync=%
14315 64 29 20 6d 6f 76 65 73 20 74 6f 20 25 64 5c 6e d) moves to %d\n
14316 22 2c 20 0a 20 20 20 20 20 20 50 41 47 45 52 49 ", . PAGERI
14317 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e D(pPager), pPg->
14318 70 67 6e 6f 2c 20 28 70 50 67 2d 3e 66 6c 61 67 pgno, (pPg->flag
14319 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s&PGHDR_NEED_SYN
1431a 43 29 3f 31 3a 30 2c 20 70 67 6e 6f 29 29 3b 0a C)?1:0, pgno));.
1431b 20 20 49 4f 54 52 41 43 45 28 28 22 4d 4f 56 45 IOTRACE(("MOVE
1431c 20 25 70 20 25 64 20 25 64 5c 6e 22 2c 20 70 50 %p %d %d\n", pP
1431d 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c ager, pPg->pgno,
1431e 20 70 67 6e 6f 29 29 0a 0a 20 20 2f 2a 20 49 66 pgno)).. /* If
1431f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 the journal nee
14320 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 ds to be sync()e
14321 64 20 62 65 66 6f 72 65 20 70 61 67 65 20 70 50 d before page pP
14322 67 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a 2a g->pgno can. **
14323 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 be written to,
14324 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f 20 store pPg->pgno
14325 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c in local variabl
14326 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e 0a e needSyncPgno..
14327 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 **. ** If the
14328 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 69 isCommit flag i
14329 73 20 73 65 74 2c 20 74 68 65 72 65 20 69 73 20 s set, there is
1432a 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d no need to remem
1432b 62 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 ber that. ** th
1432c 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 e journal needs
1432d 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20 62 to be sync()ed b
1432e 65 66 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 efore database p
1432f 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a 20 age pPg->pgno .
14330 20 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74 ** can be writt
14331 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 en to. The calle
14332 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 72 r has already pr
14333 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 omised not to wr
14334 69 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f 0a ite to it.. */.
14335 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67 if( (pPg->flag
14336 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s&PGHDR_NEED_SYN
14337 43 29 20 26 26 20 21 69 73 43 6f 6d 6d 69 74 20 C) && !isCommit
14338 29 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e 63 50 ){. needSyncP
14339 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b gno = pPg->pgno;
1433a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 67 . assert( pag
1433b 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 eInJournal(pPg)
1433c 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 || pPg->pgno>pPa
1433d 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 ger->dbOrigSize
1433e 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
1433f 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
14340 44 49 52 54 59 20 29 3b 0a 20 20 20 20 61 73 73 DIRTY );. ass
14341 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 ert( pPager->nee
14342 64 53 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a 20 20 dSync );. }..
14343 2f 2a 20 49 66 20 74 68 65 20 63 61 63 68 65 20 /* If the cache
14344 63 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 65 20 contains a page
14345 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65 72 with page-number
14346 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69 74 pgno, remove it
14347 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20 68 . ** from its h
14348 61 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f 2c ash chain. Also,
14349 20 69 66 20 74 68 65 20 50 67 48 64 72 2e 6e 65 if the PgHdr.ne
1434a 65 64 53 79 6e 63 20 77 61 73 20 73 65 74 20 66 edSync was set f
1434b 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 20 70 67 or . ** page pg
1434c 6e 6f 20 62 65 66 6f 72 65 20 74 68 65 20 27 6d no before the 'm
1434d 6f 76 65 27 20 6f 70 65 72 61 74 69 6f 6e 2c 20 ove' operation,
1434e 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 it needs to be r
1434f 65 74 61 69 6e 65 64 20 0a 20 20 2a 2a 20 66 6f etained . ** fo
14350 72 20 74 68 65 20 70 61 67 65 20 6d 6f 76 65 64 r the page moved
14351 20 74 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 70 there.. */. p
14352 50 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 Pg->flags &= ~PG
14353 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 HDR_NEED_SYNC;.
14354 20 70 50 67 4f 6c 64 20 3d 20 70 61 67 65 72 5f pPgOld = pager_
14355 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 lookup(pPager, p
14356 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 gno);. assert(
14357 21 70 50 67 4f 6c 64 20 7c 7c 20 70 50 67 4f 6c !pPgOld || pPgOl
14358 64 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 d->nRef==1 );.
14359 69 66 28 20 70 50 67 4f 6c 64 20 29 7b 0a 20 20 if( pPgOld ){.
1435a 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 pPg->flags |=
1435b 28 70 50 67 4f 6c 64 2d 3e 66 6c 61 67 73 26 50 (pPgOld->flags&P
1435c 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3b GHDR_NEED_SYNC);
1435d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 . sqlite3Pcac
1435e 68 65 44 72 6f 70 28 70 50 67 4f 6c 64 29 3b 0a heDrop(pPgOld);.
1435f 20 20 7d 0a 0a 20 20 6f 72 69 67 50 67 6e 6f 20 }.. origPgno
14360 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 73 = pPg->pgno;. s
14361 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 qlite3PcacheMove
14362 28 70 50 67 2c 20 70 67 6e 6f 29 3b 0a 20 20 73 (pPg, pgno);. s
14363 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
14364 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 70 50 Dirty(pPg);. pP
14365 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
14366 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e 65 65 = 1;.. if( nee
14367 64 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 20 20 dSyncPgno ){.
14368 20 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e 63 50 /* If needSyncP
14369 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c gno is non-zero,
1436a 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 then the journa
1436b 6c 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f 20 l file needs to
1436c 62 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e 63 28 be . ** sync(
1436d 29 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 64 )ed before any d
1436e 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 ata is written t
1436f 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 o database file
14370 70 61 67 65 20 6e 65 65 64 53 79 6e 63 50 67 6e page needSyncPgn
14371 6f 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 65 6e o.. ** Curren
14372 74 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 61 67 tly, no such pag
14373 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 e exists in the
14374 70 61 67 65 2d 63 61 63 68 65 20 61 6e 64 20 74 page-cache and t
14375 68 65 20 0a 20 20 20 20 2a 2a 20 22 69 73 20 6a he . ** "is j
14376 6f 75 72 6e 61 6c 65 64 22 20 62 69 74 76 65 63 ournaled" bitvec
14377 20 66 6c 61 67 20 68 61 73 20 62 65 65 6e 20 73 flag has been s
14378 65 74 2e 20 54 68 69 73 20 6e 65 65 64 73 20 74 et. This needs t
14379 6f 20 62 65 20 72 65 6d 65 64 69 65 64 20 62 79 o be remedied by
1437a 0a 20 20 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 20 . ** loading
1437b 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 the page into th
1437c 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e e pager-cache an
1437d 64 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 67 d setting the Pg
1437e 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20 20 Hdr.needSync .
1437f 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 20 20 2a ** flag.. *
14380 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 *. ** If the
14381 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64 20 attempt to load
14382 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 the page into th
14383 65 20 70 61 67 65 2d 63 61 63 68 65 20 66 61 69 e page-cache fai
14384 6c 73 2c 20 28 64 75 65 0a 20 20 20 20 2a 2a 20 ls, (due. **
14385 74 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 to a malloc() or
14386 20 49 4f 20 66 61 69 6c 75 72 65 29 2c 20 63 6c IO failure), cl
14387 65 61 72 20 74 68 65 20 62 69 74 20 69 6e 20 74 ear the bit in t
14388 68 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b 5d 0a he pInJournal[].
14389 20 20 20 20 2a 2a 20 61 72 72 61 79 2e 20 4f 74 ** array. Ot
1438a 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 herwise, if the
1438b 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 61 page is loaded a
1438c 6e 64 20 77 72 69 74 74 65 6e 20 61 67 61 69 6e nd written again
1438d 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 in. ** this
1438e 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 transaction, it
1438f 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e 20 74 may be written t
14390 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
14391 69 6c 65 20 62 65 66 6f 72 65 0a 20 20 20 20 2a ile before. *
14392 2a 20 69 74 20 69 73 20 73 79 6e 63 65 64 20 69 * it is synced i
14393 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 nto the journal
14394 66 69 6c 65 2e 20 54 68 69 73 20 77 61 79 2c 20 file. This way,
14395 69 74 20 6d 61 79 20 65 6e 64 20 75 70 20 69 6e it may end up in
14396 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 . ** the jour
14397 6e 61 6c 20 66 69 6c 65 20 74 77 69 63 65 2c 20 nal file twice,
14398 62 75 74 20 74 68 61 74 20 69 73 20 6e 6f 74 20 but that is not
14399 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20 20 2a a problem.. *
1439a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 71 6c *. ** The sql
1439b 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 63 ite3PagerGet() c
1439c 61 6c 6c 20 6d 61 79 20 63 61 75 73 65 20 74 68 all may cause th
1439d 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 79 6e e journal to syn
1439e 63 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20 20 2a c. So make. *
1439f 2a 20 73 75 72 65 20 74 68 65 20 50 61 67 65 72 * sure the Pager
143a0 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 .needSync flag i
143a1 73 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20 20 2a s set too.. *
143a2 2f 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 /. PgHdr *pPg
143a3 48 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 Hdr;. assert(
143a4 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
143a5 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 c );. rc = sq
143a6 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50 lite3PagerGet(pP
143a7 61 67 65 72 2c 20 6e 65 65 64 53 79 6e 63 50 67 ager, needSyncPg
143a8 6e 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 20 no, &pPgHdr);.
143a9 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
143aa 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 _OK ){. if(
143ab 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 3c 3d 70 needSyncPgno<=p
143ac 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a Pager->dbOrigSiz
143ad 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 e ){. ass
143ae 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 54 6d ert( pPager->pTm
143af 70 53 70 61 63 65 21 3d 30 20 29 3b 0a 20 20 20 pSpace!=0 );.
143b0 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 sqlite3Bitv
143b1 65 63 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e ecClear(pPager->
143b2 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64 pInJournal, need
143b3 53 79 6e 63 50 67 6e 6f 2c 20 70 50 61 67 65 72 SyncPgno, pPager
143b4 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 ->pTmpSpace);.
143b5 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 }. retu
143b6 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
143b7 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
143b8 63 20 3d 20 31 3b 0a 20 20 20 20 61 73 73 65 72 c = 1;. asser
143b9 74 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e t( pPager->noSyn
143ba 63 3d 3d 30 20 26 26 20 21 4d 45 4d 44 42 20 29 c==0 && !MEMDB )
143bb 3b 0a 20 20 20 20 70 50 67 48 64 72 2d 3e 66 6c ;. pPgHdr->fl
143bc 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 ags |= PGHDR_NEE
143bd 44 5f 53 59 4e 43 3b 0a 20 20 20 20 73 71 6c 69 D_SYNC;. sqli
143be 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 te3PcacheMakeDir
143bf 74 79 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20 ty(pPgHdr);.
143c0 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
143c1 66 28 70 50 67 48 64 72 29 3b 0a 20 20 7d 0a 0a f(pPgHdr);. }..
143c2 20 20 2f 2a 0a 20 20 2a 2a 20 46 6f 72 20 61 6e /*. ** For an
143c3 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
143c4 61 73 65 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 ase, make sure t
143c5 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 he original page
143c6 20 63 6f 6e 74 69 6e 75 65 73 0a 20 20 2a 2a 20 continues. **
143c7 74 6f 20 65 78 69 73 74 2c 20 69 6e 20 63 61 73 to exist, in cas
143c8 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f e the transactio
143c9 6e 20 6e 65 65 64 73 20 74 6f 20 72 6f 6c 6c 20 n needs to roll
143ca 62 61 63 6b 2e 20 20 57 65 20 61 6c 6c 6f 63 61 back. We alloca
143cb 74 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 te. ** the page
143cc 20 6e 6f 77 2c 20 69 6e 73 74 65 61 64 20 6f 66 now, instead of
143cd 20 61 74 20 72 6f 6c 6c 62 61 63 6b 2c 20 62 65 at rollback, be
143ce 63 61 75 73 65 20 77 65 20 63 61 6e 20 62 65 74 cause we can bet
143cf 74 65 72 20 64 65 61 6c 0a 20 20 2a 2a 20 77 69 ter deal. ** wi
143d0 74 68 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d th an out-of-mem
143d1 6f 72 79 20 65 72 72 6f 72 20 6e 6f 77 2e 20 20 ory error now.
143d2 54 69 63 6b 65 74 20 23 33 37 36 31 2e 0a 20 20 Ticket #3761..
143d3 2a 2f 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29 */. if( MEMDB )
143d4 7b 0a 20 20 20 20 44 62 50 61 67 65 20 2a 70 4e {. DbPage *pN
143d5 65 77 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ew;. rc = sql
143d6 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65 ite3PagerAcquire
143d7 28 70 50 61 67 65 72 2c 20 6f 72 69 67 50 67 6e (pPager, origPgn
143d8 6f 2c 20 26 70 4e 65 77 2c 20 31 29 3b 0a 20 20 o, &pNew, 1);.
143d9 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
143da 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
143db 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70 ite3PcacheMove(p
143dc 50 67 2c 20 6f 72 69 67 50 67 6e 6f 29 3b 0a 20 Pg, origPgno);.
143dd 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
143de 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
143df 33 50 61 67 65 72 55 6e 72 65 66 28 70 4e 65 77 3PagerUnref(pNew
143e0 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e );. }.. return
143e1 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 SQLITE_OK;.}.#e
143e2 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ndif../*.** Retu
143e3 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
143e4 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 the data for the
143e5 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 2e specified page.
143e6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
143e7 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
143e8 50 61 67 65 72 47 65 74 44 61 74 61 28 44 62 50 PagerGetData(DbP
143e9 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 61 73 73 age *pPg){. ass
143ea 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 ert( pPg->nRef>0
143eb 20 7c 7c 20 70 50 67 2d 3e 70 50 61 67 65 72 2d || pPg->pPager-
143ec 3e 6d 65 6d 44 62 20 29 3b 0a 20 20 72 65 74 75 >memDb );. retu
143ed 72 6e 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 7d rn pPg->pData;.}
143ee 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
143ef 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
143f0 50 61 67 65 72 2e 6e 45 78 74 72 61 20 62 79 74 Pager.nExtra byt
143f1 65 73 20 6f 66 20 22 65 78 74 72 61 22 20 73 70 es of "extra" sp
143f2 61 63 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 ace .** allocate
143f3 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 d along with the
143f4 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 2e specified page.
143f5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
143f6 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
143f7 50 61 67 65 72 47 65 74 45 78 74 72 61 28 44 62 PagerGetExtra(Db
143f8 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 Page *pPg){. re
143f9 74 75 72 6e 20 70 50 67 2d 3e 70 45 78 74 72 61 turn pPg->pExtra
143fa 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 ;.}../*.** Get/s
143fb 65 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d et the locking-m
143fc 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 ode for this pag
143fd 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d er. Parameter eM
143fe 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0a ode must be one.
143ff 2a 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b ** of PAGER_LOCK
14400 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50 INGMODE_QUERY, P
14401 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
14402 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 50 _NORMAL or .** P
14403 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
14404 5f 45 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74 _EXCLUSIVE. If t
14405 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 he parameter is
14406 6e 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e not _QUERY, then
14407 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d .** the locking-
14408 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 74 mode is set to t
14409 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 he value specifi
1440a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 ed..**.** The re
1440b 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 turned value is
1440c 65 69 74 68 65 72 20 50 41 47 45 52 5f 4c 4f 43 either PAGER_LOC
1440d 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 KINGMODE_NORMAL
1440e 6f 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b or.** PAGER_LOCK
1440f 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 INGMODE_EXCLUSIV
14410 45 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 E, indicating th
14411 65 20 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69 e current (possi
14412 62 6c 79 20 75 70 64 61 74 65 64 29 0a 2a 2a 20 bly updated).**
14413 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a 2f locking-mode..*/
14414 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14415 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
14416 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65 LockingMode(Page
14417 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 r *pPager, int e
14418 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28 Mode){. assert(
14419 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f eMode==PAGER_LO
1441a 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 0a CKINGMODE_QUERY.
1441b 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 || e
1441c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b Mode==PAGER_LOCK
1441d 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a 20 INGMODE_NORMAL.
1441e 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d || eM
1441f 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 ode==PAGER_LOCKI
14420 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 NGMODE_EXCLUSIVE
14421 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 );. assert( PA
14422 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
14423 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 73 QUERY<0 );. ass
14424 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 ert( PAGER_LOCKI
14425 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30 NGMODE_NORMAL>=0
14426 20 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e && PAGER_LOCKIN
14427 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3e GMODE_EXCLUSIVE>
14428 3d 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64 =0 );. if( eMod
14429 65 3e 3d 30 20 26 26 20 21 70 50 61 67 65 72 2d e>=0 && !pPager-
1442a 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 >tempFile ){.
1442b 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 pPager->exclusi
1442c 76 65 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f veMode = (u8)eMo
1442d 64 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e de;. }. return
1442e 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 78 (int)pPager->ex
1442f 63 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a 0a clusiveMode;.}..
14430 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 /*.** Get/set th
14431 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 66 e journal-mode f
14432 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 50 or this pager. P
14433 61 72 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d arameter eMode m
14434 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a 2a ust be one of:.*
14435 2a 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f *.** PAGER_JO
14436 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a URNALMODE_QUERY.
14437 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 ** PAGER_JOUR
14438 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 2a NALMODE_DELETE.*
14439 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e * PAGER_JOURN
1443a 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a ALMODE_TRUNCATE.
1443b 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 ** PAGER_JOUR
1443c 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a NALMODE_PERSIST.
1443d 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 ** PAGER_JOUR
1443e 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 20 20 NALMODE_OFF.**
1443f 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d PAGER_JOURNALM
14440 4f 44 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a 2a 2a ODE_MEMORY.**.**
14441 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 If the paramete
14442 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c r is not _QUERY,
14443 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 then the journa
14444 6c 5f 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f l_mode is set to
14445 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73 70 the.** value sp
14446 65 63 69 66 69 65 64 20 69 66 20 74 68 65 20 63 ecified if the c
14447 68 61 6e 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 hange is allowed
14448 2e 20 20 54 68 65 20 63 68 61 6e 67 65 20 69 73 . The change is
14449 20 64 69 73 61 6c 6c 6f 77 65 64 0a 2a 2a 20 66 disallowed.** f
1444a 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 or the following
1444b 20 72 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 reasons:.**.**
1444c 20 20 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 * An in-memor
1444d 79 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 6f y database can o
1444e 6e 6c 79 20 68 61 76 65 20 69 74 73 20 6a 6f 75 nly have its jou
1444f 72 6e 61 6c 5f 6d 6f 64 65 20 73 65 74 20 74 6f rnal_mode set to
14450 20 5f 4f 46 46 0a 2a 2a 20 20 20 20 20 20 6f 72 _OFF.** or
14451 20 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a 20 _MEMORY..**.**
14452 20 20 2a 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c * The journal
14453 20 6d 6f 64 65 20 6d 61 79 20 6e 6f 74 20 62 65 mode may not be
14454 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 61 changed while a
14455 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
14456 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 active..**.** Th
14457 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63 e returned indic
14458 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ate the current
14459 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 (possibly update
1445a 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e d) journal-mode.
1445b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1445c 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
1445d 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 gerJournalMode(P
1445e 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
1445f 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 t eMode){. asse
14460 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 rt( eMode==PAGER
14461 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 _JOURNALMODE_QUE
14462 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c RY. |
14463 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a | eMode==PAGER_J
14464 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 OURNALMODE_DELET
14465 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c E. ||
14466 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f eMode==PAGER_JO
14467 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 URNALMODE_TRUNCA
14468 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c TE. |
14469 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a | eMode==PAGER_J
1446a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 OURNALMODE_PERSI
1446b 53 54 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c ST. |
1446c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a | eMode==PAGER_J
1446d 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a OURNALMODE_OFF .
1446e 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 || e
1446f 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
14470 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 NALMODE_MEMORY )
14471 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 ;. assert( PAGE
14472 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 R_JOURNALMODE_QU
14473 45 52 59 3c 30 20 29 3b 0a 20 20 69 66 28 20 65 ERY<0 );. if( e
14474 4d 6f 64 65 3e 3d 30 0a 20 20 20 26 26 20 28 21 Mode>=0. && (!
14475 4d 45 4d 44 42 20 7c 7c 20 65 4d 6f 64 65 3d 3d MEMDB || eMode==
14476 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
14477 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 E_MEMORY .
14478 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 || eMode
14479 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
1447a 4f 44 45 5f 4f 46 46 29 0a 20 20 20 26 26 20 21 ODE_OFF). && !
1447b 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 pPager->dbModifi
1447c 65 64 0a 20 20 20 26 26 20 28 21 69 73 4f 70 65 ed. && (!isOpe
1447d 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c n(pPager->jfd) |
1447e 7c 20 30 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 | 0==pPager->jou
1447f 72 6e 61 6c 4f 66 66 29 0a 20 20 29 7b 0a 20 20 rnalOff). ){.
14480 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 if( isOpen(pPa
14481 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 ger->jfd) ){.
14482 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 sqlite3OsClos
14483 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a e(pPager->jfd);.
14484 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 }. pPager
14485 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 ->journalMode =
14486 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 (u8)eMode;. }.
14487 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 50 61 return (int)pPa
14488 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
14489 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 ;.}../*.** Get/s
1448a 65 74 20 74 68 65 20 73 69 7a 65 2d 6c 69 6d 69 et the size-limi
1448b 74 20 75 73 65 64 20 66 6f 72 20 70 65 72 73 69 t used for persi
1448c 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 stent journal fi
1448d 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 les..**.** Setti
1448e 6e 67 20 74 68 65 20 73 69 7a 65 20 6c 69 6d 69 ng the size limi
1448f 74 20 74 6f 20 2d 31 20 6d 65 61 6e 73 20 6e 6f t to -1 means no
14490 20 6c 69 6d 69 74 20 69 73 20 65 6e 66 6f 72 63 limit is enforc
14491 65 64 2e 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70 ed..** An attemp
14492 74 20 74 6f 20 73 65 74 20 61 20 6c 69 6d 69 74 t to set a limit
14493 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 2d 31 smaller than -1
14494 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
14495 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
14496 36 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 64 sqlite3PagerJ
14497 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 ournalSizeLimit(
14498 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
14499 36 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69 66 64 iLimit){. if
1449a 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b 0a ( iLimit>=-1 ){.
1449b 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
1449c 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 69 nalSizeLimit = i
1449d 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74 Limit;. }. ret
1449e 75 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 urn pPager->jour
1449f 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a nalSizeLimit;.}.
144a0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
144a1 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 pointer to the p
144a2 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 76 Pager->pBackup v
144a3 61 72 69 61 62 6c 65 2e 20 54 68 65 20 62 61 63 ariable. The bac
144a4 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69 6e kup module.** in
144a5 20 62 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 61 backup.c mainta
144a6 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 ins the content
144a7 6f 66 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 of this variable
144a8 2e 20 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a 2a . This module.**
144a9 20 75 73 65 73 20 69 74 20 6f 70 61 71 75 65 6c uses it opaquel
144aa 79 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 y as an argument
144ab 20 74 6f 20 73 71 6c 69 74 65 33 42 61 63 6b 75 to sqlite3Backu
144ac 70 52 65 73 74 61 72 74 28 29 20 61 6e 64 0a 2a pRestart() and.*
144ad 2a 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 * sqlite3BackupU
144ae 70 64 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a 2f pdate() only..*/
144af 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
144b0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
144b1 2a 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 *sqlite3PagerBac
144b2 6b 75 70 50 74 72 28 50 61 67 65 72 20 2a 70 50 kupPtr(Pager *pP
144b3 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
144b4 26 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 &pPager->pBackup
144b5 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ;.}..#endif /* S
144b6 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 QLITE_OMIT_DISKI
144b7 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a O */../*********
144b8 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 ***** End of pag
144b9 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a er.c ***********
144ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
144bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
144bc 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
144bd 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
144be 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a btmutex.c *****
144bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
144c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
144c1 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
144c2 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a August 27.**.**
144c3 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
144c4 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
144c5 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
144c6 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
144c7 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
144c8 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
144c9 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
144ca 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
144cb 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
144cc 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
144cd 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
144ce 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
144cf 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
144d0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
144d1 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
144d2 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
144d3 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
144d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
144d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
144d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
144d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
144d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
144d9 24 49 64 3a 20 62 74 6d 75 74 65 78 2e 63 2c 76 $Id: btmutex.c,v
144da 20 31 2e 31 37 20 32 30 30 39 2f 30 37 2f 32 30 1.17 2009/07/20
144db 20 31 32 3a 33 33 3a 33 33 20 64 72 68 20 45 78 12:33:33 drh Ex
144dc 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 p $.**.** This f
144dd 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 ile contains cod
144de 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d e used to implem
144df 65 6e 74 20 6d 75 74 65 78 65 73 20 6f 6e 20 42 ent mutexes on B
144e0 74 72 65 65 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a tree objects..**
144e1 20 54 68 69 73 20 63 6f 64 65 20 72 65 61 6c 6c This code reall
144e2 79 20 62 65 6c 6f 6e 67 73 20 69 6e 20 62 74 72 y belongs in btr
144e3 65 65 2e 63 2e 20 20 42 75 74 20 62 74 72 65 65 ee.c. But btree
144e4 2e 63 20 69 73 20 67 65 74 74 69 6e 67 20 74 6f .c is getting to
144e5 6f 0a 2a 2a 20 62 69 67 20 61 6e 64 20 77 65 20 o.** big and we
144e6 77 61 6e 74 20 74 6f 20 62 72 65 61 6b 20 69 74 want to break it
144e7 20 64 6f 77 6e 20 73 6f 6d 65 2e 20 20 54 68 69 down some. Thi
144e8 73 20 70 61 63 6b 61 67 65 64 20 73 65 65 6d 65 s packaged seeme
144e9 64 20 6c 69 6b 65 0a 2a 2a 20 61 20 67 6f 6f 64 d like.** a good
144ea 20 62 72 65 61 6b 6f 75 74 2e 0a 2a 2f 0a 2f 2a breakout..*/./*
144eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
144ec 63 6c 75 64 65 20 62 74 72 65 65 49 6e 74 2e 68 clude btreeInt.h
144ed 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
144ee 66 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a f btmutex.c ****
144ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
144f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
144f1 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 49 6e gin file btreeIn
144f2 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
144f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
144f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
144f5 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 36 .** 2004 April 6
144f6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
144f7 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
144f8 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
144f9 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
144fa 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
144fb 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
144fc 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
144fd 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
144fe 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
144ff 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
14500 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
14501 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
14502 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
14503 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
14504 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
14505 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
14506 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
14507 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
14508 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14509 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1450a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1450b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1450c 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 49 6e 74 ** $Id: btreeInt
1450d 2e 68 2c 76 20 31 2e 35 32 20 32 30 30 39 2f 30 .h,v 1.52 2009/0
1450e 37 2f 31 35 20 31 37 3a 32 35 3a 34 36 20 64 72 7/15 17:25:46 dr
1450f 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 h Exp $.**.** Th
14510 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e is file implemen
14511 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64 ts a external (d
14512 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62 isk-based) datab
14513 61 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73 ase using BTrees
14514 2e 0a 2a 2a 20 46 6f 72 20 61 20 64 65 74 61 69 ..** For a detai
14515 6c 65 64 20 64 69 73 63 75 73 73 69 6f 6e 20 6f led discussion o
14516 66 20 42 54 72 65 65 73 2c 20 72 65 66 65 72 20 f BTrees, refer
14517 74 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 6f 6e to.**.** Don
14518 61 6c 64 20 45 2e 20 4b 6e 75 74 68 2c 20 54 48 ald E. Knuth, TH
14519 45 20 41 52 54 20 4f 46 20 43 4f 4d 50 55 54 45 E ART OF COMPUTE
1451a 52 20 50 52 4f 47 52 41 4d 4d 49 4e 47 2c 20 56 R PROGRAMMING, V
1451b 6f 6c 75 6d 65 20 33 3a 0a 2a 2a 20 20 20 20 20 olume 3:.**
1451c 22 53 6f 72 74 69 6e 67 20 41 6e 64 20 53 65 61 "Sorting And Sea
1451d 72 63 68 69 6e 67 22 2c 20 70 61 67 65 73 20 34 rching", pages 4
1451e 37 33 2d 34 38 30 2e 20 41 64 64 69 73 6f 6e 2d 73-480. Addison-
1451f 57 65 73 6c 65 79 0a 2a 2a 20 20 20 20 20 50 75 Wesley.** Pu
14520 62 6c 69 73 68 69 6e 67 20 43 6f 6d 70 61 6e 79 blishing Company
14521 2c 20 52 65 61 64 69 6e 67 2c 20 4d 61 73 73 61 , Reading, Massa
14522 63 68 75 73 65 74 74 73 2e 0a 2a 2a 0a 2a 2a 20 chusetts..**.**
14523 54 68 65 20 62 61 73 69 63 20 69 64 65 61 20 69 The basic idea i
14524 73 20 74 68 61 74 20 65 61 63 68 20 70 61 67 65 s that each page
14525 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e of the file con
14526 74 61 69 6e 73 20 4e 20 64 61 74 61 62 61 73 65 tains N database
14527 0a 2a 2a 20 65 6e 74 72 69 65 73 20 61 6e 64 20 .** entries and
14528 4e 2b 31 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 N+1 pointers to
14529 73 75 62 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 subpages..**.**
1452a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d --------------
1452b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1452c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1452d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1452e 2d 2d 0a 2a 2a 20 20 20 7c 20 20 50 74 72 28 30 --.** | Ptr(0
1452f 29 20 7c 20 4b 65 79 28 30 29 20 7c 20 50 74 72 ) | Key(0) | Ptr
14530 28 31 29 20 7c 20 4b 65 79 28 31 29 20 7c 20 2e (1) | Key(1) | .
14531 2e 2e 20 7c 20 4b 65 79 28 4e 2d 31 29 20 7c 20 .. | Key(N-1) |
14532 50 74 72 28 4e 29 20 7c 0a 2a 2a 20 20 20 2d 2d Ptr(N) |.** --
14533 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14534 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14535 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14536 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a --------------.*
14537 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 *.** All of the
14538 6b 65 79 73 20 6f 6e 20 74 68 65 20 70 61 67 65 keys on the page
14539 20 74 68 61 74 20 50 74 72 28 30 29 20 70 6f 69 that Ptr(0) poi
1453a 6e 74 73 20 74 6f 20 68 61 76 65 20 76 61 6c 75 nts to have valu
1453b 65 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 es less.** than
1453c 4b 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f 66 20 Key(0). All of
1453d 74 68 65 20 6b 65 79 73 20 6f 6e 20 70 61 67 65 the keys on page
1453e 20 50 74 72 28 31 29 20 61 6e 64 20 69 74 73 20 Ptr(1) and its
1453f 73 75 62 70 61 67 65 73 20 68 61 76 65 0a 2a 2a subpages have.**
14540 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20 values greater
14541 74 68 61 6e 20 4b 65 79 28 30 29 20 61 6e 64 20 than Key(0) and
14542 6c 65 73 73 20 74 68 61 6e 20 4b 65 79 28 31 29 less than Key(1)
14543 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 . All of the ke
14544 79 73 0a 2a 2a 20 6f 6e 20 50 74 72 28 4e 29 20 ys.** on Ptr(N)
14545 61 6e 64 20 69 74 73 20 73 75 62 70 61 67 65 73 and its subpages
14546 20 68 61 76 65 20 76 61 6c 75 65 73 20 67 72 65 have values gre
14547 61 74 65 72 20 74 68 61 6e 20 4b 65 79 28 4e 2d ater than Key(N-
14548 31 29 2e 20 20 41 6e 64 0a 2a 2a 20 73 6f 20 66 1). And.** so f
14549 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 orth..**.** Find
1454a 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 ing a particular
1454b 20 6b 65 79 20 72 65 71 75 69 72 65 73 20 72 65 key requires re
1454c 61 64 69 6e 67 20 4f 28 6c 6f 67 28 4d 29 29 20 ading O(log(M))
1454d 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a pages from the .
1454e 2a 2a 20 64 69 73 6b 20 77 68 65 72 65 20 4d 20 ** disk where M
1454f 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
14550 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 entries in the
14551 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 tree..**.** In t
14552 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 his implementati
14553 6f 6e 2c 20 61 20 73 69 6e 67 6c 65 20 66 69 6c on, a single fil
14554 65 20 63 61 6e 20 68 6f 6c 64 20 6f 6e 65 20 6f e can hold one o
14555 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 20 r more separate
14556 0a 2a 2a 20 42 54 72 65 65 73 2e 20 20 45 61 63 .** BTrees. Eac
14557 68 20 42 54 72 65 65 20 69 73 20 69 64 65 6e 74 h BTree is ident
14558 69 66 69 65 64 20 62 79 20 74 68 65 20 69 6e 64 ified by the ind
14559 65 78 20 6f 66 20 69 74 73 20 72 6f 6f 74 20 70 ex of its root p
1455a 61 67 65 2e 20 20 54 68 65 0a 2a 2a 20 6b 65 79 age. The.** key
1455b 20 61 6e 64 20 64 61 74 61 20 66 6f 72 20 61 6e and data for an
1455c 79 20 65 6e 74 72 79 20 61 72 65 20 63 6f 6d 62 y entry are comb
1455d 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 65 ined to form the
1455e 20 22 70 61 79 6c 6f 61 64 22 2e 20 20 41 0a 2a "payload". A.*
1455f 2a 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f * fixed amount o
14560 66 20 70 61 79 6c 6f 61 64 20 63 61 6e 20 62 65 f payload can be
14561 20 63 61 72 72 69 65 64 20 64 69 72 65 63 74 6c carried directl
14562 79 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 y on the databas
14563 65 0a 2a 2a 20 70 61 67 65 2e 20 20 49 66 20 74 e.** page. If t
14564 68 65 20 70 61 79 6c 6f 61 64 20 69 73 20 6c 61 he payload is la
14565 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 rger than the pr
14566 65 73 65 74 20 61 6d 6f 75 6e 74 20 74 68 65 6e eset amount then
14567 20 73 75 72 70 6c 75 73 0a 2a 2a 20 62 79 74 65 surplus.** byte
14568 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 s are stored on
14569 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 overflow pages.
1456a 20 54 68 65 20 70 61 79 6c 6f 61 64 20 66 6f 72 The payload for
1456b 20 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 61 6e 64 an entry.** and
1456c 20 74 68 65 20 70 72 65 63 65 64 69 6e 67 20 70 the preceding p
1456d 6f 69 6e 74 65 72 20 61 72 65 20 63 6f 6d 62 69 ointer are combi
1456e 6e 65 64 20 74 6f 20 66 6f 72 6d 20 61 20 22 43 ned to form a "C
1456f 65 6c 6c 22 2e 20 20 45 61 63 68 20 0a 2a 2a 20 ell". Each .**
14570 70 61 67 65 20 68 61 73 20 61 20 73 6d 61 6c 6c page has a small
14571 20 68 65 61 64 65 72 20 77 68 69 63 68 20 63 6f header which co
14572 6e 74 61 69 6e 73 20 74 68 65 20 50 74 72 28 4e ntains the Ptr(N
14573 29 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 6f 74 ) pointer and ot
14574 68 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 her.** informati
14575 6f 6e 20 73 75 63 68 20 61 73 20 74 68 65 20 73 on such as the s
14576 69 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64 20 64 ize of key and d
14577 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 4f 52 4d 41 ata..**.** FORMA
14578 54 20 44 45 54 41 49 4c 53 0a 2a 2a 0a 2a 2a 20 T DETAILS.**.**
14579 54 68 65 20 66 69 6c 65 20 69 73 20 64 69 76 69 The file is divi
1457a 64 65 64 20 69 6e 74 6f 20 70 61 67 65 73 2e 20 ded into pages.
1457b 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 The first page
1457c 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31 is called page 1
1457d 2c 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 ,.** the second
1457e 69 73 20 70 61 67 65 20 32 2c 20 61 6e 64 20 73 is page 2, and s
1457f 6f 20 66 6f 72 74 68 2e 20 20 41 20 70 61 67 65 o forth. A page
14580 20 6e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20 number of zero
14581 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 22 6e 6f indicates.** "no
14582 20 73 75 63 68 20 70 61 67 65 22 2e 20 20 54 68 such page". Th
14583 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e 20 e page size can
14584 62 65 20 61 6e 79 74 68 69 6e 67 20 62 65 74 77 be anything betw
14585 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35 33 een 512 and 6553
14586 36 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 20 6..** Each page
14587 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20 can be either a
14588 62 74 72 65 65 20 70 61 67 65 2c 20 61 20 66 72 btree page, a fr
14589 65 65 6c 69 73 74 20 70 61 67 65 20 6f 72 20 61 eelist page or a
1458a 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 n overflow.** pa
1458b 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 ge..**.** The fi
1458c 72 73 74 20 70 61 67 65 20 69 73 20 61 6c 77 61 rst page is alwa
1458d 79 73 20 61 20 62 74 72 65 65 20 70 61 67 65 2e ys a btree page.
1458e 20 20 54 68 65 20 66 69 72 73 74 20 31 30 30 20 The first 100
1458f 62 79 74 65 73 20 6f 66 20 74 68 65 20 66 69 72 bytes of the fir
14590 73 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 61 st.** page conta
14591 69 6e 20 61 20 73 70 65 63 69 61 6c 20 68 65 61 in a special hea
14592 64 65 72 20 28 74 68 65 20 22 66 69 6c 65 20 68 der (the "file h
14593 65 61 64 65 72 22 29 20 74 68 61 74 20 64 65 73 eader") that des
14594 63 72 69 62 65 73 20 74 68 65 20 66 69 6c 65 2e cribes the file.
14595 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 6f .** The format o
14596 66 20 74 68 65 20 66 69 6c 65 20 68 65 61 64 65 f the file heade
14597 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a r is as follows:
14598 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20 .**.** OFFSET
14599 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 SIZE DESCRI
1459a 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 PTION.** 0
1459b 20 20 20 20 20 31 36 20 20 20 20 20 48 65 61 64 16 Head
1459c 65 72 20 73 74 72 69 6e 67 3a 20 22 53 51 4c 69 er string: "SQLi
1459d 74 65 20 66 6f 72 6d 61 74 20 33 5c 30 30 30 22 te format 3\000"
1459e 0a 2a 2a 20 20 20 20 20 31 36 20 20 20 20 20 20 .** 16
1459f 20 32 20 20 20 20 20 50 61 67 65 20 73 69 7a 65 2 Page size
145a0 20 69 6e 20 62 79 74 65 73 2e 20 20 0a 2a 2a 20 in bytes. .**
145a1 20 20 20 20 31 38 20 20 20 20 20 20 20 31 20 20 18 1
145a2 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 77 File format w
145a3 72 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 rite version.**
145a4 20 20 20 20 31 39 20 20 20 20 20 20 20 31 20 20 19 1
145a5 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 72 File format r
145a6 65 61 64 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 ead version.**
145a7 20 20 20 32 30 20 20 20 20 20 20 20 31 20 20 20 20 1
145a8 20 20 42 79 74 65 73 20 6f 66 20 75 6e 75 73 65 Bytes of unuse
145a9 64 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65 d space at the e
145aa 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 65 0a nd of each page.
145ab 2a 2a 20 20 20 20 20 32 31 20 20 20 20 20 20 20 ** 21
145ac 31 20 20 20 20 20 4d 61 78 20 65 6d 62 65 64 64 1 Max embedd
145ad 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 ed payload fract
145ae 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 32 20 20 20 ion.** 22
145af 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20 65 6d 1 Min em
145b0 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 bedded payload f
145b1 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 raction.** 2
145b2 33 20 20 20 20 20 20 20 31 20 20 20 20 20 4d 69 3 1 Mi
145b3 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 66 n leaf payload f
145b4 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 raction.** 2
145b5 34 20 20 20 20 20 20 20 34 20 20 20 20 20 46 69 4 4 Fi
145b6 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 le change counte
145b7 72 0a 2a 2a 20 20 20 20 20 32 38 20 20 20 20 20 r.** 28
145b8 20 20 34 20 20 20 20 20 52 65 73 65 72 76 65 64 4 Reserved
145b9 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65 0a for future use.
145ba 2a 2a 20 20 20 20 20 33 32 20 20 20 20 20 20 20 ** 32
145bb 34 20 20 20 20 20 46 69 72 73 74 20 66 72 65 65 4 First free
145bc 6c 69 73 74 20 70 61 67 65 0a 2a 2a 20 20 20 20 list page.**
145bd 20 33 36 20 20 20 20 20 20 20 34 20 20 20 20 20 36 4
145be 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c 69 Number of freeli
145bf 73 74 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 st pages in the
145c0 66 69 6c 65 0a 2a 2a 20 20 20 20 20 34 30 20 20 file.** 40
145c1 20 20 20 20 36 30 20 20 20 20 20 31 35 20 34 2d 60 15 4-
145c2 62 79 74 65 20 6d 65 74 61 20 76 61 6c 75 65 73 byte meta values
145c3 20 70 61 73 73 65 64 20 74 6f 20 68 69 67 68 65 passed to highe
145c4 72 20 6c 61 79 65 72 73 0a 2a 2a 0a 2a 2a 20 20 r layers.**.**
145c5 20 20 20 34 30 20 20 20 20 20 20 20 34 20 20 20 40 4
145c6 20 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a Schema cookie.
145c7 2a 2a 20 20 20 20 20 34 34 20 20 20 20 20 20 20 ** 44
145c8 34 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 4 File forma
145c9 74 20 6f 66 20 73 63 68 65 6d 61 20 6c 61 79 65 t of schema laye
145ca 72 0a 2a 2a 20 20 20 20 20 34 38 20 20 20 20 20 r.** 48
145cb 20 20 34 20 20 20 20 20 53 69 7a 65 20 6f 66 20 4 Size of
145cc 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 20 20 page cache.**
145cd 20 20 35 32 20 20 20 20 20 20 20 34 20 20 20 20 52 4
145ce 20 4c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 Largest root-pa
145cf 67 65 20 28 61 75 74 6f 2f 69 6e 63 72 5f 76 61 ge (auto/incr_va
145d0 63 75 75 6d 29 0a 2a 2a 20 20 20 20 20 35 36 20 cuum).** 56
145d1 20 20 20 20 20 20 34 20 20 20 20 20 31 3d 55 54 4 1=UT
145d2 46 2d 38 20 32 3d 55 54 46 31 36 6c 65 20 33 3d F-8 2=UTF16le 3=
145d3 55 54 46 31 36 62 65 0a 2a 2a 20 20 20 20 20 36 UTF16be.** 6
145d4 30 20 20 20 20 20 20 20 34 20 20 20 20 20 55 73 0 4 Us
145d5 65 72 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 er version.**
145d6 20 20 36 34 20 20 20 20 20 20 20 34 20 20 20 20 64 4
145d7 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 Incremental vac
145d8 75 75 6d 20 6d 6f 64 65 0a 2a 2a 20 20 20 20 20 uum mode.**
145d9 36 38 20 20 20 20 20 20 20 34 20 20 20 20 20 75 68 4 u
145da 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 37 32 20 nused.** 72
145db 20 20 20 20 20 20 34 20 20 20 20 20 75 6e 75 73 4 unus
145dc 65 64 0a 2a 2a 20 20 20 20 20 37 36 20 20 20 20 ed.** 76
145dd 20 20 20 34 20 20 20 20 20 75 6e 75 73 65 64 0a 4 unused.
145de 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 **.** All of the
145df 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 integer values
145e0 61 72 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 28 are big-endian (
145e1 6d 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 most significant
145e2 20 62 79 74 65 20 66 69 72 73 74 29 2e 0a 2a 2a byte first)..**
145e3 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 63 68 61 .** The file cha
145e4 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 73 20 69 nge counter is i
145e5 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 20 ncremented when
145e6 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
145e7 63 68 61 6e 67 65 64 0a 2a 2a 20 54 68 69 73 20 changed.** This
145e8 63 6f 75 6e 74 65 72 20 61 6c 6c 6f 77 73 20 6f counter allows o
145e9 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 74 ther processes t
145ea 6f 20 6b 6e 6f 77 20 77 68 65 6e 20 74 68 65 20 o know when the
145eb 66 69 6c 65 20 68 61 73 20 63 68 61 6e 67 65 64 file has changed
145ec 0a 2a 2a 20 61 6e 64 20 74 68 75 73 20 77 68 65 .** and thus whe
145ed 6e 20 74 68 65 79 20 6e 65 65 64 20 74 6f 20 66 n they need to f
145ee 6c 75 73 68 20 74 68 65 69 72 20 63 61 63 68 65 lush their cache
145ef 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 20 ..**.** The max
145f0 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 embedded payload
145f1 20 66 72 61 63 74 69 6f 6e 20 69 73 20 74 68 65 fraction is the
145f2 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65 20 74 amount of the t
145f3 6f 74 61 6c 20 75 73 61 62 6c 65 0a 2a 2a 20 73 otal usable.** s
145f4 70 61 63 65 20 69 6e 20 61 20 70 61 67 65 20 74 pace in a page t
145f5 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6e 73 75 hat can be consu
145f6 6d 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 med by a single
145f7 63 65 6c 6c 20 66 6f 72 20 73 74 61 6e 64 61 72 cell for standar
145f8 64 0a 2a 2a 20 42 2d 74 72 65 65 20 28 6e 6f 6e d.** B-tree (non
145f9 2d 4c 45 41 46 44 41 54 41 29 20 74 61 62 6c 65 -LEAFDATA) table
145fa 73 2e 20 20 41 20 76 61 6c 75 65 20 6f 66 20 32 s. A value of 2
145fb 35 35 20 6d 65 61 6e 73 20 31 30 30 25 2e 20 20 55 means 100%.
145fc 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 69 The default.** i
145fd 73 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 6d s to limit the m
145fe 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 aximum cell size
145ff 20 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73 so that at leas
14600 74 20 34 20 63 65 6c 6c 73 20 77 69 6c 6c 20 66 t 4 cells will f
14601 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65 20 70 61 67 it.** on one pag
14602 65 2e 20 20 54 68 75 73 20 74 68 65 20 64 65 66 e. Thus the def
14603 61 75 6c 74 20 6d 61 78 20 65 6d 62 65 64 64 65 ault max embedde
14604 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 d payload fracti
14605 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a 0a 2a 2a 20 on is 64..**.**
14606 49 66 20 74 68 65 20 70 61 79 6c 6f 61 64 20 66 If the payload f
14607 6f 72 20 61 20 63 65 6c 6c 20 69 73 20 6c 61 72 or a cell is lar
14608 67 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78 ger than the max
14609 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 6e 20 65 payload, then e
1460a 78 74 72 61 0a 2a 2a 20 70 61 79 6c 6f 61 64 20 xtra.** payload
1460b 69 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 6f 76 is spilled to ov
1460c 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 4f erflow pages. O
1460d 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 nce an overflow
1460e 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 page is allocate
1460f 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e 79 20 62 79 d,.** as many by
14610 74 65 73 20 61 73 20 70 6f 73 73 69 62 6c 65 20 tes as possible
14611 61 72 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 are moved into t
14612 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 he overflow page
14613 73 20 77 69 74 68 6f 75 74 20 6c 65 74 74 69 6e s without lettin
14614 67 0a 2a 2a 20 74 68 65 20 63 65 6c 6c 20 73 69 g.** the cell si
14615 7a 65 20 64 72 6f 70 20 62 65 6c 6f 77 20 74 68 ze drop below th
14616 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 e min embedded p
14617 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 2e ayload fraction.
14618 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e 20 6c .**.** The min l
14619 65 61 66 20 70 61 79 6c 6f 61 64 20 66 72 61 63 eaf payload frac
1461a 74 69 6f 6e 20 69 73 20 6c 69 6b 65 20 74 68 65 tion is like the
1461b 20 6d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61 min embedded pa
1461c 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a yload fraction.*
1461d 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 * except that it
1461e 20 61 70 70 6c 69 65 73 20 74 6f 20 6c 65 61 66 applies to leaf
1461f 20 6e 6f 64 65 73 20 69 6e 20 61 20 4c 45 41 46 nodes in a LEAF
14620 44 41 54 41 20 74 72 65 65 2e 20 20 54 68 65 20 DATA tree. The
14621 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70 61 79 6c 6f maximum.** paylo
14622 61 64 20 66 72 61 63 74 69 6f 6e 20 66 6f 72 20 ad fraction for
14623 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 20 a LEAFDATA tree
14624 69 73 20 61 6c 77 61 79 73 20 31 30 30 25 20 28 is always 100% (
14625 6f 72 20 32 35 35 29 20 61 6e 64 20 69 74 0a 2a or 255) and it.*
14626 2a 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20 * not specified
14627 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 0a 2a in the header..*
14628 2a 0a 2a 2a 20 45 61 63 68 20 62 74 72 65 65 20 *.** Each btree
14629 70 61 67 65 73 20 69 73 20 64 69 76 69 64 65 64 pages is divided
1462a 20 69 6e 74 6f 20 74 68 72 65 65 20 73 65 63 74 into three sect
1462b 69 6f 6e 73 3a 20 20 54 68 65 20 68 65 61 64 65 ions: The heade
1462c 72 2c 20 74 68 65 0a 2a 2a 20 63 65 6c 6c 20 70 r, the.** cell p
1462d 6f 69 6e 74 65 72 20 61 72 72 61 79 2c 20 61 6e ointer array, an
1462e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 d the cell conte
1462f 6e 74 20 61 72 65 61 2e 20 20 50 61 67 65 20 31 nt area. Page 1
14630 20 61 6c 73 6f 20 68 61 73 20 61 20 31 30 30 2d also has a 100-
14631 62 79 74 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61 byte.** file hea
14632 64 65 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 der that occurs
14633 62 65 66 6f 72 65 20 74 68 65 20 70 61 67 65 20 before the page
14634 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 header..**.**
14635 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |------------
14636 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 ----|.** |
14637 66 69 6c 65 20 68 65 61 64 65 72 20 20 20 20 7c file header |
14638 20 20 20 31 30 30 20 62 79 74 65 73 2e 20 20 50 100 bytes. P
14639 61 67 65 20 31 20 6f 6e 6c 79 2e 0a 2a 2a 20 20 age 1 only..**
1463a 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |-----------
1463b 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c -----|.** |
1463c 20 70 61 67 65 20 68 65 61 64 65 72 20 20 20 20 page header
1463d 7c 20 20 20 38 20 62 79 74 65 73 20 66 6f 72 20 | 8 bytes for
1463e 6c 65 61 76 65 73 2e 20 20 31 32 20 62 79 74 65 leaves. 12 byte
1463f 73 20 66 6f 72 20 69 6e 74 65 72 69 6f 72 20 6e s for interior n
14640 6f 64 65 73 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d odes.** |--
14641 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a --------------|.
14642 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c 20 70 ** | cell p
14643 6f 69 6e 74 65 72 20 20 20 7c 20 20 20 7c 20 20 ointer | |
14644 32 20 62 79 74 65 73 20 70 65 72 20 63 65 6c 6c 2 bytes per cell
14645 2e 20 20 53 6f 72 74 65 64 20 6f 72 64 65 72 2e . Sorted order.
14646 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72 72 61 79 .** | array
14647 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20 | |
14648 20 47 72 6f 77 73 20 64 6f 77 6e 77 61 72 64 0a Grows downward.
14649 2a 2a 20 20 20 20 20 20 7c 20 20 20 20 20 20 20 ** |
1464a 20 20 20 20 20 20 20 20 20 7c 20 20 20 76 0a 2a | v.*
1464b 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d * |--------
1464c 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 --------|.**
1464d 20 20 7c 20 75 6e 61 6c 6c 6f 63 61 74 65 64 20 | unallocated
1464e 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 73 |.** | s
1464f 70 61 63 65 20 20 20 20 20 20 20 20 20 20 7c 0a pace |.
14650 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d ** |-------
14651 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20 20 5e 20 20 ---------| ^
14652 47 72 6f 77 73 20 75 70 77 61 72 64 73 0a 2a 2a Grows upwards.**
14653 20 20 20 20 20 20 7c 20 63 65 6c 6c 20 63 6f 6e | cell con
14654 74 65 6e 74 20 20 20 7c 20 20 20 7c 20 20 41 72 tent | | Ar
14655 62 69 74 72 61 72 79 20 6f 72 64 65 72 20 69 6e bitrary order in
14656 74 65 72 73 70 65 72 73 65 64 20 77 69 74 68 20 terspersed with
14657 66 72 65 65 62 6c 6f 63 6b 73 2e 0a 2a 2a 20 20 freeblocks..**
14658 20 20 20 20 7c 20 61 72 65 61 20 20 20 20 20 20 | area
14659 20 20 20 20 20 7c 20 20 20 7c 20 20 61 6e 64 20 | | and
1465a 66 72 65 65 20 73 70 61 63 65 20 66 72 61 67 6d free space fragm
1465b 65 6e 74 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d ents..** |-
1465c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c ---------------|
1465d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 .**.** The page
1465e 68 65 61 64 65 72 73 20 6c 6f 6f 6b 73 20 6c 69 headers looks li
1465f 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 ke this:.**.**
14660 20 4f 46 46 53 45 54 20 20 20 53 49 5a 45 20 20 OFFSET SIZE
14661 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a DESCRIPTION.*
14662 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 20 31 * 0 1
14663 20 20 20 20 20 20 46 6c 61 67 73 2e 20 31 3a 20 Flags. 1:
14664 69 6e 74 6b 65 79 2c 20 32 3a 20 7a 65 72 6f 64 intkey, 2: zerod
14665 61 74 61 2c 20 34 3a 20 6c 65 61 66 64 61 74 61 ata, 4: leafdata
14666 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a 20 20 20 20 , 8: leaf.**
14667 20 20 31 20 20 20 20 20 20 20 32 20 20 20 20 20 1 2
14668 20 62 79 74 65 20 6f 66 66 73 65 74 20 74 6f 20 byte offset to
14669 74 68 65 20 66 69 72 73 74 20 66 72 65 65 62 6c the first freebl
1466a 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 33 20 20 20 ock.** 3
1466b 20 20 20 20 32 20 20 20 20 20 20 6e 75 6d 62 65 2 numbe
1466c 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 r of cells on th
1466d 69 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 is page.**
1466e 35 20 20 20 20 20 20 20 32 20 20 20 20 20 20 66 5 2 f
1466f 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 irst byte of the
14670 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 cell content ar
14671 65 61 0a 2a 2a 20 20 20 20 20 20 37 20 20 20 20 ea.** 7
14672 20 20 20 31 20 20 20 20 20 20 6e 75 6d 62 65 72 1 number
14673 20 6f 66 20 66 72 61 67 6d 65 6e 74 65 64 20 66 of fragmented f
14674 72 65 65 20 62 79 74 65 73 0a 2a 2a 20 20 20 20 ree bytes.**
14675 20 20 38 20 20 20 20 20 20 20 34 20 20 20 20 20 8 4
14676 20 52 69 67 68 74 20 63 68 69 6c 64 20 28 74 68 Right child (th
14677 65 20 50 74 72 28 4e 29 20 76 61 6c 75 65 29 2e e Ptr(N) value).
14678 20 20 4f 6d 69 74 74 65 64 20 6f 6e 20 6c 65 61 Omitted on lea
14679 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ves..**.** The f
1467a 6c 61 67 73 20 64 65 66 69 6e 65 20 74 68 65 20 lags define the
1467b 66 6f 72 6d 61 74 20 6f 66 20 74 68 69 73 20 62 format of this b
1467c 74 72 65 65 20 70 61 67 65 2e 20 20 54 68 65 20 tree page. The
1467d 6c 65 61 66 20 66 6c 61 67 20 6d 65 61 6e 73 20 leaf flag means
1467e 74 68 61 74 0a 2a 2a 20 74 68 69 73 20 70 61 67 that.** this pag
1467f 65 20 68 61 73 20 6e 6f 20 63 68 69 6c 64 72 65 e has no childre
14680 6e 2e 20 20 54 68 65 20 7a 65 72 6f 64 61 74 61 n. The zerodata
14681 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 flag means that
14682 20 74 68 69 73 20 70 61 67 65 20 63 61 72 72 69 this page carri
14683 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b 65 79 73 20 es.** only keys
14684 61 6e 64 20 6e 6f 20 64 61 74 61 2e 20 20 54 68 and no data. Th
14685 65 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 6d 65 e intkey flag me
14686 61 6e 73 20 74 68 61 74 20 74 68 65 20 6b 65 79 ans that the key
14687 20 69 73 20 61 20 69 6e 74 65 67 65 72 0a 2a 2a is a integer.**
14688 20 77 68 69 63 68 20 69 73 20 73 74 6f 72 65 64 which is stored
14689 20 69 6e 20 74 68 65 20 6b 65 79 20 73 69 7a 65 in the key size
1468a 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63 65 entry of the ce
1468b 6c 6c 20 68 65 61 64 65 72 20 72 61 74 68 65 72 ll header rather
1468c 20 74 68 61 6e 20 69 6e 0a 2a 2a 20 74 68 65 20 than in.** the
1468d 70 61 79 6c 6f 61 64 20 61 72 65 61 2e 0a 2a 2a payload area..**
1468e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 .** The cell poi
1468f 6e 74 65 72 20 61 72 72 61 79 20 62 65 67 69 6e nter array begin
14690 73 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 62 s on the first b
14691 79 74 65 20 61 66 74 65 72 20 74 68 65 20 70 61 yte after the pa
14692 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 20 54 68 ge header..** Th
14693 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 e cell pointer a
14694 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 7a 65 rray contains ze
14695 72 6f 20 6f 72 20 6d 6f 72 65 20 32 2d 62 79 74 ro or more 2-byt
14696 65 20 6e 75 6d 62 65 72 73 20 77 68 69 63 68 20 e numbers which
14697 61 72 65 0a 2a 2a 20 6f 66 66 73 65 74 73 20 66 are.** offsets f
14698 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e rom the beginnin
14699 67 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f g of the page to
1469a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
1469b 74 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a 2a 2a t in the cell.**
1469c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 content area.
1469d 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 The cell pointer
1469e 73 20 6f 63 63 75 72 20 69 6e 20 73 6f 72 74 65 s occur in sorte
1469f 64 20 6f 72 64 65 72 2e 20 20 54 68 65 20 73 79 d order. The sy
146a0 73 74 65 6d 20 73 74 72 69 76 65 73 0a 2a 2a 20 stem strives.**
146a1 74 6f 20 6b 65 65 70 20 66 72 65 65 20 73 70 61 to keep free spa
146a2 63 65 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 ce after the las
146a3 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 73 t cell pointer s
146a4 6f 20 74 68 61 74 20 6e 65 77 20 63 65 6c 6c 73 o that new cells
146a5 20 63 61 6e 0a 2a 2a 20 62 65 20 65 61 73 69 6c can.** be easil
146a6 79 20 61 64 64 65 64 20 77 69 74 68 6f 75 74 20 y added without
146a7 68 61 76 69 6e 67 20 74 6f 20 64 65 66 72 61 67 having to defrag
146a8 6d 65 6e 74 20 74 68 65 20 70 61 67 65 2e 0a 2a ment the page..*
146a9 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e *.** Cell conten
146aa 74 20 69 73 20 73 74 6f 72 65 64 20 61 74 20 74 t is stored at t
146ab 68 65 20 76 65 72 79 20 65 6e 64 20 6f 66 20 74 he very end of t
146ac 68 65 20 70 61 67 65 20 61 6e 64 20 67 72 6f 77 he page and grow
146ad 73 20 74 6f 77 61 72 64 20 74 68 65 0a 2a 2a 20 s toward the.**
146ae 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 beginning of the
146af 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 55 6e 75 page..**.** Unu
146b0 73 65 64 20 73 70 61 63 65 20 77 69 74 68 69 6e sed space within
146b1 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
146b2 74 20 61 72 65 61 20 69 73 20 63 6f 6c 6c 65 63 t area is collec
146b3 74 65 64 20 69 6e 74 6f 20 61 20 6c 69 6e 6b 65 ted into a linke
146b4 64 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 66 72 65 d list of.** fre
146b5 65 62 6c 6f 63 6b 73 2e 20 20 45 61 63 68 20 66 eblocks. Each f
146b6 72 65 65 62 6c 6f 63 6b 20 69 73 20 61 74 20 6c reeblock is at l
146b7 65 61 73 74 20 34 20 62 79 74 65 73 20 69 6e 20 east 4 bytes in
146b8 73 69 7a 65 2e 20 20 54 68 65 20 62 79 74 65 20 size. The byte
146b9 6f 66 66 73 65 74 0a 2a 2a 20 74 6f 20 74 68 65 offset.** to the
146ba 20 66 69 72 73 74 20 66 72 65 65 62 6c 6f 63 6b first freeblock
146bb 20 69 73 20 67 69 76 65 6e 20 69 6e 20 74 68 65 is given in the
146bc 20 68 65 61 64 65 72 2e 20 20 46 72 65 65 62 6c header. Freebl
146bd 6f 63 6b 73 20 6f 63 63 75 72 20 69 6e 0a 2a 2a ocks occur in.**
146be 20 69 6e 63 72 65 61 73 69 6e 67 20 6f 72 64 65 increasing orde
146bf 72 2e 20 20 42 65 63 61 75 73 65 20 61 20 66 72 r. Because a fr
146c0 65 65 62 6c 6f 63 6b 20 6d 75 73 74 20 62 65 20 eeblock must be
146c1 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 at least 4 bytes
146c2 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a 20 61 6e 79 in size,.** any
146c3 20 67 72 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 group of 3 or f
146c4 65 77 65 72 20 75 6e 75 73 65 64 20 62 79 74 65 ewer unused byte
146c5 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f s in the cell co
146c6 6e 74 65 6e 74 20 61 72 65 61 20 63 61 6e 6e 6f ntent area canno
146c7 74 0a 2a 2a 20 65 78 69 73 74 20 6f 6e 20 74 68 t.** exist on th
146c8 65 20 66 72 65 65 62 6c 6f 63 6b 20 63 68 61 69 e freeblock chai
146c9 6e 2e 20 20 41 20 67 72 6f 75 70 20 6f 66 20 33 n. A group of 3
146ca 20 6f 72 20 66 65 77 65 72 20 66 72 65 65 20 62 or fewer free b
146cb 79 74 65 73 20 69 73 20 63 61 6c 6c 65 64 0a 2a ytes is called.*
146cc 2a 20 61 20 66 72 61 67 6d 65 6e 74 2e 20 20 54 * a fragment. T
146cd 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
146ce 6f 66 20 62 79 74 65 73 20 69 6e 20 61 6c 6c 20 of bytes in all
146cf 66 72 61 67 6d 65 6e 74 73 20 69 73 20 72 65 63 fragments is rec
146d0 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e 20 74 68 65 orded..** in the
146d1 20 70 61 67 65 20 68 65 61 64 65 72 20 61 74 20 page header at
146d2 6f 66 66 73 65 74 20 37 2e 0a 2a 2a 0a 2a 2a 20 offset 7..**.**
146d3 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 SIZE DESCR
146d4 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 32 IPTION.** 2
146d5 20 20 20 20 20 42 79 74 65 20 6f 66 66 73 65 74 Byte offset
146d6 20 6f 66 20 74 68 65 20 6e 65 78 74 20 66 72 65 of the next fre
146d7 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 32 eblock.** 2
146d8 20 20 20 20 20 42 79 74 65 73 20 69 6e 20 74 68 Bytes in th
146d9 69 73 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 0a is freeblock.**.
146da 2a 2a 20 43 65 6c 6c 73 20 61 72 65 20 6f 66 20 ** Cells are of
146db 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 2e variable length.
146dc 20 20 43 65 6c 6c 73 20 61 72 65 20 73 74 6f 72 Cells are stor
146dd 65 64 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 ed in the cell c
146de 6f 6e 74 65 6e 74 20 61 72 65 61 20 61 74 0a 2a ontent area at.*
146df 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 * the end of the
146e0 20 70 61 67 65 2e 20 20 50 6f 69 6e 74 65 72 73 page. Pointers
146e1 20 74 6f 20 74 68 65 20 63 65 6c 6c 73 20 61 72 to the cells ar
146e2 65 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 70 6f e in the cell po
146e3 69 6e 74 65 72 20 61 72 72 61 79 0a 2a 2a 20 74 inter array.** t
146e4 68 61 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 hat immediately
146e5 66 6f 6c 6c 6f 77 73 20 74 68 65 20 70 61 67 65 follows the page
146e6 20 68 65 61 64 65 72 2e 20 20 43 65 6c 6c 73 20 header. Cells
146e7 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 is not necessari
146e8 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67 75 6f 75 73 ly.** contiguous
146e9 20 6f 72 20 69 6e 20 6f 72 64 65 72 2c 20 62 75 or in order, bu
146ea 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 73 20 t cell pointers
146eb 61 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 61 are contiguous a
146ec 6e 64 20 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a 0a nd in order..**.
146ed 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 ** Cell content
146ee 6d 61 6b 65 73 20 75 73 65 20 6f 66 20 76 61 72 makes use of var
146ef 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 iable length int
146f0 65 67 65 72 73 2e 20 20 41 20 76 61 72 69 61 62 egers. A variab
146f1 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 69 6e 74 le.** length int
146f2 65 67 65 72 20 69 73 20 31 20 74 6f 20 39 20 62 eger is 1 to 9 b
146f3 79 74 65 73 20 77 68 65 72 65 20 74 68 65 20 6c ytes where the l
146f4 6f 77 65 72 20 37 20 62 69 74 73 20 6f 66 20 65 ower 7 bits of e
146f5 61 63 68 20 0a 2a 2a 20 62 79 74 65 20 61 72 65 ach .** byte are
146f6 20 75 73 65 64 2e 20 20 54 68 65 20 69 6e 74 65 used. The inte
146f7 67 65 72 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 ger consists of
146f8 61 6c 6c 20 62 79 74 65 73 20 74 68 61 74 20 68 all bytes that h
146f9 61 76 65 20 62 69 74 20 38 20 73 65 74 20 61 6e ave bit 8 set an
146fa 64 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 62 d.** the first b
146fb 79 74 65 20 77 69 74 68 20 62 69 74 20 38 20 63 yte with bit 8 c
146fc 6c 65 61 72 2e 20 20 54 68 65 20 6d 6f 73 74 20 lear. The most
146fd 73 69 67 6e 69 66 69 63 61 6e 74 20 62 79 74 65 significant byte
146fe 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a of the integer.
146ff 2a 2a 20 61 70 70 65 61 72 73 20 66 69 72 73 74 ** appears first
14700 2e 20 20 41 20 76 61 72 69 61 62 6c 65 2d 6c 65 . A variable-le
14701 6e 67 74 68 20 69 6e 74 65 67 65 72 20 6d 61 79 ngth integer may
14702 20 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61 not be more tha
14703 6e 20 39 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0a n 9 bytes long..
14704 2a 2a 20 41 73 20 61 20 73 70 65 63 69 61 6c 20 ** As a special
14705 63 61 73 65 2c 20 61 6c 6c 20 38 20 62 79 74 65 case, all 8 byte
14706 73 20 6f 66 20 74 68 65 20 39 74 68 20 62 79 74 s of the 9th byt
14707 65 20 61 72 65 20 75 73 65 64 20 61 73 20 64 61 e are used as da
14708 74 61 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c ta. This.** all
14709 6f 77 73 20 61 20 36 34 2d 62 69 74 20 69 6e 74 ows a 64-bit int
1470a 65 67 65 72 20 74 6f 20 62 65 20 65 6e 63 6f 64 eger to be encod
1470b 65 64 20 69 6e 20 39 20 62 79 74 65 73 2e 0a 2a ed in 9 bytes..*
1470c 2a 0a 2a 2a 20 20 20 20 30 78 30 30 20 20 20 20 *.** 0x00
1470d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1470e 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 becomes 0x000
1470f 30 30 30 30 30 0a 2a 2a 20 20 20 20 30 78 37 66 00000.** 0x7f
14710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14711 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 becomes 0
14712 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20 x0000007f.**
14713 30 78 38 31 20 30 78 30 30 20 20 20 20 20 20 20 0x81 0x00
14714 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 become
14715 73 20 20 30 78 30 30 30 30 30 30 38 30 0a 2a 2a s 0x00000080.**
14716 20 20 20 20 30 78 38 32 20 30 78 30 30 20 20 20 0x82 0x00
14717 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 be
14718 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 31 30 comes 0x0000010
14719 30 0a 2a 2a 20 20 20 20 30 78 38 30 20 30 78 37 0.** 0x80 0x7
1471a 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f
1471b 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 becomes 0x000
1471c 30 30 30 37 66 0a 2a 2a 20 20 20 20 30 78 38 61 0007f.** 0x8a
1471d 20 30 78 39 31 20 30 78 64 31 20 30 78 61 63 20 0x91 0xd1 0xac
1471e 30 78 37 38 20 20 62 65 63 6f 6d 65 73 20 20 30 0x78 becomes 0
1471f 78 31 32 33 34 35 36 37 38 0a 2a 2a 20 20 20 20 x12345678.**
14720 30 78 38 31 20 30 78 38 31 20 30 78 38 31 20 30 0x81 0x81 0x81 0
14721 78 38 31 20 30 78 30 31 20 20 62 65 63 6f 6d 65 x81 0x01 become
14722 73 20 20 30 78 31 30 32 30 34 30 38 31 0a 2a 2a s 0x10204081.**
14723 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 6c 65 6e .** Variable len
14724 67 74 68 20 69 6e 74 65 67 65 72 73 20 61 72 65 gth integers are
14725 20 75 73 65 64 20 66 6f 72 20 72 6f 77 69 64 73 used for rowids
14726 20 61 6e 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 and to hold the
14727 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 number of.** by
14728 74 65 73 20 6f 66 20 6b 65 79 20 61 6e 64 20 64 tes of key and d
14729 61 74 61 20 69 6e 20 61 20 62 74 72 65 65 20 63 ata in a btree c
1472a 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ell..**.** The c
1472b 6f 6e 74 65 6e 74 20 6f 66 20 61 20 63 65 6c 6c ontent of a cell
1472c 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 looks like this
1472d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 :.**.** SIZE
1472e 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a DESCRIPTION.*
1472f 2a 20 20 20 20 20 20 34 20 20 20 20 20 50 61 67 * 4 Pag
14730 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
14731 6c 65 66 74 20 63 68 69 6c 64 2e 20 4f 6d 69 74 left child. Omit
14732 74 65 64 20 69 66 20 6c 65 61 66 20 66 6c 61 67 ted if leaf flag
14733 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 is set..**
14734 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 var Number of
14735 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 2e 20 bytes of data.
14736 4f 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 7a Omitted if the z
14737 65 72 6f 64 61 74 61 20 66 6c 61 67 20 69 73 20 erodata flag is
14738 73 65 74 2e 0a 2a 2a 20 20 20 20 20 76 61 72 20 set..** var
14739 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 Number of byt
1473a 65 73 20 6f 66 20 6b 65 79 2e 20 4f 72 20 74 68 es of key. Or th
1473b 65 20 6b 65 79 20 69 74 73 65 6c 66 20 69 66 20 e key itself if
1473c 69 6e 74 6b 65 79 20 66 6c 61 67 20 69 73 20 73 intkey flag is s
1473d 65 74 2e 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 et..** *
1473e 20 20 50 61 79 6c 6f 61 64 0a 2a 2a 20 20 20 20 Payload.**
1473f 20 20 34 20 20 20 20 20 46 69 72 73 74 20 70 61 4 First pa
14740 67 65 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c ge of the overfl
14741 6f 77 20 63 68 61 69 6e 2e 20 20 4f 6d 69 74 74 ow chain. Omitt
14742 65 64 20 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f ed if no overflo
14743 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72 66 6c 6f 77 w.**.** Overflow
14744 20 70 61 67 65 73 20 66 6f 72 6d 20 61 20 6c 69 pages form a li
14745 6e 6b 65 64 20 6c 69 73 74 2e 20 20 45 61 63 68 nked list. Each
14746 20 70 61 67 65 20 65 78 63 65 70 74 20 74 68 65 page except the
14747 20 6c 61 73 74 20 69 73 20 63 6f 6d 70 6c 65 74 last is complet
14748 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65 64 20 77 69 ely.** filled wi
14749 74 68 20 64 61 74 61 20 28 70 61 67 65 73 69 7a th data (pagesiz
1474a 65 20 2d 20 34 20 62 79 74 65 73 29 2e 20 20 54 e - 4 bytes). T
1474b 68 65 20 6c 61 73 74 20 70 61 67 65 20 63 61 6e he last page can
1474c 20 68 61 76 65 20 61 73 20 6c 69 74 74 6c 65 0a have as little.
1474d 2a 2a 20 61 73 20 31 20 62 79 74 65 20 6f 66 20 ** as 1 byte of
1474e 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 data..**.** S
1474f 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 IZE DESCRIPTI
14750 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 ON.** 4
14751 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 Page number of
14752 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 next overflow pa
14753 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 ge.** *
14754 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20 46 72 65 65 Data.**.** Free
14755 6c 69 73 74 20 70 61 67 65 73 20 63 6f 6d 65 20 list pages come
14756 69 6e 20 74 77 6f 20 73 75 62 74 79 70 65 73 3a in two subtypes:
14757 20 74 72 75 6e 6b 20 70 61 67 65 73 20 61 6e 64 trunk pages and
14758 20 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68 leaf pages. Th
14759 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61 64 65 72 e.** file header
1475a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 points to the f
1475b 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64 irst in a linked
1475c 20 6c 69 73 74 20 6f 66 20 74 72 75 6e 6b 20 70 list of trunk p
1475d 61 67 65 2e 20 20 45 61 63 68 20 74 72 75 6e 6b age. Each trunk
1475e 0a 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 73 20 .** page points
1475f 74 6f 20 6d 75 6c 74 69 70 6c 65 20 6c 65 61 66 to multiple leaf
14760 20 70 61 67 65 73 2e 20 20 54 68 65 20 63 6f 6e pages. The con
14761 74 65 6e 74 20 6f 66 20 61 20 6c 65 61 66 20 70 tent of a leaf p
14762 61 67 65 20 69 73 0a 2a 2a 20 75 6e 73 70 65 63 age is.** unspec
14763 69 66 69 65 64 2e 20 20 41 20 74 72 75 6e 6b 20 ified. A trunk
14764 70 61 67 65 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 page looks like
14765 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 this:.**.** S
14766 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 IZE DESCRIPTI
14767 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 ON.** 4
14768 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 Page number of
14769 6e 65 78 74 20 74 72 75 6e 6b 20 70 61 67 65 0a next trunk page.
1476a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 4e 75 ** 4 Nu
1476b 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 70 6f 69 mber of leaf poi
1476c 6e 74 65 72 73 20 6f 6e 20 74 68 69 73 20 70 61 nters on this pa
1476d 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 ge.** *
1476e 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 70 61 zero or more pa
1476f 67 65 73 20 6e 75 6d 62 65 72 73 20 6f 66 20 6c ges numbers of l
14770 65 61 76 65 73 0a 2a 2f 0a 0a 0a 2f 2a 20 54 68 eaves.*/.../* Th
14771 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 e following valu
14772 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d e is the maximum
14773 20 63 65 6c 6c 20 73 69 7a 65 20 61 73 73 75 6d cell size assum
14774 69 6e 67 20 61 20 6d 61 78 69 6d 75 6d 20 70 61 ing a maximum pa
14775 67 65 0a 2a 2a 20 73 69 7a 65 20 67 69 76 65 20 ge.** size give
14776 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e above..*/.#defin
14777 65 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 e MX_CELL_SIZE(p
14778 42 74 29 20 20 28 70 42 74 2d 3e 70 61 67 65 53 Bt) (pBt->pageS
14779 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54 68 65 20 6d ize-8)../* The m
1477a 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 aximum number of
1477b 20 63 65 6c 6c 73 20 6f 6e 20 61 20 73 69 6e 67 cells on a sing
1477c 6c 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 le page of the d
1477d 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 0a 2a atabase. This.*
1477e 2a 20 61 73 73 75 6d 65 73 20 61 20 6d 69 6e 69 * assumes a mini
1477f 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 6f 66 mum cell size of
14780 20 36 20 62 79 74 65 73 20 20 28 34 20 62 79 74 6 bytes (4 byt
14781 65 73 20 66 6f 72 20 74 68 65 20 63 65 6c 6c 20 es for the cell
14782 69 74 73 65 6c 66 0a 2a 2a 20 70 6c 75 73 20 32 itself.** plus 2
14783 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 20 69 bytes for the i
14784 6e 64 65 78 20 74 6f 20 74 68 65 20 63 65 6c 6c ndex to the cell
14785 20 69 6e 20 74 68 65 20 70 61 67 65 20 68 65 61 in the page hea
14786 64 65 72 29 2e 20 20 53 75 63 68 0a 2a 2a 20 73 der). Such.** s
14787 6d 61 6c 6c 20 63 65 6c 6c 73 20 77 69 6c 6c 20 mall cells will
14788 62 65 20 72 61 72 65 2c 20 62 75 74 20 74 68 65 be rare, but the
14789 79 20 61 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a y are possible..
1478a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 */.#define MX_CE
1478b 4c 4c 28 70 42 74 29 20 28 28 70 42 74 2d 3e 70 LL(pBt) ((pBt->p
1478c 61 67 65 53 69 7a 65 2d 38 29 2f 36 29 0a 0a 2f ageSize-8)/6)../
1478d 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 * Forward declar
1478e 61 74 69 6f 6e 73 20 2a 2f 0a 74 79 70 65 64 65 ations */.typede
1478f 66 20 73 74 72 75 63 74 20 4d 65 6d 50 61 67 65 f struct MemPage
14790 20 4d 65 6d 50 61 67 65 3b 0a 74 79 70 65 64 65 MemPage;.typede
14791 66 20 73 74 72 75 63 74 20 42 74 4c 6f 63 6b 20 f struct BtLock
14792 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 BtLock;../*.** T
14793 68 69 73 20 69 73 20 61 20 6d 61 67 69 63 20 73 his is a magic s
14794 74 72 69 6e 67 20 74 68 61 74 20 61 70 70 65 61 tring that appea
14795 72 73 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e rs at the beginn
14796 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 ing of every.**
14797 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 SQLite database
14798 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 64 65 6e in order to iden
14799 74 69 66 79 20 74 68 65 20 66 69 6c 65 20 61 73 tify the file as
1479a 20 61 20 72 65 61 6c 20 64 61 74 61 62 61 73 65 a real database
1479b 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 63 61 6e 20 ..**.** You can
1479c 63 68 61 6e 67 65 20 74 68 69 73 20 76 61 6c 75 change this valu
1479d 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d e at compile-tim
1479e 65 20 62 79 20 73 70 65 63 69 66 79 69 6e 67 20 e by specifying
1479f 61 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f 46 49 a.** -DSQLITE_FI
147a0 4c 45 5f 48 45 41 44 45 52 3d 22 2e 2e 2e 22 20 LE_HEADER="..."
147a1 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 on the compiler
147a2 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 2e 20 20 54 command-line. T
147a3 68 65 0a 2a 2a 20 68 65 61 64 65 72 20 6d 75 73 he.** header mus
147a4 74 20 62 65 20 65 78 61 63 74 6c 79 20 31 36 20 t be exactly 16
147a5 62 79 74 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 bytes including
147a6 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 the zero-termina
147a7 74 6f 72 20 73 6f 0a 2a 2a 20 74 68 65 20 73 74 tor so.** the st
147a8 72 69 6e 67 20 69 74 73 65 6c 66 20 73 68 6f 75 ring itself shou
147a9 6c 64 20 62 65 20 31 35 20 63 68 61 72 61 63 74 ld be 15 charact
147aa 65 72 73 20 6c 6f 6e 67 2e 20 20 49 66 20 79 6f ers long. If yo
147ab 75 20 63 68 61 6e 67 65 0a 2a 2a 20 74 68 65 20 u change.** the
147ac 68 65 61 64 65 72 2c 20 74 68 65 6e 20 79 6f 75 header, then you
147ad 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 r custom library
147ae 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c will not be abl
147af 65 20 74 6f 20 72 65 61 64 20 0a 2a 2a 20 64 61 e to read .** da
147b0 74 61 62 61 73 65 73 20 67 65 6e 65 72 61 74 65 tabases generate
147b1 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 61 72 d by the standar
147b2 64 20 74 6f 6f 6c 73 20 61 6e 64 20 74 68 65 20 d tools and the
147b3 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73 0a 2a standard tools.*
147b4 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 * will not be ab
147b5 6c 65 20 74 6f 20 72 65 61 64 20 64 61 74 61 62 le to read datab
147b6 61 73 65 73 20 63 72 65 61 74 65 64 20 62 79 20 ases created by
147b7 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 your custom libr
147b8 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ary..*/.#ifndef
147b9 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 SQLITE_FILE_HEAD
147ba 45 52 20 2f 2a 20 31 32 33 34 35 36 37 38 39 20 ER /* 123456789
147bb 31 32 33 34 35 36 20 2a 2f 0a 23 20 20 64 65 66 123456 */.# def
147bc 69 6e 65 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f ine SQLITE_FILE_
147bd 48 45 41 44 45 52 20 22 53 51 4c 69 74 65 20 66 HEADER "SQLite f
147be 6f 72 6d 61 74 20 33 22 0a 23 65 6e 64 69 66 0a ormat 3".#endif.
147bf 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 74 79 70 65 ./*.** Page type
147c0 20 66 6c 61 67 73 2e 20 20 41 6e 20 4f 52 65 64 flags. An ORed
147c1 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 combination of
147c2 74 68 65 73 65 20 66 6c 61 67 73 20 61 70 70 65 these flags appe
147c3 61 72 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 ar as the.** fir
147c4 73 74 20 62 79 74 65 20 6f 66 20 6f 6e 2d 64 69 st byte of on-di
147c5 73 6b 20 69 6d 61 67 65 20 6f 66 20 65 76 65 72 sk image of ever
147c6 79 20 42 54 72 65 65 20 70 61 67 65 2e 0a 2a 2f y BTree page..*/
147c7 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 49 4e 54 .#define PTF_INT
147c8 4b 45 59 20 20 20 20 30 78 30 31 0a 23 64 65 66 KEY 0x01.#def
147c9 69 6e 65 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 ine PTF_ZERODATA
147ca 20 20 30 78 30 32 0a 23 64 65 66 69 6e 65 20 50 0x02.#define P
147cb 54 46 5f 4c 45 41 46 44 41 54 41 20 20 30 78 30 TF_LEAFDATA 0x0
147cc 34 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 4.#define PTF_LE
147cd 41 46 20 20 20 20 20 20 30 78 30 38 0a 0a 2f 2a AF 0x08../*
147ce 0a 2a 2a 20 41 73 20 65 61 63 68 20 70 61 67 65 .** As each page
147cf 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 of the file is
147d0 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f loaded into memo
147d1 72 79 2c 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 ry, an instance
147d2 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
147d3 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 .** structure is
147d4 20 61 70 70 65 6e 64 65 64 20 61 6e 64 20 69 6e appended and in
147d5 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 itialized to zer
147d6 6f 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 o. This structu
147d7 72 65 20 73 74 6f 72 65 73 0a 2a 2a 20 69 6e 66 re stores.** inf
147d8 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 ormation about t
147d9 68 65 20 70 61 67 65 20 74 68 61 74 20 69 73 20 he page that is
147da 64 65 63 6f 64 65 64 20 66 72 6f 6d 20 74 68 65 decoded from the
147db 20 72 61 77 20 66 69 6c 65 20 70 61 67 65 2e 0a raw file page..
147dc 2a 2a 0a 2a 2a 20 54 68 65 20 70 50 61 72 65 6e **.** The pParen
147dd 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 62 t field points b
147de 61 63 6b 20 74 6f 20 74 68 65 20 70 61 72 65 6e ack to the paren
147df 74 20 70 61 67 65 2e 20 20 54 68 69 73 20 61 6c t page. This al
147e0 6c 6f 77 73 20 75 73 20 74 6f 0a 2a 2a 20 77 61 lows us to.** wa
147e1 6c 6b 20 75 70 20 74 68 65 20 42 54 72 65 65 20 lk up the BTree
147e2 66 72 6f 6d 20 61 6e 79 20 6c 65 61 66 20 74 6f from any leaf to
147e3 20 74 68 65 20 72 6f 6f 74 2e 20 20 43 61 72 65 the root. Care
147e4 20 6d 75 73 74 20 62 65 20 74 61 6b 65 6e 20 74 must be taken t
147e5 6f 0a 2a 2a 20 75 6e 72 65 66 28 29 20 74 68 65 o.** unref() the
147e6 20 70 61 72 65 6e 74 20 70 61 67 65 20 70 6f 69 parent page poi
147e7 6e 74 65 72 20 77 68 65 6e 20 74 68 69 73 20 70 nter when this p
147e8 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 age is no longer
147e9 20 72 65 66 65 72 65 6e 63 65 64 2e 0a 2a 2a 20 referenced..**
147ea 54 68 65 20 70 61 67 65 44 65 73 74 72 75 63 74 The pageDestruct
147eb 6f 72 28 29 20 72 6f 75 74 69 6e 65 20 68 61 6e or() routine han
147ec 64 6c 65 73 20 74 68 61 74 20 63 68 6f 72 65 2e dles that chore.
147ed 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 6f .**.** Access to
147ee 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f 66 20 74 all fields of t
147ef 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 his structure is
147f0 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 74 controlled by t
147f1 68 65 20 6d 75 74 65 78 0a 2a 2a 20 73 74 6f 72 he mutex.** stor
147f2 65 64 20 69 6e 20 4d 65 6d 50 61 67 65 2e 70 42 ed in MemPage.pB
147f3 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 t->mutex..*/.str
147f4 75 63 74 20 4d 65 6d 50 61 67 65 20 7b 0a 20 20 uct MemPage {.
147f5 75 38 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20 u8 isInit;
147f6 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
147f7 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 69 74 69 previously initi
147f8 61 6c 69 7a 65 64 2e 20 4d 55 53 54 20 42 45 20 alized. MUST BE
147f9 46 49 52 53 54 21 20 2a 2f 0a 20 20 75 38 20 6e FIRST! */. u8 n
147fa 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20 20 Overflow;
147fb 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 76 /* Number of ov
147fc 65 72 66 6c 6f 77 20 63 65 6c 6c 20 62 6f 64 69 erflow cell bodi
147fd 65 73 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 2a 2f es in aCell[] */
147fe 0a 20 20 75 38 20 69 6e 74 4b 65 79 3b 20 20 20 . u8 intKey;
147ff 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
14800 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 69 if intkey flag i
14801 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 20 6c 65 s set */. u8 le
14802 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 af;
14803 2f 2a 20 54 72 75 65 20 69 66 20 6c 65 61 66 20 /* True if leaf
14804 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f 0a 20 flag is set */.
14805 20 75 38 20 68 61 73 44 61 74 61 3b 20 20 20 20 u8 hasData;
14806 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
14807 20 74 68 69 73 20 70 61 67 65 20 73 74 6f 72 65 this page store
14808 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 68 s data */. u8 h
14809 64 72 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 drOffset;
1480a 20 2f 2a 20 31 30 30 20 66 6f 72 20 70 61 67 65 /* 100 for page
1480b 20 31 2e 20 20 30 20 6f 74 68 65 72 77 69 73 65 1. 0 otherwise
1480c 20 2a 2f 0a 20 20 75 38 20 63 68 69 6c 64 50 74 */. u8 childPt
1480d 72 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 30 20 rSize; /* 0
1480e 69 66 20 6c 65 61 66 3d 3d 31 2e 20 20 34 20 69 if leaf==1. 4 i
1480f 66 20 6c 65 61 66 3d 3d 30 20 2a 2f 0a 20 20 75 f leaf==0 */. u
14810 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20 16 maxLocal;
14811 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 /* Copy of B
14812 74 53 68 61 72 65 64 2e 6d 61 78 4c 6f 63 61 6c tShared.maxLocal
14813 20 6f 72 20 42 74 53 68 61 72 65 64 2e 6d 61 78 or BtShared.max
14814 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36 20 6d 69 Leaf */. u16 mi
14815 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 2f nLocal; /
14816 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 68 61 72 * Copy of BtShar
14817 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20 6f 72 20 42 ed.minLocal or B
14818 74 53 68 61 72 65 64 2e 6d 69 6e 4c 65 61 66 20 tShared.minLeaf
14819 2a 2f 0a 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 */. u16 cellOff
1481a 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64 set; /* Ind
1481b 65 78 20 69 6e 20 61 44 61 74 61 20 6f 66 20 66 ex in aData of f
1481c 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 irst cell pointe
1481d 72 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72 65 65 r */. u16 nFree
1481e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
1481f 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 62 79 umber of free by
14820 74 65 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20 tes on the page
14821 2a 2f 0a 20 20 75 31 36 20 6e 43 65 6c 6c 3b 20 */. u16 nCell;
14822 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
14823 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 ber of cells on
14824 74 68 69 73 20 70 61 67 65 2c 20 6c 6f 63 61 6c this page, local
14825 20 61 6e 64 20 6f 76 66 6c 20 2a 2f 0a 20 20 75 and ovfl */. u
14826 31 36 20 6d 61 73 6b 50 61 67 65 3b 20 20 20 20 16 maskPage;
14827 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66 6f 72 20 /* Mask for
14828 70 61 67 65 20 6f 66 66 73 65 74 20 2a 2f 0a 20 page offset */.
14829 20 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 65 6c struct _OvflCel
1482a 6c 20 7b 20 20 20 2f 2a 20 43 65 6c 6c 73 20 74 l { /* Cells t
1482b 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 hat will not fit
1482c 20 6f 6e 20 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 on aData[] */.
1482d 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 u8 *pCell;
1482e 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
1482f 72 73 20 74 6f 20 74 68 65 20 62 6f 64 79 20 6f rs to the body o
14830 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 f the overflow c
14831 65 6c 6c 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 ell */. u16 i
14832 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f dx; /
14833 2a 20 49 6e 73 65 72 74 20 74 68 69 73 20 63 65 * Insert this ce
14834 6c 6c 20 62 65 66 6f 72 65 20 69 64 78 2d 74 68 ll before idx-th
14835 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f 77 20 63 65 non-overflow ce
14836 6c 6c 20 2a 2f 0a 20 20 7d 20 61 4f 76 66 6c 5b ll */. } aOvfl[
14837 35 5d 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 5];. BtShared *
14838 70 42 74 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f pBt; /* Po
14839 69 6e 74 65 72 20 74 6f 20 42 74 53 68 61 72 65 inter to BtShare
1483a 64 20 74 68 61 74 20 74 68 69 73 20 70 61 67 65 d that this page
1483b 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 is part of */.
1483c 20 75 38 20 2a 61 44 61 74 61 3b 20 20 20 20 20 u8 *aData;
1483d 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
1483e 20 74 6f 20 64 69 73 6b 20 69 6d 61 67 65 20 6f to disk image o
1483f 66 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20 f the page data
14840 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 */. DbPage *pDb
14841 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 50 61 67 Page; /* Pag
14842 65 72 20 70 61 67 65 20 68 61 6e 64 6c 65 20 2a er page handle *
14843 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 /. Pgno pgno;
14844 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
14845 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69 73 number for this
14846 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a page */.};../*.
14847 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 ** The in-memory
14848 20 69 6d 61 67 65 20 6f 66 20 61 20 64 69 73 6b image of a disk
14849 20 70 61 67 65 20 68 61 73 20 74 68 65 20 61 75 page has the au
1484a 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 xiliary informat
1484b 69 6f 6e 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 ion appended.**
1484c 74 6f 20 74 68 65 20 65 6e 64 2e 20 20 45 58 54 to the end. EXT
1484d 52 41 5f 53 49 5a 45 20 69 73 20 74 68 65 20 6e RA_SIZE is the n
1484e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
1484f 66 20 73 70 61 63 65 20 6e 65 65 64 65 64 20 74 f space needed t
14850 6f 20 68 6f 6c 64 0a 2a 2a 20 74 68 61 74 20 65 o hold.** that e
14851 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e xtra information
14852 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 54 ..*/.#define EXT
14853 52 41 5f 53 49 5a 45 20 73 69 7a 65 6f 66 28 4d RA_SIZE sizeof(M
14854 65 6d 50 61 67 65 29 0a 0a 2f 2a 0a 2a 2a 20 41 emPage)../*.** A
14855 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 linked list of
14856 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
14857 72 75 63 74 75 72 65 73 20 69 73 20 73 74 6f 72 ructures is stor
14858 65 64 20 61 74 20 42 74 53 68 61 72 65 64 2e 70 ed at BtShared.p
14859 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 61 Lock..** Locks a
1485a 72 65 20 61 64 64 65 64 20 28 6f 72 20 75 70 67 re added (or upg
1485b 72 61 64 65 64 20 66 72 6f 6d 20 52 45 41 44 5f raded from READ_
1485c 4c 4f 43 4b 20 74 6f 20 57 52 49 54 45 5f 4c 4f LOCK to WRITE_LO
1485d 43 4b 29 20 77 68 65 6e 20 61 20 63 75 72 73 6f CK) when a curso
1485e 72 20 0a 2a 2a 20 69 73 20 6f 70 65 6e 65 64 20 r .** is opened
1485f 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 on the table wit
14860 68 20 72 6f 6f 74 20 70 61 67 65 20 42 74 53 68 h root page BtSh
14861 61 72 65 64 2e 69 54 61 62 6c 65 2e 20 4c 6f 63 ared.iTable. Loc
14862 6b 73 20 61 72 65 20 72 65 6d 6f 76 65 64 0a 2a ks are removed.*
14863 2a 20 66 72 6f 6d 20 74 68 69 73 20 6c 69 73 74 * from this list
14864 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 when a transact
14865 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 ion is committed
14866 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c or rolled back,
14867 20 6f 72 20 77 68 65 6e 0a 2a 2a 20 61 20 62 74 or when.** a bt
14868 72 65 65 20 68 61 6e 64 6c 65 20 69 73 20 63 6c ree handle is cl
14869 6f 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 osed..*/.struct
1486a 42 74 4c 6f 63 6b 20 7b 0a 20 20 42 74 72 65 65 BtLock {. Btree
1486b 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20 20 20 *pBtree;
1486c 20 2f 2a 20 42 74 72 65 65 20 68 61 6e 64 6c 65 /* Btree handle
1486d 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6c 6f holding this lo
1486e 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 61 ck */. Pgno iTa
1486f 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ble; /*
14870 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61 Root page of ta
14871 62 6c 65 20 2a 2f 0a 20 20 75 38 20 65 4c 6f 63 ble */. u8 eLoc
14872 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f k; /
14873 2a 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 * READ_LOCK or W
14874 52 49 54 45 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 42 RITE_LOCK */. B
14875 74 4c 6f 63 6b 20 2a 70 4e 65 78 74 3b 20 20 20 tLock *pNext;
14876 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 /* Next in
14877 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c BtShared.pLock l
14878 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 43 61 ist */.};../* Ca
14879 6e 64 69 64 61 74 65 20 76 61 6c 75 65 73 20 66 ndidate values f
1487a 6f 72 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 or BtLock.eLock
1487b 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f */.#define READ_
1487c 4c 4f 43 4b 20 20 20 20 20 31 0a 23 64 65 66 69 LOCK 1.#defi
1487d 6e 65 20 57 52 49 54 45 5f 4c 4f 43 4b 20 20 20 ne WRITE_LOCK
1487e 20 32 0a 0a 2f 2a 20 41 20 42 74 72 65 65 20 68 2../* A Btree h
1487f 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 41 20 64 61 andle.**.** A da
14880 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14881 6e 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 n contains a poi
14882 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 nter to an insta
14883 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6f nce of.** this o
14884 62 6a 65 63 74 20 66 6f 72 20 65 76 65 72 79 20 bject for every
14885 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 68 database file th
14886 61 74 20 69 74 20 68 61 73 20 6f 70 65 6e 2e 20 at it has open.
14887 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a This structure.
14888 2a 2a 20 69 73 20 6f 70 61 71 75 65 20 74 6f 20 ** is opaque to
14889 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
1488a 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 64 61 nection. The da
1488b 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1488c 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 73 65 65 20 n cannot.** see
1488d 74 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 the internals of
1488e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
1488f 61 6e 64 20 6f 6e 6c 79 20 64 65 61 6c 73 20 77 and only deals w
14890 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a ith pointers to.
14891 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72 ** this structur
14892 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 73 6f 6d e..**.** For som
14893 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 e database files
14894 2c 20 74 68 65 20 73 61 6d 65 20 75 6e 64 65 72 , the same under
14895 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 lying database c
14896 61 63 68 65 20 6d 69 67 68 74 20 62 65 20 0a 2a ache might be .*
14897 2a 20 73 68 61 72 65 64 20 62 65 74 77 65 65 6e * shared between
14898 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 6e 65 63 multiple connec
14899 74 69 6f 6e 73 2e 20 20 49 6e 20 74 68 61 74 20 tions. In that
1489a 63 61 73 65 2c 20 65 61 63 68 20 63 6f 6e 74 65 case, each conte
1489b 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 69 74 20 ction.** has it
1489c 6f 77 6e 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 own pointer to t
1489d 68 69 73 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 his object. But
1489e 20 65 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f each instance o
1489f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 0a 2a 2a f this object.**
148a0 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 points to the s
148a1 61 6d 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a ame BtShared obj
148a2 65 63 74 2e 20 20 54 68 65 20 64 61 74 61 62 61 ect. The databa
148a3 73 65 20 63 61 63 68 65 20 61 6e 64 20 74 68 65 se cache and the
148a4 0a 2a 2a 20 73 63 68 65 6d 61 20 61 73 73 6f 63 .** schema assoc
148a5 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 64 iated with the d
148a6 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 72 65 atabase file are
148a7 20 61 6c 6c 20 63 6f 6e 74 61 69 6e 65 64 20 77 all contained w
148a8 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 42 74 53 ithin.** the BtS
148a9 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a 2a 2a hared object..**
148aa 0a 2a 2a 20 41 6c 6c 20 66 69 65 6c 64 73 20 69 .** All fields i
148ab 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 n this structure
148ac 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e are accessed un
148ad 64 65 72 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 der sqlite3.mute
148ae 78 2e 0a 2a 2a 20 54 68 65 20 70 42 74 20 70 6f x..** The pBt po
148af 69 6e 74 65 72 20 69 74 73 65 6c 66 20 6d 61 79 inter itself may
148b0 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20 not be changed
148b1 77 68 69 6c 65 20 74 68 65 72 65 20 65 78 69 73 while there exis
148b2 74 73 20 63 75 72 73 6f 72 73 20 0a 2a 2a 20 69 ts cursors .** i
148b3 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 64 n the referenced
148b4 20 42 74 53 68 61 72 65 64 20 74 68 61 74 20 70 BtShared that p
148b5 6f 69 6e 74 20 62 61 63 6b 20 74 6f 20 74 68 69 oint back to thi
148b6 73 20 42 74 72 65 65 20 73 69 6e 63 65 20 74 68 s Btree since th
148b7 6f 73 65 0a 2a 2a 20 63 75 72 73 6f 72 73 20 68 ose.** cursors h
148b8 61 76 65 20 74 6f 20 64 6f 20 67 6f 20 74 68 72 ave to do go thr
148b9 6f 75 67 68 20 74 68 69 73 20 42 74 72 65 65 20 ough this Btree
148ba 74 6f 20 66 69 6e 64 20 74 68 65 69 72 20 42 74 to find their Bt
148bb 53 68 61 72 65 64 20 61 6e 64 0a 2a 2a 20 74 68 Shared and.** th
148bc 65 79 20 6f 66 74 65 6e 20 64 6f 20 73 6f 20 77 ey often do so w
148bd 69 74 68 6f 75 74 20 68 6f 6c 64 69 6e 67 20 73 ithout holding s
148be 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2f qlite3.mutex..*/
148bf 0a 73 74 72 75 63 74 20 42 74 72 65 65 20 7b 0a .struct Btree {.
148c0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
148c1 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
148c2 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
148c3 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 62 74 72 holding this btr
148c4 65 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 ee */. BtShared
148c5 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 53 68 *pBt; /* Sh
148c6 61 72 61 62 6c 65 20 63 6f 6e 74 65 6e 74 20 6f arable content o
148c7 66 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a f this btree */.
148c8 20 20 75 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 u8 inTrans;
148c9 20 20 20 20 20 2f 2a 20 54 52 41 4e 53 5f 4e 4f /* TRANS_NO
148ca 4e 45 2c 20 54 52 41 4e 53 5f 52 45 41 44 20 6f NE, TRANS_READ o
148cb 72 20 54 52 41 4e 53 5f 57 52 49 54 45 20 2a 2f r TRANS_WRITE */
148cc 0a 20 20 75 38 20 73 68 61 72 61 62 6c 65 3b 20 . u8 sharable;
148cd 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
148ce 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 70 42 we can share pB
148cf 74 20 77 69 74 68 20 61 6e 6f 74 68 65 72 20 64 t with another d
148d0 62 20 2a 2f 0a 20 20 75 38 20 6c 6f 63 6b 65 64 b */. u8 locked
148d1 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ; /* Tru
148d2 65 20 69 66 20 64 62 20 63 75 72 72 65 6e 74 6c e if db currentl
148d3 79 20 68 61 73 20 70 42 74 20 6c 6f 63 6b 65 64 y has pBt locked
148d4 20 2a 2f 0a 20 20 69 6e 74 20 77 61 6e 74 54 6f */. int wantTo
148d5 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 Lock; /* Numb
148d6 65 72 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c er of nested cal
148d7 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 ls to sqlite3Btr
148d8 65 65 45 6e 74 65 72 28 29 20 2a 2f 0a 20 20 69 eeEnter() */. i
148d9 6e 74 20 6e 42 61 63 6b 75 70 3b 20 20 20 20 20 nt nBackup;
148da 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
148db 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 73 ackup operations
148dc 20 72 65 61 64 69 6e 67 20 74 68 69 73 20 62 74 reading this bt
148dd 72 65 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a ree */. Btree *
148de 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4c pNext; /* L
148df 69 73 74 20 6f 66 20 6f 74 68 65 72 20 73 68 61 ist of other sha
148e0 72 61 62 6c 65 20 42 74 72 65 65 73 20 66 72 6f rable Btrees fro
148e1 6d 20 74 68 65 20 73 61 6d 65 20 64 62 20 2a 2f m the same db */
148e2 0a 20 20 42 74 72 65 65 20 2a 70 50 72 65 76 3b . Btree *pPrev;
148e3 20 20 20 20 20 20 2f 2a 20 42 61 63 6b 20 70 6f /* Back po
148e4 69 6e 74 65 72 20 6f 66 20 74 68 65 20 73 61 6d inter of the sam
148e5 65 20 6c 69 73 74 20 2a 2f 0a 23 69 66 6e 64 65 e list */.#ifnde
148e6 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
148e7 41 52 45 44 5f 43 41 43 48 45 0a 20 20 42 74 4c ARED_CACHE. BtL
148e8 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20 20 20 20 ock lock;
148e9 2f 2a 20 4f 62 6a 65 63 74 20 75 73 65 64 20 74 /* Object used t
148ea 6f 20 6c 6f 63 6b 20 70 61 67 65 20 31 20 2a 2f o lock page 1 */
148eb 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a .#endif.};../*.*
148ec 2a 20 42 74 72 65 65 2e 69 6e 54 72 61 6e 73 20 * Btree.inTrans
148ed 6d 61 79 20 74 61 6b 65 20 6f 6e 65 20 6f 66 20 may take one of
148ee 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 the following va
148ef 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 lues..**.** If t
148f0 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 65 he shared-data e
148f1 78 74 65 6e 73 69 6f 6e 20 69 73 20 65 6e 61 62 xtension is enab
148f2 6c 65 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62 led, there may b
148f3 65 20 6d 75 6c 74 69 70 6c 65 20 75 73 65 72 73 e multiple users
148f4 0a 2a 2a 20 6f 66 20 74 68 65 20 42 74 72 65 65 .** of the Btree
148f5 20 73 74 72 75 63 74 75 72 65 2e 20 41 74 20 6d structure. At m
148f6 6f 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 ost one of these
148f7 20 6d 61 79 20 6f 70 65 6e 20 61 20 77 72 69 74 may open a writ
148f8 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a e transaction,.*
148f9 2a 20 62 75 74 20 61 6e 79 20 6e 75 6d 62 65 72 * but any number
148fa 20 6d 61 79 20 68 61 76 65 20 61 63 74 69 76 65 may have active
148fb 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f read transactio
148fc 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 ns..*/.#define T
148fd 52 41 4e 53 5f 4e 4f 4e 45 20 20 30 0a 23 64 65 RANS_NONE 0.#de
148fe 66 69 6e 65 20 54 52 41 4e 53 5f 52 45 41 44 20 fine TRANS_READ
148ff 20 31 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 1.#define TRANS
14900 5f 57 52 49 54 45 20 32 0a 0a 2f 2a 0a 2a 2a 20 _WRITE 2../*.**
14901 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
14902 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 his object repre
14903 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 64 sents a single d
14904 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
14905 20 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 .** A single da
14906 74 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20 tabase file can
14907 62 65 20 69 6e 20 75 73 65 20 61 73 20 74 68 65 be in use as the
14908 20 73 61 6d 65 20 74 69 6d 65 20 62 79 20 74 77 same time by tw
14909 6f 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 64 61 74 o.** or more dat
1490a 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1490b 73 2e 20 20 57 68 65 6e 20 74 77 6f 20 6f 72 20 s. When two or
1490c 6d 6f 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 more connections
1490d 20 61 72 65 0a 2a 2a 20 73 68 61 72 69 6e 67 20 are.** sharing
1490e 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
1490f 65 20 66 69 6c 65 2c 20 65 61 63 68 20 63 6f 6e e file, each con
14910 6e 65 63 74 69 6f 6e 20 68 61 73 20 69 74 20 6f nection has it o
14911 77 6e 0a 2a 2a 20 70 72 69 76 61 74 65 20 42 74 wn.** private Bt
14912 72 65 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 ree object for t
14913 68 65 20 66 69 6c 65 20 61 6e 64 20 65 61 63 68 he file and each
14914 20 6f 66 20 74 68 6f 73 65 20 42 74 72 65 65 73 of those Btrees
14915 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 74 68 points.** to th
14916 69 73 20 6f 6e 65 20 42 74 53 68 61 72 65 64 20 is one BtShared
14917 6f 62 6a 65 63 74 2e 20 20 42 74 53 68 61 72 65 object. BtShare
14918 64 2e 6e 52 65 66 20 69 73 20 74 68 65 20 6e 75 d.nRef is the nu
14919 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 6f 6e 6e 65 mber of.** conne
1491a 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c 79 ctions currently
1491b 20 73 68 61 72 69 6e 67 20 74 68 69 73 20 64 61 sharing this da
1491c 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
1491d 2a 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68 69 ** Fields in thi
1491e 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 s structure are
1491f 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 accessed under t
14920 68 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 he BtShared.mute
14921 78 0a 2a 2a 20 6d 75 74 65 78 2c 20 65 78 63 65 x.** mutex, exce
14922 70 74 20 66 6f 72 20 6e 52 65 66 20 61 6e 64 20 pt for nRef and
14923 70 4e 65 78 74 20 77 68 69 63 68 20 61 72 65 20 pNext which are
14924 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 accessed under t
14925 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 53 51 4c he.** global SQL
14926 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
14927 5f 4d 41 53 54 45 52 20 6d 75 74 65 78 2e 20 20 _MASTER mutex.
14928 54 68 65 20 70 50 61 67 65 72 20 66 69 65 6c 64 The pPager field
14929 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d .** may not be m
1492a 6f 64 69 66 69 65 64 20 6f 6e 63 65 20 69 74 20 odified once it
1492b 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 73 65 74 is initially set
1492c 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66 as long as nRef
1492d 3e 30 2e 0a 2a 2a 20 54 68 65 20 70 53 63 68 65 >0..** The pSche
1492e 6d 61 20 66 69 65 6c 64 20 6d 61 79 20 62 65 20 ma field may be
1492f 73 65 74 20 6f 6e 63 65 20 75 6e 64 65 72 20 42 set once under B
14930 74 53 68 61 72 65 64 2e 6d 75 74 65 78 20 61 6e tShared.mutex an
14931 64 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 20 d.** thereafter
14932 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 73 20 is unchanged as
14933 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a long as nRef>0..
14934 2a 2a 0a 2a 2a 20 69 73 50 65 6e 64 69 6e 67 3a **.** isPending:
14935 0a 2a 2a 0a 2a 2a 20 20 20 49 66 20 61 20 42 74 .**.** If a Bt
14936 53 68 61 72 65 64 20 63 6c 69 65 6e 74 20 66 61 Shared client fa
14937 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 ils to obtain a
14938 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61 20 write-lock on a
14939 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 74 61 database.** ta
1493a 62 6c 65 20 28 62 65 63 61 75 73 65 20 74 68 65 ble (because the
1493b 72 65 20 65 78 69 73 74 73 20 6f 6e 65 20 6f 72 re exists one or
1493c 20 6d 6f 72 65 20 72 65 61 64 2d 6c 6f 63 6b 73 more read-locks
1493d 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 29 2c 0a on the table),.
1493e 2a 2a 20 20 20 74 68 65 20 73 68 61 72 65 64 2d ** the shared-
1493f 63 61 63 68 65 20 65 6e 74 65 72 73 20 27 70 65 cache enters 'pe
14940 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 74 nding-lock' stat
14941 65 20 61 6e 64 20 69 73 50 65 6e 64 69 6e 67 20 e and isPending
14942 69 73 0a 2a 2a 20 20 20 73 65 74 20 74 6f 20 74 is.** set to t
14943 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 rue..**.** The
14944 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65 shared-cache le
14945 61 76 65 73 20 74 68 65 20 27 70 65 6e 64 69 6e aves the 'pendin
14946 67 20 6c 6f 63 6b 27 20 73 74 61 74 65 20 77 68 g lock' state wh
14947 65 6e 20 65 69 74 68 65 72 20 6f 66 0a 2a 2a 20 en either of.**
14948 20 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 the following
14949 6f 63 63 75 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 occur:.**.**
1494a 20 31 29 20 54 68 65 20 63 75 72 72 65 6e 74 20 1) The current
1494b 77 72 69 74 65 72 20 28 42 74 53 68 61 72 65 64 writer (BtShared
1494c 2e 70 57 72 69 74 65 72 29 20 63 6f 6e 63 6c 75 .pWriter) conclu
1494d 64 65 73 20 69 74 73 20 74 72 61 6e 73 61 63 74 des its transact
1494e 69 6f 6e 2c 20 4f 52 0a 2a 2a 20 20 20 20 20 32 ion, OR.** 2
1494f 29 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 ) The number of
14950 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 6f 74 locks held by ot
14951 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 her connections
14952 64 72 6f 70 73 20 74 6f 20 7a 65 72 6f 2e 0a 2a drops to zero..*
14953 2a 0a 2a 2a 20 20 20 77 68 69 6c 65 20 69 6e 20 *.** while in
14954 74 68 65 20 27 70 65 6e 64 69 6e 67 2d 6c 6f 63 the 'pending-loc
14955 6b 27 20 73 74 61 74 65 2c 20 6e 6f 20 63 6f 6e k' state, no con
14956 6e 65 63 74 69 6f 6e 20 6d 61 79 20 73 74 61 72 nection may star
14957 74 20 61 20 6e 65 77 0a 2a 2a 20 20 20 74 72 61 t a new.** tra
14958 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nsaction..**.**
14959 20 20 54 68 69 73 20 66 65 61 74 75 72 65 20 69 This feature i
1495a 73 20 69 6e 63 6c 75 64 65 64 20 74 6f 20 68 65 s included to he
1495b 6c 70 20 70 72 65 76 65 6e 74 20 77 72 69 74 65 lp prevent write
1495c 72 2d 73 74 61 72 76 61 74 69 6f 6e 2e 0a 2a 2f r-starvation..*/
1495d 0a 73 74 72 75 63 74 20 42 74 53 68 61 72 65 64 .struct BtShared
1495e 20 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 {. Pager *pPag
1495f 65 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 er; /* Th
14960 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a e page cache */.
14961 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
14962 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
14963 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 ase connection c
14964 75 72 72 65 6e 74 6c 79 20 75 73 69 6e 67 20 74 urrently using t
14965 68 69 73 20 42 74 72 65 65 20 2a 2f 0a 20 20 42 his Btree */. B
14966 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 tCursor *pCursor
14967 3b 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f ; /* A list o
14968 66 20 61 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f f all open curso
14969 72 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 rs */. MemPage
1496a 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 2f 2a *pPage1; /*
1496b 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 First page of t
1496c 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 he database */.
1496d 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 u8 readOnly;
1496e 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
1496f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 f the underlying
14970 20 66 69 6c 65 20 69 73 20 72 65 61 64 6f 6e 6c file is readonl
14971 79 20 2a 2f 0a 20 20 75 38 20 70 61 67 65 53 69 y */. u8 pageSi
14972 7a 65 46 69 78 65 64 3b 20 20 20 20 20 2f 2a 20 zeFixed; /*
14973 54 72 75 65 20 69 66 20 74 68 65 20 70 61 67 65 True if the page
14974 20 73 69 7a 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e size can no lon
14975 67 65 72 20 62 65 20 63 68 61 6e 67 65 64 20 2a ger be changed *
14976 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
14977 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
14978 0a 20 20 75 38 20 61 75 74 6f 56 61 63 75 75 6d . u8 autoVacuum
14979 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 ; /* True
1497a 20 69 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 if auto-vacuum
1497b 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 is enabled */.
1497c 75 38 20 69 6e 63 72 56 61 63 75 75 6d 3b 20 20 u8 incrVacuum;
1497d 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
1497e 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 69 73 20 incr-vacuum is
1497f 65 6e 61 62 6c 65 64 20 2a 2f 0a 23 65 6e 64 69 enabled */.#endi
14980 66 0a 20 20 75 31 36 20 70 61 67 65 53 69 7a 65 f. u16 pageSize
14981 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 ; /* Tot
14982 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 al number of byt
14983 65 73 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a es on a page */.
14984 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a 65 u16 usableSize
14985 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ; /* Numbe
14986 72 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65 r of usable byte
14987 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a s on each page *
14988 2f 0a 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c /. u16 maxLocal
14989 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 ; /* Max
1498a 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f imum local paylo
1498b 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 ad in non-LEAFDA
1498c 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 75 TA tables */. u
1498d 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 16 minLocal;
1498e 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 /* Minimum
1498f 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e local payload in
14990 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74 61 non-LEAFDATA ta
14991 62 6c 65 73 20 2a 2f 0a 20 20 75 31 36 20 6d 61 bles */. u16 ma
14992 78 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 xLeaf;
14993 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c /* Maximum local
14994 20 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 payload in a LE
14995 41 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a AFDATA table */.
14996 20 20 75 31 36 20 6d 69 6e 4c 65 61 66 3b 20 20 u16 minLeaf;
14997 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d /* Minim
14998 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 um local payload
14999 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 in a LEAFDATA t
1499a 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 able */. u8 inT
1499b 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 ransaction;
1499c 2f 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 73 /* Transaction s
1499d 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 tate */. int nT
1499e 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 ransaction;
1499f 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70 65 /* Number of ope
149a0 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 28 n transactions (
149a1 72 65 61 64 20 2b 20 77 72 69 74 65 29 20 2a 2f read + write) */
149a2 0a 20 20 76 6f 69 64 20 2a 70 53 63 68 65 6d 61 . void *pSchema
149a3 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e ; /* Poin
149a4 74 65 72 20 74 6f 20 73 70 61 63 65 20 61 6c 6c ter to space all
149a5 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 ocated by sqlite
149a6 33 42 74 72 65 65 53 63 68 65 6d 61 28 29 20 2a 3BtreeSchema() *
149a7 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65 /. void (*xFree
149a8 53 63 68 65 6d 61 29 28 76 6f 69 64 2a 29 3b 20 Schema)(void*);
149a9 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 /* Destructor f
149aa 6f 72 20 42 74 53 68 61 72 65 64 2e 70 53 63 68 or BtShared.pSch
149ab 65 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ema */. sqlite3
149ac 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 2f _mutex *mutex; /
149ad 2a 20 4e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 * Non-recursive
149ae 6d 75 74 65 78 20 72 65 71 75 69 72 65 64 20 74 mutex required t
149af 6f 20 61 63 63 65 73 73 20 74 68 69 73 20 73 74 o access this st
149b0 72 75 63 74 20 2a 2f 0a 20 20 42 69 74 76 65 63 ruct */. Bitvec
149b1 20 2a 70 48 61 73 43 6f 6e 74 65 6e 74 3b 20 20 *pHasContent;
149b2 2f 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73 20 /* Set of pages
149b3 6d 6f 76 65 64 20 74 6f 20 66 72 65 65 2d 6c 69 moved to free-li
149b4 73 74 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 st this transact
149b5 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ion */.#ifndef S
149b6 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
149b7 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 52 D_CACHE. int nR
149b8 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ef;
149b9 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 /* Number of ref
149ba 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69 73 20 erences to this
149bb 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 42 structure */. B
149bc 74 53 68 61 72 65 64 20 2a 70 4e 65 78 74 3b 20 tShared *pNext;
149bd 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20 /* Next on
149be 61 20 6c 69 73 74 20 6f 66 20 73 68 61 72 61 62 a list of sharab
149bf 6c 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 le BtShared stru
149c0 63 74 73 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 cts */. BtLock
149c1 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f *pLock; /
149c2 2a 20 4c 69 73 74 20 6f 66 20 6c 6f 63 6b 73 20 * List of locks
149c3 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 73 68 61 held on this sha
149c4 72 65 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 red-btree struct
149c5 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 57 72 */. Btree *pWr
149c6 69 74 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 42 iter; /* B
149c7 74 72 65 65 20 77 69 74 68 20 63 75 72 72 65 6e tree with curren
149c8 74 6c 79 20 6f 70 65 6e 20 77 72 69 74 65 20 74 tly open write t
149c9 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 ransaction */.
149ca 75 38 20 69 73 45 78 63 6c 75 73 69 76 65 3b 20 u8 isExclusive;
149cb 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
149cc 20 70 57 72 69 74 65 72 20 68 61 73 20 61 6e 20 pWriter has an
149cd 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f EXCLUSIVE lock o
149ce 6e 20 74 68 65 20 64 62 20 2a 2f 0a 20 20 75 38 n the db */. u8
149cf 20 69 73 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 isPending;
149d0 20 20 20 20 2f 2a 20 49 66 20 77 61 69 74 69 6e /* If waitin
149d1 67 20 66 6f 72 20 72 65 61 64 2d 6c 6f 63 6b 73 g for read-locks
149d2 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 23 65 6e to clear */.#en
149d3 64 69 66 0a 20 20 75 38 20 2a 70 54 6d 70 53 70 dif. u8 *pTmpSp
149d4 61 63 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 ace; /* B
149d5 74 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a 65 tShared.pageSize
149d6 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 bytes of space
149d7 66 6f 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a 7d for tmp use */.}
149d8 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 ;../*.** An inst
149d9 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
149da 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
149db 69 73 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 is used to hold
149dc 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 information.** a
149dd 62 6f 75 74 20 61 20 63 65 6c 6c 2e 20 20 54 68 bout a cell. Th
149de 65 20 70 61 72 73 65 43 65 6c 6c 50 74 72 28 29 e parseCellPtr()
149df 20 66 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 73 20 function fills
149e0 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 in this structur
149e1 65 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20 69 6e e.** based on in
149e2 66 6f 72 6d 61 74 69 6f 6e 20 65 78 74 72 61 63 formation extrac
149e3 74 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 64 t from the raw d
149e4 69 73 6b 20 70 61 67 65 2e 0a 2a 2f 0a 74 79 70 isk page..*/.typ
149e5 65 64 65 66 20 73 74 72 75 63 74 20 43 65 6c 6c edef struct Cell
149e6 49 6e 66 6f 20 43 65 6c 6c 49 6e 66 6f 3b 0a 73 Info CellInfo;.s
149e7 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 7b truct CellInfo {
149e8 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 . u8 *pCell;
149e9 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
149ea 74 68 65 20 73 74 61 72 74 20 6f 66 20 63 65 6c the start of cel
149eb 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 l content */. i
149ec 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a 64 nKey; /*
149ed 20 54 68 65 20 6b 65 79 20 66 6f 72 20 49 4e 54 The key for INT
149ee 4b 45 59 20 74 61 62 6c 65 73 2c 20 6f 72 20 6e KEY tables, or n
149ef 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
149f0 6e 20 6b 65 79 20 2a 2f 0a 20 20 75 33 32 20 6e n key */. u32 n
149f1 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 4e 75 6d Data; /* Num
149f2 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
149f3 64 61 74 61 20 2a 2f 0a 20 20 75 33 32 20 6e 50 data */. u32 nP
149f4 61 79 6c 6f 61 64 3b 20 20 2f 2a 20 54 6f 74 61 ayload; /* Tota
149f5 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c l amount of payl
149f6 6f 61 64 20 2a 2f 0a 20 20 75 31 36 20 6e 48 65 oad */. u16 nHe
149f7 61 64 65 72 3b 20 20 20 2f 2a 20 53 69 7a 65 20 ader; /* Size
149f8 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 of the cell cont
149f9 65 6e 74 20 68 65 61 64 65 72 20 69 6e 20 62 79 ent header in by
149fa 74 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 4c 6f tes */. u16 nLo
149fb 63 61 6c 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e cal; /* Amoun
149fc 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c t of payload hel
149fd 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 75 d locally */. u
149fe 31 36 20 69 4f 76 65 72 66 6c 6f 77 3b 20 2f 2a 16 iOverflow; /*
149ff 20 4f 66 66 73 65 74 20 74 6f 20 6f 76 65 72 66 Offset to overf
14a00 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 2e low page number.
14a01 20 20 5a 65 72 6f 20 69 66 20 6e 6f 20 6f 76 65 Zero if no ove
14a02 72 66 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20 6e rflow */. u16 n
14a03 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a Size; /* Siz
14a04 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f e of the cell co
14a05 6e 74 65 6e 74 20 6f 6e 20 74 68 65 20 6d 61 69 ntent on the mai
14a06 6e 20 62 2d 74 72 65 65 20 70 61 67 65 20 2a 2f n b-tree page */
14a07 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d .};../*.** Maxim
14a08 75 6d 20 64 65 70 74 68 20 6f 66 20 61 6e 20 53 um depth of an S
14a09 51 4c 69 74 65 20 42 2d 54 72 65 65 20 73 74 72 QLite B-Tree str
14a0a 75 63 74 75 72 65 2e 20 41 6e 79 20 42 2d 54 72 ucture. Any B-Tr
14a0b 65 65 20 64 65 65 70 65 72 20 74 68 61 6e 0a 2a ee deeper than.*
14a0c 2a 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20 64 * this will be d
14a0d 65 63 6c 61 72 65 64 20 63 6f 72 72 75 70 74 2e eclared corrupt.
14a0e 20 54 68 69 73 20 76 61 6c 75 65 20 69 73 20 63 This value is c
14a0f 61 6c 63 75 6c 61 74 65 64 20 62 61 73 65 64 20 alculated based
14a10 6f 6e 20 61 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 on a.** maximum
14a11 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 6f 66 database size of
14a12 20 32 5e 33 31 20 70 61 67 65 73 20 61 20 6d 69 2^31 pages a mi
14a13 6e 69 6d 75 6d 20 66 61 6e 6f 75 74 20 6f 66 20 nimum fanout of
14a14 32 20 66 6f 72 20 61 0a 2a 2a 20 72 6f 6f 74 2d 2 for a.** root-
14a15 6e 6f 64 65 20 61 6e 64 20 33 20 66 6f 72 20 61 node and 3 for a
14a16 6c 6c 20 6f 74 68 65 72 20 69 6e 74 65 72 6e 61 ll other interna
14a17 6c 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 l nodes..**.** I
14a18 66 20 61 20 74 72 65 65 20 74 68 61 74 20 61 70 f a tree that ap
14a19 70 65 61 72 73 20 74 6f 20 62 65 20 74 61 6c 6c pears to be tall
14a1a 65 72 20 74 68 61 6e 20 74 68 69 73 20 69 73 20 er than this is
14a1b 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 69 74 20 encountered, it
14a1c 69 73 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 is.** assumed th
14a1d 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 at the database
14a1e 69 73 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a 23 is corrupt..*/.#
14a1f 64 65 66 69 6e 65 20 42 54 43 55 52 53 4f 52 5f define BTCURSOR_
14a20 4d 41 58 5f 44 45 50 54 48 20 32 30 0a 0a 2f 2a MAX_DEPTH 20../*
14a21 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 .** A cursor is
14a22 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 a pointer to a p
14a23 61 72 74 69 63 75 6c 61 72 20 65 6e 74 72 79 20 articular entry
14a24 77 69 74 68 69 6e 20 61 20 70 61 72 74 69 63 75 within a particu
14a25 6c 61 72 0a 2a 2a 20 62 2d 74 72 65 65 20 77 69 lar.** b-tree wi
14a26 74 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 thin a database
14a27 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 file..**.** The
14a28 65 6e 74 72 79 20 69 73 20 69 64 65 6e 74 69 66 entry is identif
14a29 69 65 64 20 62 79 20 69 74 73 20 4d 65 6d 50 61 ied by its MemPa
14a2a 67 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78 ge and the index
14a2b 20 69 6e 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 in.** MemPage.a
14a2c 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 65 6e Cell[] of the en
14a2d 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 try..**.** When
14a2e 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 a single databas
14a2f 65 20 66 69 6c 65 20 63 61 6e 20 73 68 61 72 65 e file can share
14a30 64 20 62 79 20 74 77 6f 20 6d 6f 72 65 20 64 61 d by two more da
14a31 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14a32 6e 73 2c 0a 2a 2a 20 62 75 74 20 63 75 72 73 6f ns,.** but curso
14a33 72 73 20 63 61 6e 6e 6f 74 20 62 65 20 73 68 61 rs cannot be sha
14a34 72 65 64 2e 20 20 45 61 63 68 20 63 75 72 73 6f red. Each curso
14a35 72 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 r is associated
14a36 77 69 74 68 20 61 0a 2a 2a 20 70 61 72 74 69 63 with a.** partic
14a37 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f ular database co
14a38 6e 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 66 nnection identif
14a39 69 65 64 20 42 74 43 75 72 73 6f 72 2e 70 42 74 ied BtCursor.pBt
14a3a 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 46 69 ree.db..**.** Fi
14a3b 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 elds in this str
14a3c 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73 ucture are acces
14a3d 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74 sed under the Bt
14a3e 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 Shared.mutex.**
14a3f 66 6f 75 6e 64 20 61 74 20 73 65 6c 66 2d 3e 70 found at self->p
14a40 42 74 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a 73 Bt->mutex. .*/.s
14a41 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 7b truct BtCursor {
14a42 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 . Btree *pBtree
14a43 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
14a44 54 68 65 20 42 74 72 65 65 20 74 6f 20 77 68 69 The Btree to whi
14a45 63 68 20 74 68 69 73 20 63 75 72 73 6f 72 20 62 ch this cursor b
14a46 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 53 68 elongs */. BtSh
14a47 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 ared *pBt;
14a48 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 53 /* The BtS
14a49 68 61 72 65 64 20 74 68 69 73 20 63 75 72 73 6f hared this curso
14a4a 72 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a 20 r points to */.
14a4b 20 42 74 43 75 72 73 6f 72 20 2a 70 4e 65 78 74 BtCursor *pNext
14a4c 2c 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46 6f , *pPrev; /* Fo
14a4d 72 6d 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 rms a linked lis
14a4e 74 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 t of all cursors
14a4f 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 */. struct Key
14a50 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 Info *pKeyInfo;
14a51 2f 2a 20 41 72 67 75 6d 65 6e 74 20 70 61 73 73 /* Argument pass
14a52 65 64 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e ed to comparison
14a53 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 50 function */. P
14a54 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20 gno pgnoRoot;
14a55 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
14a56 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69 root page of thi
14a57 73 20 74 72 65 65 20 2a 2f 0a 20 20 73 71 6c 69 s tree */. sqli
14a58 74 65 33 5f 69 6e 74 36 34 20 63 61 63 68 65 64 te3_int64 cached
14a59 52 6f 77 69 64 3b 20 2f 2a 20 4e 65 78 74 20 72 Rowid; /* Next r
14a5a 6f 77 69 64 20 63 61 63 68 65 2e 20 20 30 20 6d owid cache. 0 m
14a5b 65 61 6e 73 20 6e 6f 74 20 76 61 6c 69 64 20 2a eans not valid *
14a5c 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 /. CellInfo inf
14a5d 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a o; /*
14a5e 20 41 20 70 61 72 73 65 20 6f 66 20 74 68 65 20 A parse of the
14a5f 63 65 6c 6c 20 77 65 20 61 72 65 20 70 6f 69 6e cell we are poin
14a60 74 69 6e 67 20 61 74 20 2a 2f 0a 20 20 75 38 20 ting at */. u8
14a61 77 72 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 wrFlag;
14a62 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
14a63 66 20 77 72 69 74 61 62 6c 65 20 2a 2f 0a 20 20 f writable */.
14a64 75 38 20 61 74 4c 61 73 74 3b 20 20 20 20 20 20 u8 atLast;
14a65 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 /* Cur
14a66 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 sor pointing to
14a67 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 2a the last entry *
14a68 2f 0a 20 20 75 38 20 76 61 6c 69 64 4e 4b 65 79 /. u8 validNKey
14a69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
14a6a 20 54 72 75 65 20 69 66 20 69 6e 66 6f 2e 6e 4b True if info.nK
14a6b 65 79 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 ey is valid */.
14a6c 20 75 38 20 65 53 74 61 74 65 3b 20 20 20 20 20 u8 eState;
14a6d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e /* On
14a6e 65 20 6f 66 20 74 68 65 20 43 55 52 53 4f 52 5f e of the CURSOR_
14a6f 58 58 58 20 63 6f 6e 73 74 61 6e 74 73 20 28 73 XXX constants (s
14a70 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 20 76 ee below) */. v
14a71 6f 69 64 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 oid *pKey;
14a72 2f 2a 20 53 61 76 65 64 20 6b 65 79 20 74 68 61 /* Saved key tha
14a73 74 20 77 61 73 20 63 75 72 73 6f 72 27 73 20 6c t was cursor's l
14a74 61 73 74 20 6b 6e 6f 77 6e 20 70 6f 73 69 74 69 ast known positi
14a75 6f 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 on */. i64 nKey
14a76 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 ; /* Size
14a77 20 6f 66 20 70 4b 65 79 2c 20 6f 72 20 6c 61 73 of pKey, or las
14a78 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20 2a 2f t integer key */
14a79 0a 20 20 69 6e 74 20 73 6b 69 70 4e 65 78 74 3b . int skipNext;
14a7a 20 20 20 20 2f 2a 20 50 72 65 76 28 29 20 69 73 /* Prev() is
14a7b 20 6e 6f 6f 70 20 69 66 20 6e 65 67 61 74 69 76 noop if negativ
14a7c 65 2e 20 4e 65 78 74 28 29 20 69 73 20 6e 6f 6f e. Next() is noo
14a7d 70 20 69 66 20 70 6f 73 69 74 69 76 65 20 2a 2f p if positive */
14a7e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
14a7f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 OMIT_INCRBLOB.
14a80 75 38 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e u8 isIncrblobHan
14a81 64 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 dle; /* Tru
14a82 65 20 69 66 20 74 68 69 73 20 63 75 72 73 6f 72 e if this cursor
14a83 20 69 73 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20 is an incr. io
14a84 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f handle */. Pgno
14a85 20 2a 61 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 *aOverflow;
14a86 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 6f /* Cache o
14a87 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 f overflow page
14a88 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e locations */.#en
14a89 64 69 66 0a 20 20 69 31 36 20 69 50 61 67 65 3b dif. i16 iPage;
14a8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14a8b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
14a8c 6e 64 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 ndex of current
14a8d 70 61 67 65 20 69 6e 20 61 70 50 61 67 65 20 2a page in apPage *
14a8e 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 50 /. MemPage *apP
14a8f 61 67 65 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 age[BTCURSOR_MAX
14a90 5f 44 45 50 54 48 5d 3b 20 20 2f 2a 20 50 61 67 _DEPTH]; /* Pag
14a91 65 73 20 66 72 6f 6d 20 72 6f 6f 74 20 74 6f 20 es from root to
14a92 63 75 72 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a current page */.
14a93 20 20 75 31 36 20 61 69 49 64 78 5b 42 54 43 55 u16 aiIdx[BTCU
14a94 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b RSOR_MAX_DEPTH];
14a95 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 /* Curre
14a96 6e 74 20 69 6e 64 65 78 20 69 6e 20 61 70 50 61 nt index in apPa
14a97 67 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ge[i] */.};../*.
14a98 2a 2a 20 50 6f 74 65 6e 74 69 61 6c 20 76 61 6c ** Potential val
14a99 75 65 73 20 66 6f 72 20 42 74 43 75 72 73 6f 72 ues for BtCursor
14a9a 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 .eState..**.** C
14a9b 55 52 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a 2a 20 URSOR_VALID:.**
14a9c 20 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 Cursor points
14a9d 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 to a valid entry
14a9e 2e 20 67 65 74 50 61 79 6c 6f 61 64 28 29 20 65 . getPayload() e
14a9f 74 63 2e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 tc. may be calle
14aa0 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f d..**.** CURSOR_
14aa1 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75 INVALID:.** Cu
14aa2 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 70 6f rsor does not po
14aa3 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 65 int to a valid e
14aa4 6e 74 72 79 2e 20 54 68 69 73 20 63 61 6e 20 68 ntry. This can h
14aa5 61 70 70 65 6e 20 28 66 6f 72 20 65 78 61 6d 70 appen (for examp
14aa6 6c 65 29 20 0a 2a 2a 20 20 20 62 65 63 61 75 73 le) .** becaus
14aa7 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 e the table is e
14aa8 6d 70 74 79 20 6f 72 20 62 65 63 61 75 73 65 20 mpty or because
14aa9 42 74 72 65 65 43 75 72 73 6f 72 46 69 72 73 74 BtreeCursorFirst
14aaa 28 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a () has not been.
14aab 2a 2a 20 20 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a ** called..**.
14aac 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 ** CURSOR_REQUIR
14aad 45 53 45 45 4b 3a 0a 2a 2a 20 20 20 54 68 65 20 ESEEK:.** The
14aae 74 61 62 6c 65 20 74 68 61 74 20 74 68 69 73 20 table that this
14aaf 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 cursor was opene
14ab0 64 20 6f 6e 20 73 74 69 6c 6c 20 65 78 69 73 74 d on still exist
14ab1 73 2c 20 62 75 74 20 68 61 73 20 62 65 65 6e 20 s, but has been
14ab2 0a 2a 2a 20 20 20 6d 6f 64 69 66 69 65 64 20 73 .** modified s
14ab3 69 6e 63 65 20 74 68 65 20 63 75 72 73 6f 72 20 ince the cursor
14ab4 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e 20 54 was last used. T
14ab5 68 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 he cursor positi
14ab6 6f 6e 20 69 73 20 73 61 76 65 64 0a 2a 2a 20 20 on is saved.**
14ab7 20 69 6e 20 76 61 72 69 61 62 6c 65 73 20 42 74 in variables Bt
14ab8 43 75 72 73 6f 72 2e 70 4b 65 79 20 61 6e 64 20 Cursor.pKey and
14ab9 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 2e 20 57 BtCursor.nKey. W
14aba 68 65 6e 20 61 20 63 75 72 73 6f 72 20 69 73 20 hen a cursor is
14abb 69 6e 20 0a 2a 2a 20 20 20 74 68 69 73 20 73 74 in .** this st
14abc 61 74 65 2c 20 72 65 73 74 6f 72 65 43 75 72 73 ate, restoreCurs
14abd 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6e orPosition() can
14abe 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 74 be called to at
14abf 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 73 65 tempt to.** se
14ac0 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f ek the cursor to
14ac1 20 74 68 65 20 73 61 76 65 64 20 70 6f 73 69 74 the saved posit
14ac2 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f ion..**.** CURSO
14ac3 52 5f 46 41 55 4c 54 3a 0a 2a 2a 20 20 20 41 20 R_FAULT:.** A
14ac4 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 65 72 unrecoverable er
14ac5 72 6f 72 20 28 61 6e 20 49 2f 4f 20 65 72 72 6f ror (an I/O erro
14ac6 72 20 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 66 61 r or a malloc fa
14ac7 69 6c 75 72 65 29 20 68 61 73 20 6f 63 63 75 72 ilure) has occur
14ac8 72 65 64 0a 2a 2a 20 20 20 6f 6e 20 61 20 64 69 red.** on a di
14ac9 66 66 65 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69 fferent connecti
14aca 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73 20 74 on that shares t
14acb 68 65 20 42 74 53 68 61 72 65 64 20 63 61 63 68 he BtShared cach
14acc 65 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 20 e with this.**
14acd 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 65 72 cursor. The er
14ace 72 6f 72 20 68 61 73 20 6c 65 66 74 20 74 68 65 ror has left the
14acf 20 63 61 63 68 65 20 69 6e 20 61 6e 20 69 6e 63 cache in an inc
14ad0 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e onsistent state.
14ad1 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74 68 69 6e 67 .** Do nothing
14ad2 20 65 6c 73 65 20 77 69 74 68 20 74 68 69 73 20 else with this
14ad3 63 75 72 73 6f 72 2e 20 20 41 6e 79 20 61 74 74 cursor. Any att
14ad4 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 empt to use the
14ad5 63 75 72 73 6f 72 0a 2a 2a 20 20 20 73 68 6f 75 cursor.** shou
14ad6 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 ld return the er
14ad7 72 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 64 20 ror code stored
14ad8 69 6e 20 42 74 43 75 72 73 6f 72 2e 73 6b 69 70 in BtCursor.skip
14ad9 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 55 52 53 .*/.#define CURS
14ada 4f 52 5f 49 4e 56 41 4c 49 44 20 20 20 20 20 20 OR_INVALID
14adb 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 43 0.#define C
14adc 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 20 20 URSOR_VALID
14add 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 1.#defin
14ade 65 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 e CURSOR_REQUIRE
14adf 53 45 45 4b 20 20 20 20 20 20 20 32 0a 23 64 65 SEEK 2.#de
14ae0 66 69 6e 65 20 43 55 52 53 4f 52 5f 46 41 55 4c fine CURSOR_FAUL
14ae1 54 20 20 20 20 20 20 20 20 20 20 20 20 20 33 0a T 3.
14ae2 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 64 61 74 61 ./* .** The data
14ae3 62 61 73 65 20 70 61 67 65 20 74 68 65 20 50 45 base page the PE
14ae4 4e 44 49 4e 47 5f 42 59 54 45 20 6f 63 63 75 70 NDING_BYTE occup
14ae5 69 65 73 2e 20 54 68 69 73 20 70 61 67 65 20 69 ies. This page i
14ae6 73 20 6e 65 76 65 72 20 75 73 65 64 2e 0a 2a 2f s never used..*/
14ae7 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e .# define PENDIN
14ae8 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
14ae9 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 PAGER_MJ_PGNO(p
14aea 42 74 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 Bt)../*.** These
14aeb 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74 macros define t
14aec 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 he location of t
14aed 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 he pointer-map e
14aee 6e 74 72 79 20 66 6f 72 20 61 20 0a 2a 2a 20 64 ntry for a .** d
14aef 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 54 68 atabase page. Th
14af0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
14af1 20 74 6f 20 65 61 63 68 20 69 73 20 74 68 65 20 to each is the
14af2 6e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 number of usable
14af3 0a 2a 2a 20 62 79 74 65 73 20 6f 6e 20 65 61 63 .** bytes on eac
14af4 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 h page of the da
14af5 74 61 62 61 73 65 20 28 6f 66 74 65 6e 20 31 30 tabase (often 10
14af6 32 34 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 24). The second
14af7 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e is the.** page n
14af8 75 6d 62 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70 umber to look up
14af9 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 in the pointer
14afa 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 map..**.** PTRMA
14afb 50 5f 50 41 47 45 4e 4f 20 72 65 74 75 72 6e 73 P_PAGENO returns
14afc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61 the database pa
14afd 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
14afe 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 pointer-map.**
14aff 70 61 67 65 20 74 68 61 74 20 73 74 6f 72 65 73 page that stores
14b00 20 74 68 65 20 72 65 71 75 69 72 65 64 20 70 6f the required po
14b01 69 6e 74 65 72 2e 20 50 54 52 4d 41 50 5f 50 54 inter. PTRMAP_PT
14b02 52 4f 46 46 53 45 54 20 72 65 74 75 72 6e 73 0a ROFFSET returns.
14b03 2a 2a 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 ** the offset of
14b04 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6d the requested m
14b05 61 70 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 ap entry..**.**
14b06 49 66 20 74 68 65 20 70 67 6e 6f 20 61 72 67 75 If the pgno argu
14b07 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 50 ment passed to P
14b08 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 69 73 20 TRMAP_PAGENO is
14b09 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 a pointer-map pa
14b0a 67 65 2c 0a 2a 2a 20 74 68 65 6e 20 70 67 6e 6f ge,.** then pgno
14b0b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 53 6f is returned. So
14b0c 20 28 70 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f 50 (pgno==PTRMAP_P
14b0d 41 47 45 4e 4f 28 70 67 73 7a 2c 20 70 67 6e 6f AGENO(pgsz, pgno
14b0e 29 29 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65 )) can be.** use
14b0f 64 20 74 6f 20 74 65 73 74 20 69 66 20 70 67 6e d to test if pgn
14b10 6f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d o is a pointer-m
14b11 61 70 20 70 61 67 65 2e 20 50 54 52 4d 41 50 5f ap page. PTRMAP_
14b12 49 53 50 41 47 45 20 69 6d 70 6c 65 6d 65 6e 74 ISPAGE implement
14b13 73 0a 2a 2a 20 74 68 69 73 20 74 65 73 74 2e 0a s.** this test..
14b14 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 */.#define PTRMA
14b15 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 70 67 P_PAGENO(pBt, pg
14b16 6e 6f 29 20 70 74 72 6d 61 70 50 61 67 65 6e 6f no) ptrmapPageno
14b17 28 70 42 74 2c 20 70 67 6e 6f 29 0a 23 64 65 66 (pBt, pgno).#def
14b18 69 6e 65 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 ine PTRMAP_PTROF
14b19 46 53 45 54 28 70 67 70 74 72 6d 61 70 2c 20 70 FSET(pgptrmap, p
14b1a 67 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d 70 67 gno) (5*(pgno-pg
14b1b 70 74 72 6d 61 70 2d 31 29 29 0a 23 64 65 66 69 ptrmap-1)).#defi
14b1c 6e 65 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 ne PTRMAP_ISPAGE
14b1d 28 70 42 74 2c 20 70 67 6e 6f 29 20 28 50 54 52 (pBt, pgno) (PTR
14b1e 4d 41 50 5f 50 41 47 45 4e 4f 28 28 70 42 74 29 MAP_PAGENO((pBt)
14b1f 2c 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f 29 ,(pgno))==(pgno)
14b20 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 )../*.** The poi
14b21 6e 74 65 72 20 6d 61 70 20 69 73 20 61 20 6c 6f nter map is a lo
14b22 6f 6b 75 70 20 74 61 62 6c 65 20 74 68 61 74 20 okup table that
14b23 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 identifies the p
14b24 61 72 65 6e 74 20 70 61 67 65 20 66 6f 72 0a 2a arent page for.*
14b25 2a 20 65 61 63 68 20 63 68 69 6c 64 20 70 61 67 * each child pag
14b26 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
14b27 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 61 72 e file. The par
14b28 65 6e 74 20 70 61 67 65 20 69 73 20 74 68 65 20 ent page is the
14b29 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 63 6f 6e page that.** con
14b2a 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 tains a pointer
14b2b 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20 45 to the child. E
14b2c 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 very page in the
14b2d 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 database contai
14b2e 6e 73 0a 2a 2a 20 30 20 6f 72 20 31 20 70 61 72 ns.** 0 or 1 par
14b2f 65 6e 74 20 70 61 67 65 73 2e 20 20 28 49 6e 20 ent pages. (In
14b30 74 68 69 73 20 63 6f 6e 74 65 78 74 20 27 64 61 this context 'da
14b31 74 61 62 61 73 65 20 70 61 67 65 27 20 72 65 66 tabase page' ref
14b32 65 72 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 70 61 ers.** to any pa
14b33 67 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 ge that is not p
14b34 61 72 74 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 art of the point
14b35 65 72 20 6d 61 70 20 69 74 73 65 6c 66 2e 29 20 er map itself.)
14b36 20 45 61 63 68 20 70 6f 69 6e 74 65 72 20 6d 61 Each pointer ma
14b37 70 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 6e 73 69 p.** entry consi
14b38 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 sts of a single
14b39 62 79 74 65 20 27 74 79 70 65 27 20 61 6e 64 20 byte 'type' and
14b3a 61 20 34 20 62 79 74 65 20 70 61 72 65 6e 74 20 a 4 byte parent
14b3b 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 page number..**
14b3c 54 68 65 20 50 54 52 4d 41 50 5f 58 58 58 20 69 The PTRMAP_XXX i
14b3d 64 65 6e 74 69 66 69 65 72 73 20 62 65 6c 6f 77 dentifiers below
14b3e 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 74 are the valid t
14b3f 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ypes..**.** The
14b40 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 70 purpose of the p
14b41 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 74 6f ointer map is to
14b42 20 66 61 63 69 6c 69 74 79 20 6d 6f 76 69 6e 67 facility moving
14b43 20 70 61 67 65 73 20 66 72 6f 6d 20 6f 6e 65 0a pages from one.
14b44 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 ** position in t
14b45 68 65 20 66 69 6c 65 20 74 6f 20 61 6e 6f 74 68 he file to anoth
14b46 65 72 20 61 73 20 70 61 72 74 20 6f 66 20 61 75 er as part of au
14b47 74 6f 76 61 63 75 75 6d 2e 20 20 57 68 65 6e 20 tovacuum. When
14b48 61 20 70 61 67 65 0a 2a 2a 20 69 73 20 6d 6f 76 a page.** is mov
14b49 65 64 2c 20 74 68 65 20 70 6f 69 6e 74 65 72 20 ed, the pointer
14b4a 69 6e 20 69 74 73 20 70 61 72 65 6e 74 20 6d 75 in its parent mu
14b4b 73 74 20 62 65 20 75 70 64 61 74 65 64 20 74 6f st be updated to
14b4c 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a point to the.**
14b4d 20 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 new location.
14b4e 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 The pointer map
14b4f 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 is used to locat
14b50 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 e the parent pag
14b51 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a 2a e quickly..**.**
14b52 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 PTRMAP_ROOTPAGE
14b53 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 : The database p
14b54 61 67 65 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 age is a root-pa
14b55 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d ge. The page-num
14b56 62 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20 ber is not.**
14b57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 u
14b58 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 sed in this case
14b59 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 46 ..**.** PTRMAP_F
14b5a 52 45 45 50 41 47 45 3a 20 54 68 65 20 64 61 74 REEPAGE: The dat
14b5b 61 62 61 73 65 20 70 61 67 65 20 69 73 20 61 6e abase page is an
14b5c 20 75 6e 75 73 65 64 20 28 66 72 65 65 29 20 70 unused (free) p
14b5d 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 age. The page-nu
14b5e 6d 62 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 mber .**
14b5f 20 20 20 20 20 20 20 20 20 20 69 73 20 6e 6f 74 is not
14b60 20 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 used in this ca
14b61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 se..**.** PTRMAP
14b62 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 54 68 65 20 _OVERFLOW1: The
14b63 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 database page is
14b64 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 the first page
14b65 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 0a 2a 2a in a list of .**
14b66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14b67 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 overflow page
14b68 73 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 s. The page numb
14b69 65 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 er identifies th
14b6a 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 20 e page that.**
14b6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14b6c 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 contains the ce
14b6d 6c 6c 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 ll with a pointe
14b6e 72 20 74 6f 20 74 68 69 73 20 6f 76 65 72 66 6c r to this overfl
14b6f 6f 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 ow page..**.** P
14b70 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a TRMAP_OVERFLOW2:
14b71 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 The database pa
14b72 67 65 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 ge is the second
14b73 20 6f 72 20 6c 61 74 65 72 20 70 61 67 65 20 69 or later page i
14b74 6e 20 61 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 n a list of.**
14b75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14b76 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
14b77 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 The page-number
14b78 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 identifies the
14b79 70 72 65 76 69 6f 75 73 0a 2a 2a 20 20 20 20 20 previous.**
14b7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 pa
14b7b 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c ge in the overfl
14b7c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a ow page list..**
14b7d 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 .** PTRMAP_BTREE
14b7e 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 : The database p
14b7f 61 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f 6f age is a non-roo
14b80 74 20 62 74 72 65 65 20 70 61 67 65 2e 20 54 68 t btree page. Th
14b81 65 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a e page number.**
14b82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
14b83 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 dentifies the pa
14b84 72 65 6e 74 20 70 61 67 65 20 69 6e 20 74 68 65 rent page in the
14b85 20 62 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 btree..*/.#defi
14b86 6e 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 ne PTRMAP_ROOTPA
14b87 47 45 20 31 0a 23 64 65 66 69 6e 65 20 50 54 52 GE 1.#define PTR
14b88 4d 41 50 5f 46 52 45 45 50 41 47 45 20 32 0a 23 MAP_FREEPAGE 2.#
14b89 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56 define PTRMAP_OV
14b8a 45 52 46 4c 4f 57 31 20 33 0a 23 64 65 66 69 6e ERFLOW1 3.#defin
14b8b 65 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f e PTRMAP_OVERFLO
14b8c 57 32 20 34 0a 23 64 65 66 69 6e 65 20 50 54 52 W2 4.#define PTR
14b8d 4d 41 50 5f 42 54 52 45 45 20 35 0a 0a 2f 2a 20 MAP_BTREE 5../*
14b8e 41 20 62 75 6e 63 68 20 6f 66 20 61 73 73 65 72 A bunch of asser
14b8f 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 t() statements t
14b90 6f 20 63 68 65 63 6b 20 74 68 65 20 74 72 61 6e o check the tran
14b91 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 76 61 saction state va
14b92 72 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 68 61 riables.** of ha
14b93 6e 64 6c 65 20 70 20 28 74 79 70 65 20 42 74 72 ndle p (type Btr
14b94 65 65 2a 29 20 61 72 65 20 69 6e 74 65 72 6e 61 ee*) are interna
14b95 6c 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a lly consistent..
14b96 2a 2f 0a 23 64 65 66 69 6e 65 20 62 74 72 65 65 */.#define btree
14b97 49 6e 74 65 67 72 69 74 79 28 70 29 20 5c 0a 20 Integrity(p) \.
14b98 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d assert( p->pBt-
14b99 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d >inTransaction!=
14b9a 54 52 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d TRANS_NONE || p-
14b9b 3e 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 >pBt->nTransacti
14b9c 6f 6e 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 73 73 on==0 ); \. ass
14b9d 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 ert( p->pBt->inT
14b9e 72 61 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69 ransaction>=p->i
14b9f 6e 54 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f 2a 0a nTrans ); .../*.
14ba0 2a 2a 20 54 68 65 20 49 53 41 55 54 4f 56 41 43 ** The ISAUTOVAC
14ba1 55 55 4d 20 6d 61 63 72 6f 20 69 73 20 75 73 65 UUM macro is use
14ba2 64 20 77 69 74 68 69 6e 20 62 61 6c 61 6e 63 65 d within balance
14ba3 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64 65 _nonroot() to de
14ba4 74 65 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 74 68 termine.** if th
14ba5 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f e database suppo
14ba6 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 rts auto-vacuum
14ba7 6f 72 20 6e 6f 74 2e 20 42 65 63 61 75 73 65 20 or not. Because
14ba8 69 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 77 69 it is used.** wi
14ba9 74 68 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69 thin an expressi
14baa 6f 6e 20 74 68 61 74 20 69 73 20 61 6e 20 61 72 on that is an ar
14bab 67 75 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 68 65 gument to anothe
14bac 72 20 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 71 6c r macro .** (sql
14bad 69 74 65 4d 61 6c 6c 6f 63 52 61 77 29 2c 20 69 iteMallocRaw), i
14bae 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
14baf 65 20 74 6f 20 75 73 65 20 63 6f 6e 64 69 74 69 e to use conditi
14bb0 6f 6e 61 6c 20 63 6f 6d 70 69 6c 61 74 69 6f 6e onal compilation
14bb1 2e 0a 2a 2a 20 53 6f 2c 20 74 68 69 73 20 6d 61 ..** So, this ma
14bb2 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 20 69 cro is defined i
14bb3 6e 73 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 6e 64 nstead..*/.#ifnd
14bb4 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
14bb5 55 54 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 6e UTOVACUUM.#defin
14bb6 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 28 e ISAUTOVACUUM (
14bb7 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 pBt->autoVacuum)
14bb8 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 49 .#else.#define I
14bb9 53 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 SAUTOVACUUM 0.#e
14bba 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ndif.../*.** Thi
14bbb 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70 s structure is p
14bbc 61 73 73 65 64 20 61 72 6f 75 6e 64 20 74 68 72 assed around thr
14bbd 6f 75 67 68 20 61 6c 6c 20 74 68 65 20 73 61 6e ough all the san
14bbe 69 74 79 20 63 68 65 63 6b 69 6e 67 20 72 6f 75 ity checking rou
14bbf 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 64 65 tines.** in orde
14bc0 72 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20 r to keep track
14bc1 6f 66 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20 73 of some global s
14bc2 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e tate information
14bc3 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
14bc4 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 uct IntegrityCk
14bc5 49 6e 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 72 IntegrityCk;.str
14bc6 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 uct IntegrityCk
14bc7 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
14bc8 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 72 65 t; /* The tre
14bc9 65 20 62 65 69 6e 67 20 63 68 65 63 6b 65 64 20 e being checked
14bca 6f 75 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a out */. Pager *
14bcb 70 50 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 68 pPager; /* Th
14bcc 65 20 61 73 73 6f 63 69 61 74 65 64 20 70 61 67 e associated pag
14bcd 65 72 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 73 er. Also access
14bce 69 62 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 61 ible by pBt->pPa
14bcf 67 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 ger */. Pgno nP
14bd0 61 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 age; /* Nu
14bd1 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
14bd2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f the database */
14bd3 0a 20 20 69 6e 74 20 2a 61 6e 52 65 66 3b 20 20 . int *anRef;
14bd4 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
14bd5 66 20 74 69 6d 65 73 20 65 61 63 68 20 70 61 67 f times each pag
14bd6 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 20 e is referenced
14bd7 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 3b 20 */. int mxErr;
14bd8 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 70 20 61 /* Stop a
14bd9 63 63 75 6d 75 6c 61 74 69 6e 67 20 65 72 72 6f ccumulating erro
14bda 72 73 20 77 68 65 6e 20 74 68 69 73 20 72 65 61 rs when this rea
14bdb 63 68 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 69 ches zero */. i
14bdc 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 nt nErr;
14bdd 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 /* Number of me
14bde 73 73 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 ssages written t
14bdf 6f 20 7a 45 72 72 4d 73 67 20 73 6f 20 66 61 72 o zErrMsg so far
14be0 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 6c 6c 6f 63 */. int malloc
14be1 46 61 69 6c 65 64 3b 20 2f 2a 20 41 20 6d 65 6d Failed; /* A mem
14be2 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
14be3 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 rror has occurre
14be4 64 20 2a 2f 0a 20 20 53 74 72 41 63 63 75 6d 20 d */. StrAccum
14be5 65 72 72 4d 73 67 3b 20 20 2f 2a 20 41 63 63 75 errMsg; /* Accu
14be6 6d 75 6c 61 74 65 20 74 68 65 20 65 72 72 6f 72 mulate the error
14be7 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 68 65 message text he
14be8 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 re */.};../*.**
14be9 52 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 20 Read or write a
14bea 74 77 6f 2d 20 61 6e 64 20 66 6f 75 72 2d 62 79 two- and four-by
14beb 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e te big-endian in
14bec 74 65 67 65 72 20 76 61 6c 75 65 73 2e 0a 2a 2f teger values..*/
14bed 0a 23 64 65 66 69 6e 65 20 67 65 74 32 62 79 74 .#define get2byt
14bee 65 28 78 29 20 20 20 28 28 78 29 5b 30 5d 3c 3c e(x) ((x)[0]<<
14bef 38 20 7c 20 28 78 29 5b 31 5d 29 0a 23 64 65 66 8 | (x)[1]).#def
14bf0 69 6e 65 20 70 75 74 32 62 79 74 65 28 70 2c 76 ine put2byte(p,v
14bf1 29 20 28 28 70 29 5b 30 5d 20 3d 20 28 75 38 29 ) ((p)[0] = (u8)
14bf2 28 28 76 29 3e 3e 38 29 2c 20 28 70 29 5b 31 5d ((v)>>8), (p)[1]
14bf3 20 3d 20 28 75 38 29 28 76 29 29 0a 23 64 65 66 = (u8)(v)).#def
14bf4 69 6e 65 20 67 65 74 34 62 79 74 65 20 73 71 6c ine get4byte sql
14bf5 69 74 65 33 47 65 74 34 62 79 74 65 0a 23 64 65 ite3Get4byte.#de
14bf6 66 69 6e 65 20 70 75 74 34 62 79 74 65 20 73 71 fine put4byte sq
14bf7 6c 69 74 65 33 50 75 74 34 62 79 74 65 0a 0a 2f lite3Put4byte../
14bf8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
14bf9 6e 64 20 6f 66 20 62 74 72 65 65 49 6e 74 2e 68 nd of btreeInt.h
14bfa 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
14bfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14bfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
14bfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
14bfe 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
14bff 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 62 we left off in b
14c00 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a tmutex.c *******
14c01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 *************/.#
14c02 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
14c03 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
14c04 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
14c05 44 53 41 46 45 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 DSAFE../*.** Obt
14c06 61 69 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 ain the BtShared
14c07 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 mutex associate
14c08 64 20 77 69 74 68 20 42 2d 54 72 65 65 20 68 61 d with B-Tree ha
14c09 6e 64 6c 65 20 70 2e 20 41 6c 73 6f 2c 0a 2a 2a ndle p. Also,.**
14c0a 20 73 65 74 20 42 74 53 68 61 72 65 64 2e 64 62 set BtShared.db
14c0b 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
14c0c 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69 61 74 handle associat
14c0d 65 64 20 77 69 74 68 20 70 20 61 6e 64 20 74 68 ed with p and th
14c0e 65 0a 2a 2a 20 70 2d 3e 6c 6f 63 6b 65 64 20 62 e.** p->locked b
14c0f 6f 6f 6c 65 61 6e 20 74 6f 20 74 72 75 65 2e 0a oolean to true..
14c10 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c */.static void l
14c11 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 42 74 ockBtreeMutex(Bt
14c12 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 ree *p){. asser
14c13 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 t( p->locked==0
14c14 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
14c15 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 ite3_mutex_nothe
14c16 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 ld(p->pBt->mutex
14c17 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
14c18 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14c19 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 d(p->db->mutex)
14c1a 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 );.. sqlite3_mu
14c1b 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74 tex_enter(p->pBt
14c1c 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 70 ->mutex);. p->p
14c1d 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a Bt->db = p->db;.
14c1e 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b p->locked = 1;
14c1f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 .}../*.** Releas
14c20 65 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d e the BtShared m
14c21 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 utex associated
14c22 77 69 74 68 20 42 2d 54 72 65 65 20 68 61 6e 64 with B-Tree hand
14c23 6c 65 20 70 20 61 6e 64 0a 2a 2a 20 63 6c 65 61 le p and.** clea
14c24 72 20 74 68 65 20 70 2d 3e 6c 6f 63 6b 65 64 20 r the p->locked
14c25 62 6f 6f 6c 65 61 6e 2e 0a 2a 2f 0a 73 74 61 74 boolean..*/.stat
14c26 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 ic void unlockBt
14c27 72 65 65 4d 75 74 65 78 28 42 74 72 65 65 20 2a reeMutex(Btree *
14c28 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d p){. assert( p-
14c29 3e 6c 6f 63 6b 65 64 3d 3d 31 20 29 3b 0a 20 20 >locked==1 );.
14c2a 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
14c2b 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 mutex_held(p->pB
14c2c 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
14c2d 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
14c2e 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d utex_held(p->db-
14c2f 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
14c30 65 72 74 28 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70 ert( p->db==p->p
14c31 42 74 2d 3e 64 62 20 29 3b 0a 0a 20 20 73 71 6c Bt->db );.. sql
14c32 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
14c33 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b (p->pBt->mutex);
14c34 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30 . p->locked = 0
14c35 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 ;.}../*.** Enter
14c36 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 a mutex on the
14c37 67 69 76 65 6e 20 42 54 72 65 65 20 6f 62 6a 65 given BTree obje
14c38 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ct..**.** If the
14c39 20 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 73 object is not s
14c3a 68 61 72 61 62 6c 65 2c 20 74 68 65 6e 20 6e 6f harable, then no
14c3b 20 6d 75 74 65 78 20 69 73 20 65 76 65 72 20 72 mutex is ever r
14c3c 65 71 75 69 72 65 64 0a 2a 2a 20 61 6e 64 20 74 equired.** and t
14c3d 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
14c3e 20 6e 6f 2d 6f 70 2e 20 20 54 68 65 20 75 6e 64 no-op. The und
14c3f 65 72 6c 79 69 6e 67 20 6d 75 74 65 78 20 69 73 erlying mutex is
14c40 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 2e 0a non-recursive..
14c41 2a 2a 20 42 75 74 20 77 65 20 6b 65 65 70 20 61 ** But we keep a
14c42 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
14c43 20 69 6e 20 42 74 72 65 65 2e 77 61 6e 74 54 6f in Btree.wantTo
14c44 4c 6f 63 6b 20 73 6f 20 74 68 65 20 62 65 68 61 Lock so the beha
14c45 76 69 6f 72 0a 2a 2a 20 6f 66 20 74 68 69 73 20 vior.** of this
14c46 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 65 63 interface is rec
14c47 75 72 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 6f ursive..**.** To
14c48 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 73 avoid deadlocks
14c49 2c 20 6d 75 6c 74 69 70 6c 65 20 42 74 72 65 65 , multiple Btree
14c4a 73 20 61 72 65 20 6c 6f 63 6b 65 64 20 69 6e 20 s are locked in
14c4b 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 0a 2a the same order.*
14c4c 2a 20 62 79 20 61 6c 6c 20 64 61 74 61 62 61 73 * by all databas
14c4d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 e connections.
14c4e 54 68 65 20 70 2d 3e 70 4e 65 78 74 20 69 73 20 The p->pNext is
14c4f 61 20 6c 69 73 74 20 6f 66 20 6f 74 68 65 72 0a a list of other.
14c50 2a 2a 20 42 74 72 65 65 73 20 62 65 6c 6f 6e 67 ** Btrees belong
14c51 69 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 ing to the same
14c52 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
14c53 69 6f 6e 20 61 73 20 74 68 65 20 70 20 42 74 72 ion as the p Btr
14c54 65 65 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65 64 ee.** which need
14c55 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 61 66 to be locked af
14c56 74 65 72 20 70 2e 20 20 49 66 20 77 65 20 63 61 ter p. If we ca
14c57 6e 6e 6f 74 20 67 65 74 20 61 20 6c 6f 63 6b 20 nnot get a lock
14c58 6f 6e 0a 2a 2a 20 70 2c 20 74 68 65 6e 20 66 69 on.** p, then fi
14c59 72 73 74 20 75 6e 6c 6f 63 6b 20 61 6c 6c 20 6f rst unlock all o
14c5a 66 20 74 68 65 20 6f 74 68 65 72 73 20 6f 6e 20 f the others on
14c5b 70 2d 3e 70 4e 65 78 74 2c 20 74 68 65 6e 20 77 p->pNext, then w
14c5c 61 69 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6c ait.** for the l
14c5d 6f 63 6b 20 74 6f 20 62 65 63 6f 6d 65 20 61 76 ock to become av
14c5e 61 69 6c 61 62 6c 65 20 6f 6e 20 70 2c 20 74 68 ailable on p, th
14c5f 65 6e 20 72 65 6c 6f 63 6b 20 61 6c 6c 20 6f 66 en relock all of
14c60 20 74 68 65 0a 2a 2a 20 73 75 62 73 65 71 75 65 the.** subseque
14c61 6e 74 20 42 74 72 65 65 73 20 74 68 61 74 20 64 nt Btrees that d
14c62 65 73 69 72 65 20 61 20 6c 6f 63 6b 2e 0a 2a 2f esire a lock..*/
14c63 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14c64 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
14c65 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 eEnter(Btree *p)
14c66 7b 0a 20 20 42 74 72 65 65 20 2a 70 4c 61 74 65 {. Btree *pLate
14c67 72 3b 0a 0a 20 20 2f 2a 20 53 6f 6d 65 20 62 61 r;.. /* Some ba
14c68 73 69 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b sic sanity check
14c69 69 6e 67 20 6f 6e 20 74 68 65 20 42 74 72 65 65 ing on the Btree
14c6a 2e 20 20 54 68 65 20 6c 69 73 74 20 6f 66 20 42 . The list of B
14c6b 74 72 65 65 73 0a 20 20 2a 2a 20 63 6f 6e 6e 65 trees. ** conne
14c6c 63 74 65 64 20 62 79 20 70 4e 65 78 74 20 61 6e cted by pNext an
14c6d 64 20 70 50 72 65 76 20 73 68 6f 75 6c 64 20 62 d pPrev should b
14c6e 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 e in sorted orde
14c6f 72 20 62 79 0a 20 20 2a 2a 20 42 74 72 65 65 2e r by. ** Btree.
14c70 70 42 74 20 76 61 6c 75 65 2e 20 41 6c 6c 20 65 pBt value. All e
14c71 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c lements of the l
14c72 69 73 74 20 73 68 6f 75 6c 64 20 62 65 6c 6f 6e ist should belon
14c73 67 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 73 61 g to. ** the sa
14c74 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f me connection. O
14c75 6e 6c 79 20 73 68 61 72 65 64 20 42 74 72 65 65 nly shared Btree
14c76 73 20 61 72 65 20 6f 6e 20 74 68 65 20 6c 69 73 s are on the lis
14c77 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 t. */. assert(
14c78 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 p->pNext==0 || p
14c79 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 2d 3e ->pNext->pBt>p->
14c7a 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 pBt );. assert(
14c7b 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c 20 p->pPrev==0 ||
14c7c 70 2d 3e 70 50 72 65 76 2d 3e 70 42 74 3c 70 2d p->pPrev->pBt<p-
14c7d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 >pBt );. assert
14c7e 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c ( p->pNext==0 ||
14c7f 20 70 2d 3e 70 4e 65 78 74 2d 3e 64 62 3d 3d 70 p->pNext->db==p
14c80 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 ->db );. assert
14c81 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c ( p->pPrev==0 ||
14c82 20 70 2d 3e 70 50 72 65 76 2d 3e 64 62 3d 3d 70 p->pPrev->db==p
14c83 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 ->db );. assert
14c84 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c ( p->sharable ||
14c85 20 28 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 26 (p->pNext==0 &&
14c86 20 70 2d 3e 70 50 72 65 76 3d 3d 30 29 20 29 3b p->pPrev==0) );
14c87 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 .. /* Check for
14c88 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 73 69 73 74 locking consist
14c89 65 6e 63 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 ency */. assert
14c8a 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 ( !p->locked ||
14c8b 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 p->wantToLock>0
14c8c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
14c8d 73 68 61 72 61 62 6c 65 20 7c 7c 20 70 2d 3e 77 sharable || p->w
14c8e 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a antToLock==0 );.
14c8f 0a 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 . /* We should
14c90 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c already hold a l
14c91 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
14c92 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
14c93 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 /. assert( sqli
14c94 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
14c95 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
14c96 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20 74 68 65 . /* Unless the
14c97 20 64 61 74 61 62 61 73 65 20 69 73 20 73 68 61 database is sha
14c98 72 61 62 6c 65 20 61 6e 64 20 75 6e 6c 6f 63 6b rable and unlock
14c99 65 64 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65 ed, then BtShare
14c9a 64 2e 64 62 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 d.db. ** should
14c9b 20 61 6c 72 65 61 64 79 20 62 65 20 73 65 74 20 already be set
14c9c 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 correctly. */.
14c9d 61 73 73 65 72 74 28 20 28 70 2d 3e 6c 6f 63 6b assert( (p->lock
14c9e 65 64 3d 3d 30 20 26 26 20 70 2d 3e 73 68 61 72 ed==0 && p->shar
14c9f 61 62 6c 65 29 20 7c 7c 20 70 2d 3e 70 42 74 2d able) || p->pBt-
14ca0 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 0a 20 >db==p->db );..
14ca1 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c if( !p->sharabl
14ca2 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 2d e ) return;. p-
14ca3 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 >wantToLock++;.
14ca4 20 69 66 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 if( p->locked )
14ca5 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 return;.. /* I
14ca6 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 77 65 n most cases, we
14ca7 20 73 68 6f 75 6c 64 20 62 65 20 61 62 6c 65 20 should be able
14ca8 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 20 6c to acquire the l
14ca9 6f 63 6b 20 77 65 0a 20 20 2a 2a 20 77 61 6e 74 ock we. ** want
14caa 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 without having
14cab 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 74 20 74 to go throught t
14cac 68 65 20 61 73 63 65 6e 64 69 6e 67 20 6c 6f 63 he ascending loc
14cad 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 64 75 72 65 k. ** procedure
14cae 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 20 20 that follows.
14caf 4a 75 73 74 20 62 65 20 73 75 72 65 20 6e 6f 74 Just be sure not
14cb0 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a to block.. */.
14cb1 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6d 75 if( sqlite3_mu
14cb2 74 65 78 5f 74 72 79 28 70 2d 3e 70 42 74 2d 3e tex_try(p->pBt->
14cb3 6d 75 74 65 78 29 3d 3d 53 51 4c 49 54 45 5f 4f mutex)==SQLITE_O
14cb4 4b 20 29 7b 0a 20 20 20 20 70 2d 3e 70 42 74 2d K ){. p->pBt-
14cb5 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 >db = p->db;.
14cb6 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a p->locked = 1;.
14cb7 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
14cb8 0a 20 20 2f 2a 20 54 6f 20 61 76 6f 69 64 20 64 . /* To avoid d
14cb9 65 61 64 6c 6f 63 6b 2c 20 66 69 72 73 74 20 72 eadlock, first r
14cba 65 6c 65 61 73 65 20 61 6c 6c 20 6c 6f 63 6b 73 elease all locks
14cbb 20 77 69 74 68 20 61 20 6c 61 72 67 65 72 0a 20 with a larger.
14cbc 20 2a 2a 20 42 74 53 68 61 72 65 64 20 61 64 64 ** BtShared add
14cbd 72 65 73 73 2e 20 20 54 68 65 6e 20 61 63 71 75 ress. Then acqu
14cbe 69 72 65 20 6f 75 72 20 6c 6f 63 6b 2e 20 20 54 ire our lock. T
14cbf 68 65 6e 20 72 65 61 63 71 75 69 72 65 0a 20 20 hen reacquire.
14cc0 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 42 74 53 ** the other BtS
14cc1 68 61 72 65 64 20 6c 6f 63 6b 73 20 74 68 61 74 hared locks that
14cc2 20 77 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 we used to hold
14cc3 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 0a 20 20 in ascending.
14cc4 2a 2a 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 ** order.. */.
14cc5 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 for(pLater=p->p
14cc6 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c Next; pLater; pL
14cc7 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 ater=pLater->pNe
14cc8 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 xt){. assert(
14cc9 20 70 4c 61 74 65 72 2d 3e 73 68 61 72 61 62 6c pLater->sharabl
14cca 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 e );. assert(
14ccb 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 3d 3d pLater->pNext==
14ccc 30 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 70 4e 65 0 || pLater->pNe
14ccd 78 74 2d 3e 70 42 74 3e 70 4c 61 74 65 72 2d 3e xt->pBt>pLater->
14cce 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 pBt );. asser
14ccf 74 28 20 21 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b t( !pLater->lock
14cd0 65 64 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 77 61 ed || pLater->wa
14cd1 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 ntToLock>0 );.
14cd2 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 6c 6f if( pLater->lo
14cd3 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 75 6e cked ){. un
14cd4 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 lockBtreeMutex(p
14cd5 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 Later);. }.
14cd6 7d 0a 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 }. lockBtreeMut
14cd7 65 78 28 70 29 3b 0a 20 20 66 6f 72 28 70 4c 61 ex(p);. for(pLa
14cd8 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c ter=p->pNext; pL
14cd9 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 ater; pLater=pLa
14cda 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ter->pNext){.
14cdb 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 77 61 6e if( pLater->wan
14cdc 74 54 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 tToLock ){.
14cdd 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 lockBtreeMutex(
14cde 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20 pLater);. }.
14cdf 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 69 74 }.}../*.** Exit
14ce0 20 74 68 65 20 72 65 63 75 72 73 69 76 65 20 6d the recursive m
14ce1 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65 65 2e utex on a Btree.
14ce2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
14ce3 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
14ce4 74 72 65 65 4c 65 61 76 65 28 42 74 72 65 65 20 treeLeave(Btree
14ce5 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 73 68 *p){. if( p->sh
14ce6 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 61 73 arable ){. as
14ce7 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c sert( p->wantToL
14ce8 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e ock>0 );. p->
14ce9 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 wantToLock--;.
14cea 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c if( p->wantToL
14ceb 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ock==0 ){.
14cec 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 unlockBtreeMutex
14ced 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d (p);. }. }.}
14cee 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
14cef 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 ./*.** Return tr
14cf0 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61 72 ue if the BtShar
14cf1 65 64 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 ed mutex is held
14cf2 20 6f 6e 20 74 68 65 20 62 74 72 65 65 2c 20 6f on the btree, o
14cf3 72 20 69 66 20 74 68 65 0a 2a 2a 20 42 2d 54 72 r if the.** B-Tr
14cf4 65 65 20 69 73 20 6e 6f 74 20 6d 61 72 6b 65 64 ee is not marked
14cf5 20 61 73 20 73 68 61 72 61 62 6c 65 2e 0a 2a 2a as sharable..**
14cf6 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
14cf7 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 66 72 is used only fr
14cf8 6f 6d 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 om within assert
14cf9 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a () statements..*
14cfa 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
14cfb 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
14cfc 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65 eHoldsMutex(Btre
14cfd 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 e *p){. assert(
14cfe 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 p->sharable==0
14cff 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 || p->locked==0
14d00 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b || p->wantToLock
14d01 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 >0 );. assert(
14d02 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c p->sharable==0 |
14d03 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 7c | p->locked==0 |
14d04 7c 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70 42 74 2d | p->db==p->pBt-
14d05 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 >db );. assert(
14d06 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 p->sharable==0
14d07 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 || p->locked==0
14d08 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
14d09 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 _held(p->pBt->mu
14d0a 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
14d0b 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 ( p->sharable==0
14d0c 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 || p->locked==0
14d0d 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
14d0e 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
14d0f 74 65 78 29 20 29 3b 0a 0a 20 20 72 65 74 75 72 tex) );.. retur
14d10 6e 20 28 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d n (p->sharable==
14d11 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 29 3b 0 || p->locked);
14d12 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e .}.#endif...#ifn
14d13 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
14d14 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 45 INCRBLOB./*.** E
14d15 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 61 nter and leave a
14d16 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65 mutex on a Btre
14d17 65 20 67 69 76 65 6e 20 61 20 63 75 72 73 6f 72 e given a cursor
14d18 20 6f 77 6e 65 64 20 62 79 20 74 68 61 74 0a 2a owned by that.*
14d19 2a 20 42 74 72 65 65 2e 20 20 54 68 65 73 65 20 * Btree. These
14d1a 65 6e 74 72 79 20 70 6f 69 6e 74 73 20 61 72 65 entry points are
14d1b 20 75 73 65 64 20 62 79 20 69 6e 63 72 65 6d 65 used by increme
14d1c 6e 74 61 6c 20 49 2f 4f 20 61 6e 64 20 63 61 6e ntal I/O and can
14d1d 20 62 65 0a 2a 2a 20 6f 6d 69 74 74 65 64 20 69 be.** omitted i
14d1e 66 20 74 68 61 74 20 6d 6f 64 75 6c 65 20 69 73 f that module is
14d1f 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 not used..*/.SQ
14d20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
14d21 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e d sqlite3BtreeEn
14d22 74 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 terCursor(BtCurs
14d23 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c or *pCur){. sql
14d24 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
14d25 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a Cur->pBtree);.}.
14d26 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
14d27 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
14d28 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74 43 75 LeaveCursor(BtCu
14d29 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 rsor *pCur){. s
14d2a 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
14d2b 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a (pCur->pBtree);.
14d2c 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
14d2d 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 TE_OMIT_INCRBLOB
14d2e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 */.../*.** Ente
14d2f 72 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 65 r the mutex on e
14d30 76 65 72 79 20 42 74 72 65 65 20 61 73 73 6f 63 very Btree assoc
14d31 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74 iated with a dat
14d32 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 abase.** connect
14d33 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65 ion. This is ne
14d34 65 64 65 64 20 28 66 6f 72 20 65 78 61 6d 70 6c eded (for exampl
14d35 65 29 20 70 72 69 6f 72 20 74 6f 20 70 61 72 73 e) prior to pars
14d36 69 6e 67 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65 ing.** a stateme
14d37 6e 74 20 73 69 6e 63 65 20 77 65 20 77 69 6c 6c nt since we will
14d38 20 62 65 20 63 6f 6d 70 61 72 69 6e 67 20 74 61 be comparing ta
14d39 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e ble and column n
14d3a 61 6d 65 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20 ames.** against
14d3b 61 6c 6c 20 73 63 68 65 6d 61 73 20 61 6e 64 20 all schemas and
14d3c 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 we do not want t
14d3d 68 6f 73 65 20 73 63 68 65 6d 61 73 20 62 65 69 hose schemas bei
14d3e 6e 67 0a 2a 2a 20 72 65 73 65 74 20 6f 75 74 20 ng.** reset out
14d3f 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 2a from under us..*
14d40 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 *.** There is a
14d41 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6c 65 corresponding le
14d42 61 76 65 2d 61 6c 6c 20 70 72 6f 63 65 64 75 72 ave-all procedur
14d43 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 6e 74 65 72 20 es..**.** Enter
14d44 74 68 65 20 6d 75 74 65 78 65 73 20 69 6e 20 61 the mutexes in a
14d45 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 ccending order b
14d46 79 20 42 74 53 68 61 72 65 64 20 70 6f 69 6e 74 y BtShared point
14d47 65 72 20 61 64 64 72 65 73 73 0a 2a 2a 20 74 6f er address.** to
14d48 20 61 76 6f 69 64 20 74 68 65 20 70 6f 73 73 69 avoid the possi
14d49 62 69 6c 69 74 79 20 6f 66 20 64 65 61 64 6c 6f bility of deadlo
14d4a 63 6b 20 77 68 65 6e 20 74 77 6f 20 74 68 72 65 ck when two thre
14d4b 61 64 73 20 77 69 74 68 0a 2a 2a 20 74 77 6f 20 ads with.** two
14d4c 6f 72 20 6d 6f 72 65 20 62 74 72 65 65 73 20 69 or more btrees i
14d4d 6e 20 63 6f 6d 6d 6f 6e 20 62 6f 74 68 20 74 72 n common both tr
14d4e 79 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 74 68 y to lock all th
14d4f 65 69 72 20 62 74 72 65 65 73 0a 2a 2a 20 61 74 eir btrees.** at
14d50 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 61 6e the same instan
14d51 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
14d52 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
14d53 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 73 3BtreeEnterAll(s
14d54 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 qlite3 *db){. i
14d55 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70 nt i;. Btree *p
14d56 2c 20 2a 70 4c 61 74 65 72 3b 0a 20 20 61 73 73 , *pLater;. ass
14d57 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
14d58 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 ex_held(db->mute
14d59 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b x) );. for(i=0;
14d5a 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 i<db->nDb; i++)
14d5b 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 {. p = db->aD
14d5c 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 61 73 b[i].pBt;. as
14d5d 73 65 72 74 28 20 21 70 20 7c 7c 20 28 70 2d 3e sert( !p || (p->
14d5e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20 70 2d 3e locked==0 && p->
14d5f 73 68 61 72 61 62 6c 65 29 20 7c 7c 20 70 2d 3e sharable) || p->
14d60 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 pBt->db==p->db )
14d61 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70 ;. if( p && p
14d62 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 ->sharable ){.
14d63 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 p->wantToLoc
14d64 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 21 k++;. if( !
14d65 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 p->locked ){.
14d66 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
14d67 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 31 20 29 3b wantToLock==1 );
14d68 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 . while(
14d69 70 2d 3e 70 50 72 65 76 20 29 20 70 20 3d 20 70 p->pPrev ) p = p
14d6a 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 20 ->pPrev;.
14d6b 20 2f 2a 20 52 65 61 73 6f 6e 20 66 6f 72 20 41 /* Reason for A
14d6c 4c 57 41 59 53 3a 20 20 54 68 65 72 65 20 6d 75 LWAYS: There mu
14d6d 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f st be at least o
14d6e 6e 20 75 6e 6c 6f 63 6b 65 64 20 42 74 72 65 65 n unlocked Btree
14d6f 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 in. ** t
14d70 68 65 20 63 68 61 69 6e 2e 20 20 4f 74 68 65 72 he chain. Other
14d71 77 69 73 65 20 74 68 65 20 21 70 2d 3e 6c 6f 63 wise the !p->loc
14d72 6b 65 64 20 74 65 73 74 20 61 62 6f 76 65 20 77 ked test above w
14d73 6f 75 6c 64 20 68 61 76 65 20 66 61 69 6c 65 64 ould have failed
14d74 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c */. whil
14d75 65 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 26 26 20 e( p->locked &&
14d76 41 4c 57 41 59 53 28 70 2d 3e 70 4e 65 78 74 29 ALWAYS(p->pNext)
14d77 20 29 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b ) p = p->pNext;
14d78 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 4c 61 . for(pLa
14d79 74 65 72 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 20 ter = p->pNext;
14d7a 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 pLater; pLater=p
14d7b 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 Later->pNext){.
14d7c 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4c 61 if( pLa
14d7d 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 ter->locked ){.
14d7e 20 20 20 20 20 20 20 20 20 20 20 75 6e 6c 6f 63 unloc
14d7f 6b 42 74 72 65 65 4d 75 74 65 78 28 70 4c 61 74 kBtreeMutex(pLat
14d80 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d er);. }
14d81 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
14d82 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 while( p ){.
14d83 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 lockBtr
14d84 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 eeMutex(p);.
14d85 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 p = p->pNe
14d86 78 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 xt;. }.
14d87 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
14d88 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
14d89 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
14d8a 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 eeLeaveAll(sqlit
14d8b 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 e3 *db){. int i
14d8c 3b 0a 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 ;. Btree *p;.
14d8d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
14d8e 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d mutex_held(db->m
14d8f 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 utex) );. for(i
14d90 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 =0; i<db->nDb; i
14d91 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d ++){. p = db-
14d92 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 >aDb[i].pBt;.
14d93 20 69 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61 if( p && p->sha
14d94 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 61 rable ){. a
14d95 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f ssert( p->wantTo
14d96 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 20 20 Lock>0 );.
14d97 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b p->wantToLock--;
14d98 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 77 61 . if( p->wa
14d99 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 ntToLock==0 ){.
14d9a 20 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 unlockBtr
14d9b 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 eeMutex(p);.
14d9c 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a }. }. }.}.
14d9d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
14d9e 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
14d9f 65 20 69 66 20 74 68 65 20 63 75 72 72 65 6e 74 e if the current
14da0 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 thread holds th
14da1 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
14da2 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78 20 61 ction.** mutex a
14da3 6e 64 20 61 6c 6c 20 72 65 71 75 69 72 65 64 20 nd all required
14da4 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 65 73 BtShared mutexes
14da5 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
14da6 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 tine is used ins
14da7 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 ide assert() sta
14da8 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a 2a 2f tements only..*/
14da9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14daa 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
14dab 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 HoldsAllMutexes(
14dac 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
14dad 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71 int i;. if( !sq
14dae 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
14daf 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20 (db->mutex) ){.
14db0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
14db1 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 . for(i=0; i<db
14db2 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nDb; i++){.
14db3 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 20 20 70 Btree *p;. p
14db4 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
14db5 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 t;. if( p &&
14db6 70 2d 3e 73 68 61 72 61 62 6c 65 20 26 26 0a 20 p->sharable &&.
14db7 20 20 20 20 20 20 20 20 28 70 2d 3e 77 61 6e 74 (p->want
14db8 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21 73 71 ToLock==0 || !sq
14db9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
14dba 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29 (p->pBt->mutex))
14dbb 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
14dbc 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0;. }. }.
14dbd 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 return 1;.}.#end
14dbe 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a if /* NDEBUG */.
14dbf 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 ./*.** Add a new
14dc0 20 42 74 72 65 65 20 70 6f 69 6e 74 65 72 20 74 Btree pointer t
14dc1 6f 20 61 20 42 74 72 65 65 4d 75 74 65 78 41 72 o a BtreeMutexAr
14dc2 72 61 79 2e 20 0a 2a 2a 20 69 66 20 74 68 65 20 ray. .** if the
14dc3 70 6f 69 6e 74 65 72 20 63 61 6e 20 70 6f 73 73 pointer can poss
14dc4 69 62 6c 79 20 62 65 20 73 68 61 72 65 64 20 77 ibly be shared w
14dc5 69 74 68 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 64 ith.** another d
14dc6 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
14dc7 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f on..**.** The po
14dc8 69 6e 74 65 72 73 20 61 72 65 20 6b 65 70 74 20 inters are kept
14dc9 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20 in sorted order
14dca 62 79 20 70 42 74 72 65 65 2d 3e 70 42 74 2e 20 by pBtree->pBt.
14dcb 20 54 68 61 74 0a 2a 2a 20 77 61 79 20 77 68 65 That.** way whe
14dcc 6e 20 77 65 20 67 6f 20 74 6f 20 65 6e 74 65 72 n we go to enter
14dcd 20 61 6c 6c 20 74 68 65 20 6d 75 74 65 78 65 73 all the mutexes
14dce 2c 20 77 65 20 63 61 6e 20 65 6e 74 65 72 20 74 , we can enter t
14dcf 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 hem.** in order
14dd0 77 69 74 68 6f 75 74 20 65 76 65 72 79 20 68 61 without every ha
14dd1 76 69 6e 67 20 74 6f 20 62 61 63 6b 75 70 20 61 ving to backup a
14dd2 6e 64 20 72 65 74 72 79 20 61 6e 64 20 77 69 74 nd retry and wit
14dd3 68 6f 75 74 0a 2a 2a 20 77 6f 72 72 79 69 6e 67 hout.** worrying
14dd4 20 61 62 6f 75 74 20 64 65 61 64 6c 6f 63 6b 2e about deadlock.
14dd5 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 .**.** The numbe
14dd6 72 20 6f 66 20 73 68 61 72 65 64 20 62 74 72 65 r of shared btre
14dd7 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 es will always b
14dd8 65 20 73 6d 61 6c 6c 20 28 75 73 75 61 6c 6c 79 e small (usually
14dd9 20 30 20 6f 72 20 31 29 0a 2a 2a 20 73 6f 20 61 0 or 1).** so a
14dda 6e 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74 n insertion sort
14ddb 20 69 73 20 61 6e 20 61 64 65 71 75 61 74 65 20 is an adequate
14ddc 61 6c 67 6f 72 69 74 68 6d 20 68 65 72 65 2e 0a algorithm here..
14ddd 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14dde 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
14ddf 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 reeMutexArrayIns
14de0 65 72 74 28 42 74 72 65 65 4d 75 74 65 78 41 72 ert(BtreeMutexAr
14de1 72 61 79 20 2a 70 41 72 72 61 79 2c 20 42 74 72 ray *pArray, Btr
14de2 65 65 20 2a 70 42 74 72 65 65 29 7b 0a 20 20 69 ee *pBtree){. i
14de3 6e 74 20 69 2c 20 6a 3b 0a 20 20 42 74 53 68 61 nt i, j;. BtSha
14de4 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 66 28 20 red *pBt;. if(
14de5 70 42 74 72 65 65 3d 3d 30 20 7c 7c 20 70 42 74 pBtree==0 || pBt
14de6 72 65 65 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 ree->sharable==0
14de7 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 6e 64 ) return;.#ifnd
14de8 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a 20 20 ef NDEBUG. {.
14de9 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 for(i=0; i<pAr
14dea 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b ray->nMutex; i++
14deb 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
14dec 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b pArray->aBtree[
14ded 69 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a 20 20 i]!=pBtree );.
14dee 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
14def 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d assert( pArray-
14df0 3e 6e 4d 75 74 65 78 3e 3d 30 20 29 3b 0a 20 20 >nMutex>=0 );.
14df1 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e assert( pArray->
14df2 6e 4d 75 74 65 78 3c 41 72 72 61 79 53 69 7a 65 nMutex<ArraySize
14df3 28 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 29 (pArray->aBtree)
14df4 2d 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 42 -1 );. pBt = pB
14df5 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66 6f 72 tree->pBt;. for
14df6 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e (i=0; i<pArray->
14df7 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 nMutex; i++){.
14df8 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 assert( pArray
14df9 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 ->aBtree[i]!=pBt
14dfa 72 65 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 ree );. if( p
14dfb 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d Array->aBtree[i]
14dfc 2d 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20 20 20 ->pBt>pBt ){.
14dfd 20 20 20 66 6f 72 28 6a 3d 70 41 72 72 61 79 2d for(j=pArray-
14dfe 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d >nMutex; j>i; j-
14dff 2d 29 7b 0a 20 20 20 20 20 20 20 20 70 41 72 72 -){. pArr
14e00 61 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 3d 20 ay->aBtree[j] =
14e01 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a pArray->aBtree[j
14e02 2d 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 -1];. }.
14e03 20 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 pArray->aBtre
14e04 65 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b 0a 20 e[i] = pBtree;.
14e05 20 20 20 20 20 70 41 72 72 61 79 2d 3e 6e 4d 75 pArray->nMu
14e06 74 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 tex++;. ret
14e07 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 urn;. }. }.
14e08 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b pArray->aBtree[
14e09 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b pArray->nMutex++
14e0a 5d 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f ] = pBtree;.}../
14e0b 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d *.** Enter the m
14e0c 75 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 utex of every bt
14e0d 72 65 65 20 69 6e 20 74 68 65 20 61 72 72 61 79 ree in the array
14e0e 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
14e0f 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20 is.** called at
14e10 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
14e11 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 sqlite3VdbeExec
14e12 28 29 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 (). The mutexes
14e13 20 61 72 65 0a 2a 2a 20 65 78 69 74 65 64 20 61 are.** exited a
14e14 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
14e15 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 2e 0a same function..
14e16 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14e17 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
14e18 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 reeMutexArrayEnt
14e19 65 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 er(BtreeMutexArr
14e1a 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 ay *pArray){. i
14e1b 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b nt i;. for(i=0;
14e1c 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 i<pArray->nMute
14e1d 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 x; i++){. Btr
14e1e 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e ee *p = pArray->
14e1f 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f aBtree[i];. /
14e20 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e * Some basic san
14e21 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a ity checking */.
14e22 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 assert( i==0
14e23 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 || pArray->aBtr
14e24 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e ee[i-1]->pBt<p->
14e25 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 pBt );. asser
14e26 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c t( !p->locked ||
14e27 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 p->wantToLock>0
14e28 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73 );.. /* We s
14e29 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f hould already ho
14e2a 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 ld a lock on the
14e2b 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
14e2c 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 tion */. asse
14e2d 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
14e2e 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
14e2f 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 tex) );.. /*
14e30 54 68 65 20 42 74 72 65 65 20 69 73 20 73 68 61 The Btree is sha
14e31 72 61 62 6c 65 20 62 65 63 61 75 73 65 20 6f 6e rable because on
14e32 6c 79 20 73 68 61 72 61 62 6c 65 20 42 74 72 65 ly sharable Btre
14e33 65 73 20 61 72 65 20 65 6e 74 65 72 65 64 0a 20 es are entered.
14e34 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 61 ** into the a
14e35 72 72 61 79 20 69 6e 20 74 68 65 20 66 69 72 73 rray in the firs
14e36 74 20 70 6c 61 63 65 2e 20 2a 2f 0a 20 20 20 20 t place. */.
14e37 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 assert( p->shara
14e38 62 6c 65 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 ble );.. p->w
14e39 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 antToLock++;.
14e3a 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 if( !p->locked
14e3b 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 ){. lockBtr
14e3c 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 eeMutex(p);.
14e3d 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c }. }.}../*.** L
14e3e 65 61 76 65 20 74 68 65 20 6d 75 74 65 78 20 6f eave the mutex o
14e3f 66 20 65 76 65 72 79 20 62 74 72 65 65 20 69 6e f every btree in
14e40 20 74 68 65 20 67 72 6f 75 70 2e 0a 2a 2f 0a 53 the group..*/.S
14e41 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
14e42 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d id sqlite3BtreeM
14e43 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 42 utexArrayLeave(B
14e44 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 2a treeMutexArray *
14e45 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74 20 69 pArray){. int i
14e46 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 ;. for(i=0; i<p
14e47 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 Array->nMutex; i
14e48 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a ++){. Btree *
14e49 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72 p = pArray->aBtr
14e4a 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20 53 6f ee[i];. /* So
14e4b 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 79 20 me basic sanity
14e4c 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 checking */.
14e4d 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c 7c 20 assert( i==0 ||
14e4e 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 pArray->aBtree[i
14e4f 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 -1]->pBt<p->pBt
14e50 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
14e51 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a 20 20 20 20 ->locked );.
14e52 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 assert( p->wantT
14e53 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20 oLock>0 );..
14e54 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 /* We should alr
14e55 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b eady hold a lock
14e56 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
14e57 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
14e58 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
14e59 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
14e5a 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a >db->mutex) );..
14e5b 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 p->wantToLoc
14e5c 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e k--;. if( p->
14e5d 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b wantToLock==0 ){
14e5e 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 . unlockBtr
14e5f 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 eeMutex(p);.
14e60 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 53 }. }.}..#else.S
14e61 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
14e62 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 id sqlite3BtreeE
14e63 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a nter(Btree *p){.
14e64 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 p->pBt->db = p
14e65 2d 3e 64 62 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 ->db;.}.SQLITE_P
14e66 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
14e67 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c te3BtreeEnterAll
14e68 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
14e69 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
14e6a 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 0; i<db->nDb; i+
14e6b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 +){. Btree *p
14e6c 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
14e6d 74 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a t;. if( p ){.
14e6e 20 20 20 20 20 20 70 2d 3e 70 42 74 2d 3e 64 62 p->pBt->db
14e6f 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 7d 0a = p->db;. }.
14e70 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 }.}.#endif /*
14e71 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
14e72 53 41 46 45 20 2a 2f 0a 23 65 6e 64 69 66 20 2f SAFE */.#endif /
14e73 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f * ifndef SQLITE_
14e74 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
14e75 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a E */../*********
14e76 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 6d ***** End of btm
14e77 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a utex.c *********
14e78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14e79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14e7a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
14e7b 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
14e7c 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a btree.c *******
14e7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14e7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14e7f 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 ****/./*.** 2004
14e80 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 April 6.**.** T
14e81 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
14e82 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
14e83 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
14e84 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
14e85 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
14e86 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
14e87 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
14e88 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
14e89 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
14e8a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
14e8b 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
14e8c 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
14e8d 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
14e8e 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
14e8f 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
14e90 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
14e91 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
14e92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14e93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14e94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14e95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14e96 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 *******.** $Id:
14e97 62 74 72 65 65 2e 63 2c 76 20 31 2e 37 30 35 20 btree.c,v 1.705
14e98 32 30 30 39 2f 30 38 2f 31 30 20 30 33 3a 35 37 2009/08/10 03:57
14e99 3a 35 38 20 73 68 61 6e 65 20 45 78 70 20 24 0a :58 shane Exp $.
14e9a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
14e9b 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 74 implements a ext
14e9c 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65 ernal (disk-base
14e9d 64 29 20 64 61 74 61 62 61 73 65 20 75 73 69 6e d) database usin
14e9e 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20 53 65 65 g BTrees..** See
14e9f 20 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d the header comm
14ea0 65 6e 74 20 6f 6e 20 22 62 74 72 65 65 49 6e 74 ent on "btreeInt
14ea1 2e 68 22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e .h" for addition
14ea2 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a al information..
14ea3 2a 2a 20 49 6e 63 6c 75 64 69 6e 67 20 61 20 64 ** Including a d
14ea4 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 66 69 escription of fi
14ea5 6c 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 61 6e le format and an
14ea6 20 6f 76 65 72 76 69 65 77 20 6f 66 20 6f 70 65 overview of ope
14ea7 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a ration..*/../*.*
14ea8 2a 20 54 68 65 20 68 65 61 64 65 72 20 73 74 72 * The header str
14ea9 69 6e 67 20 74 68 61 74 20 61 70 70 65 61 72 73 ing that appears
14eaa 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e at the beginnin
14eab 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51 g of every.** SQ
14eac 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e 0a 2a Lite database..*
14ead 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 /.static const c
14eae 68 61 72 20 7a 4d 61 67 69 63 48 65 61 64 65 72 har zMagicHeader
14eaf 5b 5d 20 3d 20 53 51 4c 49 54 45 5f 46 49 4c 45 [] = SQLITE_FILE
14eb0 5f 48 45 41 44 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20 _HEADER;../*.**
14eb1 53 65 74 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 Set this global
14eb2 76 61 72 69 61 62 6c 65 20 74 6f 20 31 20 74 6f variable to 1 to
14eb3 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 enable tracing
14eb4 75 73 69 6e 67 20 74 68 65 20 54 52 41 43 45 0a using the TRACE.
14eb5 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 ** macro..*/.#if
14eb6 20 30 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 0.int sqlite3Bt
14eb7 72 65 65 54 72 61 63 65 3d 31 3b 20 20 2f 2a 20 reeTrace=1; /*
14eb8 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 74 True to enable t
14eb9 72 61 63 69 6e 67 20 2a 2f 0a 23 20 64 65 66 69 racing */.# defi
14eba 6e 65 20 54 52 41 43 45 28 58 29 20 20 69 66 28 ne TRACE(X) if(
14ebb 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 61 63 sqlite3BtreeTrac
14ebc 65 29 7b 70 72 69 6e 74 66 20 58 3b 66 66 6c 75 e){printf X;fflu
14ebd 73 68 28 73 74 64 6f 75 74 29 3b 7d 0a 23 65 6c sh(stdout);}.#el
14ebe 73 65 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43 se.# define TRAC
14ebf 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a 23 E(X).#endif....#
14ec0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
14ec1 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
14ec2 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 /*.** A list of
14ec3 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 73 BtShared objects
14ec4 20 74 68 61 74 20 61 72 65 20 65 6c 69 67 69 62 that are eligib
14ec5 6c 65 20 66 6f 72 20 70 61 72 74 69 63 69 70 61 le for participa
14ec6 74 69 6f 6e 0a 2a 2a 20 69 6e 20 73 68 61 72 65 tion.** in share
14ec7 64 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 76 d cache. This v
14ec8 61 72 69 61 62 6c 65 20 68 61 73 20 66 69 6c 65 ariable has file
14ec9 20 73 63 6f 70 65 20 64 75 72 69 6e 67 20 6e 6f scope during no
14eca 72 6d 61 6c 20 62 75 69 6c 64 73 2c 0a 2a 2a 20 rmal builds,.**
14ecb 62 75 74 20 74 68 65 20 74 65 73 74 20 68 61 72 but the test har
14ecc 6e 65 73 73 20 6e 65 65 64 73 20 74 6f 20 61 63 ness needs to ac
14ecd 63 65 73 73 20 69 74 20 73 6f 20 77 65 20 6d 61 cess it so we ma
14ece 6b 65 20 69 74 20 67 6c 6f 62 61 6c 20 66 6f 72 ke it global for
14ecf 20 0a 2a 2a 20 74 65 73 74 20 62 75 69 6c 64 73 .** test builds
14ed0 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 ..**.** Access t
14ed1 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 o this variable
14ed2 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 is protected by
14ed3 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
14ed4 54 49 43 5f 4d 41 53 54 45 52 2e 0a 2a 2f 0a 23 TIC_MASTER..*/.#
14ed5 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
14ed6 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
14ed7 20 42 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54 BtShared *SQLIT
14ed8 45 5f 57 53 44 20 73 71 6c 69 74 65 33 53 68 61 E_WSD sqlite3Sha
14ed9 72 65 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30 redCacheList = 0
14eda 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 42 ;.#else.static B
14edb 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45 5f tShared *SQLITE_
14edc 57 53 44 20 73 71 6c 69 74 65 33 53 68 61 72 65 WSD sqlite3Share
14edd 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a dCacheList = 0;.
14ede 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a #endif.#endif /*
14edf 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
14ee0 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 RED_CACHE */..#i
14ee1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
14ee2 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f T_SHARED_CACHE./
14ee3 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 *.** Enable or d
14ee4 69 73 61 62 6c 65 20 74 68 65 20 73 68 61 72 65 isable the share
14ee5 64 20 70 61 67 65 72 20 61 6e 64 20 73 63 68 65 d pager and sche
14ee6 6d 61 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a ma features..**.
14ee7 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
14ee8 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e has no effect on
14ee9 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 existing databa
14eea 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a se connections..
14eeb 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20 63 61 ** The shared ca
14eec 63 68 65 20 73 65 74 74 69 6e 67 20 65 66 66 65 che setting effe
14eed 63 74 73 20 6f 6e 6c 79 20 66 75 74 75 72 65 20 cts only future
14eee 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 calls to.** sqli
14eef 74 65 33 5f 6f 70 65 6e 28 29 2c 20 73 71 6c 69 te3_open(), sqli
14ef0 74 65 33 5f 6f 70 65 6e 31 36 28 29 2c 20 6f 72 te3_open16(), or
14ef1 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 sqlite3_open_v2
14ef2 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ()..*/.SQLITE_AP
14ef3 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e I int sqlite3_en
14ef4 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 able_shared_cach
14ef5 65 28 69 6e 74 20 65 6e 61 62 6c 65 29 7b 0a 20 e(int enable){.
14ef6 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
14ef7 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63 68 65 nfig.sharedCache
14ef8 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 Enabled = enable
14ef9 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
14efa 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a E_OK;.}.#endif..
14efb 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
14efc 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
14efd 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 E. /*. ** The
14efe 66 75 6e 63 74 69 6f 6e 73 20 71 75 65 72 79 53 functions queryS
14eff 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
14f00 6f 63 6b 28 29 2c 20 73 65 74 53 68 61 72 65 64 ock(), setShared
14f01 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 CacheTableLock()
14f02 2c 0a 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72 ,. ** and clear
14f03 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 AllSharedCacheTa
14f04 62 6c 65 4c 6f 63 6b 73 28 29 0a 20 20 2a 2a 20 bleLocks(). **
14f05 6d 61 6e 69 70 75 6c 61 74 65 20 65 6e 74 72 69 manipulate entri
14f06 65 73 20 69 6e 20 74 68 65 20 42 74 53 68 61 72 es in the BtShar
14f07 65 64 2e 70 4c 6f 63 6b 20 6c 69 6e 6b 65 64 20 ed.pLock linked
14f08 6c 69 73 74 20 75 73 65 64 20 74 6f 20 73 74 6f list used to sto
14f09 72 65 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 re. ** shared-c
14f0a 61 63 68 65 20 74 61 62 6c 65 20 6c 65 76 65 6c ache table level
14f0b 20 6c 6f 63 6b 73 2e 20 49 66 20 74 68 65 20 6c locks. If the l
14f0c 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c ibrary is compil
14f0d 65 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a ed with the. **
14f0e 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 66 65 shared-cache fe
14f0f 61 74 75 72 65 20 64 69 73 61 62 6c 65 64 2c 20 ature disabled,
14f10 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e then there is on
14f11 6c 79 20 65 76 65 72 20 6f 6e 65 20 75 73 65 72 ly ever one user
14f12 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68 20 42 74 . ** of each Bt
14f13 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 Shared structure
14f14 20 61 6e 64 20 73 6f 20 74 68 69 73 20 6c 6f 63 and so this loc
14f15 6b 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63 65 king is not nece
14f16 73 73 61 72 79 2e 20 0a 20 20 2a 2a 20 53 6f 20 ssary. . ** So
14f17 64 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 6b 20 define the lock
14f18 72 65 6c 61 74 65 64 20 66 75 6e 63 74 69 6f 6e related function
14f19 73 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a s as no-ops.. *
14f1a 2f 0a 20 20 23 64 65 66 69 6e 65 20 71 75 65 72 /. #define quer
14f1b 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c ySharedCacheTabl
14f1c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c eLock(a,b,c) SQL
14f1d 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 ITE_OK. #define
14f1e 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 setSharedCacheT
14f1f 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 ableLock(a,b,c)
14f20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 SQLITE_OK. #def
14f21 69 6e 65 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 ine clearAllShar
14f22 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
14f23 73 28 61 29 0a 20 20 23 64 65 66 69 6e 65 20 64 s(a). #define d
14f24 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 61 72 65 owngradeAllShare
14f25 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 dCacheTableLocks
14f26 28 61 29 0a 20 20 23 64 65 66 69 6e 65 20 68 61 (a). #define ha
14f27 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c sSharedCacheTabl
14f28 65 4c 6f 63 6b 28 61 2c 62 2c 63 2c 64 29 20 31 eLock(a,b,c,d) 1
14f29 0a 20 20 23 64 65 66 69 6e 65 20 68 61 73 52 65 . #define hasRe
14f2a 61 64 43 6f 6e 66 6c 69 63 74 73 28 61 2c 20 62 adConflicts(a, b
14f2b 29 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e ) 0.#endif..#ifn
14f2c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
14f2d 53 48 41 52 45 44 5f 43 41 43 48 45 0a 0a 23 69 SHARED_CACHE..#i
14f2e 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
14f2f 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e G./*.** This fun
14f30 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 ction is only us
14f31 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e ed as part of an
14f32 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
14f33 65 6e 74 2e 20 49 74 20 63 68 65 63 6b 73 0a 2a ent. It checks.*
14f34 2a 20 74 68 61 74 20 63 6f 6e 6e 65 63 74 69 6f * that connectio
14f35 6e 20 70 20 68 6f 6c 64 73 20 74 68 65 20 72 65 n p holds the re
14f36 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 74 6f 20 quired locks to
14f37 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 6f read or write to
14f38 20 74 68 65 20 0a 2a 2a 20 62 2d 74 72 65 65 20 the .** b-tree
14f39 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65 20 69 with root page i
14f3a 52 6f 6f 74 2e 20 49 66 20 73 6f 2c 20 74 72 75 Root. If so, tru
14f3b 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f e is returned. O
14f3c 74 68 65 72 77 69 73 65 2c 20 66 61 6c 73 65 2e therwise, false.
14f3d 20 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 .** For example
14f3e 2c 20 77 68 65 6e 20 77 72 69 74 69 6e 67 20 74 , when writing t
14f3f 6f 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 o a table b-tree
14f40 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 with root-page
14f41 69 52 6f 6f 74 20 76 69 61 20 0a 2a 2a 20 42 74 iRoot via .** Bt
14f42 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 ree connection p
14f43 42 74 72 65 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 Btree:.**.**
14f44 61 73 73 65 72 74 28 20 68 61 73 53 68 61 72 65 assert( hasShare
14f45 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 dCacheTableLock(
14f46 70 42 74 72 65 65 2c 20 69 52 6f 6f 74 2c 20 30 pBtree, iRoot, 0
14f47 2c 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 29 3b , WRITE_LOCK) );
14f48 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 72 69 74 .**.** When writ
14f49 69 6e 67 20 74 6f 20 61 6e 20 69 6e 64 65 78 20 ing to an index
14f4a 62 2d 74 72 65 65 20 74 68 61 74 20 72 65 73 69 b-tree that resi
14f4b 64 65 73 20 69 6e 20 61 20 73 68 61 72 61 62 6c des in a sharabl
14f4c 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 e database, the
14f4d 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c .** caller shoul
14f4e 64 20 68 61 76 65 20 66 69 72 73 74 20 6f 62 74 d have first obt
14f4f 61 69 6e 65 64 20 61 20 6c 6f 63 6b 20 73 70 65 ained a lock spe
14f50 63 69 66 79 69 6e 67 20 74 68 65 20 72 6f 6f 74 cifying the root
14f51 20 70 61 67 65 20 6f 66 0a 2a 2a 20 74 68 65 20 page of.** the
14f52 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 corresponding ta
14f53 62 6c 65 20 62 2d 74 72 65 65 2e 20 54 68 69 73 ble b-tree. This
14f54 20 6d 61 6b 65 73 20 74 68 69 6e 67 73 20 61 20 makes things a
14f55 62 69 74 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 bit more complic
14f56 61 74 65 64 2c 0a 2a 2a 20 61 73 20 74 68 69 73 ated,.** as this
14f57 20 6d 6f 64 75 6c 65 20 74 72 65 61 74 73 20 65 module treats e
14f58 61 63 68 20 62 2d 74 72 65 65 20 61 73 20 61 20 ach b-tree as a
14f59 73 65 70 61 72 61 74 65 20 73 74 72 75 63 74 75 separate structu
14f5a 72 65 2e 20 54 6f 20 64 65 74 65 72 6d 69 6e 65 re. To determine
14f5b 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 62 2d .** the table b-
14f5c 74 72 65 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 tree correspondi
14f5d 6e 67 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 ng to the index
14f5e 62 2d 74 72 65 65 20 62 65 69 6e 67 20 77 72 69 b-tree being wri
14f5f 74 74 65 6e 2c 20 74 68 69 73 0a 2a 2a 20 66 75 tten, this.** fu
14f60 6e 63 74 69 6f 6e 20 68 61 73 20 74 6f 20 73 65 nction has to se
14f61 61 72 63 68 20 74 68 72 6f 75 67 68 20 74 68 65 arch through the
14f62 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
14f63 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 20 ..**.** Instead
14f64 6f 66 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 of a lock on the
14f65 20 62 2d 74 72 65 65 20 72 6f 6f 74 65 64 20 61 b-tree rooted a
14f66 74 20 70 61 67 65 20 69 52 6f 6f 74 2c 20 74 68 t page iRoot, th
14f67 65 20 63 61 6c 6c 65 72 20 6d 61 79 0a 2a 2a 20 e caller may.**
14f68 68 6f 6c 64 20 61 20 77 72 69 74 65 2d 6c 6f 63 hold a write-loc
14f69 6b 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 20 k on the schema
14f6a 74 61 62 6c 65 20 28 72 6f 6f 74 20 70 61 67 65 table (root page
14f6b 20 31 29 2e 20 54 68 69 73 20 69 73 20 61 6c 73 1). This is als
14f6c 6f 0a 2a 2a 20 61 63 63 65 70 74 61 62 6c 65 2e o.** acceptable.
14f6d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 .*/.static int h
14f6e 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 asSharedCacheTab
14f6f 6c 65 4c 6f 63 6b 28 0a 20 20 42 74 72 65 65 20 leLock(. Btree
14f70 2a 70 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 *pBtree,
14f71 20 2f 2a 20 48 61 6e 64 6c 65 20 74 68 61 74 20 /* Handle that
14f72 6d 75 73 74 20 68 6f 6c 64 20 6c 6f 63 6b 20 2a must hold lock *
14f73 2f 0a 20 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 /. Pgno iRoot,
14f74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f /* Ro
14f75 6f 74 20 70 61 67 65 20 6f 66 20 62 2d 74 72 65 ot page of b-tre
14f76 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 64 e */. int isInd
14f77 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ex, /*
14f78 20 54 72 75 65 20 69 66 20 69 52 6f 6f 74 20 69 True if iRoot i
14f79 73 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 61 6e s the root of an
14f7a 20 69 6e 64 65 78 20 62 2d 74 72 65 65 20 2a 2f index b-tree */
14f7b 0a 20 20 69 6e 74 20 65 4c 6f 63 6b 54 79 70 65 . int eLockType
14f7c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 71 /* Req
14f7d 75 69 72 65 64 20 6c 6f 63 6b 20 74 79 70 65 20 uired lock type
14f7e 28 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 (READ_LOCK or WR
14f7f 49 54 45 5f 4c 4f 43 4b 29 20 2a 2f 0a 29 7b 0a ITE_LOCK) */.){.
14f80 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d Schema *pSchem
14f81 61 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 70 42 a = (Schema *)pB
14f82 74 72 65 65 2d 3e 70 42 74 2d 3e 70 53 63 68 65 tree->pBt->pSche
14f83 6d 61 3b 0a 20 20 50 67 6e 6f 20 69 54 61 62 20 ma;. Pgno iTab
14f84 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 = 0;. BtLock *p
14f85 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 Lock;.. /* If t
14f86 68 69 73 20 62 2d 74 72 65 65 20 64 61 74 61 62 his b-tree datab
14f87 61 73 65 20 69 73 20 6e 6f 74 20 73 68 61 72 65 ase is not share
14f88 61 62 6c 65 2c 20 6f 72 20 69 66 20 74 68 65 20 able, or if the
14f89 63 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e client is readin
14f8a 67 0a 20 20 2a 2a 20 61 6e 64 20 68 61 73 20 74 g. ** and has t
14f8b 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 he read-uncommit
14f8c 74 65 64 20 66 6c 61 67 20 73 65 74 2c 20 74 68 ted flag set, th
14f8d 65 6e 20 6e 6f 20 6c 6f 63 6b 20 69 73 20 72 65 en no lock is re
14f8e 71 75 69 72 65 64 2e 20 0a 20 20 2a 2a 20 49 6e quired. . ** In
14f8f 20 74 68 65 73 65 20 63 61 73 65 73 20 72 65 74 these cases ret
14f90 75 72 6e 20 74 72 75 65 20 69 6d 6d 65 64 69 61 urn true immedia
14f91 74 65 6c 79 2e 20 20 49 66 20 74 68 65 20 63 6c tely. If the cl
14f92 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 20 ient is reading
14f93 0a 20 20 2a 2a 20 6f 72 20 77 72 69 74 69 6e 67 . ** or writing
14f94 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 an index b-tree
14f95 2c 20 62 75 74 20 74 68 65 20 73 63 68 65 6d 61 , but the schema
14f96 20 69 73 20 6e 6f 74 20 6c 6f 61 64 65 64 2c 20 is not loaded,
14f97 74 68 65 6e 20 72 65 74 75 72 6e 0a 20 20 2a 2a then return. **
14f98 20 74 72 75 65 20 61 6c 73 6f 2e 20 49 6e 20 74 true also. In t
14f99 68 69 73 20 63 61 73 65 20 74 68 65 20 6c 6f 63 his case the loc
14f9a 6b 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 62 k is required, b
14f9b 75 74 20 69 74 20 69 73 20 74 6f 6f 20 64 69 66 ut it is too dif
14f9c 66 69 63 75 6c 74 0a 20 20 2a 2a 20 74 6f 20 63 ficult. ** to c
14f9d 68 65 63 6b 20 69 66 20 74 68 65 20 63 6c 69 65 heck if the clie
14f9e 6e 74 20 61 63 74 75 61 6c 6c 79 20 68 6f 6c 64 nt actually hold
14f9f 73 20 69 74 2e 20 54 68 69 73 20 64 6f 65 73 6e s it. This doesn
14fa0 27 74 20 68 61 70 70 65 6e 20 76 65 72 79 0a 20 't happen very.
14fa1 20 2a 2a 20 6f 66 74 65 6e 2e 20 20 2a 2f 0a 20 ** often. */.
14fa2 20 69 66 28 20 28 70 42 74 72 65 65 2d 3e 73 68 if( (pBtree->sh
14fa3 61 72 61 62 6c 65 3d 3d 30 29 0a 20 20 20 7c 7c arable==0). ||
14fa4 20 28 65 4c 6f 63 6b 54 79 70 65 3d 3d 52 45 41 (eLockType==REA
14fa5 44 5f 4c 4f 43 4b 20 26 26 20 28 70 42 74 72 65 D_LOCK && (pBtre
14fa6 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 e->db->flags & S
14fa7 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d QLITE_ReadUncomm
14fa8 69 74 74 65 64 29 29 0a 20 20 20 7c 7c 20 28 69 itted)). || (i
14fa9 73 49 6e 64 65 78 20 26 26 20 28 21 70 53 63 68 sIndex && (!pSch
14faa 65 6d 61 20 7c 7c 20 28 70 53 63 68 65 6d 61 2d ema || (pSchema-
14fab 3e 66 6c 61 67 73 26 44 42 5f 53 63 68 65 6d 61 >flags&DB_Schema
14fac 4c 6f 61 64 65 64 29 3d 3d 30 20 29 29 0a 20 20 Loaded)==0 )).
14fad 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b ){. return 1;
14fae 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 . }.. /* Figur
14faf 65 20 6f 75 74 20 74 68 65 20 72 6f 6f 74 2d 70 e out the root-p
14fb0 61 67 65 20 74 68 61 74 20 74 68 65 20 6c 6f 63 age that the loc
14fb1 6b 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 k should be held
14fb2 20 6f 6e 2e 20 46 6f 72 20 74 61 62 6c 65 0a 20 on. For table.
14fb3 20 2a 2a 20 62 2d 74 72 65 65 73 2c 20 74 68 69 ** b-trees, thi
14fb4 73 20 69 73 20 6a 75 73 74 20 74 68 65 20 72 6f s is just the ro
14fb5 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 ot page of the b
14fb6 2d 74 72 65 65 20 62 65 69 6e 67 20 72 65 61 64 -tree being read
14fb7 20 6f 72 0a 20 20 2a 2a 20 77 72 69 74 74 65 6e or. ** written
14fb8 2e 20 46 6f 72 20 69 6e 64 65 78 20 62 2d 74 72 . For index b-tr
14fb9 65 65 73 2c 20 69 74 20 69 73 20 74 68 65 20 72 ees, it is the r
14fba 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 oot page of the
14fbb 61 73 73 6f 63 69 61 74 65 64 0a 20 20 2a 2a 20 associated. **
14fbc 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 69 66 28 table. */. if(
14fbd 20 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 isIndex ){.
14fbe 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 20 20 20 HashElem *p;.
14fbf 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73 for(p=sqliteHas
14fc0 68 46 69 72 73 74 28 26 70 53 63 68 65 6d 61 2d hFirst(&pSchema-
14fc1 3e 69 64 78 48 61 73 68 29 3b 20 70 3b 20 70 3d >idxHash); p; p=
14fc2 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 sqliteHashNext(p
14fc3 29 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 )){. Index
14fc4 2a 70 49 64 78 20 3d 20 28 49 6e 64 65 78 20 2a *pIdx = (Index *
14fc5 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 )sqliteHashData(
14fc6 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 p);. if( pI
14fc7 64 78 2d 3e 74 6e 75 6d 3d 3d 28 69 6e 74 29 69 dx->tnum==(int)i
14fc8 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Root ){.
14fc9 69 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 iTab = pIdx->pTa
14fca 62 6c 65 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20 ble->tnum;.
14fcb 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 }. }. }else
14fcc 7b 0a 20 20 20 20 69 54 61 62 20 3d 20 69 52 6f {. iTab = iRo
14fcd 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 ot;. }.. /* Se
14fce 61 72 63 68 20 66 6f 72 20 74 68 65 20 72 65 71 arch for the req
14fcf 75 69 72 65 64 20 6c 6f 63 6b 2e 20 45 69 74 68 uired lock. Eith
14fd0 65 72 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 er a write-lock
14fd1 6f 6e 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 on root-page iTa
14fd2 62 2c 20 61 20 0a 20 20 2a 2a 20 77 72 69 74 65 b, a . ** write
14fd3 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 63 68 -lock on the sch
14fd4 65 6d 61 20 74 61 62 6c 65 2c 20 6f 72 20 28 69 ema table, or (i
14fd5 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69 73 20 f the client is
14fd6 72 65 61 64 69 6e 67 29 20 61 0a 20 20 2a 2a 20 reading) a. **
14fd7 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 69 54 61 read-lock on iTa
14fd8 62 20 77 69 6c 6c 20 73 75 66 66 69 63 65 2e 20 b will suffice.
14fd9 52 65 74 75 72 6e 20 31 20 69 66 20 61 6e 79 20 Return 1 if any
14fda 6f 66 20 74 68 65 73 65 20 61 72 65 20 66 6f 75 of these are fou
14fdb 6e 64 2e 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c nd. */. for(pL
14fdc 6f 63 6b 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d ock=pBtree->pBt-
14fdd 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3b 20 70 >pLock; pLock; p
14fde 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e 65 78 Lock=pLock->pNex
14fdf 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 t){. if( pLoc
14fe0 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 42 74 72 65 k->pBtree==pBtre
14fe1 65 20 0a 20 20 20 20 20 26 26 20 28 70 4c 6f 63 e . && (pLoc
14fe2 6b 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 k->iTable==iTab
14fe3 7c 7c 20 28 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b || (pLock->eLock
14fe4 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 26 26 20 ==WRITE_LOCK &&
14fe5 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 3d 3d 31 pLock->iTable==1
14fe6 29 29 0a 20 20 20 20 20 26 26 20 70 4c 6f 63 6b )). && pLock
14fe7 2d 3e 65 4c 6f 63 6b 3e 3d 65 4c 6f 63 6b 54 79 ->eLock>=eLockTy
14fe8 70 65 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 pe . ){.
14fe9 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
14fea 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 61 69 6c 65 . }.. /* Faile
14feb 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 72 65 d to find the re
14fec 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a quired lock. */.
14fed 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
14fee 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
14fef 6f 6e 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 on is also used
14ff0 61 73 20 70 61 72 74 20 6f 66 20 61 73 73 65 72 as part of asser
14ff1 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f t() statements o
14ff2 6e 6c 79 2e 20 49 74 20 0a 2a 2a 20 72 65 74 75 nly. It .** retu
14ff3 72 6e 73 20 74 72 75 65 20 69 66 20 74 68 65 72 rns true if ther
14ff4 65 20 65 78 69 73 74 20 6f 6e 65 20 6f 72 20 6d e exist one or m
14ff5 6f 72 65 20 63 75 72 73 6f 72 73 20 6f 70 65 6e ore cursors open
14ff6 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 0a 2a on the table .*
14ff7 2a 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65 * with root page
14ff8 20 69 52 6f 6f 74 20 74 68 61 74 20 64 6f 20 6e iRoot that do n
14ff9 6f 74 20 62 65 6c 6f 6e 67 20 74 6f 20 65 69 74 ot belong to eit
14ffa 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 her connection p
14ffb 42 74 72 65 65 20 0a 2a 2a 20 6f 72 20 73 6f 6d Btree .** or som
14ffc 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 e other connecti
14ffd 6f 6e 20 74 68 61 74 20 68 61 73 20 74 68 65 20 on that has the
14ffe 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 read-uncommitted
14fff 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2a 0a 2a 2a flag set..**.**
15000 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 62 65 For example, be
15001 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20 fore writing to
15002 70 61 67 65 20 69 52 6f 6f 74 3a 0a 2a 2a 0a 2a page iRoot:.**.*
15003 2a 20 20 20 20 61 73 73 65 72 74 28 20 21 68 61 * assert( !ha
15004 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 sReadConflicts(p
15005 42 74 72 65 65 2c 20 69 52 6f 6f 74 29 20 29 3b Btree, iRoot) );
15006 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 .*/.static int h
15007 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 asReadConflicts(
15008 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 50 Btree *pBtree, P
15009 67 6e 6f 20 69 52 6f 6f 74 29 7b 0a 20 20 42 74 gno iRoot){. Bt
1500a 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 66 6f 72 Cursor *p;. for
1500b 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e (p=pBtree->pBt->
1500c 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d pCursor; p; p=p-
1500d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 >pNext){. if(
1500e 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 p->pgnoRoot==iR
1500f 6f 6f 74 20 0a 20 20 20 20 20 26 26 20 70 2d 3e oot . && p->
15010 70 42 74 72 65 65 21 3d 70 42 74 72 65 65 0a 20 pBtree!=pBtree.
15011 20 20 20 20 26 26 20 30 3d 3d 28 70 2d 3e 70 42 && 0==(p->pB
15012 74 72 65 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 tree->db->flags
15013 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 & SQLITE_ReadUnc
15014 6f 6d 6d 69 74 74 65 64 29 0a 20 20 20 20 29 7b ommitted). ){
15015 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
15016 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
15017 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 urn 0;.}.#endif
15018 20 20 20 2f 2a 20 23 69 66 64 65 66 20 53 51 4c /* #ifdef SQL
15019 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a ITE_DEBUG */../*
1501a 0a 2a 2a 20 51 75 65 72 79 20 74 6f 20 73 65 65 .** Query to see
1501b 20 69 66 20 62 74 72 65 65 20 68 61 6e 64 6c 65 if btree handle
1501c 20 70 20 6d 61 79 20 6f 62 74 61 69 6e 20 61 20 p may obtain a
1501d 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f lock of type eLo
1501e 63 6b 20 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 ck .** (READ_LOC
1501f 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 K or WRITE_LOCK)
15020 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 on the table wi
15021 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 th root-page iTa
15022 62 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c b. Return.** SQL
15023 49 54 45 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f ITE_OK if the lo
15024 63 6b 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e ck may be obtain
15025 65 64 20 28 62 79 20 63 61 6c 6c 69 6e 67 0a 2a ed (by calling.*
15026 2a 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 * setSharedCache
15027 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2c 20 6f 72 TableLock()), or
15028 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 SQLITE_LOCKED i
15029 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 f not..*/.static
1502a 20 69 6e 74 20 71 75 65 72 79 53 68 61 72 65 64 int queryShared
1502b 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 42 CacheTableLock(B
1502c 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 tree *p, Pgno iT
1502d 61 62 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 ab, u8 eLock){.
1502e 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
1502f 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 p->pBt;. BtLoc
15030 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 k *pIter;.. ass
15031 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
15032 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 eHoldsMutex(p) )
15033 3b 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 ;. assert( eLoc
15034 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 k==READ_LOCK ||
15035 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 eLock==WRITE_LOC
15036 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 K );. assert( p
15037 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 ->db!=0 );. ass
15038 65 72 74 28 20 21 28 70 2d 3e 64 62 2d 3e 66 6c ert( !(p->db->fl
15039 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 ags&SQLITE_ReadU
1503a 6e 63 6f 6d 6d 69 74 74 65 64 29 7c 7c 65 4c 6f ncommitted)||eLo
1503b 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 7c 7c ck==WRITE_LOCK||
1503c 69 54 61 62 3d 3d 31 20 29 3b 0a 20 20 0a 20 20 iTab==1 );. .
1503d 2f 2a 20 49 66 20 72 65 71 75 65 73 74 69 6e 67 /* If requesting
1503e 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 2c 20 74 a write-lock, t
1503f 68 65 6e 20 74 68 65 20 42 74 72 65 65 20 6d 75 hen the Btree mu
15040 73 74 20 68 61 76 65 20 61 6e 20 6f 70 65 6e 20 st have an open
15041 77 72 69 74 65 0a 20 20 2a 2a 20 74 72 61 6e 73 write. ** trans
15042 61 63 74 69 6f 6e 20 6f 6e 20 74 68 69 73 20 66 action on this f
15043 69 6c 65 2e 20 41 6e 64 2c 20 6f 62 76 69 6f 75 ile. And, obviou
15044 73 6c 79 2c 20 66 6f 72 20 74 68 69 73 20 74 6f sly, for this to
15045 20 62 65 20 73 6f 20 74 68 65 72 65 20 0a 20 20 be so there .
15046 2a 2a 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 ** must be an op
15047 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 en write transac
15048 74 69 6f 6e 20 6f 6e 20 74 68 65 20 66 69 6c 65 tion on the file
15049 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 itself.. */.
1504a 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 assert( eLock==R
1504b 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 28 70 3d 3d EAD_LOCK || (p==
1504c 70 42 74 2d 3e 70 57 72 69 74 65 72 20 26 26 20 pBt->pWriter &&
1504d 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
1504e 53 5f 57 52 49 54 45 29 20 29 3b 0a 20 20 61 73 S_WRITE) );. as
1504f 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 sert( eLock==REA
15050 44 5f 4c 4f 43 4b 20 7c 7c 20 70 42 74 2d 3e 69 D_LOCK || pBt->i
15051 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 nTransaction==TR
15052 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 0a ANS_WRITE );. .
15053 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 6e /* This is a n
15054 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 68 61 72 o-op if the shar
15055 65 64 2d 63 61 63 68 65 20 69 73 20 6e 6f 74 20 ed-cache is not
15056 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 69 66 28 enabled */. if(
15057 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b !p->sharable ){
15058 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
15059 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a TE_OK;. }.. /*
1505a 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 If some other c
1505b 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c onnection is hol
1505c 64 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76 ding an exclusiv
1505d 65 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 20 2a 2a e lock, the. **
1505e 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 requested lock
1505f 6d 61 79 20 6e 6f 74 20 62 65 20 6f 62 74 61 69 may not be obtai
15060 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ned.. */. if(
15061 70 42 74 2d 3e 70 57 72 69 74 65 72 21 3d 70 20 pBt->pWriter!=p
15062 26 26 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 && pBt->isExclus
15063 69 76 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ive ){. sqlit
15064 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 e3ConnectionBloc
15065 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e ked(p->db, pBt->
15066 70 57 72 69 74 65 72 2d 3e 64 62 29 3b 0a 20 20 pWriter->db);.
15067 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15068 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 LOCKED_SHAREDCAC
15069 48 45 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 70 HE;. }.. for(p
1506a 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b Iter=pBt->pLock;
1506b 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 pIter; pIter=pI
1506c 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ter->pNext){.
1506d 20 2f 2a 20 54 68 65 20 63 6f 6e 64 69 74 69 6f /* The conditio
1506e 6e 20 28 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 n (pIter->eLock!
1506f 3d 65 4c 6f 63 6b 29 20 69 6e 20 74 68 65 20 66 =eLock) in the f
15070 6f 6c 6c 6f 77 69 6e 67 20 69 66 28 2e 2e 2e 29 ollowing if(...)
15071 20 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 . ** stateme
15072 6e 74 20 69 73 20 61 20 73 69 6d 70 6c 69 66 69 nt is a simplifi
15073 63 61 74 69 6f 6e 20 6f 66 3a 0a 20 20 20 20 2a cation of:. *
15074 2a 0a 20 20 20 20 2a 2a 20 20 20 28 65 4c 6f 63 *. ** (eLoc
15075 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c k==WRITE_LOCK ||
15076 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 pIter->eLock==W
15077 52 49 54 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 2a RITE_LOCK). *
15078 2a 0a 20 20 20 20 2a 2a 20 73 69 6e 63 65 20 77 *. ** since w
15079 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 66 20 65 e know that if e
1507a 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b Lock==WRITE_LOCK
1507b 2c 20 74 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 , then no other
1507c 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 2a connection. *
1507d 2a 20 6d 61 79 20 68 6f 6c 64 20 61 20 57 52 49 * may hold a WRI
1507e 54 45 5f 4c 4f 43 4b 20 6f 6e 20 61 6e 79 20 74 TE_LOCK on any t
1507f 61 62 6c 65 20 69 6e 20 74 68 69 73 20 66 69 6c able in this fil
15080 65 20 28 73 69 6e 63 65 20 74 68 65 72 65 20 63 e (since there c
15081 61 6e 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 62 an. ** only b
15082 65 20 61 20 73 69 6e 67 6c 65 20 77 72 69 74 65 e a single write
15083 72 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 r).. */. a
15084 73 73 65 72 74 28 20 70 49 74 65 72 2d 3e 65 4c ssert( pIter->eL
15085 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c ock==READ_LOCK |
15086 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d | pIter->eLock==
15087 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 WRITE_LOCK );.
15088 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d assert( eLock=
15089 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 =READ_LOCK || pI
1508a 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c ter->pBtree==p |
1508b 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d | pIter->eLock==
1508c 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 READ_LOCK);.
1508d 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65 if( pIter->pBtre
1508e 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d 3e 69 e!=p && pIter->i
1508f 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 Table==iTab && p
15090 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f Iter->eLock!=eLo
15091 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ck ){. sqli
15092 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f te3ConnectionBlo
15093 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 49 74 65 cked(p->db, pIte
15094 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29 3b 0a r->pBtree->db);.
15095 20 20 20 20 20 20 69 66 28 20 65 4c 6f 63 6b 3d if( eLock=
15096 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 7b 0a 20 =WRITE_LOCK ){.
15097 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
15098 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29 ==pBt->pWriter )
15099 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 69 ;. pBt->i
1509a 73 50 65 6e 64 69 6e 67 20 3d 20 31 3b 0a 20 20 sPending = 1;.
1509b 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 }. retu
1509c 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 rn SQLITE_LOCKED
1509d 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 _SHAREDCACHE;.
1509e 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1509f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 SQLITE_OK;.}.#e
150a0 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f ndif /* !SQLITE_
150a1 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
150a2 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 E */..#ifndef SQ
150a3 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
150a4 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 64 64 _CACHE./*.** Add
150a5 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 a lock on the t
150a6 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 able with root-p
150a7 61 67 65 20 69 54 61 62 6c 65 20 74 6f 20 74 68 age iTable to th
150a8 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 75 e shared-btree u
150a9 73 65 64 0a 2a 2a 20 62 79 20 42 74 72 65 65 20 sed.** by Btree
150aa 68 61 6e 64 6c 65 20 70 2e 20 50 61 72 61 6d 65 handle p. Parame
150ab 74 65 72 20 65 4c 6f 63 6b 20 6d 75 73 74 20 62 ter eLock must b
150ac 65 20 65 69 74 68 65 72 20 52 45 41 44 5f 4c 4f e either READ_LO
150ad 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 49 54 45 5f CK or .** WRITE_
150ae 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 LOCK..**.** This
150af 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 function assume
150b0 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a s the following:
150b1 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29 20 54 68 65 .**.** (a) The
150b2 20 73 70 65 63 69 66 69 65 64 20 62 2d 74 72 65 specified b-tre
150b3 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e e connection han
150b4 64 6c 65 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 dle is connected
150b5 20 74 6f 20 61 20 73 68 61 72 61 62 6c 65 0a 2a to a sharable.*
150b6 2a 20 20 20 20 20 20 20 62 2d 74 72 65 65 20 64 * b-tree d
150b7 61 74 61 62 61 73 65 20 28 6f 6e 65 20 77 69 74 atabase (one wit
150b8 68 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 73 h the BtShared.s
150b9 68 61 72 61 62 6c 65 29 20 66 6c 61 67 20 73 65 harable) flag se
150ba 74 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 28 t, and.**.** (
150bb 62 29 20 4e 6f 20 6f 74 68 65 72 20 62 2d 74 72 b) No other b-tr
150bc 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 ee connection ha
150bd 6e 64 6c 65 20 68 6f 6c 64 73 20 61 20 6c 6f 63 ndle holds a loc
150be 6b 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 73 k that conflicts
150bf 0a 2a 2a 20 20 20 20 20 20 20 77 69 74 68 20 74 .** with t
150c0 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 he requested loc
150c1 6b 20 28 69 2e 65 2e 20 71 75 65 72 79 53 68 61 k (i.e. querySha
150c2 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
150c3 6b 28 29 20 68 61 73 0a 2a 2a 20 20 20 20 20 20 k() has.**
150c4 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 61 already been ca
150c5 6c 6c 65 64 20 61 6e 64 20 72 65 74 75 72 6e 65 lled and returne
150c6 64 20 53 51 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a d SQLITE_OK)..**
150c7 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 .** SQLITE_OK is
150c8 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 returned if the
150c9 20 6c 6f 63 6b 20 69 73 20 61 64 64 65 64 20 73 lock is added s
150ca 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 53 51 4c uccessfully. SQL
150cb 49 54 45 5f 4e 4f 4d 45 4d 20 0a 2a 2a 20 69 73 ITE_NOMEM .** is
150cc 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 6d returned if a m
150cd 61 6c 6c 6f 63 20 61 74 74 65 6d 70 74 20 66 61 alloc attempt fa
150ce 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ils..*/.static i
150cf 6e 74 20 73 65 74 53 68 61 72 65 64 43 61 63 68 nt setSharedCach
150d0 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74 72 65 65 eTableLock(Btree
150d1 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 6c 65 *p, Pgno iTable
150d2 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 , u8 eLock){. B
150d3 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
150d4 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 ->pBt;. BtLock
150d5 2a 70 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74 *pLock = 0;. Bt
150d6 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20 Lock *pIter;..
150d7 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
150d8 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 treeHoldsMutex(p
150d9 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 ) );. assert( e
150da 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 Lock==READ_LOCK
150db 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f || eLock==WRITE_
150dc 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
150dd 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 0a 20 ( p->db!=0 );..
150de 20 2f 2a 20 41 20 63 6f 6e 6e 65 63 74 69 6f 6e /* A connection
150df 20 77 69 74 68 20 74 68 65 20 72 65 61 64 2d 75 with the read-u
150e0 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 ncommitted flag
150e1 73 65 74 20 77 69 6c 6c 20 6e 65 76 65 72 20 74 set will never t
150e2 72 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 74 61 69 ry to. ** obtai
150e3 6e 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 75 73 n a read-lock us
150e4 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f ing this functio
150e5 6e 2e 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 64 n. The only read
150e6 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 0a 20 -lock obtained.
150e7 20 2a 2a 20 62 79 20 61 20 63 6f 6e 6e 65 63 74 ** by a connect
150e8 69 6f 6e 20 69 6e 20 72 65 61 64 2d 75 6e 63 6f ion in read-unco
150e9 6d 6d 69 74 74 65 64 20 6d 6f 64 65 20 69 73 20 mmitted mode is
150ea 6f 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 on the sqlite_ma
150eb 73 74 65 72 20 0a 20 20 2a 2a 20 74 61 62 6c 65 ster . ** table
150ec 2c 20 61 6e 64 20 74 68 61 74 20 6c 6f 63 6b 20 , and that lock
150ed 69 73 20 6f 62 74 61 69 6e 65 64 20 69 6e 20 42 is obtained in B
150ee 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 29 treeBeginTrans()
150ef 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
150f0 30 3d 3d 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 0==(p->db->flags
150f1 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f &SQLITE_ReadUnco
150f2 6d 6d 69 74 74 65 64 29 20 7c 7c 20 65 4c 6f 63 mmitted) || eLoc
150f3 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b k==WRITE_LOCK );
150f4 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 .. /* This func
150f5 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 tion should only
150f6 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 be called on a
150f7 73 68 61 72 61 62 6c 65 20 62 2d 74 72 65 65 20 sharable b-tree
150f8 61 66 74 65 72 20 69 74 20 0a 20 20 2a 2a 20 68 after it . ** h
150f9 61 73 20 62 65 65 6e 20 64 65 74 65 72 6d 69 6e as been determin
150fa 65 64 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 ed that no other
150fb 20 62 2d 74 72 65 65 20 68 6f 6c 64 73 20 61 20 b-tree holds a
150fc 63 6f 6e 66 6c 69 63 74 69 6e 67 20 6c 6f 63 6b conflicting lock
150fd 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
150fe 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 20 p->sharable );.
150ff 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45 5f assert( SQLITE_
15100 4f 4b 3d 3d 71 75 65 72 79 53 68 61 72 65 64 43 OK==querySharedC
15101 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c acheTableLock(p,
15102 20 69 54 61 62 6c 65 2c 20 65 4c 6f 63 6b 29 20 iTable, eLock)
15103 29 3b 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 );.. /* First s
15104 65 61 72 63 68 20 74 68 65 20 6c 69 73 74 20 66 earch the list f
15105 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6c or an existing l
15106 6f 63 6b 20 6f 6e 20 74 68 69 73 20 74 61 62 6c ock on this tabl
15107 65 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 49 74 65 e. */. for(pIte
15108 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 r=pBt->pLock; pI
15109 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 ter; pIter=pIter
1510a 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 ->pNext){. if
1510b 28 20 70 49 74 65 72 2d 3e 69 54 61 62 6c 65 3d ( pIter->iTable=
1510c 3d 69 54 61 62 6c 65 20 26 26 20 70 49 74 65 72 =iTable && pIter
1510d 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 ->pBtree==p ){.
1510e 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 49 74 pLock = pIt
1510f 65 72 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b er;. break;
15110 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
15111 20 49 66 20 74 68 65 20 61 62 6f 76 65 20 73 65 If the above se
15112 61 72 63 68 20 64 69 64 20 6e 6f 74 20 66 69 6e arch did not fin
15113 64 20 61 20 42 74 4c 6f 63 6b 20 73 74 72 75 63 d a BtLock struc
15114 74 20 61 73 73 6f 63 69 61 74 69 6e 67 20 42 74 t associating Bt
15115 72 65 65 20 70 0a 20 20 2a 2a 20 77 69 74 68 20 ree p. ** with
15116 74 61 62 6c 65 20 69 54 61 62 6c 65 2c 20 61 6c table iTable, al
15117 6c 6f 63 61 74 65 20 6f 6e 65 20 61 6e 64 20 6c locate one and l
15118 69 6e 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20 ink it into the
15119 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 list.. */. if(
1511a 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 !pLock ){. p
1511b 4c 6f 63 6b 20 3d 20 28 42 74 4c 6f 63 6b 20 2a Lock = (BtLock *
1511c 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 )sqlite3MallocZe
1511d 72 6f 28 73 69 7a 65 6f 66 28 42 74 4c 6f 63 6b ro(sizeof(BtLock
1511e 29 29 3b 0a 20 20 20 20 69 66 28 20 21 70 4c 6f ));. if( !pLo
1511f 63 6b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ck ){. retu
15120 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
15121 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 6f 63 6b . }. pLock
15122 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c ->iTable = iTabl
15123 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 42 e;. pLock->pB
15124 74 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 4c tree = p;. pL
15125 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 ock->pNext = pBt
15126 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 ->pLock;. pBt
15127 2d 3e 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b ->pLock = pLock;
15128 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 . }.. /* Set t
15129 68 65 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 he BtLock.eLock
1512a 76 61 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20 variable to the
1512b 6d 61 78 69 6d 75 6d 20 6f 66 20 74 68 65 20 63 maximum of the c
1512c 75 72 72 65 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a urrent lock. **
1512d 20 61 6e 64 20 74 68 65 20 72 65 71 75 65 73 74 and the request
1512e 65 64 20 6c 6f 63 6b 2e 20 54 68 69 73 20 6d 65 ed lock. This me
1512f 61 6e 73 20 69 66 20 61 20 77 72 69 74 65 2d 6c ans if a write-l
15130 6f 63 6b 20 77 61 73 20 61 6c 72 65 61 64 79 20 ock was already
15131 68 65 6c 64 0a 20 20 2a 2a 20 61 6e 64 20 61 20 held. ** and a
15132 72 65 61 64 2d 6c 6f 63 6b 20 72 65 71 75 65 73 read-lock reques
15133 74 65 64 2c 20 77 65 20 64 6f 6e 27 74 20 69 6e ted, we don't in
15134 63 6f 72 72 65 63 74 6c 79 20 64 6f 77 6e 67 72 correctly downgr
15135 61 64 65 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 ade the lock..
15136 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 57 52 49 */. assert( WRI
15137 54 45 5f 4c 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43 TE_LOCK>READ_LOC
15138 4b 20 29 3b 0a 20 20 69 66 28 20 65 4c 6f 63 6b K );. if( eLock
15139 3e 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b >pLock->eLock ){
1513a 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 . pLock->eLoc
1513b 6b 20 3d 20 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a k = eLock;. }..
1513c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1513d 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 OK;.}.#endif /*
1513e 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 !SQLITE_OMIT_SHA
1513f 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 RED_CACHE */..#i
15140 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
15141 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f T_SHARED_CACHE./
15142 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c *.** Release all
15143 20 74 68 65 20 74 61 62 6c 65 20 6c 6f 63 6b 73 the table locks
15144 20 28 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 (locks obtained
15145 20 76 69 61 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a via calls to.**
15146 20 74 68 65 20 73 65 74 53 68 61 72 65 64 43 61 the setSharedCa
15147 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 70 cheTableLock() p
15148 72 6f 63 65 64 75 72 65 29 20 68 65 6c 64 20 62 rocedure) held b
15149 79 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 y Btree handle p
1514a 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
1514b 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 ction assumes th
1514c 61 74 20 68 61 6e 64 6c 65 20 70 20 68 61 73 20 at handle p has
1514d 61 6e 20 6f 70 65 6e 20 72 65 61 64 20 6f 72 20 an open read or
1514e 77 72 69 74 65 20 0a 2a 2a 20 74 72 61 6e 73 61 write .** transa
1514f 63 74 69 6f 6e 2e 20 49 66 20 69 74 20 64 6f 65 ction. If it doe
15150 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 s not, then the
15151 42 74 53 68 61 72 65 64 2e 69 73 50 65 6e 64 69 BtShared.isPendi
15152 6e 67 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d ng variable.** m
15153 61 79 20 62 65 20 69 6e 63 6f 72 72 65 63 74 6c ay be incorrectl
15154 79 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 73 74 y cleared..*/.st
15155 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 41 atic void clearA
15156 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 llSharedCacheTab
15157 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 leLocks(Btree *p
15158 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
15159 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 Bt = p->pBt;. B
1515a 74 4c 6f 63 6b 20 2a 2a 70 70 49 74 65 72 20 3d tLock **ppIter =
1515b 20 26 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 &pBt->pLock;..
1515c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1515d 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
1515e 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 p) );. assert(
1515f 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 30 p->sharable || 0
15160 3d 3d 2a 70 70 49 74 65 72 20 29 3b 0a 20 20 61 ==*ppIter );. a
15161 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e ssert( p->inTran
15162 73 3e 30 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 s>0 );.. while(
15163 20 2a 70 70 49 74 65 72 20 29 7b 0a 20 20 20 20 *ppIter ){.
15164 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 BtLock *pLock =
15165 2a 70 70 49 74 65 72 3b 0a 20 20 20 20 61 73 73 *ppIter;. ass
15166 65 72 74 28 20 70 42 74 2d 3e 69 73 45 78 63 6c ert( pBt->isExcl
15167 75 73 69 76 65 3d 3d 30 20 7c 7c 20 70 42 74 2d usive==0 || pBt-
15168 3e 70 57 72 69 74 65 72 3d 3d 70 4c 6f 63 6b 2d >pWriter==pLock-
15169 3e 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 61 >pBtree );. a
1516a 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 42 ssert( pLock->pB
1516b 74 72 65 65 2d 3e 69 6e 54 72 61 6e 73 3e 3d 70 tree->inTrans>=p
1516c 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 3b 0a 20 Lock->eLock );.
1516d 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 if( pLock->pB
1516e 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 tree==p ){.
1516f 20 2a 70 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b *ppIter = pLock
15170 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 61 ->pNext;. a
15171 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 69 54 ssert( pLock->iT
15172 61 62 6c 65 21 3d 31 20 7c 7c 20 70 4c 6f 63 6b able!=1 || pLock
15173 3d 3d 26 70 2d 3e 6c 6f 63 6b 20 29 3b 0a 20 20 ==&p->lock );.
15174 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 69 if( pLock->i
15175 54 61 62 6c 65 21 3d 31 20 29 7b 0a 20 20 20 20 Table!=1 ){.
15176 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
15177 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d (pLock);. }
15178 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
15179 20 20 70 70 49 74 65 72 20 3d 20 26 70 4c 6f 63 ppIter = &pLoc
1517a 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a k->pNext;. }.
1517b 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 }.. assert( p
1517c 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 3d 3d 30 Bt->isPending==0
1517d 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69 74 65 72 || pBt->pWriter
1517e 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 );. if( pBt->p
1517f 57 72 69 74 65 72 3d 3d 70 20 29 7b 0a 20 20 20 Writer==p ){.
15180 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 pBt->pWriter =
15181 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73 45 78 0;. pBt->isEx
15182 63 6c 75 73 69 76 65 20 3d 20 30 3b 0a 20 20 20 clusive = 0;.
15183 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 pBt->isPending
15184 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 = 0;. }else if(
15185 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 pBt->nTransacti
15186 6f 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 2f 2a 20 on==2 ){. /*
15187 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
15188 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 63 6f 6e called when con
15189 6e 65 63 74 69 6f 6e 20 70 20 69 73 20 63 6f 6e nection p is con
1518a 63 6c 75 64 69 6e 67 20 69 74 73 20 0a 20 20 20 cluding its .
1518b 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ** transaction.
1518c 20 49 66 20 74 68 65 72 65 20 63 75 72 72 65 6e If there curren
1518d 74 6c 79 20 65 78 69 73 74 73 20 61 20 77 72 69 tly exists a wri
1518e 74 65 72 2c 20 61 6e 64 20 70 20 69 73 20 6e 6f ter, and p is no
1518f 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 72 t. ** that wr
15190 69 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 6e iter, then the n
15191 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 68 umber of locks h
15192 65 6c 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f eld by connectio
15193 6e 73 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 ns other. **
15194 74 68 61 6e 20 74 68 65 20 77 72 69 74 65 72 20 than the writer
15195 6d 75 73 74 20 62 65 20 61 62 6f 75 74 20 74 6f must be about to
15196 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2e 20 49 drop to zero. I
15197 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20 20 20 n this case.
15198 2a 2a 20 73 65 74 20 74 68 65 20 69 73 50 65 6e ** set the isPen
15199 64 69 6e 67 20 66 6c 61 67 20 74 6f 20 30 2e 0a ding flag to 0..
1519a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 **. ** If
1519b 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 63 75 there is not cu
1519c 72 72 65 6e 74 6c 79 20 61 20 77 72 69 74 65 72 rrently a writer
1519d 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65 64 2e , then BtShared.
1519e 69 73 50 65 6e 64 69 6e 67 20 6d 75 73 74 0a 20 isPending must.
1519f 20 20 20 2a 2a 20 62 65 20 7a 65 72 6f 20 61 6c ** be zero al
151a0 72 65 61 64 79 2e 20 53 6f 20 74 68 69 73 20 6e ready. So this n
151a1 65 78 74 20 6c 69 6e 65 20 69 73 20 68 61 72 6d ext line is harm
151a2 6c 65 73 73 20 69 6e 20 74 68 61 74 20 63 61 73 less in that cas
151a3 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 42 e.. */. pB
151a4 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 30 t->isPending = 0
151a5 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 ;. }.}../*.** T
151a6 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 his function cha
151a7 6e 67 65 73 20 61 6c 6c 20 77 72 69 74 65 2d 6c nges all write-l
151a8 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 63 6f 6e ocks held by con
151a9 6e 65 63 74 69 6f 6e 20 70 20 74 6f 20 72 65 61 nection p to rea
151aa 64 2d 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 d-locks..*/.stat
151ab 69 63 20 76 6f 69 64 20 64 6f 77 6e 67 72 61 64 ic void downgrad
151ac 65 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 eAllSharedCacheT
151ad 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 ableLocks(Btree
151ae 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 *p){. BtShared
151af 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
151b0 20 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65 if( pBt->pWrite
151b1 72 3d 3d 70 20 29 7b 0a 20 20 20 20 42 74 4c 6f r==p ){. BtLo
151b2 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 ck *pLock;. p
151b3 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 30 3b Bt->pWriter = 0;
151b4 0a 20 20 20 20 70 42 74 2d 3e 69 73 45 78 63 6c . pBt->isExcl
151b5 75 73 69 76 65 20 3d 20 30 3b 0a 20 20 20 20 70 usive = 0;. p
151b6 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 Bt->isPending =
151b7 30 3b 0a 20 20 20 20 66 6f 72 28 70 4c 6f 63 6b 0;. for(pLock
151b8 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f =pBt->pLock; pLo
151b9 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d ck; pLock=pLock-
151ba 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 >pNext){. a
151bb 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c ssert( pLock->eL
151bc 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c ock==READ_LOCK |
151bd 7c 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d | pLock->pBtree=
151be 3d 70 20 29 3b 0a 20 20 20 20 20 20 70 4c 6f 63 =p );. pLoc
151bf 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 52 45 41 44 5f k->eLock = READ_
151c0 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a LOCK;. }. }.
151c1 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c }..#endif /* SQL
151c2 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
151c3 43 41 43 48 45 20 2a 2f 0a 0a 73 74 61 74 69 63 CACHE */..static
151c4 20 76 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67 void releasePag
151c5 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 e(MemPage *pPage
151c6 29 3b 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 72 ); /* Forward r
151c7 65 66 65 72 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a eference */../*.
151c8 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 ** Verify that t
151c9 68 65 20 63 75 72 73 6f 72 20 68 6f 6c 64 73 20 he cursor holds
151ca 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 42 a mutex on the B
151cb 74 53 68 61 72 65 64 0a 2a 2f 0a 23 69 66 6e 64 tShared.*/.#ifnd
151cc 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 ef NDEBUG.static
151cd 20 69 6e 74 20 63 75 72 73 6f 72 48 6f 6c 64 73 int cursorHolds
151ce 4d 75 74 65 78 28 42 74 43 75 72 73 6f 72 20 2a Mutex(BtCursor *
151cf 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c p){. return sql
151d0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
151d1 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a p->pBt->mutex);.
151d2 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 }.#endif...#ifnd
151d3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
151d4 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e NCRBLOB./*.** In
151d5 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 validate the ove
151d6 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 rflow page-list
151d7 63 61 63 68 65 20 66 6f 72 20 63 75 72 73 6f 72 cache for cursor
151d8 20 70 43 75 72 2c 20 69 66 20 61 6e 79 2e 0a 2a pCur, if any..*
151d9 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e /.static void in
151da 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 validateOverflow
151db 43 61 63 68 65 28 42 74 43 75 72 73 6f 72 20 2a Cache(BtCursor *
151dc 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 pCur){. assert(
151dd 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
151de 78 28 70 43 75 72 29 20 29 3b 0a 20 20 73 71 6c x(pCur) );. sql
151df 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e ite3_free(pCur->
151e0 61 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 aOverflow);. pC
151e1 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 ur->aOverflow =
151e2 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 0;.}../*.** Inva
151e3 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 lidate the overf
151e4 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 low page-list ca
151e5 63 68 65 20 66 6f 72 20 61 6c 6c 20 63 75 72 73 che for all curs
151e6 6f 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e ors opened.** on
151e7 20 74 68 65 20 73 68 61 72 65 64 20 62 74 72 65 the shared btre
151e8 65 20 73 74 72 75 63 74 75 72 65 20 70 42 74 2e e structure pBt.
151e9 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
151ea 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 invalidateAllOve
151eb 72 66 6c 6f 77 43 61 63 68 65 28 42 74 53 68 61 rflowCache(BtSha
151ec 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 red *pBt){. BtC
151ed 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 ursor *p;. asse
151ee 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
151ef 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
151f0 78 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 x) );. for(p=pB
151f1 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 t->pCursor; p; p
151f2 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 =p->pNext){.
151f3 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c invalidateOverfl
151f4 6f 77 43 61 63 68 65 28 70 29 3b 0a 20 20 7d 0a owCache(p);. }.
151f5 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
151f6 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
151f7 20 62 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e before modifyin
151f8 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f g the contents o
151f9 66 20 61 20 74 61 62 6c 65 0a 2a 2a 20 62 2d 74 f a table.** b-t
151fa 72 65 65 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 ree to invalidat
151fb 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63 e any incrblob c
151fc 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 ursors that are
151fd 6f 70 65 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 72 open on the.** r
151fe 6f 77 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 ow or one of the
151ff 20 72 6f 77 73 20 62 65 69 6e 67 20 6d 6f 64 69 rows being modi
15200 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 fied..**.** If a
15201 72 67 75 6d 65 6e 74 20 69 73 43 6c 65 61 72 54 rgument isClearT
15202 61 62 6c 65 20 69 73 20 74 72 75 65 2c 20 74 68 able is true, th
15203 65 6e 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f en the entire co
15204 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a ntents of the.**
15205 20 74 61 62 6c 65 20 69 73 20 61 62 6f 75 74 20 table is about
15206 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 20 49 to be deleted. I
15207 6e 20 74 68 69 73 20 63 61 73 65 20 69 6e 76 61 n this case inva
15208 6c 69 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 lidate all incrb
15209 6c 6f 62 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f lob.** cursors o
1520a 70 65 6e 20 6f 6e 20 61 6e 79 20 72 6f 77 20 77 pen on any row w
1520b 69 74 68 69 6e 20 74 68 65 20 74 61 62 6c 65 20 ithin the table
1520c 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 70 with root-page p
1520d 67 6e 6f 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 4f gnoRoot..**.** O
1520e 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 72 67 therwise, if arg
1520f 75 6d 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62 ument isClearTab
15210 6c 65 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 le is false, the
15211 6e 20 74 68 65 20 72 6f 77 20 77 69 74 68 0a 2a n the row with.*
15212 2a 20 72 6f 77 69 64 20 69 52 6f 77 20 69 73 20 * rowid iRow is
15213 62 65 69 6e 67 20 72 65 70 6c 61 63 65 64 20 6f being replaced o
15214 72 20 64 65 6c 65 74 65 64 2e 20 49 6e 20 74 68 r deleted. In th
15215 69 73 20 63 61 73 65 20 69 6e 76 61 6c 69 64 61 is case invalida
15216 74 65 0a 2a 2a 20 6f 6e 6c 79 20 74 68 6f 73 65 te.** only those
15217 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f 72 incrblob cursor
15218 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 73 s open on this s
15219 70 65 63 69 66 69 63 20 72 6f 77 2e 0a 2a 2f 0a pecific row..*/.
1521a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 static void inva
1521b 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 lidateIncrblobCu
1521c 72 73 6f 72 73 28 0a 20 20 42 74 72 65 65 20 2a rsors(. Btree *
1521d 70 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 pBtree,
1521e 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
1521f 20 66 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a file to check *
15220 2f 0a 20 20 69 36 34 20 69 52 6f 77 2c 20 20 20 /. i64 iRow,
15221 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
15222 68 65 20 72 6f 77 69 64 20 74 68 61 74 20 6d 69 he rowid that mi
15223 67 68 74 20 62 65 20 63 68 61 6e 67 69 6e 67 20 ght be changing
15224 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6c 65 61 72 */. int isClear
15225 54 61 62 6c 65 20 20 20 20 20 20 20 20 2f 2a 20 Table /*
15226 54 72 75 65 20 69 66 20 61 6c 6c 20 72 6f 77 73 True if all rows
15227 20 61 72 65 20 62 65 69 6e 67 20 64 65 6c 65 74 are being delet
15228 65 64 20 2a 2f 0a 29 7b 0a 20 20 42 74 43 75 72 ed */.){. BtCur
15229 73 6f 72 20 2a 70 3b 0a 20 20 42 74 53 68 61 72 sor *p;. BtShar
1522a 65 64 20 2a 70 42 74 20 3d 20 70 42 74 72 65 65 ed *pBt = pBtree
1522b 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 ->pBt;. assert(
1522c 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
1522d 64 73 4d 75 74 65 78 28 70 42 74 72 65 65 29 20 dsMutex(pBtree)
1522e 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e );. for(p=pBt->
1522f 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d pCursor; p; p=p-
15230 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 >pNext){. if(
15231 20 70 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 p->isIncrblobHa
15232 6e 64 6c 65 20 26 26 20 28 69 73 43 6c 65 61 72 ndle && (isClear
15233 54 61 62 6c 65 20 7c 7c 20 70 2d 3e 69 6e 66 6f Table || p->info
15234 2e 6e 4b 65 79 3d 3d 69 52 6f 77 29 20 29 7b 0a .nKey==iRow) ){.
15235 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65 20 p->eState
15236 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 = CURSOR_INVALID
15237 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 ;. }. }.}..#
15238 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 69 else. #define i
15239 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f nvalidateOverflo
1523a 77 43 61 63 68 65 28 78 29 0a 20 20 23 64 65 66 wCache(x). #def
1523b 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 41 6c ine invalidateAl
1523c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78 lOverflowCache(x
1523d 29 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 ). #define inva
1523e 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 lidateIncrblobCu
1523f 72 73 6f 72 73 28 78 2c 79 2c 7a 29 0a 23 65 6e rsors(x,y,z).#en
15240 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 62 dif../*.** Set b
15241 69 74 20 70 67 6e 6f 20 6f 66 20 74 68 65 20 42 it pgno of the B
15242 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 tShared.pHasCont
15243 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68 69 73 ent bitvec. This
15244 20 69 73 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 77 is called .** w
15245 68 65 6e 20 61 20 70 61 67 65 20 74 68 61 74 20 hen a page that
15246 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 61 previously conta
15247 69 6e 65 64 20 64 61 74 61 20 62 65 63 6f 6d 65 ined data become
15248 73 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 s a free-list le
15249 61 66 20 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2a 0a af .** page..**.
1524a 2a 2a 20 54 68 65 20 42 74 53 68 61 72 65 64 2e ** The BtShared.
1524b 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 pHasContent bitv
1524c 65 63 20 65 78 69 73 74 73 20 74 6f 20 77 6f 72 ec exists to wor
1524d 6b 20 61 72 6f 75 6e 64 20 61 6e 20 6f 62 73 63 k around an obsc
1524e 75 72 65 0a 2a 2a 20 62 75 67 20 63 61 75 73 65 ure.** bug cause
1524f 64 20 62 79 20 74 68 65 20 69 6e 74 65 72 61 63 d by the interac
15250 74 69 6f 6e 20 6f 66 20 74 77 6f 20 75 73 65 66 tion of two usef
15251 75 6c 20 49 4f 20 6f 70 74 69 6d 69 7a 61 74 69 ul IO optimizati
15252 6f 6e 73 20 73 75 72 72 6f 75 6e 64 69 6e 67 0a ons surrounding.
15253 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 ** free-list lea
15254 66 20 70 61 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 f pages:.**.**
15255 20 31 29 20 57 68 65 6e 20 61 6c 6c 20 64 61 74 1) When all dat
15256 61 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f a is deleted fro
15257 6d 20 61 20 70 61 67 65 20 61 6e 64 20 74 68 65 m a page and the
15258 20 70 61 67 65 20 62 65 63 6f 6d 65 73 0a 2a 2a page becomes.**
15259 20 20 20 20 20 20 61 20 66 72 65 65 2d 6c 69 73 a free-lis
1525a 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 68 65 t leaf page, the
1525b 20 70 61 67 65 20 69 73 20 6e 6f 74 20 77 72 69 page is not wri
1525c 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 tten to the data
1525d 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 28 61 73 base.** (as
1525e 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 free-list leaf
1525f 70 61 67 65 73 20 63 6f 6e 74 61 69 6e 20 6e 6f pages contain no
15260 20 6d 65 61 6e 69 6e 67 66 75 6c 20 64 61 74 61 meaningful data
15261 29 2e 20 53 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 ). Sometimes.**
15262 20 20 20 20 20 73 75 63 68 20 61 20 70 61 67 65 such a page
15263 20 69 73 20 6e 6f 74 20 65 76 65 6e 20 6a 6f 75 is not even jou
15264 72 6e 61 6c 6c 65 64 20 28 61 73 20 69 74 20 77 rnalled (as it w
15265 69 6c 6c 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 ill not be modif
15266 69 65 64 2c 0a 2a 2a 20 20 20 20 20 20 77 68 79 ied,.** why
15267 20 62 6f 74 68 65 72 20 6a 6f 75 72 6e 61 6c 6c bother journall
15268 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a 0a 2a 2a 20 ing it?)..**.**
15269 20 20 32 29 20 57 68 65 6e 20 61 20 66 72 65 65 2) When a free
1526a 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 -list leaf page
1526b 69 73 20 72 65 75 73 65 64 2c 20 69 74 73 20 63 is reused, its c
1526c 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 72 65 ontent is not re
1526d 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f 6d 20 ad.** from
1526e 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 the database or
1526f 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a written to the j
15270 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 77 68 79 ournal file (why
15271 20 73 68 6f 75 6c 64 20 69 74 0a 2a 2a 20 20 20 should it.**
15272 20 20 20 62 65 2c 20 69 66 20 69 74 20 69 73 20 be, if it is
15273 6e 6f 74 20 61 74 20 61 6c 6c 20 6d 65 61 6e 69 not at all meani
15274 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a 2a 2a 20 42 ngful?)..**.** B
15275 79 20 74 68 65 6d 73 65 6c 76 65 73 2c 20 74 68 y themselves, th
15276 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ese optimization
15277 73 20 77 6f 72 6b 20 66 69 6e 65 20 61 6e 64 20 s work fine and
15278 70 72 6f 76 69 64 65 20 61 20 68 61 6e 64 79 0a provide a handy.
15279 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 ** performance b
1527a 6f 6f 73 74 20 74 6f 20 62 75 6c 6b 20 64 65 6c oost to bulk del
1527b 65 74 65 20 6f 72 20 69 6e 73 65 72 74 20 6f 70 ete or insert op
1527c 65 72 61 74 69 6f 6e 73 2e 20 48 6f 77 65 76 65 erations. Howeve
1527d 72 2c 20 69 66 0a 2a 2a 20 61 20 70 61 67 65 20 r, if.** a page
1527e 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 is moved to the
1527f 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 74 68 free-list and th
15280 65 6e 20 72 65 75 73 65 64 20 77 69 74 68 69 6e en reused within
15281 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 72 61 the same.** tra
15282 6e 73 61 63 74 69 6f 6e 2c 20 61 20 70 72 6f 62 nsaction, a prob
15283 6c 65 6d 20 63 6f 6d 65 73 20 75 70 2e 20 49 66 lem comes up. If
15284 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
15285 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68 65 6e journalled when
15286 0a 2a 2a 20 69 74 20 69 73 20 6d 6f 76 65 64 20 .** it is moved
15287 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 to the free-list
15288 20 61 6e 64 20 69 74 20 69 73 20 61 6c 73 6f 20 and it is also
15289 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 not journalled w
1528a 68 65 6e 20 69 74 0a 2a 2a 20 69 73 20 65 78 74 hen it.** is ext
1528b 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 racted from the
1528c 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 72 65 free-list and re
1528d 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f used, then the o
1528e 72 69 67 69 6e 61 6c 20 64 61 74 61 0a 2a 2a 20 riginal data.**
1528f 6d 61 79 20 62 65 20 6c 6f 73 74 2e 20 49 6e 20 may be lost. In
15290 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 20 72 the event of a r
15291 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 6d 61 79 20 ollback, it may
15292 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65 0a not be possible.
15293 2a 2a 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 ** to restore th
15294 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 e database to it
15295 73 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 66 69 s original confi
15296 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 guration..**.**
15297 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 The solution is
15298 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 the BtShared.pHa
15299 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e sContent bitvec.
1529a 20 57 68 65 6e 65 76 65 72 20 61 20 70 61 67 65 Whenever a page
1529b 20 69 73 20 0a 2a 2a 20 6d 6f 76 65 64 20 74 6f is .** moved to
1529c 20 62 65 63 6f 6d 65 20 61 20 66 72 65 65 2d 6c become a free-l
1529d 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 ist leaf page, t
1529e 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
1529f 20 62 69 74 20 69 73 0a 2a 2a 20 73 65 74 20 69 bit is.** set i
152a0 6e 20 74 68 65 20 62 69 74 76 65 63 2e 20 57 68 n the bitvec. Wh
152a1 65 6e 65 76 65 72 20 61 20 6c 65 61 66 20 70 61 enever a leaf pa
152a2 67 65 20 69 73 20 65 78 74 72 61 63 74 65 64 20 ge is extracted
152a3 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 from the free-li
152a4 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 st,.** optimizat
152a5 69 6f 6e 20 32 20 61 62 6f 76 65 20 69 73 20 6f ion 2 above is o
152a6 6d 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 63 mmitted if the c
152a7 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 orresponding bit
152a8 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 73 is already.** s
152a9 65 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70 et in BtShared.p
152aa 48 61 73 43 6f 6e 74 65 6e 74 2e 20 54 68 65 20 HasContent. The
152ab 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
152ac 62 69 74 76 65 63 20 61 72 65 20 63 6c 65 61 72 bitvec are clear
152ad 65 64 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 ed.** at the end
152ae 20 6f 66 20 65 76 65 72 79 20 74 72 61 6e 73 61 of every transa
152af 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ction..*/.static
152b0 20 69 6e 74 20 62 74 72 65 65 53 65 74 48 61 73 int btreeSetHas
152b1 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 Content(BtShared
152b2 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f *pBt, Pgno pgno
152b3 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
152b4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21 LITE_OK;. if( !
152b5 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 pBt->pHasContent
152b6 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 ){. int nPag
152b7 65 20 3d 20 31 30 30 3b 0a 20 20 20 20 73 71 6c e = 100;. sql
152b8 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 ite3PagerPagecou
152b9 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 nt(pBt->pPager,
152ba 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 2f 2a 20 &nPage);. /*
152bb 49 66 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 If sqlite3PagerP
152bc 61 67 65 63 6f 75 6e 74 28 29 20 66 61 69 6c 73 agecount() fails
152bd 20 74 68 65 72 65 20 69 73 20 6e 6f 20 68 61 72 there is no har
152be 6d 20 62 65 63 61 75 73 65 20 74 68 65 0a 20 20 m because the.
152bf 20 20 2a 2a 20 6e 50 61 67 65 20 76 61 72 69 61 ** nPage varia
152c0 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 ble is unchanged
152c1 20 66 72 6f 6d 20 69 74 73 20 64 65 66 61 75 6c from its defaul
152c2 74 20 76 61 6c 75 65 20 6f 66 20 31 30 30 20 2a t value of 100 *
152c3 2f 0a 20 20 20 20 70 42 74 2d 3e 70 48 61 73 43 /. pBt->pHasC
152c4 6f 6e 74 65 6e 74 20 3d 20 73 71 6c 69 74 65 33 ontent = sqlite3
152c5 42 69 74 76 65 63 43 72 65 61 74 65 28 28 75 33 BitvecCreate((u3
152c6 32 29 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 66 2)nPage);. if
152c7 28 20 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 ( !pBt->pHasCont
152c8 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ent ){. rc
152c9 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
152ca 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
152cb 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
152cc 20 70 67 6e 6f 3c 3d 73 71 6c 69 74 65 33 42 69 pgno<=sqlite3Bi
152cd 74 76 65 63 53 69 7a 65 28 70 42 74 2d 3e 70 48 tvecSize(pBt->pH
152ce 61 73 43 6f 6e 74 65 6e 74 29 20 29 7b 0a 20 20 asContent) ){.
152cf 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 rc = sqlite3Bi
152d0 74 76 65 63 53 65 74 28 70 42 74 2d 3e 70 48 61 tvecSet(pBt->pHa
152d1 73 43 6f 6e 74 65 6e 74 2c 20 70 67 6e 6f 29 3b sContent, pgno);
152d2 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
152d3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 ;.}../*.** Query
152d4 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 the BtShared.pH
152d5 61 73 43 6f 6e 74 65 6e 74 20 76 65 63 74 6f 72 asContent vector
152d6 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
152d7 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
152d8 77 68 65 6e 20 61 20 66 72 65 65 2d 6c 69 73 74 when a free-list
152d9 20 6c 65 61 66 20 70 61 67 65 20 69 73 20 72 65 leaf page is re
152da 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a moved from the.*
152db 2a 20 66 72 65 65 2d 6c 69 73 74 20 66 6f 72 20 * free-list for
152dc 72 65 75 73 65 2e 20 49 74 20 72 65 74 75 72 6e reuse. It return
152dd 73 20 66 61 6c 73 65 20 69 66 20 69 74 20 69 73 s false if it is
152de 20 73 61 66 65 20 74 6f 20 72 65 74 72 69 65 76 safe to retriev
152df 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 66 72 e the.** page fr
152e0 6f 6d 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 om the pager lay
152e1 65 72 20 77 69 74 68 20 74 68 65 20 27 6e 6f 2d er with the 'no-
152e2 63 6f 6e 74 65 6e 74 27 20 66 6c 61 67 20 73 65 content' flag se
152e3 74 2e 20 54 72 75 65 20 6f 74 68 65 72 77 69 73 t. True otherwis
152e4 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
152e5 20 62 74 72 65 65 47 65 74 48 61 73 43 6f 6e 74 btreeGetHasCont
152e6 65 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 ent(BtShared *pB
152e7 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 t, Pgno pgno){.
152e8 20 42 69 74 76 65 63 20 2a 70 20 3d 20 70 42 74 Bitvec *p = pBt
152e9 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 3b 0a 20 ->pHasContent;.
152ea 20 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 70 return (p && (p
152eb 67 6e 6f 3e 73 71 6c 69 74 65 33 42 69 74 76 65 gno>sqlite3Bitve
152ec 63 53 69 7a 65 28 70 29 20 7c 7c 20 73 71 6c 69 cSize(p) || sqli
152ed 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 2c te3BitvecTest(p,
152ee 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a 0a 2f 2a 0a pgno)));.}../*.
152ef 2a 2a 20 43 6c 65 61 72 20 28 64 65 73 74 72 6f ** Clear (destro
152f0 79 29 20 74 68 65 20 42 74 53 68 61 72 65 64 2e y) the BtShared.
152f1 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 pHasContent bitv
152f2 65 63 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20 ec. This should
152f3 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 61 74 be.** invoked at
152f4 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 the conclusion
152f5 6f 66 20 65 61 63 68 20 77 72 69 74 65 2d 74 72 of each write-tr
152f6 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 ansaction..*/.st
152f7 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 43 atic void btreeC
152f8 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e 74 28 42 learHasContent(B
152f9 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 tShared *pBt){.
152fa 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 sqlite3BitvecDe
152fb 73 74 72 6f 79 28 70 42 74 2d 3e 70 48 61 73 43 stroy(pBt->pHasC
152fc 6f 6e 74 65 6e 74 29 3b 0a 20 20 70 42 74 2d 3e ontent);. pBt->
152fd 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d 20 30 3b pHasContent = 0;
152fe 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 .}../*.** Save t
152ff 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f he current curso
15300 72 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 r position in th
15301 65 20 76 61 72 69 61 62 6c 65 73 20 42 74 43 75 e variables BtCu
15302 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a 20 61 6e rsor.nKey .** an
15303 64 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 2e d BtCursor.pKey.
15304 20 54 68 65 20 63 75 72 73 6f 72 27 73 20 73 74 The cursor's st
15305 61 74 65 20 69 73 20 73 65 74 20 74 6f 20 43 55 ate is set to CU
15306 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b RSOR_REQUIRESEEK
15307 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c ..**.** The call
15308 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 er must ensure t
15309 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 hat the cursor i
1530a 73 20 76 61 6c 69 64 20 28 68 61 73 20 65 53 74 s valid (has eSt
1530b 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
1530c 44 29 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 63 D).** prior to c
1530d 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 alling this rout
1530e 69 6e 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 ine. .*/.static
1530f 20 69 6e 74 20 73 61 76 65 43 75 72 73 6f 72 50 int saveCursorP
15310 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 osition(BtCursor
15311 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 *pCur){. int r
15312 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 43 55 c;.. assert( CU
15313 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 RSOR_VALID==pCur
15314 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 61 73 ->eState );. as
15315 73 65 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e 70 sert( 0==pCur->p
15316 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 Key );. assert(
15317 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
15318 78 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 72 63 x(pCur) );.. rc
15319 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b = sqlite3BtreeK
1531a 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 70 43 eySize(pCur, &pC
1531b 75 72 2d 3e 6e 4b 65 79 29 3b 0a 20 20 61 73 73 ur->nKey);. ass
1531c 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
1531d 4f 4b 20 29 3b 20 20 2f 2a 20 4b 65 79 53 69 7a OK ); /* KeySiz
1531e 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 e() cannot fail
1531f 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 */.. /* If this
15320 20 69 73 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 is an intKey ta
15321 62 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 61 62 ble, then the ab
15322 6f 76 65 20 63 61 6c 6c 20 74 6f 20 42 74 72 65 ove call to Btre
15323 65 4b 65 79 53 69 7a 65 28 29 0a 20 20 2a 2a 20 eKeySize(). **
15324 73 74 6f 72 65 73 20 74 68 65 20 69 6e 74 65 67 stores the integ
15325 65 72 20 6b 65 79 20 69 6e 20 70 43 75 72 2d 3e er key in pCur->
15326 6e 4b 65 79 2e 20 49 6e 20 74 68 69 73 20 63 61 nKey. In this ca
15327 73 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 se this value is
15328 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69 . ** all that i
15329 73 20 72 65 71 75 69 72 65 64 2e 20 4f 74 68 65 s required. Othe
1532a 72 77 69 73 65 2c 20 69 66 20 70 43 75 72 20 69 rwise, if pCur i
1532b 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e 20 61 6e s not open on an
1532c 20 69 6e 74 4b 65 79 0a 20 20 2a 2a 20 74 61 62 intKey. ** tab
1532d 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c 6f 63 20 le, then malloc
1532e 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 73 74 space for and st
1532f 6f 72 65 20 74 68 65 20 70 43 75 72 2d 3e 6e 4b ore the pCur->nK
15330 65 79 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 ey bytes of key
15331 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20 20 2a 2f . ** data.. */
15332 0a 20 20 69 66 28 20 30 3d 3d 70 43 75 72 2d 3e . if( 0==pCur->
15333 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 apPage[0]->intKe
15334 79 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 y ){. void *p
15335 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c Key = sqlite3Mal
15336 6c 6f 63 28 20 28 69 6e 74 29 70 43 75 72 2d 3e loc( (int)pCur->
15337 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28 20 nKey );. if(
15338 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 pKey ){. rc
15339 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b = sqlite3BtreeK
1533a 65 79 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 ey(pCur, 0, (int
1533b 29 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 70 4b 65 )pCur->nKey, pKe
1533c 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 y);. if( rc
1533d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1533e 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 pCur->pKe
1533f 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20 y = pKey;.
15340 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 }else{. s
15341 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4b 65 79 qlite3_free(pKey
15342 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
15343 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d else{. rc =
15344 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
15345 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 }. }. asser
15346 74 28 20 21 70 43 75 72 2d 3e 61 70 50 61 67 65 t( !pCur->apPage
15347 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 21 [0]->intKey || !
15348 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a 20 pCur->pKey );..
15349 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1534a 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b OK ){. int i;
1534b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
1534c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b =pCur->iPage; i+
1534d 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 +){. releas
1534e 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 ePage(pCur->apPa
1534f 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 43 ge[i]);. pC
15350 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20 ur->apPage[i] =
15351 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 0;. }. pCu
15352 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a 20 r->iPage = -1;.
15353 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 pCur->eState
15354 3d 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 = CURSOR_REQUIRE
15355 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20 69 6e 76 SEEK;. }.. inv
15356 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 alidateOverflowC
15357 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 72 65 ache(pCur);. re
15358 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
15359 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 * Save the posit
1535a 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 63 75 72 73 ions of all curs
1535b 6f 72 73 20 65 78 63 65 70 74 20 70 45 78 63 65 ors except pExce
1535c 70 74 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 74 pt open on the t
1535d 61 62 6c 65 20 0a 2a 2a 20 77 69 74 68 20 72 6f able .** with ro
1535e 6f 74 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55 ot-page iRoot. U
1535f 73 75 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 sually, this is
15360 63 61 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f called just befo
15361 72 65 20 63 75 72 73 6f 72 0a 2a 2a 20 70 45 78 re cursor.** pEx
15362 63 65 70 74 20 69 73 20 75 73 65 64 20 74 6f 20 cept is used to
15363 6d 6f 64 69 66 79 20 74 68 65 20 74 61 62 6c 65 modify the table
15364 20 28 42 74 72 65 65 44 65 6c 65 74 65 28 29 20 (BtreeDelete()
15365 6f 72 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 or BtreeInsert()
15366 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
15367 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 saveAllCursors(
15368 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 BtShared *pBt, P
15369 67 6e 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 72 gno iRoot, BtCur
1536a 73 6f 72 20 2a 70 45 78 63 65 70 74 29 7b 0a 20 sor *pExcept){.
1536b 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 BtCursor *p;.
1536c 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1536d 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
1536e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
1536f 72 74 28 20 70 45 78 63 65 70 74 3d 3d 30 20 7c rt( pExcept==0 |
15370 7c 20 70 45 78 63 65 70 74 2d 3e 70 42 74 3d 3d | pExcept->pBt==
15371 70 42 74 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 pBt );. for(p=p
15372 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 Bt->pCursor; p;
15373 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 p=p->pNext){.
15374 20 69 66 28 20 70 21 3d 70 45 78 63 65 70 74 20 if( p!=pExcept
15375 26 26 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20 && (0==iRoot ||
15376 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f p->pgnoRoot==iRo
15377 6f 74 29 20 26 26 20 0a 20 20 20 20 20 20 20 20 ot) && .
15378 70 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f p->eState==CURSO
15379 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 R_VALID ){.
1537a 20 69 6e 74 20 72 63 20 3d 20 73 61 76 65 43 75 int rc = saveCu
1537b 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b rsorPosition(p);
1537c 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 . if( SQLIT
1537d 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 E_OK!=rc ){.
1537e 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1537f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
15380 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
15381 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c _OK;.}../*.** Cl
15382 65 61 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 ear the current
15383 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e cursor position.
15384 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
15385 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
15386 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 treeClearCursor(
15387 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
15388 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
15389 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
1538a 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 ) );. sqlite3_f
1538b 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b ree(pCur->pKey);
1538c 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 . pCur->pKey =
1538d 30 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 0;. pCur->eStat
1538e 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c e = CURSOR_INVAL
1538f 49 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20 ID;.}../*.** In
15390 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 this version of
15391 42 74 72 65 65 4d 6f 76 65 74 6f 2c 20 70 4b 65 BtreeMoveto, pKe
15392 79 20 69 73 20 61 20 70 61 63 6b 65 64 20 69 6e y is a packed in
15393 64 65 78 20 72 65 63 6f 72 64 0a 2a 2a 20 73 75 dex record.** su
15394 63 68 20 61 73 20 69 73 20 67 65 6e 65 72 61 74 ch as is generat
15395 65 64 20 62 79 20 74 68 65 20 4f 50 5f 4d 61 6b ed by the OP_Mak
15396 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 20 eRecord opcode.
15397 20 55 6e 70 61 63 6b 20 74 68 65 0a 2a 2a 20 72 Unpack the.** r
15398 65 63 6f 72 64 20 61 6e 64 20 74 68 65 6e 20 63 ecord and then c
15399 61 6c 6c 20 42 74 72 65 65 4d 6f 76 65 74 6f 55 all BtreeMovetoU
1539a 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 64 6f 20 npacked() to do
1539b 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 73 74 61 the work..*/.sta
1539c 74 69 63 20 69 6e 74 20 62 74 72 65 65 4d 6f 76 tic int btreeMov
1539d 65 74 6f 28 0a 20 20 42 74 43 75 72 73 6f 72 20 eto(. BtCursor
1539e 2a 70 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75 *pCur, /* Cu
1539f 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 rsor open on the
153a0 20 62 74 72 65 65 20 74 6f 20 62 65 20 73 65 61 btree to be sea
153a1 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 rched */. const
153a2 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f void *pKey, /
153a3 2a 20 50 61 63 6b 65 64 20 6b 65 79 20 69 66 20 * Packed key if
153a4 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6e 20 the btree is an
153a5 69 6e 64 65 78 20 2a 2f 0a 20 20 69 36 34 20 6e index */. i64 n
153a6 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f Key, /
153a7 2a 20 49 6e 74 65 67 65 72 20 6b 65 79 20 66 6f * Integer key fo
153a8 72 20 74 61 62 6c 65 73 2e 20 20 53 69 7a 65 20 r tables. Size
153a9 6f 66 20 70 4b 65 79 20 66 6f 72 20 69 6e 64 69 of pKey for indi
153aa 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 ces */. int bia
153ab 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s, /*
153ac 42 69 61 73 20 73 65 61 72 63 68 20 74 6f 20 74 Bias search to t
153ad 68 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 he high end */.
153ae 20 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 int *pRes
153af 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 /* Write se
153b0 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 arch results her
153b1 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
153b2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
153b3 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 63 /* Status c
153b4 6f 64 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 ode */. Unpacke
153b5 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 dRecord *pIdxKey
153b6 3b 20 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 ; /* Unpacked
153b7 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 63 index key */. c
153b8 68 61 72 20 61 53 70 61 63 65 5b 31 35 30 5d 3b har aSpace[150];
153b9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d /* Tem
153ba 70 20 73 70 61 63 65 20 66 6f 72 20 70 49 64 78 p space for pIdx
153bb 4b 65 79 20 2d 20 74 6f 20 61 76 6f 69 64 20 61 Key - to avoid a
153bc 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a 20 20 69 66 malloc */.. if
153bd 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 73 ( pKey ){. as
153be 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 69 36 34 sert( nKey==(i64
153bf 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b 0a 20 20 )(int)nKey );.
153c0 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 pIdxKey = sqli
153c1 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 te3VdbeRecordUnp
153c2 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e ack(pCur->pKeyIn
153c3 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 79 2c 20 70 fo, (int)nKey, p
153c4 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Key,.
153c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
153c6 20 20 20 20 20 20 20 20 20 20 20 61 53 70 61 63 aSpac
153c7 65 2c 20 73 69 7a 65 6f 66 28 61 53 70 61 63 65 e, sizeof(aSpace
153c8 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 ));. if( pIdx
153c9 4b 65 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 Key==0 ) return
153ca 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
153cb 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b }else{. pIdxK
153cc 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63 ey = 0;. }. rc
153cd 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d = sqlite3BtreeM
153ce 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 ovetoUnpacked(pC
153cf 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65 ur, pIdxKey, nKe
153d0 79 2c 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a y, bias, pRes);.
153d1 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 if( pKey ){.
153d2 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c sqlite3VdbeDel
153d3 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 eteUnpackedRecor
153d4 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 0a d(pIdxKey);. }.
153d5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
153d6 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 /*.** Restore th
153d7 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 e cursor to the
153d8 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20 position it was
153d9 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20 in (or as close
153da 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0a to as possible).
153db 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 72 73 ** when saveCurs
153dc 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73 orPosition() was
153dd 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68 called. Note th
153de 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c at this call del
153df 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 76 etes the .** sav
153e0 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f ed position info
153e1 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 65 43 stored by saveC
153e2 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c ursorPosition(),
153e3 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 62 65 so there can be
153e4 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 .** at most one
153e5 65 66 66 65 63 74 69 76 65 20 72 65 73 74 6f 72 effective restor
153e6 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
153e7 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 61 63 ) call after eac
153e8 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f 72 h .** saveCursor
153e9 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 73 Position()..*/.s
153ea 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 52 tatic int btreeR
153eb 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
153ec 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 tion(BtCursor *p
153ed 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Cur){. int rc;.
153ee 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
153ef 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
153f0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
153f1 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 ur->eState>=CURS
153f2 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 OR_REQUIRESEEK )
153f3 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 ;. if( pCur->eS
153f4 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 tate==CURSOR_FAU
153f5 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e LT ){. return
153f6 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b pCur->skipNext;
153f7 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74 . }. pCur->eSt
153f8 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 ate = CURSOR_INV
153f9 41 4c 49 44 3b 0a 20 20 72 63 20 3d 20 62 74 72 ALID;. rc = btr
153fa 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 eeMoveto(pCur, p
153fb 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d Cur->pKey, pCur-
153fc 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43 75 72 2d >nKey, 0, &pCur-
153fd 3e 73 6b 69 70 4e 65 78 74 29 3b 0a 20 20 69 66 >skipNext);. if
153fe 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
153ff 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
15400 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b ree(pCur->pKey);
15401 0a 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 . pCur->pKey
15402 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 = 0;. assert(
15403 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
15404 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 URSOR_VALID || p
15405 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
15406 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 SOR_INVALID );.
15407 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
15408 7d 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f }..#define resto
15409 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
1540a 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61 (p) \. (p->eSta
1540b 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 te>=CURSOR_REQUI
1540c 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20 RESEEK ? \.
1540d 20 20 20 20 62 74 72 65 65 52 65 73 74 6f 72 65 btreeRestore
1540e 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
1540f 29 20 3a 20 5c 0a 20 20 20 20 20 20 20 20 20 53 ) : \. S
15410 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a QLITE_OK)../*.**
15411 20 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 Determine wheth
15412 65 72 20 6f 72 20 6e 6f 74 20 61 20 63 75 72 73 er or not a curs
15413 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 66 72 6f or has moved fro
15414 6d 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 69 m the position i
15415 74 0a 2a 2a 20 77 61 73 20 6c 61 73 74 20 70 6c t.** was last pl
15416 61 63 65 64 20 61 74 2e 20 20 43 75 72 73 6f 72 aced at. Cursor
15417 73 20 63 61 6e 20 6d 6f 76 65 20 77 68 65 6e 20 s can move when
15418 74 68 65 20 72 6f 77 20 74 68 65 79 20 61 72 65 the row they are
15419 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 61 74 20 pointing.** at
1541a 69 73 20 64 65 6c 65 74 65 64 20 6f 75 74 20 66 is deleted out f
1541b 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 6d 2e 0a rom under them..
1541c 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
1541d 6e 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 ne returns an er
1541e 72 6f 72 20 63 6f 64 65 20 69 66 20 73 6f 6d 65 ror code if some
1541f 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 thing goes wrong
15420 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65 . The.** intege
15421 72 20 2a 70 48 61 73 4d 6f 76 65 64 20 69 73 20 r *pHasMoved is
15422 73 65 74 20 74 6f 20 6f 6e 65 20 69 66 20 74 68 set to one if th
15423 65 20 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 e cursor has mov
15424 65 64 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e ed and 0 if not.
15425 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
15426 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
15427 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65 reeCursorHasMove
15428 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 d(BtCursor *pCur
15429 2c 20 69 6e 74 20 2a 70 48 61 73 4d 6f 76 65 64 , int *pHasMoved
1542a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 ){. int rc;..
1542b 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 rc = restoreCurs
1542c 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 orPosition(pCur)
1542d 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 ;. if( rc ){.
1542e 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 *pHasMoved = 1
1542f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b ;. return rc;
15430 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d . }. if( pCur-
15431 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f >eState!=CURSOR_
15432 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 73 VALID || pCur->s
15433 6b 69 70 4e 65 78 74 21 3d 30 20 29 7b 0a 20 20 kipNext!=0 ){.
15434 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 *pHasMoved = 1
15435 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a ;. }else{. *
15436 70 48 61 73 4d 6f 76 65 64 20 3d 20 30 3b 0a 20 pHasMoved = 0;.
15437 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
15438 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 TE_OK;.}..#ifnde
15439 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1543a 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47 TOVACUUM./*.** G
1543b 69 76 65 6e 20 61 20 70 61 67 65 20 6e 75 6d 62 iven a page numb
1543c 65 72 20 6f 66 20 61 20 72 65 67 75 6c 61 72 20 er of a regular
1543d 64 61 74 61 62 61 73 65 20 70 61 67 65 2c 20 72 database page, r
1543e 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 0a 2a eturn the page.*
1543f 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 * number for the
15440 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 pointer-map pag
15441 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 e that contains
15442 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 the entry for th
15443 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67 65 20 e.** input page
15444 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 number..*/.stati
15445 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 50 61 67 c Pgno ptrmapPag
15446 65 6e 6f 28 42 74 53 68 61 72 65 64 20 2a 70 42 eno(BtShared *pB
15447 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 t, Pgno pgno){.
15448 20 69 6e 74 20 6e 50 61 67 65 73 50 65 72 4d 61 int nPagesPerMa
15449 70 50 61 67 65 3b 0a 20 20 50 67 6e 6f 20 69 50 pPage;. Pgno iP
1544a 74 72 4d 61 70 2c 20 72 65 74 3b 0a 20 20 61 73 trMap, ret;. as
1544b 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1544c 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
1544d 74 65 78 29 20 29 3b 0a 20 20 6e 50 61 67 65 73 tex) );. nPages
1544e 50 65 72 4d 61 70 50 61 67 65 20 3d 20 28 70 42 PerMapPage = (pB
1544f 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 29 t->usableSize/5)
15450 2b 31 3b 0a 20 20 69 50 74 72 4d 61 70 20 3d 20 +1;. iPtrMap =
15451 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73 50 (pgno-2)/nPagesP
15452 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 72 65 74 erMapPage;. ret
15453 20 3d 20 28 69 50 74 72 4d 61 70 2a 6e 50 61 67 = (iPtrMap*nPag
15454 65 73 50 65 72 4d 61 70 50 61 67 65 29 20 2b 20 esPerMapPage) +
15455 32 3b 20 0a 20 20 69 66 28 20 72 65 74 3d 3d 50 2; . if( ret==P
15456 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
15457 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65 74 (pBt) ){. ret
15458 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ++;. }. return
15459 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 ret;.}../*.** W
1545a 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e rite an entry in
1545b 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d to the pointer m
1545c 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ap..**.** This r
1545d 6f 75 74 69 6e 65 20 75 70 64 61 74 65 73 20 74 outine updates t
1545e 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 he pointer map e
1545f 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 6e 75 ntry for page nu
15460 6d 62 65 72 20 27 6b 65 79 27 0a 2a 2a 20 73 6f mber 'key'.** so
15461 20 74 68 61 74 20 69 74 20 6d 61 70 73 20 74 6f that it maps to
15462 20 74 79 70 65 20 27 65 54 79 70 65 27 20 61 6e type 'eType' an
15463 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 d parent page nu
15464 6d 62 65 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a 0a mber 'pgno'..**.
15465 2a 2a 20 49 66 20 2a 70 52 43 20 69 73 20 69 6e ** If *pRC is in
15466 69 74 69 61 6c 6c 79 20 6e 6f 6e 2d 7a 65 72 6f itially non-zero
15467 20 28 6e 6f 6e 2d 53 51 4c 49 54 45 5f 4f 4b 29 (non-SQLITE_OK)
15468 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
15469 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 ne is.** a no-op
1546a 2e 20 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f . If an error o
1546b 63 63 75 72 73 2c 20 74 68 65 20 61 70 70 72 6f ccurs, the appro
1546c 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64 priate error cod
1546d 65 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 e is written.**
1546e 69 6e 74 6f 20 2a 70 52 43 2e 0a 2a 2f 0a 73 74 into *pRC..*/.st
1546f 61 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61 70 atic void ptrmap
15470 50 75 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 Put(BtShared *pB
15471 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 t, Pgno key, u8
15472 65 54 79 70 65 2c 20 50 67 6e 6f 20 70 61 72 65 eType, Pgno pare
15473 6e 74 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 nt, int *pRC){.
15474 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 DbPage *pDbPage
15475 3b 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 ; /* The pointe
15476 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 r map page */.
15477 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 u8 *pPtrmap;
15478 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 /* The pointer
15479 20 6d 61 70 20 64 61 74 61 20 2a 2f 0a 20 20 50 map data */. P
1547a 67 6e 6f 20 69 50 74 72 6d 61 70 3b 20 20 20 20 gno iPtrmap;
1547b 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 /* The pointer
1547c 6d 61 70 20 70 61 67 65 20 6e 75 6d 62 65 72 20 map page number
1547d 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 3b */. int offset;
1547e 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
1547f 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 in pointer map
15480 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 page */. int rc
15481 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 ; /* R
15482 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 eturn code from
15483 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a subfunctions */.
15484 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 65 . if( *pRC ) re
15485 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 turn;.. assert(
15486 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15487 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
15488 29 3b 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73 74 );. /* The mast
15489 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 er-journal page
1548a 6e 75 6d 62 65 72 20 6d 75 73 74 20 6e 65 76 65 number must neve
1548b 72 20 62 65 20 75 73 65 64 20 61 73 20 61 20 70 r be used as a p
1548c 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 ointer map page
1548d 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d */. assert( 0==
1548e 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 PTRMAP_ISPAGE(pB
1548f 74 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f t, PENDING_BYTE_
15490 50 41 47 45 28 70 42 74 29 29 20 29 3b 0a 0a 20 PAGE(pBt)) );..
15491 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 assert( pBt->au
15492 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 69 66 toVacuum );. if
15493 28 20 6b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 ( key==0 ){.
15494 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f *pRC = SQLITE_CO
15495 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
15496 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 50 return;. }. iP
15497 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 trmap = PTRMAP_P
15498 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b AGENO(pBt, key);
15499 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
1549a 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 agerGet(pBt->pPa
1549b 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 ger, iPtrmap, &p
1549c 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 DbPage);. if( r
1549d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1549e 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 *pRC = rc;.
1549f 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
154a0 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 offset = PTRMAP
154a1 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d _PTROFFSET(iPtrm
154a2 61 70 2c 20 6b 65 79 29 3b 0a 20 20 69 66 28 20 ap, key);. if(
154a3 6f 66 66 73 65 74 3c 30 20 29 7b 0a 20 20 20 20 offset<0 ){.
154a4 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f *pRC = SQLITE_CO
154a5 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
154a6 67 6f 74 6f 20 70 74 72 6d 61 70 5f 65 78 69 74 goto ptrmap_exit
154a7 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d 61 70 20 ;. }. pPtrmap
154a8 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 = (u8 *)sqlite3P
154a9 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50 agerGetData(pDbP
154aa 61 67 65 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 age);.. if( eTy
154ab 70 65 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73 pe!=pPtrmap[offs
154ac 65 74 5d 20 7c 7c 20 67 65 74 34 62 79 74 65 28 et] || get4byte(
154ad 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b &pPtrmap[offset+
154ae 31 5d 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20 1])!=parent ){.
154af 20 20 20 54 52 41 43 45 28 28 22 50 54 52 4d 41 TRACE(("PTRMA
154b0 50 5f 55 50 44 41 54 45 3a 20 25 64 2d 3e 28 25 P_UPDATE: %d->(%
154b1 64 2c 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 d,%d)\n", key, e
154b2 54 79 70 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a Type, parent));.
154b3 20 20 20 20 2a 70 52 43 3d 20 72 63 20 3d 20 73 *pRC= rc = s
154b4 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
154b5 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 (pDbPage);. i
154b6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
154b7 20 29 7b 0a 20 20 20 20 20 20 70 50 74 72 6d 61 ){. pPtrma
154b8 70 5b 6f 66 66 73 65 74 5d 20 3d 20 65 54 79 70 p[offset] = eTyp
154b9 65 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 e;. put4byt
154ba 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 e(&pPtrmap[offse
154bb 74 2b 31 5d 2c 20 70 61 72 65 6e 74 29 3b 0a 20 t+1], parent);.
154bc 20 20 20 7d 0a 20 20 7d 0a 0a 70 74 72 6d 61 70 }. }..ptrmap
154bd 5f 65 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 _exit:. sqlite3
154be 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 PagerUnref(pDbPa
154bf 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ge);.}../*.** Re
154c0 61 64 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d ad an entry from
154c1 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
154c2 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
154c3 74 69 6e 65 20 72 65 74 72 69 65 76 65 73 20 74 tine retrieves t
154c4 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 he pointer map e
154c5 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 27 6b ntry for page 'k
154c6 65 79 27 2c 20 77 72 69 74 69 6e 67 0a 2a 2a 20 ey', writing.**
154c7 74 68 65 20 74 79 70 65 20 61 6e 64 20 70 61 72 the type and par
154c8 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 ent page number
154c9 74 6f 20 2a 70 45 54 79 70 65 20 61 6e 64 20 2a to *pEType and *
154ca 70 50 67 6e 6f 20 72 65 73 70 65 63 74 69 76 65 pPgno respective
154cb 6c 79 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 ly..** An error
154cc 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
154cd 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f if something go
154ce 65 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77 es wrong, otherw
154cf 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a ise SQLITE_OK..*
154d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 /.static int ptr
154d1 6d 61 70 47 65 74 28 42 74 53 68 61 72 65 64 20 mapGet(BtShared
154d2 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 *pBt, Pgno key,
154d3 75 38 20 2a 70 45 54 79 70 65 2c 20 50 67 6e 6f u8 *pEType, Pgno
154d4 20 2a 70 50 67 6e 6f 29 7b 0a 20 20 44 62 50 61 *pPgno){. DbPa
154d5 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 2f ge *pDbPage; /
154d6 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 * The pointer ma
154d7 70 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 p page */. int
154d8 69 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f iPtrmap; /
154d9 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 * Pointer map pa
154da 67 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 ge index */. u8
154db 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 *pPtrmap;
154dc 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 /* Pointer map
154dd 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 page data */. i
154de 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 nt offset;
154df 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 65 /* Offset of e
154e0 6e 74 72 79 20 69 6e 20 70 6f 69 6e 74 65 72 20 ntry in pointer
154e1 6d 61 70 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b map */. int rc;
154e2 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
154e3 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
154e4 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 Bt->mutex) );..
154e5 20 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 iPtrmap = PTRMA
154e6 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 P_PAGENO(pBt, ke
154e7 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 y);. rc = sqlit
154e8 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e e3PagerGet(pBt->
154e9 70 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c pPager, iPtrmap,
154ea 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 &pDbPage);. if
154eb 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 72 ( rc!=0 ){. r
154ec 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
154ed 70 50 74 72 6d 61 70 20 3d 20 28 75 38 20 2a 29 pPtrmap = (u8 *)
154ee 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 sqlite3PagerGetD
154ef 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 ata(pDbPage);..
154f0 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 offset = PTRMAP
154f1 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d _PTROFFSET(iPtrm
154f2 61 70 2c 20 6b 65 79 29 3b 0a 20 20 61 73 73 65 ap, key);. asse
154f3 72 74 28 20 70 45 54 79 70 65 21 3d 30 20 29 3b rt( pEType!=0 );
154f4 0a 20 20 2a 70 45 54 79 70 65 20 3d 20 70 50 74 . *pEType = pPt
154f5 72 6d 61 70 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 rmap[offset];.
154f6 69 66 28 20 70 50 67 6e 6f 20 29 20 2a 70 50 67 if( pPgno ) *pPg
154f7 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 no = get4byte(&p
154f8 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d Ptrmap[offset+1]
154f9 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 );.. sqlite3Pag
154fa 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 erUnref(pDbPage)
154fb 3b 0a 20 20 69 66 28 20 2a 70 45 54 79 70 65 3c ;. if( *pEType<
154fc 31 20 7c 7c 20 2a 70 45 54 79 70 65 3e 35 20 29 1 || *pEType>5 )
154fd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
154fe 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 72 ORRUPT_BKPT;. r
154ff 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
15500 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 .}..#else /* if
15501 64 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 4f defined SQLITE_O
15502 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a MIT_AUTOVACUUM *
15503 2f 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d /. #define ptrm
15504 61 70 50 75 74 28 77 2c 78 2c 79 2c 7a 2c 72 63 apPut(w,x,y,z,rc
15505 29 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d ). #define ptrm
15506 61 70 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20 53 apGet(w,x,y,z) S
15507 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 QLITE_OK. #defi
15508 6e 65 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c ne ptrmapPutOvfl
15509 50 74 72 28 78 2c 20 79 2c 20 72 63 29 0a 23 65 Ptr(x, y, rc).#e
1550a 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 ndif../*.** Give
1550b 6e 20 61 20 62 74 72 65 65 20 70 61 67 65 20 61 n a btree page a
1550c 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64 65 78 20 nd a cell index
1550d 28 30 20 6d 65 61 6e 73 20 74 68 65 20 66 69 72 (0 means the fir
1550e 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68 st cell on.** th
1550f 65 20 70 61 67 65 2c 20 31 20 6d 65 61 6e 73 20 e page, 1 means
15510 74 68 65 20 73 65 63 6f 6e 64 20 63 65 6c 6c 2c the second cell,
15511 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 20 72 and so forth) r
15512 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a eturn a pointer.
15513 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 ** to the cell c
15514 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 ontent..**.** Th
15515 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 is routine works
15516 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67 65 73 20 only for pages
15517 74 68 61 74 20 64 6f 20 6e 6f 74 20 63 6f 6e 74 that do not cont
15518 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c ain overflow cel
15519 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 ls..*/.#define f
1551a 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20 5c 0a 20 indCell(P,I) \.
1551b 20 28 28 50 29 2d 3e 61 44 61 74 61 20 2b 20 28 ((P)->aData + (
1551c 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65 20 26 20 (P)->maskPage &
1551d 67 65 74 32 62 79 74 65 28 26 28 50 29 2d 3e 61 get2byte(&(P)->a
1551e 44 61 74 61 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66 Data[(P)->cellOf
1551f 66 73 65 74 2b 32 2a 28 49 29 5d 29 29 29 0a 0a fset+2*(I)])))..
15520 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 20 6d 6f 72 /*.** This a mor
15521 65 20 63 6f 6d 70 6c 65 78 20 76 65 72 73 69 6f e complex versio
15522 6e 20 6f 66 20 66 69 6e 64 43 65 6c 6c 28 29 20 n of findCell()
15523 74 68 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a 2a that works for.*
15524 2a 20 70 61 67 65 73 20 74 68 61 74 20 64 6f 20 * pages that do
15525 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 contain overflow
15526 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 cells..*/.stati
15527 63 20 75 38 20 2a 66 69 6e 64 4f 76 65 72 66 6c c u8 *findOverfl
15528 6f 77 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a owCell(MemPage *
15529 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c pPage, int iCell
1552a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 ){. int i;. as
1552b 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1552c 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
1552d 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
1552e 20 66 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e 4f for(i=pPage->nO
1552f 76 65 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b verflow-1; i>=0;
15530 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6b i--){. int k
15531 3b 0a 20 20 20 20 73 74 72 75 63 74 20 5f 4f 76 ;. struct _Ov
15532 66 6c 43 65 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20 flCell *pOvfl;.
15533 20 20 20 70 4f 76 66 6c 20 3d 20 26 70 50 61 67 pOvfl = &pPag
15534 65 2d 3e 61 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20 e->aOvfl[i];.
15535 20 6b 20 3d 20 70 4f 76 66 6c 2d 3e 69 64 78 3b k = pOvfl->idx;
15536 0a 20 20 20 20 69 66 28 20 6b 3c 3d 69 43 65 6c . if( k<=iCel
15537 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6b l ){. if( k
15538 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 ==iCell ){.
15539 20 20 20 72 65 74 75 72 6e 20 70 4f 76 66 6c 2d return pOvfl-
1553a 3e 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a >pCell;. }.
1553b 20 20 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20 iCell--;.
1553c 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
1553d 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 n findCell(pPage
1553e 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a , iCell);.}../*.
1553f 2a 2a 20 50 61 72 73 65 20 61 20 63 65 6c 6c 20 ** Parse a cell
15540 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 6e content block an
15541 64 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 65 d fill in the Ce
15542 6c 6c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 llInfo structure
15543 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20 . There.** are
15544 74 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 two versions of
15545 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 this function.
15546 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 btreeParseCell()
15547 20 74 61 6b 65 73 20 61 20 0a 2a 2a 20 63 65 6c takes a .** cel
15548 6c 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 73 l index as the s
15549 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 61 econd argument a
1554a 6e 64 20 62 74 72 65 65 50 61 72 73 65 43 65 6c nd btreeParseCel
1554b 6c 50 74 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73 lPtr() .** takes
1554c 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
1554d 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 63 65 e body of the ce
1554e 6c 6c 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64 ll as its second
1554f 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
15550 20 57 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c Within this fil
15551 65 2c 20 74 68 65 20 70 61 72 73 65 43 65 6c 6c e, the parseCell
15552 28 29 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 () macro can be
15553 63 61 6c 6c 65 64 20 69 6e 73 74 65 61 64 20 6f called instead o
15554 66 0a 2a 2a 20 62 74 72 65 65 50 61 72 73 65 43 f.** btreeParseC
15555 65 6c 6c 50 74 72 28 29 2e 20 55 73 69 6e 67 20 ellPtr(). Using
15556 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20 some compilers,
15557 74 68 69 73 20 77 69 6c 6c 20 62 65 20 66 61 73 this will be fas
15558 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ter..*/.static v
15559 6f 69 64 20 62 74 72 65 65 50 61 72 73 65 43 65 oid btreeParseCe
1555a 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 65 llPtr(. MemPage
1555b 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 *pPage,
1555c 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e /* Page contain
1555d 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a ing the cell */.
1555e 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 u8 *pCell,
1555f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
15560 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c nter to the cell
15561 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c text. */. Cell
15562 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 Info *pInfo
15563 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 /* Fill in t
15564 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f his structure */
15565 0a 29 7b 0a 20 20 75 31 36 20 6e 3b 20 20 20 20 .){. u16 n;
15566 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
15567 20 4e 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e Number bytes in
15568 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 cell content he
15569 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 50 ader */. u32 nP
1556a 61 79 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20 ayload;
1556b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1556c 79 74 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79 ytes of cell pay
1556d 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 load */.. asser
1556e 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1556f 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
15570 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 ->mutex) );.. p
15571 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20 70 43 Info->pCell = pC
15572 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ell;. assert( p
15573 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c Page->leaf==0 ||
15574 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 pPage->leaf==1
15575 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67 65 2d 3e );. n = pPage->
15576 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 childPtrSize;.
15577 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d 34 2a 70 assert( n==4-4*p
15578 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 Page->leaf );.
15579 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 if( pPage->intKe
1557a 79 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 y ){. if( pPa
1557b 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 ge->hasData ){.
1557c 20 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 n += getVar
1557d 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c int32(&pCell[n],
1557e 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 nPayload);.
1557f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 }else{. nPa
15580 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 20 20 7d yload = 0;. }
15581 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 . n += getVar
15582 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28 int(&pCell[n], (
15583 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 u64*)&pInfo->nKe
15584 79 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e y);. pInfo->n
15585 44 61 74 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b Data = nPayload;
15586 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 . }else{. pI
15587 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a nfo->nData = 0;.
15588 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 n += getVari
15589 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 nt32(&pCell[n],
1558a 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70 nPayload);. p
1558b 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61 Info->nKey = nPa
1558c 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20 70 49 6e yload;. }. pIn
1558d 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e fo->nPayload = n
1558e 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f Payload;. pInfo
1558f 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e 3b 0a 20 ->nHeader = n;.
15590 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 79 6c testcase( nPayl
15591 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c oad==pPage->maxL
15592 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73 74 63 61 ocal );. testca
15593 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 50 se( nPayload==pP
15594 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 age->maxLocal+1
15595 29 3b 0a 20 20 69 66 28 20 6c 69 6b 65 6c 79 28 );. if( likely(
15596 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 2d nPayload<=pPage-
15597 3e 6d 61 78 4c 6f 63 61 6c 29 20 29 7b 0a 20 20 >maxLocal) ){.
15598 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 /* This is the
15599 20 28 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 63 (easy) common c
1559a 61 73 65 20 77 68 65 72 65 20 74 68 65 20 65 6e ase where the en
1559b 74 69 72 65 20 70 61 79 6c 6f 61 64 20 66 69 74 tire payload fit
1559c 73 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 s. ** on the
1559d 6c 6f 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f 20 local page. No
1559e 6f 76 65 72 66 6c 6f 77 20 69 73 20 72 65 71 75 overflow is requ
1559f 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ired.. */.
155a0 20 69 6e 74 20 6e 53 69 7a 65 3b 20 20 20 20 20 int nSize;
155a1 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 /* Total si
155a2 7a 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65 ze of cell conte
155a3 6e 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 nt in bytes */.
155a4 20 20 20 6e 53 69 7a 65 20 3d 20 6e 50 61 79 6c nSize = nPayl
155a5 6f 61 64 20 2b 20 6e 3b 0a 20 20 20 20 70 49 6e oad + n;. pIn
155a6 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 fo->nLocal = (u1
155a7 36 29 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 6)nPayload;.
155a8 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 pInfo->iOverflow
155a9 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 28 6e = 0;. if( (n
155aa 53 69 7a 65 20 26 20 7e 33 29 3d 3d 30 20 29 7b Size & ~3)==0 ){
155ab 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 34 . nSize = 4
155ac 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 ; /* Mini
155ad 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 mum cell size is
155ae 20 34 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 4 */. }.
155af 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 28 pInfo->nSize = (
155b0 75 31 36 29 6e 53 69 7a 65 3b 0a 20 20 7d 65 6c u16)nSize;. }el
155b1 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 se{. /* If th
155b2 65 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c 20 6e e payload will n
155b3 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65 74 65 6c ot fit completel
155b4 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 y on the local p
155b5 61 67 65 2c 20 77 65 20 68 61 76 65 0a 20 20 20 age, we have.
155b6 20 2a 2a 20 74 6f 20 64 65 63 69 64 65 20 68 6f ** to decide ho
155b7 77 20 6d 75 63 68 20 74 6f 20 73 74 6f 72 65 20 w much to store
155b8 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77 20 locally and how
155b9 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f 6e much to spill on
155ba 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c to. ** overfl
155bb 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 73 ow pages. The s
155bc 74 72 61 74 65 67 79 20 69 73 20 74 6f 20 6d 69 trategy is to mi
155bd 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75 6e nimize the amoun
155be 74 20 6f 66 20 75 6e 75 73 65 64 0a 20 20 20 20 t of unused.
155bf 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f 76 65 72 ** space on over
155c0 66 6c 6f 77 20 70 61 67 65 73 20 77 68 69 6c 65 flow pages while
155c1 20 6b 65 65 70 69 6e 67 20 74 68 65 20 61 6d 6f keeping the amo
155c2 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74 6f unt of local sto
155c3 72 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20 62 rage. ** in b
155c4 65 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20 etween minLocal
155c5 61 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20 and maxLocal..
155c6 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 72 6e **. ** Warn
155c7 69 6e 67 3a 20 20 63 68 61 6e 67 69 6e 67 20 74 ing: changing t
155c8 68 65 20 77 61 79 20 6f 76 65 72 66 6c 6f 77 20 he way overflow
155c9 70 61 79 6c 6f 61 64 20 69 73 20 64 69 73 74 72 payload is distr
155ca 69 62 75 74 65 64 20 69 6e 20 61 6e 79 0a 20 20 ibuted in any.
155cb 20 20 2a 2a 20 77 61 79 20 77 69 6c 6c 20 72 65 ** way will re
155cc 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6d sult in an incom
155cd 70 61 74 69 62 6c 65 20 66 69 6c 65 20 66 6f 72 patible file for
155ce 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 mat.. */.
155cf 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f int minLocal; /
155d0 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 * Minimum amount
155d1 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 of payload held
155d2 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 locally */.
155d3 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f int maxLocal; /
155d4 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 * Maximum amount
155d5 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 of payload held
155d6 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 locally */.
155d7 69 6e 74 20 73 75 72 70 6c 75 73 3b 20 20 20 2f int surplus; /
155d8 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f * Overflow paylo
155d9 61 64 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 ad available for
155da 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 20 2a local storage *
155db 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c 20 /.. minLocal
155dc 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 = pPage->minLoca
155dd 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c 20 l;. maxLocal
155de 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 = pPage->maxLoca
155df 6c 3b 0a 20 20 20 20 73 75 72 70 6c 75 73 20 3d l;. surplus =
155e0 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50 61 minLocal + (nPa
155e1 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c yload - minLocal
155e2 29 25 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 )%(pPage->pBt->u
155e3 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a sableSize - 4);.
155e4 20 20 20 20 74 65 73 74 63 61 73 65 28 20 73 75 testcase( su
155e5 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 20 rplus==maxLocal
155e6 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
155e7 20 73 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 surplus==maxLoc
155e8 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 al+1 );. if(
155e9 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f surplus <= maxLo
155ea 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 70 49 6e cal ){. pIn
155eb 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 fo->nLocal = (u1
155ec 36 29 73 75 72 70 6c 75 73 3b 0a 20 20 20 20 7d 6)surplus;. }
155ed 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 66 else{. pInf
155ee 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 o->nLocal = (u16
155ef 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d )minLocal;. }
155f0 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 . pInfo->iOve
155f1 72 66 6c 6f 77 20 3d 20 28 75 31 36 29 28 70 49 rflow = (u16)(pI
155f2 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 29 nfo->nLocal + n)
155f3 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 ;. pInfo->nSi
155f4 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 ze = pInfo->iOve
155f5 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d 0a 7d rflow + 4;. }.}
155f6 0a 23 64 65 66 69 6e 65 20 70 61 72 73 65 43 65 .#define parseCe
155f7 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c ll(pPage, iCell,
155f8 20 70 49 6e 66 6f 29 20 5c 0a 20 20 62 74 72 65 pInfo) \. btre
155f9 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70 eParseCellPtr((p
155fa 50 61 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28 Page), findCell(
155fb 28 70 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29 (pPage), (iCell)
155fc 29 2c 20 28 70 49 6e 66 6f 29 29 0a 73 74 61 74 ), (pInfo)).stat
155fd 69 63 20 76 6f 69 64 20 62 74 72 65 65 50 61 72 ic void btreePar
155fe 73 65 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 seCell(. MemPag
155ff 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 e *pPage,
15600 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 /* Page contai
15601 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f ning the cell */
15602 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20 . int iCell,
15603 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
15604 65 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46 e cell index. F
15605 69 72 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a irst cell is 0 *
15606 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 /. CellInfo *pI
15607 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 nfo /* F
15608 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75 ill in this stru
15609 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61 cture */.){. pa
1560a 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 rseCell(pPage, i
1560b 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a Cell, pInfo);.}.
1560c 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 ./*.** Compute t
1560d 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
1560e 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 61 20 of bytes that a
1560f 43 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68 Cell needs in th
15610 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 e cell.** data a
15611 72 65 61 20 6f 66 20 74 68 65 20 62 74 72 65 65 rea of the btree
15612 2d 70 61 67 65 2e 20 20 54 68 65 20 72 65 74 75 -page. The retu
15613 72 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64 rn number includ
15614 65 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 es the cell.** d
15615 61 74 61 20 68 65 61 64 65 72 20 61 6e 64 20 74 ata header and t
15616 68 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 he local payload
15617 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76 , but not any ov
15618 65 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a erflow page or.*
15619 2a 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64 * the space used
1561a 20 62 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 by the cell poi
1561b 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 nter..*/.static
1561c 75 31 36 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 u16 cellSizePtr(
1561d 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
1561e 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38 u8 *pCell){. u8
1561f 20 2a 70 49 74 65 72 20 3d 20 26 70 43 65 6c 6c *pIter = &pCell
15620 5b 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 [pPage->childPtr
15621 53 69 7a 65 5d 3b 0a 20 20 75 33 32 20 6e 53 69 Size];. u32 nSi
15622 7a 65 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ze;..#ifdef SQLI
15623 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 TE_DEBUG. /* Th
15624 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
15625 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
15626 6e 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 n should always
15627 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 0a 20 be the same as.
15628 20 2a 2a 20 74 68 65 20 28 43 65 6c 6c 49 6e 66 ** the (CellInf
15629 6f 2e 6e 53 69 7a 65 29 20 76 61 6c 75 65 20 66 o.nSize) value f
1562a 6f 75 6e 64 20 62 79 20 64 6f 69 6e 67 20 61 20 ound by doing a
1562b 66 75 6c 6c 20 70 61 72 73 65 20 6f 66 20 74 68 full parse of th
1562c 65 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20 49 66 20 e. ** cell. If
1562d 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 SQLITE_DEBUG is
1562e 64 65 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 defined, an asse
1562f 72 74 28 29 20 61 74 20 74 68 65 20 62 6f 74 74 rt() at the bott
15630 6f 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20 om of. ** this
15631 66 75 6e 63 74 69 6f 6e 20 76 65 72 69 66 69 65 function verifie
15632 73 20 74 68 61 74 20 74 68 69 73 20 69 6e 76 61 s that this inva
15633 72 69 61 6e 74 20 69 73 20 6e 6f 74 20 76 69 6f riant is not vio
15634 6c 61 74 65 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c lated. */. Cell
15635 49 6e 66 6f 20 64 65 62 75 67 69 6e 66 6f 3b 0a Info debuginfo;.
15636 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c btreeParseCell
15637 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c Ptr(pPage, pCell
15638 2c 20 26 64 65 62 75 67 69 6e 66 6f 29 3b 0a 23 , &debuginfo);.#
15639 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 70 50 61 endif.. if( pPa
1563a 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 ge->intKey ){.
1563b 20 20 75 38 20 2a 70 45 6e 64 3b 0a 20 20 20 20 u8 *pEnd;.
1563c 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 if( pPage->hasDa
1563d 74 61 20 29 7b 0a 20 20 20 20 20 20 70 49 74 65 ta ){. pIte
1563e 72 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 r += getVarint32
1563f 28 70 49 74 65 72 2c 20 6e 53 69 7a 65 29 3b 0a (pIter, nSize);.
15640 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
15641 20 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 nSize = 0;.
15642 7d 0a 0a 20 20 20 20 2f 2a 20 70 49 74 65 72 20 }.. /* pIter
15643 6e 6f 77 20 70 6f 69 6e 74 73 20 61 74 20 74 68 now points at th
15644 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 e 64-bit integer
15645 20 6b 65 79 20 76 61 6c 75 65 2c 20 61 20 76 61 key value, a va
15646 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 0a 20 riable length .
15647 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 54 ** integer. T
15648 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f he following blo
15649 63 6b 20 6d 6f 76 65 73 20 70 49 74 65 72 20 74 ck moves pIter t
1564a 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 66 o point at the f
1564b 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 2a 2a irst byte. **
1564c 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
1564d 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 the key value.
1564e 2a 2f 0a 20 20 20 20 70 45 6e 64 20 3d 20 26 70 */. pEnd = &p
1564f 49 74 65 72 5b 39 5d 3b 0a 20 20 20 20 77 68 69 Iter[9];. whi
15650 6c 65 28 20 28 2a 70 49 74 65 72 2b 2b 29 26 30 le( (*pIter++)&0
15651 78 38 30 20 26 26 20 70 49 74 65 72 3c 70 45 6e x80 && pIter<pEn
15652 64 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 d );. }else{.
15653 20 20 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61 pIter += getVa
15654 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53 rint32(pIter, nS
15655 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 74 65 73 ize);. }.. tes
15656 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 tcase( nSize==pP
15657 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b age->maxLocal );
15658 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 . testcase( nSi
15659 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f ze==pPage->maxLo
1565a 63 61 6c 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e cal+1 );. if( n
1565b 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 4c Size>pPage->maxL
1565c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 ocal ){. int
1565d 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 minLocal = pPage
1565e 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 ->minLocal;.
1565f 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c nSize = minLocal
15660 20 2b 20 28 6e 53 69 7a 65 20 2d 20 6d 69 6e 4c + (nSize - minL
15661 6f 63 61 6c 29 20 25 20 28 70 50 61 67 65 2d 3e ocal) % (pPage->
15662 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
15663 2d 20 34 29 3b 0a 20 20 20 20 74 65 73 74 63 61 - 4);. testca
15664 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 se( nSize==pPage
15665 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 ->maxLocal );.
15666 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a testcase( nSiz
15667 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 e==pPage->maxLoc
15668 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 al+1 );. if(
15669 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 nSize>pPage->max
1566a 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 6e Local ){. n
1566b 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b Size = minLocal;
1566c 0a 20 20 20 20 7d 0a 20 20 20 20 6e 53 69 7a 65 . }. nSize
1566d 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 6e 53 69 += 4;. }. nSi
1566e 7a 65 20 2b 3d 20 28 75 33 32 29 28 70 49 74 65 ze += (u32)(pIte
1566f 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 0a 20 20 2f r - pCell);.. /
15670 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69 * The minimum si
15671 7a 65 20 6f 66 20 61 6e 79 20 63 65 6c 6c 20 69 ze of any cell i
15672 73 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 s 4 bytes. */.
15673 69 66 28 20 6e 53 69 7a 65 3c 34 20 29 7b 0a 20 if( nSize<4 ){.
15674 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 20 nSize = 4;.
15675 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 53 69 }.. assert( nSi
15676 7a 65 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e 53 ze==debuginfo.nS
15677 69 7a 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ize );. return
15678 28 75 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a 23 69 (u16)nSize;.}.#i
15679 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 fndef NDEBUG.sta
1567a 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 tic u16 cellSize
1567b 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
1567c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 72 int iCell){. r
1567d 65 74 75 72 6e 20 63 65 6c 6c 53 69 7a 65 50 74 eturn cellSizePt
1567e 72 28 70 50 61 67 65 2c 20 66 69 6e 64 43 65 6c r(pPage, findCel
1567f 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 29 29 l(pPage, iCell))
15680 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e ;.}.#endif..#ifn
15681 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
15682 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a AUTOVACUUM./*.**
15683 20 49 66 20 74 68 65 20 63 65 6c 6c 20 70 43 65 If the cell pCe
15684 6c 6c 2c 20 70 61 72 74 20 6f 66 20 70 61 67 65 ll, part of page
15685 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 pPage contains
15686 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 a pointer.** to
15687 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 an overflow page
15688 2c 20 69 6e 73 65 72 74 20 61 6e 20 65 6e 74 72 , insert an entr
15689 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 y into the point
1568a 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 68 er-map.** for th
1568b 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e e overflow page.
1568c 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1568d 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 ptrmapPutOvflPtr
1568e 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
1568f 20 75 38 20 2a 70 43 65 6c 6c 2c 20 69 6e 74 20 u8 *pCell, int
15690 2a 70 52 43 29 7b 0a 20 20 43 65 6c 6c 49 6e 66 *pRC){. CellInf
15691 6f 20 69 6e 66 6f 3b 0a 20 20 69 66 28 20 2a 70 o info;. if( *p
15692 52 43 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 RC ) return;. a
15693 73 73 65 72 74 28 20 70 43 65 6c 6c 21 3d 30 20 ssert( pCell!=0
15694 29 3b 0a 20 20 62 74 72 65 65 50 61 72 73 65 43 );. btreeParseC
15695 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 ellPtr(pPage, pC
15696 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 61 ell, &info);. a
15697 73 73 65 72 74 28 20 28 69 6e 66 6f 2e 6e 44 61 ssert( (info.nDa
15698 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 ta+(pPage->intKe
15699 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d y?0:info.nKey))=
1569a 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 =info.nPayload )
1569b 3b 0a 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 ;. if( info.iOv
1569c 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 50 67 erflow ){. Pg
1569d 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34 62 79 no ovfl = get4by
1569e 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 te(&pCell[info.i
1569f 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 Overflow]);.
156a0 70 74 72 6d 61 70 50 75 74 28 70 50 61 67 65 2d ptrmapPut(pPage-
156a1 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 54 52 4d >pBt, ovfl, PTRM
156a2 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70 50 AP_OVERFLOW1, pP
156a3 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 52 43 29 3b age->pgno, pRC);
156a4 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a . }.}.#endif...
156a5 2f 2a 0a 2a 2a 20 44 65 66 72 61 67 6d 65 6e 74 /*.** Defragment
156a6 20 74 68 65 20 70 61 67 65 20 67 69 76 65 6e 2e the page given.
156a7 20 20 41 6c 6c 20 43 65 6c 6c 73 20 61 72 65 20 All Cells are
156a8 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 moved to the.**
156a9 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 20 end of the page
156aa 61 6e 64 20 61 6c 6c 20 66 72 65 65 20 73 70 61 and all free spa
156ab 63 65 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 ce is collected
156ac 69 6e 74 6f 20 6f 6e 65 0a 2a 2a 20 62 69 67 20 into one.** big
156ad 46 72 65 65 42 6c 6b 20 74 68 61 74 20 6f 63 63 FreeBlk that occ
156ae 75 72 73 20 69 6e 20 62 65 74 77 65 65 6e 20 74 urs in between t
156af 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 63 65 he header and ce
156b0 6c 6c 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 72 ll.** pointer ar
156b1 72 61 79 20 61 6e 64 20 74 68 65 20 63 65 6c 6c ray and the cell
156b2 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 0a 2a content area..*
156b3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 66 /.static int def
156b4 72 61 67 6d 65 6e 74 50 61 67 65 28 4d 65 6d 50 ragmentPage(MemP
156b5 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 age *pPage){. i
156b6 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
156b7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
156b8 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 p counter */. i
156b9 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 nt pc;
156ba 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 /* Add
156bb 72 65 73 73 20 6f 66 20 61 20 69 2d 74 68 20 63 ress of a i-th c
156bc 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 ell */. int hdr
156bd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
156be 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f /* Offset to
156bf 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 the page header
156c0 20 2a 2f 0a 20 20 69 6e 74 20 73 69 7a 65 3b 20 */. int size;
156c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
156c2 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65 /* Size of a ce
156c3 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 ll */. int usab
156c4 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 leSize;
156c5 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
156c6 75 73 61 62 6c 65 20 62 79 74 65 73 20 6f 6e 20 usable bytes on
156c7 61 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 a page */. int
156c8 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 cellOffset;
156c9 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
156ca 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 to the cell poi
156cb 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 nter array */.
156cc 69 6e 74 20 63 62 72 6b 3b 20 20 20 20 20 20 20 int cbrk;
156cd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 /* Of
156ce 66 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c fset to the cell
156cf 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f content area */
156d0 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 . int nCell;
156d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
156d2 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 Number of cells
156d3 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a on the page */.
156d4 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
156d5 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 *data; /*
156d6 54 68 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f The page data */
156d7 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
156d8 20 2a 74 65 6d 70 3b 20 20 20 20 20 20 20 2f 2a *temp; /*
156d9 20 54 65 6d 70 20 61 72 65 61 20 66 6f 72 20 63 Temp area for c
156da 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 ell content */.
156db 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 73 74 3b int iCellFirst;
156dc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
156dd 69 72 73 74 20 61 6c 6c 6f 77 61 62 6c 65 20 63 irst allowable c
156de 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 ell index */. i
156df 6e 74 20 69 43 65 6c 6c 4c 61 73 74 3b 20 20 20 nt iCellLast;
156e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 /* Las
156e1 74 20 70 6f 73 73 69 62 6c 65 20 63 65 6c 6c 20 t possible cell
156e2 69 6e 64 65 78 20 2a 2f 0a 0a 0a 20 20 61 73 73 index */... ass
156e3 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
156e4 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 rIswriteable(pPa
156e5 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
156e6 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
156e7 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 >pBt!=0 );. ass
156e8 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 2d ert( pPage->pBt-
156e9 3e 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53 >usableSize <= S
156ea 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 QLITE_MAX_PAGE_S
156eb 49 5a 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 IZE );. assert(
156ec 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f pPage->nOverflo
156ed 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 w==0 );. assert
156ee 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
156ef 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
156f0 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 65 6d >mutex) );. tem
156f1 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 p = sqlite3Pager
156f2 54 65 6d 70 53 70 61 63 65 28 70 50 61 67 65 2d TempSpace(pPage-
156f3 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 >pBt->pPager);.
156f4 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 data = pPage->a
156f5 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 Data;. hdr = pP
156f6 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a age->hdrOffset;.
156f7 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 cellOffset = p
156f8 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 Page->cellOffset
156f9 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 ;. nCell = pPag
156fa 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65 e->nCell;. asse
156fb 72 74 28 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62 rt( nCell==get2b
156fc 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d yte(&data[hdr+3]
156fd 29 20 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a ) );. usableSiz
156fe 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e e = pPage->pBt->
156ff 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 62 usableSize;. cb
15700 72 6b 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 rk = get2byte(&d
15701 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6d ata[hdr+5]);. m
15702 65 6d 63 70 79 28 26 74 65 6d 70 5b 63 62 72 6b emcpy(&temp[cbrk
15703 5d 2c 20 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 ], &data[cbrk],
15704 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 63 62 72 usableSize - cbr
15705 6b 29 3b 0a 20 20 63 62 72 6b 20 3d 20 75 73 61 k);. cbrk = usa
15706 62 6c 65 53 69 7a 65 3b 0a 20 20 69 43 65 6c 6c bleSize;. iCell
15707 46 69 72 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73 First = cellOffs
15708 65 74 20 2b 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20 et + 2*nCell;.
15709 69 43 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 iCellLast = usab
1570a 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 66 6f leSize - 4;. fo
1570b 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 r(i=0; i<nCell;
1570c 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 41 i++){. u8 *pA
1570d 64 64 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 ddr; /* The
1570e 69 2d 74 68 20 63 65 6c 6c 20 70 6f 69 6e 74 65 i-th cell pointe
1570f 72 20 2a 2f 0a 20 20 20 20 70 41 64 64 72 20 3d r */. pAddr =
15710 20 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 &data[cellOffse
15711 74 20 2b 20 69 2a 32 5d 3b 0a 20 20 20 20 70 63 t + i*2];. pc
15712 20 3d 20 67 65 74 32 62 79 74 65 28 70 41 64 64 = get2byte(pAdd
15713 72 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 r);. testcase
15714 28 20 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74 ( pc==iCellFirst
15715 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 );. testcase
15716 28 20 70 63 3d 3d 69 43 65 6c 6c 4c 61 73 74 20 ( pc==iCellLast
15717 29 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 );.#if !defined(
15718 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 SQLITE_ENABLE_OV
15719 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 ERSIZE_CELL_CHEC
1571a 4b 29 0a 20 20 20 20 2f 2a 20 54 68 65 73 65 20 K). /* These
1571b 63 6f 6e 64 69 74 69 6f 6e 73 20 68 61 76 65 20 conditions have
1571c 61 6c 72 65 61 64 79 20 62 65 65 6e 20 76 65 72 already been ver
1571d 69 66 69 65 64 20 69 6e 20 62 74 72 65 65 49 6e ified in btreeIn
1571e 69 74 50 61 67 65 28 29 0a 20 20 20 20 2a 2a 20 itPage(). **
1571f 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 if SQLITE_ENABLE
15720 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 _OVERSIZE_CELL_C
15721 48 45 43 4b 20 69 73 20 64 65 66 69 6e 65 64 20 HECK is defined
15722 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
15723 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c pc<iCellFirst ||
15724 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b pc>iCellLast ){
15725 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
15726 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
15727 54 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a T;. }.#endif.
15728 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d assert( pc>=
15729 69 43 65 6c 6c 46 69 72 73 74 20 26 26 20 70 63 iCellFirst && pc
1572a 3c 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 <=iCellLast );.
1572b 20 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69 size = cellSi
1572c 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 74 65 zePtr(pPage, &te
1572d 6d 70 5b 70 63 5d 29 3b 0a 20 20 20 20 63 62 72 mp[pc]);. cbr
1572e 6b 20 2d 3d 20 73 69 7a 65 3b 0a 23 69 66 20 64 k -= size;.#if d
1572f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e efined(SQLITE_EN
15730 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 ABLE_OVERSIZE_CE
15731 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20 69 66 LL_CHECK). if
15732 28 20 63 62 72 6b 3c 69 43 65 6c 6c 46 69 72 73 ( cbrk<iCellFirs
15733 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 t ){. retur
15734 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
15735 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6c _BKPT;. }.#el
15736 73 65 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c se. if( cbrk<
15737 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 iCellFirst || pc
15738 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a 65 +size>usableSize
15739 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1573a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1573b 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 BKPT;. }.#end
1573c 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 if. assert( c
1573d 62 72 6b 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 brk+size<=usable
1573e 53 69 7a 65 20 26 26 20 63 62 72 6b 3e 3d 69 43 Size && cbrk>=iC
1573f 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 ellFirst );.
15740 74 65 73 74 63 61 73 65 28 20 63 62 72 6b 2b 73 testcase( cbrk+s
15741 69 7a 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20 ize==usableSize
15742 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
15743 20 70 63 2b 73 69 7a 65 3d 3d 75 73 61 62 6c 65 pc+size==usable
15744 53 69 7a 65 20 29 3b 0a 20 20 20 20 6d 65 6d 63 Size );. memc
15745 70 79 28 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 py(&data[cbrk],
15746 26 74 65 6d 70 5b 70 63 5d 2c 20 73 69 7a 65 29 &temp[pc], size)
15747 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 70 ;. put2byte(p
15748 41 64 64 72 2c 20 63 62 72 6b 29 3b 0a 20 20 7d Addr, cbrk);. }
15749 0a 20 20 61 73 73 65 72 74 28 20 63 62 72 6b 3e . assert( cbrk>
1574a 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 =iCellFirst );.
1574b 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
1574c 68 64 72 2b 35 5d 2c 20 63 62 72 6b 29 3b 0a 20 hdr+5], cbrk);.
1574d 20 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d 20 30 data[hdr+1] = 0
1574e 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 32 5d 20 ;. data[hdr+2]
1574f 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b = 0;. data[hdr+
15750 37 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65 74 7] = 0;. memset
15751 28 26 64 61 74 61 5b 69 43 65 6c 6c 46 69 72 73 (&data[iCellFirs
15752 74 5d 2c 20 30 2c 20 63 62 72 6b 2d 69 43 65 6c t], 0, cbrk-iCel
15753 6c 46 69 72 73 74 29 3b 0a 20 20 61 73 73 65 72 lFirst);. asser
15754 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
15755 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
15756 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
15757 69 66 28 20 63 62 72 6b 2d 69 43 65 6c 6c 46 69 if( cbrk-iCellFi
15758 72 73 74 21 3d 70 50 61 67 65 2d 3e 6e 46 72 65 rst!=pPage->nFre
15759 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
1575a 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
1575b 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 KPT;. }. retur
1575c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1575d 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e /*.** Allocate n
1575e 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 73 70 Byte bytes of sp
1575f 61 63 65 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 ace from within
15760 74 68 65 20 42 2d 54 72 65 65 20 70 61 67 65 20 the B-Tree page
15761 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 passed.** as the
15762 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
15763 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 49 64 Write into *pId
15764 78 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f x the index into
15765 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 0a pPage->aData[].
15766 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 ** of the first
15767 62 79 74 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 byte of allocate
15768 64 20 73 70 61 63 65 2e 20 52 65 74 75 72 6e 20 d space. Return
15769 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b either SQLITE_OK
1576a 20 6f 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 or.** an error
1576b 63 6f 64 65 20 28 75 73 75 61 6c 6c 79 20 53 51 code (usually SQ
1576c 4c 49 54 45 5f 43 4f 52 52 55 50 54 29 2e 0a 2a LITE_CORRUPT)..*
1576d 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 *.** The caller
1576e 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 guarantees that
1576f 74 68 65 72 65 20 69 73 20 73 75 66 66 69 63 69 there is suffici
15770 65 6e 74 20 73 70 61 63 65 20 74 6f 20 6d 61 6b ent space to mak
15771 65 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 e the.** allocat
15772 69 6f 6e 2e 20 20 54 68 69 73 20 72 6f 75 74 69 ion. This routi
15773 6e 65 20 6d 69 67 68 74 20 6e 65 65 64 20 74 6f ne might need to
15774 20 64 65 66 72 61 67 6d 65 6e 74 20 69 6e 20 6f defragment in o
15775 72 64 65 72 20 74 6f 20 62 72 69 6e 67 0a 2a 2a rder to bring.**
15776 20 61 6c 6c 20 74 68 65 20 73 70 61 63 65 20 74 all the space t
15777 6f 67 65 74 68 65 72 2c 20 68 6f 77 65 76 65 72 ogether, however
15778 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
15779 77 69 6c 6c 20 61 76 6f 69 64 20 75 73 69 6e 67 will avoid using
1577a 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 77 .** the first tw
1577b 6f 20 62 79 74 65 73 20 70 61 73 74 20 74 68 65 o bytes past the
1577c 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 cell pointer ar
1577d 65 61 20 73 69 6e 63 65 20 70 72 65 73 75 6d 61 ea since presuma
1577e 62 6c 79 20 74 68 69 73 0a 2a 2a 20 61 6c 6c 6f bly this.** allo
1577f 63 61 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 cation is being
15780 6d 61 64 65 20 69 6e 20 6f 72 64 65 72 20 74 6f made in order to
15781 20 69 6e 73 65 72 74 20 61 20 6e 65 77 20 63 65 insert a new ce
15782 6c 6c 2c 20 73 6f 20 77 65 20 77 69 6c 6c 0a 2a ll, so we will.*
15783 2a 20 61 6c 73 6f 20 65 6e 64 20 75 70 20 6e 65 * also end up ne
15784 65 64 69 6e 67 20 61 20 6e 65 77 20 63 65 6c 6c eding a new cell
15785 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 pointer..*/.sta
15786 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 tic int allocate
15787 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70 Space(MemPage *p
15788 50 61 67 65 2c 20 69 6e 74 20 6e 42 79 74 65 2c Page, int nByte,
15789 20 69 6e 74 20 2a 70 49 64 78 29 7b 0a 20 20 63 int *pIdx){. c
1578a 6f 6e 73 74 20 69 6e 74 20 68 64 72 20 3d 20 70 onst int hdr = p
1578b 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b Page->hdrOffset;
1578c 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 /* Local cac
1578d 68 65 20 6f 66 20 70 50 61 67 65 2d 3e 68 64 72 he of pPage->hdr
1578e 4f 66 66 73 65 74 20 2a 2f 0a 20 20 75 38 20 2a Offset */. u8 *
1578f 20 63 6f 6e 73 74 20 64 61 74 61 20 3d 20 70 50 const data = pP
15790 61 67 65 2d 3e 61 44 61 74 61 3b 20 20 20 20 20 age->aData;
15791 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 /* Local cache
15792 6f 66 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 of pPage->aData
15793 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 61 67 3b 20 */. int nFrag;
15794 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15795 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
15796 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 65 ber of fragmente
15797 64 20 62 79 74 65 73 20 6f 6e 20 70 50 61 67 65 d bytes on pPage
15798 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20 20 */. int top;
15799 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1579a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
1579b 72 73 74 20 62 79 74 65 20 6f 66 20 63 65 6c 6c rst byte of cell
1579c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f content area */
1579d 0a 20 20 69 6e 74 20 67 61 70 3b 20 20 20 20 20 . int gap;
1579e 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 /* First byte
1579f 20 6f 66 20 67 61 70 20 62 65 74 77 65 65 6e 20 of gap between
157a0 63 65 6c 6c 20 70 6f 69 6e 74 65 72 73 20 61 6e cell pointers an
157a1 64 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a d cell content *
157a2 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
157a3 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 72 /* Integer r
157a4 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
157a5 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
157a6 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
157a7 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
157a8 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 e) );. assert(
157a9 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 pPage->pBt );.
157aa 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
157ab 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
157ac 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
157ad 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 . assert( nByte
157ae 3e 3d 30 20 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d >=0 ); /* Minim
157af 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 um cell size is
157b0 34 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4 */. assert( p
157b1 50 61 67 65 2d 3e 6e 46 72 65 65 3e 3d 6e 42 79 Page->nFree>=nBy
157b2 74 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 te );. assert(
157b3 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
157b4 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
157b5 20 6e 42 79 74 65 3c 70 50 61 67 65 2d 3e 70 42 nByte<pPage->pB
157b6 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 38 20 t->usableSize-8
157b7 29 3b 0a 0a 20 20 6e 46 72 61 67 20 3d 20 64 61 );.. nFrag = da
157b8 74 61 5b 68 64 72 2b 37 5d 3b 0a 20 20 61 73 73 ta[hdr+7];. ass
157b9 65 72 74 28 20 70 50 61 67 65 2d 3e 63 65 6c 6c ert( pPage->cell
157ba 4f 66 66 73 65 74 20 3d 3d 20 68 64 72 20 2b 20 Offset == hdr +
157bb 31 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 12 - 4*pPage->le
157bc 61 66 20 29 3b 0a 20 20 67 61 70 20 3d 20 70 50 af );. gap = pP
157bd 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 age->cellOffset
157be 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c + 2*pPage->nCell
157bf 3b 0a 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 ;. top = get2by
157c0 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 te(&data[hdr+5])
157c1 3b 0a 20 20 69 66 28 20 67 61 70 3e 74 6f 70 20 ;. if( gap>top
157c2 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
157c3 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
157c4 74 65 73 74 63 61 73 65 28 20 67 61 70 2b 32 3d testcase( gap+2=
157c5 3d 74 6f 70 20 29 3b 0a 20 20 74 65 73 74 63 61 =top );. testca
157c6 73 65 28 20 67 61 70 2b 31 3d 3d 74 6f 70 20 29 se( gap+1==top )
157c7 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61 ;. testcase( ga
157c8 70 3d 3d 74 6f 70 20 29 3b 0a 0a 20 20 69 66 28 p==top );.. if(
157c9 20 6e 46 72 61 67 3e 3d 36 30 20 29 7b 0a 20 20 nFrag>=60 ){.
157ca 20 20 2f 2a 20 41 6c 77 61 79 73 20 64 65 66 72 /* Always defr
157cb 61 67 6d 65 6e 74 20 68 69 67 68 6c 79 20 66 72 agment highly fr
157cc 61 67 6d 65 6e 74 65 64 20 70 61 67 65 73 20 2a agmented pages *
157cd 2f 0a 20 20 20 20 72 63 20 3d 20 64 65 66 72 61 /. rc = defra
157ce 67 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 gmentPage(pPage)
157cf 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 ;. if( rc ) r
157d0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f eturn rc;. to
157d1 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 p = get2byte(&da
157d2 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 7d 65 ta[hdr+5]);. }e
157d3 6c 73 65 20 69 66 28 20 67 61 70 2b 32 3c 3d 74 lse if( gap+2<=t
157d4 6f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 61 op ){. /* Sea
157d5 72 63 68 20 74 68 65 20 66 72 65 65 6c 69 73 74 rch the freelist
157d6 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 66 looking for a f
157d7 72 65 65 20 73 6c 6f 74 20 62 69 67 20 65 6e 6f ree slot big eno
157d8 75 67 68 20 74 6f 20 73 61 74 69 73 66 79 20 0a ugh to satisfy .
157d9 20 20 20 20 2a 2a 20 74 68 65 20 72 65 71 75 65 ** the reque
157da 73 74 2e 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 st. The allocati
157db 6f 6e 20 69 73 20 6d 61 64 65 20 66 72 6f 6d 20 on is made from
157dc 74 68 65 20 66 69 72 73 74 20 66 72 65 65 20 73 the first free s
157dd 6c 6f 74 20 69 6e 20 0a 20 20 20 20 2a 2a 20 74 lot in . ** t
157de 68 65 20 6c 69 73 74 20 74 68 61 74 20 69 73 20 he list that is
157df 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 large enough to
157e0 61 63 63 6f 6d 61 64 61 74 65 20 69 74 2e 0a 20 accomadate it..
157e1 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 70 63 */. int pc
157e2 2c 20 61 64 64 72 3b 0a 20 20 20 20 66 6f 72 28 , addr;. for(
157e3 61 64 64 72 3d 68 64 72 2b 31 3b 20 28 70 63 20 addr=hdr+1; (pc
157e4 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
157e5 5b 61 64 64 72 5d 29 29 3e 30 3b 20 61 64 64 72 [addr]))>0; addr
157e6 3d 70 63 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 =pc){. int
157e7 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 size = get2byte(
157e8 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 20 20 20 &data[pc+2]);
157e9 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 66 72 65 /* Size of fre
157ea 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 20 20 20 20 e slot */.
157eb 69 66 28 20 73 69 7a 65 3e 3d 6e 42 79 74 65 20 if( size>=nByte
157ec 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 ){. int x
157ed 20 3d 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 3b = size - nByte;
157ee 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
157ef 65 28 20 78 3d 3d 34 20 29 3b 0a 20 20 20 20 20 e( x==4 );.
157f0 20 20 20 74 65 73 74 63 61 73 65 28 20 78 3d 3d testcase( x==
157f1 33 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 3 );. if(
157f2 20 78 3c 34 20 29 7b 0a 20 20 20 20 20 20 20 20 x<4 ){.
157f3 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 /* Remove the
157f4 73 6c 6f 74 20 66 72 6f 6d 20 74 68 65 20 66 72 slot from the fr
157f5 65 65 2d 6c 69 73 74 2e 20 55 70 64 61 74 65 20 ee-list. Update
157f6 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 the number of.
157f7 20 20 20 20 20 20 20 20 2a 2a 20 66 72 61 67 6d ** fragm
157f8 65 6e 74 65 64 20 62 79 74 65 73 20 77 69 74 68 ented bytes with
157f9 69 6e 20 74 68 65 20 70 61 67 65 2e 20 2a 2f 0a in the page. */.
157fa 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 memcpy
157fb 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 26 64 (&data[addr], &d
157fc 61 74 61 5b 70 63 5d 2c 20 32 29 3b 0a 20 20 20 ata[pc], 2);.
157fd 20 20 20 20 20 20 20 64 61 74 61 5b 68 64 72 2b data[hdr+
157fe 37 5d 20 3d 20 28 75 38 29 28 6e 46 72 61 67 20 7] = (u8)(nFrag
157ff 2b 20 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 + x);. }e
15800 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f lse{. /
15801 2a 20 54 68 65 20 73 6c 6f 74 20 72 65 6d 61 69 * The slot remai
15802 6e 73 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c ns on the free-l
15803 69 73 74 2e 20 52 65 64 75 63 65 20 69 74 73 20 ist. Reduce its
15804 73 69 7a 65 20 74 6f 20 61 63 63 6f 75 6e 74 0a size to account.
15805 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 ** for
15806 20 74 68 65 20 70 6f 72 74 69 6f 6e 20 75 73 65 the portion use
15807 64 20 62 79 20 74 68 65 20 6e 65 77 20 61 6c 6c d by the new all
15808 6f 63 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 ocation. */.
15809 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26 put2byte(&
1580a 64 61 74 61 5b 70 63 2b 32 5d 2c 20 78 29 3b 0a data[pc+2], x);.
1580b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1580c 20 20 2a 70 49 64 78 20 3d 20 70 63 20 2b 20 78 *pIdx = pc + x
1580d 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
1580e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
1580f 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 }. }. }..
15810 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b /* Check to mak
15811 65 20 73 75 72 65 20 74 68 65 72 65 20 69 73 20 e sure there is
15812 65 6e 6f 75 67 68 20 73 70 61 63 65 20 69 6e 20 enough space in
15813 74 68 65 20 67 61 70 20 74 6f 20 73 61 74 69 73 the gap to satis
15814 66 79 0a 20 20 2a 2a 20 74 68 65 20 61 6c 6c 6f fy. ** the allo
15815 63 61 74 69 6f 6e 2e 20 20 49 66 20 6e 6f 74 2c cation. If not,
15816 20 64 65 66 72 61 67 6d 65 6e 74 2e 0a 20 20 2a defragment.. *
15817 2f 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61 /. testcase( ga
15818 70 2b 32 2b 6e 42 79 74 65 3d 3d 74 6f 70 20 29 p+2+nByte==top )
15819 3b 0a 20 20 69 66 28 20 67 61 70 2b 32 2b 6e 42 ;. if( gap+2+nB
1581a 79 74 65 3e 74 6f 70 20 29 7b 0a 20 20 20 20 72 yte>top ){. r
1581b 63 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61 c = defragmentPa
1581c 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69 ge(pPage);. i
1581d 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
1581e 63 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 c;. top = get
1581f 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
15820 35 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 5]);. assert(
15821 20 67 61 70 2b 6e 42 79 74 65 3c 3d 74 6f 70 20 gap+nByte<=top
15822 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 6c );. }... /* Al
15823 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 72 locate memory fr
15824 6f 6d 20 74 68 65 20 67 61 70 20 69 6e 20 62 65 om the gap in be
15825 74 77 65 65 6e 20 74 68 65 20 63 65 6c 6c 20 70 tween the cell p
15826 6f 69 6e 74 65 72 20 61 72 72 61 79 0a 20 20 2a ointer array. *
15827 2a 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 * and the cell c
15828 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 54 68 ontent area. Th
15829 65 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 e btreeInitPage(
1582a 29 20 63 61 6c 6c 20 68 61 73 20 61 6c 72 65 61 ) call has alrea
1582b 64 79 0a 20 20 2a 2a 20 76 61 6c 69 64 61 74 65 dy. ** validate
1582c 64 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 d the freelist.
1582d 20 47 69 76 65 6e 20 74 68 61 74 20 74 68 65 20 Given that the
1582e 66 72 65 65 6c 69 73 74 20 69 73 20 76 61 6c 69 freelist is vali
1582f 64 2c 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 d, there. ** is
15830 20 6e 6f 20 77 61 79 20 74 68 61 74 20 74 68 65 no way that the
15831 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 61 6e 20 allocation can
15832 65 78 74 65 6e 64 20 6f 66 66 20 74 68 65 20 65 extend off the e
15833 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a nd of the page..
15834 20 20 2a 2a 20 54 68 65 20 61 73 73 65 72 74 28 ** The assert(
15835 29 20 62 65 6c 6f 77 20 76 65 72 69 66 69 65 73 ) below verifies
15836 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 the previous se
15837 6e 74 65 6e 63 65 2e 0a 20 20 2a 2f 0a 20 20 74 ntence.. */. t
15838 6f 70 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 70 op -= nByte;. p
15839 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 ut2byte(&data[hd
1583a 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a 20 20 61 73 r+5], top);. as
1583b 73 65 72 74 28 20 74 6f 70 2b 6e 42 79 74 65 20 sert( top+nByte
1583c 3c 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 <= pPage->pBt->u
1583d 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 2a sableSize );. *
1583e 70 49 64 78 20 3d 20 74 6f 70 3b 0a 20 20 72 65 pIdx = top;. re
1583f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
15840 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
15841 61 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 a section of the
15842 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 74 6f pPage->aData to
15843 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a the freelist..*
15844 2a 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 * The first byte
15845 20 6f 66 20 74 68 65 20 6e 65 77 20 66 72 65 65 of the new free
15846 20 62 6c 6f 63 6b 20 69 73 20 70 50 61 67 65 2d block is pPage-
15847 3e 61 44 69 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a >aDisk[start].**
15848 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 and the size of
15849 20 74 68 65 20 62 6c 6f 63 6b 20 69 73 20 22 73 the block is "s
1584a 69 7a 65 22 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a ize" bytes..**.*
1584b 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 65 66 * Most of the ef
1584c 66 6f 72 74 20 68 65 72 65 20 69 73 20 69 6e 76 fort here is inv
1584d 6f 6c 76 65 64 20 69 6e 20 63 6f 61 6c 65 73 69 olved in coalesi
1584e 6e 67 20 61 64 6a 61 63 65 6e 74 0a 2a 2a 20 66 ng adjacent.** f
1584f 72 65 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20 ree blocks into
15850 61 20 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65 a single big fre
15851 65 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 e block..*/.stat
15852 69 63 20 69 6e 74 20 66 72 65 65 53 70 61 63 65 ic int freeSpace
15853 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
15854 20 69 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20 int start, int
15855 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 size){. int add
15856 72 2c 20 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a r, pbegin, hdr;.
15857 20 20 69 6e 74 20 69 4c 61 73 74 3b 20 20 20 20 int iLast;
15858 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15859 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 /* Largest p
1585a 6f 73 73 69 62 6c 65 20 66 72 65 65 62 6c 6f 63 ossible freebloc
1585b 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 75 6e k offset */. un
1585c 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 signed char *dat
1585d 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 a = pPage->aData
1585e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 ;.. assert( pPa
1585f 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 ge->pBt!=0 );.
15860 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
15861 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
15862 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
15863 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 74 61 );. assert( sta
15864 72 74 3e 3d 70 50 61 67 65 2d 3e 68 64 72 4f 66 rt>=pPage->hdrOf
15865 66 73 65 74 2b 36 2b 70 50 61 67 65 2d 3e 63 68 fset+6+pPage->ch
15866 69 6c 64 50 74 72 53 69 7a 65 20 29 3b 0a 20 20 ildPtrSize );.
15867 61 73 73 65 72 74 28 20 28 73 74 61 72 74 20 2b assert( (start +
15868 20 73 69 7a 65 29 3c 3d 70 50 61 67 65 2d 3e 70 size)<=pPage->p
15869 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 Bt->usableSize )
1586a 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
1586b 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1586c 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
1586d 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
1586e 69 7a 65 3e 3d 30 20 29 3b 20 20 20 2f 2a 20 4d ize>=0 ); /* M
1586f 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 inimum cell size
15870 20 69 73 20 34 20 2a 2f 0a 0a 23 69 66 64 65 66 is 4 */..#ifdef
15871 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 SQLITE_SECURE_D
15872 45 4c 45 54 45 0a 20 20 2f 2a 20 4f 76 65 72 77 ELETE. /* Overw
15873 72 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 rite deleted inf
15874 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 ormation with ze
15875 72 6f 73 20 77 68 65 6e 20 74 68 65 20 53 45 43 ros when the SEC
15876 55 52 45 5f 44 45 4c 45 54 45 20 0a 20 20 2a 2a URE_DELETE . **
15877 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c option is enabl
15878 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 ed at compile-ti
15879 6d 65 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 me */. memset(&
1587a 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 30 2c 20 data[start], 0,
1587b 73 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 size);.#endif..
1587c 20 2f 2a 20 41 64 64 20 74 68 65 20 73 70 61 63 /* Add the spac
1587d 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 e back into the
1587e 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 66 linked list of f
1587f 72 65 65 62 6c 6f 63 6b 73 2e 20 20 4e 6f 74 65 reeblocks. Note
15880 20 74 68 61 74 0a 20 20 2a 2a 20 65 76 65 6e 20 that. ** even
15881 74 68 6f 75 67 68 20 74 68 65 20 66 72 65 65 62 though the freeb
15882 6c 6f 63 6b 20 6c 69 73 74 20 77 61 73 20 63 68 lock list was ch
15883 65 63 6b 65 64 20 62 79 20 62 74 72 65 65 49 6e ecked by btreeIn
15884 69 74 50 61 67 65 28 29 2c 0a 20 20 2a 2a 20 62 itPage(),. ** b
15885 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 64 treeInitPage() d
15886 69 64 20 6e 6f 74 20 64 65 74 65 63 74 20 6f 76 id not detect ov
15887 65 72 6c 61 70 70 69 6e 67 20 63 65 6c 6c 73 20 erlapping cells
15888 6f 72 0a 20 20 2a 2a 20 66 72 65 65 62 6c 6f 63 or. ** freebloc
15889 6b 73 20 74 68 61 74 20 6f 76 65 72 6c 61 70 70 ks that overlapp
1588a 65 64 20 63 65 6c 6c 73 2e 20 20 20 4e 6f 72 20 ed cells. Nor
1588b 64 6f 65 73 20 69 74 20 64 65 74 65 63 74 20 77 does it detect w
1588c 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c hen the. ** cel
1588d 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 65 l content area e
1588e 78 63 65 65 64 73 20 74 68 65 20 76 61 6c 75 65 xceeds the value
1588f 20 69 6e 20 74 68 65 20 70 61 67 65 20 68 65 61 in the page hea
15890 64 65 72 2e 20 20 49 66 20 74 68 65 73 65 0a 20 der. If these.
15891 20 2a 2a 20 73 69 74 75 61 74 69 6f 6e 73 20 61 ** situations a
15892 72 69 73 65 2c 20 74 68 65 6e 20 73 75 62 73 65 rise, then subse
15893 71 75 65 6e 74 20 69 6e 73 65 72 74 20 6f 70 65 quent insert ope
15894 72 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 63 6f rations might co
15895 72 72 75 70 74 0a 20 20 2a 2a 20 74 68 65 20 66 rrupt. ** the f
15896 72 65 65 6c 69 73 74 2e 20 20 53 6f 20 77 65 20 reelist. So we
15897 64 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b do need to check
15898 20 66 6f 72 20 63 6f 72 72 75 70 74 69 6f 6e 20 for corruption
15899 77 68 69 6c 65 20 73 63 61 6e 6e 69 6e 67 0a 20 while scanning.
1589a 20 2a 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74 ** the freelist
1589b 2e 0a 20 20 2a 2f 0a 20 20 68 64 72 20 3d 20 70 .. */. hdr = p
1589c 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b Page->hdrOffset;
1589d 0a 20 20 61 64 64 72 20 3d 20 68 64 72 20 2b 20 . addr = hdr +
1589e 31 3b 0a 20 20 69 4c 61 73 74 20 3d 20 70 50 61 1;. iLast = pPa
1589f 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 ge->pBt->usableS
158a0 69 7a 65 20 2d 20 34 3b 0a 20 20 61 73 73 65 72 ize - 4;. asser
158a1 74 28 20 73 74 61 72 74 3c 3d 69 4c 61 73 74 20 t( start<=iLast
158a2 29 3b 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 );. while( (pbe
158a3 67 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 gin = get2byte(&
158a4 64 61 74 61 5b 61 64 64 72 5d 29 29 3c 73 74 61 data[addr]))<sta
158a5 72 74 20 26 26 20 70 62 65 67 69 6e 3e 30 20 29 rt && pbegin>0 )
158a6 7b 0a 20 20 20 20 69 66 28 20 70 62 65 67 69 6e {. if( pbegin
158a7 3c 61 64 64 72 2b 34 20 29 7b 0a 20 20 20 20 20 <addr+4 ){.
158a8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
158a9 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
158aa 20 7d 0a 20 20 20 20 61 64 64 72 20 3d 20 70 62 }. addr = pb
158ab 65 67 69 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 egin;. }. if(
158ac 70 62 65 67 69 6e 3e 69 4c 61 73 74 20 29 7b 0a pbegin>iLast ){.
158ad 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
158ae 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
158af 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 62 }. assert( pb
158b0 65 67 69 6e 3e 61 64 64 72 20 7c 7c 20 70 62 65 egin>addr || pbe
158b1 67 69 6e 3d 3d 30 20 29 3b 0a 20 20 70 75 74 32 gin==0 );. put2
158b2 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d byte(&data[addr]
158b3 2c 20 73 74 61 72 74 29 3b 0a 20 20 70 75 74 32 , start);. put2
158b4 62 79 74 65 28 26 64 61 74 61 5b 73 74 61 72 74 byte(&data[start
158b5 5d 2c 20 70 62 65 67 69 6e 29 3b 0a 20 20 70 75 ], pbegin);. pu
158b6 74 32 62 79 74 65 28 26 64 61 74 61 5b 73 74 61 t2byte(&data[sta
158b7 72 74 2b 32 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 rt+2], size);.
158b8 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 70 pPage->nFree = p
158b9 50 61 67 65 2d 3e 6e 46 72 65 65 20 2b 20 28 75 Page->nFree + (u
158ba 31 36 29 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43 16)size;.. /* C
158bb 6f 61 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74 oalesce adjacent
158bc 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a free blocks */.
158bd 20 20 61 64 64 72 20 3d 20 68 64 72 20 2b 20 31 addr = hdr + 1
158be 3b 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 ;. while( (pbeg
158bf 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 in = get2byte(&d
158c0 61 74 61 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b ata[addr]))>0 ){
158c1 0a 20 20 20 20 69 6e 74 20 70 6e 65 78 74 2c 20 . int pnext,
158c2 70 73 69 7a 65 2c 20 78 3b 0a 20 20 20 20 61 73 psize, x;. as
158c3 73 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64 sert( pbegin>add
158c4 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 r );. assert(
158c5 20 70 62 65 67 69 6e 3c 3d 70 50 61 67 65 2d 3e pbegin<=pPage->
158c6 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d pBt->usableSize-
158c7 34 20 29 3b 0a 20 20 20 20 70 6e 65 78 74 20 3d 4 );. pnext =
158c8 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
158c9 70 62 65 67 69 6e 5d 29 3b 0a 20 20 20 20 70 73 pbegin]);. ps
158ca 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 ize = get2byte(&
158cb 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b data[pbegin+2]);
158cc 0a 20 20 20 20 69 66 28 20 70 62 65 67 69 6e 20 . if( pbegin
158cd 2b 20 70 73 69 7a 65 20 2b 20 33 20 3e 3d 20 70 + psize + 3 >= p
158ce 6e 65 78 74 20 26 26 20 70 6e 65 78 74 3e 30 20 next && pnext>0
158cf 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 66 72 61 ){. int fra
158d0 67 20 3d 20 70 6e 65 78 74 20 2d 20 28 70 62 65 g = pnext - (pbe
158d1 67 69 6e 2b 70 73 69 7a 65 29 3b 0a 20 20 20 20 gin+psize);.
158d2 20 20 69 66 28 20 28 66 72 61 67 3c 30 29 20 7c if( (frag<0) |
158d3 7c 20 28 66 72 61 67 3e 28 69 6e 74 29 64 61 74 | (frag>(int)dat
158d4 61 5b 68 64 72 2b 37 5d 29 20 29 7b 0a 20 20 20 a[hdr+7]) ){.
158d5 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
158d6 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
158d7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 . }. d
158d8 61 74 61 5b 68 64 72 2b 37 5d 20 2d 3d 20 28 75 ata[hdr+7] -= (u
158d9 38 29 66 72 61 67 3b 0a 20 20 20 20 20 20 78 20 8)frag;. x
158da 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
158db 5b 70 6e 65 78 74 5d 29 3b 0a 20 20 20 20 20 20 [pnext]);.
158dc 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 put2byte(&data[p
158dd 62 65 67 69 6e 5d 2c 20 78 29 3b 0a 20 20 20 20 begin], x);.
158de 20 20 78 20 3d 20 70 6e 65 78 74 20 2b 20 67 65 x = pnext + ge
158df 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 6e 65 t2byte(&data[pne
158e0 78 74 2b 32 5d 29 20 2d 20 70 62 65 67 69 6e 3b xt+2]) - pbegin;
158e1 0a 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28 . put2byte(
158e2 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d 2c &data[pbegin+2],
158e3 20 78 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a x);. }else{.
158e4 20 20 20 20 20 20 61 64 64 72 20 3d 20 70 62 65 addr = pbe
158e5 67 69 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a gin;. }. }..
158e6 20 20 2f 2a 20 49 66 20 74 68 65 20 63 65 6c 6c /* If the cell
158e7 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 62 65 content area be
158e8 67 69 6e 73 20 77 69 74 68 20 61 20 66 72 65 65 gins with a free
158e9 62 6c 6f 63 6b 2c 20 72 65 6d 6f 76 65 20 69 74 block, remove it
158ea 2e 20 2a 2f 0a 20 20 69 66 28 20 64 61 74 61 5b . */. if( data[
158eb 68 64 72 2b 31 5d 3d 3d 64 61 74 61 5b 68 64 72 hdr+1]==data[hdr
158ec 2b 35 5d 20 26 26 20 64 61 74 61 5b 68 64 72 2b +5] && data[hdr+
158ed 32 5d 3d 3d 64 61 74 61 5b 68 64 72 2b 36 5d 20 2]==data[hdr+6]
158ee 29 7b 0a 20 20 20 20 69 6e 74 20 74 6f 70 3b 0a ){. int top;.
158ef 20 20 20 20 70 62 65 67 69 6e 20 3d 20 67 65 74 pbegin = get
158f0 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
158f1 31 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 1]);. memcpy(
158f2 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 26 64 &data[hdr+1], &d
158f3 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 32 29 3b ata[pbegin], 2);
158f4 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 . top = get2b
158f5 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d yte(&data[hdr+5]
158f6 29 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 ) + get2byte(&da
158f7 74 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 ta[pbegin+2]);.
158f8 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 put2byte(&dat
158f9 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a a[hdr+5], top);.
158fa 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71 }. assert( sq
158fb 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
158fc 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
158fd 50 61 67 65 29 20 29 3b 0a 20 20 72 65 74 75 72 Page) );. retur
158fe 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
158ff 2f 2a 0a 2a 2a 20 44 65 63 6f 64 65 20 74 68 65 /*.** Decode the
15900 20 66 6c 61 67 73 20 62 79 74 65 20 28 74 68 65 flags byte (the
15901 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 first byte of t
15902 68 65 20 68 65 61 64 65 72 29 20 66 6f 72 20 61 he header) for a
15903 20 70 61 67 65 0a 2a 2a 20 61 6e 64 20 69 6e 69 page.** and ini
15904 74 69 61 6c 69 7a 65 20 66 69 65 6c 64 73 20 6f tialize fields o
15905 66 20 74 68 65 20 4d 65 6d 50 61 67 65 20 73 74 f the MemPage st
15906 72 75 63 74 75 72 65 20 61 63 63 6f 72 64 69 6e ructure accordin
15907 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 gly..**.** Only
15908 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f the following co
15909 6d 62 69 6e 61 74 69 6f 6e 73 20 61 72 65 20 73 mbinations are s
1590a 75 70 70 6f 72 74 65 64 2e 20 20 41 6e 79 74 68 upported. Anyth
1590b 69 6e 67 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a ing different.**
1590c 20 69 6e 64 69 63 61 74 65 73 20 61 20 63 6f 72 indicates a cor
1590d 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 rupt database fi
1590e 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 les:.**.**
1590f 20 20 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 0a PTF_ZERODATA.
15910 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a ** PTF_Z
15911 45 52 4f 44 41 54 41 20 7c 20 50 54 46 5f 4c 45 ERODATA | PTF_LE
15912 41 46 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 AF.** PT
15913 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 F_LEAFDATA | PTF
15914 5f 49 4e 54 4b 45 59 0a 2a 2a 20 20 20 20 20 20 _INTKEY.**
15915 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 PTF_LEAFDATA
15916 7c 20 50 54 46 5f 49 4e 54 4b 45 59 20 7c 20 50 | PTF_INTKEY | P
15917 54 46 5f 4c 45 41 46 0a 2a 2f 0a 73 74 61 74 69 TF_LEAF.*/.stati
15918 63 20 69 6e 74 20 64 65 63 6f 64 65 46 6c 61 67 c int decodeFlag
15919 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 s(MemPage *pPage
1591a 2c 20 69 6e 74 20 66 6c 61 67 42 79 74 65 29 7b , int flagByte){
1591b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
1591c 3b 20 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 ; /* A copy
1591d 6f 66 20 70 50 61 67 65 2d 3e 70 42 74 20 2a 2f of pPage->pBt */
1591e 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
1591f 65 2d 3e 68 64 72 4f 66 66 73 65 74 3d 3d 28 70 e->hdrOffset==(p
15920 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 Page->pgno==1 ?
15921 31 30 30 20 3a 20 30 29 20 29 3b 0a 20 20 61 73 100 : 0) );. as
15922 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
15923 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
15924 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
15925 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 3d 20 28 pPage->leaf = (
15926 75 38 29 28 66 6c 61 67 42 79 74 65 3e 3e 33 29 u8)(flagByte>>3)
15927 3b 20 20 61 73 73 65 72 74 28 20 50 54 46 5f 4c ; assert( PTF_L
15928 45 41 46 20 3d 3d 20 31 3c 3c 33 20 29 3b 0a 20 EAF == 1<<3 );.
15929 20 66 6c 61 67 42 79 74 65 20 26 3d 20 7e 50 54 flagByte &= ~PT
1592a 46 5f 4c 45 41 46 3b 0a 20 20 70 50 61 67 65 2d F_LEAF;. pPage-
1592b 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20 3d 20 >childPtrSize =
1592c 34 2d 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 4-4*pPage->leaf;
1592d 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e . pBt = pPage->
1592e 70 42 74 3b 0a 20 20 69 66 28 20 66 6c 61 67 42 pBt;. if( flagB
1592f 79 74 65 3d 3d 28 50 54 46 5f 4c 45 41 46 44 41 yte==(PTF_LEAFDA
15930 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 29 TA | PTF_INTKEY)
15931 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 ){. pPage->i
15932 6e 74 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 70 ntKey = 1;. p
15933 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 Page->hasData =
15934 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 pPage->leaf;.
15935 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c pPage->maxLocal
15936 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 3b = pBt->maxLeaf;
15937 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c . pPage->minL
15938 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c ocal = pBt->minL
15939 65 61 66 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 eaf;. }else if(
1593a 20 66 6c 61 67 42 79 74 65 3d 3d 50 54 46 5f 5a flagByte==PTF_Z
1593b 45 52 4f 44 41 54 41 20 29 7b 0a 20 20 20 20 70 ERODATA ){. p
1593c 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 30 Page->intKey = 0
1593d 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 ;. pPage->has
1593e 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 70 50 Data = 0;. pP
1593f 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 age->maxLocal =
15940 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 pBt->maxLocal;.
15941 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 pPage->minLoc
15942 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 al = pBt->minLoc
15943 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 al;. }else{.
15944 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
15945 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d ORRUPT_BKPT;. }
15946 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
15947 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e _OK;.}../*.** In
15948 69 74 69 61 6c 69 7a 65 20 74 68 65 20 61 75 78 itialize the aux
15949 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 iliary informati
1594a 6f 6e 20 66 6f 72 20 61 20 64 69 73 6b 20 62 6c on for a disk bl
1594b 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ock..**.** Retur
1594c 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 n SQLITE_OK on s
1594d 75 63 63 65 73 73 2e 20 20 49 66 20 77 65 20 73 uccess. If we s
1594e 65 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 ee that the page
1594f 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e does.** not con
15950 74 61 69 6e 20 61 20 77 65 6c 6c 2d 66 6f 72 6d tain a well-form
15951 65 64 20 64 61 74 61 62 61 73 65 20 70 61 67 65 ed database page
15952 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 0a 2a , then return .*
15953 2a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 * SQLITE_CORRUPT
15954 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 61 20 72 . Note that a r
15955 65 74 75 72 6e 20 6f 66 20 53 51 4c 49 54 45 5f eturn of SQLITE_
15956 4f 4b 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 67 OK does not.** g
15957 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 uarantee that th
15958 65 20 70 61 67 65 20 69 73 20 77 65 6c 6c 2d 66 e page is well-f
15959 6f 72 6d 65 64 2e 20 20 49 74 20 6f 6e 6c 79 20 ormed. It only
1595a 73 68 6f 77 73 20 74 68 61 74 0a 2a 2a 20 77 65 shows that.** we
1595b 20 66 61 69 6c 65 64 20 74 6f 20 64 65 74 65 63 failed to detec
1595c 74 20 61 6e 79 20 63 6f 72 72 75 70 74 69 6f 6e t any corruption
1595d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1595e 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 4d 65 btreeInitPage(Me
1595f 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 0a mPage *pPage){..
15960 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
15961 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 >pBt!=0 );. ass
15962 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
15963 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
15964 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
15965 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 assert( pPage->p
15966 67 6e 6f 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 gno==sqlite3Page
15967 72 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 rPagenumber(pPag
15968 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
15969 20 61 73 73 65 72 74 28 20 70 50 61 67 65 20 3d assert( pPage =
1596a 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
1596b 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 tExtra(pPage->pD
1596c 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 bPage) );. asse
1596d 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74 61 rt( pPage->aData
1596e 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 == sqlite3Pager
1596f 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 GetData(pPage->p
15970 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 69 66 DbPage) );.. if
15971 28 20 21 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 ( !pPage->isInit
15972 20 29 7b 0a 20 20 20 20 75 31 36 20 70 63 3b 20 ){. u16 pc;
15973 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 /* Ad
15974 64 72 65 73 73 20 6f 66 20 61 20 66 72 65 65 62 dress of a freeb
15975 6c 6f 63 6b 20 77 69 74 68 69 6e 20 70 50 61 67 lock within pPag
15976 65 2d 3e 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 20 e->aData[] */.
15977 20 20 75 38 20 68 64 72 3b 20 20 20 20 20 20 20 u8 hdr;
15978 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 /* Offset t
15979 6f 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 70 o beginning of p
1597a 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 age header */.
1597b 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 u8 *data;
1597c 20 20 20 20 20 2f 2a 20 45 71 75 61 6c 20 74 6f /* Equal to
1597d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f pPage->aData */
1597e 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 . BtShared *p
1597f 42 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 Bt; /* Th
15980 65 20 6d 61 69 6e 20 62 74 72 65 65 20 73 74 72 e main btree str
15981 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20 75 31 ucture */. u1
15982 36 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 6 usableSize;
15983 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 75 73 /* Amount of us
15984 61 62 6c 65 20 73 70 61 63 65 20 6f 6e 20 65 61 able space on ea
15985 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 75 ch page */. u
15986 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 16 cellOffset;
15987 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d /* Offset from
15988 20 73 74 61 72 74 20 6f 66 20 70 61 67 65 20 74 start of page t
15989 6f 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 o first cell poi
1598a 6e 74 65 72 20 2a 2f 0a 20 20 20 20 75 31 36 20 nter */. u16
1598b 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 2f nFree; /
1598c 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 6e 75 73 * Number of unus
1598d 65 64 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20 ed bytes on the
1598e 70 61 67 65 20 2a 2f 0a 20 20 20 20 75 31 36 20 page */. u16
1598f 74 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f top; /
15990 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 * First byte of
15991 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
15992 20 61 72 65 61 20 2a 2f 0a 20 20 20 20 69 6e 74 area */. int
15993 20 69 43 65 6c 6c 46 69 72 73 74 3b 20 20 20 20 iCellFirst;
15994 2f 2a 20 46 69 72 73 74 20 61 6c 6c 6f 77 61 62 /* First allowab
15995 6c 65 20 63 65 6c 6c 20 6f 72 20 66 72 65 65 62 le cell or freeb
15996 6c 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 20 lock offset */.
15997 20 20 20 69 6e 74 20 69 43 65 6c 6c 4c 61 73 74 int iCellLast
15998 3b 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f ; /* Last po
15999 73 73 69 62 6c 65 20 63 65 6c 6c 20 6f 72 20 66 ssible cell or f
1599a 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20 reeblock offset
1599b 2a 2f 0a 0a 20 20 20 20 70 42 74 20 3d 20 70 50 */.. pBt = pP
1599c 61 67 65 2d 3e 70 42 74 3b 0a 0a 20 20 20 20 68 age->pBt;.. h
1599d 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f dr = pPage->hdrO
1599e 66 66 73 65 74 3b 0a 20 20 20 20 64 61 74 61 20 ffset;. data
1599f 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a = pPage->aData;.
159a0 20 20 20 20 69 66 28 20 64 65 63 6f 64 65 46 6c if( decodeFl
159a1 61 67 73 28 70 50 61 67 65 2c 20 64 61 74 61 5b ags(pPage, data[
159a2 68 64 72 5d 29 20 29 20 72 65 74 75 72 6e 20 53 hdr]) ) return S
159a3 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
159a4 50 54 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 PT;. assert(
159a5 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 pBt->pageSize>=5
159a6 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 12 && pBt->pageS
159a7 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 ize<=32768 );.
159a8 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 pPage->maskPag
159a9 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a e = pBt->pageSiz
159aa 65 20 2d 20 31 3b 0a 20 20 20 20 70 50 61 67 65 e - 1;. pPage
159ab 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b ->nOverflow = 0;
159ac 0a 20 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20 . usableSize
159ad 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
159ae 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 63 65 e;. pPage->ce
159af 6c 6c 4f 66 66 73 65 74 20 3d 20 63 65 6c 6c 4f llOffset = cellO
159b0 66 66 73 65 74 20 3d 20 68 64 72 20 2b 20 31 32 ffset = hdr + 12
159b1 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 - 4*pPage->leaf
159b2 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 ;. top = get2
159b3 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
159b4 5d 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e ]);. pPage->n
159b5 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28 Cell = get2byte(
159b6 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 &data[hdr+3]);.
159b7 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 if( pPage->nC
159b8 65 6c 6c 3e 4d 58 5f 43 45 4c 4c 28 70 42 74 29 ell>MX_CELL(pBt)
159b9 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 6f 20 ){. /* To
159ba 6d 61 6e 79 20 63 65 6c 6c 73 20 66 6f 72 20 61 many cells for a
159bb 20 73 69 6e 67 6c 65 20 70 61 67 65 2e 20 20 54 single page. T
159bc 68 65 20 70 61 67 65 20 6d 75 73 74 20 62 65 20 he page must be
159bd 63 6f 72 72 75 70 74 20 2a 2f 0a 20 20 20 20 20 corrupt */.
159be 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
159bf 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
159c0 20 7d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 }. testcase(
159c1 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 4d pPage->nCell==M
159c2 58 5f 43 45 4c 4c 28 70 42 74 29 20 29 3b 0a 0a X_CELL(pBt) );..
159c3 20 20 20 20 2f 2a 20 41 20 6d 61 6c 66 6f 72 6d /* A malform
159c4 65 64 20 64 61 74 61 62 61 73 65 20 70 61 67 65 ed database page
159c5 20 6d 69 67 68 74 20 63 61 75 73 65 20 75 73 20 might cause us
159c6 74 6f 20 72 65 61 64 20 70 61 73 74 20 74 68 65 to read past the
159c7 20 65 6e 64 0a 20 20 20 20 2a 2a 20 6f 66 20 70 end. ** of p
159c8 61 67 65 20 77 68 65 6e 20 70 61 72 73 69 6e 67 age when parsing
159c9 20 61 20 63 65 6c 6c 2e 20 20 0a 20 20 20 20 2a a cell. . *
159ca 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 6c *. ** The fol
159cb 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20 lowing block of
159cc 63 6f 64 65 20 63 68 65 63 6b 73 20 65 61 72 6c code checks earl
159cd 79 20 74 6f 20 73 65 65 20 69 66 20 61 20 63 65 y to see if a ce
159ce 6c 6c 20 65 78 74 65 6e 64 73 0a 20 20 20 20 2a ll extends. *
159cf 2a 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f * past the end o
159d0 66 20 61 20 70 61 67 65 20 62 6f 75 6e 64 61 72 f a page boundar
159d1 79 20 61 6e 64 20 63 61 75 73 65 73 20 53 51 4c y and causes SQL
159d2 49 54 45 5f 43 4f 52 52 55 50 54 20 74 6f 20 62 ITE_CORRUPT to b
159d3 65 20 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e e . ** return
159d4 65 64 20 69 66 20 69 74 20 64 6f 65 73 2e 0a 20 ed if it does..
159d5 20 20 20 2a 2f 0a 20 20 20 20 69 43 65 6c 6c 46 */. iCellF
159d6 69 72 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 irst = cellOffse
159d7 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 t + 2*pPage->nCe
159d8 6c 6c 3b 0a 20 20 20 20 69 43 65 6c 6c 4c 61 73 ll;. iCellLas
159d9 74 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 20 2d t = usableSize -
159da 20 34 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 4;.#if defined(
159db 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 SQLITE_ENABLE_OV
159dc 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 ERSIZE_CELL_CHEC
159dd 4b 29 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 K). {. i
159de 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
159df 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 74 /* Index into t
159e0 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 he cell pointer
159e1 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 20 20 69 array */. i
159e2 6e 74 20 73 7a 3b 20 20 20 20 20 20 20 20 20 20 nt sz;
159e3 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65 /* Size of a ce
159e4 6c 6c 20 2a 2f 0a 0a 20 20 20 20 20 20 69 66 28 ll */.. if(
159e5 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 !pPage->leaf )
159e6 69 43 65 6c 6c 4c 61 73 74 2d 2d 3b 0a 20 20 20 iCellLast--;.
159e7 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 for(i=0; i<pP
159e8 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 age->nCell; i++)
159e9 7b 0a 20 20 20 20 20 20 20 20 70 63 20 3d 20 67 {. pc = g
159ea 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 et2byte(&data[ce
159eb 6c 6c 4f 66 66 73 65 74 2b 69 2a 32 5d 29 3b 0a llOffset+i*2]);.
159ec 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
159ed 28 20 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74 ( pc==iCellFirst
159ee 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 );. test
159ef 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 4c case( pc==iCellL
159f0 61 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 69 ast );. i
159f1 66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 f( pc<iCellFirst
159f2 20 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 || pc>iCellLast
159f3 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
159f4 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
159f5 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
159f6 20 20 7d 0a 20 20 20 20 20 20 20 20 73 7a 20 3d }. sz =
159f7 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 cellSizePtr(pPa
159f8 67 65 2c 20 26 64 61 74 61 5b 70 63 5d 29 3b 0a ge, &data[pc]);.
159f9 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
159fa 28 20 70 63 2b 73 7a 3d 3d 75 73 61 62 6c 65 53 ( pc+sz==usableS
159fb 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 ize );. i
159fc 66 28 20 70 63 2b 73 7a 3e 75 73 61 62 6c 65 53 f( pc+sz>usableS
159fd 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ize ){.
159fe 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
159ff 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
15a00 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
15a01 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d if( !pPage-
15a02 3e 6c 65 61 66 20 29 20 69 43 65 6c 6c 4c 61 73 >leaf ) iCellLas
15a03 74 2b 2b 3b 0a 20 20 20 20 7d 20 20 0a 23 65 6e t++;. } .#en
15a04 64 69 66 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 dif.. /* Comp
15a05 75 74 65 20 74 68 65 20 74 6f 74 61 6c 20 66 72 ute the total fr
15a06 65 65 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 ee space on the
15a07 70 61 67 65 20 2a 2f 0a 20 20 20 20 70 63 20 3d page */. pc =
15a08 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
15a09 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6e 46 72 hdr+1]);. nFr
15a0a 65 65 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d ee = data[hdr+7]
15a0b 20 2b 20 74 6f 70 3b 0a 20 20 20 20 77 68 69 6c + top;. whil
15a0c 65 28 20 70 63 3e 30 20 29 7b 0a 20 20 20 20 20 e( pc>0 ){.
15a0d 20 75 31 36 20 6e 65 78 74 2c 20 73 69 7a 65 3b u16 next, size;
15a0e 0a 20 20 20 20 20 20 69 66 28 20 70 63 3c 69 43 . if( pc<iC
15a0f 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 ellFirst || pc>i
15a10 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 CellLast ){.
15a11 20 20 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 /* Start of
15a12 66 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 6f 66 free block is of
15a13 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 f the page */.
15a14 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
15a15 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
15a16 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 ; . }.
15a17 20 6e 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 next = get2byte
15a18 28 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 (&data[pc]);.
15a19 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 size = get2by
15a1a 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b te(&data[pc+2]);
15a1b 0a 20 20 20 20 20 20 69 66 28 20 28 6e 65 78 74 . if( (next
15a1c 3e 30 20 26 26 20 6e 65 78 74 3c 3d 70 63 2b 73 >0 && next<=pc+s
15a1d 69 7a 65 2b 33 29 20 7c 7c 20 70 63 2b 73 69 7a ize+3) || pc+siz
15a1e 65 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a e>usableSize ){.
15a1f 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 /* Free
15a20 62 6c 6f 63 6b 73 20 6d 75 73 74 20 62 65 20 69 blocks must be i
15a21 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 n ascending orde
15a22 72 2e 20 41 6e 64 20 74 68 65 20 6c 61 73 74 20 r. And the last
15a23 62 79 74 65 20 6f 66 0a 09 2a 2a 20 74 68 65 20 byte of..** the
15a24 66 72 65 65 2d 62 6c 6f 63 6b 20 6d 75 73 74 20 free-block must
15a25 6c 69 65 20 6f 6e 20 74 68 65 20 64 61 74 61 62 lie on the datab
15a26 61 73 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 ase page. */.
15a27 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
15a28 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
15a29 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 ; . }.
15a2a 20 6e 46 72 65 65 20 3d 20 6e 46 72 65 65 20 2b nFree = nFree +
15a2b 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 63 20 size;. pc
15a2c 3d 20 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 0a 20 = next;. }..
15a2d 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f /* At this po
15a2e 69 6e 74 2c 20 6e 46 72 65 65 20 63 6f 6e 74 61 int, nFree conta
15a2f 69 6e 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74 ins the sum of t
15a30 68 65 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65 he offset to the
15a31 20 73 74 61 72 74 0a 20 20 20 20 2a 2a 20 6f 66 start. ** of
15a32 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e the cell-conten
15a33 74 20 61 72 65 61 20 70 6c 75 73 20 74 68 65 20 t area plus the
15a34 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 62 number of free b
15a35 79 74 65 73 20 77 69 74 68 69 6e 0a 20 20 20 20 ytes within.
15a36 2a 2a 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 ** the cell-cont
15a37 65 6e 74 20 61 72 65 61 2e 20 49 66 20 74 68 69 ent area. If thi
15a38 73 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 s is greater tha
15a39 6e 20 74 68 65 20 75 73 61 62 6c 65 2d 73 69 7a n the usable-siz
15a3a 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 e. ** of the
15a3b 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 70 page, then the p
15a3c 61 67 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 age must be corr
15a3d 75 70 74 65 64 2e 20 54 68 69 73 20 63 68 65 63 upted. This chec
15a3e 6b 20 61 6c 73 6f 0a 20 20 20 20 2a 2a 20 73 65 k also. ** se
15a3f 72 76 65 73 20 74 6f 20 76 65 72 69 66 79 20 74 rves to verify t
15a40 68 61 74 20 74 68 65 20 6f 66 66 73 65 74 20 74 hat the offset t
15a41 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 o the start of t
15a42 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 0a he cell-content.
15a43 20 20 20 20 2a 2a 20 61 72 65 61 2c 20 61 63 63 ** area, acc
15a44 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 70 61 ording to the pa
15a45 67 65 20 68 65 61 64 65 72 2c 20 6c 69 65 73 20 ge header, lies
15a46 77 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 2e within the page.
15a47 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
15a48 6e 46 72 65 65 3e 75 73 61 62 6c 65 53 69 7a 65 nFree>usableSize
15a49 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
15a4a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
15a4b 42 4b 50 54 3b 20 0a 20 20 20 20 7d 0a 20 20 20 BKPT; . }.
15a4c 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 pPage->nFree =
15a4d 28 75 31 36 29 28 6e 46 72 65 65 20 2d 20 69 43 (u16)(nFree - iC
15a4e 65 6c 6c 46 69 72 73 74 29 3b 0a 20 20 20 20 70 ellFirst);. p
15a4f 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 Page->isInit = 1
15a50 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
15a51 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
15a52 2a 2a 20 53 65 74 20 75 70 20 61 20 72 61 77 20 ** Set up a raw
15a53 70 61 67 65 20 73 6f 20 74 68 61 74 20 69 74 20 page so that it
15a54 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 64 61 74 looks like a dat
15a55 61 62 61 73 65 20 70 61 67 65 20 68 6f 6c 64 69 abase page holdi
15a56 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72 69 65 73 ng.** no entries
15a57 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
15a58 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d 50 61 67 zeroPage(MemPag
15a59 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 6c e *pPage, int fl
15a5a 61 67 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 ags){. unsigned
15a5b 20 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70 50 char *data = pP
15a5c 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 42 74 age->aData;. Bt
15a5d 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 Shared *pBt = pP
15a5e 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 68 age->pBt;. u8 h
15a5f 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f dr = pPage->hdrO
15a60 66 66 73 65 74 3b 0a 20 20 75 31 36 20 66 69 72 ffset;. u16 fir
15a61 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 st;.. assert( s
15a62 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e qlite3PagerPagen
15a63 75 6d 62 65 72 28 70 50 61 67 65 2d 3e 70 44 62 umber(pPage->pDb
15a64 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e 70 67 Page)==pPage->pg
15a65 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 no );. assert(
15a66 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 sqlite3PagerGetE
15a67 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50 xtra(pPage->pDbP
15a68 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 age) == (void*)p
15a69 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 Page );. assert
15a6a 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 ( sqlite3PagerGe
15a6b 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 tData(pPage->pDb
15a6c 50 61 67 65 29 20 3d 3d 20 64 61 74 61 20 29 3b Page) == data );
15a6d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15a6e 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
15a6f 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
15a70 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 e) );. assert(
15a71 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
15a72 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
15a73 3b 0a 20 20 2f 2a 6d 65 6d 73 65 74 28 26 64 61 ;. /*memset(&da
15a74 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70 42 74 2d ta[hdr], 0, pBt-
15a75 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 68 64 >usableSize - hd
15a76 72 29 3b 2a 2f 0a 20 20 64 61 74 61 5b 68 64 72 r);*/. data[hdr
15a77 5d 20 3d 20 28 63 68 61 72 29 66 6c 61 67 73 3b ] = (char)flags;
15a78 0a 20 20 66 69 72 73 74 20 3d 20 68 64 72 20 2b . first = hdr +
15a79 20 38 20 2b 20 34 2a 28 28 66 6c 61 67 73 26 50 8 + 4*((flags&P
15a7a 54 46 5f 4c 45 41 46 29 3d 3d 30 20 3f 31 3a 30 TF_LEAF)==0 ?1:0
15a7b 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 );. memset(&dat
15a7c 61 5b 68 64 72 2b 31 5d 2c 20 30 2c 20 34 29 3b a[hdr+1], 0, 4);
15a7d 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d . data[hdr+7] =
15a7e 20 30 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 0;. put2byte(&
15a7f 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 70 42 74 data[hdr+5], pBt
15a80 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 ->usableSize);.
15a81 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 pPage->nFree =
15a82 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
15a83 2d 20 66 69 72 73 74 3b 0a 20 20 64 65 63 6f 64 - first;. decod
15a84 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 66 6c eFlags(pPage, fl
15a85 61 67 73 29 3b 0a 20 20 70 50 61 67 65 2d 3e 68 ags);. pPage->h
15a86 64 72 4f 66 66 73 65 74 20 3d 20 68 64 72 3b 0a drOffset = hdr;.
15a87 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 pPage->cellOff
15a88 73 65 74 20 3d 20 66 69 72 73 74 3b 0a 20 20 70 set = first;. p
15a89 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 Page->nOverflow
15a8a 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 = 0;. assert( p
15a8b 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31 Bt->pageSize>=51
15a8c 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69 2 && pBt->pageSi
15a8d 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 70 ze<=32768 );. p
15a8e 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d Page->maskPage =
15a8f 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d pBt->pageSize -
15a90 20 31 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 1;. pPage->nCe
15a91 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d ll = 0;. pPage-
15a92 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 7d 0a 0a >isInit = 1;.}..
15a93 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 ./*.** Convert a
15a94 20 44 62 50 61 67 65 20 6f 62 74 61 69 6e 65 64 DbPage obtained
15a95 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 from the pager
15a96 69 6e 74 6f 20 61 20 4d 65 6d 50 61 67 65 20 75 into a MemPage u
15a97 73 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 62 74 sed by.** the bt
15a98 72 65 65 20 6c 61 79 65 72 2e 0a 2a 2f 0a 73 74 ree layer..*/.st
15a99 61 74 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 74 atic MemPage *bt
15a9a 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 reePageFromDbPag
15a9b 65 28 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 e(DbPage *pDbPag
15a9c 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 42 74 e, Pgno pgno, Bt
15a9d 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
15a9e 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d MemPage *pPage =
15a9f 20 28 4d 65 6d 50 61 67 65 2a 29 73 71 6c 69 74 (MemPage*)sqlit
15aa0 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 e3PagerGetExtra(
15aa1 70 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67 pDbPage);. pPag
15aa2 65 2d 3e 61 44 61 74 61 20 3d 20 73 71 6c 69 74 e->aData = sqlit
15aa3 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 e3PagerGetData(p
15aa4 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67 65 DbPage);. pPage
15aa5 2d 3e 70 44 62 50 61 67 65 20 3d 20 70 44 62 50 ->pDbPage = pDbP
15aa6 61 67 65 3b 0a 20 20 70 50 61 67 65 2d 3e 70 42 age;. pPage->pB
15aa7 74 20 3d 20 70 42 74 3b 0a 20 20 70 50 61 67 65 t = pBt;. pPage
15aa8 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 ->pgno = pgno;.
15aa9 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
15aaa 74 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d t = pPage->pgno=
15aab 3d 31 20 3f 20 31 30 30 20 3a 20 30 3b 0a 20 20 =1 ? 100 : 0;.
15aac 72 65 74 75 72 6e 20 70 50 61 67 65 3b 20 0a 7d return pPage; .}
15aad 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61 ../*.** Get a pa
15aae 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 ge from the page
15aaf 72 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 r. Initialize t
15ab0 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61 he MemPage.pBt a
15ab1 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44 nd.** MemPage.aD
15ab2 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20 ata elements if
15ab3 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 needed..**.** If
15ab4 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 66 the noContent f
15ab5 6c 61 67 20 69 73 20 73 65 74 2c 20 69 74 20 6d lag is set, it m
15ab6 65 61 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 eans that we do
15ab7 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 0a 2a not care about.*
15ab8 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 * the content of
15ab9 20 74 68 65 20 70 61 67 65 20 61 74 20 74 68 69 the page at thi
15aba 73 20 74 69 6d 65 2e 20 20 53 6f 20 64 6f 20 6e s time. So do n
15abb 6f 74 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73 ot go to the dis
15abc 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 68 20 74 68 k.** to fetch th
15abd 65 20 63 6f 6e 74 65 6e 74 2e 20 20 4a 75 73 74 e content. Just
15abe 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 63 6f 6e fill in the con
15abf 74 65 6e 74 20 77 69 74 68 20 7a 65 72 6f 73 20 tent with zeros
15ac0 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 49 66 20 69 for now..** If i
15ac1 6e 20 74 68 65 20 66 75 74 75 72 65 20 77 65 20 n the future we
15ac2 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 call sqlite3Page
15ac3 72 57 72 69 74 65 28 29 20 6f 6e 20 74 68 69 73 rWrite() on this
15ac4 20 70 61 67 65 2c 20 74 68 61 74 0a 2a 2a 20 6d page, that.** m
15ac5 65 61 6e 73 20 77 65 20 68 61 76 65 20 73 74 61 eans we have sta
15ac6 72 74 65 64 20 74 6f 20 62 65 20 63 6f 6e 63 65 rted to be conce
15ac7 72 6e 65 64 20 61 62 6f 75 74 20 63 6f 6e 74 65 rned about conte
15ac8 6e 74 20 61 6e 64 20 74 68 65 20 64 69 73 6b 0a nt and the disk.
15ac9 2a 2a 20 72 65 61 64 20 73 68 6f 75 6c 64 20 6f ** read should o
15aca 63 63 75 72 20 61 74 20 74 68 61 74 20 70 6f 69 ccur at that poi
15acb 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e nt..*/.static in
15acc 74 20 62 74 72 65 65 47 65 74 50 61 67 65 28 0a t btreeGetPage(.
15acd 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c BtShared *pBt,
15ace 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 /* The bt
15acf 72 65 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 ree */. Pgno pg
15ad0 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a no, /*
15ad1 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 Number of the p
15ad2 61 67 65 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a age to fetch */.
15ad3 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 MemPage **ppPa
15ad4 67 65 2c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e ge, /* Return
15ad5 20 74 68 65 20 70 61 67 65 20 69 6e 20 74 68 69 the page in thi
15ad6 73 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 s parameter */.
15ad7 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 20 20 int noContent
15ad8 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 /* Do not
15ad9 6c 6f 61 64 20 70 61 67 65 20 63 6f 6e 74 65 6e load page conten
15ada 74 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a t if true */.){.
15adb 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 62 50 61 int rc;. DbPa
15adc 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 0a 20 20 ge *pDbPage;..
15add 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
15ade 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
15adf 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d mutex) );. rc =
15ae0 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 sqlite3PagerAcq
15ae1 75 69 72 65 28 70 42 74 2d 3e 70 50 61 67 65 72 uire(pBt->pPager
15ae2 2c 20 70 67 6e 6f 2c 20 28 44 62 50 61 67 65 2a , pgno, (DbPage*
15ae3 2a 29 26 70 44 62 50 61 67 65 2c 20 6e 6f 43 6f *)&pDbPage, noCo
15ae4 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 ntent);. if( rc
15ae5 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
15ae6 2a 70 70 50 61 67 65 20 3d 20 62 74 72 65 65 50 *ppPage = btreeP
15ae7 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28 70 44 ageFromDbPage(pD
15ae8 62 50 61 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74 bPage, pgno, pBt
15ae9 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
15aea 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
15aeb 52 65 74 72 69 65 76 65 20 61 20 70 61 67 65 20 Retrieve a page
15aec 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 63 from the pager c
15aed 61 63 68 65 2e 20 49 66 20 74 68 65 20 72 65 71 ache. If the req
15aee 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20 6e uested page is n
15aef 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e ot.** already in
15af0 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 the pager cache
15af1 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 49 6e return NULL. In
15af2 69 74 69 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d itialize the Mem
15af3 50 61 67 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 Page.pBt and.**
15af4 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 20 65 6c MemPage.aData el
15af5 65 6d 65 6e 74 73 20 69 66 20 6e 65 65 64 65 64 ements if needed
15af6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 ..*/.static MemP
15af7 61 67 65 20 2a 62 74 72 65 65 50 61 67 65 4c 6f age *btreePageLo
15af8 6f 6b 75 70 28 42 74 53 68 61 72 65 64 20 2a 70 okup(BtShared *p
15af9 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a Bt, Pgno pgno){.
15afa 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 DbPage *pDbPag
15afb 65 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c e;. assert( sql
15afc 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
15afd 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
15afe 20 70 44 62 50 61 67 65 20 3d 20 73 71 6c 69 74 pDbPage = sqlit
15aff 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 70 42 e3PagerLookup(pB
15b00 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 t->pPager, pgno)
15b01 3b 0a 20 20 69 66 28 20 70 44 62 50 61 67 65 20 ;. if( pDbPage
15b02 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 62 74 ){. return bt
15b03 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 reePageFromDbPag
15b04 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c e(pDbPage, pgno,
15b05 20 70 42 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 pBt);. }. ret
15b06 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
15b07 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 Return the size
15b08 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
15b09 66 69 6c 65 20 69 6e 20 70 61 67 65 73 2e 20 49 file in pages. I
15b0a 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b f there is any k
15b0b 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 72 6f 72 2c ind of.** error,
15b0c 20 72 65 74 75 72 6e 20 28 28 75 6e 73 69 67 6e return ((unsign
15b0d 65 64 20 69 6e 74 29 2d 31 29 2e 0a 2a 2f 0a 73 ed int)-1)..*/.s
15b0e 74 61 74 69 63 20 50 67 6e 6f 20 70 61 67 65 72 tatic Pgno pager
15b0f 50 61 67 65 63 6f 75 6e 74 28 42 74 53 68 61 72 Pagecount(BtShar
15b10 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 ed *pBt){. int
15b11 6e 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 69 6e nPage = -1;. in
15b12 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 t rc;. assert(
15b13 70 42 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20 pBt->pPage1 );.
15b14 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
15b15 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d erPagecount(pBt-
15b16 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 >pPager, &nPage)
15b17 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d ;. assert( rc==
15b18 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e 50 61 SQLITE_OK || nPa
15b19 67 65 3d 3d 2d 31 20 29 3b 0a 20 20 72 65 74 75 ge==-1 );. retu
15b1a 72 6e 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a rn (Pgno)nPage;.
15b1b 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 }../*.** Get a p
15b1c 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 age from the pag
15b1d 65 72 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a er and initializ
15b1e 65 20 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 e it. This rout
15b1f 69 6e 65 20 69 73 20 6a 75 73 74 20 61 0a 2a 2a ine is just a.**
15b20 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 convenience wra
15b21 70 70 65 72 20 61 72 6f 75 6e 64 20 73 65 70 61 pper around sepa
15b22 72 61 74 65 20 63 61 6c 6c 73 20 74 6f 20 62 74 rate calls to bt
15b23 72 65 65 47 65 74 50 61 67 65 28 29 20 61 6e 64 reeGetPage() and
15b24 20 0a 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61 .** btreeInitPa
15b25 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 ge()..**.** If a
15b26 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
15b27 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 2a then the value *
15b28 70 70 50 61 67 65 20 69 73 20 73 65 74 20 74 6f ppPage is set to
15b29 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 49 is undefined. I
15b2a 74 0a 2a 2a 20 6d 61 79 20 72 65 6d 61 69 6e 20 t.** may remain
15b2b 75 6e 63 68 61 6e 67 65 64 2c 20 6f 72 20 69 74 unchanged, or it
15b2c 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f 20 61 may be set to a
15b2d 6e 20 69 6e 76 61 6c 69 64 20 76 61 6c 75 65 2e n invalid value.
15b2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 .*/.static int g
15b2f 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 0a 20 etAndInitPage(.
15b30 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 BtShared *pBt,
15b31 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
15b32 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
15b33 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 . Pgno pgno,
15b34 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
15b35 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f r of the page to
15b36 20 67 65 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 get */. MemPag
15b37 65 20 2a 2a 70 70 50 61 67 65 20 20 20 20 20 2f e **ppPage /
15b38 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 67 65 * Write the page
15b39 20 70 6f 69 6e 74 65 72 20 68 65 72 65 20 2a 2f pointer here */
15b3a 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 .){. int rc;.
15b3b 54 45 53 54 4f 4e 4c 59 28 20 50 67 6e 6f 20 69 TESTONLY( Pgno i
15b3c 4c 61 73 74 50 67 20 3d 20 70 61 67 65 72 50 61 LastPg = pagerPa
15b3d 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 20 29 0a gecount(pBt); ).
15b3e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15b3f 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
15b40 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 72 ->mutex) );.. r
15b41 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
15b42 28 70 42 74 2c 20 70 67 6e 6f 2c 20 70 70 50 61 (pBt, pgno, ppPa
15b43 67 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 ge, 0);. if( rc
15b44 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
15b45 20 20 20 72 63 20 3d 20 62 74 72 65 65 49 6e 69 rc = btreeIni
15b46 74 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a tPage(*ppPage);.
15b47 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
15b48 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
15b49 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 eleasePage(*ppPa
15b4a 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a ge);. }. }..
15b4b 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 71 75 /* If the requ
15b4c 65 73 74 65 64 20 70 61 67 65 20 6e 75 6d 62 65 ested page numbe
15b4d 72 20 77 61 73 20 65 69 74 68 65 72 20 30 20 6f r was either 0 o
15b4e 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 r greater than t
15b4f 68 65 20 70 61 67 65 0a 20 20 2a 2a 20 6e 75 6d he page. ** num
15b50 62 65 72 20 6f 66 20 74 68 65 20 6c 61 73 74 20 ber of the last
15b51 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 page in the data
15b52 62 61 73 65 2c 20 74 68 69 73 20 66 75 6e 63 74 base, this funct
15b53 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65 74 75 72 ion should retur
15b54 6e 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 43 4f n. ** SQLITE_CO
15b55 52 52 55 50 54 20 6f 72 20 73 6f 6d 65 20 6f 74 RRUPT or some ot
15b56 68 65 72 20 65 72 72 6f 72 20 28 69 2e 65 2e 20 her error (i.e.
15b57 53 51 4c 49 54 45 5f 46 55 4c 4c 29 2e 20 43 68 SQLITE_FULL). Ch
15b58 65 63 6b 20 74 68 61 74 20 74 68 69 73 0a 20 20 eck that this.
15b59 2a 2a 20 69 73 20 74 68 65 20 63 61 73 65 2e 20 ** is the case.
15b5a 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 */. assert( (p
15b5b 67 6e 6f 3e 30 20 26 26 20 70 67 6e 6f 3c 3d 69 gno>0 && pgno<=i
15b5c 4c 61 73 74 50 67 29 20 7c 7c 20 72 63 21 3d 53 LastPg) || rc!=S
15b5d 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 74 65 QLITE_OK );. te
15b5e 73 74 63 61 73 65 28 20 70 67 6e 6f 3d 3d 30 20 stcase( pgno==0
15b5f 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 );. testcase( p
15b60 67 6e 6f 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a gno==iLastPg );.
15b61 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
15b62 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 ./*.** Release a
15b63 20 4d 65 6d 50 61 67 65 2e 20 20 54 68 69 73 20 MemPage. This
15b64 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 should be called
15b65 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 70 once for each p
15b66 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 rior.** call to
15b67 62 74 72 65 65 47 65 74 50 61 67 65 2e 0a 2a 2f btreeGetPage..*/
15b68 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c .static void rel
15b69 65 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65 easePage(MemPage
15b6a 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 66 28 20 *pPage){. if(
15b6b 70 50 61 67 65 20 29 7b 0a 20 20 20 20 61 73 73 pPage ){. ass
15b6c 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 ert( pPage->nOve
15b6d 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 73 71 6c 69 rflow==0 || sqli
15b6e 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 te3PagerPageRefc
15b6f 6f 75 6e 74 28 70 50 61 67 65 2d 3e 70 44 62 50 ount(pPage->pDbP
15b70 61 67 65 29 3e 31 20 29 3b 0a 20 20 20 20 61 73 age)>1 );. as
15b71 73 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 sert( pPage->aDa
15b72 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ta );. assert
15b73 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a ( pPage->pBt );.
15b74 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
15b75 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 te3PagerGetExtra
15b76 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
15b77 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 65 == (void*)pPage
15b78 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
15b79 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 sqlite3PagerGetD
15b7a 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ata(pPage->pDbPa
15b7b 67 65 29 3d 3d 70 50 61 67 65 2d 3e 61 44 61 74 ge)==pPage->aDat
15b7c 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 a );. assert(
15b7d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15b7e 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
15b7f 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 73 71 mutex) );. sq
15b80 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
15b81 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b pPage->pDbPage);
15b82 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 75 . }.}../*.** Du
15b83 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c ring a rollback,
15b84 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 when the pager
15b85 72 65 6c 6f 61 64 73 20 69 6e 66 6f 72 6d 61 74 reloads informat
15b86 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 63 61 63 ion into the cac
15b87 68 65 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 he.** so that th
15b88 65 20 63 61 63 68 65 20 69 73 20 72 65 73 74 6f e cache is resto
15b89 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 69 red to its origi
15b8a 6e 61 6c 20 73 74 61 74 65 20 61 74 20 74 68 65 nal state at the
15b8b 20 73 74 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 start of.** the
15b8c 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 66 6f transaction, fo
15b8d 72 20 65 61 63 68 20 70 61 67 65 20 72 65 73 74 r each page rest
15b8e 6f 72 65 64 20 74 68 69 73 20 72 6f 75 74 69 6e ored this routin
15b8f 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a e is called..**.
15b90 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
15b91 6e 65 65 64 73 20 74 6f 20 72 65 73 65 74 20 74 needs to reset t
15b92 68 65 20 65 78 74 72 61 20 64 61 74 61 20 73 65 he extra data se
15b93 63 74 69 6f 6e 20 61 74 20 74 68 65 20 65 6e 64 ction at the end
15b94 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 of the.** page
15b95 74 6f 20 61 67 72 65 65 20 77 69 74 68 20 74 68 to agree with th
15b96 65 20 72 65 73 74 6f 72 65 64 20 64 61 74 61 2e e restored data.
15b97 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
15b98 70 61 67 65 52 65 69 6e 69 74 28 44 62 50 61 67 pageReinit(DbPag
15b99 65 20 2a 70 44 61 74 61 29 7b 0a 20 20 4d 65 6d e *pData){. Mem
15b9a 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 70 Page *pPage;. p
15b9b 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 20 Page = (MemPage
15b9c 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *)sqlite3PagerGe
15b9d 74 45 78 74 72 61 28 70 44 61 74 61 29 3b 0a 20 tExtra(pData);.
15b9e 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15b9f 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e PagerPageRefcoun
15ba0 74 28 70 44 61 74 61 29 3e 30 20 29 3b 0a 20 20 t(pData)>0 );.
15ba1 69 66 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 if( pPage->isIni
15ba2 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 t ){. assert(
15ba3 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15ba4 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
15ba5 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 70 50 mutex) );. pP
15ba6 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b age->isInit = 0;
15ba7 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
15ba8 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e PagerPageRefcoun
15ba9 74 28 70 44 61 74 61 29 3e 31 20 29 7b 0a 20 20 t(pData)>1 ){.
15baa 20 20 20 20 2f 2a 20 70 50 61 67 65 20 6d 69 67 /* pPage mig
15bab 68 74 20 6e 6f 74 20 62 65 20 61 20 62 74 72 65 ht not be a btre
15bac 65 20 70 61 67 65 3b 20 20 69 74 20 6d 69 67 68 e page; it migh
15bad 74 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 t be an overflow
15bae 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 6f page. ** o
15baf 72 20 70 74 72 6d 61 70 20 70 61 67 65 20 6f 72 r ptrmap page or
15bb0 20 61 20 66 72 65 65 20 70 61 67 65 2e 20 20 49 a free page. I
15bb1 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c 20 74 n those cases, t
15bb2 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 he following.
15bb3 20 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 62 74 ** call to bt
15bb4 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 77 69 reeInitPage() wi
15bb5 6c 6c 20 6c 69 6b 65 6c 79 20 72 65 74 75 72 6e ll likely return
15bb6 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e SQLITE_CORRUPT.
15bb7 0a 20 20 20 20 20 20 2a 2a 20 42 75 74 20 6e 6f . ** But no
15bb8 20 68 61 72 6d 20 69 73 20 64 6f 6e 65 20 62 79 harm is done by
15bb9 20 74 68 69 73 2e 20 20 41 6e 64 20 69 74 20 69 this. And it i
15bba 73 20 76 65 72 79 20 69 6d 70 6f 72 74 61 6e 74 s very important
15bbb 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 62 that. ** b
15bbc 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 62 treeInitPage() b
15bbd 65 20 63 61 6c 6c 65 64 20 6f 6e 20 65 76 65 72 e called on ever
15bbe 79 20 62 74 72 65 65 20 70 61 67 65 20 73 6f 20 y btree page so
15bbf 77 65 20 6d 61 6b 65 0a 20 20 20 20 20 20 2a 2a we make. **
15bc0 20 74 68 65 20 63 61 6c 6c 20 66 6f 72 20 65 76 the call for ev
15bc1 65 72 79 20 70 61 67 65 20 74 68 61 74 20 63 6f ery page that co
15bc2 6d 65 73 20 69 6e 20 66 6f 72 20 72 65 2d 69 6e mes in for re-in
15bc3 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 iting. */.
15bc4 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 btreeInitPage(pP
15bc5 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a age);. }. }.
15bc6 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 }../*.** Invoke
15bc7 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 the busy handler
15bc8 20 66 6f 72 20 61 20 62 74 72 65 65 2e 0a 2a 2f for a btree..*/
15bc9 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 .static int btre
15bca 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c eInvokeBusyHandl
15bcb 65 72 28 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a er(void *pArg){.
15bcc 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
15bcd 3d 20 28 42 74 53 68 61 72 65 64 2a 29 70 41 72 = (BtShared*)pAr
15bce 67 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 g;. assert( pBt
15bcf 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 ->db );. assert
15bd0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
15bd1 68 65 6c 64 28 70 42 74 2d 3e 64 62 2d 3e 6d 75 held(pBt->db->mu
15bd2 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e tex) );. return
15bd3 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 sqlite3InvokeBu
15bd4 73 79 48 61 6e 64 6c 65 72 28 26 70 42 74 2d 3e syHandler(&pBt->
15bd5 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 29 db->busyHandler)
15bd6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 ;.}../*.** Open
15bd7 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e a database file.
15bd8 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c 65 6e 61 6d .** .** zFilenam
15bd9 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 e is the name of
15bda 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
15bdb 6c 65 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d le. If zFilenam
15bdc 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 61 20 6e e is NULL.** a n
15bdd 65 77 20 64 61 74 61 62 61 73 65 20 77 69 74 68 ew database with
15bde 20 61 20 72 61 6e 64 6f 6d 20 6e 61 6d 65 20 69 a random name i
15bdf 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 69 73 s created. This
15be0 20 72 61 6e 64 6f 6d 6c 79 20 6e 61 6d 65 64 0a randomly named.
15be1 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
15be2 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 64 will be deleted
15be3 20 77 68 65 6e 20 73 71 6c 69 74 65 33 42 74 72 when sqlite3Btr
15be4 65 65 43 6c 6f 73 65 28 29 20 69 73 20 63 61 6c eeClose() is cal
15be5 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 led..** If zFile
15be6 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 name is ":memory
15be7 3a 22 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d 65 :" then an in-me
15be8 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 69 73 mory database is
15be9 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 68 61 74 created.** that
15bea 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c is automaticall
15beb 79 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e y destroyed when
15bec 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a it is closed..*
15bed 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 *.** If the data
15bee 62 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20 base is already
15bef 6f 70 65 6e 65 64 20 69 6e 20 74 68 65 20 73 61 opened in the sa
15bf0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
15bf1 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 77 65 ection.** and we
15bf2 20 61 72 65 20 69 6e 20 73 68 61 72 65 64 20 63 are in shared c
15bf3 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 65 6e 20 ache mode, then
15bf4 74 68 65 20 6f 70 65 6e 20 77 69 6c 6c 20 66 61 the open will fa
15bf5 69 6c 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51 il with an.** SQ
15bf6 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 LITE_CONSTRAINT
15bf7 65 72 72 6f 72 2e 20 20 57 65 20 63 61 6e 6e 6f error. We canno
15bf8 74 20 61 6c 6c 6f 77 20 74 77 6f 20 6f 72 20 6d t allow two or m
15bf9 6f 72 65 20 42 74 53 68 61 72 65 64 0a 2a 2a 20 ore BtShared.**
15bfa 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 65 20 73 objects in the s
15bfb 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e ame database con
15bfc 6e 65 63 74 69 6f 6e 20 73 69 6e 63 65 20 64 6f nection since do
15bfd 69 6e 67 20 73 6f 20 77 69 6c 6c 20 6c 65 61 64 ing so will lead
15bfe 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c 65 6d 73 20 .** to problems
15bff 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f with locking..*/
15c00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
15c01 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
15c02 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 Open(. const ch
15c03 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 ar *zFilename,
15c04 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 /* Name of the f
15c05 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ile containing t
15c06 68 65 20 42 54 72 65 65 20 64 61 74 61 62 61 73 he BTree databas
15c07 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a e */. sqlite3 *
15c08 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f db, /
15c09 2a 20 41 73 73 6f 63 69 61 74 65 64 20 64 61 74 * Associated dat
15c0a 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a abase handle */.
15c0b 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 Btree **ppBtre
15c0c 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 e, /* Poi
15c0d 6e 74 65 72 20 74 6f 20 6e 65 77 20 42 74 72 65 nter to new Btre
15c0e 65 20 6f 62 6a 65 63 74 20 77 72 69 74 74 65 6e e object written
15c0f 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 here */. int f
15c10 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
15c11 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 73 20 2a 2f /* Options */
15c12 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 . int vfsFlags
15c13 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c /* Fl
15c14 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 ags passed throu
15c15 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 gh to sqlite3_vf
15c16 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 29 7b 0a s.xOpen() */.){.
15c17 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
15c18 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 Vfs;
15c19 20 2f 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75 /* The VFS to u
15c1a 73 65 20 66 6f 72 20 74 68 69 73 20 62 74 72 65 se for this btre
15c1b 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 e */. BtShared
15c1c 2a 70 42 74 20 3d 20 30 3b 20 20 20 20 20 20 20 *pBt = 0;
15c1d 20 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20 /* Shared
15c1e 70 61 72 74 20 6f 66 20 62 74 72 65 65 20 73 74 part of btree st
15c1f 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 42 74 72 ructure */. Btr
15c20 65 65 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 ee *p;
15c21 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
15c22 61 6e 64 6c 65 20 74 6f 20 72 65 74 75 72 6e 20 andle to return
15c23 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 */. sqlite3_mut
15c24 65 78 20 2a 6d 75 74 65 78 4f 70 65 6e 20 3d 20 ex *mutexOpen =
15c25 30 3b 20 20 2f 2a 20 50 72 65 76 65 6e 74 73 20 0; /* Prevents
15c26 61 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e a race condition
15c27 2e 20 54 69 63 6b 65 74 20 23 33 35 33 37 20 2a . Ticket #3537 *
15c28 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c /. int rc = SQL
15c29 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 ITE_OK;
15c2a 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 /* Result cod
15c2b 65 20 66 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 e from this func
15c2c 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 6e 52 65 tion */. u8 nRe
15c2d 73 65 72 76 65 3b 20 20 20 20 20 20 20 20 20 20 serve;
15c2e 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 /* Byte
15c2f 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 65 of unused space
15c30 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f on each page */
15c31 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
15c32 20 7a 44 62 48 65 61 64 65 72 5b 31 30 30 5d 3b zDbHeader[100];
15c33 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 65 /* Database he
15c34 61 64 65 72 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a ader content */.
15c35 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 76 61 . /* Set the va
15c36 72 69 61 62 6c 65 20 69 73 4d 65 6d 64 62 20 74 riable isMemdb t
15c37 6f 20 74 72 75 65 20 66 6f 72 20 61 6e 20 69 6e o true for an in
15c38 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
15c39 2c 20 6f 72 20 0a 20 20 2a 2a 20 66 61 6c 73 65 , or . ** false
15c3a 20 66 6f 72 20 61 20 66 69 6c 65 2d 62 61 73 65 for a file-base
15c3b 64 20 64 61 74 61 62 61 73 65 2e 20 54 68 69 73 d database. This
15c3c 20 73 79 6d 62 6f 6c 20 69 73 20 6f 6e 6c 79 20 symbol is only
15c3d 72 65 71 75 69 72 65 64 20 69 66 0a 20 20 2a 2a required if. **
15c3e 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 73 either of the s
15c3f 68 61 72 65 64 2d 64 61 74 61 20 6f 72 20 61 75 hared-data or au
15c40 74 6f 76 61 63 75 75 6d 20 66 65 61 74 75 72 65 tovacuum feature
15c41 73 20 61 72 65 20 63 6f 6d 70 69 6c 65 64 20 0a s are compiled .
15c42 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6c 69 ** into the li
15c43 62 72 61 72 79 2e 0a 20 20 2a 2f 0a 23 69 66 20 brary.. */.#if
15c44 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
15c45 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
15c46 45 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 E) || !defined(S
15c47 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
15c48 41 43 55 55 4d 29 0a 20 20 23 69 66 64 65 66 20 ACUUM). #ifdef
15c49 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f SQLITE_OMIT_MEMO
15c4a 52 59 44 42 0a 20 20 20 20 63 6f 6e 73 74 20 69 RYDB. const i
15c4b 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20 30 3b 0a nt isMemdb = 0;.
15c4c 20 20 23 65 6c 73 65 0a 20 20 20 20 63 6f 6e 73 #else. cons
15c4d 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20 t int isMemdb =
15c4e 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 21 73 74 zFilename && !st
15c4f 72 63 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 rcmp(zFilename,
15c50 22 3a 6d 65 6d 6f 72 79 3a 22 29 3b 0a 20 20 23 ":memory:");. #
15c51 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20 20 endif.#endif..
15c52 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b assert( db!=0 );
15c53 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15c54 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 e3_mutex_held(db
15c55 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 ->mutex) );.. p
15c56 56 66 73 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a Vfs = db->pVfs;.
15c57 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c p = sqlite3Mal
15c58 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42 locZero(sizeof(B
15c59 74 72 65 65 29 29 3b 0a 20 20 69 66 28 20 21 70 tree));. if( !p
15c5a 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
15c5b 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
15c5c 0a 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 . p->inTrans =
15c5d 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 70 2d TRANS_NONE;. p-
15c5e 3e 64 62 20 3d 20 64 62 3b 0a 23 69 66 6e 64 65 >db = db;.#ifnde
15c5f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
15c60 41 52 45 44 5f 43 41 43 48 45 0a 20 20 70 2d 3e ARED_CACHE. p->
15c61 6c 6f 63 6b 2e 70 42 74 72 65 65 20 3d 20 70 3b lock.pBtree = p;
15c62 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61 62 6c . p->lock.iTabl
15c63 65 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 23 e = 1;.#endif..#
15c64 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
15c65 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
15c66 41 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 ACHE) && !define
15c67 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 d(SQLITE_OMIT_DI
15c68 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 SKIO). /*. **
15c69 49 66 20 74 68 69 73 20 42 74 72 65 65 20 69 73 If this Btree is
15c6a 20 61 20 63 61 6e 64 69 64 61 74 65 20 66 6f 72 a candidate for
15c6b 20 73 68 61 72 65 64 20 63 61 63 68 65 2c 20 74 shared cache, t
15c6c 72 79 20 74 6f 20 66 69 6e 64 20 61 6e 0a 20 20 ry to find an.
15c6d 2a 2a 20 65 78 69 73 74 69 6e 67 20 42 74 53 68 ** existing BtSh
15c6e 61 72 65 64 20 6f 62 6a 65 63 74 20 74 68 61 74 ared object that
15c6f 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 77 69 we can share wi
15c70 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 th. */. if( is
15c71 4d 65 6d 64 62 3d 3d 30 20 26 26 20 7a 46 69 6c Memdb==0 && zFil
15c72 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 ename && zFilena
15c73 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 20 69 66 28 me[0] ){. if(
15c74 20 76 66 73 46 6c 61 67 73 20 26 20 53 51 4c 49 vfsFlags & SQLI
15c75 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 TE_OPEN_SHAREDCA
15c76 43 48 45 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 CHE ){. int
15c77 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d nFullPathname =
15c78 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d pVfs->mxPathnam
15c79 65 2b 31 3b 0a 20 20 20 20 20 20 63 68 61 72 20 e+1;. char
15c7a 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d *zFullPathname =
15c7b 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e sqlite3Malloc(n
15c7c 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 FullPathname);.
15c7d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
15c7e 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b ex *mutexShared;
15c7f 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62 . p->sharab
15c80 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66 le = 1;. if
15c81 28 20 21 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 ( !zFullPathname
15c82 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
15c83 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 te3_free(p);.
15c84 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
15c85 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
15c86 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f }. sqlite3O
15c87 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 sFullPathname(pV
15c88 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e fs, zFilename, n
15c89 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 7a 46 FullPathname, zF
15c8a 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 ullPathname);.
15c8b 20 20 20 20 6d 75 74 65 78 4f 70 65 6e 20 3d 20 mutexOpen =
15c8c 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
15c8d 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
15c8e 54 41 54 49 43 5f 4f 50 45 4e 29 3b 0a 20 20 20 TATIC_OPEN);.
15c8f 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
15c90 5f 65 6e 74 65 72 28 6d 75 74 65 78 4f 70 65 6e _enter(mutexOpen
15c91 29 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68 );. mutexSh
15c92 61 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 ared = sqlite3Mu
15c93 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
15c94 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
15c95 54 45 52 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 TER);. sqli
15c96 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
15c97 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 mutexShared);.
15c98 20 20 20 20 66 6f 72 28 70 42 74 3d 47 4c 4f 42 for(pBt=GLOB
15c99 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c AL(BtShared*,sql
15c9a 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c ite3SharedCacheL
15c9b 69 73 74 29 3b 20 70 42 74 3b 20 70 42 74 3d 70 ist); pBt; pBt=p
15c9c 42 74 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 Bt->pNext){.
15c9d 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d assert( pBt-
15c9e 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 20 >nRef>0 );.
15c9f 20 20 20 69 66 28 20 30 3d 3d 73 74 72 63 6d 70 if( 0==strcmp
15ca0 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 (zFullPathname,
15ca1 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 sqlite3PagerFile
15ca2 6e 61 6d 65 28 70 42 74 2d 3e 70 50 61 67 65 72 name(pBt->pPager
15ca3 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 )).
15ca4 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 61 && sqlite3Pa
15ca5 67 65 72 56 66 73 28 70 42 74 2d 3e 70 50 61 67 gerVfs(pBt->pPag
15ca6 65 72 29 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 er)==pVfs ){.
15ca7 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a int iDb;.
15ca8 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 44 for(iD
15ca9 62 3d 64 62 2d 3e 6e 44 62 2d 31 3b 20 69 44 62 b=db->nDb-1; iDb
15caa 3e 3d 30 3b 20 69 44 62 2d 2d 29 7b 0a 20 20 20 >=0; iDb--){.
15cab 20 20 20 20 20 20 20 20 20 42 74 72 65 65 20 2a Btree *
15cac 70 45 78 69 73 74 69 6e 67 20 3d 20 64 62 2d 3e pExisting = db->
15cad 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 0a 20 20 aDb[iDb].pBt;.
15cae 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 45 if( pE
15caf 78 69 73 74 69 6e 67 20 26 26 20 70 45 78 69 73 xisting && pExis
15cb0 74 69 6e 67 2d 3e 70 42 74 3d 3d 70 42 74 20 29 ting->pBt==pBt )
15cb1 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
15cb2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
15cb3 61 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 ave(mutexShared)
15cb4 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
15cb5 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
15cb6 61 76 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a ave(mutexOpen);.
15cb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
15cb8 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c lite3_free(zFull
15cb9 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 Pathname);.
15cba 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
15cbb 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 _free(p);.
15cbc 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
15cbd 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 QLITE_CONSTRAINT
15cbe 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
15cbf 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
15cc0 20 20 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 p->pBt = p
15cc1 42 74 3b 0a 20 20 20 20 20 20 20 20 20 20 70 42 Bt;. pB
15cc2 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 t->nRef++;.
15cc3 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
15cc4 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
15cc5 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
15cc6 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 x_leave(mutexSha
15cc7 72 65 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 red);. sqli
15cc8 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 te3_free(zFullPa
15cc9 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 thname);. }.#
15cca 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
15ccb 55 47 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 UG. else{.
15ccc 20 20 20 2f 2a 20 49 6e 20 64 65 62 75 67 20 6d /* In debug m
15ccd 6f 64 65 2c 20 77 65 20 6d 61 72 6b 20 61 6c 6c ode, we mark all
15cce 20 70 65 72 73 69 73 74 65 6e 74 20 64 61 74 61 persistent data
15ccf 62 61 73 65 73 20 61 73 20 73 68 61 72 61 62 6c bases as sharabl
15cd0 65 0a 20 20 20 20 20 20 2a 2a 20 65 76 65 6e 20 e. ** even
15cd1 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 6e 6f when they are no
15cd2 74 2e 20 20 54 68 69 73 20 65 78 65 72 63 69 73 t. This exercis
15cd3 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 es the locking c
15cd4 6f 64 65 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a ode and. **
15cd5 20 67 69 76 65 73 20 6d 6f 72 65 20 6f 70 70 6f gives more oppo
15cd6 72 74 75 6e 69 74 79 20 66 6f 72 20 61 73 73 65 rtunity for asse
15cd7 72 74 73 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 rts(sqlite3_mute
15cd8 78 5f 68 65 6c 64 28 29 29 0a 20 20 20 20 20 20 x_held()).
15cd9 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f ** statements to
15cda 20 66 69 6e 64 20 6c 6f 63 6b 69 6e 67 20 70 72 find locking pr
15cdb 6f 62 6c 65 6d 73 2e 0a 20 20 20 20 20 20 2a 2f oblems.. */
15cdc 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62 . p->sharab
15cdd 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65 le = 1;. }.#e
15cde 6e 64 69 66 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ndif. }.#endif.
15cdf 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a if( pBt==0 ){.
15ce0 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 /*. ** Th
15ce1 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 e following asse
15ce2 72 74 73 20 6d 61 6b 65 20 73 75 72 65 20 74 68 rts make sure th
15ce3 61 74 20 73 74 72 75 63 74 75 72 65 73 20 75 73 at structures us
15ce4 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65 20 ed by the btree
15ce5 61 72 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 are. ** the r
15ce6 69 67 68 74 20 73 69 7a 65 2e 20 20 54 68 69 73 ight size. This
15ce7 20 69 73 20 74 6f 20 67 75 61 72 64 20 61 67 61 is to guard aga
15ce8 69 6e 73 74 20 73 69 7a 65 20 63 68 61 6e 67 65 inst size change
15ce9 73 20 74 68 61 74 20 72 65 73 75 6c 74 0a 20 20 s that result.
15cea 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c ** when compil
15ceb 69 6e 67 20 6f 6e 20 61 20 64 69 66 66 65 72 65 ing on a differe
15cec 6e 74 20 61 72 63 68 69 74 65 63 74 75 72 65 2e nt architecture.
15ced 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
15cee 72 74 28 20 73 69 7a 65 6f 66 28 69 36 34 29 3d rt( sizeof(i64)=
15cef 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 69 36 34 =8 || sizeof(i64
15cf0 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 )==4 );. asse
15cf1 72 74 28 20 73 69 7a 65 6f 66 28 75 36 34 29 3d rt( sizeof(u64)=
15cf2 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 75 36 34 =8 || sizeof(u64
15cf3 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 )==4 );. asse
15cf4 72 74 28 20 73 69 7a 65 6f 66 28 75 33 32 29 3d rt( sizeof(u32)=
15cf5 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =4 );. assert
15cf6 28 20 73 69 7a 65 6f 66 28 75 31 36 29 3d 3d 32 ( sizeof(u16)==2
15cf7 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
15cf8 73 69 7a 65 6f 66 28 50 67 6e 6f 29 3d 3d 34 20 sizeof(Pgno)==4
15cf9 29 3b 0a 20 20 0a 20 20 20 20 70 42 74 20 3d 20 );. . pBt =
15cfa 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
15cfb 6f 28 20 73 69 7a 65 6f 66 28 2a 70 42 74 29 20 o( sizeof(*pBt)
15cfc 29 3b 0a 20 20 20 20 69 66 28 20 70 42 74 3d 3d );. if( pBt==
15cfd 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
15cfe 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
15cff 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f goto btree_o
15d00 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 pen_out;. }.
15d01 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
15d02 61 67 65 72 4f 70 65 6e 28 70 56 66 73 2c 20 26 agerOpen(pVfs, &
15d03 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 46 69 pBt->pPager, zFi
15d04 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 lename,.
15d05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d06 20 20 45 58 54 52 41 5f 53 49 5a 45 2c 20 66 6c EXTRA_SIZE, fl
15d07 61 67 73 2c 20 76 66 73 46 6c 61 67 73 2c 20 70 ags, vfsFlags, p
15d08 61 67 65 52 65 69 6e 69 74 29 3b 0a 20 20 20 20 ageReinit);.
15d09 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
15d0a 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 K ){. rc =
15d0b 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 61 64 sqlite3PagerRead
15d0c 46 69 6c 65 68 65 61 64 65 72 28 70 42 74 2d 3e Fileheader(pBt->
15d0d 70 50 61 67 65 72 2c 73 69 7a 65 6f 66 28 7a 44 pPager,sizeof(zD
15d0e 62 48 65 61 64 65 72 29 2c 7a 44 62 48 65 61 64 bHeader),zDbHead
15d0f 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 er);. }. i
15d10 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
15d11 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 ){. goto b
15d12 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 tree_open_out;.
15d13 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 64 62 }. pBt->db
15d14 20 3d 20 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 = db;. sqlit
15d15 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 e3PagerSetBusyha
15d16 6e 64 6c 65 72 28 70 42 74 2d 3e 70 50 61 67 65 ndler(pBt->pPage
15d17 72 2c 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 r, btreeInvokeBu
15d18 73 79 48 61 6e 64 6c 65 72 2c 20 70 42 74 29 3b syHandler, pBt);
15d19 0a 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 . p->pBt = pB
15d1a 74 3b 0a 20 20 0a 20 20 20 20 70 42 74 2d 3e 70 t;. . pBt->p
15d1b 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 Cursor = 0;.
15d1c 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b pBt->pPage1 = 0;
15d1d 0a 20 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e . pBt->readOn
15d1e 6c 79 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 ly = sqlite3Page
15d1f 72 49 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 2d rIsreadonly(pBt-
15d20 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42 >pPager);. pB
15d21 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 67 65 t->pageSize = ge
15d22 74 32 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 t2byte(&zDbHeade
15d23 72 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 r[16]);. if(
15d24 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 35 31 pBt->pageSize<51
15d25 32 20 7c 7c 20 70 42 74 2d 3e 70 61 67 65 53 69 2 || pBt->pageSi
15d26 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 ze>SQLITE_MAX_PA
15d27 47 45 5f 53 49 5a 45 0a 20 20 20 20 20 20 20 20 GE_SIZE.
15d28 20 7c 7c 20 28 28 70 42 74 2d 3e 70 61 67 65 53 || ((pBt->pageS
15d29 69 7a 65 2d 31 29 26 70 42 74 2d 3e 70 61 67 65 ize-1)&pBt->page
15d2a 53 69 7a 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 Size)!=0 ){.
15d2b 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 pBt->pageSize
15d2c 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c = 0;.#ifndef SQL
15d2d 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
15d2e 55 55 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 UUM. /* If
15d2f 74 68 65 20 6d 61 67 69 63 20 6e 61 6d 65 20 22 the magic name "
15d30 3a 6d 65 6d 6f 72 79 3a 22 20 77 69 6c 6c 20 63 :memory:" will c
15d31 72 65 61 74 65 20 61 6e 20 69 6e 2d 6d 65 6d 6f reate an in-memo
15d32 72 79 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 ry database, the
15d33 6e 0a 20 20 20 20 20 20 2a 2a 20 6c 65 61 76 65 n. ** leave
15d34 20 74 68 65 20 61 75 74 6f 56 61 63 75 75 6d 20 the autoVacuum
15d35 6d 6f 64 65 20 61 74 20 30 20 28 64 6f 20 6e 6f mode at 0 (do no
15d36 74 20 61 75 74 6f 2d 76 61 63 75 75 6d 29 2c 20 t auto-vacuum),
15d37 65 76 65 6e 20 69 66 0a 20 20 20 20 20 20 2a 2a even if. **
15d38 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
15d39 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 74 72 AUTOVACUUM is tr
15d3a 75 65 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 ue. On the other
15d3b 20 68 61 6e 64 2c 20 69 66 0a 20 20 20 20 20 20 hand, if.
15d3c 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d ** SQLITE_OMIT_M
15d3d 45 4d 4f 52 59 44 42 20 68 61 73 20 62 65 65 6e EMORYDB has been
15d3e 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 22 defined, then "
15d3f 3a 6d 65 6d 6f 72 79 3a 22 20 69 73 20 6a 75 73 :memory:" is jus
15d40 74 20 61 0a 20 20 20 20 20 20 2a 2a 20 72 65 67 t a. ** reg
15d41 75 6c 61 72 20 66 69 6c 65 2d 6e 61 6d 65 2e 20 ular file-name.
15d42 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
15d43 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 70 70 auto-vacuum app
15d44 6c 69 65 73 20 61 73 20 70 65 72 20 6e 6f 72 6d lies as per norm
15d45 61 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 al.. */.
15d46 20 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 if( zFilename
15d47 20 26 26 20 21 69 73 4d 65 6d 64 62 20 29 7b 0a && !isMemdb ){.
15d48 20 20 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74 pBt->aut
15d49 6f 56 61 63 75 75 6d 20 3d 20 28 53 51 4c 49 54 oVacuum = (SQLIT
15d4a 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 E_DEFAULT_AUTOVA
15d4b 43 55 55 4d 20 3f 20 31 20 3a 20 30 29 3b 0a 20 CUUM ? 1 : 0);.
15d4c 20 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 pBt->incr
15d4d 56 61 63 75 75 6d 20 3d 20 28 53 51 4c 49 54 45 Vacuum = (SQLITE
15d4e 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 _DEFAULT_AUTOVAC
15d4f 55 55 4d 3d 3d 32 20 3f 20 31 20 3a 20 30 29 3b UUM==2 ? 1 : 0);
15d50 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
15d51 20 20 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d nReserve =
15d52 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 0;. }else{.
15d53 20 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20 nReserve =
15d54 7a 44 62 48 65 61 64 65 72 5b 32 30 5d 3b 0a 20 zDbHeader[20];.
15d55 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 pBt->pageSi
15d56 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 23 69 66 zeFixed = 1;.#if
15d57 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
15d58 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
15d59 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 pBt->autoVacuu
15d5a 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 7a m = (get4byte(&z
15d5b 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20 34 2a DbHeader[36 + 4*
15d5c 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 20 20 4])?1:0);.
15d5d 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 pBt->incrVacuum
15d5e 3d 20 28 67 65 74 34 62 79 74 65 28 26 7a 44 62 = (get4byte(&zDb
15d5f 48 65 61 64 65 72 5b 33 36 20 2b 20 37 2a 34 5d Header[36 + 7*4]
15d60 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 )?1:0);.#endif.
15d61 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 }. rc = sq
15d62 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 lite3PagerSetPag
15d63 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 esize(pBt->pPage
15d64 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a r, &pBt->pageSiz
15d65 65 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 e, nReserve);.
15d66 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
15d67 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a btree_open_out;.
15d68 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 pBt->usableS
15d69 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 ize = pBt->pageS
15d6a 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 65 3b 0a ize - nReserve;.
15d6b 20 20 20 20 61 73 73 65 72 74 28 20 28 70 42 74 assert( (pBt
15d6c 2d 3e 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d ->pageSize & 7)=
15d6d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 62 79 74 65 =0 ); /* 8-byte
15d6e 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 70 61 alignment of pa
15d6f 67 65 53 69 7a 65 20 2a 2f 0a 20 20 20 0a 23 69 geSize */. .#i
15d70 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
15d71 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
15d72 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 CHE) && !defined
15d73 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 (SQLITE_OMIT_DIS
15d74 4b 49 4f 29 0a 20 20 20 20 2f 2a 20 41 64 64 20 KIO). /* Add
15d75 74 68 65 20 6e 65 77 20 42 74 53 68 61 72 65 64 the new BtShared
15d76 20 6f 62 6a 65 63 74 20 74 6f 20 74 68 65 20 6c object to the l
15d77 69 6e 6b 65 64 20 6c 69 73 74 20 73 68 61 72 61 inked list shara
15d78 62 6c 65 20 42 74 53 68 61 72 65 64 73 2e 0a 20 ble BtShareds..
15d79 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d */. if( p-
15d7a 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 >sharable ){.
15d7b 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
15d7c 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 0a 20 *mutexShared;.
15d7d 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 20 3d pBt->nRef =
15d7e 20 31 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 53 1;. mutexS
15d7f 68 61 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d hared = sqlite3M
15d80 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
15d81 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
15d82 53 54 45 52 29 3b 0a 20 20 20 20 20 20 69 66 28 STER);. if(
15d83 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
15d84 46 45 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f FE && sqlite3Glo
15d85 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d balConfig.bCoreM
15d86 75 74 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 utex ){.
15d87 70 42 74 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c pBt->mutex = sql
15d88 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
15d89 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
15d8a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
15d8b 42 74 2d 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a Bt->mutex==0 ){.
15d8c 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
15d8d 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
15d8e 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f db->mallo
15d8f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 cFailed = 0;.
15d90 20 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 goto btre
15d91 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 e_open_out;.
15d92 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
15d93 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
15d94 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61 x_enter(mutexSha
15d95 72 65 64 29 3b 0a 20 20 20 20 20 20 70 42 74 2d red);. pBt-
15d96 3e 70 4e 65 78 74 20 3d 20 47 4c 4f 42 41 4c 28 >pNext = GLOBAL(
15d97 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 BtShared*,sqlite
15d98 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 3SharedCacheList
15d99 29 3b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 );. GLOBAL(
15d9a 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 BtShared*,sqlite
15d9b 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 3SharedCacheList
15d9c 29 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 73 ) = pBt;. s
15d9d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
15d9e 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b ve(mutexShared);
15d9f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
15da0 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 }..#if !defined(
15da1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
15da2 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 ED_CACHE) && !de
15da3 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
15da4 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 20 49 T_DISKIO). /* I
15da5 66 20 74 68 65 20 6e 65 77 20 42 74 72 65 65 20 f the new Btree
15da6 75 73 65 73 20 61 20 73 68 61 72 61 62 6c 65 20 uses a sharable
15da7 70 42 74 53 68 61 72 65 64 2c 20 74 68 65 6e 20 pBtShared, then
15da8 6c 69 6e 6b 20 74 68 65 20 6e 65 77 0a 20 20 2a link the new. *
15da9 2a 20 42 74 72 65 65 20 69 6e 74 6f 20 74 68 65 * Btree into the
15daa 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 73 68 61 list of all sha
15dab 72 61 62 6c 65 20 42 74 72 65 65 73 20 66 6f 72 rable Btrees for
15dac 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 the same connec
15dad 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 65 20 6c tion.. ** The l
15dae 69 73 74 20 69 73 20 6b 65 70 74 20 69 6e 20 61 ist is kept in a
15daf 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 scending order b
15db0 79 20 70 42 74 20 61 64 64 72 65 73 73 2e 0a 20 y pBt address..
15db1 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 */. if( p->sha
15db2 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 rable ){. int
15db3 20 69 3b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 i;. Btree *p
15db4 53 69 62 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 Sib;. for(i=0
15db5 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
15db6 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 53 ){. if( (pS
15db7 69 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e ib = db->aDb[i].
15db8 70 42 74 29 21 3d 30 20 26 26 20 70 53 69 62 2d pBt)!=0 && pSib-
15db9 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 >sharable ){.
15dba 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 while( pSib
15dbb 2d 3e 70 50 72 65 76 20 29 7b 20 70 53 69 62 20 ->pPrev ){ pSib
15dbc 3d 20 70 53 69 62 2d 3e 70 50 72 65 76 3b 20 7d = pSib->pPrev; }
15dbd 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e . if( p->
15dbe 70 42 74 3c 70 53 69 62 2d 3e 70 42 74 20 29 7b pBt<pSib->pBt ){
15dbf 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e . p->pN
15dc0 65 78 74 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 ext = pSib;.
15dc1 20 20 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d p->pPrev =
15dc2 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 0;. pS
15dc3 69 62 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 ib->pPrev = p;.
15dc4 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
15dc5 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 while( p
15dc6 53 69 62 2d 3e 70 4e 65 78 74 20 26 26 20 70 53 Sib->pNext && pS
15dc7 69 62 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3c 70 ib->pNext->pBt<p
15dc8 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 ->pBt ){.
15dc9 20 20 20 20 20 70 53 69 62 20 3d 20 70 53 69 62 pSib = pSib
15dca 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 ->pNext;.
15dcb 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 }. p
15dcc 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 2d 3e ->pNext = pSib->
15dcd 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20 pNext;.
15dce 20 70 2d 3e 70 50 72 65 76 20 3d 20 70 53 69 62 p->pPrev = pSib
15dcf 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
15dd0 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 p->pNext ){.
15dd1 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 p->pNext
15dd2 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 ->pPrev = p;.
15dd3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
15dd4 20 20 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 3d pSib->pNext =
15dd5 20 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 p;. }.
15dd6 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
15dd7 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 }. }. }.#
15dd8 65 6e 64 69 66 0a 20 20 2a 70 70 42 74 72 65 65 endif. *ppBtree
15dd9 20 3d 20 70 3b 0a 0a 62 74 72 65 65 5f 6f 70 65 = p;..btree_ope
15dda 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 21 n_out:. if( rc!
15ddb 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
15ddc 20 20 69 66 28 20 70 42 74 20 26 26 20 70 42 74 if( pBt && pBt
15ddd 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20 20 20 20 ->pPager ){.
15dde 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c sqlite3PagerCl
15ddf 6f 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 ose(pBt->pPager)
15de0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
15de1 74 65 33 5f 66 72 65 65 28 70 42 74 29 3b 0a 20 te3_free(pBt);.
15de2 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
15de3 70 29 3b 0a 20 20 20 20 2a 70 70 42 74 72 65 65 p);. *ppBtree
15de4 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = 0;. }. if(
15de5 6d 75 74 65 78 4f 70 65 6e 20 29 7b 0a 20 20 20 mutexOpen ){.
15de6 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15de7 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 75 74 65 _mutex_held(mute
15de8 78 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 73 71 xOpen) );. sq
15de9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
15dea 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 e(mutexOpen);.
15deb 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
15dec 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e ../*.** Decremen
15ded 74 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e t the BtShared.n
15dee 52 65 66 20 63 6f 75 6e 74 65 72 2e 20 20 57 68 Ref counter. Wh
15def 65 6e 20 69 74 20 72 65 61 63 68 65 73 20 7a 65 en it reaches ze
15df0 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 68 ro,.** remove th
15df1 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 e BtShared struc
15df2 74 75 72 65 20 66 72 6f 6d 20 74 68 65 20 73 68 ture from the sh
15df3 61 72 69 6e 67 20 6c 69 73 74 2e 20 20 52 65 74 aring list. Ret
15df4 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 74 urn.** true if t
15df5 68 65 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 he BtShared.nRef
15df6 20 63 6f 75 6e 74 65 72 20 72 65 61 63 68 65 73 counter reaches
15df7 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e zero and return
15df8 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 74 20 .** false if it
15df9 69 73 20 73 74 69 6c 6c 20 70 6f 73 69 74 69 76 is still positiv
15dfa 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
15dfb 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 removeFromShari
15dfc 6e 67 4c 69 73 74 28 42 74 53 68 61 72 65 64 20 ngList(BtShared
15dfd 2a 70 42 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 *pBt){.#ifndef S
15dfe 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
15dff 44 5f 43 41 43 48 45 0a 20 20 73 71 6c 69 74 65 D_CACHE. sqlite
15e00 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 72 3_mutex *pMaster
15e01 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 4c ;. BtShared *pL
15e02 69 73 74 3b 0a 20 20 69 6e 74 20 72 65 6d 6f 76 ist;. int remov
15e03 65 64 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 ed = 0;.. asser
15e04 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
15e05 5f 6e 6f 74 68 65 6c 64 28 70 42 74 2d 3e 6d 75 _notheld(pBt->mu
15e06 74 65 78 29 20 29 3b 0a 20 20 70 4d 61 73 74 65 tex) );. pMaste
15e07 72 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 r = sqlite3Mutex
15e08 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
15e09 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
15e0a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 );. sqlite3_mut
15e0b 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72 ex_enter(pMaster
15e0c 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 65 66 2d 2d );. pBt->nRef--
15e0d 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e 52 65 ;. if( pBt->nRe
15e0e 66 3c 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 f<=0 ){. if(
15e0f 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a GLOBAL(BtShared*
15e10 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 ,sqlite3SharedCa
15e11 63 68 65 4c 69 73 74 29 3d 3d 70 42 74 20 29 7b cheList)==pBt ){
15e12 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 . GLOBAL(Bt
15e13 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 Shared*,sqlite3S
15e14 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 20 haredCacheList)
15e15 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 = pBt->pNext;.
15e16 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
15e17 4c 69 73 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 List = GLOBAL(Bt
15e18 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 Shared*,sqlite3S
15e19 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b haredCacheList);
15e1a 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 41 4c . while( AL
15e1b 57 41 59 53 28 70 4c 69 73 74 29 20 26 26 20 70 WAYS(pList) && p
15e1c 4c 69 73 74 2d 3e 70 4e 65 78 74 21 3d 70 42 74 List->pNext!=pBt
15e1d 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 ){. pLis
15e1e 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a t=pList->pNext;.
15e1f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
15e20 28 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20 ( ALWAYS(pList)
15e21 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 ){. pList
15e22 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 ->pNext = pBt->p
15e23 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Next;. }.
15e24 20 20 7d 0a 20 20 20 20 69 66 28 20 53 51 4c 49 }. if( SQLI
15e25 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 29 7b TE_THREADSAFE ){
15e26 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
15e27 75 74 65 78 5f 66 72 65 65 28 70 42 74 2d 3e 6d utex_free(pBt->m
15e28 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 utex);. }.
15e29 20 72 65 6d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 removed = 1;.
15e2a 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 }. sqlite3_mute
15e2b 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 65 72 29 x_leave(pMaster)
15e2c 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 6d 6f 76 ;. return remov
15e2d 65 64 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 ed;.#else. retu
15e2e 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a rn 1;.#endif.}..
15e2f 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 /*.** Make sure
15e30 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 70 pBt->pTmpSpace p
15e31 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f oints to an allo
15e32 63 61 74 69 6f 6e 20 6f 66 20 0a 2a 2a 20 4d 58 cation of .** MX
15e33 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 _CELL_SIZE(pBt)
15e34 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 bytes..*/.static
15e35 20 76 6f 69 64 20 61 6c 6c 6f 63 61 74 65 54 65 void allocateTe
15e36 6d 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64 mpSpace(BtShared
15e37 20 2a 70 42 74 29 7b 0a 20 20 69 66 28 20 21 70 *pBt){. if( !p
15e38 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 7b Bt->pTmpSpace ){
15e39 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d 70 53 70 . pBt->pTmpSp
15e3a 61 63 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 ace = sqlite3Pag
15e3b 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61 eMalloc( pBt->pa
15e3c 67 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a 7d 0a geSize );. }.}.
15e3d 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 ./*.** Free the
15e3e 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 61 pBt->pTmpSpace a
15e3f 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 llocation.*/.sta
15e40 74 69 63 20 76 6f 69 64 20 66 72 65 65 54 65 6d tic void freeTem
15e41 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64 20 pSpace(BtShared
15e42 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 *pBt){. sqlite3
15e43 50 61 67 65 46 72 65 65 28 20 70 42 74 2d 3e 70 PageFree( pBt->p
15e44 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 70 42 74 TmpSpace);. pBt
15e45 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 30 3b ->pTmpSpace = 0;
15e46 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 .}../*.** Close
15e47 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 an open database
15e48 20 61 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 20 and invalidate
15e49 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a all cursors..*/.
15e4a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
15e4b 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
15e4c 6c 6f 73 65 28 42 74 72 65 65 20 2a 70 29 7b 0a lose(Btree *p){.
15e4d 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
15e4e 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75 = p->pBt;. BtCu
15e4f 72 73 6f 72 20 2a 70 43 75 72 3b 0a 0a 20 20 2f rsor *pCur;.. /
15e50 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73 * Close all curs
15e51 6f 72 73 20 6f 70 65 6e 65 64 20 76 69 61 20 74 ors opened via t
15e52 68 69 73 20 68 61 6e 64 6c 65 2e 20 20 2a 2f 0a his handle. */.
15e53 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15e54 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
15e55 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
15e56 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
15e57 72 28 70 29 3b 0a 20 20 70 43 75 72 20 3d 20 70 r(p);. pCur = p
15e58 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77 Bt->pCursor;. w
15e59 68 69 6c 65 28 20 70 43 75 72 20 29 7b 0a 20 20 hile( pCur ){.
15e5a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 54 6d 70 BtCursor *pTmp
15e5b 20 3d 20 70 43 75 72 3b 0a 20 20 20 20 70 43 75 = pCur;. pCu
15e5c 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b r = pCur->pNext;
15e5d 0a 20 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 70 . if( pTmp->p
15e5e 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 Btree==p ){.
15e5f 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c sqlite3BtreeCl
15e60 6f 73 65 43 75 72 73 6f 72 28 70 54 6d 70 29 3b oseCursor(pTmp);
15e61 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
15e62 20 52 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 Rollback any ac
15e63 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e tive transaction
15e64 20 61 6e 64 20 66 72 65 65 20 74 68 65 20 68 61 and free the ha
15e65 6e 64 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a ndle structure..
15e66 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f ** The call to
15e67 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c sqlite3BtreeRol
15e68 6c 62 61 63 6b 28 29 20 64 72 6f 70 73 20 61 6e lback() drops an
15e69 79 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65 y table-locks he
15e6a 6c 64 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 ld by. ** this
15e6b 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 handle.. */. s
15e6c 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 qlite3BtreeRollb
15e6d 61 63 6b 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 ack(p);. sqlite
15e6e 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
15e6f 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 . /* If there a
15e70 72 65 20 73 74 69 6c 6c 20 6f 74 68 65 72 20 6f re still other o
15e71 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 utstanding refer
15e72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 73 68 61 ences to the sha
15e73 72 65 64 2d 62 74 72 65 65 0a 20 20 2a 2a 20 73 red-btree. ** s
15e74 74 72 75 63 74 75 72 65 2c 20 72 65 74 75 72 6e tructure, return
15e75 20 6e 6f 77 2e 20 54 68 65 20 72 65 6d 61 69 6e now. The remain
15e76 64 65 72 20 6f 66 20 74 68 69 73 20 70 72 6f 63 der of this proc
15e77 65 64 75 72 65 20 63 6c 65 61 6e 73 20 0a 20 20 edure cleans .
15e78 2a 2a 20 75 70 20 74 68 65 20 73 68 61 72 65 64 ** up the shared
15e79 2d 62 74 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 61 -btree.. */. a
15e7a 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f ssert( p->wantTo
15e7b 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 2d 3e 6c 6f Lock==0 && p->lo
15e7c 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 cked==0 );. if(
15e7d 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c !p->sharable ||
15e7e 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 removeFromShari
15e7f 6e 67 4c 69 73 74 28 70 42 74 29 20 29 7b 0a 20 ngList(pBt) ){.
15e80 20 20 20 2f 2a 20 54 68 65 20 70 42 74 20 69 73 /* The pBt is
15e81 20 6e 6f 20 6c 6f 6e 67 65 72 20 6f 6e 20 74 68 no longer on th
15e82 65 20 73 68 61 72 69 6e 67 20 6c 69 73 74 2c 20 e sharing list,
15e83 73 6f 20 77 65 20 63 61 6e 20 61 63 63 65 73 73 so we can access
15e84 0a 20 20 20 20 2a 2a 20 69 74 20 77 69 74 68 6f . ** it witho
15e85 75 74 20 68 61 76 69 6e 67 20 74 6f 20 68 6f 6c ut having to hol
15e86 64 20 74 68 65 20 6d 75 74 65 78 2e 0a 20 20 20 d the mutex..
15e87 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c 65 61 6e **. ** Clean
15e88 20 6f 75 74 20 61 6e 64 20 64 65 6c 65 74 65 20 out and delete
15e89 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a the BtShared obj
15e8a 65 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ect.. */.
15e8b 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 43 assert( !pBt->pC
15e8c 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 73 71 6c ursor );. sql
15e8d 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 ite3PagerClose(p
15e8e 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 Bt->pPager);.
15e8f 20 69 66 28 20 70 42 74 2d 3e 78 46 72 65 65 53 if( pBt->xFreeS
15e90 63 68 65 6d 61 20 26 26 20 70 42 74 2d 3e 70 53 chema && pBt->pS
15e91 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 70 chema ){. p
15e92 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 28 Bt->xFreeSchema(
15e93 70 42 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 pBt->pSchema);.
15e94 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
15e95 5f 66 72 65 65 28 70 42 74 2d 3e 70 53 63 68 65 _free(pBt->pSche
15e96 6d 61 29 3b 0a 20 20 20 20 66 72 65 65 54 65 6d ma);. freeTem
15e97 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 pSpace(pBt);.
15e98 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 sqlite3_free(pB
15e99 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 t);. }..#ifndef
15e9a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
15e9b 52 45 44 5f 43 41 43 48 45 0a 20 20 61 73 73 65 RED_CACHE. asse
15e9c 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 rt( p->wantToLoc
15e9d 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 k==0 );. assert
15e9e 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 ( p->locked==0 )
15e9f 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 ;. if( p->pPrev
15ea0 20 29 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 ) p->pPrev->pNe
15ea1 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 xt = p->pNext;.
15ea2 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 20 if( p->pNext )
15ea3 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 p->pNext->pPrev
15ea4 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64 = p->pPrev;.#end
15ea5 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 if.. sqlite3_fr
15ea6 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ee(p);. return
15ea7 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
15ea8 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6c .** Change the l
15ea9 69 6d 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 imit on the numb
15eaa 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f er of pages allo
15eab 77 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 wed in the cache
15eac 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 ..**.** The maxi
15ead 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 mum number of ca
15eae 63 68 65 20 70 61 67 65 73 20 69 73 20 73 65 74 che pages is set
15eaf 20 74 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65 to the absolute
15eb0 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 .** value of mxP
15eb1 61 67 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20 age. If mxPage
15eb2 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 is negative, the
15eb3 20 70 61 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f pager will.** o
15eb4 70 65 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e perate asynchron
15eb5 6f 75 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20 ously - it will
15eb6 6e 6f 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66 not stop to do f
15eb7 73 79 6e 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e sync()s.** to in
15eb8 73 75 72 65 20 64 61 74 61 20 69 73 20 77 72 69 sure data is wri
15eb9 74 74 65 6e 20 74 6f 20 74 68 65 20 64 69 73 6b tten to the disk
15eba 20 73 75 72 66 61 63 65 20 62 65 66 6f 72 65 0a surface before.
15ebb 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 ** continuing.
15ebc 54 72 61 6e 73 61 63 74 69 6f 6e 73 20 73 74 69 Transactions sti
15ebd 6c 6c 20 77 6f 72 6b 20 69 66 20 73 79 6e 63 68 ll work if synch
15ebe 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a ronous is off,.*
15ebf 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 * and the databa
15ec0 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72 se cannot be cor
15ec1 72 75 70 74 65 64 20 69 66 20 74 68 69 73 20 70 rupted if this p
15ec2 72 6f 67 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65 rogram.** crashe
15ec3 73 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 6f s. But if the o
15ec4 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
15ec5 63 72 61 73 68 65 73 20 6f 72 20 74 68 65 72 65 crashes or there
15ec6 20 69 73 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74 is.** an abrupt
15ec7 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 power failure w
15ec8 68 65 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 hen synchronous
15ec9 69 73 20 6f 66 66 2c 20 74 68 65 20 64 61 74 61 is off, the data
15eca 62 61 73 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 base.** could be
15ecb 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f left in an inco
15ecc 6e 73 69 73 74 65 6e 74 20 61 6e 64 20 75 6e 72 nsistent and unr
15ecd 65 63 6f 76 65 72 61 62 6c 65 20 73 74 61 74 65 ecoverable state
15ece 2e 0a 2a 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73 ..** Synchronous
15ecf 20 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c is on by defaul
15ed0 74 20 73 6f 20 64 61 74 61 62 61 73 65 20 63 6f t so database co
15ed1 72 72 75 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a rruption is not.
15ed2 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f ** normally a wo
15ed3 72 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 rry..*/.SQLITE_P
15ed4 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
15ed5 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53 e3BtreeSetCacheS
15ed6 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e ize(Btree *p, in
15ed7 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 53 t mxPage){. BtS
15ed8 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
15ed9 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 pBt;. assert( s
15eda 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
15edb 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 d(p->db->mutex)
15edc 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
15edd 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c eEnter(p);. sql
15ede 69 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68 ite3PagerSetCach
15edf 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 esize(pBt->pPage
15ee0 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 r, mxPage);. sq
15ee1 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
15ee2 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c p);. return SQL
15ee3 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
15ee4 20 43 68 61 6e 67 65 20 74 68 65 20 77 61 79 20 Change the way
15ee5 64 61 74 61 20 69 73 20 73 79 6e 63 65 64 20 74 data is synced t
15ee6 6f 20 64 69 73 6b 20 69 6e 20 6f 72 64 65 72 20 o disk in order
15ee7 74 6f 20 69 6e 63 72 65 61 73 65 20 6f 72 20 64 to increase or d
15ee8 65 63 72 65 61 73 65 0a 2a 2a 20 68 6f 77 20 77 ecrease.** how w
15ee9 65 6c 6c 20 74 68 65 20 64 61 74 61 62 61 73 65 ell the database
15eea 20 72 65 73 69 73 74 73 20 64 61 6d 61 67 65 20 resists damage
15eeb 64 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 due to OS crashe
15eec 73 20 61 6e 64 20 70 6f 77 65 72 0a 2a 2a 20 66 s and power.** f
15eed 61 69 6c 75 72 65 73 2e 20 20 4c 65 76 65 6c 20 ailures. Level
15eee 31 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 1 is the same as
15eef 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 20 28 6e asynchronous (n
15ef0 6f 20 73 79 6e 63 73 28 29 20 6f 63 63 75 72 20 o syncs() occur
15ef1 61 6e 64 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 and.** there is
15ef2 61 20 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 a high probabili
15ef3 74 79 20 6f 66 20 64 61 6d 61 67 65 29 20 20 4c ty of damage) L
15ef4 65 76 65 6c 20 32 20 69 73 20 74 68 65 20 64 65 evel 2 is the de
15ef5 66 61 75 6c 74 2e 20 20 54 68 65 72 65 0a 2a 2a fault. There.**
15ef6 20 69 73 20 61 20 76 65 72 79 20 6c 6f 77 20 62 is a very low b
15ef7 75 74 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 ut non-zero prob
15ef8 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 ability of damag
15ef9 65 2e 20 20 4c 65 76 65 6c 20 33 20 72 65 64 75 e. Level 3 redu
15efa 63 65 73 20 74 68 65 0a 2a 2a 20 70 72 6f 62 61 ces the.** proba
15efb 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 bility of damage
15efc 20 74 6f 20 6e 65 61 72 20 7a 65 72 6f 20 62 75 to near zero bu
15efd 74 20 77 69 74 68 20 61 20 77 72 69 74 65 20 70 t with a write p
15efe 65 72 66 6f 72 6d 61 6e 63 65 20 72 65 64 75 63 erformance reduc
15eff 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 tion..*/.#ifndef
15f00 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 SQLITE_OMIT_PAG
15f01 45 52 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 ER_PRAGMAS.SQLIT
15f02 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
15f03 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66 lite3BtreeSetSaf
15f04 65 74 79 4c 65 76 65 6c 28 42 74 72 65 65 20 2a etyLevel(Btree *
15f05 70 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e p, int level, in
15f06 74 20 66 75 6c 6c 53 79 6e 63 29 7b 0a 20 20 42 t fullSync){. B
15f07 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
15f08 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 ->pBt;. assert(
15f09 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15f0a 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 eld(p->db->mutex
15f0b 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ) );. sqlite3Bt
15f0c 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 reeEnter(p);. s
15f0d 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61 qlite3PagerSetSa
15f0e 66 65 74 79 4c 65 76 65 6c 28 70 42 74 2d 3e 70 fetyLevel(pBt->p
15f0f 50 61 67 65 72 2c 20 6c 65 76 65 6c 2c 20 66 75 Pager, level, fu
15f10 6c 6c 53 79 6e 63 29 3b 0a 20 20 73 71 6c 69 74 llSync);. sqlit
15f11 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
15f12 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
15f13 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f _OK;.}.#endif../
15f14 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 *.** Return TRUE
15f15 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 62 74 if the given bt
15f16 72 65 65 20 69 73 20 73 65 74 20 74 6f 20 73 61 ree is set to sa
15f17 66 65 74 79 20 6c 65 76 65 6c 20 31 2e 20 20 49 fety level 1. I
15f18 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 n other.** words
15f19 2c 20 72 65 74 75 72 6e 20 54 52 55 45 20 69 66 , return TRUE if
15f1a 20 6e 6f 20 73 79 6e 63 28 29 20 6f 63 63 75 72 no sync() occur
15f1b 73 20 6f 6e 20 74 68 65 20 64 69 73 6b 20 66 69 s on the disk fi
15f1c 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 les..*/.SQLITE_P
15f1d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
15f1e 65 33 42 74 72 65 65 53 79 6e 63 44 69 73 61 62 e3BtreeSyncDisab
15f1f 6c 65 64 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 led(Btree *p){.
15f20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
15f21 20 70 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 p->pBt;. int r
15f22 63 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c c;. assert( sql
15f23 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
15f24 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
15f25 20 20 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
15f26 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 eEnter(p);. ass
15f27 65 72 74 28 20 70 42 74 20 26 26 20 70 42 74 2d ert( pBt && pBt-
15f28 3e 70 50 61 67 65 72 20 29 3b 0a 20 20 72 63 20 >pPager );. rc
15f29 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f = sqlite3PagerNo
15f2a 73 79 6e 63 28 70 42 74 2d 3e 70 50 61 67 65 72 sync(pBt->pPager
15f2b 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
15f2c 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
15f2d 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 urn rc;.}..#if !
15f2e 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
15f2f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 MIT_PAGER_PRAGMA
15f30 53 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 S) || !defined(S
15f31 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 QLITE_OMIT_VACUU
15f32 4d 29 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 M)./*.** Change
15f33 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 the default page
15f34 73 20 73 69 7a 65 20 61 6e 64 20 74 68 65 20 6e s size and the n
15f35 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 umber of reserve
15f36 64 20 62 79 74 65 73 20 70 65 72 20 70 61 67 65 d bytes per page
15f37 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20 74 68 65 20 ..** Or, if the
15f38 70 61 67 65 20 73 69 7a 65 20 68 61 73 20 61 6c page size has al
15f39 72 65 61 64 79 20 62 65 65 6e 20 66 69 78 65 64 ready been fixed
15f3a 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
15f3b 52 45 41 44 4f 4e 4c 59 20 0a 2a 2a 20 77 69 74 READONLY .** wit
15f3c 68 6f 75 74 20 63 68 61 6e 67 69 6e 67 20 61 6e hout changing an
15f3d 79 74 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 ything..**.** Th
15f3e 65 20 70 61 67 65 20 73 69 7a 65 20 6d 75 73 74 e page size must
15f3f 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 be a power of 2
15f40 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 between 512 and
15f41 20 36 35 35 33 36 2e 20 20 49 66 20 74 68 65 20 65536. If the
15f42 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 73 75 70 page.** size sup
15f43 70 6c 69 65 64 20 64 6f 65 73 20 6e 6f 74 20 6d plied does not m
15f44 65 65 74 20 74 68 69 73 20 63 6f 6e 73 74 72 61 eet this constra
15f45 69 6e 74 20 74 68 65 6e 20 74 68 65 20 70 61 67 int then the pag
15f46 65 20 73 69 7a 65 20 69 73 20 6e 6f 74 0a 2a 2a e size is not.**
15f47 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 changed..**.**
15f48 50 61 67 65 20 73 69 7a 65 73 20 61 72 65 20 63 Page sizes are c
15f49 6f 6e 73 74 72 61 69 6e 65 64 20 74 6f 20 62 65 onstrained to be
15f4a 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 a power of two
15f4b 73 6f 20 74 68 61 74 20 74 68 65 20 72 65 67 69 so that the regi
15f4c 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 on.** of the dat
15f4d 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 abase file used
15f4e 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 28 62 65 67 for locking (beg
15f4f 69 6e 6e 69 6e 67 20 61 74 20 50 45 4e 44 49 4e inning at PENDIN
15f50 47 5f 42 59 54 45 2c 0a 2a 2a 20 74 68 65 20 66 G_BYTE,.** the f
15f51 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 irst byte past t
15f52 68 65 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2c he 1GB boundary,
15f53 20 30 78 34 30 30 30 30 30 30 30 29 20 6e 65 65 0x40000000) nee
15f54 64 73 20 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 61 ds to occur.** a
15f55 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 t the beginning
15f56 6f 66 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a of a page..**.**
15f57 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 6e 52 If parameter nR
15f58 65 73 65 72 76 65 20 69 73 20 6c 65 73 73 20 74 eserve is less t
15f59 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 han zero, then t
15f5a 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 he number of res
15f5b 65 72 76 65 64 0a 2a 2a 20 62 79 74 65 73 20 70 erved.** bytes p
15f5c 65 72 20 70 61 67 65 20 69 73 20 6c 65 66 74 20 er page is left
15f5d 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a unchanged..**.**
15f5e 20 49 66 20 74 68 65 20 69 46 69 78 21 3d 30 20 If the iFix!=0
15f5f 74 68 65 6e 20 74 68 65 20 70 61 67 65 53 69 7a then the pageSiz
15f60 65 46 69 78 65 64 20 66 6c 61 67 20 69 73 20 73 eFixed flag is s
15f61 65 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 et so that the p
15f62 61 67 65 20 73 69 7a 65 0a 2a 2a 20 61 6e 64 20 age size.** and
15f63 61 75 74 6f 76 61 63 75 75 6d 20 6d 6f 64 65 20 autovacuum mode
15f64 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 can no longer be
15f65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c changed..*/.SQL
15f66 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
15f67 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 sqlite3BtreeSetP
15f68 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 ageSize(Btree *p
15f69 2c 20 69 6e 74 20 70 61 67 65 53 69 7a 65 2c 20 , int pageSize,
15f6a 69 6e 74 20 6e 52 65 73 65 72 76 65 2c 20 69 6e int nReserve, in
15f6b 74 20 69 46 69 78 29 7b 0a 20 20 69 6e 74 20 72 t iFix){. int r
15f6c 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
15f6d 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
15f6e 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 p->pBt;. asser
15f6f 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 2d 31 20 t( nReserve>=-1
15f70 26 26 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 && nReserve<=255
15f71 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 );. sqlite3Btr
15f72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 eeEnter(p);. if
15f73 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 ( pBt->pageSizeF
15f74 69 78 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 ixed ){. sqli
15f75 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
15f76 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
15f77 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 ITE_READONLY;.
15f78 7d 0a 20 20 69 66 28 20 6e 52 65 73 65 72 76 65 }. if( nReserve
15f79 3c 30 20 29 7b 0a 20 20 20 20 6e 52 65 73 65 72 <0 ){. nReser
15f7a 76 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 ve = pBt->pageSi
15f7b 7a 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65 ze - pBt->usable
15f7c 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 Size;. }. asse
15f7d 72 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 rt( nReserve>=0
15f7e 26 26 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 && nReserve<=255
15f7f 20 29 3b 0a 20 20 69 66 28 20 70 61 67 65 53 69 );. if( pageSi
15f80 7a 65 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53 ze>=512 && pageS
15f81 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f ize<=SQLITE_MAX_
15f82 50 41 47 45 5f 53 49 5a 45 20 26 26 0a 20 20 20 PAGE_SIZE &&.
15f83 20 20 20 20 20 28 28 70 61 67 65 53 69 7a 65 2d ((pageSize-
15f84 31 29 26 70 61 67 65 53 69 7a 65 29 3d 3d 30 20 1)&pageSize)==0
15f85 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 ){. assert( (
15f86 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 pageSize & 7)==0
15f87 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
15f88 21 70 42 74 2d 3e 70 50 61 67 65 31 20 26 26 20 !pBt->pPage1 &&
15f89 21 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b !pBt->pCursor );
15f8a 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 . pBt->pageSi
15f8b 7a 65 20 3d 20 28 75 31 36 29 70 61 67 65 53 69 ze = (u16)pageSi
15f8c 7a 65 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 ze;. freeTemp
15f8d 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 7d 0a Space(pBt);. }.
15f8e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
15f8f 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 gerSetPagesize(p
15f90 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 Bt->pPager, &pBt
15f91 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6e 52 65 73 ->pageSize, nRes
15f92 65 72 76 65 29 3b 0a 20 20 70 42 74 2d 3e 75 73 erve);. pBt->us
15f93 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e ableSize = pBt->
15f94 70 61 67 65 53 69 7a 65 20 2d 20 28 75 31 36 29 pageSize - (u16)
15f95 6e 52 65 73 65 72 76 65 3b 0a 20 20 69 66 28 20 nReserve;. if(
15f96 69 46 69 78 20 29 20 70 42 74 2d 3e 70 61 67 65 iFix ) pBt->page
15f97 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 20 SizeFixed = 1;.
15f98 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
15f99 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
15f9a 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 rc;.}../*.** Ret
15f9b 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 6c urn the currentl
15f9c 79 20 64 65 66 69 6e 65 64 20 70 61 67 65 20 73 y defined page s
15f9d 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ize.*/.SQLITE_PR
15f9e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
15f9f 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 3BtreeGetPageSiz
15fa0 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 e(Btree *p){. r
15fa1 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 61 eturn p->pBt->pa
15fa2 67 65 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a geSize;.}../*.**
15fa3 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
15fa4 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 er of bytes of s
15fa5 70 61 63 65 20 61 74 20 74 68 65 20 65 6e 64 20 pace at the end
15fa6 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 74 68 of every page th
15fa7 61 74 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 74 at.** are intent
15fa8 75 61 6c 6c 79 20 6c 65 66 74 20 75 6e 75 73 65 ually left unuse
15fa9 64 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 d. This is the
15faa 22 72 65 73 65 72 76 65 64 22 20 73 70 61 63 65 "reserved" space
15fab 20 74 68 61 74 20 69 73 0a 2a 2a 20 73 6f 6d 65 that is.** some
15fac 74 69 6d 65 73 20 75 73 65 64 20 62 79 20 65 78 times used by ex
15fad 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c tensions..*/.SQL
15fae 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
15faf 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52 sqlite3BtreeGetR
15fb0 65 73 65 72 76 65 28 42 74 72 65 65 20 2a 70 29 eserve(Btree *p)
15fb1 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c {. int n;. sql
15fb2 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
15fb3 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e 70 42 74 2d );. n = p->pBt-
15fb4 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 2d 3e 70 >pageSize - p->p
15fb5 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a Bt->usableSize;.
15fb6 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
15fb7 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
15fb8 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 n;.}../*.** Set
15fb9 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 the maximum pag
15fba 65 20 63 6f 75 6e 74 20 66 6f 72 20 61 20 64 61 e count for a da
15fbb 74 61 62 61 73 65 20 69 66 20 6d 78 50 61 67 65 tabase if mxPage
15fbc 20 69 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a is positive..**
15fbd 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 No changes are
15fbe 6d 61 64 65 20 69 66 20 6d 78 50 61 67 65 20 69 made if mxPage i
15fbf 73 20 30 20 6f 72 20 6e 65 67 61 74 69 76 65 2e s 0 or negative.
15fc0 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f .** Regardless o
15fc1 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d f the value of m
15fc2 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 xPage, return th
15fc3 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 e maximum page c
15fc4 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ount..*/.SQLITE_
15fc5 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
15fc6 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65 43 te3BtreeMaxPageC
15fc7 6f 75 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69 ount(Btree *p, i
15fc8 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 6e nt mxPage){. in
15fc9 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 t n;. sqlite3Bt
15fca 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e reeEnter(p);. n
15fcb 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d = sqlite3PagerM
15fcc 61 78 50 61 67 65 43 6f 75 6e 74 28 70 2d 3e 70 axPageCount(p->p
15fcd 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d 78 50 61 Bt->pPager, mxPa
15fce 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ge);. sqlite3Bt
15fcf 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
15fd0 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23 65 6e 64 69 eturn n;.}.#endi
15fd1 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 f /* !defined(SQ
15fd2 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f LITE_OMIT_PAGER_
15fd3 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 PRAGMAS) || !def
15fd4 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
15fd5 5f 56 41 43 55 55 4d 29 20 2a 2f 0a 0a 2f 2a 0a _VACUUM) */../*.
15fd6 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 27 61 ** Change the 'a
15fd7 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 uto-vacuum' prop
15fd8 65 72 74 79 20 6f 66 20 74 68 65 20 64 61 74 61 erty of the data
15fd9 62 61 73 65 2e 20 49 66 20 74 68 65 20 27 61 75 base. If the 'au
15fda 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 20 70 61 72 toVacuum'.** par
15fdb 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 ameter is non-ze
15fdc 72 6f 2c 20 74 68 65 6e 20 61 75 74 6f 2d 76 61 ro, then auto-va
15fdd 63 75 75 6d 20 6d 6f 64 65 20 69 73 20 65 6e 61 cuum mode is ena
15fde 62 6c 65 64 2e 20 49 66 20 7a 65 72 6f 2c 20 69 bled. If zero, i
15fdf 74 0a 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64 t.** is disabled
15fe0 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 . The default va
15fe1 6c 75 65 20 66 6f 72 20 74 68 65 20 61 75 74 6f lue for the auto
15fe2 2d 76 61 63 75 75 6d 20 70 72 6f 70 65 72 74 79 -vacuum property
15fe3 20 69 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e is .** determin
15fe4 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 ed by the SQLITE
15fe5 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 _DEFAULT_AUTOVAC
15fe6 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 53 51 UUM macro..*/.SQ
15fe7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
15fe8 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 sqlite3BtreeSet
15fe9 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 AutoVacuum(Btree
15fea 20 2a 70 2c 20 69 6e 74 20 61 75 74 6f 56 61 63 *p, int autoVac
15feb 75 75 6d 29 7b 0a 23 69 66 64 65 66 20 53 51 4c uum){.#ifdef SQL
15fec 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
15fed 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c UUM. return SQL
15fee 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 23 65 ITE_READONLY;.#e
15fef 6c 73 65 0a 20 20 42 74 53 68 61 72 65 64 20 2a lse. BtShared *
15ff0 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
15ff1 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
15ff2 4f 4b 3b 0a 20 20 75 38 20 61 76 20 3d 20 28 75 OK;. u8 av = (u
15ff3 38 29 61 75 74 6f 56 61 63 75 75 6d 3b 0a 0a 20 8)autoVacuum;..
15ff4 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
15ff5 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70 42 74 er(p);. if( pBt
15ff6 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 ->pageSizeFixed
15ff7 26 26 20 28 61 76 20 3f 31 3a 30 29 21 3d 70 42 && (av ?1:0)!=pB
15ff8 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b t->autoVacuum ){
15ff9 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
15ffa 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 65 6c _READONLY;. }el
15ffb 73 65 7b 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 se{. pBt->aut
15ffc 6f 56 61 63 75 75 6d 20 3d 20 61 76 20 3f 31 3a oVacuum = av ?1:
15ffd 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 0;. pBt->incr
15ffe 56 61 63 75 75 6d 20 3d 20 61 76 3d 3d 32 20 3f Vacuum = av==2 ?
15fff 31 3a 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 1:0;. }. sqlit
16000 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
16001 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 . return rc;.#e
16002 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ndif.}../*.** Re
16003 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f turn the value o
16004 66 20 74 68 65 20 27 61 75 74 6f 2d 76 61 63 75 f the 'auto-vacu
16005 75 6d 27 20 70 72 6f 70 65 72 74 79 2e 20 49 66 um' property. If
16006 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 auto-vacuum is
16007 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 31 20 69 73 .** enabled 1 is
16008 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 returned. Other
16009 77 69 73 65 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 wise 0..*/.SQLIT
1600a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1600b 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 75 74 lite3BtreeGetAut
1600c 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70 oVacuum(Btree *p
1600d 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ){.#ifdef SQLITE
1600e 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
1600f 0a 20 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f . return BTREE_
16010 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b AUTOVACUUM_NONE;
16011 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 72 63 3b .#else. int rc;
16012 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
16013 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 nter(p);. rc =
16014 28 0a 20 20 20 20 28 21 70 2d 3e 70 42 74 2d 3e (. (!p->pBt->
16015 61 75 74 6f 56 61 63 75 75 6d 29 3f 42 54 52 45 autoVacuum)?BTRE
16016 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e E_AUTOVACUUM_NON
16017 45 3a 0a 20 20 20 20 28 21 70 2d 3e 70 42 74 2d E:. (!p->pBt-
16018 3e 69 6e 63 72 56 61 63 75 75 6d 29 3f 42 54 52 >incrVacuum)?BTR
16019 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 EE_AUTOVACUUM_FU
1601a 4c 4c 3a 0a 20 20 20 20 42 54 52 45 45 5f 41 55 LL:. BTREE_AU
1601b 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 0a 20 20 TOVACUUM_INCR.
1601c 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
1601d 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
1601e 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d urn rc;.#endif.}
1601f 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 72 .../*.** Get a r
16020 65 66 65 72 65 6e 63 65 20 74 6f 20 70 50 61 67 eference to pPag
16021 65 31 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 e1 of the databa
16022 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 77 se file. This w
16023 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 61 63 71 75 ill.** also acqu
16024 69 72 65 20 61 20 72 65 61 64 6c 6f 63 6b 20 6f ire a readlock o
16025 6e 20 74 68 61 74 20 66 69 6c 65 2e 0a 2a 2a 0a n that file..**.
16026 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
16027 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 returned on succ
16028 65 73 73 2e 20 20 49 66 20 74 68 65 20 66 69 6c ess. If the fil
16029 65 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 77 65 e is not a.** we
1602a 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 ll-formed databa
1602b 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 se file, then SQ
1602c 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 73 20 LITE_CORRUPT is
1602d 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 53 51 4c returned..** SQL
1602e 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 ITE_BUSY is retu
1602f 72 6e 65 64 20 69 66 20 74 68 65 20 64 61 74 61 rned if the data
16030 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 base is locked.
16031 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a SQLITE_NOMEM.**
16032 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
16033 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 we run out of me
16034 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 mory. .*/.static
16035 20 69 6e 74 20 6c 6f 63 6b 42 74 72 65 65 28 42 int lockBtree(B
16036 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 tShared *pBt){.
16037 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 int rc;. MemPa
16038 67 65 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e ge *pPage1;. in
16039 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 t nPage;.. asse
1603a 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1603b 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
1603c 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
1603d 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20 29 pBt->pPage1==0 )
1603e 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1603f 50 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 PagerSharedLock(
16040 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 pBt->pPager);.
16041 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
16042 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 K ) return rc;.
16043 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 rc = btreeGetPa
16044 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67 ge(pBt, 1, &pPag
16045 65 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 e1, 0);. if( rc
16046 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 !=SQLITE_OK ) re
16047 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 turn rc;.. /* D
16048 6f 20 73 6f 6d 65 20 63 68 65 63 6b 69 6e 67 20 o some checking
16049 74 6f 20 68 65 6c 70 20 69 6e 73 75 72 65 20 74 to help insure t
1604a 68 65 20 66 69 6c 65 20 77 65 20 6f 70 65 6e 65 he file we opene
1604b 64 20 72 65 61 6c 6c 79 20 69 73 0a 20 20 2a 2a d really is. **
1604c 20 61 20 76 61 6c 69 64 20 64 61 74 61 62 61 73 a valid databas
1604d 65 20 66 69 6c 65 2e 20 0a 20 20 2a 2f 0a 20 20 e file. . */.
1604e 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
1604f 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e rPagecount(pBt->
16050 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b pPager, &nPage);
16051 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
16052 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f E_OK ){. goto
16053 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c page1_init_fail
16054 65 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 ed;. }else if(
16055 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 69 nPage>0 ){. i
16056 6e 74 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 nt pageSize;.
16057 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b int usableSize;
16058 0a 20 20 20 20 75 38 20 2a 70 61 67 65 31 20 3d . u8 *page1 =
16059 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a pPage1->aData;.
1605a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1605b 4e 4f 54 41 44 42 3b 0a 20 20 20 20 69 66 28 20 NOTADB;. if(
1605c 6d 65 6d 63 6d 70 28 70 61 67 65 31 2c 20 7a 4d memcmp(page1, zM
1605d 61 67 69 63 48 65 61 64 65 72 2c 20 31 36 29 21 agicHeader, 16)!
1605e 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f =0 ){. goto
1605f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c page1_init_fail
16060 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ed;. }. if
16061 28 20 70 61 67 65 31 5b 31 38 5d 3e 31 20 29 7b ( page1[18]>1 ){
16062 0a 20 20 20 20 20 20 70 42 74 2d 3e 72 65 61 64 . pBt->read
16063 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 20 20 7d 0a Only = 1;. }.
16064 20 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 39 if( page1[19
16065 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 ]>1 ){. got
16066 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 o page1_init_fai
16067 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 led;. }..
16068 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 65 /* The maximum e
16069 6d 62 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e mbedded fraction
1606a 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 must be exactly
1606b 20 32 35 25 2e 20 20 41 6e 64 20 74 68 65 20 6d 25%. And the m
1606c 69 6e 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 65 6d inimum. ** em
1606d 62 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e 20 bedded fraction
1606e 6d 75 73 74 20 62 65 20 31 32 2e 35 25 20 66 6f must be 12.5% fo
1606f 72 20 62 6f 74 68 20 6c 65 61 66 2d 64 61 74 61 r both leaf-data
16070 20 61 6e 64 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 and non-leaf-da
16071 74 61 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f ta.. ** The o
16072 72 69 67 69 6e 61 6c 20 64 65 73 69 67 6e 20 61 riginal design a
16073 6c 6c 6f 77 65 64 20 74 68 65 73 65 20 61 6d 6f llowed these amo
16074 75 6e 74 73 20 74 6f 20 76 61 72 79 2c 20 62 75 unts to vary, bu
16075 74 20 61 73 20 6f 66 0a 20 20 20 20 2a 2a 20 76 t as of. ** v
16076 65 72 73 69 6f 6e 20 33 2e 36 2e 30 2c 20 77 65 ersion 3.6.0, we
16077 20 72 65 71 75 69 72 65 20 74 68 65 6d 20 74 6f require them to
16078 20 62 65 20 66 69 78 65 64 2e 0a 20 20 20 20 2a be fixed.. *
16079 2f 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 /. if( memcmp
1607a 28 26 70 61 67 65 31 5b 32 31 5d 2c 20 22 5c 31 (&page1[21], "\1
1607b 30 30 5c 30 34 30 5c 30 34 30 22 2c 33 29 21 3d 00\040\040",3)!=
1607c 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 0 ){. goto
1607d 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 page1_init_faile
1607e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 67 d;. }. pag
1607f 65 53 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 eSize = get2byte
16080 28 26 70 61 67 65 31 5b 31 36 5d 29 3b 0a 20 20 (&page1[16]);.
16081 20 20 69 66 28 20 28 28 70 61 67 65 53 69 7a 65 if( ((pageSize
16082 2d 31 29 26 70 61 67 65 53 69 7a 65 29 21 3d 30 -1)&pageSize)!=0
16083 20 7c 7c 20 70 61 67 65 53 69 7a 65 3c 35 31 32 || pageSize<512
16084 20 7c 7c 0a 20 20 20 20 20 20 20 20 28 53 51 4c ||. (SQL
16085 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a ITE_MAX_PAGE_SIZ
16086 45 3c 33 32 37 36 38 20 26 26 20 70 61 67 65 53 E<32768 && pageS
16087 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 ize>SQLITE_MAX_P
16088 41 47 45 5f 53 49 5a 45 29 0a 20 20 20 20 29 7b AGE_SIZE). ){
16089 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 . goto page
1608a 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 1_init_failed;.
1608b 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1608c 20 28 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d (pageSize & 7)=
1608d 3d 30 20 29 3b 0a 20 20 20 20 75 73 61 62 6c 65 =0 );. usable
1608e 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 20 Size = pageSize
1608f 2d 20 70 61 67 65 31 5b 32 30 5d 3b 0a 20 20 20 - page1[20];.
16090 20 69 66 28 20 70 61 67 65 53 69 7a 65 21 3d 70 if( pageSize!=p
16091 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a Bt->pageSize ){.
16092 20 20 20 20 20 20 2f 2a 20 41 66 74 65 72 20 72 /* After r
16093 65 61 64 69 6e 67 20 74 68 65 20 66 69 72 73 74 eading the first
16094 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 page of the dat
16095 61 62 61 73 65 20 61 73 73 75 6d 69 6e 67 20 61 abase assuming a
16096 20 70 61 67 65 20 73 69 7a 65 0a 20 20 20 20 20 page size.
16097 20 2a 2a 20 6f 66 20 42 74 53 68 61 72 65 64 2e ** of BtShared.
16098 70 61 67 65 53 69 7a 65 2c 20 77 65 20 68 61 76 pageSize, we hav
16099 65 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 e discovered tha
1609a 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 t the page-size
1609b 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 63 74 75 is. ** actu
1609c 61 6c 6c 79 20 70 61 67 65 53 69 7a 65 2e 20 55 ally pageSize. U
1609d 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 nlock the databa
1609e 73 65 2c 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 se, leave pBt->p
1609f 50 61 67 65 31 20 61 74 0a 20 20 20 20 20 20 2a Page1 at. *
160a0 2a 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 * zero and retur
160a1 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 65 n SQLITE_OK. The
160a2 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 63 61 6c caller will cal
160a3 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a l this function.
160a4 20 20 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 ** again w
160a5 69 74 68 20 74 68 65 20 63 6f 72 72 65 63 74 20 ith the correct
160a6 70 61 67 65 2d 73 69 7a 65 2e 0a 20 20 20 20 20 page-size..
160a7 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 73 */. releas
160a8 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20 ePage(pPage1);.
160a9 20 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 pBt->usable
160aa 53 69 7a 65 20 3d 20 28 75 31 36 29 75 73 61 62 Size = (u16)usab
160ab 6c 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 70 42 leSize;. pB
160ac 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 t->pageSize = (u
160ad 31 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 16)pageSize;.
160ae 20 20 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 freeTempSpace
160af 28 70 42 74 29 3b 0a 20 20 20 20 20 20 72 63 20 (pBt);. rc
160b0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 = sqlite3PagerSe
160b1 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 tPagesize(pBt->p
160b2 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 Pager, &pBt->pag
160b3 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 eSize,.
160b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
160b5 20 20 20 20 20 20 20 20 20 20 70 61 67 65 53 69 pageSi
160b6 7a 65 2d 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a ze-usableSize);.
160b7 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
160b8 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 . }. if( u
160b9 73 61 62 6c 65 53 69 7a 65 3c 34 38 30 20 29 7b sableSize<480 ){
160ba 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 . goto page
160bb 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 1_init_failed;.
160bc 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 70 61 }. pBt->pa
160bd 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61 geSize = (u16)pa
160be 67 65 53 69 7a 65 3b 0a 20 20 20 20 70 42 74 2d geSize;. pBt-
160bf 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 28 75 >usableSize = (u
160c0 31 36 29 75 73 61 62 6c 65 53 69 7a 65 3b 0a 23 16)usableSize;.#
160c1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
160c2 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
160c3 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 pBt->autoVacuu
160c4 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 m = (get4byte(&p
160c5 61 67 65 31 5b 33 36 20 2b 20 34 2a 34 5d 29 3f age1[36 + 4*4])?
160c6 31 3a 30 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 1:0);. pBt->i
160c7 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74 ncrVacuum = (get
160c8 34 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 4byte(&page1[36
160c9 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 + 7*4])?1:0);.#e
160ca 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 6d ndif. }.. /* m
160cb 61 78 4c 6f 63 61 6c 20 69 73 20 74 68 65 20 6d axLocal is the m
160cc 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 aximum amount of
160cd 20 70 61 79 6c 6f 61 64 20 74 6f 20 73 74 6f 72 payload to stor
160ce 65 20 6c 6f 63 61 6c 6c 79 20 66 6f 72 0a 20 20 e locally for.
160cf 2a 2a 20 61 20 63 65 6c 6c 2e 20 20 4d 61 6b 65 ** a cell. Make
160d0 20 73 75 72 65 20 69 74 20 69 73 20 73 6d 61 6c sure it is smal
160d1 6c 20 65 6e 6f 75 67 68 20 73 6f 20 74 68 61 74 l enough so that
160d2 20 61 74 20 6c 65 61 73 74 20 6d 69 6e 46 61 6e at least minFan
160d3 6f 75 74 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 63 out. ** cells c
160d4 61 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 6f an will fit on o
160d5 6e 65 20 70 61 67 65 2e 20 20 57 65 20 61 73 73 ne page. We ass
160d6 75 6d 65 20 61 20 31 30 2d 62 79 74 65 20 70 61 ume a 10-byte pa
160d7 67 65 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20 ge header.. **
160d8 42 65 73 69 64 65 73 20 74 68 65 20 70 61 79 6c Besides the payl
160d9 6f 61 64 2c 20 74 68 65 20 63 65 6c 6c 20 6d 75 oad, the cell mu
160da 73 74 20 73 74 6f 72 65 3a 0a 20 20 2a 2a 20 20 st store:. **
160db 20 20 20 32 2d 62 79 74 65 20 70 6f 69 6e 74 65 2-byte pointe
160dc 72 20 74 6f 20 74 68 65 20 63 65 6c 6c 0a 20 20 r to the cell.
160dd 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 63 68 ** 4-byte ch
160de 69 6c 64 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a ild pointer. **
160df 20 20 20 20 20 39 2d 62 79 74 65 20 6e 4b 65 79 9-byte nKey
160e0 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 value. **
160e1 34 2d 62 79 74 65 20 6e 44 61 74 61 20 76 61 6c 4-byte nData val
160e2 75 65 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 ue. ** 4-by
160e3 74 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 te overflow page
160e4 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 53 6f pointer. ** So
160e5 20 61 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73 a cell consists
160e6 20 6f 66 20 61 20 32 2d 62 79 74 65 20 70 6f 69 of a 2-byte poi
160e7 6e 65 72 2c 20 61 20 68 65 61 64 65 72 20 77 68 ner, a header wh
160e8 69 63 68 20 69 73 20 61 73 20 6d 75 63 68 20 61 ich is as much a
160e9 73 0a 20 20 2a 2a 20 31 37 20 62 79 74 65 73 20 s. ** 17 bytes
160ea 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e 20 62 79 74 long, 0 to N byt
160eb 65 73 20 6f 66 20 70 61 79 6c 6f 61 64 2c 20 61 es of payload, a
160ec 6e 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 34 nd an optional 4
160ed 20 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 0a 20 byte overflow.
160ee 20 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 65 72 ** page pointer
160ef 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 2d 3e 6d 61 .. */. pBt->ma
160f0 78 4c 6f 63 61 6c 20 3d 20 28 70 42 74 2d 3e 75 xLocal = (pBt->u
160f1 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 36 34 sableSize-12)*64
160f2 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 70 42 74 /255 - 23;. pBt
160f3 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 28 70 42 ->minLocal = (pB
160f4 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 t->usableSize-12
160f5 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 )*32/255 - 23;.
160f6 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 3d 20 pBt->maxLeaf =
160f7 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
160f8 2d 20 33 35 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e - 35;. pBt->min
160f9 4c 65 61 66 20 3d 20 28 70 42 74 2d 3e 75 73 61 Leaf = (pBt->usa
160fa 62 6c 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32 bleSize-12)*32/2
160fb 35 35 20 2d 20 32 33 3b 0a 20 20 61 73 73 65 72 55 - 23;. asser
160fc 74 28 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 t( pBt->maxLeaf
160fd 2b 20 32 33 20 3c 3d 20 4d 58 5f 43 45 4c 4c 5f + 23 <= MX_CELL_
160fe 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20 70 SIZE(pBt) );. p
160ff 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61 Bt->pPage1 = pPa
16100 67 65 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 ge1;. return SQ
16101 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 31 5f LITE_OK;..page1_
16102 69 6e 69 74 5f 66 61 69 6c 65 64 3a 0a 20 20 72 init_failed:. r
16103 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 eleasePage(pPage
16104 31 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 1);. pBt->pPage
16105 31 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 1 = 0;. return
16106 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 rc;.}../*.** If
16107 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 there are no out
16108 73 74 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73 standing cursors
16109 20 61 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 and we are not
1610a 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 0a 2a 2a in the middle.**
1610b 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f of a transactio
1610c 6e 20 62 75 74 20 74 68 65 72 65 20 69 73 20 61 n but there is a
1610d 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 read lock on th
1610e 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e e database, then
1610f 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 .** this routine
16110 20 75 6e 72 65 66 73 20 74 68 65 20 66 69 72 73 unrefs the firs
16111 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 t page of the da
16112 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 69 63 tabase file whic
16113 68 20 0a 2a 2a 20 68 61 73 20 74 68 65 20 65 66 h .** has the ef
16114 66 65 63 74 20 6f 66 20 72 65 6c 65 61 73 69 6e fect of releasin
16115 67 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e g the read lock.
16116 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 .**.** If there
16117 69 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e is a transaction
16118 20 69 6e 20 70 72 6f 67 72 65 73 73 2c 20 74 68 in progress, th
16119 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
1611a 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 no-op..*/.static
1611b 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65 void unlockBtre
1611c 65 49 66 55 6e 75 73 65 64 28 42 74 53 68 61 72 eIfUnused(BtShar
1611d 65 64 20 2a 70 42 74 29 7b 0a 20 20 61 73 73 65 ed *pBt){. asse
1611e 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1611f 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
16120 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
16121 70 42 74 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 pBt->pCursor==0
16122 7c 7c 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 || pBt->inTransa
16123 63 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e 45 ction>TRANS_NONE
16124 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 );. if( pBt->i
16125 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 nTransaction==TR
16126 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d ANS_NONE && pBt-
16127 3e 70 50 61 67 65 31 21 3d 30 20 29 7b 0a 20 20 >pPage1!=0 ){.
16128 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 assert( pBt->p
16129 50 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a Page1->aData );.
1612a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
1612b 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 te3PagerRefcount
1612c 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 31 (pBt->pPager)==1
1612d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1612e 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 pBt->pPage1->aDa
1612f 74 61 20 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 ta );. releas
16130 65 50 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 ePage(pBt->pPage
16131 31 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 1);. pBt->pPa
16132 67 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a ge1 = 0;. }.}..
16133 2f 2a 0a 2a 2a 20 49 66 20 70 42 74 20 70 6f 69 /*.** If pBt poi
16134 6e 74 73 20 74 6f 20 61 6e 20 65 6d 70 74 79 20 nts to an empty
16135 66 69 6c 65 20 74 68 65 6e 20 63 6f 6e 76 65 72 file then conver
16136 74 20 74 68 61 74 20 65 6d 70 74 79 20 66 69 6c t that empty fil
16137 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e 65 77 20 e.** into a new
16138 65 6d 70 74 79 20 64 61 74 61 62 61 73 65 20 62 empty database b
16139 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 y initializing t
1613a 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 he first page of
1613b 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
1613c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1613d 6e 65 77 44 61 74 61 62 61 73 65 28 42 74 53 68 newDatabase(BtSh
1613e 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65 ared *pBt){. Me
1613f 6d 50 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 6e mPage *pP1;. un
16140 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 signed char *dat
16141 61 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 a;. int rc;. i
16142 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73 nt nPage;.. ass
16143 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
16144 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
16145 65 78 29 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 ex) );. /* The
16146 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 68 61 database size ha
16147 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6d s already been m
16148 65 61 73 75 72 65 64 20 61 6e 64 20 63 61 63 68 easured and cach
16149 65 64 2c 20 73 6f 20 66 61 69 6c 75 72 65 0a 20 ed, so failure.
1614a 20 2a 2a 20 69 73 20 69 6d 70 6f 73 73 69 62 6c ** is impossibl
1614b 65 20 68 65 72 65 2e 20 20 49 66 20 74 68 65 20 e here. If the
1614c 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 20 6d 65 original size me
1614d 61 73 75 72 65 6d 65 6e 74 20 66 61 69 6c 65 64 asurement failed
1614e 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 70 72 6f 63 , then. ** proc
1614f 65 73 73 69 6e 67 20 61 62 6f 72 74 73 20 62 65 essing aborts be
16150 66 6f 72 65 20 65 6e 74 65 72 69 6e 67 20 74 68 fore entering th
16151 69 73 20 72 6f 75 74 69 6e 65 2e 20 2a 2f 0a 20 is routine. */.
16152 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
16153 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d erPagecount(pBt-
16154 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 >pPager, &nPage)
16155 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 ;. if( NEVER(rc
16156 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 7c 7c 20 !=SQLITE_OK) ||
16157 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 72 nPage>0 ){. r
16158 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
16159 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 pP1 = pBt->pPage
1615a 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 31 1;. assert( pP1
1615b 21 3d 30 20 29 3b 0a 20 20 64 61 74 61 20 3d 20 !=0 );. data =
1615c 70 50 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63 pP1->aData;. rc
1615d 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
1615e 72 69 74 65 28 70 50 31 2d 3e 70 44 62 50 61 67 rite(pP1->pDbPag
1615f 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 e);. if( rc ) r
16160 65 74 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 63 eturn rc;. memc
16161 70 79 28 64 61 74 61 2c 20 7a 4d 61 67 69 63 48 py(data, zMagicH
16162 65 61 64 65 72 2c 20 73 69 7a 65 6f 66 28 7a 4d eader, sizeof(zM
16163 61 67 69 63 48 65 61 64 65 72 29 29 3b 0a 20 20 agicHeader));.
16164 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 7a assert( sizeof(z
16165 4d 61 67 69 63 48 65 61 64 65 72 29 3d 3d 31 36 MagicHeader)==16
16166 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 );. put2byte(&
16167 64 61 74 61 5b 31 36 5d 2c 20 70 42 74 2d 3e 70 data[16], pBt->p
16168 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 ageSize);. data
16169 5b 31 38 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 [18] = 1;. data
1616a 5b 31 39 5d 20 3d 20 31 3b 0a 20 20 61 73 73 65 [19] = 1;. asse
1616b 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 rt( pBt->usableS
1616c 69 7a 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 ize<=pBt->pageSi
1616d 7a 65 20 26 26 20 70 42 74 2d 3e 75 73 61 62 6c ze && pBt->usabl
1616e 65 53 69 7a 65 2b 32 35 35 3e 3d 70 42 74 2d 3e eSize+255>=pBt->
1616f 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 pageSize);. dat
16170 61 5b 32 30 5d 20 3d 20 28 75 38 29 28 70 42 74 a[20] = (u8)(pBt
16171 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 ->pageSize - pBt
16172 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 ->usableSize);.
16173 20 64 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a data[21] = 64;.
16174 20 20 64 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b data[22] = 32;
16175 0a 20 20 64 61 74 61 5b 32 33 5d 20 3d 20 33 32 . data[23] = 32
16176 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 ;. memset(&data
16177 5b 32 34 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29 [24], 0, 100-24)
16178 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 50 31 ;. zeroPage(pP1
16179 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 , PTF_INTKEY|PTF
1617a 5f 4c 45 41 46 7c 50 54 46 5f 4c 45 41 46 44 41 _LEAF|PTF_LEAFDA
1617b 54 41 20 29 3b 0a 20 20 70 42 74 2d 3e 70 61 67 TA );. pBt->pag
1617c 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a eSizeFixed = 1;.
1617d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1617e 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
1617f 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 assert( pBt->au
16180 74 6f 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 toVacuum==1 || p
16181 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d Bt->autoVacuum==
16182 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
16183 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d Bt->incrVacuum==
16184 31 20 7c 7c 20 70 42 74 2d 3e 69 6e 63 72 56 61 1 || pBt->incrVa
16185 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 70 75 74 cuum==0 );. put
16186 34 62 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b 4byte(&data[36 +
16187 20 34 2a 34 5d 2c 20 70 42 74 2d 3e 61 75 74 6f 4*4], pBt->auto
16188 56 61 63 75 75 6d 29 3b 0a 20 20 70 75 74 34 62 Vacuum);. put4b
16189 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20 37 yte(&data[36 + 7
1618a 2a 34 5d 2c 20 70 42 74 2d 3e 69 6e 63 72 56 61 *4], pBt->incrVa
1618b 63 75 75 6d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 cuum);.#endif.
1618c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1618d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d ;.}../*.** Attem
1618e 70 74 20 74 6f 20 73 74 61 72 74 20 61 20 6e 65 pt to start a ne
1618f 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 w transaction. A
16190 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
16191 6f 6e 0a 2a 2a 20 69 73 20 73 74 61 72 74 65 64 on.** is started
16192 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 if the second a
16193 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 7a 65 rgument is nonze
16194 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 61 20 ro, otherwise a
16195 72 65 61 64 2d 0a 2a 2a 20 74 72 61 6e 73 61 63 read-.** transac
16196 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 73 65 tion. If the se
16197 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 cond argument is
16198 20 32 20 6f 72 20 6d 6f 72 65 20 61 6e 64 20 65 2 or more and e
16199 78 63 6c 75 73 69 76 65 0a 2a 2a 20 74 72 61 6e xclusive.** tran
1619a 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 saction is start
1619b 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 ed, meaning that
1619c 20 6e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 no other proces
1619d 73 20 69 73 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 s is allowed.**
1619e 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 64 61 to access the da
1619f 74 61 62 61 73 65 2e 20 20 41 20 70 72 65 65 78 tabase. A preex
161a0 69 73 74 69 6e 67 20 74 72 61 6e 73 61 63 74 69 isting transacti
161a1 6f 6e 20 6d 61 79 20 6e 6f 74 20 62 65 0a 2a 2a on may not be.**
161a2 20 75 70 67 72 61 64 65 64 20 74 6f 20 65 78 63 upgraded to exc
161a3 6c 75 73 69 76 65 20 62 79 20 63 61 6c 6c 69 6e lusive by callin
161a4 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 g this routine a
161a5 20 73 65 63 6f 6e 64 20 74 69 6d 65 20 2d 20 74 second time - t
161a6 68 65 0a 2a 2a 20 65 78 63 6c 75 73 69 76 69 74 he.** exclusivit
161a7 79 20 66 6c 61 67 20 6f 6e 6c 79 20 77 6f 72 6b y flag only work
161a8 73 20 66 6f 72 20 61 20 6e 65 77 20 74 72 61 6e s for a new tran
161a9 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 saction..**.** A
161aa 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
161ab 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 on must be start
161ac 65 64 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 ed before attemp
161ad 74 69 6e 67 20 61 6e 79 20 0a 2a 2a 20 63 68 61 ting any .** cha
161ae 6e 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 nges to the data
161af 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 base. None of t
161b0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 he following rou
161b1 74 69 6e 65 73 20 0a 2a 2a 20 77 69 6c 6c 20 77 tines .** will w
161b2 6f 72 6b 20 75 6e 6c 65 73 73 20 61 20 74 72 61 ork unless a tra
161b3 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 nsaction is star
161b4 74 65 64 20 66 69 72 73 74 3a 0a 2a 2a 0a 2a 2a ted first:.**.**
161b5 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
161b6 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 29 0a eeCreateTable().
161b7 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 ** sqlite3B
161b8 74 72 65 65 43 72 65 61 74 65 49 6e 64 65 78 28 treeCreateIndex(
161b9 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 ).** sqlite
161ba 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 3BtreeClearTable
161bb 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 ().** sqlit
161bc 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 e3BtreeDropTable
161bd 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 ().** sqlit
161be 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 29 0a e3BtreeInsert().
161bf 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 ** sqlite3B
161c0 74 72 65 65 44 65 6c 65 74 65 28 29 0a 2a 2a 20 treeDelete().**
161c1 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
161c2 65 55 70 64 61 74 65 4d 65 74 61 28 29 0a 2a 2a eUpdateMeta().**
161c3 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 69 74 69 61 .** If an initia
161c4 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63 71 l attempt to acq
161c5 75 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 66 61 uire the lock fa
161c6 69 6c 73 20 62 65 63 61 75 73 65 20 6f 66 20 6c ils because of l
161c7 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 0a 2a ock contention.*
161c8 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 * and the databa
161c9 73 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c se was previousl
161ca 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e y unlocked, then
161cb 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 invoke the busy
161cc 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 69 66 20 74 handler.** if t
161cd 68 65 72 65 20 69 73 20 6f 6e 65 2e 20 20 42 75 here is one. Bu
161ce 74 20 69 66 20 74 68 65 72 65 20 77 61 73 20 70 t if there was p
161cf 72 65 76 69 6f 75 73 6c 79 20 61 20 72 65 61 64 reviously a read
161d0 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f 74 0a 2a 2a -lock, do not.**
161d1 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 invoke the busy
161d2 20 68 61 6e 64 6c 65 72 20 2d 20 6a 75 73 74 20 handler - just
161d3 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 return SQLITE_BU
161d4 53 59 2e 20 20 53 51 4c 49 54 45 5f 42 55 53 59 SY. SQLITE_BUSY
161d5 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 is .** returned
161d6 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 when there is a
161d7 6c 72 65 61 64 79 20 61 20 72 65 61 64 2d 6c 6f lready a read-lo
161d8 63 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 ck in order to a
161d9 76 6f 69 64 20 61 20 64 65 61 64 6c 6f 63 6b 2e void a deadlock.
161da 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 .**.** Suppose t
161db 68 65 72 65 20 61 72 65 20 74 77 6f 20 70 72 6f here are two pro
161dc 63 65 73 73 65 73 20 41 20 61 6e 64 20 42 2e 20 cesses A and B.
161dd 20 41 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f A has a read lo
161de 63 6b 20 61 6e 64 20 42 20 68 61 73 0a 2a 2a 20 ck and B has.**
161df 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e a reserved lock.
161e0 20 20 42 20 74 72 69 65 73 20 74 6f 20 70 72 6f B tries to pro
161e1 6d 6f 74 65 20 74 6f 20 65 78 63 6c 75 73 69 76 mote to exclusiv
161e2 65 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 e but is blocked
161e3 20 62 65 63 61 75 73 65 0a 2a 2a 20 6f 66 20 41 because.** of A
161e4 27 73 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20 41 's read lock. A
161e5 20 74 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f 74 tries to promot
161e6 65 20 74 6f 20 72 65 73 65 72 76 65 64 20 62 75 e to reserved bu
161e7 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 t is blocked by
161e8 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 20 74 68 65 B..** One or the
161e9 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 74 77 other of the tw
161ea 6f 20 70 72 6f 63 65 73 73 65 73 20 6d 75 73 74 o processes must
161eb 20 67 69 76 65 20 77 61 79 20 6f 72 20 74 68 65 give way or the
161ec 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 6e 6f 20 re can be.** no
161ed 70 72 6f 67 72 65 73 73 2e 20 20 42 79 20 72 65 progress. By re
161ee 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 42 turning SQLITE_B
161ef 55 53 59 20 61 6e 64 20 6e 6f 74 20 69 6e 76 6f USY and not invo
161f0 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20 63 61 king the busy ca
161f1 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68 65 6e 20 41 llback.** when A
161f2 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 72 already has a r
161f3 65 61 64 20 6c 6f 63 6b 2c 20 77 65 20 65 6e 63 ead lock, we enc
161f4 6f 75 72 61 67 65 20 41 20 74 6f 20 67 69 76 65 ourage A to give
161f5 20 75 70 20 61 6e 64 20 6c 65 74 20 42 0a 2a 2a up and let B.**
161f6 20 70 72 6f 63 65 65 64 2e 0a 2a 2f 0a 53 51 4c proceed..*/.SQL
161f7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
161f8 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 sqlite3BtreeBegi
161f9 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 2c nTrans(Btree *p,
161fa 20 69 6e 74 20 77 72 66 6c 61 67 29 7b 0a 20 20 int wrflag){.
161fb 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 20 sqlite3 *pBlock
161fc 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64 20 = 0;. BtShared
161fd 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
161fe 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
161ff 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 _OK;.. sqlite3B
16200 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
16201 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 btreeIntegrity(p
16202 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
16203 62 74 72 65 65 20 69 73 20 61 6c 72 65 61 64 79 btree is already
16204 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e in a write-tran
16205 73 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a 20 saction, or it.
16206 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 69 ** is already i
16207 6e 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 n a read-transac
16208 74 69 6f 6e 20 61 6e 64 20 61 20 72 65 61 64 2d tion and a read-
16209 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a transaction. **
1620a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 74 is requested, t
1620b 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a his is a no-op..
1620c 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e */. if( p->in
1620d 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 Trans==TRANS_WRI
1620e 54 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61 6e TE || (p->inTran
1620f 73 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 26 26 s==TRANS_READ &&
16210 20 21 77 72 66 6c 61 67 29 20 29 7b 0a 20 20 20 !wrflag) ){.
16211 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 goto trans_begu
16212 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 n;. }.. /* Wri
16213 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 te transactions
16214 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 are not possible
16215 20 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 on a read-only
16216 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 66 database */. if
16217 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 ( pBt->readOnly
16218 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 && wrflag ){.
16219 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 rc = SQLITE_REA
1621a 44 4f 4e 4c 59 3b 0a 20 20 20 20 67 6f 74 6f 20 DONLY;. goto
1621b 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d trans_begun;. }
1621c 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1621d 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
1621e 48 45 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74 68 HE. /* If anoth
1621f 65 72 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 er database hand
16220 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 6f le has already o
16221 70 65 6e 65 64 20 61 20 77 72 69 74 65 20 74 72 pened a write tr
16222 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 ansaction . **
16223 6f 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d 62 on this shared-b
16224 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61 tree structure a
16225 6e 64 20 61 20 73 65 63 6f 6e 64 20 77 72 69 74 nd a second writ
16226 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 e transaction is
16227 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 2c . ** requested,
16228 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c return SQLITE_L
16229 4f 43 4b 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 OCKED.. */. if
1622a 28 20 28 77 72 66 6c 61 67 20 26 26 20 70 42 74 ( (wrflag && pBt
1622b 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
1622c 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 20 7c 7c =TRANS_WRITE) ||
1622d 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 pBt->isPending
1622e 29 7b 0a 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20 ){. pBlock =
1622f 70 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 pBt->pWriter->db
16230 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 77 72 ;. }else if( wr
16231 66 6c 61 67 3e 31 20 29 7b 0a 20 20 20 20 42 74 flag>1 ){. Bt
16232 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 20 20 20 Lock *pIter;.
16233 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e for(pIter=pBt->
16234 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 pLock; pIter; pI
16235 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 ter=pIter->pNext
16236 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 ){. if( pIt
16237 65 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 7b er->pBtree!=p ){
16238 0a 20 20 20 20 20 20 20 20 70 42 6c 6f 63 6b 20 . pBlock
16239 3d 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d = pIter->pBtree-
1623a 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 62 72 65 >db;. bre
1623b 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
1623c 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 42 6c 6f }. }. if( pBlo
1623d 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ck ){. sqlite
1623e 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 3ConnectionBlock
1623f 65 64 28 70 2d 3e 64 62 2c 20 70 42 6c 6f 63 6b ed(p->db, pBlock
16240 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 );. rc = SQLI
16241 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 TE_LOCKED_SHARED
16242 43 41 43 48 45 3b 0a 20 20 20 20 67 6f 74 6f 20 CACHE;. goto
16243 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d trans_begun;. }
16244 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6e .#endif.. /* An
16245 79 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72 y read-only or r
16246 65 61 64 2d 77 72 69 74 65 20 74 72 61 6e 73 61 ead-write transa
16247 63 74 69 6f 6e 20 69 6d 70 6c 69 65 73 20 61 20 ction implies a
16248 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 0a 20 20 read-lock on .
16249 2a 2a 20 70 61 67 65 20 31 2e 20 53 6f 20 69 66 ** page 1. So if
1624a 20 73 6f 6d 65 20 6f 74 68 65 72 20 73 68 61 72 some other shar
1624b 65 64 2d 63 61 63 68 65 20 63 6c 69 65 6e 74 20 ed-cache client
1624c 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 77 72 already has a wr
1624d 69 74 65 2d 6c 6f 63 6b 20 0a 20 20 2a 2a 20 6f ite-lock . ** o
1624e 6e 20 70 61 67 65 20 31 2c 20 74 68 65 20 74 72 n page 1, the tr
1624f 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 ansaction cannot
16250 20 62 65 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 be opened. */.
16251 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 65 rc = queryShare
16252 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 dCacheTableLock(
16253 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 p, MASTER_ROOT,
16254 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 READ_LOCK);. if
16255 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 ( SQLITE_OK!=rc
16256 29 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 ) goto trans_beg
16257 75 6e 3b 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 un;.. do {.
16258 2f 2a 20 43 61 6c 6c 20 6c 6f 63 6b 42 74 72 65 /* Call lockBtre
16259 65 28 29 20 75 6e 74 69 6c 20 65 69 74 68 65 72 e() until either
1625a 20 70 42 74 2d 3e 70 50 61 67 65 31 20 69 73 20 pBt->pPage1 is
1625b 70 6f 70 75 6c 61 74 65 64 20 6f 72 0a 20 20 20 populated or.
1625c 20 2a 2a 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 ** lockBtree()
1625d 72 65 74 75 72 6e 73 20 73 6f 6d 65 74 68 69 6e returns somethin
1625e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c g other than SQL
1625f 49 54 45 5f 4f 4b 2e 20 6c 6f 63 6b 42 74 72 65 ITE_OK. lockBtre
16260 65 28 29 0a 20 20 20 20 2a 2a 20 6d 61 79 20 72 e(). ** may r
16261 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
16262 62 75 74 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 but leave pBt->p
16263 50 61 67 65 31 20 73 65 74 20 74 6f 20 30 20 69 Page1 set to 0 i
16264 66 20 61 66 74 65 72 0a 20 20 20 20 2a 2a 20 72 f after. ** r
16265 65 61 64 69 6e 67 20 70 61 67 65 20 31 20 69 74 eading page 1 it
16266 20 64 69 73 63 6f 76 65 72 73 20 74 68 61 74 20 discovers that
16267 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 the page-size of
16268 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 the database .
16269 20 20 20 2a 2a 20 66 69 6c 65 20 69 73 20 6e 6f ** file is no
1626a 74 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2e t pBt->pageSize.
1626b 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 6c 6f In this case lo
1626c 63 6b 42 74 72 65 65 28 29 20 77 69 6c 6c 20 75 ckBtree() will u
1626d 70 64 61 74 65 0a 20 20 20 20 2a 2a 20 70 42 74 pdate. ** pBt
1626e 2d 3e 70 61 67 65 53 69 7a 65 20 74 6f 20 74 68 ->pageSize to th
1626f 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 74 e page-size of t
16270 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e he file on disk.
16271 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 69 6c . */. whil
16272 65 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d e( pBt->pPage1==
16273 30 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 0 && SQLITE_OK==
16274 28 72 63 20 3d 20 6c 6f 63 6b 42 74 72 65 65 28 (rc = lockBtree(
16275 70 42 74 29 29 20 29 3b 0a 0a 20 20 20 20 69 66 pBt)) );.. if
16276 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
16277 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 && wrflag ){.
16278 20 20 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64 if( pBt->read
16279 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 20 20 Only ){.
1627a 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 rc = SQLITE_READ
1627b 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 ONLY;. }els
1627c 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 e{. rc =
1627d 73 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 sqlite3PagerBegi
1627e 6e 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 77 72 n(pBt->pPager,wr
1627f 66 6c 61 67 3e 31 2c 73 71 6c 69 74 65 33 54 65 flag>1,sqlite3Te
16280 6d 70 49 6e 4d 65 6d 6f 72 79 28 70 2d 3e 64 62 mpInMemory(p->db
16281 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ));. if(
16282 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
16283 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
16284 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74 29 newDatabase(pBt)
16285 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
16286 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. }. .
16287 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
16288 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 6e 6c 6f OK ){. unlo
16289 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 ckBtreeIfUnused(
1628a 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 pBt);. }. }w
1628b 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 hile( rc==SQLITE
1628c 5f 42 55 53 59 20 26 26 20 70 42 74 2d 3e 69 6e _BUSY && pBt->in
1628d 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 Transaction==TRA
1628e 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20 20 20 20 20 NS_NONE &&.
1628f 20 20 20 20 20 62 74 72 65 65 49 6e 76 6f 6b 65 btreeInvoke
16290 42 75 73 79 48 61 6e 64 6c 65 72 28 70 42 74 29 BusyHandler(pBt)
16291 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 );.. if( rc==S
16292 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
16293 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d if( p->inTrans==
16294 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 TRANS_NONE ){.
16295 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 pBt->nTransa
16296 63 74 69 6f 6e 2b 2b 3b 0a 23 69 66 6e 64 65 66 ction++;.#ifndef
16297 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
16298 52 45 44 5f 43 41 43 48 45 0a 20 20 20 20 20 20 RED_CACHE.
16299 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 if( p->sharable
1629a 29 7b 0a 09 61 73 73 65 72 74 28 20 70 2d 3e 6c ){..assert( p->l
1629b 6f 63 6b 2e 70 42 74 72 65 65 3d 3d 70 20 26 26 ock.pBtree==p &&
1629c 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61 62 6c 65 3d p->lock.iTable=
1629d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d =1 );. p-
1629e 3e 6c 6f 63 6b 2e 65 4c 6f 63 6b 20 3d 20 52 45 >lock.eLock = RE
1629f 41 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 AD_LOCK;.
162a0 20 70 2d 3e 6c 6f 63 6b 2e 70 4e 65 78 74 20 3d p->lock.pNext =
162a1 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 pBt->pLock;.
162a2 20 20 20 20 20 70 42 74 2d 3e 70 4c 6f 63 6b 20 pBt->pLock
162a3 3d 20 26 70 2d 3e 6c 6f 63 6b 3b 0a 20 20 20 20 = &p->lock;.
162a4 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d }.#endif. }
162a5 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 . p->inTrans
162a6 3d 20 28 77 72 66 6c 61 67 3f 54 52 41 4e 53 5f = (wrflag?TRANS_
162a7 57 52 49 54 45 3a 54 52 41 4e 53 5f 52 45 41 44 WRITE:TRANS_READ
162a8 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e );. if( p->in
162a9 54 72 61 6e 73 3e 70 42 74 2d 3e 69 6e 54 72 61 Trans>pBt->inTra
162aa 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 nsaction ){.
162ab 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 pBt->inTransac
162ac 74 69 6f 6e 20 3d 20 70 2d 3e 69 6e 54 72 61 6e tion = p->inTran
162ad 73 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 s;. }.#ifndef
162ae 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
162af 52 45 44 5f 43 41 43 48 45 0a 20 20 20 20 69 66 RED_CACHE. if
162b0 28 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 ( wrflag ){.
162b1 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e assert( !pBt->
162b2 70 57 72 69 74 65 72 20 29 3b 0a 20 20 20 20 20 pWriter );.
162b3 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 pBt->pWriter =
162b4 70 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 73 p;. pBt->is
162b5 45 78 63 6c 75 73 69 76 65 20 3d 20 28 75 38 29 Exclusive = (u8)
162b6 28 77 72 66 6c 61 67 3e 31 29 3b 0a 20 20 20 20 (wrflag>1);.
162b7 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 0a 74 }.#endif. }...t
162b8 72 61 6e 73 5f 62 65 67 75 6e 3a 0a 20 20 69 66 rans_begun:. if
162b9 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
162ba 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 && wrflag ){.
162bb 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 61 /* This call ma
162bc 6b 65 73 20 73 75 72 65 20 74 68 61 74 20 74 68 kes sure that th
162bd 65 20 70 61 67 65 72 20 68 61 73 20 74 68 65 20 e pager has the
162be 63 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f correct number o
162bf 66 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 20 73 61 f. ** open sa
162c0 76 65 70 6f 69 6e 74 73 2e 20 49 66 20 74 68 65 vepoints. If the
162c1 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
162c2 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 r is greater tha
162c3 6e 20 30 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 n 0 and. ** t
162c4 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 he sub-journal i
162c5 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 s not already op
162c6 65 6e 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c en, then it will
162c7 20 62 65 20 6f 70 65 6e 65 64 20 68 65 72 65 2e be opened here.
162c8 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
162c9 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 sqlite3PagerOpe
162ca 6e 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e nSavepoint(pBt->
162cb 70 50 61 67 65 72 2c 20 70 2d 3e 64 62 2d 3e 6e pPager, p->db->n
162cc 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a Savepoint);. }.
162cd 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 . btreeIntegrit
162ce 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 y(p);. sqlite3B
162cf 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
162d0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 return rc;.}..#i
162d1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
162d2 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 0a 2f 2a T_AUTOVACUUM../*
162d3 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e .** Set the poin
162d4 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 ter-map entries
162d5 66 6f 72 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e for all children
162d6 20 6f 66 20 70 61 67 65 20 70 50 61 67 65 2e 20 of page pPage.
162d7 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20 70 50 61 67 Also, if.** pPag
162d8 65 20 63 6f 6e 74 61 69 6e 73 20 63 65 6c 6c 73 e contains cells
162d9 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f that point to o
162da 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 73 verflow pages, s
162db 65 74 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a et the pointer.*
162dc 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f * map entries fo
162dd 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 r the overflow p
162de 61 67 65 73 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f ages as well..*/
162df 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 43 .static int setC
162e0 68 69 6c 64 50 74 72 6d 61 70 73 28 4d 65 6d 50 hildPtrmaps(MemP
162e1 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 age *pPage){. i
162e2 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
162e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
162e4 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61 72 /* Counter var
162e5 69 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e iable */. int n
162e6 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 Cell;
162e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
162e8 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 Number of cells
162e9 20 69 6e 20 70 61 67 65 20 70 50 61 67 65 20 2a in page pPage *
162ea 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
162eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
162ec 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
162ed 20 63 6f 64 65 20 2a 2f 0a 20 20 42 74 53 68 61 code */. BtSha
162ee 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 red *pBt = pPage
162ef 2d 3e 70 42 74 3b 0a 20 20 75 38 20 69 73 49 6e ->pBt;. u8 isIn
162f0 69 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e itOrig = pPage->
162f1 69 73 49 6e 69 74 3b 0a 20 20 50 67 6e 6f 20 70 isInit;. Pgno p
162f2 67 6e 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e gno = pPage->pgn
162f3 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 o;.. assert( sq
162f4 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
162f5 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
162f6 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 62 74 ex) );. rc = bt
162f7 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 reeInitPage(pPag
162f8 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 e);. if( rc!=SQ
162f9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 LITE_OK ){. g
162fa 6f 74 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 oto set_child_pt
162fb 72 6d 61 70 73 5f 6f 75 74 3b 0a 20 20 7d 0a 20 rmaps_out;. }.
162fc 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e nCell = pPage->
162fd 6e 43 65 6c 6c 3b 0a 0a 20 20 66 6f 72 28 69 3d nCell;.. for(i=
162fe 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 0; i<nCell; i++)
162ff 7b 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 {. u8 *pCell
16300 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 = findCell(pPage
16301 2c 20 69 29 3b 0a 0a 20 20 20 20 70 74 72 6d 61 , i);.. ptrma
16302 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 pPutOvflPtr(pPag
16303 65 2c 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a e, pCell, &rc);.
16304 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d . if( !pPage-
16305 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 50 >leaf ){. P
16306 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 gno childPgno =
16307 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b get4byte(pCell);
16308 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 . ptrmapPut
16309 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c (pBt, childPgno,
1630a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 PTRMAP_BTREE, p
1630b 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d gno, &rc);. }
1630c 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 . }.. if( !pPa
1630d 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 ge->leaf ){.
1630e 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d Pgno childPgno =
1630f 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
16310 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 ->aData[pPage->h
16311 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 drOffset+8]);.
16312 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c ptrmapPut(pBt,
16313 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52 4d childPgno, PTRM
16314 41 50 5f 42 54 52 45 45 2c 20 70 67 6e 6f 2c 20 AP_BTREE, pgno,
16315 26 72 63 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f 63 &rc);. }..set_c
16316 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 hild_ptrmaps_out
16317 3a 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 :. pPage->isIni
16318 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a t = isInitOrig;.
16319 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1631a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 65 20 /*.** Somewhere
1631b 6f 6e 20 70 50 61 67 65 20 69 73 20 61 20 70 6f on pPage is a po
1631c 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 69 46 inter to page iF
1631d 72 6f 6d 2e 20 20 4d 6f 64 69 66 79 20 74 68 69 rom. Modify thi
1631e 73 20 70 6f 69 6e 74 65 72 20 73 6f 0a 2a 2a 20 s pointer so.**
1631f 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 that it points t
16320 6f 20 69 54 6f 2e 20 50 61 72 61 6d 65 74 65 72 o iTo. Parameter
16321 20 65 54 79 70 65 20 64 65 73 63 72 69 62 65 73 eType describes
16322 20 74 68 65 20 74 79 70 65 20 6f 66 20 70 6f 69 the type of poi
16323 6e 74 65 72 20 74 6f 0a 2a 2a 20 62 65 20 6d 6f nter to.** be mo
16324 64 69 66 69 65 64 2c 20 61 73 20 20 66 6f 6c 6c dified, as foll
16325 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 ows:.**.** PTRMA
16326 50 5f 42 54 52 45 45 3a 20 20 20 20 20 70 50 61 P_BTREE: pPa
16327 67 65 20 69 73 20 61 20 62 74 72 65 65 2d 70 61 ge is a btree-pa
16328 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 ge. The pointer
16329 70 6f 69 6e 74 73 20 61 74 20 61 20 63 68 69 6c points at a chil
1632a 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 d .**
1632b 20 20 20 20 20 20 20 20 70 61 67 65 20 6f 66 20 page of
1632c 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 pPage..**.** PTR
1632d 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 70 MAP_OVERFLOW1: p
1632e 50 61 67 65 20 69 73 20 61 20 62 74 72 65 65 2d Page is a btree-
1632f 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 page. The pointe
16330 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 6f r points at an o
16331 76 65 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 verflow.**
16332 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 pag
16333 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 e pointed to by
16334 6f 6e 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 one of the cells
16335 20 6f 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a on pPage..**.**
16336 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
16337 32 3a 20 70 50 61 67 65 20 69 73 20 61 6e 20 6f 2: pPage is an o
16338 76 65 72 66 6c 6f 77 2d 70 61 67 65 2e 20 54 68 verflow-page. Th
16339 65 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 e pointer points
1633a 20 61 74 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 at the next.**
1633b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1633c 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 overflow page
1633d 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a in the list..*/.
1633e 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 64 69 66 static int modif
1633f 79 50 61 67 65 50 6f 69 6e 74 65 72 28 4d 65 6d yPagePointer(Mem
16340 50 61 67 65 20 2a 70 50 61 67 65 2c 20 50 67 6e Page *pPage, Pgn
16341 6f 20 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 69 54 o iFrom, Pgno iT
16342 6f 2c 20 75 38 20 65 54 79 70 65 29 7b 0a 20 20 o, u8 eType){.
16343 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
16344 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
16345 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
16346 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
16347 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
16348 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
16349 65 29 20 29 3b 0a 20 20 69 66 28 20 65 54 79 70 e) );. if( eTyp
1634a 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c e==PTRMAP_OVERFL
1634b 4f 57 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 OW2 ){. /* Th
1634c 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 77 e pointer is alw
1634d 61 79 73 20 74 68 65 20 66 69 72 73 74 20 34 20 ays the first 4
1634e 62 79 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 bytes of the pag
1634f 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 e in this case.
16350 20 2a 2f 0a 20 20 20 20 69 66 28 20 67 65 74 34 */. if( get4
16351 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 byte(pPage->aDat
16352 61 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 a)!=iFrom ){.
16353 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
16354 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
16355 20 20 20 7d 0a 20 20 20 20 70 75 74 34 62 79 74 }. put4byt
16356 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 e(pPage->aData,
16357 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 iTo);. }else{.
16358 20 20 20 75 38 20 69 73 49 6e 69 74 4f 72 69 67 u8 isInitOrig
16359 20 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 = pPage->isInit
1635a 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 ;. int i;.
1635b 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 20 int nCell;..
1635c 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 btreeInitPage(p
1635d 50 61 67 65 29 3b 0a 20 20 20 20 6e 43 65 6c 6c Page);. nCell
1635e 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b = pPage->nCell;
1635f 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 .. for(i=0; i
16360 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 <nCell; i++){.
16361 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 u8 *pCell =
16362 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
16363 69 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54 i);. if( eT
16364 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 ype==PTRMAP_OVER
16365 46 4c 4f 57 31 20 29 7b 0a 20 20 20 20 20 20 20 FLOW1 ){.
16366 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a CellInfo info;.
16367 20 20 20 20 20 20 20 20 62 74 72 65 65 50 61 72 btreePar
16368 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c seCellPtr(pPage,
16369 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a pCell, &info);.
1636a 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f if( info
1636b 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 .iOverflow ){.
1636c 20 20 20 20 20 20 20 20 69 66 28 20 69 46 72 6f if( iFro
1636d 6d 3d 3d 67 65 74 34 62 79 74 65 28 26 70 43 65 m==get4byte(&pCe
1636e 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
1636f 77 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 w]) ){.
16370 20 20 20 70 75 74 34 62 79 74 65 28 26 70 43 65 put4byte(&pCe
16371 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
16372 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 w], iTo);.
16373 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
16374 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
16375 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
16376 20 20 20 20 20 20 20 20 69 66 28 20 67 65 74 34 if( get4
16377 62 79 74 65 28 70 43 65 6c 6c 29 3d 3d 69 46 72 byte(pCell)==iFr
16378 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 om ){.
16379 70 75 74 34 62 79 74 65 28 70 43 65 6c 6c 2c 20 put4byte(pCell,
1637a 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 iTo);.
1637b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d break;. }
1637c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1637d 20 0a 20 20 20 20 69 66 28 20 69 3d 3d 6e 43 65 . if( i==nCe
1637e 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ll ){. if(
1637f 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 42 54 eType!=PTRMAP_BT
16380 52 45 45 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 REE || .
16381 20 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 get4byte(&pPag
16382 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e e->aData[pPage->
16383 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 21 3d 69 hdrOffset+8])!=i
16384 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 From ){.
16385 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
16386 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
16387 20 20 7d 0a 20 20 20 20 20 20 70 75 74 34 62 79 }. put4by
16388 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 te(&pPage->aData
16389 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
1638a 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 t+8], iTo);.
1638b 7d 0a 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 }.. pPage->is
1638c 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 Init = isInitOri
1638d 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 g;. }. return
1638e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f SQLITE_OK;.}.../
1638f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70 *.** Move the op
16390 65 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 65 en database page
16391 20 70 44 62 50 61 67 65 20 74 6f 20 6c 6f 63 61 pDbPage to loca
16392 74 69 6f 6e 20 69 46 72 65 65 50 61 67 65 20 69 tion iFreePage i
16393 6e 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 n the .** databa
16394 73 65 2e 20 54 68 65 20 70 44 62 50 61 67 65 20 se. The pDbPage
16395 72 65 66 65 72 65 6e 63 65 20 72 65 6d 61 69 6e reference remain
16396 73 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 s valid..**.** T
16397 68 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 he isCommit flag
16398 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 indicates that
16399 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 there is no need
1639a 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61 to remember tha
1639b 74 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c t.** the journal
1639c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e needs to be syn
1639d 63 28 29 65 64 20 62 65 66 6f 72 65 20 64 61 74 c()ed before dat
1639e 61 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61 abase page pDbPa
1639f 67 65 2d 3e 70 67 6e 6f 20 0a 2a 2a 20 63 61 6e ge->pgno .** can
163a0 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 be written to.
163a1 54 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 61 The caller has a
163a2 6c 72 65 61 64 79 20 70 72 6f 6d 69 73 65 64 20 lready promised
163a3 6e 6f 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 not to write to
163a4 74 68 61 74 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2f that.** page..*/
163a5 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6c 6f .static int relo
163a6 63 61 74 65 50 61 67 65 28 0a 20 20 42 74 53 68 catePage(. BtSh
163a7 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 ared *pBt,
163a8 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 2a 2f /* Btree */
163a9 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 44 62 50 . MemPage *pDbP
163aa 61 67 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f age, /* O
163ab 70 65 6e 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 pen page to move
163ac 20 2a 2f 0a 20 20 75 38 20 65 54 79 70 65 2c 20 */. u8 eType,
163ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
163ae 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 74 * Pointer map 't
163af 79 70 65 27 20 65 6e 74 72 79 20 66 6f 72 20 70 ype' entry for p
163b0 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f DbPage */. Pgno
163b1 20 69 50 74 72 50 61 67 65 2c 20 20 20 20 20 20 iPtrPage,
163b2 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
163b3 6d 61 70 20 27 70 61 67 65 2d 6e 6f 27 20 65 6e map 'page-no' en
163b4 74 72 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 try for pDbPage
163b5 2a 2f 0a 20 20 50 67 6e 6f 20 69 46 72 65 65 50 */. Pgno iFreeP
163b6 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a age, /*
163b7 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20 74 6f The location to
163b8 20 6d 6f 76 65 20 70 44 62 50 61 67 65 20 74 6f move pDbPage to
163b9 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f 6d 6d */. int isComm
163ba 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20 2f it /
163bb 2a 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 * isCommit flag
163bc 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 passed to sqlite
163bd 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 20 2a 3PagerMovepage *
163be 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a /.){. MemPage *
163bf 70 50 74 72 50 61 67 65 3b 20 20 20 2f 2a 20 54 pPtrPage; /* T
163c0 68 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e he page that con
163c1 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 tains a pointer
163c2 74 6f 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 to pDbPage */.
163c3 50 67 6e 6f 20 69 44 62 50 61 67 65 20 3d 20 70 Pgno iDbPage = p
163c4 44 62 50 61 67 65 2d 3e 70 67 6e 6f 3b 0a 20 20 DbPage->pgno;.
163c5 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
163c6 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 pBt->pPager;. i
163c7 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
163c8 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f ( eType==PTRMAP_
163c9 4f 56 45 52 46 4c 4f 57 32 20 7c 7c 20 65 54 79 OVERFLOW2 || eTy
163ca 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 pe==PTRMAP_OVERF
163cb 4c 4f 57 31 20 7c 7c 20 0a 20 20 20 20 20 20 65 LOW1 || . e
163cc 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 Type==PTRMAP_BTR
163cd 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 EE || eType==PTR
163ce 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0a MAP_ROOTPAGE );.
163cf 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
163d0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
163d1 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
163d2 73 65 72 74 28 20 70 44 62 50 61 67 65 2d 3e 70 sert( pDbPage->p
163d3 42 74 3d 3d 70 42 74 20 29 3b 0a 0a 20 20 2f 2a Bt==pBt );.. /*
163d4 20 4d 6f 76 65 20 70 61 67 65 20 69 44 62 50 61 Move page iDbPa
163d5 67 65 20 66 72 6f 6d 20 69 74 73 20 63 75 72 72 ge from its curr
163d6 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 ent location to
163d7 70 61 67 65 20 6e 75 6d 62 65 72 20 69 46 72 65 page number iFre
163d8 65 50 61 67 65 20 2a 2f 0a 20 20 54 52 41 43 45 ePage */. TRACE
163d9 28 28 22 41 55 54 4f 56 41 43 55 55 4d 3a 20 4d (("AUTOVACUUM: M
163da 6f 76 69 6e 67 20 25 64 20 74 6f 20 66 72 65 65 oving %d to free
163db 20 70 61 67 65 20 25 64 20 28 70 74 72 20 70 61 page %d (ptr pa
163dc 67 65 20 25 64 20 74 79 70 65 20 25 64 29 5c 6e ge %d type %d)\n
163dd 22 2c 20 0a 20 20 20 20 20 20 69 44 62 50 61 67 ", . iDbPag
163de 65 2c 20 69 46 72 65 65 50 61 67 65 2c 20 69 50 e, iFreePage, iP
163df 74 72 50 61 67 65 2c 20 65 54 79 70 65 29 29 3b trPage, eType));
163e0 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
163e1 61 67 65 72 4d 6f 76 65 70 61 67 65 28 70 50 61 agerMovepage(pPa
163e2 67 65 72 2c 20 70 44 62 50 61 67 65 2d 3e 70 44 ger, pDbPage->pD
163e3 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 bPage, iFreePage
163e4 2c 20 69 73 43 6f 6d 6d 69 74 29 3b 0a 20 20 69 , isCommit);. i
163e5 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
163e6 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
163e7 63 3b 0a 20 20 7d 0a 20 20 70 44 62 50 61 67 65 c;. }. pDbPage
163e8 2d 3e 70 67 6e 6f 20 3d 20 69 46 72 65 65 50 61 ->pgno = iFreePa
163e9 67 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 44 62 ge;.. /* If pDb
163ea 50 61 67 65 20 77 61 73 20 61 20 62 74 72 65 65 Page was a btree
163eb 2d 70 61 67 65 2c 20 74 68 65 6e 20 69 74 20 6d -page, then it m
163ec 61 79 20 68 61 76 65 20 63 68 69 6c 64 20 70 61 ay have child pa
163ed 67 65 73 20 61 6e 64 2f 6f 72 20 63 65 6c 6c 73 ges and/or cells
163ee 0a 20 20 2a 2a 20 74 68 61 74 20 70 6f 69 6e 74 . ** that point
163ef 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 to overflow pag
163f0 65 73 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 es. The pointer
163f1 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 map entries for
163f2 61 6c 6c 20 74 68 65 73 65 0a 20 20 2a 2a 20 70 all these. ** p
163f3 61 67 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 ages need to be
163f4 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2a 0a 20 20 changed.. **.
163f5 2a 2a 20 49 66 20 70 44 62 50 61 67 65 20 69 73 ** If pDbPage is
163f6 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
163f7 65 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 e, then the firs
163f8 74 20 34 20 62 79 74 65 73 20 6d 61 79 20 73 74 t 4 bytes may st
163f9 6f 72 65 20 61 0a 20 20 2a 2a 20 70 6f 69 6e 74 ore a. ** point
163fa 65 72 20 74 6f 20 61 20 73 75 62 73 65 71 75 65 er to a subseque
163fb 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 nt overflow page
163fc 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 . If this is the
163fd 20 63 61 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a case, then. **
163fe 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
163ff 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 needs to be upd
16400 61 74 65 64 20 66 6f 72 20 74 68 65 20 73 75 62 ated for the sub
16401 73 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 sequent overflow
16402 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 page.. */. if
16403 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f ( eType==PTRMAP_
16404 42 54 52 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d BTREE || eType==
16405 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 PTRMAP_ROOTPAGE
16406 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 65 74 43 ){. rc = setC
16407 68 69 6c 64 50 74 72 6d 61 70 73 28 70 44 62 50 hildPtrmaps(pDbP
16408 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 age);. if( rc
16409 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1640a 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1640b 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1640c 20 20 20 50 67 6e 6f 20 6e 65 78 74 4f 76 66 6c Pgno nextOvfl
1640d 20 3d 20 67 65 74 34 62 79 74 65 28 70 44 62 50 = get4byte(pDbP
1640e 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 age->aData);.
1640f 20 69 66 28 20 6e 65 78 74 4f 76 66 6c 21 3d 30 if( nextOvfl!=0
16410 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 ){. ptrmap
16411 50 75 74 28 70 42 74 2c 20 6e 65 78 74 4f 76 66 Put(pBt, nextOvf
16412 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c l, PTRMAP_OVERFL
16413 4f 57 32 2c 20 69 46 72 65 65 50 61 67 65 2c 20 OW2, iFreePage,
16414 26 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 &rc);. if(
16415 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
16416 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
16417 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
16418 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 78 20 }. }.. /* Fix
16419 74 68 65 20 64 61 74 61 62 61 73 65 20 70 6f 69 the database poi
1641a 6e 74 65 72 20 6f 6e 20 70 61 67 65 20 69 50 74 nter on page iPt
1641b 72 50 61 67 65 20 74 68 61 74 20 70 6f 69 6e 74 rPage that point
1641c 65 64 20 61 74 20 69 44 62 50 61 67 65 20 73 6f ed at iDbPage so
1641d 0a 20 20 2a 2a 20 74 68 61 74 20 69 74 20 70 6f . ** that it po
1641e 69 6e 74 73 20 61 74 20 69 46 72 65 65 50 61 67 ints at iFreePag
1641f 65 2e 20 41 6c 73 6f 20 66 69 78 20 74 68 65 20 e. Also fix the
16420 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 pointer map entr
16421 79 20 66 6f 72 0a 20 20 2a 2a 20 69 50 74 72 50 y for. ** iPtrP
16422 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 age.. */. if(
16423 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f eType!=PTRMAP_RO
16424 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63 OTPAGE ){. rc
16425 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
16426 70 42 74 2c 20 69 50 74 72 50 61 67 65 2c 20 26 pBt, iPtrPage, &
16427 70 50 74 72 50 61 67 65 2c 20 30 29 3b 0a 20 20 pPtrPage, 0);.
16428 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
16429 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 _OK ){. ret
1642a 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
1642b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1642c 67 65 72 57 72 69 74 65 28 70 50 74 72 50 61 67 gerWrite(pPtrPag
1642d 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 e->pDbPage);.
1642e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1642f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 OK ){. rele
16430 61 73 65 50 61 67 65 28 70 50 74 72 50 61 67 65 asePage(pPtrPage
16431 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
16432 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 rc;. }. rc
16433 20 3d 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 = modifyPagePoi
16434 6e 74 65 72 28 70 50 74 72 50 61 67 65 2c 20 69 nter(pPtrPage, i
16435 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 DbPage, iFreePag
16436 65 2c 20 65 54 79 70 65 29 3b 0a 20 20 20 20 72 e, eType);. r
16437 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50 eleasePage(pPtrP
16438 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 age);. if( rc
16439 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1643a 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 ptrmapPut(p
1643b 42 74 2c 20 69 46 72 65 65 50 61 67 65 2c 20 65 Bt, iFreePage, e
1643c 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 Type, iPtrPage,
1643d 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a &rc);. }. }.
1643e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1643f 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 /* Forward decla
16440 72 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64 20 ration required
16441 62 79 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 by incrVacuumSte
16442 70 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 p(). */.static i
16443 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 nt allocateBtree
16444 50 61 67 65 28 42 74 53 68 61 72 65 64 20 2a 2c Page(BtShared *,
16445 20 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 50 67 6e MemPage **, Pgn
16446 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 3b 0a o *, Pgno, u8);.
16447 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 ./*.** Perform a
16448 20 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20 single step of
16449 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 an incremental-v
1644a 61 63 75 75 6d 2e 20 49 66 20 73 75 63 63 65 73 acuum. If succes
1644b 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 sful,.** return
1644c 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 SQLITE_OK. If th
1644d 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 20 74 ere is no work t
1644e 6f 20 64 6f 20 28 61 6e 64 20 74 68 65 72 65 66 o do (and theref
1644f 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69 6e 74 20 ore no.** point
16450 69 6e 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 in calling this
16451 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 29 2c function again),
16452 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 return SQLITE_D
16453 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 65 20 ONE..**.** More
16454 73 70 65 63 69 66 69 63 6c 79 2c 20 74 68 69 73 specificly, this
16455 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 function attemp
16456 74 73 20 74 6f 20 72 65 2d 6f 72 67 61 6e 69 7a ts to re-organiz
16457 65 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 e the .** databa
16458 73 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 6c se so that the l
16459 61 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 ast page of the
1645a 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 69 file currently i
1645b 6e 20 75 73 65 0a 2a 2a 20 69 73 20 6e 6f 20 6c n use.** is no l
1645c 6f 6e 67 65 72 20 69 6e 20 75 73 65 2e 0a 2a 2a onger in use..**
1645d 0a 2a 2a 20 49 66 20 74 68 65 20 6e 46 69 6e 20 .** If the nFin
1645e 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e parameter is non
1645f 2d 7a 65 72 6f 2c 20 74 68 69 73 20 66 75 6e 63 -zero, this func
16460 74 69 6f 6e 20 61 73 73 75 6d 65 73 0a 2a 2a 20 tion assumes.**
16461 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 that the caller
16462 77 69 6c 6c 20 6b 65 65 70 20 63 61 6c 6c 69 6e will keep callin
16463 67 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 g incrVacuumStep
16464 28 29 20 75 6e 74 69 6c 0a 2a 2a 20 69 74 20 72 () until.** it r
16465 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f eturns SQLITE_DO
16466 4e 45 20 6f 72 20 61 6e 20 65 72 72 6f 72 2c 20 NE or an error,
16467 61 6e 64 20 74 68 61 74 20 6e 46 69 6e 20 69 73 and that nFin is
16468 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f the.** number o
16469 66 20 70 61 67 65 73 20 74 68 65 20 64 61 74 61 f pages the data
1646a 62 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 63 base file will c
1646b 6f 6e 74 61 69 6e 20 61 66 74 65 72 20 74 68 69 ontain after thi
1646c 73 20 0a 2a 2a 20 70 72 6f 63 65 73 73 20 69 73 s .** process is
1646d 20 63 6f 6d 70 6c 65 74 65 2e 20 20 49 66 20 6e complete. If n
1646e 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20 69 74 20 Fin is zero, it
1646f 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a is assumed that.
16470 2a 2a 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 ** incrVacuumSte
16471 70 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c p() will be call
16472 65 64 20 61 20 66 69 6e 69 74 65 20 61 6d 6f 75 ed a finite amou
16473 6e 74 20 6f 66 20 74 69 6d 65 73 0a 2a 2a 20 77 nt of times.** w
16474 68 69 63 68 20 6d 61 79 20 6f 72 20 6d 61 79 20 hich may or may
16475 6e 6f 74 20 65 6d 70 74 79 20 74 68 65 20 66 72 not empty the fr
16476 65 65 6c 69 73 74 2e 20 20 41 20 66 75 6c 6c 20 eelist. A full
16477 61 75 74 6f 76 61 63 75 75 6d 0a 2a 2a 20 68 61 autovacuum.** ha
16478 73 20 6e 46 69 6e 3e 30 2e 20 20 41 20 22 50 52 s nFin>0. A "PR
16479 41 47 4d 41 20 69 6e 63 72 65 6d 65 6e 74 61 6c AGMA incremental
1647a 5f 76 61 63 75 75 6d 22 20 68 61 73 20 6e 46 69 _vacuum" has nFi
1647b 6e 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 n==0..*/.static
1647c 69 6e 74 20 69 6e 63 72 56 61 63 75 75 6d 53 74 int incrVacuumSt
1647d 65 70 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ep(BtShared *pBt
1647e 2c 20 50 67 6e 6f 20 6e 46 69 6e 2c 20 50 67 6e , Pgno nFin, Pgn
1647f 6f 20 69 4c 61 73 74 50 67 29 7b 0a 20 20 50 67 o iLastPg){. Pg
16480 6e 6f 20 6e 46 72 65 65 4c 69 73 74 3b 20 20 20 no nFreeList;
16481 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
16482 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c 6c r of pages still
16483 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 on the free-lis
16484 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 t */.. assert(
16485 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
16486 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
16487 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c 61 73 ;. assert( iLas
16488 74 50 67 3e 6e 46 69 6e 20 29 3b 0a 0a 20 20 69 tPg>nFin );.. i
16489 66 28 20 21 50 54 52 4d 41 50 5f 49 53 50 41 47 f( !PTRMAP_ISPAG
1648a 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 E(pBt, iLastPg)
1648b 26 26 20 69 4c 61 73 74 50 67 21 3d 50 45 4e 44 && iLastPg!=PEND
1648c 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
1648d 74 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 t) ){. int rc
1648e 3b 0a 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a ;. u8 eType;.
1648f 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 Pgno iPtrPag
16490 65 3b 0a 0a 20 20 20 20 6e 46 72 65 65 4c 69 73 e;.. nFreeLis
16491 74 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 t = get4byte(&pB
16492 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
16493 5b 33 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e [36]);. if( n
16494 46 72 65 65 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 FreeList==0 ){.
16495 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
16496 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a TE_DONE;. }..
16497 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 rc = ptrmapG
16498 65 74 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c et(pBt, iLastPg,
16499 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 61 &eType, &iPtrPa
1649a 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 ge);. if( rc!
1649b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1649c 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1649d 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 54 79 }. if( eTy
1649e 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 pe==PTRMAP_ROOTP
1649f 41 47 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74 AGE ){. ret
164a0 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
164a1 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a PT_BKPT;. }..
164a2 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 if( eType==P
164a3 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 TRMAP_FREEPAGE )
164a4 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 46 69 6e {. if( nFin
164a5 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ==0 ){. /
164a6 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67 * Remove the pag
164a7 65 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 73 e from the files
164a8 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73 free-list. This
164a9 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 is not required
164aa 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 20 6e . ** if n
164ab 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e Fin is non-zero.
164ac 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74 In that case, t
164ad 68 65 20 66 72 65 65 2d 6c 69 73 74 20 77 69 6c he free-list wil
164ae 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 l be. **
164af 74 72 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 truncated to zer
164b0 6f 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e o after this fun
164b1 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73 ction returns, s
164b2 6f 20 69 74 20 64 6f 65 73 6e 27 74 20 0a 20 20 o it doesn't .
164b3 20 20 20 20 20 20 2a 2a 20 6d 61 74 74 65 72 20 ** matter
164b4 69 66 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74 if it still cont
164b5 61 69 6e 73 20 73 6f 6d 65 20 67 61 72 62 61 67 ains some garbag
164b6 65 20 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20 e entries..
164b7 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 50 67 */. Pg
164b8 6e 6f 20 69 46 72 65 65 50 67 3b 0a 20 20 20 20 no iFreePg;.
164b9 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 MemPage *pFr
164ba 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 eePg;. rc
164bb 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 = allocateBtree
164bc 50 61 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 Page(pBt, &pFree
164bd 50 67 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4c Pg, &iFreePg, iL
164be 61 73 74 50 67 2c 20 31 29 3b 0a 20 20 20 20 20 astPg, 1);.
164bf 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
164c0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
164c1 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
164c2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 }. a
164c3 73 73 65 72 74 28 20 69 46 72 65 65 50 67 3d 3d ssert( iFreePg==
164c4 69 4c 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20 iLastPg );.
164c5 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
164c6 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20 7d FreePg);. }
164c7 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 . } else {.
164c8 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 Pgno iFreePg
164c9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
164ca 20 49 6e 64 65 78 20 6f 66 20 66 72 65 65 20 70 Index of free p
164cb 61 67 65 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73 age to move pLas
164cc 74 50 67 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20 tPg to */.
164cd 4d 65 6d 50 61 67 65 20 2a 70 4c 61 73 74 50 67 MemPage *pLastPg
164ce 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 ;.. rc = bt
164cf 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
164d0 69 4c 61 73 74 50 67 2c 20 26 70 4c 61 73 74 50 iLastPg, &pLastP
164d1 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 g, 0);. if(
164d2 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
164d3 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
164d4 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 rc;. }..
164d5 20 20 20 20 2f 2a 20 49 66 20 6e 46 69 6e 20 69 /* If nFin i
164d6 73 20 7a 65 72 6f 2c 20 74 68 69 73 20 6c 6f 6f s zero, this loo
164d7 70 20 72 75 6e 73 20 65 78 61 63 74 6c 79 20 6f p runs exactly o
164d8 6e 63 65 20 61 6e 64 20 70 61 67 65 20 70 4c 61 nce and page pLa
164d9 73 74 50 67 0a 20 20 20 20 20 20 2a 2a 20 69 73 stPg. ** is
164da 20 73 77 61 70 70 65 64 20 77 69 74 68 20 74 68 swapped with th
164db 65 20 66 69 72 73 74 20 66 72 65 65 20 70 61 67 e first free pag
164dc 65 20 70 75 6c 6c 65 64 20 6f 66 66 20 74 68 65 e pulled off the
164dd 20 66 72 65 65 20 6c 69 73 74 2e 0a 20 20 20 20 free list..
164de 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4f 6e **. ** On
164df 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c the other hand,
164e0 20 69 66 20 6e 46 69 6e 20 69 73 20 67 72 65 61 if nFin is grea
164e1 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 ter than zero, t
164e2 68 65 6e 20 6b 65 65 70 0a 20 20 20 20 20 20 2a hen keep. *
164e3 2a 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20 * looping until
164e4 61 20 66 72 65 65 2d 70 61 67 65 20 6c 6f 63 61 a free-page loca
164e5 74 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 66 ted within the f
164e6 69 72 73 74 20 6e 46 69 6e 20 70 61 67 65 73 0a irst nFin pages.
164e7 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 ** of the
164e8 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 2e 0a 20 file is found..
164e9 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f */. do
164ea 20 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 {. MemPa
164eb 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 ge *pFreePg;.
164ec 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 rc = alloca
164ed 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c teBtreePage(pBt,
164ee 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 65 &pFreePg, &iFre
164ef 65 50 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 ePg, 0, 0);.
164f0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
164f1 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
164f2 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
164f3 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20 20 20 LastPg);.
164f4 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
164f5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
164f6 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 72 65 releasePage(pFre
164f7 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 ePg);. }whi
164f8 6c 65 28 20 6e 46 69 6e 21 3d 30 20 26 26 20 69 le( nFin!=0 && i
164f9 46 72 65 65 50 67 3e 6e 46 69 6e 20 29 3b 0a 20 FreePg>nFin );.
164fa 20 20 20 20 20 61 73 73 65 72 74 28 20 69 46 72 assert( iFr
164fb 65 65 50 67 3c 69 4c 61 73 74 50 67 20 29 3b 0a eePg<iLastPg );.
164fc 20 20 20 20 20 20 0a 20 20 20 20 20 20 72 63 20 . rc
164fd 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
164fe 69 74 65 28 70 4c 61 73 74 50 67 2d 3e 70 44 62 ite(pLastPg->pDb
164ff 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 Page);. if(
16500 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
16501 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 {. rc = r
16502 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c elocatePage(pBt,
16503 20 70 4c 61 73 74 50 67 2c 20 65 54 79 70 65 2c pLastPg, eType,
16504 20 69 50 74 72 50 61 67 65 2c 20 69 46 72 65 65 iPtrPage, iFree
16505 50 67 2c 20 6e 46 69 6e 21 3d 30 29 3b 0a 20 20 Pg, nFin!=0);.
16506 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 }. rele
16507 61 73 65 50 61 67 65 28 70 4c 61 73 74 50 67 29 asePage(pLastPg)
16508 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
16509 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1650a 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1650b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1650c 7d 0a 0a 20 20 69 66 28 20 6e 46 69 6e 3d 3d 30 }.. if( nFin==0
1650d 20 29 7b 0a 20 20 20 20 69 4c 61 73 74 50 67 2d ){. iLastPg-
1650e 2d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 4c -;. while( iL
1650f 61 73 74 50 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 astPg==PENDING_B
16510 59 54 45 5f 50 41 47 45 28 70 42 74 29 7c 7c 50 YTE_PAGE(pBt)||P
16511 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 TRMAP_ISPAGE(pBt
16512 2c 20 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 , iLastPg) ){.
16513 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49 if( PTRMAP_I
16514 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 SPAGE(pBt, iLast
16515 50 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 4d Pg) ){. M
16516 65 6d 50 61 67 65 20 2a 70 50 67 3b 0a 20 20 20 emPage *pPg;.
16517 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62 74 int rc = bt
16518 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
16519 69 4c 61 73 74 50 67 2c 20 26 70 50 67 2c 20 30 iLastPg, &pPg, 0
1651a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
1651b 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1651c 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
1651d 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rc;. }.
1651e 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1651f 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
16520 67 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 g->pDbPage);.
16521 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
16522 28 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20 69 (pPg);. i
16523 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
16524 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
16525 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
16526 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
16527 20 69 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 20 iLastPg--;.
16528 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 }. sqlite3Pag
16529 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 erTruncateImage(
1652a 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 4c 61 pBt->pPager, iLa
1652b 73 74 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 stPg);. }. ret
1652c 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1652d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d ../*.** A write-
1652e 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 transaction must
1652f 20 62 65 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 be opened befor
16530 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 e calling this f
16531 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 74 20 70 unction..** It p
16532 65 72 66 6f 72 6d 73 20 61 20 73 69 6e 67 6c 65 erforms a single
16533 20 75 6e 69 74 20 6f 66 20 77 6f 72 6b 20 74 6f unit of work to
16534 77 61 72 64 73 20 61 6e 20 69 6e 63 72 65 6d 65 wards an increme
16535 6e 74 61 6c 20 76 61 63 75 75 6d 2e 0a 2a 2a 0a ntal vacuum..**.
16536 2a 2a 20 49 66 20 74 68 65 20 69 6e 63 72 65 6d ** If the increm
16537 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 69 73 20 ental vacuum is
16538 66 69 6e 69 73 68 65 64 20 61 66 74 65 72 20 74 finished after t
16539 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 his function has
1653a 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f run,.** SQLITE_
1653b 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e 65 64 DONE is returned
1653c 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 66 . If it is not f
1653d 69 6e 69 73 68 65 64 2c 20 62 75 74 20 6e 6f 20 inished, but no
1653e 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 0a error occurred,.
1653f 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
16540 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 returned. Otherw
16541 69 73 65 20 61 6e 20 53 51 4c 69 74 65 20 65 72 ise an SQLite er
16542 72 6f 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a 53 51 ror code. .*/.SQ
16543 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
16544 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 sqlite3BtreeInc
16545 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70 rVacuum(Btree *p
16546 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 ){. int rc;. B
16547 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
16548 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65 ->pBt;.. sqlite
16549 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
1654a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 assert( pBt->i
1654b 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 nTransaction==TR
1654c 41 4e 53 5f 57 52 49 54 45 20 26 26 20 70 2d 3e ANS_WRITE && p->
1654d 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 inTrans==TRANS_W
1654e 52 49 54 45 20 29 3b 0a 20 20 69 66 28 20 21 70 RITE );. if( !p
1654f 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
16550 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
16551 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b E_DONE;. }else{
16552 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41 . invalidateA
16553 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 llOverflowCache(
16554 70 42 74 29 3b 0a 20 20 20 20 72 63 20 3d 20 69 pBt);. rc = i
16555 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 70 42 ncrVacuumStep(pB
16556 74 2c 20 30 2c 20 70 61 67 65 72 50 61 67 65 63 t, 0, pagerPagec
16557 6f 75 6e 74 28 70 42 74 29 29 3b 0a 20 20 7d 0a ount(pBt));. }.
16558 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
16559 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
1655a 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
1655b 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
1655c 6c 6c 65 64 20 70 72 69 6f 72 20 74 6f 20 73 71 lled prior to sq
1655d 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 lite3PagerCommit
1655e 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 when a transact
1655f 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 ion.** is commit
16560 65 64 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76 ed for an auto-v
16561 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e 0a acuum database..
16562 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f **.** If SQLITE_
16563 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 OK is returned,
16564 74 68 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69 73 then *pnTrunc is
16565 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 set to the numb
16566 65 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 74 er of pages.** t
16567 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
16568 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 should be trunc
16569 61 74 65 64 20 74 6f 20 64 75 72 69 6e 67 20 74 ated to during t
1656a 68 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 he commit proces
1656b 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 s. .** i.e. the
1656c 64 61 74 61 62 61 73 65 20 68 61 73 20 62 65 65 database has bee
1656d 6e 20 72 65 6f 72 67 61 6e 69 7a 65 64 20 73 6f n reorganized so
1656e 20 74 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 66 that only the f
1656f 69 72 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a 2a irst *pnTrunc.**
16570 20 70 61 67 65 73 20 61 72 65 20 69 6e 20 75 73 pages are in us
16571 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
16572 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 autoVacuumCommi
16573 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 t(BtShared *pBt)
16574 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
16575 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 ITE_OK;. Pager
16576 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 *pPager = pBt->p
16577 50 61 67 65 72 3b 0a 20 20 56 56 41 5f 4f 4e 4c Pager;. VVA_ONL
16578 59 28 20 69 6e 74 20 6e 52 65 66 20 3d 20 73 71 Y( int nRef = sq
16579 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 lite3PagerRefcou
1657a 6e 74 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 nt(pPager) );..
1657b 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1657c 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
1657d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 6e 76 >mutex) );. inv
1657e 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c alidateAllOverfl
1657f 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 owCache(pBt);.
16580 61 73 73 65 72 74 28 70 42 74 2d 3e 61 75 74 6f assert(pBt->auto
16581 56 61 63 75 75 6d 29 3b 0a 20 20 69 66 28 20 21 Vacuum);. if( !
16582 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 pBt->incrVacuum
16583 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69 6e ){. Pgno nFin
16584 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ; /* Num
16585 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
16586 64 61 74 61 62 61 73 65 20 61 66 74 65 72 20 61 database after a
16587 75 74 6f 76 61 63 75 75 6d 69 6e 67 20 2a 2f 0a utovacuuming */.
16588 20 20 20 20 50 67 6e 6f 20 6e 46 72 65 65 3b 20 Pgno nFree;
16589 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1658a 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 of pages on the
1658b 20 66 72 65 65 6c 69 73 74 20 69 6e 69 74 69 61 freelist initia
1658c 6c 6c 79 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 lly */. Pgno
1658d 6e 50 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a nPtrmap; /*
1658e 20 4e 75 6d 62 65 72 20 6f 66 20 50 74 72 4d 61 Number of PtrMa
1658f 70 20 70 61 67 65 73 20 74 6f 20 62 65 20 66 72 p pages to be fr
16590 65 65 64 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 eed */. Pgno
16591 69 46 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a iFree; /*
16592 20 54 68 65 20 6e 65 78 74 20 70 61 67 65 20 74 The next page t
16593 6f 20 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 o be freed */.
16594 20 20 69 6e 74 20 6e 45 6e 74 72 79 3b 20 20 20 int nEntry;
16595 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
16596 66 20 65 6e 74 72 69 65 73 20 6f 6e 20 6f 6e 65 f entries on one
16597 20 70 74 72 6d 61 70 20 70 61 67 65 20 2a 2f 0a ptrmap page */.
16598 20 20 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 Pgno nOrig;
16599 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
1659a 73 65 20 73 69 7a 65 20 62 65 66 6f 72 65 20 66 se size before f
1659b 72 65 65 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 6e reeing */.. n
1659c 4f 72 69 67 20 3d 20 70 61 67 65 72 50 61 67 65 Orig = pagerPage
1659d 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 20 20 20 count(pBt);.
1659e 69 66 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 if( PTRMAP_ISPAG
1659f 45 28 70 42 74 2c 20 6e 4f 72 69 67 29 20 7c 7c E(pBt, nOrig) ||
165a0 20 6e 4f 72 69 67 3d 3d 50 45 4e 44 49 4e 47 5f nOrig==PENDING_
165a1 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 BYTE_PAGE(pBt) )
165a2 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 {. /* It is
165a3 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
165a4 20 63 72 65 61 74 65 20 61 20 64 61 74 61 62 61 create a databa
165a5 73 65 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 se for which the
165a6 20 66 69 6e 61 6c 20 70 61 67 65 0a 20 20 20 20 final page.
165a7 20 20 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61 ** is either a
165a8 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 pointer-map pag
165a9 65 20 6f 72 20 74 68 65 20 70 65 6e 64 69 6e 67 e or the pending
165aa 2d 62 79 74 65 20 70 61 67 65 2e 20 49 66 20 6f -byte page. If o
165ab 6e 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65 ne. ** is e
165ac 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 68 69 73 ncountered, this
165ad 20 69 6e 64 69 63 61 74 65 73 20 63 6f 72 72 75 indicates corru
165ae 70 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a ption.. */.
165af 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
165b0 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
165b1 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 46 72 ;. }.. nFr
165b2 65 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 ee = get4byte(&p
165b3 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
165b4 61 5b 33 36 5d 29 3b 0a 20 20 20 20 6e 45 6e 74 a[36]);. nEnt
165b5 72 79 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 ry = pBt->usable
165b6 53 69 7a 65 2f 35 3b 0a 20 20 20 20 6e 50 74 72 Size/5;. nPtr
165b7 6d 61 70 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 map = (nFree-nOr
165b8 69 67 2b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f ig+PTRMAP_PAGENO
165b9 28 70 42 74 2c 20 6e 4f 72 69 67 29 2b 6e 45 6e (pBt, nOrig)+nEn
165ba 74 72 79 29 2f 6e 45 6e 74 72 79 3b 0a 20 20 20 try)/nEntry;.
165bb 20 6e 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d 20 nFin = nOrig -
165bc 6e 46 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 3b nFree - nPtrmap;
165bd 0a 20 20 20 20 69 66 28 20 6e 4f 72 69 67 3e 50 . if( nOrig>P
165be 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
165bf 28 70 42 74 29 20 26 26 20 6e 46 69 6e 3c 50 45 (pBt) && nFin<PE
165c0 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
165c1 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 pBt) ){. nF
165c2 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 in--;. }.
165c3 77 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 while( PTRMAP_IS
165c4 50 41 47 45 28 70 42 74 2c 20 6e 46 69 6e 29 20 PAGE(pBt, nFin)
165c5 7c 7c 20 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47 || nFin==PENDING
165c6 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
165c7 29 7b 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b ){. nFin--;
165c8 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e . }. if( n
165c9 46 69 6e 3e 6e 4f 72 69 67 20 29 20 72 65 74 75 Fin>nOrig ) retu
165ca 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
165cb 54 5f 42 4b 50 54 3b 0a 0a 20 20 20 20 66 6f 72 T_BKPT;.. for
165cc 28 69 46 72 65 65 3d 6e 4f 72 69 67 3b 20 69 46 (iFree=nOrig; iF
165cd 72 65 65 3e 6e 46 69 6e 20 26 26 20 72 63 3d 3d ree>nFin && rc==
165ce 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 46 72 65 65 SQLITE_OK; iFree
165cf 2d 2d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 --){. rc =
165d0 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 70 incrVacuumStep(p
165d1 42 74 2c 20 6e 46 69 6e 2c 20 69 46 72 65 65 29 Bt, nFin, iFree)
165d2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
165d3 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 (rc==SQLITE_DONE
165d4 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f || rc==SQLITE_O
165d5 4b 29 20 26 26 20 6e 46 72 65 65 3e 30 20 29 7b K) && nFree>0 ){
165d6 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
165d7 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 72 63 20 TE_OK;. rc
165d8 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
165d9 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d ite(pBt->pPage1-
165da 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
165db 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e put4byte(&pBt->
165dc 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 pPage1->aData[32
165dd 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 75 74 ], 0);. put
165de 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 4byte(&pBt->pPag
165df 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 30 e1->aData[36], 0
165e0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
165e1 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 PagerTruncateIma
165e2 67 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 ge(pBt->pPager,
165e3 6e 46 69 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 nFin);. }.
165e4 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
165e5 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 OK ){. sqli
165e6 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
165e7 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a (pPager);. }.
165e8 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e }.. assert( n
165e9 52 65 66 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 Ref==sqlite3Page
165ea 72 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72 rRefcount(pPager
165eb 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ) );. return rc
165ec 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 ;.}..#else /* if
165ed 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
165ee 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 23 _AUTOVACUUM */.#
165ef 20 64 65 66 69 6e 65 20 73 65 74 43 68 69 6c 64 define setChild
165f0 50 74 72 6d 61 70 73 28 78 29 20 53 51 4c 49 54 Ptrmaps(x) SQLIT
165f1 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a E_OK.#endif../*.
165f2 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
165f3 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70 does the first p
165f4 68 61 73 65 20 6f 66 20 61 20 74 77 6f 2d 70 68 hase of a two-ph
165f5 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 69 ase commit. Thi
165f6 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 75 s routine.** cau
165f7 73 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a ses a rollback j
165f8 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 63 72 65 ournal to be cre
165f9 61 74 65 64 20 28 69 66 20 69 74 20 64 6f 65 73 ated (if it does
165fa 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 not already exi
165fb 73 74 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 6c st).** and popul
165fc 61 74 65 64 20 77 69 74 68 20 65 6e 6f 75 67 68 ated with enough
165fd 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f 20 information so
165fe 74 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 20 that if a power
165ff 6c 6f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20 74 loss occurs.** t
16600 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 he database can
16601 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 be restored to i
16602 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 ts original stat
16603 65 20 62 79 20 70 6c 61 79 69 6e 67 20 62 61 63 e by playing bac
16604 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c k.** the journal
16605 2e 20 20 54 68 65 6e 20 74 68 65 20 63 6f 6e 74 . Then the cont
16606 65 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 ents of the jour
16607 6e 61 6c 20 61 72 65 20 66 6c 75 73 68 65 64 20 nal are flushed
16608 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 69 out to.** the di
16609 73 6b 2e 20 20 41 66 74 65 72 20 74 68 65 20 6a sk. After the j
1660a 6f 75 72 6e 61 6c 20 69 73 20 73 61 66 65 6c 79 ournal is safely
1660b 20 6f 6e 20 6f 78 69 64 65 2c 20 74 68 65 20 63 on oxide, the c
1660c 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a hanges to the.**
1660d 20 64 61 74 61 62 61 73 65 20 61 72 65 20 77 72 database are wr
1660e 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 64 itten into the d
1660f 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
16610 20 66 6c 75 73 68 65 64 20 74 6f 20 6f 78 69 64 flushed to oxid
16611 65 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 e..** At the end
16612 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 2c 20 74 of this call, t
16613 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 he rollback jour
16614 6e 61 6c 20 73 74 69 6c 6c 20 65 78 69 73 74 73 nal still exists
16615 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20 on the.** disk
16616 61 6e 64 20 77 65 20 61 72 65 20 73 74 69 6c 6c and we are still
16617 20 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f 63 holding all loc
16618 6b 73 2c 20 73 6f 20 74 68 65 20 74 72 61 6e 73 ks, so the trans
16619 61 63 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a 2a action has not.*
1661a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53 65 * committed. Se
1661b 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f e sqlite3BtreeCo
1661c 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 66 mmitPhaseTwo() f
1661d 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 or the second ph
1661e 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f ase of the.** co
1661f 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a mmit process..**
16620 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 .** This call is
16621 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 77 a no-op if no w
16622 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
16623 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 63 is currently ac
16624 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a 0a tive on pBt..**.
16625 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73 79 ** Otherwise, sy
16626 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20 nc the database
16627 66 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74 72 file for the btr
16628 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72 20 ee pBt. zMaster
16629 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 points to.** the
1662a 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 name of a maste
1662b 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 r journal file t
1662c 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 hat should be wr
1662d 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a itten into the.*
1662e 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 * individual jou
1662f 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69 73 rnal file, or is
16630 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69 6e NULL, indicatin
16631 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 g no master jour
16632 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73 69 nal file .** (si
16633 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 ngle database tr
16634 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a ansaction)..**.*
16635 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 * When this is c
16636 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74 65 alled, the maste
16637 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c 64 r journal should
16638 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62 65 already have be
16639 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20 70 en.** created, p
1663a 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 opulated with th
1663b 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 is journal point
1663c 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74 6f er and synced to
1663d 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 disk..**.** Onc
1663e 65 20 74 68 69 73 20 69 73 20 72 6f 75 74 69 6e e this is routin
1663f 65 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c 20 e has returned,
16640 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 72 the only thing r
16641 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d 69 equired to commi
16642 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d 74 t.** the write-t
16643 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 74 ransaction for t
16644 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c his database fil
16645 65 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20 74 e is to delete t
16646 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a 53 he journal..*/.S
16647 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16648 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f t sqlite3BtreeCo
16649 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72 mmitPhaseOne(Btr
1664a 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 ee *p, const cha
1664b 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 r *zMaster){. i
1664c 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1664d 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 K;. if( p->inTr
1664e 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
1664f 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 ){. BtShared
16650 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
16651 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
16652 45 6e 74 65 72 28 70 29 3b 0a 23 69 66 6e 64 65 Enter(p);.#ifnde
16653 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
16654 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 TOVACUUM. if(
16655 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
16656 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 ){. rc = a
16657 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28 utoVacuumCommit(
16658 70 42 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 pBt);. if(
16659 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1665a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1665b 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
1665c 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
1665d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1665e 23 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20 #endif. rc =
1665f 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d sqlite3PagerComm
16660 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2d 3e itPhaseOne(pBt->
16661 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 2c pPager, zMaster,
16662 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 0);. sqlite3
16663 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
16664 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
16665 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
16666 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
16667 20 66 72 6f 6d 20 62 6f 74 68 20 42 74 72 65 65 from both Btree
16668 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 CommitPhaseTwo()
16669 20 61 6e 64 20 42 74 72 65 65 52 6f 6c 6c 62 61 and BtreeRollba
1666a 63 6b 28 29 0a 2a 2a 20 61 74 20 74 68 65 20 63 ck().** at the c
1666b 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 61 20 74 onclusion of a t
1666c 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 ransaction..*/.s
1666d 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 tatic void btree
1666e 45 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 42 EndTransaction(B
1666f 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 tree *p){. BtSh
16670 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
16671 42 74 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a Bt;. BtCursor *
16672 70 43 73 72 3b 0a 20 20 61 73 73 65 72 74 28 20 pCsr;. assert(
16673 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
16674 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 0a 20 20 sMutex(p) );..
16675 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61 20 /* Search for a
16676 63 75 72 73 6f 72 20 68 65 6c 64 20 6f 70 65 6e cursor held open
16677 20 62 79 20 74 68 69 73 20 62 2d 74 72 65 65 20 by this b-tree
16678 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 66 20 6f connection. If o
16679 6e 65 20 65 78 69 73 74 73 2c 0a 20 20 2a 2a 20 ne exists,. **
1667a 74 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 then the transac
1667b 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 64 6f 77 tion will be dow
1667c 6e 67 72 61 64 65 64 20 74 6f 20 61 20 72 65 61 ngraded to a rea
1667d 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 d-only transacti
1667e 6f 6e 0a 20 20 2a 2a 20 69 6e 73 74 65 61 64 20 on. ** instead
1667f 6f 66 20 61 63 74 75 61 6c 6c 79 20 63 6f 6e 63 of actually conc
16680 6c 75 64 65 64 2e 20 41 20 73 75 62 73 65 71 75 luded. A subsequ
16681 65 6e 74 20 63 61 6c 6c 20 74 6f 20 43 6f 6d 6d ent call to Comm
16682 69 74 50 68 61 73 65 54 77 6f 28 29 20 0a 20 20 itPhaseTwo() .
16683 2a 2a 20 6f 72 20 52 6f 6c 6c 62 61 63 6b 28 29 ** or Rollback()
16684 20 77 69 6c 6c 20 66 69 6e 69 73 68 20 74 68 65 will finish the
16685 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 transaction and
16686 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 unlock the data
16687 62 61 73 65 2e 20 20 2a 2f 0a 20 20 66 6f 72 28 base. */. for(
16688 70 43 73 72 3d 70 42 74 2d 3e 70 43 75 72 73 6f pCsr=pBt->pCurso
16689 72 3b 20 70 43 73 72 20 26 26 20 70 43 73 72 2d r; pCsr && pCsr-
1668a 3e 70 42 74 72 65 65 21 3d 70 3b 20 70 43 73 72 >pBtree!=p; pCsr
1668b 3d 70 43 73 72 2d 3e 70 4e 65 78 74 29 3b 0a 20 =pCsr->pNext);.
1668c 20 61 73 73 65 72 74 28 20 70 43 73 72 3d 3d 30 assert( pCsr==0
1668d 20 7c 7c 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 || p->inTrans>T
1668e 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 0a 20 20 RANS_NONE );..
1668f 62 74 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e btreeClearHasCon
16690 74 65 6e 74 28 70 42 74 29 3b 0a 20 20 69 66 28 tent(pBt);. if(
16691 20 70 43 73 72 20 29 7b 0a 20 20 20 20 64 6f 77 pCsr ){. dow
16692 6e 67 72 61 64 65 41 6c 6c 53 68 61 72 65 64 43 ngradeAllSharedC
16693 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 acheTableLocks(p
16694 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e );. p->inTran
16695 73 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a s = TRANS_READ;.
16696 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
16697 49 66 20 74 68 65 20 68 61 6e 64 6c 65 20 68 61 If the handle ha
16698 64 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 74 72 d any kind of tr
16699 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 ansaction open,
1669a 64 65 63 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 decrement the .
1669b 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f ** transactio
1669c 6e 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 73 n count of the s
1669d 68 61 72 65 64 20 62 74 72 65 65 2e 20 49 66 20 hared btree. If
1669e 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
1669f 63 6f 75 6e 74 20 0a 20 20 20 20 2a 2a 20 72 65 count . ** re
166a0 61 63 68 65 73 20 30 2c 20 73 65 74 20 74 68 65 aches 0, set the
166a1 20 73 68 61 72 65 64 20 73 74 61 74 65 20 74 6f shared state to
166a2 20 54 52 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 TRANS_NONE. The
166a3 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e unlockBtreeIfUn
166a4 75 73 65 64 28 29 0a 20 20 20 20 2a 2a 20 63 61 used(). ** ca
166a5 6c 6c 20 62 65 6c 6f 77 20 77 69 6c 6c 20 75 6e ll below will un
166a6 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 72 2e 20 lock the pager.
166a7 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 */. if( p->i
166a8 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f nTrans!=TRANS_NO
166a9 4e 45 20 29 7b 0a 20 20 20 20 20 20 63 6c 65 61 NE ){. clea
166aa 72 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 rAllSharedCacheT
166ab 61 62 6c 65 4c 6f 63 6b 73 28 70 29 3b 0a 20 20 ableLocks(p);.
166ac 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 pBt->nTransa
166ad 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 20 20 69 ction--;. i
166ae 66 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e f( 0==pBt->nTran
166af 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 saction ){.
166b0 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 pBt->inTransa
166b1 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f ction = TRANS_NO
166b2 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 NE;. }.
166b3 7d 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 }.. /* Set th
166b4 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 e current transa
166b5 63 74 69 6f 6e 20 73 74 61 74 65 20 74 6f 20 54 ction state to T
166b6 52 41 4e 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e RANS_NONE and un
166b7 6c 6f 63 6b 20 74 68 65 20 0a 20 20 20 20 2a 2a lock the . **
166b8 20 70 61 67 65 72 20 69 66 20 74 68 69 73 20 63 pager if this c
166b9 61 6c 6c 20 63 6c 6f 73 65 64 20 74 68 65 20 6f all closed the o
166ba 6e 6c 79 20 72 65 61 64 20 6f 72 20 77 72 69 74 nly read or writ
166bb 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 e transaction.
166bc 2a 2f 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e */. p->inTran
166bd 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a s = TRANS_NONE;.
166be 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 unlockBtreeI
166bf 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 fUnused(pBt);.
166c0 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 }.. btreeIntegr
166c1 69 74 79 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ity(p);.}../*.**
166c2 20 43 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e Commit the tran
166c3 73 61 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c saction currentl
166c4 79 20 69 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a y in progress..*
166c5 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
166c6 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 e implements the
166c7 20 73 65 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 second phase of
166c8 20 61 20 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69 a 2-phase commi
166c9 74 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 t. The.** sqlit
166ca 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
166cb 73 65 4f 6e 65 28 29 20 72 6f 75 74 69 6e 65 20 seOne() routine
166cc 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70 does the first p
166cd 68 61 73 65 20 61 6e 64 20 73 68 6f 75 6c 64 0a hase and should.
166ce 2a 2a 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72 ** be invoked pr
166cf 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 ior to calling t
166d0 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68 his routine. Th
166d1 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f e sqlite3BtreeCo
166d2 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 0a 2a mmitPhaseOne().*
166d3 2a 20 72 6f 75 74 69 6e 65 20 64 69 64 20 61 6c * routine did al
166d4 6c 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 77 72 l the work of wr
166d5 69 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f iting informatio
166d6 6e 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61 6e n out to disk an
166d7 64 20 66 6c 75 73 68 69 6e 67 20 74 68 65 0a 2a d flushing the.*
166d8 2a 20 63 6f 6e 74 65 6e 74 73 20 73 6f 20 74 68 * contents so th
166d9 61 74 20 74 68 65 79 20 61 72 65 20 77 72 69 74 at they are writ
166da 74 65 6e 20 6f 6e 74 6f 20 74 68 65 20 64 69 73 ten onto the dis
166db 6b 20 70 6c 61 74 74 65 72 2e 20 20 41 6c 6c 20 k platter. All
166dc 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 this.** routine
166dd 68 61 73 20 74 6f 20 64 6f 20 69 73 20 64 65 6c has to do is del
166de 65 74 65 20 6f 72 20 74 72 75 6e 63 61 74 65 20 ete or truncate
166df 6f 72 20 7a 65 72 6f 20 74 68 65 20 68 65 61 64 or zero the head
166e0 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 74 68 65 er in the.** the
166e1 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
166e2 6c 20 28 77 68 69 63 68 20 63 61 75 73 65 73 20 l (which causes
166e3 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
166e4 74 6f 20 63 6f 6d 6d 69 74 29 20 61 6e 64 0a 2a to commit) and.*
166e5 2a 20 64 72 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a * drop locks..**
166e6 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 .** This will re
166e7 6c 65 61 73 65 20 74 68 65 20 77 72 69 74 65 20 lease the write
166e8 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
166e9 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74 base file. If t
166ea 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 here.** are no a
166eb 63 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69 ctive cursors, i
166ec 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 t also releases
166ed 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a the read lock..*
166ee 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
166ef 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
166f0 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 eCommitPhaseTwo(
166f1 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 Btree *p){. BtS
166f2 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
166f3 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 pBt;.. sqlite3B
166f4 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
166f5 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 btreeIntegrity(p
166f6 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
166f7 68 61 6e 64 6c 65 20 68 61 73 20 61 20 77 72 69 handle has a wri
166f8 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f te-transaction o
166f9 70 65 6e 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20 pen, commit the
166fa 73 68 61 72 65 64 2d 62 74 72 65 65 73 20 0a 20 shared-btrees .
166fb 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ** transaction
166fc 61 6e 64 20 73 65 74 20 74 68 65 20 73 68 61 72 and set the shar
166fd 65 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e ed state to TRAN
166fe 53 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69 S_READ.. */. i
166ff 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 f( p->inTrans==T
16700 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 RANS_WRITE ){.
16701 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61 73 int rc;. as
16702 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 sert( pBt->inTra
16703 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
16704 57 52 49 54 45 20 29 3b 0a 20 20 20 20 61 73 73 WRITE );. ass
16705 65 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 ert( pBt->nTrans
16706 61 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 action>0 );.
16707 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
16708 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 rCommitPhaseTwo(
16709 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 pBt->pPager);.
1670a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1670b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
1670c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1670d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1670e 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 rc;. }. pB
1670f 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
16710 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 = TRANS_READ;.
16711 20 7d 0a 0a 20 20 62 74 72 65 65 45 6e 64 54 72 }.. btreeEndTr
16712 61 6e 73 61 63 74 69 6f 6e 28 70 29 3b 0a 20 20 ansaction(p);.
16713 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
16714 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 e(p);. return S
16715 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
16716 2a 2a 20 44 6f 20 62 6f 74 68 20 70 68 61 73 65 ** Do both phase
16717 73 20 6f 66 20 61 20 63 6f 6d 6d 69 74 2e 0a 2a s of a commit..*
16718 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16719 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
1671a 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 20 2a 70 eCommit(Btree *p
1671b 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 ){. int rc;. s
1671c 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1671d 28 70 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 (p);. rc = sqli
1671e 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
1671f 61 73 65 4f 6e 65 28 70 2c 20 30 29 3b 0a 20 20 aseOne(p, 0);.
16720 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
16721 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 K ){. rc = sq
16722 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 lite3BtreeCommit
16723 50 68 61 73 65 54 77 6f 28 70 29 3b 0a 20 20 7d PhaseTwo(p);. }
16724 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
16725 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
16726 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 n rc;.}..#ifndef
16727 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 NDEBUG./*.** Re
16728 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
16729 6f 66 20 77 72 69 74 65 2d 63 75 72 73 6f 72 73 of write-cursors
1672a 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 68 61 open on this ha
1672b 6e 64 6c 65 2e 20 54 68 69 73 20 69 73 20 66 6f ndle. This is fo
1672c 72 20 75 73 65 0a 2a 2a 20 69 6e 20 61 73 73 65 r use.** in asse
1672d 72 74 28 29 20 65 78 70 72 65 73 73 69 6f 6e 73 rt() expressions
1672e 2c 20 73 6f 20 69 74 20 69 73 20 6f 6e 6c 79 20 , so it is only
1672f 63 6f 6d 70 69 6c 65 64 20 69 66 20 4e 44 45 42 compiled if NDEB
16730 55 47 20 69 73 20 6e 6f 74 0a 2a 2a 20 64 65 66 UG is not.** def
16731 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 ined..**.** For
16732 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 the purposes of
16733 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 20 this routine, a
16734 77 72 69 74 65 2d 63 75 72 73 6f 72 20 69 73 20 write-cursor is
16735 61 6e 79 20 63 75 72 73 6f 72 20 74 68 61 74 0a any cursor that.
16736 2a 2a 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66 ** is capable of
16737 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 writing to the
16738 64 61 74 61 62 73 65 2e 20 20 54 68 61 74 20 6d databse. That m
16739 65 61 6e 73 20 74 68 65 20 63 75 72 73 6f 72 20 eans the cursor
1673a 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c was.** originall
1673b 79 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 y opened for wri
1673c 74 69 6e 67 20 61 6e 64 20 74 68 65 20 63 75 72 ting and the cur
1673d 73 6f 72 20 68 61 73 20 6e 6f 74 20 62 65 20 64 sor has not be d
1673e 69 73 61 62 6c 65 64 0a 2a 2a 20 62 79 20 68 61 isabled.** by ha
1673f 76 69 6e 67 20 69 74 73 20 73 74 61 74 65 20 63 ving its state c
16740 68 61 6e 67 65 64 20 74 6f 20 43 55 52 53 4f 52 hanged to CURSOR
16741 5f 46 41 55 4c 54 2e 0a 2a 2f 0a 73 74 61 74 69 _FAULT..*/.stati
16742 63 20 69 6e 74 20 63 6f 75 6e 74 57 72 69 74 65 c int countWrite
16743 43 75 72 73 6f 72 73 28 42 74 53 68 61 72 65 64 Cursors(BtShared
16744 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 72 73 *pBt){. BtCurs
16745 6f 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20 or *pCur;. int
16746 72 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 43 75 r = 0;. for(pCu
16747 72 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 r=pBt->pCursor;
16748 70 43 75 72 3b 20 70 43 75 72 3d 70 43 75 72 2d pCur; pCur=pCur-
16749 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 >pNext){. if(
1674a 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 26 26 pCur->wrFlag &&
1674b 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 pCur->eState!=C
1674c 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 20 72 2b URSOR_FAULT ) r+
1674d 2b 3b 20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e +; . }. return
1674e 20 72 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a r;.}.#endif../*
1674f 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
16750 20 73 65 74 73 20 74 68 65 20 73 74 61 74 65 20 sets the state
16751 74 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 to CURSOR_FAULT
16752 61 6e 64 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a and the error.**
16753 20 63 6f 64 65 20 74 6f 20 65 72 72 43 6f 64 65 code to errCode
16754 20 66 6f 72 20 65 76 65 72 79 20 63 75 72 73 6f for every curso
16755 72 20 6f 6e 20 42 74 53 68 61 72 65 64 20 74 68 r on BtShared th
16756 61 74 20 70 42 74 72 65 65 0a 2a 2a 20 72 65 66 at pBtree.** ref
16757 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 erences..**.** E
16758 76 65 72 79 20 63 75 72 73 6f 72 20 69 73 20 74 very cursor is t
16759 72 69 70 70 65 64 2c 20 69 6e 63 6c 75 64 69 6e ripped, includin
1675a 67 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 62 g cursors that b
1675b 65 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 6f 74 68 65 elong.** to othe
1675c 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
1675d 63 74 69 6f 6e 73 20 74 68 61 74 20 68 61 70 70 ctions that happ
1675e 65 6e 20 74 6f 20 62 65 20 73 68 61 72 69 6e 67 en to be sharing
1675f 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 20 77 69 .** the cache wi
16760 74 68 20 70 42 74 72 65 65 2e 0a 2a 2a 0a 2a 2a th pBtree..**.**
16761 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 This routine ge
16762 74 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 ts called when a
16763 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 rollback occurs
16764 2e 0a 2a 2a 20 41 6c 6c 20 63 75 72 73 6f 72 73 ..** All cursors
16765 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 using the same
16766 63 61 63 68 65 20 6d 75 73 74 20 62 65 20 74 72 cache must be tr
16767 69 70 70 65 64 0a 2a 2a 20 74 6f 20 70 72 65 76 ipped.** to prev
16768 65 6e 74 20 74 68 65 6d 20 66 72 6f 6d 20 74 72 ent them from tr
16769 79 69 6e 67 20 74 6f 20 75 73 65 20 74 68 65 20 ying to use the
1676a 62 74 72 65 65 20 61 66 74 65 72 0a 2a 2a 20 74 btree after.** t
1676b 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 he rollback. Th
1676c 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 e rollback may h
1676d 61 76 65 20 64 65 6c 65 74 65 64 20 74 61 62 6c ave deleted tabl
1676e 65 73 0a 2a 2a 20 6f 72 20 6d 6f 76 65 64 20 72 es.** or moved r
1676f 6f 6f 74 20 70 61 67 65 73 2c 20 73 6f 20 69 74 oot pages, so it
16770 20 69 73 20 6e 6f 74 20 73 75 66 66 69 63 69 65 is not sufficie
16771 6e 74 20 74 6f 0a 2a 2a 20 73 61 76 65 20 74 68 nt to.** save th
16772 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 63 e state of the c
16773 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73 ursor. The curs
16774 6f 72 20 6d 75 73 74 20 62 65 0a 2a 2a 20 69 6e or must be.** in
16775 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a 53 51 validated..*/.SQ
16776 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
16777 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 d sqlite3BtreeTr
16778 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 72 ipAllCursors(Btr
16779 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 ee *pBtree, int
1677a 65 72 72 43 6f 64 65 29 7b 0a 20 20 42 74 43 75 errCode){. BtCu
1677b 72 73 6f 72 20 2a 70 3b 0a 20 20 73 71 6c 69 74 rsor *p;. sqlit
1677c 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 e3BtreeEnter(pBt
1677d 72 65 65 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 ree);. for(p=pB
1677e 74 72 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 tree->pBt->pCurs
1677f 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 or; p; p=p->pNex
16780 74 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 t){. int i;.
16781 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 sqlite3BtreeC
16782 6c 65 61 72 43 75 72 73 6f 72 28 70 29 3b 0a 20 learCursor(p);.
16783 20 20 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 p->eState = C
16784 55 52 53 4f 52 5f 46 41 55 4c 54 3b 0a 20 20 20 URSOR_FAULT;.
16785 20 70 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 65 p->skipNext = e
16786 72 72 43 6f 64 65 3b 0a 20 20 20 20 66 6f 72 28 rrCode;. for(
16787 69 3d 30 3b 20 69 3c 3d 70 2d 3e 69 50 61 67 65 i=0; i<=p->iPage
16788 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 ; i++){. re
16789 6c 65 61 73 65 50 61 67 65 28 70 2d 3e 61 70 50 leasePage(p->apP
1678a 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 age[i]);. p
1678b 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20 30 3b ->apPage[i] = 0;
1678c 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c . }. }. sql
1678d 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1678e 42 74 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Btree);.}../*.**
1678f 20 52 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 Rollback the tr
16790 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f ansaction in pro
16791 67 72 65 73 73 2e 20 20 41 6c 6c 20 63 75 72 73 gress. All curs
16792 6f 72 73 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 69 ors will be.** i
16793 6e 76 61 6c 69 64 65 64 20 62 79 20 74 68 69 73 nvalided by this
16794 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79 operation. Any
16795 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 attempt to use
16796 61 20 63 75 72 73 6f 72 0a 2a 2a 20 74 68 61 74 a cursor.** that
16797 20 77 61 73 20 6f 70 65 6e 20 61 74 20 74 68 65 was open at the
16798 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
16799 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c is operation wil
1679a 6c 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61 l result.** in a
1679b 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 n error..**.** T
1679c 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 his will release
1679d 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 the write lock
1679e 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
1679f 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a file. If there.
167a0 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 ** are no active
167a1 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 cursors, it als
167a2 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 20 72 o releases the r
167a3 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c ead lock..*/.SQL
167a4 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
167a5 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c sqlite3BtreeRoll
167a6 62 61 63 6b 28 42 74 72 65 65 20 2a 70 29 7b 0a back(Btree *p){.
167a7 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 int rc;. BtSh
167a8 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
167a9 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 Bt;. MemPage *p
167aa 50 61 67 65 31 3b 0a 0a 20 20 73 71 6c 69 74 65 Page1;.. sqlite
167ab 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
167ac 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 rc = saveAllCu
167ad 72 73 6f 72 73 28 70 42 74 2c 20 30 2c 20 30 29 rsors(pBt, 0, 0)
167ae 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
167af 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
167b0 48 45 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c HE. if( rc!=SQL
167b1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a ITE_OK ){. /*
167b2 20 54 68 69 73 20 69 73 20 61 20 68 6f 72 72 69 This is a horri
167b3 62 6c 65 20 73 69 74 75 61 74 69 6f 6e 2e 20 41 ble situation. A
167b4 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 n IO or malloc()
167b5 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 error occurred
167b6 77 68 69 6c 73 74 0a 20 20 20 20 2a 2a 20 74 72 whilst. ** tr
167b7 79 69 6e 67 20 74 6f 20 73 61 76 65 20 63 75 72 ying to save cur
167b8 73 6f 72 20 70 6f 73 69 74 69 6f 6e 73 2e 20 49 sor positions. I
167b9 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 f this is an aut
167ba 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 omatic rollback
167bb 28 61 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 (as. ** the r
167bc 65 73 75 6c 74 20 6f 66 20 61 20 63 6f 6e 73 74 esult of a const
167bd 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f 63 28 29 20 raint, malloc()
167be 66 61 69 6c 75 72 65 20 6f 72 20 49 4f 20 65 72 failure or IO er
167bf 72 6f 72 29 20 74 68 65 6e 20 0a 20 20 20 20 2a ror) then . *
167c0 2a 20 74 68 65 20 63 61 63 68 65 20 6d 61 79 20 * the cache may
167c1 62 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 69 6e be internally in
167c2 63 6f 6e 73 69 73 74 65 6e 74 20 28 6e 6f 74 20 consistent (not
167c3 63 6f 6e 74 61 69 6e 20 76 61 6c 69 64 20 74 72 contain valid tr
167c4 65 65 73 29 20 73 6f 0a 20 20 20 20 2a 2a 20 77 ees) so. ** w
167c5 65 20 63 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20 e cannot simply
167c6 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 return the error
167c7 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 to the caller.
167c8 49 6e 73 74 65 61 64 2c 20 61 62 6f 72 74 20 0a Instead, abort .
167c9 20 20 20 20 2a 2a 20 61 6c 6c 20 71 75 65 72 69 ** all queri
167ca 65 73 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 es that may be u
167cb 73 69 6e 67 20 61 6e 79 20 6f 66 20 74 68 65 20 sing any of the
167cc 63 75 72 73 6f 72 73 20 74 68 61 74 20 66 61 69 cursors that fai
167cd 6c 65 64 20 74 6f 20 73 61 76 65 2e 0a 20 20 20 led to save..
167ce 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 */. sqlite3B
167cf 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f treeTripAllCurso
167d0 72 73 28 70 2c 20 72 63 29 3b 0a 20 20 7d 0a 23 rs(p, rc);. }.#
167d1 65 6e 64 69 66 0a 20 20 62 74 72 65 65 49 6e 74 endif. btreeInt
167d2 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 69 66 egrity(p);.. if
167d3 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
167d4 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 ANS_WRITE ){.
167d5 20 69 6e 74 20 72 63 32 3b 0a 0a 20 20 20 20 61 int rc2;.. a
167d6 73 73 65 72 74 28 20 54 52 41 4e 53 5f 57 52 49 ssert( TRANS_WRI
167d7 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 72 61 6e 73 TE==pBt->inTrans
167d8 61 63 74 69 6f 6e 20 29 3b 0a 20 20 20 20 72 63 action );. rc
167d9 32 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 2 = sqlite3Pager
167da 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d 3e 70 50 Rollback(pBt->pP
167db 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 ager);. if( r
167dc 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b c2!=SQLITE_OK ){
167dd 0a 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b . rc = rc2;
167de 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 . }.. /* T
167df 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 he rollback may
167e0 68 61 76 65 20 64 65 73 74 72 6f 79 65 64 20 74 have destroyed t
167e1 68 65 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 he pPage1->aData
167e2 20 76 61 6c 75 65 2e 20 20 53 6f 0a 20 20 20 20 value. So.
167e3 2a 2a 20 63 61 6c 6c 20 62 74 72 65 65 47 65 74 ** call btreeGet
167e4 50 61 67 65 28 29 20 6f 6e 20 70 61 67 65 20 31 Page() on page 1
167e5 20 61 67 61 69 6e 20 74 6f 20 6d 61 6b 65 0a 20 again to make.
167e6 20 20 20 2a 2a 20 73 75 72 65 20 70 50 61 67 65 ** sure pPage
167e7 31 2d 3e 61 44 61 74 61 20 69 73 20 73 65 74 20 1->aData is set
167e8 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 correctly. */.
167e9 20 20 69 66 28 20 62 74 72 65 65 47 65 74 50 61 if( btreeGetPa
167ea 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67 ge(pBt, 1, &pPag
167eb 65 31 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 4f e1, 0)==SQLITE_O
167ec 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 K ){. relea
167ed 73 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a sePage(pPage1);.
167ee 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
167ef 28 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73 ( countWriteCurs
167f0 6f 72 73 28 70 42 74 29 3d 3d 30 20 29 3b 0a 20 ors(pBt)==0 );.
167f1 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 pBt->inTransa
167f2 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 ction = TRANS_RE
167f3 41 44 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 AD;. }.. btree
167f4 45 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 EndTransaction(p
167f5 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
167f6 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
167f7 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
167f8 20 53 74 61 72 74 20 61 20 73 74 61 74 65 6d 65 Start a stateme
167f9 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f nt subtransactio
167fa 6e 2e 20 54 68 65 20 73 75 62 74 72 61 6e 73 61 n. The subtransa
167fb 63 74 69 6f 6e 20 63 61 6e 20 63 61 6e 20 62 65 ction can can be
167fc 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 rolled.** back
167fd 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 20 6f 66 independently of
167fe 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 the main transa
167ff 63 74 69 6f 6e 2e 20 59 6f 75 20 6d 75 73 74 20 ction. You must
16800 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 start a transact
16801 69 6f 6e 20 0a 2a 2a 20 62 65 66 6f 72 65 20 73 ion .** before s
16802 74 61 72 74 69 6e 67 20 61 20 73 75 62 74 72 61 tarting a subtra
16803 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 nsaction. The su
16804 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 btransaction is
16805 65 6e 64 65 64 20 61 75 74 6f 6d 61 74 69 63 61 ended automatica
16806 6c 6c 79 20 0a 2a 2a 20 69 66 20 74 68 65 20 6d lly .** if the m
16807 61 69 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ain transaction
16808 63 6f 6d 6d 69 74 73 20 6f 72 20 72 6f 6c 6c 73 commits or rolls
16809 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 74 61 back..**.** Sta
1680a 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 tement subtransa
1680b 63 74 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20 ctions are used
1680c 61 72 6f 75 6e 64 20 69 6e 64 69 76 69 64 75 61 around individua
1680d 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 l SQL statements
1680e 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 63 6f 6e .** that are con
1680f 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 61 20 tained within a
16810 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49 54 20 62 BEGIN...COMMIT b
16811 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63 6f 6e 73 lock. If a cons
16812 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 20 traint.** error
16813 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 74 68 occurs within th
16814 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 e statement, the
16815 20 65 66 66 65 63 74 20 6f 66 20 74 68 61 74 20 effect of that
16816 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a one statement.**
16817 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 can be rolled b
16818 61 63 6b 20 77 69 74 68 6f 75 74 20 68 61 76 69 ack without havi
16819 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 ng to rollback t
1681a 68 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 he entire transa
1681b 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 ction..**.** A s
1681c 74 61 74 65 6d 65 6e 74 20 73 75 62 2d 74 72 61 tatement sub-tra
1681d 6e 73 61 63 74 69 6f 6e 20 69 73 20 69 6d 70 6c nsaction is impl
1681e 65 6d 65 6e 74 65 64 20 61 73 20 61 6e 20 61 6e emented as an an
1681f 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e onymous savepoin
16820 74 2e 20 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20 t. The.** value
16821 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 passed as the se
16822 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 cond parameter i
16823 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 s the total numb
16824 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 73 er of savepoints
16825 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74 ,.** including t
16826 68 65 20 6e 65 77 20 61 6e 6f 6e 79 6d 6f 75 73 he new anonymous
16827 20 73 61 76 65 70 6f 69 6e 74 2c 20 6f 70 65 6e savepoint, open
16828 20 6f 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20 on the B-Tree.
16829 69 2e 65 2e 20 69 66 20 74 68 65 72 65 0a 2a 2a i.e. if there.**
1682a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 73 are no active s
1682b 61 76 65 70 6f 69 6e 74 73 20 61 6e 64 20 6e 6f avepoints and no
1682c 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 other statement
1682d 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70 -transactions op
1682e 65 6e 2c 0a 2a 2a 20 69 53 74 61 74 65 6d 65 6e en,.** iStatemen
1682f 74 20 69 73 20 31 2e 20 54 68 69 73 20 61 6e 6f t is 1. This ano
16830 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 nymous savepoint
16831 20 63 61 6e 20 62 65 20 72 65 6c 65 61 73 65 64 can be released
16832 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 0a or rolled back.
16833 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c ** using the sql
16834 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 ite3BtreeSavepoi
16835 6e 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a nt() function..*
16836 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16837 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
16838 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72 65 65 eBeginStmt(Btree
16839 20 2a 70 2c 20 69 6e 74 20 69 53 74 61 74 65 6d *p, int iStatem
1683a 65 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ent){. int rc;.
1683b 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
1683c 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 = p->pBt;. sqli
1683d 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
1683e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 ;. assert( p->i
1683f 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 nTrans==TRANS_WR
16840 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ITE );. assert(
16841 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d pBt->readOnly==
16842 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 0 );. assert( i
16843 53 74 61 74 65 6d 65 6e 74 3e 30 20 29 3b 0a 20 Statement>0 );.
16844 20 61 73 73 65 72 74 28 20 69 53 74 61 74 65 6d assert( iStatem
16845 65 6e 74 3e 70 2d 3e 64 62 2d 3e 6e 53 61 76 65 ent>p->db->nSave
16846 70 6f 69 6e 74 20 29 3b 0a 20 20 69 66 28 20 4e point );. if( N
16847 45 56 45 52 28 70 2d 3e 69 6e 54 72 61 6e 73 21 EVER(p->inTrans!
16848 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 7c 7c 20 =TRANS_WRITE ||
16849 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29 pBt->readOnly) )
1684a 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
1684b 45 5f 49 4e 54 45 52 4e 41 4c 3b 0a 20 20 7d 65 E_INTERNAL;. }e
1684c 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
1684d 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
1684e 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion==TRANS_WRITE
1684f 20 29 3b 0a 20 20 20 20 2f 2a 20 41 74 20 74 68 );. /* At th
16850 65 20 70 61 67 65 72 20 6c 65 76 65 6c 2c 20 61 e pager level, a
16851 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
16852 61 63 74 69 6f 6e 20 69 73 20 61 20 73 61 76 65 action is a save
16853 70 6f 69 6e 74 20 77 69 74 68 0a 20 20 20 20 2a point with. *
16854 2a 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61 74 * an index great
16855 65 72 20 74 68 61 6e 20 61 6c 6c 20 73 61 76 65 er than all save
16856 70 6f 69 6e 74 73 20 63 72 65 61 74 65 64 20 65 points created e
16857 78 70 6c 69 63 69 74 6c 79 20 75 73 69 6e 67 0a xplicitly using.
16858 20 20 20 20 2a 2a 20 53 51 4c 20 73 74 61 74 65 ** SQL state
16859 6d 65 6e 74 73 2e 20 49 74 20 69 73 20 69 6c 6c ments. It is ill
1685a 65 67 61 6c 20 74 6f 20 6f 70 65 6e 2c 20 72 65 egal to open, re
1685b 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63 lease or rollbac
1685c 6b 20 61 6e 79 0a 20 20 20 20 2a 2a 20 73 75 63 k any. ** suc
1685d 68 20 73 61 76 65 70 6f 69 6e 74 73 20 77 68 69 h savepoints whi
1685e 6c 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 le the statement
1685f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 transaction sav
16860 65 70 6f 69 6e 74 20 69 73 20 61 63 74 69 76 65 epoint is active
16861 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
16862 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 = sqlite3PagerOp
16863 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d enSavepoint(pBt-
16864 3e 70 50 61 67 65 72 2c 20 69 53 74 61 74 65 6d >pPager, iStatem
16865 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 ent);. }. sqli
16866 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
16867 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
16868 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f ../*.** The seco
16869 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 nd argument to t
1686a 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 70 his function, op
1686b 2c 20 69 73 20 61 6c 77 61 79 73 20 53 41 56 45 , is always SAVE
1686c 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 0a 2a POINT_ROLLBACK.*
1686d 2a 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 * or SAVEPOINT_R
1686e 45 4c 45 41 53 45 2e 20 54 68 69 73 20 66 75 6e ELEASE. This fun
1686f 63 74 69 6f 6e 20 65 69 74 68 65 72 20 72 65 6c ction either rel
16870 65 61 73 65 73 20 6f 72 20 72 6f 6c 6c 73 20 62 eases or rolls b
16871 61 63 6b 20 74 68 65 0a 2a 2a 20 73 61 76 65 70 ack the.** savep
16872 6f 69 6e 74 20 69 64 65 6e 74 69 66 69 65 64 20 oint identified
16873 62 79 20 70 61 72 61 6d 65 74 65 72 20 69 53 61 by parameter iSa
16874 76 65 70 6f 69 6e 74 2c 20 64 65 70 65 6e 64 69 vepoint, dependi
16875 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 ng on the value
16876 0a 2a 2a 20 6f 66 20 6f 70 2e 0a 2a 2a 0a 2a 2a .** of op..**.**
16877 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 53 61 76 65 Normally, iSave
16878 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65 72 point is greater
16879 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
1687a 6f 20 7a 65 72 6f 2e 20 48 6f 77 65 76 65 72 2c o zero. However,
1687b 20 69 66 20 6f 70 20 69 73 0a 2a 2a 20 53 41 56 if op is.** SAV
1687c 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c EPOINT_ROLLBACK,
1687d 20 74 68 65 6e 20 69 53 61 76 65 70 6f 69 6e 74 then iSavepoint
1687e 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 2d 31 2e may also be -1.
1687f 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
16880 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f e .** contents o
16881 66 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61 f the entire tra
16882 6e 73 61 63 74 69 6f 6e 20 61 72 65 20 72 6f 6c nsaction are rol
16883 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20 69 led back. This i
16884 73 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 s different.** f
16885 72 6f 6d 20 61 20 6e 6f 72 6d 61 6c 20 74 72 61 rom a normal tra
16886 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 62 61 63 nsaction rollbac
16887 6b 2c 20 61 73 20 6e 6f 20 6c 6f 63 6b 73 20 61 k, as no locks a
16888 72 65 20 72 65 6c 65 61 73 65 64 20 61 6e 64 20 re released and
16889 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 the.** transacti
1688a 6f 6e 20 72 65 6d 61 69 6e 73 20 6f 70 65 6e 2e on remains open.
1688b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1688c 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
1688d 72 65 65 53 61 76 65 70 6f 69 6e 74 28 42 74 72 reeSavepoint(Btr
1688e 65 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 ee *p, int op, i
1688f 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a nt iSavepoint){.
16890 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
16891 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 20 26 26 E_OK;. if( p &&
16892 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 p->inTrans==TRA
16893 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 NS_WRITE ){.
16894 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
16895 70 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 73 65 p->pBt;. asse
16896 72 74 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e rt( op==SAVEPOIN
16897 54 5f 52 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d T_RELEASE || op=
16898 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 =SAVEPOINT_ROLLB
16899 41 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ACK );. asser
1689a 74 28 20 69 53 61 76 65 70 6f 69 6e 74 3e 3d 30 t( iSavepoint>=0
1689b 20 7c 7c 20 28 69 53 61 76 65 70 6f 69 6e 74 3d || (iSavepoint=
1689c 3d 2d 31 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 =-1 && op==SAVEP
1689d 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 20 29 OINT_ROLLBACK) )
1689e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1689f 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 eeEnter(p);.
168a0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
168a1 72 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e rSavepoint(pBt->
168a2 70 50 61 67 65 72 2c 20 6f 70 2c 20 69 53 61 76 pPager, op, iSav
168a3 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 69 66 28 epoint);. if(
168a4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
168a5 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 65 77 {. rc = new
168a6 44 61 74 61 62 61 73 65 28 70 42 74 29 3b 0a 20 Database(pBt);.
168a7 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
168a8 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
168a9 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
168aa 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 }../*.** Create
168ab 61 20 6e 65 77 20 63 75 72 73 6f 72 20 66 6f 72 a new cursor for
168ac 20 74 68 65 20 42 54 72 65 65 20 77 68 6f 73 65 the BTree whose
168ad 20 72 6f 6f 74 20 69 73 20 6f 6e 20 74 68 65 20 root is on the
168ae 70 61 67 65 0a 2a 2a 20 69 54 61 62 6c 65 2e 20 page.** iTable.
168af 49 66 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 63 If a read-only c
168b0 75 72 73 6f 72 20 69 73 20 72 65 71 75 65 73 74 ursor is request
168b1 65 64 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65 ed, it is assume
168b2 64 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 d that.** the ca
168b3 6c 6c 65 72 20 61 6c 72 65 61 64 79 20 68 61 73 ller already has
168b4 20 61 74 20 6c 65 61 73 74 20 61 20 72 65 61 64 at least a read
168b5 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 6f -only transactio
168b6 6e 20 6f 70 65 6e 0a 2a 2a 20 6f 6e 20 74 68 65 n open.** on the
168b7 20 64 61 74 61 62 61 73 65 20 61 6c 72 65 61 64 database alread
168b8 79 2e 20 49 66 20 61 20 77 72 69 74 65 2d 63 75 y. If a write-cu
168b9 72 73 6f 72 20 69 73 20 72 65 71 75 65 73 74 65 rsor is requeste
168ba 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 63 d, then.** the c
168bb 61 6c 6c 65 72 20 69 73 20 61 73 73 75 6d 65 64 aller is assumed
168bc 20 74 6f 20 68 61 76 65 20 61 6e 20 6f 70 65 6e to have an open
168bd 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
168be 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 46 on..**.** If wrF
168bf 6c 61 67 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 lag==0, then the
168c0 20 63 75 72 73 6f 72 20 63 61 6e 20 6f 6e 6c 79 cursor can only
168c1 20 62 65 20 75 73 65 64 20 66 6f 72 20 72 65 61 be used for rea
168c2 64 69 6e 67 2e 0a 2a 2a 20 49 66 20 77 72 46 6c ding..** If wrFl
168c3 61 67 3d 3d 31 2c 20 74 68 65 6e 20 74 68 65 20 ag==1, then the
168c4 63 75 72 73 6f 72 20 63 61 6e 20 62 65 20 75 73 cursor can be us
168c5 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f ed for reading o
168c6 72 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 r for.** writing
168c7 20 69 66 20 6f 74 68 65 72 20 63 6f 6e 64 69 74 if other condit
168c8 69 6f 6e 73 20 66 6f 72 20 77 72 69 74 69 6e 67 ions for writing
168c9 20 61 72 65 20 61 6c 73 6f 20 6d 65 74 2e 20 20 are also met.
168ca 54 68 65 73 65 0a 2a 2a 20 61 72 65 20 74 68 65 These.** are the
168cb 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 conditions that
168cc 20 6d 75 73 74 20 62 65 20 6d 65 74 20 69 6e 20 must be met in
168cd 6f 72 64 65 72 20 66 6f 72 20 77 72 69 74 69 6e order for writin
168ce 67 20 74 6f 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77 g to.** be allow
168cf 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68 ed:.**.** 1: Th
168d0 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20 68 61 e cursor must ha
168d1 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 77 ve been opened w
168d2 69 74 68 20 77 72 46 6c 61 67 3d 3d 31 0a 2a 2a ith wrFlag==1.**
168d3 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 72 20 64 61 .** 2: Other da
168d4 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
168d5 6e 73 20 74 68 61 74 20 73 68 61 72 65 20 74 68 ns that share th
168d6 65 20 73 61 6d 65 20 70 61 67 65 72 20 63 61 63 e same pager cac
168d7 68 65 0a 2a 2a 20 20 20 20 20 62 75 74 20 77 68 he.** but wh
168d8 69 63 68 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 ich are not in t
168d9 68 65 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 he READ_UNCOMMIT
168da 54 45 44 20 73 74 61 74 65 20 6d 61 79 20 6e 6f TED state may no
168db 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 63 75 t have.** cu
168dc 72 73 6f 72 73 20 6f 70 65 6e 20 77 69 74 68 20 rsors open with
168dd 77 72 46 6c 61 67 3d 3d 30 20 6f 6e 20 74 68 65 wrFlag==0 on the
168de 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 20 4f 74 same table. Ot
168df 68 65 72 77 69 73 65 0a 2a 2a 20 20 20 20 20 74 herwise.** t
168e0 68 65 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 he changes made
168e1 62 79 20 74 68 69 73 20 77 72 69 74 65 20 63 75 by this write cu
168e2 72 73 6f 72 20 77 6f 75 6c 64 20 62 65 20 76 69 rsor would be vi
168e3 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 20 20 20 20 sible to.**
168e4 74 68 65 20 72 65 61 64 20 63 75 72 73 6f 72 73 the read cursors
168e5 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 64 61 in the other da
168e6 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
168e7 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 54 68 65 n..**.** 3: The
168e8 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 database must b
168e9 65 20 77 72 69 74 61 62 6c 65 20 28 6e 6f 74 20 e writable (not
168ea 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 65 64 on read-only med
168eb 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68 ia).**.** 4: Th
168ec 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 61 ere must be an a
168ed 63 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f ctive transactio
168ee 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63 n..**.** No chec
168ef 6b 69 6e 67 20 69 73 20 64 6f 6e 65 20 74 6f 20 king is done to
168f0 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 70 make sure that p
168f1 61 67 65 20 69 54 61 62 6c 65 20 72 65 61 6c 6c age iTable reall
168f2 79 20 69 73 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 y is the.** root
168f3 20 70 61 67 65 20 6f 66 20 61 20 62 2d 74 72 65 page of a b-tre
168f4 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 e. If it is not
168f5 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f , then the curso
168f6 72 20 61 63 71 75 69 72 65 64 0a 2a 2a 20 77 69 r acquired.** wi
168f7 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 ll not work corr
168f8 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 ectly..**.** It
168f9 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 is assumed that
168fa 74 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 the sqlite3Btree
168fb 43 75 72 73 6f 72 53 69 7a 65 28 29 20 62 79 74 CursorSize() byt
168fc 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 0a 2a 2a es of memory .**
168fd 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 pointed to by p
168fe 43 75 72 20 68 61 76 65 20 62 65 65 6e 20 7a 65 Cur have been ze
168ff 72 6f 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c roed by the call
16900 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e er..*/.static in
16901 74 20 62 74 72 65 65 43 75 72 73 6f 72 28 0a 20 t btreeCursor(.
16902 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 20 Btree *p,
16903 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16904 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 /* The b
16905 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 tree */. int iT
16906 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 able,
16907 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16908 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 /* Root page of
16909 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a table to open *
1690a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 /. int wrFlag,
1690b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1690c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 /* 1
1690d 74 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61 64 to write. 0 read
1690e 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 -only */. struc
1690f 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 t KeyInfo *pKeyI
16910 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 nfo,
16911 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 /* First arg t
16912 6f 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e o comparison fun
16913 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 ction */. BtCur
16914 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20 20 sor *pCur
16915 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16916 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 6e /* Space for n
16917 65 77 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a ew cursor */.){.
16918 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
16919 3d 20 70 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 = p->pBt;
1691a 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61 72 /* Shar
1691b 65 64 20 62 2d 74 72 65 65 20 68 61 6e 64 6c 65 ed b-tree handle
1691c 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 */.. assert( s
1691d 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
1691e 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 Mutex(p) );. as
1691f 73 65 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 sert( wrFlag==0
16920 7c 7c 20 77 72 46 6c 61 67 3d 3d 31 20 29 3b 0a || wrFlag==1 );.
16921 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 . /* The follow
16922 69 6e 67 20 61 73 73 65 72 74 20 73 74 61 74 65 ing assert state
16923 6d 65 6e 74 73 20 76 65 72 69 66 79 20 74 68 61 ments verify tha
16924 74 20 69 66 20 74 68 69 73 20 69 73 20 61 20 73 t if this is a s
16925 68 61 72 61 62 6c 65 20 0a 20 20 2a 2a 20 62 2d harable . ** b-
16926 74 72 65 65 20 64 61 74 61 62 61 73 65 2c 20 74 tree database, t
16927 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 he connection is
16928 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 65 71 holding the req
16929 75 69 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b uired table lock
1692a 73 2c 20 0a 20 20 2a 2a 20 61 6e 64 20 74 68 61 s, . ** and tha
1692b 74 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 t no other conne
1692c 63 74 69 6f 6e 20 68 61 73 20 61 6e 79 20 6f 70 ction has any op
1692d 65 6e 20 63 75 72 73 6f 72 20 74 68 61 74 20 63 en cursor that c
1692e 6f 6e 66 6c 69 63 74 73 20 77 69 74 68 20 0a 20 onflicts with .
1692f 20 2a 2a 20 74 68 69 73 20 6c 6f 63 6b 2e 20 20 ** this lock.
16930 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 68 61 73 */. assert( has
16931 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
16932 4c 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 Lock(p, iTable,
16933 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 77 72 46 pKeyInfo!=0, wrF
16934 6c 61 67 2b 31 29 20 29 3b 0a 20 20 61 73 73 65 lag+1) );. asse
16935 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c rt( wrFlag==0 ||
16936 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 !hasReadConflic
16937 74 73 28 70 2c 20 69 54 61 62 6c 65 29 20 29 3b ts(p, iTable) );
16938 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 .. /* Assert th
16939 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 at the caller ha
1693a 73 20 6f 70 65 6e 65 64 20 74 68 65 20 72 65 71 s opened the req
1693b 75 69 72 65 64 20 74 72 61 6e 73 61 63 74 69 6f uired transactio
1693c 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 n. */. assert(
1693d 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 p->inTrans>TRANS
1693e 5f 4e 4f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 _NONE );. asser
1693f 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 t( wrFlag==0 ||
16940 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
16941 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 S_WRITE );. ass
16942 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 ert( pBt->pPage1
16943 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 31 2d && pBt->pPage1-
16944 3e 61 44 61 74 61 20 29 3b 0a 0a 20 20 69 66 28 >aData );.. if(
16945 20 4e 45 56 45 52 28 77 72 46 6c 61 67 20 26 26 NEVER(wrFlag &&
16946 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 pBt->readOnly)
16947 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
16948 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 LITE_READONLY;.
16949 20 7d 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3d }. if( iTable=
1694a 3d 31 20 26 26 20 70 61 67 65 72 50 61 67 65 63 =1 && pagerPagec
1694b 6f 75 6e 74 28 70 42 74 29 3d 3d 30 20 29 7b 0a ount(pBt)==0 ){.
1694c 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1694d 45 5f 45 4d 50 54 59 3b 0a 20 20 7d 0a 0a 20 20 E_EMPTY;. }..
1694e 2f 2a 20 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f /* Now that no o
1694f 74 68 65 72 20 65 72 72 6f 72 73 20 63 61 6e 20 ther errors can
16950 6f 63 63 75 72 2c 20 66 69 6e 69 73 68 20 66 69 occur, finish fi
16951 6c 6c 69 6e 67 20 69 6e 20 74 68 65 20 42 74 43 lling in the BtC
16952 75 72 73 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61 ursor. ** varia
16953 62 6c 65 73 20 61 6e 64 20 6c 69 6e 6b 20 74 68 bles and link th
16954 65 20 63 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 e cursor into th
16955 65 20 42 74 53 68 61 72 65 64 20 6c 69 73 74 2e e BtShared list.
16956 20 20 2a 2f 0a 20 20 70 43 75 72 2d 3e 70 67 6e */. pCur->pgn
16957 6f 52 6f 6f 74 20 3d 20 28 50 67 6e 6f 29 69 54 oRoot = (Pgno)iT
16958 61 62 6c 65 3b 0a 20 20 70 43 75 72 2d 3e 69 50 able;. pCur->iP
16959 61 67 65 20 3d 20 2d 31 3b 0a 20 20 70 43 75 72 age = -1;. pCur
1695a 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 ->pKeyInfo = pKe
1695b 79 49 6e 66 6f 3b 0a 20 20 70 43 75 72 2d 3e 70 yInfo;. pCur->p
1695c 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 70 43 75 Btree = p;. pCu
1695d 72 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 r->pBt = pBt;.
1695e 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 3d 20 28 pCur->wrFlag = (
1695f 75 38 29 77 72 46 6c 61 67 3b 0a 20 20 70 43 75 u8)wrFlag;. pCu
16960 72 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e r->pNext = pBt->
16961 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 70 pCursor;. if( p
16962 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 Cur->pNext ){.
16963 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 pCur->pNext->p
16964 50 72 65 76 20 3d 20 70 43 75 72 3b 0a 20 20 7d Prev = pCur;. }
16965 0a 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 . pBt->pCursor
16966 3d 20 70 43 75 72 3b 0a 20 20 70 43 75 72 2d 3e = pCur;. pCur->
16967 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f eState = CURSOR_
16968 49 4e 56 41 4c 49 44 3b 0a 20 20 70 43 75 72 2d INVALID;. pCur-
16969 3e 63 61 63 68 65 64 52 6f 77 69 64 20 3d 20 30 >cachedRowid = 0
1696a 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1696b 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 E_OK;.}.SQLITE_P
1696c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1696d 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a 20 e3BtreeCursor(.
1696e 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 20 Btree *p,
1696f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16970 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16971 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 69 The btree */. i
16972 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 nt iTable,
16973 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16974 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f /* Ro
16975 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 ot page of table
16976 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e to open */. in
16977 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 t wrFlag,
16978 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16979 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 74 /* 1 t
1697a 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61 64 2d o write. 0 read-
1697b 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 only */. struct
1697c 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
1697d 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 fo,
1697e 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 /* First a
1697f 72 67 20 74 6f 20 78 43 6f 6d 70 61 72 65 28 29 rg to xCompare()
16980 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a */. BtCursor *
16981 70 43 75 72 20 20 20 20 20 20 20 20 20 20 20 20 pCur
16982 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16983 20 20 2f 2a 20 57 72 69 74 65 20 6e 65 77 20 63 /* Write new c
16984 75 72 73 6f 72 20 68 65 72 65 20 2a 2f 0a 29 7b ursor here */.){
16985 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c . int rc;. sql
16986 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
16987 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 );. rc = btreeC
16988 75 72 73 6f 72 28 70 2c 20 69 54 61 62 6c 65 2c ursor(p, iTable,
16989 20 77 72 46 6c 61 67 2c 20 70 4b 65 79 49 6e 66 wrFlag, pKeyInf
1698a 6f 2c 20 70 43 75 72 29 3b 0a 20 20 73 71 6c 69 o, pCur);. sqli
1698b 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
1698c 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
1698d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1698e 68 65 20 73 69 7a 65 20 6f 66 20 61 20 42 74 43 he size of a BtC
1698f 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 6e 20 ursor object in
16990 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 bytes..**.** Thi
16991 73 20 69 6e 74 65 72 66 61 63 65 73 20 69 73 20 s interfaces is
16992 6e 65 65 64 65 64 20 73 6f 20 74 68 61 74 20 75 needed so that u
16993 73 65 72 73 20 6f 66 20 63 75 72 73 6f 72 73 20 sers of cursors
16994 63 61 6e 20 70 72 65 61 6c 6c 6f 63 61 74 65 0a can preallocate.
16995 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 73 74 ** sufficient st
16996 6f 72 61 67 65 20 74 6f 20 68 6f 6c 64 20 61 20 orage to hold a
16997 63 75 72 73 6f 72 2e 20 20 54 68 65 20 42 74 43 cursor. The BtC
16998 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 73 20 ursor object is
16999 6f 70 61 71 75 65 0a 2a 2a 20 74 6f 20 75 73 65 opaque.** to use
1699a 72 73 20 73 6f 20 74 68 65 79 20 63 61 6e 6e 6f rs so they canno
1699b 74 20 64 6f 20 74 68 65 20 73 69 7a 65 6f 66 28 t do the sizeof(
1699c 29 20 74 68 65 6d 73 65 6c 76 65 73 20 2d 20 74 ) themselves - t
1699d 68 65 79 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a hey must call.**
1699e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a this routine..*
1699f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
169a0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
169a1 65 43 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 eCursorSize(void
169a2 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 69 7a 65 ){. return size
169a3 6f 66 28 42 74 43 75 72 73 6f 72 29 3b 0a 7d 0a of(BtCursor);.}.
169a4 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 ./*.** Set the c
169a5 61 63 68 65 64 20 72 6f 77 69 64 20 76 61 6c 75 ached rowid valu
169a6 65 20 6f 66 20 65 76 65 72 79 20 63 75 72 73 6f e of every curso
169a7 72 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 r in the same da
169a8 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 tabase file.** a
169a9 73 20 70 43 75 72 20 61 6e 64 20 68 61 76 69 6e s pCur and havin
169aa 67 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 20 g the same root
169ab 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 70 page number as p
169ac 43 75 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20 Cur. The value
169ad 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 69 52 6f is.** set to iRo
169ae 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 wid..**.** Only
169af 70 6f 73 69 74 69 76 65 20 72 6f 77 69 64 20 76 positive rowid v
169b0 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69 64 alues are consid
169b1 65 72 65 64 20 76 61 6c 69 64 20 66 6f 72 20 74 ered valid for t
169b2 68 69 73 20 63 61 63 68 65 2e 0a 2a 2a 20 54 68 his cache..** Th
169b3 65 20 63 61 63 68 65 20 69 73 20 69 6e 69 74 69 e cache is initi
169b4 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2c 20 alized to zero,
169b5 69 6e 64 69 63 61 74 69 6e 67 20 61 6e 20 69 6e indicating an in
169b6 76 61 6c 69 64 20 63 61 63 68 65 2e 0a 2a 2a 20 valid cache..**
169b7 41 20 62 74 72 65 65 20 77 69 6c 6c 20 77 6f 72 A btree will wor
169b8 6b 20 66 69 6e 65 20 77 69 74 68 20 7a 65 72 6f k fine with zero
169b9 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77 or negative row
169ba 69 64 73 2e 20 20 57 65 20 6a 75 73 74 20 63 61 ids. We just ca
169bb 6e 6e 6f 74 0a 2a 2a 20 63 61 63 68 65 20 7a 65 nnot.** cache ze
169bc 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 ro or negative r
169bd 6f 77 69 64 73 2c 20 77 68 69 63 68 20 6d 65 61 owids, which mea
169be 6e 73 20 74 61 62 6c 65 73 20 74 68 61 74 20 75 ns tables that u
169bf 73 65 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 se zero or.** ne
169c0 67 61 74 69 76 65 20 72 6f 77 69 64 73 20 6d 69 gative rowids mi
169c1 67 68 74 20 72 75 6e 20 61 20 6c 69 74 74 6c 65 ght run a little
169c2 20 73 6c 6f 77 65 72 2e 20 20 42 75 74 20 69 6e slower. But in
169c3 20 70 72 61 63 74 69 63 65 2c 20 7a 65 72 6f 0a practice, zero.
169c4 2a 2a 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 ** or negative r
169c5 6f 77 69 64 73 20 61 72 65 20 76 65 72 79 20 75 owids are very u
169c6 6e 63 6f 6d 6d 6f 6e 20 73 6f 20 74 68 69 73 20 ncommon so this
169c7 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 20 should not be a
169c8 70 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 problem..*/.SQLI
169c9 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
169ca 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 sqlite3BtreeSetC
169cb 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 achedRowid(BtCur
169cc 73 6f 72 20 2a 70 43 75 72 2c 20 73 71 6c 69 74 sor *pCur, sqlit
169cd 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29 e3_int64 iRowid)
169ce 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b {. BtCursor *p;
169cf 0a 20 20 66 6f 72 28 70 3d 70 43 75 72 2d 3e 70 . for(p=pCur->p
169d0 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 Bt->pCursor; p;
169d1 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 p=p->pNext){.
169d2 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 if( p->pgnoRoot
169d3 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 ==pCur->pgnoRoot
169d4 20 29 20 70 2d 3e 63 61 63 68 65 64 52 6f 77 69 ) p->cachedRowi
169d5 64 20 3d 20 69 52 6f 77 69 64 3b 0a 20 20 7d 0a d = iRowid;. }.
169d6 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
169d7 63 61 63 68 65 64 52 6f 77 69 64 3d 3d 69 52 6f cachedRowid==iRo
169d8 77 69 64 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 wid );.}../*.**
169d9 52 65 74 75 72 6e 20 74 68 65 20 63 61 63 68 65 Return the cache
169da 64 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 d rowid for the
169db 67 69 76 65 6e 20 63 75 72 73 6f 72 2e 20 20 41 given cursor. A
169dc 20 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72 negative or zer
169dd 6f 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 o.** return valu
169de 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 e indicates that
169df 20 74 68 65 20 72 6f 77 69 64 20 63 61 63 68 65 the rowid cache
169e0 20 69 73 20 69 6e 76 61 6c 69 64 20 61 6e 64 20 is invalid and
169e1 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 67 6e should be.** ign
169e2 6f 72 65 64 2e 20 20 49 66 20 74 68 65 20 72 6f ored. If the ro
169e3 77 69 64 20 63 61 63 68 65 20 68 61 73 20 6e 65 wid cache has ne
169e4 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20 ver before been
169e5 73 65 74 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 7a set, then a.** z
169e6 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e ero is returned.
169e7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
169e8 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 TE sqlite3_int64
169e9 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
169ea 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 CachedRowid(BtCu
169eb 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72 rsor *pCur){. r
169ec 65 74 75 72 6e 20 70 43 75 72 2d 3e 63 61 63 68 eturn pCur->cach
169ed 65 64 52 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a edRowid;.}../*.*
169ee 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 * Close a cursor
169ef 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b . The read lock
169f0 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
169f1 20 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73 65 file is release
169f2 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 d.** when the la
169f3 73 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f st cursor is clo
169f4 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 sed..*/.SQLITE_P
169f5 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
169f6 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 e3BtreeCloseCurs
169f7 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 or(BtCursor *pCu
169f8 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 r){. Btree *pBt
169f9 72 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 ree = pCur->pBtr
169fa 65 65 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 ee;. if( pBtree
169fb 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
169fc 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 BtShared *pBt
169fd 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20 = pCur->pBt;.
169fe 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
169ff 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20 ter(pBtree);.
16a00 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 sqlite3BtreeCle
16a01 61 72 43 75 72 73 6f 72 28 70 43 75 72 29 3b 0a arCursor(pCur);.
16a02 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 70 50 if( pCur->pP
16a03 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70 43 75 rev ){. pCu
16a04 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 r->pPrev->pNext
16a05 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 = pCur->pNext;.
16a06 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
16a07 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70 pBt->pCursor = p
16a08 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 Cur->pNext;.
16a09 7d 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e }. if( pCur->
16a0a 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 pNext ){. p
16a0b 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 Cur->pNext->pPre
16a0c 76 20 3d 20 70 43 75 72 2d 3e 70 50 72 65 76 3b v = pCur->pPrev;
16a0d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 . }. for(i
16a0e 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 =0; i<=pCur->iPa
16a0f 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ge; i++){.
16a10 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 releasePage(pCur
16a11 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 ->apPage[i]);.
16a12 20 20 7d 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 }. unlockBt
16a13 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 reeIfUnused(pBt)
16a14 3b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 ;. invalidate
16a15 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43 OverflowCache(pC
16a16 75 72 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69 ur);. /* sqli
16a17 74 65 33 5f 66 72 65 65 28 70 43 75 72 29 3b 20 te3_free(pCur);
16a18 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 */. sqlite3Bt
16a19 72 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 29 reeLeave(pBtree)
16a1a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
16a1b 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
16a1c 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 ** Make sure the
16a1d 20 42 74 43 75 72 73 6f 72 2a 20 67 69 76 65 6e BtCursor* given
16a1e 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 in the argument
16a1f 20 68 61 73 20 61 20 76 61 6c 69 64 0a 2a 2a 20 has a valid.**
16a20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 73 74 BtCursor.info st
16a21 72 75 63 74 75 72 65 2e 20 20 49 66 20 69 74 20 ructure. If it
16a22 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 76 is not already v
16a23 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a 2a 20 62 74 alid, call.** bt
16a24 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74 reeParseCell() t
16a25 6f 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a o fill it in..**
16a26 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 .** BtCursor.inf
16a27 6f 20 69 73 20 61 20 63 61 63 68 65 20 6f 66 20 o is a cache of
16a28 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 the information
16a29 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 in the current c
16a2a 65 6c 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68 ell..** Using th
16a2b 69 73 20 63 61 63 68 65 20 72 65 64 75 63 65 73 is cache reduces
16a2c 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
16a2d 61 6c 6c 73 20 74 6f 20 62 74 72 65 65 50 61 72 alls to btreePar
16a2e 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a 2a 20 seCell()..**.**
16a2f 32 30 30 37 2d 30 36 2d 32 35 3a 20 20 54 68 65 2007-06-25: The
16a30 72 65 20 69 73 20 61 20 62 75 67 20 69 6e 20 73 re is a bug in s
16a31 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 ome versions of
16a32 4d 53 56 43 20 74 68 61 74 20 63 61 75 73 65 20 MSVC that cause
16a33 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 the.** compiler
16a34 74 6f 20 63 72 61 73 68 20 77 68 65 6e 20 67 65 to crash when ge
16a35 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73 20 69 tCellInfo() is i
16a36 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 mplemented as a
16a37 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 74 68 macro..** But th
16a38 65 72 65 20 69 73 20 61 20 6d 65 61 73 75 72 65 ere is a measure
16a39 61 62 6c 65 20 73 70 65 65 64 20 61 64 76 61 6e able speed advan
16a3a 74 61 67 65 20 74 6f 20 75 73 69 6e 67 20 74 68 tage to using th
16a3b 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63 0a 2a e macro on gcc.*
16a3c 2a 20 28 77 68 65 6e 20 6c 65 73 73 20 63 6f 6d * (when less com
16a3d 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 piler optimizati
16a3e 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f 72 20 ons like -Os or
16a3f 2d 4f 30 20 61 72 65 20 75 73 65 64 20 61 6e 64 -O0 are used and
16a40 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 the.** compiler
16a41 20 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20 61 67 is not doing ag
16a42 72 65 73 73 69 76 65 20 69 6e 6c 69 6e 69 6e 67 ressive inlining
16a43 2e 29 20 20 53 6f 20 77 65 20 75 73 65 20 61 20 .) So we use a
16a44 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a real function.**
16a45 20 66 6f 72 20 4d 53 56 43 20 61 6e 64 20 61 20 for MSVC and a
16a46 6d 61 63 72 6f 20 66 6f 72 20 65 76 65 72 79 74 macro for everyt
16a47 68 69 6e 67 20 65 6c 73 65 2e 20 20 54 69 63 6b hing else. Tick
16a48 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 69 66 et #2457..*/.#if
16a49 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 74 ndef NDEBUG. st
16a4a 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 atic void assert
16a4b 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f CellInfo(BtCurso
16a4c 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 43 65 r *pCur){. Ce
16a4d 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 llInfo info;.
16a4e 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 int iPage = pCu
16a4f 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 6d 65 r->iPage;. me
16a50 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c 20 73 mset(&info, 0, s
16a51 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a 20 20 izeof(info));.
16a52 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c btreeParseCell
16a53 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 (pCur->apPage[iP
16a54 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64 age], pCur->aiId
16a55 78 5b 69 50 61 67 65 5d 2c 20 26 69 6e 66 6f 29 x[iPage], &info)
16a56 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 ;. assert( me
16a57 6d 63 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 75 mcmp(&info, &pCu
16a58 72 2d 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 r->info, sizeof(
16a59 69 6e 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d info))==0 );. }
16a5a 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
16a5b 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 assertCellInfo(
16a5c 78 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 x).#endif.#ifdef
16a5d 20 5f 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20 55 _MSC_VER. /* U
16a5e 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 se a real functi
16a5f 6f 6e 20 69 6e 20 4d 53 56 43 20 74 6f 20 77 6f on in MSVC to wo
16a60 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 73 20 69 rk around bugs i
16a61 6e 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 2e n that compiler.
16a62 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69 */. static voi
16a63 64 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42 74 d getCellInfo(Bt
16a64 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
16a65 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 if( pCur->inf
16a66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 o.nSize==0 ){.
16a67 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 int iPage =
16a68 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 pCur->iPage;.
16a69 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c btreeParseCel
16a6a 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 l(pCur->apPage[i
16a6b 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 Page],pCur->aiId
16a6c 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e x[iPage],&pCur->
16a6d 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75 info);. pCu
16a6e 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 r->validNKey = 1
16a6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
16a70 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 assertCellInf
16a71 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 o(pCur);. }.
16a72 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e }.#else /* if n
16a73 6f 74 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 ot _MSC_VER */.
16a74 20 2f 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20 /* Use a macro
16a75 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d in all other com
16a76 70 69 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74 pilers so that t
16a77 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 he function is i
16a78 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e nlined */.#defin
16a79 65 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 e getCellInfo(pC
16a7a 75 72 29 20 20 20 20 20 20 20 20 20 20 20 20 20 ur)
16a7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a7d 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 \. if(
16a7e 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
16a7f 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 e==0 ){
16a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a82 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
16a83 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 int iPage = pCur
16a84 2d 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 ->iPage;
16a85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a87 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
16a88 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 btreeParseCell(
16a89 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 pCur->apPage[iPa
16a8a 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b ge],pCur->aiIdx[
16a8b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e iPage],&pCur->in
16a8c 66 6f 29 3b 20 5c 0a 20 20 20 20 70 43 75 72 2d fo); \. pCur-
16a8d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 20 >validNKey = 1;
16a8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a91 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b \. }else{
16a92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a96 20 20 20 20 20 20 20 5c 0a 20 20 20 20 61 73 73 \. ass
16a97 65 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 ertCellInfo(pCur
16a98 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
16a99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a9b 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 \. }.#e
16a9c 6e 64 69 66 20 2f 2a 20 5f 4d 53 43 5f 56 45 52 ndif /* _MSC_VER
16a9d 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 */..#ifndef NDE
16a9e 42 55 47 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 BUG /* The next
16a9f 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 6f 6e routine used on
16aa0 6c 79 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 ly within assert
16aa1 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f () statements */
16aa2 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 ./*.** Return tr
16aa3 75 65 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 ue if the given
16aa4 42 74 43 75 72 73 6f 72 20 69 73 20 76 61 6c 69 BtCursor is vali
16aa5 64 2e 20 20 41 20 76 61 6c 69 64 20 63 75 72 73 d. A valid curs
16aa6 6f 72 20 69 73 20 6f 6e 65 0a 2a 2a 20 74 68 61 or is one.** tha
16aa7 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 t is currently p
16aa8 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f 77 ointing to a row
16aa9 20 69 6e 20 61 20 28 6e 6f 6e 2d 65 6d 70 74 79 in a (non-empty
16aaa 29 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 69 73 ) table..** This
16aab 20 69 73 20 61 20 76 65 72 69 66 69 63 61 74 69 is a verificati
16aac 6f 6e 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 on routine is us
16aad 65 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61 ed only within a
16aae 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e ssert() statemen
16aaf 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ts..*/.SQLITE_PR
16ab0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
16ab1 33 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 3BtreeCursorIsVa
16ab2 6c 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 lid(BtCursor *pC
16ab3 75 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 ur){. return pC
16ab4 75 72 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 ur && pCur->eSta
16ab5 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
16ab6 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 ;.}.#endif /* ND
16ab7 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 EBUG */../*.** S
16ab8 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 et *pSize to the
16ab9 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66 size of the buf
16aba 66 65 72 20 6e 65 65 64 65 64 20 74 6f 20 68 6f fer needed to ho
16abb 6c 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a ld the value of.
16abc 2a 2a 20 74 68 65 20 6b 65 79 20 66 6f 72 20 74 ** the key for t
16abd 68 65 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 he current entry
16abe 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 . If the cursor
16abf 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 is not pointing
16ac0 0a 2a 2a 20 74 6f 20 61 20 76 61 6c 69 64 20 65 .** to a valid e
16ac1 6e 74 72 79 2c 20 2a 70 53 69 7a 65 20 69 73 20 ntry, *pSize is
16ac2 73 65 74 20 74 6f 20 30 2e 20 0a 2a 2a 0a 2a 2a set to 0. .**.**
16ac3 20 46 6f 72 20 61 20 74 61 62 6c 65 20 77 69 74 For a table wit
16ac4 68 20 74 68 65 20 49 4e 54 4b 45 59 20 66 6c 61 h the INTKEY fla
16ac5 67 20 73 65 74 2c 20 74 68 69 73 20 72 6f 75 74 g set, this rout
16ac6 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 ine returns the
16ac7 6b 65 79 0a 2a 2a 20 69 74 73 65 6c 66 2c 20 6e key.** itself, n
16ac8 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ot the number of
16ac9 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6b 65 bytes in the ke
16aca 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c y..**.** The cal
16acb 6c 65 72 20 6d 75 73 74 20 70 6f 73 69 74 69 6f ler must positio
16acc 6e 20 74 68 65 20 63 75 72 73 6f 72 20 70 72 69 n the cursor pri
16acd 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 or to invoking t
16ace 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20 his routine..**
16acf 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
16ad0 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 20 49 cannot fail. I
16ad1 74 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 t always returns
16ad2 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 20 0a 2a 2f SQLITE_OK. .*/
16ad3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16ad4 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
16ad5 4b 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72 KeySize(BtCursor
16ad6 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 53 69 *pCur, i64 *pSi
16ad7 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 ze){. assert( c
16ad8 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
16ad9 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
16ada 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
16adb 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
16adc 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d || pCur->eState=
16add 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
16ade 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 . if( pCur->eSt
16adf 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate!=CURSOR_VALI
16ae0 44 20 29 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 D ){. *pSize
16ae1 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 0;. }else{.
16ae2 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 getCellInfo(pC
16ae3 75 72 29 3b 0a 20 20 20 20 2a 70 53 69 7a 65 20 ur);. *pSize
16ae4 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 = pCur->info.nKe
16ae5 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 y;. }. return
16ae6 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
16ae7 0a 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 .** Set *pSize t
16ae8 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 o the number of
16ae9 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 69 6e bytes of data in
16aea 20 74 68 65 20 65 6e 74 72 79 20 74 68 65 0a 2a the entry the.*
16aeb 2a 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74 * cursor current
16aec 6c 79 20 70 6f 69 6e 74 73 20 74 6f 2e 0a 2a 2a ly points to..**
16aed 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d .** The caller m
16aee 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74 68 ust guarantee th
16aef 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 at the cursor is
16af0 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 6e pointing to a n
16af1 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 69 64 on-NULL.** valid
16af2 20 65 6e 74 72 79 2e 20 20 49 6e 20 6f 74 68 65 entry. In othe
16af3 72 20 77 6f 72 64 73 2c 20 74 68 65 20 63 61 6c r words, the cal
16af4 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 6d ling procedure m
16af5 75 73 74 20 67 75 61 72 61 6e 74 65 65 0a 2a 2a ust guarantee.**
16af6 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 that the cursor
16af7 20 68 61 73 20 43 75 72 73 6f 72 2e 65 53 74 61 has Cursor.eSta
16af8 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
16af9 2e 0a 2a 2a 0a 2a 2a 20 46 61 69 6c 75 72 65 20 ..**.** Failure
16afa 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e is not possible.
16afb 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 This function
16afc 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 53 always returns S
16afd 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 74 20 QLITE_OK..** It
16afe 6d 69 67 68 74 20 6a 75 73 74 20 61 73 20 77 65 might just as we
16aff 6c 6c 20 62 65 20 61 20 70 72 6f 63 65 64 75 72 ll be a procedur
16b00 65 20 28 72 65 74 75 72 6e 69 6e 67 20 76 6f 69 e (returning voi
16b01 64 29 20 62 75 74 20 77 65 20 63 6f 6e 74 69 6e d) but we contin
16b02 75 65 0a 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20 ue.** to return
16b03 61 6e 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c an integer resul
16b04 74 20 63 6f 64 65 20 66 6f 72 20 68 69 73 74 6f t code for histo
16b05 72 69 63 61 6c 20 72 65 61 73 6f 6e 73 2e 0a 2a rical reasons..*
16b06 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16b07 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
16b08 65 44 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 eDataSize(BtCurs
16b09 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 or *pCur, u32 *p
16b0a 53 69 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 28 Size){. assert(
16b0b 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
16b0c 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 x(pCur) );. ass
16b0d 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 ert( pCur->eStat
16b0e 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e==CURSOR_VALID
16b0f 29 3b 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f );. getCellInfo
16b10 28 70 43 75 72 29 3b 0a 20 20 2a 70 53 69 7a 65 (pCur);. *pSize
16b11 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 = pCur->info.nD
16b12 61 74 61 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 ata;. return SQ
16b13 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
16b14 2a 20 47 69 76 65 6e 20 74 68 65 20 70 61 67 65 * Given the page
16b15 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e 20 6f 76 number of an ov
16b16 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 erflow page in t
16b17 68 65 20 64 61 74 61 62 61 73 65 20 28 70 61 72 he database (par
16b18 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 66 6c 29 2c ameter.** ovfl),
16b19 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 this function f
16b1a 69 6e 64 73 20 74 68 65 20 70 61 67 65 20 6e 75 inds the page nu
16b1b 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 mber of the next
16b1c 20 70 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a page in the .**
16b1d 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 linked list of
16b1e 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 overflow pages.
16b1f 49 66 20 70 6f 73 73 69 62 6c 65 2c 20 69 74 20 If possible, it
16b20 75 73 65 73 20 74 68 65 20 61 75 74 6f 2d 76 61 uses the auto-va
16b21 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72 2d cuum.** pointer-
16b22 6d 61 70 20 64 61 74 61 20 69 6e 73 74 65 61 64 map data instead
16b23 20 6f 66 20 72 65 61 64 69 6e 67 20 74 68 65 20 of reading the
16b24 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 20 content of page
16b25 6f 76 66 6c 20 74 6f 20 64 6f 20 73 6f 2e 20 0a ovfl to do so. .
16b26 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f **.** If an erro
16b27 72 20 6f 63 63 75 72 73 20 61 6e 20 53 51 4c 69 r occurs an SQLi
16b28 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 te error code is
16b29 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 returned. Other
16b2a 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 wise:.**.** The
16b2b 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 page number of t
16b2c 68 65 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 he next overflow
16b2d 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 6e page in the lin
16b2e 6b 65 64 20 6c 69 73 74 20 69 73 20 0a 2a 2a 20 ked list is .**
16b2f 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e written to *pPgn
16b30 6f 4e 65 78 74 2e 20 49 66 20 70 61 67 65 20 6f oNext. If page o
16b31 76 66 6c 20 69 73 20 74 68 65 20 6c 61 73 74 20 vfl is the last
16b32 70 61 67 65 20 69 6e 20 69 74 73 20 6c 69 6e 6b page in its link
16b33 65 64 20 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70 50 ed .** list, *pP
16b34 67 6e 6f 4e 65 78 74 20 69 73 20 73 65 74 20 74 gnoNext is set t
16b35 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 o zero. .**.** I
16b36 66 20 70 70 50 61 67 65 20 69 73 20 6e 6f 74 20 f ppPage is not
16b37 4e 55 4c 4c 2c 20 61 6e 64 20 61 20 72 65 66 65 NULL, and a refe
16b38 72 65 6e 63 65 20 74 6f 20 74 68 65 20 4d 65 6d rence to the Mem
16b39 50 61 67 65 20 6f 62 6a 65 63 74 20 63 6f 72 72 Page object corr
16b3a 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 esponding.** to
16b3b 70 61 67 65 20 6e 75 6d 62 65 72 20 70 4f 76 66 page number pOvf
16b3c 6c 20 77 61 73 20 6f 62 74 61 69 6e 65 64 2c 20 l was obtained,
16b3d 74 68 65 6e 20 2a 70 70 50 61 67 65 20 69 73 20 then *ppPage is
16b3e 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 set to point to
16b3f 74 68 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e 63 that.** referenc
16b40 65 2e 20 49 74 20 69 73 20 74 68 65 20 72 65 73 e. It is the res
16b41 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 ponsibility of t
16b42 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 63 61 6c he caller to cal
16b43 6c 20 72 65 6c 65 61 73 65 50 61 67 65 28 29 0a l releasePage().
16b44 2a 2a 20 6f 6e 20 2a 70 70 50 61 67 65 20 74 6f ** on *ppPage to
16b45 20 66 72 65 65 20 74 68 65 20 72 65 66 65 72 65 free the refere
16b46 6e 63 65 2e 20 49 6e 20 6e 6f 20 72 65 66 65 72 nce. In no refer
16b47 65 6e 63 65 20 77 61 73 20 6f 62 74 61 69 6e 65 ence was obtaine
16b48 64 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 d (because.** th
16b49 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 77 61 e pointer-map wa
16b4a 73 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e s used to obtain
16b4b 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20 2a the value for *
16b4c 70 50 67 6e 6f 4e 65 78 74 29 2c 20 74 68 65 6e pPgnoNext), then
16b4d 0a 2a 2a 20 2a 70 70 50 61 67 65 20 69 73 20 73 .** *ppPage is s
16b4e 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 et to zero..*/.s
16b4f 74 61 74 69 63 20 69 6e 74 20 67 65 74 4f 76 65 tatic int getOve
16b50 72 66 6c 6f 77 50 61 67 65 28 0a 20 20 42 74 53 rflowPage(. BtS
16b51 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 hared *pBt,
16b52 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
16b53 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
16b54 2f 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 /. Pgno ovfl,
16b55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b56 20 2f 2a 20 43 75 72 72 65 6e 74 20 6f 76 65 72 /* Current over
16b57 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 flow page number
16b58 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a */. MemPage **
16b59 70 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 ppPage,
16b5a 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 /* OUT: MemPa
16b5b 67 65 20 68 61 6e 64 6c 65 20 28 6d 61 79 20 62 ge handle (may b
16b5c 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e e NULL) */. Pgn
16b5d 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20 20 o *pPgnoNext
16b5e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
16b5f 3a 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 : Next overflow
16b60 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29 page number */.)
16b61 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20 {. Pgno next =
16b62 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 0;. MemPage *pP
16b63 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 age = 0;. int r
16b64 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a c = SQLITE_OK;..
16b65 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
16b66 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
16b67 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
16b68 73 65 72 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b sert(pPgnoNext);
16b69 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
16b6a 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
16b6b 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e . /* Try to fin
16b6c 64 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 d the next page
16b6d 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 in the overflow
16b6e 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20 list using the.
16b6f 20 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70 ** autovacuum p
16b70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 ointer-map pages
16b71 2e 20 47 75 65 73 73 20 74 68 61 74 20 74 68 65 . Guess that the
16b72 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20 next page in .
16b73 20 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 ** the overflow
16b74 20 6c 69 73 74 20 69 73 20 70 61 67 65 20 6e 75 list is page nu
16b75 6d 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 mber (ovfl+1). I
16b76 66 20 74 68 61 74 20 67 75 65 73 73 20 74 75 72 f that guess tur
16b77 6e 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 ns . ** out to
16b78 62 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 be wrong, fall b
16b79 61 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 ack to loading t
16b7a 68 65 20 64 61 74 61 20 6f 66 20 70 61 67 65 20 he data of page
16b7b 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 . ** number ovf
16b7c 6c 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 l to determine t
16b7d 68 65 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d he next page num
16b7e 62 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ber.. */. if(
16b7f 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
16b80 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f ){. Pgno pgno
16b81 3b 0a 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73 ;. Pgno iGues
16b82 73 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 s = ovfl+1;.
16b83 75 38 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 u8 eType;.. w
16b84 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 hile( PTRMAP_ISP
16b85 41 47 45 28 70 42 74 2c 20 69 47 75 65 73 73 29 AGE(pBt, iGuess)
16b86 20 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 || iGuess==PEND
16b87 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
16b88 74 29 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65 t) ){. iGue
16b89 73 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 ss++;. }..
16b8a 20 69 66 28 20 69 47 75 65 73 73 3c 3d 70 61 67 if( iGuess<=pag
16b8b 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 erPagecount(pBt)
16b8c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 ){. rc = p
16b8d 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 47 trmapGet(pBt, iG
16b8e 75 65 73 73 2c 20 26 65 54 79 70 65 2c 20 26 70 uess, &eType, &p
16b8f 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 gno);. if(
16b90 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
16b91 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f eType==PTRMAP_O
16b92 56 45 52 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f VERFLOW2 && pgno
16b93 3d 3d 6f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 ==ovfl ){.
16b94 20 20 6e 65 78 74 20 3d 20 69 47 75 65 73 73 3b next = iGuess;
16b95 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
16b96 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 LITE_DONE;.
16b97 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e }. }. }.#en
16b98 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 6e dif.. assert( n
16b99 65 78 74 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 ext==0 || rc==SQ
16b9a 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69 LITE_DONE );. i
16b9b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
16b9c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 ){. rc = btr
16b9d 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f eeGetPage(pBt, o
16b9e 76 66 6c 2c 20 26 70 50 61 67 65 2c 20 30 29 3b vfl, &pPage, 0);
16b9f 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d . assert( rc=
16ba0 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 =SQLITE_OK || pP
16ba1 61 67 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 age==0 );. if
16ba2 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
16ba3 29 7b 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20 ){. next =
16ba4 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e get4byte(pPage->
16ba5 61 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 aData);. }.
16ba6 7d 0a 0a 20 20 2a 70 50 67 6e 6f 4e 65 78 74 20 }.. *pPgnoNext
16ba7 3d 20 6e 65 78 74 3b 0a 20 20 69 66 28 20 70 70 = next;. if( pp
16ba8 50 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 70 50 Page ){. *ppP
16ba9 61 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d age = pPage;. }
16baa 65 6c 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 else{. releas
16bab 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
16bac 7d 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d }. return (rc==
16bad 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 SQLITE_DONE ? SQ
16bae 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a 7d LITE_OK : rc);.}
16baf 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 64 61 74 ../*.** Copy dat
16bb0 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 a from a buffer
16bb1 74 6f 20 61 20 70 61 67 65 2c 20 6f 72 20 66 72 to a page, or fr
16bb2 6f 6d 20 61 20 70 61 67 65 20 74 6f 20 61 20 62 om a page to a b
16bb3 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 uffer..**.** pPa
16bb4 79 6c 6f 61 64 20 69 73 20 61 20 70 6f 69 6e 74 yload is a point
16bb5 65 72 20 74 6f 20 64 61 74 61 20 73 74 6f 72 65 er to data store
16bb6 64 20 6f 6e 20 64 61 74 61 62 61 73 65 20 70 61 d on database pa
16bb7 67 65 20 70 44 62 50 61 67 65 2e 0a 2a 2a 20 49 ge pDbPage..** I
16bb8 66 20 61 72 67 75 6d 65 6e 74 20 65 4f 70 20 69 f argument eOp i
16bb9 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 6e 42 s false, then nB
16bba 79 74 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 yte bytes of dat
16bbb 61 20 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20 a are copied.**
16bbc 66 72 6f 6d 20 70 50 61 79 6c 6f 61 64 20 74 6f from pPayload to
16bbd 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e the buffer poin
16bbe 74 65 64 20 61 74 20 62 79 20 70 42 75 66 2e 20 ted at by pBuf.
16bbf 49 66 20 65 4f 70 20 69 73 20 74 72 75 65 2c 0a If eOp is true,.
16bc0 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 65 33 50 ** then sqlite3P
16bc1 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20 63 agerWrite() is c
16bc2 61 6c 6c 65 64 20 6f 6e 20 70 44 62 50 61 67 65 alled on pDbPage
16bc3 20 61 6e 64 20 6e 42 79 74 65 20 62 79 74 65 73 and nByte bytes
16bc4 0a 2a 2a 20 6f 66 20 64 61 74 61 20 61 72 65 20 .** of data are
16bc5 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 copied from the
16bc6 62 75 66 66 65 72 20 70 42 75 66 20 74 6f 20 70 buffer pBuf to p
16bc7 50 61 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a 2a 20 53 Payload..**.** S
16bc8 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
16bc9 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2c rned on success,
16bca 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65 72 otherwise an er
16bcb 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 ror code..*/.sta
16bcc 74 69 63 20 69 6e 74 20 63 6f 70 79 50 61 79 6c tic int copyPayl
16bcd 6f 61 64 28 0a 20 20 76 6f 69 64 20 2a 70 50 61 oad(. void *pPa
16bce 79 6c 6f 61 64 2c 20 20 20 20 20 20 20 20 20 20 yload,
16bcf 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 /* Pointer to p
16bd0 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f age data */. vo
16bd1 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 id *pBuf,
16bd2 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
16bd3 65 72 20 74 6f 20 62 75 66 66 65 72 20 2a 2f 0a er to buffer */.
16bd4 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 int nByte,
16bd5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
16bd6 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
16bd7 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20 o copy */. int
16bd8 65 4f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 eOp,
16bd9 20 20 20 20 20 20 2f 2a 20 30 20 2d 3e 20 63 6f /* 0 -> co
16bda 70 79 20 66 72 6f 6d 20 70 61 67 65 2c 20 31 20 py from page, 1
16bdb 2d 3e 20 63 6f 70 79 20 74 6f 20 70 61 67 65 20 -> copy to page
16bdc 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 */. DbPage *pDb
16bdd 50 61 67 65 20 20 20 20 20 20 20 20 20 20 20 2f Page /
16bde 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e * Page containin
16bdf 67 20 70 50 61 79 6c 6f 61 64 20 2a 2f 0a 29 7b g pPayload */.){
16be0 0a 20 20 69 66 28 20 65 4f 70 20 29 7b 0a 20 20 . if( eOp ){.
16be1 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66 /* Copy data f
16be2 72 6f 6d 20 62 75 66 66 65 72 20 74 6f 20 70 61 rom buffer to pa
16be3 67 65 20 28 61 20 77 72 69 74 65 20 6f 70 65 72 ge (a write oper
16be4 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 69 6e ation) */. in
16be5 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 t rc = sqlite3Pa
16be6 67 65 72 57 72 69 74 65 28 70 44 62 50 61 67 65 gerWrite(pDbPage
16be7 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
16be8 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
16be9 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
16bea 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 }. memcpy(pP
16beb 61 79 6c 6f 61 64 2c 20 70 42 75 66 2c 20 6e 42 ayload, pBuf, nB
16bec 79 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 yte);. }else{.
16bed 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 /* Copy data
16bee 66 72 6f 6d 20 70 61 67 65 20 74 6f 20 62 75 66 from page to buf
16bef 66 65 72 20 28 61 20 72 65 61 64 20 6f 70 65 72 fer (a read oper
16bf0 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 6d 65 ation) */. me
16bf1 6d 63 70 79 28 70 42 75 66 2c 20 70 50 61 79 6c mcpy(pBuf, pPayl
16bf2 6f 61 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d oad, nByte);. }
16bf3 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
16bf4 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 _OK;.}../*.** Th
16bf5 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 is function is u
16bf6 73 65 64 20 74 6f 20 72 65 61 64 20 6f 72 20 6f sed to read or o
16bf7 76 65 72 77 72 69 74 65 20 70 61 79 6c 6f 61 64 verwrite payload
16bf8 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 information.**
16bf9 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 for the entry th
16bfa 61 74 20 74 68 65 20 70 43 75 72 20 63 75 72 73 at the pCur curs
16bfb 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 or is pointing t
16bfc 6f 2e 20 49 66 20 74 68 65 20 65 4f 70 0a 2a 2a o. If the eOp.**
16bfd 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c parameter is 0,
16bfe 20 74 68 69 73 20 69 73 20 61 20 72 65 61 64 20 this is a read
16bff 6f 70 65 72 61 74 69 6f 6e 20 28 64 61 74 61 20 operation (data
16c00 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 62 copied into.** b
16c01 75 66 66 65 72 20 70 42 75 66 29 2e 20 49 66 20 uffer pBuf). If
16c02 69 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 it is non-zero,
16c03 61 20 77 72 69 74 65 20 28 64 61 74 61 20 63 6f a write (data co
16c04 70 69 65 64 20 66 72 6f 6d 0a 2a 2a 20 62 75 66 pied from.** buf
16c05 66 65 72 20 70 42 75 66 29 2e 0a 2a 2a 0a 2a 2a fer pBuf)..**.**
16c06 20 41 20 74 6f 74 61 6c 20 6f 66 20 22 61 6d 74 A total of "amt
16c07 22 20 62 79 74 65 73 20 61 72 65 20 72 65 61 64 " bytes are read
16c08 20 6f 72 20 77 72 69 74 74 65 6e 20 62 65 67 69 or written begi
16c09 6e 6e 69 6e 67 20 61 74 20 22 6f 66 66 73 65 74 nning at "offset
16c0a 22 2e 0a 2a 2a 20 44 61 74 61 20 69 73 20 72 65 "..** Data is re
16c0b 61 64 20 74 6f 20 6f 72 20 66 72 6f 6d 20 74 68 ad to or from th
16c0c 65 20 62 75 66 66 65 72 20 70 42 75 66 2e 0a 2a e buffer pBuf..*
16c0d 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 *.** The content
16c0e 20 62 65 69 6e 67 20 72 65 61 64 20 6f 72 20 77 being read or w
16c0f 72 69 74 74 65 6e 20 6d 69 67 68 74 20 61 70 70 ritten might app
16c10 65 61 72 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 ear on the main
16c11 70 61 67 65 0a 2a 2a 20 6f 72 20 62 65 20 73 63 page.** or be sc
16c12 61 74 74 65 72 65 64 20 6f 75 74 20 6f 6e 20 6d attered out on m
16c13 75 6c 74 69 70 6c 65 20 6f 76 65 72 66 6c 6f 77 ultiple overflow
16c14 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 pages..**.** If
16c15 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69 73 the BtCursor.is
16c16 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 66 IncrblobHandle f
16c17 6c 61 67 20 69 73 20 73 65 74 2c 20 61 6e 64 20 lag is set, and
16c18 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63 the current.** c
16c19 75 72 73 6f 72 20 65 6e 74 72 79 20 75 73 65 73 ursor entry uses
16c1a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65 one or more ove
16c1b 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 69 rflow pages, thi
16c1c 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 6c s function.** al
16c1d 6c 6f 63 61 74 65 73 20 73 70 61 63 65 20 66 6f locates space fo
16c1e 72 20 61 6e 64 20 6c 61 7a 69 6c 79 20 70 6f 70 r and lazily pop
16c1f 6c 75 61 74 65 73 20 74 68 65 20 6f 76 65 72 66 luates the overf
16c20 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 0a 2a low page-list .*
16c21 2a 20 63 61 63 68 65 20 61 72 72 61 79 20 28 42 * cache array (B
16c22 74 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f tCursor.aOverflo
16c23 77 29 2e 20 53 75 62 73 65 71 75 65 6e 74 20 63 w). Subsequent c
16c24 61 6c 6c 73 20 75 73 65 20 74 68 69 73 0a 2a 2a alls use this.**
16c25 20 63 61 63 68 65 20 74 6f 20 6d 61 6b 65 20 73 cache to make s
16c26 65 65 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 75 eeking to the su
16c27 70 70 6c 69 65 64 20 6f 66 66 73 65 74 20 6d 6f pplied offset mo
16c28 72 65 20 65 66 66 69 63 69 65 6e 74 2e 0a 2a 2a re efficient..**
16c29 0a 2a 2a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 .** Once an over
16c2a 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 flow page-list c
16c2b 61 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c ache has been al
16c2c 6c 6f 63 61 74 65 64 2c 20 69 74 20 6d 61 79 20 located, it may
16c2d 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 be.** invalidate
16c2e 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 d if some other
16c2f 63 75 72 73 6f 72 20 77 72 69 74 65 73 20 74 6f cursor writes to
16c30 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c the same table,
16c31 20 6f 72 20 69 66 0a 2a 2a 20 74 68 65 20 63 75 or if.** the cu
16c32 72 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 6f rsor is moved to
16c33 20 61 20 64 69 66 66 65 72 65 6e 74 20 72 6f 77 a different row
16c34 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 . Additionally,
16c35 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a in auto-vacuum.*
16c36 2a 20 6d 6f 64 65 2c 20 74 68 65 20 66 6f 6c 6c * mode, the foll
16c37 6f 77 69 6e 67 20 65 76 65 6e 74 73 20 6d 61 79 owing events may
16c38 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 20 6f invalidate an o
16c39 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 verflow page-lis
16c3a 74 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 20 t cache..**.**
16c3b 20 2a 20 41 6e 20 69 6e 63 72 65 6d 65 6e 74 61 * An incrementa
16c3c 6c 20 76 61 63 75 75 6d 2c 0a 2a 2a 20 20 20 2a l vacuum,.** *
16c3d 20 41 20 63 6f 6d 6d 69 74 20 69 6e 20 61 75 74 A commit in aut
16c3e 6f 5f 76 61 63 75 75 6d 3d 22 66 75 6c 6c 22 20 o_vacuum="full"
16c3f 6d 6f 64 65 2c 0a 2a 2a 20 20 20 2a 20 43 72 65 mode,.** * Cre
16c40 61 74 69 6e 67 20 61 20 74 61 62 6c 65 20 28 6d ating a table (m
16c41 61 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e ay require movin
16c42 67 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 g an overflow pa
16c43 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ge)..*/.static i
16c44 6e 74 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 nt accessPayload
16c45 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 (. BtCursor *pC
16c46 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 73 ur, /* Curs
16c47 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65 or pointing to e
16c48 6e 74 72 79 20 74 6f 20 72 65 61 64 20 66 72 6f ntry to read fro
16c49 6d 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 65 m */. u32 offse
16c4a 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 t, /* B
16c4b 65 67 69 6e 20 72 65 61 64 69 6e 67 20 74 68 69 egin reading thi
16c4c 73 20 66 61 72 20 69 6e 74 6f 20 70 61 79 6c 6f s far into paylo
16c4d 61 64 20 2a 2f 0a 20 20 75 33 32 20 61 6d 74 2c ad */. u32 amt,
16c4e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16c4f 52 65 61 64 20 74 68 69 73 20 6d 61 6e 79 20 62 Read this many b
16c50 79 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e ytes */. unsign
16c51 65 64 20 63 68 61 72 20 2a 70 42 75 66 2c 20 2f ed char *pBuf, /
16c52 2a 20 57 72 69 74 65 20 74 68 65 20 62 79 74 65 * Write the byte
16c53 73 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 s into this buff
16c54 65 72 20 2a 2f 20 0a 20 20 69 6e 74 20 65 4f 70 er */ . int eOp
16c55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16c56 20 7a 65 72 6f 20 74 6f 20 72 65 61 64 2e 20 6e zero to read. n
16c57 6f 6e 2d 7a 65 72 6f 20 74 6f 20 77 72 69 74 65 on-zero to write
16c58 2e 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e . */.){. unsign
16c59 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 ed char *aPayloa
16c5a 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 d;. int rc = SQ
16c5b 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 33 32 20 6e LITE_OK;. u32 n
16c5c 4b 65 79 3b 0a 20 20 69 6e 74 20 69 49 64 78 20 Key;. int iIdx
16c5d 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a = 0;. MemPage *
16c5e 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
16c5f 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
16c60 5d 3b 20 2f 2a 20 42 74 72 65 65 20 70 61 67 65 ]; /* Btree page
16c61 20 6f 66 20 63 75 72 72 65 6e 74 20 65 6e 74 72 of current entr
16c62 79 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 y */. BtShared
16c63 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 *pBt = pCur->pBt
16c64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
16c65 20 20 20 2f 2a 20 42 74 72 65 65 20 74 68 69 73 /* Btree this
16c66 20 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 cursor belongs
16c67 74 6f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 to */.. assert(
16c68 20 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 pPage );. asse
16c69 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
16c6a 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
16c6b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
16c6c 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
16c6d 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c age]<pPage->nCel
16c6e 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 l );. assert( c
16c6f 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
16c70 70 43 75 72 29 20 29 3b 0a 0a 20 20 67 65 74 43 pCur) );.. getC
16c71 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 ellInfo(pCur);.
16c72 20 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 aPayload = pCur
16c73 2d 3e 69 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20 70 ->info.pCell + p
16c74 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 Cur->info.nHeade
16c75 72 3b 0a 20 20 6e 4b 65 79 20 3d 20 28 70 50 61 r;. nKey = (pPa
16c76 67 65 2d 3e 69 6e 74 4b 65 79 20 3f 20 30 20 3a ge->intKey ? 0 :
16c77 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f (int)pCur->info
16c78 2e 6e 4b 65 79 29 3b 0a 0a 20 20 69 66 28 20 4e .nKey);.. if( N
16c79 45 56 45 52 28 6f 66 66 73 65 74 2b 61 6d 74 20 EVER(offset+amt
16c7a 3e 20 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66 > nKey+pCur->inf
16c7b 6f 2e 6e 44 61 74 61 29 20 0a 20 20 20 7c 7c 20 o.nData) . ||
16c7c 26 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e &aPayload[pCur->
16c7d 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26 info.nLocal] > &
16c7e 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 pPage->aData[pBt
16c7f 2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d 0a 20 20 ->usableSize].
16c80 29 7b 0a 20 20 20 20 2f 2a 20 54 72 79 69 6e 67 ){. /* Trying
16c81 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 to read or writ
16c82 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f e past the end o
16c83 66 20 74 68 65 20 64 61 74 61 20 69 73 20 61 6e f the data is an
16c84 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 72 65 error */. re
16c85 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
16c86 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 UPT_BKPT;. }..
16c87 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 64 61 74 /* Check if dat
16c88 61 20 6d 75 73 74 20 62 65 20 72 65 61 64 2f 77 a must be read/w
16c89 72 69 74 74 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 ritten to/from t
16c8a 68 65 20 62 74 72 65 65 20 70 61 67 65 20 69 74 he btree page it
16c8b 73 65 6c 66 2e 20 2a 2f 0a 20 20 69 66 28 20 6f self. */. if( o
16c8c 66 66 73 65 74 3c 70 43 75 72 2d 3e 69 6e 66 6f ffset<pCur->info
16c8d 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 .nLocal ){. i
16c8e 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 nt a = amt;.
16c8f 69 66 28 20 61 2b 6f 66 66 73 65 74 3e 70 43 75 if( a+offset>pCu
16c90 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 r->info.nLocal )
16c91 7b 0a 20 20 20 20 20 20 61 20 3d 20 70 43 75 72 {. a = pCur
16c92 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 ->info.nLocal -
16c93 6f 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20 offset;. }.
16c94 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f rc = copyPaylo
16c95 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 ad(&aPayload[off
16c96 73 65 74 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65 set], pBuf, a, e
16c97 4f 70 2c 20 70 50 61 67 65 2d 3e 70 44 62 50 61 Op, pPage->pDbPa
16c98 67 65 29 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 ge);. offset
16c99 3d 20 30 3b 0a 20 20 20 20 70 42 75 66 20 2b 3d = 0;. pBuf +=
16c9a 20 61 3b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61 a;. amt -= a
16c9b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f ;. }else{. o
16c9c 66 66 73 65 74 20 2d 3d 20 70 43 75 72 2d 3e 69 ffset -= pCur->i
16c9d 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a nfo.nLocal;. }.
16c9e 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
16c9f 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b E_OK && amt>0 ){
16ca0 0a 20 20 20 20 63 6f 6e 73 74 20 75 33 32 20 6f . const u32 o
16ca1 76 66 6c 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 vflSize = pBt->u
16ca2 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 20 20 sableSize - 4;
16ca3 2f 2a 20 42 79 74 65 73 20 63 6f 6e 74 65 6e 74 /* Bytes content
16ca4 20 70 65 72 20 6f 76 66 6c 20 70 61 67 65 20 2a per ovfl page *
16ca5 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 50 /. Pgno nextP
16ca6 61 67 65 3b 0a 0a 20 20 20 20 6e 65 78 74 50 61 age;.. nextPa
16ca7 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 ge = get4byte(&a
16ca8 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e Payload[pCur->in
16ca9 66 6f 2e 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 69 fo.nLocal]);..#i
16caa 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
16cab 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 2f T_INCRBLOB. /
16cac 2a 20 49 66 20 74 68 65 20 69 73 49 6e 63 72 62 * If the isIncrb
16cad 6c 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 lobHandle flag i
16cae 73 20 73 65 74 20 61 6e 64 20 74 68 65 20 42 74 s set and the Bt
16caf 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 Cursor.aOverflow
16cb0 5b 5d 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 6f []. ** has no
16cb1 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 t been allocated
16cb2 2c 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f , allocate it no
16cb3 77 2e 20 54 68 65 20 61 72 72 61 79 20 69 73 20 w. The array is
16cb4 73 69 7a 65 64 20 61 74 0a 20 20 20 20 2a 2a 20 sized at. **
16cb5 6f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 one entry for ea
16cb6 63 68 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 ch overflow page
16cb7 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 in the overflow
16cb8 20 63 68 61 69 6e 2e 20 54 68 65 0a 20 20 20 20 chain. The.
16cb9 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f ** page number o
16cba 66 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 f the first over
16cbb 66 6c 6f 77 20 70 61 67 65 20 69 73 20 73 74 6f flow page is sto
16cbc 72 65 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77 red in aOverflow
16cbd 5b 30 5d 2c 0a 20 20 20 20 2a 2a 20 65 74 63 2e [0],. ** etc.
16cbe 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 69 6e A value of 0 in
16cbf 20 74 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d the aOverflow[]
16cc0 20 61 72 72 61 79 20 6d 65 61 6e 73 20 22 6e 6f array means "no
16cc1 74 20 79 65 74 20 6b 6e 6f 77 6e 22 0a 20 20 20 t yet known".
16cc2 20 2a 2a 20 28 74 68 65 20 63 61 63 68 65 20 69 ** (the cache i
16cc3 73 20 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74 s lazily populat
16cc4 65 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ed).. */.
16cc5 69 66 28 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 if( pCur->isIncr
16cc6 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 21 70 blobHandle && !p
16cc7 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 Cur->aOverflow )
16cc8 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4f 76 66 {. int nOvf
16cc9 6c 20 3d 20 28 70 43 75 72 2d 3e 69 6e 66 6f 2e l = (pCur->info.
16cca 6e 50 61 79 6c 6f 61 64 2d 70 43 75 72 2d 3e 69 nPayload-pCur->i
16ccb 6e 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53 nfo.nLocal+ovflS
16ccc 69 7a 65 2d 31 29 2f 6f 76 66 6c 53 69 7a 65 3b ize-1)/ovflSize;
16ccd 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 . pCur->aOv
16cce 65 72 66 6c 6f 77 20 3d 20 28 50 67 6e 6f 20 2a erflow = (Pgno *
16ccf 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 )sqlite3MallocZe
16cd0 72 6f 28 73 69 7a 65 6f 66 28 50 67 6e 6f 29 2a ro(sizeof(Pgno)*
16cd1 6e 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 2f 2a nOvfl);. /*
16cd2 20 6e 4f 76 66 6c 20 69 73 20 61 6c 77 61 79 73 nOvfl is always
16cd3 20 70 6f 73 69 74 69 76 65 2e 20 20 49 66 20 69 positive. If i
16cd4 74 20 77 65 72 65 20 7a 65 72 6f 2c 20 66 65 74 t were zero, fet
16cd5 63 68 50 61 79 6c 6f 61 64 20 77 6f 75 6c 64 20 chPayload would
16cd6 68 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 have. ** be
16cd7 65 6e 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 en used instead
16cd8 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e of this routine.
16cd9 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c */. if( AL
16cda 57 41 59 53 28 6e 4f 76 66 6c 29 20 26 26 20 21 WAYS(nOvfl) && !
16cdb 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 pCur->aOverflow
16cdc 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
16cdd 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
16cde 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
16cdf 20 2f 2a 20 49 66 20 74 68 65 20 6f 76 65 72 66 /* If the overf
16ce0 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 low page-list ca
16ce1 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c che has been all
16ce2 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 0a 20 ocated and the.
16ce3 20 20 20 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 ** entry for
16ce4 74 68 65 20 66 69 72 73 74 20 72 65 71 75 69 72 the first requir
16ce5 65 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 ed overflow page
16ce6 20 69 73 20 76 61 6c 69 64 2c 20 73 6b 69 70 0a is valid, skip.
16ce7 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 ** directly
16ce8 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 to it.. */.
16ce9 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 if( pCur->aOve
16cea 72 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 rflow && pCur->a
16ceb 4f 76 65 72 66 6c 6f 77 5b 6f 66 66 73 65 74 2f Overflow[offset/
16cec 6f 76 66 6c 53 69 7a 65 5d 20 29 7b 0a 20 20 20 ovflSize] ){.
16ced 20 20 20 69 49 64 78 20 3d 20 28 6f 66 66 73 65 iIdx = (offse
16cee 74 2f 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 t/ovflSize);.
16cef 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43 nextPage = pC
16cf0 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 ur->aOverflow[iI
16cf1 64 78 5d 3b 0a 20 20 20 20 20 20 6f 66 66 73 65 dx];. offse
16cf2 74 20 3d 20 28 6f 66 66 73 65 74 25 6f 76 66 6c t = (offset%ovfl
16cf3 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e Size);. }.#en
16cf4 64 69 66 0a 0a 20 20 20 20 66 6f 72 28 20 3b 20 dif.. for( ;
16cf5 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
16cf6 20 61 6d 74 3e 30 20 26 26 20 6e 65 78 74 50 61 amt>0 && nextPa
16cf7 67 65 3b 20 69 49 64 78 2b 2b 29 7b 0a 0a 23 69 ge; iIdx++){..#i
16cf8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
16cf9 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 T_INCRBLOB.
16cfa 20 2f 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c /* If required,
16cfb 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 76 populate the ov
16cfc 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 erflow page-list
16cfd 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20 20 cache. */.
16cfe 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 if( pCur->aOver
16cff 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 flow ){.
16d00 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61 4f assert(!pCur->aO
16d01 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 7c 7c verflow[iIdx] ||
16d02 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 pCur->aOverflow
16d03 5b 69 49 64 78 5d 3d 3d 6e 65 78 74 50 61 67 65 [iIdx]==nextPage
16d04 29 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d );. pCur-
16d05 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d >aOverflow[iIdx]
16d06 20 3d 20 6e 65 78 74 50 61 67 65 3b 0a 20 20 20 = nextPage;.
16d07 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
16d08 20 20 20 69 66 28 20 6f 66 66 73 65 74 3e 3d 6f if( offset>=o
16d09 76 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 vflSize ){.
16d0a 20 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 72 /* The only r
16d0b 65 61 73 6f 6e 20 74 6f 20 72 65 61 64 20 74 68 eason to read th
16d0c 69 73 20 70 61 67 65 20 69 73 20 74 6f 20 6f 62 is page is to ob
16d0d 74 61 69 6e 20 74 68 65 20 70 61 67 65 0a 20 20 tain the page.
16d0e 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 ** number
16d0f 66 6f 72 20 74 68 65 20 6e 65 78 74 20 70 61 67 for the next pag
16d10 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f e in the overflo
16d11 77 20 63 68 61 69 6e 2e 20 54 68 65 20 70 61 67 w chain. The pag
16d12 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74 e. ** dat
16d13 61 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 a is not require
16d14 64 2e 20 53 6f 20 66 69 72 73 74 20 74 72 79 20 d. So first try
16d15 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 6f 76 to lookup the ov
16d16 65 72 66 6c 6f 77 0a 20 20 20 20 20 20 20 20 2a erflow. *
16d17 2a 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 * page-list cach
16d18 65 2c 20 69 66 20 61 6e 79 2c 20 74 68 65 6e 20 e, if any, then
16d19 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 74 68 65 fall back to the
16d1a 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 getOverflowPage
16d1b 28 29 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75 (). ** fu
16d1c 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 nction..
16d1d 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
16d1e 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a E_OMIT_INCRBLOB.
16d1f 20 20 20 20 20 20 20 20 69 66 28 20 70 43 75 72 if( pCur
16d20 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 ->aOverflow && p
16d21 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 Cur->aOverflow[i
16d22 49 64 78 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20 Idx+1] ){.
16d23 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70 nextPage = p
16d24 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 Cur->aOverflow[i
16d25 49 64 78 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 Idx+1];.
16d26 7d 20 65 6c 73 65 20 0a 23 65 6e 64 69 66 0a 20 } else .#endif.
16d27 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 67 65 rc = ge
16d28 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 tOverflowPage(pB
16d29 74 2c 20 6e 65 78 74 50 61 67 65 2c 20 30 2c 20 t, nextPage, 0,
16d2a 26 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20 &nextPage);.
16d2b 20 20 20 20 6f 66 66 73 65 74 20 2d 3d 20 6f 76 offset -= ov
16d2c 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 7d 65 flSize;. }e
16d2d 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 lse{. /*
16d2e 4e 65 65 64 20 74 6f 20 72 65 61 64 20 74 68 69 Need to read thi
16d2f 73 20 70 61 67 65 20 70 72 6f 70 65 72 6c 79 2e s page properly.
16d30 20 49 74 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d It contains som
16d31 65 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 e of the.
16d32 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 74 ** range of dat
16d33 61 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 a that is being
16d34 72 65 61 64 20 28 65 4f 70 3d 3d 30 29 20 6f 72 read (eOp==0) or
16d35 20 77 72 69 74 74 65 6e 20 28 65 4f 70 21 3d 30 written (eOp!=0
16d36 29 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 ).. */.
16d37 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 DbPage *pD
16d38 62 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 69 bPage;. i
16d39 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 nt a = amt;.
16d3a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
16d3b 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 PagerGet(pBt->pP
16d3c 61 67 65 72 2c 20 6e 65 78 74 50 61 67 65 2c 20 ager, nextPage,
16d3d 26 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 &pDbPage);.
16d3e 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
16d3f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
16d40 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 73 71 6c aPayload = sql
16d41 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 ite3PagerGetData
16d42 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 (pDbPage);.
16d43 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 nextPage =
16d44 67 65 74 34 62 79 74 65 28 61 50 61 79 6c 6f 61 get4byte(aPayloa
16d45 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 d);. if
16d46 28 20 61 20 2b 20 6f 66 66 73 65 74 20 3e 20 6f ( a + offset > o
16d47 76 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 vflSize ){.
16d48 20 20 20 20 20 20 20 61 20 3d 20 6f 76 66 6c 53 a = ovflS
16d49 69 7a 65 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20 ize - offset;.
16d4a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
16d4b 20 20 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 rc = copyPay
16d4c 6c 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f load(&aPayload[o
16d4d 66 66 73 65 74 2b 34 5d 2c 20 70 42 75 66 2c 20 ffset+4], pBuf,
16d4e 61 2c 20 65 4f 70 2c 20 70 44 62 50 61 67 65 29 a, eOp, pDbPage)
16d4f 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
16d50 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 te3PagerUnref(pD
16d51 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
16d52 20 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 offset = 0;.
16d53 20 20 20 20 20 20 20 20 61 6d 74 20 2d 3d 20 61 amt -= a
16d54 3b 0a 20 20 20 20 20 20 20 20 20 20 70 42 75 66 ;. pBuf
16d55 20 2b 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 7d += a;. }
16d56 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
16d57 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 }.. if( rc==SQ
16d58 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 LITE_OK && amt>0
16d59 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
16d5a 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
16d5b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e PT;. }. return
16d5c 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
16d5d 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 6b ad part of the k
16d5e 65 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ey associated wi
16d5f 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 th cursor pCur.
16d60 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 Exactly.** "amt
16d61 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 " bytes will be
16d62 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 transfered into
16d63 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 pBuf[]. The tra
16d64 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 nsfer.** begins
16d65 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a at "offset"..**.
16d66 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 ** The caller mu
16d67 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 70 st ensure that p
16d68 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 Cur is pointing
16d69 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 0a 2a to a valid row.*
16d6a 2a 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a * in the table..
16d6b 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c **.** Return SQL
16d6c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 ITE_OK on succes
16d6d 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f s or an error co
16d6e 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 de if anything g
16d6f 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 oes.** wrong. A
16d70 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 n error is retur
16d71 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 ned if "offset+a
16d72 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 mt" is larger th
16d73 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 an.** the availa
16d74 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a ble payload..*/.
16d75 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
16d76 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b nt sqlite3BtreeK
16d77 65 79 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 ey(BtCursor *pCu
16d78 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 r, u32 offset, u
16d79 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 32 amt, void *pB
16d7a 75 66 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 uf){. assert( c
16d7b 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
16d7c 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
16d7d 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
16d7e 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
16d7f 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
16d80 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 >iPage>=0 && pCu
16d81 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
16d82 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 iPage] );. asse
16d83 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b rt( pCur->aiIdx[
16d84 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 pCur->iPage]<pCu
16d85 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
16d86 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b iPage]->nCell );
16d87 0a 20 20 72 65 74 75 72 6e 20 61 63 63 65 73 73 . return access
16d88 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 Payload(pCur, of
16d89 66 73 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 fset, amt, (unsi
16d8a 67 6e 65 64 20 63 68 61 72 2a 29 70 42 75 66 2c gned char*)pBuf,
16d8b 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 0);.}../*.** Re
16d8c 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 ad part of the d
16d8d 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 ata associated w
16d8e 69 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e ith cursor pCur.
16d8f 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d Exactly.** "am
16d90 74 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 t" bytes will be
16d91 20 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f transfered into
16d92 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 pBuf[]. The tr
16d93 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 ansfer.** begins
16d94 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a at "offset"..**
16d95 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
16d96 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 E_OK on success
16d97 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 or an error code
16d98 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 if anything goe
16d99 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 s.** wrong. An
16d9a 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 error is returne
16d9b 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 d if "offset+amt
16d9c 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e " is larger than
16d9d 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c .** the availabl
16d9e 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 e payload..*/.SQ
16d9f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
16da0 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 sqlite3BtreeDat
16da1 61 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 a(BtCursor *pCur
16da2 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 , u32 offset, u3
16da3 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 2 amt, void *pBu
16da4 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23 f){. int rc;..#
16da5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
16da6 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 IT_INCRBLOB. if
16da7 20 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d ( pCur->eState=
16da8 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
16da9 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
16daa 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a LITE_ABORT;. }.
16dab 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 #endif.. assert
16dac 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
16dad 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 ex(pCur) );. rc
16dae 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 = restoreCursor
16daf 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a Position(pCur);.
16db0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
16db1 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 _OK ){. asser
16db2 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
16db3 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
16db4 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 . assert( pCu
16db5 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 r->iPage>=0 && p
16db6 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
16db7 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 ->iPage] );.
16db8 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 assert( pCur->ai
16db9 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
16dba 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 <pCur->apPage[pC
16dbb 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c ur->iPage]->nCel
16dbc 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 63 l );. rc = ac
16dbd 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 cessPayload(pCur
16dbe 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 , offset, amt, p
16dbf 42 75 66 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 Buf, 0);. }. r
16dc0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
16dc1 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
16dc2 74 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20 69 ter to payload i
16dc3 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 nformation from
16dc4 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 the entry that t
16dc5 68 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72 73 he .** pCur curs
16dc6 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 or is pointing t
16dc7 6f 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20 o. The pointer
16dc8 69 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e is to the beginn
16dc9 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 ing of.** the ke
16dca 79 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20 y if skipKey==0
16dcb 61 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74 6f and it points to
16dcc 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
16dcd 66 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69 f data if.** ski
16dce 70 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75 pKey==1. The nu
16dcf 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
16dd0 20 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64 available key/d
16dd1 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a 2a ata is written.*
16dd2 2a 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49 * into *pAmt. I
16dd3 66 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e f *pAmt==0, then
16dd4 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 the value retur
16dd5 6e 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a ned will not be.
16dd6 2a 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 ** a valid point
16dd7 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 er..**.** This r
16dd8 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70 74 outine is an opt
16dd9 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69 imization. It i
16dda 73 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65 s common for the
16ddb 20 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61 entire key.** a
16ddc 6e 64 20 64 61 74 61 20 74 6f 20 66 69 74 20 6f nd data to fit o
16ddd 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 n the local page
16dde 20 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20 74 and for there t
16ddf 6f 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 o be no overflow
16de0 0a 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65 6e .** pages. When
16de1 20 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68 69 that is so, thi
16de2 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 s routine can be
16de3 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 used to access
16de4 74 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 the.** key and d
16de5 61 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b 69 ata without maki
16de6 6e 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20 74 ng a copy. If t
16de7 68 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61 he key and/or da
16de8 74 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 ta spills.** ont
16de9 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 o overflow pages
16dea 2c 20 74 68 65 6e 20 61 63 63 65 73 73 50 61 79 , then accessPay
16deb 6c 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20 75 load() must be u
16dec 73 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62 6c sed to reassembl
16ded 65 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 74 e.** the key/dat
16dee 61 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69 6e a and copy it in
16def 74 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 to a preallocate
16df0 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 d buffer..**.**
16df1 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 The pointer retu
16df2 72 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 rned by this rou
16df3 74 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63 tine looks direc
16df4 74 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61 63 tly into the cac
16df5 68 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74 hed.** page of t
16df6 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 he database. Th
16df7 65 20 64 61 74 61 20 6d 69 67 68 74 20 63 68 61 e data might cha
16df8 6e 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65 20 nge or move the
16df9 6e 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79 next time.** any
16dfa 20 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20 69 btree routine i
16dfb 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 s called..*/.sta
16dfc 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e tic const unsign
16dfd 65 64 20 63 68 61 72 20 2a 66 65 74 63 68 50 61 ed char *fetchPa
16dfe 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f yload(. BtCurso
16dff 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a r *pCur, /*
16e00 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 Cursor pointing
16e01 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 to entry to rea
16e02 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 d from */. int
16e03 2a 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 *pAmt,
16e04 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 /* Write the nu
16e05 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c mber of availabl
16e06 65 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f 0a e bytes here */.
16e07 20 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20 20 int skipKey
16e08 20 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20 62 /* read b
16e09 65 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74 61 eginning at data
16e0a 20 69 66 20 74 68 69 73 20 69 73 20 74 72 75 65 if this is true
16e0b 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 */.){. unsigne
16e0c 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 d char *aPayload
16e0d 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
16e0e 67 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a ge;. u32 nKey;.
16e0f 20 20 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20 u32 nLocal;..
16e10 20 61 73 73 65 72 74 28 20 70 43 75 72 21 3d 30 assert( pCur!=0
16e11 20 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 3e && pCur->iPage>
16e12 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 =0 && pCur->apPa
16e13 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 ge[pCur->iPage])
16e14 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
16e15 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
16e16 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 _VALID );. asse
16e17 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
16e18 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
16e19 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
16e1a 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
16e1b 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 ];. assert( pCu
16e1c 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
16e1d 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 Page]<pPage->nCe
16e1e 6c 6c 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 ll );. if( NEVE
16e1f 52 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 R(pCur->info.nSi
16e20 7a 65 3d 3d 30 29 20 29 7b 0a 20 20 20 20 62 74 ze==0) ){. bt
16e21 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75 reeParseCell(pCu
16e22 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
16e23 69 50 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 iPage], pCur->ai
16e24 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
16e25 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
16e26 20 20 20 20 20 26 70 43 75 72 2d 3e 69 6e 66 6f &pCur->info
16e27 29 3b 0a 20 20 7d 0a 20 20 61 50 61 79 6c 6f 61 );. }. aPayloa
16e28 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 d = pCur->info.p
16e29 43 65 6c 6c 3b 0a 20 20 61 50 61 79 6c 6f 61 64 Cell;. aPayload
16e2a 20 2b 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e += pCur->info.n
16e2b 48 65 61 64 65 72 3b 0a 20 20 69 66 28 20 70 50 Header;. if( pP
16e2c 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 age->intKey ){.
16e2d 20 20 20 6e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d nKey = 0;. }
16e2e 65 6c 73 65 7b 0a 20 20 20 20 6e 4b 65 79 20 3d else{. nKey =
16e2f 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f (int)pCur->info
16e30 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 .nKey;. }. if(
16e31 20 73 6b 69 70 4b 65 79 20 29 7b 0a 20 20 20 20 skipKey ){.
16e32 61 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79 aPayload += nKey
16e33 3b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 ;. nLocal = p
16e34 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c Cur->info.nLocal
16e35 20 2d 20 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 - nKey;. }else
16e36 7b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 {. nLocal = p
16e37 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c Cur->info.nLocal
16e38 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 4c ;. assert( nL
16e39 6f 63 61 6c 3c 3d 6e 4b 65 79 20 29 3b 0a 20 20 ocal<=nKey );.
16e3a 7d 0a 20 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63 }. *pAmt = nLoc
16e3b 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 50 61 al;. return aPa
16e3c 79 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a yload;.}.../*.**
16e3d 20 46 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 For the entry t
16e3e 68 61 74 20 63 75 72 73 6f 72 20 70 43 75 72 20 hat cursor pCur
16e3f 69 73 20 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 is point to, ret
16e40 75 72 6e 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62 urn as.** many b
16e41 79 74 65 73 20 6f 66 20 74 68 65 20 6b 65 79 20 ytes of the key
16e42 6f 72 20 64 61 74 61 20 61 73 20 61 72 65 20 61 or data as are a
16e43 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 vailable on the
16e44 6c 6f 63 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20 local.** b-tree
16e45 70 61 67 65 2e 20 20 57 72 69 74 65 20 74 68 65 page. Write the
16e46 20 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c number of avail
16e47 61 62 6c 65 20 62 79 74 65 73 20 69 6e 74 6f 20 able bytes into
16e48 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 *pAmt..**.** The
16e49 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 pointer returne
16e4a 64 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 d is ephemeral.
16e4b 20 54 68 65 20 6b 65 79 2f 64 61 74 61 20 6d 61 The key/data ma
16e4c 79 20 6d 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20 y move.** or be
16e4d 64 65 73 74 72 6f 79 65 64 20 6f 6e 20 74 68 65 destroyed on the
16e4e 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e next call to an
16e4f 79 20 42 74 72 65 65 20 72 6f 75 74 69 6e 65 2c y Btree routine,
16e50 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 .** including ca
16e51 6c 6c 73 20 66 72 6f 6d 20 6f 74 68 65 72 20 74 lls from other t
16e52 68 72 65 61 64 73 20 61 67 61 69 6e 73 74 20 74 hreads against t
16e53 68 65 20 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a he same cache..*
16e54 2a 20 48 65 6e 63 65 2c 20 61 20 6d 75 74 65 78 * Hence, a mutex
16e55 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 on the BtShared
16e56 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 should be held
16e57 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 prior to calling
16e58 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 .** this routine
16e59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f ..**.** These ro
16e5a 75 74 69 6e 65 73 20 69 73 20 75 73 65 64 20 74 utines is used t
16e5b 6f 20 67 65 74 20 71 75 69 63 6b 20 61 63 63 65 o get quick acce
16e5c 73 73 20 74 6f 20 6b 65 79 20 61 6e 64 20 64 61 ss to key and da
16e5d 74 61 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d ta.** in the com
16e5e 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 6e mon case where n
16e5f 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 o overflow pages
16e60 20 61 72 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 are used..*/.SQ
16e61 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
16e62 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
16e63 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 74 BtreeKeyFetch(Bt
16e64 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e Cursor *pCur, in
16e65 74 20 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 t *pAmt){. cons
16e66 74 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 t void *p = 0;.
16e67 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
16e68 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 _mutex_held(pCur
16e69 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 ->pBtree->db->mu
16e6a 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
16e6b 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
16e6c 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 ex(pCur) );. if
16e6d 28 20 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 ( ALWAYS(pCur->e
16e6e 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
16e6f 4c 49 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 LID) ){. p =
16e70 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 (const void*)fet
16e71 63 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 chPayload(pCur,
16e72 70 41 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 pAmt, 0);. }.
16e73 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c 49 return p;.}.SQLI
16e74 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
16e75 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 void *sqlite3Bt
16e76 72 65 65 44 61 74 61 46 65 74 63 68 28 42 74 43 reeDataFetch(BtC
16e77 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 ursor *pCur, int
16e78 20 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 *pAmt){. const
16e79 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 void *p = 0;.
16e7a 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
16e7b 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d mutex_held(pCur-
16e7c 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 >pBtree->db->mut
16e7d 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
16e7e 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
16e7f 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 x(pCur) );. if(
16e80 20 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53 ALWAYS(pCur->eS
16e81 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
16e82 49 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 ID) ){. p = (
16e83 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 const void*)fetc
16e84 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 hPayload(pCur, p
16e85 41 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 Amt, 1);. }. r
16e86 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a eturn p;.}.../*.
16e87 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 ** Move the curs
16e88 6f 72 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 or down to a new
16e89 20 63 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68 child page. Th
16e8a 65 20 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 e newPgno argume
16e8b 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 nt is the.** pag
16e8c 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
16e8d 63 68 69 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f child page to mo
16e8e 76 65 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ve to..**.** Thi
16e8f 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
16e90 6e 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 ns SQLITE_CORRUP
16e91 54 20 69 66 20 74 68 65 20 70 61 67 65 2d 68 65 T if the page-he
16e92 61 64 65 72 20 66 6c 61 67 73 20 66 69 65 6c 64 ader flags field
16e93 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 of.** the new c
16e94 68 69 6c 64 20 70 61 67 65 20 64 6f 65 73 20 6e hild page does n
16e95 6f 74 20 6d 61 74 63 68 20 74 68 65 20 66 6c 61 ot match the fla
16e96 67 73 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 gs field of the
16e97 70 61 72 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a 20 parent (i.e..**
16e98 69 66 20 61 6e 20 69 6e 74 6b 65 79 20 70 61 67 if an intkey pag
16e99 65 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 e appears to be
16e9a 74 68 65 20 70 61 72 65 6e 74 20 6f 66 20 61 20 the parent of a
16e9b 6e 6f 6e 2d 69 6e 74 6b 65 79 20 70 61 67 65 2c non-intkey page,
16e9c 20 6f 72 0a 2a 2a 20 76 69 63 65 2d 76 65 72 73 or.** vice-vers
16e9d 61 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e a)..*/.static in
16e9e 74 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 42 74 t moveToChild(Bt
16e9f 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 Cursor *pCur, u3
16ea0 32 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 69 6e 2 newPgno){. in
16ea1 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 20 3d 20 t rc;. int i =
16ea2 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 4d pCur->iPage;. M
16ea3 65 6d 50 61 67 65 20 2a 70 4e 65 77 50 61 67 65 emPage *pNewPage
16ea4 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
16ea5 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 0a t = pCur->pBt;..
16ea6 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
16ea7 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
16ea8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
16ea9 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
16eaa 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 OR_VALID );. as
16eab 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 sert( pCur->iPag
16eac 65 3c 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 e<BTCURSOR_MAX_D
16ead 45 50 54 48 20 29 3b 0a 20 20 69 66 28 20 70 43 EPTH );. if( pC
16eae 75 72 2d 3e 69 50 61 67 65 3e 3d 28 42 54 43 55 ur->iPage>=(BTCU
16eaf 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 2d 31 RSOR_MAX_DEPTH-1
16eb0 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
16eb1 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
16eb2 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 KPT;. }. rc =
16eb3 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 getAndInitPage(p
16eb4 42 74 2c 20 6e 65 77 50 67 6e 6f 2c 20 26 70 4e Bt, newPgno, &pN
16eb5 65 77 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 ewPage);. if( r
16eb6 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
16eb7 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 2b pCur->apPage[i+
16eb8 31 5d 20 3d 20 70 4e 65 77 50 61 67 65 3b 0a 20 1] = pNewPage;.
16eb9 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 2b 31 pCur->aiIdx[i+1
16eba 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 ] = 0;. pCur->i
16ebb 50 61 67 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 2d Page++;.. pCur-
16ebc 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b >info.nSize = 0;
16ebd 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b . pCur->validNK
16ebe 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4e ey = 0;. if( pN
16ebf 65 77 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20 ewPage->nCell<1
16ec0 7c 7c 20 70 4e 65 77 50 61 67 65 2d 3e 69 6e 74 || pNewPage->int
16ec1 4b 65 79 21 3d 70 43 75 72 2d 3e 61 70 50 61 67 Key!=pCur->apPag
16ec2 65 5b 69 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a e[i]->intKey ){.
16ec3 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
16ec4 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
16ec5 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
16ec6 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 ITE_OK;.}..#ifnd
16ec7 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 ef NDEBUG./*.**
16ec8 50 61 67 65 20 70 50 61 72 65 6e 74 20 69 73 20 Page pParent is
16ec9 61 6e 20 69 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e an internal (non
16eca 2d 6c 65 61 66 29 20 74 72 65 65 20 70 61 67 65 -leaf) tree page
16ecb 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
16ecc 0a 2a 2a 20 61 73 73 65 72 74 73 20 74 68 61 74 .** asserts that
16ecd 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68 page number iCh
16ece 69 6c 64 20 69 73 20 74 68 65 20 6c 65 66 74 2d ild is the left-
16ecf 63 68 69 6c 64 20 69 66 20 74 68 65 20 69 49 64 child if the iId
16ed0 78 27 74 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 x'th.** cell in
16ed1 70 61 67 65 20 70 50 61 72 65 6e 74 2e 20 4f 72 page pParent. Or
16ed2 2c 20 69 66 20 69 49 64 78 20 69 73 20 65 71 75 , if iIdx is equ
16ed3 61 6c 20 74 6f 20 74 68 65 20 74 6f 74 61 6c 20 al to the total
16ed4 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c number of.** cel
16ed5 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 2c 20 74 ls in pParent, t
16ed6 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 hat page number
16ed7 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 72 69 iChild is the ri
16ed8 67 68 74 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a 20 ght-child of.**
16ed9 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 the page..*/.sta
16eda 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 50 tic void assertP
16edb 61 72 65 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61 arentIndex(MemPa
16edc 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74 ge *pParent, int
16edd 20 69 49 64 78 2c 20 50 67 6e 6f 20 69 43 68 69 iIdx, Pgno iChi
16ede 6c 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 ld){. assert( i
16edf 49 64 78 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 Idx<=pParent->nC
16ee0 65 6c 6c 20 29 3b 0a 20 20 69 66 28 20 69 49 64 ell );. if( iId
16ee1 78 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c x==pParent->nCel
16ee2 6c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 l ){. assert(
16ee3 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72 65 get4byte(&pPare
16ee4 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e nt->aData[pParen
16ee5 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 t->hdrOffset+8])
16ee6 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 65 ==iChild );. }e
16ee7 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
16ee8 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 get4byte(findCe
16ee9 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 49 64 78 ll(pParent, iIdx
16eea 29 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 ))==iChild );.
16eeb 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 }.}.#else.# def
16eec 69 6e 65 20 61 73 73 65 72 74 50 61 72 65 6e 74 ine assertParent
16eed 49 6e 64 65 78 28 78 2c 79 2c 7a 29 20 0a 23 65 Index(x,y,z) .#e
16eee 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 ndif../*.** Move
16eef 20 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 the cursor up t
16ef0 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 o the parent pag
16ef1 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69 e..**.** pCur->i
16ef2 64 78 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 dx is set to the
16ef3 20 63 65 6c 6c 20 69 6e 64 65 78 20 74 68 61 74 cell index that
16ef4 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f contains the po
16ef5 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 inter.** to the
16ef6 70 61 67 65 20 77 65 20 61 72 65 20 63 6f 6d 69 page we are comi
16ef7 6e 67 20 66 72 6f 6d 2e 20 20 49 66 20 77 65 20 ng from. If we
16ef8 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 are coming from
16ef9 74 68 65 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 the.** right-mos
16efa 74 20 63 68 69 6c 64 20 70 61 67 65 20 74 68 65 t child page the
16efb 6e 20 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73 n pCur->idx is s
16efc 65 74 20 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 74 et to one more t
16efd 68 61 6e 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 han.** the large
16efe 73 74 20 63 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a st cell index..*
16eff 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 6f /.static void mo
16f00 76 65 54 6f 50 61 72 65 6e 74 28 42 74 43 75 72 veToParent(BtCur
16f01 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 sor *pCur){. as
16f02 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
16f03 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
16f04 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
16f05 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
16f06 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 ALID );. assert
16f07 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 30 20 ( pCur->iPage>0
16f08 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
16f09 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
16f0a 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 iPage] );. asse
16f0b 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 0a 20 rtParentIndex(.
16f0c 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b pCur->apPage[
16f0d 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 pCur->iPage-1],
16f0e 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 . pCur->aiIdx
16f0f 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c [pCur->iPage-1],
16f10 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 . pCur->apPa
16f11 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d ge[pCur->iPage]-
16f12 3e 70 67 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 6c >pgno. );. rel
16f13 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 easePage(pCur->a
16f14 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
16f15 65 5d 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 e]);. pCur->iPa
16f16 67 65 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 6e ge--;. pCur->in
16f17 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
16f18 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 pCur->validNKey
16f19 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f = 0;.}../*.** Mo
16f1a 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f ve the cursor to
16f1b 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 72 6f point to the ro
16f1c 6f 74 20 70 61 67 65 20 6f 66 20 69 74 73 20 62 ot page of its b
16f1d 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 2e -tree structure.
16f1e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 .**.** If the ta
16f1f 62 6c 65 20 68 61 73 20 61 20 76 69 72 74 75 61 ble has a virtua
16f20 6c 20 72 6f 6f 74 20 70 61 67 65 2c 20 74 68 65 l root page, the
16f21 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 n the cursor is
16f22 6d 6f 76 65 64 20 74 6f 20 70 6f 69 6e 74 0a 2a moved to point.*
16f23 2a 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c * to the virtual
16f24 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 73 74 65 root page inste
16f25 61 64 20 6f 66 20 74 68 65 20 61 63 74 75 61 6c ad of the actual
16f26 20 72 6f 6f 74 20 70 61 67 65 2e 20 41 20 74 61 root page. A ta
16f27 62 6c 65 20 68 61 73 20 61 0a 2a 2a 20 76 69 72 ble has a.** vir
16f28 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 77 tual root page w
16f29 68 65 6e 20 74 68 65 20 61 63 74 75 61 6c 20 72 hen the actual r
16f2a 6f 6f 74 20 70 61 67 65 20 63 6f 6e 74 61 69 6e oot page contain
16f2b 73 20 6e 6f 20 63 65 6c 6c 73 20 61 6e 64 20 61 s no cells and a
16f2c 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 63 68 69 6c .** single chil
16f2d 64 20 70 61 67 65 2e 20 54 68 69 73 20 63 61 6e d page. This can
16f2e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 77 69 74 only happen wit
16f2f 68 20 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 h the table root
16f30 65 64 20 61 74 20 70 61 67 65 20 31 2e 0a 2a 2a ed at page 1..**
16f31 0a 2a 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65 .** If the b-tre
16f32 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 65 e structure is e
16f33 6d 70 74 79 2c 20 74 68 65 20 63 75 72 73 6f 72 mpty, the cursor
16f34 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74 6f state is set to
16f35 20 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e 56 41 .** CURSOR_INVA
16f36 4c 49 44 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 LID. Otherwise,
16f37 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 73 65 the cursor is se
16f38 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 t to point to th
16f39 65 20 66 69 72 73 74 0a 2a 2a 20 63 65 6c 6c 20 e first.** cell
16f3a 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20 72 located on the r
16f3b 6f 6f 74 20 28 6f 72 20 76 69 72 74 75 61 6c 20 oot (or virtual
16f3c 72 6f 6f 74 29 20 70 61 67 65 20 61 6e 64 20 74 root) page and t
16f3d 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 0a he cursor state.
16f3e 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 43 55 52 ** is set to CUR
16f3f 53 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a SOR_VALID..**.**
16f40 20 49 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f If this functio
16f41 6e 20 72 65 74 75 72 6e 73 20 73 75 63 63 65 73 n returns succes
16f42 73 66 75 6c 6c 79 2c 20 69 74 20 6d 61 79 20 62 sfully, it may b
16f43 65 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 e assumed that t
16f44 68 65 0a 2a 2a 20 70 61 67 65 2d 68 65 61 64 65 he.** page-heade
16f45 72 20 66 6c 61 67 73 20 69 6e 64 69 63 61 74 65 r flags indicate
16f46 20 74 68 61 74 20 74 68 65 20 5b 76 69 72 74 75 that the [virtu
16f47 61 6c 5d 20 72 6f 6f 74 2d 70 61 67 65 20 69 73 al] root-page is
16f48 20 74 68 65 20 65 78 70 65 63 74 65 64 20 0a 2a the expected .*
16f49 2a 20 6b 69 6e 64 20 6f 66 20 62 2d 74 72 65 65 * kind of b-tree
16f4a 20 70 61 67 65 20 28 69 2e 65 2e 20 69 66 20 77 page (i.e. if w
16f4b 68 65 6e 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 hen opening the
16f4c 63 75 72 73 6f 72 20 74 68 65 20 63 61 6c 6c 65 cursor the calle
16f4d 72 20 64 69 64 20 6e 6f 74 0a 2a 2a 20 73 70 65 r did not.** spe
16f4e 63 69 66 79 20 61 20 4b 65 79 49 6e 66 6f 20 73 cify a KeyInfo s
16f4f 74 72 75 63 74 75 72 65 20 74 68 65 20 66 6c 61 tructure the fla
16f50 67 73 20 62 79 74 65 20 69 73 20 73 65 74 20 74 gs byte is set t
16f51 6f 20 30 78 30 35 20 6f 72 20 30 78 30 44 2c 0a o 0x05 or 0x0D,.
16f52 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 61 20 ** indicating a
16f53 74 61 62 6c 65 20 62 2d 74 72 65 65 2c 20 6f 72 table b-tree, or
16f54 20 69 66 20 74 68 65 20 63 61 6c 6c 65 72 20 64 if the caller d
16f55 69 64 20 73 70 65 63 69 66 79 20 61 20 4b 65 79 id specify a Key
16f56 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 Info .** structu
16f57 72 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 re the flags byt
16f58 65 20 69 73 20 73 65 74 20 74 6f 20 30 78 30 32 e is set to 0x02
16f59 20 6f 72 20 30 78 30 41 2c 20 69 6e 64 69 63 61 or 0x0A, indica
16f5a 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 0a 2a 2a ting an index.**
16f5b 20 62 2d 74 72 65 65 29 2e 0a 2a 2f 0a 73 74 61 b-tree)..*/.sta
16f5c 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f tic int moveToRo
16f5d 6f 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 ot(BtCursor *pCu
16f5e 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 r){. MemPage *p
16f5f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d Root;. int rc =
16f60 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 SQLITE_OK;. Bt
16f61 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 ree *p = pCur->p
16f62 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 Btree;. BtShare
16f63 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
16f64 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 .. assert( curs
16f65 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
16f66 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r) );. assert(
16f67 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c CURSOR_INVALID <
16f68 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 CURSOR_REQUIRES
16f69 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 EEK );. assert(
16f6a 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 CURSOR_VALID
16f6b 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 < CURSOR_REQUIRE
16f6c 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 SEEK );. assert
16f6d 28 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 ( CURSOR_FAULT
16f6e 20 3e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 > CURSOR_REQUIR
16f6f 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 ESEEK );. if( p
16f70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 Cur->eState>=CUR
16f71 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 SOR_REQUIRESEEK
16f72 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d ){. if( pCur-
16f73 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
16f74 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 20 20 61 FAULT ){. a
16f75 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 ssert( pCur->ski
16f76 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b pNext!=SQLITE_OK
16f77 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e );. return
16f78 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b pCur->skipNext;
16f79 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
16f7a 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 e3BtreeClearCurs
16f7b 6f 72 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 or(pCur);. }..
16f7c 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 if( pCur->iPage
16f7d 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 >=0 ){. int i
16f7e 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 ;. for(i=1; i
16f7f 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 <=pCur->iPage; i
16f80 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 ++){. relea
16f81 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 sePage(pCur->apP
16f82 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 age[i]);. }.
16f83 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d pCur->iPage =
16f84 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
16f85 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 rc = getAndInit
16f86 50 61 67 65 28 70 42 74 2c 20 70 43 75 72 2d 3e Page(pBt, pCur->
16f87 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d pgnoRoot, &pCur-
16f88 3e 61 70 50 61 67 65 5b 30 5d 29 3b 0a 20 20 20 >apPage[0]);.
16f89 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
16f8a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 OK ){. pCur
16f8b 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f ->eState = CURSO
16f8c 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 R_INVALID;.
16f8d 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
16f8e 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 }. pCur->iPag
16f8f 65 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 e = 0;.. /* I
16f90 66 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f f pCur->pKeyInfo
16f91 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 is not NULL, th
16f92 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 74 68 en the caller th
16f93 61 74 20 6f 70 65 6e 65 64 20 74 68 69 73 20 63 at opened this c
16f94 75 72 73 6f 72 0a 20 20 20 20 2a 2a 20 65 78 70 ursor. ** exp
16f95 65 63 74 65 64 20 74 6f 20 6f 70 65 6e 20 69 74 ected to open it
16f96 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 on an index b-t
16f97 72 65 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ree. Otherwise,
16f98 69 66 20 70 4b 65 79 49 6e 66 6f 20 69 73 0a 20 if pKeyInfo is.
16f99 20 20 20 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 ** NULL, the
16f9a 63 61 6c 6c 65 72 20 65 78 70 65 63 74 73 20 61 caller expects a
16f9b 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2e 20 49 table b-tree. I
16f9c 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74 68 f this is not th
16f9d 65 20 63 61 73 65 2c 0a 20 20 20 20 2a 2a 20 72 e case,. ** r
16f9e 65 74 75 72 6e 20 61 6e 20 53 51 4c 49 54 45 5f eturn an SQLITE_
16f9f 43 4f 52 52 55 50 54 20 65 72 72 6f 72 2e 20 20 CORRUPT error.
16fa0 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
16fa1 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e Cur->apPage[0]->
16fa2 69 6e 74 4b 65 79 3d 3d 31 20 7c 7c 20 70 43 75 intKey==1 || pCu
16fa3 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e r->apPage[0]->in
16fa4 74 4b 65 79 3d 3d 30 20 29 3b 0a 20 20 20 20 69 tKey==0 );. i
16fa5 66 28 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e f( (pCur->pKeyIn
16fa6 66 6f 3d 3d 30 29 21 3d 70 43 75 72 2d 3e 61 70 fo==0)!=pCur->ap
16fa7 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 Page[0]->intKey
16fa8 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
16fa9 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
16faa 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a KPT;. }. }..
16fab 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 /* Assert that
16fac 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 the root page i
16fad 73 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 s of the correct
16fae 20 74 79 70 65 2e 20 54 68 69 73 20 6d 75 73 74 type. This must
16faf 20 62 65 20 74 68 65 0a 20 20 2a 2a 20 63 61 73 be the. ** cas
16fb0 65 20 61 73 20 74 68 65 20 63 61 6c 6c 20 74 6f e as the call to
16fb1 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 this function t
16fb2 68 61 74 20 6c 6f 61 64 65 64 20 74 68 65 20 72 hat loaded the r
16fb3 6f 6f 74 2d 70 61 67 65 20 28 65 69 74 68 65 72 oot-page (either
16fb4 0a 20 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 . ** this call
16fb5 6f 72 20 61 20 70 72 65 76 69 6f 75 73 20 69 6e or a previous in
16fb6 76 6f 63 61 74 69 6f 6e 29 20 77 6f 75 6c 64 20 vocation) would
16fb7 68 61 76 65 20 64 65 74 65 63 74 65 64 20 63 6f have detected co
16fb8 72 72 75 70 74 69 6f 6e 20 0a 20 20 2a 2a 20 69 rruption . ** i
16fb9 66 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e f the assumption
16fba 20 77 65 72 65 20 6e 6f 74 20 74 72 75 65 2c 20 were not true,
16fbb 61 6e 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f and it is not po
16fbc 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 20 66 ssible for the f
16fbd 6c 61 67 73 20 0a 20 20 2a 2a 20 62 79 74 65 20 lags . ** byte
16fbe 74 6f 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 to have been mod
16fbf 69 66 69 65 64 20 77 68 69 6c 65 20 74 68 69 73 ified while this
16fc0 20 63 75 72 73 6f 72 20 69 73 20 68 6f 6c 64 69 cursor is holdi
16fc1 6e 67 20 61 20 72 65 66 65 72 65 6e 63 65 0a 20 ng a reference.
16fc2 20 2a 2a 20 74 6f 20 74 68 65 20 70 61 67 65 2e ** to the page.
16fc3 20 20 2a 2f 0a 20 20 70 52 6f 6f 74 20 3d 20 70 */. pRoot = p
16fc4 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a Cur->apPage[0];.
16fc5 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d assert( pRoot-
16fc6 3e 70 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e >pgno==pCur->pgn
16fc7 6f 52 6f 6f 74 20 29 3b 0a 20 20 61 73 73 65 72 oRoot );. asser
16fc8 74 28 20 70 52 6f 6f 74 2d 3e 69 73 49 6e 69 74 t( pRoot->isInit
16fc9 20 26 26 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 && (pCur->pKeyI
16fca 6e 66 6f 3d 3d 30 29 3d 3d 70 52 6f 6f 74 2d 3e nfo==0)==pRoot->
16fcb 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 70 43 75 intKey );.. pCu
16fcc 72 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b r->aiIdx[0] = 0;
16fcd 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 . pCur->info.nS
16fce 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d ize = 0;. pCur-
16fcf 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 >atLast = 0;. p
16fd0 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d Cur->validNKey =
16fd1 20 30 3b 0a 0a 20 20 69 66 28 20 70 52 6f 6f 74 0;.. if( pRoot
16fd2 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 21 70 ->nCell==0 && !p
16fd3 52 6f 6f 74 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Root->leaf ){.
16fd4 20 20 50 67 6e 6f 20 73 75 62 70 61 67 65 3b 0a Pgno subpage;.
16fd5 20 20 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 70 if( pRoot->p
16fd6 67 6e 6f 21 3d 31 20 29 20 72 65 74 75 72 6e 20 gno!=1 ) return
16fd7 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
16fd8 4b 50 54 3b 0a 20 20 20 20 73 75 62 70 61 67 65 KPT;. subpage
16fd9 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 52 6f = get4byte(&pRo
16fda 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d ot->aData[pRoot-
16fdb 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a >hdrOffset+8]);.
16fdc 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 pCur->eState
16fdd 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b = CURSOR_VALID;
16fde 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f . rc = moveTo
16fdf 43 68 69 6c 64 28 70 43 75 72 2c 20 73 75 62 70 Child(pCur, subp
16fe0 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 age);. }else{.
16fe1 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 pCur->eState
16fe2 3d 20 28 28 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c = ((pRoot->nCell
16fe3 3e 30 29 3f 43 55 52 53 4f 52 5f 56 41 4c 49 44 >0)?CURSOR_VALID
16fe4 3a 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 29 :CURSOR_INVALID)
16fe5 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
16fe6 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 c;.}../*.** Move
16fe7 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e the cursor down
16fe8 20 74 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 to the left-mos
16fe9 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e t leaf entry ben
16fea 65 61 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74 72 eath the.** entr
16feb 79 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 y to which it is
16fec 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
16fed 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c ing..**.** The l
16fee 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69 73 eft-most leaf is
16fef 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 the one with th
16ff0 65 20 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 2d e smallest key -
16ff1 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e the first.** in
16ff2 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 ascending order
16ff3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
16ff4 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 42 moveToLeftmost(B
16ff5 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
16ff6 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 Pgno pgno;. i
16ff7 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
16ff8 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 K;. MemPage *pP
16ff9 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 age;.. assert(
16ffa 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
16ffb 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
16ffc 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
16ffd 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
16ffe 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 ;. while( rc==S
16fff 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 QLITE_OK && !(pP
17000 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
17001 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 ge[pCur->iPage])
17002 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 61 73 ->leaf ){. as
17003 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 sert( pCur->aiId
17004 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 x[pCur->iPage]<p
17005 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 Page->nCell );.
17006 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 pgno = get4by
17007 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 te(findCell(pPag
17008 65 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 e, pCur->aiIdx[p
17009 43 75 72 2d 3e 69 50 61 67 65 5d 29 29 3b 0a 20 Cur->iPage]));.
1700a 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 rc = moveToCh
1700b 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b ild(pCur, pgno);
1700c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1700d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 ;.}../*.** Move
1700e 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 the cursor down
1700f 74 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 to the right-mos
17010 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e t leaf entry ben
17011 65 61 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 65 eath the.** page
17012 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 20 to which it is
17013 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 currently pointi
17014 6e 67 2e 20 20 4e 6f 74 69 63 65 20 74 68 65 20 ng. Notice the
17015 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 62 65 difference.** be
17016 74 77 65 65 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 tween moveToLeft
17017 6d 6f 73 74 28 29 20 61 6e 64 20 6d 6f 76 65 54 most() and moveT
17018 6f 52 69 67 68 74 6d 6f 73 74 28 29 2e 20 20 6d oRightmost(). m
17019 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 0a oveToLeftmost().
1701a 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 6c 65 66 ** finds the lef
1701b 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e t-most entry ben
1701c 65 61 74 68 20 74 68 65 20 2a 65 6e 74 72 79 2a eath the *entry*
1701d 20 77 68 65 72 65 61 73 20 6d 6f 76 65 54 6f 52 whereas moveToR
1701e 69 67 68 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 ightmost().** fi
1701f 6e 64 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f nds the right-mo
17020 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 st entry beneath
17021 20 74 68 65 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a the *page*..**.
17022 2a 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 ** The right-mos
17023 74 20 65 6e 74 72 79 20 69 73 20 74 68 65 20 6f t entry is the o
17024 6e 65 20 77 69 74 68 20 74 68 65 20 6c 61 72 67 ne with the larg
17025 65 73 74 20 6b 65 79 20 2d 20 74 68 65 20 6c 61 est key - the la
17026 73 74 0a 2a 2a 20 6b 65 79 20 69 6e 20 61 73 63 st.** key in asc
17027 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f ending order..*/
17028 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 .static int move
17029 54 6f 52 69 67 68 74 6d 6f 73 74 28 42 74 43 75 ToRightmost(BtCu
1702a 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 rsor *pCur){. P
1702b 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 gno pgno;. int
1702c 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1702d 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
1702e 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 = 0;.. assert(
1702f 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
17030 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 x(pCur) );. ass
17031 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 ert( pCur->eStat
17032 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e==CURSOR_VALID
17033 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d );. while( rc==
17034 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 SQLITE_OK && !(p
17035 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 Page = pCur->apP
17036 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
17037 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 )->leaf ){. p
17038 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 gno = get4byte(&
17039 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 pPage->aData[pPa
1703a 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d ge->hdrOffset+8]
1703b 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 );. pCur->aiI
1703c 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 dx[pCur->iPage]
1703d 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a = pPage->nCell;.
1703e 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 rc = moveToC
1703f 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 hild(pCur, pgno)
17040 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d ;. }. if( rc==
17041 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
17042 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
17043 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 r->iPage] = pPag
17044 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 e->nCell-1;.
17045 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
17046 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e = 0;. pCur->
17047 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 validNKey = 0;.
17048 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
17049 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 }../* Move the c
1704a 75 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69 72 ursor to the fir
1704b 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 st entry in the
1704c 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 table. Return S
1704d 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 QLITE_OK.** on s
1704e 75 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 uccess. Set *pR
1704f 65 73 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 es to 0 if the c
17050 75 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 ursor actually p
17051 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 oints to somethi
17052 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 ng.** or set *pR
17053 65 73 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 es to 1 if the t
17054 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a able is empty..*
17055 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
17056 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
17057 65 46 69 72 73 74 28 42 74 43 75 72 73 6f 72 20 eFirst(BtCursor
17058 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 *pCur, int *pRes
17059 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 ){. int rc;..
1705a 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
1705b 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
1705c 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
1705d 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1705e 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d Cur->pBtree->db-
1705f 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 >mutex) );. rc
17060 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 = moveToRoot(pCu
17061 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 r);. if( rc==SQ
17062 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 LITE_OK ){. i
17063 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d f( pCur->eState=
17064 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
17065 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
17066 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
17067 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c ur->iPage]->nCel
17068 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 l==0 );. *p
17069 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 Res = 1;. r
1706a 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1706b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1706c 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 assert( pCur->ap
1706d 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
1706e 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 ]->nCell>0 );.
1706f 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 *pRes = 0;.
17070 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f rc = moveTo
17071 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a Leftmost(pCur);.
17072 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
17073 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 rn rc;.}../* Mov
17074 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 e the cursor to
17075 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 the last entry i
17076 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 n the table. Re
17077 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a turn SQLITE_OK.*
17078 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 * on success. S
17079 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 et *pRes to 0 if
1707a 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 the cursor actu
1707b 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 ally points to s
1707c 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 omething.** or s
1707d 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 et *pRes to 1 if
1707e 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d the table is em
1707f 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 pty..*/.SQLITE_P
17080 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
17081 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 75 e3BtreeLast(BtCu
17082 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 rsor *pCur, int
17083 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 *pRes){. int rc
17084 3b 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63 75 ;. . assert( cu
17085 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
17086 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
17087 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
17088 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 held(pCur->pBtre
17089 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b e->db->mutex) );
1708a 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 .. /* If the cu
1708b 72 73 6f 72 20 61 6c 72 65 61 64 79 20 70 6f 69 rsor already poi
1708c 6e 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20 nts to the last
1708d 65 6e 74 72 79 2c 20 74 68 69 73 20 69 73 20 61 entry, this is a
1708e 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 no-op. */. if(
1708f 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 CURSOR_VALID==p
17090 43 75 72 2d 3e 65 53 74 61 74 65 20 26 26 20 70 Cur->eState && p
17091 43 75 72 2d 3e 61 74 4c 61 73 74 20 29 7b 0a 23 Cur->atLast ){.#
17092 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
17093 55 47 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62 UG. /* This b
17094 6c 6f 63 6b 20 73 65 72 76 65 73 20 74 6f 20 61 lock serves to a
17095 73 73 65 72 74 28 29 20 74 68 61 74 20 74 68 65 ssert() that the
17096 20 63 75 72 73 6f 72 20 72 65 61 6c 6c 79 20 64 cursor really d
17097 6f 65 73 20 70 6f 69 6e 74 20 0a 20 20 20 20 2a oes point . *
17098 2a 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e * to the last en
17099 74 72 79 20 69 6e 20 74 68 65 20 62 2d 74 72 65 try in the b-tre
1709a 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 e. */. int ii
1709b 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 ;. for(ii=0;
1709c 69 69 3c 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 ii<pCur->iPage;
1709d 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 ii++){. ass
1709e 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ert( pCur->aiIdx
1709f 5b 69 69 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 61 [ii]==pCur->apPa
170a0 67 65 5b 69 69 5d 2d 3e 6e 43 65 6c 6c 20 29 3b ge[ii]->nCell );
170a1 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
170a2 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 t( pCur->aiIdx[p
170a3 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 70 43 75 Cur->iPage]==pCu
170a4 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
170a5 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 20 iPage]->nCell-1
170a6 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
170a7 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
170a8 2d 3e 69 50 61 67 65 5d 2d 3e 6c 65 61 66 20 29 ->iPage]->leaf )
170a9 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74 ;.#endif. ret
170aa 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
170ab 20 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 }.. rc = moveT
170ac 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 oRoot(pCur);. i
170ad 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
170ae 20 29 7b 0a 20 20 20 20 69 66 28 20 43 55 52 53 ){. if( CURS
170af 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 OR_INVALID==pCur
170b0 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 ->eState ){.
170b1 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
170b2 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
170b3 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b ge]->nCell==0 );
170b4 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 . *pRes = 1
170b5 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
170b6 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
170b7 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
170b8 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 2a VALID );. *
170b9 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 pRes = 0;.
170ba 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 rc = moveToRight
170bb 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 most(pCur);.
170bc 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d pCur->atLast =
170bd 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f rc==SQLITE_OK ?
170be 31 3a 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 1:0;. }. }.
170bf 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
170c0 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f * Move the curso
170c1 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 r so that it poi
170c2 6e 74 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 nts to an entry
170c3 6e 65 61 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a near the key .**
170c4 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 49 specified by pI
170c5 64 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e dxKey or intKey.
170c6 20 20 20 52 65 74 75 72 6e 20 61 20 73 75 63 63 Return a succ
170c7 65 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 ess code..**.**
170c8 46 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 For INTKEY table
170c9 73 2c 20 74 68 65 20 69 6e 74 4b 65 79 20 70 61 s, the intKey pa
170ca 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e rameter is used.
170cb 20 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75 pIdxKey .** mu
170cc 73 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 st be NULL. For
170cd 20 69 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70 index tables, p
170ce 49 64 78 4b 65 79 20 69 73 20 75 73 65 64 20 61 IdxKey is used a
170cf 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20 nd intKey.** is
170d0 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 ignored..**.** I
170d1 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68 f an exact match
170d2 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74 is not found, t
170d3 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 hen the cursor i
170d4 73 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74 s always.** left
170d5 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 6c pointing at a l
170d6 65 61 66 20 70 61 67 65 20 77 68 69 63 68 20 77 eaf page which w
170d7 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 6e ould hold the en
170d8 74 72 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 72 try if it.** wer
170d9 65 20 70 72 65 73 65 6e 74 2e 20 20 54 68 65 20 e present. The
170da 63 75 72 73 6f 72 20 6d 69 67 68 74 20 70 6f 69 cursor might poi
170db 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74 nt to an entry t
170dc 68 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66 hat comes.** bef
170dd 6f 72 65 20 6f 72 20 61 66 74 65 72 20 74 68 65 ore or after the
170de 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 key..**.** An i
170df 6e 74 65 67 65 72 20 69 73 20 77 72 69 74 74 65 nteger is writte
170e0 6e 20 69 6e 74 6f 20 2a 70 52 65 73 20 77 68 69 n into *pRes whi
170e1 63 68 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 ch is the result
170e2 20 6f 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e 67 of.** comparing
170e3 20 74 68 65 20 6b 65 79 20 77 69 74 68 20 74 68 the key with th
170e4 65 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 e entry to which
170e5 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 0a the cursor is .
170e6 2a 2a 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54 68 ** pointing. Th
170e7 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68 65 e meaning of the
170e8 20 69 6e 74 65 67 65 72 20 77 72 69 74 74 65 6e integer written
170e9 20 69 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 69 into.** *pRes i
170ea 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a s as follows:.**
170eb 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 20 .** *pRes<0
170ec 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 The cursor
170ed 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 is left pointing
170ee 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 at an entry tha
170ef 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
170f0 20 20 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 72 is smaller
170f1 20 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 than intKey/pId
170f2 78 4b 65 79 20 6f 72 20 69 66 20 74 68 65 20 74 xKey or if the t
170f3 61 62 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a able is empty.**
170f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
170f5 20 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 and the cursor
170f6 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 6c 65 is therefore le
170f7 66 74 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 ft point to noth
170f8 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a ing..**.** *
170f9 70 52 65 73 3d 3d 30 20 20 20 20 20 54 68 65 20 pRes==0 The
170fa 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 cursor is left p
170fb 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e ointing at an en
170fc 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 try that.**
170fd 20 20 20 20 20 20 20 20 20 20 20 20 20 65 78 61 exa
170fe 63 74 6c 79 20 6d 61 74 63 68 65 73 20 69 6e 74 ctly matches int
170ff 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a Key/pIdxKey..**.
17100 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30 20 20 ** *pRes>0
17101 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 The cursor i
17102 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 s left pointing
17103 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 at an entry that
17104 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
17105 20 20 20 20 20 69 73 20 6c 61 72 67 65 72 20 74 is larger t
17106 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b han intKey/pIdxK
17107 65 79 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 ey..**.*/.SQLITE
17108 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
17109 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 ite3BtreeMovetoU
1710a 6e 70 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 npacked(. BtCur
1710b 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 sor *pCur,
1710c 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f /* The curso
1710d 72 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f r to be moved */
1710e 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 . UnpackedRecor
1710f 64 20 2a 70 49 64 78 4b 65 79 2c 20 2f 2a 20 55 d *pIdxKey, /* U
17110 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 npacked index ke
17111 79 20 2a 2f 0a 20 20 69 36 34 20 69 6e 74 4b 65 y */. i64 intKe
17112 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y,
17113 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6b 65 79 /* The table key
17114 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 52 69 */. int biasRi
17115 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f ght, /
17116 2a 20 49 66 20 74 72 75 65 2c 20 62 69 61 73 20 * If true, bias
17117 74 68 65 20 73 65 61 72 63 68 20 74 6f 20 74 68 the search to th
17118 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 e high end */.
17119 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 int *pRes
1711a 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
1711b 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 73 e search results
1711c 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e here */.){. in
1711d 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 t rc;.. assert(
1711e 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
1711f 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 x(pCur) );. ass
17120 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
17121 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 ex_held(pCur->pB
17122 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 tree->db->mutex)
17123 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52 );. assert( pR
17124 65 73 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 es );. assert(
17125 28 70 49 64 78 4b 65 79 3d 3d 30 29 3d 3d 28 70 (pIdxKey==0)==(p
17126 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 Cur->pKeyInfo==0
17127 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ) );.. /* If th
17128 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 e cursor is alre
17129 61 64 79 20 70 6f 73 69 74 69 6f 6e 65 64 20 61 ady positioned a
1712a 74 20 74 68 65 20 70 6f 69 6e 74 20 77 65 20 61 t the point we a
1712b 72 65 20 74 72 79 69 6e 67 0a 20 20 2a 2a 20 74 re trying. ** t
1712c 6f 20 6d 6f 76 65 20 74 6f 2c 20 74 68 65 6e 20 o move to, then
1712d 6a 75 73 74 20 72 65 74 75 72 6e 20 77 69 74 68 just return with
1712e 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f out doing any wo
1712f 72 6b 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 rk */. if( pCur
17130 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
17131 5f 56 41 4c 49 44 20 26 26 20 70 43 75 72 2d 3e _VALID && pCur->
17132 76 61 6c 69 64 4e 4b 65 79 20 0a 20 20 20 26 26 validNKey . &&
17133 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d pCur->apPage[0]
17134 2d 3e 69 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20 ->intKey . ){.
17135 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 if( pCur->inf
17136 6f 2e 6e 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 o.nKey==intKey )
17137 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 {. *pRes =
17138 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 0;. return
17139 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
1713a 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 . if( pCur->a
1713b 74 4c 61 73 74 20 26 26 20 70 43 75 72 2d 3e 69 tLast && pCur->i
1713c 6e 66 6f 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 20 nfo.nKey<intKey
1713d 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d ){. *pRes =
1713e 20 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 -1;. retur
1713f 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 n SQLITE_OK;.
17140 20 7d 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d }. }.. rc = m
17141 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b oveToRoot(pCur);
17142 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
17143 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
17144 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
17145 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
17146 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge] );. assert(
17147 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
17148 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 73 49 6e ur->iPage]->isIn
17149 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 it );. assert(
1714a 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
1714b 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c r->iPage]->nCell
1714c 3e 30 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 >0 || pCur->eSta
1714d 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c te==CURSOR_INVAL
1714e 49 44 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 ID );. if( pCur
1714f 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
17150 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 _INVALID ){.
17151 2a 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 *pRes = -1;.
17152 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 assert( pCur->ap
17153 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
17154 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 ]->nCell==0 );.
17155 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
17156 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 _OK;. }. asser
17157 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b t( pCur->apPage[
17158 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 49 0]->intKey || pI
17159 64 78 4b 65 79 20 29 3b 0a 20 20 66 6f 72 28 3b dxKey );. for(;
1715a 3b 29 7b 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c ;){. int lwr,
1715b 20 75 70 72 3b 0a 20 20 20 20 50 67 6e 6f 20 63 upr;. Pgno c
1715c 68 6c 64 50 67 3b 0a 20 20 20 20 4d 65 6d 50 61 hldPg;. MemPa
1715d 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 ge *pPage = pCur
1715e 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
1715f 50 61 67 65 5d 3b 0a 20 20 20 20 69 6e 74 20 63 Page];. int c
17160 3b 0a 0a 20 20 20 20 2f 2a 20 70 50 61 67 65 2d ;.. /* pPage-
17161 3e 6e 43 65 6c 6c 20 6d 75 73 74 20 62 65 20 67 >nCell must be g
17162 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f reater than zero
17163 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 . If this is the
17164 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a root-page. *
17165 2a 20 74 68 65 20 63 75 72 73 6f 72 20 77 6f 75 * the cursor wou
17166 6c 64 20 68 61 76 65 20 62 65 65 6e 20 49 4e 56 ld have been INV
17167 41 4c 49 44 20 61 62 6f 76 65 20 61 6e 64 20 74 ALID above and t
17168 68 69 73 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70 his for(;;) loop
17169 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72 75 6e 2e . ** not run.
1716a 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 If this is not
1716b 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 2c 20 74 the root-page, t
1716c 68 65 6e 20 74 68 65 20 6d 6f 76 65 54 6f 43 68 hen the moveToCh
1716d 69 6c 64 28 29 20 72 6f 75 74 69 6e 65 0a 20 20 ild() routine.
1716e 20 20 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 ** would have
1716f 61 6c 72 65 61 64 79 20 64 65 74 65 63 74 65 64 already detected
17170 20 64 62 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 db corruption.
17171 53 69 6d 69 6c 61 72 6c 79 2c 20 70 50 61 67 65 Similarly, pPage
17172 20 6d 75 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 must. ** be
17173 74 68 65 20 72 69 67 68 74 20 6b 69 6e 64 20 28 the right kind (
17174 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 29 20 index or table)
17175 6f 66 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 of b-tree page.
17176 4f 74 68 65 72 77 69 73 65 0a 20 20 20 20 2a 2a Otherwise. **
17177 20 61 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29 a moveToChild()
17178 20 6f 72 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29 or moveToRoot()
17179 20 63 61 6c 6c 20 77 6f 75 6c 64 20 68 61 76 65 call would have
1717a 20 64 65 74 65 63 74 65 64 20 63 6f 72 72 75 70 detected corrup
1717b 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 61 73 tion. */. as
1717c 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 sert( pPage->nCe
1717d 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 ll>0 );. asse
1717e 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 rt( pPage->intKe
1717f 79 3d 3d 28 70 49 64 78 4b 65 79 3d 3d 30 29 20 y==(pIdxKey==0)
17180 29 3b 0a 20 20 20 20 6c 77 72 20 3d 20 30 3b 0a );. lwr = 0;.
17181 20 20 20 20 75 70 72 20 3d 20 70 50 61 67 65 2d upr = pPage-
17182 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 66 >nCell-1;. if
17183 28 20 62 69 61 73 52 69 67 68 74 20 29 7b 0a 20 ( biasRight ){.
17184 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 pCur->aiIdx
17185 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 [pCur->iPage] =
17186 28 75 31 36 29 75 70 72 3b 0a 20 20 20 20 7d 65 (u16)upr;. }e
17187 6c 73 65 7b 0a 20 20 20 20 20 20 70 43 75 72 2d lse{. pCur-
17188 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
17189 67 65 5d 20 3d 20 28 75 31 36 29 28 28 75 70 72 ge] = (u16)((upr
1718a 2b 6c 77 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a +lwr)/2);. }.
1718b 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 for(;;){.
1718c 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 int idx = pCu
1718d 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
1718e 50 61 67 65 5d 3b 20 2f 2a 20 49 6e 64 65 78 20 Page]; /* Index
1718f 6f 66 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 of current cell
17190 69 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 20 20 in pPage */.
17191 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 u8 *pCell;
17192 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17193 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
17194 20 74 6f 20 63 75 72 72 65 6e 74 20 63 65 6c 6c to current cell
17195 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a 0a 20 20 in pPage */..
17196 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e pCur->info.n
17197 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 Size = 0;.
17198 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c pCell = findCell
17199 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b 20 70 (pPage, idx) + p
1719a 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 Page->childPtrSi
1719b 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 ze;. if( pP
1719c 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 age->intKey ){.
1719d 20 20 20 20 20 20 20 69 36 34 20 6e 43 65 6c 6c i64 nCell
1719e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 Key;. if(
1719f 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 pPage->hasData
171a0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 32 ){. u32
171a1 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20 dummy;.
171a2 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56 61 pCell += getVa
171a3 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64 75 rint32(pCell, du
171a4 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a mmy);. }.
171a5 20 20 20 20 20 20 20 20 67 65 74 56 61 72 69 6e getVarin
171a6 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29 26 t(pCell, (u64*)&
171a7 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 nCellKey);.
171a8 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3d if( nCellKey=
171a9 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 =intKey ){.
171aa 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 20 c = 0;.
171ab 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 43 }else if( nC
171ac 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b ellKey<intKey ){
171ad 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2d . c = -
171ae 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 1;. }else
171af 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 {. asse
171b0 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e 74 rt( nCellKey>int
171b1 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20 20 Key );.
171b2 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20 20 c = +1;.
171b3 20 7d 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d }. pCur-
171b4 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a >validNKey = 1;.
171b5 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e pCur->in
171b6 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b fo.nKey = nCellK
171b7 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ey;. }else{
171b8 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
171b9 6d 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 65 maximum supporte
171ba 64 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 33 d page-size is 3
171bb 32 37 36 38 20 62 79 74 65 73 2e 20 54 68 69 73 2768 bytes. This
171bc 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 20 20 means that.
171bd 20 20 20 20 2a 2a 20 74 68 65 20 6d 61 78 69 6d ** the maxim
171be 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 63 um number of rec
171bf 6f 72 64 20 62 79 74 65 73 20 73 74 6f 72 65 64 ord bytes stored
171c0 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 42 2d 54 on an index B-T
171c1 72 65 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 ree. ** p
171c2 61 67 65 20 69 73 20 61 74 20 6d 6f 73 74 20 38 age is at most 8
171c3 31 39 38 20 62 79 74 65 73 2c 20 77 68 69 63 68 198 bytes, which
171c4 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64 20 61 may be stored a
171c5 73 20 61 20 32 2d 62 79 74 65 0a 20 20 20 20 20 s a 2-byte.
171c6 20 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20 54 68 ** varint. Th
171c7 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 is information i
171c8 73 20 75 73 65 64 20 74 6f 20 61 74 74 65 6d 70 s used to attemp
171c9 74 20 74 6f 20 61 76 6f 69 64 20 70 61 72 73 69 t to avoid parsi
171ca 6e 67 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 ng . ** t
171cb 68 65 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 62 he entire cell b
171cc 79 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 74 y checking for t
171cd 68 65 20 63 61 73 65 73 20 77 68 65 72 65 20 74 he cases where t
171ce 68 65 20 72 65 63 6f 72 64 20 69 73 20 0a 20 20 he record is .
171cf 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 ** stored
171d0 65 6e 74 69 72 65 6c 79 20 77 69 74 68 69 6e 20 entirely within
171d1 74 68 65 20 62 2d 74 72 65 65 20 70 61 67 65 20 the b-tree page
171d2 62 79 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68 by inspecting th
171d3 65 20 66 69 72 73 74 20 0a 20 20 20 20 20 20 20 e first .
171d4 20 2a 2a 20 32 20 62 79 74 65 73 20 6f 66 20 74 ** 2 bytes of t
171d5 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 20 20 20 he cell..
171d6 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 */. int
171d7 6e 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b 30 5d nCell = pCell[0]
171d8 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 28 ;. if( !(
171d9 6e 43 65 6c 6c 20 26 20 30 78 38 30 29 20 26 26 nCell & 0x80) &&
171da 20 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 6d nCell<=pPage->m
171db 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 axLocal ){.
171dc 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 /* This bra
171dd 6e 63 68 20 72 75 6e 73 20 69 66 20 74 68 65 20 nch runs if the
171de 72 65 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c record-size fiel
171df 64 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73 d of the cell is
171e0 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 a. **
171e1 73 69 6e 67 6c 65 20 62 79 74 65 20 76 61 72 69 single byte vari
171e2 6e 74 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72 nt and the recor
171e3 64 20 66 69 74 73 20 65 6e 74 69 72 65 6c 79 20 d fits entirely
171e4 6f 6e 20 74 68 65 20 6d 61 69 6e 0a 20 20 20 20 on the main.
171e5 20 20 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 ** b-tree
171e6 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 page. */.
171e7 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 c = sqlite3V
171e8 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 dbeRecordCompare
171e9 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 (nCell, (void*)&
171ea 70 43 65 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b 65 pCell[1], pIdxKe
171eb 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 y);. }els
171ec 65 20 69 66 28 20 21 28 70 43 65 6c 6c 5b 31 5d e if( !(pCell[1]
171ed 20 26 20 30 78 38 30 29 20 0a 20 20 20 20 20 20 & 0x80) .
171ee 20 20 20 20 26 26 20 28 6e 43 65 6c 6c 20 3d 20 && (nCell =
171ef 28 28 6e 43 65 6c 6c 26 30 78 37 66 29 3c 3c 37 ((nCell&0x7f)<<7
171f0 29 20 2b 20 70 43 65 6c 6c 5b 31 5d 29 3c 3d 70 ) + pCell[1])<=p
171f1 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 0a 20 Page->maxLocal.
171f2 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 ){.
171f3 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 /* The recor
171f4 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 20 d-size field is
171f5 61 20 32 20 62 79 74 65 20 76 61 72 69 6e 74 20 a 2 byte varint
171f6 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 0a and the record .
171f7 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69 74 ** fit
171f8 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68 s entirely on th
171f9 65 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 e main b-tree pa
171fa 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 ge. */.
171fb 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 c = sqlite3Vdb
171fc 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e eRecordCompare(n
171fd 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43 Cell, (void*)&pC
171fe 65 6c 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 79 29 ell[2], pIdxKey)
171ff 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
17200 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 . /* Th
17201 65 20 72 65 63 6f 72 64 20 66 6c 6f 77 73 20 6f e record flows o
17202 76 65 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72 20 ver onto one or
17203 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 more overflow pa
17204 67 65 73 2e 20 49 6e 0a 20 20 20 20 20 20 20 20 ges. In.
17205 20 20 2a 2a 20 74 68 69 73 20 63 61 73 65 20 74 ** this case t
17206 68 65 20 77 68 6f 6c 65 20 63 65 6c 6c 20 6e 65 he whole cell ne
17207 65 64 73 20 74 6f 20 62 65 20 70 61 72 73 65 64 eds to be parsed
17208 2c 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 , a buffer alloc
17209 61 74 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a ated. *
1720a 2a 20 61 6e 64 20 61 63 63 65 73 73 50 61 79 6c * and accessPayl
1720b 6f 61 64 28 29 20 75 73 65 64 20 74 6f 20 72 65 oad() used to re
1720c 74 72 69 65 76 65 20 74 68 65 20 72 65 63 6f 72 trieve the recor
1720d 64 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20 d into the.
1720e 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 20 62 ** buffer b
1720f 65 66 6f 72 65 20 56 64 62 65 52 65 63 6f 72 64 efore VdbeRecord
17210 43 6f 6d 70 61 72 65 28 29 20 63 61 6e 20 62 65 Compare() can be
17211 20 63 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20 20 called. */.
17212 20 20 20 20 20 20 76 6f 69 64 20 2a 70 43 65 6c void *pCel
17213 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 lKey;.
17214 75 38 20 2a 20 63 6f 6e 73 74 20 70 43 65 6c 6c u8 * const pCell
17215 42 6f 64 79 20 3d 20 70 43 65 6c 6c 20 2d 20 70 Body = pCell - p
17216 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 Page->childPtrSi
17217 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 62 74 ze;. bt
17218 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
17219 70 50 61 67 65 2c 20 70 43 65 6c 6c 42 6f 64 79 pPage, pCellBody
1721a 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a , &pCur->info);.
1721b 20 20 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 20 nCell
1721c 3d 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 = (int)pCur->inf
1721d 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 o.nKey;.
1721e 20 20 70 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c pCellKey = sql
1721f 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 43 65 6c ite3Malloc( nCel
17220 6c 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 l );. i
17221 66 28 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 f( pCellKey==0 )
17222 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 {. rc
17223 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
17224 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 . got
17225 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b o moveto_finish;
17226 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
17227 20 20 20 20 20 20 20 72 63 20 3d 20 61 63 63 65 rc = acce
17228 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 ssPayload(pCur,
17229 30 2c 20 6e 43 65 6c 6c 2c 20 28 75 6e 73 69 67 0, nCell, (unsig
1722a 6e 65 64 20 63 68 61 72 2a 29 70 43 65 6c 6c 4b ned char*)pCellK
1722b 65 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 ey, 0);.
1722c 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
1722d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
1722e 66 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a free(pCellKey);.
1722f 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f goto
17230 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a moveto_finish;.
17231 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
17232 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 c = sqlite
17233 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 3VdbeRecordCompa
17234 72 65 28 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c 4b re(nCell, pCellK
17235 65 79 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 ey, pIdxKey);.
17236 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
17237 66 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a free(pCellKey);.
17238 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
17239 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 }. if( c==0
1723a 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
1723b 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 pPage->intKey &&
1723c 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
1723d 0a 20 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d . lwr =
1723e 20 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 idx;.
1723f 75 70 72 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20 upr = lwr - 1;.
17240 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
17241 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
17242 20 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d *pRes =
17243 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 0;. rc
17244 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
17245 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 goto mov
17246 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 eto_finish;.
17247 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
17248 20 20 20 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 if( c<0 ){.
17249 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64 78 lwr = idx
1724a 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b +1;. }else{
1724b 0a 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 69 . upr = i
1724c 64 78 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 dx-1;. }.
1724d 20 20 20 20 69 66 28 20 6c 77 72 3e 75 70 72 20 if( lwr>upr
1724e 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b ){. break
1724f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
17250 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
17251 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 ->iPage] = (u16)
17252 28 28 6c 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20 ((lwr+upr)/2);.
17253 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
17254 20 6c 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 lwr==upr+1 );.
17255 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
17256 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 ->isInit );.
17257 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 if( pPage->leaf
17258 29 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 ){. chldPg
17259 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 = 0;. }else i
1725a 66 28 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e f( lwr>=pPage->n
1725b 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 Cell ){. ch
1725c 6c 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 ldPg = get4byte(
1725d 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 &pPage->aData[pP
1725e 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 age->hdrOffset+8
1725f 5d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ]);. }else{.
17260 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 chldPg = ge
17261 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 t4byte(findCell(
17262 70 50 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 pPage, lwr));.
17263 20 20 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64 }. if( chld
17264 50 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 Pg==0 ){. a
17265 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 ssert( pCur->aiI
17266 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c dx[pCur->iPage]<
17267 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
17268 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c r->iPage]->nCell
17269 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 );. *pRes
1726a 3d 20 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = c;. rc =
1726b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
1726c 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e goto moveto_fin
1726d 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 ish;. }. p
1726e 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
1726f 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 6c >iPage] = (u16)l
17270 77 72 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e wr;. pCur->in
17271 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
17272 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 pCur->validNKe
17273 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 y = 0;. rc =
17274 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 moveToChild(pCur
17275 2c 20 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 69 , chldPg);. i
17276 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76 f( rc ) goto mov
17277 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a eto_finish;. }.
17278 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 moveto_finish:.
17279 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a return rc;.}...
1727a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 /*.** Return TRU
1727b 45 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 E if the cursor
1727c 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 is not pointing
1727d 61 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 at an entry of t
1727e 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 he table..**.**
1727f 54 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65 74 TRUE will be ret
17280 75 72 6e 65 64 20 61 66 74 65 72 20 61 20 63 61 urned after a ca
17281 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 ll to sqlite3Btr
17282 65 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a eeNext() moves.*
17283 2a 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 * past the last
17284 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 entry in the tab
17285 6c 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74 72 le or sqlite3Btr
17286 65 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20 70 eePrev() moves p
17287 61 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 ast.** the first
17288 20 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69 73 entry. TRUE is
17289 20 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69 also returned i
1728a 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 f the table is e
1728b 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f mpty..*/.SQLITE_
1728c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1728d 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 te3BtreeEof(BtCu
1728e 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f rsor *pCur){. /
1728f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 20 * TODO: What if
17290 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e the cursor is in
17291 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 CURSOR_REQUIRES
17292 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 6c EEK but all tabl
17293 65 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68 e entries. ** h
17294 61 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64 ave been deleted
17295 3f 20 54 68 69 73 20 41 50 49 20 77 69 6c 6c 20 ? This API will
17296 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 need to change t
17297 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f o return an erro
17298 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77 r code. ** as w
17299 65 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c 65 ell as the boole
1729a 61 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 2e an result value.
1729b 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 . */. return (
1729c 43 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43 CURSOR_VALID!=pC
1729d 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a ur->eState);.}..
1729e 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 68 /*.** Advance th
1729f 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 e cursor to the
172a0 6e 65 78 74 20 65 6e 74 72 79 20 69 6e 20 74 68 next entry in th
172a1 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a e database. If.
172a2 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 ** successful th
172a3 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 en set *pRes=0.
172a4 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a If the cursor.*
172a5 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f * was already po
172a6 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 inting to the la
172a7 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 st entry in the
172a8 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a database before.
172a9 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ** this routine
172aa 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e was called, then
172ab 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f set *pRes=1..*/
172ac 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
172ad 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
172ae 4e 65 78 74 28 42 74 43 75 72 73 6f 72 20 2a 70 Next(BtCursor *p
172af 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b Cur, int *pRes){
172b0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 . int rc;. int
172b1 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 idx;. MemPage
172b2 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 *pPage;.. asser
172b3 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
172b4 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 tex(pCur) );. r
172b5 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f c = restoreCurso
172b6 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b rPosition(pCur);
172b7 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
172b8 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 E_OK ){. retu
172b9 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 rn rc;. }. ass
172ba 65 72 74 28 20 70 52 65 73 21 3d 30 20 29 3b 0a ert( pRes!=0 );.
172bb 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 if( CURSOR_INV
172bc 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 ALID==pCur->eSta
172bd 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 te ){. *pRes
172be 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = 1;. return
172bf 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
172c0 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e if( pCur->skipN
172c1 65 78 74 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 ext>0 ){. pCu
172c2 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b r->skipNext = 0;
172c3 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a . *pRes = 0;.
172c4 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
172c5 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 E_OK;. }. pCur
172c6 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a ->skipNext = 0;.
172c7 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d . pPage = pCur-
172c8 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
172c9 61 67 65 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b age];. idx = ++
172ca 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
172cb 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 ->iPage];. asse
172cc 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 rt( pPage->isIni
172cd 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 t );. assert( i
172ce 64 78 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c dx<=pPage->nCell
172cf 20 29 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 );.. pCur->inf
172d0 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 o.nSize = 0;. p
172d1 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d Cur->validNKey =
172d2 20 30 3b 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 0;. if( idx>=p
172d3 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 Page->nCell ){.
172d4 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c if( !pPage->l
172d5 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 eaf ){. rc
172d6 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 = moveToChild(pC
172d7 75 72 2c 20 67 65 74 34 62 79 74 65 28 26 70 50 ur, get4byte(&pP
172d8 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 age->aData[pPage
172d9 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 ->hdrOffset+8]))
172da 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
172db 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
172dc 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 rc = moveToLef
172dd 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 tmost(pCur);.
172de 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 *pRes = 0;.
172df 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
172e0 20 20 20 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20 }. do{.
172e1 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 if( pCur->iPa
172e2 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ge==0 ){.
172e3 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 *pRes = 1;.
172e4 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 pCur->eState
172e5 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 = CURSOR_INVALI
172e6 44 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 D;. retur
172e7 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 n SQLITE_OK;.
172e8 20 20 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 }. moveT
172e9 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 oParent(pCur);.
172ea 20 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 pPage = pCu
172eb 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
172ec 69 50 61 67 65 5d 3b 0a 20 20 20 20 7d 77 68 69 iPage];. }whi
172ed 6c 65 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b le( pCur->aiIdx[
172ee 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 50 pCur->iPage]>=pP
172ef 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 age->nCell );.
172f0 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 *pRes = 0;.
172f1 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b if( pPage->intK
172f2 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d ey ){. rc =
172f3 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 sqlite3BtreeNex
172f4 74 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 t(pCur, pRes);.
172f5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
172f6 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
172f7 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
172f8 20 72 63 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 rc;. }. *pRes
172f9 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61 67 = 0;. if( pPag
172fa 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 e->leaf ){. r
172fb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
172fc 0a 20 20 7d 0a 20 20 72 63 20 3d 20 6d 6f 76 65 . }. rc = move
172fd 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 ToLeftmost(pCur)
172fe 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
172ff 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74 68 .../*.** Step th
17300 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 e cursor to the
17301 62 61 63 6b 20 74 6f 20 74 68 65 20 70 72 65 76 back to the prev
17302 69 6f 75 73 20 65 6e 74 72 79 20 69 6e 20 74 68 ious entry in th
17303 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a e database. If.
17304 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 ** successful th
17305 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 en set *pRes=0.
17306 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a If the cursor.*
17307 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f * was already po
17308 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 66 69 inting to the fi
17309 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 rst entry in the
1730a 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 database before
1730b 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 .** this routine
1730c 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 was called, the
1730d 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a n set *pRes=1..*
1730e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1730f 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
17310 65 50 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 ePrevious(BtCurs
17311 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 or *pCur, int *p
17312 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Res){. int rc;.
17313 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
17314 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
17315 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
17316 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 ur) );. rc = re
17317 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 storeCursorPosit
17318 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 ion(pCur);. if(
17319 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1731a 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
1731b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 61 74 4c . }. pCur->atL
1731c 61 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 43 ast = 0;. if( C
1731d 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 URSOR_INVALID==p
1731e 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 Cur->eState ){.
1731f 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 *pRes = 1;.
17320 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
17321 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 OK;. }. if( pC
17322 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3c 30 20 29 ur->skipNext<0 )
17323 7b 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 {. pCur->skip
17324 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 2a 70 Next = 0;. *p
17325 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 Res = 0;. ret
17326 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
17327 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e }. pCur->skipN
17328 65 78 74 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 ext = 0;.. pPag
17329 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 e = pCur->apPage
1732a 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 [pCur->iPage];.
1732b 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
1732c 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 isInit );. if(
1732d 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
1732e 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 int idx = pC
1732f 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
17330 69 50 61 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d iPage];. rc =
17331 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 moveToChild(pCu
17332 72 2c 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 r, get4byte(find
17333 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 Cell(pPage, idx)
17334 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 ));. if( rc )
17335 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
17336 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 c;. }. rc
17337 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 = moveToRightmos
17338 74 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 t(pCur);. }else
17339 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 75 {. while( pCu
1733a 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
1733b 50 61 67 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 Page]==0 ){.
1733c 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 if( pCur->iPag
1733d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 e==0 ){.
1733e 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 pCur->eState = C
1733f 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 URSOR_INVALID;.
17340 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 *pRes = 1
17341 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
17342 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
17343 20 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f }. moveTo
17344 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 Parent(pCur);.
17345 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e }. pCur->in
17346 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
17347 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 pCur->validNKe
17348 79 20 3d 20 30 3b 0a 0a 20 20 20 20 70 43 75 72 y = 0;.. pCur
17349 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
1734a 61 67 65 5d 2d 2d 3b 0a 20 20 20 20 70 50 61 67 age]--;. pPag
1734b 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 e = pCur->apPage
1734c 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 [pCur->iPage];.
1734d 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e if( pPage->in
1734e 74 4b 65 79 20 26 26 20 21 70 50 61 67 65 2d 3e tKey && !pPage->
1734f 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 leaf ){. rc
17350 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 = sqlite3BtreeP
17351 72 65 76 69 6f 75 73 28 70 43 75 72 2c 20 70 52 revious(pCur, pR
17352 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a es);. }else{.
17353 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
17354 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a E_OK;. }. }.
17355 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 72 *pRes = 0;. r
17356 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
17357 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 ** Allocate a ne
17358 77 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 w page from the
17359 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
1735a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 70 61 67 *.** The new pag
1735b 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 e is marked as d
1735c 69 72 74 79 2e 20 20 28 49 6e 20 6f 74 68 65 72 irty. (In other
1735d 20 77 6f 72 64 73 2c 20 73 71 6c 69 74 65 33 50 words, sqlite3P
1735e 61 67 65 72 57 72 69 74 65 28 29 0a 2a 2a 20 68 agerWrite().** h
1735f 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
17360 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6e 65 called on the ne
17361 77 20 70 61 67 65 2e 29 20 20 54 68 65 20 6e 65 w page.) The ne
17362 77 20 70 61 67 65 20 68 61 73 20 61 6c 73 6f 0a w page has also.
17363 2a 2a 20 62 65 65 6e 20 72 65 66 65 72 65 6e 63 ** been referenc
17364 65 64 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 ed and the calli
17365 6e 67 20 72 6f 75 74 69 6e 65 20 69 73 20 72 65 ng routine is re
17366 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 61 sponsible for ca
17367 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 lling.** sqlite3
17368 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 PagerUnref() on
17369 74 68 65 20 6e 65 77 20 70 61 67 65 20 77 68 65 the new page whe
1736a 6e 20 69 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2a n it is done..**
1736b 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 .** SQLITE_OK is
1736c 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 returned on suc
1736d 63 65 73 73 2e 20 20 41 6e 79 20 6f 74 68 65 72 cess. Any other
1736e 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e return value in
1736f 64 69 63 61 74 65 73 0a 2a 2a 20 61 6e 20 65 72 dicates.** an er
17370 72 6f 72 2e 20 20 2a 70 70 50 61 67 65 20 61 6e ror. *ppPage an
17371 64 20 2a 70 50 67 6e 6f 20 61 72 65 20 75 6e 64 d *pPgno are und
17372 65 66 69 6e 65 64 20 69 6e 20 74 68 65 20 65 76 efined in the ev
17373 65 6e 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 2e ent of an error.
17374 0a 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b .** Do not invok
17375 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e e sqlite3PagerUn
17376 72 65 66 28 29 20 6f 6e 20 2a 70 70 50 61 67 65 ref() on *ppPage
17377 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 if an error is
17378 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
17379 49 66 20 74 68 65 20 22 6e 65 61 72 62 79 22 20 If the "nearby"
1737a 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 parameter is not
1737b 20 30 2c 20 74 68 65 6e 20 61 20 28 66 65 65 62 0, then a (feeb
1737c 6c 65 29 20 65 66 66 6f 72 74 20 69 73 20 6d 61 le) effort is ma
1737d 64 65 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 74 65 de to .** locate
1737e 20 61 20 70 61 67 65 20 63 6c 6f 73 65 20 74 6f a page close to
1737f 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
17380 20 22 6e 65 61 72 62 79 22 2e 20 20 54 68 69 73 "nearby". This
17381 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 can be used in
17382 61 6e 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f an.** attempt to
17383 20 6b 65 65 70 20 72 65 6c 61 74 65 64 20 70 61 keep related pa
17384 67 65 73 20 63 6c 6f 73 65 20 74 6f 20 65 61 63 ges close to eac
17385 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 64 h other in the d
17386 61 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a atabase file,.**
17387 20 77 68 69 63 68 20 69 6e 20 74 75 72 6e 20 63 which in turn c
17388 61 6e 20 6d 61 6b 65 20 64 61 74 61 62 61 73 65 an make database
17389 20 61 63 63 65 73 73 20 66 61 73 74 65 72 2e 0a access faster..
1738a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 65 78 **.** If the "ex
1738b 61 63 74 22 20 70 61 72 61 6d 65 74 65 72 20 69 act" parameter i
1738c 73 20 6e 6f 74 20 30 2c 20 61 6e 64 20 74 68 65 s not 0, and the
1738d 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 6e 65 61 page-number nea
1738e 72 62 79 20 65 78 69 73 74 73 20 0a 2a 2a 20 61 rby exists .** a
1738f 6e 79 77 68 65 72 65 20 6f 6e 20 74 68 65 20 66 nywhere on the f
17390 72 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e 20 69 ree-list, then i
17391 74 20 69 73 20 67 75 61 72 65 6e 74 65 65 64 20 t is guarenteed
17392 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 to be returned.
17393 54 68 69 73 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 This.** is only
17394 75 73 65 64 20 62 79 20 61 75 74 6f 2d 76 61 63 used by auto-vac
17395 75 75 6d 20 64 61 74 61 62 61 73 65 73 20 77 68 uum databases wh
17396 65 6e 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 en allocating a
17397 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 new table..*/.st
17398 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 atic int allocat
17399 65 42 74 72 65 65 50 61 67 65 28 0a 20 20 42 74 eBtreePage(. Bt
1739a 53 68 61 72 65 64 20 2a 70 42 74 2c 20 0a 20 20 Shared *pBt, .
1739b 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 MemPage **ppPage
1739c 2c 20 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f , . Pgno *pPgno
1739d 2c 20 0a 20 20 50 67 6e 6f 20 6e 65 61 72 62 79 , . Pgno nearby
1739e 2c 0a 20 20 75 38 20 65 78 61 63 74 0a 29 7b 0a ,. u8 exact.){.
1739f 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
173a0 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 1;. int rc;. u
173a1 33 32 20 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 32 n; /* Num
173a2 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 ber of pages on
173a3 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a the freelist */.
173a4 20 20 75 33 32 20 6b 3b 20 20 20 20 20 2f 2a 20 u32 k; /*
173a5 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 76 65 73 Number of leaves
173a6 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 6f 66 on the trunk of
173a7 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f the freelist */
173a8 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 . MemPage *pTru
173a9 6e 6b 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 nk = 0;. MemPag
173aa 65 20 2a 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 e *pPrevTrunk =
173ab 30 3b 0a 20 20 50 67 6e 6f 20 6d 78 50 61 67 65 0;. Pgno mxPage
173ac 3b 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 ; /* Total s
173ad 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
173ae 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61 ase file */.. a
173af 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
173b0 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
173b1 75 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 utex) );. pPage
173b2 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 1 = pBt->pPage1;
173b3 0a 20 20 6d 78 50 61 67 65 20 3d 20 70 61 67 65 . mxPage = page
173b4 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b rPagecount(pBt);
173b5 0a 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 . n = get4byte(
173b6 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
173b7 36 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 6]);. testcase(
173b8 20 6e 3d 3d 6d 78 50 61 67 65 2d 31 20 29 3b 0a n==mxPage-1 );.
173b9 20 20 69 66 28 20 6e 3e 3d 6d 78 50 61 67 65 20 if( n>=mxPage
173ba 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
173bb 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
173bc 54 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e 30 T;. }. if( n>0
173bd 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 ){. /* There
173be 20 61 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68 are pages on th
173bf 65 20 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75 e freelist. Reu
173c0 73 65 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 se one of those
173c1 70 61 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 pages. */. Pg
173c2 6e 6f 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 no iTrunk;. u
173c3 38 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 8 searchList = 0
173c4 3b 20 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65 ; /* If the free
173c5 2d 6c 69 73 74 20 6d 75 73 74 20 62 65 20 73 65 -list must be se
173c6 61 72 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72 arched for 'near
173c7 62 79 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 by' */. .
173c8 2f 2a 20 49 66 20 74 68 65 20 27 65 78 61 63 74 /* If the 'exact
173c9 27 20 70 61 72 61 6d 65 74 65 72 20 77 61 73 20 ' parameter was
173ca 74 72 75 65 20 61 6e 64 20 61 20 71 75 65 72 79 true and a query
173cb 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d of the pointer-
173cc 6d 61 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73 map. ** shows
173cd 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 27 that the page '
173ce 6e 65 61 72 62 79 27 20 69 73 20 73 6f 6d 65 77 nearby' is somew
173cf 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65 here on the free
173d0 2d 6c 69 73 74 2c 20 74 68 65 6e 0a 20 20 20 20 -list, then.
173d1 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 2d 6c 69 ** the entire-li
173d2 73 74 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63 st will be searc
173d3 68 65 64 20 66 6f 72 20 74 68 61 74 20 70 61 67 hed for that pag
173d4 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 e.. */.#ifnde
173d5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
173d6 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 TOVACUUM. if(
173d7 20 65 78 61 63 74 20 26 26 20 6e 65 61 72 62 79 exact && nearby
173d8 3c 3d 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 20 <=mxPage ){.
173d9 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20 u8 eType;.
173da 20 20 61 73 73 65 72 74 28 20 6e 65 61 72 62 79 assert( nearby
173db 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 >0 );. asse
173dc 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 rt( pBt->autoVac
173dd 75 75 6d 20 29 3b 0a 20 20 20 20 20 20 72 63 20 uum );. rc
173de 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c = ptrmapGet(pBt,
173df 20 6e 65 61 72 62 79 2c 20 26 65 54 79 70 65 2c nearby, &eType,
173e0 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 0);. if( r
173e1 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
173e2 20 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d if( eType==
173e3 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 PTRMAP_FREEPAGE
173e4 29 7b 0a 20 20 20 20 20 20 20 20 73 65 61 72 63 ){. searc
173e5 68 4c 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 hList = 1;.
173e6 20 7d 0a 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 }. *pPgno
173e7 3d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 7d 0a = nearby;. }.
173e8 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 #endif.. /* D
173e9 65 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72 65 ecrement the fre
173ea 65 2d 6c 69 73 74 20 63 6f 75 6e 74 20 62 79 20 e-list count by
173eb 31 2e 20 53 65 74 20 69 54 72 75 6e 6b 20 74 6f 1. Set iTrunk to
173ec 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 the index of th
173ed 65 0a 20 20 20 20 2a 2a 20 66 69 72 73 74 20 66 e. ** first f
173ee 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 ree-list trunk p
173ef 61 67 65 2e 20 69 50 72 65 76 54 72 75 6e 6b 20 age. iPrevTrunk
173f0 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 2e 0a is initially 1..
173f1 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 */. rc =
173f2 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
173f3 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 e(pPage1->pDbPag
173f4 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 e);. if( rc )
173f5 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
173f6 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31 put4byte(&pPage1
173f7 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 31 ->aData[36], n-1
173f8 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 );.. /* The c
173f9 6f 64 65 20 77 69 74 68 69 6e 20 74 68 69 73 20 ode within this
173fa 6c 6f 6f 70 20 69 73 20 72 75 6e 20 6f 6e 6c 79 loop is run only
173fb 20 6f 6e 63 65 20 69 66 20 74 68 65 20 27 73 65 once if the 'se
173fc 61 72 63 68 4c 69 73 74 27 20 76 61 72 69 61 62 archList' variab
173fd 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 le. ** is not
173fe 20 74 72 75 65 2e 20 4f 74 68 65 72 77 69 73 65 true. Otherwise
173ff 2c 20 69 74 20 72 75 6e 73 20 6f 6e 63 65 20 66 , it runs once f
17400 6f 72 20 65 61 63 68 20 74 72 75 6e 6b 2d 70 61 or each trunk-pa
17401 67 65 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a ge on the. **
17402 20 66 72 65 65 2d 6c 69 73 74 20 75 6e 74 69 6c free-list until
17403 20 74 68 65 20 70 61 67 65 20 27 6e 65 61 72 62 the page 'nearb
17404 79 27 20 69 73 20 6c 6f 63 61 74 65 64 2e 0a 20 y' is located..
17405 20 20 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 */. do {.
17406 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b 20 pPrevTrunk
17407 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 = pTrunk;.
17408 69 66 28 20 70 50 72 65 76 54 72 75 6e 6b 20 29 if( pPrevTrunk )
17409 7b 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b {. iTrunk
1740a 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 72 = get4byte(&pPr
1740b 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 evTrunk->aData[0
1740c 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ]);. }else{
1740d 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20 . iTrunk
1740e 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 = get4byte(&pPag
1740f 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a e1->aData[32]);.
17410 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74 65 }. te
17411 73 74 63 61 73 65 28 20 69 54 72 75 6e 6b 3d 3d stcase( iTrunk==
17412 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 mxPage );.
17413 69 66 28 20 69 54 72 75 6e 6b 3e 6d 78 50 61 67 if( iTrunk>mxPag
17414 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 e ){. rc
17415 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
17416 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 65 6c _BKPT;. }el
17417 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d se{. rc =
17418 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 btreeGetPage(pB
17419 74 2c 20 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 t, iTrunk, &pTru
1741a 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a nk, 0);. }.
1741b 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a if( rc ){.
1741c 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d pTrunk =
1741d 20 30 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 0;. goto
1741e 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 end_allocate_pa
1741f 67 65 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 ge;. }..
17420 20 20 20 6b 20 3d 20 67 65 74 34 62 79 74 65 28 k = get4byte(
17421 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 &pTrunk->aData[4
17422 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d ]);. if( k=
17423 3d 30 20 26 26 20 21 73 65 61 72 63 68 4c 69 73 =0 && !searchLis
17424 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 t ){. /*
17425 54 68 65 20 74 72 75 6e 6b 20 68 61 73 20 6e 6f The trunk has no
17426 20 6c 65 61 76 65 73 20 61 6e 64 20 74 68 65 20 leaves and the
17427 6c 69 73 74 20 69 73 20 6e 6f 74 20 62 65 69 6e list is not bein
17428 67 20 73 65 61 72 63 68 65 64 2e 20 0a 20 20 20 g searched. .
17429 20 20 20 20 20 2a 2a 20 53 6f 20 65 78 74 72 61 ** So extra
1742a 63 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 ct the trunk pag
1742b 65 20 69 74 73 65 6c 66 20 61 6e 64 20 75 73 65 e itself and use
1742c 20 69 74 20 61 73 20 74 68 65 20 6e 65 77 6c 79 it as the newly
1742d 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c . ** all
1742e 6f 63 61 74 65 64 20 70 61 67 65 20 2a 2f 0a 20 ocated page */.
1742f 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
17430 50 72 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a PrevTrunk==0 );.
17431 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
17432 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
17433 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b Trunk->pDbPage);
17434 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 . if( rc
17435 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 ){. got
17436 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 o end_allocate_p
17437 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 age;. }.
17438 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 *pPgno =
17439 69 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 iTrunk;.
1743a 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e memcpy(&pPage1->
1743b 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 aData[32], &pTru
1743c 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 nk->aData[0], 4)
1743d 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 67 ;. *ppPag
1743e 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 e = pTrunk;.
1743f 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a pTrunk = 0;.
17440 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 TRACE(("
17441 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 ALLOCATE: %d tru
17442 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 nk - %d free pag
17443 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 es left\n", *pPg
17444 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 no, n-1));.
17445 20 7d 65 6c 73 65 20 69 66 28 20 6b 3e 28 75 33 }else if( k>(u3
17446 32 29 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 2)(pBt->usableSi
17447 7a 65 2f 34 20 2d 20 32 29 20 29 7b 0a 20 20 20 ze/4 - 2) ){.
17448 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 /* Value of
17449 20 6b 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e k is out of ran
1744a 67 65 2e 20 20 44 61 74 61 62 61 73 65 20 63 6f ge. Database co
1744b 72 72 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 rruption */.
1744c 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1744d 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1744e 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 goto end_a
1744f 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 23 69 llocate_page;.#i
17450 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
17451 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
17452 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 65 61 }else if( sea
17453 72 63 68 4c 69 73 74 20 26 26 20 6e 65 61 72 62 rchList && nearb
17454 79 3d 3d 69 54 72 75 6e 6b 20 29 7b 0a 20 20 20 y==iTrunk ){.
17455 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 74 /* The list
17456 20 69 73 20 62 65 69 6e 67 20 73 65 61 72 63 68 is being search
17457 65 64 20 61 6e 64 20 74 68 69 73 20 74 72 75 6e ed and this trun
17458 6b 20 70 61 67 65 20 69 73 20 74 68 65 20 70 61 k page is the pa
17459 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f ge. ** to
1745a 20 61 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61 72 allocate, regar
1745b 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 dless of whether
1745c 20 69 74 20 68 61 73 20 6c 65 61 76 65 73 2e 0a it has leaves..
1745d 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1745e 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e assert( *pPgn
1745f 6f 3d 3d 69 54 72 75 6e 6b 20 29 3b 0a 20 20 20 o==iTrunk );.
17460 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 *ppPage = p
17461 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 73 Trunk;. s
17462 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 earchList = 0;.
17463 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
17464 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 te3PagerWrite(pT
17465 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a runk->pDbPage);.
17466 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 if( rc )
17467 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f {. goto
17468 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 end_allocate_pa
17469 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ge;. }.
1746a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 29 if( k==0 )
1746b 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
1746c 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 !pPrevTrunk ){.
1746d 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 memcp
1746e 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 y(&pPage1->aData
1746f 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 [32], &pTrunk->a
17470 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 Data[0], 4);.
17471 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
17472 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 memcpy
17473 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 (&pPrevTrunk->aD
17474 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d ata[0], &pTrunk-
17475 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 >aData[0], 4);.
17476 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
17477 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
17478 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b /* The trunk
17479 20 70 61 67 65 20 69 73 20 72 65 71 75 69 72 65 page is require
1747a 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20 d by the caller
1747b 62 75 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 but it contains
1747c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 6f . ** po
1747d 69 6e 74 65 72 73 20 74 6f 20 66 72 65 65 2d 6c inters to free-l
1747e 69 73 74 20 6c 65 61 76 65 73 2e 20 54 68 65 20 ist leaves. The
1747f 66 69 72 73 74 20 6c 65 61 66 20 62 65 63 6f 6d first leaf becom
17480 65 73 20 61 20 74 72 75 6e 6b 0a 20 20 20 20 20 es a trunk.
17481 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 ** page in
17482 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 this case..
17483 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
17484 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 54 MemPage *pNewT
17485 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 runk;.
17486 50 67 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20 3d Pgno iNewTrunk =
17487 20 67 65 74 34 62 79 74 65 28 26 70 54 72 75 6e get4byte(&pTrun
17488 6b 2d 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 k->aData[8]);.
17489 20 20 20 20 20 20 20 20 69 66 28 20 69 4e 65 77 if( iNew
1748a 54 72 75 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 20 Trunk>mxPage ){
1748b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 . rc
1748c 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
1748d 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 _BKPT;.
1748e 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f goto end_allo
1748f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 cate_page;.
17490 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
17491 20 74 65 73 74 63 61 73 65 28 20 69 4e 65 77 54 testcase( iNewT
17492 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a runk==mxPage );.
17493 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 rc = b
17494 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
17495 20 69 4e 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65 iNewTrunk, &pNe
17496 77 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 wTrunk, 0);.
17497 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
17498 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
17499 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
1749a 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 allocate_page;.
1749b 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1749c 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1749d 33 50 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 3PagerWrite(pNew
1749e 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b Trunk->pDbPage);
1749f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
174a0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
174a1 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 rele
174a2 61 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e asePage(pNewTrun
174a3 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 k);.
174a4 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 goto end_allocat
174a5 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 e_page;.
174a6 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d 65 }. me
174a7 6d 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d mcpy(&pNewTrunk-
174a8 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 >aData[0], &pTru
174a9 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 nk->aData[0], 4)
174aa 3b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 ;. put4
174ab 62 79 74 65 28 26 70 4e 65 77 54 72 75 6e 6b 2d byte(&pNewTrunk-
174ac 3e 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b >aData[4], k-1);
174ad 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 . memcp
174ae 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 y(&pNewTrunk->aD
174af 61 74 61 5b 38 5d 2c 20 26 70 54 72 75 6e 6b 2d ata[8], &pTrunk-
174b0 3e 61 44 61 74 61 5b 31 32 5d 2c 20 28 6b 2d 31 >aData[12], (k-1
174b1 29 2a 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 )*4);.
174b2 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 releasePage(pNew
174b3 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 Trunk);.
174b4 20 20 69 66 28 20 21 70 50 72 65 76 54 72 75 6e if( !pPrevTrun
174b5 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 k ){.
174b6 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
174b7 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
174b8 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 (pPage1->pDbPage
174b9 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ) );.
174ba 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 put4byte(&pPage
174bb 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e 1->aData[32], iN
174bc 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 ewTrunk);.
174bd 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
174be 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
174bf 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
174c0 72 65 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 revTrunk->pDbPag
174c1 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 e);.
174c2 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
174c3 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
174c4 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a _allocate_page;.
174c5 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
174c6 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 put4by
174c7 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e te(&pPrevTrunk->
174c8 61 44 61 74 61 5b 30 5d 2c 20 69 4e 65 77 54 72 aData[0], iNewTr
174c9 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 unk);.
174ca 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
174cb 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a pTrunk = 0;.
174cc 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 TRACE(("
174cd 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 ALLOCATE: %d tru
174ce 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 nk - %d free pag
174cf 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 es left\n", *pPg
174d0 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 69 no, n-1));.#endi
174d1 66 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 f. }else if
174d2 28 20 6b 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 ( k>0 ){.
174d3 20 2f 2a 20 45 78 74 72 61 63 74 20 61 20 6c 65 /* Extract a le
174d4 61 66 20 66 72 6f 6d 20 74 68 65 20 74 72 75 6e af from the trun
174d5 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 33 32 k */. u32
174d6 20 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20 20 closest;.
174d7 20 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20 Pgno iPage;.
174d8 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 unsigned c
174d9 68 61 72 20 2a 61 44 61 74 61 20 3d 20 70 54 72 har *aData = pTr
174da 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 unk->aData;.
174db 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
174dc 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e PagerWrite(pTrun
174dd 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 k->pDbPage);.
174de 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 if( rc ){.
174df 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e goto en
174e0 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b d_allocate_page;
174e1 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
174e2 20 20 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20 if( nearby>0
174e3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 32 ){. u32
174e4 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e i;. in
174e5 74 20 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20 t dist;.
174e6 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 closest = 0;.
174e7 20 20 20 20 20 20 20 20 20 64 69 73 74 20 3d 20 dist =
174e8 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 5b get4byte(&aData[
174e9 38 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20 8]) - nearby;.
174ea 20 20 20 20 20 20 20 20 69 66 28 20 64 69 73 74 if( dist
174eb 3c 30 20 29 20 64 69 73 74 20 3d 20 2d 64 69 73 <0 ) dist = -dis
174ec 74 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 t;. for
174ed 28 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b (i=1; i<k; i++){
174ee 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 . int
174ef 20 64 32 20 3d 20 67 65 74 34 62 79 74 65 28 26 d2 = get4byte(&
174f0 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20 2d 20 aData[8+i*4]) -
174f1 6e 65 61 72 62 79 3b 0a 20 20 20 20 20 20 20 20 nearby;.
174f2 20 20 20 20 69 66 28 20 64 32 3c 30 20 29 20 64 if( d2<0 ) d
174f3 32 20 3d 20 2d 64 32 3b 0a 20 20 20 20 20 20 20 2 = -d2;.
174f4 20 20 20 20 20 69 66 28 20 64 32 3c 64 69 73 74 if( d2<dist
174f5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
174f6 20 20 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20 closest = i;.
174f7 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69 73 dis
174f8 74 20 3d 20 64 32 3b 0a 20 20 20 20 20 20 20 20 t = d2;.
174f9 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
174fa 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
174fb 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 . close
174fc 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 st = 0;.
174fd 7d 0a 0a 20 20 20 20 20 20 20 20 69 50 61 67 65 }.. iPage
174fe 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 61 = get4byte(&aDa
174ff 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 29 ta[8+closest*4])
17500 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
17501 73 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 se( iPage==mxPag
17502 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 e );. if(
17503 20 69 50 61 67 65 3e 6d 78 50 61 67 65 20 29 7b iPage>mxPage ){
17504 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
17505 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
17506 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 KPT;. g
17507 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 oto end_allocate
17508 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d _page;. }
17509 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
1750a 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 e( iPage==mxPage
1750b 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 );. if(
1750c 21 73 65 61 72 63 68 4c 69 73 74 20 7c 7c 20 69 !searchList || i
1750d 50 61 67 65 3d 3d 6e 65 61 72 62 79 20 29 7b 0a Page==nearby ){.
1750e 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 6f int no
1750f 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 20 Content;.
17510 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 50 61 67 *pPgno = iPag
17511 65 3b 0a 20 20 20 20 20 20 20 20 20 20 54 52 41 e;. TRA
17512 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 CE(("ALLOCATE: %
17513 64 20 77 61 73 20 6c 65 61 66 20 25 64 20 6f 66 d was leaf %d of
17514 20 25 64 20 6f 6e 20 74 72 75 6e 6b 20 25 64 22 %d on trunk %d"
17515 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
17516 20 20 22 3a 20 25 64 20 6d 6f 72 65 20 66 72 65 ": %d more fre
17517 65 20 70 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20 e pages\n",.
17518 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 50 *pP
17519 67 6e 6f 2c 20 63 6c 6f 73 65 73 74 2b 31 2c 20 gno, closest+1,
1751a 6b 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 2c k, pTrunk->pgno,
1751b 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20 n-1));.
1751c 20 20 69 66 28 20 63 6c 6f 73 65 73 74 3c 6b 2d if( closest<k-
1751d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 1 ){.
1751e 20 6d 65 6d 63 70 79 28 26 61 44 61 74 61 5b 38 memcpy(&aData[8
1751f 2b 63 6c 6f 73 65 73 74 2a 34 5d 2c 20 26 61 44 +closest*4], &aD
17520 61 74 61 5b 34 2b 6b 2a 34 5d 2c 20 34 29 3b 0a ata[4+k*4], 4);.
17521 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
17522 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
17523 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a aData[4], k-1);.
17524 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
17525 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
17526 77 72 69 74 65 61 62 6c 65 28 70 54 72 75 6e 6b writeable(pTrunk
17527 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
17528 20 20 20 20 20 20 20 20 6e 6f 43 6f 6e 74 65 6e noConten
17529 74 20 3d 20 21 62 74 72 65 65 47 65 74 48 61 73 t = !btreeGetHas
1752a 43 6f 6e 74 65 6e 74 28 70 42 74 2c 20 2a 70 50 Content(pBt, *pP
1752b 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 gno);.
1752c 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
1752d 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 e(pBt, *pPgno, p
1752e 70 50 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 pPage, noContent
1752f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
17530 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
17531 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 {. rc
17532 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
17533 72 69 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e rite((*ppPage)->
17534 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
17535 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
17536 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
17537 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 release
17538 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 Page(*ppPage);.
17539 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1753a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1753b 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 searchList =
1753c 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 0;. }.
1753d 20 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 }. relea
1753e 73 65 50 61 67 65 28 70 50 72 65 76 54 72 75 6e sePage(pPrevTrun
1753f 6b 29 3b 0a 20 20 20 20 20 20 70 50 72 65 76 54 k);. pPrevT
17540 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 7d 77 runk = 0;. }w
17541 68 69 6c 65 28 20 73 65 61 72 63 68 4c 69 73 74 hile( searchList
17542 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 );. }else{.
17543 20 2f 2a 20 54 68 65 72 65 20 61 72 65 20 6e 6f /* There are no
17544 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 pages on the fr
17545 65 65 6c 69 73 74 2c 20 73 6f 20 63 72 65 61 74 eelist, so creat
17546 65 20 61 20 6e 65 77 20 70 61 67 65 20 61 74 20 e a new page at
17547 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f the. ** end o
17548 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 f the file */.
17549 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 70 61 int nPage = pa
1754a 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 gerPagecount(pBt
1754b 29 3b 0a 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 );. *pPgno =
1754c 6e 50 61 67 65 20 2b 20 31 3b 0a 0a 20 20 20 20 nPage + 1;..
1754d 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 if( *pPgno==PEND
1754e 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
1754f 74 29 20 29 7b 0a 20 20 20 20 20 20 28 2a 70 50 t) ){. (*pP
17550 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23 gno)++;. }..#
17551 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
17552 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
17553 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 if( pBt->autoV
17554 61 63 75 75 6d 20 26 26 20 50 54 52 4d 41 50 5f acuum && PTRMAP_
17555 49 53 50 41 47 45 28 70 42 74 2c 20 2a 70 50 67 ISPAGE(pBt, *pPg
17556 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 no) ){. /*
17557 49 66 20 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 If *pPgno refers
17558 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 to a pointer-ma
17559 70 20 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 p page, allocate
1755a 20 74 77 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 two new pages.
1755b 20 20 20 20 20 2a 2a 20 61 74 20 74 68 65 20 65 ** at the e
1755c 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 nd of the file i
1755d 6e 73 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 nstead of one. T
1755e 68 65 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 he first allocat
1755f 65 64 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a ed page. **
17560 20 62 65 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 becomes a new p
17561 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c ointer-map page,
17562 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 75 the second is u
17563 73 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 sed by the calle
17564 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 r.. */.
17565 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 20 3d MemPage *pPg =
17566 20 30 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 0;. TRACE(
17567 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 ("ALLOCATE: %d f
17568 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 20 rom end of file
17569 28 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 (pointer-map pag
1756a 65 29 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b e)\n", *pPgno));
1756b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2a . assert( *
1756c 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 pPgno!=PENDING_B
1756d 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b YTE_PAGE(pBt) );
1756e 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 . rc = btre
1756f 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70 eGetPage(pBt, *p
17570 50 67 6e 6f 2c 20 26 70 50 67 2c 20 30 29 3b 0a Pgno, &pPg, 0);.
17571 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
17572 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
17573 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
17574 61 67 65 72 57 72 69 74 65 28 70 50 67 2d 3e 70 agerWrite(pPg->p
17575 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
17576 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 67 releasePage(pPg
17577 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
17578 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
17579 20 72 63 3b 0a 20 20 20 20 20 20 28 2a 70 50 67 rc;. (*pPg
1757a 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 no)++;. if(
1757b 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 *pPgno==PENDING
1757c 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
1757d 29 7b 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 20 7d ){ (*pPgno)++; }
1757e 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 . }.#endif..
1757f 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e assert( *pPgn
17580 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f o!=PENDING_BYTE_
17581 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20 PAGE(pBt) );.
17582 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 rc = btreeGetPa
17583 67 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 ge(pBt, *pPgno,
17584 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 ppPage, 0);.
17585 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
17586 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c rc;. rc = sql
17587 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 28 ite3PagerWrite((
17588 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 *ppPage)->pDbPag
17589 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
1758a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1758b 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a releasePage(*
1758c 70 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 ppPage);. }.
1758d 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 TRACE(("ALLOC
1758e 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 ATE: %d from end
1758f 20 6f 66 20 66 69 6c 65 5c 6e 22 2c 20 2a 70 50 of file\n", *pP
17590 67 6e 6f 29 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 gno));. }.. as
17591 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 sert( *pPgno!=PE
17592 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
17593 70 42 74 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c pBt) );..end_all
17594 6f 63 61 74 65 5f 70 61 67 65 3a 0a 20 20 72 65 ocate_page:. re
17595 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b leasePage(pTrunk
17596 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 );. releasePage
17597 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20 (pPrevTrunk);.
17598 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
17599 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c K ){. if( sql
1759a 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 ite3PagerPageRef
1759b 63 6f 75 6e 74 28 28 2a 70 70 50 61 67 65 29 2d count((*ppPage)-
1759c 3e 70 44 62 50 61 67 65 29 3e 31 20 29 7b 0a 20 >pDbPage)>1 ){.
1759d 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
1759e 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 (*ppPage);.
1759f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
175a0 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
175a1 20 7d 0a 20 20 20 20 28 2a 70 70 50 61 67 65 29 }. (*ppPage)
175a2 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 ->isInit = 0;.
175a3 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 50 61 }else{. *ppPa
175a4 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 ge = 0;. }. re
175a5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
175a6 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
175a7 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 70 is used to add p
175a8 61 67 65 20 69 50 61 67 65 20 74 6f 20 74 68 65 age iPage to the
175a9 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 database file f
175aa 72 65 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74 ree-list. .** It
175ab 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 is assumed that
175ac 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
175ad 20 61 6c 72 65 61 64 79 20 61 20 70 61 72 74 20 already a part
175ae 6f 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 of the free-list
175af 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 ..**.** The valu
175b0 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
175b1 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
175b2 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
175b3 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a is optional..**
175b4 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 68 If the caller h
175b5 61 70 70 65 6e 73 20 74 6f 20 68 61 76 65 20 61 appens to have a
175b6 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
175b7 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 0a MemPage object .
175b8 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ** corresponding
175b9 20 74 6f 20 70 61 67 65 20 69 50 61 67 65 20 68 to page iPage h
175ba 61 6e 64 79 2c 20 69 74 20 6d 61 79 20 70 61 73 andy, it may pas
175bb 73 20 69 74 20 61 73 20 74 68 65 20 73 65 63 6f s it as the seco
175bc 6e 64 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74 nd value. .** Ot
175bd 68 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 20 herwise, it may
175be 70 61 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a pass NULL..**.**
175bf 20 49 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f If a pointer to
175c0 20 61 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 a MemPage objec
175c1 74 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 t is passed as t
175c2 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
175c3 6e 74 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 72 nt,.** its refer
175c4 65 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f ence count is no
175c5 74 20 61 6c 74 65 72 65 64 20 62 79 20 74 68 69 t altered by thi
175c6 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 s function..*/.s
175c7 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 50 61 tatic int freePa
175c8 67 65 32 28 42 74 53 68 61 72 65 64 20 2a 70 42 ge2(BtShared *pB
175c9 74 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d t, MemPage *pMem
175ca 50 61 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 65 Page, Pgno iPage
175cb 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 ){. MemPage *pT
175cc 72 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 runk = 0;
175cd 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 /* Free
175ce 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 -list trunk page
175cf 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e */. Pgno iTrun
175d0 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 k = 0;
175d1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
175d2 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 e number of free
175d3 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 -list trunk page
175d4 20 2a 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a */ . MemPage *
175d5 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 pPage1 = pBt->pP
175d6 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f age1; /* Lo
175d7 63 61 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f cal reference to
175d8 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d page 1 */. Mem
175d9 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 Page *pPage;
175da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
175db 20 2f 2a 20 50 61 67 65 20 62 65 69 6e 67 20 66 /* Page being f
175dc 72 65 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c reed. May be NUL
175dd 4c 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 L. */. int rc;
175de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
175df 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
175e0 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 eturn Code */.
175e1 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20 int nFree;
175e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
175e3 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e /* Initial n
175e4 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f umber of pages o
175e5 6e 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a n free-list */..
175e6 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
175e7 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
175e8 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
175e9 73 65 72 74 28 20 69 50 61 67 65 3e 31 20 29 3b sert( iPage>1 );
175ea 0a 20 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d . assert( !pMem
175eb 50 61 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65 Page || pMemPage
175ec 2d 3e 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b ->pgno==iPage );
175ed 0a 0a 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65 .. if( pMemPage
175ee 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 ){. pPage =
175ef 70 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71 pMemPage;. sq
175f0 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70 50 lite3PagerRef(pP
175f1 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 age->pDbPage);.
175f2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 }else{. pPag
175f3 65 20 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f e = btreePageLoo
175f4 6b 75 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b kup(pBt, iPage);
175f5 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 . }.. /* Incre
175f6 6d 65 6e 74 20 74 68 65 20 66 72 65 65 20 70 61 ment the free pa
175f7 67 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67 ge count on pPag
175f8 65 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c e1 */. rc = sql
175f9 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
175fa 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b Page1->pDbPage);
175fb 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f . if( rc ) goto
175fc 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 freepage_out;.
175fd 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 nFree = get4byt
175fe 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
175ff 5b 33 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 [36]);. put4byt
17600 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
17601 5b 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a [36], nFree+1);.
17602 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 .#ifdef SQLITE_S
17603 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f ECURE_DELETE. /
17604 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f * If the SQLITE_
17605 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 63 6f SECURE_DELETE co
17606 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f mpile-time optio
17607 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 n is enabled, th
17608 65 6e 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20 66 en. ** always f
17609 75 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 20 64 ully overwrite d
1760a 65 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 eleted informati
1760b 6f 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 on with zeros..
1760c 20 2a 2f 0a 20 20 69 66 28 20 28 21 70 50 61 67 */. if( (!pPag
1760d 65 20 26 26 20 28 72 63 20 3d 20 62 74 72 65 65 e && (rc = btree
1760e 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 50 61 GetPage(pBt, iPa
1760f 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 29 ge, &pPage, 0)))
17610 0a 20 20 20 7c 7c 20 20 20 20 20 20 20 20 20 20 . ||
17611 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 (rc = sqlite3P
17612 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d agerWrite(pPage-
17613 3e 70 44 62 50 61 67 65 29 29 0a 20 20 29 7b 0a >pDbPage)). ){.
17614 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 goto freepag
17615 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d e_out;. }. mem
17616 73 65 74 28 70 50 61 67 65 2d 3e 61 44 61 74 61 set(pPage->aData
17617 2c 20 30 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d , 0, pPage->pBt-
17618 3e 70 61 67 65 53 69 7a 65 29 3b 0a 23 65 6e 64 >pageSize);.#end
17619 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 if.. /* If the
1761a 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 database support
1761b 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77 s auto-vacuum, w
1761c 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e rite an entry in
1761d 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 the pointer-map
1761e 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 . ** to indicat
1761f 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 e that the page
17620 69 73 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 is free.. */.
17621 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d if( ISAUTOVACUUM
17622 20 29 7b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 ){. ptrmapPu
17623 74 28 70 42 74 2c 20 69 50 61 67 65 2c 20 50 54 t(pBt, iPage, PT
17624 52 4d 41 50 5f 46 52 45 45 50 41 47 45 2c 20 30 RMAP_FREEPAGE, 0
17625 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 , &rc);. if(
17626 72 63 20 29 20 67 6f 74 6f 20 66 72 65 65 70 61 rc ) goto freepa
17627 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f ge_out;. }.. /
17628 2a 20 4e 6f 77 20 6d 61 6e 69 70 75 6c 61 74 65 * Now manipulate
17629 20 74 68 65 20 61 63 74 75 61 6c 20 64 61 74 61 the actual data
1762a 62 61 73 65 20 66 72 65 65 2d 6c 69 73 74 20 73 base free-list s
1762b 74 72 75 63 74 75 72 65 2e 20 54 68 65 72 65 20 tructure. There
1762c 61 72 65 20 74 77 6f 0a 20 20 2a 2a 20 70 6f 73 are two. ** pos
1762d 73 69 62 69 6c 69 74 69 65 73 2e 20 49 66 20 74 sibilities. If t
1762e 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 he free-list is
1762f 63 75 72 72 65 6e 74 6c 79 20 65 6d 70 74 79 2c currently empty,
17630 20 6f 72 20 69 66 20 74 68 65 20 66 69 72 73 74 or if the first
17631 0a 20 20 2a 2a 20 74 72 75 6e 6b 20 70 61 67 65 . ** trunk page
17632 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 in the free-lis
17633 74 20 69 73 20 66 75 6c 6c 2c 20 74 68 65 6e 20 t is full, then
17634 74 68 69 73 20 70 61 67 65 20 77 69 6c 6c 20 62 this page will b
17635 65 63 6f 6d 65 20 61 0a 20 20 2a 2a 20 6e 65 77 ecome a. ** new
17636 20 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b free-list trunk
17637 20 70 61 67 65 2e 20 4f 74 68 65 72 77 69 73 65 page. Otherwise
17638 2c 20 69 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 , it will become
17639 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65 0a 20 a leaf of the.
1763a 20 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 ** first trunk
1763b 70 61 67 65 20 69 6e 20 74 68 65 20 63 75 72 72 page in the curr
1763c 65 6e 74 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 ent free-list. T
1763d 68 69 73 20 62 6c 6f 63 6b 20 74 65 73 74 73 20 his block tests
1763e 69 66 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f if it. ** is po
1763f 73 73 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68 ssible to add th
17640 65 20 70 61 67 65 20 61 73 20 61 20 6e 65 77 20 e page as a new
17641 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 2e 0a free-list leaf..
17642 20 20 2a 2f 0a 20 20 69 66 28 20 6e 46 72 65 65 */. if( nFree
17643 21 3d 30 20 29 7b 0a 20 20 20 20 75 33 32 20 6e !=0 ){. u32 n
17644 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 Leaf;
17645 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 /* Initial
17646 6e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 63 number of leaf c
17647 65 6c 6c 73 20 6f 6e 20 74 72 75 6e 6b 20 70 61 ells on trunk pa
17648 67 65 20 2a 2f 0a 0a 20 20 20 20 69 54 72 75 6e ge */.. iTrun
17649 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 k = get4byte(&pP
1764a 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 age1->aData[32])
1764b 3b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 ;. rc = btree
1764c 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 GetPage(pBt, iTr
1764d 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 unk, &pTrunk, 0)
1764e 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
1764f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
17650 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f goto freepage_o
17651 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e ut;. }.. n
17652 4c 65 61 66 20 3d 20 67 65 74 34 62 79 74 65 28 Leaf = get4byte(
17653 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 &pTrunk->aData[4
17654 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ]);. assert(
17655 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3e pBt->usableSize>
17656 33 32 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 4c 32 );. if( nL
17657 65 61 66 20 3e 20 28 75 33 32 29 70 42 74 2d 3e eaf > (u32)pBt->
17658 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 usableSize/4 - 2
17659 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
1765a 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
1765b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 PT;. goto f
1765c 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 reepage_out;.
1765d 20 7d 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66 }. if( nLeaf
1765e 20 3c 20 28 75 33 32 29 70 42 74 2d 3e 75 73 61 < (u32)pBt->usa
1765f 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 29 7b bleSize/4 - 8 ){
17660 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 . /* In thi
17661 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 s case there is
17662 72 6f 6f 6d 20 6f 6e 20 74 68 65 20 74 72 75 6e room on the trun
17663 6b 20 70 61 67 65 20 74 6f 20 69 6e 73 65 72 74 k page to insert
17664 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 the page.
17665 2a 2a 20 62 65 69 6e 67 20 66 72 65 65 64 20 61 ** being freed a
17666 73 20 61 20 6e 65 77 20 6c 65 61 66 2e 0a 20 20 s a new leaf..
17667 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
17668 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 74 72 Note that the tr
17669 75 6e 6b 20 70 61 67 65 20 69 73 20 6e 6f 74 20 unk page is not
1766a 72 65 61 6c 6c 79 20 66 75 6c 6c 20 75 6e 74 69 really full unti
1766b 6c 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 l it contains.
1766c 20 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a ** usableSiz
1766d 65 2f 34 20 2d 20 32 20 65 6e 74 72 69 65 73 2c e/4 - 2 entries,
1766e 20 6e 6f 74 20 75 73 61 62 6c 65 53 69 7a 65 2f not usableSize/
1766f 34 20 2d 20 38 20 65 6e 74 72 69 65 73 20 61 73 4 - 8 entries as
17670 20 77 65 20 68 61 76 65 0a 20 20 20 20 20 20 2a we have. *
17671 2a 20 63 6f 64 65 64 2e 20 20 42 75 74 20 64 75 * coded. But du
17672 65 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 e to a coding er
17673 72 6f 72 20 69 6e 20 76 65 72 73 69 6f 6e 73 20 ror in versions
17674 6f 66 20 53 51 4c 69 74 65 20 70 72 69 6f 72 20 of SQLite prior
17675 74 6f 0a 20 20 20 20 20 20 2a 2a 20 33 2e 36 2e to. ** 3.6.
17676 30 2c 20 64 61 74 61 62 61 73 65 73 20 77 69 74 0, databases wit
17677 68 20 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b h freelist trunk
17678 20 70 61 67 65 73 20 68 6f 6c 64 69 6e 67 20 6d pages holding m
17679 6f 72 65 20 74 68 61 6e 0a 20 20 20 20 20 20 2a ore than. *
1767a 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d * usableSize/4 -
1767b 20 38 20 65 6e 74 72 69 65 73 20 77 69 6c 6c 20 8 entries will
1767c 62 65 20 72 65 70 6f 72 74 65 64 20 61 73 20 63 be reported as c
1767d 6f 72 72 75 70 74 2e 20 20 49 6e 20 6f 72 64 65 orrupt. In orde
1767e 72 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 r. ** to ma
1767f 69 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64 73 intain backwards
17680 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 compatibility w
17681 69 74 68 20 6f 6c 64 65 72 20 76 65 72 73 69 6f ith older versio
17682 6e 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a 20 20 ns of SQLite,.
17683 20 20 20 20 2a 2a 20 77 65 20 77 69 6c 6c 20 63 ** we will c
17684 6f 6e 74 69 6e 75 65 20 74 6f 20 72 65 73 74 72 ontinue to restr
17685 69 63 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ict the number o
17686 66 20 65 6e 74 72 69 65 73 20 74 6f 20 75 73 61 f entries to usa
17687 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 0a 20 20 bleSize/4 - 8.
17688 20 20 20 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e 20 ** for now.
17689 20 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 At some point i
1768a 6e 20 74 68 65 20 66 75 74 75 72 65 20 28 6f 6e n the future (on
1768b 63 65 20 65 76 65 72 79 6f 6e 65 20 68 61 73 20 ce everyone has
1768c 75 70 67 72 61 64 65 64 0a 20 20 20 20 20 20 2a upgraded. *
1768d 2a 20 74 6f 20 33 2e 36 2e 30 20 6f 72 20 6c 61 * to 3.6.0 or la
1768e 74 65 72 29 20 77 65 20 73 68 6f 75 6c 64 20 63 ter) we should c
1768f 6f 6e 73 69 64 65 72 20 66 69 78 69 6e 67 20 74 onsider fixing t
17690 68 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 61 he conditional a
17691 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f bove. ** to
17692 20 72 65 61 64 20 22 75 73 61 62 6c 65 53 69 7a read "usableSiz
17693 65 2f 34 2d 32 22 20 69 6e 73 74 65 61 64 20 6f e/4-2" instead o
17694 66 20 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d f "usableSize/4-
17695 38 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 8".. */.
17696 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
17697 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b agerWrite(pTrunk
17698 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
17699 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1769a 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 _OK ){. p
1769b 75 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d ut4byte(&pTrunk-
1769c 3e 61 44 61 74 61 5b 34 5d 2c 20 6e 4c 65 61 66 >aData[4], nLeaf
1769d 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 +1);. put
1769e 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 4byte(&pTrunk->a
1769f 44 61 74 61 5b 38 2b 6e 4c 65 61 66 2a 34 5d 2c Data[8+nLeaf*4],
176a0 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 66 iPage);.#ifndef
176a1 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 SQLITE_SECURE_D
176a2 45 4c 45 54 45 0a 20 20 20 20 20 20 20 20 69 66 ELETE. if
176a3 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 ( pPage ){.
176a4 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
176a5 72 44 6f 6e 74 57 72 69 74 65 28 70 50 61 67 65 rDontWrite(pPage
176a6 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
176a7 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
176a8 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 53 rc = btreeS
176a9 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 etHasContent(pBt
176aa 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 , iPage);.
176ab 7d 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22 }. TRACE(("
176ac 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6c 65 FREE-PAGE: %d le
176ad 61 66 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 65 af on trunk page
176ae 20 25 64 5c 6e 22 2c 70 50 61 67 65 2d 3e 70 67 %d\n",pPage->pg
176af 6e 6f 2c 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 no,pTrunk->pgno)
176b0 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 );. goto fr
176b1 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 eepage_out;.
176b2 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 63 }. }.. /* If c
176b3 6f 6e 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f 20 ontrol flows to
176b4 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e this point, then
176b5 20 69 74 20 77 61 73 20 6e 6f 74 20 70 6f 73 73 it was not poss
176b6 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65 0a ible to add the.
176b7 20 20 2a 2a 20 74 68 65 20 70 61 67 65 20 62 65 ** the page be
176b8 69 6e 67 20 66 72 65 65 64 20 61 73 20 61 20 6c ing freed as a l
176b9 65 61 66 20 70 61 67 65 20 6f 66 20 74 68 65 20 eaf page of the
176ba 66 69 72 73 74 20 74 72 75 6e 6b 20 69 6e 20 74 first trunk in t
176bb 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 he free-list..
176bc 2a 2a 20 50 6f 73 73 69 62 6c 79 20 62 65 63 61 ** Possibly beca
176bd 75 73 65 20 74 68 65 20 66 72 65 65 2d 6c 69 73 use the free-lis
176be 74 20 69 73 20 65 6d 70 74 79 2c 20 6f 72 20 70 t is empty, or p
176bf 6f 73 73 69 62 6c 79 20 62 65 63 61 75 73 65 20 ossibly because
176c0 74 68 65 20 0a 20 20 2a 2a 20 66 69 72 73 74 20 the . ** first
176c1 74 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 trunk in the fre
176c2 65 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c 2e 20 e-list is full.
176c3 45 69 74 68 65 72 20 77 61 79 2c 20 74 68 65 20 Either way, the
176c4 70 61 67 65 20 62 65 69 6e 67 20 66 72 65 65 64 page being freed
176c5 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 63 6f 6d . ** will becom
176c6 65 20 74 68 65 20 6e 65 77 20 66 69 72 73 74 20 e the new first
176c7 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 trunk page in th
176c8 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a e free-list.. *
176c9 2f 0a 20 20 69 66 28 20 70 50 61 67 65 3d 3d 30 /. if( pPage==0
176ca 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 && SQLITE_OK!=(
176cb 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
176cc 65 28 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 e(pBt, iPage, &p
176cd 50 61 67 65 2c 20 30 29 29 20 29 7b 0a 20 20 20 Page, 0)) ){.
176ce 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f goto freepage_o
176cf 75 74 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 ut;. }. rc = s
176d0 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
176d1 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
176d2 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
176d3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 TE_OK ){. got
176d4 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a o freepage_out;.
176d5 20 20 7d 0a 20 20 70 75 74 34 62 79 74 65 28 70 }. put4byte(p
176d6 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 72 Page->aData, iTr
176d7 75 6e 6b 29 3b 0a 20 20 70 75 74 34 62 79 74 65 unk);. put4byte
176d8 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 (&pPage->aData[4
176d9 5d 2c 20 30 29 3b 0a 20 20 70 75 74 34 62 79 74 ], 0);. put4byt
176da 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
176db 5b 33 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 [32], iPage);.
176dc 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 TRACE(("FREE-PAG
176dd 45 3a 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 E: %d new trunk
176de 70 61 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25 page replacing %
176df 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e d\n", pPage->pgn
176e0 6f 2c 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72 o, iTrunk));..fr
176e1 65 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66 eepage_out:. if
176e2 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 ( pPage ){. p
176e3 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 Page->isInit = 0
176e4 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 ;. }. releaseP
176e5 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 age(pPage);. re
176e6 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b leasePage(pTrunk
176e7 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
176e8 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 }.static void fr
176e9 65 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a eePage(MemPage *
176ea 70 50 61 67 65 2c 20 69 6e 74 20 2a 70 52 43 29 pPage, int *pRC)
176eb 7b 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d {. if( (*pRC)==
176ec 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
176ed 20 2a 70 52 43 20 3d 20 66 72 65 65 50 61 67 65 *pRC = freePage
176ee 32 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 50 2(pPage->pBt, pP
176ef 61 67 65 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f age, pPage->pgno
176f0 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
176f1 46 72 65 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f Free any overflo
176f2 77 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 w pages associat
176f3 65 64 20 77 69 74 68 20 74 68 65 20 67 69 76 65 ed with the give
176f4 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 n Cell..*/.stati
176f5 63 20 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c 28 c int clearCell(
176f6 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
176f7 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
176f8 43 65 6c 6c 29 7b 0a 20 20 42 74 53 68 61 72 65 Cell){. BtShare
176f9 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e d *pBt = pPage->
176fa 70 42 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 pBt;. CellInfo
176fb 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76 66 info;. Pgno ovf
176fc 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 3b lPgno;. int rc;
176fd 0a 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 20 . int nOvfl;.
176fe 75 31 36 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 u16 ovflPageSize
176ff 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
17700 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
17701 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
17702 78 29 20 29 3b 0a 20 20 62 74 72 65 65 50 61 72 x) );. btreePar
17703 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c seCellPtr(pPage,
17704 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a pCell, &info);.
17705 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 if( info.iOver
17706 66 6c 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20 72 flow==0 ){. r
17707 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
17708 20 20 2f 2a 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 /* No overflow
17709 20 70 61 67 65 73 2e 20 52 65 74 75 72 6e 20 77 pages. Return w
1770a 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 ithout doing any
1770b 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 6f thing */. }. o
1770c 76 66 6c 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 vflPgno = get4by
1770d 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 te(&pCell[info.i
1770e 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 61 73 Overflow]);. as
1770f 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c sert( pBt->usabl
17710 65 53 69 7a 65 20 3e 20 34 20 29 3b 0a 20 20 6f eSize > 4 );. o
17711 76 66 6c 50 61 67 65 53 69 7a 65 20 3d 20 70 42 vflPageSize = pB
17712 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 t->usableSize -
17713 34 3b 0a 20 20 6e 4f 76 66 6c 20 3d 20 28 69 6e 4;. nOvfl = (in
17714 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 2d 20 69 6e fo.nPayload - in
17715 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66 6c fo.nLocal + ovfl
17716 50 61 67 65 53 69 7a 65 20 2d 20 31 29 2f 6f 76 PageSize - 1)/ov
17717 66 6c 50 61 67 65 53 69 7a 65 3b 0a 20 20 61 73 flPageSize;. as
17718 73 65 72 74 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d sert( ovflPgno==
17719 30 20 7c 7c 20 6e 4f 76 66 6c 3e 30 20 29 3b 0a 0 || nOvfl>0 );.
1771a 20 20 77 68 69 6c 65 28 20 6e 4f 76 66 6c 2d 2d while( nOvfl--
1771b 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 69 4e 65 ){. Pgno iNe
1771c 78 74 20 3d 20 30 3b 0a 20 20 20 20 4d 65 6d 50 xt = 0;. MemP
1771d 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a age *pOvfl = 0;.
1771e 20 20 20 20 69 66 28 20 6f 76 66 6c 50 67 6e 6f if( ovflPgno
1771f 3c 32 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 70 <2 || ovflPgno>p
17720 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 agerPagecount(pB
17721 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 30 t) ){. /* 0
17722 20 69 73 20 6e 6f 74 20 61 20 6c 65 67 61 6c 20 is not a legal
17723 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 page number and
17724 70 61 67 65 20 31 20 63 61 6e 6e 6f 74 20 62 65 page 1 cannot be
17725 20 61 6e 20 0a 20 20 20 20 20 20 2a 2a 20 6f 76 an . ** ov
17726 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 54 68 65 erflow page. The
17727 72 65 66 6f 72 65 20 69 66 20 6f 76 66 6c 50 67 refore if ovflPg
17728 6e 6f 3c 32 20 6f 72 20 70 61 73 74 20 74 68 65 no<2 or past the
17729 20 65 6e 64 20 6f 66 20 74 68 65 20 0a 20 20 20 end of the .
1772a 20 20 20 2a 2a 20 66 69 6c 65 20 74 68 65 20 64 ** file the d
1772b 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 atabase must be
1772c 63 6f 72 72 75 70 74 2e 20 2a 2f 0a 20 20 20 20 corrupt. */.
1772d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1772e 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1772f 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4f 76 66 }. if( nOvf
17730 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 l ){. rc =
17731 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 getOverflowPage(
17732 70 42 74 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 26 pBt, ovflPgno, &
17733 70 4f 76 66 6c 2c 20 26 69 4e 65 78 74 29 3b 0a pOvfl, &iNext);.
17734 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 if( rc ) r
17735 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
17736 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 rc = freePag
17737 65 32 28 70 42 74 2c 20 70 4f 76 66 6c 2c 20 6f e2(pBt, pOvfl, o
17738 76 66 6c 50 67 6e 6f 29 3b 0a 20 20 20 20 69 66 vflPgno);. if
17739 28 20 70 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20 ( pOvfl ){.
1773a 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
1773b 65 66 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67 ef(pOvfl->pDbPag
1773c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 e);. }. if
1773d 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
1773e 3b 0a 20 20 20 20 6f 76 66 6c 50 67 6e 6f 20 3d ;. ovflPgno =
1773f 20 69 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 iNext;. }. re
17740 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
17741 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 }../*.** Create
17742 74 68 65 20 62 79 74 65 20 73 65 71 75 65 6e 63 the byte sequenc
17743 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 e used to repres
17744 65 6e 74 20 61 20 63 65 6c 6c 20 6f 6e 20 70 61 ent a cell on pa
17745 67 65 20 70 50 61 67 65 0a 2a 2a 20 61 6e 64 20 ge pPage.** and
17746 77 72 69 74 65 20 74 68 61 74 20 62 79 74 65 20 write that byte
17747 73 65 71 75 65 6e 63 65 20 69 6e 74 6f 20 70 43 sequence into pC
17748 65 6c 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f 77 ell[]. Overflow
17749 20 70 61 67 65 73 20 61 72 65 0a 2a 2a 20 61 6c pages are.** al
1774a 6c 6f 63 61 74 65 64 20 61 6e 64 20 66 69 6c 6c located and fill
1774b 65 64 20 69 6e 20 61 73 20 6e 65 63 65 73 73 61 ed in as necessa
1774c 72 79 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 ry. The calling
1774d 20 70 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73 procedure.** is
1774e 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 responsible for
1774f 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 73 75 66 making sure suf
17750 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 68 61 ficient space ha
17751 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 s been allocated
17752 0a 2a 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e .** for pCell[].
17753 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
17754 20 70 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 pCell does not
17755 6e 65 63 65 73 73 61 72 79 20 6e 65 65 64 20 74 necessary need t
17756 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 70 o point to the p
17757 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 61 Page->aData.** a
17758 72 65 61 2e 20 20 70 43 65 6c 6c 20 6d 69 67 68 rea. pCell migh
17759 74 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20 t point to some
1775a 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 temporary storag
1775b 65 2e 20 20 54 68 65 20 63 65 6c 6c 20 77 69 6c e. The cell wil
1775c 6c 0a 2a 2a 20 62 65 20 63 6f 6e 73 74 72 75 63 l.** be construc
1775d 74 65 64 20 69 6e 20 74 68 69 73 20 74 65 6d 70 ted in this temp
1775e 6f 72 61 72 79 20 61 72 65 61 20 74 68 65 6e 20 orary area then
1775f 63 6f 70 69 65 64 20 69 6e 74 6f 20 70 50 61 67 copied into pPag
17760 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 74 65 e->aData.** late
17761 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
17762 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d fillInCell(. M
17763 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 emPage *pPage,
17764 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17765 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20 63 The page that c
17766 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c ontains the cell
17767 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
17768 68 61 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 har *pCell,
17769 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 /* Complete
1776a 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 65 6c text of the cel
1776b 6c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 l */. const voi
1776c 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 d *pKey, i64 nKe
1776d 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 y, /* The key
1776e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
1776f 20 2a 70 44 61 74 61 2c 69 6e 74 20 6e 44 61 74 *pData,int nDat
17770 61 2c 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 a, /* The data
17771 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c */. int nZero,
17772 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17773 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 7a 65 /* Extra ze
17774 72 6f 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 ro bytes to appe
17775 6e 64 20 74 6f 20 70 44 61 74 61 20 2a 2f 0a 20 nd to pData */.
17776 20 69 6e 74 20 2a 70 6e 53 69 7a 65 20 20 20 20 int *pnSize
17777 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17778 2f 2a 20 57 72 69 74 65 20 63 65 6c 6c 20 73 69 /* Write cell si
17779 7a 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 ze here */.){.
1777a 69 6e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 int nPayload;.
1777b 63 6f 6e 73 74 20 75 38 20 2a 70 53 72 63 3b 0a const u8 *pSrc;.
1777c 20 20 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72 int nSrc, n, r
1777d 63 3b 0a 20 20 69 6e 74 20 73 70 61 63 65 4c 65 c;. int spaceLe
1777e 66 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 ft;. MemPage *p
1777f 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 Ovfl = 0;. MemP
17780 61 67 65 20 2a 70 54 6f 52 65 6c 65 61 73 65 20 age *pToRelease
17781 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 = 0;. unsigned
17782 63 68 61 72 20 2a 70 50 72 69 6f 72 3b 0a 20 20 char *pPrior;.
17783 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
17784 50 61 79 6c 6f 61 64 3b 0a 20 20 42 74 53 68 61 Payload;. BtSha
17785 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 red *pBt = pPage
17786 2d 3e 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67 ->pBt;. Pgno pg
17787 6e 6f 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 69 6e noOvfl = 0;. in
17788 74 20 6e 48 65 61 64 65 72 3b 0a 20 20 43 65 6c t nHeader;. Cel
17789 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 61 lInfo info;.. a
1778a 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1778b 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d utex_held(pPage-
1778c 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
1778d 0a 20 20 2f 2a 20 70 50 61 67 65 20 69 73 20 6e . /* pPage is n
1778e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 77 ot necessarily w
1778f 72 69 74 65 61 62 6c 65 20 73 69 6e 63 65 20 70 riteable since p
17790 43 65 6c 6c 20 6d 69 67 68 74 20 62 65 20 61 75 Cell might be au
17791 78 69 6c 69 61 72 79 0a 20 20 2a 2a 20 62 75 66 xiliary. ** buf
17792 66 65 72 20 73 70 61 63 65 20 74 68 61 74 20 69 fer space that i
17793 73 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 s separate from
17794 74 68 65 20 70 50 61 67 65 20 62 75 66 66 65 72 the pPage buffer
17795 20 61 72 65 61 20 2a 2f 0a 20 20 61 73 73 65 72 area */. asser
17796 74 28 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d 3e t( pCell<pPage->
17797 61 44 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e 3d aData || pCell>=
17798 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 &pPage->aData[pB
17799 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 t->pageSize].
1779a 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 || sqli
1779b 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
1779c 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
1779d 67 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c ge) );.. /* Fil
1779e 6c 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e l in the header.
1779f 20 2a 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d 20 */. nHeader =
177a0 30 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 0;. if( !pPage-
177a1 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 65 >leaf ){. nHe
177a2 61 64 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 ader += 4;. }.
177a3 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 if( pPage->hasD
177a4 61 74 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 ata ){. nHead
177a5 65 72 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 er += putVarint(
177a6 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c &pCell[nHeader],
177a7 20 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 nData+nZero);.
177a8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 }else{. nDat
177a9 61 20 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 a = nZero = 0;.
177aa 20 7d 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 }. nHeader +=
177ab 70 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c putVarint(&pCell
177ac 5b 6e 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 [nHeader], *(u64
177ad 2a 29 26 6e 4b 65 79 29 3b 0a 20 20 62 74 72 65 *)&nKey);. btre
177ae 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 eParseCellPtr(pP
177af 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 age, pCell, &inf
177b0 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e o);. assert( in
177b1 66 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61 fo.nHeader==nHea
177b2 64 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 der );. assert(
177b3 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79 info.nKey==nKey
177b4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e );. assert( in
177b5 66 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32 29 28 fo.nData==(u32)(
177b6 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0a nData+nZero) );.
177b7 20 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 . /* Fill in
177b8 74 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 the payload */.
177b9 20 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 nPayload = nDat
177ba 61 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 a + nZero;. if(
177bb 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 pPage->intKey )
177bc 7b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 44 61 {. pSrc = pDa
177bd 74 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e ta;. nSrc = n
177be 44 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 20 Data;. nData
177bf 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 = 0;. }else{ .
177c0 20 20 20 69 66 28 20 4e 45 56 45 52 28 6e 4b 65 if( NEVER(nKe
177c1 79 3e 30 78 37 66 66 66 66 66 66 66 20 7c 7c 20 y>0x7fffffff ||
177c2 70 4b 65 79 3d 3d 30 29 20 29 7b 0a 20 20 20 20 pKey==0) ){.
177c3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
177c4 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
177c5 20 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 }. nPayload
177c6 20 2b 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 += (int)nKey;.
177c7 20 20 20 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a pSrc = pKey;.
177c8 20 20 20 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 nSrc = (int)
177c9 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 nKey;. }. *pnS
177ca 69 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 ize = info.nSize
177cb 3b 0a 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 ;. spaceLeft =
177cc 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 info.nLocal;. p
177cd 50 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c Payload = &pCell
177ce 5b 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 [nHeader];. pPr
177cf 69 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 ior = &pCell[inf
177d0 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 o.iOverflow];..
177d1 20 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 while( nPayload
177d2 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 70 >0 ){. if( sp
177d3 61 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69 aceLeft==0 ){.#i
177d4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
177d5 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
177d6 20 20 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d Pgno pgnoPtrm
177d7 61 70 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f ap = pgnoOvfl; /
177d8 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 * Overflow page
177d9 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
177da 79 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 y page */.
177db 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
177dc 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 64 uum ){. d
177dd 6f 7b 0a 20 20 20 20 20 20 20 20 20 20 70 67 6e o{. pgn
177de 6f 4f 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 oOvfl++;.
177df 20 7d 20 77 68 69 6c 65 28 20 0a 20 20 20 20 20 } while( .
177e0 20 20 20 20 20 50 54 52 4d 41 50 5f 49 53 50 41 PTRMAP_ISPA
177e1 47 45 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c GE(pBt, pgnoOvfl
177e2 29 20 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 ) || pgnoOvfl==P
177e3 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
177e4 28 70 42 74 29 20 0a 20 20 20 20 20 20 20 20 29 (pBt) . )
177e5 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 ;. }.#endif
177e6 0a 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f . rc = allo
177e7 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 cateBtreePage(pB
177e8 74 2c 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f t, &pOvfl, &pgno
177e9 4f 76 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 Ovfl, pgnoOvfl,
177ea 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 0);.#ifndef SQLI
177eb 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
177ec 55 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 UM. /* If t
177ed 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 he database supp
177ee 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d orts auto-vacuum
177ef 2c 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 , and the second
177f0 20 6f 72 20 73 75 62 73 65 71 75 65 6e 74 0a 20 or subsequent.
177f1 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 ** overflow
177f2 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 61 page is being a
177f3 6c 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 61 6e llocated, add an
177f4 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 70 6f entry to the po
177f5 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 inter-map.
177f6 2a 2a 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 ** for that page
177f7 20 6e 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a now. . **.
177f8 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 ** If this
177f9 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 76 is the first ov
177fa 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 erflow page, the
177fb 6e 20 77 72 69 74 65 20 61 20 70 61 72 74 69 61 n write a partia
177fc 6c 20 65 6e 74 72 79 20 0a 20 20 20 20 20 20 2a l entry . *
177fd 2a 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 * to the pointer
177fe 2d 6d 61 70 2e 20 49 66 20 77 65 20 77 72 69 74 -map. If we writ
177ff 65 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 e nothing to thi
17800 73 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c s pointer-map sl
17801 6f 74 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 ot,. ** the
17802 6e 20 74 68 65 20 6f 70 74 69 6d 69 73 74 69 63 n the optimistic
17803 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 overflow chain
17804 70 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 63 6c processing in cl
17805 65 61 72 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 earCell().
17806 2a 2a 20 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 ** may misinterp
17807 72 65 74 20 74 68 65 20 75 6e 69 6e 69 74 69 61 ret the uninitia
17808 6c 69 73 65 64 20 76 61 6c 75 65 73 20 61 6e 64 lised values and
17809 20 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20 delete the.
1780a 20 20 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 ** wrong pages
1780b 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 from the databa
1780c 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 se.. */.
1780d 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f if( pBt->auto
1780e 56 61 63 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 Vacuum && rc==SQ
1780f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
17810 20 20 20 75 38 20 65 54 79 70 65 20 3d 20 28 70 u8 eType = (p
17811 67 6e 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 gnoPtrmap?PTRMAP
17812 5f 4f 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 _OVERFLOW2:PTRMA
17813 50 5f 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 P_OVERFLOW1);.
17814 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 ptrmapPut(
17815 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 65 pBt, pgnoOvfl, e
17816 54 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d 61 70 Type, pgnoPtrmap
17817 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 , &rc);.
17818 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
17819 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
1781a 70 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 pOvfl);.
1781b 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
1781c 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b . if( rc ){
1781d 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
1781e 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 Page(pToRelease)
1781f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
17820 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 rc;. }..
17821 20 20 20 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c /* If pToRel
17822 65 61 73 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f ease is not zero
17823 20 74 68 61 6e 20 70 50 72 69 6f 72 20 70 6f 69 than pPrior poi
17824 6e 74 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 nts into the dat
17825 61 20 61 72 65 61 0a 20 20 20 20 20 20 2a 2a 20 a area. **
17826 6f 66 20 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 of pToRelease.
17827 4d 61 6b 65 20 73 75 72 65 20 70 54 6f 52 65 6c Make sure pToRel
17828 65 61 73 65 20 69 73 20 73 74 69 6c 6c 20 77 72 ease is still wr
17829 69 74 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 iteable. */.
1782a 20 20 61 73 73 65 72 74 28 20 70 54 6f 52 65 6c assert( pToRel
1782b 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ease==0 || sqlit
1782c 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
1782d 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70 le(pToRelease->p
1782e 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 DbPage) );..
1782f 20 20 2f 2a 20 49 66 20 70 50 72 69 6f 72 20 69 /* If pPrior i
17830 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 s part of the da
17831 74 61 20 61 72 65 61 20 6f 66 20 70 50 61 67 65 ta area of pPage
17832 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 , then make sure
17833 20 70 50 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 pPage. **
17834 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 is still writeab
17835 6c 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 le */. asse
17836 72 74 28 20 70 50 72 69 6f 72 3c 70 50 61 67 65 rt( pPrior<pPage
17837 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 72 69 6f ->aData || pPrio
17838 72 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61 r>=&pPage->aData
17839 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a [pBt->pageSize].
1783a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 || s
1783b 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
1783c 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 teable(pPage->pD
1783d 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 bPage) );..
1783e 20 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 put4byte(pPrior
1783f 2c 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 , pgnoOvfl);.
17840 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
17841 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 ToRelease);.
17842 20 20 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 pToRelease = p
17843 4f 76 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 Ovfl;. pPri
17844 6f 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 or = pOvfl->aDat
17845 61 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 a;. put4byt
17846 65 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 e(pPrior, 0);.
17847 20 20 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 pPayload = &
17848 70 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b pOvfl->aData[4];
17849 0a 20 20 20 20 20 20 73 70 61 63 65 4c 65 66 74 . spaceLeft
1784a 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 = pBt->usableSi
1784b 7a 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 ze - 4;. }.
1784c 20 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a n = nPayload;.
1784d 20 20 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c if( n>spaceL
1784e 65 66 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c eft ) n = spaceL
1784f 65 66 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 eft;.. /* If
17850 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f pToRelease is no
17851 74 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 61 79 t zero than pPay
17852 6c 6f 61 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f load points into
17853 20 74 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 the data area.
17854 20 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 ** of pToRele
17855 61 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 ase. Make sure
17856 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 pToRelease is st
17857 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a ill writeable. *
17858 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 /. assert( pT
17859 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 oRelease==0 || s
1785a 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
1785b 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 teable(pToReleas
1785c 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a e->pDbPage) );..
1785d 20 20 20 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f /* If pPaylo
1785e 61 64 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 ad is part of th
1785f 65 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70 e data area of p
17860 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 Page, then make
17861 73 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 2a sure pPage. *
17862 2a 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 * is still write
17863 61 62 6c 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 able */. asse
17864 72 74 28 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 rt( pPayload<pPa
17865 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 ge->aData || pPa
17866 79 6c 6f 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 yload>=&pPage->a
17867 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 Data[pBt->pageSi
17868 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 ze].
17869 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 || sqlite3PagerI
1786a 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
1786b 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 ->pDbPage) );..
1786c 20 20 20 69 66 28 20 6e 53 72 63 3e 30 20 29 7b if( nSrc>0 ){
1786d 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 . if( n>nSr
1786e 63 20 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 c ) n = nSrc;.
1786f 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 assert( pSrc
17870 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 );. memcpy
17871 28 70 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c (pPayload, pSrc,
17872 20 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a n);. }else{.
17873 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 memset(pPa
17874 79 6c 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 yload, 0, n);.
17875 20 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 }. nPayload
17876 20 2d 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c -= n;. pPayl
17877 6f 61 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 oad += n;. pS
17878 72 63 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 rc += n;. nSr
17879 63 20 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 c -= n;. spac
1787a 65 4c 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 eLeft -= n;.
1787b 69 66 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 if( nSrc==0 ){.
1787c 20 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 nSrc = nDat
1787d 61 3b 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 a;. pSrc =
1787e 70 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d pData;. }. }
1787f 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 . releasePage(p
17880 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 ToRelease);. re
17881 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
17882 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 }../*.** Remove
17883 74 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 the i-th cell fr
17884 6f 6d 20 70 50 61 67 65 2e 20 20 54 68 69 73 20 om pPage. This
17885 72 6f 75 74 69 6e 65 20 65 66 66 65 63 74 73 20 routine effects
17886 70 50 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 pPage only..** T
17887 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
17888 69 73 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 is not freed or
17889 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 deallocated. It
1788a 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 is assumed that
1788b 0a 2a 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e .** the cell con
1788c 74 65 6e 74 20 68 61 73 20 62 65 65 6e 20 63 6f tent has been co
1788d 70 69 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 pied someplace e
1788e 6c 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 lse. This routi
1788f 6e 65 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 ne just.** remov
17890 65 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 es the reference
17891 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 6f to the cell fro
17892 6d 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 m pPage..**.** "
17893 73 7a 22 20 6d 75 73 74 20 62 65 20 74 68 65 20 sz" must be the
17894 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
17895 69 6e 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a in the cell..*/.
17896 73 74 61 74 69 63 20 76 6f 69 64 20 64 72 6f 70 static void drop
17897 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 Cell(MemPage *pP
17898 61 67 65 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e age, int idx, in
17899 74 20 73 7a 2c 20 69 6e 74 20 2a 70 52 43 29 7b t sz, int *pRC){
1789a 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
1789b 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
1789c 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 er */. int pc;
1789d 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
1789e 74 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e t to cell conten
1789f 74 20 6f 66 20 63 65 6c 6c 20 62 65 69 6e 67 20 t of cell being
178a0 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 75 38 20 deleted */. u8
178a1 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 *data; /*
178a2 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a pPage->aData */.
178a3 20 20 75 38 20 2a 70 74 72 3b 20 20 20 20 20 20 u8 *ptr;
178a4 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6d 6f 76 /* Used to mov
178a5 65 20 62 79 74 65 73 20 61 72 6f 75 6e 64 20 77 e bytes around w
178a6 69 74 68 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a ithin data[] */.
178a7 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
178a8 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 /* The return
178a9 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 64 code */. int hd
178aa 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67 r; /* Beg
178ab 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 68 65 inning of the he
178ac 61 64 65 72 2e 20 20 30 20 6d 6f 73 74 20 70 61 ader. 0 most pa
178ad 67 65 73 2e 20 20 31 30 30 20 70 61 67 65 20 31 ges. 100 page 1
178ae 20 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 */.. if( *pRC
178af 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 ) return;.. ass
178b0 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 ert( idx>=0 && i
178b1 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 dx<pPage->nCell
178b2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d );. assert( sz=
178b3 3d 63 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c =cellSize(pPage,
178b4 20 69 64 78 29 20 29 3b 0a 20 20 61 73 73 65 72 idx) );. asser
178b5 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
178b6 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
178b7 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
178b8 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
178b9 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
178ba 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
178bb 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d . data = pPage-
178bc 3e 61 44 61 74 61 3b 0a 20 20 70 74 72 20 3d 20 >aData;. ptr =
178bd 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c &data[pPage->cel
178be 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 64 78 5d lOffset + 2*idx]
178bf 3b 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 ;. pc = get2byt
178c0 65 28 70 74 72 29 3b 0a 20 20 68 64 72 20 3d 20 e(ptr);. hdr =
178c1 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
178c2 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 63 ;. testcase( pc
178c3 3d 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 61 ==get2byte(&data
178c4 5b 68 64 72 2b 35 5d 29 20 29 3b 0a 20 20 74 65 [hdr+5]) );. te
178c5 73 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 70 stcase( pc+sz==p
178c6 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
178c7 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 70 eSize );. if( p
178c8 63 20 3c 20 67 65 74 32 62 79 74 65 28 26 64 61 c < get2byte(&da
178c9 74 61 5b 68 64 72 2b 35 5d 29 20 7c 7c 20 70 63 ta[hdr+5]) || pc
178ca 2b 73 7a 20 3e 20 70 50 61 67 65 2d 3e 70 42 74 +sz > pPage->pBt
178cb 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a ->usableSize ){.
178cc 20 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 *pRC = SQLIT
178cd 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
178ce 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
178cf 20 20 72 63 20 3d 20 66 72 65 65 53 70 61 63 65 rc = freeSpace
178d0 28 70 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b (pPage, pc, sz);
178d1 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
178d2 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 *pRC = rc;.
178d3 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f return;. }. fo
178d4 72 28 69 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61 r(i=idx+1; i<pPa
178d5 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 ge->nCell; i++,
178d6 70 74 72 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72 ptr+=2){. ptr
178d7 5b 30 5d 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 [0] = ptr[2];.
178d8 20 20 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33 ptr[1] = ptr[3
178d9 5d 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e ];. }. pPage->
178da 6e 43 65 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 nCell--;. put2b
178db 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d yte(&data[hdr+3]
178dc 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b , pPage->nCell);
178dd 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 . pPage->nFree
178de 2b 3d 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 += 2;.}../*.** I
178df 6e 73 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c nsert a new cell
178e0 20 6f 6e 20 70 50 61 67 65 20 61 74 20 63 65 6c on pPage at cel
178e1 6c 20 69 6e 64 65 78 20 22 69 22 2e 20 20 70 43 l index "i". pC
178e2 65 6c 6c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 ell points to th
178e3 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 e.** content of
178e4 74 68 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 the cell..**.**
178e5 49 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 If the cell cont
178e6 65 6e 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 ent will fit on
178e7 74 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20 70 the page, then p
178e8 75 74 20 69 74 20 74 68 65 72 65 2e 20 20 49 66 ut it there. If
178e9 20 69 74 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 it.** will not
178ea 66 69 74 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61 fit, then make a
178eb 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 65 6c copy of the cel
178ec 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70 l content into p
178ed 54 65 6d 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70 Temp if.** pTemp
178ee 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52 is not null. R
178ef 65 67 61 72 64 6c 65 73 73 20 6f 66 20 70 54 65 egardless of pTe
178f0 6d 70 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e mp, allocate a n
178f1 65 77 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 ew entry.** in p
178f2 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e Page->aOvfl[] an
178f3 64 20 6d 61 6b 65 20 69 74 20 70 6f 69 6e 74 20 d make it point
178f4 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 to the cell cont
178f5 65 6e 74 20 28 65 69 74 68 65 72 0a 2a 2a 20 69 ent (either.** i
178f6 6e 20 70 54 65 6d 70 20 6f 72 20 74 68 65 20 6f n pTemp or the o
178f7 72 69 67 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61 riginal pCell) a
178f8 6e 64 20 61 6c 73 6f 20 72 65 63 6f 72 64 20 69 nd also record i
178f9 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c ts index. .** Al
178fa 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 65 locating a new e
178fb 6e 74 72 79 20 69 6e 20 70 50 61 67 65 2d 3e 61 ntry in pPage->a
178fc 43 65 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74 Cell[] implies t
178fd 68 61 74 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e hat .** pPage->n
178fe 4f 76 65 72 66 6c 6f 77 20 69 73 20 69 6e 63 72 Overflow is incr
178ff 65 6d 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 emented..**.** I
17900 66 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a f nSkip is non-z
17901 65 72 6f 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 ero, then do not
17902 20 63 6f 70 79 20 74 68 65 20 66 69 72 73 74 20 copy the first
17903 6e 53 6b 69 70 20 62 79 74 65 73 20 6f 66 20 74 nSkip bytes of t
17904 68 65 0a 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20 he.** cell. The
17905 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72 caller will over
17906 77 72 69 74 65 20 74 68 65 6d 20 61 66 74 65 72 write them after
17907 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
17908 65 74 75 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53 eturns. If.** nS
17909 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c kip is non-zero,
1790a 20 74 68 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20 then pCell may
1790b 6e 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 not point to an
1790c 69 6e 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c invalid memory l
1790d 6f 63 61 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74 ocation .** (but
1790e 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20 pCell+nSkip is
1790f 61 6c 77 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a always valid)..*
17910 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e /.static void in
17911 73 65 72 74 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 sertCell(. MemP
17912 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a age *pPage, /*
17913 20 50 61 67 65 20 69 6e 74 6f 20 77 68 69 63 68 Page into which
17914 20 77 65 20 61 72 65 20 63 6f 70 79 69 6e 67 20 we are copying
17915 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20 */. int i,
17916 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 65 /* New ce
17917 6c 6c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 69 ll becomes the i
17918 2d 74 68 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 -th cell of the
17919 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 43 page */. u8 *pC
1791a 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 ell, /* C
1791b 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65 ontent of the ne
1791c 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 w cell */. int
1791d 73 7a 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a sz, /*
1791e 20 42 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e Bytes of conten
1791f 74 20 69 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 t in pCell */.
17920 75 38 20 2a 70 54 65 6d 70 2c 20 20 20 20 20 20 u8 *pTemp,
17921 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 67 /* Temp storag
17922 65 20 73 70 61 63 65 20 66 6f 72 20 70 43 65 6c e space for pCel
17923 6c 2c 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a l, if needed */.
17924 20 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 Pgno iChild,
17925 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 /* If non-ze
17926 72 6f 2c 20 72 65 70 6c 61 63 65 20 66 69 72 73 ro, replace firs
17927 74 20 34 20 62 79 74 65 73 20 77 69 74 68 20 74 t 4 bytes with t
17928 68 69 73 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 his value */. i
17929 6e 74 20 2a 70 52 43 20 20 20 20 20 20 20 20 20 nt *pRC
1792a 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69 /* Read and wri
1792b 74 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 te return code f
1792c 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 rom here */.){.
1792d 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 int idx;
1792e 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 /* Where to w
1792f 72 69 74 65 20 6e 65 77 20 63 65 6c 6c 20 63 6f rite new cell co
17930 6e 74 65 6e 74 20 69 6e 20 64 61 74 61 5b 5d 20 ntent in data[]
17931 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 */. int j;
17932 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
17933 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 ounter */. int
17934 65 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a end; /*
17935 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73 74 First byte past
17936 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 the last cell p
17937 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d ointer in data[]
17938 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 73 3b 20 20 */. int ins;
17939 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
1793a 20 69 6e 20 64 61 74 61 5b 5d 20 77 68 65 72 65 in data[] where
1793b 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 new cell pointe
1793c 72 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f r is inserted */
1793d 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 . int cellOffse
1793e 74 3b 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 t; /* Address
1793f 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f of first cell po
17940 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 inter in data[]
17941 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 */. u8 *data;
17942 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f /* The co
17943 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 77 68 6f ntent of the who
17944 6c 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 le page */. u8
17945 2a 70 74 72 3b 20 20 20 20 20 20 20 20 20 20 2f *ptr; /
17946 2a 20 55 73 65 64 20 66 6f 72 20 6d 6f 76 69 6e * Used for movin
17947 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72 g information ar
17948 6f 75 6e 64 20 69 6e 20 64 61 74 61 5b 5d 20 2a ound in data[] *
17949 2f 0a 0a 20 20 69 6e 74 20 6e 53 6b 69 70 20 3d /.. int nSkip =
1794a 20 28 69 43 68 69 6c 64 20 3f 20 34 20 3a 20 30 (iChild ? 4 : 0
1794b 29 3b 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 );.. if( *pRC )
1794c 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 return;.. asse
1794d 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 3d 70 rt( i>=0 && i<=p
1794e 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 70 50 61 67 Page->nCell+pPag
1794f 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a e->nOverflow );.
17950 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
17951 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c 28 >nCell<=MX_CELL(
17952 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20 4d pPage->pBt) && M
17953 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 X_CELL(pPage->pB
17954 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61 73 t)<=5460 );. as
17955 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 sert( pPage->nOv
17956 65 72 66 6c 6f 77 3c 3d 41 72 72 61 79 53 69 7a erflow<=ArraySiz
17957 65 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 20 e(pPage->aOvfl)
17958 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d );. assert( sz=
17959 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 =cellSizePtr(pPa
1795a 67 65 2c 20 70 43 65 6c 6c 29 20 29 3b 0a 20 20 ge, pCell) );.
1795b 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1795c 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
1795d 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
1795e 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f . if( pPage->nO
1795f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a 2b 32 3e verflow || sz+2>
17960 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a pPage->nFree ){.
17961 20 20 20 20 69 66 28 20 70 54 65 6d 70 20 29 7b if( pTemp ){
17962 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54 . memcpy(pT
17963 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 43 65 6c 6c emp+nSkip, pCell
17964 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 70 +nSkip, sz-nSkip
17965 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d );. pCell =
17966 20 70 54 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 pTemp;. }.
17967 20 20 69 66 28 20 69 43 68 69 6c 64 20 29 7b 0a if( iChild ){.
17968 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 put4byte(p
17969 43 65 6c 6c 2c 20 69 43 68 69 6c 64 29 3b 0a 20 Cell, iChild);.
1796a 20 20 20 7d 0a 20 20 20 20 6a 20 3d 20 70 50 61 }. j = pPa
1796b 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b ge->nOverflow++;
1796c 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 28 . assert( j<(
1796d 69 6e 74 29 28 73 69 7a 65 6f 66 28 70 50 61 67 int)(sizeof(pPag
1796e 65 2d 3e 61 4f 76 66 6c 29 2f 73 69 7a 65 6f 66 e->aOvfl)/sizeof
1796f 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d (pPage->aOvfl[0]
17970 29 29 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d )) );. pPage-
17971 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c 20 >aOvfl[j].pCell
17972 3d 20 70 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61 = pCell;. pPa
17973 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64 78 ge->aOvfl[j].idx
17974 20 3d 20 28 75 31 36 29 69 3b 0a 20 20 7d 65 6c = (u16)i;. }el
17975 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d se{. int rc =
17976 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
17977 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 te(pPage->pDbPag
17978 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
17979 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1797a 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 *pRC = rc;.
1797b 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 return;.
1797c 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 }. assert( sq
1797d 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
1797e 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
1797f 50 61 67 65 29 20 29 3b 0a 20 20 20 20 64 61 74 Page) );. dat
17980 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 a = pPage->aData
17981 3b 0a 20 20 20 20 63 65 6c 6c 4f 66 66 73 65 74 ;. cellOffset
17982 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 = pPage->cellOf
17983 66 73 65 74 3b 0a 20 20 20 20 65 6e 64 20 3d 20 fset;. end =
17984 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 cellOffset + 2*p
17985 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 Page->nCell;.
17986 20 69 6e 73 20 3d 20 63 65 6c 6c 4f 66 66 73 65 ins = cellOffse
17987 74 20 2b 20 32 2a 69 3b 0a 20 20 20 20 72 63 20 t + 2*i;. rc
17988 3d 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 = allocateSpace(
17989 70 50 61 67 65 2c 20 73 7a 2c 20 26 69 64 78 29 pPage, sz, &idx)
1798a 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 20 ;. if( rc ){
1798b 2a 70 52 43 20 3d 20 72 63 3b 20 72 65 74 75 72 *pRC = rc; retur
1798c 6e 3b 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 n; }. /* The
1798d 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 29 20 allocateSpace()
1798e 72 6f 75 74 69 6e 65 20 67 75 61 72 61 6e 74 65 routine guarante
1798f 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 es the following
17990 20 74 77 6f 20 70 72 6f 70 65 72 74 69 65 73 0a two properties.
17991 20 20 20 20 2a 2a 20 69 66 20 69 74 20 72 65 74 ** if it ret
17992 75 72 6e 73 20 73 75 63 63 65 73 73 20 2a 2f 0a urns success */.
17993 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 20 assert( idx
17994 3e 3d 20 65 6e 64 2b 32 20 29 3b 0a 20 20 20 20 >= end+2 );.
17995 61 73 73 65 72 74 28 20 69 64 78 2b 73 7a 20 3c assert( idx+sz <
17996 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 = pPage->pBt->us
17997 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 ableSize );.
17998 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b 0a pPage->nCell++;.
17999 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 pPage->nFree
1799a 20 2d 3d 20 28 75 31 36 29 28 32 20 2b 20 73 7a -= (u16)(2 + sz
1799b 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 );. memcpy(&d
1799c 61 74 61 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c 20 ata[idx+nSkip],
1799d 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d pCell+nSkip, sz-
1799e 6e 53 6b 69 70 29 3b 0a 20 20 20 20 69 66 28 20 nSkip);. if(
1799f 69 43 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 iChild ){.
179a0 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b 69 put4byte(&data[i
179a1 64 78 5d 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 dx], iChild);.
179a2 20 20 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e }. for(j=en
179a3 64 2c 20 70 74 72 3d 26 64 61 74 61 5b 6a 5d 3b d, ptr=&data[j];
179a4 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32 2c 20 70 74 j>ins; j-=2, pt
179a5 72 2d 3d 32 29 7b 0a 20 20 20 20 20 20 70 74 72 r-=2){. ptr
179a6 5b 30 5d 20 3d 20 70 74 72 5b 2d 32 5d 3b 0a 20 [0] = ptr[-2];.
179a7 20 20 20 20 20 70 74 72 5b 31 5d 20 3d 20 70 74 ptr[1] = pt
179a8 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 r[-1];. }.
179a9 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
179aa 69 6e 73 5d 2c 20 69 64 78 29 3b 0a 20 20 20 20 ins], idx);.
179ab 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 put2byte(&data[p
179ac 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b Page->hdrOffset+
179ad 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3], pPage->nCell
179ae 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
179af 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
179b0 4d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d M. if( pPage-
179b1 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d >pBt->autoVacuum
179b2 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 ){. /* The
179b3 20 63 65 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 cell may contai
179b4 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 n a pointer to a
179b5 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e n overflow page.
179b6 20 49 66 20 73 6f 2c 20 77 72 69 74 65 0a 20 20 If so, write.
179b7 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 ** the entry
179b8 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f for the overflo
179b9 77 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 w page into the
179ba 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20 pointer map..
179bb 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 74 72 6d */. ptrm
179bc 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 apPutOvflPtr(pPa
179bd 67 65 2c 20 70 43 65 6c 6c 2c 20 70 52 43 29 3b ge, pCell, pRC);
179be 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
179bf 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 }.}../*.** Add a
179c0 20 6c 69 73 74 20 6f 66 20 63 65 6c 6c 73 20 74 list of cells t
179c1 6f 20 61 20 70 61 67 65 2e 20 20 54 68 65 20 70 o a page. The p
179c2 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e age should be in
179c3 69 74 69 61 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a itially empty..*
179c4 2a 20 54 68 65 20 63 65 6c 6c 73 20 61 72 65 20 * The cells are
179c5 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 66 69 guaranteed to fi
179c6 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a t on the page..*
179c7 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 /.static void as
179c8 73 65 6d 62 6c 65 50 61 67 65 28 0a 20 20 4d 65 semblePage(. Me
179c9 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 mPage *pPage,
179ca 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 62 /* The page to b
179cb 65 20 61 73 73 65 6d 62 6c 69 65 64 20 2a 2f 0a e assemblied */.
179cc 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 int nCell,
179cd 20 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 /* The numbe
179ce 72 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 64 r of cells to ad
179cf 64 20 74 6f 20 74 68 69 73 20 70 61 67 65 20 2a d to this page *
179d0 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c /. u8 **apCell,
179d1 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
179d2 73 20 74 6f 20 63 65 6c 6c 20 62 6f 64 69 65 73 s to cell bodies
179d3 20 2a 2f 0a 20 20 75 31 36 20 2a 61 53 69 7a 65 */. u16 *aSize
179d4 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 73 /* Sizes
179d5 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 2a 2f of the cells */
179d6 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 .){. int i;
179d7 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
179d8 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 counter */. u8
179d9 2a 70 43 65 6c 6c 70 74 72 3b 20 20 20 20 20 2f *pCellptr; /
179da 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e 65 78 * Address of nex
179db 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a t cell pointer *
179dc 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79 /. int cellbody
179dd 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 ; /* Address
179de 20 6f 66 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f of next cell bo
179df 64 79 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e dy */. u8 * con
179e0 73 74 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d st data = pPage-
179e1 3e 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 >aData;
179e2 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
179e3 6f 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 65 o data for pPage
179e4 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 */. const int
179e5 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 hdr = pPage->hdr
179e6 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 Offset;
179e7 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 /* Offset of h
179e8 65 61 64 65 72 20 6f 6e 20 70 50 61 67 65 20 2a eader on pPage *
179e9 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 55 /. const int nU
179ea 73 61 62 6c 65 20 3d 20 70 50 61 67 65 2d 3e 70 sable = pPage->p
179eb 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 20 Bt->usableSize;
179ec 2f 2a 20 55 73 61 62 6c 65 20 73 69 7a 65 20 6f /* Usable size o
179ed 66 20 70 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73 f page */.. ass
179ee 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 ert( pPage->nOve
179ef 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 rflow==0 );. as
179f0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
179f1 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
179f2 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
179f3 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3e 3d assert( nCell>=
179f4 30 20 26 26 20 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 0 && nCell<=MX_C
179f5 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 20 ELL(pPage->pBt)
179f6 26 26 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 && MX_CELL(pPage
179f7 2d 3e 70 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a ->pBt)<=5460 );.
179f8 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
179f9 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
179fa 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
179fb 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b ) );.. /* Check
179fc 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 68 that the page h
179fd 61 73 20 6a 75 73 74 20 62 65 65 6e 20 7a 65 72 as just been zer
179fe 6f 65 64 20 62 79 20 7a 65 72 6f 50 61 67 65 28 oed by zeroPage(
179ff 29 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 ) */. assert( p
17a00 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 Page->nCell==0 )
17a01 3b 0a 20 20 61 73 73 65 72 74 28 20 67 65 74 32 ;. assert( get2
17a02 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
17a03 5d 29 3d 3d 6e 55 73 61 62 6c 65 20 29 3b 0a 0a ])==nUsable );..
17a04 20 20 70 43 65 6c 6c 70 74 72 20 3d 20 26 64 61 pCellptr = &da
17a05 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 ta[pPage->cellOf
17a06 66 73 65 74 20 2b 20 6e 43 65 6c 6c 2a 32 5d 3b fset + nCell*2];
17a07 0a 20 20 63 65 6c 6c 62 6f 64 79 20 3d 20 6e 55 . cellbody = nU
17a08 73 61 62 6c 65 3b 0a 20 20 66 6f 72 28 69 3d 6e sable;. for(i=n
17a09 43 65 6c 6c 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d Cell-1; i>=0; i-
17a0a 2d 29 7b 0a 20 20 20 20 70 43 65 6c 6c 70 74 72 -){. pCellptr
17a0b 20 2d 3d 20 32 3b 0a 20 20 20 20 63 65 6c 6c 62 -= 2;. cellb
17a0c 6f 64 79 20 2d 3d 20 61 53 69 7a 65 5b 69 5d 3b ody -= aSize[i];
17a0d 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 70 43 . put2byte(pC
17a0e 65 6c 6c 70 74 72 2c 20 63 65 6c 6c 62 6f 64 79 ellptr, cellbody
17a0f 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 );. memcpy(&d
17a10 61 74 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61 ata[cellbody], a
17a11 70 43 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b pCell[i], aSize[
17a12 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 75 74 32 62 i]);. }. put2b
17a13 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d yte(&data[hdr+3]
17a14 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20 70 75 74 32 , nCell);. put2
17a15 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
17a16 5d 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 ], cellbody);.
17a17 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 pPage->nFree -=
17a18 28 6e 43 65 6c 6c 2a 32 20 2b 20 6e 55 73 61 62 (nCell*2 + nUsab
17a19 6c 65 20 2d 20 63 65 6c 6c 62 6f 64 79 29 3b 0a le - cellbody);.
17a1a 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d pPage->nCell =
17a1b 20 28 75 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a (u16)nCell;.}..
17a1c 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
17a1d 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73 20 64 ing parameters d
17a1e 65 74 65 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e etermine how man
17a1f 79 20 61 64 6a 61 63 65 6e 74 20 70 61 67 65 73 y adjacent pages
17a20 20 67 65 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a get involved.**
17a21 20 69 6e 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 in a balancing
17a22 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 operation. NN i
17a23 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
17a24 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 neighbors on eit
17a25 68 65 72 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 her side.** of t
17a26 68 65 20 70 61 67 65 20 74 68 61 74 20 70 61 72 he page that par
17a27 74 69 63 69 70 61 74 65 20 69 6e 20 74 68 65 20 ticipate in the
17a28 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 balancing operat
17a29 69 6f 6e 2e 20 20 4e 42 20 69 73 20 74 68 65 0a ion. NB is the.
17a2a 2a 2a 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 ** total number
17a2b 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 70 61 of pages that pa
17a2c 72 74 69 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 rticipate, inclu
17a2d 64 69 6e 67 20 74 68 65 20 74 61 72 67 65 74 20 ding the target
17a2e 70 61 67 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e page and.** NN n
17a2f 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 eighbors on eith
17a30 65 72 20 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 er side..**.** T
17a31 68 65 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 he minimum value
17a32 20 6f 66 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 of NN is 1 (of
17a33 63 6f 75 72 73 65 29 2e 20 20 49 6e 63 72 65 61 course). Increa
17a34 73 69 6e 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a sing NN above 1.
17a35 2a 2a 20 28 74 6f 20 32 20 6f 72 20 33 29 20 67 ** (to 2 or 3) g
17a36 69 76 65 73 20 61 20 6d 6f 64 65 73 74 20 69 6d ives a modest im
17a37 70 72 6f 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c provement in SEL
17a38 45 43 54 20 61 6e 64 20 44 45 4c 45 54 45 20 70 ECT and DELETE p
17a39 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e erformance.** in
17a3a 20 65 78 63 68 61 6e 67 65 20 66 6f 72 20 61 20 exchange for a
17a3b 6c 61 72 67 65 72 20 64 65 67 72 61 64 61 74 69 larger degradati
17a3c 6f 6e 20 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 on in INSERT and
17a3d 20 55 50 44 41 54 45 20 70 65 72 66 6f 72 6d 61 UPDATE performa
17a3e 6e 63 65 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 nce..** The valu
17a3f 65 20 6f 66 20 4e 4e 20 61 70 70 65 61 72 73 20 e of NN appears
17a40 74 6f 20 67 69 76 65 20 74 68 65 20 62 65 73 74 to give the best
17a41 20 72 65 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c results overall
17a42 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 ..*/.#define NN
17a43 31 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 1 /*
17a44 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 Number of neigh
17a45 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 bors on either s
17a46 69 64 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a ide of pPage */.
17a47 23 64 65 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 #define NB (NN*2
17a48 2b 31 29 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 +1) /* Tota
17a49 6c 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 l pages involved
17a4a 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 65 20 in the balance
17a4b 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */...#ifndef SQL
17a4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 ITE_OMIT_QUICKBA
17a4d 4c 41 4e 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 LANCE./*.** This
17a4e 20 76 65 72 73 69 6f 6e 20 6f 66 20 62 61 6c 61 version of bala
17a4f 6e 63 65 28 29 20 68 61 6e 64 6c 65 73 20 74 68 nce() handles th
17a50 65 20 63 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 6c e common special
17a51 20 63 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 61 case where.** a
17a52 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 62 65 new entry is be
17a53 69 6e 67 20 69 6e 73 65 72 74 65 64 20 6f 6e 20 ing inserted on
17a54 74 68 65 20 65 78 74 72 65 6d 65 20 72 69 67 68 the extreme righ
17a55 74 2d 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 t-end of the.**
17a56 74 72 65 65 2c 20 69 6e 20 6f 74 68 65 72 20 77 tree, in other w
17a57 6f 72 64 73 2c 20 77 68 65 6e 20 74 68 65 20 6e ords, when the n
17a58 65 77 20 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 ew entry will be
17a59 63 6f 6d 65 20 74 68 65 20 6c 61 72 67 65 73 74 come the largest
17a5a 0a 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65 .** entry in the
17a5b 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 tree..**.** Ins
17a5c 74 65 61 64 20 6f 66 20 74 72 79 69 6e 67 20 74 tead of trying t
17a5d 6f 20 62 61 6c 61 6e 63 65 20 74 68 65 20 33 20 o balance the 3
17a5e 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 right-most leaf
17a5f 70 61 67 65 73 2c 20 6a 75 73 74 20 61 64 64 0a pages, just add.
17a60 2a 2a 20 61 20 6e 65 77 20 70 61 67 65 20 74 6f ** a new page to
17a61 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 the right-hand
17a62 73 69 64 65 20 61 6e 64 20 70 75 74 20 74 68 65 side and put the
17a63 20 6f 6e 65 20 6e 65 77 20 65 6e 74 72 79 20 69 one new entry i
17a64 6e 0a 2a 2a 20 74 68 61 74 20 70 61 67 65 2e 20 n.** that page.
17a65 20 54 68 69 73 20 6c 65 61 76 65 73 20 74 68 65 This leaves the
17a66 20 72 69 67 68 74 20 73 69 64 65 20 6f 66 20 74 right side of t
17a67 68 65 20 74 72 65 65 20 73 6f 6d 65 77 68 61 74 he tree somewhat
17a68 0a 2a 2a 20 75 6e 62 61 6c 61 6e 63 65 64 2e 20 .** unbalanced.
17a69 20 42 75 74 20 6f 64 64 73 20 61 72 65 20 74 68 But odds are th
17a6a 61 74 20 77 65 20 77 69 6c 6c 20 62 65 20 69 6e at we will be in
17a6b 73 65 72 74 69 6e 67 20 6e 65 77 20 65 6e 74 72 serting new entr
17a6c 69 65 73 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e ies.** at the en
17a6d 64 20 73 6f 6f 6e 20 61 66 74 65 72 77 61 72 64 d soon afterward
17a6e 73 20 73 6f 20 74 68 65 20 6e 65 61 72 6c 79 20 s so the nearly
17a6f 65 6d 70 74 79 20 70 61 67 65 20 77 69 6c 6c 20 empty page will
17a70 71 75 69 63 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20 quickly.** fill
17a71 75 70 2e 20 20 4f 6e 20 61 76 65 72 61 67 65 2e up. On average.
17a72 0a 2a 2a 0a 2a 2a 20 70 50 61 67 65 20 69 73 20 .**.** pPage is
17a73 74 68 65 20 6c 65 61 66 20 70 61 67 65 20 77 68 the leaf page wh
17a74 69 63 68 20 69 73 20 74 68 65 20 72 69 67 68 74 ich is the right
17a75 2d 6d 6f 73 74 20 70 61 67 65 20 69 6e 20 74 68 -most page in th
17a76 65 20 74 72 65 65 2e 0a 2a 2a 20 70 50 61 72 65 e tree..** pPare
17a77 6e 74 20 69 73 20 69 74 73 20 70 61 72 65 6e 74 nt is its parent
17a78 2e 20 20 70 50 61 67 65 20 6d 75 73 74 20 68 61 . pPage must ha
17a79 76 65 20 61 20 73 69 6e 67 6c 65 20 6f 76 65 72 ve a single over
17a7a 66 6c 6f 77 20 65 6e 74 72 79 0a 2a 2a 20 77 68 flow entry.** wh
17a7b 69 63 68 20 69 73 20 61 6c 73 6f 20 74 68 65 20 ich is also the
17a7c 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 right-most entry
17a7d 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a on the page..**
17a7e 0a 2a 2a 20 54 68 65 20 70 53 70 61 63 65 20 62 .** The pSpace b
17a7f 75 66 66 65 72 20 69 73 20 75 73 65 64 20 74 6f uffer is used to
17a80 20 73 74 6f 72 65 20 61 20 74 65 6d 70 6f 72 61 store a tempora
17a81 72 79 20 63 6f 70 79 20 6f 66 20 74 68 65 20 64 ry copy of the d
17a82 69 76 69 64 65 72 0a 2a 2a 20 63 65 6c 6c 20 74 ivider.** cell t
17a83 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 hat will be inse
17a84 72 74 65 64 20 69 6e 74 6f 20 70 50 61 72 65 6e rted into pParen
17a85 74 2e 20 53 75 63 68 20 61 20 63 65 6c 6c 20 63 t. Such a cell c
17a86 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 34 0a 2a onsists of a 4.*
17a87 2a 20 62 79 74 65 20 70 61 67 65 20 6e 75 6d 62 * byte page numb
17a88 65 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 er followed by a
17a89 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 variable length
17a8a 20 69 6e 74 65 67 65 72 2e 20 49 6e 20 6f 74 68 integer. In oth
17a8b 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 61 74 20 er.** words, at
17a8c 6d 6f 73 74 20 31 33 20 62 79 74 65 73 2e 20 48 most 13 bytes. H
17a8d 65 6e 63 65 20 74 68 65 20 70 53 70 61 63 65 20 ence the pSpace
17a8e 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 61 buffer must be a
17a8f 74 0a 2a 2a 20 6c 65 61 73 74 20 31 33 20 62 79 t.** least 13 by
17a90 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a tes in size..*/.
17a91 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e static int balan
17a92 63 65 5f 71 75 69 63 6b 28 4d 65 6d 50 61 67 65 ce_quick(MemPage
17a93 20 2a 70 50 61 72 65 6e 74 2c 20 4d 65 6d 50 61 *pParent, MemPa
17a94 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 ge *pPage, u8 *p
17a95 53 70 61 63 65 29 7b 0a 20 20 42 74 53 68 61 72 Space){. BtShar
17a96 65 64 20 2a 63 6f 6e 73 74 20 70 42 74 20 3d 20 ed *const pBt =
17a97 70 50 61 67 65 2d 3e 70 42 74 3b 20 20 20 20 2f pPage->pBt; /
17a98 2a 20 42 2d 54 72 65 65 20 44 61 74 61 62 61 73 * B-Tree Databas
17a99 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a e */. MemPage *
17a9a 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 pNew;
17a9b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
17a9c 65 77 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 70 ewly allocated p
17a9d 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b age */. int rc;
17a9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17a9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17aa0 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a Return Code */.
17aa1 20 20 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 20 Pgno pgnoNew;
17aa2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17aa3 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e /* Page n
17aa4 75 6d 62 65 72 20 6f 66 20 70 4e 65 77 20 2a 2f umber of pNew */
17aa5 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
17aa6 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
17aa7 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
17aa8 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
17aa9 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
17aaa 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e teable(pParent->
17aab 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 pDbPage) );. as
17aac 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 sert( pPage->nOv
17aad 65 72 66 6c 6f 77 3d 3d 31 20 29 3b 0a 0a 20 20 erflow==1 );..
17aae 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c if( pPage->nCell
17aaf 3c 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c <=0 ) return SQL
17ab0 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
17ab1 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 ;.. /* Allocate
17ab2 20 61 20 6e 65 77 20 70 61 67 65 2e 20 54 68 69 a new page. Thi
17ab3 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65 63 6f s page will beco
17ab4 6d 65 20 74 68 65 20 72 69 67 68 74 2d 73 69 62 me the right-sib
17ab5 6c 69 6e 67 20 6f 66 20 0a 20 20 2a 2a 20 70 50 ling of . ** pP
17ab6 61 67 65 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 age. Make the pa
17ab7 72 65 6e 74 20 70 61 67 65 20 77 72 69 74 61 62 rent page writab
17ab8 6c 65 2c 20 73 6f 20 74 68 61 74 20 74 68 65 20 le, so that the
17ab9 6e 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c new divider cell
17aba 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 69 6e 73 . ** may be ins
17abb 65 72 74 65 64 2e 20 49 66 20 62 6f 74 68 20 74 erted. If both t
17abc 68 65 73 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 hese operations
17abd 61 72 65 20 73 75 63 63 65 73 73 66 75 6c 2c 20 are successful,
17abe 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 proceed.. */.
17abf 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 rc = allocateBtr
17ac0 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65 eePage(pBt, &pNe
17ac1 77 2c 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20 w, &pgnoNew, 0,
17ac2 30 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 0);.. if( rc==S
17ac3 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 0a 20 20 20 QLITE_OK ){..
17ac4 20 75 38 20 2a 70 4f 75 74 20 3d 20 26 70 53 70 u8 *pOut = &pSp
17ac5 61 63 65 5b 34 5d 3b 0a 20 20 20 20 75 38 20 2a ace[4];. u8 *
17ac6 70 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61 pCell = pPage->a
17ac7 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 Ovfl[0].pCell;.
17ac8 20 20 20 75 31 36 20 73 7a 43 65 6c 6c 20 3d 20 u16 szCell =
17ac9 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 cellSizePtr(pPag
17aca 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 75 e, pCell);. u
17acb 38 20 2a 70 53 74 6f 70 3b 0a 0a 20 20 20 20 61 8 *pStop;.. a
17acc 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
17acd 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
17ace 4e 65 77 2d 3e 70 44 62 50 61 67 65 29 20 29 3b New->pDbPage) );
17acf 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
17ad0 67 65 2d 3e 61 44 61 74 61 5b 30 5d 3d 3d 28 50 ge->aData[0]==(P
17ad1 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 TF_INTKEY|PTF_LE
17ad2 41 46 44 41 54 41 7c 50 54 46 5f 4c 45 41 46 29 AFDATA|PTF_LEAF)
17ad3 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 );. zeroPage
17ad4 28 70 4e 65 77 2c 20 50 54 46 5f 49 4e 54 4b 45 (pNew, PTF_INTKE
17ad5 59 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 7c 50 Y|PTF_LEAFDATA|P
17ad6 54 46 5f 4c 45 41 46 29 3b 0a 20 20 20 20 61 73 TF_LEAF);. as
17ad7 73 65 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 2c semblePage(pNew,
17ad8 20 31 2c 20 26 70 43 65 6c 6c 2c 20 26 73 7a 43 1, &pCell, &szC
17ad9 65 6c 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 ell);.. /* If
17ada 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f this is an auto
17adb 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 -vacuum database
17adc 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 , update the poi
17add 6e 74 65 72 20 6d 61 70 0a 20 20 20 20 2a 2a 20 nter map. **
17ade 77 69 74 68 20 65 6e 74 72 69 65 73 20 66 6f 72 with entries for
17adf 20 74 68 65 20 6e 65 77 20 70 61 67 65 2c 20 61 the new page, a
17ae0 6e 64 20 61 6e 79 20 70 6f 69 6e 74 65 72 20 66 nd any pointer f
17ae1 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 rom the . **
17ae2 63 65 6c 6c 20 6f 6e 20 74 68 65 20 70 61 67 65 cell on the page
17ae3 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 to an overflow
17ae4 70 61 67 65 2e 20 49 66 20 65 69 74 68 65 72 20 page. If either
17ae5 6f 66 20 74 68 65 73 65 0a 20 20 20 20 2a 2a 20 of these. **
17ae6 6f 70 65 72 61 74 69 6f 6e 73 20 66 61 69 6c 73 operations fails
17ae7 2c 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 , the return cod
17ae8 65 20 69 73 20 73 65 74 2c 20 62 75 74 20 74 68 e is set, but th
17ae9 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20 20 20 2a e contents. *
17aea 2a 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 * of the parent
17aeb 70 61 67 65 20 61 72 65 20 73 74 69 6c 6c 20 6d page are still m
17aec 61 6e 69 70 75 6c 61 74 65 64 20 62 79 20 74 68 anipulated by th
17aed 68 20 63 6f 64 65 20 62 65 6c 6f 77 2e 0a 20 20 h code below..
17aee 20 20 2a 2a 20 54 68 61 74 20 69 73 20 4f 6b 2c ** That is Ok,
17aef 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 at this point t
17af0 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 he parent page i
17af1 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 0a s guaranteed to.
17af2 20 20 20 20 2a 2a 20 62 65 20 6d 61 72 6b 65 64 ** be marked
17af3 20 61 73 20 64 69 72 74 79 2e 20 52 65 74 75 72 as dirty. Retur
17af4 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f ning an error co
17af5 64 65 20 77 69 6c 6c 20 63 61 75 73 65 20 61 0a de will cause a.
17af6 20 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c ** rollback,
17af7 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20 63 68 61 undoing any cha
17af8 6e 67 65 73 20 6d 61 64 65 20 74 6f 20 74 68 65 nges made to the
17af9 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 parent page..
17afa 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 41 */. if( ISA
17afb 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 UTOVACUUM ){.
17afc 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 ptrmapPut(pBt
17afd 2c 20 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 , pgnoNew, PTRMA
17afe 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74 P_BTREE, pParent
17aff 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 ->pgno, &rc);.
17b00 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 3e 70 if( szCell>p
17b01 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b New->minLocal ){
17b02 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 50 . ptrmapP
17b03 75 74 4f 76 66 6c 50 74 72 28 70 4e 65 77 2c 20 utOvflPtr(pNew,
17b04 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 pCell, &rc);.
17b05 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 }. }. .
17b06 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 64 69 /* Create a di
17b07 76 69 64 65 72 20 63 65 6c 6c 20 74 6f 20 69 6e vider cell to in
17b08 73 65 72 74 20 69 6e 74 6f 20 70 50 61 72 65 6e sert into pParen
17b09 74 2e 20 54 68 65 20 64 69 76 69 64 65 72 20 63 t. The divider c
17b0a 65 6c 6c 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69 ell. ** consi
17b0b 73 74 73 20 6f 66 20 61 20 34 2d 62 79 74 65 20 sts of a 4-byte
17b0c 70 61 67 65 20 6e 75 6d 62 65 72 20 28 74 68 65 page number (the
17b0d 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
17b0e 70 50 61 67 65 29 20 61 6e 64 0a 20 20 20 20 2a pPage) and. *
17b0f 2a 20 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e * a variable len
17b10 67 74 68 20 6b 65 79 20 76 61 6c 75 65 20 28 77 gth key value (w
17b11 68 69 63 68 20 6d 75 73 74 20 62 65 20 74 68 65 hich must be the
17b12 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 same value as t
17b13 68 65 0a 20 20 20 20 2a 2a 20 6c 61 72 67 65 73 he. ** larges
17b14 74 20 6b 65 79 20 6f 6e 20 70 50 61 67 65 29 2e t key on pPage).
17b15 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
17b16 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 o find the large
17b17 73 74 20 6b 65 79 20 76 61 6c 75 65 20 6f 6e 20 st key value on
17b18 70 50 61 67 65 2c 20 66 69 72 73 74 20 66 69 6e pPage, first fin
17b19 64 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 d the right-most
17b1a 20 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e . ** cell on
17b1b 20 70 50 61 67 65 2e 20 54 68 65 20 66 69 72 73 pPage. The firs
17b1c 74 20 74 77 6f 20 66 69 65 6c 64 73 20 6f 66 20 t two fields of
17b1d 74 68 69 73 20 63 65 6c 6c 20 61 72 65 20 74 68 this cell are th
17b1e 65 20 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 e . ** record
17b1f 2d 6c 65 6e 67 74 68 20 28 61 20 76 61 72 69 61 -length (a varia
17b20 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 ble length integ
17b21 65 72 20 61 74 20 6d 6f 73 74 20 33 32 2d 62 69 er at most 32-bi
17b22 74 73 20 69 6e 20 73 69 7a 65 29 0a 20 20 20 20 ts in size).
17b23 2a 2a 20 61 6e 64 20 74 68 65 20 6b 65 79 20 76 ** and the key v
17b24 61 6c 75 65 20 28 61 20 76 61 72 69 61 62 6c 65 alue (a variable
17b25 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 2c length integer,
17b26 20 6d 61 79 20 68 61 76 65 20 61 6e 79 20 76 61 may have any va
17b27 6c 75 65 29 2e 0a 20 20 20 20 2a 2a 20 54 68 65 lue).. ** The
17b28 20 66 69 72 73 74 20 6f 66 20 74 68 65 20 77 68 first of the wh
17b29 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f 70 73 20 62 ile(...) loops b
17b2a 65 6c 6f 77 20 73 6b 69 70 73 20 6f 76 65 72 20 elow skips over
17b2b 74 68 65 20 72 65 63 6f 72 64 2d 6c 65 6e 67 74 the record-lengt
17b2c 68 0a 20 20 20 20 2a 2a 20 66 69 65 6c 64 2e 20 h. ** field.
17b2d 54 68 65 20 73 65 63 6f 6e 64 20 77 68 69 6c 65 The second while
17b2e 28 2e 2e 2e 29 20 6c 6f 6f 70 20 63 6f 70 69 65 (...) loop copie
17b2f 73 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20 s the key value
17b30 66 72 6f 6d 20 74 68 65 0a 20 20 20 20 2a 2a 20 from the. **
17b31 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65 20 69 6e cell on pPage in
17b32 74 6f 20 74 68 65 20 70 53 70 61 63 65 20 62 75 to the pSpace bu
17b33 66 66 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ffer.. */.
17b34 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c pCell = findCel
17b35 6c 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e l(pPage, pPage->
17b36 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20 20 20 70 53 nCell-1);. pS
17b37 74 6f 70 20 3d 20 26 70 43 65 6c 6c 5b 39 5d 3b top = &pCell[9];
17b38 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a 28 70 . while( (*(p
17b39 43 65 6c 6c 2b 2b 29 26 30 78 38 30 29 20 26 26 Cell++)&0x80) &&
17b3a 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 29 3b 0a pCell<pStop );.
17b3b 20 20 20 20 70 53 74 6f 70 20 3d 20 26 70 43 65 pStop = &pCe
17b3c 6c 6c 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 ll[9];. while
17b3d 28 20 28 28 2a 28 70 4f 75 74 2b 2b 29 20 3d 20 ( ((*(pOut++) =
17b3e 2a 28 70 43 65 6c 6c 2b 2b 29 29 26 30 78 38 30 *(pCell++))&0x80
17b3f 29 20 26 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 ) && pCell<pStop
17b40 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 65 );.. /* Inse
17b41 72 74 20 74 68 65 20 6e 65 77 20 64 69 76 69 64 rt the new divid
17b42 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20 70 50 61 er cell into pPa
17b43 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 6e 73 rent. */. ins
17b44 65 72 74 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c ertCell(pParent,
17b45 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 2c pParent->nCell,
17b46 20 70 53 70 61 63 65 2c 20 28 69 6e 74 29 28 70 pSpace, (int)(p
17b47 4f 75 74 2d 70 53 70 61 63 65 29 2c 0a 20 20 20 Out-pSpace),.
17b48 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 70 0, p
17b49 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 Page->pgno, &rc)
17b4a 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 ;.. /* Set th
17b4b 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70 6f e right-child po
17b4c 69 6e 74 65 72 20 6f 66 20 70 50 61 72 65 6e 74 inter of pParent
17b4d 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 to point to the
17b4e 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 new page. */.
17b4f 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 72 put4byte(&pPar
17b50 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 ent->aData[pPare
17b51 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d nt->hdrOffset+8]
17b52 2c 20 70 67 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20 , pgnoNew);. .
17b53 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 /* Release th
17b54 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 e reference to t
17b55 68 65 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a he new page. */.
17b56 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
17b57 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 pNew);. }.. re
17b58 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 turn rc;.}.#endi
17b59 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
17b5a 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f _QUICKBALANCE */
17b5b 0a 0a 23 69 66 20 30 0a 2f 2a 0a 2a 2a 20 54 68 ..#if 0./*.** Th
17b5c 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 is function does
17b5d 20 6e 6f 74 20 63 6f 6e 74 72 69 62 75 74 65 20 not contribute
17b5e 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20 anything to the
17b5f 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 53 51 4c operation of SQL
17b60 69 74 65 2e 0a 2a 2a 20 69 74 20 69 73 20 73 6f ite..** it is so
17b61 6d 65 74 69 6d 65 73 20 61 63 74 69 76 61 74 65 metimes activate
17b62 64 20 74 65 6d 70 6f 72 61 72 69 6c 79 20 77 68 d temporarily wh
17b63 69 6c 65 20 64 65 62 75 67 67 69 6e 67 20 63 6f ile debugging co
17b64 64 65 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 0a de responsible .
17b65 2a 2a 20 66 6f 72 20 73 65 74 74 69 6e 67 20 70 ** for setting p
17b66 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 ointer-map entri
17b67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e es..*/.static in
17b68 74 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67 t ptrmapCheckPag
17b69 65 73 28 4d 65 6d 50 61 67 65 20 2a 2a 61 70 50 es(MemPage **apP
17b6a 61 67 65 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b age, int nPage){
17b6b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 66 . int i, j;. f
17b6c 6f 72 28 69 3d 30 3b 20 69 3c 6e 50 61 67 65 3b or(i=0; i<nPage;
17b6d 20 69 2b 2b 29 7b 0a 20 20 20 20 50 67 6e 6f 20 i++){. Pgno
17b6e 6e 3b 0a 20 20 20 20 75 38 20 65 3b 0a 20 20 20 n;. u8 e;.
17b6f 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 MemPage *pPage
17b70 3d 20 61 70 50 61 67 65 5b 69 5d 3b 0a 20 20 20 = apPage[i];.
17b71 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
17b72 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 20 pPage->pBt;.
17b73 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
17b74 69 73 49 6e 69 74 20 29 3b 0a 0a 20 20 20 20 66 isInit );.. f
17b75 6f 72 28 6a 3d 30 3b 20 6a 3c 70 50 61 67 65 2d or(j=0; j<pPage-
17b76 3e 6e 43 65 6c 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 >nCell; j++){.
17b77 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 CellInfo inf
17b78 6f 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 3b 0a o;. u8 *z;.
17b79 20 20 20 20 20 0a 20 20 20 20 20 20 7a 20 3d 20 . z =
17b7a 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
17b7b 6a 29 3b 0a 20 20 20 20 20 20 62 74 72 65 65 50 j);. btreeP
17b7c 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 arseCellPtr(pPag
17b7d 65 2c 20 7a 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 e, z, &info);.
17b7e 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 if( info.iOv
17b7f 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 erflow ){.
17b80 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 Pgno ovfl = ge
17b81 74 34 62 79 74 65 28 26 7a 5b 69 6e 66 6f 2e 69 t4byte(&z[info.i
17b82 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 Overflow]);.
17b83 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42 ptrmapGet(pB
17b84 74 2c 20 6f 76 66 6c 2c 20 26 65 2c 20 26 6e 29 t, ovfl, &e, &n)
17b85 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
17b86 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f ( n==pPage->pgno
17b87 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 && e==PTRMAP_OV
17b88 45 52 46 4c 4f 57 31 20 29 3b 0a 20 20 20 20 20 ERFLOW1 );.
17b89 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70 50 }. if( !pP
17b8a 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
17b8b 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 Pgno child
17b8c 3d 20 67 65 74 34 62 79 74 65 28 7a 29 3b 0a 20 = get4byte(z);.
17b8d 20 20 20 20 20 20 20 70 74 72 6d 61 70 47 65 74 ptrmapGet
17b8e 28 70 42 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c (pBt, child, &e,
17b8f 20 26 6e 29 3b 0a 20 20 20 20 20 20 20 20 61 73 &n);. as
17b90 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e sert( n==pPage->
17b91 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 pgno && e==PTRMA
17b92 50 5f 42 54 52 45 45 20 29 3b 0a 20 20 20 20 20 P_BTREE );.
17b93 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
17b94 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
17b95 0a 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c . Pgno chil
17b96 64 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 d = get4byte(&pP
17b97 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 age->aData[pPage
17b98 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b ->hdrOffset+8]);
17b99 0a 20 20 20 20 20 20 70 74 72 6d 61 70 47 65 74 . ptrmapGet
17b9a 28 70 42 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c (pBt, child, &e,
17b9b 20 26 6e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 &n);. asse
17b9c 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 rt( n==pPage->pg
17b9d 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f no && e==PTRMAP_
17b9e 42 54 52 45 45 20 29 3b 0a 20 20 20 20 7d 0a 20 BTREE );. }.
17b9f 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d }. return 1;.}
17ba0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
17ba1 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
17ba2 75 73 65 64 20 74 6f 20 63 6f 70 79 20 74 68 65 used to copy the
17ba3 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
17ba4 20 62 2d 74 72 65 65 20 6e 6f 64 65 20 73 74 6f b-tree node sto
17ba5 72 65 64 20 0a 2a 2a 20 6f 6e 20 70 61 67 65 20 red .** on page
17ba6 70 46 72 6f 6d 20 74 6f 20 70 61 67 65 20 70 54 pFrom to page pT
17ba7 6f 2e 20 49 66 20 70 61 67 65 20 70 46 72 6f 6d o. If page pFrom
17ba8 20 77 61 73 20 6e 6f 74 20 61 20 6c 65 61 66 20 was not a leaf
17ba9 70 61 67 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 page, then.** th
17baa 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e e pointer-map en
17bab 74 72 69 65 73 20 66 6f 72 20 65 61 63 68 20 63 tries for each c
17bac 68 69 6c 64 20 70 61 67 65 20 61 72 65 20 75 70 hild page are up
17bad 64 61 74 65 64 20 73 6f 20 74 68 61 74 20 74 68 dated so that th
17bae 65 0a 2a 2a 20 70 61 72 65 6e 74 20 70 61 67 65 e.** parent page
17baf 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 70 stored in the p
17bb0 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 70 61 ointer map is pa
17bb1 67 65 20 70 54 6f 2e 20 49 66 20 70 46 72 6f 6d ge pTo. If pFrom
17bb2 20 63 6f 6e 74 61 69 6e 65 64 0a 2a 2a 20 61 6e contained.** an
17bb3 79 20 63 65 6c 6c 73 20 77 69 74 68 20 6f 76 65 y cells with ove
17bb4 72 66 6c 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 rflow page point
17bb5 65 72 73 2c 20 74 68 65 6e 20 74 68 65 20 63 6f ers, then the co
17bb6 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 6f 69 6e rresponding poin
17bb7 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 ter.** map entri
17bb8 65 73 20 61 72 65 20 61 6c 73 6f 20 75 70 64 61 es are also upda
17bb9 74 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 ted so that the
17bba 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 70 parent page is p
17bbb 61 67 65 20 70 54 6f 2e 0a 2a 2a 0a 2a 2a 20 49 age pTo..**.** I
17bbc 66 20 70 46 72 6f 6d 20 69 73 20 63 75 72 72 65 f pFrom is curre
17bbd 6e 74 6c 79 20 63 61 72 72 79 69 6e 67 20 61 6e ntly carrying an
17bbe 79 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 y overflow cells
17bbf 20 28 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 (entries in the
17bc0 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 4f 76 66 .** MemPage.aOvf
17bc1 6c 5b 5d 20 61 72 72 61 79 29 2c 20 74 68 65 79 l[] array), they
17bc2 20 61 72 65 20 6e 6f 74 20 63 6f 70 69 65 64 20 are not copied
17bc3 74 6f 20 70 54 6f 2e 20 0a 2a 2a 0a 2a 2a 20 42 to pTo. .**.** B
17bc4 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c efore returning,
17bc5 20 70 61 67 65 20 70 54 6f 20 69 73 20 72 65 69 page pTo is rei
17bc6 6e 69 74 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 nitialized using
17bc7 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 btreeInitPage()
17bc8 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 72 66 ..**.** The perf
17bc9 6f 72 6d 61 6e 63 65 20 6f 66 20 74 68 69 73 20 ormance of this
17bca 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 function is not
17bcb 63 72 69 74 69 63 61 6c 2e 20 49 74 20 69 73 20 critical. It is
17bcc 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 0a 2a 2a only used by .**
17bcd 20 74 68 65 20 62 61 6c 61 6e 63 65 5f 73 68 61 the balance_sha
17bce 6c 6c 6f 77 65 72 28 29 20 61 6e 64 20 62 61 6c llower() and bal
17bcf 61 6e 63 65 5f 64 65 65 70 65 72 28 29 20 70 72 ance_deeper() pr
17bd0 6f 63 65 64 75 72 65 73 2c 20 6e 65 69 74 68 65 ocedures, neithe
17bd1 72 20 6f 66 0a 2a 2a 20 77 68 69 63 68 20 61 72 r of.** which ar
17bd2 65 20 63 61 6c 6c 65 64 20 6f 66 74 65 6e 20 75 e called often u
17bd3 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63 69 72 63 nder normal circ
17bd4 75 6d 73 74 61 6e 63 65 73 2e 0a 2a 2f 0a 73 74 umstances..*/.st
17bd5 61 74 69 63 20 76 6f 69 64 20 63 6f 70 79 4e 6f atic void copyNo
17bd6 64 65 43 6f 6e 74 65 6e 74 28 4d 65 6d 50 61 67 deContent(MemPag
17bd7 65 20 2a 70 46 72 6f 6d 2c 20 4d 65 6d 50 61 67 e *pFrom, MemPag
17bd8 65 20 2a 70 54 6f 2c 20 69 6e 74 20 2a 70 52 43 e *pTo, int *pRC
17bd9 29 7b 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d ){. if( (*pRC)=
17bda 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
17bdb 20 20 42 74 53 68 61 72 65 64 20 2a 20 63 6f 6e BtShared * con
17bdc 73 74 20 70 42 74 20 3d 20 70 46 72 6f 6d 2d 3e st pBt = pFrom->
17bdd 70 42 74 3b 0a 20 20 20 20 75 38 20 2a 20 63 6f pBt;. u8 * co
17bde 6e 73 74 20 61 46 72 6f 6d 20 3d 20 70 46 72 6f nst aFrom = pFro
17bdf 6d 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 75 38 m->aData;. u8
17be0 20 2a 20 63 6f 6e 73 74 20 61 54 6f 20 3d 20 70 * const aTo = p
17be1 54 6f 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 To->aData;. i
17be2 6e 74 20 63 6f 6e 73 74 20 69 46 72 6f 6d 48 64 nt const iFromHd
17be3 72 20 3d 20 70 46 72 6f 6d 2d 3e 68 64 72 4f 66 r = pFrom->hdrOf
17be4 66 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 63 6f fset;. int co
17be5 6e 73 74 20 69 54 6f 48 64 72 20 3d 20 28 28 70 nst iToHdr = ((p
17be6 54 6f 2d 3e 70 67 6e 6f 3d 3d 31 29 20 3f 20 31 To->pgno==1) ? 1
17be7 30 30 20 3a 20 30 29 3b 0a 20 20 20 20 54 45 53 00 : 0);. TES
17be8 54 4f 4e 4c 59 28 69 6e 74 20 72 63 3b 29 0a 20 TONLY(int rc;).
17be9 20 20 20 69 6e 74 20 69 44 61 74 61 3b 0a 20 20 int iData;.
17bea 0a 20 20 0a 20 20 20 20 61 73 73 65 72 74 28 20 . . assert(
17beb 70 46 72 6f 6d 2d 3e 69 73 49 6e 69 74 20 29 3b pFrom->isInit );
17bec 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72 . assert( pFr
17bed 6f 6d 2d 3e 6e 46 72 65 65 3e 3d 69 54 6f 48 64 om->nFree>=iToHd
17bee 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 r );. assert(
17bef 20 67 65 74 32 62 79 74 65 28 26 61 46 72 6f 6d get2byte(&aFrom
17bf0 5b 69 46 72 6f 6d 48 64 72 2b 35 5d 29 3c 3d 70 [iFromHdr+5])<=p
17bf1 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 Bt->usableSize )
17bf2 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 43 6f 70 79 ;. . /* Copy
17bf3 20 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 64 65 the b-tree node
17bf4 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 70 61 content from pa
17bf5 67 65 20 70 46 72 6f 6d 20 74 6f 20 70 61 67 65 ge pFrom to page
17bf6 20 70 54 6f 2e 20 2a 2f 0a 20 20 20 20 69 44 61 pTo. */. iDa
17bf7 74 61 20 3d 20 67 65 74 32 62 79 74 65 28 26 61 ta = get2byte(&a
17bf8 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d From[iFromHdr+5]
17bf9 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 );. memcpy(&a
17bfa 54 6f 5b 69 44 61 74 61 5d 2c 20 26 61 46 72 6f To[iData], &aFro
17bfb 6d 5b 69 44 61 74 61 5d 2c 20 70 42 74 2d 3e 75 m[iData], pBt->u
17bfc 73 61 62 6c 65 53 69 7a 65 2d 69 44 61 74 61 29 sableSize-iData)
17bfd 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 54 ;. memcpy(&aT
17bfe 6f 5b 69 54 6f 48 64 72 5d 2c 20 26 61 46 72 6f o[iToHdr], &aFro
17bff 6d 5b 69 46 72 6f 6d 48 64 72 5d 2c 20 70 46 72 m[iFromHdr], pFr
17c00 6f 6d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b om->cellOffset +
17c01 20 32 2a 70 46 72 6f 6d 2d 3e 6e 43 65 6c 6c 29 2*pFrom->nCell)
17c02 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 69 6e ;. . /* Rein
17c03 69 74 69 61 6c 69 7a 65 20 70 61 67 65 20 70 54 itialize page pT
17c04 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f o so that the co
17c05 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 4d 65 ntents of the Me
17c06 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65 0a mPage structure.
17c07 20 20 20 20 2a 2a 20 6d 61 74 63 68 20 74 68 65 ** match the
17c08 20 6e 65 77 20 64 61 74 61 2e 20 54 68 65 20 69 new data. The i
17c09 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 nitialization of
17c0a 20 70 54 6f 20 22 63 61 6e 6e 6f 74 22 20 66 61 pTo "cannot" fa
17c0b 69 6c 2c 20 61 73 20 74 68 65 0a 20 20 20 20 2a il, as the. *
17c0c 2a 20 64 61 74 61 20 63 6f 70 69 65 64 20 66 72 * data copied fr
17c0d 6f 6d 20 70 46 72 6f 6d 20 69 73 20 6b 6e 6f 77 om pFrom is know
17c0e 6e 20 74 6f 20 62 65 20 76 61 6c 69 64 2e 20 20 n to be valid.
17c0f 2a 2f 0a 20 20 20 20 70 54 6f 2d 3e 69 73 49 6e */. pTo->isIn
17c10 69 74 20 3d 20 30 3b 0a 20 20 20 20 54 45 53 54 it = 0;. TEST
17c11 4f 4e 4c 59 28 72 63 20 3d 20 29 20 62 74 72 65 ONLY(rc = ) btre
17c12 65 49 6e 69 74 50 61 67 65 28 70 54 6f 29 3b 0a eInitPage(pTo);.
17c13 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
17c14 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 0a SQLITE_OK );. .
17c15 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 /* If this i
17c16 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d s an auto-vacuum
17c17 20 64 61 74 61 62 61 73 65 2c 20 75 70 64 61 74 database, updat
17c18 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 e the pointer-ma
17c19 70 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a p entries. **
17c1a 20 66 6f 72 20 61 6e 79 20 62 2d 74 72 65 65 20 for any b-tree
17c1b 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 or overflow page
17c1c 73 20 74 68 61 74 20 70 54 6f 20 6e 6f 77 20 63 s that pTo now c
17c1d 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69 6e ontains the poin
17c1e 74 65 72 73 20 74 6f 2e 0a 20 20 20 20 2a 2f 0a ters to.. */.
17c1f 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 if( ISAUTOVA
17c20 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 2a 70 CUUM ){. *p
17c21 52 43 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 RC = setChildPtr
17c22 6d 61 70 73 28 70 54 6f 29 3b 0a 20 20 20 20 7d maps(pTo);. }
17c23 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 . }.}../*.** Th
17c24 69 73 20 72 6f 75 74 69 6e 65 20 72 65 64 69 73 is routine redis
17c25 74 72 69 62 75 74 65 73 20 63 65 6c 6c 73 20 6f tributes cells o
17c26 6e 20 74 68 65 20 69 50 61 72 65 6e 74 49 64 78 n the iParentIdx
17c27 27 74 68 20 63 68 69 6c 64 20 6f 66 20 70 50 61 'th child of pPa
17c28 72 65 6e 74 0a 2a 2a 20 28 68 65 72 65 61 66 74 rent.** (hereaft
17c29 65 72 20 22 74 68 65 20 70 61 67 65 22 29 20 61 er "the page") a
17c2a 6e 64 20 75 70 20 74 6f 20 32 20 73 69 62 6c 69 nd up to 2 sibli
17c2b 6e 67 73 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 ngs so that all
17c2c 70 61 67 65 73 20 68 61 76 65 20 61 62 6f 75 74 pages have about
17c2d 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 6d 6f the.** same amo
17c2e 75 6e 74 20 6f 66 20 66 72 65 65 20 73 70 61 63 unt of free spac
17c2f 65 2e 20 55 73 75 61 6c 6c 79 20 61 20 73 69 6e e. Usually a sin
17c30 67 6c 65 20 73 69 62 6c 69 6e 67 20 6f 6e 20 65 gle sibling on e
17c31 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 74 68 ither side of th
17c32 65 0a 2a 2a 20 70 61 67 65 20 61 72 65 20 75 73 e.** page are us
17c33 65 64 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 ed in the balanc
17c34 69 6e 67 2c 20 74 68 6f 75 67 68 20 62 6f 74 68 ing, though both
17c35 20 73 69 62 6c 69 6e 67 73 20 6d 69 67 68 74 20 siblings might
17c36 63 6f 6d 65 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a come from one.**
17c37 20 73 69 64 65 20 69 66 20 74 68 65 20 70 61 67 side if the pag
17c38 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f e is the first o
17c39 72 20 6c 61 73 74 20 63 68 69 6c 64 20 6f 66 20 r last child of
17c3a 69 74 73 20 70 61 72 65 6e 74 2e 20 49 66 20 74 its parent. If t
17c3b 68 65 20 70 61 67 65 20 0a 2a 2a 20 68 61 73 20 he page .** has
17c3c 66 65 77 65 72 20 74 68 61 6e 20 32 20 73 69 62 fewer than 2 sib
17c3d 6c 69 6e 67 73 20 28 73 6f 6d 65 74 68 69 6e 67 lings (something
17c3e 20 77 68 69 63 68 20 63 61 6e 20 6f 6e 6c 79 20 which can only
17c3f 68 61 70 70 65 6e 20 69 66 20 74 68 65 20 70 61 happen if the pa
17c40 67 65 0a 2a 2a 20 69 73 20 61 20 72 6f 6f 74 20 ge.** is a root
17c41 70 61 67 65 20 6f 72 20 61 20 63 68 69 6c 64 20 page or a child
17c42 6f 66 20 61 20 72 6f 6f 74 20 70 61 67 65 29 20 of a root page)
17c43 74 68 65 6e 20 61 6c 6c 20 61 76 61 69 6c 61 62 then all availab
17c44 6c 65 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 70 le siblings.** p
17c45 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 74 68 articipate in th
17c46 65 20 62 61 6c 61 6e 63 69 6e 67 2e 0a 2a 2a 0a e balancing..**.
17c47 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** The number of
17c48 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 74 68 65 siblings of the
17c49 20 70 61 67 65 20 6d 69 67 68 74 20 62 65 20 69 page might be i
17c4a 6e 63 72 65 61 73 65 64 20 6f 72 20 64 65 63 72 ncreased or decr
17c4b 65 61 73 65 64 20 62 79 20 0a 2a 2a 20 6f 6e 65 eased by .** one
17c4c 20 6f 72 20 74 77 6f 20 69 6e 20 61 6e 20 65 66 or two in an ef
17c4d 66 6f 72 74 20 74 6f 20 6b 65 65 70 20 70 61 67 fort to keep pag
17c4e 65 73 20 6e 65 61 72 6c 79 20 66 75 6c 6c 20 62 es nearly full b
17c4f 75 74 20 6e 6f 74 20 6f 76 65 72 20 66 75 6c 6c ut not over full
17c50 2e 20 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 . .**.** Note th
17c51 61 74 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 at when this rou
17c52 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 tine is called,
17c53 73 6f 6d 65 20 6f 66 20 74 68 65 20 63 65 6c 6c some of the cell
17c54 73 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a s on the page.**
17c55 20 6d 69 67 68 74 20 6e 6f 74 20 61 63 74 75 61 might not actua
17c56 6c 6c 79 20 62 65 20 73 74 6f 72 65 64 20 69 6e lly be stored in
17c57 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 5b 5d MemPage.aData[]
17c58 2e 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 . This can happe
17c59 6e 0a 2a 2a 20 69 66 20 74 68 65 20 70 61 67 65 n.** if the page
17c5a 20 69 73 20 6f 76 65 72 66 75 6c 6c 2e 20 54 68 is overfull. Th
17c5b 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 73 75 72 is routine ensur
17c5c 65 73 20 74 68 61 74 20 61 6c 6c 20 63 65 6c 6c es that all cell
17c5d 73 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 74 s allocated.** t
17c5e 6f 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 69 o the page and i
17c5f 74 73 20 73 69 62 6c 69 6e 67 73 20 66 69 74 20 ts siblings fit
17c60 69 6e 74 6f 20 4d 65 6d 50 61 67 65 2e 61 44 61 into MemPage.aDa
17c61 74 61 5b 5d 20 62 65 66 6f 72 65 20 72 65 74 75 ta[] before retu
17c62 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 rning..**.** In
17c63 74 68 65 20 63 6f 75 72 73 65 20 6f 66 20 62 61 the course of ba
17c64 6c 61 6e 63 69 6e 67 20 74 68 65 20 70 61 67 65 lancing the page
17c65 20 61 6e 64 20 69 74 73 20 73 69 62 6c 69 6e 67 and its sibling
17c66 73 2c 20 63 65 6c 6c 73 20 6d 61 79 20 62 65 0a s, cells may be.
17c67 2a 2a 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f ** inserted into
17c68 20 6f 72 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d or removed from
17c69 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
17c6a 20 28 70 50 61 72 65 6e 74 29 2e 20 44 6f 69 6e (pParent). Doin
17c6b 67 20 73 6f 0a 2a 2a 20 6d 61 79 20 63 61 75 73 g so.** may caus
17c6c 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 e the parent pag
17c6d 65 20 74 6f 20 62 65 63 6f 6d 65 20 6f 76 65 72 e to become over
17c6e 66 75 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c full or underful
17c6f 6c 2e 20 49 66 20 74 68 69 73 0a 2a 2a 20 68 61 l. If this.** ha
17c70 70 70 65 6e 73 2c 20 69 74 20 69 73 20 74 68 65 ppens, it is the
17c71 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 responsibility
17c72 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f of the caller to
17c73 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f 72 72 invoke the corr
17c74 65 63 74 0a 2a 2a 20 62 61 6c 61 6e 63 69 6e 67 ect.** balancing
17c75 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 69 78 20 routine to fix
17c76 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 73 65 this problem (se
17c77 65 20 74 68 65 20 62 61 6c 61 6e 63 65 28 29 20 e the balance()
17c78 72 6f 75 74 69 6e 65 29 2e 20 0a 2a 2a 0a 2a 2a routine). .**.**
17c79 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 If this routine
17c7a 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 fails for any r
17c7b 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67 68 74 20 eason, it might
17c7c 6c 65 61 76 65 20 74 68 65 20 64 61 74 61 62 61 leave the databa
17c7d 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f 72 72 75 se.** in a corru
17c7e 70 74 65 64 20 73 74 61 74 65 2e 20 53 6f 20 69 pted state. So i
17c7f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 f this routine f
17c80 61 69 6c 73 2c 20 74 68 65 20 64 61 74 61 62 61 ails, the databa
17c81 73 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 se should.** be
17c82 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a rolled back..**.
17c83 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 67 ** The third arg
17c84 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 ument to this fu
17c85 6e 63 74 69 6f 6e 2c 20 61 4f 76 66 6c 53 70 61 nction, aOvflSpa
17c86 63 65 2c 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ce, is a pointer
17c87 20 74 6f 20 61 0a 2a 2a 20 62 75 66 66 65 72 20 to a.** buffer
17c88 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f big enough to ho
17c89 6c 64 20 6f 6e 65 20 70 61 67 65 2e 20 49 66 20 ld one page. If
17c8a 77 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 20 while inserting
17c8b 63 65 6c 6c 73 20 69 6e 74 6f 20 74 68 65 20 70 cells into the p
17c8c 61 72 65 6e 74 0a 2a 2a 20 70 61 67 65 20 28 70 arent.** page (p
17c8d 50 61 72 65 6e 74 29 20 74 68 65 20 70 61 72 65 Parent) the pare
17c8e 6e 74 20 70 61 67 65 20 62 65 63 6f 6d 65 73 20 nt page becomes
17c8f 6f 76 65 72 66 75 6c 6c 2c 20 74 68 69 73 20 62 overfull, this b
17c90 75 66 66 65 72 20 69 73 0a 2a 2a 20 75 73 65 64 uffer is.** used
17c91 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 70 61 to store the pa
17c92 72 65 6e 74 27 73 20 6f 76 65 72 66 6c 6f 77 20 rent's overflow
17c93 63 65 6c 6c 73 2e 20 42 65 63 61 75 73 65 20 74 cells. Because t
17c94 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 73 his function ins
17c95 65 72 74 73 0a 2a 2a 20 61 20 6d 61 78 69 6d 75 erts.** a maximu
17c96 6d 20 6f 66 20 66 6f 75 72 20 64 69 76 69 64 65 m of four divide
17c97 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 74 68 65 r cells into the
17c98 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 61 6e parent page, an
17c99 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a d the maximum.**
17c9a 20 73 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 size of a cell
17c9b 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20 61 6e stored within an
17c9c 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 69 internal node i
17c9d 73 20 61 6c 77 61 79 73 20 6c 65 73 73 20 74 68 s always less th
17c9e 61 6e 20 31 2f 34 0a 2a 2a 20 6f 66 20 74 68 65 an 1/4.** of the
17c9f 20 70 61 67 65 2d 73 69 7a 65 2c 20 74 68 65 20 page-size, the
17ca0 61 4f 76 66 6c 53 70 61 63 65 5b 5d 20 62 75 66 aOvflSpace[] buf
17ca1 66 65 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 fer is guarantee
17ca2 64 20 74 6f 20 62 65 20 6c 61 72 67 65 0a 2a 2a d to be large.**
17ca3 20 65 6e 6f 75 67 68 20 66 6f 72 20 61 6c 6c 20 enough for all
17ca4 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a overflow cells..
17ca5 2a 2a 0a 2a 2a 20 49 66 20 61 4f 76 66 6c 53 70 **.** If aOvflSp
17ca6 61 63 65 20 69 73 20 73 65 74 20 74 6f 20 61 20 ace is set to a
17ca7 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2c 20 74 68 null pointer, th
17ca8 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
17ca9 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e rns .** SQLITE_N
17caa 4f 4d 45 4d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 OMEM..*/.static
17cab 69 6e 74 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 int balance_nonr
17cac 6f 6f 74 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a oot(. MemPage *
17cad 70 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20 pParent,
17cae 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 /* Parent
17caf 20 70 61 67 65 20 6f 66 20 73 69 62 6c 69 6e 67 page of sibling
17cb0 73 20 62 65 69 6e 67 20 62 61 6c 61 6e 63 65 64 s being balanced
17cb1 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 65 6e */. int iParen
17cb2 74 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 tIdx,
17cb3 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
17cb4 66 20 22 74 68 65 20 70 61 67 65 22 20 69 6e 20 f "the page" in
17cb5 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 75 38 20 pParent */. u8
17cb6 2a 61 4f 76 66 6c 53 70 61 63 65 2c 20 20 20 20 *aOvflSpace,
17cb7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17cb8 70 61 67 65 2d 73 69 7a 65 20 62 79 74 65 73 20 page-size bytes
17cb9 6f 66 20 73 70 61 63 65 20 66 6f 72 20 70 61 72 of space for par
17cba 65 6e 74 20 6f 76 66 6c 20 2a 2f 0a 20 20 69 6e ent ovfl */. in
17cbb 74 20 69 73 52 6f 6f 74 20 20 20 20 20 20 20 20 t isRoot
17cbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17cbd 20 54 72 75 65 20 69 66 20 70 50 61 72 65 6e 74 True if pParent
17cbe 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 is a root-page
17cbf 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 64 */.){. BtShared
17cc0 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 *pBt;
17cc1 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c /* The whol
17cc2 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 e database */.
17cc3 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 20 int nCell = 0;
17cc4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17cc5 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 Number of cells
17cc6 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 in apCell[] */.
17cc7 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d int nMaxCells =
17cc8 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 0; /*
17cc9 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 Allocated size
17cca 6f 66 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c of apCell, szCel
17ccb 6c 2c 20 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69 l, aFrom. */. i
17ccc 6e 74 20 6e 4e 65 77 20 3d 20 30 3b 20 20 20 20 nt nNew = 0;
17ccd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
17cce 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
17ccf 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69 n apNew[] */. i
17cd0 6e 74 20 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20 nt nOld;
17cd1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
17cd2 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
17cd3 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69 n apOld[] */. i
17cd4 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20 nt i, j, k;
17cd5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
17cd6 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a oop counters */.
17cd7 20 20 69 6e 74 20 6e 78 44 69 76 3b 20 20 20 20 int nxDiv;
17cd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
17cd9 2a 20 4e 65 78 74 20 64 69 76 69 64 65 72 20 73 * Next divider s
17cda 6c 6f 74 20 69 6e 20 70 50 61 72 65 6e 74 2d 3e lot in pParent->
17cdb 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 aCell[] */. int
17cdc 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
17cdd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
17cde 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a return code */.
17cdf 20 20 75 31 36 20 6c 65 61 66 43 6f 72 72 65 63 u16 leafCorrec
17ce0 74 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 2f tion; /
17ce1 2a 20 34 20 69 66 20 70 50 61 67 65 20 69 73 20 * 4 if pPage is
17ce2 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 6e 6f a leaf. 0 if no
17ce3 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66 44 t */. int leafD
17ce4 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 ata;
17ce5 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 /* True if p
17ce6 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 20 6f Page is a leaf o
17ce7 66 20 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 f a LEAFDATA tre
17ce8 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c e */. int usabl
17ce9 65 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 20 eSpace;
17cea 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 /* Bytes in
17ceb 70 50 61 67 65 20 62 65 79 6f 6e 64 20 74 68 65 pPage beyond the
17cec 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 header */. int
17ced 20 70 61 67 65 46 6c 61 67 73 3b 20 20 20 20 20 pageFlags;
17cee 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c /* Val
17cef 75 65 20 6f 66 20 70 50 61 67 65 2d 3e 61 44 61 ue of pPage->aDa
17cf0 74 61 5b 30 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 ta[0] */. int s
17cf1 75 62 74 6f 74 61 6c 3b 20 20 20 20 20 20 20 20 ubtotal;
17cf2 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 74 6f /* Subto
17cf3 74 61 6c 20 6f 66 20 62 79 74 65 73 20 69 6e 20 tal of bytes in
17cf4 63 65 6c 6c 73 20 6f 6e 20 6f 6e 65 20 70 61 67 cells on one pag
17cf5 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 70 61 63 e */. int iSpac
17cf6 65 31 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 e1 = 0;
17cf7 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 /* First unu
17cf8 73 65 64 20 62 79 74 65 20 6f 66 20 61 53 70 61 sed byte of aSpa
17cf9 63 65 31 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 ce1[] */. int i
17cfa 4f 76 66 6c 53 70 61 63 65 20 3d 20 30 3b 20 20 OvflSpace = 0;
17cfb 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
17cfc 20 75 6e 75 73 65 64 20 62 79 74 65 20 6f 66 20 unused byte of
17cfd 61 4f 76 66 6c 53 70 61 63 65 5b 5d 20 2a 2f 0a aOvflSpace[] */.
17cfe 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b int szScratch;
17cff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
17d00 2a 20 53 69 7a 65 20 6f 66 20 73 63 72 61 74 63 * Size of scratc
17d01 68 20 6d 65 6d 6f 72 79 20 72 65 71 75 65 73 74 h memory request
17d02 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 ed */. MemPage
17d03 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20 *apOld[NB];
17d04 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61 6e /* pPage an
17d05 64 20 75 70 20 74 6f 20 74 77 6f 20 73 69 62 6c d up to two sibl
17d06 69 6e 67 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 ings */. MemPag
17d07 65 20 2a 61 70 43 6f 70 79 5b 4e 42 5d 3b 20 20 e *apCopy[NB];
17d08 20 20 20 20 20 20 20 2f 2a 20 50 72 69 76 61 74 /* Privat
17d09 65 20 63 6f 70 69 65 73 20 6f 66 20 61 70 4f 6c e copies of apOl
17d0a 64 5b 5d 20 70 61 67 65 73 20 2a 2f 0a 20 20 4d d[] pages */. M
17d0b 65 6d 50 61 67 65 20 2a 61 70 4e 65 77 5b 4e 42 emPage *apNew[NB
17d0c 2b 32 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 70 +2]; /* p
17d0d 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 4e Page and up to N
17d0e 42 20 73 69 62 6c 69 6e 67 73 20 61 66 74 65 72 B siblings after
17d0f 20 62 61 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20 20 balancing */.
17d10 75 38 20 2a 70 52 69 67 68 74 3b 20 20 20 20 20 u8 *pRight;
17d11 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17d12 4c 6f 63 61 74 69 6f 6e 20 69 6e 20 70 61 72 65 Location in pare
17d13 6e 74 20 6f 66 20 72 69 67 68 74 2d 73 69 62 6c nt of right-sibl
17d14 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 ing pointer */.
17d15 20 75 38 20 2a 61 70 44 69 76 5b 4e 42 2d 31 5d u8 *apDiv[NB-1]
17d16 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
17d17 20 44 69 76 69 64 65 72 20 63 65 6c 6c 73 20 69 Divider cells i
17d18 6e 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 69 n pParent */. i
17d19 6e 74 20 63 6e 74 4e 65 77 5b 4e 42 2b 32 5d 3b nt cntNew[NB+2];
17d1a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
17d1b 6e 64 65 78 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 ndex in aCell[]
17d1c 6f 66 20 63 65 6c 6c 20 61 66 74 65 72 20 69 2d of cell after i-
17d1d 74 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 th page */. int
17d1e 20 73 7a 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 szNew[NB+2];
17d1f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d /* Com
17d20 62 69 6e 65 64 20 73 69 7a 65 20 6f 66 20 63 65 bined size of ce
17d21 6c 6c 73 20 70 6c 61 63 65 20 6f 6e 20 69 2d 74 lls place on i-t
17d22 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a h page */. u8 *
17d23 2a 61 70 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 *apCell = 0;
17d24 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 /* All
17d25 63 65 6c 6c 73 20 62 65 67 69 6e 20 62 61 6c 61 cells begin bala
17d26 6e 63 65 64 20 2a 2f 0a 20 20 75 31 36 20 2a 73 nced */. u16 *s
17d27 7a 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 zCell;
17d28 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 /* Local
17d29 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c size of all cell
17d2a 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f s in apCell[] */
17d2b 0a 20 20 75 38 20 2a 61 53 70 61 63 65 31 3b 20 . u8 *aSpace1;
17d2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17d2d 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 63 6f 70 /* Space for cop
17d2e 69 65 73 20 6f 66 20 64 69 76 69 64 65 72 73 20 ies of dividers
17d2f 63 65 6c 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 cells */. Pgno
17d30 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 pgno;
17d31 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 /* Temp
17d32 76 61 72 20 74 6f 20 73 74 6f 72 65 20 61 20 70 var to store a p
17d33 61 67 65 20 6e 75 6d 62 65 72 20 69 6e 20 2a 2f age number in */
17d34 0a 0a 20 20 70 42 74 20 3d 20 70 50 61 72 65 6e .. pBt = pParen
17d35 74 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 t->pBt;. assert
17d36 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
17d37 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
17d38 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
17d39 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
17d3a 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 eable(pParent->p
17d3b 44 62 50 61 67 65 29 20 29 3b 0a 0a 23 69 66 20 DbPage) );..#if
17d3c 30 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 0. TRACE(("BALA
17d3d 4e 43 45 3a 20 62 65 67 69 6e 20 70 61 67 65 20 NCE: begin page
17d3e 25 64 20 63 68 69 6c 64 20 6f 66 20 25 64 5c 6e %d child of %d\n
17d3f 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 ", pPage->pgno,
17d40 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 29 29 3b pParent->pgno));
17d41 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 74 .#endif.. /* At
17d42 20 74 68 69 73 20 70 6f 69 6e 74 20 70 50 61 72 this point pPar
17d43 65 6e 74 20 6d 61 79 20 68 61 76 65 20 61 74 20 ent may have at
17d44 6d 6f 73 74 20 6f 6e 65 20 6f 76 65 72 66 6c 6f most one overflo
17d45 77 20 63 65 6c 6c 2e 20 41 6e 64 20 69 66 0a 20 w cell. And if.
17d46 20 2a 2a 20 74 68 69 73 20 6f 76 65 72 66 6c 6f ** this overflo
17d47 77 20 63 65 6c 6c 20 69 73 20 70 72 65 73 65 6e w cell is presen
17d48 74 2c 20 69 74 20 6d 75 73 74 20 62 65 20 74 68 t, it must be th
17d49 65 20 63 65 6c 6c 20 77 69 74 68 20 0a 20 20 2a e cell with . *
17d4a 2a 20 69 6e 64 65 78 20 69 50 61 72 65 6e 74 49 * index iParentI
17d4b 64 78 2e 20 54 68 69 73 20 73 63 65 6e 61 72 69 dx. This scenari
17d4c 6f 20 63 6f 6d 65 73 20 61 62 6f 75 74 20 77 68 o comes about wh
17d4d 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
17d4e 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 . ** is called
17d4f 28 69 6e 64 69 72 65 63 74 6c 79 29 20 66 72 6f (indirectly) fro
17d50 6d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 m sqlite3BtreeDe
17d51 6c 65 74 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 lete().. */. a
17d52 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e ssert( pParent->
17d53 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 nOverflow==0 ||
17d54 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c pParent->nOverfl
17d55 6f 77 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 ow==1 );. asser
17d56 74 28 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 t( pParent->nOve
17d57 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50 61 72 rflow==0 || pPar
17d58 65 6e 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 ent->aOvfl[0].id
17d59 78 3d 3d 69 50 61 72 65 6e 74 49 64 78 20 29 3b x==iParentIdx );
17d5a 0a 0a 20 20 69 66 28 20 21 61 4f 76 66 6c 53 70 .. if( !aOvflSp
17d5b 61 63 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ace ){. retur
17d5c 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
17d5d 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 }.. /* Find t
17d5e 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 he sibling pages
17d5f 20 74 6f 20 62 61 6c 61 6e 63 65 2e 20 41 6c 73 to balance. Als
17d60 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 63 65 6c o locate the cel
17d61 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 0a 20 ls in pParent .
17d62 20 2a 2a 20 74 68 61 74 20 64 69 76 69 64 65 20 ** that divide
17d63 74 68 65 20 73 69 62 6c 69 6e 67 73 2e 20 41 6e the siblings. An
17d64 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 attempt is made
17d65 20 74 6f 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c to find NN sibl
17d66 69 6e 67 73 20 6f 6e 20 0a 20 20 2a 2a 20 65 69 ings on . ** ei
17d67 74 68 65 72 20 73 69 64 65 20 6f 66 20 70 50 61 ther side of pPa
17d68 67 65 2e 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67 ge. More sibling
17d69 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d s are taken from
17d6a 20 6f 6e 65 20 73 69 64 65 2c 20 68 6f 77 65 76 one side, howev
17d6b 65 72 2c 20 0a 20 20 2a 2a 20 69 66 20 74 68 65 er, . ** if the
17d6c 72 65 20 61 72 65 20 66 65 77 65 72 20 74 68 61 re are fewer tha
17d6d 6e 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e n NN siblings on
17d6e 20 74 68 65 20 6f 74 68 65 72 20 73 69 64 65 2e the other side.
17d6f 20 49 66 20 70 50 61 72 65 6e 74 0a 20 20 2a 2a If pParent. **
17d70 20 68 61 73 20 4e 42 20 6f 72 20 66 65 77 65 72 has NB or fewer
17d71 20 63 68 69 6c 64 72 65 6e 20 74 68 65 6e 20 61 children then a
17d72 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 70 ll children of p
17d73 50 61 72 65 6e 74 20 61 72 65 20 74 61 6b 65 6e Parent are taken
17d74 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 . . **. ** Th
17d75 69 73 20 6c 6f 6f 70 20 61 6c 73 6f 20 64 72 6f is loop also dro
17d76 70 73 20 74 68 65 20 64 69 76 69 64 65 72 20 63 ps the divider c
17d77 65 6c 6c 73 20 66 72 6f 6d 20 74 68 65 20 70 61 ells from the pa
17d78 72 65 6e 74 20 70 61 67 65 2e 20 54 68 69 73 0a rent page. This.
17d79 20 20 2a 2a 20 77 61 79 2c 20 74 68 65 20 72 65 ** way, the re
17d7a 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 65 20 66 mainder of the f
17d7b 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 unction does not
17d7c 20 68 61 76 65 20 74 6f 20 64 65 61 6c 20 77 69 have to deal wi
17d7d 74 68 20 61 6e 79 0a 20 20 2a 2a 20 6f 76 65 72 th any. ** over
17d7e 66 6c 6f 77 20 63 65 6c 6c 73 20 69 6e 20 74 68 flow cells in th
17d7f 65 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 73 e parent page, s
17d80 69 6e 63 65 20 69 66 20 61 6e 79 20 65 78 69 73 ince if any exis
17d81 74 65 64 20 74 68 65 79 20 77 69 6c 6c 0a 20 20 ted they will.
17d82 2a 2a 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 ** have already
17d83 62 65 65 6e 20 72 65 6d 6f 76 65 64 2e 0a 20 20 been removed..
17d84 2a 2f 0a 20 20 69 20 3d 20 70 50 61 72 65 6e 74 */. i = pParent
17d85 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 2b 20 70 50 ->nOverflow + pP
17d86 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 arent->nCell;.
17d87 69 66 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 6e if( i<2 ){. n
17d88 78 44 69 76 20 3d 20 30 3b 0a 20 20 20 20 6e 4f xDiv = 0;. nO
17d89 6c 64 20 3d 20 69 2b 31 3b 0a 20 20 7d 65 6c 73 ld = i+1;. }els
17d8a 65 7b 0a 20 20 20 20 6e 4f 6c 64 20 3d 20 33 3b e{. nOld = 3;
17d8b 0a 20 20 20 20 69 66 28 20 69 50 61 72 65 6e 74 . if( iParent
17d8c 49 64 78 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 Idx==0 ){
17d8d 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 .
17d8e 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 20 20 20 nxDiv = 0;.
17d8f 7d 65 6c 73 65 20 69 66 28 20 69 50 61 72 65 6e }else if( iParen
17d90 74 49 64 78 3d 3d 69 20 29 7b 0a 20 20 20 20 20 tIdx==i ){.
17d91 20 6e 78 44 69 76 20 3d 20 69 2d 32 3b 0a 20 20 nxDiv = i-2;.
17d92 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e }else{. n
17d93 78 44 69 76 20 3d 20 69 50 61 72 65 6e 74 49 64 xDiv = iParentId
17d94 78 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 x-1;. }. i
17d95 20 3d 20 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = 2;. }. if(
17d96 28 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 (i+nxDiv-pParent
17d97 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 3d 3d 70 50 ->nOverflow)==pP
17d98 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a arent->nCell ){.
17d99 20 20 20 20 70 52 69 67 68 74 20 3d 20 26 70 50 pRight = &pP
17d9a 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 arent->aData[pPa
17d9b 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b rent->hdrOffset+
17d9c 38 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 8];. }else{.
17d9d 20 70 52 69 67 68 74 20 3d 20 66 69 6e 64 43 65 pRight = findCe
17d9e 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 ll(pParent, i+nx
17d9f 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 Div-pParent->nOv
17da0 65 72 66 6c 6f 77 29 3b 0a 20 20 7d 0a 20 20 70 erflow);. }. p
17da1 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 gno = get4byte(p
17da2 52 69 67 68 74 29 3b 0a 20 20 77 68 69 6c 65 28 Right);. while(
17da3 20 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 67 1 ){. rc = g
17da4 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 etAndInitPage(pB
17da5 74 2c 20 70 67 6e 6f 2c 20 26 61 70 4f 6c 64 5b t, pgno, &apOld[
17da6 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 i]);. if( rc
17da7 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 ){. memset(
17da8 61 70 4f 6c 64 2c 20 30 2c 20 28 69 2b 31 29 2a apOld, 0, (i+1)*
17da9 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 2a 29 sizeof(MemPage*)
17daa 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 61 );. goto ba
17dab 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 lance_cleanup;.
17dac 20 20 20 7d 0a 20 20 20 20 6e 4d 61 78 43 65 6c }. nMaxCel
17dad 6c 73 20 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d ls += 1+apOld[i]
17dae 2d 3e 6e 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d ->nCell+apOld[i]
17daf 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 ->nOverflow;.
17db0 20 69 66 28 20 28 69 2d 2d 29 3d 3d 30 20 29 20 if( (i--)==0 )
17db1 62 72 65 61 6b 3b 0a 0a 20 20 20 20 69 66 28 20 break;.. if(
17db2 69 2b 6e 78 44 69 76 3d 3d 70 50 61 72 65 6e 74 i+nxDiv==pParent
17db3 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 20 26 ->aOvfl[0].idx &
17db4 26 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 & pParent->nOver
17db5 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 61 70 flow ){. ap
17db6 44 69 76 5b 69 5d 20 3d 20 70 50 61 72 65 6e 74 Div[i] = pParent
17db7 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c ->aOvfl[0].pCell
17db8 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 67 ;. pgno = g
17db9 65 74 34 62 79 74 65 28 61 70 44 69 76 5b 69 5d et4byte(apDiv[i]
17dba 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 69 );. szNew[i
17dbb 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 ] = cellSizePtr(
17dbc 70 50 61 72 65 6e 74 2c 20 61 70 44 69 76 5b 69 pParent, apDiv[i
17dbd 5d 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e ]);. pParen
17dbe 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 t->nOverflow = 0
17dbf 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
17dc0 20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 66 69 apDiv[i] = fi
17dc1 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 ndCell(pParent,
17dc2 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d i+nxDiv-pParent-
17dc3 3e 6e 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 20 >nOverflow);.
17dc4 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 pgno = get4by
17dc5 74 65 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 te(apDiv[i]);.
17dc6 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 63 szNew[i] = c
17dc7 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65 ellSizePtr(pPare
17dc8 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29 3b 0a 0a nt, apDiv[i]);..
17dc9 20 20 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 /* Drop th
17dca 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20 e cell from the
17dcb 70 61 72 65 6e 74 20 70 61 67 65 2e 20 61 70 44 parent page. apD
17dcc 69 76 5b 69 5d 20 73 74 69 6c 6c 20 70 6f 69 6e iv[i] still poin
17dcd 74 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 ts to. ** t
17dce 68 65 20 63 65 6c 6c 20 77 69 74 68 69 6e 20 74 he cell within t
17dcf 68 65 20 70 61 72 65 6e 74 2c 20 65 76 65 6e 20 he parent, even
17dd0 74 68 6f 75 67 68 20 69 74 20 68 61 73 20 62 65 though it has be
17dd1 65 6e 20 64 72 6f 70 70 65 64 2e 0a 20 20 20 20 en dropped..
17dd2 20 20 2a 2a 20 54 68 69 73 20 69 73 20 73 61 66 ** This is saf
17dd3 65 20 62 65 63 61 75 73 65 20 64 72 6f 70 70 69 e because droppi
17dd4 6e 67 20 61 20 63 65 6c 6c 20 6f 6e 6c 79 20 6f ng a cell only o
17dd5 76 65 72 77 72 69 74 65 73 20 74 68 65 20 66 69 verwrites the fi
17dd6 72 73 74 0a 20 20 20 20 20 20 2a 2a 20 66 6f 75 rst. ** fou
17dd7 72 20 62 79 74 65 73 20 6f 66 20 69 74 2c 20 61 r bytes of it, a
17dd8 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e nd this function
17dd9 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
17dda 68 65 20 66 69 72 73 74 0a 20 20 20 20 20 20 2a he first. *
17ddb 2a 20 66 6f 75 72 20 62 79 74 65 73 20 6f 66 20 * four bytes of
17ddc 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c the divider cell
17ddd 2e 20 53 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 . So the pointer
17dde 20 69 73 20 73 61 66 65 20 74 6f 20 75 73 65 0a is safe to use.
17ddf 20 20 20 20 20 20 2a 2a 20 6c 61 74 65 72 20 6f ** later o
17de0 6e 2e 20 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 n. . **.
17de1 20 20 20 20 2a 2a 20 55 6e 6c 65 73 73 20 53 51 ** Unless SQ
17de2 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 Lite is compiled
17de3 20 69 6e 20 73 65 63 75 72 65 2d 64 65 6c 65 74 in secure-delet
17de4 65 20 6d 6f 64 65 2e 20 49 6e 20 74 68 69 73 20 e mode. In this
17de5 63 61 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 case,. ** t
17de6 68 65 20 64 72 6f 70 43 65 6c 6c 28 29 20 72 6f he dropCell() ro
17de7 75 74 69 6e 65 20 77 69 6c 6c 20 6f 76 65 72 77 utine will overw
17de8 72 69 74 65 20 74 68 65 20 65 6e 74 69 72 65 20 rite the entire
17de9 63 65 6c 6c 20 77 69 74 68 20 7a 65 72 6f 65 73 cell with zeroes
17dea 2e 0a 20 20 20 20 20 20 2a 2a 20 49 6e 20 74 68 .. ** In th
17deb 69 73 20 63 61 73 65 2c 20 74 65 6d 70 6f 72 61 is case, tempora
17dec 72 69 6c 79 20 63 6f 70 79 20 74 68 65 20 63 65 rily copy the ce
17ded 6c 6c 20 69 6e 74 6f 20 74 68 65 20 61 4f 76 66 ll into the aOvf
17dee 6c 53 70 61 63 65 5b 5d 0a 20 20 20 20 20 20 2a lSpace[]. *
17def 2a 20 62 75 66 66 65 72 2e 20 49 74 20 77 69 6c * buffer. It wil
17df0 6c 20 62 65 20 63 6f 70 69 65 64 20 6f 75 74 20 l be copied out
17df1 61 67 61 69 6e 20 61 73 20 73 6f 6f 6e 20 61 73 again as soon as
17df2 20 74 68 65 20 61 53 70 61 63 65 5b 5d 20 62 75 the aSpace[] bu
17df3 66 66 65 72 0a 20 20 20 20 20 20 2a 2a 20 69 73 ffer. ** is
17df4 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 2a 2f 0a allocated. */.
17df5 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 #ifdef SQLITE_SE
17df6 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 CURE_DELETE.
17df7 20 20 6d 65 6d 63 70 79 28 26 61 4f 76 66 6c 53 memcpy(&aOvflS
17df8 70 61 63 65 5b 61 70 44 69 76 5b 69 5d 2d 70 50 pace[apDiv[i]-pP
17df9 61 72 65 6e 74 2d 3e 61 44 61 74 61 5d 2c 20 61 arent->aData], a
17dfa 70 44 69 76 5b 69 5d 2c 20 73 7a 4e 65 77 5b 69 pDiv[i], szNew[i
17dfb 5d 29 3b 0a 20 20 20 20 20 20 61 70 44 69 76 5b ]);. apDiv[
17dfc 69 5d 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 i] = &aOvflSpace
17dfd 5b 61 70 44 69 76 5b 69 5d 2d 70 50 61 72 65 6e [apDiv[i]-pParen
17dfe 74 2d 3e 61 44 61 74 61 5d 3b 0a 23 65 6e 64 69 t->aData];.#endi
17dff 66 0a 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c f. dropCell
17e00 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69 (pParent, i+nxDi
17e01 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 v-pParent->nOver
17e02 66 6c 6f 77 2c 20 73 7a 4e 65 77 5b 69 5d 2c 20 flow, szNew[i],
17e03 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a &rc);. }. }.
17e04 0a 20 20 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43 . /* Make nMaxC
17e05 65 6c 6c 73 20 61 20 6d 75 6c 74 69 70 6c 65 20 ells a multiple
17e06 6f 66 20 34 20 69 6e 20 6f 72 64 65 72 20 74 6f of 4 in order to
17e07 20 70 72 65 73 65 72 76 65 20 38 2d 62 79 74 65 preserve 8-byte
17e08 0a 20 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20 . ** alignment
17e09 2a 2f 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d */. nMaxCells =
17e0a 20 28 6e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29 (nMaxCells + 3)
17e0b 26 7e 33 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 &~3;.. /*. **
17e0c 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 Allocate space f
17e0d 6f 72 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 or memory struct
17e0e 75 72 65 73 0a 20 20 2a 2f 0a 20 20 6b 20 3d 20 ures. */. k =
17e0f 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 pBt->pageSize +
17e10 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 ROUND8(sizeof(Me
17e11 6d 50 61 67 65 29 29 3b 0a 20 20 73 7a 53 63 72 mPage));. szScr
17e12 61 74 63 68 20 3d 0a 20 20 20 20 20 20 20 6e 4d atch =. nM
17e13 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 axCells*sizeof(u
17e14 38 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20 8*)
17e15 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 70 43 /* apC
17e16 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d ell */. + nM
17e17 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 axCells*sizeof(u
17e18 31 36 29 20 20 20 20 20 20 20 20 20 20 20 20 20 16)
17e19 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 43 /* szC
17e1a 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42 ell */. + pB
17e1b 74 2d 3e 70 61 67 65 53 69 7a 65 20 20 20 20 20 t->pageSize
17e1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17e1d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70 /* aSp
17e1e 61 63 65 31 20 2a 2f 0a 20 20 20 20 20 2b 20 6b ace1 */. + k
17e1f 2a 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20 *nOld;
17e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17e21 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
17e22 67 65 20 63 6f 70 69 65 73 20 28 61 70 43 6f 70 ge copies (apCop
17e23 79 29 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d y) */. apCell =
17e24 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d sqlite3ScratchM
17e25 61 6c 6c 6f 63 28 20 73 7a 53 63 72 61 74 63 68 alloc( szScratch
17e26 20 29 3b 20 0a 20 20 69 66 28 20 61 70 43 65 6c ); . if( apCel
17e27 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d l==0 ){. rc =
17e28 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
17e29 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f goto balance_
17e2a 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73 cleanup;. }. s
17e2b 7a 43 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61 zCell = (u16*)&a
17e2c 70 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d pCell[nMaxCells]
17e2d 3b 0a 20 20 61 53 70 61 63 65 31 20 3d 20 28 75 ;. aSpace1 = (u
17e2e 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43 8*)&szCell[nMaxC
17e2f 65 6c 6c 73 5d 3b 0a 20 20 61 73 73 65 72 74 28 ells];. assert(
17e30 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 EIGHT_BYTE_ALIG
17e31 4e 4d 45 4e 54 28 61 53 70 61 63 65 31 29 20 29 NMENT(aSpace1) )
17e32 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 6f 61 ;.. /*. ** Loa
17e33 64 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c d pointers to al
17e34 6c 20 63 65 6c 6c 73 20 6f 6e 20 73 69 62 6c 69 l cells on sibli
17e35 6e 67 20 70 61 67 65 73 20 61 6e 64 20 74 68 65 ng pages and the
17e36 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 0a 20 divider cells.
17e37 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6c 6f 63 ** into the loc
17e38 61 6c 20 61 70 43 65 6c 6c 5b 5d 20 61 72 72 61 al apCell[] arra
17e39 79 2e 20 20 4d 61 6b 65 20 63 6f 70 69 65 73 20 y. Make copies
17e3a 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20 63 of the divider c
17e3b 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 73 ells. ** into s
17e3c 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 pace obtained fr
17e3d 6f 6d 20 61 53 70 61 63 65 31 5b 5d 20 61 6e 64 om aSpace1[] and
17e3e 20 72 65 6d 6f 76 65 20 74 68 65 20 74 68 65 20 remove the the
17e3f 64 69 76 69 64 65 72 20 43 65 6c 6c 73 0a 20 20 divider Cells.
17e40 2a 2a 20 66 72 6f 6d 20 70 50 61 72 65 6e 74 2e ** from pParent.
17e41 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 . **. ** If th
17e42 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6f e siblings are o
17e43 6e 20 6c 65 61 66 20 70 61 67 65 73 2c 20 74 68 n leaf pages, th
17e44 65 6e 20 74 68 65 20 63 68 69 6c 64 20 70 6f 69 en the child poi
17e45 6e 74 65 72 73 20 6f 66 20 74 68 65 0a 20 20 2a nters of the. *
17e46 2a 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 20 * divider cells
17e47 61 72 65 20 73 74 72 69 70 70 65 64 20 66 72 6f are stripped fro
17e48 6d 20 74 68 65 20 63 65 6c 6c 73 20 62 65 66 6f m the cells befo
17e49 72 65 20 74 68 65 79 20 61 72 65 20 63 6f 70 69 re they are copi
17e4a 65 64 0a 20 20 2a 2a 20 69 6e 74 6f 20 61 53 70 ed. ** into aSp
17e4b 61 63 65 31 5b 5d 2e 20 20 49 6e 20 74 68 69 73 ace1[]. In this
17e4c 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 way, all cells
17e4d 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 72 65 20 in apCell[] are
17e4e 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 63 68 69 without. ** chi
17e4f 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20 20 49 66 ld pointers. If
17e50 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f siblings are no
17e51 74 20 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 61 t leaves, then a
17e52 6c 6c 20 63 65 6c 6c 20 69 6e 0a 20 20 2a 2a 20 ll cell in. **
17e53 61 70 43 65 6c 6c 5b 5d 20 69 6e 63 6c 75 64 65 apCell[] include
17e54 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e child pointers.
17e55 20 20 45 69 74 68 65 72 20 77 61 79 2c 20 61 6c Either way, al
17e56 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c l cells in apCel
17e57 6c 5b 5d 0a 20 20 2a 2a 20 61 72 65 20 61 6c 69 l[]. ** are ali
17e58 6b 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 6c 65 ke.. **. ** le
17e59 61 66 43 6f 72 72 65 63 74 69 6f 6e 3a 20 20 34 afCorrection: 4
17e5a 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20 6c if pPage is a l
17e5b 65 61 66 2e 20 20 30 20 69 66 20 70 50 61 67 65 eaf. 0 if pPage
17e5c 20 69 73 20 6e 6f 74 20 61 20 6c 65 61 66 2e 0a is not a leaf..
17e5d 20 20 2a 2a 20 20 20 20 20 20 20 6c 65 61 66 44 ** leafD
17e5e 61 74 61 3a 20 20 31 20 69 66 20 70 50 61 67 65 ata: 1 if pPage
17e5f 20 68 6f 6c 64 73 20 6b 65 79 2b 64 61 74 61 20 holds key+data
17e60 61 6e 64 20 70 50 61 72 65 6e 74 20 68 6f 6c 64 and pParent hold
17e61 73 20 6f 6e 6c 79 20 6b 65 79 73 2e 0a 20 20 2a s only keys.. *
17e62 2f 0a 20 20 6c 65 61 66 43 6f 72 72 65 63 74 69 /. leafCorrecti
17e63 6f 6e 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 6c on = apOld[0]->l
17e64 65 61 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 eaf*4;. leafDat
17e65 61 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 68 61 a = apOld[0]->ha
17e66 73 44 61 74 61 3b 0a 20 20 66 6f 72 28 69 3d 30 sData;. for(i=0
17e67 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a ; i<nOld; i++){.
17e68 20 20 20 20 69 6e 74 20 6c 69 6d 69 74 3b 0a 20 int limit;.
17e69 20 20 20 0a 20 20 20 20 2f 2a 20 42 65 66 6f 72 . /* Befor
17e6a 65 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 e doing anything
17e6b 20 65 6c 73 65 2c 20 74 61 6b 65 20 61 20 63 6f else, take a co
17e6c 70 79 20 6f 66 20 74 68 65 20 69 27 74 68 20 6f py of the i'th o
17e6d 72 69 67 69 6e 61 6c 20 73 69 62 6c 69 6e 67 0a riginal sibling.
17e6e 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73 74 20 ** The rest
17e6f 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e of this function
17e70 20 77 69 6c 6c 20 75 73 65 20 64 61 74 61 20 66 will use data f
17e71 72 6f 6d 20 74 68 65 20 63 6f 70 69 65 73 20 72 rom the copies r
17e72 61 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 ather. ** tha
17e73 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 t the original p
17e74 61 67 65 73 20 73 69 6e 63 65 20 74 68 65 20 6f ages since the o
17e75 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 77 69 riginal pages wi
17e76 6c 6c 20 62 65 20 69 6e 20 74 68 65 0a 20 20 20 ll be in the.
17e77 20 2a 2a 20 70 72 6f 63 65 73 73 20 6f 66 20 62 ** process of b
17e78 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e eing overwritten
17e79 2e 20 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 . */. MemPag
17e7a 65 20 2a 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 e *pOld = apCopy
17e7b 5b 69 5d 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 [i] = (MemPage*)
17e7c 26 61 53 70 61 63 65 31 5b 70 42 74 2d 3e 70 61 &aSpace1[pBt->pa
17e7d 67 65 53 69 7a 65 20 2b 20 6b 2a 69 5d 3b 0a 20 geSize + k*i];.
17e7e 20 20 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2c 20 memcpy(pOld,
17e7f 61 70 4f 6c 64 5b 69 5d 2c 20 73 69 7a 65 6f 66 apOld[i], sizeof
17e80 28 4d 65 6d 50 61 67 65 29 29 3b 0a 20 20 20 20 (MemPage));.
17e81 70 4f 6c 64 2d 3e 61 44 61 74 61 20 3d 20 28 76 pOld->aData = (v
17e82 6f 69 64 2a 29 26 70 4f 6c 64 5b 31 5d 3b 0a 20 oid*)&pOld[1];.
17e83 20 20 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2d 3e memcpy(pOld->
17e84 61 44 61 74 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d aData, apOld[i]-
17e85 3e 61 44 61 74 61 2c 20 70 42 74 2d 3e 70 61 67 >aData, pBt->pag
17e86 65 53 69 7a 65 29 3b 0a 0a 20 20 20 20 6c 69 6d eSize);.. lim
17e87 69 74 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c it = pOld->nCell
17e88 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 +pOld->nOverflow
17e89 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a ;. for(j=0; j
17e8a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29 7b 0a 20 20 <limit; j++){.
17e8b 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c assert( nCel
17e8c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 l<nMaxCells );.
17e8d 20 20 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65 6c apCell[nCel
17e8e 6c 5d 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c 6f l] = findOverflo
17e8f 77 43 65 6c 6c 28 70 4f 6c 64 2c 20 6a 29 3b 0a wCell(pOld, j);.
17e90 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 szCell[nCe
17e91 6c 6c 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 ll] = cellSizePt
17e92 72 28 70 4f 6c 64 2c 20 61 70 43 65 6c 6c 5b 6e r(pOld, apCell[n
17e93 43 65 6c 6c 5d 29 3b 0a 20 20 20 20 20 20 6e 43 Cell]);. nC
17e94 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 ell++;. }.
17e95 20 69 66 28 20 69 3c 6e 4f 6c 64 2d 31 20 26 26 if( i<nOld-1 &&
17e96 20 21 6c 65 61 66 44 61 74 61 29 7b 0a 20 20 20 !leafData){.
17e97 20 20 20 75 31 36 20 73 7a 20 3d 20 28 75 31 36 u16 sz = (u16
17e98 29 73 7a 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 )szNew[i];.
17e99 20 75 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 u8 *pTemp;.
17e9a 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c assert( nCell<
17e9b 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 nMaxCells );.
17e9c 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d szCell[nCell]
17e9d 20 3d 20 73 7a 3b 0a 20 20 20 20 20 20 70 54 65 = sz;. pTe
17e9e 6d 70 20 3d 20 26 61 53 70 61 63 65 31 5b 69 53 mp = &aSpace1[iS
17e9f 70 61 63 65 31 5d 3b 0a 20 20 20 20 20 20 69 53 pace1];. iS
17ea0 70 61 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20 pace1 += sz;.
17ea1 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d 70 assert( sz<=p
17ea2 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 29 Bt->pageSize/4 )
17ea3 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
17ea4 69 53 70 61 63 65 31 3c 3d 70 42 74 2d 3e 70 61 iSpace1<=pBt->pa
17ea5 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 geSize );.
17ea6 6d 65 6d 63 70 79 28 70 54 65 6d 70 2c 20 61 70 memcpy(pTemp, ap
17ea7 44 69 76 5b 69 5d 2c 20 73 7a 29 3b 0a 20 20 20 Div[i], sz);.
17ea8 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d apCell[nCell]
17ea9 20 3d 20 70 54 65 6d 70 2b 6c 65 61 66 43 6f 72 = pTemp+leafCor
17eaa 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 61 rection;. a
17eab 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65 ssert( leafCorre
17eac 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20 6c 65 61 66 ction==0 || leaf
17ead 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b Correction==4 );
17eae 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 . szCell[nC
17eaf 65 6c 6c 5d 20 3d 20 73 7a 43 65 6c 6c 5b 6e 43 ell] = szCell[nC
17eb0 65 6c 6c 5d 20 2d 20 6c 65 61 66 43 6f 72 72 65 ell] - leafCorre
17eb1 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 ction;. if(
17eb2 20 21 70 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a !pOld->leaf ){.
17eb3 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
17eb4 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d leafCorrection==
17eb5 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 0 );. ass
17eb6 65 72 74 28 20 70 4f 6c 64 2d 3e 68 64 72 4f 66 ert( pOld->hdrOf
17eb7 66 73 65 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 fset==0 );.
17eb8 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 20 /* The right
17eb9 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 63 pointer of the c
17eba 68 69 6c 64 20 70 61 67 65 20 70 4f 6c 64 20 62 hild page pOld b
17ebb 65 63 6f 6d 65 73 20 74 68 65 20 6c 65 66 74 0a ecomes the left.
17ebc 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 ** point
17ebd 65 72 20 6f 66 20 74 68 65 20 64 69 76 69 64 65 er of the divide
17ebe 72 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 r cell */.
17ebf 20 20 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b memcpy(apCell[
17ec0 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61 nCell], &pOld->a
17ec1 44 61 74 61 5b 38 5d 2c 20 34 29 3b 0a 20 20 20 Data[8], 4);.
17ec2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
17ec3 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f assert( leafCo
17ec4 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20 rrection==4 );.
17ec5 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c if( szCel
17ec6 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20 20 l[nCell]<4 ){.
17ec7 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f /* Do no
17ec8 74 20 61 6c 6c 6f 77 20 61 6e 79 20 63 65 6c 6c t allow any cell
17ec9 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 34 s smaller than 4
17eca 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 20 20 20 bytes. */.
17ecb 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c szCell[nCel
17ecc 6c 5d 20 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 l] = 4;.
17ecd 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
17ece 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 nCell++;. }.
17ecf 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 }.. /*. ** Fi
17ed0 67 75 72 65 20 6f 75 74 20 74 68 65 20 6e 75 6d gure out the num
17ed1 62 65 72 20 6f 66 20 70 61 67 65 73 20 6e 65 65 ber of pages nee
17ed2 64 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 ded to hold all
17ed3 6e 43 65 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20 2a nCell cells.. *
17ed4 2a 20 53 74 6f 72 65 20 74 68 69 73 20 6e 75 6d * Store this num
17ed5 62 65 72 20 69 6e 20 22 6b 22 2e 20 20 41 6c 73 ber in "k". Als
17ed6 6f 20 63 6f 6d 70 75 74 65 20 73 7a 4e 65 77 5b o compute szNew[
17ed7 5d 20 77 68 69 63 68 20 69 73 20 74 68 65 20 74 ] which is the t
17ed8 6f 74 61 6c 0a 20 20 2a 2a 20 73 69 7a 65 20 6f otal. ** size o
17ed9 66 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 74 f all cells on t
17eda 68 65 20 69 2d 74 68 20 70 61 67 65 20 61 6e 64 he i-th page and
17edb 20 63 6e 74 4e 65 77 5b 5d 20 77 68 69 63 68 20 cntNew[] which
17edc 69 73 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a is the index. *
17edd 2a 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 6f 66 * in apCell[] of
17ede 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 64 the cell that d
17edf 69 76 69 64 65 73 20 70 61 67 65 20 69 20 66 72 ivides page i fr
17ee0 6f 6d 20 70 61 67 65 20 69 2b 31 2e 20 20 0a 20 om page i+1. .
17ee1 20 2a 2a 20 63 6e 74 4e 65 77 5b 6b 5d 20 73 68 ** cntNew[k] sh
17ee2 6f 75 6c 64 20 65 71 75 61 6c 20 6e 43 65 6c 6c ould equal nCell
17ee3 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 6c 75 .. **. ** Valu
17ee4 65 73 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74 es computed by t
17ee5 68 69 73 20 62 6c 6f 63 6b 3a 0a 20 20 2a 2a 0a his block:. **.
17ee6 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6b ** k
17ee7 3a 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 : The total numb
17ee8 65 72 20 6f 66 20 73 69 62 6c 69 6e 67 20 70 61 er of sibling pa
17ee9 67 65 73 0a 20 20 2a 2a 20 20 20 20 73 7a 4e 65 ges. ** szNe
17eea 77 5b 69 5d 3a 20 53 70 61 63 65 64 20 75 73 65 w[i]: Spaced use
17eeb 64 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 73 69 d on the i-th si
17eec 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a bling page.. **
17eed 20 20 20 63 6e 74 4e 65 77 5b 69 5d 3a 20 49 6e cntNew[i]: In
17eee 64 65 78 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 dex in apCell[]
17eef 61 6e 64 20 73 7a 43 65 6c 6c 5b 5d 20 66 6f 72 and szCell[] for
17ef0 20 74 68 65 20 66 69 72 73 74 20 63 65 6c 6c 20 the first cell
17ef1 74 6f 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 to. **
17ef2 20 20 20 20 20 74 68 65 20 72 69 67 68 74 20 6f the right o
17ef3 66 20 74 68 65 20 69 2d 74 68 20 73 69 62 6c 69 f the i-th sibli
17ef4 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 75 73 ng page.. ** us
17ef5 61 62 6c 65 53 70 61 63 65 3a 20 4e 75 6d 62 65 ableSpace: Numbe
17ef6 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 r of bytes of sp
17ef7 61 63 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e ace available on
17ef8 20 65 61 63 68 20 73 69 62 6c 69 6e 67 2e 0a 20 each sibling..
17ef9 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20 75 73 61 62 ** . */. usab
17efa 6c 65 53 70 61 63 65 20 3d 20 70 42 74 2d 3e 75 leSpace = pBt->u
17efb 73 61 62 6c 65 53 69 7a 65 20 2d 20 31 32 20 2b sableSize - 12 +
17efc 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b leafCorrection;
17efd 0a 20 20 66 6f 72 28 73 75 62 74 6f 74 61 6c 3d . for(subtotal=
17efe 6b 3d 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 k=i=0; i<nCell;
17eff 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 i++){. assert
17f00 28 20 69 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b ( i<nMaxCells );
17f01 0a 20 20 20 20 73 75 62 74 6f 74 61 6c 20 2b 3d . subtotal +=
17f02 20 73 7a 43 65 6c 6c 5b 69 5d 20 2b 20 32 3b 0a szCell[i] + 2;.
17f03 20 20 20 20 69 66 28 20 73 75 62 74 6f 74 61 6c if( subtotal
17f04 20 3e 20 75 73 61 62 6c 65 53 70 61 63 65 20 29 > usableSpace )
17f05 7b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 6b 5d {. szNew[k]
17f06 20 3d 20 73 75 62 74 6f 74 61 6c 20 2d 20 73 7a = subtotal - sz
17f07 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 63 Cell[i];. c
17f08 6e 74 4e 65 77 5b 6b 5d 20 3d 20 69 3b 0a 20 20 ntNew[k] = i;.
17f09 20 20 20 20 69 66 28 20 6c 65 61 66 44 61 74 61 if( leafData
17f0a 20 29 7b 20 69 2d 2d 3b 20 7d 0a 20 20 20 20 20 ){ i--; }.
17f0b 20 73 75 62 74 6f 74 61 6c 20 3d 20 30 3b 0a 20 subtotal = 0;.
17f0c 20 20 20 20 20 6b 2b 2b 3b 0a 20 20 20 20 20 20 k++;.
17f0d 69 66 28 20 6b 3e 4e 42 2b 31 20 29 7b 20 72 63 if( k>NB+1 ){ rc
17f0e 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
17f0f 54 3b 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f T; goto balance_
17f10 63 6c 65 61 6e 75 70 3b 20 7d 0a 20 20 20 20 7d cleanup; }. }
17f11 0a 20 20 7d 0a 20 20 73 7a 4e 65 77 5b 6b 5d 20 . }. szNew[k]
17f12 3d 20 73 75 62 74 6f 74 61 6c 3b 0a 20 20 63 6e = subtotal;. cn
17f13 74 4e 65 77 5b 6b 5d 20 3d 20 6e 43 65 6c 6c 3b tNew[k] = nCell;
17f14 0a 20 20 6b 2b 2b 3b 0a 0a 20 20 2f 2a 0a 20 20 . k++;.. /*.
17f15 2a 2a 20 54 68 65 20 70 61 63 6b 69 6e 67 20 63 ** The packing c
17f16 6f 6d 70 75 74 65 64 20 62 79 20 74 68 65 20 70 omputed by the p
17f17 72 65 76 69 6f 75 73 20 62 6c 6f 63 6b 20 69 73 revious block is
17f18 20 62 69 61 73 65 64 20 74 6f 77 61 72 64 20 74 biased toward t
17f19 68 65 20 73 69 62 6c 69 6e 67 73 0a 20 20 2a 2a he siblings. **
17f1a 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 73 69 64 on the left sid
17f1b 65 2e 20 20 54 68 65 20 6c 65 66 74 20 73 69 62 e. The left sib
17f1c 6c 69 6e 67 73 20 61 72 65 20 61 6c 77 61 79 73 lings are always
17f1d 20 6e 65 61 72 6c 79 20 66 75 6c 6c 2c 20 77 68 nearly full, wh
17f1e 69 6c 65 20 74 68 65 0a 20 20 2a 2a 20 72 69 67 ile the. ** rig
17f1f 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 20 ht-most sibling
17f20 6d 69 67 68 74 20 62 65 20 6e 65 61 72 6c 79 20 might be nearly
17f21 65 6d 70 74 79 2e 20 20 54 68 69 73 20 62 6c 6f empty. This blo
17f22 63 6b 20 6f 66 20 63 6f 64 65 20 61 74 74 65 6d ck of code attem
17f23 70 74 73 0a 20 20 2a 2a 20 74 6f 20 61 64 6a 75 pts. ** to adju
17f24 73 74 20 74 68 65 20 70 61 63 6b 69 6e 67 20 6f st the packing o
17f25 66 20 73 69 62 6c 69 6e 67 73 20 74 6f 20 67 65 f siblings to ge
17f26 74 20 61 20 62 65 74 74 65 72 20 62 61 6c 61 6e t a better balan
17f27 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 ce.. **. ** Th
17f28 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 is adjustment is
17f29 20 6d 6f 72 65 20 74 68 61 6e 20 61 6e 20 6f 70 more than an op
17f2a 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 timization. The
17f2b 20 70 61 63 6b 69 6e 67 20 61 62 6f 76 65 20 6d packing above m
17f2c 69 67 68 74 0a 20 20 2a 2a 20 62 65 20 73 6f 20 ight. ** be so
17f2d 6f 75 74 20 6f 66 20 62 61 6c 61 6e 63 65 20 61 out of balance a
17f2e 73 20 74 6f 20 62 65 20 69 6c 6c 65 67 61 6c 2e s to be illegal.
17f2f 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 For example, t
17f30 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20 he right-most.
17f31 2a 2a 20 73 69 62 6c 69 6e 67 20 6d 69 67 68 74 ** sibling might
17f32 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 be completely e
17f33 6d 70 74 79 2e 20 20 54 68 69 73 20 61 64 6a 75 mpty. This adju
17f34 73 74 6d 65 6e 74 20 69 73 20 6e 6f 74 20 6f 70 stment is not op
17f35 74 69 6f 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 66 tional.. */. f
17f36 6f 72 28 69 3d 6b 2d 31 3b 20 69 3e 30 3b 20 69 or(i=k-1; i>0; i
17f37 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 73 7a 52 --){. int szR
17f38 69 67 68 74 20 3d 20 73 7a 4e 65 77 5b 69 5d 3b ight = szNew[i];
17f39 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 /* Size of sib
17f3a 6c 69 6e 67 20 6f 6e 20 74 68 65 20 72 69 67 68 ling on the righ
17f3b 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 4c t */. int szL
17f3c 65 66 74 20 3d 20 73 7a 4e 65 77 5b 69 2d 31 5d eft = szNew[i-1]
17f3d 3b 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 ; /* Size of sib
17f3e 6c 69 6e 67 20 6f 6e 20 74 68 65 20 6c 65 66 74 ling on the left
17f3f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 3b 20 20 */. int r;
17f40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
17f41 6e 64 65 78 20 6f 66 20 72 69 67 68 74 2d 6d 6f ndex of right-mo
17f42 73 74 20 63 65 6c 6c 20 69 6e 20 6c 65 66 74 20 st cell in left
17f43 73 69 62 6c 69 6e 67 20 2a 2f 0a 20 20 20 20 69 sibling */. i
17f44 6e 74 20 64 3b 20 20 20 20 20 20 20 20 20 20 20 nt d;
17f45 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 /* Index of f
17f46 69 72 73 74 20 63 65 6c 6c 20 74 6f 20 74 68 65 irst cell to the
17f47 20 6c 65 66 74 20 6f 66 20 72 69 67 68 74 20 73 left of right s
17f48 69 62 6c 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 72 ibling */.. r
17f49 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d = cntNew[i-1] -
17f4a 20 31 3b 0a 20 20 20 20 64 20 3d 20 72 20 2b 20 1;. d = r +
17f4b 31 20 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 1 - leafData;.
17f4c 20 20 61 73 73 65 72 74 28 20 64 3c 6e 4d 61 78 assert( d<nMax
17f4d 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73 73 Cells );. ass
17f4e 65 72 74 28 20 72 3c 6e 4d 61 78 43 65 6c 6c 73 ert( r<nMaxCells
17f4f 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 );. while( s
17f50 7a 52 69 67 68 74 3d 3d 30 20 7c 7c 20 73 7a 52 zRight==0 || szR
17f51 69 67 68 74 2b 73 7a 43 65 6c 6c 5b 64 5d 2b 32 ight+szCell[d]+2
17f52 3c 3d 73 7a 4c 65 66 74 2d 28 73 7a 43 65 6c 6c <=szLeft-(szCell
17f53 5b 72 5d 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 [r]+2) ){.
17f54 73 7a 52 69 67 68 74 20 2b 3d 20 73 7a 43 65 6c szRight += szCel
17f55 6c 5b 64 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 l[d] + 2;.
17f56 73 7a 4c 65 66 74 20 2d 3d 20 73 7a 43 65 6c 6c szLeft -= szCell
17f57 5b 72 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 63 [r] + 2;. c
17f58 6e 74 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20 20 ntNew[i-1]--;.
17f59 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 r = cntNew[i
17f5a 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 64 -1] - 1;. d
17f5b 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 = r + 1 - leafD
17f5c 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 ata;. }. s
17f5d 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67 68 zNew[i] = szRigh
17f5e 74 3b 0a 20 20 20 20 73 7a 4e 65 77 5b 69 2d 31 t;. szNew[i-1
17f5f 5d 20 3d 20 73 7a 4c 65 66 74 3b 0a 20 20 7d 0a ] = szLeft;. }.
17f60 0a 20 20 2f 2a 20 45 69 74 68 65 72 20 77 65 20 . /* Either we
17f61 66 6f 75 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 found one or mor
17f62 65 20 63 65 6c 6c 73 20 28 63 6e 74 6e 65 77 5b e cells (cntnew[
17f63 30 5d 29 3e 30 29 20 6f 72 20 70 50 61 67 65 20 0])>0) or pPage
17f64 69 73 0a 20 20 2a 2a 20 61 20 76 69 72 74 75 61 is. ** a virtua
17f65 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 41 20 l root page. A
17f66 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 virtual root pag
17f67 65 20 69 73 20 77 68 65 6e 20 74 68 65 20 72 65 e is when the re
17f68 61 6c 20 72 6f 6f 74 0a 20 20 2a 2a 20 70 61 67 al root. ** pag
17f69 65 20 69 73 20 70 61 67 65 20 31 20 61 6e 64 20 e is page 1 and
17f6a 77 65 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 20 we are the only
17f6b 63 68 69 6c 64 20 6f 66 20 74 68 61 74 20 70 61 child of that pa
17f6c 67 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ge.. */. asser
17f6d 74 28 20 63 6e 74 4e 65 77 5b 30 5d 3e 30 20 7c t( cntNew[0]>0 |
17f6e 7c 20 28 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f | (pParent->pgno
17f6f 3d 3d 31 20 26 26 20 70 50 61 72 65 6e 74 2d 3e ==1 && pParent->
17f70 6e 43 65 6c 6c 3d 3d 30 29 20 29 3b 0a 0a 20 20 nCell==0) );..
17f71 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a TRACE(("BALANCE:
17f72 20 6f 6c 64 3a 20 25 64 20 25 64 20 25 64 20 20 old: %d %d %d
17f73 22 2c 0a 20 20 20 20 61 70 4f 6c 64 5b 30 5d 2d ",. apOld[0]-
17f74 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 6e 4f 6c 64 >pgno, . nOld
17f75 3e 3d 32 20 3f 20 61 70 4f 6c 64 5b 31 5d 2d 3e >=2 ? apOld[1]->
17f76 70 67 6e 6f 20 3a 20 30 2c 0a 20 20 20 20 6e 4f pgno : 0,. nO
17f77 6c 64 3e 3d 33 20 3f 20 61 70 4f 6c 64 5b 32 5d ld>=3 ? apOld[2]
17f78 2d 3e 70 67 6e 6f 20 3a 20 30 0a 20 20 29 29 3b ->pgno : 0. ));
17f79 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f .. /*. ** Allo
17f7a 63 61 74 65 20 6b 20 6e 65 77 20 70 61 67 65 73 cate k new pages
17f7b 2e 20 20 52 65 75 73 65 20 6f 6c 64 20 70 61 67 . Reuse old pag
17f7c 65 73 20 77 68 65 72 65 20 70 6f 73 73 69 62 6c es where possibl
17f7d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 70 e.. */. if( ap
17f7e 4f 6c 64 5b 30 5d 2d 3e 70 67 6e 6f 3c 3d 31 20 Old[0]->pgno<=1
17f7f 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
17f80 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 20 20 TE_CORRUPT;.
17f81 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 goto balance_cle
17f82 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 61 67 65 anup;. }. page
17f83 46 6c 61 67 73 20 3d 20 61 70 4f 6c 64 5b 30 5d Flags = apOld[0]
17f84 2d 3e 61 44 61 74 61 5b 30 5d 3b 0a 20 20 66 6f ->aData[0];. fo
17f85 72 28 69 3d 30 3b 20 69 3c 6b 3b 20 69 2b 2b 29 r(i=0; i<k; i++)
17f86 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 {. MemPage *p
17f87 4e 65 77 3b 0a 20 20 20 20 69 66 28 20 69 3c 6e New;. if( i<n
17f88 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 4e 65 Old ){. pNe
17f89 77 20 3d 20 61 70 4e 65 77 5b 69 5d 20 3d 20 61 w = apNew[i] = a
17f8a 70 4f 6c 64 5b 69 5d 3b 0a 20 20 20 20 20 20 61 pOld[i];. a
17f8b 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 pOld[i] = 0;.
17f8c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
17f8d 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 2d 3e agerWrite(pNew->
17f8e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
17f8f 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 20 20 69 66 nNew++;. if
17f90 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 ( rc ) goto bala
17f91 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 nce_cleanup;.
17f92 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 }else{. as
17f93 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 20 sert( i>0 );.
17f94 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 rc = allocate
17f95 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 BtreePage(pBt, &
17f96 70 4e 65 77 2c 20 26 70 67 6e 6f 2c 20 70 67 6e pNew, &pgno, pgn
17f97 6f 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 o, 0);. if(
17f98 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e rc ) goto balan
17f99 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 ce_cleanup;.
17f9a 20 20 61 70 4e 65 77 5b 69 5d 20 3d 20 70 4e 65 apNew[i] = pNe
17f9b 77 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b w;. nNew++;
17f9c 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 .. /* Set t
17f9d 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 he pointer-map e
17f9e 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6e 65 77 ntry for the new
17f9f 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a sibling page. *
17fa0 2f 0a 20 20 20 20 20 20 69 66 28 20 49 53 41 55 /. if( ISAU
17fa1 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 TOVACUUM ){.
17fa2 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 ptrmapPut(pB
17fa3 74 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 t, pNew->pgno, P
17fa4 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 TRMAP_BTREE, pPa
17fa5 72 65 6e 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 rent->pgno, &rc)
17fa6 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
17fa7 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
17fa8 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 goto ba
17fa9 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 lance_cleanup;.
17faa 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
17fab 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
17fac 20 46 72 65 65 20 61 6e 79 20 6f 6c 64 20 70 61 Free any old pa
17fad 67 65 73 20 74 68 61 74 20 77 65 72 65 20 6e 6f ges that were no
17fae 74 20 72 65 75 73 65 64 20 61 73 20 6e 65 77 20 t reused as new
17faf 70 61 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 77 68 pages.. */. wh
17fb0 69 6c 65 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 ile( i<nOld ){.
17fb1 20 20 20 66 72 65 65 50 61 67 65 28 61 70 4f 6c freePage(apOl
17fb2 64 5b 69 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 d[i], &rc);.
17fb3 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 if( rc ) goto ba
17fb4 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 lance_cleanup;.
17fb5 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 releasePage(a
17fb6 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 61 70 pOld[i]);. ap
17fb7 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 Old[i] = 0;.
17fb8 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 i++;. }.. /*.
17fb9 20 2a 2a 20 50 75 74 20 74 68 65 20 6e 65 77 20 ** Put the new
17fba 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e 64 69 pages in accendi
17fbb 6e 67 20 6f 72 64 65 72 2e 20 20 54 68 69 73 20 ng order. This
17fbc 68 65 6c 70 73 20 74 6f 0a 20 20 2a 2a 20 6b 65 helps to. ** ke
17fbd 65 70 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 ep entries in th
17fbe 65 20 64 69 73 6b 20 66 69 6c 65 20 69 6e 20 6f e disk file in o
17fbf 72 64 65 72 20 73 6f 20 74 68 61 74 20 61 20 73 rder so that a s
17fc0 63 61 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 can. ** of the
17fc1 74 61 62 6c 65 20 69 73 20 61 20 6c 69 6e 65 61 table is a linea
17fc2 72 20 73 63 61 6e 20 74 68 72 6f 75 67 68 20 74 r scan through t
17fc3 68 65 20 66 69 6c 65 2e 20 20 54 68 61 74 0a 20 he file. That.
17fc4 20 2a 2a 20 69 6e 20 74 75 72 6e 20 68 65 6c 70 ** in turn help
17fc5 73 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 s the operating
17fc6 73 79 73 74 65 6d 20 74 6f 20 64 65 6c 69 76 65 system to delive
17fc7 72 20 70 61 67 65 73 0a 20 20 2a 2a 20 66 72 6f r pages. ** fro
17fc8 6d 20 74 68 65 20 64 69 73 6b 20 6d 6f 72 65 20 m the disk more
17fc9 72 61 70 69 64 6c 79 2e 0a 20 20 2a 2a 0a 20 20 rapidly.. **.
17fca 2a 2a 20 41 6e 20 4f 28 6e 5e 32 29 20 69 6e 73 ** An O(n^2) ins
17fcb 65 72 74 69 6f 6e 20 73 6f 72 74 20 61 6c 67 6f ertion sort algo
17fcc 72 69 74 68 6d 20 69 73 20 75 73 65 64 2c 20 62 rithm is used, b
17fcd 75 74 20 73 69 6e 63 65 0a 20 20 2a 2a 20 6e 20 ut since. ** n
17fce 69 73 20 6e 65 76 65 72 20 6d 6f 72 65 20 74 68 is never more th
17fcf 61 6e 20 4e 42 20 28 61 20 73 6d 61 6c 6c 20 63 an NB (a small c
17fd0 6f 6e 73 74 61 6e 74 29 2c 20 74 68 61 74 20 73 onstant), that s
17fd1 68 6f 75 6c 64 0a 20 20 2a 2a 20 6e 6f 74 20 62 hould. ** not b
17fd2 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 2a e a problem.. *
17fd3 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 4e 42 3d 3d *. ** When NB==
17fd4 33 2c 20 74 68 69 73 20 6f 6e 65 20 6f 70 74 69 3, this one opti
17fd5 6d 69 7a 61 74 69 6f 6e 20 6d 61 6b 65 73 20 74 mization makes t
17fd6 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a he database. **
17fd7 20 61 62 6f 75 74 20 32 35 25 20 66 61 73 74 65 about 25% faste
17fd8 72 20 66 6f 72 20 6c 61 72 67 65 20 69 6e 73 65 r for large inse
17fd9 72 74 69 6f 6e 73 20 61 6e 64 20 64 65 6c 65 74 rtions and delet
17fda 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 ions.. */. for
17fdb 28 69 3d 30 3b 20 69 3c 6b 2d 31 3b 20 69 2b 2b (i=0; i<k-1; i++
17fdc 29 7b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 56 20 ){. int minV
17fdd 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f = apNew[i]->pgno
17fde 3b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 49 20 3d ;. int minI =
17fdf 20 69 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 69 2b i;. for(j=i+
17fe0 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29 7b 0a 20 20 1; j<k; j++){.
17fe1 20 20 20 20 69 66 28 20 61 70 4e 65 77 5b 6a 5d if( apNew[j]
17fe2 2d 3e 70 67 6e 6f 3c 28 75 6e 73 69 67 6e 65 64 ->pgno<(unsigned
17fe3 29 6d 69 6e 56 20 29 7b 0a 20 20 20 20 20 20 20 )minV ){.
17fe4 20 6d 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20 minI = j;.
17fe5 20 20 20 6d 69 6e 56 20 3d 20 61 70 4e 65 77 5b minV = apNew[
17fe6 6a 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 20 20 j]->pgno;.
17fe7 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
17fe8 6d 69 6e 49 3e 69 20 29 7b 0a 20 20 20 20 20 20 minI>i ){.
17fe9 69 6e 74 20 74 3b 0a 20 20 20 20 20 20 4d 65 6d int t;. Mem
17fea 50 61 67 65 20 2a 70 54 3b 0a 20 20 20 20 20 20 Page *pT;.
17feb 74 20 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 t = apNew[i]->pg
17fec 6e 6f 3b 0a 20 20 20 20 20 20 70 54 20 3d 20 61 no;. pT = a
17fed 70 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 61 pNew[i];. a
17fee 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4e 65 77 5b pNew[i] = apNew[
17fef 6d 69 6e 49 5d 3b 0a 20 20 20 20 20 20 61 70 4e minI];. apN
17ff0 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a 20 ew[minI] = pT;.
17ff1 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 41 43 45 }. }. TRACE
17ff2 28 28 22 6e 65 77 3a 20 25 64 28 25 64 29 20 25 (("new: %d(%d) %
17ff3 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64 28 d(%d) %d(%d) %d(
17ff4 25 64 29 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20 %d) %d(%d)\n",.
17ff5 20 20 20 61 70 4e 65 77 5b 30 5d 2d 3e 70 67 6e apNew[0]->pgn
17ff6 6f 2c 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20 20 o, szNew[0],.
17ff7 20 6e 4e 65 77 3e 3d 32 20 3f 20 61 70 4e 65 77 nNew>=2 ? apNew
17ff8 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e [1]->pgno : 0, n
17ff9 4e 65 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31 New>=2 ? szNew[1
17ffa 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e ] : 0,. nNew>
17ffb 3d 33 20 3f 20 61 70 4e 65 77 5b 32 5d 2d 3e 70 =3 ? apNew[2]->p
17ffc 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 33 gno : 0, nNew>=3
17ffd 20 3f 20 73 7a 4e 65 77 5b 32 5d 20 3a 20 30 2c ? szNew[2] : 0,
17ffe 0a 20 20 20 20 6e 4e 65 77 3e 3d 34 20 3f 20 61 . nNew>=4 ? a
17fff 70 4e 65 77 5b 33 5d 2d 3e 70 67 6e 6f 20 3a 20 pNew[3]->pgno :
18000 30 2c 20 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a 4e 0, nNew>=4 ? szN
18001 65 77 5b 33 5d 20 3a 20 30 2c 0a 20 20 20 20 6e ew[3] : 0,. n
18002 4e 65 77 3e 3d 35 20 3f 20 61 70 4e 65 77 5b 34 New>=5 ? apNew[4
18003 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 ]->pgno : 0, nNe
18004 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 5b 34 5d 20 w>=5 ? szNew[4]
18005 3a 20 30 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 : 0));.. assert
18006 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
18007 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e writeable(pParen
18008 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 t->pDbPage) );.
18009 20 70 75 74 34 62 79 74 65 28 70 52 69 67 68 74 put4byte(pRight
1800a 2c 20 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d , apNew[nNew-1]-
1800b 3e 70 67 6e 6f 29 3b 0a 0a 20 20 2f 2a 0a 20 20 >pgno);.. /*.
1800c 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 74 72 69 ** Evenly distri
1800d 62 75 74 65 20 74 68 65 20 64 61 74 61 20 69 6e bute the data in
1800e 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 73 apCell[] across
1800f 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 2e 0a the new pages..
18010 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 69 76 69 ** Insert divi
18011 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 70 der cells into p
18012 50 61 72 65 6e 74 20 61 73 20 6e 65 63 65 73 73 Parent as necess
18013 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d 20 ary.. */. j =
18014 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 0;. for(i=0; i<
18015 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nNew; i++){.
18016 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 68 65 20 /* Assemble the
18017 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65 new sibling page
18018 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 . */. MemPage
18019 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69 *pNew = apNew[i
1801a 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a ];. assert( j
1801b 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 <nMaxCells );.
1801c 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c zeroPage(pNew,
1801d 20 70 61 67 65 46 6c 61 67 73 29 3b 0a 20 20 20 pageFlags);.
1801e 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 4e assemblePage(pN
1801f 65 77 2c 20 63 6e 74 4e 65 77 5b 69 5d 2d 6a 2c ew, cntNew[i]-j,
18020 20 26 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 73 7a &apCell[j], &sz
18021 43 65 6c 6c 5b 6a 5d 29 3b 0a 20 20 20 20 61 73 Cell[j]);. as
18022 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 65 6c sert( pNew->nCel
18023 6c 3e 30 20 7c 7c 20 28 6e 4e 65 77 3d 3d 31 20 l>0 || (nNew==1
18024 26 26 20 63 6e 74 4e 65 77 5b 30 5d 3d 3d 30 29 && cntNew[0]==0)
18025 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
18026 70 4e 65 77 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d pNew->nOverflow=
18027 3d 30 20 29 3b 0a 0a 20 20 20 20 6a 20 3d 20 63 =0 );.. j = c
18028 6e 74 4e 65 77 5b 69 5d 3b 0a 0a 20 20 20 20 2f ntNew[i];.. /
18029 2a 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 * If the sibling
1802a 20 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 page assembled
1802b 61 62 6f 76 65 20 77 61 73 20 6e 6f 74 20 74 68 above was not th
1802c 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 e right-most sib
1802d 6c 69 6e 67 2c 0a 20 20 20 20 2a 2a 20 69 6e 73 ling,. ** ins
1802e 65 72 74 20 61 20 64 69 76 69 64 65 72 20 63 65 ert a divider ce
1802f 6c 6c 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 ll into the pare
18030 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a nt page.. */.
18031 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e 4e assert( i<nN
18032 65 77 2d 31 20 7c 7c 20 6a 3d 3d 6e 43 65 6c 6c ew-1 || j==nCell
18033 20 29 3b 0a 20 20 20 20 69 66 28 20 6a 3c 6e 43 );. if( j<nC
18034 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 75 38 20 ell ){. u8
18035 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38 *pCell;. u8
18036 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 69 *pTemp;. i
18037 6e 74 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73 nt sz;.. as
18038 73 65 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c sert( j<nMaxCell
18039 73 20 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c s );. pCell
1803a 20 3d 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 = apCell[j];.
1803b 20 20 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b sz = szCell[
1803c 6a 5d 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 j] + leafCorrect
1803d 69 6f 6e 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 ion;. pTemp
1803e 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 5b 69 = &aOvflSpace[i
1803f 4f 76 66 6c 53 70 61 63 65 5d 3b 0a 20 20 20 20 OvflSpace];.
18040 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 6c 65 61 if( !pNew->lea
18041 66 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d f ){. mem
18042 63 70 79 28 26 70 4e 65 77 2d 3e 61 44 61 74 61 cpy(&pNew->aData
18043 5b 38 5d 2c 20 70 43 65 6c 6c 2c 20 34 29 3b 0a [8], pCell, 4);.
18044 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
18045 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20 leafData ){.
18046 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 /* If the tr
18047 65 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61 74 ee is a leaf-dat
18048 61 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65 20 a tree, and the
18049 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 61 siblings are lea
1804a 76 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a ves, . **
1804b 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e then there is n
1804c 6f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 o divider cell i
1804d 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74 n apCell[]. Inst
1804e 65 61 64 2c 20 74 68 65 20 64 69 76 69 64 65 72 ead, the divider
1804f 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c . ** cel
18050 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 l consists of th
18051 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66 6f e integer key fo
18052 72 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 r the right-most
18053 20 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20 cell of .
18054 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e 67 ** the sibling
18055 2d 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 -page assembled
18056 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20 above only..
18057 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 */. C
18058 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 ellInfo info;.
18059 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20 j--;.
1805a 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c btreeParseCel
1805b 6c 50 74 72 28 70 4e 65 77 2c 20 61 70 43 65 6c lPtr(pNew, apCel
1805c 6c 5b 6a 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 l[j], &info);.
1805d 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54 pCell = pT
1805e 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 73 7a 20 emp;. sz
1805f 3d 20 34 20 2b 20 70 75 74 56 61 72 69 6e 74 28 = 4 + putVarint(
18060 26 70 43 65 6c 6c 5b 34 5d 2c 20 69 6e 66 6f 2e &pCell[4], info.
18061 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 70 nKey);. p
18062 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 Temp = 0;.
18063 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
18064 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 20 Cell -= 4;.
18065 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 20 63 61 /* Obscure ca
18066 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 2d se for non-leaf-
18067 64 61 74 61 20 74 72 65 65 73 3a 20 49 66 20 74 data trees: If t
18068 68 65 20 63 65 6c 6c 20 61 74 20 70 43 65 6c 6c he cell at pCell
18069 20 77 61 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 was. **
1806a 70 72 65 76 69 6f 75 73 6c 79 20 73 74 6f 72 65 previously store
1806b 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65 d on a leaf node
1806c 2c 20 61 6e 64 20 69 74 73 20 72 65 70 6f 72 74 , and its report
1806d 65 64 20 73 69 7a 65 20 77 61 73 20 34 0a 20 20 ed size was 4.
1806e 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 2c 20 ** bytes,
1806f 74 68 65 6e 20 69 74 20 6d 61 79 20 61 63 74 75 then it may actu
18070 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72 20 ally be smaller
18071 74 68 61 6e 20 74 68 69 73 20 0a 20 20 20 20 20 than this .
18072 20 20 20 2a 2a 20 28 73 65 65 20 62 74 72 65 65 ** (see btree
18073 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c 20 ParseCellPtr(),
18074 34 20 62 79 74 65 73 20 69 73 20 74 68 65 20 6d 4 bytes is the m
18075 69 6e 69 6d 75 6d 20 73 69 7a 65 20 6f 66 0a 20 inimum size of.
18076 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 20 63 65 ** any ce
18077 6c 6c 29 2e 20 42 75 74 20 69 74 20 69 73 20 69 ll). But it is i
18078 6d 70 6f 72 74 61 6e 74 20 74 6f 20 70 61 73 73 mportant to pass
18079 20 74 68 65 20 63 6f 72 72 65 63 74 20 73 69 7a the correct siz
1807a 65 20 74 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a e to . **
1807b 20 69 6e 73 65 72 74 43 65 6c 6c 28 29 2c 20 73 insertCell(), s
1807c 6f 20 72 65 70 61 72 73 65 20 74 68 65 20 63 65 o reparse the ce
1807d 6c 6c 20 6e 6f 77 2e 0a 20 20 20 20 20 20 20 20 ll now..
1807e 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f **. ** No
1807f 74 65 20 74 68 61 74 20 74 68 69 73 20 63 61 6e te that this can
18080 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20 69 6e never happen in
18081 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 20 an SQLite data
18082 66 69 6c 65 2c 20 61 73 20 61 6c 6c 0a 20 20 20 file, as all.
18083 20 20 20 20 20 2a 2a 20 63 65 6c 6c 73 20 61 72 ** cells ar
18084 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 e at least 4 byt
18085 65 73 2e 20 49 74 20 6f 6e 6c 79 20 68 61 70 70 es. It only happ
18086 65 6e 73 20 69 6e 20 62 2d 74 72 65 65 73 20 75 ens in b-trees u
18087 73 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 sed. ** t
18088 6f 20 65 76 61 6c 75 61 74 65 20 22 49 4e 20 28 o evaluate "IN (
18089 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20 61 6e 64 SELECT ...)" and
1808a 20 73 69 6d 69 6c 61 72 20 63 6c 61 75 73 65 73 similar clauses
1808b 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
1808c 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b if( szCell[
1808d 6a 5d 3d 3d 34 20 29 7b 0a 20 20 20 20 20 20 20 j]==4 ){.
1808e 20 20 20 61 73 73 65 72 74 28 6c 65 61 66 43 6f assert(leafCo
1808f 72 72 65 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20 20 rrection==4);.
18090 20 20 20 20 20 20 20 20 73 7a 20 3d 20 63 65 6c sz = cel
18091 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74 lSizePtr(pParent
18092 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 , pCell);.
18093 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
18094 20 20 69 4f 76 66 6c 53 70 61 63 65 20 2b 3d 20 iOvflSpace +=
18095 73 7a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 sz;. assert
18096 28 20 73 7a 3c 3d 70 42 74 2d 3e 70 61 67 65 53 ( sz<=pBt->pageS
18097 69 7a 65 2f 34 20 29 3b 0a 20 20 20 20 20 20 61 ize/4 );. a
18098 73 73 65 72 74 28 20 69 4f 76 66 6c 53 70 61 63 ssert( iOvflSpac
18099 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 e<=pBt->pageSize
1809a 20 29 3b 0a 20 20 20 20 20 20 69 6e 73 65 72 74 );. insert
1809b 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 Cell(pParent, nx
1809c 44 69 76 2c 20 70 43 65 6c 6c 2c 20 73 7a 2c 20 Div, pCell, sz,
1809d 70 54 65 6d 70 2c 20 70 4e 65 77 2d 3e 70 67 6e pTemp, pNew->pgn
1809e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 69 o, &rc);. i
1809f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
180a0 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f ) goto balance_
180a1 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 61 cleanup;. a
180a2 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
180a3 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
180a4 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 Parent->pDbPage)
180a5 20 29 3b 0a 0a 20 20 20 20 20 20 6a 2b 2b 3b 0a );.. j++;.
180a6 20 20 20 20 20 20 6e 78 44 69 76 2b 2b 3b 0a 20 nxDiv++;.
180a7 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 }. }. asser
180a8 74 28 20 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 t( j==nCell );.
180a9 20 61 73 73 65 72 74 28 20 6e 4f 6c 64 3e 30 20 assert( nOld>0
180aa 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4e 65 );. assert( nNe
180ab 77 3e 30 20 29 3b 0a 20 20 69 66 28 20 28 70 61 w>0 );. if( (pa
180ac 67 65 46 6c 61 67 73 20 26 20 50 54 46 5f 4c 45 geFlags & PTF_LE
180ad 41 46 29 3d 3d 30 20 29 7b 0a 20 20 20 20 75 38 AF)==0 ){. u8
180ae 20 2a 7a 43 68 69 6c 64 20 3d 20 26 61 70 43 6f *zChild = &apCo
180af 70 79 5b 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74 py[nOld-1]->aDat
180b0 61 5b 38 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 a[8];. memcpy
180b1 28 26 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d (&apNew[nNew-1]-
180b2 3e 61 44 61 74 61 5b 38 5d 2c 20 7a 43 68 69 6c >aData[8], zChil
180b3 64 2c 20 34 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 d, 4);. }.. if
180b4 28 20 69 73 52 6f 6f 74 20 26 26 20 70 50 61 72 ( isRoot && pPar
180b5 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 ent->nCell==0 &&
180b6 20 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 pParent->hdrOff
180b7 73 65 74 3c 3d 61 70 4e 65 77 5b 30 5d 2d 3e 6e set<=apNew[0]->n
180b8 46 72 65 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 Free ){. /* T
180b9 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 he root page of
180ba 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 77 20 63 the b-tree now c
180bb 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 ontains no cells
180bc 2e 20 54 68 65 20 6f 6e 6c 79 20 73 69 62 6c 69 . The only sibli
180bd 6e 67 0a 20 20 20 20 2a 2a 20 70 61 67 65 20 69 ng. ** page i
180be 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c s the right-chil
180bf 64 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 2e d of the parent.
180c0 20 43 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e Copy the conten
180c1 74 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a ts of the. **
180c2 20 63 68 69 6c 64 20 70 61 67 65 20 69 6e 74 6f child page into
180c3 20 74 68 65 20 70 61 72 65 6e 74 2c 20 64 65 63 the parent, dec
180c4 72 65 61 73 69 6e 67 20 74 68 65 20 6f 76 65 72 reasing the over
180c5 61 6c 6c 20 68 65 69 67 68 74 20 6f 66 20 74 68 all height of th
180c6 65 0a 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 e. ** b-tree
180c7 73 74 72 75 63 74 75 72 65 20 62 79 20 6f 6e 65 structure by one
180c8 2e 20 54 68 69 73 20 69 73 20 64 65 73 63 72 69 . This is descri
180c9 62 65 64 20 61 73 20 74 68 65 20 22 62 61 6c 61 bed as the "bala
180ca 6e 63 65 2d 73 68 61 6c 6c 6f 77 65 72 22 0a 20 nce-shallower".
180cb 20 20 20 2a 2a 20 73 75 62 2d 61 6c 67 6f 72 69 ** sub-algori
180cc 74 68 6d 20 69 6e 20 73 6f 6d 65 20 64 6f 63 75 thm in some docu
180cd 6d 65 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a mentation.. *
180ce 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 *. ** If this
180cf 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 is an auto-vacu
180d0 75 6d 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 um database, the
180d1 20 63 61 6c 6c 20 74 6f 20 63 6f 70 79 4e 6f 64 call to copyNod
180d2 65 43 6f 6e 74 65 6e 74 28 29 20 0a 20 20 20 20 eContent() .
180d3 2a 2a 20 73 65 74 73 20 61 6c 6c 20 70 6f 69 6e ** sets all poin
180d4 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 ter-map entries
180d5 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f corresponding to
180d6 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 database image
180d7 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 66 6f pages . ** fo
180d8 72 20 77 68 69 63 68 20 74 68 65 20 70 6f 69 6e r which the poin
180d9 74 65 72 20 69 73 20 73 74 6f 72 65 64 20 77 69 ter is stored wi
180da 74 68 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 thin the content
180db 20 62 65 69 6e 67 20 63 6f 70 69 65 64 2e 0a 20 being copied..
180dc 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 **. ** The
180dd 20 73 65 63 6f 6e 64 20 61 73 73 65 72 74 20 62 second assert b
180de 65 6c 6f 77 20 76 65 72 69 66 69 65 73 20 74 68 elow verifies th
180df 61 74 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 at the child pag
180e0 65 20 69 73 20 64 65 66 72 61 67 6d 65 6e 74 65 e is defragmente
180e1 64 0a 20 20 20 20 2a 2a 20 28 69 74 20 6d 75 73 d. ** (it mus
180e2 74 20 62 65 2c 20 61 73 20 69 74 20 77 61 73 20 t be, as it was
180e3 6a 75 73 74 20 72 65 63 6f 6e 73 74 72 75 63 74 just reconstruct
180e4 65 64 20 75 73 69 6e 67 20 61 73 73 65 6d 62 6c ed using assembl
180e5 65 50 61 67 65 28 29 29 2e 20 54 68 69 73 0a 20 ePage()). This.
180e6 20 20 20 2a 2a 20 69 73 20 69 6d 70 6f 72 74 61 ** is importa
180e7 6e 74 20 69 66 20 74 68 65 20 70 61 72 65 6e 74 nt if the parent
180e8 20 70 61 67 65 20 68 61 70 70 65 6e 73 20 74 6f page happens to
180e9 20 62 65 20 70 61 67 65 20 31 20 6f 66 20 74 68 be page 1 of th
180ea 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a e database. *
180eb 2a 20 69 6d 61 67 65 2e 20 20 2a 2f 0a 20 20 20 * image. */.
180ec 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3d 3d 31 assert( nNew==1
180ed 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
180ee 61 70 4e 65 77 5b 30 5d 2d 3e 6e 46 72 65 65 20 apNew[0]->nFree
180ef 3d 3d 20 0a 20 20 20 20 20 20 20 20 28 67 65 74 == . (get
180f0 32 62 79 74 65 28 26 61 70 4e 65 77 5b 30 5d 2d 2byte(&apNew[0]-
180f1 3e 61 44 61 74 61 5b 35 5d 29 2d 61 70 4e 65 77 >aData[5])-apNew
180f2 5b 30 5d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 2d [0]->cellOffset-
180f3 61 70 4e 65 77 5b 30 5d 2d 3e 6e 43 65 6c 6c 2a apNew[0]->nCell*
180f4 32 29 20 0a 20 20 20 20 29 3b 0a 20 20 20 20 63 2) . );. c
180f5 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 61 opyNodeContent(a
180f6 70 4e 65 77 5b 30 5d 2c 20 70 50 61 72 65 6e 74 pNew[0], pParent
180f7 2c 20 26 72 63 29 3b 0a 20 20 20 20 66 72 65 65 , &rc);. free
180f8 50 61 67 65 28 61 70 4e 65 77 5b 30 5d 2c 20 26 Page(apNew[0], &
180f9 72 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 rc);. }else if(
180fa 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b ISAUTOVACUUM ){
180fb 0a 20 20 20 20 2f 2a 20 46 69 78 20 74 68 65 20 . /* Fix the
180fc 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
180fd 69 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 20 ies for all the
180fe 63 65 6c 6c 73 20 74 68 61 74 20 77 65 72 65 20 cells that were
180ff 73 68 69 66 74 65 64 20 61 72 6f 75 6e 64 2e 20 shifted around.
18100 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 20 61 72 . ** There ar
18101 65 20 73 65 76 65 72 61 6c 20 64 69 66 66 65 72 e several differ
18102 65 6e 74 20 74 79 70 65 73 20 6f 66 20 70 6f 69 ent types of poi
18103 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 nter-map entries
18104 20 74 68 61 74 20 6e 65 65 64 20 74 6f 0a 20 20 that need to.
18105 20 20 2a 2a 20 62 65 20 64 65 61 6c 74 20 77 69 ** be dealt wi
18106 74 68 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 th by this routi
18107 6e 65 2e 20 53 6f 6d 65 20 6f 66 20 74 68 65 73 ne. Some of thes
18108 65 20 68 61 76 65 20 62 65 65 6e 20 73 65 74 20 e have been set
18109 61 6c 72 65 61 64 79 2c 20 62 75 74 0a 20 20 20 already, but.
1810a 20 2a 2a 20 6d 61 6e 79 20 68 61 76 65 20 6e 6f ** many have no
1810b 74 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 t. The following
1810c 20 69 73 20 61 20 73 75 6d 6d 61 72 79 3a 0a 20 is a summary:.
1810d 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 31 **. ** 1
1810e 29 20 54 68 65 20 65 6e 74 72 69 65 73 20 61 73 ) The entries as
1810f 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e 65 sociated with ne
18110 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 w sibling pages
18111 74 68 61 74 20 77 65 72 65 20 6e 6f 74 0a 20 20 that were not.
18112 20 20 2a 2a 20 20 20 20 20 20 73 69 62 6c 69 6e ** siblin
18113 67 73 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e gs when this fun
18114 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 ction was called
18115 2e 20 54 68 65 73 65 20 68 61 76 65 20 61 6c 72 . These have alr
18116 65 61 64 79 0a 20 20 20 20 2a 2a 20 20 20 20 20 eady. **
18117 20 62 65 65 6e 20 73 65 74 2e 20 57 65 20 64 6f been set. We do
18118 6e 27 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 n't need to worr
18119 79 20 61 62 6f 75 74 20 6f 6c 64 20 73 69 62 6c y about old sibl
1811a 69 6e 67 73 20 74 68 61 74 20 77 65 72 65 0a 20 ings that were.
1811b 20 20 20 2a 2a 20 20 20 20 20 20 6d 6f 76 65 64 ** moved
1811c 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 to the free-lis
1811d 74 20 2d 20 74 68 65 20 66 72 65 65 50 61 67 65 t - the freePage
1811e 28 29 20 63 6f 64 65 20 68 61 73 20 74 61 6b 65 () code has take
1811f 6e 20 63 61 72 65 0a 20 20 20 20 2a 2a 20 20 20 n care. **
18120 20 20 20 6f 66 20 74 68 6f 73 65 2e 0a 20 20 20 of those..
18121 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 32 29 20 **. ** 2)
18122 54 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 The pointer-map
18123 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 entries associat
18124 65 64 20 77 69 74 68 20 74 68 65 20 66 69 72 73 ed with the firs
18125 74 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20 20 2a t overflow. *
18126 2a 20 20 20 20 20 20 70 61 67 65 20 69 6e 20 61 * page in a
18127 6e 79 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 ny overflow chai
18128 6e 73 20 75 73 65 64 20 62 79 20 6e 65 77 20 64 ns used by new d
18129 69 76 69 64 65 72 20 63 65 6c 6c 73 2e 20 54 68 ivider cells. Th
1812a 65 73 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 ese . **
1812b 20 68 61 76 65 20 61 6c 73 6f 20 61 6c 72 65 61 have also alrea
1812c 64 79 20 62 65 65 6e 20 74 61 6b 65 6e 20 63 61 dy been taken ca
1812d 72 65 20 6f 66 20 62 79 20 74 68 65 20 69 6e 73 re of by the ins
1812e 65 72 74 43 65 6c 6c 28 29 20 63 6f 64 65 2e 0a ertCell() code..
1812f 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 **. **
18130 33 29 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 3) If the siblin
18131 67 20 70 61 67 65 73 20 61 72 65 20 6e 6f 74 20 g pages are not
18132 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 74 68 65 leaves, then the
18133 20 63 68 69 6c 64 20 70 61 67 65 73 20 6f 66 0a child pages of.
18134 20 20 20 20 2a 2a 20 20 20 20 20 20 63 65 6c 6c ** cell
18135 73 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 s stored on the
18136 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 6d 61 sibling pages ma
18137 79 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64 y need to be upd
18138 61 74 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 ated.. **.
18139 20 2a 2a 20 20 20 34 29 20 49 66 20 74 68 65 20 ** 4) If the
1813a 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 72 sibling pages ar
1813b 65 20 6e 6f 74 20 69 6e 74 65 72 6e 61 6c 20 69 e not internal i
1813c 6e 74 6b 65 79 20 6e 6f 64 65 73 2c 20 74 68 65 ntkey nodes, the
1813d 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 20 20 20 n any. **
1813e 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 overflow pages
1813f 20 75 73 65 64 20 62 79 20 74 68 65 73 65 20 63 used by these c
18140 65 6c 6c 73 20 6d 61 79 20 6e 65 65 64 20 74 6f ells may need to
18141 20 62 65 20 75 70 64 61 74 65 64 0a 20 20 20 20 be updated.
18142 2a 2a 20 20 20 20 20 20 28 69 6e 74 65 72 6e 61 ** (interna
18143 6c 20 69 6e 74 6b 65 79 20 6e 6f 64 65 73 20 6e l intkey nodes n
18144 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 70 6f 69 ever contain poi
18145 6e 74 65 72 73 20 74 6f 20 6f 76 65 72 66 6c 6f nters to overflo
18146 77 20 70 61 67 65 73 29 2e 0a 20 20 20 20 2a 2a w pages).. **
18147 0a 20 20 20 20 2a 2a 20 20 20 35 29 20 49 66 20 . ** 5) If
18148 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 the sibling page
18149 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 s are not leaves
1814a 2c 20 74 68 65 6e 20 74 68 65 20 70 6f 69 6e 74 , then the point
1814b 65 72 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 20 20 er-map. **
1814c 20 20 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74 entries for t
1814d 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70 he right-child p
1814e 61 67 65 73 20 6f 66 20 65 61 63 68 20 73 69 62 ages of each sib
1814f 6c 69 6e 67 20 6d 61 79 20 6e 65 65 64 0a 20 20 ling may need.
18150 20 20 2a 2a 20 20 20 20 20 20 74 6f 20 62 65 20 ** to be
18151 75 70 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a 0a updated.. **.
18152 20 20 20 20 2a 2a 20 43 61 73 65 73 20 31 20 61 ** Cases 1 a
18153 6e 64 20 32 20 61 72 65 20 64 65 61 6c 74 20 77 nd 2 are dealt w
18154 69 74 68 20 61 62 6f 76 65 20 62 79 20 6f 74 68 ith above by oth
18155 65 72 20 63 6f 64 65 2e 20 54 68 65 20 6e 65 78 er code. The nex
18156 74 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 64 t. ** block d
18157 65 61 6c 73 20 77 69 74 68 20 63 61 73 65 73 20 eals with cases
18158 33 20 61 6e 64 20 34 20 61 6e 64 20 74 68 65 20 3 and 4 and the
18159 6f 6e 65 20 61 66 74 65 72 20 74 68 61 74 2c 20 one after that,
1815a 63 61 73 65 20 35 2e 20 53 69 6e 63 65 0a 20 20 case 5. Since.
1815b 20 20 2a 2a 20 73 65 74 74 69 6e 67 20 61 20 70 ** setting a p
1815c 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 ointer map entry
1815d 20 69 73 20 61 20 72 65 6c 61 74 69 76 65 6c 79 is a relatively
1815e 20 65 78 70 65 6e 73 69 76 65 20 6f 70 65 72 61 expensive opera
1815f 74 69 6f 6e 2c 20 74 68 69 73 0a 20 20 20 20 2a tion, this. *
18160 2a 20 63 6f 64 65 20 6f 6e 6c 79 20 73 65 74 73 * code only sets
18161 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 pointer map ent
18162 72 69 65 73 20 66 6f 72 20 63 68 69 6c 64 20 6f ries for child o
18163 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 r overflow pages
18164 20 74 68 61 74 20 68 61 76 65 0a 20 20 20 20 2a that have. *
18165 2a 20 61 63 74 75 61 6c 6c 79 20 6d 6f 76 65 64 * actually moved
18166 20 62 65 74 77 65 65 6e 20 70 61 67 65 73 2e 20 between pages.
18167 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 */. MemPage
18168 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 30 5d *pNew = apNew[0]
18169 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 ;. MemPage *p
1816a 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 30 5d 3b Old = apCopy[0];
1816b 0a 20 20 20 20 69 6e 74 20 6e 4f 76 65 72 66 6c . int nOverfl
1816c 6f 77 20 3d 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 ow = pOld->nOver
1816d 66 6c 6f 77 3b 0a 20 20 20 20 69 6e 74 20 69 4e flow;. int iN
1816e 65 78 74 4f 6c 64 20 3d 20 70 4f 6c 64 2d 3e 6e extOld = pOld->n
1816f 43 65 6c 6c 20 2b 20 6e 4f 76 65 72 66 6c 6f 77 Cell + nOverflow
18170 3b 0a 20 20 20 20 69 6e 74 20 69 4f 76 65 72 66 ;. int iOverf
18171 6c 6f 77 20 3d 20 28 6e 4f 76 65 72 66 6c 6f 77 low = (nOverflow
18172 20 3f 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 ? pOld->aOvfl[0
18173 5d 2e 69 64 78 20 3a 20 2d 31 29 3b 0a 20 20 20 ].idx : -1);.
18174 20 6a 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 j = 0;
18175 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18176 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 27 /* Current '
18177 6f 6c 64 27 20 73 69 62 6c 69 6e 67 20 70 61 67 old' sibling pag
18178 65 20 2a 2f 0a 20 20 20 20 6b 20 3d 20 30 3b 20 e */. k = 0;
18179 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1817a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
1817b 75 72 72 65 6e 74 20 27 6e 65 77 27 20 73 69 62 urrent 'new' sib
1817c 6c 69 6e 67 20 70 61 67 65 20 2a 2f 0a 20 20 20 ling page */.
1817d 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c for(i=0; i<nCel
1817e 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 l; i++){. i
1817f 6e 74 20 69 73 44 69 76 69 64 65 72 20 3d 20 30 nt isDivider = 0
18180 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 ;. while( i
18181 3d 3d 69 4e 65 78 74 4f 6c 64 20 29 7b 0a 20 20 ==iNextOld ){.
18182 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c 20 69 20 /* Cell i
18183 69 73 20 74 68 65 20 63 65 6c 6c 20 69 6d 6d 65 is the cell imme
18184 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e diately followin
18185 67 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 g the last cell
18186 6f 6e 20 6f 6c 64 0a 20 20 20 20 20 20 20 20 2a on old. *
18187 2a 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 6a * sibling page j
18188 2e 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 . If the sibling
18189 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 66 20 70 s are not leaf p
1818a 61 67 65 73 20 6f 66 20 61 6e 0a 20 20 20 20 20 ages of an.
1818b 20 20 20 2a 2a 20 69 6e 74 6b 65 79 20 62 2d 74 ** intkey b-t
1818c 72 65 65 2c 20 74 68 65 6e 20 63 65 6c 6c 20 69 ree, then cell i
1818d 20 77 61 73 20 61 20 64 69 76 69 64 65 72 20 63 was a divider c
1818e 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 ell. */.
1818f 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 2b 2b pOld = apCopy[++
18190 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69 4e 65 78 j];. iNex
18191 74 4f 6c 64 20 3d 20 69 20 2b 20 21 6c 65 61 66 tOld = i + !leaf
18192 44 61 74 61 20 2b 20 70 4f 6c 64 2d 3e 6e 43 65 Data + pOld->nCe
18193 6c 6c 20 2b 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 ll + pOld->nOver
18194 66 6c 6f 77 3b 0a 20 20 20 20 20 20 20 20 69 66 flow;. if
18195 28 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f ( pOld->nOverflo
18196 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e w ){. n
18197 4f 76 65 72 66 6c 6f 77 20 3d 20 70 4f 6c 64 2d Overflow = pOld-
18198 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 >nOverflow;.
18199 20 20 20 20 20 20 69 4f 76 65 72 66 6c 6f 77 20 iOverflow
1819a 3d 20 69 20 2b 20 21 6c 65 61 66 44 61 74 61 20 = i + !leafData
1819b 2b 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d + pOld->aOvfl[0]
1819c 2e 69 64 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a .idx;. }.
1819d 20 20 20 20 20 20 20 20 69 73 44 69 76 69 64 65 isDivide
1819e 72 20 3d 20 21 6c 65 61 66 44 61 74 61 3b 20 20 r = !leafData;
1819f 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
181a0 61 73 73 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77 assert(nOverflow
181a1 3e 30 20 7c 7c 20 69 4f 76 65 72 66 6c 6f 77 3c >0 || iOverflow<
181a2 69 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 i );. asser
181a3 74 28 6e 4f 76 65 72 66 6c 6f 77 3c 32 20 7c 7c t(nOverflow<2 ||
181a4 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e pOld->aOvfl[0].
181a5 69 64 78 3d 3d 70 4f 6c 64 2d 3e 61 4f 76 66 6c idx==pOld->aOvfl
181a6 5b 31 5d 2e 69 64 78 2d 31 29 3b 0a 20 20 20 20 [1].idx-1);.
181a7 20 20 61 73 73 65 72 74 28 6e 4f 76 65 72 66 6c assert(nOverfl
181a8 6f 77 3c 33 20 7c 7c 20 70 4f 6c 64 2d 3e 61 4f ow<3 || pOld->aO
181a9 76 66 6c 5b 31 5d 2e 69 64 78 3d 3d 70 4f 6c 64 vfl[1].idx==pOld
181aa 2d 3e 61 4f 76 66 6c 5b 32 5d 2e 69 64 78 2d 31 ->aOvfl[2].idx-1
181ab 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d );. if( i==
181ac 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 iOverflow ){.
181ad 20 20 20 20 20 69 73 44 69 76 69 64 65 72 20 3d isDivider =
181ae 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 1;. if(
181af 28 2d 2d 6e 4f 76 65 72 66 6c 6f 77 29 3e 30 20 (--nOverflow)>0
181b0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 4f 76 ){. iOv
181b1 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20 20 20 erflow++;.
181b2 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 }. }..
181b3 20 20 20 69 66 28 20 69 3d 3d 63 6e 74 4e 65 77 if( i==cntNew
181b4 5b 6b 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f [k] ){. /
181b5 2a 20 43 65 6c 6c 20 69 20 69 73 20 74 68 65 20 * Cell i is the
181b6 63 65 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 cell immediately
181b7 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c following the l
181b8 61 73 74 20 63 65 6c 6c 20 6f 6e 20 6e 65 77 0a ast cell on new.
181b9 20 20 20 20 20 20 20 20 2a 2a 20 73 69 62 6c 69 ** sibli
181ba 6e 67 20 70 61 67 65 20 6b 2e 20 49 66 20 74 68 ng page k. If th
181bb 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e e siblings are n
181bc 6f 74 20 6c 65 61 66 20 70 61 67 65 73 20 6f 66 ot leaf pages of
181bd 20 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 an. ** i
181be 6e 74 6b 65 79 20 62 2d 74 72 65 65 2c 20 74 68 ntkey b-tree, th
181bf 65 6e 20 63 65 6c 6c 20 69 20 69 73 20 61 20 64 en cell i is a d
181c0 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 20 2a 2f ivider cell. */
181c1 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 . pNew =
181c2 61 70 4e 65 77 5b 2b 2b 6b 5d 3b 0a 20 20 20 20 apNew[++k];.
181c3 20 20 20 20 69 66 28 20 21 6c 65 61 66 44 61 74 if( !leafDat
181c4 61 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 a ) continue;.
181c5 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 }. asse
181c6 72 74 28 20 6a 3c 6e 4f 6c 64 20 29 3b 0a 20 20 rt( j<nOld );.
181c7 20 20 20 20 61 73 73 65 72 74 28 20 6b 3c 6e 4e assert( k<nN
181c8 65 77 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 ew );.. /*
181c9 49 66 20 74 68 65 20 63 65 6c 6c 20 77 61 73 20 If the cell was
181ca 6f 72 69 67 69 6e 61 6c 6c 79 20 64 69 76 69 64 originally divid
181cb 65 72 20 63 65 6c 6c 20 28 61 6e 64 20 69 73 20 er cell (and is
181cc 6e 6f 74 20 6e 6f 77 29 20 6f 72 0a 20 20 20 20 not now) or.
181cd 20 20 2a 2a 20 61 6e 20 6f 76 65 72 66 6c 6f 77 ** an overflow
181ce 20 63 65 6c 6c 2c 20 6f 72 20 69 66 20 74 68 65 cell, or if the
181cf 20 63 65 6c 6c 20 77 61 73 20 6c 6f 63 61 74 65 cell was locate
181d0 64 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74 d on a different
181d1 20 73 69 62 6c 69 6e 67 0a 20 20 20 20 20 20 2a sibling. *
181d2 2a 20 70 61 67 65 20 62 65 66 6f 72 65 20 74 68 * page before th
181d3 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65 e balancing, the
181d4 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 n the pointer ma
181d5 70 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 p entries associ
181d6 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 77 69 ated. ** wi
181d7 74 68 20 61 6e 79 20 63 68 69 6c 64 20 6f 72 20 th any child or
181d8 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 6e overflow pages n
181d9 65 65 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 eed to be update
181da 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 d. */. if(
181db 20 69 73 44 69 76 69 64 65 72 20 7c 7c 20 70 4f isDivider || pO
181dc 6c 64 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e ld->pgno!=pNew->
181dd 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 pgno ){.
181de 69 66 28 20 21 6c 65 61 66 43 6f 72 72 65 63 74 if( !leafCorrect
181df 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ion ){.
181e0 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 ptrmapPut(pBt,
181e1 67 65 74 34 62 79 74 65 28 61 70 43 65 6c 6c 5b get4byte(apCell[
181e2 69 5d 29 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 i]), PTRMAP_BTRE
181e3 45 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26 E, pNew->pgno, &
181e4 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rc);. }.
181e5 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c if( szCel
181e6 6c 5b 69 5d 3e 70 4e 65 77 2d 3e 6d 69 6e 4c 6f l[i]>pNew->minLo
181e7 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 cal ){.
181e8 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 ptrmapPutOvflPt
181e9 72 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b 69 r(pNew, apCell[i
181ea 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 ], &rc);.
181eb 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
181ec 0a 0a 20 20 20 20 69 66 28 20 21 6c 65 61 66 43 .. if( !leafC
181ed 6f 72 72 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20 orrection ){.
181ee 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e for(i=0; i<nN
181ef 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ew; i++){.
181f0 20 20 75 33 32 20 6b 65 79 20 3d 20 67 65 74 34 u32 key = get4
181f1 62 79 74 65 28 26 61 70 4e 65 77 5b 69 5d 2d 3e byte(&apNew[i]->
181f2 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20 aData[8]);.
181f3 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 ptrmapPut(pBt
181f4 2c 20 6b 65 79 2c 20 50 54 52 4d 41 50 5f 42 54 , key, PTRMAP_BT
181f5 52 45 45 2c 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 REE, apNew[i]->p
181f6 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 gno, &rc);.
181f7 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30 0a }. }..#if 0.
181f8 20 20 20 20 2f 2a 20 54 68 65 20 70 74 72 6d 61 /* The ptrma
181f9 70 43 68 65 63 6b 50 61 67 65 73 28 29 20 63 6f pCheckPages() co
181fa 6e 74 61 69 6e 73 20 61 73 73 65 72 74 28 29 20 ntains assert()
181fb 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 statements that
181fc 76 65 72 69 66 79 20 74 68 61 74 0a 20 20 20 20 verify that.
181fd 2a 2a 20 61 6c 6c 20 70 6f 69 6e 74 65 72 20 6d ** all pointer m
181fe 61 70 20 70 61 67 65 73 20 61 72 65 20 73 65 74 ap pages are set
181ff 20 63 6f 72 72 65 63 74 6c 79 2e 20 54 68 69 73 correctly. This
18200 20 69 73 20 68 65 6c 70 66 75 6c 20 77 68 69 6c is helpful whil
18201 65 20 0a 20 20 20 20 2a 2a 20 64 65 62 75 67 67 e . ** debugg
18202 69 6e 67 2e 20 54 68 69 73 20 69 73 20 75 73 75 ing. This is usu
18203 61 6c 6c 79 20 64 69 73 61 62 6c 65 64 20 62 65 ally disabled be
18204 63 61 75 73 65 20 61 20 63 6f 72 72 75 70 74 20 cause a corrupt
18205 64 61 74 61 62 61 73 65 20 6d 61 79 0a 20 20 20 database may.
18206 20 2a 2a 20 63 61 75 73 65 20 61 6e 20 61 73 73 ** cause an ass
18207 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 20 ert() statement
18208 74 6f 20 66 61 69 6c 2e 20 20 2a 2f 0a 20 20 20 to fail. */.
18209 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 ptrmapCheckPage
1820a 73 28 61 70 4e 65 77 2c 20 6e 4e 65 77 29 3b 0a s(apNew, nNew);.
1820b 20 20 20 20 70 74 72 6d 61 70 43 68 65 63 6b 50 ptrmapCheckP
1820c 61 67 65 73 28 26 70 50 61 72 65 6e 74 2c 20 31 ages(&pParent, 1
1820d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 );.#endif. }..
1820e 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 assert( pParent
1820f 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 54 52 ->isInit );. TR
18210 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 66 ACE(("BALANCE: f
18211 69 6e 69 73 68 65 64 3a 20 6f 6c 64 3d 25 64 20 inished: old=%d
18212 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64 5c new=%d cells=%d\
18213 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 6e 4f n",. nO
18214 6c 64 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 ld, nNew, nCell)
18215 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c );.. /*. ** Cl
18216 65 61 6e 75 70 20 62 65 66 6f 72 65 20 72 65 74 eanup before ret
18217 75 72 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61 6c urning.. */.bal
18218 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 ance_cleanup:.
18219 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 sqlite3ScratchFr
1821a 65 65 28 61 70 43 65 6c 6c 29 3b 0a 20 20 66 6f ee(apCell);. fo
1821b 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 r(i=0; i<nOld; i
1821c 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 ++){. release
1821d 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a Page(apOld[i]);.
1821e 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
1821f 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 <nNew; i++){.
18220 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 4e releasePage(apN
18221 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72 ew[i]);. }.. r
18222 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
18223 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
18224 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e n is called when
18225 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f the root page o
18226 66 20 61 20 62 2d 74 72 65 65 20 73 74 72 75 63 f a b-tree struc
18227 74 75 72 65 20 69 73 0a 2a 2a 20 6f 76 65 72 66 ture is.** overf
18228 75 6c 6c 20 28 68 61 73 20 6f 6e 65 20 6f 72 20 ull (has one or
18229 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 more overflow pa
1822a 67 65 73 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 ges)..**.** A ne
1822b 77 20 63 68 69 6c 64 20 70 61 67 65 20 69 73 20 w child page is
1822c 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 allocated and th
1822d 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
1822e 65 20 63 75 72 72 65 6e 74 20 72 6f 6f 74 0a 2a e current root.*
1822f 2a 20 70 61 67 65 2c 20 69 6e 63 6c 75 64 69 6e * page, includin
18230 67 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 g overflow cells
18231 2c 20 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 , are copied int
18232 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 54 68 65 o the child. The
18233 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 root.** page is
18234 20 74 68 65 6e 20 6f 76 65 72 77 72 69 74 74 65 then overwritte
18235 6e 20 74 6f 20 6d 61 6b 65 20 69 74 20 61 6e 20 n to make it an
18236 65 6d 70 74 79 20 70 61 67 65 20 77 69 74 68 20 empty page with
18237 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 the right-child
18238 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e .** pointer poin
18239 74 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 77 20 ting to the new
1823a 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f page..**.** Befo
1823b 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 61 6c re returning, al
1823c 6c 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e l pointer-map en
1823d 74 72 69 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 tries correspond
1823e 69 6e 67 20 74 6f 20 70 61 67 65 73 20 0a 2a 2a ing to pages .**
1823f 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 63 68 that the new ch
18240 69 6c 64 2d 70 61 67 65 20 6e 6f 77 20 63 6f 6e ild-page now con
18241 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20 74 tains pointers t
18242 6f 20 61 72 65 20 75 70 64 61 74 65 64 2e 20 54 o are updated. T
18243 68 65 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 72 72 he.** entry corr
18244 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 esponding to the
18245 20 6e 65 77 20 72 69 67 68 74 2d 63 68 69 6c 64 new right-child
18246 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 pointer of the
18247 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 root.** page is
18248 61 6c 73 6f 20 75 70 64 61 74 65 64 2e 0a 2a 2a also updated..**
18249 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 .** If successfu
1824a 6c 2c 20 2a 70 70 43 68 69 6c 64 20 69 73 20 73 l, *ppChild is s
1824b 65 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 et to contain a
1824c 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 reference to the
1824d 20 63 68 69 6c 64 20 0a 2a 2a 20 70 61 67 65 20 child .** page
1824e 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 and SQLITE_OK is
1824f 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 returned. In th
18250 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c is case the call
18251 65 72 20 69 73 20 72 65 71 75 69 72 65 64 0a 2a er is required.*
18252 2a 20 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73 * to call releas
18253 65 50 61 67 65 28 29 20 6f 6e 20 2a 70 70 43 68 ePage() on *ppCh
18254 69 6c 64 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 ild exactly once
18255 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 . If an error oc
18256 63 75 72 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f curs,.** an erro
18257 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
18258 65 64 20 61 6e 64 20 2a 70 70 43 68 69 6c 64 20 ed and *ppChild
18259 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a is set to 0..*/.
1825a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e static int balan
1825b 63 65 5f 64 65 65 70 65 72 28 4d 65 6d 50 61 67 ce_deeper(MemPag
1825c 65 20 2a 70 52 6f 6f 74 2c 20 4d 65 6d 50 61 67 e *pRoot, MemPag
1825d 65 20 2a 2a 70 70 43 68 69 6c 64 29 7b 0a 20 20 e **ppChild){.
1825e 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
1825f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18260 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 66 * Return value f
18261 72 6f 6d 20 73 75 62 70 72 6f 63 65 64 75 72 65 rom subprocedure
18262 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a s */. MemPage *
18263 70 43 68 69 6c 64 20 3d 20 30 3b 20 20 20 20 20 pChild = 0;
18264 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
18265 20 74 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20 to a new child
18266 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 page */. Pgno p
18267 67 6e 6f 43 68 69 6c 64 20 3d 20 30 3b 20 20 20 gnoChild = 0;
18268 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
18269 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e number of the n
1826a 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f ew child page */
1826b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
1826c 20 3d 20 70 52 6f 6f 74 2d 3e 70 42 74 3b 20 20 = pRoot->pBt;
1826d 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 2a /* The BTree *
1826e 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 52 6f /.. assert( pRo
1826f 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 ot->nOverflow>0
18270 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
18271 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
18272 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a pBt->mutex) );..
18273 20 20 2f 2a 20 4d 61 6b 65 20 70 52 6f 6f 74 2c /* Make pRoot,
18274 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f the root page o
18275 66 20 74 68 65 20 62 2d 74 72 65 65 2c 20 77 72 f the b-tree, wr
18276 69 74 61 62 6c 65 2e 20 41 6c 6c 6f 63 61 74 65 itable. Allocate
18277 20 61 20 6e 65 77 20 0a 20 20 2a 2a 20 70 61 67 a new . ** pag
18278 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65 63 6f e that will beco
18279 6d 65 20 74 68 65 20 6e 65 77 20 72 69 67 68 74 me the new right
1827a 2d 63 68 69 6c 64 20 6f 66 20 70 50 61 67 65 2e -child of pPage.
1827b 20 43 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e Copy the conten
1827c 74 73 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e ts. ** of the n
1827d 6f 64 65 20 73 74 6f 72 65 64 20 6f 6e 20 70 52 ode stored on pR
1827e 6f 6f 74 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 oot into the new
1827f 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 2a child page.. *
18280 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 /. rc = sqlite3
18281 50 61 67 65 72 57 72 69 74 65 28 70 52 6f 6f 74 PagerWrite(pRoot
18282 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 ->pDbPage);. if
18283 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
18284 29 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f ){. rc = allo
18285 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 cateBtreePage(pB
18286 74 2c 26 70 43 68 69 6c 64 2c 26 70 67 6e 6f 43 t,&pChild,&pgnoC
18287 68 69 6c 64 2c 70 52 6f 6f 74 2d 3e 70 67 6e 6f hild,pRoot->pgno
18288 2c 30 29 3b 0a 20 20 20 20 63 6f 70 79 4e 6f 64 ,0);. copyNod
18289 65 43 6f 6e 74 65 6e 74 28 70 52 6f 6f 74 2c 20 eContent(pRoot,
1828a 70 43 68 69 6c 64 2c 20 26 72 63 29 3b 0a 20 20 pChild, &rc);.
1828b 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 if( ISAUTOVACU
1828c 55 4d 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d UM ){. ptrm
1828d 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 43 apPut(pBt, pgnoC
1828e 68 69 6c 64 2c 20 50 54 52 4d 41 50 5f 42 54 52 hild, PTRMAP_BTR
1828f 45 45 2c 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c EE, pRoot->pgno,
18290 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d &rc);. }. }
18291 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
18292 20 2a 70 70 43 68 69 6c 64 20 3d 20 30 3b 0a 20 *ppChild = 0;.
18293 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
18294 43 68 69 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 Child);. retu
18295 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 rn rc;. }. ass
18296 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
18297 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 43 68 rIswriteable(pCh
18298 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 20 29 3b ild->pDbPage) );
18299 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1829a 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
1829b 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 le(pRoot->pDbPag
1829c 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 e) );. assert(
1829d 70 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 pChild->nCell==p
1829e 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a Root->nCell );..
1829f 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 TRACE(("BALANC
182a0 45 3a 20 63 6f 70 79 20 72 6f 6f 74 20 25 64 20 E: copy root %d
182a1 69 6e 74 6f 20 25 64 5c 6e 22 2c 20 70 52 6f 6f into %d\n", pRoo
182a2 74 2d 3e 70 67 6e 6f 2c 20 70 43 68 69 6c 64 2d t->pgno, pChild-
182a3 3e 70 67 6e 6f 29 29 3b 0a 0a 20 20 2f 2a 20 43 >pgno));.. /* C
182a4 6f 70 79 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 opy the overflow
182a5 20 63 65 6c 6c 73 20 66 72 6f 6d 20 70 52 6f 6f cells from pRoo
182a6 74 20 74 6f 20 70 43 68 69 6c 64 20 2a 2f 0a 20 t to pChild */.
182a7 20 6d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e memcpy(pChild->
182a8 61 4f 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 61 4f aOvfl, pRoot->aO
182a9 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65 vfl, pRoot->nOve
182aa 72 66 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 52 6f rflow*sizeof(pRo
182ab 6f 74 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 3b 0a ot->aOvfl[0]));.
182ac 20 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 pChild->nOverf
182ad 6c 6f 77 20 3d 20 70 52 6f 6f 74 2d 3e 6e 4f 76 low = pRoot->nOv
182ae 65 72 66 6c 6f 77 3b 0a 0a 20 20 2f 2a 20 5a 65 erflow;.. /* Ze
182af 72 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 ro the contents
182b0 6f 66 20 70 52 6f 6f 74 2e 20 54 68 65 6e 20 69 of pRoot. Then i
182b1 6e 73 74 61 6c 6c 20 70 43 68 69 6c 64 20 61 73 nstall pChild as
182b2 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 the right-child
182b3 2e 20 2a 2f 0a 20 20 7a 65 72 6f 50 61 67 65 28 . */. zeroPage(
182b4 70 52 6f 6f 74 2c 20 70 43 68 69 6c 64 2d 3e 61 pRoot, pChild->a
182b5 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 46 5f 4c Data[0] & ~PTF_L
182b6 45 41 46 29 3b 0a 20 20 70 75 74 34 62 79 74 65 EAF);. put4byte
182b7 28 26 70 52 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 (&pRoot->aData[p
182b8 52 6f 6f 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b Root->hdrOffset+
182b9 38 5d 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b 0a 8], pgnoChild);.
182ba 0a 20 20 2a 70 70 43 68 69 6c 64 20 3d 20 70 43 . *ppChild = pC
182bb 68 69 6c 64 3b 0a 20 20 72 65 74 75 72 6e 20 53 hild;. return S
182bc 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
182bd 2a 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 74 ** The page that
182be 20 70 43 75 72 20 63 75 72 72 65 6e 74 6c 79 20 pCur currently
182bf 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 6a 75 points to has ju
182c0 73 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 st been modified
182c1 20 69 6e 0a 2a 2a 20 73 6f 6d 65 20 77 61 79 2e in.** some way.
182c2 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 This function f
182c3 69 67 75 72 65 73 20 6f 75 74 20 69 66 20 74 68 igures out if th
182c4 69 73 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 is modification
182c5 6d 65 61 6e 73 20 74 68 65 0a 2a 2a 20 74 72 65 means the.** tre
182c6 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 62 61 e needs to be ba
182c7 6c 61 6e 63 65 64 2c 20 61 6e 64 20 69 66 20 73 lanced, and if s
182c8 6f 20 63 61 6c 6c 73 20 74 68 65 20 61 70 70 72 o calls the appr
182c9 6f 70 72 69 61 74 65 20 62 61 6c 61 6e 63 69 6e opriate balancin
182ca 67 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 42 g .** routine. B
182cb 61 6c 61 6e 63 69 6e 67 20 72 6f 75 74 69 6e 65 alancing routine
182cc 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 62 s are:.**.** b
182cd 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 0a 2a alance_quick().*
182ce 2a 20 20 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 * balance_deep
182cf 65 72 28 29 0a 2a 2a 20 20 20 62 61 6c 61 6e 63 er().** balanc
182d0 65 5f 6e 6f 6e 72 6f 6f 74 28 29 0a 2a 2f 0a 73 e_nonroot().*/.s
182d1 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 tatic int balanc
182d2 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 e(BtCursor *pCur
182d3 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
182d4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74 LITE_OK;. const
182d5 20 69 6e 74 20 6e 4d 69 6e 20 3d 20 70 43 75 72 int nMin = pCur
182d6 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
182d7 65 20 2a 20 32 20 2f 20 33 3b 0a 20 20 75 38 20 e * 2 / 3;. u8
182d8 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 aBalanceQuickSpa
182d9 63 65 5b 31 33 5d 3b 0a 20 20 75 38 20 2a 70 46 ce[13];. u8 *pF
182da 72 65 65 20 3d 20 30 3b 0a 0a 20 20 54 45 53 54 ree = 0;.. TEST
182db 4f 4e 4c 59 28 20 69 6e 74 20 62 61 6c 61 6e 63 ONLY( int balanc
182dc 65 5f 71 75 69 63 6b 5f 63 61 6c 6c 65 64 20 3d e_quick_called =
182dd 20 30 20 29 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 0 );. TESTONLY
182de 28 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 64 65 ( int balance_de
182df 65 70 65 72 5f 63 61 6c 6c 65 64 20 3d 20 30 20 eper_called = 0
182e0 29 3b 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 );.. do {. i
182e1 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d nt iPage = pCur-
182e2 3e 69 50 61 67 65 3b 0a 20 20 20 20 4d 65 6d 50 >iPage;. MemP
182e3 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 age *pPage = pCu
182e4 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d r->apPage[iPage]
182e5 3b 0a 0a 20 20 20 20 69 66 28 20 69 50 61 67 65 ;.. if( iPage
182e6 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ==0 ){. if(
182e7 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f pPage->nOverflo
182e8 77 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 w ){. /*
182e9 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 The root page of
182ea 20 74 68 65 20 62 2d 74 72 65 65 20 69 73 20 6f the b-tree is o
182eb 76 65 72 66 75 6c 6c 2e 20 49 6e 20 74 68 69 73 verfull. In this
182ec 20 63 61 73 65 20 63 61 6c 6c 20 74 68 65 0a 20 case call the.
182ed 20 20 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 ** balanc
182ee 65 5f 64 65 65 70 65 72 28 29 20 66 75 6e 63 74 e_deeper() funct
182ef 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20 61 20 ion to create a
182f0 6e 65 77 20 63 68 69 6c 64 20 66 6f 72 20 74 68 new child for th
182f1 65 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 e root-page.
182f2 20 20 20 20 2a 2a 20 61 6e 64 20 63 6f 70 79 20 ** and copy
182f3 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 the current cont
182f4 65 6e 74 73 20 6f 66 20 74 68 65 20 72 6f 6f 74 ents of the root
182f5 2d 70 61 67 65 20 74 6f 20 69 74 2e 20 54 68 65 -page to it. The
182f6 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 65 78 74 . ** next
182f7 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 iteration of th
182f8 65 20 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62 e do-loop will b
182f9 61 6c 61 6e 63 65 20 74 68 65 20 63 68 69 6c 64 alance the child
182fa 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a page.. *
182fb 2f 20 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 / . asser
182fc 74 28 20 28 62 61 6c 61 6e 63 65 5f 64 65 65 70 t( (balance_deep
182fd 65 72 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20 er_called++)==0
182fe 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
182ff 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 70 balance_deeper(p
18300 50 61 67 65 2c 20 26 70 43 75 72 2d 3e 61 70 50 Page, &pCur->apP
18301 61 67 65 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20 age[1]);.
18302 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
18303 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
18304 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 31 3b pCur->iPage = 1;
18305 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 72 2d . pCur-
18306 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 >aiIdx[0] = 0;.
18307 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61 pCur->a
18308 69 49 64 78 5b 31 5d 20 3d 20 30 3b 0a 20 20 20 iIdx[1] = 0;.
18309 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1830a 43 75 72 2d 3e 61 70 50 61 67 65 5b 31 5d 2d 3e Cur->apPage[1]->
1830b 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a 20 20 20 nOverflow );.
1830c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
1830d 73 65 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 se{. brea
1830e 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
1830f 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 2d 3e else if( pPage->
18310 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 nOverflow==0 &&
18311 70 50 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 6e 4d pPage->nFree<=nM
18312 69 6e 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61 in ){. brea
18313 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 k;. }else{.
18314 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 20 63 6f MemPage * co
18315 6e 73 74 20 70 50 61 72 65 6e 74 20 3d 20 70 43 nst pParent = pC
18316 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 ur->apPage[iPage
18317 2d 31 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 -1];. int c
18318 6f 6e 73 74 20 69 49 64 78 20 3d 20 70 43 75 72 onst iIdx = pCur
18319 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 2d 31 5d ->aiIdx[iPage-1]
1831a 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ;.. rc = sq
1831b 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
1831c 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 pParent->pDbPage
1831d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
1831e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 =SQLITE_OK ){.#i
1831f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
18320 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a 20 T_QUICKBALANCE.
18321 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 if( pPage
18322 2d 3e 68 61 73 44 61 74 61 0a 20 20 20 20 20 20 ->hasData.
18323 20 20 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76 && pPage->nOv
18324 65 72 66 6c 6f 77 3d 3d 31 0a 20 20 20 20 20 20 erflow==1.
18325 20 20 20 26 26 20 70 50 61 67 65 2d 3e 61 4f 76 && pPage->aOv
18326 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 50 61 67 65 fl[0].idx==pPage
18327 2d 3e 6e 43 65 6c 6c 0a 20 20 20 20 20 20 20 20 ->nCell.
18328 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e && pParent->pgn
18329 6f 21 3d 31 0a 20 20 20 20 20 20 20 20 20 26 26 o!=1. &&
1832a 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d pParent->nCell=
1832b 3d 69 49 64 78 0a 20 20 20 20 20 20 20 20 29 7b =iIdx. ){
1832c 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 . /* Ca
1832d 6c 6c 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b ll balance_quick
1832e 28 29 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e () to create a n
1832f 65 77 20 73 69 62 6c 69 6e 67 20 6f 66 20 70 50 ew sibling of pP
18330 61 67 65 20 6f 6e 20 77 68 69 63 68 0a 20 20 20 age on which.
18331 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 73 74 6f ** to sto
18332 72 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 re the overflow
18333 63 65 6c 6c 2e 20 62 61 6c 61 6e 63 65 5f 71 75 cell. balance_qu
18334 69 63 6b 28 29 20 69 6e 73 65 72 74 73 20 61 20 ick() inserts a
18335 6e 65 77 20 63 65 6c 6c 0a 20 20 20 20 20 20 20 new cell.
18336 20 20 20 2a 2a 20 69 6e 74 6f 20 70 50 61 72 65 ** into pPare
18337 6e 74 2c 20 77 68 69 63 68 20 6d 61 79 20 63 61 nt, which may ca
18338 75 73 65 20 70 50 61 72 65 6e 74 20 6f 76 65 72 use pParent over
18339 66 6c 6f 77 2e 20 49 66 20 74 68 69 73 0a 20 20 flow. If this.
1833a 20 20 20 20 20 20 20 20 2a 2a 20 68 61 70 70 65 ** happe
1833b 6e 73 2c 20 74 68 65 20 6e 65 78 74 20 69 6e 74 ns, the next int
1833c 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 eration of the d
1833d 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62 61 6c 61 o-loop will bala
1833e 6e 63 65 20 70 50 61 72 65 6e 74 20 0a 20 20 20 nce pParent .
1833f 20 20 20 20 20 20 20 2a 2a 20 75 73 65 20 65 69 ** use ei
18340 74 68 65 72 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e ther balance_non
18341 72 6f 6f 74 28 29 20 6f 72 20 62 61 6c 61 6e 63 root() or balanc
18342 65 5f 64 65 65 70 65 72 28 29 2e 20 55 6e 74 69 e_deeper(). Unti
18343 6c 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 20 l this.
18344 20 2a 2a 20 68 61 70 70 65 6e 73 2c 20 74 68 65 ** happens, the
18345 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 69 overflow cell i
18346 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 s stored in the
18347 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 aBalanceQuickSpa
18348 63 65 5b 5d 0a 20 20 20 20 20 20 20 20 20 20 2a ce[]. *
18349 2a 20 62 75 66 66 65 72 2e 20 0a 20 20 20 20 20 * buffer. .
1834a 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 **.
1834b 20 20 2a 2a 20 54 68 65 20 70 75 72 70 6f 73 65 ** The purpose
1834c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
1834d 67 20 61 73 73 65 72 74 28 29 20 69 73 20 74 6f g assert() is to
1834e 20 63 68 65 63 6b 20 74 68 61 74 20 6f 6e 6c 79 check that only
1834f 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 a. **
18350 73 69 6e 67 6c 65 20 63 61 6c 6c 20 74 6f 20 62 single call to b
18351 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 20 69 alance_quick() i
18352 73 20 6d 61 64 65 20 66 6f 72 20 65 61 63 68 20 s made for each
18353 63 61 6c 6c 20 74 6f 20 74 68 69 73 0a 20 20 20 call to this.
18354 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 ** functi
18355 6f 6e 2e 20 49 66 20 74 68 69 73 20 77 65 72 65 on. If this were
18356 20 6e 6f 74 20 76 65 72 69 66 69 65 64 2c 20 61 not verified, a
18357 20 73 75 62 74 6c 65 20 62 75 67 20 69 6e 76 6f subtle bug invo
18358 6c 76 69 6e 67 20 72 65 75 73 65 0a 20 20 20 20 lving reuse.
18359 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 ** of the
1835a 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 aBalanceQuickSpa
1835b 63 65 5b 5d 20 6d 69 67 68 74 20 73 6e 65 61 6b ce[] might sneak
1835c 20 69 6e 2e 0a 20 20 20 20 20 20 20 20 20 20 2a in.. *
1835d 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 /. asse
1835e 72 74 28 20 28 62 61 6c 61 6e 63 65 5f 71 75 69 rt( (balance_qui
1835f 63 6b 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20 ck_called++)==0
18360 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 );. rc
18361 3d 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 = balance_quick(
18362 70 50 61 72 65 6e 74 2c 20 70 50 61 67 65 2c 20 pParent, pPage,
18363 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 aBalanceQuickSpa
18364 63 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c ce);. }el
18365 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 se.#endif.
18366 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a {. /*
18367 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 63 In this case, c
18368 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 all balance_nonr
18369 6f 6f 74 28 29 20 74 6f 20 72 65 64 69 73 74 72 oot() to redistr
1836a 69 62 75 74 65 20 63 65 6c 6c 73 0a 20 20 20 20 ibute cells.
1836b 20 20 20 20 20 20 2a 2a 20 62 65 74 77 65 65 6e ** between
1836c 20 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f pPage and up to
1836d 20 32 20 6f 66 20 69 74 73 20 73 69 62 6c 69 6e 2 of its siblin
1836e 67 20 70 61 67 65 73 2e 20 54 68 69 73 20 69 6e g pages. This in
1836f 76 6f 6c 76 65 73 0a 20 20 20 20 20 20 20 20 20 volves.
18370 20 2a 2a 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 ** modifying th
18371 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 50 e contents of pP
18372 61 72 65 6e 74 2c 20 77 68 69 63 68 20 6d 61 79 arent, which may
18373 20 63 61 75 73 65 20 70 50 61 72 65 6e 74 20 74 cause pParent t
18374 6f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 o. ** b
18375 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20 6f ecome overfull o
18376 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20 54 68 65 r underfull. The
18377 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 next iteration
18378 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 0a 20 of the do-loop.
18379 20 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c ** will
1837a 20 62 61 6c 61 6e 63 65 20 74 68 65 20 70 61 72 balance the par
1837b 65 6e 74 20 70 61 67 65 20 74 6f 20 63 6f 72 72 ent page to corr
1837c 65 63 74 20 74 68 69 73 2e 0a 20 20 20 20 20 20 ect this..
1837d 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 20 20 ** .
1837e 20 20 2a 2a 20 49 66 20 74 68 65 20 70 61 72 65 ** If the pare
1837f 6e 74 20 70 61 67 65 20 62 65 63 6f 6d 65 73 20 nt page becomes
18380 6f 76 65 72 66 75 6c 6c 2c 20 74 68 65 20 6f 76 overfull, the ov
18381 65 72 66 6c 6f 77 20 63 65 6c 6c 20 6f 72 20 63 erflow cell or c
18382 65 6c 6c 73 0a 20 20 20 20 20 20 20 20 20 20 2a ells. *
18383 2a 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 * are stored in
18384 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65 the pSpace buffe
18385 72 20 61 6c 6c 6f 63 61 74 65 64 20 69 6d 6d 65 r allocated imme
18386 64 69 61 74 65 6c 79 20 62 65 6c 6f 77 2e 20 0a diately below. .
18387 20 20 20 20 20 20 20 20 20 20 2a 2a 20 41 20 73 ** A s
18388 75 62 73 65 71 75 65 6e 74 20 69 74 65 72 61 74 ubsequent iterat
18389 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f ion of the do-lo
1838a 6f 70 20 77 69 6c 6c 20 64 65 61 6c 20 77 69 74 op will deal wit
1838b 68 20 74 68 69 73 20 62 79 0a 20 20 20 20 20 20 h this by.
1838c 20 20 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 62 ** calling b
1838d 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 alance_nonroot()
1838e 20 28 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 (balance_deeper
1838f 28 29 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 () may be called
18390 20 66 69 72 73 74 2c 0a 20 20 20 20 20 20 20 20 first,.
18391 20 20 2a 2a 20 62 75 74 20 69 74 20 64 6f 65 73 ** but it does
18392 6e 27 74 20 64 65 61 6c 20 77 69 74 68 20 6f 76 n't deal with ov
18393 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 2d 20 6a erflow cells - j
18394 75 73 74 20 6d 6f 76 65 73 20 74 68 65 6d 20 74 ust moves them t
18395 6f 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a o a. **
18396 20 64 69 66 66 65 72 65 6e 74 20 70 61 67 65 29 different page)
18397 2e 20 4f 6e 63 65 20 74 68 69 73 20 73 75 62 73 . Once this subs
18398 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20 62 equent call to b
18399 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 alance_nonroot()
1839a 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68 . ** h
1839b 61 73 20 63 6f 6d 70 6c 65 74 65 64 2c 20 69 74 as completed, it
1839c 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 6c 65 is safe to rele
1839d 61 73 65 20 74 68 65 20 70 53 70 61 63 65 20 62 ase the pSpace b
1839e 75 66 66 65 72 20 75 73 65 64 20 62 79 0a 20 20 uffer used by.
1839f 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70 ** the p
183a0 72 65 76 69 6f 75 73 20 63 61 6c 6c 2c 20 61 73 revious call, as
183a1 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 the overflow ce
183a2 6c 6c 20 64 61 74 61 20 77 69 6c 6c 20 68 61 76 ll data will hav
183a3 65 20 62 65 65 6e 20 0a 20 20 20 20 20 20 20 20 e been .
183a4 20 20 2a 2a 20 63 6f 70 69 65 64 20 65 69 74 68 ** copied eith
183a5 65 72 20 69 6e 74 6f 20 74 68 65 20 62 6f 64 79 er into the body
183a6 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 70 of a database p
183a7 61 67 65 20 6f 72 20 69 6e 74 6f 20 74 68 65 20 age or into the
183a8 6e 65 77 0a 20 20 20 20 20 20 20 20 20 20 2a 2a new. **
183a9 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20 70 pSpace buffer p
183aa 61 73 73 65 64 20 74 6f 20 74 68 65 20 6c 61 74 assed to the lat
183ab 74 65 72 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 ter call to bala
183ac 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 2e 0a 20 nce_nonroot()..
183ad 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 */.
183ae 20 20 20 20 20 20 75 38 20 2a 70 53 70 61 63 65 u8 *pSpace
183af 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 = sqlite3PageMa
183b0 6c 6c 6f 63 28 70 43 75 72 2d 3e 70 42 74 2d 3e lloc(pCur->pBt->
183b1 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 pageSize);.
183b2 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 rc = balanc
183b3 65 5f 6e 6f 6e 72 6f 6f 74 28 70 50 61 72 65 6e e_nonroot(pParen
183b4 74 2c 20 69 49 64 78 2c 20 70 53 70 61 63 65 2c t, iIdx, pSpace,
183b5 20 69 50 61 67 65 3d 3d 31 29 3b 0a 20 20 20 20 iPage==1);.
183b6 20 20 20 20 20 20 69 66 28 20 70 46 72 65 65 20 if( pFree
183b7 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f ){. /
183b8 2a 20 49 66 20 70 46 72 65 65 20 69 73 20 6e 6f * If pFree is no
183b9 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 t NULL, it point
183ba 73 20 74 6f 20 74 68 65 20 70 53 70 61 63 65 20 s to the pSpace
183bb 62 75 66 66 65 72 20 75 73 65 64 20 0a 20 20 20 buffer used .
183bc 20 20 20 20 20 20 20 20 20 2a 2a 20 62 79 20 61 ** by a
183bd 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 previous call t
183be 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f o balance_nonroo
183bf 74 28 29 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 t(). Its content
183c0 73 20 61 72 65 0a 20 20 20 20 20 20 20 20 20 20 s are.
183c1 20 20 2a 2a 20 6e 6f 77 20 73 74 6f 72 65 64 20 ** now stored
183c2 65 69 74 68 65 72 20 6f 6e 20 72 65 61 6c 20 64 either on real d
183c3 61 74 61 62 61 73 65 20 70 61 67 65 73 20 6f 72 atabase pages or
183c4 20 77 69 74 68 69 6e 20 74 68 65 20 0a 20 20 20 within the .
183c5 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 65 77 20 ** new
183c6 70 53 70 61 63 65 20 62 75 66 66 65 72 2c 20 73 pSpace buffer, s
183c7 6f 20 69 74 20 6d 61 79 20 62 65 20 73 61 66 65 o it may be safe
183c8 6c 79 20 66 72 65 65 64 20 68 65 72 65 2e 20 2a ly freed here. *
183c9 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 /. sq
183ca 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 46 lite3PageFree(pF
183cb 72 65 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ree);.
183cc 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 }.. /*
183cd 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65 The pSpace buffe
183ce 72 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 r will be freed
183cf 61 66 74 65 72 20 74 68 65 20 6e 65 78 74 20 63 after the next c
183d0 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 20 20 20 all to.
183d1 20 2a 2a 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 ** balance_nonr
183d2 6f 6f 74 28 29 2c 20 6f 72 20 6a 75 73 74 20 62 oot(), or just b
183d3 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 efore this funct
183d4 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 77 68 69 ion returns, whi
183d5 63 68 65 76 65 72 0a 20 20 20 20 20 20 20 20 20 chever.
183d6 20 2a 2a 20 63 6f 6d 65 73 20 66 69 72 73 74 2e ** comes first.
183d7 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 46 */. pF
183d8 72 65 65 20 3d 20 70 53 70 61 63 65 3b 0a 20 20 ree = pSpace;.
183d9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
183da 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f . pPage->nO
183db 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20 verflow = 0;..
183dc 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 /* The next
183dd 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 iteration of the
183de 20 64 6f 2d 6c 6f 6f 70 20 62 61 6c 61 6e 63 65 do-loop balance
183df 73 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 s the parent pag
183e0 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 e. */. rele
183e1 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a asePage(pPage);.
183e2 20 20 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 pCur->iPag
183e3 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 68 e--;. }. }wh
183e4 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ile( rc==SQLITE_
183e5 4f 4b 20 29 3b 0a 0a 20 20 69 66 28 20 70 46 72 OK );.. if( pFr
183e6 65 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ee ){. sqlite
183e7 33 50 61 67 65 46 72 65 65 28 70 46 72 65 65 29 3PageFree(pFree)
183e8 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
183e9 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 c;.}.../*.** Ins
183ea 65 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64 ert a new record
183eb 20 69 6e 74 6f 20 74 68 65 20 42 54 72 65 65 2e into the BTree.
183ec 20 20 54 68 65 20 6b 65 79 20 69 73 20 67 69 76 The key is giv
183ed 65 6e 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79 en by (pKey,nKey
183ee 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 ).** and the dat
183ef 61 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 70 a is given by (p
183f0 44 61 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68 Data,nData). Th
183f1 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 e cursor is used
183f2 20 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69 only to.** defi
183f3 6e 65 20 77 68 61 74 20 74 61 62 6c 65 20 74 68 ne what table th
183f4 65 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20 e record should
183f5 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f be inserted into
183f6 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a . The cursor.**
183f7 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
183f8 67 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f g at a random lo
183f9 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f cation..**.** Fo
183fa 72 20 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c r an INTKEY tabl
183fb 65 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79 e, only the nKey
183fc 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 value of the ke
183fd 79 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79 y is used. pKey
183fe 20 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 is.** ignored.
183ff 20 46 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 20 For a ZERODATA
18400 74 61 62 6c 65 2c 20 74 68 65 20 70 44 61 74 61 table, the pData
18401 20 61 6e 64 20 6e 44 61 74 61 20 61 72 65 20 62 and nData are b
18402 6f 74 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a oth ignored..**.
18403 2a 2a 20 49 66 20 74 68 65 20 73 65 65 6b 52 65 ** If the seekRe
18404 73 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20 69 sult parameter i
18405 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
18406 20 61 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 a successful ca
18407 6c 6c 20 74 6f 0a 2a 2a 20 4d 6f 76 65 74 6f 55 ll to.** MovetoU
18408 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 73 65 65 npacked() to see
18409 6b 20 63 75 72 73 6f 72 20 70 43 75 72 20 74 6f k cursor pCur to
1840a 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 68 61 (pKey, nKey) ha
1840b 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65 s already.** bee
1840c 6e 20 70 65 72 66 6f 72 6d 65 64 2e 20 73 65 65 n performed. see
1840d 6b 52 65 73 75 6c 74 20 69 73 20 74 68 65 20 73 kResult is the s
1840e 65 61 72 63 68 20 72 65 73 75 6c 74 20 72 65 74 earch result ret
1840f 75 72 6e 65 64 20 28 61 20 6e 65 67 61 74 69 76 urned (a negativ
18410 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 69 66 20 70 e.** number if p
18411 43 75 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e Cur points at an
18412 20 65 6e 74 72 79 20 74 68 61 74 20 69 73 20 73 entry that is s
18413 6d 61 6c 6c 65 72 20 74 68 61 6e 20 28 70 4b 65 maller than (pKe
18414 79 2c 20 6e 4b 65 79 29 2c 20 6f 72 0a 2a 2a 20 y, nKey), or.**
18415 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65 a positive value
18416 20 69 66 20 70 43 75 72 20 70 6f 69 6e 74 73 20 if pCur points
18417 61 74 20 61 6e 20 65 74 72 79 20 74 68 61 74 20 at an etry that
18418 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 0a is larger than .
18419 2a 2a 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 29 ** (pKey, nKey))
1841a 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 . .**.** If the
1841b 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d seekResult param
1841c 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f eter is non-zero
1841d 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 , then the calle
1841e 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 r guarantees tha
1841f 74 0a 2a 2a 20 63 75 72 73 6f 72 20 70 43 75 72 t.** cursor pCur
18420 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 is pointing at
18421 74 68 65 20 65 78 69 73 74 69 6e 67 20 63 6f 70 the existing cop
18422 79 20 6f 66 20 61 20 72 6f 77 20 74 68 61 74 20 y of a row that
18423 69 73 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 is to be.** over
18424 77 72 69 74 74 65 6e 2e 20 20 49 66 20 74 68 65 written. If the
18425 20 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61 seekResult para
18426 6d 65 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e meter is 0, then
18427 20 63 75 72 73 6f 72 20 70 43 75 72 20 6d 61 79 cursor pCur may
18428 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 6e 79 .** point to any
18429 20 65 6e 74 72 79 20 6f 72 20 74 6f 20 6e 6f 20 entry or to no
1842a 65 6e 74 72 79 20 61 74 20 61 6c 6c 20 61 6e 64 entry at all and
1842b 20 73 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f so this functio
1842c 6e 20 68 61 73 20 74 6f 20 73 65 65 6b 0a 2a 2a n has to seek.**
1842d 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 66 6f the cursor befo
1842e 72 65 20 74 68 65 20 6e 65 77 20 6b 65 79 20 63 re the new key c
1842f 61 6e 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a an be inserted..
18430 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18431 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
18432 65 65 49 6e 73 65 72 74 28 0a 20 20 42 74 43 75 eeInsert(. BtCu
18433 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 rsor *pCur,
18434 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
18435 73 65 72 74 20 64 61 74 61 20 69 6e 74 6f 20 74 sert data into t
18436 68 65 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 he table of this
18437 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e cursor */. con
18438 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 st void *pKey, i
18439 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 64 nKey, /* T
1843a 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 6e 65 he key of the ne
1843b 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f w record */. co
1843c 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c nst void *pData,
1843d 20 69 6e 74 20 6e 44 61 74 61 2c 20 20 2f 2a 20 int nData, /*
1843e 54 68 65 20 64 61 74 61 20 6f 66 20 74 68 65 20 The data of the
1843f 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 new record */.
18440 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20 int nZero,
18441 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18442 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72 * Number of extr
18443 61 20 30 20 62 79 74 65 73 20 74 6f 20 61 70 70 a 0 bytes to app
18444 65 6e 64 20 74 6f 20 64 61 74 61 20 2a 2f 0a 20 end to data */.
18445 20 69 6e 74 20 61 70 70 65 6e 64 42 69 61 73 2c int appendBias,
18446 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18447 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
18448 69 73 20 6c 69 6b 65 6c 79 20 61 6e 20 61 70 70 is likely an app
18449 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65 end */. int see
1844a 6b 52 65 73 75 6c 74 20 20 20 20 20 20 20 20 20 kResult
1844b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c /* Resul
1844c 74 20 6f 66 20 70 72 69 6f 72 20 4d 6f 76 65 74 t of prior Movet
1844d 6f 55 6e 70 61 63 6b 65 64 28 29 20 63 61 6c 6c oUnpacked() call
1844e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
1844f 0a 20 20 69 6e 74 20 6c 6f 63 20 3d 20 73 65 65 . int loc = see
18450 6b 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 kResult;
18451 20 20 2f 2a 20 2d 31 3a 20 62 65 66 6f 72 65 20 /* -1: before
18452 64 65 73 69 72 65 64 20 6c 6f 63 61 74 69 6f 6e desired location
18453 20 20 2b 31 3a 20 61 66 74 65 72 20 2a 2f 0a 20 +1: after */.
18454 20 69 6e 74 20 73 7a 4e 65 77 3b 0a 20 20 69 6e int szNew;. in
18455 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 t idx;. MemPage
18456 20 2a 70 50 61 67 65 3b 0a 20 20 42 74 72 65 65 *pPage;. Btree
18457 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 *p = pCur->pBtr
18458 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a ee;. BtShared *
18459 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
1845a 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6f unsigned char *o
1845b 6c 64 43 65 6c 6c 3b 0a 20 20 75 6e 73 69 67 6e ldCell;. unsign
1845c 65 64 20 63 68 61 72 20 2a 6e 65 77 43 65 6c 6c ed char *newCell
1845d 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 43 75 = 0;.. if( pCu
1845e 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
1845f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 61 R_FAULT ){. a
18460 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 ssert( pCur->ski
18461 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b pNext!=SQLITE_OK
18462 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 );. return p
18463 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 Cur->skipNext;.
18464 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 }.. assert( cu
18465 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
18466 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
18467 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 26 ( pCur->wrFlag &
18468 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 & pBt->inTransac
18469 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 tion==TRANS_WRIT
1846a 45 20 26 26 20 21 70 42 74 2d 3e 72 65 61 64 4f E && !pBt->readO
1846b 6e 6c 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 nly );. assert(
1846c 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54 hasSharedCacheT
1846d 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 75 72 ableLock(p, pCur
1846e 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 ->pgnoRoot, pCur
1846f 2d 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 32 ->pKeyInfo!=0, 2
18470 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 ) );.. /* Asser
18471 74 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 t that the calle
18472 72 20 68 61 73 20 62 65 65 6e 20 63 6f 6e 73 69 r has been consi
18473 73 74 65 6e 74 2e 20 49 66 20 74 68 69 73 20 63 stent. If this c
18474 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 ursor was opened
18475 0a 20 20 2a 2a 20 65 78 70 65 63 74 69 6e 67 20 . ** expecting
18476 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 2c an index b-tree,
18477 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 then the caller
18478 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72 should be inser
18479 74 69 6e 67 20 62 6c 6f 62 0a 20 20 2a 2a 20 6b ting blob. ** k
1847a 65 79 73 20 77 69 74 68 20 6e 6f 20 61 73 73 6f eys with no asso
1847b 63 69 61 74 65 64 20 64 61 74 61 2e 20 49 66 20 ciated data. If
1847c 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f the cursor was o
1847d 70 65 6e 65 64 20 65 78 70 65 63 74 69 6e 67 20 pened expecting
1847e 61 6e 0a 20 20 2a 2a 20 69 6e 74 6b 65 79 20 74 an. ** intkey t
1847f 61 62 6c 65 2c 20 74 68 65 20 63 61 6c 6c 65 72 able, the caller
18480 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72 should be inser
18481 74 69 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 79 ting integer key
18482 73 20 77 69 74 68 20 61 0a 20 20 2a 2a 20 62 6c s with a. ** bl
18483 6f 62 20 6f 66 20 61 73 73 6f 63 69 61 74 65 64 ob of associated
18484 20 64 61 74 61 2e 20 20 2a 2f 0a 20 20 61 73 73 data. */. ass
18485 65 72 74 28 20 28 70 4b 65 79 3d 3d 30 29 3d 3d ert( (pKey==0)==
18486 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d (pCur->pKeyInfo=
18487 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 =0) );.. /* If
18488 74 68 69 73 20 69 73 20 61 6e 20 69 6e 73 65 72 this is an inser
18489 74 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 62 t into a table b
1848a 2d 74 72 65 65 2c 20 69 6e 76 61 6c 69 64 61 74 -tree, invalidat
1848b 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 0a e any incrblob .
1848c 20 20 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 65 ** cursors ope
1848d 6e 20 6f 6e 20 74 68 65 20 72 6f 77 20 62 65 69 n on the row bei
1848e 6e 67 20 72 65 70 6c 61 63 65 64 20 28 61 73 73 ng replaced (ass
1848f 75 6d 69 6e 67 20 74 68 69 73 20 69 73 20 61 20 uming this is a
18490 72 65 70 6c 61 63 65 0a 20 20 2a 2a 20 6f 70 65 replace. ** ope
18491 72 61 74 69 6f 6e 20 2d 20 69 66 20 69 74 20 69 ration - if it i
18492 73 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c 6f s not, the follo
18493 77 69 6e 67 20 69 73 20 61 20 6e 6f 2d 6f 70 29 wing is a no-op)
18494 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 . */. if( pCur
18495 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20 29 7b ->pKeyInfo==0 ){
18496 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 49 . invalidateI
18497 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 70 ncrblobCursors(p
18498 2c 20 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a , nKey, 0);. }.
18499 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20 70 . /* Save the p
1849a 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20 ositions of any
1849b 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20 6f 70 other cursors op
1849c 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 en on this table
1849d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 73 .. **. ** In s
1849e 6f 6d 65 20 63 61 73 65 73 2c 20 74 68 65 20 63 ome cases, the c
1849f 61 6c 6c 20 74 6f 20 62 74 72 65 65 4d 6f 76 65 all to btreeMove
184a0 74 6f 28 29 20 62 65 6c 6f 77 20 69 73 20 61 20 to() below is a
184a1 6e 6f 2d 6f 70 2e 20 46 6f 72 0a 20 20 2a 2a 20 no-op. For. **
184a2 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 69 6e example, when in
184a3 73 65 72 74 69 6e 67 20 64 61 74 61 20 69 6e 74 serting data int
184a4 6f 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 61 o a table with a
184a5 75 74 6f 2d 67 65 6e 65 72 61 74 65 64 20 69 6e uto-generated in
184a6 74 65 67 65 72 0a 20 20 2a 2a 20 6b 65 79 73 2c teger. ** keys,
184a7 20 74 68 65 20 56 44 42 45 20 6c 61 79 65 72 20 the VDBE layer
184a8 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65 33 42 invokes sqlite3B
184a9 74 72 65 65 4c 61 73 74 28 29 20 74 6f 20 66 69 treeLast() to fi
184aa 67 75 72 65 20 6f 75 74 20 74 68 65 20 0a 20 20 gure out the .
184ab 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20 74 ** integer key t
184ac 6f 20 75 73 65 2e 20 49 74 20 74 68 65 6e 20 63 o use. It then c
184ad 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 alls this functi
184ae 6f 6e 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 69 on to actually i
184af 6e 73 65 72 74 20 74 68 65 20 0a 20 20 2a 2a 20 nsert the . **
184b0 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 69 6e data into the in
184b1 74 6b 65 79 20 42 2d 54 72 65 65 2e 20 49 6e 20 tkey B-Tree. In
184b2 74 68 69 73 20 63 61 73 65 20 62 74 72 65 65 4d this case btreeM
184b3 6f 76 65 74 6f 28 29 20 72 65 63 6f 67 6e 69 7a oveto() recogniz
184b4 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 es. ** that the
184b5 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61 cursor is alrea
184b6 64 79 20 77 68 65 72 65 20 69 74 20 6e 65 65 64 dy where it need
184b7 73 20 74 6f 20 62 65 20 61 6e 64 20 72 65 74 75 s to be and retu
184b8 72 6e 73 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a rns without. **
184b9 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b 2e doing any work.
184ba 20 54 6f 20 61 76 6f 69 64 20 74 68 77 61 72 74 To avoid thwart
184bb 69 6e 67 20 74 68 65 73 65 20 6f 70 74 69 6d 69 ing these optimi
184bc 7a 61 74 69 6f 6e 73 2c 20 69 74 20 69 73 20 69 zations, it is i
184bd 6d 70 6f 72 74 61 6e 74 0a 20 20 2a 2a 20 6e 6f mportant. ** no
184be 74 20 74 6f 20 63 6c 65 61 72 20 74 68 65 20 63 t to clear the c
184bf 75 72 73 6f 72 20 68 65 72 65 2e 0a 20 20 2a 2f ursor here.. */
184c0 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 . rc = saveAllC
184c1 75 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 ursors(pBt, pCur
184c2 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 ->pgnoRoot, pCur
184c3 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 );. if( rc ) re
184c4 74 75 72 6e 20 72 63 3b 0a 20 20 69 66 28 20 21 turn rc;. if( !
184c5 6c 6f 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 loc ){. rc =
184c6 62 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 btreeMoveto(pCur
184c7 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 61 70 , pKey, nKey, ap
184c8 70 65 6e 64 42 69 61 73 2c 20 26 6c 6f 63 29 3b pendBias, &loc);
184c9 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 . if( rc ) re
184ca 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 turn rc;. }. a
184cb 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 ssert( pCur->eSt
184cc 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
184cd 44 20 7c 7c 20 28 70 43 75 72 2d 3e 65 53 74 61 D || (pCur->eSta
184ce 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c te==CURSOR_INVAL
184cf 49 44 20 26 26 20 6c 6f 63 29 20 29 3b 0a 0a 20 ID && loc) );..
184d0 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 pPage = pCur->a
184d1 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
184d2 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 e];. assert( pP
184d3 61 67 65 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 6e age->intKey || n
184d4 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 Key>=0 );. asse
184d5 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 rt( pPage->leaf
184d6 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 || !pPage->intKe
184d7 79 20 29 3b 0a 0a 20 20 54 52 41 43 45 28 28 22 y );.. TRACE(("
184d8 49 4e 53 45 52 54 3a 20 74 61 62 6c 65 3d 25 64 INSERT: table=%d
184d9 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64 61 74 61 nkey=%lld ndata
184da 3d 25 64 20 70 61 67 65 3d 25 64 20 25 73 5c 6e =%d page=%d %s\n
184db 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 ",. pCu
184dc 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 6e 4b 65 r->pgnoRoot, nKe
184dd 79 2c 20 6e 44 61 74 61 2c 20 70 50 61 67 65 2d y, nData, pPage-
184de 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 >pgno,.
184df 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76 65 72 77 loc==0 ? "overw
184e0 72 69 74 65 22 20 3a 20 22 6e 65 77 20 65 6e 74 rite" : "new ent
184e1 72 79 22 29 29 3b 0a 20 20 61 73 73 65 72 74 28 ry"));. assert(
184e2 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 pPage->isInit )
184e3 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 ;. allocateTemp
184e4 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 6e 65 Space(pBt);. ne
184e5 77 43 65 6c 6c 20 3d 20 70 42 74 2d 3e 70 54 6d wCell = pBt->pTm
184e6 70 53 70 61 63 65 3b 0a 20 20 69 66 28 20 6e 65 pSpace;. if( ne
184e7 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74 75 72 wCell==0 ) retur
184e8 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
184e9 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 65 6c rc = fillInCel
184ea 6c 28 70 50 61 67 65 2c 20 6e 65 77 43 65 6c 6c l(pPage, newCell
184eb 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 70 44 , pKey, nKey, pD
184ec 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e 5a 65 72 ata, nData, nZer
184ed 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 20 69 66 o, &szNew);. if
184ee 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f ( rc ) goto end_
184ef 69 6e 73 65 72 74 3b 0a 20 20 61 73 73 65 72 74 insert;. assert
184f0 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c 53 69 7a ( szNew==cellSiz
184f1 65 50 74 72 28 70 50 61 67 65 2c 20 6e 65 77 43 ePtr(pPage, newC
184f2 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 ell) );. assert
184f3 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 45 4c 4c ( szNew<=MX_CELL
184f4 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20 _SIZE(pBt) );.
184f5 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 idx = pCur->aiId
184f6 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a x[pCur->iPage];.
184f7 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20 29 7b 0a if( loc==0 ){.
184f8 20 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b 0a 20 u16 szOld;.
184f9 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3c 70 assert( idx<p
184fa 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 Page->nCell );.
184fb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
184fc 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d agerWrite(pPage-
184fd 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 >pDbPage);. i
184fe 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 67 f( rc ){. g
184ff 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a oto end_insert;.
18500 20 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43 65 6c }. oldCel
18501 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 l = findCell(pPa
18502 67 65 2c 20 69 64 78 29 3b 0a 20 20 20 20 69 66 ge, idx);. if
18503 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 ( !pPage->leaf )
18504 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 6e {. memcpy(n
18505 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c 2c ewCell, oldCell,
18506 20 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 4);. }. s
18507 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a 65 50 zOld = cellSizeP
18508 74 72 28 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c tr(pPage, oldCel
18509 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 l);. rc = cle
1850a 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 6f 6c arCell(pPage, ol
1850b 64 43 65 6c 6c 29 3b 0a 20 20 20 20 64 72 6f 70 dCell);. drop
1850c 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c Cell(pPage, idx,
1850d 20 73 7a 4f 6c 64 2c 20 26 72 63 29 3b 0a 20 20 szOld, &rc);.
1850e 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
1850f 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 7d 65 end_insert;. }e
18510 6c 73 65 20 69 66 28 20 6c 6f 63 3c 30 20 26 26 lse if( loc<0 &&
18511 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 pPage->nCell>0
18512 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
18513 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 Page->leaf );.
18514 20 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d 3e idx = ++pCur->
18515 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
18516 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 e];. }else{.
18517 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
18518 6c 65 61 66 20 29 3b 0a 20 20 7d 0a 20 20 69 6e leaf );. }. in
18519 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 sertCell(pPage,
1851a 69 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 73 7a idx, newCell, sz
1851b 4e 65 77 2c 20 30 2c 20 30 2c 20 26 72 63 29 3b New, 0, 0, &rc);
1851c 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 . assert( rc!=S
1851d 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 QLITE_OK || pPag
1851e 65 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 70 50 e->nCell>0 || pP
1851f 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 age->nOverflow>0
18520 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 );.. /* If no
18521 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 65 error has occure
18522 64 20 61 6e 64 20 70 50 61 67 65 20 68 61 73 20 d and pPage has
18523 61 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c an overflow cell
18524 2c 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 28 29 , call balance()
18525 20 0a 20 20 2a 2a 20 74 6f 20 72 65 64 69 73 74 . ** to redist
18526 72 69 62 75 74 65 20 74 68 65 20 63 65 6c 6c 73 ribute the cells
18527 20 77 69 74 68 69 6e 20 74 68 65 20 74 72 65 65 within the tree
18528 2e 20 53 69 6e 63 65 20 62 61 6c 61 6e 63 65 28 . Since balance(
18529 29 20 6d 61 79 20 6d 6f 76 65 0a 20 20 2a 2a 20 ) may move. **
1852a 74 68 65 20 63 75 72 73 6f 72 2c 20 7a 65 72 6f the cursor, zero
1852b 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69 6e the BtCursor.in
1852c 66 6f 2e 6e 53 69 7a 65 20 61 6e 64 20 42 74 43 fo.nSize and BtC
1852d 75 72 73 6f 72 2e 76 61 6c 69 64 4e 4b 65 79 0a ursor.validNKey.
1852e 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 73 2e 0a ** variables..
1852f 20 20 2a 2a 0a 20 20 2a 2a 20 50 72 65 76 69 6f **. ** Previo
18530 75 73 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 us versions of S
18531 51 4c 69 74 65 20 63 61 6c 6c 65 64 20 6d 6f 76 QLite called mov
18532 65 54 6f 52 6f 6f 74 28 29 20 74 6f 20 6d 6f 76 eToRoot() to mov
18533 65 20 74 68 65 20 63 75 72 73 6f 72 0a 20 20 2a e the cursor. *
18534 2a 20 62 61 63 6b 20 74 6f 20 74 68 65 20 72 6f * back to the ro
18535 6f 74 20 70 61 67 65 20 61 73 20 62 61 6c 61 6e ot page as balan
18536 63 65 28 29 20 75 73 65 64 20 74 6f 20 69 6e 76 ce() used to inv
18537 61 6c 69 64 61 74 65 20 74 68 65 20 63 6f 6e 74 alidate the cont
18538 65 6e 74 73 0a 20 20 2a 2a 20 6f 66 20 42 74 43 ents. ** of BtC
18539 75 72 73 6f 72 2e 61 70 50 61 67 65 5b 5d 20 61 ursor.apPage[] a
1853a 6e 64 20 42 74 43 75 72 73 6f 72 2e 61 69 49 64 nd BtCursor.aiId
1853b 78 5b 5d 2e 20 49 6e 73 74 65 61 64 20 6f 66 20 x[]. Instead of
1853c 64 6f 69 6e 67 20 74 68 61 74 2c 0a 20 20 2a 2a doing that,. **
1853d 20 73 65 74 20 74 68 65 20 63 75 72 73 6f 72 20 set the cursor
1853e 73 74 61 74 65 20 74 6f 20 22 69 6e 76 61 6c 69 state to "invali
1853f 64 22 2e 20 54 68 69 73 20 6d 61 6b 65 73 20 63 d". This makes c
18540 6f 6d 6d 6f 6e 20 69 6e 73 65 72 74 20 6f 70 65 ommon insert ope
18541 72 61 74 69 6f 6e 73 0a 20 20 2a 2a 20 73 6c 69 rations. ** sli
18542 67 68 74 6c 79 20 66 61 73 74 65 72 2e 0a 20 20 ghtly faster..
18543 2a 2a 0a 20 20 2a 2a 20 54 68 65 72 65 20 69 73 **. ** There is
18544 20 61 20 73 75 62 74 6c 65 20 62 75 74 20 69 6d a subtle but im
18545 70 6f 72 74 61 6e 74 20 6f 70 74 69 6d 69 7a 61 portant optimiza
18546 74 69 6f 6e 20 68 65 72 65 20 74 6f 6f 2e 20 57 tion here too. W
18547 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 0a 20 20 hen inserting.
18548 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 72 65 63 6f ** multiple reco
18549 72 64 73 20 69 6e 74 6f 20 61 6e 20 69 6e 74 6b rds into an intk
1854a 65 79 20 62 2d 74 72 65 65 20 75 73 69 6e 67 20 ey b-tree using
1854b 61 20 73 69 6e 67 6c 65 20 63 75 72 73 6f 72 20 a single cursor
1854c 28 61 73 20 63 61 6e 0a 20 20 2a 2a 20 68 61 70 (as can. ** hap
1854d 70 65 6e 20 77 68 69 6c 65 20 70 72 6f 63 65 73 pen while proces
1854e 73 69 6e 67 20 61 6e 20 22 49 4e 53 45 52 54 20 sing an "INSERT
1854f 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54 22 INTO ... SELECT"
18550 20 73 74 61 74 65 6d 65 6e 74 29 2c 20 69 74 0a statement), it.
18551 20 20 2a 2a 20 69 73 20 61 64 76 61 6e 74 61 67 ** is advantag
18552 65 6f 75 73 20 74 6f 20 6c 65 61 76 65 20 74 68 eous to leave th
18553 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e e cursor pointin
18554 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e g to the last en
18555 74 72 79 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 try in. ** the
18556 62 2d 74 72 65 65 20 69 66 20 70 6f 73 73 69 62 b-tree if possib
18557 6c 65 2e 20 49 66 20 74 68 65 20 63 75 72 73 6f le. If the curso
18558 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 r is left pointi
18559 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 0a 20 ng to the last.
1855a 20 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65 ** entry in the
1855b 20 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 65 20 table, and the
1855c 6e 65 78 74 20 72 6f 77 20 69 6e 73 65 72 74 65 next row inserte
1855d 64 20 68 61 73 20 61 6e 20 69 6e 74 65 67 65 72 d has an integer
1855e 20 6b 65 79 0a 20 20 2a 2a 20 6c 61 72 67 65 72 key. ** larger
1855f 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67 65 73 than the larges
18560 74 20 65 78 69 73 74 69 6e 67 20 6b 65 79 2c 20 t existing key,
18561 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 it is possible t
18562 6f 20 69 6e 73 65 72 74 20 74 68 65 0a 20 20 2a o insert the. *
18563 2a 20 72 6f 77 20 77 69 74 68 6f 75 74 20 73 65 * row without se
18564 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 eking the cursor
18565 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 61 20 . This can be a
18566 62 69 67 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 big performance
18567 62 6f 6f 73 74 2e 0a 20 20 2a 2f 0a 20 20 70 43 boost.. */. pC
18568 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d ur->info.nSize =
18569 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 0;. pCur->vali
1856a 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 dNKey = 0;. if(
1856b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
1856c 26 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c & pPage->nOverfl
1856d 6f 77 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 ow ){. rc = b
1856e 61 6c 61 6e 63 65 28 70 43 75 72 29 3b 0a 0a 20 alance(pCur);..
1856f 20 20 20 2f 2a 20 4d 75 73 74 20 6d 61 6b 65 20 /* Must make
18570 73 75 72 65 20 6e 4f 76 65 72 66 6c 6f 77 20 69 sure nOverflow i
18571 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20 s reset to zero
18572 65 76 65 6e 20 69 66 20 74 68 65 20 62 61 6c 61 even if the bala
18573 6e 63 65 28 29 0a 20 20 20 20 2a 2a 20 66 61 69 nce(). ** fai
18574 6c 73 2e 20 49 6e 74 65 72 6e 61 6c 20 64 61 74 ls. Internal dat
18575 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 72 72 a structure corr
18576 75 70 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 75 uption will resu
18577 6c 74 20 6f 74 68 65 72 77 69 73 65 2e 20 0a 20 lt otherwise. .
18578 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 73 65 74 20 ** Also, set
18579 74 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 the cursor state
1857a 20 74 6f 20 69 6e 76 61 6c 69 64 2e 20 54 68 69 to invalid. Thi
1857b 73 20 73 74 6f 70 73 20 73 61 76 65 43 75 72 73 s stops saveCurs
1857c 6f 72 50 6f 73 69 74 69 6f 6e 28 29 0a 20 20 20 orPosition().
1857d 20 2a 2a 20 66 72 6f 6d 20 74 72 79 69 6e 67 20 ** from trying
1857e 74 6f 20 73 61 76 65 20 74 68 65 20 63 75 72 72 to save the curr
1857f 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 6f 66 20 ent position of
18580 74 68 65 20 63 75 72 73 6f 72 2e 20 20 2a 2f 0a the cursor. */.
18581 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 pCur->apPage
18582 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e [pCur->iPage]->n
18583 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 Overflow = 0;.
18584 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d pCur->eState =
18585 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b CURSOR_INVALID;
18586 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
18587 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
18588 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 72 66 ->iPage]->nOverf
18589 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 65 6e 64 5f 69 low==0 );..end_i
1858a 6e 73 65 72 74 3a 0a 20 20 72 65 74 75 72 6e 20 nsert:. return
1858b 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c rc;.}../*.** Del
1858c 65 74 65 20 74 68 65 20 65 6e 74 72 79 20 74 68 ete the entry th
1858d 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 at the cursor is
1858e 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 pointing to. T
1858f 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 he cursor.** is
18590 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 left pointing at
18591 20 61 20 61 72 62 69 74 72 61 72 79 20 6c 6f 63 a arbitrary loc
18592 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ation..*/.SQLITE
18593 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
18594 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 ite3BtreeDelete(
18595 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
18596 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43 . Btree *p = pC
18597 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 ur->pBtree;. Bt
18598 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
18599 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 >pBt;
1859a 20 20 20 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 . int rc;
1859b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1859c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
1859d 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 4d turn code */. M
1859e 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 emPage *pPage;
1859f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
185a0 20 20 20 20 2f 2a 20 50 61 67 65 20 74 6f 20 64 /* Page to d
185a1 65 6c 65 74 65 20 63 65 6c 6c 20 66 72 6f 6d 20 elete cell from
185a2 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
185a3 61 72 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 ar *pCell;
185a4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
185a5 6e 74 65 72 20 74 6f 20 63 65 6c 6c 20 74 6f 20 nter to cell to
185a6 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 delete */. int
185a7 69 43 65 6c 6c 49 64 78 3b 20 20 20 20 20 20 20 iCellIdx;
185a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
185a9 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 65 6c /* Index of cel
185aa 6c 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 l to delete */.
185ab 20 69 6e 74 20 69 43 65 6c 6c 44 65 70 74 68 3b int iCellDepth;
185ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
185ad 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20 6f /* Depth o
185ae 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 69 6e f node containin
185af 67 20 70 43 65 6c 6c 20 2a 2f 20 0a 0a 20 20 61 g pCell */ .. a
185b0 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
185b1 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
185b2 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e . assert( pBt->
185b3 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 inTransaction==T
185b4 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 RANS_WRITE );.
185b5 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 72 65 assert( !pBt->re
185b6 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 73 65 adOnly );. asse
185b7 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 rt( pCur->wrFlag
185b8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 61 );. assert( ha
185b9 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c sSharedCacheTabl
185ba 65 4c 6f 63 6b 28 70 2c 20 70 43 75 72 2d 3e 70 eLock(p, pCur->p
185bb 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2d 3e 70 gnoRoot, pCur->p
185bc 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 32 29 20 29 KeyInfo!=0, 2) )
185bd 3b 0a 20 20 61 73 73 65 72 74 28 20 21 68 61 73 ;. assert( !has
185be 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c ReadConflicts(p,
185bf 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 29 pCur->pgnoRoot)
185c0 20 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 );.. if( NEVER
185c1 28 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 (pCur->aiIdx[pCu
185c2 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d r->iPage]>=pCur-
185c3 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
185c4 61 67 65 5d 2d 3e 6e 43 65 6c 6c 29 20 0a 20 20 age]->nCell) .
185c5 20 7c 7c 20 4e 45 56 45 52 28 70 43 75 72 2d 3e || NEVER(pCur->
185c6 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 eState!=CURSOR_V
185c7 41 4c 49 44 29 0a 20 20 29 7b 0a 20 20 20 20 72 ALID). ){. r
185c8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
185c9 4f 52 3b 20 20 2f 2a 20 53 6f 6d 65 74 68 69 6e OR; /* Somethin
185ca 67 20 68 61 73 20 67 6f 6e 65 20 61 77 72 79 2e g has gone awry.
185cb 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 */. }.. /* If
185cc 20 74 68 69 73 20 69 73 20 61 20 64 65 6c 65 74 this is a delet
185cd 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 72 e operation to r
185ce 65 6d 6f 76 65 20 61 20 72 6f 77 20 66 72 6f 6d emove a row from
185cf 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2c a table b-tree,
185d0 0a 20 20 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 . ** invalidate
185d1 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63 75 any incrblob cu
185d2 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 rsors open on th
185d3 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 e row being dele
185d4 74 65 64 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 ted. */. if( p
185d5 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 Cur->pKeyInfo==0
185d6 20 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 ){. invalida
185d7 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 teIncrblobCursor
185d8 73 28 70 2c 20 70 43 75 72 2d 3e 69 6e 66 6f 2e s(p, pCur->info.
185d9 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 nKey, 0);. }..
185da 20 69 43 65 6c 6c 44 65 70 74 68 20 3d 20 70 43 iCellDepth = pC
185db 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 69 43 65 ur->iPage;. iCe
185dc 6c 6c 49 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 llIdx = pCur->ai
185dd 49 64 78 5b 69 43 65 6c 6c 44 65 70 74 68 5d 3b Idx[iCellDepth];
185de 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d . pPage = pCur-
185df 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65 70 >apPage[iCellDep
185e0 74 68 5d 3b 0a 20 20 70 43 65 6c 6c 20 3d 20 66 th];. pCell = f
185e1 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 indCell(pPage, i
185e2 43 65 6c 6c 49 64 78 29 3b 0a 0a 20 20 2f 2a 20 CellIdx);.. /*
185e3 49 66 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74 If the page cont
185e4 61 69 6e 69 6e 67 20 74 68 65 20 65 6e 74 72 79 aining the entry
185e5 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20 6e 6f to delete is no
185e6 74 20 61 20 6c 65 61 66 20 70 61 67 65 2c 20 6d t a leaf page, m
185e7 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 ove. ** the cur
185e8 73 6f 72 20 74 6f 20 74 68 65 20 6c 61 72 67 65 sor to the large
185e9 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 st entry in the
185ea 74 72 65 65 20 74 68 61 74 20 69 73 20 73 6d 61 tree that is sma
185eb 6c 6c 65 72 20 74 68 61 6e 0a 20 20 2a 2a 20 74 ller than. ** t
185ec 68 65 20 65 6e 74 72 79 20 62 65 69 6e 67 20 64 he entry being d
185ed 65 6c 65 74 65 64 2e 20 54 68 69 73 20 63 65 6c eleted. This cel
185ee 6c 20 77 69 6c 6c 20 72 65 70 6c 61 63 65 20 74 l will replace t
185ef 68 65 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 65 he cell being de
185f0 6c 65 74 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 leted. ** from
185f1 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 the internal nod
185f2 65 2e 20 54 68 65 20 27 70 72 65 76 69 6f 75 73 e. The 'previous
185f3 27 20 65 6e 74 72 79 20 69 73 20 75 73 65 64 20 ' entry is used
185f4 66 6f 72 20 74 68 69 73 20 69 6e 73 74 65 61 64 for this instead
185f5 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 27 6e 65 . ** of the 'ne
185f6 78 74 27 20 65 6e 74 72 79 2c 20 61 73 20 74 68 xt' entry, as th
185f7 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79 e previous entry
185f8 20 69 73 20 61 6c 77 61 79 73 20 61 20 70 61 72 is always a par
185f9 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73 75 t of the. ** su
185fa 62 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 79 b-tree headed by
185fb 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 the child page
185fc 6f 66 20 74 68 65 20 63 65 6c 6c 20 62 65 69 6e of the cell bein
185fd 67 20 64 65 6c 65 74 65 64 2e 20 54 68 69 73 20 g deleted. This
185fe 6d 61 6b 65 73 0a 20 20 2a 2a 20 62 61 6c 61 6e makes. ** balan
185ff 63 69 6e 67 20 74 68 65 20 74 72 65 65 20 66 6f cing the tree fo
18600 6c 6c 6f 77 69 6e 67 20 74 68 65 20 64 65 6c 65 llowing the dele
18601 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 65 61 73 te operation eas
18602 69 65 72 2e 20 20 2a 2f 0a 20 20 69 66 28 20 21 ier. */. if( !
18603 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
18604 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b 0a int notUsed;.
18605 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
18606 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 43 BtreePrevious(pC
18607 75 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20 ur, ¬Used);.
18608 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
18609 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a rn rc;. }.. /*
1860a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 Save the positi
1860b 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 ons of any other
1860c 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e cursors open on
1860d 20 74 68 69 73 20 74 61 62 6c 65 20 62 65 66 6f this table befo
1860e 72 65 0a 20 20 2a 2a 20 6d 61 6b 69 6e 67 20 61 re. ** making a
1860f 6e 79 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 ny modifications
18610 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 67 65 20 . Make the page
18611 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 65 containing the e
18612 6e 74 72 79 20 74 6f 20 62 65 20 0a 20 20 2a 2a ntry to be . **
18613 20 64 65 6c 65 74 65 64 20 77 72 69 74 61 62 6c deleted writabl
18614 65 2e 20 54 68 65 6e 20 66 72 65 65 20 61 6e 79 e. Then free any
18615 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 overflow pages
18616 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
18617 74 68 65 20 0a 20 20 2a 2a 20 65 6e 74 72 79 20 the . ** entry
18618 61 6e 64 20 66 69 6e 61 6c 6c 79 20 72 65 6d 6f and finally remo
18619 76 65 20 74 68 65 20 63 65 6c 6c 20 69 74 73 65 ve the cell itse
1861a 6c 66 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 lf from within t
1861b 68 65 20 70 61 67 65 2e 20 20 0a 20 20 2a 2f 0a he page. . */.
1861c 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 rc = saveAllCu
1861d 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d rsors(pBt, pCur-
1861e 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 >pgnoRoot, pCur)
1861f 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 ;. if( rc ) ret
18620 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 urn rc;. rc = s
18621 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
18622 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
18623 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 ;. if( rc ) ret
18624 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 63 urn rc;. rc = c
18625 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 learCell(pPage,
18626 70 43 65 6c 6c 29 3b 0a 20 20 64 72 6f 70 43 65 pCell);. dropCe
18627 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 49 ll(pPage, iCellI
18628 64 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 dx, cellSizePtr(
18629 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 2c 20 26 pPage, pCell), &
1862a 72 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 rc);. if( rc )
1862b 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a return rc;.. /*
1862c 20 49 66 20 74 68 65 20 63 65 6c 6c 20 64 65 6c If the cell del
1862d 65 74 65 64 20 77 61 73 20 6e 6f 74 20 6c 6f 63 eted was not loc
1862e 61 74 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 70 ated on a leaf p
1862f 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 63 75 age, then the cu
18630 72 73 6f 72 0a 20 20 2a 2a 20 69 73 20 63 75 72 rsor. ** is cur
18631 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 rently pointing
18632 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 to the largest e
18633 6e 74 72 79 20 69 6e 20 74 68 65 20 73 75 62 2d ntry in the sub-
18634 74 72 65 65 20 68 65 61 64 65 64 0a 20 20 2a 2a tree headed. **
18635 20 62 79 20 74 68 65 20 63 68 69 6c 64 2d 70 61 by the child-pa
18636 67 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 ge of the cell t
18637 68 61 74 20 77 61 73 20 6a 75 73 74 20 64 65 6c hat was just del
18638 65 74 65 64 20 66 72 6f 6d 20 61 6e 20 69 6e 74 eted from an int
18639 65 72 6e 61 6c 0a 20 20 2a 2a 20 6e 6f 64 65 2e ernal. ** node.
1863a 20 54 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74 The cell from t
1863b 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 6e 65 65 he leaf node nee
1863c 64 73 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 74 ds to be moved t
1863d 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 20 o the internal.
1863e 20 2a 2a 20 6e 6f 64 65 20 74 6f 20 72 65 70 6c ** node to repl
1863f 61 63 65 20 74 68 65 20 64 65 6c 65 74 65 64 20 ace the deleted
18640 63 65 6c 6c 2e 20 20 2a 2f 0a 20 20 69 66 28 20 cell. */. if(
18641 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
18642 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 65 MemPage *pLe
18643 61 66 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 af = pCur->apPag
18644 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a e[pCur->iPage];.
18645 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 int nCell;.
18646 20 20 20 50 67 6e 6f 20 6e 20 3d 20 70 43 75 72 Pgno n = pCur
18647 2d 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65 ->apPage[iCellDe
18648 70 74 68 2b 31 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 pth+1]->pgno;.
18649 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
1864a 2a 70 54 6d 70 3b 0a 0a 20 20 20 20 70 43 65 6c *pTmp;.. pCel
1864b 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 4c 65 l = findCell(pLe
1864c 61 66 2c 20 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c af, pLeaf->nCell
1864d 2d 31 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d -1);. nCell =
1864e 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c 65 cellSizePtr(pLe
1864f 61 66 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 af, pCell);.
18650 61 73 73 65 72 74 28 20 4d 58 5f 43 45 4c 4c 5f assert( MX_CELL_
18651 53 49 5a 45 28 70 42 74 29 3e 3d 6e 43 65 6c 6c SIZE(pBt)>=nCell
18652 20 29 3b 0a 0a 20 20 20 20 61 6c 6c 6f 63 61 74 );.. allocat
18653 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b eTempSpace(pBt);
18654 0a 20 20 20 20 70 54 6d 70 20 3d 20 70 42 74 2d . pTmp = pBt-
18655 3e 70 54 6d 70 53 70 61 63 65 3b 0a 0a 20 20 20 >pTmpSpace;..
18656 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
18657 65 72 57 72 69 74 65 28 70 4c 65 61 66 2d 3e 70 erWrite(pLeaf->p
18658 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 6e 73 DbPage);. ins
18659 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 ertCell(pPage, i
1865a 43 65 6c 6c 49 64 78 2c 20 70 43 65 6c 6c 2d 34 CellIdx, pCell-4
1865b 2c 20 6e 43 65 6c 6c 2b 34 2c 20 70 54 6d 70 2c , nCell+4, pTmp,
1865c 20 6e 2c 20 26 72 63 29 3b 0a 20 20 20 20 64 72 n, &rc);. dr
1865d 6f 70 43 65 6c 6c 28 70 4c 65 61 66 2c 20 70 4c opCell(pLeaf, pL
1865e 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31 2c 20 6e 43 eaf->nCell-1, nC
1865f 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 ell, &rc);. i
18660 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
18661 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 61 6c c;. }.. /* Bal
18662 61 6e 63 65 20 74 68 65 20 74 72 65 65 2e 20 49 ance the tree. I
18663 66 20 74 68 65 20 65 6e 74 72 79 20 64 65 6c 65 f the entry dele
18664 74 65 64 20 77 61 73 20 6c 6f 63 61 74 65 64 20 ted was located
18665 6f 6e 20 61 20 6c 65 61 66 20 70 61 67 65 2c 0a on a leaf page,.
18666 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 75 ** then the cu
18667 72 73 6f 72 20 73 74 69 6c 6c 20 70 6f 69 6e 74 rsor still point
18668 73 20 74 6f 20 74 68 61 74 20 70 61 67 65 2e 20 s to that page.
18669 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
1866a 20 66 69 72 73 74 0a 20 20 2a 2a 20 63 61 6c 6c first. ** call
1866b 20 74 6f 20 62 61 6c 61 6e 63 65 28 29 20 72 65 to balance() re
1866c 70 61 69 72 73 20 74 68 65 20 74 72 65 65 2c 20 pairs the tree,
1866d 61 6e 64 20 74 68 65 20 69 66 28 2e 2e 2e 29 20 and the if(...)
1866e 63 6f 6e 64 69 74 69 6f 6e 20 69 73 0a 20 20 2a condition is. *
1866f 2a 20 6e 65 76 65 72 20 74 72 75 65 2e 0a 20 20 * never true..
18670 2a 2a 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 **. ** Otherwis
18671 65 2c 20 69 66 20 74 68 65 20 65 6e 74 72 79 20 e, if the entry
18672 64 65 6c 65 74 65 64 20 77 61 73 20 6f 6e 20 61 deleted was on a
18673 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 n internal node
18674 70 61 67 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 page, then. **
18675 70 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 pCur is pointing
18676 20 74 6f 20 74 68 65 20 6c 65 61 66 20 70 61 67 to the leaf pag
18677 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 63 e from which a c
18678 65 6c 6c 20 77 61 73 20 72 65 6d 6f 76 65 64 20 ell was removed
18679 74 6f 0a 20 20 2a 2a 20 72 65 70 6c 61 63 65 20 to. ** replace
1867a 74 68 65 20 63 65 6c 6c 20 64 65 6c 65 74 65 64 the cell deleted
1867b 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e from the intern
1867c 61 6c 20 6e 6f 64 65 2e 20 54 68 69 73 20 69 73 al node. This is
1867d 20 73 6c 69 67 68 74 6c 79 0a 20 20 2a 2a 20 74 slightly. ** t
1867e 72 69 63 6b 79 20 61 73 20 74 68 65 20 6c 65 61 ricky as the lea
1867f 66 20 6e 6f 64 65 20 6d 61 79 20 62 65 20 75 6e f node may be un
18680 64 65 72 66 75 6c 6c 2c 20 61 6e 64 20 74 68 65 derfull, and the
18681 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 6d internal node m
18682 61 79 0a 20 20 2a 2a 20 62 65 20 65 69 74 68 65 ay. ** be eithe
18683 72 20 75 6e 64 65 72 20 6f 72 20 6f 76 65 72 66 r under or overf
18684 75 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ull. In this cas
18685 65 20 72 75 6e 20 74 68 65 20 62 61 6c 61 6e 63 e run the balanc
18686 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 0a 20 20 ing algorithm.
18687 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 61 66 20 6e ** on the leaf n
18688 6f 64 65 20 66 69 72 73 74 2e 20 49 66 20 74 68 ode first. If th
18689 65 20 62 61 6c 61 6e 63 65 20 70 72 6f 63 65 65 e balance procee
1868a 64 73 20 66 61 72 20 65 6e 6f 75 67 68 20 75 70 ds far enough up
1868b 20 74 68 65 0a 20 20 2a 2a 20 74 72 65 65 20 74 the. ** tree t
1868c 68 61 74 20 77 65 20 63 61 6e 20 62 65 20 73 75 hat we can be su
1868d 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 6f 62 re that any prob
1868e 6c 65 6d 20 69 6e 20 74 68 65 20 69 6e 74 65 72 lem in the inter
1868f 6e 61 6c 20 6e 6f 64 65 20 68 61 73 0a 20 20 2a nal node has. *
18690 2a 20 62 65 65 6e 20 63 6f 72 72 65 63 74 65 64 * been corrected
18691 2c 20 73 6f 20 62 65 20 69 74 2e 20 4f 74 68 65 , so be it. Othe
18692 72 77 69 73 65 2c 20 61 66 74 65 72 20 62 61 6c rwise, after bal
18693 61 6e 63 69 6e 67 20 74 68 65 20 6c 65 61 66 20 ancing the leaf
18694 6e 6f 64 65 2c 0a 20 20 2a 2a 20 77 61 6c 6b 20 node,. ** walk
18695 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 68 the cursor up th
18696 65 20 74 72 65 65 20 74 6f 20 74 68 65 20 69 6e e tree to the in
18697 74 65 72 6e 61 6c 20 6e 6f 64 65 20 61 6e 64 20 ternal node and
18698 62 61 6c 61 6e 63 65 20 69 74 20 61 73 20 0a 20 balance it as .
18699 20 2a 2a 20 77 65 6c 6c 2e 20 20 2a 2f 0a 20 20 ** well. */.
1869a 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 rc = balance(pCu
1869b 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 r);. if( rc==SQ
1869c 4c 49 54 45 5f 4f 4b 20 26 26 20 70 43 75 72 2d LITE_OK && pCur-
1869d 3e 69 50 61 67 65 3e 69 43 65 6c 6c 44 65 70 74 >iPage>iCellDept
1869e 68 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 h ){. while(
1869f 70 43 75 72 2d 3e 69 50 61 67 65 3e 69 43 65 6c pCur->iPage>iCel
186a0 6c 44 65 70 74 68 20 29 7b 0a 20 20 20 20 20 20 lDepth ){.
186a1 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 releasePage(pCur
186a2 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
186a3 50 61 67 65 2d 2d 5d 29 3b 0a 20 20 20 20 7d 0a Page--]);. }.
186a4 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 rc = balance
186a5 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 (pCur);. }.. i
186a6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
186a7 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f ){. moveToRo
186a8 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20 ot(pCur);. }.
186a9 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
186aa 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
186ab 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57 BTree table. W
186ac 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62 rite into *piTab
186ad 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e le the page.** n
186ae 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f umber for the ro
186af 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e ot page of the n
186b0 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ew table..**.**
186b1 54 68 65 20 74 79 70 65 20 6f 66 20 74 79 70 65 The type of type
186b2 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 is determined b
186b3 79 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 y the flags para
186b4 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65 meter. Only the
186b5 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 .** following va
186b6 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72 lues of flags ar
186b7 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 e currently in u
186b8 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65 se. Other value
186b9 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d s for.** flags m
186ba 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a ight not work:.*
186bb 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49 *.** BTREE_I
186bc 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46 NTKEY|BTREE_LEAF
186bd 44 41 54 41 20 20 20 20 20 55 73 65 64 20 66 6f DATA Used fo
186be 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74 r SQL tables wit
186bf 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20 h rowid keys.**
186c0 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 BTREE_ZERODA
186c1 54 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 TA
186c2 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c Used for SQL
186c3 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74 indices.*/.stat
186c4 69 63 20 69 6e 74 20 62 74 72 65 65 43 72 65 61 ic int btreeCrea
186c5 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 teTable(Btree *p
186c6 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20 , int *piTable,
186c7 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 74 int flags){. Bt
186c8 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
186c9 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 >pBt;. MemPage
186ca 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 70 *pRoot;. Pgno p
186cb 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 gnoRoot;. int r
186cc 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 c;.. assert( sq
186cd 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
186ce 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 utex(p) );. ass
186cf 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e ert( pBt->inTran
186d0 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 saction==TRANS_W
186d1 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 RITE );. assert
186d2 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 ( !pBt->readOnly
186d3 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 );..#ifdef SQLI
186d4 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
186d5 55 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 UM. rc = alloca
186d6 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c teBtreePage(pBt,
186d7 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f &pRoot, &pgnoRo
186d8 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66 28 ot, 1, 0);. if(
186d9 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 rc ){. retur
186da 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a n rc;. }.#else.
186db 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 if( pBt->autoV
186dc 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e acuum ){. Pgn
186dd 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20 20 o pgnoMove;
186de 20 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 65 20 /* Move a page
186df 68 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f 6f here to make roo
186e0 6d 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 m for the root-p
186e1 61 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 age */. MemPa
186e2 67 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20 2f ge *pPageMove; /
186e3 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 6d 6f * The page to mo
186e4 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f ve to. */.. /
186e5 2a 20 43 72 65 61 74 69 6e 67 20 61 20 6e 65 77 * Creating a new
186e6 20 74 61 62 6c 65 20 6d 61 79 20 70 72 6f 62 61 table may proba
186e7 62 6c 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 bly require movi
186e8 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20 64 ng an existing d
186e9 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 74 atabase. ** t
186ea 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20 o make room for
186eb 74 68 65 20 6e 65 77 20 74 61 62 6c 65 73 20 72 the new tables r
186ec 6f 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 61 73 oot page. In cas
186ed 65 20 74 68 69 73 20 70 61 67 65 20 74 75 72 6e e this page turn
186ee 73 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f 20 s. ** out to
186ef 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 be an overflow p
186f0 61 67 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20 age, delete all
186f1 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d 61 overflow page-ma
186f2 70 20 63 61 63 68 65 73 0a 20 20 20 20 2a 2a 20 p caches. **
186f3 68 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 75 72 held by open cur
186f4 73 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 sors.. */.
186f5 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 invalidateAllOv
186f6 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 erflowCache(pBt)
186f7 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 ;.. /* Read t
186f8 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61 he value of meta
186f9 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 [3] from the dat
186fa 61 62 61 73 65 20 74 6f 20 64 65 74 65 72 6d 69 abase to determi
186fb 6e 65 20 77 68 65 72 65 20 74 68 65 0a 20 20 20 ne where the.
186fc 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 ** root page of
186fd 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 73 the new table s
186fe 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b 33 hould go. meta[3
186ff 5d 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74 ] is the largest
18700 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a root-page. *
18701 2a 20 63 72 65 61 74 65 64 20 73 6f 20 66 61 72 * created so far
18702 2c 20 73 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f , so the new roo
18703 74 2d 70 61 67 65 20 69 73 20 28 6d 65 74 61 5b t-page is (meta[
18704 33 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 3]+1).. */.
18705 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 sqlite3BtreeGe
18706 74 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f 4c tMeta(p, BTREE_L
18707 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 ARGEST_ROOT_PAGE
18708 2c 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 , &pgnoRoot);.
18709 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 pgnoRoot++;..
1870a 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f /* The new ro
1870b 6f 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 ot-page may not
1870c 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 be allocated on
1870d 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 a pointer-map pa
1870e 67 65 2c 20 6f 72 20 74 68 65 0a 20 20 20 20 2a ge, or the. *
1870f 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 70 * PENDING_BYTE p
18710 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 age.. */.
18711 77 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 3d while( pgnoRoot=
18712 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 =PTRMAP_PAGENO(p
18713 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c Bt, pgnoRoot) ||
18714 0a 20 20 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f . pgnoRoo
18715 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f t==PENDING_BYTE_
18716 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 PAGE(pBt) ){.
18717 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 pgnoRoot++;.
18718 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
18719 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a pgnoRoot>=3 );.
1871a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 . /* Allocate
1871b 20 61 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 a page. The pag
1871c 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 e that currently
1871d 20 72 65 73 69 64 65 73 20 61 74 20 70 67 6e 6f resides at pgno
1871e 52 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a 2a Root will. **
1871f 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 be moved to the
18720 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 allocated page
18721 28 75 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c 6f (unless the allo
18722 63 61 74 65 64 20 70 61 67 65 20 68 61 70 70 65 cated page happe
18723 6e 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 73 ns. ** to res
18724 69 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 29 ide at pgnoRoot)
18725 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
18726 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 = allocateBtreeP
18727 61 67 65 28 70 42 74 2c 20 26 70 50 61 67 65 4d age(pBt, &pPageM
18728 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c 20 ove, &pgnoMove,
18729 70 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 20 pgnoRoot, 1);.
1872a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1872b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 _OK ){. ret
1872c 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 urn rc;. }..
1872d 20 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65 21 if( pgnoMove!
1872e 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20 =pgnoRoot ){.
1872f 20 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 69 /* pgnoRoot i
18730 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 s the page that
18731 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72 will be used for
18732 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f the root-page o
18733 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e f. ** the n
18734 65 77 20 74 61 62 6c 65 20 28 61 73 73 75 6d 69 ew table (assumi
18735 6e 67 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20 ng an error did
18736 6e 6f 74 20 6f 63 63 75 72 29 2e 20 42 75 74 20 not occur). But
18737 77 65 20 77 65 72 65 0a 20 20 20 20 20 20 2a 2a we were. **
18738 20 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f 4d allocated pgnoM
18739 6f 76 65 2e 20 49 66 20 72 65 71 75 69 72 65 64 ove. If required
1873a 20 28 69 2e 65 2e 20 69 66 20 69 74 20 77 61 73 (i.e. if it was
1873b 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a 20 not allocated.
1873c 20 20 20 20 20 2a 2a 20 62 79 20 65 78 74 65 6e ** by exten
1873d 64 69 6e 67 20 74 68 65 20 66 69 6c 65 29 2c 20 ding the file),
1873e 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 the current page
1873f 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 70 67 6e at position pgn
18740 6f 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 69 oMove. ** i
18741 73 20 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e 61 s already journa
18742 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 led.. */.
18743 20 20 20 20 75 38 20 65 54 79 70 65 20 3d 20 30 u8 eType = 0
18744 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 50 74 ;. Pgno iPt
18745 72 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 20 20 rPage = 0;..
18746 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
18747 61 67 65 4d 6f 76 65 29 3b 0a 0a 20 20 20 20 20 ageMove);..
18748 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 /* Move the pag
18749 65 20 63 75 72 72 65 6e 74 6c 79 20 61 74 20 70 e currently at p
1874a 67 6e 6f 52 6f 6f 74 20 74 6f 20 70 67 6e 6f 4d gnoRoot to pgnoM
1874b 6f 76 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 ove. */. rc
1874c 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
1874d 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 pBt, pgnoRoot, &
1874e 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20 pRoot, 0);.
1874f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
18750 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 OK ){. re
18751 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
18752 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d . rc = ptrm
18753 61 70 47 65 74 28 70 42 74 2c 20 70 67 6e 6f 52 apGet(pBt, pgnoR
18754 6f 6f 74 2c 20 26 65 54 79 70 65 2c 20 26 69 50 oot, &eType, &iP
18755 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 trPage);. i
18756 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 f( eType==PTRMAP
18757 5f 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54 79 _ROOTPAGE || eTy
18758 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 pe==PTRMAP_FREEP
18759 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 AGE ){. r
1875a 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 c = SQLITE_CORRU
1875b 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d PT_BKPT;. }
1875c 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
1875d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1875e 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
1875f 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 pRoot);.
18760 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
18761 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 }. assert(
18762 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 eType!=PTRMAP_R
18763 4f 4f 54 50 41 47 45 20 29 3b 0a 20 20 20 20 20 OOTPAGE );.
18764 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d assert( eType!=
18765 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 PTRMAP_FREEPAGE
18766 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 );. rc = re
18767 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20 locatePage(pBt,
18768 70 52 6f 6f 74 2c 20 65 54 79 70 65 2c 20 69 50 pRoot, eType, iP
18769 74 72 50 61 67 65 2c 20 70 67 6e 6f 4d 6f 76 65 trPage, pgnoMove
1876a 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 , 0);. rele
1876b 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a asePage(pRoot);.
1876c 0a 20 20 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e . /* Obtain
1876d 20 74 68 65 20 70 61 67 65 20 61 74 20 70 67 6e the page at pgn
1876e 6f 52 6f 6f 74 20 2a 2f 0a 20 20 20 20 20 20 69 oRoot */. i
1876f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
18770 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
18771 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 rn rc;. }.
18772 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 rc = btreeG
18773 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f etPage(pBt, pgno
18774 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29 Root, &pRoot, 0)
18775 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
18776 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
18777 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
18778 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 }. rc
18779 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
1877a 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 rite(pRoot->pDbP
1877b 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 age);. if(
1877c 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1877d 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
1877e 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 Page(pRoot);.
1877f 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
18780 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
18781 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20 3d e{. pRoot =
18782 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20 20 pPageMove;.
18783 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 } .. /* Updat
18784 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 e the pointer-ma
18785 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61 20 p and meta-data
18786 77 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f 6f with the new roo
18787 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 2a t-page number. *
18788 2f 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 /. ptrmapPut(
18789 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 50 pBt, pgnoRoot, P
1878a 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 TRMAP_ROOTPAGE,
1878b 30 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 0, &rc);. if(
1878c 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c rc ){. rel
1878d 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b easePage(pRoot);
1878e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
1878f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
18790 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 sqlite3BtreeUpd
18791 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70 67 ateMeta(p, 4, pg
18792 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66 28 noRoot);. if(
18793 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c rc ){. rel
18794 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b easePage(pRoot);
18795 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
18796 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 ;. }.. }else
18797 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 {. rc = alloc
18798 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 ateBtreePage(pBt
18799 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 , &pRoot, &pgnoR
1879a 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 oot, 1, 0);.
1879b 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
1879c 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 rc;. }.#endif.
1879d 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1879e 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
1879f 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 (pRoot->pDbPage)
187a0 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 );. zeroPage(p
187a1 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50 54 Root, flags | PT
187a2 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69 74 F_LEAF);. sqlit
187a3 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 52 6f e3PagerUnref(pRo
187a4 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ot->pDbPage);.
187a5 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 29 *piTable = (int)
187a6 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74 75 pgnoRoot;. retu
187a7 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
187a8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
187a9 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
187aa 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 reateTable(Btree
187ab 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c *p, int *piTabl
187ac 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 e, int flags){.
187ad 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 int rc;. sqlit
187ae 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
187af 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 72 65 . rc = btreeCre
187b0 61 74 65 54 61 62 6c 65 28 70 2c 20 70 69 54 61 ateTable(p, piTa
187b1 62 6c 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 73 ble, flags);. s
187b2 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
187b3 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
187b4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 ;.}../*.** Erase
187b5 20 74 68 65 20 67 69 76 65 6e 20 64 61 74 61 62 the given datab
187b6 61 73 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c ase page and all
187b7 20 69 74 73 20 63 68 69 6c 64 72 65 6e 2e 20 20 its children.
187b8 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 70 61 Return.** the pa
187b9 67 65 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 ge to the freeli
187ba 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e st..*/.static in
187bb 74 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 t clearDatabaseP
187bc 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 age(. BtShared
187bd 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 *pBt,
187be 2f 2a 20 54 68 65 20 42 54 72 65 65 20 74 68 61 /* The BTree tha
187bf 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 t contains the t
187c0 61 62 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 able */. Pgno p
187c1 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 gno,
187c2 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 /* Page number t
187c3 6f 20 63 6c 65 61 72 20 2a 2f 0a 20 20 69 6e 74 o clear */. int
187c4 20 66 72 65 65 50 61 67 65 46 6c 61 67 2c 20 20 freePageFlag,
187c5 20 20 20 2f 2a 20 44 65 61 6c 6c 6f 63 61 74 65 /* Deallocate
187c6 20 70 61 67 65 20 69 66 20 74 72 75 65 20 2a 2f page if true */
187c7 0a 20 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67 65 . int *pnChange
187c8 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 .){. MemPage *p
187c9 50 61 67 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a Page;. int rc;.
187ca 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
187cb 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b *pCell;. int i;
187cc 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
187cd 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
187ce 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
187cf 69 66 28 20 70 67 6e 6f 3e 70 61 67 65 72 50 61 if( pgno>pagerPa
187d0 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a gecount(pBt) ){.
187d1 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
187d2 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
187d3 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65 74 41 }.. rc = getA
187d4 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 ndInitPage(pBt,
187d5 70 67 6e 6f 2c 20 26 70 50 61 67 65 29 3b 0a 20 pgno, &pPage);.
187d6 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
187d7 20 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 rc;. for(i=0;
187d8 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 i<pPage->nCell;
187d9 69 2b 2b 29 7b 0a 20 20 20 20 70 43 65 6c 6c 20 i++){. pCell
187da 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 = findCell(pPage
187db 2c 20 69 29 3b 0a 20 20 20 20 69 66 28 20 21 70 , i);. if( !p
187dc 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
187dd 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61 rc = clearDa
187de 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c 20 tabasePage(pBt,
187df 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 2c get4byte(pCell),
187e0 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20 1, pnChange);.
187e1 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f if( rc ) go
187e2 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 65 to cleardatabase
187e3 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a page_out;. }.
187e4 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 43 65 rc = clearCe
187e5 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 ll(pPage, pCell)
187e6 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 ;. if( rc ) g
187e7 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 oto cleardatabas
187e8 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 epage_out;. }.
187e9 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
187ea 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 6c f ){. rc = cl
187eb 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28 earDatabasePage(
187ec 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 26 70 pBt, get4byte(&p
187ed 50 61 67 65 2d 3e 61 44 61 74 61 5b 38 5d 29 2c Page->aData[8]),
187ee 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20 1, pnChange);.
187ef 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f if( rc ) goto
187f0 20 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 cleardatabasepa
187f1 67 65 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 20 ge_out;. }else
187f2 69 66 28 20 70 6e 43 68 61 6e 67 65 20 29 7b 0a if( pnChange ){.
187f3 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
187f4 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20 e->intKey );.
187f5 20 2a 70 6e 43 68 61 6e 67 65 20 2b 3d 20 70 50 *pnChange += pP
187f6 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 7d 0a age->nCell;. }.
187f7 20 20 69 66 28 20 66 72 65 65 50 61 67 65 46 6c if( freePageFl
187f8 61 67 20 29 7b 0a 20 20 20 20 66 72 65 65 50 61 ag ){. freePa
187f9 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b 0a ge(pPage, &rc);.
187fa 20 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63 20 }else if( (rc
187fb 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
187fc 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ite(pPage->pDbPa
187fd 67 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 7a ge))==0 ){. z
187fe 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c 20 70 eroPage(pPage, p
187ff 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20 7c Page->aData[0] |
18800 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 7d 0a PTF_LEAF);. }.
18801 0a 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 .cleardatabasepa
18802 67 65 5f 6f 75 74 3a 0a 20 20 72 65 6c 65 61 73 ge_out:. releas
18803 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
18804 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
18805 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 69 .** Delete all i
18806 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 nformation from
18807 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 a single table i
18808 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 n the database.
18809 20 69 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68 iTable is.** th
1880a 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 e page number of
1880b 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 the root of the
1880c 20 74 61 62 6c 65 2e 20 20 41 66 74 65 72 20 74 table. After t
1880d 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
1880e 72 6e 73 2c 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 rns,.** the root
1880f 20 70 61 67 65 20 69 73 20 65 6d 70 74 79 2c 20 page is empty,
18810 62 75 74 20 73 74 69 6c 6c 20 65 78 69 73 74 73 but still exists
18811 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
18812 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c 20 77 tine will fail w
18813 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 ith SQLITE_LOCKE
18814 44 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 D if there are a
18815 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72 65 61 64 20 ny open.** read
18816 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 74 cursors on the t
18817 61 62 6c 65 2e 20 20 4f 70 65 6e 20 77 72 69 74 able. Open writ
18818 65 20 63 75 72 73 6f 72 73 20 61 72 65 20 6d 6f e cursors are mo
18819 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 72 6f ved to the.** ro
1881a 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e ot of the table.
1881b 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e 43 68 61 6e .**.** If pnChan
1881c 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 ge is not NULL,
1881d 74 68 65 6e 20 74 61 62 6c 65 20 69 54 61 62 6c then table iTabl
1881e 65 20 6d 75 73 74 20 62 65 20 61 6e 20 69 6e 74 e must be an int
1881f 6b 65 79 20 74 61 62 6c 65 2e 20 54 68 65 0a 2a key table. The.*
18820 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 * integer value
18821 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 6e pointed to by pn
18822 43 68 61 6e 67 65 20 69 73 20 69 6e 63 72 65 6d Change is increm
18823 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d ented by the num
18824 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74 72 69 65 ber of.** entrie
18825 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a s in the table..
18826 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18827 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
18828 65 65 43 6c 65 61 72 54 61 62 6c 65 28 42 74 72 eeClearTable(Btr
18829 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c ee *p, int iTabl
1882a 65 2c 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67 65 e, int *pnChange
1882b 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 ){. int rc;. B
1882c 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
1882d 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 ->pBt;. sqlite3
1882e 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
1882f 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 assert( p->inTr
18830 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
18831 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 61 6c 69 );.. /* Invali
18832 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f date all incrblo
18833 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f b cursors open o
18834 6e 20 74 61 62 6c 65 20 69 54 61 62 6c 65 20 28 n table iTable (
18835 61 73 73 75 6d 69 6e 67 20 69 54 61 62 6c 65 0a assuming iTable.
18836 20 20 2a 2a 20 69 73 20 74 68 65 20 72 6f 6f 74 ** is the root
18837 20 6f 66 20 61 20 74 61 62 6c 65 20 62 2d 74 72 of a table b-tr
18838 65 65 20 2d 20 69 66 20 69 74 20 69 73 20 6e 6f ee - if it is no
18839 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 t, the following
1883a 20 63 61 6c 6c 20 69 73 0a 20 20 2a 2a 20 61 20 call is. ** a
1883b 6e 6f 2d 6f 70 29 2e 20 20 2a 2f 0a 20 20 69 6e no-op). */. in
1883c 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 validateIncrblob
1883d 43 75 72 73 6f 72 73 28 70 2c 20 30 2c 20 31 29 Cursors(p, 0, 1)
1883e 3b 0a 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c ;.. rc = saveAl
1883f 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 28 50 lCursors(pBt, (P
18840 67 6e 6f 29 69 54 61 62 6c 65 2c 20 30 29 3b 0a gno)iTable, 0);.
18841 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d if( SQLITE_OK=
18842 3d 72 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 =rc ){. rc =
18843 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67 clearDatabasePag
18844 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 e(pBt, (Pgno)iTa
18845 62 6c 65 2c 20 30 2c 20 70 6e 43 68 61 6e 67 65 ble, 0, pnChange
18846 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
18847 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
18848 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
18849 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 69 *.** Erase all i
1884a 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20 nformation in a
1884b 74 61 62 6c 65 20 61 6e 64 20 61 64 64 20 74 68 table and add th
1884c 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 e root of the ta
1884d 62 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 66 72 ble to.** the fr
1884e 65 65 6c 69 73 74 2e 20 20 45 78 63 65 70 74 2c eelist. Except,
1884f 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 the root of the
18850 20 70 72 69 6e 63 69 70 6c 65 20 74 61 62 6c 65 principle table
18851 20 28 74 68 65 20 6f 6e 65 20 6f 6e 0a 2a 2a 20 (the one on.**
18852 70 61 67 65 20 31 29 20 69 73 20 6e 65 76 65 72 page 1) is never
18853 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 added to the fr
18854 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 eelist..**.** Th
18855 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
18856 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45 fail with SQLITE
18857 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65 _LOCKED if there
18858 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a are any open.**
18859 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 cursors on the
1885a 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 table..**.** If
1885b 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e AUTOVACUUM is en
1885c 61 62 6c 65 64 20 61 6e 64 20 74 68 65 20 70 61 abled and the pa
1885d 67 65 20 61 74 20 69 54 61 62 6c 65 20 69 73 20 ge at iTable is
1885e 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 not the last.**
1885f 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 root page in the
18860 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
18861 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 72 6f then the last ro
18862 6f 74 20 70 61 67 65 20 0a 2a 2a 20 69 6e 20 74 ot page .** in t
18863 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
18864 20 69 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 is moved into t
18865 68 65 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 he slot formerly
18866 20 6f 63 63 75 70 69 65 64 20 62 79 0a 2a 2a 20 occupied by.**
18867 69 54 61 62 6c 65 20 61 6e 64 20 74 68 61 74 20 iTable and that
18868 6c 61 73 74 20 73 6c 6f 74 20 66 6f 72 6d 65 72 last slot former
18869 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79 20 74 ly occupied by t
1886a 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 he last root pag
1886b 65 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f e.** is added to
1886c 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e the freelist in
1886d 73 74 65 61 64 20 6f 66 20 69 54 61 62 6c 65 2e stead of iTable.
1886e 20 20 49 6e 20 74 68 69 73 20 73 61 79 2c 20 61 In this say, a
1886f 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 ll.** root pages
18870 20 61 72 65 20 6b 65 70 74 20 61 74 20 74 68 65 are kept at the
18871 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
18872 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
18873 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 6e 65 63 which.** is nec
18874 65 73 73 61 72 79 20 66 6f 72 20 41 55 54 4f 56 essary for AUTOV
18875 41 43 55 55 4d 20 74 6f 20 77 6f 72 6b 20 72 69 ACUUM to work ri
18876 67 68 74 2e 20 20 2a 70 69 4d 6f 76 65 64 20 69 ght. *piMoved i
18877 73 20 73 65 74 20 74 6f 20 74 68 65 20 0a 2a 2a s set to the .**
18878 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 68 61 page number tha
18879 74 20 75 73 65 64 20 74 6f 20 62 65 20 74 68 65 t used to be the
1887a 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 last root page
1887b 69 6e 20 74 68 65 20 66 69 6c 65 20 62 65 66 6f in the file befo
1887c 72 65 0a 2a 2a 20 74 68 65 20 6d 6f 76 65 2e 20 re.** the move.
1887d 20 49 66 20 6e 6f 20 70 61 67 65 20 67 65 74 73 If no page gets
1887e 20 6d 6f 76 65 64 2c 20 2a 70 69 4d 6f 76 65 64 moved, *piMoved
1887f 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a is set to 0..**
18880 20 54 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 The last root p
18881 61 67 65 20 69 73 20 72 65 63 6f 72 64 65 64 20 age is recorded
18882 69 6e 20 6d 65 74 61 5b 33 5d 20 61 6e 64 20 74 in meta[3] and t
18883 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 6d he value of.** m
18884 65 74 61 5b 33 5d 20 69 73 20 75 70 64 61 74 65 eta[3] is update
18885 64 20 62 79 20 74 68 69 73 20 70 72 6f 63 65 64 d by this proced
18886 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ure..*/.static i
18887 6e 74 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c nt btreeDropTabl
18888 65 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f e(Btree *p, Pgno
18889 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69 iTable, int *pi
1888a 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 Moved){. int rc
1888b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
1888c 67 65 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 ge = 0;. BtShar
1888d 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
1888e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
1888f 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
18890 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 tex(p) );. asse
18891 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d rt( p->inTrans==
18892 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 0a TRANS_WRITE );..
18893 20 20 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 /* It is illeg
18894 61 6c 20 74 6f 20 64 72 6f 70 20 61 20 74 61 62 al to drop a tab
18895 6c 65 20 69 66 20 61 6e 79 20 63 75 72 73 6f 72 le if any cursor
18896 73 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68 s are open on th
18897 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e e. ** database.
18898 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 This is because
18899 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 in auto-vacuum
1889a 6d 6f 64 65 20 74 68 65 20 62 61 63 6b 65 6e 64 mode the backend
1889b 20 6d 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74 may. ** need t
1889c 6f 20 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20 72 o move another r
1889d 6f 6f 74 2d 70 61 67 65 20 74 6f 20 66 69 6c 6c oot-page to fill
1889e 20 61 20 67 61 70 20 6c 65 66 74 20 62 79 20 74 a gap left by t
1889f 68 65 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20 he deleted. **
188a0 72 6f 6f 74 20 70 61 67 65 2e 20 49 66 20 61 6e root page. If an
188a1 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 77 61 73 open cursor was
188a2 20 75 73 69 6e 67 20 74 68 69 73 20 70 61 67 65 using this page
188a3 20 61 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c 64 a problem would
188a4 20 0a 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20 20 . ** occur..
188a5 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 65 72 72 **. ** This err
188a6 6f 72 20 69 73 20 63 61 75 67 68 74 20 6c 6f 6e or is caught lon
188a7 67 20 62 65 66 6f 72 65 20 63 6f 6e 74 72 6f 6c g before control
188a8 20 72 65 61 63 68 65 73 20 74 68 69 73 20 70 6f reaches this po
188a9 69 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 int.. */. if(
188aa 4e 45 56 45 52 28 70 42 74 2d 3e 70 43 75 72 73 NEVER(pBt->pCurs
188ab 6f 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 or) ){. sqlit
188ac 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 e3ConnectionBloc
188ad 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e ked(p->db, pBt->
188ae 70 43 75 72 73 6f 72 2d 3e 70 42 74 72 65 65 2d pCursor->pBtree-
188af 3e 64 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e >db);. return
188b0 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 SQLITE_LOCKED_S
188b1 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 7d 0a HAREDCACHE;. }.
188b2 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 . rc = btreeGet
188b3 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 Page(pBt, (Pgno)
188b4 69 54 61 62 6c 65 2c 20 26 70 50 61 67 65 2c 20 iTable, &pPage,
188b5 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 0);. if( rc ) r
188b6 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d eturn rc;. rc =
188b7 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 sqlite3BtreeCle
188b8 61 72 54 61 62 6c 65 28 70 2c 20 69 54 61 62 6c arTable(p, iTabl
188b9 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 e, 0);. if( rc
188ba 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 ){. releasePa
188bb 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 ge(pPage);. r
188bc 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 eturn rc;. }..
188bd 20 2a 70 69 4d 6f 76 65 64 20 3d 20 30 3b 0a 0a *piMoved = 0;..
188be 20 20 69 66 28 20 69 54 61 62 6c 65 3e 31 20 29 if( iTable>1 )
188bf 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f {.#ifdef SQLITE_
188c0 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
188c1 20 20 20 20 66 72 65 65 50 61 67 65 28 70 50 61 freePage(pPa
188c2 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 72 65 ge, &rc);. re
188c3 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
188c4 3b 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 28 20 ;.#else. if(
188c5 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
188c6 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6d 61 ){. Pgno ma
188c7 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 xRootPgno;.
188c8 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
188c9 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f 4c 41 Meta(p, BTREE_LA
188ca 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 2c RGEST_ROOT_PAGE,
188cb 20 26 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a &maxRootPgno);.
188cc 0a 20 20 20 20 20 20 69 66 28 20 69 54 61 62 6c . if( iTabl
188cd 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 6e 6f 20 29 e==maxRootPgno )
188ce 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 {. /* If
188cf 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 the table being
188d0 64 72 6f 70 70 65 64 20 69 73 20 74 68 65 20 74 dropped is the t
188d1 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 6c 61 able with the la
188d2 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a rgest root-page.
188d3 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 ** numbe
188d4 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 r in the databas
188d5 65 2c 20 70 75 74 20 74 68 65 20 72 6f 6f 74 20 e, put the root
188d6 70 61 67 65 20 6f 6e 20 74 68 65 20 66 72 65 65 page on the free
188d7 20 6c 69 73 74 2e 20 0a 20 20 20 20 20 20 20 20 list. .
188d8 2a 2f 0a 20 20 20 20 20 20 20 20 66 72 65 65 50 */. freeP
188d9 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b age(pPage, &rc);
188da 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
188db 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
188dc 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
188dd 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
188de 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
188df 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
188e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a else{. /*
188e1 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 The table being
188e2 20 64 72 6f 70 70 65 64 20 64 6f 65 73 20 6e 6f dropped does no
188e3 74 20 68 61 76 65 20 74 68 65 20 6c 61 72 67 65 t have the large
188e4 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 st root-page.
188e5 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 ** number i
188e6 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 n the database.
188e7 53 6f 20 6d 6f 76 65 20 74 68 65 20 70 61 67 65 So move the page
188e8 20 74 68 61 74 20 64 6f 65 73 20 69 6e 74 6f 20 that does into
188e9 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 the . **
188ea 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 65 20 gap left by the
188eb 64 65 6c 65 74 65 64 20 72 6f 6f 74 2d 70 61 67 deleted root-pag
188ec 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 e.. */.
188ed 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 MemPage *p
188ee 4d 6f 76 65 3b 0a 20 20 20 20 20 20 20 20 72 65 Move;. re
188ef 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
188f0 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 ;. rc = b
188f1 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
188f2 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 maxRootPgno, &p
188f3 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 Move, 0);.
188f4 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
188f5 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
188f6 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
188f7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 }. rc
188f8 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 = relocatePage(
188f9 70 42 74 2c 20 70 4d 6f 76 65 2c 20 50 54 52 4d pBt, pMove, PTRM
188fa 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c 20 AP_ROOTPAGE, 0,
188fb 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 20 20 iTable, 0);.
188fc 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
188fd 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 20 pMove);.
188fe 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
188ff 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
18900 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
18901 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d 6f 76 }. pMov
18902 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72 e = 0;. r
18903 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
18904 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e (pBt, maxRootPgn
18905 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 o, &pMove, 0);.
18906 20 20 20 20 20 20 20 66 72 65 65 50 61 67 65 28 freePage(
18907 70 4d 6f 76 65 2c 20 26 72 63 29 3b 0a 20 20 20 pMove, &rc);.
18908 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
18909 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 (pMove);.
1890a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1890b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
1890c 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
1890d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 69 }. *pi
1890e 4d 6f 76 65 64 20 3d 20 6d 61 78 52 6f 6f 74 50 Moved = maxRootP
1890f 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 gno;. }..
18910 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6e /* Set the n
18911 65 77 20 27 6d 61 78 2d 72 6f 6f 74 2d 70 61 67 ew 'max-root-pag
18912 65 27 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 e' value in the
18913 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 2e database header.
18914 20 54 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 This. ** i
18915 73 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 20 s the old value
18916 6c 65 73 73 20 6f 6e 65 2c 20 6c 65 73 73 20 6f less one, less o
18917 6e 65 20 6d 6f 72 65 20 69 66 20 74 68 61 74 20 ne more if that
18918 68 61 70 70 65 6e 73 20 74 6f 0a 20 20 20 20 20 happens to.
18919 20 2a 2a 20 62 65 20 61 20 72 6f 6f 74 2d 70 61 ** be a root-pa
1891a 67 65 20 6e 75 6d 62 65 72 2c 20 6c 65 73 73 20 ge number, less
1891b 6f 6e 65 20 61 67 61 69 6e 20 69 66 20 74 68 61 one again if tha
1891c 74 20 69 73 20 74 68 65 0a 20 20 20 20 20 20 2a t is the. *
1891d 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 * PENDING_BYTE_P
1891e 41 47 45 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 AGE.. */.
1891f 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d maxRootPgno-
18920 2d 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 -;. while(
18921 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 45 4e maxRootPgno==PEN
18922 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
18923 42 74 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 Bt).
18924 20 7c 7c 20 50 54 52 4d 41 50 5f 49 53 50 41 47 || PTRMAP_ISPAG
18925 45 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 E(pBt, maxRootPg
18926 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d no) ){. m
18927 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 axRootPgno--;.
18928 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 }. asse
18929 72 74 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 21 rt( maxRootPgno!
1892a 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
1892b 47 45 28 70 42 74 29 20 29 3b 0a 0a 20 20 20 20 GE(pBt) );..
1892c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1892d 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 2c reeUpdateMeta(p,
1892e 20 34 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 4, maxRootPgno)
1892f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
18930 20 20 20 66 72 65 65 50 61 67 65 28 70 50 61 67 freePage(pPag
18931 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 72 e, &rc);. r
18932 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 eleasePage(pPage
18933 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a );. }.#endif.
18934 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
18935 49 66 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 If sqlite3BtreeD
18936 72 6f 70 54 61 62 6c 65 20 77 61 73 20 63 61 6c ropTable was cal
18937 6c 65 64 20 6f 6e 20 70 61 67 65 20 31 2e 0a 20 led on page 1..
18938 20 20 20 2a 2a 20 54 68 69 73 20 72 65 61 6c 6c ** This reall
18939 79 20 6e 65 76 65 72 20 73 68 6f 75 6c 64 20 68 y never should h
1893a 61 70 70 65 6e 20 65 78 63 65 70 74 20 69 6e 20 appen except in
1893b 61 20 63 6f 72 72 75 70 74 0a 20 20 20 20 2a 2a a corrupt. **
1893c 20 64 61 74 61 62 61 73 65 2e 20 0a 20 20 20 20 database. .
1893d 2a 2f 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 */. zeroPage(
1893e 70 50 61 67 65 2c 20 50 54 46 5f 49 4e 54 4b 45 pPage, PTF_INTKE
1893f 59 7c 50 54 46 5f 4c 45 41 46 20 29 3b 0a 20 20 Y|PTF_LEAF );.
18940 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
18941 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 age);. }. retu
18942 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 51 4c 49 54 rn rc; .}.SQLIT
18943 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
18944 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 lite3BtreeDropTa
18945 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e ble(Btree *p, in
18946 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 t iTable, int *p
18947 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 iMoved){. int r
18948 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 c;. sqlite3Btre
18949 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 eEnter(p);. rc
1894a 3d 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c 65 = btreeDropTable
1894b 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 69 4d 6f (p, iTable, piMo
1894c 76 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 ved);. sqlite3B
1894d 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
1894e 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
1894f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
18950 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63 on may only be c
18951 61 6c 6c 65 64 20 69 66 20 74 68 65 20 62 2d 74 alled if the b-t
18952 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 ree connection a
18953 6c 72 65 61 64 79 0a 2a 2a 20 68 61 73 20 61 20 lready.** has a
18954 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 read or write tr
18955 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20 6f ansaction open o
18956 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a n the database..
18957 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 6d **.** Read the m
18958 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 eta-information
18959 6f 75 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 out of a databas
1895a 65 20 66 69 6c 65 2e 20 20 4d 65 74 61 5b 30 5d e file. Meta[0]
1895b 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 .** is the numbe
1895c 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 r of free pages
1895d 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 currently in the
1895e 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61 database. Meta
1895f 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75 67 68 20 6d [1].** through m
18960 65 74 61 5b 31 35 5d 20 61 72 65 20 61 76 61 69 eta[15] are avai
18961 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 62 79 lable for use by
18962 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 2e 20 higher layers.
18963 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 72 Meta[0].** is r
18964 65 61 64 2d 6f 6e 6c 79 2c 20 74 68 65 20 6f 74 ead-only, the ot
18965 68 65 72 73 20 61 72 65 20 72 65 61 64 2f 77 72 hers are read/wr
18966 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 ite..** .** The
18967 73 63 68 65 6d 61 20 6c 61 79 65 72 20 6e 75 6d schema layer num
18968 62 65 72 73 20 6d 65 74 61 20 76 61 6c 75 65 73 bers meta values
18969 20 64 69 66 66 65 72 65 6e 74 6c 79 2e 20 20 41 differently. A
1896a 74 20 74 68 65 20 73 63 68 65 6d 61 0a 2a 2a 20 t the schema.**
1896b 6c 61 79 65 72 20 28 61 6e 64 20 74 68 65 20 53 layer (and the S
1896c 65 74 43 6f 6f 6b 69 65 20 61 6e 64 20 52 65 61 etCookie and Rea
1896d 64 43 6f 6f 6b 69 65 20 6f 70 63 6f 64 65 73 29 dCookie opcodes)
1896e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
1896f 2a 20 66 72 65 65 20 70 61 67 65 73 20 69 73 20 * free pages is
18970 6e 6f 74 20 76 69 73 69 62 6c 65 2e 20 20 53 6f not visible. So
18971 20 43 6f 6f 6b 69 65 5b 30 5d 20 69 73 20 74 68 Cookie[0] is th
18972 65 20 73 61 6d 65 20 61 73 20 4d 65 74 61 5b 31 e same as Meta[1
18973 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 ]..*/.SQLITE_PRI
18974 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
18975 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 42 74 3BtreeGetMeta(Bt
18976 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c ree *p, int idx,
18977 20 75 33 32 20 2a 70 4d 65 74 61 29 7b 0a 20 20 u32 *pMeta){.
18978 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
18979 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 p->pBt;.. sqlit
1897a 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
1897b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e . assert( p->in
1897c 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 Trans>TRANS_NONE
1897d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 53 51 );. assert( SQ
1897e 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 68 LITE_OK==querySh
1897f 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
18980 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f ck(p, MASTER_ROO
18981 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 20 29 3b T, READ_LOCK) );
18982 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e . assert( pBt->
18983 70 50 61 67 65 31 20 29 3b 0a 20 20 61 73 73 65 pPage1 );. asse
18984 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64 rt( idx>=0 && id
18985 78 3c 3d 31 35 20 29 3b 0a 0a 20 20 2a 70 4d 65 x<=15 );.. *pMe
18986 74 61 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 ta = get4byte(&p
18987 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
18988 61 5b 33 36 20 2b 20 69 64 78 2a 34 5d 29 3b 0a a[36 + idx*4]);.
18989 0a 20 20 2f 2a 20 49 66 20 61 75 74 6f 2d 76 61 . /* If auto-va
1898a 63 75 75 6d 20 69 73 20 64 69 73 61 62 6c 65 64 cuum is disabled
1898b 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 20 61 in this build a
1898c 6e 64 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 nd this is an au
1898d 74 6f 2d 76 61 63 75 75 6d 0a 20 20 2a 2a 20 64 to-vacuum. ** d
1898e 61 74 61 62 61 73 65 2c 20 6d 61 72 6b 20 74 68 atabase, mark th
1898f 65 20 64 61 74 61 62 61 73 65 20 61 73 20 72 65 e database as re
18990 61 64 2d 6f 6e 6c 79 2e 20 20 2a 2f 0a 23 69 66 ad-only. */.#if
18991 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
18992 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28 AUTOVACUUM. if(
18993 20 69 64 78 3d 3d 42 54 52 45 45 5f 4c 41 52 47 idx==BTREE_LARG
18994 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20 26 26 EST_ROOT_PAGE &&
18995 20 2a 70 4d 65 74 61 3e 30 20 29 20 70 42 74 2d *pMeta>0 ) pBt-
18996 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23 >readOnly = 1;.#
18997 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 endif.. sqlite3
18998 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 7d BtreeLeave(p);.}
18999 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 65 ../*.** Write me
1899a 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 ta-information b
1899b 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74 ack into the dat
1899c 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 30 5d 20 abase. Meta[0]
1899d 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 20 is.** read-only
1899e 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20 77 and may not be w
1899f 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 ritten..*/.SQLIT
189a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
189a1 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 lite3BtreeUpdate
189a2 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20 69 Meta(Btree *p, i
189a3 6e 74 20 69 64 78 2c 20 75 33 32 20 69 4d 65 74 nt idx, u32 iMet
189a4 61 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a a){. BtShared *
189a5 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
189a6 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
189a7 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 P1;. int rc;.
189a8 61 73 73 65 72 74 28 20 69 64 78 3e 3d 31 20 26 assert( idx>=1 &
189a9 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 73 & idx<=15 );. s
189aa 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
189ab 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 (p);. assert( p
189ac 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
189ad 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 _WRITE );. asse
189ae 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 21 rt( pBt->pPage1!
189af 3d 30 20 29 3b 0a 20 20 70 50 31 20 3d 20 70 42 =0 );. pP1 = pB
189b0 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
189b1 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
189b2 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e PagerWrite(pBt->
189b3 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 pPage1->pDbPage)
189b4 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
189b5 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 75 74 TE_OK ){. put
189b6 34 62 79 74 65 28 26 70 50 31 5b 33 36 20 2b 20 4byte(&pP1[36 +
189b7 69 64 78 2a 34 5d 2c 20 69 4d 65 74 61 29 3b 0a idx*4], iMeta);.
189b8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
189b9 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
189ba 20 20 20 69 66 28 20 69 64 78 3d 3d 42 54 52 45 if( idx==BTRE
189bb 45 5f 49 4e 43 52 5f 56 41 43 55 55 4d 20 29 7b E_INCR_VACUUM ){
189bc 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
189bd 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 7c Bt->autoVacuum |
189be 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b 0a 20 20 | iMeta==0 );.
189bf 20 20 20 20 61 73 73 65 72 74 28 20 69 4d 65 74 assert( iMet
189c0 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 61 3d 3d 31 a==0 || iMeta==1
189c1 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 );. pBt->i
189c2 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 75 38 29 ncrVacuum = (u8)
189c3 69 4d 65 74 61 3b 0a 20 20 20 20 7d 0a 23 65 6e iMeta;. }.#en
189c4 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 dif. }. sqlite
189c5 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
189c6 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
189c7 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
189c8 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 2f MIT_BTREECOUNT./
189c9 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 *.** The first a
189ca 72 67 75 6d 65 6e 74 2c 20 70 43 75 72 2c 20 69 rgument, pCur, i
189cb 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 s a cursor opene
189cc 64 20 6f 6e 20 73 6f 6d 65 20 62 2d 74 72 65 65 d on some b-tree
189cd 2e 20 43 6f 75 6e 74 20 74 68 65 0a 2a 2a 20 6e . Count the.** n
189ce 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
189cf 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65 20 61 in the b-tree a
189d0 6e 64 20 77 72 69 74 65 20 74 68 65 20 72 65 73 nd write the res
189d1 75 6c 74 20 74 6f 20 2a 70 6e 45 6e 74 72 79 2e ult to *pnEntry.
189d2 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
189d3 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
189d4 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 the operation is
189d5 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 65 78 successfully ex
189d6 65 63 75 74 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 ecuted. .** Othe
189d7 72 77 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72 rwise, if an err
189d8 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 or is encountere
189d9 64 20 28 69 2e 65 2e 20 61 6e 20 49 4f 20 65 72 d (i.e. an IO er
189da 72 6f 72 20 6f 72 20 64 61 74 61 62 61 73 65 0a ror or database.
189db 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 29 20 61 ** corruption) a
189dc 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 n SQLite error c
189dd 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
189de 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
189df 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
189e0 72 65 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f reeCount(BtCurso
189e1 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 6e r *pCur, i64 *pn
189e2 45 6e 74 72 79 29 7b 0a 20 20 69 36 34 20 6e 45 Entry){. i64 nE
189e3 6e 74 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20 ntry = 0;
189e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
189e5 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 * Value to retur
189e6 6e 20 69 6e 20 2a 70 6e 45 6e 74 72 79 20 2a 2f n in *pnEntry */
189e7 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
189e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
189e9 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
189ea 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 72 63 20 3d n code */. rc =
189eb 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 moveToRoot(pCur
189ec 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20 );.. /* Unless
189ed 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c an error occurs,
189ee 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c the following l
189ef 6f 6f 70 20 72 75 6e 73 20 6f 6e 65 20 69 74 65 oop runs one ite
189f0 72 61 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 0a ration for each.
189f1 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 ** page in the
189f2 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72 B-Tree structur
189f3 65 20 28 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 e (not including
189f4 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 29 overflow pages)
189f5 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 . . */. while(
189f6 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
189f7 7b 0a 20 20 20 20 69 6e 74 20 69 49 64 78 3b 20 {. int iIdx;
189f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
189f9 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
189fa 78 20 6f 66 20 63 68 69 6c 64 20 6e 6f 64 65 20 x of child node
189fb 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20 20 in parent */.
189fc 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
189fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
189fe 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 70 /* Current p
189ff 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65 age of the b-tre
18a00 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 e */.. /* If
18a01 74 68 69 73 20 69 73 20 61 20 6c 65 61 66 20 70 this is a leaf p
18a02 61 67 65 20 6f 72 20 74 68 65 20 74 72 65 65 20 age or the tree
18a03 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 2d 6b 65 is not an int-ke
18a04 79 20 74 72 65 65 2c 20 74 68 65 6e 20 0a 20 20 y tree, then .
18a05 20 20 2a 2a 20 74 68 69 73 20 70 61 67 65 20 63 ** this page c
18a06 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 61 62 6c ontains countabl
18a07 65 20 65 6e 74 72 69 65 73 2e 20 49 6e 63 72 65 e entries. Incre
18a08 6d 65 6e 74 20 74 68 65 20 65 6e 74 72 79 20 63 ment the entry c
18a09 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a 20 61 63 ounter. ** ac
18a0a 63 6f 72 64 69 6e 67 6c 79 2e 0a 20 20 20 20 2a cordingly.. *
18a0b 2f 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 /. pPage = pC
18a0c 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
18a0d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 >iPage];. if(
18a0e 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c 20 pPage->leaf ||
18a0f 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 !pPage->intKey )
18a10 7b 0a 20 20 20 20 20 20 6e 45 6e 74 72 79 20 2b {. nEntry +
18a11 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a = pPage->nCell;.
18a12 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70 50 }.. /* pP
18a13 61 67 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f age is a leaf no
18a14 64 65 2e 20 54 68 69 73 20 6c 6f 6f 70 20 6e 61 de. This loop na
18a15 76 69 67 61 74 65 73 20 74 68 65 20 63 75 72 73 vigates the curs
18a16 6f 72 20 73 6f 20 74 68 61 74 20 69 74 20 0a 20 or so that it .
18a17 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 ** points to
18a18 74 68 65 20 66 69 72 73 74 20 69 6e 74 65 72 69 the first interi
18a19 6f 72 20 63 65 6c 6c 20 74 68 61 74 20 69 74 20 or cell that it
18a1a 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70 61 points to the pa
18a1b 72 65 6e 74 20 6f 66 0a 20 20 20 20 2a 2a 20 74 rent of. ** t
18a1c 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 he next page in
18a1d 74 68 65 20 74 72 65 65 20 74 68 61 74 20 68 61 the tree that ha
18a1e 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 76 s not yet been v
18a1f 69 73 69 74 65 64 2e 20 54 68 65 0a 20 20 20 20 isited. The.
18a20 2a 2a 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 ** pCur->aiIdx[p
18a21 43 75 72 2d 3e 69 50 61 67 65 5d 20 76 61 6c 75 Cur->iPage] valu
18a22 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 e is set to the
18a23 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61 72 index of the par
18a24 65 6e 74 20 63 65 6c 6c 0a 20 20 20 20 2a 2a 20 ent cell. **
18a25 6f 66 20 74 68 65 20 70 61 67 65 2c 20 6f 72 20 of the page, or
18a26 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 to the number of
18a27 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 70 61 cells in the pa
18a28 67 65 20 69 66 20 74 68 65 20 6e 65 78 74 20 70 ge if the next p
18a29 61 67 65 0a 20 20 20 20 2a 2a 20 74 6f 20 76 69 age. ** to vi
18a2a 73 69 74 20 69 73 20 74 68 65 20 72 69 67 68 74 sit is the right
18a2b 2d 63 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61 -child of its pa
18a2c 72 65 6e 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 rent.. **.
18a2d 20 2a 2a 20 49 66 20 61 6c 6c 20 70 61 67 65 73 ** If all pages
18a2e 20 69 6e 20 74 68 65 20 74 72 65 65 20 68 61 76 in the tree hav
18a2f 65 20 62 65 65 6e 20 76 69 73 69 74 65 64 2c 20 e been visited,
18a30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
18a31 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 to the. ** c
18a32 61 6c 6c 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 aller.. */.
18a33 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 if( pPage->lea
18a34 66 20 29 7b 0a 20 20 20 20 20 20 64 6f 20 7b 0a f ){. do {.
18a35 20 20 20 20 20 20 20 20 69 66 28 20 70 43 75 72 if( pCur
18a36 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 ->iPage==0 ){.
18a37 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 70 /* All p
18a38 61 67 65 73 20 6f 66 20 74 68 65 20 62 2d 74 72 ages of the b-tr
18a39 65 65 20 68 61 76 65 20 62 65 65 6e 20 76 69 73 ee have been vis
18a3a 69 74 65 64 2e 20 52 65 74 75 72 6e 20 73 75 63 ited. Return suc
18a3b 63 65 73 73 66 75 6c 6c 79 2e 20 2a 2f 0a 20 20 cessfully. */.
18a3c 20 20 20 20 20 20 20 20 2a 70 6e 45 6e 74 72 79 *pnEntry
18a3d 20 3d 20 6e 45 6e 74 72 79 3b 0a 20 20 20 20 20 = nEntry;.
18a3e 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
18a3f 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d TE_OK;. }
18a40 0a 20 20 20 20 20 20 20 20 6d 6f 76 65 54 6f 50 . moveToP
18a41 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 arent(pCur);.
18a42 20 20 20 7d 77 68 69 6c 65 20 28 20 70 43 75 72 }while ( pCur
18a43 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
18a44 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70 50 61 age]>=pCur->apPa
18a45 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d ge[pCur->iPage]-
18a46 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20 20 >nCell );..
18a47 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
18a48 72 2d 3e 69 50 61 67 65 5d 2b 2b 3b 0a 20 20 20 r->iPage]++;.
18a49 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d pPage = pCur-
18a4a 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
18a4b 61 67 65 5d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 age];. }..
18a4c 20 2f 2a 20 44 65 73 63 65 6e 64 20 74 6f 20 74 /* Descend to t
18a4d 68 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 6f 66 he child node of
18a4e 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 74 the cell that t
18a4f 68 65 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e he cursor curren
18a50 74 6c 79 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e tly . ** poin
18a51 74 73 20 61 74 2e 20 54 68 69 73 20 69 73 20 74 ts at. This is t
18a52 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 69 he right-child i
18a53 66 20 28 69 49 64 78 3d 3d 70 50 61 67 65 2d 3e f (iIdx==pPage->
18a54 6e 43 65 6c 6c 29 2e 0a 20 20 20 20 2a 2f 0a 20 nCell).. */.
18a55 20 20 20 69 49 64 78 20 3d 20 70 43 75 72 2d 3e iIdx = pCur->
18a56 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
18a57 65 5d 3b 0a 20 20 20 20 69 66 28 20 69 49 64 78 e];. if( iIdx
18a58 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 ==pPage->nCell )
18a59 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 {. rc = mov
18a5a 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 eToChild(pCur, g
18a5b 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e et4byte(&pPage->
18a5c 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
18a5d 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20 20 Offset+8]));.
18a5e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
18a5f 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 = moveToChild(p
18a60 43 75 72 2c 20 67 65 74 34 62 79 74 65 28 66 69 Cur, get4byte(fi
18a61 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 49 ndCell(pPage, iI
18a62 64 78 29 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d dx)));. }. }
18a63 0a 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 .. /* An error
18a64 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 52 65 has occurred. Re
18a65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f turn an error co
18a66 64 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 de. */. return
18a67 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a rc;.}.#endif../*
18a68 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 .** Return the p
18a69 61 67 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 ager associated
18a6a 77 69 74 68 20 61 20 42 54 72 65 65 2e 20 20 54 with a BTree. T
18a6b 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
18a6c 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 sed for.** testi
18a6d 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 ng and debugging
18a6e 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 only..*/.SQLITE
18a6f 5f 50 52 49 56 41 54 45 20 50 61 67 65 72 20 2a _PRIVATE Pager *
18a70 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 sqlite3BtreePage
18a71 72 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 r(Btree *p){. r
18a72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 50 eturn p->pBt->pP
18a73 61 67 65 72 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 ager;.}..#ifndef
18a74 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 SQLITE_OMIT_INT
18a75 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a EGRITY_CHECK./*.
18a76 2a 2a 20 41 70 70 65 6e 64 20 61 20 6d 65 73 73 ** Append a mess
18a77 61 67 65 20 74 6f 20 74 68 65 20 65 72 72 6f 72 age to the error
18a78 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 2e message string.
18a79 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
18a7a 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 0a checkAppendMsg(.
18a7b 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 IntegrityCk *p
18a7c 43 68 65 63 6b 2c 0a 20 20 63 68 61 72 20 2a 7a Check,. char *z
18a7d 4d 73 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 Msg1,. const ch
18a7e 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20 2e ar *zFormat,. .
18a7f 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
18a80 61 70 3b 0a 20 20 69 66 28 20 21 70 43 68 65 63 ap;. if( !pChec
18a81 6b 2d 3e 6d 78 45 72 72 20 29 20 72 65 74 75 72 k->mxErr ) retur
18a82 6e 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6d 78 45 n;. pCheck->mxE
18a83 72 72 2d 2d 3b 0a 20 20 70 43 68 65 63 6b 2d 3e rr--;. pCheck->
18a84 6e 45 72 72 2b 2b 3b 0a 20 20 76 61 5f 73 74 61 nErr++;. va_sta
18a85 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b rt(ap, zFormat);
18a86 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 65 . if( pCheck->e
18a87 72 72 4d 73 67 2e 6e 43 68 61 72 20 29 7b 0a 20 rrMsg.nChar ){.
18a88 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
18a89 75 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63 6b umAppend(&pCheck
18a8a 2d 3e 65 72 72 4d 73 67 2c 20 22 5c 6e 22 2c 20 ->errMsg, "\n",
18a8b 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4d 1);. }. if( zM
18a8c 73 67 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 sg1 ){. sqlit
18a8d 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 e3StrAccumAppend
18a8e 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 (&pCheck->errMsg
18a8f 2c 20 7a 4d 73 67 31 2c 20 2d 31 29 3b 0a 20 20 , zMsg1, -1);.
18a90 7d 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 }. sqlite3VXPri
18a91 6e 74 66 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 ntf(&pCheck->err
18a92 4d 73 67 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 2c Msg, 1, zFormat,
18a93 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 ap);. va_end(a
18a94 70 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b p);. if( pCheck
18a95 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c 6c 6f 63 46 ->errMsg.mallocF
18a96 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 43 68 ailed ){. pCh
18a97 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 eck->mallocFaile
18a98 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e d = 1;. }.}.#en
18a99 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
18a9a 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 IT_INTEGRITY_CHE
18a9b 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 CK */..#ifndef S
18a9c 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 QLITE_OMIT_INTEG
18a9d 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a RITY_CHECK./*.**
18a9e 20 41 64 64 20 31 20 74 6f 20 74 68 65 20 72 65 Add 1 to the re
18a9f 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f ference count fo
18aa0 72 20 70 61 67 65 20 69 50 61 67 65 2e 20 20 49 r page iPage. I
18aa1 66 20 74 68 69 73 20 69 73 20 74 68 65 20 73 65 f this is the se
18aa2 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e 63 cond.** referenc
18aa3 65 20 74 6f 20 74 68 65 20 70 61 67 65 2c 20 61 e to the page, a
18aa4 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 dd an error mess
18aa5 61 67 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e 7a age to pCheck->z
18aa6 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75 72 ErrMsg..** Retur
18aa7 6e 20 31 20 69 66 20 74 68 65 72 65 20 61 72 65 n 1 if there are
18aa8 20 32 20 6f 72 65 20 6d 6f 72 65 20 72 65 66 65 2 ore more refe
18aa9 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 rences to the pa
18aaa 67 65 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20 69 ge and 0 if.** i
18aab 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 f this is the fi
18aac 72 73 74 20 72 65 66 65 72 65 6e 63 65 20 74 6f rst reference to
18aad 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a the page..**.**
18aae 20 41 6c 73 6f 20 63 68 65 63 6b 20 74 68 61 74 Also check that
18aaf 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
18ab0 20 69 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a 2a is in bounds..*
18ab1 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 /.static int che
18ab2 63 6b 52 65 66 28 49 6e 74 65 67 72 69 74 79 43 ckRef(IntegrityC
18ab3 6b 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f 20 k *pCheck, Pgno
18ab4 69 50 61 67 65 2c 20 63 68 61 72 20 2a 7a 43 6f iPage, char *zCo
18ab5 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 69 50 ntext){. if( iP
18ab6 61 67 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 age==0 ) return
18ab7 31 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3e 70 1;. if( iPage>p
18ab8 43 68 65 63 6b 2d 3e 6e 50 61 67 65 20 29 7b 0a Check->nPage ){.
18ab9 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
18aba 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 sg(pCheck, zCont
18abb 65 78 74 2c 20 22 69 6e 76 61 6c 69 64 20 70 61 ext, "invalid pa
18abc 67 65 20 6e 75 6d 62 65 72 20 25 64 22 2c 20 69 ge number %d", i
18abd 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 Page);. retur
18abe 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 n 1;. }. if( p
18abf 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 61 Check->anRef[iPa
18ac0 67 65 5d 3d 3d 31 20 29 7b 0a 20 20 20 20 63 68 ge]==1 ){. ch
18ac1 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 eckAppendMsg(pCh
18ac2 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 eck, zContext, "
18ac3 32 6e 64 20 72 65 66 65 72 65 6e 63 65 20 74 6f 2nd reference to
18ac4 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 page %d", iPage
18ac5 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b );. return 1;
18ac6 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 20 28 . }. return (
18ac7 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 pCheck->anRef[iP
18ac8 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d 0a 0a 23 69 age]++)>1;.}..#i
18ac9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
18aca 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a T_AUTOVACUUM./*.
18acb 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 ** Check that th
18acc 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 e entry in the p
18acd 6f 69 6e 74 65 72 2d 6d 61 70 20 66 6f 72 20 70 ointer-map for p
18ace 61 67 65 20 69 43 68 69 6c 64 20 6d 61 70 73 20 age iChild maps
18acf 74 6f 20 0a 2a 2a 20 70 61 67 65 20 69 50 61 72 to .** page iPar
18ad0 65 6e 74 2c 20 70 6f 69 6e 74 65 72 20 74 79 70 ent, pointer typ
18ad1 65 20 70 74 72 54 79 70 65 2e 20 49 66 20 6e 6f e ptrType. If no
18ad2 74 2c 20 61 70 70 65 6e 64 20 61 6e 20 65 72 72 t, append an err
18ad3 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74 6f or message.** to
18ad4 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61 74 pCheck..*/.stat
18ad5 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 74 72 ic void checkPtr
18ad6 6d 61 70 28 0a 20 20 49 6e 74 65 67 72 69 74 79 map(. Integrity
18ad7 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 20 2f 2a Ck *pCheck, /*
18ad8 20 49 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b Integrity check
18ad9 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 50 67 context */. Pg
18ada 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20 20 no iChild,
18adb 20 20 20 20 20 2f 2a 20 43 68 69 6c 64 20 70 61 /* Child pa
18adc 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 75 ge number */. u
18add 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20 8 eType,
18ade 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65 /* Expecte
18adf 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 74 79 d pointer map ty
18ae0 70 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 61 pe */. Pgno iPa
18ae1 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 2f rent, /
18ae2 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e 74 * Expected point
18ae3 65 72 20 6d 61 70 20 70 61 72 65 6e 74 20 70 61 er map parent pa
18ae4 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63 ge number */. c
18ae5 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20 20 har *zContext
18ae6 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 /* Context
18ae7 20 64 65 73 63 72 69 70 74 69 6f 6e 20 28 75 73 description (us
18ae8 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 73 67 ed for error msg
18ae9 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 ) */.){. int rc
18aea 3b 0a 20 20 75 38 20 65 50 74 72 6d 61 70 54 79 ;. u8 ePtrmapTy
18aeb 70 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72 6d pe;. Pgno iPtrm
18aec 61 70 50 61 72 65 6e 74 3b 0a 0a 20 20 72 63 20 apParent;.. rc
18aed 3d 20 70 74 72 6d 61 70 47 65 74 28 70 43 68 65 = ptrmapGet(pChe
18aee 63 6b 2d 3e 70 42 74 2c 20 69 43 68 69 6c 64 2c ck->pBt, iChild,
18aef 20 26 65 50 74 72 6d 61 70 54 79 70 65 2c 20 26 &ePtrmapType, &
18af0 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a iPtrmapParent);.
18af1 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
18af2 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 72 _OK ){. if( r
18af3 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
18af4 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f || rc==SQLITE_IO
18af5 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 65 ERR_NOMEM ) pChe
18af6 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 ck->mallocFailed
18af7 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b 41 = 1;. checkA
18af8 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c ppendMsg(pCheck,
18af9 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69 6c zContext, "Fail
18afa 65 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d 61 ed to read ptrma
18afb 70 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69 6c p key=%d", iChil
18afc 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a d);. return;.
18afd 20 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72 6d }.. if( ePtrm
18afe 61 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c 7c apType!=eType ||
18aff 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21 3d iPtrmapParent!=
18b00 69 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 63 iParent ){. c
18b01 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
18b02 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 heck, zContext,
18b03 0a 20 20 20 20 20 20 22 42 61 64 20 70 74 72 20 . "Bad ptr
18b04 6d 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25 64 map entry key=%d
18b05 20 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25 64 expected=(%d,%d
18b06 29 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c 20 ) got=(%d,%d)",
18b07 0a 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20 65 . iChild, e
18b08 54 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20 65 Type, iParent, e
18b09 50 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74 72 PtrmapType, iPtr
18b0a 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a mapParent);. }.
18b0b 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
18b0c 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 Check the integr
18b0d 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c ity of the freel
18b0e 69 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76 65 ist or of an ove
18b0f 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e rflow page list.
18b10 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 .** Verify that
18b11 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
18b12 67 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 ges on the list
18b13 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 is N..*/.static
18b14 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28 0a void checkList(.
18b15 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 IntegrityCk *p
18b16 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 67 Check, /* Integ
18b17 72 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63 6f rity checking co
18b18 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 ntext */. int i
18b19 73 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20 20 sFreeList,
18b1a 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 66 /* True for a f
18b1b 72 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65 20 reelist. False
18b1c 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 for overflow pag
18b1d 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 e list */. int
18b1e 69 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 iPage,
18b1f 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 /* Page number
18b20 20 66 6f 72 20 66 69 72 73 74 20 70 61 67 65 20 for first page
18b21 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 in the list */.
18b22 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20 int N,
18b23 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 /* Expect
18b24 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 ed number of pag
18b25 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a es in the list *
18b26 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 /. char *zConte
18b27 78 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e xt /* Con
18b28 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d text for error m
18b29 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 20 essages */.){.
18b2a 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78 70 int i;. int exp
18b2b 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e 74 ected = N;. int
18b2c 20 69 46 69 72 73 74 20 3d 20 69 50 61 67 65 3b iFirst = iPage;
18b2d 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 . while( N-- >
18b2e 30 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45 0 && pCheck->mxE
18b2f 72 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67 65 rr ){. DbPage
18b30 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20 20 *pOvflPage;.
18b31 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
18b32 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20 69 pOvflData;. i
18b33 66 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20 20 f( iPage<1 ){.
18b34 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
18b35 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 sg(pCheck, zCont
18b36 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22 25 ext,. "%
18b37 64 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d 69 d of %d pages mi
18b38 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72 66 ssing from overf
18b39 6c 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69 6e low list startin
18b3a 67 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20 20 g at %d",.
18b3b 20 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74 65 N+1, expecte
18b3c 64 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20 20 d, iFirst);.
18b3d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
18b3e 20 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 28 if( checkRef(
18b3f 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a pCheck, iPage, z
18b40 43 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61 6b Context) ) break
18b41 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
18b42 33 50 61 67 65 72 47 65 74 28 70 43 68 65 63 6b 3PagerGet(pCheck
18b43 2d 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f 29 ->pPager, (Pgno)
18b44 69 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61 67 iPage, &pOvflPag
18b45 65 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 e) ){. chec
18b46 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
18b47 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66 61 k, zContext, "fa
18b48 69 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67 65 iled to get page
18b49 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 %d", iPage);.
18b4a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
18b4b 0a 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20 3d . pOvflData =
18b4c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 (unsigned char
18b4d 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *)sqlite3PagerGe
18b4e 74 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65 29 tData(pOvflPage)
18b4f 3b 0a 20 20 20 20 69 66 28 20 69 73 46 72 65 65 ;. if( isFree
18b50 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69 6e List ){. in
18b51 74 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 t n = get4byte(&
18b52 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a 23 pOvflData[4]);.#
18b53 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
18b54 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
18b55 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e if( pCheck->
18b56 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
18b57 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b ){. check
18b58 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 Ptrmap(pCheck, i
18b59 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 Page, PTRMAP_FRE
18b5a 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 EPAGE, 0, zConte
18b5b 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e xt);. }.#en
18b5c 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e 3e dif. if( n>
18b5d 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 75 73 61 pCheck->pBt->usa
18b5e 62 6c 65 53 69 7a 65 2f 34 2d 32 20 29 7b 0a 20 bleSize/4-2 ){.
18b5f 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 checkAppe
18b60 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 ndMsg(pCheck, zC
18b61 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 ontext,.
18b62 20 20 20 22 66 72 65 65 6c 69 73 74 20 6c 65 61 "freelist lea
18b63 66 20 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67 20 f count too big
18b64 6f 6e 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 on page %d", iPa
18b65 67 65 29 3b 0a 20 20 20 20 20 20 20 20 4e 2d 2d ge);. N--
18b66 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
18b67 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 for(i=0;
18b68 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 i<n; i++){.
18b69 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 Pgno iFreeP
18b6a 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 age = get4byte(&
18b6b 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69 2a 34 5d pOvflData[8+i*4]
18b6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
18b6d 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
18b6e 4d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 M. if(
18b6f 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 pCheck->pBt->aut
18b70 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 oVacuum ){.
18b71 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d checkPtrm
18b72 61 70 28 70 43 68 65 63 6b 2c 20 69 46 72 65 65 ap(pCheck, iFree
18b73 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 Page, PTRMAP_FRE
18b74 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 EPAGE, 0, zConte
18b75 78 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d xt);. }
18b76 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 .#endif.
18b77 20 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 checkRef(pChec
18b78 6b 2c 20 69 46 72 65 65 50 61 67 65 2c 20 7a 43 k, iFreePage, zC
18b79 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 ontext);.
18b7a 20 7d 0a 20 20 20 20 20 20 20 20 4e 20 2d 3d 20 }. N -=
18b7b 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d n;. }. }
18b7c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
18b7d 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
18b7e 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 else{.
18b7f 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62 /* If this datab
18b80 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 ase supports aut
18b81 6f 2d 76 61 63 75 75 6d 20 61 6e 64 20 69 50 61 o-vacuum and iPa
18b82 67 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 ge is not the la
18b83 73 74 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 st. ** page
18b84 20 69 6e 20 74 68 69 73 20 6f 76 65 72 66 6c 6f in this overflo
18b85 77 20 6c 69 73 74 2c 20 63 68 65 63 6b 20 74 68 w list, check th
18b86 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d at the pointer-m
18b87 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 20 ap entry for.
18b88 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 ** the follow
18b89 69 6e 67 20 70 61 67 65 20 6d 61 74 63 68 65 73 ing page matches
18b8a 20 69 50 61 67 65 2e 0a 20 20 20 20 20 20 2a 2f iPage.. */
18b8b 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 63 . if( pChec
18b8c 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 k->pBt->autoVacu
18b8d 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0a 20 20 20 um && N>0 ){.
18b8e 20 20 20 20 20 69 20 3d 20 67 65 74 34 62 79 74 i = get4byt
18b8f 65 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 e(pOvflData);.
18b90 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 checkPtrma
18b91 70 28 70 43 68 65 63 6b 2c 20 69 2c 20 50 54 52 p(pCheck, i, PTR
18b92 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69 MAP_OVERFLOW2, i
18b93 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b Page, zContext);
18b94 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
18b95 65 6e 64 69 66 0a 20 20 20 20 69 50 61 67 65 20 endif. iPage
18b96 3d 20 67 65 74 34 62 79 74 65 28 70 4f 76 66 6c = get4byte(pOvfl
18b97 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 Data);. sqlit
18b98 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76 e3PagerUnref(pOv
18b99 66 6c 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23 flPage);. }.}.#
18b9a 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
18b9b 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 OMIT_INTEGRITY_C
18b9c 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 HECK */..#ifndef
18b9d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 SQLITE_OMIT_INT
18b9e 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a EGRITY_CHECK./*.
18b9f 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73 20 73 61 ** Do various sa
18ba0 6e 69 74 79 20 63 68 65 63 6b 73 20 6f 6e 20 61 nity checks on a
18ba1 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 single page of
18ba2 61 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 0a a tree. Return.
18ba3 2a 2a 20 74 68 65 20 74 72 65 65 20 64 65 70 74 ** the tree dept
18ba4 68 2e 20 20 52 6f 6f 74 20 70 61 67 65 73 20 72 h. Root pages r
18ba5 65 74 75 72 6e 20 30 2e 20 20 50 61 72 65 6e 74 eturn 0. Parent
18ba6 73 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 0a s of root pages.
18ba7 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20 61 6e 64 ** return 1, and
18ba8 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a so forth..** .*
18ba9 2a 20 54 68 65 73 65 20 63 68 65 63 6b 73 20 61 * These checks a
18baa 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 re done:.**.**
18bab 20 20 20 20 31 2e 20 20 4d 61 6b 65 20 73 75 72 1. Make sur
18bac 65 20 74 68 61 74 20 63 65 6c 6c 73 20 61 6e 64 e that cells and
18bad 20 66 72 65 65 62 6c 6f 63 6b 73 20 64 6f 20 6e freeblocks do n
18bae 6f 74 20 6f 76 65 72 6c 61 70 0a 2a 2a 20 20 20 ot overlap.**
18baf 20 20 20 20 20 20 20 62 75 74 20 63 6f 6d 62 69 but combi
18bb0 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 ne to completely
18bb1 20 63 6f 76 65 72 20 74 68 65 20 70 61 67 65 2e cover the page.
18bb2 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 20 4d 61 6b .** NO 2. Mak
18bb3 65 20 73 75 72 65 20 63 65 6c 6c 20 6b 65 79 73 e sure cell keys
18bb4 20 61 72 65 20 69 6e 20 6f 72 64 65 72 2e 0a 2a are in order..*
18bb5 2a 20 20 4e 4f 20 20 33 2e 20 20 4d 61 6b 65 20 * NO 3. Make
18bb6 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 6c sure no key is l
18bb7 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ess than or equa
18bb8 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e 64 l to zLowerBound
18bb9 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20 20 4d 61 ..** NO 4. Ma
18bba 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 ke sure no key i
18bbb 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f s greater than o
18bbc 72 20 65 71 75 61 6c 20 74 6f 20 7a 55 70 70 65 r equal to zUppe
18bbd 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20 20 20 20 rBound..**
18bbe 35 2e 20 20 43 68 65 63 6b 20 74 68 65 20 69 6e 5. Check the in
18bbf 74 65 67 72 69 74 79 20 6f 66 20 6f 76 65 72 66 tegrity of overf
18bc0 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 20 20 20 low pages..**
18bc1 20 20 20 36 2e 20 20 52 65 63 75 72 73 69 76 65 6. Recursive
18bc2 6c 79 20 63 61 6c 6c 20 63 68 65 63 6b 54 72 65 ly call checkTre
18bc3 65 50 61 67 65 20 6f 6e 20 61 6c 6c 20 63 68 69 ePage on all chi
18bc4 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 37 ldren..** 7
18bc5 2e 20 20 56 65 72 69 66 79 20 74 68 61 74 20 74 . Verify that t
18bc6 68 65 20 64 65 70 74 68 20 6f 66 20 61 6c 6c 20 he depth of all
18bc7 63 68 69 6c 64 72 65 6e 20 69 73 20 74 68 65 20 children is the
18bc8 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 38 2e same..** 8.
18bc9 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 Make sure this
18bca 20 70 61 67 65 20 69 73 20 61 74 20 6c 65 61 73 page is at leas
18bcb 74 20 33 33 25 20 66 75 6c 6c 20 6f 72 20 65 6c t 33% full or el
18bcc 73 65 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 se it is.**
18bcd 20 20 20 20 20 74 68 65 20 72 6f 6f 74 20 6f 66 the root of
18bce 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 the tree..*/.st
18bcf 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 54 72 atic int checkTr
18bd0 65 65 50 61 67 65 28 0a 20 20 49 6e 74 65 67 72 eePage(. Integr
18bd1 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 ityCk *pCheck,
18bd2 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 74 /* Context for t
18bd3 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 20 he sanity check
18bd4 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c 20 */. int iPage,
18bd5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
18bd6 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
18bd7 20 70 61 67 65 20 74 6f 20 63 68 65 63 6b 20 2a page to check *
18bd8 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 72 65 6e /. char *zParen
18bd9 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20 50 61 72 tContext /* Par
18bda 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 29 ent context */.)
18bdb 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 {. MemPage *pPa
18bdc 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c ge;. int i, rc,
18bdd 20 64 65 70 74 68 2c 20 64 32 2c 20 70 67 6e 6f depth, d2, pgno
18bde 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20 68 64 72 , cnt;. int hdr
18bdf 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a 20 20 69 , cellStart;. i
18be0 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 38 20 2a nt nCell;. u8 *
18be1 64 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 64 data;. BtShared
18be2 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20 75 73 61 *pBt;. int usa
18be3 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68 61 72 20 bleSize;. char
18be4 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d 3b 0a 20 zContext[100];.
18be5 20 63 68 61 72 20 2a 68 69 74 20 3d 20 30 3b 0a char *hit = 0;.
18be6 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 . sqlite3_snpri
18be7 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 ntf(sizeof(zCont
18be8 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 ext), zContext,
18be9 22 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50 61 "Page %d: ", iPa
18bea 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b ge);.. /* Check
18beb 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 65 that the page e
18bec 78 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42 74 xists. */. pBt
18bed 20 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b 0a = pCheck->pBt;.
18bee 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 usableSize = p
18bef 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a Bt->usableSize;.
18bf0 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29 if( iPage==0 )
18bf1 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 return 0;. if(
18bf2 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b checkRef(pCheck
18bf3 2c 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e 74 , iPage, zParent
18bf4 43 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75 72 Context) ) retur
18bf5 6e 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d n 0;. if( (rc =
18bf6 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 btreeGetPage(pB
18bf7 74 2c 20 28 50 67 6e 6f 29 69 50 61 67 65 2c 20 t, (Pgno)iPage,
18bf8 26 70 50 61 67 65 2c 20 30 29 29 21 3d 30 20 29 &pPage, 0))!=0 )
18bf9 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e {. checkAppen
18bfa 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f dMsg(pCheck, zCo
18bfb 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 22 75 ntext,. "u
18bfc 6e 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68 65 nable to get the
18bfd 20 70 61 67 65 2e 20 65 72 72 6f 72 20 63 6f 64 page. error cod
18bfe 65 3d 25 64 22 2c 20 72 63 29 3b 0a 20 20 20 20 e=%d", rc);.
18bff 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 return 0;. }..
18c00 20 2f 2a 20 43 6c 65 61 72 20 4d 65 6d 50 61 67 /* Clear MemPag
18c01 65 2e 69 73 49 6e 69 74 20 74 6f 20 6d 61 6b 65 e.isInit to make
18c02 20 73 75 72 65 20 74 68 65 20 63 6f 72 72 75 70 sure the corrup
18c03 74 69 6f 6e 20 64 65 74 65 63 74 69 6f 6e 20 63 tion detection c
18c04 6f 64 65 20 69 6e 0a 20 20 2a 2a 20 62 74 72 65 ode in. ** btre
18c05 65 49 6e 69 74 50 61 67 65 28 29 20 69 73 20 65 eInitPage() is e
18c06 78 65 63 75 74 65 64 2e 20 20 2a 2f 0a 20 20 70 xecuted. */. p
18c07 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 Page->isInit = 0
18c08 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20 62 74 ;. if( (rc = bt
18c09 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 reeInitPage(pPag
18c0a 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 e))!=0 ){. as
18c0b 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
18c0c 5f 43 4f 52 52 55 50 54 20 29 3b 20 20 2f 2a 20 _CORRUPT ); /*
18c0d 54 68 65 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c The only possibl
18c0e 65 20 65 72 72 6f 72 20 66 72 6f 6d 20 49 6e 69 e error from Ini
18c0f 74 50 61 67 65 20 2a 2f 0a 20 20 20 20 63 68 65 tPage */. che
18c10 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
18c11 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 ck, zContext, .
18c12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18c13 20 20 22 62 74 72 65 65 49 6e 69 74 50 61 67 65 "btreeInitPage
18c14 28 29 20 72 65 74 75 72 6e 73 20 65 72 72 6f 72 () returns error
18c15 20 63 6f 64 65 20 25 64 22 2c 20 72 63 29 3b 0a code %d", rc);.
18c16 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
18c17 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 pPage);. retu
18c18 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 rn 0;. }.. /*
18c19 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20 74 68 Check out all th
18c1a 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20 20 e cells.. */.
18c1b 64 65 70 74 68 20 3d 20 30 3b 0a 20 20 66 6f 72 depth = 0;. for
18c1c 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e (i=0; i<pPage->n
18c1d 43 65 6c 6c 20 26 26 20 70 43 68 65 63 6b 2d 3e Cell && pCheck->
18c1e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 mxErr; i++){.
18c1f 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 u8 *pCell;.
18c20 75 33 32 20 73 7a 3b 0a 20 20 20 20 43 65 6c 6c u32 sz;. Cell
18c21 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20 20 Info info;..
18c22 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c 6f 61 64 /* Check payload
18c23 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 0a overflow pages.
18c24 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 */. sqlit
18c25 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 e3_snprintf(size
18c26 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43 of(zContext), zC
18c27 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 ontext,.
18c28 20 20 20 20 20 22 4f 6e 20 74 72 65 65 20 70 61 "On tree pa
18c29 67 65 20 25 64 20 63 65 6c 6c 20 25 64 3a 20 22 ge %d cell %d: "
18c2a 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 , iPage, i);.
18c2b 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c pCell = findCel
18c2c 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20 20 20 20 l(pPage,i);.
18c2d 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
18c2e 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 r(pPage, pCell,
18c2f 26 69 6e 66 6f 29 3b 0a 20 20 20 20 73 7a 20 3d &info);. sz =
18c30 20 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 20 info.nData;.
18c31 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 6e 74 if( !pPage->int
18c32 4b 65 79 20 29 20 73 7a 20 2b 3d 20 28 69 6e 74 Key ) sz += (int
18c33 29 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 )info.nKey;.
18c34 61 73 73 65 72 74 28 20 73 7a 3d 3d 69 6e 66 6f assert( sz==info
18c35 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 20 .nPayload );.
18c36 20 69 66 28 20 28 73 7a 3e 69 6e 66 6f 2e 6e 4c if( (sz>info.nL
18c37 6f 63 61 6c 29 20 0a 20 20 20 20 20 26 26 20 28 ocal) . && (
18c38 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 &pCell[info.iOve
18c39 72 66 6c 6f 77 5d 3c 3d 26 70 50 61 67 65 2d 3e rflow]<=&pPage->
18c3a 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c aData[pBt->usabl
18c3b 65 53 69 7a 65 5d 29 0a 20 20 20 20 29 7b 0a 20 eSize]). ){.
18c3c 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 20 3d int nPage =
18c3d 20 28 73 7a 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 (sz - info.nLoc
18c3e 61 6c 20 2b 20 75 73 61 62 6c 65 53 69 7a 65 20 al + usableSize
18c3f 2d 20 35 29 2f 28 75 73 61 62 6c 65 53 69 7a 65 - 5)/(usableSize
18c40 20 2d 20 34 29 3b 0a 20 20 20 20 20 20 50 67 6e - 4);. Pgn
18c41 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 o pgnoOvfl = get
18c42 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 4byte(&pCell[inf
18c43 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 23 o.iOverflow]);.#
18c44 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
18c45 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
18c46 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 if( pBt->aut
18c47 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 oVacuum ){.
18c48 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 checkPtrmap(p
18c49 43 68 65 63 6b 2c 20 70 67 6e 6f 4f 76 66 6c 2c Check, pgnoOvfl,
18c4a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
18c4b 31 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 1, iPage, zConte
18c4c 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e xt);. }.#en
18c4d 64 69 66 0a 20 20 20 20 20 20 63 68 65 63 6b 4c dif. checkL
18c4e 69 73 74 28 70 43 68 65 63 6b 2c 20 30 2c 20 70 ist(pCheck, 0, p
18c4f 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61 67 65 2c 20 gnoOvfl, nPage,
18c50 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d zContext);. }
18c51 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 73 .. /* Check s
18c52 61 6e 69 74 79 20 6f 66 20 6c 65 66 74 20 63 68 anity of left ch
18c53 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f ild page.. */
18c54 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d . if( !pPage-
18c55 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 70 >leaf ){. p
18c56 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 gno = get4byte(p
18c57 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 Cell);.#ifndef S
18c58 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
18c59 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28 20 ACUUM. if(
18c5a 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
18c5b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b ){. check
18c5c 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 Ptrmap(pCheck, p
18c5d 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 gno, PTRMAP_BTRE
18c5e 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 E, iPage, zConte
18c5f 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e xt);. }.#en
18c60 64 69 66 0a 20 20 20 20 20 20 64 32 20 3d 20 63 dif. d2 = c
18c61 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 heckTreePage(pCh
18c62 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 eck, pgno, zCont
18c63 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ext);. if(
18c64 69 3e 30 20 26 26 20 64 32 21 3d 64 65 70 74 68 i>0 && d2!=depth
18c65 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 ){. chec
18c66 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
18c67 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 43 68 k, zContext, "Ch
18c68 69 6c 64 20 70 61 67 65 20 64 65 70 74 68 20 64 ild page depth d
18c69 69 66 66 65 72 73 22 29 3b 0a 20 20 20 20 20 20 iffers");.
18c6a 7d 0a 20 20 20 20 20 20 64 65 70 74 68 20 3d 20 }. depth =
18c6b 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 d2;. }. }.
18c6c 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
18c6d 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 ){. pgno = g
18c6e 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e et4byte(&pPage->
18c6f 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
18c70 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 Offset+8]);.
18c71 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
18c72 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 (sizeof(zContext
18c73 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 ), zContext, .
18c74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18c75 20 20 20 22 4f 6e 20 70 61 67 65 20 25 64 20 61 "On page %d a
18c76 74 20 72 69 67 68 74 20 63 68 69 6c 64 3a 20 22 t right child: "
18c77 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 , iPage);.#ifnde
18c78 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
18c79 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 TOVACUUM. if(
18c7a 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
18c7b 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 50 ){. checkP
18c7c 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67 trmap(pCheck, pg
18c7d 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 no, PTRMAP_BTREE
18c7e 2c 20 69 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 , iPage, 0);.
18c7f 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68 }.#endif. ch
18c80 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 65 eckTreePage(pChe
18c81 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65 ck, pgno, zConte
18c82 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 xt);. }. . /*
18c83 43 68 65 63 6b 20 66 6f 72 20 63 6f 6d 70 6c 65 Check for comple
18c84 74 65 20 63 6f 76 65 72 61 67 65 20 6f 66 20 74 te coverage of t
18c85 68 65 20 70 61 67 65 0a 20 20 2a 2f 0a 20 20 64 he page. */. d
18c86 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 ata = pPage->aDa
18c87 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67 ta;. hdr = pPag
18c88 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 e->hdrOffset;.
18c89 68 69 74 20 3d 20 73 71 6c 69 74 65 33 50 61 67 hit = sqlite3Pag
18c8a 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61 eMalloc( pBt->pa
18c8b 67 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 geSize );. if(
18c8c 68 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 43 hit==0 ){. pC
18c8d 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c heck->mallocFail
18c8e 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b ed = 1;. }else{
18c8f 0a 20 20 20 20 75 31 36 20 63 6f 6e 74 65 6e 74 . u16 content
18c90 4f 66 66 73 65 74 20 3d 20 67 65 74 32 62 79 74 Offset = get2byt
18c91 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b e(&data[hdr+5]);
18c92 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 6f 6e . assert( con
18c93 74 65 6e 74 4f 66 66 73 65 74 3c 3d 75 73 61 62 tentOffset<=usab
18c94 6c 65 53 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e leSize ); /* En
18c95 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49 forced by btreeI
18c96 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 nitPage() */.
18c97 20 6d 65 6d 73 65 74 28 68 69 74 2b 63 6f 6e 74 memset(hit+cont
18c98 65 6e 74 4f 66 66 73 65 74 2c 20 30 2c 20 75 73 entOffset, 0, us
18c99 61 62 6c 65 53 69 7a 65 2d 63 6f 6e 74 65 6e 74 ableSize-content
18c9a 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 6d 65 6d Offset);. mem
18c9b 73 65 74 28 68 69 74 2c 20 31 2c 20 63 6f 6e 74 set(hit, 1, cont
18c9c 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 entOffset);.
18c9d 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 nCell = get2byte
18c9e 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a (&data[hdr+3]);.
18c9f 20 20 20 20 63 65 6c 6c 53 74 61 72 74 20 3d 20 cellStart =
18ca0 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 hdr + 12 - 4*pPa
18ca1 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 66 6f ge->leaf;. fo
18ca2 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 r(i=0; i<nCell;
18ca3 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 i++){. int
18ca4 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 pc = get2byte(&d
18ca5 61 74 61 5b 63 65 6c 6c 53 74 61 72 74 2b 69 2a ata[cellStart+i*
18ca6 32 5d 29 3b 0a 20 20 20 20 20 20 75 31 36 20 73 2]);. u16 s
18ca7 69 7a 65 20 3d 20 31 30 32 34 3b 0a 20 20 20 20 ize = 1024;.
18ca8 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69 int j;. i
18ca9 66 28 20 70 63 3c 3d 75 73 61 62 6c 65 53 69 7a f( pc<=usableSiz
18caa 65 2d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 73 e-4 ){. s
18cab 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 ize = cellSizePt
18cac 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b 70 r(pPage, &data[p
18cad 63 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 c]);. }.
18cae 20 20 20 69 66 28 20 28 70 63 2b 73 69 7a 65 2d if( (pc+size-
18caf 31 29 3e 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 1)>=usableSize )
18cb0 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 {. checkA
18cb1 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c ppendMsg(pCheck,
18cb2 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 0, .
18cb3 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 "Corruption det
18cb4 65 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64 ected in cell %d
18cb5 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69 on page %d",i,i
18cb6 50 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d Page,0);. }
18cb7 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f else{. fo
18cb8 72 28 6a 3d 70 63 2b 73 69 7a 65 2d 31 3b 20 6a r(j=pc+size-1; j
18cb9 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a >=pc; j--) hit[j
18cba 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ]++;. }.
18cbb 20 7d 0a 20 20 20 20 69 20 3d 20 67 65 74 32 62 }. i = get2b
18cbc 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d yte(&data[hdr+1]
18cbd 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 3e );. while( i>
18cbe 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 0 ){. int s
18cbf 69 7a 65 2c 20 6a 3b 0a 20 20 20 20 20 20 61 73 ize, j;. as
18cc0 73 65 72 74 28 20 69 3c 3d 75 73 61 62 6c 65 53 sert( i<=usableS
18cc1 69 7a 65 2d 34 20 29 3b 20 20 20 20 20 2f 2a 20 ize-4 ); /*
18cc2 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 Enforced by btre
18cc3 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 eInitPage() */.
18cc4 20 20 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32 size = get2
18cc5 62 79 74 65 28 26 64 61 74 61 5b 69 2b 32 5d 29 byte(&data[i+2])
18cc6 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
18cc7 69 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69 i+size<=usableSi
18cc8 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63 ze ); /* Enforc
18cc9 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 ed by btreeInitP
18cca 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 66 age() */. f
18ccb 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d 31 3b 20 6a or(j=i+size-1; j
18ccc 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d >=i; j--) hit[j]
18ccd 2b 2b 3b 0a 20 20 20 20 20 20 6a 20 3d 20 67 65 ++;. j = ge
18cce 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 5d 29 t2byte(&data[i])
18ccf 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
18cd0 6a 3d 3d 30 20 7c 7c 20 6a 3e 69 2b 73 69 7a 65 j==0 || j>i+size
18cd1 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64 ); /* Enforced
18cd2 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 61 67 by btreeInitPag
18cd3 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 e() */. ass
18cd4 65 72 74 28 20 6a 3c 3d 75 73 61 62 6c 65 53 69 ert( j<=usableSi
18cd5 7a 65 2d 34 20 29 3b 20 20 20 2f 2a 20 45 6e 66 ze-4 ); /* Enf
18cd6 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49 6e orced by btreeIn
18cd7 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 itPage() */.
18cd8 20 20 69 20 3d 20 6a 3b 0a 20 20 20 20 7d 0a 20 i = j;. }.
18cd9 20 20 20 66 6f 72 28 69 3d 63 6e 74 3d 30 3b 20 for(i=cnt=0;
18cda 69 3c 75 73 61 62 6c 65 53 69 7a 65 3b 20 69 2b i<usableSize; i+
18cdb 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 68 69 +){. if( hi
18cdc 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 t[i]==0 ){.
18cdd 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 cnt++;.
18cde 7d 65 6c 73 65 20 69 66 28 20 68 69 74 5b 69 5d }else if( hit[i]
18cdf 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 >1 ){. ch
18ce0 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 eckAppendMsg(pCh
18ce1 65 63 6b 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 eck, 0,.
18ce2 20 20 22 4d 75 6c 74 69 70 6c 65 20 75 73 65 73 "Multiple uses
18ce3 20 66 6f 72 20 62 79 74 65 20 25 64 20 6f 66 20 for byte %d of
18ce4 70 61 67 65 20 25 64 22 2c 20 69 2c 20 69 50 61 page %d", i, iPa
18ce5 67 65 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 ge);. bre
18ce6 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
18ce7 7d 0a 20 20 20 20 69 66 28 20 63 6e 74 21 3d 64 }. if( cnt!=d
18ce8 61 74 61 5b 68 64 72 2b 37 5d 20 29 7b 0a 20 20 ata[hdr+7] ){.
18ce9 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
18cea 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20 sg(pCheck, 0, .
18ceb 20 20 20 20 20 20 20 20 20 22 46 72 61 67 6d 65 "Fragme
18cec 6e 74 61 74 69 6f 6e 20 6f 66 20 25 64 20 62 79 ntation of %d by
18ced 74 65 73 20 72 65 70 6f 72 74 65 64 20 61 73 20 tes reported as
18cee 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 0a %d on page %d",.
18cef 20 20 20 20 20 20 20 20 20 20 63 6e 74 2c 20 64 cnt, d
18cf0 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69 50 61 67 ata[hdr+7], iPag
18cf1 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 e);. }. }.
18cf2 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 sqlite3PageFree(
18cf3 68 69 74 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 hit);. releaseP
18cf4 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 age(pPage);. re
18cf5 74 75 72 6e 20 64 65 70 74 68 2b 31 3b 0a 7d 0a turn depth+1;.}.
18cf6 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
18cf7 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f _OMIT_INTEGRITY_
18cf8 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 CHECK */..#ifnde
18cf9 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
18cfa 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a TEGRITY_CHECK./*
18cfb 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
18cfc 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c 65 74 65 does a complete
18cfd 20 63 68 65 63 6b 20 6f 66 20 74 68 65 20 67 69 check of the gi
18cfe 76 65 6e 20 42 54 72 65 65 20 66 69 6c 65 2e 20 ven BTree file.
18cff 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a 2a 20 61 aRoot[] is.** a
18d00 6e 20 61 72 72 61 79 20 6f 66 20 70 61 67 65 73 n array of pages
18d01 20 6e 75 6d 62 65 72 73 20 77 65 72 65 20 65 61 numbers were ea
18d02 63 68 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 ch page number i
18d03 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 s the root page
18d04 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65 2e 20 20 of.** a table.
18d05 6e 52 6f 6f 74 20 69 73 20 74 68 65 20 6e 75 6d nRoot is the num
18d06 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
18d07 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 41 n aRoot..**.** A
18d08 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72 65 read-only or re
18d09 61 64 2d 77 72 69 74 65 20 74 72 61 6e 73 61 63 ad-write transac
18d0a 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65 tion must be ope
18d0b 6e 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 ned before calli
18d0c 6e 67 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 ng.** this funct
18d0d 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 ion..**.** Write
18d0e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
18d0f 72 72 6f 72 20 73 65 65 6e 20 69 6e 20 2a 70 6e rror seen in *pn
18d10 45 72 72 2e 20 20 45 78 63 65 70 74 20 66 6f 72 Err. Except for
18d11 20 73 6f 6d 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 some memory.**
18d12 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 allocation error
18d13 73 2c 20 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 s, an error mes
18d14 73 61 67 65 20 68 65 6c 64 20 69 6e 20 6d 65 6d sage held in mem
18d15 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
18d16 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 69 73 20 72 m.** malloc is r
18d17 65 74 75 72 6e 65 64 20 69 66 20 2a 70 6e 45 72 eturned if *pnEr
18d18 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 r is non-zero.
18d19 49 66 20 2a 70 6e 45 72 72 3d 3d 30 20 74 68 65 If *pnErr==0 the
18d1a 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 72 65 74 n NULL is.** ret
18d1b 75 72 6e 65 64 2e 20 20 49 66 20 61 20 6d 65 6d urned. If a mem
18d1c 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
18d1d 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c rror occurs, NUL
18d1e 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a L is returned..*
18d1f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18d20 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 char *sqlite3Bt
18d21 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63 reeIntegrityChec
18d22 6b 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20 k(. Btree *p,
18d23 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 /* The btree
18d24 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a 2f to be checked */
18d25 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 20 . int *aRoot,
18d26 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20 /* An array of
18d27 72 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d 62 65 root pages numbe
18d28 72 73 20 66 6f 72 20 69 6e 64 69 76 69 64 75 61 rs for individua
18d29 6c 20 74 72 65 65 73 20 2a 2f 0a 20 20 69 6e 74 l trees */. int
18d2a 20 6e 52 6f 6f 74 2c 20 20 20 20 2f 2a 20 4e 75 nRoot, /* Nu
18d2b 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
18d2c 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20 in aRoot[] */.
18d2d 69 6e 74 20 6d 78 45 72 72 2c 20 20 20 20 2f 2a int mxErr, /*
18d2e 20 53 74 6f 70 20 72 65 70 6f 72 74 69 6e 67 20 Stop reporting
18d2f 65 72 72 6f 72 73 20 61 66 74 65 72 20 74 68 69 errors after thi
18d30 73 20 6d 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 s many */. int
18d31 2a 70 6e 45 72 72 20 20 20 20 2f 2a 20 57 72 69 *pnErr /* Wri
18d32 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 te number of err
18d33 6f 72 73 20 73 65 65 6e 20 74 6f 20 74 68 69 73 ors seen to this
18d34 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a variable */.){.
18d35 20 20 50 67 6e 6f 20 69 3b 0a 20 20 69 6e 74 20 Pgno i;. int
18d36 6e 52 65 66 3b 0a 20 20 49 6e 74 65 67 72 69 74 nRef;. Integrit
18d37 79 43 6b 20 73 43 68 65 63 6b 3b 0a 20 20 42 74 yCk sCheck;. Bt
18d38 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
18d39 3e 70 42 74 3b 0a 20 20 63 68 61 72 20 7a 45 72 >pBt;. char zEr
18d3a 72 5b 31 30 30 5d 3b 0a 0a 20 20 73 71 6c 69 74 r[100];.. sqlit
18d3b 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
18d3c 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e . assert( p->in
18d3d 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 Trans>TRANS_NONE
18d3e 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 && pBt->inTrans
18d3f 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e action>TRANS_NON
18d40 45 20 29 3b 0a 20 20 6e 52 65 66 20 3d 20 73 71 E );. nRef = sq
18d41 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 lite3PagerRefcou
18d42 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b nt(pBt->pPager);
18d43 0a 20 20 73 43 68 65 63 6b 2e 70 42 74 20 3d 20 . sCheck.pBt =
18d44 70 42 74 3b 0a 20 20 73 43 68 65 63 6b 2e 70 50 pBt;. sCheck.pP
18d45 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 ager = pBt->pPag
18d46 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 50 61 er;. sCheck.nPa
18d47 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f ge = pagerPageco
18d48 75 6e 74 28 73 43 68 65 63 6b 2e 70 42 74 29 3b unt(sCheck.pBt);
18d49 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 20 . sCheck.mxErr
18d4a 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43 68 65 63 = mxErr;. sChec
18d4b 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 73 43 k.nErr = 0;. sC
18d4c 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 heck.mallocFaile
18d4d 64 20 3d 20 30 3b 0a 20 20 2a 70 6e 45 72 72 20 d = 0;. *pnErr
18d4e 3d 20 30 3b 0a 20 20 69 66 28 20 73 43 68 65 63 = 0;. if( sChec
18d4f 6b 2e 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 k.nPage==0 ){.
18d50 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
18d51 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 ave(p);. retu
18d52 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 43 68 65 rn 0;. }. sChe
18d53 63 6b 2e 61 6e 52 65 66 20 3d 20 73 71 6c 69 74 ck.anRef = sqlit
18d54 65 33 4d 61 6c 6c 6f 63 28 20 28 73 43 68 65 63 e3Malloc( (sChec
18d55 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f k.nPage+1)*sizeo
18d56 66 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30 f(sCheck.anRef[0
18d57 5d 29 20 29 3b 0a 20 20 69 66 28 20 21 73 43 68 ]) );. if( !sCh
18d58 65 63 6b 2e 61 6e 52 65 66 20 29 7b 0a 20 20 20 eck.anRef ){.
18d59 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20 20 20 *pnErr = 1;.
18d5a 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
18d5b 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 ve(p);. retur
18d5c 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 n 0;. }. for(i
18d5d 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 =0; i<=sCheck.nP
18d5e 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43 68 65 63 age; i++){ sChec
18d5f 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 30 3b 20 k.anRef[i] = 0;
18d60 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 4e 47 5f }. i = PENDING_
18d61 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 3b 0a BYTE_PAGE(pBt);.
18d62 20 20 69 66 28 20 69 3c 3d 73 43 68 65 63 6b 2e if( i<=sCheck.
18d63 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 73 43 68 nPage ){. sCh
18d64 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 31 eck.anRef[i] = 1
18d65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 ;. }. sqlite3S
18d66 74 72 41 63 63 75 6d 49 6e 69 74 28 26 73 43 68 trAccumInit(&sCh
18d67 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a 45 72 72 eck.errMsg, zErr
18d68 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 , sizeof(zErr),
18d69 32 30 30 30 30 29 3b 0a 0a 20 20 2f 2a 20 43 68 20000);.. /* Ch
18d6a 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74 eck the integrit
18d6b 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 y of the freelis
18d6c 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63 6b 4c 69 t. */. checkLi
18d6d 73 74 28 26 73 43 68 65 63 6b 2c 20 31 2c 20 67 st(&sCheck, 1, g
18d6e 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 et4byte(&pBt->pP
18d6f 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 age1->aData[32])
18d70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 65 ,. ge
18d71 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 t4byte(&pBt->pPa
18d72 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 2c ge1->aData[36]),
18d73 20 22 4d 61 69 6e 20 66 72 65 65 6c 69 73 74 3a "Main freelist:
18d74 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b ");.. /* Check
18d75 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 73 2e all the tables.
18d76 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b . */. for(i=0;
18d77 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74 20 26 26 (int)i<nRoot &&
18d78 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 sCheck.mxErr; i
18d79 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 52 6f ++){. if( aRo
18d7a 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 ot[i]==0 ) conti
18d7b 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c nue;.#ifndef SQL
18d7c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
18d7d 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d UUM. if( pBt-
18d7e 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 61 >autoVacuum && a
18d7f 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 Root[i]>1 ){.
18d80 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 26 checkPtrmap(&
18d81 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d sCheck, aRoot[i]
18d82 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 , PTRMAP_ROOTPAG
18d83 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a E, 0, 0);. }.
18d84 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b #endif. check
18d85 54 72 65 65 50 61 67 65 28 26 73 43 68 65 63 6b TreePage(&sCheck
18d86 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22 4c 69 73 , aRoot[i], "Lis
18d87 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74 73 3a t of tree roots:
18d88 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d ");. }.. /* M
18d89 61 6b 65 20 73 75 72 65 20 65 76 65 72 79 20 70 ake sure every p
18d8a 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20 age in the file
18d8b 69 73 20 72 65 66 65 72 65 6e 63 65 64 0a 20 20 is referenced.
18d8c 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c */. for(i=1; i<
18d8d 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 26 26 =sCheck.nPage &&
18d8e 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 sCheck.mxErr; i
18d8f 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 ++){.#ifdef SQLI
18d90 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
18d91 55 4d 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 UM. if( sChec
18d92 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 29 7b k.anRef[i]==0 ){
18d93 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 . checkAppe
18d94 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 ndMsg(&sCheck, 0
18d95 2c 20 22 50 61 67 65 20 25 64 20 69 73 20 6e 65 , "Page %d is ne
18d96 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 ver used", i);.
18d97 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 2f }.#else. /
18d98 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
18d99 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d e supports auto-
18d9a 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73 75 72 vacuum, make sur
18d9b 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f 6e 74 e no tables cont
18d9c 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 66 65 72 ain. ** refer
18d9d 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74 65 72 ences to pointer
18d9e 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 20 20 20 -map pages..
18d9f 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 */. if( sChec
18da0 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 26 26 k.anRef[i]==0 &&
18da1 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 . (PTRMAP
18da2 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 21 _PAGENO(pBt, i)!
18da3 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 75 74 6f =i || !pBt->auto
18da4 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 Vacuum) ){.
18da5 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 checkAppendMsg(
18da6 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 61 67 &sCheck, 0, "Pag
18da7 65 20 25 64 20 69 73 20 6e 65 76 65 72 20 75 73 e %d is never us
18da8 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 ed", i);. }.
18da9 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61 6e if( sCheck.an
18daa 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 0a 20 20 Ref[i]!=0 && .
18dab 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 41 47 (PTRMAP_PAG
18dac 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d 69 20 26 ENO(pBt, i)==i &
18dad 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 & pBt->autoVacuu
18dae 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 m) ){. chec
18daf 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 kAppendMsg(&sChe
18db0 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 65 72 20 ck, 0, "Pointer
18db1 6d 61 70 20 70 61 67 65 20 25 64 20 69 73 20 72 map page %d is r
18db2 65 66 65 72 65 6e 63 65 64 22 2c 20 69 29 3b 0a eferenced", i);.
18db3 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
18db4 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
18db5 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 20 64 this analysis d
18db6 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 61 6e 79 id not leave any
18db7 20 75 6e 72 65 66 28 29 20 70 61 67 65 73 2e 0a unref() pages..
18db8 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 ** This is an
18db9 69 6e 74 65 72 6e 61 6c 20 63 6f 6e 73 69 73 74 internal consist
18dba 65 6e 63 79 20 63 68 65 63 6b 3b 20 61 6e 20 69 ency check; an i
18dbb 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 0a 20 ntegrity check.
18dbc 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e 74 65 67 ** of the integ
18dbd 72 69 74 79 20 63 68 65 63 6b 2e 0a 20 20 2a 2f rity check.. */
18dbe 0a 20 20 69 66 28 20 4e 45 56 45 52 28 6e 52 65 . if( NEVER(nRe
18dbf 66 20 21 3d 20 73 71 6c 69 74 65 33 50 61 67 65 f != sqlite3Page
18dc0 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 rRefcount(pBt->p
18dc1 50 61 67 65 72 29 29 20 29 7b 0a 20 20 20 20 63 Pager)) ){. c
18dc2 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 heckAppendMsg(&s
18dc3 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 Check, 0, .
18dc4 20 22 4f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 "Outstanding pa
18dc5 67 65 20 63 6f 75 6e 74 20 67 6f 65 73 20 66 72 ge count goes fr
18dc6 6f 6d 20 25 64 20 74 6f 20 25 64 20 64 75 72 69 om %d to %d duri
18dc7 6e 67 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 ng this analysis
18dc8 22 2c 0a 20 20 20 20 20 20 6e 52 65 66 2c 20 73 ",. nRef, s
18dc9 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f qlite3PagerRefco
18dca 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 unt(pBt->pPager)
18dcb 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f . );. }.. /
18dcc 2a 20 43 6c 65 61 6e 20 20 75 70 20 61 6e 64 20 * Clean up and
18dcd 72 65 70 6f 72 74 20 65 72 72 6f 72 73 2e 0a 20 report errors..
18dce 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 */. sqlite3Btr
18dcf 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 73 71 eeLeave(p);. sq
18dd0 6c 69 74 65 33 5f 66 72 65 65 28 73 43 68 65 63 lite3_free(sChec
18dd1 6b 2e 61 6e 52 65 66 29 3b 0a 20 20 69 66 28 20 k.anRef);. if(
18dd2 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 sCheck.mallocFai
18dd3 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 led ){. sqlit
18dd4 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 e3StrAccumReset(
18dd5 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b &sCheck.errMsg);
18dd6 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 73 43 . *pnErr = sC
18dd7 68 65 63 6b 2e 6e 45 72 72 2b 31 3b 0a 20 20 20 heck.nErr+1;.
18dd8 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
18dd9 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 65 63 6b *pnErr = sCheck
18dda 2e 6e 45 72 72 3b 0a 20 20 69 66 28 20 73 43 68 .nErr;. if( sCh
18ddb 65 63 6b 2e 6e 45 72 72 3d 3d 30 20 29 20 73 71 eck.nErr==0 ) sq
18ddc 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 lite3StrAccumRes
18ddd 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 et(&sCheck.errMs
18dde 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c g);. return sql
18ddf 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 ite3StrAccumFini
18de0 73 68 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 sh(&sCheck.errMs
18de1 67 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 g);.}.#endif /*
18de2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 SQLITE_OMIT_INTE
18de3 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a GRITY_CHECK */..
18de4 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
18de5 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f full pathname o
18de6 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 f the underlying
18de7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
18de8 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 **.** The pager
18de9 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 61 filename is inva
18dea 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 riant as long as
18deb 20 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a the pager is.**
18dec 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73 open so it is s
18ded 61 66 65 20 74 6f 20 61 63 63 65 73 73 20 77 69 afe to access wi
18dee 74 68 6f 75 74 20 74 68 65 20 42 74 53 68 61 72 thout the BtShar
18def 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c ed mutex..*/.SQL
18df0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
18df1 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 t char *sqlite3B
18df2 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 treeGetFilename(
18df3 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 Btree *p){. ass
18df4 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 ert( p->pBt->pPa
18df5 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 ger!=0 );. retu
18df6 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 rn sqlite3PagerF
18df7 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e ilename(p->pBt->
18df8 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a pPager);.}../*.*
18df9 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 74 * Return the pat
18dfa 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 hname of the jou
18dfb 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 74 68 rnal file for th
18dfc 69 73 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 is database. The
18dfd 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 return.** value
18dfe 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 of this routine
18dff 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 65 67 is the same reg
18e00 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 ardless of wheth
18e01 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 er the journal f
18e02 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 ile.** has been
18e03 63 72 65 61 74 65 64 20 6f 72 20 6e 6f 74 2e 0a created or not..
18e04 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 **.** The pager
18e05 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 journal filename
18e06 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61 73 is invariant as
18e07 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61 67 long as the pag
18e08 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f er is.** open so
18e09 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 61 it is safe to a
18e0a 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 74 68 ccess without th
18e0b 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 e BtShared mutex
18e0c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18e0d 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
18e0e 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a sqlite3BtreeGetJ
18e0f 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 ournalname(Btree
18e10 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
18e11 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 3d p->pBt->pPager!=
18e12 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 0 );. return sq
18e13 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 lite3PagerJourna
18e14 6c 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 lname(p->pBt->pP
18e15 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ager);.}../*.**
18e16 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 Return non-zero
18e17 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e if a transaction
18e18 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 is active..*/.S
18e19 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
18e1a 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 t sqlite3BtreeIs
18e1b 49 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 InTrans(Btree *p
18e1c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d ){. assert( p==
18e1d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
18e1e 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
18e1f 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 utex) );. retur
18e20 6e 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54 72 n (p && (p->inTr
18e21 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
18e22 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ));.}../*.** Ret
18e23 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 urn non-zero if
18e24 61 20 72 65 61 64 20 28 6f 72 20 77 72 69 74 65 a read (or write
18e25 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 ) transaction is
18e26 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 active..*/.SQLI
18e27 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
18e28 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 qlite3BtreeIsInR
18e29 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 20 2a eadTrans(Btree *
18e2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 p){. assert( p
18e2b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
18e2c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
18e2d 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
18e2e 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e 54 . return p->inT
18e2f 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 rans!=TRANS_NONE
18e30 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 ;.}..SQLITE_PRIV
18e31 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
18e32 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 42 treeIsInBackup(B
18e33 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 tree *p){. asse
18e34 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 rt( p );. asser
18e35 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
18e36 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 _held(p->db->mut
18e37 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ex) );. return
18e38 70 2d 3e 6e 42 61 63 6b 75 70 21 3d 30 3b 0a 7d p->nBackup!=0;.}
18e39 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
18e3a 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 ction returns a
18e3b 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f pointer to a blo
18e3c 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f b of memory asso
18e3d 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 61 ciated with.** a
18e3e 20 73 69 6e 67 6c 65 20 73 68 61 72 65 64 2d 62 single shared-b
18e3f 74 72 65 65 2e 20 54 68 65 20 6d 65 6d 6f 72 79 tree. The memory
18e40 20 69 73 20 75 73 65 64 20 62 79 20 63 6c 69 65 is used by clie
18e41 6e 74 20 63 6f 64 65 20 66 6f 72 20 69 74 73 20 nt code for its
18e42 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 own.** purposes
18e43 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f (for example, to
18e44 20 73 74 6f 72 65 20 61 20 68 69 67 68 2d 6c 65 store a high-le
18e45 76 65 6c 20 73 63 68 65 6d 61 20 61 73 73 6f 63 vel schema assoc
18e46 69 61 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 iated with .** t
18e47 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 29 he shared-btree)
18e48 2e 20 54 68 65 20 62 74 72 65 65 20 6c 61 79 65 . The btree laye
18e49 72 20 6d 61 6e 61 67 65 73 20 72 65 66 65 72 65 r manages refere
18e4a 6e 63 65 20 63 6f 75 6e 74 69 6e 67 20 69 73 73 nce counting iss
18e4b 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ues..**.** The f
18e4c 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 69 irst time this i
18e4d 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68 s called on a sh
18e4e 61 72 65 64 2d 62 74 72 65 65 2c 20 6e 42 79 74 ared-btree, nByt
18e4f 65 73 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f es bytes of memo
18e50 72 79 0a 2a 2a 20 61 72 65 20 61 6c 6c 6f 63 61 ry.** are alloca
18e51 74 65 64 2c 20 7a 65 72 6f 65 64 2c 20 61 6e 64 ted, zeroed, and
18e52 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 returned to the
18e53 20 63 61 6c 6c 65 72 2e 20 46 6f 72 20 65 61 63 caller. For eac
18e54 68 20 73 75 62 73 65 71 75 65 6e 74 20 0a 2a 2a h subsequent .**
18e55 20 63 61 6c 6c 20 74 68 65 20 6e 42 79 74 65 73 call the nBytes
18e56 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 69 67 parameter is ig
18e57 6e 6f 72 65 64 20 61 6e 64 20 61 20 70 6f 69 6e nored and a poin
18e58 74 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65 20 ter to the same
18e59 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 blob.** of memor
18e5a 79 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a y returned. .**.
18e5b 2a 2a 20 49 66 20 74 68 65 20 6e 42 79 74 65 73 ** If the nBytes
18e5c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 20 parameter is 0
18e5d 61 6e 64 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 and the blob of
18e5e 6d 65 6d 6f 72 79 20 68 61 73 20 6e 6f 74 20 79 memory has not y
18e5f 65 74 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 et been.** alloc
18e60 61 74 65 64 2c 20 61 20 6e 75 6c 6c 20 70 6f 69 ated, a null poi
18e61 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 nter is returned
18e62 2e 20 49 66 20 74 68 65 20 62 6c 6f 62 20 68 61 . If the blob ha
18e63 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 2a s already been.*
18e64 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 * allocated, it
18e65 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 6e is returned as n
18e66 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 73 ormal..**.** Jus
18e67 74 20 62 65 66 6f 72 65 20 74 68 65 20 73 68 61 t before the sha
18e68 72 65 64 2d 62 74 72 65 65 20 69 73 20 63 6c 6f red-btree is clo
18e69 73 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f sed, the functio
18e6a 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 n passed as the
18e6b 0a 2a 2a 20 78 46 72 65 65 20 61 72 67 75 6d 65 .** xFree argume
18e6c 6e 74 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f nt when the memo
18e6d 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 ry allocation wa
18e6e 73 20 6d 61 64 65 20 69 73 20 69 6e 76 6f 6b 65 s made is invoke
18e6f 64 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 62 6c 6f d on the .** blo
18e70 62 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d b of allocated m
18e71 65 6d 6f 72 79 2e 20 54 68 69 73 20 66 75 6e 63 emory. This func
18e72 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 tion should not
18e73 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72 65 call sqlite3_fre
18e74 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 65 e().** on the me
18e75 6d 6f 72 79 2c 20 74 68 65 20 62 74 72 65 65 20 mory, the btree
18e76 6c 61 79 65 72 20 64 6f 65 73 20 74 68 61 74 2e layer does that.
18e77 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18e78 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
18e79 42 74 72 65 65 53 63 68 65 6d 61 28 42 74 72 65 BtreeSchema(Btre
18e7a 65 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 73 e *p, int nBytes
18e7b 2c 20 76 6f 69 64 28 2a 78 46 72 65 65 29 28 76 , void(*xFree)(v
18e7c 6f 69 64 20 2a 29 29 7b 0a 20 20 42 74 53 68 61 oid *)){. BtSha
18e7d 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
18e7e 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 t;. sqlite3Btre
18e7f 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 eEnter(p);. if(
18e80 20 21 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 26 !pBt->pSchema &
18e81 26 20 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20 & nBytes ){.
18e82 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 3d 20 73 pBt->pSchema = s
18e83 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
18e84 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 70 42 (nBytes);. pB
18e85 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20 3d t->xFreeSchema =
18e86 20 78 46 72 65 65 3b 0a 20 20 7d 0a 20 20 73 71 xFree;. }. sq
18e87 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
18e88 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 42 74 p);. return pBt
18e89 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f 2a ->pSchema;.}../*
18e8a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
18e8b 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 E_LOCKED_SHAREDC
18e8c 41 43 48 45 20 69 66 20 61 6e 6f 74 68 65 72 20 ACHE if another
18e8d 75 73 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65 user of the same
18e8e 20 73 68 61 72 65 64 20 0a 2a 2a 20 62 74 72 65 shared .** btre
18e8f 65 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e e as the argumen
18e90 74 20 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20 61 t handle holds a
18e91 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
18e92 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 on the .** sqli
18e93 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e te_master table.
18e94 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49 54 Otherwise SQLIT
18e95 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f E_OK..*/.SQLITE_
18e96 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
18e97 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f te3BtreeSchemaLo
18e98 63 6b 65 64 28 42 74 72 65 65 20 2a 70 29 7b 0a cked(Btree *p){.
18e99 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
18e9a 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
18e9b 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
18e9c 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
18e9d 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
18e9e 20 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 rc = queryShar
18e9f 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
18ea0 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c (p, MASTER_ROOT,
18ea1 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 61 READ_LOCK);. a
18ea2 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
18ea3 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 E_OK || rc==SQLI
18ea4 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 TE_LOCKED_SHARED
18ea5 43 41 43 48 45 20 29 3b 0a 20 20 73 71 6c 69 74 CACHE );. sqlit
18ea6 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
18ea7 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
18ea8 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
18ea9 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
18eaa 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 HE./*.** Obtain
18eab 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 a lock on the ta
18eac 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 ble whose root p
18ead 61 67 65 20 69 73 20 69 54 61 62 2e 20 20 54 68 age is iTab. Th
18eae 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 20 77 e.** lock is a w
18eaf 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 69 73 57 rite lock if isW
18eb0 72 69 74 65 6c 6f 63 6b 20 69 73 20 74 72 75 65 ritelock is true
18eb1 20 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b 0a or a read lock.
18eb2 2a 2a 20 69 66 20 69 74 20 69 73 20 66 61 6c 73 ** if it is fals
18eb3 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
18eb4 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
18eb5 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 BtreeLockTable(B
18eb6 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 tree *p, int iTa
18eb7 62 2c 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 b, u8 isWriteLoc
18eb8 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 k){. int rc = S
18eb9 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 QLITE_OK;. asse
18eba 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d rt( p->inTrans!=
18ebb 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 TRANS_NONE );.
18ebc 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 if( p->sharable
18ebd 29 7b 0a 20 20 20 20 75 38 20 6c 6f 63 6b 54 79 ){. u8 lockTy
18ebe 70 65 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 20 2b pe = READ_LOCK +
18ebf 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 isWriteLock;.
18ec0 20 20 61 73 73 65 72 74 28 20 52 45 41 44 5f 4c assert( READ_L
18ec1 4f 43 4b 2b 31 3d 3d 57 52 49 54 45 5f 4c 4f 43 OCK+1==WRITE_LOC
18ec2 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 K );. assert(
18ec3 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 isWriteLock==0
18ec4 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d || isWriteLock==
18ec5 31 20 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 1 );.. sqlite
18ec6 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
18ec7 20 20 20 20 72 63 20 3d 20 71 75 65 72 79 53 68 rc = querySh
18ec8 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
18ec9 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b ck(p, iTab, lock
18eca 54 79 70 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Type);. if( r
18ecb 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
18ecc 20 20 20 20 20 20 72 63 20 3d 20 73 65 74 53 68 rc = setSh
18ecd 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
18ece 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b ck(p, iTab, lock
18ecf 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Type);. }.
18ed0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
18ed1 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ve(p);. }. ret
18ed2 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 urn rc;.}.#endif
18ed3 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
18ed4 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f _OMIT_INCRBLOB./
18ed5 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 43 *.** Argument pC
18ed6 73 72 20 6d 75 73 74 20 62 65 20 61 20 63 75 72 sr must be a cur
18ed7 73 6f 72 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 sor opened for w
18ed8 72 69 74 69 6e 67 20 6f 6e 20 61 6e 20 0a 2a 2a riting on an .**
18ed9 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 20 63 75 INTKEY table cu
18eda 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 rrently pointing
18edb 20 61 74 20 61 20 76 61 6c 69 64 20 74 61 62 6c at a valid tabl
18edc 65 20 65 6e 74 72 79 2e 20 0a 2a 2a 20 54 68 69 e entry. .** Thi
18edd 73 20 66 75 6e 63 74 69 6f 6e 20 6d 6f 64 69 66 s function modif
18ede 69 65 73 20 74 68 65 20 64 61 74 61 20 73 74 6f ies the data sto
18edf 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 red as part of t
18ee0 68 61 74 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a hat entry..**.**
18ee1 20 4f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 63 Only the data c
18ee2 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e 6c 79 20 ontent may only
18ee3 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 69 74 20 be modified, it
18ee4 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
18ee5 74 6f 20 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 to .** change th
18ee6 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 e length of the
18ee7 64 61 74 61 20 73 74 6f 72 65 64 2e 20 49 66 20 data stored. If
18ee8 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
18ee9 20 63 61 6c 6c 65 64 20 77 69 74 68 0a 2a 2a 20 called with.**
18eea 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74 20 parameters that
18eeb 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 attempt to write
18eec 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
18eed 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 64 61 the existing da
18eee 74 61 2c 0a 2a 2a 20 6e 6f 20 6d 6f 64 69 66 69 ta,.** no modifi
18eef 63 61 74 69 6f 6e 73 20 61 72 65 20 6d 61 64 65 cations are made
18ef0 20 61 6e 64 20 53 51 4c 49 54 45 5f 43 4f 52 52 and SQLITE_CORR
18ef1 55 50 54 20 69 73 20 72 65 74 75 72 6e 65 64 2e UPT is returned.
18ef2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18ef3 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
18ef4 72 65 65 50 75 74 44 61 74 61 28 42 74 43 75 72 reePutData(BtCur
18ef5 73 6f 72 20 2a 70 43 73 72 2c 20 75 33 32 20 6f sor *pCsr, u32 o
18ef6 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 ffset, u32 amt,
18ef7 76 6f 69 64 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 void *z){. int
18ef8 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 rc;. assert( cu
18ef9 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
18efa 43 73 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Csr) );. assert
18efb 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
18efc 68 65 6c 64 28 70 43 73 72 2d 3e 70 42 74 72 65 held(pCsr->pBtre
18efd 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b e->db->mutex) );
18efe 0a 20 20 61 73 73 65 72 74 28 20 70 43 73 72 2d . assert( pCsr-
18eff 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c >isIncrblobHandl
18f00 65 20 29 3b 0a 0a 20 20 72 63 20 3d 20 72 65 73 e );.. rc = res
18f01 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 toreCursorPositi
18f02 6f 6e 28 70 43 73 72 29 3b 0a 20 20 69 66 28 20 on(pCsr);. if(
18f03 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
18f04 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
18f05 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 }. assert( pC
18f06 73 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 sr->eState!=CURS
18f07 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 OR_REQUIRESEEK )
18f08 3b 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65 53 ;. if( pCsr->eS
18f09 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c tate!=CURSOR_VAL
18f0a 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ID ){. return
18f0b 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 SQLITE_ABORT;.
18f0c 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 73 }.. /* Check s
18f0d 6f 6d 65 20 61 73 73 75 6d 70 74 69 6f 6e 73 3a ome assumptions:
18f0e 20 0a 20 20 2a 2a 20 20 20 28 61 29 20 74 68 65 . ** (a) the
18f0f 20 63 75 72 73 6f 72 20 69 73 20 6f 70 65 6e 20 cursor is open
18f10 66 6f 72 20 77 72 69 74 69 6e 67 2c 0a 20 20 2a for writing,. *
18f11 2a 20 20 20 28 62 29 20 74 68 65 72 65 20 69 73 * (b) there is
18f12 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 74 72 a read/write tr
18f13 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 0a ansaction open,.
18f14 20 20 2a 2a 20 20 20 28 63 29 20 74 68 65 20 63 ** (c) the c
18f15 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 73 20 onnection holds
18f16 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 a write-lock on
18f17 74 68 65 20 74 61 62 6c 65 20 28 69 66 20 72 65 the table (if re
18f18 71 75 69 72 65 64 29 2c 0a 20 20 2a 2a 20 20 20 quired),. **
18f19 28 64 29 20 74 68 65 72 65 20 61 72 65 20 6e 6f (d) there are no
18f1a 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 61 conflicting rea
18f1b 64 2d 6c 6f 63 6b 73 2c 20 61 6e 64 0a 20 20 2a d-locks, and. *
18f1c 2a 20 20 20 28 65 29 20 74 68 65 20 63 75 72 73 * (e) the curs
18f1d 6f 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 76 or points at a v
18f1e 61 6c 69 64 20 72 6f 77 20 6f 66 20 61 6e 20 69 alid row of an i
18f1f 6e 74 4b 65 79 20 74 61 62 6c 65 2e 0a 20 20 2a ntKey table.. *
18f20 2f 0a 20 20 69 66 28 20 21 70 43 73 72 2d 3e 77 /. if( !pCsr->w
18f21 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 65 74 rFlag ){. ret
18f22 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f urn SQLITE_READO
18f23 4e 4c 59 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 NLY;. }. asser
18f24 74 28 20 21 70 43 73 72 2d 3e 70 42 74 2d 3e 72 t( !pCsr->pBt->r
18f25 65 61 64 4f 6e 6c 79 20 26 26 20 70 43 73 72 2d eadOnly && pCsr-
18f26 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 >pBt->inTransact
18f27 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion==TRANS_WRITE
18f28 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 61 );. assert( ha
18f29 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c sSharedCacheTabl
18f2a 65 4c 6f 63 6b 28 70 43 73 72 2d 3e 70 42 74 72 eLock(pCsr->pBtr
18f2b 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e 6f 52 6f ee, pCsr->pgnoRo
18f2c 6f 74 2c 20 30 2c 20 32 29 20 29 3b 0a 20 20 61 ot, 0, 2) );. a
18f2d 73 73 65 72 74 28 20 21 68 61 73 52 65 61 64 43 ssert( !hasReadC
18f2e 6f 6e 66 6c 69 63 74 73 28 70 43 73 72 2d 3e 70 onflicts(pCsr->p
18f2f 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e Btree, pCsr->pgn
18f30 6f 52 6f 6f 74 29 20 29 3b 0a 20 20 61 73 73 65 oRoot) );. asse
18f31 72 74 28 20 70 43 73 72 2d 3e 61 70 50 61 67 65 rt( pCsr->apPage
18f32 5b 70 43 73 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 [pCsr->iPage]->i
18f33 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 72 65 74 75 ntKey );.. retu
18f34 72 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 rn accessPayload
18f35 28 70 43 73 72 2c 20 6f 66 66 73 65 74 2c 20 61 (pCsr, offset, a
18f36 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 mt, (unsigned ch
18f37 61 72 20 2a 29 7a 2c 20 31 29 3b 0a 7d 0a 0a 2f ar *)z, 1);.}../
18f38 2a 20 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 * .** Set a flag
18f39 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20 on this cursor
18f3a 74 6f 20 63 61 63 68 65 20 74 68 65 20 6c 6f 63 to cache the loc
18f3b 61 74 69 6f 6e 73 20 6f 66 20 70 61 67 65 73 20 ations of pages
18f3c 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 6f 76 65 from the .** ove
18f3d 72 66 6c 6f 77 20 6c 69 73 74 20 66 6f 72 20 74 rflow list for t
18f3e 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2e 20 he current row.
18f3f 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 This is used by
18f40 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a 2a cursors opened.*
18f41 2a 20 66 6f 72 20 69 6e 63 72 65 6d 65 6e 74 61 * for incrementa
18f42 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e 6c 79 2e 0a l blob IO only..
18f43 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
18f44 69 6f 6e 20 73 65 74 73 20 61 20 66 6c 61 67 20 ion sets a flag
18f45 6f 6e 6c 79 2e 20 54 68 65 20 61 63 74 75 61 6c only. The actual
18f46 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 20 63 page location c
18f47 61 63 68 65 0a 2a 2a 20 28 73 74 6f 72 65 64 20 ache.** (stored
18f48 69 6e 20 42 74 43 75 72 73 6f 72 2e 61 4f 76 65 in BtCursor.aOve
18f49 72 66 6c 6f 77 5b 5d 29 20 69 73 20 61 6c 6c 6f rflow[]) is allo
18f4a 63 61 74 65 64 20 61 6e 64 20 75 73 65 64 20 62 cated and used b
18f4b 79 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 63 y function.** ac
18f4c 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 28 74 cessPayload() (t
18f4d 68 65 20 77 6f 72 6b 65 72 20 66 75 6e 63 74 69 he worker functi
18f4e 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65 33 42 74 on for sqlite3Bt
18f4f 72 65 65 44 61 74 61 28 29 20 61 6e 64 0a 2a 2a reeData() and.**
18f50 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74 sqlite3BtreePut
18f51 44 61 74 61 28 29 29 2e 0a 2a 2f 0a 53 51 4c 49 Data())..*/.SQLI
18f52 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
18f53 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 sqlite3BtreeCach
18f54 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72 73 eOverflow(BtCurs
18f55 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 or *pCur){. ass
18f56 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
18f57 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
18f58 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
18f59 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 _mutex_held(pCur
18f5a 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 ->pBtree->db->mu
18f5b 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
18f5c 28 21 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c (!pCur->isIncrbl
18f5d 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20 61 73 73 obHandle);. ass
18f5e 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72 ert(!pCur->aOver
18f5f 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e 69 flow);. pCur->i
18f60 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 sIncrblobHandle
18f61 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f = 1;.}.#endif../
18f62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
18f63 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 20 2a 2a nd of btree.c **
18f64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
18f67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
18f68 65 67 69 6e 20 66 69 6c 65 20 62 61 63 6b 75 70 egin file backup
18f69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
18f6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
18f6c 2a 0a 2a 2a 20 32 30 30 39 20 4a 61 6e 75 61 72 *.** 2009 Januar
18f6d 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 y 28.**.** The a
18f6e 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
18f6f 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
18f70 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
18f71 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
18f72 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
18f73 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
18f74 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
18f75 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
18f76 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
18f77 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
18f78 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
18f79 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
18f7a 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
18f7b 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
18f7c 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
18f7d 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
18f7e 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
18f7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f83 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
18f84 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d contains the im
18f85 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
18f86 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b the sqlite3_back
18f87 75 70 5f 58 58 58 28 29 20 0a 2a 2a 20 41 50 49 up_XXX() .** API
18f88 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 74 functions and t
18f89 68 65 20 72 65 6c 61 74 65 64 20 66 65 61 74 75 he related featu
18f8a 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 res..**.** $Id:
18f8b 62 61 63 6b 75 70 2e 63 2c 76 20 31 2e 31 39 20 backup.c,v 1.19
18f8c 32 30 30 39 2f 30 37 2f 30 36 20 31 39 3a 30 33 2009/07/06 19:03
18f8d 3a 31 33 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :13 drh Exp $.*/
18f8e 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69 ../* Macro to fi
18f8f 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f nd the minimum o
18f90 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 61 f two numeric va
18f91 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 lues..*/.#ifndef
18f92 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49 MIN.# define MI
18f93 4e 28 78 2c 79 29 20 28 28 78 29 3c 28 79 29 3f N(x,y) ((x)<(y)?
18f94 28 78 29 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a (x):(y)).#endif.
18f95 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 ./*.** Structure
18f96 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 allocated for e
18f97 61 63 68 20 62 61 63 6b 75 70 20 6f 70 65 72 61 ach backup opera
18f98 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 tion..*/.struct
18f99 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 7b sqlite3_backup {
18f9a 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 44 65 73 . sqlite3* pDes
18f9b 74 44 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 44 tDb; /* D
18f9c 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
18f9d 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 ase handle */.
18f9e 42 74 72 65 65 20 2a 70 44 65 73 74 3b 20 20 20 Btree *pDest;
18f9f 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 /* Dest
18fa0 69 6e 61 74 69 6f 6e 20 62 2d 74 72 65 65 20 66 ination b-tree f
18fa1 69 6c 65 20 2a 2f 0a 20 20 75 33 32 20 69 44 65 ile */. u32 iDe
18fa2 73 74 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 stSchema;
18fa3 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 73 63 /* Original sc
18fa4 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69 6e 20 64 hema cookie in d
18fa5 65 73 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 estination */.
18fa6 69 6e 74 20 62 44 65 73 74 4c 6f 63 6b 65 64 3b int bDestLocked;
18fa7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
18fa8 20 6f 6e 63 65 20 61 20 77 72 69 74 65 2d 74 72 once a write-tr
18fa9 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 ansaction is ope
18faa 6e 20 6f 6e 20 70 44 65 73 74 20 2a 2f 0a 0a 20 n on pDest */..
18fab 20 50 67 6e 6f 20 69 4e 65 78 74 3b 20 20 20 20 Pgno iNext;
18fac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
18fad 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
18fae 6e 65 78 74 20 73 6f 75 72 63 65 20 70 61 67 65 next source page
18faf 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 73 71 to copy */. sq
18fb0 6c 69 74 65 33 2a 20 70 53 72 63 44 62 3b 20 20 lite3* pSrcDb;
18fb1 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 /* Source
18fb2 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
18fb3 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 53 72 */. Btree *pSr
18fb4 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f c; /
18fb5 2a 20 53 6f 75 72 63 65 20 62 2d 74 72 65 65 20 * Source b-tree
18fb6 66 69 6c 65 20 2a 2f 0a 0a 20 20 69 6e 74 20 72 file */.. int r
18fb7 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
18fb8 20 20 20 20 2f 2a 20 42 61 63 6b 75 70 20 70 72 /* Backup pr
18fb9 6f 63 65 73 73 20 65 72 72 6f 72 20 63 6f 64 65 ocess error code
18fba 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 73 65 20 */.. /* These
18fbb 74 77 6f 20 76 61 72 69 61 62 6c 65 73 20 61 72 two variables ar
18fbc 65 20 73 65 74 20 62 79 20 65 76 65 72 79 20 63 e set by every c
18fbd 61 6c 6c 20 74 6f 20 62 61 63 6b 75 70 5f 73 74 all to backup_st
18fbe 65 70 28 29 2e 20 54 68 65 79 20 61 72 65 0a 20 ep(). They are.
18fbf 20 2a 2a 20 72 65 61 64 20 62 79 20 63 61 6c 6c ** read by call
18fc0 73 20 74 6f 20 62 61 63 6b 75 70 5f 72 65 6d 61 s to backup_rema
18fc1 69 6e 69 6e 67 28 29 20 61 6e 64 20 62 61 63 6b ining() and back
18fc2 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a up_pagecount()..
18fc3 20 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 52 65 6d */. Pgno nRem
18fc4 61 69 6e 69 6e 67 3b 20 20 20 20 20 20 20 20 20 aining;
18fc5 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 /* Number of pag
18fc6 65 73 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 es left to copy
18fc7 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 63 */. Pgno nPagec
18fc8 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a ount; /*
18fc9 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 Total number of
18fca 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 20 2a pages to copy *
18fcb 2f 0a 0a 20 20 69 6e 74 20 69 73 41 74 74 61 63 /.. int isAttac
18fcc 68 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a hed; /*
18fcd 20 54 72 75 65 20 6f 6e 63 65 20 62 61 63 6b 75 True once backu
18fce 70 20 68 61 73 20 62 65 65 6e 20 72 65 67 69 73 p has been regis
18fcf 74 65 72 65 64 20 77 69 74 68 20 70 61 67 65 72 tered with pager
18fd0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 */. sqlite3_ba
18fd1 63 6b 75 70 20 2a 70 4e 65 78 74 3b 20 20 20 2f ckup *pNext; /
18fd2 2a 20 4e 65 78 74 20 62 61 63 6b 75 70 20 61 73 * Next backup as
18fd3 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 73 6f sociated with so
18fd4 75 72 63 65 20 70 61 67 65 72 20 2a 2f 0a 7d 3b urce pager */.};
18fd5 0a 0a 2f 2a 0a 2a 2a 20 54 48 52 45 41 44 20 53 ../*.** THREAD S
18fd6 41 46 45 54 59 20 4e 4f 54 45 53 3a 0a 2a 2a 0a AFETY NOTES:.**.
18fd7 2a 2a 20 20 20 4f 6e 63 65 20 69 74 20 68 61 73 ** Once it has
18fd8 20 62 65 65 6e 20 63 72 65 61 74 65 64 20 75 73 been created us
18fd9 69 6e 67 20 62 61 63 6b 75 70 5f 69 6e 69 74 28 ing backup_init(
18fda 29 2c 20 61 20 73 69 6e 67 6c 65 20 73 71 6c 69 ), a single sqli
18fdb 74 65 33 5f 62 61 63 6b 75 70 0a 2a 2a 20 20 20 te3_backup.**
18fdc 73 74 72 75 63 74 75 72 65 20 6d 61 79 20 62 65 structure may be
18fdd 20 61 63 63 65 73 73 65 64 20 76 69 61 20 74 77 accessed via tw
18fde 6f 20 67 72 6f 75 70 73 20 6f 66 20 74 68 72 65 o groups of thre
18fdf 61 64 2d 73 61 66 65 20 65 6e 74 72 79 20 70 6f ad-safe entry po
18fe0 69 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ints:.**.**
18fe1 2a 20 56 69 61 20 74 68 65 20 73 71 6c 69 74 65 * Via the sqlite
18fe2 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 20 41 3_backup_XXX() A
18fe3 50 49 20 66 75 6e 63 74 69 6f 6e 20 62 61 63 6b PI function back
18fe4 75 70 5f 73 74 65 70 28 29 20 61 6e 64 20 0a 2a up_step() and .*
18fe5 2a 20 20 20 20 20 20 20 62 61 63 6b 75 70 5f 66 * backup_f
18fe6 69 6e 69 73 68 28 29 2e 20 42 6f 74 68 20 74 68 inish(). Both th
18fe7 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 6f 62 ese functions ob
18fe8 74 61 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 tain the source
18fe9 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 database.**
18fea 20 20 68 61 6e 64 6c 65 20 6d 75 74 65 78 20 61 handle mutex a
18feb 6e 64 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 nd the mutex ass
18fec 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
18fed 20 73 6f 75 72 63 65 20 42 74 53 68 61 72 65 64 source BtShared
18fee 20 0a 2a 2a 20 20 20 20 20 20 20 73 74 72 75 63 .** struc
18fef 74 75 72 65 2c 20 69 6e 20 74 68 61 74 20 6f 72 ture, in that or
18ff0 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a der..**.** *
18ff1 20 56 69 61 20 74 68 65 20 42 61 63 6b 75 70 55 Via the BackupU
18ff2 70 64 61 74 65 28 29 20 61 6e 64 20 42 61 63 6b pdate() and Back
18ff3 75 70 52 65 73 74 61 72 74 28 29 20 66 75 6e 63 upRestart() func
18ff4 74 69 6f 6e 73 2c 20 77 68 69 63 68 20 61 72 65 tions, which are
18ff5 0a 2a 2a 20 20 20 20 20 20 20 69 6e 76 6f 6b 65 .** invoke
18ff6 64 20 62 79 20 74 68 65 20 70 61 67 65 72 20 6c d by the pager l
18ff7 61 79 65 72 20 74 6f 20 72 65 70 6f 72 74 20 76 ayer to report v
18ff8 61 72 69 6f 75 73 20 73 74 61 74 65 20 63 68 61 arious state cha
18ff9 6e 67 65 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20 nges in.**
18ffa 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20 the page cache
18ffb 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
18ffc 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 the source datab
18ffd 61 73 65 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a ase. The mutex.*
18ffe 2a 20 20 20 20 20 20 20 61 73 73 6f 63 69 61 74 * associat
18fff 65 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 72 ed with the sour
19000 63 65 20 64 61 74 61 62 61 73 65 20 42 74 53 68 ce database BtSh
19001 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20 77 ared structure w
19002 69 6c 6c 20 61 6c 77 61 79 73 20 0a 2a 2a 20 20 ill always .**
19003 20 20 20 20 20 62 65 20 68 65 6c 64 20 77 68 65 be held whe
19004 6e 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 n either of thes
19005 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 e functions are
19006 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 invoked..**.**
19007 20 54 68 65 20 6f 74 68 65 72 20 73 71 6c 69 74 The other sqlit
19008 65 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 20 e3_backup_XXX()
19009 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 2c 20 62 API functions, b
1900a 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 ackup_remaining(
1900b 29 20 61 6e 64 0a 2a 2a 20 20 20 62 61 63 6b 75 ) and.** backu
1900c 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 20 61 72 p_pagecount() ar
1900d 65 20 6e 6f 74 20 74 68 72 65 61 64 2d 73 61 66 e not thread-saf
1900e 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20 49 66 20 e functions. If
1900f 74 68 65 79 20 61 72 65 20 63 61 6c 6c 65 64 0a they are called.
19010 2a 2a 20 20 20 77 68 69 6c 65 20 73 6f 6d 65 20 ** while some
19011 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 other thread is
19012 63 61 6c 6c 69 6e 67 20 62 61 63 6b 75 70 5f 73 calling backup_s
19013 74 65 70 28 29 20 6f 72 20 62 61 63 6b 75 70 5f tep() or backup_
19014 66 69 6e 69 73 68 28 29 2c 0a 2a 2a 20 20 20 74 finish(),.** t
19015 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e he values return
19016 65 64 20 6d 61 79 20 62 65 20 69 6e 76 61 6c 69 ed may be invali
19017 64 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 77 d. There is no w
19018 61 79 20 66 6f 72 20 61 20 63 61 6c 6c 20 74 6f ay for a call to
19019 0a 2a 2a 20 20 20 42 61 63 6b 75 70 55 70 64 61 .** BackupUpda
1901a 74 65 28 29 20 6f 72 20 42 61 63 6b 75 70 52 65 te() or BackupRe
1901b 73 74 61 72 74 28 29 20 74 6f 20 69 6e 74 65 72 start() to inter
1901c 66 65 72 65 20 77 69 74 68 20 62 61 63 6b 75 70 fere with backup
1901d 5f 72 65 6d 61 69 6e 69 6e 67 28 29 0a 2a 2a 20 _remaining().**
1901e 20 20 6f 72 20 62 61 63 6b 75 70 5f 70 61 67 65 or backup_page
1901f 63 6f 75 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 count()..**.**
19020 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 Depending on th
19021 65 20 53 51 4c 69 74 65 20 63 6f 6e 66 69 67 75 e SQLite configu
19022 72 61 74 69 6f 6e 2c 20 74 68 65 20 64 61 74 61 ration, the data
19023 62 61 73 65 20 68 61 6e 64 6c 65 73 20 61 6e 64 base handles and
19024 2f 6f 72 0a 2a 2a 20 20 20 74 68 65 20 42 74 72 /or.** the Btr
19025 65 65 20 6f 62 6a 65 63 74 73 20 6d 61 79 20 68 ee objects may h
19026 61 76 65 20 74 68 65 69 72 20 6f 77 6e 20 6d 75 ave their own mu
19027 74 65 78 65 73 20 74 68 61 74 20 72 65 71 75 69 texes that requi
19028 72 65 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 re locking..**
19029 20 4e 6f 6e 2d 73 68 61 72 61 62 6c 65 20 42 74 Non-sharable Bt
1902a 72 65 65 73 20 28 69 6e 2d 6d 65 6d 6f 72 79 20 rees (in-memory
1902b 64 61 74 61 62 61 73 65 73 20 66 6f 72 20 65 78 databases for ex
1902c 61 6d 70 6c 65 29 2c 20 64 6f 20 6e 6f 74 20 68 ample), do not h
1902d 61 76 65 0a 2a 2a 20 20 20 61 73 73 6f 63 69 61 ave.** associa
1902e 74 65 64 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a ted mutexes..*/.
1902f 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
19030 70 6f 69 6e 74 65 72 20 63 6f 72 72 65 73 70 6f pointer correspo
19031 6e 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61 73 nding to databas
19032 65 20 7a 44 62 20 28 69 2e 65 2e 20 22 6d 61 69 e zDb (i.e. "mai
19033 6e 22 2c 20 22 74 65 6d 70 22 29 0a 2a 2a 20 69 n", "temp").** i
19034 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e n connection han
19035 64 6c 65 20 70 44 62 2e 20 49 66 20 73 75 63 68 dle pDb. If such
19036 20 61 20 64 61 74 61 62 61 73 65 20 63 61 6e 6e a database cann
19037 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 72 65 74 ot be found, ret
19038 75 72 6e 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f urn.** a NULL po
19039 69 6e 74 65 72 20 61 6e 64 20 77 72 69 74 65 20 inter and write
1903a 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
1903b 20 74 6f 20 70 45 72 72 6f 72 44 62 2e 0a 2a 2a to pErrorDb..**
1903c 0a 2a 2a 20 49 66 20 74 68 65 20 22 74 65 6d 70 .** If the "temp
1903d 22 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65 " database is re
1903e 71 75 65 73 74 65 64 2c 20 69 74 20 6d 61 79 20 quested, it may
1903f 6e 65 65 64 20 74 6f 20 62 65 20 6f 70 65 6e 65 need to be opene
19040 64 20 62 79 20 74 68 69 73 20 0a 2a 2a 20 66 75 d by this .** fu
19041 6e 63 74 69 6f 6e 2e 20 49 66 20 61 6e 20 65 72 nction. If an er
19042 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 ror occurs while
19043 20 64 6f 69 6e 67 20 73 6f 2c 20 72 65 74 75 72 doing so, retur
19044 6e 20 30 20 61 6e 64 20 77 72 69 74 65 20 61 6e n 0 and write an
19045 20 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 .** error messa
19046 67 65 20 74 6f 20 70 45 72 72 6f 72 44 62 2e 0a ge to pErrorDb..
19047 2a 2f 0a 73 74 61 74 69 63 20 42 74 72 65 65 20 */.static Btree
19048 2a 66 69 6e 64 42 74 72 65 65 28 73 71 6c 69 74 *findBtree(sqlit
19049 65 33 20 2a 70 45 72 72 6f 72 44 62 2c 20 73 71 e3 *pErrorDb, sq
1904a 6c 69 74 65 33 20 2a 70 44 62 2c 20 63 6f 6e 73 lite3 *pDb, cons
1904b 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0a 20 20 t char *zDb){.
1904c 69 6e 74 20 69 20 3d 20 73 71 6c 69 74 65 33 46 int i = sqlite3F
1904d 69 6e 64 44 62 4e 61 6d 65 28 70 44 62 2c 20 7a indDbName(pDb, z
1904e 44 62 29 3b 0a 0a 20 20 69 66 28 20 69 3d 3d 31 Db);.. if( i==1
1904f 20 29 7b 0a 20 20 20 20 50 61 72 73 65 20 2a 70 ){. Parse *p
19050 50 61 72 73 65 3b 0a 20 20 20 20 69 6e 74 20 72 Parse;. int r
19051 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 73 c = 0;. pPars
19052 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b e = sqlite3Stack
19053 41 6c 6c 6f 63 5a 65 72 6f 28 70 45 72 72 6f 72 AllocZero(pError
19054 44 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61 72 Db, sizeof(*pPar
19055 73 65 29 29 3b 0a 20 20 20 20 69 66 28 20 70 50 se));. if( pP
19056 61 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 arse==0 ){.
19057 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 45 sqlite3Error(pE
19058 72 72 6f 72 44 62 2c 20 53 51 4c 49 54 45 5f 4e rrorDb, SQLITE_N
19059 4f 4d 45 4d 2c 20 22 6f 75 74 20 6f 66 20 6d 65 OMEM, "out of me
1905a 6d 6f 72 79 22 29 3b 0a 20 20 20 20 20 20 72 63 mory");. rc
1905b 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
1905c 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1905d 20 20 70 50 61 72 73 65 2d 3e 64 62 20 3d 20 70 pParse->db = p
1905e 44 62 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 Db;. if( sq
1905f 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 lite3OpenTempDat
19060 61 62 61 73 65 28 70 50 61 72 73 65 29 20 29 7b abase(pParse) ){
19061 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
19062 45 72 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73 ErrorClear(pPars
19063 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 e);. sqli
19064 74 65 33 45 72 72 6f 72 28 70 45 72 72 6f 72 44 te3Error(pErrorD
19065 62 2c 20 70 50 61 72 73 65 2d 3e 72 63 2c 20 22 b, pParse->rc, "
19066 25 73 22 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72 %s", pParse->zEr
19067 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 72 rMsg);. r
19068 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
19069 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1906a 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 sqlite3StackFree
1906b 28 70 45 72 72 6f 72 44 62 2c 20 70 50 61 72 73 (pErrorDb, pPars
1906c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 e);. }. if
1906d 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 ( rc ){. re
1906e 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 turn 0;. }.
1906f 7d 0a 0a 20 20 69 66 28 20 69 3c 30 20 29 7b 0a }.. if( i<0 ){.
19070 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
19071 28 70 45 72 72 6f 72 44 62 2c 20 53 51 4c 49 54 (pErrorDb, SQLIT
19072 45 5f 45 52 52 4f 52 2c 20 22 75 6e 6b 6e 6f 77 E_ERROR, "unknow
19073 6e 20 64 61 74 61 62 61 73 65 20 25 73 22 2c 20 n database %s",
19074 7a 44 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e zDb);. return
19075 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 0;. }.. retur
19076 6e 20 70 44 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 n pDb->aDb[i].pB
19077 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 t;.}../*.** Crea
19078 74 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 61 te an sqlite3_ba
19079 63 6b 75 70 20 70 72 6f 63 65 73 73 20 74 6f 20 ckup process to
1907a 63 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 copy the content
1907b 73 20 6f 66 20 7a 53 72 63 44 62 20 66 72 6f 6d s of zSrcDb from
1907c 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 .** connection h
1907d 61 6e 64 6c 65 20 70 53 72 63 44 62 20 74 6f 20 andle pSrcDb to
1907e 7a 44 65 73 74 44 62 20 69 6e 20 70 44 65 73 74 zDestDb in pDest
1907f 44 62 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 Db. If successfu
19080 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 l, return.** a p
19081 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
19082 77 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 w sqlite3_backup
19083 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 object..**.** I
19084 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
19085 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 s, NULL is retur
19086 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 ned and an error
19087 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20 code and error
19088 6d 65 73 73 61 67 65 0a 2a 2a 20 73 74 6f 72 65 message.** store
19089 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 68 61 d in database ha
1908a 6e 64 6c 65 20 70 44 65 73 74 44 62 2e 0a 2a 2f ndle pDestDb..*/
1908b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
1908c 74 65 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69 te3_backup *sqli
1908d 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 te3_backup_init(
1908e 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 44 65 73 . sqlite3* pDes
1908f 74 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 tDb,
19090 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
19091 62 61 73 65 20 74 6f 20 77 72 69 74 65 20 74 6f base to write to
19092 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
19093 20 2a 7a 44 65 73 74 44 62 2c 20 20 20 20 20 20 *zDestDb,
19094 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
19095 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 ame of database
19096 77 69 74 68 69 6e 20 70 44 65 73 74 44 62 20 2a within pDestDb *
19097 2f 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 53 72 /. sqlite3* pSr
19098 63 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 cDb,
19099 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
1909a 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1909b 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f to read from */
1909c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1909d 53 72 63 44 62 20 20 20 20 20 20 20 20 20 20 20 SrcDb
1909e 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
1909f 20 6f 66 20 64 61 74 61 62 61 73 65 20 77 69 74 of database wit
190a0 68 69 6e 20 70 53 72 63 44 62 20 2a 2f 0a 29 7b hin pSrcDb */.){
190a1 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 . sqlite3_backu
190a2 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 p *p;
190a3 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 /* Valu
190a4 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a e to return */..
190a5 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 73 6f /* Lock the so
190a6 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61 urce database ha
190a7 6e 64 6c 65 2e 20 54 68 65 20 64 65 73 74 69 6e ndle. The destin
190a8 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 0a 20 ation database.
190a9 20 2a 2a 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f ** handle is no
190aa 74 20 6c 6f 63 6b 65 64 20 69 6e 20 74 68 69 73 t locked in this
190ab 20 72 6f 75 74 69 6e 65 2c 20 62 75 74 20 69 74 routine, but it
190ac 20 69 73 20 6c 6f 63 6b 65 64 20 69 6e 0a 20 20 is locked in.
190ad 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ** sqlite3_backu
190ae 70 5f 73 74 65 70 28 29 2e 20 54 68 65 20 75 73 p_step(). The us
190af 65 72 20 69 73 20 72 65 71 75 69 72 65 64 20 74 er is required t
190b0 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 6e 6f o ensure that no
190b1 0a 20 20 2a 2a 20 6f 74 68 65 72 20 74 68 72 65 . ** other thre
190b2 61 64 20 61 63 63 65 73 73 65 73 20 74 68 65 20 ad accesses the
190b3 64 65 73 74 69 6e 61 74 69 6f 6e 20 68 61 6e 64 destination hand
190b4 6c 65 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 le for the durat
190b5 69 6f 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 ion. ** of the
190b6 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
190b7 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 . Any attempt t
190b8 6f 20 75 73 65 20 74 68 65 20 64 65 73 74 69 6e o use the destin
190b9 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 61 74 61 62 ation. ** datab
190ba 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 ase connection w
190bb 68 69 6c 65 20 61 20 62 61 63 6b 75 70 20 69 73 hile a backup is
190bc 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6d 61 79 in progress may
190bd 20 63 61 75 73 65 0a 20 20 2a 2a 20 61 20 6d 61 cause. ** a ma
190be 6c 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 20 64 lfunction or a d
190bf 65 61 64 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 eadlock.. */.
190c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
190c1 74 65 72 28 70 53 72 63 44 62 2d 3e 6d 75 74 65 ter(pSrcDb->mute
190c2 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 x);. sqlite3_mu
190c3 74 65 78 5f 65 6e 74 65 72 28 70 44 65 73 74 44 tex_enter(pDestD
190c4 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a 20 20 69 66 b->mutex);.. if
190c5 28 20 70 53 72 63 44 62 3d 3d 70 44 65 73 74 44 ( pSrcDb==pDestD
190c6 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 b ){. sqlite3
190c7 45 72 72 6f 72 28 0a 20 20 20 20 20 20 20 20 70 Error(. p
190c8 44 65 73 74 44 62 2c 20 53 51 4c 49 54 45 5f 45 DestDb, SQLITE_E
190c9 52 52 4f 52 2c 20 22 73 6f 75 72 63 65 20 61 6e RROR, "source an
190ca 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d 75 d destination mu
190cb 73 74 20 62 65 20 64 69 73 74 69 6e 63 74 22 0a st be distinct".
190cc 20 20 20 20 29 3b 0a 20 20 20 20 70 20 3d 20 30 );. p = 0
190cd 3b 0a 20 20 7d 65 6c 73 65 20 7b 0a 20 20 20 20 ;. }else {.
190ce 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 /* Allocate spac
190cf 65 20 66 6f 72 20 61 20 6e 65 77 20 73 71 6c 69 e for a new sqli
190d0 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 te3_backup objec
190d1 74 20 2a 2f 0a 20 20 20 20 70 20 3d 20 28 73 71 t */. p = (sq
190d2 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 73 lite3_backup *)s
190d3 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 qlite3_malloc(si
190d4 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 63 zeof(sqlite3_bac
190d5 6b 75 70 29 29 3b 0a 20 20 20 20 69 66 28 20 21 kup));. if( !
190d6 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 p ){. sqlit
190d7 65 33 45 72 72 6f 72 28 70 44 65 73 74 44 62 2c e3Error(pDestDb,
190d8 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 SQLITE_NOMEM, 0
190d9 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
190da 2f 2a 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 /* If the alloca
190db 74 69 6f 6e 20 73 75 63 63 65 65 64 65 64 2c 20 tion succeeded,
190dc 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6e 65 77 populate the new
190dd 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 69 66 object. */. if
190de 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 ( p ){. memse
190df 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 t(p, 0, sizeof(s
190e0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 29 29 3b qlite3_backup));
190e1 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20 66 . p->pSrc = f
190e2 69 6e 64 42 74 72 65 65 28 70 44 65 73 74 44 62 indBtree(pDestDb
190e3 2c 20 70 53 72 63 44 62 2c 20 7a 53 72 63 44 62 , pSrcDb, zSrcDb
190e4 29 3b 0a 20 20 20 20 70 2d 3e 70 44 65 73 74 20 );. p->pDest
190e5 3d 20 66 69 6e 64 42 74 72 65 65 28 70 44 65 73 = findBtree(pDes
190e6 74 44 62 2c 20 70 44 65 73 74 44 62 2c 20 7a 44 tDb, pDestDb, zD
190e7 65 73 74 44 62 29 3b 0a 20 20 20 20 70 2d 3e 70 estDb);. p->p
190e8 44 65 73 74 44 62 20 3d 20 70 44 65 73 74 44 62 DestDb = pDestDb
190e9 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 44 62 20 ;. p->pSrcDb
190ea 3d 20 70 53 72 63 44 62 3b 0a 20 20 20 20 70 2d = pSrcDb;. p-
190eb 3e 69 4e 65 78 74 20 3d 20 31 3b 0a 20 20 20 20 >iNext = 1;.
190ec 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20 3d 20 p->isAttached =
190ed 30 3b 0a 0a 20 20 20 20 69 66 28 20 30 3d 3d 70 0;.. if( 0==p
190ee 2d 3e 70 53 72 63 20 7c 7c 20 30 3d 3d 70 2d 3e ->pSrc || 0==p->
190ef 70 44 65 73 74 20 29 7b 0a 20 20 20 20 20 20 2f pDest ){. /
190f0 2a 20 4f 6e 65 20 28 6f 72 20 62 6f 74 68 29 20 * One (or both)
190f1 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61 74 of the named dat
190f2 61 62 61 73 65 73 20 64 69 64 20 6e 6f 74 20 65 abases did not e
190f3 78 69 73 74 2e 20 41 6e 20 65 72 72 6f 72 20 68 xist. An error h
190f4 61 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 65 as. ** alre
190f5 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e ady been written
190f6 20 69 6e 74 6f 20 74 68 65 20 70 44 65 73 74 44 into the pDestD
190f7 62 20 68 61 6e 64 6c 65 2e 20 41 6c 6c 20 74 68 b handle. All th
190f8 61 74 20 69 73 20 6c 65 66 74 0a 20 20 20 20 20 at is left.
190f9 20 2a 2a 20 74 6f 20 64 6f 20 68 65 72 65 20 69 ** to do here i
190fa 73 20 66 72 65 65 20 74 68 65 20 73 71 6c 69 74 s free the sqlit
190fb 65 33 5f 62 61 63 6b 75 70 20 73 74 72 75 63 74 e3_backup struct
190fc 75 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ure.. */.
190fd 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
190fe 28 70 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 30 (p);. p = 0
190ff 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
19100 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 ( p ){. p->pS
19101 72 63 2d 3e 6e 42 61 63 6b 75 70 2b 2b 3b 0a 20 rc->nBackup++;.
19102 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 }.. sqlite3_mu
19103 74 65 78 5f 6c 65 61 76 65 28 70 44 65 73 74 44 tex_leave(pDestD
19104 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c b->mutex);. sql
19105 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
19106 28 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b (pSrcDb->mutex);
19107 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
19108 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 72 /*.** Argument r
19109 63 20 69 73 20 61 6e 20 53 51 4c 69 74 65 20 65 c is an SQLite e
1910a 72 72 6f 72 20 63 6f 64 65 2e 20 52 65 74 75 72 rror code. Retur
1910b 6e 20 74 72 75 65 20 69 66 20 74 68 69 73 20 65 n true if this e
1910c 72 72 6f 72 20 69 73 20 0a 2a 2a 20 63 6f 6e 73 rror is .** cons
1910d 69 64 65 72 65 64 20 66 61 74 61 6c 20 69 66 20 idered fatal if
1910e 65 6e 63 6f 75 6e 74 65 72 65 64 20 64 75 72 69 encountered duri
1910f 6e 67 20 61 20 62 61 63 6b 75 70 20 6f 70 65 72 ng a backup oper
19110 61 74 69 6f 6e 2e 20 41 6c 6c 20 65 72 72 6f 72 ation. All error
19111 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 73 69 64 65 s.** are conside
19112 72 65 64 20 66 61 74 61 6c 20 65 78 63 65 70 74 red fatal except
19113 20 66 6f 72 20 53 51 4c 49 54 45 5f 42 55 53 59 for SQLITE_BUSY
19114 20 61 6e 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b and SQLITE_LOCK
19115 45 44 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ED..*/.static in
19116 74 20 69 73 46 61 74 61 6c 45 72 72 6f 72 28 69 t isFatalError(i
19117 6e 74 20 72 63 29 7b 0a 20 20 72 65 74 75 72 6e nt rc){. return
19118 20 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 (rc!=SQLITE_OK
19119 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 && rc!=SQLITE_BU
1911a 53 59 20 26 26 20 41 4c 57 41 59 53 28 72 63 21 SY && ALWAYS(rc!
1911b 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29 29 =SQLITE_LOCKED))
1911c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d ;.}../*.** Param
1911d 65 74 65 72 20 7a 53 72 63 44 61 74 61 20 70 6f eter zSrcData po
1911e 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65 72 ints to a buffer
1911f 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
19120 64 61 74 61 20 66 6f 72 20 0a 2a 2a 20 70 61 67 data for .** pag
19121 65 20 69 53 72 63 50 67 20 66 72 6f 6d 20 74 68 e iSrcPg from th
19122 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
19123 65 2e 20 43 6f 70 79 20 74 68 69 73 20 64 61 74 e. Copy this dat
19124 61 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 64 a into the .** d
19125 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
19126 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ase..*/.static i
19127 6e 74 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 nt backupOnePage
19128 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
19129 2a 70 2c 20 50 67 6e 6f 20 69 53 72 63 50 67 2c *p, Pgno iSrcPg,
1912a 20 63 6f 6e 73 74 20 75 38 20 2a 7a 53 72 63 44 const u8 *zSrcD
1912b 61 74 61 29 7b 0a 20 20 50 61 67 65 72 20 2a 20 ata){. Pager *
1912c 63 6f 6e 73 74 20 70 44 65 73 74 50 61 67 65 72 const pDestPager
1912d 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 = sqlite3BtreeP
1912e 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 3b 0a ager(p->pDest);.
1912f 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 53 72 63 const int nSrc
19130 50 67 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 Pgsz = sqlite3Bt
19131 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 reeGetPageSize(p
19132 2d 3e 70 53 72 63 29 3b 0a 20 20 69 6e 74 20 6e ->pSrc);. int n
19133 44 65 73 74 50 67 73 7a 20 3d 20 73 71 6c 69 74 DestPgsz = sqlit
19134 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 e3BtreeGetPageSi
19135 7a 65 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 ze(p->pDest);.
19136 63 6f 6e 73 74 20 69 6e 74 20 6e 43 6f 70 79 20 const int nCopy
19137 3d 20 4d 49 4e 28 6e 53 72 63 50 67 73 7a 2c 20 = MIN(nSrcPgsz,
19138 6e 44 65 73 74 50 67 73 7a 29 3b 0a 20 20 63 6f nDestPgsz);. co
19139 6e 73 74 20 69 36 34 20 69 45 6e 64 20 3d 20 28 nst i64 iEnd = (
1913a 69 36 34 29 69 53 72 63 50 67 2a 28 69 36 34 29 i64)iSrcPg*(i64)
1913b 6e 53 72 63 50 67 73 7a 3b 0a 0a 20 20 69 6e 74 nSrcPgsz;.. int
1913c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1913d 0a 20 20 69 36 34 20 69 4f 66 66 3b 0a 0a 20 20 . i64 iOff;..
1913e 61 73 73 65 72 74 28 20 70 2d 3e 62 44 65 73 74 assert( p->bDest
1913f 4c 6f 63 6b 65 64 20 29 3b 0a 20 20 61 73 73 65 Locked );. asse
19140 72 74 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f rt( !isFatalErro
19141 72 28 70 2d 3e 72 63 29 20 29 3b 0a 20 20 61 73 r(p->rc) );. as
19142 73 65 72 74 28 20 69 53 72 63 50 67 21 3d 50 45 sert( iSrcPg!=PE
19143 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
19144 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 3b p->pSrc->pBt) );
19145 0a 20 20 61 73 73 65 72 74 28 20 7a 53 72 63 44 . assert( zSrcD
19146 61 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 74 ata );.. /* Cat
19147 63 68 20 74 68 65 20 63 61 73 65 20 77 68 65 72 ch the case wher
19148 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f e the destinatio
19149 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 n is an in-memor
1914a 79 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 y database and t
1914b 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 73 69 7a he. ** page siz
1914c 65 73 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 es of the source
1914d 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e and destination
1914e 20 64 69 66 66 65 72 2e 20 0a 20 20 2a 2f 0a 20 differ. . */.
1914f 20 69 66 28 20 6e 53 72 63 50 67 73 7a 21 3d 6e if( nSrcPgsz!=n
19150 44 65 73 74 50 67 73 7a 20 26 26 20 73 71 6c 69 DestPgsz && sqli
19151 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28 te3PagerIsMemdb(
19152 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 sqlite3BtreePage
19153 72 28 70 2d 3e 70 44 65 73 74 29 29 20 29 7b 0a r(p->pDest)) ){.
19154 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
19155 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 READONLY;. }..
19156 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 72 75 /* This loop ru
19157 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 ns once for each
19158 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 destination pag
19159 65 20 73 70 61 6e 6e 65 64 20 62 79 20 74 68 65 e spanned by the
1915a 20 73 6f 75 72 63 65 20 0a 20 20 2a 2a 20 70 61 source . ** pa
1915b 67 65 2e 20 46 6f 72 20 65 61 63 68 20 69 74 65 ge. For each ite
1915c 72 61 74 69 6f 6e 2c 20 76 61 72 69 61 62 6c 65 ration, variable
1915d 20 69 4f 66 66 20 69 73 20 73 65 74 20 74 6f 20 iOff is set to
1915e 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 0a the byte offset.
1915f 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 73 74 ** of the dest
19160 69 6e 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20 ination page..
19161 2a 2f 0a 20 20 66 6f 72 28 69 4f 66 66 3d 69 45 */. for(iOff=iE
19162 6e 64 2d 28 69 36 34 29 6e 53 72 63 50 67 73 7a nd-(i64)nSrcPgsz
19163 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ; rc==SQLITE_OK
19164 26 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20 69 4f && iOff<iEnd; iO
19165 66 66 2b 3d 6e 44 65 73 74 50 67 73 7a 29 7b 0a ff+=nDestPgsz){.
19166 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 65 73 DbPage *pDes
19167 74 50 67 20 3d 20 30 3b 0a 20 20 20 20 50 67 6e tPg = 0;. Pgn
19168 6f 20 69 44 65 73 74 20 3d 20 28 50 67 6e 6f 29 o iDest = (Pgno)
19169 28 69 4f 66 66 2f 6e 44 65 73 74 50 67 73 7a 29 (iOff/nDestPgsz)
1916a 2b 31 3b 0a 20 20 20 20 69 66 28 20 69 44 65 73 +1;. if( iDes
1916b 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f t==PENDING_BYTE_
1916c 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 PAGE(p->pDest->p
1916d 42 74 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a Bt) ) continue;.
1916e 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f if( SQLITE_O
1916f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 K==(rc = sqlite3
19170 50 61 67 65 72 47 65 74 28 70 44 65 73 74 50 61 PagerGet(pDestPa
19171 67 65 72 2c 20 69 44 65 73 74 2c 20 26 70 44 65 ger, iDest, &pDe
19172 73 74 50 67 29 29 0a 20 20 20 20 20 26 26 20 53 stPg)). && S
19173 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 QLITE_OK==(rc =
19174 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
19175 65 28 70 44 65 73 74 50 67 29 29 0a 20 20 20 20 e(pDestPg)).
19176 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75 ){. const u
19177 38 20 2a 7a 49 6e 20 3d 20 26 7a 53 72 63 44 61 8 *zIn = &zSrcDa
19178 74 61 5b 69 4f 66 66 25 6e 53 72 63 50 67 73 7a ta[iOff%nSrcPgsz
19179 5d 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 44 65 ];. u8 *zDe
1917a 73 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 stData = sqlite3
1917b 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 65 PagerGetData(pDe
1917c 73 74 50 67 29 3b 0a 20 20 20 20 20 20 75 38 20 stPg);. u8
1917d 2a 7a 4f 75 74 20 3d 20 26 7a 44 65 73 74 44 61 *zOut = &zDestDa
1917e 74 61 5b 69 4f 66 66 25 6e 44 65 73 74 50 67 73 ta[iOff%nDestPgs
1917f 7a 5d 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f z];.. /* Co
19180 70 79 20 74 68 65 20 64 61 74 61 20 66 72 6f 6d py the data from
19181 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 the source page
19182 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e into the destin
19183 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20 20 20 ation page..
19184 20 20 2a 2a 20 54 68 65 6e 20 63 6c 65 61 72 20 ** Then clear
19185 74 68 65 20 42 74 72 65 65 20 6c 61 79 65 72 20 the Btree layer
19186 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 66 MemPage.isInit f
19187 6c 61 67 2e 20 42 6f 74 68 20 74 68 69 73 20 6d lag. Both this m
19188 6f 64 75 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 odule. ** a
19189 6e 64 20 74 68 65 20 70 61 67 65 72 20 63 6f 64 nd the pager cod
1918a 65 20 75 73 65 20 74 68 69 73 20 74 72 69 63 6b e use this trick
1918b 20 28 63 6c 65 61 72 69 6e 67 20 74 68 65 20 66 (clearing the f
1918c 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 20 20 irst byte.
1918d 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 27 ** of the page '
1918e 65 78 74 72 61 27 20 73 70 61 63 65 20 74 6f 20 extra' space to
1918f 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 42 invalidate the B
19190 74 72 65 65 20 6c 61 79 65 72 73 0a 20 20 20 20 tree layers.
19191 20 20 2a 2a 20 63 61 63 68 65 64 20 70 61 72 73 ** cached pars
19192 65 20 6f 66 20 74 68 65 20 70 61 67 65 29 2e 20 e of the page).
19193 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 69 MemPage.isInit i
19194 73 20 6d 61 72 6b 65 64 20 0a 20 20 20 20 20 20 s marked .
19195 2a 2a 20 22 4d 55 53 54 20 42 45 20 46 49 52 53 ** "MUST BE FIRS
19196 54 22 20 66 6f 72 20 74 68 69 73 20 70 75 72 70 T" for this purp
19197 6f 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ose.. */.
19198 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c memcpy(zOut,
19199 20 7a 49 6e 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 zIn, nCopy);.
1919a 20 20 20 20 28 28 75 38 20 2a 29 73 71 6c 69 74 ((u8 *)sqlit
1919b 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 e3PagerGetExtra(
1919c 70 44 65 73 74 50 67 29 29 5b 30 5d 20 3d 20 30 pDestPg))[0] = 0
1919d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
1919e 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 te3PagerUnref(pD
1919f 65 73 74 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 estPg);. }.. r
191a0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
191a1 2a 2a 20 49 66 20 70 46 69 6c 65 20 69 73 20 63 ** If pFile is c
191a2 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 urrently larger
191a3 74 68 61 6e 20 69 53 69 7a 65 20 62 79 74 65 73 than iSize bytes
191a4 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20 , then truncate
191a5 69 74 20 74 6f 0a 2a 2a 20 65 78 61 63 74 6c 79 it to.** exactly
191a6 20 69 53 69 7a 65 20 62 79 74 65 73 2e 20 49 66 iSize bytes. If
191a7 20 70 46 69 6c 65 20 69 73 20 6e 6f 74 20 6c 61 pFile is not la
191a8 72 67 65 72 20 74 68 61 6e 20 69 53 69 7a 65 20 rger than iSize
191a9 62 79 74 65 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 bytes, then.** t
191aa 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
191ab 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 52 a no-op..**.** R
191ac 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
191ad 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 if everything is
191ae 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 successful, or
191af 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 an SQLite error
191b0 0a 2a 2a 20 63 6f 64 65 20 69 66 20 61 6e 20 65 .** code if an e
191b1 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a rror occurs..*/.
191b2 73 74 61 74 69 63 20 69 6e 74 20 62 61 63 6b 75 static int backu
191b3 70 54 72 75 6e 63 61 74 65 46 69 6c 65 28 73 71 pTruncateFile(sq
191b4 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c lite3_file *pFil
191b5 65 2c 20 69 36 34 20 69 53 69 7a 65 29 7b 0a 20 e, i64 iSize){.
191b6 20 69 36 34 20 69 43 75 72 72 65 6e 74 3b 0a 20 i64 iCurrent;.
191b7 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 int rc = sqlite
191b8 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 46 69 6c 3OsFileSize(pFil
191b9 65 2c 20 26 69 43 75 72 72 65 6e 74 29 3b 0a 20 e, &iCurrent);.
191ba 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
191bb 4f 4b 20 26 26 20 69 43 75 72 72 65 6e 74 3e 69 OK && iCurrent>i
191bc 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63 20 3d Size ){. rc =
191bd 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 sqlite3OsTrunca
191be 74 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65 29 te(pFile, iSize)
191bf 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
191c0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 c;.}../*.** Regi
191c1 73 74 65 72 20 74 68 69 73 20 62 61 63 6b 75 70 ster this backup
191c2 20 6f 62 6a 65 63 74 20 77 69 74 68 20 74 68 65 object with the
191c3 20 61 73 73 6f 63 69 61 74 65 64 20 73 6f 75 72 associated sour
191c4 63 65 20 70 61 67 65 72 20 66 6f 72 0a 2a 2a 20 ce pager for.**
191c5 63 61 6c 6c 62 61 63 6b 73 20 77 68 65 6e 20 70 callbacks when p
191c6 61 67 65 73 20 61 72 65 20 63 68 61 6e 67 65 64 ages are changed
191c7 20 6f 72 20 74 68 65 20 63 61 63 68 65 20 69 6e or the cache in
191c8 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a 73 74 validated..*/.st
191c9 61 74 69 63 20 76 6f 69 64 20 61 74 74 61 63 68 atic void attach
191ca 42 61 63 6b 75 70 4f 62 6a 65 63 74 28 73 71 6c BackupObject(sql
191cb 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b ite3_backup *p){
191cc 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 . sqlite3_backu
191cd 70 20 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72 74 p **pp;. assert
191ce 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
191cf 6c 64 73 4d 75 74 65 78 28 70 2d 3e 70 53 72 63 ldsMutex(p->pSrc
191d0 29 20 29 3b 0a 20 20 70 70 20 3d 20 73 71 6c 69 ) );. pp = sqli
191d1 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 74 te3PagerBackupPt
191d2 72 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 r(sqlite3BtreePa
191d3 67 65 72 28 70 2d 3e 70 53 72 63 29 29 3b 0a 20 ger(p->pSrc));.
191d4 20 70 2d 3e 70 4e 65 78 74 20 3d 20 2a 70 70 3b p->pNext = *pp;
191d5 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 70 2d . *pp = p;. p-
191d6 3e 69 73 41 74 74 61 63 68 65 64 20 3d 20 31 3b >isAttached = 1;
191d7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 6e .}../*.** Copy n
191d8 50 61 67 65 20 70 61 67 65 73 20 66 72 6f 6d 20 Page pages from
191d9 74 68 65 20 73 6f 75 72 63 65 20 62 2d 74 72 65 the source b-tre
191da 65 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 e to the destina
191db 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
191dc 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
191dd 62 61 63 6b 75 70 5f 73 74 65 70 28 73 71 6c 69 backup_step(sqli
191de 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20 69 te3_backup *p, i
191df 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e 74 nt nPage){. int
191e0 20 72 63 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f rc;.. sqlite3_
191e1 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 mutex_enter(p->p
191e2 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 SrcDb->mutex);.
191e3 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
191e4 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69 er(p->pSrc);. i
191e5 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b f( p->pDestDb ){
191e6 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
191e7 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44 65 73 ex_enter(p->pDes
191e8 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d tDb->mutex);. }
191e9 0a 0a 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b 0a .. rc = p->rc;.
191ea 20 20 69 66 28 20 21 69 73 46 61 74 61 6c 45 72 if( !isFatalEr
191eb 72 6f 72 28 72 63 29 20 29 7b 0a 20 20 20 20 50 ror(rc) ){. P
191ec 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 53 72 ager * const pSr
191ed 63 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 cPager = sqlite3
191ee 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53 BtreePager(p->pS
191ef 72 63 29 3b 20 20 20 20 20 2f 2a 20 53 6f 75 72 rc); /* Sour
191f0 63 65 20 70 61 67 65 72 20 2a 2f 0a 20 20 20 20 ce pager */.
191f1 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 44 Pager * const pD
191f2 65 73 74 50 61 67 65 72 20 3d 20 73 71 6c 69 74 estPager = sqlit
191f3 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e e3BtreePager(p->
191f4 70 44 65 73 74 29 3b 20 20 20 2f 2a 20 44 65 73 pDest); /* Des
191f5 74 20 70 61 67 65 72 20 2a 2f 0a 20 20 20 20 69 t pager */. i
191f6 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 nt ii;
191f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
191f8 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 /* Iterator va
191f9 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e riable */. in
191fa 74 20 6e 53 72 63 50 61 67 65 20 3d 20 2d 31 3b t nSrcPage = -1;
191fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
191fc 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 6f 75 72 /* Size of sour
191fd 63 65 20 64 62 20 69 6e 20 70 61 67 65 73 20 2a ce db in pages *
191fe 2f 0a 20 20 20 20 69 6e 74 20 62 43 6c 6f 73 65 /. int bClose
191ff 54 72 61 6e 73 20 3d 20 30 3b 20 20 20 20 20 20 Trans = 0;
19200 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
19201 20 69 66 20 73 72 63 20 64 62 20 72 65 71 75 69 if src db requi
19202 72 65 73 20 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f res unlocking */
19203 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 .. /* If the
19204 73 6f 75 72 63 65 20 70 61 67 65 72 20 69 73 20 source pager is
19205 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 20 77 currently in a w
19206 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
19207 2c 20 72 65 74 75 72 6e 0a 20 20 20 20 2a 2a 20 , return. **
19208 53 51 4c 49 54 45 5f 42 55 53 59 20 69 6d 6d 65 SQLITE_BUSY imme
19209 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2f 0a diately.. */.
1920a 20 20 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 if( p->pDest
1920b 44 62 20 26 26 20 70 2d 3e 70 53 72 63 2d 3e 70 Db && p->pSrc->p
1920c 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
1920d 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 n==TRANS_WRITE )
1920e 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
1920f 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 ITE_BUSY;. }e
19210 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
19211 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
19212 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 .. /* Lock th
19213 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 e destination da
19214 74 61 62 61 73 65 2c 20 69 66 20 69 74 20 69 73 tabase, if it is
19215 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 61 6c 72 65 not locked alre
19216 61 64 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 ady. */. if(
19217 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 26 26 SQLITE_OK==rc &&
19218 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 3d p->bDestLocked=
19219 3d 30 0a 20 20 20 20 20 26 26 20 53 51 4c 49 54 =0. && SQLIT
1921a 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK==(rc = sqli
1921b 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 te3BtreeBeginTra
1921c 6e 73 28 70 2d 3e 70 44 65 73 74 2c 20 32 29 29 ns(p->pDest, 2))
1921d 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 70 . ){. p
1921e 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 20 3d 20 ->bDestLocked =
1921f 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 1;. sqlite3
19220 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 2d 3e BtreeGetMeta(p->
19221 70 44 65 73 74 2c 20 42 54 52 45 45 5f 53 43 48 pDest, BTREE_SCH
19222 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 26 70 2d EMA_VERSION, &p-
19223 3e 69 44 65 73 74 53 63 68 65 6d 61 29 3b 0a 20 >iDestSchema);.
19224 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
19225 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e there is no open
19226 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f read-transactio
19227 6e 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 n on the source
19228 64 61 74 61 62 61 73 65 2c 20 6f 70 65 6e 0a 20 database, open.
19229 20 20 20 2a 2a 20 6f 6e 65 20 6e 6f 77 2e 20 49 ** one now. I
1922a 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 f a transaction
1922b 69 73 20 6f 70 65 6e 65 64 20 68 65 72 65 2c 20 is opened here,
1922c 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 62 65 20 then it will be
1922d 63 6c 6f 73 65 64 0a 20 20 20 20 2a 2a 20 62 65 closed. ** be
1922e 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 fore this functi
1922f 6f 6e 20 65 78 69 74 73 2e 0a 20 20 20 20 2a 2f on exits.. */
19230 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
19231 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 73 71 6c ITE_OK && 0==sql
19232 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 ite3BtreeIsInRea
19233 64 54 72 61 6e 73 28 70 2d 3e 70 53 72 63 29 20 dTrans(p->pSrc)
19234 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
19235 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 lite3BtreeBeginT
19236 72 61 6e 73 28 70 2d 3e 70 53 72 63 2c 20 30 29 rans(p->pSrc, 0)
19237 3b 0a 20 20 20 20 20 20 62 43 6c 6f 73 65 54 72 ;. bCloseTr
19238 61 6e 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 ans = 1;. }.
19239 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 74 68 61 . /* Now tha
1923a 74 20 74 68 65 72 65 20 69 73 20 61 20 72 65 61 t there is a rea
1923b 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 6f d-lock on the so
1923c 75 72 63 65 20 64 61 74 61 62 61 73 65 2c 20 71 urce database, q
1923d 75 65 72 79 20 74 68 65 0a 20 20 20 20 2a 2a 20 uery the. **
1923e 73 6f 75 72 63 65 20 70 61 67 65 72 20 66 6f 72 source pager for
1923f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
19240 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 ages in the data
19241 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 base.. */.
19242 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
19243 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d OK ){. rc =
19244 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
19245 65 63 6f 75 6e 74 28 70 53 72 63 50 61 67 65 72 ecount(pSrcPager
19246 2c 20 26 6e 53 72 63 50 61 67 65 29 3b 0a 20 20 , &nSrcPage);.
19247 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 }. for(ii=0
19248 3b 20 28 6e 50 61 67 65 3c 30 20 7c 7c 20 69 69 ; (nPage<0 || ii
19249 3c 6e 50 61 67 65 29 20 26 26 20 70 2d 3e 69 4e <nPage) && p->iN
1924a 65 78 74 3c 3d 28 50 67 6e 6f 29 6e 53 72 63 50 ext<=(Pgno)nSrcP
1924b 61 67 65 20 26 26 20 21 72 63 3b 20 69 69 2b 2b age && !rc; ii++
1924c 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 ){. const P
1924d 67 6e 6f 20 69 53 72 63 50 67 20 3d 20 70 2d 3e gno iSrcPg = p->
1924e 69 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 iNext;
1924f 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 /* Source
19250 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a page number */.
19251 20 20 20 20 20 20 69 66 28 20 69 53 72 63 50 67 if( iSrcPg
19252 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 !=PENDING_BYTE_P
19253 41 47 45 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 AGE(p->pSrc->pBt
19254 29 20 29 7b 0a 20 20 20 20 20 20 20 20 44 62 50 ) ){. DbP
19255 61 67 65 20 2a 70 53 72 63 50 67 3b 20 20 20 20 age *pSrcPg;
19256 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19257 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 /* Sour
19258 63 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 2a ce page object *
19259 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 /. rc = s
1925a 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 qlite3PagerGet(p
1925b 53 72 63 50 61 67 65 72 2c 20 69 53 72 63 50 67 SrcPager, iSrcPg
1925c 2c 20 26 70 53 72 63 50 67 29 3b 0a 20 20 20 20 , &pSrcPg);.
1925d 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1925e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1925f 20 20 20 72 63 20 3d 20 62 61 63 6b 75 70 4f 6e rc = backupOn
19260 65 50 61 67 65 28 70 2c 20 69 53 72 63 50 67 2c ePage(p, iSrcPg,
19261 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
19262 44 61 74 61 28 70 53 72 63 50 67 29 29 3b 0a 20 Data(pSrcPg));.
19263 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
19264 50 61 67 65 72 55 6e 72 65 66 28 70 53 72 63 50 PagerUnref(pSrcP
19265 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 g);. }.
19266 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 69 }. p->i
19267 4e 65 78 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 Next++;. }.
19268 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
19269 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e _OK ){. p->
1926a 6e 50 61 67 65 63 6f 75 6e 74 20 3d 20 6e 53 72 nPagecount = nSr
1926b 63 50 61 67 65 3b 0a 20 20 20 20 20 20 70 2d 3e cPage;. p->
1926c 6e 52 65 6d 61 69 6e 69 6e 67 20 3d 20 6e 53 72 nRemaining = nSr
1926d 63 50 61 67 65 2b 31 2d 70 2d 3e 69 4e 65 78 74 cPage+1-p->iNext
1926e 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 ;. if( p->i
1926f 4e 65 78 74 3e 28 50 67 6e 6f 29 6e 53 72 63 50 Next>(Pgno)nSrcP
19270 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 age ){. r
19271 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b c = SQLITE_DONE;
19272 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
19273 20 21 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20 !p->isAttached
19274 29 7b 0a 20 20 20 20 20 20 20 20 61 74 74 61 63 ){. attac
19275 68 42 61 63 6b 75 70 4f 62 6a 65 63 74 28 70 29 hBackupObject(p)
19276 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
19277 20 20 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65 . /* Update
19278 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 the schema vers
19279 69 6f 6e 20 66 69 65 6c 64 20 69 6e 20 74 68 65 ion field in the
1927a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
1927b 61 62 61 73 65 2e 20 54 68 69 73 0a 20 20 20 20 abase. This.
1927c 2a 2a 20 69 73 20 74 6f 20 6d 61 6b 65 20 73 75 ** is to make su
1927d 72 65 20 74 68 61 74 20 74 68 65 20 73 63 68 65 re that the sche
1927e 6d 61 2d 76 65 72 73 69 6f 6e 20 72 65 61 6c 6c ma-version reall
1927f 79 20 64 6f 65 73 20 63 68 61 6e 67 65 20 69 6e y does change in
19280 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 73 65 . ** the case
19281 20 77 68 65 72 65 20 74 68 65 20 73 6f 75 72 63 where the sourc
19282 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f e and destinatio
19283 6e 20 64 61 74 61 62 61 73 65 73 20 68 61 76 65 n databases have
19284 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 61 6d 65 the. ** same
19285 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e schema version.
19286 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
19287 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc==SQLITE_DONE
19288 0a 20 20 20 20 20 26 26 20 28 72 63 20 3d 20 73 . && (rc = s
19289 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 qlite3BtreeUpdat
1928a 65 4d 65 74 61 28 70 2d 3e 70 44 65 73 74 2c 31 eMeta(p->pDest,1
1928b 2c 70 2d 3e 69 44 65 73 74 53 63 68 65 6d 61 2b ,p->iDestSchema+
1928c 31 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 1))==SQLITE_OK.
1928d 20 20 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 ){. cons
1928e 74 20 69 6e 74 20 6e 53 72 63 50 61 67 65 73 69 t int nSrcPagesi
1928f 7a 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 ze = sqlite3Btre
19290 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e eGetPageSize(p->
19291 70 53 72 63 29 3b 0a 20 20 20 20 20 20 63 6f 6e pSrc);. con
19292 73 74 20 69 6e 74 20 6e 44 65 73 74 50 61 67 65 st int nDestPage
19293 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 42 74 size = sqlite3Bt
19294 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 reeGetPageSize(p
19295 2d 3e 70 44 65 73 74 29 3b 0a 20 20 20 20 20 20 ->pDest);.
19296 69 6e 74 20 6e 44 65 73 74 54 72 75 6e 63 61 74 int nDestTruncat
19297 65 3b 0a 20 20 0a 20 20 20 20 20 20 69 66 28 20 e;. . if(
19298 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 p->pDestDb ){.
19299 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 sqlite3Res
1929a 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 etInternalSchema
1929b 28 70 2d 3e 70 44 65 73 74 44 62 2c 20 30 29 3b (p->pDestDb, 0);
1929c 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
1929d 2f 2a 20 53 65 74 20 6e 44 65 73 74 54 72 75 6e /* Set nDestTrun
1929e 63 61 74 65 20 74 6f 20 74 68 65 20 66 69 6e 61 cate to the fina
1929f 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 l number of page
192a0 73 20 69 6e 20 74 68 65 20 64 65 73 74 69 6e 61 s in the destina
192a1 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 64 61 tion. ** da
192a2 74 61 62 61 73 65 2e 20 54 68 65 20 63 6f 6d 70 tabase. The comp
192a3 6c 69 63 61 74 69 6f 6e 20 68 65 72 65 20 69 73 lication here is
192a4 20 74 68 61 74 20 74 68 65 20 64 65 73 74 69 6e that the destin
192a5 61 74 69 6f 6e 20 70 61 67 65 0a 20 20 20 20 20 ation page.
192a6 20 2a 2a 20 73 69 7a 65 20 6d 61 79 20 62 65 20 ** size may be
192a7 64 69 66 66 65 72 65 6e 74 20 74 6f 20 74 68 65 different to the
192a8 20 73 6f 75 72 63 65 20 70 61 67 65 20 73 69 7a source page siz
192a9 65 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 e. . **.
192aa 20 20 20 2a 2a 20 49 66 20 74 68 65 20 73 6f 75 ** If the sou
192ab 72 63 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 rce page size is
192ac 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 smaller than th
192ad 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 e destination pa
192ae 67 65 20 73 69 7a 65 2c 20 0a 20 20 20 20 20 20 ge size, .
192af 2a 2a 20 72 6f 75 6e 64 20 75 70 2e 20 49 6e 20 ** round up. In
192b0 74 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61 this case the ca
192b1 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 54 ll to sqlite3OsT
192b2 72 75 6e 63 61 74 65 28 29 20 62 65 6c 6f 77 20 runcate() below
192b3 77 69 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 66 69 will. ** fi
192b4 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 x the size of th
192b5 65 20 66 69 6c 65 2e 20 48 6f 77 65 76 65 72 20 e file. However
192b6 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 it is important
192b7 74 6f 20 63 61 6c 6c 0a 20 20 20 20 20 20 2a 2a to call. **
192b8 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 sqlite3PagerTru
192b9 6e 63 61 74 65 49 6d 61 67 65 28 29 20 68 65 72 ncateImage() her
192ba 65 20 73 6f 20 74 68 61 74 20 61 6e 79 20 70 61 e so that any pa
192bb 67 65 73 20 69 6e 20 74 68 65 20 0a 20 20 20 20 ges in the .
192bc 20 20 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e ** destination
192bd 20 66 69 6c 65 20 74 68 61 74 20 6c 69 65 20 62 file that lie b
192be 65 79 6f 6e 64 20 74 68 65 20 6e 44 65 73 74 54 eyond the nDestT
192bf 72 75 6e 63 61 74 65 20 70 61 67 65 20 6d 61 72 runcate page mar
192c0 6b 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 6a k are. ** j
192c1 6f 75 72 6e 61 6c 6c 65 64 20 62 79 20 50 61 67 ournalled by Pag
192c2 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 erCommitPhaseOne
192c3 28 29 20 62 65 66 6f 72 65 20 74 68 65 79 20 61 () before they a
192c4 72 65 20 64 65 73 74 72 6f 79 65 64 0a 20 20 20 re destroyed.
192c5 20 20 20 2a 2a 20 62 79 20 74 68 65 20 66 69 6c ** by the fil
192c6 65 20 74 72 75 6e 63 61 74 69 6f 6e 2e 0a 20 20 e truncation..
192c7 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
192c8 20 6e 53 72 63 50 61 67 65 73 69 7a 65 3c 6e 44 nSrcPagesize<nD
192c9 65 73 74 50 61 67 65 73 69 7a 65 20 29 7b 0a 20 estPagesize ){.
192ca 20 20 20 20 20 20 20 69 6e 74 20 72 61 74 69 6f int ratio
192cb 20 3d 20 6e 44 65 73 74 50 61 67 65 73 69 7a 65 = nDestPagesize
192cc 2f 6e 53 72 63 50 61 67 65 73 69 7a 65 3b 0a 20 /nSrcPagesize;.
192cd 20 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 6e nDestTrun
192ce 63 61 74 65 20 3d 20 28 6e 53 72 63 50 61 67 65 cate = (nSrcPage
192cf 2b 72 61 74 69 6f 2d 31 29 2f 72 61 74 69 6f 3b +ratio-1)/ratio;
192d0 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 44 65 . if( nDe
192d1 73 74 54 72 75 6e 63 61 74 65 3d 3d 28 69 6e 74 stTruncate==(int
192d2 29 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 )PENDING_BYTE_PA
192d3 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74 GE(p->pDest->pBt
192d4 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e ) ){. n
192d5 44 65 73 74 54 72 75 6e 63 61 74 65 2d 2d 3b 0a DestTruncate--;.
192d6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
192d7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e }else{. n
192d8 44 65 73 74 54 72 75 6e 63 61 74 65 20 3d 20 6e DestTruncate = n
192d9 53 72 63 50 61 67 65 20 2a 20 28 6e 53 72 63 50 SrcPage * (nSrcP
192da 61 67 65 73 69 7a 65 2f 6e 44 65 73 74 50 61 67 agesize/nDestPag
192db 65 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a esize);. }.
192dc 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
192dd 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 erTruncateImage(
192de 70 44 65 73 74 50 61 67 65 72 2c 20 6e 44 65 73 pDestPager, nDes
192df 74 54 72 75 6e 63 61 74 65 29 3b 0a 0a 20 20 20 tTruncate);..
192e0 20 20 20 69 66 28 20 6e 53 72 63 50 61 67 65 73 if( nSrcPages
192e1 69 7a 65 3c 6e 44 65 73 74 50 61 67 65 73 69 7a ize<nDestPagesiz
192e2 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 e ){. /*
192e3 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 If the source pa
192e4 67 65 2d 73 69 7a 65 20 69 73 20 73 6d 61 6c 6c ge-size is small
192e5 65 72 20 74 68 61 6e 20 74 68 65 20 64 65 73 74 er than the dest
192e6 69 6e 61 74 69 6f 6e 20 70 61 67 65 2d 73 69 7a ination page-siz
192e7 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 77 e,. ** tw
192e8 6f 20 65 78 74 72 61 20 74 68 69 6e 67 73 20 6d o extra things m
192e9 61 79 20 6e 65 65 64 20 74 6f 20 68 61 70 70 65 ay need to happe
192ea 6e 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 n:. **.
192eb 20 20 20 20 20 20 2a 2a 20 20 20 2a 20 54 68 65 ** * The
192ec 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d 61 79 destination may
192ed 20 6e 65 65 64 20 74 6f 20 62 65 20 74 72 75 6e need to be trun
192ee 63 61 74 65 64 2c 20 61 6e 64 0a 20 20 20 20 20 cated, and.
192ef 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a **. **
192f0 20 20 20 2a 20 44 61 74 61 20 73 74 6f 72 65 64 * Data stored
192f1 20 6f 6e 20 74 68 65 20 70 61 67 65 73 20 69 6d on the pages im
192f2 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 mediately follow
192f3 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 20 ing the .
192f4 20 2a 2a 20 20 20 20 20 70 65 6e 64 69 6e 67 2d ** pending-
192f5 62 79 74 65 20 70 61 67 65 20 69 6e 20 74 68 65 byte page in the
192f6 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 source database
192f7 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 0a may need to be.
192f8 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 63 ** c
192f9 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 64 opied into the d
192fa 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
192fb 61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ase.. */.
192fc 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 69 36 const i6
192fd 34 20 69 53 69 7a 65 20 3d 20 28 69 36 34 29 6e 4 iSize = (i64)n
192fe 53 72 63 50 61 67 65 73 69 7a 65 20 2a 20 28 69 SrcPagesize * (i
192ff 36 34 29 6e 53 72 63 50 61 67 65 3b 0a 20 20 20 64)nSrcPage;.
19300 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c sqlite3_fil
19301 65 20 2a 20 63 6f 6e 73 74 20 70 46 69 6c 65 20 e * const pFile
19302 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 = sqlite3PagerFi
19303 6c 65 28 70 44 65 73 74 50 61 67 65 72 29 3b 0a le(pDestPager);.
19304 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
19305 20 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 20 pFile );.
19306 20 20 61 73 73 65 72 74 28 20 28 69 36 34 29 6e assert( (i64)n
19307 44 65 73 74 54 72 75 6e 63 61 74 65 2a 28 69 36 DestTruncate*(i6
19308 34 29 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20 4)nDestPagesize
19309 3e 3d 20 69 53 69 7a 65 20 7c 7c 20 28 0a 20 20 >= iSize || (.
1930a 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 65 73 nDes
1930b 74 54 72 75 6e 63 61 74 65 3d 3d 28 69 6e 74 29 tTruncate==(int)
1930c 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 (PENDING_BYTE_PA
1930d 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74 GE(p->pDest->pBt
1930e 29 2d 31 29 0a 20 20 20 20 20 20 20 20 20 20 20 )-1).
1930f 26 26 20 69 53 69 7a 65 3e 3d 50 45 4e 44 49 4e && iSize>=PENDIN
19310 47 5f 42 59 54 45 20 26 26 20 69 53 69 7a 65 3c G_BYTE && iSize<
19311 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 6e 44 =PENDING_BYTE+nD
19312 65 73 74 50 61 67 65 73 69 7a 65 0a 20 20 20 20 estPagesize.
19313 20 20 20 20 29 29 3b 0a 20 20 20 20 20 20 20 20 ));.
19314 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 if( SQLITE_OK==(
19315 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
19316 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 rCommitPhaseOne(
19317 70 44 65 73 74 50 61 67 65 72 2c 20 30 2c 20 31 pDestPager, 0, 1
19318 29 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 53 )). && S
19319 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 QLITE_OK==(rc =
1931a 62 61 63 6b 75 70 54 72 75 6e 63 61 74 65 46 69 backupTruncateFi
1931b 6c 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65 29 le(pFile, iSize)
1931c 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 53 51 ). && SQ
1931d 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 LITE_OK==(rc = s
1931e 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 qlite3PagerSync(
1931f 70 44 65 73 74 50 61 67 65 72 29 29 0a 20 20 20 pDestPager)).
19320 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 ){.
19321 20 20 69 36 34 20 69 4f 66 66 3b 0a 20 20 20 20 i64 iOff;.
19322 20 20 20 20 20 20 69 36 34 20 69 45 6e 64 20 3d i64 iEnd =
19323 20 4d 49 4e 28 50 45 4e 44 49 4e 47 5f 42 59 54 MIN(PENDING_BYT
19324 45 20 2b 20 6e 44 65 73 74 50 61 67 65 73 69 7a E + nDestPagesiz
19325 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 e, iSize);.
19326 20 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20 20 for(.
19327 20 20 20 20 20 20 69 4f 66 66 3d 50 45 4e 44 49 iOff=PENDI
19328 4e 47 5f 42 59 54 45 2b 6e 53 72 63 50 61 67 65 NG_BYTE+nSrcPage
19329 73 69 7a 65 3b 20 0a 20 20 20 20 20 20 20 20 20 size; .
1932a 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b rc==SQLITE_OK
1932b 20 26 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20 0a && iOff<iEnd; .
1932c 20 20 20 20 20 20 20 20 20 20 20 20 69 4f 66 66 iOff
1932d 2b 3d 6e 53 72 63 50 61 67 65 73 69 7a 65 0a 20 +=nSrcPagesize.
1932e 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 ){.
1932f 20 20 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 PgHdr *p
19330 53 72 63 50 67 20 3d 20 30 3b 0a 20 20 20 20 20 SrcPg = 0;.
19331 20 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e const Pgn
19332 6f 20 69 53 72 63 50 67 20 3d 20 28 50 67 6e 6f o iSrcPg = (Pgno
19333 29 28 28 69 4f 66 66 2f 6e 53 72 63 50 61 67 65 )((iOff/nSrcPage
19334 73 69 7a 65 29 2b 31 29 3b 0a 20 20 20 20 20 20 size)+1);.
19335 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
19336 65 33 50 61 67 65 72 47 65 74 28 70 53 72 63 50 e3PagerGet(pSrcP
19337 61 67 65 72 2c 20 69 53 72 63 50 67 2c 20 26 70 ager, iSrcPg, &p
19338 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 SrcPg);.
19339 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1933a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1933b 20 20 20 20 20 20 20 75 38 20 2a 7a 44 61 74 61 u8 *zData
1933c 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 = sqlite3PagerG
1933d 65 74 44 61 74 61 28 70 53 72 63 50 67 29 3b 0a etData(pSrcPg);.
1933e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63 rc
1933f 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
19340 65 28 70 46 69 6c 65 2c 20 7a 44 61 74 61 2c 20 e(pFile, zData,
19341 6e 53 72 63 50 61 67 65 73 69 7a 65 2c 20 69 4f nSrcPagesize, iO
19342 66 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ff);.
19343 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 }. s
19344 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
19345 28 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 (pSrcPg);.
19346 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
19347 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
19348 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
19349 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 3PagerCommitPhas
1934a 65 4f 6e 65 28 70 44 65 73 74 50 61 67 65 72 2c eOne(pDestPager,
1934b 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0, 0);. }.
1934c 20 20 0a 20 20 20 20 20 20 2f 2a 20 46 69 6e 69 . /* Fini
1934d 73 68 20 63 6f 6d 6d 69 74 74 69 6e 67 20 74 68 sh committing th
1934e 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f e transaction to
1934f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
19350 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a 20 20 database. */.
19351 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f if( SQLITE_O
19352 4b 3d 3d 72 63 0a 20 20 20 20 20 20 20 26 26 20 K==rc. &&
19353 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d SQLITE_OK==(rc =
19354 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
19355 6d 69 74 50 68 61 73 65 54 77 6f 28 70 2d 3e 70 mitPhaseTwo(p->p
19356 44 65 73 74 29 29 0a 20 20 20 20 20 20 29 7b 0a Dest)). ){.
19357 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
19358 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 ITE_DONE;.
19359 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f }. }. . /
1935a 2a 20 49 66 20 62 43 6c 6f 73 65 54 72 61 6e 73 * If bCloseTrans
1935b 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
1935c 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 70 65 his function ope
1935d 6e 65 64 20 61 20 72 65 61 64 20 74 72 61 6e 73 ned a read trans
1935e 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 6f 6e action. ** on
1935f 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
19360 62 61 73 65 2e 20 43 6c 6f 73 65 20 74 68 65 20 base. Close the
19361 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e read transaction
19362 20 68 65 72 65 2e 20 54 68 65 72 65 20 69 73 0a here. There is.
19363 20 20 20 20 2a 2a 20 6e 6f 20 6e 65 65 64 20 74 ** no need t
19364 6f 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 o check the retu
19365 72 6e 20 76 61 6c 75 65 73 20 6f 66 20 74 68 65 rn values of the
19366 20 62 74 72 65 65 20 6d 65 74 68 6f 64 73 20 68 btree methods h
19367 65 72 65 2c 20 61 73 0a 20 20 20 20 2a 2a 20 22 ere, as. ** "
19368 63 6f 6d 6d 69 74 74 69 6e 67 22 20 61 20 72 65 committing" a re
19369 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 ad-only transact
1936a 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e ion cannot fail.
1936b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1936c 62 43 6c 6f 73 65 54 72 61 6e 73 20 29 7b 0a 20 bCloseTrans ){.
1936d 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 69 TESTONLY( i
1936e 6e 74 20 72 63 32 20 29 3b 0a 20 20 20 20 20 20 nt rc2 );.
1936f 54 45 53 54 4f 4e 4c 59 28 20 72 63 32 20 20 3d TESTONLY( rc2 =
19370 20 29 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 ) sqlite3BtreeC
19371 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 2d ommitPhaseOne(p-
19372 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 >pSrc, 0);.
19373 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 32 20 7c TESTONLY( rc2 |
19374 3d 20 29 20 73 71 6c 69 74 65 33 42 74 72 65 65 = ) sqlite3Btree
19375 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 CommitPhaseTwo(p
19376 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20 61 ->pSrc);. a
19377 73 73 65 72 74 28 20 72 63 32 3d 3d 53 51 4c 49 ssert( rc2==SQLI
19378 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 7d 0a 20 TE_OK );. }.
19379 20 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 . p->rc = rc
1937a 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 ;. }. if( p->p
1937b 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71 DestDb ){. sq
1937c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1937d 65 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 e(p->pDestDb->mu
1937e 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 tex);. }. sqli
1937f 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 2d te3BtreeLeave(p-
19380 3e 70 53 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 >pSrc);. sqlite
19381 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3_mutex_leave(p-
19382 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b >pSrcDb->mutex);
19383 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
19384 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 ./*.** Release a
19385 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 61 73 73 ll resources ass
19386 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6e 20 ociated with an
19387 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2a 20 sqlite3_backup*
19388 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 handle..*/.SQLIT
19389 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1938a 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 3_backup_finish(
1938b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
1938c 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 p){. sqlite3_ba
1938d 63 6b 75 70 20 2a 2a 70 70 3b 20 20 20 20 20 20 ckup **pp;
1938e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 74 /* Pt
1938f 72 20 74 6f 20 68 65 61 64 20 6f 66 20 70 61 67 r to head of pag
19390 65 72 73 20 62 61 63 6b 75 70 20 6c 69 73 74 20 ers backup list
19391 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 */. sqlite3_mut
19392 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 ex *mutex;
19393 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 /* Mut
19394 65 78 20 74 6f 20 70 72 6f 74 65 63 74 20 73 6f ex to protect so
19395 75 72 63 65 20 64 61 74 61 62 61 73 65 20 2a 2f urce database */
19396 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
19397 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19398 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 /* Value
19399 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 to return */..
1939a 20 2f 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 /* Enter the mu
1939b 74 65 78 65 73 20 2a 2f 0a 20 20 69 66 28 20 70 texes */. if( p
1939c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c ==0 ) return SQL
1939d 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 ITE_OK;. sqlite
1939e 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3_mutex_enter(p-
1939f 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b >pSrcDb->mutex);
193a0 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
193a1 6e 74 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20 nter(p->pSrc);.
193a2 20 6d 75 74 65 78 20 3d 20 70 2d 3e 70 53 72 63 mutex = p->pSrc
193a3 44 62 2d 3e 6d 75 74 65 78 3b 0a 20 20 69 66 28 Db->mutex;. if(
193a4 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 p->pDestDb ){.
193a5 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
193a6 5f 65 6e 74 65 72 28 70 2d 3e 70 44 65 73 74 44 _enter(p->pDestD
193a7 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a b->mutex);. }..
193a8 20 20 2f 2a 20 44 65 74 61 63 68 20 74 68 69 73 /* Detach this
193a9 20 62 61 63 6b 75 70 20 66 72 6f 6d 20 74 68 65 backup from the
193aa 20 73 6f 75 72 63 65 20 70 61 67 65 72 2e 20 2a source pager. *
193ab 2f 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 /. if( p->pDest
193ac 44 62 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 72 Db ){. p->pSr
193ad 63 2d 3e 6e 42 61 63 6b 75 70 2d 2d 3b 0a 20 20 c->nBackup--;.
193ae 7d 0a 20 20 69 66 28 20 70 2d 3e 69 73 41 74 74 }. if( p->isAtt
193af 61 63 68 65 64 20 29 7b 0a 20 20 20 20 70 70 20 ached ){. pp
193b0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 = sqlite3PagerBa
193b1 63 6b 75 70 50 74 72 28 73 71 6c 69 74 65 33 42 ckupPtr(sqlite3B
193b2 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53 72 treePager(p->pSr
193b3 63 29 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 c));. while(
193b4 2a 70 70 21 3d 70 20 29 7b 0a 20 20 20 20 20 20 *pp!=p ){.
193b5 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 pp = &(*pp)->pNe
193b6 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 xt;. }. *p
193b7 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 p = p->pNext;.
193b8 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 74 72 61 }.. /* If a tra
193b9 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 69 6c nsaction is stil
193ba 6c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42 74 l open on the Bt
193bb 72 65 65 2c 20 72 6f 6c 6c 20 69 74 20 62 61 63 ree, roll it bac
193bc 6b 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 k. */. sqlite3B
193bd 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 2d 3e treeRollback(p->
193be 70 44 65 73 74 29 3b 0a 0a 20 20 2f 2a 20 53 65 pDest);.. /* Se
193bf 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 t the error code
193c0 20 6f 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 of the destinat
193c1 69 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e ion database han
193c2 64 6c 65 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 28 dle. */. rc = (
193c3 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f p->rc==SQLITE_DO
193c4 4e 45 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 NE) ? SQLITE_OK
193c5 3a 20 70 2d 3e 72 63 3b 0a 20 20 73 71 6c 69 74 : p->rc;. sqlit
193c6 65 33 45 72 72 6f 72 28 70 2d 3e 70 44 65 73 74 e3Error(p->pDest
193c7 44 62 2c 20 72 63 2c 20 30 29 3b 0a 0a 20 20 2f Db, rc, 0);.. /
193c8 2a 20 45 78 69 74 20 74 68 65 20 6d 75 74 65 78 * Exit the mutex
193c9 65 73 20 61 6e 64 20 66 72 65 65 20 74 68 65 20 es and free the
193ca 62 61 63 6b 75 70 20 63 6f 6e 74 65 78 74 20 73 backup context s
193cb 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 69 tructure. */. i
193cc 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b f( p->pDestDb ){
193cd 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
193ce 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 44 65 73 ex_leave(p->pDes
193cf 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d tDb->mutex);. }
193d0 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
193d1 65 61 76 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 eave(p->pSrc);.
193d2 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 if( p->pDestDb
193d3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
193d4 72 65 65 28 70 29 3b 0a 20 20 7d 0a 20 20 73 71 ree(p);. }. sq
193d5 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
193d6 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 e(mutex);. retu
193d7 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
193d8 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
193d9 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c 6c r of pages still
193da 20 74 6f 20 62 65 20 62 61 63 6b 65 64 20 75 70 to be backed up
193db 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 as of the most
193dc 72 65 63 65 6e 74 0a 2a 2a 20 63 61 6c 6c 20 74 recent.** call t
193dd 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 o sqlite3_backup
193de 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 _step()..*/.SQLI
193df 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
193e0 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e e3_backup_remain
193e1 69 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63 6b ing(sqlite3_back
193e2 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e up *p){. return
193e3 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e 67 3b 0a p->nRemaining;.
193e4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
193e5 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 the total number
193e6 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 of pages in the
193e7 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 source database
193e8 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 as of the most
193e9 0a 2a 2a 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 .** recent call
193ea 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
193eb 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c p_step()..*/.SQL
193ec 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
193ed 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 te3_backup_pagec
193ee 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 62 61 63 ount(sqlite3_bac
193ef 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 kup *p){. retur
193f0 6e 20 70 2d 3e 6e 50 61 67 65 63 6f 75 6e 74 3b n p->nPagecount;
193f1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
193f2 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
193f3 64 20 61 66 74 65 72 20 74 68 65 20 63 6f 6e 74 d after the cont
193f4 65 6e 74 73 20 6f 66 20 70 61 67 65 20 69 50 61 ents of page iPa
193f5 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 6f 75 ge of the.** sou
193f6 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61 76 rce database hav
193f7 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e e been modified.
193f8 20 49 66 20 70 61 67 65 20 69 50 61 67 65 20 68 If page iPage h
193f9 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
193fa 0a 2a 2a 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 .** copied into
193fb 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
193fc 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 74 database, then t
193fd 68 65 20 64 61 74 61 20 77 72 69 74 74 65 6e 20 he data written
193fe 74 6f 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e to the.** destin
193ff 61 74 69 6f 6e 20 69 73 20 6e 6f 77 20 69 6e 76 ation is now inv
19400 61 6c 69 64 61 74 65 64 2e 20 54 68 65 20 64 65 alidated. The de
19401 73 74 69 6e 61 74 69 6f 6e 20 63 6f 70 79 20 6f stination copy o
19402 66 20 69 50 61 67 65 20 6e 65 65 64 73 0a 2a 2a f iPage needs.**
19403 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 77 to be updated w
19404 69 74 68 20 74 68 65 20 6e 65 77 20 64 61 74 61 ith the new data
19405 20 62 65 66 6f 72 65 20 74 68 65 20 62 61 63 6b before the back
19406 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a up operation is.
19407 2a 2a 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a ** complete..**.
19408 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 ** It is assumed
19409 20 74 68 61 74 20 74 68 65 20 6d 75 74 65 78 20 that the mutex
1940a 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1940b 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a the BtShared obj
1940c 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e ect.** correspon
1940d 64 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 72 ding to the sour
1940e 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 68 ce database is h
1940f 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 eld when this fu
19410 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c nction is.** cal
19411 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 led..*/.SQLITE_P
19412 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
19413 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 65 28 te3BackupUpdate(
19414 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
19415 70 42 61 63 6b 75 70 2c 20 50 67 6e 6f 20 69 50 pBackup, Pgno iP
19416 61 67 65 2c 20 63 6f 6e 73 74 20 75 38 20 2a 61 age, const u8 *a
19417 44 61 74 61 29 7b 0a 20 20 73 71 6c 69 74 65 33 Data){. sqlite3
19418 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 20 _backup *p;
19419 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1941a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 Iterator variab
1941b 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 3d 70 42 le */. for(p=pB
1941c 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 ackup; p; p=p->p
1941d 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72 Next){. asser
1941e 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1941f 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 2d 3e 70 _held(p->pSrc->p
19420 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
19421 20 20 69 66 28 20 21 69 73 46 61 74 61 6c 45 72 if( !isFatalEr
19422 72 6f 72 28 70 2d 3e 72 63 29 20 26 26 20 69 50 ror(p->rc) && iP
19423 61 67 65 3c 70 2d 3e 69 4e 65 78 74 20 29 7b 0a age<p->iNext ){.
19424 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 61 63 /* The bac
19425 6b 75 70 20 70 72 6f 63 65 73 73 20 70 20 68 61 kup process p ha
19426 73 20 61 6c 72 65 61 64 79 20 63 6f 70 69 65 64 s already copied
19427 20 70 61 67 65 20 69 50 61 67 65 2e 20 42 75 74 page iPage. But
19428 20 6e 6f 77 20 69 74 0a 20 20 20 20 20 20 2a 2a now it. **
19429 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 has been modifi
1942a 65 64 20 62 79 20 61 20 74 72 61 6e 73 61 63 74 ed by a transact
1942b 69 6f 6e 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 ion on the sourc
1942c 65 20 70 61 67 65 72 2e 20 43 6f 70 79 0a 20 20 e pager. Copy.
1942d 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 64 ** the new d
1942e 61 74 61 20 69 6e 74 6f 20 74 68 65 20 62 61 63 ata into the bac
1942f 6b 75 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 kup.. */.
19430 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62 61 63 int rc = bac
19431 6b 75 70 4f 6e 65 50 61 67 65 28 70 2c 20 69 50 kupOnePage(p, iP
19432 61 67 65 2c 20 61 44 61 74 61 29 3b 0a 20 20 20 age, aData);.
19433 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 assert( rc!=S
19434 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 72 63 QLITE_BUSY && rc
19435 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 !=SQLITE_LOCKED
19436 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
19437 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
19438 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 p->rc = rc
19439 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1943a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 }.}../*.** Res
1943b 74 61 72 74 20 74 68 65 20 62 61 63 6b 75 70 20 tart the backup
1943c 70 72 6f 63 65 73 73 2e 20 54 68 69 73 20 69 73 process. This is
1943d 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 called when the
1943e 20 70 61 67 65 72 20 6c 61 79 65 72 0a 2a 2a 20 pager layer.**
1943f 64 65 74 65 63 74 73 20 74 68 61 74 20 74 68 65 detects that the
19440 20 64 61 74 61 62 61 73 65 20 68 61 73 20 62 65 database has be
19441 65 6e 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 en modified by a
19442 6e 20 65 78 74 65 72 6e 61 6c 20 64 61 74 61 62 n external datab
19443 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f ase.** connectio
19444 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 n. In this case
19445 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 there is no way
19446 6f 66 20 6b 6e 6f 77 69 6e 67 20 77 68 69 63 68 of knowing which
19447 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 73 of the.** pages
19448 20 74 68 61 74 20 68 61 76 65 20 62 65 65 6e 20 that have been
19449 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 copied into the
1944a 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 destination data
1944b 62 61 73 65 20 61 72 65 20 73 74 69 6c 6c 20 0a base are still .
1944c 2a 2a 20 76 61 6c 69 64 20 61 6e 64 20 77 68 69 ** valid and whi
1944d 63 68 20 61 72 65 20 6e 6f 74 2c 20 73 6f 20 74 ch are not, so t
1944e 68 65 20 65 6e 74 69 72 65 20 70 72 6f 63 65 73 he entire proces
1944f 73 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 s needs to be re
19450 73 74 61 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 started..**.** I
19451 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 t is assumed tha
19452 74 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f t the mutex asso
19453 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
19454 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 0a BtShared object.
19455 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ** corresponding
19456 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 64 to the source d
19457 61 74 61 62 61 73 65 20 69 73 20 68 65 6c 64 20 atabase is held
19458 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
19459 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e on is.** called.
1945a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1945b 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
1945c 61 63 6b 75 70 52 65 73 74 61 72 74 28 73 71 6c ackupRestart(sql
1945d 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 ite3_backup *pBa
1945e 63 6b 75 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 ckup){. sqlite3
1945f 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 20 _backup *p;
19460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19461 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 Iterator variab
19462 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 3d 70 42 le */. for(p=pB
19463 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 ackup; p; p=p->p
19464 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72 Next){. asser
19465 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
19466 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 2d 3e 70 _held(p->pSrc->p
19467 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
19468 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 31 3b 0a p->iNext = 1;.
19469 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 }.}..#ifndef S
1946a 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 QLITE_OMIT_VACUU
1946b 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 74 68 65 M./*.** Copy the
1946c 20 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e complete conten
1946d 74 20 6f 66 20 70 42 74 46 72 6f 6d 20 69 6e 74 t of pBtFrom int
1946e 6f 20 70 42 74 54 6f 2e 20 20 41 20 74 72 61 6e o pBtTo. A tran
1946f 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 saction.** must
19470 62 65 20 61 63 74 69 76 65 20 66 6f 72 20 62 6f be active for bo
19471 74 68 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 th files..**.**
19472 54 68 65 20 73 69 7a 65 20 6f 66 20 66 69 6c 65 The size of file
19473 20 70 54 6f 20 6d 61 79 20 62 65 20 72 65 64 75 pTo may be redu
19474 63 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72 ced by this oper
19475 61 74 69 6f 6e 2e 20 49 66 20 61 6e 79 74 68 69 ation. If anythi
19476 6e 67 20 0a 2a 2a 20 67 6f 65 73 20 77 72 6f 6e ng .** goes wron
19477 67 2c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 g, the transacti
19478 6f 6e 20 6f 6e 20 70 54 6f 20 69 73 20 72 6f 6c on on pTo is rol
19479 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 73 75 63 led back. If suc
1947a 63 65 73 73 66 75 6c 2c 20 74 68 65 20 0a 2a 2a cessful, the .**
1947b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1947c 63 6f 6d 6d 69 74 74 65 64 20 62 65 66 6f 72 65 committed before
1947d 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 returning..*/.S
1947e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1947f 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f t sqlite3BtreeCo
19480 70 79 46 69 6c 65 28 42 74 72 65 65 20 2a 70 54 pyFile(Btree *pT
19481 6f 2c 20 42 74 72 65 65 20 2a 70 46 72 6f 6d 29 o, Btree *pFrom)
19482 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 {. int rc;. sq
19483 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 62 3b 0a lite3_backup b;.
19484 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
19485 74 65 72 28 70 54 6f 29 3b 0a 20 20 73 71 6c 69 ter(pTo);. sqli
19486 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 46 te3BtreeEnter(pF
19487 72 6f 6d 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 rom);.. /* Set
19488 75 70 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 61 up an sqlite3_ba
19489 63 6b 75 70 20 6f 62 6a 65 63 74 2e 20 73 71 6c ckup object. sql
1948a 69 74 65 33 5f 62 61 63 6b 75 70 2e 70 44 65 73 ite3_backup.pDes
1948b 74 44 62 20 6d 75 73 74 20 62 65 20 73 65 74 0a tDb must be set.
1948c 20 20 2a 2a 20 74 6f 20 30 2e 20 54 68 69 73 20 ** to 0. This
1948d 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 69 is used by the i
1948e 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f mplementations o
1948f 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 f sqlite3_backup
19490 5f 73 74 65 70 28 29 0a 20 20 2a 2a 20 61 6e 64 _step(). ** and
19491 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
19492 66 69 6e 69 73 68 28 29 20 74 6f 20 64 65 74 65 finish() to dete
19493 63 74 20 74 68 61 74 20 74 68 65 79 20 61 72 65 ct that they are
19494 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 0a 20 20 being called.
19495 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 66 75 6e ** from this fun
19496 63 74 69 6f 6e 2c 20 6e 6f 74 20 64 69 72 65 63 ction, not direc
19497 74 6c 79 20 62 79 20 74 68 65 20 75 73 65 72 2e tly by the user.
19498 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 . */. memset(&
19499 62 2c 20 30 2c 20 73 69 7a 65 6f 66 28 62 29 29 b, 0, sizeof(b))
1949a 3b 0a 20 20 62 2e 70 53 72 63 44 62 20 3d 20 70 ;. b.pSrcDb = p
1949b 46 72 6f 6d 2d 3e 64 62 3b 0a 20 20 62 2e 70 53 From->db;. b.pS
1949c 72 63 20 3d 20 70 46 72 6f 6d 3b 0a 20 20 62 2e rc = pFrom;. b.
1949d 70 44 65 73 74 20 3d 20 70 54 6f 3b 0a 20 20 62 pDest = pTo;. b
1949e 2e 69 4e 65 78 74 20 3d 20 31 3b 0a 0a 20 20 2f .iNext = 1;.. /
1949f 2a 20 30 78 37 46 46 46 46 46 46 46 20 69 73 20 * 0x7FFFFFFF is
194a0 74 68 65 20 68 61 72 64 20 6c 69 6d 69 74 20 66 the hard limit f
194a1 6f 72 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 or the number of
194a2 20 70 61 67 65 73 20 69 6e 20 61 20 64 61 74 61 pages in a data
194a3 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 base. ** file.
194a4 42 79 20 70 61 73 73 69 6e 67 20 74 68 69 73 20 By passing this
194a5 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 as the number of
194a6 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 20 74 pages to copy t
194a7 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 62 o. ** sqlite3_b
194a8 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 20 77 65 ackup_step(), we
194a9 20 63 61 6e 20 67 75 61 72 61 6e 74 65 65 20 74 can guarantee t
194aa 68 61 74 20 74 68 65 20 63 6f 70 79 20 66 69 6e hat the copy fin
194ab 69 73 68 65 73 20 0a 20 20 2a 2a 20 77 69 74 68 ishes . ** with
194ac 69 6e 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c in a single call
194ad 20 28 75 6e 6c 65 73 73 20 61 6e 20 65 72 72 6f (unless an erro
194ae 72 20 6f 63 63 75 72 73 29 2e 20 54 68 65 20 61 r occurs). The a
194af 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e ssert() statemen
194b0 74 0a 20 20 2a 2a 20 63 68 65 63 6b 73 20 74 68 t. ** checks th
194b1 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 20 2d 20 is assumption -
194b2 28 70 2d 3e 72 63 29 20 73 68 6f 75 6c 64 20 62 (p->rc) should b
194b3 65 20 73 65 74 20 74 6f 20 65 69 74 68 65 72 20 e set to either
194b4 53 51 4c 49 54 45 5f 44 4f 4e 45 20 0a 20 20 2a SQLITE_DONE . *
194b5 2a 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f * or an error co
194b6 64 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 de.. */. sqlit
194b7 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 26 e3_backup_step(&
194b8 62 2c 20 30 78 37 46 46 46 46 46 46 46 29 3b 0a b, 0x7FFFFFFF);.
194b9 20 20 61 73 73 65 72 74 28 20 62 2e 72 63 21 3d assert( b.rc!=
194ba 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 SQLITE_OK );. r
194bb 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 61 63 6b c = sqlite3_back
194bc 75 70 5f 66 69 6e 69 73 68 28 26 62 29 3b 0a 20 up_finish(&b);.
194bd 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
194be 4f 4b 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 70 OK ){. pTo->p
194bf 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 Bt->pageSizeFixe
194c0 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 73 71 d = 0;. }.. sq
194c1 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
194c2 70 46 72 6f 6d 29 3b 0a 20 20 73 71 6c 69 74 65 pFrom);. sqlite
194c3 33 42 74 72 65 65 4c 65 61 76 65 28 70 54 6f 29 3BtreeLeave(pTo)
194c4 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
194c5 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
194c6 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 2a 2f E_OMIT_VACUUM */
194c7 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
194c8 2a 20 45 6e 64 20 6f 66 20 62 61 63 6b 75 70 2e * End of backup.
194c9 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
194ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
194cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
194cc 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
194cd 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 * Begin file vdb
194ce 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a emem.c *********
194cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
194d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
194d1 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 /./*.** 2004 May
194d2 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 26.**.** The au
194d3 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
194d4 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
194d5 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
194d6 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
194d7 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
194d8 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
194d9 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
194da 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
194db 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
194dc 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
194dd 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
194de 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
194df 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
194e0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
194e1 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
194e2 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
194e3 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
194e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
194e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
194e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
194e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
194e8 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
194e9 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 le contains code
194ea 20 75 73 65 20 74 6f 20 6d 61 6e 69 70 75 6c 61 use to manipula
194eb 74 65 20 22 4d 65 6d 22 20 73 74 72 75 63 74 75 te "Mem" structu
194ec 72 65 2e 20 20 41 20 22 4d 65 6d 22 0a 2a 2a 20 re. A "Mem".**
194ed 73 74 6f 72 65 73 20 61 20 73 69 6e 67 6c 65 20 stores a single
194ee 76 61 6c 75 65 20 69 6e 20 74 68 65 20 56 44 42 value in the VDB
194ef 45 2e 20 20 4d 65 6d 20 69 73 20 61 6e 20 6f 70 E. Mem is an op
194f0 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 76 aque structure v
194f1 69 73 69 62 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77 isible.** only w
194f2 69 74 68 69 6e 20 74 68 65 20 56 44 42 45 2e 20 ithin the VDBE.
194f3 20 49 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 Interface routi
194f4 6e 65 73 20 72 65 66 65 72 20 74 6f 20 61 20 4d nes refer to a M
194f5 65 6d 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 em using the.**
194f6 6e 61 6d 65 20 73 71 6c 69 74 65 5f 76 61 6c 75 name sqlite_valu
194f7 65 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 e.**.** $Id: vdb
194f8 65 6d 65 6d 2e 63 2c 76 20 31 2e 31 35 32 20 32 emem.c,v 1.152 2
194f9 30 30 39 2f 30 37 2f 32 32 20 31 38 3a 30 37 3a 009/07/22 18:07:
194fa 34 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 41 drh Exp $.*/.
194fb 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 ./*.** Call sqli
194fc 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 te3VdbeMemExpand
194fd 42 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75 Blob() on the su
194fe 70 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79 pplied value (ty
194ff 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 pe Mem*).** P if
19500 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 required..*/.#d
19501 65 66 69 6e 65 20 65 78 70 61 6e 64 42 6c 6f 62 efine expandBlob
19502 28 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 (P) (((P)->flags
19503 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 &MEM_Zero)?sqlit
19504 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 e3VdbeMemExpandB
19505 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a lob(P):0)../*.**
19506 20 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 6f If pMem is an o
19507 62 6a 65 63 74 20 77 69 74 68 20 61 20 76 61 6c bject with a val
19508 69 64 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 id string repres
19509 65 6e 74 61 74 69 6f 6e 2c 20 74 68 69 73 20 72 entation, this r
1950a 6f 75 74 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 outine.** ensure
1950b 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 65 s the internal e
1950c 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 ncoding for the
1950d 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 string represent
1950e 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 73 ation is.** 'des
1950f 69 72 65 64 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 iredEnc', one of
19510 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 SQLITE_UTF8, SQ
19511 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 LITE_UTF16LE or
19512 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2e 0a SQLITE_UTF16BE..
19513 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 **.** If pMem is
19514 20 6e 6f 74 20 61 20 73 74 72 69 6e 67 20 6f 62 not a string ob
19515 6a 65 63 74 2c 20 6f 72 20 74 68 65 20 65 6e 63 ject, or the enc
19516 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 73 74 72 oding of the str
19517 69 6e 67 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 ing.** represent
19518 61 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 ation is already
19519 20 73 74 6f 72 65 64 20 75 73 69 6e 67 20 74 68 stored using th
1951a 65 20 72 65 71 75 65 73 74 65 64 20 65 6e 63 6f e requested enco
1951b 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 69 73 0a ding, then this.
1951c 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 ** routine is a
1951d 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c no-op..**.** SQL
1951e 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
1951f 65 64 20 69 66 20 74 68 65 20 63 6f 6e 76 65 72 ed if the conver
19520 73 69 6f 6e 20 69 73 20 73 75 63 63 65 73 73 66 sion is successf
19521 75 6c 20 28 6f 72 20 6e 6f 74 20 72 65 71 75 69 ul (or not requi
19522 72 65 64 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f red)..** SQLITE_
19523 4e 4f 4d 45 4d 20 6d 61 79 20 62 65 20 72 65 74 NOMEM may be ret
19524 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f urned if a mallo
19525 63 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67 c() fails during
19526 20 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 conversion.** b
19527 65 74 77 65 65 6e 20 66 6f 72 6d 61 74 73 2e 0a etween formats..
19528 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19529 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
1952a 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 eChangeEncoding(
1952b 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 Mem *pMem, int d
1952c 65 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e esiredEnc){. in
1952d 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 t rc;. assert(
1952e 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d (pMem->flags&MEM
1952f 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 _RowSet)==0 );.
19530 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64 assert( desired
19531 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 Enc==SQLITE_UTF8
19532 20 7c 7c 20 64 65 73 69 72 65 64 45 6e 63 3d 3d || desiredEnc==
19533 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a 20 SQLITE_UTF16LE.
19534 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 73 || des
19535 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f iredEnc==SQLITE_
19536 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28 UTF16BE );. if(
19537 20 21 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d !(pMem->flags&M
19538 45 4d 5f 53 74 72 29 20 7c 7c 20 70 4d 65 6d 2d EM_Str) || pMem-
19539 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e 63 >enc==desiredEnc
1953a 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
1953b 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
1953c 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 assert( pMem->db
1953d 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
1953e 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e utex_held(pMem->
1953f 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23 69 db->mutex) );.#i
19540 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
19541 5f 55 54 46 31 36 0a 20 20 72 65 74 75 72 6e 20 _UTF16. return
19542 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 23 65 SQLITE_ERROR;.#e
19543 6c 73 65 0a 0a 20 20 2f 2a 20 4d 65 6d 54 72 61 lse.. /* MemTra
19544 6e 73 6c 61 74 65 28 29 20 6d 61 79 20 72 65 74 nslate() may ret
19545 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 urn SQLITE_OK or
19546 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 SQLITE_NOMEM. I
19547 66 20 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 f NOMEM is retur
19548 6e 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 ned,. ** then t
19549 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 he encoding of t
1954a 68 65 20 76 61 6c 75 65 20 6d 61 79 20 6e 6f 74 he value may not
1954b 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a 20 have changed..
1954c 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 */. rc = sqlit
1954d 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 e3VdbeMemTransla
1954e 74 65 28 70 4d 65 6d 2c 20 28 75 38 29 64 65 73 te(pMem, (u8)des
1954f 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73 65 iredEnc);. asse
19550 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b rt(rc==SQLITE_OK
19551 20 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 || rc==SQLIT
19552 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 61 73 73 65 E_NOMEM);. asse
19553 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b rt(rc==SQLITE_OK
19554 20 20 20 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 || pMem->enc
19555 21 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 !=desiredEnc);.
19556 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 assert(rc==SQLI
19557 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 70 4d 65 6d TE_NOMEM || pMem
19558 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e ->enc==desiredEn
19559 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b c);. return rc;
1955a 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
1955b 20 4d 61 6b 65 20 73 75 72 65 20 70 4d 65 6d 2d Make sure pMem-
1955c 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 77 >z points to a w
1955d 72 69 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 69 ritable allocati
1955e 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 0a on of at least .
1955f 2a 2a 20 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a ** n bytes..**.*
19560 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 * If the memory
19561 63 65 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 63 cell currently c
19562 6f 6e 74 61 69 6e 73 20 73 74 72 69 6e 67 20 6f ontains string o
19563 72 20 62 6c 6f 62 20 64 61 74 61 0a 2a 2a 20 61 r blob data.** a
19564 6e 64 20 74 68 65 20 74 68 69 72 64 20 61 72 67 nd the third arg
19565 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 ument passed to
19566 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
19567 20 74 72 75 65 2c 20 74 68 65 20 0a 2a 2a 20 63 true, the .** c
19568 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 20 6f urrent content o
19569 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 70 72 f the cell is pr
1956a 65 73 65 72 76 65 64 2e 20 4f 74 68 65 72 77 69 eserved. Otherwi
1956b 73 65 2c 20 69 74 20 6d 61 79 0a 2a 2a 20 62 65 se, it may.** be
1956c 20 64 69 73 63 61 72 64 65 64 2e 20 20 0a 2a 2a discarded. .**
1956d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
1956e 6e 20 73 65 74 73 20 74 68 65 20 4d 45 4d 5f 44 n sets the MEM_D
1956f 79 6e 20 66 6c 61 67 20 61 6e 64 20 63 6c 65 61 yn flag and clea
19570 72 73 20 61 6e 79 20 78 44 65 6c 20 63 61 6c 6c rs any xDel call
19571 62 61 63 6b 2e 0a 2a 2a 20 49 74 20 61 6c 73 6f back..** It also
19572 20 63 6c 65 61 72 73 20 4d 45 4d 5f 45 70 68 65 clears MEM_Ephe
19573 6d 20 61 6e 64 20 4d 45 4d 5f 53 74 61 74 69 63 m and MEM_Static
19574 2e 20 49 66 20 74 68 65 20 70 72 65 73 65 72 76 . If the preserv
19575 65 20 66 6c 61 67 20 69 73 20 0a 2a 2a 20 6e 6f e flag is .** no
19576 74 20 73 65 74 2c 20 4d 65 6d 2e 6e 20 69 73 20 t set, Mem.n is
19577 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 zeroed..*/.SQLIT
19578 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
19579 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
1957a 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 (Mem *pMem, int
1957b 6e 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65 29 n, int preserve)
1957c 7b 0a 20 20 61 73 73 65 72 74 28 20 31 20 3e 3d {. assert( 1 >=
1957d 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 7a 4d 61 . ((pMem->zMa
1957e 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d lloc && pMem->zM
1957f 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d 3e 7a 29 20 alloc==pMem->z)
19580 3f 20 31 20 3a 20 30 29 20 2b 0a 20 20 20 20 28 ? 1 : 0) +. (
19581 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 ((pMem->flags&ME
19582 4d 5f 44 79 6e 29 26 26 70 4d 65 6d 2d 3e 78 44 M_Dyn)&&pMem->xD
19583 65 6c 29 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a el) ? 1 : 0) + .
19584 20 20 20 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 ((pMem->flag
19585 73 26 4d 45 4d 5f 45 70 68 65 6d 29 20 3f 20 31 s&MEM_Ephem) ? 1
19586 20 3a 20 30 29 20 2b 20 0a 20 20 20 20 28 28 70 : 0) + . ((p
19587 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 Mem->flags&MEM_S
19588 74 61 74 69 63 29 20 3f 20 31 20 3a 20 30 29 0a tatic) ? 1 : 0).
19589 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 );. assert( (
1958a 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
1958b 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 RowSet)==0 );..
1958c 20 69 66 28 20 6e 3c 33 32 20 29 20 6e 20 3d 20 if( n<32 ) n =
1958d 33 32 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 32;. if( sqlite
1958e 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 3DbMallocSize(pM
1958f 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d em->db, pMem->zM
19590 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a 20 20 20 20 alloc)<n ){.
19591 69 66 28 20 70 72 65 73 65 72 76 65 20 26 26 20 if( preserve &&
19592 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a pMem->z==pMem->z
19593 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 Malloc ){.
19594 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e pMem->z = pMem->
19595 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 zMalloc = sqlite
19596 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 3DbReallocOrFree
19597 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d (pMem->db, pMem-
19598 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 70 72 >z, n);. pr
19599 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 eserve = 0;.
1959a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c }else{. sql
1959b 69 74 65 33 44 62 46 72 65 65 28 70 4d 65 6d 2d ite3DbFree(pMem-
1959c 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c >db, pMem->zMall
1959d 6f 63 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d oc);. pMem-
1959e 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 >zMalloc = sqlit
1959f 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d e3DbMallocRaw(pM
195a0 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a 20 20 20 20 em->db, n);.
195a1 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d 65 }. }.. if( pMe
195a2 6d 2d 3e 7a 20 26 26 20 70 72 65 73 65 72 76 65 m->z && preserve
195a3 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f && pMem->zMallo
195a4 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d c && pMem->z!=pM
195a5 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 em->zMalloc ){.
195a6 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d 3e memcpy(pMem->
195a7 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 6d 2d 3e 7a zMalloc, pMem->z
195a8 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a , pMem->n);. }.
195a9 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 if( pMem->flag
195aa 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70 4d 65 s&MEM_Dyn && pMe
195ab 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20 70 m->xDel ){. p
195ac 4d 65 6d 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 Mem->xDel((void
195ad 2a 29 28 70 4d 65 6d 2d 3e 7a 29 29 3b 0a 20 20 *)(pMem->z));.
195ae 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 }.. pMem->z = p
195af 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 Mem->zMalloc;.
195b0 69 66 28 20 70 4d 65 6d 2d 3e 7a 3d 3d 30 20 29 if( pMem->z==0 )
195b1 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 {. pMem->flag
195b2 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
195b3 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d }else{. pMem-
195b4 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f >flags &= ~(MEM_
195b5 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 Ephem|MEM_Static
195b6 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 78 );. }. pMem->x
195b7 44 65 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 Del = 0;. retur
195b8 6e 20 28 70 4d 65 6d 2d 3e 7a 20 3f 20 53 51 4c n (pMem->z ? SQL
195b9 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f ITE_OK : SQLITE_
195ba 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a NOMEM);.}../*.**
195bb 20 4d 61 6b 65 20 74 68 65 20 67 69 76 65 6e 20 Make the given
195bc 4d 65 6d 20 6f 62 6a 65 63 74 20 4d 45 4d 5f 44 Mem object MEM_D
195bd 79 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f yn. In other wo
195be 72 64 73 2c 20 6d 61 6b 65 20 69 74 20 73 6f 0a rds, make it so.
195bf 2a 2a 20 74 68 61 74 20 61 6e 79 20 54 45 58 54 ** that any TEXT
195c0 20 6f 72 20 42 4c 4f 42 20 63 6f 6e 74 65 6e 74 or BLOB content
195c1 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 is stored in me
195c2 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
195c3 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e 20 om.** malloc().
195c4 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 77 65 In this way, we
195c5 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6d know that the m
195c6 65 6d 6f 72 79 20 69 73 20 73 61 66 65 20 74 6f emory is safe to
195c7 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 be.** overwritt
195c8 65 6e 20 6f 72 20 61 6c 74 65 72 65 64 2e 0a 2a en or altered..*
195c9 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 *.** Return SQLI
195ca 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 TE_OK on success
195cb 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d or SQLITE_NOMEM
195cc 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 if malloc fails
195cd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
195ce 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
195cf 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 dbeMemMakeWritea
195d0 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a ble(Mem *pMem){.
195d1 20 20 69 6e 74 20 66 3b 0a 20 20 61 73 73 65 72 int f;. asser
195d2 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c t( pMem->db==0 |
195d3 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
195d4 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d held(pMem->db->m
195d5 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
195d6 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 t( (pMem->flags&
195d7 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 MEM_RowSet)==0 )
195d8 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28 70 ;. expandBlob(p
195d9 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 70 4d 65 6d Mem);. f = pMem
195da 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 28 ->flags;. if( (
195db 66 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 f&(MEM_Str|MEM_B
195dc 6c 6f 62 29 29 20 26 26 20 70 4d 65 6d 2d 3e 7a lob)) && pMem->z
195dd 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 !=pMem->zMalloc
195de 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
195df 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d e3VdbeMemGrow(pM
195e0 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b 20 32 2c em, pMem->n + 2,
195e1 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 1) ){. ret
195e2 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
195e3 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d ;. }. pMem
195e4 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 ->z[pMem->n] = 0
195e5 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d ;. pMem->z[pM
195e6 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 em->n+1] = 0;.
195e7 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d pMem->flags |=
195e8 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d 0a 0a MEM_Term;. }..
195e9 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
195ea 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 OK;.}../*.** If
195eb 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2a 20 68 the given Mem* h
195ec 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 as a zero-filled
195ed 20 74 61 69 6c 2c 20 74 75 72 6e 20 69 74 20 69 tail, turn it i
195ee 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61 72 79 0a nto an ordinary.
195ef 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65 64 20 69 ** blob stored i
195f0 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c n dynamically al
195f1 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 0a 2a located space..*
195f2 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
195f3 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 _OMIT_INCRBLOB.S
195f4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
195f5 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
195f6 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a ExpandBlob(Mem *
195f7 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65 pMem){. if( pMe
195f8 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a m->flags & MEM_Z
195f9 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e ero ){. int n
195fa 42 79 74 65 3b 0a 20 20 20 20 61 73 73 65 72 74 Byte;. assert
195fb 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 ( pMem->flags&ME
195fc 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 61 73 M_Blob );. as
195fd 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
195fe 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d gs&MEM_RowSet)==
195ff 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
19600 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 pMem->db==0 ||
19601 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
19602 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 ld(pMem->db->mut
19603 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 ex) );.. /* S
19604 65 74 20 6e 42 79 74 65 20 74 6f 20 74 68 65 20 et nByte to the
19605 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
19606 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 required to stor
19607 65 20 74 68 65 20 65 78 70 61 6e 64 65 64 20 62 e the expanded b
19608 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20 6e 42 79 74 lob. */. nByt
19609 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2b 20 70 4d e = pMem->n + pM
1960a 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 em->u.nZero;.
1960b 20 69 66 28 20 6e 42 79 74 65 3c 3d 30 20 29 7b if( nByte<=0 ){
1960c 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 31 . nByte = 1
1960d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1960e 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
1960f 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 ow(pMem, nByte,
19610 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 1) ){. retu
19611 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
19612 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 73 . }.. mems
19613 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d et(&pMem->z[pMem
19614 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e 75 ->n], 0, pMem->u
19615 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 70 4d 65 .nZero);. pMe
19616 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e m->n += pMem->u.
19617 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4d 65 6d 2d nZero;. pMem-
19618 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f >flags &= ~(MEM_
19619 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a Zero|MEM_Term);.
1961a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
1961b 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 ITE_OK;.}.#endif
1961c 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 .../*.** Make su
1961d 72 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d re the given Mem
1961e 20 69 73 20 5c 75 30 30 30 30 20 74 65 72 6d 69 is \u0000 termi
1961f 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 nated..*/.SQLITE
19620 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
19621 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 ite3VdbeMemNulTe
19622 72 6d 69 6e 61 74 65 28 4d 65 6d 20 2a 70 4d 65 rminate(Mem *pMe
19623 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d m){. assert( pM
19624 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
19625 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
19626 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
19627 20 29 3b 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d );. if( (pMem-
19628 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 >flags & MEM_Ter
19629 6d 29 21 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e m)!=0 || (pMem->
1962a 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 flags & MEM_Str)
1962b 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
1962c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f n SQLITE_OK; /
1962d 2a 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 * Nothing to do
1962e 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c */. }. if( sql
1962f 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
19630 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c pMem, pMem->n+2,
19631 20 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 1) ){. retur
19632 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
19633 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d }. pMem->z[pM
19634 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d em->n] = 0;. pM
19635 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d em->z[pMem->n+1]
19636 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c = 0;. pMem->fl
19637 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b ags |= MEM_Term;
19638 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
19639 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 _OK;.}../*.** Ad
1963a 64 20 4d 45 4d 5f 53 74 72 20 74 6f 20 74 68 65 d MEM_Str to the
1963b 20 73 65 74 20 6f 66 20 72 65 70 72 65 73 65 6e set of represen
1963c 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 tations for the
1963d 67 69 76 65 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 given Mem. Numb
1963e 65 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 ers.** are conve
1963f 72 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 rted using sqlit
19640 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 2e 20 20 e3_snprintf().
19641 43 6f 6e 76 65 72 74 69 6e 67 20 61 20 42 4c 4f Converting a BLO
19642 42 20 74 6f 20 61 20 73 74 72 69 6e 67 0a 2a 2a B to a string.**
19643 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a is a no-op..**.
19644 2a 2a 20 45 78 69 73 74 69 6e 67 20 72 65 70 72 ** Existing repr
19645 65 73 65 6e 74 61 74 69 6f 6e 73 20 4d 45 4d 5f esentations MEM_
19646 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c Int and MEM_Real
19647 20 61 72 65 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c are *not* inval
19648 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 idated..**.** A
19649 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75 65 20 77 MEM_Null value w
1964a 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 70 61 73 ill never be pas
1964b 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 sed to this func
1964c 74 69 6f 6e 2e 20 54 68 69 73 20 66 75 6e 63 74 tion. This funct
1964d 69 6f 6e 20 69 73 0a 2a 2a 20 75 73 65 64 20 66 ion is.** used f
1964e 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 76 61 or converting va
1964f 6c 75 65 73 20 74 6f 20 74 65 78 74 20 66 6f 72 lues to text for
19650 20 72 65 74 75 72 6e 69 6e 67 20 74 6f 20 74 68 returning to th
19651 65 20 75 73 65 72 20 28 69 2e 65 2e 20 76 69 61 e user (i.e. via
19652 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 .** sqlite3_valu
19653 65 5f 74 65 78 74 28 29 29 2c 20 6f 72 20 66 6f e_text()), or fo
19654 72 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74 20 r ensuring that
19655 76 61 6c 75 65 73 20 74 6f 20 62 65 20 75 73 65 values to be use
19656 64 20 61 73 20 62 74 72 65 65 0a 2a 2a 20 6b 65 d as btree.** ke
19657 79 73 20 61 72 65 20 73 74 72 69 6e 67 73 2e 20 ys are strings.
19658 49 6e 20 74 68 65 20 66 6f 72 6d 65 72 20 63 61 In the former ca
19659 73 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 se a NULL pointe
1965a 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 68 r is returned th
1965b 65 0a 2a 2a 20 75 73 65 72 20 61 6e 64 20 74 68 e.** user and th
1965c 65 20 6c 61 74 65 72 20 69 73 20 61 6e 20 69 6e e later is an in
1965d 74 65 72 6e 61 6c 20 70 72 6f 67 72 61 6d 6d 69 ternal programmi
1965e 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c ng error..*/.SQL
1965f 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
19660 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 sqlite3VdbeMemSt
19661 72 69 6e 67 69 66 79 28 4d 65 6d 20 2a 70 4d 65 ringify(Mem *pMe
19662 6d 2c 20 69 6e 74 20 65 6e 63 29 7b 0a 20 20 69 m, int enc){. i
19663 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
19664 4b 3b 0a 20 20 69 6e 74 20 66 67 20 3d 20 70 4d K;. int fg = pM
19665 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6e em->flags;. con
19666 73 74 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 33 st int nByte = 3
19667 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 2;.. assert( pM
19668 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
19669 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1966a 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
1966b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28 );. assert( !(
1966c 66 67 26 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a fg&MEM_Zero) );.
1966d 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26 28 assert( !(fg&(
1966e 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 MEM_Str|MEM_Blob
1966f 29 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 )) );. assert(
19670 66 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f fg&(MEM_Int|MEM_
19671 52 65 61 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 Real) );. asser
19672 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 t( (pMem->flags&
19673 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 MEM_RowSet)==0 )
19674 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 ;. assert( EIGH
19675 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 T_BYTE_ALIGNMENT
19676 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a 20 20 69 66 (pMem) );... if
19677 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d ( sqlite3VdbeMem
19678 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 Grow(pMem, nByte
19679 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 , 0) ){. retu
1967a 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
1967b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 . }.. /* For a
1967c 20 52 65 61 6c 20 6f 72 20 49 6e 74 65 67 65 72 Real or Integer
1967d 2c 20 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 70 , use sqlite3_mp
1967e 72 69 6e 74 66 28 29 20 74 6f 20 70 72 6f 64 75 rintf() to produ
1967f 63 65 20 74 68 65 20 55 54 46 2d 38 0a 20 20 2a ce the UTF-8. *
19680 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 * string represe
19681 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 ntation of the v
19682 61 6c 75 65 2e 20 54 68 65 6e 2c 20 69 66 20 74 alue. Then, if t
19683 68 65 20 72 65 71 75 69 72 65 64 20 65 6e 63 6f he required enco
19684 64 69 6e 67 0a 20 20 2a 2a 20 69 73 20 55 54 46 ding. ** is UTF
19685 2d 31 36 6c 65 20 6f 72 20 55 54 46 2d 31 36 62 -16le or UTF-16b
19686 65 20 64 6f 20 61 20 74 72 61 6e 73 6c 61 74 69 e do a translati
19687 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 46 on.. ** . ** F
19688 49 58 20 4d 45 3a 20 49 74 20 77 6f 75 6c 64 20 IX ME: It would
19689 62 65 20 62 65 74 74 65 72 20 69 66 20 73 71 6c be better if sql
1968a 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 ite3_snprintf()
1968b 63 6f 75 6c 64 20 64 6f 20 55 54 46 2d 31 36 2e could do UTF-16.
1968c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66 67 20 26 . */. if( fg &
1968d 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 MEM_Int ){.
1968e 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1968f 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c (nByte, pMem->z,
19690 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 "%lld", pMem->u
19691 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 .i);. }else{.
19692 20 20 61 73 73 65 72 74 28 20 66 67 20 26 20 4d assert( fg & M
19693 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20 73 EM_Real );. s
19694 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
19695 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 nByte, pMem->z,
19696 22 25 21 2e 31 35 67 22 2c 20 70 4d 65 6d 2d 3e "%!.15g", pMem->
19697 72 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e r);. }. pMem->
19698 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 n = sqlite3Strle
19699 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 n30(pMem->z);.
1969a 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pMem->enc = SQLI
1969b 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d 2d TE_UTF8;. pMem-
1969c 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 >flags |= MEM_St
1969d 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73 71 r|MEM_Term;. sq
1969e 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
1969f 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e ncoding(pMem, en
196a0 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b c);. return rc;
196a1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 .}../*.** Memory
196a2 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 61 cell pMem conta
196a3 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74 20 ins the context
196a4 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 of an aggregate
196a5 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 function..** Thi
196a6 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20 s routine calls
196a7 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 74 the finalize met
196a8 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75 6e hod for that fun
196a9 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 72 ction. The.** r
196aa 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 67 esult of the agg
196ab 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 64 regate is stored
196ac 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e back into pMem.
196ad 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 .**.** Return SQ
196ae 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 68 LITE_ERROR if th
196af 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70 6f e finalizer repo
196b0 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 53 rts an error. S
196b1 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65 QLITE_OK.** othe
196b2 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 rwise..*/.SQLITE
196b3 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
196b4 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c ite3VdbeMemFinal
196b5 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46 ize(Mem *pMem, F
196b6 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b 0a uncDef *pFunc){.
196b7 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
196b8 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 41 4c 57 41 E_OK;. if( ALWA
196b9 59 53 28 70 46 75 6e 63 20 26 26 20 70 46 75 6e YS(pFunc && pFun
196ba 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 29 20 29 7b c->xFinalize) ){
196bb 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e . sqlite3_con
196bc 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 61 73 text ctx;. as
196bd 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
196be 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d gs & MEM_Null)!=
196bf 30 20 7c 7c 20 70 46 75 6e 63 3d 3d 70 4d 65 6d 0 || pFunc==pMem
196c0 2d 3e 75 2e 70 44 65 66 20 29 3b 0a 20 20 20 20 ->u.pDef );.
196c1 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 assert( pMem->db
196c2 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
196c3 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e utex_held(pMem->
196c4 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
196c5 20 20 6d 65 6d 73 65 74 28 26 63 74 78 2c 20 30 memset(&ctx, 0
196c6 2c 20 73 69 7a 65 6f 66 28 63 74 78 29 29 3b 0a , sizeof(ctx));.
196c7 20 20 20 20 63 74 78 2e 73 2e 66 6c 61 67 73 20 ctx.s.flags
196c8 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 = MEM_Null;.
196c9 63 74 78 2e 73 2e 64 62 20 3d 20 70 4d 65 6d 2d ctx.s.db = pMem-
196ca 3e 64 62 3b 0a 20 20 20 20 63 74 78 2e 70 4d 65 >db;. ctx.pMe
196cb 6d 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20 63 74 m = pMem;. ct
196cc 78 2e 70 46 75 6e 63 20 3d 20 70 46 75 6e 63 3b x.pFunc = pFunc;
196cd 0a 20 20 20 20 70 46 75 6e 63 2d 3e 78 46 69 6e . pFunc->xFin
196ce 61 6c 69 7a 65 28 26 63 74 78 29 3b 0a 20 20 20 alize(&ctx);.
196cf 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 4d 65 assert( 0==(pMe
196d0 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e m->flags&MEM_Dyn
196d1 29 20 26 26 20 21 70 4d 65 6d 2d 3e 78 44 65 6c ) && !pMem->xDel
196d2 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 );. sqlite3D
196d3 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 bFree(pMem->db,
196d4 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a pMem->zMalloc);.
196d5 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2c memcpy(pMem,
196d6 20 26 63 74 78 2e 73 2c 20 73 69 7a 65 6f 66 28 &ctx.s, sizeof(
196d7 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 20 ctx.s));. rc
196d8 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 = ctx.isError;.
196d9 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
196da 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 }../*.** If the
196db 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 63 6f 6e 74 memory cell cont
196dc 61 69 6e 73 20 61 20 73 74 72 69 6e 67 20 76 61 ains a string va
196dd 6c 75 65 20 74 68 61 74 20 6d 75 73 74 20 62 65 lue that must be
196de 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 69 6e 76 freed by.** inv
196df 6f 6b 69 6e 67 20 61 6e 20 65 78 74 65 72 6e 61 oking an externa
196e0 6c 20 63 61 6c 6c 62 61 63 6b 2c 20 66 72 65 65 l callback, free
196e1 20 69 74 20 6e 6f 77 2e 20 43 61 6c 6c 69 6e 67 it now. Calling
196e2 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a this function.*
196e3 2a 20 64 6f 65 73 20 6e 6f 74 20 66 72 65 65 20 * does not free
196e4 61 6e 79 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 any Mem.zMalloc
196e5 62 75 66 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 buffer..*/.SQLIT
196e6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
196e7 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
196e8 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d easeExternal(Mem
196e9 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
196ea 70 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 p->db==0 || sqli
196eb 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
196ec 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
196ed 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 66 testcase( p->f
196ee 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 20 29 lags & MEM_Agg )
196ef 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 2d ;. testcase( p-
196f0 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e >flags & MEM_Dyn
196f1 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
196f2 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 p->flags & MEM_R
196f3 6f 77 53 65 74 20 29 3b 0a 20 20 74 65 73 74 63 owSet );. testc
196f4 61 73 65 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 ase( p->flags &
196f5 4d 45 4d 5f 46 72 61 6d 65 20 29 3b 0a 20 20 69 MEM_Frame );. i
196f6 66 28 20 70 2d 3e 66 6c 61 67 73 26 28 4d 45 4d f( p->flags&(MEM
196f7 5f 41 67 67 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d _Agg|MEM_Dyn|MEM
196f8 5f 52 6f 77 53 65 74 7c 4d 45 4d 5f 46 72 61 6d _RowSet|MEM_Fram
196f9 65 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d e) ){. if( p-
196fa 3e 66 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20 29 >flags&MEM_Agg )
196fb 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
196fc 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 dbeMemFinalize(p
196fd 2c 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 20 , p->u.pDef);.
196fe 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e assert( (p->
196ff 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 flags & MEM_Agg)
19700 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c ==0 );. sql
19701 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
19702 73 65 28 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 se(p);. }else
19703 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 if( p->flags&ME
19704 4d 5f 44 79 6e 20 26 26 20 70 2d 3e 78 44 65 6c M_Dyn && p->xDel
19705 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
19706 28 20 28 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f ( (p->flags&MEM_
19707 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 RowSet)==0 );.
19708 20 20 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69 p->xDel((voi
19709 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 20 d *)p->z);.
1970a 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 p->xDel = 0;.
1970b 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 }else if( p->f
1970c 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 20 lags&MEM_RowSet
1970d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1970e 52 6f 77 53 65 74 43 6c 65 61 72 28 70 2d 3e 75 RowSetClear(p->u
1970f 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d .pRowSet);. }
19710 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 else if( p->flag
19711 73 26 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a 20 s&MEM_Frame ){.
19712 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
19713 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 29 3b 0a 20 MemSetNull(p);.
19714 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
19715 2a 20 52 65 6c 65 61 73 65 20 61 6e 79 20 6d 65 * Release any me
19716 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 65 mory held by the
19717 20 4d 65 6d 2e 20 54 68 69 73 20 6d 61 79 20 6c Mem. This may l
19718 65 61 76 65 20 74 68 65 20 4d 65 6d 20 69 6e 20 eave the Mem in
19719 61 6e 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 an.** inconsiste
1971a 6e 74 20 73 74 61 74 65 2c 20 66 6f 72 20 65 78 nt state, for ex
1971b 61 6d 70 6c 65 20 77 69 74 68 20 28 4d 65 6d 2e ample with (Mem.
1971c 7a 3d 3d 30 29 20 61 6e 64 0a 2a 2a 20 28 4d 65 z==0) and.** (Me
1971d 6d 2e 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54 m.type==SQLITE_T
1971e 45 58 54 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f EXT)..*/.SQLITE_
1971f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19720 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
19721 73 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 73 71 se(Mem *p){. sq
19722 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
19723 61 73 65 45 78 74 65 72 6e 61 6c 28 70 29 3b 0a aseExternal(p);.
19724 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
19725 70 2d 3e 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f p->db, p->zMallo
19726 63 29 3b 0a 20 20 70 2d 3e 7a 20 3d 20 30 3b 0a c);. p->z = 0;.
19727 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 p->zMalloc = 0
19728 3b 0a 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b ;. p->xDel = 0;
19729 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 .}../*.** Conver
1972a 74 20 61 20 36 34 2d 62 69 74 20 49 45 45 45 20 t a 64-bit IEEE
1972b 64 6f 75 62 6c 65 20 69 6e 74 6f 20 61 20 36 34 double into a 64
1972c 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 -bit signed inte
1972d 67 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 ger..** If the d
1972e 6f 75 62 6c 65 20 69 73 20 74 6f 6f 20 6c 61 72 ouble is too lar
1972f 67 65 2c 20 72 65 74 75 72 6e 20 30 78 38 30 30 ge, return 0x800
19730 30 30 30 30 30 30 30 30 30 30 30 30 30 2e 0a 2a 0000000000000..*
19731 2a 0a 2a 2a 20 4d 6f 73 74 20 73 79 73 74 65 6d *.** Most system
19732 73 20 61 70 70 65 61 72 20 74 6f 20 64 6f 20 74 s appear to do t
19733 68 69 73 20 73 69 6d 70 6c 79 20 62 79 20 61 73 his simply by as
19734 73 69 67 6e 69 6e 67 0a 2a 2a 20 76 61 72 69 61 signing.** varia
19735 62 6c 65 73 20 61 6e 64 20 77 69 74 68 6f 75 74 bles and without
19736 20 74 68 65 20 65 78 74 72 61 20 72 61 6e 67 65 the extra range
19737 20 74 65 73 74 73 2e 20 20 42 75 74 0a 2a 2a 20 tests. But.**
19738 74 68 65 72 65 20 61 72 65 20 72 65 70 6f 72 74 there are report
19739 73 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20 74 s that windows t
1973a 68 72 6f 77 73 20 61 6e 20 65 78 70 65 63 74 69 hrows an expecti
1973b 6f 6e 0a 2a 2a 20 69 66 20 74 68 65 20 66 6c 6f on.** if the flo
1973c 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 ating point valu
1973d 65 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 e is out of rang
1973e 65 2e 20 28 53 65 65 20 74 69 63 6b 65 74 20 23 e. (See ticket #
1973f 32 38 38 30 2e 29 0a 2a 2a 20 42 65 63 61 75 73 2880.).** Becaus
19740 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 6f 6d 70 e we do not comp
19741 6c 65 74 65 6c 79 20 75 6e 64 65 72 73 74 61 6e letely understan
19742 64 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20 77 d the problem, w
19743 65 20 77 69 6c 6c 0a 2a 2a 20 74 61 6b 65 20 74 e will.** take t
19744 68 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65 20 he conservative
19745 61 70 70 72 6f 61 63 68 20 61 6e 64 20 61 6c 77 approach and alw
19746 61 79 73 20 64 6f 20 72 61 6e 67 65 20 74 65 73 ays do range tes
19747 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 61 74 74 ts.** before att
19748 65 6d 70 74 69 6e 67 20 74 68 65 20 63 6f 6e 76 empting the conv
19749 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 ersion..*/.stati
1974a 63 20 69 36 34 20 64 6f 75 62 6c 65 54 6f 49 6e c i64 doubleToIn
1974b 74 36 34 28 64 6f 75 62 6c 65 20 72 29 7b 0a 20 t64(double r){.
1974c 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6e 79 20 63 6f /*. ** Many co
1974d 6d 70 69 6c 65 72 73 20 77 65 20 65 6e 63 6f 75 mpilers we encou
1974e 6e 74 65 72 20 64 6f 20 6e 6f 74 20 64 65 66 69 nter do not defi
1974f 6e 65 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72 ne constants for
19750 20 74 68 65 0a 20 20 2a 2a 20 6d 69 6e 69 6d 75 the. ** minimu
19751 6d 20 61 6e 64 20 6d 61 78 69 6d 75 6d 20 36 34 m and maximum 64
19752 2d 62 69 74 20 69 6e 74 65 67 65 72 73 2c 20 6f -bit integers, o
19753 72 20 74 68 65 79 20 64 65 66 69 6e 65 20 74 68 r they define th
19754 65 6d 0a 20 20 2a 2a 20 69 6e 63 6f 6e 73 69 73 em. ** inconsis
19755 74 65 6e 74 6c 79 2e 20 20 41 6e 64 20 6d 61 6e tently. And man
19756 79 20 64 6f 20 6e 6f 74 20 75 6e 64 65 72 73 74 y do not underst
19757 61 6e 64 20 74 68 65 20 22 4c 4c 22 20 6e 6f 74 and the "LL" not
19758 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 77 ation.. ** So w
19759 65 20 64 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e e define our own
1975a 20 73 74 61 74 69 63 20 63 6f 6e 73 74 61 6e 74 static constant
1975b 73 20 68 65 72 65 20 75 73 69 6e 67 20 6e 6f 74 s here using not
1975c 68 69 6e 67 0a 20 20 2a 2a 20 6c 61 72 67 65 72 hing. ** larger
1975d 20 74 68 61 6e 20 61 20 33 32 2d 62 69 74 20 69 than a 32-bit i
1975e 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e nteger constant.
1975f 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 . */. static c
19760 6f 6e 73 74 20 69 36 34 20 6d 61 78 49 6e 74 20 onst i64 maxInt
19761 3d 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 3b = LARGEST_INT64;
19762 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
19763 69 36 34 20 6d 69 6e 49 6e 74 20 3d 20 53 4d 41 i64 minInt = SMA
19764 4c 4c 45 53 54 5f 49 4e 54 36 34 3b 0a 0a 20 20 LLEST_INT64;..
19765 69 66 28 20 72 3c 28 64 6f 75 62 6c 65 29 6d 69 if( r<(double)mi
19766 6e 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75 nInt ){. retu
19767 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c rn minInt;. }el
19768 73 65 20 69 66 28 20 72 3e 28 64 6f 75 62 6c 65 se if( r>(double
19769 29 6d 61 78 49 6e 74 20 29 7b 0a 20 20 20 20 2f )maxInt ){. /
1976a 2a 20 6d 69 6e 49 6e 74 20 69 73 20 63 6f 72 72 * minInt is corr
1976b 65 63 74 20 68 65 72 65 20 2d 20 6e 6f 74 20 6d ect here - not m
1976c 61 78 49 6e 74 2e 20 20 49 74 20 74 75 72 6e 73 axInt. It turns
1976d 20 6f 75 74 20 74 68 61 74 20 61 73 73 69 67 6e out that assign
1976e 69 6e 67 0a 20 20 20 20 2a 2a 20 61 20 76 65 72 ing. ** a ver
1976f 79 20 6c 61 72 67 65 20 70 6f 73 69 74 69 76 65 y large positive
19770 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 69 6e number to an in
19771 74 65 67 65 72 20 72 65 73 75 6c 74 73 20 69 6e teger results in
19772 20 61 20 76 65 72 79 20 6c 61 72 67 65 0a 20 20 a very large.
19773 20 20 2a 2a 20 6e 65 67 61 74 69 76 65 20 69 6e ** negative in
19774 74 65 67 65 72 2e 20 20 54 68 69 73 20 6d 61 6b teger. This mak
19775 65 73 20 6e 6f 20 73 65 6e 73 65 2c 20 62 75 74 es no sense, but
19776 20 69 74 20 69 73 20 77 68 61 74 20 78 38 36 20 it is what x86
19777 68 61 72 64 77 61 72 65 0a 20 20 20 20 2a 2a 20 hardware. **
19778 64 6f 65 73 20 73 6f 20 66 6f 72 20 63 6f 6d 70 does so for comp
19779 61 74 69 62 69 6c 69 74 79 20 77 65 20 77 69 6c atibility we wil
1977a 6c 20 64 6f 20 74 68 65 20 73 61 6d 65 20 69 6e l do the same in
1977b 20 73 6f 66 74 77 61 72 65 2e 20 2a 2f 0a 20 20 software. */.
1977c 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74 3b return minInt;
1977d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
1977e 74 75 72 6e 20 28 69 36 34 29 72 3b 0a 20 20 7d turn (i64)r;. }
1977f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
19780 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 69 6e some kind of in
19781 74 65 67 65 72 20 76 61 6c 75 65 20 77 68 69 63 teger value whic
19782 68 20 69 73 20 74 68 65 20 62 65 73 74 20 77 65 h is the best we
19783 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 74 20 72 65 can do.** at re
19784 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 76 presenting the v
19785 61 6c 75 65 20 74 68 61 74 20 2a 70 4d 65 6d 20 alue that *pMem
19786 64 65 73 63 72 69 62 65 73 20 61 73 20 61 6e 20 describes as an
19787 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66 20 70 integer..** If p
19788 4d 65 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 Mem is an intege
19789 72 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 r, then the valu
1978a 65 20 69 73 20 65 78 61 63 74 2e 20 20 49 66 20 e is exact. If
1978b 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 20 66 6c 6f pMem is.** a flo
1978c 61 74 69 6e 67 2d 70 6f 69 6e 74 20 74 68 65 6e ating-point then
1978d 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 the value retur
1978e 6e 65 64 20 69 73 20 74 68 65 20 69 6e 74 65 67 ned is the integ
1978f 65 72 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 70 er part..** If p
19790 4d 65 6d 20 69 73 20 61 20 73 74 72 69 6e 67 20 Mem is a string
19791 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 77 65 or blob, then we
19792 20 6d 61 6b 65 20 61 6e 20 61 74 74 65 6d 70 74 make an attempt
19793 20 74 6f 20 63 6f 6e 76 65 72 74 0a 2a 2a 20 69 to convert.** i
19794 74 20 69 6e 74 6f 20 61 20 69 6e 74 65 67 65 72 t into a integer
19795 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 61 74 and return that
19796 2e 20 20 49 66 20 70 4d 65 6d 20 72 65 70 72 65 . If pMem repre
19797 73 65 6e 74 73 20 61 6e 0a 2a 2a 20 61 6e 20 53 sents an.** an S
19798 51 4c 2d 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 72 QL-NULL value, r
19799 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 eturn 0..**.** I
1979a 66 20 70 4d 65 6d 20 72 65 70 72 65 73 65 6e 74 f pMem represent
1979b 73 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75 65 s a string value
1979c 2c 20 69 74 73 20 65 6e 63 6f 64 69 6e 67 20 6d , its encoding m
1979d 69 67 68 74 20 62 65 20 63 68 61 6e 67 65 64 2e ight be changed.
1979e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1979f 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 64 TE i64 sqlite3Vd
197a0 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 20 2a beIntValue(Mem *
197a1 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 pMem){. int fla
197a2 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d gs;. assert( pM
197a3 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
197a4 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
197a5 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
197a6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 );. assert( EI
197a7 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
197a8 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 66 6c NT(pMem) );. fl
197a9 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 ags = pMem->flag
197aa 73 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 s;. if( flags &
197ab 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 MEM_Int ){.
197ac 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 75 2e 69 return pMem->u.i
197ad 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c ;. }else if( fl
197ae 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 ags & MEM_Real )
197af 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 6f 75 {. return dou
197b0 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d bleToInt64(pMem-
197b1 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 >r);. }else if(
197b2 20 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 flags & (MEM_St
197b3 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 r|MEM_Blob) ){.
197b4 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 i64 value;.
197b5 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d pMem->flags |=
197b6 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 MEM_Str;. if
197b7 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 ( sqlite3VdbeCha
197b8 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d ngeEncoding(pMem
197b9 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 , SQLITE_UTF8).
197ba 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 || sqlite3
197bb 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e VdbeMemNulTermin
197bc 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 ate(pMem) ){.
197bd 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 return 0;.
197be 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
197bf 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71 Mem->z );. sq
197c0 6c 69 74 65 33 41 74 6f 69 36 34 28 70 4d 65 6d lite3Atoi64(pMem
197c1 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 ->z, &value);.
197c2 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 3b 0a return value;.
197c3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
197c4 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a urn 0;. }.}../*
197c5 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 62 .** Return the b
197c6 65 73 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 est representati
197c7 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 68 61 74 20 on of pMem that
197c8 77 65 20 63 61 6e 20 67 65 74 20 69 6e 74 6f 20 we can get into
197c9 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 49 66 a.** double. If
197ca 20 70 4d 65 6d 20 69 73 20 61 6c 72 65 61 64 79 pMem is already
197cb 20 61 20 64 6f 75 62 6c 65 20 6f 72 20 61 6e 20 a double or an
197cc 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 integer, return
197cd 69 74 73 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 49 its.** value. I
197ce 66 20 69 74 20 69 73 20 61 20 73 74 72 69 6e 67 f it is a string
197cf 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f or blob, try to
197d0 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 convert it to a
197d1 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66 20 69 double..** If i
197d2 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72 65 74 t is a NULL, ret
197d3 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51 4c 49 urn 0.0..*/.SQLI
197d4 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62 6c TE_PRIVATE doubl
197d5 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 e sqlite3VdbeRea
197d6 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d lValue(Mem *pMem
197d7 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 ){. assert( pMe
197d8 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 m->db==0 || sqli
197d9 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
197da 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 Mem->db->mutex)
197db 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 );. assert( EIG
197dc 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e HT_BYTE_ALIGNMEN
197dd 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 69 66 28 T(pMem) );. if(
197de 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d pMem->flags & M
197df 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 EM_Real ){. r
197e0 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b 0a 20 eturn pMem->r;.
197e1 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d }else if( pMem-
197e2 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 >flags & MEM_Int
197e3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 ){. return (
197e4 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 double)pMem->u.i
197e5 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d ;. }else if( pM
197e6 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d em->flags & (MEM
197e7 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 _Str|MEM_Blob) )
197e8 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 {. /* (double
197e9 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 )0 In case of SQ
197ea 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
197eb 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 NG_POINT... */.
197ec 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 double val =
197ed 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20 70 (double)0;. p
197ee 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 Mem->flags |= ME
197ef 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20 73 M_Str;. if( s
197f0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
197f1 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53 Encoding(pMem, S
197f2 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20 20 QLITE_UTF8).
197f3 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 || sqlite3Vdb
197f4 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 eMemNulTerminate
197f5 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20 20 (pMem) ){.
197f6 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 /* (double)0 In
197f7 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f case of SQLITE_O
197f8 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
197f9 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 72 NT... */. r
197fa 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29 30 3b eturn (double)0;
197fb 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
197fc 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 t( pMem->z );.
197fd 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 70 4d sqlite3AtoF(pM
197fe 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a 20 20 em->z, &val);.
197ff 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 return val;.
19800 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 28 64 }else{. /* (d
19801 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20 ouble)0 In case
19802 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 of SQLITE_OMIT_F
19803 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e LOATING_POINT...
19804 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 28 */. return (
19805 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 0a 7d 0a double)0;. }.}.
19806 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d 20 73 ./*.** The MEM s
19807 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 72 65 tructure is alre
19808 61 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c 2e 20 ady a MEM_Real.
19809 20 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d 61 6b Try to also mak
1980a 65 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f 49 6e e it a.** MEM_In
1980b 74 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a 2f 0a t if we can..*/.
1980c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1980d 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 oid sqlite3VdbeI
1980e 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d ntegerAffinity(M
1980f 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 em *pMem){. ass
19810 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 ert( pMem->flags
19811 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 & MEM_Real );.
19812 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
19813 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
19814 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 et)==0 );. asse
19815 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 rt( pMem->db==0
19816 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
19817 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e _held(pMem->db->
19818 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
19819 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
1981a 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 LIGNMENT(pMem) )
1981b 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d ;.. pMem->u.i =
1981c 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 doubleToInt64(p
1981d 4d 65 6d 2d 3e 72 29 3b 0a 0a 20 20 2f 2a 20 4f Mem->r);.. /* O
1981e 6e 6c 79 20 6d 61 72 6b 20 74 68 65 20 76 61 6c nly mark the val
1981f 75 65 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72 ue as an integer
19820 20 69 66 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 if. **. **
19821 20 28 31 29 20 74 68 65 20 72 6f 75 6e 64 2d 74 (1) the round-t
19822 72 69 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72 rip conversion r
19823 65 61 6c 2d 3e 69 6e 74 2d 3e 72 65 61 6c 20 69 eal->int->real i
19824 73 20 61 20 6e 6f 2d 6f 70 2c 20 61 6e 64 0a 20 s a no-op, and.
19825 20 2a 2a 20 20 20 20 28 32 29 20 54 68 65 20 69 ** (2) The i
19826 6e 74 65 67 65 72 20 69 73 20 6e 65 69 74 68 65 nteger is neithe
19827 72 20 74 68 65 20 6c 61 72 67 65 73 74 20 6e 6f r the largest no
19828 72 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 0a 20 r the smallest.
19829 20 2a 2a 20 20 20 20 20 20 20 20 70 6f 73 73 69 ** possi
1982a 62 6c 65 20 69 6e 74 65 67 65 72 20 28 74 69 63 ble integer (tic
1982b 6b 65 74 20 23 33 39 32 32 29 0a 20 20 2a 2a 0a ket #3922). **.
1982c 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 ** The second
1982d 61 6e 64 20 74 68 69 72 64 20 74 65 72 6d 73 20 and third terms
1982e 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 in the following
1982f 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 65 6e 66 conditional enf
19830 6f 72 63 65 73 0a 20 20 2a 2a 20 74 68 65 20 73 orces. ** the s
19831 65 63 6f 6e 64 20 63 6f 6e 64 69 74 69 6f 6e 20 econd condition
19832 75 6e 64 65 72 20 74 68 65 20 61 73 73 75 6d 70 under the assump
19833 74 69 6f 6e 20 74 68 61 74 20 61 64 64 69 74 69 tion that additi
19834 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 63 61 75 73 on overflow caus
19835 65 73 0a 20 20 2a 2a 20 76 61 6c 75 65 73 20 74 es. ** values t
19836 6f 20 77 72 61 70 20 61 72 6f 75 6e 64 2e 20 20 o wrap around.
19837 4f 6e 20 78 38 36 20 68 61 72 64 77 61 72 65 2c On x86 hardware,
19838 20 74 68 65 20 74 68 69 72 64 20 74 65 72 6d 20 the third term
19839 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 74 is always. ** t
1983a 72 75 65 20 61 6e 64 20 63 6f 75 6c 64 20 62 65 rue and could be
1983b 20 6f 6d 69 74 74 65 64 2e 20 20 42 75 74 20 77 omitted. But w
1983c 65 20 6c 65 61 76 65 20 69 74 20 69 6e 20 62 65 e leave it in be
1983d 63 61 75 73 65 20 6f 74 68 65 72 0a 20 20 2a 2a cause other. **
1983e 20 61 72 63 68 69 74 65 63 74 75 72 65 73 20 6d architectures m
1983f 69 67 68 74 20 62 65 68 61 76 65 20 64 69 66 66 ight behave diff
19840 65 72 65 6e 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 erently.. */.
19841 69 66 28 20 70 4d 65 6d 2d 3e 72 3d 3d 28 64 6f if( pMem->r==(do
19842 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 20 26 uble)pMem->u.i &
19843 26 20 70 4d 65 6d 2d 3e 75 2e 69 3e 53 4d 41 4c & pMem->u.i>SMAL
19844 4c 45 53 54 5f 49 4e 54 36 34 0a 20 20 20 20 20 LEST_INT64.
19845 20 26 26 20 41 4c 57 41 59 53 28 70 4d 65 6d 2d && ALWAYS(pMem-
19846 3e 75 2e 69 3c 4c 41 52 47 45 53 54 5f 49 4e 54 >u.i<LARGEST_INT
19847 36 34 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 64) ){. pMem-
19848 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e >flags |= MEM_In
19849 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 t;. }.}../*.**
1984a 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f 20 Convert pMem to
1984b 74 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20 49 type integer. I
1984c 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 nvalidate any pr
1984d 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 ior representati
1984e 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ons..*/.SQLITE_P
1984f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19850 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 e3VdbeMemInteger
19851 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a ify(Mem *pMem){.
19852 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
19853 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
19854 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d _mutex_held(pMem
19855 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
19856 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
19857 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
19858 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 Set)==0 );. ass
19859 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f ert( EIGHT_BYTE_
1985a 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 ALIGNMENT(pMem)
1985b 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 );.. pMem->u.i
1985c 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 = sqlite3VdbeInt
1985d 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d Value(pMem);. M
1985e 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d emSetTypeFlag(pM
1985f 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 em, MEM_Int);.
19860 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
19861 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
19862 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 rt pMem so that
19863 69 74 20 69 73 20 6f 66 20 74 79 70 65 20 4d 45 it is of type ME
19864 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c M_Real..** Inval
19865 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 idate any prior
19866 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e representations.
19867 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19868 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
19869 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 6d beMemRealify(Mem
1986a 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 *pMem){. asser
1986b 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c t( pMem->db==0 |
1986c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
1986d 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d held(pMem->db->m
1986e 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
1986f 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c t( EIGHT_BYTE_AL
19870 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b IGNMENT(pMem) );
19871 0a 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 .. pMem->r = sq
19872 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c lite3VdbeRealVal
19873 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 ue(pMem);. MemS
19874 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c etTypeFlag(pMem,
19875 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72 65 MEM_Real);. re
19876 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
19877 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 }../*.** Convert
19878 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 pMem so that it
19879 20 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f 52 has types MEM_R
1987a 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f eal or MEM_Int o
1987b 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c r both..** Inval
1987c 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 idate any prior
1987d 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e representations.
1987e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1987f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
19880 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65 beMemNumerify(Me
19881 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75 62 m *pMem){. doub
19882 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 36 34 le r1, r2;. i64
19883 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 i;. assert( (p
19884 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 Mem->flags & (ME
19885 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d M_Int|MEM_Real|M
19886 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a EM_Null))==0 );.
19887 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
19888 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c >flags & (MEM_Bl
19889 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 20 ob|MEM_Str))!=0
1988a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 );. assert( pMe
1988b 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 m->db==0 || sqli
1988c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1988d 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 Mem->db->mutex)
1988e 29 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65 );. r1 = sqlite
1988f 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 3VdbeRealValue(p
19890 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75 62 Mem);. i = doub
19891 6c 65 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a 20 leToInt64(r1);.
19892 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 69 3b r2 = (double)i;
19893 0a 20 20 69 66 28 20 72 31 3d 3d 72 32 20 29 7b . if( r1==r2 ){
19894 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
19895 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 4d MemIntegerify(pM
19896 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 em);. }else{.
19897 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a pMem->r = r1;.
19898 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c MemSetTypeFl
19899 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 ag(pMem, MEM_Rea
1989a 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e l);. }. return
1989b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
1989c 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 *.** Delete any
1989d 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 previous value a
1989e 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 nd set the value
1989f 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d stored in *pMem
198a0 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c to NULL..*/.SQL
198a1 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
198a2 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
198a3 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d etNull(Mem *pMem
198a4 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 ){. if( pMem->f
198a5 6c 61 67 73 20 26 20 4d 45 4d 5f 46 72 61 6d 65 lags & MEM_Frame
198a6 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
198a7 64 62 65 46 72 61 6d 65 44 65 6c 65 74 65 28 70 dbeFrameDelete(p
198a8 4d 65 6d 2d 3e 75 2e 70 46 72 61 6d 65 29 3b 0a Mem->u.pFrame);.
198a9 20 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e }. if( pMem->
198aa 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
198ab 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 et ){. sqlite
198ac 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d 65 3RowSetClear(pMe
198ad 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 m->u.pRowSet);.
198ae 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 }. MemSetTypeF
198af 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 lag(pMem, MEM_Nu
198b0 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 ll);. pMem->typ
198b1 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b e = SQLITE_NULL;
198b2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
198b3 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 any previous va
198b4 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 lue and set the
198b5 76 61 6c 75 65 20 74 6f 20 62 65 20 61 20 42 4c value to be a BL
198b6 4f 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a 20 OB of length.**
198b7 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c n containing all
198b8 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 zeros..*/.SQLIT
198b9 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
198ba 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
198bb 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d ZeroBlob(Mem *pM
198bc 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73 71 em, int n){. sq
198bd 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
198be 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 ase(pMem);. pMe
198bf 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 m->flags = MEM_B
198c0 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 lob|MEM_Zero;.
198c1 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
198c2 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d ITE_BLOB;. pMem
198c3 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e ->n = 0;. if( n
198c4 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70 4d <0 ) n = 0;. pM
198c5 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b em->u.nZero = n;
198c6 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 . pMem->enc = S
198c7 51 4c 49 54 45 5f 55 54 46 38 3b 0a 0a 23 69 66 QLITE_UTF8;..#if
198c8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
198c9 49 4e 43 52 42 4c 4f 42 0a 20 20 73 71 6c 69 74 INCRBLOB. sqlit
198ca 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d e3VdbeMemGrow(pM
198cb 65 6d 2c 20 6e 2c 20 30 29 3b 0a 20 20 69 66 28 em, n, 0);. if(
198cc 20 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 pMem->z ){.
198cd 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 20 pMem->n = n;.
198ce 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c memset(pMem->z,
198cf 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 0, n);. }.#end
198d0 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 if.}../*.** Dele
198d1 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 te any previous
198d2 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 value and set th
198d3 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 e value stored i
198d4 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a n *pMem to val,.
198d5 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 ** manifest type
198d6 20 49 4e 54 45 47 45 52 2e 0a 2a 2f 0a 53 51 4c INTEGER..*/.SQL
198d7 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
198d8 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
198d9 65 74 49 6e 74 36 34 28 4d 65 6d 20 2a 70 4d 65 etInt64(Mem *pMe
198da 6d 2c 20 69 36 34 20 76 61 6c 29 7b 0a 20 20 73 m, i64 val){. s
198db 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
198dc 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d ease(pMem);. pM
198dd 65 6d 2d 3e 75 2e 69 20 3d 20 76 61 6c 3b 0a 20 em->u.i = val;.
198de 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
198df 45 4d 5f 49 6e 74 3b 0a 20 20 70 4d 65 6d 2d 3e EM_Int;. pMem->
198e0 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e type = SQLITE_IN
198e1 54 45 47 45 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TEGER;.}../*.**
198e2 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69 Delete any previ
198e3 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 ous value and se
198e4 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 t the value stor
198e5 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 ed in *pMem to v
198e6 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 20 al,.** manifest
198e7 74 79 70 65 20 52 45 41 4c 2e 0a 2a 2f 0a 53 51 type REAL..*/.SQ
198e8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
198e9 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
198ea 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 20 2a 70 SetDouble(Mem *p
198eb 4d 65 6d 2c 20 64 6f 75 62 6c 65 20 76 61 6c 29 Mem, double val)
198ec 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 {. if( sqlite3I
198ed 73 4e 61 4e 28 76 61 6c 29 20 29 7b 0a 20 20 20 sNaN(val) ){.
198ee 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
198ef 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20 etNull(pMem);.
198f0 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
198f1 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
198f2 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d (pMem);. pMem
198f3 2d 3e 72 20 3d 20 76 61 6c 3b 0a 20 20 20 20 70 ->r = val;. p
198f4 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
198f5 5f 52 65 61 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d _Real;. pMem-
198f6 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 46 >type = SQLITE_F
198f7 4c 4f 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a LOAT;. }.}../*.
198f8 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 ** Delete any pr
198f9 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 evious value and
198fa 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f set the value o
198fb 66 20 70 4d 65 6d 20 74 6f 20 62 65 20 61 6e 0a f pMem to be an.
198fc 2a 2a 20 65 6d 70 74 79 20 62 6f 6f 6c 65 61 6e ** empty boolean
198fd 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 index..*/.SQLIT
198fe 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
198ff 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
19900 52 6f 77 53 65 74 28 4d 65 6d 20 2a 70 4d 65 6d RowSet(Mem *pMem
19901 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
19902 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20 61 = pMem->db;. a
19903 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a ssert( db!=0 );.
19904 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
19905 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
19906 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c Set)==0 );. sql
19907 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
19908 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d se(pMem);. pMem
19909 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 ->zMalloc = sqli
1990a 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
1990b 62 2c 20 36 34 29 3b 0a 20 20 69 66 28 20 64 62 b, 64);. if( db
1990c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1990d 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 {. pMem->flag
1990e 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
1990f 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
19910 74 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 t( pMem->zMalloc
19911 20 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e );. pMem->u.
19912 70 52 6f 77 53 65 74 20 3d 20 73 71 6c 69 74 65 pRowSet = sqlite
19913 33 52 6f 77 53 65 74 49 6e 69 74 28 64 62 2c 20 3RowSetInit(db,
19914 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 0a pMem->zMalloc, .
19915 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19916 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19917 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
19918 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 70 MallocSize(db, p
19919 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 3b 0a Mem->zMalloc));.
1991a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
1991b 2d 3e 75 2e 70 52 6f 77 53 65 74 21 3d 30 20 29 ->u.pRowSet!=0 )
1991c 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 ;. pMem->flag
1991d 73 20 3d 20 4d 45 4d 5f 52 6f 77 53 65 74 3b 0a s = MEM_RowSet;.
1991e 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
1991f 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 urn true if the
19920 4d 65 6d 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 Mem object conta
19921 69 6e 73 20 61 20 54 45 58 54 20 6f 72 20 42 4c ins a TEXT or BL
19922 4f 42 20 74 68 61 74 20 69 73 0a 2a 2a 20 74 6f OB that is.** to
19923 6f 20 6c 61 72 67 65 20 2d 20 77 68 6f 73 65 20 o large - whose
19924 73 69 7a 65 20 65 78 63 65 65 64 73 20 53 51 4c size exceeds SQL
19925 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a ITE_MAX_LENGTH..
19926 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19927 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
19928 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 20 2a eMemTooBig(Mem *
19929 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d p){. assert( p-
1992a 3e 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 >db!=0 );. if(
1992b 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f p->flags & (MEM_
1992c 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b Str|MEM_Blob) ){
1992d 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e . int n = p->
1992e 6e 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c n;. if( p->fl
1992f 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 ags & MEM_Zero )
19930 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 70 2d 3e {. n += p->
19931 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 0a 20 u.nZero;. }.
19932 20 20 20 72 65 74 75 72 6e 20 6e 3e 70 2d 3e 64 return n>p->d
19933 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
19934 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a _LIMIT_LENGTH];.
19935 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 20 }. return 0;
19936 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f .}../*.** Size o
19937 66 20 73 74 72 75 63 74 20 4d 65 6d 20 6e 6f 74 f struct Mem not
19938 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 4d including the M
19939 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 6d 65 6d 62 65 em.zMalloc membe
1993a 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 r..*/.#define ME
1993b 4d 43 45 4c 4c 53 49 5a 45 20 28 73 69 7a 65 5f MCELLSIZE (size_
1993c 74 29 28 26 28 28 28 4d 65 6d 20 2a 29 30 29 2d t)(&(((Mem *)0)-
1993d 3e 7a 4d 61 6c 6c 6f 63 29 29 0a 0a 2f 2a 0a 2a >zMalloc))../*.*
1993e 2a 20 4d 61 6b 65 20 61 6e 20 73 68 61 6c 6c 6f * Make an shallo
1993f 77 20 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d 20 w copy of pFrom
19940 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f 72 into pTo. Prior
19941 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a 20 contents of.**
19942 70 54 6f 20 61 72 65 20 66 72 65 65 64 2e 20 20 pTo are freed.
19943 54 68 65 20 70 46 72 6f 6d 2d 3e 7a 20 66 69 65 The pFrom->z fie
19944 6c 64 20 69 73 20 6e 6f 74 20 64 75 70 6c 69 63 ld is not duplic
19945 61 74 65 64 2e 20 20 49 66 0a 2a 2a 20 70 46 72 ated. If.** pFr
19946 6f 6d 2d 3e 7a 20 69 73 20 75 73 65 64 2c 20 74 om->z is used, t
19947 68 65 6e 20 70 54 6f 2d 3e 7a 20 70 6f 69 6e 74 hen pTo->z point
19948 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 68 s to the same th
19949 69 6e 67 20 61 73 20 70 46 72 6f 6d 2d 3e 7a 0a ing as pFrom->z.
1994a 2a 2a 20 61 6e 64 20 66 6c 61 67 73 20 67 65 74 ** and flags get
1994b 73 20 73 72 63 54 79 70 65 20 28 65 69 74 68 65 s srcType (eithe
1994c 72 20 4d 45 4d 5f 45 70 68 65 6d 20 6f 72 20 4d r MEM_Ephem or M
1994d 45 4d 5f 53 74 61 74 69 63 29 2e 0a 2a 2f 0a 53 EM_Static)..*/.S
1994e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1994f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 id sqlite3VdbeMe
19950 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d mShallowCopy(Mem
19951 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d *pTo, const Mem
19952 20 2a 70 46 72 6f 6d 2c 20 69 6e 74 20 73 72 63 *pFrom, int src
19953 54 79 70 65 29 7b 0a 20 20 61 73 73 65 72 74 28 Type){. assert(
19954 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 26 (pFrom->flags &
19955 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 MEM_RowSet)==0
19956 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
19957 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e MemReleaseExtern
19958 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 al(pTo);. memcp
19959 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 y(pTo, pFrom, ME
1995a 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 54 MCELLSIZE);. pT
1995b 6f 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 69 o->xDel = 0;. i
1995c 66 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 f( (pFrom->flags
1995d 26 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 7c 7c 20 &MEM_Dyn)!=0 ||
1995e 70 46 72 6f 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d pFrom->z==pFrom-
1995f 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 >zMalloc ){.
19960 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 pTo->flags &= ~(
19961 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 61 74 MEM_Dyn|MEM_Stat
19962 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 ic|MEM_Ephem);.
19963 20 20 20 61 73 73 65 72 74 28 20 73 72 63 54 79 assert( srcTy
19964 70 65 3d 3d 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c pe==MEM_Ephem ||
19965 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 53 74 srcType==MEM_St
19966 61 74 69 63 20 29 3b 0a 20 20 20 20 70 54 6f 2d atic );. pTo-
19967 3e 66 6c 61 67 73 20 7c 3d 20 73 72 63 54 79 70 >flags |= srcTyp
19968 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 e;. }.}../*.**
19969 4d 61 6b 65 20 61 20 66 75 6c 6c 20 63 6f 70 79 Make a full copy
1996a 20 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 of pFrom into p
1996b 54 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 To. Prior conte
1996c 6e 74 73 20 6f 66 20 70 54 6f 20 61 72 65 0a 2a nts of pTo are.*
1996d 2a 20 66 72 65 65 64 20 62 65 66 6f 72 65 20 74 * freed before t
1996e 68 65 20 63 6f 70 79 20 69 73 20 6d 61 64 65 2e he copy is made.
1996f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19970 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
19971 62 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 20 2a 70 beMemCopy(Mem *p
19972 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 To, const Mem *p
19973 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 From){. int rc
19974 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 = SQLITE_OK;..
19975 61 73 73 65 72 74 28 20 28 70 46 72 6f 6d 2d 3e assert( (pFrom->
19976 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
19977 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 et)==0 );. sqli
19978 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
19979 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b 0a eExternal(pTo);.
1997a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 memcpy(pTo, pF
1997b 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 rom, MEMCELLSIZE
1997c 29 3b 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 );. pTo->flags
1997d 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 20 &= ~MEM_Dyn;..
1997e 69 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26 28 if( pTo->flags&(
1997f 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 MEM_Str|MEM_Blob
19980 29 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d ) ){. if( 0==
19981 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 (pFrom->flags&ME
19982 4d 5f 53 74 61 74 69 63 29 20 29 7b 0a 20 20 20 M_Static) ){.
19983 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d pTo->flags |=
19984 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 MEM_Ephem;.
19985 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
19986 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 beMemMakeWriteab
19987 6c 65 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 le(pTo);. }.
19988 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }.. return rc;
19989 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 .}../*.** Transf
1998a 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 er the contents
1998b 6f 66 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 2e of pFrom to pTo.
1998c 20 41 6e 79 20 65 78 69 73 74 69 6e 67 20 76 61 Any existing va
1998d 6c 75 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a 2a lue in pTo is.**
1998e 20 66 72 65 65 64 2e 20 49 66 20 70 46 72 6f 6d freed. If pFrom
1998f 20 63 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d 65 contains epheme
19990 72 61 6c 20 64 61 74 61 2c 20 61 20 63 6f 70 79 ral data, a copy
19991 20 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 is made..**.**
19992 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 61 pFrom contains a
19993 6e 20 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e 20 n SQL NULL when
19994 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 this routine ret
19995 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f urns..*/.SQLITE_
19996 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19997 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 ite3VdbeMemMove(
19998 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a 70 Mem *pTo, Mem *p
19999 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 From){. assert(
1999a 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c pFrom->db==0 ||
1999b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1999c 65 6c 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e 6d eld(pFrom->db->m
1999d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
1999e 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c t( pTo->db==0 ||
1999f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
199a0 65 6c 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 eld(pTo->db->mut
199a1 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
199a2 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c pFrom->db==0 ||
199a3 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 pTo->db==0 || p
199a4 46 72 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64 From->db==pTo->d
199a5 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 56 b );.. sqlite3V
199a6 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 54 dbeMemRelease(pT
199a7 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f o);. memcpy(pTo
199a8 2c 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 28 , pFrom, sizeof(
199a9 4d 65 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d 3e Mem));. pFrom->
199aa 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
199ab 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c 20 ;. pFrom->xDel
199ac 3d 20 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a 4d = 0;. pFrom->zM
199ad 61 6c 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a alloc = 0;.}../*
199ae 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 .** Change the v
199af 61 6c 75 65 20 6f 66 20 61 20 4d 65 6d 20 74 6f alue of a Mem to
199b0 20 62 65 20 61 20 73 74 72 69 6e 67 20 6f 72 20 be a string or
199b1 61 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 a BLOB..**.** Th
199b2 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d e memory managem
199b3 65 6e 74 20 73 74 72 61 74 65 67 79 20 64 65 70 ent strategy dep
199b4 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 ends on the valu
199b5 65 20 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a 2a e of the xDel.**
199b6 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20 74 parameter. If t
199b7 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 he value passed
199b8 69 73 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 is SQLITE_TRANSI
199b9 45 4e 54 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a ENT, then the .*
199ba 2a 20 73 74 72 69 6e 67 20 69 73 20 63 6f 70 69 * string is copi
199bb 65 64 20 69 6e 74 6f 20 61 20 28 70 6f 73 73 69 ed into a (possi
199bc 62 6c 79 20 65 78 69 73 74 69 6e 67 29 20 62 75 bly existing) bu
199bd 66 66 65 72 20 6d 61 6e 61 67 65 64 20 62 79 20 ffer managed by
199be 74 68 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72 75 the .** Mem stru
199bf 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65 cture. Otherwise
199c0 2c 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 62 , any existing b
199c1 75 66 66 65 72 20 69 73 20 66 72 65 65 64 20 61 uffer is freed a
199c2 6e 64 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 nd the.** pointe
199c3 72 20 63 6f 70 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 r copied..**.**
199c4 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 If the string is
199c5 20 74 6f 6f 20 6c 61 72 67 65 20 28 69 66 20 69 too large (if i
199c6 74 20 65 78 63 65 65 64 73 20 74 68 65 20 53 51 t exceeds the SQ
199c7 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
199c8 48 0a 2a 2a 20 73 69 7a 65 20 6c 69 6d 69 74 29 H.** size limit)
199c9 20 74 68 65 6e 20 6e 6f 20 6d 65 6d 6f 72 79 20 then no memory
199ca 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75 72 allocation occur
199cb 73 2e 20 20 49 66 20 74 68 65 20 73 74 72 69 6e s. If the strin
199cc 67 20 63 61 6e 20 62 65 0a 2a 2a 20 73 74 6f 72 g can be.** stor
199cd 65 64 20 77 69 74 68 6f 75 74 20 61 6c 6c 6f 63 ed without alloc
199ce 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 74 68 ating memory, th
199cf 65 6e 20 69 74 20 69 73 2e 20 20 49 66 20 61 20 en it is. If a
199d0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
199d1 6e 0a 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 n.** is required
199d2 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 74 to store the st
199d3 72 69 6e 67 2c 20 74 68 65 6e 20 76 61 6c 75 65 ring, then value
199d4 20 6f 66 20 70 4d 65 6d 20 69 73 20 75 6e 63 68 of pMem is unch
199d5 61 6e 67 65 64 2e 20 20 49 6e 0a 2a 2a 20 65 69 anged. In.** ei
199d6 74 68 65 72 20 63 61 73 65 2c 20 53 51 4c 49 54 ther case, SQLIT
199d7 45 5f 54 4f 4f 42 49 47 20 69 73 20 72 65 74 75 E_TOOBIG is retu
199d8 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rned..*/.SQLITE_
199d9 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
199da 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
199db 28 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 (. Mem *pMem,
199dc 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 /* Memor
199dd 79 20 63 65 6c 6c 20 74 6f 20 73 65 74 20 74 6f y cell to set to
199de 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 2a 2f string value */
199df 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
199e0 2c 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 , /* String
199e1 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e pointer */. in
199e2 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 t n,
199e3 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 73 74 /* Bytes in st
199e4 72 69 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69 76 ring, or negativ
199e5 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 e */. u8 enc,
199e6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e /* En
199e7 63 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 coding of z. 0
199e8 66 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 for BLOBs */. v
199e9 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 oid (*xDel)(void
199ea 2a 29 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 *) /* Destructor
199eb 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a function */.){.
199ec 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 6e 3b int nByte = n;
199ed 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c /* New val
199ee 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a ue for pMem->n *
199ef 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 20 /. int iLimit;
199f0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d /* Maxim
199f1 75 6d 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e um allowed strin
199f2 67 20 6f 72 20 62 6c 6f 62 20 73 69 7a 65 20 2a g or blob size *
199f3 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 20 3d 20 /. u16 flags =
199f4 30 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 0; /* New v
199f5 61 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 alue for pMem->f
199f6 6c 61 67 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 lags */.. asser
199f7 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c t( pMem->db==0 |
199f8 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
199f9 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d held(pMem->db->m
199fa 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
199fb 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 t( (pMem->flags
199fc 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 & MEM_RowSet)==0
199fd 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 7a 20 69 );.. /* If z i
199fe 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 s a NULL pointer
199ff 2c 20 73 65 74 20 70 4d 65 6d 20 74 6f 20 63 6f , set pMem to co
19a00 6e 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55 4c ntain an SQL NUL
19a01 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 21 7a 20 29 L. */. if( !z )
19a02 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
19a03 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 6d eMemSetNull(pMem
19a04 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
19a05 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 LITE_OK;. }..
19a06 69 66 28 20 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a if( pMem->db ){.
19a07 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 4d 65 iLimit = pMe
19a08 6d 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 m->db->aLimit[SQ
19a09 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
19a0a 48 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 H];. }else{.
19a0b 20 69 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 iLimit = SQLITE
19a0c 5f 4d 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d _MAX_LENGTH;. }
19a0d 0a 20 20 66 6c 61 67 73 20 3d 20 28 65 6e 63 3d . flags = (enc=
19a0e 3d 30 3f 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f =0?MEM_Blob:MEM_
19a0f 53 74 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 Str);. if( nByt
19a10 65 3c 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 e<0 ){. asser
19a11 74 28 20 65 6e 63 21 3d 30 20 29 3b 0a 20 20 20 t( enc!=0 );.
19a12 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 if( enc==SQLITE
19a13 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 20 20 66 _UTF8 ){. f
19a14 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 or(nByte=0; nByt
19a15 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 7a 5b 6e e<=iLimit && z[n
19a16 42 79 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b Byte]; nByte++){
19a17 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
19a18 20 20 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 for(nByte=0;
19a19 6e 42 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 nByte<=iLimit &&
19a1a 20 28 7a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e (z[nByte] | z[n
19a1b 42 79 74 65 2b 31 5d 29 3b 20 6e 42 79 74 65 2b Byte+1]); nByte+
19a1c 3d 32 29 7b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 =2){}. }.
19a1d 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 flags |= MEM_Ter
19a1e 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 m;. }.. /* The
19a1f 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b following block
19a20 20 73 65 74 73 20 74 68 65 20 6e 65 77 20 76 61 sets the new va
19a21 6c 75 65 73 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e lues of Mem.z an
19a22 64 20 4d 65 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 d Mem.xDel. It.
19a23 20 2a 2a 20 61 6c 73 6f 20 73 65 74 73 20 61 20 ** also sets a
19a24 66 6c 61 67 20 69 6e 20 6c 6f 63 61 6c 20 76 61 flag in local va
19a25 72 69 61 62 6c 65 20 22 66 6c 61 67 73 22 20 74 riable "flags" t
19a26 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 6d o indicate the m
19a27 65 6d 6f 72 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 emory. ** manag
19a28 65 6d 65 6e 74 20 28 6f 6e 65 20 6f 66 20 4d 45 ement (one of ME
19a29 4d 5f 44 79 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 M_Dyn or MEM_Sta
19a2a 74 69 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 tic).. */. if(
19a2b 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 xDel==SQLITE_TR
19a2c 41 4e 53 49 45 4e 54 20 29 7b 0a 20 20 20 20 69 ANSIENT ){. i
19a2d 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 nt nAlloc = nByt
19a2e 65 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 e;. if( flags
19a2f 26 4d 45 4d 5f 54 65 72 6d 20 29 7b 0a 20 20 20 &MEM_Term ){.
19a30 20 20 20 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e nAlloc += (en
19a31 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 c==SQLITE_UTF8?1
19a32 3a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 :2);. }. i
19a33 66 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 f( nByte>iLimit
19a34 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
19a35 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 SQLITE_TOOBIG;.
19a36 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c }. if( sql
19a37 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
19a38 70 4d 65 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 pMem, nAlloc, 0)
19a39 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
19a3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
19a3b 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 }. memcpy(
19a3c 70 4d 65 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c pMem->z, z, nAll
19a3d 6f 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 oc);. }else if(
19a3e 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 xDel==SQLITE_DY
19a3f 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 73 71 6c NAMIC ){. sql
19a40 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
19a41 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d se(pMem);. pM
19a42 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d em->zMalloc = pM
19a43 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29 em->z = (char *)
19a44 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65 z;. pMem->xDe
19a45 6c 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a l = 0;. }else{.
19a46 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
19a47 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b emRelease(pMem);
19a48 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 . pMem->z = (
19a49 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 70 4d char *)z;. pM
19a4a 65 6d 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b em->xDel = xDel;
19a4b 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 28 28 . flags |= ((
19a4c 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54 41 xDel==SQLITE_STA
19a4d 54 49 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63 3a TIC)?MEM_Static:
19a4e 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 7d 0a 0a 20 MEM_Dyn);. }..
19a4f 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 pMem->n = nByte
19a50 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 ;. pMem->flags
19a51 3d 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d = flags;. pMem-
19a52 3e 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f >enc = (enc==0 ?
19a53 20 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 65 SQLITE_UTF8 : e
19a54 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 nc);. pMem->typ
19a55 65 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 e = (enc==0 ? SQ
19a56 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 LITE_BLOB : SQLI
19a57 54 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64 TE_TEXT);..#ifnd
19a58 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 ef SQLITE_OMIT_U
19a59 54 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d TF16. if( pMem-
19a5a 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 >enc!=SQLITE_UTF
19a5b 38 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 8 && sqlite3Vdbe
19a5c 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 MemHandleBom(pMe
19a5d 6d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e m) ){. return
19a5e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
19a5f 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 }.#endif.. if(
19a60 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b nByte>iLimit ){
19a61 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
19a62 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a TE_TOOBIG;. }..
19a63 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
19a64 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d OK;.}../*.** Com
19a65 70 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 pare the values
19a66 63 6f 6e 74 61 69 6e 65 64 20 62 79 20 74 68 65 contained by the
19a67 20 74 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c two memory cell
19a68 73 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 s, returning.**
19a69 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 20 6f negative, zero o
19a6a 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 70 4d r positive if pM
19a6b 65 6d 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e em1 is less than
19a6c 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 , equal to, or g
19a6d 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 70 reater.** than p
19a6e 4d 65 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f 72 Mem2. Sorting or
19a6f 64 65 72 20 69 73 20 4e 55 4c 4c 27 73 20 66 69 der is NULL's fi
19a70 72 73 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 rst, followed by
19a71 20 6e 75 6d 62 65 72 73 20 28 69 6e 74 65 67 65 numbers (intege
19a72 72 73 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73 29 rs.** and reals)
19a73 20 73 6f 72 74 65 64 20 6e 75 6d 65 72 69 63 61 sorted numerica
19a74 6c 6c 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 lly, followed by
19a75 20 74 65 78 74 20 6f 72 64 65 72 65 64 20 62 79 text ordered by
19a76 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a the collating.*
19a77 2a 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c * sequence pColl
19a78 20 61 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c 6f and finally blo
19a79 62 27 73 20 6f 72 64 65 72 65 64 20 62 79 20 6d b's ordered by m
19a7a 65 6d 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 emcmp()..**.** T
19a7b 77 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61 wo NULL values a
19a7c 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 65 71 re considered eq
19a7d 75 61 6c 20 62 79 20 74 68 69 73 20 66 75 6e 63 ual by this func
19a7e 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
19a7f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19a80 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f te3MemCompare(co
19a81 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 nst Mem *pMem1,
19a82 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 const Mem *pMem2
19a83 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 20 , const CollSeq
19a84 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 *pColl){. int r
19a85 63 3b 0a 20 20 69 6e 74 20 66 31 2c 20 66 32 3b c;. int f1, f2;
19a86 0a 20 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64 5f . int combined_
19a87 66 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 49 6e 74 flags;.. /* Int
19a88 65 72 63 68 61 6e 67 65 20 70 4d 65 6d 31 20 61 erchange pMem1 a
19a89 6e 64 20 70 4d 65 6d 32 20 69 66 20 74 68 65 20 nd pMem2 if the
19a8a 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
19a8b 63 65 20 73 70 65 63 69 66 69 65 73 0a 20 20 2a ce specifies. *
19a8c 2a 20 44 45 53 43 20 6f 72 64 65 72 2e 0a 20 20 * DESC order..
19a8d 2a 2f 0a 20 20 66 31 20 3d 20 70 4d 65 6d 31 2d */. f1 = pMem1-
19a8e 3e 66 6c 61 67 73 3b 0a 20 20 66 32 20 3d 20 70 >flags;. f2 = p
19a8f 4d 65 6d 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 Mem2->flags;. c
19a90 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 3d 20 ombined_flags =
19a91 66 31 7c 66 32 3b 0a 20 20 61 73 73 65 72 74 28 f1|f2;. assert(
19a92 20 28 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 (combined_flags
19a93 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d & MEM_RowSet)==
19a94 30 20 29 3b 0a 20 0a 20 20 2f 2a 20 49 66 20 6f 0 );. . /* If o
19a95 6e 65 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c ne value is NULL
19a96 2c 20 69 74 20 69 73 20 6c 65 73 73 20 74 68 61 , it is less tha
19a97 6e 20 74 68 65 20 6f 74 68 65 72 2e 20 49 66 20 n the other. If
19a98 62 6f 74 68 20 76 61 6c 75 65 73 0a 20 20 2a 2a both values. **
19a99 20 61 72 65 20 4e 55 4c 4c 2c 20 72 65 74 75 72 are NULL, retur
19a9a 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 n 0.. */. if(
19a9b 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d combined_flags&M
19a9c 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 EM_Null ){. r
19a9d 65 74 75 72 6e 20 28 66 32 26 4d 45 4d 5f 4e 75 eturn (f2&MEM_Nu
19a9e 6c 6c 29 20 2d 20 28 66 31 26 4d 45 4d 5f 4e 75 ll) - (f1&MEM_Nu
19a9f 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 ll);. }.. /* I
19aa0 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 61 f one value is a
19aa1 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65 20 number and the
19aa2 6f 74 68 65 72 20 69 73 20 6e 6f 74 2c 20 74 68 other is not, th
19aa3 65 20 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 e number is less
19aa4 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61 .. ** If both a
19aa5 72 65 20 6e 75 6d 62 65 72 73 2c 20 63 6f 6d 70 re numbers, comp
19aa6 61 72 65 20 61 73 20 72 65 61 6c 73 20 69 66 20 are as reals if
19aa7 6f 6e 65 20 69 73 20 61 20 72 65 61 6c 2c 20 6f one is a real, o
19aa8 72 20 61 73 20 69 6e 74 65 67 65 72 73 0a 20 20 r as integers.
19aa9 2a 2a 20 69 66 20 62 6f 74 68 20 76 61 6c 75 65 ** if both value
19aaa 73 20 61 72 65 20 69 6e 74 65 67 65 72 73 2e 0a s are integers..
19aab 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 */. if( combi
19aac 6e 65 64 5f 66 6c 61 67 73 26 28 4d 45 4d 5f 49 ned_flags&(MEM_I
19aad 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 7b 0a nt|MEM_Real) ){.
19aae 20 20 20 20 69 66 28 20 21 28 66 31 26 28 4d 45 if( !(f1&(ME
19aaf 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 M_Int|MEM_Real))
19ab0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
19ab1 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 1;. }. if
19ab2 28 20 21 28 66 32 26 28 4d 45 4d 5f 49 6e 74 7c ( !(f2&(MEM_Int|
19ab3 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20 20 MEM_Real)) ){.
19ab4 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 return -1;.
19ab5 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 31 }. if( (f1
19ab6 20 26 20 66 32 20 26 20 4d 45 4d 5f 49 6e 74 29 & f2 & MEM_Int)
19ab7 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 ==0 ){. dou
19ab8 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 20 20 ble r1, r2;.
19ab9 20 20 69 66 28 20 28 66 31 26 4d 45 4d 5f 52 65 if( (f1&MEM_Re
19aba 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 al)==0 ){.
19abb 20 20 72 31 20 3d 20 28 64 6f 75 62 6c 65 29 70 r1 = (double)p
19abc 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 Mem1->u.i;.
19abd 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
19abe 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 72 3b 0a 20 r1 = pMem1->r;.
19abf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
19ac0 20 28 66 32 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d (f2&MEM_Real)==
19ac1 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 32 20 0 ){. r2
19ac2 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 32 2d = (double)pMem2-
19ac3 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73 >u.i;. }els
19ac4 65 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 e{. r2 =
19ac5 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20 20 20 20 20 pMem2->r;.
19ac6 7d 0a 20 20 20 20 20 20 69 66 28 20 72 31 3c 72 }. if( r1<r
19ac7 32 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 2 ) return -1;.
19ac8 20 20 20 20 20 69 66 28 20 72 31 3e 72 32 20 29 if( r1>r2 )
19ac9 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
19aca 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d return 0;. }
19acb 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 else{. asse
19acc 72 74 28 20 66 31 26 4d 45 4d 5f 49 6e 74 20 29 rt( f1&MEM_Int )
19acd 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
19ace 66 32 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 f2&MEM_Int );.
19acf 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75 if( pMem1->u
19ad0 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 .i < pMem2->u.i
19ad1 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 ) return -1;.
19ad2 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75 2e if( pMem1->u.
19ad3 69 20 3e 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 i > pMem2->u.i )
19ad4 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
19ad5 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d return 0;. }
19ad6 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e . }.. /* If on
19ad7 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 72 e value is a str
19ad8 69 6e 67 20 61 6e 64 20 74 68 65 20 6f 74 68 65 ing and the othe
19ad9 72 20 69 73 20 61 20 62 6c 6f 62 2c 20 74 68 65 r is a blob, the
19ada 20 73 74 72 69 6e 67 20 69 73 20 6c 65 73 73 2e string is less.
19adb 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61 72 . ** If both ar
19adc 65 20 73 74 72 69 6e 67 73 2c 20 63 6f 6d 70 61 e strings, compa
19add 72 65 20 75 73 69 6e 67 20 74 68 65 20 63 6f 6c re using the col
19ade 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 lating functions
19adf 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d .. */. if( com
19ae0 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f bined_flags&MEM_
19ae1 53 74 72 20 29 7b 0a 20 20 20 20 69 66 28 20 28 Str ){. if( (
19ae2 66 31 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 f1 & MEM_Str)==0
19ae3 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
19ae4 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 1;. }. if
19ae5 28 20 28 66 32 20 26 20 4d 45 4d 5f 53 74 72 29 ( (f2 & MEM_Str)
19ae6 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ==0 ){. ret
19ae7 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20 urn -1;. }..
19ae8 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 31 assert( pMem1
19ae9 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32 2d 3e 65 6e ->enc==pMem2->en
19aea 63 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 c );. assert(
19aeb 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c pMem1->enc==SQL
19aec 49 54 45 5f 55 54 46 38 20 7c 7c 20 0a 20 20 20 ITE_UTF8 || .
19aed 20 20 20 20 20 20 20 20 20 70 4d 65 6d 31 2d 3e pMem1->
19aee 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 enc==SQLITE_UTF1
19aef 36 4c 45 20 7c 7c 20 70 4d 65 6d 31 2d 3e 65 6e 6LE || pMem1->en
19af0 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 c==SQLITE_UTF16B
19af1 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 E );.. /* The
19af2 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
19af3 6e 63 65 20 6d 75 73 74 20 62 65 20 64 65 66 69 nce must be defi
19af4 6e 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e ned at this poin
19af5 74 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20 2a t, even if. *
19af6 2a 20 74 68 65 20 75 73 65 72 20 64 65 6c 65 74 * the user delet
19af7 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e es the collation
19af8 20 73 65 71 75 65 6e 63 65 20 61 66 74 65 72 20 sequence after
19af9 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d the vdbe program
19afa 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 69 is. ** compi
19afb 6c 65 64 20 28 74 68 69 73 20 77 61 73 20 6e 6f led (this was no
19afc 74 20 61 6c 77 61 79 73 20 74 68 65 20 63 61 73 t always the cas
19afd 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 e).. */. a
19afe 73 73 65 72 74 28 20 21 70 43 6f 6c 6c 20 7c 7c ssert( !pColl ||
19aff 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b 0a pColl->xCmp );.
19b00 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 . if( pColl )
19b01 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d {. if( pMem
19b02 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 1->enc==pColl->e
19b03 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a nc ){. /*
19b04 20 54 68 65 20 73 74 72 69 6e 67 73 20 61 72 65 The strings are
19b05 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
19b06 63 6f 72 72 65 63 74 20 65 6e 63 6f 64 69 6e 67 correct encoding
19b07 2e 20 20 43 61 6c 6c 20 74 68 65 0a 20 20 20 20 . Call the.
19b08 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f ** compariso
19b09 6e 20 66 75 6e 63 74 69 6f 6e 20 64 69 72 65 63 n function direc
19b0a 74 6c 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 tly */. r
19b0b 65 74 75 72 6e 20 70 43 6f 6c 6c 2d 3e 78 43 6d eturn pColl->xCm
19b0c 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 70 p(pColl->pUser,p
19b0d 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a Mem1->n,pMem1->z
19b0e 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d 65 6d 32 2d ,pMem2->n,pMem2-
19b0f 3e 7a 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 >z);. }else
19b10 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 {. const
19b11 76 6f 69 64 20 2a 76 31 2c 20 2a 76 32 3b 0a 20 void *v1, *v2;.
19b12 20 20 20 20 20 20 20 69 6e 74 20 6e 31 2c 20 6e int n1, n
19b13 32 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 63 2;. Mem c
19b14 31 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 63 1;. Mem c
19b15 32 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 2;. memse
19b16 74 28 26 63 31 2c 20 30 2c 20 73 69 7a 65 6f 66 t(&c1, 0, sizeof
19b17 28 63 31 29 29 3b 0a 20 20 20 20 20 20 20 20 6d (c1));. m
19b18 65 6d 73 65 74 28 26 63 32 2c 20 30 2c 20 73 69 emset(&c2, 0, si
19b19 7a 65 6f 66 28 63 32 29 29 3b 0a 20 20 20 20 20 zeof(c2));.
19b1a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
19b1b 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63 31 mShallowCopy(&c1
19b1c 2c 20 70 4d 65 6d 31 2c 20 4d 45 4d 5f 45 70 68 , pMem1, MEM_Eph
19b1d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c em);. sql
19b1e 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c ite3VdbeMemShall
19b1f 6f 77 43 6f 70 79 28 26 63 32 2c 20 70 4d 65 6d owCopy(&c2, pMem
19b20 32 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 2, MEM_Ephem);.
19b21 20 20 20 20 20 20 20 76 31 20 3d 20 73 71 6c 69 v1 = sqli
19b22 74 65 33 56 61 6c 75 65 54 65 78 74 28 28 73 71 te3ValueText((sq
19b23 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 31 lite3_value*)&c1
19b24 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 , pColl->enc);.
19b25 20 20 20 20 20 20 20 6e 31 20 3d 20 76 31 3d 3d n1 = v1==
19b26 30 20 3f 20 30 20 3a 20 63 31 2e 6e 3b 0a 20 20 0 ? 0 : c1.n;.
19b27 20 20 20 20 20 20 76 32 20 3d 20 73 71 6c 69 74 v2 = sqlit
19b28 65 33 56 61 6c 75 65 54 65 78 74 28 28 73 71 6c e3ValueText((sql
19b29 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 32 2c ite3_value*)&c2,
19b2a 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 pColl->enc);.
19b2b 20 20 20 20 20 20 6e 32 20 3d 20 76 32 3d 3d 30 n2 = v2==0
19b2c 20 3f 20 30 20 3a 20 63 32 2e 6e 3b 0a 20 20 20 ? 0 : c2.n;.
19b2d 20 20 20 20 20 72 63 20 3d 20 70 43 6f 6c 6c 2d rc = pColl-
19b2e 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 >xCmp(pColl->pUs
19b2f 65 72 2c 20 6e 31 2c 20 76 31 2c 20 6e 32 2c 20 er, n1, v1, n2,
19b30 76 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c v2);. sql
19b31 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
19b32 73 65 28 26 63 31 29 3b 0a 20 20 20 20 20 20 20 se(&c1);.
19b33 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
19b34 65 6c 65 61 73 65 28 26 63 32 29 3b 0a 20 20 20 elease(&c2);.
19b35 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
19b36 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
19b37 20 20 2f 2a 20 49 66 20 61 20 4e 55 4c 4c 20 70 /* If a NULL p
19b38 6f 69 6e 74 65 72 20 77 61 73 20 70 61 73 73 65 ointer was passe
19b39 64 20 61 73 20 74 68 65 20 63 6f 6c 6c 61 74 65 d as the collate
19b3a 20 66 75 6e 63 74 69 6f 6e 2c 20 66 61 6c 6c 20 function, fall
19b3b 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74 through. ** t
19b3c 6f 20 74 68 65 20 62 6c 6f 62 20 63 61 73 65 20 o the blob case
19b3d 61 6e 64 20 75 73 65 20 6d 65 6d 63 6d 70 28 29 and use memcmp()
19b3e 2e 20 20 2a 2f 0a 20 20 7d 0a 20 0a 20 20 2f 2a . */. }. . /*
19b3f 20 42 6f 74 68 20 76 61 6c 75 65 73 20 6d 75 73 Both values mus
19b40 74 20 62 65 20 62 6c 6f 62 73 2e 20 20 43 6f 6d t be blobs. Com
19b41 70 61 72 65 20 75 73 69 6e 67 20 6d 65 6d 63 6d pare using memcm
19b42 70 28 29 2e 20 20 2a 2f 0a 20 20 72 63 20 3d 20 p(). */. rc =
19b43 6d 65 6d 63 6d 70 28 70 4d 65 6d 31 2d 3e 7a 2c memcmp(pMem1->z,
19b44 20 70 4d 65 6d 32 2d 3e 7a 2c 20 28 70 4d 65 6d pMem2->z, (pMem
19b45 31 2d 3e 6e 3e 70 4d 65 6d 32 2d 3e 6e 29 3f 70 1->n>pMem2->n)?p
19b46 4d 65 6d 32 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e Mem2->n:pMem1->n
19b47 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29 );. if( rc==0 )
19b48 7b 0a 20 20 20 20 72 63 20 3d 20 70 4d 65 6d 31 {. rc = pMem1
19b49 2d 3e 6e 20 2d 20 70 4d 65 6d 32 2d 3e 6e 3b 0a ->n - pMem2->n;.
19b4a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
19b4b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 64 .}../*.** Move d
19b4c 61 74 61 20 6f 75 74 20 6f 66 20 61 20 62 74 72 ata out of a btr
19b4d 65 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 66 ee key or data f
19b4e 69 65 6c 64 20 61 6e 64 20 69 6e 74 6f 20 61 20 ield and into a
19b4f 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 0a 2a Mem structure..*
19b50 2a 20 54 68 65 20 64 61 74 61 20 6f 72 20 6b 65 * The data or ke
19b51 79 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 y is taken from
19b52 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 70 the entry that p
19b53 43 75 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 Cur is currently
19b54 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f 2e pointing.** to.
19b55 20 20 6f 66 66 73 65 74 20 61 6e 64 20 61 6d 74 offset and amt
19b56 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 determine what
19b57 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 portion of the d
19b58 61 74 61 20 6f 72 20 6b 65 79 20 74 6f 20 72 65 ata or key to re
19b59 74 72 69 65 76 65 2e 0a 2a 2a 20 6b 65 79 20 69 trieve..** key i
19b5a 73 20 74 72 75 65 20 74 6f 20 67 65 74 20 74 68 s true to get th
19b5b 65 20 6b 65 79 20 6f 72 20 66 61 6c 73 65 20 74 e key or false t
19b5c 6f 20 67 65 74 20 64 61 74 61 2e 20 20 54 68 65 o get data. The
19b5d 20 72 65 73 75 6c 74 20 69 73 20 77 72 69 74 74 result is writt
19b5e 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 70 en.** into the p
19b5f 4d 65 6d 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a Mem element..**.
19b60 2a 2a 20 54 68 65 20 70 4d 65 6d 20 73 74 72 75 ** The pMem stru
19b61 63 74 75 72 65 20 69 73 20 61 73 73 75 6d 65 64 cture is assumed
19b62 20 74 6f 20 62 65 20 75 6e 69 6e 69 74 69 61 6c to be uninitial
19b63 69 7a 65 64 2e 20 20 41 6e 79 20 70 72 69 6f 72 ized. Any prior
19b64 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 73 20 6f content.** is o
19b65 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 6f verwritten witho
19b66 75 74 20 62 65 69 6e 67 20 66 72 65 65 64 2e 0a ut being freed..
19b67 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 6f **.** If this ro
19b68 75 74 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 20 utine fails for
19b69 61 6e 79 20 72 65 61 73 6f 6e 20 28 6d 61 6c 6c any reason (mall
19b6a 6f 63 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 oc returns NULL
19b6b 6f 72 20 75 6e 61 62 6c 65 0a 2a 2a 20 74 6f 20 or unable.** to
19b6c 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 69 read from the di
19b6d 73 6b 29 20 74 68 65 6e 20 74 68 65 20 70 4d 65 sk) then the pMe
19b6e 6d 20 69 73 20 6c 65 66 74 20 69 6e 20 61 6e 20 m is left in an
19b6f 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 inconsistent sta
19b70 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 te..*/.SQLITE_PR
19b71 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
19b72 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 3VdbeMemFromBtre
19b73 65 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 e(. BtCursor *p
19b74 43 75 72 2c 20 20 20 2f 2a 20 43 75 72 73 6f 72 Cur, /* Cursor
19b75 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 72 65 63 pointing at rec
19b76 6f 72 64 20 74 6f 20 72 65 74 72 69 65 76 65 2e ord to retrieve.
19b77 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 */. int offset
19b78 2c 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 , /* Offse
19b79 74 20 66 72 6f 6d 20 74 68 65 20 73 74 61 72 74 t from the start
19b7a 20 6f 66 20 64 61 74 61 20 74 6f 20 72 65 74 75 of data to retu
19b7b 72 6e 20 62 79 74 65 73 20 66 72 6f 6d 2e 20 2a rn bytes from. *
19b7c 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 /. int amt,
19b7d 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
19b7e 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 74 75 of bytes to retu
19b7f 72 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 rn. */. int key
19b80 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 , /* If
19b81 20 74 72 75 65 2c 20 72 65 74 72 69 65 76 65 20 true, retrieve
19b82 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6b from the btree k
19b83 65 79 2c 20 6e 6f 74 20 64 61 74 61 2e 20 2a 2f ey, not data. */
19b84 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 20 . Mem *pMem
19b85 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 /* OUT: Ret
19b86 75 72 6e 20 64 61 74 61 20 69 6e 20 74 68 69 73 urn data in this
19b87 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 20 Mem structure.
19b88 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 44 */.){. char *zD
19b89 61 74 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 44 ata; /* D
19b8a 61 74 61 20 66 72 6f 6d 20 74 68 65 20 62 74 72 ata from the btr
19b8b 65 65 20 6c 61 79 65 72 20 2a 2f 0a 20 20 69 6e ee layer */. in
19b8c 74 20 61 76 61 69 6c 61 62 6c 65 20 3d 20 30 3b t available = 0;
19b8d 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
19b8e 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20 6f ytes available o
19b8f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 62 74 72 65 n the local btre
19b90 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 e page */. int
19b91 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
19b92 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
19b93 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c /.. assert( sql
19b94 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 ite3BtreeCursorI
19b95 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b 0a sValid(pCur) );.
19b96 0a 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 65 20 . /* Note: the
19b97 63 61 6c 6c 73 20 74 6f 20 42 74 72 65 65 4b 65 calls to BtreeKe
19b98 79 46 65 74 63 68 28 29 20 61 6e 64 20 44 61 74 yFetch() and Dat
19b99 61 46 65 74 63 68 28 29 20 62 65 6c 6f 77 20 61 aFetch() below a
19b9a 73 73 65 72 74 28 29 20 0a 20 20 2a 2a 20 74 68 ssert() . ** th
19b9b 61 74 20 62 6f 74 68 20 74 68 65 20 42 74 53 68 at both the BtSh
19b9c 61 72 65 64 20 61 6e 64 20 64 61 74 61 62 61 73 ared and databas
19b9d 65 20 68 61 6e 64 6c 65 20 6d 75 74 65 78 65 73 e handle mutexes
19b9e 20 61 72 65 20 68 65 6c 64 2e 20 2a 2f 0a 20 20 are held. */.
19b9f 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 assert( (pMem->f
19ba0 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 lags & MEM_RowSe
19ba1 74 29 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 6b t)==0 );. if( k
19ba2 65 79 20 29 7b 0a 20 20 20 20 7a 44 61 74 61 20 ey ){. zData
19ba3 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 = (char *)sqlite
19ba4 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 70 3BtreeKeyFetch(p
19ba5 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 29 Cur, &available)
19ba6 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a ;. }else{. z
19ba7 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 73 Data = (char *)s
19ba8 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 46 qlite3BtreeDataF
19ba9 65 74 63 68 28 70 43 75 72 2c 20 26 61 76 61 69 etch(pCur, &avai
19baa 6c 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 61 73 lable);. }. as
19bab 73 65 72 74 28 20 7a 44 61 74 61 21 3d 30 20 29 sert( zData!=0 )
19bac 3b 0a 0a 20 20 69 66 28 20 6f 66 66 73 65 74 2b ;.. if( offset+
19bad 61 6d 74 3c 3d 61 76 61 69 6c 61 62 6c 65 20 26 amt<=available &
19bae 26 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d & (pMem->flags&M
19baf 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 7b 0a 20 20 EM_Dyn)==0 ){.
19bb0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
19bb1 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 Release(pMem);.
19bb2 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 26 7a 44 pMem->z = &zD
19bb3 61 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20 ata[offset];.
19bb4 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
19bb5 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 45 70 68 65 EM_Blob|MEM_Ephe
19bb6 6d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 53 m;. }else if( S
19bb7 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 QLITE_OK==(rc =
19bb8 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
19bb9 6f 77 28 70 4d 65 6d 2c 20 61 6d 74 2b 32 2c 20 ow(pMem, amt+2,
19bba 30 29 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 0)) ){. pMem-
19bbb 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f >flags = MEM_Blo
19bbc 62 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 65 b|MEM_Dyn|MEM_Te
19bbd 72 6d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e rm;. pMem->en
19bbe 63 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d c = 0;. pMem-
19bbf 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 >type = SQLITE_B
19bc0 4c 4f 42 3b 0a 20 20 20 20 69 66 28 20 6b 65 79 LOB;. if( key
19bc1 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
19bc2 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 70 qlite3BtreeKey(p
19bc3 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 Cur, offset, amt
19bc4 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 , pMem->z);.
19bc5 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
19bc6 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 = sqlite3BtreeDa
19bc7 74 61 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c ta(pCur, offset,
19bc8 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a amt, pMem->z);.
19bc9 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e }. pMem->
19bca 7a 5b 61 6d 74 5d 20 3d 20 30 3b 0a 20 20 20 20 z[amt] = 0;.
19bcb 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 2b 31 5d 20 3d pMem->z[amt+1] =
19bcc 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 0;. if( rc!=
19bcd 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
19bce 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
19bcf 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a mRelease(pMem);.
19bd0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4d 65 6d }. }. pMem
19bd1 2d 3e 6e 20 3d 20 61 6d 74 3b 0a 0a 20 20 72 65 ->n = amt;.. re
19bd2 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 turn rc;.}../* T
19bd3 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
19bd4 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 only available i
19bd5 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74 20 69 73 nternally, it is
19bd6 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 not part of the
19bd7 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 41 50 49 .** external API
19bd8 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e 20 61 20 . It works in a
19bd9 73 69 6d 69 6c 61 72 20 77 61 79 20 74 6f 20 73 similar way to s
19bda 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
19bdb 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70 74 20 74 t(),.** except t
19bdc 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 he data returned
19bdd 20 69 73 20 69 6e 20 74 68 65 20 65 6e 63 6f 64 is in the encod
19bde 69 6e 67 20 73 70 65 63 69 66 69 65 64 20 62 79 ing specified by
19bdf 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 the second.** p
19be0 61 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68 20 arameter, which
19be1 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 53 must be one of S
19be2 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 QLITE_UTF16BE, S
19be3 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 QLITE_UTF16LE or
19be4 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 2e .** SQLITE_UTF8.
19be5 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d 30 32 2d .**.** (2006-02-
19be6 31 36 3a 29 20 20 54 68 65 20 65 6e 63 20 76 61 16:) The enc va
19be7 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 2d 65 64 lue can be or-ed
19be8 20 77 69 74 68 20 53 51 4c 49 54 45 5f 55 54 46 with SQLITE_UTF
19be9 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a 2a 20 49 16_ALIGNED..** I
19bea 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63 61 f that is the ca
19beb 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 se, then the res
19bec 75 6c 74 20 6d 75 73 74 20 62 65 20 61 6c 69 67 ult must be alig
19bed 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65 6e 20 62 ned on an even b
19bee 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 2e yte.** boundary.
19bef 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19bf0 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 TE const void *s
19bf1 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 qlite3ValueText(
19bf2 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 sqlite3_value* p
19bf3 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 Val, u8 enc){.
19bf4 69 66 28 20 21 70 56 61 6c 20 29 20 72 65 74 75 if( !pVal ) retu
19bf5 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 rn 0;.. assert(
19bf6 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 7c 7c 20 pVal->db==0 ||
19bf7 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
19bf8 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e 6d 75 74 ld(pVal->db->mut
19bf9 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
19bfa 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e 63 26 7e (enc&3)==(enc&~
19bfb 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 SQLITE_UTF16_ALI
19bfc 47 4e 45 44 29 20 29 3b 0a 20 20 61 73 73 65 72 GNED) );. asser
19bfd 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 t( (pVal->flags
19bfe 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 & MEM_RowSet)==0
19bff 20 29 3b 0a 0a 20 20 69 66 28 20 70 56 61 6c 2d );.. if( pVal-
19c00 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 >flags&MEM_Null
19c01 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
19c02 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 . }. assert( (
19c03 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 3d 3d 20 MEM_Blob>>3) ==
19c04 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 70 56 61 MEM_Str );. pVa
19c05 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 56 61 l->flags |= (pVa
19c06 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 l->flags & MEM_B
19c07 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65 78 70 61 6e lob)>>3;. expan
19c08 64 42 6c 6f 62 28 70 56 61 6c 29 3b 0a 20 20 69 dBlob(pVal);. i
19c09 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d f( pVal->flags&M
19c0a 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 73 71 EM_Str ){. sq
19c0b 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
19c0c 6e 63 6f 64 69 6e 67 28 70 56 61 6c 2c 20 65 6e ncoding(pVal, en
19c0d 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 c & ~SQLITE_UTF1
19c0e 36 5f 41 4c 49 47 4e 45 44 29 3b 0a 20 20 20 20 6_ALIGNED);.
19c0f 69 66 28 20 28 65 6e 63 20 26 20 53 51 4c 49 54 if( (enc & SQLIT
19c10 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 E_UTF16_ALIGNED)
19c11 21 3d 30 20 26 26 20 31 3d 3d 28 31 26 53 51 4c !=0 && 1==(1&SQL
19c12 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 ITE_PTR_TO_INT(p
19c13 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a 20 20 20 20 Val->z)) ){.
19c14 20 20 61 73 73 65 72 74 28 20 28 70 56 61 6c 2d assert( (pVal-
19c15 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 45 70 >flags & (MEM_Ep
19c16 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 29 hem|MEM_Static))
19c17 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 !=0 );. if(
19c18 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d sqlite3VdbeMemM
19c19 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 56 61 akeWriteable(pVa
19c1a 6c 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b l)!=SQLITE_OK ){
19c1b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
19c1c 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0;. }. }
19c1d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
19c1e 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 MemNulTerminate(
19c1f 70 56 61 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a pVal);. }else{.
19c20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 56 61 assert( (pVa
19c21 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f l->flags&MEM_Blo
19c22 62 29 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c b)==0 );. sql
19c23 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e ite3VdbeMemStrin
19c24 67 69 66 79 28 70 56 61 6c 2c 20 65 6e 63 29 3b gify(pVal, enc);
19c25 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 3d 3d . assert( 0==
19c26 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f (1&SQLITE_PTR_TO
19c27 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29 _INT(pVal->z)) )
19c28 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 70 ;. }. assert(p
19c29 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 26 Val->enc==(enc &
19c2a 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 ~SQLITE_UTF16_A
19c2b 4c 49 47 4e 45 44 29 20 7c 7c 20 70 56 61 6c 2d LIGNED) || pVal-
19c2c 3e 64 62 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 >db==0.
19c2d 20 20 20 20 20 7c 7c 20 70 56 61 6c 2d 3e 64 62 || pVal->db
19c2e 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
19c2f 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 65 6e ;. if( pVal->en
19c30 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 54 c==(enc & ~SQLIT
19c31 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 E_UTF16_ALIGNED)
19c32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 ){. return p
19c33 56 61 6c 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b Val->z;. }else{
19c34 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
19c35 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 }.}../*.** Crea
19c36 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 te a new sqlite3
19c37 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 0a 2a _value object..*
19c38 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19c39 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
19c3a 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 sqlite3ValueNew(
19c3b 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
19c3c 4d 65 6d 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 Mem *p = sqlite3
19c3d 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c DbMallocZero(db,
19c3e 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 sizeof(*p));.
19c3f 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e if( p ){. p->
19c40 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
19c41 3b 0a 20 20 20 20 70 2d 3e 74 79 70 65 20 3d 20 ;. p->type =
19c42 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20 SQLITE_NULL;.
19c43 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 7d p->db = db;. }
19c44 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
19c45 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e /*.** Create a n
19c46 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ew sqlite3_value
19c47 20 6f 62 6a 65 63 74 2c 20 63 6f 6e 74 61 69 6e object, contain
19c48 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f 66 ing the value of
19c49 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 pExpr..**.** Th
19c4a 69 73 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f is only works fo
19c4b 72 20 76 65 72 79 20 73 69 6d 70 6c 65 20 65 78 r very simple ex
19c4c 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20 63 pressions that c
19c4d 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 63 6f onsist of one co
19c4e 6e 73 74 61 6e 74 0a 2a 2a 20 74 6f 6b 65 6e 20 nstant.** token
19c4f 28 69 2e 65 2e 20 22 35 22 2c 20 22 35 2e 31 22 (i.e. "5", "5.1"
19c50 2c 20 22 27 61 20 73 74 72 69 6e 67 27 22 29 2e , "'a string'").
19c51 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 If the expressi
19c52 6f 6e 20 63 61 6e 0a 2a 2a 20 62 65 20 63 6f 6e on can.** be con
19c53 76 65 72 74 65 64 20 64 69 72 65 63 74 6c 79 20 verted directly
19c54 69 6e 74 6f 20 61 20 76 61 6c 75 65 2c 20 74 68 into a value, th
19c55 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 en the value is
19c56 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a allocated and.**
19c57 20 61 20 70 6f 69 6e 74 65 72 20 77 72 69 74 74 a pointer writt
19c58 65 6e 20 74 6f 20 2a 70 70 56 61 6c 2e 20 54 68 en to *ppVal. Th
19c59 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 e caller is resp
19c5a 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 61 6c onsible for deal
19c5b 6c 6f 63 61 74 69 6e 67 0a 2a 2a 20 74 68 65 20 locating.** the
19c5c 76 61 6c 75 65 20 62 79 20 70 61 73 73 69 6e 67 value by passing
19c5d 20 69 74 20 74 6f 20 73 71 6c 69 74 65 33 56 61 it to sqlite3Va
19c5e 6c 75 65 46 72 65 65 28 29 20 6c 61 74 65 72 20 lueFree() later
19c5f 6f 6e 2e 20 49 66 20 74 68 65 20 65 78 70 72 65 on. If the expre
19c60 73 73 69 6f 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 ssion.** cannot
19c61 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 be converted to
19c62 61 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a 70 a value, then *p
19c63 70 56 61 6c 20 69 73 20 73 65 74 20 74 6f 20 4e pVal is set to N
19c64 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ULL..*/.SQLITE_P
19c65 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19c66 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 e3ValueFromExpr(
19c67 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
19c68 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19c69 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e The database con
19c6a 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 nection */. Exp
19c6b 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 r *pExpr,
19c6c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 /* The ex
19c6d 70 72 65 73 73 69 6f 6e 20 74 6f 20 65 76 61 6c pression to eval
19c6e 75 61 74 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 uate */. u8 enc
19c6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
19c70 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 /* Encoding
19c71 74 6f 20 75 73 65 20 2a 2f 0a 20 20 75 38 20 61 to use */. u8 a
19c72 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 20 20 ffinity,
19c73 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 /* Affinit
19c74 79 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 73 71 y to use */. sq
19c75 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 70 lite3_value **pp
19c76 56 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74 65 Val /* Write
19c77 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 20 68 the new value h
19c78 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ere */.){. int
19c79 6f 70 3b 0a 20 20 63 68 61 72 20 2a 7a 56 61 6c op;. char *zVal
19c7a 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f = 0;. sqlite3_
19c7b 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20 30 3b value *pVal = 0;
19c7c 0a 0a 20 20 69 66 28 20 21 70 45 78 70 72 20 29 .. if( !pExpr )
19c7d 7b 0a 20 20 20 20 2a 70 70 56 61 6c 20 3d 20 30 {. *ppVal = 0
19c7e 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
19c7f 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 6f 70 ITE_OK;. }. op
19c80 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 = pExpr->op;.
19c81 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 if( op==TK_REGIS
19c82 54 45 52 20 29 7b 0a 20 20 20 20 6f 70 20 3d 20 TER ){. op =
19c83 70 45 78 70 72 2d 3e 6f 70 32 3b 0a 20 20 7d 0a pExpr->op2;. }.
19c84 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 54 . if( op==TK_ST
19c85 52 49 4e 47 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 RING || op==TK_F
19c86 4c 4f 41 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 LOAT || op==TK_I
19c87 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 70 56 NTEGER ){. pV
19c88 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 al = sqlite3Valu
19c89 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 eNew(db);. if
19c8a 28 20 70 56 61 6c 3d 3d 30 20 29 20 67 6f 74 6f ( pVal==0 ) goto
19c8b 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 69 66 28 no_mem;. if(
19c8c 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
19c8d 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 (pExpr, EP_IntVa
19c8e 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 lue) ){. sq
19c8f 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 lite3VdbeMemSetI
19c90 6e 74 36 34 28 70 56 61 6c 2c 20 28 69 36 34 29 nt64(pVal, (i64)
19c91 70 45 78 70 72 2d 3e 75 2e 69 56 61 6c 75 65 29 pExpr->u.iValue)
19c92 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
19c93 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69 74 65 zVal = sqlite
19c94 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 45 3DbStrDup(db, pE
19c95 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a xpr->u.zToken);.
19c96 20 20 20 20 20 20 69 66 28 20 7a 56 61 6c 3d 3d if( zVal==
19c97 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0 ) goto no_mem;
19c98 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 . sqlite3Va
19c99 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20 lueSetStr(pVal,
19c9a 2d 31 2c 20 7a 56 61 6c 2c 20 53 51 4c 49 54 45 -1, zVal, SQLITE
19c9b 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 59 _UTF8, SQLITE_DY
19c9c 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 69 66 NAMIC);. if
19c9d 28 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 ( op==TK_FLOAT )
19c9e 20 70 56 61 6c 2d 3e 74 79 70 65 20 3d 20 53 51 pVal->type = SQ
19c9f 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 20 20 LITE_FLOAT;.
19ca0 7d 0a 20 20 20 20 69 66 28 20 28 6f 70 3d 3d 54 }. if( (op==T
19ca1 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20 6f 70 3d K_INTEGER || op=
19ca2 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26 26 20 61 =TK_FLOAT ) && a
19ca3 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f ffinity==SQLITE_
19ca4 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 AFF_NONE ){.
19ca5 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 sqlite3ValueAp
19ca6 70 6c 79 41 66 66 69 6e 69 74 79 28 70 56 61 6c plyAffinity(pVal
19ca7 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d , SQLITE_AFF_NUM
19ca8 45 52 49 43 2c 20 53 51 4c 49 54 45 5f 55 54 46 ERIC, SQLITE_UTF
19ca9 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 8);. }else{.
19caa 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 sqlite3Valu
19cab 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 eApplyAffinity(p
19cac 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20 53 Val, affinity, S
19cad 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 QLITE_UTF8);.
19cae 20 7d 0a 20 20 20 20 69 66 28 20 65 6e 63 21 3d }. if( enc!=
19caf 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 SQLITE_UTF8 ){.
19cb0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
19cb1 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 ChangeEncoding(p
19cb2 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 20 7d Val, enc);. }
19cb3 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d . }else if( op=
19cb4 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 20 7b 0a 20 =TK_UMINUS ) {.
19cb5 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b if( SQLITE_OK
19cb6 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 ==sqlite3ValueFr
19cb7 6f 6d 45 78 70 72 28 64 62 2c 70 45 78 70 72 2d omExpr(db,pExpr-
19cb8 3e 70 4c 65 66 74 2c 65 6e 63 2c 61 66 66 69 6e >pLeft,enc,affin
19cb9 69 74 79 2c 26 70 56 61 6c 29 20 29 7b 0a 20 20 ity,&pVal) ){.
19cba 20 20 20 20 70 56 61 6c 2d 3e 75 2e 69 20 3d 20 pVal->u.i =
19cbb 2d 31 20 2a 20 70 56 61 6c 2d 3e 75 2e 69 3b 0a -1 * pVal->u.i;.
19cbc 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 /* (double
19cbd 29 2d 31 20 49 6e 20 63 61 73 65 20 6f 66 20 53 )-1 In case of S
19cbe 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
19cbf 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a ING_POINT... */.
19cc0 20 20 20 20 20 20 70 56 61 6c 2d 3e 72 20 3d 20 pVal->r =
19cc1 28 64 6f 75 62 6c 65 29 2d 31 20 2a 20 70 56 61 (double)-1 * pVa
19cc2 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a l->r;. }. }.
19cc3 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
19cc4 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c MIT_BLOB_LITERAL
19cc5 0a 20 20 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d . else if( op==
19cc6 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 69 TK_BLOB ){. i
19cc7 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 20 61 73 73 nt nVal;. ass
19cc8 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 ert( pExpr->u.zT
19cc9 6f 6b 65 6e 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 oken[0]=='x' ||
19cca 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b pExpr->u.zToken[
19ccb 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20 61 0]=='X' );. a
19ccc 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e ssert( pExpr->u.
19ccd 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 27 5c 27 27 20 zToken[1]=='\''
19cce 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 73 71 );. pVal = sq
19ccf 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 lite3ValueNew(db
19cd0 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 61 6c );. if( !pVal
19cd1 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
19cd2 20 20 20 20 7a 56 61 6c 20 3d 20 26 70 45 78 70 zVal = &pExp
19cd3 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a r->u.zToken[2];.
19cd4 20 20 20 20 6e 56 61 6c 20 3d 20 73 71 6c 69 74 nVal = sqlit
19cd5 65 33 53 74 72 6c 65 6e 33 30 28 7a 56 61 6c 29 e3Strlen30(zVal)
19cd6 2d 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 -1;. assert(
19cd7 7a 56 61 6c 5b 6e 56 61 6c 5d 3d 3d 27 5c 27 27 zVal[nVal]=='\''
19cd8 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 );. sqlite3V
19cd9 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 56 61 dbeMemSetStr(pVa
19cda 6c 2c 20 73 71 6c 69 74 65 33 48 65 78 54 6f 42 l, sqlite3HexToB
19cdb 6c 6f 62 28 64 62 2c 20 7a 56 61 6c 2c 20 6e 56 lob(db, zVal, nV
19cdc 61 6c 29 2c 20 6e 56 61 6c 2f 32 2c 0a 20 20 20 al), nVal/2,.
19cdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19cde 20 20 20 20 20 20 30 2c 20 53 51 4c 49 54 45 5f 0, SQLITE_
19cdf 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 23 65 DYNAMIC);. }.#e
19ce0 6e 64 69 66 0a 0a 20 20 2a 70 70 56 61 6c 20 3d ndif.. *ppVal =
19ce1 20 70 56 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20 pVal;. return
19ce2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 6e 6f 5f 6d SQLITE_OK;..no_m
19ce3 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 em:. db->malloc
19ce4 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 73 71 Failed = 1;. sq
19ce5 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
19ce6 7a 56 61 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 zVal);. sqlite3
19ce7 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b ValueFree(pVal);
19ce8 0a 20 20 2a 70 70 56 61 6c 20 3d 20 30 3b 0a 20 . *ppVal = 0;.
19ce9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
19cea 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 OMEM;.}../*.** C
19ceb 68 61 6e 67 65 20 74 68 65 20 73 74 72 69 6e 67 hange the string
19cec 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 73 71 6c value of an sql
19ced 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
19cee 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 t.*/.SQLITE_PRIV
19cef 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
19cf0 56 61 6c 75 65 53 65 74 53 74 72 28 0a 20 20 73 ValueSetStr(. s
19cf1 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76 2c qlite3_value *v,
19cf2 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f /* Value to
19cf3 20 62 65 20 73 65 74 20 2a 2f 0a 20 20 69 6e 74 be set */. int
19cf4 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 n,
19cf5 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 /* Length of
19cf6 73 74 72 69 6e 67 20 7a 20 2a 2f 0a 20 20 63 6f string z */. co
19cf7 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 20 20 20 nst void *z,
19cf8 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 /* Text of t
19cf9 68 65 20 6e 65 77 20 73 74 72 69 6e 67 20 2a 2f he new string */
19cfa 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 . u8 enc,
19cfb 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f /* Enco
19cfc 64 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f 0a 20 ding to use */.
19cfd 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
19cfe 69 64 2a 29 20 20 20 2f 2a 20 44 65 73 74 72 75 id*) /* Destru
19cff 63 74 6f 72 20 66 6f 72 20 74 68 65 20 73 74 72 ctor for the str
19d00 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 ing */.){. if(
19d01 76 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d v ) sqlite3VdbeM
19d02 65 6d 53 65 74 53 74 72 28 28 4d 65 6d 20 2a 29 emSetStr((Mem *)
19d03 76 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44 v, z, n, enc, xD
19d04 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 el);.}../*.** Fr
19d05 65 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 ee an sqlite3_va
19d06 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 lue object.*/.SQ
19d07 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
19d08 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 d sqlite3ValueFr
19d09 65 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ee(sqlite3_value
19d0a 20 2a 76 29 7b 0a 20 20 69 66 28 20 21 76 20 29 *v){. if( !v )
19d0b 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 return;. sqlit
19d0c 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
19d0d 28 28 4d 65 6d 20 2a 29 76 29 3b 0a 20 20 73 71 ((Mem *)v);. sq
19d0e 6c 69 74 65 33 44 62 46 72 65 65 28 28 28 4d 65 lite3DbFree(((Me
19d0f 6d 2a 29 76 29 2d 3e 64 62 2c 20 76 29 3b 0a 7d m*)v)->db, v);.}
19d10 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
19d11 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
19d12 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 es in the sqlite
19d13 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 61 3_value object a
19d14 73 73 75 6d 69 6e 67 0a 2a 2a 20 74 68 61 74 20 ssuming.** that
19d15 69 74 20 75 73 65 73 20 74 68 65 20 65 6e 63 6f it uses the enco
19d16 64 69 6e 67 20 22 65 6e 63 22 0a 2a 2f 0a 53 51 ding "enc".*/.SQ
19d17 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
19d18 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 sqlite3ValueByt
19d19 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 es(sqlite3_value
19d1a 20 2a 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b *pVal, u8 enc){
19d1b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 6d . Mem *p = (Mem
19d1c 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20 28 70 *)pVal;. if( (p
19d1d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c ->flags & MEM_Bl
19d1e 6f 62 29 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65 ob)!=0 || sqlite
19d1f 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 3ValueText(pVal,
19d20 20 65 6e 63 29 20 29 7b 0a 20 20 20 20 69 66 28 enc) ){. if(
19d21 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f p->flags & MEM_
19d22 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 72 65 Zero ){. re
19d23 74 75 72 6e 20 70 2d 3e 6e 20 2b 20 70 2d 3e 75 turn p->n + p->u
19d24 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 65 6c 73 .nZero;. }els
19d25 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 e{. return
19d26 70 2d 3e 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a p->n;. }. }.
19d27 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
19d28 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
19d29 6e 64 20 6f 66 20 76 64 62 65 6d 65 6d 2e 63 20 nd of vdbemem.c
19d2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
19d2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
19d2e 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 61 75 egin file vdbeau
19d2f 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a x.c ************
19d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
19d32 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65 6d *.** 2003 Septem
19d33 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ber 6.**.** The
19d34 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
19d35 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
19d36 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
19d37 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
19d38 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
19d39 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
19d3a 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
19d3b 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
19d3c 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
19d3d 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
19d3e 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
19d3f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
19d40 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
19d41 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
19d42 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
19d43 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
19d44 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
19d45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d49 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
19d4a 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 e contains code
19d4b 75 73 65 64 20 66 6f 72 20 63 72 65 61 74 69 6e used for creatin
19d4c 67 2c 20 64 65 73 74 72 6f 79 69 6e 67 2c 20 61 g, destroying, a
19d4d 6e 64 20 70 6f 70 75 6c 61 74 69 6e 67 0a 2a 2a nd populating.**
19d4e 20 61 20 56 44 42 45 20 28 6f 72 20 61 6e 20 22 a VDBE (or an "
19d4f 73 71 6c 69 74 65 33 5f 73 74 6d 74 22 20 61 73 sqlite3_stmt" as
19d50 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 74 6f 20 it is known to
19d51 74 68 65 20 6f 75 74 73 69 64 65 20 77 6f 72 6c the outside worl
19d52 64 2e 29 20 20 50 72 69 6f 72 0a 2a 2a 20 74 6f d.) Prior.** to
19d53 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 37 2c 20 version 2.8.7,
19d54 61 6c 6c 20 74 68 69 73 20 63 6f 64 65 20 77 61 all this code wa
19d55 73 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20 s combined into
19d56 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63 the vdbe.c sourc
19d57 65 20 66 69 6c 65 2e 0a 2a 2a 20 42 75 74 20 74 e file..** But t
19d58 68 61 74 20 66 69 6c 65 20 77 61 73 20 67 65 74 hat file was get
19d59 74 69 6e 67 20 74 6f 6f 20 62 69 67 20 73 6f 20 ting too big so
19d5a 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65 73 this subroutines
19d5b 20 77 65 72 65 20 73 70 6c 69 74 20 6f 75 74 2e were split out.
19d5c 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 .**.** $Id: vdbe
19d5d 61 75 78 2e 63 2c 76 20 31 2e 34 38 30 20 32 30 aux.c,v 1.480 20
19d5e 30 39 2f 30 38 2f 30 38 20 31 38 3a 30 31 3a 30 09/08/08 18:01:0
19d5f 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 8 drh Exp $.*/..
19d60 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 64 65 62 ../*.** When deb
19d61 75 67 67 69 6e 67 20 74 68 65 20 63 6f 64 65 20 ugging the code
19d62 67 65 6e 65 72 61 74 6f 72 20 69 6e 20 61 20 73 generator in a s
19d63 79 6d 62 6f 6c 69 63 20 64 65 62 75 67 67 65 72 ymbolic debugger
19d64 2c 20 6f 6e 65 20 63 61 6e 0a 2a 2a 20 73 65 74 , one can.** set
19d65 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 the sqlite3Vdbe
19d66 41 64 64 6f 70 54 72 61 63 65 20 74 6f 20 31 20 AddopTrace to 1
19d67 61 6e 64 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 and all opcodes
19d68 77 69 6c 6c 20 62 65 20 70 72 69 6e 74 65 64 0a will be printed.
19d69 2a 2a 20 61 73 20 74 68 65 79 20 61 72 65 20 61 ** as they are a
19d6a 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e 73 74 dded to the inst
19d6b 72 75 63 74 69 6f 6e 20 73 74 72 65 61 6d 2e 0a ruction stream..
19d6c 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
19d6d 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 _DEBUG.SQLITE_PR
19d6e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
19d6f 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20 3VdbeAddopTrace
19d70 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a = 0;.#endif.../*
19d71 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
19d72 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 virtual databas
19d73 65 20 65 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 51 4c e engine..*/.SQL
19d74 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 ITE_PRIVATE Vdbe
19d75 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 *sqlite3VdbeCre
19d76 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 ate(sqlite3 *db)
19d77 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 70 {. Vdbe *p;. p
19d78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
19d79 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f ocZero(db, sizeo
19d7a 66 28 56 64 62 65 29 20 29 3b 0a 20 20 69 66 28 f(Vdbe) );. if(
19d7b 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 p==0 ) return 0
19d7c 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a ;. p->db = db;.
19d7d 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 65 20 if( db->pVdbe
19d7e 29 7b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65 ){. db->pVdbe
19d7f 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d ->pPrev = p;. }
19d80 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62 . p->pNext = db
19d81 2d 3e 70 56 64 62 65 3b 0a 20 20 70 2d 3e 70 50 ->pVdbe;. p->pP
19d82 72 65 76 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70 rev = 0;. db->p
19d83 56 64 62 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6d Vdbe = p;. p->m
19d84 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 agic = VDBE_MAGI
19d85 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e C_INIT;. return
19d86 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d p;.}../*.** Rem
19d87 65 6d 62 65 72 20 74 68 65 20 53 51 4c 20 73 74 ember the SQL st
19d88 72 69 6e 67 20 66 6f 72 20 61 20 70 72 65 70 61 ring for a prepa
19d89 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a red statement..*
19d8a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19d8b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
19d8c 65 53 65 74 53 71 6c 28 56 64 62 65 20 2a 70 2c eSetSql(Vdbe *p,
19d8d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 const char *z,
19d8e 69 6e 74 20 6e 2c 20 69 6e 74 20 69 73 50 72 65 int n, int isPre
19d8f 70 61 72 65 56 32 29 7b 0a 20 20 69 66 28 20 70 pareV2){. if( p
19d90 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 ==0 ) return;.#i
19d91 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
19d92 5f 54 52 41 43 45 0a 20 20 69 66 28 20 21 69 73 _TRACE. if( !is
19d93 50 72 65 70 61 72 65 56 32 20 29 20 72 65 74 75 PrepareV2 ) retu
19d94 72 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 rn;.#endif. ass
19d95 65 72 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d 30 20 ert( p->zSql==0
19d96 29 3b 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d 20 73 );. p->zSql = s
19d97 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 qlite3DbStrNDup(
19d98 70 2d 3e 64 62 2c 20 7a 2c 20 6e 29 3b 0a 20 20 p->db, z, n);.
19d99 70 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d p->isPrepareV2 =
19d9a 20 69 73 50 72 65 70 61 72 65 56 32 20 3f 20 31 isPrepareV2 ? 1
19d9b 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 : 0;.}../*.** R
19d9c 65 74 75 72 6e 20 74 68 65 20 53 51 4c 20 61 73 eturn the SQL as
19d9d 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 sociated with a
19d9e 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
19d9f 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 nt.*/.SQLITE_API
19da0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
19da1 69 74 65 33 5f 73 71 6c 28 73 71 6c 69 74 65 33 ite3_sql(sqlite3
19da2 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 _stmt *pStmt){.
19da3 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 Vdbe *p = (Vdbe
19da4 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 *)pStmt;. retu
19da5 72 6e 20 28 70 2d 3e 69 73 50 72 65 70 61 72 65 rn (p->isPrepare
19da6 56 32 20 3f 20 70 2d 3e 7a 53 71 6c 20 3a 20 30 V2 ? p->zSql : 0
19da7 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70 );.}../*.** Swap
19da8 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 65 74 all content bet
19da9 77 65 65 6e 20 74 77 6f 20 56 44 42 45 20 73 74 ween two VDBE st
19daa 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c ructures..*/.SQL
19dab 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
19dac 20 73 71 6c 69 74 65 33 56 64 62 65 53 77 61 70 sqlite3VdbeSwap
19dad 28 56 64 62 65 20 2a 70 41 2c 20 56 64 62 65 20 (Vdbe *pA, Vdbe
19dae 2a 70 42 29 7b 0a 20 20 56 64 62 65 20 74 6d 70 *pB){. Vdbe tmp
19daf 2c 20 2a 70 54 6d 70 3b 0a 20 20 63 68 61 72 20 , *pTmp;. char
19db0 2a 7a 54 6d 70 3b 0a 20 20 74 6d 70 20 3d 20 2a *zTmp;. tmp = *
19db1 70 41 3b 0a 20 20 2a 70 41 20 3d 20 2a 70 42 3b pA;. *pA = *pB;
19db2 0a 20 20 2a 70 42 20 3d 20 74 6d 70 3b 0a 20 20 . *pB = tmp;.
19db3 70 54 6d 70 20 3d 20 70 41 2d 3e 70 4e 65 78 74 pTmp = pA->pNext
19db4 3b 0a 20 20 70 41 2d 3e 70 4e 65 78 74 20 3d 20 ;. pA->pNext =
19db5 70 42 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 42 2d pB->pNext;. pB-
19db6 3e 70 4e 65 78 74 20 3d 20 70 54 6d 70 3b 0a 20 >pNext = pTmp;.
19db7 20 70 54 6d 70 20 3d 20 70 41 2d 3e 70 50 72 65 pTmp = pA->pPre
19db8 76 3b 0a 20 20 70 41 2d 3e 70 50 72 65 76 20 3d v;. pA->pPrev =
19db9 20 70 42 2d 3e 70 50 72 65 76 3b 0a 20 20 70 42 pB->pPrev;. pB
19dba 2d 3e 70 50 72 65 76 20 3d 20 70 54 6d 70 3b 0a ->pPrev = pTmp;.
19dbb 20 20 7a 54 6d 70 20 3d 20 70 41 2d 3e 7a 53 71 zTmp = pA->zSq
19dbc 6c 3b 0a 20 20 70 41 2d 3e 7a 53 71 6c 20 3d 20 l;. pA->zSql =
19dbd 70 42 2d 3e 7a 53 71 6c 3b 0a 20 20 70 42 2d 3e pB->zSql;. pB->
19dbe 7a 53 71 6c 20 3d 20 7a 54 6d 70 3b 0a 20 20 70 zSql = zTmp;. p
19dbf 42 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d B->isPrepareV2 =
19dc0 20 70 41 2d 3e 69 73 50 72 65 70 61 72 65 56 32 pA->isPrepareV2
19dc1 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
19dc2 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 TE_DEBUG./*.** T
19dc3 75 72 6e 20 74 72 61 63 69 6e 67 20 6f 6e 20 6f urn tracing on o
19dc4 72 20 6f 66 66 0a 2a 2f 0a 53 51 4c 49 54 45 5f r off.*/.SQLITE_
19dc5 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19dc6 69 74 65 33 56 64 62 65 54 72 61 63 65 28 56 64 ite3VdbeTrace(Vd
19dc7 62 65 20 2a 70 2c 20 46 49 4c 45 20 2a 74 72 61 be *p, FILE *tra
19dc8 63 65 29 7b 0a 20 20 70 2d 3e 74 72 61 63 65 20 ce){. p->trace
19dc9 3d 20 74 72 61 63 65 3b 0a 7d 0a 23 65 6e 64 69 = trace;.}.#endi
19dca 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 f../*.** Resize
19dcb 74 68 65 20 56 64 62 65 2e 61 4f 70 20 61 72 72 the Vdbe.aOp arr
19dcc 61 79 20 73 6f 20 74 68 61 74 20 69 74 20 69 73 ay so that it is
19dcd 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70 at least one op
19dce 20 6c 61 72 67 65 72 20 74 68 61 6e 20 0a 2a 2a larger than .**
19dcf 20 69 74 20 77 61 73 2e 0a 2a 2a 0a 2a 2a 20 49 it was..**.** I
19dd0 66 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f f an out-of-memo
19dd1 72 79 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 ry error occurs
19dd2 77 68 69 6c 65 20 72 65 73 69 7a 69 6e 67 20 74 while resizing t
19dd3 68 65 20 61 72 72 61 79 2c 20 72 65 74 75 72 6e he array, return
19dd4 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d .** SQLITE_NOMEM
19dd5 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 56 . In this case V
19dd6 64 62 65 2e 61 4f 70 20 61 6e 64 20 56 64 62 65 dbe.aOp and Vdbe
19dd7 2e 6e 4f 70 41 6c 6c 6f 63 20 72 65 6d 61 69 6e .nOpAlloc remain
19dd8 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 28 .** unchanged (
19dd9 74 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 20 this is so that
19dda 61 6e 79 20 6f 70 63 6f 64 65 73 20 61 6c 72 65 any opcodes alre
19ddb 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 63 61 ady allocated ca
19ddc 6e 20 62 65 20 0a 2a 2a 20 63 6f 72 72 65 63 74 n be .** correct
19ddd 6c 79 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 61 ly deallocated a
19dde 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 72 65 long with the re
19ddf 73 74 20 6f 66 20 74 68 65 20 56 64 62 65 29 2e st of the Vdbe).
19de0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 .*/.static int g
19de1 72 6f 77 4f 70 41 72 72 61 79 28 56 64 62 65 20 rowOpArray(Vdbe
19de2 2a 70 29 7b 0a 20 20 56 64 62 65 4f 70 20 2a 70 *p){. VdbeOp *p
19de3 4e 65 77 3b 0a 20 20 69 6e 74 20 6e 4e 65 77 20 New;. int nNew
19de4 3d 20 28 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3f = (p->nOpAlloc ?
19de5 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 2a 32 20 3a p->nOpAlloc*2 :
19de6 20 28 69 6e 74 29 28 31 30 32 34 2f 73 69 7a 65 (int)(1024/size
19de7 6f 66 28 4f 70 29 29 29 3b 0a 20 20 70 4e 65 77 of(Op)));. pNew
19de8 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c = sqlite3DbReal
19de9 6c 6f 63 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4f loc(p->db, p->aO
19dea 70 2c 20 6e 4e 65 77 2a 73 69 7a 65 6f 66 28 4f p, nNew*sizeof(O
19deb 70 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 p));. if( pNew
19dec 29 7b 0a 20 20 20 20 70 2d 3e 6e 4f 70 41 6c 6c ){. p->nOpAll
19ded 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 oc = sqlite3DbMa
19dee 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64 62 2c 20 llocSize(p->db,
19def 70 4e 65 77 29 2f 73 69 7a 65 6f 66 28 4f 70 29 pNew)/sizeof(Op)
19df0 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 20 3d 20 70 ;. p->aOp = p
19df1 4e 65 77 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 New;. }. retur
19df2 6e 20 28 70 4e 65 77 20 3f 20 53 51 4c 49 54 45 n (pNew ? SQLITE
19df3 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d _OK : SQLITE_NOM
19df4 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 EM);.}../*.** Ad
19df5 64 20 61 20 6e 65 77 20 69 6e 73 74 72 75 63 74 d a new instruct
19df6 69 6f 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 ion to the list
19df7 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 of instructions
19df8 63 75 72 72 65 6e 74 20 69 6e 20 74 68 65 0a 2a current in the.*
19df9 2a 20 56 44 42 45 2e 20 20 52 65 74 75 72 6e 20 * VDBE. Return
19dfa 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 the address of t
19dfb 68 65 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 he new instructi
19dfc 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 on..**.** Parame
19dfd 74 65 72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 ters:.**.** p
19dfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 P
19dff 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 44 ointer to the VD
19e00 42 45 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 70 20 20 BE.**.** op
19e01 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 The
19e02 6f 70 63 6f 64 65 20 66 6f 72 20 74 68 69 73 20 opcode for this
19e03 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a instruction.**.*
19e04 2a 20 20 20 20 70 31 2c 20 70 32 2c 20 70 33 20 * p1, p2, p3
19e05 20 20 20 20 20 4f 70 65 72 61 6e 64 73 0a 2a 2a Operands.**
19e06 0a 2a 2a 20 55 73 65 20 74 68 65 20 73 71 6c 69 .** Use the sqli
19e07 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 te3VdbeResolveLa
19e08 62 65 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 bel() function t
19e09 6f 20 66 69 78 20 61 6e 20 61 64 64 72 65 73 73 o fix an address
19e0a 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 71 6c 69 and.** the sqli
19e0b 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 te3VdbeChangeP4(
19e0c 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 68 ) function to ch
19e0d 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f ange the value o
19e0e 66 20 74 68 65 20 50 34 0a 2a 2a 20 6f 70 65 72 f the P4.** oper
19e0f 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 and..*/.SQLITE_P
19e10 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19e11 65 33 56 64 62 65 41 64 64 4f 70 33 28 56 64 62 e3VdbeAddOp3(Vdb
19e12 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e e *p, int op, in
19e13 74 20 70 31 2c 20 69 6e 74 20 70 32 2c 20 69 6e t p1, int p2, in
19e14 74 20 70 33 29 7b 0a 20 20 69 6e 74 20 69 3b 0a t p3){. int i;.
19e15 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 0a VdbeOp *pOp;..
19e16 20 20 69 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 i = p->nOp;.
19e17 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 assert( p->magic
19e18 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 ==VDBE_MAGIC_INI
19e19 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f T );. assert( o
19e1a 70 3e 30 20 26 26 20 6f 70 3c 30 78 66 66 20 29 p>0 && op<0xff )
19e1b 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 41 6c ;. if( p->nOpAl
19e1c 6c 6f 63 3c 3d 69 20 29 7b 0a 20 20 20 20 69 66 loc<=i ){. if
19e1d 28 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29 ( growOpArray(p)
19e1e 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
19e1f 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
19e20 70 2d 3e 6e 4f 70 2b 2b 3b 0a 20 20 70 4f 70 20 p->nOp++;. pOp
19e21 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 = &p->aOp[i];.
19e22 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 28 75 pOp->opcode = (u
19e23 38 29 6f 70 3b 0a 20 20 70 4f 70 2d 3e 70 35 20 8)op;. pOp->p5
19e24 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 3d = 0;. pOp->p1 =
19e25 20 70 31 3b 0a 20 20 70 4f 70 2d 3e 70 32 20 3d p1;. pOp->p2 =
19e26 20 70 32 3b 0a 20 20 70 4f 70 2d 3e 70 33 20 3d p2;. pOp->p3 =
19e27 20 70 33 3b 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 p3;. pOp->p4.p
19e28 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 34 74 = 0;. pOp->p4t
19e29 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 ype = P4_NOTUSED
19e2a 3b 0a 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d ;. p->expired =
19e2b 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 0;.#ifdef SQLIT
19e2c 45 5f 44 45 42 55 47 0a 20 20 70 4f 70 2d 3e 7a E_DEBUG. pOp->z
19e2d 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 69 Comment = 0;. i
19e2e 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 f( sqlite3VdbeAd
19e2f 64 6f 70 54 72 61 63 65 20 29 20 73 71 6c 69 74 dopTrace ) sqlit
19e30 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c e3VdbePrintOp(0,
19e31 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b i, &p->aOp[i]);
19e32 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 56 .#endif.#ifdef V
19e33 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 70 4f DBE_PROFILE. pO
19e34 70 2d 3e 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 p->cycles = 0;.
19e35 20 70 4f 70 2d 3e 63 6e 74 20 3d 20 30 3b 0a 23 pOp->cnt = 0;.#
19e36 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 69 endif. return i
19e37 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
19e38 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
19e39 62 65 41 64 64 4f 70 30 28 56 64 62 65 20 2a 70 beAddOp0(Vdbe *p
19e3a 2c 20 69 6e 74 20 6f 70 29 7b 0a 20 20 72 65 74 , int op){. ret
19e3b 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 41 urn sqlite3VdbeA
19e3c 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 30 2c 20 ddOp3(p, op, 0,
19e3d 30 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 0, 0);.}.SQLITE_
19e3e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19e3f 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 56 64 te3VdbeAddOp1(Vd
19e40 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 be *p, int op, i
19e41 6e 74 20 70 31 29 7b 0a 20 20 72 65 74 75 72 6e nt p1){. return
19e42 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
19e43 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 30 2c p3(p, op, p1, 0,
19e44 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 0);.}.SQLITE_PR
19e45 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
19e46 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65 3VdbeAddOp2(Vdbe
19e47 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 *p, int op, int
19e48 20 70 31 2c 20 69 6e 74 20 70 32 29 7b 0a 20 20 p1, int p2){.
19e49 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 return sqlite3Vd
19e4a 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 beAddOp3(p, op,
19e4b 70 31 2c 20 70 32 2c 20 30 29 3b 0a 7d 0a 0a 0a p1, p2, 0);.}...
19e4c 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63 /*.** Add an opc
19e4d 6f 64 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65 ode that include
19e4e 73 20 74 68 65 20 70 34 20 76 61 6c 75 65 20 61 s the p4 value a
19e4f 73 20 61 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a s a pointer..*/.
19e50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19e51 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
19e52 64 4f 70 34 28 0a 20 20 56 64 62 65 20 2a 70 2c dOp4(. Vdbe *p,
19e53 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
19e54 64 64 20 74 68 65 20 6f 70 63 6f 64 65 20 74 6f dd the opcode to
19e55 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 6e this VM */. in
19e56 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 t op,
19e57 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 6f 70 63 /* The new opc
19e58 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c ode */. int p1,
19e59 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19e5a 54 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 2a The P1 operand *
19e5b 2f 0a 20 20 69 6e 74 20 70 32 2c 20 20 20 20 20 /. int p2,
19e5c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 /* The P
19e5d 32 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 2 operand */. i
19e5e 6e 74 20 70 33 2c 20 20 20 20 20 20 20 20 20 20 nt p3,
19e5f 20 20 20 2f 2a 20 54 68 65 20 50 33 20 6f 70 65 /* The P3 ope
19e60 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 rand */. const
19e61 63 68 61 72 20 2a 7a 50 34 2c 20 20 20 20 2f 2a char *zP4, /*
19e62 20 54 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 The P4 operand
19e63 2a 2f 0a 20 20 69 6e 74 20 70 34 74 79 70 65 20 */. int p4type
19e64 20 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 6f /* P4 o
19e65 70 65 72 61 6e 64 20 74 79 70 65 20 2a 2f 0a 29 perand type */.)
19e66 7b 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73 {. int addr = s
19e67 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
19e68 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20 (p, op, p1, p2,
19e69 70 33 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 p3);. sqlite3Vd
19e6a 62 65 43 68 61 6e 67 65 50 34 28 70 2c 20 61 64 beChangeP4(p, ad
19e6b 64 72 2c 20 7a 50 34 2c 20 70 34 74 79 70 65 29 dr, zP4, p4type)
19e6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b ;. return addr;
19e6d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 .}../*.** Create
19e6e 20 61 20 6e 65 77 20 73 79 6d 62 6f 6c 69 63 20 a new symbolic
19e6f 6c 61 62 65 6c 20 66 6f 72 20 61 6e 20 69 6e 73 label for an ins
19e70 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 68 61 truction that ha
19e71 73 20 79 65 74 20 74 6f 20 62 65 0a 2a 2a 20 63 s yet to be.** c
19e72 6f 64 65 64 2e 20 20 54 68 65 20 73 79 6d 62 6f oded. The symbo
19e73 6c 69 63 20 6c 61 62 65 6c 20 69 73 20 72 65 61 lic label is rea
19e74 6c 6c 79 20 6a 75 73 74 20 61 20 6e 65 67 61 74 lly just a negat
19e75 69 76 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 ive number. The
19e76 0a 2a 2a 20 6c 61 62 65 6c 20 63 61 6e 20 62 65 .** label can be
19e77 20 75 73 65 64 20 61 73 20 74 68 65 20 50 32 20 used as the P2
19e78 76 61 6c 75 65 20 6f 66 20 61 6e 20 6f 70 65 72 value of an oper
19e79 61 74 69 6f 6e 2e 20 20 4c 61 74 65 72 2c 20 77 ation. Later, w
19e7a 68 65 6e 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c hen.** the label
19e7b 20 69 73 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 is resolved to
19e7c 61 20 73 70 65 63 69 66 69 63 20 61 64 64 72 65 a specific addre
19e7d 73 73 2c 20 74 68 65 20 56 44 42 45 20 77 69 6c ss, the VDBE wil
19e7e 6c 20 73 63 61 6e 0a 2a 2a 20 74 68 72 6f 75 67 l scan.** throug
19e7f 68 20 69 74 73 20 6f 70 65 72 61 74 69 6f 6e 20 h its operation
19e80 6c 69 73 74 20 61 6e 64 20 63 68 61 6e 67 65 20 list and change
19e81 61 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 50 32 all values of P2
19e82 20 77 68 69 63 68 20 6d 61 74 63 68 0a 2a 2a 20 which match.**
19e83 74 68 65 20 6c 61 62 65 6c 20 69 6e 74 6f 20 74 the label into t
19e84 68 65 20 72 65 73 6f 6c 76 65 64 20 61 64 64 72 he resolved addr
19e85 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 ess..**.** The V
19e86 44 42 45 20 6b 6e 6f 77 73 20 74 68 61 74 20 61 DBE knows that a
19e87 20 50 32 20 76 61 6c 75 65 20 69 73 20 61 20 6c P2 value is a l
19e88 61 62 65 6c 20 62 65 63 61 75 73 65 20 6c 61 62 abel because lab
19e89 65 6c 73 20 61 72 65 0a 2a 2a 20 61 6c 77 61 79 els are.** alway
19e8a 73 20 6e 65 67 61 74 69 76 65 20 61 6e 64 20 50 s negative and P
19e8b 32 20 76 61 6c 75 65 73 20 61 72 65 20 73 75 70 2 values are sup
19e8c 70 6f 73 65 20 74 6f 20 62 65 20 6e 6f 6e 2d 6e pose to be non-n
19e8d 65 67 61 74 69 76 65 2e 0a 2a 2a 20 48 65 6e 63 egative..** Henc
19e8e 65 2c 20 61 20 6e 65 67 61 74 69 76 65 20 50 32 e, a negative P2
19e8f 20 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 value is a labe
19e90 6c 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74 l that has yet t
19e91 6f 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a o be resolved..*
19e92 2a 0a 2a 2a 20 5a 65 72 6f 20 69 73 20 72 65 74 *.** Zero is ret
19e93 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f urned if a mallo
19e94 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 c() fails..*/.SQ
19e95 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
19e96 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
19e97 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 29 7b 0a Label(Vdbe *p){.
19e98 20 20 69 6e 74 20 69 3b 0a 20 20 69 20 3d 20 70 int i;. i = p
19e99 2d 3e 6e 4c 61 62 65 6c 2b 2b 3b 0a 20 20 61 73 ->nLabel++;. as
19e9a 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d sert( p->magic==
19e9b 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 VDBE_MAGIC_INIT
19e9c 29 3b 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e 6e );. if( i>=p->n
19e9d 4c 61 62 65 6c 41 6c 6c 6f 63 20 29 7b 0a 20 20 LabelAlloc ){.
19e9e 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 4c 61 int n = p->nLa
19e9f 62 65 6c 41 6c 6c 6f 63 2a 32 20 2b 20 35 3b 0a belAlloc*2 + 5;.
19ea0 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 p->aLabel =
19ea1 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 sqlite3DbRealloc
19ea2 4f 72 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d OrFree(p->db, p-
19ea3 3e 61 4c 61 62 65 6c 2c 0a 20 20 20 20 20 20 20 >aLabel,.
19ea4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19ea5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19ea6 6e 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 4c 61 62 n*sizeof(p->aLab
19ea7 65 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 70 2d 3e el[0]));. p->
19ea8 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20 3d 20 73 71 nLabelAlloc = sq
19ea9 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a lite3DbMallocSiz
19eaa 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 e(p->db, p->aLab
19eab 65 6c 29 2f 73 69 7a 65 6f 66 28 70 2d 3e 61 4c el)/sizeof(p->aL
19eac 61 62 65 6c 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20 abel[0]);. }.
19ead 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b if( p->aLabel ){
19eae 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 69 . p->aLabel[i
19eaf 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 72 65 ] = -1;. }. re
19eb0 74 75 72 6e 20 2d 31 2d 69 3b 0a 7d 0a 0a 2f 2a turn -1-i;.}../*
19eb1 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6c 61 62 65 .** Resolve labe
19eb2 6c 20 22 78 22 20 74 6f 20 62 65 20 74 68 65 20 l "x" to be the
19eb3 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e address of the n
19eb4 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ext instruction
19eb5 74 6f 0a 2a 2a 20 62 65 20 69 6e 73 65 72 74 65 to.** be inserte
19eb6 64 2e 20 20 54 68 65 20 70 61 72 61 6d 65 74 65 d. The paramete
19eb7 72 20 22 78 22 20 6d 75 73 74 20 68 61 76 65 20 r "x" must have
19eb8 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 72 been obtained fr
19eb9 6f 6d 0a 2a 2a 20 61 20 70 72 69 6f 72 20 63 61 om.** a prior ca
19eba 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 ll to sqlite3Vdb
19ebb 65 4d 61 6b 65 4c 61 62 65 6c 28 29 2e 0a 2a 2f eMakeLabel()..*/
19ebc 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
19ebd 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
19ebe 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 64 62 ResolveLabel(Vdb
19ebf 65 20 2a 70 2c 20 69 6e 74 20 78 29 7b 0a 20 20 e *p, int x){.
19ec0 69 6e 74 20 6a 20 3d 20 2d 31 2d 78 3b 0a 20 20 int j = -1-x;.
19ec1 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 assert( p->magic
19ec2 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 ==VDBE_MAGIC_INI
19ec3 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6a T );. assert( j
19ec4 3e 3d 30 20 26 26 20 6a 3c 70 2d 3e 6e 4c 61 62 >=0 && j<p->nLab
19ec5 65 6c 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 el );. if( p->a
19ec6 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e Label ){. p->
19ec7 61 4c 61 62 65 6c 5b 6a 5d 20 3d 20 70 2d 3e 6e aLabel[j] = p->n
19ec8 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 Op;. }.}..#ifde
19ec9 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 0a f SQLITE_DEBUG..
19eca 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
19ecb 69 6e 67 20 74 79 70 65 20 61 6e 64 20 66 75 6e ing type and fun
19ecc 63 74 69 6f 6e 20 61 72 65 20 75 73 65 64 20 74 ction are used t
19ecd 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 o iterate throug
19ece 68 20 61 6c 6c 20 6f 70 63 6f 64 65 73 0a 2a 2a h all opcodes.**
19ecf 20 69 6e 20 61 20 56 64 62 65 20 6d 61 69 6e 20 in a Vdbe main
19ed0 70 72 6f 67 72 61 6d 20 61 6e 64 20 65 61 63 68 program and each
19ed1 20 6f 66 20 74 68 65 20 73 75 62 2d 70 72 6f 67 of the sub-prog
19ed2 72 61 6d 73 20 28 74 72 69 67 67 65 72 73 29 20 rams (triggers)
19ed3 69 74 20 6d 61 79 20 0a 2a 2a 20 69 6e 76 6f 6b it may .** invok
19ed4 65 20 64 69 72 65 63 74 6c 79 20 6f 72 20 69 6e e directly or in
19ed5 64 69 72 65 63 74 6c 79 2e 20 49 74 20 73 68 6f directly. It sho
19ed6 75 6c 64 20 62 65 20 75 73 65 64 20 61 73 20 66 uld be used as f
19ed7 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 ollows:.**.**
19ed8 4f 70 20 2a 70 4f 70 3b 0a 2a 2a 20 20 20 56 64 Op *pOp;.** Vd
19ed9 62 65 4f 70 49 74 65 72 20 73 49 74 65 72 3b 0a beOpIter sIter;.
19eda 2a 2a 0a 2a 2a 20 20 20 6d 65 6d 73 65 74 28 26 **.** memset(&
19edb 73 49 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 sIter, 0, sizeof
19edc 28 73 49 74 65 72 29 29 3b 0a 2a 2a 20 20 20 73 (sIter));.** s
19edd 49 74 65 72 2e 76 20 3d 20 76 3b 20 20 20 20 20 Iter.v = v;
19ede 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19edf 20 20 20 20 20 20 20 2f 2f 20 76 20 69 73 20 6f // v is o
19ee0 66 20 74 79 70 65 20 56 64 62 65 2a 20 0a 2a 2a f type Vdbe* .**
19ee1 20 20 20 77 68 69 6c 65 28 20 28 70 4f 70 20 3d while( (pOp =
19ee2 20 6f 70 49 74 65 72 4e 65 78 74 28 26 73 49 74 opIterNext(&sIt
19ee3 65 72 29 29 20 29 7b 0a 2a 2a 20 20 20 20 20 2f er)) ){.** /
19ee4 2f 20 44 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 / Do something w
19ee5 69 74 68 20 70 4f 70 0a 2a 2a 20 20 20 7d 0a 2a ith pOp.** }.*
19ee6 2a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 * sqlite3DbFre
19ee7 65 28 76 2d 3e 64 62 2c 20 73 49 74 65 72 2e 61 e(v->db, sIter.a
19ee8 70 53 75 62 29 3b 0a 2a 2a 20 0a 2a 2f 0a 74 79 pSub);.** .*/.ty
19ee9 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 pedef struct Vdb
19eea 65 4f 70 49 74 65 72 20 56 64 62 65 4f 70 49 74 eOpIter VdbeOpIt
19eeb 65 72 3b 0a 73 74 72 75 63 74 20 56 64 62 65 4f er;.struct VdbeO
19eec 70 49 74 65 72 20 7b 0a 20 20 56 64 62 65 20 2a pIter {. Vdbe *
19eed 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 v;
19eee 20 20 20 20 20 2f 2a 20 56 64 62 65 20 74 6f 20 /* Vdbe to
19eef 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 iterate through
19ef0 74 68 65 20 6f 70 63 6f 64 65 73 20 6f 66 20 2a the opcodes of *
19ef1 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a /. SubProgram *
19ef2 2a 61 70 53 75 62 3b 20 20 20 20 20 20 20 20 2f *apSub; /
19ef3 2a 20 41 72 72 61 79 20 6f 66 20 73 75 62 70 72 * Array of subpr
19ef4 6f 67 72 61 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 ograms */. int
19ef5 6e 53 75 62 3b 20 20 20 20 20 20 20 20 20 20 20 nSub;
19ef6 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
19ef7 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 of entries in a
19ef8 70 53 75 62 20 2a 2f 0a 20 20 69 6e 74 20 69 41 pSub */. int iA
19ef9 64 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 ddr;
19efa 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 /* Address
19efb 6f 66 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 of next instruct
19efc 69 6f 6e 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f ion to return */
19efd 0a 20 20 69 6e 74 20 69 53 75 62 3b 20 20 20 20 . int iSub;
19efe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19eff 20 30 20 3d 20 6d 61 69 6e 20 70 72 6f 67 72 61 0 = main progra
19f00 6d 2c 20 31 20 3d 20 66 69 72 73 74 20 73 75 62 m, 1 = first sub
19f01 2d 70 72 6f 67 72 61 6d 20 65 74 63 2e 20 2a 2f -program etc. */
19f02 0a 7d 3b 0a 73 74 61 74 69 63 20 4f 70 20 2a 6f .};.static Op *o
19f03 70 49 74 65 72 4e 65 78 74 28 56 64 62 65 4f 70 pIterNext(VdbeOp
19f04 49 74 65 72 20 2a 70 29 7b 0a 20 20 56 64 62 65 Iter *p){. Vdbe
19f05 20 2a 76 20 3d 20 70 2d 3e 76 3b 0a 20 20 4f 70 *v = p->v;. Op
19f06 20 2a 70 52 65 74 20 3d 20 30 3b 0a 20 20 4f 70 *pRet = 0;. Op
19f07 20 2a 61 4f 70 3b 0a 20 20 69 6e 74 20 6e 4f 70 *aOp;. int nOp
19f08 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 69 53 75 62 ;.. if( p->iSub
19f09 3c 3d 70 2d 3e 6e 53 75 62 20 29 7b 0a 0a 20 20 <=p->nSub ){..
19f0a 20 20 69 66 28 20 70 2d 3e 69 53 75 62 3d 3d 30 if( p->iSub==0
19f0b 20 29 7b 0a 20 20 20 20 20 20 61 4f 70 20 3d 20 ){. aOp =
19f0c 76 2d 3e 61 4f 70 3b 0a 20 20 20 20 20 20 6e 4f v->aOp;. nO
19f0d 70 20 3d 20 76 2d 3e 6e 4f 70 3b 0a 20 20 20 20 p = v->nOp;.
19f0e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 4f 70 }else{. aOp
19f0f 20 3d 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e 69 = p->apSub[p->i
19f10 53 75 62 2d 31 5d 2d 3e 61 4f 70 3b 0a 20 20 20 Sub-1]->aOp;.
19f11 20 20 20 6e 4f 70 20 3d 20 70 2d 3e 61 70 53 75 nOp = p->apSu
19f12 62 5b 70 2d 3e 69 53 75 62 2d 31 5d 2d 3e 6e 4f b[p->iSub-1]->nO
19f13 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 p;. }. ass
19f14 65 72 74 28 20 70 2d 3e 69 41 64 64 72 3c 6e 4f ert( p->iAddr<nO
19f15 70 20 29 3b 0a 0a 20 20 20 20 70 52 65 74 20 3d p );.. pRet =
19f16 20 26 61 4f 70 5b 70 2d 3e 69 41 64 64 72 5d 3b &aOp[p->iAddr];
19f17 0a 20 20 20 20 70 2d 3e 69 41 64 64 72 2b 2b 3b . p->iAddr++;
19f18 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 41 64 64 . if( p->iAdd
19f19 72 3d 3d 6e 4f 70 20 29 7b 0a 20 20 20 20 20 20 r==nOp ){.
19f1a 70 2d 3e 69 53 75 62 2b 2b 3b 0a 20 20 20 20 20 p->iSub++;.
19f1b 20 70 2d 3e 69 41 64 64 72 20 3d 20 30 3b 0a 20 p->iAddr = 0;.
19f1c 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 }. . if(
19f1d 70 52 65 74 2d 3e 70 34 74 79 70 65 3d 3d 50 34 pRet->p4type==P4
19f1e 5f 53 55 42 50 52 4f 47 52 41 4d 20 29 7b 0a 20 _SUBPROGRAM ){.
19f1f 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d int nByte =
19f20 20 28 70 2d 3e 6e 53 75 62 2b 31 29 2a 73 69 7a (p->nSub+1)*siz
19f21 65 6f 66 28 53 75 62 50 72 6f 67 72 61 6d 2a 29 eof(SubProgram*)
19f22 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 ;. int j;.
19f23 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c for(j=0; j<
19f24 70 2d 3e 6e 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20 p->nSub; j++){.
19f25 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 61 70 if( p->ap
19f26 53 75 62 5b 6a 5d 3d 3d 70 52 65 74 2d 3e 70 34 Sub[j]==pRet->p4
19f27 2e 70 50 72 6f 67 72 61 6d 20 29 20 62 72 65 61 .pProgram ) brea
19f28 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
19f29 20 69 66 28 20 6a 3d 3d 70 2d 3e 6e 53 75 62 20 if( j==p->nSub
19f2a 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 70 ){. p->ap
19f2b 53 75 62 20 3d 20 73 71 6c 69 74 65 33 44 62 52 Sub = sqlite3DbR
19f2c 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 76 2d 3e eallocOrFree(v->
19f2d 64 62 2c 20 70 2d 3e 61 70 53 75 62 2c 20 6e 42 db, p->apSub, nB
19f2e 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 yte);. if
19f2f 28 20 21 70 2d 3e 61 70 53 75 62 20 29 7b 0a 20 ( !p->apSub ){.
19f30 20 20 20 20 20 20 20 20 20 70 52 65 74 20 3d 20 pRet =
19f31 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0;. }else
19f32 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 {. p->a
19f33 70 53 75 62 5b 70 2d 3e 6e 53 75 62 2b 2b 5d 20 pSub[p->nSub++]
19f34 3d 20 70 52 65 74 2d 3e 70 34 2e 70 50 72 6f 67 = pRet->p4.pProg
19f35 72 61 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ram;. }.
19f36 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
19f37 0a 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b .. return pRet;
19f38 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
19f39 20 74 72 75 65 20 69 66 20 74 68 65 20 70 72 6f true if the pro
19f3a 67 72 61 6d 20 73 74 6f 72 65 64 20 69 6e 20 74 gram stored in t
19f3b 68 65 20 56 4d 20 70 61 73 73 65 64 20 61 73 20 he VM passed as
19f3c 61 6e 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 0a an argument may.
19f3d 2a 2a 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52 ** throw an ABOR
19f3e 54 20 65 78 63 65 70 74 69 6f 6e 20 28 63 61 75 T exception (cau
19f3f 73 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d 65 sing the stateme
19f40 6e 74 2c 20 62 75 74 20 6e 6f 74 20 74 72 61 6e nt, but not tran
19f41 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 saction.** to be
19f42 20 72 6f 6c 6c 65 64 20 62 61 63 6b 29 2e 20 54 rolled back). T
19f43 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 his condition is
19f44 20 74 72 75 65 20 69 66 20 74 68 65 20 6d 61 69 true if the mai
19f45 6e 20 70 72 6f 67 72 61 6d 20 6f 72 20 61 6e 79 n program or any
19f46 0a 2a 2a 20 73 75 62 2d 70 72 6f 67 72 61 6d 73 .** sub-programs
19f47 20 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 6f 66 contains any of
19f48 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
19f49 2a 2a 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61 **.** * OP_Ha
19f4a 6c 74 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 lt with P1=SQLIT
19f4b 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 E_CONSTRAINT and
19f4c 20 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a P2=OE_Abort..**
19f4d 20 20 20 2a 20 20 4f 50 5f 48 61 6c 74 49 66 4e * OP_HaltIfN
19f4e 75 6c 6c 20 77 69 74 68 20 50 31 3d 53 51 4c 49 ull with P1=SQLI
19f4f 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e TE_CONSTRAINT an
19f50 64 20 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a d P2=OE_Abort..*
19f51 2a 20 20 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f * * OP_Destro
19f52 79 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70 y.** * OP_VUp
19f53 64 61 74 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f date.** * OP_
19f54 56 52 65 6e 61 6d 65 0a 2a 2a 0a 2a 2a 20 54 68 VRename.**.** Th
19f55 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f is function is o
19f56 6e 6c 79 20 75 73 65 64 20 61 73 20 70 61 72 74 nly used as part
19f57 20 6f 66 20 61 6e 20 61 73 73 65 72 74 28 29 20 of an assert()
19f58 73 74 61 74 65 6d 65 6e 74 2e 20 0a 2a 2f 0a 53 statement. .*/.S
19f59 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
19f5a 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 79 t sqlite3VdbeMay
19f5b 41 62 6f 72 74 28 56 64 62 65 20 2a 76 29 7b 0a Abort(Vdbe *v){.
19f5c 20 20 69 6e 74 20 6d 61 79 41 62 6f 72 74 20 3d int mayAbort =
19f5d 20 30 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 0;. Op *pOp;.
19f5e 20 56 64 62 65 4f 70 49 74 65 72 20 73 49 74 65 VdbeOpIter sIte
19f5f 72 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 49 74 r;. memset(&sIt
19f60 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 49 er, 0, sizeof(sI
19f61 74 65 72 29 29 3b 0a 20 20 73 49 74 65 72 2e 76 ter));. sIter.v
19f62 20 3d 20 76 3b 0a 0a 20 20 77 68 69 6c 65 28 20 = v;.. while(
19f63 28 70 4f 70 20 3d 20 6f 70 49 74 65 72 4e 65 78 (pOp = opIterNex
19f64 74 28 26 73 49 74 65 72 29 29 21 3d 30 20 29 7b t(&sIter))!=0 ){
19f65 0a 20 20 20 20 69 6e 74 20 6f 70 63 6f 64 65 20 . int opcode
19f66 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 = pOp->opcode;.
19f67 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f if( opcode==O
19f68 50 5f 44 65 73 74 72 6f 79 20 7c 7c 20 6f 70 63 P_Destroy || opc
19f69 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20 ode==OP_VUpdate
19f6a 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 52 || opcode==OP_VR
19f6b 65 6e 61 6d 65 20 0a 20 20 20 20 20 7c 7c 20 28 ename . || (
19f6c 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 (opcode==OP_Halt
19f6d 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 || opcode==OP_H
19f6e 61 6c 74 49 66 4e 75 6c 6c 29 20 0a 20 20 20 20 altIfNull) .
19f6f 20 20 26 26 20 28 70 4f 70 2d 3e 70 31 3d 3d 53 && (pOp->p1==S
19f70 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 QLITE_CONSTRAINT
19f71 20 26 26 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f && pOp->p2==OE_
19f72 41 62 6f 72 74 29 29 0a 20 20 20 20 29 7b 0a 20 Abort)). ){.
19f73 20 20 20 20 20 6d 61 79 41 62 6f 72 74 20 3d 20 mayAbort =
19f74 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 1;. break;.
19f75 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c }. }.. sql
19f76 69 74 65 33 44 62 46 72 65 65 28 76 2d 3e 64 62 ite3DbFree(v->db
19f77 2c 20 73 49 74 65 72 2e 61 70 53 75 62 29 3b 0a , sIter.apSub);.
19f78 20 20 72 65 74 75 72 6e 20 6d 61 79 41 62 6f 72 return mayAbor
19f79 74 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a t;.}.#endif../*.
19f7a 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 ** Loop through
19f7b 74 68 65 20 70 72 6f 67 72 61 6d 20 6c 6f 6f 6b the program look
19f7c 69 6e 67 20 66 6f 72 20 50 32 20 76 61 6c 75 65 ing for P2 value
19f7d 73 20 74 68 61 74 20 61 72 65 20 6e 65 67 61 74 s that are negat
19f7e 69 76 65 0a 2a 2a 20 6f 6e 20 6a 75 6d 70 20 69 ive.** on jump i
19f7f 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20 45 61 nstructions. Ea
19f80 63 68 20 73 75 63 68 20 76 61 6c 75 65 20 69 73 ch such value is
19f81 20 61 20 6c 61 62 65 6c 2e 20 20 52 65 73 6f 6c a label. Resol
19f82 76 65 20 74 68 65 0a 2a 2a 20 6c 61 62 65 6c 20 ve the.** label
19f83 62 79 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 by setting the P
19f84 32 20 76 61 6c 75 65 20 74 6f 20 69 74 73 20 63 2 value to its c
19f85 6f 72 72 65 63 74 20 6e 6f 6e 2d 7a 65 72 6f 20 orrect non-zero
19f86 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 value..**.** Thi
19f87 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
19f88 6c 65 64 20 6f 6e 63 65 20 61 66 74 65 72 20 61 led once after a
19f89 6c 6c 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20 ll opcodes have
19f8a 62 65 65 6e 20 69 6e 73 65 72 74 65 64 2e 0a 2a been inserted..*
19f8b 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 2a 70 *.** Variable *p
19f8c 4d 61 78 46 75 6e 63 41 72 67 73 20 69 73 20 73 MaxFuncArgs is s
19f8d 65 74 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 et to the maximu
19f8e 6d 20 76 61 6c 75 65 20 6f 66 20 61 6e 79 20 50 m value of any P
19f8f 32 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 74 2 argument .** t
19f90 6f 20 61 6e 20 4f 50 5f 46 75 6e 63 74 69 6f 6e o an OP_Function
19f91 2c 20 4f 50 5f 41 67 67 53 74 65 70 20 6f 72 20 , OP_AggStep or
19f92 4f 50 5f 56 46 69 6c 74 65 72 20 6f 70 63 6f 64 OP_VFilter opcod
19f93 65 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 e. This is used
19f94 62 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 by .** sqlite3Vd
19f95 62 65 4d 61 6b 65 52 65 61 64 79 28 29 20 74 6f beMakeReady() to
19f96 20 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e 61 size the Vdbe.a
19f97 70 41 72 67 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f pArg[] array..*/
19f98 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 .static void res
19f99 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 56 64 62 olveP2Values(Vdb
19f9a 65 20 2a 70 2c 20 69 6e 74 20 2a 70 4d 61 78 46 e *p, int *pMaxF
19f9b 75 6e 63 41 72 67 73 29 7b 0a 20 20 69 6e 74 20 uncArgs){. int
19f9c 69 3b 0a 20 20 69 6e 74 20 6e 4d 61 78 41 72 67 i;. int nMaxArg
19f9d 73 20 3d 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 s = *pMaxFuncArg
19f9e 73 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 s;. Op *pOp;.
19f9f 69 6e 74 20 2a 61 4c 61 62 65 6c 20 3d 20 70 2d int *aLabel = p-
19fa0 3e 61 4c 61 62 65 6c 3b 0a 20 20 70 2d 3e 72 65 >aLabel;. p->re
19fa1 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 66 6f adOnly = 1;. fo
19fa2 72 28 70 4f 70 3d 70 2d 3e 61 4f 70 2c 20 69 3d r(pOp=p->aOp, i=
19fa3 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e 3d 30 3b 20 p->nOp-1; i>=0;
19fa4 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 i--, pOp++){.
19fa5 20 75 38 20 6f 70 63 6f 64 65 20 3d 20 70 4f 70 u8 opcode = pOp
19fa6 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20 20 20 20 69 ->opcode;.. i
19fa7 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 75 f( opcode==OP_Fu
19fa8 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70 63 6f 64 65 nction || opcode
19fa9 3d 3d 4f 50 5f 41 67 67 53 74 65 70 20 29 7b 0a ==OP_AggStep ){.
19faa 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 if( pOp->p
19fab 35 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 5>nMaxArgs ) nMa
19fac 78 41 72 67 73 20 3d 20 70 4f 70 2d 3e 70 35 3b xArgs = pOp->p5;
19fad 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
19fae 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
19faf 45 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 E. }else if(
19fb0 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61 opcode==OP_VUpda
19fb1 74 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 te ){. if(
19fb2 70 4f 70 2d 3e 70 32 3e 6e 4d 61 78 41 72 67 73 pOp->p2>nMaxArgs
19fb3 20 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f ) nMaxArgs = pO
19fb4 70 2d 3e 70 32 3b 0a 23 65 6e 64 69 66 0a 20 20 p->p2;.#endif.
19fb5 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f }else if( opco
19fb6 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 61 63 74 69 de==OP_Transacti
19fb7 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 32 21 3d 30 on && pOp->p2!=0
19fb8 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 65 61 ){. p->rea
19fb9 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69 66 6e 64 dOnly = 0;.#ifnd
19fba 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
19fbb 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 IRTUALTABLE.
19fbc 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 }else if( opcode
19fbd 3d 3d 4f 50 5f 56 46 69 6c 74 65 72 20 29 7b 0a ==OP_VFilter ){.
19fbe 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 int n;.
19fbf 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f assert( p->nO
19fc0 70 20 2d 20 69 20 3e 3d 20 33 20 29 3b 0a 20 20 p - i >= 3 );.
19fc1 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b assert( pOp[
19fc2 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 -1].opcode==OP_I
19fc3 6e 74 65 67 65 72 20 29 3b 0a 20 20 20 20 20 20 nteger );.
19fc4 6e 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 31 3b 0a n = pOp[-1].p1;.
19fc5 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 4d 61 78 if( n>nMax
19fc6 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 Args ) nMaxArgs
19fc7 3d 20 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 = n;.#endif.
19fc8 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 }.. if( sqlit
19fc9 65 33 56 64 62 65 4f 70 63 6f 64 65 48 61 73 50 e3VdbeOpcodeHasP
19fca 72 6f 70 65 72 74 79 28 6f 70 63 6f 64 65 2c 20 roperty(opcode,
19fcb 4f 50 46 4c 47 5f 4a 55 4d 50 29 20 26 26 20 70 OPFLG_JUMP) && p
19fcc 4f 70 2d 3e 70 32 3c 30 20 29 7b 0a 20 20 20 20 Op->p2<0 ){.
19fcd 20 20 61 73 73 65 72 74 28 20 2d 31 2d 70 4f 70 assert( -1-pOp
19fce 2d 3e 70 32 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 ->p2<p->nLabel )
19fcf 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 ;. pOp->p2
19fd0 3d 20 61 4c 61 62 65 6c 5b 2d 31 2d 70 4f 70 2d = aLabel[-1-pOp-
19fd1 3e 70 32 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a >p2];. }. }.
19fd2 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
19fd3 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c p->db, p->aLabel
19fd4 29 3b 0a 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d );. p->aLabel =
19fd5 20 30 3b 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63 0;.. *pMaxFunc
19fd6 41 72 67 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b Args = nMaxArgs;
19fd7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
19fd8 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 the address of
19fd9 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 the next instruc
19fda 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 73 65 72 tion to be inser
19fdb 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ted..*/.SQLITE_P
19fdc 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19fdd 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 e3VdbeCurrentAdd
19fde 72 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 61 73 r(Vdbe *p){. as
19fdf 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d sert( p->magic==
19fe0 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 VDBE_MAGIC_INIT
19fe1 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e );. return p->n
19fe2 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 Op;.}../*.** Thi
19fe3 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
19fe4 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
19fe5 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f 70 63 the array of opc
19fe6 6f 64 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 odes associated
19fe7 77 69 74 68 0a 2a 2a 20 74 68 65 20 56 64 62 65 with.** the Vdbe
19fe8 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 passed as the f
19fe9 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 49 irst argument. I
19fea 74 20 69 73 20 74 68 65 20 63 61 6c 6c 65 72 73 t is the callers
19feb 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 0a responsibility.
19fec 2a 2a 20 74 6f 20 61 72 72 61 6e 67 65 20 66 6f ** to arrange fo
19fed 72 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 61 r the returned a
19fee 72 72 61 79 20 74 6f 20 62 65 20 65 76 65 6e 74 rray to be event
19fef 75 61 6c 6c 79 20 66 72 65 65 64 20 75 73 69 6e ually freed usin
19ff0 67 20 74 68 65 20 0a 2a 2a 20 76 64 62 65 46 72 g the .** vdbeFr
19ff1 65 65 4f 70 41 72 72 61 79 28 29 20 66 75 6e 63 eeOpArray() func
19ff2 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f tion..**.** Befo
19ff3 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 re returning, *p
19ff4 6e 4f 70 20 69 73 20 73 65 74 20 74 6f 20 74 68 nOp is set to th
19ff5 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 e number of entr
19ff6 69 65 73 20 69 6e 20 74 68 65 20 72 65 74 75 72 ies in the retur
19ff7 6e 65 64 0a 2a 2a 20 61 72 72 61 79 2e 20 41 6c ned.** array. Al
19ff8 73 6f 2c 20 2a 70 6e 4d 61 78 41 72 67 20 69 73 so, *pnMaxArg is
19ff9 20 73 65 74 20 74 6f 20 74 68 65 20 6c 61 72 67 set to the larg
19ffa 65 72 20 6f 66 20 69 74 73 20 63 75 72 72 65 6e er of its curren
19ffb 74 20 76 61 6c 75 65 20 61 6e 64 20 0a 2a 2a 20 t value and .**
19ffc 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e the number of en
19ffd 74 72 69 65 73 20 69 6e 20 74 68 65 20 56 64 62 tries in the Vdb
19ffe 65 2e 61 70 41 72 67 5b 5d 20 61 72 72 61 79 20 e.apArg[] array
19fff 72 65 71 75 69 72 65 64 20 74 6f 20 65 78 65 63 required to exec
1a000 75 74 65 20 74 68 65 20 0a 2a 2a 20 72 65 74 75 ute the .** retu
1a001 72 6e 65 64 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f rned program..*/
1a002 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a003 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 VdbeOp *sqlite3V
1a004 64 62 65 54 61 6b 65 4f 70 41 72 72 61 79 28 56 dbeTakeOpArray(V
1a005 64 62 65 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 4f dbe *p, int *pnO
1a006 70 2c 20 69 6e 74 20 2a 70 6e 4d 61 78 41 72 67 p, int *pnMaxArg
1a007 29 7b 0a 20 20 56 64 62 65 4f 70 20 2a 61 4f 70 ){. VdbeOp *aOp
1a008 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 61 73 73 = p->aOp;. ass
1a009 65 72 74 28 20 61 4f 70 20 26 26 20 21 70 2d 3e ert( aOp && !p->
1a00a 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1a00b 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 );.. /* Check
1a00c 74 68 61 74 20 73 71 6c 69 74 65 33 56 64 62 65 that sqlite3Vdbe
1a00d 55 73 65 73 42 74 72 65 65 28 29 20 77 61 73 20 UsesBtree() was
1a00e 6e 6f 74 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 not called on th
1a00f 69 73 20 56 4d 20 2a 2f 0a 20 20 61 73 73 65 72 is VM */. asser
1a010 74 28 20 70 2d 3e 61 4d 75 74 65 78 2e 6e 4d 75 t( p->aMutex.nMu
1a011 74 65 78 3d 3d 30 20 29 3b 0a 0a 20 20 72 65 73 tex==0 );.. res
1a012 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 70 2c 20 olveP2Values(p,
1a013 70 6e 4d 61 78 41 72 67 29 3b 0a 20 20 2a 70 6e pnMaxArg);. *pn
1a014 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 70 Op = p->nOp;. p
1a015 2d 3e 61 4f 70 20 3d 20 30 3b 0a 20 20 72 65 74 ->aOp = 0;. ret
1a016 75 72 6e 20 61 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a urn aOp;.}../*.*
1a017 2a 20 41 64 64 20 61 20 77 68 6f 6c 65 20 6c 69 * Add a whole li
1a018 73 74 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 st of operations
1a019 20 74 6f 20 74 68 65 20 6f 70 65 72 61 74 69 6f to the operatio
1a01a 6e 20 73 74 61 63 6b 2e 20 20 52 65 74 75 72 6e n stack. Return
1a01b 20 74 68 65 0a 2a 2a 20 61 64 64 72 65 73 73 20 the.** address
1a01c 6f 66 20 74 68 65 20 66 69 72 73 74 20 6f 70 65 of the first ope
1a01d 72 61 74 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2f ration added..*/
1a01e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a01f 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
1a020 64 64 4f 70 4c 69 73 74 28 56 64 62 65 20 2a 70 ddOpList(Vdbe *p
1a021 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 4f , int nOp, VdbeO
1a022 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f 70 pList const *aOp
1a023 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 ){. int addr;.
1a024 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 assert( p->magi
1a025 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e c==VDBE_MAGIC_IN
1a026 49 54 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e IT );. if( p->n
1a027 4f 70 20 2b 20 6e 4f 70 20 3e 20 70 2d 3e 6e 4f Op + nOp > p->nO
1a028 70 41 6c 6c 6f 63 20 26 26 20 67 72 6f 77 4f 70 pAlloc && growOp
1a029 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20 20 Array(p) ){.
1a02a 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
1a02b 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 addr = p->nOp;.
1a02c 20 69 66 28 20 41 4c 57 41 59 53 28 6e 4f 70 3e if( ALWAYS(nOp>
1a02d 30 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0) ){. int i;
1a02e 0a 20 20 20 20 56 64 62 65 4f 70 4c 69 73 74 20 . VdbeOpList
1a02f 63 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61 4f 70 const *pIn = aOp
1a030 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
1a031 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e 2b 2b <nOp; i++, pIn++
1a032 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 20 ){. int p2
1a033 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 20 20 = pIn->p2;.
1a034 20 56 64 62 65 4f 70 20 2a 70 4f 75 74 20 3d 20 VdbeOp *pOut =
1a035 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 5d 3b &p->aOp[i+addr];
1a036 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f 70 63 . pOut->opc
1a037 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 6f 64 ode = pIn->opcod
1a038 65 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 e;. pOut->p
1a039 31 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 20 20 1 = pIn->p1;.
1a03a 20 20 20 69 66 28 20 70 32 3c 30 20 26 26 20 73 if( p2<0 && s
1a03b 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 qlite3VdbeOpcode
1a03c 48 61 73 50 72 6f 70 65 72 74 79 28 70 4f 75 74 HasProperty(pOut
1a03d 2d 3e 6f 70 63 6f 64 65 2c 20 4f 50 46 4c 47 5f ->opcode, OPFLG_
1a03e 4a 55 4d 50 29 20 29 7b 0a 20 20 20 20 20 20 20 JUMP) ){.
1a03f 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64 72 pOut->p2 = addr
1a040 20 2b 20 41 44 44 52 28 70 32 29 3b 0a 20 20 20 + ADDR(p2);.
1a041 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1a042 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 3b pOut->p2 = p2;
1a043 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
1a044 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e 70 Out->p3 = pIn->p
1a045 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 3;. pOut->p
1a046 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 4type = P4_NOTUS
1a047 45 44 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e ED;. pOut->
1a048 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 p4.p = 0;.
1a049 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23 69 pOut->p5 = 0;.#i
1a04a 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1a04b 47 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a 43 G. pOut->zC
1a04c 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 omment = 0;.
1a04d 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
1a04e 65 41 64 64 6f 70 54 72 61 63 65 20 29 7b 0a 20 eAddopTrace ){.
1a04f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1a050 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b 61 bePrintOp(0, i+a
1a051 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 ddr, &p->aOp[i+a
1a052 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23 ddr]);. }.#
1a053 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 endif. }.
1a054 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a 20 p->nOp += nOp;.
1a055 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 }. return addr
1a056 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
1a057 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 e the value of t
1a058 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 66 6f he P1 operand fo
1a059 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 r a specific ins
1a05a 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 truction..** Thi
1a05b 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 s routine is use
1a05c 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 ful when a large
1a05d 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 program is load
1a05e 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 ed from a.** sta
1a05f 74 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 tic array using
1a060 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1a061 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 List but we want
1a062 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 to make a.** fe
1a063 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 w minor changes
1a064 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a to the program..
1a065 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a066 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1a067 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 20 beChangeP1(Vdbe
1a068 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e *p, int addr, in
1a069 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 t val){. assert
1a06a 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 ( p!=0 );. asse
1a06b 72 74 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a 20 rt( addr>=0 );.
1a06c 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 if( p->nOp>addr
1a06d 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61 ){. p->aOp[a
1a06e 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c 3b 0a 20 ddr].p1 = val;.
1a06f 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e }.}../*.** Chan
1a070 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ge the value of
1a071 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20 66 the P2 operand f
1a072 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e or a specific in
1a073 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 struction..** Th
1a074 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
1a075 65 66 75 6c 20 66 6f 72 20 73 65 74 74 69 6e 67 eful for setting
1a076 20 61 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 a jump destinat
1a077 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
1a078 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1a079 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 te3VdbeChangeP2(
1a07a 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 Vdbe *p, int add
1a07b 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 r, int val){. a
1a07c 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
1a07d 20 61 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30 assert( addr>=0
1a07e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 );. if( p->nOp
1a07f 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e >addr ){. p->
1a080 61 4f 70 5b 61 64 64 72 5d 2e 70 32 20 3d 20 76 aOp[addr].p2 = v
1a081 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a al;. }.}../*.**
1a082 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 Change the valu
1a083 65 20 6f 66 20 74 68 65 20 50 33 20 6f 70 65 72 e of the P3 oper
1a084 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 and for a specif
1a085 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a ic instruction..
1a086 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a087 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1a088 62 65 43 68 61 6e 67 65 50 33 28 56 64 62 65 20 beChangeP3(Vdbe
1a089 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e *p, int addr, in
1a08a 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 t val){. assert
1a08b 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 ( p!=0 );. asse
1a08c 72 74 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a 20 rt( addr>=0 );.
1a08d 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 if( p->nOp>addr
1a08e 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61 ){. p->aOp[a
1a08f 64 64 72 5d 2e 70 33 20 3d 20 76 61 6c 3b 0a 20 ddr].p3 = val;.
1a090 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e }.}../*.** Chan
1a091 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ge the value of
1a092 74 68 65 20 50 35 20 6f 70 65 72 61 6e 64 20 66 the P5 operand f
1a093 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 or the most rece
1a094 6e 74 6c 79 0a 2a 2a 20 61 64 64 65 64 20 6f 70 ntly.** added op
1a095 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 eration..*/.SQLI
1a096 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1a097 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1a098 65 50 35 28 56 64 62 65 20 2a 70 2c 20 75 38 20 eP5(Vdbe *p, u8
1a099 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 val){. assert(
1a09a 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d p!=0 );. if( p-
1a09b 3e 61 4f 70 20 29 7b 0a 20 20 20 20 61 73 73 65 >aOp ){. asse
1a09c 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a rt( p->nOp>0 );.
1a09d 20 20 20 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f p->aOp[p->nO
1a09e 70 2d 31 5d 2e 70 35 20 3d 20 76 61 6c 3b 0a 20 p-1].p5 = val;.
1a09f 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e }.}../*.** Chan
1a0a0 67 65 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e ge the P2 operan
1a0a1 64 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e d of instruction
1a0a2 20 61 64 64 72 20 73 6f 20 74 68 61 74 20 69 74 addr so that it
1a0a3 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 points to.** th
1a0a4 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 e address of the
1a0a5 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f next instructio
1a0a6 6e 20 74 6f 20 62 65 20 63 6f 64 65 64 2e 0a 2a n to be coded..*
1a0a7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1a0a8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
1a0a9 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65 20 2a eJumpHere(Vdbe *
1a0aa 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 p, int addr){.
1a0ab 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1a0ac 65 50 32 28 70 2c 20 61 64 64 72 2c 20 70 2d 3e eP2(p, addr, p->
1a0ad 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 nOp);.}.../*.**
1a0ae 49 66 20 74 68 65 20 69 6e 70 75 74 20 46 75 6e If the input Fun
1a0af 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20 69 cDef structure i
1a0b0 73 20 65 70 68 65 6d 65 72 61 6c 2c 20 74 68 65 s ephemeral, the
1a0b1 6e 20 66 72 65 65 20 69 74 2e 20 20 49 66 0a 2a n free it. If.*
1a0b2 2a 20 74 68 65 20 46 75 6e 63 44 65 66 20 69 73 * the FuncDef is
1a0b3 20 6e 6f 74 20 65 70 68 65 72 6d 61 6c 2c 20 74 not ephermal, t
1a0b4 68 65 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a hen do nothing..
1a0b5 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 */.static void f
1a0b6 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 reeEphemeralFunc
1a0b7 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 tion(sqlite3 *db
1a0b8 2c 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 29 , FuncDef *pDef)
1a0b9 7b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 {. if( ALWAYS(p
1a0ba 44 65 66 29 20 26 26 20 28 70 44 65 66 2d 3e 66 Def) && (pDef->f
1a0bb 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 lags & SQLITE_FU
1a0bc 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b 0a NC_EPHEM)!=0 ){.
1a0bd 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1a0be 65 28 64 62 2c 20 70 44 65 66 29 3b 0a 20 20 7d e(db, pDef);. }
1a0bf 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
1a0c0 20 61 20 50 34 20 76 61 6c 75 65 20 69 66 20 6e a P4 value if n
1a0c1 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 ecessary..*/.sta
1a0c2 74 69 63 20 76 6f 69 64 20 66 72 65 65 50 34 28 tic void freeP4(
1a0c3 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 sqlite3 *db, int
1a0c4 20 70 34 74 79 70 65 2c 20 76 6f 69 64 20 2a 70 p4type, void *p
1a0c5 34 29 7b 0a 20 20 69 66 28 20 70 34 20 29 7b 0a 4){. if( p4 ){.
1a0c6 20 20 20 20 73 77 69 74 63 68 28 20 70 34 74 79 switch( p4ty
1a0c7 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 pe ){. case
1a0c8 20 50 34 5f 52 45 41 4c 3a 0a 20 20 20 20 20 20 P4_REAL:.
1a0c9 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 0a 20 case P4_INT64:.
1a0ca 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d 50 52 case P4_MPR
1a0cb 49 4e 54 46 3a 0a 20 20 20 20 20 20 63 61 73 65 INTF:. case
1a0cc 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20 20 P4_DYNAMIC:.
1a0cd 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e case P4_KEYIN
1a0ce 46 4f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 FO:. case P
1a0cf 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20 20 20 4_INTARRAY:.
1a0d0 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 case P4_KEYINF
1a0d1 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20 O_HANDOFF: {.
1a0d2 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1a0d3 65 65 28 64 62 2c 20 70 34 29 3b 0a 20 20 20 20 ee(db, p4);.
1a0d4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1a0d5 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 }. case P4
1a0d6 5f 56 44 42 45 46 55 4e 43 3a 20 7b 0a 20 20 20 _VDBEFUNC: {.
1a0d7 20 20 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 VdbeFunc *p
1a0d8 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65 VdbeFunc = (Vdbe
1a0d9 46 75 6e 63 20 2a 29 70 34 3b 0a 20 20 20 20 20 Func *)p4;.
1a0da 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c freeEphemeral
1a0db 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 56 64 Function(db, pVd
1a0dc 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b 0a beFunc->pFunc);.
1a0dd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1a0de 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 dbeDeleteAuxData
1a0df 28 70 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a (pVdbeFunc, 0);.
1a0e0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
1a0e1 62 46 72 65 65 28 64 62 2c 20 70 56 64 62 65 46 bFree(db, pVdbeF
1a0e2 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 62 72 unc);. br
1a0e3 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
1a0e4 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e 43 44 case P4_FUNCD
1a0e5 45 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 66 72 EF: {. fr
1a0e6 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 eeEphemeralFunct
1a0e7 69 6f 6e 28 64 62 2c 20 28 46 75 6e 63 44 65 66 ion(db, (FuncDef
1a0e8 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 *)p4);. b
1a0e9 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
1a0ea 20 20 20 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a case P4_MEM:
1a0eb 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 {. sqlit
1a0ec 65 33 56 61 6c 75 65 46 72 65 65 28 28 73 71 6c e3ValueFree((sql
1a0ed 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 34 29 3b ite3_value*)p4);
1a0ee 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
1a0ef 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 }. ca
1a0f0 73 65 20 50 34 5f 56 54 41 42 20 3a 20 7b 0a 20 se P4_VTAB : {.
1a0f1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 sqlite3Vt
1a0f2 61 62 55 6e 6c 6f 63 6b 28 28 56 54 61 62 6c 65 abUnlock((VTable
1a0f3 20 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 *)p4);.
1a0f4 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
1a0f5 20 20 20 20 20 63 61 73 65 20 50 34 5f 53 55 42 case P4_SUB
1a0f6 50 52 4f 47 52 41 4d 20 3a 20 7b 0a 20 20 20 20 PROGRAM : {.
1a0f7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 sqlite3VdbeP
1a0f8 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 64 62 2c rogramDelete(db,
1a0f9 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a 29 70 (SubProgram *)p
1a0fa 34 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 62 4, 1);. b
1a0fb 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
1a0fc 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
1a0fd 20 46 72 65 65 20 74 68 65 20 73 70 61 63 65 20 Free the space
1a0fe 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 4f allocated for aO
1a0ff 70 20 61 6e 64 20 61 6e 79 20 70 34 20 76 61 6c p and any p4 val
1a100 75 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f ues allocated fo
1a101 72 20 74 68 65 0a 2a 2a 20 6f 70 63 6f 64 65 73 r the.** opcodes
1a102 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 contained withi
1a103 6e 2e 20 49 66 20 61 4f 70 20 69 73 20 6e 6f 74 n. If aOp is not
1a104 20 4e 55 4c 4c 20 69 74 20 69 73 20 61 73 73 75 NULL it is assu
1a105 6d 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 0a med to contain .
1a106 2a 2a 20 6e 4f 70 20 65 6e 74 72 69 65 73 2e 20 ** nOp entries.
1a107 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1a108 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 vdbeFreeOpArray(
1a109 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 4f 70 20 sqlite3 *db, Op
1a10a 2a 61 4f 70 2c 20 69 6e 74 20 6e 4f 70 29 7b 0a *aOp, int nOp){.
1a10b 20 20 69 66 28 20 61 4f 70 20 29 7b 0a 20 20 20 if( aOp ){.
1a10c 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 66 6f Op *pOp;. fo
1a10d 72 28 70 4f 70 3d 61 4f 70 3b 20 70 4f 70 3c 26 r(pOp=aOp; pOp<&
1a10e 61 4f 70 5b 6e 4f 70 5d 3b 20 70 4f 70 2b 2b 29 aOp[nOp]; pOp++)
1a10f 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 28 64 {. freeP4(d
1a110 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 b, pOp->p4type,
1a111 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 23 69 66 64 pOp->p4.p);.#ifd
1a112 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1a113 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1a114 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e 7a 43 6f ree(db, pOp->zCo
1a115 6d 6d 65 6e 74 29 3b 0a 23 65 6e 64 69 66 20 20 mment);.#endif
1a116 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 . }. }.
1a117 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1a118 2c 20 61 4f 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a , aOp);.}../*.**
1a119 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 Decrement the r
1a11a 65 66 2d 63 6f 75 6e 74 20 6f 6e 20 74 68 65 20 ef-count on the
1a11b 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 SubProgram struc
1a11c 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20 74 ture passed as t
1a11d 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 61 72 67 he.** second arg
1a11e 75 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 72 65 ument. If the re
1a11f 66 2d 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 f-count reaches
1a120 7a 65 72 6f 2c 20 66 72 65 65 20 74 68 65 20 73 zero, free the s
1a121 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
1a122 54 68 65 20 61 72 72 61 79 20 6f 66 20 56 44 42 The array of VDB
1a123 45 20 6f 70 63 6f 64 65 73 20 73 74 6f 72 65 64 E opcodes stored
1a124 20 61 73 20 53 75 62 50 72 6f 67 72 61 6d 2e 61 as SubProgram.a
1a125 4f 70 20 69 73 20 66 72 65 65 64 20 69 66 0a 2a Op is freed if.*
1a126 2a 20 65 69 74 68 65 72 20 74 68 65 20 72 65 66 * either the ref
1a127 2d 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a -count reaches z
1a128 65 72 6f 20 6f 72 20 70 61 72 61 6d 65 74 65 72 ero or parameter
1a129 20 66 72 65 65 6f 70 20 69 73 20 6e 6f 6e 2d 7a freeop is non-z
1a12a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 ero..**.** Since
1a12b 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f 70 the array of op
1a12c 63 6f 64 65 73 20 70 6f 69 6e 74 65 64 20 74 6f codes pointed to
1a12d 20 62 79 20 53 75 62 50 72 6f 67 72 61 6d 2e 61 by SubProgram.a
1a12e 4f 70 20 6d 61 79 20 64 69 72 65 63 74 6c 79 0a Op may directly.
1a12f 2a 2a 20 6f 72 20 69 6e 64 69 72 65 63 74 6c 79 ** or indirectly
1a130 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 66 65 72 contain a refer
1a131 65 6e 63 65 20 74 6f 20 74 68 65 20 53 75 62 50 ence to the SubP
1a132 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75 72 65 rogram structure
1a133 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 42 79 20 70 itself..** By p
1a134 61 73 73 69 6e 67 20 61 20 6e 6f 6e 2d 7a 65 72 assing a non-zer
1a135 6f 20 66 72 65 65 6f 70 20 70 61 72 61 6d 65 74 o freeop paramet
1a136 65 72 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 6d er, the caller m
1a137 61 79 20 65 6e 73 75 72 65 20 74 68 61 74 20 61 ay ensure that a
1a138 6c 6c 0a 2a 2a 20 53 75 62 50 72 6f 67 72 61 6d ll.** SubProgram
1a139 20 73 74 72 75 63 74 75 72 65 73 20 61 6e 64 20 structures and
1a13a 74 68 65 69 72 20 61 4f 70 20 61 72 72 61 79 73 their aOp arrays
1a13b 20 61 72 65 20 66 72 65 65 64 2c 20 65 76 65 6e are freed, even
1a13c 20 77 68 65 6e 20 74 68 65 72 65 0a 2a 2a 20 61 when there.** a
1a13d 72 65 20 73 75 63 68 20 63 69 72 63 75 6c 61 72 re such circular
1a13e 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2f 0a references..*/.
1a13f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1a140 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 oid sqlite3VdbeP
1a141 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 73 71 6c rogramDelete(sql
1a142 69 74 65 33 20 2a 64 62 2c 20 53 75 62 50 72 6f ite3 *db, SubPro
1a143 67 72 61 6d 20 2a 70 2c 20 69 6e 74 20 66 72 65 gram *p, int fre
1a144 65 6f 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b eop){. if( p ){
1a145 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e . assert( p->
1a146 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 69 66 nRef>0 );. if
1a147 28 20 66 72 65 65 6f 70 20 7c 7c 20 70 2d 3e 6e ( freeop || p->n
1a148 52 65 66 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 Ref==1 ){.
1a149 4f 70 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 Op *aOp = p->aOp
1a14a 3b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 20 3d ;. p->aOp =
1a14b 20 30 3b 0a 20 20 20 20 20 20 76 64 62 65 46 72 0;. vdbeFr
1a14c 65 65 4f 70 41 72 72 61 79 28 64 62 2c 20 61 4f eeOpArray(db, aO
1a14d 70 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 20 20 20 20 p, p->nOp);.
1a14e 20 20 70 2d 3e 6e 4f 70 20 3d 20 30 3b 0a 20 20 p->nOp = 0;.
1a14f 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2d }. p->nRef-
1a150 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 -;. if( p->nR
1a151 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 ef==0 ){. s
1a152 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1a153 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d p);. }. }.}
1a154 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 .../*.** Change
1a155 4e 20 6f 70 63 6f 64 65 73 20 73 74 61 72 74 69 N opcodes starti
1a156 6e 67 20 61 74 20 61 64 64 72 20 74 6f 20 4e 6f ng at addr to No
1a157 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f -ops..*/.SQLITE_
1a158 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1a159 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f ite3VdbeChangeTo
1a15a 4e 6f 6f 70 28 56 64 62 65 20 2a 70 2c 20 69 6e Noop(Vdbe *p, in
1a15b 74 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 7b 0a t addr, int N){.
1a15c 20 20 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b 0a if( p->aOp ){.
1a15d 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 20 VdbeOp *pOp
1a15e 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b = &p->aOp[addr];
1a15f 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 . sqlite3 *db
1a160 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 77 68 = p->db;. wh
1a161 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 ile( N-- ){.
1a162 20 20 66 72 65 65 50 34 28 64 62 2c 20 70 4f 70 freeP4(db, pOp
1a163 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 ->p4type, pOp->p
1a164 34 2e 70 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 4.p);. mems
1a165 65 74 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65 6f et(pOp, 0, sizeo
1a166 66 28 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20 20 f(pOp[0]));.
1a167 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 pOp->opcode =
1a168 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 20 70 OP_Noop;. p
1a169 4f 70 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Op++;. }. }.
1a16a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 }../*.** Change
1a16b 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
1a16c 20 50 34 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 P4 operand for
1a16d 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 a specific instr
1a16e 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 uction..** This
1a16f 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66 75 routine is usefu
1a170 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 20 70 l when a large p
1a171 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 64 rogram is loaded
1a172 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 69 from a.** stati
1a173 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 73 71 c array using sq
1a174 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 lite3VdbeAddOpLi
1a175 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 20 74 st but we want t
1a176 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 20 o make a.** few
1a177 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f minor changes to
1a178 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a the program..**
1a179 0a 2a 2a 20 49 66 20 6e 3e 3d 30 20 74 68 65 6e .** If n>=0 then
1a17a 20 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 the P4 operand
1a17b 69 73 20 64 79 6e 61 6d 69 63 2c 20 6d 65 61 6e is dynamic, mean
1a17c 69 6e 67 20 74 68 61 74 20 61 20 63 6f 70 79 20 ing that a copy
1a17d 6f 66 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 of.** the string
1a17e 20 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 6d 65 is made into me
1a17f 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
1a180 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f om sqlite3_mallo
1a181 63 28 29 2e 0a 2a 2a 20 41 20 76 61 6c 75 65 20 c()..** A value
1a182 6f 66 20 6e 3d 3d 30 20 6d 65 61 6e 73 20 63 6f of n==0 means co
1a183 70 79 20 62 79 74 65 73 20 6f 66 20 7a 50 34 20 py bytes of zP4
1a184 75 70 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64 up to and includ
1a185 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 ing the.** first
1a186 20 6e 75 6c 6c 20 62 79 74 65 2e 20 20 49 66 20 null byte. If
1a187 6e 3e 30 20 74 68 65 6e 20 63 6f 70 79 20 6e 2b n>0 then copy n+
1a188 31 20 62 79 74 65 73 20 6f 66 20 7a 50 34 2e 0a 1 bytes of zP4..
1a189 2a 2a 0a 2a 2a 20 49 66 20 6e 3d 3d 50 34 5f 4b **.** If n==P4_K
1a18a 45 59 49 4e 46 4f 20 69 74 20 6d 65 61 6e 73 20 EYINFO it means
1a18b 74 68 61 74 20 7a 50 34 20 69 73 20 61 20 70 6f that zP4 is a po
1a18c 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e inter to a KeyIn
1a18d 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a fo structure..**
1a18e 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20 A copy is made
1a18f 6f 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 of the KeyInfo s
1a190 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6d 65 tructure into me
1a191 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
1a192 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 om.** sqlite3_ma
1a193 6c 6c 6f 63 2c 20 74 6f 20 62 65 20 66 72 65 65 lloc, to be free
1a194 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 d when the Vdbe
1a195 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a is finalized..**
1a196 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 n==P4_KEYINFO_H
1a197 41 4e 44 4f 46 46 20 69 6e 64 69 63 61 74 65 73 ANDOFF indicates
1a198 20 74 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73 that zP4 points
1a199 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 to a KeyInfo st
1a19a 72 75 63 74 75 72 65 0a 2a 2a 20 73 74 6f 72 65 ructure.** store
1a19b 64 20 69 6e 20 6d 65 6d 6f 72 79 20 74 68 61 74 d in memory that
1a19c 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 the caller has
1a19d 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
1a19e 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2e 20 54 68 lite3_malloc. Th
1a19f 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f e .** caller sho
1a1a0 75 6c 64 20 6e 6f 74 20 66 72 65 65 20 74 68 65 uld not free the
1a1a1 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20 allocation, it
1a1a2 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 77 68 will be freed wh
1a1a3 65 6e 20 74 68 65 20 56 64 62 65 20 69 73 0a 2a en the Vdbe is.*
1a1a4 2a 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 * finalized..**
1a1a5 0a 2a 2a 20 4f 74 68 65 72 20 76 61 6c 75 65 73 .** Other values
1a1a6 20 6f 66 20 6e 20 28 50 34 5f 53 54 41 54 49 43 of n (P4_STATIC
1a1a7 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 20 65 74 63 , P4_COLLSEQ etc
1a1a8 2e 29 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 .) indicate that
1a1a9 20 7a 50 34 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 zP4 points.** t
1a1aa 6f 20 61 20 73 74 72 69 6e 67 20 6f 72 20 73 74 o a string or st
1a1ab 72 75 63 74 75 72 65 20 74 68 61 74 20 69 73 20 ructure that is
1a1ac 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 guaranteed to ex
1a1ad 69 73 74 20 66 6f 72 20 74 68 65 20 6c 69 66 65 ist for the life
1a1ae 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 56 time of.** the V
1a1af 64 62 65 2e 20 49 6e 20 74 68 65 73 65 20 63 61 dbe. In these ca
1a1b0 73 65 73 20 77 65 20 63 61 6e 20 6a 75 73 74 20 ses we can just
1a1b1 63 6f 70 79 20 74 68 65 20 70 6f 69 6e 74 65 72 copy the pointer
1a1b2 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 64 64 72 3c ..**.** If addr<
1a1b3 30 20 74 68 65 6e 20 63 68 61 6e 67 65 20 50 34 0 then change P4
1a1b4 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 on the most rec
1a1b5 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 69 ently inserted i
1a1b6 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 nstruction..*/.S
1a1b7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1a1b8 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 id sqlite3VdbeCh
1a1b9 61 6e 67 65 50 34 28 56 64 62 65 20 2a 70 2c 20 angeP4(Vdbe *p,
1a1ba 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 int addr, const
1a1bb 63 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 6e char *zP4, int n
1a1bc 29 7b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 ){. Op *pOp;.
1a1bd 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 61 sqlite3 *db;. a
1a1be 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
1a1bf 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 db = p->db;. a
1a1c0 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d ssert( p->magic=
1a1c1 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 =VDBE_MAGIC_INIT
1a1c2 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 );. if( p->aOp
1a1c3 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f ==0 || db->mallo
1a1c4 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 69 cFailed ){. i
1a1c5 66 20 28 20 6e 21 3d 50 34 5f 4b 45 59 49 4e 46 f ( n!=P4_KEYINF
1a1c6 4f 20 26 26 20 6e 21 3d 50 34 5f 56 54 41 42 20 O && n!=P4_VTAB
1a1c7 29 20 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 ) {. freeP4
1a1c8 28 64 62 2c 20 6e 2c 20 28 76 6f 69 64 2a 29 2a (db, n, (void*)*
1a1c9 28 63 68 61 72 2a 2a 29 26 7a 50 34 29 3b 0a 20 (char**)&zP4);.
1a1ca 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 3b }. return;
1a1cb 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
1a1cc 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20 61 73 73 ->nOp>0 );. ass
1a1cd 65 72 74 28 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 ert( addr<p->nOp
1a1ce 20 29 3b 0a 20 20 69 66 28 20 61 64 64 72 3c 30 );. if( addr<0
1a1cf 20 29 7b 0a 20 20 20 20 61 64 64 72 20 3d 20 70 ){. addr = p
1a1d0 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a 20 ->nOp - 1;. }.
1a1d1 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 pOp = &p->aOp[a
1a1d2 64 64 72 5d 3b 0a 20 20 66 72 65 65 50 34 28 64 ddr];. freeP4(d
1a1d3 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 b, pOp->p4type,
1a1d4 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 70 4f pOp->p4.p);. pO
1a1d5 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 69 p->p4.p = 0;. i
1a1d6 66 28 20 6e 3d 3d 50 34 5f 49 4e 54 33 32 20 29 f( n==P4_INT32 )
1a1d7 7b 0a 20 20 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 {. /* Note: t
1a1d8 68 69 73 20 63 61 73 74 20 69 73 20 73 61 66 65 his cast is safe
1a1d9 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 6f 72 , because the or
1a1da 69 67 69 6e 20 64 61 74 61 20 70 6f 69 6e 74 20 igin data point
1a1db 77 61 73 20 61 6e 20 69 6e 74 0a 20 20 20 20 2a was an int. *
1a1dc 2a 20 74 68 61 74 20 77 61 73 20 63 61 73 74 20 * that was cast
1a1dd 74 6f 20 61 20 28 63 6f 6e 73 74 20 63 68 61 72 to a (const char
1a1de 20 2a 29 2e 20 2a 2f 0a 20 20 20 20 70 4f 70 2d *). */. pOp-
1a1df 3e 70 34 2e 69 20 3d 20 53 51 4c 49 54 45 5f 50 >p4.i = SQLITE_P
1a1e0 54 52 5f 54 4f 5f 49 4e 54 28 7a 50 34 29 3b 0a TR_TO_INT(zP4);.
1a1e1 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 pOp->p4type
1a1e2 3d 20 50 34 5f 49 4e 54 33 32 3b 0a 20 20 7d 65 = P4_INT32;. }e
1a1e3 6c 73 65 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 lse if( zP4==0 )
1a1e4 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 {. pOp->p4.p
1a1e5 3d 20 30 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 = 0;. pOp->p4
1a1e6 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 type = P4_NOTUSE
1a1e7 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e D;. }else if( n
1a1e8 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a ==P4_KEYINFO ){.
1a1e9 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 KeyInfo *pKe
1a1ea 79 49 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20 6e yInfo;. int n
1a1eb 46 69 65 6c 64 2c 20 6e 42 79 74 65 3b 0a 0a 20 Field, nByte;..
1a1ec 20 20 20 6e 46 69 65 6c 64 20 3d 20 28 28 4b 65 nField = ((Ke
1a1ed 79 49 6e 66 6f 2a 29 7a 50 34 29 2d 3e 6e 46 69 yInfo*)zP4)->nFi
1a1ee 65 6c 64 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d eld;. nByte =
1a1ef 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66 sizeof(*pKeyInf
1a1f0 6f 29 20 2b 20 28 6e 46 69 65 6c 64 2d 31 29 2a o) + (nField-1)*
1a1f1 73 69 7a 65 6f 66 28 70 4b 65 79 49 6e 66 6f 2d sizeof(pKeyInfo-
1a1f2 3e 61 43 6f 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69 >aColl[0]) + nFi
1a1f3 65 6c 64 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 eld;. pKeyInf
1a1f4 6f 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f o = sqlite3Mallo
1a1f5 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 20 20 c( nByte );.
1a1f6 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f pOp->p4.pKeyInfo
1a1f7 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 = pKeyInfo;.
1a1f8 20 69 66 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b if( pKeyInfo ){
1a1f9 0a 20 20 20 20 20 20 75 38 20 2a 61 53 6f 72 74 . u8 *aSort
1a1fa 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 6d 65 6d Order;. mem
1a1fb 63 70 79 28 70 4b 65 79 49 6e 66 6f 2c 20 7a 50 cpy(pKeyInfo, zP
1a1fc 34 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 4, nByte);.
1a1fd 20 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 4b aSortOrder = pK
1a1fe 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 eyInfo->aSortOrd
1a1ff 65 72 3b 0a 20 20 20 20 20 20 69 66 28 20 61 53 er;. if( aS
1a200 6f 72 74 4f 72 64 65 72 20 29 7b 0a 20 20 20 20 ortOrder ){.
1a201 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 pKeyInfo->aS
1a202 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 6e 73 69 ortOrder = (unsi
1a203 67 6e 65 64 20 63 68 61 72 2a 29 26 70 4b 65 79 gned char*)&pKey
1a204 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 65 Info->aColl[nFie
1a205 6c 64 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d ld];. mem
1a206 63 70 79 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 cpy(pKeyInfo->aS
1a207 6f 72 74 4f 72 64 65 72 2c 20 61 53 6f 72 74 4f ortOrder, aSortO
1a208 72 64 65 72 2c 20 6e 46 69 65 6c 64 29 3b 0a 20 rder, nField);.
1a209 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 }. pOp
1a20a 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 ->p4type = P4_KE
1a20b 59 49 4e 46 4f 3b 0a 20 20 20 20 7d 65 6c 73 65 YINFO;. }else
1a20c 7b 0a 20 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d {. p->db->m
1a20d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
1a20e 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 . pOp->p4ty
1a20f 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b pe = P4_NOTUSED;
1a210 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 . }. }else i
1a211 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f f( n==P4_KEYINFO
1a212 5f 48 41 4e 44 4f 46 46 20 29 7b 0a 20 20 20 20 _HANDOFF ){.
1a213 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 pOp->p4.p = (voi
1a214 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d d*)zP4;. pOp-
1a215 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 >p4type = P4_KEY
1a216 49 4e 46 4f 3b 0a 20 20 7d 65 6c 73 65 20 69 66 INFO;. }else if
1a217 28 20 6e 3d 3d 50 34 5f 56 54 41 42 20 29 7b 0a ( n==P4_VTAB ){.
1a218 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 pOp->p4.p =
1a219 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 (void*)zP4;.
1a21a 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 pOp->p4type = P4
1a21b 5f 56 54 41 42 3b 0a 20 20 20 20 73 71 6c 69 74 _VTAB;. sqlit
1a21c 65 33 56 74 61 62 4c 6f 63 6b 28 28 56 54 61 62 e3VtabLock((VTab
1a21d 6c 65 20 2a 29 7a 50 34 29 3b 0a 20 20 20 20 61 le *)zP4);. a
1a21e 73 73 65 72 74 28 20 28 28 56 54 61 62 6c 65 20 ssert( ((VTable
1a21f 2a 29 7a 50 34 29 2d 3e 64 62 3d 3d 70 2d 3e 64 *)zP4)->db==p->d
1a220 62 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 b );. }else if(
1a221 20 6e 3c 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d n<0 ){. pOp-
1a222 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a >p4.p = (void*)z
1a223 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 P4;. pOp->p4t
1a224 79 70 65 20 3d 20 28 73 69 67 6e 65 64 20 63 68 ype = (signed ch
1a225 61 72 29 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ar)n;. }else{.
1a226 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 20 6e 20 if( n==0 ) n
1a227 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
1a228 30 28 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 0(zP4);. pOp-
1a229 3e 70 34 2e 7a 20 3d 20 73 71 6c 69 74 65 33 44 >p4.z = sqlite3D
1a22a 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20 bStrNDup(p->db,
1a22b 7a 50 34 2c 20 6e 29 3b 0a 20 20 20 20 70 4f 70 zP4, n);. pOp
1a22c 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 44 59 ->p4type = P4_DY
1a22d 4e 41 4d 49 43 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 NAMIC;. }.}..#i
1a22e 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a fndef NDEBUG./*.
1a22f 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 63 6f ** Change the co
1a230 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20 74 68 65 mment on the the
1a231 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 most recently c
1a232 6f 64 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e oded instruction
1a233 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73 65 72 74 20 . Or.** insert
1a234 61 20 4e 6f 2d 6f 70 20 61 6e 64 20 61 64 64 20 a No-op and add
1a235 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 6f 20 74 the comment to t
1a236 68 61 74 20 6e 65 77 20 69 6e 73 74 72 75 63 74 hat new instruct
1a237 69 6f 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 61 ion. This.** ma
1a238 6b 65 73 20 74 68 65 20 63 6f 64 65 20 65 61 73 kes the code eas
1a239 69 65 72 20 74 6f 20 72 65 61 64 20 64 75 72 69 ier to read duri
1a23a 6e 67 20 64 65 62 75 67 67 69 6e 67 2e 20 20 4e ng debugging. N
1a23b 6f 6e 65 20 6f 66 20 74 68 69 73 20 68 61 70 70 one of this happ
1a23c 65 6e 73 0a 2a 2a 20 69 6e 20 61 20 70 72 6f 64 ens.** in a prod
1a23d 75 63 74 69 6f 6e 20 62 75 69 6c 64 2e 0a 2a 2f uction build..*/
1a23e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a23f 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1a240 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c Comment(Vdbe *p,
1a241 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
1a242 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 rmat, ...){. va
1a243 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20 _list ap;. if(
1a244 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 !p ) return;. a
1a245 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 ssert( p->nOp>0
1a246 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a || p->aOp==0 );.
1a247 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70 assert( p->aOp
1a248 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d ==0 || p->aOp[p-
1a249 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 >nOp-1].zComment
1a24a 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 ==0 || p->db->ma
1a24b 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
1a24c 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 if( p->nOp ){.
1a24d 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70 char **pz = &p
1a24e 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e ->aOp[p->nOp-1].
1a24f 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61 zComment;. va
1a250 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d _start(ap, zForm
1a251 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 at);. sqlite3
1a252 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70 DbFree(p->db, *p
1a253 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71 z);. *pz = sq
1a254 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70 2d lite3VMPrintf(p-
1a255 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 >db, zFormat, ap
1a256 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 );. va_end(ap
1a257 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f );. }.}.SQLITE_
1a258 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1a259 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d ite3VdbeNoopComm
1a25a 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e ent(Vdbe *p, con
1a25b 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 st char *zFormat
1a25c 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 , ...){. va_lis
1a25d 74 20 61 70 3b 0a 20 20 69 66 28 20 21 70 20 29 t ap;. if( !p )
1a25e 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 return;. sqlit
1a25f 65 33 56 64 62 65 41 64 64 4f 70 30 28 70 2c 20 e3VdbeAddOp0(p,
1a260 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20 61 73 73 65 OP_Noop);. asse
1a261 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 rt( p->nOp>0 ||
1a262 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 p->aOp==0 );. a
1a263 73 73 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 ssert( p->aOp==0
1a264 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f || p->aOp[p->nO
1a265 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 p-1].zComment==0
1a266 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f || p->db->mallo
1a267 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 cFailed );. if(
1a268 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63 p->nOp ){. c
1a269 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61 har **pz = &p->a
1a26a 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f Op[p->nOp-1].zCo
1a26b 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74 mment;. va_st
1a26c 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 art(ap, zFormat)
1a26d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
1a26e 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b ree(p->db, *pz);
1a26f 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74 . *pz = sqlit
1a270 65 33 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62 e3VMPrintf(p->db
1a271 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
1a272 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a va_end(ap);.
1a273 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a }.}.#endif /*
1a274 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a NDEBUG */../*.*
1a275 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6f 70 63 * Return the opc
1a276 6f 64 65 20 66 6f 72 20 61 20 67 69 76 65 6e 20 ode for a given
1a277 61 64 64 72 65 73 73 2e 20 20 49 66 20 74 68 65 address. If the
1a278 20 61 64 64 72 65 73 73 20 69 73 20 2d 31 2c 20 address is -1,
1a279 74 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 74 then.** return t
1a27a 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 he most recently
1a27b 20 69 6e 73 65 72 74 65 64 20 6f 70 63 6f 64 65 inserted opcode
1a27c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d ..**.** If a mem
1a27d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
1a27e 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 rror has occurre
1a27f 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63 d prior to the c
1a280 61 6c 6c 69 6e 67 20 6f 66 20 74 68 69 73 0a 2a alling of this.*
1a281 2a 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 6e 20 * routine, then
1a282 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 64 a pointer to a d
1a283 75 6d 6d 79 20 56 64 62 65 4f 70 20 77 69 6c 6c ummy VdbeOp will
1a284 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 20 54 be returned. T
1a285 68 61 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 hat opcode.** is
1a286 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 readable and wr
1a287 69 74 61 62 6c 65 2c 20 62 75 74 20 69 74 20 68 itable, but it h
1a288 61 73 20 6e 6f 20 65 66 66 65 63 74 2e 20 20 54 as no effect. T
1a289 68 65 20 72 65 74 75 72 6e 20 6f 66 20 61 20 64 he return of a d
1a28a 75 6d 6d 79 0a 2a 2a 20 6f 70 63 6f 64 65 20 61 ummy.** opcode a
1a28b 6c 6c 6f 77 73 20 74 68 65 20 63 61 6c 6c 20 74 llows the call t
1a28c 6f 20 63 6f 6e 74 69 6e 75 65 20 66 75 6e 63 74 o continue funct
1a28d 69 6f 6e 69 6e 67 20 61 66 74 65 72 20 61 20 4f ioning after a O
1a28e 4f 4d 20 66 61 75 6c 74 20 77 69 74 68 6f 75 74 OM fault without
1a28f 0a 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 63 68 .** having to ch
1a290 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 eck to see if th
1a291 65 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68 e return from th
1a292 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
1a293 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 2e 0a 2a valid pointer..*
1a294 2a 0a 2a 2a 20 41 62 6f 75 74 20 74 68 65 20 23 *.** About the #
1a295 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
1a296 54 5f 54 52 41 43 45 3a 20 20 4e 6f 72 6d 61 6c T_TRACE: Normal
1a297 6c 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ly, this routine
1a298 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 is never called
1a299 0a 2a 2a 20 75 6e 6c 65 73 73 20 70 2d 3e 6e 4f .** unless p->nO
1a29a 70 3e 30 2e 20 20 54 68 69 73 20 69 73 20 62 65 p>0. This is be
1a29b 63 61 75 73 65 20 69 6e 20 74 68 65 20 61 62 73 cause in the abs
1a29c 65 6e 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f ense of SQLITE_O
1a29d 4d 49 54 5f 54 52 41 43 45 2c 0a 2a 2a 20 61 6e MIT_TRACE,.** an
1a29e 20 4f 50 5f 54 72 61 63 65 20 69 6e 73 74 72 75 OP_Trace instru
1a29f 63 74 69 6f 6e 20 69 73 20 61 6c 77 61 79 73 20 ction is always
1a2a0 69 6e 73 65 72 74 65 64 20 62 79 20 73 71 6c 69 inserted by sqli
1a2a1 74 65 33 56 64 62 65 47 65 74 28 29 20 61 73 20 te3VdbeGet() as
1a2a2 73 6f 6f 6e 20 61 73 0a 2a 2a 20 61 20 6e 65 77 soon as.** a new
1a2a3 20 56 44 42 45 20 69 73 20 63 72 65 61 74 65 64 VDBE is created
1a2a4 2e 20 20 53 6f 20 77 65 20 61 72 65 20 66 72 65 . So we are fre
1a2a5 65 20 74 6f 20 73 65 74 20 61 64 64 72 20 74 6f e to set addr to
1a2a6 20 70 2d 3e 6e 4f 70 2d 31 20 77 69 74 68 6f 75 p->nOp-1 withou
1a2a7 74 0a 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 64 t.** having to d
1a2a8 6f 75 62 6c 65 2d 63 68 65 63 6b 20 74 6f 20 6d ouble-check to m
1a2a9 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 ake sure that th
1a2aa 65 20 72 65 73 75 6c 74 20 69 73 20 6e 6f 6e 2d e result is non-
1a2ab 6e 65 67 61 74 69 76 65 2e 20 42 75 74 0a 2a 2a negative. But.**
1a2ac 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f if SQLITE_OMIT_
1a2ad 54 52 41 43 45 20 69 73 20 64 65 66 69 6e 65 64 TRACE is defined
1a2ae 2c 20 74 68 65 20 4f 50 5f 54 72 61 63 65 20 69 , the OP_Trace i
1a2af 73 20 6f 6d 69 74 74 65 64 20 61 6e 64 20 77 65 s omitted and we
1a2b0 20 64 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 63 do need to.** c
1a2b1 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20 6f heck the value o
1a2b2 66 20 70 2d 3e 6e 4f 70 2d 31 20 62 65 66 6f 72 f p->nOp-1 befor
1a2b3 65 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2f e continuing..*/
1a2b4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a2b5 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 VdbeOp *sqlite3V
1a2b6 64 62 65 47 65 74 4f 70 28 56 64 62 65 20 2a 70 dbeGetOp(Vdbe *p
1a2b7 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 73 , int addr){. s
1a2b8 74 61 74 69 63 20 56 64 62 65 4f 70 20 64 75 6d tatic VdbeOp dum
1a2b9 6d 79 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d my;. assert( p-
1a2ba 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 >magic==VDBE_MAG
1a2bb 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 IC_INIT );. if(
1a2bc 20 61 64 64 72 3c 30 20 29 7b 0a 23 69 66 64 65 addr<0 ){.#ifde
1a2bd 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 f SQLITE_OMIT_TR
1a2be 41 43 45 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e ACE. if( p->n
1a2bf 4f 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 26 Op==0 ) return &
1a2c0 64 75 6d 6d 79 3b 0a 23 65 6e 64 69 66 0a 20 20 dummy;.#endif.
1a2c1 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 20 addr = p->nOp
1a2c2 2d 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 - 1;. }. asser
1a2c3 74 28 20 28 61 64 64 72 3e 3d 30 20 26 26 20 61 t( (addr>=0 && a
1a2c4 64 64 72 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 70 ddr<p->nOp) || p
1a2c5 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
1a2c6 65 64 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 ed );. if( p->d
1a2c7 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1a2c8 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64 ){. return &d
1a2c9 75 6d 6d 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ummy;. }else{.
1a2ca 20 20 20 72 65 74 75 72 6e 20 26 70 2d 3e 61 4f return &p->aO
1a2cb 70 5b 61 64 64 72 5d 3b 0a 20 20 7d 0a 7d 0a 0a p[addr];. }.}..
1a2cc 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
1a2cd 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e ITE_OMIT_EXPLAIN
1a2ce 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 ) || !defined(ND
1a2cf 45 42 55 47 29 20 5c 0a 20 20 20 20 20 7c 7c 20 EBUG) \. ||
1a2d0 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f defined(VDBE_PRO
1a2d1 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 FILE) || defined
1a2d2 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f (SQLITE_DEBUG)./
1a2d3 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 *.** Compute a s
1a2d4 74 72 69 6e 67 20 74 68 61 74 20 64 65 73 63 72 tring that descr
1a2d5 69 62 65 73 20 74 68 65 20 50 34 20 70 61 72 61 ibes the P4 para
1a2d6 6d 65 74 65 72 20 66 6f 72 20 61 6e 20 6f 70 63 meter for an opc
1a2d7 6f 64 65 2e 0a 2a 2a 20 55 73 65 20 7a 54 65 6d ode..** Use zTem
1a2d8 70 20 66 6f 72 20 61 6e 79 20 72 65 71 75 69 72 p for any requir
1a2d9 65 64 20 74 65 6d 70 6f 72 61 72 79 20 62 75 66 ed temporary buf
1a2da 66 65 72 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 fer space..*/.st
1a2db 61 74 69 63 20 63 68 61 72 20 2a 64 69 73 70 6c atic char *displ
1a2dc 61 79 50 34 28 4f 70 20 2a 70 4f 70 2c 20 63 68 ayP4(Op *pOp, ch
1a2dd 61 72 20 2a 7a 54 65 6d 70 2c 20 69 6e 74 20 6e ar *zTemp, int n
1a2de 54 65 6d 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a Temp){. char *z
1a2df 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 61 73 P4 = zTemp;. as
1a2e0 73 65 72 74 28 20 6e 54 65 6d 70 3e 3d 32 30 20 sert( nTemp>=20
1a2e1 29 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 );. switch( pOp
1a2e2 2d 3e 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20 ->p4type ){.
1a2e3 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f case P4_KEYINFO_
1a2e4 53 54 41 54 49 43 3a 0a 20 20 20 20 63 61 73 65 STATIC:. case
1a2e5 20 50 34 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20 P4_KEYINFO: {.
1a2e6 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 int i, j;.
1a2e7 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b KeyInfo *pK
1a2e8 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 eyInfo = pOp->p4
1a2e9 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 .pKeyInfo;.
1a2ea 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1a2eb 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 f(nTemp, zTemp,
1a2ec 22 6b 65 79 69 6e 66 6f 28 25 64 22 2c 20 70 4b "keyinfo(%d", pK
1a2ed 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29 3b eyInfo->nField);
1a2ee 0a 20 20 20 20 20 20 69 20 3d 20 73 71 6c 69 74 . i = sqlit
1a2ef 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 e3Strlen30(zTemp
1a2f0 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 );. for(j=0
1a2f1 3b 20 6a 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 ; j<pKeyInfo->nF
1a2f2 69 65 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 ield; j++){.
1a2f3 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f CollSeq *pCo
1a2f4 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 ll = pKeyInfo->a
1a2f5 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 Coll[j];.
1a2f6 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 if( pColl ){.
1a2f7 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 int n =
1a2f8 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1a2f9 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pColl->zName);.
1a2fa 20 20 20 20 20 20 20 20 20 69 66 28 20 69 2b 6e if( i+n
1a2fb 3e 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 >nTemp-6 ){.
1a2fc 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
1a2fd 7a 54 65 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 2c zTemp[i],",...",
1a2fe 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 4);.
1a2ff 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 break;.
1a300 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 }. zTe
1a301 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a 20 mp[i++] = ',';.
1a302 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4b 65 if( pKe
1a303 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 yInfo->aSortOrde
1a304 72 20 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 r && pKeyInfo->a
1a305 53 6f 72 74 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a SortOrder[j] ){.
1a306 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d zTem
1a307 70 5b 69 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20 p[i++] = '-';.
1a308 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1a309 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d memcpy(&zTem
1a30a 70 5b 69 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 p[i], pColl->zNa
1a30b 6d 65 2c 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 me,n+1);.
1a30c 20 20 20 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 i += n;.
1a30d 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 2b 34 }else if( i+4
1a30e 3c 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 <nTemp-6 ){.
1a30f 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 memcpy(&zT
1a310 65 6d 70 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 emp[i],",nil",4)
1a311 3b 0a 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d ;. i +=
1a312 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 4;. }.
1a313 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 54 65 6d }. zTem
1a314 70 5b 69 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 20 p[i++] = ')';.
1a315 20 20 20 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 30 zTemp[i] = 0
1a316 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1a317 69 3c 6e 54 65 6d 70 20 29 3b 0a 20 20 20 20 20 i<nTemp );.
1a318 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1a319 20 20 63 61 73 65 20 50 34 5f 43 4f 4c 4c 53 45 case P4_COLLSE
1a31a 51 3a 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 Q: {. CollS
1a31b 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 2d eq *pColl = pOp-
1a31c 3e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 >p4.pColl;.
1a31d 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1a31e 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 f(nTemp, zTemp,
1a31f 22 63 6f 6c 6c 73 65 71 28 25 2e 32 30 73 29 22 "collseq(%.20s)"
1a320 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b , pColl->zName);
1a321 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1a322 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f }. case P4_
1a323 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 FUNCDEF: {.
1a324 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20 3d FuncDef *pDef =
1a325 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a pOp->p4.pFunc;.
1a326 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
1a327 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 printf(nTemp, zT
1a328 65 6d 70 2c 20 22 25 73 28 25 64 29 22 2c 20 70 emp, "%s(%d)", p
1a329 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 Def->zName, pDef
1a32a 2d 3e 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 62 ->nArg);. b
1a32b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1a32c 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b case P4_INT64: {
1a32d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
1a32e 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a nprintf(nTemp, z
1a32f 54 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a 70 Temp, "%lld", *p
1a330 4f 70 2d 3e 70 34 2e 70 49 36 34 29 3b 0a 20 20 Op->p4.pI64);.
1a331 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1a332 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 . case P4_INT
1a333 33 32 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 32: {. sqli
1a334 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 te3_snprintf(nTe
1a335 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 64 22 2c mp, zTemp, "%d",
1a336 20 70 4f 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 20 pOp->p4.i);.
1a337 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1a338 20 20 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c case P4_REAL
1a339 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 : {. sqlite
1a33a 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 3_snprintf(nTemp
1a33b 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 , zTemp, "%.16g"
1a33c 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c , *pOp->p4.pReal
1a33d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
1a33e 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 }. case P
1a33f 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 4d 4_MEM: {. M
1a340 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e em *pMem = pOp->
1a341 70 34 2e 70 4d 65 6d 3b 0a 20 20 20 20 20 20 61 p4.pMem;. a
1a342 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c ssert( (pMem->fl
1a343 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d ags & MEM_Null)=
1a344 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 =0 );. if(
1a345 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
1a346 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20 M_Str ){.
1a347 20 7a 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a zP4 = pMem->z;.
1a348 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1a349 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
1a34a 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 M_Int ){.
1a34b 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1a34c 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 f(nTemp, zTemp,
1a34d 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e "%lld", pMem->u.
1a34e 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 i);. }else
1a34f 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 if( pMem->flags
1a350 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 & MEM_Real ){.
1a351 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
1a352 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 printf(nTemp, zT
1a353 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 70 4d emp, "%.16g", pM
1a354 65 6d 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d 65 em->r);. }e
1a355 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 lse{. ass
1a356 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 ert( pMem->flags
1a357 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 & MEM_Blob );.
1a358 20 20 20 20 20 20 20 7a 50 34 20 3d 20 22 28 62 zP4 = "(b
1a359 6c 6f 62 29 22 3b 0a 20 20 20 20 20 20 7d 0a 20 lob)";. }.
1a35a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1a35b 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
1a35c 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1a35d 4c 45 0a 20 20 20 20 63 61 73 65 20 50 34 5f 56 LE. case P4_V
1a35e 54 41 42 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c TAB: {. sql
1a35f 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
1a360 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 = pOp->p4.pVtab
1a361 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20 20 20 73 ->pVtab;. s
1a362 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1a363 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 76 nTemp, zTemp, "v
1a364 74 61 62 3a 25 70 3a 25 70 22 2c 20 70 56 74 61 tab:%p:%p", pVta
1a365 62 2c 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c b, pVtab->pModul
1a366 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b e);. break;
1a367 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
1a368 20 20 63 61 73 65 20 50 34 5f 49 4e 54 41 52 52 case P4_INTARR
1a369 41 59 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 AY: {. sqli
1a36a 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 te3_snprintf(nTe
1a36b 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 69 6e 74 61 mp, zTemp, "inta
1a36c 72 72 61 79 22 29 3b 0a 20 20 20 20 20 20 62 72 rray");. br
1a36d 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1a36e 61 73 65 20 50 34 5f 53 55 42 50 52 4f 47 52 41 ase P4_SUBPROGRA
1a36f 4d 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 M: {. sqlit
1a370 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d e3_snprintf(nTem
1a371 70 2c 20 7a 54 65 6d 70 2c 20 22 70 72 6f 67 72 p, zTemp, "progr
1a372 61 6d 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 am");. brea
1a373 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 k;. }. def
1a374 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 7a 50 ault: {. zP
1a375 34 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 4 = pOp->p4.z;.
1a376 20 20 20 20 20 69 66 28 20 7a 50 34 3d 3d 30 20 if( zP4==0
1a377 29 7b 0a 20 20 20 20 20 20 20 20 7a 50 34 20 3d ){. zP4 =
1a378 20 7a 54 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 zTemp;.
1a379 7a 54 65 6d 70 5b 30 5d 20 3d 20 30 3b 0a 20 20 zTemp[0] = 0;.
1a37a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1a37b 20 20 61 73 73 65 72 74 28 20 7a 50 34 21 3d 30 assert( zP4!=0
1a37c 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 50 34 );. return zP4
1a37d 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
1a37e 2a 20 44 65 63 6c 61 72 65 20 74 6f 20 74 68 65 * Declare to the
1a37f 20 56 64 62 65 20 74 68 61 74 20 74 68 65 20 42 Vdbe that the B
1a380 54 72 65 65 20 6f 62 6a 65 63 74 20 61 74 20 64 Tree object at d
1a381 62 2d 3e 61 44 62 5b 69 5d 20 69 73 20 75 73 65 b->aDb[i] is use
1a382 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1a383 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1a384 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 56 3VdbeUsesBtree(V
1a385 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a dbe *p, int i){.
1a386 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 20 20 61 73 int mask;. as
1a387 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
1a388 70 2d 3e 64 62 2d 3e 6e 44 62 20 26 26 20 69 3c p->db->nDb && i<
1a389 73 69 7a 65 6f 66 28 75 33 32 29 2a 38 20 29 3b sizeof(u32)*8 );
1a38a 0a 20 20 61 73 73 65 72 74 28 20 69 3c 28 69 6e . assert( i<(in
1a38b 74 29 73 69 7a 65 6f 66 28 70 2d 3e 62 74 72 65 t)sizeof(p->btre
1a38c 65 4d 61 73 6b 29 2a 38 20 29 3b 0a 20 20 6d 61 eMask)*8 );. ma
1a38d 73 6b 20 3d 20 28 28 75 33 32 29 31 29 3c 3c 69 sk = ((u32)1)<<i
1a38e 3b 0a 20 20 69 66 28 20 28 70 2d 3e 62 74 72 65 ;. if( (p->btre
1a38f 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 eMask & mask)==0
1a390 20 29 7b 0a 20 20 20 20 70 2d 3e 62 74 72 65 65 ){. p->btree
1a391 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 Mask |= mask;.
1a392 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 sqlite3BtreeMu
1a393 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 28 26 texArrayInsert(&
1a394 70 2d 3e 61 4d 75 74 65 78 2c 20 70 2d 3e 64 62 p->aMutex, p->db
1a395 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b 0a 20 ->aDb[i].pBt);.
1a396 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 65 66 69 6e }.}...#if defin
1a397 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 ed(VDBE_PROFILE)
1a398 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
1a399 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 TE_DEBUG)./*.**
1a39a 50 72 69 6e 74 20 61 20 73 69 6e 67 6c 65 20 6f Print a single o
1a39b 70 63 6f 64 65 2e 20 20 54 68 69 73 20 72 6f 75 pcode. This rou
1a39c 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 tine is used for
1a39d 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e debugging only.
1a39e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a39f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1a3a0 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 45 20 dbePrintOp(FILE
1a3a1 2a 70 4f 75 74 2c 20 69 6e 74 20 70 63 2c 20 4f *pOut, int pc, O
1a3a2 70 20 2a 70 4f 70 29 7b 0a 20 20 63 68 61 72 20 p *pOp){. char
1a3a3 2a 7a 50 34 3b 0a 20 20 63 68 61 72 20 7a 50 74 *zP4;. char zPt
1a3a4 72 5b 35 30 5d 3b 0a 20 20 73 74 61 74 69 63 20 r[50];. static
1a3a5 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
1a3a6 6d 61 74 31 20 3d 20 22 25 34 64 20 25 2d 31 33 mat1 = "%4d %-13
1a3a7 73 20 25 34 64 20 25 34 64 20 25 34 64 20 25 2d s %4d %4d %4d %-
1a3a8 34 73 20 25 2e 32 58 20 25 73 5c 6e 22 3b 0a 20 4s %.2X %s\n";.
1a3a9 20 69 66 28 20 70 4f 75 74 3d 3d 30 20 29 20 70 if( pOut==0 ) p
1a3aa 4f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 Out = stdout;.
1a3ab 7a 50 34 20 3d 20 64 69 73 70 6c 61 79 50 34 28 zP4 = displayP4(
1a3ac 70 4f 70 2c 20 7a 50 74 72 2c 20 73 69 7a 65 6f pOp, zPtr, sizeo
1a3ad 66 28 7a 50 74 72 29 29 3b 0a 20 20 66 70 72 69 f(zPtr));. fpri
1a3ae 6e 74 66 28 70 4f 75 74 2c 20 7a 46 6f 72 6d 61 ntf(pOut, zForma
1a3af 74 31 2c 20 70 63 2c 20 0a 20 20 20 20 20 20 73 t1, pc, . s
1a3b0 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 qlite3OpcodeName
1a3b1 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 2c 20 70 (pOp->opcode), p
1a3b2 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c Op->p1, pOp->p2,
1a3b3 20 70 4f 70 2d 3e 70 33 2c 20 7a 50 34 2c 20 70 pOp->p3, zP4, p
1a3b4 4f 70 2d 3e 70 35 2c 0a 23 69 66 64 65 66 20 53 Op->p5,.#ifdef S
1a3b5 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 QLITE_DEBUG.
1a3b6 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 pOp->zComment
1a3b7 3f 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 ? pOp->zComment
1a3b8 3a 20 22 22 0a 23 65 6c 73 65 0a 20 20 20 20 20 : "".#else.
1a3b9 20 22 22 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a "".#endif. );.
1a3ba 20 20 66 66 6c 75 73 68 28 70 4f 75 74 29 3b 0a fflush(pOut);.
1a3bb 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1a3bc 52 65 6c 65 61 73 65 20 61 6e 20 61 72 72 61 79 Release an array
1a3bd 20 6f 66 20 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e of N Mem elemen
1a3be 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 ts.*/.static voi
1a3bf 64 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 d releaseMemArra
1a3c0 79 28 4d 65 6d 20 2a 70 2c 20 69 6e 74 20 4e 29 y(Mem *p, int N)
1a3c1 7b 0a 20 20 69 66 28 20 70 20 26 26 20 4e 20 29 {. if( p && N )
1a3c2 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 3b {. Mem *pEnd;
1a3c3 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 . sqlite3 *db
1a3c4 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 75 38 = p->db;. u8
1a3c5 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 20 3d malloc_failed =
1a3c6 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1a3c7 64 3b 0a 20 20 20 20 66 6f 72 28 70 45 6e 64 3d d;. for(pEnd=
1a3c8 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e 64 3b 20 70 &p[N]; p<pEnd; p
1a3c9 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 ++){. asser
1a3ca 74 28 20 28 26 70 5b 31 5d 29 3d 3d 70 45 6e 64 t( (&p[1])==pEnd
1a3cb 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d 3d 70 5b 31 || p[0].db==p[1
1a3cc 5d 2e 64 62 20 29 3b 0a 0a 20 20 20 20 20 20 2f ].db );.. /
1a3cd 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 69 73 20 * This block is
1a3ce 72 65 61 6c 6c 79 20 61 6e 20 69 6e 6c 69 6e 65 really an inline
1a3cf 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c d version of sql
1a3d0 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1a3d1 73 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 74 68 se(). ** th
1a3d2 61 74 20 74 61 6b 65 73 20 61 64 76 61 6e 74 61 at takes advanta
1a3d3 67 65 20 6f 66 20 74 68 65 20 66 61 63 74 20 74 ge of the fact t
1a3d4 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 hat the memory c
1a3d5 65 6c 6c 20 76 61 6c 75 65 20 69 73 20 0a 20 20 ell value is .
1a3d6 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 73 65 74 ** being set
1a3d7 20 74 6f 20 4e 55 4c 4c 20 61 66 74 65 72 20 72 to NULL after r
1a3d8 65 6c 65 61 73 69 6e 67 20 61 6e 79 20 64 79 6e eleasing any dyn
1a3d9 61 6d 69 63 20 72 65 73 6f 75 72 63 65 73 2e 0a amic resources..
1a3da 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
1a3db 2a 20 54 68 65 20 6a 75 73 74 69 66 69 63 61 74 * The justificat
1a3dc 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69 63 61 74 ion for duplicat
1a3dd 69 6e 67 20 63 6f 64 65 20 69 73 20 74 68 61 74 ing code is that
1a3de 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 0a 20 according to .
1a3df 20 20 20 20 20 2a 2a 20 63 61 6c 6c 67 72 69 6e ** callgrin
1a3e0 64 2c 20 74 68 69 73 20 63 61 75 73 65 73 20 61 d, this causes a
1a3e1 20 63 65 72 74 61 69 6e 20 74 65 73 74 20 63 61 certain test ca
1a3e2 73 65 20 74 6f 20 68 69 74 20 74 68 65 20 43 50 se to hit the CP
1a3e3 55 20 34 2e 37 20 0a 20 20 20 20 20 20 2a 2a 20 U 4.7 . **
1a3e4 70 65 72 63 65 6e 74 20 6c 65 73 73 20 28 78 38 percent less (x8
1a3e5 36 20 6c 69 6e 75 78 2c 20 67 63 63 20 76 65 72 6 linux, gcc ver
1a3e6 73 69 6f 6e 20 34 2e 31 2e 32 2c 20 2d 4f 36 29 sion 4.1.2, -O6)
1a3e7 20 74 68 61 6e 20 69 66 20 0a 20 20 20 20 20 20 than if .
1a3e8 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 6c ** sqlite3MemRel
1a3e9 65 61 73 65 28 29 20 77 65 72 65 20 63 61 6c 6c ease() were call
1a3ea 65 64 20 66 72 6f 6d 20 68 65 72 65 2e 20 57 69 ed from here. Wi
1a3eb 74 68 20 2d 4f 32 2c 20 74 68 69 73 20 6a 75 6d th -O2, this jum
1a3ec 70 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 36 ps. ** to 6
1a3ed 2e 36 20 70 65 72 63 65 6e 74 2e 20 54 68 65 20 .6 percent. The
1a3ee 74 65 73 74 20 63 61 73 65 20 69 73 20 69 6e 73 test case is ins
1a3ef 65 72 74 69 6e 67 20 31 30 30 30 20 72 6f 77 73 erting 1000 rows
1a3f0 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 0a 20 into a table .
1a3f1 20 20 20 20 20 2a 2a 20 77 69 74 68 20 6e 6f 20 ** with no
1a3f2 69 6e 64 65 78 65 73 20 75 73 69 6e 67 20 61 20 indexes using a
1a3f3 73 69 6e 67 6c 65 20 70 72 65 70 61 72 65 64 20 single prepared
1a3f4 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 INSERT statement
1a3f5 2c 20 62 69 6e 64 28 29 20 0a 20 20 20 20 20 20 , bind() .
1a3f6 2a 2a 20 61 6e 64 20 72 65 73 65 74 28 29 2e 20 ** and reset().
1a3f7 49 6e 73 65 72 74 73 20 61 72 65 20 67 72 6f 75 Inserts are grou
1a3f8 70 65 64 20 69 6e 74 6f 20 61 20 74 72 61 6e 73 ped into a trans
1a3f9 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f action.. */
1a3fa 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 66 6c . if( p->fl
1a3fb 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d ags&(MEM_Agg|MEM
1a3fc 5f 44 79 6e 7c 4d 45 4d 5f 46 72 61 6d 65 7c 4d _Dyn|MEM_Frame|M
1a3fd 45 4d 5f 52 6f 77 53 65 74 29 20 29 7b 0a 20 20 EM_RowSet) ){.
1a3fe 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1a3ff 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 29 3b 0a eMemRelease(p);.
1a400 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1a401 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 p->zMalloc ){.
1a402 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1a403 72 65 65 28 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c ree(db, p->zMall
1a404 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e oc);. p->
1a405 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 zMalloc = 0;.
1a406 20 20 20 7d 0a 0a 20 20 20 20 20 20 70 2d 3e 66 }.. p->f
1a407 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b lags = MEM_Null;
1a408 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6d . }. db->m
1a409 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 6d 61 allocFailed = ma
1a40a 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 7d lloc_failed;. }
1a40b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
1a40c 20 61 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a a VdbeFrame obj
1a40d 65 63 74 20 61 6e 64 20 69 74 73 20 63 6f 6e 74 ect and its cont
1a40e 65 6e 74 73 2e 20 56 64 62 65 46 72 61 6d 65 20 ents. VdbeFrame
1a40f 6f 62 6a 65 63 74 73 20 61 72 65 0a 2a 2a 20 61 objects are.** a
1a410 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 llocated by the
1a411 4f 50 5f 50 72 6f 67 72 61 6d 20 6f 70 63 6f 64 OP_Program opcod
1a412 65 20 69 6e 20 73 71 6c 69 74 65 33 56 64 62 65 e in sqlite3Vdbe
1a413 45 78 65 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 Exec()..*/.SQLIT
1a414 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1a415 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 44 qlite3VdbeFrameD
1a416 65 6c 65 74 65 28 56 64 62 65 46 72 61 6d 65 20 elete(VdbeFrame
1a417 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 *p){. int i;.
1a418 4d 65 6d 20 2a 61 4d 65 6d 20 3d 20 56 64 62 65 Mem *aMem = Vdbe
1a419 46 72 61 6d 65 4d 65 6d 28 70 29 3b 0a 20 20 56 FrameMem(p);. V
1a41a 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 43 73 dbeCursor **apCs
1a41b 72 20 3d 20 28 56 64 62 65 43 75 72 73 6f 72 20 r = (VdbeCursor
1a41c 2a 2a 29 26 61 4d 65 6d 5b 70 2d 3e 6e 43 68 69 **)&aMem[p->nChi
1a41d 6c 64 4d 65 6d 5d 3b 0a 20 20 66 6f 72 28 69 3d ldMem];. for(i=
1a41e 30 3b 20 69 3c 70 2d 3e 6e 43 68 69 6c 64 43 73 0; i<p->nChildCs
1a41f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c r; i++){. sql
1a420 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 ite3VdbeFreeCurs
1a421 6f 72 28 70 2d 3e 76 2c 20 61 70 43 73 72 5b 69 or(p->v, apCsr[i
1a422 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 ]);. }. releas
1a423 65 4d 65 6d 41 72 72 61 79 28 61 4d 65 6d 2c 20 eMemArray(aMem,
1a424 70 2d 3e 6e 43 68 69 6c 64 4d 65 6d 29 3b 0a 20 p->nChildMem);.
1a425 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 sqlite3DbFree(p
1a426 2d 3e 76 2d 3e 64 62 2c 20 70 29 3b 0a 7d 0a 0a ->v->db, p);.}..
1a427 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
1a428 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e NABLE_MEMORY_MAN
1a429 41 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 AGEMENT.SQLITE_P
1a42a 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1a42b 65 33 56 64 62 65 52 65 6c 65 61 73 65 42 75 66 e3VdbeReleaseBuf
1a42c 66 65 72 73 28 56 64 62 65 20 2a 70 29 7b 0a 20 fers(Vdbe *p){.
1a42d 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 6e int ii;. int n
1a42e 46 72 65 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 Free = 0;. asse
1a42f 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1a430 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
1a431 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 69 tex) );. for(ii
1a432 3d 31 3b 20 69 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b =1; ii<=p->nMem;
1a433 20 69 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 20 ii++){. Mem
1a434 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d *pMem = &p->aMem
1a435 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 4d [ii];. if( pM
1a436 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
1a437 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 20 20 RowSet ){.
1a438 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 sqlite3RowSetCle
1a439 61 72 28 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 ar(pMem->u.pRowS
1a43a 65 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 et);. }. i
1a43b 66 28 20 70 4d 65 6d 2d 3e 7a 20 26 26 20 70 4d f( pMem->z && pM
1a43c 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 em->flags&MEM_Dy
1a43d 6e 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 n ){. asser
1a43e 74 28 20 21 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 t( !pMem->xDel )
1a43f 3b 0a 20 20 20 20 20 20 6e 46 72 65 65 20 2b 3d ;. nFree +=
1a440 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1a441 53 69 7a 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 Size(pMem->db, p
1a442 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 73 Mem->z);. s
1a443 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1a444 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 ease(pMem);.
1a445 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e }. }. return n
1a446 46 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a Free;.}.#endif..
1a447 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1a448 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 2f 2a 0a 2a MIT_EXPLAIN./*.*
1a449 2a 20 47 69 76 65 20 61 20 6c 69 73 74 69 6e 67 * Give a listing
1a44a 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 of the program
1a44b 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d in the virtual m
1a44c 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 achine..**.** Th
1a44d 65 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 74 e interface is t
1a44e 68 65 20 73 61 6d 65 20 61 73 20 73 71 6c 69 74 he same as sqlit
1a44f 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 42 e3VdbeExec(). B
1a450 75 74 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a ut instead of.**
1a451 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 63 6f 64 running the cod
1a452 65 2c 20 69 74 20 69 6e 76 6f 6b 65 73 20 74 68 e, it invokes th
1a453 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20 e callback once
1a454 66 6f 72 20 65 61 63 68 20 69 6e 73 74 72 75 63 for each instruc
1a455 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 65 tion..** This fe
1a456 61 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f ature is used to
1a457 20 69 6d 70 6c 65 6d 65 6e 74 20 22 45 58 50 4c implement "EXPL
1a458 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e AIN"..**.** When
1a459 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 2c 20 p->explain==1,
1a45a 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e each instruction
1a45b 20 69 73 20 6c 69 73 74 65 64 2e 20 20 57 68 65 is listed. Whe
1a45c 6e 0a 2a 2a 20 70 2d 3e 65 78 70 6c 61 69 6e 3d n.** p->explain=
1a45d 3d 32 2c 20 6f 6e 6c 79 20 4f 50 5f 45 78 70 6c =2, only OP_Expl
1a45e 61 69 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 ain instructions
1a45f 20 61 72 65 20 6c 69 73 74 65 64 20 61 6e 64 20 are listed and
1a460 74 68 65 73 65 0a 2a 2a 20 61 72 65 20 73 68 6f these.** are sho
1a461 77 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e wn in a differen
1a462 74 20 66 6f 72 6d 61 74 2e 20 20 70 2d 3e 65 78 t format. p->ex
1a463 70 6c 61 69 6e 3d 3d 32 20 69 73 20 75 73 65 64 plain==2 is used
1a464 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a 2a to implement.**
1a465 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50 EXPLAIN QUERY P
1a466 4c 41 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 LAN..*/.SQLITE_P
1a467 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1a468 65 33 56 64 62 65 4c 69 73 74 28 0a 20 20 56 64 e3VdbeList(. Vd
1a469 62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 20 be *p
1a46a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 /* The V
1a46b 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 DBE */.){. int
1a46c 6e 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20 nRow;
1a46d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a46e 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 /* Total number
1a46f 20 6f 66 20 72 6f 77 73 20 74 6f 20 72 65 74 75 of rows to retu
1a470 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75 62 rn */. int nSub
1a471 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1a472 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a473 4e 75 6d 62 65 72 20 6f 66 20 73 75 62 2d 76 64 Number of sub-vd
1a474 62 65 73 20 73 65 65 6e 20 73 6f 20 66 61 72 20 bes seen so far
1a475 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 */. SubProgram
1a476 2a 2a 61 70 53 75 62 20 3d 20 30 3b 20 20 20 20 **apSub = 0;
1a477 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 /* Arr
1a478 61 79 20 6f 66 20 73 75 62 2d 76 64 62 65 73 20 ay of sub-vdbes
1a479 2a 2f 0a 20 20 4d 65 6d 20 2a 70 53 75 62 20 3d */. Mem *pSub =
1a47a 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 0;. sqlite3 *d
1a47b 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 b = p->db;. int
1a47c 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 i;. int rc = S
1a47d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 20 QLITE_OK;. Mem
1a47e 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 *pMem = p->pResu
1a47f 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d ltSet = &p->aMem
1a480 5b 31 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 [1];.. assert(
1a481 70 2d 3e 65 78 70 6c 61 69 6e 20 29 3b 0a 20 20 p->explain );.
1a482 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 assert( p->magic
1a483 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e ==VDBE_MAGIC_RUN
1a484 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 );. assert( db
1a485 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f ->magic==SQLITE_
1a486 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 20 MAGIC_BUSY );.
1a487 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 assert( p->rc==S
1a488 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 QLITE_OK || p->r
1a489 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 7c c==SQLITE_BUSY |
1a48a 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f | p->rc==SQLITE_
1a48b 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20 2f 2a 20 45 NOMEM );.. /* E
1a48c 76 65 6e 20 74 68 6f 75 67 68 20 74 68 69 73 20 ven though this
1a48d 6f 70 63 6f 64 65 20 64 6f 65 73 20 6e 6f 74 20 opcode does not
1a48e 75 73 65 20 64 79 6e 61 6d 69 63 20 73 74 72 69 use dynamic stri
1a48f 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65 ngs for. ** the
1a490 20 72 65 73 75 6c 74 2c 20 72 65 73 75 6c 74 20 result, result
1a491 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62 65 63 6f columns may beco
1a492 6d 65 20 64 79 6e 61 6d 69 63 20 69 66 20 74 68 me dynamic if th
1a493 65 20 75 73 65 72 20 63 61 6c 6c 73 0a 20 20 2a e user calls. *
1a494 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e * sqlite3_column
1a495 5f 74 65 78 74 31 36 28 29 2c 20 63 61 75 73 69 _text16(), causi
1a496 6e 67 20 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e ng a translation
1a497 20 74 6f 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 to UTF-16 encod
1a498 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 72 65 6c 65 ing.. */. rele
1a499 61 73 65 4d 65 6d 41 72 72 61 79 28 70 4d 65 6d aseMemArray(pMem
1a49a 2c 20 38 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e , 8);.. if( p->
1a49b 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
1a49c 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 ){. /* This
1a49d 68 61 70 70 65 6e 73 20 69 66 20 61 20 6d 61 6c happens if a mal
1a49e 6c 6f 63 28 29 20 69 6e 73 69 64 65 20 61 20 63 loc() inside a c
1a49f 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 all to sqlite3_c
1a4a0 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a olumn_text() or.
1a4a1 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 ** sqlite3_c
1a4a2 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 olumn_text16() f
1a4a3 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 64 ailed. */. d
1a4a4 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1a4a5 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = 1;. return
1a4a6 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1a4a7 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f }.. /* Figure o
1a4a8 75 74 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 ut total number
1a4a9 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 69 6c of rows that wil
1a4aa 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 l be returned by
1a4ab 20 74 68 69 73 20 0a 20 20 2a 2a 20 45 58 50 4c this . ** EXPL
1a4ac 41 49 4e 20 70 72 6f 67 72 61 6d 2e 20 20 2a 2f AIN program. */
1a4ad 0a 20 20 6e 52 6f 77 20 3d 20 70 2d 3e 6e 4f 70 . nRow = p->nOp
1a4ae 3b 0a 20 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 ;. if( p->expla
1a4af 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 70 53 75 in==1 ){. pSu
1a4b0 62 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 39 5d 3b b = &p->aMem[9];
1a4b1 0a 20 20 20 20 69 66 28 20 70 53 75 62 2d 3e 66 . if( pSub->f
1a4b2 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b lags&MEM_Blob ){
1a4b3 0a 20 20 20 20 20 20 6e 53 75 62 20 3d 20 70 53 . nSub = pS
1a4b4 75 62 2d 3e 6e 2f 73 69 7a 65 6f 66 28 56 64 62 ub->n/sizeof(Vdb
1a4b5 65 2a 29 3b 0a 20 20 20 20 20 20 61 70 53 75 62 e*);. apSub
1a4b6 20 3d 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a = (SubProgram *
1a4b7 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 7d *)pSub->z;. }
1a4b8 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
1a4b9 6e 53 75 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nSub; i++){.
1a4ba 20 20 6e 52 6f 77 20 2b 3d 20 61 70 53 75 62 5b nRow += apSub[
1a4bb 69 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 0a 20 i]->nOp;. }.
1a4bc 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 69 20 }.. do{. i
1a4bd 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20 7d 77 68 = p->pc++;. }wh
1a4be 69 6c 65 28 20 69 3c 6e 52 6f 77 20 26 26 20 70 ile( i<nRow && p
1a4bf 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 26 26 20 ->explain==2 &&
1a4c0 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 p->aOp[i].opcode
1a4c1 21 3d 4f 50 5f 45 78 70 6c 61 69 6e 20 29 3b 0a !=OP_Explain );.
1a4c2 20 20 69 66 28 20 69 3e 3d 6e 52 6f 77 20 29 7b if( i>=nRow ){
1a4c3 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c . p->rc = SQL
1a4c4 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20 3d ITE_OK;. rc =
1a4c5 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 SQLITE_DONE;.
1a4c6 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 75 31 }else if( db->u1
1a4c7 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 29 .isInterrupted )
1a4c8 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 {. p->rc = SQ
1a4c9 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a LITE_INTERRUPT;.
1a4ca 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1a4cb 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 ERROR;. sqlit
1a4cc 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1a4cd 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 zErrMsg, db, "%s
1a4ce 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 ", sqlite3ErrStr
1a4cf 28 70 2d 3e 72 63 29 29 3b 0a 20 20 7d 65 6c 73 (p->rc));. }els
1a4d0 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a e{. char *z;.
1a4d1 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 Op *pOp;.
1a4d2 20 69 66 28 20 69 3c 70 2d 3e 6e 4f 70 20 29 7b if( i<p->nOp ){
1a4d3 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 26 70 2d . pOp = &p-
1a4d4 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c >aOp[i];. }el
1a4d5 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b se{. int j;
1a4d6 0a 20 20 20 20 20 20 69 20 2d 3d 20 70 2d 3e 6e . i -= p->n
1a4d7 4f 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d Op;. for(j=
1a4d8 30 3b 20 69 3e 3d 61 70 53 75 62 5b 6a 5d 2d 3e 0; i>=apSub[j]->
1a4d9 6e 4f 70 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 nOp; j++){.
1a4da 20 20 20 69 20 2d 3d 20 61 70 53 75 62 5b 6a 5d i -= apSub[j]
1a4db 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 20 7d 0a 20 ->nOp;. }.
1a4dc 20 20 20 20 20 70 4f 70 20 3d 20 26 61 70 53 75 pOp = &apSu
1a4dd 62 5b 6a 5d 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 b[j]->aOp[i];.
1a4de 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 }. if( p->e
1a4df 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 xplain==1 ){.
1a4e0 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1a4e1 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 MEM_Int;.
1a4e2 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1a4e3 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 ITE_INTEGER;.
1a4e4 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 69 pMem->u.i = i
1a4e5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1a4e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a4e7 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e /* Program coun
1a4e8 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 ter */. pMe
1a4e9 6d 2b 2b 3b 0a 20 20 0a 20 20 20 20 20 20 70 4d m++;. . pM
1a4ea 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
1a4eb 53 74 61 74 69 63 7c 4d 45 4d 5f 53 74 72 7c 4d Static|MEM_Str|M
1a4ec 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 EM_Term;. p
1a4ed 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 Mem->z = (char*)
1a4ee 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d sqlite3OpcodeNam
1a4ef 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b 20 e(pOp->opcode);
1a4f0 20 2f 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a 20 20 /* Opcode */.
1a4f1 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
1a4f2 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 ->z!=0 );.
1a4f3 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 pMem->n = sqlite
1a4f4 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 3Strlen30(pMem->
1a4f5 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e z);. pMem->
1a4f6 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 type = SQLITE_TE
1a4f7 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e XT;. pMem->
1a4f8 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 enc = SQLITE_UTF
1a4f9 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 8;. pMem++;
1a4fa 0a 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d .. if( pOp-
1a4fb 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53 55 42 50 >p4type==P4_SUBP
1a4fc 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20 20 20 20 ROGRAM ){.
1a4fd 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 6e int nByte = (n
1a4fe 53 75 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53 75 Sub+1)*sizeof(Su
1a4ff 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20 bProgram*);.
1a500 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 int j;.
1a501 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 53 for(j=0; j<nS
1a502 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 ub; j++){.
1a503 20 20 20 20 69 66 28 20 61 70 53 75 62 5b 6a 5d if( apSub[j]
1a504 3d 3d 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72 ==pOp->p4.pProgr
1a505 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 am ) break;.
1a506 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
1a507 28 20 6a 3d 3d 6e 53 75 62 20 26 26 20 53 51 4c ( j==nSub && SQL
1a508 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 ITE_OK==sqlite3V
1a509 64 62 65 4d 65 6d 47 72 6f 77 28 70 53 75 62 2c dbeMemGrow(pSub,
1a50a 20 6e 42 79 74 65 2c 20 31 29 20 29 7b 0a 20 20 nByte, 1) ){.
1a50b 20 20 20 20 20 20 20 20 61 70 53 75 62 20 3d 20 apSub =
1a50c 28 53 75 62 50 72 6f 67 72 61 6d 20 2a 2a 29 70 (SubProgram **)p
1a50d 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 20 20 20 20 Sub->z;.
1a50e 20 20 61 70 53 75 62 5b 6e 53 75 62 2b 2b 5d 20 apSub[nSub++]
1a50f 3d 20 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72 = pOp->p4.pProgr
1a510 61 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 am;. pS
1a511 75 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d ub->flags |= MEM
1a512 5f 42 6c 6f 62 3b 0a 20 20 20 20 20 20 20 20 20 _Blob;.
1a513 20 70 53 75 62 2d 3e 6e 20 3d 20 6e 53 75 62 2a pSub->n = nSub*
1a514 73 69 7a 65 6f 66 28 53 75 62 50 72 6f 67 72 61 sizeof(SubProgra
1a515 6d 2a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 m*);. }.
1a516 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 }. }..
1a517 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
1a518 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 MEM_Int;. pMe
1a519 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 m->u.i = pOp->p1
1a51a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1a51b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31 /* P1
1a51c 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 */. pMem->ty
1a51d 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 pe = SQLITE_INTE
1a51e 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b GER;. pMem++;
1a51f 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 .. pMem->flag
1a520 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
1a521 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 pMem->u.i = pOp
1a522 2d 3e 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 ->p2;
1a523 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a524 2a 20 50 32 20 2a 2f 0a 20 20 20 20 70 4d 65 6d * P2 */. pMem
1a525 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
1a526 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 INTEGER;. pMe
1a527 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 2d m++;.. if( p-
1a528 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 >explain==1 ){.
1a529 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
1a52a 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 = MEM_Int;.
1a52b 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f pMem->u.i = pO
1a52c 70 2d 3e 70 33 3b 20 20 20 20 20 20 20 20 20 20 p->p3;
1a52d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a52e 2f 2a 20 50 33 20 2a 2f 0a 20 20 20 20 20 20 70 /* P3 */. p
1a52f 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
1a530 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 TE_INTEGER;.
1a531 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 7d 0a pMem++;. }.
1a532 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1a533 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d VdbeMemGrow(pMem
1a534 2c 20 33 32 2c 20 30 29 20 29 7b 20 20 20 20 20 , 32, 0) ){
1a535 20 20 20 20 20 20 20 2f 2a 20 50 34 20 2a 2f 0a /* P4 */.
1a536 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d assert( p-
1a537 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
1a538 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 d );. retur
1a539 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
1a53a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e }. pMem->
1a53b 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c flags = MEM_Dyn|
1a53c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d MEM_Str|MEM_Term
1a53d 3b 0a 20 20 20 20 7a 20 3d 20 64 69 73 70 6c 61 ;. z = displa
1a53e 79 50 34 28 70 4f 70 2c 20 70 4d 65 6d 2d 3e 7a yP4(pOp, pMem->z
1a53f 2c 20 33 32 29 3b 0a 20 20 20 20 69 66 28 20 7a , 32);. if( z
1a540 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 !=pMem->z ){.
1a541 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1a542 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c 20 7a 2c mSetStr(pMem, z,
1a543 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 -1, SQLITE_UTF8
1a544 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b , 0);. }else{
1a545 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1a546 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 Mem->z!=0 );.
1a547 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c pMem->n = sql
1a548 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 ite3Strlen30(pMe
1a549 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 m->z);. pMe
1a54a 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f m->enc = SQLITE_
1a54b 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 UTF8;. }.
1a54c 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1a54d 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 70 4d ITE_TEXT;. pM
1a54e 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 em++;.. if( p
1a54f 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a ->explain==1 ){.
1a550 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1a551 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 3VdbeMemGrow(pMe
1a552 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20 20 20 20 m, 4, 0) ){.
1a553 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 assert( p->d
1a554 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1a555 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
1a556 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
1a557 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4d }. pM
1a558 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
1a559 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f Dyn|MEM_Str|MEM_
1a55a 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d Term;. pMem
1a55b 2d 3e 6e 20 3d 20 32 3b 0a 20 20 20 20 20 20 73 ->n = 2;. s
1a55c 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1a55d 33 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 2e 32 3, pMem->z, "%.2
1a55e 78 22 2c 20 70 4f 70 2d 3e 70 35 29 3b 20 20 20 x", pOp->p5);
1a55f 2f 2a 20 50 35 20 2a 2f 0a 20 20 20 20 20 20 70 /* P5 */. p
1a560 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
1a561 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70 TE_TEXT;. p
1a562 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 Mem->enc = SQLIT
1a563 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d E_UTF8;. pM
1a564 65 6d 2b 2b 3b 0a 20 20 0a 23 69 66 64 65 66 20 em++;. .#ifdef
1a565 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
1a566 20 20 20 69 66 28 20 70 4f 70 2d 3e 7a 43 6f 6d if( pOp->zCom
1a567 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 ment ){.
1a568 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
1a569 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a M_Str|MEM_Term;.
1a56a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 pMem->z
1a56b 3d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 3b = pOp->zComment;
1a56c 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e . pMem->n
1a56d 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
1a56e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 30(pMem->z);.
1a56f 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d pMem->enc =
1a570 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 SQLITE_UTF8;.
1a571 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 pMem->type
1a572 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a = SQLITE_TEXT;.
1a573 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 }else.#end
1a574 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 if. {.
1a575 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1a576 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20 20 MEM_Null;
1a577 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a578 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 /* Comment */.
1a579 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 pMem->typ
1a57a 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b e = SQLITE_NULL;
1a57b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
1a57c 20 20 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d p->nResColum
1a57d 6e 20 3d 20 38 20 2d 20 35 2a 28 70 2d 3e 65 78 n = 8 - 5*(p->ex
1a57e 70 6c 61 69 6e 2d 31 29 3b 0a 20 20 20 20 70 2d plain-1);. p-
1a57f 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b >rc = SQLITE_OK;
1a580 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1a581 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20 72 65 74 75 _ROW;. }. retu
1a582 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 rn rc;.}.#endif
1a583 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 /* SQLITE_OMIT_E
1a584 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23 69 66 64 65 XPLAIN */..#ifde
1a585 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f f SQLITE_DEBUG./
1a586 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 53 *.** Print the S
1a587 51 4c 20 74 68 61 74 20 77 61 73 20 75 73 65 64 QL that was used
1a588 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 56 to generate a V
1a589 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a DBE program..*/.
1a58a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1a58b 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 oid sqlite3VdbeP
1a58c 72 69 6e 74 53 71 6c 28 56 64 62 65 20 2a 70 29 rintSql(Vdbe *p)
1a58d 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 2d {. int nOp = p-
1a58e 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20 2a >nOp;. VdbeOp *
1a58f 70 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3c 31 pOp;. if( nOp<1
1a590 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 70 ) return;. pOp
1a591 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 = &p->aOp[0];.
1a592 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
1a593 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70 4f ==OP_Trace && pO
1a594 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 p->p4.z!=0 ){.
1a595 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 const char *z
1a596 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 = pOp->p4.z;.
1a597 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 while( sqlite3I
1a598 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b sspace(*z) ) z++
1a599 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53 51 ;. printf("SQ
1a59a 4c 3a 20 5b 25 73 5d 5c 6e 22 2c 20 7a 29 3b 0a L: [%s]\n", z);.
1a59b 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 }.}.#endif..#i
1a59c 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
1a59d 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 26 E_OMIT_TRACE) &&
1a59e 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
1a59f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29 0a ENABLE_IOTRACE).
1a5a0 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 6e 20 49 /*.** Print an I
1a5a1 4f 54 52 41 43 45 20 6d 65 73 73 61 67 65 20 73 OTRACE message s
1a5a2 68 6f 77 69 6e 67 20 53 51 4c 20 63 6f 6e 74 65 howing SQL conte
1a5a3 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nt..*/.SQLITE_PR
1a5a4 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1a5a5 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c e3VdbeIOTraceSql
1a5a6 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 (Vdbe *p){. int
1a5a7 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 nOp = p->nOp;.
1a5a8 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 VdbeOp *pOp;.
1a5a9 69 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72 61 if( sqlite3IoTra
1a5aa 63 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a ce==0 ) return;.
1a5ab 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 65 if( nOp<1 ) re
1a5ac 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 70 turn;. pOp = &p
1a5ad 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 20 ->aOp[0];. if(
1a5ae 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f pOp->opcode==OP_
1a5af 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 34 Trace && pOp->p4
1a5b0 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 .z!=0 ){. int
1a5b1 20 69 2c 20 6a 3b 0a 20 20 20 20 63 68 61 72 20 i, j;. char
1a5b2 7a 5b 31 30 30 30 5d 3b 0a 20 20 20 20 73 71 6c z[1000];. sql
1a5b3 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 ite3_snprintf(si
1a5b4 7a 65 6f 66 28 7a 29 2c 20 7a 2c 20 22 25 73 22 zeof(z), z, "%s"
1a5b5 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 , pOp->p4.z);.
1a5b6 20 20 66 6f 72 28 69 3d 30 3b 20 73 71 6c 69 74 for(i=0; sqlit
1a5b7 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b e3Isspace(z[i]);
1a5b8 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f 72 28 i++){}. for(
1a5b9 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b j=0; z[i]; i++){
1a5ba 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1a5bb 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 20 e3Isspace(z[i])
1a5bc 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a ){. if( z
1a5bd 5b 69 2d 31 5d 21 3d 27 20 27 20 29 7b 0a 20 20 [i-1]!=' ' ){.
1a5be 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d z[j++] =
1a5bf 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a ' ';. }.
1a5c0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1a5c1 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b z[j++] = z[
1a5c2 69 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 i];. }.
1a5c3 7d 0a 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a }. z[j] = 0;.
1a5c4 20 20 20 20 73 71 6c 69 74 65 33 49 6f 54 72 61 sqlite3IoTra
1a5c5 63 65 28 22 53 51 4c 20 25 73 5c 6e 22 2c 20 7a ce("SQL %s\n", z
1a5c6 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 );. }.}.#endif
1a5c7 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* !SQLITE_OMIT_
1a5c8 54 52 41 43 45 20 26 26 20 53 51 4c 49 54 45 5f TRACE && SQLITE_
1a5c9 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 2a ENABLE_IOTRACE *
1a5ca 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 /../*.** Allocat
1a5cb 65 20 73 70 61 63 65 20 66 72 6f 6d 20 61 20 66 e space from a f
1a5cc 69 78 65 64 20 73 69 7a 65 20 62 75 66 66 65 72 ixed size buffer
1a5cd 2e 20 20 4d 61 6b 65 20 2a 70 70 20 70 6f 69 6e . Make *pp poin
1a5ce 74 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f t to the.** allo
1a5cf 63 61 74 65 64 20 73 70 61 63 65 2e 20 20 28 4e cated space. (N
1a5d0 6f 74 65 3a 20 20 70 70 20 69 73 20 61 20 63 68 ote: pp is a ch
1a5d1 61 72 2a 20 72 61 74 68 65 72 20 74 68 61 6e 20 ar* rather than
1a5d2 61 20 76 6f 69 64 2a 2a 20 74 6f 0a 2a 2a 20 77 a void** to.** w
1a5d3 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 70 ork around the p
1a5d4 6f 69 6e 74 65 72 20 61 6c 69 61 73 69 6e 67 20 ointer aliasing
1a5d5 72 75 6c 65 73 20 6f 66 20 43 2e 29 20 20 2a 70 rules of C.) *p
1a5d6 70 20 73 68 6f 75 6c 64 20 69 6e 69 74 69 61 6c p should initial
1a5d7 6c 79 0a 2a 2a 20 62 65 20 7a 65 72 6f 2e 20 20 ly.** be zero.
1a5d8 49 66 20 2a 70 70 20 69 73 20 6e 6f 74 20 7a 65 If *pp is not ze
1a5d9 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 ro, that means t
1a5da 68 61 74 20 74 68 65 20 73 70 61 63 65 20 68 61 hat the space ha
1a5db 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65 s already.** bee
1a5dc 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 n allocated and
1a5dd 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
1a5de 61 20 6e 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 6e 42 a noop..**.** nB
1a5df 79 74 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65 yte is the numbe
1a5e0 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 r of bytes of sp
1a5e1 61 63 65 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a ace needed..**.*
1a5e2 2a 20 2a 70 70 46 72 6f 6d 20 70 6f 69 6e 74 20 * *ppFrom point
1a5e3 74 6f 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61 to available spa
1a5e4 63 65 20 61 6e 64 20 70 45 6e 64 20 70 6f 69 6e ce and pEnd poin
1a5e5 74 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 ts to the end of
1a5e6 20 74 68 65 0a 2a 2a 20 61 76 61 69 6c 61 62 6c the.** availabl
1a5e7 65 20 73 70 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 2a e space..**.** *
1a5e8 70 6e 42 79 74 65 20 69 73 20 61 20 63 6f 75 6e pnByte is a coun
1a5e9 74 65 72 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 ter of the numbe
1a5ea 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 r of bytes of sp
1a5eb 61 63 65 20 74 68 61 74 20 68 61 76 65 20 66 61 ace that have fa
1a5ec 69 6c 65 64 0a 2a 2a 20 74 6f 20 61 6c 6c 6f 63 iled.** to alloc
1a5ed 61 74 65 2e 20 20 49 66 20 74 68 65 72 65 20 69 ate. If there i
1a5ee 73 20 69 6e 73 75 66 66 69 63 69 65 6e 74 20 73 s insufficient s
1a5ef 70 61 63 65 20 69 6e 20 2a 70 70 46 72 6f 6d 20 pace in *ppFrom
1a5f0 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a 2a to satisfy the.*
1a5f1 2a 20 72 65 71 75 65 73 74 2c 20 74 68 65 6e 20 * request, then
1a5f2 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74 increment *pnByt
1a5f3 65 20 62 79 20 74 68 65 20 61 6d 6f 75 6e 74 20 e by the amount
1a5f4 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 2e 0a of the request..
1a5f5 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 */.static void a
1a5f6 6c 6c 6f 63 53 70 61 63 65 28 0a 20 20 63 68 61 llocSpace(. cha
1a5f7 72 20 2a 70 70 2c 20 20 20 20 20 20 20 20 20 20 r *pp,
1a5f8 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 53 65 74 /* IN/OUT: Set
1a5f9 20 2a 70 70 20 74 6f 20 70 6f 69 6e 74 20 74 6f *pp to point to
1a5fa 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 allocated buffe
1a5fb 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 r */. int nByte
1a5fc 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e , /* N
1a5fd 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
1a5fe 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 o allocate */.
1a5ff 75 38 20 2a 2a 70 70 46 72 6f 6d 2c 20 20 20 20 u8 **ppFrom,
1a600 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 /* IN/OUT:
1a601 41 6c 6c 6f 63 61 74 65 20 66 72 6f 6d 20 2a 70 Allocate from *p
1a602 70 46 72 6f 6d 20 2a 2f 0a 20 20 75 38 20 2a 70 pFrom */. u8 *p
1a603 45 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 End,
1a604 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 31 20 /* Pointer to 1
1a605 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e byte past the en
1a606 64 20 6f 66 20 2a 70 70 46 72 6f 6d 20 62 75 66 d of *ppFrom buf
1a607 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e fer */. int *pn
1a608 42 79 74 65 20 20 20 20 20 20 20 20 20 20 2f 2a Byte /*
1a609 20 49 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 If allocation c
1a60a 61 6e 6e 6f 74 20 62 65 20 6d 61 64 65 2c 20 69 annot be made, i
1a60b 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74 65 ncrement *pnByte
1a60c 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 */.){. assert(
1a60d 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 EIGHT_BYTE_ALIG
1a60e 4e 4d 45 4e 54 28 2a 70 70 46 72 6f 6d 29 20 29 NMENT(*ppFrom) )
1a60f 3b 0a 20 20 69 66 28 20 28 2a 28 76 6f 69 64 2a ;. if( (*(void*
1a610 2a 29 70 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 *)pp)==0 ){.
1a611 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 6e nByte = ROUND8(n
1a612 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 26 Byte);. if( &
1a613 28 2a 70 70 46 72 6f 6d 29 5b 6e 42 79 74 65 5d (*ppFrom)[nByte]
1a614 20 3c 3d 20 70 45 6e 64 20 29 7b 0a 20 20 20 20 <= pEnd ){.
1a615 20 20 2a 28 76 6f 69 64 2a 2a 29 70 70 20 3d 20 *(void**)pp =
1a616 28 76 6f 69 64 20 2a 29 2a 70 70 46 72 6f 6d 3b (void *)*ppFrom;
1a617 0a 20 20 20 20 20 20 2a 70 70 46 72 6f 6d 20 2b . *ppFrom +
1a618 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d 65 6c = nByte;. }el
1a619 73 65 7b 0a 20 20 20 20 20 20 2a 70 6e 42 79 74 se{. *pnByt
1a61a 65 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 e += nByte;.
1a61b 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 }. }.}../*.** P
1a61c 72 65 70 61 72 65 20 61 20 76 69 72 74 75 61 6c repare a virtual
1a61d 20 6d 61 63 68 69 6e 65 20 66 6f 72 20 65 78 65 machine for exe
1a61e 63 75 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e cution. This in
1a61f 76 6f 6c 76 65 73 20 74 68 69 6e 67 73 20 73 75 volves things su
1a620 63 68 0a 2a 2a 20 61 73 20 61 6c 6c 6f 63 61 74 ch.** as allocat
1a621 69 6e 67 20 73 74 61 63 6b 20 73 70 61 63 65 20 ing stack space
1a622 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 and initializing
1a623 20 74 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 the program cou
1a624 6e 74 65 72 2e 0a 2a 2a 20 41 66 74 65 72 20 74 nter..** After t
1a625 68 65 20 56 44 42 45 20 68 61 73 20 62 65 20 70 he VDBE has be p
1a626 72 65 70 70 65 64 2c 20 69 74 20 63 61 6e 20 62 repped, it can b
1a627 65 20 65 78 65 63 75 74 65 64 20 62 79 20 6f 6e e executed by on
1a628 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 63 61 6c e or more.** cal
1a629 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 ls to sqlite3Vdb
1a62a 65 45 78 65 63 28 29 2e 20 20 0a 2a 2a 0a 2a 2a eExec(). .**.**
1a62b 20 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c This is the onl
1a62c 79 20 77 61 79 20 74 6f 20 6d 6f 76 65 20 61 20 y way to move a
1a62d 56 44 42 45 20 66 72 6f 6d 20 56 44 42 45 5f 4d VDBE from VDBE_M
1a62e 41 47 49 43 5f 49 4e 49 54 20 74 6f 0a 2a 2a 20 AGIC_INIT to.**
1a62f 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 2e 0a VDBE_MAGIC_RUN..
1a630 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
1a631 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 ion may be calle
1a632 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 d more than once
1a633 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 76 69 72 on a single vir
1a634 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a tual machine..**
1a635 20 54 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 The first call
1a636 69 73 20 6d 61 64 65 20 77 68 69 6c 65 20 63 6f is made while co
1a637 6d 70 69 6c 69 6e 67 20 74 68 65 20 53 51 4c 20 mpiling the SQL
1a638 73 74 61 74 65 6d 65 6e 74 2e 20 53 75 62 73 65 statement. Subse
1a639 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 61 quent.** calls a
1a63a 72 65 20 6d 61 64 65 20 61 73 20 70 61 72 74 20 re made as part
1a63b 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f of the process o
1a63c 66 20 72 65 73 65 74 74 69 6e 67 20 61 20 73 74 f resetting a st
1a63d 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 0a 2a 2a atement to be.**
1a63e 20 72 65 2d 65 78 65 63 75 74 65 64 20 28 66 72 re-executed (fr
1a63f 6f 6d 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c om a call to sql
1a640 69 74 65 33 5f 72 65 73 65 74 28 29 29 2e 20 54 ite3_reset()). T
1a641 68 65 20 6e 56 61 72 2c 20 6e 4d 65 6d 2c 20 6e he nVar, nMem, n
1a642 43 75 72 73 6f 72 20 0a 2a 2a 20 61 6e 64 20 69 Cursor .** and i
1a643 73 45 78 70 6c 61 69 6e 20 70 61 72 61 6d 65 74 sExplain paramet
1a644 65 72 73 20 61 72 65 20 6f 6e 6c 79 20 70 61 73 ers are only pas
1a645 73 65 64 20 63 6f 72 72 65 63 74 20 76 61 6c 75 sed correct valu
1a646 65 73 20 74 68 65 20 66 69 72 73 74 20 74 69 6d es the first tim
1a647 65 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f e.** the functio
1a648 6e 20 69 73 20 63 61 6c 6c 65 64 2e 20 4f 6e 20 n is called. On
1a649 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 subsequent calls
1a64a 2c 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 , from sqlite3_r
1a64b 65 73 65 74 28 29 2c 20 6e 56 61 72 0a 2a 2a 20 eset(), nVar.**
1a64c 69 73 20 70 61 73 73 65 64 20 2d 31 20 61 6e 64 is passed -1 and
1a64d 20 6e 4d 65 6d 2c 20 6e 43 75 72 73 6f 72 20 61 nMem, nCursor a
1a64e 6e 64 20 69 73 45 78 70 6c 61 69 6e 20 61 72 65 nd isExplain are
1a64f 20 61 6c 6c 20 70 61 73 73 65 64 20 7a 65 72 6f all passed zero
1a650 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a651 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1a652 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 0a 20 VdbeMakeReady(.
1a653 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 Vdbe *p,
1a654 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a655 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 20 /* The VDBE */.
1a656 20 69 6e 74 20 6e 56 61 72 2c 20 20 20 20 20 20 int nVar,
1a657 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a658 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f 27 /* Number of '?'
1a659 20 73 65 65 20 69 6e 20 74 68 65 20 53 51 4c 20 see in the SQL
1a65a 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 statement */. i
1a65b 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 20 20 20 20 nt nMem,
1a65c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a65d 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 Number of memor
1a65e 79 20 63 65 6c 6c 73 20 74 6f 20 61 6c 6c 6f 63 y cells to alloc
1a65f 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 ate */. int nCu
1a660 72 73 6f 72 2c 20 20 20 20 20 20 20 20 20 20 20 rsor,
1a661 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1a662 72 20 6f 66 20 63 75 72 73 6f 72 73 20 74 6f 20 r of cursors to
1a663 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e allocate */. in
1a664 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 20 20 t nArg,
1a665 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a666 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f Maximum number o
1a667 66 20 61 72 67 73 20 69 6e 20 53 75 62 50 72 6f f args in SubPro
1a668 67 72 61 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 69 grams */. int i
1a669 73 45 78 70 6c 61 69 6e 2c 20 20 20 20 20 20 20 sExplain,
1a66a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
1a66b 65 20 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e e if the EXPLAIN
1a66c 20 6b 65 79 77 6f 72 64 73 20 69 73 20 70 72 65 keywords is pre
1a66d 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 75 73 sent */. int us
1a66e 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 20 20 esStmtJournal
1a66f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
1a670 65 20 74 6f 20 73 65 74 20 56 64 62 65 2e 75 73 e to set Vdbe.us
1a671 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 2a 2f esStmtJournal */
1a672 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 .){. int n;. s
1a673 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e qlite3 *db = p->
1a674 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 db;.. assert( p
1a675 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
1a676 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
1a677 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 0a 20 MAGIC_INIT );..
1a678 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 6c 64 /* There should
1a679 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 be at least one
1a67a 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 opcode.. */.
1a67b 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 assert( p->nOp>0
1a67c 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 );.. /* Set th
1a67d 65 20 6d 61 67 69 63 20 74 6f 20 56 44 42 45 5f e magic to VDBE_
1a67e 4d 41 47 49 43 5f 52 55 4e 20 73 6f 6f 6e 65 72 MAGIC_RUN sooner
1a67f 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c 61 74 rather than lat
1a680 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69 er. */. p->magi
1a681 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 c = VDBE_MAGIC_R
1a682 55 4e 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 65 61 UN;.. /* For ea
1a683 63 68 20 63 75 72 73 6f 72 20 72 65 71 75 69 72 ch cursor requir
1a684 65 64 2c 20 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 ed, also allocat
1a685 65 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e e a memory cell.
1a686 20 4d 65 6d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c Memory. ** cel
1a687 6c 73 20 28 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 ls (nMem+1-nCurs
1a688 6f 72 29 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 or)..nMem, inclu
1a689 73 69 76 65 2c 20 77 69 6c 6c 20 6e 65 76 65 72 sive, will never
1a68a 20 62 65 20 75 73 65 64 20 62 79 0a 20 20 2a 2a be used by. **
1a68b 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 the vdbe progra
1a68c 6d 2e 20 49 6e 73 74 65 61 64 20 74 68 65 79 20 m. Instead they
1a68d 61 72 65 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f are used to allo
1a68e 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 0a 20 cate space for.
1a68f 20 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 2f 42 ** VdbeCursor/B
1a690 74 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 tCursor structur
1a691 65 73 2e 20 54 68 65 20 62 6c 6f 62 20 6f 66 20 es. The blob of
1a692 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 memory associate
1a693 64 20 77 69 74 68 20 0a 20 20 2a 2a 20 63 75 72 d with . ** cur
1a694 73 6f 72 20 30 20 69 73 20 73 74 6f 72 65 64 20 sor 0 is stored
1a695 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 6e in memory cell n
1a696 4d 65 6d 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c Mem. Memory cell
1a697 20 28 6e 4d 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 (nMem-1). ** s
1a698 74 6f 72 65 73 20 74 68 65 20 62 6c 6f 62 20 6f tores the blob o
1a699 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 f memory associa
1a69a 74 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 ted with cursor
1a69b 31 2c 20 65 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 1, etc.. **. *
1a69c 2a 20 53 65 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f * See also: allo
1a69d 63 61 74 65 43 75 72 73 6f 72 28 29 2e 0a 20 20 cateCursor()..
1a69e 2a 2f 0a 20 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 */. nMem += nCu
1a69f 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f rsor;.. /* Allo
1a6a0 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d cate space for m
1a6a1 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 2c emory registers,
1a6a2 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73 2c 20 SQL variables,
1a6a3 56 44 42 45 20 63 75 72 73 6f 72 73 20 61 6e 64 VDBE cursors and
1a6a4 20 0a 20 20 2a 2a 20 61 6e 20 61 72 72 61 79 20 . ** an array
1a6a5 74 6f 20 6d 61 72 73 68 61 6c 20 53 51 4c 20 66 to marshal SQL f
1a6a6 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 unction argument
1a6a7 73 20 69 6e 2e 20 54 68 69 73 20 69 73 20 6f 6e s in. This is on
1a6a8 6c 79 20 64 6f 6e 65 20 74 68 65 0a 20 20 2a 2a ly done the. **
1a6a9 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 first time this
1a6aa 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
1a6ab 6c 65 64 20 66 6f 72 20 61 20 67 69 76 65 6e 20 led for a given
1a6ac 56 44 42 45 2c 20 6e 6f 74 20 77 68 65 6e 20 69 VDBE, not when i
1a6ad 74 20 69 73 0a 20 20 2a 2a 20 62 65 69 6e 67 20 t is. ** being
1a6ae 63 61 6c 6c 65 64 20 66 72 6f 6d 20 73 71 6c 69 called from sqli
1a6af 74 65 33 5f 72 65 73 65 74 28 29 20 74 6f 20 72 te3_reset() to r
1a6b0 65 73 65 74 20 74 68 65 20 76 69 72 74 75 61 6c eset the virtual
1a6b1 20 6d 61 63 68 69 6e 65 2e 0a 20 20 2a 2f 0a 20 machine.. */.
1a6b2 20 69 66 28 20 6e 56 61 72 3e 3d 30 20 26 26 20 if( nVar>=0 &&
1a6b3 41 4c 57 41 59 53 28 64 62 2d 3e 6d 61 6c 6c 6f ALWAYS(db->mallo
1a6b4 63 46 61 69 6c 65 64 3d 3d 30 29 20 29 7b 0a 20 cFailed==0) ){.
1a6b5 20 20 20 75 38 20 2a 7a 43 73 72 20 3d 20 28 75 u8 *zCsr = (u
1a6b6 38 20 2a 29 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 8 *)&p->aOp[p->n
1a6b7 4f 70 5d 3b 0a 20 20 20 20 75 38 20 2a 7a 45 6e Op];. u8 *zEn
1a6b8 64 20 3d 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f d = (u8 *)&p->aO
1a6b9 70 5b 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 5d 3b 0a p[p->nOpAlloc];.
1a6ba 20 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 int nByte;.
1a6bb 20 20 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 resolveP2Valu
1a6bc 65 73 28 70 2c 20 26 6e 41 72 67 29 3b 0a 20 20 es(p, &nArg);.
1a6bd 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 p->usesStmtJou
1a6be 72 6e 61 6c 20 3d 20 75 73 65 73 53 74 6d 74 4a rnal = usesStmtJ
1a6bf 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 69 66 28 20 ournal;. if(
1a6c0 69 73 45 78 70 6c 61 69 6e 20 26 26 20 6e 4d 65 isExplain && nMe
1a6c1 6d 3c 31 30 20 29 7b 0a 20 20 20 20 20 20 6e 4d m<10 ){. nM
1a6c2 65 6d 20 3d 20 31 30 3b 0a 20 20 20 20 7d 0a 20 em = 10;. }.
1a6c3 20 20 20 6d 65 6d 73 65 74 28 7a 43 73 72 2c 20 memset(zCsr,
1a6c4 30 2c 20 7a 45 6e 64 2d 7a 43 73 72 29 3b 0a 20 0, zEnd-zCsr);.
1a6c5 20 20 20 7a 43 73 72 20 2b 3d 20 28 7a 43 73 72 zCsr += (zCsr
1a6c6 20 2d 20 28 75 38 2a 29 30 29 26 37 3b 0a 20 20 - (u8*)0)&7;.
1a6c7 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f assert( EIGHT_
1a6c8 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 7a BYTE_ALIGNMENT(z
1a6c9 43 73 72 29 20 29 3b 0a 0a 20 20 20 20 64 6f 20 Csr) );.. do
1a6ca 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 {. nByte =
1a6cb 30 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 0;. allocSp
1a6cc 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 ace((char*)&p->a
1a6cd 4d 65 6d 2c 20 6e 4d 65 6d 2a 73 69 7a 65 6f 66 Mem, nMem*sizeof
1a6ce 28 4d 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45 (Mem), &zCsr, zE
1a6cf 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 nd, &nByte);.
1a6d0 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 allocSpace((c
1a6d1 68 61 72 2a 29 26 70 2d 3e 61 56 61 72 2c 20 6e har*)&p->aVar, n
1a6d2 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c Var*sizeof(Mem),
1a6d3 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e &zCsr, zEnd, &n
1a6d4 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c Byte);. all
1a6d5 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 ocSpace((char*)&
1a6d6 70 2d 3e 61 70 41 72 67 2c 20 6e 41 72 67 2a 73 p->apArg, nArg*s
1a6d7 69 7a 65 6f 66 28 4d 65 6d 2a 29 2c 20 26 7a 43 izeof(Mem*), &zC
1a6d8 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 sr, zEnd, &nByte
1a6d9 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 );. allocSp
1a6da 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 ace((char*)&p->a
1a6db 7a 56 61 72 2c 20 6e 56 61 72 2a 73 69 7a 65 6f zVar, nVar*sizeo
1a6dc 66 28 63 68 61 72 2a 29 2c 20 26 7a 43 73 72 2c f(char*), &zCsr,
1a6dd 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a zEnd, &nByte);.
1a6de 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 allocSpace
1a6df 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 70 43 73 ((char*)&p->apCs
1a6e0 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 r, .
1a6e1 20 20 20 20 20 6e 43 75 72 73 6f 72 2a 73 69 7a nCursor*siz
1a6e2 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 2a 29 eof(VdbeCursor*)
1a6e3 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 , &zCsr, zEnd, &
1a6e4 6e 42 79 74 65 0a 20 20 20 20 20 20 29 3b 0a 20 nByte. );.
1a6e5 20 20 20 20 20 69 66 28 20 6e 42 79 74 65 20 29 if( nByte )
1a6e6 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 46 72 {. p->pFr
1a6e7 65 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ee = sqlite3DbMa
1a6e8 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79 llocZero(db, nBy
1a6e9 74 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 te);. }.
1a6ea 20 20 20 7a 43 73 72 20 3d 20 70 2d 3e 70 46 72 zCsr = p->pFr
1a6eb 65 65 3b 0a 20 20 20 20 20 20 7a 45 6e 64 20 3d ee;. zEnd =
1a6ec 20 26 7a 43 73 72 5b 6e 42 79 74 65 5d 3b 0a 20 &zCsr[nByte];.
1a6ed 20 20 20 7d 77 68 69 6c 65 28 20 6e 42 79 74 65 }while( nByte
1a6ee 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 && !db->mallocF
1a6ef 61 69 6c 65 64 20 29 3b 0a 0a 20 20 20 20 70 2d ailed );.. p-
1a6f0 3e 6e 43 75 72 73 6f 72 20 3d 20 28 75 31 36 29 >nCursor = (u16)
1a6f1 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20 69 66 28 nCursor;. if(
1a6f2 20 70 2d 3e 61 56 61 72 20 29 7b 0a 20 20 20 20 p->aVar ){.
1a6f3 20 20 70 2d 3e 6e 56 61 72 20 3d 20 28 75 31 36 p->nVar = (u16
1a6f4 29 6e 56 61 72 3b 0a 20 20 20 20 20 20 66 6f 72 )nVar;. for
1a6f5 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b 20 6e 2b (n=0; n<nVar; n+
1a6f6 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 +){. p->a
1a6f7 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20 4d Var[n].flags = M
1a6f8 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 EM_Null;.
1a6f9 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64 62 20 3d p->aVar[n].db =
1a6fa 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 db;. }.
1a6fb 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 61 4d }. if( p->aM
1a6fc 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 em ){. p->a
1a6fd 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 20 20 20 20 Mem--;
1a6fe 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 /* a
1a6ff 4d 65 6d 5b 5d 20 67 6f 65 73 20 66 72 6f 6d 20 Mem[] goes from
1a700 31 2e 2e 6e 4d 65 6d 20 2a 2f 0a 20 20 20 20 20 1..nMem */.
1a701 20 70 2d 3e 6e 4d 65 6d 20 3d 20 6e 4d 65 6d 3b p->nMem = nMem;
1a702 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a703 20 2f 2a 20 20 20 20 20 20 20 6e 6f 74 20 66 72 /* not fr
1a704 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20 2a 2f 0a om 0..nMem-1 */.
1a705 20 20 20 20 20 20 66 6f 72 28 6e 3d 31 3b 20 6e for(n=1; n
1a706 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 <=nMem; n++){.
1a707 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d p->aMem[n]
1a708 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c .flags = MEM_Nul
1a709 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d l;. p->aM
1a70a 65 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 em[n].db = db;.
1a70b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1a70c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
1a70d 45 42 55 47 0a 20 20 66 6f 72 28 6e 3d 31 3b 20 EBUG. for(n=1;
1a70e 6e 3c 70 2d 3e 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b n<p->nMem; n++){
1a70f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e . assert( p->
1a710 61 4d 65 6d 5b 6e 5d 2e 64 62 3d 3d 64 62 20 29 aMem[n].db==db )
1a711 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
1a712 70 2d 3e 70 63 20 3d 20 2d 31 3b 0a 20 20 70 2d p->pc = -1;. p-
1a713 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b >rc = SQLITE_OK;
1a714 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f . p->errorActio
1a715 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 n = OE_Abort;.
1a716 70 2d 3e 65 78 70 6c 61 69 6e 20 7c 3d 20 69 73 p->explain |= is
1a717 45 78 70 6c 61 69 6e 3b 0a 20 20 70 2d 3e 6d 61 Explain;. p->ma
1a718 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 gic = VDBE_MAGIC
1a719 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e _RUN;. p->nChan
1a71a 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 63 61 63 ge = 0;. p->cac
1a71b 68 65 43 74 72 20 3d 20 31 3b 0a 20 20 70 2d 3e heCtr = 1;. p->
1a71c 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d minWriteFileForm
1a71d 61 74 20 3d 20 32 35 35 3b 0a 20 20 70 2d 3e 69 at = 255;. p->i
1a71e 53 74 61 74 65 6d 65 6e 74 20 3d 20 30 3b 0a 23 Statement = 0;.#
1a71f 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 ifdef VDBE_PROFI
1a720 4c 45 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 LE. {. int i
1a721 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
1a722 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 <p->nOp; i++){.
1a723 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 p->aOp[i].c
1a724 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d nt = 0;. p-
1a725 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 20 3d >aOp[i].cycles =
1a726 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 0;. }. }.#e
1a727 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c ndif.}../*.** Cl
1a728 6f 73 65 20 61 20 56 44 42 45 20 63 75 72 73 6f ose a VDBE curso
1a729 72 20 61 6e 64 20 72 65 6c 65 61 73 65 20 61 6c r and release al
1a72a 6c 20 74 68 65 20 72 65 73 6f 75 72 63 65 73 20 l the resources
1a72b 74 68 61 74 20 63 75 72 73 6f 72 20 0a 2a 2a 20 that cursor .**
1a72c 68 61 70 70 65 6e 73 20 74 6f 20 68 6f 6c 64 2e happens to hold.
1a72d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a72e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1a72f 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 56 64 dbeFreeCursor(Vd
1a730 62 65 20 2a 70 2c 20 56 64 62 65 43 75 72 73 6f be *p, VdbeCurso
1a731 72 20 2a 70 43 78 29 7b 0a 20 20 69 66 28 20 70 r *pCx){. if( p
1a732 43 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 Cx==0 ){. ret
1a733 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 urn;. }. if( p
1a734 43 78 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 73 Cx->pBt ){. s
1a735 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 qlite3BtreeClose
1a736 28 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 20 20 (pCx->pBt);.
1a737 2f 2a 20 54 68 65 20 70 43 78 2d 3e 70 43 75 72 /* The pCx->pCur
1a738 73 6f 72 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73 sor will be clos
1a739 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2c e automatically,
1a73a 20 69 66 20 69 74 20 65 78 69 73 74 73 2c 20 62 if it exists, b
1a73b 79 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 6c y. ** the cal
1a73c 6c 20 61 62 6f 76 65 2e 20 2a 2f 0a 20 20 7d 65 l above. */. }e
1a73d 6c 73 65 20 69 66 28 20 70 43 78 2d 3e 70 43 75 lse if( pCx->pCu
1a73e 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 rsor ){. sqli
1a73f 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 te3BtreeCloseCur
1a740 73 6f 72 28 70 43 78 2d 3e 70 43 75 72 73 6f 72 sor(pCx->pCursor
1a741 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 );. }.#ifndef S
1a742 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1a743 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 70 43 ALTABLE. if( pC
1a744 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29 x->pVtabCursor )
1a745 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 {. sqlite3_vt
1a746 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 ab_cursor *pVtab
1a747 43 75 72 73 6f 72 20 3d 20 70 43 78 2d 3e 70 56 Cursor = pCx->pV
1a748 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 63 tabCursor;. c
1a749 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 onst sqlite3_mod
1a74a 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70 ule *pModule = p
1a74b 43 78 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 Cx->pModule;.
1a74c 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 p->inVtabMethod
1a74d 20 3d 20 31 3b 0a 20 20 20 20 28 76 6f 69 64 29 = 1;. (void)
1a74e 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 sqlite3SafetyOff
1a74f 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20 70 4d 6f (p->db);. pMo
1a750 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 56 74 dule->xClose(pVt
1a751 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 28 abCursor);. (
1a752 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 void)sqlite3Safe
1a753 74 79 4f 6e 28 70 2d 3e 64 62 29 3b 0a 20 20 20 tyOn(p->db);.
1a754 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 p->inVtabMethod
1a755 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 = 0;. }.#endif
1a756 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 74 .}../*.** Copy t
1a757 68 65 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 he values stored
1a758 20 69 6e 20 74 68 65 20 56 64 62 65 46 72 61 6d in the VdbeFram
1a759 65 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 69 e structure to i
1a75a 74 73 20 56 64 62 65 2e 20 54 68 69 73 0a 2a 2a ts Vdbe. This.**
1a75b 20 69 73 20 75 73 65 64 2c 20 66 6f 72 20 65 78 is used, for ex
1a75c 61 6d 70 6c 65 2c 20 77 68 65 6e 20 61 20 74 72 ample, when a tr
1a75d 69 67 67 65 72 20 73 75 62 2d 70 72 6f 67 72 61 igger sub-progra
1a75e 6d 20 69 73 20 68 61 6c 74 65 64 20 74 6f 20 72 m is halted to r
1a75f 65 73 74 6f 72 65 0a 2a 2a 20 63 6f 6e 74 72 6f estore.** contro
1a760 6c 20 74 6f 20 74 68 65 20 6d 61 69 6e 20 70 72 l to the main pr
1a761 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ogram..*/.SQLITE
1a762 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1a763 69 74 65 33 56 64 62 65 46 72 61 6d 65 52 65 73 ite3VdbeFrameRes
1a764 74 6f 72 65 28 56 64 62 65 46 72 61 6d 65 20 2a tore(VdbeFrame *
1a765 70 46 72 61 6d 65 29 7b 0a 20 20 56 64 62 65 20 pFrame){. Vdbe
1a766 2a 76 20 3d 20 70 46 72 61 6d 65 2d 3e 76 3b 0a *v = pFrame->v;.
1a767 20 20 76 2d 3e 61 4f 70 20 3d 20 70 46 72 61 6d v->aOp = pFram
1a768 65 2d 3e 61 4f 70 3b 0a 20 20 76 2d 3e 6e 4f 70 e->aOp;. v->nOp
1a769 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 4f 70 3b 0a = pFrame->nOp;.
1a76a 20 20 76 2d 3e 61 4d 65 6d 20 3d 20 70 46 72 61 v->aMem = pFra
1a76b 6d 65 2d 3e 61 4d 65 6d 3b 0a 20 20 76 2d 3e 6e me->aMem;. v->n
1a76c 4d 65 6d 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 4d Mem = pFrame->nM
1a76d 65 6d 3b 0a 20 20 76 2d 3e 61 70 43 73 72 20 3d em;. v->apCsr =
1a76e 20 70 46 72 61 6d 65 2d 3e 61 70 43 73 72 3b 0a pFrame->apCsr;.
1a76f 20 20 76 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 70 v->nCursor = p
1a770 46 72 61 6d 65 2d 3e 6e 43 75 72 73 6f 72 3b 0a Frame->nCursor;.
1a771 20 20 76 2d 3e 64 62 2d 3e 6c 61 73 74 52 6f 77 v->db->lastRow
1a772 69 64 20 3d 20 70 46 72 61 6d 65 2d 3e 6c 61 73 id = pFrame->las
1a773 74 52 6f 77 69 64 3b 0a 20 20 76 2d 3e 6e 43 68 tRowid;. v->nCh
1a774 61 6e 67 65 20 3d 20 70 46 72 61 6d 65 2d 3e 6e ange = pFrame->n
1a775 43 68 61 6e 67 65 3b 0a 20 20 72 65 74 75 72 6e Change;. return
1a776 20 70 46 72 61 6d 65 2d 3e 70 63 3b 0a 7d 0a 0a pFrame->pc;.}..
1a777 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 /*.** Close all
1a778 63 75 72 73 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 41 cursors..**.** A
1a779 6c 73 6f 20 72 65 6c 65 61 73 65 20 61 6e 79 20 lso release any
1a77a 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 68 dynamic memory h
1a77b 65 6c 64 20 62 79 20 74 68 65 20 56 4d 20 69 6e eld by the VM in
1a77c 20 74 68 65 20 56 64 62 65 2e 61 4d 65 6d 20 6d the Vdbe.aMem m
1a77d 65 6d 6f 72 79 20 0a 2a 2a 20 63 65 6c 6c 20 61 emory .** cell a
1a77e 72 72 61 79 2e 20 54 68 69 73 20 69 73 20 6e 65 rray. This is ne
1a77f 63 65 73 73 61 72 79 20 61 73 20 74 68 65 20 6d cessary as the m
1a780 65 6d 6f 72 79 20 63 65 6c 6c 20 61 72 72 61 79 emory cell array
1a781 20 6d 61 79 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 may contain.**
1a782 70 6f 69 6e 74 65 72 73 20 74 6f 20 56 64 62 65 pointers to Vdbe
1a783 46 72 61 6d 65 20 6f 62 6a 65 63 74 73 2c 20 77 Frame objects, w
1a784 68 69 63 68 20 6d 61 79 20 69 6e 20 74 75 72 6e hich may in turn
1a785 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74 65 72 contain pointer
1a786 73 20 74 6f 0a 2a 2a 20 6f 70 65 6e 20 63 75 72 s to.** open cur
1a787 73 6f 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 sors..*/.static
1a788 76 6f 69 64 20 63 6c 6f 73 65 41 6c 6c 43 75 72 void closeAllCur
1a789 73 6f 72 73 28 56 64 62 65 20 2a 70 29 7b 0a 20 sors(Vdbe *p){.
1a78a 20 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29 if( p->pFrame )
1a78b 7b 0a 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 {. VdbeFrame
1a78c 2a 70 46 72 61 6d 65 20 3d 20 70 2d 3e 70 46 72 *pFrame = p->pFr
1a78d 61 6d 65 3b 0a 20 20 20 20 66 6f 72 28 70 46 72 ame;. for(pFr
1a78e 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b 20 70 ame=p->pFrame; p
1a78f 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 20 Frame->pParent;
1a790 70 46 72 61 6d 65 3d 70 46 72 61 6d 65 2d 3e 70 pFrame=pFrame->p
1a791 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c Parent);. sql
1a792 69 74 65 33 56 64 62 65 46 72 61 6d 65 52 65 73 ite3VdbeFrameRes
1a793 74 6f 72 65 28 70 46 72 61 6d 65 29 3b 0a 20 20 tore(pFrame);.
1a794 7d 0a 20 20 70 2d 3e 70 46 72 61 6d 65 20 3d 20 }. p->pFrame =
1a795 30 3b 0a 20 20 70 2d 3e 6e 46 72 61 6d 65 20 3d 0;. p->nFrame =
1a796 20 30 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 61 70 0;.. if( p->ap
1a797 43 73 72 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 Csr ){. int i
1a798 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
1a799 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b 20 69 2b 2b <p->nCursor; i++
1a79a 29 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 ){. VdbeCur
1a79b 73 6f 72 20 2a 70 43 20 3d 20 70 2d 3e 61 70 43 sor *pC = p->apC
1a79c 73 72 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 sr[i];. if(
1a79d 20 70 43 20 29 7b 0a 20 20 20 20 20 20 20 20 73 pC ){. s
1a79e 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 qlite3VdbeFreeCu
1a79f 72 73 6f 72 28 70 2c 20 70 43 29 3b 0a 20 20 20 rsor(p, pC);.
1a7a0 20 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d p->apCsr[i]
1a7a1 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 0;. }.
1a7a2 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d }. }. if( p-
1a7a3 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20 72 65 6c >aMem ){. rel
1a7a4 65 61 73 65 4d 65 6d 41 72 72 61 79 28 26 70 2d easeMemArray(&p-
1a7a5 3e 61 4d 65 6d 5b 31 5d 2c 20 70 2d 3e 6e 4d 65 >aMem[1], p->nMe
1a7a6 6d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a m);. }.}../*.**
1a7a7 20 43 6c 65 61 6e 20 75 70 20 74 68 65 20 56 4d Clean up the VM
1a7a8 20 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e after execution
1a7a9 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1a7aa 74 69 6e 65 20 77 69 6c 6c 20 61 75 74 6f 6d 61 tine will automa
1a7ab 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 20 61 6e tically close an
1a7ac 79 20 63 75 72 73 6f 72 73 2c 20 6c 69 73 74 73 y cursors, lists
1a7ad 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73 6f 72 74 , and/or.** sort
1a7ae 65 72 73 20 74 68 61 74 20 77 65 72 65 20 6c 65 ers that were le
1a7af 66 74 20 6f 70 65 6e 2e 20 20 49 74 20 61 6c 73 ft open. It als
1a7b0 6f 20 64 65 6c 65 74 65 73 20 74 68 65 20 76 61 o deletes the va
1a7b1 6c 75 65 73 20 6f 66 0a 2a 2a 20 76 61 72 69 61 lues of.** varia
1a7b2 62 6c 65 73 20 69 6e 20 74 68 65 20 61 56 61 72 bles in the aVar
1a7b3 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f 0a 73 74 61 [] array..*/.sta
1a7b4 74 69 63 20 76 6f 69 64 20 43 6c 65 61 6e 75 70 tic void Cleanup
1a7b5 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c (Vdbe *p){. sql
1a7b6 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 ite3 *db = p->db
1a7b7 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
1a7b8 5f 44 45 42 55 47 0a 20 20 2f 2a 20 45 78 65 63 _DEBUG. /* Exec
1a7b9 75 74 65 20 61 73 73 65 72 74 28 29 20 73 74 61 ute assert() sta
1a7ba 74 65 6d 65 6e 74 73 20 74 6f 20 65 6e 73 75 72 tements to ensur
1a7bb 65 20 74 68 61 74 20 74 68 65 20 56 64 62 65 2e e that the Vdbe.
1a7bc 61 70 43 73 72 5b 5d 20 61 6e 64 20 0a 20 20 2a apCsr[] and . *
1a7bd 2a 20 56 64 62 65 2e 61 4d 65 6d 5b 5d 20 61 72 * Vdbe.aMem[] ar
1a7be 72 61 79 73 20 68 61 76 65 20 61 6c 72 65 61 64 rays have alread
1a7bf 79 20 62 65 65 6e 20 63 6c 65 61 6e 65 64 20 75 y been cleaned u
1a7c0 70 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 0a p. */. int i;.
1a7c1 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e for(i=0; i<p->
1a7c2 6e 43 75 72 73 6f 72 3b 20 69 2b 2b 29 20 61 73 nCursor; i++) as
1a7c3 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 3d 3d sert( p->apCsr==
1a7c4 30 20 7c 7c 20 70 2d 3e 61 70 43 73 72 5b 69 5d 0 || p->apCsr[i]
1a7c5 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d 31 ==0 );. for(i=1
1a7c6 3b 20 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 2b ; i<=p->nMem; i+
1a7c7 2b 29 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4d +) assert( p->aM
1a7c8 65 6d 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4d 65 6d em==0 || p->aMem
1a7c9 5b 69 5d 2e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 4e [i].flags==MEM_N
1a7ca 75 6c 6c 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 ull );.#endif..
1a7cb 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1a7cc 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a b, p->zErrMsg);.
1a7cd 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 p->zErrMsg = 0
1a7ce 3b 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 ;. p->pResultSe
1a7cf 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 t = 0;.}../*.**
1a7d0 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f Set the number o
1a7d1 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 f result columns
1a7d2 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 72 65 that will be re
1a7d3 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 53 turned by this S
1a7d4 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e QL.** statement.
1a7d5 20 54 68 69 73 20 69 73 20 6e 6f 77 20 73 65 74 This is now set
1a7d6 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 at compile time
1a7d7 2c 20 72 61 74 68 65 72 20 74 68 61 6e 20 64 75 , rather than du
1a7d8 72 69 6e 67 0a 2a 2a 20 65 78 65 63 75 74 69 6f ring.** executio
1a7d9 6e 20 6f 66 20 74 68 65 20 76 64 62 65 20 70 72 n of the vdbe pr
1a7da 6f 67 72 61 6d 20 73 6f 20 74 68 61 74 20 73 71 ogram so that sq
1a7db 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 lite3_column_cou
1a7dc 6e 74 28 29 20 63 61 6e 0a 2a 2a 20 62 65 20 63 nt() can.** be c
1a7dd 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 53 51 4c 20 alled on an SQL
1a7de 73 74 61 74 65 6d 65 6e 74 20 62 65 66 6f 72 65 statement before
1a7df 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e sqlite3_step().
1a7e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a7e1 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1a7e2 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 56 64 dbeSetNumCols(Vd
1a7e3 62 65 20 2a 70 2c 20 69 6e 74 20 6e 52 65 73 43 be *p, int nResC
1a7e4 6f 6c 75 6d 6e 29 7b 0a 20 20 4d 65 6d 20 2a 70 olumn){. Mem *p
1a7e5 43 6f 6c 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e ColName;. int n
1a7e6 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
1a7e7 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 72 65 6c 65 = p->db;.. rele
1a7e8 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 aseMemArray(p->a
1a7e9 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 ColName, p->nRes
1a7ea 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e Column*COLNAME_N
1a7eb 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
1a7ec 65 65 28 64 62 2c 20 70 2d 3e 61 43 6f 6c 4e 61 ee(db, p->aColNa
1a7ed 6d 65 29 3b 0a 20 20 6e 20 3d 20 6e 52 65 73 43 me);. n = nResC
1a7ee 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 3b olumn*COLNAME_N;
1a7ef 0a 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e . p->nResColumn
1a7f0 20 3d 20 28 75 31 36 29 6e 52 65 73 43 6f 6c 75 = (u16)nResColu
1a7f1 6d 6e 3b 0a 20 20 70 2d 3e 61 43 6f 6c 4e 61 6d mn;. p->aColNam
1a7f2 65 20 3d 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 28 e = pColName = (
1a7f3 4d 65 6d 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 Mem*)sqlite3DbMa
1a7f4 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a llocZero(db, siz
1a7f5 65 6f 66 28 4d 65 6d 29 2a 6e 20 29 3b 0a 20 20 eof(Mem)*n );.
1a7f6 69 66 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 3d if( p->aColName=
1a7f7 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 77 =0 ) return;. w
1a7f8 68 69 6c 65 28 20 6e 2d 2d 20 3e 20 30 20 29 7b hile( n-- > 0 ){
1a7f9 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 66 . pColName->f
1a7fa 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b lags = MEM_Null;
1a7fb 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 64 . pColName->d
1a7fc 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 70 b = p->db;. p
1a7fd 43 6f 6c 4e 61 6d 65 2b 2b 3b 0a 20 20 7d 0a 7d ColName++;. }.}
1a7fe 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
1a7ff 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 64 78 27 name of the idx'
1a800 74 68 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 th column to be
1a801 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 returned by the
1a802 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a SQL statement..*
1a803 2a 20 7a 4e 61 6d 65 20 6d 75 73 74 20 62 65 20 * zName must be
1a804 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6e a pointer to a n
1a805 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 20 73 74 ul terminated st
1a806 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ring..**.** This
1a807 20 63 61 6c 6c 20 6d 75 73 74 20 62 65 20 6d 61 call must be ma
1a808 64 65 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20 de after a call
1a809 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 to sqlite3VdbeSe
1a80a 74 4e 75 6d 43 6f 6c 73 28 29 2e 0a 2a 2a 0a 2a tNumCols()..**.*
1a80b 2a 20 54 68 65 20 66 69 6e 61 6c 20 70 61 72 61 * The final para
1a80c 6d 65 74 65 72 2c 20 78 44 65 6c 2c 20 6d 75 73 meter, xDel, mus
1a80d 74 20 62 65 20 6f 6e 65 20 6f 66 20 53 51 4c 49 t be one of SQLI
1a80e 54 45 5f 44 59 4e 41 4d 49 43 2c 20 53 51 4c 49 TE_DYNAMIC, SQLI
1a80f 54 45 5f 53 54 41 54 49 43 0a 2a 2a 20 6f 72 20 TE_STATIC.** or
1a810 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
1a811 2e 20 49 66 20 69 74 20 69 73 20 53 51 4c 49 54 . If it is SQLIT
1a812 45 5f 44 59 4e 41 4d 49 43 2c 20 74 68 65 6e 20 E_DYNAMIC, then
1a813 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 the buffer point
1a814 65 64 0a 2a 2a 20 74 6f 20 62 79 20 7a 4e 61 6d ed.** to by zNam
1a815 65 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 e will be freed
1a816 62 79 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 by sqlite3DbFree
1a817 28 29 20 77 68 65 6e 20 74 68 65 20 76 64 62 65 () when the vdbe
1a818 20 69 73 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a is destroyed..*
1a819 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1a81a 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1a81b 53 65 74 43 6f 6c 4e 61 6d 65 28 0a 20 20 56 64 SetColName(. Vd
1a81c 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 be *p,
1a81d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a81e 2a 20 56 64 62 65 20 62 65 69 6e 67 20 63 6f 6e * Vdbe being con
1a81f 66 69 67 75 72 65 64 20 2a 2f 0a 20 20 69 6e 74 figured */. int
1a820 20 69 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 idx,
1a821 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a822 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e Index of column
1a823 20 7a 4e 61 6d 65 20 61 70 70 6c 69 65 73 20 74 zName applies t
1a824 6f 20 2a 2f 0a 20 20 69 6e 74 20 76 61 72 2c 20 o */. int var,
1a825 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a826 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f /* One o
1a827 66 20 74 68 65 20 43 4f 4c 4e 41 4d 45 5f 2a 20 f the COLNAME_*
1a828 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 20 20 63 constants */. c
1a829 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
1a82a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1a82b 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 /* Pointer to bu
1a82c 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ffer containing
1a82d 6e 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 name */. void (
1a82e 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 *xDel)(void*)
1a82f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 /* Me
1a830 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20 mory management
1a831 73 74 72 61 74 65 67 79 20 66 6f 72 20 7a 4e 61 strategy for zNa
1a832 6d 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 me */.){. int r
1a833 63 3b 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 c;. Mem *pColNa
1a834 6d 65 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 me;. assert( id
1a835 78 3c 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 x<p->nResColumn
1a836 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 61 72 );. assert( var
1a837 3c 43 4f 4c 4e 41 4d 45 5f 4e 20 29 3b 0a 20 20 <COLNAME_N );.
1a838 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f if( p->db->mallo
1a839 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 61 cFailed ){. a
1a83a 73 73 65 72 74 28 20 21 7a 4e 61 6d 65 20 7c 7c ssert( !zName ||
1a83b 20 78 44 65 6c 21 3d 53 51 4c 49 54 45 5f 44 59 xDel!=SQLITE_DY
1a83c 4e 41 4d 49 43 20 29 3b 0a 20 20 20 20 72 65 74 NAMIC );. ret
1a83d 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1a83e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
1a83f 70 2d 3e 61 43 6f 6c 4e 61 6d 65 21 3d 30 20 29 p->aColName!=0 )
1a840 3b 0a 20 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 26 ;. pColName = &
1a841 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 69 64 78 (p->aColName[idx
1a842 2b 76 61 72 2a 70 2d 3e 6e 52 65 73 43 6f 6c 75 +var*p->nResColu
1a843 6d 6e 5d 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c mn]);. rc = sql
1a844 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
1a845 72 28 70 43 6f 6c 4e 61 6d 65 2c 20 7a 4e 61 6d r(pColName, zNam
1a846 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 e, -1, SQLITE_UT
1a847 46 38 2c 20 78 44 65 6c 29 3b 0a 20 20 61 73 73 F8, xDel);. ass
1a848 65 72 74 28 20 72 63 21 3d 30 20 7c 7c 20 21 7a ert( rc!=0 || !z
1a849 4e 61 6d 65 20 7c 7c 20 28 70 43 6f 6c 4e 61 6d Name || (pColNam
1a84a 65 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 54 65 72 e->flags&MEM_Ter
1a84b 6d 29 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 m)!=0 );. retur
1a84c 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 n rc;.}../*.** A
1a84d 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 read or write t
1a84e 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 6f ransaction may o
1a84f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 63 74 r may not be act
1a850 69 76 65 20 6f 6e 20 64 61 74 61 62 61 73 65 20 ive on database
1a851 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20 49 66 handle.** db. If
1a852 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
1a853 73 20 61 63 74 69 76 65 2c 20 63 6f 6d 6d 69 74 s active, commit
1a854 20 69 74 2e 20 49 66 20 74 68 65 72 65 20 69 73 it. If there is
1a855 20 61 0a 2a 2a 20 77 72 69 74 65 2d 74 72 61 6e a.** write-tran
1a856 73 61 63 74 69 6f 6e 20 73 70 61 6e 6e 69 6e 67 saction spanning
1a857 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 more than one d
1a858 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 atabase file, th
1a859 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 74 61 is routine.** ta
1a85a 6b 65 73 20 63 61 72 65 20 6f 66 20 74 68 65 20 kes care of the
1a85b 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 74 master journal t
1a85c 72 69 63 6b 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 rickery..*/.stat
1a85d 69 63 20 69 6e 74 20 76 64 62 65 43 6f 6d 6d 69 ic int vdbeCommi
1a85e 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56 t(sqlite3 *db, V
1a85f 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 dbe *p){. int i
1a860 3b 0a 20 20 69 6e 74 20 6e 54 72 61 6e 73 20 3d ;. int nTrans =
1a861 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 0; /* Number o
1a862 66 20 64 61 74 61 62 61 73 65 73 20 77 69 74 68 f databases with
1a863 20 61 6e 20 61 63 74 69 76 65 20 77 72 69 74 65 an active write
1a864 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a -transaction */.
1a865 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1a866 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 65 65 64 E_OK;. int need
1a867 58 63 6f 6d 6d 69 74 20 3d 20 30 3b 0a 0a 23 69 Xcommit = 0;..#i
1a868 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
1a869 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
1a86a 2f 2a 20 57 69 74 68 20 74 68 69 73 20 6f 70 74 /* With this opt
1a86b 69 6f 6e 2c 20 73 71 6c 69 74 65 33 56 74 61 62 ion, sqlite3Vtab
1a86c 53 79 6e 63 28 29 20 69 73 20 64 65 66 69 6e 65 Sync() is define
1a86d 64 20 74 6f 20 62 65 20 73 69 6d 70 6c 79 20 0a d to be simply .
1a86e 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 73 ** SQLITE_OK s
1a86f 6f 20 70 20 69 73 20 6e 6f 74 20 75 73 65 64 2e o p is not used.
1a870 20 0a 20 20 2a 2f 0a 20 20 55 4e 55 53 45 44 5f . */. UNUSED_
1a871 50 41 52 41 4d 45 54 45 52 28 70 29 3b 0a 23 65 PARAMETER(p);.#e
1a872 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 66 6f 72 ndif.. /* Befor
1a873 65 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 e doing anything
1a874 20 65 6c 73 65 2c 20 63 61 6c 6c 20 74 68 65 20 else, call the
1a875 78 53 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b xSync() callback
1a876 20 66 6f 72 20 61 6e 79 0a 20 20 2a 2a 20 76 69 for any. ** vi
1a877 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20 74 61 62 rtual module tab
1a878 6c 65 73 20 77 72 69 74 74 65 6e 20 69 6e 20 74 les written in t
1a879 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e his transaction.
1a87a 20 54 68 69 73 20 68 61 73 20 74 6f 0a 20 20 2a This has to. *
1a87b 2a 20 62 65 20 64 6f 6e 65 20 62 65 66 6f 72 65 * be done before
1a87c 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 65 determining whe
1a87d 74 68 65 72 20 61 20 6d 61 73 74 65 72 20 6a 6f ther a master jo
1a87e 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 0a 20 urnal file is .
1a87f 20 2a 2a 20 72 65 71 75 69 72 65 64 2c 20 61 73 ** required, as
1a880 20 61 6e 20 78 53 79 6e 63 28 29 20 63 61 6c 6c an xSync() call
1a881 62 61 63 6b 20 6d 61 79 20 61 64 64 20 61 6e 20 back may add an
1a882 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 attached databas
1a883 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 72 e. ** to the tr
1a884 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a ansaction.. */.
1a885 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 rc = sqlite3Vt
1a886 61 62 53 79 6e 63 28 64 62 2c 20 26 70 2d 3e 7a abSync(db, &p->z
1a887 45 72 72 4d 73 67 29 3b 0a 20 20 69 66 28 20 72 ErrMsg);. if( r
1a888 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1a889 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1a88a 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f }.. /* This lo
1a88b 6f 70 20 64 65 74 65 72 6d 69 6e 65 73 20 28 61 op determines (a
1a88c 29 20 69 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 ) if the commit
1a88d 68 6f 6f 6b 20 73 68 6f 75 6c 64 20 62 65 20 69 hook should be i
1a88e 6e 76 6f 6b 65 64 20 61 6e 64 0a 20 20 2a 2a 20 nvoked and. **
1a88f 28 62 29 20 68 6f 77 20 6d 61 6e 79 20 64 61 74 (b) how many dat
1a890 61 62 61 73 65 20 66 69 6c 65 73 20 68 61 76 65 abase files have
1a891 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e open write tran
1a892 73 61 63 74 69 6f 6e 73 2c 20 6e 6f 74 20 0a 20 sactions, not .
1a893 20 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 ** including th
1a894 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e e temp database.
1a895 20 28 62 29 20 69 73 20 69 6d 70 6f 72 74 61 6e (b) is importan
1a896 74 20 62 65 63 61 75 73 65 20 69 66 20 6d 6f 72 t because if mor
1a897 65 20 74 68 61 6e 20 0a 20 20 2a 2a 20 6f 6e 65 e than . ** one
1a898 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 database file h
1a899 61 73 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 as an open write
1a89a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20 transaction, a
1a89b 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 20 master journal.
1a89c 20 2a 2a 20 66 69 6c 65 20 69 73 20 72 65 71 75 ** file is requ
1a89d 69 72 65 64 20 66 6f 72 20 61 6e 20 61 74 6f 6d ired for an atom
1a89e 69 63 20 63 6f 6d 6d 69 74 2e 0a 20 20 2a 2f 20 ic commit.. */
1a89f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 . for(i=0; i<db
1a8a0 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 ->nDb; i++){ .
1a8a1 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 Btree *pBt = d
1a8a2 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 b->aDb[i].pBt;.
1a8a3 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 if( sqlite3Bt
1a8a4 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74 reeIsInTrans(pBt
1a8a5 29 20 29 7b 0a 20 20 20 20 20 20 6e 65 65 64 58 ) ){. needX
1a8a6 63 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 commit = 1;.
1a8a7 20 20 69 66 28 20 69 21 3d 31 20 29 20 6e 54 72 if( i!=1 ) nTr
1a8a8 61 6e 73 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d ans++;. }. }
1a8a9 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 .. /* If there
1a8aa 61 72 65 20 61 6e 79 20 77 72 69 74 65 2d 74 72 are any write-tr
1a8ab 61 6e 73 61 63 74 69 6f 6e 73 20 61 74 20 61 6c ansactions at al
1a8ac 6c 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f l, invoke the co
1a8ad 6d 6d 69 74 20 68 6f 6f 6b 20 2a 2f 0a 20 20 69 mmit hook */. i
1a8ae 66 28 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 26 f( needXcommit &
1a8af 26 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c & db->xCommitCal
1a8b0 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 28 76 6f lback ){. (vo
1a8b1 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 id)sqlite3Safety
1a8b2 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 72 63 20 Off(db);. rc
1a8b3 3d 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c = db->xCommitCal
1a8b4 6c 62 61 63 6b 28 64 62 2d 3e 70 43 6f 6d 6d 69 lback(db->pCommi
1a8b5 74 41 72 67 29 3b 0a 20 20 20 20 28 76 6f 69 64 tArg);. (void
1a8b6 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e )sqlite3SafetyOn
1a8b7 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63 (db);. if( rc
1a8b8 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1a8b9 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 SQLITE_CONSTRAI
1a8ba 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 NT;. }. }..
1a8bb 20 2f 2a 20 54 68 65 20 73 69 6d 70 6c 65 20 63 /* The simple c
1a8bc 61 73 65 20 2d 20 6e 6f 20 6d 6f 72 65 20 74 68 ase - no more th
1a8bd 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 an one database
1a8be 66 69 6c 65 20 28 6e 6f 74 20 63 6f 75 6e 74 69 file (not counti
1a8bf 6e 67 20 74 68 65 0a 20 20 2a 2a 20 54 45 4d 50 ng the. ** TEMP
1a8c0 20 64 61 74 61 62 61 73 65 29 20 68 61 73 20 61 database) has a
1a8c1 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 transaction act
1a8c2 69 76 65 2e 20 20 20 54 68 65 72 65 20 69 73 20 ive. There is
1a8c3 6e 6f 20 6e 65 65 64 20 66 6f 72 20 74 68 65 0a no need for the.
1a8c4 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 72 ** master-jour
1a8c5 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 nal.. **. ** I
1a8c6 66 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c f the return val
1a8c7 75 65 20 6f 66 20 73 71 6c 69 74 65 33 42 74 72 ue of sqlite3Btr
1a8c8 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 29 20 eeGetFilename()
1a8c9 69 73 20 61 20 7a 65 72 6f 20 6c 65 6e 67 74 68 is a zero length
1a8ca 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2c 20 69 74 . ** string, it
1a8cb 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20 means the main
1a8cc 64 61 74 61 62 61 73 65 20 69 73 20 3a 6d 65 6d database is :mem
1a8cd 6f 72 79 3a 20 6f 72 20 61 20 74 65 6d 70 20 66 ory: or a temp f
1a8ce 69 6c 65 2e 20 20 49 6e 20 0a 20 20 2a 2a 20 74 ile. In . ** t
1a8cf 68 61 74 20 63 61 73 65 20 77 65 20 64 6f 20 6e hat case we do n
1a8d0 6f 74 20 73 75 70 70 6f 72 74 20 61 74 6f 6d 69 ot support atomi
1a8d1 63 20 6d 75 6c 74 69 2d 66 69 6c 65 20 63 6f 6d c multi-file com
1a8d2 6d 69 74 73 2c 20 73 6f 20 75 73 65 20 74 68 65 mits, so use the
1a8d3 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20 63 61 . ** simple ca
1a8d4 73 65 20 74 68 65 6e 20 74 6f 6f 2e 0a 20 20 2a se then too.. *
1a8d5 2f 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 /. if( 0==sqlit
1a8d6 65 33 53 74 72 6c 65 6e 33 30 28 73 71 6c 69 74 e3Strlen30(sqlit
1a8d7 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 e3BtreeGetFilena
1a8d8 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 me(db->aDb[0].pB
1a8d9 74 29 29 0a 20 20 20 7c 7c 20 6e 54 72 61 6e 73 t)). || nTrans
1a8da 3c 3d 31 0a 20 20 29 7b 0a 20 20 20 20 66 6f 72 <=1. ){. for
1a8db 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 (i=0; rc==SQLITE
1a8dc 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 _OK && i<db->nDb
1a8dd 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 ; i++){. Bt
1a8de 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 ree *pBt = db->a
1a8df 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 Db[i].pBt;.
1a8e0 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 if( pBt ){.
1a8e1 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1a8e2 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
1a8e3 4f 6e 65 28 70 42 74 2c 20 30 29 3b 0a 20 20 20 One(pBt, 0);.
1a8e4 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
1a8e5 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 6d 69 74 /* Do the commit
1a8e6 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20 64 61 74 only if all dat
1a8e7 61 62 61 73 65 73 20 73 75 63 63 65 73 73 66 75 abases successfu
1a8e8 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20 70 68 61 lly complete pha
1a8e9 73 65 20 31 2e 20 0a 20 20 20 20 2a 2a 20 49 66 se 1. . ** If
1a8ea 20 6f 6e 65 20 6f 66 20 74 68 65 20 42 74 72 65 one of the Btre
1a8eb 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 eCommitPhaseOne(
1a8ec 29 20 63 61 6c 6c 73 20 66 61 69 6c 73 2c 20 74 ) calls fails, t
1a8ed 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 61 6e his indicates an
1a8ee 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72 72 6f 72 . ** IO error
1a8ef 20 77 68 69 6c 65 20 64 65 6c 65 74 69 6e 67 20 while deleting
1a8f0 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 61 20 or truncating a
1a8f1 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 74 journal file. It
1a8f2 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c 0a 20 20 is unlikely,.
1a8f3 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c 64 20 68 ** but could h
1a8f4 61 70 70 65 6e 2e 20 49 6e 20 74 68 69 73 20 63 appen. In this c
1a8f5 61 73 65 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63 ase abandon proc
1a8f6 65 73 73 69 6e 67 20 61 6e 64 20 72 65 74 75 72 essing and retur
1a8f7 6e 20 74 68 65 20 65 72 72 6f 72 2e 0a 20 20 20 n the error..
1a8f8 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b */. for(i=0;
1a8f9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
1a8fa 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b & i<db->nDb; i++
1a8fb 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a ){. Btree *
1a8fc 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d pBt = db->aDb[i]
1a8fd 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 .pBt;. if(
1a8fe 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 pBt ){. r
1a8ff 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1a900 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 CommitPhaseTwo(p
1a901 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 Bt);. }.
1a902 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 }. if( rc==S
1a903 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1a904 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d sqlite3VtabCom
1a905 6d 69 74 28 64 62 29 3b 0a 20 20 20 20 7d 0a 20 mit(db);. }.
1a906 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 63 6f 6d }.. /* The com
1a907 70 6c 65 78 20 63 61 73 65 20 2d 20 54 68 65 72 plex case - Ther
1a908 65 20 69 73 20 61 20 6d 75 6c 74 69 2d 66 69 6c e is a multi-fil
1a909 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 e write-transact
1a90a 69 6f 6e 20 61 63 74 69 76 65 2e 0a 20 20 2a 2a ion active.. **
1a90b 20 54 68 69 73 20 72 65 71 75 69 72 65 73 20 61 This requires a
1a90c 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1a90d 66 69 6c 65 20 74 6f 20 65 6e 73 75 72 65 20 74 file to ensure t
1a90e 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 he transaction i
1a90f 73 0a 20 20 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 s. ** committed
1a910 20 61 74 6f 6d 69 63 6c 79 2e 0a 20 20 2a 2f 0a atomicly.. */.
1a911 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1a912 4d 49 54 5f 44 49 53 4b 49 4f 0a 20 20 65 6c 73 MIT_DISKIO. els
1a913 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 e{. sqlite3_v
1a914 66 73 20 2a 70 56 66 73 20 3d 20 64 62 2d 3e 70 fs *pVfs = db->p
1a915 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 6e 65 65 Vfs;. int nee
1a916 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 63 dSync = 0;. c
1a917 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30 har *zMaster = 0
1a918 3b 20 20 20 2f 2a 20 46 69 6c 65 2d 6e 61 6d 65 ; /* File-name
1a919 20 66 6f 72 20 74 68 65 20 6d 61 73 74 65 72 20 for the master
1a91a 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 63 journal */. c
1a91b 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 69 6e har const *zMain
1a91c 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 42 74 File = sqlite3Bt
1a91d 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64 reeGetFilename(d
1a91e 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b 0a b->aDb[0].pBt);.
1a91f 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 sqlite3_file
1a920 20 2a 70 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 *pMaster = 0;.
1a921 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 i64 offset =
1a922 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 0;. int res;.
1a923 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 61 . /* Select a
1a924 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1a925 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 file name */.
1a926 20 64 6f 20 7b 0a 20 20 20 20 20 20 75 33 32 20 do {. u32
1a927 69 52 61 6e 64 6f 6d 3b 0a 20 20 20 20 20 20 73 iRandom;. s
1a928 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1a929 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 zMaster);.
1a92a 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e sqlite3_randomn
1a92b 65 73 73 28 73 69 7a 65 6f 66 28 69 52 61 6e 64 ess(sizeof(iRand
1a92c 6f 6d 29 2c 20 26 69 52 61 6e 64 6f 6d 29 3b 0a om), &iRandom);.
1a92d 20 20 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 zMaster =
1a92e 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
1a92f 62 2c 20 22 25 73 2d 6d 6a 25 30 38 58 22 2c 20 b, "%s-mj%08X",
1a930 7a 4d 61 69 6e 46 69 6c 65 2c 20 69 52 61 6e 64 zMainFile, iRand
1a931 6f 6d 26 30 78 37 66 66 66 66 66 66 66 29 3b 0a om&0x7fffffff);.
1a932 20 20 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 if( !zMast
1a933 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 er ){. re
1a934 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
1a935 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 M;. }.
1a936 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 rc = sqlite3OsA
1a937 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 ccess(pVfs, zMas
1a938 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 ter, SQLITE_ACCE
1a939 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 SS_EXISTS, &res)
1a93a 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 ;. }while( rc
1a93b 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 ==SQLITE_OK && r
1a93c 65 73 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 es );. if( rc
1a93d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1a93e 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 /* Open the
1a93f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e master journal.
1a940 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 */. rc = s
1a941 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c qlite3OsOpenMall
1a942 6f 63 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 oc(pVfs, zMaster
1a943 2c 20 26 70 4d 61 73 74 65 72 2c 20 0a 20 20 20 , &pMaster, .
1a944 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 SQLITE_OP
1a945 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c EN_READWRITE|SQL
1a946 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c ITE_OPEN_CREATE|
1a947 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 . SQLIT
1a948 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 E_OPEN_EXCLUSIVE
1a949 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 |SQLITE_OPEN_MAS
1a94a 54 45 52 5f 4a 4f 55 52 4e 41 4c 2c 20 30 0a 20 TER_JOURNAL, 0.
1a94b 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 );. }.
1a94c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1a94d 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
1a94e 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
1a94f 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72 Master);. r
1a950 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
1a951 20 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 . /* Write t
1a952 68 65 20 6e 61 6d 65 20 6f 66 20 65 61 63 68 20 he name of each
1a953 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e database file in
1a954 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
1a955 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20 20 into the new.
1a956 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 ** master jour
1a957 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 6e 20 nal file. If an
1a958 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 74 20 error occurs at
1a959 74 68 69 73 20 70 6f 69 6e 74 20 63 6c 6f 73 65 this point close
1a95a 0a 20 20 20 20 2a 2a 20 61 6e 64 20 64 65 6c 65 . ** and dele
1a95b 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f te the master jo
1a95c 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 6c 6c 20 urnal file. All
1a95d 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 6a the individual j
1a95e 6f 75 72 6e 61 6c 20 66 69 6c 65 73 0a 20 20 20 ournal files.
1a95f 20 2a 2a 20 73 74 69 6c 6c 20 68 61 76 65 20 27 ** still have '
1a960 6e 75 6c 6c 27 20 61 73 20 74 68 65 20 6d 61 73 null' as the mas
1a961 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e ter journal poin
1a962 74 65 72 2c 20 73 6f 20 74 68 65 79 20 77 69 6c ter, so they wil
1a963 6c 20 72 6f 6c 6c 0a 20 20 20 20 2a 2a 20 62 61 l roll. ** ba
1a964 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 ck independently
1a965 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 if a failure oc
1a966 63 75 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 curs.. */.
1a967 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e for(i=0; i<db->
1a968 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 nDb; i++){.
1a969 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 Btree *pBt = db
1a96a 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 ->aDb[i].pBt;.
1a96b 20 20 20 20 69 66 28 20 69 3d 3d 31 20 29 20 63 if( i==1 ) c
1a96c 6f 6e 74 69 6e 75 65 3b 20 20 20 2f 2a 20 49 67 ontinue; /* Ig
1a96d 6e 6f 72 65 20 74 68 65 20 54 45 4d 50 20 64 61 nore the TEMP da
1a96e 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 tabase */.
1a96f 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 if( sqlite3Btree
1a970 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29 20 29 IsInTrans(pBt) )
1a971 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 63 {. char c
1a972 6f 6e 73 74 20 2a 7a 46 69 6c 65 20 3d 20 73 71 onst *zFile = sq
1a973 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 lite3BtreeGetJou
1a974 72 6e 61 6c 6e 61 6d 65 28 70 42 74 29 3b 0a 20 rnalname(pBt);.
1a975 20 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65 if( zFile
1a976 5b 30 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 [0]==0 ) continu
1a977 65 3b 20 20 2f 2a 20 49 67 6e 6f 72 65 20 3a 6d e; /* Ignore :m
1a978 65 6d 6f 72 79 3a 20 64 61 74 61 62 61 73 65 73 emory: databases
1a979 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
1a97a 21 6e 65 65 64 53 79 6e 63 20 26 26 20 21 73 71 !needSync && !sq
1a97b 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 lite3BtreeSyncDi
1a97c 73 61 62 6c 65 64 28 70 42 74 29 20 29 7b 0a 20 sabled(pBt) ){.
1a97d 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e needSyn
1a97e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d c = 1;. }
1a97f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
1a980 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 4d 61 lite3OsWrite(pMa
1a981 73 74 65 72 2c 20 7a 46 69 6c 65 2c 20 73 71 6c ster, zFile, sql
1a982 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 46 69 ite3Strlen30(zFi
1a983 6c 65 29 2b 31 2c 20 6f 66 66 73 65 74 29 3b 0a le)+1, offset);.
1a984 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 2b offset +
1a985 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
1a986 30 28 7a 46 69 6c 65 29 2b 31 3b 0a 20 20 20 20 0(zFile)+1;.
1a987 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1a988 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1a989 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 sqlite3OsClos
1a98a 65 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a eFree(pMaster);.
1a98b 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1a98c 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 3OsDelete(pVfs,
1a98d 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 zMaster, 0);.
1a98e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
1a98f 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 Free(db, zMaster
1a990 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 );. ret
1a991 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 urn rc;.
1a992 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
1a993 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 . /* Sync the
1a994 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1a995 66 69 6c 65 2e 20 49 66 20 74 68 65 20 49 4f 43 file. If the IOC
1a996 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 64 65 AP_SEQUENTIAL de
1a997 76 69 63 65 0a 20 20 20 20 2a 2a 20 66 6c 61 67 vice. ** flag
1a998 20 69 73 20 73 65 74 20 74 68 69 73 20 69 73 20 is set this is
1a999 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 0a 20 20 not required..
1a99a 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65 65 */. if( nee
1a99b 64 53 79 6e 63 20 0a 20 20 20 20 20 26 26 20 30 dSync . && 0
1a99c 3d 3d 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69 ==(sqlite3OsDevi
1a99d 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
1a99e 73 28 70 4d 61 73 74 65 72 29 26 53 51 4c 49 54 s(pMaster)&SQLIT
1a99f 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 E_IOCAP_SEQUENTI
1a9a0 41 4c 29 0a 20 20 20 20 20 26 26 20 53 51 4c 49 AL). && SQLI
1a9a1 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c TE_OK!=(rc = sql
1a9a2 69 74 65 33 4f 73 53 79 6e 63 28 70 4d 61 73 74 ite3OsSync(pMast
1a9a3 65 72 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f er, SQLITE_SYNC_
1a9a4 4e 4f 52 4d 41 4c 29 29 0a 20 20 20 20 29 7b 0a NORMAL)). ){.
1a9a5 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 sqlite3OsC
1a9a6 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 loseFree(pMaster
1a9a7 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1a9a8 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a OsDelete(pVfs, z
1a9a9 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 Master, 0);.
1a9aa 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1a9ab 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 db, zMaster);.
1a9ac 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1a9ad 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 6e }.. /* Syn
1a9ae 63 20 61 6c 6c 20 74 68 65 20 64 62 20 66 69 6c c all the db fil
1a9af 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74 es involved in t
1a9b0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 he transaction.
1a9b1 54 68 65 20 73 61 6d 65 20 63 61 6c 6c 0a 20 20 The same call.
1a9b2 20 20 2a 2a 20 73 65 74 73 20 74 68 65 20 6d 61 ** sets the ma
1a9b3 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 ster journal poi
1a9b4 6e 74 65 72 20 69 6e 20 65 61 63 68 20 69 6e 64 nter in each ind
1a9b5 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 2e ividual journal.
1a9b6 20 49 66 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72 If. ** an er
1a9b7 72 6f 72 20 6f 63 63 75 72 73 20 68 65 72 65 2c ror occurs here,
1a9b8 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 do not delete t
1a9b9 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
1a9ba 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 l file.. **.
1a9bb 20 20 20 2a 2a 20 49 66 20 74 68 65 20 65 72 72 ** If the err
1a9bc 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
1a9bd 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 the first call
1a9be 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 to. ** sqlite
1a9bf 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
1a9c0 65 4f 6e 65 28 29 2c 20 74 68 65 6e 20 74 68 65 eOne(), then the
1a9c1 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 re is a chance t
1a9c2 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 6d hat the. ** m
1a9c3 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1a9c4 6c 65 20 77 69 6c 6c 20 62 65 20 6f 72 70 68 61 le will be orpha
1a9c5 6e 65 64 2e 20 42 75 74 20 77 65 20 63 61 6e 6e ned. But we cann
1a9c6 6f 74 20 64 65 6c 65 74 65 20 69 74 2c 0a 20 20 ot delete it,.
1a9c7 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 65 ** in case the
1a9c8 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1a9c9 66 69 6c 65 20 6e 61 6d 65 20 77 61 73 20 77 72 file name was wr
1a9ca 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a itten into the j
1a9cb 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 ournal. ** fi
1a9cc 6c 65 20 62 65 66 6f 72 65 20 74 68 65 20 66 61 le before the fa
1a9cd 69 6c 75 72 65 20 6f 63 63 75 72 72 65 64 2e 0a ilure occurred..
1a9ce 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 */. for(i
1a9cf 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f =0; rc==SQLITE_O
1a9d0 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 K && i<db->nDb;
1a9d1 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 72 i++){ . Btr
1a9d2 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 ee *pBt = db->aD
1a9d3 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 b[i].pBt;.
1a9d4 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 if( pBt ){.
1a9d5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1a9d6 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f treeCommitPhaseO
1a9d7 6e 65 28 70 42 74 2c 20 7a 4d 61 73 74 65 72 29 ne(pBt, zMaster)
1a9d8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1a9d9 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
1a9da 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b seFree(pMaster);
1a9db 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1a9dc 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1a9dd 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1a9de 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 , zMaster);.
1a9df 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
1a9e0 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 }.. /* Delet
1a9e1 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 e the master jou
1a9e2 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 rnal file. This
1a9e3 63 6f 6d 6d 69 74 73 20 74 68 65 20 74 72 61 6e commits the tran
1a9e4 73 61 63 74 69 6f 6e 2e 20 41 66 74 65 72 0a 20 saction. After.
1a9e5 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 68 69 73 ** doing this
1a9e6 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 69 the directory i
1a9e7 73 20 73 79 6e 63 65 64 20 61 67 61 69 6e 20 62 s synced again b
1a9e8 65 66 6f 72 65 20 61 6e 79 20 69 6e 64 69 76 69 efore any indivi
1a9e9 64 75 61 6c 0a 20 20 20 20 2a 2a 20 74 72 61 6e dual. ** tran
1a9ea 73 61 63 74 69 6f 6e 20 66 69 6c 65 73 20 61 72 saction files ar
1a9eb 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a e deleted.. *
1a9ec 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
1a9ed 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c e3OsDelete(pVfs,
1a9ee 20 7a 4d 61 73 74 65 72 2c 20 31 29 3b 0a 20 20 zMaster, 1);.
1a9ef 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1a9f0 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 db, zMaster);.
1a9f1 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 zMaster = 0;.
1a9f2 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
1a9f3 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1a9f4 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 20 }.. /* All
1a9f5 66 69 6c 65 73 20 61 6e 64 20 64 69 72 65 63 74 files and direct
1a9f6 6f 72 69 65 73 20 68 61 76 65 20 61 6c 72 65 61 ories have alrea
1a9f7 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 2c 20 dy been synced,
1a9f8 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 so the following
1a9f9 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f . ** calls to
1a9fa 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
1a9fb 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 61 72 mitPhaseTwo() ar
1a9fc 65 20 6f 6e 6c 79 20 63 6c 6f 73 69 6e 67 20 66 e only closing f
1a9fd 69 6c 65 73 20 61 6e 64 0a 20 20 20 20 2a 2a 20 iles and. **
1a9fe 64 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 75 6e deleting or trun
1a9ff 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 73 2e cating journals.
1aa00 20 49 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f If something go
1aa01 65 73 20 77 72 6f 6e 67 20 77 68 69 6c 65 0a 20 es wrong while.
1aa02 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 61 ** this is ha
1aa03 70 70 65 6e 69 6e 67 20 77 65 20 64 6f 6e 27 74 ppening we don't
1aa04 20 72 65 61 6c 6c 79 20 63 61 72 65 2e 20 54 68 really care. Th
1aa05 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 74 e integrity of t
1aa06 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 he. ** transa
1aa07 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 ction is already
1aa08 20 67 75 61 72 61 6e 74 65 65 64 2c 20 62 75 74 guaranteed, but
1aa09 20 73 6f 6d 65 20 73 74 72 61 79 20 27 63 6f 6c some stray 'col
1aa0a 64 27 20 6a 6f 75 72 6e 61 6c 73 0a 20 20 20 20 d' journals.
1aa0b 2a 2a 20 6d 61 79 20 62 65 20 6c 79 69 6e 67 20 ** may be lying
1aa0c 61 72 6f 75 6e 64 2e 20 52 65 74 75 72 6e 69 6e around. Returnin
1aa0d 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 g an error code
1aa0e 77 6f 6e 27 74 20 68 65 6c 70 20 6d 61 74 74 65 won't help matte
1aa0f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 rs.. */. d
1aa10 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 isable_simulated
1aa11 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 _io_errors();.
1aa12 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 sqlite3BeginBe
1aa13 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
1aa14 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d for(i=0; i<db-
1aa15 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 >nDb; i++){ .
1aa16 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 Btree *pBt =
1aa17 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a db->aDb[i].pBt;.
1aa18 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b if( pBt ){
1aa19 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1aa1a 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
1aa1b 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 20 Two(pBt);.
1aa1c 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 }. }. sqli
1aa1d 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
1aa1e 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61 62 6c 65 oc();. enable
1aa1f 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 _simulated_io_er
1aa20 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 73 71 6c rors();.. sql
1aa21 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 64 ite3VtabCommit(d
1aa22 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a b);. }.#endif..
1aa23 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1aa24 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 /* .** This rout
1aa25 69 6e 65 20 63 68 65 63 6b 73 20 74 68 61 74 20 ine checks that
1aa26 74 68 65 20 73 71 6c 69 74 65 33 2e 61 63 74 69 the sqlite3.acti
1aa27 76 65 56 64 62 65 43 6e 74 20 63 6f 75 6e 74 20 veVdbeCnt count
1aa28 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 74 63 variable.** matc
1aa29 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f hes the number o
1aa2a 66 20 76 64 62 65 27 73 20 69 6e 20 74 68 65 20 f vdbe's in the
1aa2b 6c 69 73 74 20 73 71 6c 69 74 65 33 2e 70 56 64 list sqlite3.pVd
1aa2c 62 65 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 63 be that are.** c
1aa2d 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65 2e urrently active.
1aa2e 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 An assertion fa
1aa2f 69 6c 73 20 69 66 20 74 68 65 20 74 77 6f 20 63 ils if the two c
1aa30 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61 74 ounts do not mat
1aa31 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 ch..** This is a
1aa32 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 6c 66 2d n internal self-
1aa33 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 69 74 20 check only - it
1aa34 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 65 6e 74 is not an essent
1aa35 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 0a 2a ial processing.*
1aa36 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 * step..**.** Th
1aa37 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 is is a no-op if
1aa38 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 69 6e NDEBUG is defin
1aa39 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e ed..*/.#ifndef N
1aa3a 44 45 42 55 47 0a 73 74 61 74 69 63 20 76 6f 69 DEBUG.static voi
1aa3b 64 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 d checkActiveVdb
1aa3c 65 43 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62 eCnt(sqlite3 *db
1aa3d 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 ){. Vdbe *p;.
1aa3e 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69 int cnt = 0;. i
1aa3f 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 3b 0a 20 nt nWrite = 0;.
1aa40 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 0a p = db->pVdbe;.
1aa41 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 while( p ){.
1aa42 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d if( p->magic==
1aa43 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 VDBE_MAGIC_RUN &
1aa44 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 & p->pc>=0 ){.
1aa45 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 cnt++;.
1aa46 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 if( p->readOnly
1aa47 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b 2b 3b 0a ==0 ) nWrite++;.
1aa48 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d }. p = p-
1aa49 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 61 73 >pNext;. }. as
1aa4a 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 2d 3e 61 sert( cnt==db->a
1aa4b 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 3b 0a ctiveVdbeCnt );.
1aa4c 20 20 61 73 73 65 72 74 28 20 6e 57 72 69 74 65 assert( nWrite
1aa4d 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 ==db->writeVdbeC
1aa4e 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 nt );.}.#else.#d
1aa4f 65 66 69 6e 65 20 63 68 65 63 6b 41 63 74 69 76 efine checkActiv
1aa50 65 56 64 62 65 43 6e 74 28 78 29 0a 23 65 6e 64 eVdbeCnt(x).#end
1aa51 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 76 if../*.** For ev
1aa52 65 72 79 20 42 74 72 65 65 20 74 68 61 74 20 69 ery Btree that i
1aa53 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 n database conne
1aa54 63 74 69 6f 6e 20 64 62 20 77 68 69 63 68 20 0a ction db which .
1aa55 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 ** has been modi
1aa56 66 69 65 64 2c 20 22 74 72 69 70 22 20 6f 72 20 fied, "trip" or
1aa57 69 6e 76 61 6c 69 64 61 74 65 20 65 61 63 68 20 invalidate each
1aa58 63 75 72 73 6f 72 20 69 6e 0a 2a 2a 20 74 68 61 cursor in.** tha
1aa59 74 20 42 74 72 65 65 20 6d 69 67 68 74 20 68 61 t Btree might ha
1aa5a 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 ve been modified
1aa5b 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 75 72 so that the cur
1aa5c 73 6f 72 0a 2a 2a 20 63 61 6e 20 6e 65 76 65 72 sor.** can never
1aa5d 20 62 65 20 75 73 65 64 20 61 67 61 69 6e 2e 20 be used again.
1aa5e 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 This happens wh
1aa5f 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a en a rollback.**
1aa60 2a 20 6f 63 63 75 72 73 2e 20 20 57 65 20 68 61 * occurs. We ha
1aa61 76 65 20 74 6f 20 74 72 69 70 20 61 6c 6c 20 74 ve to trip all t
1aa62 68 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73 he other cursors
1aa63 2c 20 65 76 65 6e 0a 2a 2a 20 63 75 72 73 6f 72 , even.** cursor
1aa64 20 66 72 6f 6d 20 6f 74 68 65 72 20 56 4d 73 20 from other VMs
1aa65 69 6e 20 64 69 66 66 65 72 65 6e 74 20 64 61 74 in different dat
1aa66 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1aa67 73 2c 0a 2a 2a 20 73 6f 20 74 68 61 74 20 6e 6f s,.** so that no
1aa68 6e 65 20 6f 66 20 74 68 65 6d 20 74 72 79 20 74 ne of them try t
1aa69 6f 20 75 73 65 20 74 68 65 20 64 61 74 61 20 61 o use the data a
1aa6a 74 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a 20 t which they.**
1aa6b 77 65 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 6e were pointing an
1aa6c 64 20 77 68 69 63 68 20 6e 6f 77 20 6d 61 79 20 d which now may
1aa6d 68 61 76 65 20 62 65 65 6e 20 63 68 61 6e 67 65 have been change
1aa6e 64 20 64 75 65 0a 2a 2a 20 74 6f 20 74 68 65 20 d due.** to the
1aa6f 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 rollback..**.**
1aa70 52 65 6d 65 6d 62 65 72 20 74 68 61 74 20 61 20 Remember that a
1aa71 72 6f 6c 6c 62 61 63 6b 20 63 61 6e 20 64 65 6c rollback can del
1aa72 65 74 65 20 74 61 62 6c 65 73 20 63 6f 6d 70 6c ete tables compl
1aa73 65 74 65 20 61 6e 64 0a 2a 2a 20 72 65 6f 72 64 ete and.** reord
1aa74 65 72 20 72 6f 6f 74 70 61 67 65 73 2e 20 20 53 er rootpages. S
1aa75 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66 66 o it is not suff
1aa76 69 63 69 65 6e 74 20 6a 75 73 74 20 74 6f 20 73 icient just to s
1aa77 61 76 65 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 ave.** the state
1aa78 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e 20 of the cursor.
1aa79 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 76 61 We have to inva
1aa7a 6c 69 64 61 74 65 20 74 68 65 20 63 75 72 73 6f lidate the curso
1aa7b 72 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74 20 r.** so that it
1aa7c 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 61 67 is never used ag
1aa7d 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ain..*/.static v
1aa7e 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 43 75 oid invalidateCu
1aa7f 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42 rsorsOnModifiedB
1aa80 74 72 65 65 73 28 73 71 6c 69 74 65 33 20 2a 64 trees(sqlite3 *d
1aa81 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 b){. int i;. f
1aa82 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 or(i=0; i<db->nD
1aa83 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 b; i++){. Btr
1aa84 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 62 5b ee *p = db->aDb[
1aa85 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 i].pBt;. if(
1aa86 70 20 26 26 20 73 71 6c 69 74 65 33 42 74 72 65 p && sqlite3Btre
1aa87 65 49 73 49 6e 54 72 61 6e 73 28 70 29 20 29 7b eIsInTrans(p) ){
1aa88 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 . sqlite3Bt
1aa89 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 reeTripAllCursor
1aa8a 73 28 70 2c 20 53 51 4c 49 54 45 5f 41 42 4f 52 s(p, SQLITE_ABOR
1aa8b 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a T);. }. }.}.
1aa8c 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 56 64 ./*.** If the Vd
1aa8d 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 be passed as the
1aa8e 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
1aa8f 6f 70 65 6e 65 64 20 61 20 73 74 61 74 65 6d 65 opened a stateme
1aa90 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a nt-transaction,.
1aa91 2a 2a 20 63 6c 6f 73 65 20 69 74 20 6e 6f 77 2e ** close it now.
1aa92 20 41 72 67 75 6d 65 6e 74 20 65 4f 70 20 6d 75 Argument eOp mu
1aa93 73 74 20 62 65 20 65 69 74 68 65 72 20 53 41 56 st be either SAV
1aa94 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
1aa95 6f 72 0a 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f or.** SAVEPOINT_
1aa96 52 45 4c 45 41 53 45 2e 20 49 66 20 69 74 20 69 RELEASE. If it i
1aa97 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c s SAVEPOINT_ROLL
1aa98 42 41 43 4b 2c 20 74 68 65 6e 20 74 68 65 20 73 BACK, then the s
1aa99 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e tatement.** tran
1aa9a 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 saction is rolle
1aa9b 64 20 62 61 63 6b 2e 20 49 66 20 65 4f 70 20 69 d back. If eOp i
1aa9c 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 s SAVEPOINT_RELE
1aa9d 41 53 45 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a ASE, then the .*
1aa9e 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e * statement tran
1aa9f 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 74 saction is commt
1aaa0 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ted..**.** If an
1aaa1 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 IO error occurs
1aaa2 2c 20 61 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 , an SQLITE_IOER
1aaa3 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64 65 R_XXX error code
1aaa4 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a is returned. .*
1aaa5 2a 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49 * Otherwise SQLI
1aaa6 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 TE_OK..*/.SQLITE
1aaa7 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1aaa8 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 ite3VdbeCloseSta
1aaa9 74 65 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 tement(Vdbe *p,
1aaaa 69 6e 74 20 65 4f 70 29 7b 0a 20 20 73 71 6c 69 int eOp){. sqli
1aaab 74 65 33 20 2a 63 6f 6e 73 74 20 64 62 20 3d 20 te3 *const db =
1aaac 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63 20 p->db;. int rc
1aaad 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 = SQLITE_OK;..
1aaae 2f 2a 20 49 66 20 70 2d 3e 69 53 74 61 74 65 6d /* If p->iStatem
1aaaf 65 6e 74 20 69 73 20 67 72 65 61 74 65 72 20 74 ent is greater t
1aab0 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 han zero, then t
1aab1 68 69 73 20 56 64 62 65 20 6f 70 65 6e 65 64 20 his Vdbe opened
1aab2 61 20 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e a . ** statemen
1aab3 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 68 t transaction th
1aab4 61 74 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f at should be clo
1aab5 73 65 64 20 68 65 72 65 2e 20 54 68 65 20 6f 6e sed here. The on
1aab6 6c 79 20 65 78 63 65 70 74 69 6f 6e 0a 20 20 2a ly exception. *
1aab7 2a 20 69 73 20 74 68 61 74 20 61 6e 20 49 4f 20 * is that an IO
1aab8 65 72 72 6f 72 20 6d 61 79 20 68 61 76 65 20 6f error may have o
1aab9 63 63 75 72 65 64 2c 20 63 61 75 73 69 6e 67 20 ccured, causing
1aaba 61 6e 20 65 6d 65 72 67 65 6e 63 79 20 72 6f 6c an emergency rol
1aabb 6c 62 61 63 6b 2e 0a 20 20 2a 2a 20 49 6e 20 74 lback.. ** In t
1aabc 68 69 73 20 63 61 73 65 20 28 64 62 2d 3e 6e 53 his case (db->nS
1aabd 74 61 74 65 6d 65 6e 74 3d 3d 30 29 2c 20 61 6e tatement==0), an
1aabe 64 20 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 d there is nothi
1aabf 6e 67 20 74 6f 20 64 6f 2e 0a 20 20 2a 2f 0a 20 ng to do.. */.
1aac0 20 69 66 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d if( db->nStatem
1aac1 65 6e 74 20 26 26 20 70 2d 3e 69 53 74 61 74 65 ent && p->iState
1aac2 6d 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 ment ){. int
1aac3 69 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 i;. const int
1aac4 20 69 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 2d iSavepoint = p-
1aac5 3e 69 53 74 61 74 65 6d 65 6e 74 2d 31 3b 0a 0a >iStatement-1;..
1aac6 20 20 20 20 61 73 73 65 72 74 28 20 65 4f 70 3d assert( eOp=
1aac7 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 =SAVEPOINT_ROLLB
1aac8 41 43 4b 20 7c 7c 20 65 4f 70 3d 3d 53 41 56 45 ACK || eOp==SAVE
1aac9 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 3b 0a POINT_RELEASE);.
1aaca 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
1aacb 6e 53 74 61 74 65 6d 65 6e 74 3e 30 20 29 3b 0a nStatement>0 );.
1aacc 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 assert( p->i
1aacd 53 74 61 74 65 6d 65 6e 74 3d 3d 28 64 62 2d 3e Statement==(db->
1aace 6e 53 74 61 74 65 6d 65 6e 74 2b 64 62 2d 3e 6e nStatement+db->n
1aacf 53 61 76 65 70 6f 69 6e 74 29 20 29 3b 0a 0a 20 Savepoint) );..
1aad0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 for(i=0; i<db
1aad1 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 ->nDb; i++){ .
1aad2 20 20 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51 int rc2 = SQ
1aad3 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 42 LITE_OK;. B
1aad4 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e tree *pBt = db->
1aad5 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 aDb[i].pBt;.
1aad6 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 if( pBt ){.
1aad7 20 20 20 20 20 69 66 28 20 65 4f 70 3d 3d 53 41 if( eOp==SA
1aad8 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b VEPOINT_ROLLBACK
1aad9 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
1aada 32 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 2 = sqlite3Btree
1aadb 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2c 20 53 Savepoint(pBt, S
1aadc 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
1aadd 4b 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a K, iSavepoint);.
1aade 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1aadf 20 20 69 66 28 20 72 63 32 3d 3d 53 51 4c 49 54 if( rc2==SQLIT
1aae0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1aae1 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 42 rc2 = sqlite3B
1aae2 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 70 42 treeSavepoint(pB
1aae3 74 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c t, SAVEPOINT_REL
1aae4 45 41 53 45 2c 20 69 53 61 76 65 70 6f 69 6e 74 EASE, iSavepoint
1aae5 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
1aae6 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
1aae7 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1aae8 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 rc = rc2;.
1aae9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1aaea 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6e 53 }. db->nS
1aaeb 74 61 74 65 6d 65 6e 74 2d 2d 3b 0a 20 20 20 20 tatement--;.
1aaec 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 p->iStatement =
1aaed 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
1aaee 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 rc;.}../*.** If
1aaef 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c SQLite is compil
1aaf0 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 73 68 ed to support sh
1aaf1 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 ared-cache mode
1aaf2 61 6e 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 and to be thread
1aaf3 73 61 66 65 2c 0a 2a 2a 20 74 68 69 73 20 72 6f safe,.** this ro
1aaf4 75 74 69 6e 65 20 6f 62 74 61 69 6e 73 20 74 68 utine obtains th
1aaf5 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 e mutex associat
1aaf6 65 64 20 77 69 74 68 20 65 61 63 68 20 42 74 53 ed with each BtS
1aaf7 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 0a hared structure.
1aaf8 2a 2a 20 74 68 61 74 20 6d 61 79 20 62 65 20 61 ** that may be a
1aaf9 63 63 65 73 73 65 64 20 62 79 20 74 68 65 20 56 ccessed by the V
1aafa 4d 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 M passed as an a
1aafb 72 67 75 6d 65 6e 74 2e 20 49 6e 20 64 6f 69 6e rgument. In doin
1aafc 67 20 73 6f 20 69 74 0a 2a 2a 20 73 65 74 73 20 g so it.** sets
1aafd 74 68 65 20 42 74 53 68 61 72 65 64 2e 64 62 20 the BtShared.db
1aafe 6d 65 6d 62 65 72 20 6f 66 20 65 61 63 68 20 6f member of each o
1aaff 66 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 f the BtShared s
1ab00 74 72 75 63 74 75 72 65 73 2c 20 65 6e 73 75 72 tructures, ensur
1ab01 69 6e 67 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 ing.** that the
1ab02 63 6f 72 72 65 63 74 20 62 75 73 79 2d 68 61 6e correct busy-han
1ab03 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 dler callback is
1ab04 20 69 6e 76 6f 6b 65 64 20 69 66 20 72 65 71 75 invoked if requ
1ab05 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 ired..**.** If S
1ab06 51 4c 69 74 65 20 69 73 20 6e 6f 74 20 74 68 72 QLite is not thr
1ab07 65 61 64 73 61 66 65 20 62 75 74 20 64 6f 65 73 eadsafe but does
1ab08 20 73 75 70 70 6f 72 74 20 73 68 61 72 65 64 2d support shared-
1ab09 63 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 65 6e cache mode, then
1ab0a 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 .** sqlite3Btree
1ab0b 45 6e 74 65 72 41 6c 6c 28 29 20 69 73 20 69 6e EnterAll() is in
1ab0c 76 6f 6b 65 64 20 74 6f 20 73 65 74 20 74 68 65 voked to set the
1ab0d 20 42 74 53 68 61 72 65 64 2e 64 62 20 76 61 72 BtShared.db var
1ab0e 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 61 6c 6c iables.** of all
1ab0f 20 6f 66 20 42 74 53 68 61 72 65 64 20 73 74 72 of BtShared str
1ab10 75 63 74 75 72 65 73 20 61 63 63 65 73 73 69 62 uctures accessib
1ab11 6c 65 20 76 69 61 20 74 68 65 20 64 61 74 61 62 le via the datab
1ab12 61 73 65 20 68 61 6e 64 6c 65 20 0a 2a 2a 20 61 ase handle .** a
1ab13 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
1ab14 68 65 20 56 4d 2e 20 4f 66 20 63 6f 75 72 73 65 he VM. Of course
1ab15 20 6f 6e 6c 79 20 61 20 73 75 62 73 65 74 20 6f only a subset o
1ab16 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 f these structur
1ab17 65 73 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 61 63 es.** will be ac
1ab18 63 65 73 73 65 64 20 62 79 20 74 68 65 20 56 4d cessed by the VM
1ab19 2c 20 61 6e 64 20 77 65 20 63 6f 75 6c 64 20 75 , and we could u
1ab1a 73 65 20 56 64 62 65 2e 62 74 72 65 65 4d 61 73 se Vdbe.btreeMas
1ab1b 6b 20 74 6f 20 66 69 67 75 72 65 0a 2a 2a 20 74 k to figure.** t
1ab1c 68 61 74 20 73 75 62 73 65 74 20 6f 75 74 2c 20 hat subset out,
1ab1d 62 75 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 but there is no
1ab1e 61 64 76 61 6e 74 61 67 65 20 74 6f 20 64 6f 69 advantage to doi
1ab1f 6e 67 20 73 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ng so..**.** If
1ab20 53 51 4c 69 74 65 20 69 73 20 6e 6f 74 20 74 68 SQLite is not th
1ab21 72 65 61 64 73 61 66 65 20 61 6e 64 20 64 6f 65 readsafe and doe
1ab22 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 68 s not support sh
1ab23 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c ared-cache mode,
1ab24 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f this.** functio
1ab25 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f n is a no-op..*/
1ab26 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1ab27 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
1ab28 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 E.SQLITE_PRIVATE
1ab29 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
1ab2a 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 eMutexArrayEnter
1ab2b 28 56 64 62 65 20 2a 70 29 7b 0a 23 69 66 20 53 (Vdbe *p){.#if S
1ab2c 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
1ab2d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d . sqlite3BtreeM
1ab2e 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 26 utexArrayEnter(&
1ab2f 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 23 65 6c 73 p->aMutex);.#els
1ab30 65 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 e. sqlite3Btree
1ab31 45 6e 74 65 72 41 6c 6c 28 70 2d 3e 64 62 29 3b EnterAll(p->db);
1ab32 0a 23 65 6e 64 69 66 0a 7d 0a 23 65 6e 64 69 66 .#endif.}.#endif
1ab33 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
1ab34 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 tine is called t
1ab35 68 65 20 77 68 65 6e 20 61 20 56 44 42 45 20 74 he when a VDBE t
1ab36 72 69 65 73 20 74 6f 20 68 61 6c 74 2e 20 20 49 ries to halt. I
1ab37 66 20 74 68 65 20 56 44 42 45 0a 2a 2a 20 68 61 f the VDBE.** ha
1ab38 73 20 6d 61 64 65 20 63 68 61 6e 67 65 73 20 61 s made changes a
1ab39 6e 64 20 69 73 20 69 6e 20 61 75 74 6f 63 6f 6d nd is in autocom
1ab3a 6d 69 74 20 6d 6f 64 65 2c 20 74 68 65 6e 20 63 mit mode, then c
1ab3b 6f 6d 6d 69 74 20 74 68 6f 73 65 0a 2a 2a 20 63 ommit those.** c
1ab3c 68 61 6e 67 65 73 2e 20 20 49 66 20 61 20 72 6f hanges. If a ro
1ab3d 6c 6c 62 61 63 6b 20 69 73 20 6e 65 65 64 65 64 llback is needed
1ab3e 2c 20 74 68 65 6e 20 64 6f 20 74 68 65 20 72 6f , then do the ro
1ab3f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 llback..**.** Th
1ab40 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 is routine is th
1ab41 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f e only way to mo
1ab42 76 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 ve the state of
1ab43 61 20 56 4d 20 66 72 6f 6d 0a 2a 2a 20 53 51 4c a VM from.** SQL
1ab44 49 54 45 5f 4d 41 47 49 43 5f 52 55 4e 20 74 6f ITE_MAGIC_RUN to
1ab45 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41 SQLITE_MAGIC_HA
1ab46 4c 54 2e 20 20 49 74 20 69 73 20 68 61 72 6d 6c LT. It is harml
1ab47 65 73 73 20 74 6f 0a 2a 2a 20 63 61 6c 6c 20 74 ess to.** call t
1ab48 68 69 73 20 6f 6e 20 61 20 56 4d 20 74 68 61 74 his on a VM that
1ab49 20 69 73 20 69 6e 20 74 68 65 20 53 51 4c 49 54 is in the SQLIT
1ab4a 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 73 74 61 E_MAGIC_HALT sta
1ab4b 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e te..**.** Return
1ab4c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 an error code.
1ab4d 20 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 63 If the commit c
1ab4e 6f 75 6c 64 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 ould not complet
1ab4f 65 20 62 65 63 61 75 73 65 20 6f 66 0a 2a 2a 20 e because of.**
1ab50 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2c lock contention,
1ab51 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 return SQLITE_B
1ab52 55 53 59 2e 20 20 49 66 20 53 51 4c 49 54 45 5f USY. If SQLITE_
1ab53 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 64 BUSY is returned
1ab54 2c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 , it.** means th
1ab55 65 20 63 6c 6f 73 65 20 64 69 64 20 6e 6f 74 20 e close did not
1ab56 68 61 70 70 65 6e 20 61 6e 64 20 6e 65 65 64 73 happen and needs
1ab57 20 74 6f 20 62 65 20 72 65 70 65 61 74 65 64 2e to be repeated.
1ab58 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1ab59 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
1ab5a 62 65 48 61 6c 74 28 56 64 62 65 20 2a 70 29 7b beHalt(Vdbe *p){
1ab5b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
1ab5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ab5d 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 73 74 /* Used to st
1ab5e 6f 72 65 20 74 72 61 6e 73 69 65 6e 74 20 72 65 ore transient re
1ab5f 74 75 72 6e 20 63 6f 64 65 73 20 2a 2f 0a 20 20 turn codes */.
1ab60 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d sqlite3 *db = p-
1ab61 3e 64 62 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 >db;.. /* This
1ab62 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e function contain
1ab63 73 20 74 68 65 20 6c 6f 67 69 63 20 74 68 61 74 s the logic that
1ab64 20 64 65 74 65 72 6d 69 6e 65 73 20 69 66 20 61 determines if a
1ab65 20 73 74 61 74 65 6d 65 6e 74 20 6f 72 0a 20 20 statement or.
1ab66 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 ** transaction w
1ab67 69 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 ill be committed
1ab68 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 or rolled back
1ab69 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 as a result of t
1ab6a 68 65 0a 20 20 2a 2a 20 65 78 65 63 75 74 69 6f he. ** executio
1ab6b 6e 20 6f 66 20 74 68 69 73 20 76 69 72 74 75 61 n of this virtua
1ab6c 6c 20 6d 61 63 68 69 6e 65 2e 20 0a 20 20 2a 2a l machine. . **
1ab6d 0a 20 20 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 . ** If any of
1ab6e 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72 the following er
1ab6f 72 6f 72 73 20 6f 63 63 75 72 3a 0a 20 20 2a 2a rors occur:. **
1ab70 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 . ** SQLITE
1ab71 5f 4e 4f 4d 45 4d 0a 20 20 2a 2a 20 20 20 20 20 _NOMEM. **
1ab72 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 2a SQLITE_IOERR. *
1ab73 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 46 55 4c * SQLITE_FUL
1ab74 4c 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 L. ** SQLIT
1ab75 45 5f 49 4e 54 45 52 52 55 50 54 0a 20 20 2a 2a E_INTERRUPT. **
1ab76 0a 20 20 2a 2a 20 54 68 65 6e 20 74 68 65 20 69 . ** Then the i
1ab77 6e 74 65 72 6e 61 6c 20 63 61 63 68 65 20 6d 69 nternal cache mi
1ab78 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 6c 65 ght have been le
1ab79 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 ft in an inconsi
1ab7a 73 74 65 6e 74 0a 20 20 2a 2a 20 73 74 61 74 65 stent. ** state
1ab7b 2e 20 20 57 65 20 6e 65 65 64 20 74 6f 20 72 6f . We need to ro
1ab7c 6c 6c 62 61 63 6b 20 74 68 65 20 73 74 61 74 65 llback the state
1ab7d 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1ab7e 2c 20 69 66 20 74 68 65 72 65 20 69 73 0a 20 20 , if there is.
1ab7f 2a 2a 20 6f 6e 65 2c 20 6f 72 20 74 68 65 20 63 ** one, or the c
1ab80 6f 6d 70 6c 65 74 65 20 74 72 61 6e 73 61 63 74 omplete transact
1ab81 69 6f 6e 20 69 66 20 74 68 65 72 65 20 69 73 20 ion if there is
1ab82 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 no statement tra
1ab83 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 0a nsaction.. */..
1ab84 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c if( p->db->mal
1ab85 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
1ab86 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f p->rc = SQLITE_
1ab87 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 63 6c 6f NOMEM;. }. clo
1ab88 73 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 29 3b seAllCursors(p);
1ab89 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 . if( p->magic!
1ab8a 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 =VDBE_MAGIC_RUN
1ab8b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
1ab8c 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 63 LITE_OK;. }. c
1ab8d 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e heckActiveVdbeCn
1ab8e 74 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 20 t(db);.. /* No
1ab8f 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 commit or rollba
1ab90 63 6b 20 6e 65 65 64 65 64 20 69 66 20 74 68 65 ck needed if the
1ab91 20 70 72 6f 67 72 61 6d 20 6e 65 76 65 72 20 73 program never s
1ab92 74 61 72 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 tarted */. if(
1ab93 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 p->pc>=0 ){.
1ab94 69 6e 74 20 6d 72 63 3b 20 20 20 2f 2a 20 50 72 int mrc; /* Pr
1ab95 69 6d 61 72 79 20 65 72 72 6f 72 20 63 6f 64 65 imary error code
1ab96 20 66 72 6f 6d 20 70 2d 3e 72 63 20 2a 2f 0a 20 from p->rc */.
1ab97 20 20 20 69 6e 74 20 65 53 74 61 74 65 6d 65 6e int eStatemen
1ab98 74 4f 70 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 tOp = 0;. int
1ab99 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 3b isSpecialError;
1ab9a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
1ab9b 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 61 20 et to true if a
1ab9c 27 73 70 65 63 69 61 6c 27 20 65 72 72 6f 72 20 'special' error
1ab9d 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 */.. /* Lock
1ab9e 61 6c 6c 20 62 74 72 65 65 73 20 75 73 65 64 20 all btrees used
1ab9f 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 by the statement
1aba0 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 */. sqlite3V
1aba1 64 62 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 dbeMutexArrayEnt
1aba2 65 72 28 70 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 er(p);.. /* C
1aba3 68 65 63 6b 20 66 6f 72 20 6f 6e 65 20 6f 66 20 heck for one of
1aba4 74 68 65 20 73 70 65 63 69 61 6c 20 65 72 72 6f the special erro
1aba5 72 73 20 2a 2f 0a 20 20 20 20 6d 72 63 20 3d 20 rs */. mrc =
1aba6 70 2d 3e 72 63 20 26 20 30 78 66 66 3b 0a 20 20 p->rc & 0xff;.
1aba7 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 21 assert( p->rc!
1aba8 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c =SQLITE_IOERR_BL
1aba9 4f 43 4b 45 44 20 29 3b 20 20 2f 2a 20 54 68 69 OCKED ); /* Thi
1abaa 73 20 65 72 72 6f 72 20 6e 6f 20 6c 6f 6e 67 65 s error no longe
1abab 72 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 r exists */.
1abac 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 20 3d isSpecialError =
1abad 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d mrc==SQLITE_NOM
1abae 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 EM || mrc==SQLIT
1abaf 45 5f 49 4f 45 52 52 0a 20 20 20 20 20 20 20 20 E_IOERR.
1abb0 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
1abb1 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 mrc==SQLITE_INTE
1abb2 52 52 55 50 54 20 7c 7c 20 6d 72 63 3d 3d 53 51 RRUPT || mrc==SQ
1abb3 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 69 LITE_FULL;. i
1abb4 66 28 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f f( isSpecialErro
1abb5 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 r ){. /* If
1abb6 20 74 68 65 20 71 75 65 72 79 20 77 61 73 20 72 the query was r
1abb7 65 61 64 2d 6f 6e 6c 79 2c 20 77 65 20 6e 65 65 ead-only, we nee
1abb8 64 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b d do no rollback
1abb9 20 61 74 20 61 6c 6c 2e 20 4f 74 68 65 72 77 69 at all. Otherwi
1abba 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f se,. ** pro
1abbb 63 65 65 64 20 77 69 74 68 20 74 68 65 20 73 70 ceed with the sp
1abbc 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 2e 0a ecial handling..
1abbd 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
1abbe 66 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 f( !p->readOnly
1abbf 7c 7c 20 6d 72 63 21 3d 53 51 4c 49 54 45 5f 49 || mrc!=SQLITE_I
1abc0 4e 54 45 52 52 55 50 54 20 29 7b 0a 20 20 20 20 NTERRUPT ){.
1abc1 20 20 20 20 69 66 28 20 28 6d 72 63 3d 3d 53 51 if( (mrc==SQ
1abc2 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 LITE_NOMEM || mr
1abc3 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 29 20 c==SQLITE_FULL)
1abc4 26 26 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f && p->usesStmtJo
1abc5 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 urnal ){.
1abc6 20 20 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 eStatementOp
1abc7 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c = SAVEPOINT_ROLL
1abc8 42 41 43 4b 3b 0a 20 20 20 20 20 20 20 20 7d 65 BACK;. }e
1abc9 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f lse{. /
1abca 2a 20 57 65 20 61 72 65 20 66 6f 72 63 65 64 20 * We are forced
1abcb 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65 to roll back the
1abcc 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63 74 active transact
1abcd 69 6f 6e 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e ion. Before doin
1abce 67 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 g. ** s
1abcf 6f 2c 20 61 62 6f 72 74 20 61 6e 79 20 6f 74 68 o, abort any oth
1abd0 65 72 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 er statements th
1abd1 69 73 20 68 61 6e 64 6c 65 20 63 75 72 72 65 6e is handle curren
1abd2 74 6c 79 20 68 61 73 20 61 63 74 69 76 65 2e 0a tly has active..
1abd3 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 */.
1abd4 20 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 invalidat
1abd5 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 eCursorsOnModifi
1abd6 65 64 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20 edBtrees(db);.
1abd7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 sqlite3R
1abd8 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a ollbackAll(db);.
1abd9 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1abda 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 3CloseSavepoints
1abdb 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 (db);.
1abdc 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d db->autoCommit =
1abdd 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 1;. }.
1abde 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 }. }. .
1abdf 20 20 20 2f 2a 20 49 66 20 74 68 65 20 61 75 74 /* If the aut
1abe0 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 o-commit flag is
1abe1 20 73 65 74 20 61 6e 64 20 74 68 69 73 20 69 73 set and this is
1abe2 20 74 68 65 20 6f 6e 6c 79 20 61 63 74 69 76 65 the only active
1abe3 20 77 72 69 74 65 72 20 0a 20 20 20 20 2a 2a 20 writer . **
1abe4 56 4d 2c 20 74 68 65 6e 20 77 65 20 64 6f 20 65 VM, then we do e
1abe5 69 74 68 65 72 20 61 20 63 6f 6d 6d 69 74 20 6f ither a commit o
1abe6 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 68 r rollback of th
1abe7 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 e current transa
1abe8 63 74 69 6f 6e 2e 20 0a 20 20 20 20 2a 2a 0a 20 ction. . **.
1abe9 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 69 73 ** Note: This
1abea 20 62 6c 6f 63 6b 20 61 6c 73 6f 20 72 75 6e 73 block also runs
1abeb 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 if one of the s
1abec 70 65 63 69 61 6c 20 65 72 72 6f 72 73 20 68 61 pecial errors ha
1abed 6e 64 6c 65 64 20 0a 20 20 20 20 2a 2a 20 61 62 ndled . ** ab
1abee 6f 76 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 ove has occurred
1abef 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 . . */. if
1abf0 28 20 21 73 71 6c 69 74 65 33 56 74 61 62 49 6e ( !sqlite3VtabIn
1abf1 53 79 6e 63 28 64 62 29 20 0a 20 20 20 20 20 26 Sync(db) . &
1abf2 26 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 & db->autoCommit
1abf3 20 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 77 72 . && db->wr
1abf4 69 74 65 56 64 62 65 43 6e 74 3d 3d 28 70 2d 3e iteVdbeCnt==(p->
1abf5 72 65 61 64 4f 6e 6c 79 3d 3d 30 29 20 0a 20 20 readOnly==0) .
1abf6 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 ){. if( p
1abf7 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ->rc==SQLITE_OK
1abf8 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41 63 74 69 || (p->errorActi
1abf9 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26 26 20 21 on==OE_Fail && !
1abfa 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 29 20 isSpecialError)
1abfb 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
1abfc 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c e auto-commit fl
1abfd 61 67 20 69 73 20 74 72 75 65 2c 20 61 6e 64 20 ag is true, and
1abfe 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d the vdbe program
1abff 20 77 61 73 20 0a 20 20 20 20 20 20 20 20 2a 2a was . **
1ac00 20 73 75 63 63 65 73 73 66 75 6c 20 6f 72 20 68 successful or h
1ac01 69 74 20 61 6e 20 27 4f 52 20 46 41 49 4c 27 20 it an 'OR FAIL'
1ac02 63 6f 6e 73 74 72 61 69 6e 74 2e 20 54 68 69 73 constraint. This
1ac03 20 6d 65 61 6e 73 20 61 20 63 6f 6d 6d 69 74 20 means a commit
1ac04 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 72 . ** is r
1ac05 65 71 75 69 72 65 64 2e 0a 20 20 20 20 20 20 20 equired..
1ac06 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d */. rc =
1ac07 20 76 64 62 65 43 6f 6d 6d 69 74 28 64 62 2c 20 vdbeCommit(db,
1ac08 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 p);. if(
1ac09 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 rc==SQLITE_BUSY
1ac0a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
1ac0b 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 ite3BtreeMutexAr
1ac0c 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75 rayLeave(&p->aMu
1ac0d 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 tex);.
1ac0e 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 return SQLITE_BU
1ac0f 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 SY;. }els
1ac10 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 e if( rc!=SQLITE
1ac11 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
1ac12 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 p->rc = rc;.
1ac13 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f sqlite3Ro
1ac14 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 llbackAll(db);.
1ac15 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1ac16 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 sqlite3C
1ac17 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 ommitInternalCha
1ac18 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20 nges(db);.
1ac19 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
1ac1a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1ac1b 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b RollbackAll(db);
1ac1c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 . }. d
1ac1d 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d 20 b->nStatement =
1ac1e 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 0;. }else if(
1ac1f 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 3d 3d 30 eStatementOp==0
1ac20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d ){. if( p-
1ac21 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c >rc==SQLITE_OK |
1ac22 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e | p->errorAction
1ac23 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 20 20 ==OE_Fail ){.
1ac24 20 20 20 20 20 65 53 74 61 74 65 6d 65 6e 74 4f eStatementO
1ac25 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 p = SAVEPOINT_RE
1ac26 4c 45 41 53 45 3b 0a 20 20 20 20 20 20 7d 65 6c LEASE;. }el
1ac27 73 65 20 69 66 28 20 70 2d 3e 65 72 72 6f 72 41 se if( p->errorA
1ac28 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72 74 20 ction==OE_Abort
1ac29 29 7b 0a 20 20 20 20 20 20 20 20 65 53 74 61 74 ){. eStat
1ac2a 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f ementOp = SAVEPO
1ac2b 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 INT_ROLLBACK;.
1ac2c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1ac2d 20 20 20 69 6e 76 61 6c 69 64 61 74 65 43 75 72 invalidateCur
1ac2e 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42 74 sorsOnModifiedBt
1ac2f 72 65 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20 rees(db);.
1ac30 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 sqlite3Rollbac
1ac31 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 kAll(db);.
1ac32 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 sqlite3CloseSa
1ac33 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 vepoints(db);.
1ac34 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f db->autoCo
1ac35 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 mmit = 1;.
1ac36 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f }. }. . /
1ac37 2a 20 49 66 20 65 53 74 61 74 65 6d 65 6e 74 4f * If eStatementO
1ac38 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 p is non-zero, t
1ac39 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 hen a statement
1ac3a 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 transaction need
1ac3b 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 63 s to. ** be c
1ac3c 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c ommitted or roll
1ac3d 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c 20 73 71 ed back. Call sq
1ac3e 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 lite3VdbeCloseSt
1ac3f 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a 20 20 20 atement() to.
1ac40 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66 20 74 68 ** do so. If th
1ac41 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 72 65 74 is operation ret
1ac42 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2c 20 61 urns an error, a
1ac43 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 nd the current s
1ac44 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 tatement. **
1ac45 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 53 51 error code is SQ
1ac46 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54 LITE_OK or SQLIT
1ac47 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 74 68 E_CONSTRAINT, th
1ac48 65 6e 20 73 65 74 20 74 68 65 20 65 72 72 6f 72 en set the error
1ac49 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 74 6f 20 . ** code to
1ac4a 74 68 65 20 6e 65 77 20 76 61 6c 75 65 2e 0a 20 the new value..
1ac4b 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 53 */. if( eS
1ac4c 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b 0a 20 20 tatementOp ){.
1ac4d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1ac4e 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 VdbeCloseStateme
1ac4f 6e 74 28 70 2c 20 65 53 74 61 74 65 6d 65 6e 74 nt(p, eStatement
1ac50 4f 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 Op);. if( r
1ac51 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d 53 51 4c c && (p->rc==SQL
1ac52 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d ITE_OK || p->rc=
1ac53 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 =SQLITE_CONSTRAI
1ac54 4e 54 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 NT) ){. p
1ac55 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 ->rc = rc;.
1ac56 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1ac57 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 (db, p->zErrMsg)
1ac58 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 45 72 ;. p->zEr
1ac59 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 rMsg = 0;.
1ac5a 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f }. }. . /
1ac5b 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 61 6e * If this was an
1ac5c 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 20 INSERT, UPDATE
1ac5d 6f 72 20 44 45 4c 45 54 45 20 61 6e 64 20 6e 6f or DELETE and no
1ac5e 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
1ac5f 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 68 61 action. ** ha
1ac60 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 s been rolled ba
1ac61 63 6b 2c 20 75 70 64 61 74 65 20 74 68 65 20 64 ck, update the d
1ac62 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1ac63 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 on change-counte
1ac64 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 r. . */. i
1ac65 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e 74 4f f( p->changeCntO
1ac66 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 n ){. if( e
1ac67 53 74 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41 56 StatementOp!=SAV
1ac68 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
1ac69 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1ac6a 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 e3VdbeSetChanges
1ac6b 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 (db, p->nChange)
1ac6c 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1ac6d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1ac6e 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c beSetChanges(db,
1ac6f 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0);. }.
1ac70 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 p->nChange =
1ac71 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 0;. }. .
1ac72 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 63 /* Rollback or c
1ac73 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68 65 6d 61 ommit any schema
1ac74 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 6f 63 changes that oc
1ac75 63 75 72 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69 curred. */. i
1ac76 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 f( p->rc!=SQLITE
1ac77 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 _OK && db->flags
1ac78 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 &SQLITE_InternCh
1ac79 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 20 20 73 anges ){. s
1ac7a 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 qlite3ResetInter
1ac7b 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 nalSchema(db, 0)
1ac7c 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67 ;. db->flag
1ac7d 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 7c s = (db->flags |
1ac7e 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 SQLITE_InternCh
1ac7f 61 6e 67 65 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 anges);. }..
1ac80 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 /* Release th
1ac81 65 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 73 e locks */. s
1ac82 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
1ac83 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 ArrayLeave(&p->a
1ac84 4d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f Mutex);. }.. /
1ac85 2a 20 57 65 20 68 61 76 65 20 73 75 63 63 65 73 * We have succes
1ac86 73 66 75 6c 6c 79 20 68 61 6c 74 65 64 20 61 6e sfully halted an
1ac87 64 20 63 6c 6f 73 65 64 20 74 68 65 20 56 4d 2e d closed the VM.
1ac88 20 20 52 65 63 6f 72 64 20 74 68 69 73 20 66 61 Record this fa
1ac89 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e ct. */. if( p->
1ac8a 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d pc>=0 ){. db-
1ac8b 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 2d 2d >activeVdbeCnt--
1ac8c 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65 ;. if( !p->re
1ac8d 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 adOnly ){.
1ac8e 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 db->writeVdbeCnt
1ac8f 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 --;. }. as
1ac90 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 sert( db->active
1ac91 56 64 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 72 69 VdbeCnt>=db->wri
1ac92 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 20 20 7d teVdbeCnt );. }
1ac93 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 . p->magic = VD
1ac94 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0a 20 BE_MAGIC_HALT;.
1ac95 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 checkActiveVdbe
1ac96 43 6e 74 28 64 62 29 3b 0a 20 20 69 66 28 20 70 Cnt(db);. if( p
1ac97 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
1ac98 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 ed ){. p->rc
1ac99 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
1ac9a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
1ac9b 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 auto-commit fla
1ac9c 67 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 g is set to true
1ac9d 2c 20 74 68 65 6e 20 61 6e 79 20 6c 6f 63 6b 73 , then any locks
1ac9e 20 74 68 61 74 20 77 65 72 65 20 68 65 6c 64 0a that were held.
1ac9f 20 20 2a 2a 20 62 79 20 63 6f 6e 6e 65 63 74 69 ** by connecti
1aca0 6f 6e 20 64 62 20 68 61 76 65 20 6e 6f 77 20 62 on db have now b
1aca1 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 20 43 61 een released. Ca
1aca2 6c 6c 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 ll sqlite3Connec
1aca3 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 20 0a tionUnlocked() .
1aca4 20 20 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 20 61 ** to invoke a
1aca5 6e 79 20 72 65 71 75 69 72 65 64 20 75 6e 6c 6f ny required unlo
1aca6 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 ck-notify callba
1aca7 63 6b 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 cks.. */. if(
1aca8 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 db->autoCommit )
1aca9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e {. sqlite3Con
1acaa 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 nectionUnlocked(
1acab 64 62 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 db);. }.. asse
1acac 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 rt( db->activeVd
1acad 62 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 beCnt>0 || db->a
1acae 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 utoCommit==0 ||
1acaf 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d db->nStatement==
1acb0 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 0 );. return SQ
1acb1 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a LITE_OK;.}.../*.
1acb2 2a 2a 20 45 61 63 68 20 56 44 42 45 20 68 6f 6c ** Each VDBE hol
1acb3 64 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 ds the result of
1acb4 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
1acb5 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 sqlite3_step()
1acb6 63 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e 72 63 call.** in p->rc
1acb7 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
1acb8 73 65 74 73 20 74 68 61 74 20 72 65 73 75 6c 74 sets that result
1acb9 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f back to SQLITE_
1acba 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 OK..*/.SQLITE_PR
1acbb 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1acbc 65 33 56 64 62 65 52 65 73 65 74 53 74 65 70 52 e3VdbeResetStepR
1acbd 65 73 75 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a esult(Vdbe *p){.
1acbe 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 p->rc = SQLITE
1acbf 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c _OK;.}../*.** Cl
1acc0 65 61 6e 20 75 70 20 61 20 56 44 42 45 20 61 66 ean up a VDBE af
1acc1 74 65 72 20 65 78 65 63 75 74 69 6f 6e 20 62 75 ter execution bu
1acc2 74 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 t do not delete
1acc3 74 68 65 20 56 44 42 45 20 6a 75 73 74 20 79 65 the VDBE just ye
1acc4 74 2e 0a 2a 2a 20 57 72 69 74 65 20 61 6e 79 20 t..** Write any
1acc5 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 error messages i
1acc6 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 20 20 nto *pzErrMsg.
1acc7 52 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c Return the resul
1acc8 74 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 66 t code..**.** Af
1acc9 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ter this routine
1acca 20 69 73 20 72 75 6e 2c 20 74 68 65 20 56 44 42 is run, the VDB
1accb 45 20 73 68 6f 75 6c 64 20 62 65 20 72 65 61 64 E should be read
1accc 79 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 y to be executed
1accd 0a 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a .** again..**.**
1acce 20 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20 61 To look at it a
1accf 6e 6f 74 68 65 72 20 77 61 79 2c 20 74 68 69 73 nother way, this
1acd0 20 72 6f 75 74 69 6e 65 20 72 65 73 65 74 73 20 routine resets
1acd1 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 the state of the
1acd2 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 .** virtual mach
1acd3 69 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f 4d 41 ine from VDBE_MA
1acd4 47 49 43 5f 52 55 4e 20 6f 72 20 56 44 42 45 5f GIC_RUN or VDBE_
1acd5 4d 41 47 49 43 5f 48 41 4c 54 20 62 61 63 6b 20 MAGIC_HALT back
1acd6 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 to.** VDBE_MAGIC
1acd7 5f 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 _INIT..*/.SQLITE
1acd8 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1acd9 69 74 65 33 56 64 62 65 52 65 73 65 74 28 56 64 ite3VdbeReset(Vd
1acda 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 be *p){. sqlite
1acdb 33 20 2a 64 62 3b 0a 20 20 64 62 20 3d 20 70 2d 3 *db;. db = p-
1acdc 3e 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 >db;.. /* If th
1acdd 65 20 56 4d 20 64 69 64 20 6e 6f 74 20 72 75 6e e VM did not run
1acde 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f to completion o
1acdf 72 20 69 66 20 69 74 20 65 6e 63 6f 75 6e 74 65 r if it encounte
1ace0 72 65 64 20 61 6e 0a 20 20 2a 2a 20 65 72 72 6f red an. ** erro
1ace1 72 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 r, then it might
1ace2 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 68 not have been h
1ace3 61 6c 74 65 64 20 70 72 6f 70 65 72 6c 79 2e 20 alted properly.
1ace4 20 53 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 69 74 So halt. ** it
1ace5 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f now.. */. (vo
1ace6 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 id)sqlite3Safety
1ace7 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 On(db);. sqlite
1ace8 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 3VdbeHalt(p);.
1ace9 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 (void)sqlite3Saf
1acea 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f etyOff(db);.. /
1aceb 2a 20 49 66 20 74 68 65 20 56 44 42 45 20 68 61 * If the VDBE ha
1acec 73 20 62 65 20 72 75 6e 20 65 76 65 6e 20 70 61 s be run even pa
1aced 72 74 69 61 6c 6c 79 2c 20 74 68 65 6e 20 74 72 rtially, then tr
1acee 61 6e 73 66 65 72 20 74 68 65 20 65 72 72 6f 72 ansfer the error
1acef 20 63 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 65 code. ** and e
1acf0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f rror message fro
1acf1 6d 20 74 68 65 20 56 44 42 45 20 69 6e 74 6f 20 m the VDBE into
1acf2 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
1acf3 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 42 75 e structure. Bu
1acf4 74 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 56 44 t. ** if the VD
1acf5 42 45 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e BE has just been
1acf6 20 73 65 74 20 74 6f 20 72 75 6e 20 62 75 74 20 set to run but
1acf7 68 61 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 has not actually
1acf8 20 65 78 65 63 75 74 65 64 20 61 6e 79 0a 20 20 executed any.
1acf9 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 ** instructions
1acfa 79 65 74 2c 20 6c 65 61 76 65 20 74 68 65 20 6d yet, leave the m
1acfb 61 69 6e 20 64 61 74 61 62 61 73 65 20 65 72 72 ain database err
1acfc 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 or information u
1acfd 6e 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 nchanged.. */.
1acfe 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b if( p->pc>=0 ){
1acff 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a 45 72 72 . if( p->zErr
1ad00 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c Msg ){. sql
1ad01 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
1ad02 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 73 alloc();. s
1ad03 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 qlite3ValueSetSt
1ad04 72 28 64 62 2d 3e 70 45 72 72 2c 2d 31 2c 70 2d r(db->pErr,-1,p-
1ad05 3e 7a 45 72 72 4d 73 67 2c 53 51 4c 49 54 45 5f >zErrMsg,SQLITE_
1ad06 55 54 46 38 2c 53 51 4c 49 54 45 5f 54 52 41 4e UTF8,SQLITE_TRAN
1ad07 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 73 71 SIENT);. sq
1ad08 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 lite3EndBenignMa
1ad09 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 64 62 lloc();. db
1ad0a 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 ->errCode = p->r
1ad0b 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 c;. sqlite3
1ad0c 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 DbFree(db, p->zE
1ad0d 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 70 2d rrMsg);. p-
1ad0e 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 >zErrMsg = 0;.
1ad0f 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 }else if( p->r
1ad10 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 c ){. sqlit
1ad11 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72 e3Error(db, p->r
1ad12 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 c, 0);. }else
1ad13 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
1ad14 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f rror(db, SQLITE_
1ad15 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 OK, 0);. }.
1ad16 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20 }else if( p->rc
1ad17 26 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b && p->expired ){
1ad18 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 69 . /* The expi
1ad19 72 65 64 20 66 6c 61 67 20 77 61 73 20 73 65 74 red flag was set
1ad1a 20 6f 6e 20 74 68 65 20 56 44 42 45 20 62 65 66 on the VDBE bef
1ad1b 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 63 61 ore the first ca
1ad1c 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c ll. ** to sql
1ad1d 69 74 65 33 5f 73 74 65 70 28 29 2e 20 46 6f 72 ite3_step(). For
1ad1e 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 28 73 69 consistency (si
1ad1f 6e 63 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 nce sqlite3_step
1ad20 28 29 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 61 () was. ** ca
1ad21 6c 6c 65 64 29 2c 20 73 65 74 20 74 68 65 20 64 lled), set the d
1ad22 61 74 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e atabase error in
1ad23 20 74 68 69 73 20 63 61 73 65 20 61 73 20 77 65 this case as we
1ad24 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 ll.. */. s
1ad25 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 qlite3Error(db,
1ad26 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 73 p->rc, 0);. s
1ad27 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 qlite3ValueSetSt
1ad28 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 r(db->pErr, -1,
1ad29 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c 49 p->zErrMsg, SQLI
1ad2a 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f TE_UTF8, SQLITE_
1ad2b 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 TRANSIENT);.
1ad2c 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1ad2d 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 , p->zErrMsg);.
1ad2e 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 p->zErrMsg =
1ad2f 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 63 0;. }.. /* Rec
1ad30 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 laim all memory
1ad31 75 73 65 64 20 62 79 20 74 68 65 20 56 44 42 45 used by the VDBE
1ad32 0a 20 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 70 28 . */. Cleanup(
1ad33 70 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20 70 p);.. /* Save p
1ad34 72 6f 66 69 6c 69 6e 67 20 69 6e 66 6f 72 6d 61 rofiling informa
1ad35 74 69 6f 6e 20 66 72 6f 6d 20 74 68 69 73 20 56 tion from this V
1ad36 44 42 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a 23 69 DBE run.. */.#i
1ad37 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c fdef VDBE_PROFIL
1ad38 45 0a 20 20 7b 0a 20 20 20 20 46 49 4c 45 20 2a E. {. FILE *
1ad39 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22 76 64 62 out = fopen("vdb
1ad3a 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 22 2c 20 e_profile.out",
1ad3b 22 61 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 "a");. if( ou
1ad3c 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 t ){. int i
1ad3d 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 ;. fprintf(
1ad3e 6f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b 0a 20 out, "---- ");.
1ad3f 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c for(i=0; i<
1ad40 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 p->nOp; i++){.
1ad41 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 fprintf(ou
1ad42 74 2c 20 22 25 30 32 78 22 2c 20 70 2d 3e 61 4f t, "%02x", p->aO
1ad43 70 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a 20 20 p[i].opcode);.
1ad44 20 20 20 20 7d 0a 20 20 20 20 20 20 66 70 72 69 }. fpri
1ad45 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a ntf(out, "\n");.
1ad46 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 for(i=0; i
1ad47 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 <p->nOp; i++){.
1ad48 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f fprintf(o
1ad49 75 74 2c 20 22 25 36 64 20 25 31 30 6c 6c 64 20 ut, "%6d %10lld
1ad4a 25 38 6c 6c 64 20 22 2c 0a 20 20 20 20 20 20 20 %8lld ",.
1ad4b 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e p->aOp[i].cn
1ad4c 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d t,. p-
1ad4d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2c 0a >aOp[i].cycles,.
1ad4e 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4f p->aO
1ad4f 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 2d 3e p[i].cnt>0 ? p->
1ad50 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f 70 2d aOp[i].cycles/p-
1ad51 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 30 0a >aOp[i].cnt : 0.
1ad52 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 );.
1ad53 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 sqlite3VdbePr
1ad54 69 6e 74 4f 70 28 6f 75 74 2c 20 69 2c 20 26 70 intOp(out, i, &p
1ad55 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 20 ->aOp[i]);.
1ad56 20 7d 0a 20 20 20 20 20 20 66 63 6c 6f 73 65 28 }. fclose(
1ad57 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a out);. }. }.
1ad58 23 65 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 67 69 #endif. p->magi
1ad59 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 c = VDBE_MAGIC_I
1ad5a 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d NIT;. return p-
1ad5b 3e 72 63 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 >rc & db->errMas
1ad5c 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c 65 k;.}. ./*.** Cle
1ad5d 61 6e 20 75 70 20 61 6e 64 20 64 65 6c 65 74 65 an up and delete
1ad5e 20 61 20 56 44 42 45 20 61 66 74 65 72 20 65 78 a VDBE after ex
1ad5f 65 63 75 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e ecution. Return
1ad60 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 an integer whic
1ad61 68 20 69 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 h is.** the resu
1ad62 6c 74 20 63 6f 64 65 2e 20 20 57 72 69 74 65 20 lt code. Write
1ad63 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 any error messag
1ad64 65 20 74 65 78 74 20 69 6e 74 6f 20 2a 70 7a 45 e text into *pzE
1ad65 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 rrMsg..*/.SQLITE
1ad66 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1ad67 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 ite3VdbeFinalize
1ad68 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 (Vdbe *p){. int
1ad69 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1ad6a 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d . if( p->magic=
1ad6b 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 =VDBE_MAGIC_RUN
1ad6c 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 || p->magic==VDB
1ad6d 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 29 7b 0a E_MAGIC_HALT ){.
1ad6e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1ad6f 56 64 62 65 52 65 73 65 74 28 70 29 3b 0a 20 20 VdbeReset(p);.
1ad70 20 20 61 73 73 65 72 74 28 20 28 72 63 20 26 20 assert( (rc &
1ad71 70 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d p->db->errMask)=
1ad72 3d 72 63 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c =rc );. }. sql
1ad73 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 70 ite3VdbeDelete(p
1ad74 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1ad75 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 }../*.** Call th
1ad76 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 e destructor for
1ad77 20 65 61 63 68 20 61 75 78 64 61 74 61 20 65 6e each auxdata en
1ad78 74 72 79 20 69 6e 20 70 56 64 62 65 46 75 6e 63 try in pVdbeFunc
1ad79 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20 74 68 for which.** th
1ad7a 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
1ad7b 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73 20 63 bit in mask is c
1ad7c 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61 20 65 lear. Auxdata e
1ad7d 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20 33 31 ntries beyond 31
1ad7e 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73 20 64 .** are always d
1ad7f 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20 64 65 estroyed. To de
1ad80 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64 61 74 stroy all auxdat
1ad81 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c 6c 20 a entries, call
1ad82 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 this.** routine
1ad83 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a 2a 2f with mask==0..*/
1ad84 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1ad85 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1ad86 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 56 64 DeleteAuxData(Vd
1ad87 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e beFunc *pVdbeFun
1ad88 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20 20 c, int mask){.
1ad89 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 int i;. for(i=0
1ad8a 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 2d 3e 6e ; i<pVdbeFunc->n
1ad8b 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 Aux; i++){. s
1ad8c 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a 70 truct AuxData *p
1ad8d 41 75 78 20 3d 20 26 70 56 64 62 65 46 75 6e 63 Aux = &pVdbeFunc
1ad8e 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20 20 20 ->apAux[i];.
1ad8f 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 21 28 6d if( (i>31 || !(m
1ad90 61 73 6b 26 28 28 28 75 33 32 29 31 29 3c 3c 69 ask&(((u32)1)<<i
1ad91 29 29 29 20 26 26 20 70 41 75 78 2d 3e 70 41 75 ))) && pAux->pAu
1ad92 78 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 x ){. if( p
1ad93 41 75 78 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a Aux->xDelete ){.
1ad94 20 20 20 20 20 20 20 20 70 41 75 78 2d 3e 78 44 pAux->xD
1ad95 65 6c 65 74 65 28 70 41 75 78 2d 3e 70 41 75 78 elete(pAux->pAux
1ad96 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1ad97 20 70 41 75 78 2d 3e 70 41 75 78 20 3d 20 30 3b pAux->pAux = 0;
1ad98 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
1ad99 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e .** Delete an en
1ad9a 74 69 72 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 tire VDBE..*/.SQ
1ad9b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1ad9c 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c d sqlite3VdbeDel
1ad9d 65 74 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 ete(Vdbe *p){.
1ad9e 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 sqlite3 *db;..
1ad9f 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20 if( NEVER(p==0)
1ada0 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d ) return;. db =
1ada1 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d p->db;. if( p-
1ada2 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 70 2d >pPrev ){. p-
1ada3 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 >pPrev->pNext =
1ada4 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 p->pNext;. }els
1ada5 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 e{. assert( d
1ada6 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29 3b 0a 20 b->pVdbe==p );.
1ada7 20 20 20 64 62 2d 3e 70 56 64 62 65 20 3d 20 70 db->pVdbe = p
1ada8 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 ->pNext;. }. i
1ada9 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 f( p->pNext ){.
1adaa 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 p->pNext->pPr
1adab 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20 ev = p->pPrev;.
1adac 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 }. releaseMemA
1adad 72 72 61 79 28 70 2d 3e 61 56 61 72 2c 20 70 2d rray(p->aVar, p-
1adae 3e 6e 56 61 72 29 3b 0a 20 20 72 65 6c 65 61 73 >nVar);. releas
1adaf 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f eMemArray(p->aCo
1adb0 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f lName, p->nResCo
1adb1 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b lumn*COLNAME_N);
1adb2 0a 20 20 76 64 62 65 46 72 65 65 4f 70 41 72 72 . vdbeFreeOpArr
1adb3 61 79 28 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 70 ay(db, p->aOp, p
1adb4 2d 3e 6e 4f 70 29 3b 0a 20 20 73 71 6c 69 74 65 ->nOp);. sqlite
1adb5 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 3DbFree(db, p->a
1adb6 4c 61 62 65 6c 29 3b 0a 20 20 73 71 6c 69 74 65 Label);. sqlite
1adb7 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 3DbFree(db, p->a
1adb8 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 ColName);. sqli
1adb9 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
1adba 3e 7a 53 71 6c 29 3b 0a 20 20 70 2d 3e 6d 61 67 >zSql);. p->mag
1adbb 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f ic = VDBE_MAGIC_
1adbc 44 45 41 44 3b 0a 20 20 73 71 6c 69 74 65 33 44 DEAD;. sqlite3D
1adbd 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 70 46 72 bFree(db, p->pFr
1adbe 65 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 ee);. sqlite3Db
1adbf 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a Free(db, p);.}..
1adc0 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 /*.** Make sure
1adc1 74 68 65 20 63 75 72 73 6f 72 20 70 20 69 73 20 the cursor p is
1adc2 72 65 61 64 79 20 74 6f 20 72 65 61 64 20 6f 72 ready to read or
1adc3 20 77 72 69 74 65 20 74 68 65 20 72 6f 77 20 74 write the row t
1adc4 6f 20 77 68 69 63 68 20 69 74 0a 2a 2a 20 77 61 o which it.** wa
1adc5 73 20 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e 65 s last positione
1adc6 64 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 65 72 d. Return an er
1adc7 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 20 4f ror code if an O
1adc8 4f 4d 20 66 61 75 6c 74 20 6f 72 20 49 2f 4f 20 OM fault or I/O
1adc9 65 72 72 6f 72 0a 2a 2a 20 70 72 65 76 65 6e 74 error.** prevent
1adca 73 20 75 73 20 66 72 6f 6d 20 70 6f 73 69 74 69 s us from positi
1adcb 6f 6e 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 oning the cursor
1adcc 20 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 to its correct
1adcd 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 position..**.**
1adce 49 66 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 72 If a MoveTo oper
1adcf 61 74 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e 67 ation is pending
1add0 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 63 75 on the given cu
1add1 72 73 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 68 rsor, then do th
1add2 61 74 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f 77 at.** MoveTo now
1add3 2e 20 20 49 66 20 6e 6f 20 6d 6f 76 65 20 69 73 . If no move is
1add4 20 70 65 6e 64 69 6e 67 2c 20 63 68 65 63 6b 20 pending, check
1add5 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 72 6f to see if the ro
1add6 77 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 64 65 w has been.** de
1add7 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 leted out from u
1add8 6e 64 65 72 20 74 68 65 20 63 75 72 73 6f 72 20 nder the cursor
1add9 61 6e 64 20 69 66 20 69 74 20 68 61 73 2c 20 6d and if it has, m
1adda 61 72 6b 20 74 68 65 20 72 6f 77 20 61 73 0a 2a ark the row as.*
1addb 2a 20 61 20 4e 55 4c 4c 20 72 6f 77 2e 0a 2a 2a * a NULL row..**
1addc 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f .** If the curso
1addd 72 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f 69 r is already poi
1adde 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 63 6f 72 nting to the cor
1addf 72 65 63 74 20 72 6f 77 20 61 6e 64 20 74 68 61 rect row and tha
1ade0 74 20 72 6f 77 20 68 61 73 0a 2a 2a 20 6e 6f 74 t row has.** not
1ade1 20 62 65 65 6e 20 64 65 6c 65 74 65 64 20 6f 75 been deleted ou
1ade2 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 t from under the
1ade3 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 74 68 cursor, then th
1ade4 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
1ade5 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 no-op..*/.SQLITE
1ade6 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1ade7 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f ite3VdbeCursorMo
1ade8 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 20 veto(VdbeCursor
1ade9 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 64 65 *p){. if( p->de
1adea 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a ferredMoveto ){.
1adeb 20 20 20 20 69 6e 74 20 72 65 73 2c 20 72 63 3b int res, rc;
1adec 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
1aded 45 53 54 0a 20 20 20 20 65 78 74 65 72 6e 20 69 EST. extern i
1adee 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 nt sqlite3_searc
1adef 68 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 0a h_count;.#endif.
1adf0 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 assert( p->i
1adf1 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 72 63 sTable );. rc
1adf2 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d = sqlite3BtreeM
1adf3 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 2d ovetoUnpacked(p-
1adf4 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 70 2d 3e >pCursor, 0, p->
1adf5 6d 6f 76 65 74 6f 54 61 72 67 65 74 2c 20 30 2c movetoTarget, 0,
1adf6 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 &res);. if(
1adf7 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
1adf8 20 20 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69 64 p->lastRowid
1adf9 20 3d 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 = p->movetoTarg
1adfa 65 74 3b 0a 20 20 20 20 70 2d 3e 72 6f 77 69 64 et;. p->rowid
1adfb 49 73 56 61 6c 69 64 20 3d 20 41 4c 57 41 59 53 IsValid = ALWAYS
1adfc 28 72 65 73 3d 3d 30 29 20 3f 31 3a 30 3b 0a 20 (res==0) ?1:0;.
1adfd 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 65 73 if( NEVER(res
1adfe 3c 30 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 <0) ){. rc
1adff 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 = sqlite3BtreeNe
1ae00 78 74 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26 xt(p->pCursor, &
1ae01 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 res);. if(
1ae02 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
1ae03 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c }.#ifdef SQL
1ae04 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c ITE_TEST. sql
1ae05 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e ite3_search_coun
1ae06 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 t++;.#endif.
1ae07 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 p->deferredMovet
1ae08 6f 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 63 61 o = 0;. p->ca
1ae09 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 cheStatus = CACH
1ae0a 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 6c 73 65 E_STALE;. }else
1ae0b 20 69 66 28 20 41 4c 57 41 59 53 28 70 2d 3e 70 if( ALWAYS(p->p
1ae0c 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 69 Cursor) ){. i
1ae0d 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a 20 20 20 nt hasMoved;.
1ae0e 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 int rc = sqlite
1ae0f 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d 3BtreeCursorHasM
1ae10 6f 76 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c oved(p->pCursor,
1ae11 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a 20 20 20 &hasMoved);.
1ae12 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
1ae13 20 72 63 3b 0a 20 20 20 20 69 66 28 20 68 61 73 rc;. if( has
1ae14 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20 20 20 70 Moved ){. p
1ae15 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1ae16 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 CACHE_STALE;.
1ae17 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 p->nullRow =
1ae18 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 1;. }. }. r
1ae19 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1ae1a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f .}../*.** The fo
1ae1b 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e llowing function
1ae1c 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 s:.**.** sqlite3
1ae1d 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 29 VdbeSerialType()
1ae1e 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 .** sqlite3VdbeS
1ae1f 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 29 0a 2a erialTypeLen().*
1ae20 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 * sqlite3VdbeSer
1ae21 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c 69 ialLen().** sqli
1ae22 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 te3VdbeSerialPut
1ae23 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 ().** sqlite3Vdb
1ae24 65 53 65 72 69 61 6c 47 65 74 28 29 0a 2a 2a 0a eSerialGet().**.
1ae25 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 65 20 74 ** encapsulate t
1ae26 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 65 72 he code that ser
1ae27 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 73 20 66 ializes values f
1ae28 6f 72 20 73 74 6f 72 61 67 65 20 69 6e 20 53 51 or storage in SQ
1ae29 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 61 6e 64 Lite.** data and
1ae2a 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 2e 20 index records.
1ae2b 45 61 63 68 20 73 65 72 69 61 6c 69 7a 65 64 20 Each serialized
1ae2c 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 73 20 6f value consists o
1ae2d 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 6c 2d 74 f a.** 'serial-t
1ae2e 79 70 65 27 20 61 6e 64 20 61 20 62 6c 6f 62 20 ype' and a blob
1ae2f 6f 66 20 64 61 74 61 2e 20 54 68 65 20 73 65 72 of data. The ser
1ae30 69 61 6c 20 74 79 70 65 20 69 73 20 61 6e 20 38 ial type is an 8
1ae31 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 0a 2a -byte unsigned.*
1ae32 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 6f 72 65 * integer, store
1ae33 64 20 61 73 20 61 20 76 61 72 69 6e 74 2e 0a 2a d as a varint..*
1ae34 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 4c 69 74 *.** In an SQLit
1ae35 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64 2c 20 e index record,
1ae36 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 the serial type
1ae37 69 73 20 73 74 6f 72 65 64 20 64 69 72 65 63 74 is stored direct
1ae38 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 ly before.** the
1ae39 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 74 68 blob of data th
1ae3a 61 74 20 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 at it correspond
1ae3b 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 62 6c 65 s to. In a table
1ae3c 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 73 65 72 record, all ser
1ae3d 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 61 72 65 ial.** types are
1ae3e 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 73 stored at the s
1ae3f 74 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f tart of the reco
1ae40 72 64 2c 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 rd, and the blob
1ae41 73 20 6f 66 20 64 61 74 61 20 61 74 0a 2a 2a 20 s of data at.**
1ae42 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 65 20 74 the end. Hence t
1ae43 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 hese functions a
1ae44 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c 65 72 20 llow the caller
1ae45 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a 2a to handle the.**
1ae46 20 73 65 72 69 61 6c 2d 74 79 70 65 20 61 6e 64 serial-type and
1ae47 20 64 61 74 61 20 62 6c 6f 62 20 73 65 70 65 72 data blob seper
1ae48 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ately..**.** The
1ae49 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 following table
1ae4a 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 76 describes the v
1ae4b 61 72 69 6f 75 73 20 73 74 6f 72 61 67 65 20 63 arious storage c
1ae4c 6c 61 73 73 65 73 20 66 6f 72 20 64 61 74 61 3a lasses for data:
1ae4d 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 61 6c 20 .**.** serial
1ae4e 74 79 70 65 20 20 20 20 20 20 20 20 62 79 74 65 type byte
1ae4f 73 20 6f 66 20 64 61 74 61 20 20 20 20 20 20 74 s of data t
1ae50 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d ype.** -------
1ae51 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d ------- ----
1ae52 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d ----------- -
1ae53 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a --------------.*
1ae54 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20 * 0
1ae55 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 0
1ae56 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 0a 2a NULL.*
1ae57 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20 * 1
1ae58 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20 20 1
1ae59 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 signed
1ae5a 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 integer.**
1ae5b 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2
1ae5c 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20 20 2
1ae5d 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 signed integ
1ae5e 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 20 20 20 er.** 3
1ae5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ae60 20 33 20 20 20 20 20 20 20 20 20 20 20 20 73 69 3 si
1ae61 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 gned integer.**
1ae62 20 20 20 20 20 34 20 20 20 20 20 20 20 20 20 20 4
1ae63 20 20 20 20 20 20 20 20 20 20 20 34 20 20 20 20 4
1ae64 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 signed i
1ae65 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 35 nteger.** 5
1ae66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ae67 20 20 20 20 20 36 20 20 20 20 20 20 20 20 20 20 6
1ae68 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 signed integer
1ae69 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 20 20 20 .** 6
1ae6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 8
1ae6b 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e sign
1ae6c 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 ed integer.**
1ae6d 20 20 20 37 20 20 20 20 20 20 20 20 20 20 20 20 7
1ae6e 20 20 20 20 20 20 20 20 20 38 20 20 20 20 20 20 8
1ae6f 20 20 20 20 20 20 49 45 45 45 20 66 6c 6f 61 74 IEEE float
1ae70 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20 20 .** 8
1ae71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
1ae72 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 Inte
1ae73 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 30 0a 2a ger constant 0.*
1ae74 2a 20 20 20 20 20 20 39 20 20 20 20 20 20 20 20 * 9
1ae75 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 0
1ae76 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 67 65 Intege
1ae77 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a 2a 2a 20 r constant 1.**
1ae78 20 20 20 20 31 30 2c 31 31 20 20 20 20 20 20 20 10,11
1ae79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ae7a 20 20 20 20 20 20 20 20 72 65 73 65 72 76 65 64 reserved
1ae7b 20 66 6f 72 20 65 78 70 61 6e 73 69 6f 6e 0a 2a for expansion.*
1ae7c 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e 64 20 65 * N>=12 and e
1ae7d 76 65 6e 20 20 20 20 20 20 20 28 4e 2d 31 32 29 ven (N-12)
1ae7e 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 42 0a 2a /2 BLOB.*
1ae7f 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e 64 20 6f * N>=13 and o
1ae80 64 64 20 20 20 20 20 20 20 20 28 4e 2d 31 33 29 dd (N-13)
1ae81 2f 32 20 20 20 20 20 20 20 20 74 65 78 74 0a 2a /2 text.*
1ae82 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e 64 20 39 *.** The 8 and 9
1ae83 20 74 79 70 65 73 20 77 65 72 65 20 61 64 64 65 types were adde
1ae84 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 69 6c 65 d in 3.3.0, file
1ae85 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 72 69 6f format 4. Prio
1ae86 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66 r versions.** of
1ae87 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 SQLite will not
1ae88 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 6f 73 understand thos
1ae89 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 2e 0a e serial types..
1ae8a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e */../*.** Return
1ae8b 20 74 68 65 20 73 65 72 69 61 6c 2d 74 79 70 65 the serial-type
1ae8c 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 73 for the value s
1ae8d 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d 2e 0a 2a tored in pMem..*
1ae8e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1ae8f 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 u32 sqlite3Vdbe
1ae90 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 20 2a SerialType(Mem *
1ae91 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66 pMem, int file_f
1ae92 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 20 66 6c ormat){. int fl
1ae93 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 ags = pMem->flag
1ae94 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 69 s;. int n;.. i
1ae95 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c f( flags&MEM_Nul
1ae96 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 l ){. return
1ae97 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 0;. }. if( fla
1ae98 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 gs&MEM_Int ){.
1ae99 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 /* Figure out
1ae9a 77 68 65 74 68 65 72 20 74 6f 20 75 73 65 20 31 whether to use 1
1ae9b 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 38 20 62 , 2, 4, 6 or 8 b
1ae9c 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 64 65 66 ytes. */.# def
1ae9d 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 20 28 28 ine MAX_6BYTE ((
1ae9e 28 28 69 36 34 29 30 78 30 30 30 30 38 30 30 30 ((i64)0x00008000
1ae9f 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 20 69 36 )<<32)-1). i6
1aea0 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 4 i = pMem->u.i;
1aea1 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 20 20 20 . u64 u;.
1aea2 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3e if( file_format>
1aea3 3d 34 20 26 26 20 28 69 26 31 29 3d 3d 69 20 29 =4 && (i&1)==i )
1aea4 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 38 {. return 8
1aea5 2b 28 75 33 32 29 69 3b 0a 20 20 20 20 7d 0a 20 +(u32)i;. }.
1aea6 20 20 20 75 20 3d 20 69 3c 30 20 3f 20 2d 69 20 u = i<0 ? -i
1aea7 3a 20 69 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d : i;. if( u<=
1aea8 31 32 37 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 127 ) return 1;.
1aea9 20 20 20 20 69 66 28 20 75 3c 3d 33 32 37 36 37 if( u<=32767
1aeaa 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 ) return 2;.
1aeab 20 69 66 28 20 75 3c 3d 38 33 38 38 36 30 37 20 if( u<=8388607
1aeac 29 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 20 ) return 3;.
1aead 69 66 28 20 75 3c 3d 32 31 34 37 34 38 33 36 34 if( u<=214748364
1aeae 37 20 29 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 7 ) return 4;.
1aeaf 20 20 69 66 28 20 75 3c 3d 4d 41 58 5f 36 42 59 if( u<=MAX_6BY
1aeb0 54 45 20 29 20 72 65 74 75 72 6e 20 35 3b 0a 20 TE ) return 5;.
1aeb1 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d return 6;. }
1aeb2 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d . if( flags&MEM
1aeb3 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 _Real ){. ret
1aeb4 75 72 6e 20 37 3b 0a 20 20 7d 0a 20 20 61 73 73 urn 7;. }. ass
1aeb5 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 2d 3e 6d ert( pMem->db->m
1aeb6 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 66 allocFailed || f
1aeb7 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 lags&(MEM_Str|ME
1aeb8 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e 20 3d M_Blob) );. n =
1aeb9 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66 28 20 pMem->n;. if(
1aeba 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f flags & MEM_Zero
1aebb 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 70 4d 65 ){. n += pMe
1aebc 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 7d 0a m->u.nZero;. }.
1aebd 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29 assert( n>=0 )
1aebe 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6e 2a 32 ;. return ((n*2
1aebf 29 20 2b 20 31 32 20 2b 20 28 28 66 6c 61 67 73 ) + 12 + ((flags
1aec0 26 4d 45 4d 5f 53 74 72 29 21 3d 30 29 29 3b 0a &MEM_Str)!=0));.
1aec1 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1aec2 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 the length of th
1aec3 65 20 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e e data correspon
1aec4 64 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70 ding to the supp
1aec5 6c 69 65 64 20 73 65 72 69 61 6c 2d 74 79 70 65 lied serial-type
1aec6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1aec7 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 ATE u32 sqlite3V
1aec8 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e dbeSerialTypeLen
1aec9 28 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 (u32 serial_type
1aeca 29 7b 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f ){. if( serial_
1aecb 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20 type>=12 ){.
1aecc 72 65 74 75 72 6e 20 28 73 65 72 69 61 6c 5f 74 return (serial_t
1aecd 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20 7d 65 6c ype-12)/2;. }el
1aece 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 se{. static c
1aecf 6f 6e 73 74 20 75 38 20 61 53 69 7a 65 5b 5d 20 onst u8 aSize[]
1aed0 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 = { 0, 1, 2, 3,
1aed1 34 2c 20 36 2c 20 38 2c 20 38 2c 20 30 2c 20 30 4, 6, 8, 8, 0, 0
1aed2 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20 20 72 65 , 0, 0 };. re
1aed3 74 75 72 6e 20 61 53 69 7a 65 5b 73 65 72 69 61 turn aSize[seria
1aed4 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d 0a 0a l_type];. }.}..
1aed5 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 /*.** If we are
1aed6 6f 6e 20 61 6e 20 61 72 63 68 69 74 65 63 74 75 on an architectu
1aed7 72 65 20 77 69 74 68 20 6d 69 78 65 64 2d 65 6e re with mixed-en
1aed8 64 69 61 6e 20 66 6c 6f 61 74 69 6e 67 20 0a 2a dian floating .*
1aed9 2a 20 70 6f 69 6e 74 73 20 28 65 78 3a 20 41 52 * points (ex: AR
1aeda 4d 37 29 20 74 68 65 6e 20 73 77 61 70 20 74 68 M7) then swap th
1aedb 65 20 6c 6f 77 65 72 20 34 20 62 79 74 65 73 20 e lower 4 bytes
1aedc 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 75 70 70 with the .** upp
1aedd 65 72 20 34 20 62 79 74 65 73 2e 20 20 52 65 74 er 4 bytes. Ret
1aede 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 2e 0a urn the result..
1aedf 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74 20 61 **.** For most a
1aee0 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74 68 rchitectures, th
1aee1 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a is is a no-op..*
1aee2 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 3a 20 20 49 *.** (later): I
1aee3 74 20 69 73 20 72 65 70 6f 72 74 65 64 20 74 6f t is reported to
1aee4 20 6d 65 20 74 68 61 74 20 74 68 65 20 6d 69 78 me that the mix
1aee5 65 64 2d 65 6e 64 69 61 6e 20 70 72 6f 62 6c 65 ed-endian proble
1aee6 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 20 69 73 20 m.** on ARM7 is
1aee7 61 6e 20 69 73 73 75 65 20 77 69 74 68 20 47 43 an issue with GC
1aee8 43 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65 20 C, not with the
1aee9 41 52 4d 37 20 63 68 69 70 2e 20 20 49 74 20 73 ARM7 chip. It s
1aeea 65 65 6d 73 0a 2a 2a 20 74 68 61 74 20 65 61 72 eems.** that ear
1aeeb 6c 79 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47 ly versions of G
1aeec 43 43 20 73 74 6f 72 65 64 20 74 68 65 20 74 77 CC stored the tw
1aeed 6f 20 77 6f 72 64 73 20 6f 66 20 61 20 36 34 2d o words of a 64-
1aeee 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 20 69 6e 20 bit.** float in
1aeef 74 68 65 20 77 72 6f 6e 67 20 6f 72 64 65 72 2e the wrong order.
1aef0 20 20 41 6e 64 20 74 68 61 74 20 65 72 72 6f 72 And that error
1aef1 20 68 61 73 20 62 65 65 6e 20 70 72 6f 70 61 67 has been propag
1aef2 61 74 65 64 0a 2a 2a 20 65 76 65 72 20 73 69 6e ated.** ever sin
1aef3 63 65 2e 20 20 54 68 65 20 62 6c 61 6d 65 20 69 ce. The blame i
1aef4 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c s not necessaril
1aef5 79 20 77 69 74 68 20 47 43 43 2c 20 74 68 6f 75 y with GCC, thou
1aef6 67 68 2e 0a 2a 2a 20 47 43 43 20 6d 69 67 68 74 gh..** GCC might
1aef7 20 68 61 76 65 20 6a 75 73 74 20 63 6f 70 79 69 have just copyi
1aef8 6e 67 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 66 ng the problem f
1aef9 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 6f 6d 70 rom a prior comp
1aefa 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 6d 20 61 6c iler..** I am al
1aefb 73 6f 20 74 6f 6c 64 20 74 68 61 74 20 6e 65 77 so told that new
1aefc 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47 er versions of G
1aefd 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 61 CC that follow a
1aefe 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 41 42 different.** AB
1aeff 49 20 67 65 74 20 74 68 65 20 62 79 74 65 20 6f I get the byte o
1af00 72 64 65 72 20 72 69 67 68 74 2e 0a 2a 2a 0a 2a rder right..**.*
1af01 2a 20 44 65 76 65 6c 6f 70 65 72 73 20 75 73 69 * Developers usi
1af02 6e 67 20 53 51 4c 69 74 65 20 6f 6e 20 61 6e 20 ng SQLite on an
1af03 41 52 4d 37 20 73 68 6f 75 6c 64 20 63 6f 6d 70 ARM7 should comp
1af04 69 6c 65 20 61 6e 64 20 72 75 6e 20 74 68 65 69 ile and run thei
1af05 72 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e r.** application
1af06 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f using -DSQLITE_
1af07 44 45 42 55 47 3d 31 20 61 74 20 6c 65 61 73 74 DEBUG=1 at least
1af08 20 6f 6e 63 65 2e 20 20 57 69 74 68 20 44 45 42 once. With DEB
1af09 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 73 UG.** enabled, s
1af0a 6f 6d 65 20 61 73 73 65 72 74 73 20 62 65 6c 6f ome asserts belo
1af0b 77 20 77 69 6c 6c 20 65 6e 73 75 72 65 20 74 68 w will ensure th
1af0c 61 74 20 74 68 65 20 62 79 74 65 20 6f 72 64 65 at the byte orde
1af0d 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69 6e 67 r of.** floating
1af0e 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 69 73 point values is
1af0f 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 correct..**.**
1af10 28 32 30 30 37 2d 30 38 2d 33 30 29 20 20 46 72 (2007-08-30) Fr
1af11 61 6e 6b 20 76 61 6e 20 56 75 67 74 20 68 61 73 ank van Vugt has
1af12 20 73 74 75 64 69 65 64 20 74 68 69 73 20 70 72 studied this pr
1af13 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c 79 0a 2a 2a oblem closely.**
1af14 20 61 6e 64 20 68 61 73 20 73 65 6e 64 20 68 69 and has send hi
1af15 73 20 66 69 6e 64 69 6e 67 73 20 74 6f 20 74 68 s findings to th
1af16 65 20 53 51 4c 69 74 65 20 64 65 76 65 6c 6f 70 e SQLite develop
1af17 65 72 73 2e 20 20 46 72 61 6e 6b 0a 2a 2a 20 77 ers. Frank.** w
1af18 72 69 74 65 73 20 74 68 61 74 20 73 6f 6d 65 20 rites that some
1af19 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 6f 66 Linux kernels of
1af1a 66 65 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 fer floating poi
1af1b 6e 74 20 68 61 72 64 77 61 72 65 0a 2a 2a 20 65 nt hardware.** e
1af1c 6d 75 6c 61 74 69 6f 6e 20 74 68 61 74 20 75 73 mulation that us
1af1d 65 73 20 6f 6e 6c 79 20 33 32 2d 62 69 74 20 6d es only 32-bit m
1af1e 61 6e 74 69 73 73 61 73 20 69 6e 73 74 65 61 64 antissas instead
1af1f 20 6f 66 20 61 20 66 75 6c 6c 20 0a 2a 2a 20 34 of a full .** 4
1af20 38 2d 62 69 74 73 20 61 73 20 72 65 71 75 69 72 8-bits as requir
1af21 65 64 20 62 79 20 74 68 65 20 49 45 45 45 20 73 ed by the IEEE s
1af22 74 61 6e 64 61 72 64 2e 20 20 28 54 68 69 73 20 tandard. (This
1af23 69 73 20 74 68 65 0a 2a 2a 20 43 4f 4e 46 49 47 is the.** CONFIG
1af24 5f 46 50 45 5f 46 41 53 54 46 50 45 20 6f 70 74 _FPE_FASTFPE opt
1af25 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 63 68 20 73 ion.) On such s
1af26 79 73 74 65 6d 73 2c 20 66 6c 6f 61 74 69 6e 67 ystems, floating
1af27 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 74 65 20 73 point.** byte s
1af28 77 61 70 70 69 6e 67 20 62 65 63 6f 6d 65 73 20 wapping becomes
1af29 76 65 72 79 20 63 6f 6d 70 6c 69 63 61 74 65 64 very complicated
1af2a 2e 20 20 54 6f 20 61 76 6f 69 64 20 70 72 6f 62 . To avoid prob
1af2b 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 20 6e 65 63 lems,.** the nec
1af2c 65 73 73 61 72 79 20 62 79 74 65 20 73 77 61 70 essary byte swap
1af2d 70 69 6e 67 20 69 73 20 63 61 72 72 69 65 64 20 ping is carried
1af2e 6f 75 74 20 75 73 69 6e 67 20 61 20 36 34 2d 62 out using a 64-b
1af2f 69 74 20 69 6e 74 65 67 65 72 0a 2a 2a 20 72 61 it integer.** ra
1af30 74 68 65 72 20 74 68 61 6e 20 61 20 36 34 2d 62 ther than a 64-b
1af31 69 74 20 66 6c 6f 61 74 2e 20 20 46 72 61 6e 6b it float. Frank
1af32 20 61 73 73 75 72 65 73 20 75 73 20 74 68 61 74 assures us that
1af33 20 74 68 65 20 63 6f 64 65 20 68 65 72 65 0a 2a the code here.*
1af34 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68 69 6d 2e * works for him.
1af35 20 20 57 65 2c 20 74 68 65 20 64 65 76 65 6c 6f We, the develo
1af36 70 65 72 73 2c 20 68 61 76 65 20 6e 6f 20 77 61 pers, have no wa
1af37 79 20 74 6f 20 69 6e 64 65 70 65 6e 64 65 6e 74 y to independent
1af38 6c 79 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 69 ly.** verify thi
1af39 73 2c 20 62 75 74 20 46 72 61 6e 6b 20 73 65 65 s, but Frank see
1af3a 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68 61 74 20 ms to know what
1af3b 68 65 20 69 73 20 74 61 6c 6b 69 6e 67 20 61 62 he is talking ab
1af3c 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 20 74 72 75 out.** so we tru
1af3d 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 69 66 64 65 st him..*/.#ifde
1af3e 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45 f SQLITE_MIXED_E
1af3f 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41 NDIAN_64BIT_FLOA
1af40 54 0a 73 74 61 74 69 63 20 75 36 34 20 66 6c 6f T.static u64 flo
1af41 61 74 53 77 61 70 28 75 36 34 20 69 6e 29 7b 0a atSwap(u64 in){.
1af42 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 75 36 union {. u6
1af43 34 20 72 3b 0a 20 20 20 20 75 33 32 20 69 5b 32 4 r;. u32 i[2
1af44 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 75 33 32 20 ];. } u;. u32
1af45 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 69 6e 3b 0a t;.. u.r = in;.
1af46 20 20 74 20 3d 20 75 2e 69 5b 30 5d 3b 0a 20 20 t = u.i[0];.
1af47 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b 31 5d 3b u.i[0] = u.i[1];
1af48 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74 3b 0a 20 . u.i[1] = t;.
1af49 20 72 65 74 75 72 6e 20 75 2e 72 3b 0a 7d 0a 23 return u.r;.}.#
1af4a 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78 65 define swapMixe
1af4b 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 20 dEndianFloat(X)
1af4c 20 58 20 3d 20 66 6c 6f 61 74 53 77 61 70 28 58 X = floatSwap(X
1af4d 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ).#else.# define
1af4e 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e swapMixedEndian
1af4f 46 6c 6f 61 74 28 58 29 0a 23 65 6e 64 69 66 0a Float(X).#endif.
1af50 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 ./*.** Write the
1af51 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74 61 serialized data
1af52 20 62 6c 6f 62 20 66 6f 72 20 74 68 65 20 76 61 blob for the va
1af53 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 70 4d lue stored in pM
1af54 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 62 75 66 2e em into .** buf.
1af55 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 It is assumed t
1af56 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68 hat the caller h
1af57 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 73 75 66 as allocated suf
1af58 66 69 63 69 65 6e 74 20 73 70 61 63 65 2e 0a 2a ficient space..*
1af59 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
1af5a 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 72 69 ber of bytes wri
1af5b 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e 42 75 66 tten..**.** nBuf
1af5c 20 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f is the amount o
1af5d 66 20 73 70 61 63 65 20 6c 65 66 74 20 69 6e 20 f space left in
1af5e 62 75 66 5b 5d 2e 20 20 6e 42 75 66 20 6d 75 73 buf[]. nBuf mus
1af5f 74 20 61 6c 77 61 79 73 20 62 65 0a 2a 2a 20 6c t always be.** l
1af60 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 arge enough to h
1af61 6f 6c 64 20 74 68 65 20 65 6e 74 69 72 65 20 66 old the entire f
1af62 69 65 6c 64 2e 20 20 45 78 63 65 70 74 2c 20 69 ield. Except, i
1af63 66 20 74 68 65 20 66 69 65 6c 64 20 69 73 0a 2a f the field is.*
1af64 2a 20 61 20 62 6c 6f 62 20 77 69 74 68 20 61 20 * a blob with a
1af65 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c zero-filled tail
1af66 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20 6d 69 67 , then buf[] mig
1af67 68 74 20 62 65 20 6a 75 73 74 20 74 68 65 20 72 ht be just the r
1af68 69 67 68 74 0a 2a 2a 20 73 69 7a 65 20 74 6f 20 ight.** size to
1af69 68 6f 6c 64 20 65 76 65 72 79 74 68 69 6e 67 20 hold everything
1af6a 65 78 63 65 70 74 20 66 6f 72 20 74 68 65 20 7a except for the z
1af6b 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e ero-filled tail.
1af6c 20 20 49 66 20 62 75 66 5b 5d 0a 2a 2a 20 69 73 If buf[].** is
1af6d 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f 75 67 68 only big enough
1af6e 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e 6f 6e to hold the non
1af6f 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c 20 74 68 -zero prefix, th
1af70 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65 20 74 68 en only write th
1af71 61 74 0a 2a 2a 20 70 72 65 66 69 78 20 69 6e 74 at.** prefix int
1af72 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74 20 69 66 o buf[]. But if
1af73 20 62 75 66 5b 5d 20 69 73 20 6c 61 72 67 65 20 buf[] is large
1af74 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 62 enough to hold b
1af75 6f 74 68 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 oth the.** prefi
1af76 78 20 61 6e 64 20 74 68 65 20 74 61 69 6c 20 74 x and the tail t
1af77 68 65 6e 20 77 72 69 74 65 20 74 68 65 20 70 72 hen write the pr
1af78 65 66 69 78 20 61 6e 64 20 73 65 74 20 74 68 65 efix and set the
1af79 20 74 61 69 6c 20 74 6f 20 61 6c 6c 0a 2a 2a 20 tail to all.**
1af7a 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 zeros..**.** Ret
1af7b 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1af7c 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 f bytes actually
1af7d 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 62 75 written into bu
1af7e 66 5b 5d 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 f[]. The number
1af7f 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 69 6e 20 .** of bytes in
1af80 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 the zero-filled
1af81 74 61 69 6c 20 69 73 20 69 6e 63 6c 75 64 65 64 tail is included
1af82 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 in the return v
1af83 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66 20 alue only.** if
1af84 74 68 6f 73 65 20 62 79 74 65 73 20 77 65 72 65 those bytes were
1af85 20 7a 65 72 6f 65 64 20 69 6e 20 62 75 66 5b 5d zeroed in buf[]
1af86 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 ..*/ .SQLITE_PRI
1af87 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 VATE u32 sqlite3
1af88 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 75 38 VdbeSerialPut(u8
1af89 20 2a 62 75 66 2c 20 69 6e 74 20 6e 42 75 66 2c *buf, int nBuf,
1af8a 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 Mem *pMem, int
1af8b 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 file_format){.
1af8c 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 20 u32 serial_type
1af8d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 = sqlite3VdbeSer
1af8e 69 61 6c 54 79 70 65 28 70 4d 65 6d 2c 20 66 69 ialType(pMem, fi
1af8f 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 75 33 le_format);. u3
1af90 32 20 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 49 6e 74 2 len;.. /* Int
1af91 65 67 65 72 20 61 6e 64 20 52 65 61 6c 20 2a 2f eger and Real */
1af92 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 . if( serial_ty
1af93 70 65 3c 3d 37 20 26 26 20 73 65 72 69 61 6c 5f pe<=7 && serial_
1af94 74 79 70 65 3e 30 20 29 7b 0a 20 20 20 20 75 36 type>0 ){. u6
1af95 34 20 76 3b 0a 20 20 20 20 75 33 32 20 69 3b 0a 4 v;. u32 i;.
1af96 20 20 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 if( serial_t
1af97 79 70 65 3d 3d 37 20 29 7b 0a 20 20 20 20 20 20 ype==7 ){.
1af98 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 76 assert( sizeof(v
1af99 29 3d 3d 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e )==sizeof(pMem->
1af9a 72 29 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 r) );. memc
1af9b 70 79 28 26 76 2c 20 26 70 4d 65 6d 2d 3e 72 2c py(&v, &pMem->r,
1af9c 20 73 69 7a 65 6f 66 28 76 29 29 3b 0a 20 20 20 sizeof(v));.
1af9d 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 swapMixedEndi
1af9e 61 6e 46 6c 6f 61 74 28 76 29 3b 0a 20 20 20 20 anFloat(v);.
1af9f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76 20 3d }else{. v =
1afa0 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 pMem->u.i;.
1afa1 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 69 20 3d 20 }. len = i =
1afa2 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1afa3 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f lTypeLen(serial_
1afa4 74 79 70 65 29 3b 0a 20 20 20 20 61 73 73 65 72 type);. asser
1afa5 74 28 20 6c 65 6e 3c 3d 28 75 33 32 29 6e 42 75 t( len<=(u32)nBu
1afa6 66 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 f );. while(
1afa7 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20 62 75 66 i-- ){. buf
1afa8 5b 69 5d 20 3d 20 28 75 38 29 28 76 26 30 78 46 [i] = (u8)(v&0xF
1afa9 46 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 F);. v >>=
1afaa 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 8;. }. ret
1afab 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 urn len;. }..
1afac 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 62 6c 6f /* String or blo
1afad 62 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61 b */. if( seria
1afae 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 l_type>=12 ){.
1afaf 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
1afb0 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 n + ((pMem->flag
1afb1 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29 3f 70 4d s & MEM_Zero)?pM
1afb2 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3a 30 29 0a 20 em->u.nZero:0).
1afb3 20 20 20 20 20 20 20 20 20 20 20 20 3d 3d 20 28 == (
1afb4 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62 65 53 int)sqlite3VdbeS
1afb5 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 erialTypeLen(ser
1afb6 69 61 6c 5f 74 79 70 65 29 20 29 3b 0a 20 20 20 ial_type) );.
1afb7 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e assert( pMem->n
1afb8 3c 3d 6e 42 75 66 20 29 3b 0a 20 20 20 20 6c 65 <=nBuf );. le
1afb9 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 20 n = pMem->n;.
1afba 20 6d 65 6d 63 70 79 28 62 75 66 2c 20 70 4d 65 memcpy(buf, pMe
1afbb 6d 2d 3e 7a 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 m->z, len);.
1afbc 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 if( pMem->flags
1afbd 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 & MEM_Zero ){.
1afbe 20 20 20 20 6c 65 6e 20 2b 3d 20 70 4d 65 6d 2d len += pMem-
1afbf 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 20 20 >u.nZero;.
1afc0 61 73 73 65 72 74 28 20 6e 42 75 66 3e 3d 30 20 assert( nBuf>=0
1afc1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 6e );. if( len
1afc2 20 3e 20 28 75 33 32 29 6e 42 75 66 20 29 7b 0a > (u32)nBuf ){.
1afc3 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 28 75 len = (u
1afc4 33 32 29 6e 42 75 66 3b 0a 20 20 20 20 20 20 7d 32)nBuf;. }
1afc5 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 62 . memset(&b
1afc6 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 uf[pMem->n], 0,
1afc7 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 len-pMem->n);.
1afc8 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6c }. return l
1afc9 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 55 en;. }.. /* NU
1afca 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e 74 73 20 LL or constants
1afcb 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72 65 74 75 0 or 1 */. retu
1afcc 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 rn 0;.}../*.** D
1afcd 65 73 65 72 69 61 6c 69 7a 65 20 74 68 65 20 64 eserialize the d
1afce 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e 74 65 64 ata blob pointed
1afcf 20 74 6f 20 62 79 20 62 75 66 20 61 73 20 73 65 to by buf as se
1afd0 72 69 61 6c 20 74 79 70 65 20 73 65 72 69 61 6c rial type serial
1afd1 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20 73 74 6f _type.** and sto
1afd2 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e re the result in
1afd3 20 70 4d 65 6d 2e 20 20 52 65 74 75 72 6e 20 74 pMem. Return t
1afd4 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
1afd5 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a 53 51 4c es read..*/ .SQL
1afd6 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 ITE_PRIVATE u32
1afd7 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1afd8 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74 20 75 6e lGet(. const un
1afd9 73 69 67 6e 65 64 20 63 68 61 72 20 2a 62 75 66 signed char *buf
1afda 2c 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 , /* Buffer
1afdb 74 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 20 66 to deserialize f
1afdc 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 73 65 72 rom */. u32 ser
1afdd 69 61 6c 5f 74 79 70 65 2c 20 20 20 20 20 20 20 ial_type,
1afde 20 20 20 20 20 20 20 2f 2a 20 53 65 72 69 61 6c /* Serial
1afdf 20 74 79 70 65 20 74 6f 20 64 65 73 65 72 69 61 type to deseria
1afe0 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 lize */. Mem *p
1afe1 4d 65 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 Mem
1afe2 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 /* Memor
1afe3 79 20 63 65 6c 6c 20 74 6f 20 77 72 69 74 65 20 y cell to write
1afe4 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f 0a 29 7b value into */.){
1afe5 0a 20 20 73 77 69 74 63 68 28 20 73 65 72 69 61 . switch( seria
1afe6 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 l_type ){. ca
1afe7 73 65 20 31 30 3a 20 20 20 2f 2a 20 52 65 73 65 se 10: /* Rese
1afe8 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 rved for future
1afe9 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 use */. case
1afea 31 31 3a 20 20 20 2f 2a 20 52 65 73 65 72 76 65 11: /* Reserve
1afeb 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65 d for future use
1afec 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 30 3a 20 */. case 0:
1afed 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a 20 20 { /* NULL */.
1afee 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
1afef 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 = MEM_Null;.
1aff0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1aff1 20 20 20 63 61 73 65 20 31 3a 20 7b 20 2f 2a 20 case 1: { /*
1aff2 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 1-byte signed in
1aff3 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 teger */. p
1aff4 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73 69 67 6e Mem->u.i = (sign
1aff5 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d 3b 0a ed char)buf[0];.
1aff6 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
1aff7 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
1aff8 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
1aff9 20 7d 0a 20 20 20 20 63 61 73 65 20 32 3a 20 7b }. case 2: {
1affa 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 /* 2-byte signe
1affb 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 d integer */.
1affc 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 pMem->u.i = (
1affd 28 28 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 ((signed char)bu
1affe 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b f[0])<<8) | buf[
1afff 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 1];. pMem->
1b000 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
1b001 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b . return 2;
1b002 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1b003 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74 65 20 73 3: { /* 3-byte s
1b004 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f igned integer */
1b005 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 . pMem->u.i
1b006 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 = (((signed cha
1b007 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36 29 20 7c r)buf[0])<<16) |
1b008 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20 7c 20 62 (buf[1]<<8) | b
1b009 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20 70 4d 65 uf[2];. pMe
1b00a 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 m->flags = MEM_I
1b00b 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e nt;. return
1b00c 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 3;. }. ca
1b00d 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d 62 79 74 se 4: { /* 4-byt
1b00e 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 e signed integer
1b00f 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e */. pMem->
1b010 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 u.i = (buf[0]<<2
1b011 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36 4) | (buf[1]<<16
1b012 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20 ) | (buf[2]<<8)
1b013 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20 | buf[3];.
1b014 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
1b015 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 M_Int;. ret
1b016 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 4;. }.
1b017 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a 20 36 2d case 5: { /* 6-
1b018 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 byte signed inte
1b019 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 ger */. u64
1b01a 20 78 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 x = (((signed c
1b01b 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 har)buf[0])<<8)
1b01c 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 | buf[1];.
1b01d 75 33 32 20 79 20 3d 20 28 62 75 66 5b 32 5d 3c u32 y = (buf[2]<
1b01e 3c 32 34 29 20 7c 20 28 62 75 66 5b 33 5d 3c 3c <24) | (buf[3]<<
1b01f 31 36 29 20 7c 20 28 62 75 66 5b 34 5d 3c 3c 38 16) | (buf[4]<<8
1b020 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20 20 20 20 ) | buf[5];.
1b021 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c 20 x = (x<<32) |
1b022 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 y;. pMem->u
1b023 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a .i = *(i64*)&x;.
1b024 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
1b025 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
1b026 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 20 return 6;.
1b027 20 7d 0a 20 20 20 20 63 61 73 65 20 36 3a 20 20 }. case 6:
1b028 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 6e 65 /* 8-byte signe
1b029 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 d integer */.
1b02a 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a 20 49 45 case 7: { /* IE
1b02b 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e EE floating poin
1b02c 74 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 78 t */. u64 x
1b02d 3b 0a 20 20 20 20 20 20 75 33 32 20 79 3b 0a 23 ;. u32 y;.#
1b02e 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 if !defined(NDEB
1b02f 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 UG) && !defined(
1b030 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
1b031 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 20 20 20 TING_POINT).
1b032 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 /* Verify that
1b033 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 66 6c integers and fl
1b034 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c oating point val
1b035 75 65 73 20 75 73 65 20 74 68 65 20 73 61 6d 65 ues use the same
1b036 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 20 6f . ** byte o
1b037 72 64 65 72 2e 20 20 4f 72 2c 20 74 68 61 74 20 rder. Or, that
1b038 69 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f if SQLITE_MIXED_
1b039 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f ENDIAN_64BIT_FLO
1b03a 41 54 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 64 AT is. ** d
1b03b 65 66 69 6e 65 64 20 74 68 61 74 20 36 34 2d 62 efined that 64-b
1b03c 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e it floating poin
1b03d 74 20 76 61 6c 75 65 73 20 72 65 61 6c 6c 79 20 t values really
1b03e 61 72 65 20 6d 69 78 65 64 0a 20 20 20 20 20 20 are mixed.
1b03f 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20 20 20 20 ** endian..
1b040 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63 */. static
1b041 20 63 6f 6e 73 74 20 75 36 34 20 74 31 20 3d 20 const u64 t1 =
1b042 28 28 75 36 34 29 30 78 33 66 66 30 30 30 30 30 ((u64)0x3ff00000
1b043 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20 73 74 61 )<<32;. sta
1b044 74 69 63 20 63 6f 6e 73 74 20 64 6f 75 62 6c 65 tic const double
1b045 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20 r1 = 1.0;.
1b046 20 75 36 34 20 74 32 20 3d 20 74 31 3b 0a 20 20 u64 t2 = t1;.
1b047 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 swapMixedEnd
1b048 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b 0a 20 20 ianFloat(t2);.
1b049 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 assert( size
1b04a 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f 66 28 74 of(r1)==sizeof(t
1b04b 32 29 20 26 26 20 6d 65 6d 63 6d 70 28 26 72 31 2) && memcmp(&r1
1b04c 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66 28 72 31 , &t2, sizeof(r1
1b04d 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a ))==0 );.#endif.
1b04e 0a 20 20 20 20 20 20 78 20 3d 20 28 62 75 66 5b . x = (buf[
1b04f 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 0]<<24) | (buf[1
1b050 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d ]<<16) | (buf[2]
1b051 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 <<8) | buf[3];.
1b052 20 20 20 20 20 79 20 3d 20 28 62 75 66 5b 34 5d y = (buf[4]
1b053 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 35 5d 3c <<24) | (buf[5]<
1b054 3c 31 36 29 20 7c 20 28 62 75 66 5b 36 5d 3c 3c <16) | (buf[6]<<
1b055 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a 20 20 20 8) | buf[7];.
1b056 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c x = (x<<32) |
1b057 20 79 3b 0a 20 20 20 20 20 20 69 66 28 20 73 65 y;. if( se
1b058 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20 29 7b 0a rial_type==6 ){.
1b059 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e pMem->u.
1b05a 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a 20 i = *(i64*)&x;.
1b05b 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 pMem->fla
1b05c 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 gs = MEM_Int;.
1b05d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1b05e 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f assert( sizeo
1b05f 66 28 78 29 3d 3d 38 20 26 26 20 73 69 7a 65 6f f(x)==8 && sizeo
1b060 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38 20 29 3b f(pMem->r)==8 );
1b061 0a 20 20 20 20 20 20 20 20 73 77 61 70 4d 69 78 . swapMix
1b062 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 78 29 edEndianFloat(x)
1b063 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 ;. memcpy
1b064 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78 2c 20 73 (&pMem->r, &x, s
1b065 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 20 izeof(x));.
1b066 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1b067 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 70 4d sqlite3IsNaN(pM
1b068 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f 4e 75 6c em->r) ? MEM_Nul
1b069 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 l : MEM_Real;.
1b06a 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 }. retu
1b06b 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 8;. }.
1b06c 63 61 73 65 20 38 3a 20 20 20 20 2f 2a 20 49 6e case 8: /* In
1b06d 74 65 67 65 72 20 30 20 2a 2f 0a 20 20 20 20 63 teger 0 */. c
1b06e 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20 49 6e 74 ase 9: { /* Int
1b06f 65 67 65 72 20 31 20 2a 2f 0a 20 20 20 20 20 20 eger 1 */.
1b070 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 65 72 69 pMem->u.i = seri
1b071 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20 20 20 20 al_type-8;.
1b072 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
1b073 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 EM_Int;. re
1b074 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 turn 0;. }.
1b075 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
1b076 20 20 20 75 33 32 20 6c 65 6e 20 3d 20 28 73 65 u32 len = (se
1b077 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32 3b rial_type-12)/2;
1b078 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d . pMem->z =
1b079 20 28 63 68 61 72 20 2a 29 62 75 66 3b 0a 20 20 (char *)buf;.
1b07a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6c 65 pMem->n = le
1b07b 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 78 n;. pMem->x
1b07c 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 Del = 0;. i
1b07d 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65 26 30 f( serial_type&0
1b07e 78 30 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70 x01 ){. p
1b07f 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
1b080 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70 68 65 6d _Str | MEM_Ephem
1b081 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1b082 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 pMem->fla
1b083 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 gs = MEM_Blob |
1b084 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 MEM_Ephem;.
1b085 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 }. return
1b086 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 len;. }. }.
1b087 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f return 0;.}.../
1b088 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e *.** Given the n
1b089 4b 65 79 2d 62 79 74 65 20 65 6e 63 6f 64 69 6e Key-byte encodin
1b08a 67 20 6f 66 20 61 20 72 65 63 6f 72 64 20 69 6e g of a record in
1b08b 20 70 4b 65 79 5b 5d 2c 20 70 61 72 73 65 20 74 pKey[], parse t
1b08c 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e 74 he.** record int
1b08d 6f 20 61 20 55 6e 70 61 63 6b 65 64 52 65 63 6f o a UnpackedReco
1b08e 72 64 20 73 74 72 75 63 74 75 72 65 2e 20 20 52 rd structure. R
1b08f 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
1b090 74 6f 0a 2a 2a 20 74 68 61 74 20 73 74 72 75 63 to.** that struc
1b091 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ture..**.** The
1b092 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e calling function
1b093 20 6d 69 67 68 74 20 70 72 6f 76 69 64 65 20 73 might provide s
1b094 7a 53 70 61 63 65 20 62 79 74 65 73 20 6f 66 20 zSpace bytes of
1b095 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 65 20 memory.** space
1b096 61 74 20 70 53 70 61 63 65 2e 20 20 54 68 69 73 at pSpace. This
1b097 20 73 70 61 63 65 20 63 61 6e 20 62 65 20 75 73 space can be us
1b098 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 ed to hold the r
1b099 65 74 75 72 6e 65 64 0a 2a 2a 20 56 44 62 65 50 eturned.** VDbeP
1b09a 61 72 73 65 64 52 65 63 6f 72 64 20 73 74 72 75 arsedRecord stru
1b09b 63 74 75 72 65 20 69 66 20 69 74 20 69 73 20 6c cture if it is l
1b09c 61 72 67 65 20 65 6e 6f 75 67 68 2e 20 20 49 66 arge enough. If
1b09d 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 62 69 it is.** not bi
1b09e 67 20 65 6e 6f 75 67 68 2c 20 73 70 61 63 65 20 g enough, space
1b09f 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d is obtained from
1b0a0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
1b0a1 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 )..**.** The ret
1b0a2 75 72 6e 65 64 20 73 74 72 75 63 74 75 72 65 20 urned structure
1b0a3 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f 73 65 64 should be closed
1b0a4 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a by a call to.**
1b0a5 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 sqlite3VdbeDele
1b0a6 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 teUnpackedRecord
1b0a7 28 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 ()..*/ .SQLITE_P
1b0a8 52 49 56 41 54 45 20 55 6e 70 61 63 6b 65 64 52 RIVATE UnpackedR
1b0a9 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 33 56 64 ecord *sqlite3Vd
1b0aa 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 0a beRecordUnpack(.
1b0ab 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 KeyInfo *pKeyI
1b0ac 6e 66 6f 2c 20 20 20 20 20 2f 2a 20 49 6e 66 6f nfo, /* Info
1b0ad 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 rmation about th
1b0ae 65 20 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 20 e record format
1b0af 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 */. int nKey,
1b0b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
1b0b1 69 7a 65 20 6f 66 20 74 68 65 20 62 69 6e 61 72 ize of the binar
1b0b2 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f y record */. co
1b0b3 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 nst void *pKey,
1b0b4 20 20 20 20 20 2f 2a 20 54 68 65 20 62 69 6e 61 /* The bina
1b0b5 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 ry record */. c
1b0b6 68 61 72 20 2a 70 53 70 61 63 65 2c 20 20 20 20 har *pSpace,
1b0b7 20 20 20 20 20 20 2f 2a 20 55 6e 61 6c 69 67 6e /* Unalign
1b0b8 65 64 20 73 70 61 63 65 20 61 76 61 69 6c 61 62 ed space availab
1b0b9 6c 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6f le to hold the o
1b0ba 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 73 bject */. int s
1b0bb 7a 53 70 61 63 65 20 20 20 20 20 20 20 20 20 20 zSpace
1b0bc 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 53 70 /* Size of pSp
1b0bd 61 63 65 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a ace[] in bytes *
1b0be 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 /.){. const uns
1b0bf 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79 igned char *aKey
1b0c0 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e = (const unsign
1b0c1 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a ed char *)pKey;.
1b0c2 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
1b0c3 20 2a 70 3b 20 20 2f 2a 20 54 68 65 20 75 6e 70 *p; /* The unp
1b0c4 61 63 6b 65 64 20 72 65 63 6f 72 64 20 74 68 61 acked record tha
1b0c5 74 20 77 65 20 77 69 6c 6c 20 72 65 74 75 72 6e t we will return
1b0c6 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b */. int nByte;
1b0c7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d /* Mem
1b0c8 6f 72 79 20 73 70 61 63 65 20 6e 65 65 64 65 64 ory space needed
1b0c9 20 74 6f 20 68 6f 6c 64 20 70 2c 20 69 6e 20 62 to hold p, in b
1b0ca 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 64 3b ytes */. int d;
1b0cb 0a 20 20 75 33 32 20 69 64 78 3b 0a 20 20 75 31 . u32 idx;. u1
1b0cc 36 20 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 6 u;
1b0cd 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f /* Unsigned lo
1b0ce 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
1b0cf 75 33 32 20 73 7a 48 64 72 3b 0a 20 20 4d 65 6d u32 szHdr;. Mem
1b0d0 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 20 6e 4f *pMem;. int nO
1b0d1 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ff; /*
1b0d2 20 49 6e 63 72 65 61 73 65 20 70 53 70 61 63 65 Increase pSpace
1b0d3 20 62 79 20 74 68 69 73 20 6d 75 63 68 20 74 6f by this much to
1b0d4 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 20 69 74 8-byte align it
1b0d5 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a */. . /*. **
1b0d6 20 57 65 20 77 61 6e 74 20 74 6f 20 73 68 69 66 We want to shif
1b0d7 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 70 53 t the pointer pS
1b0d8 70 61 63 65 20 75 70 20 73 75 63 68 20 74 68 61 pace up such tha
1b0d9 74 20 69 74 20 69 73 20 38 2d 62 79 74 65 20 61 t it is 8-byte a
1b0da 6c 69 67 6e 65 64 2e 0a 20 20 2a 2a 20 54 68 75 ligned.. ** Thu
1b0db 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 63 61 s, we need to ca
1b0dc 6c 63 75 6c 61 74 65 20 61 20 76 61 6c 75 65 2c lculate a value,
1b0dd 20 6e 4f 66 66 2c 20 62 65 74 77 65 65 6e 20 30 nOff, between 0
1b0de 20 61 6e 64 20 37 2c 20 74 6f 20 73 68 69 66 74 and 7, to shift
1b0df 20 0a 20 20 2a 2a 20 69 74 20 62 79 2e 20 20 49 . ** it by. I
1b0e0 66 20 70 53 70 61 63 65 20 69 73 20 61 6c 72 65 f pSpace is alre
1b0e1 61 64 79 20 38 2d 62 79 74 65 20 61 6c 69 67 6e ady 8-byte align
1b0e2 65 64 2c 20 6e 4f 66 66 20 73 68 6f 75 6c 64 20 ed, nOff should
1b0e3 62 65 20 7a 65 72 6f 2e 0a 20 20 2a 2f 0a 20 20 be zero.. */.
1b0e4 6e 4f 66 66 20 3d 20 28 38 20 2d 20 28 53 51 4c nOff = (8 - (SQL
1b0e5 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 ITE_PTR_TO_INT(p
1b0e6 53 70 61 63 65 29 20 26 20 37 29 29 20 26 20 37 Space) & 7)) & 7
1b0e7 3b 0a 20 20 70 53 70 61 63 65 20 2b 3d 20 6e 4f ;. pSpace += nO
1b0e8 66 66 3b 0a 20 20 73 7a 53 70 61 63 65 20 2d 3d ff;. szSpace -=
1b0e9 20 6e 4f 66 66 3b 0a 20 20 6e 42 79 74 65 20 3d nOff;. nByte =
1b0ea 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 ROUND8(sizeof(U
1b0eb 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29 20 npackedRecord))
1b0ec 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 28 70 + sizeof(Mem)*(p
1b0ed 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b KeyInfo->nField+
1b0ee 31 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 1);. if( nByte>
1b0ef 73 7a 53 70 61 63 65 20 29 7b 0a 20 20 20 20 70 szSpace ){. p
1b0f0 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
1b0f1 6f 63 52 61 77 28 70 4b 65 79 49 6e 66 6f 2d 3e ocRaw(pKeyInfo->
1b0f2 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 db, nByte);.
1b0f3 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
1b0f4 6e 20 30 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 n 0;. p->flag
1b0f5 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 s = UNPACKED_NEE
1b0f6 44 5f 46 52 45 45 20 7c 20 55 4e 50 41 43 4b 45 D_FREE | UNPACKE
1b0f7 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b 0a D_NEED_DESTROY;.
1b0f8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d }else{. p =
1b0f9 20 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 (UnpackedRecord
1b0fa 2a 29 70 53 70 61 63 65 3b 0a 20 20 20 20 70 2d *)pSpace;. p-
1b0fb 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 >flags = UNPACKE
1b0fc 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b 0a D_NEED_DESTROY;.
1b0fd 20 20 7d 0a 20 20 70 2d 3e 70 4b 65 79 49 6e 66 }. p->pKeyInf
1b0fe 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 o = pKeyInfo;.
1b0ff 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79 p->nField = pKey
1b100 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 Info->nField + 1
1b101 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 70 4d ;. p->aMem = pM
1b102 65 6d 20 3d 20 28 4d 65 6d 2a 29 26 28 28 63 68 em = (Mem*)&((ch
1b103 61 72 2a 29 70 29 5b 52 4f 55 4e 44 38 28 73 69 ar*)p)[ROUND8(si
1b104 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63 zeof(UnpackedRec
1b105 6f 72 64 29 29 5d 3b 0a 20 20 61 73 73 65 72 74 ord))];. assert
1b106 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 ( EIGHT_BYTE_ALI
1b107 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a GNMENT(pMem) );.
1b108 20 20 69 64 78 20 3d 20 67 65 74 56 61 72 69 6e idx = getVarin
1b109 74 33 32 28 61 4b 65 79 2c 20 73 7a 48 64 72 29 t32(aKey, szHdr)
1b10a 3b 0a 20 20 64 20 3d 20 73 7a 48 64 72 3b 0a 20 ;. d = szHdr;.
1b10b 20 75 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 u = 0;. while(
1b10c 20 69 64 78 3c 73 7a 48 64 72 20 26 26 20 75 3c idx<szHdr && u<
1b10d 70 2d 3e 6e 46 69 65 6c 64 20 26 26 20 64 3c 3d p->nField && d<=
1b10e 6e 4b 65 79 20 29 7b 0a 20 20 20 20 75 33 32 20 nKey ){. u32
1b10f 73 65 72 69 61 6c 5f 74 79 70 65 3b 0a 0a 20 20 serial_type;..
1b110 20 20 69 64 78 20 2b 3d 20 67 65 74 56 61 72 69 idx += getVari
1b111 6e 74 33 32 28 26 61 4b 65 79 5b 69 64 78 5d 2c nt32(&aKey[idx],
1b112 20 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 serial_type);.
1b113 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 70 pMem->enc = p
1b114 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20 KeyInfo->enc;.
1b115 20 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 70 4b 65 pMem->db = pKe
1b116 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20 20 70 yInfo->db;. p
1b117 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 30 3b 0a Mem->flags = 0;.
1b118 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f pMem->zMallo
1b119 63 20 3d 20 30 3b 0a 20 20 20 20 64 20 2b 3d 20 c = 0;. d +=
1b11a 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1b11b 6c 47 65 74 28 26 61 4b 65 79 5b 64 5d 2c 20 73 lGet(&aKey[d], s
1b11c 65 72 69 61 6c 5f 74 79 70 65 2c 20 70 4d 65 6d erial_type, pMem
1b11d 29 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 );. pMem++;.
1b11e 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20 61 73 u++;. }. as
1b11f 73 65 72 74 28 20 75 3c 3d 70 4b 65 79 49 6e 66 sert( u<=pKeyInf
1b120 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 20 29 3b o->nField + 1 );
1b121 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 75 . p->nField = u
1b122 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 ;. return (void
1b123 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 *)p;.}../*.** Th
1b124 69 73 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72 is routine destr
1b125 6f 79 73 20 61 20 55 6e 70 61 63 6b 65 64 52 65 oys a UnpackedRe
1b126 63 6f 72 64 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a cord object..*/.
1b127 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1b128 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 oid sqlite3VdbeD
1b129 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 eleteUnpackedRec
1b12a 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f ord(UnpackedReco
1b12b 72 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b rd *p){. int i;
1b12c 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 0a 20 . Mem *pMem;..
1b12d 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b assert( p!=0 );
1b12e 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c . assert( p->fl
1b12f 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e ags & UNPACKED_N
1b130 45 45 44 5f 44 45 53 54 52 4f 59 20 29 3b 0a 20 EED_DESTROY );.
1b131 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65 6d 3d 70 for(i=0, pMem=p
1b132 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e 6e 46 69 ->aMem; i<p->nFi
1b133 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65 6d 2b 2b eld; i++, pMem++
1b134 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 75 6e ){. /* The un
1b135 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20 69 73 packed record is
1b136 20 61 6c 77 61 79 73 20 63 6f 6e 73 74 72 75 63 always construc
1b137 74 65 64 20 62 79 20 74 68 65 0a 20 20 20 20 2a ted by the. *
1b138 2a 20 73 71 6c 69 74 65 33 56 64 62 65 55 6e 70 * sqlite3VdbeUnp
1b139 61 63 6b 52 65 63 6f 72 64 28 29 20 66 75 6e 63 ackRecord() func
1b13a 74 69 6f 6e 20 61 62 6f 76 65 2c 20 77 68 69 63 tion above, whic
1b13b 68 20 6d 61 6b 65 73 20 61 6c 6c 0a 20 20 20 20 h makes all.
1b13c 2a 2a 20 73 74 72 69 6e 67 73 20 61 6e 64 20 62 ** strings and b
1b13d 6c 6f 62 73 20 73 74 61 74 69 63 2e 20 20 41 6e lobs static. An
1b13e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 65 6c d none of the el
1b13f 65 6d 65 6e 74 73 20 61 72 65 0a 20 20 20 20 2a ements are. *
1b140 2a 20 65 76 65 72 20 74 72 61 6e 73 66 6f 72 6d * ever transform
1b141 65 64 2c 20 73 6f 20 74 68 65 72 65 20 69 73 20 ed, so there is
1b142 6e 65 76 65 72 20 61 6e 79 74 68 69 6e 67 20 74 never anything t
1b143 6f 20 64 65 6c 65 74 65 2e 0a 20 20 20 20 2a 2f o delete.. */
1b144 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 70 . if( NEVER(p
1b145 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 20 29 20 Mem->zMalloc) )
1b146 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
1b147 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 7d lease(pMem);. }
1b148 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 . if( p->flags
1b149 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f & UNPACKED_NEED_
1b14a 46 52 45 45 20 29 7b 0a 20 20 20 20 73 71 6c 69 FREE ){. sqli
1b14b 74 65 33 44 62 46 72 65 65 28 70 2d 3e 70 4b 65 te3DbFree(p->pKe
1b14c 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 29 3b 0a 20 yInfo->db, p);.
1b14d 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 }.}../*.** This
1b14e 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 70 61 72 function compar
1b14f 65 73 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 es the two table
1b150 20 72 6f 77 73 20 6f 72 20 69 6e 64 65 78 20 72 rows or index r
1b151 65 63 6f 72 64 73 0a 2a 2a 20 73 70 65 63 69 66 ecords.** specif
1b152 69 65 64 20 62 79 20 7b 6e 4b 65 79 31 2c 20 70 ied by {nKey1, p
1b153 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b 65 79 32 Key1} and pPKey2
1b154 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 . It returns a
1b155 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 0a 2a negative, zero.*
1b156 2a 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 6e * or positive in
1b157 74 65 67 65 72 20 69 66 20 6b 65 79 31 20 69 73 teger if key1 is
1b158 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 less than, equa
1b159 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67 72 65 61 l to or .** grea
1b15a 74 65 72 20 74 68 61 6e 20 6b 65 79 32 2e 20 20 ter than key2.
1b15b 54 68 65 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 79 The {nKey1, pKey
1b15c 31 7d 20 6b 65 79 20 6d 75 73 74 20 62 65 20 61 1} key must be a
1b15d 20 62 6c 6f 62 0a 2a 2a 20 63 72 65 61 74 65 64 blob.** created
1b15e 20 62 79 20 74 68 20 4f 50 5f 4d 61 6b 65 52 65 by th OP_MakeRe
1b15f 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66 20 74 cord opcode of t
1b160 68 65 20 56 44 42 45 2e 20 20 54 68 65 20 70 50 he VDBE. The pP
1b161 4b 65 79 32 0a 2a 2a 20 6b 65 79 20 6d 75 73 74 Key2.** key must
1b162 20 62 65 20 61 20 70 61 72 73 65 64 20 6b 65 79 be a parsed key
1b163 20 73 75 63 68 20 61 73 20 6f 62 74 61 69 6e 65 such as obtaine
1b164 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 d from.** sqlite
1b165 33 56 64 62 65 50 61 72 73 65 52 65 63 6f 72 64 3VdbeParseRecord
1b166 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79 31 20 61 6e 64 ..**.** Key1 and
1b167 20 4b 65 79 32 20 64 6f 20 6e 6f 74 20 68 61 76 Key2 do not hav
1b168 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 e to contain the
1b169 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 same number of
1b16a 66 69 65 6c 64 73 2e 0a 2a 2a 20 54 68 65 20 6b fields..** The k
1b16b 65 79 20 77 69 74 68 20 66 65 77 65 72 20 66 69 ey with fewer fi
1b16c 65 6c 64 73 20 69 73 20 75 73 75 61 6c 6c 79 20 elds is usually
1b16d 63 6f 6d 70 61 72 65 73 20 6c 65 73 73 20 74 68 compares less th
1b16e 61 6e 20 74 68 65 20 0a 2a 2a 20 6c 6f 6e 67 65 an the .** longe
1b16f 72 20 6b 65 79 2e 20 20 48 6f 77 65 76 65 72 20 r key. However
1b170 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f if the UNPACKED_
1b171 49 4e 43 52 4b 45 59 20 66 6c 61 67 73 20 69 6e INCRKEY flags in
1b172 20 70 50 4b 65 79 32 20 69 73 20 73 65 74 0a 2a pPKey2 is set.*
1b173 2a 20 61 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e * and the common
1b174 20 70 72 65 66 69 78 65 73 20 61 72 65 20 65 71 prefixes are eq
1b175 75 61 6c 2c 20 74 68 65 6e 20 6b 65 79 31 20 69 ual, then key1 i
1b176 73 20 6c 65 73 73 20 74 68 61 6e 20 6b 65 79 32 s less than key2
1b177 2e 0a 2a 2a 20 4f 72 20 69 66 20 74 68 65 20 55 ..** Or if the U
1b178 4e 50 41 43 4b 45 44 5f 4d 41 54 43 48 5f 50 52 NPACKED_MATCH_PR
1b179 45 46 49 58 20 66 6c 61 67 20 69 73 20 73 65 74 EFIX flag is set
1b17a 20 61 6e 64 20 74 68 65 20 70 72 65 66 69 78 65 and the prefixe
1b17b 73 20 61 72 65 0a 2a 2a 20 65 71 75 61 6c 2c 20 s are.** equal,
1b17c 74 68 65 6e 20 74 68 65 20 6b 65 79 73 20 61 72 then the keys ar
1b17d 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 e considered to
1b17e 62 65 20 65 71 75 61 6c 20 61 6e 64 0a 2a 2a 20 be equal and.**
1b17f 74 68 65 20 70 61 72 74 73 20 62 65 79 6f 6e 64 the parts beyond
1b180 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 the common pref
1b181 69 78 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a ix are ignored..
1b182 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 55 4e 50 **.** If the UNP
1b183 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 ACKED_IGNORE_ROW
1b184 49 44 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 ID flag is set,
1b185 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 62 79 then the last by
1b186 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 68 65 61 te of.** the hea
1b187 64 65 72 20 6f 66 20 70 4b 65 79 31 20 69 73 20 der of pKey1 is
1b188 69 67 6e 6f 72 65 64 2e 20 20 49 74 20 69 73 20 ignored. It is
1b189 61 73 73 75 6d 65 64 20 74 68 61 74 20 70 4b 65 assumed that pKe
1b18a 79 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 65 y1 is.** an inde
1b18b 78 20 6b 65 79 2c 20 61 6e 64 20 74 68 75 73 20 x key, and thus
1b18c 65 6e 64 73 20 77 69 74 68 20 61 20 72 6f 77 69 ends with a rowi
1b18d 64 20 76 61 6c 75 65 2e 20 20 54 68 65 20 6c 61 d value. The la
1b18e 73 74 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 st byte.** of th
1b18f 65 20 68 65 61 64 65 72 20 77 69 6c 6c 20 74 68 e header will th
1b190 65 72 65 66 6f 72 65 20 62 65 20 74 68 65 20 73 erefore be the s
1b191 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 erial type of th
1b192 65 20 72 6f 77 69 64 3a 0a 2a 2a 20 6f 6e 65 20 e rowid:.** one
1b193 6f 66 20 31 2c 20 32 2c 20 33 2c 20 34 2c 20 35 of 1, 2, 3, 4, 5
1b194 2c 20 36 2c 20 38 2c 20 6f 72 20 39 20 2d 20 74 , 6, 8, or 9 - t
1b195 68 65 20 69 6e 74 65 67 65 72 20 73 65 72 69 61 he integer seria
1b196 6c 20 74 79 70 65 73 2e 0a 2a 2a 20 54 68 65 20 l types..** The
1b197 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 serial type of t
1b198 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64 20 77 he final rowid w
1b199 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 61 20 ill always be a
1b19a 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20 single byte..**
1b19b 42 79 20 69 67 6e 6f 72 69 6e 67 20 74 68 69 73 By ignoring this
1b19c 20 6c 61 73 74 20 62 79 74 65 20 6f 66 20 74 68 last byte of th
1b19d 65 20 68 65 61 64 65 72 2c 20 77 65 20 66 6f 72 e header, we for
1b19e 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f ce the compariso
1b19f 6e 0a 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 20 74 n.** to ignore t
1b1a0 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 he rowid at the
1b1a1 65 6e 64 20 6f 66 20 6b 65 79 31 2e 0a 2a 2f 0a end of key1..*/.
1b1a2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1b1a3 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 nt sqlite3VdbeRe
1b1a4 63 6f 72 64 43 6f 6d 70 61 72 65 28 0a 20 20 69 cordCompare(. i
1b1a5 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20 nt nKey1, const
1b1a6 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 2f 2a 20 void *pKey1, /*
1b1a7 4c 65 66 74 20 6b 65 79 20 2a 2f 0a 20 20 55 6e Left key */. Un
1b1a8 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 50 packedRecord *pP
1b1a9 4b 65 79 32 20 20 20 20 20 20 20 20 2f 2a 20 52 Key2 /* R
1b1aa 69 67 68 74 20 6b 65 79 20 2a 2f 0a 29 7b 0a 20 ight key */.){.
1b1ab 20 69 6e 74 20 64 31 3b 20 20 20 20 20 20 20 20 int d1;
1b1ac 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
1b1ad 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78 to aKey[] of nex
1b1ae 74 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 20 2a t data element *
1b1af 2f 0a 20 20 75 33 32 20 69 64 78 31 3b 20 20 20 /. u32 idx1;
1b1b0 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
1b1b1 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 into aKey[] of
1b1b2 6e 65 78 74 20 68 65 61 64 65 72 20 65 6c 65 6d next header elem
1b1b3 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48 ent */. u32 szH
1b1b4 64 72 31 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e dr1; /* N
1b1b5 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
1b1b6 6e 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e n header */. in
1b1b7 74 20 69 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e t i = 0;. int n
1b1b8 46 69 65 6c 64 3b 0a 20 20 69 6e 74 20 72 63 20 Field;. int rc
1b1b9 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 = 0;. const uns
1b1ba 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79 igned char *aKey
1b1bb 31 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 1 = (const unsig
1b1bc 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79 31 ned char *)pKey1
1b1bd 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 ;. KeyInfo *pKe
1b1be 79 49 6e 66 6f 3b 0a 20 20 4d 65 6d 20 6d 65 6d yInfo;. Mem mem
1b1bf 31 3b 0a 0a 20 20 70 4b 65 79 49 6e 66 6f 20 3d 1;.. pKeyInfo =
1b1c0 20 70 50 4b 65 79 32 2d 3e 70 4b 65 79 49 6e 66 pPKey2->pKeyInf
1b1c1 6f 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63 20 3d 20 o;. mem1.enc =
1b1c2 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 pKeyInfo->enc;.
1b1c3 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b 65 79 49 mem1.db = pKeyI
1b1c4 6e 66 6f 2d 3e 64 62 3b 0a 20 20 6d 65 6d 31 2e nfo->db;. mem1.
1b1c5 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 6d 65 6d flags = 0;. mem
1b1c6 31 2e 75 2e 69 20 3d 20 30 3b 20 20 2f 2a 20 6e 1.u.i = 0; /* n
1b1c7 6f 74 20 6e 65 65 64 65 64 2c 20 68 65 72 65 20 ot needed, here
1b1c8 74 6f 20 73 69 6c 65 6e 63 65 20 63 6f 6d 70 69 to silence compi
1b1c9 6c 65 72 20 77 61 72 6e 69 6e 67 20 2a 2f 0a 20 ler warning */.
1b1ca 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 mem1.zMalloc =
1b1cb 30 3b 0a 20 20 0a 20 20 69 64 78 31 20 3d 20 67 0;. . idx1 = g
1b1cc 65 74 56 61 72 69 6e 74 33 32 28 61 4b 65 79 31 etVarint32(aKey1
1b1cd 2c 20 73 7a 48 64 72 31 29 3b 0a 20 20 64 31 20 , szHdr1);. d1
1b1ce 3d 20 73 7a 48 64 72 31 3b 0a 20 20 69 66 28 20 = szHdr1;. if(
1b1cf 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 pPKey2->flags &
1b1d0 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f UNPACKED_IGNORE_
1b1d1 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 73 7a 48 ROWID ){. szH
1b1d2 64 72 31 2d 2d 3b 0a 20 20 7d 0a 20 20 6e 46 69 dr1--;. }. nFi
1b1d3 65 6c 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e eld = pKeyInfo->
1b1d4 6e 46 69 65 6c 64 3b 0a 20 20 77 68 69 6c 65 28 nField;. while(
1b1d5 20 69 64 78 31 3c 73 7a 48 64 72 31 20 26 26 20 idx1<szHdr1 &&
1b1d6 69 3c 70 50 4b 65 79 32 2d 3e 6e 46 69 65 6c 64 i<pPKey2->nField
1b1d7 20 29 7b 0a 20 20 20 20 75 33 32 20 73 65 72 69 ){. u32 seri
1b1d8 61 6c 5f 74 79 70 65 31 3b 0a 0a 20 20 20 20 2f al_type1;.. /
1b1d9 2a 20 52 65 61 64 20 74 68 65 20 73 65 72 69 61 * Read the seria
1b1da 6c 20 74 79 70 65 73 20 66 6f 72 20 74 68 65 20 l types for the
1b1db 6e 65 78 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 next element in
1b1dc 65 61 63 68 20 6b 65 79 2e 20 2a 2f 0a 20 20 20 each key. */.
1b1dd 20 69 64 78 31 20 2b 3d 20 67 65 74 56 61 72 69 idx1 += getVari
1b1de 6e 74 33 32 28 20 61 4b 65 79 31 2b 69 64 78 31 nt32( aKey1+idx1
1b1df 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 31 20 29 , serial_type1 )
1b1e0 3b 0a 20 20 20 20 69 66 28 20 64 31 3e 3d 6e 4b ;. if( d1>=nK
1b1e1 65 79 31 20 26 26 20 73 71 6c 69 74 65 33 56 64 ey1 && sqlite3Vd
1b1e2 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 beSerialTypeLen(
1b1e3 73 65 72 69 61 6c 5f 74 79 70 65 31 29 3e 30 20 serial_type1)>0
1b1e4 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 2f 2a ) break;.. /*
1b1e5 20 45 78 74 72 61 63 74 20 74 68 65 20 76 61 6c Extract the val
1b1e6 75 65 73 20 74 6f 20 62 65 20 63 6f 6d 70 61 72 ues to be compar
1b1e7 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 ed.. */. d
1b1e8 31 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 1 += sqlite3Vdbe
1b1e9 53 65 72 69 61 6c 47 65 74 28 26 61 4b 65 79 31 SerialGet(&aKey1
1b1ea 5b 64 31 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 [d1], serial_typ
1b1eb 65 31 2c 20 26 6d 65 6d 31 29 3b 0a 0a 20 20 20 e1, &mem1);..
1b1ec 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 70 61 /* Do the compa
1b1ed 72 69 73 6f 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 rison. */.
1b1ee 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d rc = sqlite3Mem
1b1ef 43 6f 6d 70 61 72 65 28 26 6d 65 6d 31 2c 20 26 Compare(&mem1, &
1b1f0 70 50 4b 65 79 32 2d 3e 61 4d 65 6d 5b 69 5d 2c pPKey2->aMem[i],
1b1f1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1b1f2 20 20 20 20 20 20 20 20 20 20 20 20 69 3c 6e 46 i<nF
1b1f3 69 65 6c 64 20 3f 20 70 4b 65 79 49 6e 66 6f 2d ield ? pKeyInfo-
1b1f4 3e 61 43 6f 6c 6c 5b 69 5d 20 3a 20 30 29 3b 0a >aColl[i] : 0);.
1b1f5 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b if( rc!=0 ){
1b1f6 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1b1f7 20 20 7d 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d }. i++;. }
1b1f8 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 65 6d 6f 72 79 .. /* No memory
1b1f9 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 65 allocation is e
1b1fa 76 65 72 20 75 73 65 64 20 6f 6e 20 6d 65 6d 31 ver used on mem1
1b1fb 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 . */. if( NEVER
1b1fc 28 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 29 20 29 (mem1.zMalloc) )
1b1fd 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1b1fe 65 6c 65 61 73 65 28 26 6d 65 6d 31 29 3b 0a 0a elease(&mem1);..
1b1ff 20 20 2f 2a 20 49 66 20 74 68 65 20 50 52 45 46 /* If the PREF
1b200 49 58 5f 53 45 41 52 43 48 20 66 6c 61 67 20 69 IX_SEARCH flag i
1b201 73 20 73 65 74 20 61 6e 64 20 61 6c 6c 20 66 69 s set and all fi
1b202 65 6c 64 73 20 65 78 63 65 70 74 20 74 68 65 20 elds except the
1b203 66 69 6e 61 6c 0a 20 20 2a 2a 20 72 6f 77 69 64 final. ** rowid
1b204 20 66 69 65 6c 64 20 77 65 72 65 20 65 71 75 61 field were equa
1b205 6c 2c 20 74 68 65 6e 20 63 6c 65 61 72 20 74 68 l, then clear th
1b206 65 20 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 e PREFIX_SEARCH
1b207 66 6c 61 67 20 61 6e 64 20 73 65 74 20 0a 20 20 flag and set .
1b208 2a 2a 20 70 50 4b 65 79 32 2d 3e 72 6f 77 69 64 ** pPKey2->rowid
1b209 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 to the value of
1b20a 20 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c 64 the rowid field
1b20b 20 69 6e 20 28 70 4b 65 79 31 2c 20 6e 4b 65 79 in (pKey1, nKey
1b20c 31 29 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 1).. ** This is
1b20d 20 75 73 65 64 20 62 79 20 74 68 65 20 4f 50 5f used by the OP_
1b20e 49 73 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 2e IsUnique opcode.
1b20f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 50 4b . */. if( (pPK
1b210 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 ey2->flags & UNP
1b211 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 ACKED_PREFIX_SEA
1b212 52 43 48 29 20 26 26 20 69 3d 3d 28 70 50 4b 65 RCH) && i==(pPKe
1b213 79 32 2d 3e 6e 46 69 65 6c 64 2d 31 29 20 29 7b y2->nField-1) ){
1b214 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 . assert( idx
1b215 31 3d 3d 73 7a 48 64 72 31 20 26 26 20 72 63 20 1==szHdr1 && rc
1b216 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d );. assert( m
1b217 65 6d 31 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em1.flags & MEM_
1b218 49 6e 74 20 29 3b 0a 20 20 20 20 70 50 4b 65 79 Int );. pPKey
1b219 32 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 55 4e 50 2->flags &= ~UNP
1b21a 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 ACKED_PREFIX_SEA
1b21b 52 43 48 3b 0a 20 20 20 20 70 50 4b 65 79 32 2d RCH;. pPKey2-
1b21c 3e 72 6f 77 69 64 20 3d 20 6d 65 6d 31 2e 75 2e >rowid = mem1.u.
1b21d 69 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 i;. }.. if( rc
1b21e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 72 63 ==0 ){. /* rc
1b21f 3d 3d 30 20 68 65 72 65 20 6d 65 61 6e 73 20 74 ==0 here means t
1b220 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 6b hat one of the k
1b221 65 79 73 20 72 61 6e 20 6f 75 74 20 6f 66 20 66 eys ran out of f
1b222 69 65 6c 64 73 20 61 6e 64 0a 20 20 20 20 2a 2a ields and. **
1b223 20 61 6c 6c 20 74 68 65 20 66 69 65 6c 64 73 20 all the fields
1b224 75 70 20 74 6f 20 74 68 61 74 20 70 6f 69 6e 74 up to that point
1b225 20 77 65 72 65 20 65 71 75 61 6c 2e 20 49 66 20 were equal. If
1b226 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 the UNPACKED_INC
1b227 52 4b 45 59 0a 20 20 20 20 2a 2a 20 66 6c 61 67 RKEY. ** flag
1b228 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 62 72 is set, then br
1b229 65 61 6b 20 74 68 65 20 74 69 65 20 62 79 20 74 eak the tie by t
1b22a 72 65 61 74 69 6e 67 20 6b 65 79 32 20 61 73 20 reating key2 as
1b22b 6c 61 72 67 65 72 2e 0a 20 20 20 20 2a 2a 20 49 larger.. ** I
1b22c 66 20 74 68 65 20 55 50 41 43 4b 45 44 5f 50 52 f the UPACKED_PR
1b22d 45 46 49 58 5f 4d 41 54 43 48 20 66 6c 61 67 20 EFIX_MATCH flag
1b22e 69 73 20 73 65 74 2c 20 74 68 65 6e 20 6b 65 79 is set, then key
1b22f 73 20 77 69 74 68 20 63 6f 6d 6d 6f 6e 20 70 72 s with common pr
1b230 65 66 69 78 65 73 0a 20 20 20 20 2a 2a 20 61 72 efixes. ** ar
1b231 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 e considered to
1b232 62 65 20 65 71 75 61 6c 2e 20 20 4f 74 68 65 72 be equal. Other
1b233 77 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67 65 72 wise, the longer
1b234 20 6b 65 79 20 69 73 20 74 68 65 20 0a 20 20 20 key is the .
1b235 20 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41 73 20 ** larger. As
1b236 69 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20 it happens, the
1b237 70 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c 77 61 pPKey2 will alwa
1b238 79 73 20 62 65 20 74 68 65 20 6c 6f 6e 67 65 72 ys be the longer
1b239 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65 72 65 . ** if there
1b23a 20 69 73 20 61 20 64 69 66 66 65 72 65 6e 63 65 is a difference
1b23b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1b23c 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 pPKey2->flags &
1b23d 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 UNPACKED_INCRKE
1b23e 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 Y ){. rc =
1b23f 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 -1;. }else if
1b240 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 ( pPKey2->flags
1b241 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 & UNPACKED_PREFI
1b242 58 5f 4d 41 54 43 48 20 29 7b 0a 20 20 20 20 20 X_MATCH ){.
1b243 20 2f 2a 20 4c 65 61 76 65 20 72 63 3d 3d 30 20 /* Leave rc==0
1b244 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 */. }else if(
1b245 20 69 64 78 31 3c 73 7a 48 64 72 31 20 29 7b 0a idx1<szHdr1 ){.
1b246 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 rc = 1;.
1b247 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 }. }else if(
1b248 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f pKeyInfo->aSortO
1b249 72 64 65 72 20 26 26 20 69 3c 70 4b 65 79 49 6e rder && i<pKeyIn
1b24a 66 6f 2d 3e 6e 46 69 65 6c 64 0a 20 20 20 20 20 fo->nField.
1b24b 20 20 20 20 20 20 20 20 20 20 26 26 20 70 4b 65 && pKe
1b24c 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 yInfo->aSortOrde
1b24d 72 5b 69 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d r[i] ){. rc =
1b24e 20 2d 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 -rc;. }.. ret
1b24f 75 72 6e 20 72 63 3b 0a 7d 0a 20 0a 0a 2f 2a 0a urn rc;.}. ../*.
1b250 2a 2a 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61 ** pCur points a
1b251 74 20 61 6e 20 69 6e 64 65 78 20 65 6e 74 72 79 t an index entry
1b252 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 created using t
1b253 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 he OP_MakeRecord
1b254 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 52 65 61 64 opcode..** Read
1b255 20 74 68 65 20 72 6f 77 69 64 20 28 74 68 65 20 the rowid (the
1b256 6c 61 73 74 20 66 69 65 6c 64 20 69 6e 20 74 68 last field in th
1b257 65 20 72 65 63 6f 72 64 29 20 61 6e 64 20 73 74 e record) and st
1b258 6f 72 65 20 69 74 20 69 6e 20 2a 72 6f 77 69 64 ore it in *rowid
1b259 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 ..** Return SQLI
1b25a 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 TE_OK if everyth
1b25b 69 6e 67 20 77 6f 72 6b 73 2c 20 6f 72 20 61 6e ing works, or an
1b25c 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68 65 error code othe
1b25d 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 rwise..**.** pCu
1b25e 72 20 6d 69 67 68 74 20 62 65 20 70 6f 69 6e 74 r might be point
1b25f 69 6e 67 20 74 6f 20 74 65 78 74 20 6f 62 74 61 ing to text obta
1b260 69 6e 65 64 20 66 72 6f 6d 20 61 20 63 6f 72 72 ined from a corr
1b261 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c upt database fil
1b262 65 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 63 6f 6e e..** So the con
1b263 74 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 74 tent cannot be t
1b264 72 75 73 74 65 64 2e 20 20 44 6f 20 61 70 70 72 rusted. Do appr
1b265 6f 70 72 69 61 74 65 20 63 68 65 63 6b 73 20 6f opriate checks o
1b266 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 0a 2a n the content..*
1b267 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1b268 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1b269 49 64 78 52 6f 77 69 64 28 73 71 6c 69 74 65 33 IdxRowid(sqlite3
1b26a 20 2a 64 62 2c 20 42 74 43 75 72 73 6f 72 20 2a *db, BtCursor *
1b26b 70 43 75 72 2c 20 69 36 34 20 2a 72 6f 77 69 64 pCur, i64 *rowid
1b26c 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 ){. i64 nCellKe
1b26d 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b y = 0;. int rc;
1b26e 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 20 20 20 . u32 szHdr;
1b26f 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
1b270 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 the header */.
1b271 75 33 32 20 74 79 70 65 52 6f 77 69 64 3b 20 20 u32 typeRowid;
1b272 20 20 2f 2a 20 53 65 72 69 61 6c 20 74 79 70 65 /* Serial type
1b273 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f of the rowid */
1b274 0a 20 20 75 33 32 20 6c 65 6e 52 6f 77 69 64 3b . u32 lenRowid;
1b275 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
1b276 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 4d the rowid */. M
1b277 65 6d 20 6d 2c 20 76 3b 0a 0a 20 20 55 4e 55 53 em m, v;.. UNUS
1b278 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 62 29 ED_PARAMETER(db)
1b279 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 ;.. /* Get the
1b27a 73 69 7a 65 20 6f 66 20 74 68 65 20 69 6e 64 65 size of the inde
1b27b 78 20 65 6e 74 72 79 2e 20 20 4f 6e 6c 79 20 69 x entry. Only i
1b27c 6e 64 69 63 65 73 20 65 6e 74 72 69 65 73 20 6f ndices entries o
1b27d 66 20 6c 65 73 73 0a 20 20 2a 2a 20 74 68 61 6e f less. ** than
1b27e 20 32 47 69 42 20 61 72 65 20 73 75 70 70 6f 72 2GiB are suppor
1b27f 74 20 2d 20 61 6e 79 74 68 69 6e 67 20 6c 61 72 t - anything lar
1b280 67 65 20 6d 75 73 74 20 62 65 20 64 61 74 61 62 ge must be datab
1b281 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a ase corruption..
1b282 20 20 2a 2a 20 41 6e 79 20 63 6f 72 72 75 70 74 ** Any corrupt
1b283 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65 64 20 ion is detected
1b284 69 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 in sqlite3BtreeP
1b285 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c 20 74 arseCellPtr(), t
1b286 68 6f 75 67 68 2c 20 73 6f 0a 20 20 2a 2a 20 74 hough, so. ** t
1b287 68 69 73 20 63 6f 64 65 20 63 61 6e 20 73 61 66 his code can saf
1b288 65 6c 79 20 61 73 73 75 6d 65 20 74 68 61 74 20 ely assume that
1b289 6e 43 65 6c 6c 4b 65 79 20 69 73 20 33 32 2d 62 nCellKey is 32-b
1b28a 69 74 73 20 20 0a 20 20 2a 2f 0a 20 20 61 73 73 its . */. ass
1b28b 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1b28c 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 70 eCursorIsValid(p
1b28d 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 Cur) );. rc = s
1b28e 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 qlite3BtreeKeySi
1b28f 7a 65 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b ze(pCur, &nCellK
1b290 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 ey);. assert( r
1b291 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 c==SQLITE_OK );
1b292 20 20 20 20 2f 2a 20 70 43 75 72 20 69 73 20 61 /* pCur is a
1b293 6c 77 61 79 73 20 76 61 6c 69 64 20 73 6f 20 4b lways valid so K
1b294 65 79 53 69 7a 65 20 63 61 6e 6e 6f 74 20 66 61 eySize cannot fa
1b295 69 6c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 il */. assert(
1b296 28 6e 43 65 6c 6c 4b 65 79 20 26 20 53 51 4c 49 (nCellKey & SQLI
1b297 54 45 5f 4d 41 58 5f 55 33 32 29 3d 3d 28 75 36 TE_MAX_U32)==(u6
1b298 34 29 6e 43 65 6c 6c 4b 65 79 20 29 3b 0a 0a 20 4)nCellKey );..
1b299 20 2f 2a 20 52 65 61 64 20 69 6e 20 74 68 65 20 /* Read in the
1b29a 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74 complete content
1b29b 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e of the index en
1b29c 74 72 79 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 try */. memset(
1b29d 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29 &m, 0, sizeof(m)
1b29e 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
1b29f 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 3VdbeMemFromBtre
1b2a0 65 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29 e(pCur, 0, (int)
1b2a1 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 nCellKey, 1, &m)
1b2a2 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 ;. if( rc ){.
1b2a3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
1b2a4 0a 0a 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 .. /* The index
1b2a5 20 65 6e 74 72 79 20 6d 75 73 74 20 62 65 67 69 entry must begi
1b2a6 6e 20 77 69 74 68 20 61 20 68 65 61 64 65 72 20 n with a header
1b2a7 73 69 7a 65 20 2a 2f 0a 20 20 28 76 6f 69 64 29 size */. (void)
1b2a8 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a getVarint32((u8*
1b2a9 29 6d 2e 7a 2c 20 73 7a 48 64 72 29 3b 0a 20 20 )m.z, szHdr);.
1b2aa 74 65 73 74 63 61 73 65 28 20 73 7a 48 64 72 3d testcase( szHdr=
1b2ab 3d 33 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 =3 );. testcase
1b2ac 28 20 73 7a 48 64 72 3d 3d 6d 2e 6e 20 29 3b 0a ( szHdr==m.n );.
1b2ad 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 73 if( unlikely(s
1b2ae 7a 48 64 72 3c 33 20 7c 7c 20 28 69 6e 74 29 73 zHdr<3 || (int)s
1b2af 7a 48 64 72 3e 6d 2e 6e 29 20 29 7b 0a 20 20 20 zHdr>m.n) ){.
1b2b0 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f goto idx_rowid_
1b2b1 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a corruption;. }.
1b2b2 0a 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20 66 . /* The last f
1b2b3 69 65 6c 64 20 6f 66 20 74 68 65 20 69 6e 64 65 ield of the inde
1b2b4 78 20 73 68 6f 75 6c 64 20 62 65 20 61 6e 20 69 x should be an i
1b2b5 6e 74 65 67 65 72 20 2d 20 74 68 65 20 52 4f 57 nteger - the ROW
1b2b6 49 44 2e 0a 20 20 2a 2a 20 56 65 72 69 66 79 20 ID.. ** Verify
1b2b7 74 68 61 74 20 74 68 65 20 6c 61 73 74 20 65 6e that the last en
1b2b8 74 72 79 20 72 65 61 6c 6c 79 20 69 73 20 61 6e try really is an
1b2b9 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 20 20 28 integer. */. (
1b2ba 76 6f 69 64 29 67 65 74 56 61 72 69 6e 74 33 32 void)getVarint32
1b2bb 28 28 75 38 2a 29 26 6d 2e 7a 5b 73 7a 48 64 72 ((u8*)&m.z[szHdr
1b2bc 2d 31 5d 2c 20 74 79 70 65 52 6f 77 69 64 29 3b -1], typeRowid);
1b2bd 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 . testcase( typ
1b2be 65 52 6f 77 69 64 3d 3d 31 20 29 3b 0a 20 20 74 eRowid==1 );. t
1b2bf 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 estcase( typeRow
1b2c0 69 64 3d 3d 32 20 29 3b 0a 20 20 74 65 73 74 63 id==2 );. testc
1b2c1 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d ase( typeRowid==
1b2c2 33 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 3 );. testcase(
1b2c3 20 74 79 70 65 52 6f 77 69 64 3d 3d 34 20 29 3b typeRowid==4 );
1b2c4 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 . testcase( typ
1b2c5 65 52 6f 77 69 64 3d 3d 35 20 29 3b 0a 20 20 74 eRowid==5 );. t
1b2c6 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 estcase( typeRow
1b2c7 69 64 3d 3d 36 20 29 3b 0a 20 20 74 65 73 74 63 id==6 );. testc
1b2c8 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d ase( typeRowid==
1b2c9 38 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 8 );. testcase(
1b2ca 20 74 79 70 65 52 6f 77 69 64 3d 3d 39 20 29 3b typeRowid==9 );
1b2cb 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 . if( unlikely(
1b2cc 74 79 70 65 52 6f 77 69 64 3c 31 20 7c 7c 20 74 typeRowid<1 || t
1b2cd 79 70 65 52 6f 77 69 64 3e 39 20 7c 7c 20 74 79 ypeRowid>9 || ty
1b2ce 70 65 52 6f 77 69 64 3d 3d 37 29 20 29 7b 0a 20 peRowid==7) ){.
1b2cf 20 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 goto idx_rowi
1b2d0 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 d_corruption;.
1b2d1 7d 0a 20 20 6c 65 6e 52 6f 77 69 64 20 3d 20 73 }. lenRowid = s
1b2d2 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1b2d3 54 79 70 65 4c 65 6e 28 74 79 70 65 52 6f 77 69 TypeLen(typeRowi
1b2d4 64 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 d);. testcase(
1b2d5 28 75 33 32 29 6d 2e 6e 3d 3d 73 7a 48 64 72 2b (u32)m.n==szHdr+
1b2d6 6c 65 6e 52 6f 77 69 64 20 29 3b 0a 20 20 69 66 lenRowid );. if
1b2d7 28 20 75 6e 6c 69 6b 65 6c 79 28 28 75 33 32 29 ( unlikely((u32)
1b2d8 6d 2e 6e 3c 73 7a 48 64 72 2b 6c 65 6e 52 6f 77 m.n<szHdr+lenRow
1b2d9 69 64 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 id) ){. goto
1b2da 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 idx_rowid_corrup
1b2db 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 tion;. }.. /*
1b2dc 46 65 74 63 68 20 74 68 65 20 69 6e 74 65 67 65 Fetch the intege
1b2dd 72 20 6f 66 66 20 74 68 65 20 65 6e 64 20 6f 66 r off the end of
1b2de 20 74 68 65 20 69 6e 64 65 78 20 72 65 63 6f 72 the index recor
1b2df 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 d */. sqlite3Vd
1b2e0 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 2a beSerialGet((u8*
1b2e1 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c 65 6e 52 6f 77 )&m.z[m.n-lenRow
1b2e2 69 64 5d 2c 20 74 79 70 65 52 6f 77 69 64 2c 20 id], typeRowid,
1b2e3 26 76 29 3b 0a 20 20 2a 72 6f 77 69 64 20 3d 20 &v);. *rowid =
1b2e4 76 2e 75 2e 69 3b 0a 20 20 73 71 6c 69 74 65 33 v.u.i;. sqlite3
1b2e5 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 VdbeMemRelease(&
1b2e6 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c m);. return SQL
1b2e7 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 4a 75 ITE_OK;.. /* Ju
1b2e8 6d 70 20 68 65 72 65 20 69 66 20 64 61 74 61 62 mp here if datab
1b2e9 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 ase corruption i
1b2ea 73 20 64 65 74 65 63 74 65 64 20 61 66 74 65 72 s detected after
1b2eb 20 6d 20 68 61 73 20 62 65 65 6e 0a 20 20 2a 2a m has been. **
1b2ec 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 72 65 allocated. Fre
1b2ed 65 20 74 68 65 20 6d 20 6f 62 6a 65 63 74 20 61 e the m object a
1b2ee 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 nd return SQLITE
1b2ef 5f 43 4f 52 52 55 50 54 2e 20 2a 2f 0a 69 64 78 _CORRUPT. */.idx
1b2f0 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f _rowid_corruptio
1b2f1 6e 3a 0a 20 20 74 65 73 74 63 61 73 65 28 20 6d n:. testcase( m
1b2f2 2e 7a 4d 61 6c 6c 6f 63 21 3d 30 20 29 3b 0a 20 .zMalloc!=0 );.
1b2f3 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1b2f4 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 elease(&m);. re
1b2f5 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
1b2f6 55 50 54 5f 42 4b 50 54 3b 0a 7d 0a 0a 2f 2a 0a UPT_BKPT;.}../*.
1b2f7 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 6b ** Compare the k
1b2f8 65 79 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 ey of the index
1b2f9 65 6e 74 72 79 20 74 68 61 74 20 63 75 72 73 6f entry that curso
1b2fa 72 20 70 43 20 69 73 20 70 6f 69 6e 74 69 6e 67 r pC is pointing
1b2fb 20 74 6f 20 61 67 61 69 6e 73 74 0a 2a 2a 20 74 to against.** t
1b2fc 68 65 20 6b 65 79 20 73 74 72 69 6e 67 20 69 6e he key string in
1b2fd 20 70 55 6e 70 61 63 6b 65 64 2e 20 20 57 72 69 pUnpacked. Wri
1b2fe 74 65 20 69 6e 74 6f 20 2a 70 52 65 73 20 61 20 te into *pRes a
1b2ff 6e 75 6d 62 65 72 0a 2a 2a 20 74 68 61 74 20 69 number.** that i
1b300 73 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f s negative, zero
1b301 2c 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 , or positive if
1b302 20 70 43 20 69 73 20 6c 65 73 73 20 74 68 61 6e pC is less than
1b303 2c 20 65 71 75 61 6c 20 74 6f 2c 0a 2a 2a 20 6f , equal to,.** o
1b304 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 r greater than p
1b305 55 6e 70 61 63 6b 65 64 2e 20 20 52 65 74 75 72 Unpacked. Retur
1b306 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 n SQLITE_OK on s
1b307 75 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 70 55 uccess..**.** pU
1b308 6e 70 61 63 6b 65 64 20 69 73 20 65 69 74 68 65 npacked is eithe
1b309 72 20 63 72 65 61 74 65 64 20 77 69 74 68 6f 75 r created withou
1b30a 74 20 61 20 72 6f 77 69 64 20 6f 72 20 69 73 20 t a rowid or is
1b30b 74 72 75 6e 63 61 74 65 64 20 73 6f 20 74 68 61 truncated so tha
1b30c 74 20 69 74 0a 2a 2a 20 6f 6d 69 74 73 20 74 68 t it.** omits th
1b30d 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 e rowid at the e
1b30e 6e 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 61 nd. The rowid a
1b30f 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
1b310 20 69 6e 64 65 78 20 65 6e 74 72 79 0a 2a 2a 20 index entry.**
1b311 69 73 20 69 67 6e 6f 72 65 64 20 61 73 20 77 65 is ignored as we
1b312 6c 6c 2e 20 20 48 65 6e 63 65 2c 20 74 68 69 73 ll. Hence, this
1b313 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f routine only co
1b314 6d 70 61 72 65 73 20 74 68 65 20 70 72 65 66 69 mpares the prefi
1b315 78 65 73 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6b xes .** of the k
1b316 65 79 73 20 70 72 69 6f 72 20 74 6f 20 74 68 65 eys prior to the
1b317 20 66 69 6e 61 6c 20 72 6f 77 69 64 2c 20 6e 6f final rowid, no
1b318 74 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79 t the entire key
1b319 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1b31a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
1b31b 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 dbeIdxKeyCompare
1b31c 28 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a (. VdbeCursor *
1b31d 70 43 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 pC,
1b31e 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 74 6f /* The cursor to
1b31f 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74 compare against
1b320 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 */. UnpackedRe
1b321 63 6f 72 64 20 2a 70 55 6e 70 61 63 6b 65 64 2c cord *pUnpacked,
1b322 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 76 65 /* Unpacked ve
1b323 72 73 69 6f 6e 20 6f 66 20 6b 65 79 20 74 6f 20 rsion of key to
1b324 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 compare against
1b325 2a 2f 0a 20 20 69 6e 74 20 2a 72 65 73 20 20 20 */. int *res
1b326 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b327 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f /* Write the co
1b328 6d 70 61 72 69 73 6f 6e 20 72 65 73 75 6c 74 20 mparison result
1b329 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 36 34 here */.){. i64
1b32a 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 nCellKey = 0;.
1b32b 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 43 75 72 int rc;. BtCur
1b32c 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 43 2d 3e sor *pCur = pC->
1b32d 70 43 75 72 73 6f 72 3b 0a 20 20 4d 65 6d 20 6d pCursor;. Mem m
1b32e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
1b32f 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 ite3BtreeCursorI
1b330 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b 0a sValid(pCur) );.
1b331 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1b332 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c reeKeySize(pCur,
1b333 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 61 &nCellKey);. a
1b334 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
1b335 45 5f 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 70 43 E_OK ); /* pC
1b336 75 72 20 69 73 20 61 6c 77 61 79 73 20 76 61 6c ur is always val
1b337 69 64 20 73 6f 20 4b 65 79 53 69 7a 65 20 63 61 id so KeySize ca
1b338 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 2f nnot fail */. /
1b339 2a 20 6e 43 65 6c 6c 4b 65 79 20 77 69 6c 6c 20 * nCellKey will
1b33a 61 6c 77 61 79 73 20 62 65 20 62 65 74 77 65 65 always be betwee
1b33b 6e 20 30 20 61 6e 64 20 30 78 66 66 66 66 66 66 n 0 and 0xffffff
1b33c 66 66 20 62 65 63 61 75 73 65 20 6f 66 20 74 68 ff because of th
1b33d 65 20 73 61 79 0a 20 20 2a 2a 20 74 68 61 74 20 e say. ** that
1b33e 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
1b33f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 47 r() and sqlite3G
1b340 65 74 56 61 72 69 6e 74 33 32 28 29 20 61 72 65 etVarint32() are
1b341 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 2a 2f 0a implemented */.
1b342 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d if( nCellKey<=
1b343 30 20 7c 7c 20 6e 43 65 6c 6c 4b 65 79 3e 30 78 0 || nCellKey>0x
1b344 37 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20 20 7fffffff ){.
1b345 2a 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 *res = 0;. re
1b346 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
1b347 55 50 54 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 UPT;. }. memse
1b348 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 t(&m, 0, sizeof(
1b349 6d 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 m));. rc = sqli
1b34a 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 te3VdbeMemFromBt
1b34b 72 65 65 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c ree(pC->pCursor,
1b34c 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 0, (int)nCellKe
1b34d 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69 66 28 y, 1, &m);. if(
1b34e 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 rc ){. retur
1b34f 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 n rc;. }. asse
1b350 72 74 28 20 70 55 6e 70 61 63 6b 65 64 2d 3e 66 rt( pUnpacked->f
1b351 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f lags & UNPACKED_
1b352 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 29 3b 0a IGNORE_ROWID );.
1b353 20 20 2a 72 65 73 20 3d 20 73 71 6c 69 74 65 33 *res = sqlite3
1b354 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 VdbeRecordCompar
1b355 65 28 6d 2e 6e 2c 20 6d 2e 7a 2c 20 70 55 6e 70 e(m.n, m.z, pUnp
1b356 61 63 6b 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 acked);. sqlite
1b357 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
1b358 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 &m);. return SQ
1b359 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1b35a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 * This routine s
1b35b 65 74 73 20 74 68 65 20 76 61 6c 75 65 20 74 6f ets the value to
1b35c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 be returned by
1b35d 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 subsequent calls
1b35e 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 to.** sqlite3_c
1b35f 68 61 6e 67 65 73 28 29 20 6f 6e 20 74 68 65 20 hanges() on the
1b360 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 database handle
1b361 27 64 62 27 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 'db'. .*/.SQLITE
1b362 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1b363 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e lite3VdbeSetChan
1b364 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ges(sqlite3 *db,
1b365 20 69 6e 74 20 6e 43 68 61 6e 67 65 29 7b 0a 20 int nChange){.
1b366 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1b367 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e _mutex_held(db->
1b368 6d 75 74 65 78 29 20 29 3b 0a 20 20 64 62 2d 3e mutex) );. db->
1b369 6e 43 68 61 6e 67 65 20 3d 20 6e 43 68 61 6e 67 nChange = nChang
1b36a 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 e;. db->nTotalC
1b36b 68 61 6e 67 65 20 2b 3d 20 6e 43 68 61 6e 67 65 hange += nChange
1b36c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61 ;.}../*.** Set a
1b36d 20 66 6c 61 67 20 69 6e 20 74 68 65 20 76 64 62 flag in the vdb
1b36e 65 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 e to update the
1b36f 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 77 change counter w
1b370 68 65 6e 20 69 74 20 69 73 20 66 69 6e 61 6c 69 hen it is finali
1b371 73 65 64 0a 2a 2a 20 6f 72 20 72 65 73 65 74 2e sed.** or reset.
1b372 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1b373 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1b374 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 dbeCountChanges(
1b375 56 64 62 65 20 2a 76 29 7b 0a 20 20 76 2d 3e 63 Vdbe *v){. v->c
1b376 68 61 6e 67 65 43 6e 74 4f 6e 20 3d 20 31 3b 0a hangeCntOn = 1;.
1b377 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 65 76 }../*.** Mark ev
1b378 65 72 79 20 70 72 65 70 61 72 65 64 20 73 74 61 ery prepared sta
1b379 74 65 6d 65 6e 74 20 61 73 73 6f 63 69 61 74 65 tement associate
1b37a 64 20 77 69 74 68 20 61 20 64 61 74 61 62 61 73 d with a databas
1b37b 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 e connection.**
1b37c 61 73 20 65 78 70 69 72 65 64 2e 0a 2a 2a 0a 2a as expired..**.*
1b37d 2a 20 41 6e 20 65 78 70 69 72 65 64 20 73 74 61 * An expired sta
1b37e 74 65 6d 65 6e 74 20 6d 65 61 6e 73 20 74 68 61 tement means tha
1b37f 74 20 72 65 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 t recompilation
1b380 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 of the statement
1b381 20 69 73 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64 is.** recommend
1b382 2e 20 20 53 74 61 74 65 6d 65 6e 74 73 20 65 78 . Statements ex
1b383 70 69 72 65 20 77 68 65 6e 20 74 68 69 6e 67 73 pire when things
1b384 20 68 61 70 70 65 6e 20 74 68 61 74 20 6d 61 6b happen that mak
1b385 65 20 74 68 65 69 72 0a 2a 2a 20 70 72 6f 67 72 e their.** progr
1b386 61 6d 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 52 ams obsolete. R
1b387 65 6d 6f 76 69 6e 67 20 75 73 65 72 2d 64 65 66 emoving user-def
1b388 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 6f ined functions o
1b389 72 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 r collating.** s
1b38a 65 71 75 65 6e 63 65 73 2c 20 6f 72 20 63 68 61 equences, or cha
1b38b 6e 67 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 69 nging an authori
1b38c 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 zation function
1b38d 61 72 65 20 74 68 65 20 74 79 70 65 73 20 6f 66 are the types of
1b38e 0a 2a 2a 20 74 68 69 6e 67 73 20 74 68 61 74 20 .** things that
1b38f 6d 61 6b 65 20 70 72 65 70 61 72 65 64 20 73 74 make prepared st
1b390 61 74 65 6d 65 6e 74 73 20 6f 62 73 6f 6c 65 74 atements obsolet
1b391 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1b392 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1b393 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 3ExpirePreparedS
1b394 74 61 74 65 6d 65 6e 74 73 28 73 71 6c 69 74 65 tatements(sqlite
1b395 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a 3 *db){. Vdbe *
1b396 70 3b 0a 20 20 66 6f 72 28 70 20 3d 20 64 62 2d p;. for(p = db-
1b397 3e 70 56 64 62 65 3b 20 70 3b 20 70 3d 70 2d 3e >pVdbe; p; p=p->
1b398 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 65 pNext){. p->e
1b399 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a xpired = 1;. }.
1b39a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1b39b 74 68 65 20 64 61 74 61 62 61 73 65 20 61 73 73 the database ass
1b39c 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
1b39d 20 56 64 62 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 Vdbe..*/.SQLITE
1b39e 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
1b39f 20 2a 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 *sqlite3VdbeDb(
1b3a0 56 64 62 65 20 2a 76 29 7b 0a 20 20 72 65 74 75 Vdbe *v){. retu
1b3a1 72 6e 20 76 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 2a rn v->db;.}../**
1b3a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
1b3a3 20 6f 66 20 76 64 62 65 61 75 78 2e 63 20 2a 2a of vdbeaux.c **
1b3a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b3a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b3a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
1b3a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
1b3a8 69 6e 20 66 69 6c 65 20 76 64 62 65 61 70 69 2e in file vdbeapi.
1b3a9 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1b3aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b3ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
1b3ac 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36 0a 2a ** 2004 May 26.*
1b3ad 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
1b3ae 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
1b3af 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
1b3b0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
1b3b1 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
1b3b2 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
1b3b3 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
1b3b4 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
1b3b5 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
1b3b6 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
1b3b7 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
1b3b8 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
1b3b9 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
1b3ba 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
1b3bb 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
1b3bc 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
1b3bd 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
1b3be 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
1b3bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b3c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b3c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b3c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
1b3c3 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
1b3c4 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20 ntains code use
1b3c5 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 41 50 49 to implement API
1b3c6 73 20 74 68 61 74 20 61 72 65 20 70 61 72 74 20 s that are part
1b3c7 6f 66 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e 0a of the.** VDBE..
1b3c8 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 61 **.** $Id: vdbea
1b3c9 70 69 2e 63 2c 76 20 31 2e 31 36 37 20 32 30 30 pi.c,v 1.167 200
1b3ca 39 2f 30 36 2f 32 35 20 30 31 3a 34 37 3a 31 32 9/06/25 01:47:12
1b3cb 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 drh Exp $.*/..#
1b3cc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1b3cd 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a IT_DEPRECATED./*
1b3ce 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 .** Return TRUE
1b3cf 28 6e 6f 6e 2d 7a 65 72 6f 29 20 6f 66 20 74 68 (non-zero) of th
1b3d0 65 20 73 74 61 74 65 6d 65 6e 74 20 73 75 70 70 e statement supp
1b3d1 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d lied as an argum
1b3d2 65 6e 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 ent needs.** to
1b3d3 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 2e 20 20 be recompiled.
1b3d4 41 20 73 74 61 74 65 6d 65 6e 74 20 6e 65 65 64 A statement need
1b3d5 73 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c s to be recompil
1b3d6 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 65 0a ed whenever the.
1b3d7 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 65 6e 76 ** execution env
1b3d8 69 72 6f 6e 6d 65 6e 74 20 63 68 61 6e 67 65 73 ironment changes
1b3d9 20 69 6e 20 61 20 77 61 79 20 74 68 61 74 20 77 in a way that w
1b3da 6f 75 6c 64 20 61 6c 74 65 72 20 74 68 65 20 70 ould alter the p
1b3db 72 6f 67 72 61 6d 0a 2a 2a 20 74 68 61 74 20 73 rogram.** that s
1b3dc 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 qlite3_prepare()
1b3dd 20 67 65 6e 65 72 61 74 65 73 2e 20 20 46 6f 72 generates. For
1b3de 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 6e 65 77 example, if new
1b3df 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 0a 2a 2a functions or.**
1b3e0 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
1b3e1 6e 63 65 73 20 61 72 65 20 72 65 67 69 73 74 65 nces are registe
1b3e2 72 65 64 20 6f 72 20 69 66 20 61 6e 20 61 75 74 red or if an aut
1b3e3 68 6f 72 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e horizer function
1b3e4 20 69 73 0a 2a 2a 20 61 64 64 65 64 20 6f 72 20 is.** added or
1b3e5 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 changed..*/.SQLI
1b3e6 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1b3e7 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69 74 e3_expired(sqlit
1b3e8 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b e3_stmt *pStmt){
1b3e9 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 . Vdbe *p = (Vd
1b3ea 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 be*)pStmt;. ret
1b3eb 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 65 urn p==0 || p->e
1b3ec 78 70 69 72 65 64 3b 0a 7d 0a 23 65 6e 64 69 66 xpired;.}.#endif
1b3ed 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
1b3ee 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 64 65 owing routine de
1b3ef 73 74 72 6f 79 73 20 61 20 76 69 72 74 75 61 6c stroys a virtual
1b3f0 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 69 73 machine that is
1b3f1 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74 created by.** t
1b3f2 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 he sqlite3_compi
1b3f3 6c 65 28 29 20 72 6f 75 74 69 6e 65 2e 20 54 68 le() routine. Th
1b3f4 65 20 69 6e 74 65 67 65 72 20 72 65 74 75 72 6e e integer return
1b3f5 65 64 20 69 73 20 61 6e 20 53 51 4c 49 54 45 5f ed is an SQLITE_
1b3f6 0a 2a 2a 20 73 75 63 63 65 73 73 2f 66 61 69 6c .** success/fail
1b3f7 75 72 65 20 63 6f 64 65 20 74 68 61 74 20 64 65 ure code that de
1b3f8 73 63 72 69 62 65 73 20 74 68 65 20 72 65 73 75 scribes the resu
1b3f9 6c 74 20 6f 66 20 65 78 65 63 75 74 69 6e 67 20 lt of executing
1b3fa 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d the virtual.** m
1b3fb 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 achine..**.** Th
1b3fc 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 is routine sets
1b3fd 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61 the error code a
1b3fe 6e 64 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e nd string return
1b3ff 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 ed by.** sqlite3
1b400 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 71 6c 69 _errcode(), sqli
1b401 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 te3_errmsg() and
1b402 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 sqlite3_errmsg1
1b403 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 6()..*/.SQLITE_A
1b404 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 PI int sqlite3_f
1b405 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f inalize(sqlite3_
1b406 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 stmt *pStmt){.
1b407 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 53 int rc;. if( pS
1b408 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 tmt==0 ){. rc
1b409 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1b40a 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 62 65 20 }else{. Vdbe
1b40b 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d *v = (Vdbe*)pStm
1b40c 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a t;. sqlite3 *
1b40d 64 62 20 3d 20 76 2d 3e 64 62 3b 0a 23 69 66 20 db = v->db;.#if
1b40e 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
1b40f 45 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 E. sqlite3_mu
1b410 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 76 2d 3e tex *mutex = v->
1b411 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 65 6e 64 69 db->mutex;.#endi
1b412 66 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 f. sqlite3_mu
1b413 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 tex_enter(mutex)
1b414 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1b415 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 e3VdbeFinalize(v
1b416 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 );. rc = sqli
1b417 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 te3ApiExit(db, r
1b418 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f c);. sqlite3_
1b419 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 mutex_leave(mute
1b41a 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e x);. }. return
1b41b 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 rc;.}../*.** Te
1b41c 72 6d 69 6e 61 74 65 20 74 68 65 20 63 75 72 72 rminate the curr
1b41d 65 6e 74 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 ent execution of
1b41e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e an SQL statemen
1b41f 74 20 61 6e 64 20 72 65 73 65 74 20 69 74 0a 2a t and reset it.*
1b420 2a 20 62 61 63 6b 20 74 6f 20 69 74 73 20 73 74 * back to its st
1b421 61 72 74 69 6e 67 20 73 74 61 74 65 20 73 6f 20 arting state so
1b422 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 that it can be r
1b423 65 75 73 65 64 2e 20 41 20 73 75 63 63 65 73 73 eused. A success
1b424 20 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 code from.** th
1b425 65 20 70 72 69 6f 72 20 65 78 65 63 75 74 69 6f e prior executio
1b426 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a n is returned..*
1b427 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1b428 65 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 e sets the error
1b429 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67 code and string
1b42a 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 returned by.**
1b42b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 sqlite3_errcode(
1b42c 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 ), sqlite3_errms
1b42d 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f g() and sqlite3_
1b42e 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 errmsg16()..*/.S
1b42f 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1b430 6c 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69 lite3_reset(sqli
1b431 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
1b432 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 {. int rc;. if
1b433 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 ( pStmt==0 ){.
1b434 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
1b435 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 ;. }else{. V
1b436 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 dbe *v = (Vdbe*)
1b437 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 pStmt;. sqlit
1b438 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 76 e3_mutex_enter(v
1b439 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 ->db->mutex);.
1b43a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1b43b 62 65 52 65 73 65 74 28 76 29 3b 0a 20 20 20 20 beReset(v);.
1b43c 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 sqlite3VdbeMakeR
1b43d 65 61 64 79 28 76 2c 20 2d 31 2c 20 30 2c 20 30 eady(v, -1, 0, 0
1b43e 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 , 0, 0, 0);.
1b43f 61 73 73 65 72 74 28 20 28 72 63 20 26 20 28 76 assert( (rc & (v
1b440 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 29 3d ->db->errMask))=
1b441 3d 72 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 =rc );. rc =
1b442 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 76 sqlite3ApiExit(v
1b443 2d 3e 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 73 ->db, rc);. s
1b444 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1b445 76 65 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ve(v->db->mutex)
1b446 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1b447 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 c;.}../*.** Set
1b448 61 6c 6c 20 74 68 65 20 70 61 72 61 6d 65 74 65 all the paramete
1b449 72 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c rs in the compil
1b44a 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 ed SQL statement
1b44b 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c to NULL..*/.SQL
1b44c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1b44d 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e te3_clear_bindin
1b44e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 gs(sqlite3_stmt
1b44f 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 69 *pStmt){. int i
1b450 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
1b451 49 54 45 5f 4f 4b 3b 0a 20 20 56 64 62 65 20 2a ITE_OK;. Vdbe *
1b452 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 p = (Vdbe*)pStmt
1b453 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 ;.#if SQLITE_THR
1b454 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 EADSAFE. sqlite
1b455 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 3_mutex *mutex =
1b456 20 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d ((Vdbe*)pStmt)-
1b457 3e 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 65 6e 64 >db->mutex;.#end
1b458 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 if. sqlite3_mut
1b459 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b ex_enter(mutex);
1b45a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d . for(i=0; i<p-
1b45b 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 >nVar; i++){.
1b45c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1b45d 65 6c 65 61 73 65 28 26 70 2d 3e 61 56 61 72 5b elease(&p->aVar[
1b45e 69 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 56 61 72 i]);. p->aVar
1b45f 5b 69 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f [i].flags = MEM_
1b460 4e 75 6c 6c 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 Null;. }. sqli
1b461 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1b462 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e mutex);. return
1b463 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a rc;.}.../******
1b464 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b465 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 ****** sqlite3_v
1b466 61 6c 75 65 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a alue_ *********
1b467 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b468 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f ******.** The fo
1b469 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 llowing routines
1b46a 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d 61 extract informa
1b46b 74 69 6f 6e 20 66 72 6f 6d 20 61 20 4d 65 6d 20 tion from a Mem
1b46c 6f 72 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 or sqlite3_value
1b46d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a .** structure..*
1b46e 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
1b46f 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
1b470 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 69 _value_blob(sqli
1b471 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 te3_value *pVal)
1b472 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 {. Mem *p = (Me
1b473 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20 70 m*)pVal;. if( p
1b474 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 ->flags & (MEM_B
1b475 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 7b 0a lob|MEM_Str) ){.
1b476 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1b477 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 70 29 3b emExpandBlob(p);
1b478 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 26 3d . p->flags &=
1b479 20 7e 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 70 ~MEM_Str;. p
1b47a 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 42 ->flags |= MEM_B
1b47b 6c 6f 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 lob;. return
1b47c 70 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 p->z;. }else{.
1b47d 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 return sqlite
1b47e 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 56 61 3_value_text(pVa
1b47f 6c 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 l);. }.}.SQLITE
1b480 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1b481 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73 71 6c _value_bytes(sql
1b482 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c ite3_value *pVal
1b483 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
1b484 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 70 56 te3ValueBytes(pV
1b485 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 al, SQLITE_UTF8)
1b486 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 ;.}.SQLITE_API i
1b487 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 nt sqlite3_value
1b488 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 _bytes16(sqlite3
1b489 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 _value *pVal){.
1b48a 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 return sqlite3V
1b48b 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c 20 alueBytes(pVal,
1b48c 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 SQLITE_UTF16NATI
1b48d 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 VE);.}.SQLITE_AP
1b48e 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 I double sqlite3
1b48f 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 71 _value_double(sq
1b490 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
1b491 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c l){. return sql
1b492 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 ite3VdbeRealValu
1b493 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d e((Mem*)pVal);.}
1b494 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1b495 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e sqlite3_value_in
1b496 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 t(sqlite3_value
1b497 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e *pVal){. return
1b498 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62 (int)sqlite3Vdb
1b499 65 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29 eIntValue((Mem*)
1b49a 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f pVal);.}.SQLITE_
1b49b 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 API sqlite_int64
1b49c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 sqlite3_value_i
1b49d 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c nt64(sqlite3_val
1b49e 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 ue *pVal){. ret
1b49f 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 49 urn sqlite3VdbeI
1b4a0 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 ntValue((Mem*)pV
1b4a1 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 al);.}.SQLITE_AP
1b4a2 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 I const unsigned
1b4a3 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 char *sqlite3_v
1b4a4 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 74 65 alue_text(sqlite
1b4a5 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 3_value *pVal){.
1b4a6 20 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 74 20 return (const
1b4a7 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 unsigned char *)
1b4a8 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 sqlite3ValueText
1b4a9 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 (pVal, SQLITE_UT
1b4aa 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 F8);.}.#ifndef S
1b4ab 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
1b4ac 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
1b4ad 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
1b4ae 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71 6c value_text16(sql
1b4af 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c ite3_value* pVal
1b4b0 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
1b4b1 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 te3ValueText(pVa
1b4b2 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e l, SQLITE_UTF16N
1b4b3 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 ATIVE);.}.SQLITE
1b4b4 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
1b4b5 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 *sqlite3_value_t
1b4b6 65 78 74 31 36 62 65 28 73 71 6c 69 74 65 33 5f ext16be(sqlite3_
1b4b7 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 value *pVal){.
1b4b8 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 return sqlite3Va
1b4b9 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 lueText(pVal, SQ
1b4ba 4c 49 54 45 5f 55 54 46 31 36 42 45 29 3b 0a 7d LITE_UTF16BE);.}
1b4bb 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
1b4bc 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
1b4bd 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73 value_text16le(s
1b4be 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
1b4bf 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 al){. return sq
1b4c0 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 lite3ValueText(p
1b4c1 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 Val, SQLITE_UTF1
1b4c2 36 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 6LE);.}.#endif /
1b4c3 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 * SQLITE_OMIT_UT
1b4c4 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 F16 */.SQLITE_AP
1b4c5 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 I int sqlite3_va
1b4c6 6c 75 65 5f 74 79 70 65 28 73 71 6c 69 74 65 33 lue_type(sqlite3
1b4c7 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 20 _value* pVal){.
1b4c8 20 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 74 79 return pVal->ty
1b4c9 70 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a pe;.}../********
1b4ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b4cb 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 **** sqlite3_res
1b4cc 75 6c 74 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ult_ **********
1b4cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b4ce 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c *****.** The fol
1b4cf 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 lowing routines
1b4d0 61 72 65 20 75 73 65 64 20 62 79 20 75 73 65 72 are used by user
1b4d1 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
1b4d2 6e 73 20 74 6f 20 73 70 65 63 69 66 79 0a 2a 2a ns to specify.**
1b4d3 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 the function re
1b4d4 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 sult..**.** The
1b4d5 73 65 74 53 74 72 4f 72 45 72 72 6f 72 28 29 20 setStrOrError()
1b4d6 66 75 6e 74 69 6f 6e 20 63 61 6c 6c 73 20 73 71 funtion calls sq
1b4d7 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 lite3VdbeMemSetS
1b4d8 74 72 28 29 20 74 6f 20 73 74 6f 72 65 20 74 68 tr() to store th
1b4d9 65 0a 2a 2a 20 72 65 73 75 6c 74 20 61 73 20 61 e.** result as a
1b4da 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 string or blob
1b4db 62 75 74 20 69 66 20 74 68 65 20 73 74 72 69 6e but if the strin
1b4dc 67 20 6f 72 20 62 6c 6f 62 20 69 73 20 74 6f 6f g or blob is too
1b4dd 20 6c 61 72 67 65 2c 20 69 74 0a 2a 2a 20 74 68 large, it.** th
1b4de 65 6e 20 73 65 74 73 20 74 68 65 20 65 72 72 6f en sets the erro
1b4df 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 54 45 r code to SQLITE
1b4e0 5f 54 4f 4f 42 49 47 0a 2a 2f 0a 73 74 61 74 69 _TOOBIG.*/.stati
1b4e1 63 20 76 6f 69 64 20 73 65 74 52 65 73 75 6c 74 c void setResult
1b4e2 53 74 72 4f 72 45 72 72 6f 72 28 0a 20 20 73 71 StrOrError(. sq
1b4e3 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1b4e4 43 74 78 2c 20 20 2f 2a 20 46 75 6e 63 74 69 6f Ctx, /* Functio
1b4e5 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 n context */. c
1b4e6 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 20 20 onst char *z,
1b4e7 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 /* String
1b4e8 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e pointer */. in
1b4e9 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 t n,
1b4ea 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69 /* Bytes i
1b4eb 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20 6e 65 67 n string, or neg
1b4ec 61 74 69 76 65 20 2a 2f 0a 20 20 75 38 20 65 6e ative */. u8 en
1b4ed 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c,
1b4ee 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 6f /* Encoding o
1b4ef 66 20 7a 2e 20 20 30 20 66 6f 72 20 42 4c 4f 42 f z. 0 for BLOB
1b4f0 73 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 s */. void (*xD
1b4f1 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 20 20 2f el)(void*) /
1b4f2 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 75 6e * Destructor fun
1b4f3 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 66 ction */.){. if
1b4f4 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d ( sqlite3VdbeMem
1b4f5 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c SetStr(&pCtx->s,
1b4f6 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44 65 6c z, n, enc, xDel
1b4f7 29 3d 3d 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 )==SQLITE_TOOBIG
1b4f8 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
1b4f9 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f result_error_too
1b4fa 62 69 67 28 70 43 74 78 29 3b 0a 20 20 7d 0a 7d big(pCtx);. }.}
1b4fb 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
1b4fc 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
1b4fd 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f blob(. sqlite3_
1b4fe 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a context *pCtx, .
1b4ff 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c const void *z,
1b500 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f . int n, . vo
1b501 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 id (*xDel)(void
1b502 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *).){. assert(
1b503 6e 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 n>=0 );. assert
1b504 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1b505 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d held(pCtx->s.db-
1b506 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65 74 >mutex) );. set
1b507 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 ResultStrOrError
1b508 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 30 2c 20 (pCtx, z, n, 0,
1b509 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f xDel);.}.SQLITE_
1b50a 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
1b50b 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 _result_double(s
1b50c 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1b50d 70 43 74 78 2c 20 64 6f 75 62 6c 65 20 72 56 61 pCtx, double rVa
1b50e 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 l){. assert( sq
1b50f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1b510 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 (pCtx->s.db->mut
1b511 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ex) );. sqlite3
1b512 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 VdbeMemSetDouble
1b513 28 26 70 43 74 78 2d 3e 73 2c 20 72 56 61 6c 29 (&pCtx->s, rVal)
1b514 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 ;.}.SQLITE_API v
1b515 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
1b516 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69 74 65 33 lt_error(sqlite3
1b517 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
1b518 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 const char *z, i
1b519 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 nt n){. assert(
1b51a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1b51b 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e eld(pCtx->s.db->
1b51c 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 78 mutex) );. pCtx
1b51d 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 ->isError = SQLI
1b51e 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73 71 6c 69 TE_ERROR;. sqli
1b51f 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
1b520 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c (&pCtx->s, z, n,
1b521 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 SQLITE_UTF8, SQ
1b522 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b LITE_TRANSIENT);
1b523 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 .}.#ifndef SQLIT
1b524 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c E_OMIT_UTF16.SQL
1b525 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
1b526 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
1b527 72 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 r16(sqlite3_cont
1b528 65 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e 73 74 ext *pCtx, const
1b529 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 29 void *z, int n)
1b52a 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
1b52b 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1b52c 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 Ctx->s.db->mutex
1b52d 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 ) );. pCtx->isE
1b52e 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 45 52 rror = SQLITE_ER
1b52f 52 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 ROR;. sqlite3Vd
1b530 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 beMemSetStr(&pCt
1b531 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 x->s, z, n, SQLI
1b532 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 TE_UTF16NATIVE,
1b533 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
1b534 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 53 51 4c 49 );.}.#endif.SQLI
1b535 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
1b536 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 73 te3_result_int(s
1b537 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1b538 70 43 74 78 2c 20 69 6e 74 20 69 56 61 6c 29 7b pCtx, int iVal){
1b539 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1b53a 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
1b53b 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 tx->s.db->mutex)
1b53c 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );. sqlite3Vdb
1b53d 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70 43 eMemSetInt64(&pC
1b53e 74 78 2d 3e 73 2c 20 28 69 36 34 29 69 56 61 6c tx->s, (i64)iVal
1b53f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1b540 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
1b541 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 ult_int64(sqlite
1b542 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 3_context *pCtx,
1b543 20 69 36 34 20 69 56 61 6c 29 7b 0a 20 20 61 73 i64 iVal){. as
1b544 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1b545 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1b546 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1b547 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1b548 65 74 49 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 etInt64(&pCtx->s
1b549 2c 20 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 , iVal);.}.SQLIT
1b54a 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
1b54b 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 73 e3_result_null(s
1b54c 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1b54d 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28 pCtx){. assert(
1b54e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1b54f 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e eld(pCtx->s.db->
1b550 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 mutex) );. sqli
1b551 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c te3VdbeMemSetNul
1b552 6c 28 26 70 43 74 78 2d 3e 73 29 3b 0a 7d 0a 53 l(&pCtx->s);.}.S
1b553 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
1b554 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
1b555 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f xt(. sqlite3_co
1b556 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 ntext *pCtx, .
1b557 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 0a const char *z, .
1b558 20 20 69 6e 74 20 6e 2c 0a 20 20 76 6f 69 64 20 int n,. void
1b559 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a (*xDel)(void *).
1b55a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
1b55b 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1b55c 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 pCtx->s.db->mute
1b55d 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c x) );. setResul
1b55e 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78 tStrOrError(pCtx
1b55f 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 , z, n, SQLITE_U
1b560 54 46 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 69 TF8, xDel);.}.#i
1b561 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1b562 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 T_UTF16.SQLITE_A
1b563 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
1b564 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 0a 20 result_text16(.
1b565 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1b566 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 *pCtx, . const
1b567 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 void *z, . int
1b568 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 n, . void (*xD
1b569 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 el)(void *).){.
1b56a 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1b56b 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 _mutex_held(pCtx
1b56c 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ->s.db->mutex) )
1b56d 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74 72 ;. setResultStr
1b56e 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c OrError(pCtx, z,
1b56f 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 n, SQLITE_UTF16
1b570 4e 41 54 49 56 45 2c 20 78 44 65 6c 29 3b 0a 7d NATIVE, xDel);.}
1b571 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
1b572 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
1b573 74 65 78 74 31 36 62 65 28 0a 20 20 73 71 6c 69 text16be(. sqli
1b574 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
1b575 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 x, . const void
1b576 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a *z, . int n, .
1b577 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 void (*xDel)(v
1b578 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 oid *).){. asse
1b579 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1b57a 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 x_held(pCtx->s.d
1b57b 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 b->mutex) );. s
1b57c 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 etResultStrOrErr
1b57d 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 or(pCtx, z, n, S
1b57e 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 78 QLITE_UTF16BE, x
1b57f 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 Del);.}.SQLITE_A
1b580 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
1b581 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 28 result_text16le(
1b582 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
1b583 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e xt *pCtx, . con
1b584 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 st void *z, . i
1b585 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a nt n, . void (*
1b586 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b xDel)(void *).){
1b587 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1b588 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
1b589 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 tx->s.db->mutex)
1b58a 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 53 );. setResultS
1b58b 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20 trOrError(pCtx,
1b58c 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 z, n, SQLITE_UTF
1b58d 31 36 4c 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 16LE, xDel);.}.#
1b58e 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1b58f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 OMIT_UTF16 */.SQ
1b590 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
1b591 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c lite3_result_val
1b592 75 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ue(sqlite3_conte
1b593 78 74 20 2a 70 43 74 78 2c 20 73 71 6c 69 74 65 xt *pCtx, sqlite
1b594 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 3_value *pValue)
1b595 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
1b596 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1b597 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 Ctx->s.db->mutex
1b598 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 ) );. sqlite3Vd
1b599 62 65 4d 65 6d 43 6f 70 79 28 26 70 43 74 78 2d beMemCopy(&pCtx-
1b59a 3e 73 2c 20 70 56 61 6c 75 65 29 3b 0a 7d 0a 53 >s, pValue);.}.S
1b59b 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
1b59c 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 qlite3_result_ze
1b59d 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 roblob(sqlite3_c
1b59e 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e ontext *pCtx, in
1b59f 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 t n){. assert(
1b5a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1b5a1 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d ld(pCtx->s.db->m
1b5a2 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
1b5a3 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f e3VdbeMemSetZero
1b5a4 42 6c 6f 62 28 26 70 43 74 78 2d 3e 73 2c 20 6e Blob(&pCtx->s, n
1b5a5 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1b5a6 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
1b5a7 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73 ult_error_code(s
1b5a8 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1b5a9 70 43 74 78 2c 20 69 6e 74 20 65 72 72 43 6f 64 pCtx, int errCod
1b5aa 65 29 7b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 e){. pCtx->isEr
1b5ab 72 6f 72 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 ror = errCode;.
1b5ac 20 69 66 28 20 70 43 74 78 2d 3e 73 2e 66 6c 61 if( pCtx->s.fla
1b5ad 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b gs & MEM_Null ){
1b5ae 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1b5af 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d MemSetStr(&pCtx-
1b5b0 3e 73 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 >s, sqlite3ErrSt
1b5b1 72 28 65 72 72 43 6f 64 65 29 2c 20 2d 31 2c 20 r(errCode), -1,
1b5b2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1b5b3 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 SQLITE
1b5b4 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 _UTF8, SQLITE_ST
1b5b5 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ATIC);. }.}../*
1b5b6 20 46 6f 72 63 65 20 61 6e 20 53 51 4c 49 54 45 Force an SQLITE
1b5b7 5f 54 4f 4f 42 49 47 20 65 72 72 6f 72 2e 20 2a _TOOBIG error. *
1b5b8 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 /.SQLITE_API voi
1b5b9 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
1b5ba 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 _error_toobig(sq
1b5bb 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1b5bc 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Ctx){. assert(
1b5bd 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1b5be 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d ld(pCtx->s.db->m
1b5bf 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 78 2d utex) );. pCtx-
1b5c0 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 >isError = SQLIT
1b5c1 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 73 71 6c 69 E_TOOBIG;. sqli
1b5c2 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
1b5c3 28 26 70 43 74 78 2d 3e 73 2c 20 22 73 74 72 69 (&pCtx->s, "stri
1b5c4 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62 ng or blob too b
1b5c5 69 67 22 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 ig", -1, .
1b5c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b5c7 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 SQLITE_UTF8, SQ
1b5c8 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a LITE_STATIC);.}.
1b5c9 0a 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f 4e 4f ./* An SQLITE_NO
1b5ca 4d 45 4d 20 65 72 72 6f 72 2e 20 2a 2f 0a 53 51 MEM error. */.SQ
1b5cb 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
1b5cc 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
1b5cd 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 or_nomem(sqlite3
1b5ce 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b _context *pCtx){
1b5cf 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1b5d0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
1b5d1 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 tx->s.db->mutex)
1b5d2 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );. sqlite3Vdb
1b5d3 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74 eMemSetNull(&pCt
1b5d4 78 2d 3e 73 29 3b 0a 20 20 70 43 74 78 2d 3e 69 x->s);. pCtx->i
1b5d5 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f sError = SQLITE_
1b5d6 4e 4f 4d 45 4d 3b 0a 20 20 70 43 74 78 2d 3e 73 NOMEM;. pCtx->s
1b5d7 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 .db->mallocFaile
1b5d8 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 d = 1;.}../*.**
1b5d9 45 78 65 63 75 74 65 20 74 68 65 20 73 74 61 74 Execute the stat
1b5da 65 6d 65 6e 74 20 70 53 74 6d 74 2c 20 65 69 74 ement pStmt, eit
1b5db 68 65 72 20 75 6e 74 69 6c 20 61 20 72 6f 77 20 her until a row
1b5dc 6f 66 20 64 61 74 61 20 69 73 20 72 65 61 64 79 of data is ready
1b5dd 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 , the.** stateme
1b5de 6e 74 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 nt is completely
1b5df 20 65 78 65 63 75 74 65 64 20 6f 72 20 61 6e 20 executed or an
1b5e0 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a error occurs..**
1b5e1 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1b5e2 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 implements the
1b5e3 62 75 6c 6b 20 6f 66 20 74 68 65 20 6c 6f 67 69 bulk of the logi
1b5e4 63 20 62 65 68 69 6e 64 20 74 68 65 20 73 71 6c c behind the sql
1b5e5 69 74 65 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50 ite_step().** AP
1b5e6 49 2e 20 20 54 68 65 20 6f 6e 6c 79 20 74 68 69 I. The only thi
1b5e7 6e 67 20 6f 6d 69 74 74 65 64 20 69 73 20 74 68 ng omitted is th
1b5e8 65 20 61 75 74 6f 6d 61 74 69 63 20 72 65 63 6f e automatic reco
1b5e9 6d 70 69 6c 65 20 69 66 20 61 20 0a 2a 2a 20 73 mpile if a .** s
1b5ea 63 68 65 6d 61 20 63 68 61 6e 67 65 20 68 61 73 chema change has
1b5eb 20 6f 63 63 75 72 72 65 64 2e 20 20 54 68 61 74 occurred. That
1b5ec 20 64 65 74 61 69 6c 20 69 73 20 68 61 6e 64 6c detail is handl
1b5ed 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 6f 75 74 ed by the.** out
1b5ee 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 er sqlite3_step(
1b5ef 29 20 77 72 61 70 70 65 72 20 70 72 6f 63 65 64 ) wrapper proced
1b5f0 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ure..*/.static i
1b5f1 6e 74 20 73 71 6c 69 74 65 33 53 74 65 70 28 56 nt sqlite3Step(V
1b5f2 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 dbe *p){. sqlit
1b5f3 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 72 63 e3 *db;. int rc
1b5f4 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 29 3b 0a ;.. assert(p);.
1b5f5 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d if( p->magic!=
1b5f6 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 VDBE_MAGIC_RUN )
1b5f7 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
1b5f8 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a ITE_MISUSE;. }.
1b5f9 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 . /* Assert tha
1b5fa 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 6e t malloc() has n
1b5fb 6f 74 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 64 ot failed */. d
1b5fc 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 b = p->db;. if(
1b5fd 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1b5fe 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 d ){. return
1b5ff 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
1b600 7d 0a 0a 20 20 69 66 28 20 70 2d 3e 70 63 3c 3d }.. if( p->pc<=
1b601 30 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64 20 0 && p->expired
1b602 29 7b 0a 20 20 20 20 69 66 28 20 41 4c 57 41 59 ){. if( ALWAY
1b603 53 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f S(p->rc==SQLITE_
1b604 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e OK) ){. p->
1b605 72 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 rc = SQLITE_SCHE
1b606 4d 41 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 MA;. }. rc
1b607 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b = SQLITE_ERROR;
1b608 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 . goto end_of
1b609 5f 73 74 65 70 3b 0a 20 20 7d 0a 20 20 69 66 28 _step;. }. if(
1b60a 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e sqlite3SafetyOn
1b60b 28 64 62 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 (db) ){. p->r
1b60c 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 c = SQLITE_MISUS
1b60d 45 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 E;. return SQ
1b60e 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d LITE_MISUSE;. }
1b60f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3c 30 20 29 . if( p->pc<0 )
1b610 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 {. /* If ther
1b611 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 73 e are no other s
1b612 74 61 74 65 6d 65 6e 74 73 20 63 75 72 72 65 6e tatements curren
1b613 74 6c 79 20 72 75 6e 6e 69 6e 67 2c 20 74 68 65 tly running, the
1b614 6e 0a 20 20 20 20 2a 2a 20 72 65 73 65 74 20 74 n. ** reset t
1b615 68 65 20 69 6e 74 65 72 72 75 70 74 20 66 6c 61 he interrupt fla
1b616 67 2e 20 20 54 68 69 73 20 70 72 65 76 65 6e 74 g. This prevent
1b617 73 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 s a call to sqli
1b618 74 65 33 5f 69 6e 74 65 72 72 75 70 74 0a 20 20 te3_interrupt.
1b619 20 20 2a 2a 20 66 72 6f 6d 20 69 6e 74 65 72 72 ** from interr
1b61a 75 70 74 69 6e 67 20 61 20 73 74 61 74 65 6d 65 upting a stateme
1b61b 6e 74 20 74 68 61 74 20 68 61 73 20 6e 6f 74 20 nt that has not
1b61c 79 65 74 20 73 74 61 72 74 65 64 2e 0a 20 20 20 yet started..
1b61d 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e */. if( db->
1b61e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3d 3d 30 activeVdbeCnt==0
1b61f 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 75 31 ){. db->u1
1b620 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d .isInterrupted =
1b621 20 30 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 0;. }..#ifnd
1b622 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
1b623 52 41 43 45 0a 20 20 20 20 69 66 28 20 64 62 2d RACE. if( db-
1b624 3e 78 50 72 6f 66 69 6c 65 20 26 26 20 21 64 62 >xProfile && !db
1b625 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 ->init.busy ){.
1b626 20 20 20 20 20 64 6f 75 62 6c 65 20 72 4e 6f 77 double rNow
1b627 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f ;. sqlite3O
1b628 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d sCurrentTime(db-
1b629 3e 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 >pVfs, &rNow);.
1b62a 20 20 20 20 20 70 2d 3e 73 74 61 72 74 54 69 6d p->startTim
1b62b 65 20 3d 20 28 75 36 34 29 28 28 72 4e 6f 77 20 e = (u64)((rNow
1b62c 2d 20 28 69 6e 74 29 72 4e 6f 77 29 2a 33 36 30 - (int)rNow)*360
1b62d 30 2e 30 2a 32 34 2e 30 2a 31 30 30 30 30 30 30 0.0*24.0*1000000
1b62e 30 30 30 2e 30 29 3b 0a 20 20 20 20 7d 0a 23 65 000.0);. }.#e
1b62f 6e 64 69 66 0a 0a 20 20 20 20 64 62 2d 3e 61 63 ndif.. db->ac
1b630 74 69 76 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 tiveVdbeCnt++;.
1b631 20 20 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e if( p->readOn
1b632 6c 79 3d 3d 30 20 29 20 64 62 2d 3e 77 72 69 74 ly==0 ) db->writ
1b633 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 eVdbeCnt++;.
1b634 70 2d 3e 70 63 20 3d 20 30 3b 0a 20 20 7d 0a 23 p->pc = 0;. }.#
1b635 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1b636 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69 66 28 IT_EXPLAIN. if(
1b637 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 p->explain ){.
1b638 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
1b639 64 62 65 4c 69 73 74 28 70 29 3b 0a 20 20 7d 65 dbeList(p);. }e
1b63a 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 lse.#endif /* SQ
1b63b 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 LITE_OMIT_EXPLAI
1b63c 4e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 72 63 20 N */. {. rc
1b63d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 = sqlite3VdbeExe
1b63e 63 28 70 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 c(p);. }.. if(
1b63f 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1b640 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72 63 20 f(db) ){. rc
1b641 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b = SQLITE_MISUSE;
1b642 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 . }..#ifndef SQ
1b643 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a LITE_OMIT_TRACE.
1b644 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 /* Invoke the
1b645 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b profile callback
1b646 20 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 if there is one
1b647 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d . */. if( rc!=
1b648 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 64 62 SQLITE_ROW && db
1b649 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20 21 64 ->xProfile && !d
1b64a 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 b->init.busy &&
1b64b 70 2d 3e 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64 p->zSql ){. d
1b64c 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 20 20 ouble rNow;.
1b64d 75 36 34 20 65 6c 61 70 73 65 54 69 6d 65 3b 0a u64 elapseTime;.
1b64e 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75 . sqlite3OsCu
1b64f 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 rrentTime(db->pV
1b650 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 fs, &rNow);.
1b651 65 6c 61 70 73 65 54 69 6d 65 20 3d 20 28 75 36 elapseTime = (u6
1b652 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 6e 74 29 4)((rNow - (int)
1b653 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32 34 2e rNow)*3600.0*24.
1b654 30 2a 31 30 30 30 30 30 30 30 30 30 2e 30 29 3b 0*1000000000.0);
1b655 0a 20 20 20 20 65 6c 61 70 73 65 54 69 6d 65 20 . elapseTime
1b656 2d 3d 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 3b -= p->startTime;
1b657 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 66 69 6c . db->xProfil
1b658 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72 e(db->pProfileAr
1b659 67 2c 20 70 2d 3e 7a 53 71 6c 2c 20 65 6c 61 70 g, p->zSql, elap
1b65a 73 65 54 69 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e seTime);. }.#en
1b65b 64 69 66 0a 0a 20 20 64 62 2d 3e 65 72 72 43 6f dif.. db->errCo
1b65c 64 65 20 3d 20 72 63 3b 0a 20 20 69 66 28 20 53 de = rc;. if( S
1b65d 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c QLITE_NOMEM==sql
1b65e 69 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 ite3ApiExit(p->d
1b65f 62 2c 20 70 2d 3e 72 63 29 20 29 7b 0a 20 20 20 b, p->rc) ){.
1b660 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f p->rc = SQLITE_
1b661 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 65 6e 64 5f 6f NOMEM;. }.end_o
1b662 66 5f 73 74 65 70 3a 0a 20 20 2f 2a 20 41 74 20 f_step:. /* At
1b663 74 68 69 73 20 70 6f 69 6e 74 20 6c 6f 63 61 6c this point local
1b664 20 76 61 72 69 61 62 6c 65 20 72 63 20 68 6f 6c variable rc hol
1b665 64 73 20 74 68 65 20 76 61 6c 75 65 20 74 68 61 ds the value tha
1b666 74 20 73 68 6f 75 6c 64 20 62 65 20 0a 20 20 2a t should be . *
1b667 2a 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 * returned if th
1b668 69 73 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 is statement was
1b669 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 compiled using
1b66a 74 68 65 20 6c 65 67 61 63 79 20 0a 20 20 2a 2a the legacy . **
1b66b 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
1b66c 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20 41 63 () interface. Ac
1b66d 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 64 cording to the d
1b66e 6f 63 73 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e ocs, this can on
1b66f 6c 79 0a 20 20 2a 2a 20 62 65 20 6f 6e 65 20 6f ly. ** be one o
1b670 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 f the values in
1b671 74 68 65 20 66 69 72 73 74 20 61 73 73 65 72 74 the first assert
1b672 28 29 20 62 65 6c 6f 77 2e 20 56 61 72 69 61 62 () below. Variab
1b673 6c 65 20 70 2d 3e 72 63 20 0a 20 20 2a 2a 20 63 le p->rc . ** c
1b674 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 61 6c 75 ontains the valu
1b675 65 20 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 e that would be
1b676 72 65 74 75 72 6e 65 64 20 69 66 20 73 71 6c 69 returned if sqli
1b677 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 0a te3_finalize() .
1b678 20 20 2a 2a 20 77 65 72 65 20 63 61 6c 6c 65 64 ** were called
1b679 20 6f 6e 20 73 74 61 74 65 6d 65 6e 74 20 70 2e on statement p.
1b67a 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
1b67b 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 20 rc==SQLITE_ROW
1b67c 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f || rc==SQLITE_DO
1b67d 4e 45 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 NE || rc==SQLI
1b67e 54 45 5f 45 52 52 4f 52 20 0a 20 20 20 20 20 20 TE_ERROR .
1b67f 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 || rc==SQLITE_B
1b680 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 USY || rc==SQLIT
1b681 45 5f 4d 49 53 55 53 45 0a 20 20 29 3b 0a 20 20 E_MISUSE. );.
1b682 61 73 73 65 72 74 28 20 70 2d 3e 72 63 21 3d 53 assert( p->rc!=S
1b683 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 70 2d 3e QLITE_ROW && p->
1b684 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc!=SQLITE_DONE
1b685 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 73 50 72 );. if( p->isPr
1b686 65 70 61 72 65 56 32 20 26 26 20 72 63 21 3d 53 epareV2 && rc!=S
1b687 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 72 63 21 QLITE_ROW && rc!
1b688 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a =SQLITE_DONE ){.
1b689 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 73 /* If this s
1b68a 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70 72 65 tatement was pre
1b68b 70 61 72 65 64 20 75 73 69 6e 67 20 73 71 6c 69 pared using sqli
1b68c 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
1b68d 2c 20 61 6e 64 20 61 6e 0a 20 20 20 20 2a 2a 20 , and an. **
1b68e 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 65 error has occure
1b68f 64 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74 d, then return t
1b690 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 6e he error code in
1b691 20 70 2d 3e 72 63 20 74 6f 20 74 68 65 0a 20 20 p->rc to the.
1b692 20 20 2a 2a 20 63 61 6c 6c 65 72 2e 20 53 65 74 ** caller. Set
1b693 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 the error code
1b694 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
1b695 68 61 6e 64 6c 65 20 74 6f 20 74 68 65 20 73 61 handle to the sa
1b696 6d 65 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f me value.. */
1b697 20 0a 20 20 20 20 72 63 20 3d 20 64 62 2d 3e 65 . rc = db->e
1b698 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63 3b 0a rrCode = p->rc;.
1b699 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 72 63 }. return (rc
1b69a 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3b 0a 7d &db->errMask);.}
1b69b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ../*.** This is
1b69c 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 69 6d the top-level im
1b69d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
1b69e 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 sqlite3_step().
1b69f 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74 65 33 Call.** sqlite3
1b6a0 53 74 65 70 28 29 20 74 6f 20 64 6f 20 6d 6f 73 Step() to do mos
1b6a1 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 2e 20 20 t of the work.
1b6a2 49 66 20 61 20 73 63 68 65 6d 61 20 65 72 72 6f If a schema erro
1b6a3 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 63 61 6c r occurs,.** cal
1b6a4 6c 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 l sqlite3Reprepa
1b6a5 72 65 28 29 20 61 6e 64 20 74 72 79 20 61 67 61 re() and try aga
1b6a6 69 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 in..*/.SQLITE_AP
1b6a7 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 I int sqlite3_st
1b6a8 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 ep(sqlite3_stmt
1b6a9 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 *pStmt){. int r
1b6aa 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 c = SQLITE_MISUS
1b6ab 45 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 20 29 E;. if( pStmt )
1b6ac 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 {. int cnt =
1b6ad 30 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 0;. Vdbe *v =
1b6ae 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe*)pStmt;.
1b6af 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d sqlite3 *db =
1b6b0 20 76 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 v->db;. sqli
1b6b1 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
1b6b2 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 db->mutex);.
1b6b3 77 68 69 6c 65 28 20 28 72 63 20 3d 20 73 71 6c while( (rc = sql
1b6b4 69 74 65 33 53 74 65 70 28 76 29 29 3d 3d 53 51 ite3Step(v))==SQ
1b6b5 4c 49 54 45 5f 53 43 48 45 4d 41 0a 20 20 20 20 LITE_SCHEMA.
1b6b6 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b 2b 20 && cnt++
1b6b7 3c 20 35 0a 20 20 20 20 20 20 20 20 20 20 20 26 < 5. &
1b6b8 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 52 & (rc = sqlite3R
1b6b9 65 70 72 65 70 61 72 65 28 76 29 29 3d 3d 53 51 eprepare(v))==SQ
1b6ba 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1b6bb 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 sqlite3_reset(p
1b6bc 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 76 2d 3e Stmt);. v->
1b6bd 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 20 20 20 expired = 0;.
1b6be 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 }. if( rc==S
1b6bf 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 26 26 20 QLITE_SCHEMA &&
1b6c0 41 4c 57 41 59 53 28 76 2d 3e 69 73 50 72 65 70 ALWAYS(v->isPrep
1b6c1 61 72 65 56 32 29 20 26 26 20 41 4c 57 41 59 53 areV2) && ALWAYS
1b6c2 28 64 62 2d 3e 70 45 72 72 29 20 29 7b 0a 20 20 (db->pErr) ){.
1b6c3 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65 /* This case
1b6c4 20 6f 63 63 75 72 73 20 61 66 74 65 72 20 66 61 occurs after fa
1b6c5 69 6c 69 6e 67 20 74 6f 20 72 65 63 6f 6d 70 69 iling to recompi
1b6c6 6c 65 20 61 6e 20 73 71 6c 20 73 74 61 74 65 6d le an sql statem
1b6c7 65 6e 74 2e 20 0a 20 20 20 20 20 20 2a 2a 20 54 ent. . ** T
1b6c8 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
1b6c9 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 20 63 6f from the SQL co
1b6ca 6d 70 69 6c 65 72 20 68 61 73 20 61 6c 72 65 61 mpiler has alrea
1b6cb 64 79 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 0a dy been loaded .
1b6cc 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 ** into th
1b6cd 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
1b6ce 65 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f e. This block co
1b6cf 70 69 65 73 20 74 68 65 20 65 72 72 6f 72 20 6d pies the error m
1b6d0 65 73 73 61 67 65 20 0a 20 20 20 20 20 20 2a 2a essage . **
1b6d1 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 from the databa
1b6d2 73 65 20 68 61 6e 64 6c 65 20 69 6e 74 6f 20 74 se handle into t
1b6d3 68 65 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 he statement and
1b6d4 20 73 65 74 73 20 74 68 65 20 73 74 61 74 65 6d sets the statem
1b6d5 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f ent. ** pro
1b6d6 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 74 6f 20 gram counter to
1b6d7 30 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 0 to ensure that
1b6d8 20 77 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d when the statem
1b6d9 65 6e 74 20 69 73 20 0a 20 20 20 20 20 20 2a 2a ent is . **
1b6da 20 66 69 6e 61 6c 69 7a 65 64 20 6f 72 20 72 65 finalized or re
1b6db 73 65 74 20 74 68 65 20 70 61 72 73 65 72 20 65 set the parser e
1b6dc 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 rror message is
1b6dd 61 76 61 69 6c 61 62 6c 65 20 76 69 61 0a 20 20 available via.
1b6de 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 65 ** sqlite3_e
1b6df 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69 rrmsg() and sqli
1b6e0 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2e 0a 20 te3_errcode()..
1b6e1 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f */. co
1b6e2 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72 20 3d nst char *zErr =
1b6e3 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 (const char *)s
1b6e4 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1b6e5 74 28 64 62 2d 3e 70 45 72 72 29 3b 20 0a 20 20 t(db->pErr); .
1b6e6 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1b6e7 65 28 64 62 2c 20 76 2d 3e 7a 45 72 72 4d 73 67 e(db, v->zErrMsg
1b6e8 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 64 62 );. if( !db
1b6e9 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1b6ea 7b 0a 20 20 20 20 20 20 20 20 76 2d 3e 7a 45 72 {. v->zEr
1b6eb 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62 rMsg = sqlite3Db
1b6ec 53 74 72 44 75 70 28 64 62 2c 20 7a 45 72 72 29 StrDup(db, zErr)
1b6ed 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b ;. } else {
1b6ee 0a 20 20 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 . v->zErr
1b6ef 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 Msg = 0;.
1b6f0 20 76 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f v->rc = SQLITE_
1b6f1 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 NOMEM;. }.
1b6f2 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 }. rc = sq
1b6f3 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c lite3ApiExit(db,
1b6f4 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 rc);. sqlite
1b6f5 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 3_mutex_leave(db
1b6f6 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 ->mutex);. }.
1b6f7 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1b6f8 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 .** Extract the
1b6f9 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61 user data from a
1b6fa 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1b6fb 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 structure and r
1b6fc 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 eturn a.** point
1b6fd 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c er to it..*/.SQL
1b6fe 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 ITE_API void *sq
1b6ff 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 lite3_user_data(
1b700 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b701 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
1b702 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a && p->pFunc );.
1b703 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e return p->pFun
1b704 63 2d 3e 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a c->pUserData;.}.
1b705 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 ./*.** Extract t
1b706 68 65 20 75 73 65 72 20 64 61 74 61 20 66 72 6f he user data fro
1b707 6d 20 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 m a sqlite3_cont
1b708 65 78 74 20 73 74 72 75 63 74 75 72 65 20 61 6e ext structure an
1b709 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f d return a.** po
1b70a 69 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a inter to it..*/.
1b70b 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
1b70c 65 33 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 e3 *sqlite3_cont
1b70d 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 ext_db_handle(sq
1b70e 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1b70f 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 ){. assert( p &
1b710 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 & p->pFunc );.
1b711 72 65 74 75 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a return p->s.db;.
1b712 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c }../*.** The fol
1b713 6c 6f 77 69 6e 67 20 69 73 20 74 68 65 20 69 6d lowing is the im
1b714 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
1b715 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 an SQL function
1b716 74 68 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 66 that always.** f
1b717 61 69 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 ails with an err
1b718 6f 72 20 6d 65 73 73 61 67 65 20 73 74 61 74 69 or message stati
1b719 6e 67 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 ng that the func
1b71a 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 20 tion is used in
1b71b 74 68 65 0a 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e the.** wrong con
1b71c 74 65 78 74 2e 20 20 54 68 65 20 73 71 6c 69 74 text. The sqlit
1b71d 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 e3_overload_func
1b71e 74 69 6f 6e 28 29 20 41 50 49 20 6d 69 67 68 74 tion() API might
1b71f 20 63 6f 6e 73 74 72 75 63 74 0a 2a 2a 20 53 51 construct.** SQ
1b720 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 L function that
1b721 75 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 use this routine
1b722 20 73 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e so that the fun
1b723 63 74 69 6f 6e 73 20 77 69 6c 6c 20 65 78 69 73 ctions will exis
1b724 74 0a 2a 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65 t.** for name re
1b725 73 6f 6c 75 74 69 6f 6e 20 62 75 74 20 61 72 65 solution but are
1b726 20 61 63 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f actually overlo
1b727 61 64 65 64 20 62 79 20 74 68 65 20 78 46 69 6e aded by the xFin
1b728 64 46 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 dFunction.** met
1b729 68 6f 64 20 6f 66 20 76 69 72 74 75 61 6c 20 74 hod of virtual t
1b72a 61 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ables..*/.SQLITE
1b72b 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1b72c 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 lite3InvalidFunc
1b72d 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f tion(. sqlite3_
1b72e 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
1b72f 2c 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 , /* The functi
1b730 6f 6e 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 on calling conte
1b731 78 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 6f 74 55 xt */. int NotU
1b732 73 65 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 sed,
1b733 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1b734 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 arguments to the
1b735 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 function */. s
1b736 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e qlite3_value **N
1b737 6f 74 55 73 65 64 32 20 20 20 2f 2a 20 56 61 6c otUsed2 /* Val
1b738 75 65 20 6f 66 20 65 61 63 68 20 61 72 67 75 6d ue of each argum
1b739 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 ent */.){. cons
1b73a 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 t char *zName =
1b73b 63 6f 6e 74 65 78 74 2d 3e 70 46 75 6e 63 2d 3e context->pFunc->
1b73c 7a 4e 61 6d 65 3b 0a 20 20 63 68 61 72 20 2a 7a zName;. char *z
1b73d 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 Err;. UNUSED_PA
1b73e 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 RAMETER2(NotUsed
1b73f 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 7a , NotUsed2);. z
1b740 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 Err = sqlite3_mp
1b741 72 69 6e 74 66 28 0a 20 20 20 20 20 20 22 75 6e rintf(. "un
1b742 61 62 6c 65 20 74 6f 20 75 73 65 20 66 75 6e 63 able to use func
1b743 74 69 6f 6e 20 25 73 20 69 6e 20 74 68 65 20 72 tion %s in the r
1b744 65 71 75 65 73 74 65 64 20 63 6f 6e 74 65 78 74 equested context
1b745 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c ", zName);. sql
1b746 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
1b747 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c r(context, zErr,
1b748 20 2d 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f -1);. sqlite3_
1b749 66 72 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f free(zErr);.}../
1b74a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 *.** Allocate or
1b74b 20 72 65 74 75 72 6e 20 74 68 65 20 61 67 67 72 return the aggr
1b74c 65 67 61 74 65 20 63 6f 6e 74 65 78 74 20 66 6f egate context fo
1b74d 72 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f r a user functio
1b74e 6e 2e 20 20 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e n. A new.** con
1b74f 74 65 78 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 text is allocate
1b750 64 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 d on the first c
1b751 61 6c 6c 2e 20 20 53 75 62 73 65 71 75 65 6e 74 all. Subsequent
1b752 20 63 61 6c 6c 73 20 72 65 74 75 72 6e 20 74 68 calls return th
1b753 65 0a 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 65 78 e.** same contex
1b754 74 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72 t that was retur
1b755 6e 65 64 20 6f 6e 20 70 72 69 6f 72 20 63 61 6c ned on prior cal
1b756 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ls..*/.SQLITE_AP
1b757 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f I void *sqlite3_
1b758 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 aggregate_contex
1b759 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 t(sqlite3_contex
1b75a 74 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 29 t *p, int nByte)
1b75b 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 {. Mem *pMem;.
1b75c 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d assert( p && p-
1b75d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 >pFunc && p->pFu
1b75e 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 61 nc->xStep );. a
1b75f 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1b760 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 utex_held(p->s.d
1b761 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 b->mutex) );. p
1b762 4d 65 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 Mem = p->pMem;.
1b763 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 if( (pMem->flag
1b764 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 s & MEM_Agg)==0
1b765 29 7b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 ){. if( nByte
1b766 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==0 ){. sql
1b767 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1b768 73 65 45 78 74 65 72 6e 61 6c 28 70 4d 65 6d 29 seExternal(pMem)
1b769 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c ;. pMem->fl
1b76a 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a ags = MEM_Null;.
1b76b 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 pMem->z =
1b76c 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 0;. }else{.
1b76d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1b76e 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 emGrow(pMem, nBy
1b76f 74 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 4d te, 0);. pM
1b770 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
1b771 41 67 67 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d Agg;. pMem-
1b772 3e 75 2e 70 44 65 66 20 3d 20 70 2d 3e 70 46 75 >u.pDef = p->pFu
1b773 6e 63 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d nc;. if( pM
1b774 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20 em->z ){.
1b775 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c memset(pMem->z,
1b776 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 0, nByte);.
1b777 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
1b778 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d return (void*)pM
1b779 65 6d 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 em->z;.}../*.**
1b77a 52 65 74 75 72 6e 20 74 68 65 20 61 75 78 69 6c Return the auxil
1b77b 61 72 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72 ary data pointer
1b77c 2c 20 69 66 20 61 6e 79 2c 20 66 6f 72 20 74 68 , if any, for th
1b77d 65 20 69 41 72 67 27 74 68 20 61 72 67 75 6d 65 e iArg'th argume
1b77e 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 75 73 65 nt to.** the use
1b77f 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e r-function defin
1b780 65 64 20 62 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 ed by pCtx..*/.S
1b781 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
1b782 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 sqlite3_get_auxd
1b783 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ata(sqlite3_cont
1b784 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 ext *pCtx, int i
1b785 41 72 67 29 7b 0a 20 20 56 64 62 65 46 75 6e 63 Arg){. VdbeFunc
1b786 20 2a 70 56 64 62 65 46 75 6e 63 3b 0a 0a 20 20 *pVdbeFunc;..
1b787 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1b788 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
1b789 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
1b78a 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70 . pVdbeFunc = p
1b78b 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a Ctx->pVdbeFunc;.
1b78c 20 20 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 if( !pVdbeFunc
1b78d 20 7c 7c 20 69 41 72 67 3e 3d 70 56 64 62 65 46 || iArg>=pVdbeF
1b78e 75 6e 63 2d 3e 6e 41 75 78 20 7c 7c 20 69 41 72 unc->nAux || iAr
1b78f 67 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 g<0 ){. retur
1b790 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 n 0;. }. retur
1b791 6e 20 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 n pVdbeFunc->apA
1b792 75 78 5b 69 41 72 67 5d 2e 70 41 75 78 3b 0a 7d ux[iArg].pAux;.}
1b793 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
1b794 61 75 78 69 6c 61 72 79 20 64 61 74 61 20 70 6f auxilary data po
1b795 69 6e 74 65 72 20 61 6e 64 20 64 65 6c 65 74 65 inter and delete
1b796 20 66 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 20 74 function, for t
1b797 68 65 20 69 41 72 67 27 74 68 0a 2a 2a 20 61 72 he iArg'th.** ar
1b798 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 73 gument to the us
1b799 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 er-function defi
1b79a 6e 65 64 20 62 79 20 70 43 74 78 2e 20 41 6e 79 ned by pCtx. Any
1b79b 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 previous value
1b79c 69 73 0a 2a 2a 20 64 65 6c 65 74 65 64 20 62 79 is.** deleted by
1b79d 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 6c calling the del
1b79e 65 74 65 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 ete function spe
1b79f 63 69 66 69 65 64 20 77 68 65 6e 20 69 74 20 77 cified when it w
1b7a0 61 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 as set..*/.SQLIT
1b7a1 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
1b7a2 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 0a e3_set_auxdata(.
1b7a3 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
1b7a4 74 20 2a 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 t *pCtx, . int
1b7a5 69 41 72 67 2c 20 0a 20 20 76 6f 69 64 20 2a 70 iArg, . void *p
1b7a6 41 75 78 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 Aux, . void (*x
1b7a7 44 65 6c 65 74 65 29 28 76 6f 69 64 2a 29 0a 29 Delete)(void*).)
1b7a8 7b 0a 20 20 73 74 72 75 63 74 20 41 75 78 44 61 {. struct AuxDa
1b7a9 74 61 20 2a 70 41 75 78 44 61 74 61 3b 0a 20 20 ta *pAuxData;.
1b7aa 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 VdbeFunc *pVdbeF
1b7ab 75 6e 63 3b 0a 20 20 69 66 28 20 69 41 72 67 3c unc;. if( iArg<
1b7ac 30 20 29 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0 ) goto failed;
1b7ad 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
1b7ae 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1b7af 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 Ctx->s.db->mutex
1b7b0 29 20 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 ) );. pVdbeFunc
1b7b1 20 3d 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 = pCtx->pVdbeFu
1b7b2 6e 63 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 nc;. if( !pVdbe
1b7b3 46 75 6e 63 20 7c 7c 20 70 56 64 62 65 46 75 6e Func || pVdbeFun
1b7b4 63 2d 3e 6e 41 75 78 3c 3d 69 41 72 67 20 29 7b c->nAux<=iArg ){
1b7b5 0a 20 20 20 20 69 6e 74 20 6e 41 75 78 20 3d 20 . int nAux =
1b7b6 28 70 56 64 62 65 46 75 6e 63 20 3f 20 70 56 64 (pVdbeFunc ? pVd
1b7b7 62 65 46 75 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 beFunc->nAux : 0
1b7b8 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 6c 6c );. int nMall
1b7b9 6f 63 20 3d 20 73 69 7a 65 6f 66 28 56 64 62 65 oc = sizeof(Vdbe
1b7ba 46 75 6e 63 29 20 2b 20 73 69 7a 65 6f 66 28 73 Func) + sizeof(s
1b7bb 74 72 75 63 74 20 41 75 78 44 61 74 61 29 2a 69 truct AuxData)*i
1b7bc 41 72 67 3b 0a 20 20 20 20 70 56 64 62 65 46 75 Arg;. pVdbeFu
1b7bd 6e 63 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 nc = sqlite3DbRe
1b7be 61 6c 6c 6f 63 28 70 43 74 78 2d 3e 73 2e 64 62 alloc(pCtx->s.db
1b7bf 2c 20 70 56 64 62 65 46 75 6e 63 2c 20 6e 4d 61 , pVdbeFunc, nMa
1b7c0 6c 6c 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 21 lloc);. if( !
1b7c1 70 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 pVdbeFunc ){.
1b7c2 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a goto failed;.
1b7c3 20 20 20 20 7d 0a 20 20 20 20 70 43 74 78 2d 3e }. pCtx->
1b7c4 70 56 64 62 65 46 75 6e 63 20 3d 20 70 56 64 62 pVdbeFunc = pVdb
1b7c5 65 46 75 6e 63 3b 0a 20 20 20 20 6d 65 6d 73 65 eFunc;. memse
1b7c6 74 28 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 t(&pVdbeFunc->ap
1b7c7 41 75 78 5b 6e 41 75 78 5d 2c 20 30 2c 20 73 69 Aux[nAux], 0, si
1b7c8 7a 65 6f 66 28 73 74 72 75 63 74 20 41 75 78 44 zeof(struct AuxD
1b7c9 61 74 61 29 2a 28 69 41 72 67 2b 31 2d 6e 41 75 ata)*(iArg+1-nAu
1b7ca 78 29 29 3b 0a 20 20 20 20 70 56 64 62 65 46 75 x));. pVdbeFu
1b7cb 6e 63 2d 3e 6e 41 75 78 20 3d 20 69 41 72 67 2b nc->nAux = iArg+
1b7cc 31 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 1;. pVdbeFunc
1b7cd 2d 3e 70 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e ->pFunc = pCtx->
1b7ce 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 pFunc;. }.. pA
1b7cf 75 78 44 61 74 61 20 3d 20 26 70 56 64 62 65 46 uxData = &pVdbeF
1b7d0 75 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d unc->apAux[iArg]
1b7d1 3b 0a 20 20 69 66 28 20 70 41 75 78 44 61 74 61 ;. if( pAuxData
1b7d2 2d 3e 70 41 75 78 20 26 26 20 70 41 75 78 44 61 ->pAux && pAuxDa
1b7d3 74 61 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 ta->xDelete ){.
1b7d4 20 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 pAuxData->xDe
1b7d5 6c 65 74 65 28 70 41 75 78 44 61 74 61 2d 3e 70 lete(pAuxData->p
1b7d6 41 75 78 29 3b 0a 20 20 7d 0a 20 20 70 41 75 78 Aux);. }. pAux
1b7d7 44 61 74 61 2d 3e 70 41 75 78 20 3d 20 70 41 75 Data->pAux = pAu
1b7d8 78 3b 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 78 x;. pAuxData->x
1b7d9 44 65 6c 65 74 65 20 3d 20 78 44 65 6c 65 74 65 Delete = xDelete
1b7da 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 66 61 69 ;. return;..fai
1b7db 6c 65 64 3a 0a 20 20 69 66 28 20 78 44 65 6c 65 led:. if( xDele
1b7dc 74 65 20 29 7b 0a 20 20 20 20 78 44 65 6c 65 74 te ){. xDelet
1b7dd 65 28 70 41 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a e(pAux);. }.}..
1b7de 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1b7df 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f MIT_DEPRECATED./
1b7e0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1b7e1 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 number of times
1b7e2 74 68 65 20 53 74 65 70 20 66 75 6e 63 74 69 6f the Step functio
1b7e3 6e 20 6f 66 20 61 20 61 67 67 72 65 67 61 74 65 n of a aggregate
1b7e4 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20 63 61 has been .** ca
1b7e5 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 lled..**.** This
1b7e6 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 function is dep
1b7e7 72 65 63 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 recated. Do not
1b7e8 20 75 73 65 20 69 74 20 66 6f 72 20 6e 65 77 20 use it for new
1b7e9 63 6f 64 65 2e 20 20 49 74 20 69 73 0a 2a 2a 20 code. It is.**
1b7ea 70 72 6f 76 69 64 65 20 6f 6e 6c 79 20 74 6f 20 provide only to
1b7eb 61 76 6f 69 64 20 62 72 65 61 6b 69 6e 67 20 6c avoid breaking l
1b7ec 65 67 61 63 79 20 63 6f 64 65 2e 20 20 4e 65 77 egacy code. New
1b7ed 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
1b7ee 69 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 ion.** implement
1b7ef 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65 ations should ke
1b7f0 65 70 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 75 ep their own cou
1b7f1 6e 74 73 20 77 69 74 68 69 6e 20 74 68 65 69 72 nts within their
1b7f2 20 61 67 67 72 65 67 61 74 65 0a 2a 2a 20 63 6f aggregate.** co
1b7f3 6e 74 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ntext..*/.SQLITE
1b7f4 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1b7f5 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 _aggregate_count
1b7f6 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
1b7f7 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
1b7f8 70 20 26 26 20 70 2d 3e 70 4d 65 6d 20 26 26 20 p && p->pMem &&
1b7f9 70 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 p->pFunc && p->p
1b7fa 46 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 Func->xStep );.
1b7fb 20 72 65 74 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d return p->pMem-
1b7fc 3e 6e 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a >n;.}.#endif../*
1b7fd 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
1b7fe 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
1b7ff 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
1b800 65 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 65 et for the state
1b801 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 ment pStmt..*/.S
1b802 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1b803 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 lite3_column_cou
1b804 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 nt(sqlite3_stmt
1b805 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 *pStmt){. Vdbe
1b806 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 *pVm = (Vdbe *)p
1b807 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 Stmt;. return p
1b808 56 6d 20 3f 20 70 56 6d 2d 3e 6e 52 65 73 43 6f Vm ? pVm->nResCo
1b809 6c 75 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a lumn : 0;.}../*.
1b80a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
1b80b 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 61 mber of values a
1b80c 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74 68 vailable from th
1b80d 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 e current row of
1b80e 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c the.** currentl
1b80f 79 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 y executing stat
1b810 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a ement pStmt..*/.
1b811 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1b812 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e qlite3_data_coun
1b813 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a t(sqlite3_stmt *
1b814 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a pStmt){. Vdbe *
1b815 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 pVm = (Vdbe *)pS
1b816 74 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d 3d 3d tmt;. if( pVm==
1b817 30 20 7c 7c 20 70 56 6d 2d 3e 70 52 65 73 75 6c 0 || pVm->pResul
1b818 74 53 65 74 3d 3d 30 20 29 20 72 65 74 75 72 6e tSet==0 ) return
1b819 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 0;. return pVm
1b81a 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a ->nResColumn;.}.
1b81b 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f ../*.** Check to
1b81c 20 73 65 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 see if column i
1b81d 43 6f 6c 20 6f 66 20 74 68 65 20 67 69 76 65 6e Col of the given
1b81e 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 76 61 statement is va
1b81f 6c 69 64 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 lid. If.** it i
1b820 73 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e s, return a poin
1b821 74 65 72 20 74 6f 20 74 68 65 20 4d 65 6d 20 66 ter to the Mem f
1b822 6f 72 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 or the value of
1b823 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 that column..**
1b824 49 66 20 69 43 6f 6c 20 69 73 20 6e 6f 74 20 76 If iCol is not v
1b825 61 6c 69 64 2c 20 72 65 74 75 72 6e 20 61 20 70 alid, return a p
1b826 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 20 ointer to a Mem
1b827 77 68 69 63 68 20 68 61 73 20 61 20 76 61 6c 75 which has a valu
1b828 65 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f e.** of NULL..*/
1b829 0a 73 74 61 74 69 63 20 4d 65 6d 20 2a 63 6f 6c .static Mem *col
1b82a 75 6d 6e 4d 65 6d 28 73 71 6c 69 74 65 33 5f 73 umnMem(sqlite3_s
1b82b 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1b82c 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 3b i){. Vdbe *pVm;
1b82d 0a 20 20 69 6e 74 20 76 61 6c 73 3b 0a 20 20 4d . int vals;. M
1b82e 65 6d 20 2a 70 4f 75 74 3b 0a 0a 20 20 70 56 6d em *pOut;.. pVm
1b82f 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 = (Vdbe *)pStmt
1b830 3b 0a 20 20 69 66 28 20 70 56 6d 20 26 26 20 70 ;. if( pVm && p
1b831 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 21 3d Vm->pResultSet!=
1b832 30 20 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 0 && i<pVm->nRes
1b833 43 6f 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 20 29 Column && i>=0 )
1b834 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 {. sqlite3_mu
1b835 74 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 tex_enter(pVm->d
1b836 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 76 b->mutex);. v
1b837 61 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f 64 61 als = sqlite3_da
1b838 74 61 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b ta_count(pStmt);
1b839 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70 56 6d . pOut = &pVm
1b83a 2d 3e 70 52 65 73 75 6c 74 53 65 74 5b 69 5d 3b ->pResultSet[i];
1b83b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
1b83c 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 70 61 If the value pa
1b83d 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f ssed as the seco
1b83e 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f nd argument is o
1b83f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 72 65 74 ut of range, ret
1b840 75 72 6e 0a 20 20 20 20 2a 2a 20 61 20 70 6f 69 urn. ** a poi
1b841 6e 74 65 72 20 74 6f 20 74 68 65 20 66 6f 6c 6c nter to the foll
1b842 6f 77 69 6e 67 20 73 74 61 74 69 63 20 4d 65 6d owing static Mem
1b843 20 6f 62 6a 65 63 74 20 77 68 69 63 68 20 63 6f object which co
1b844 6e 74 61 69 6e 73 20 74 68 65 0a 20 20 20 20 2a ntains the. *
1b845 2a 20 76 61 6c 75 65 20 53 51 4c 20 4e 55 4c 4c * value SQL NULL
1b846 2e 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 . Even though th
1b847 65 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 20 e Mem structure
1b848 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6c 65 6d contains an elem
1b849 65 6e 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 79 ent. ** of ty
1b84a 70 65 20 69 36 34 2c 20 6f 6e 20 63 65 72 74 61 pe i64, on certa
1b84b 69 6e 20 61 72 63 68 69 74 65 63 74 75 72 65 20 in architecture
1b84c 28 78 38 36 29 20 77 69 74 68 20 63 65 72 74 61 (x86) with certa
1b84d 69 6e 20 63 6f 6d 70 69 6c 65 72 0a 20 20 20 20 in compiler.
1b84e 2a 2a 20 73 77 69 74 63 68 65 73 20 28 2d 4f 73 ** switches (-Os
1b84f 29 2c 20 67 63 63 20 6d 61 79 20 61 6c 69 67 6e ), gcc may align
1b850 20 74 68 69 73 20 4d 65 6d 20 6f 62 6a 65 63 74 this Mem object
1b851 20 6f 6e 20 61 20 34 2d 62 79 74 65 20 62 6f 75 on a 4-byte bou
1b852 6e 64 61 72 79 0a 20 20 20 20 2a 2a 20 69 6e 73 ndary. ** ins
1b853 74 65 61 64 20 6f 66 20 61 6e 20 38 2d 62 79 74 tead of an 8-byt
1b854 65 20 6f 6e 65 2e 20 54 68 69 73 20 61 6c 6c 20 e one. This all
1b855 77 6f 72 6b 73 20 66 69 6e 65 2c 20 65 78 63 65 works fine, exce
1b856 70 74 20 74 68 61 74 20 77 68 65 6e 0a 20 20 20 pt that when.
1b857 20 2a 2a 20 72 75 6e 6e 69 6e 67 20 77 69 74 68 ** running with
1b858 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 64 65 SQLITE_DEBUG de
1b859 66 69 6e 65 64 20 74 68 65 20 53 51 4c 69 74 65 fined the SQLite
1b85a 20 63 6f 64 65 20 73 6f 6d 65 74 69 6d 65 73 20 code sometimes
1b85b 61 73 73 65 72 74 28 29 73 0a 20 20 20 20 2a 2a assert()s. **
1b85c 20 74 68 61 74 20 61 20 4d 65 6d 20 73 74 72 75 that a Mem stru
1b85d 63 74 75 72 65 20 69 73 20 6c 6f 63 61 74 65 64 cture is located
1b85e 20 6f 6e 20 61 6e 20 38 2d 62 79 74 65 20 62 6f on an 8-byte bo
1b85f 75 6e 64 61 72 79 2e 20 54 6f 20 70 72 65 76 65 undary. To preve
1b860 6e 74 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 61 nt. ** this a
1b861 73 73 65 72 74 28 29 20 66 72 6f 6d 20 66 61 69 ssert() from fai
1b862 6c 69 6e 67 2c 20 77 68 65 6e 20 62 75 69 6c 64 ling, when build
1b863 69 6e 67 20 77 69 74 68 20 53 51 4c 49 54 45 5f ing with SQLITE_
1b864 44 45 42 55 47 20 64 65 66 69 6e 65 64 0a 20 20 DEBUG defined.
1b865 20 20 2a 2a 20 75 73 69 6e 67 20 67 63 63 2c 20 ** using gcc,
1b866 66 6f 72 63 65 20 6e 75 6c 6c 4d 65 6d 20 74 6f force nullMem to
1b867 20 62 65 20 38 2d 62 79 74 65 20 61 6c 69 67 6e be 8-byte align
1b868 65 64 20 75 73 69 6e 67 20 74 68 65 20 6d 61 67 ed using the mag
1b869 69 63 61 6c 0a 20 20 20 20 2a 2a 20 5f 5f 61 74 ical. ** __at
1b86a 74 72 69 62 75 74 65 5f 5f 28 28 61 6c 69 67 6e tribute__((align
1b86b 65 64 28 38 29 29 29 20 6d 61 63 72 6f 2e 20 20 ed(8))) macro.
1b86c 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f */. static co
1b86d 6e 73 74 20 4d 65 6d 20 6e 75 6c 6c 4d 65 6d 20 nst Mem nullMem
1b86e 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
1b86f 49 54 45 5f 44 45 42 55 47 29 20 26 26 20 64 65 ITE_DEBUG) && de
1b870 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a fined(__GNUC__).
1b871 20 20 20 20 20 20 5f 5f 61 74 74 72 69 62 75 74 __attribut
1b872 65 5f 5f 28 28 61 6c 69 67 6e 65 64 28 38 29 29 e__((aligned(8))
1b873 29 20 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 ) .#endif.
1b874 3d 20 7b 7b 30 7d 2c 20 28 64 6f 75 62 6c 65 29 = {{0}, (double)
1b875 30 2c 20 30 2c 20 22 22 2c 20 30 2c 20 4d 45 4d 0, 0, "", 0, MEM
1b876 5f 4e 75 6c 6c 2c 20 53 51 4c 49 54 45 5f 4e 55 _Null, SQLITE_NU
1b877 4c 4c 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a LL, 0, 0, 0 };..
1b878 20 20 20 20 69 66 28 20 70 56 6d 20 26 26 20 41 if( pVm && A
1b879 4c 57 41 59 53 28 70 56 6d 2d 3e 64 62 29 20 29 LWAYS(pVm->db) )
1b87a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
1b87b 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d mutex_enter(pVm-
1b87c 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 >db->mutex);.
1b87d 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 sqlite3Error(
1b87e 70 56 6d 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f pVm->db, SQLITE_
1b87f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 20 7d RANGE, 0);. }
1b880 0a 20 20 20 20 70 4f 75 74 20 3d 20 28 4d 65 6d . pOut = (Mem
1b881 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a 20 20 7d 0a *)&nullMem;. }.
1b882 20 20 72 65 74 75 72 6e 20 70 4f 75 74 3b 0a 7d return pOut;.}
1b883 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
1b884 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
1b885 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67 20 61 after invoking a
1b886 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f n sqlite3_value_
1b887 58 58 58 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 20 XXX function on
1b888 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 76 61 6c a .** column val
1b889 75 65 20 28 69 2e 65 2e 20 61 20 76 61 6c 75 65 ue (i.e. a value
1b88a 20 72 65 74 75 72 6e 65 64 20 62 79 20 65 76 61 returned by eva
1b88b 6c 75 61 74 69 6e 67 20 61 6e 20 53 51 4c 20 65 luating an SQL e
1b88c 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 xpression in the
1b88d 0a 2a 2a 20 73 65 6c 65 63 74 20 6c 69 73 74 20 .** select list
1b88e 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 of a SELECT stat
1b88f 65 6d 65 6e 74 29 20 74 68 61 74 20 6d 61 79 20 ement) that may
1b890 63 61 75 73 65 20 61 20 6d 61 6c 6c 6f 63 28 29 cause a malloc()
1b891 20 66 61 69 6c 75 72 65 2e 20 49 66 20 0a 2a 2a failure. If .**
1b892 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 malloc() has fa
1b893 69 6c 65 64 2c 20 74 68 65 20 74 68 72 65 61 64 iled, the thread
1b894 73 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 s mallocFailed f
1b895 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64 20 61 lag is cleared a
1b896 6e 64 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a nd the result.**
1b897 20 63 6f 64 65 20 6f 66 20 73 74 61 74 65 6d 65 code of stateme
1b898 6e 74 20 70 53 74 6d 74 20 73 65 74 20 74 6f 20 nt pStmt set to
1b899 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2a SQLITE_NOMEM..**
1b89a 0a 2a 2a 20 53 70 65 63 69 66 69 63 61 6c 6c 79 .** Specifically
1b89b 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 , this is called
1b89c 20 66 72 6f 6d 20 77 69 74 68 69 6e 3a 0a 2a 2a from within:.**
1b89d 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f .** sqlite3_
1b89e 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29 0a 2a 2a 20 column_int().**
1b89f 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 sqlite3_colu
1b8a0 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a 2a 20 20 20 mn_int64().**
1b8a1 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e sqlite3_column
1b8a2 5f 74 65 78 74 28 29 0a 2a 2a 20 20 20 20 20 73 _text().** s
1b8a3 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
1b8a4 78 74 31 36 28 29 0a 2a 2a 20 20 20 20 20 73 71 xt16().** sq
1b8a5 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 72 65 61 lite3_column_rea
1b8a6 6c 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 l().** sqlit
1b8a7 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 e3_column_bytes(
1b8a8 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 ).** sqlite3
1b8a9 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 _column_bytes16(
1b8aa 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 ).**.** But not
1b8ab 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 for sqlite3_colu
1b8ac 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77 68 69 63 68 mn_blob(), which
1b8ad 20 6e 65 76 65 72 20 63 61 6c 6c 73 20 6d 61 6c never calls mal
1b8ae 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 loc()..*/.static
1b8af 20 76 6f 69 64 20 63 6f 6c 75 6d 6e 4d 61 6c 6c void columnMall
1b8b0 6f 63 46 61 69 6c 75 72 65 28 73 71 6c 69 74 65 ocFailure(sqlite
1b8b1 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 0a 7b 3_stmt *pStmt).{
1b8b2 0a 20 20 2f 2a 20 49 66 20 6d 61 6c 6c 6f 63 28 . /* If malloc(
1b8b3 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e 67 20 ) failed during
1b8b4 61 6e 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 an encoding conv
1b8b5 65 72 73 69 6f 6e 20 77 69 74 68 69 6e 20 61 6e ersion within an
1b8b6 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f . ** sqlite3_co
1b8b7 6c 75 6d 6e 5f 58 58 58 20 41 50 49 2c 20 74 68 lumn_XXX API, th
1b8b8 65 6e 20 73 65 74 20 74 68 65 20 72 65 74 75 72 en set the retur
1b8b9 6e 20 63 6f 64 65 20 6f 66 20 74 68 65 20 73 74 n code of the st
1b8ba 61 74 65 6d 65 6e 74 20 74 6f 0a 20 20 2a 2a 20 atement to. **
1b8bb 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 54 68 SQLITE_NOMEM. Th
1b8bc 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 5f e next call to _
1b8bd 73 74 65 70 28 29 20 28 69 66 20 61 6e 79 29 20 step() (if any)
1b8be 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 will return SQLI
1b8bf 54 45 5f 45 52 52 4f 52 0a 20 20 2a 2a 20 61 6e TE_ERROR. ** an
1b8c0 64 20 5f 66 69 6e 61 6c 69 7a 65 28 29 20 77 69 d _finalize() wi
1b8c1 6c 6c 20 72 65 74 75 72 6e 20 4e 4f 4d 45 4d 2e ll return NOMEM.
1b8c2 0a 20 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 20 . */. Vdbe *p
1b8c3 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b = (Vdbe *)pStmt;
1b8c4 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
1b8c5 70 2d 3e 72 63 20 3d 20 73 71 6c 69 74 65 33 41 p->rc = sqlite3A
1b8c6 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 70 2d piExit(p->db, p-
1b8c7 3e 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 >rc);. sqlite
1b8c8 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3_mutex_leave(p-
1b8c9 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d >db->mutex);. }
1b8ca 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
1b8cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b8cc 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e * sqlite3_column
1b8cd 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a _ *************
1b8ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b8cf 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 **.** The follow
1b8d0 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ing routines are
1b8d1 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 used to access
1b8d2 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 elements of the
1b8d3 63 75 72 72 65 6e 74 20 72 6f 77 0a 2a 2a 20 69 current row.** i
1b8d4 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 n the result set
1b8d5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1b8d6 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
1b8d7 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 te3_column_blob(
1b8d8 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1b8d9 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 tmt, int i){. c
1b8da 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c 3b 0a onst void *val;.
1b8db 20 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f val = sqlite3_
1b8dc 76 61 6c 75 65 5f 62 6c 6f 62 28 20 63 6f 6c 75 value_blob( colu
1b8dd 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 mnMem(pStmt,i) )
1b8de 3b 0a 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 ;. /* Even thou
1b8df 67 68 20 74 68 65 72 65 20 69 73 20 6e 6f 20 65 gh there is no e
1b8e0 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 73 69 ncoding conversi
1b8e1 6f 6e 2c 20 76 61 6c 75 65 5f 62 6c 6f 62 28 29 on, value_blob()
1b8e2 20 6d 69 67 68 74 0a 20 20 2a 2a 20 6e 65 65 64 might. ** need
1b8e3 20 74 6f 20 63 61 6c 6c 20 6d 61 6c 6c 6f 63 28 to call malloc(
1b8e4 29 20 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20 ) to expand the
1b8e5 72 65 73 75 6c 74 20 6f 66 20 61 20 7a 65 72 6f result of a zero
1b8e6 62 6c 6f 62 28 29 20 0a 20 20 2a 2a 20 65 78 70 blob() . ** exp
1b8e7 72 65 73 73 69 6f 6e 2e 20 0a 20 20 2a 2f 0a 20 ression. . */.
1b8e8 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 columnMallocFai
1b8e9 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 lure(pStmt);. r
1b8ea 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c eturn val;.}.SQL
1b8eb 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1b8ec 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 te3_column_bytes
1b8ed 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1b8ee 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 Stmt, int i){.
1b8ef 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 int val = sqlite
1b8f0 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 20 63 3_value_bytes( c
1b8f1 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 olumnMem(pStmt,i
1b8f2 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c ) );. columnMal
1b8f3 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 locFailure(pStmt
1b8f4 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b );. return val;
1b8f5 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e .}.SQLITE_API in
1b8f6 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e t sqlite3_column
1b8f7 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 _bytes16(sqlite3
1b8f8 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1b8f9 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20 t i){. int val
1b8fa 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
1b8fb 62 79 74 65 73 31 36 28 20 63 6f 6c 75 6d 6e 4d bytes16( columnM
1b8fc 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 em(pStmt,i) );.
1b8fd 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 columnMallocFai
1b8fe 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 lure(pStmt);. r
1b8ff 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c eturn val;.}.SQL
1b900 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 ITE_API double s
1b901 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f qlite3_column_do
1b902 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d uble(sqlite3_stm
1b903 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1b904 7b 0a 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d {. double val =
1b905 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 sqlite3_value_d
1b906 6f 75 62 6c 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d ouble( columnMem
1b907 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 (pStmt,i) );. c
1b908 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 olumnMallocFailu
1b909 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 re(pStmt);. ret
1b90a 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 urn val;.}.SQLIT
1b90b 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1b90c 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 3_column_int(sql
1b90d 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1b90e 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 , int i){. int
1b90f 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 val = sqlite3_va
1b910 6c 75 65 5f 69 6e 74 28 20 63 6f 6c 75 6d 6e 4d lue_int( columnM
1b911 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 em(pStmt,i) );.
1b912 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 columnMallocFai
1b913 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 lure(pStmt);. r
1b914 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c eturn val;.}.SQL
1b915 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 5f 69 ITE_API sqlite_i
1b916 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 63 6f 6c nt64 sqlite3_col
1b917 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 umn_int64(sqlite
1b918 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
1b919 6e 74 20 69 29 7b 0a 20 20 73 71 6c 69 74 65 5f nt i){. sqlite_
1b91a 69 6e 74 36 34 20 76 61 6c 20 3d 20 73 71 6c 69 int64 val = sqli
1b91b 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 te3_value_int64(
1b91c 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 columnMem(pStmt
1b91d 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d ,i) );. columnM
1b91e 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 allocFailure(pSt
1b91f 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 mt);. return va
1b920 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 l;.}.SQLITE_API
1b921 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
1b922 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c har *sqlite3_col
1b923 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 65 33 umn_text(sqlite3
1b924 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1b925 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e t i){. const un
1b926 73 69 67 6e 65 64 20 63 68 61 72 20 2a 76 61 6c signed char *val
1b927 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
1b928 5f 74 65 78 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d _text( columnMem
1b929 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 (pStmt,i) );. c
1b92a 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 olumnMallocFailu
1b92b 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 re(pStmt);. ret
1b92c 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 urn val;.}.SQLIT
1b92d 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 E_API sqlite3_va
1b92e 6c 75 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c lue *sqlite3_col
1b92f 75 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 umn_value(sqlite
1b930 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
1b931 6e 74 20 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 4f nt i){. Mem *pO
1b932 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 ut = columnMem(p
1b933 53 74 6d 74 2c 20 69 29 3b 0a 20 20 69 66 28 20 Stmt, i);. if(
1b934 70 4f 75 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pOut->flags&MEM_
1b935 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 70 4f Static ){. pO
1b936 75 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 ut->flags &= ~ME
1b937 4d 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 70 4f M_Static;. pO
1b938 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d ut->flags |= MEM
1b939 5f 45 70 68 65 6d 3b 0a 20 20 7d 0a 20 20 63 6f _Ephem;. }. co
1b93a 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 lumnMallocFailur
1b93b 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 e(pStmt);. retu
1b93c 72 6e 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 rn (sqlite3_valu
1b93d 65 20 2a 29 70 4f 75 74 3b 0a 7d 0a 23 69 66 6e e *)pOut;.}.#ifn
1b93e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1b93f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 UTF16.SQLITE_API
1b940 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
1b941 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
1b942 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 16(sqlite3_stmt
1b943 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a *pStmt, int i){.
1b944 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 const void *va
1b945 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 l = sqlite3_valu
1b946 65 5f 74 65 78 74 31 36 28 20 63 6f 6c 75 6d 6e e_text16( column
1b947 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a Mem(pStmt,i) );.
1b948 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 columnMallocFa
1b949 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 ilure(pStmt);.
1b94a 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 23 65 return val;.}.#e
1b94b 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1b94c 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c MIT_UTF16 */.SQL
1b94d 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1b94e 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 te3_column_type(
1b94f 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1b950 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 tmt, int i){. i
1b951 6e 74 20 69 54 79 70 65 20 3d 20 73 71 6c 69 74 nt iType = sqlit
1b952 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 20 63 e3_value_type( c
1b953 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 olumnMem(pStmt,i
1b954 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c ) );. columnMal
1b955 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 locFailure(pStmt
1b956 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 54 79 70 );. return iTyp
1b957 65 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c e;.}../* The fol
1b958 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 lowing function
1b959 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 is experimental
1b95a 61 6e 64 20 73 75 62 6a 65 63 74 20 74 6f 20 63 and subject to c
1b95b 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f hange or.** remo
1b95c 76 61 6c 20 2a 2f 0a 2f 2a 69 6e 74 20 73 71 6c val */./*int sql
1b95d 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 75 6d 65 ite3_column_nume
1b95e 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33 ric_type(sqlite3
1b95f 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1b960 74 20 69 29 7b 0a 2a 2a 20 20 72 65 74 75 72 6e t i){.** return
1b961 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e sqlite3_value_n
1b962 75 6d 65 72 69 63 5f 74 79 70 65 28 20 63 6f 6c umeric_type( col
1b963 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 umnMem(pStmt,i)
1b964 29 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a );.**}.*/../*.**
1b965 20 43 6f 6e 76 65 72 74 20 74 68 65 20 4e 2d 74 Convert the N-t
1b966 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70 53 74 h element of pSt
1b967 6d 74 2d 3e 70 43 6f 6c 4e 61 6d 65 5b 5d 20 69 mt->pColName[] i
1b968 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 75 73 69 nto a string usi
1b969 6e 67 0a 2a 2a 20 78 46 75 6e 63 28 29 20 74 68 ng.** xFunc() th
1b96a 65 6e 20 72 65 74 75 72 6e 20 74 68 61 74 20 73 en return that s
1b96b 74 72 69 6e 67 2e 20 20 49 66 20 4e 20 69 73 20 tring. If N is
1b96c 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 72 65 out of range, re
1b96d 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 turn 0..**.** Th
1b96e 65 72 65 20 61 72 65 20 75 70 20 74 6f 20 35 20 ere are up to 5
1b96f 6e 61 6d 65 73 20 66 6f 72 20 65 61 63 68 20 63 names for each c
1b970 6f 6c 75 6d 6e 2e 20 20 75 73 65 54 79 70 65 20 olumn. useType
1b971 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68 determines which
1b972 0a 2a 2a 20 6e 61 6d 65 20 69 73 20 72 65 74 75 .** name is retu
1b973 72 6e 65 64 2e 20 20 48 65 72 65 20 61 72 65 20 rned. Here are
1b974 74 68 65 20 6e 61 6d 65 73 3a 0a 2a 2a 0a 2a 2a the names:.**.**
1b975 20 20 20 20 30 20 20 20 20 20 20 54 68 65 20 63 0 The c
1b976 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 20 69 74 olumn name as it
1b977 20 73 68 6f 75 6c 64 20 62 65 20 64 69 73 70 6c should be displ
1b978 61 79 65 64 20 66 6f 72 20 6f 75 74 70 75 74 0a ayed for output.
1b979 2a 2a 20 20 20 20 31 20 20 20 20 20 20 54 68 65 ** 1 The
1b97a 20 64 61 74 61 74 79 70 65 20 6e 61 6d 65 20 66 datatype name f
1b97b 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a or the column.**
1b97c 20 20 20 20 32 20 20 20 20 20 20 54 68 65 20 6e 2 The n
1b97d 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
1b97e 61 73 65 20 74 68 61 74 20 74 68 65 20 63 6f 6c ase that the col
1b97f 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d umn derives from
1b980 0a 2a 2a 20 20 20 20 33 20 20 20 20 20 20 54 68 .** 3 Th
1b981 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 e name of the ta
1b982 62 6c 65 20 74 68 61 74 20 74 68 65 20 63 6f 6c ble that the col
1b983 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d umn derives from
1b984 0a 2a 2a 20 20 20 20 34 20 20 20 20 20 20 54 68 .** 4 Th
1b985 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 e name of the ta
1b986 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 ble column that
1b987 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d the result colum
1b988 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a n derives from.*
1b989 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 *.** If the resu
1b98a 6c 74 20 69 73 20 6e 6f 74 20 61 20 73 69 6d 70 lt is not a simp
1b98b 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 le column refere
1b98c 6e 63 65 20 28 69 66 20 69 74 20 69 73 20 61 6e nce (if it is an
1b98d 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f expression.** o
1b98e 72 20 61 20 63 6f 6e 73 74 61 6e 74 29 20 74 68 r a constant) th
1b98f 65 6e 20 75 73 65 54 79 70 65 73 20 32 2c 20 33 en useTypes 2, 3
1b990 2c 20 61 6e 64 20 34 20 72 65 74 75 72 6e 20 4e , and 4 return N
1b991 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 ULL..*/.static c
1b992 6f 6e 73 74 20 76 6f 69 64 20 2a 63 6f 6c 75 6d onst void *colum
1b993 6e 4e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 nName(. sqlite3
1b994 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 0a 20 20 _stmt *pStmt,.
1b995 69 6e 74 20 4e 2c 0a 20 20 63 6f 6e 73 74 20 76 int N,. const v
1b996 6f 69 64 20 2a 28 2a 78 46 75 6e 63 29 28 4d 65 oid *(*xFunc)(Me
1b997 6d 2a 29 2c 0a 20 20 69 6e 74 20 75 73 65 54 79 m*),. int useTy
1b998 70 65 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f pe.){. const vo
1b999 69 64 20 2a 72 65 74 20 3d 20 30 3b 0a 20 20 56 id *ret = 0;. V
1b99a 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a dbe *p = (Vdbe *
1b99b 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 6e 3b )pStmt;. int n;
1b99c 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1b99d 20 70 2d 3e 64 62 3b 0a 20 20 0a 20 20 61 73 73 p->db;. . ass
1b99e 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 ert( db!=0 );.
1b99f 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 n = sqlite3_colu
1b9a0 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b mn_count(pStmt);
1b9a1 0a 20 20 69 66 28 20 4e 3c 6e 20 26 26 20 4e 3e . if( N<n && N>
1b9a2 3d 30 20 29 7b 0a 20 20 20 20 4e 20 2b 3d 20 75 =0 ){. N += u
1b9a3 73 65 54 79 70 65 2a 6e 3b 0a 20 20 20 20 73 71 seType*n;. sq
1b9a4 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
1b9a5 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 r(db->mutex);.
1b9a6 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 assert( db->ma
1b9a7 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 3b llocFailed==0 );
1b9a8 0a 20 20 20 20 72 65 74 20 3d 20 78 46 75 6e 63 . ret = xFunc
1b9a9 28 26 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 4e 5d (&p->aColName[N]
1b9aa 29 3b 0a 20 20 20 20 20 2f 2a 20 41 20 6d 61 6c );. /* A mal
1b9ab 6c 6f 63 20 6d 61 79 20 68 61 76 65 20 66 61 69 loc may have fai
1b9ac 6c 65 64 20 69 6e 73 69 64 65 20 6f 66 20 74 68 led inside of th
1b9ad 65 20 78 46 75 6e 63 28 29 20 63 61 6c 6c 2e 20 e xFunc() call.
1b9ae 49 66 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 If this. ** i
1b9af 73 20 74 68 65 20 63 61 73 65 2c 20 63 6c 65 61 s the case, clea
1b9b0 72 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c r the mallocFail
1b9b1 65 64 20 66 6c 61 67 20 61 6e 64 20 72 65 74 75 ed flag and retu
1b9b2 72 6e 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a rn NULL.. */.
1b9b3 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c if( db->mall
1b9b4 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
1b9b5 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
1b9b6 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 ed = 0;. re
1b9b7 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 t = 0;. }.
1b9b8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1b9b9 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b eave(db->mutex);
1b9ba 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 . }. return re
1b9bb 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 t;.}../*.** Retu
1b9bc 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 rn the name of t
1b9bd 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 he Nth column of
1b9be 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
1b9bf 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 0a returned by SQL.
1b9c0 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 ** statement pSt
1b9c1 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 mt..*/.SQLITE_AP
1b9c2 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 I const char *sq
1b9c3 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d lite3_column_nam
1b9c4 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
1b9c5 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 pStmt, int N){.
1b9c6 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 return columnNa
1b9c7 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c me(. pStmt,
1b9c8 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a N, (const void*
1b9c9 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 (*)(Mem*))sqlite
1b9ca 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 3_value_text, CO
1b9cb 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 LNAME_NAME);.}.#
1b9cc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1b9cd 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f IT_UTF16.SQLITE_
1b9ce 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
1b9cf 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e sqlite3_column_n
1b9d0 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 ame16(sqlite3_st
1b9d1 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e mt *pStmt, int N
1b9d2 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 ){. return colu
1b9d3 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 mnName(. pS
1b9d4 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 tmt, N, (const v
1b9d5 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 oid*(*)(Mem*))sq
1b9d6 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1b9d7 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 16, COLNAME_NAME
1b9d8 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a );.}.#endif../*.
1b9d9 2a 2a 20 43 6f 6e 73 74 72 61 69 6e 74 3a 20 20 ** Constraint:
1b9da 49 66 20 79 6f 75 20 68 61 76 65 20 45 4e 41 42 If you have ENAB
1b9db 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 LE_COLUMN_METADA
1b9dc 54 41 20 74 68 65 6e 20 79 6f 75 20 6d 75 73 74 TA then you must
1b9dd 0a 2a 2a 20 6e 6f 74 20 64 65 66 69 6e 65 20 4f .** not define O
1b9de 4d 49 54 5f 44 45 43 4c 54 59 50 45 2e 0a 2a 2f MIT_DECLTYPE..*/
1b9df 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
1b9e0 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 ITE_OMIT_DECLTYP
1b9e1 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 E) && defined(SQ
1b9e2 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 LITE_ENABLE_COLU
1b9e3 4d 4e 5f 4d 45 54 41 44 41 54 41 29 0a 23 20 65 MN_METADATA).# e
1b9e4 72 72 6f 72 20 22 4d 75 73 74 20 6e 6f 74 20 64 rror "Must not d
1b9e5 65 66 69 6e 65 20 62 6f 74 68 20 53 51 4c 49 54 efine both SQLIT
1b9e6 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 E_OMIT_DECLTYPE
1b9e7 5c 0a 20 20 20 20 20 20 20 20 20 61 6e 64 20 53 \. and S
1b9e8 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c QLITE_ENABLE_COL
1b9e9 55 4d 4e 5f 4d 45 54 41 44 41 54 41 22 0a 23 65 UMN_METADATA".#e
1b9ea 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
1b9eb 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 LITE_OMIT_DECLTY
1b9ec 50 45 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 PE./*.** Return
1b9ed 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 the column decla
1b9ee 72 61 74 69 6f 6e 20 74 79 70 65 20 28 69 66 20 ration type (if
1b9ef 61 70 70 6c 69 63 61 62 6c 65 29 20 6f 66 20 74 applicable) of t
1b9f0 68 65 20 27 69 27 74 68 20 63 6f 6c 75 6d 6e 0a he 'i'th column.
1b9f1 2a 2a 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 ** of the result
1b9f2 20 73 65 74 20 6f 66 20 53 51 4c 20 73 74 61 74 set of SQL stat
1b9f3 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a ement pStmt..*/.
1b9f4 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
1b9f5 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
1b9f6 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73 olumn_decltype(s
1b9f7 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1b9f8 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 mt, int N){. re
1b9f9 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 turn columnName(
1b9fa 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c . pStmt, N,
1b9fb 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 (const void*(*)
1b9fc 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 (Mem*))sqlite3_v
1b9fd 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 alue_text, COLNA
1b9fe 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a 7d 0a ME_DECLTYPE);.}.
1b9ff 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ba00 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 MIT_UTF16.SQLITE
1ba01 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
1ba02 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
1ba03 64 65 63 6c 74 79 70 65 31 36 28 73 71 6c 69 74 decltype16(sqlit
1ba04 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1ba05 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e int N){. return
1ba06 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 columnName(.
1ba07 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f pStmt, N, (co
1ba08 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d nst void*(*)(Mem
1ba09 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 *))sqlite3_value
1ba0a 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 _text16, COLNAME
1ba0b 5f 44 45 43 4c 54 59 50 45 29 3b 0a 7d 0a 23 65 _DECLTYPE);.}.#e
1ba0c 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1ba0d 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e MIT_UTF16 */.#en
1ba0e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1ba0f 49 54 5f 44 45 43 4c 54 59 50 45 20 2a 2f 0a 0a IT_DECLTYPE */..
1ba10 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
1ba11 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 ABLE_COLUMN_META
1ba12 44 41 54 41 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 DATA./*.** Retur
1ba13 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 n the name of th
1ba14 65 20 64 61 74 61 62 61 73 65 20 66 72 6f 6d 20 e database from
1ba15 77 68 69 63 68 20 61 20 72 65 73 75 6c 74 20 63 which a result c
1ba16 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a olumn derives..*
1ba17 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e * NULL is return
1ba18 65 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 ed if the result
1ba19 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 column is an ex
1ba1a 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 pression or cons
1ba1b 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 tant or.** anyth
1ba1c 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68 20 69 ing else which i
1ba1d 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 s not an unabigu
1ba1e 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f ous reference to
1ba1f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6c 75 a database colu
1ba20 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 mn..*/.SQLITE_AP
1ba21 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 I const char *sq
1ba22 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 lite3_column_dat
1ba23 61 62 61 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 abase_name(sqlit
1ba24 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1ba25 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e int N){. return
1ba26 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 columnName(.
1ba27 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f pStmt, N, (co
1ba28 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d nst void*(*)(Mem
1ba29 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 *))sqlite3_value
1ba2a 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 _text, COLNAME_D
1ba2b 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23 69 66 6e ATABASE);.}.#ifn
1ba2c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1ba2d 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 UTF16.SQLITE_API
1ba2e 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
1ba2f 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 ite3_column_data
1ba30 62 61 73 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 base_name16(sqli
1ba31 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1ba32 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 int N){. retur
1ba33 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 n columnName(.
1ba34 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 pStmt, N, (c
1ba35 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 onst void*(*)(Me
1ba36 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 m*))sqlite3_valu
1ba37 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d e_text16, COLNAM
1ba38 45 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23 E_DATABASE);.}.#
1ba39 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1ba3a 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f OMIT_UTF16 */../
1ba3b 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1ba3c 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c name of the tabl
1ba3d 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 72 e from which a r
1ba3e 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 esult column der
1ba3f 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 ives..** NULL is
1ba40 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 returned if the
1ba41 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 result column i
1ba42 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 s an expression
1ba43 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a or constant or.*
1ba44 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 * anything else
1ba45 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 which is not an
1ba46 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66 65 72 unabiguous refer
1ba47 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61 62 61 ence to a databa
1ba48 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 se column..*/.SQ
1ba49 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 LITE_API const c
1ba4a 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c har *sqlite3_col
1ba4b 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 28 73 umn_table_name(s
1ba4c 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1ba4d 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 mt, int N){. re
1ba4e 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 turn columnName(
1ba4f 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c . pStmt, N,
1ba50 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 (const void*(*)
1ba51 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 (Mem*))sqlite3_v
1ba52 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 alue_text, COLNA
1ba53 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 69 66 ME_TABLE);.}.#if
1ba54 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1ba55 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 _UTF16.SQLITE_AP
1ba56 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
1ba57 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 lite3_column_tab
1ba58 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 le_name16(sqlite
1ba59 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
1ba5a 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 nt N){. return
1ba5b 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 columnName(.
1ba5c 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e pStmt, N, (con
1ba5d 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a st void*(*)(Mem*
1ba5e 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ))sqlite3_value_
1ba5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f text16, COLNAME_
1ba60 54 41 42 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 TABLE);.}.#endif
1ba61 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1ba62 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 UTF16 */../*.**
1ba63 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 Return the name
1ba64 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c of the table col
1ba65 75 6d 6e 20 66 72 6f 6d 20 77 68 69 63 68 20 61 umn from which a
1ba66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 result column d
1ba67 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 erives..** NULL
1ba68 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 is returned if t
1ba69 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e he result column
1ba6a 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f is an expressio
1ba6b 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72 n or constant or
1ba6c 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 .** anything els
1ba6d 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61 e which is not a
1ba6e 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66 n unabiguous ref
1ba6f 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61 erence to a data
1ba70 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a base column..*/.
1ba71 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
1ba72 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
1ba73 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d olumn_origin_nam
1ba74 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
1ba75 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 pStmt, int N){.
1ba76 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 return columnNa
1ba77 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c me(. pStmt,
1ba78 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a N, (const void*
1ba79 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 (*)(Mem*))sqlite
1ba7a 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 3_value_text, CO
1ba7b 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d LNAME_COLUMN);.}
1ba7c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1ba7d 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 OMIT_UTF16.SQLIT
1ba7e 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
1ba7f 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
1ba80 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 _origin_name16(s
1ba81 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1ba82 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 mt, int N){. re
1ba83 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 turn columnName(
1ba84 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c . pStmt, N,
1ba85 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 (const void*(*)
1ba86 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 (Mem*))sqlite3_v
1ba87 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c alue_text16, COL
1ba88 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a NAME_COLUMN);.}.
1ba89 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1ba8a 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23 _OMIT_UTF16 */.#
1ba8b 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1ba8c 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 ENABLE_COLUMN_ME
1ba8d 54 41 44 41 54 41 20 2a 2f 0a 0a 0a 2f 2a 2a 2a TADATA */.../***
1ba8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ba8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c ************ sql
1ba90 69 74 65 33 5f 62 69 6e 64 5f 20 20 2a 2a 2a 2a ite3_bind_ ****
1ba91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ba92 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a 20 52 *******.** .** R
1ba93 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20 outines used to
1ba94 61 74 74 61 63 68 20 76 61 6c 75 65 73 20 74 6f attach values to
1ba95 20 77 69 6c 64 63 61 72 64 73 20 69 6e 20 61 20 wildcards in a
1ba96 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 compiled SQL sta
1ba97 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a tement..*/./*.**
1ba98 20 55 6e 62 69 6e 64 20 74 68 65 20 76 61 6c 75 Unbind the valu
1ba99 65 20 62 6f 75 6e 64 20 74 6f 20 76 61 72 69 61 e bound to varia
1ba9a 62 6c 65 20 69 20 69 6e 20 76 69 72 74 75 61 6c ble i in virtual
1ba9b 20 6d 61 63 68 69 6e 65 20 70 2e 20 54 68 69 73 machine p. This
1ba9c 20 69 73 20 74 68 65 20 0a 2a 2a 20 74 68 65 20 is the .** the
1ba9d 73 61 6d 65 20 61 73 20 62 69 6e 64 69 6e 67 20 same as binding
1ba9e 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 74 6f 20 a NULL value to
1ba9f 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 20 74 the column. If t
1baa0 68 65 20 22 69 22 20 70 61 72 61 6d 65 74 65 72 he "i" parameter
1baa1 20 69 73 0a 2a 2a 20 6f 75 74 20 6f 66 20 72 61 is.** out of ra
1baa2 6e 67 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 nge, then SQLITE
1baa3 5f 52 41 4e 47 45 20 69 73 20 72 65 74 75 72 6e _RANGE is return
1baa4 65 64 2e 20 4f 74 68 65 77 69 73 65 20 53 51 4c ed. Othewise SQL
1baa5 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 41 20 ITE_OK..**.** A
1baa6 73 75 63 63 65 73 73 66 75 6c 20 65 76 61 6c 75 successful evalu
1baa7 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f ation of this ro
1baa8 75 74 69 6e 65 20 61 63 71 75 69 72 65 73 20 74 utine acquires t
1baa9 68 65 20 6d 75 74 65 78 20 6f 6e 20 70 2e 0a 2a he mutex on p..*
1baaa 2a 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 72 * the mutex is r
1baab 65 6c 65 61 73 65 64 20 69 66 20 61 6e 79 20 6b eleased if any k
1baac 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 6f 63 63 ind of error occ
1baad 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 urs..**.** The e
1baae 72 72 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 64 rror code stored
1baaf 20 69 6e 20 64 61 74 61 62 61 73 65 20 70 2d 3e in database p->
1bab0 64 62 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 db is overwritte
1bab1 6e 20 77 69 74 68 20 74 68 65 20 72 65 74 75 72 n with the retur
1bab2 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 61 6e n.** value in an
1bab3 79 20 63 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 y case..*/.stati
1bab4 63 20 69 6e 74 20 76 64 62 65 55 6e 62 69 6e 64 c int vdbeUnbind
1bab5 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 29 (Vdbe *p, int i)
1bab6 7b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 {. Mem *pVar;.
1bab7 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
1bab8 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 rn SQLITE_MISUSE
1bab9 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
1baba 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 2d 3e 6d x_enter(p->db->m
1babb 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 2d 3e utex);. if( p->
1babc 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 magic!=VDBE_MAGI
1babd 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 70 63 3e 3d C_RUN || p->pc>=
1babe 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
1babf 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c Error(p->db, SQL
1bac0 49 54 45 5f 4d 49 53 55 53 45 2c 20 30 29 3b 0a ITE_MISUSE, 0);.
1bac1 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1bac2 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d x_leave(p->db->m
1bac3 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74 75 72 utex);. retur
1bac4 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b n SQLITE_MISUSE;
1bac5 0a 20 20 7d 0a 20 20 69 66 28 20 69 3c 31 20 7c . }. if( i<1 |
1bac6 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a 20 | i>p->nVar ){.
1bac7 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 sqlite3Error(
1bac8 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 52 41 p->db, SQLITE_RA
1bac9 4e 47 45 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c NGE, 0);. sql
1baca 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1bacb 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (p->db->mutex);.
1bacc 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1bacd 45 5f 52 41 4e 47 45 3b 0a 20 20 7d 0a 20 20 69 E_RANGE;. }. i
1bace 2d 2d 3b 0a 20 20 70 56 61 72 20 3d 20 26 70 2d --;. pVar = &p-
1bacf 3e 61 56 61 72 5b 69 5d 3b 0a 20 20 73 71 6c 69 >aVar[i];. sqli
1bad0 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
1bad1 65 28 70 56 61 72 29 3b 0a 20 20 70 56 61 72 2d e(pVar);. pVar-
1bad2 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c >flags = MEM_Nul
1bad3 6c 3b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f l;. sqlite3Erro
1bad4 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f r(p->db, SQLITE_
1bad5 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e OK, 0);. return
1bad6 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
1bad7 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 74 65 78 74 *.** Bind a text
1bad8 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75 65 2e 0a or BLOB value..
1bad9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 69 */.static int bi
1bada 6e 64 54 65 78 74 28 0a 20 20 73 71 6c 69 74 65 ndText(. sqlite
1badb 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 20 3_stmt *pStmt,
1badc 20 2f 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e /* The statemen
1badd 74 20 74 6f 20 62 69 6e 64 20 61 67 61 69 6e 73 t to bind agains
1bade 74 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 t */. int i,
1badf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1bae0 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61 Index of the pa
1bae1 72 61 6d 65 74 65 72 20 74 6f 20 62 69 6e 64 20 rameter to bind
1bae2 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
1bae3 2a 7a 44 61 74 61 2c 20 20 20 20 20 2f 2a 20 50 *zData, /* P
1bae4 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 64 61 ointer to the da
1bae5 74 61 20 74 6f 20 62 65 20 62 6f 75 6e 64 20 2a ta to be bound *
1bae6 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 20 /. int nData,
1bae7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1bae8 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
1bae9 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75 6e data to be boun
1baea 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 d */. void (*xD
1baeb 65 6c 29 28 76 6f 69 64 2a 29 2c 20 20 20 2f 2a el)(void*), /*
1baec 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 Destructor for
1baed 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 the data */. u8
1baee 20 65 6e 63 6f 64 69 6e 67 20 20 20 20 20 20 20 encoding
1baef 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 /* Encoding
1baf0 20 66 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f for the data */
1baf1 0a 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 .){. Vdbe *p =
1baf2 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe *)pStmt;.
1baf3 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69 6e Mem *pVar;. in
1baf4 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 76 64 t rc;.. rc = vd
1baf5 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a beUnbind(p, i);.
1baf6 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1baf7 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 7a _OK ){. if( z
1baf8 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20 Data!=0 ){.
1baf9 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 pVar = &p->aVar
1bafa 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 20 72 63 20 [i-1];. rc
1bafb 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d = sqlite3VdbeMem
1bafc 53 65 74 53 74 72 28 70 56 61 72 2c 20 7a 44 61 SetStr(pVar, zDa
1bafd 74 61 2c 20 6e 44 61 74 61 2c 20 65 6e 63 6f 64 ta, nData, encod
1bafe 69 6e 67 2c 20 78 44 65 6c 29 3b 0a 20 20 20 20 ing, xDel);.
1baff 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1bb00 5f 4f 4b 20 26 26 20 65 6e 63 6f 64 69 6e 67 21 _OK && encoding!
1bb01 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 =0 ){. rc
1bb02 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 = sqlite3VdbeCh
1bb03 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61 angeEncoding(pVa
1bb04 72 2c 20 45 4e 43 28 70 2d 3e 64 62 29 29 3b 0a r, ENC(p->db));.
1bb05 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
1bb06 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 lite3Error(p->db
1bb07 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 , rc, 0);.
1bb08 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 rc = sqlite3ApiE
1bb09 78 69 74 28 70 2d 3e 64 62 2c 20 72 63 29 3b 0a xit(p->db, rc);.
1bb0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
1bb0b 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3_mutex_leave(p-
1bb0c 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d >db->mutex);. }
1bb0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1bb0e 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 62 ../*.** Bind a b
1bb0f 6c 6f 62 20 76 61 6c 75 65 20 74 6f 20 61 6e 20 lob value to an
1bb10 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 76 61 SQL statement va
1bb11 72 69 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 riable..*/.SQLIT
1bb12 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1bb13 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 0a 20 20 73 3_bind_blob(. s
1bb14 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1bb15 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c 20 0a 20 mt, . int i, .
1bb16 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61 const void *zDa
1bb17 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44 61 74 61 ta, . int nData
1bb18 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c , . void (*xDel
1bb19 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 72 65 )(void*).){. re
1bb1a 74 75 72 6e 20 62 69 6e 64 54 65 78 74 28 70 53 turn bindText(pS
1bb1b 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 2c 20 6e tmt, i, zData, n
1bb1c 44 61 74 61 2c 20 78 44 65 6c 2c 20 30 29 3b 0a Data, xDel, 0);.
1bb1d 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 }.SQLITE_API int
1bb1e 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f sqlite3_bind_do
1bb1f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d uble(sqlite3_stm
1bb20 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c t *pStmt, int i,
1bb21 20 64 6f 75 62 6c 65 20 72 56 61 6c 75 65 29 7b double rValue){
1bb22 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 . int rc;. Vdb
1bb23 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 e *p = (Vdbe *)p
1bb24 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 Stmt;. rc = vdb
1bb25 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 eUnbind(p, i);.
1bb26 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1bb27 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 OK ){. sqlite
1bb28 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 3VdbeMemSetDoubl
1bb29 65 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c e(&p->aVar[i-1],
1bb2a 20 72 56 61 6c 75 65 29 3b 0a 20 20 20 20 73 71 rValue);. sq
1bb2b 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1bb2c 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b e(p->db->mutex);
1bb2d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1bb2e 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 ;.}.SQLITE_API i
1bb2f 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
1bb30 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 int(sqlite3_stmt
1bb31 20 2a 70 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20 *p, int i, int
1bb32 69 56 61 6c 75 65 29 7b 0a 20 20 72 65 74 75 72 iValue){. retur
1bb33 6e 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 n sqlite3_bind_i
1bb34 6e 74 36 34 28 70 2c 20 69 2c 20 28 69 36 34 29 nt64(p, i, (i64)
1bb35 69 56 61 6c 75 65 29 3b 0a 7d 0a 53 51 4c 49 54 iValue);.}.SQLIT
1bb36 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1bb37 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 71 6c 3_bind_int64(sql
1bb38 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bb39 2c 20 69 6e 74 20 69 2c 20 73 71 6c 69 74 65 5f , int i, sqlite_
1bb3a 69 6e 74 36 34 20 69 56 61 6c 75 65 29 7b 0a 20 int64 iValue){.
1bb3b 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 20 int rc;. Vdbe
1bb3c 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 *p = (Vdbe *)pSt
1bb3d 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55 mt;. rc = vdbeU
1bb3e 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 nbind(p, i);. i
1bb3f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1bb40 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1bb41 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 dbeMemSetInt64(&
1bb42 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 69 56 p->aVar[i-1], iV
1bb43 61 6c 75 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 alue);. sqlit
1bb44 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
1bb45 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 ->db->mutex);.
1bb46 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1bb47 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1bb48 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c sqlite3_bind_nul
1bb49 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a l(sqlite3_stmt *
1bb4a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 pStmt, int i){.
1bb4b 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 20 int rc;. Vdbe
1bb4c 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d *p = (Vdbe*)pStm
1bb4d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e t;. rc = vdbeUn
1bb4e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 bind(p, i);. if
1bb4f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1bb50 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d ){. sqlite3_m
1bb51 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 utex_leave(p->db
1bb52 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 ->mutex);. }.
1bb53 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c return rc;.}.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 62 69 6e 64 5f 74 65 78 74 28 20 0a te3_bind_text( .
1bb56 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a sqlite3_stmt *
1bb57 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c pStmt, . int i,
1bb58 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a . const char *
1bb59 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44 zData, . int nD
1bb5a 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 ata, . void (*x
1bb5b 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 Del)(void*).){.
1bb5c 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74 return bindText
1bb5d 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 (pStmt, i, zData
1bb5e 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 53 , nData, xDel, S
1bb5f 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 23 QLITE_UTF8);.}.#
1bb60 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1bb61 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f IT_UTF16.SQLITE_
1bb62 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1bb63 62 69 6e 64 5f 74 65 78 74 31 36 28 0a 20 20 73 bind_text16(. s
1bb64 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1bb65 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c 20 0a 20 mt, . int i, .
1bb66 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61 const void *zDa
1bb67 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44 61 74 61 ta, . int nData
1bb68 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c , . void (*xDel
1bb69 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 72 65 )(void*).){. re
1bb6a 74 75 72 6e 20 62 69 6e 64 54 65 78 74 28 70 53 turn bindText(pS
1bb6b 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 2c 20 6e tmt, i, zData, n
1bb6c 44 61 74 61 2c 20 78 44 65 6c 2c 20 53 51 4c 49 Data, xDel, SQLI
1bb6d 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b TE_UTF16NATIVE);
1bb6e 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1bb6f 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a ITE_OMIT_UTF16 *
1bb70 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1bb71 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 sqlite3_bind_va
1bb72 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 lue(sqlite3_stmt
1bb73 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 *pStmt, int i,
1bb74 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 61 const sqlite3_va
1bb75 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 lue *pValue){.
1bb76 69 6e 74 20 72 63 3b 0a 20 20 73 77 69 74 63 68 int rc;. switch
1bb77 28 20 70 56 61 6c 75 65 2d 3e 74 79 70 65 20 29 ( pValue->type )
1bb78 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 {. case SQLIT
1bb79 45 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 E_INTEGER: {.
1bb7a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f rc = sqlite3_
1bb7b 62 69 6e 64 5f 69 6e 74 36 34 28 70 53 74 6d 74 bind_int64(pStmt
1bb7c 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e 75 2e 69 , i, pValue->u.i
1bb7d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
1bb7e 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 }. case S
1bb7f 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20 QLITE_FLOAT: {.
1bb80 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1bb81 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 70 53 3_bind_double(pS
1bb82 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e tmt, i, pValue->
1bb83 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b r);. break;
1bb84 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1bb85 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 7b 0a 20 SQLITE_BLOB: {.
1bb86 20 20 20 20 20 69 66 28 20 70 56 61 6c 75 65 2d if( pValue-
1bb87 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 >flags & MEM_Zer
1bb88 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 o ){. rc
1bb89 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a = sqlite3_bind_z
1bb8a 65 72 6f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 69 eroblob(pStmt, i
1bb8b 2c 20 70 56 61 6c 75 65 2d 3e 75 2e 6e 5a 65 72 , pValue->u.nZer
1bb8c 6f 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b o);. }else{
1bb8d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
1bb8e 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 lite3_bind_blob(
1bb8f 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 pStmt, i, pValue
1bb90 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e 2c 53 ->z, pValue->n,S
1bb91 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
1bb92 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1bb93 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1bb94 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58 case SQLITE_TEX
1bb95 54 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 T: {. rc =
1bb96 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 69 bindText(pStmt,i
1bb97 2c 20 20 70 56 61 6c 75 65 2d 3e 7a 2c 20 70 56 , pValue->z, pV
1bb98 61 6c 75 65 2d 3e 6e 2c 20 53 51 4c 49 54 45 5f alue->n, SQLITE_
1bb99 54 52 41 4e 53 49 45 4e 54 2c 0a 20 20 20 20 20 TRANSIENT,.
1bb9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bb9b 20 20 20 20 20 20 20 20 20 70 56 61 6c 75 65 2d pValue-
1bb9c 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 62 72 65 >enc);. bre
1bb9d 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
1bb9e 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 fault: {. r
1bb9f 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 c = sqlite3_bind
1bba0 5f 6e 75 6c 6c 28 70 53 74 6d 74 2c 20 69 29 3b _null(pStmt, i);
1bba1 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1bba2 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1bba3 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 rc;.}.SQLITE_AP
1bba4 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
1bba5 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 nd_zeroblob(sqli
1bba6 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bba7 20 69 6e 74 20 69 2c 20 69 6e 74 20 6e 29 7b 0a int i, int n){.
1bba8 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 int rc;. Vdbe
1bba9 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 *p = (Vdbe *)pS
1bbaa 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 tmt;. rc = vdbe
1bbab 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 Unbind(p, i);.
1bbac 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1bbad 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 K ){. sqlite3
1bbae 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c VdbeMemSetZeroBl
1bbaf 6f 62 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d ob(&p->aVar[i-1]
1bbb0 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , n);. sqlite
1bbb1 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3_mutex_leave(p-
1bbb2 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d >db->mutex);. }
1bbb3 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1bbb4 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
1bbb5 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 69 6c 64 e number of wild
1bbb6 63 61 72 64 73 20 74 68 61 74 20 63 61 6e 20 62 cards that can b
1bbb7 65 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 6f e potentially bo
1bbb8 75 6e 64 20 74 6f 2e 0a 2a 2a 20 54 68 69 73 20 und to..** This
1bbb9 72 6f 75 74 69 6e 65 20 69 73 20 61 64 64 65 64 routine is added
1bbba 20 74 6f 20 73 75 70 70 6f 72 74 20 44 42 44 3a to support DBD:
1bbbb 3a 53 51 4c 69 74 65 2e 20 20 0a 2a 2f 0a 53 51 :SQLite. .*/.SQ
1bbbc 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1bbbd 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
1bbbe 74 65 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 ter_count(sqlite
1bbbf 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 3_stmt *pStmt){.
1bbc0 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 Vdbe *p = (Vdb
1bbc1 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 e*)pStmt;. retu
1bbc2 72 6e 20 70 20 3f 20 70 2d 3e 6e 56 61 72 20 3a rn p ? p->nVar :
1bbc3 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 0;.}../*.** Cre
1bbc4 61 74 65 20 61 20 6d 61 70 70 69 6e 67 20 66 72 ate a mapping fr
1bbc5 6f 6d 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 om variable numb
1bbc6 65 72 73 20 74 6f 20 76 61 72 69 61 62 6c 65 20 ers to variable
1bbc7 6e 61 6d 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 names.** in the
1bbc8 56 64 62 65 2e 61 7a 56 61 72 5b 5d 20 61 72 72 Vdbe.azVar[] arr
1bbc9 61 79 2c 20 69 66 20 73 75 63 68 20 61 20 6d 61 ay, if such a ma
1bbca 70 70 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 61 pping does not a
1bbcb 6c 72 65 61 64 79 0a 2a 2a 20 65 78 69 73 74 2e lready.** exist.
1bbcc 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1bbcd 63 72 65 61 74 65 56 61 72 4d 61 70 28 56 64 62 createVarMap(Vdb
1bbce 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 70 2d e *p){. if( !p-
1bbcf 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 20 20 69 6e >okVar ){. in
1bbd0 74 20 6a 3b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 t j;. Op *pOp
1bbd1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1bbd2 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 2d tex_enter(p->db-
1bbd3 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 2f 2a 20 >mutex);. /*
1bbd4 54 68 65 20 72 61 63 65 20 63 6f 6e 64 69 74 69 The race conditi
1bbd5 6f 6e 20 68 65 72 65 20 69 73 20 68 61 72 6d 6c on here is harml
1bbd6 65 73 73 2e 20 20 49 66 20 74 77 6f 20 74 68 72 ess. If two thr
1bbd7 65 61 64 73 20 63 61 6c 6c 20 74 68 69 73 0a 20 eads call this.
1bbd8 20 20 20 2a 2a 20 72 6f 75 74 69 6e 65 20 6f 6e ** routine on
1bbd9 20 74 68 65 20 73 61 6d 65 20 56 64 62 65 20 61 the same Vdbe a
1bbda 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c t the same time,
1bbdb 20 74 68 65 79 20 62 6f 74 68 20 6d 69 67 68 74 they both might
1bbdc 20 65 6e 64 0a 20 20 20 20 2a 2a 20 75 70 20 69 end. ** up i
1bbdd 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 nitializing the
1bbde 56 64 62 65 2e 61 7a 56 61 72 5b 5d 20 61 72 72 Vdbe.azVar[] arr
1bbdf 61 79 2e 20 20 54 68 61 74 20 69 73 20 61 20 6c ay. That is a l
1bbe0 69 74 74 6c 65 20 65 78 74 72 61 0a 20 20 20 20 ittle extra.
1bbe1 2a 2a 20 77 6f 72 6b 20 62 75 74 20 69 74 20 72 ** work but it r
1bbe2 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 73 61 esults in the sa
1bbe3 6d 65 20 61 6e 73 77 65 72 2e 0a 20 20 20 20 2a me answer.. *
1bbe4 2f 0a 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 /. for(j=0, p
1bbe5 4f 70 3d 70 2d 3e 61 4f 70 3b 20 6a 3c 70 2d 3e Op=p->aOp; j<p->
1bbe6 6e 4f 70 3b 20 6a 2b 2b 2c 20 70 4f 70 2b 2b 29 nOp; j++, pOp++)
1bbe7 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d {. if( pOp-
1bbe8 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 61 72 69 >opcode==OP_Vari
1bbe9 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 able ){.
1bbea 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1bbeb 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 0 && pOp->p1<=p-
1bbec 3e 6e 56 61 72 20 29 3b 0a 20 20 20 20 20 20 20 >nVar );.
1bbed 20 70 2d 3e 61 7a 56 61 72 5b 70 4f 70 2d 3e 70 p->azVar[pOp->p
1bbee 31 2d 31 5d 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 1-1] = pOp->p4.z
1bbef 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1bbf0 20 20 20 20 70 2d 3e 6f 6b 56 61 72 20 3d 20 31 p->okVar = 1
1bbf1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1bbf2 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d tex_leave(p->db-
1bbf3 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a >mutex);. }.}..
1bbf4 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
1bbf5 20 6e 61 6d 65 20 6f 66 20 61 20 77 69 6c 64 63 name of a wildc
1bbf6 61 72 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 ard parameter.
1bbf7 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 Return NULL if t
1bbf8 68 65 20 69 6e 64 65 78 0a 2a 2a 20 69 73 20 6f he index.** is o
1bbf9 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72 20 69 ut of range or i
1bbfa 66 20 74 68 65 20 77 69 6c 64 63 61 72 64 20 69 f the wildcard i
1bbfb 73 20 75 6e 6e 61 6d 65 64 2e 0a 2a 2a 0a 2a 2a s unnamed..**.**
1bbfc 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 The result is a
1bbfd 6c 77 61 79 73 20 55 54 46 2d 38 2e 0a 2a 2f 0a lways UTF-8..*/.
1bbfe 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
1bbff 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 char *sqlite3_b
1bc00 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 ind_parameter_na
1bc01 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 me(sqlite3_stmt
1bc02 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a *pStmt, int i){.
1bc03 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 Vdbe *p = (Vdb
1bc04 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 e*)pStmt;. if(
1bc05 70 3d 3d 30 20 7c 7c 20 69 3c 31 20 7c 7c 20 69 p==0 || i<1 || i
1bc06 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 >p->nVar ){.
1bc07 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
1bc08 63 72 65 61 74 65 56 61 72 4d 61 70 28 70 29 3b createVarMap(p);
1bc09 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 61 7a 56 . return p->azV
1bc0a 61 72 5b 69 2d 31 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a ar[i-1];.}../*.*
1bc0b 2a 20 47 69 76 65 6e 20 61 20 77 69 6c 64 63 61 * Given a wildca
1bc0c 72 64 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d rd parameter nam
1bc0d 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 69 6e e, return the in
1bc0e 64 65 78 20 6f 66 20 74 68 65 20 76 61 72 69 61 dex of the varia
1bc0f 62 6c 65 0a 2a 2a 20 77 69 74 68 20 74 68 61 74 ble.** with that
1bc10 20 6e 61 6d 65 2e 20 20 49 66 20 74 68 65 72 65 name. If there
1bc11 20 69 73 20 6e 6f 20 76 61 72 69 61 62 6c 65 20 is no variable
1bc12 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e with the given n
1bc13 61 6d 65 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 30 ame,.** return 0
1bc14 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1bc15 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
1bc16 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 _parameter_index
1bc17 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1bc18 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 Stmt, const char
1bc19 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56 64 62 65 *zName){. Vdbe
1bc1a 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 *p = (Vdbe*)pSt
1bc1b 6d 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 mt;. int i;. i
1bc1c 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 f( p==0 ){. r
1bc1d 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 63 eturn 0;. }. c
1bc1e 72 65 61 74 65 56 61 72 4d 61 70 28 70 29 3b 20 reateVarMap(p);
1bc1f 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a . if( zName ){.
1bc20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
1bc21 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 ->nVar; i++){.
1bc22 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
1bc23 7a 20 3d 20 70 2d 3e 61 7a 56 61 72 5b 69 5d 3b z = p->azVar[i];
1bc24 0a 20 20 20 20 20 20 69 66 28 20 7a 20 26 26 20 . if( z &&
1bc25 73 74 72 63 6d 70 28 7a 2c 7a 4e 61 6d 65 29 3d strcmp(z,zName)=
1bc26 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 =0 ){. re
1bc27 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 turn i+1;.
1bc28 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 }. }. }. re
1bc29 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
1bc2a 20 54 72 61 6e 73 66 65 72 20 61 6c 6c 20 62 69 Transfer all bi
1bc2b 6e 64 69 6e 67 73 20 66 72 6f 6d 20 74 68 65 20 ndings from the
1bc2c 66 69 72 73 74 20 73 74 61 74 65 6d 65 6e 74 20 first statement
1bc2d 6f 76 65 72 20 74 6f 20 74 68 65 20 73 65 63 6f over to the seco
1bc2e 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nd..*/.SQLITE_PR
1bc2f 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1bc30 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 3TransferBinding
1bc31 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a s(sqlite3_stmt *
1bc32 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c 69 74 pFromStmt, sqlit
1bc33 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74 6d 74 e3_stmt *pToStmt
1bc34 29 7b 0a 20 20 56 64 62 65 20 2a 70 46 72 6f 6d ){. Vdbe *pFrom
1bc35 20 3d 20 28 56 64 62 65 2a 29 70 46 72 6f 6d 53 = (Vdbe*)pFromS
1bc36 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a 70 54 6f tmt;. Vdbe *pTo
1bc37 20 3d 20 28 56 64 62 65 2a 29 70 54 6f 53 74 6d = (Vdbe*)pToStm
1bc38 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 t;. int i;. as
1bc39 73 65 72 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 70 sert( pTo->db==p
1bc3a 46 72 6f 6d 2d 3e 64 62 20 29 3b 0a 20 20 61 73 From->db );. as
1bc3b 73 65 72 74 28 20 70 54 6f 2d 3e 6e 56 61 72 3d sert( pTo->nVar=
1bc3c 3d 70 46 72 6f 6d 2d 3e 6e 56 61 72 20 29 3b 0a =pFrom->nVar );.
1bc3d 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1bc3e 65 6e 74 65 72 28 70 54 6f 2d 3e 64 62 2d 3e 6d enter(pTo->db->m
1bc3f 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 69 3d 30 utex);. for(i=0
1bc40 3b 20 69 3c 70 46 72 6f 6d 2d 3e 6e 56 61 72 3b ; i<pFrom->nVar;
1bc41 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 i++){. sqlit
1bc42 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 70 e3VdbeMemMove(&p
1bc43 54 6f 2d 3e 61 56 61 72 5b 69 5d 2c 20 26 70 46 To->aVar[i], &pF
1bc44 72 6f 6d 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 rom->aVar[i]);.
1bc45 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 }. sqlite3_mut
1bc46 65 78 5f 6c 65 61 76 65 28 70 54 6f 2d 3e 64 62 ex_leave(pTo->db
1bc47 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 ->mutex);. retu
1bc48 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1bc49 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1bc4a 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a OMIT_DEPRECATED.
1bc4b 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 64 /*.** Deprecated
1bc4c 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 external interf
1bc4d 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f 63 ace. Internal/c
1bc4e 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65 0a ore SQLite code.
1bc4f 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 ** should call s
1bc50 71 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 qlite3TransferBi
1bc51 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 49 73 ndings..**.** Is
1bc52 20 69 73 20 6d 69 73 75 73 65 20 74 6f 20 63 61 is misuse to ca
1bc53 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ll this routine
1bc54 77 69 74 68 20 73 74 61 74 65 6d 65 6e 74 73 20 with statements
1bc55 66 72 6f 6d 20 64 69 66 66 65 72 65 6e 74 0a 2a from different.*
1bc56 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * database conne
1bc57 63 74 69 6f 6e 73 2e 20 20 42 75 74 20 61 73 20 ctions. But as
1bc58 74 68 69 73 20 69 73 20 61 20 64 65 70 72 65 63 this is a deprec
1bc59 61 74 65 64 20 69 6e 74 65 72 66 61 63 65 2c 20 ated interface,
1bc5a 77 65 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 we.** will not b
1bc5b 6f 74 68 65 72 20 74 6f 20 63 68 65 63 6b 20 66 other to check f
1bc5c 6f 72 20 74 68 61 74 20 63 6f 6e 64 69 74 69 6f or that conditio
1bc5d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 n..**.** If the
1bc5e 74 77 6f 20 73 74 61 74 65 6d 65 6e 74 73 20 63 two statements c
1bc5f 6f 6e 74 61 69 6e 20 61 20 64 69 66 66 65 72 65 ontain a differe
1bc60 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 6e nt number of bin
1bc61 64 69 6e 67 73 2c 20 74 68 65 6e 0a 2a 2a 20 61 dings, then.** a
1bc62 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 n SQLITE_ERROR i
1bc63 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4e 6f 74 s returned. Not
1bc64 68 69 6e 67 20 65 6c 73 65 20 63 61 6e 20 67 6f hing else can go
1bc65 20 77 72 6f 6e 67 2c 20 73 6f 20 6f 74 68 65 72 wrong, so other
1bc66 77 69 73 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f wise.** SQLITE_O
1bc67 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a K is returned..*
1bc68 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1bc69 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 sqlite3_transfe
1bc6a 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 r_bindings(sqlit
1bc6b 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74 e3_stmt *pFromSt
1bc6c 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 mt, sqlite3_stmt
1bc6d 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56 64 *pToStmt){. Vd
1bc6e 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64 62 be *pFrom = (Vdb
1bc6f 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20 20 e*)pFromStmt;.
1bc70 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64 62 Vdbe *pTo = (Vdb
1bc71 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69 66 e*)pToStmt;. if
1bc72 28 20 70 46 72 6f 6d 2d 3e 6e 56 61 72 21 3d 70 ( pFrom->nVar!=p
1bc73 54 6f 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 To->nVar ){.
1bc74 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
1bc75 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ROR;. }. retur
1bc76 6e 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65 n sqlite3Transfe
1bc77 72 42 69 6e 64 69 6e 67 73 28 70 46 72 6f 6d 53 rBindings(pFromS
1bc78 74 6d 74 2c 20 70 54 6f 53 74 6d 74 29 3b 0a 7d tmt, pToStmt);.}
1bc79 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
1bc7a 65 74 75 72 6e 20 74 68 65 20 73 71 6c 69 74 65 eturn the sqlite
1bc7b 33 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 3* database hand
1bc7c 6c 65 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 le to which the
1bc7d 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
1bc7e 6e 74 20 67 69 76 65 6e 0a 2a 2a 20 69 6e 20 74 nt given.** in t
1bc7f 68 65 20 61 72 67 75 6d 65 6e 74 20 62 65 6c 6f he argument belo
1bc80 6e 67 73 2e 20 20 54 68 69 73 20 69 73 20 74 68 ngs. This is th
1bc81 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
1bc82 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61 73 0a handle that was.
1bc83 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61 72 67 ** the first arg
1bc84 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 73 71 6c ument to the sql
1bc85 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 74 ite3_prepare() t
1bc86 68 61 74 20 77 61 73 20 75 73 65 64 20 74 6f 20 hat was used to
1bc87 63 72 65 61 74 65 0a 2a 2a 20 74 68 65 20 73 74 create.** the st
1bc88 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 66 atement in the f
1bc89 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2f 0a 53 irst place..*/.S
1bc8a 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
1bc8b 33 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 3 *sqlite3_db_ha
1bc8c 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d ndle(sqlite3_stm
1bc8d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 72 65 74 t *pStmt){. ret
1bc8e 75 72 6e 20 70 53 74 6d 74 20 3f 20 28 28 56 64 urn pStmt ? ((Vd
1bc8f 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64 62 20 3a be*)pStmt)->db :
1bc90 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 0;.}../*.** Ret
1bc91 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
1bc92 20 74 68 65 20 6e 65 78 74 20 70 72 65 70 61 72 the next prepar
1bc93 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74 ed statement aft
1bc94 65 72 20 70 53 74 6d 74 20 61 73 73 6f 63 69 61 er pStmt associa
1bc95 74 65 64 0a 2a 2a 20 77 69 74 68 20 64 61 74 61 ted.** with data
1bc96 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1bc97 70 44 62 2e 20 20 49 66 20 70 53 74 6d 74 20 69 pDb. If pStmt i
1bc98 73 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 74 s NULL, return t
1bc99 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 72 65 70 he first.** prep
1bc9a 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 66 ared statement f
1bc9b 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 or the database
1bc9c 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 52 65 74 connection. Ret
1bc9d 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 urn NULL if ther
1bc9e 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d 6f 72 65 e.** are no more
1bc9f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1bca0 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 71 sqlite3_stmt *sq
1bca1 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 lite3_next_stmt(
1bca2 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 71 sqlite3 *pDb, sq
1bca3 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1bca4 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 t){. sqlite3_st
1bca5 6d 74 20 2a 70 4e 65 78 74 3b 0a 20 20 73 71 6c mt *pNext;. sql
1bca6 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
1bca7 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 (pDb->mutex);.
1bca8 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a if( pStmt==0 ){.
1bca9 20 20 20 20 70 4e 65 78 74 20 3d 20 28 73 71 6c pNext = (sql
1bcaa 69 74 65 33 5f 73 74 6d 74 2a 29 70 44 62 2d 3e ite3_stmt*)pDb->
1bcab 70 56 64 62 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a pVdbe;. }else{.
1bcac 20 20 20 20 70 4e 65 78 74 20 3d 20 28 73 71 6c pNext = (sql
1bcad 69 74 65 33 5f 73 74 6d 74 2a 29 28 28 56 64 62 ite3_stmt*)((Vdb
1bcae 65 2a 29 70 53 74 6d 74 29 2d 3e 70 4e 65 78 74 e*)pStmt)->pNext
1bcaf 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
1bcb0 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 44 62 2d mutex_leave(pDb-
1bcb1 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 >mutex);. retur
1bcb2 6e 20 70 4e 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a n pNext;.}../*.*
1bcb3 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c * Return the val
1bcb4 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 63 ue of a status c
1bcb5 6f 75 6e 74 65 72 20 66 6f 72 20 61 20 70 72 65 ounter for a pre
1bcb6 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a pared statement.
1bcb7 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1bcb8 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73 t sqlite3_stmt_s
1bcb9 74 61 74 75 73 28 73 71 6c 69 74 65 33 5f 73 74 tatus(sqlite3_st
1bcba 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 6f mt *pStmt, int o
1bcbb 70 2c 20 69 6e 74 20 72 65 73 65 74 46 6c 61 67 p, int resetFlag
1bcbc 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 ){. Vdbe *pVdbe
1bcbd 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b = (Vdbe*)pStmt;
1bcbe 0a 20 20 69 6e 74 20 76 20 3d 20 70 56 64 62 65 . int v = pVdbe
1bcbf 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d ->aCounter[op-1]
1bcc0 3b 0a 20 20 69 66 28 20 72 65 73 65 74 46 6c 61 ;. if( resetFla
1bcc1 67 20 29 20 70 56 64 62 65 2d 3e 61 43 6f 75 6e g ) pVdbe->aCoun
1bcc2 74 65 72 5b 6f 70 2d 31 5d 20 3d 20 30 3b 0a 20 ter[op-1] = 0;.
1bcc3 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0a 0a 2f 2a return v;.}../*
1bcc4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
1bcc5 64 20 6f 66 20 76 64 62 65 61 70 69 2e 63 20 2a d of vdbeapi.c *
1bcc6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bcc7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bcc8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1bcc9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
1bcca 67 69 6e 20 66 69 6c 65 20 76 64 62 65 2e 63 20 gin file vdbe.c
1bccb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bccc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bccd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1bcce 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 .** 2001 Septemb
1bccf 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 15.**.** The
1bcd0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
1bcd1 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
1bcd2 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
1bcd3 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
1bcd4 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
1bcd5 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
1bcd6 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
1bcd7 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
1bcd8 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
1bcd9 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
1bcda 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
1bcdb 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
1bcdc 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
1bcdd 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
1bcde 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
1bcdf 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
1bce0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
1bce1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bce2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bce3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bce4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bce5 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 ****.** The code
1bce6 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 6d in this file im
1bce7 70 6c 65 6d 65 6e 74 73 20 65 78 65 63 75 74 69 plements executi
1bce8 6f 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 on method of the
1bce9 20 0a 2a 2a 20 56 69 72 74 75 61 6c 20 44 61 74 .** Virtual Dat
1bcea 61 62 61 73 65 20 45 6e 67 69 6e 65 20 28 56 44 abase Engine (VD
1bceb 42 45 29 2e 20 20 41 20 73 65 70 61 72 61 74 65 BE). A separate
1bcec 20 66 69 6c 65 20 28 22 76 64 62 65 61 75 78 2e file ("vdbeaux.
1bced 63 22 29 0a 2a 2a 20 68 61 6e 64 6c 65 73 20 68 c").** handles h
1bcee 6f 75 73 65 6b 65 65 70 69 6e 67 20 64 65 74 61 ousekeeping deta
1bcef 69 6c 73 20 73 75 63 68 20 61 73 20 63 72 65 61 ils such as crea
1bcf0 74 69 6e 67 20 61 6e 64 20 64 65 6c 65 74 69 6e ting and deletin
1bcf1 67 0a 2a 2a 20 56 44 42 45 20 69 6e 73 74 61 6e g.** VDBE instan
1bcf2 63 65 73 2e 20 20 54 68 69 73 20 66 69 6c 65 20 ces. This file
1bcf3 69 73 20 73 6f 6c 65 6c 79 20 69 6e 74 65 72 65 is solely intere
1bcf4 73 74 65 64 20 69 6e 20 65 78 65 63 75 74 69 6e sted in executin
1bcf5 67 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 70 72 g.** the VDBE pr
1bcf6 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 ogram..**.** In
1bcf7 74 68 65 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 the external int
1bcf8 65 72 66 61 63 65 2c 20 61 6e 20 22 73 71 6c 69 erface, an "sqli
1bcf9 74 65 33 5f 73 74 6d 74 2a 22 20 69 73 20 61 6e te3_stmt*" is an
1bcfa 20 6f 70 61 71 75 65 20 70 6f 69 6e 74 65 72 0a opaque pointer.
1bcfb 2a 2a 20 74 6f 20 61 20 56 44 42 45 2e 0a 2a 2a ** to a VDBE..**
1bcfc 0a 2a 2a 20 54 68 65 20 53 51 4c 20 70 61 72 73 .** The SQL pars
1bcfd 65 72 20 67 65 6e 65 72 61 74 65 73 20 61 20 70 er generates a p
1bcfe 72 6f 67 72 61 6d 20 77 68 69 63 68 20 69 73 20 rogram which is
1bcff 74 68 65 6e 20 65 78 65 63 75 74 65 64 20 62 79 then executed by
1bd00 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 74 6f 20 .** the VDBE to
1bd01 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 74 do the work of t
1bd02 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
1bd03 2e 20 20 56 44 42 45 20 70 72 6f 67 72 61 6d 73 . VDBE programs
1bd04 20 61 72 65 20 0a 2a 2a 20 73 69 6d 69 6c 61 72 are .** similar
1bd05 20 69 6e 20 66 6f 72 6d 20 74 6f 20 61 73 73 65 in form to asse
1bd06 6d 62 6c 79 20 6c 61 6e 67 75 61 67 65 2e 20 20 mbly language.
1bd07 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 6e 73 The program cons
1bd08 69 73 74 73 20 6f 66 0a 2a 2a 20 61 20 6c 69 6e ists of.** a lin
1bd09 65 61 72 20 73 65 71 75 65 6e 63 65 20 6f 66 20 ear sequence of
1bd0a 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 45 61 63 operations. Eac
1bd0b 68 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 73 20 h operation has
1bd0c 61 6e 20 6f 70 63 6f 64 65 20 0a 2a 2a 20 61 6e an opcode .** an
1bd0d 64 20 35 20 6f 70 65 72 61 6e 64 73 2e 20 20 4f d 5 operands. O
1bd0e 70 65 72 61 6e 64 73 20 50 31 2c 20 50 32 2c 20 perands P1, P2,
1bd0f 61 6e 64 20 50 33 20 61 72 65 20 69 6e 74 65 67 and P3 are integ
1bd10 65 72 73 2e 20 20 4f 70 65 72 61 6e 64 20 50 34 ers. Operand P4
1bd11 20 0a 2a 2a 20 69 73 20 61 20 6e 75 6c 6c 2d 74 .** is a null-t
1bd12 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 erminated string
1bd13 2e 20 20 4f 70 65 72 61 6e 64 20 50 35 20 69 73 . Operand P5 is
1bd14 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 63 68 61 an unsigned cha
1bd15 72 61 63 74 65 72 2e 0a 2a 2a 20 46 65 77 20 6f racter..** Few o
1bd16 70 63 6f 64 65 73 20 75 73 65 20 61 6c 6c 20 35 pcodes use all 5
1bd17 20 6f 70 65 72 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a operands..**.**
1bd18 20 43 6f 6d 70 75 74 61 74 69 6f 6e 20 72 65 73 Computation res
1bd19 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 ults are stored
1bd1a 6f 6e 20 61 20 73 65 74 20 6f 66 20 72 65 67 69 on a set of regi
1bd1b 73 74 65 72 73 20 6e 75 6d 62 65 72 65 64 20 62 sters numbered b
1bd1c 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74 68 eginning.** with
1bd1d 20 31 20 61 6e 64 20 67 6f 69 6e 67 20 75 70 20 1 and going up
1bd1e 74 6f 20 56 64 62 65 2e 6e 4d 65 6d 2e 20 20 45 to Vdbe.nMem. E
1bd1f 61 63 68 20 72 65 67 69 73 74 65 72 20 63 61 6e ach register can
1bd20 20 73 74 6f 72 65 0a 2a 2a 20 65 69 74 68 65 72 store.** either
1bd21 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 61 20 6e an integer, a n
1bd22 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 ull-terminated s
1bd23 74 72 69 6e 67 2c 20 61 20 66 6c 6f 61 74 69 6e tring, a floatin
1bd24 67 20 70 6f 69 6e 74 0a 2a 2a 20 6e 75 6d 62 65 g point.** numbe
1bd25 72 2c 20 6f 72 20 74 68 65 20 53 51 4c 20 22 4e r, or the SQL "N
1bd26 55 4c 4c 22 20 76 61 6c 75 65 2e 20 20 41 6e 20 ULL" value. An
1bd27 69 6d 70 6c 69 63 69 74 20 63 6f 6e 76 65 72 73 implicit convers
1bd28 69 6f 6e 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 ion from one.**
1bd29 74 79 70 65 20 74 6f 20 74 68 65 20 6f 74 68 65 type to the othe
1bd2a 72 20 6f 63 63 75 72 73 20 61 73 20 6e 65 63 65 r occurs as nece
1bd2b 73 73 61 72 79 2e 0a 2a 2a 20 0a 2a 2a 20 4d 6f ssary..** .** Mo
1bd2c 73 74 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69 st of the code i
1bd2d 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 74 n this file is t
1bd2e 61 6b 65 6e 20 75 70 20 62 79 20 74 68 65 20 73 aken up by the s
1bd2f 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 qlite3VdbeExec()
1bd30 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 68 69 .** function whi
1bd31 63 68 20 64 6f 65 73 20 74 68 65 20 77 6f 72 6b ch does the work
1bd32 20 6f 66 20 69 6e 74 65 72 70 72 65 74 69 6e 67 of interpreting
1bd33 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e a VDBE program.
1bd34 0a 2a 2a 20 42 75 74 20 6f 74 68 65 72 20 72 6f .** But other ro
1bd35 75 74 69 6e 65 73 20 61 72 65 20 61 6c 73 6f 20 utines are also
1bd36 70 72 6f 76 69 64 65 64 20 74 6f 20 68 65 6c 70 provided to help
1bd37 20 69 6e 20 62 75 69 6c 64 69 6e 67 20 75 70 0a in building up.
1bd38 2a 2a 20 61 20 70 72 6f 67 72 61 6d 20 69 6e 73 ** a program ins
1bd39 74 72 75 63 74 69 6f 6e 20 62 79 20 69 6e 73 74 truction by inst
1bd3a 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 56 ruction..**.** V
1bd3b 61 72 69 6f 75 73 20 73 63 72 69 70 74 73 20 73 arious scripts s
1bd3c 63 61 6e 20 74 68 69 73 20 73 6f 75 72 63 65 20 can this source
1bd3d 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 74 6f file in order to
1bd3e 20 67 65 6e 65 72 61 74 65 20 48 54 4d 4c 0a 2a generate HTML.*
1bd3f 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 2c * documentation,
1bd40 20 68 65 61 64 65 72 73 20 66 69 6c 65 73 2c 20 headers files,
1bd41 6f 72 20 6f 74 68 65 72 20 64 65 72 69 76 65 64 or other derived
1bd42 20 66 69 6c 65 73 2e 20 20 54 68 65 20 66 6f 72 files. The for
1bd43 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 matting.** of th
1bd44 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 e code in this f
1bd45 69 6c 65 20 69 73 2c 20 74 68 65 72 65 66 6f 72 ile is, therefor
1bd46 65 2c 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 53 e, important. S
1bd47 65 65 20 6f 74 68 65 72 20 63 6f 6d 6d 65 6e 74 ee other comment
1bd48 73 0a 2a 2a 20 69 6e 20 74 68 69 73 20 66 69 6c s.** in this fil
1bd49 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 20 e for details.
1bd4a 49 66 20 69 6e 20 64 6f 75 62 74 2c 20 64 6f 20 If in doubt, do
1bd4b 6e 6f 74 20 64 65 76 69 61 74 65 20 66 72 6f 6d not deviate from
1bd4c 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 63 6f 6d existing.** com
1bd4d 6d 65 6e 74 69 6e 67 20 61 6e 64 20 69 6e 64 65 menting and inde
1bd4e 6e 74 61 74 69 6f 6e 20 70 72 61 63 74 69 63 65 ntation practice
1bd4f 73 20 77 68 65 6e 20 63 68 61 6e 67 69 6e 67 20 s when changing
1bd50 6f 72 20 61 64 64 69 6e 67 20 63 6f 64 65 2e 0a or adding code..
1bd51 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 2e **.** $Id: vdbe.
1bd52 63 2c 76 20 31 2e 38 37 34 20 32 30 30 39 2f 30 c,v 1.874 2009/0
1bd53 37 2f 32 34 20 31 37 3a 35 38 3a 35 33 20 64 61 7/24 17:58:53 da
1bd54 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a nielk1977 Exp $.
1bd55 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f */../*.** The fo
1bd56 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 llowing global v
1bd57 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 ariable is incre
1bd58 6d 65 6e 74 65 64 20 65 76 65 72 79 20 74 69 6d mented every tim
1bd59 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20 6d 6f e a cursor.** mo
1bd5a 76 65 73 2c 20 65 69 74 68 65 72 20 62 79 20 74 ves, either by t
1bd5b 68 65 20 4f 50 5f 53 65 65 6b 58 58 2c 20 4f 50 he OP_SeekXX, OP
1bd5c 5f 4e 65 78 74 2c 20 6f 72 20 4f 50 5f 50 72 65 _Next, or OP_Pre
1bd5d 76 20 6f 70 63 6f 64 65 73 2e 20 20 54 68 65 20 v opcodes. The
1bd5e 74 65 73 74 0a 2a 2a 20 70 72 6f 63 65 64 75 72 test.** procedur
1bd5f 65 73 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f es use this info
1bd60 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 rmation to make
1bd61 73 75 72 65 20 74 68 61 74 20 69 6e 64 69 63 65 sure that indice
1bd62 73 20 61 72 65 0a 2a 2a 20 77 6f 72 6b 69 6e 67 s are.** working
1bd63 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54 68 69 correctly. Thi
1bd64 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 20 6e s variable has n
1bd65 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 o function other
1bd66 20 74 68 61 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 than to.** help
1bd67 20 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 verify the corr
1bd68 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 ect operation of
1bd69 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f the library..*/
1bd6a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
1bd6b 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
1bd6c 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 nt sqlite3_searc
1bd6d 68 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e h_count = 0;.#en
1bd6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 dif../*.** When
1bd6f 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 this global vari
1bd70 61 62 6c 65 20 69 73 20 70 6f 73 69 74 69 76 65 able is positive
1bd71 2c 20 69 74 20 67 65 74 73 20 64 65 63 72 65 6d , it gets decrem
1bd72 65 6e 74 65 64 20 6f 6e 63 65 20 62 65 66 6f 72 ented once befor
1bd73 65 0a 2a 2a 20 65 61 63 68 20 69 6e 73 74 72 75 e.** each instru
1bd74 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 56 44 42 ction in the VDB
1bd75 45 2e 20 20 57 68 65 6e 20 72 65 61 63 68 65 73 E. When reaches
1bd76 20 7a 65 72 6f 2c 20 74 68 65 20 75 31 2e 69 73 zero, the u1.is
1bd77 49 6e 74 65 72 72 75 70 74 65 64 0a 2a 2a 20 66 Interrupted.** f
1bd78 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 6c 69 ield of the sqli
1bd79 74 65 33 20 73 74 72 75 63 74 75 72 65 20 69 73 te3 structure is
1bd7a 20 73 65 74 20 69 6e 20 6f 72 64 65 72 20 74 6f set in order to
1bd7b 20 73 69 6d 75 6c 61 74 65 20 61 6e 64 20 69 6e simulate and in
1bd7c 74 65 72 72 75 70 74 2e 0a 2a 2a 0a 2a 2a 20 54 terrupt..**.** T
1bd7d 68 69 73 20 66 61 63 69 6c 69 74 79 20 69 73 20 his facility is
1bd7e 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 used for testing
1bd7f 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 purposes only.
1bd80 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 66 75 6e It does not fun
1bd81 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 61 6e 20 6f ction.** in an o
1bd82 72 64 69 6e 61 72 79 20 62 75 69 6c 64 2e 0a 2a rdinary build..*
1bd83 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
1bd84 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 TEST.SQLITE_API
1bd85 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 int sqlite3_inte
1bd86 72 72 75 70 74 5f 63 6f 75 6e 74 20 3d 20 30 3b rrupt_count = 0;
1bd87 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
1bd88 68 65 20 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 he next global v
1bd89 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 ariable is incre
1bd8a 6d 65 6e 74 65 64 20 65 61 63 68 20 74 79 70 65 mented each type
1bd8b 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63 the OP_Sort opc
1bd8c 6f 64 65 0a 2a 2a 20 69 73 20 65 78 65 63 75 74 ode.** is execut
1bd8d 65 64 2e 20 20 54 68 65 20 74 65 73 74 20 70 72 ed. The test pr
1bd8e 6f 63 65 64 75 72 65 73 20 75 73 65 20 74 68 69 ocedures use thi
1bd8f 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f s information to
1bd90 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a make sure that.
1bd91 2a 2a 20 73 6f 72 74 69 6e 67 20 69 73 20 6f 63 ** sorting is oc
1bd92 63 75 72 72 69 6e 67 20 6f 72 20 6e 6f 74 20 6f curring or not o
1bd93 63 63 75 72 72 69 6e 67 20 61 74 20 61 70 70 72 ccurring at appr
1bd94 6f 70 72 69 61 74 65 20 74 69 6d 65 73 2e 20 20 opriate times.
1bd95 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 0a 2a This variable.*
1bd96 2a 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f * has no functio
1bd97 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 20 n other than to
1bd98 68 65 6c 70 20 76 65 72 69 66 79 20 74 68 65 20 help verify the
1bd99 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f correct operatio
1bd9a 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 69 62 72 n of the.** libr
1bd9b 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ary..*/.#ifdef S
1bd9c 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
1bd9d 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1bd9e 33 5f 73 6f 72 74 5f 63 6f 75 6e 74 20 3d 20 30 3_sort_count = 0
1bd9f 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
1bda0 54 68 65 20 6e 65 78 74 20 67 6c 6f 62 61 6c 20 The next global
1bda1 76 61 72 69 61 62 6c 65 20 72 65 63 6f 72 64 73 variable records
1bda2 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
1bda3 20 6c 61 72 67 65 73 74 20 4d 45 4d 5f 42 6c 6f largest MEM_Blo
1bda4 62 0a 2a 2a 20 6f 72 20 4d 45 4d 5f 53 74 72 20 b.** or MEM_Str
1bda5 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 75 73 that has been us
1bda6 65 64 20 62 79 20 61 20 56 44 42 45 20 6f 70 63 ed by a VDBE opc
1bda7 6f 64 65 2e 20 20 54 68 65 20 74 65 73 74 20 70 ode. The test p
1bda8 72 6f 63 65 64 75 72 65 73 0a 2a 2a 20 75 73 65 rocedures.** use
1bda9 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f this informatio
1bdaa 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 n to make sure t
1bdab 68 61 74 20 74 68 65 20 7a 65 72 6f 2d 62 6c 6f hat the zero-blo
1bdac 62 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a b functionality.
1bdad 2a 2a 20 69 73 20 77 6f 72 6b 69 6e 67 20 63 6f ** is working co
1bdae 72 72 65 63 74 6c 79 2e 20 20 20 54 68 69 73 20 rrectly. This
1bdaf 76 61 72 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 variable has no
1bdb0 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 function other t
1bdb1 68 61 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 han to.** help v
1bdb2 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 erify the correc
1bdb3 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 t operation of t
1bdb4 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 he library..*/.#
1bdb5 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
1bdb6 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
1bdb7 20 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f sqlite3_max_blo
1bdb8 62 73 69 7a 65 20 3d 20 30 3b 0a 73 74 61 74 69 bsize = 0;.stati
1bdb9 63 20 76 6f 69 64 20 75 70 64 61 74 65 4d 61 78 c void updateMax
1bdba 42 6c 6f 62 73 69 7a 65 28 4d 65 6d 20 2a 70 29 Blobsize(Mem *p)
1bdbb 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 {. if( (p->flag
1bdbc 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d s & (MEM_Str|MEM
1bdbd 5f 42 6c 6f 62 29 29 21 3d 30 20 26 26 20 70 2d _Blob))!=0 && p-
1bdbe 3e 6e 3e 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62 >n>sqlite3_max_b
1bdbf 6c 6f 62 73 69 7a 65 20 29 7b 0a 20 20 20 20 73 lobsize ){. s
1bdc0 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 qlite3_max_blobs
1bdc1 69 7a 65 20 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 0a ize = p->n;. }.
1bdc2 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1bdc3 54 65 73 74 20 61 20 72 65 67 69 73 74 65 72 20 Test a register
1bdc4 74 6f 20 73 65 65 20 69 66 20 69 74 20 65 78 63 to see if it exc
1bdc5 65 65 64 73 20 74 68 65 20 63 75 72 72 65 6e 74 eeds the current
1bdc6 20 6d 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69 maximum blob si
1bdc7 7a 65 2e 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 ze..** If it doe
1bdc8 73 2c 20 72 65 63 6f 72 64 20 74 68 65 20 6e 65 s, record the ne
1bdc9 77 20 6d 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73 w maximum blob s
1bdca 69 7a 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 ize..*/.#if defi
1bdcb 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 ned(SQLITE_TEST)
1bdcc 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c && !defined(SQL
1bdcd 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e ITE_OMIT_BUILTIN
1bdce 5f 54 45 53 54 29 0a 23 20 64 65 66 69 6e 65 20 _TEST).# define
1bdcf 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 UPDATE_MAX_BLOBS
1bdd0 49 5a 45 28 50 29 20 20 75 70 64 61 74 65 4d 61 IZE(P) updateMa
1bdd1 78 42 6c 6f 62 73 69 7a 65 28 50 29 0a 23 65 6c xBlobsize(P).#el
1bdd2 73 65 0a 23 20 64 65 66 69 6e 65 20 55 50 44 41 se.# define UPDA
1bdd3 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 TE_MAX_BLOBSIZE(
1bdd4 50 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a P).#endif../*.**
1bdd5 20 43 6f 6e 76 65 72 74 20 74 68 65 20 67 69 76 Convert the giv
1bdd6 65 6e 20 72 65 67 69 73 74 65 72 20 69 6e 74 6f en register into
1bdd7 20 61 20 73 74 72 69 6e 67 20 69 66 20 69 74 20 a string if it
1bdd8 69 73 6e 27 74 20 6f 6e 65 0a 2a 2a 20 61 6c 72 isn't one.** alr
1bdd9 65 61 64 79 2e 20 52 65 74 75 72 6e 20 6e 6f 6e eady. Return non
1bdda 2d 7a 65 72 6f 20 69 66 20 61 20 6d 61 6c 6c 6f -zero if a mallo
1bddb 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 23 64 c() fails..*/.#d
1bddc 65 66 69 6e 65 20 53 74 72 69 6e 67 69 66 79 28 efine Stringify(
1bddd 50 2c 20 65 6e 63 29 20 5c 0a 20 20 20 69 66 28 P, enc) \. if(
1bdde 28 28 50 29 2d 3e 66 6c 61 67 73 26 28 4d 45 4d ((P)->flags&(MEM
1bddf 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d _Str|MEM_Blob))=
1bde0 3d 30 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 =0 && sqlite3Vdb
1bde1 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 50 2c eMemStringify(P,
1bde2 65 6e 63 29 29 20 5c 0a 20 20 20 20 20 7b 20 67 enc)) \. { g
1bde3 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 20 7d 0a 0a 2f oto no_mem; }../
1bde4 2a 0a 2a 2a 20 41 6e 20 65 70 68 65 6d 65 72 61 *.** An ephemera
1bde5 6c 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 28 l string value (
1bde6 73 69 67 6e 69 66 69 65 64 20 62 79 20 74 68 65 signified by the
1bde7 20 4d 45 4d 5f 45 70 68 65 6d 20 66 6c 61 67 29 MEM_Ephem flag)
1bde8 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70 contains.** a p
1bde9 6f 69 6e 74 65 72 20 74 6f 20 61 20 64 79 6e 61 ointer to a dyna
1bdea 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 mically allocate
1bdeb 64 20 73 74 72 69 6e 67 20 77 68 65 72 65 20 73 d string where s
1bdec 6f 6d 65 20 6f 74 68 65 72 20 65 6e 74 69 74 79 ome other entity
1bded 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 .** is responsib
1bdee 6c 65 20 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 le for deallocat
1bdef 69 6e 67 20 74 68 61 74 20 73 74 72 69 6e 67 2e ing that string.
1bdf0 20 20 42 65 63 61 75 73 65 20 74 68 65 20 72 65 Because the re
1bdf1 67 69 73 74 65 72 0a 2a 2a 20 64 6f 65 73 20 6e gister.** does n
1bdf2 6f 74 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 73 ot control the s
1bdf3 74 72 69 6e 67 2c 20 69 74 20 6d 69 67 68 74 20 tring, it might
1bdf4 62 65 20 64 65 6c 65 74 65 64 20 77 69 74 68 6f be deleted witho
1bdf5 75 74 20 74 68 65 20 72 65 67 69 73 74 65 72 0a ut the register.
1bdf6 2a 2a 20 6b 6e 6f 77 69 6e 67 20 69 74 2e 0a 2a ** knowing it..*
1bdf7 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1bdf8 65 20 63 6f 6e 76 65 72 74 73 20 61 6e 20 65 70 e converts an ep
1bdf9 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67 20 69 hemeral string i
1bdfa 6e 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c nto a dynamicall
1bdfb 79 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 73 y allocated.** s
1bdfc 74 72 69 6e 67 20 74 68 61 74 20 74 68 65 20 72 tring that the r
1bdfd 65 67 69 73 74 65 72 20 69 74 73 65 6c 66 20 63 egister itself c
1bdfe 6f 6e 74 72 6f 6c 73 2e 20 20 49 6e 20 6f 74 68 ontrols. In oth
1bdff 65 72 20 77 6f 72 64 73 2c 20 69 74 0a 2a 2a 20 er words, it.**
1be00 63 6f 6e 76 65 72 74 73 20 61 6e 20 4d 45 4d 5f converts an MEM_
1be01 45 70 68 65 6d 20 73 74 72 69 6e 67 20 69 6e 74 Ephem string int
1be02 6f 20 61 6e 20 4d 45 4d 5f 44 79 6e 20 73 74 72 o an MEM_Dyn str
1be03 69 6e 67 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ing..*/.#define
1be04 44 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 28 50 Deephemeralize(P
1be05 29 20 5c 0a 20 20 20 69 66 28 20 28 28 50 29 2d ) \. if( ((P)-
1be06 3e 66 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d >flags&MEM_Ephem
1be07 29 21 3d 30 20 5c 0a 20 20 20 20 20 20 20 26 26 )!=0 \. &&
1be08 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d sqlite3VdbeMemM
1be09 61 6b 65 57 72 69 74 65 61 62 6c 65 28 50 29 20 akeWriteable(P)
1be0a 29 7b 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d ){ goto no_mem;}
1be0b 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c ../*.** Call sql
1be0c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e ite3VdbeMemExpan
1be0d 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 dBlob() on the s
1be0e 75 70 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74 upplied value (t
1be0f 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 ype Mem*).** P i
1be10 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 f required..*/.#
1be11 64 65 66 69 6e 65 20 45 78 70 61 6e 64 42 6c 6f define ExpandBlo
1be12 62 28 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 b(P) (((P)->flag
1be13 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 s&MEM_Zero)?sqli
1be14 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 te3VdbeMemExpand
1be15 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a Blob(P):0)../*.*
1be16 2a 20 41 72 67 75 6d 65 6e 74 20 70 4d 65 6d 20 * Argument pMem
1be17 70 6f 69 6e 74 73 20 61 74 20 61 20 72 65 67 69 points at a regi
1be18 73 74 65 72 20 74 68 61 74 20 77 69 6c 6c 20 62 ster that will b
1be19 65 20 70 61 73 73 65 64 20 74 6f 20 61 0a 2a 2a e passed to a.**
1be1a 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 user-defined fu
1be1b 6e 63 74 69 6f 6e 20 6f 72 20 72 65 74 75 72 6e nction or return
1be1c 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 20 61 ed to the user a
1be1d 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 s the result of
1be1e 61 20 71 75 65 72 79 2e 0a 2a 2a 20 54 68 65 20 a query..** The
1be1f 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c second argument,
1be20 20 27 64 62 5f 65 6e 63 27 20 69 73 20 74 68 65 'db_enc' is the
1be21 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 text encoding u
1be22 73 65 64 20 62 79 20 74 68 65 20 76 64 62 65 20 sed by the vdbe
1be23 66 6f 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 for.** register
1be24 76 61 72 69 61 62 6c 65 73 2e 20 20 54 68 69 73 variables. This
1be25 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 routine sets th
1be26 65 20 70 4d 65 6d 2d 3e 65 6e 63 20 61 6e 64 20 e pMem->enc and
1be27 70 4d 65 6d 2d 3e 74 79 70 65 0a 2a 2a 20 76 61 pMem->type.** va
1be28 72 69 61 62 6c 65 73 20 75 73 65 64 20 62 79 20 riables used by
1be29 74 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 the sqlite3_valu
1be2a 65 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a e_*() routines..
1be2b 2a 2f 0a 23 64 65 66 69 6e 65 20 73 74 6f 72 65 */.#define store
1be2c 54 79 70 65 49 6e 66 6f 28 41 2c 42 29 20 5f 73 TypeInfo(A,B) _s
1be2d 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 41 29 0a toreTypeInfo(A).
1be2e 73 74 61 74 69 63 20 76 6f 69 64 20 5f 73 74 6f static void _sto
1be2f 72 65 54 79 70 65 49 6e 66 6f 28 4d 65 6d 20 2a reTypeInfo(Mem *
1be30 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 pMem){. int fla
1be31 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 gs = pMem->flags
1be32 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 ;. if( flags &
1be33 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 MEM_Null ){.
1be34 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1be35 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 ITE_NULL;. }.
1be36 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 else if( flags &
1be37 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 MEM_Int ){.
1be38 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1be39 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 7d ITE_INTEGER;. }
1be3a 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 . else if( flag
1be3b 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a s & MEM_Real ){.
1be3c 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d pMem->type =
1be3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 SQLITE_FLOAT;.
1be3e 20 7d 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c }. else if( fl
1be3f 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b ags & MEM_Str ){
1be40 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 . pMem->type
1be41 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 = SQLITE_TEXT;.
1be42 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d }else{. pMem
1be43 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
1be44 42 4c 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a BLOB;. }.}../*.
1be45 2a 2a 20 50 72 6f 70 65 72 74 69 65 73 20 6f 66 ** Properties of
1be46 20 6f 70 63 6f 64 65 73 2e 20 20 54 68 65 20 4f opcodes. The O
1be47 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 PFLG_INITIALIZER
1be48 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 63 72 65 macro is.** cre
1be49 61 74 65 64 20 62 79 20 6d 6b 6f 70 63 6f 64 65 ated by mkopcode
1be4a 68 2e 61 77 6b 20 64 75 72 69 6e 67 20 63 6f 6d h.awk during com
1be4b 70 69 6c 61 74 69 6f 6e 2e 20 20 44 61 74 61 20 pilation. Data
1be4c 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 is obtained.** f
1be4d 72 6f 6d 20 74 68 65 20 63 6f 6d 6d 65 6e 74 73 rom the comments
1be4e 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 following the "
1be4f 63 61 73 65 20 4f 50 5f 78 78 78 78 3a 22 20 73 case OP_xxxx:" s
1be50 74 61 74 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 tatements in.**
1be51 74 68 69 73 20 66 69 6c 65 2e 20 20 0a 2a 2f 0a this file. .*/.
1be52 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 static const uns
1be53 69 67 6e 65 64 20 63 68 61 72 20 6f 70 63 6f 64 igned char opcod
1be54 65 50 72 6f 70 65 72 74 79 5b 5d 20 3d 20 4f 50 eProperty[] = OP
1be55 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 3b FLG_INITIALIZER;
1be56 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1be57 72 75 65 20 69 66 20 61 6e 20 6f 70 63 6f 64 65 rue if an opcode
1be58 20 68 61 73 20 61 6e 79 20 6f 66 20 74 68 65 20 has any of the
1be59 4f 50 46 4c 47 5f 78 78 78 20 70 72 6f 70 65 72 OPFLG_xxx proper
1be5a 74 69 65 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 ties.** specifie
1be5b 64 20 62 79 20 6d 61 73 6b 2e 0a 2a 2f 0a 53 51 d by mask..*/.SQ
1be5c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1be5d 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f sqlite3VdbeOpco
1be5e 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 69 6e deHasProperty(in
1be5f 74 20 6f 70 63 6f 64 65 2c 20 69 6e 74 20 6d 61 t opcode, int ma
1be60 73 6b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 6f sk){. assert( o
1be61 70 63 6f 64 65 3e 30 20 26 26 20 6f 70 63 6f 64 pcode>0 && opcod
1be62 65 3c 28 69 6e 74 29 73 69 7a 65 6f 66 28 6f 70 e<(int)sizeof(op
1be63 63 6f 64 65 50 72 6f 70 65 72 74 79 29 20 29 3b codeProperty) );
1be64 0a 20 20 72 65 74 75 72 6e 20 28 6f 70 63 6f 64 . return (opcod
1be65 65 50 72 6f 70 65 72 74 79 5b 6f 70 63 6f 64 65 eProperty[opcode
1be66 5d 26 6d 61 73 6b 29 21 3d 30 3b 0a 7d 0a 0a 2f ]&mask)!=0;.}../
1be67 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 56 64 *.** Allocate Vd
1be68 62 65 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 beCursor number
1be69 69 43 75 72 2e 20 20 52 65 74 75 72 6e 20 61 20 iCur. Return a
1be6a 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 pointer to it.
1be6b 52 65 74 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 Return NULL.** i
1be6c 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 f we run out of
1be6d 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 memory..*/.stati
1be6e 63 20 56 64 62 65 43 75 72 73 6f 72 20 2a 61 6c c VdbeCursor *al
1be6f 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 0a 20 20 locateCursor(.
1be70 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 Vdbe *p,
1be71 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 /* The vir
1be72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a tual machine */.
1be73 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20 int iCur,
1be74 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
1be75 20 6f 66 20 74 68 65 20 6e 65 77 20 56 64 62 65 of the new Vdbe
1be76 43 75 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 Cursor */. int
1be77 6e 46 69 65 6c 64 2c 20 20 20 20 20 20 20 20 20 nField,
1be78 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 /* Number of f
1be79 69 65 6c 64 73 20 69 6e 20 74 68 65 20 74 61 62 ields in the tab
1be7a 6c 65 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 le or index */.
1be7b 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 int iDb,
1be7c 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 64 /* When d
1be7d 61 74 61 62 61 73 65 20 74 68 65 20 63 75 72 73 atabase the curs
1be7e 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 6f or belongs to, o
1be7f 72 20 2d 31 20 2a 2f 0a 20 20 69 6e 74 20 69 73 r -1 */. int is
1be80 42 74 72 65 65 43 75 72 73 6f 72 20 20 20 20 20 BtreeCursor
1be81 2f 2a 20 54 72 75 65 20 66 6f 72 20 42 2d 54 72 /* True for B-Tr
1be82 65 65 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 70 ee. False for p
1be83 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f 72 20 76 seudo-table or v
1be84 74 61 62 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 46 tab */.){. /* F
1be85 69 6e 64 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 ind the memory c
1be86 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c 20 62 65 ell that will be
1be87 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 used to store t
1be88 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 he blob of memor
1be89 79 0a 20 20 2a 2a 20 72 65 71 75 69 72 65 64 20 y. ** required
1be8a 66 6f 72 20 74 68 69 73 20 56 64 62 65 43 75 72 for this VdbeCur
1be8b 73 6f 72 20 73 74 72 75 63 74 75 72 65 2e 20 49 sor structure. I
1be8c 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 t is convenient
1be8d 74 6f 20 75 73 65 20 61 20 0a 20 20 2a 2a 20 76 to use a . ** v
1be8e 64 62 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 dbe memory cell
1be8f 74 6f 20 6d 61 6e 61 67 65 20 74 68 65 20 6d 65 to manage the me
1be90 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
1be91 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 0a 20 required for a.
1be92 20 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 20 73 ** VdbeCursor s
1be93 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 tructure for the
1be94 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 61 73 6f following reaso
1be95 6e 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 ns:. **. **
1be96 2a 20 53 6f 6d 65 74 69 6d 65 73 20 63 75 72 73 * Sometimes curs
1be97 6f 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 75 or numbers are u
1be98 73 65 64 20 66 6f 72 20 61 20 63 6f 75 70 6c 65 sed for a couple
1be99 20 6f 66 20 64 69 66 66 65 72 65 6e 74 0a 20 20 of different.
1be9a 2a 2a 20 20 20 20 20 70 75 72 70 6f 73 65 73 20 ** purposes
1be9b 69 6e 20 61 20 76 64 62 65 20 70 72 6f 67 72 61 in a vdbe progra
1be9c 6d 2e 20 54 68 65 20 64 69 66 66 65 72 65 6e 74 m. The different
1be9d 20 75 73 65 73 20 6d 69 67 68 74 20 72 65 71 75 uses might requ
1be9e 69 72 65 0a 20 20 2a 2a 20 20 20 20 20 64 69 66 ire. ** dif
1be9f 66 65 72 65 6e 74 20 73 69 7a 65 64 20 61 6c 6c ferent sized all
1bea0 6f 63 61 74 69 6f 6e 73 2e 20 4d 65 6d 6f 72 79 ocations. Memory
1bea1 20 63 65 6c 6c 73 20 70 72 6f 76 69 64 65 20 67 cells provide g
1bea2 72 6f 77 61 62 6c 65 0a 20 20 2a 2a 20 20 20 20 rowable. **
1bea3 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 allocations..
1bea4 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e **. ** * When
1bea5 20 75 73 69 6e 67 20 45 4e 41 42 4c 45 5f 4d 45 using ENABLE_ME
1bea6 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 2c MORY_MANAGEMENT,
1bea7 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 62 75 66 memory cell buf
1bea8 66 65 72 73 20 63 61 6e 0a 20 20 2a 2a 20 20 20 fers can. **
1bea9 20 20 62 65 20 66 72 65 65 64 20 6c 61 7a 69 6c be freed lazil
1beaa 79 20 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 y via the sqlite
1beab 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 3_release_memory
1beac 28 29 20 41 50 49 2e 20 54 68 69 73 0a 20 20 2a () API. This. *
1bead 2a 20 20 20 20 20 6d 69 6e 69 6d 69 7a 65 73 20 * minimizes
1beae 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 61 the number of ma
1beaf 6c 6c 6f 63 20 63 61 6c 6c 73 20 6d 61 64 65 20 lloc calls made
1beb0 62 79 20 74 68 65 20 73 79 73 74 65 6d 2e 0a 20 by the system..
1beb1 20 2a 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 **. ** Memory
1beb2 63 65 6c 6c 73 20 66 6f 72 20 63 75 72 73 6f 72 cells for cursor
1beb3 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 s are allocated
1beb4 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 at the top of th
1beb5 65 20 61 64 64 72 65 73 73 0a 20 20 2a 2a 20 73 e address. ** s
1beb6 70 61 63 65 2e 20 4d 65 6d 6f 72 79 20 63 65 6c pace. Memory cel
1beb7 6c 20 28 70 2d 3e 6e 4d 65 6d 29 20 63 6f 72 72 l (p->nMem) corr
1beb8 65 73 70 6f 6e 64 73 20 74 6f 20 63 75 72 73 6f esponds to curso
1beb9 72 20 30 2e 20 53 70 61 63 65 20 66 6f 72 0a 20 r 0. Space for.
1beba 20 2a 2a 20 63 75 72 73 6f 72 20 31 20 69 73 20 ** cursor 1 is
1bebb 6d 61 6e 61 67 65 64 20 62 79 20 6d 65 6d 6f 72 managed by memor
1bebc 79 20 63 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 2d y cell (p->nMem-
1bebd 31 29 2c 20 65 74 63 2e 0a 20 20 2a 2f 0a 20 20 1), etc.. */.
1bebe 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e Mem *pMem = &p->
1bebf 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2d 69 43 75 aMem[p->nMem-iCu
1bec0 72 5d 3b 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65 r];.. int nByte
1bec1 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a ;. VdbeCursor *
1bec2 70 43 78 20 3d 20 30 3b 0a 20 20 6e 42 79 74 65 pCx = 0;. nByte
1bec3 20 3d 20 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 = . sizeof
1bec4 28 56 64 62 65 43 75 72 73 6f 72 29 20 2b 20 0a (VdbeCursor) + .
1bec5 20 20 20 20 20 20 28 69 73 42 74 72 65 65 43 75 (isBtreeCu
1bec6 72 73 6f 72 3f 73 71 6c 69 74 65 33 42 74 72 65 rsor?sqlite3Btre
1bec7 65 43 75 72 73 6f 72 53 69 7a 65 28 29 3a 30 29 eCursorSize():0)
1bec8 20 2b 20 0a 20 20 20 20 20 20 32 2a 6e 46 69 65 + . 2*nFie
1bec9 6c 64 2a 73 69 7a 65 6f 66 28 75 33 32 29 3b 0a ld*sizeof(u32);.
1beca 0a 20 20 61 73 73 65 72 74 28 20 69 43 75 72 3c . assert( iCur<
1becb 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1becc 69 66 28 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 if( p->apCsr[iCu
1becd 72 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 r] ){. sqlite
1bece 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 3VdbeFreeCursor(
1becf 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 p, p->apCsr[iCur
1bed0 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 ]);. p->apCsr
1bed1 5b 69 43 75 72 5d 20 3d 20 30 3b 0a 20 20 7d 0a [iCur] = 0;. }.
1bed2 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d if( SQLITE_OK=
1bed3 3d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 =sqlite3VdbeMemG
1bed4 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c row(pMem, nByte,
1bed5 20 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e 61 70 0) ){. p->ap
1bed6 43 73 72 5b 69 43 75 72 5d 20 3d 20 70 43 78 20 Csr[iCur] = pCx
1bed7 3d 20 28 56 64 62 65 43 75 72 73 6f 72 2a 29 70 = (VdbeCursor*)p
1bed8 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 6d 65 6d 73 Mem->z;. mems
1bed9 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e et(pMem->z, 0, n
1beda 42 79 74 65 29 3b 0a 20 20 20 20 70 43 78 2d 3e Byte);. pCx->
1bedb 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 iDb = iDb;. p
1bedc 43 78 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 46 69 Cx->nField = nFi
1bedd 65 6c 64 3b 0a 20 20 20 20 69 66 28 20 6e 46 69 eld;. if( nFi
1bede 65 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 43 78 eld ){. pCx
1bedf 2d 3e 61 54 79 70 65 20 3d 20 28 75 33 32 20 2a ->aType = (u32 *
1bee0 29 26 70 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 )&pMem->z[sizeof
1bee1 28 56 64 62 65 43 75 72 73 6f 72 29 5d 3b 0a 20 (VdbeCursor)];.
1bee2 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 42 }. if( isB
1bee3 74 72 65 65 43 75 72 73 6f 72 20 29 7b 0a 20 20 treeCursor ){.
1bee4 20 20 20 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 pCx->pCursor
1bee5 20 3d 20 28 42 74 43 75 72 73 6f 72 2a 29 0a 20 = (BtCursor*).
1bee6 20 20 20 20 20 20 20 20 20 26 70 4d 65 6d 2d 3e &pMem->
1bee7 7a 5b 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 z[sizeof(VdbeCur
1bee8 73 6f 72 29 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 sor)+2*nField*si
1bee9 7a 65 6f 66 28 75 33 32 29 5d 3b 0a 20 20 20 20 zeof(u32)];.
1beea 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 }. }. return p
1beeb 43 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 Cx;.}../*.** Try
1beec 20 74 6f 20 63 6f 6e 76 65 72 74 20 61 20 76 61 to convert a va
1beed 6c 75 65 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 lue into a numer
1beee 69 63 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ic representatio
1beef 6e 20 69 66 20 77 65 20 63 61 6e 0a 2a 2a 20 64 n if we can.** d
1bef0 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 6c 6f 73 o so without los
1bef1 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e s of information
1bef2 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 . In other word
1bef3 73 2c 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 s, if the string
1bef4 0a 2a 2a 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 .** looks like a
1bef5 20 6e 75 6d 62 65 72 2c 20 63 6f 6e 76 65 72 74 number, convert
1bef6 20 69 74 20 69 6e 74 6f 20 61 20 6e 75 6d 62 65 it into a numbe
1bef7 72 2e 20 20 49 66 20 69 74 20 64 6f 65 73 20 6e r. If it does n
1bef8 6f 74 0a 2a 2a 20 6c 6f 6f 6b 20 6c 69 6b 65 20 ot.** look like
1bef9 61 20 6e 75 6d 62 65 72 2c 20 6c 65 61 76 65 20 a number, leave
1befa 69 74 20 61 6c 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 it alone..*/.sta
1befb 74 69 63 20 76 6f 69 64 20 61 70 70 6c 79 4e 75 tic void applyNu
1befc 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 4d 65 mericAffinity(Me
1befd 6d 20 2a 70 52 65 63 29 7b 0a 20 20 69 66 28 20 m *pRec){. if(
1befe 28 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 28 (pRec->flags & (
1beff 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 MEM_Real|MEM_Int
1bf00 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 ))==0 ){. int
1bf01 20 72 65 61 6c 6e 75 6d 3b 0a 20 20 20 20 73 71 realnum;. sq
1bf02 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 lite3VdbeMemNulT
1bf03 65 72 6d 69 6e 61 74 65 28 70 52 65 63 29 3b 0a erminate(pRec);.
1bf04 20 20 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66 if( (pRec->f
1bf05 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 0a 20 20 lags&MEM_Str).
1bf06 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 && sqlite
1bf07 33 49 73 4e 75 6d 62 65 72 28 70 52 65 63 2d 3e 3IsNumber(pRec->
1bf08 7a 2c 20 26 72 65 61 6c 6e 75 6d 2c 20 70 52 65 z, &realnum, pRe
1bf09 63 2d 3e 65 6e 63 29 20 29 7b 0a 20 20 20 20 20 c->enc) ){.
1bf0a 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 i64 value;.
1bf0b 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1bf0c 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 52 65 63 ngeEncoding(pRec
1bf0d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a , SQLITE_UTF8);.
1bf0e 20 20 20 20 20 20 69 66 28 20 21 72 65 61 6c 6e if( !realn
1bf0f 75 6d 20 26 26 20 73 71 6c 69 74 65 33 41 74 6f um && sqlite3Ato
1bf10 69 36 34 28 70 52 65 63 2d 3e 7a 2c 20 26 76 61 i64(pRec->z, &va
1bf11 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 lue) ){.
1bf12 70 52 65 63 2d 3e 75 2e 69 20 3d 20 76 61 6c 75 pRec->u.i = valu
1bf13 65 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 53 65 e;. MemSe
1bf14 74 54 79 70 65 46 6c 61 67 28 70 52 65 63 2c 20 tTypeFlag(pRec,
1bf15 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20 MEM_Int);.
1bf16 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 }else{. s
1bf17 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 qlite3VdbeMemRea
1bf18 6c 69 66 79 28 70 52 65 63 29 3b 0a 20 20 20 20 lify(pRec);.
1bf19 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a }. }. }.}.
1bf1a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 69 6e ./*.** Processin
1bf1b 67 20 69 73 20 64 65 74 65 72 6d 69 6e 65 20 62 g is determine b
1bf1c 79 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 70 y the affinity p
1bf1d 61 72 61 6d 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 arameter:.**.**
1bf1e 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 SQLITE_AFF_INTEG
1bf1f 45 52 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 ER:.** SQLITE_AF
1bf20 46 5f 52 45 41 4c 3a 0a 2a 2a 20 53 51 4c 49 54 F_REAL:.** SQLIT
1bf21 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3a 0a 2a E_AFF_NUMERIC:.*
1bf22 2a 20 20 20 20 54 72 79 20 74 6f 20 63 6f 6e 76 * Try to conv
1bf23 65 72 74 20 70 52 65 63 20 74 6f 20 61 6e 20 69 ert pRec to an i
1bf24 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 nteger represent
1bf25 61 74 69 6f 6e 20 6f 72 20 61 20 0a 2a 2a 20 20 ation or a .**
1bf26 20 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 floating-point
1bf27 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
1bf28 69 66 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 if an integer re
1bf29 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 presentation.**
1bf2a 20 20 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 is not possib
1bf2b 6c 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 le. Note that t
1bf2c 68 65 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 he integer repre
1bf2d 73 65 6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 sentation is.**
1bf2e 20 20 20 61 6c 77 61 79 73 20 70 72 65 66 65 72 always prefer
1bf2f 72 65 64 2c 20 65 76 65 6e 20 69 66 20 74 68 65 red, even if the
1bf30 20 61 66 66 69 6e 69 74 79 20 69 73 20 52 45 41 affinity is REA
1bf31 4c 2c 20 62 65 63 61 75 73 65 0a 2a 2a 20 20 20 L, because.**
1bf32 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 an integer repr
1bf33 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 20 6d 6f esentation is mo
1bf34 72 65 20 73 70 61 63 65 20 65 66 66 69 63 69 65 re space efficie
1bf35 6e 74 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 0a 2a nt on disk..**.*
1bf36 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 * SQLITE_AFF_TEX
1bf37 54 3a 0a 2a 2a 20 20 20 20 43 6f 6e 76 65 72 74 T:.** Convert
1bf38 20 70 52 65 63 20 74 6f 20 61 20 74 65 78 74 20 pRec to a text
1bf39 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a representation..
1bf3a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 **.** SQLITE_AFF
1bf3b 5f 4e 4f 4e 45 3a 0a 2a 2a 20 20 20 20 4e 6f 2d _NONE:.** No-
1bf3c 6f 70 2e 20 20 70 52 65 63 20 69 73 20 75 6e 63 op. pRec is unc
1bf3d 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 hanged..*/.stati
1bf3e 63 20 76 6f 69 64 20 61 70 70 6c 79 41 66 66 69 c void applyAffi
1bf3f 6e 69 74 79 28 0a 20 20 4d 65 6d 20 2a 70 52 65 nity(. Mem *pRe
1bf40 63 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 c, /* T
1bf41 68 65 20 76 61 6c 75 65 20 74 6f 20 61 70 70 6c he value to appl
1bf42 79 20 61 66 66 69 6e 69 74 79 20 74 6f 20 2a 2f y affinity to */
1bf43 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 . char affinity
1bf44 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 , /* The af
1bf45 66 69 6e 69 74 79 20 74 6f 20 62 65 20 61 70 70 finity to be app
1bf46 6c 69 65 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63 lied */. u8 enc
1bf47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1bf48 20 55 73 65 20 74 68 69 73 20 74 65 78 74 20 65 Use this text e
1bf49 6e 63 6f 64 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 ncoding */.){.
1bf4a 69 66 28 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 if( affinity==SQ
1bf4b 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 29 7b LITE_AFF_TEXT ){
1bf4c 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 74 74 . /* Only att
1bf4d 65 6d 70 74 20 74 68 65 20 63 6f 6e 76 65 72 73 empt the convers
1bf4e 69 6f 6e 20 74 6f 20 54 45 58 54 20 69 66 20 74 ion to TEXT if t
1bf4f 68 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65 67 here is an integ
1bf50 65 72 20 6f 72 20 72 65 61 6c 0a 20 20 20 20 2a er or real. *
1bf51 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e * representation
1bf52 20 28 62 6c 6f 62 20 61 6e 64 20 4e 55 4c 4c 20 (blob and NULL
1bf53 64 6f 20 6e 6f 74 20 67 65 74 20 63 6f 6e 76 65 do not get conve
1bf54 72 74 65 64 29 20 62 75 74 20 6e 6f 20 73 74 72 rted) but no str
1bf55 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65 70 72 65 ing. ** repre
1bf56 73 65 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a sentation.. *
1bf57 2f 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 70 52 /. if( 0==(pR
1bf58 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 ec->flags&MEM_St
1bf59 72 29 20 26 26 20 28 70 52 65 63 2d 3e 66 6c 61 r) && (pRec->fla
1bf5a 67 73 26 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d gs&(MEM_Real|MEM
1bf5b 5f 49 6e 74 29 29 20 29 7b 0a 20 20 20 20 20 20 _Int)) ){.
1bf5c 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 sqlite3VdbeMemSt
1bf5d 72 69 6e 67 69 66 79 28 70 52 65 63 2c 20 65 6e ringify(pRec, en
1bf5e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 c);. }. pR
1bf5f 65 63 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d ec->flags &= ~(M
1bf60 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 EM_Real|MEM_Int)
1bf61 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 61 66 ;. }else if( af
1bf62 66 69 6e 69 74 79 21 3d 53 51 4c 49 54 45 5f 41 finity!=SQLITE_A
1bf63 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61 FF_NONE ){. a
1bf64 73 73 65 72 74 28 20 61 66 66 69 6e 69 74 79 3d ssert( affinity=
1bf65 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 =SQLITE_AFF_INTE
1bf66 47 45 52 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d GER || affinity=
1bf67 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c =SQLITE_AFF_REAL
1bf68 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c . ||
1bf69 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 affinity==SQLIT
1bf6a 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b E_AFF_NUMERIC );
1bf6b 0a 20 20 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 . applyNumeri
1bf6c 63 41 66 66 69 6e 69 74 79 28 70 52 65 63 29 3b cAffinity(pRec);
1bf6d 0a 20 20 20 20 69 66 28 20 70 52 65 63 2d 3e 66 . if( pRec->f
1bf6e 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 lags & MEM_Real
1bf6f 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1bf70 56 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e VdbeIntegerAffin
1bf71 69 74 79 28 70 52 65 63 29 3b 0a 20 20 20 20 7d ity(pRec);. }
1bf72 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 . }.}../*.** Tr
1bf73 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 y to convert the
1bf74 20 74 79 70 65 20 6f 66 20 61 20 66 75 6e 63 74 type of a funct
1bf75 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 72 20 ion argument or
1bf76 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a a result column.
1bf77 2a 2a 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 ** into a numeri
1bf78 63 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e c representation
1bf79 2e 20 20 55 73 65 20 65 69 74 68 65 72 20 49 4e . Use either IN
1bf7a 54 45 47 45 52 20 6f 72 20 52 45 41 4c 20 77 68 TEGER or REAL wh
1bf7b 69 63 68 65 76 65 72 0a 2a 2a 20 69 73 20 61 70 ichever.** is ap
1bf7c 70 72 6f 70 72 69 61 74 65 2e 20 20 42 75 74 20 propriate. But
1bf7d 6f 6e 6c 79 20 64 6f 20 74 68 65 20 63 6f 6e 76 only do the conv
1bf7e 65 72 73 69 6f 6e 20 69 66 20 69 74 20 69 73 20 ersion if it is
1bf7f 70 6f 73 73 69 62 6c 65 20 77 69 74 68 6f 75 74 possible without
1bf80 0a 2a 2a 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f .** loss of info
1bf81 72 6d 61 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 rmation and retu
1bf82 72 6e 20 74 68 65 20 72 65 76 69 73 65 64 20 74 rn the revised t
1bf83 79 70 65 20 6f 66 20 74 68 65 20 61 72 67 75 6d ype of the argum
1bf84 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ent..**.** This
1bf85 69 73 20 61 6e 20 45 58 50 45 52 49 4d 45 4e 54 is an EXPERIMENT
1bf86 41 4c 20 61 70 69 20 61 6e 64 20 69 73 20 73 75 AL api and is su
1bf87 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 bject to change
1bf88 6f 72 20 72 65 6d 6f 76 61 6c 2e 0a 2a 2f 0a 53 or removal..*/.S
1bf89 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1bf8a 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 lite3_value_nume
1bf8b 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33 ric_type(sqlite3
1bf8c 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 _value *pVal){.
1bf8d 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 28 4d 65 Mem *pMem = (Me
1bf8e 6d 2a 29 70 56 61 6c 3b 0a 20 20 61 70 70 6c 79 m*)pVal;. apply
1bf8f 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 NumericAffinity(
1bf90 70 4d 65 6d 29 3b 0a 20 20 73 74 6f 72 65 54 79 pMem);. storeTy
1bf91 70 65 49 6e 66 6f 28 70 4d 65 6d 2c 20 30 29 3b peInfo(pMem, 0);
1bf92 0a 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e . return pMem->
1bf93 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 type;.}../*.** E
1bf94 78 70 6f 72 74 65 64 20 76 65 72 73 69 6f 6e 20 xported version
1bf95 6f 66 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 of applyAffinity
1bf96 28 29 2e 20 54 68 69 73 20 6f 6e 65 20 77 6f 72 (). This one wor
1bf97 6b 73 20 6f 6e 20 73 71 6c 69 74 65 33 5f 76 61 ks on sqlite3_va
1bf98 6c 75 65 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74 68 lue*, .** not th
1bf99 65 20 69 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a 20 e internal Mem*
1bf9a 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f type..*/.SQLITE_
1bf9b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1bf9c 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 ite3ValueApplyAf
1bf9d 66 69 6e 69 74 79 28 0a 20 20 73 71 6c 69 74 65 finity(. sqlite
1bf9e 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 0a 3_value *pVal, .
1bf9f 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20 0a u8 affinity, .
1bfa0 20 20 75 38 20 65 6e 63 0a 29 7b 0a 20 20 61 70 u8 enc.){. ap
1bfa1 70 6c 79 41 66 66 69 6e 69 74 79 28 28 4d 65 6d plyAffinity((Mem
1bfa2 20 2a 29 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 *)pVal, affinit
1bfa3 79 2c 20 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66 64 y, enc);.}..#ifd
1bfa4 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1bfa5 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 6e 69 /*.** Write a ni
1bfa6 63 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 ce string repres
1bfa7 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
1bfa8 63 6f 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c contents of cell
1bfa9 20 70 4d 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62 75 pMem.** into bu
1bfaa 66 66 65 72 20 7a 42 75 66 2c 20 6c 65 6e 67 74 ffer zBuf, lengt
1bfab 68 20 6e 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49 54 h nBuf..*/.SQLIT
1bfac 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1bfad 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 qlite3VdbeMemPre
1bfae 74 74 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d ttyPrint(Mem *pM
1bfaf 65 6d 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b em, char *zBuf){
1bfb0 0a 20 20 63 68 61 72 20 2a 7a 43 73 72 20 3d 20 . char *zCsr =
1bfb1 7a 42 75 66 3b 0a 20 20 69 6e 74 20 66 20 3d 20 zBuf;. int f =
1bfb2 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 pMem->flags;..
1bfb3 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 static const cha
1bfb4 72 20 2a 63 6f 6e 73 74 20 65 6e 63 6e 61 6d 65 r *const encname
1bfb5 73 5b 5d 20 3d 20 7b 22 28 58 29 22 2c 20 22 28 s[] = {"(X)", "(
1bfb6 38 29 22 2c 20 22 28 31 36 4c 45 29 22 2c 20 22 8)", "(16LE)", "
1bfb7 28 31 36 42 45 29 22 7d 3b 0a 0a 20 20 69 66 28 (16BE)"};.. if(
1bfb8 20 66 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20 f&MEM_Blob ){.
1bfb9 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 68 int i;. ch
1bfba 61 72 20 63 3b 0a 20 20 20 20 69 66 28 20 66 20 ar c;. if( f
1bfbb 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 & MEM_Dyn ){.
1bfbc 20 20 20 63 20 3d 20 27 7a 27 3b 0a 20 20 20 20 c = 'z';.
1bfbd 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 assert( (f & (
1bfbe 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 MEM_Static|MEM_E
1bfbf 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 phem))==0 );.
1bfc0 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d }else if( f & M
1bfc1 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 EM_Static ){.
1bfc2 20 20 20 63 20 3d 20 27 74 27 3b 0a 20 20 20 20 c = 't';.
1bfc3 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 assert( (f & (
1bfc4 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 MEM_Dyn|MEM_Ephe
1bfc5 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 m))==0 );. }e
1bfc6 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f lse if( f & MEM_
1bfc7 45 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20 63 Ephem ){. c
1bfc8 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73 = 'e';. as
1bfc9 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f sert( (f & (MEM_
1bfca 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 Static|MEM_Dyn))
1bfcb 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 ==0 );. }else
1bfcc 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 73 27 3b {. c = 's';
1bfcd 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 . }.. sqli
1bfce 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 te3_snprintf(100
1bfcf 2c 20 7a 43 73 72 2c 20 22 25 63 22 2c 20 63 29 , zCsr, "%c", c)
1bfd0 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 ;. zCsr += sq
1bfd1 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 lite3Strlen30(zC
1bfd2 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 sr);. sqlite3
1bfd3 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a _snprintf(100, z
1bfd4 43 73 72 2c 20 22 25 64 5b 22 2c 20 70 4d 65 6d Csr, "%d[", pMem
1bfd5 2d 3e 6e 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b ->n);. zCsr +
1bfd6 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
1bfd7 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 66 6f 72 0(zCsr);. for
1bfd8 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c (i=0; i<16 && i<
1bfd9 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 pMem->n; i++){.
1bfda 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
1bfdb 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c rintf(100, zCsr,
1bfdc 20 22 25 30 32 58 22 2c 20 28 28 69 6e 74 29 70 "%02X", ((int)p
1bfdd 4d 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30 78 46 46 Mem->z[i] & 0xFF
1bfde 29 29 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b ));. zCsr +
1bfdf 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
1bfe0 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 20 0(zCsr);. }.
1bfe1 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 36 for(i=0; i<16
1bfe2 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 && i<pMem->n; i
1bfe3 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 ++){. char
1bfe4 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d 3b 0a z = pMem->z[i];.
1bfe5 20 20 20 20 20 20 69 66 28 20 7a 3c 33 32 20 7c if( z<32 |
1bfe6 7c 20 7a 3e 31 32 36 20 29 20 2a 7a 43 73 72 2b | z>126 ) *zCsr+
1bfe7 2b 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 65 + = '.';. e
1bfe8 6c 73 65 20 2a 7a 43 73 72 2b 2b 20 3d 20 7a 3b lse *zCsr++ = z;
1bfe9 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 . }.. sqli
1bfea 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 te3_snprintf(100
1bfeb 2c 20 7a 43 73 72 2c 20 22 5d 25 73 22 2c 20 65 , zCsr, "]%s", e
1bfec 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e ncnames[pMem->en
1bfed 63 5d 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d c]);. zCsr +=
1bfee 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
1bfef 28 7a 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20 (zCsr);. if(
1bff0 66 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a f & MEM_Zero ){.
1bff1 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
1bff2 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 printf(100, zCsr
1bff3 2c 22 2b 25 64 7a 22 2c 70 4d 65 6d 2d 3e 75 2e ,"+%dz",pMem->u.
1bff4 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7a 43 nZero);. zC
1bff5 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 sr += sqlite3Str
1bff6 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 len30(zCsr);.
1bff7 20 7d 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20 27 }. *zCsr = '
1bff8 5c 30 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 \0';. }else if(
1bff9 20 66 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a f & MEM_Str ){.
1bffa 20 20 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20 20 int j, k;.
1bffb 20 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b zBuf[0] = ' ';
1bffc 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d . if( f & MEM
1bffd 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a 42 _Dyn ){. zB
1bffe 75 66 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20 20 uf[1] = 'z';.
1bfff 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 assert( (f &
1c000 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f (MEM_Static|MEM_
1c001 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 Ephem))==0 );.
1c002 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 }else if( f &
1c003 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 MEM_Static ){.
1c004 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 74 zBuf[1] = 't
1c005 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ';. assert(
1c006 20 28 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d (f & (MEM_Dyn|M
1c007 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b EM_Ephem))==0 );
1c008 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 . }else if( f
1c009 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a & MEM_Ephem ){.
1c00a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 zBuf[1] =
1c00b 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 'e';. asser
1c00c 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 t( (f & (MEM_Sta
1c00d 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 tic|MEM_Dyn))==0
1c00e 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 );. }else{.
1c00f 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 zBuf[1] = '
1c010 73 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b 20 s';. }. k
1c011 3d 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 = 2;. sqlite3
1c012 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 26 _snprintf(100, &
1c013 7a 42 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20 70 zBuf[k], "%d", p
1c014 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20 2b Mem->n);. k +
1c015 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
1c016 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 0(&zBuf[k]);.
1c017 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5b 27 zBuf[k++] = '['
1c018 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a ;. for(j=0; j
1c019 3c 31 35 20 26 26 20 6a 3c 70 4d 65 6d 2d 3e 6e <15 && j<pMem->n
1c01a 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 ; j++){. u8
1c01b 20 63 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d 3b c = pMem->z[j];
1c01c 0a 20 20 20 20 20 20 69 66 28 20 63 3e 3d 30 78 . if( c>=0x
1c01d 32 30 20 26 26 20 63 3c 30 78 37 66 20 29 7b 0a 20 && c<0x7f ){.
1c01e 20 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b zBuf[k++
1c01f 5d 20 3d 20 63 3b 0a 20 20 20 20 20 20 7d 65 6c ] = c;. }el
1c020 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 66 se{. zBuf
1c021 5b 6b 2b 2b 5d 20 3d 20 27 2e 27 3b 0a 20 20 20 [k++] = '.';.
1c022 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a }. }. z
1c023 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5d 27 3b 0a Buf[k++] = ']';.
1c024 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1c025 69 6e 74 66 28 31 30 30 2c 26 7a 42 75 66 5b 6b intf(100,&zBuf[k
1c026 5d 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d ], encnames[pMem
1c027 2d 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 6b 20 2b ->enc]);. k +
1c028 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
1c029 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 0(&zBuf[k]);.
1c02a 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 30 3b 0a zBuf[k++] = 0;.
1c02b 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 }.}.#endif..#i
1c02c 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1c02d 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 G./*.** Print th
1c02e 65 20 76 61 6c 75 65 20 6f 66 20 61 20 72 65 67 e value of a reg
1c02f 69 73 74 65 72 20 66 6f 72 20 74 72 61 63 69 6e ister for tracin
1c030 67 20 70 75 72 70 6f 73 65 73 3a 0a 2a 2f 0a 73 g purposes:.*/.s
1c031 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 54 72 tatic void memTr
1c032 61 63 65 50 72 69 6e 74 28 46 49 4c 45 20 2a 6f acePrint(FILE *o
1c033 75 74 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 ut, Mem *p){. i
1c034 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 f( p->flags & ME
1c035 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 66 70 M_Null ){. fp
1c036 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 4e 55 4c rintf(out, " NUL
1c037 4c 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 L");. }else if(
1c038 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 (p->flags & (ME
1c039 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 29 3d M_Int|MEM_Str))=
1c03a 3d 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 =(MEM_Int|MEM_St
1c03b 72 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 r) ){. fprint
1c03c 66 28 6f 75 74 2c 20 22 20 73 69 3a 25 6c 6c 64 f(out, " si:%lld
1c03d 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 ", p->u.i);. }e
1c03e 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 lse if( p->flags
1c03f 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 & MEM_Int ){.
1c040 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
1c041 20 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 i:%lld", p->u.i
1c042 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
1c043 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
1c044 50 4f 49 4e 54 0a 20 20 7d 65 6c 73 65 20 69 66 POINT. }else if
1c045 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d ( p->flags & MEM
1c046 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 66 70 72 _Real ){. fpr
1c047 69 6e 74 66 28 6f 75 74 2c 20 22 20 72 3a 25 67 intf(out, " r:%g
1c048 22 2c 20 70 2d 3e 72 29 3b 0a 23 65 6e 64 69 66 ", p->r);.#endif
1c049 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e . }else if( p->
1c04a 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
1c04b 65 74 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 et ){. fprint
1c04c 66 28 6f 75 74 2c 20 22 20 28 72 6f 77 73 65 74 f(out, " (rowset
1c04d 29 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 )");. }else{.
1c04e 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 30 5d char zBuf[200]
1c04f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1c050 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 eMemPrettyPrint(
1c051 70 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 p, zBuf);. fp
1c052 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 22 29 3b rintf(out, " ");
1c053 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 . fprintf(out
1c054 2c 20 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a 20 , "%s", zBuf);.
1c055 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 }.}.static void
1c056 20 72 65 67 69 73 74 65 72 54 72 61 63 65 28 46 registerTrace(F
1c057 49 4c 45 20 2a 6f 75 74 2c 20 69 6e 74 20 69 52 ILE *out, int iR
1c058 65 67 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 66 eg, Mem *p){. f
1c059 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 52 45 47 printf(out, "REG
1c05a 5b 25 64 5d 20 3d 20 22 2c 20 69 52 65 67 29 3b [%d] = ", iReg);
1c05b 0a 20 20 6d 65 6d 54 72 61 63 65 50 72 69 6e 74 . memTracePrint
1c05c 28 6f 75 74 2c 20 70 29 3b 0a 20 20 66 70 72 69 (out, p);. fpri
1c05d 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a ntf(out, "\n");.
1c05e 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 }.#endif..#ifdef
1c05f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 20 SQLITE_DEBUG.#
1c060 20 64 65 66 69 6e 65 20 52 45 47 49 53 54 45 52 define REGISTER
1c061 5f 54 52 41 43 45 28 52 2c 4d 29 20 69 66 28 70 _TRACE(R,M) if(p
1c062 2d 3e 74 72 61 63 65 29 72 65 67 69 73 74 65 72 ->trace)register
1c063 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c 52 Trace(p->trace,R
1c064 2c 4d 29 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 ,M).#else.# def
1c065 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54 52 41 ine REGISTER_TRA
1c066 43 45 28 52 2c 4d 29 0a 23 65 6e 64 69 66 0a 0a CE(R,M).#endif..
1c067 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f .#ifdef VDBE_PRO
1c068 46 49 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 FILE../* .** hwt
1c069 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 ime.h contains i
1c06a 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 nline assembler
1c06b 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 code for impleme
1c06c 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 nting .** high-p
1c06d 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e erformance timin
1c06e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f g routines..*/./
1c06f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 ************** I
1c070 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 nclude hwtime.h
1c071 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
1c072 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a vdbe.c ********
1c073 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1c074 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
1c075 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 egin file hwtime
1c076 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
1c077 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c078 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1c079 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 *.** 2008 May 27
1c07a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
1c07b 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1c07c 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1c07d 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
1c07e 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
1c07f 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
1c080 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
1c081 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
1c082 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
1c083 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
1c084 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
1c085 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
1c086 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
1c087 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
1c088 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
1c089 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
1c08a 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
1c08b 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
1c08c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c08d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c08e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c08f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c090 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
1c091 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e file contains in
1c092 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f line asm code fo
1c093 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 r retrieving "hi
1c094 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a gh-performance".
1c095 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 ** counters for
1c096 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a x86 class CPUs..
1c097 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d **.** $Id: hwtim
1c098 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 e.h,v 1.3 2008/0
1c099 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 8/01 14:33:15 sh
1c09a 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 ane Exp $.*/.#if
1c09b 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a ndef _HWTIME_H_.
1c09c 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f #define _HWTIME_
1c09d 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f H_../*.** The fo
1c09e 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 llowing routine
1c09f 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 only works on pe
1c0a0 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 ntium-class (or
1c0a1 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 newer) processor
1c0a2 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 s..** It uses th
1c0a3 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 e RDTSC opcode t
1c0a4 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 o read the cycle
1c0a5 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 count value out
1c0a6 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 of the.** proce
1c0a7 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 ssor and returns
1c0a8 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 that value. Th
1c0a9 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 is can be used f
1c0aa 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 or high-res.** p
1c0ab 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 rofiling..*/.#if
1c0ac 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 (defined(__GNUC
1c0ad 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f __) || defined(_
1c0ae 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 MSC_VER)) && \.
1c0af 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 (defined(i3
1c0b0 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 86) || defined(_
1c0b1 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 _i386__) || defi
1c0b2 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 ned(_M_IX86))..
1c0b3 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 #if defined(__G
1c0b4 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 NUC__).. __inli
1c0b5 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 ne__ sqlite_uint
1c0b6 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 64 sqlite3Hwtime
1c0b7 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 (void){. uns
1c0b8 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 igned int lo, hi
1c0b9 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f ;. __asm__ _
1c0ba 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 _volatile__ ("rd
1c0bb 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 tsc" : "=a" (lo)
1c0bc 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 , "=d" (hi));.
1c0bd 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 return (sqlit
1c0be 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 e_uint64)hi << 3
1c0bf 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 2 | lo;. }.. #
1c0c0 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 elif defined(_MS
1c0c1 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c C_VER).. __decl
1c0c2 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e spec(naked) __in
1c0c3 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 line sqlite_uint
1c0c4 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 64 __cdecl sqlit
1c0c5 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
1c0c6 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 __asm {.
1c0c7 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 rdtsc.
1c0c8 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 ret ; r
1c0c9 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 eturn value at E
1c0ca 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 DX:EAX. }.
1c0cb 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c }.. #endif..#el
1c0cc 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e if (defined(__GN
1c0cd 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 UC__) && defined
1c0ce 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 (__x86_64__))..
1c0cf 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 __inline__ sqli
1c0d0 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
1c0d1 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 3Hwtime(void){.
1c0d2 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f unsigned lo
1c0d3 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f ng val;. __
1c0d4 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 asm__ __volatile
1c0d5 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d __ ("rdtsc" : "=
1c0d6 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 A" (val));.
1c0d7 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d return val;. }
1c0d8 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 . .#elif (define
1c0d9 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 d(__GNUC__) && d
1c0da 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 efined(__ppc__))
1c0db 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 .. __inline__ s
1c0dc 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
1c0dd 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
1c0de 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 {. unsigned
1c0df 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 long long retva
1c0e0 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 l;. unsigne
1c0e1 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 d long junk;.
1c0e2 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
1c0e3 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 atile__ ("\n\.
1c0e4 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 1:
1c0e5 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 mftbu %1\n\.
1c0e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c0e7 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 mftb %L0\n\.
1c0e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c0e9 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 mftbu %0\n\.
1c0ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c0eb 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e cmpw %0,%1\n
1c0ec 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
1c0ed 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a bne 1b".
1c0ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c0ef 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c : "=r" (retval
1c0f0 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b ), "=r" (junk));
1c0f1 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 . return re
1c0f2 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 tval;. }..#else
1c0f3 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 .. #error Need
1c0f4 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
1c0f5 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 f sqlite3Hwtime(
1c0f6 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 ) for your platf
1c0f7 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 orm... /*. **
1c0f8 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f To compile witho
1c0f9 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 ut implementing
1c0fa 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 sqlite3Hwtime()
1c0fb 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 for your platfor
1c0fc 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 m,. ** you can
1c0fd 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 remove the above
1c0fe 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 #error and use
1c0ff 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 the following.
1c100 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e ** stub function
1c101 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 . You will lose
1c102 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 timing support
1c103 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 for many. ** of
1c104 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 the debugging a
1c105 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 nd testing utili
1c106 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f ties, but it sho
1c107 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 uld at. ** leas
1c108 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 t compile and ru
1c109 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 n.. */.SQLITE_P
1c10a 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f RIVATE sqlite_
1c10b 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 uint64 sqlite3Hw
1c10c 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 time(void){ retu
1c10d 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 rn ((sqlite_uint
1c10e 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 64)0); }..#endif
1c10f 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 ..#endif /* !def
1c110 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 ined(_HWTIME_H_)
1c111 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
1c112 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 **** End of hwti
1c113 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a me.h ***********
1c114 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c115 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c116 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
1c117 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
1c118 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
1c119 66 20 69 6e 20 76 64 62 65 2e 63 20 2a 2a 2a 2a f in vdbe.c ****
1c11a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c11b 2a 2a 2a 2f 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a ***/..#endif../*
1c11c 0a 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f 46 4f .** The CHECK_FO
1c11d 52 5f 49 4e 54 45 52 52 55 50 54 20 6d 61 63 72 R_INTERRUPT macr
1c11e 6f 20 64 65 66 69 6e 65 64 20 68 65 72 65 20 6c o defined here l
1c11f 6f 6f 6b 73 20 74 6f 20 73 65 65 20 69 66 20 74 ooks to see if t
1c120 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e he.** sqlite3_in
1c121 74 65 72 72 75 70 74 28 29 20 72 6f 75 74 69 6e terrupt() routin
1c122 65 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 e has been calle
1c123 64 2e 20 20 49 66 20 69 74 20 68 61 73 20 62 65 d. If it has be
1c124 65 6e 2c 20 74 68 65 6e 0a 2a 2a 20 70 72 6f 63 en, then.** proc
1c125 65 73 73 69 6e 67 20 6f 66 20 74 68 65 20 56 44 essing of the VD
1c126 42 45 20 70 72 6f 67 72 61 6d 20 69 73 20 69 6e BE program is in
1c127 74 65 72 72 75 70 74 65 64 2e 0a 2a 2a 0a 2a 2a terrupted..**.**
1c128 20 54 68 69 73 20 6d 61 63 72 6f 20 61 64 64 65 This macro adde
1c129 64 20 74 6f 20 65 76 65 72 79 20 69 6e 73 74 72 d to every instr
1c12a 75 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65 73 uction that does
1c12b 20 61 20 6a 75 6d 70 20 69 6e 20 6f 72 64 65 72 a jump in order
1c12c 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 to.** implement
1c12d 20 61 20 6c 6f 6f 70 2e 20 20 54 68 69 73 20 74 a loop. This t
1c12e 65 73 74 20 75 73 65 64 20 74 6f 20 62 65 20 6f est used to be o
1c12f 6e 20 65 76 65 72 79 20 73 69 6e 67 6c 65 20 69 n every single i
1c130 6e 73 74 72 75 63 74 69 6f 6e 2c 0a 2a 2a 20 62 nstruction,.** b
1c131 75 74 20 74 68 61 74 20 6d 65 61 6e 74 20 77 65 ut that meant we
1c132 20 6d 6f 72 65 20 74 65 73 74 69 6e 67 20 74 68 more testing th
1c133 61 74 20 77 65 20 6e 65 65 64 65 64 2e 20 20 42 at we needed. B
1c134 79 20 6f 6e 6c 79 20 74 65 73 74 69 6e 67 20 74 y only testing t
1c135 68 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 6a 75 he.** flag on ju
1c136 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2c mp instructions,
1c137 20 77 65 20 67 65 74 20 61 20 28 73 6d 61 6c 6c we get a (small
1c138 29 20 73 70 65 65 64 20 69 6d 70 72 6f 76 65 6d ) speed improvem
1c139 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ent..*/.#define
1c13a 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 CHECK_FOR_INTERR
1c13b 55 50 54 20 5c 0a 20 20 20 69 66 28 20 64 62 2d UPT \. if( db-
1c13c 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 >u1.isInterrupte
1c13d 64 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 d ) goto abort_d
1c13e 75 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74 3b ue_to_interrupt;
1c13f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
1c140 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74 DEBUG.static int
1c141 20 66 69 6c 65 45 78 69 73 74 73 28 73 71 6c 69 fileExists(sqli
1c142 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 te3 *db, const c
1c143 68 61 72 20 2a 7a 46 69 6c 65 29 7b 0a 20 20 69 har *zFile){. i
1c144 6e 74 20 72 65 73 20 3d 20 30 3b 0a 20 20 69 6e nt res = 0;. in
1c145 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
1c146 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
1c147 54 45 53 54 0a 20 20 2f 2a 20 49 66 20 77 65 20 TEST. /* If we
1c148 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 74 65 are currently te
1c149 73 74 69 6e 67 20 49 4f 20 65 72 72 6f 72 73 2c sting IO errors,
1c14a 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 63 61 6c then do not cal
1c14b 6c 20 4f 73 41 63 63 65 73 73 28 29 20 74 6f 0a l OsAccess() to.
1c14c 20 20 2a 2a 20 74 65 73 74 20 66 6f 72 20 74 68 ** test for th
1c14d 65 20 70 72 65 73 65 6e 63 65 20 6f 66 20 7a 46 e presence of zF
1c14e 69 6c 65 2e 20 54 68 69 73 20 69 73 20 62 65 63 ile. This is bec
1c14f 61 75 73 65 20 61 6e 79 20 49 4f 20 65 72 72 6f ause any IO erro
1c150 72 20 74 68 61 74 0a 20 20 2a 2a 20 6f 63 63 75 r that. ** occu
1c151 72 73 20 68 65 72 65 20 77 69 6c 6c 20 6e 6f 74 rs here will not
1c152 20 62 65 20 72 65 70 6f 72 74 65 64 2c 20 63 61 be reported, ca
1c153 75 73 69 6e 67 20 74 68 65 20 74 65 73 74 20 74 using the test t
1c154 6f 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 65 o fail.. */. e
1c155 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 xtern int sqlite
1c156 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 3_io_error_pendi
1c157 6e 67 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 ng;. if( sqlite
1c158 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 3_io_error_pendi
1c159 6e 67 3c 3d 30 20 29 0a 23 65 6e 64 69 66 0a 20 ng<=0 ).#endif.
1c15a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
1c15b 73 41 63 63 65 73 73 28 64 62 2d 3e 70 56 66 73 sAccess(db->pVfs
1c15c 2c 20 7a 46 69 6c 65 2c 20 53 51 4c 49 54 45 5f , zFile, SQLITE_
1c15d 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 ACCESS_EXISTS, &
1c15e 72 65 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 res);. return (
1c15f 72 65 73 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 res && rc==SQLIT
1c160 45 5f 4f 4b 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a E_OK);.}.#endif.
1c161 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
1c162 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
1c163 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c ion is only call
1c164 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 ed from within a
1c165 6e 20 61 73 73 65 72 74 28 29 20 65 78 70 72 65 n assert() expre
1c166 73 73 69 6f 6e 2e 20 49 74 0a 2a 2a 20 63 68 65 ssion. It.** che
1c167 63 6b 73 20 74 68 61 74 20 74 68 65 20 73 71 6c cks that the sql
1c168 69 74 65 33 2e 6e 54 72 61 6e 73 61 63 74 69 6f ite3.nTransactio
1c169 6e 20 76 61 72 69 61 62 6c 65 20 69 73 20 63 6f n variable is co
1c16a 72 72 65 63 74 6c 79 20 73 65 74 20 74 6f 0a 2a rrectly set to.*
1c16b 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
1c16c 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 non-transaction
1c16d 73 61 76 65 70 6f 69 6e 74 73 20 63 75 72 72 65 savepoints curre
1c16e 6e 74 6c 79 20 69 6e 20 74 68 65 20 0a 2a 2a 20 ntly in the .**
1c16f 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 74 61 72 linked list star
1c170 74 69 6e 67 20 61 74 20 73 71 6c 69 74 65 33 2e ting at sqlite3.
1c171 70 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 20 0a pSavepoint..** .
1c172 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 ** Usage:.**.**
1c173 20 20 20 20 61 73 73 65 72 74 28 20 63 68 65 63 assert( chec
1c174 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 kSavepointCount(
1c175 64 62 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 db) );.*/.static
1c176 20 69 6e 74 20 63 68 65 63 6b 53 61 76 65 70 6f int checkSavepo
1c177 69 6e 74 43 6f 75 6e 74 28 73 71 6c 69 74 65 33 intCount(sqlite3
1c178 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 6e 20 3d *db){. int n =
1c179 20 30 3b 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 0;. Savepoint
1c17a 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 64 62 2d 3e *p;. for(p=db->
1c17b 70 53 61 76 65 70 6f 69 6e 74 3b 20 70 3b 20 70 pSavepoint; p; p
1c17c 3d 70 2d 3e 70 4e 65 78 74 29 20 6e 2b 2b 3b 0a =p->pNext) n++;.
1c17d 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 28 64 62 assert( n==(db
1c17e 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2b 20 64 ->nSavepoint + d
1c17f 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e b->isTransaction
1c180 53 61 76 65 70 6f 69 6e 74 29 20 29 3b 0a 20 20 Savepoint) );.
1c181 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 return 1;.}.#end
1c182 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 if../*.** Execut
1c183 65 20 61 73 20 6d 75 63 68 20 6f 66 20 61 20 56 e as much of a V
1c184 44 42 45 20 70 72 6f 67 72 61 6d 20 61 73 20 77 DBE program as w
1c185 65 20 63 61 6e 20 74 68 65 6e 20 72 65 74 75 72 e can then retur
1c186 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 n..**.** sqlite3
1c187 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 29 20 VdbeMakeReady()
1c188 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62 must be called b
1c189 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74 69 efore this routi
1c18a 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a ne in order to.*
1c18b 2a 20 63 6c 6f 73 65 20 74 68 65 20 70 72 6f 67 * close the prog
1c18c 72 61 6d 20 77 69 74 68 20 61 20 66 69 6e 61 6c ram with a final
1c18d 20 4f 50 5f 48 61 6c 74 20 61 6e 64 20 74 6f 20 OP_Halt and to
1c18e 73 65 74 20 75 70 20 74 68 65 20 63 61 6c 6c 62 set up the callb
1c18f 61 63 6b 73 0a 2a 2a 20 61 6e 64 20 74 68 65 20 acks.** and the
1c190 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 70 6f error message po
1c191 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 inter..**.** Whe
1c192 6e 65 76 65 72 20 61 20 72 6f 77 20 6f 72 20 72 never a row or r
1c193 65 73 75 6c 74 20 64 61 74 61 20 69 73 20 61 76 esult data is av
1c194 61 69 6c 61 62 6c 65 2c 20 74 68 69 73 20 72 6f ailable, this ro
1c195 75 74 69 6e 65 20 77 69 6c 6c 20 65 69 74 68 65 utine will eithe
1c196 72 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 r.** invoke the
1c197 72 65 73 75 6c 74 20 63 61 6c 6c 62 61 63 6b 20 result callback
1c198 28 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 (if there is one
1c199 29 20 6f 72 20 72 65 74 75 72 6e 20 77 69 74 68 ) or return with
1c19a 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f 57 2e 0a .** SQLITE_ROW..
1c19b 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 61 74 74 65 **.** If an atte
1c19c 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f mpt is made to o
1c19d 70 65 6e 20 61 20 6c 6f 63 6b 65 64 20 64 61 74 pen a locked dat
1c19e 61 62 61 73 65 2c 20 74 68 65 6e 20 74 68 69 73 abase, then this
1c19f 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 69 6c 6c routine.** will
1c1a0 20 65 69 74 68 65 72 20 69 6e 76 6f 6b 65 20 74 either invoke t
1c1a1 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b he busy callback
1c1a2 20 28 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e (if there is on
1c1a3 65 29 20 6f 72 20 69 74 20 77 69 6c 6c 0a 2a 2a e) or it will.**
1c1a4 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 return SQLITE_B
1c1a5 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e USY..**.** If an
1c1a6 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 error occurs, a
1c1a7 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 n error message
1c1a8 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 6d 65 is written to me
1c1a9 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a mory obtained.**
1c1aa 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 from sqlite3_ma
1c1ab 6c 6c 6f 63 28 29 20 61 6e 64 20 70 2d 3e 7a 45 lloc() and p->zE
1c1ac 72 72 4d 73 67 20 69 73 20 6d 61 64 65 20 74 6f rrMsg is made to
1c1ad 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 6d point to that m
1c1ae 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68 65 20 65 72 emory..** The er
1c1af 72 6f 72 20 63 6f 64 65 20 69 73 20 73 74 6f 72 ror code is stor
1c1b0 65 64 20 69 6e 20 70 2d 3e 72 63 20 61 6e 64 20 ed in p->rc and
1c1b1 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 this routine ret
1c1b2 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 52 52 4f urns SQLITE_ERRO
1c1b3 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 R..**.** If the
1c1b4 63 61 6c 6c 62 61 63 6b 20 65 76 65 72 20 72 65 callback ever re
1c1b5 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 turns non-zero,
1c1b6 74 68 65 6e 20 74 68 65 20 70 72 6f 67 72 61 6d then the program
1c1b7 20 65 78 69 74 73 0a 2a 2a 20 69 6d 6d 65 64 69 exits.** immedi
1c1b8 61 74 65 6c 79 2e 20 20 54 68 65 72 65 20 77 69 ately. There wi
1c1b9 6c 6c 20 62 65 20 6e 6f 20 65 72 72 6f 72 20 6d ll be no error m
1c1ba 65 73 73 61 67 65 20 62 75 74 20 74 68 65 20 70 essage but the p
1c1bb 2d 3e 72 63 20 66 69 65 6c 64 20 69 73 0a 2a 2a ->rc field is.**
1c1bc 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 41 set to SQLITE_A
1c1bd 42 4f 52 54 20 61 6e 64 20 74 68 69 73 20 72 6f BORT and this ro
1c1be 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 utine will retur
1c1bf 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a n SQLITE_ERROR..
1c1c0 2a 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79 20 61 **.** A memory a
1c1c1 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 llocation error
1c1c2 63 61 75 73 65 73 20 70 2d 3e 72 63 20 74 6f 20 causes p->rc to
1c1c3 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 be set to SQLITE
1c1c4 5f 4e 4f 4d 45 4d 20 61 6e 64 20 74 68 69 73 0a _NOMEM and this.
1c1c5 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 72 65 ** routine to re
1c1c6 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
1c1c7 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20 66 R..**.** Other f
1c1c8 61 74 61 6c 20 65 72 72 6f 72 73 20 72 65 74 75 atal errors retu
1c1c9 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e rn SQLITE_ERROR.
1c1ca 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 69 .**.** After thi
1c1cb 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 66 69 s routine has fi
1c1cc 6e 69 73 68 65 64 2c 20 73 71 6c 69 74 65 33 56 nished, sqlite3V
1c1cd 64 62 65 46 69 6e 61 6c 69 7a 65 28 29 20 73 68 dbeFinalize() sh
1c1ce 6f 75 6c 64 20 62 65 0a 2a 2a 20 75 73 65 64 20 ould be.** used
1c1cf 74 6f 20 63 6c 65 61 6e 20 75 70 20 74 68 65 20 to clean up the
1c1d0 6d 65 73 73 20 74 68 61 74 20 77 61 73 20 6c 65 mess that was le
1c1d1 66 74 20 62 65 68 69 6e 64 2e 0a 2a 2f 0a 53 51 ft behind..*/.SQ
1c1d2 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1c1d3 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 sqlite3VdbeExec
1c1d4 28 0a 20 20 56 64 62 65 20 2a 70 20 20 20 20 20 (. Vdbe *p
1c1d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1c1d6 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 29 7b * The VDBE */.){
1c1d7 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 . int pc;
1c1d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1c1d9 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 The program cou
1c1da 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 2a 70 4f nter */. Op *pO
1c1db 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
1c1dc 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 /* Current
1c1dd 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 operation */. i
1c1de 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1c1df 4b 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c K; /* Val
1c1e0 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a ue to return */.
1c1e1 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1c1e2 70 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 p->db; /*
1c1e3 54 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a The database */.
1c1e4 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 3d 20 u8 encoding =
1c1e5 45 4e 43 28 64 62 29 3b 20 20 20 20 20 2f 2a 20 ENC(db); /*
1c1e6 54 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63 The database enc
1c1e7 6f 64 69 6e 67 20 2a 2f 0a 20 20 4d 65 6d 20 2a oding */. Mem *
1c1e8 70 49 6e 31 20 3d 20 30 3b 20 20 20 20 20 20 20 pIn1 = 0;
1c1e9 20 20 20 20 20 20 2f 2a 20 31 73 74 20 69 6e 70 /* 1st inp
1c1ea 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 ut operand */.
1c1eb 4d 65 6d 20 2a 70 49 6e 32 20 3d 20 30 3b 20 20 Mem *pIn2 = 0;
1c1ec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 6e /* 2n
1c1ed 64 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 64 20 d input operand
1c1ee 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 33 20 3d */. Mem *pIn3 =
1c1ef 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1c1f0 2f 2a 20 33 72 64 20 69 6e 70 75 74 20 6f 70 65 /* 3rd input ope
1c1f1 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 rand */. Mem *p
1c1f2 4f 75 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 Out = 0;
1c1f3 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 6f /* Output o
1c1f4 70 65 72 61 6e 64 20 2a 2f 0a 20 20 75 38 20 6f perand */. u8 o
1c1f5 70 50 72 6f 70 65 72 74 79 3b 0a 20 20 69 6e 74 pProperty;. int
1c1f6 20 69 43 6f 6d 70 61 72 65 20 3d 20 30 3b 20 20 iCompare = 0;
1c1f7 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c /* Resul
1c1f8 74 20 6f 66 20 6c 61 73 74 20 4f 50 5f 43 6f 6d t of last OP_Com
1c1f9 70 61 72 65 20 6f 70 65 72 61 74 69 6f 6e 20 2a pare operation *
1c1fa 2f 0a 20 20 69 6e 74 20 2a 61 50 65 72 6d 75 74 /. int *aPermut
1c1fb 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f e = 0; /
1c1fc 2a 20 50 65 72 6d 75 74 61 74 69 6f 6e 20 6f 66 * Permutation of
1c1fd 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 4f 50 5f columns for OP_
1c1fe 43 6f 6d 70 61 72 65 20 2a 2f 0a 23 69 66 64 65 Compare */.#ifde
1c1ff 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 f VDBE_PROFILE.
1c200 20 75 36 34 20 73 74 61 72 74 3b 20 20 20 20 20 u64 start;
1c201 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
1c202 50 55 20 63 6c 6f 63 6b 20 63 6f 75 6e 74 20 61 PU clock count a
1c203 74 20 73 74 61 72 74 20 6f 66 20 6f 70 63 6f 64 t start of opcod
1c204 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 69 67 50 e */. int origP
1c205 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
1c206 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 /* Program cou
1c207 6e 74 65 72 20 61 74 20 73 74 61 72 74 20 6f 66 nter at start of
1c208 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 opcode */.#endi
1c209 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 f.#ifndef SQLITE
1c20a 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 _OMIT_PROGRESS_C
1c20b 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 6e 50 ALLBACK. int nP
1c20c 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 20 rogressOps = 0;
1c20d 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 73 20 /* Opcodes
1c20e 65 78 65 63 75 74 65 64 20 73 69 6e 63 65 20 70 executed since p
1c20f 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b rogress callback
1c210 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 2f 2a . */.#endif. /*
1c211 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c212 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c213 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c214 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c215 2a 2a 2a 0a 20 20 2a 2a 20 41 75 74 6f 6d 61 74 ***. ** Automat
1c216 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 ically generated
1c217 20 63 6f 64 65 0a 20 20 2a 2a 0a 20 20 2a 2a 20 code. **. **
1c218 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 75 6e The following un
1c219 69 6f 6e 20 69 73 20 61 75 74 6f 6d 61 74 69 63 ion is automatic
1c21a 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 62 ally generated b
1c21b 79 20 74 68 65 0a 20 20 2a 2a 20 76 64 62 65 2d y the. ** vdbe-
1c21c 63 6f 6d 70 72 65 73 73 2e 74 63 6c 20 73 63 72 compress.tcl scr
1c21d 69 70 74 2e 20 20 54 68 65 20 70 75 72 70 6f 73 ipt. The purpos
1c21e 65 20 6f 66 20 74 68 69 73 20 75 6e 69 6f 6e 20 e of this union
1c21f 69 73 20 74 6f 0a 20 20 2a 2a 20 72 65 64 75 63 is to. ** reduc
1c220 65 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 e the amount of
1c221 73 74 61 63 6b 20 73 70 61 63 65 20 72 65 71 75 stack space requ
1c222 69 72 65 64 20 62 79 20 74 68 69 73 20 66 75 6e ired by this fun
1c223 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 65 65 20 ction.. ** See
1c224 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 comments in the
1c225 76 64 62 65 2d 63 6f 6d 70 72 65 73 73 2e 74 63 vdbe-compress.tc
1c226 6c 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 74 l script for det
1c227 61 69 6c 73 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69 ails.. */. uni
1c228 6f 6e 20 76 64 62 65 45 78 65 63 55 6e 69 6f 6e on vdbeExecUnion
1c229 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 {. struct OP
1c22a 5f 59 69 65 6c 64 5f 73 74 61 63 6b 5f 76 61 72 _Yield_stack_var
1c22b 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 63 s {. int pc
1c22c 44 65 73 74 3b 0a 20 20 20 20 7d 20 61 61 3b 0a Dest;. } aa;.
1c22d 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 61 struct OP_Va
1c22e 72 69 61 62 6c 65 5f 73 74 61 63 6b 5f 76 61 72 riable_stack_var
1c22f 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 31 s {. int p1
1c230 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 ; /* Va
1c231 72 69 61 62 6c 65 20 74 6f 20 63 6f 70 79 20 66 riable to copy f
1c232 72 6f 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 rom */. int
1c233 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a p2; /*
1c234 20 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 Register to cop
1c235 79 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20 69 6e y to */. in
1c236 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f t n; /
1c237 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 * Number of valu
1c238 65 73 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 es left to copy
1c239 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 56 */. Mem *pV
1c23a 61 72 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 6c ar; /* Val
1c23b 75 65 20 62 65 69 6e 67 20 74 72 61 6e 73 66 65 ue being transfe
1c23c 72 72 65 64 20 2a 2f 0a 20 20 20 20 7d 20 61 62 rred */. } ab
1c23d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c23e 4d 6f 76 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 Move_stack_vars
1c23f 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4d {. char *zM
1c240 61 6c 6c 6f 63 3b 20 20 20 2f 2a 20 48 6f 6c 64 alloc; /* Hold
1c241 69 6e 67 20 76 61 72 69 61 62 6c 65 20 66 6f 72 ing variable for
1c242 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 allocated memor
1c243 79 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e y */. int n
1c244 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
1c245 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65 umber of registe
1c246 72 73 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 rs left to copy
1c247 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 31 3b */. int p1;
1c248 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
1c249 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 66 72 ister to copy fr
1c24a 6f 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 om */. int
1c24b 70 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 p2; /*
1c24c 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79 Register to copy
1c24d 20 74 6f 20 2a 2f 0a 20 20 20 20 7d 20 61 63 3b to */. } ac;
1c24e 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 . struct OP_R
1c24f 65 73 75 6c 74 52 6f 77 5f 73 74 61 63 6b 5f 76 esultRow_stack_v
1c250 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 ars {. Mem
1c251 2a 70 4d 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74 *pMem;. int
1c252 20 69 3b 0a 20 20 20 20 7d 20 61 64 3b 0a 20 20 i;. } ad;.
1c253 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6f 6e 63 struct OP_Conc
1c254 61 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a at_stack_vars {.
1c255 20 20 20 20 20 20 69 36 34 20 6e 42 79 74 65 3b i64 nByte;
1c256 0a 20 20 20 20 7d 20 61 65 3b 0a 20 20 20 20 73 . } ae;. s
1c257 74 72 75 63 74 20 4f 50 5f 52 65 6d 61 69 6e 64 truct OP_Remaind
1c258 65 72 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a er_stack_vars {.
1c259 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 3b int flags;
1c25a 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 /* Combine
1c25b 64 20 4d 45 4d 5f 2a 20 66 6c 61 67 73 20 66 72 d MEM_* flags fr
1c25c 6f 6d 20 62 6f 74 68 20 69 6e 70 75 74 73 20 2a om both inputs *
1c25d 2f 0a 20 20 20 20 20 20 69 36 34 20 69 41 3b 20 /. i64 iA;
1c25e 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 /* Integ
1c25f 65 72 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 er value of left
1c260 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 operand */.
1c261 20 20 69 36 34 20 69 42 3b 20 20 20 20 20 20 20 i64 iB;
1c262 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c /* Integer val
1c263 75 65 20 6f 66 20 72 69 67 68 74 20 6f 70 65 72 ue of right oper
1c264 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 64 6f 75 and */. dou
1c265 62 6c 65 20 72 41 3b 20 20 20 20 20 20 2f 2a 20 ble rA; /*
1c266 52 65 61 6c 20 76 61 6c 75 65 20 6f 66 20 6c 65 Real value of le
1c267 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 ft operand */.
1c268 20 20 20 20 64 6f 75 62 6c 65 20 72 42 3b 20 20 double rB;
1c269 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75 /* Real valu
1c26a 65 20 6f 66 20 72 69 67 68 74 20 6f 70 65 72 61 e of right opera
1c26b 6e 64 20 2a 2f 0a 20 20 20 20 7d 20 61 66 3b 0a nd */. } af;.
1c26c 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 46 75 struct OP_Fu
1c26d 6e 63 74 69 6f 6e 5f 73 74 61 63 6b 5f 76 61 72 nction_stack_var
1c26e 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b s {. int i;
1c26f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 41 72 67 . Mem *pArg
1c270 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
1c271 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 20 context ctx;.
1c272 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 sqlite3_value
1c273 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 20 20 20 20 **apVal;.
1c274 69 6e 74 20 6e 3b 0a 20 20 20 20 7d 20 61 67 3b int n;. } ag;
1c275 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 . struct OP_S
1c276 68 69 66 74 52 69 67 68 74 5f 73 74 61 63 6b 5f hiftRight_stack_
1c277 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 36 34 vars {. i64
1c278 20 61 3b 0a 20 20 20 20 20 20 69 36 34 20 62 3b a;. i64 b;
1c279 0a 20 20 20 20 7d 20 61 68 3b 0a 20 20 20 20 73 . } ah;. s
1c27a 74 72 75 63 74 20 4f 50 5f 47 65 5f 73 74 61 63 truct OP_Ge_stac
1c27b 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c27c 6e 74 20 66 6c 61 67 73 3b 0a 20 20 20 20 20 20 nt flags;.
1c27d 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 63 int res;. c
1c27e 68 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20 har affinity;.
1c27f 20 20 7d 20 61 69 3b 0a 20 20 20 20 73 74 72 75 } ai;. stru
1c280 63 74 20 4f 50 5f 43 6f 6d 70 61 72 65 5f 73 74 ct OP_Compare_st
1c281 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c282 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20 69 6e int n;. in
1c283 74 20 69 3b 0a 20 20 20 20 20 20 69 6e 74 20 70 t i;. int p
1c284 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b 1;. int p2;
1c285 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 4b 65 79 . const Key
1c286 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a Info *pKeyInfo;.
1c287 20 20 20 20 20 20 69 6e 74 20 69 64 78 3b 0a 20 int idx;.
1c288 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 CollSeq *pC
1c289 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 oll; /* Colla
1c28a 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 6f ting sequence to
1c28b 20 75 73 65 20 6f 6e 20 74 68 69 73 20 74 65 72 use on this ter
1c28c 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 62 m */. int b
1c28d 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Rev; /*
1c28e 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 45 4e True for DESCEN
1c28f 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65 72 20 DING sort order
1c290 2a 2f 0a 20 20 20 20 7d 20 61 6a 3b 0a 20 20 20 */. } aj;.
1c291 20 73 74 72 75 63 74 20 4f 50 5f 4f 72 5f 73 74 struct OP_Or_st
1c292 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c293 20 69 6e 74 20 76 31 3b 20 20 20 20 2f 2a 20 4c int v1; /* L
1c294 65 66 74 20 6f 70 65 72 61 6e 64 3a 20 20 30 3d eft operand: 0=
1c295 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c =FALSE, 1==TRUE,
1c296 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 2==UNKNOWN or N
1c297 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ULL */. int
1c298 20 76 32 3b 20 20 20 20 2f 2a 20 52 69 67 68 74 v2; /* Right
1c299 20 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46 41 4c operand: 0==FAL
1c29a 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 3d 3d SE, 1==TRUE, 2==
1c29b 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 UNKNOWN or NULL
1c29c 2a 2f 0a 20 20 20 20 7d 20 61 6b 3b 0a 20 20 20 */. } ak;.
1c29d 20 73 74 72 75 63 74 20 4f 50 5f 49 66 4e 6f 74 struct OP_IfNot
1c29e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c29f 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 7d int c;. }
1c2a0 20 61 6c 3b 0a 20 20 20 20 73 74 72 75 63 74 20 al;. struct
1c2a1 4f 50 5f 43 6f 6c 75 6d 6e 5f 73 74 61 63 6b 5f OP_Column_stack_
1c2a2 76 61 72 73 20 7b 0a 20 20 20 20 20 20 75 33 32 vars {. u32
1c2a3 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 payloadSize;
1c2a4 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1c2a5 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 es in the record
1c2a6 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 70 61 */. i64 pa
1c2a7 79 6c 6f 61 64 53 69 7a 65 36 34 3b 20 2f 2a 20 yloadSize64; /*
1c2a8 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1c2a9 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f in the record */
1c2aa 0a 20 20 20 20 20 20 69 6e 74 20 70 31 3b 20 20 . int p1;
1c2ab 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31 20 /* P1
1c2ac 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 63 value of the opc
1c2ad 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ode */. int
1c2ae 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 p2;
1c2af 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 /* column number
1c2b0 20 74 6f 20 72 65 74 72 69 65 76 65 20 2a 2f 0a to retrieve */.
1c2b1 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c2b2 20 2a 70 43 3b 20 20 20 20 2f 2a 20 54 68 65 20 *pC; /* The
1c2b3 56 44 42 45 20 63 75 72 73 6f 72 20 2a 2f 0a 20 VDBE cursor */.
1c2b4 20 20 20 20 20 63 68 61 72 20 2a 7a 52 65 63 3b char *zRec;
1c2b5 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
1c2b6 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 72 er to complete r
1c2b7 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20 20 ecord-data */.
1c2b8 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 BtCursor *pC
1c2b9 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20 42 54 rsr; /* The BT
1c2ba 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 ree cursor */.
1c2bb 20 20 20 20 75 33 32 20 2a 61 54 79 70 65 3b 20 u32 *aType;
1c2bc 20 20 20 20 20 20 20 2f 2a 20 61 54 79 70 65 5b /* aType[
1c2bd 69 5d 20 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d i] holds the num
1c2be 65 72 69 63 20 74 79 70 65 20 6f 66 20 74 68 65 eric type of the
1c2bf 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a i-th column */.
1c2c0 20 20 20 20 20 20 75 33 32 20 2a 61 4f 66 66 73 u32 *aOffs
1c2c1 65 74 3b 20 20 20 20 20 20 2f 2a 20 61 4f 66 66 et; /* aOff
1c2c2 73 65 74 5b 69 5d 20 69 73 20 6f 66 66 73 65 74 set[i] is offset
1c2c3 20 74 6f 20 73 74 61 72 74 20 6f 66 20 64 61 74 to start of dat
1c2c4 61 20 66 6f 72 20 69 2d 74 68 20 63 6f 6c 75 6d a for i-th colum
1c2c5 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e n */. int n
1c2c6 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 2f 2a Field; /*
1c2c7 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 number of field
1c2c8 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 s in the record
1c2c9 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6c 65 6e */. int len
1c2ca 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
1c2cb 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 he length of the
1c2cc 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74 61 serialized data
1c2cd 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 for the column
1c2ce 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 */. int i;
1c2cf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1c2d0 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
1c2d1 20 20 20 20 20 63 68 61 72 20 2a 7a 44 61 74 61 char *zData
1c2d2 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 72 74 20 ; /* Part
1c2d3 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 62 65 of the record be
1c2d4 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20 ing decoded */.
1c2d5 20 20 20 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b Mem *pDest;
1c2d6 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 /* Where
1c2d7 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65 78 to write the ex
1c2d8 74 72 61 63 74 65 64 20 76 61 6c 75 65 20 2a 2f tracted value */
1c2d9 0a 20 20 20 20 20 20 4d 65 6d 20 73 4d 65 6d 3b . Mem sMem;
1c2da 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 /* For
1c2db 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72 65 63 storing the rec
1c2dc 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 ord being decode
1c2dd 64 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 2a 7a d */. u8 *z
1c2de 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Idx; /*
1c2df 20 49 6e 64 65 78 20 69 6e 74 6f 20 68 65 61 64 Index into head
1c2e0 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 2a er */. u8 *
1c2e1 7a 45 6e 64 48 64 72 3b 20 20 20 20 20 20 20 2f zEndHdr; /
1c2e2 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 * Pointer to fir
1c2e3 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74 68 st byte after th
1c2e4 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 e header */.
1c2e5 20 20 75 33 32 20 6f 66 66 73 65 74 3b 20 20 20 u32 offset;
1c2e6 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 /* Offset i
1c2e7 6e 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f 0a nto the data */.
1c2e8 20 20 20 20 20 20 75 36 34 20 6f 66 66 73 65 74 u64 offset
1c2e9 36 34 3b 20 20 20 20 20 20 2f 2a 20 36 34 2d 62 64; /* 64-b
1c2ea 69 74 20 6f 66 66 73 65 74 2e 20 20 36 34 20 62 it offset. 64 b
1c2eb 69 74 73 20 6e 65 65 64 65 64 20 74 6f 20 63 61 its needed to ca
1c2ec 74 63 68 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a tch overflow */.
1c2ed 20 20 20 20 20 20 69 6e 74 20 73 7a 48 64 72 3b int szHdr;
1c2ee 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
1c2ef 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 73 of the header s
1c2f0 69 7a 65 20 66 69 65 6c 64 20 61 74 20 73 74 61 ize field at sta
1c2f1 72 74 20 6f 66 20 72 65 63 6f 72 64 20 2a 2f 0a rt of record */.
1c2f2 20 20 20 20 20 20 69 6e 74 20 61 76 61 69 6c 3b int avail;
1c2f3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1c2f4 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 61 er of bytes of a
1c2f5 76 61 69 6c 61 62 6c 65 20 64 61 74 61 20 2a 2f vailable data */
1c2f6 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 67 . Mem *pReg
1c2f7 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 73 65 ; /* Pse
1c2f8 75 64 6f 54 61 62 6c 65 20 69 6e 70 75 74 20 72 udoTable input r
1c2f9 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 7d egister */. }
1c2fa 20 61 6d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 am;. struct
1c2fb 4f 50 5f 41 66 66 69 6e 69 74 79 5f 73 74 61 63 OP_Affinity_stac
1c2fc 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 63 k_vars {. c
1c2fd 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 har *zAffinity;
1c2fe 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 /* The affinit
1c2ff 79 20 74 6f 20 62 65 20 61 70 70 6c 69 65 64 20 y to be applied
1c300 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 */. Mem *pD
1c301 61 74 61 30 3b 20 20 20 20 20 20 20 2f 2a 20 46 ata0; /* F
1c302 69 72 73 74 20 72 65 67 69 73 74 65 72 20 74 6f irst register to
1c303 20 77 68 69 63 68 20 74 6f 20 61 70 70 6c 79 20 which to apply
1c304 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20 20 20 20 affinity */.
1c305 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20 20 Mem *pLast;
1c306 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 72 65 67 /* Last reg
1c307 69 73 74 65 72 20 74 6f 20 77 68 69 63 68 20 74 ister to which t
1c308 6f 20 61 70 70 6c 79 20 61 66 66 69 6e 69 74 79 o apply affinity
1c309 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 */. Mem *p
1c30a 52 65 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Rec; /*
1c30b 43 75 72 72 65 6e 74 20 72 65 67 69 73 74 65 72 Current register
1c30c 20 2a 2f 0a 20 20 20 20 7d 20 61 6e 3b 0a 20 20 */. } an;.
1c30d 20 20 73 74 72 75 63 74 20 4f 50 5f 4d 61 6b 65 struct OP_Make
1c30e 52 65 63 6f 72 64 5f 73 74 61 63 6b 5f 76 61 72 Record_stack_var
1c30f 73 20 7b 0a 20 20 20 20 20 20 75 38 20 2a 7a 4e s {. u8 *zN
1c310 65 77 52 65 63 6f 72 64 3b 20 20 20 20 20 20 20 ewRecord;
1c311 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 /* A buffer to
1c312 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 66 6f hold the data fo
1c313 72 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 r the new record
1c314 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 */. Mem *p
1c315 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 Rec;
1c316 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 65 63 6f /* The new reco
1c317 72 64 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 rd */. u64
1c318 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 nData;
1c319 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1c31a 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 73 70 bytes of data sp
1c31b 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ace */. int
1c31c 20 6e 48 64 72 3b 20 20 20 20 20 20 20 20 20 20 nHdr;
1c31d 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1c31e 20 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 bytes of header
1c31f 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 space */.
1c320 69 36 34 20 6e 42 79 74 65 3b 20 20 20 20 20 20 i64 nByte;
1c321 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 73 /* Data s
1c322 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f pace required fo
1c323 72 20 74 68 69 73 20 72 65 63 6f 72 64 20 2a 2f r this record */
1c324 0a 20 20 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f . int nZero
1c325 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1c326 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20 Number of zero
1c327 62 79 74 65 73 20 61 74 20 74 68 65 20 65 6e 64 bytes at the end
1c328 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 2a of the record *
1c329 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 56 61 72 /. int nVar
1c32a 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f int; /
1c32b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
1c32c 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f s in a varint */
1c32d 0a 20 20 20 20 20 20 75 33 32 20 73 65 72 69 61 . u32 seria
1c32e 6c 5f 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a l_type; /*
1c32f 20 54 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 Type field */.
1c330 20 20 20 20 20 4d 65 6d 20 2a 70 44 61 74 61 30 Mem *pData0
1c331 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 ; /* F
1c332 69 72 73 74 20 66 69 65 6c 64 20 74 6f 20 62 65 irst field to be
1c333 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74 combined into t
1c334 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 he record */.
1c335 20 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20 Mem *pLast;
1c336 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 /* Las
1c337 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 72 t field of the r
1c338 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 ecord */. i
1c339 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 nt nField;
1c33a 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1c33b 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 of fields in the
1c33c 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 record */.
1c33d 20 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 char *zAffinity
1c33e 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 ; /* The a
1c33f 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 ffinity string f
1c340 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f or the record */
1c341 0a 20 20 20 20 20 20 69 6e 74 20 66 69 6c 65 5f . int file_
1c342 66 6f 72 6d 61 74 3b 20 20 20 20 20 20 20 2f 2a format; /*
1c343 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 74 6f 20 File format to
1c344 75 73 65 20 66 6f 72 20 65 6e 63 6f 64 69 6e 67 use for encoding
1c345 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 3b */. int i;
1c346 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c347 20 2f 2a 20 53 70 61 63 65 20 75 73 65 64 20 69 /* Space used i
1c348 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b 5d 20 2a n zNewRecord[] *
1c349 2f 0a 20 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b /. int len;
1c34a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1c34b 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61 20 66 69 * Length of a fi
1c34c 65 6c 64 20 2a 2f 0a 20 20 20 20 7d 20 61 6f 3b eld */. } ao;
1c34d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 43 . struct OP_C
1c34e 6f 75 6e 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 ount_stack_vars
1c34f 7b 0a 20 20 20 20 20 20 69 36 34 20 6e 45 6e 74 {. i64 nEnt
1c350 72 79 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 ry;. BtCurs
1c351 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 7d or *pCrsr;. }
1c352 20 61 70 3b 0a 20 20 20 20 73 74 72 75 63 74 20 ap;. struct
1c353 4f 50 5f 53 61 76 65 70 6f 69 6e 74 5f 73 74 61 OP_Savepoint_sta
1c354 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c355 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 int p1;
1c356 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c357 2f 2a 20 56 61 6c 75 65 20 6f 66 20 50 31 20 6f /* Value of P1 o
1c358 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 perand */.
1c359 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
1c35a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c35b 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 61 76 65 70 /* Name of savep
1c35c 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e oint */. in
1c35d 74 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 20 20 53 t nName;. S
1c35e 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a avepoint *pNew;.
1c35f 20 20 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 Savepoint
1c360 2a 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 *pSavepoint;.
1c361 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 54 Savepoint *pT
1c362 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 53 mp;. int iS
1c363 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 avepoint;.
1c364 69 6e 74 20 69 69 3b 0a 20 20 20 20 7d 20 61 71 int ii;. } aq
1c365 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c366 41 75 74 6f 43 6f 6d 6d 69 74 5f 73 74 61 63 6b AutoCommit_stack
1c367 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e _vars {. in
1c368 74 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d t desiredAutoCom
1c369 6d 69 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 mit;. int i
1c36a 52 6f 6c 6c 62 61 63 6b 3b 0a 20 20 20 20 20 20 Rollback;.
1c36b 69 6e 74 20 74 75 72 6e 4f 6e 41 43 3b 0a 20 20 int turnOnAC;.
1c36c 20 20 7d 20 61 72 3b 0a 20 20 20 20 73 74 72 75 } ar;. stru
1c36d 63 74 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f ct OP_Transactio
1c36e 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 n_stack_vars {.
1c36f 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 3b Btree *pBt;
1c370 0a 20 20 20 20 7d 20 61 73 3b 0a 20 20 20 20 73 . } as;. s
1c371 74 72 75 63 74 20 4f 50 5f 52 65 61 64 43 6f 6f truct OP_ReadCoo
1c372 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b kie_stack_vars {
1c373 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65 74 61 . int iMeta
1c374 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b ;. int iDb;
1c375 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6f 6b . int iCook
1c376 69 65 3b 0a 20 20 20 20 7d 20 61 74 3b 0a 20 20 ie;. } at;.
1c377 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65 74 43 struct OP_SetC
1c378 6f 6f 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 ookie_stack_vars
1c379 20 7b 0a 20 20 20 20 20 20 44 62 20 2a 70 44 62 {. Db *pDb
1c37a 3b 0a 20 20 20 20 7d 20 61 75 3b 0a 20 20 20 20 ;. } au;.
1c37b 73 74 72 75 63 74 20 4f 50 5f 56 65 72 69 66 79 struct OP_Verify
1c37c 43 6f 6f 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72 Cookie_stack_var
1c37d 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 4d s {. int iM
1c37e 65 74 61 3b 0a 20 20 20 20 20 20 42 74 72 65 65 eta;. Btree
1c37f 20 2a 70 42 74 3b 0a 20 20 20 20 7d 20 61 76 3b *pBt;. } av;
1c380 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4f . struct OP_O
1c381 70 65 6e 57 72 69 74 65 5f 73 74 61 63 6b 5f 76 penWrite_stack_v
1c382 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 ars {. int
1c383 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20 4b 65 nField;. Ke
1c384 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b yInfo *pKeyInfo;
1c385 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b 0a 20 . int p2;.
1c386 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 int iDb;.
1c387 20 20 20 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a int wrFlag;.
1c388 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 58 3b Btree *pX;
1c389 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f . VdbeCurso
1c38a 72 20 2a 70 43 75 72 3b 0a 20 20 20 20 20 20 44 r *pCur;. D
1c38b 62 20 2a 70 44 62 3b 0a 20 20 20 20 7d 20 61 77 b *pDb;. } aw
1c38c 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c38d 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 5f 73 74 OpenEphemeral_st
1c38e 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c38f 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 VdbeCursor *pCx
1c390 3b 0a 20 20 20 20 7d 20 61 78 3b 0a 20 20 20 20 ;. } ax;.
1c391 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e 50 73 struct OP_OpenPs
1c392 65 75 64 6f 5f 73 74 61 63 6b 5f 76 61 72 73 20 eudo_stack_vars
1c393 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 {. VdbeCurs
1c394 6f 72 20 2a 70 43 78 3b 0a 20 20 20 20 7d 20 61 or *pCx;. } a
1c395 79 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 y;. struct OP
1c396 5f 53 65 65 6b 47 74 5f 73 74 61 63 6b 5f 76 61 _SeekGt_stack_va
1c397 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 rs {. int r
1c398 65 73 3b 0a 20 20 20 20 20 20 69 6e 74 20 6f 63 es;. int oc
1c399 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 ;. VdbeCurs
1c39a 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 55 6e or *pC;. Un
1c39b 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a packedRecord r;.
1c39c 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65 6c 64 int nField
1c39d 3b 0a 20 20 20 20 20 20 69 36 34 20 69 4b 65 79 ;. i64 iKey
1c39e 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f ; /* The ro
1c39f 77 69 64 20 77 65 20 61 72 65 20 74 6f 20 73 65 wid we are to se
1c3a0 65 6b 20 74 6f 20 2a 2f 0a 20 20 20 20 7d 20 61 ek to */. } a
1c3a1 7a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 z;. struct OP
1c3a2 5f 53 65 65 6b 5f 73 74 61 63 6b 5f 76 61 72 73 _Seek_stack_vars
1c3a3 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 {. VdbeCur
1c3a4 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 sor *pC;. } b
1c3a5 61 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 a;. struct OP
1c3a6 5f 46 6f 75 6e 64 5f 73 74 61 63 6b 5f 76 61 72 _Found_stack_var
1c3a7 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 6c s {. int al
1c3a8 72 65 61 64 79 45 78 69 73 74 73 3b 0a 20 20 20 readyExists;.
1c3a9 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 VdbeCursor *p
1c3aa 43 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 C;. int res
1c3ab 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 ;. Unpacked
1c3ac 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b Record *pIdxKey;
1c3ad 0a 20 20 20 20 20 20 63 68 61 72 20 61 54 65 6d . char aTem
1c3ae 70 52 65 63 5b 52 4f 55 4e 44 38 28 73 69 7a 65 pRec[ROUND8(size
1c3af 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 of(UnpackedRecor
1c3b0 64 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d 65 6d d)) + sizeof(Mem
1c3b1 29 2a 33 20 2b 20 37 5d 3b 0a 20 20 20 20 7d 20 )*3 + 7];. }
1c3b2 62 62 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f bb;. struct O
1c3b3 50 5f 49 73 55 6e 69 71 75 65 5f 73 74 61 63 6b P_IsUnique_stack
1c3b4 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 75 31 _vars {. u1
1c3b5 36 20 69 69 3b 0a 20 20 20 20 20 20 56 64 62 65 6 ii;. Vdbe
1c3b6 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 20 Cursor *pCx;.
1c3b7 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 BtCursor *pCr
1c3b8 73 72 3b 0a 20 20 20 20 20 20 75 31 36 20 6e 46 sr;. u16 nF
1c3b9 69 65 6c 64 3b 0a 20 20 20 20 20 20 4d 65 6d 20 ield;. Mem
1c3ba 2a 61 4d 65 6d 3b 0a 20 20 20 20 20 20 55 6e 70 *aMem;. Unp
1c3bb 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 20 20 ackedRecord r;
1c3bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c3bd 2f 2a 20 42 2d 54 72 65 65 20 69 6e 64 65 78 20 /* B-Tree index
1c3be 73 65 61 72 63 68 20 6b 65 79 20 2a 2f 0a 20 20 search key */.
1c3bf 20 20 20 20 69 36 34 20 52 3b 20 20 20 20 20 20 i64 R;
1c3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c3c1 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 /* Rowid
1c3c2 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 stored in regist
1c3c3 65 72 20 50 33 20 2a 2f 0a 20 20 20 20 7d 20 62 er P3 */. } b
1c3c4 63 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 c;. struct OP
1c3c5 5f 4e 6f 74 45 78 69 73 74 73 5f 73 74 61 63 6b _NotExists_stack
1c3c6 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 _vars {. Vd
1c3c7 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
1c3c8 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 BtCursor *pC
1c3c9 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 rsr;. int r
1c3ca 65 73 3b 0a 20 20 20 20 20 20 75 36 34 20 69 4b es;. u64 iK
1c3cb 65 79 3b 0a 20 20 20 20 7d 20 62 64 3b 0a 20 20 ey;. } bd;.
1c3cc 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 65 77 52 struct OP_NewR
1c3cd 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 owid_stack_vars
1c3ce 7b 0a 20 20 20 20 20 20 69 36 34 20 76 3b 20 20 {. i64 v;
1c3cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1c3d0 2a 20 54 68 65 20 6e 65 77 20 72 6f 77 69 64 20 * The new rowid
1c3d1 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 */. VdbeCur
1c3d2 73 6f 72 20 2a 70 43 3b 20 20 20 20 20 20 20 20 sor *pC;
1c3d3 2f 2a 20 43 75 72 73 6f 72 20 6f 66 20 74 61 62 /* Cursor of tab
1c3d4 6c 65 20 74 6f 20 67 65 74 20 74 68 65 20 6e 65 le to get the ne
1c3d5 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20 20 20 w rowid */.
1c3d6 20 69 6e 74 20 72 65 73 3b 20 20 20 20 20 20 20 int res;
1c3d7 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c /* Resul
1c3d8 74 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 42 t of an sqlite3B
1c3d9 74 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a 20 20 treeLast() */.
1c3da 20 20 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 int cnt;
1c3db 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
1c3dc 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69 74 20 74 unter to limit t
1c3dd 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 61 he number of sea
1c3de 72 63 68 65 73 20 2a 2f 0a 20 20 20 20 20 20 4d rches */. M
1c3df 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 em *pMem;
1c3e0 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 /* Registe
1c3e1 72 20 68 6f 6c 64 69 6e 67 20 6c 61 72 67 65 73 r holding larges
1c3e2 74 20 72 6f 77 69 64 20 66 6f 72 20 41 55 54 4f t rowid for AUTO
1c3e3 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 20 INCREMENT */.
1c3e4 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 VdbeFrame *pF
1c3e5 72 61 6d 65 3b 20 20 20 20 20 2f 2a 20 52 6f 6f rame; /* Roo
1c3e6 74 20 66 72 61 6d 65 20 6f 66 20 56 44 42 45 20 t frame of VDBE
1c3e7 2a 2f 0a 20 20 20 20 7d 20 62 65 3b 0a 20 20 20 */. } be;.
1c3e8 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 73 65 72 struct OP_Inser
1c3e9 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 t_stack_vars {.
1c3ea 20 20 20 20 20 4d 65 6d 20 2a 70 44 61 74 61 3b Mem *pData;
1c3eb 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 /* MEM ce
1c3ec 6c 6c 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 ll holding data
1c3ed 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 for the record t
1c3ee 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f o be inserted */
1c3ef 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4b 65 79 . Mem *pKey
1c3f0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 ; /* MEM
1c3f1 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 6b 65 79 cell holding key
1c3f2 20 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 for the record
1c3f3 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 69 4b */. i64 iK
1c3f4 65 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ey; /* T
1c3f5 68 65 20 69 6e 74 65 67 65 72 20 52 4f 57 49 44 he integer ROWID
1c3f6 20 6f 72 20 6b 65 79 20 66 6f 72 20 74 68 65 20 or key for the
1c3f7 72 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 record to be ins
1c3f8 65 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 56 erted */. V
1c3f9 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20 dbeCursor *pC;
1c3fa 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f 20 74 61 /* Cursor to ta
1c3fb 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 69 ble into which i
1c3fc 6e 73 65 72 74 20 69 73 20 77 72 69 74 74 65 6e nsert is written
1c3fd 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 5a */. int nZ
1c3fe 65 72 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e ero; /* N
1c3ff 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 2d 62 79 umber of zero-by
1c400 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 2a 2f tes to append */
1c401 0a 20 20 20 20 20 20 69 6e 74 20 73 65 65 6b 52 . int seekR
1c402 65 73 75 6c 74 3b 20 20 20 2f 2a 20 52 65 73 75 esult; /* Resu
1c403 6c 74 20 6f 66 20 70 72 69 6f 72 20 73 65 65 6b lt of prior seek
1c404 20 6f 72 20 30 20 69 66 20 6e 6f 20 55 53 45 53 or 0 if no USES
1c405 45 45 4b 52 45 53 55 4c 54 20 66 6c 61 67 20 2a EEKRESULT flag *
1c406 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 /. const ch
1c407 61 72 20 2a 7a 44 62 3b 20 20 2f 2a 20 64 61 74 ar *zDb; /* dat
1c408 61 62 61 73 65 20 6e 61 6d 65 20 2d 20 75 73 65 abase name - use
1c409 64 20 62 79 20 74 68 65 20 75 70 64 61 74 65 20 d by the update
1c40a 68 6f 6f 6b 20 2a 2f 0a 20 20 20 20 20 20 63 6f hook */. co
1c40b 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 3b 20 nst char *zTbl;
1c40c 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2d 20 /* Table name -
1c40d 75 73 65 64 20 62 79 20 74 68 65 20 6f 70 64 61 used by the opda
1c40e 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 20 20 20 te hook */.
1c40f 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 int op;
1c410 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 66 6f 72 /* Opcode for
1c411 20 75 70 64 61 74 65 20 68 6f 6f 6b 3a 20 53 51 update hook: SQ
1c412 4c 49 54 45 5f 55 50 44 41 54 45 20 6f 72 20 53 LITE_UPDATE or S
1c413 51 4c 49 54 45 5f 49 4e 53 45 52 54 20 2a 2f 0a QLITE_INSERT */.
1c414 20 20 20 20 7d 20 62 66 3b 0a 20 20 20 20 73 74 } bf;. st
1c415 72 75 63 74 20 4f 50 5f 44 65 6c 65 74 65 5f 73 ruct OP_Delete_s
1c416 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c417 20 20 69 36 34 20 69 4b 65 79 3b 0a 20 20 20 20 i64 iKey;.
1c418 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1c419 3b 0a 20 20 20 20 7d 20 62 67 3b 0a 20 20 20 20 ;. } bg;.
1c41a 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 44 61 74 struct OP_RowDat
1c41b 61 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 a_stack_vars {.
1c41c 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 VdbeCursor
1c41d 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75 72 *pC;. BtCur
1c41e 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 sor *pCrsr;.
1c41f 20 20 75 33 32 20 6e 3b 0a 20 20 20 20 20 20 69 u32 n;. i
1c420 36 34 20 6e 36 34 3b 0a 20 20 20 20 7d 20 62 68 64 n64;. } bh
1c421 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c422 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61 72 73 Rowid_stack_vars
1c423 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 {. VdbeCur
1c424 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 sor *pC;. i
1c425 36 34 20 76 3b 0a 20 20 20 20 20 20 73 71 6c 69 64 v;. sqli
1c426 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b te3_vtab *pVtab;
1c427 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c . const sql
1c428 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f ite3_module *pMo
1c429 64 75 6c 65 3b 0a 20 20 20 20 7d 20 62 69 3b 0a dule;. } bi;.
1c42a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 75 struct OP_Nu
1c42b 6c 6c 52 6f 77 5f 73 74 61 63 6b 5f 76 61 72 73 llRow_stack_vars
1c42c 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 {. VdbeCur
1c42d 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 sor *pC;. } b
1c42e 6a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 j;. struct OP
1c42f 5f 4c 61 73 74 5f 73 74 61 63 6b 5f 76 61 72 73 _Last_stack_vars
1c430 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 {. VdbeCur
1c431 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 sor *pC;. B
1c432 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1c433 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 int res;.
1c434 20 20 20 7d 20 62 6b 3b 0a 20 20 20 20 73 74 72 } bk;. str
1c435 75 63 74 20 4f 50 5f 52 65 77 69 6e 64 5f 73 74 uct OP_Rewind_st
1c436 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c437 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1c438 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 . BtCursor
1c439 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e *pCrsr;. in
1c43a 74 20 72 65 73 3b 0a 20 20 20 20 7d 20 62 6c 3b t res;. } bl;
1c43b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e . struct OP_N
1c43c 65 78 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b ext_stack_vars {
1c43d 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f . VdbeCurso
1c43e 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 r *pC;. BtC
1c43f 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 ursor *pCrsr;.
1c440 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 int res;.
1c441 20 7d 20 62 6d 3b 0a 20 20 20 20 73 74 72 75 63 } bm;. struc
1c442 74 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 5f 73 t OP_IdxInsert_s
1c443 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c444 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1c445 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 ;. BtCursor
1c446 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 *pCrsr;. i
1c447 6e 74 20 6e 4b 65 79 3b 0a 20 20 20 20 20 20 63 nt nKey;. c
1c448 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4b 65 79 3b onst char *zKey;
1c449 0a 20 20 20 20 7d 20 62 6e 3b 0a 20 20 20 20 73 . } bn;. s
1c44a 74 72 75 63 74 20 4f 50 5f 49 64 78 44 65 6c 65 truct OP_IdxDele
1c44b 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a te_stack_vars {.
1c44c 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c44d 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75 *pC;. BtCu
1c44e 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 rsor *pCrsr;.
1c44f 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 int res;.
1c450 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
1c451 20 72 3b 0a 20 20 20 20 7d 20 62 6f 3b 0a 20 20 r;. } bo;.
1c452 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78 52 struct OP_IdxR
1c453 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 owid_stack_vars
1c454 7b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 {. BtCursor
1c455 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 56 *pCrsr;. V
1c456 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 dbeCursor *pC;.
1c457 20 20 20 20 20 69 36 34 20 72 6f 77 69 64 3b 0a i64 rowid;.
1c458 20 20 20 20 7d 20 62 70 3b 0a 20 20 20 20 73 74 } bp;. st
1c459 72 75 63 74 20 4f 50 5f 49 64 78 47 45 5f 73 74 ruct OP_IdxGE_st
1c45a 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c45b 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1c45c 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a . int res;.
1c45d 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 UnpackedRe
1c45e 63 6f 72 64 20 72 3b 0a 20 20 20 20 7d 20 62 71 cord r;. } bq
1c45f 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c460 44 65 73 74 72 6f 79 5f 73 74 61 63 6b 5f 76 61 Destroy_stack_va
1c461 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 rs {. int i
1c462 4d 6f 76 65 64 3b 0a 20 20 20 20 20 20 69 6e 74 Moved;. int
1c463 20 69 43 6e 74 3b 0a 20 20 20 20 20 20 56 64 62 iCnt;. Vdb
1c464 65 20 2a 70 56 64 62 65 3b 0a 20 20 20 20 20 20 e *pVdbe;.
1c465 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 7d 20 62 int iDb;. } b
1c466 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 r;. struct OP
1c467 5f 43 6c 65 61 72 5f 73 74 61 63 6b 5f 76 61 72 _Clear_stack_var
1c468 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43 s {. int nC
1c469 68 61 6e 67 65 3b 0a 20 20 20 20 7d 20 62 73 3b hange;. } bs;
1c46a 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 43 . struct OP_C
1c46b 72 65 61 74 65 54 61 62 6c 65 5f 73 74 61 63 6b reateTable_stack
1c46c 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e _vars {. in
1c46d 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 69 6e t pgno;. in
1c46e 74 20 66 6c 61 67 73 3b 0a 20 20 20 20 20 20 44 t flags;. D
1c46f 62 20 2a 70 44 62 3b 0a 20 20 20 20 7d 20 62 74 b *pDb;. } bt
1c470 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c471 50 61 72 73 65 53 63 68 65 6d 61 5f 73 74 61 63 ParseSchema_stac
1c472 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c473 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 20 63 6f nt iDb;. co
1c474 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 nst char *zMaste
1c475 72 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a r;. char *z
1c476 53 71 6c 3b 0a 20 20 20 20 20 20 49 6e 69 74 44 Sql;. InitD
1c477 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 20 20 ata initData;.
1c478 20 20 7d 20 62 75 3b 0a 20 20 20 20 73 74 72 75 } bu;. stru
1c479 63 74 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 ct OP_IntegrityC
1c47a 6b 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 k_stack_vars {.
1c47b 20 20 20 20 20 69 6e 74 20 6e 52 6f 6f 74 3b 20 int nRoot;
1c47c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1c47d 66 20 74 61 62 6c 65 73 20 74 6f 20 63 68 65 63 f tables to chec
1c47e 6b 2e 20 20 28 4e 75 6d 62 65 72 20 6f 66 20 72 k. (Number of r
1c47f 6f 6f 74 20 70 61 67 65 73 2e 29 20 2a 2f 0a 20 oot pages.) */.
1c480 20 20 20 20 20 69 6e 74 20 2a 61 52 6f 6f 74 3b int *aRoot;
1c481 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 /* Array of
1c482 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62 65 72 rootpage number
1c483 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74 6f 20 s for tables to
1c484 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20 be checked */.
1c485 20 20 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 int j;
1c486 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
1c487 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ter */. int
1c488 20 6e 45 72 72 3b 20 20 20 20 20 20 20 2f 2a 20 nErr; /*
1c489 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 Number of errors
1c48a 20 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20 20 20 reported */.
1c48b 20 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 char *z;
1c48c 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 /* Text of th
1c48d 65 20 65 72 72 6f 72 20 72 65 70 6f 72 74 20 2a e error report *
1c48e 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 6e 45 /. Mem *pnE
1c48f 72 72 3b 20 20 20 20 20 2f 2a 20 52 65 67 69 73 rr; /* Regis
1c490 74 65 72 20 6b 65 65 70 69 6e 67 20 74 72 61 63 ter keeping trac
1c491 6b 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61 k of errors rema
1c492 69 6e 69 6e 67 20 2a 2f 0a 20 20 20 20 7d 20 62 ining */. } b
1c493 76 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 v;. struct OP
1c494 5f 52 6f 77 53 65 74 41 64 64 5f 73 74 61 63 6b _RowSetAdd_stack
1c495 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 _vars {. Me
1c496 6d 20 2a 70 49 64 78 3b 0a 20 20 20 20 20 20 4d m *pIdx;. M
1c497 65 6d 20 2a 70 56 61 6c 3b 0a 20 20 20 20 7d 20 em *pVal;. }
1c498 62 77 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f bw;. struct O
1c499 50 5f 52 6f 77 53 65 74 52 65 61 64 5f 73 74 61 P_RowSetRead_sta
1c49a 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c49b 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20 20 20 20 Mem *pIdx;.
1c49c 20 69 36 34 20 76 61 6c 3b 0a 20 20 20 20 7d 20 i64 val;. }
1c49d 62 78 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f bx;. struct O
1c49e 50 5f 52 6f 77 53 65 74 54 65 73 74 5f 73 74 61 P_RowSetTest_sta
1c49f 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c4a0 69 6e 74 20 69 53 65 74 3b 0a 20 20 20 20 20 20 int iSet;.
1c4a1 69 6e 74 20 65 78 69 73 74 73 3b 0a 20 20 20 20 int exists;.
1c4a2 7d 20 62 79 3b 0a 20 20 20 20 73 74 72 75 63 74 } by;. struct
1c4a3 20 4f 50 5f 50 72 6f 67 72 61 6d 5f 73 74 61 63 OP_Program_stac
1c4a4 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c4a5 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 nt nMem;
1c4a6 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1c4a7 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 of memory regis
1c4a8 74 65 72 73 20 66 6f 72 20 73 75 62 2d 70 72 6f ters for sub-pro
1c4a9 67 72 61 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e gram */. in
1c4aa 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 t nByte;
1c4ab 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f /* Bytes o
1c4ac 66 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65 20 f runtime space
1c4ad 72 65 71 75 69 72 65 64 20 66 6f 72 20 73 75 62 required for sub
1c4ae 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20 20 -program */.
1c4af 20 20 4d 65 6d 20 2a 70 52 74 3b 20 20 20 20 20 Mem *pRt;
1c4b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
1c4b1 69 73 74 65 72 20 74 6f 20 61 6c 6c 6f 63 61 74 ister to allocat
1c4b2 65 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65 20 e runtime space
1c4b3 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d */. Mem *pM
1c4b4 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 em;
1c4b5 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72 /* Used to iter
1c4b6 61 74 65 20 74 68 72 6f 75 67 68 20 6d 65 6d 6f ate through memo
1c4b7 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 20 20 ry cells */.
1c4b8 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 20 20 20 20 Mem *pEnd;
1c4b9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 /* Las
1c4ba 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e t memory cell in
1c4bb 20 6e 65 77 20 61 72 72 61 79 20 2a 2f 0a 20 20 new array */.
1c4bc 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 VdbeFrame *p
1c4bd 46 72 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e Frame; /* N
1c4be 65 77 20 76 64 62 65 20 66 72 61 6d 65 20 74 6f ew vdbe frame to
1c4bf 20 65 78 65 63 75 74 65 20 69 6e 20 2a 2f 0a 20 execute in */.
1c4c0 20 20 20 20 20 53 75 62 50 72 6f 67 72 61 6d 20 SubProgram
1c4c1 2a 70 50 72 6f 67 72 61 6d 3b 20 20 20 2f 2a 20 *pProgram; /*
1c4c2 53 75 62 2d 70 72 6f 67 72 61 6d 20 74 6f 20 65 Sub-program to e
1c4c3 78 65 63 75 74 65 20 2a 2f 0a 20 20 20 20 20 20 xecute */.
1c4c4 76 6f 69 64 20 2a 74 3b 20 20 20 20 20 20 20 20 void *t;
1c4c5 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e /* Token
1c4c6 20 69 64 65 6e 74 69 66 79 69 6e 67 20 74 72 69 identifying tri
1c4c7 67 67 65 72 20 2a 2f 0a 20 20 20 20 7d 20 62 7a gger */. } bz
1c4c8 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c4c9 50 61 72 61 6d 5f 73 74 61 63 6b 5f 76 61 72 73 Param_stack_vars
1c4ca 20 7b 0a 20 20 20 20 20 20 56 64 62 65 46 72 61 {. VdbeFra
1c4cb 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20 20 20 me *pFrame;.
1c4cc 20 20 4d 65 6d 20 2a 70 49 6e 3b 0a 20 20 20 20 Mem *pIn;.
1c4cd 7d 20 63 61 3b 0a 20 20 20 20 73 74 72 75 63 74 } ca;. struct
1c4ce 20 4f 50 5f 4d 65 6d 4d 61 78 5f 73 74 61 63 6b OP_MemMax_stack
1c4cf 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 _vars {. Me
1c4d0 6d 20 2a 70 49 6e 31 3b 0a 20 20 20 20 20 20 56 m *pIn1;. V
1c4d1 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 dbeFrame *pFrame
1c4d2 3b 0a 20 20 20 20 7d 20 63 62 3b 0a 20 20 20 20 ;. } cb;.
1c4d3 73 74 72 75 63 74 20 4f 50 5f 41 67 67 53 74 65 struct OP_AggSte
1c4d4 70 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 p_stack_vars {.
1c4d5 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 int n;.
1c4d6 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 4d int i;. M
1c4d7 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20 20 20 em *pMem;.
1c4d8 4d 65 6d 20 2a 70 52 65 63 3b 0a 20 20 20 20 20 Mem *pRec;.
1c4d9 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1c4da 20 63 74 78 3b 0a 20 20 20 20 20 20 73 71 6c 69 ctx;. sqli
1c4db 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 te3_value **apVa
1c4dc 6c 3b 0a 20 20 20 20 7d 20 63 63 3b 0a 20 20 20 l;. } cc;.
1c4dd 20 73 74 72 75 63 74 20 4f 50 5f 41 67 67 46 69 struct OP_AggFi
1c4de 6e 61 6c 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b nal_stack_vars {
1c4df 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d . Mem *pMem
1c4e0 3b 0a 20 20 20 20 7d 20 63 64 3b 0a 20 20 20 20 ;. } cd;.
1c4e1 73 74 72 75 63 74 20 4f 50 5f 49 6e 63 72 56 61 struct OP_IncrVa
1c4e2 63 75 75 6d 5f 73 74 61 63 6b 5f 76 61 72 73 20 cuum_stack_vars
1c4e3 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 {. Btree *p
1c4e4 42 74 3b 0a 20 20 20 20 7d 20 63 65 3b 0a 20 20 Bt;. } ce;.
1c4e5 20 20 73 74 72 75 63 74 20 4f 50 5f 56 42 65 67 struct OP_VBeg
1c4e6 69 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a in_stack_vars {.
1c4e7 20 20 20 20 20 20 56 54 61 62 6c 65 20 2a 70 56 VTable *pV
1c4e8 54 61 62 3b 0a 20 20 20 20 7d 20 63 66 3b 0a 20 Tab;. } cf;.
1c4e9 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 4f 70 struct OP_VOp
1c4ea 65 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a en_stack_vars {.
1c4eb 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c4ec 20 2a 70 43 75 72 3b 0a 20 20 20 20 20 20 73 71 *pCur;. sq
1c4ed 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
1c4ee 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a r *pVtabCursor;.
1c4ef 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 sqlite3_vt
1c4f0 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 ab *pVtab;.
1c4f1 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 sqlite3_module
1c4f2 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 20 *pModule;. }
1c4f3 63 67 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f cg;. struct O
1c4f4 50 5f 56 46 69 6c 74 65 72 5f 73 74 61 63 6b 5f P_VFilter_stack_
1c4f5 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 vars {. int
1c4f6 20 6e 41 72 67 3b 0a 20 20 20 20 20 20 69 6e 74 nArg;. int
1c4f7 20 69 51 75 65 72 79 3b 0a 20 20 20 20 20 20 63 iQuery;. c
1c4f8 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 onst sqlite3_mod
1c4f9 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 ule *pModule;.
1c4fa 20 20 20 20 4d 65 6d 20 2a 70 51 75 65 72 79 3b Mem *pQuery;
1c4fb 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 41 72 67 . Mem *pArg
1c4fc 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 c;. sqlite3
1c4fd 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 _vtab_cursor *pV
1c4fe 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 tabCursor;.
1c4ff 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1c500 56 74 61 62 3b 0a 20 20 20 20 20 20 56 64 62 65 Vtab;. Vdbe
1c501 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 Cursor *pCur;.
1c502 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 int res;.
1c503 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
1c504 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 20 20 Mem **apArg;.
1c505 20 7d 20 63 68 3b 0a 20 20 20 20 73 74 72 75 63 } ch;. struc
1c506 74 20 4f 50 5f 56 43 6f 6c 75 6d 6e 5f 73 74 61 t OP_VColumn_sta
1c507 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c508 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
1c509 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 tab;. const
1c50a 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 sqlite3_module
1c50b 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20 *pModule;.
1c50c 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20 20 20 20 Mem *pDest;.
1c50d 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
1c50e 74 20 73 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 t sContext;.
1c50f 7d 20 63 69 3b 0a 20 20 20 20 73 74 72 75 63 74 } ci;. struct
1c510 20 4f 50 5f 56 4e 65 78 74 5f 73 74 61 63 6b 5f OP_VNext_stack_
1c511 76 61 72 73 20 7b 0a 20 20 20 20 20 20 73 71 6c vars {. sql
1c512 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
1c513 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71 ;. const sq
1c514 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d lite3_module *pM
1c515 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20 69 6e 74 odule;. int
1c516 20 72 65 73 3b 0a 20 20 20 20 20 20 56 64 62 65 res;. Vdbe
1c517 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 Cursor *pCur;.
1c518 20 20 7d 20 63 6a 3b 0a 20 20 20 20 73 74 72 75 } cj;. stru
1c519 63 74 20 4f 50 5f 56 52 65 6e 61 6d 65 5f 73 74 ct OP_VRename_st
1c51a 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c51b 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1c51c 56 74 61 62 3b 0a 20 20 20 20 20 20 4d 65 6d 20 Vtab;. Mem
1c51d 2a 70 4e 61 6d 65 3b 0a 20 20 20 20 7d 20 63 6b *pName;. } ck
1c51e 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c51f 56 55 70 64 61 74 65 5f 73 74 61 63 6b 5f 76 61 VUpdate_stack_va
1c520 72 73 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 rs {. sqlit
1c521 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a e3_vtab *pVtab;.
1c522 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 6f sqlite3_mo
1c523 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 dule *pModule;.
1c524 20 20 20 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 int nArg;.
1c525 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 int i;.
1c526 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 sqlite_int64 r
1c527 6f 77 69 64 3b 0a 20 20 20 20 20 20 4d 65 6d 20 owid;. Mem
1c528 2a 2a 61 70 41 72 67 3b 0a 20 20 20 20 20 20 4d **apArg;. M
1c529 65 6d 20 2a 70 58 3b 0a 20 20 20 20 7d 20 63 6c em *pX;. } cl
1c52a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c52b 50 61 67 65 63 6f 75 6e 74 5f 73 74 61 63 6b 5f Pagecount_stack_
1c52c 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 vars {. int
1c52d 20 70 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e p1;. int n
1c52e 50 61 67 65 3b 0a 20 20 20 20 20 20 50 61 67 65 Page;. Page
1c52f 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 7d r *pPager;. }
1c530 20 63 6d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 cm;. struct
1c531 4f 50 5f 54 72 61 63 65 5f 73 74 61 63 6b 5f 76 OP_Trace_stack_v
1c532 61 72 73 20 7b 0a 20 20 20 20 20 20 63 68 61 72 ars {. char
1c533 20 2a 7a 54 72 61 63 65 3b 0a 20 20 20 20 7d 20 *zTrace;. }
1c534 63 6e 3b 0a 20 20 7d 20 75 3b 0a 20 20 2f 2a 20 cn;. } u;. /*
1c535 45 6e 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c End automaticall
1c536 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 y generated code
1c537 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a . *************
1c538 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c539 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c53a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c53b 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 61 73 73 65 *******/.. asse
1c53c 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 rt( p->magic==VD
1c53d 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 3b 20 BE_MAGIC_RUN );
1c53e 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 /* sqlite3_step
1c53f 28 29 20 76 65 72 69 66 69 65 73 20 74 68 69 73 () verifies this
1c540 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 */. assert( db
1c541 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f ->magic==SQLITE_
1c542 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 20 MAGIC_BUSY );.
1c543 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 sqlite3VdbeMutex
1c544 41 72 72 61 79 45 6e 74 65 72 28 70 29 3b 0a 20 ArrayEnter(p);.
1c545 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 if( p->rc==SQLI
1c546 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 TE_NOMEM ){.
1c547 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 /* This happens
1c548 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e if a malloc() in
1c549 73 69 64 65 20 61 20 63 61 6c 6c 20 74 6f 20 73 side a call to s
1c54a 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
1c54b 78 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73 xt() or. ** s
1c54c 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
1c54d 78 74 31 36 28 29 20 66 61 69 6c 65 64 2e 20 20 xt16() failed.
1c54e 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d */. goto no_m
1c54f 65 6d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 em;. }. assert
1c550 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f ( p->rc==SQLITE_
1c551 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c OK || p->rc==SQL
1c552 49 54 45 5f 42 55 53 59 20 29 3b 0a 20 20 70 2d ITE_BUSY );. p-
1c553 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b >rc = SQLITE_OK;
1c554 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 78 . assert( p->ex
1c555 70 6c 61 69 6e 3d 3d 30 20 29 3b 0a 20 20 70 2d plain==0 );. p-
1c556 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 30 3b >pResultSet = 0;
1c557 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c . db->busyHandl
1c558 65 72 2e 6e 42 75 73 79 20 3d 20 30 3b 0a 20 20 er.nBusy = 0;.
1c559 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 CHECK_FOR_INTERR
1c55a 55 50 54 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 UPT;. sqlite3Vd
1c55b 62 65 49 4f 54 72 61 63 65 53 71 6c 28 70 29 3b beIOTraceSql(p);
1c55c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
1c55d 45 42 55 47 0a 20 20 73 71 6c 69 74 65 33 42 65 EBUG. sqlite3Be
1c55e 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
1c55f 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 63 3d 3d );. if( p->pc==
1c560 30 20 0a 20 20 20 26 26 20 28 28 70 2d 3e 64 62 0 . && ((p->db
1c561 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
1c562 5f 56 64 62 65 4c 69 73 74 69 6e 67 29 20 7c 7c _VdbeListing) ||
1c563 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c 20 fileExists(db,
1c564 22 76 64 62 65 5f 65 78 70 6c 61 69 6e 22 29 29 "vdbe_explain"))
1c565 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b . ){. int i;
1c566 0a 20 20 20 20 70 72 69 6e 74 66 28 22 56 44 42 . printf("VDB
1c567 45 20 50 72 6f 67 72 61 6d 20 4c 69 73 74 69 6e E Program Listin
1c568 67 3a 5c 6e 22 29 3b 0a 20 20 20 20 73 71 6c 69 g:\n");. sqli
1c569 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 te3VdbePrintSql(
1c56a 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b p);. for(i=0;
1c56b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b i<p->nOp; i++){
1c56c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1c56d 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f 75 74 bePrintOp(stdout
1c56e 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 , i, &p->aOp[i])
1c56f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
1c570 28 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c ( fileExists(db,
1c571 20 22 76 64 62 65 5f 74 72 61 63 65 22 29 20 29 "vdbe_trace") )
1c572 7b 0a 20 20 20 20 70 2d 3e 74 72 61 63 65 20 3d {. p->trace =
1c573 20 73 74 64 6f 75 74 3b 0a 20 20 7d 0a 20 20 73 stdout;. }. s
1c574 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
1c575 61 6c 6c 6f 63 28 29 3b 0a 23 65 6e 64 69 66 0a alloc();.#endif.
1c576 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70 63 3b 20 for(pc=p->pc;
1c577 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 70 rc==SQLITE_OK; p
1c578 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 c++){. assert
1c579 28 20 70 63 3e 3d 30 20 26 26 20 70 63 3c 70 2d ( pc>=0 && pc<p-
1c57a 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 69 66 28 20 >nOp );. if(
1c57b 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1c57c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1c57d 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 #ifdef VDBE_PROF
1c57e 49 4c 45 0a 20 20 20 20 6f 72 69 67 50 63 20 3d ILE. origPc =
1c57f 20 70 63 3b 0a 20 20 20 20 73 74 61 72 74 20 3d pc;. start =
1c580 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 sqlite3Hwtime()
1c581 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 4f 70 ;.#endif. pOp
1c582 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 63 5d 3b 0a = &p->aOp[pc];.
1c583 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c . /* Only all
1c584 6f 77 20 74 72 61 63 69 6e 67 20 69 66 20 53 51 ow tracing if SQ
1c585 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 65 LITE_DEBUG is de
1c586 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 fined.. */.#i
1c587 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1c588 47 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 G. if( p->tra
1c589 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ce ){. if(
1c58a 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 pc==0 ){.
1c58b 20 70 72 69 6e 74 66 28 22 56 44 42 45 20 45 78 printf("VDBE Ex
1c58c 65 63 75 74 69 6f 6e 20 54 72 61 63 65 3a 5c 6e ecution Trace:\n
1c58d 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ");. sqli
1c58e 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 te3VdbePrintSql(
1c58f 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 p);. }.
1c590 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 sqlite3VdbePri
1c591 6e 74 4f 70 28 70 2d 3e 74 72 61 63 65 2c 20 70 ntOp(p->trace, p
1c592 63 2c 20 70 4f 70 29 3b 0a 20 20 20 20 7d 0a 20 c, pOp);. }.
1c593 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 3d if( p->trace=
1c594 3d 30 20 26 26 20 70 63 3d 3d 30 20 29 7b 0a 20 =0 && pc==0 ){.
1c595 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 sqlite3Begi
1c596 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b nBenignMalloc();
1c597 0a 20 20 20 20 20 20 69 66 28 20 66 69 6c 65 45 . if( fileE
1c598 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f xists(db, "vdbe_
1c599 73 71 6c 74 72 61 63 65 22 29 20 29 7b 0a 20 20 sqltrace") ){.
1c59a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1c59b 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 ePrintSql(p);.
1c59c 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
1c59d 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
1c59e 6f 63 28 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 oc();. }.#end
1c59f 69 66 0a 20 20 20 20 20 20 0a 0a 20 20 20 20 2f if. .. /
1c5a0 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 * Check to see i
1c5a1 66 20 77 65 20 6e 65 65 64 20 74 6f 20 73 69 6d f we need to sim
1c5a2 75 6c 61 74 65 20 61 6e 20 69 6e 74 65 72 72 75 ulate an interru
1c5a3 70 74 2e 20 20 54 68 69 73 20 6f 6e 6c 79 20 68 pt. This only h
1c5a4 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 69 66 appens. ** if
1c5a5 20 77 65 20 68 61 76 65 20 61 20 73 70 65 63 69 we have a speci
1c5a6 61 6c 20 74 65 73 74 20 62 75 69 6c 64 2e 0a 20 al test build..
1c5a7 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c */.#ifdef SQL
1c5a8 49 54 45 5f 54 45 53 54 0a 20 20 20 20 69 66 28 ITE_TEST. if(
1c5a9 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
1c5aa 70 74 5f 63 6f 75 6e 74 3e 30 20 29 7b 0a 20 20 pt_count>0 ){.
1c5ab 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 sqlite3_inte
1c5ac 72 72 75 70 74 5f 63 6f 75 6e 74 2d 2d 3b 0a 20 rrupt_count--;.
1c5ad 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1c5ae 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 _interrupt_count
1c5af 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ==0 ){. s
1c5b0 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
1c5b1 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 (db);. }.
1c5b2 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e }.#endif..#ifn
1c5b3 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1c5b4 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 PROGRESS_CALLBAC
1c5b5 4b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68 K. /* Call th
1c5b6 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 e progress callb
1c5b7 61 63 6b 20 69 66 20 69 74 20 69 73 20 63 6f 6e ack if it is con
1c5b8 66 69 67 75 72 65 64 20 61 6e 64 20 74 68 65 20 figured and the
1c5b9 72 65 71 75 69 72 65 64 20 6e 75 6d 62 65 72 0a required number.
1c5ba 20 20 20 20 2a 2a 20 6f 66 20 56 44 42 45 20 6f ** of VDBE o
1c5bb 70 73 20 68 61 76 65 20 62 65 65 6e 20 65 78 65 ps have been exe
1c5bc 63 75 74 65 64 20 28 65 69 74 68 65 72 20 73 69 cuted (either si
1c5bd 6e 63 65 20 74 68 69 73 20 69 6e 76 6f 63 61 74 nce this invocat
1c5be 69 6f 6e 20 6f 66 0a 20 20 20 20 2a 2a 20 73 71 ion of. ** sq
1c5bf 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 20 lite3VdbeExec()
1c5c0 6f 72 20 73 69 6e 63 65 20 6c 61 73 74 20 74 69 or since last ti
1c5c1 6d 65 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 me the progress
1c5c2 63 61 6c 6c 62 61 63 6b 20 77 61 73 20 63 61 6c callback was cal
1c5c3 6c 65 64 29 2e 0a 20 20 20 20 2a 2a 20 49 66 20 led).. ** If
1c5c4 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c the progress cal
1c5c5 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f lback returns no
1c5c6 6e 2d 7a 65 72 6f 2c 20 65 78 69 74 20 74 68 65 n-zero, exit the
1c5c7 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1c5c8 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 61 20 72 with. ** a r
1c5c9 65 74 75 72 6e 20 63 6f 64 65 20 53 51 4c 49 54 eturn code SQLIT
1c5ca 45 5f 41 42 4f 52 54 2e 0a 20 20 20 20 2a 2f 0a E_ABORT.. */.
1c5cb 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 72 6f if( db->xPro
1c5cc 67 72 65 73 73 20 29 7b 0a 20 20 20 20 20 20 69 gress ){. i
1c5cd 66 28 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73 f( db->nProgress
1c5ce 4f 70 73 3d 3d 6e 50 72 6f 67 72 65 73 73 4f 70 Ops==nProgressOp
1c5cf 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 s ){. int
1c5d0 20 70 72 63 3b 0a 20 20 20 20 20 20 20 20 69 66 prc;. if
1c5d1 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1c5d2 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 ff(db) ) goto ab
1c5d3 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1c5d4 65 3b 0a 20 20 20 20 20 20 20 20 70 72 63 20 3d e;. prc =
1c5d5 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 28 64 62 db->xProgress(db
1c5d6 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67 29 3b ->pProgressArg);
1c5d7 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c . if( sql
1c5d8 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 ite3SafetyOn(db)
1c5d9 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1c5da 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 e_to_misuse;.
1c5db 20 20 20 20 20 69 66 28 20 70 72 63 21 3d 30 20 if( prc!=0
1c5dc 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
1c5dd 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 = SQLITE_INTERRU
1c5de 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f PT;. go
1c5df 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 to vdbe_error_ha
1c5e0 6c 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 lt;. }.
1c5e1 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f nProgressO
1c5e2 70 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a ps = 0;. }.
1c5e3 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f nProgressO
1c5e4 70 73 2b 2b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 ps++;. }.#end
1c5e5 69 66 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f if.. /* Do co
1c5e6 6d 6d 6f 6e 20 73 65 74 75 70 20 70 72 6f 63 65 mmon setup proce
1c5e7 73 73 69 6e 67 20 66 6f 72 20 61 6e 79 20 6f 70 ssing for any op
1c5e8 63 6f 64 65 20 74 68 61 74 20 69 73 20 6d 61 72 code that is mar
1c5e9 6b 65 64 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 ked. ** with
1c5ea 74 68 65 20 22 6f 75 74 32 2d 70 72 65 72 65 6c the "out2-prerel
1c5eb 65 61 73 65 22 20 74 61 67 2e 20 20 53 75 63 68 ease" tag. Such
1c5ec 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20 61 20 opcodes have a
1c5ed 73 69 6e 67 6c 65 0a 20 20 20 20 2a 2a 20 6f 75 single. ** ou
1c5ee 74 70 75 74 20 77 68 69 63 68 20 69 73 20 73 70 tput which is sp
1c5ef 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 50 ecified by the P
1c5f0 32 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 2 parameter. Th
1c5f1 65 20 50 32 20 72 65 67 69 73 74 65 72 0a 20 20 e P2 register.
1c5f2 20 20 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 ** is initiali
1c5f3 7a 65 64 20 74 6f 20 61 20 4e 55 4c 4c 2e 0a 20 zed to a NULL..
1c5f4 20 20 20 2a 2f 0a 20 20 20 20 6f 70 50 72 6f 70 */. opProp
1c5f5 65 72 74 79 20 3d 20 6f 70 63 6f 64 65 50 72 6f erty = opcodePro
1c5f6 70 65 72 74 79 5b 70 4f 70 2d 3e 6f 70 63 6f 64 perty[pOp->opcod
1c5f7 65 5d 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 50 e];. if( (opP
1c5f8 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f roperty & OPFLG_
1c5f9 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 29 OUT2_PRERELEASE)
1c5fa 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 !=0 ){. ass
1c5fb 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 ert( pOp->p2>0 )
1c5fc 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1c5fd 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p2<=p->nMem
1c5fe 20 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 20 3d );. pOut =
1c5ff 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1c600 32 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 2];. sqlite
1c601 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 3VdbeMemReleaseE
1c602 78 74 65 72 6e 61 6c 28 70 4f 75 74 29 3b 0a 20 xternal(pOut);.
1c603 20 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 pOut->flags
1c604 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 = MEM_Null;.
1c605 20 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 30 3b 0a pOut->n = 0;.
1c606 20 20 20 20 7d 65 6c 73 65 0a 20 0a 20 20 20 20 }else. .
1c607 2f 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74 /* Do common set
1c608 75 70 20 66 6f 72 20 6f 70 63 6f 64 65 73 20 6d up for opcodes m
1c609 61 72 6b 65 64 20 77 69 74 68 20 6f 6e 65 20 6f arked with one o
1c60a 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a f the following.
1c60b 20 20 20 20 2a 2a 20 63 6f 6d 62 69 6e 61 74 69 ** combinati
1c60c 6f 6e 73 20 6f 66 20 70 72 6f 70 65 72 74 69 65 ons of propertie
1c60d 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a s.. **. **
1c60e 20 20 20 20 20 20 20 20 20 20 20 69 6e 31 0a 20 in1.
1c60f 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **
1c610 69 6e 31 20 69 6e 32 0a 20 20 20 20 2a 2a 20 20 in1 in2. **
1c611 20 20 20 20 20 20 20 20 20 69 6e 31 20 69 6e 32 in1 in2
1c612 20 6f 75 74 33 0a 20 20 20 20 2a 2a 20 20 20 20 out3. **
1c613 20 20 20 20 20 20 20 69 6e 31 20 69 6e 33 0a 20 in1 in3.
1c614 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 56 61 72 **. ** Var
1c615 69 61 62 6c 65 73 20 70 49 6e 31 2c 20 70 49 6e iables pIn1, pIn
1c616 32 2c 20 61 6e 64 20 70 49 6e 33 20 61 72 65 20 2, and pIn3 are
1c617 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f made to point to
1c618 20 61 70 70 72 6f 70 72 69 61 74 65 0a 20 20 20 appropriate.
1c619 20 2a 2a 20 72 65 67 69 73 74 65 72 73 20 66 6f ** registers fo
1c61a 72 20 69 6e 70 75 74 73 2e 20 20 56 61 72 69 61 r inputs. Varia
1c61b 62 6c 65 20 70 4f 75 74 20 70 6f 69 6e 74 73 20 ble pOut points
1c61c 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 72 65 to the output re
1c61d 67 69 73 74 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 gister.. */.
1c61e 20 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 if( (opProper
1c61f 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 31 29 21 ty & OPFLG_IN1)!
1c620 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 =0 ){. asse
1c621 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b rt( pOp->p1>0 );
1c622 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1c623 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p1<=p->nMem
1c624 29 3b 0a 20 20 20 20 20 20 70 49 6e 31 20 3d 20 );. pIn1 =
1c625 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 &p->aMem[pOp->p1
1c626 5d 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 ];. REGISTE
1c627 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c R_TRACE(pOp->p1,
1c628 20 70 49 6e 31 29 3b 0a 20 20 20 20 20 20 69 66 pIn1);. if
1c629 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 ( (opProperty &
1c62a 4f 50 46 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b OPFLG_IN2)!=0 ){
1c62b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1c62c 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 pOp->p2>0 );.
1c62d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1c62e 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p2<=p->nMem )
1c62f 3b 0a 20 20 20 20 20 20 20 20 70 49 6e 32 20 3d ;. pIn2 =
1c630 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1c631 32 5d 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49 2];. REGI
1c632 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1c633 70 32 2c 20 70 49 6e 32 29 3b 0a 20 20 20 20 20 p2, pIn2);.
1c634 20 20 20 2f 2a 20 41 73 20 63 75 72 72 65 6e 74 /* As current
1c635 6c 79 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2c 20 ly implemented,
1c636 69 6e 32 20 69 6d 70 6c 69 65 73 20 6f 75 74 33 in2 implies out3
1c637 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 72 . There is no r
1c638 65 61 73 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a eason. **
1c639 20 77 68 79 20 74 68 69 73 20 68 61 73 20 74 6f why this has to
1c63a 20 62 65 2c 20 69 74 20 6a 75 73 74 20 77 6f 72 be, it just wor
1c63b 6b 65 64 20 6f 75 74 20 74 68 61 74 20 77 61 79 ked out that way
1c63c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 . */. ass
1c63d 65 72 74 28 20 28 6f 70 50 72 6f 70 65 72 74 79 ert( (opProperty
1c63e 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 29 21 3d & OPFLG_OUT3)!=
1c63f 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 0 );. ass
1c640 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 ert( pOp->p3>0 )
1c641 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
1c642 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d ( pOp->p3<=p->nM
1c643 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4f em );. pO
1c644 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ut = &p->aMem[pO
1c645 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 7d 65 p->p3];. }e
1c646 6c 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 lse if( (opPrope
1c647 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 33 29 rty & OPFLG_IN3)
1c648 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 !=0 ){. a
1c649 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 ssert( pOp->p3>0
1c64a 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 );. asse
1c64b 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e rt( pOp->p3<=p->
1c64c 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 nMem );.
1c64d 70 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pIn3 = &p->aMem[
1c64e 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 pOp->p3];.
1c64f 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1c650 28 70 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29 3b (pOp->p3, pIn3);
1c651 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
1c652 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 se if( (opProper
1c653 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29 21 ty & OPFLG_IN2)!
1c654 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 =0 ){. asse
1c655 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b rt( pOp->p2>0 );
1c656 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1c657 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p2<=p->nMem
1c658 29 3b 0a 20 20 20 20 20 20 70 49 6e 32 20 3d 20 );. pIn2 =
1c659 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 &p->aMem[pOp->p2
1c65a 5d 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 ];. REGISTE
1c65b 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c R_TRACE(pOp->p2,
1c65c 20 70 49 6e 32 29 3b 0a 20 20 20 20 7d 65 6c 73 pIn2);. }els
1c65d 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 e if( (opPropert
1c65e 79 20 26 20 4f 50 46 4c 47 5f 49 4e 33 29 21 3d y & OPFLG_IN3)!=
1c65f 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 0 ){. asser
1c660 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a t( pOp->p3>0 );.
1c661 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1c662 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p3<=p->nMem )
1c663 3b 0a 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26 ;. pIn3 = &
1c664 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
1c665 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 52 ;. REGISTER
1c666 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 _TRACE(pOp->p3,
1c667 70 49 6e 33 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 pIn3);. }..
1c668 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f switch( pOp->o
1c669 70 63 6f 64 65 20 29 7b 0a 0a 2f 2a 2a 2a 2a 2a pcode ){../*****
1c66a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c66b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c66c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c66d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c66e 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 57 68 61 74 ********.** What
1c66f 20 66 6f 6c 6c 6f 77 73 20 69 73 20 61 20 6d 61 follows is a ma
1c670 73 73 69 76 65 20 73 77 69 74 63 68 20 73 74 61 ssive switch sta
1c671 74 65 6d 65 6e 74 20 77 68 65 72 65 20 65 61 63 tement where eac
1c672 68 20 63 61 73 65 20 69 6d 70 6c 65 6d 65 6e 74 h case implement
1c673 73 20 61 0a 2a 2a 20 73 65 70 61 72 61 74 65 20 s a.** separate
1c674 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 74 instruction in t
1c675 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 he virtual machi
1c676 6e 65 2e 20 20 49 66 20 77 65 20 66 6f 6c 6c 6f ne. If we follo
1c677 77 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 69 w the usual.** i
1c678 6e 64 65 6e 74 61 74 69 6f 6e 20 63 6f 6e 76 65 ndentation conve
1c679 6e 74 69 6f 6e 73 2c 20 65 61 63 68 20 63 61 73 ntions, each cas
1c67a 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 64 65 e should be inde
1c67b 6e 74 65 64 20 62 79 20 36 20 73 70 61 63 65 73 nted by 6 spaces
1c67c 2e 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 69 . But.** that i
1c67d 73 20 61 20 6c 6f 74 20 6f 66 20 77 61 73 74 65 s a lot of waste
1c67e 64 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 6c d space on the l
1c67f 65 66 74 20 6d 61 72 67 69 6e 2e 20 20 53 6f 20 eft margin. So
1c680 74 68 65 20 63 6f 64 65 20 77 69 74 68 69 6e 0a the code within.
1c681 2a 2a 20 74 68 65 20 73 77 69 74 63 68 20 73 74 ** the switch st
1c682 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20 62 72 65 atement will bre
1c683 61 6b 20 77 69 74 68 20 63 6f 6e 76 65 6e 74 69 ak with conventi
1c684 6f 6e 20 61 6e 64 20 62 65 20 66 6c 75 73 68 2d on and be flush-
1c685 6c 65 66 74 2e 20 41 6e 6f 74 68 65 72 0a 2a 2a left. Another.**
1c686 20 62 69 67 20 63 6f 6d 6d 65 6e 74 20 28 73 69 big comment (si
1c687 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f 6e milar to this on
1c688 65 29 20 77 69 6c 6c 20 6d 61 72 6b 20 74 68 65 e) will mark the
1c689 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f point in the co
1c68a 64 65 20 77 68 65 72 65 0a 2a 2a 20 77 65 20 74 de where.** we t
1c68b 72 61 6e 73 69 74 69 6f 6e 20 62 61 63 6b 20 74 ransition back t
1c68c 6f 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 o normal indenta
1c68d 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tion..**.** The
1c68e 66 6f 72 6d 61 74 74 69 6e 67 20 6f 66 20 65 61 formatting of ea
1c68f 63 68 20 63 61 73 65 20 69 73 20 69 6d 70 6f 72 ch case is impor
1c690 74 61 6e 74 2e 20 20 54 68 65 20 6d 61 6b 65 66 tant. The makef
1c691 69 6c 65 20 66 6f 72 20 53 51 4c 69 74 65 0a 2a ile for SQLite.*
1c692 2a 20 67 65 6e 65 72 61 74 65 73 20 74 77 6f 20 * generates two
1c693 43 20 66 69 6c 65 73 20 22 6f 70 63 6f 64 65 73 C files "opcodes
1c694 2e 68 22 20 61 6e 64 20 22 6f 70 63 6f 64 65 73 .h" and "opcodes
1c695 2e 63 22 20 62 79 20 73 63 61 6e 6e 69 6e 67 20 .c" by scanning
1c696 74 68 69 73 0a 2a 2a 20 66 69 6c 65 20 6c 6f 6f this.** file loo
1c697 6b 69 6e 67 20 66 6f 72 20 6c 69 6e 65 73 20 74 king for lines t
1c698 68 61 74 20 62 65 67 69 6e 20 77 69 74 68 20 22 hat begin with "
1c699 63 61 73 65 20 4f 50 5f 22 2e 20 20 54 68 65 20 case OP_". The
1c69a 6f 70 63 6f 64 65 73 2e 68 20 66 69 6c 65 73 0a opcodes.h files.
1c69b 2a 2a 20 77 69 6c 6c 20 62 65 20 66 69 6c 6c 65 ** will be fille
1c69c 64 20 77 69 74 68 20 23 64 65 66 69 6e 65 73 20 d with #defines
1c69d 74 68 61 74 20 67 69 76 65 20 75 6e 69 71 75 65 that give unique
1c69e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 integer values
1c69f 74 6f 20 65 61 63 68 0a 2a 2a 20 6f 70 63 6f 64 to each.** opcod
1c6a0 65 20 61 6e 64 20 74 68 65 20 6f 70 63 6f 64 65 e and the opcode
1c6a1 73 2e 63 20 66 69 6c 65 20 69 73 20 66 69 6c 6c s.c file is fill
1c6a2 65 64 20 77 69 74 68 20 61 6e 20 61 72 72 61 79 ed with an array
1c6a3 20 6f 66 20 73 74 72 69 6e 67 73 20 77 68 65 72 of strings wher
1c6a4 65 0a 2a 2a 20 65 61 63 68 20 73 74 72 69 6e 67 e.** each string
1c6a5 20 69 73 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 is the symbolic
1c6a6 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63 6f name for the co
1c6a7 72 72 65 73 70 6f 6e 64 69 6e 67 20 6f 70 63 6f rresponding opco
1c6a8 64 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 63 de. If the.** c
1c6a9 61 73 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 ase statement is
1c6aa 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 63 followed by a c
1c6ab 6f 6d 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f omment of the fo
1c6ac 72 6d 20 22 2f 23 20 73 61 6d 65 20 61 73 20 2e rm "/# same as .
1c6ad 2e 2e 20 23 2f 22 0a 2a 2a 20 74 68 61 74 20 63 .. #/".** that c
1c6ae 6f 6d 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74 omment is used t
1c6af 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
1c6b0 70 61 72 74 69 63 75 6c 61 72 20 76 61 6c 75 65 particular value
1c6b1 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e 0a of the opcode..
1c6b2 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20 6b 65 79 77 **.** Other keyw
1c6b3 6f 72 64 73 20 69 6e 20 74 68 65 20 63 6f 6d 6d ords in the comm
1c6b4 65 6e 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 ent that follows
1c6b5 20 65 61 63 68 20 63 61 73 65 20 61 72 65 20 75 each case are u
1c6b6 73 65 64 20 74 6f 0a 2a 2a 20 63 6f 6e 73 74 72 sed to.** constr
1c6b7 75 63 74 20 74 68 65 20 4f 50 46 4c 47 5f 49 4e uct the OPFLG_IN
1c6b8 49 54 49 41 4c 49 5a 45 52 20 76 61 6c 75 65 20 ITIALIZER value
1c6b9 74 68 61 74 20 69 6e 69 74 69 61 6c 69 7a 65 73 that initializes
1c6ba 20 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b opcodeProperty[
1c6bb 5d 2e 0a 2a 2a 20 4b 65 79 77 6f 72 64 73 20 69 ]..** Keywords i
1c6bc 6e 63 6c 75 64 65 3a 20 69 6e 31 2c 20 69 6e 32 nclude: in1, in2
1c6bd 2c 20 69 6e 33 2c 20 6f 75 74 32 5f 70 72 65 72 , in3, out2_prer
1c6be 65 6c 65 61 73 65 2c 20 6f 75 74 32 2c 20 6f 75 elease, out2, ou
1c6bf 74 33 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65 20 t3. See.** the
1c6c0 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 63 mkopcodeh.awk sc
1c6c1 72 69 70 74 20 66 6f 72 20 61 64 64 69 74 69 6f ript for additio
1c6c2 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
1c6c3 0a 2a 2a 0a 2a 2a 20 44 6f 63 75 6d 65 6e 74 61 .**.** Documenta
1c6c4 74 69 6f 6e 20 61 62 6f 75 74 20 56 44 42 45 20 tion about VDBE
1c6c5 6f 70 63 6f 64 65 73 20 69 73 20 67 65 6e 65 72 opcodes is gener
1c6c6 61 74 65 64 20 62 79 20 73 63 61 6e 6e 69 6e 67 ated by scanning
1c6c7 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 66 6f this file.** fo
1c6c8 72 20 6c 69 6e 65 73 20 6f 66 20 74 68 61 74 20 r lines of that
1c6c9 63 6f 6e 74 61 69 6e 20 22 4f 70 63 6f 64 65 3a contain "Opcode:
1c6ca 22 2e 20 20 54 68 61 74 20 6c 69 6e 65 20 61 6e ". That line an
1c6cb 64 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 d all subsequent
1c6cc 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 6c 69 6e 65 .** comment line
1c6cd 73 20 61 72 65 20 75 73 65 64 20 69 6e 20 74 68 s are used in th
1c6ce 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 e generation of
1c6cf 74 68 65 20 6f 70 63 6f 64 65 2e 68 74 6d 6c 20 the opcode.html
1c6d0 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a documentation.**
1c6d1 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 55 4d file..**.** SUM
1c6d2 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 MARY:.**.**
1c6d3 46 6f 72 6d 61 74 74 69 6e 67 20 69 73 20 69 6d Formatting is im
1c6d4 70 6f 72 74 61 6e 74 20 74 6f 20 73 63 72 69 70 portant to scrip
1c6d5 74 73 20 74 68 61 74 20 73 63 61 6e 20 74 68 69 ts that scan thi
1c6d6 73 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20 20 44 s file..** D
1c6d7 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66 72 o not deviate fr
1c6d8 6f 6d 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e om the formattin
1c6d9 67 20 73 74 79 6c 65 20 63 75 72 72 65 6e 74 6c g style currentl
1c6da 79 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 2a y in use..**.***
1c6db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c6dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c6dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c6de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c6df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 **********/../*
1c6e0 4f 70 63 6f 64 65 3a 20 20 47 6f 74 6f 20 2a 20 Opcode: Goto *
1c6e1 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 P2 * * *.**.** A
1c6e2 6e 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 n unconditional
1c6e3 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20 jump to address
1c6e4 50 32 2e 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 P2..** The next
1c6e5 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 78 65 63 instruction exec
1c6e6 75 74 65 64 20 77 69 6c 6c 20 62 65 20 0a 2a 2a uted will be .**
1c6e7 20 74 68 65 20 6f 6e 65 20 61 74 20 69 6e 64 65 the one at inde
1c6e8 78 20 50 32 20 66 72 6f 6d 20 74 68 65 20 62 65 x P2 from the be
1c6e9 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 ginning of.** th
1c6ea 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 63 61 e program..*/.ca
1c6eb 73 65 20 4f 50 5f 47 6f 74 6f 3a 20 7b 20 20 20 se OP_Goto: {
1c6ec 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d /* jum
1c6ed 70 20 2a 2f 0a 20 20 43 48 45 43 4b 5f 46 4f 52 p */. CHECK_FOR
1c6ee 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 63 _INTERRUPT;. pc
1c6ef 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1c6f0 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1c6f1 70 63 6f 64 65 3a 20 20 47 6f 73 75 62 20 50 31 pcode: Gosub P1
1c6f2 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1c6f3 57 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e Write the curren
1c6f4 74 20 61 64 64 72 65 73 73 20 6f 6e 74 6f 20 72 t address onto r
1c6f5 65 67 69 73 74 65 72 20 50 31 0a 2a 2a 20 61 6e egister P1.** an
1c6f6 64 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 61 d then jump to a
1c6f7 64 64 72 65 73 73 20 50 32 2e 0a 2a 2f 0a 63 61 ddress P2..*/.ca
1c6f8 73 65 20 4f 50 5f 47 6f 73 75 62 3a 20 7b 20 20 se OP_Gosub: {
1c6f9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d /* jum
1c6fa 70 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 p */. assert( p
1c6fb 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 Op->p1>0 );. as
1c6fc 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 sert( pOp->p1<=p
1c6fd 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 6e 31 ->nMem );. pIn1
1c6fe 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1c6ff 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p1];. assert(
1c700 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d (pIn1->flags & M
1c701 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 EM_Dyn)==0 );.
1c702 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pIn1->flags = ME
1c703 4d 5f 49 6e 74 3b 0a 20 20 70 49 6e 31 2d 3e 75 M_Int;. pIn1->u
1c704 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 49 53 .i = pc;. REGIS
1c705 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1c706 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 20 3d 1, pIn1);. pc =
1c707 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1c708 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1c709 6f 64 65 3a 20 20 52 65 74 75 72 6e 20 50 31 20 ode: Return P1
1c70a 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 * * * *.**.** Ju
1c70b 6d 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 mp to the next i
1c70c 6e 73 74 72 75 63 74 69 6f 6e 20 61 66 74 65 72 nstruction after
1c70d 20 74 68 65 20 61 64 64 72 65 73 73 20 69 6e 20 the address in
1c70e 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f 0a register P1..*/.
1c70f 63 61 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a 20 case OP_Return:
1c710 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 { /* i
1c711 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 n1 */. assert(
1c712 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1c713 4d 5f 49 6e 74 20 29 3b 0a 20 20 70 63 20 3d 20 M_Int );. pc =
1c714 28 69 6e 74 29 70 49 6e 31 2d 3e 75 2e 69 3b 0a (int)pIn1->u.i;.
1c715 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1c716 70 63 6f 64 65 3a 20 20 59 69 65 6c 64 20 50 31 pcode: Yield P1
1c717 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 * * * *.**.** S
1c718 77 61 70 20 74 68 65 20 70 72 6f 67 72 61 6d 20 wap the program
1c719 63 6f 75 6e 74 65 72 20 77 69 74 68 20 74 68 65 counter with the
1c71a 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1c71b 65 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f er P1..*/.case O
1c71c 50 5f 59 69 65 6c 64 3a 20 7b 20 20 20 20 20 20 P_Yield: {
1c71d 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a /* in1 */.
1c71e 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1c71f 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1c720 69 6e 74 6f 20 75 2e 61 61 20 2a 2f 0a 20 20 69 into u.aa */. i
1c721 6e 74 20 70 63 44 65 73 74 3b 0a 23 65 6e 64 69 nt pcDest;.#endi
1c722 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1c723 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1c724 75 2e 61 61 20 2a 2f 0a 20 20 61 73 73 65 72 74 u.aa */. assert
1c725 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 ( (pIn1->flags &
1c726 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a MEM_Dyn)==0 );.
1c727 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 pIn1->flags =
1c728 4d 45 4d 5f 49 6e 74 3b 0a 20 20 75 2e 61 61 2e MEM_Int;. u.aa.
1c729 70 63 44 65 73 74 20 3d 20 28 69 6e 74 29 70 49 pcDest = (int)pI
1c72a 6e 31 2d 3e 75 2e 69 3b 0a 20 20 70 49 6e 31 2d n1->u.i;. pIn1-
1c72b 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 >u.i = pc;. REG
1c72c 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1c72d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 >p1, pIn1);. pc
1c72e 20 3d 20 75 2e 61 61 2e 70 63 44 65 73 74 3b 0a = u.aa.pcDest;.
1c72f 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1c730 70 63 6f 64 65 3a 20 20 48 61 6c 74 49 66 4e 75 pcode: HaltIfNu
1c731 6c 6c 20 20 50 31 20 50 32 20 50 33 20 50 34 20 ll P1 P2 P3 P4
1c732 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 *.**.** Check th
1c733 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1c734 74 65 72 20 50 33 2e 20 20 49 66 20 69 73 20 69 ter P3. If is i
1c735 73 20 4e 55 4c 4c 20 74 68 65 6e 20 48 61 6c 74 s NULL then Halt
1c736 20 75 73 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65 using.** parame
1c737 74 65 72 20 50 31 2c 20 50 32 2c 20 61 6e 64 20 ter P1, P2, and
1c738 50 34 20 61 73 20 69 66 20 74 68 69 73 20 77 65 P4 as if this we
1c739 72 65 20 61 20 48 61 6c 74 20 69 6e 73 74 72 75 re a Halt instru
1c73a 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 0a 2a ction. If the.*
1c73b 2a 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 * value in regis
1c73c 74 65 72 20 50 33 20 69 73 20 6e 6f 74 20 4e 55 ter P3 is not NU
1c73d 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f LL, then this ro
1c73e 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 utine is a no-op
1c73f 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c ..*/.case OP_Hal
1c740 74 49 66 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 tIfNull: {
1c741 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 69 66 28 20 /* in3 */. if(
1c742 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d (pIn3->flags & M
1c743 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 20 62 72 EM_Null)==0 ) br
1c744 65 61 6b 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 eak;. /* Fall t
1c745 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50 5f 48 hrough into OP_H
1c746 61 6c 74 20 2a 2f 0a 7d 0a 0a 2f 2a 20 4f 70 63 alt */.}../* Opc
1c747 6f 64 65 3a 20 20 48 61 6c 74 20 50 31 20 50 32 ode: Halt P1 P2
1c748 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 * P4 *.**.** Ex
1c749 69 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 it immediately.
1c74a 20 41 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72 All open cursor
1c74b 73 2c 20 65 74 63 20 61 72 65 20 63 6c 6f 73 65 s, etc are close
1c74c 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c d.** automatical
1c74d 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 ly..**.** P1 is
1c74e 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65 20 the result code
1c74f 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 returned by sqli
1c750 74 65 33 5f 65 78 65 63 28 29 2c 20 73 71 6c 69 te3_exec(), sqli
1c751 74 65 33 5f 72 65 73 65 74 28 29 2c 0a 2a 2a 20 te3_reset(),.**
1c752 6f 72 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c or sqlite3_final
1c753 69 7a 65 28 29 2e 20 20 46 6f 72 20 61 20 6e 6f ize(). For a no
1c754 72 6d 61 6c 20 68 61 6c 74 2c 20 74 68 69 73 20 rmal halt, this
1c755 73 68 6f 75 6c 64 20 62 65 20 53 51 4c 49 54 45 should be SQLITE
1c756 5f 4f 4b 20 28 30 29 2e 0a 2a 2a 20 46 6f 72 20 _OK (0)..** For
1c757 65 72 72 6f 72 73 2c 20 69 74 20 63 61 6e 20 62 errors, it can b
1c758 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 76 61 6c e some other val
1c759 75 65 2e 20 20 49 66 20 50 31 21 3d 30 20 74 68 ue. If P1!=0 th
1c75a 65 6e 20 50 32 20 77 69 6c 6c 20 64 65 74 65 72 en P2 will deter
1c75b 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65 72 20 mine.** whether
1c75c 6f 72 20 6e 6f 74 20 74 6f 20 72 6f 6c 6c 62 61 or not to rollba
1c75d 63 6b 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 ck the current t
1c75e 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 44 6f 20 ransaction. Do
1c75f 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 not rollback.**
1c760 69 66 20 50 32 3d 3d 4f 45 5f 46 61 69 6c 2e 20 if P2==OE_Fail.
1c761 44 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 Do the rollback
1c762 69 66 20 50 32 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 if P2==OE_Rollba
1c763 63 6b 2e 20 20 49 66 20 50 32 3d 3d 4f 45 5f 41 ck. If P2==OE_A
1c764 62 6f 72 74 2c 0a 2a 2a 20 74 68 65 6e 20 62 61 bort,.** then ba
1c765 63 6b 20 6f 75 74 20 61 6c 6c 20 63 68 61 6e 67 ck out all chang
1c766 65 73 20 74 68 61 74 20 68 61 76 65 20 6f 63 63 es that have occ
1c767 75 72 72 65 64 20 64 75 72 69 6e 67 20 74 68 69 urred during thi
1c768 73 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 s execution of t
1c769 68 65 0a 2a 2a 20 56 44 42 45 2c 20 62 75 74 20 he.** VDBE, but
1c76a 64 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 do not rollback
1c76b 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e the transaction.
1c76c 20 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 .**.** If P4 is
1c76d 20 6e 6f 74 20 6e 75 6c 6c 20 74 68 65 6e 20 69 not null then i
1c76e 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 t is an error me
1c76f 73 73 61 67 65 20 73 74 72 69 6e 67 2e 0a 2a 2a ssage string..**
1c770 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 6e 20 .** There is an
1c771 69 6d 70 6c 69 65 64 20 22 48 61 6c 74 20 30 20 implied "Halt 0
1c772 30 20 30 22 20 69 6e 73 74 72 75 63 74 69 6f 6e 0 0" instruction
1c773 20 69 6e 73 65 72 74 65 64 20 61 74 20 74 68 65 inserted at the
1c774 20 76 65 72 79 20 65 6e 64 20 6f 66 0a 2a 2a 20 very end of.**
1c775 65 76 65 72 79 20 70 72 6f 67 72 61 6d 2e 20 20 every program.
1c776 53 6f 20 61 20 6a 75 6d 70 20 70 61 73 74 20 74 So a jump past t
1c777 68 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74 he last instruct
1c778 69 6f 6e 20 6f 66 20 74 68 65 20 70 72 6f 67 72 ion of the progr
1c779 61 6d 0a 2a 2a 20 69 73 20 74 68 65 20 73 61 6d am.** is the sam
1c77a 65 20 61 73 20 65 78 65 63 75 74 69 6e 67 20 48 e as executing H
1c77b 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f alt..*/.case OP_
1c77c 48 61 6c 74 3a 20 7b 0a 20 20 69 66 28 20 70 4f Halt: {. if( pO
1c77d 70 2d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 4f 4b p->p1==SQLITE_OK
1c77e 20 26 26 20 70 2d 3e 70 46 72 61 6d 65 20 29 7b && p->pFrame ){
1c77f 0a 20 20 20 20 2f 2a 20 48 61 6c 74 20 74 68 65 . /* Halt the
1c780 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20 52 65 sub-program. Re
1c781 74 75 72 6e 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 turn control to
1c782 74 68 65 20 70 61 72 65 6e 74 20 66 72 61 6d 65 the parent frame
1c783 2e 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 72 61 . */. VdbeFra
1c784 6d 65 20 2a 70 46 72 61 6d 65 20 3d 20 70 2d 3e me *pFrame = p->
1c785 70 46 72 61 6d 65 3b 0a 20 20 20 20 70 2d 3e 70 pFrame;. p->p
1c786 46 72 61 6d 65 20 3d 20 70 46 72 61 6d 65 2d 3e Frame = pFrame->
1c787 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 70 2d 3e pParent;. p->
1c788 6e 46 72 61 6d 65 2d 2d 3b 0a 20 20 20 20 73 71 nFrame--;. sq
1c789 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e lite3VdbeSetChan
1c78a 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e ges(db, p->nChan
1c78b 67 65 29 3b 0a 20 20 20 20 70 63 20 3d 20 73 71 ge);. pc = sq
1c78c 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 52 65 lite3VdbeFrameRe
1c78d 73 74 6f 72 65 28 70 46 72 61 6d 65 29 3b 0a 20 store(pFrame);.
1c78e 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d if( pOp->p2==
1c78f 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20 20 OE_Ignore ){.
1c790 20 20 20 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f /* Instructio
1c791 6e 20 70 63 20 69 73 20 74 68 65 20 4f 50 5f 50 n pc is the OP_P
1c792 72 6f 67 72 61 6d 20 74 68 61 74 20 69 6e 76 6f rogram that invo
1c793 6b 65 64 20 74 68 65 20 73 75 62 2d 70 72 6f 67 ked the sub-prog
1c794 72 61 6d 20 0a 20 20 20 20 20 20 2a 2a 20 63 75 ram . ** cu
1c795 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 68 61 rrently being ha
1c796 6c 74 65 64 2e 20 49 66 20 74 68 65 20 70 32 20 lted. If the p2
1c797 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 instruction of t
1c798 68 69 73 20 4f 50 5f 48 61 6c 74 0a 20 20 20 20 his OP_Halt.
1c799 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e ** instruction
1c79a 20 69 73 20 73 65 74 20 74 6f 20 4f 45 5f 49 67 is set to OE_Ig
1c79b 6e 6f 72 65 2c 20 74 68 65 6e 20 74 68 65 20 73 nore, then the s
1c79c 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 74 68 ub-program is th
1c79d 72 6f 77 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 rowing. **
1c79e 61 6e 20 49 47 4e 4f 52 45 20 65 78 63 65 70 74 an IGNORE except
1c79f 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ion. In this cas
1c7a0 65 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 61 64 e jump to the ad
1c7a1 64 72 65 73 73 20 73 70 65 63 69 66 69 65 64 0a dress specified.
1c7a2 20 20 20 20 20 20 2a 2a 20 61 73 20 74 68 65 20 ** as the
1c7a3 70 32 20 6f 66 20 74 68 65 20 63 61 6c 6c 69 6e p2 of the callin
1c7a4 67 20 4f 50 5f 50 72 6f 67 72 61 6d 2e 20 20 2a g OP_Program. *
1c7a5 2f 0a 20 20 20 20 20 20 70 63 20 3d 20 70 2d 3e /. pc = p->
1c7a6 61 4f 70 5b 70 63 5d 2e 70 32 2d 31 3b 0a 20 20 aOp[pc].p2-1;.
1c7a7 20 20 7d 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
1c7a8 20 7d 0a 0a 20 20 70 2d 3e 72 63 20 3d 20 70 4f }.. p->rc = pO
1c7a9 70 2d 3e 70 31 3b 0a 20 20 70 2d 3e 65 72 72 6f p->p1;. p->erro
1c7aa 72 41 63 74 69 6f 6e 20 3d 20 28 75 38 29 70 4f rAction = (u8)pO
1c7ab 70 2d 3e 70 32 3b 0a 20 20 70 2d 3e 70 63 20 3d p->p2;. p->pc =
1c7ac 20 70 63 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e pc;. if( pOp->
1c7ad 70 34 2e 7a 20 29 7b 0a 20 20 20 20 73 71 6c 69 p4.z ){. sqli
1c7ae 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
1c7af 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 >zErrMsg, db, "%
1c7b0 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a s", pOp->p4.z);.
1c7b1 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
1c7b2 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 e3VdbeHalt(p);.
1c7b3 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
1c7b4 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d ITE_BUSY || rc==
1c7b5 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 SQLITE_OK );. i
1c7b6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 f( rc==SQLITE_BU
1c7b7 53 59 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 SY ){. p->rc
1c7b8 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 = rc = SQLITE_BU
1c7b9 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 SY;. }else{.
1c7ba 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f 20 53 51 rc = p->rc ? SQ
1c7bb 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 53 51 4c LITE_ERROR : SQL
1c7bc 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 ITE_DONE;. }.
1c7bd 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e goto vdbe_return
1c7be 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1c7bf 49 6e 74 65 67 65 72 20 50 31 20 50 32 20 2a 20 Integer P1 P2 *
1c7c0 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 33 32 * *.**.** The 32
1c7c1 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c -bit integer val
1c7c2 75 65 20 50 31 20 69 73 20 77 72 69 74 74 65 6e ue P1 is written
1c7c3 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 into register P
1c7c4 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 2..*/.case OP_In
1c7c5 74 65 67 65 72 3a 20 7b 20 20 20 20 20 20 20 20 teger: {
1c7c6 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 /* out2-prerele
1c7c7 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e 66 ase */. pOut->f
1c7c8 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
1c7c9 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 4f pOut->u.i = pO
1c7ca 70 2d 3e 70 31 3b 0a 20 20 62 72 65 61 6b 3b 0a p->p1;. break;.
1c7cb 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e }../* Opcode: In
1c7cc 74 36 34 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a t64 * P2 * P4 *.
1c7cd 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f **.** P4 is a po
1c7ce 69 6e 74 65 72 20 74 6f 20 61 20 36 34 2d 62 69 inter to a 64-bi
1c7cf 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e t integer value.
1c7d0 0a 2a 2a 20 57 72 69 74 65 20 74 68 61 74 20 76 .** Write that v
1c7d1 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 alue into regist
1c7d2 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f er P2..*/.case O
1c7d3 50 5f 49 6e 74 36 34 3a 20 7b 20 20 20 20 20 20 P_Int64: {
1c7d4 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 /* out2-pre
1c7d5 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 release */. ass
1c7d6 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 70 49 36 ert( pOp->p4.pI6
1c7d7 34 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 4!=0 );. pOut->
1c7d8 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
1c7d9 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 2a . pOut->u.i = *
1c7da 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b 0a 20 20 pOp->p4.pI64;.
1c7db 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1c7dc 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50 32 20 2a ode: Real * P2 *
1c7dd 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 P4 *.**.** P4 i
1c7de 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
1c7df 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 64-bit floating
1c7e0 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a point value..**
1c7e1 20 57 72 69 74 65 20 74 68 61 74 20 76 61 6c 75 Write that valu
1c7e2 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 e into register
1c7e3 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 P2..*/.case OP_R
1c7e4 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 eal: {
1c7e5 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1c7e6 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70 72 65 72 FLOAT, out2-prer
1c7e7 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 elease */. pOut
1c7e8 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65 ->flags = MEM_Re
1c7e9 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 21 73 al;. assert( !s
1c7ea 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a 70 4f 70 qlite3IsNaN(*pOp
1c7eb 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29 3b 0a 20 ->p4.pReal) );.
1c7ec 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70 4f 70 2d pOut->r = *pOp-
1c7ed 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20 62 72 65 >p4.pReal;. bre
1c7ee 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1c7ef 3a 20 53 74 72 69 6e 67 38 20 2a 20 50 32 20 2a : String8 * P2 *
1c7f0 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 70 P4 *.**.** P4 p
1c7f1 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 20 74 oints to a nul t
1c7f2 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 erminated UTF-8
1c7f3 73 74 72 69 6e 67 2e 20 54 68 69 73 20 6f 70 63 string. This opc
1c7f4 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72 6d ode is transform
1c7f5 65 64 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 4f ed .** into an O
1c7f6 50 5f 53 74 72 69 6e 67 20 62 65 66 6f 72 65 20 P_String before
1c7f7 69 74 20 69 73 20 65 78 65 63 75 74 65 64 20 66 it is executed f
1c7f8 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69 6d or the first tim
1c7f9 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 e..*/.case OP_St
1c7fa 72 69 6e 67 38 3a 20 7b 20 20 20 20 20 20 20 20 ring8: {
1c7fb 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 /* same as TK_S
1c7fc 54 52 49 4e 47 2c 20 6f 75 74 32 2d 70 72 65 72 TRING, out2-prer
1c7fd 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 elease */. asse
1c7fe 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 rt( pOp->p4.z!=0
1c7ff 20 29 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 );. pOp->opcod
1c800 65 20 3d 20 4f 50 5f 53 74 72 69 6e 67 3b 0a 20 e = OP_String;.
1c801 20 70 4f 70 2d 3e 70 31 20 3d 20 73 71 6c 69 74 pOp->p1 = sqlit
1c802 65 33 53 74 72 6c 65 6e 33 30 28 70 4f 70 2d 3e e3Strlen30(pOp->
1c803 70 34 2e 7a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 p4.z);..#ifndef
1c804 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1c805 36 0a 20 20 69 66 28 20 65 6e 63 6f 64 69 6e 67 6. if( encoding
1c806 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b !=SQLITE_UTF8 ){
1c807 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1c808 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 3VdbeMemSetStr(p
1c809 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 Out, pOp->p4.z,
1c80a 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c -1, SQLITE_UTF8,
1c80b 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b SQLITE_STATIC);
1c80c 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
1c80d 49 54 45 5f 54 4f 4f 42 49 47 20 29 20 67 6f 74 ITE_TOOBIG ) got
1c80e 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 69 o too_big;. i
1c80f 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 f( SQLITE_OK!=sq
1c810 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
1c811 6e 63 6f 64 69 6e 67 28 70 4f 75 74 2c 20 65 6e ncoding(pOut, en
1c812 63 6f 64 69 6e 67 29 20 29 20 67 6f 74 6f 20 6e coding) ) goto n
1c813 6f 5f 6d 65 6d 3b 0a 20 20 20 20 61 73 73 65 72 o_mem;. asser
1c814 74 28 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 t( pOut->zMalloc
1c815 3d 3d 70 4f 75 74 2d 3e 7a 20 29 3b 0a 20 20 20 ==pOut->z );.
1c816 20 61 73 73 65 72 74 28 20 70 4f 75 74 2d 3e 66 assert( pOut->f
1c817 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 29 lags & MEM_Dyn )
1c818 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c ;. pOut->zMal
1c819 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 70 4f 75 loc = 0;. pOu
1c81a 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f t->flags |= MEM_
1c81b 53 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 74 Static;. pOut
1c81c 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f ->flags &= ~MEM_
1c81d 44 79 6e 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 Dyn;. if( pOp
1c81e 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 44 59 4e ->p4type==P4_DYN
1c81f 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73 71 AMIC ){. sq
1c820 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1c821 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 pOp->p4.z);.
1c822 7d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 }. pOp->p4typ
1c823 65 20 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b 0a e = P4_DYNAMIC;.
1c824 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 pOp->p4.z =
1c825 70 4f 75 74 2d 3e 7a 3b 0a 20 20 20 20 70 4f 70 pOut->z;. pOp
1c826 2d 3e 70 31 20 3d 20 70 4f 75 74 2d 3e 6e 3b 0a ->p1 = pOut->n;.
1c827 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 }.#endif. if(
1c828 20 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c 69 pOp->p1>db->aLi
1c829 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
1c82a 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 _LENGTH] ){.
1c82b 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 goto too_big;.
1c82c 7d 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f }. /* Fall thro
1c82d 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ugh to the next
1c82e 63 61 73 65 2c 20 4f 50 5f 53 74 72 69 6e 67 20 case, OP_String
1c82f 2a 2f 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f 64 */.}. ./* Opcod
1c830 65 3a 20 53 74 72 69 6e 67 20 50 31 20 50 32 20 e: String P1 P2
1c831 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 * P4 *.**.** The
1c832 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 50 34 string value P4
1c833 20 6f 66 20 6c 65 6e 67 74 68 20 50 31 20 28 62 of length P1 (b
1c834 79 74 65 73 29 20 69 73 20 73 74 6f 72 65 64 20 ytes) is stored
1c835 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a in register P2..
1c836 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 6e */.case OP_Strin
1c837 67 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a g: { /*
1c838 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 out2-prerelease
1c839 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f */. assert( pO
1c83a 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 p->p4.z!=0 );.
1c83b 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pOut->flags = ME
1c83c 4d 5f 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69 63 M_Str|MEM_Static
1c83d 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 |MEM_Term;. pOu
1c83e 74 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a t->z = pOp->p4.z
1c83f 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70 4f ;. pOut->n = pO
1c840 70 2d 3e 70 31 3b 0a 20 20 70 4f 75 74 2d 3e 65 p->p1;. pOut->e
1c841 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 nc = encoding;.
1c842 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 UPDATE_MAX_BLOB
1c843 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 SIZE(pOut);. br
1c844 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1c845 65 3a 20 4e 75 6c 6c 20 2a 20 50 32 20 2a 20 2a e: Null * P2 * *
1c846 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 *.**.** Write a
1c847 20 4e 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69 73 NULL into regis
1c848 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 ter P2..*/.case
1c849 4f 50 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 OP_Null: {
1c84a 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 /* out2-pre
1c84b 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 62 72 65 release */. bre
1c84c 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 ak;.}.../* Opcod
1c84d 65 3a 20 42 6c 6f 62 20 50 31 20 50 32 20 2a 20 e: Blob P1 P2 *
1c84e 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 70 6f 69 6e P4.**.** P4 poin
1c84f 74 73 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66 20 ts to a blob of
1c850 64 61 74 61 20 50 31 20 62 79 74 65 73 20 6c 6f data P1 bytes lo
1c851 6e 67 2e 20 20 53 74 6f 72 65 20 74 68 69 73 0a ng. Store this.
1c852 2a 2a 20 62 6c 6f 62 20 69 6e 20 72 65 67 69 73 ** blob in regis
1c853 74 65 72 20 50 32 2e 20 54 68 69 73 20 69 6e 73 ter P2. This ins
1c854 74 72 75 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 truction is not
1c855 63 6f 64 65 64 20 64 69 72 65 63 74 6c 79 0a 2a coded directly.*
1c856 2a 20 62 79 20 74 68 65 20 63 6f 6d 70 69 6c 65 * by the compile
1c857 72 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20 r. Instead, the
1c858 63 6f 6d 70 69 6c 65 72 20 6c 61 79 65 72 20 73 compiler layer s
1c859 70 65 63 69 66 69 65 73 0a 2a 2a 20 61 6e 20 4f pecifies.** an O
1c85a 50 5f 48 65 78 42 6c 6f 62 20 6f 70 63 6f 64 65 P_HexBlob opcode
1c85b 2c 20 77 69 74 68 20 74 68 65 20 68 65 78 20 73 , with the hex s
1c85c 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 tring representa
1c85d 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 62 tion of.** the b
1c85e 6c 6f 62 20 61 73 20 50 34 2e 20 54 68 69 73 20 lob as P4. This
1c85f 6f 70 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66 opcode is transf
1c860 6f 72 6d 65 64 20 74 6f 20 61 6e 20 4f 50 5f 42 ormed to an OP_B
1c861 6c 6f 62 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 lob.** the first
1c862 20 74 69 6d 65 20 69 74 20 69 73 20 65 78 65 63 time it is exec
1c863 75 74 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 uted..*/.case OP
1c864 5f 42 6c 6f 62 3a 20 7b 20 20 20 20 20 20 20 20 _Blob: {
1c865 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d /* out2-
1c866 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 prerelease */.
1c867 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 20 assert( pOp->p1
1c868 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 <= SQLITE_MAX_LE
1c869 4e 47 54 48 20 29 3b 0a 20 20 73 71 6c 69 74 65 NGTH );. sqlite
1c86a 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 3VdbeMemSetStr(p
1c86b 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 Out, pOp->p4.z,
1c86c 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 30 29 3b 0a pOp->p1, 0, 0);.
1c86d 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e pOut->enc = en
1c86e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 coding;. UPDATE
1c86f 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f _MAX_BLOBSIZE(pO
1c870 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a ut);. break;.}.
1c871 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 72 69 ./* Opcode: Vari
1c872 61 62 6c 65 20 50 31 20 50 32 20 50 33 20 50 34 able P1 P2 P3 P4
1c873 20 2a 0a 2a 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 *.**.** Transfe
1c874 72 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 r the values of
1c875 62 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 73 bound parameters
1c876 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 69 6e 74 P1..P1+P3-1 int
1c877 6f 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 50 o registers.** P
1c878 32 2e 2e 50 32 2b 50 33 2d 31 2e 0a 2a 2a 0a 2a 2..P2+P3-1..**.*
1c879 2a 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 * If the paramet
1c87a 65 72 20 69 73 20 6e 61 6d 65 64 2c 20 74 68 65 er is named, the
1c87b 6e 20 69 74 73 20 6e 61 6d 65 20 61 70 70 65 61 n its name appea
1c87c 72 73 20 69 6e 20 50 34 20 61 6e 64 20 50 33 3d rs in P4 and P3=
1c87d 3d 31 2e 0a 2a 2a 20 54 68 65 20 50 34 20 76 61 =1..** The P4 va
1c87e 6c 75 65 20 69 73 20 75 73 65 64 20 62 79 20 73 lue is used by s
1c87f 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
1c880 6d 65 74 65 72 5f 6e 61 6d 65 28 29 2e 0a 2a 2f meter_name()..*/
1c881 0a 63 61 73 65 20 4f 50 5f 56 61 72 69 61 62 6c .case OP_Variabl
1c882 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c e: {.#if 0 /* l
1c883 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1c884 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 62 20 2a oved into u.ab *
1c885 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 /. int p1;
1c886 20 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 /* Variable
1c887 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f to copy from */
1c888 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 . int p2;
1c889 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 /* Register
1c88a 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 20 20 to copy to */.
1c88b 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 int n;
1c88c 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 61 /* Number of va
1c88d 6c 75 65 73 20 6c 65 66 74 20 74 6f 20 63 6f 70 lues left to cop
1c88e 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 56 61 72 y */. Mem *pVar
1c88f 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 ; /* Value
1c890 20 62 65 69 6e 67 20 74 72 61 6e 73 66 65 72 72 being transferr
1c891 65 64 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 ed */.#endif /*
1c892 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1c893 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 62 20 moved into u.ab
1c894 2a 2f 0a 0a 20 20 75 2e 61 62 2e 70 31 20 3d 20 */.. u.ab.p1 =
1c895 70 4f 70 2d 3e 70 31 20 2d 20 31 3b 0a 20 20 75 pOp->p1 - 1;. u
1c896 2e 61 62 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 .ab.p2 = pOp->p2
1c897 3b 0a 20 20 75 2e 61 62 2e 6e 20 3d 20 70 4f 70 ;. u.ab.n = pOp
1c898 2d 3e 70 33 3b 0a 20 20 61 73 73 65 72 74 28 20 ->p3;. assert(
1c899 75 2e 61 62 2e 70 31 3e 3d 30 20 26 26 20 75 2e u.ab.p1>=0 && u.
1c89a 61 62 2e 70 31 2b 75 2e 61 62 2e 6e 3c 3d 70 2d ab.p1+u.ab.n<=p-
1c89b 3e 6e 56 61 72 20 29 3b 0a 20 20 61 73 73 65 72 >nVar );. asser
1c89c 74 28 20 75 2e 61 62 2e 70 32 3e 3d 31 20 26 26 t( u.ab.p2>=1 &&
1c89d 20 75 2e 61 62 2e 70 32 2b 75 2e 61 62 2e 6e 2d u.ab.p2+u.ab.n-
1c89e 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 1<=p->nMem );.
1c89f 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e assert( pOp->p4.
1c8a0 7a 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 33 3d z==0 || pOp->p3=
1c8a1 3d 31 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 =1 );.. while(
1c8a2 75 2e 61 62 2e 6e 2d 2d 20 3e 20 30 20 29 7b 0a u.ab.n-- > 0 ){.
1c8a3 20 20 20 20 75 2e 61 62 2e 70 56 61 72 20 3d 20 u.ab.pVar =
1c8a4 26 70 2d 3e 61 56 61 72 5b 75 2e 61 62 2e 70 31 &p->aVar[u.ab.p1
1c8a5 2b 2b 5d 3b 0a 20 20 20 20 69 66 28 20 73 71 6c ++];. if( sql
1c8a6 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 ite3VdbeMemTooBi
1c8a7 67 28 75 2e 61 62 2e 70 56 61 72 29 20 29 7b 0a g(u.ab.pVar) ){.
1c8a8 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 goto too_b
1c8a9 69 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f ig;. }. pO
1c8aa 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e ut = &p->aMem[u.
1c8ab 61 62 2e 70 32 2b 2b 5d 3b 0a 20 20 20 20 73 71 ab.p2++];. sq
1c8ac 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
1c8ad 61 73 65 45 78 74 65 72 6e 61 6c 28 70 4f 75 74 aseExternal(pOut
1c8ae 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 );. pOut->fla
1c8af 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
1c8b0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1c8b1 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 mShallowCopy(pOu
1c8b2 74 2c 20 75 2e 61 62 2e 70 56 61 72 2c 20 4d 45 t, u.ab.pVar, ME
1c8b3 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 55 M_Static);. U
1c8b4 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 PDATE_MAX_BLOBSI
1c8b5 5a 45 28 70 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 ZE(pOut);. }.
1c8b6 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1c8b7 6f 64 65 3a 20 4d 6f 76 65 20 50 31 20 50 32 20 ode: Move P1 P2
1c8b8 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 P3 * *.**.** Mov
1c8b9 65 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 e the values in
1c8ba 72 65 67 69 73 74 65 72 20 50 31 2e 2e 50 31 2b register P1..P1+
1c8bb 50 33 2d 31 20 6f 76 65 72 20 69 6e 74 6f 0a 2a P3-1 over into.*
1c8bc 2a 20 72 65 67 69 73 74 65 72 73 20 50 32 2e 2e * registers P2..
1c8bd 50 32 2b 50 33 2d 31 2e 20 20 52 65 67 69 73 74 P2+P3-1. Regist
1c8be 65 72 73 20 50 31 2e 2e 50 31 2b 50 31 2d 31 20 ers P1..P1+P1-1
1c8bf 61 72 65 0a 2a 2a 20 6c 65 66 74 20 68 6f 6c 64 are.** left hold
1c8c0 69 6e 67 20 61 20 4e 55 4c 4c 2e 20 20 49 74 20 ing a NULL. It
1c8c1 69 73 20 61 6e 20 65 72 72 6f 72 20 66 6f 72 20 is an error for
1c8c2 72 65 67 69 73 74 65 72 20 72 61 6e 67 65 73 0a register ranges.
1c8c3 2a 2a 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 61 ** P1..P1+P3-1 a
1c8c4 6e 64 20 50 32 2e 2e 50 32 2b 50 33 2d 31 20 74 nd P2..P2+P3-1 t
1c8c5 6f 20 6f 76 65 72 6c 61 70 2e 0a 2a 2f 0a 63 61 o overlap..*/.ca
1c8c6 73 65 20 4f 50 5f 4d 6f 76 65 3a 20 7b 0a 23 69 se OP_Move: {.#i
1c8c7 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1c8c8 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1c8c9 74 6f 20 75 2e 61 63 20 2a 2f 0a 20 20 63 68 61 to u.ac */. cha
1c8ca 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 20 2f 2a r *zMalloc; /*
1c8cb 20 48 6f 6c 64 69 6e 67 20 76 61 72 69 61 62 6c Holding variabl
1c8cc 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 65 64 20 e for allocated
1c8cd 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 20 memory */. int
1c8ce 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 n; /*
1c8cf 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 Number of regist
1c8d0 65 72 73 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 ers left to copy
1c8d1 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 */. int p1;
1c8d2 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
1c8d3 65 72 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 er to copy from
1c8d4 2a 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 */. int p2;
1c8d5 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 /* Registe
1c8d6 72 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a r to copy to */.
1c8d7 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1c8d8 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1c8d9 69 6e 74 6f 20 75 2e 61 63 20 2a 2f 0a 0a 20 20 into u.ac */..
1c8da 75 2e 61 63 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33 u.ac.n = pOp->p3
1c8db 3b 0a 20 20 75 2e 61 63 2e 70 31 20 3d 20 70 4f ;. u.ac.p1 = pO
1c8dc 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 63 2e 70 32 p->p1;. u.ac.p2
1c8dd 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 = pOp->p2;. as
1c8de 73 65 72 74 28 20 75 2e 61 63 2e 6e 3e 30 20 26 sert( u.ac.n>0 &
1c8df 26 20 75 2e 61 63 2e 70 31 3e 30 20 26 26 20 75 & u.ac.p1>0 && u
1c8e0 2e 61 63 2e 70 32 3e 30 20 29 3b 0a 20 20 61 73 .ac.p2>0 );. as
1c8e1 73 65 72 74 28 20 75 2e 61 63 2e 70 31 2b 75 2e sert( u.ac.p1+u.
1c8e2 61 63 2e 6e 3c 3d 75 2e 61 63 2e 70 32 20 7c 7c ac.n<=u.ac.p2 ||
1c8e3 20 75 2e 61 63 2e 70 32 2b 75 2e 61 63 2e 6e 3c u.ac.p2+u.ac.n<
1c8e4 3d 75 2e 61 63 2e 70 31 20 29 3b 0a 0a 20 20 70 =u.ac.p1 );.. p
1c8e5 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 In1 = &p->aMem[u
1c8e6 2e 61 63 2e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 .ac.p1];. pOut
1c8e7 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 63 2e = &p->aMem[u.ac.
1c8e8 70 32 5d 3b 0a 20 20 77 68 69 6c 65 28 20 75 2e p2];. while( u.
1c8e9 61 63 2e 6e 2d 2d 20 29 7b 0a 20 20 20 20 61 73 ac.n-- ){. as
1c8ea 73 65 72 74 28 20 70 4f 75 74 3c 3d 26 70 2d 3e sert( pOut<=&p->
1c8eb 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b aMem[p->nMem] );
1c8ec 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e . assert( pIn
1c8ed 31 3c 3d 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 1<=&p->aMem[p->n
1c8ee 4d 65 6d 5d 20 29 3b 0a 20 20 20 20 75 2e 61 63 Mem] );. u.ac
1c8ef 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4f 75 74 2d .zMalloc = pOut-
1c8f0 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 70 4f >zMalloc;. pO
1c8f1 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b ut->zMalloc = 0;
1c8f2 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1c8f3 4d 65 6d 4d 6f 76 65 28 70 4f 75 74 2c 20 70 49 MemMove(pOut, pI
1c8f4 6e 31 29 3b 0a 20 20 20 20 70 49 6e 31 2d 3e 7a n1);. pIn1->z
1c8f5 4d 61 6c 6c 6f 63 20 3d 20 75 2e 61 63 2e 7a 4d Malloc = u.ac.zM
1c8f6 61 6c 6c 6f 63 3b 0a 20 20 20 20 52 45 47 49 53 alloc;. REGIS
1c8f7 54 45 52 5f 54 52 41 43 45 28 75 2e 61 63 2e 70 TER_TRACE(u.ac.p
1c8f8 32 2b 2b 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 2++, pOut);.
1c8f9 70 49 6e 31 2b 2b 3b 0a 20 20 20 20 70 4f 75 74 pIn1++;. pOut
1c8fa 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b ++;. }. break;
1c8fb 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 .}../* Opcode: C
1c8fc 6f 70 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a opy P1 P2 * * *.
1c8fd 2a 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 **.** Make a cop
1c8fe 79 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 y of register P1
1c8ff 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 into register P
1c900 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 2..**.** This in
1c901 73 74 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20 struction makes
1c902 61 20 64 65 65 70 20 63 6f 70 79 20 6f 66 20 74 a deep copy of t
1c903 68 65 20 76 61 6c 75 65 2e 20 20 41 20 64 75 70 he value. A dup
1c904 6c 69 63 61 74 65 0a 2a 2a 20 69 73 20 6d 61 64 licate.** is mad
1c905 65 20 6f 66 20 61 6e 79 20 73 74 72 69 6e 67 20 e of any string
1c906 6f 72 20 62 6c 6f 62 20 63 6f 6e 73 74 61 6e 74 or blob constant
1c907 2e 20 20 53 65 65 20 61 6c 73 6f 20 4f 50 5f 53 . See also OP_S
1c908 43 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 Copy..*/.case OP
1c909 5f 43 6f 70 79 3a 20 7b 20 20 20 20 20 20 20 20 _Copy: {
1c90a 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 /* in1 */.
1c90b 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 assert( pOp->p2
1c90c 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 >0 );. assert(
1c90d 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p2<=p->nMem
1c90e 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d );. pOut = &p-
1c90f 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a >aMem[pOp->p2];.
1c910 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d assert( pOut!=
1c911 70 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 pIn1 );. sqlite
1c912 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 3VdbeMemShallowC
1c913 6f 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 opy(pOut, pIn1,
1c914 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 44 65 MEM_Ephem);. De
1c915 65 70 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f 75 ephemeralize(pOu
1c916 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 t);. REGISTER_T
1c917 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f RACE(pOp->p2, pO
1c918 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a ut);. break;.}.
1c919 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 43 6f 70 ./* Opcode: SCop
1c91a 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a y P1 P2 * * *.**
1c91b 0a 2a 2a 20 4d 61 6b 65 20 61 20 73 68 61 6c 6c .** Make a shall
1c91c 6f 77 20 63 6f 70 79 20 6f 66 20 72 65 67 69 73 ow copy of regis
1c91d 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65 67 69 ter P1 into regi
1c91e 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 ster P2..**.** T
1c91f 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1c920 6d 61 6b 65 73 20 61 20 73 68 61 6c 6c 6f 77 20 makes a shallow
1c921 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75 copy of the valu
1c922 65 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 e. If the value
1c923 0a 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20 .** is a string
1c924 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 74 68 or blob, then th
1c925 65 20 63 6f 70 79 20 69 73 20 6f 6e 6c 79 20 61 e copy is only a
1c926 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a pointer to the.
1c927 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 61 6e 64 20 ** original and
1c928 68 65 6e 63 65 20 69 66 20 74 68 65 20 6f 72 69 hence if the ori
1c929 67 69 6e 61 6c 20 63 68 61 6e 67 65 73 20 73 6f ginal changes so
1c92a 20 77 69 6c 6c 20 74 68 65 20 63 6f 70 79 2e 0a will the copy..
1c92b 2a 2a 20 57 6f 72 73 65 2c 20 69 66 20 74 68 65 ** Worse, if the
1c92c 20 6f 72 69 67 69 6e 61 6c 20 69 73 20 64 65 61 original is dea
1c92d 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 20 63 6f llocated, the co
1c92e 70 79 20 62 65 63 6f 6d 65 73 20 69 6e 76 61 6c py becomes inval
1c92f 69 64 2e 0a 2a 2a 20 54 68 75 73 20 74 68 65 20 id..** Thus the
1c930 70 72 6f 67 72 61 6d 20 6d 75 73 74 20 67 75 61 program must gua
1c931 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 rantee that the
1c932 6f 72 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e 6f original will no
1c933 74 20 63 68 61 6e 67 65 0a 2a 2a 20 64 75 72 69 t change.** duri
1c934 6e 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 ng the lifetime
1c935 6f 66 20 74 68 65 20 63 6f 70 79 2e 20 20 55 73 of the copy. Us
1c936 65 20 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61 6b e OP_Copy to mak
1c937 65 20 61 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 e a complete.**
1c938 63 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 copy..*/.case OP
1c939 5f 53 43 6f 70 79 3a 20 7b 20 20 20 20 20 20 20 _SCopy: {
1c93a 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 /* in1 */.
1c93b 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1c93c 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a pOp->p1, pIn1);.
1c93d 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1c93e 32 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 2>0 );. assert(
1c93f 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 pOp->p2<=p->nMe
1c940 6d 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 m );. pOut = &p
1c941 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b ->aMem[pOp->p2];
1c942 0a 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 21 . assert( pOut!
1c943 3d 70 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 =pIn1 );. sqlit
1c944 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 e3VdbeMemShallow
1c945 43 6f 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c Copy(pOut, pIn1,
1c946 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 52 MEM_Ephem);. R
1c947 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1c948 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 p->p2, pOut);.
1c949 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1c94a 6f 64 65 3a 20 52 65 73 75 6c 74 52 6f 77 20 50 ode: ResultRow P
1c94b 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1c94c 20 54 68 65 20 72 65 67 69 73 74 65 72 73 20 50 The registers P
1c94d 31 20 74 68 72 6f 75 67 68 20 50 31 2b 50 32 2d 1 through P1+P2-
1c94e 31 20 63 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67 1 contain a sing
1c94f 6c 65 20 72 6f 77 20 6f 66 0a 2a 2a 20 72 65 73 le row of.** res
1c950 75 6c 74 73 2e 20 54 68 69 73 20 6f 70 63 6f 64 ults. This opcod
1c951 65 20 63 61 75 73 65 73 20 74 68 65 20 73 71 6c e causes the sql
1c952 69 74 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c ite3_step() call
1c953 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a to terminate.**
1c954 20 77 69 74 68 20 61 6e 20 53 51 4c 49 54 45 5f with an SQLITE_
1c955 52 4f 57 20 72 65 74 75 72 6e 20 63 6f 64 65 20 ROW return code
1c956 61 6e 64 20 69 74 20 73 65 74 73 20 75 70 20 74 and it sets up t
1c957 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 0a he sqlite3_stmt.
1c958 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 ** structure to
1c959 70 72 6f 76 69 64 65 20 61 63 63 65 73 73 20 74 provide access t
1c95a 6f 20 74 68 65 20 74 6f 70 20 50 31 20 76 61 6c o the top P1 val
1c95b 75 65 73 20 61 73 20 74 68 65 20 72 65 73 75 6c ues as the resul
1c95c 74 0a 2a 2a 20 72 6f 77 2e 0a 2a 2f 0a 63 61 73 t.** row..*/.cas
1c95d 65 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 3a 20 e OP_ResultRow:
1c95e 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1c95f 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1c960 64 20 69 6e 74 6f 20 75 2e 61 64 20 2a 2f 0a 20 d into u.ad */.
1c961 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e Mem *pMem;. in
1c962 74 20 69 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c t i;.#endif /* l
1c963 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1c964 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 64 20 2a oved into u.ad *
1c965 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e /. assert( p->n
1c966 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70 4f 70 2d 3e ResColumn==pOp->
1c967 70 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 p2 );. assert(
1c968 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 pOp->p1>0 );. a
1c969 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 2b 70 ssert( pOp->p1+p
1c96a 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 2b Op->p2<=p->nMem+
1c96b 31 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 1 );.. /* If th
1c96c 65 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f e SQLITE_CountRo
1c96d 77 73 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 ws flag is set i
1c96e 6e 20 73 71 6c 69 74 65 33 2e 66 6c 61 67 73 20 n sqlite3.flags
1c96f 6d 61 73 6b 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 mask, then. **
1c970 44 4d 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 DML statements i
1c971 6e 76 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f 64 nvoke this opcod
1c972 65 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65 20 e to return the
1c973 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a 20 number of rows.
1c974 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 74 6f 20 ** modified to
1c975 74 68 65 20 75 73 65 72 2e 20 54 68 69 73 20 69 the user. This i
1c976 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 s the only way t
1c977 68 61 74 20 61 20 56 4d 20 74 68 61 74 0a 20 20 hat a VM that.
1c978 2a 2a 20 6f 70 65 6e 73 20 61 20 73 74 61 74 65 ** opens a state
1c979 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1c97a 20 6d 61 79 20 69 6e 76 6f 6b 65 20 74 68 69 73 may invoke this
1c97b 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20 20 opcode.. **.
1c97c 2a 2a 20 49 6e 20 63 61 73 65 20 74 68 69 73 20 ** In case this
1c97d 69 73 20 73 75 63 68 20 61 20 73 74 61 74 65 6d is such a statem
1c97e 65 6e 74 2c 20 63 6c 6f 73 65 20 61 6e 79 20 73 ent, close any s
1c97f 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
1c980 74 69 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 tion. ** opened
1c981 20 62 79 20 74 68 69 73 20 56 4d 20 62 65 66 6f by this VM befo
1c982 72 65 20 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e re returning con
1c983 74 72 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 72 trol to the user
1c984 2e 20 54 68 69 73 20 69 73 20 74 6f 0a 20 20 2a . This is to. *
1c985 2a 20 65 6e 73 75 72 65 20 74 68 61 74 20 73 74 * ensure that st
1c986 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 atement-transact
1c987 69 6f 6e 73 20 61 72 65 20 61 6c 77 61 79 73 20 ions are always
1c988 6e 65 73 74 65 64 2c 20 6e 6f 74 20 6f 76 65 72 nested, not over
1c989 6c 61 70 70 69 6e 67 2e 0a 20 20 2a 2a 20 49 66 lapping.. ** If
1c98a 20 74 68 65 20 6f 70 65 6e 20 73 74 61 74 65 6d the open statem
1c98b 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent-transaction
1c98c 69 73 20 6e 6f 74 20 63 6c 6f 73 65 64 20 68 65 is not closed he
1c98d 72 65 2c 20 74 68 65 6e 20 74 68 65 20 75 73 65 re, then the use
1c98e 72 0a 20 20 2a 2a 20 6d 61 79 20 73 74 65 70 20 r. ** may step
1c98f 61 6e 6f 74 68 65 72 20 56 4d 20 74 68 61 74 20 another VM that
1c990 6f 70 65 6e 73 20 69 74 73 20 6f 77 6e 20 73 74 opens its own st
1c991 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 atement transact
1c992 69 6f 6e 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6d ion. This. ** m
1c993 61 79 20 6c 65 61 64 20 74 6f 20 6f 76 65 72 6c ay lead to overl
1c994 61 70 70 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 apping statement
1c995 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 20 transactions..
1c996 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74 61 **. ** The sta
1c997 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
1c998 6f 6e 20 69 73 20 6e 65 76 65 72 20 61 20 74 6f on is never a to
1c999 70 2d 6c 65 76 65 6c 20 74 72 61 6e 73 61 63 74 p-level transact
1c99a 69 6f 6e 2e 20 20 48 65 6e 63 65 0a 20 20 2a 2a ion. Hence. **
1c99b 20 74 68 65 20 52 45 4c 45 41 53 45 20 63 61 6c the RELEASE cal
1c99c 6c 20 62 65 6c 6f 77 20 63 61 6e 20 6e 65 76 65 l below can neve
1c99d 72 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 61 r fail.. */. a
1c99e 73 73 65 72 74 28 20 70 2d 3e 69 53 74 61 74 65 ssert( p->iState
1c99f 6d 65 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 66 ment==0 || db->f
1c9a0 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e lags&SQLITE_Coun
1c9a1 74 52 6f 77 73 20 29 3b 0a 20 20 72 63 20 3d 20 tRows );. rc =
1c9a2 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 sqlite3VdbeClose
1c9a3 53 74 61 74 65 6d 65 6e 74 28 70 2c 20 53 41 56 Statement(p, SAV
1c9a4 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 3b EPOINT_RELEASE);
1c9a5 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 21 . if( NEVER(rc!
1c9a6 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 =SQLITE_OK) ){.
1c9a7 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 break;. }..
1c9a8 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 /* Invalidate a
1c9a9 6c 6c 20 65 70 68 65 6d 65 72 61 6c 20 63 75 72 ll ephemeral cur
1c9aa 73 6f 72 20 72 6f 77 20 63 61 63 68 65 73 20 2a sor row caches *
1c9ab 2f 0a 20 20 70 2d 3e 63 61 63 68 65 43 74 72 20 /. p->cacheCtr
1c9ac 3d 20 28 70 2d 3e 63 61 63 68 65 43 74 72 20 2b = (p->cacheCtr +
1c9ad 20 32 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 2)|1;.. /* Mak
1c9ae 65 20 73 75 72 65 20 74 68 65 20 72 65 73 75 6c e sure the resul
1c9af 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e ts of the curren
1c9b0 74 20 72 6f 77 20 61 72 65 20 5c 30 30 30 20 74 t row are \000 t
1c9b1 65 72 6d 69 6e 61 74 65 64 0a 20 20 2a 2a 20 61 erminated. ** a
1c9b2 6e 64 20 68 61 76 65 20 61 6e 20 61 73 73 69 67 nd have an assig
1c9b3 6e 65 64 20 74 79 70 65 2e 20 20 54 68 65 20 72 ned type. The r
1c9b4 65 73 75 6c 74 73 20 61 72 65 20 64 65 2d 65 70 esults are de-ep
1c9b5 68 65 6d 65 72 61 6c 69 7a 65 64 20 61 73 0a 20 hemeralized as.
1c9b6 20 2a 2a 20 61 73 20 73 69 64 65 20 65 66 66 65 ** as side effe
1c9b7 63 74 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 64 2e ct.. */. u.ad.
1c9b8 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c pMem = p->pResul
1c9b9 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b tSet = &p->aMem[
1c9ba 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 pOp->p1];. for(
1c9bb 75 2e 61 64 2e 69 3d 30 3b 20 75 2e 61 64 2e 69 u.ad.i=0; u.ad.i
1c9bc 3c 70 4f 70 2d 3e 70 32 3b 20 75 2e 61 64 2e 69 <pOp->p2; u.ad.i
1c9bd 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ++){. sqlite3
1c9be 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e VdbeMemNulTermin
1c9bf 61 74 65 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 ate(&u.ad.pMem[u
1c9c0 2e 61 64 2e 69 5d 29 3b 0a 20 20 20 20 73 74 6f .ad.i]);. sto
1c9c1 72 65 54 79 70 65 49 6e 66 6f 28 26 75 2e 61 64 reTypeInfo(&u.ad
1c9c2 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 2c 20 65 .pMem[u.ad.i], e
1c9c3 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 52 45 ncoding);. RE
1c9c4 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
1c9c5 2d 3e 70 31 2b 75 2e 61 64 2e 69 2c 20 26 75 2e ->p1+u.ad.i, &u.
1c9c6 61 64 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 29 ad.pMem[u.ad.i])
1c9c7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e ;. }. if( db->
1c9c8 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 mallocFailed ) g
1c9c9 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 0a 20 20 2f oto no_mem;.. /
1c9ca 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f * Return SQLITE_
1c9cb 52 4f 57 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 63 ROW. */. p->pc
1c9cc 20 3d 20 70 63 20 2b 20 31 3b 0a 20 20 72 63 20 = pc + 1;. rc
1c9cd 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 = SQLITE_ROW;.
1c9ce 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e goto vdbe_return
1c9cf 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1c9d0 43 6f 6e 63 61 74 20 50 31 20 50 32 20 50 33 20 Concat P1 P2 P3
1c9d1 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 * *.**.** Add th
1c9d2 65 20 74 65 78 74 20 69 6e 20 72 65 67 69 73 74 e text in regist
1c9d3 65 72 20 50 31 20 6f 6e 74 6f 20 74 68 65 20 65 er P1 onto the e
1c9d4 6e 64 20 6f 66 20 74 68 65 20 74 65 78 74 20 69 nd of the text i
1c9d5 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 n.** register P2
1c9d6 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 and store the r
1c9d7 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 esult in registe
1c9d8 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 r P3..** If eith
1c9d9 65 72 20 74 68 65 20 50 31 20 6f 72 20 50 32 20 er the P1 or P2
1c9da 74 65 78 74 20 61 72 65 20 4e 55 4c 4c 20 74 68 text are NULL th
1c9db 65 6e 20 73 74 6f 72 65 20 4e 55 4c 4c 20 69 6e en store NULL in
1c9dc 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 33 20 P3..**.** P3
1c9dd 3d 20 50 32 20 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a = P2 || P1.**.**
1c9de 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 66 It is illegal f
1c9df 6f 72 20 50 31 20 61 6e 64 20 50 33 20 74 6f 20 or P1 and P3 to
1c9e0 62 65 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 be the same regi
1c9e1 73 74 65 72 2e 20 53 6f 6d 65 74 69 6d 65 73 2c ster. Sometimes,
1c9e2 0a 2a 2a 20 69 66 20 50 33 20 69 73 20 74 68 65 .** if P3 is the
1c9e3 20 73 61 6d 65 20 72 65 67 69 73 74 65 72 20 61 same register a
1c9e4 73 20 50 32 2c 20 74 68 65 20 69 6d 70 6c 65 6d s P2, the implem
1c9e5 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 62 6c 65 entation is able
1c9e6 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 6d .** to avoid a m
1c9e7 65 6d 63 70 79 28 29 2e 0a 2a 2f 0a 63 61 73 65 emcpy()..*/.case
1c9e8 20 4f 50 5f 43 6f 6e 63 61 74 3a 20 7b 20 20 20 OP_Concat: {
1c9e9 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1c9ea 61 73 20 54 4b 5f 43 4f 4e 43 41 54 2c 20 69 6e as TK_CONCAT, in
1c9eb 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 1, in2, out3 */.
1c9ec 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1c9ed 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1c9ee 69 6e 74 6f 20 75 2e 61 65 20 2a 2f 0a 20 20 69 into u.ae */. i
1c9ef 36 34 20 6e 42 79 74 65 3b 0a 23 65 6e 64 69 66 64 nByte;.#endif
1c9f0 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1c9f1 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1c9f2 2e 61 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 .ae */.. assert
1c9f3 28 20 70 49 6e 31 21 3d 70 4f 75 74 20 29 3b 0a ( pIn1!=pOut );.
1c9f4 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 if( (pIn1->fla
1c9f5 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 gs | pIn2->flags
1c9f6 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a ) & MEM_Null ){.
1c9f7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1c9f8 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b emSetNull(pOut);
1c9f9 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a . break;. }.
1c9fa 20 20 69 66 28 20 45 78 70 61 6e 64 42 6c 6f 62 if( ExpandBlob
1c9fb 28 70 49 6e 31 29 20 7c 7c 20 45 78 70 61 6e 64 (pIn1) || Expand
1c9fc 42 6c 6f 62 28 70 49 6e 32 29 20 29 20 67 6f 74 Blob(pIn2) ) got
1c9fd 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 53 74 72 69 o no_mem;. Stri
1c9fe 6e 67 69 66 79 28 70 49 6e 31 2c 20 65 6e 63 6f ngify(pIn1, enco
1c9ff 64 69 6e 67 29 3b 0a 20 20 53 74 72 69 6e 67 69 ding);. Stringi
1ca00 66 79 28 70 49 6e 32 2c 20 65 6e 63 6f 64 69 6e fy(pIn2, encodin
1ca01 67 29 3b 0a 20 20 75 2e 61 65 2e 6e 42 79 74 65 g);. u.ae.nByte
1ca02 20 3d 20 70 49 6e 31 2d 3e 6e 20 2b 20 70 49 6e = pIn1->n + pIn
1ca03 32 2d 3e 6e 3b 0a 20 20 69 66 28 20 75 2e 61 65 2->n;. if( u.ae
1ca04 2e 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 .nByte>db->aLimi
1ca05 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
1ca06 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f ENGTH] ){. go
1ca07 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a to too_big;. }.
1ca08 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1ca09 28 70 4f 75 74 2c 20 4d 45 4d 5f 53 74 72 29 3b (pOut, MEM_Str);
1ca0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 . if( sqlite3Vd
1ca0b 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 beMemGrow(pOut,
1ca0c 28 69 6e 74 29 75 2e 61 65 2e 6e 42 79 74 65 2b (int)u.ae.nByte+
1ca0d 32 2c 20 70 4f 75 74 3d 3d 70 49 6e 32 29 20 29 2, pOut==pIn2) )
1ca0e 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 {. goto no_me
1ca0f 6d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75 m;. }. if( pOu
1ca10 74 21 3d 70 49 6e 32 20 29 7b 0a 20 20 20 20 6d t!=pIn2 ){. m
1ca11 65 6d 63 70 79 28 70 4f 75 74 2d 3e 7a 2c 20 70 emcpy(pOut->z, p
1ca12 49 6e 32 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 6e 29 In2->z, pIn2->n)
1ca13 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26 ;. }. memcpy(&
1ca14 70 4f 75 74 2d 3e 7a 5b 70 49 6e 32 2d 3e 6e 5d pOut->z[pIn2->n]
1ca15 2c 20 70 49 6e 31 2d 3e 7a 2c 20 70 49 6e 31 2d , pIn1->z, pIn1-
1ca16 3e 6e 29 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b 75 >n);. pOut->z[u
1ca17 2e 61 65 2e 6e 42 79 74 65 5d 20 3d 20 30 3b 0a .ae.nByte] = 0;.
1ca18 20 20 70 4f 75 74 2d 3e 7a 5b 75 2e 61 65 2e 6e pOut->z[u.ae.n
1ca19 42 79 74 65 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 Byte+1] = 0;. p
1ca1a 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 Out->flags |= ME
1ca1b 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 74 2d 3e M_Term;. pOut->
1ca1c 6e 20 3d 20 28 69 6e 74 29 75 2e 61 65 2e 6e 42 n = (int)u.ae.nB
1ca1d 79 74 65 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 yte;. pOut->enc
1ca1e 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 = encoding;. U
1ca1f 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 PDATE_MAX_BLOBSI
1ca20 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 ZE(pOut);. brea
1ca21 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1ca22 20 41 64 64 20 50 31 20 50 32 20 50 33 20 2a 20 Add P1 P2 P3 *
1ca23 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 *.**.** Add the
1ca24 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1ca25 72 20 50 31 20 74 6f 20 74 68 65 20 76 61 6c 75 r P1 to the valu
1ca26 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 e in register P2
1ca27 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 .** and store th
1ca28 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 e result in regi
1ca29 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 ster P3..** If e
1ca2a 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e ither input is N
1ca2b 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 ULL, the result
1ca2c 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f is NULL..*/./* O
1ca2d 70 63 6f 64 65 3a 20 4d 75 6c 74 69 70 6c 79 20 pcode: Multiply
1ca2e 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1ca2f 2a 2a 0a 2a 2a 20 4d 75 6c 74 69 70 6c 79 20 74 **.** Multiply t
1ca30 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1ca31 73 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76 ster P1 by the v
1ca32 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1ca33 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 P2.** and store
1ca34 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 the result in r
1ca35 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 egister P3..** I
1ca36 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 f either input i
1ca37 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 s NULL, the resu
1ca38 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f lt is NULL..*/./
1ca39 2a 20 4f 70 63 6f 64 65 3a 20 53 75 62 74 72 61 * Opcode: Subtra
1ca3a 63 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a ct P1 P2 P3 * *.
1ca3b 2a 2a 0a 2a 2a 20 53 75 62 74 72 61 63 74 20 74 **.** Subtract t
1ca3c 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1ca3d 73 74 65 72 20 50 31 20 66 72 6f 6d 20 74 68 65 ster P1 from the
1ca3e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1ca3f 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f er P2.** and sto
1ca40 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e re the result in
1ca41 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a register P3..**
1ca42 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 If either input
1ca43 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 is NULL, the re
1ca44 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f sult is NULL..*/
1ca45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 69 76 69 ./* Opcode: Divi
1ca46 64 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a de P1 P2 P3 * *.
1ca47 2a 2a 0a 2a 2a 20 44 69 76 69 64 65 20 74 68 65 **.** Divide the
1ca48 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1ca49 65 72 20 50 31 20 62 79 20 74 68 65 20 76 61 6c er P1 by the val
1ca4a 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1ca4b 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 2.** and store t
1ca4c 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 he result in reg
1ca4d 69 73 74 65 72 20 50 33 20 28 50 33 3d 50 32 2f ister P3 (P3=P2/
1ca4e 50 31 29 2e 20 49 66 20 74 68 65 20 76 61 6c 75 P1). If the valu
1ca4f 65 20 69 6e 20 0a 2a 2a 20 72 65 67 69 73 74 65 e in .** registe
1ca50 72 20 50 31 20 69 73 20 7a 65 72 6f 2c 20 74 68 r P1 is zero, th
1ca51 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 en the result is
1ca52 20 4e 55 4c 4c 2e 20 49 66 20 65 69 74 68 65 72 NULL. If either
1ca53 20 69 6e 70 75 74 20 69 73 20 0a 2a 2a 20 4e 55 input is .** NU
1ca54 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 LL, the result i
1ca55 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 s NULL..*/./* Op
1ca56 63 6f 64 65 3a 20 52 65 6d 61 69 6e 64 65 72 20 code: Remainder
1ca57 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1ca58 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 72 ** Compute the r
1ca59 65 6d 61 69 6e 64 65 72 20 61 66 74 65 72 20 69 emainder after i
1ca5a 6e 74 65 67 65 72 20 64 69 76 69 73 69 6f 6e 20 nteger division
1ca5b 6f 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 0a of the value in.
1ca5c 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 62 ** register P1 b
1ca5d 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 y the value in r
1ca5e 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 73 egister P2 and s
1ca5f 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 tore the result
1ca60 69 6e 20 50 33 2e 20 0a 2a 2a 20 49 66 20 74 68 in P3. .** If th
1ca61 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1ca62 74 65 72 20 50 32 20 69 73 20 7a 65 72 6f 20 74 ter P2 is zero t
1ca63 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c he result is NUL
1ca64 4c 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 L..** If either
1ca65 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c operand is NULL,
1ca66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e the result is N
1ca67 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ULL..*/.case OP_
1ca68 41 64 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 Add:
1ca69 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1ca6a 73 20 54 4b 5f 50 4c 55 53 2c 20 69 6e 31 2c 20 s TK_PLUS, in1,
1ca6b 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 in2, out3 */.cas
1ca6c 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20 20 e OP_Subtract:
1ca6d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1ca6e 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 2c ame as TK_MINUS,
1ca6f 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 in1, in2, out3
1ca70 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 */.case OP_Multi
1ca71 70 6c 79 3a 20 20 20 20 20 20 20 20 20 20 20 20 ply:
1ca72 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1ca73 53 54 41 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 STAR, in1, in2,
1ca74 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f out3 */.case OP_
1ca75 44 69 76 69 64 65 3a 20 20 20 20 20 20 20 20 20 Divide:
1ca76 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1ca77 73 20 54 4b 5f 53 4c 41 53 48 2c 20 69 6e 31 2c s TK_SLASH, in1,
1ca78 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 in2, out3 */.ca
1ca79 73 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 3a se OP_Remainder:
1ca7a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1ca7b 73 61 6d 65 20 61 73 20 54 4b 5f 52 45 4d 2c 20 same as TK_REM,
1ca7c 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a in1, in2, out3 *
1ca7d 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1ca7e 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1ca7f 64 20 69 6e 74 6f 20 75 2e 61 66 20 2a 2f 0a 20 d into u.af */.
1ca80 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 int flags;
1ca81 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 4d 45 4d /* Combined MEM
1ca82 5f 2a 20 66 6c 61 67 73 20 66 72 6f 6d 20 62 6f _* flags from bo
1ca83 74 68 20 69 6e 70 75 74 73 20 2a 2f 0a 20 20 69 th inputs */. i
1ca84 36 34 20 69 41 3b 20 20 20 20 20 20 20 20 20 2f 64 iA; /
1ca85 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 * Integer value
1ca86 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 of left operand
1ca87 2a 2f 0a 20 20 69 36 34 20 69 42 3b 20 20 20 20 */. i64 iB;
1ca88 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 /* Integer
1ca89 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f value of right o
1ca8a 70 65 72 61 6e 64 20 2a 2f 0a 20 20 64 6f 75 62 perand */. doub
1ca8b 6c 65 20 72 41 3b 20 20 20 20 20 20 2f 2a 20 52 le rA; /* R
1ca8c 65 61 6c 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 eal value of lef
1ca8d 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 64 t operand */. d
1ca8e 6f 75 62 6c 65 20 72 42 3b 20 20 20 20 20 20 2f ouble rB; /
1ca8f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 6f 66 20 * Real value of
1ca90 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f right operand */
1ca91 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1ca92 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ca93 20 69 6e 74 6f 20 75 2e 61 66 20 2a 2f 0a 0a 20 into u.af */..
1ca94 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 applyNumericAff
1ca95 69 6e 69 74 79 28 70 49 6e 31 29 3b 0a 20 20 61 inity(pIn1);. a
1ca96 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e pplyNumericAffin
1ca97 69 74 79 28 70 49 6e 32 29 3b 0a 20 20 75 2e 61 ity(pIn2);. u.a
1ca98 66 2e 66 6c 61 67 73 20 3d 20 70 49 6e 31 2d 3e f.flags = pIn1->
1ca99 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c flags | pIn2->fl
1ca9a 61 67 73 3b 0a 20 20 69 66 28 20 28 75 2e 61 66 ags;. if( (u.af
1ca9b 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c .flags & MEM_Nul
1ca9c 6c 29 21 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 l)!=0 ) goto ari
1ca9d 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 thmetic_result_i
1ca9e 73 5f 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 28 70 s_null;. if( (p
1ca9f 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 70 49 6e In1->flags & pIn
1caa0 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 2->flags & MEM_I
1caa1 6e 74 29 3d 3d 4d 45 4d 5f 49 6e 74 20 29 7b 0a nt)==MEM_Int ){.
1caa2 20 20 20 20 75 2e 61 66 2e 69 41 20 3d 20 70 49 u.af.iA = pI
1caa3 6e 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 75 2e 61 n1->u.i;. u.a
1caa4 66 2e 69 42 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 f.iB = pIn2->u.i
1caa5 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 4f ;. switch( pO
1caa6 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 p->opcode ){.
1caa7 20 20 20 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 case OP_Add:
1caa8 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20 u.af.iB
1caa9 2b 3d 20 75 2e 61 66 2e 69 41 3b 20 20 20 20 20 += u.af.iA;
1caaa 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
1caab 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a ase OP_Subtract:
1caac 20 20 20 20 75 2e 61 66 2e 69 42 20 2d 3d 20 75 u.af.iB -= u
1caad 2e 61 66 2e 69 41 3b 20 20 20 20 20 20 20 62 72 .af.iA; br
1caae 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 eak;. case
1caaf 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 OP_Multiply:
1cab0 75 2e 61 66 2e 69 42 20 2a 3d 20 75 2e 61 66 2e u.af.iB *= u.af.
1cab1 69 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b iA; break;
1cab2 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 44 . case OP_D
1cab3 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 ivide: {.
1cab4 20 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 30 20 if( u.af.iA==0
1cab5 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 ) goto arithmeti
1cab6 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c c_result_is_null
1cab7 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 44 69 76 ;. /* Div
1cab8 69 64 69 6e 67 20 74 68 65 20 6c 61 72 67 65 73 iding the larges
1cab9 74 20 70 6f 73 73 69 62 6c 65 20 6e 65 67 61 74 t possible negat
1caba 69 76 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 ive 64-bit integ
1cabb 65 72 20 28 31 3c 3c 36 33 29 20 62 79 0a 20 20 er (1<<63) by.
1cabc 20 20 20 20 20 20 2a 2a 20 2d 31 20 72 65 74 75 ** -1 retu
1cabd 72 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 rns an integer t
1cabe 6f 6f 20 6c 61 72 67 65 20 74 6f 20 73 74 6f 72 oo large to stor
1cabf 65 20 69 6e 20 61 20 36 34 2d 62 69 74 20 64 61 e in a 64-bit da
1cac0 74 61 2d 74 79 70 65 2e 20 4f 6e 0a 20 20 20 20 ta-type. On.
1cac1 20 20 20 20 2a 2a 20 73 6f 6d 65 20 61 72 63 68 ** some arch
1cac2 69 74 65 63 74 75 72 65 73 2c 20 74 68 65 20 76 itectures, the v
1cac3 61 6c 75 65 20 6f 76 65 72 66 6c 6f 77 73 20 74 alue overflows t
1cac4 6f 20 28 31 3c 3c 36 33 29 2e 20 4f 6e 20 6f 74 o (1<<63). On ot
1cac5 68 65 72 73 2c 0a 20 20 20 20 20 20 20 20 2a 2a hers,. **
1cac6 20 61 20 53 49 47 46 50 45 20 69 73 20 69 73 73 a SIGFPE is iss
1cac7 75 65 64 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ued. The followi
1cac8 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 72 ng statement nor
1cac9 6d 61 6c 69 7a 65 73 20 74 68 69 73 0a 20 20 20 malizes this.
1caca 20 20 20 20 20 2a 2a 20 62 65 68 61 76 69 6f 72 ** behavior
1cacb 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 61 72 63 so that all arc
1cacc 68 69 74 65 63 74 75 72 65 73 20 62 65 68 61 76 hitectures behav
1cacd 65 20 61 73 20 69 66 20 69 6e 74 65 67 65 72 0a e as if integer.
1cace 20 20 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 ** overf
1cacf 6c 6f 77 20 6f 63 63 75 72 72 65 64 2e 0a 20 20 low occurred..
1cad0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
1cad1 20 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 2d 31 if( u.af.iA==-1
1cad2 20 26 26 20 75 2e 61 66 2e 69 42 3d 3d 53 4d 41 && u.af.iB==SMA
1cad3 4c 4c 45 53 54 5f 49 4e 54 36 34 20 29 20 75 2e LLEST_INT64 ) u.
1cad4 61 66 2e 69 41 20 3d 20 31 3b 0a 20 20 20 20 20 af.iA = 1;.
1cad5 20 20 20 75 2e 61 66 2e 69 42 20 2f 3d 20 75 2e u.af.iB /= u.
1cad6 61 66 2e 69 41 3b 0a 20 20 20 20 20 20 20 20 62 af.iA;. b
1cad7 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
1cad8 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
1cad9 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e if( u.af.
1cada 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 iA==0 ) goto ari
1cadb 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 thmetic_result_i
1cadc 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 s_null;.
1cadd 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 2d 31 20 if( u.af.iA==-1
1cade 29 20 75 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20 ) u.af.iA = 1;.
1cadf 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20 25 u.af.iB %
1cae0 3d 20 75 2e 61 66 2e 69 41 3b 0a 20 20 20 20 20 = u.af.iA;.
1cae1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1cae2 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 }. }. pOut
1cae3 2d 3e 75 2e 69 20 3d 20 75 2e 61 66 2e 69 42 3b ->u.i = u.af.iB;
1cae4 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 . MemSetTypeF
1cae5 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e lag(pOut, MEM_In
1cae6 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 t);. }else{.
1cae7 20 75 2e 61 66 2e 72 41 20 3d 20 73 71 6c 69 74 u.af.rA = sqlit
1cae8 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 e3VdbeRealValue(
1cae9 70 49 6e 31 29 3b 0a 20 20 20 20 75 2e 61 66 2e pIn1);. u.af.
1caea 72 42 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rB = sqlite3Vdbe
1caeb 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 32 29 3b RealValue(pIn2);
1caec 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 . switch( pOp
1caed 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 ->opcode ){.
1caee 20 20 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 case OP_Add:
1caef 20 20 20 20 20 20 20 75 2e 61 66 2e 72 42 20 2b u.af.rB +
1caf0 3d 20 75 2e 61 66 2e 72 41 3b 20 20 20 20 20 20 = u.af.rA;
1caf1 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 break;. ca
1caf2 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20 se OP_Subtract:
1caf3 20 20 20 75 2e 61 66 2e 72 42 20 2d 3d 20 75 2e u.af.rB -= u.
1caf4 61 66 2e 72 41 3b 20 20 20 20 20 20 20 62 72 65 af.rA; bre
1caf5 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f ak;. case O
1caf6 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 75 P_Multiply: u
1caf7 2e 61 66 2e 72 42 20 2a 3d 20 75 2e 61 66 2e 72 .af.rB *= u.af.r
1caf8 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a A; break;.
1caf9 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 44 69 case OP_Di
1cafa 76 69 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 vide: {.
1cafb 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 /* (double)0 In
1cafc 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f case of SQLITE_O
1cafd 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
1cafe 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 NT... */.
1caff 20 69 66 28 20 75 2e 61 66 2e 72 41 3d 3d 28 64 if( u.af.rA==(d
1cb00 6f 75 62 6c 65 29 30 20 29 20 67 6f 74 6f 20 61 ouble)0 ) goto a
1cb01 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 rithmetic_result
1cb02 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 _is_null;.
1cb03 20 20 75 2e 61 66 2e 72 42 20 2f 3d 20 75 2e 61 u.af.rB /= u.a
1cb04 66 2e 72 41 3b 0a 20 20 20 20 20 20 20 20 62 72 f.rA;. br
1cb05 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
1cb06 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 default: {.
1cb07 20 20 20 20 20 20 75 2e 61 66 2e 69 41 20 3d 20 u.af.iA =
1cb08 28 69 36 34 29 75 2e 61 66 2e 72 41 3b 0a 20 20 (i64)u.af.rA;.
1cb09 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20 3d 20 u.af.iB =
1cb0a 28 69 36 34 29 75 2e 61 66 2e 72 42 3b 0a 20 20 (i64)u.af.rB;.
1cb0b 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 if( u.af.i
1cb0c 41 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 A==0 ) goto arit
1cb0d 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 hmetic_result_is
1cb0e 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 _null;. i
1cb0f 66 28 20 75 2e 61 66 2e 69 41 3d 3d 2d 31 20 29 f( u.af.iA==-1 )
1cb10 20 75 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20 20 u.af.iA = 1;.
1cb11 20 20 20 20 20 20 75 2e 61 66 2e 72 42 20 3d 20 u.af.rB =
1cb12 28 64 6f 75 62 6c 65 29 28 75 2e 61 66 2e 69 42 (double)(u.af.iB
1cb13 20 25 20 75 2e 61 66 2e 69 41 29 3b 0a 20 20 20 % u.af.iA);.
1cb14 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1cb15 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
1cb16 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 75 ( sqlite3IsNaN(u
1cb17 2e 61 66 2e 72 42 29 20 29 7b 0a 20 20 20 20 20 .af.rB) ){.
1cb18 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 goto arithmetic
1cb19 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b _result_is_null;
1cb1a 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 2d . }. pOut-
1cb1b 3e 72 20 3d 20 75 2e 61 66 2e 72 42 3b 0a 20 20 >r = u.af.rB;.
1cb1c 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1cb1d 28 70 4f 75 74 2c 20 4d 45 4d 5f 52 65 61 6c 29 (pOut, MEM_Real)
1cb1e 3b 0a 20 20 20 20 69 66 28 20 28 75 2e 61 66 2e ;. if( (u.af.
1cb1f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c flags & MEM_Real
1cb20 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 )==0 ){. sq
1cb21 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 lite3VdbeInteger
1cb22 41 66 66 69 6e 69 74 79 28 70 4f 75 74 29 3b 0a Affinity(pOut);.
1cb23 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 }. }. brea
1cb24 6b 3b 0a 0a 61 72 69 74 68 6d 65 74 69 63 5f 72 k;..arithmetic_r
1cb25 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3a 0a 20 esult_is_null:.
1cb26 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1cb27 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 etNull(pOut);.
1cb28 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1cb29 6f 64 65 3a 20 43 6f 6c 6c 53 65 71 20 2a 20 2a ode: CollSeq * *
1cb2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 P4.**.** P4 is
1cb2b 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 a pointer to a C
1cb2c 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 2e 20 49 ollSeq struct. I
1cb2d 66 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 f the next call
1cb2e 74 6f 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 to a user functi
1cb2f 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67 72 65 67 61 on.** or aggrega
1cb30 74 65 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 te calls sqlite3
1cb31 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 29 GetFuncCollSeq()
1cb32 2c 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e , this collation
1cb33 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 0a 2a sequence will.*
1cb34 2a 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 54 * be returned. T
1cb35 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 his is used by t
1cb36 68 65 20 62 75 69 6c 74 2d 69 6e 20 6d 69 6e 28 he built-in min(
1cb37 29 2c 20 6d 61 78 28 29 20 61 6e 64 20 6e 75 6c ), max() and nul
1cb38 6c 69 66 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f lif().** functio
1cb39 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e ns..**.** The in
1cb3a 74 65 72 66 61 63 65 20 75 73 65 64 20 62 79 20 terface used by
1cb3b 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
1cb3c 6f 6e 20 6f 66 20 74 68 65 20 61 66 6f 72 65 6d on of the aforem
1cb3d 65 6e 74 69 6f 6e 65 64 20 66 75 6e 63 74 69 6f entioned functio
1cb3e 6e 73 0a 2a 2a 20 74 6f 20 72 65 74 72 69 65 76 ns.** to retriev
1cb3f 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 e the collation
1cb40 73 65 71 75 65 6e 63 65 20 73 65 74 20 62 79 20 sequence set by
1cb41 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 6e this opcode is n
1cb42 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 ot available.**
1cb43 70 75 62 6c 69 63 6c 79 2c 20 6f 6e 6c 79 20 74 publicly, only t
1cb44 6f 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 o user functions
1cb45 20 64 65 66 69 6e 65 64 20 69 6e 20 66 75 6e 63 defined in func
1cb46 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 .c..*/.case OP_C
1cb47 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20 61 73 73 65 ollSeq: {. asse
1cb48 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d rt( pOp->p4type=
1cb49 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 =P4_COLLSEQ );.
1cb4a 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1cb4b 63 6f 64 65 3a 20 46 75 6e 63 74 69 6f 6e 20 50 code: Function P
1cb4c 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 1 P2 P3 P4 P5.**
1cb4d 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 20 75 73 65 .** Invoke a use
1cb4e 72 20 66 75 6e 63 74 69 6f 6e 20 28 50 34 20 69 r function (P4 i
1cb4f 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
1cb50 20 46 75 6e 63 74 69 6f 6e 20 73 74 72 75 63 74 Function struct
1cb51 75 72 65 20 74 68 61 74 0a 2a 2a 20 64 65 66 69 ure that.** defi
1cb52 6e 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e nes the function
1cb53 29 20 77 69 74 68 20 50 35 20 61 72 67 75 6d 65 ) with P5 argume
1cb54 6e 74 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 nts taken from r
1cb55 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 0a 2a egister P2 and.*
1cb56 2a 20 73 75 63 63 65 73 73 6f 72 73 2e 20 20 54 * successors. T
1cb57 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 he result of the
1cb58 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 74 6f function is sto
1cb59 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 red in register
1cb5a 50 33 2e 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 P3..** Register
1cb5b 50 33 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 6f P3 must not be o
1cb5c 6e 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 ne of the functi
1cb5d 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a 2a 0a 2a 2a on inputs..**.**
1cb5e 20 50 31 20 69 73 20 61 20 33 32 2d 62 69 74 20 P1 is a 32-bit
1cb5f 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 61 74 69 bitmask indicati
1cb60 6e 67 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ng whether or no
1cb61 74 20 65 61 63 68 20 61 72 67 75 6d 65 6e 74 20 t each argument
1cb62 74 6f 20 74 68 65 20 0a 2a 2a 20 66 75 6e 63 74 to the .** funct
1cb63 69 6f 6e 20 77 61 73 20 64 65 74 65 72 6d 69 6e ion was determin
1cb64 65 64 20 74 6f 20 62 65 20 63 6f 6e 73 74 61 6e ed to be constan
1cb65 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d t at compile tim
1cb66 65 2e 20 49 66 20 74 68 65 20 66 69 72 73 74 0a e. If the first.
1cb67 2a 2a 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 ** argument was
1cb68 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 62 69 constant then bi
1cb69 74 20 30 20 6f 66 20 50 31 20 69 73 20 73 65 74 t 0 of P1 is set
1cb6a 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 . This is used t
1cb6b 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 o determine.** w
1cb6c 68 65 74 68 65 72 20 6d 65 74 61 20 64 61 74 61 hether meta data
1cb6d 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
1cb6e 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e a user function
1cb6f 20 61 72 67 75 6d 65 6e 74 20 75 73 69 6e 67 20 argument using
1cb70 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 the.** sqlite3_s
1cb71 65 74 5f 61 75 78 64 61 74 61 28 29 20 41 50 49 et_auxdata() API
1cb72 20 6d 61 79 20 62 65 20 73 61 66 65 6c 79 20 72 may be safely r
1cb73 65 74 61 69 6e 65 64 20 75 6e 74 69 6c 20 74 68 etained until th
1cb74 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 76 6f 63 61 e next.** invoca
1cb75 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6f 70 63 tion of this opc
1cb76 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 ode..**.** See a
1cb77 6c 73 6f 3a 20 41 67 67 53 74 65 70 20 61 6e 64 lso: AggStep and
1cb78 20 41 67 67 46 69 6e 61 6c 0a 2a 2f 0a 63 61 73 AggFinal.*/.cas
1cb79 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 3a 20 7b e OP_Function: {
1cb7a 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1cb7b 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1cb7c 20 69 6e 74 6f 20 75 2e 61 67 20 2a 2f 0a 20 20 into u.ag */.
1cb7d 69 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 41 int i;. Mem *pA
1cb7e 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f rg;. sqlite3_co
1cb7f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 73 71 6c ntext ctx;. sql
1cb80 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 ite3_value **apV
1cb81 61 6c 3b 0a 20 20 69 6e 74 20 6e 3b 0a 23 65 6e al;. int n;.#en
1cb82 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1cb83 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1cb84 6f 20 75 2e 61 67 20 2a 2f 0a 0a 20 20 75 2e 61 o u.ag */.. u.a
1cb85 67 2e 6e 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 g.n = pOp->p5;.
1cb86 20 75 2e 61 67 2e 61 70 56 61 6c 20 3d 20 70 2d u.ag.apVal = p-
1cb87 3e 61 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 >apArg;. assert
1cb88 28 20 75 2e 61 67 2e 61 70 56 61 6c 20 7c 7c 20 ( u.ag.apVal ||
1cb89 75 2e 61 67 2e 6e 3d 3d 30 20 29 3b 0a 0a 20 20 u.ag.n==0 );..
1cb8a 61 73 73 65 72 74 28 20 75 2e 61 67 2e 6e 3d 3d assert( u.ag.n==
1cb8b 30 20 7c 7c 20 28 70 4f 70 2d 3e 70 32 3e 30 20 0 || (pOp->p2>0
1cb8c 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2e 61 67 2e && pOp->p2+u.ag.
1cb8d 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 29 20 29 3b n<=p->nMem+1) );
1cb8e 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1cb8f 70 33 3c 70 4f 70 2d 3e 70 32 20 7c 7c 20 70 4f p3<pOp->p2 || pO
1cb90 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70 32 2b 75 p->p3>=pOp->p2+u
1cb91 2e 61 67 2e 6e 20 29 3b 0a 20 20 75 2e 61 67 2e .ag.n );. u.ag.
1cb92 70 41 72 67 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pArg = &p->aMem[
1cb93 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 66 6f 72 28 pOp->p2];. for(
1cb94 75 2e 61 67 2e 69 3d 30 3b 20 75 2e 61 67 2e 69 u.ag.i=0; u.ag.i
1cb95 3c 75 2e 61 67 2e 6e 3b 20 75 2e 61 67 2e 69 2b <u.ag.n; u.ag.i+
1cb96 2b 2c 20 75 2e 61 67 2e 70 41 72 67 2b 2b 29 7b +, u.ag.pArg++){
1cb97 0a 20 20 20 20 75 2e 61 67 2e 61 70 56 61 6c 5b . u.ag.apVal[
1cb98 75 2e 61 67 2e 69 5d 20 3d 20 75 2e 61 67 2e 70 u.ag.i] = u.ag.p
1cb99 41 72 67 3b 0a 20 20 20 20 73 74 6f 72 65 54 79 Arg;. storeTy
1cb9a 70 65 49 6e 66 6f 28 75 2e 61 67 2e 70 41 72 67 peInfo(u.ag.pArg
1cb9b 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 , encoding);.
1cb9c 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1cb9d 70 4f 70 2d 3e 70 32 2c 20 75 2e 61 67 2e 70 41 pOp->p2, u.ag.pA
1cb9e 72 67 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 rg);. }.. asse
1cb9f 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d rt( pOp->p4type=
1cba0 3d 50 34 5f 46 55 4e 43 44 45 46 20 7c 7c 20 70 =P4_FUNCDEF || p
1cba1 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 Op->p4type==P4_V
1cba2 44 42 45 46 55 4e 43 20 29 3b 0a 20 20 69 66 28 DBEFUNC );. if(
1cba3 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 pOp->p4type==P4
1cba4 5f 46 55 4e 43 44 45 46 20 29 7b 0a 20 20 20 20 _FUNCDEF ){.
1cba5 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 3d u.ag.ctx.pFunc =
1cba6 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a pOp->p4.pFunc;.
1cba7 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 u.ag.ctx.pVd
1cba8 62 65 46 75 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 beFunc = 0;. }e
1cba9 6c 73 65 7b 0a 20 20 20 20 75 2e 61 67 2e 63 74 lse{. u.ag.ct
1cbaa 78 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 28 56 x.pVdbeFunc = (V
1cbab 64 62 65 46 75 6e 63 2a 29 70 4f 70 2d 3e 70 34 dbeFunc*)pOp->p4
1cbac 2e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 .pVdbeFunc;.
1cbad 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 3d u.ag.ctx.pFunc =
1cbae 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 u.ag.ctx.pVdbeF
1cbaf 75 6e 63 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a unc->pFunc;. }.
1cbb0 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1cbb1 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c p3>0 && pOp->p3<
1cbb2 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f =p->nMem );. pO
1cbb3 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ut = &p->aMem[pO
1cbb4 70 2d 3e 70 33 5d 3b 0a 20 20 75 2e 61 67 2e 63 p->p3];. u.ag.c
1cbb5 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d tx.s.flags = MEM
1cbb6 5f 4e 75 6c 6c 3b 0a 20 20 75 2e 61 67 2e 63 74 _Null;. u.ag.ct
1cbb7 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20 75 x.s.db = db;. u
1cbb8 2e 61 67 2e 63 74 78 2e 73 2e 78 44 65 6c 20 3d .ag.ctx.s.xDel =
1cbb9 20 30 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73 0;. u.ag.ctx.s
1cbba 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 0a 20 .zMalloc = 0;..
1cbbb 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 /* The output c
1cbbc 65 6c 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 ell may already
1cbbd 68 61 76 65 20 61 20 62 75 66 66 65 72 20 61 6c have a buffer al
1cbbe 6c 6f 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 located. Move.
1cbbf 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 ** the pointer t
1cbc0 6f 20 75 2e 61 67 2e 63 74 78 2e 73 20 73 6f 20 o u.ag.ctx.s so
1cbc1 69 6e 20 63 61 73 65 20 74 68 65 20 75 73 65 72 in case the user
1cbc2 2d 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 75 73 -function can us
1cbc3 65 0a 20 20 2a 2a 20 74 68 65 20 61 6c 72 65 61 e. ** the alrea
1cbc4 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 dy allocated buf
1cbc5 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 fer instead of a
1cbc6 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 llocating a new
1cbc7 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 one.. */. sqli
1cbc8 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 te3VdbeMemMove(&
1cbc9 75 2e 61 67 2e 63 74 78 2e 73 2c 20 70 4f 75 74 u.ag.ctx.s, pOut
1cbca 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 );. MemSetTypeF
1cbcb 6c 61 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 2c lag(&u.ag.ctx.s,
1cbcc 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 75 MEM_Null);.. u
1cbcd 2e 61 67 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 .ag.ctx.isError
1cbce 3d 20 30 3b 0a 20 20 69 66 28 20 75 2e 61 67 2e = 0;. if( u.ag.
1cbcf 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 ctx.pFunc->flags
1cbd0 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e & SQLITE_FUNC_N
1cbd1 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 EEDCOLL ){. a
1cbd2 73 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f ssert( pOp>p->aO
1cbd3 70 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 p );. assert(
1cbd4 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 3d pOp[-1].p4type=
1cbd5 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 =P4_COLLSEQ );.
1cbd6 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d assert( pOp[-
1cbd7 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 1].opcode==OP_Co
1cbd8 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 75 2e 61 llSeq );. u.a
1cbd9 67 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f g.ctx.pColl = pO
1cbda 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a p[-1].p4.pColl;.
1cbdb 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
1cbdc 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 3SafetyOff(db) )
1cbdd 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1cbde 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 28 2a 75 to_misuse;. (*u
1cbdf 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 2d 3e 78 .ag.ctx.pFunc->x
1cbe0 46 75 6e 63 29 28 26 75 2e 61 67 2e 63 74 78 2c Func)(&u.ag.ctx,
1cbe1 20 75 2e 61 67 2e 6e 2c 20 75 2e 61 67 2e 61 70 u.ag.n, u.ag.ap
1cbe2 56 61 6c 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 Val);. if( sqli
1cbe3 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 te3SafetyOn(db)
1cbe4 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1cbe5 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e beMemRelease(&u.
1cbe6 61 67 2e 63 74 78 2e 73 29 3b 0a 20 20 20 20 67 ag.ctx.s);. g
1cbe7 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1cbe8 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20 69 _misuse;. }. i
1cbe9 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
1cbea 6c 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 45 76 led ){. /* Ev
1cbeb 65 6e 20 74 68 6f 75 67 68 20 61 20 6d 61 6c 6c en though a mall
1cbec 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65 64 2c oc() has failed,
1cbed 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
1cbee 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 20 20 2a ion of the. *
1cbef 2a 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 * user function
1cbf0 6d 61 79 20 68 61 76 65 20 63 61 6c 6c 65 64 20 may have called
1cbf1 61 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c an sqlite3_resul
1cbf2 74 5f 58 58 58 28 29 20 66 75 6e 63 74 69 6f 6e t_XXX() function
1cbf3 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 74 75 72 . ** to retur
1cbf4 6e 20 61 20 76 61 6c 75 65 2e 20 54 68 65 20 66 n a value. The f
1cbf5 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 72 65 ollowing call re
1cbf6 6c 65 61 73 65 73 20 61 6e 79 20 72 65 73 6f 75 leases any resou
1cbf7 72 63 65 73 0a 20 20 20 20 2a 2a 20 61 73 73 6f rces. ** asso
1cbf8 63 69 61 74 65 64 20 77 69 74 68 20 73 75 63 68 ciated with such
1cbf9 20 61 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2a a value.. **
1cbfa 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 4d 61 . ** Note: Ma
1cbfb 79 62 65 20 4d 65 6d 52 65 6c 65 61 73 65 28 29 ybe MemRelease()
1cbfc 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 should be calle
1cbfd 64 20 69 66 20 73 71 6c 69 74 65 33 53 61 66 65 d if sqlite3Safe
1cbfe 74 79 4f 6e 28 29 0a 20 20 20 20 2a 2a 20 66 61 tyOn(). ** fa
1cbff 69 6c 73 20 61 6c 73 6f 20 28 74 68 65 20 69 66 ils also (the if
1cc00 28 2e 2e 2e 29 20 73 74 61 74 65 6d 65 6e 74 20 (...) statement
1cc01 61 62 6f 76 65 29 2e 20 42 75 74 20 69 66 20 70 above). But if p
1cc02 65 6f 70 6c 65 20 61 72 65 0a 20 20 20 20 2a 2a eople are. **
1cc03 20 6d 69 73 75 73 69 6e 67 20 73 71 6c 69 74 65 misusing sqlite
1cc04 2c 20 74 68 65 79 20 68 61 76 65 20 62 69 67 67 , they have bigg
1cc05 65 72 20 70 72 6f 62 6c 65 6d 73 20 74 68 61 6e er problems than
1cc06 20 61 20 6c 65 61 6b 65 64 20 76 61 6c 75 65 2e a leaked value.
1cc07 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 . */. sqli
1cc08 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
1cc09 65 28 26 75 2e 61 67 2e 63 74 78 2e 73 29 3b 0a e(&u.ag.ctx.s);.
1cc0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b goto no_mem;
1cc0b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e . }.. /* If an
1cc0c 79 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 y auxiliary data
1cc0d 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 functions have
1cc0e 62 65 65 6e 20 63 61 6c 6c 65 64 20 62 79 20 74 been called by t
1cc0f 68 69 73 20 75 73 65 72 20 66 75 6e 63 74 69 6f his user functio
1cc10 6e 2c 0a 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 n,. ** immediat
1cc11 65 6c 79 20 63 61 6c 6c 20 74 68 65 20 64 65 73 ely call the des
1cc12 74 72 75 63 74 6f 72 20 66 6f 72 20 61 6e 79 20 tructor for any
1cc13 6e 6f 6e 2d 73 74 61 74 69 63 20 76 61 6c 75 65 non-static value
1cc14 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 75 2e s.. */. if( u.
1cc15 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63 ag.ctx.pVdbeFunc
1cc16 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1cc17 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 dbeDeleteAuxData
1cc18 28 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 (u.ag.ctx.pVdbeF
1cc19 75 6e 63 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 unc, pOp->p1);.
1cc1a 20 20 20 70 4f 70 2d 3e 70 34 2e 70 56 64 62 65 pOp->p4.pVdbe
1cc1b 46 75 6e 63 20 3d 20 75 2e 61 67 2e 63 74 78 2e Func = u.ag.ctx.
1cc1c 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 70 pVdbeFunc;. p
1cc1d 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f Op->p4type = P4_
1cc1e 56 44 42 45 46 55 4e 43 3b 0a 20 20 7d 0a 0a 20 VDBEFUNC;. }..
1cc1f 20 2f 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 /* If the funct
1cc20 69 6f 6e 20 72 65 74 75 72 6e 65 64 20 61 6e 20 ion returned an
1cc21 65 72 72 6f 72 2c 20 74 68 72 6f 77 20 61 6e 20 error, throw an
1cc22 65 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20 20 69 exception */. i
1cc23 66 28 20 75 2e 61 67 2e 63 74 78 2e 69 73 45 72 f( u.ag.ctx.isEr
1cc24 72 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ror ){. sqlit
1cc25 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1cc26 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 zErrMsg, db, "%s
1cc27 22 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ", sqlite3_value
1cc28 5f 74 65 78 74 28 26 75 2e 61 67 2e 63 74 78 2e _text(&u.ag.ctx.
1cc29 73 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e s));. rc = u.
1cc2a 61 67 2e 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a ag.ctx.isError;.
1cc2b 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 }.. /* Copy t
1cc2c 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 he result of the
1cc2d 20 66 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 72 function into r
1cc2e 65 67 69 73 74 65 72 20 50 33 20 2a 2f 0a 20 20 egister P3 */.
1cc2f 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1cc30 65 45 6e 63 6f 64 69 6e 67 28 26 75 2e 61 67 2e eEncoding(&u.ag.
1cc31 63 74 78 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 ctx.s, encoding)
1cc32 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
1cc33 65 6d 4d 6f 76 65 28 70 4f 75 74 2c 20 26 75 2e emMove(pOut, &u.
1cc34 61 67 2e 63 74 78 2e 73 29 3b 0a 20 20 69 66 28 ag.ctx.s);. if(
1cc35 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 sqlite3VdbeMemT
1cc36 6f 6f 42 69 67 28 70 4f 75 74 29 20 29 7b 0a 20 ooBig(pOut) ){.
1cc37 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b goto too_big;
1cc38 0a 20 20 7d 0a 20 20 52 45 47 49 53 54 45 52 5f . }. REGISTER_
1cc39 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 TRACE(pOp->p3, p
1cc3a 4f 75 74 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d Out);. UPDATE_M
1cc3b 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 AX_BLOBSIZE(pOut
1cc3c 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f );. break;.}../
1cc3d 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 41 6e 64 * Opcode: BitAnd
1cc3e 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1cc3f 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 62 69 74 .** Take the bit
1cc40 2d 77 69 73 65 20 41 4e 44 20 6f 66 20 74 68 65 -wise AND of the
1cc41 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 values in regis
1cc42 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e ter P1 and P2 an
1cc43 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72 d.** store the r
1cc44 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 esult in registe
1cc45 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 r P3..** If eith
1cc46 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c er input is NULL
1cc47 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 , the result is
1cc48 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f NULL..*/./* Opco
1cc49 64 65 3a 20 42 69 74 4f 72 20 50 31 20 50 32 20 de: BitOr P1 P2
1cc4a 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b P3 * *.**.** Tak
1cc4b 65 20 74 68 65 20 62 69 74 2d 77 69 73 65 20 4f e the bit-wise O
1cc4c 52 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 R of the values
1cc4d 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 61 in register P1 a
1cc4e 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f nd P2 and.** sto
1cc4f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e re the result in
1cc50 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a register P3..**
1cc51 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 If either input
1cc52 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 is NULL, the re
1cc53 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f sult is NULL..*/
1cc54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69 66 ./* Opcode: Shif
1cc55 74 4c 65 66 74 20 50 31 20 50 32 20 50 33 20 2a tLeft P1 P2 P3 *
1cc56 20 2a 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 *.**.** Shift t
1cc57 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 he integer value
1cc58 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 in register P2
1cc59 74 6f 20 74 68 65 20 6c 65 66 74 20 62 79 20 74 to the left by t
1cc5a 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 he.** number of
1cc5b 62 69 74 73 20 73 70 65 63 69 66 69 65 64 20 62 bits specified b
1cc5c 79 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e y the integer in
1cc5d 20 72 65 67 69 73 65 72 20 50 31 2e 0a 2a 2a 20 regiser P1..**
1cc5e 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 Store the result
1cc5f 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e in register P3.
1cc60 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e .** If either in
1cc61 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 put is NULL, the
1cc62 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e result is NULL.
1cc63 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 .*/./* Opcode: S
1cc64 68 69 66 74 52 69 67 68 74 20 50 31 20 50 32 20 hiftRight P1 P2
1cc65 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 68 69 P3 * *.**.** Shi
1cc66 66 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 ft the integer v
1cc67 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1cc68 20 50 32 20 74 6f 20 74 68 65 20 72 69 67 68 74 P2 to the right
1cc69 20 62 79 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 by the.** numbe
1cc6a 72 20 6f 66 20 62 69 74 73 20 73 70 65 63 69 66 r of bits specif
1cc6b 69 65 64 20 62 79 20 74 68 65 20 69 6e 74 65 67 ied by the integ
1cc6c 65 72 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 er in register P
1cc6d 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 1..** Store the
1cc6e 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 result in regist
1cc6f 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 er P3..** If eit
1cc70 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c her input is NUL
1cc71 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 L, the result is
1cc72 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f NULL..*/.case O
1cc73 50 5f 42 69 74 41 6e 64 3a 20 20 20 20 20 20 20 P_BitAnd:
1cc74 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1cc75 65 20 61 73 20 54 4b 5f 42 49 54 41 4e 44 2c 20 e as TK_BITAND,
1cc76 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a in1, in2, out3 *
1cc77 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74 4f 72 3a /.case OP_BitOr:
1cc78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cc79 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1cc7a 42 49 54 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c BITOR, in1, in2,
1cc7b 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 out3 */.case OP
1cc7c 5f 53 68 69 66 74 4c 65 66 74 3a 20 20 20 20 20 _ShiftLeft:
1cc7d 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1cc7e 20 61 73 20 54 4b 5f 4c 53 48 49 46 54 2c 20 69 as TK_LSHIFT, i
1cc7f 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f n1, in2, out3 */
1cc80 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 74 52 69 .case OP_ShiftRi
1cc81 67 68 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 ght: {
1cc82 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 /* same as TK_R
1cc83 53 48 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c SHIFT, in1, in2,
1cc84 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 out3 */.#if 0
1cc85 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1cc86 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1cc87 61 68 20 2a 2f 0a 20 20 69 36 34 20 61 3b 0a 20 ah */. i64 a;.
1cc88 20 69 36 34 20 62 3b 0a 23 65 6e 64 69 66 20 2f i64 b;.#endif /
1cc89 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1cc8a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1cc8b 68 20 2a 2f 0a 0a 20 20 69 66 28 20 28 70 49 6e h */.. if( (pIn
1cc8c 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d 1->flags | pIn2-
1cc8d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d 5f 4e 75 >flags) & MEM_Nu
1cc8e 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ll ){. sqlite
1cc8f 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 3VdbeMemSetNull(
1cc90 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 65 61 6b pOut);. break
1cc91 3b 0a 20 20 7d 0a 20 20 75 2e 61 68 2e 61 20 3d ;. }. u.ah.a =
1cc92 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 sqlite3VdbeIntV
1cc93 61 6c 75 65 28 70 49 6e 32 29 3b 0a 20 20 75 2e alue(pIn2);. u.
1cc94 61 68 2e 62 20 3d 20 73 71 6c 69 74 65 33 56 64 ah.b = sqlite3Vd
1cc95 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 beIntValue(pIn1)
1cc96 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d ;. switch( pOp-
1cc97 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 63 >opcode ){. c
1cc98 61 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 20 ase OP_BitAnd:
1cc99 20 20 20 20 75 2e 61 68 2e 61 20 26 3d 20 75 2e u.ah.a &= u.
1cc9a 61 68 2e 62 3b 20 20 20 20 20 62 72 65 61 6b 3b ah.b; break;
1cc9b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42 69 74 . case OP_Bit
1cc9c 4f 72 3a 20 20 20 20 20 20 20 75 2e 61 68 2e 61 Or: u.ah.a
1cc9d 20 7c 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 20 |= u.ah.b;
1cc9e 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
1cc9f 4f 50 5f 53 68 69 66 74 4c 65 66 74 3a 20 20 20 OP_ShiftLeft:
1cca0 75 2e 61 68 2e 61 20 3c 3c 3d 20 75 2e 61 68 2e u.ah.a <<= u.ah.
1cca1 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 b; break;.
1cca2 20 64 65 66 61 75 6c 74 3a 20 20 61 73 73 65 72 default: asser
1cca3 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d t( pOp->opcode==
1cca4 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b OP_ShiftRight );
1cca5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1cca6 20 20 20 20 20 20 20 20 20 20 75 2e 61 68 2e 61 u.ah.a
1cca7 20 3e 3e 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 >>= u.ah.b;
1cca8 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 70 4f 75 break;. }. pOu
1cca9 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 68 2e 61 3b t->u.i = u.ah.a;
1ccaa 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
1ccab 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 g(pOut, MEM_Int)
1ccac 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1ccad 20 4f 70 63 6f 64 65 3a 20 41 64 64 49 6d 6d 20 Opcode: AddImm
1ccae 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 P1 P2 * * *.**
1ccaf 0a 2a 2a 20 41 64 64 20 74 68 65 20 63 6f 6e 73 .** Add the cons
1ccb0 74 61 6e 74 20 50 32 20 74 6f 20 74 68 65 20 76 tant P2 to the v
1ccb1 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1ccb2 20 50 31 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75 P1..** The resu
1ccb3 6c 74 20 69 73 20 61 6c 77 61 79 73 20 61 6e 20 lt is always an
1ccb4 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 integer..**.** T
1ccb5 6f 20 66 6f 72 63 65 20 61 6e 79 20 72 65 67 69 o force any regi
1ccb6 73 74 65 72 20 74 6f 20 62 65 20 61 6e 20 69 6e ster to be an in
1ccb7 74 65 67 65 72 2c 20 6a 75 73 74 20 61 64 64 20 teger, just add
1ccb8 30 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 0..*/.case OP_Ad
1ccb9 64 49 6d 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 dImm: {
1ccba 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 73 /* in1 */. s
1ccbb 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 qlite3VdbeMemInt
1ccbc 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 egerify(pIn1);.
1ccbd 20 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f pIn1->u.i += pO
1ccbe 70 2d 3e 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a p->p2;. break;.
1ccbf 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 }../* Opcode: Mu
1ccc0 73 74 42 65 49 6e 74 20 50 31 20 50 32 20 2a 20 stBeInt P1 P2 *
1ccc1 2a 20 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65 * *.** .** Force
1ccc2 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1ccc3 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 gister P1 to be
1ccc4 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66 20 an integer. If
1ccc5 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 the value.** in
1ccc6 50 31 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 P1 is not an int
1ccc7 65 67 65 72 20 61 6e 64 20 63 61 6e 6e 6f 74 20 eger and cannot
1ccc8 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 be converted int
1ccc9 6f 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 o an integer.**
1ccca 77 69 74 68 6f 75 74 20 64 61 74 61 20 6c 6f 73 without data los
1cccb 73 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d s, then jump imm
1cccc 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2c 20 ediately to P2,
1cccd 6f 72 20 69 66 20 50 32 3d 3d 30 0a 2a 2a 20 72 or if P2==0.** r
1ccce 61 69 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 4d aise an SQLITE_M
1cccf 49 53 4d 41 54 43 48 20 65 78 63 65 70 74 69 6f ISMATCH exceptio
1ccd0 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 n..*/.case OP_Mu
1ccd1 73 74 42 65 49 6e 74 3a 20 7b 20 20 20 20 20 20 stBeInt: {
1ccd2 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 /* jump, i
1ccd3 6e 31 20 2a 2f 0a 20 20 61 70 70 6c 79 41 66 66 n1 */. applyAff
1ccd4 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 inity(pIn1, SQLI
1ccd5 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20 TE_AFF_NUMERIC,
1ccd6 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 69 66 28 encoding);. if(
1ccd7 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 (pIn1->flags &
1ccd8 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 MEM_Int)==0 ){.
1ccd9 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d if( pOp->p2==
1ccda 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
1ccdb 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 3b SQLITE_MISMATCH;
1ccdc 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 . goto abor
1ccdd 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1ccde 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1ccdf 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1cce0 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 1;. }. }else
1cce1 7b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 {. MemSetType
1cce2 46 6c 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 49 Flag(pIn1, MEM_I
1cce3 6e 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b nt);. }. break
1cce4 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1cce5 52 65 61 6c 41 66 66 69 6e 69 74 79 20 50 31 20 RealAffinity P1
1cce6 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 * * * *.**.** If
1cce7 20 72 65 67 69 73 74 65 72 20 50 31 20 68 6f 6c register P1 hol
1cce8 64 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f ds an integer co
1cce9 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 72 65 nvert it to a re
1ccea 61 6c 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 al value..**.**
1cceb 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75 This opcode is u
1ccec 73 65 64 20 77 68 65 6e 20 65 78 74 72 61 63 74 sed when extract
1cced 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ing information
1ccee 66 72 6f 6d 20 61 20 63 6f 6c 75 6d 6e 20 74 68 from a column th
1ccef 61 74 0a 2a 2a 20 68 61 73 20 52 45 41 4c 20 61 at.** has REAL a
1ccf0 66 66 69 6e 69 74 79 2e 20 20 53 75 63 68 20 63 ffinity. Such c
1ccf1 6f 6c 75 6d 6e 20 76 61 6c 75 65 73 20 6d 61 79 olumn values may
1ccf2 20 73 74 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 still be stored
1ccf3 20 61 73 0a 2a 2a 20 69 6e 74 65 67 65 72 73 2c as.** integers,
1ccf4 20 66 6f 72 20 73 70 61 63 65 20 65 66 66 69 63 for space effic
1ccf5 69 65 6e 63 79 2c 20 62 75 74 20 61 66 74 65 72 iency, but after
1ccf6 20 65 78 74 72 61 63 74 69 6f 6e 20 77 65 20 77 extraction we w
1ccf7 61 6e 74 20 74 68 65 6d 0a 2a 2a 20 74 6f 20 68 ant them.** to h
1ccf8 61 76 65 20 6f 6e 6c 79 20 61 20 72 65 61 6c 20 ave only a real
1ccf9 76 61 6c 75 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f value..*/.case O
1ccfa 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 3a 20 P_RealAffinity:
1ccfb 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
1ccfc 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 69 /* in1 */. i
1ccfd 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 f( pIn1->flags &
1ccfe 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 MEM_Int ){.
1ccff 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
1cd00 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d alify(pIn1);. }
1cd01 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 . break;.}..#if
1cd02 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1cd03 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a _CAST./* Opcode:
1cd04 20 54 6f 54 65 78 74 20 50 31 20 2a 20 2a 20 2a ToText P1 * * *
1cd05 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 *.**.** Force t
1cd06 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1cd07 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 74 65 ster P1 to be te
1cd08 78 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 xt..** If the va
1cd09 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 lue is numeric,
1cd0a 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 convert it to a
1cd0b 73 74 72 69 6e 67 20 75 73 69 6e 67 20 74 68 65 string using the
1cd0c 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f .** equivalent o
1cd0d 66 20 70 72 69 6e 74 66 28 29 2e 20 20 42 6c 6f f printf(). Blo
1cd0e 62 20 76 61 6c 75 65 73 20 61 72 65 20 75 6e 63 b values are unc
1cd0f 68 61 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61 72 hanged and.** ar
1cd10 65 20 61 66 74 65 72 77 61 72 64 73 20 73 69 6d e afterwards sim
1cd11 70 6c 79 20 69 6e 74 65 72 70 72 65 74 65 64 20 ply interpreted
1cd12 61 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 41 as text..**.** A
1cd13 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e NULL value is n
1cd14 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 ot changed by th
1cd15 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 is routine. It
1cd16 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f remains NULL..*/
1cd17 0a 63 61 73 65 20 4f 50 5f 54 6f 54 65 78 74 3a .case OP_ToText:
1cd18 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
1cd19 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1cd1a 4b 5f 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20 2a K_TO_TEXT, in1 *
1cd1b 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c /. if( pIn1->fl
1cd1c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 ags & MEM_Null )
1cd1d 20 62 72 65 61 6b 3b 0a 20 20 61 73 73 65 72 74 break;. assert
1cd1e 28 20 4d 45 4d 5f 53 74 72 3d 3d 28 4d 45 4d 5f ( MEM_Str==(MEM_
1cd1f 42 6c 6f 62 3e 3e 33 29 20 29 3b 0a 20 20 70 49 Blob>>3) );. pI
1cd20 6e 31 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 49 n1->flags |= (pI
1cd21 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c n1->flags&MEM_Bl
1cd22 6f 62 29 3e 3e 33 3b 0a 20 20 61 70 70 6c 79 41 ob)>>3;. applyA
1cd23 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 ffinity(pIn1, SQ
1cd24 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 LITE_AFF_TEXT, e
1cd25 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 72 63 20 3d ncoding);. rc =
1cd26 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 ExpandBlob(pIn1
1cd27 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e );. assert( pIn
1cd28 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 1->flags & MEM_S
1cd29 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 tr || db->malloc
1cd2a 46 61 69 6c 65 64 20 29 3b 0a 20 20 70 49 6e 31 Failed );. pIn1
1cd2b 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d ->flags &= ~(MEM
1cd2c 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 _Int|MEM_Real|ME
1cd2d 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 29 M_Blob|MEM_Zero)
1cd2e 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 ;. UPDATE_MAX_B
1cd2f 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 LOBSIZE(pIn1);.
1cd30 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1cd31 63 6f 64 65 3a 20 54 6f 42 6c 6f 62 20 50 31 20 code: ToBlob P1
1cd32 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f * * * *.**.** Fo
1cd33 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e rce the value in
1cd34 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 register P1 to
1cd35 62 65 20 61 20 42 4c 4f 42 2e 0a 2a 2a 20 49 66 be a BLOB..** If
1cd36 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75 the value is nu
1cd37 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72 74 20 69 meric, convert i
1cd38 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 66 69 t to a string fi
1cd39 72 73 74 2e 0a 2a 2a 20 53 74 72 69 6e 67 73 20 rst..** Strings
1cd3a 61 72 65 20 73 69 6d 70 6c 79 20 72 65 69 6e 74 are simply reint
1cd3b 65 72 70 72 65 74 65 64 20 61 73 20 62 6c 6f 62 erpreted as blob
1cd3c 73 20 77 69 74 68 20 6e 6f 20 63 68 61 6e 67 65 s with no change
1cd3d 0a 2a 2a 20 74 6f 20 74 68 65 20 75 6e 64 65 72 .** to the under
1cd3e 6c 79 69 6e 67 20 64 61 74 61 2e 0a 2a 2a 0a 2a lying data..**.*
1cd3f 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 * A NULL value i
1cd40 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 s not changed by
1cd41 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 this routine.
1cd42 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e It remains NULL.
1cd43 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 42 6c .*/.case OP_ToBl
1cd44 6f 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 ob: {
1cd45 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1cd46 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 2c 20 69 6e s TK_TO_BLOB, in
1cd47 31 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 1 */. if( pIn1-
1cd48 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
1cd49 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 69 66 28 l ) break;. if(
1cd4a 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 (pIn1->flags &
1cd4b 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 7b 0a MEM_Blob)==0 ){.
1cd4c 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 applyAffinit
1cd4d 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 y(pIn1, SQLITE_A
1cd4e 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e FF_TEXT, encodin
1cd4f 67 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 g);. assert(
1cd50 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1cd51 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c M_Str || db->mal
1cd52 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 locFailed );.
1cd53 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
1cd54 70 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b pIn1, MEM_Blob);
1cd55 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 . }else{. pI
1cd56 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d n1->flags &= ~(M
1cd57 45 4d 5f 54 79 70 65 4d 61 73 6b 26 7e 4d 45 4d EM_TypeMask&~MEM
1cd58 5f 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20 20 55 50 _Blob);. }. UP
1cd59 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a DATE_MAX_BLOBSIZ
1cd5a 45 28 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b E(pIn1);. break
1cd5b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1cd5c 54 6f 4e 75 6d 65 72 69 63 20 50 31 20 2a 20 2a ToNumeric P1 * *
1cd5d 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 * *.**.** Force
1cd5e 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1cd5f 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 gister P1 to be
1cd60 6e 75 6d 65 72 69 63 20 28 65 69 74 68 65 72 20 numeric (either
1cd61 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 72 an.** integer or
1cd62 20 61 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e a floating-poin
1cd63 74 20 6e 75 6d 62 65 72 2e 29 0a 2a 2a 20 49 66 t number.).** If
1cd64 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 the value is te
1cd65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 xt or blob, try
1cd66 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f to convert it to
1cd67 20 61 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a an using the.**
1cd68 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 equivalent of a
1cd69 74 6f 69 28 29 20 6f 72 20 61 74 6f 66 28 29 20 toi() or atof()
1cd6a 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e and store 0 if n
1cd6b 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f o such conversio
1cd6c 6e 20 0a 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c n .** is possibl
1cd6d 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 e..**.** A NULL
1cd6e 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 value is not cha
1cd6f 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 nged by this rou
1cd70 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e tine. It remain
1cd71 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 s NULL..*/.case
1cd72 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 3a 20 7b 20 OP_ToNumeric: {
1cd73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cd74 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 /* same as TK_T
1cd75 4f 5f 4e 55 4d 45 52 49 43 2c 20 69 6e 31 20 2a O_NUMERIC, in1 *
1cd76 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 /. if( (pIn1->f
1cd77 6c 61 67 73 20 26 20 28 4d 45 4d 5f 4e 75 6c 6c lags & (MEM_Null
1cd78 7c 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 |MEM_Int|MEM_Rea
1cd79 6c 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 l))==0 ){. sq
1cd7a 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 lite3VdbeMemNume
1cd7b 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a rify(pIn1);. }.
1cd7c 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 break;.}.#endi
1cd7d 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1cd7e 5f 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 _CAST */../* Opc
1cd7f 6f 64 65 3a 20 54 6f 49 6e 74 20 50 31 20 2a 20 ode: ToInt P1 *
1cd80 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 * * *.**.** Forc
1cd81 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 e the value in r
1cd82 65 67 69 73 74 65 72 20 50 31 20 62 65 20 61 6e egister P1 be an
1cd83 20 69 6e 74 65 67 65 72 2e 20 20 49 66 0a 2a 2a integer. If.**
1cd84 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 63 75 The value is cu
1cd85 72 72 65 6e 74 6c 79 20 61 20 72 65 61 6c 20 6e rrently a real n
1cd86 75 6d 62 65 72 2c 20 64 72 6f 70 20 69 74 73 20 umber, drop its
1cd87 66 72 61 63 74 69 6f 6e 61 6c 20 70 61 72 74 2e fractional part.
1cd88 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 .** If the value
1cd89 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 is text or blob
1cd8a 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 , try to convert
1cd8b 20 69 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 it to an intege
1cd8c 72 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 r using the.** e
1cd8d 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f quivalent of ato
1cd8e 69 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 i() and store 0
1cd8f 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 if no such conve
1cd90 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c rsion is possibl
1cd91 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 e..**.** A NULL
1cd92 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 value is not cha
1cd93 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 nged by this rou
1cd94 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e tine. It remain
1cd95 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 s NULL..*/.case
1cd96 4f 50 5f 54 6f 49 6e 74 3a 20 7b 20 20 20 20 20 OP_ToInt: {
1cd97 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1cd98 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e same as TK_TO_IN
1cd99 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 T, in1 */. if(
1cd9a 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d (pIn1->flags & M
1cd9b 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 EM_Null)==0 ){.
1cd9c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1cd9d 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31 mIntegerify(pIn1
1cd9e 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a );. }. break;.
1cd9f 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
1cda0 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f E_OMIT_CAST./* O
1cda1 70 63 6f 64 65 3a 20 54 6f 52 65 61 6c 20 50 31 pcode: ToReal P1
1cda2 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 * * * *.**.** F
1cda3 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 orce the value i
1cda4 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f n register P1 to
1cda5 20 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 be a floating p
1cda6 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 oint number..**
1cda7 49 66 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 If The value is
1cda8 63 75 72 72 65 6e 74 6c 79 20 61 6e 20 69 6e 74 currently an int
1cda9 65 67 65 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 eger, convert it
1cdaa 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 ..** If the valu
1cdab 65 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f e is text or blo
1cdac 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 b, try to conver
1cdad 74 20 69 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 t it to an integ
1cdae 65 72 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 er using the.**
1cdaf 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 equivalent of at
1cdb0 6f 69 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 oi() and store 0
1cdb1 2e 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f .0 if no such co
1cdb2 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 nversion is poss
1cdb3 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 ible..**.** A NU
1cdb4 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 LL value is not
1cdb5 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 changed by this
1cdb6 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d routine. It rem
1cdb7 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 ains NULL..*/.ca
1cdb8 73 65 20 4f 50 5f 54 6f 52 65 61 6c 3a 20 7b 20 se OP_ToReal: {
1cdb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cdba 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 /* same as TK_T
1cdbb 4f 5f 52 45 41 4c 2c 20 69 6e 31 20 2a 2f 0a 20 O_REAL, in1 */.
1cdbc 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 if( (pIn1->flag
1cdbd 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 s & MEM_Null)==0
1cdbe 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1cdbf 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 dbeMemRealify(pI
1cdc0 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b n1);. }. break
1cdc1 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
1cdc2 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a LITE_OMIT_CAST *
1cdc3 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 74 /../* Opcode: Lt
1cdc4 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a P1 P2 P3 P4 P5.
1cdc5 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 **.** Compare th
1cdc6 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 e values in regi
1cdc7 73 74 65 72 20 50 31 20 61 6e 64 20 50 33 2e 20 ster P1 and P3.
1cdc8 20 49 66 20 72 65 67 28 50 33 29 3c 72 65 67 28 If reg(P3)<reg(
1cdc9 50 31 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 P1) then.** jump
1cdca 20 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e 20 to address P2.
1cdcb 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 .**.** If the S
1cdcc 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c QLITE_JUMPIFNULL
1cdcd 20 62 69 74 20 6f 66 20 50 35 20 69 73 20 73 65 bit of P5 is se
1cdce 74 20 61 6e 64 20 65 69 74 68 65 72 20 72 65 67 t and either reg
1cdcf 28 50 31 29 20 6f 72 0a 2a 2a 20 72 65 67 28 50 (P1) or.** reg(P
1cdd0 33 29 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 3) is NULL then
1cdd1 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20 20 take the jump.
1cdd2 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 If the SQLITE_JU
1cdd3 4d 50 49 46 4e 55 4c 4c 20 0a 2a 2a 20 62 69 74 MPIFNULL .** bit
1cdd4 20 69 73 20 63 6c 65 61 72 20 74 68 65 6e 20 66 is clear then f
1cdd5 61 6c 6c 20 74 68 72 75 20 69 66 20 65 69 74 68 all thru if eith
1cdd6 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 er operand is NU
1cdd7 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 LL..**.** The SQ
1cdd8 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 70 6f LITE_AFF_MASK po
1cdd9 72 74 69 6f 6e 20 6f 66 20 50 35 20 6d 75 73 74 rtion of P5 must
1cdda 20 62 65 20 61 6e 20 61 66 66 69 6e 69 74 79 20 be an affinity
1cddb 63 68 61 72 61 63 74 65 72 20 2d 0a 2a 2a 20 53 character -.** S
1cddc 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 QLITE_AFF_TEXT,
1cddd 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 SQLITE_AFF_INTEG
1cdde 45 52 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 ER, and so forth
1cddf 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20 . An attempt is
1cde0 6d 61 64 65 20 0a 2a 2a 20 74 6f 20 63 6f 65 72 made .** to coer
1cde1 63 65 20 62 6f 74 68 20 69 6e 70 75 74 73 20 61 ce both inputs a
1cde2 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 ccording to this
1cde3 20 61 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65 affinity before
1cde4 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 the.** comparis
1cde5 6f 6e 20 69 73 20 6d 61 64 65 2e 20 49 66 20 74 on is made. If t
1cde6 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 he SQLITE_AFF_MA
1cde7 53 4b 20 69 73 20 30 78 30 30 2c 20 74 68 65 6e SK is 0x00, then
1cde8 20 6e 75 6d 65 72 69 63 0a 2a 2a 20 61 66 66 69 numeric.** affi
1cde9 6e 69 74 79 20 69 73 20 75 73 65 64 2e 20 4e 6f nity is used. No
1cdea 74 65 20 74 68 61 74 20 74 68 65 20 61 66 66 69 te that the affi
1cdeb 6e 69 74 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 nity conversions
1cdec 20 61 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 62 are stored.** b
1cded 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 ack into the inp
1cdee 75 74 20 72 65 67 69 73 74 65 72 73 20 50 31 20 ut registers P1
1cdef 61 6e 64 20 50 33 2e 20 20 53 6f 20 74 68 69 73 and P3. So this
1cdf0 20 6f 70 63 6f 64 65 20 63 61 6e 20 63 61 75 73 opcode can caus
1cdf1 65 0a 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 20 e.** persistent
1cdf2 63 68 61 6e 67 65 73 20 74 6f 20 72 65 67 69 73 changes to regis
1cdf3 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 2e 0a ters P1 and P3..
1cdf4 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63 **.** Once any c
1cdf5 6f 6e 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 onversions have
1cdf6 74 61 6b 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64 taken place, and
1cdf7 20 6e 65 69 74 68 65 72 20 76 61 6c 75 65 20 69 neither value i
1cdf8 73 20 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20 s NULL, .** the
1cdf9 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6d 70 61 values are compa
1cdfa 72 65 64 2e 20 49 66 20 62 6f 74 68 20 76 61 6c red. If both val
1cdfb 75 65 73 20 61 72 65 20 62 6c 6f 62 73 20 74 68 ues are blobs th
1cdfc 65 6e 20 6d 65 6d 63 6d 70 28 29 20 69 73 0a 2a en memcmp() is.*
1cdfd 2a 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d * used to determ
1cdfe 69 6e 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 ine the results
1cdff 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f of the compariso
1ce00 6e 2e 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 n. If both valu
1ce01 65 73 0a 2a 2a 20 61 72 65 20 74 65 78 74 2c 20 es.** are text,
1ce02 74 68 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72 then the appropr
1ce03 69 61 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 iate collating f
1ce04 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 unction specifie
1ce05 64 20 69 6e 0a 2a 2a 20 50 34 20 69 73 20 20 75 d in.** P4 is u
1ce06 73 65 64 20 74 6f 20 64 6f 20 74 68 65 20 63 6f sed to do the co
1ce07 6d 70 61 72 69 73 6f 6e 2e 20 20 49 66 20 50 34 mparison. If P4
1ce08 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 is not specifie
1ce09 64 20 74 68 65 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 d then.** memcmp
1ce0a 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f () is used to co
1ce0b 6d 70 61 72 65 20 74 65 78 74 20 73 74 72 69 6e mpare text strin
1ce0c 67 2e 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 g. If both valu
1ce0d 65 73 20 61 72 65 0a 2a 2a 20 6e 75 6d 65 72 69 es are.** numeri
1ce0e 63 2c 20 74 68 65 6e 20 61 20 6e 75 6d 65 72 69 c, then a numeri
1ce0f 63 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 c comparison is
1ce10 75 73 65 64 2e 20 49 66 20 74 68 65 20 74 77 6f used. If the two
1ce11 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 6f values.** are o
1ce12 66 20 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 f different type
1ce13 73 2c 20 74 68 65 6e 20 6e 75 6d 62 65 72 73 20 s, then numbers
1ce14 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c are considered l
1ce15 65 73 73 20 74 68 61 6e 0a 2a 2a 20 73 74 72 69 ess than.** stri
1ce16 6e 67 73 20 61 6e 64 20 73 74 72 69 6e 67 73 20 ngs and strings
1ce17 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c are considered l
1ce18 65 73 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a ess than blobs..
1ce19 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c **.** If the SQL
1ce1a 49 54 45 5f 53 54 4f 52 45 50 32 20 62 69 74 20 ITE_STOREP2 bit
1ce1b 6f 66 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 of P5 is set, th
1ce1c 65 6e 20 64 6f 20 6e 6f 74 20 6a 75 6d 70 2e 20 en do not jump.
1ce1d 20 49 6e 73 74 65 61 64 2c 0a 2a 2a 20 73 74 6f Instead,.** sto
1ce1e 72 65 20 61 20 62 6f 6f 6c 65 61 6e 20 72 65 73 re a boolean res
1ce1f 75 6c 74 20 28 65 69 74 68 65 72 20 30 2c 20 6f ult (either 0, o
1ce20 72 20 31 2c 20 6f 72 20 4e 55 4c 4c 29 20 69 6e r 1, or NULL) in
1ce21 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f register P2..*/
1ce22 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 20 50 ./* Opcode: Ne P
1ce23 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 1 P2 P3 P4 P5.**
1ce24 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a .** This works j
1ce25 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 ust like the Lt
1ce26 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 opcode except th
1ce27 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 at the jump is t
1ce28 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f aken if.** the o
1ce29 70 65 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73 perands in regis
1ce2a 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 20 61 ters P1 and P3 a
1ce2b 72 65 20 6e 6f 74 20 65 71 75 61 6c 2e 20 20 53 re not equal. S
1ce2c 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 ee the Lt opcode
1ce2d 20 66 6f 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e for.** addition
1ce2e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a al information..
1ce2f 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 71 */./* Opcode: Eq
1ce30 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a P1 P2 P3 P4 P5.
1ce31 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 **.** This works
1ce32 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c just like the L
1ce33 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 t opcode except
1ce34 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 that the jump is
1ce35 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 taken if.** the
1ce36 20 6f 70 65 72 61 6e 64 73 20 69 6e 20 72 65 67 operands in reg
1ce37 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 isters P1 and P3
1ce38 20 61 72 65 20 65 71 75 61 6c 2e 0a 2a 2a 20 53 are equal..** S
1ce39 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 ee the Lt opcode
1ce3a 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
1ce3b 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a information..*/.
1ce3c 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 65 20 50 31 /* Opcode: Le P1
1ce3d 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a P2 P3 P4 P5.**.
1ce3e 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 ** This works ju
1ce3f 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f st like the Lt o
1ce40 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 pcode except tha
1ce41 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 t the jump is ta
1ce42 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f ken if.** the co
1ce43 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 ntent of registe
1ce44 72 20 50 33 20 69 73 20 6c 65 73 73 20 74 68 61 r P3 is less tha
1ce45 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 n or equal to th
1ce46 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 e content of.**
1ce47 72 65 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 register P1. Se
1ce48 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 e the Lt opcode
1ce49 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 for additional i
1ce4a 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f nformation..*/./
1ce4b 2a 20 4f 70 63 6f 64 65 3a 20 47 74 20 50 31 20 * Opcode: Gt P1
1ce4c 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a P2 P3 P4 P5.**.*
1ce4d 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 * This works jus
1ce4e 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 t like the Lt op
1ce4f 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74 code except that
1ce50 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b the jump is tak
1ce51 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e en if.** the con
1ce52 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 tent of register
1ce53 20 50 33 20 69 73 20 67 72 65 61 74 65 72 20 74 P3 is greater t
1ce54 68 61 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 han the content
1ce55 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 of.** register P
1ce56 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 1. See the Lt o
1ce57 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 pcode for additi
1ce58 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
1ce59 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1ce5a 47 65 20 50 31 20 50 32 20 50 33 20 50 34 20 50 Ge P1 P2 P3 P4 P
1ce5b 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 5.**.** This wor
1ce5c 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 ks just like the
1ce5d 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 Lt opcode excep
1ce5e 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 t that the jump
1ce5f 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 is taken if.** t
1ce60 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 he content of re
1ce61 67 69 73 74 65 72 20 50 33 20 69 73 20 67 72 65 gister P3 is gre
1ce62 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 ater than or equ
1ce63 61 6c 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e al to the conten
1ce64 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 t of.** register
1ce65 20 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 P1. See the Lt
1ce66 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 opcode for addi
1ce67 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
1ce68 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 on..*/.case OP_E
1ce69 71 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 q:
1ce6a 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 /* same as TK_E
1ce6b 51 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e Q, jump, in1, in
1ce6c 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 3a 3 */.case OP_Ne:
1ce6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1ce6e 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 2c * same as TK_NE,
1ce6f 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 jump, in1, in3
1ce70 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 74 3a 20 20 */.case OP_Lt:
1ce71 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1ce72 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54 2c 20 6a same as TK_LT, j
1ce73 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f ump, in1, in3 */
1ce74 0a 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 .case OP_Le:
1ce75 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1ce76 6d 65 20 61 73 20 54 4b 5f 4c 45 2c 20 6a 75 6d me as TK_LE, jum
1ce77 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 p, in1, in3 */.c
1ce78 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20 20 20 ase OP_Gt:
1ce79 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1ce7a 20 61 73 20 54 4b 5f 47 54 2c 20 6a 75 6d 70 2c as TK_GT, jump,
1ce7b 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 in1, in3 */.cas
1ce7c 65 20 4f 50 5f 47 65 3a 20 7b 20 20 20 20 20 20 e OP_Ge: {
1ce7d 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1ce7e 73 20 54 4b 5f 47 45 2c 20 6a 75 6d 70 2c 20 69 s TK_GE, jump, i
1ce7f 6e 31 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 n1, in3 */.#if 0
1ce80 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1ce81 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1ce82 75 2e 61 69 20 2a 2f 0a 20 20 69 6e 74 20 66 6c u.ai */. int fl
1ce83 61 67 73 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a ags;. int res;.
1ce84 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b char affinity;
1ce85 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1ce86 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ce87 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a 0a 20 into u.ai */..
1ce88 20 75 2e 61 69 2e 66 6c 61 67 73 20 3d 20 70 49 u.ai.flags = pI
1ce89 6e 31 2d 3e 66 6c 61 67 73 7c 70 49 6e 33 2d 3e n1->flags|pIn3->
1ce8a 66 6c 61 67 73 3b 0a 0a 20 20 69 66 28 20 75 2e flags;.. if( u.
1ce8b 61 69 2e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c ai.flags&MEM_Nul
1ce8c 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 65 l ){. /* If e
1ce8d 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 ither operand is
1ce8e 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72 NULL then the r
1ce8f 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 esult is always
1ce90 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2a 20 54 68 65 NULL.. ** The
1ce91 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 jump is taken i
1ce92 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d f the SQLITE_JUM
1ce93 50 49 46 4e 55 4c 4c 20 62 69 74 20 69 73 20 73 PIFNULL bit is s
1ce94 65 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 et.. */. i
1ce95 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c f( pOp->p5 & SQL
1ce96 49 54 45 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20 ITE_STOREP2 ){.
1ce97 20 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e pOut = &p->
1ce98 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 aMem[pOp->p2];.
1ce99 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 MemSetTypeF
1ce9a 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 lag(pOut, MEM_Nu
1ce9b 6c 6c 29 3b 0a 20 20 20 20 20 20 52 45 47 49 53 ll);. REGIS
1ce9c 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1ce9d 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 7d 65 2, pOut);. }e
1ce9e 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 lse if( pOp->p5
1ce9f 26 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e & SQLITE_JUMPIFN
1cea0 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 70 63 20 ULL ){. pc
1cea1 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 20 = pOp->p2-1;.
1cea2 20 7d 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 }. break;.
1cea3 7d 0a 0a 20 20 75 2e 61 69 2e 61 66 66 69 6e 69 }.. u.ai.affini
1cea4 74 79 20 3d 20 70 4f 70 2d 3e 70 35 20 26 20 53 ty = pOp->p5 & S
1cea5 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 3b 0a QLITE_AFF_MASK;.
1cea6 20 20 69 66 28 20 75 2e 61 69 2e 61 66 66 69 6e if( u.ai.affin
1cea7 69 74 79 20 29 7b 0a 20 20 20 20 61 70 70 6c 79 ity ){. apply
1cea8 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 75 Affinity(pIn1, u
1cea9 2e 61 69 2e 61 66 66 69 6e 69 74 79 2c 20 65 6e .ai.affinity, en
1ceaa 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 61 70 70 coding);. app
1ceab 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e 33 2c lyAffinity(pIn3,
1ceac 20 75 2e 61 69 2e 61 66 66 69 6e 69 74 79 2c 20 u.ai.affinity,
1cead 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 69 encoding);. i
1ceae 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
1ceaf 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 led ) goto no_me
1ceb0 6d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 m;. }.. assert
1ceb1 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1ceb2 34 5f 43 4f 4c 4c 53 45 51 20 7c 7c 20 70 4f 70 4_COLLSEQ || pOp
1ceb3 2d 3e 70 34 2e 70 43 6f 6c 6c 3d 3d 30 20 29 3b ->p4.pColl==0 );
1ceb4 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 . ExpandBlob(pI
1ceb5 6e 31 29 3b 0a 20 20 45 78 70 61 6e 64 42 6c 6f n1);. ExpandBlo
1ceb6 62 28 70 49 6e 33 29 3b 0a 20 20 75 2e 61 69 2e b(pIn3);. u.ai.
1ceb7 72 65 73 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d res = sqlite3Mem
1ceb8 43 6f 6d 70 61 72 65 28 70 49 6e 33 2c 20 70 49 Compare(pIn3, pI
1ceb9 6e 31 2c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c n1, pOp->p4.pCol
1ceba 6c 29 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f l);. switch( pO
1cebb 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 p->opcode ){.
1cebc 20 63 61 73 65 20 4f 50 5f 45 71 3a 20 20 20 20 case OP_Eq:
1cebd 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e u.ai.res = u.ai.
1cebe 72 65 73 3d 3d 30 3b 20 20 20 20 20 62 72 65 61 res==0; brea
1cebf 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4e k;. case OP_N
1cec0 65 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d e: u.ai.res =
1cec1 20 75 2e 61 69 2e 72 65 73 21 3d 30 3b 20 20 20 u.ai.res!=0;
1cec2 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 break;. cas
1cec3 65 20 4f 50 5f 4c 74 3a 20 20 20 20 75 2e 61 69 e OP_Lt: u.ai
1cec4 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3c .res = u.ai.res<
1cec5 30 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 0; break;.
1cec6 20 20 20 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20 case OP_Le:
1cec7 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 u.ai.res = u.a
1cec8 69 2e 72 65 73 3c 3d 30 3b 20 20 20 20 20 62 72 i.res<=0; br
1cec9 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 eak;. case OP
1ceca 5f 47 74 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 _Gt: u.ai.res
1cecb 20 3d 20 75 2e 61 69 2e 72 65 73 3e 30 3b 20 20 = u.ai.res>0;
1cecc 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 break;. d
1cecd 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20 75 2e efault: u.
1cece 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 ai.res = u.ai.re
1cecf 73 3e 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b s>=0; break;
1ced0 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4f 70 2d . }.. if( pOp-
1ced1 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54 4f >p5 & SQLITE_STO
1ced2 52 45 50 32 20 29 7b 0a 20 20 20 20 70 4f 75 74 REP2 ){. pOut
1ced3 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1ced4 3e 70 32 5d 3b 0a 20 20 20 20 4d 65 6d 53 65 74 >p2];. MemSet
1ced5 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d TypeFlag(pOut, M
1ced6 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 70 4f 75 EM_Int);. pOu
1ced7 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 69 2e 72 65 t->u.i = u.ai.re
1ced8 73 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f s;. REGISTER_
1ced9 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 TRACE(pOp->p2, p
1ceda 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 Out);. }else if
1cedb 28 20 75 2e 61 69 2e 72 65 73 20 29 7b 0a 20 20 ( u.ai.res ){.
1cedc 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 pc = pOp->p2-1
1cedd 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1cede 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 65 72 ../* Opcode: Per
1cedf 6d 75 74 61 74 69 6f 6e 20 2a 20 2a 20 2a 20 50 mutation * * * P
1cee0 34 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 4 *.**.** Set th
1cee1 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 75 73 e permutation us
1cee2 65 64 20 62 79 20 74 68 65 20 4f 50 5f 43 6f 6d ed by the OP_Com
1cee3 70 61 72 65 20 6f 70 65 72 61 74 6f 72 20 74 6f pare operator to
1cee4 20 62 65 20 74 68 65 20 61 72 72 61 79 0a 2a 2a be the array.**
1cee5 20 6f 66 20 69 6e 74 65 67 65 72 73 20 69 6e 20 of integers in
1cee6 50 34 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 P4..**.** The pe
1cee7 72 6d 75 74 61 74 69 6f 6e 20 69 73 20 6f 6e 6c rmutation is onl
1cee8 79 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 y valid until th
1cee9 65 20 6e 65 78 74 20 4f 50 5f 50 65 72 6d 75 74 e next OP_Permut
1ceea 61 74 69 6f 6e 2c 20 4f 50 5f 43 6f 6d 70 61 72 ation, OP_Compar
1ceeb 65 2c 0a 2a 2a 20 4f 50 5f 48 61 6c 74 2c 20 6f e,.** OP_Halt, o
1ceec 72 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 20 r OP_ResultRow.
1ceed 20 54 79 70 69 63 61 6c 6c 79 20 74 68 65 20 4f Typically the O
1ceee 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 73 68 P_Permutation sh
1ceef 6f 75 6c 64 20 6f 63 63 75 72 0a 2a 2a 20 69 6d ould occur.** im
1cef0 6d 65 64 69 61 74 65 6c 79 20 70 72 69 6f 72 20 mediately prior
1cef1 74 6f 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72 to the OP_Compar
1cef2 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 65 e..*/.case OP_Pe
1cef3 72 6d 75 74 61 74 69 6f 6e 3a 20 7b 0a 20 20 61 rmutation: {. a
1cef4 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 ssert( pOp->p4ty
1cef5 70 65 3d 3d 50 34 5f 49 4e 54 41 52 52 41 59 20 pe==P4_INTARRAY
1cef6 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1cef7 2d 3e 70 34 2e 61 69 20 29 3b 0a 20 20 61 50 65 ->p4.ai );. aPe
1cef8 72 6d 75 74 65 20 3d 20 70 4f 70 2d 3e 70 34 2e rmute = pOp->p4.
1cef9 61 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a ai;. break;.}..
1cefa 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6d 70 61 /* Opcode: Compa
1cefb 72 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a re P1 P2 P3 P4 *
1cefc 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 .**.** Compare t
1cefd 6f 20 76 65 63 74 6f 72 73 20 6f 66 20 72 65 67 o vectors of reg
1cefe 69 73 74 65 72 73 20 69 6e 20 72 65 67 28 50 31 isters in reg(P1
1ceff 29 2e 2e 72 65 67 28 50 31 2b 50 33 2d 31 29 20 )..reg(P1+P3-1)
1cf00 28 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 (all this.** one
1cf01 20 22 41 22 29 20 61 6e 64 20 69 6e 20 72 65 67 "A") and in reg
1cf02 28 50 32 29 2e 2e 72 65 67 28 50 32 2b 50 33 2d (P2)..reg(P2+P3-
1cf03 31 29 20 28 22 42 22 29 2e 20 20 53 61 76 65 20 1) ("B"). Save
1cf04 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a the result of.**
1cf05 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
1cf06 66 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 6e for use by the n
1cf07 65 78 74 20 4f 50 5f 4a 75 6d 70 20 69 6e 73 74 ext OP_Jump inst
1cf08 72 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 ruct..**.** P4 i
1cf09 73 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 s a KeyInfo stru
1cf0a 63 74 75 72 65 20 74 68 61 74 20 64 65 66 69 6e cture that defin
1cf0b 65 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 es collating seq
1cf0c 75 65 6e 63 65 73 20 61 6e 64 20 73 6f 72 74 0a uences and sort.
1cf0d 2a 2a 20 6f 72 64 65 72 73 20 66 6f 72 20 74 68 ** orders for th
1cf0e 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54 e comparison. T
1cf0f 68 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 61 he permutation a
1cf10 70 70 6c 69 65 73 20 74 6f 20 72 65 67 69 73 74 pplies to regist
1cf11 65 72 73 0a 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68 ers.** only. Th
1cf12 65 20 4b 65 79 49 6e 66 6f 20 65 6c 65 6d 65 6e e KeyInfo elemen
1cf13 74 73 20 61 72 65 20 75 73 65 64 20 73 65 71 75 ts are used sequ
1cf14 65 6e 74 69 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 entially..**.**
1cf15 54 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 The comparison i
1cf16 73 20 61 20 73 6f 72 74 20 63 6f 6d 70 61 72 69 s a sort compari
1cf17 73 6f 6e 2c 20 73 6f 20 4e 55 4c 4c 73 20 63 6f son, so NULLs co
1cf18 6d 70 61 72 65 20 65 71 75 61 6c 2c 0a 2a 2a 20 mpare equal,.**
1cf19 4e 55 4c 4c 73 20 61 72 65 20 6c 65 73 73 20 74 NULLs are less t
1cf1a 68 61 6e 20 6e 75 6d 62 65 72 73 2c 20 6e 75 6d han numbers, num
1cf1b 62 65 72 73 20 61 72 65 20 6c 65 73 73 20 74 68 bers are less th
1cf1c 61 6e 20 73 74 72 69 6e 67 73 2c 0a 2a 2a 20 61 an strings,.** a
1cf1d 6e 64 20 73 74 72 69 6e 67 73 20 61 72 65 20 6c nd strings are l
1cf1e 65 73 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a ess than blobs..
1cf1f 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6d 70 61 */.case OP_Compa
1cf20 72 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 re: {.#if 0 /*
1cf21 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1cf22 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6a 20 moved into u.aj
1cf23 2a 2f 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 69 6e */. int n;. in
1cf24 74 20 69 3b 0a 20 20 69 6e 74 20 70 31 3b 0a 20 t i;. int p1;.
1cf25 20 69 6e 74 20 70 32 3b 0a 20 20 63 6f 6e 73 74 int p2;. const
1cf26 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
1cf27 66 6f 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 fo;. int idx;.
1cf28 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b CollSeq *pColl;
1cf29 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 /* Collating
1cf2a 20 73 65 71 75 65 6e 63 65 20 74 6f 20 75 73 65 sequence to use
1cf2b 20 6f 6e 20 74 68 69 73 20 74 65 72 6d 20 2a 2f on this term */
1cf2c 0a 20 20 69 6e 74 20 62 52 65 76 3b 20 20 20 20 . int bRev;
1cf2d 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f /* True fo
1cf2e 72 20 44 45 53 43 45 4e 44 49 4e 47 20 73 6f 72 r DESCENDING sor
1cf2f 74 20 6f 72 64 65 72 20 2a 2f 0a 23 65 6e 64 69 t order */.#endi
1cf30 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1cf31 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1cf32 75 2e 61 6a 20 2a 2f 0a 0a 20 20 75 2e 61 6a 2e u.aj */.. u.aj.
1cf33 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 75 n = pOp->p3;. u
1cf34 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 .aj.pKeyInfo = p
1cf35 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b Op->p4.pKeyInfo;
1cf36 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e . assert( u.aj.
1cf37 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 n>0 );. assert(
1cf38 20 75 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 21 3d u.aj.pKeyInfo!=
1cf39 30 20 29 3b 0a 20 20 75 2e 61 6a 2e 70 31 20 3d 0 );. u.aj.p1 =
1cf3a 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 65 pOp->p1;. asse
1cf3b 72 74 28 20 75 2e 61 6a 2e 70 31 3e 30 20 26 26 rt( u.aj.p1>0 &&
1cf3c 20 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a 2e 6e 3c u.aj.p1+u.aj.n<
1cf3d 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 =p->nMem+1 );.
1cf3e 75 2e 61 6a 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 u.aj.p2 = pOp->p
1cf3f 32 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 2;. assert( u.a
1cf40 6a 2e 70 32 3e 30 20 26 26 20 75 2e 61 6a 2e 70 j.p2>0 && u.aj.p
1cf41 32 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d 3e 6e 4d 65 2+u.aj.n<=p->nMe
1cf42 6d 2b 31 20 29 3b 0a 20 20 66 6f 72 28 75 2e 61 m+1 );. for(u.a
1cf43 6a 2e 69 3d 30 3b 20 75 2e 61 6a 2e 69 3c 75 2e j.i=0; u.aj.i<u.
1cf44 61 6a 2e 6e 3b 20 75 2e 61 6a 2e 69 2b 2b 29 7b aj.n; u.aj.i++){
1cf45 0a 20 20 20 20 75 2e 61 6a 2e 69 64 78 20 3d 20 . u.aj.idx =
1cf46 61 50 65 72 6d 75 74 65 20 3f 20 61 50 65 72 6d aPermute ? aPerm
1cf47 75 74 65 5b 75 2e 61 6a 2e 69 5d 20 3a 20 75 2e ute[u.aj.i] : u.
1cf48 61 6a 2e 69 3b 0a 20 20 20 20 52 45 47 49 53 54 aj.i;. REGIST
1cf49 45 52 5f 54 52 41 43 45 28 75 2e 61 6a 2e 70 31 ER_TRACE(u.aj.p1
1cf4a 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 70 2d 3e 61 +u.aj.idx, &p->a
1cf4b 4d 65 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a Mem[u.aj.p1+u.aj
1cf4c 2e 69 64 78 5d 29 3b 0a 20 20 20 20 52 45 47 49 .idx]);. REGI
1cf4d 53 54 45 52 5f 54 52 41 43 45 28 75 2e 61 6a 2e STER_TRACE(u.aj.
1cf4e 70 32 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 70 2d p2+u.aj.idx, &p-
1cf4f 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e >aMem[u.aj.p2+u.
1cf50 61 6a 2e 69 64 78 5d 29 3b 0a 20 20 20 20 61 73 aj.idx]);. as
1cf51 73 65 72 74 28 20 75 2e 61 6a 2e 69 3c 75 2e 61 sert( u.aj.i<u.a
1cf52 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 j.pKeyInfo->nFie
1cf53 6c 64 20 29 3b 0a 20 20 20 20 75 2e 61 6a 2e 70 ld );. u.aj.p
1cf54 43 6f 6c 6c 20 3d 20 75 2e 61 6a 2e 70 4b 65 79 Coll = u.aj.pKey
1cf55 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 75 2e 61 6a Info->aColl[u.aj
1cf56 2e 69 5d 3b 0a 20 20 20 20 75 2e 61 6a 2e 62 52 .i];. u.aj.bR
1cf57 65 76 20 3d 20 75 2e 61 6a 2e 70 4b 65 79 49 6e ev = u.aj.pKeyIn
1cf58 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 75 fo->aSortOrder[u
1cf59 2e 61 6a 2e 69 5d 3b 0a 20 20 20 20 69 43 6f 6d .aj.i];. iCom
1cf5a 70 61 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 65 pare = sqlite3Me
1cf5b 6d 43 6f 6d 70 61 72 65 28 26 70 2d 3e 61 4d 65 mCompare(&p->aMe
1cf5c 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a 2e 69 m[u.aj.p1+u.aj.i
1cf5d 64 78 5d 2c 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e dx], &p->aMem[u.
1cf5e 61 6a 2e 70 32 2b 75 2e 61 6a 2e 69 64 78 5d 2c aj.p2+u.aj.idx],
1cf5f 20 75 2e 61 6a 2e 70 43 6f 6c 6c 29 3b 0a 20 20 u.aj.pColl);.
1cf60 20 20 69 66 28 20 69 43 6f 6d 70 61 72 65 20 29 if( iCompare )
1cf61 7b 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 6a {. if( u.aj
1cf62 2e 62 52 65 76 20 29 20 69 43 6f 6d 70 61 72 65 .bRev ) iCompare
1cf63 20 3d 20 2d 69 43 6f 6d 70 61 72 65 3b 0a 20 20 = -iCompare;.
1cf64 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1cf65 0a 20 20 7d 0a 20 20 61 50 65 72 6d 75 74 65 20 . }. aPermute
1cf66 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a = 0;. break;.}.
1cf67 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4a 75 6d 70 ./* Opcode: Jump
1cf68 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1cf69 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 .** Jump to the
1cf6a 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 74 20 61 instruction at a
1cf6b 64 64 72 65 73 73 20 50 31 2c 20 50 32 2c 20 6f ddress P1, P2, o
1cf6c 72 20 50 33 20 64 65 70 65 6e 64 69 6e 67 20 6f r P3 depending o
1cf6d 6e 20 77 68 65 74 68 65 72 0a 2a 2a 20 69 6e 20 n whether.** in
1cf6e 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
1cf6f 4f 50 5f 43 6f 6d 70 61 72 65 20 69 6e 73 74 72 OP_Compare instr
1cf70 75 63 74 69 6f 6e 20 74 68 65 20 50 31 20 76 65 uction the P1 ve
1cf71 63 74 6f 72 20 77 61 73 20 6c 65 73 73 20 74 68 ctor was less th
1cf72 61 6e 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20 an.** equal to,
1cf73 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 or greater than
1cf74 74 68 65 20 50 32 20 76 65 63 74 6f 72 2c 20 72 the P2 vector, r
1cf75 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a espectively..*/.
1cf76 63 61 73 65 20 4f 50 5f 4a 75 6d 70 3a 20 7b 20 case OP_Jump: {
1cf77 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a /* j
1cf78 75 6d 70 20 2a 2f 0a 20 20 69 66 28 20 69 43 6f ump */. if( iCo
1cf79 6d 70 61 72 65 3c 30 20 29 7b 0a 20 20 20 20 70 mpare<0 ){. p
1cf7a 63 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 31 3b c = pOp->p1 - 1;
1cf7b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 43 6f . }else if( iCo
1cf7c 6d 70 61 72 65 3d 3d 30 20 29 7b 0a 20 20 20 20 mpare==0 ){.
1cf7d 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1cf7e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
1cf7f 63 20 3d 20 70 4f 70 2d 3e 70 33 20 2d 20 31 3b c = pOp->p3 - 1;
1cf80 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1cf81 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 6e 64 20 ./* Opcode: And
1cf82 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1cf83 2a 2a 20 54 61 6b 65 20 74 68 65 20 6c 6f 67 69 ** Take the logi
1cf84 63 61 6c 20 41 4e 44 20 6f 66 20 74 68 65 20 76 cal AND of the v
1cf85 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 alues in registe
1cf86 72 73 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 rs P1 and P2 and
1cf87 0a 2a 2a 20 77 72 69 74 65 20 74 68 65 20 72 65 .** write the re
1cf88 73 75 6c 74 20 69 6e 74 6f 20 72 65 67 69 73 74 sult into regist
1cf89 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 er P3..**.** If
1cf8a 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20 either P1 or P2
1cf8b 69 73 20 30 20 28 66 61 6c 73 65 29 20 74 68 65 is 0 (false) the
1cf8c 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 n the result is
1cf8d 30 20 65 76 65 6e 20 69 66 0a 2a 2a 20 74 68 65 0 even if.** the
1cf8e 20 6f 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 other input is
1cf8f 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c 4c 20 61 6e NULL. A NULL an
1cf90 64 20 74 72 75 65 20 6f 72 20 74 77 6f 20 4e 55 d true or two NU
1cf91 4c 4c 73 20 67 69 76 65 0a 2a 2a 20 61 20 4e 55 LLs give.** a NU
1cf92 4c 4c 20 6f 75 74 70 75 74 2e 0a 2a 2f 0a 2f 2a LL output..*/./*
1cf93 20 4f 70 63 6f 64 65 3a 20 4f 72 20 50 31 20 50 Opcode: Or P1 P
1cf94 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 2 P3 * *.**.** T
1cf95 61 6b 65 20 74 68 65 20 6c 6f 67 69 63 61 6c 20 ake the logical
1cf96 4f 52 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 OR of the values
1cf97 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1cf98 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 and P2 and.** st
1cf99 6f 72 65 20 74 68 65 20 61 6e 73 77 65 72 20 69 ore the answer i
1cf9a 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a n register P3..*
1cf9b 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50 *.** If either P
1cf9c 31 20 6f 72 20 50 32 20 69 73 20 6e 6f 6e 7a 65 1 or P2 is nonze
1cf9d 72 6f 20 28 74 72 75 65 29 20 74 68 65 6e 20 74 ro (true) then t
1cf9e 68 65 20 72 65 73 75 6c 74 20 69 73 20 31 20 28 he result is 1 (
1cf9f 74 72 75 65 29 0a 2a 2a 20 65 76 65 6e 20 69 66 true).** even if
1cfa0 20 74 68 65 20 6f 74 68 65 72 20 69 6e 70 75 74 the other input
1cfa1 20 69 73 20 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c is NULL. A NUL
1cfa2 4c 20 61 6e 64 20 66 61 6c 73 65 20 6f 72 20 74 L and false or t
1cfa3 77 6f 20 4e 55 4c 4c 73 0a 2a 2a 20 67 69 76 65 wo NULLs.** give
1cfa4 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 74 2e 0a a NULL output..
1cfa5 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 6e 64 3a 20 */.case OP_And:
1cfa6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1cfa7 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 2c 20 same as TK_AND,
1cfa8 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a in1, in2, out3 *
1cfa9 2f 0a 63 61 73 65 20 4f 50 5f 4f 72 3a 20 7b 20 /.case OP_Or: {
1cfaa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1cfab 61 6d 65 20 61 73 20 54 4b 5f 4f 52 2c 20 69 6e ame as TK_OR, in
1cfac 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 1, in2, out3 */.
1cfad 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1cfae 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1cfaf 69 6e 74 6f 20 75 2e 61 6b 20 2a 2f 0a 20 20 69 into u.ak */. i
1cfb0 6e 74 20 76 31 3b 20 20 20 20 2f 2a 20 4c 65 66 nt v1; /* Lef
1cfb1 74 20 6f 70 65 72 61 6e 64 3a 20 20 30 3d 3d 46 t operand: 0==F
1cfb2 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 ALSE, 1==TRUE, 2
1cfb3 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c ==UNKNOWN or NUL
1cfb4 4c 20 2a 2f 0a 20 20 69 6e 74 20 76 32 3b 20 20 L */. int v2;
1cfb5 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61 /* Right opera
1cfb6 6e 64 3a 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d nd: 0==FALSE, 1=
1cfb7 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 =TRUE, 2==UNKNOW
1cfb8 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e N or NULL */.#en
1cfb9 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1cfba 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1cfbb 6f 20 75 2e 61 6b 20 2a 2f 0a 0a 20 20 69 66 28 o u.ak */.. if(
1cfbc 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d pIn1->flags & M
1cfbd 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 EM_Null ){. u
1cfbe 2e 61 6b 2e 76 31 20 3d 20 32 3b 0a 20 20 7d 65 .ak.v1 = 2;. }e
1cfbf 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 lse{. u.ak.v1
1cfc0 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e = sqlite3VdbeIn
1cfc1 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b tValue(pIn1)!=0;
1cfc2 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 32 2d . }. if( pIn2-
1cfc3 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
1cfc4 6c 20 29 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 32 l ){. u.ak.v2
1cfc5 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 2;. }else{.
1cfc6 20 20 20 75 2e 61 6b 2e 76 32 20 3d 20 73 71 6c u.ak.v2 = sql
1cfc7 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 ite3VdbeIntValue
1cfc8 28 70 49 6e 32 29 21 3d 30 3b 0a 20 20 7d 0a 20 (pIn2)!=0;. }.
1cfc9 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
1cfca 3d 3d 4f 50 5f 41 6e 64 20 29 7b 0a 20 20 20 20 ==OP_And ){.
1cfcb 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 static const uns
1cfcc 69 67 6e 65 64 20 63 68 61 72 20 61 6e 64 5f 6c igned char and_l
1cfcd 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 30 2c ogic[] = { 0, 0,
1cfce 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 0, 0, 1, 2, 0,
1cfcf 32 2c 20 32 20 7d 3b 0a 20 20 20 20 75 2e 61 6b 2, 2 };. u.ak
1cfd0 2e 76 31 20 3d 20 61 6e 64 5f 6c 6f 67 69 63 5b .v1 = and_logic[
1cfd1 75 2e 61 6b 2e 76 31 2a 33 2b 75 2e 61 6b 2e 76 u.ak.v1*3+u.ak.v
1cfd2 32 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 2];. }else{.
1cfd3 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e static const un
1cfd4 73 69 67 6e 65 64 20 63 68 61 72 20 6f 72 5f 6c signed char or_l
1cfd5 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c ogic[] = { 0, 1,
1cfd6 20 32 2c 20 31 2c 20 31 2c 20 31 2c 20 32 2c 20 2, 1, 1, 1, 2,
1cfd7 31 2c 20 32 20 7d 3b 0a 20 20 20 20 75 2e 61 6b 1, 2 };. u.ak
1cfd8 2e 76 31 20 3d 20 6f 72 5f 6c 6f 67 69 63 5b 75 .v1 = or_logic[u
1cfd9 2e 61 6b 2e 76 31 2a 33 2b 75 2e 61 6b 2e 76 32 .ak.v1*3+u.ak.v2
1cfda 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 75 2e 61 ];. }. if( u.a
1cfdb 6b 2e 76 31 3d 3d 32 20 29 7b 0a 20 20 20 20 4d k.v1==2 ){. M
1cfdc 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f emSetTypeFlag(pO
1cfdd 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 ut, MEM_Null);.
1cfde 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4f 75 74 }else{. pOut
1cfdf 2d 3e 75 2e 69 20 3d 20 75 2e 61 6b 2e 76 31 3b ->u.i = u.ak.v1;
1cfe0 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 . MemSetTypeF
1cfe1 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e lag(pOut, MEM_In
1cfe2 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b t);. }. break;
1cfe3 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e .}../* Opcode: N
1cfe4 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ot P1 P2 * * *.*
1cfe5 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 *.** Interpret t
1cfe6 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1cfe7 73 74 65 72 20 50 31 20 61 73 20 61 20 62 6f 6f ster P1 as a boo
1cfe8 6c 65 61 6e 20 76 61 6c 75 65 2e 20 20 53 74 6f lean value. Sto
1cfe9 72 65 20 74 68 65 0a 2a 2a 20 62 6f 6f 6c 65 61 re the.** boolea
1cfea 6e 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 69 6e 20 n complement in
1cfeb 72 65 67 69 73 74 65 72 20 50 32 2e 20 20 49 66 register P2. If
1cfec 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1cfed 67 69 73 74 65 72 20 50 31 20 69 73 20 0a 2a 2a gister P1 is .**
1cfee 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 20 4e 55 NULL, then a NU
1cfef 4c 4c 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 LL is stored in
1cff0 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e P2..*/.case OP_N
1cff1 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 ot: {
1cff2 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1cff3 54 4b 5f 4e 4f 54 2c 20 69 6e 31 20 2a 2f 0a 20 TK_NOT, in1 */.
1cff4 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d pOut = &p->aMem
1cff5 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 66 28 [pOp->p2];. if(
1cff6 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d pIn1->flags & M
1cff7 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 EM_Null ){. s
1cff8 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1cff9 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 7d 65 Null(pOut);. }e
1cffa 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
1cffb 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 VdbeMemSetInt64(
1cffc 70 4f 75 74 2c 20 21 73 71 6c 69 74 65 33 56 64 pOut, !sqlite3Vd
1cffd 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 beIntValue(pIn1)
1cffe 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a );. }. break;.
1cfff 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 }../* Opcode: Bi
1d000 74 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a tNot P1 P2 * * *
1d001 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 .**.** Interpret
1d002 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 the content of
1d003 72 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 register P1 as a
1d004 6e 20 69 6e 74 65 67 65 72 2e 20 20 53 74 6f 72 n integer. Stor
1d005 65 20 74 68 65 0a 2a 2a 20 6f 6e 65 73 2d 63 6f e the.** ones-co
1d006 6d 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 mplement of the
1d007 50 31 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 P1 value into re
1d008 67 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 50 gister P2. If P
1d009 31 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 4e 55 4c 1 holds.** a NUL
1d00a 4c 20 74 68 65 6e 20 73 74 6f 72 65 20 61 20 4e L then store a N
1d00b 55 4c 4c 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 ULL in P2..*/.ca
1d00c 73 65 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b 20 se OP_BitNot: {
1d00d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1d00e 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 ame as TK_BITNOT
1d00f 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20 , in1 */. pOut
1d010 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
1d011 70 32 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d p2];. if( pIn1-
1d012 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
1d013 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 l ){. sqlite3
1d014 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 VdbeMemSetNull(p
1d015 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Out);. }else{.
1d016 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1d017 6d 53 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 mSetInt64(pOut,
1d018 7e 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 ~sqlite3VdbeIntV
1d019 61 6c 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d alue(pIn1));. }
1d01a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1d01b 4f 70 63 6f 64 65 3a 20 49 66 20 50 31 20 50 32 Opcode: If P1 P2
1d01c 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 P3 * *.**.** Ju
1d01d 6d 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 mp to P2 if the
1d01e 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1d01f 72 20 50 31 20 69 73 20 74 72 75 65 2e 20 20 54 r P1 is true. T
1d020 68 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 69 he value is.** i
1d021 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 72 75 s considered tru
1d022 65 20 69 66 20 69 74 20 69 73 20 6e 75 6d 65 72 e if it is numer
1d023 69 63 20 61 6e 64 20 6e 6f 6e 2d 7a 65 72 6f 2e ic and non-zero.
1d024 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a If the value.*
1d025 2a 20 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 * in P1 is NULL
1d026 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 then take the ju
1d027 6d 70 20 69 66 20 50 33 20 69 73 20 74 72 75 65 mp if P3 is true
1d028 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1d029 49 66 4e 6f 74 20 50 31 20 50 32 20 50 33 20 2a IfNot P1 P2 P3 *
1d02a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f *.**.** Jump to
1d02b 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 P2 if the value
1d02c 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1d02d 69 73 20 46 61 6c 73 65 2e 20 20 54 68 65 20 76 is False. The v
1d02e 61 6c 75 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f alue is.** is co
1d02f 6e 73 69 64 65 72 65 64 20 74 72 75 65 20 69 66 nsidered true if
1d030 20 69 74 20 68 61 73 20 61 20 6e 75 6d 65 72 69 it has a numeri
1d031 63 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 2e c value of zero.
1d032 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a If the value.*
1d033 2a 20 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 * in P1 is NULL
1d034 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 then take the ju
1d035 6d 70 20 69 66 20 50 33 20 69 73 20 74 72 75 65 mp if P3 is true
1d036 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 3a ..*/.case OP_If:
1d037 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d038 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f /* jump, in1 */
1d039 0a 63 61 73 65 20 4f 50 5f 49 66 4e 6f 74 3a 20 .case OP_IfNot:
1d03a 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1d03b 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 23 69 66 jump, in1 */.#if
1d03c 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1d03d 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1d03e 6f 20 75 2e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 o u.al */. int
1d03f 63 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 c;.#endif /* loc
1d040 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1d041 65 64 20 69 6e 74 6f 20 75 2e 61 6c 20 2a 2f 0a ed into u.al */.
1d042 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 if( pIn1->flag
1d043 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a s & MEM_Null ){.
1d044 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 70 4f 70 u.al.c = pOp
1d045 2d 3e 70 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23 ->p3;. }else{.#
1d046 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
1d047 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
1d048 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 73 71 . u.al.c = sq
1d049 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 lite3VdbeIntValu
1d04a 65 28 70 49 6e 31 29 21 3d 30 3b 0a 23 65 6c 73 e(pIn1)!=0;.#els
1d04b 65 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 73 e. u.al.c = s
1d04c 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 qlite3VdbeRealVa
1d04d 6c 75 65 28 70 49 6e 31 29 21 3d 30 2e 30 3b 0a lue(pIn1)!=0.0;.
1d04e 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 70 #endif. if( p
1d04f 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 Op->opcode==OP_I
1d050 66 4e 6f 74 20 29 20 75 2e 61 6c 2e 63 20 3d 20 fNot ) u.al.c =
1d051 21 75 2e 61 6c 2e 63 3b 0a 20 20 7d 0a 20 20 69 !u.al.c;. }. i
1d052 66 28 20 75 2e 61 6c 2e 63 20 29 7b 0a 20 20 20 f( u.al.c ){.
1d053 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b pc = pOp->p2-1;
1d054 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1d055 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 4e 75 ./* Opcode: IsNu
1d056 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ll P1 P2 * * *.*
1d057 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 *.** Jump to P2
1d058 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 if the value in
1d059 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 4e register P1 is N
1d05a 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ULL..*/.case OP_
1d05b 49 73 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 IsNull: {
1d05c 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1d05d 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a 75 6d 70 2c TK_ISNULL, jump,
1d05e 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 in1 */. if( (p
1d05f 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1d060 5f 4e 75 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 _Null)!=0 ){.
1d061 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1d062 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 1;. }. break;.
1d063 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f }../* Opcode: No
1d064 74 4e 75 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20 tNull P1 P2 * *
1d065 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 *.**.** Jump to
1d066 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 P2 if the value
1d067 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 in register P1 i
1d068 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20 0a 2a 2f s not NULL. .*/
1d069 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c .case OP_NotNull
1d06a 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f : { /
1d06b 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 * same as TK_NOT
1d06c 4e 55 4c 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 NULL, jump, in1
1d06d 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e */. if( (pIn1->
1d06e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1d06f 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d )==0 ){. pc =
1d070 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1d071 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1d072 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 75 6d 6e 20 Opcode: Column
1d073 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
1d074 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 *.** Interpret t
1d075 68 65 20 64 61 74 61 20 74 68 61 74 20 63 75 72 he data that cur
1d076 73 6f 72 20 50 31 20 70 6f 69 6e 74 73 20 74 6f sor P1 points to
1d077 20 61 73 20 61 20 73 74 72 75 63 74 75 72 65 20 as a structure
1d078 62 75 69 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 74 built using.** t
1d079 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e he MakeRecord in
1d07a 73 74 72 75 63 74 69 6f 6e 2e 20 20 28 53 65 65 struction. (See
1d07b 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20 the MakeRecord
1d07c 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 opcode for addit
1d07d 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 ional.** informa
1d07e 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 tion about the f
1d07f 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 64 61 74 ormat of the dat
1d080 61 2e 29 20 20 45 78 74 72 61 63 74 20 74 68 65 a.) Extract the
1d081 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a P2-th column.**
1d082 20 66 72 6f 6d 20 74 68 69 73 20 72 65 63 6f 72 from this recor
1d083 64 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 d. If there are
1d084 20 6c 65 73 73 20 74 68 61 74 20 28 50 32 2b 31 less that (P2+1
1d085 29 20 0a 2a 2a 20 76 61 6c 75 65 73 20 69 6e 20 ) .** values in
1d086 74 68 65 20 72 65 63 6f 72 64 2c 20 65 78 74 72 the record, extr
1d087 61 63 74 20 61 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a act a NULL..**.*
1d088 2a 20 54 68 65 20 76 61 6c 75 65 20 65 78 74 72 * The value extr
1d089 61 63 74 65 64 20 69 73 20 73 74 6f 72 65 64 20 acted is stored
1d08a 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a in register P3..
1d08b 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6c **.** If the col
1d08c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 66 65 77 umn contains few
1d08d 65 72 20 74 68 61 6e 20 50 32 20 66 69 65 6c 64 er than P2 field
1d08e 73 2c 20 74 68 65 6e 20 65 78 74 72 61 63 74 20 s, then extract
1d08f 61 20 4e 55 4c 4c 2e 20 20 4f 72 2c 0a 2a 2a 20 a NULL. Or,.**
1d090 69 66 20 74 68 65 20 50 34 20 61 72 67 75 6d 65 if the P4 argume
1d091 6e 74 20 69 73 20 61 20 50 34 5f 4d 45 4d 20 75 nt is a P4_MEM u
1d092 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 se the value of
1d093 74 68 65 20 50 34 20 61 72 67 75 6d 65 6e 74 20 the P4 argument
1d094 61 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 as.** the result
1d095 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f ..**.** If the O
1d096 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45 PFLAG_CLEARCACHE
1d097 20 62 69 74 20 69 73 20 73 65 74 20 6f 6e 20 50 bit is set on P
1d098 35 20 61 6e 64 20 50 31 20 69 73 20 61 20 70 73 5 and P1 is a ps
1d099 65 75 64 6f 2d 74 61 62 6c 65 20 63 75 72 73 6f eudo-table curso
1d09a 72 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 r,.** then the c
1d09b 61 63 68 65 20 6f 66 20 74 68 65 20 63 75 72 73 ache of the curs
1d09c 6f 72 20 69 73 20 72 65 73 65 74 20 70 72 69 6f or is reset prio
1d09d 72 20 74 6f 20 65 78 74 72 61 63 74 69 6e 67 20 r to extracting
1d09e 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 the column..** T
1d09f 68 65 20 66 69 72 73 74 20 4f 50 5f 43 6f 6c 75 he first OP_Colu
1d0a0 6d 6e 20 61 67 61 69 6e 73 74 20 61 20 70 73 65 mn against a pse
1d0a1 75 64 6f 2d 74 61 62 6c 65 20 61 66 74 65 72 20 udo-table after
1d0a2 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
1d0a3 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 72 65 67 69 content.** regi
1d0a4 73 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 ster has changed
1d0a5 20 73 68 6f 75 6c 64 20 68 61 76 65 20 74 68 69 should have thi
1d0a6 73 20 62 69 74 20 73 65 74 2e 0a 2a 2f 0a 63 61 s bit set..*/.ca
1d0a7 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a 20 7b 0a se OP_Column: {.
1d0a8 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1d0a9 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1d0aa 69 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a 20 20 75 into u.am */. u
1d0ab 33 32 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 32 payloadSize;
1d0ac 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1d0ad 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f ytes in the reco
1d0ae 72 64 20 2a 2f 0a 20 20 69 36 34 20 70 61 79 6c rd */. i64 payl
1d0af 6f 61 64 53 69 7a 65 36 34 3b 20 2f 2a 20 4e 75 oadSize64; /* Nu
1d0b0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
1d0b1 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
1d0b2 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 int p1;
1d0b3 20 20 20 20 2f 2a 20 50 31 20 76 61 6c 75 65 20 /* P1 value
1d0b4 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 2a 2f of the opcode */
1d0b5 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 . int p2;
1d0b6 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 /* column
1d0b7 6e 75 6d 62 65 72 20 74 6f 20 72 65 74 72 69 65 number to retrie
1d0b8 76 65 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 ve */. VdbeCurs
1d0b9 6f 72 20 2a 70 43 3b 20 20 20 20 2f 2a 20 54 68 or *pC; /* Th
1d0ba 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 2a 2f e VDBE cursor */
1d0bb 0a 20 20 63 68 61 72 20 2a 7a 52 65 63 3b 20 20 . char *zRec;
1d0bc 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
1d0bd 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 to complete rec
1d0be 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20 20 42 74 ord-data */. Bt
1d0bf 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 20 20 Cursor *pCrsr;
1d0c0 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 63 75 /* The BTree cu
1d0c1 72 73 6f 72 20 2a 2f 0a 20 20 75 33 32 20 2a 61 rsor */. u32 *a
1d0c2 54 79 70 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 Type; /*
1d0c3 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64 73 20 74 aType[i] holds t
1d0c4 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70 65 20 he numeric type
1d0c5 6f 66 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 of the i-th colu
1d0c6 6d 6e 20 2a 2f 0a 20 20 75 33 32 20 2a 61 4f 66 mn */. u32 *aOf
1d0c7 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 61 4f fset; /* aO
1d0c8 66 66 73 65 74 5b 69 5d 20 69 73 20 6f 66 66 73 ffset[i] is offs
1d0c9 65 74 20 74 6f 20 73 74 61 72 74 20 6f 66 20 64 et to start of d
1d0ca 61 74 61 20 66 6f 72 20 69 2d 74 68 20 63 6f 6c ata for i-th col
1d0cb 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 umn */. int nFi
1d0cc 65 6c 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e eld; /* n
1d0cd 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 umber of fields
1d0ce 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f in the record */
1d0cf 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 . int len;
1d0d0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 6e /* The len
1d0d1 67 74 68 20 6f 66 20 74 68 65 20 73 65 72 69 61 gth of the seria
1d0d2 6c 69 7a 65 64 20 64 61 74 61 20 66 6f 72 20 74 lized data for t
1d0d3 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 he column */. i
1d0d4 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
1d0d5 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
1d0d6 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 61 r */. char *zDa
1d0d7 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 72 ta; /* Par
1d0d8 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 t of the record
1d0d9 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f being decoded */
1d0da 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 20 20 . Mem *pDest;
1d0db 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 /* Where t
1d0dc 6f 20 77 72 69 74 65 20 74 68 65 20 65 78 74 72 o write the extr
1d0dd 61 63 74 65 64 20 76 61 6c 75 65 20 2a 2f 0a 20 acted value */.
1d0de 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 20 20 20 Mem sMem;
1d0df 20 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f 72 69 /* For stori
1d0e0 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 62 65 ng the record be
1d0e1 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20 ing decoded */.
1d0e2 20 75 38 20 2a 7a 49 64 78 3b 20 20 20 20 20 20 u8 *zIdx;
1d0e3 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 /* Index int
1d0e4 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 38 o header */. u8
1d0e5 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 20 20 20 *zEndHdr;
1d0e6 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 /* Pointer to f
1d0e7 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20 irst byte after
1d0e8 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 the header */.
1d0e9 75 33 32 20 6f 66 66 73 65 74 3b 20 20 20 20 20 u32 offset;
1d0ea 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 /* Offset int
1d0eb 6f 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 o the data */.
1d0ec 75 36 34 20 6f 66 66 73 65 74 36 34 3b 20 20 20 u64 offset64;
1d0ed 20 20 20 2f 2a 20 36 34 2d 62 69 74 20 6f 66 66 /* 64-bit off
1d0ee 73 65 74 2e 20 20 36 34 20 62 69 74 73 20 6e 65 set. 64 bits ne
1d0ef 65 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f 76 eded to catch ov
1d0f0 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 erflow */. int
1d0f1 73 7a 48 64 72 3b 20 20 20 20 20 20 20 20 20 2f szHdr; /
1d0f2 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65 * Size of the he
1d0f3 61 64 65 72 20 73 69 7a 65 20 66 69 65 6c 64 20 ader size field
1d0f4 61 74 20 73 74 61 72 74 20 6f 66 20 72 65 63 6f at start of reco
1d0f5 72 64 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61 69 rd */. int avai
1d0f6 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 l; /* Nu
1d0f7 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
1d0f8 20 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 20 available data
1d0f9 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 67 3b 20 */. Mem *pReg;
1d0fa 20 20 20 20 20 20 20 20 2f 2a 20 50 73 65 75 64 /* Pseud
1d0fb 6f 54 61 62 6c 65 20 69 6e 70 75 74 20 72 65 67 oTable input reg
1d0fc 69 73 74 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20 ister */.#endif
1d0fd 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1d0fe 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1d0ff 61 6d 20 2a 2f 0a 0a 0a 20 20 75 2e 61 6d 2e 70 am */... u.am.p
1d100 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 1 = pOp->p1;. u
1d101 2e 61 6d 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 .am.p2 = pOp->p2
1d102 3b 0a 20 20 75 2e 61 6d 2e 70 43 20 3d 20 30 3b ;. u.am.pC = 0;
1d103 0a 20 20 6d 65 6d 73 65 74 28 26 75 2e 61 6d 2e . memset(&u.am.
1d104 73 4d 65 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 sMem, 0, sizeof(
1d105 75 2e 61 6d 2e 73 4d 65 6d 29 29 3b 0a 20 20 61 u.am.sMem));. a
1d106 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 31 3c 70 ssert( u.am.p1<p
1d107 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 ->nCursor );. a
1d108 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 ssert( pOp->p3>0
1d109 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e && pOp->p3<=p->
1d10a 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 61 6d 2e 70 nMem );. u.am.p
1d10b 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b Dest = &p->aMem[
1d10c 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d 65 6d 53 pOp->p3];. MemS
1d10d 65 74 54 79 70 65 46 6c 61 67 28 75 2e 61 6d 2e etTypeFlag(u.am.
1d10e 70 44 65 73 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 pDest, MEM_Null)
1d10f 3b 0a 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 ;. u.am.zRec =
1d110 30 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 62 6c 0;.. /* This bl
1d111 6f 63 6b 20 73 65 74 73 20 74 68 65 20 76 61 72 ock sets the var
1d112 69 61 62 6c 65 20 75 2e 61 6d 2e 70 61 79 6c 6f iable u.am.paylo
1d113 61 64 53 69 7a 65 20 74 6f 20 62 65 20 74 68 65 adSize to be the
1d114 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
1d115 0a 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 . ** bytes in t
1d116 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2a 0a he record.. **.
1d117 20 20 2a 2a 20 75 2e 61 6d 2e 7a 52 65 63 20 69 ** u.am.zRec i
1d118 73 20 73 65 74 20 74 6f 20 62 65 20 74 68 65 20 s set to be the
1d119 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 complete text of
1d11a 20 74 68 65 20 72 65 63 6f 72 64 20 69 66 20 69 the record if i
1d11b 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a t is available..
1d11c 20 20 2a 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 ** The complet
1d11d 65 20 72 65 63 6f 72 64 20 74 65 78 74 20 69 73 e record text is
1d11e 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c always availabl
1d11f 65 20 66 6f 72 20 70 73 65 75 64 6f 2d 74 61 62 e for pseudo-tab
1d120 6c 65 73 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 les. ** If the
1d121 72 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64 record is stored
1d122 20 69 6e 20 61 20 63 75 72 73 6f 72 2c 20 74 68 in a cursor, th
1d123 65 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 e complete recor
1d124 64 20 74 65 78 74 0a 20 20 2a 2a 20 6d 69 67 68 d text. ** migh
1d125 74 20 62 65 20 61 76 61 69 6c 61 62 6c 65 20 69 t be available i
1d126 6e 20 74 68 65 20 20 75 2e 61 6d 2e 70 43 2d 3e n the u.am.pC->
1d127 61 52 6f 77 20 63 61 63 68 65 2e 20 20 4f 72 20 aRow cache. Or
1d128 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 2e it might not be.
1d129 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 64 61 74 . ** If the dat
1d12a 61 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65 a is unavailable
1d12b 2c 20 20 75 2e 61 6d 2e 7a 52 65 63 20 69 73 20 , u.am.zRec is
1d12c 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a set to NULL.. *
1d12d 2a 0a 20 20 2a 2a 20 57 65 20 61 6c 73 6f 20 63 *. ** We also c
1d12e 6f 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 ompute the numbe
1d12f 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
1d130 74 68 65 20 72 65 63 6f 72 64 2e 20 20 46 6f 72 the record. For
1d131 20 63 75 72 73 6f 72 73 2c 0a 20 20 2a 2a 20 74 cursors,. ** t
1d132 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
1d133 75 6d 6e 73 20 69 73 20 73 74 6f 72 65 64 20 69 umns is stored i
1d134 6e 20 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 n the VdbeCursor
1d135 2e 6e 46 69 65 6c 64 20 65 6c 65 6d 65 6e 74 2e .nField element.
1d136 0a 20 20 2a 2f 0a 20 20 75 2e 61 6d 2e 70 43 20 . */. u.am.pC
1d137 3d 20 70 2d 3e 61 70 43 73 72 5b 75 2e 61 6d 2e = p->apCsr[u.am.
1d138 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1d139 2e 61 6d 2e 70 43 21 3d 30 20 29 3b 0a 23 69 66 .am.pC!=0 );.#if
1d13a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1d13b 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
1d13c 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 43 2d assert( u.am.pC-
1d13d 3e 70 56 74 61 62 43 75 72 73 6f 72 3d 3d 30 20 >pVtabCursor==0
1d13e 29 3b 0a 23 65 6e 64 69 66 0a 20 20 75 2e 61 6d );.#endif. u.am
1d13f 2e 70 43 72 73 72 20 3d 20 75 2e 61 6d 2e 70 43 .pCrsr = u.am.pC
1d140 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 ->pCursor;. if(
1d141 20 75 2e 61 6d 2e 70 43 72 73 72 21 3d 30 20 29 u.am.pCrsr!=0 )
1d142 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 {. /* The rec
1d143 6f 72 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e ord is stored in
1d144 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20 20 a B-Tree */.
1d145 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1d146 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e eCursorMoveto(u.
1d147 61 6d 2e 70 43 29 3b 0a 20 20 20 20 69 66 28 20 am.pC);. if(
1d148 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f rc ) goto abort_
1d149 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1d14a 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 6e if( u.am.pC->n
1d14b 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 ullRow ){.
1d14c 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d14d 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 = 0;. }else
1d14e 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 if( u.am.pC->cac
1d14f 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63 heStatus==p->cac
1d150 68 65 43 74 72 20 29 7b 0a 20 20 20 20 20 20 75 heCtr ){. u
1d151 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 .am.payloadSize
1d152 3d 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c 6f = u.am.pC->paylo
1d153 61 64 53 69 7a 65 3b 0a 20 20 20 20 20 20 75 2e adSize;. u.
1d154 61 6d 2e 7a 52 65 63 20 3d 20 28 63 68 61 72 2a am.zRec = (char*
1d155 29 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 3b 0a )u.am.pC->aRow;.
1d156 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e }else if( u.
1d157 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 am.pC->isIndex )
1d158 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1d159 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
1d15a 6f 72 49 73 56 61 6c 69 64 28 75 2e 61 6d 2e 70 orIsValid(u.am.p
1d15b 43 72 73 72 29 20 29 3b 0a 20 20 20 20 20 20 72 Crsr) );. r
1d15c 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1d15d 4b 65 79 53 69 7a 65 28 75 2e 61 6d 2e 70 43 72 KeySize(u.am.pCr
1d15e 73 72 2c 20 26 75 2e 61 6d 2e 70 61 79 6c 6f 61 sr, &u.am.payloa
1d15f 64 53 69 7a 65 36 34 29 3b 0a 20 20 20 20 20 20 dSize64);.
1d160 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
1d161 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a 20 54 72 TE_OK ); /* Tr
1d162 75 65 20 62 65 63 61 75 73 65 20 6f 66 20 43 75 ue because of Cu
1d163 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c rsorMoveto() cal
1d164 6c 20 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 20 l above */.
1d165 20 2f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 /* sqlite3Btree
1d166 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 75 ParseCellPtr() u
1d167 73 65 73 20 67 65 74 56 61 72 69 6e 74 33 32 28 ses getVarint32(
1d168 29 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 65 ) to extract the
1d169 0a 20 20 20 20 20 20 2a 2a 20 70 61 79 6c 6f 61 . ** payloa
1d16a 64 20 73 69 7a 65 2c 20 73 6f 20 69 74 20 69 73 d size, so it is
1d16b 20 69 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 impossible for
1d16c 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d16d 36 34 20 74 6f 20 62 65 0a 20 20 20 20 20 20 2a 64 to be. *
1d16e 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 33 32 * larger than 32
1d16f 20 62 69 74 73 2e 20 2a 2f 0a 20 20 20 20 20 20 bits. */.
1d170 61 73 73 65 72 74 28 20 28 75 2e 61 6d 2e 70 61 assert( (u.am.pa
1d171 79 6c 6f 61 64 53 69 7a 65 36 34 20 26 20 53 51 yloadSize64 & SQ
1d172 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29 3d 3d 28 LITE_MAX_U32)==(
1d173 75 36 34 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 u64)u.am.payload
1d174 53 69 7a 65 36 34 20 29 3b 0a 20 20 20 20 20 20 Size64 );.
1d175 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d176 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e 70 61 79 = (u32)u.am.pay
1d177 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 20 20 20 loadSize64;.
1d178 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
1d179 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1d17a 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75 eCursorIsValid(u
1d17b 2e 61 6d 2e 70 43 72 73 72 29 20 29 3b 0a 20 20 .am.pCrsr) );.
1d17c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1d17d 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 75 2e BtreeDataSize(u.
1d17e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61 6d 2e am.pCrsr, &u.am.
1d17f 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b 0a 20 20 payloadSize);.
1d180 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
1d181 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f SQLITE_OK ); /
1d182 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63 61 6e * DataSize() can
1d183 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 20 20 not fail */.
1d184 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e }. }else if( u.
1d185 61 6d 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 am.pC->pseudoTab
1d186 6c 65 52 65 67 3e 30 20 29 7b 0a 20 20 20 20 75 leReg>0 ){. u
1d187 2e 61 6d 2e 70 52 65 67 20 3d 20 26 70 2d 3e 61 .am.pReg = &p->a
1d188 4d 65 6d 5b 75 2e 61 6d 2e 70 43 2d 3e 70 73 65 Mem[u.am.pC->pse
1d189 75 64 6f 54 61 62 6c 65 52 65 67 5d 3b 0a 20 20 udoTableReg];.
1d18a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 assert( u.am.p
1d18b 52 65 67 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Reg->flags & MEM
1d18c 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 75 2e 61 _Blob );. u.a
1d18d 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 m.payloadSize =
1d18e 75 2e 61 6d 2e 70 52 65 67 2d 3e 6e 3b 0a 20 20 u.am.pReg->n;.
1d18f 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 75 2e u.am.zRec = u.
1d190 61 6d 2e 70 52 65 67 2d 3e 7a 3b 0a 20 20 20 20 am.pReg->z;.
1d191 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.am.pC->cacheSt
1d192 61 74 75 73 20 3d 20 28 70 4f 70 2d 3e 70 35 26 atus = (pOp->p5&
1d193 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 OPFLAG_CLEARCACH
1d194 45 29 20 3f 20 43 41 43 48 45 5f 53 54 41 4c 45 E) ? CACHE_STALE
1d195 20 3a 20 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a : p->cacheCtr;.
1d196 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d assert( u.am
1d197 2e 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 .payloadSize==0
1d198 7c 7c 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 20 || u.am.zRec!=0
1d199 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1d19a 2f 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20 /* Consider the
1d19b 72 6f 77 20 74 6f 20 62 65 20 4e 55 4c 4c 20 2a row to be NULL *
1d19c 2f 0a 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f /. u.am.paylo
1d19d 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a adSize = 0;. }.
1d19e 0a 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 70 61 . /* If u.am.pa
1d19f 79 6c 6f 61 64 53 69 7a 65 20 69 73 20 30 2c 20 yloadSize is 0,
1d1a0 74 68 65 6e 20 6a 75 73 74 20 73 74 6f 72 65 20 then just store
1d1a1 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 66 28 20 a NULL */. if(
1d1a2 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d1a3 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ==0 ){. asser
1d1a4 74 28 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 t( u.am.pDest->f
1d1a5 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b lags&MEM_Null );
1d1a6 0a 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c . goto op_col
1d1a7 75 6d 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 61 umn_out;. }. a
1d1a8 73 73 65 72 74 28 20 64 62 2d 3e 61 4c 69 6d 69 ssert( db->aLimi
1d1a9 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
1d1aa 45 4e 47 54 48 5d 3e 3d 30 20 29 3b 0a 20 20 69 ENGTH]>=0 );. i
1d1ab 66 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 f( u.am.payloadS
1d1ac 69 7a 65 20 3e 20 28 75 33 32 29 64 62 2d 3e 61 ize > (u32)db->a
1d1ad 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
1d1ae 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 IT_LENGTH] ){.
1d1af 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a goto too_big;.
1d1b0 20 20 7d 0a 0a 20 20 75 2e 61 6d 2e 6e 46 69 65 }.. u.am.nFie
1d1b1 6c 64 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e 6e 46 ld = u.am.pC->nF
1d1b2 69 65 6c 64 3b 0a 20 20 61 73 73 65 72 74 28 20 ield;. assert(
1d1b3 75 2e 61 6d 2e 70 32 3c 75 2e 61 6d 2e 6e 46 69 u.am.p2<u.am.nFi
1d1b4 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 eld );.. /* Rea
1d1b5 64 20 61 6e 64 20 70 61 72 73 65 20 74 68 65 20 d and parse the
1d1b6 74 61 62 6c 65 20 68 65 61 64 65 72 2e 20 20 53 table header. S
1d1b7 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 tore the results
1d1b8 20 6f 66 20 74 68 65 20 70 61 72 73 65 0a 20 20 of the parse.
1d1b9 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f ** into the reco
1d1ba 72 64 20 68 65 61 64 65 72 20 63 61 63 68 65 20 rd header cache
1d1bb 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 fields of the cu
1d1bc 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 rsor.. */. u.a
1d1bd 6d 2e 61 54 79 70 65 20 3d 20 75 2e 61 6d 2e 70 m.aType = u.am.p
1d1be 43 2d 3e 61 54 79 70 65 3b 0a 20 20 69 66 28 20 C->aType;. if(
1d1bf 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.am.pC->cacheSt
1d1c0 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74 atus==p->cacheCt
1d1c1 72 20 29 7b 0a 20 20 20 20 75 2e 61 6d 2e 61 4f r ){. u.am.aO
1d1c2 66 66 73 65 74 20 3d 20 75 2e 61 6d 2e 70 43 2d ffset = u.am.pC-
1d1c3 3e 61 4f 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73 >aOffset;. }els
1d1c4 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 75 2e e{. assert(u.
1d1c5 61 6d 2e 61 54 79 70 65 29 3b 0a 20 20 20 20 75 am.aType);. u
1d1c6 2e 61 6d 2e 61 76 61 69 6c 20 3d 20 30 3b 0a 20 .am.avail = 0;.
1d1c7 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 4f 66 66 u.am.pC->aOff
1d1c8 73 65 74 20 3d 20 75 2e 61 6d 2e 61 4f 66 66 73 set = u.am.aOffs
1d1c9 65 74 20 3d 20 26 75 2e 61 6d 2e 61 54 79 70 65 et = &u.am.aType
1d1ca 5b 75 2e 61 6d 2e 6e 46 69 65 6c 64 5d 3b 0a 20 [u.am.nField];.
1d1cb 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c u.am.pC->payl
1d1cc 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70 oadSize = u.am.p
1d1cd 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 ayloadSize;.
1d1ce 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.am.pC->cacheSt
1d1cf 61 74 75 73 20 3d 20 70 2d 3e 63 61 63 68 65 43 atus = p->cacheC
1d1d0 74 72 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 tr;.. /* Figu
1d1d1 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 re out how many
1d1d2 62 79 74 65 73 20 61 72 65 20 69 6e 20 74 68 65 bytes are in the
1d1d3 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 69 header */. i
1d1d4 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a f( u.am.zRec ){.
1d1d5 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 u.am.zData
1d1d6 20 3d 20 75 2e 61 6d 2e 7a 52 65 63 3b 0a 20 20 = u.am.zRec;.
1d1d7 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 }else{. i
1d1d8 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e f( u.am.pC->isIn
1d1d9 64 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 75 dex ){. u
1d1da 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 28 63 68 61 .am.zData = (cha
1d1db 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b r*)sqlite3BtreeK
1d1dc 65 79 46 65 74 63 68 28 75 2e 61 6d 2e 70 43 72 eyFetch(u.am.pCr
1d1dd 73 72 2c 20 26 75 2e 61 6d 2e 61 76 61 69 6c 29 sr, &u.am.avail)
1d1de 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1d1df 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 u.am.zDat
1d1e0 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 a = (char*)sqlit
1d1e1 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 e3BtreeDataFetch
1d1e2 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e (u.am.pCrsr, &u.
1d1e3 61 6d 2e 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 am.avail);.
1d1e4 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 4b }. /* If K
1d1e5 65 79 46 65 74 63 68 28 29 2f 44 61 74 61 46 65 eyFetch()/DataFe
1d1e6 74 63 68 28 29 20 6d 61 6e 61 67 65 64 20 74 6f tch() managed to
1d1e7 20 67 65 74 20 74 68 65 20 65 6e 74 69 72 65 20 get the entire
1d1e8 70 61 79 6c 6f 61 64 2c 0a 20 20 20 20 20 20 2a payload,. *
1d1e9 2a 20 73 61 76 65 20 74 68 65 20 70 61 79 6c 6f * save the paylo
1d1ea 61 64 20 69 6e 20 74 68 65 20 75 2e 61 6d 2e 70 ad in the u.am.p
1d1eb 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20 C->aRow cache.
1d1ec 54 68 61 74 20 77 69 6c 6c 20 73 61 76 65 20 75 That will save u
1d1ed 73 20 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20 s from. **
1d1ee 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b 65 20 61 having to make a
1d1ef 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 20 dditional calls
1d1f0 74 6f 20 66 65 74 63 68 20 74 68 65 20 63 6f 6e to fetch the con
1d1f1 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20 6f 66 0a tent portion of.
1d1f2 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 63 ** the rec
1d1f3 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ord.. */.
1d1f4 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d assert( u.am
1d1f5 2e 61 76 61 69 6c 3e 3d 30 20 29 3b 0a 20 20 20 .avail>=0 );.
1d1f6 20 20 20 69 66 28 20 75 2e 61 6d 2e 70 61 79 6c if( u.am.payl
1d1f7 6f 61 64 53 69 7a 65 20 3c 3d 20 28 75 33 32 29 oadSize <= (u32)
1d1f8 75 2e 61 6d 2e 61 76 61 69 6c 20 29 7b 0a 20 20 u.am.avail ){.
1d1f9 20 20 20 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20 u.am.zRec
1d1fa 3d 20 75 2e 61 6d 2e 7a 44 61 74 61 3b 0a 20 20 = u.am.zData;.
1d1fb 20 20 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 u.am.pC->a
1d1fc 52 6f 77 20 3d 20 28 75 38 2a 29 75 2e 61 6d 2e Row = (u8*)u.am.
1d1fd 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c zData;. }el
1d1fe 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d se{. u.am
1d1ff 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 30 3b 0a 20 .pC->aRow = 0;.
1d200 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1d201 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e /* The followin
1d202 67 20 61 73 73 65 72 74 20 69 73 20 74 72 75 65 g assert is true
1d203 20 69 6e 20 61 6c 6c 20 63 61 73 65 73 20 61 63 in all cases ac
1d204 63 65 70 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a cept when. **
1d205 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1d206 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f 72 72 le has been corr
1d207 75 70 74 65 64 20 65 78 74 65 72 6e 61 6c 6c 79 upted externally
1d208 2e 0a 20 20 20 20 2a 2a 20 20 20 20 61 73 73 65 .. ** asse
1d209 72 74 28 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 rt( u.am.zRec!=0
1d20a 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d || u.am.avail>=
1d20b 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d20c 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d || u.am.avail>=
1d20d 39 20 29 3b 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 9 ); */. u.am
1d20e 2e 73 7a 48 64 72 20 3d 20 67 65 74 56 61 72 69 .szHdr = getVari
1d20f 6e 74 33 32 28 28 75 38 2a 29 75 2e 61 6d 2e 7a nt32((u8*)u.am.z
1d210 44 61 74 61 2c 20 75 2e 61 6d 2e 6f 66 66 73 65 Data, u.am.offse
1d211 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 t);.. /* Make
1d212 20 73 75 72 65 20 61 20 63 6f 72 72 75 70 74 20 sure a corrupt
1d213 64 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74 database has not
1d214 20 67 69 76 65 6e 20 75 73 20 61 6e 20 6f 76 65 given us an ove
1d215 72 73 69 7a 65 20 68 65 61 64 65 72 2e 0a 20 20 rsize header..
1d216 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 6e 6f 77 ** Do this now
1d217 20 74 6f 20 61 76 6f 69 64 20 61 6e 20 6f 76 65 to avoid an ove
1d218 72 73 69 7a 65 20 6d 65 6d 6f 72 79 20 61 6c 6c rsize memory all
1d219 6f 63 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a ocation.. **.
1d21a 20 20 20 20 2a 2a 20 54 79 70 65 20 65 6e 74 72 ** Type entr
1d21b 69 65 73 20 63 61 6e 20 62 65 20 62 65 74 77 65 ies can be betwe
1d21c 65 6e 20 31 20 61 6e 64 20 35 20 62 79 74 65 73 en 1 and 5 bytes
1d21d 20 65 61 63 68 2e 20 20 42 75 74 20 34 20 61 6e each. But 4 an
1d21e 64 20 35 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 d 5 byte. **
1d21f 74 79 70 65 73 20 75 73 65 20 73 6f 20 6d 75 63 types use so muc
1d220 68 20 64 61 74 61 20 73 70 61 63 65 20 74 68 61 h data space tha
1d221 74 20 74 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 t there can only
1d222 20 62 65 20 34 30 39 36 20 61 6e 64 20 33 32 20 be 4096 and 32
1d223 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 6d 2c 20 of. ** them,
1d224 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 53 respectively. S
1d225 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 o the maximum he
1d226 61 64 65 72 20 6c 65 6e 67 74 68 20 72 65 73 75 ader length resu
1d227 6c 74 73 20 66 72 6f 6d 20 61 0a 20 20 20 20 2a lts from a. *
1d228 2a 20 33 2d 62 79 74 65 20 74 79 70 65 20 66 6f * 3-byte type fo
1d229 72 20 65 61 63 68 20 6f 66 20 74 68 65 20 6d 61 r each of the ma
1d22a 78 69 6d 75 6d 20 6f 66 20 33 32 37 36 38 20 63 ximum of 32768 c
1d22b 6f 6c 75 6d 6e 73 20 70 6c 75 73 20 74 68 72 65 olumns plus thre
1d22c 65 0a 20 20 20 20 2a 2a 20 65 78 74 72 61 20 62 e. ** extra b
1d22d 79 74 65 73 20 66 6f 72 20 74 68 65 20 68 65 61 ytes for the hea
1d22e 64 65 72 20 6c 65 6e 67 74 68 20 69 74 73 65 6c der length itsel
1d22f 66 2e 20 20 33 32 37 36 38 2a 33 20 2b 20 33 20 f. 32768*3 + 3
1d230 3d 20 39 38 33 30 37 2e 0a 20 20 20 20 2a 2f 0a = 98307.. */.
1d231 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 6f 66 66 if( u.am.off
1d232 73 65 74 20 3e 20 39 38 33 30 37 20 29 7b 0a 20 set > 98307 ){.
1d233 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1d234 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
1d235 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c goto op_col
1d236 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a umn_out;. }..
1d237 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 69 /* Compute i
1d238 6e 20 75 2e 61 6d 2e 6c 65 6e 20 74 68 65 20 6e n u.am.len the n
1d239 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
1d23a 66 20 64 61 74 61 20 77 65 20 6e 65 65 64 20 74 f data we need t
1d23b 6f 20 72 65 61 64 20 69 6e 20 6f 72 64 65 72 0a o read in order.
1d23c 20 20 20 20 2a 2a 20 74 6f 20 67 65 74 20 75 2e ** to get u.
1d23d 61 6d 2e 6e 46 69 65 6c 64 20 74 79 70 65 20 76 am.nField type v
1d23e 61 6c 75 65 73 2e 20 20 75 2e 61 6d 2e 6f 66 66 alues. u.am.off
1d23f 73 65 74 20 69 73 20 61 6e 20 75 70 70 65 72 20 set is an upper
1d240 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 2e 20 20 bound on this.
1d241 42 75 74 0a 20 20 20 20 2a 2a 20 75 2e 61 6d 2e But. ** u.am.
1d242 6e 46 69 65 6c 64 20 6d 69 67 68 74 20 62 65 20 nField might be
1d243 73 69 67 6e 69 66 69 63 61 6e 74 6c 79 20 6c 65 significantly le
1d244 73 73 20 74 68 61 6e 20 74 68 65 20 74 72 75 65 ss than the true
1d245 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
1d246 6e 73 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 ns. ** in the
1d247 20 74 61 62 6c 65 2c 20 61 6e 64 20 69 6e 20 74 table, and in t
1d248 68 61 74 20 63 61 73 65 2c 20 35 2a 75 2e 61 6d hat case, 5*u.am
1d249 2e 6e 46 69 65 6c 64 2b 33 20 6d 69 67 68 74 20 .nField+3 might
1d24a 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 be smaller than
1d24b 75 2e 61 6d 2e 6f 66 66 73 65 74 2e 0a 20 20 20 u.am.offset..
1d24c 20 2a 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 6d ** We want to m
1d24d 69 6e 69 6d 69 7a 65 20 75 2e 61 6d 2e 6c 65 6e inimize u.am.len
1d24e 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6c 69 6d in order to lim
1d24f 69 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 it the size of t
1d250 68 65 20 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a he memory. **
1d251 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 65 73 70 allocation, esp
1d252 65 63 69 61 6c 6c 79 20 69 66 20 61 20 63 6f 72 ecially if a cor
1d253 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 rupt database fi
1d254 6c 65 20 68 61 73 20 63 61 75 73 65 64 20 75 2e le has caused u.
1d255 61 6d 2e 6f 66 66 73 65 74 0a 20 20 20 20 2a 2a am.offset. **
1d256 20 74 6f 20 62 65 20 6f 76 65 72 73 69 7a 65 64 to be oversized
1d257 2e 20 4f 66 66 73 65 74 20 69 73 20 6c 69 6d 69 . Offset is limi
1d258 74 65 64 20 74 6f 20 39 38 33 30 37 20 61 62 6f ted to 98307 abo
1d259 76 65 2e 20 20 42 75 74 20 39 38 33 30 37 20 6d ve. But 98307 m
1d25a 69 67 68 74 0a 20 20 20 20 2a 2a 20 73 74 69 6c ight. ** stil
1d25b 6c 20 65 78 63 65 65 64 20 52 6f 62 73 6f 6e 20 l exceed Robson
1d25c 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
1d25d 6e 20 6c 69 6d 69 74 73 20 6f 6e 20 73 6f 6d 65 n limits on some
1d25e 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 73 2e configurations.
1d25f 0a 20 20 20 20 2a 2a 20 4f 6e 20 73 79 73 74 65 . ** On syste
1d260 6d 73 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 74 ms that cannot t
1d261 6f 6c 65 72 61 74 65 20 6c 61 72 67 65 20 6d 65 olerate large me
1d262 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 mory allocations
1d263 2c 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 2b , u.am.nField*5+
1d264 33 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 6c 69 3. ** will li
1d265 6b 65 6c 79 20 62 65 20 6d 75 63 68 20 73 6d 61 kely be much sma
1d266 6c 6c 65 72 20 73 69 6e 63 65 20 75 2e 61 6d 2e ller since u.am.
1d267 6e 46 69 65 6c 64 20 77 69 6c 6c 20 6c 69 6b 65 nField will like
1d268 6c 79 20 62 65 20 6c 65 73 73 20 74 68 61 6e 0a ly be less than.
1d269 20 20 20 20 2a 2a 20 32 30 20 6f 72 20 73 6f 2e ** 20 or so.
1d26a 20 20 54 68 69 73 20 69 6e 73 75 72 65 73 20 74 This insures t
1d26b 68 61 74 20 52 6f 62 73 6f 6e 20 6d 65 6d 6f 72 hat Robson memor
1d26c 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 69 6d y allocation lim
1d26d 69 74 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e its are. ** n
1d26e 6f 74 20 65 78 63 65 65 64 65 64 20 65 76 65 6e ot exceeded even
1d26f 20 66 6f 72 20 63 6f 72 72 75 70 74 20 64 61 74 for corrupt dat
1d270 61 62 61 73 65 20 66 69 6c 65 73 2e 0a 20 20 20 abase files..
1d271 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 6c 65 6e */. u.am.len
1d272 20 3d 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 = u.am.nField*5
1d273 20 2b 20 33 3b 0a 20 20 20 20 69 66 28 20 75 2e + 3;. if( u.
1d274 61 6d 2e 6c 65 6e 20 3e 20 28 69 6e 74 29 75 2e am.len > (int)u.
1d275 61 6d 2e 6f 66 66 73 65 74 20 29 20 75 2e 61 6d am.offset ) u.am
1d276 2e 6c 65 6e 20 3d 20 28 69 6e 74 29 75 2e 61 6d .len = (int)u.am
1d277 2e 6f 66 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a .offset;.. /*
1d278 20 54 68 65 20 4b 65 79 46 65 74 63 68 28 29 20 The KeyFetch()
1d279 6f 72 20 44 61 74 61 46 65 74 63 68 28 29 20 61 or DataFetch() a
1d27a 62 6f 76 65 20 61 72 65 20 66 61 73 74 20 61 6e bove are fast an
1d27b 64 20 77 69 6c 6c 20 67 65 74 20 74 68 65 20 65 d will get the e
1d27c 6e 74 69 72 65 0a 20 20 20 20 2a 2a 20 72 65 63 ntire. ** rec
1d27d 6f 72 64 20 68 65 61 64 65 72 20 69 6e 20 6d 6f ord header in mo
1d27e 73 74 20 63 61 73 65 73 2e 20 20 42 75 74 20 74 st cases. But t
1d27f 68 65 79 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f hey will fail to
1d280 20 67 65 74 20 74 68 65 20 63 6f 6d 70 6c 65 74 get the complet
1d281 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 e. ** record
1d282 68 65 61 64 65 72 20 69 66 20 74 68 65 20 72 65 header if the re
1d283 63 6f 72 64 20 68 65 61 64 65 72 20 64 6f 65 73 cord header does
1d284 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 20 73 69 not fit on a si
1d285 6e 67 6c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a ngle page. **
1d286 20 69 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20 in the B-Tree.
1d287 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65 When that happe
1d288 6e 73 2c 20 75 73 65 20 73 71 6c 69 74 65 33 56 ns, use sqlite3V
1d289 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 dbeMemFromBtree(
1d28a 29 20 74 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75 ) to. ** acqu
1d28b 69 72 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 ire the complete
1d28c 20 68 65 61 64 65 72 20 74 65 78 74 2e 0a 20 20 header text..
1d28d 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 75 2e */. if( !u.
1d28e 61 6d 2e 7a 52 65 63 20 26 26 20 75 2e 61 6d 2e am.zRec && u.am.
1d28f 61 76 61 69 6c 3c 75 2e 61 6d 2e 6c 65 6e 20 29 avail<u.am.len )
1d290 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 73 4d 65 {. u.am.sMe
1d291 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 m.flags = 0;.
1d292 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e 64 62 20 u.am.sMem.db
1d293 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 0;. rc =
1d294 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 sqlite3VdbeMemFr
1d295 6f 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72 omBtree(u.am.pCr
1d296 73 72 2c 20 30 2c 20 75 2e 61 6d 2e 6c 65 6e 2c sr, 0, u.am.len,
1d297 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 u.am.pC->isInde
1d298 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d 29 3b 0a x, &u.am.sMem);.
1d299 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1d29a 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1d29b 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d goto op_colum
1d29c 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 n_out;. }.
1d29d 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20 u.am.zData
1d29e 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 = u.am.sMem.z;.
1d29f 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 7a 45 }. u.am.zE
1d2a0 6e 64 48 64 72 20 3d 20 28 75 38 20 2a 29 26 75 ndHdr = (u8 *)&u
1d2a1 2e 61 6d 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 6c .am.zData[u.am.l
1d2a2 65 6e 5d 3b 0a 20 20 20 20 75 2e 61 6d 2e 7a 49 en];. u.am.zI
1d2a3 64 78 20 3d 20 28 75 38 20 2a 29 26 75 2e 61 6d dx = (u8 *)&u.am
1d2a4 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 73 7a 48 64 .zData[u.am.szHd
1d2a5 72 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e r];.. /* Scan
1d2a6 20 74 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 the header and
1d2a7 75 73 65 20 69 74 20 74 6f 20 66 69 6c 6c 20 69 use it to fill i
1d2a8 6e 20 74 68 65 20 75 2e 61 6d 2e 61 54 79 70 65 n the u.am.aType
1d2a9 5b 5d 20 61 6e 64 20 75 2e 61 6d 2e 61 4f 66 66 [] and u.am.aOff
1d2aa 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 set[]. ** arr
1d2ab 61 79 73 2e 20 20 75 2e 61 6d 2e 61 54 79 70 65 ays. u.am.aType
1d2ac 5b 75 2e 61 6d 2e 69 5d 20 77 69 6c 6c 20 63 6f [u.am.i] will co
1d2ad 6e 74 61 69 6e 20 74 68 65 20 74 79 70 65 20 69 ntain the type i
1d2ae 6e 74 65 67 65 72 20 66 6f 72 20 74 68 65 20 75 nteger for the u
1d2af 2e 61 6d 2e 69 2d 74 68 0a 20 20 20 20 2a 2a 20 .am.i-th. **
1d2b0 63 6f 6c 75 6d 6e 20 61 6e 64 20 75 2e 61 6d 2e column and u.am.
1d2b1 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20 aOffset[u.am.i]
1d2b2 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65 will contain the
1d2b3 20 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 72 6f u.am.offset fro
1d2b4 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a m the beginning.
1d2b5 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 ** of the re
1d2b6 63 6f 72 64 20 74 6f 20 74 68 65 20 73 74 61 72 cord to the star
1d2b7 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f t of the data fo
1d2b8 72 20 74 68 65 20 75 2e 61 6d 2e 69 2d 74 68 20 r the u.am.i-th
1d2b9 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20 column. */.
1d2ba 20 20 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 u.am.offset64
1d2bb 3d 20 75 2e 61 6d 2e 6f 66 66 73 65 74 3b 0a 20 = u.am.offset;.
1d2bc 20 20 20 66 6f 72 28 75 2e 61 6d 2e 69 3d 30 3b for(u.am.i=0;
1d2bd 20 75 2e 61 6d 2e 69 3c 75 2e 61 6d 2e 6e 46 69 u.am.i<u.am.nFi
1d2be 65 6c 64 3b 20 75 2e 61 6d 2e 69 2b 2b 29 7b 0a eld; u.am.i++){.
1d2bf 20 20 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 7a if( u.am.z
1d2c0 49 64 78 3c 75 2e 61 6d 2e 7a 45 6e 64 48 64 72 Idx<u.am.zEndHdr
1d2c1 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d ){. u.am
1d2c2 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d .aOffset[u.am.i]
1d2c3 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e 6f 66 66 = (u32)u.am.off
1d2c4 73 65 74 36 34 3b 0a 20 20 20 20 20 20 20 20 75 set64;. u
1d2c5 2e 61 6d 2e 7a 49 64 78 20 2b 3d 20 67 65 74 56 .am.zIdx += getV
1d2c6 61 72 69 6e 74 33 32 28 75 2e 61 6d 2e 7a 49 64 arint32(u.am.zId
1d2c7 78 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e x, u.am.aType[u.
1d2c8 61 6d 2e 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 am.i]);.
1d2c9 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 2b 3d u.am.offset64 +=
1d2ca 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1d2cb 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d 2e 61 alTypeLen(u.am.a
1d2cc 54 79 70 65 5b 75 2e 61 6d 2e 69 5d 29 3b 0a 20 Type[u.am.i]);.
1d2cd 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1d2ce 20 20 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 69 /* If u.am.i
1d2cf 20 69 73 20 6c 65 73 73 20 74 68 61 74 20 75 2e is less that u.
1d2d0 61 6d 2e 6e 46 69 65 6c 64 2c 20 74 68 65 6e 20 am.nField, then
1d2d1 74 68 65 72 65 20 61 72 65 20 6c 65 73 73 20 66 there are less f
1d2d2 69 65 6c 64 73 20 69 6e 20 74 68 69 73 0a 20 20 ields in this.
1d2d3 20 20 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 ** record
1d2d4 74 68 61 6e 20 53 65 74 4e 75 6d 43 6f 6c 75 6d than SetNumColum
1d2d5 6e 73 20 69 6e 64 69 63 61 74 65 64 20 74 68 65 ns indicated the
1d2d6 72 65 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 69 re are columns i
1d2d7 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a n the. **
1d2d8 20 74 61 62 6c 65 2e 20 53 65 74 20 74 68 65 20 table. Set the
1d2d9 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 6f 72 20 u.am.offset for
1d2da 61 6e 79 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e any extra column
1d2db 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e s not present in
1d2dc 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 . ** the
1d2dd 72 65 63 6f 72 64 20 74 6f 20 30 2e 20 54 68 69 record to 0. Thi
1d2de 73 20 74 65 6c 6c 73 20 63 6f 64 65 20 62 65 6c s tells code bel
1d2df 6f 77 20 74 6f 20 73 74 6f 72 65 20 61 20 4e 55 ow to store a NU
1d2e0 4c 4c 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e LL. ** in
1d2e1 73 74 65 61 64 20 6f 66 20 64 65 73 65 72 69 61 stead of deseria
1d2e2 6c 69 7a 69 6e 67 20 61 20 76 61 6c 75 65 20 66 lizing a value f
1d2e3 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 0a rom the record..
1d2e4 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1d2e5 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b u.am.aOffset[
1d2e6 75 2e 61 6d 2e 69 5d 20 3d 20 30 3b 0a 20 20 20 u.am.i] = 0;.
1d2e7 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 }. }. s
1d2e8 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1d2e9 65 61 73 65 28 26 75 2e 61 6d 2e 73 4d 65 6d 29 ease(&u.am.sMem)
1d2ea 3b 0a 20 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e ;. u.am.sMem.
1d2eb 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1d2ec 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 ;.. /* If we
1d2ed 68 61 76 65 20 72 65 61 64 20 6d 6f 72 65 20 68 have read more h
1d2ee 65 61 64 65 72 20 64 61 74 61 20 74 68 61 6e 20 eader data than
1d2ef 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e was contained in
1d2f0 20 74 68 65 20 68 65 61 64 65 72 2c 0a 20 20 20 the header,.
1d2f1 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 65 6e ** or if the en
1d2f2 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69 d of the last fi
1d2f3 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 62 eld appears to b
1d2f4 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f e past the end o
1d2f5 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 63 f the. ** rec
1d2f6 6f 72 64 2c 20 6f 72 20 69 66 20 74 68 65 20 65 ord, or if the e
1d2f7 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 nd of the last f
1d2f8 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 ield appears to
1d2f9 62 65 20 62 65 66 6f 72 65 20 74 68 65 20 65 6e be before the en
1d2fa 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 d. ** of the
1d2fb 72 65 63 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c record (when all
1d2fc 20 66 69 65 6c 64 73 20 70 72 65 73 65 6e 74 29 fields present)
1d2fd 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 62 , then we must b
1d2fe 65 20 64 65 61 6c 69 6e 67 0a 20 20 20 20 2a 2a e dealing. **
1d2ff 20 77 69 74 68 20 61 20 63 6f 72 72 75 70 74 20 with a corrupt
1d300 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f database.. */
1d301 0a 20 20 20 20 69 66 28 20 28 75 2e 61 6d 2e 7a . if( (u.am.z
1d302 49 64 78 20 3e 20 75 2e 61 6d 2e 7a 45 6e 64 48 Idx > u.am.zEndH
1d303 64 72 29 7c 7c 20 28 75 2e 61 6d 2e 6f 66 66 73 dr)|| (u.am.offs
1d304 65 74 36 34 20 3e 20 75 2e 61 6d 2e 70 61 79 6c et64 > u.am.payl
1d305 6f 61 64 53 69 7a 65 29 0a 20 20 20 20 20 7c 7c oadSize). ||
1d306 20 28 75 2e 61 6d 2e 7a 49 64 78 3d 3d 75 2e 61 (u.am.zIdx==u.a
1d307 6d 2e 7a 45 6e 64 48 64 72 20 26 26 20 75 2e 61 m.zEndHdr && u.a
1d308 6d 2e 6f 66 66 73 65 74 36 34 21 3d 28 75 36 34 m.offset64!=(u64
1d309 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a )u.am.payloadSiz
1d30a 65 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d e) ){. rc =
1d30b 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1d30c 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f BKPT;. goto
1d30d 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a op_column_out;.
1d30e 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
1d30f 47 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 Get the column i
1d310 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 49 66 20 75 nformation. If u
1d311 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d .am.aOffset[u.am
1d312 2e 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f .p2] is non-zero
1d313 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 64 65 73 65 , then. ** dese
1d314 72 69 61 6c 69 7a 65 20 74 68 65 20 76 61 6c 75 rialize the valu
1d315 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 e from the recor
1d316 64 2e 20 49 66 20 75 2e 61 6d 2e 61 4f 66 66 73 d. If u.am.aOffs
1d317 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 69 73 20 7a et[u.am.p2] is z
1d318 65 72 6f 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 ero,. ** then t
1d319 68 65 72 65 20 61 72 65 20 6e 6f 74 20 65 6e 6f here are not eno
1d31a 75 67 68 20 66 69 65 6c 64 73 20 69 6e 20 74 68 ugh fields in th
1d31b 65 20 72 65 63 6f 72 64 20 74 6f 20 73 61 74 69 e record to sati
1d31c 73 66 79 20 74 68 65 0a 20 20 2a 2a 20 72 65 71 sfy the. ** req
1d31d 75 65 73 74 2e 20 20 49 6e 20 74 68 69 73 20 63 uest. In this c
1d31e 61 73 65 2c 20 73 65 74 20 74 68 65 20 76 61 6c ase, set the val
1d31f 75 65 20 4e 55 4c 4c 20 6f 72 20 74 6f 20 50 34 ue NULL or to P4
1d320 20 69 66 20 50 34 20 69 73 0a 20 20 2a 2a 20 61 if P4 is. ** a
1d321 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 pointer to a Me
1d322 6d 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f 0a 20 m object.. */.
1d323 20 69 66 28 20 75 2e 61 6d 2e 61 4f 66 66 73 65 if( u.am.aOffse
1d324 74 5b 75 2e 61 6d 2e 70 32 5d 20 29 7b 0a 20 20 t[u.am.p2] ){.
1d325 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
1d326 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 LITE_OK );. i
1d327 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a f( u.am.zRec ){.
1d328 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1d329 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 eMemReleaseExter
1d32a 6e 61 6c 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b nal(u.am.pDest);
1d32b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1d32c 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 20 beSerialGet((u8
1d32d 2a 29 26 75 2e 61 6d 2e 7a 52 65 63 5b 75 2e 61 *)&u.am.zRec[u.a
1d32e 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70 m.aOffset[u.am.p
1d32f 32 5d 5d 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b 2]], u.am.aType[
1d330 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 70 u.am.p2], u.am.p
1d331 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 Dest);. }else
1d332 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 6c 65 6e {. u.am.len
1d333 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 = sqlite3VdbeSe
1d334 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d rialTypeLen(u.am
1d335 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 5d 29 .aType[u.am.p2])
1d336 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1d337 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 61 6d dbeMemMove(&u.am
1d338 2e 73 4d 65 6d 2c 20 75 2e 61 6d 2e 70 44 65 73 .sMem, u.am.pDes
1d339 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 t);. rc = s
1d33a 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f qlite3VdbeMemFro
1d33b 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72 73 mBtree(u.am.pCrs
1d33c 72 2c 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b r, u.am.aOffset[
1d33d 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 6c u.am.p2], u.am.l
1d33e 65 6e 2c 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 en, u.am.pC->isI
1d33f 6e 64 65 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d ndex, &u.am.sMem
1d340 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
1d341 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1d342 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f goto op_co
1d343 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 lumn_out;.
1d344 7d 0a 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 }. u.am.zDa
1d345 74 61 20 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a ta = u.am.sMem.z
1d346 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1d347 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 dbeSerialGet((u8
1d348 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 2c 20 75 2e *)u.am.zData, u.
1d349 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 am.aType[u.am.p2
1d34a 5d 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a ], u.am.pDest);.
1d34b 20 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 70 }. u.am.p
1d34c 44 65 73 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f Dest->enc = enco
1d34d 64 69 6e 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ding;. }else{.
1d34e 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 if( pOp->p4ty
1d34f 70 65 3d 3d 50 34 5f 4d 45 4d 20 29 7b 0a 20 20 pe==P4_MEM ){.
1d350 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1d351 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 75 2e emShallowCopy(u.
1d352 61 6d 2e 70 44 65 73 74 2c 20 70 4f 70 2d 3e 70 am.pDest, pOp->p
1d353 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53 74 61 74 4.pMem, MEM_Stat
1d354 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ic);. }else{.
1d355 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e assert( u.
1d356 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 am.pDest->flags&
1d357 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 MEM_Null );.
1d358 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 }. }.. /* If w
1d359 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c e dynamically al
1d35a 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 74 6f located space to
1d35b 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 28 hold the data (
1d35c 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 in the. ** sqli
1d35d 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 te3VdbeMemFromBt
1d35e 72 65 65 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 ree() call above
1d35f 29 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 ) then transfer
1d360 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61 74 0a control of that.
1d361 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79 ** dynamically
1d362 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 allocated space
1d363 20 6f 76 65 72 20 74 6f 20 74 68 65 20 75 2e 61 over to the u.a
1d364 6d 2e 70 44 65 73 74 20 73 74 72 75 63 74 75 72 m.pDest structur
1d365 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70 72 65 e.. ** This pre
1d366 76 65 6e 74 73 20 61 20 6d 65 6d 6f 72 79 20 63 vents a memory c
1d367 6f 70 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 opy.. */. if(
1d368 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f u.am.sMem.zMallo
1d369 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 c ){. assert(
1d36a 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3d 3d 75 2e u.am.sMem.z==u.
1d36b 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 am.sMem.zMalloc
1d36c 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 );. assert( !
1d36d 28 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 (u.am.pDest->fla
1d36e 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 20 29 3b gs & MEM_Dyn) );
1d36f 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 28 75 . assert( !(u
1d370 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 .am.pDest->flags
1d371 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d & (MEM_Blob|MEM
1d372 5f 53 74 72 29 29 20 7c 7c 20 75 2e 61 6d 2e 70 _Str)) || u.am.p
1d373 44 65 73 74 2d 3e 7a 3d 3d 75 2e 61 6d 2e 73 4d Dest->z==u.am.sM
1d374 65 6d 2e 7a 20 29 3b 0a 20 20 20 20 75 2e 61 6d em.z );. u.am
1d375 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 3d .pDest->flags &=
1d376 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d ~(MEM_Ephem|MEM
1d377 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 75 2e _Static);. u.
1d378 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 am.pDest->flags
1d379 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 |= MEM_Term;.
1d37a 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 20 3d u.am.pDest->z =
1d37b 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20 u.am.sMem.z;.
1d37c 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 4d u.am.pDest->zM
1d37d 61 6c 6c 6f 63 20 3d 20 75 2e 61 6d 2e 73 4d 65 alloc = u.am.sMe
1d37e 6d 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a m.zMalloc;. }..
1d37f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1d380 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 beMemMakeWriteab
1d381 6c 65 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a le(u.am.pDest);.
1d382 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a 0a .op_column_out:.
1d383 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1d384 42 53 49 5a 45 28 75 2e 61 6d 2e 70 44 65 73 74 BSIZE(u.am.pDest
1d385 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 );. REGISTER_TR
1d386 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 61 ACE(pOp->p3, u.a
1d387 6d 2e 70 44 65 73 74 29 3b 0a 20 20 62 72 65 61 m.pDest);. brea
1d388 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1d389 20 41 66 66 69 6e 69 74 79 20 50 31 20 50 32 20 Affinity P1 P2
1d38a 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70 * P4 *.**.** App
1d38b 6c 79 20 61 66 66 69 6e 69 74 69 65 73 20 74 6f ly affinities to
1d38c 20 61 20 72 61 6e 67 65 20 6f 66 20 50 32 20 72 a range of P2 r
1d38d 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e egisters startin
1d38e 67 20 77 69 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a g with P1..**.**
1d38f 20 50 34 20 69 73 20 61 20 73 74 72 69 6e 67 20 P4 is a string
1d390 74 68 61 74 20 69 73 20 50 32 20 63 68 61 72 61 that is P2 chara
1d391 63 74 65 72 73 20 6c 6f 6e 67 2e 20 54 68 65 20 cters long. The
1d392 6e 74 68 20 63 68 61 72 61 63 74 65 72 20 6f 66 nth character of
1d393 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 the.** string i
1d394 6e 64 69 63 61 74 65 73 20 74 68 65 20 63 6f 6c ndicates the col
1d395 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 68 61 umn affinity tha
1d396 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 t should be used
1d397 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20 for the nth.**
1d398 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74 memory cell in t
1d399 68 65 20 72 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73 he range..*/.cas
1d39a 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 3a 20 7b e OP_Affinity: {
1d39b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1d39c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1d39d 20 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f 0a 20 20 into u.an */.
1d39e 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b char *zAffinity;
1d39f 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 /* The affini
1d3a0 74 79 20 74 6f 20 62 65 20 61 70 70 6c 69 65 64 ty to be applied
1d3a1 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 */. Mem *pData
1d3a2 30 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 0; /* Firs
1d3a3 74 20 72 65 67 69 73 74 65 72 20 74 6f 20 77 68 t register to wh
1d3a4 69 63 68 20 74 6f 20 61 70 70 6c 79 20 61 66 66 ich to apply aff
1d3a5 69 6e 69 74 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a inity */. Mem *
1d3a6 70 4c 61 73 74 3b 20 20 20 20 20 20 20 20 2f 2a pLast; /*
1d3a7 20 4c 61 73 74 20 72 65 67 69 73 74 65 72 20 74 Last register t
1d3a8 6f 20 77 68 69 63 68 20 74 6f 20 61 70 70 6c 79 o which to apply
1d3a9 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20 20 4d affinity */. M
1d3aa 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20 20 em *pRec;
1d3ab 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 72 65 67 /* Current reg
1d3ac 69 73 74 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20 ister */.#endif
1d3ad 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1d3ae 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1d3af 61 6e 20 2a 2f 0a 0a 20 20 75 2e 61 6e 2e 7a 41 an */.. u.an.zA
1d3b0 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 ffinity = pOp->p
1d3b1 34 2e 7a 3b 0a 20 20 75 2e 61 6e 2e 70 44 61 74 4.z;. u.an.pDat
1d3b2 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f a0 = &p->aMem[pO
1d3b3 70 2d 3e 70 31 5d 3b 0a 20 20 75 2e 61 6e 2e 70 p->p1];. u.an.p
1d3b4 4c 61 73 74 20 3d 20 26 75 2e 61 6e 2e 70 44 61 Last = &u.an.pDa
1d3b5 74 61 30 5b 70 4f 70 2d 3e 70 32 2d 31 5d 3b 0a ta0[pOp->p2-1];.
1d3b6 20 20 66 6f 72 28 75 2e 61 6e 2e 70 52 65 63 3d for(u.an.pRec=
1d3b7 75 2e 61 6e 2e 70 44 61 74 61 30 3b 20 75 2e 61 u.an.pData0; u.a
1d3b8 6e 2e 70 52 65 63 3c 3d 75 2e 61 6e 2e 70 4c 61 n.pRec<=u.an.pLa
1d3b9 73 74 3b 20 75 2e 61 6e 2e 70 52 65 63 2b 2b 29 st; u.an.pRec++)
1d3ba 7b 0a 20 20 20 20 45 78 70 61 6e 64 42 6c 6f 62 {. ExpandBlob
1d3bb 28 75 2e 61 6e 2e 70 52 65 63 29 3b 0a 20 20 20 (u.an.pRec);.
1d3bc 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 75 applyAffinity(u
1d3bd 2e 61 6e 2e 70 52 65 63 2c 20 75 2e 61 6e 2e 7a .an.pRec, u.an.z
1d3be 41 66 66 69 6e 69 74 79 5b 75 2e 61 6e 2e 70 52 Affinity[u.an.pR
1d3bf 65 63 2d 75 2e 61 6e 2e 70 44 61 74 61 30 5d 2c ec-u.an.pData0],
1d3c0 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a encoding);. }.
1d3c1 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1d3c2 70 63 6f 64 65 3a 20 4d 61 6b 65 52 65 63 6f 72 pcode: MakeRecor
1d3c3 64 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a d P1 P2 P3 P4 *.
1d3c4 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 50 32 **.** Convert P2
1d3c5 20 72 65 67 69 73 74 65 72 73 20 62 65 67 69 6e registers begin
1d3c6 6e 69 6e 67 20 77 69 74 68 20 50 31 20 69 6e 74 ning with P1 int
1d3c7 6f 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 o a single entry
1d3c8 0a 2a 2a 20 73 75 69 74 61 62 6c 65 20 66 6f 72 .** suitable for
1d3c9 20 75 73 65 20 61 73 20 61 20 64 61 74 61 20 72 use as a data r
1d3ca 65 63 6f 72 64 20 69 6e 20 61 20 64 61 74 61 62 ecord in a datab
1d3cb 61 73 65 20 74 61 62 6c 65 20 6f 72 20 61 73 20 ase table or as
1d3cc 61 20 6b 65 79 0a 2a 2a 20 69 6e 20 61 6e 20 69 a key.** in an i
1d3cd 6e 64 65 78 2e 20 20 54 68 65 20 64 65 74 61 69 ndex. The detai
1d3ce 6c 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 ls of the format
1d3cf 20 61 72 65 20 69 72 72 65 6c 65 76 61 6e 74 20 are irrelevant
1d3d0 61 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 74 68 as long as.** th
1d3d1 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f e OP_Column opco
1d3d2 64 65 20 63 61 6e 20 64 65 63 6f 64 65 20 74 68 de can decode th
1d3d3 65 20 72 65 63 6f 72 64 20 6c 61 74 65 72 2e 0a e record later..
1d3d4 2a 2a 20 52 65 66 65 72 20 74 6f 20 73 6f 75 72 ** Refer to sour
1d3d5 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 ce code comments
1d3d6 20 66 6f 72 20 74 68 65 20 64 65 74 61 69 6c 73 for the details
1d3d7 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 0a 2a of the record.*
1d3d8 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 * format..**.**
1d3d9 50 34 20 6d 61 79 20 62 65 20 61 20 73 74 72 69 P4 may be a stri
1d3da 6e 67 20 74 68 61 74 20 69 73 20 50 32 20 63 68 ng that is P2 ch
1d3db 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 aracters long.
1d3dc 54 68 65 20 6e 74 68 20 63 68 61 72 61 63 74 65 The nth characte
1d3dd 72 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74 72 69 r of the.** stri
1d3de 6e 67 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 ng indicates the
1d3df 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 column affinity
1d3e0 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 that should be
1d3e1 75 73 65 64 20 66 6f 72 20 74 68 65 20 6e 74 68 used for the nth
1d3e2 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 .** field of the
1d3e3 20 69 6e 64 65 78 20 6b 65 79 2e 0a 2a 2a 0a 2a index key..**.*
1d3e4 2a 20 54 68 65 20 6d 61 70 70 69 6e 67 20 66 72 * The mapping fr
1d3e5 6f 6d 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 om character to
1d3e6 61 66 66 69 6e 69 74 79 20 69 73 20 67 69 76 65 affinity is give
1d3e7 6e 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f n by the SQLITE_
1d3e8 41 46 46 5f 0a 2a 2a 20 6d 61 63 72 6f 73 20 64 AFF_.** macros d
1d3e9 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 efined in sqlite
1d3ea 49 6e 74 2e 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 Int.h..**.** If
1d3eb 50 34 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 P4 is NULL then
1d3ec 61 6c 6c 20 69 6e 64 65 78 20 66 69 65 6c 64 73 all index fields
1d3ed 20 68 61 76 65 20 74 68 65 20 61 66 66 69 6e 69 have the affini
1d3ee 74 79 20 4e 4f 4e 45 2e 0a 2a 2f 0a 63 61 73 65 ty NONE..*/.case
1d3ef 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 3a 20 OP_MakeRecord:
1d3f0 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1d3f1 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1d3f2 64 20 69 6e 74 6f 20 75 2e 61 6f 20 2a 2f 0a 20 d into u.ao */.
1d3f3 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f 72 64 3b u8 *zNewRecord;
1d3f4 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62 75 66 /* A buf
1d3f5 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 fer to hold the
1d3f6 64 61 74 61 20 66 6f 72 20 74 68 65 20 6e 65 77 data for the new
1d3f7 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d record */. Mem
1d3f8 20 2a 70 52 65 63 3b 20 20 20 20 20 20 20 20 20 *pRec;
1d3f9 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 /* The new r
1d3fa 65 63 6f 72 64 20 2a 2f 0a 20 20 75 36 34 20 6e ecord */. u64 n
1d3fb 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 Data;
1d3fc 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1d3fd 79 74 65 73 20 6f 66 20 64 61 74 61 20 73 70 61 ytes of data spa
1d3fe 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 64 72 ce */. int nHdr
1d3ff 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1d400 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
1d401 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 63 s of header spac
1d402 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 65 e */. i64 nByte
1d403 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1d404 20 44 61 74 61 20 73 70 61 63 65 20 72 65 71 75 Data space requ
1d405 69 72 65 64 20 66 6f 72 20 74 68 69 73 20 72 65 ired for this re
1d406 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a cord */. int nZ
1d407 65 72 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 ero;
1d408 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 /* Number of ze
1d409 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20 ro bytes at the
1d40a 65 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 end of the recor
1d40b 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 69 d */. int nVari
1d40c 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a nt; /*
1d40d 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1d40e 20 69 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a in a varint */.
1d40f 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 u32 serial_typ
1d410 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 e; /* Type
1d411 20 66 69 65 6c 64 20 2a 2f 0a 20 20 4d 65 6d 20 field */. Mem
1d412 2a 70 44 61 74 61 30 3b 20 20 20 20 20 20 20 20 *pData0;
1d413 20 20 20 2f 2a 20 46 69 72 73 74 20 66 69 65 6c /* First fiel
1d414 64 20 74 6f 20 62 65 20 63 6f 6d 62 69 6e 65 64 d to be combined
1d415 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f 72 64 into the record
1d416 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 74 */. Mem *pLast
1d417 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1d418 4c 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74 68 Last field of th
1d419 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e e record */. in
1d41a 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 t nField;
1d41b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1d41c 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 f fields in the
1d41d 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 68 61 72 record */. char
1d41e 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 20 *zAffinity;
1d41f 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 /* The affini
1d420 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 ty string for th
1d421 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e e record */. in
1d422 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 t file_format;
1d423 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 66 6f 72 /* File for
1d424 6d 61 74 20 74 6f 20 75 73 65 20 66 6f 72 20 65 mat to use for e
1d425 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 ncoding */. int
1d426 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
1d427 20 20 20 20 2f 2a 20 53 70 61 63 65 20 75 73 65 /* Space use
1d428 64 20 69 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b d in zNewRecord[
1d429 5d 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 ] */. int len;
1d42a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d42b 20 4c 65 6e 67 74 68 20 6f 66 20 61 20 66 69 65 Length of a fie
1d42c 6c 64 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 ld */.#endif /*
1d42d 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1d42e 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6f 20 moved into u.ao
1d42f 2a 2f 0a 0a 20 20 2f 2a 20 41 73 73 75 6d 69 6e */.. /* Assumin
1d430 67 20 74 68 65 20 72 65 63 6f 72 64 20 63 6f 6e g the record con
1d431 74 61 69 6e 73 20 4e 20 66 69 65 6c 64 73 2c 20 tains N fields,
1d432 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d 61 the record forma
1d433 74 20 6c 6f 6f 6b 73 0a 20 20 2a 2a 20 6c 69 6b t looks. ** lik
1d434 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a e this:. **. *
1d435 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * --------------
1d436 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d437 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d438 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d439 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 ----------. **
1d43a 7c 20 68 64 72 2d 73 69 7a 65 20 7c 20 74 79 70 | hdr-size | typ
1d43b 65 20 30 20 7c 20 74 79 70 65 20 31 20 7c 20 2e e 0 | type 1 | .
1d43c 2e 2e 20 7c 20 74 79 70 65 20 4e 2d 31 20 7c 20 .. | type N-1 |
1d43d 64 61 74 61 30 20 7c 20 2e 2e 2e 20 7c 20 64 61 data0 | ... | da
1d43e 74 61 20 4e 2d 31 20 7c 0a 20 20 2a 2a 20 2d 2d ta N-1 |. ** --
1d43f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d440 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d441 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d442 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d443 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 0a 20 20 2a 2a ------. **. **
1d444 20 44 61 74 61 28 30 29 20 69 73 20 74 61 6b 65 Data(0) is take
1d445 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 n from register
1d446 50 31 2e 20 20 44 61 74 61 28 31 29 20 63 6f 6d P1. Data(1) com
1d447 65 73 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 es from register
1d448 20 50 31 2b 31 0a 20 20 2a 2a 20 61 6e 64 20 73 P1+1. ** and s
1d449 6f 20 66 72 6f 74 68 2e 0a 20 20 2a 2a 0a 20 20 o froth.. **.
1d44a 2a 2a 20 45 61 63 68 20 74 79 70 65 20 66 69 65 ** Each type fie
1d44b 6c 64 20 69 73 20 61 20 76 61 72 69 6e 74 20 72 ld is a varint r
1d44c 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 epresenting the
1d44d 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 serial type of t
1d44e 68 65 0a 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f he. ** correspo
1d44f 6e 64 69 6e 67 20 64 61 74 61 20 65 6c 65 6d 65 nding data eleme
1d450 6e 74 20 28 73 65 65 20 73 71 6c 69 74 65 33 56 nt (see sqlite3V
1d451 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 29 29 dbeSerialType())
1d452 2e 20 54 68 65 0a 20 20 2a 2a 20 68 64 72 2d 73 . The. ** hdr-s
1d453 69 7a 65 20 66 69 65 6c 64 20 69 73 20 61 6c 73 ize field is als
1d454 6f 20 61 20 76 61 72 69 6e 74 20 77 68 69 63 68 o a varint which
1d455 20 69 73 20 74 68 65 20 6f 66 66 73 65 74 20 66 is the offset f
1d456 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e rom the beginnin
1d457 67 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 g. ** of the re
1d458 63 6f 72 64 20 74 6f 20 64 61 74 61 30 2e 0a 20 cord to data0..
1d459 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 44 61 74 61 */. u.ao.nData
1d45a 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a = 0; /*
1d45b 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1d45c 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 20 2a of data space *
1d45d 2f 0a 20 20 75 2e 61 6f 2e 6e 48 64 72 20 3d 20 /. u.ao.nHdr =
1d45e 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 0; /* N
1d45f 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
1d460 66 20 68 65 61 64 65 72 20 73 70 61 63 65 20 2a f header space *
1d461 2f 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20 3d /. u.ao.nByte =
1d462 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44 0; /* D
1d463 61 74 61 20 73 70 61 63 65 20 72 65 71 75 69 72 ata space requir
1d464 65 64 20 66 6f 72 20 74 68 69 73 20 72 65 63 6f ed for this reco
1d465 72 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 5a 65 rd */. u.ao.nZe
1d466 72 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ro = 0;
1d467 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 /* Number of zer
1d468 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20 65 o bytes at the e
1d469 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 nd of the record
1d46a 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 46 69 65 6c */. u.ao.nFiel
1d46b 64 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 d = pOp->p1;. u
1d46c 2e 61 6f 2e 7a 41 66 66 69 6e 69 74 79 20 3d 20 .ao.zAffinity =
1d46d 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 61 73 73 pOp->p4.z;. ass
1d46e 65 72 74 28 20 75 2e 61 6f 2e 6e 46 69 65 6c 64 ert( u.ao.nField
1d46f 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3e 30 20 >0 && pOp->p2>0
1d470 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2e 61 6f 2e && pOp->p2+u.ao.
1d471 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d 65 6d 2b nField<=p->nMem+
1d472 31 20 29 3b 0a 20 20 75 2e 61 6f 2e 70 44 61 74 1 );. u.ao.pDat
1d473 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e a0 = &p->aMem[u.
1d474 61 6f 2e 6e 46 69 65 6c 64 5d 3b 0a 20 20 75 2e ao.nField];. u.
1d475 61 6f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d ao.nField = pOp-
1d476 3e 70 32 3b 0a 20 20 75 2e 61 6f 2e 70 4c 61 73 >p2;. u.ao.pLas
1d477 74 20 3d 20 26 75 2e 61 6f 2e 70 44 61 74 61 30 t = &u.ao.pData0
1d478 5b 75 2e 61 6f 2e 6e 46 69 65 6c 64 2d 31 5d 3b [u.ao.nField-1];
1d479 0a 20 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72 . u.ao.file_for
1d47a 6d 61 74 20 3d 20 70 2d 3e 6d 69 6e 57 72 69 74 mat = p->minWrit
1d47b 65 46 69 6c 65 46 6f 72 6d 61 74 3b 0a 0a 20 20 eFileFormat;..
1d47c 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 /* Loop through
1d47d 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 the elements tha
1d47e 74 20 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20 74 t will make up t
1d47f 68 65 20 72 65 63 6f 72 64 20 74 6f 20 66 69 67 he record to fig
1d480 75 72 65 0a 20 20 2a 2a 20 6f 75 74 20 68 6f 77 ure. ** out how
1d481 20 6d 75 63 68 20 73 70 61 63 65 20 69 73 20 72 much space is r
1d482 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 65 20 equired for the
1d483 6e 65 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2f new record.. */
1d484 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52 65 63 . for(u.ao.pRec
1d485 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20 75 2e =u.ao.pData0; u.
1d486 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c ao.pRec<=u.ao.pL
1d487 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63 2b 2b ast; u.ao.pRec++
1d488 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f 2e ){. if( u.ao.
1d489 7a 41 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 zAffinity ){.
1d48a 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 applyAffinity
1d48b 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61 6f (u.ao.pRec, u.ao
1d48c 2e 7a 41 66 66 69 6e 69 74 79 5b 75 2e 61 6f 2e .zAffinity[u.ao.
1d48d 70 52 65 63 2d 75 2e 61 6f 2e 70 44 61 74 61 30 pRec-u.ao.pData0
1d48e 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 ], encoding);.
1d48f 20 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 61 6f }. if( u.ao
1d490 2e 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d .pRec->flags&MEM
1d491 5f 5a 65 72 6f 20 26 26 20 75 2e 61 6f 2e 70 52 _Zero && u.ao.pR
1d492 65 63 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20 ec->n>0 ){.
1d493 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 sqlite3VdbeMemE
1d494 78 70 61 6e 64 42 6c 6f 62 28 75 2e 61 6f 2e 70 xpandBlob(u.ao.p
1d495 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Rec);. }.
1d496 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 u.ao.serial_type
1d497 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 = sqlite3VdbeSe
1d498 72 69 61 6c 54 79 70 65 28 75 2e 61 6f 2e 70 52 rialType(u.ao.pR
1d499 65 63 2c 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f ec, u.ao.file_fo
1d49a 72 6d 61 74 29 3b 0a 20 20 20 20 75 2e 61 6f 2e rmat);. u.ao.
1d49b 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 len = sqlite3Vdb
1d49c 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 eSerialTypeLen(u
1d49d 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 29 .ao.serial_type)
1d49e 3b 0a 20 20 20 20 75 2e 61 6f 2e 6e 44 61 74 61 ;. u.ao.nData
1d49f 20 2b 3d 20 75 2e 61 6f 2e 6c 65 6e 3b 0a 20 20 += u.ao.len;.
1d4a0 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 73 u.ao.nHdr += s
1d4a1 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 qlite3VarintLen(
1d4a2 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 u.ao.serial_type
1d4a3 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f 2e );. if( u.ao.
1d4a4 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pRec->flags & ME
1d4a5 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 M_Zero ){.
1d4a6 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20 7a 65 72 /* Only pure zer
1d4a7 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42 73 20 63 o-filled BLOBs c
1d4a8 61 6e 20 62 65 20 69 6e 70 75 74 20 74 6f 20 74 an be input to t
1d4a9 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20 20 20 20 his Opcode..
1d4aa 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 61 ** We do not a
1d4ab 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69 74 68 20 llow blobs with
1d4ac 61 20 70 72 65 66 69 78 20 61 6e 64 20 61 20 7a a prefix and a z
1d4ad 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e ero-filled tail.
1d4ae 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 6f 2e 6e */. u.ao.n
1d4af 5a 65 72 6f 20 2b 3d 20 75 2e 61 6f 2e 70 52 65 Zero += u.ao.pRe
1d4b0 63 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 c->u.nZero;.
1d4b1 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 6f 2e 6c }else if( u.ao.l
1d4b2 65 6e 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 6f en ){. u.ao
1d4b3 2e 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 20 .nZero = 0;.
1d4b4 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 }. }.. /* Add
1d4b5 74 68 65 20 69 6e 69 74 69 61 6c 20 68 65 61 64 the initial head
1d4b6 65 72 20 76 61 72 69 6e 74 20 61 6e 64 20 74 6f er varint and to
1d4b7 74 61 6c 20 74 68 65 20 73 69 7a 65 20 2a 2f 0a tal the size */.
1d4b8 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 75 u.ao.nHdr += u
1d4b9 2e 61 6f 2e 6e 56 61 72 69 6e 74 20 3d 20 73 71 .ao.nVarint = sq
1d4ba 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 lite3VarintLen(u
1d4bb 2e 61 6f 2e 6e 48 64 72 29 3b 0a 20 20 69 66 28 .ao.nHdr);. if(
1d4bc 20 75 2e 61 6f 2e 6e 56 61 72 69 6e 74 3c 73 71 u.ao.nVarint<sq
1d4bd 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 lite3VarintLen(u
1d4be 2e 61 6f 2e 6e 48 64 72 29 20 29 7b 0a 20 20 20 .ao.nHdr) ){.
1d4bf 20 75 2e 61 6f 2e 6e 48 64 72 2b 2b 3b 0a 20 20 u.ao.nHdr++;.
1d4c0 7d 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20 3d }. u.ao.nByte =
1d4c1 20 75 2e 61 6f 2e 6e 48 64 72 2b 75 2e 61 6f 2e u.ao.nHdr+u.ao.
1d4c2 6e 44 61 74 61 2d 75 2e 61 6f 2e 6e 5a 65 72 6f nData-u.ao.nZero
1d4c3 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 42 79 ;. if( u.ao.nBy
1d4c4 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 te>db->aLimit[SQ
1d4c5 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
1d4c6 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 H] ){. goto t
1d4c7 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 2f oo_big;. }.. /
1d4c8 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 * Make sure the
1d4c9 6f 75 74 70 75 74 20 72 65 67 69 73 74 65 72 20 output register
1d4ca 68 61 73 20 61 20 62 75 66 66 65 72 20 6c 61 72 has a buffer lar
1d4cb 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f ge enough to sto
1d4cc 72 65 0a 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 re. ** the new
1d4cd 72 65 63 6f 72 64 2e 20 54 68 65 20 6f 75 74 70 record. The outp
1d4ce 75 74 20 72 65 67 69 73 74 65 72 20 28 70 4f 70 ut register (pOp
1d4cf 2d 3e 70 33 29 20 69 73 20 6e 6f 74 20 61 6c 6c ->p3) is not all
1d4d0 6f 77 65 64 20 74 6f 0a 20 20 2a 2a 20 62 65 20 owed to. ** be
1d4d1 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74 one of the input
1d4d2 20 72 65 67 69 73 74 65 72 73 20 28 62 65 63 61 registers (beca
1d4d3 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e use the followin
1d4d4 67 20 63 61 6c 6c 20 74 6f 0a 20 20 2a 2a 20 73 g call to. ** s
1d4d5 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
1d4d6 77 28 29 20 63 6f 75 6c 64 20 63 6c 6f 62 62 65 w() could clobbe
1d4d7 72 20 74 68 65 20 76 61 6c 75 65 20 62 65 66 6f r the value befo
1d4d8 72 65 20 69 74 20 69 73 20 75 73 65 64 29 2e 0a re it is used)..
1d4d9 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
1d4da 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 31 20 7c Op->p3<pOp->p1 |
1d4db 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e | pOp->p3>=pOp->
1d4dc 70 31 2b 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 p1+pOp->p2 );.
1d4dd 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pOut = &p->aMem[
1d4de 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 pOp->p3];. if(
1d4df 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
1d4e0 6f 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e ow(pOut, (int)u.
1d4e1 61 6f 2e 6e 42 79 74 65 2c 20 30 29 20 29 7b 0a ao.nByte, 0) ){.
1d4e2 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b goto no_mem;
1d4e3 0a 20 20 7d 0a 20 20 75 2e 61 6f 2e 7a 4e 65 77 . }. u.ao.zNew
1d4e4 52 65 63 6f 72 64 20 3d 20 28 75 38 20 2a 29 70 Record = (u8 *)p
1d4e5 4f 75 74 2d 3e 7a 3b 0a 0a 20 20 2f 2a 20 57 72 Out->z;.. /* Wr
1d4e6 69 74 65 20 74 68 65 20 72 65 63 6f 72 64 20 2a ite the record *
1d4e7 2f 0a 20 20 75 2e 61 6f 2e 69 20 3d 20 70 75 74 /. u.ao.i = put
1d4e8 56 61 72 69 6e 74 33 32 28 75 2e 61 6f 2e 7a 4e Varint32(u.ao.zN
1d4e9 65 77 52 65 63 6f 72 64 2c 20 75 2e 61 6f 2e 6e ewRecord, u.ao.n
1d4ea 48 64 72 29 3b 0a 20 20 66 6f 72 28 75 2e 61 6f Hdr);. for(u.ao
1d4eb 2e 70 52 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 .pRec=u.ao.pData
1d4ec 30 3b 20 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 0; u.ao.pRec<=u.
1d4ed 61 6f 2e 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 ao.pLast; u.ao.p
1d4ee 52 65 63 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 6f Rec++){. u.ao
1d4ef 2e 73 65 72 69 61 6c 5f 74 79 70 65 20 3d 20 73 .serial_type = s
1d4f0 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1d4f1 54 79 70 65 28 75 2e 61 6f 2e 70 52 65 63 2c 20 Type(u.ao.pRec,
1d4f2 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72 6d 61 74 u.ao.file_format
1d4f3 29 3b 0a 20 20 20 20 75 2e 61 6f 2e 69 20 2b 3d );. u.ao.i +=
1d4f4 20 70 75 74 56 61 72 69 6e 74 33 32 28 26 75 2e putVarint32(&u.
1d4f5 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e ao.zNewRecord[u.
1d4f6 61 6f 2e 69 5d 2c 20 75 2e 61 6f 2e 73 65 72 69 ao.i], u.ao.seri
1d4f7 61 6c 5f 74 79 70 65 29 3b 20 20 20 20 20 20 2f al_type); /
1d4f8 2a 20 73 65 72 69 61 6c 20 74 79 70 65 20 2a 2f * serial type */
1d4f9 0a 20 20 7d 0a 20 20 66 6f 72 28 75 2e 61 6f 2e . }. for(u.ao.
1d4fa 70 52 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 pRec=u.ao.pData0
1d4fb 3b 20 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 ; u.ao.pRec<=u.a
1d4fc 6f 2e 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 o.pLast; u.ao.pR
1d4fd 65 63 2b 2b 29 7b 20 20 2f 2a 20 73 65 72 69 61 ec++){ /* seria
1d4fe 6c 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 75 2e l data */. u.
1d4ff 61 6f 2e 69 20 2b 3d 20 73 71 6c 69 74 65 33 56 ao.i += sqlite3V
1d500 64 62 65 53 65 72 69 61 6c 50 75 74 28 26 75 2e dbeSerialPut(&u.
1d501 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e ao.zNewRecord[u.
1d502 61 6f 2e 69 5d 2c 20 28 69 6e 74 29 28 75 2e 61 ao.i], (int)(u.a
1d503 6f 2e 6e 42 79 74 65 2d 75 2e 61 6f 2e 69 29 2c o.nByte-u.ao.i),
1d504 20 75 2e 61 6f 2e 70 52 65 63 2c 75 2e 61 6f 2e u.ao.pRec,u.ao.
1d505 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 file_format);.
1d506 7d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6f }. assert( u.ao
1d507 2e 69 3d 3d 75 2e 61 6f 2e 6e 42 79 74 65 20 29 .i==u.ao.nByte )
1d508 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 ;.. assert( pOp
1d509 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p3>0 && pOp->p
1d50a 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 3<=p->nMem );.
1d50b 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 75 pOut->n = (int)u
1d50c 2e 61 6f 2e 6e 42 79 74 65 3b 0a 20 20 70 4f 75 .ao.nByte;. pOu
1d50d 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 t->flags = MEM_B
1d50e 6c 6f 62 20 7c 20 4d 45 4d 5f 44 79 6e 3b 0a 20 lob | MEM_Dyn;.
1d50f 20 70 4f 75 74 2d 3e 78 44 65 6c 20 3d 20 30 3b pOut->xDel = 0;
1d510 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 5a 65 72 . if( u.ao.nZer
1d511 6f 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 o ){. pOut->u
1d512 2e 6e 5a 65 72 6f 20 3d 20 75 2e 61 6f 2e 6e 5a .nZero = u.ao.nZ
1d513 65 72 6f 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 ero;. pOut->f
1d514 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 5a 65 72 6f lags |= MEM_Zero
1d515 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e ;. }. pOut->en
1d516 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b c = SQLITE_UTF8;
1d517 20 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 /* In case the
1d518 20 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63 6f blob is ever co
1d519 6e 76 65 72 74 65 64 20 74 6f 20 74 65 78 74 20 nverted to text
1d51a 2a 2f 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 */. REGISTER_TR
1d51b 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 ACE(pOp->p3, pOu
1d51c 74 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 t);. UPDATE_MAX
1d51d 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b _BLOBSIZE(pOut);
1d51e 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1d51f 4f 70 63 6f 64 65 3a 20 43 6f 75 6e 74 20 50 31 Opcode: Count P1
1d520 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1d521 53 74 6f 72 65 20 74 68 65 20 6e 75 6d 62 65 72 Store the number
1d522 20 6f 66 20 65 6e 74 72 69 65 73 20 28 61 6e 20 of entries (an
1d523 69 6e 74 65 67 65 72 20 76 61 6c 75 65 29 20 69 integer value) i
1d524 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 n the table or i
1d525 6e 64 65 78 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 ndex .** opened
1d526 62 79 20 63 75 72 73 6f 72 20 50 31 20 69 6e 20 by cursor P1 in
1d527 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2f 0a 23 register P2.*/.#
1d528 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1d529 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 63 61 IT_BTREECOUNT.ca
1d52a 73 65 20 4f 50 5f 43 6f 75 6e 74 3a 20 7b 20 20 se OP_Count: {
1d52b 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 /* out2-p
1d52c 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 rerelease */.#if
1d52d 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1d52e 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1d52f 6f 20 75 2e 61 70 20 2a 2f 0a 20 20 69 36 34 20 o u.ap */. i64
1d530 6e 45 6e 74 72 79 3b 0a 20 20 42 74 43 75 72 73 nEntry;. BtCurs
1d531 6f 72 20 2a 70 43 72 73 72 3b 0a 23 65 6e 64 69 or *pCrsr;.#endi
1d532 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1d533 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1d534 75 2e 61 70 20 2a 2f 0a 0a 20 20 75 2e 61 70 2e u.ap */.. u.ap.
1d535 70 43 72 73 72 20 3d 20 70 2d 3e 61 70 43 73 72 pCrsr = p->apCsr
1d536 5b 70 4f 70 2d 3e 70 31 5d 2d 3e 70 43 75 72 73 [pOp->p1]->pCurs
1d537 6f 72 3b 0a 20 20 69 66 28 20 75 2e 61 70 2e 70 or;. if( u.ap.p
1d538 43 72 73 72 20 29 7b 0a 20 20 20 20 72 63 20 3d Crsr ){. rc =
1d539 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 75 sqlite3BtreeCou
1d53a 6e 74 28 75 2e 61 70 2e 70 43 72 73 72 2c 20 26 nt(u.ap.pCrsr, &
1d53b 75 2e 61 70 2e 6e 45 6e 74 72 79 29 3b 0a 20 20 u.ap.nEntry);.
1d53c 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 70 2e }else{. u.ap.
1d53d 6e 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 7d 0a nEntry = 0;. }.
1d53e 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 pOut->flags =
1d53f 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d MEM_Int;. pOut-
1d540 3e 75 2e 69 20 3d 20 75 2e 61 70 2e 6e 45 6e 74 >u.i = u.ap.nEnt
1d541 72 79 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 ry;. break;.}.#
1d542 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65 endif../* Opcode
1d543 3a 20 53 61 76 65 70 6f 69 6e 74 20 50 31 20 2a : Savepoint P1 *
1d544 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 * P4 *.**.** Op
1d545 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72 en, release or r
1d546 6f 6c 6c 62 61 63 6b 20 74 68 65 20 73 61 76 65 ollback the save
1d547 70 6f 69 6e 74 20 6e 61 6d 65 64 20 62 79 20 70 point named by p
1d548 61 72 61 6d 65 74 65 72 20 50 34 2c 20 64 65 70 arameter P4, dep
1d549 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 ending.** on the
1d54a 20 76 61 6c 75 65 20 6f 66 20 50 31 2e 20 54 6f value of P1. To
1d54b 20 6f 70 65 6e 20 61 20 6e 65 77 20 73 61 76 65 open a new save
1d54c 70 6f 69 6e 74 2c 20 50 31 3d 3d 30 2e 20 54 6f point, P1==0. To
1d54d 20 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 release (commit
1d54e 29 20 61 6e 0a 2a 2a 20 65 78 69 73 74 69 6e 67 ) an.** existing
1d54f 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d savepoint, P1==
1d550 31 2c 20 6f 72 20 74 6f 20 72 6f 6c 6c 62 61 63 1, or to rollbac
1d551 6b 20 61 6e 20 65 78 69 73 74 69 6e 67 20 73 61 k an existing sa
1d552 76 65 70 6f 69 6e 74 20 50 31 3d 3d 32 2e 0a 2a vepoint P1==2..*
1d553 2f 0a 63 61 73 65 20 4f 50 5f 53 61 76 65 70 6f /.case OP_Savepo
1d554 69 6e 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a int: {.#if 0 /*
1d555 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1d556 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 71 moved into u.aq
1d557 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 */. int p1;
1d558 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d559 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f /* Value o
1d55a 66 20 50 31 20 6f 70 65 72 61 6e 64 20 2a 2f 0a f P1 operand */.
1d55b 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
1d55c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d55d 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 61 76 /* Name of sav
1d55e 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 epoint */. int
1d55f 6e 4e 61 6d 65 3b 0a 20 20 53 61 76 65 70 6f 69 nName;. Savepoi
1d560 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 53 61 76 65 nt *pNew;. Save
1d561 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e point *pSavepoin
1d562 74 3b 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a t;. Savepoint *
1d563 70 54 6d 70 3b 0a 20 20 69 6e 74 20 69 53 61 76 pTmp;. int iSav
1d564 65 70 6f 69 6e 74 3b 0a 20 20 69 6e 74 20 69 69 epoint;. int ii
1d565 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1d566 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1d567 64 20 69 6e 74 6f 20 75 2e 61 71 20 2a 2f 0a 0a d into u.aq */..
1d568 20 20 75 2e 61 71 2e 70 31 20 3d 20 70 4f 70 2d u.aq.p1 = pOp-
1d569 3e 70 31 3b 0a 20 20 75 2e 61 71 2e 7a 4e 61 6d >p1;. u.aq.zNam
1d56a 65 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 0a e = pOp->p4.z;..
1d56b 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 /* Assert that
1d56c 20 74 68 65 20 75 2e 61 71 2e 70 31 20 70 61 72 the u.aq.p1 par
1d56d 61 6d 65 74 65 72 20 69 73 20 76 61 6c 69 64 2e ameter is valid.
1d56e 20 41 6c 73 6f 20 74 68 61 74 20 69 66 20 74 68 Also that if th
1d56f 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 0a 20 ere is no open.
1d570 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c ** transaction,
1d571 20 74 68 65 6e 20 74 68 65 72 65 20 63 61 6e 6e then there cann
1d572 6f 74 20 62 65 20 61 6e 79 20 73 61 76 65 70 6f ot be any savepo
1d573 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 ints.. */. ass
1d574 65 72 74 28 20 64 62 2d 3e 70 53 61 76 65 70 6f ert( db->pSavepo
1d575 69 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 75 int==0 || db->au
1d576 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b 0a 20 toCommit==0 );.
1d577 20 61 73 73 65 72 74 28 20 75 2e 61 71 2e 70 31 assert( u.aq.p1
1d578 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 ==SAVEPOINT_BEGI
1d579 4e 7c 7c 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 N||u.aq.p1==SAVE
1d57a 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 7c 7c 75 POINT_RELEASE||u
1d57b 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e .aq.p1==SAVEPOIN
1d57c 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 T_ROLLBACK );.
1d57d 61 73 73 65 72 74 28 20 64 62 2d 3e 70 53 61 76 assert( db->pSav
1d57e 65 70 6f 69 6e 74 20 7c 7c 20 64 62 2d 3e 69 73 epoint || db->is
1d57f 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 TransactionSavep
1d580 6f 69 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 oint==0 );. ass
1d581 65 72 74 28 20 63 68 65 63 6b 53 61 76 65 70 6f ert( checkSavepo
1d582 69 6e 74 43 6f 75 6e 74 28 64 62 29 20 29 3b 0a intCount(db) );.
1d583 0a 20 20 69 66 28 20 75 2e 61 71 2e 70 31 3d 3d . if( u.aq.p1==
1d584 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 20 SAVEPOINT_BEGIN
1d585 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 77 ){. if( db->w
1d586 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20 29 7b riteVdbeCnt>0 ){
1d587 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e 65 77 20 . /* A new
1d588 73 61 76 65 70 6f 69 6e 74 20 63 61 6e 6e 6f 74 savepoint cannot
1d589 20 62 65 20 63 72 65 61 74 65 64 20 69 66 20 74 be created if t
1d58a 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65 20 here are active
1d58b 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 73 write. ** s
1d58c 74 61 74 65 6d 65 6e 74 73 20 28 69 2e 65 2e 20 tatements (i.e.
1d58d 6f 70 65 6e 20 72 65 61 64 2f 77 72 69 74 65 20 open read/write
1d58e 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 incremental blob
1d58f 20 68 61 6e 64 6c 65 73 29 2e 0a 20 20 20 20 20 handles)..
1d590 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 */. sqlite
1d591 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 3SetString(&p->z
1d592 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e ErrMsg, db, "can
1d593 6e 6f 74 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 not open savepoi
1d594 6e 74 20 2d 20 22 0a 20 20 20 20 20 20 20 20 22 nt - ". "
1d595 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 SQL statements i
1d596 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20 20 n progress");.
1d597 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1d598 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b BUSY;. }else{
1d599 0a 20 20 20 20 20 20 75 2e 61 71 2e 6e 4e 61 6d . u.aq.nNam
1d59a 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 e = sqlite3Strle
1d59b 6e 33 30 28 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b n30(u.aq.zName);
1d59c 0a 0a 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74 .. /* Creat
1d59d 65 20 61 20 6e 65 77 20 73 61 76 65 70 6f 69 6e e a new savepoin
1d59e 74 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a t structure. */.
1d59f 20 20 20 20 20 20 75 2e 61 71 2e 70 4e 65 77 20 u.aq.pNew
1d5a0 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
1d5a1 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 cRaw(db, sizeof(
1d5a2 53 61 76 65 70 6f 69 6e 74 29 2b 75 2e 61 71 2e Savepoint)+u.aq.
1d5a3 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 20 20 nName+1);.
1d5a4 69 66 28 20 75 2e 61 71 2e 70 4e 65 77 20 29 7b if( u.aq.pNew ){
1d5a5 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e 70 4e . u.aq.pN
1d5a6 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 ew->zName = (cha
1d5a7 72 20 2a 29 26 75 2e 61 71 2e 70 4e 65 77 5b 31 r *)&u.aq.pNew[1
1d5a8 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 ];. memcp
1d5a9 79 28 75 2e 61 71 2e 70 4e 65 77 2d 3e 7a 4e 61 y(u.aq.pNew->zNa
1d5aa 6d 65 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65 2c 20 me, u.aq.zName,
1d5ab 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29 3b 0a 0a u.aq.nName+1);..
1d5ac 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 /* If th
1d5ad 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20 74 ere is no open t
1d5ae 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e ransaction, then
1d5af 20 6d 61 72 6b 20 74 68 69 73 20 61 73 20 61 20 mark this as a
1d5b0 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20 20 20 special.
1d5b1 2a 2a 20 22 74 72 61 6e 73 61 63 74 69 6f 6e 20 ** "transaction
1d5b2 73 61 76 65 70 6f 69 6e 74 22 2e 20 2a 2f 0a 20 savepoint". */.
1d5b3 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61 if( db->a
1d5b4 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 utoCommit ){.
1d5b5 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 db->autoC
1d5b6 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 ommit = 0;.
1d5b7 20 20 20 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 db->isTrans
1d5b8 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 actionSavepoint
1d5b9 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c = 1;. }el
1d5ba 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 se{. db
1d5bb 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b 2b 3b 0a ->nSavepoint++;.
1d5bc 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 }..
1d5bd 20 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e /* Link the n
1d5be 65 77 20 73 61 76 65 70 6f 69 6e 74 20 69 6e 74 ew savepoint int
1d5bf 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 o the database h
1d5c0 61 6e 64 6c 65 27 73 20 6c 69 73 74 2e 20 2a 2f andle's list. */
1d5c1 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e 70 4e . u.aq.pN
1d5c2 65 77 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e ew->pNext = db->
1d5c3 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 pSavepoint;.
1d5c4 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 db->pSavepoi
1d5c5 6e 74 20 3d 20 75 2e 61 71 2e 70 4e 65 77 3b 0a nt = u.aq.pNew;.
1d5c6 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1d5c7 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 71 2e }else{. u.aq.
1d5c8 69 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a iSavepoint = 0;.
1d5c9 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 . /* Find the
1d5ca 20 6e 61 6d 65 64 20 73 61 76 65 70 6f 69 6e 74 named savepoint
1d5cb 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f . If there is no
1d5cc 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74 2c such savepoint,
1d5cd 20 74 68 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20 then an. **
1d5ce 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 an error is retu
1d5cf 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 rned to the user
1d5d0 2e 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 0a 20 . */. for(.
1d5d1 20 20 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70 u.aq.pSavep
1d5d2 6f 69 6e 74 20 3d 20 64 62 2d 3e 70 53 61 76 65 oint = db->pSave
1d5d3 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 75 2e 61 point;. u.a
1d5d4 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 26 26 20 q.pSavepoint &&
1d5d5 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 75 sqlite3StrICmp(u
1d5d6 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e .aq.pSavepoint->
1d5d7 7a 4e 61 6d 65 2c 20 75 2e 61 71 2e 7a 4e 61 6d zName, u.aq.zNam
1d5d8 65 29 3b 0a 20 20 20 20 20 20 75 2e 61 71 2e 70 e);. u.aq.p
1d5d9 53 61 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 71 Savepoint = u.aq
1d5da 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 .pSavepoint->pNe
1d5db 78 74 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 xt. ){.
1d5dc 75 2e 61 71 2e 69 53 61 76 65 70 6f 69 6e 74 2b u.aq.iSavepoint+
1d5dd 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 +;. }. if(
1d5de 20 21 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e !u.aq.pSavepoin
1d5df 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 t ){. sqlit
1d5e0 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1d5e1 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 6e 6f zErrMsg, db, "no
1d5e2 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74 3a such savepoint:
1d5e3 20 25 73 22 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65 %s", u.aq.zName
1d5e4 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 );. rc = SQ
1d5e5 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 LITE_ERROR;.
1d5e6 7d 65 6c 73 65 20 69 66 28 0a 20 20 20 20 20 20 }else if(.
1d5e7 20 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 db->writeVdbeC
1d5e8 6e 74 3e 30 20 7c 7c 20 28 75 2e 61 71 2e 70 31 nt>0 || (u.aq.p1
1d5e9 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c ==SAVEPOINT_ROLL
1d5ea 42 41 43 4b 20 26 26 20 64 62 2d 3e 61 63 74 69 BACK && db->acti
1d5eb 76 65 56 64 62 65 43 6e 74 3e 31 29 0a 20 20 20 veVdbeCnt>1).
1d5ec 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74 20 ){. /* It
1d5ed 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
1d5ee 74 6f 20 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d to release (comm
1d5ef 69 74 29 20 61 20 73 61 76 65 70 6f 69 6e 74 20 it) a savepoint
1d5f0 69 66 20 74 68 65 72 65 20 61 72 65 0a 20 20 20 if there are.
1d5f1 20 20 20 2a 2a 20 61 63 74 69 76 65 20 77 72 69 ** active wri
1d5f2 74 65 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 te statements. I
1d5f3 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
1d5f4 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 e to rollback a
1d5f5 73 61 76 65 70 6f 69 6e 74 0a 20 20 20 20 20 20 savepoint.
1d5f6 2a 2a 20 69 66 20 74 68 65 72 65 20 61 72 65 20 ** if there are
1d5f7 61 6e 79 20 61 63 74 69 76 65 20 73 74 61 74 65 any active state
1d5f8 6d 65 6e 74 73 20 61 74 20 61 6c 6c 2e 0a 20 20 ments at all..
1d5f9 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c */. sql
1d5fa 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 ite3SetString(&p
1d5fb 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 0a 20 ->zErrMsg, db,.
1d5fc 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 25 "cannot %
1d5fd 73 20 73 61 76 65 70 6f 69 6e 74 20 2d 20 53 51 s savepoint - SQ
1d5fe 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 L statements in
1d5ff 70 72 6f 67 72 65 73 73 22 2c 0a 20 20 20 20 20 progress",.
1d600 20 20 20 28 75 2e 61 71 2e 70 31 3d 3d 53 41 56 (u.aq.p1==SAV
1d601 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
1d602 3f 20 22 72 6f 6c 6c 62 61 63 6b 22 3a 20 22 72 ? "rollback": "r
1d603 65 6c 65 61 73 65 22 29 0a 20 20 20 20 20 20 29 elease"). )
1d604 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
1d605 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 ITE_BUSY;. }e
1d606 6c 73 65 7b 0a 0a 20 20 20 20 20 20 2f 2a 20 44 lse{.. /* D
1d607 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 etermine whether
1d608 20 6f 72 20 6e 6f 74 20 74 68 69 73 20 69 73 20 or not this is
1d609 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 a transaction sa
1d60a 76 65 70 6f 69 6e 74 2e 20 49 66 20 73 6f 2c 0a vepoint. If so,.
1d60b 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 69 ** and thi
1d60c 73 20 69 73 20 61 20 52 45 4c 45 41 53 45 20 63 s is a RELEASE c
1d60d 6f 6d 6d 61 6e 64 2c 20 74 68 65 6e 20 74 68 65 ommand, then the
1d60e 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 current transac
1d60f 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 73 tion. ** is
1d610 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 20 20 20 20 committed..
1d611 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 */. int i
1d612 73 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 75 sTransaction = u
1d613 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e .aq.pSavepoint->
1d614 70 4e 65 78 74 3d 3d 30 20 26 26 20 64 62 2d 3e pNext==0 && db->
1d615 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 isTransactionSav
1d616 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 69 66 epoint;. if
1d617 28 20 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 20 ( isTransaction
1d618 26 26 20 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 && u.aq.p1==SAVE
1d619 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b POINT_RELEASE ){
1d61a 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 . db->aut
1d61b 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 oCommit = 1;.
1d61c 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1d61d 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c VdbeHalt(p)==SQL
1d61e 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 ITE_BUSY ){.
1d61f 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 p->pc = pc
1d620 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e ;. db->
1d621 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a autoCommit = 0;.
1d622 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 p->rc
1d623 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 = rc = SQLITE_BU
1d624 53 59 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f SY;. go
1d625 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a to vdbe_return;.
1d626 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1d627 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 db->isTransact
1d628 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 ionSavepoint = 0
1d629 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 ;. rc = p
1d62a 2d 3e 72 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73 ->rc;. }els
1d62b 65 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e e{. u.aq.
1d62c 69 53 61 76 65 70 6f 69 6e 74 20 3d 20 64 62 2d iSavepoint = db-
1d62d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2d 20 75 2e >nSavepoint - u.
1d62e 61 71 2e 69 53 61 76 65 70 6f 69 6e 74 20 2d 20 aq.iSavepoint -
1d62f 31 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 75 1;. for(u
1d630 2e 61 71 2e 69 69 3d 30 3b 20 75 2e 61 71 2e 69 .aq.ii=0; u.aq.i
1d631 69 3c 64 62 2d 3e 6e 44 62 3b 20 75 2e 61 71 2e i<db->nDb; u.aq.
1d632 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 ii++){.
1d633 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1d634 65 65 53 61 76 65 70 6f 69 6e 74 28 64 62 2d 3e eeSavepoint(db->
1d635 61 44 62 5b 75 2e 61 71 2e 69 69 5d 2e 70 42 74 aDb[u.aq.ii].pBt
1d636 2c 20 75 2e 61 71 2e 70 31 2c 20 75 2e 61 71 2e , u.aq.p1, u.aq.
1d637 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 iSavepoint);.
1d638 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
1d639 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1d63a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f goto abo
1d63b 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
1d63c 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1d63d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
1d63e 66 28 20 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 f( u.aq.p1==SAVE
1d63f 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 POINT_ROLLBACK &
1d640 26 20 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c & (db->flags&SQL
1d641 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 ITE_InternChange
1d642 73 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 s)!=0 ){.
1d643 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 sqlite3Expire
1d644 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e PreparedStatemen
1d645 74 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 ts(db);.
1d646 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e sqlite3ResetIn
1d647 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c ternalSchema(db,
1d648 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 0);. }.
1d649 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a }.. /*
1d64a 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 Regardless of w
1d64b 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61 hether this is a
1d64c 20 52 45 4c 45 41 53 45 20 6f 72 20 52 4f 4c 4c RELEASE or ROLL
1d64d 42 41 43 4b 2c 20 64 65 73 74 72 6f 79 20 61 6c BACK, destroy al
1d64e 6c 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 70 l. ** savep
1d64f 6f 69 6e 74 73 20 6e 65 73 74 65 64 20 69 6e 73 oints nested ins
1d650 69 64 65 20 6f 66 20 74 68 65 20 73 61 76 65 70 ide of the savep
1d651 6f 69 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 61 oint being opera
1d652 74 65 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 ted on. */.
1d653 20 77 68 69 6c 65 28 20 64 62 2d 3e 70 53 61 76 while( db->pSav
1d654 65 70 6f 69 6e 74 21 3d 75 2e 61 71 2e 70 53 61 epoint!=u.aq.pSa
1d655 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 vepoint ){.
1d656 20 20 20 75 2e 61 71 2e 70 54 6d 70 20 3d 20 64 u.aq.pTmp = d
1d657 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 b->pSavepoint;.
1d658 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76 65 db->pSave
1d659 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 54 6d point = u.aq.pTm
1d65a 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 p->pNext;.
1d65b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1d65c 64 62 2c 20 75 2e 61 71 2e 70 54 6d 70 29 3b 0a db, u.aq.pTmp);.
1d65d 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 db->nSav
1d65e 65 70 6f 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 epoint--;.
1d65f 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 69 }.. /* If i
1d660 74 20 69 73 20 61 20 52 45 4c 45 41 53 45 2c 20 t is a RELEASE,
1d661 74 68 65 6e 20 64 65 73 74 72 6f 79 20 74 68 65 then destroy the
1d662 20 73 61 76 65 70 6f 69 6e 74 20 62 65 69 6e 67 savepoint being
1d663 20 6f 70 65 72 61 74 65 64 20 6f 6e 20 74 6f 6f operated on too
1d664 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 75 2e */. if( u.
1d665 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 aq.p1==SAVEPOINT
1d666 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 _RELEASE ){.
1d667 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 71 assert( u.aq
1d668 2e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 64 62 2d .pSavepoint==db-
1d669 3e 70 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 >pSavepoint );.
1d66a 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76 65 db->pSave
1d66b 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 53 61 point = u.aq.pSa
1d66c 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3b 0a vepoint->pNext;.
1d66d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
1d66e 62 46 72 65 65 28 64 62 2c 20 75 2e 61 71 2e 70 bFree(db, u.aq.p
1d66f 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 Savepoint);.
1d670 20 20 20 20 69 66 28 20 21 69 73 54 72 61 6e 73 if( !isTrans
1d671 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 action ){.
1d672 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 db->nSavepoi
1d673 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a nt--;. }.
1d674 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1d675 7d 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }.. break;.}../
1d676 2a 20 4f 70 63 6f 64 65 3a 20 41 75 74 6f 43 6f * Opcode: AutoCo
1d677 6d 6d 69 74 20 50 31 20 50 32 20 2a 20 2a 20 2a mmit P1 P2 * * *
1d678 0a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 64 .**.** Set the d
1d679 61 74 61 62 61 73 65 20 61 75 74 6f 2d 63 6f 6d atabase auto-com
1d67a 6d 69 74 20 66 6c 61 67 20 74 6f 20 50 31 20 28 mit flag to P1 (
1d67b 31 20 6f 72 20 30 29 2e 20 49 66 20 50 32 20 69 1 or 0). If P2 i
1d67c 73 20 74 72 75 65 2c 20 72 6f 6c 6c 0a 2a 2a 20 s true, roll.**
1d67d 62 61 63 6b 20 61 6e 79 20 63 75 72 72 65 6e 74 back any current
1d67e 6c 79 20 61 63 74 69 76 65 20 62 74 72 65 65 20 ly active btree
1d67f 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 20 49 66 transactions. If
1d680 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 61 there are any a
1d681 63 74 69 76 65 0a 2a 2a 20 56 4d 73 20 28 61 70 ctive.** VMs (ap
1d682 61 72 74 20 66 72 6f 6d 20 74 68 69 73 20 6f 6e art from this on
1d683 65 29 2c 20 74 68 65 6e 20 61 20 52 4f 4c 4c 42 e), then a ROLLB
1d684 41 43 4b 20 66 61 69 6c 73 2e 20 20 41 20 43 4f ACK fails. A CO
1d685 4d 4d 49 54 20 66 61 69 6c 73 20 69 66 0a 2a 2a MMIT fails if.**
1d686 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76 there are activ
1d687 65 20 77 72 69 74 69 6e 67 20 56 4d 73 20 6f 72 e writing VMs or
1d688 20 61 63 74 69 76 65 20 56 4d 73 20 74 68 61 74 active VMs that
1d689 20 75 73 65 20 73 68 61 72 65 64 20 63 61 63 68 use shared cach
1d68a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e e..**.** This in
1d68b 73 74 72 75 63 74 69 6f 6e 20 63 61 75 73 65 73 struction causes
1d68c 20 74 68 65 20 56 4d 20 74 6f 20 68 61 6c 74 2e the VM to halt.
1d68d 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 75 74 6f .*/.case OP_Auto
1d68e 43 6f 6d 6d 69 74 3a 20 7b 0a 23 69 66 20 30 20 Commit: {.#if 0
1d68f 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1d690 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1d691 2e 61 72 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 .ar */. int des
1d692 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a iredAutoCommit;.
1d693 20 20 69 6e 74 20 69 52 6f 6c 6c 62 61 63 6b 3b int iRollback;
1d694 0a 20 20 69 6e 74 20 74 75 72 6e 4f 6e 41 43 3b . int turnOnAC;
1d695 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1d696 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1d697 20 69 6e 74 6f 20 75 2e 61 72 20 2a 2f 0a 0a 20 into u.ar */..
1d698 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 u.ar.desiredAut
1d699 6f 43 6f 6d 6d 69 74 20 3d 20 70 4f 70 2d 3e 70 oCommit = pOp->p
1d69a 31 3b 0a 20 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 1;. u.ar.iRollb
1d69b 61 63 6b 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 ack = pOp->p2;.
1d69c 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43 20 3d u.ar.turnOnAC =
1d69d 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 u.ar.desiredAut
1d69e 6f 43 6f 6d 6d 69 74 20 26 26 20 21 64 62 2d 3e oCommit && !db->
1d69f 61 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 61 73 autoCommit;. as
1d6a0 73 65 72 74 28 20 75 2e 61 72 2e 64 65 73 69 72 sert( u.ar.desir
1d6a1 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 edAutoCommit==1
1d6a2 7c 7c 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 || u.ar.desiredA
1d6a3 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b 0a utoCommit==0 );.
1d6a4 20 20 61 73 73 65 72 74 28 20 75 2e 61 72 2e 64 assert( u.ar.d
1d6a5 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 esiredAutoCommit
1d6a6 3d 3d 31 20 7c 7c 20 75 2e 61 72 2e 69 52 6f 6c ==1 || u.ar.iRol
1d6a7 6c 62 61 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 lback==0 );. as
1d6a8 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 sert( db->active
1d6a9 56 64 62 65 43 6e 74 3e 30 20 29 3b 20 20 2f 2a VdbeCnt>0 ); /*
1d6aa 20 41 74 20 6c 65 61 73 74 20 74 68 69 73 20 6f At least this o
1d6ab 6e 65 20 56 4d 20 69 73 20 61 63 74 69 76 65 20 ne VM is active
1d6ac 2a 2f 0a 0a 20 20 69 66 28 20 75 2e 61 72 2e 74 */.. if( u.ar.t
1d6ad 75 72 6e 4f 6e 41 43 20 26 26 20 75 2e 61 72 2e urnOnAC && u.ar.
1d6ae 69 52 6f 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d iRollback && db-
1d6af 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31 >activeVdbeCnt>1
1d6b0 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ){. /* If th
1d6b1 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 is instruction i
1d6b2 6d 70 6c 65 6d 65 6e 74 73 20 61 20 52 4f 4c 4c mplements a ROLL
1d6b3 42 41 43 4b 20 61 6e 64 20 6f 74 68 65 72 20 56 BACK and other V
1d6b4 4d 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 73 74 Ms are. ** st
1d6b5 69 6c 6c 20 72 75 6e 6e 69 6e 67 2c 20 61 6e 64 ill running, and
1d6b6 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
1d6b7 73 20 61 63 74 69 76 65 2c 20 72 65 74 75 72 6e s active, return
1d6b8 20 61 6e 20 65 72 72 6f 72 20 69 6e 64 69 63 61 an error indica
1d6b9 74 69 6e 67 0a 20 20 20 20 2a 2a 20 74 68 61 74 ting. ** that
1d6ba 20 74 68 65 20 6f 74 68 65 72 20 56 4d 73 20 6d the other VMs m
1d6bb 75 73 74 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 ust complete fir
1d6bc 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 st.. */. s
1d6bd 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1d6be 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1d6bf 20 22 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 "cannot rollbac
1d6c0 6b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 k transaction -
1d6c1 22 0a 20 20 20 20 20 20 20 20 22 53 51 4c 20 73 ". "SQL s
1d6c2 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f tatements in pro
1d6c3 67 72 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20 gress");. rc
1d6c4 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 = SQLITE_BUSY;.
1d6c5 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 72 2e }else if( u.ar.
1d6c6 74 75 72 6e 4f 6e 41 43 20 26 26 20 21 75 2e 61 turnOnAC && !u.a
1d6c7 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 26 26 20 64 r.iRollback && d
1d6c8 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e b->writeVdbeCnt>
1d6c9 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 0 ){. /* If t
1d6ca 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1d6cb 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 43 4f 4d implements a COM
1d6cc 4d 49 54 20 61 6e 64 20 6f 74 68 65 72 20 56 4d MIT and other VM
1d6cd 73 20 61 72 65 20 77 72 69 74 69 6e 67 0a 20 20 s are writing.
1d6ce 20 20 2a 2a 20 72 65 74 75 72 6e 20 61 6e 20 65 ** return an e
1d6cf 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e 67 20 rror indicating
1d6d0 74 68 61 74 20 74 68 65 20 6f 74 68 65 72 20 56 that the other V
1d6d1 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c 65 74 65 Ms must complete
1d6d2 20 66 69 72 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 first.. */.
1d6d3 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 sqlite3SetStr
1d6d4 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
1d6d5 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20 63 6f 6d db, "cannot com
1d6d6 6d 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 mit transaction
1d6d7 2d 20 22 0a 20 20 20 20 20 20 20 20 22 53 51 4c - ". "SQL
1d6d8 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 statements in p
1d6d9 72 6f 67 72 65 73 73 22 29 3b 0a 20 20 20 20 72 rogress");. r
1d6da 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
1d6db 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 . }else if( u.a
1d6dc 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d r.desiredAutoCom
1d6dd 6d 69 74 21 3d 64 62 2d 3e 61 75 74 6f 43 6f 6d mit!=db->autoCom
1d6de 6d 69 74 20 29 7b 0a 20 20 20 20 69 66 28 20 75 mit ){. if( u
1d6df 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 29 7b .ar.iRollback ){
1d6e0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 . assert( u
1d6e1 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 .ar.desiredAutoC
1d6e2 6f 6d 6d 69 74 3d 3d 31 20 29 3b 0a 20 20 20 20 ommit==1 );.
1d6e3 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 sqlite3Rollbac
1d6e4 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 kAll(db);.
1d6e5 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d db->autoCommit =
1d6e6 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 1;. }else{.
1d6e7 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d db->autoCom
1d6e8 6d 69 74 20 3d 20 28 75 38 29 75 2e 61 72 2e 64 mit = (u8)u.ar.d
1d6e9 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 esiredAutoCommit
1d6ea 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 ;. if( sqli
1d6eb 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3d 3d te3VdbeHalt(p)==
1d6ec 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 SQLITE_BUSY ){.
1d6ed 20 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 p->pc = p
1d6ee 63 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 c;. db->a
1d6ef 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 utoCommit = (u8)
1d6f0 28 31 2d 75 2e 61 72 2e 64 65 73 69 72 65 64 41 (1-u.ar.desiredA
1d6f1 75 74 6f 43 6f 6d 6d 69 74 29 3b 0a 20 20 20 20 utoCommit);.
1d6f2 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d p->rc = rc =
1d6f3 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
1d6f4 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f goto vdbe_
1d6f5 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a return;. }.
1d6f6 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
1d6f7 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 ( db->nStatement
1d6f8 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ==0 );. sqlit
1d6f9 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 e3CloseSavepoint
1d6fa 73 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 70 s(db);. if( p
1d6fb 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ->rc==SQLITE_OK
1d6fc 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
1d6fd 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d LITE_DONE;. }
1d6fe 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d else{. rc =
1d6ff 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1d700 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 76 64 }. goto vd
1d701 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c be_return;. }el
1d702 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 se{. sqlite3S
1d703 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 etString(&p->zEr
1d704 72 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 rMsg, db,.
1d705 20 20 28 21 75 2e 61 72 2e 64 65 73 69 72 65 64 (!u.ar.desired
1d706 41 75 74 6f 43 6f 6d 6d 69 74 29 3f 22 63 61 6e AutoCommit)?"can
1d707 6e 6f 74 20 73 74 61 72 74 20 61 20 74 72 61 6e not start a tran
1d708 73 61 63 74 69 6f 6e 20 77 69 74 68 69 6e 20 61 saction within a
1d709 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 3a 28 0a transaction":(.
1d70a 20 20 20 20 20 20 20 20 28 75 2e 61 72 2e 69 52 (u.ar.iR
1d70b 6f 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e 6f 74 ollback)?"cannot
1d70c 20 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20 74 rollback - no t
1d70d 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 ransaction is ac
1d70e 74 69 76 65 22 3a 0a 20 20 20 20 20 20 20 20 20 tive":.
1d70f 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f "canno
1d710 74 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74 72 t commit - no tr
1d711 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 ansaction is act
1d712 69 76 65 22 29 29 3b 0a 0a 20 20 20 20 72 63 20 ive"));.. rc
1d713 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
1d714 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1d715 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 6e 73 /* Opcode: Trans
1d716 61 63 74 69 6f 6e 20 50 31 20 50 32 20 2a 20 2a action P1 P2 * *
1d717 20 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 *.**.** Begin a
1d718 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 transaction. T
1d719 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 65 he transaction e
1d71a 6e 64 73 20 77 68 65 6e 20 61 20 43 6f 6d 6d 69 nds when a Commi
1d71b 74 20 6f 72 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a t or Rollback.**
1d71c 20 6f 70 63 6f 64 65 20 69 73 20 65 6e 63 6f 75 opcode is encou
1d71d 6e 74 65 72 65 64 2e 20 20 44 65 70 65 6e 64 69 ntered. Dependi
1d71e 6e 67 20 6f 6e 20 74 68 65 20 4f 4e 20 43 4f 4e ng on the ON CON
1d71f 46 4c 49 43 54 20 73 65 74 74 69 6e 67 2c 20 74 FLICT setting, t
1d720 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f he.** transactio
1d721 6e 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 n might also be
1d722 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 20 61 rolled back if a
1d723 6e 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 n error is encou
1d724 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 ntered..**.** P1
1d725 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 is the index of
1d726 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1d727 6c 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 le on which the
1d728 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a transaction is.*
1d729 2a 20 73 74 61 72 74 65 64 2e 20 20 49 6e 64 65 * started. Inde
1d72a 78 20 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20 x 0 is the main
1d72b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e database file an
1d72c 64 20 69 6e 64 65 78 20 31 20 69 73 20 74 68 65 d index 1 is the
1d72d 0a 2a 2a 20 66 69 6c 65 20 75 73 65 64 20 66 6f .** file used fo
1d72e 72 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c r temporary tabl
1d72f 65 73 2e 20 20 49 6e 64 69 63 65 73 20 6f 66 20 es. Indices of
1d730 32 20 6f 72 20 6d 6f 72 65 20 61 72 65 20 75 73 2 or more are us
1d731 65 64 20 66 6f 72 0a 2a 2a 20 61 74 74 61 63 68 ed for.** attach
1d732 65 64 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a ed databases..**
1d733 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 6e 6f 6e .** If P2 is non
1d734 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 77 72 -zero, then a wr
1d735 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite-transaction
1d736 69 73 20 73 74 61 72 74 65 64 2e 20 20 41 20 52 is started. A R
1d737 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 0a ESERVED lock is.
1d738 2a 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 ** obtained on t
1d739 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1d73a 20 77 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 when a write-tr
1d73b 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 ansaction is sta
1d73c 72 74 65 64 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68 rted. No.** oth
1d73d 65 72 20 70 72 6f 63 65 73 73 20 63 61 6e 20 73 er process can s
1d73e 74 61 72 74 20 61 6e 6f 74 68 65 72 20 77 72 69 tart another wri
1d73f 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 te transaction w
1d740 68 69 6c 65 20 74 68 69 73 20 74 72 61 6e 73 61 hile this transa
1d741 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 6e 64 65 ction is.** unde
1d742 72 77 61 79 2e 20 20 53 74 61 72 74 69 6e 67 20 rway. Starting
1d743 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 a write transact
1d744 69 6f 6e 20 61 6c 73 6f 20 63 72 65 61 74 65 73 ion also creates
1d745 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 a rollback jour
1d746 6e 61 6c 2e 20 41 0a 2a 2a 20 77 72 69 74 65 20 nal. A.** write
1d747 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 transaction must
1d748 20 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f be started befo
1d749 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 63 re any changes c
1d74a 61 6e 20 62 65 20 6d 61 64 65 20 74 6f 20 74 68 an be made to th
1d74b 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 e.** database.
1d74c 49 66 20 50 32 20 69 73 20 32 20 6f 72 20 67 72 If P2 is 2 or gr
1d74d 65 61 74 65 72 20 74 68 65 6e 20 61 6e 20 45 58 eater then an EX
1d74e 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 CLUSIVE lock is
1d74f 61 6c 73 6f 20 6f 62 74 61 69 6e 65 64 0a 2a 2a also obtained.**
1d750 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a on the file..**
1d751 0a 2a 2a 20 49 66 20 61 20 77 72 69 74 65 2d 74 .** If a write-t
1d752 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 ransaction is st
1d753 61 72 74 65 64 20 61 6e 64 20 74 68 65 20 56 64 arted and the Vd
1d754 62 65 2e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e be.usesStmtJourn
1d755 61 6c 20 66 6c 61 67 20 69 73 0a 2a 2a 20 74 72 al flag is.** tr
1d756 75 65 20 28 74 68 69 73 20 66 6c 61 67 20 69 73 ue (this flag is
1d757 20 73 65 74 20 69 66 20 74 68 65 20 56 64 62 65 set if the Vdbe
1d758 20 6d 61 79 20 6d 6f 64 69 66 79 20 6d 6f 72 65 may modify more
1d759 20 74 68 61 6e 20 6f 6e 65 20 72 6f 77 20 61 6e than one row an
1d75a 64 20 6d 61 79 0a 2a 2a 20 74 68 72 6f 77 20 61 d may.** throw a
1d75b 6e 20 41 42 4f 52 54 20 65 78 63 65 70 74 69 6f n ABORT exceptio
1d75c 6e 29 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 n), a statement
1d75d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 transaction may
1d75e 61 6c 73 6f 20 62 65 20 6f 70 65 6e 65 64 2e 0a also be opened..
1d75f 2a 2a 20 4d 6f 72 65 20 73 70 65 63 69 66 69 63 ** More specific
1d760 61 6c 6c 79 2c 20 61 20 73 74 61 74 65 6d 65 6e ally, a statemen
1d761 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 t transaction is
1d762 20 6f 70 65 6e 65 64 20 69 66 66 20 74 68 65 20 opened iff the
1d763 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e database.** conn
1d764 65 63 74 69 6f 6e 20 69 73 20 63 75 72 72 65 6e ection is curren
1d765 74 6c 79 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63 tly not in autoc
1d766 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20 6f 72 20 69 ommit mode, or i
1d767 66 20 74 68 65 72 65 20 61 72 65 20 6f 74 68 65 f there are othe
1d768 72 0a 2a 2a 20 61 63 74 69 76 65 20 73 74 61 74 r.** active stat
1d769 65 6d 65 6e 74 73 2e 20 41 20 73 74 61 74 65 6d ements. A statem
1d76a 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
1d76b 61 6c 6c 6f 77 73 20 74 68 65 20 61 66 66 65 63 allows the affec
1d76c 74 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 56 44 ts of this.** VD
1d76d 42 45 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 BE to be rolled
1d76e 62 61 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72 back after an er
1d76f 72 6f 72 20 77 69 74 68 6f 75 74 20 68 61 76 69 ror without havi
1d770 6e 67 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 ng to roll back
1d771 74 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20 74 72 the.** entire tr
1d772 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 6e 6f ansaction. If no
1d773 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e error is encoun
1d774 74 65 72 65 64 2c 20 74 68 65 20 73 74 61 74 65 tered, the state
1d775 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1d776 0a 2a 2a 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 .** will automat
1d777 69 63 61 6c 6c 79 20 63 6f 6d 6d 69 74 20 77 68 ically commit wh
1d778 65 6e 20 74 68 65 20 56 44 42 45 20 68 61 6c 74 en the VDBE halt
1d779 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69 s..**.** If P2 i
1d77a 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 72 s zero, then a r
1d77b 65 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 ead-lock is obta
1d77c 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 ined on the data
1d77d 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 63 61 base file..*/.ca
1d77e 73 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f se OP_Transactio
1d77f 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c n: {.#if 0 /* l
1d780 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1d781 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 73 20 2a oved into u.as *
1d782 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a /. Btree *pBt;.
1d783 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1d784 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1d785 69 6e 74 6f 20 75 2e 61 73 20 2a 2f 0a 0a 20 20 into u.as */..
1d786 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1d787 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 =0 && pOp->p1<db
1d788 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 ->nDb );. asser
1d789 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b t( (p->btreeMask
1d78a 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 & (1<<pOp->p1))
1d78b 21 3d 30 20 29 3b 0a 20 20 75 2e 61 73 2e 70 42 !=0 );. u.as.pB
1d78c 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d t = db->aDb[pOp-
1d78d 3e 70 31 5d 2e 70 42 74 3b 0a 0a 20 20 69 66 28 >p1].pBt;.. if(
1d78e 20 75 2e 61 73 2e 70 42 74 20 29 7b 0a 20 20 20 u.as.pBt ){.
1d78f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1d790 65 65 42 65 67 69 6e 54 72 61 6e 73 28 75 2e 61 eeBeginTrans(u.a
1d791 73 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 29 3b s.pBt, pOp->p2);
1d792 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
1d793 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 ITE_BUSY ){.
1d794 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 p->pc = pc;.
1d795 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d p->rc = rc =
1d796 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
1d797 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 goto vdbe_re
1d798 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 turn;. }.
1d799 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1d79a 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f K && rc!=SQLITE_
1d79b 52 45 41 44 4f 4e 4c 59 20 2f 2a 20 26 26 20 72 READONLY /* && r
1d79c 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 2a c!=SQLITE_BUSY *
1d79d 2f 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 / ){. goto
1d79e 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 abort_due_to_err
1d79f 6f 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 or;. }.. i
1d7a0 66 28 20 70 4f 70 2d 3e 70 32 20 26 26 20 70 2d f( pOp->p2 && p-
1d7a1 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c >usesStmtJournal
1d7a2 0a 20 20 20 20 20 26 26 20 28 64 62 2d 3e 61 75 . && (db->au
1d7a3 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 toCommit==0 || d
1d7a4 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 b->activeVdbeCnt
1d7a5 3e 31 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 >1). ){.
1d7a6 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1d7a7 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 75 BtreeIsInTrans(u
1d7a8 2e 61 73 2e 70 42 74 29 20 29 3b 0a 20 20 20 20 .as.pBt) );.
1d7a9 20 20 69 66 28 20 70 2d 3e 69 53 74 61 74 65 6d if( p->iStatem
1d7aa 65 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ent==0 ){.
1d7ab 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 53 assert( db->nS
1d7ac 74 61 74 65 6d 65 6e 74 3e 3d 30 20 26 26 20 64 tatement>=0 && d
1d7ad 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3e 3d 30 b->nSavepoint>=0
1d7ae 20 29 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e );. db->
1d7af 6e 53 74 61 74 65 6d 65 6e 74 2b 2b 3b 0a 20 20 nStatement++;.
1d7b0 20 20 20 20 20 20 70 2d 3e 69 53 74 61 74 65 6d p->iStatem
1d7b1 65 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70 ent = db->nSavep
1d7b2 6f 69 6e 74 20 2b 20 64 62 2d 3e 6e 53 74 61 74 oint + db->nStat
1d7b3 65 6d 65 6e 74 3b 0a 20 20 20 20 20 20 7d 0a 20 ement;. }.
1d7b4 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1d7b5 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 3BtreeBeginStmt(
1d7b6 75 2e 61 73 2e 70 42 74 2c 20 70 2d 3e 69 53 74 u.as.pBt, p->iSt
1d7b7 61 74 65 6d 65 6e 74 29 3b 0a 20 20 20 20 7d 0a atement);. }.
1d7b8 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1d7b9 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 64 43 /* Opcode: ReadC
1d7ba 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a ookie P1 P2 P3 *
1d7bb 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63 6f *.**.** Read co
1d7bc 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 33 20 66 okie number P3 f
1d7bd 72 6f 6d 20 64 61 74 61 62 61 73 65 20 50 31 20 rom database P1
1d7be 61 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 and write it int
1d7bf 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a o register P2..*
1d7c0 2a 20 50 33 3d 3d 31 20 69 73 20 74 68 65 20 73 * P3==1 is the s
1d7c1 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 chema version.
1d7c2 50 33 3d 3d 32 20 69 73 20 74 68 65 20 64 61 74 P3==2 is the dat
1d7c3 61 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a abase format..**
1d7c4 20 50 33 3d 3d 33 20 69 73 20 74 68 65 20 72 65 P3==3 is the re
1d7c5 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 commended pager
1d7c6 63 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20 cache size, and
1d7c7 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 so forth. P1==0
1d7c8 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 is.** the main
1d7c9 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e database file an
1d7ca 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64 d P1==1 is the d
1d7cb 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 atabase file use
1d7cc 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 d to store.** te
1d7cd 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a mporary tables..
1d7ce 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 **.** There must
1d7cf 20 62 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 be a read-lock
1d7d0 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
1d7d1 28 65 69 74 68 65 72 20 61 20 74 72 61 6e 73 61 (either a transa
1d7d2 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 ction.** must be
1d7d3 20 73 74 61 72 74 65 64 20 6f 72 20 74 68 65 72 started or ther
1d7d4 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 e must be an ope
1d7d5 6e 20 63 75 72 73 6f 72 29 20 62 65 66 6f 72 65 n cursor) before
1d7d6 0a 2a 2a 20 65 78 65 63 75 74 69 6e 67 20 74 68 .** executing th
1d7d7 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a is instruction..
1d7d8 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 64 43 */.case OP_ReadC
1d7d9 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 20 ookie: {
1d7da 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 /* out2-p
1d7db 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 rerelease */.#if
1d7dc 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1d7dd 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1d7de 6f 20 75 2e 61 74 20 2a 2f 0a 20 20 69 6e 74 20 o u.at */. int
1d7df 69 4d 65 74 61 3b 0a 20 20 69 6e 74 20 69 44 62 iMeta;. int iDb
1d7e0 3b 0a 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b ;. int iCookie;
1d7e1 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1d7e2 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1d7e3 20 69 6e 74 6f 20 75 2e 61 74 20 2a 2f 0a 0a 20 into u.at */..
1d7e4 20 75 2e 61 74 2e 69 44 62 20 3d 20 70 4f 70 2d u.at.iDb = pOp-
1d7e5 3e 70 31 3b 0a 20 20 75 2e 61 74 2e 69 43 6f 6f >p1;. u.at.iCoo
1d7e6 6b 69 65 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 kie = pOp->p3;.
1d7e7 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 assert( pOp->p3
1d7e8 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f <SQLITE_N_BTREE_
1d7e9 4d 45 54 41 20 29 3b 0a 20 20 61 73 73 65 72 74 META );. assert
1d7ea 28 20 75 2e 61 74 2e 69 44 62 3e 3d 30 20 26 26 ( u.at.iDb>=0 &&
1d7eb 20 75 2e 61 74 2e 69 44 62 3c 64 62 2d 3e 6e 44 u.at.iDb<db->nD
1d7ec 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 b );. assert( d
1d7ed 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62 5d b->aDb[u.at.iDb]
1d7ee 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 .pBt!=0 );. ass
1d7ef 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 ert( (p->btreeMa
1d7f0 73 6b 20 26 20 28 31 3c 3c 75 2e 61 74 2e 69 44 sk & (1<<u.at.iD
1d7f1 62 29 29 21 3d 30 20 29 3b 0a 0a 20 20 73 71 6c b))!=0 );.. sql
1d7f2 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 ite3BtreeGetMeta
1d7f3 28 64 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 44 (db->aDb[u.at.iD
1d7f4 62 5d 2e 70 42 74 2c 20 75 2e 61 74 2e 69 43 6f b].pBt, u.at.iCo
1d7f5 6f 6b 69 65 2c 20 28 75 33 32 20 2a 29 26 75 2e okie, (u32 *)&u.
1d7f6 61 74 2e 69 4d 65 74 61 29 3b 0a 20 20 70 4f 75 at.iMeta);. pOu
1d7f7 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 74 2e 69 4d t->u.i = u.at.iM
1d7f8 65 74 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 eta;. MemSetTyp
1d7f9 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f eFlag(pOut, MEM_
1d7fa 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Int);. break;.}
1d7fb 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74 ../* Opcode: Set
1d7fc 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 Cookie P1 P2 P3
1d7fd 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 * *.**.** Write
1d7fe 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 the content of r
1d7ff 65 67 69 73 74 65 72 20 50 33 20 28 69 6e 74 65 egister P3 (inte
1d800 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 69 6e rpreted as an in
1d801 74 65 67 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63 teger).** into c
1d802 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 32 20 ookie number P2
1d803 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 of database P1.
1d804 20 50 32 3d 3d 31 20 69 73 20 74 68 65 20 73 63 P2==1 is the sc
1d805 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 0a hema version. .
1d806 2a 2a 20 50 32 3d 3d 32 20 69 73 20 74 68 65 20 ** P2==2 is the
1d807 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e database format.
1d808 20 50 32 3d 3d 33 20 69 73 20 74 68 65 20 72 65 P2==3 is the re
1d809 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 commended pager
1d80a 63 61 63 68 65 20 0a 2a 2a 20 73 69 7a 65 2c 20 cache .** size,
1d80b 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 and so forth. P
1d80c 31 3d 3d 30 20 69 73 20 74 68 65 20 6d 61 69 6e 1==0 is the main
1d80d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
1d80e 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 nd P1==1 is the
1d80f 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c .** database fil
1d810 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 e used to store
1d811 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 temporary tables
1d812 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 ..**.** A transa
1d813 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 ction must be st
1d814 61 72 74 65 64 20 62 65 66 6f 72 65 20 65 78 65 arted before exe
1d815 63 75 74 69 6e 67 20 74 68 69 73 20 6f 70 63 6f cuting this opco
1d816 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 de..*/.case OP_S
1d817 65 74 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 etCookie: {
1d818 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 23 69 66 20 /* in3 */.#if
1d819 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1d81a 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1d81b 20 75 2e 61 75 20 2a 2f 0a 20 20 44 62 20 2a 70 u.au */. Db *p
1d81c 44 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f Db;.#endif /* lo
1d81d 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1d81e 76 65 64 20 69 6e 74 6f 20 75 2e 61 75 20 2a 2f ved into u.au */
1d81f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1d820 70 32 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 p2<SQLITE_N_BTRE
1d821 45 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73 73 65 E_META );. asse
1d822 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1d823 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 & pOp->p1<db->nD
1d824 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 b );. assert( (
1d825 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 p->btreeMask & (
1d826 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 1<<pOp->p1))!=0
1d827 29 3b 0a 20 20 75 2e 61 75 2e 70 44 62 20 3d 20 );. u.au.pDb =
1d828 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 &db->aDb[pOp->p1
1d829 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 ];. assert( u.a
1d82a 75 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b u.pDb->pBt!=0 );
1d82b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1d82c 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 33 mIntegerify(pIn3
1d82d 29 3b 0a 20 20 2f 2a 20 53 65 65 20 6e 6f 74 65 );. /* See note
1d82e 20 61 62 6f 75 74 20 69 6e 64 65 78 20 73 68 69 about index shi
1d82f 66 74 69 6e 67 20 6f 6e 20 4f 50 5f 52 65 61 64 fting on OP_Read
1d830 43 6f 6f 6b 69 65 20 2a 2f 0a 20 20 72 63 20 3d Cookie */. rc =
1d831 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 sqlite3BtreeUpd
1d832 61 74 65 4d 65 74 61 28 75 2e 61 75 2e 70 44 62 ateMeta(u.au.pDb
1d833 2d 3e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 2c 20 ->pBt, pOp->p2,
1d834 28 69 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 29 3b (int)pIn3->u.i);
1d835 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d . if( pOp->p2==
1d836 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 BTREE_SCHEMA_VER
1d837 53 49 4f 4e 20 29 7b 0a 20 20 20 20 2f 2a 20 57 SION ){. /* W
1d838 68 65 6e 20 74 68 65 20 73 63 68 65 6d 61 20 63 hen the schema c
1d839 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73 2c 20 72 ookie changes, r
1d83a 65 63 6f 72 64 20 74 68 65 20 6e 65 77 20 63 6f ecord the new co
1d83b 6f 6b 69 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 okie internally
1d83c 2a 2f 0a 20 20 20 20 75 2e 61 75 2e 70 44 62 2d */. u.au.pDb-
1d83d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 >pSchema->schema
1d83e 5f 63 6f 6f 6b 69 65 20 3d 20 28 69 6e 74 29 70 _cookie = (int)p
1d83f 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 64 62 In3->u.i;. db
1d840 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 ->flags |= SQLIT
1d841 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b E_InternChanges;
1d842 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 . }else if( pOp
1d843 2d 3e 70 32 3d 3d 42 54 52 45 45 5f 46 49 4c 45 ->p2==BTREE_FILE
1d844 5f 46 4f 52 4d 41 54 20 29 7b 0a 20 20 20 20 2f _FORMAT ){. /
1d845 2a 20 52 65 63 6f 72 64 20 63 68 61 6e 67 65 73 * Record changes
1d846 20 69 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72 in the file for
1d847 6d 61 74 20 2a 2f 0a 20 20 20 20 75 2e 61 75 2e mat */. u.au.
1d848 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 pDb->pSchema->fi
1d849 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 28 75 38 29 le_format = (u8)
1d84a 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 pIn3->u.i;. }.
1d84b 20 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 31 20 if( pOp->p1==1
1d84c 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 76 61 6c 69 ){. /* Invali
1d84d 64 61 74 65 20 61 6c 6c 20 70 72 65 70 61 72 65 date all prepare
1d84e 64 20 73 74 61 74 65 6d 65 6e 74 73 20 77 68 65 d statements whe
1d84f 6e 65 76 65 72 20 74 68 65 20 54 45 4d 50 20 64 never the TEMP d
1d850 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 73 atabase. ** s
1d851 63 68 65 6d 61 20 69 73 20 63 68 61 6e 67 65 64 chema is changed
1d852 2e 20 20 54 69 63 6b 65 74 20 23 31 36 34 34 20 . Ticket #1644
1d853 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 */. sqlite3Ex
1d854 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 pirePreparedStat
1d855 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 7d 0a ements(db);. }.
1d856 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1d857 70 63 6f 64 65 3a 20 56 65 72 69 66 79 43 6f 6f pcode: VerifyCoo
1d858 6b 69 65 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a kie P1 P2 *.**.*
1d859 2a 20 43 68 65 63 6b 20 74 68 65 20 76 61 6c 75 * Check the valu
1d85a 65 20 6f 66 20 67 6c 6f 62 61 6c 20 64 61 74 61 e of global data
1d85b 62 61 73 65 20 70 61 72 61 6d 65 74 65 72 20 6e base parameter n
1d85c 75 6d 62 65 72 20 30 20 28 74 68 65 0a 2a 2a 20 umber 0 (the.**
1d85d 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 29 20 schema version)
1d85e 61 6e 64 20 6d 61 6b 65 20 73 75 72 65 20 69 74 and make sure it
1d85f 20 69 73 20 65 71 75 61 6c 20 74 6f 20 50 32 2e is equal to P2.
1d860 20 20 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 .** P1 is the
1d861 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 database number
1d862 77 68 69 63 68 20 69 73 20 30 20 66 6f 72 20 74 which is 0 for t
1d863 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
1d864 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 31 20 66 file.** and 1 f
1d865 6f 72 20 74 68 65 20 66 69 6c 65 20 68 6f 6c 64 or the file hold
1d866 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61 ing temporary ta
1d867 62 6c 65 73 20 61 6e 64 20 73 6f 6d 65 20 68 69 bles and some hi
1d868 67 68 65 72 20 6e 75 6d 62 65 72 0a 2a 2a 20 66 gher number.** f
1d869 6f 72 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 or auxiliary dat
1d86a 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 abases..**.** Th
1d86b 65 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73 e cookie changes
1d86c 20 69 74 73 20 76 61 6c 75 65 20 77 68 65 6e 65 its value whene
1d86d 76 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65 ver the database
1d86e 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 2e schema changes.
1d86f 0a 2a 2a 20 54 68 69 73 20 6f 70 65 72 61 74 69 .** This operati
1d870 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 on is used to de
1d871 74 65 63 74 20 77 68 65 6e 20 74 68 61 74 20 74 tect when that t
1d872 68 65 20 63 6f 6f 6b 69 65 20 68 61 73 20 63 68 he cookie has ch
1d873 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 61 anged.** and tha
1d874 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 72 t the current pr
1d875 6f 63 65 73 73 20 6e 65 65 64 73 20 74 6f 20 72 ocess needs to r
1d876 65 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 eread the schema
1d877 2e 0a 2a 2a 0a 2a 2a 20 45 69 74 68 65 72 20 61 ..**.** Either a
1d878 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 transaction nee
1d879 64 73 20 74 6f 20 68 61 76 65 20 62 65 65 6e 20 ds to have been
1d87a 73 74 61 72 74 65 64 20 6f 72 20 61 6e 20 4f 50 started or an OP
1d87b 5f 4f 70 65 6e 20 6e 65 65 64 73 0a 2a 2a 20 74 _Open needs.** t
1d87c 6f 20 62 65 20 65 78 65 63 75 74 65 64 20 28 74 o be executed (t
1d87d 6f 20 65 73 74 61 62 6c 69 73 68 20 61 20 72 65 o establish a re
1d87e 61 64 20 6c 6f 63 6b 29 20 62 65 66 6f 72 65 20 ad lock) before
1d87f 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 0a 2a this opcode is.*
1d880 2a 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2f 0a 63 61 * invoked..*/.ca
1d881 73 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b se OP_VerifyCook
1d882 69 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 ie: {.#if 0 /*
1d883 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1d884 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 76 20 moved into u.av
1d885 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a */. int iMeta;.
1d886 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 23 65 Btree *pBt;.#e
1d887 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1d888 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1d889 74 6f 20 75 2e 61 76 20 2a 2f 0a 20 20 61 73 73 to u.av */. ass
1d88a 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1d88b 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e && pOp->p1<db->n
1d88c 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Db );. assert(
1d88d 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 (p->btreeMask &
1d88e 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 (1<<pOp->p1))!=0
1d88f 20 29 3b 0a 20 20 75 2e 61 76 2e 70 42 74 20 3d );. u.av.pBt =
1d890 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 db->aDb[pOp->p1
1d891 5d 2e 70 42 74 3b 0a 20 20 69 66 28 20 75 2e 61 ].pBt;. if( u.a
1d892 76 2e 70 42 74 20 29 7b 0a 20 20 20 20 73 71 6c v.pBt ){. sql
1d893 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 ite3BtreeGetMeta
1d894 28 75 2e 61 76 2e 70 42 74 2c 20 42 54 52 45 45 (u.av.pBt, BTREE
1d895 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c _SCHEMA_VERSION,
1d896 20 28 75 33 32 20 2a 29 26 75 2e 61 76 2e 69 4d (u32 *)&u.av.iM
1d897 65 74 61 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 eta);. }else{.
1d898 20 20 20 75 2e 61 76 2e 69 4d 65 74 61 20 3d 20 u.av.iMeta =
1d899 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 75 2e 61 0;. }. if( u.a
1d89a 76 2e 69 4d 65 74 61 21 3d 70 4f 70 2d 3e 70 32 v.iMeta!=pOp->p2
1d89b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 ){. sqlite3D
1d89c 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 bFree(db, p->zEr
1d89d 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 rMsg);. p->zE
1d89e 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44 rrMsg = sqlite3D
1d89f 62 53 74 72 44 75 70 28 64 62 2c 20 22 64 61 74 bStrDup(db, "dat
1d8a0 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 abase schema has
1d8a1 20 63 68 61 6e 67 65 64 22 29 3b 0a 20 20 20 20 changed");.
1d8a2 2f 2a 20 49 66 20 74 68 65 20 73 63 68 65 6d 61 /* If the schema
1d8a3 2d 63 6f 6f 6b 69 65 20 66 72 6f 6d 20 74 68 65 -cookie from the
1d8a4 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6d database file m
1d8a5 61 74 63 68 65 73 20 74 68 65 20 63 6f 6f 6b 69 atches the cooki
1d8a6 65 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 e. ** stored
1d8a7 77 69 74 68 20 74 68 65 20 69 6e 2d 6d 65 6d 6f with the in-memo
1d8a8 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ry representatio
1d8a9 6e 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 2c n of the schema,
1d8aa 20 64 6f 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72 do. ** not r
1d8ab 65 6c 6f 61 64 20 74 68 65 20 73 63 68 65 6d 61 eload the schema
1d8ac 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 from the databa
1d8ad 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a se file.. **.
1d8ae 20 20 20 20 2a 2a 20 49 66 20 76 69 72 74 75 61 ** If virtua
1d8af 6c 2d 74 61 62 6c 65 73 20 61 72 65 20 69 6e 20 l-tables are in
1d8b0 75 73 65 2c 20 74 68 69 73 20 69 73 20 6e 6f 74 use, this is not
1d8b1 20 6a 75 73 74 20 61 6e 20 6f 70 74 69 6d 69 7a just an optimiz
1d8b2 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 20 4f 66 ation.. ** Of
1d8b3 74 65 6e 2c 20 76 2d 74 61 62 6c 65 73 20 73 74 ten, v-tables st
1d8b4 6f 72 65 20 74 68 65 69 72 20 64 61 74 61 20 69 ore their data i
1d8b5 6e 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 74 n other SQLite t
1d8b6 61 62 6c 65 73 2c 20 77 68 69 63 68 0a 20 20 20 ables, which.
1d8b7 20 2a 2a 20 61 72 65 20 71 75 65 72 69 65 64 20 ** are queried
1d8b8 66 72 6f 6d 20 77 69 74 68 69 6e 20 78 4e 65 78 from within xNex
1d8b9 74 28 29 20 61 6e 64 20 6f 74 68 65 72 20 76 2d t() and other v-
1d8ba 74 61 62 6c 65 20 6d 65 74 68 6f 64 73 20 75 73 table methods us
1d8bb 69 6e 67 0a 20 20 20 20 2a 2a 20 70 72 65 70 61 ing. ** prepa
1d8bc 72 65 64 20 71 75 65 72 69 65 73 2e 20 49 66 20 red queries. If
1d8bd 73 75 63 68 20 61 20 71 75 65 72 79 20 69 73 20 such a query is
1d8be 6f 75 74 2d 6f 66 2d 64 61 74 65 2c 20 77 65 20 out-of-date, we
1d8bf 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f 0a 20 do not want to.
1d8c0 20 20 20 2a 2a 20 64 69 73 63 61 72 64 20 74 68 ** discard th
1d8c1 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d e database schem
1d8c2 61 2c 20 61 73 20 74 68 65 20 75 73 65 72 20 63 a, as the user c
1d8c3 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 ode implementing
1d8c4 20 74 68 65 0a 20 20 20 20 2a 2a 20 76 2d 74 61 the. ** v-ta
1d8c5 62 6c 65 20 77 6f 75 6c 64 20 68 61 76 65 20 74 ble would have t
1d8c6 6f 20 62 65 20 72 65 61 64 79 20 66 6f 72 20 74 o be ready for t
1d8c7 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 he sqlite3_vtab
1d8c8 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 structure itself
1d8c9 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e . ** to be in
1d8ca 76 61 6c 69 64 61 74 65 64 20 77 68 65 6e 65 76 validated whenev
1d8cb 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 er sqlite3_step(
1d8cc 29 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d ) is called from
1d8cd 20 77 69 74 68 69 6e 0a 20 20 20 20 2a 2a 20 61 within. ** a
1d8ce 20 76 2d 74 61 62 6c 65 20 6d 65 74 68 6f 64 2e v-table method.
1d8cf 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1d8d0 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d db->aDb[pOp->p1]
1d8d1 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 .pSchema->schema
1d8d2 5f 63 6f 6f 6b 69 65 21 3d 75 2e 61 76 2e 69 4d _cookie!=u.av.iM
1d8d3 65 74 61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c eta ){. sql
1d8d4 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 ite3ResetInterna
1d8d5 6c 53 63 68 65 6d 61 28 64 62 2c 20 70 4f 70 2d lSchema(db, pOp-
1d8d6 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 >p1);. }..
1d8d7 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 sqlite3ExpirePr
1d8d8 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 eparedStatements
1d8d9 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 (db);. rc = S
1d8da 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 QLITE_SCHEMA;.
1d8db 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1d8dc 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 52 65 61 Opcode: OpenRea
1d8dd 64 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 d P1 P2 P3 P4 P5
1d8de 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65 .**.** Open a re
1d8df 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 20 66 ad-only cursor f
1d8e0 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 or the database
1d8e1 74 61 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 table whose root
1d8e2 20 70 61 67 65 20 69 73 0a 2a 2a 20 50 32 20 69 page is.** P2 i
1d8e3 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c n a database fil
1d8e4 65 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65 e. The database
1d8e5 20 66 69 6c 65 20 69 73 20 64 65 74 65 72 6d 69 file is determi
1d8e6 6e 65 64 20 62 79 20 50 33 2e 20 0a 2a 2a 20 50 ned by P3. .** P
1d8e7 33 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 20 6d 3==0 means the m
1d8e8 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 50 33 ain database, P3
1d8e9 3d 3d 31 20 6d 65 61 6e 73 20 74 68 65 20 64 61 ==1 means the da
1d8ea 74 61 62 61 73 65 20 75 73 65 64 20 66 6f 72 20 tabase used for
1d8eb 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 .** temporary ta
1d8ec 62 6c 65 73 2c 20 61 6e 64 20 50 33 3e 31 20 6d bles, and P3>1 m
1d8ed 65 61 6e 73 20 75 73 65 64 20 74 68 65 20 63 6f eans used the co
1d8ee 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 74 74 61 rresponding atta
1d8ef 63 68 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 ched.** database
1d8f0 2e 20 20 47 69 76 65 20 74 68 65 20 6e 65 77 20 . Give the new
1d8f1 63 75 72 73 6f 72 20 61 6e 20 69 64 65 6e 74 69 cursor an identi
1d8f2 66 69 65 72 20 6f 66 20 50 31 2e 20 20 54 68 65 fier of P1. The
1d8f3 20 50 31 0a 2a 2a 20 76 61 6c 75 65 73 20 6e 65 P1.** values ne
1d8f4 65 64 20 6e 6f 74 20 62 65 20 63 6f 6e 74 69 67 ed not be contig
1d8f5 75 6f 75 73 20 62 75 74 20 61 6c 6c 20 50 31 20 uous but all P1
1d8f6 76 61 6c 75 65 73 20 73 68 6f 75 6c 64 20 62 65 values should be
1d8f7 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e small integers.
1d8f8 0a 2a 2a 20 49 74 20 69 73 20 61 6e 20 65 72 72 .** It is an err
1d8f9 6f 72 20 66 6f 72 20 50 31 20 74 6f 20 62 65 20 or for P1 to be
1d8fa 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 negative..**.**
1d8fb 49 66 20 50 35 21 3d 30 20 74 68 65 6e 20 75 73 If P5!=0 then us
1d8fc 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 e the content of
1d8fd 20 72 65 67 69 73 74 65 72 20 50 32 20 61 73 20 register P2 as
1d8fe 74 68 65 20 72 6f 6f 74 20 70 61 67 65 2c 20 6e the root page, n
1d8ff 6f 74 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 ot.** the value
1d900 6f 66 20 50 32 20 69 74 73 65 6c 66 2e 0a 2a 2a of P2 itself..**
1d901 0a 2a 2a 20 54 68 65 72 65 20 77 69 6c 6c 20 62 .** There will b
1d902 65 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e e a read lock on
1d903 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 68 the database wh
1d904 65 6e 65 76 65 72 20 74 68 65 72 65 20 69 73 20 enever there is
1d905 61 6e 0a 2a 2a 20 6f 70 65 6e 20 63 75 72 73 6f an.** open curso
1d906 72 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 62 r. If the datab
1d907 61 73 65 20 77 61 73 20 75 6e 6c 6f 63 6b 65 64 ase was unlocked
1d908 20 70 72 69 6f 72 20 74 6f 20 74 68 69 73 20 69 prior to this i
1d909 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 74 68 nstruction.** th
1d90a 65 6e 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 69 en a read lock i
1d90b 73 20 61 63 71 75 69 72 65 64 20 61 73 20 70 61 s acquired as pa
1d90c 72 74 20 6f 66 20 74 68 69 73 20 69 6e 73 74 72 rt of this instr
1d90d 75 63 74 69 6f 6e 2e 20 20 41 20 72 65 61 64 0a uction. A read.
1d90e 2a 2a 20 6c 6f 63 6b 20 61 6c 6c 6f 77 73 20 6f ** lock allows o
1d90f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 74 ther processes t
1d910 6f 20 72 65 61 64 20 74 68 65 20 64 61 74 61 62 o read the datab
1d911 61 73 65 20 62 75 74 20 70 72 6f 68 69 62 69 74 ase but prohibit
1d912 73 0a 2a 2a 20 61 6e 79 20 6f 74 68 65 72 20 70 s.** any other p
1d913 72 6f 63 65 73 73 20 66 72 6f 6d 20 6d 6f 64 69 rocess from modi
1d914 66 79 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 fying the databa
1d915 73 65 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f se. The read lo
1d916 63 6b 20 69 73 0a 2a 2a 20 72 65 6c 65 61 73 65 ck is.** release
1d917 64 20 77 68 65 6e 20 61 6c 6c 20 63 75 72 73 6f d when all curso
1d918 72 73 20 61 72 65 20 63 6c 6f 73 65 64 2e 20 20 rs are closed.
1d919 49 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 If this instruct
1d91a 69 6f 6e 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20 ion attempts.**
1d91b 74 6f 20 67 65 74 20 61 20 72 65 61 64 20 6c 6f to get a read lo
1d91c 63 6b 20 62 75 74 20 66 61 69 6c 73 2c 20 74 68 ck but fails, th
1d91d 65 20 73 63 72 69 70 74 20 74 65 72 6d 69 6e 61 e script termina
1d91e 74 65 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 tes with an.** S
1d91f 51 4c 49 54 45 5f 42 55 53 59 20 65 72 72 6f 72 QLITE_BUSY error
1d920 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 code..**.** The
1d921 20 50 34 20 76 61 6c 75 65 20 6d 61 79 20 62 65 P4 value may be
1d922 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67 either an integ
1d923 65 72 20 28 50 34 5f 49 4e 54 33 32 29 20 6f 72 er (P4_INT32) or
1d924 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a a pointer to.**
1d925 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 a KeyInfo struc
1d926 74 75 72 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f ture (P4_KEYINFO
1d927 29 2e 20 49 66 20 69 74 20 69 73 20 61 20 70 6f ). If it is a po
1d928 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e inter to a KeyIn
1d929 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 fo .** structure
1d92a 2c 20 74 68 65 6e 20 73 61 69 64 20 73 74 72 75 , then said stru
1d92b 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 68 cture defines th
1d92c 65 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f e content and co
1d92d 6c 6c 61 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75 llating .** sequ
1d92e 65 6e 63 65 20 6f 66 20 74 68 65 20 69 6e 64 65 ence of the inde
1d92f 78 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 x being opened.
1d930 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 50 34 Otherwise, if P4
1d931 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 0a is an integer .
1d932 2a 2a 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20 ** value, it is
1d933 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 set to the numbe
1d934 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
1d935 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a the table..**.**
1d936 20 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e 57 72 See also OpenWr
1d937 69 74 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 ite..*/./* Opcod
1d938 65 3a 20 4f 70 65 6e 57 72 69 74 65 20 50 31 20 e: OpenWrite P1
1d939 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a P2 P3 P4 P5.**.*
1d93a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2f 77 72 * Open a read/wr
1d93b 69 74 65 20 63 75 72 73 6f 72 20 6e 61 6d 65 64 ite cursor named
1d93c 20 50 31 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 P1 on the table
1d93d 20 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20 or index whose
1d93e 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 root.** page is
1d93f 50 32 2e 20 20 4f 72 20 69 66 20 50 35 21 3d 30 P2. Or if P5!=0
1d940 20 75 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 use the content
1d941 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 32 20 of register P2
1d942 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20 72 to find the.** r
1d943 6f 6f 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 oot page..**.**
1d944 54 68 65 20 50 34 20 76 61 6c 75 65 20 6d 61 79 The P4 value may
1d945 20 62 65 20 65 69 74 68 65 72 20 61 6e 20 69 6e be either an in
1d946 74 65 67 65 72 20 28 50 34 5f 49 4e 54 33 32 29 teger (P4_INT32)
1d947 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f or a pointer to
1d948 0a 2a 2a 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 .** a KeyInfo st
1d949 72 75 63 74 75 72 65 20 28 50 34 5f 4b 45 59 49 ructure (P4_KEYI
1d94a 4e 46 4f 29 2e 20 49 66 20 69 74 20 69 73 20 61 NFO). If it is a
1d94b 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 pointer to a Ke
1d94c 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 yInfo .** struct
1d94d 75 72 65 2c 20 74 68 65 6e 20 73 61 69 64 20 73 ure, then said s
1d94e 74 72 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 tructure defines
1d94f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 6e 64 the content and
1d950 20 63 6f 6c 6c 61 74 69 6e 67 20 0a 2a 2a 20 73 collating .** s
1d951 65 71 75 65 6e 63 65 20 6f 66 20 74 68 65 20 69 equence of the i
1d952 6e 64 65 78 20 62 65 69 6e 67 20 6f 70 65 6e 65 ndex being opene
1d953 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 d. Otherwise, if
1d954 20 50 34 20 69 73 20 61 6e 20 69 6e 74 65 67 65 P4 is an intege
1d955 72 20 0a 2a 2a 20 76 61 6c 75 65 2c 20 69 74 20 r .** value, it
1d956 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 is set to the nu
1d957 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
1d958 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 6f 72 in the table, or
1d959 20 74 6f 20 74 68 65 0a 2a 2a 20 6c 61 72 67 65 to the.** large
1d95a 73 74 20 69 6e 64 65 78 20 6f 66 20 61 6e 79 20 st index of any
1d95b 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 column of the ta
1d95c 62 6c 65 20 74 68 61 74 20 69 73 20 61 63 74 75 ble that is actu
1d95d 61 6c 6c 79 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a ally used..**.**
1d95e 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f This instructio
1d95f 6e 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b n works just lik
1d960 65 20 4f 70 65 6e 52 65 61 64 20 65 78 63 65 70 e OpenRead excep
1d961 74 20 74 68 61 74 20 69 74 20 6f 70 65 6e 73 20 t that it opens
1d962 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 6e the cursor.** in
1d963 20 72 65 61 64 2f 77 72 69 74 65 20 6d 6f 64 65 read/write mode
1d964 2e 20 20 46 6f 72 20 61 20 67 69 76 65 6e 20 74 . For a given t
1d965 61 62 6c 65 2c 20 74 68 65 72 65 20 63 61 6e 20 able, there can
1d966 62 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 be one or more r
1d967 65 61 64 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73 ead-only.** curs
1d968 6f 72 73 20 6f 72 20 61 20 73 69 6e 67 6c 65 20 ors or a single
1d969 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f read/write curso
1d96a 72 20 62 75 74 20 6e 6f 74 20 62 6f 74 68 2e 0a r but not both..
1d96b 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f **.** See also O
1d96c 70 65 6e 52 65 61 64 2e 0a 2a 2f 0a 63 61 73 65 penRead..*/.case
1d96d 20 4f 50 5f 4f 70 65 6e 52 65 61 64 3a 0a 63 61 OP_OpenRead:.ca
1d96e 73 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 3a se OP_OpenWrite:
1d96f 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1d970 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1d971 65 64 20 69 6e 74 6f 20 75 2e 61 77 20 2a 2f 0a ed into u.aw */.
1d972 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 int nField;.
1d973 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 KeyInfo *pKeyInf
1d974 6f 3b 0a 20 20 69 6e 74 20 70 32 3b 0a 20 20 69 o;. int p2;. i
1d975 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 77 72 nt iDb;. int wr
1d976 46 6c 61 67 3b 0a 20 20 42 74 72 65 65 20 2a 70 Flag;. Btree *p
1d977 58 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 X;. VdbeCursor
1d978 2a 70 43 75 72 3b 0a 20 20 44 62 20 2a 70 44 62 *pCur;. Db *pDb
1d979 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1d97a 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1d97b 64 20 69 6e 74 6f 20 75 2e 61 77 20 2a 2f 0a 0a d into u.aw */..
1d97c 20 20 75 2e 61 77 2e 6e 46 69 65 6c 64 20 3d 20 u.aw.nField =
1d97d 30 3b 0a 20 20 75 2e 61 77 2e 70 4b 65 79 49 6e 0;. u.aw.pKeyIn
1d97e 66 6f 20 3d 20 30 3b 0a 20 20 75 2e 61 77 2e 70 fo = 0;. u.aw.p
1d97f 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2 = pOp->p2;. u
1d980 2e 61 77 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 .aw.iDb = pOp->p
1d981 33 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 3;. assert( u.a
1d982 77 2e 69 44 62 3e 3d 30 20 26 26 20 75 2e 61 77 w.iDb>=0 && u.aw
1d983 2e 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a .iDb<db->nDb );.
1d984 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 assert( (p->bt
1d985 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 75 2e reeMask & (1<<u.
1d986 61 77 2e 69 44 62 29 29 21 3d 30 20 29 3b 0a 20 aw.iDb))!=0 );.
1d987 20 75 2e 61 77 2e 70 44 62 20 3d 20 26 64 62 2d u.aw.pDb = &db-
1d988 3e 61 44 62 5b 75 2e 61 77 2e 69 44 62 5d 3b 0a >aDb[u.aw.iDb];.
1d989 20 20 75 2e 61 77 2e 70 58 20 3d 20 75 2e 61 77 u.aw.pX = u.aw
1d98a 2e 70 44 62 2d 3e 70 42 74 3b 0a 20 20 61 73 73 .pDb->pBt;. ass
1d98b 65 72 74 28 20 75 2e 61 77 2e 70 58 21 3d 30 20 ert( u.aw.pX!=0
1d98c 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 );. if( pOp->op
1d98d 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 code==OP_OpenWri
1d98e 74 65 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 77 te ){. u.aw.w
1d98f 72 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 69 rFlag = 1;. i
1d990 66 28 20 75 2e 61 77 2e 70 44 62 2d 3e 70 53 63 f( u.aw.pDb->pSc
1d991 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 hema->file_forma
1d992 74 20 3c 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 t < p->minWriteF
1d993 69 6c 65 46 6f 72 6d 61 74 20 29 7b 0a 20 20 20 ileFormat ){.
1d994 20 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 p->minWriteFi
1d995 6c 65 46 6f 72 6d 61 74 20 3d 20 75 2e 61 77 2e leFormat = u.aw.
1d996 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 pDb->pSchema->fi
1d997 6c 65 5f 66 6f 72 6d 61 74 3b 0a 20 20 20 20 7d le_format;. }
1d998 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e . }else{. u.
1d999 61 77 2e 77 72 46 6c 61 67 20 3d 20 30 3b 0a 20 aw.wrFlag = 0;.
1d99a 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 }. if( pOp->p5
1d99b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1d99c 75 2e 61 77 2e 70 32 3e 30 20 29 3b 0a 20 20 20 u.aw.p2>0 );.
1d99d 20 61 73 73 65 72 74 28 20 75 2e 61 77 2e 70 32 assert( u.aw.p2
1d99e 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 <=p->nMem );.
1d99f 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d pIn2 = &p->aMem
1d9a0 5b 75 2e 61 77 2e 70 32 5d 3b 0a 20 20 20 20 73 [u.aw.p2];. s
1d9a1 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 qlite3VdbeMemInt
1d9a2 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b 0a 20 egerify(pIn2);.
1d9a3 20 20 20 75 2e 61 77 2e 70 32 20 3d 20 28 69 6e u.aw.p2 = (in
1d9a4 74 29 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 t)pIn2->u.i;.
1d9a5 20 2f 2a 20 54 68 65 20 75 2e 61 77 2e 70 32 20 /* The u.aw.p2
1d9a6 76 61 6c 75 65 20 61 6c 77 61 79 73 20 63 6f 6d value always com
1d9a7 65 73 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 es from a prior
1d9a8 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 6f OP_CreateTable o
1d9a9 70 63 6f 64 65 20 61 6e 64 0a 20 20 20 20 2a 2a pcode and. **
1d9aa 20 74 68 61 74 20 6f 70 63 6f 64 65 20 77 69 6c that opcode wil
1d9ab 6c 20 61 6c 77 61 79 73 20 73 65 74 20 74 68 65 l always set the
1d9ac 20 75 2e 61 77 2e 70 32 20 76 61 6c 75 65 20 74 u.aw.p2 value t
1d9ad 6f 20 32 20 6f 72 20 6d 6f 72 65 20 6f 72 20 65 o 2 or more or e
1d9ae 6c 73 65 20 66 61 69 6c 2e 0a 20 20 20 20 2a 2a lse fail.. **
1d9af 20 49 66 20 74 68 65 72 65 20 77 65 72 65 20 61 If there were a
1d9b0 20 66 61 69 6c 75 72 65 2c 20 74 68 65 20 70 72 failure, the pr
1d9b1 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
1d9b2 20 77 6f 75 6c 64 20 68 61 76 65 20 68 61 6c 74 would have halt
1d9b3 65 64 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 ed. ** before
1d9b4 20 72 65 61 63 68 69 6e 67 20 74 68 69 73 20 69 reaching this i
1d9b5 6e 73 74 72 75 63 74 69 6f 6e 2e 20 2a 2f 0a 20 nstruction. */.
1d9b6 20 20 20 69 66 28 20 4e 45 56 45 52 28 75 2e 61 if( NEVER(u.a
1d9b7 77 2e 70 32 3c 32 29 20 29 20 7b 0a 20 20 20 20 w.p2<2) ) {.
1d9b8 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f rc = SQLITE_CO
1d9b9 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
1d9ba 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 goto abort_due
1d9bb 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d _to_error;. }
1d9bc 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e . }. if( pOp->
1d9bd 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e p4type==P4_KEYIN
1d9be 46 4f 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 70 FO ){. u.aw.p
1d9bf 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 KeyInfo = pOp->p
1d9c0 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 4.pKeyInfo;.
1d9c1 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 2d 3e 65 u.aw.pKeyInfo->e
1d9c2 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 29 3b nc = ENC(p->db);
1d9c3 0a 20 20 20 20 75 2e 61 77 2e 6e 46 69 65 6c 64 . u.aw.nField
1d9c4 20 3d 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f = u.aw.pKeyInfo
1d9c5 2d 3e 6e 46 69 65 6c 64 2b 31 3b 0a 20 20 7d 65 ->nField+1;. }e
1d9c6 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 lse if( pOp->p4t
1d9c7 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 7b ype==P4_INT32 ){
1d9c8 0a 20 20 20 20 75 2e 61 77 2e 6e 46 69 65 6c 64 . u.aw.nField
1d9c9 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 = pOp->p4.i;.
1d9ca 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d }. assert( pOp-
1d9cb 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61 77 >p1>=0 );. u.aw
1d9cc 2e 70 43 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65 .pCur = allocate
1d9cd 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 Cursor(p, pOp->p
1d9ce 31 2c 20 75 2e 61 77 2e 6e 46 69 65 6c 64 2c 20 1, u.aw.nField,
1d9cf 75 2e 61 77 2e 69 44 62 2c 20 31 29 3b 0a 20 20 u.aw.iDb, 1);.
1d9d0 69 66 28 20 75 2e 61 77 2e 70 43 75 72 3d 3d 30 if( u.aw.pCur==0
1d9d1 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1d9d2 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 6e 75 6c u.aw.pCur->nul
1d9d3 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72 63 20 3d lRow = 1;. rc =
1d9d4 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
1d9d5 73 6f 72 28 75 2e 61 77 2e 70 58 2c 20 75 2e 61 sor(u.aw.pX, u.a
1d9d6 77 2e 70 32 2c 20 75 2e 61 77 2e 77 72 46 6c 61 w.p2, u.aw.wrFla
1d9d7 67 2c 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f g, u.aw.pKeyInfo
1d9d8 2c 20 75 2e 61 77 2e 70 43 75 72 2d 3e 70 43 75 , u.aw.pCur->pCu
1d9d9 72 73 6f 72 29 3b 0a 20 20 75 2e 61 77 2e 70 43 rsor);. u.aw.pC
1d9da 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 ur->pKeyInfo = u
1d9db 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 3b 0a 0a 20 .aw.pKeyInfo;..
1d9dc 20 2f 2a 20 53 69 6e 63 65 20 69 74 20 70 65 72 /* Since it per
1d9dd 66 6f 72 6d 73 20 6e 6f 20 6d 65 6d 6f 72 79 20 forms no memory
1d9de 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 20 49 4f allocation or IO
1d9df 2c 20 74 68 65 20 6f 6e 6c 79 20 76 61 6c 75 65 , the only value
1d9e0 73 20 74 68 61 74 0a 20 20 2a 2a 20 73 71 6c 69 s that. ** sqli
1d9e1 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 29 te3BtreeCursor()
1d9e2 20 6d 61 79 20 72 65 74 75 72 6e 20 61 72 65 20 may return are
1d9e3 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 61 6e 64 SQLITE_EMPTY and
1d9e4 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2a SQLITE_OK.. **
1d9e5 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 69 73 SQLITE_EMPTY is
1d9e6 20 6f 6e 6c 79 20 72 65 74 75 72 6e 65 64 20 77 only returned w
1d9e7 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74 hen attempting t
1d9e8 6f 20 6f 70 65 6e 20 74 68 65 20 74 61 62 6c 65 o open the table
1d9e9 0a 20 20 2a 2a 20 72 6f 6f 74 65 64 20 61 74 20 . ** rooted at
1d9ea 70 61 67 65 20 31 20 6f 66 20 61 20 7a 65 72 6f page 1 of a zero
1d9eb 2d 62 79 74 65 20 64 61 74 61 62 61 73 65 2e 20 -byte database.
1d9ec 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 72 63 */. assert( rc
1d9ed 3d 3d 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 7c ==SQLITE_EMPTY |
1d9ee 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 | rc==SQLITE_OK
1d9ef 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
1d9f0 49 54 45 5f 45 4d 50 54 59 20 29 7b 0a 20 20 20 ITE_EMPTY ){.
1d9f1 20 75 2e 61 77 2e 70 43 75 72 2d 3e 70 43 75 72 u.aw.pCur->pCur
1d9f2 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 sor = 0;. rc
1d9f3 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d = SQLITE_OK;. }
1d9f4 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 56 .. /* Set the V
1d9f5 64 62 65 43 75 72 73 6f 72 2e 69 73 54 61 62 6c dbeCursor.isTabl
1d9f6 65 20 61 6e 64 20 69 73 49 6e 64 65 78 20 76 61 e and isIndex va
1d9f7 72 69 61 62 6c 65 73 2e 20 50 72 65 76 69 6f 75 riables. Previou
1d9f8 73 20 76 65 72 73 69 6f 6e 73 20 6f 66 0a 20 20 s versions of.
1d9f9 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 64 20 74 ** SQLite used t
1d9fa 6f 20 63 68 65 63 6b 20 69 66 20 74 68 65 20 72 o check if the r
1d9fb 6f 6f 74 2d 70 61 67 65 20 66 6c 61 67 73 20 77 oot-page flags w
1d9fc 65 72 65 20 73 61 6e 65 20 61 74 20 74 68 69 73 ere sane at this
1d9fd 20 70 6f 69 6e 74 0a 20 20 2a 2a 20 61 6e 64 20 point. ** and
1d9fe 72 65 70 6f 72 74 20 64 61 74 61 62 61 73 65 20 report database
1d9ff 63 6f 72 72 75 70 74 69 6f 6e 20 69 66 20 74 68 corruption if th
1da00 65 79 20 77 65 72 65 20 6e 6f 74 2c 20 62 75 74 ey were not, but
1da01 20 74 68 69 73 20 63 68 65 63 6b 20 68 61 73 0a this check has.
1da02 20 20 2a 2a 20 73 69 6e 63 65 20 6d 6f 76 65 64 ** since moved
1da03 20 69 6e 74 6f 20 74 68 65 20 62 74 72 65 65 20 into the btree
1da04 6c 61 79 65 72 2e 20 20 2a 2f 0a 20 20 75 2e 61 layer. */. u.a
1da05 77 2e 70 43 75 72 2d 3e 69 73 54 61 62 6c 65 20 w.pCur->isTable
1da06 3d 20 70 4f 70 2d 3e 70 34 74 79 70 65 21 3d 50 = pOp->p4type!=P
1da07 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 75 2e 61 4_KEYINFO;. u.a
1da08 77 2e 70 43 75 72 2d 3e 69 73 49 6e 64 65 78 20 w.pCur->isIndex
1da09 3d 20 21 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73 = !u.aw.pCur->is
1da0a 54 61 62 6c 65 3b 0a 20 20 62 72 65 61 6b 3b 0a Table;. break;.
1da0b 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 }../* Opcode: Op
1da0c 65 6e 45 70 68 65 6d 65 72 61 6c 20 50 31 20 50 enEphemeral P1 P
1da0d 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 2 * P4 *.**.** O
1da0e 70 65 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72 pen a new cursor
1da0f 20 50 31 20 74 6f 20 61 20 74 72 61 6e 73 69 65 P1 to a transie
1da10 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 nt table..** The
1da11 20 63 75 72 73 6f 72 20 69 73 20 61 6c 77 61 79 cursor is alway
1da12 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2f 77 72 s opened read/wr
1da13 69 74 65 20 65 76 65 6e 20 69 66 20 0a 2a 2a 20 ite even if .**
1da14 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
1da15 65 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 e is read-only.
1da16 20 54 68 65 20 74 72 61 6e 73 69 65 6e 74 20 6f The transient o
1da17 72 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 r virtual.** tab
1da18 6c 65 20 69 73 20 64 65 6c 65 74 65 64 20 61 75 le is deleted au
1da19 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77 68 65 6e tomatically when
1da1a 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 63 the cursor is c
1da1b 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 losed..**.** P2
1da1c 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
1da1d 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
1da1e 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a virtual table..*
1da1f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f 69 * The cursor poi
1da20 6e 74 73 20 74 6f 20 61 20 42 54 72 65 65 20 74 nts to a BTree t
1da21 61 62 6c 65 20 69 66 20 50 34 3d 3d 30 20 61 6e able if P4==0 an
1da22 64 20 74 6f 20 61 20 42 54 72 65 65 20 69 6e 64 d to a BTree ind
1da23 65 78 0a 2a 2a 20 69 66 20 50 34 20 69 73 20 6e ex.** if P4 is n
1da24 6f 74 20 30 2e 20 20 49 66 20 50 34 20 69 73 20 ot 0. If P4 is
1da25 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 not NULL, it poi
1da26 6e 74 73 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f nts to a KeyInfo
1da27 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 74 68 structure.** th
1da28 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 66 at defines the f
1da29 6f 72 6d 61 74 20 6f 66 20 6b 65 79 73 20 69 6e ormat of keys in
1da2a 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a the index..**.*
1da2b 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 61 * This opcode wa
1da2c 73 20 6f 6e 63 65 20 63 61 6c 6c 65 64 20 4f 70 s once called Op
1da2d 65 6e 54 65 6d 70 2e 20 20 42 75 74 20 74 68 61 enTemp. But tha
1da2e 74 20 63 72 65 61 74 65 64 0a 2a 2a 20 63 6f 6e t created.** con
1da2f 66 75 73 69 6f 6e 20 62 65 63 61 75 73 65 20 74 fusion because t
1da30 68 65 20 74 65 72 6d 20 22 74 65 6d 70 20 74 61 he term "temp ta
1da31 62 6c 65 22 2c 20 6d 69 67 68 74 20 72 65 66 65 ble", might refe
1da32 72 20 65 69 74 68 65 72 0a 2a 2a 20 74 6f 20 61 r either.** to a
1da33 20 54 45 4d 50 20 74 61 62 6c 65 20 61 74 20 74 TEMP table at t
1da34 68 65 20 53 51 4c 20 6c 65 76 65 6c 2c 20 6f 72 he SQL level, or
1da35 20 74 6f 20 61 20 74 61 62 6c 65 20 6f 70 65 6e to a table open
1da36 65 64 20 62 79 0a 2a 2a 20 74 68 69 73 20 6f 70 ed by.** this op
1da37 63 6f 64 65 2e 20 20 54 68 65 6e 20 74 68 69 73 code. Then this
1da38 20 6f 70 63 6f 64 65 20 77 61 73 20 63 61 6c 6c opcode was call
1da39 20 4f 70 65 6e 56 69 72 74 75 61 6c 2e 20 20 42 OpenVirtual. B
1da3a 75 74 0a 2a 2a 20 74 68 61 74 20 63 72 65 61 74 ut.** that creat
1da3b 65 64 20 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74 ed confusion wit
1da3c 68 20 74 68 65 20 77 68 6f 6c 65 20 76 69 72 74 h the whole virt
1da3d 75 61 6c 2d 74 61 62 6c 65 20 69 64 65 61 2e 0a ual-table idea..
1da3e 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 45 */.case OP_OpenE
1da3f 70 68 65 6d 65 72 61 6c 3a 20 7b 0a 23 69 66 20 phemeral: {.#if
1da40 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1da41 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1da42 20 75 2e 61 78 20 2a 2f 0a 20 20 56 64 62 65 43 u.ax */. VdbeC
1da43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 23 65 6e 64 ursor *pCx;.#end
1da44 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1da45 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1da46 20 75 2e 61 78 20 2a 2f 0a 20 20 73 74 61 74 69 u.ax */. stati
1da47 63 20 63 6f 6e 73 74 20 69 6e 74 20 6f 70 65 6e c const int open
1da48 46 6c 61 67 73 20 3d 0a 20 20 20 20 20 20 53 51 Flags =. SQ
1da49 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
1da4a 49 54 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 ITE |. SQLI
1da4b 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c TE_OPEN_CREATE |
1da4c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 . SQLITE_OP
1da4d 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 0a 20 EN_EXCLUSIVE |.
1da4e 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e SQLITE_OPEN
1da4f 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c _DELETEONCLOSE |
1da50 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 . SQLITE_OP
1da51 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 3b EN_TRANSIENT_DB;
1da52 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d .. assert( pOp-
1da53 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61 78 >p1>=0 );. u.ax
1da54 2e 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65 43 .pCx = allocateC
1da55 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 ursor(p, pOp->p1
1da56 2c 20 70 4f 70 2d 3e 70 32 2c 20 2d 31 2c 20 31 , pOp->p2, -1, 1
1da57 29 3b 0a 20 20 69 66 28 20 75 2e 61 78 2e 70 43 );. if( u.ax.pC
1da58 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d x==0 ) goto no_m
1da59 65 6d 3b 0a 20 20 75 2e 61 78 2e 70 43 78 2d 3e em;. u.ax.pCx->
1da5a 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72 nullRow = 1;. r
1da5b 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1da5c 46 61 63 74 6f 72 79 28 64 62 2c 20 30 2c 20 31 Factory(db, 0, 1
1da5d 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 , SQLITE_DEFAULT
1da5e 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 _TEMP_CACHE_SIZE
1da5f 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20 , openFlags,.
1da60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1da61 20 20 20 20 20 20 20 20 26 75 2e 61 78 2e 70 43 &u.ax.pC
1da62 78 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20 72 x->pBt);. if( r
1da63 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1da64 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1da65 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 BtreeBeginTrans(
1da66 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 31 u.ax.pCx->pBt, 1
1da67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d );. }. if( rc=
1da68 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1da69 20 20 2f 2a 20 49 66 20 61 20 74 72 61 6e 73 69 /* If a transi
1da6a 65 6e 74 20 69 6e 64 65 78 20 69 73 20 72 65 71 ent index is req
1da6b 75 69 72 65 64 2c 20 63 72 65 61 74 65 20 69 74 uired, create it
1da6c 20 62 79 20 63 61 6c 6c 69 6e 67 0a 20 20 20 20 by calling.
1da6d 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 ** sqlite3BtreeC
1da6e 72 65 61 74 65 54 61 62 6c 65 28 29 20 77 69 74 reateTable() wit
1da6f 68 20 74 68 65 20 42 54 52 45 45 5f 5a 45 52 4f h the BTREE_ZERO
1da70 44 41 54 41 20 66 6c 61 67 20 62 65 66 6f 72 65 DATA flag before
1da71 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 . ** opening
1da72 69 74 2e 20 49 66 20 61 20 74 72 61 6e 73 69 65 it. If a transie
1da73 6e 74 20 74 61 62 6c 65 20 69 73 20 72 65 71 75 nt table is requ
1da74 69 72 65 64 2c 20 6a 75 73 74 20 75 73 65 20 74 ired, just use t
1da75 68 65 0a 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 he. ** automa
1da76 74 69 63 61 6c 6c 79 20 63 72 65 61 74 65 64 20 tically created
1da77 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d table with root-
1da78 70 61 67 65 20 31 20 28 61 6e 20 49 4e 54 4b 45 page 1 (an INTKE
1da79 59 20 74 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f Y table).. */
1da7a 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 . if( pOp->p4
1da7b 2e 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 .pKeyInfo ){.
1da7c 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 20 int pgno;.
1da7d 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1da7e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e p4type==P4_KEYIN
1da7f 46 4f 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d FO );. rc =
1da80 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 sqlite3BtreeCre
1da81 61 74 65 54 61 62 6c 65 28 75 2e 61 78 2e 70 43 ateTable(u.ax.pC
1da82 78 2d 3e 70 42 74 2c 20 26 70 67 6e 6f 2c 20 42 x->pBt, &pgno, B
1da83 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 29 3b 0a TREE_ZERODATA);.
1da84 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
1da85 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1da86 20 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3d assert( pgno=
1da87 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 2b 31 20 29 =MASTER_ROOT+1 )
1da88 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
1da89 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
1da8a 72 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 2c r(u.ax.pCx->pBt,
1da8b 20 70 67 6e 6f 2c 20 31 2c 0a 20 20 20 20 20 20 pgno, 1,.
1da8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1da8d 20 20 20 20 20 20 20 20 20 20 28 4b 65 79 49 6e (KeyIn
1da8e 66 6f 2a 29 70 4f 70 2d 3e 70 34 2e 7a 2c 20 75 fo*)pOp->p4.z, u
1da8f 2e 61 78 2e 70 43 78 2d 3e 70 43 75 72 73 6f 72 .ax.pCx->pCursor
1da90 29 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 78 2e );. u.ax.
1da91 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 pCx->pKeyInfo =
1da92 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f pOp->p4.pKeyInfo
1da93 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 78 2e 70 ;. u.ax.p
1da94 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e Cx->pKeyInfo->en
1da95 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 29 3b 0a c = ENC(p->db);.
1da96 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 2e }. u.
1da97 61 78 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 ax.pCx->isTable
1da98 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a = 0;. }else{.
1da99 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1da9a 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 75 2e e3BtreeCursor(u.
1da9b 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 4d 41 53 ax.pCx->pBt, MAS
1da9c 54 45 52 5f 52 4f 4f 54 2c 20 31 2c 20 30 2c 20 TER_ROOT, 1, 0,
1da9d 75 2e 61 78 2e 70 43 78 2d 3e 70 43 75 72 73 6f u.ax.pCx->pCurso
1da9e 72 29 3b 0a 20 20 20 20 20 20 75 2e 61 78 2e 70 r);. u.ax.p
1da9f 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 31 3b Cx->isTable = 1;
1daa0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75 2e 61 . }. }. u.a
1daa1 78 2e 70 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d x.pCx->isIndex =
1daa2 20 21 75 2e 61 78 2e 70 43 78 2d 3e 69 73 54 61 !u.ax.pCx->isTa
1daa3 62 6c 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a ble;. break;.}.
1daa4 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e ./* Opcode: Open
1daa5 50 73 65 75 64 6f 20 50 31 20 50 32 20 50 33 20 Pseudo P1 P2 P3
1daa6 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 * *.**.** Open a
1daa7 20 6e 65 77 20 63 75 72 73 6f 72 20 74 68 61 74 new cursor that
1daa8 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66 61 6b points to a fak
1daa9 65 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e e table that con
1daaa 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 0a 2a tains a single.*
1daab 2a 20 72 6f 77 20 6f 66 20 64 61 74 61 2e 20 20 * row of data.
1daac 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 The content of t
1daad 68 61 74 20 6f 6e 65 20 72 6f 77 20 69 6e 20 74 hat one row in t
1daae 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 6d 65 he content of me
1daaf 6d 6f 72 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 mory.** register
1dab0 20 50 32 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 P2. In other w
1dab1 6f 72 64 73 2c 20 63 75 72 73 6f 72 20 50 31 20 ords, cursor P1
1dab2 62 65 63 6f 6d 65 73 20 61 6e 20 61 6c 69 61 73 becomes an alias
1dab3 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 4d 45 4d for the .** MEM
1dab4 5f 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 20 63 6f _Blob content co
1dab5 6e 74 61 69 6e 65 64 20 69 6e 20 72 65 67 69 73 ntained in regis
1dab6 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 41 20 ter P2..**.** A
1dab7 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 63 72 65 pseudo-table cre
1dab8 61 74 65 64 20 62 79 20 74 68 69 73 20 6f 70 63 ated by this opc
1dab9 6f 64 65 20 69 73 20 75 73 65 64 20 74 6f 20 68 ode is used to h
1daba 6f 6c 64 20 74 68 65 20 61 20 73 69 6e 67 6c 65 old the a single
1dabb 0a 2a 2a 20 72 6f 77 20 6f 75 74 70 75 74 20 66 .** row output f
1dabc 72 6f 6d 20 74 68 65 20 73 6f 72 74 65 72 20 73 rom the sorter s
1dabd 6f 20 74 68 61 74 20 74 68 65 20 72 6f 77 20 63 o that the row c
1dabe 61 6e 20 62 65 20 64 65 63 6f 6d 70 6f 73 65 64 an be decomposed
1dabf 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 69 76 69 64 into.** individ
1dac0 75 61 6c 20 63 6f 6c 75 6d 6e 73 20 75 73 69 6e ual columns usin
1dac1 67 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 g the OP_Column
1dac2 6f 70 63 6f 64 65 2e 20 20 54 68 65 20 4f 50 5f opcode. The OP_
1dac3 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 0a 2a 2a Column opcode.**
1dac4 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 63 75 72 is the only cur
1dac5 73 6f 72 20 6f 70 63 6f 64 65 20 74 68 61 74 20 sor opcode that
1dac6 77 6f 72 6b 73 20 77 69 74 68 20 61 20 70 73 65 works with a pse
1dac7 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a udo-table..**.**
1dac8 20 50 33 20 69 73 20 74 68 65 20 6e 75 6d 62 65 P3 is the numbe
1dac9 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 r of fields in t
1daca 68 65 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 he records that
1dacb 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 62 will be stored b
1dacc 79 0a 2a 2a 20 74 68 65 20 70 73 65 75 64 6f 2d y.** the pseudo-
1dacd 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f table..*/.case O
1dace 50 5f 4f 70 65 6e 50 73 65 75 64 6f 3a 20 7b 0a P_OpenPseudo: {.
1dacf 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1dad0 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1dad1 69 6e 74 6f 20 75 2e 61 79 20 2a 2f 0a 20 20 56 into u.ay */. V
1dad2 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a dbeCursor *pCx;.
1dad3 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1dad4 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1dad5 69 6e 74 6f 20 75 2e 61 79 20 2a 2f 0a 0a 20 20 into u.ay */..
1dad6 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1dad7 3d 30 20 29 3b 0a 20 20 75 2e 61 79 2e 70 43 78 =0 );. u.ay.pCx
1dad8 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f = allocateCurso
1dad9 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f r(p, pOp->p1, pO
1dada 70 2d 3e 70 33 2c 20 2d 31 2c 20 30 29 3b 0a 20 p->p3, -1, 0);.
1dadb 20 69 66 28 20 75 2e 61 79 2e 70 43 78 3d 3d 30 if( u.ay.pCx==0
1dadc 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1dadd 20 20 75 2e 61 79 2e 70 43 78 2d 3e 6e 75 6c 6c u.ay.pCx->null
1dade 52 6f 77 20 3d 20 31 3b 0a 20 20 75 2e 61 79 2e Row = 1;. u.ay.
1dadf 70 43 78 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 pCx->pseudoTable
1dae0 52 65 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 Reg = pOp->p2;.
1dae1 20 75 2e 61 79 2e 70 43 78 2d 3e 69 73 54 61 62 u.ay.pCx->isTab
1dae2 6c 65 20 3d 20 31 3b 0a 20 20 75 2e 61 79 2e 70 le = 1;. u.ay.p
1dae3 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20 30 3b Cx->isIndex = 0;
1dae4 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1dae5 4f 70 63 6f 64 65 3a 20 43 6c 6f 73 65 20 50 31 Opcode: Close P1
1dae6 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 * * * *.**.** C
1dae7 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 20 70 72 lose a cursor pr
1dae8 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 eviously opened
1dae9 61 73 20 50 31 2e 20 20 49 66 20 50 31 20 69 73 as P1. If P1 is
1daea 20 6e 6f 74 0a 2a 2a 20 63 75 72 72 65 6e 74 6c not.** currentl
1daeb 79 20 6f 70 65 6e 2c 20 74 68 69 73 20 69 6e 73 y open, this ins
1daec 74 72 75 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f truction is a no
1daed 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f -op..*/.case OP_
1daee 43 6c 6f 73 65 3a 20 7b 0a 20 20 61 73 73 65 72 Close: {. asser
1daef 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 t( pOp->p1>=0 &&
1daf0 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 pOp->p1<p->nCur
1daf1 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 sor );. sqlite3
1daf2 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 VdbeFreeCursor(p
1daf3 2c 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e , p->apCsr[pOp->
1daf4 70 31 5d 29 3b 0a 20 20 70 2d 3e 61 70 43 73 72 p1]);. p->apCsr
1daf5 5b 70 4f 70 2d 3e 70 31 5d 20 3d 20 30 3b 0a 20 [pOp->p1] = 0;.
1daf6 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1daf7 63 6f 64 65 3a 20 53 65 65 6b 47 65 20 50 31 20 code: SeekGe P1
1daf8 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a P2 P3 P4 *.**.**
1daf9 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 If cursor P1 re
1dafa 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 fers to an SQL t
1dafb 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 able (B-Tree tha
1dafc 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b t uses integer k
1dafd 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 eys), .** use th
1dafe 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1daff 74 65 72 20 50 33 20 61 73 20 74 68 65 20 6b 65 ter P3 as the ke
1db00 79 2e 20 20 49 66 20 63 75 72 73 6f 72 20 50 31 y. If cursor P1
1db01 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 refers .** to a
1db02 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 n SQL index, the
1db03 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 72 73 n P3 is the firs
1db04 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 t in an array of
1db05 20 50 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a P4 registers .*
1db06 2a 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20 * that are used
1db07 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 as an unpacked i
1db08 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a ndex key. .**.**
1db09 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 Reposition curs
1db0a 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 20 69 or P1 so that i
1db0b 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 t points to the
1db0c 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72 79 20 74 smallest entry t
1db0d 68 61 74 20 0a 2a 2a 20 69 73 20 67 72 65 61 74 hat .** is great
1db0e 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c er than or equal
1db0f 20 74 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 to the key valu
1db10 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20 e. If there are
1db11 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 67 no records .** g
1db12 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 reater than or e
1db13 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 qual to the key
1db14 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 and P2 is not ze
1db15 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f ro, then jump to
1db16 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 P2..**.** See a
1db17 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 lso: Found, NotF
1db18 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 ound, Distinct,
1db19 53 65 65 6b 4c 74 2c 20 53 65 65 6b 47 74 2c 20 SeekLt, SeekGt,
1db1a 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 SeekLe.*/./* Opc
1db1b 6f 64 65 3a 20 53 65 65 6b 47 74 20 50 31 20 50 ode: SeekGt P1 P
1db1c 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 2 P3 P4 *.**.**
1db1d 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 If cursor P1 ref
1db1e 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 ers to an SQL ta
1db1f 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 ble (B-Tree that
1db20 20 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 uses integer ke
1db21 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 ys), .** use the
1db22 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1db23 65 72 20 50 33 20 61 73 20 61 20 6b 65 79 2e 20 er P3 as a key.
1db24 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 If cursor P1 ref
1db25 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 ers .** to an SQ
1db26 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 L index, then P3
1db27 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69 6e is the first in
1db28 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50 34 20 an array of P4
1db29 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 registers .** th
1db2a 61 74 20 61 72 65 20 75 73 65 64 20 61 73 20 61 at are used as a
1db2b 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 n unpacked index
1db2c 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 key. .**.** Rep
1db2d 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 osition cursor P
1db2e 31 20 73 6f 20 74 68 61 74 20 20 69 74 20 70 6f 1 so that it po
1db2f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 6d 61 6c ints to the smal
1db30 6c 65 73 74 20 65 6e 74 72 79 20 74 68 61 74 20 lest entry that
1db31 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 74 .** is greater t
1db32 68 61 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 han the key valu
1db33 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20 e. If there are
1db34 6e 6f 20 72 65 63 6f 72 64 73 20 67 72 65 61 74 no records great
1db35 65 72 20 74 68 61 6e 20 0a 2a 2a 20 74 68 65 20 er than .** the
1db36 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f key and P2 is no
1db37 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d t zero, then jum
1db38 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 p to P2..**.** S
1db39 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 ee also: Found,
1db3a 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e NotFound, Distin
1db3b 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53 65 65 6b ct, SeekLt, Seek
1db3c 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a Ge, SeekLe.*/./*
1db3d 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 4c 74 20 Opcode: SeekLt
1db3e 50 31 20 50 32 20 50 33 20 50 34 20 2a 20 0a 2a P1 P2 P3 P4 * .*
1db3f 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 *.** If cursor P
1db40 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 1 refers to an S
1db41 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 QL table (B-Tree
1db42 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 that uses integ
1db43 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 er keys), .** us
1db44 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 e the value in r
1db45 65 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20 egister P3 as a
1db46 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50 key. If cursor P
1db47 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 1 refers .** to
1db48 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 an SQL index, th
1db49 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 72 en P3 is the fir
1db4a 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f st in an array o
1db4b 66 20 50 34 20 72 65 67 69 73 74 65 72 73 20 0a f P4 registers .
1db4c 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 65 64 ** that are used
1db4d 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 as an unpacked
1db4e 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a index key. .**.*
1db4f 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 * Reposition cur
1db50 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 20 sor P1 so that
1db51 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 it points to the
1db52 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20 74 largest entry t
1db53 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 73 73 20 hat .** is less
1db54 74 68 61 6e 20 74 68 65 20 6b 65 79 20 76 61 6c than the key val
1db55 75 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 ue. If there are
1db56 20 6e 6f 20 72 65 63 6f 72 64 73 20 6c 65 73 73 no records less
1db57 20 74 68 61 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 than .** the ke
1db58 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 y and P2 is not
1db59 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 zero, then jump
1db5a 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 to P2..**.** See
1db5b 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f also: Found, No
1db5c 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 tFound, Distinct
1db5d 2c 20 53 65 65 6b 47 74 2c 20 53 65 65 6b 47 65 , SeekGt, SeekGe
1db5e 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f , SeekLe.*/./* O
1db5f 70 63 6f 64 65 3a 20 53 65 65 6b 4c 65 20 50 31 pcode: SeekLe P1
1db60 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a P2 P3 P4 *.**.*
1db61 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 * If cursor P1 r
1db62 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 efers to an SQL
1db63 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68 table (B-Tree th
1db64 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 20 at uses integer
1db65 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 keys), .** use t
1db66 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1db67 73 74 65 72 20 50 33 20 61 73 20 61 20 6b 65 79 ster P3 as a key
1db68 2e 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 . If cursor P1 r
1db69 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 efers .** to an
1db6a 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 SQL index, then
1db6b 50 33 20 69 73 20 74 68 65 20 66 69 72 73 74 20 P3 is the first
1db6c 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50 in an array of P
1db6d 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 4 registers .**
1db6e 74 68 61 74 20 61 72 65 20 75 73 65 64 20 61 73 that are used as
1db6f 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 an unpacked ind
1db70 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 ex key. .**.** R
1db71 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 eposition cursor
1db72 20 50 31 20 73 6f 20 74 68 61 74 20 69 74 20 70 P1 so that it p
1db73 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c 61 72 oints to the lar
1db74 67 65 73 74 20 65 6e 74 72 79 20 74 68 61 74 20 gest entry that
1db75 0a 2a 2a 20 69 73 20 6c 65 73 73 20 74 68 61 6e .** is less than
1db76 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 or equal to the
1db77 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 key value. If t
1db78 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f here are no reco
1db79 72 64 73 20 0a 2a 2a 20 6c 65 73 73 20 74 68 61 rds .** less tha
1db7a 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 n or equal to th
1db7b 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 e key and P2 is
1db7c 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a not zero, then j
1db7d 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a ump to P2..**.**
1db7e 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 See also: Found
1db7f 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 , NotFound, Dist
1db80 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c 20 53 65 inct, SeekGt, Se
1db81 65 6b 47 65 2c 20 53 65 65 6b 4c 74 0a 2a 2f 0a ekGe, SeekLt.*/.
1db82 63 61 73 65 20 4f 50 5f 53 65 65 6b 4c 74 3a 20 case OP_SeekLt:
1db83 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c /* jump,
1db84 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f in3 */.case OP_
1db85 53 65 65 6b 4c 65 3a 20 20 20 20 20 20 20 20 20 SeekLe:
1db86 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a /* jump, in3 */.
1db87 63 61 73 65 20 4f 50 5f 53 65 65 6b 47 65 3a 20 case OP_SeekGe:
1db88 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c /* jump,
1db89 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f in3 */.case OP_
1db8a 53 65 65 6b 47 74 3a 20 7b 20 20 20 20 20 20 20 SeekGt: {
1db8b 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a /* jump, in3 */.
1db8c 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1db8d 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1db8e 69 6e 74 6f 20 75 2e 61 7a 20 2a 2f 0a 20 20 69 into u.az */. i
1db8f 6e 74 20 72 65 73 3b 0a 20 20 69 6e 74 20 6f 63 nt res;. int oc
1db90 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a ;. VdbeCursor *
1db91 70 43 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 pC;. UnpackedRe
1db92 63 6f 72 64 20 72 3b 0a 20 20 69 6e 74 20 6e 46 cord r;. int nF
1db93 69 65 6c 64 3b 0a 20 20 69 36 34 20 69 4b 65 79 ield;. i64 iKey
1db94 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f ; /* The ro
1db95 77 69 64 20 77 65 20 61 72 65 20 74 6f 20 73 65 wid we are to se
1db96 65 6b 20 74 6f 20 2a 2f 0a 23 65 6e 64 69 66 20 ek to */.#endif
1db97 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1db98 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1db99 61 7a 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 az */.. assert(
1db9a 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 pOp->p1>=0 && p
1db9b 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f Op->p1<p->nCurso
1db9c 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 r );. assert( p
1db9d 4f 70 2d 3e 70 32 21 3d 30 20 29 3b 0a 20 20 75 Op->p2!=0 );. u
1db9e 2e 61 7a 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 .az.pC = p->apCs
1db9f 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 r[pOp->p1];. as
1dba0 73 65 72 74 28 20 75 2e 61 7a 2e 70 43 21 3d 30 sert( u.az.pC!=0
1dba1 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e );. assert( u.
1dba2 61 7a 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 az.pC->pseudoTab
1dba3 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20 69 66 leReg==0 );. if
1dba4 28 20 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 ( u.az.pC->pCurs
1dba5 6f 72 21 3d 30 20 29 7b 0a 20 20 20 20 75 2e 61 or!=0 ){. u.a
1dba6 7a 2e 6f 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f z.oc = pOp->opco
1dba7 64 65 3b 0a 20 20 20 20 75 2e 61 7a 2e 70 43 2d de;. u.az.pC-
1dba8 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 >nullRow = 0;.
1dba9 20 20 69 66 28 20 75 2e 61 7a 2e 70 43 2d 3e 69 if( u.az.pC->i
1dbaa 73 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 sTable ){.
1dbab 2f 2a 20 54 68 65 20 69 6e 70 75 74 20 76 61 6c /* The input val
1dbac 75 65 20 69 6e 20 50 33 20 6d 69 67 68 74 20 62 ue in P3 might b
1dbad 65 20 6f 66 20 61 6e 79 20 74 79 70 65 3a 20 69 e of any type: i
1dbae 6e 74 65 67 65 72 2c 20 72 65 61 6c 2c 20 73 74 nteger, real, st
1dbaf 72 69 6e 67 2c 0a 20 20 20 20 20 20 2a 2a 20 62 ring,. ** b
1dbb0 6c 6f 62 2c 20 6f 72 20 4e 55 4c 4c 2e 20 20 42 lob, or NULL. B
1dbb1 75 74 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 ut it needs to b
1dbb2 65 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 66 e an integer bef
1dbb3 6f 72 65 20 77 65 20 63 61 6e 20 64 6f 0a 20 20 ore we can do.
1dbb4 20 20 20 20 2a 2a 20 74 68 65 20 73 65 65 6b 2c ** the seek,
1dbb5 20 73 6f 20 63 6f 76 65 72 74 20 69 74 2e 20 2a so covert it. *
1dbb6 2f 0a 20 20 20 20 20 20 61 70 70 6c 79 4e 75 6d /. applyNum
1dbb7 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e ericAffinity(pIn
1dbb8 33 29 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e 69 3);. u.az.i
1dbb9 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 Key = sqlite3Vdb
1dbba 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 33 29 3b eIntValue(pIn3);
1dbbb 0a 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e . u.az.pC->
1dbbc 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 rowidIsValid = 0
1dbbd 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 ;.. /* If t
1dbbe 68 65 20 50 33 20 76 61 6c 75 65 20 63 6f 75 6c he P3 value coul
1dbbf 64 20 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 d not be convert
1dbc0 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 ed into an integ
1dbc1 65 72 20 77 69 74 68 6f 75 74 0a 20 20 20 20 20 er without.
1dbc2 20 2a 2a 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f ** loss of info
1dbc3 72 6d 61 74 69 6f 6e 2c 20 74 68 65 6e 20 73 70 rmation, then sp
1dbc4 65 63 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 ecial processing
1dbc5 20 69 73 20 72 65 71 75 69 72 65 64 2e 2e 2e 20 is required...
1dbc6 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 28 70 49 */. if( (pI
1dbc7 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n3->flags & MEM_
1dbc8 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Int)==0 ){.
1dbc9 20 20 20 69 66 28 20 28 70 49 6e 33 2d 3e 66 6c if( (pIn3->fl
1dbca 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 3d ags & MEM_Real)=
1dbcb 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
1dbcc 2f 2a 20 49 66 20 74 68 65 20 50 33 20 76 61 6c /* If the P3 val
1dbcd 75 65 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e ue cannot be con
1dbce 76 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 79 20 verted into any
1dbcf 6b 69 6e 64 20 6f 66 20 61 20 6e 75 6d 62 65 72 kind of a number
1dbd0 2c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 ,. ** t
1dbd1 68 65 6e 20 74 68 65 20 73 65 65 6b 20 69 73 20 hen the seek is
1dbd2 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2c 20 73 6f not possible, so
1dbd3 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a 2f 0a 20 jump to P2 */.
1dbd4 20 20 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f pc = pO
1dbd5 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 p->p2 - 1;.
1dbd6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1dbd7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a }. /*
1dbd8 20 49 66 20 77 65 20 72 65 61 63 68 20 74 68 69 If we reach thi
1dbd9 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 s point, then th
1dbda 65 20 50 33 20 76 61 6c 75 65 20 6d 75 73 74 20 e P3 value must
1dbdb 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 0a 20 20 be a floating.
1dbdc 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 20 6e ** point n
1dbdd 75 6d 62 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 umber. */.
1dbde 20 20 61 73 73 65 72 74 28 20 28 70 49 6e 33 2d assert( (pIn3-
1dbdf 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 >flags & MEM_Rea
1dbe0 6c 29 21 3d 30 20 29 3b 0a 0a 20 20 20 20 20 20 l)!=0 );..
1dbe1 20 20 69 66 28 20 75 2e 61 7a 2e 69 4b 65 79 3d if( u.az.iKey=
1dbe2 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 =SMALLEST_INT64
1dbe3 26 26 20 28 70 49 6e 33 2d 3e 72 3c 28 64 6f 75 && (pIn3->r<(dou
1dbe4 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20 7c 7c ble)u.az.iKey ||
1dbe5 20 70 49 6e 33 2d 3e 72 3e 30 29 20 29 7b 0a 20 pIn3->r>0) ){.
1dbe6 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1dbe7 50 33 20 76 61 6c 75 65 20 69 73 20 74 6f 6f 20 P3 value is too
1dbe8 6c 61 72 67 65 20 69 6e 20 6d 61 67 6e 69 74 75 large in magnitu
1dbe9 64 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 de to be express
1dbea 65 64 20 61 73 20 61 6e 0a 20 20 20 20 20 20 20 ed as an.
1dbeb 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 2a ** integer. *
1dbec 2f 0a 20 20 20 20 20 20 20 20 20 20 75 2e 61 7a /. u.az
1dbed 2e 72 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 .res = 1;.
1dbee 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 3c if( pIn3->r<
1dbef 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
1dbf0 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 if( u.az.oc==OP
1dbf1 5f 53 65 65 6b 47 74 20 7c 7c 20 75 2e 61 7a 2e _SeekGt || u.az.
1dbf2 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29 7b oc==OP_SeekGe ){
1dbf3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 . r
1dbf4 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1dbf5 46 69 72 73 74 28 75 2e 61 7a 2e 70 43 2d 3e 70 First(u.az.pC->p
1dbf6 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 Cursor, &u.az.re
1dbf7 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 s);.
1dbf8 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1dbf9 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 _OK ) goto abort
1dbfa 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 _due_to_error;.
1dbfb 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1dbfc 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1dbfd 20 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e if( u.
1dbfe 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 az.oc==OP_SeekLt
1dbff 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f || u.az.oc==OP_
1dc00 53 65 65 6b 4c 65 20 29 7b 0a 20 20 20 20 20 20 SeekLe ){.
1dc01 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1dc02 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e ite3BtreeLast(u.
1dc03 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 az.pC->pCursor,
1dc04 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 &u.az.res);.
1dc05 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
1dc06 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
1dc07 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1dc08 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 error;.
1dc09 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d }. }
1dc0a 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 75 . if( u
1dc0b 2e 61 7a 2e 72 65 73 20 29 7b 0a 20 20 20 20 20 .az.res ){.
1dc0c 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d pc = pOp-
1dc0d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 >p2 - 1;.
1dc0e 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 }. b
1dc0f 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 reak;. }e
1dc10 6c 73 65 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d lse if( u.az.oc=
1dc11 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 75 2e =OP_SeekLt || u.
1dc12 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 az.oc==OP_SeekGe
1dc13 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a ){. /*
1dc14 20 55 73 65 20 74 68 65 20 63 65 69 6c 69 6e 67 Use the ceiling
1dc15 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 () function to c
1dc16 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69 6e 74 onvert real->int
1dc17 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 */. if
1dc18 28 20 70 49 6e 33 2d 3e 72 20 3e 20 28 64 6f 75 ( pIn3->r > (dou
1dc19 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20 29 20 ble)u.az.iKey )
1dc1a 75 2e 61 7a 2e 69 4b 65 79 2b 2b 3b 0a 20 20 20 u.az.iKey++;.
1dc1b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1dc1c 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 /* Use the
1dc1d 20 66 6c 6f 6f 72 28 29 20 66 75 6e 63 74 69 6f floor() functio
1dc1e 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 72 65 61 n to convert rea
1dc1f 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 l->int */.
1dc20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a assert( u.az
1dc21 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 7c .oc==OP_SeekLe |
1dc22 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 | u.az.oc==OP_Se
1dc23 65 6b 47 74 20 29 3b 0a 20 20 20 20 20 20 20 20 ekGt );.
1dc24 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 3c 20 if( pIn3->r <
1dc25 28 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 (double)u.az.iKe
1dc26 79 20 29 20 75 2e 61 7a 2e 69 4b 65 79 2d 2d 3b y ) u.az.iKey--;
1dc27 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1dc28 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 }. rc = sq
1dc29 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f lite3BtreeMoveto
1dc2a 55 6e 70 61 63 6b 65 64 28 75 2e 61 7a 2e 70 43 Unpacked(u.az.pC
1dc2b 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 75 ->pCursor, 0, (u
1dc2c 36 34 29 75 2e 61 7a 2e 69 4b 65 79 2c 20 30 2c 64)u.az.iKey, 0,
1dc2d 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 &u.az.res);.
1dc2e 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1dc2f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1dc30 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1dc31 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d o_error;. }
1dc32 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e . if( u.az.
1dc33 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 res==0 ){.
1dc34 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 u.az.pC->rowid
1dc35 49 73 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20 IsValid = 1;.
1dc36 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 6c 61 u.az.pC->la
1dc37 73 74 52 6f 77 69 64 20 3d 20 75 2e 61 7a 2e 69 stRowid = u.az.i
1dc38 4b 65 79 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 Key;. }.
1dc39 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e }else{. u.
1dc3a 61 7a 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d az.nField = pOp-
1dc3b 3e 70 34 2e 69 3b 0a 20 20 20 20 20 20 61 73 73 >p4.i;. ass
1dc3c 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 ert( pOp->p4type
1dc3d 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 ==P4_INT32 );.
1dc3e 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a assert( u.az
1dc3f 2e 6e 46 69 65 6c 64 3e 30 20 29 3b 0a 20 20 20 .nField>0 );.
1dc40 20 20 20 75 2e 61 7a 2e 72 2e 70 4b 65 79 49 6e u.az.r.pKeyIn
1dc41 66 6f 20 3d 20 75 2e 61 7a 2e 70 43 2d 3e 70 4b fo = u.az.pC->pK
1dc42 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 75 2e eyInfo;. u.
1dc43 61 7a 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 az.r.nField = (u
1dc44 31 36 29 75 2e 61 7a 2e 6e 46 69 65 6c 64 3b 0a 16)u.az.nField;.
1dc45 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f if( u.az.o
1dc46 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20 c==OP_SeekGt ||
1dc47 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b u.az.oc==OP_Seek
1dc48 4c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e Le ){. u.
1dc49 61 7a 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 az.r.flags = UNP
1dc4a 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 3b 0a 20 ACKED_INCRKEY;.
1dc4b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1dc4c 20 20 20 20 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 u.az.r.flags
1dc4d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 0;. }.
1dc4e 20 20 20 20 75 2e 61 7a 2e 72 2e 61 4d 65 6d 20 u.az.r.aMem
1dc4f 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
1dc50 70 33 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 p3];. rc =
1dc51 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 sqlite3BtreeMove
1dc52 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 61 7a 2e toUnpacked(u.az.
1dc53 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e pC->pCursor, &u.
1dc54 61 7a 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e 61 az.r, 0, 0, &u.a
1dc55 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 z.res);. if
1dc56 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1dc57 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 ){. goto
1dc58 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 abort_due_to_err
1dc59 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 or;. }.
1dc5a 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 u.az.pC->rowid
1dc5b 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 IsValid = 0;.
1dc5c 20 7d 0a 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e }. u.az.pC->
1dc5d 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d deferredMoveto =
1dc5e 20 30 3b 0a 20 20 20 20 75 2e 61 7a 2e 70 43 2d 0;. u.az.pC-
1dc5f 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 >cacheStatus = C
1dc60 41 43 48 45 5f 53 54 41 4c 45 3b 0a 23 69 66 64 ACHE_STALE;.#ifd
1dc61 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 ef SQLITE_TEST.
1dc62 20 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 sqlite3_searc
1dc63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 h_count++;.#endi
1dc64 66 0a 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f f. if( u.az.o
1dc65 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20 c==OP_SeekGe ||
1dc66 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b u.az.oc==OP_Seek
1dc67 47 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 Gt ){. if(
1dc68 75 2e 61 7a 2e 72 65 73 3c 30 20 7c 7c 20 28 75 u.az.res<0 || (u
1dc69 2e 61 7a 2e 72 65 73 3d 3d 30 20 26 26 20 75 2e .az.res==0 && u.
1dc6a 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 az.oc==OP_SeekGt
1dc6b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 ) ){. rc
1dc6c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 = sqlite3BtreeNe
1dc6d 78 74 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 xt(u.az.pC->pCur
1dc6e 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b sor, &u.az.res);
1dc6f 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
1dc70 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 =SQLITE_OK ) got
1dc71 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1dc72 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 75 2e rror;. u.
1dc73 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 az.pC->rowidIsVa
1dc74 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d lid = 0;. }
1dc75 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e else{. u.
1dc76 61 7a 2e 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 az.res = 0;.
1dc77 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1dc78 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 assert( u.a
1dc79 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 z.oc==OP_SeekLt
1dc7a 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 || u.az.oc==OP_S
1dc7b 65 65 6b 4c 65 20 29 3b 0a 20 20 20 20 20 20 69 eekLe );. i
1dc7c 66 28 20 75 2e 61 7a 2e 72 65 73 3e 30 20 7c 7c f( u.az.res>0 ||
1dc7d 20 28 75 2e 61 7a 2e 72 65 73 3d 3d 30 20 26 26 (u.az.res==0 &&
1dc7e 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 u.az.oc==OP_See
1dc7f 6b 4c 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 kLt) ){.
1dc80 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1dc81 65 50 72 65 76 69 6f 75 73 28 75 2e 61 7a 2e 70 ePrevious(u.az.p
1dc82 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 C->pCursor, &u.a
1dc83 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 z.res);.
1dc84 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1dc85 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 K ) goto abort_d
1dc86 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
1dc87 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f u.az.pC->ro
1dc88 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a widIsValid = 0;.
1dc89 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1dc8a 20 20 20 20 20 2f 2a 20 75 2e 61 7a 2e 72 65 73 /* u.az.res
1dc8b 20 6d 69 67 68 74 20 62 65 20 6e 65 67 61 74 69 might be negati
1dc8c 76 65 20 62 65 63 61 75 73 65 20 74 68 65 20 74 ve because the t
1dc8d 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 20 20 able is empty.
1dc8e 43 68 65 63 6b 20 74 6f 0a 20 20 20 20 20 20 20 Check to.
1dc8f 20 2a 2a 20 73 65 65 20 69 66 20 74 68 69 73 20 ** see if this
1dc90 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20 20 is the case..
1dc91 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
1dc92 75 2e 61 7a 2e 72 65 73 20 3d 20 73 71 6c 69 74 u.az.res = sqlit
1dc93 65 33 42 74 72 65 65 45 6f 66 28 75 2e 61 7a 2e e3BtreeEof(u.az.
1dc94 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 pC->pCursor);.
1dc95 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1dc96 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e assert( pOp->p2>
1dc97 30 20 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 0 );. if( u.a
1dc98 7a 2e 72 65 73 20 29 7b 0a 20 20 20 20 20 20 70 z.res ){. p
1dc99 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1dc9a 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
1dc9b 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 /* This happ
1dc9c 65 6e 73 20 77 68 65 6e 20 61 74 74 65 6d 70 74 ens when attempt
1dc9d 69 6e 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 ing to open the
1dc9e 73 71 6c 69 74 65 33 5f 6d 61 73 74 65 72 20 74 sqlite3_master t
1dc9f 61 62 6c 65 0a 20 20 20 20 2a 2a 20 66 6f 72 20 able. ** for
1dca0 72 65 61 64 20 61 63 63 65 73 73 20 72 65 74 75 read access retu
1dca1 72 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 rns SQLITE_EMPTY
1dca2 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 . In this case a
1dca3 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 74 61 6b lways. ** tak
1dca4 65 20 74 68 65 20 6a 75 6d 70 20 28 73 69 6e 63 e the jump (sinc
1dca5 65 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 e there are no r
1dca6 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20 74 61 ecords in the ta
1dca7 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ble).. */.
1dca8 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1dca9 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 1;. }. break;.
1dcaa 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 }../* Opcode: Se
1dcab 65 6b 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ek P1 P2 * * *.*
1dcac 2a 0a 2a 2a 20 50 31 20 69 73 20 61 6e 20 6f 70 *.** P1 is an op
1dcad 65 6e 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20 en table cursor
1dcae 61 6e 64 20 50 32 20 69 73 20 61 20 72 6f 77 69 and P2 is a rowi
1dcaf 64 20 69 6e 74 65 67 65 72 2e 20 20 41 72 72 61 d integer. Arra
1dcb0 6e 67 65 0a 2a 2a 20 66 6f 72 20 50 31 20 74 6f nge.** for P1 to
1dcb1 20 6d 6f 76 65 20 73 6f 20 74 68 61 74 20 69 74 move so that it
1dcb2 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 72 points to the r
1dcb3 6f 77 69 64 20 67 69 76 65 6e 20 62 79 20 50 32 owid given by P2
1dcb4 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ..**.** This is
1dcb5 61 63 74 75 61 6c 6c 79 20 61 20 64 65 66 65 72 actually a defer
1dcb6 72 65 64 20 73 65 65 6b 2e 20 20 4e 6f 74 68 69 red seek. Nothi
1dcb7 6e 67 20 61 63 74 75 61 6c 6c 79 20 68 61 70 70 ng actually happ
1dcb8 65 6e 73 20 75 6e 74 69 6c 0a 2a 2a 20 74 68 65 ens until.** the
1dcb9 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 20 cursor is used
1dcba 74 6f 20 72 65 61 64 20 61 20 72 65 63 6f 72 64 to read a record
1dcbb 2e 20 20 54 68 61 74 20 77 61 79 2c 20 69 66 20 . That way, if
1dcbc 6e 6f 20 72 65 61 64 73 0a 2a 2a 20 6f 63 63 75 no reads.** occu
1dcbd 72 2c 20 6e 6f 20 75 6e 6e 65 63 65 73 73 61 72 r, no unnecessar
1dcbe 79 20 49 2f 4f 20 68 61 70 70 65 6e 73 2e 0a 2a y I/O happens..*
1dcbf 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 3a 20 /.case OP_Seek:
1dcc0 7b 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 { /* in2 */.#
1dcc1 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1dcc2 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1dcc3 6e 74 6f 20 75 2e 62 61 20 2a 2f 0a 20 20 56 64 nto u.ba */. Vd
1dcc4 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 23 65 beCursor *pC;.#e
1dcc5 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1dcc6 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1dcc7 74 6f 20 75 2e 62 61 20 2a 2f 0a 0a 20 20 61 73 to u.ba */.. as
1dcc8 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1dcc9 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e && pOp->p1<p->n
1dcca 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 61 Cursor );. u.ba
1dccb 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 .pC = p->apCsr[p
1dccc 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1dccd 74 28 20 75 2e 62 61 2e 70 43 21 3d 30 20 29 3b t( u.ba.pC!=0 );
1dcce 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e . if( ALWAYS(u.
1dccf 62 61 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d ba.pC->pCursor!=
1dcd0 30 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 0) ){. assert
1dcd1 28 20 75 2e 62 61 2e 70 43 2d 3e 69 73 54 61 62 ( u.ba.pC->isTab
1dcd2 6c 65 20 29 3b 0a 20 20 20 20 75 2e 62 61 2e 70 le );. u.ba.p
1dcd3 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a C->nullRow = 0;.
1dcd4 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e 6d 6f 76 u.ba.pC->mov
1dcd5 65 74 6f 54 61 72 67 65 74 20 3d 20 73 71 6c 69 etoTarget = sqli
1dcd6 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 te3VdbeIntValue(
1dcd7 70 49 6e 32 29 3b 0a 20 20 20 20 75 2e 62 61 2e pIn2);. u.ba.
1dcd8 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 pC->rowidIsValid
1dcd9 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 61 2e 70 = 0;. u.ba.p
1dcda 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1dcdb 6f 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 o = 1;. }. bre
1dcdc 61 6b 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20 4f 70 63 ak;.}. ../* Opc
1dcdd 6f 64 65 3a 20 46 6f 75 6e 64 20 50 31 20 50 32 ode: Found P1 P2
1dcde 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 P3 * *.**.** Re
1dcdf 67 69 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20 gister P3 holds
1dce0 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 a blob construct
1dce1 65 64 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 ed by MakeRecord
1dce2 2e 20 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 . P1 is an inde
1dce3 78 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 6e 74 72 x..** If an entr
1dce4 79 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 y that matches t
1dce5 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1dce6 73 74 65 72 20 70 33 20 65 78 69 73 74 73 20 69 ster p3 exists i
1dce7 6e 20 50 31 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d n P1 then.** jum
1dce8 70 20 74 6f 20 50 32 2e 20 20 49 66 20 74 68 65 p to P2. If the
1dce9 20 50 33 20 76 61 6c 75 65 20 64 6f 65 73 20 6e P3 value does n
1dcea 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 65 6e 74 ot match any ent
1dceb 72 79 20 69 6e 20 50 31 0a 2a 2a 20 74 68 65 6e ry in P1.** then
1dcec 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 54 68 65 fall thru. The
1dced 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 6c 65 P1 cursor is le
1dcee 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 ft pointing at t
1dcef 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72 he matching entr
1dcf0 79 0a 2a 2a 20 69 66 20 69 74 20 65 78 69 73 74 y.** if it exist
1dcf1 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e s..**.** This in
1dcf2 73 74 72 75 63 74 69 6f 6e 20 69 73 20 75 73 65 struction is use
1dcf3 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 d to implement t
1dcf4 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 he IN operator w
1dcf5 68 65 72 65 20 74 68 65 0a 2a 2a 20 6c 65 66 74 here the.** left
1dcf6 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20 61 20 -hand side is a
1dcf7 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
1dcf8 2e 20 20 50 31 20 6d 61 79 20 62 65 20 61 20 74 . P1 may be a t
1dcf9 72 75 65 20 69 6e 64 65 78 2c 20 6f 72 20 69 74 rue index, or it
1dcfa 0a 2a 2a 20 6d 61 79 20 62 65 20 61 20 74 65 6d .** may be a tem
1dcfb 70 6f 72 61 72 79 20 69 6e 64 65 78 20 74 68 61 porary index tha
1dcfc 74 20 68 6f 6c 64 73 20 74 68 65 20 72 65 73 75 t holds the resu
1dcfd 6c 74 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43 lts of the SELEC
1dcfe 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 T.** statement.
1dcff 20 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 This instructi
1dd00 6f 6e 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 on is also used
1dd01 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
1dd02 0a 2a 2a 20 44 49 53 54 49 4e 43 54 20 6b 65 79 .** DISTINCT key
1dd03 77 6f 72 64 20 69 6e 20 53 45 4c 45 43 54 20 73 word in SELECT s
1dd04 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a tatements..**.**
1dd05 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f This instructio
1dd06 6e 20 63 68 65 63 6b 73 20 69 66 20 69 6e 64 65 n checks if inde
1dd07 78 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 61 20 x P1 contains a
1dd08 72 65 63 6f 72 64 20 66 6f 72 20 77 68 69 63 68 record for which
1dd09 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 4e .** the first N
1dd0a 20 73 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 serialized valu
1dd0b 65 73 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 es exactly match
1dd0c 20 74 68 65 20 4e 20 73 65 72 69 61 6c 69 7a 65 the N serialize
1dd0d 64 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e 20 74 d values.** in t
1dd0e 68 65 20 72 65 63 6f 72 64 20 69 6e 20 72 65 67 he record in reg
1dd0f 69 73 74 65 72 20 50 33 2c 20 77 68 65 72 65 20 ister P3, where
1dd10 4e 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e N is the total n
1dd11 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 umber of values
1dd12 69 6e 0a 2a 2a 20 74 68 65 20 50 33 20 72 65 63 in.** the P3 rec
1dd13 6f 72 64 20 28 74 68 65 20 50 33 20 72 65 63 6f ord (the P3 reco
1dd14 72 64 20 69 73 20 61 20 70 72 65 66 69 78 20 6f rd is a prefix o
1dd15 66 20 74 68 65 20 50 31 20 72 65 63 6f 72 64 29 f the P1 record)
1dd16 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 . .**.** See als
1dd17 6f 3a 20 4e 6f 74 46 6f 75 6e 64 2c 20 49 73 55 o: NotFound, IsU
1dd18 6e 69 71 75 65 2c 20 4e 6f 74 45 78 69 73 74 73 nique, NotExists
1dd19 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e .*/./* Opcode: N
1dd1a 6f 74 46 6f 75 6e 64 20 50 31 20 50 32 20 50 33 otFound P1 P2 P3
1dd1b 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 * *.**.** Regis
1dd1c 74 65 72 20 50 33 20 68 6f 6c 64 73 20 61 20 62 ter P3 holds a b
1dd1d 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20 lob constructed
1dd1e 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 20 by MakeRecord.
1dd1f 50 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 65 P1 is.** an inde
1dd20 78 2e 20 20 49 66 20 6e 6f 20 65 6e 74 72 79 20 x. If no entry
1dd21 65 78 69 73 74 73 20 69 6e 20 50 31 20 74 68 61 exists in P1 tha
1dd22 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 62 6c t matches the bl
1dd23 6f 62 20 74 68 65 6e 20 6a 75 6d 70 0a 2a 2a 20 ob then jump.**
1dd24 74 6f 20 50 32 2e 20 20 49 66 20 61 6e 20 65 6e to P2. If an en
1dd25 74 72 79 20 64 6f 65 73 20 65 78 69 73 74 69 6e try does existin
1dd26 67 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 2e g, fall through.
1dd27 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 The cursor is
1dd28 6c 65 66 74 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 left.** pointing
1dd29 20 74 6f 20 74 68 65 20 65 6e 74 72 79 20 74 68 to the entry th
1dd2a 61 74 20 6d 61 74 63 68 65 73 2e 0a 2a 2a 0a 2a at matches..**.*
1dd2b 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e * See also: Foun
1dd2c 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 49 73 d, NotExists, Is
1dd2d 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 65 20 4f Unique.*/.case O
1dd2e 50 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20 20 20 20 P_NotFound:
1dd2f 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a /* jump, in3 *
1dd30 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 75 6e 64 3a /.case OP_Found:
1dd31 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1dd32 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 p, in3 */.#if 0
1dd33 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1dd34 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1dd35 2e 62 62 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 72 .bb */. int alr
1dd36 65 61 64 79 45 78 69 73 74 73 3b 0a 20 20 56 64 eadyExists;. Vd
1dd37 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
1dd38 69 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 int res;. Unpac
1dd39 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b kedRecord *pIdxK
1dd3a 65 79 3b 0a 20 20 63 68 61 72 20 61 54 65 6d 70 ey;. char aTemp
1dd3b 52 65 63 5b 52 4f 55 4e 44 38 28 73 69 7a 65 6f Rec[ROUND8(sizeo
1dd3c 66 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 f(UnpackedRecord
1dd3d 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29 )) + sizeof(Mem)
1dd3e 2a 33 20 2b 20 37 5d 3b 0a 23 65 6e 64 69 66 20 *3 + 7];.#endif
1dd3f 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1dd40 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1dd41 62 62 20 2a 2f 0a 0a 20 20 75 2e 62 62 2e 61 6c bb */.. u.bb.al
1dd42 72 65 61 64 79 45 78 69 73 74 73 20 3d 20 30 3b readyExists = 0;
1dd43 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1dd44 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1dd45 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1dd46 20 75 2e 62 62 2e 70 43 20 3d 20 70 2d 3e 61 70 u.bb.pC = p->ap
1dd47 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1dd48 61 73 73 65 72 74 28 20 75 2e 62 62 2e 70 43 21 assert( u.bb.pC!
1dd49 3d 30 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 =0 );. if( ALWA
1dd4a 59 53 28 75 2e 62 62 2e 70 43 2d 3e 70 43 75 72 YS(u.bb.pC->pCur
1dd4b 73 6f 72 21 3d 30 29 20 29 7b 0a 0a 20 20 20 20 sor!=0) ){..
1dd4c 61 73 73 65 72 74 28 20 75 2e 62 62 2e 70 43 2d assert( u.bb.pC-
1dd4d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 >isTable==0 );.
1dd4e 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d assert( pIn3-
1dd4f 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f >flags & MEM_Blo
1dd50 62 20 29 3b 0a 20 20 20 20 45 78 70 61 6e 64 42 b );. ExpandB
1dd51 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20 20 20 75 lob(pIn3);. u
1dd52 2e 62 62 2e 70 49 64 78 4b 65 79 20 3d 20 73 71 .bb.pIdxKey = sq
1dd53 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 lite3VdbeRecordU
1dd54 6e 70 61 63 6b 28 75 2e 62 62 2e 70 43 2d 3e 70 npack(u.bb.pC->p
1dd55 4b 65 79 49 6e 66 6f 2c 20 70 49 6e 33 2d 3e 6e KeyInfo, pIn3->n
1dd56 2c 20 70 49 6e 33 2d 3e 7a 2c 0a 20 20 20 20 20 , pIn3->z,.
1dd57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dd58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dd59 20 75 2e 62 62 2e 61 54 65 6d 70 52 65 63 2c 20 u.bb.aTempRec,
1dd5a 73 69 7a 65 6f 66 28 75 2e 62 62 2e 61 54 65 6d sizeof(u.bb.aTem
1dd5b 70 52 65 63 29 29 3b 0a 20 20 20 20 69 66 28 20 pRec));. if(
1dd5c 75 2e 62 62 2e 70 49 64 78 4b 65 79 3d 3d 30 20 u.bb.pIdxKey==0
1dd5d 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f ){. goto no
1dd5e 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _mem;. }.
1dd5f 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d if( pOp->opcode=
1dd60 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 20 =OP_Found ){.
1dd61 20 20 20 75 2e 62 62 2e 70 49 64 78 4b 65 79 2d u.bb.pIdxKey-
1dd62 3e 66 6c 61 67 73 20 7c 3d 20 55 4e 50 41 43 4b >flags |= UNPACK
1dd63 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 3b ED_PREFIX_MATCH;
1dd64 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
1dd65 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 sqlite3BtreeMove
1dd66 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 62 2e toUnpacked(u.bb.
1dd67 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 75 2e 62 pC->pCursor, u.b
1dd68 62 2e 70 49 64 78 4b 65 79 2c 20 30 2c 20 30 2c b.pIdxKey, 0, 0,
1dd69 20 26 75 2e 62 62 2e 72 65 73 29 3b 0a 20 20 20 &u.bb.res);.
1dd6a 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 sqlite3VdbeDele
1dd6b 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 teUnpackedRecord
1dd6c 28 75 2e 62 62 2e 70 49 64 78 4b 65 79 29 3b 0a (u.bb.pIdxKey);.
1dd6d 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1dd6e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 62 TE_OK ){. b
1dd6f 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1dd70 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73 u.bb.alreadyExis
1dd71 74 73 20 3d 20 28 75 2e 62 62 2e 72 65 73 3d 3d ts = (u.bb.res==
1dd72 30 29 3b 0a 20 20 20 20 75 2e 62 62 2e 70 43 2d 0);. u.bb.pC-
1dd73 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 >deferredMoveto
1dd74 3d 20 30 3b 0a 20 20 20 20 75 2e 62 62 2e 70 43 = 0;. u.bb.pC
1dd75 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1dd76 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d CACHE_STALE;. }
1dd77 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f . if( pOp->opco
1dd78 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a de==OP_Found ){.
1dd79 20 20 20 20 69 66 28 20 75 2e 62 62 2e 61 6c 72 if( u.bb.alr
1dd7a 65 61 64 79 45 78 69 73 74 73 20 29 20 70 63 20 eadyExists ) pc
1dd7b 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1dd7c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 }else{. if(
1dd7d 21 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 !u.bb.alreadyExi
1dd7e 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e sts ) pc = pOp->
1dd7f 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 p2 - 1;. }. br
1dd80 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1dd81 65 3a 20 49 73 55 6e 69 71 75 65 20 50 31 20 50 e: IsUnique P1 P
1dd82 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 2 P3 P4 *.**.**
1dd83 43 75 72 73 6f 72 20 50 31 20 69 73 20 6f 70 65 Cursor P1 is ope
1dd84 6e 20 6f 6e 20 61 6e 20 69 6e 64 65 78 2e 20 20 n on an index.
1dd85 53 6f 20 69 74 20 68 61 73 20 6e 6f 20 64 61 74 So it has no dat
1dd86 61 20 61 6e 64 20 69 74 73 20 6b 65 79 20 63 6f a and its key co
1dd87 6e 73 69 73 74 73 20 0a 2a 2a 20 6f 66 20 61 20 nsists .** of a
1dd88 72 65 63 6f 72 64 20 67 65 6e 65 72 61 74 65 64 record generated
1dd89 20 62 79 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 by OP_MakeRecor
1dd8a 64 20 77 68 65 72 65 20 74 68 65 20 6c 61 73 74 d where the last
1dd8b 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 0a 2a field is the .*
1dd8c 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 65 * rowid of the e
1dd8d 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 69 6e ntry that the in
1dd8e 64 65 78 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a dex refers to..*
1dd8f 2a 0a 2a 2a 20 54 68 65 20 50 33 20 72 65 67 69 *.** The P3 regi
1dd90 73 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 61 6e ster contains an
1dd91 20 69 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 integer record
1dd92 6e 75 6d 62 65 72 2e 20 43 61 6c 6c 20 74 68 69 number. Call thi
1dd93 73 20 72 65 63 6f 72 64 20 0a 2a 2a 20 6e 75 6d s record .** num
1dd94 62 65 72 20 52 2e 20 52 65 67 69 73 74 65 72 20 ber R. Register
1dd95 50 34 20 69 73 20 74 68 65 20 66 69 72 73 74 20 P4 is the first
1dd96 69 6e 20 61 20 73 65 74 20 6f 66 20 4e 20 63 6f in a set of N co
1dd97 6e 74 69 67 75 6f 75 73 20 72 65 67 69 73 74 65 ntiguous registe
1dd98 72 73 0a 2a 2a 20 74 68 61 74 20 6d 61 6b 65 20 rs.** that make
1dd99 75 70 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 up an unpacked i
1dd9a 6e 64 65 78 20 6b 65 79 20 74 68 61 74 20 63 61 ndex key that ca
1dd9b 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 63 n be used with c
1dd9c 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65 ursor P1..** The
1dd9d 20 76 61 6c 75 65 20 6f 66 20 4e 20 63 61 6e 20 value of N can
1dd9e 62 65 20 69 6e 66 65 72 72 65 64 20 66 72 6f 6d be inferred from
1dd9f 20 74 68 65 20 63 75 72 73 6f 72 2e 20 4e 20 69 the cursor. N i
1dda0 6e 63 6c 75 64 65 73 20 74 68 65 20 72 6f 77 69 ncludes the rowi
1dda1 64 0a 2a 2a 20 76 61 6c 75 65 20 61 70 70 65 6e d.** value appen
1dda2 64 65 64 20 74 6f 20 74 68 65 20 65 6e 64 20 6f ded to the end o
1dda3 66 20 74 68 65 20 69 6e 64 65 78 20 72 65 63 6f f the index reco
1dda4 72 64 2e 20 54 68 69 73 20 72 6f 77 69 64 20 76 rd. This rowid v
1dda5 61 6c 75 65 20 6d 61 79 0a 2a 2a 20 6f 72 20 6d alue may.** or m
1dda6 61 79 20 6e 6f 74 20 62 65 20 74 68 65 20 73 61 ay not be the sa
1dda7 6d 65 20 61 73 20 52 2e 0a 2a 2a 0a 2a 2a 20 49 me as R..**.** I
1dda8 66 20 61 6e 79 20 6f 66 20 74 68 65 20 4e 20 72 f any of the N r
1dda9 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 egisters beginni
1ddaa 6e 67 20 77 69 74 68 20 72 65 67 69 73 74 65 72 ng with register
1ddab 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 20 4e P4 contains a N
1ddac 55 4c 4c 0a 2a 2a 20 76 61 6c 75 65 2c 20 6a 75 ULL.** value, ju
1ddad 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 mp immediately t
1ddae 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 o P2..**.** Othe
1ddaf 72 77 69 73 65 2c 20 74 68 69 73 20 69 6e 73 74 rwise, this inst
1ddb0 72 75 63 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 ruction checks i
1ddb1 66 20 63 75 72 73 6f 72 20 50 31 20 63 6f 6e 74 f cursor P1 cont
1ddb2 61 69 6e 73 20 61 6e 20 65 6e 74 72 79 0a 2a 2a ains an entry.**
1ddb3 20 77 68 65 72 65 20 74 68 65 20 66 69 72 73 74 where the first
1ddb4 20 28 4e 2d 31 29 20 66 69 65 6c 64 73 20 6d 61 (N-1) fields ma
1ddb5 74 63 68 20 62 75 74 20 74 68 65 20 72 6f 77 69 tch but the rowi
1ddb6 64 20 76 61 6c 75 65 20 61 74 20 74 68 65 20 65 d value at the e
1ddb7 6e 64 0a 2a 2a 20 6f 66 20 74 68 65 20 69 6e 64 nd.** of the ind
1ddb8 65 78 20 65 6e 74 72 79 20 69 73 20 6e 6f 74 20 ex entry is not
1ddb9 52 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e R. If there is n
1ddba 6f 20 73 75 63 68 20 65 6e 74 72 79 2c 20 63 6f o such entry, co
1ddbb 6e 74 72 6f 6c 20 6a 75 6d 70 73 0a 2a 2a 20 74 ntrol jumps.** t
1ddbc 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 o instruction P2
1ddbd 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 . Otherwise, the
1ddbe 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 63 6f rowid of the co
1ddbf 6e 66 6c 69 63 74 69 6e 67 20 69 6e 64 65 78 0a nflicting index.
1ddc0 2a 2a 20 65 6e 74 72 79 20 69 73 20 63 6f 70 69 ** entry is copi
1ddc1 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 50 ed to register P
1ddc2 33 20 61 6e 64 20 63 6f 6e 74 72 6f 6c 20 66 61 3 and control fa
1ddc3 6c 6c 73 20 74 68 72 6f 75 67 68 20 74 6f 20 74 lls through to t
1ddc4 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 73 74 72 he next.** instr
1ddc5 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 uction..**.** Se
1ddc6 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f 75 6e 64 e also: NotFound
1ddc7 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 46 6f 75 , NotExists, Fou
1ddc8 6e 64 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 73 nd.*/.case OP_Is
1ddc9 55 6e 69 71 75 65 3a 20 7b 20 20 20 20 20 20 20 Unique: {
1ddca 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f /* jump, in3 */
1ddcb 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1ddcc 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ddcd 20 69 6e 74 6f 20 75 2e 62 63 20 2a 2f 0a 20 20 into u.bc */.
1ddce 75 31 36 20 69 69 3b 0a 20 20 56 64 62 65 43 75 u16 ii;. VdbeCu
1ddcf 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 42 74 43 rsor *pCx;. BtC
1ddd0 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 ursor *pCrsr;.
1ddd1 75 31 36 20 6e 46 69 65 6c 64 3b 0a 20 20 4d 65 u16 nField;. Me
1ddd2 6d 20 2a 61 4d 65 6d 3b 0a 20 20 55 6e 70 61 63 m *aMem;. Unpac
1ddd3 6b 65 64 52 65 63 6f 72 64 20 72 3b 20 20 20 20 kedRecord r;
1ddd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ddd5 20 42 2d 54 72 65 65 20 69 6e 64 65 78 20 73 65 B-Tree index se
1ddd6 61 72 63 68 20 6b 65 79 20 2a 2f 0a 20 20 69 36 arch key */. i6
1ddd7 34 20 52 3b 20 20 20 20 20 20 20 20 20 20 20 20 4 R;
1ddd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ddd9 20 2f 2a 20 52 6f 77 69 64 20 73 74 6f 72 65 64 /* Rowid stored
1ddda 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 in register P3
1dddb 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 */.#endif /* loc
1dddc 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1dddd 65 64 20 69 6e 74 6f 20 75 2e 62 63 20 2a 2f 0a ed into u.bc */.
1ddde 0a 20 20 75 2e 62 63 2e 61 4d 65 6d 20 3d 20 26 . u.bc.aMem = &
1dddf 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 34 2e p->aMem[pOp->p4.
1dde0 69 5d 3b 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 i];. /* Assert
1dde1 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 73 20 that the values
1dde2 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 50 31 of parameters P1
1dde3 20 61 6e 64 20 50 34 20 61 72 65 20 69 6e 20 72 and P4 are in r
1dde4 61 6e 67 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 ange. */. asser
1dde5 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d t( pOp->p4type==
1dde6 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 61 73 P4_INT32 );. as
1dde7 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 69 3e sert( pOp->p4.i>
1dde8 30 20 26 26 20 70 4f 70 2d 3e 70 34 2e 69 3c 3d 0 && pOp->p4.i<=
1dde9 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 p->nMem );. ass
1ddea 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1ddeb 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 && pOp->p1<p->nC
1ddec 75 72 73 6f 72 20 29 3b 0a 0a 20 20 2f 2a 20 46 ursor );.. /* F
1dded 69 6e 64 20 74 68 65 20 69 6e 64 65 78 20 63 75 ind the index cu
1ddee 72 73 6f 72 2e 20 2a 2f 0a 20 20 75 2e 62 63 2e rsor. */. u.bc.
1ddef 70 43 78 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 pCx = p->apCsr[p
1ddf0 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1ddf1 74 28 20 75 2e 62 63 2e 70 43 78 2d 3e 64 65 66 t( u.bc.pCx->def
1ddf2 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 erredMoveto==0 )
1ddf3 3b 0a 20 20 75 2e 62 63 2e 70 43 78 2d 3e 73 65 ;. u.bc.pCx->se
1ddf4 65 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20 ekResult = 0;.
1ddf5 75 2e 62 63 2e 70 43 78 2d 3e 63 61 63 68 65 53 u.bc.pCx->cacheS
1ddf6 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 tatus = CACHE_ST
1ddf7 41 4c 45 3b 0a 20 20 75 2e 62 63 2e 70 43 72 73 ALE;. u.bc.pCrs
1ddf8 72 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e 70 43 r = u.bc.pCx->pC
1ddf9 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 49 66 20 ursor;.. /* If
1ddfa 61 6e 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 any of the value
1ddfb 73 20 61 72 65 20 4e 55 4c 4c 2c 20 74 61 6b 65 s are NULL, take
1ddfc 20 74 68 65 20 6a 75 6d 70 2e 20 2a 2f 0a 20 20 the jump. */.
1ddfd 75 2e 62 63 2e 6e 46 69 65 6c 64 20 3d 20 75 2e u.bc.nField = u.
1ddfe 62 63 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f bc.pCx->pKeyInfo
1ddff 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 66 6f 72 28 ->nField;. for(
1de00 75 2e 62 63 2e 69 69 3d 30 3b 20 75 2e 62 63 2e u.bc.ii=0; u.bc.
1de01 69 69 3c 75 2e 62 63 2e 6e 46 69 65 6c 64 3b 20 ii<u.bc.nField;
1de02 75 2e 62 63 2e 69 69 2b 2b 29 7b 0a 20 20 20 20 u.bc.ii++){.
1de03 69 66 28 20 75 2e 62 63 2e 61 4d 65 6d 5b 75 2e if( u.bc.aMem[u.
1de04 62 63 2e 69 69 5d 2e 66 6c 61 67 73 20 26 20 4d bc.ii].flags & M
1de05 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 EM_Null ){.
1de06 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1de07 31 3b 0a 20 20 20 20 20 20 75 2e 62 63 2e 70 43 1;. u.bc.pC
1de08 72 73 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 62 rsr = 0;. b
1de09 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
1de0a 20 20 61 73 73 65 72 74 28 20 28 75 2e 62 63 2e assert( (u.bc.
1de0b 61 4d 65 6d 5b 75 2e 62 63 2e 6e 46 69 65 6c 64 aMem[u.bc.nField
1de0c 5d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ].flags & MEM_Nu
1de0d 6c 6c 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 ll)==0 );.. if(
1de0e 20 75 2e 62 63 2e 70 43 72 73 72 21 3d 30 20 29 u.bc.pCrsr!=0 )
1de0f 7b 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 {. /* Populat
1de10 65 20 74 68 65 20 69 6e 64 65 78 20 73 65 61 72 e the index sear
1de11 63 68 20 6b 65 79 2e 20 2a 2f 0a 20 20 20 20 75 ch key. */. u
1de12 2e 62 63 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d .bc.r.pKeyInfo =
1de13 20 75 2e 62 63 2e 70 43 78 2d 3e 70 4b 65 79 49 u.bc.pCx->pKeyI
1de14 6e 66 6f 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e nfo;. u.bc.r.
1de15 6e 46 69 65 6c 64 20 3d 20 75 2e 62 63 2e 6e 46 nField = u.bc.nF
1de16 69 65 6c 64 20 2b 20 31 3b 0a 20 20 20 20 75 2e ield + 1;. u.
1de17 62 63 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 bc.r.flags = UNP
1de18 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 ACKED_PREFIX_SEA
1de19 52 43 48 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e RCH;. u.bc.r.
1de1a 61 4d 65 6d 20 3d 20 75 2e 62 63 2e 61 4d 65 6d aMem = u.bc.aMem
1de1b 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 63 ;.. /* Extrac
1de1c 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 75 t the value of u
1de1d 2e 62 63 2e 52 20 66 72 6f 6d 20 72 65 67 69 73 .bc.R from regis
1de1e 74 65 72 20 50 33 2e 20 2a 2f 0a 20 20 20 20 73 ter P3. */. s
1de1f 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 qlite3VdbeMemInt
1de20 65 67 65 72 69 66 79 28 70 49 6e 33 29 3b 0a 20 egerify(pIn3);.
1de21 20 20 20 75 2e 62 63 2e 52 20 3d 20 70 49 6e 33 u.bc.R = pIn3
1de22 2d 3e 75 2e 69 3b 0a 0a 20 20 20 20 2f 2a 20 53 ->u.i;.. /* S
1de23 65 61 72 63 68 20 74 68 65 20 42 2d 54 72 65 65 earch the B-Tree
1de24 20 69 6e 64 65 78 2e 20 49 66 20 6e 6f 20 63 6f index. If no co
1de25 6e 66 6c 69 63 74 69 6e 67 20 72 65 63 6f 72 64 nflicting record
1de26 20 69 73 20 66 6f 75 6e 64 2c 20 6a 75 6d 70 0a is found, jump.
1de27 20 20 20 20 2a 2a 20 74 6f 20 50 32 2e 20 4f 74 ** to P2. Ot
1de28 68 65 72 77 69 73 65 2c 20 63 6f 70 79 20 74 68 herwise, copy th
1de29 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 63 e rowid of the c
1de2a 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 63 6f 72 onflicting recor
1de2b 64 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 67 69 d to. ** regi
1de2c 73 74 65 72 20 50 33 20 61 6e 64 20 66 61 6c 6c ster P3 and fall
1de2d 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 through to the
1de2e 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e next instruction
1de2f 2e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 . */. rc = s
1de30 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 qlite3BtreeMovet
1de31 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 63 2e 70 oUnpacked(u.bc.p
1de32 43 72 73 72 2c 20 26 75 2e 62 63 2e 72 2c 20 30 Crsr, &u.bc.r, 0
1de33 2c 20 30 2c 20 26 75 2e 62 63 2e 70 43 78 2d 3e , 0, &u.bc.pCx->
1de34 73 65 65 6b 52 65 73 75 6c 74 29 3b 0a 20 20 20 seekResult);.
1de35 20 69 66 28 20 28 75 2e 62 63 2e 72 2e 66 6c 61 if( (u.bc.r.fla
1de36 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 gs & UNPACKED_PR
1de37 45 46 49 58 5f 53 45 41 52 43 48 29 20 7c 7c 20 EFIX_SEARCH) ||
1de38 75 2e 62 63 2e 72 2e 72 6f 77 69 64 3d 3d 75 2e u.bc.r.rowid==u.
1de39 62 63 2e 52 20 29 7b 0a 20 20 20 20 20 20 70 63 bc.R ){. pc
1de3a 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1de3b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1de3c 20 70 49 6e 33 2d 3e 75 2e 69 20 3d 20 75 2e 62 pIn3->u.i = u.b
1de3d 63 2e 72 2e 72 6f 77 69 64 3b 0a 20 20 20 20 7d c.r.rowid;. }
1de3e 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1de3f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 45 ./* Opcode: NotE
1de40 78 69 73 74 73 20 50 31 20 50 32 20 50 33 20 2a xists P1 P2 P3 *
1de41 20 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 *.**.** Use the
1de42 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 content of regi
1de43 73 74 65 72 20 50 33 20 61 73 20 61 20 69 6e 74 ster P3 as a int
1de44 65 67 65 72 20 6b 65 79 2e 20 20 49 66 20 61 20 eger key. If a
1de45 72 65 63 6f 72 64 20 0a 2a 2a 20 77 69 74 68 20 record .** with
1de46 74 68 61 74 20 6b 65 79 20 64 6f 65 73 20 6e 6f that key does no
1de47 74 20 65 78 69 73 74 20 69 6e 20 74 61 62 6c 65 t exist in table
1de48 20 6f 66 20 50 31 2c 20 74 68 65 6e 20 6a 75 6d of P1, then jum
1de49 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 20 49 66 20 p to P2. .** If
1de4a 74 68 65 20 72 65 63 6f 72 64 20 64 6f 65 73 20 the record does
1de4b 65 78 69 73 74 2c 20 74 68 65 6e 20 66 61 6c 6c exist, then fall
1de4c 20 74 68 72 75 2e 20 20 54 68 65 20 63 75 72 73 thru. The curs
1de4d 6f 72 20 69 73 20 6c 65 66 74 20 0a 2a 2a 20 70 or is left .** p
1de4e 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 72 ointing to the r
1de4f 65 63 6f 72 64 20 69 66 20 69 74 20 65 78 69 73 ecord if it exis
1de50 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 ts..**.** The di
1de51 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e fference between
1de52 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 this operation
1de53 61 6e 64 20 4e 6f 74 46 6f 75 6e 64 20 69 73 20 and NotFound is
1de54 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 6f 70 65 that this.** ope
1de55 72 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 ration assumes t
1de56 68 65 20 6b 65 79 20 69 73 20 61 6e 20 69 6e 74 he key is an int
1de57 65 67 65 72 20 61 6e 64 20 74 68 61 74 20 50 31 eger and that P1
1de58 20 69 73 20 61 20 74 61 62 6c 65 20 77 68 65 72 is a table wher
1de59 65 61 73 0a 2a 2a 20 4e 6f 74 46 6f 75 6e 64 20 eas.** NotFound
1de5a 61 73 73 75 6d 65 73 20 6b 65 79 20 69 73 20 61 assumes key is a
1de5b 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 blob constructe
1de5c 64 20 66 72 6f 6d 20 4d 61 6b 65 52 65 63 6f 72 d from MakeRecor
1de5d 64 20 61 6e 64 0a 2a 2a 20 50 31 20 69 73 20 61 d and.** P1 is a
1de5e 6e 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 n index..**.** S
1de5f 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 ee also: Found,
1de60 4e 6f 74 46 6f 75 6e 64 2c 20 49 73 55 6e 69 71 NotFound, IsUniq
1de61 75 65 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f ue.*/.case OP_No
1de62 74 45 78 69 73 74 73 3a 20 7b 20 20 20 20 20 20 tExists: {
1de63 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a /* jump, in3 *
1de64 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1de65 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1de66 64 20 69 6e 74 6f 20 75 2e 62 64 20 2a 2f 0a 20 d into u.bd */.
1de67 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1de68 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 . BtCursor *pCr
1de69 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 sr;. int res;.
1de6a 20 75 36 34 20 69 4b 65 79 3b 0a 23 65 6e 64 69 u64 iKey;.#endi
1de6b 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1de6c 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1de6d 75 2e 62 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 u.bd */.. asser
1de6e 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 t( pIn3->flags &
1de6f 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 61 73 MEM_Int );. as
1de70 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1de71 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e && pOp->p1<p->n
1de72 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 64 Cursor );. u.bd
1de73 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 .pC = p->apCsr[p
1de74 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1de75 74 28 20 75 2e 62 64 2e 70 43 21 3d 30 20 29 3b t( u.bd.pC!=0 );
1de76 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e . assert( u.bd.
1de77 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 pC->isTable );.
1de78 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43 assert( u.bd.pC
1de79 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 ->pseudoTableReg
1de7a 3d 3d 30 20 29 3b 0a 20 20 75 2e 62 64 2e 70 43 ==0 );. u.bd.pC
1de7b 72 73 72 20 3d 20 75 2e 62 64 2e 70 43 2d 3e 70 rsr = u.bd.pC->p
1de7c 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e Cursor;. if( u.
1de7d 62 64 2e 70 43 72 73 72 21 3d 30 20 29 7b 0a 20 bd.pCrsr!=0 ){.
1de7e 20 20 20 75 2e 62 64 2e 72 65 73 20 3d 20 30 3b u.bd.res = 0;
1de7f 0a 20 20 20 20 75 2e 62 64 2e 69 4b 65 79 20 3d . u.bd.iKey =
1de80 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 pIn3->u.i;.
1de81 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1de82 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 eMovetoUnpacked(
1de83 75 2e 62 64 2e 70 43 72 73 72 2c 20 30 2c 20 75 u.bd.pCrsr, 0, u
1de84 2e 62 64 2e 69 4b 65 79 2c 20 30 2c 20 26 75 2e .bd.iKey, 0, &u.
1de85 62 64 2e 72 65 73 29 3b 0a 20 20 20 20 75 2e 62 bd.res);. u.b
1de86 64 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 20 d.pC->lastRowid
1de87 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 = pIn3->u.i;.
1de88 20 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69 64 49 u.bd.pC->rowidI
1de89 73 56 61 6c 69 64 20 3d 20 75 2e 62 64 2e 72 65 sValid = u.bd.re
1de8a 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 75 s==0 ?1:0;. u
1de8b 2e 62 64 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 .bd.pC->nullRow
1de8c 3d 20 30 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 = 0;. u.bd.pC
1de8d 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1de8e 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 CACHE_STALE;.
1de8f 20 75 2e 62 64 2e 70 43 2d 3e 64 65 66 65 72 72 u.bd.pC->deferr
1de90 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 edMoveto = 0;.
1de91 20 20 69 66 28 20 75 2e 62 64 2e 72 65 73 21 3d if( u.bd.res!=
1de92 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 0 ){. pc =
1de93 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 pOp->p2 - 1;.
1de94 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e assert( u.bd.
1de95 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 pC->rowidIsValid
1de96 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ==0 );. }.
1de97 20 75 2e 62 64 2e 70 43 2d 3e 73 65 65 6b 52 65 u.bd.pC->seekRe
1de98 73 75 6c 74 20 3d 20 75 2e 62 64 2e 72 65 73 3b sult = u.bd.res;
1de99 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
1de9a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 This happens wh
1de9b 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f en an attempt to
1de9c 20 6f 70 65 6e 20 61 20 72 65 61 64 20 63 75 72 open a read cur
1de9d 73 6f 72 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a sor on the. *
1de9e 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 * sqlite_master
1de9f 74 61 62 6c 65 20 72 65 74 75 72 6e 73 20 53 51 table returns SQ
1dea0 4c 49 54 45 5f 45 4d 50 54 59 2e 0a 20 20 20 20 LITE_EMPTY..
1dea1 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d */. pc = pOp-
1dea2 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 61 73 73 >p2 - 1;. ass
1dea3 65 72 74 28 20 75 2e 62 64 2e 70 43 2d 3e 72 6f ert( u.bd.pC->ro
1dea4 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b widIsValid==0 );
1dea5 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 73 65 . u.bd.pC->se
1dea6 65 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20 ekResult = 0;.
1dea7 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1dea8 20 4f 70 63 6f 64 65 3a 20 53 65 71 75 65 6e 63 Opcode: Sequenc
1dea9 65 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a e P1 P2 * * *.**
1deaa 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e 65 78 .** Find the nex
1deab 74 20 61 76 61 69 6c 61 62 6c 65 20 73 65 71 75 t available sequ
1deac 65 6e 63 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 ence number for
1dead 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 57 72 cursor P1..** Wr
1deae 69 74 65 20 74 68 65 20 73 65 71 75 65 6e 63 65 ite the sequence
1deaf 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 72 65 67 number into reg
1deb0 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 54 68 65 ister P2..** The
1deb1 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 sequence number
1deb2 20 6f 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 on the cursor i
1deb3 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 61 66 s incremented af
1deb4 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 73 74 ter this.** inst
1deb5 72 75 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a 63 61 ruction. .*/.ca
1deb6 73 65 20 4f 50 5f 53 65 71 75 65 6e 63 65 3a 20 se OP_Sequence:
1deb7 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f { /* o
1deb8 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1deb9 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d /. assert( pOp-
1deba 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 >p1>=0 && pOp->p
1debb 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 1<p->nCursor );.
1debc 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 assert( p->apC
1debd 73 72 5b 70 4f 70 2d 3e 70 31 5d 21 3d 30 20 29 sr[pOp->p1]!=0 )
1debe 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 ;. pOut->u.i =
1debf 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 p->apCsr[pOp->p1
1dec0 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b 0a 20 ]->seqCount++;.
1dec1 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
1dec2 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a pOut, MEM_Int);.
1dec3 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 break;.}.../*
1dec4 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77 69 64 Opcode: NewRowid
1dec5 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1dec6 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20 69 6e .** Get a new in
1dec7 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75 6d teger record num
1dec8 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f 77 69 ber (a.k.a "rowi
1dec9 64 22 29 20 75 73 65 64 20 61 73 20 74 68 65 20 d") used as the
1deca 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65 2e 0a key to a table..
1decb 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75 ** The record nu
1decc 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72 65 76 mber is not prev
1decd 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73 20 61 iously used as a
1dece 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61 74 61 key in the data
1decf 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 74 68 base.** table th
1ded0 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f 69 at cursor P1 poi
1ded1 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e 65 77 nts to. The new
1ded2 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 record number i
1ded3 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77 72 69 s written.** wri
1ded4 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74 65 72 tten to register
1ded5 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 33 P2..**.** If P3
1ded6 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20 61 20 >0 then P3 is a
1ded7 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65 20 register in the
1ded8 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 74 68 root frame of th
1ded9 69 73 20 56 44 42 45 20 74 68 61 74 20 68 6f 6c is VDBE that hol
1deda 64 73 20 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 ds .** the large
1dedb 73 74 20 70 72 65 76 69 6f 75 73 6c 79 20 67 65 st previously ge
1dedc 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 20 6e nerated record n
1dedd 75 6d 62 65 72 2e 20 4e 6f 20 6e 65 77 20 72 65 umber. No new re
1dede 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 61 72 65 cord numbers are
1dedf 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 .** allowed to b
1dee0 65 20 6c 65 73 73 20 74 68 61 6e 20 74 68 69 73 e less than this
1dee1 20 76 61 6c 75 65 2e 20 57 68 65 6e 20 74 68 69 value. When thi
1dee2 73 20 76 61 6c 75 65 20 72 65 61 63 68 65 73 20 s value reaches
1dee3 69 74 73 20 6d 61 78 69 6d 75 6d 2c 20 0a 2a 2a its maximum, .**
1dee4 20 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 65 a SQLITE_FULL e
1dee5 72 72 6f 72 20 69 73 20 67 65 6e 65 72 61 74 65 rror is generate
1dee6 64 2e 20 54 68 65 20 50 33 20 72 65 67 69 73 74 d. The P3 regist
1dee7 65 72 20 69 73 20 75 70 64 61 74 65 64 20 77 69 er is updated wi
1dee8 74 68 20 74 68 65 20 27 0a 2a 2a 20 67 65 6e 65 th the '.** gene
1dee9 72 61 74 65 64 20 72 65 63 6f 72 64 20 6e 75 6d rated record num
1deea 62 65 72 2e 20 54 68 69 73 20 50 33 20 6d 65 63 ber. This P3 mec
1deeb 68 61 6e 69 73 6d 20 69 73 20 75 73 65 64 20 74 hanism is used t
1deec 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 o help implement
1deed 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49 4e 43 52 the.** AUTOINCR
1deee 45 4d 45 4e 54 20 66 65 61 74 75 72 65 2e 0a 2a EMENT feature..*
1deef 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77 52 6f 77 /.case OP_NewRow
1def0 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 id: {
1def1 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 /* out2-prerelea
1def2 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 se */.#if 0 /*
1def3 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1def4 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 65 20 moved into u.be
1def5 2a 2f 0a 20 20 69 36 34 20 76 3b 20 20 20 20 20 */. i64 v;
1def6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1def7 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a he new rowid */.
1def8 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1def9 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 ; /* Curs
1defa 6f 72 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 67 or of table to g
1defb 65 74 20 74 68 65 20 6e 65 77 20 72 6f 77 69 64 et the new rowid
1defc 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 3b 20 20 */. int res;
1defd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1defe 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c Result of an sql
1deff 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 29 20 ite3BtreeLast()
1df00 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 */. int cnt;
1df01 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
1df02 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69 74 20 ounter to limit
1df03 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 the number of se
1df04 61 72 63 68 65 73 20 2a 2f 0a 20 20 4d 65 6d 20 arches */. Mem
1df05 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 *pMem;
1df06 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 /* Register h
1df07 6f 6c 64 69 6e 67 20 6c 61 72 67 65 73 74 20 72 olding largest r
1df08 6f 77 69 64 20 66 6f 72 20 41 55 54 4f 49 4e 43 owid for AUTOINC
1df09 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 56 64 62 65 REMENT */. Vdbe
1df0a 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 Frame *pFrame;
1df0b 20 20 20 2f 2a 20 52 6f 6f 74 20 66 72 61 6d 65 /* Root frame
1df0c 20 6f 66 20 56 44 42 45 20 2a 2f 0a 23 65 6e 64 of VDBE */.#end
1df0d 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1df0e 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1df0f 20 75 2e 62 65 20 2a 2f 0a 0a 20 20 75 2e 62 65 u.be */.. u.be
1df10 2e 76 20 3d 20 30 3b 0a 20 20 75 2e 62 65 2e 72 .v = 0;. u.be.r
1df11 65 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 es = 0;. assert
1df12 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1df13 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 pOp->p1<p->nCurs
1df14 6f 72 20 29 3b 0a 20 20 75 2e 62 65 2e 70 43 20 or );. u.be.pC
1df15 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e = p->apCsr[pOp->
1df16 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1df17 2e 62 65 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69 .be.pC!=0 );. i
1df18 66 28 20 4e 45 56 45 52 28 75 2e 62 65 2e 70 43 f( NEVER(u.be.pC
1df19 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 29 20 29 7b ->pCursor==0) ){
1df1a 0a 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f . /* The zero
1df1b 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
1df1c 61 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61 above is all tha
1df1d 74 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 t is needed */.
1df1e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 }else{. /* T
1df1f 68 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72 he next rowid or
1df20 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 record number (
1df21 64 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20 different terms
1df22 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 for the same.
1df23 20 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62 ** thing) is ob
1df24 74 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d tained in a two-
1df25 73 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a step algorithm..
1df26 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69 **. ** Fi
1df27 72 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74 rst we attempt t
1df28 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 o find the large
1df29 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 st existing rowi
1df2a 64 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20 d and add one.
1df2b 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42 ** to that. B
1df2c 75 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73 ut if the larges
1df2d 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 t existing rowid
1df2e 20 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20 is already the
1df2f 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70 maximum. ** p
1df30 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c ositive integer,
1df31 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c we have to fall
1df32 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 through to the
1df33 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72 second. ** pr
1df34 6f 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f obabilistic algo
1df35 72 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20 rithm. **.
1df36 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 ** The second a
1df37 6c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73 lgorithm is to s
1df38 65 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 74 elect a rowid at
1df39 20 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20 random and see
1df3a 69 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72 if. ** it alr
1df3b 65 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 74 eady exists in t
1df3c 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74 he table. If it
1df3d 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c does not exist,
1df3e 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 we have. **
1df3f 73 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 74 succeeded. If t
1df40 68 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 he random rowid
1df41 64 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 73 does exist, we s
1df42 65 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a elect a new one.
1df43 20 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61 ** and try a
1df44 67 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 20 gain, up to 100
1df45 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 times.. */.
1df46 20 20 61 73 73 65 72 74 28 20 75 2e 62 65 2e 70 assert( u.be.p
1df47 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 C->isTable );.
1df48 20 20 75 2e 62 65 2e 63 6e 74 20 3d 20 30 3b 0a u.be.cnt = 0;.
1df49 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 33 .#ifdef SQLITE_3
1df4a 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 20 64 2BIT_ROWID.# d
1df4b 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 efine MAX_ROWID
1df4c 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73 65 0x7fffffff.#else
1df4d 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d . /* Some com
1df4e 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 pilers complain
1df4f 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 73 20 about constants
1df50 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 37 66 of the form 0x7f
1df51 66 66 66 66 66 66 66 66 66 66 66 66 66 66 2e 0a ffffffffffffff..
1df52 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 63 6f ** Others co
1df53 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 78 37 mplain about 0x7
1df54 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 ffffffffffffffff
1df55 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 LL. The followi
1df56 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 0a 20 ng macro seems.
1df57 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 64 65 ** to provide
1df58 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 77 68 the constant wh
1df59 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c 20 63 ile making all c
1df5a 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 2e 0a ompilers happy..
1df5b 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e */.# defin
1df5c 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 69 36 e MAX_ROWID (i6
1df5d 34 29 28 20 28 28 28 75 36 34 29 30 78 37 66 66 4)( (((u64)0x7ff
1df5e 66 66 66 66 66 29 3c 3c 33 32 29 20 7c 20 28 75 fffff)<<32) | (u
1df5f 36 34 29 30 78 66 66 66 66 66 66 66 66 20 29 0a 64)0xffffffff ).
1df60 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20 #endif.. if(
1df61 21 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e !u.be.pC->useRan
1df62 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 domRowid ){.
1df63 20 20 75 2e 62 65 2e 76 20 3d 20 73 71 6c 69 74 u.be.v = sqlit
1df64 65 33 42 74 72 65 65 47 65 74 43 61 63 68 65 64 e3BtreeGetCached
1df65 52 6f 77 69 64 28 75 2e 62 65 2e 70 43 2d 3e 70 Rowid(u.be.pC->p
1df66 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 69 Cursor);. i
1df67 66 28 20 75 2e 62 65 2e 76 3d 3d 30 20 29 7b 0a f( u.be.v==0 ){.
1df68 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1df69 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e ite3BtreeLast(u.
1df6a 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 be.pC->pCursor,
1df6b 26 75 2e 62 65 2e 72 65 73 29 3b 0a 20 20 20 20 &u.be.res);.
1df6c 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1df6d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1df6e 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1df6f 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1df70 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
1df71 28 20 75 2e 62 65 2e 72 65 73 20 29 7b 0a 20 20 ( u.be.res ){.
1df72 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d u.be.v =
1df73 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 1;. }els
1df74 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 e{. ass
1df75 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1df76 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75 eCursorIsValid(u
1df77 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 .be.pC->pCursor)
1df78 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 );. rc
1df79 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b = sqlite3BtreeK
1df7a 65 79 53 69 7a 65 28 75 2e 62 65 2e 70 43 2d 3e eySize(u.be.pC->
1df7b 70 43 75 72 73 6f 72 2c 20 26 75 2e 62 65 2e 76 pCursor, &u.be.v
1df7c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 );. ass
1df7d 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
1df7e 4f 4b 20 29 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f OK ); /* Canno
1df7f 74 20 66 61 69 6c 20 66 6f 6c 6c 6f 77 69 6e 67 t fail following
1df80 20 42 74 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a BtreeLast() */.
1df81 20 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e if( u.
1df82 62 65 2e 76 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 be.v==MAX_ROWID
1df83 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 ){. u
1df84 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f .be.pC->useRando
1df85 6d 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 mRowid = 1;.
1df86 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1df87 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 2b u.be.v+
1df88 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 +;. }.
1df89 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1df8a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1df8b 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d _OMIT_AUTOINCREM
1df8c 45 4e 54 0a 20 20 20 20 20 20 69 66 28 20 70 4f ENT. if( pO
1df8d 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 20 20 20 p->p3 ){.
1df8e 20 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29 if( p->pFrame )
1df8f 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 {. for(
1df90 75 2e 62 65 2e 70 46 72 61 6d 65 3d 70 2d 3e 70 u.be.pFrame=p->p
1df91 46 72 61 6d 65 3b 20 75 2e 62 65 2e 70 46 72 61 Frame; u.be.pFra
1df92 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 20 75 2e 62 me->pParent; u.b
1df93 65 2e 70 46 72 61 6d 65 3d 75 2e 62 65 2e 70 46 e.pFrame=u.be.pF
1df94 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a rame->pParent);.
1df95 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 70 u.be.p
1df96 4d 65 6d 20 3d 20 26 75 2e 62 65 2e 70 46 72 61 Mem = &u.be.pFra
1df97 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 me->aMem[pOp->p3
1df98 5d 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 ];. }else
1df99 7b 0a 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 {. u.be
1df9a 2e 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d .pMem = &p->aMem
1df9b 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 [pOp->p3];.
1df9c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 }. /*
1df9d 41 73 73 65 72 74 20 74 68 61 74 20 50 33 20 69 Assert that P3 i
1df9e 73 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 s a valid memory
1df9f 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 cell. */.
1dfa0 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1dfa1 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 3>0 && pOp->p3<=
1dfa2 28 70 2d 3e 70 46 72 61 6d 65 20 3f 20 75 2e 62 (p->pFrame ? u.b
1dfa3 65 2e 70 46 72 61 6d 65 2d 3e 6e 4d 65 6d 20 3a e.pFrame->nMem :
1dfa4 20 70 2d 3e 6e 4d 65 6d 29 20 29 3b 0a 0a 20 20 p->nMem) );..
1dfa5 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 REGISTER_T
1dfa6 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e RACE(pOp->p3, u.
1dfa7 62 65 2e 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 be.pMem);.
1dfa8 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1dfa9 49 6e 74 65 67 65 72 69 66 79 28 75 2e 62 65 2e Integerify(u.be.
1dfaa 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 61 pMem);. a
1dfab 73 73 65 72 74 28 20 28 75 2e 62 65 2e 70 4d 65 ssert( (u.be.pMe
1dfac 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 m->flags & MEM_I
1dfad 6e 74 29 21 3d 30 20 29 3b 20 20 2f 2a 20 6d 65 nt)!=0 ); /* me
1dfae 6d 28 50 33 29 20 68 6f 6c 64 73 20 61 6e 20 69 m(P3) holds an i
1dfaf 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 nteger */.
1dfb0 20 20 69 66 28 20 75 2e 62 65 2e 70 4d 65 6d 2d if( u.be.pMem-
1dfb1 3e 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 >u.i==MAX_ROWID
1dfb2 7c 7c 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 || u.be.pC->useR
1dfb3 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 andomRowid ){.
1dfb4 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
1dfb5 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 ITE_FULL;.
1dfb6 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 goto abort_d
1dfb7 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
1dfb8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
1dfb9 66 28 20 75 2e 62 65 2e 76 3c 75 2e 62 65 2e 70 f( u.be.v<u.be.p
1dfba 4d 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 20 Mem->u.i+1 ){.
1dfbb 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d u.be.v =
1dfbc 20 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 20 u.be.pMem->u.i
1dfbd 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 + 1;. }.
1dfbe 20 20 20 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d u.be.pMem
1dfbf 2d 3e 75 2e 69 20 3d 20 75 2e 62 65 2e 76 3b 0a ->u.i = u.be.v;.
1dfc0 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a }.#endif..
1dfc1 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
1dfc2 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 eeSetCachedRowid
1dfc3 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f (u.be.pC->pCurso
1dfc4 72 2c 20 75 2e 62 65 2e 76 3c 4d 41 58 5f 52 4f r, u.be.v<MAX_RO
1dfc5 57 49 44 20 3f 20 75 2e 62 65 2e 76 2b 31 20 3a WID ? u.be.v+1 :
1dfc6 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 0);. }. i
1dfc7 66 28 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 f( u.be.pC->useR
1dfc8 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 andomRowid ){.
1dfc9 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1dfca 3e 70 33 3d 3d 30 20 29 3b 20 20 2f 2a 20 57 65 >p3==0 ); /* We
1dfcb 20 63 61 6e 6e 6f 74 20 62 65 20 69 6e 20 72 61 cannot be in ra
1dfcc 6e 64 6f 6d 20 72 6f 77 69 64 20 6d 6f 64 65 20 ndom rowid mode
1dfcd 69 66 20 74 68 69 73 20 69 73 0a 20 20 20 20 20 if this is.
1dfce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dfcf 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 41 55 ** an AU
1dfd0 54 4f 49 4e 43 52 45 4d 45 4e 54 20 74 61 62 6c TOINCREMENT tabl
1dfd1 65 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 62 65 e. */. u.be
1dfd2 2e 76 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77 .v = db->lastRow
1dfd3 69 64 3b 0a 20 20 20 20 20 20 75 2e 62 65 2e 63 id;. u.be.c
1dfd4 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f nt = 0;. do
1dfd5 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e {. if( u.
1dfd6 62 65 2e 63 6e 74 3d 3d 30 20 26 26 20 28 75 2e be.cnt==0 && (u.
1dfd7 62 65 2e 76 26 30 78 66 66 66 66 66 66 29 3d 3d be.v&0xffffff)==
1dfd8 75 2e 62 65 2e 76 20 29 7b 0a 20 20 20 20 20 20 u.be.v ){.
1dfd9 20 20 20 20 75 2e 62 65 2e 76 2b 2b 3b 0a 20 20 u.be.v++;.
1dfda 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1dfdb 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 sqlite3_r
1dfdc 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 andomness(sizeof
1dfdd 28 75 2e 62 65 2e 76 29 2c 20 26 75 2e 62 65 2e (u.be.v), &u.be.
1dfde 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 v);. if
1dfdf 28 20 75 2e 62 65 2e 63 6e 74 3c 35 20 29 20 75 ( u.be.cnt<5 ) u
1dfe0 2e 62 65 2e 76 20 26 3d 20 30 78 66 66 66 66 66 .be.v &= 0xfffff
1dfe1 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 f;. }.
1dfe2 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1dfe3 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 3BtreeMovetoUnpa
1dfe4 63 6b 65 64 28 75 2e 62 65 2e 70 43 2d 3e 70 43 cked(u.be.pC->pC
1dfe5 75 72 73 6f 72 2c 20 30 2c 20 28 75 36 34 29 75 ursor, 0, (u64)u
1dfe6 2e 62 65 2e 76 2c 20 30 2c 20 26 75 2e 62 65 2e .be.v, 0, &u.be.
1dfe7 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 75 2e res);. u.
1dfe8 62 65 2e 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 be.cnt++;.
1dfe9 7d 77 68 69 6c 65 28 20 75 2e 62 65 2e 63 6e 74 }while( u.be.cnt
1dfea 3c 31 30 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 <100 && rc==SQLI
1dfeb 54 45 5f 4f 4b 20 26 26 20 75 2e 62 65 2e 72 65 TE_OK && u.be.re
1dfec 73 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 s==0 );. if
1dfed 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1dfee 26 26 20 75 2e 62 65 2e 72 65 73 3d 3d 30 20 29 && u.be.res==0 )
1dfef 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 {. rc = S
1dff0 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 QLITE_FULL;.
1dff1 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 goto abort_d
1dff2 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
1dff3 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 75 }. }. u
1dff4 2e 62 65 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 .be.pC->rowidIsV
1dff5 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 75 2e alid = 0;. u.
1dff6 62 65 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d be.pC->deferredM
1dff7 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 75 oveto = 0;. u
1dff8 2e 62 65 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 .be.pC->cacheSta
1dff9 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c tus = CACHE_STAL
1dffa 45 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 E;. }. MemSetT
1dffb 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 ypeFlag(pOut, ME
1dffc 4d 5f 49 6e 74 29 3b 0a 20 20 70 4f 75 74 2d 3e M_Int);. pOut->
1dffd 75 2e 69 20 3d 20 75 2e 62 65 2e 76 3b 0a 20 20 u.i = u.be.v;.
1dffe 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1dfff 6f 64 65 3a 20 49 6e 73 65 72 74 20 50 31 20 50 ode: Insert P1 P
1e000 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 2 P3 P4 P5.**.**
1e001 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 Write an entry
1e002 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f into the table o
1e003 66 20 63 75 72 73 6f 72 20 50 31 2e 20 20 41 20 f cursor P1. A
1e004 6e 65 77 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20 new entry is.**
1e005 63 72 65 61 74 65 64 20 69 66 20 69 74 20 64 6f created if it do
1e006 65 73 6e 27 74 20 61 6c 72 65 61 64 79 20 65 78 esn't already ex
1e007 69 73 74 20 6f 72 20 74 68 65 20 64 61 74 61 20 ist or the data
1e008 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 0a for an existing.
1e009 2a 2a 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72 ** entry is over
1e00a 77 72 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61 written. The da
1e00b 74 61 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 ta is the value
1e00c 4d 45 4d 5f 42 6c 6f 62 20 73 74 6f 72 65 64 20 MEM_Blob stored
1e00d 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6e in register.** n
1e00e 75 6d 62 65 72 20 50 32 2e 20 54 68 65 20 6b 65 umber P2. The ke
1e00f 79 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 y is stored in r
1e010 65 67 69 73 74 65 72 20 50 33 2e 20 54 68 65 20 egister P3. The
1e011 6b 65 79 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 key must.** be a
1e012 20 4d 45 4d 5f 49 6e 74 2e 0a 2a 2a 0a 2a 2a 20 MEM_Int..**.**
1e013 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43 If the OPFLAG_NC
1e014 48 41 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 35 HANGE flag of P5
1e015 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 is set, then th
1e016 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 e row change cou
1e017 6e 74 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 nt is.** increme
1e018 6e 74 65 64 20 28 6f 74 68 65 72 77 69 73 65 20 nted (otherwise
1e019 6e 6f 74 29 2e 20 20 49 66 20 74 68 65 20 4f 50 not). If the OP
1e01a 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 66 FLAG_LASTROWID f
1e01b 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 lag of P5 is set
1e01c 2c 0a 2a 2a 20 74 68 65 6e 20 72 6f 77 69 64 20 ,.** then rowid
1e01d 69 73 20 73 74 6f 72 65 64 20 66 6f 72 20 73 75 is stored for su
1e01e 62 73 65 71 75 65 6e 74 20 72 65 74 75 72 6e 20 bsequent return
1e01f 62 79 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 by the.** sqlite
1e020 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 3_last_insert_ro
1e021 77 69 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 wid() function (
1e022 6f 74 68 65 72 77 69 73 65 20 69 74 20 69 73 20 otherwise it is
1e023 75 6e 6d 6f 64 69 66 69 65 64 29 2e 0a 2a 2a 0a unmodified)..**.
1e024 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47 ** If the OPFLAG
1e025 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 _USESEEKRESULT f
1e026 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 lag of P5 is set
1e027 20 61 6e 64 20 69 66 20 74 68 65 20 72 65 73 75 and if the resu
1e028 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 61 73 lt of.** the las
1e029 74 20 73 65 65 6b 20 6f 70 65 72 61 74 69 6f 6e t seek operation
1e02a 20 28 4f 50 5f 4e 6f 74 45 78 69 73 74 73 29 20 (OP_NotExists)
1e02b 77 61 73 20 61 20 73 75 63 63 65 73 73 2c 20 74 was a success, t
1e02c 68 65 6e 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72 hen this.** oper
1e02d 61 74 69 6f 6e 20 77 69 6c 6c 20 6e 6f 74 20 61 ation will not a
1e02e 74 74 65 6d 70 74 20 74 6f 20 66 69 6e 64 20 74 ttempt to find t
1e02f 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 he appropriate r
1e030 6f 77 20 62 65 66 6f 72 65 20 64 6f 69 6e 67 0a ow before doing.
1e031 2a 2a 20 74 68 65 20 69 6e 73 65 72 74 20 62 75 ** the insert bu
1e032 74 20 77 69 6c 6c 20 69 6e 73 74 65 61 64 20 6f t will instead o
1e033 76 65 72 77 72 69 74 65 20 74 68 65 20 72 6f 77 verwrite the row
1e034 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 that the cursor
1e035 20 69 73 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 is.** currently
1e036 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 50 pointing to. P
1e037 72 65 73 75 6d 61 62 6c 79 2c 20 74 68 65 20 70 resumably, the p
1e038 72 69 6f 72 20 4f 50 5f 4e 6f 74 45 78 69 73 74 rior OP_NotExist
1e039 73 20 6f 70 63 6f 64 65 0a 2a 2a 20 68 61 73 20 s opcode.** has
1e03a 61 6c 72 65 61 64 79 20 70 6f 73 69 74 69 6f 6e already position
1e03b 65 64 20 74 68 65 20 63 75 72 73 6f 72 20 63 6f ed the cursor co
1e03c 72 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 69 rrectly. This i
1e03d 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f s an optimizatio
1e03e 6e 0a 2a 2a 20 74 68 61 74 20 62 6f 6f 73 74 73 n.** that boosts
1e03f 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 79 20 performance by
1e040 61 76 6f 69 64 69 6e 67 20 72 65 64 75 6e 64 61 avoiding redunda
1e041 6e 74 20 73 65 65 6b 73 2e 0a 2a 2a 0a 2a 2a 20 nt seeks..**.**
1e042 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 49 53 If the OPFLAG_IS
1e043 55 50 44 41 54 45 20 66 6c 61 67 20 69 73 20 73 UPDATE flag is s
1e044 65 74 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 et, then this op
1e045 63 6f 64 65 20 69 73 20 70 61 72 74 20 6f 66 20 code is part of
1e046 61 6e 0a 2a 2a 20 55 50 44 41 54 45 20 6f 70 65 an.** UPDATE ope
1e047 72 61 74 69 6f 6e 2e 20 20 4f 74 68 65 72 77 69 ration. Otherwi
1e048 73 65 20 28 69 66 20 74 68 65 20 66 6c 61 67 20 se (if the flag
1e049 69 73 20 63 6c 65 61 72 29 20 74 68 65 6e 20 74 is clear) then t
1e04a 68 69 73 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 his opcode.** is
1e04b 20 70 61 72 74 20 6f 66 20 61 6e 20 49 4e 53 45 part of an INSE
1e04c 52 54 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 54 RT operation. T
1e04d 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 he difference is
1e04e 20 6f 6e 6c 79 20 69 6d 70 6f 72 74 61 6e 74 20 only important
1e04f 74 6f 0a 2a 2a 20 74 68 65 20 75 70 64 61 74 65 to.** the update
1e050 20 68 6f 6f 6b 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 hook..**.** Par
1e051 61 6d 65 74 65 72 20 50 34 20 6d 61 79 20 70 6f ameter P4 may po
1e052 69 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 int to a string
1e053 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 containing the t
1e054 61 62 6c 65 2d 6e 61 6d 65 2c 20 6f 72 0a 2a 2a able-name, or.**
1e055 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 49 66 may be NULL. If
1e056 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c it is not NULL,
1e057 20 74 68 65 6e 20 74 68 65 20 75 70 64 61 74 65 then the update
1e058 2d 68 6f 6f 6b 20 0a 2a 2a 20 28 73 71 6c 69 74 -hook .** (sqlit
1e059 65 33 2e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 e3.xUpdateCallba
1e05a 63 6b 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 ck) is invoked f
1e05b 6f 6c 6c 6f 77 69 6e 67 20 61 20 73 75 63 63 65 ollowing a succe
1e05c 73 73 66 75 6c 20 69 6e 73 65 72 74 2e 0a 2a 2a ssful insert..**
1e05d 0a 2a 2a 20 28 57 41 52 4e 49 4e 47 2f 54 4f 44 .** (WARNING/TOD
1e05e 4f 3a 20 49 66 20 50 31 20 69 73 20 61 20 70 73 O: If P1 is a ps
1e05f 65 75 64 6f 2d 63 75 72 73 6f 72 20 61 6e 64 20 eudo-cursor and
1e060 50 32 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c P2 is dynamicall
1e061 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 y.** allocated,
1e062 74 68 65 6e 20 6f 77 6e 65 72 73 68 69 70 20 6f then ownership o
1e063 66 20 50 32 20 69 73 20 74 72 61 6e 73 66 65 72 f P2 is transfer
1e064 72 65 64 20 74 6f 20 74 68 65 20 70 73 65 75 64 red to the pseud
1e065 6f 2d 63 75 72 73 6f 72 0a 2a 2a 20 61 6e 64 20 o-cursor.** and
1e066 72 65 67 69 73 74 65 72 20 50 32 20 62 65 63 6f register P2 beco
1e067 6d 65 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 mes ephemeral.
1e068 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 If the cursor is
1e069 20 63 68 61 6e 67 65 64 2c 20 74 68 65 0a 2a 2a changed, the.**
1e06a 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 value of regist
1e06b 65 72 20 50 32 20 77 69 6c 6c 20 74 68 65 6e 20 er P2 will then
1e06c 63 68 61 6e 67 65 2e 20 20 4d 61 6b 65 20 73 75 change. Make su
1e06d 72 65 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 re this does not
1e06e 0a 2a 2a 20 63 61 75 73 65 20 61 6e 79 20 70 72 .** cause any pr
1e06f 6f 62 6c 65 6d 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 oblems.).**.** T
1e070 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1e071 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 74 61 only works on ta
1e072 62 6c 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 bles. The equiv
1e073 61 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f alent instructio
1e074 6e 0a 2a 2a 20 66 6f 72 20 69 6e 64 69 63 65 73 n.** for indices
1e075 20 69 73 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 is OP_IdxInsert
1e076 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 73 ..*/.case OP_Ins
1e077 65 72 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a ert: {.#if 0 /*
1e078 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e079 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 66 moved into u.bf
1e07a 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 */. Mem *pData
1e07b 3b 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63 ; /* MEM c
1e07c 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 ell holding data
1e07d 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 for the record
1e07e 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a to be inserted *
1e07f 2f 0a 20 20 4d 65 6d 20 2a 70 4b 65 79 3b 20 20 /. Mem *pKey;
1e080 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c /* MEM cel
1e081 6c 20 68 6f 6c 64 69 6e 67 20 6b 65 79 20 20 66 l holding key f
1e082 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f or the record */
1e083 0a 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 . i64 iKey;
1e084 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 /* The inte
1e085 67 65 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 ger ROWID or key
1e086 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 for the record
1e087 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a to be inserted *
1e088 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
1e089 70 43 3b 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 pC; /* Cursor
1e08a 74 6f 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 to table into wh
1e08b 69 63 68 20 69 6e 73 65 72 74 20 69 73 20 77 72 ich insert is wr
1e08c 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e itten */. int n
1e08d 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 Zero; /*
1e08e 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 2d 62 Number of zero-b
1e08f 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 2a ytes to append *
1e090 2f 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75 /. int seekResu
1e091 6c 74 3b 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 lt; /* Result
1e092 6f 66 20 70 72 69 6f 72 20 73 65 65 6b 20 6f 72 of prior seek or
1e093 20 30 20 69 66 20 6e 6f 20 55 53 45 53 45 45 4b 0 if no USESEEK
1e094 52 45 53 55 4c 54 20 66 6c 61 67 20 2a 2f 0a 20 RESULT flag */.
1e095 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 const char *zDb
1e096 3b 20 20 2f 2a 20 64 61 74 61 62 61 73 65 20 6e ; /* database n
1e097 61 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 ame - used by th
1e098 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f e update hook */
1e099 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1e09a 54 62 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 6e 61 Tbl; /* Table na
1e09b 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 me - used by the
1e09c 20 6f 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a opdate hook */.
1e09d 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 int op;
1e09e 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 66 6f /* Opcode fo
1e09f 72 20 75 70 64 61 74 65 20 68 6f 6f 6b 3a 20 53 r update hook: S
1e0a0 51 4c 49 54 45 5f 55 50 44 41 54 45 20 6f 72 20 QLITE_UPDATE or
1e0a1 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 20 2a 2f SQLITE_INSERT */
1e0a2 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1e0a3 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1e0a4 20 69 6e 74 6f 20 75 2e 62 66 20 2a 2f 0a 0a 20 into u.bf */..
1e0a5 20 75 2e 62 66 2e 70 44 61 74 61 20 3d 20 26 70 u.bf.pData = &p
1e0a6 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b ->aMem[pOp->p2];
1e0a7 0a 20 20 75 2e 62 66 2e 70 4b 65 79 20 3d 20 26 . u.bf.pKey = &
1e0a8 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
1e0a9 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1e0aa 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 >p1>=0 && pOp->p
1e0ab 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 1<p->nCursor );.
1e0ac 20 20 75 2e 62 66 2e 70 43 20 3d 20 70 2d 3e 61 u.bf.pC = p->a
1e0ad 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 pCsr[pOp->p1];.
1e0ae 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 assert( u.bf.pC
1e0af 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
1e0b0 20 75 2e 62 66 2e 70 43 2d 3e 70 43 75 72 73 6f u.bf.pC->pCurso
1e0b1 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 r!=0 );. assert
1e0b2 28 20 75 2e 62 66 2e 70 43 2d 3e 70 73 65 75 64 ( u.bf.pC->pseud
1e0b3 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a oTableReg==0 );.
1e0b4 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 assert( u.bf.p
1e0b5 4b 65 79 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Key->flags & MEM
1e0b6 5f 49 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 74 _Int );. assert
1e0b7 28 20 75 2e 62 66 2e 70 43 2d 3e 69 73 54 61 62 ( u.bf.pC->isTab
1e0b8 6c 65 20 29 3b 0a 20 20 52 45 47 49 53 54 45 52 le );. REGISTER
1e0b9 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 _TRACE(pOp->p2,
1e0ba 75 2e 62 66 2e 70 44 61 74 61 29 3b 0a 20 20 52 u.bf.pData);. R
1e0bb 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1e0bc 70 2d 3e 70 33 2c 20 75 2e 62 66 2e 70 4b 65 79 p->p3, u.bf.pKey
1e0bd 29 3b 0a 0a 20 20 75 2e 62 66 2e 69 4b 65 79 20 );.. u.bf.iKey
1e0be 3d 20 75 2e 62 66 2e 70 4b 65 79 2d 3e 75 2e 69 = u.bf.pKey->u.i
1e0bf 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 ;. if( pOp->p5
1e0c0 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 & OPFLAG_NCHANGE
1e0c1 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b ) p->nChange++;
1e0c2 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 . if( pOp->p5 &
1e0c3 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 OPFLAG_LASTROWI
1e0c4 44 20 29 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 D ) db->lastRowi
1e0c5 64 20 3d 20 75 2e 62 66 2e 70 4b 65 79 2d 3e 75 d = u.bf.pKey->u
1e0c6 2e 69 3b 0a 20 20 69 66 28 20 75 2e 62 66 2e 70 .i;. if( u.bf.p
1e0c7 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 Data->flags & ME
1e0c8 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e M_Null ){. u.
1e0c9 62 66 2e 70 44 61 74 61 2d 3e 7a 20 3d 20 30 3b bf.pData->z = 0;
1e0ca 0a 20 20 20 20 75 2e 62 66 2e 70 44 61 74 61 2d . u.bf.pData-
1e0cb 3e 6e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b >n = 0;. }else{
1e0cc 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 . assert( u.b
1e0cd 66 2e 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 f.pData->flags &
1e0ce 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 (MEM_Blob|MEM_S
1e0cf 74 72 29 20 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 tr) );. }. u.b
1e0d0 66 2e 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 28 f.seekResult = (
1e0d1 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 (pOp->p5 & OPFLA
1e0d2 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 G_USESEEKRESULT)
1e0d3 20 3f 20 75 2e 62 66 2e 70 43 2d 3e 73 65 65 6b ? u.bf.pC->seek
1e0d4 52 65 73 75 6c 74 20 3a 20 30 29 3b 0a 20 20 69 Result : 0);. i
1e0d5 66 28 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 66 f( u.bf.pData->f
1e0d6 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 lags & MEM_Zero
1e0d7 29 7b 0a 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72 ){. u.bf.nZer
1e0d8 6f 20 3d 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e o = u.bf.pData->
1e0d9 75 2e 6e 5a 65 72 6f 3b 0a 20 20 7d 65 6c 73 65 u.nZero;. }else
1e0da 7b 0a 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72 6f {. u.bf.nZero
1e0db 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 = 0;. }. sqli
1e0dc 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 te3BtreeSetCache
1e0dd 64 52 6f 77 69 64 28 75 2e 62 66 2e 70 43 2d 3e dRowid(u.bf.pC->
1e0de 70 43 75 72 73 6f 72 2c 20 30 29 3b 0a 20 20 72 pCursor, 0);. r
1e0df 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1e0e0 49 6e 73 65 72 74 28 75 2e 62 66 2e 70 43 2d 3e Insert(u.bf.pC->
1e0e1 70 43 75 72 73 6f 72 2c 20 30 2c 20 75 2e 62 66 pCursor, 0, u.bf
1e0e2 2e 69 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 .iKey,.
1e0e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e0e4 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 7a 2c 20 u.bf.pData->z,
1e0e5 75 2e 62 66 2e 70 44 61 74 61 2d 3e 6e 2c 20 75 u.bf.pData->n, u
1e0e6 2e 62 66 2e 6e 5a 65 72 6f 2c 0a 20 20 20 20 20 .bf.nZero,.
1e0e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e0e8 20 20 20 20 20 70 4f 70 2d 3e 70 35 20 26 20 4f pOp->p5 & O
1e0e9 50 46 4c 41 47 5f 41 50 50 45 4e 44 2c 20 75 2e PFLAG_APPEND, u.
1e0ea 62 66 2e 73 65 65 6b 52 65 73 75 6c 74 0a 20 20 bf.seekResult.
1e0eb 29 3b 0a 20 20 75 2e 62 66 2e 70 43 2d 3e 72 6f );. u.bf.pC->ro
1e0ec 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a widIsValid = 0;.
1e0ed 20 20 75 2e 62 66 2e 70 43 2d 3e 64 65 66 65 72 u.bf.pC->defer
1e0ee 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 redMoveto = 0;.
1e0ef 20 75 2e 62 66 2e 70 43 2d 3e 63 61 63 68 65 53 u.bf.pC->cacheS
1e0f0 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 tatus = CACHE_ST
1e0f1 41 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b ALE;.. /* Invok
1e0f2 65 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f e the update-hoo
1e0f3 6b 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a k if required. *
1e0f4 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 /. if( rc==SQLI
1e0f5 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 TE_OK && db->xUp
1e0f6 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 dateCallback &&
1e0f7 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 pOp->p4.z ){.
1e0f8 20 75 2e 62 66 2e 7a 44 62 20 3d 20 64 62 2d 3e u.bf.zDb = db->
1e0f9 61 44 62 5b 75 2e 62 66 2e 70 43 2d 3e 69 44 62 aDb[u.bf.pC->iDb
1e0fa 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 75 2e 62 ].zName;. u.b
1e0fb 66 2e 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 f.zTbl = pOp->p4
1e0fc 2e 7a 3b 0a 20 20 20 20 75 2e 62 66 2e 6f 70 20 .z;. u.bf.op
1e0fd 3d 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 = ((pOp->p5 & OP
1e0fe 46 4c 41 47 5f 49 53 55 50 44 41 54 45 29 20 3f FLAG_ISUPDATE) ?
1e0ff 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20 3a SQLITE_UPDATE :
1e100 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 29 3b SQLITE_INSERT);
1e101 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 . assert( u.b
1e102 66 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b f.pC->isTable );
1e103 0a 20 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 . db->xUpdate
1e104 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 Callback(db->pUp
1e105 64 61 74 65 41 72 67 2c 20 75 2e 62 66 2e 6f 70 dateArg, u.bf.op
1e106 2c 20 75 2e 62 66 2e 7a 44 62 2c 20 75 2e 62 66 , u.bf.zDb, u.bf
1e107 2e 7a 54 62 6c 2c 20 75 2e 62 66 2e 69 4b 65 79 .zTbl, u.bf.iKey
1e108 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 );. assert( u
1e109 2e 62 66 2e 70 43 2d 3e 69 44 62 3e 3d 30 20 29 .bf.pC->iDb>=0 )
1e10a 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1e10b 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 6c ../* Opcode: Del
1e10c 65 74 65 20 50 31 20 50 32 20 2a 20 50 34 20 2a ete P1 P2 * P4 *
1e10d 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 .**.** Delete th
1e10e 65 20 72 65 63 6f 72 64 20 61 74 20 77 68 69 63 e record at whic
1e10f 68 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 h the P1 cursor
1e110 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 is currently poi
1e111 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 nting..**.** The
1e112 20 63 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 cursor will be
1e113 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 left pointing at
1e114 20 65 69 74 68 65 72 20 74 68 65 20 6e 65 78 74 either the next
1e115 20 6f 72 20 74 68 65 20 70 72 65 76 69 6f 75 73 or the previous
1e116 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e 20 74 68 .** record in th
1e117 65 20 74 61 62 6c 65 2e 20 49 66 20 69 74 20 69 e table. If it i
1e118 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 s left pointing
1e119 61 74 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f at the next reco
1e11a 72 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 rd, then.** the
1e11b 6e 65 78 74 20 4e 65 78 74 20 69 6e 73 74 72 75 next Next instru
1e11c 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 61 20 ction will be a
1e11d 6e 6f 2d 6f 70 2e 20 20 48 65 6e 63 65 20 69 74 no-op. Hence it
1e11e 20 69 73 20 4f 4b 20 74 6f 20 64 65 6c 65 74 65 is OK to delete
1e11f 0a 2a 2a 20 61 20 72 65 63 6f 72 64 20 66 72 6f .** a record fro
1e120 6d 20 77 69 74 68 69 6e 20 61 6e 20 4e 65 78 74 m within an Next
1e121 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 loop..**.** If
1e122 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e the OPFLAG_NCHAN
1e123 47 45 20 66 6c 61 67 20 6f 66 20 50 32 20 69 73 GE flag of P2 is
1e124 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 set, then the r
1e125 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 ow change count
1e126 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 is.** incremente
1e127 64 20 28 6f 74 68 65 72 77 69 73 65 20 6e 6f 74 d (otherwise not
1e128 29 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 6d 75 73 74 )..**.** P1 must
1e129 20 6e 6f 74 20 62 65 20 70 73 65 75 64 6f 2d 74 not be pseudo-t
1e12a 61 62 6c 65 2e 20 20 49 74 20 68 61 73 20 74 6f able. It has to
1e12b 20 62 65 20 61 20 72 65 61 6c 20 74 61 62 6c 65 be a real table
1e12c 20 77 69 74 68 0a 2a 2a 20 6d 75 6c 74 69 70 6c with.** multipl
1e12d 65 20 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 e rows..**.** If
1e12e 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c P4 is not NULL,
1e12f 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 then it is the
1e130 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c name of the tabl
1e131 65 20 74 68 61 74 20 50 31 20 69 73 0a 2a 2a 20 e that P1 is.**
1e132 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 pointing to. Th
1e133 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 77 69 e update hook wi
1e134 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 69 ll be invoked, i
1e135 66 20 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 20 f it exists..**
1e136 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c If P4 is not NUL
1e137 4c 20 74 68 65 6e 20 74 68 65 20 50 31 20 63 75 L then the P1 cu
1e138 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62 rsor must have b
1e139 65 65 6e 20 70 6f 73 69 74 69 6f 6e 65 64 0a 2a een positioned.*
1e13a 2a 20 75 73 69 6e 67 20 4f 50 5f 4e 6f 74 46 6f * using OP_NotFo
1e13b 75 6e 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 und prior to inv
1e13c 6f 6b 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 oking this opcod
1e13d 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65 e..*/.case OP_De
1e13e 6c 65 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f lete: {.#if 0 /
1e13f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1e140 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1e141 67 20 2a 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b g */. i64 iKey;
1e142 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1e143 43 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 C;.#endif /* loc
1e144 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e145 65 64 20 69 6e 74 6f 20 75 2e 62 67 20 2a 2f 0a ed into u.bg */.
1e146 0a 20 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 30 . u.bg.iKey = 0
1e147 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1e148 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 >p1>=0 && pOp->p
1e149 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 1<p->nCursor );.
1e14a 20 20 75 2e 62 67 2e 70 43 20 3d 20 70 2d 3e 61 u.bg.pC = p->a
1e14b 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 pCsr[pOp->p1];.
1e14c 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 assert( u.bg.pC
1e14d 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
1e14e 20 75 2e 62 67 2e 70 43 2d 3e 70 43 75 72 73 6f u.bg.pC->pCurso
1e14f 72 21 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c 79 r!=0 ); /* Only
1e150 20 76 61 6c 69 64 20 66 6f 72 20 72 65 61 6c 20 valid for real
1e151 74 61 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75 64 tables, no pseud
1e152 6f 74 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a otables */.. /*
1e153 20 49 66 20 74 68 65 20 75 70 64 61 74 65 2d 68 If the update-h
1e154 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f ook will be invo
1e155 6b 65 64 2c 20 73 65 74 20 75 2e 62 67 2e 69 4b ked, set u.bg.iK
1e156 65 79 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20 ey to the rowid
1e157 6f 66 20 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20 of the. ** row
1e158 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 20 being deleted..
1e159 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 55 */. if( db->xU
1e15a 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 pdateCallback &&
1e15b 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 pOp->p4.z ){.
1e15c 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 assert( u.bg.p
1e15d 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 C->isTable );.
1e15e 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 assert( u.bg.p
1e15f 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 C->rowidIsValid
1e160 29 3b 20 20 2f 2a 20 6c 61 73 74 52 6f 77 69 64 ); /* lastRowid
1e161 20 73 65 74 20 62 79 20 70 72 65 76 69 6f 75 73 set by previous
1e162 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a 2f 0a OP_NotFound */.
1e163 20 20 20 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 u.bg.iKey =
1e164 75 2e 62 67 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 u.bg.pC->lastRow
1e165 69 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 id;. }.. /* Th
1e166 65 20 4f 50 5f 44 65 6c 65 74 65 20 6f 70 63 6f e OP_Delete opco
1e167 64 65 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 de always follow
1e168 73 20 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74 s an OP_NotExist
1e169 73 20 6f 72 20 4f 50 5f 4c 61 73 74 20 6f 72 0a s or OP_Last or.
1e16a 20 20 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f ** OP_Column o
1e16b 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 n the same table
1e16c 20 77 69 74 68 6f 75 74 20 61 6e 79 20 69 6e 74 without any int
1e16d 65 72 76 65 6e 69 6e 67 20 6f 70 65 72 61 74 69 ervening operati
1e16e 6f 6e 73 20 74 68 61 74 0a 20 20 2a 2a 20 6d 69 ons that. ** mi
1e16f 67 68 74 20 6d 6f 76 65 20 6f 72 20 69 6e 76 61 ght move or inva
1e170 6c 69 64 61 74 65 20 74 68 65 20 63 75 72 73 6f lidate the curso
1e171 72 2e 20 20 48 65 6e 63 65 20 63 75 72 73 6f 72 r. Hence cursor
1e172 20 75 2e 62 67 2e 70 43 20 69 73 20 61 6c 77 61 u.bg.pC is alwa
1e173 79 73 20 70 6f 69 6e 74 69 6e 67 0a 20 20 2a 2a ys pointing. **
1e174 20 74 6f 20 74 68 65 20 72 6f 77 20 74 6f 20 62 to the row to b
1e175 65 20 64 65 6c 65 74 65 64 20 61 6e 64 20 74 68 e deleted and th
1e176 65 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 e sqlite3VdbeCur
1e177 73 6f 72 4d 6f 76 65 74 6f 28 29 20 6f 70 65 72 sorMoveto() oper
1e178 61 74 69 6f 6e 0a 20 20 2a 2a 20 62 65 6c 6f 77 ation. ** below
1e179 20 69 73 20 61 6c 77 61 79 73 20 61 20 6e 6f 2d is always a no-
1e17a 6f 70 20 61 6e 64 20 63 61 6e 6e 6f 74 20 66 61 op and cannot fa
1e17b 69 6c 2e 20 20 57 65 20 77 69 6c 6c 20 72 75 6e il. We will run
1e17c 20 69 74 20 61 6e 79 68 6f 77 2c 20 74 68 6f 75 it anyhow, thou
1e17d 67 68 2c 0a 20 20 2a 2a 20 74 6f 20 67 75 61 72 gh,. ** to guar
1e17e 64 20 61 67 61 69 6e 73 74 20 66 75 74 75 72 65 d against future
1e17f 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 changes to the
1e180 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a code generator..
1e181 20 20 2a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 **/. assert(
1e182 75 2e 62 67 2e 70 43 2d 3e 64 65 66 65 72 72 65 u.bg.pC->deferre
1e183 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 dMoveto==0 );.
1e184 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1e185 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 CursorMoveto(u.b
1e186 67 2e 70 43 29 3b 0a 20 20 69 66 28 20 4e 45 56 g.pC);. if( NEV
1e187 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b ER(rc!=SQLITE_OK
1e188 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 ) ) goto abort_d
1e189 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 0a 20 20 ue_to_error;..
1e18a 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 sqlite3BtreeSetC
1e18b 61 63 68 65 64 52 6f 77 69 64 28 75 2e 62 67 2e achedRowid(u.bg.
1e18c 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 29 3b pC->pCursor, 0);
1e18d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
1e18e 74 72 65 65 44 65 6c 65 74 65 28 75 2e 62 67 2e treeDelete(u.bg.
1e18f 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 pC->pCursor);.
1e190 75 2e 62 67 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.bg.pC->cacheSt
1e191 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
1e192 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 LE;.. /* Invoke
1e193 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b the update-hook
1e194 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f if required. */
1e195 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1e196 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 E_OK && db->xUpd
1e197 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 ateCallback && p
1e198 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 Op->p4.z ){.
1e199 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 const char *zDb
1e19a 3d 20 64 62 2d 3e 61 44 62 5b 75 2e 62 67 2e 70 = db->aDb[u.bg.p
1e19b 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 C->iDb].zName;.
1e19c 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1e19d 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b Tbl = pOp->p4.z;
1e19e 0a 20 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 . db->xUpdate
1e19f 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 Callback(db->pUp
1e1a0 64 61 74 65 41 72 67 2c 20 53 51 4c 49 54 45 5f dateArg, SQLITE_
1e1a1 44 45 4c 45 54 45 2c 20 7a 44 62 2c 20 7a 54 62 DELETE, zDb, zTb
1e1a2 6c 2c 20 75 2e 62 67 2e 69 4b 65 79 29 3b 0a 20 l, u.bg.iKey);.
1e1a3 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e assert( u.bg.
1e1a4 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 pC->iDb>=0 );.
1e1a5 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 }. if( pOp->p2
1e1a6 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 & OPFLAG_NCHANGE
1e1a7 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b ) p->nChange++;
1e1a8 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 2f 2a 20 4f . break;.}./* O
1e1a9 70 63 6f 64 65 3a 20 52 65 73 65 74 43 6f 75 6e pcode: ResetCoun
1e1aa 74 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a t * * * * *.**.*
1e1ab 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 * The value of t
1e1ac 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 he change counte
1e1ad 72 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74 r is copied to t
1e1ae 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 he database hand
1e1af 6c 65 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 le.** change cou
1e1b0 6e 74 65 72 20 28 72 65 74 75 72 6e 65 64 20 62 nter (returned b
1e1b1 79 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c y subsequent cal
1e1b2 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 68 ls to sqlite3_ch
1e1b3 61 6e 67 65 73 28 29 29 2e 0a 2a 2a 20 54 68 65 anges())..** The
1e1b4 6e 20 74 68 65 20 56 4d 73 20 69 6e 74 65 72 6e n the VMs intern
1e1b5 61 6c 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 al change counte
1e1b6 72 20 72 65 73 65 74 73 20 74 6f 20 30 2e 0a 2a r resets to 0..*
1e1b7 2a 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 * This is used b
1e1b8 79 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 y trigger progra
1e1b9 6d 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 ms..*/.case OP_R
1e1ba 65 73 65 74 43 6f 75 6e 74 3a 20 7b 0a 20 20 73 esetCount: {. s
1e1bb 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 qlite3VdbeSetCha
1e1bc 6e 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 nges(db, p->nCha
1e1bd 6e 67 65 29 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e nge);. p->nChan
1e1be 67 65 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b ge = 0;. break;
1e1bf 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 .}../* Opcode: R
1e1c0 6f 77 44 61 74 61 20 50 31 20 50 32 20 2a 20 2a owData P1 P2 * *
1e1c1 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 *.**.** Write i
1e1c2 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 nto register P2
1e1c3 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 the complete row
1e1c4 20 64 61 74 61 20 66 6f 72 20 63 75 72 73 6f 72 data for cursor
1e1c5 20 50 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 P1..** There is
1e1c6 20 6e 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69 no interpretati
1e1c7 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20 on of the data.
1e1c8 20 0a 2a 2a 20 49 74 20 69 73 20 6a 75 73 74 20 .** It is just
1e1c9 63 6f 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 copied onto the
1e1ca 50 32 20 72 65 67 69 73 74 65 72 20 65 78 61 63 P2 register exac
1e1cb 74 6c 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 tly as .** it is
1e1cc 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 found in the da
1e1cd 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
1e1ce 2a 2a 20 49 66 20 74 68 65 20 50 31 20 63 75 72 ** If the P1 cur
1e1cf 73 6f 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e sor must be poin
1e1d0 74 69 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 ting to a valid
1e1d1 72 6f 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 row (not a NULL
1e1d2 72 6f 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 row).** of a rea
1e1d3 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 l table, not a p
1e1d4 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a seudo-table..*/.
1e1d5 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65 /* Opcode: RowKe
1e1d6 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a y P1 P2 * * *.**
1e1d7 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72 .** Write into r
1e1d8 65 67 69 73 74 65 72 20 50 32 20 74 68 65 20 63 egister P2 the c
1e1d9 6f 6d 70 6c 65 74 65 20 72 6f 77 20 6b 65 79 20 omplete row key
1e1da 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a for cursor P1..*
1e1db 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 69 6e * There is no in
1e1dc 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 terpretation of
1e1dd 74 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20 54 the data. .** T
1e1de 68 65 20 6b 65 79 20 69 73 20 63 6f 70 69 65 64 he key is copied
1e1df 20 6f 6e 74 6f 20 74 68 65 20 50 33 20 72 65 67 onto the P3 reg
1e1e0 69 73 74 65 72 20 65 78 61 63 74 6c 79 20 61 73 ister exactly as
1e1e1 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e 64 .** it is found
1e1e2 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1e1e3 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 file..**.** If
1e1e4 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 the P1 cursor mu
1e1e5 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 st be pointing t
1e1e6 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 20 28 6e o a valid row (n
1e1e7 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a ot a NULL row).*
1e1e8 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c * of a real tabl
1e1e9 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d e, not a pseudo-
1e1ea 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f table..*/.case O
1e1eb 50 5f 52 6f 77 4b 65 79 3a 0a 63 61 73 65 20 4f P_RowKey:.case O
1e1ec 50 5f 52 6f 77 44 61 74 61 3a 20 7b 0a 23 69 66 P_RowData: {.#if
1e1ed 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1e1ee 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e1ef 6f 20 75 2e 62 68 20 2a 2f 0a 20 20 56 64 62 65 o u.bh */. Vdbe
1e1f0 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 Cursor *pC;. Bt
1e1f1 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 Cursor *pCrsr;.
1e1f2 20 75 33 32 20 6e 3b 0a 20 20 69 36 34 20 6e 36 u32 n;. i64 n6
1e1f3 34 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 4;.#endif /* loc
1e1f4 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e1f5 65 64 20 69 6e 74 6f 20 75 2e 62 68 20 2a 2f 0a ed into u.bh */.
1e1f6 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d . pOut = &p->aM
1e1f7 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a 20 20 em[pOp->p2];..
1e1f8 2f 2a 20 4e 6f 74 65 20 74 68 61 74 20 52 6f 77 /* Note that Row
1e1f9 4b 65 79 20 61 6e 64 20 52 6f 77 44 61 74 61 20 Key and RowData
1e1fa 61 72 65 20 72 65 61 6c 6c 79 20 65 78 61 63 74 are really exact
1e1fb 6c 79 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 ly the same inst
1e1fc 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 ruction */. ass
1e1fd 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1e1fe 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 && pOp->p1<p->nC
1e1ff 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 68 2e ursor );. u.bh.
1e200 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f pC = p->apCsr[pO
1e201 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1e202 28 20 75 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62 ( u.bh.pC->isTab
1e203 6c 65 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 le || pOp->opcod
1e204 65 3d 3d 4f 50 5f 52 6f 77 4b 65 79 20 29 3b 0a e==OP_RowKey );.
1e205 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 assert( u.bh.p
1e206 43 2d 3e 69 73 49 6e 64 65 78 20 7c 7c 20 70 4f C->isIndex || pO
1e207 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f p->opcode==OP_Ro
1e208 77 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 wData );. asser
1e209 74 28 20 75 2e 62 68 2e 70 43 21 3d 30 20 29 3b t( u.bh.pC!=0 );
1e20a 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e . assert( u.bh.
1e20b 70 43 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d 30 20 29 pC->nullRow==0 )
1e20c 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 ;. assert( u.bh
1e20d 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 .pC->pseudoTable
1e20e 52 65 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 Reg==0 );. asse
1e20f 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 70 43 75 rt( u.bh.pC->pCu
1e210 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20 75 2e 62 rsor!=0 );. u.b
1e211 68 2e 70 43 72 73 72 20 3d 20 75 2e 62 68 2e 70 h.pCrsr = u.bh.p
1e212 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61 73 C->pCursor;. as
1e213 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
1e214 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 eeCursorIsValid(
1e215 75 2e 62 68 2e 70 43 72 73 72 29 20 29 3b 0a 0a u.bh.pCrsr) );..
1e216 20 20 2f 2a 20 54 68 65 20 4f 50 5f 52 6f 77 4b /* The OP_RowK
1e217 65 79 20 61 6e 64 20 4f 50 5f 52 6f 77 44 61 74 ey and OP_RowDat
1e218 61 20 6f 70 63 6f 64 65 73 20 61 6c 77 61 79 73 a opcodes always
1e219 20 66 6f 6c 6c 6f 77 20 4f 50 5f 4e 6f 74 45 78 follow OP_NotEx
1e21a 69 73 74 73 20 6f 72 0a 20 20 2a 2a 20 4f 50 5f ists or. ** OP_
1e21b 52 65 77 69 6e 64 2f 4f 70 5f 4e 65 78 74 20 77 Rewind/Op_Next w
1e21c 69 74 68 20 6e 6f 20 69 6e 74 65 72 76 65 6e 69 ith no interveni
1e21d 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 ng instructions
1e21e 74 68 61 74 20 6d 69 67 68 74 20 69 6e 76 61 6c that might inval
1e21f 69 64 61 74 65 0a 20 20 2a 2a 20 74 68 65 20 63 idate. ** the c
1e220 75 72 73 6f 72 2e 20 20 48 65 6e 63 65 20 74 68 ursor. Hence th
1e221 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69 e following sqli
1e222 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 te3VdbeCursorMov
1e223 65 74 6f 28 29 20 63 61 6c 6c 20 69 73 20 61 6c eto() call is al
1e224 77 61 79 73 0a 20 20 2a 2a 20 61 20 6e 6f 2d 6f ways. ** a no-o
1e225 70 20 61 6e 64 20 63 61 6e 20 6e 65 76 65 72 20 p and can never
1e226 66 61 69 6c 2e 20 20 42 75 74 20 77 65 20 6c 65 fail. But we le
1e227 61 76 65 20 69 74 20 69 6e 20 70 6c 61 63 65 20 ave it in place
1e228 61 73 20 61 20 73 61 66 65 74 79 2e 0a 20 20 2a as a safety.. *
1e229 2f 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 /. assert( u.bh
1e22a 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 .pC->deferredMov
1e22b 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d eto==0 );. rc =
1e22c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 sqlite3VdbeCurs
1e22d 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 68 2e 70 43 orMoveto(u.bh.pC
1e22e 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 );. if( NEVER(r
1e22f 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 20 c!=SQLITE_OK) )
1e230 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1e231 6f 5f 65 72 72 6f 72 3b 0a 0a 20 20 69 66 28 20 o_error;.. if(
1e232 75 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78 u.bh.pC->isIndex
1e233 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1e234 21 75 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62 6c !u.bh.pC->isTabl
1e235 65 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 e );. rc = sq
1e236 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a lite3BtreeKeySiz
1e237 65 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 26 75 e(u.bh.pCrsr, &u
1e238 2e 62 68 2e 6e 36 34 29 3b 0a 20 20 20 20 61 73 .bh.n64);. as
1e239 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
1e23a 5f 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 54 72 75 _OK ); /* Tru
1e23b 65 20 62 65 63 61 75 73 65 20 6f 66 20 43 75 72 e because of Cur
1e23c 73 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c sorMoveto() call
1e23d 20 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 69 66 above */. if
1e23e 28 20 75 2e 62 68 2e 6e 36 34 3e 64 62 2d 3e 61 ( u.bh.n64>db->a
1e23f 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
1e240 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 IT_LENGTH] ){.
1e241 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 goto too_big
1e242 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 68 ;. }. u.bh
1e243 2e 6e 20 3d 20 28 75 33 32 29 75 2e 62 68 2e 6e .n = (u32)u.bh.n
1e244 36 34 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 64;. }else{.
1e245 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1e246 65 65 44 61 74 61 53 69 7a 65 28 75 2e 62 68 2e eeDataSize(u.bh.
1e247 70 43 72 73 72 2c 20 26 75 2e 62 68 2e 6e 29 3b pCrsr, &u.bh.n);
1e248 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d . assert( rc=
1e249 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 =SQLITE_OK );
1e24a 20 2f 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63 /* DataSize() c
1e24b 61 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 annot fail */.
1e24c 20 20 69 66 28 20 75 2e 62 68 2e 6e 3e 28 75 33 if( u.bh.n>(u3
1e24d 32 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 2)db->aLimit[SQL
1e24e 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 ITE_LIMIT_LENGTH
1e24f 5d 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 ] ){. goto
1e250 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 too_big;. }.
1e251 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
1e252 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 VdbeMemGrow(pOut
1e253 2c 20 75 2e 62 68 2e 6e 2c 20 30 29 20 29 7b 0a , u.bh.n, 0) ){.
1e254 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b goto no_mem;
1e255 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d . }. pOut->n =
1e256 20 75 2e 62 68 2e 6e 3b 0a 20 20 4d 65 6d 53 65 u.bh.n;. MemSe
1e257 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1e258 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 69 66 28 MEM_Blob);. if(
1e259 20 75 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 u.bh.pC->isInde
1e25a 78 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 x ){. rc = sq
1e25b 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 75 2e lite3BtreeKey(u.
1e25c 62 68 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62 bh.pCrsr, 0, u.b
1e25d 68 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 h.n, pOut->z);.
1e25e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d }else{. rc =
1e25f 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 sqlite3BtreeDat
1e260 61 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 30 2c a(u.bh.pCrsr, 0,
1e261 20 75 2e 62 68 2e 6e 2c 20 70 4f 75 74 2d 3e 7a u.bh.n, pOut->z
1e262 29 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 );. }. pOut->e
1e263 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 nc = SQLITE_UTF8
1e264 3b 20 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 ; /* In case th
1e265 65 20 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63 e blob is ever c
1e266 61 73 74 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20 ast to text */.
1e267 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 UPDATE_MAX_BLOB
1e268 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 SIZE(pOut);. br
1e269 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1e26a 65 3a 20 52 6f 77 69 64 20 50 31 20 50 32 20 2a e: Rowid P1 P2 *
1e26b 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 * *.**.** Store
1e26c 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 in register P2
1e26d 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 an integer which
1e26e 20 69 73 20 74 68 65 20 6b 65 79 20 6f 66 20 74 is the key of t
1e26f 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 74 he table entry t
1e270 68 61 74 0a 2a 2a 20 50 31 20 69 73 20 63 75 72 hat.** P1 is cur
1e271 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f 2e rently point to.
1e272 0a 2a 2a 0a 2a 2a 20 50 31 20 63 61 6e 20 62 65 .**.** P1 can be
1e273 20 65 69 74 68 65 72 20 61 6e 20 6f 72 64 69 6e either an ordin
1e274 61 72 79 20 74 61 62 6c 65 20 6f 72 20 61 20 76 ary table or a v
1e275 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 irtual table. T
1e276 68 65 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 here used to.**
1e277 62 65 20 61 20 73 65 70 61 72 61 74 65 20 4f 50 be a separate OP
1e278 5f 56 52 6f 77 69 64 20 6f 70 63 6f 64 65 20 66 _VRowid opcode f
1e279 6f 72 20 75 73 65 20 77 69 74 68 20 76 69 72 74 or use with virt
1e27a 75 61 6c 20 74 61 62 6c 65 73 2c 20 62 75 74 20 ual tables, but
1e27b 74 68 69 73 0a 2a 2a 20 6f 6e 65 20 6f 70 63 6f this.** one opco
1e27c 64 65 20 6e 6f 77 20 77 6f 72 6b 73 20 66 6f 72 de now works for
1e27d 20 62 6f 74 68 20 74 61 62 6c 65 20 74 79 70 65 both table type
1e27e 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f s..*/.case OP_Ro
1e27f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 wid: {
1e280 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 /* out2-p
1e281 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 rerelease */.#if
1e282 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1e283 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e284 6f 20 75 2e 62 69 20 2a 2f 0a 20 20 56 64 62 65 o u.bi */. Vdbe
1e285 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36 Cursor *pC;. i6
1e286 34 20 76 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 4 v;. sqlite3_v
1e287 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f tab *pVtab;. co
1e288 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 nst sqlite3_modu
1e289 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 23 65 6e le *pModule;.#en
1e28a 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e28b 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e28c 6f 20 75 2e 62 69 20 2a 2f 0a 0a 20 20 61 73 73 o u.bi */.. ass
1e28d 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1e28e 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 && pOp->p1<p->nC
1e28f 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 69 2e ursor );. u.bi.
1e290 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f pC = p->apCsr[pO
1e291 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1e292 28 20 75 2e 62 69 2e 70 43 21 3d 30 20 29 3b 0a ( u.bi.pC!=0 );.
1e293 20 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70 assert( u.bi.p
1e294 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 C->pseudoTableRe
1e295 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 75 2e g==0 );. if( u.
1e296 62 69 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 bi.pC->nullRow )
1e297 7b 0a 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 {. /* Do noth
1e298 69 6e 67 20 73 6f 20 74 68 61 74 20 72 65 67 5b ing so that reg[
1e299 50 32 5d 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c P2] remains NULL
1e29a 20 2a 2f 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 */. break;.
1e29b 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 62 69 2e }else if( u.bi.
1e29c 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 pC->deferredMove
1e29d 74 6f 20 29 7b 0a 20 20 20 20 75 2e 62 69 2e 76 to ){. u.bi.v
1e29e 20 3d 20 75 2e 62 69 2e 70 43 2d 3e 6d 6f 76 65 = u.bi.pC->move
1e29f 74 6f 54 61 72 67 65 74 3b 0a 23 69 66 6e 64 65 toTarget;.#ifnde
1e2a0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
1e2a1 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 7d 65 6c RTUALTABLE. }el
1e2a2 73 65 20 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e se if( u.bi.pC->
1e2a3 70 56 74 61 62 43 75 72 73 6f 72 20 29 7b 0a 20 pVtabCursor ){.
1e2a4 20 20 20 75 2e 62 69 2e 70 56 74 61 62 20 3d 20 u.bi.pVtab =
1e2a5 75 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62 43 75 u.bi.pC->pVtabCu
1e2a6 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 20 rsor->pVtab;.
1e2a7 20 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 20 3d 20 u.bi.pModule =
1e2a8 75 2e 62 69 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 u.bi.pVtab->pMod
1e2a9 75 6c 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ule;. assert(
1e2aa 20 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 u.bi.pModule->x
1e2ab 52 6f 77 69 64 20 29 3b 0a 20 20 20 20 69 66 28 Rowid );. if(
1e2ac 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1e2ad 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1e2ae 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1e2af 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 62 69 2e ;. rc = u.bi.
1e2b0 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 28 pModule->xRowid(
1e2b1 75 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62 43 75 u.bi.pC->pVtabCu
1e2b2 72 73 6f 72 2c 20 26 75 2e 62 69 2e 76 29 3b 0a rsor, &u.bi.v);.
1e2b3 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1e2b4 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 e(db, p->zErrMsg
1e2b5 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 );. p->zErrMs
1e2b6 67 20 3d 20 75 2e 62 69 2e 70 56 74 61 62 2d 3e g = u.bi.pVtab->
1e2b7 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 62 zErrMsg;. u.b
1e2b8 69 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 i.pVtab->zErrMsg
1e2b9 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 = 0;. if( sq
1e2ba 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 lite3SafetyOn(db
1e2bb 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 ) ) goto abort_d
1e2bc 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 23 65 ue_to_misuse;.#e
1e2bd 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1e2be 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1e2bf 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 */. }else{.
1e2c0 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70 43 assert( u.bi.pC
1e2c1 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a ->pCursor!=0 );.
1e2c2 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1e2c3 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f VdbeCursorMoveto
1e2c4 28 75 2e 62 69 2e 70 43 29 3b 0a 20 20 20 20 69 (u.bi.pC);. i
1e2c5 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f f( rc ) goto abo
1e2c6 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
1e2c7 0a 20 20 20 20 69 66 28 20 75 2e 62 69 2e 70 43 . if( u.bi.pC
1e2c8 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29 ->rowidIsValid )
1e2c9 7b 0a 20 20 20 20 20 20 75 2e 62 69 2e 76 20 3d {. u.bi.v =
1e2ca 20 75 2e 62 69 2e 70 43 2d 3e 6c 61 73 74 52 6f u.bi.pC->lastRo
1e2cb 77 69 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a wid;. }else{.
1e2cc 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1e2cd 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 75 e3BtreeKeySize(u
1e2ce 2e 62 69 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c .bi.pC->pCursor,
1e2cf 20 26 75 2e 62 69 2e 76 29 3b 0a 20 20 20 20 20 &u.bi.v);.
1e2d0 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
1e2d1 49 54 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 41 6c ITE_OK ); /* Al
1e2d2 77 61 79 73 20 73 6f 20 62 65 63 61 75 73 65 20 ways so because
1e2d3 6f 66 20 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 of CursorMoveto(
1e2d4 29 20 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 7d ) above */. }
1e2d5 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 . }. pOut->u.i
1e2d6 20 3d 20 75 2e 62 69 2e 76 3b 0a 20 20 4d 65 6d = u.bi.v;. Mem
1e2d7 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 SetTypeFlag(pOut
1e2d8 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 , MEM_Int);. br
1e2d9 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1e2da 65 3a 20 4e 75 6c 6c 52 6f 77 20 50 31 20 2a 20 e: NullRow P1 *
1e2db 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 * * *.**.** Move
1e2dc 20 74 68 65 20 63 75 72 73 6f 72 20 50 31 20 74 the cursor P1 t
1e2dd 6f 20 61 20 6e 75 6c 6c 20 72 6f 77 2e 20 20 41 o a null row. A
1e2de 6e 79 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 65 ny OP_Column ope
1e2df 72 61 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 rations.** that
1e2e0 6f 63 63 75 72 20 77 68 69 6c 65 20 74 68 65 20 occur while the
1e2e1 63 75 72 73 6f 72 20 69 73 20 6f 6e 20 74 68 65 cursor is on the
1e2e2 20 6e 75 6c 6c 20 72 6f 77 20 77 69 6c 6c 20 61 null row will a
1e2e3 6c 77 61 79 73 0a 2a 2a 20 77 72 69 74 65 20 61 lways.** write a
1e2e4 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f NULL..*/.case O
1e2e5 50 5f 4e 75 6c 6c 52 6f 77 3a 20 7b 0a 23 69 66 P_NullRow: {.#if
1e2e6 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1e2e7 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e2e8 6f 20 75 2e 62 6a 20 2a 2f 0a 20 20 56 64 62 65 o u.bj */. Vdbe
1e2e9 43 75 72 73 6f 72 20 2a 70 43 3b 0a 23 65 6e 64 Cursor *pC;.#end
1e2ea 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1e2eb 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e2ec 20 75 2e 62 6a 20 2a 2f 0a 0a 20 20 61 73 73 65 u.bj */.. asse
1e2ed 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1e2ee 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1e2ef 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6a 2e 70 rsor );. u.bj.p
1e2f0 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 C = p->apCsr[pOp
1e2f1 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1e2f2 20 75 2e 62 6a 2e 70 43 21 3d 30 20 29 3b 0a 20 u.bj.pC!=0 );.
1e2f3 20 75 2e 62 6a 2e 70 43 2d 3e 6e 75 6c 6c 52 6f u.bj.pC->nullRo
1e2f4 77 20 3d 20 31 3b 0a 20 20 75 2e 62 6a 2e 70 43 w = 1;. u.bj.pC
1e2f5 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d ->rowidIsValid =
1e2f6 20 30 3b 0a 20 20 69 66 28 20 75 2e 62 6a 2e 70 0;. if( u.bj.p
1e2f7 43 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 C->pCursor ){.
1e2f8 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c sqlite3BtreeCl
1e2f9 65 61 72 43 75 72 73 6f 72 28 75 2e 62 6a 2e 70 earCursor(u.bj.p
1e2fa 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d C->pCursor);. }
1e2fb 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1e2fc 4f 70 63 6f 64 65 3a 20 4c 61 73 74 20 50 31 20 Opcode: Last P1
1e2fd 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 P2 * * *.**.** T
1e2fe 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20 74 he next use of t
1e2ff 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75 he Rowid or Colu
1e300 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 72 mn or Next instr
1e301 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a uction for P1 .*
1e302 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f 20 * will refer to
1e303 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 the last entry i
1e304 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 n the database t
1e305 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a able or index..*
1e306 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6f * If the table o
1e307 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79 r index is empty
1e308 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e 20 and P2>0, then
1e309 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 jump immediately
1e30a 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 32 to P2..** If P2
1e30b 20 69 73 20 30 20 6f 72 20 69 66 20 74 68 65 20 is 0 or if the
1e30c 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 table or index i
1e30d 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c s not empty, fal
1e30e 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 l through.** to
1e30f 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e the following in
1e310 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 struction..*/.ca
1e311 73 65 20 4f 50 5f 4c 61 73 74 3a 20 7b 20 20 20 se OP_Last: {
1e312 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a /* jump */.
1e313 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1e314 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1e315 69 6e 74 6f 20 75 2e 62 6b 20 2a 2f 0a 20 20 56 into u.bk */. V
1e316 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 dbeCursor *pC;.
1e317 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1e318 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 23 65 6e ;. int res;.#en
1e319 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e31a 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e31b 6f 20 75 2e 62 6b 20 2a 2f 0a 0a 20 20 61 73 73 o u.bk */.. ass
1e31c 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1e31d 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 && pOp->p1<p->nC
1e31e 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6b 2e ursor );. u.bk.
1e31f 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f pC = p->apCsr[pO
1e320 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1e321 28 20 75 2e 62 6b 2e 70 43 21 3d 30 20 29 3b 0a ( u.bk.pC!=0 );.
1e322 20 20 75 2e 62 6b 2e 70 43 72 73 72 20 3d 20 75 u.bk.pCrsr = u
1e323 2e 62 6b 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b .bk.pC->pCursor;
1e324 0a 20 20 69 66 28 20 75 2e 62 6b 2e 70 43 72 73 . if( u.bk.pCrs
1e325 72 3d 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62 6b r==0 ){. u.bk
1e326 2e 72 65 73 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 .res = 1;. }els
1e327 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 e{. rc = sqli
1e328 74 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e 62 te3BtreeLast(u.b
1e329 6b 2e 70 43 72 73 72 2c 20 26 75 2e 62 6b 2e 72 k.pCrsr, &u.bk.r
1e32a 65 73 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 6b 2e es);. }. u.bk.
1e32b 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 pC->nullRow = (u
1e32c 38 29 75 2e 62 6b 2e 72 65 73 3b 0a 20 20 75 2e 8)u.bk.res;. u.
1e32d 62 6b 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d bk.pC->deferredM
1e32e 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 75 2e 62 oveto = 0;. u.b
1e32f 6b 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c k.pC->rowidIsVal
1e330 69 64 20 3d 20 30 3b 0a 20 20 75 2e 62 6b 2e 70 id = 0;. u.bk.p
1e331 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d C->cacheStatus =
1e332 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 CACHE_STALE;.
1e333 69 66 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 if( pOp->p2>0 &&
1e334 20 75 2e 62 6b 2e 72 65 73 20 29 7b 0a 20 20 20 u.bk.res ){.
1e335 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1e336 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 1;. }. break;.
1e337 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 }.../* Opcode: S
1e338 6f 72 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a ort P1 P2 * * *.
1e339 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 **.** This opcod
1e33a 65 20 64 6f 65 73 20 65 78 61 63 74 6c 79 20 74 e does exactly t
1e33b 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 he same thing as
1e33c 20 4f 50 5f 52 65 77 69 6e 64 20 65 78 63 65 70 OP_Rewind excep
1e33d 74 20 74 68 61 74 0a 2a 2a 20 69 74 20 69 6e 63 t that.** it inc
1e33e 72 65 6d 65 6e 74 73 20 61 6e 20 75 6e 64 6f 63 rements an undoc
1e33f 75 6d 65 6e 74 65 64 20 67 6c 6f 62 61 6c 20 76 umented global v
1e340 61 72 69 61 62 6c 65 20 75 73 65 64 20 66 6f 72 ariable used for
1e341 20 74 65 73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 testing..**.**
1e342 53 6f 72 74 69 6e 67 20 69 73 20 61 63 63 6f 6d Sorting is accom
1e343 70 6c 69 73 68 65 64 20 62 79 20 77 72 69 74 69 plished by writi
1e344 6e 67 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 ng records into
1e345 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c a sorting index,
1e346 0a 2a 2a 20 74 68 65 6e 20 72 65 77 69 6e 64 69 .** then rewindi
1e347 6e 67 20 74 68 61 74 20 69 6e 64 65 78 20 61 6e ng that index an
1e348 64 20 70 6c 61 79 69 6e 67 20 69 74 20 62 61 63 d playing it bac
1e349 6b 20 66 72 6f 6d 20 62 65 67 69 6e 6e 69 6e 67 k from beginning
1e34a 20 74 6f 0a 2a 2a 20 65 6e 64 2e 20 20 57 65 20 to.** end. We
1e34b 75 73 65 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 use the OP_Sort
1e34c 6f 70 63 6f 64 65 20 69 6e 73 74 65 61 64 20 6f opcode instead o
1e34d 66 20 4f 50 5f 52 65 77 69 6e 64 20 74 6f 20 64 f OP_Rewind to d
1e34e 6f 20 74 68 65 0a 2a 2a 20 72 65 77 69 6e 64 69 o the.** rewindi
1e34f 6e 67 20 73 6f 20 74 68 61 74 20 74 68 65 20 67 ng so that the g
1e350 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 77 lobal variable w
1e351 69 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e 74 ill be increment
1e352 65 64 20 61 6e 64 0a 2a 2a 20 72 65 67 72 65 73 ed and.** regres
1e353 73 69 6f 6e 20 74 65 73 74 73 20 63 61 6e 20 64 sion tests can d
1e354 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 etermine whether
1e355 20 6f 72 20 6e 6f 74 20 74 68 65 20 6f 70 74 69 or not the opti
1e356 6d 69 7a 65 72 20 69 73 0a 2a 2a 20 63 6f 72 72 mizer is.** corr
1e357 65 63 74 6c 79 20 6f 70 74 69 6d 69 7a 69 6e 67 ectly optimizing
1e358 20 6f 75 74 20 73 6f 72 74 73 2e 0a 2a 2f 0a 63 out sorts..*/.c
1e359 61 73 65 20 4f 50 5f 53 6f 72 74 3a 20 7b 20 20 ase OP_Sort: {
1e35a 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f /* jump */
1e35b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
1e35c 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f EST. sqlite3_so
1e35d 72 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73 71 rt_count++;. sq
1e35e 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 lite3_search_cou
1e35f 6e 74 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20 20 70 nt--;.#endif. p
1e360 2d 3e 61 43 6f 75 6e 74 65 72 5b 53 51 4c 49 54 ->aCounter[SQLIT
1e361 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 E_STMTSTATUS_SOR
1e362 54 2d 31 5d 2b 2b 3b 0a 20 20 2f 2a 20 46 61 6c T-1]++;. /* Fal
1e363 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f l through into O
1e364 50 5f 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a 2f 2a P_Rewind */.}./*
1e365 20 4f 70 63 6f 64 65 3a 20 52 65 77 69 6e 64 20 Opcode: Rewind
1e366 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1e367 2a 20 54 68 65 20 6e 65 78 74 20 75 73 65 20 6f * The next use o
1e368 66 20 74 68 65 20 52 6f 77 69 64 20 6f 72 20 43 f the Rowid or C
1e369 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e olumn or Next in
1e36a 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 struction for P1
1e36b 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 .** will refer
1e36c 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 to the first ent
1e36d 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ry in the databa
1e36e 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 se table or inde
1e36f 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 x..** If the tab
1e370 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20 65 le or index is e
1e371 6d 70 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 74 mpty and P2>0, t
1e372 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 hen jump immedia
1e373 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 tely to P2..** I
1e374 66 20 50 32 20 69 73 20 30 20 6f 72 20 69 66 20 f P2 is 0 or if
1e375 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 the table or ind
1e376 65 78 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c ex is not empty,
1e377 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a fall through.**
1e378 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e to the followin
1e379 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a g instruction..*
1e37a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 77 69 6e 64 /.case OP_Rewind
1e37b 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 : { /* ju
1e37c 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 mp */.#if 0 /*
1e37d 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e37e 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6c 20 moved into u.bl
1e37f 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 */. VdbeCursor
1e380 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 *pC;. BtCursor
1e381 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 *pCrsr;. int re
1e382 73 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 s;.#endif /* loc
1e383 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e384 65 64 20 69 6e 74 6f 20 75 2e 62 6c 20 2a 2f 0a ed into u.bl */.
1e385 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e386 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1e387 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1e388 20 75 2e 62 6c 2e 70 43 20 3d 20 70 2d 3e 61 70 u.bl.pC = p->ap
1e389 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1e38a 61 73 73 65 72 74 28 20 75 2e 62 6c 2e 70 43 21 assert( u.bl.pC!
1e38b 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 75 2e 62 =0 );. if( (u.b
1e38c 6c 2e 70 43 72 73 72 20 3d 20 75 2e 62 6c 2e 70 l.pCrsr = u.bl.p
1e38d 43 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 C->pCursor)!=0 )
1e38e 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1e38f 65 33 42 74 72 65 65 46 69 72 73 74 28 75 2e 62 e3BtreeFirst(u.b
1e390 6c 2e 70 43 72 73 72 2c 20 26 75 2e 62 6c 2e 72 l.pCrsr, &u.bl.r
1e391 65 73 29 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43 es);. u.bl.pC
1e392 2d 3e 61 74 46 69 72 73 74 20 3d 20 75 2e 62 6c ->atFirst = u.bl
1e393 2e 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 .res==0 ?1:0;.
1e394 20 20 75 2e 62 6c 2e 70 43 2d 3e 64 65 66 65 72 u.bl.pC->defer
1e395 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 redMoveto = 0;.
1e396 20 20 20 75 2e 62 6c 2e 70 43 2d 3e 63 61 63 68 u.bl.pC->cach
1e397 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f eStatus = CACHE_
1e398 53 54 41 4c 45 3b 0a 20 20 20 20 75 2e 62 6c 2e STALE;. u.bl.
1e399 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 pC->rowidIsValid
1e39a 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 0;. }else{.
1e39b 20 20 20 75 2e 62 6c 2e 72 65 73 20 3d 20 31 3b u.bl.res = 1;
1e39c 0a 20 20 7d 0a 20 20 75 2e 62 6c 2e 70 43 2d 3e . }. u.bl.pC->
1e39d 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e nullRow = (u8)u.
1e39e 62 6c 2e 72 65 73 3b 0a 20 20 61 73 73 65 72 74 bl.res;. assert
1e39f 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 ( pOp->p2>0 && p
1e3a0 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20 29 3b Op->p2<p->nOp );
1e3a1 0a 20 20 69 66 28 20 75 2e 62 6c 2e 72 65 73 20 . if( u.bl.res
1e3a2 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d ){. pc = pOp-
1e3a3 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p2 - 1;. }. b
1e3a4 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1e3a5 64 65 3a 20 4e 65 78 74 20 50 31 20 50 32 20 2a de: Next P1 P2 *
1e3a6 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e * *.**.** Advan
1e3a7 63 65 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 ce cursor P1 so
1e3a8 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 that it points t
1e3a9 6f 20 74 68 65 20 6e 65 78 74 20 6b 65 79 2f 64 o the next key/d
1e3aa 61 74 61 20 70 61 69 72 20 69 6e 20 69 74 73 0a ata pair in its.
1e3ab 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 ** table or inde
1e3ac 78 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 x. If there are
1e3ad 20 6e 6f 20 6d 6f 72 65 20 6b 65 79 2f 76 61 6c no more key/val
1e3ae 75 65 20 70 61 69 72 73 20 74 68 65 6e 20 66 61 ue pairs then fa
1e3af 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f ll through.** to
1e3b0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 the following i
1e3b1 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 nstruction. But
1e3b2 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 if the cursor a
1e3b3 64 76 61 6e 63 65 20 77 61 73 20 73 75 63 63 65 dvance was succe
1e3b4 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 ssful,.** jump i
1e3b5 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 mmediately to P2
1e3b6 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 ..**.** The P1 c
1e3b7 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f ursor must be fo
1e3b8 72 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 r a real table,
1e3b9 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 not a pseudo-tab
1e3ba 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c le..**.** See al
1e3bb 73 6f 3a 20 50 72 65 76 0a 2a 2f 0a 2f 2a 20 4f so: Prev.*/./* O
1e3bc 70 63 6f 64 65 3a 20 50 72 65 76 20 50 31 20 50 pcode: Prev P1 P
1e3bd 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 61 2 * * *.**.** Ba
1e3be 63 6b 20 75 70 20 63 75 72 73 6f 72 20 50 31 20 ck up cursor P1
1e3bf 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 so that it point
1e3c0 73 20 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75 s to the previou
1e3c1 73 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20 s key/data pair
1e3c2 69 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 in its.** table
1e3c3 6f 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68 or index. If th
1e3c4 65 72 65 20 69 73 20 6e 6f 20 70 72 65 76 69 6f ere is no previo
1e3c5 75 73 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 us key/value pai
1e3c6 72 73 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 rs then fall thr
1e3c7 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 ough.** to the f
1e3c8 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 ollowing instruc
1e3c9 74 69 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 68 tion. But if th
1e3ca 65 20 63 75 72 73 6f 72 20 62 61 63 6b 75 70 20 e cursor backup
1e3cb 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 2c 0a was successful,.
1e3cc 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 ** jump immediat
1e3cd 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a ely to P2..**.**
1e3ce 20 54 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d The P1 cursor m
1e3cf 75 73 74 20 62 65 20 66 6f 72 20 61 20 72 65 61 ust be for a rea
1e3d0 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 l table, not a p
1e3d1 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a seudo-table..*/.
1e3d2 63 61 73 65 20 4f 50 5f 50 72 65 76 3a 20 20 20 case OP_Prev:
1e3d3 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a /* jump *
1e3d4 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 78 74 3a 20 /.case OP_Next:
1e3d5 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 { /* jump
1e3d6 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f */.#if 0 /* lo
1e3d7 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1e3d8 76 65 64 20 69 6e 74 6f 20 75 2e 62 6d 20 2a 2f ved into u.bm */
1e3d9 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1e3da 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 C;. BtCursor *p
1e3db 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b Crsr;. int res;
1e3dc 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1e3dd 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1e3de 20 69 6e 74 6f 20 75 2e 62 6d 20 2a 2f 0a 0a 20 into u.bm */..
1e3df 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 CHECK_FOR_INTER
1e3e0 52 55 50 54 3b 0a 20 20 61 73 73 65 72 74 28 20 RUPT;. assert(
1e3e1 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f pOp->p1>=0 && pO
1e3e2 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 p->p1<p->nCursor
1e3e3 20 29 3b 0a 20 20 75 2e 62 6d 2e 70 43 20 3d 20 );. u.bm.pC =
1e3e4 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 p->apCsr[pOp->p1
1e3e5 5d 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e 70 43 ];. if( u.bm.pC
1e3e6 3d 3d 30 20 29 7b 0a 20 20 20 20 62 72 65 61 6b ==0 ){. break
1e3e7 3b 20 20 2f 2a 20 53 65 65 20 74 69 63 6b 65 74 ; /* See ticket
1e3e8 20 23 32 32 37 33 20 2a 2f 0a 20 20 7d 0a 20 20 #2273 */. }.
1e3e9 75 2e 62 6d 2e 70 43 72 73 72 20 3d 20 75 2e 62 u.bm.pCrsr = u.b
1e3ea 6d 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 m.pC->pCursor;.
1e3eb 20 69 66 28 20 75 2e 62 6d 2e 70 43 72 73 72 3d if( u.bm.pCrsr=
1e3ec 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62 6d 2e 70 =0 ){. u.bm.p
1e3ed 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a C->nullRow = 1;.
1e3ee 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 break;. }.
1e3ef 20 75 2e 62 6d 2e 72 65 73 20 3d 20 31 3b 0a 20 u.bm.res = 1;.
1e3f0 20 61 73 73 65 72 74 28 20 75 2e 62 6d 2e 70 43 assert( u.bm.pC
1e3f1 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f ->deferredMoveto
1e3f2 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 70 4f ==0 );. rc = pO
1e3f3 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 65 p->opcode==OP_Ne
1e3f4 78 74 20 3f 20 73 71 6c 69 74 65 33 42 74 72 65 xt ? sqlite3Btre
1e3f5 65 4e 65 78 74 28 75 2e 62 6d 2e 70 43 72 73 72 eNext(u.bm.pCrsr
1e3f6 2c 20 26 75 2e 62 6d 2e 72 65 73 29 20 3a 0a 20 , &u.bm.res) :.
1e3f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e3f8 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
1e3f9 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 ite3BtreePreviou
1e3fa 73 28 75 2e 62 6d 2e 70 43 72 73 72 2c 20 26 75 s(u.bm.pCrsr, &u
1e3fb 2e 62 6d 2e 72 65 73 29 3b 0a 20 20 75 2e 62 6d .bm.res);. u.bm
1e3fc 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 .pC->nullRow = (
1e3fd 75 38 29 75 2e 62 6d 2e 72 65 73 3b 0a 20 20 75 u8)u.bm.res;. u
1e3fe 2e 62 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 .bm.pC->cacheSta
1e3ff 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c tus = CACHE_STAL
1e400 45 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e 72 65 E;. if( u.bm.re
1e401 73 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d s==0 ){. pc =
1e402 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1e403 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 20 if( pOp->p5 )
1e404 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 70 4f 70 2d p->aCounter[pOp-
1e405 3e 70 35 2d 31 5d 2b 2b 3b 0a 23 69 66 64 65 66 >p5-1]++;.#ifdef
1e406 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 SQLITE_TEST.
1e407 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f sqlite3_search_
1e408 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a count++;.#endif.
1e409 20 20 7d 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e 72 }. u.bm.pC->r
1e40a 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b owidIsValid = 0;
1e40b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1e40c 4f 70 63 6f 64 65 3a 20 49 64 78 49 6e 73 65 72 Opcode: IdxInser
1e40d 74 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a t P1 P2 P3 * P5.
1e40e 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 **.** Register P
1e40f 32 20 68 6f 6c 64 73 20 61 20 53 51 4c 20 69 6e 2 holds a SQL in
1e410 64 65 78 20 6b 65 79 20 6d 61 64 65 20 75 73 69 dex key made usi
1e411 6e 67 20 74 68 65 0a 2a 2a 20 4d 61 6b 65 52 65 ng the.** MakeRe
1e412 63 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f 6e cord instruction
1e413 73 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 s. This opcode
1e414 77 72 69 74 65 73 20 74 68 61 74 20 6b 65 79 0a writes that key.
1e415 2a 2a 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 65 ** into the inde
1e416 78 20 50 31 2e 20 20 44 61 74 61 20 66 6f 72 20 x P1. Data for
1e417 74 68 65 20 65 6e 74 72 79 20 69 73 20 6e 69 6c the entry is nil
1e418 2e 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 61 20 ..**.** P3 is a
1e419 66 6c 61 67 20 74 68 61 74 20 70 72 6f 76 69 64 flag that provid
1e41a 65 73 20 61 20 68 69 6e 74 20 74 6f 20 74 68 65 es a hint to the
1e41b 20 62 2d 74 72 65 65 20 6c 61 79 65 72 20 74 68 b-tree layer th
1e41c 61 74 20 74 68 69 73 0a 2a 2a 20 69 6e 73 65 72 at this.** inser
1e41d 74 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 t is likely to b
1e41e 65 20 61 6e 20 61 70 70 65 6e 64 2e 0a 2a 2a 0a e an append..**.
1e41f 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 ** This instruct
1e420 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 ion only works f
1e421 6f 72 20 69 6e 64 69 63 65 73 2e 20 20 54 68 65 or indices. The
1e422 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 74 equivalent inst
1e423 72 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 ruction.** for t
1e424 61 62 6c 65 73 20 69 73 20 4f 50 5f 49 6e 73 65 ables is OP_Inse
1e425 72 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 rt..*/.case OP_I
1e426 64 78 49 6e 73 65 72 74 3a 20 7b 20 20 20 20 20 dxInsert: {
1e427 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 66 /* in2 */.#if
1e428 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1e429 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e42a 6f 20 75 2e 62 6e 20 2a 2f 0a 20 20 56 64 62 65 o u.bn */. Vdbe
1e42b 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 Cursor *pC;. Bt
1e42c 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 Cursor *pCrsr;.
1e42d 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 20 63 6f 6e int nKey;. con
1e42e 73 74 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 23 st char *zKey;.#
1e42f 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1e430 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1e431 6e 74 6f 20 75 2e 62 6e 20 2a 2f 0a 0a 20 20 61 nto u.bn */.. a
1e432 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d ssert( pOp->p1>=
1e433 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 0 && pOp->p1<p->
1e434 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 nCursor );. u.b
1e435 6e 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b n.pC = p->apCsr[
1e436 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 pOp->p1];. asse
1e437 72 74 28 20 75 2e 62 6e 2e 70 43 21 3d 30 20 29 rt( u.bn.pC!=0 )
1e438 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 32 ;. assert( pIn2
1e439 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c ->flags & MEM_Bl
1e43a 6f 62 20 29 3b 0a 20 20 75 2e 62 6e 2e 70 43 72 ob );. u.bn.pCr
1e43b 73 72 20 3d 20 75 2e 62 6e 2e 70 43 2d 3e 70 43 sr = u.bn.pC->pC
1e43c 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57 ursor;. if( ALW
1e43d 41 59 53 28 75 2e 62 6e 2e 70 43 72 73 72 21 3d AYS(u.bn.pCrsr!=
1e43e 30 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 0) ){. assert
1e43f 28 20 75 2e 62 6e 2e 70 43 2d 3e 69 73 54 61 62 ( u.bn.pC->isTab
1e440 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 le==0 );. rc
1e441 3d 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e = ExpandBlob(pIn
1e442 32 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 2);. if( rc==
1e443 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1e444 20 20 20 75 2e 62 6e 2e 6e 4b 65 79 20 3d 20 70 u.bn.nKey = p
1e445 49 6e 32 2d 3e 6e 3b 0a 20 20 20 20 20 20 75 2e In2->n;. u.
1e446 62 6e 2e 7a 4b 65 79 20 3d 20 70 49 6e 32 2d 3e bn.zKey = pIn2->
1e447 7a 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 z;. rc = sq
1e448 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 lite3BtreeInsert
1e449 28 75 2e 62 6e 2e 70 43 72 73 72 2c 20 75 2e 62 (u.bn.pCrsr, u.b
1e44a 6e 2e 7a 4b 65 79 2c 20 75 2e 62 6e 2e 6e 4b 65 n.zKey, u.bn.nKe
1e44b 79 2c 20 22 22 2c 20 30 2c 20 30 2c 20 70 4f 70 y, "", 0, 0, pOp
1e44c 2d 3e 70 33 2c 0a 20 20 20 20 20 20 20 20 20 20 ->p3,.
1e44d 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c ((pOp->p5 & OPFL
1e44e 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 AG_USESEEKRESULT
1e44f 29 20 3f 20 75 2e 62 6e 2e 70 43 2d 3e 73 65 65 ) ? u.bn.pC->see
1e450 6b 52 65 73 75 6c 74 20 3a 20 30 29 0a 20 20 20 kResult : 0).
1e451 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 );. asse
1e452 72 74 28 20 75 2e 62 6e 2e 70 43 2d 3e 64 65 66 rt( u.bn.pC->def
1e453 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 erredMoveto==0 )
1e454 3b 0a 20 20 20 20 20 20 75 2e 62 6e 2e 70 43 2d ;. u.bn.pC-
1e455 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 >cacheStatus = C
1e456 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 ACHE_STALE;.
1e457 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d }. }. break;.}
1e458 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 ../* Opcode: Idx
1e459 44 65 6c 65 74 65 20 50 31 20 50 32 20 50 33 20 Delete P1 P2 P3
1e45a 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f * *.**.** The co
1e45b 6e 74 65 6e 74 20 6f 66 20 50 33 20 72 65 67 69 ntent of P3 regi
1e45c 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 61 sters starting a
1e45d 74 20 72 65 67 69 73 74 65 72 20 50 32 20 66 6f t register P2 fo
1e45e 72 6d 0a 2a 2a 20 61 6e 20 75 6e 70 61 63 6b 65 rm.** an unpacke
1e45f 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 54 68 69 d index key. Thi
1e460 73 20 6f 70 63 6f 64 65 20 72 65 6d 6f 76 65 73 s opcode removes
1e461 20 74 68 61 74 20 65 6e 74 72 79 20 66 72 6f 6d that entry from
1e462 20 74 68 65 20 0a 2a 2a 20 69 6e 64 65 78 20 6f the .** index o
1e463 70 65 6e 65 64 20 62 79 20 63 75 72 73 6f 72 20 pened by cursor
1e464 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 P1..*/.case OP_I
1e465 64 78 44 65 6c 65 74 65 3a 20 7b 0a 23 69 66 20 dxDelete: {.#if
1e466 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e467 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e468 20 75 2e 62 6f 20 2a 2f 0a 20 20 56 64 62 65 43 u.bo */. VdbeC
1e469 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 ursor *pC;. BtC
1e46a 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 ursor *pCrsr;.
1e46b 69 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 int res;. Unpac
1e46c 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e kedRecord r;.#en
1e46d 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e46e 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e46f 6f 20 75 2e 62 6f 20 2a 2f 0a 0a 20 20 61 73 73 o u.bo */.. ass
1e470 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 ert( pOp->p3>0 )
1e471 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1e472 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 >p2>0 && pOp->p2
1e473 2b 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 +pOp->p3<=p->nMe
1e474 6d 2b 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 m+1 );. assert(
1e475 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 pOp->p1>=0 && p
1e476 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f Op->p1<p->nCurso
1e477 72 20 29 3b 0a 20 20 75 2e 62 6f 2e 70 43 20 3d r );. u.bo.pC =
1e478 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 p->apCsr[pOp->p
1e479 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 1];. assert( u.
1e47a 62 6f 2e 70 43 21 3d 30 20 29 3b 0a 20 20 75 2e bo.pC!=0 );. u.
1e47b 62 6f 2e 70 43 72 73 72 20 3d 20 75 2e 62 6f 2e bo.pCrsr = u.bo.
1e47c 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 pC->pCursor;. i
1e47d 66 28 20 41 4c 57 41 59 53 28 75 2e 62 6f 2e 70 f( ALWAYS(u.bo.p
1e47e 43 72 73 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 Crsr!=0) ){.
1e47f 75 2e 62 6f 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 u.bo.r.pKeyInfo
1e480 3d 20 75 2e 62 6f 2e 70 43 2d 3e 70 4b 65 79 49 = u.bo.pC->pKeyI
1e481 6e 66 6f 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e nfo;. u.bo.r.
1e482 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f nField = (u16)pO
1e483 70 2d 3e 70 33 3b 0a 20 20 20 20 75 2e 62 6f 2e p->p3;. u.bo.
1e484 72 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 r.flags = 0;.
1e485 20 75 2e 62 6f 2e 72 2e 61 4d 65 6d 20 3d 20 26 u.bo.r.aMem = &
1e486 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d p->aMem[pOp->p2]
1e487 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1e488 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 e3BtreeMovetoUnp
1e489 61 63 6b 65 64 28 75 2e 62 6f 2e 70 43 72 73 72 acked(u.bo.pCrsr
1e48a 2c 20 26 75 2e 62 6f 2e 72 2c 20 30 2c 20 30 2c , &u.bo.r, 0, 0,
1e48b 20 26 75 2e 62 6f 2e 72 65 73 29 3b 0a 20 20 20 &u.bo.res);.
1e48c 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1e48d 4f 4b 20 26 26 20 75 2e 62 6f 2e 72 65 73 3d 3d OK && u.bo.res==
1e48e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
1e48f 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 sqlite3BtreeDele
1e490 74 65 28 75 2e 62 6f 2e 70 43 72 73 72 29 3b 0a te(u.bo.pCrsr);.
1e491 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
1e492 28 20 75 2e 62 6f 2e 70 43 2d 3e 64 65 66 65 72 ( u.bo.pC->defer
1e493 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a redMoveto==0 );.
1e494 20 20 20 20 75 2e 62 6f 2e 70 43 2d 3e 63 61 63 u.bo.pC->cac
1e495 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 heStatus = CACHE
1e496 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72 _STALE;. }. br
1e497 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1e498 65 3a 20 49 64 78 52 6f 77 69 64 20 50 31 20 50 e: IdxRowid P1 P
1e499 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 2 * * *.**.** Wr
1e49a 69 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 ite into registe
1e49b 72 20 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20 r P2 an integer
1e49c 77 68 69 63 68 20 69 73 20 74 68 65 20 6c 61 73 which is the las
1e49d 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 t entry in the r
1e49e 65 63 6f 72 64 20 61 74 0a 2a 2a 20 74 68 65 20 ecord at.** the
1e49f 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 end of the index
1e4a0 20 6b 65 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 key pointed to
1e4a1 62 79 20 63 75 72 73 6f 72 20 50 31 2e 20 20 54 by cursor P1. T
1e4a2 68 69 73 20 69 6e 74 65 67 65 72 20 73 68 6f 75 his integer shou
1e4a3 6c 64 20 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77 ld be.** the row
1e4a4 69 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 id of the table
1e4a5 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 entry to which t
1e4a6 68 69 73 20 69 6e 64 65 78 20 65 6e 74 72 79 20 his index entry
1e4a7 70 6f 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 points..**.** Se
1e4a8 65 20 61 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d e also: Rowid, M
1e4a9 61 6b 65 52 65 63 6f 72 64 2e 0a 2a 2f 0a 63 61 akeRecord..*/.ca
1e4aa 73 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20 se OP_IdxRowid:
1e4ab 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f { /
1e4ac 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1e4ad 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c e */.#if 0 /* l
1e4ae 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e4af 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a oved into u.bp *
1e4b0 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 /. BtCursor *pC
1e4b1 72 73 72 3b 0a 20 20 56 64 62 65 43 75 72 73 6f rsr;. VdbeCurso
1e4b2 72 20 2a 70 43 3b 0a 20 20 69 36 34 20 72 6f 77 r *pC;. i64 row
1e4b3 69 64 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f id;.#endif /* lo
1e4b4 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1e4b5 76 65 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a 2f ved into u.bp */
1e4b6 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d .. assert( pOp-
1e4b7 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 >p1>=0 && pOp->p
1e4b8 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 1<p->nCursor );.
1e4b9 20 20 75 2e 62 70 2e 70 43 20 3d 20 70 2d 3e 61 u.bp.pC = p->a
1e4ba 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 pCsr[pOp->p1];.
1e4bb 20 61 73 73 65 72 74 28 20 75 2e 62 70 2e 70 43 assert( u.bp.pC
1e4bc 21 3d 30 20 29 3b 0a 20 20 75 2e 62 70 2e 70 43 !=0 );. u.bp.pC
1e4bd 72 73 72 20 3d 20 75 2e 62 70 2e 70 43 2d 3e 70 rsr = u.bp.pC->p
1e4be 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c Cursor;. if( AL
1e4bf 57 41 59 53 28 75 2e 62 70 2e 70 43 72 73 72 21 WAYS(u.bp.pCrsr!
1e4c0 3d 30 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 =0) ){. rc =
1e4c1 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f sqlite3VdbeCurso
1e4c2 72 4d 6f 76 65 74 6f 28 75 2e 62 70 2e 70 43 29 rMoveto(u.bp.pC)
1e4c3 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 ;. if( NEVER(
1e4c4 72 63 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 rc) ) goto abort
1e4c5 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 _due_to_error;.
1e4c6 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 70 2e assert( u.bp.
1e4c7 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 pC->deferredMove
1e4c8 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 to==0 );. ass
1e4c9 65 72 74 28 20 75 2e 62 70 2e 70 43 2d 3e 69 73 ert( u.bp.pC->is
1e4ca 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 Table==0 );.
1e4cb 69 66 28 20 21 75 2e 62 70 2e 70 43 2d 3e 6e 75 if( !u.bp.pC->nu
1e4cc 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 72 llRow ){. r
1e4cd 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 c = sqlite3VdbeI
1e4ce 64 78 52 6f 77 69 64 28 64 62 2c 20 75 2e 62 70 dxRowid(db, u.bp
1e4cf 2e 70 43 72 73 72 2c 20 26 75 2e 62 70 2e 72 6f .pCrsr, &u.bp.ro
1e4d0 77 69 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 wid);. if(
1e4d1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1e4d2 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 . goto ab
1e4d3 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1e4d4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1e4d5 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
1e4d6 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 Out, MEM_Int);.
1e4d7 20 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d pOut->u.i =
1e4d8 20 75 2e 62 70 2e 72 6f 77 69 64 3b 0a 20 20 20 u.bp.rowid;.
1e4d9 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a }. }. break;.
1e4da 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 }../* Opcode: Id
1e4db 78 47 45 20 50 31 20 50 32 20 50 33 20 50 34 20 xGE P1 P2 P3 P4
1e4dc 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 P5.**.** The P4
1e4dd 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 73 20 register values
1e4de 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 beginning with P
1e4df 33 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 3 form an unpack
1e4e0 65 64 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 ed index .** key
1e4e1 20 74 68 61 74 20 6f 6d 69 74 73 20 74 68 65 20 that omits the
1e4e2 52 4f 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 20 ROWID. Compare
1e4e3 74 68 69 73 20 6b 65 79 20 76 61 6c 75 65 20 61 this key value a
1e4e4 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78 gainst the index
1e4e5 20 0a 2a 2a 20 74 68 61 74 20 50 31 20 69 73 20 .** that P1 is
1e4e6 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 currently pointi
1e4e7 6e 67 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 ng to, ignoring
1e4e8 74 68 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65 the ROWID on the
1e4e9 20 50 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a P1 index..**.**
1e4ea 20 49 66 20 74 68 65 20 50 31 20 69 6e 64 65 78 If the P1 index
1e4eb 20 65 6e 74 72 79 20 69 73 20 67 72 65 61 74 65 entry is greate
1e4ec 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 r than or equal
1e4ed 74 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 to the key value
1e4ee 0a 2a 2a 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f .** then jump to
1e4ef 20 50 32 2e 20 20 4f 74 68 65 72 77 69 73 65 20 P2. Otherwise
1e4f0 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 fall through to
1e4f1 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 the next instruc
1e4f2 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 tion..**.** If P
1e4f3 35 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 5 is non-zero th
1e4f4 65 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 en the key value
1e4f5 20 69 73 20 69 6e 63 72 65 61 73 65 64 20 62 79 is increased by
1e4f6 20 61 6e 20 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20 an epsilon .**
1e4f7 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63 6f 6d prior to the com
1e4f8 70 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d parison. This m
1e4f9 61 6b 65 20 74 68 65 20 6f 70 63 6f 64 65 20 77 ake the opcode w
1e4fa 6f 72 6b 20 6c 69 6b 65 20 49 64 78 47 54 20 65 ork like IdxGT e
1e4fb 78 63 65 70 74 0a 2a 2a 20 74 68 61 74 20 69 66 xcept.** that if
1e4fc 20 74 68 65 20 6b 65 79 20 66 72 6f 6d 20 72 65 the key from re
1e4fd 67 69 73 74 65 72 20 50 33 20 69 73 20 61 20 70 gister P3 is a p
1e4fe 72 65 66 69 78 20 6f 66 20 74 68 65 20 6b 65 79 refix of the key
1e4ff 20 69 6e 20 74 68 65 20 63 75 72 73 6f 72 2c 0a in the cursor,.
1e500 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 ** the result is
1e501 20 66 61 6c 73 65 20 77 68 65 72 65 61 73 20 69 false whereas i
1e502 74 20 77 6f 75 6c 64 20 62 65 20 74 72 75 65 20 t would be true
1e503 77 69 74 68 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f with IdxGT..*/./
1e504 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 4c 54 20 * Opcode: IdxLT
1e505 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a P1 P2 P3 * P5.**
1e506 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69 73 .** The P4 regis
1e507 74 65 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e ter values begin
1e508 6e 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72 ning with P3 for
1e509 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e m an unpacked in
1e50a 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74 dex .** key that
1e50b 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44 omits the ROWID
1e50c 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20 . Compare this
1e50d 6b 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73 key value agains
1e50e 74 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 t the index .**
1e50f 74 68 61 74 20 50 31 20 69 73 20 63 75 72 72 65 that P1 is curre
1e510 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f ntly pointing to
1e511 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52 , ignoring the R
1e512 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69 OWID on the P1 i
1e513 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ndex..**.** If t
1e514 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72 he P1 index entr
1e515 79 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 y is less than t
1e516 68 65 20 6b 65 79 20 76 61 6c 75 65 20 74 68 65 he key value the
1e517 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a n jump to P2..**
1e518 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 Otherwise fall
1e519 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e through to the n
1e51a 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e ext instruction.
1e51b 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 .**.** If P5 is
1e51c 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 non-zero then th
1e51d 65 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69 e key value is i
1e51e 6e 63 72 65 61 73 65 64 20 62 79 20 61 6e 20 65 ncreased by an e
1e51f 70 73 69 6c 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a psilon prior .**
1e520 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 to the comparis
1e521 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 on. This makes
1e522 74 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 the opcode work
1e523 6c 69 6b 65 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63 like IdxLE..*/.c
1e524 61 73 65 20 4f 50 5f 49 64 78 4c 54 3a 20 20 20 ase OP_IdxLT:
1e525 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 /* jump,
1e526 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 in3 */.case OP_I
1e527 64 78 47 45 3a 20 7b 20 20 20 20 20 20 20 20 2f dxGE: { /
1e528 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 * jump, in3 */.#
1e529 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1e52a 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1e52b 6e 74 6f 20 75 2e 62 71 20 2a 2f 0a 20 20 56 64 nto u.bq */. Vd
1e52c 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
1e52d 69 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 int res;. Unpac
1e52e 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e kedRecord r;.#en
1e52f 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e530 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e531 6f 20 75 2e 62 71 20 2a 2f 0a 0a 20 20 61 73 73 o u.bq */.. ass
1e532 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1e533 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 && pOp->p1<p->nC
1e534 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 71 2e ursor );. u.bq.
1e535 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f pC = p->apCsr[pO
1e536 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1e537 28 20 75 2e 62 71 2e 70 43 21 3d 30 20 29 3b 0a ( u.bq.pC!=0 );.
1e538 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 if( ALWAYS(u.b
1e539 71 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 q.pC->pCursor!=0
1e53a 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 ) ){. assert(
1e53b 20 75 2e 62 71 2e 70 43 2d 3e 64 65 66 65 72 72 u.bq.pC->deferr
1e53c 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 edMoveto==0 );.
1e53d 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1e53e 70 35 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 35 p5==0 || pOp->p5
1e53f 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==1 );. asser
1e540 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d t( pOp->p4type==
1e541 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 P4_INT32 );.
1e542 75 2e 62 71 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 u.bq.r.pKeyInfo
1e543 3d 20 75 2e 62 71 2e 70 43 2d 3e 70 4b 65 79 49 = u.bq.pC->pKeyI
1e544 6e 66 6f 3b 0a 20 20 20 20 75 2e 62 71 2e 72 2e nfo;. u.bq.r.
1e545 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f nField = (u16)pO
1e546 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 69 66 28 p->p4.i;. if(
1e547 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 pOp->p5 ){.
1e548 20 20 75 2e 62 71 2e 72 2e 66 6c 61 67 73 20 3d u.bq.r.flags =
1e549 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 UNPACKED_INCRKE
1e54a 59 20 7c 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e Y | UNPACKED_IGN
1e54b 4f 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d ORE_ROWID;. }
1e54c 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 62 71 else{. u.bq
1e54d 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 .r.flags = UNPAC
1e54e 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 KED_IGNORE_ROWID
1e54f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 71 ;. }. u.bq
1e550 2e 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d .r.aMem = &p->aM
1e551 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 em[pOp->p3];.
1e552 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1e553 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 75 eIdxKeyCompare(u
1e554 2e 62 71 2e 70 43 2c 20 26 75 2e 62 71 2e 72 2c .bq.pC, &u.bq.r,
1e555 20 26 75 2e 62 71 2e 72 65 73 29 3b 0a 20 20 20 &u.bq.res);.
1e556 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
1e557 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 7b 0a 20 20 ==OP_IdxLT ){.
1e558 20 20 20 20 75 2e 62 71 2e 72 65 73 20 3d 20 2d u.bq.res = -
1e559 75 2e 62 71 2e 72 65 73 3b 0a 20 20 20 20 7d 65 u.bq.res;. }e
1e55a 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 lse{. asser
1e55b 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d t( pOp->opcode==
1e55c 4f 50 5f 49 64 78 47 45 20 29 3b 0a 20 20 20 20 OP_IdxGE );.
1e55d 20 20 75 2e 62 71 2e 72 65 73 2b 2b 3b 0a 20 20 u.bq.res++;.
1e55e 20 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 62 71 }. if( u.bq
1e55f 2e 72 65 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 .res>0 ){.
1e560 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1e561 20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 ;. }. }. b
1e562 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1e563 64 65 3a 20 44 65 73 74 72 6f 79 20 50 31 20 50 de: Destroy P1 P
1e564 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 2 P3 * *.**.** D
1e565 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 elete an entire
1e566 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f database table o
1e567 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f r index whose ro
1e568 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 ot page in the d
1e569 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 atabase.** file
1e56a 69 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 0a is given by P1..
1e56b 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 **.** The table
1e56c 62 65 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20 being destroyed
1e56d 69 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 is in the main d
1e56e 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 atabase file if
1e56f 50 33 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 33 P3==0. If.** P3
1e570 3d 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61 62 ==1 then the tab
1e571 6c 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69 le to be clear i
1e572 73 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 s in the auxilia
1e573 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ry database file
1e574 0a 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65 64 .** that is used
1e575 20 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73 to store tables
1e576 20 63 72 65 61 74 65 20 75 73 69 6e 67 20 43 52 create using CR
1e577 45 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54 EATE TEMPORARY T
1e578 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 ABLE..**.** If A
1e579 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61 UTOVACUUM is ena
1e57a 62 6c 65 64 20 74 68 65 6e 20 69 74 20 69 73 20 bled then it is
1e57b 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e possible that an
1e57c 6f 74 68 65 72 20 72 6f 6f 74 20 70 61 67 65 0a other root page.
1e57d 2a 2a 20 6d 69 67 68 74 20 62 65 20 6d 6f 76 65 ** might be move
1e57e 64 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 6c 79 d into the newly
1e57f 20 64 65 6c 65 74 65 64 20 72 6f 6f 74 20 70 61 deleted root pa
1e580 67 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b ge in order to k
1e581 65 65 70 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 eep all.** root
1e582 70 61 67 65 73 20 63 6f 6e 74 69 67 75 6f 75 73 pages contiguous
1e583 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e at the beginnin
1e584 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 g of the databas
1e585 65 2e 20 20 54 68 65 20 66 6f 72 6d 65 72 0a 2a e. The former.*
1e586 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 * value of the r
1e587 6f 6f 74 20 70 61 67 65 20 74 68 61 74 20 6d 6f oot page that mo
1e588 76 65 64 20 2d 20 69 74 73 20 76 61 6c 75 65 20 ved - its value
1e589 62 65 66 6f 72 65 20 74 68 65 20 6d 6f 76 65 20 before the move
1e58a 6f 63 63 75 72 72 65 64 20 2d 0a 2a 2a 20 69 73 occurred -.** is
1e58b 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1e58c 74 65 72 20 50 32 2e 20 20 49 66 20 6e 6f 20 70 ter P2. If no p
1e58d 61 67 65 20 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 age .** movement
1e58e 20 77 61 73 20 72 65 71 75 69 72 65 64 20 28 62 was required (b
1e58f 65 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 ecause the table
1e590 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20 77 being dropped w
1e591 61 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 74 as already .** t
1e592 68 65 20 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74 he last one in t
1e593 68 65 20 64 61 74 61 62 61 73 65 29 20 74 68 65 he database) the
1e594 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 n a zero is stor
1e595 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ed in register P
1e596 32 2e 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 2..** If AUTOVAC
1e597 55 55 4d 20 69 73 20 64 69 73 61 62 6c 65 64 20 UUM is disabled
1e598 74 68 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 then a zero is s
1e599 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 tored in registe
1e59a 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 r P2..**.** See
1e59b 61 6c 73 6f 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63 also: Clear.*/.c
1e59c 61 73 65 20 4f 50 5f 44 65 73 74 72 6f 79 3a 20 ase OP_Destroy:
1e59d 7b 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 { /* out2-pr
1e59e 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 erelease */.#if
1e59f 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e5a0 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e5a1 20 75 2e 62 72 20 2a 2f 0a 20 20 69 6e 74 20 69 u.br */. int i
1e5a2 4d 6f 76 65 64 3b 0a 20 20 69 6e 74 20 69 43 6e Moved;. int iCn
1e5a3 74 3b 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 t;. Vdbe *pVdbe
1e5a4 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 23 65 6e ;. int iDb;.#en
1e5a5 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e5a6 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e5a7 6f 20 75 2e 62 72 20 2a 2f 0a 23 69 66 6e 64 65 o u.br */.#ifnde
1e5a8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
1e5a9 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 75 2e 62 RTUALTABLE. u.b
1e5aa 72 2e 69 43 6e 74 20 3d 20 30 3b 0a 20 20 66 6f r.iCnt = 0;. fo
1e5ab 72 28 75 2e 62 72 2e 70 56 64 62 65 3d 64 62 2d r(u.br.pVdbe=db-
1e5ac 3e 70 56 64 62 65 3b 20 75 2e 62 72 2e 70 56 64 >pVdbe; u.br.pVd
1e5ad 62 65 3b 20 75 2e 62 72 2e 70 56 64 62 65 20 3d be; u.br.pVdbe =
1e5ae 20 75 2e 62 72 2e 70 56 64 62 65 2d 3e 70 4e 65 u.br.pVdbe->pNe
1e5af 78 74 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 xt){. if( u.b
1e5b0 72 2e 70 56 64 62 65 2d 3e 6d 61 67 69 63 3d 3d r.pVdbe->magic==
1e5b1 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 VDBE_MAGIC_RUN &
1e5b2 26 20 75 2e 62 72 2e 70 56 64 62 65 2d 3e 69 6e & u.br.pVdbe->in
1e5b3 56 74 61 62 4d 65 74 68 6f 64 3c 32 20 26 26 20 VtabMethod<2 &&
1e5b4 75 2e 62 72 2e 70 56 64 62 65 2d 3e 70 63 3e 3d u.br.pVdbe->pc>=
1e5b5 30 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 72 2e 0 ){. u.br.
1e5b6 69 43 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 iCnt++;. }.
1e5b7 7d 0a 23 65 6c 73 65 0a 20 20 75 2e 62 72 2e 69 }.#else. u.br.i
1e5b8 43 6e 74 20 3d 20 64 62 2d 3e 61 63 74 69 76 65 Cnt = db->active
1e5b9 56 64 62 65 43 6e 74 3b 0a 23 65 6e 64 69 66 0a VdbeCnt;.#endif.
1e5ba 20 20 69 66 28 20 75 2e 62 72 2e 69 43 6e 74 3e if( u.br.iCnt>
1e5bb 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 1 ){. rc = SQ
1e5bc 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 LITE_LOCKED;.
1e5bd 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 p->errorAction
1e5be 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 65 = OE_Abort;. }e
1e5bf 6c 73 65 7b 0a 20 20 20 20 75 2e 62 72 2e 69 44 lse{. u.br.iD
1e5c0 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 b = pOp->p3;.
1e5c1 20 61 73 73 65 72 74 28 20 75 2e 62 72 2e 69 43 assert( u.br.iC
1e5c2 6e 74 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 nt==1 );. ass
1e5c3 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 ert( (p->btreeMa
1e5c4 73 6b 20 26 20 28 31 3c 3c 75 2e 62 72 2e 69 44 sk & (1<<u.br.iD
1e5c5 62 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 72 63 b))!=0 );. rc
1e5c6 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 = sqlite3BtreeD
1e5c7 72 6f 70 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 ropTable(db->aDb
1e5c8 5b 75 2e 62 72 2e 69 44 62 5d 2e 70 42 74 2c 20 [u.br.iDb].pBt,
1e5c9 70 4f 70 2d 3e 70 31 2c 20 26 75 2e 62 72 2e 69 pOp->p1, &u.br.i
1e5ca 4d 6f 76 65 64 29 3b 0a 20 20 20 20 4d 65 6d 53 Moved);. MemS
1e5cb 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c etTypeFlag(pOut,
1e5cc 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 70 MEM_Int);. p
1e5cd 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 72 2e Out->u.i = u.br.
1e5ce 69 4d 6f 76 65 64 3b 0a 23 69 66 6e 64 65 66 20 iMoved;.#ifndef
1e5cf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
1e5d0 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 72 VACUUM. if( r
1e5d1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
1e5d2 75 2e 62 72 2e 69 4d 6f 76 65 64 21 3d 30 20 29 u.br.iMoved!=0 )
1e5d3 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 {. sqlite3R
1e5d4 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 26 64 62 ootPageMoved(&db
1e5d5 2d 3e 61 44 62 5b 75 2e 62 72 2e 69 44 62 5d 2c ->aDb[u.br.iDb],
1e5d6 20 75 2e 62 72 2e 69 4d 6f 76 65 64 2c 20 70 4f u.br.iMoved, pO
1e5d7 70 2d 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 23 65 p->p1);. }.#e
1e5d8 6e 64 69 66 0a 20 20 7d 0a 20 20 62 72 65 61 6b ndif. }. break
1e5d9 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1e5da 43 6c 65 61 72 20 50 31 20 50 32 20 50 33 0a 2a Clear P1 P2 P3.*
1e5db 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 *.** Delete all
1e5dc 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
1e5dd 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f database table o
1e5de 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f r index whose ro
1e5df 6f 74 20 70 61 67 65 0a 2a 2a 20 69 6e 20 74 68 ot page.** in th
1e5e0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1e5e1 69 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 20 is given by P1.
1e5e2 20 42 75 74 2c 20 75 6e 6c 69 6b 65 20 44 65 73 But, unlike Des
1e5e3 74 72 6f 79 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 troy, do not.**
1e5e4 72 65 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65 remove the table
1e5e5 20 6f 72 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 or index from t
1e5e6 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1e5e7 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c ..**.** The tabl
1e5e8 65 20 62 65 69 6e 67 20 63 6c 65 61 72 20 69 73 e being clear is
1e5e9 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 in the main dat
1e5ea 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 32 abase file if P2
1e5eb 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 32 3d 3d ==0. If.** P2==
1e5ec 31 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 1 then the table
1e5ed 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20 to be clear is
1e5ee 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 in the auxiliary
1e5ef 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a database file.*
1e5f0 2a 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74 * that is used t
1e5f1 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63 o store tables c
1e5f2 72 65 61 74 65 20 75 73 69 6e 67 20 43 52 45 41 reate using CREA
1e5f3 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42 TE TEMPORARY TAB
1e5f4 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 LE..**.** If the
1e5f5 20 50 33 20 76 61 6c 75 65 20 69 73 20 6e 6f 6e P3 value is non
1e5f6 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 -zero, then the
1e5f7 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 table referred t
1e5f8 6f 20 6d 75 73 74 20 62 65 20 61 6e 0a 2a 2a 20 o must be an.**
1e5f9 69 6e 74 6b 65 79 20 74 61 62 6c 65 20 28 61 6e intkey table (an
1e5fa 20 53 51 4c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 SQL table, not
1e5fb 61 6e 20 69 6e 64 65 78 29 2e 20 49 6e 20 74 68 an index). In th
1e5fc 69 73 20 63 61 73 65 20 74 68 65 20 72 6f 77 20 is case the row
1e5fd 63 68 61 6e 67 65 20 0a 2a 2a 20 63 6f 75 6e 74 change .** count
1e5fe 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 is incremented
1e5ff 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 by the number of
1e600 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 rows in the tab
1e601 6c 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65 64 le being cleared
1e602 2e 20 0a 2a 2a 20 49 66 20 50 33 20 69 73 20 67 . .** If P3 is g
1e603 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f reater than zero
1e604 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 , then the value
1e605 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1e606 74 65 72 20 50 33 20 69 73 0a 2a 2a 20 61 6c 73 ter P3 is.** als
1e607 6f 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 o incremented by
1e608 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 the number of r
1e609 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 ows in the table
1e60a 20 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e 0a being cleared..
1e60b 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
1e60c 44 65 73 74 72 6f 79 0a 2a 2f 0a 63 61 73 65 20 Destroy.*/.case
1e60d 4f 50 5f 43 6c 65 61 72 3a 20 7b 0a 23 69 66 20 OP_Clear: {.#if
1e60e 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e60f 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e610 20 75 2e 62 73 20 2a 2f 0a 20 20 69 6e 74 20 6e u.bs */. int n
1e611 43 68 61 6e 67 65 3b 0a 23 65 6e 64 69 66 20 2f Change;.#endif /
1e612 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1e613 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1e614 73 20 2a 2f 0a 0a 20 20 75 2e 62 73 2e 6e 43 68 s */.. u.bs.nCh
1e615 61 6e 67 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 ange = 0;. asse
1e616 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 rt( (p->btreeMas
1e617 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 32 29 k & (1<<pOp->p2)
1e618 29 21 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 )!=0 );. rc = s
1e619 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 qlite3BtreeClear
1e61a 54 61 62 6c 65 28 0a 20 20 20 20 20 20 64 62 2d Table(. db-
1e61b 3e 61 44 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 >aDb[pOp->p2].pB
1e61c 74 2c 20 70 4f 70 2d 3e 70 31 2c 20 28 70 4f 70 t, pOp->p1, (pOp
1e61d 2d 3e 70 33 20 3f 20 26 75 2e 62 73 2e 6e 43 68 ->p3 ? &u.bs.nCh
1e61e 61 6e 67 65 20 3a 20 30 29 0a 20 20 29 3b 0a 20 ange : 0). );.
1e61f 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a if( pOp->p3 ){.
1e620 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 2b p->nChange +
1e621 3d 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b 0a = u.bs.nChange;.
1e622 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 3e if( pOp->p3>
1e623 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4d 0 ){. p->aM
1e624 65 6d 5b 70 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20 em[pOp->p3].u.i
1e625 2b 3d 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b += u.bs.nChange;
1e626 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 . }. }. bre
1e627 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1e628 3a 20 43 72 65 61 74 65 54 61 62 6c 65 20 50 31 : CreateTable P1
1e629 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1e62a 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 74 Allocate a new t
1e62b 61 62 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 6e able in the main
1e62c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
1e62d 66 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 f P1==0 or in th
1e62e 65 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 e.** auxiliary d
1e62f 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 atabase file if
1e630 50 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 P1==1 or in an a
1e631 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 ttached database
1e632 20 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 if.** P1>1. Wr
1e633 69 74 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 ite the root pag
1e634 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
1e635 6e 65 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a new table into.*
1e636 2a 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a * register P2.**
1e637 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e .** The differen
1e638 63 65 20 62 65 74 77 65 65 6e 20 61 20 74 61 62 ce between a tab
1e639 6c 65 20 61 6e 64 20 61 6e 20 69 6e 64 65 78 20 le and an index
1e63a 69 73 20 74 68 69 73 3a 20 20 41 20 74 61 62 6c is this: A tabl
1e63b 65 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20 61 e must.** have a
1e63c 20 34 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20 4-byte integer
1e63d 6b 65 79 20 61 6e 64 20 63 61 6e 20 68 61 76 65 key and can have
1e63e 20 61 72 62 69 74 72 61 72 79 20 64 61 74 61 2e arbitrary data.
1e63f 20 20 41 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 61 An index.** ha
1e640 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 6b s an arbitrary k
1e641 65 79 20 62 75 74 20 6e 6f 20 64 61 74 61 2e 0a ey but no data..
1e642 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
1e643 43 72 65 61 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f CreateIndex.*/./
1e644 2a 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65 * Opcode: Create
1e645 49 6e 64 65 78 20 50 31 20 50 32 20 2a 20 2a 20 Index P1 P2 * *
1e646 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 *.**.** Allocate
1e647 20 61 20 6e 65 77 20 69 6e 64 65 78 20 69 6e 20 a new index in
1e648 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
1e649 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 e file if P1==0
1e64a 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 or in the.** aux
1e64b 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 iliary database
1e64c 66 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 file if P1==1 or
1e64d 20 69 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20 in an attached
1e64e 64 61 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 database if.** P
1e64f 31 3e 31 2e 20 20 57 72 69 74 65 20 74 68 65 20 1>1. Write the
1e650 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 root page number
1e651 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c of the new tabl
1e652 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 e into.** regist
1e653 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 er P2..**.** See
1e654 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f documentation o
1e655 6e 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 n OP_CreateTable
1e656 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
1e657 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a information..*/.
1e658 63 61 73 65 20 4f 50 5f 43 72 65 61 74 65 49 6e case OP_CreateIn
1e659 64 65 78 3a 20 20 20 20 20 20 20 20 20 20 20 20 dex:
1e65a 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 /* out2-prerelea
1e65b 73 65 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72 se */.case OP_Cr
1e65c 65 61 74 65 54 61 62 6c 65 3a 20 7b 20 20 20 20 eateTable: {
1e65d 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1e65e 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 erelease */.#if
1e65f 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e660 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e661 20 75 2e 62 74 20 2a 2f 0a 20 20 69 6e 74 20 70 u.bt */. int p
1e662 67 6e 6f 3b 0a 20 20 69 6e 74 20 66 6c 61 67 73 gno;. int flags
1e663 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e ;. Db *pDb;.#en
1e664 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e665 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e666 6f 20 75 2e 62 74 20 2a 2f 0a 0a 20 20 75 2e 62 o u.bt */.. u.b
1e667 74 2e 70 67 6e 6f 20 3d 20 30 3b 0a 20 20 61 73 t.pgno = 0;. as
1e668 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1e669 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e && pOp->p1<db->
1e66a 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 nDb );. assert(
1e66b 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 (p->btreeMask &
1e66c 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d (1<<pOp->p1))!=
1e66d 30 20 29 3b 0a 20 20 75 2e 62 74 2e 70 44 62 20 0 );. u.bt.pDb
1e66e 3d 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e = &db->aDb[pOp->
1e66f 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1e670 2e 62 74 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20 .bt.pDb->pBt!=0
1e671 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 );. if( pOp->op
1e672 63 6f 64 65 3d 3d 4f 50 5f 43 72 65 61 74 65 54 code==OP_CreateT
1e673 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 75 able ){. /* u
1e674 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42 54 52 45 .bt.flags = BTRE
1e675 45 5f 49 4e 54 4b 45 59 3b 20 2a 2f 0a 20 20 20 E_INTKEY; */.
1e676 20 75 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42 54 u.bt.flags = BT
1e677 52 45 45 5f 4c 45 41 46 44 41 54 41 7c 42 54 52 REE_LEAFDATA|BTR
1e678 45 45 5f 49 4e 54 4b 45 59 3b 0a 20 20 7d 65 6c EE_INTKEY;. }el
1e679 73 65 7b 0a 20 20 20 20 75 2e 62 74 2e 66 6c 61 se{. u.bt.fla
1e67a 67 73 20 3d 20 42 54 52 45 45 5f 5a 45 52 4f 44 gs = BTREE_ZEROD
1e67b 41 54 41 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 ATA;. }. rc =
1e67c 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 sqlite3BtreeCrea
1e67d 74 65 54 61 62 6c 65 28 75 2e 62 74 2e 70 44 62 teTable(u.bt.pDb
1e67e 2d 3e 70 42 74 2c 20 26 75 2e 62 74 2e 70 67 6e ->pBt, &u.bt.pgn
1e67f 6f 2c 20 75 2e 62 74 2e 66 6c 61 67 73 29 3b 0a o, u.bt.flags);.
1e680 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e pOut->u.i = u.
1e681 62 74 2e 70 67 6e 6f 3b 0a 20 20 4d 65 6d 53 65 bt.pgno;. MemSe
1e682 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1e683 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 MEM_Int);. brea
1e684 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1e685 20 50 61 72 73 65 53 63 68 65 6d 61 20 50 31 20 ParseSchema P1
1e686 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * P4 *.**.**
1e687 52 65 61 64 20 61 6e 64 20 70 61 72 73 65 20 61 Read and parse a
1e688 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 ll entries from
1e689 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 the SQLITE_MASTE
1e68a 52 20 74 61 62 6c 65 20 6f 66 20 64 61 74 61 62 R table of datab
1e68b 61 73 65 20 50 31 0a 2a 2a 20 74 68 61 74 20 6d ase P1.** that m
1e68c 61 74 63 68 20 74 68 65 20 57 48 45 52 45 20 63 atch the WHERE c
1e68d 6c 61 75 73 65 20 50 34 2e 20 20 50 32 20 69 73 lause P4. P2 is
1e68e 20 74 68 65 20 22 66 6f 72 63 65 22 20 66 6c 61 the "force" fla
1e68f 67 2e 20 20 20 41 6c 77 61 79 73 20 64 6f 0a 2a g. Always do.*
1e690 2a 20 74 68 65 20 70 61 72 73 69 6e 67 20 69 66 * the parsing if
1e691 20 50 32 20 69 73 20 74 72 75 65 2e 20 20 49 66 P2 is true. If
1e692 20 50 32 20 69 73 20 66 61 6c 73 65 2c 20 74 68 P2 is false, th
1e693 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
1e694 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 20 69 66 is a.** no-op if
1e695 20 74 68 65 20 73 63 68 65 6d 61 20 69 73 20 6e the schema is n
1e696 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6c 6f 61 ot currently loa
1e697 64 65 64 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 ded. In other w
1e698 6f 72 64 73 2c 20 69 66 20 50 32 0a 2a 2a 20 69 ords, if P2.** i
1e699 73 20 66 61 6c 73 65 2c 20 74 68 65 20 53 51 4c s false, the SQL
1e69a 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 ITE_MASTER table
1e69b 20 69 73 20 6f 6e 6c 79 20 70 61 72 73 65 64 20 is only parsed
1e69c 69 66 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 if the rest of t
1e69d 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 he.** schema is
1e69e 61 6c 72 65 61 64 79 20 6c 6f 61 64 65 64 20 69 already loaded i
1e69f 6e 74 6f 20 74 68 65 20 73 79 6d 62 6f 6c 20 74 nto the symbol t
1e6a0 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 able..**.** This
1e6a1 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 opcode invokes
1e6a2 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 63 72 the parser to cr
1e6a3 65 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 eate a new virtu
1e6a4 61 6c 20 6d 61 63 68 69 6e 65 2c 0a 2a 2a 20 74 al machine,.** t
1e6a5 68 65 6e 20 72 75 6e 73 20 74 68 65 20 6e 65 77 hen runs the new
1e6a6 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1e6a7 2e 20 20 49 74 20 69 73 20 74 68 75 73 20 61 20 . It is thus a
1e6a8 72 65 2d 65 6e 74 72 61 6e 74 20 6f 70 63 6f 64 re-entrant opcod
1e6a9 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 e..*/.case OP_Pa
1e6aa 72 73 65 53 63 68 65 6d 61 3a 20 7b 0a 23 69 66 rseSchema: {.#if
1e6ab 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1e6ac 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e6ad 6f 20 75 2e 62 75 20 2a 2f 0a 20 20 69 6e 74 20 o u.bu */. int
1e6ae 69 44 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 iDb;. const cha
1e6af 72 20 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 63 68 r *zMaster;. ch
1e6b0 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 49 6e 69 74 ar *zSql;. Init
1e6b1 44 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 23 Data initData;.#
1e6b2 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1e6b3 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1e6b4 6e 74 6f 20 75 2e 62 75 20 2a 2f 0a 0a 20 20 75 nto u.bu */.. u
1e6b5 2e 62 75 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 .bu.iDb = pOp->p
1e6b6 31 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 1;. assert( u.b
1e6b7 75 2e 69 44 62 3e 3d 30 20 26 26 20 75 2e 62 75 u.iDb>=0 && u.bu
1e6b8 2e 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a .iDb<db->nDb );.
1e6b9 0a 20 20 2f 2a 20 49 66 20 70 4f 70 2d 3e 70 32 . /* If pOp->p2
1e6ba 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 69 73 is 0, then this
1e6bb 20 6f 70 63 6f 64 65 20 69 73 20 62 65 69 6e 67 opcode is being
1e6bc 20 65 78 65 63 75 74 65 64 20 74 6f 20 72 65 61 executed to rea
1e6bd 64 20 61 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 20 d a. ** single
1e6be 72 6f 77 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 row, for example
1e6bf 20 74 68 65 20 72 6f 77 20 63 6f 72 72 65 73 70 the row corresp
1e6c0 6f 6e 64 69 6e 67 20 74 6f 20 61 20 6e 65 77 20 onding to a new
1e6c1 69 6e 64 65 78 0a 20 20 2a 2a 20 63 72 65 61 74 index. ** creat
1e6c2 65 64 20 62 79 20 74 68 69 73 20 56 44 42 45 2c ed by this VDBE,
1e6c3 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 from the sqlite
1e6c4 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49 _master table. I
1e6c5 74 20 6f 6e 6c 79 0a 20 20 2a 2a 20 64 6f 65 73 t only. ** does
1e6c6 20 74 68 69 73 20 69 66 20 74 68 65 20 63 6f 72 this if the cor
1e6c7 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e 2d 6d 65 responding in-me
1e6c8 6d 6f 72 79 20 73 63 68 65 6d 61 20 69 73 20 63 mory schema is c
1e6c9 75 72 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 6c 6f urrently. ** lo
1e6ca 61 64 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c aded. Otherwise,
1e6cb 20 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20 64 the new index d
1e6cc 65 66 69 6e 69 74 69 6f 6e 20 63 61 6e 20 62 65 efinition can be
1e6cd 20 6c 6f 61 64 65 64 20 61 6c 6f 6e 67 0a 20 20 loaded along.
1e6ce 2a 2a 20 77 69 74 68 20 74 68 65 20 72 65 73 74 ** with the rest
1e6cf 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 77 of the schema w
1e6d0 68 65 6e 20 69 74 20 69 73 20 72 65 71 75 69 72 hen it is requir
1e6d1 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6c ed.. **. ** Al
1e6d2 74 68 6f 75 67 68 20 74 68 65 20 6d 75 74 65 78 though the mutex
1e6d3 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 on the BtShared
1e6d4 20 6f 62 6a 65 63 74 20 74 68 61 74 20 63 6f 72 object that cor
1e6d5 72 65 73 70 6f 6e 64 73 20 74 6f 0a 20 20 2a 2a responds to. **
1e6d6 20 64 61 74 61 62 61 73 65 20 75 2e 62 75 2e 69 database u.bu.i
1e6d7 44 62 20 28 74 68 65 20 64 61 74 61 62 61 73 65 Db (the database
1e6d8 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
1e6d9 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 sqlite_master ta
1e6da 62 6c 65 0a 20 20 2a 2a 20 72 65 61 64 20 62 79 ble. ** read by
1e6db 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f this instructio
1e6dc 6e 29 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 n) is currently
1e6dd 68 65 6c 64 2c 20 69 74 20 69 73 20 6e 65 63 65 held, it is nece
1e6de 73 73 61 72 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 ssary to. ** ob
1e6df 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 65 73 tain the mutexes
1e6e0 20 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 on all attached
1e6e1 20 64 61 74 61 62 61 73 65 73 20 62 65 66 6f 72 databases befor
1e6e2 65 20 63 68 65 63 6b 69 6e 67 20 69 66 0a 20 20 e checking if.
1e6e3 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 ** the schema of
1e6e4 20 75 2e 62 75 2e 69 44 62 20 69 73 20 6c 6f 61 u.bu.iDb is loa
1e6e5 64 65 64 2e 20 54 68 69 73 20 69 73 20 62 65 63 ded. This is bec
1e6e6 61 75 73 65 2c 20 61 74 20 74 68 65 20 73 74 61 ause, at the sta
1e6e7 72 74 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73 rt of. ** the s
1e6e8 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 qlite3_exec() ca
1e6e9 6c 6c 20 62 65 6c 6f 77 2c 20 53 51 4c 69 74 65 ll below, SQLite
1e6ea 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 0a 20 20 2a will invoke. *
1e6eb 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e * sqlite3BtreeEn
1e6ec 74 65 72 41 6c 6c 28 29 2e 20 49 66 20 61 6c 6c terAll(). If all
1e6ed 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 mutexes are not
1e6ee 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74 already held, t
1e6ef 68 65 0a 20 20 2a 2a 20 75 2e 62 75 2e 69 44 62 he. ** u.bu.iDb
1e6f0 20 6d 75 74 65 78 20 6d 61 79 20 62 65 20 74 65 mutex may be te
1e6f1 6d 70 6f 72 61 72 69 6c 79 20 72 65 6c 65 61 73 mporarily releas
1e6f2 65 64 20 74 6f 20 61 76 6f 69 64 20 64 65 61 64 ed to avoid dead
1e6f3 6c 6f 63 6b 2e 20 49 66 0a 20 20 2a 2a 20 74 68 lock. If. ** th
1e6f4 69 73 20 68 61 70 70 65 6e 73 2c 20 74 68 65 6e is happens, then
1e6f5 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 some other thre
1e6f6 61 64 20 6d 61 79 20 64 65 6c 65 74 65 20 74 68 ad may delete th
1e6f7 65 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 2a 2a e in-memory. **
1e6f8 20 73 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62 schema of datab
1e6f9 61 73 65 20 75 2e 62 75 2e 69 44 62 20 62 65 66 ase u.bu.iDb bef
1e6fa 6f 72 65 20 74 68 65 20 53 51 4c 20 73 74 61 74 ore the SQL stat
1e6fb 65 6d 65 6e 74 20 72 75 6e 73 2e 20 54 68 65 20 ement runs. The
1e6fc 73 63 68 65 6d 61 0a 20 20 2a 2a 20 77 69 6c 6c schema. ** will
1e6fd 20 6e 6f 74 20 62 65 20 72 65 6c 6f 61 64 65 64 not be reloaded
1e6fe 20 62 65 63 75 61 73 65 20 74 68 65 20 64 62 2d becuase the db-
1e6ff 3e 69 6e 69 74 2e 62 75 73 79 20 66 6c 61 67 20 >init.busy flag
1e700 69 73 20 73 65 74 2e 20 54 68 69 73 0a 20 20 2a is set. This. *
1e701 2a 20 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 * can result in
1e702 61 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 a "no such table
1e703 3a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 : sqlite_master"
1e704 20 6f 72 20 22 6d 61 6c 66 6f 72 6d 65 64 0a 20 or "malformed.
1e705 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 ** database sch
1e706 65 6d 61 22 20 65 72 72 6f 72 20 62 65 69 6e 67 ema" error being
1e707 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 returned to the
1e708 20 75 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73 user.. */. as
1e709 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
1e70a 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d eeHoldsMutex(db-
1e70b 3e 61 44 62 5b 75 2e 62 75 2e 69 44 62 5d 2e 70 >aDb[u.bu.iDb].p
1e70c 42 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 Bt) );. sqlite3
1e70d 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 BtreeEnterAll(db
1e70e 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 );. if( pOp->p2
1e70f 20 7c 7c 20 44 62 48 61 73 50 72 6f 70 65 72 74 || DbHasPropert
1e710 79 28 64 62 2c 20 75 2e 62 75 2e 69 44 62 2c 20 y(db, u.bu.iDb,
1e711 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 DB_SchemaLoaded)
1e712 20 29 7b 0a 20 20 20 20 75 2e 62 75 2e 7a 4d 61 ){. u.bu.zMa
1e713 73 74 65 72 20 3d 20 53 43 48 45 4d 41 5f 54 41 ster = SCHEMA_TA
1e714 42 4c 45 28 75 2e 62 75 2e 69 44 62 29 3b 0a 20 BLE(u.bu.iDb);.
1e715 20 20 20 75 2e 62 75 2e 69 6e 69 74 44 61 74 61 u.bu.initData
1e716 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 75 2e .db = db;. u.
1e717 62 75 2e 69 6e 69 74 44 61 74 61 2e 69 44 62 20 bu.initData.iDb
1e718 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20 75 = pOp->p1;. u
1e719 2e 62 75 2e 69 6e 69 74 44 61 74 61 2e 70 7a 45 .bu.initData.pzE
1e71a 72 72 4d 73 67 20 3d 20 26 70 2d 3e 7a 45 72 72 rrMsg = &p->zErr
1e71b 4d 73 67 3b 0a 20 20 20 20 75 2e 62 75 2e 7a 53 Msg;. u.bu.zS
1e71c 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 ql = sqlite3MPri
1e71d 6e 74 66 28 64 62 2c 0a 20 20 20 20 20 20 20 22 ntf(db,. "
1e71e 53 45 4c 45 43 54 20 6e 61 6d 65 2c 20 72 6f 6f SELECT name, roo
1e71f 74 70 61 67 65 2c 20 73 71 6c 20 46 52 4f 4d 20 tpage, sql FROM
1e720 27 25 71 27 2e 25 73 20 57 48 45 52 45 20 25 73 '%q'.%s WHERE %s
1e721 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 44 ",. db->aD
1e722 62 5b 75 2e 62 75 2e 69 44 62 5d 2e 7a 4e 61 6d b[u.bu.iDb].zNam
1e723 65 2c 20 75 2e 62 75 2e 7a 4d 61 73 74 65 72 2c e, u.bu.zMaster,
1e724 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 pOp->p4.z);.
1e725 20 69 66 28 20 75 2e 62 75 2e 7a 53 71 6c 3d 3d if( u.bu.zSql==
1e726 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
1e727 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
1e728 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 }else{. (
1e729 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 void)sqlite3Safe
1e72a 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 tyOff(db);.
1e72b 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 assert( db->ini
1e72c 74 2e 62 75 73 79 3d 3d 30 20 29 3b 0a 20 20 20 t.busy==0 );.
1e72d 20 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 db->init.busy
1e72e 20 3d 20 31 3b 0a 20 20 20 20 20 20 75 2e 62 75 = 1;. u.bu
1e72f 2e 69 6e 69 74 44 61 74 61 2e 72 63 20 3d 20 53 .initData.rc = S
1e730 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
1e731 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c assert( !db->mal
1e732 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 locFailed );.
1e733 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f rc = sqlite3_
1e734 65 78 65 63 28 64 62 2c 20 75 2e 62 75 2e 7a 53 exec(db, u.bu.zS
1e735 71 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69 74 43 ql, sqlite3InitC
1e736 61 6c 6c 62 61 63 6b 2c 20 26 75 2e 62 75 2e 69 allback, &u.bu.i
1e737 6e 69 74 44 61 74 61 2c 20 30 29 3b 0a 20 20 20 nitData, 0);.
1e738 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1e739 45 5f 4f 4b 20 29 20 72 63 20 3d 20 75 2e 62 75 E_OK ) rc = u.bu
1e73a 2e 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 .initData.rc;.
1e73b 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1e73c 65 28 64 62 2c 20 75 2e 62 75 2e 7a 53 71 6c 29 e(db, u.bu.zSql)
1e73d 3b 0a 20 20 20 20 20 20 64 62 2d 3e 69 6e 69 74 ;. db->init
1e73e 2e 62 75 73 79 20 3d 20 30 3b 0a 20 20 20 20 20 .busy = 0;.
1e73f 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 (void)sqlite3Sa
1e740 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 fetyOn(db);.
1e741 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 }. }. sqlite3B
1e742 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 treeLeaveAll(db)
1e743 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
1e744 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 TE_NOMEM ){.
1e745 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d goto no_mem;. }
1e746 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 . break;.}..#if
1e747 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1e748 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 0a 2f _OMIT_ANALYZE)./
1e749 2a 20 4f 70 63 6f 64 65 3a 20 4c 6f 61 64 41 6e * Opcode: LoadAn
1e74a 61 6c 79 73 69 73 20 50 31 20 2a 20 2a 20 2a 20 alysis P1 * * *
1e74b 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 *.**.** Read the
1e74c 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 sqlite_stat1 ta
1e74d 62 6c 65 20 66 6f 72 20 64 61 74 61 62 61 73 65 ble for database
1e74e 20 50 31 20 61 6e 64 20 6c 6f 61 64 20 74 68 65 P1 and load the
1e74f 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 content.** of t
1e750 68 61 74 20 74 61 62 6c 65 20 69 6e 74 6f 20 74 hat table into t
1e751 68 65 20 69 6e 74 65 72 6e 61 6c 20 69 6e 64 65 he internal inde
1e752 78 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 x hash table. T
1e753 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 2a his will cause.*
1e754 2a 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 74 * the analysis t
1e755 6f 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 70 o be used when p
1e756 72 65 70 61 72 69 6e 67 20 61 6c 6c 20 73 75 62 reparing all sub
1e757 73 65 71 75 65 6e 74 20 71 75 65 72 69 65 73 2e sequent queries.
1e758 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 6f 61 64 .*/.case OP_Load
1e759 41 6e 61 6c 79 73 69 73 3a 20 7b 0a 20 20 61 73 Analysis: {. as
1e75a 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1e75b 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e && pOp->p1<db->
1e75c 6e 44 62 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 nDb );. rc = sq
1e75d 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 lite3AnalysisLoa
1e75e 64 28 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a d(db, pOp->p1);.
1e75f 20 20 62 72 65 61 6b 3b 20 20 0a 7d 0a 23 65 6e break; .}.#en
1e760 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 dif /* !defined(
1e761 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c SQLITE_OMIT_ANAL
1e762 59 5a 45 29 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f YZE) */../* Opco
1e763 64 65 3a 20 44 72 6f 70 54 61 62 6c 65 20 50 31 de: DropTable P1
1e764 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 * * P4 *.**.**
1e765 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 Remove the inter
1e766 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 nal (in-memory)
1e767 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 data structures
1e768 74 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a that describe.**
1e769 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 the table named
1e76a 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 P4 in database
1e76b 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c P1. This is cal
1e76c 6c 65 64 20 61 66 74 65 72 20 61 20 74 61 62 6c led after a tabl
1e76d 65 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 e.** is dropped
1e76e 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 in order to keep
1e76f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 the internal re
1e770 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 presentation of
1e771 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f the.** schema co
1e772 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 nsistent with wh
1e773 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a at is on disk..*
1e774 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 61 /.case OP_DropTa
1e775 62 6c 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 ble: {. sqlite3
1e776 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 UnlinkAndDeleteT
1e777 61 62 6c 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 able(db, pOp->p1
1e778 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 , pOp->p4.z);.
1e779 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1e77a 6f 64 65 3a 20 44 72 6f 70 49 6e 64 65 78 20 50 ode: DropIndex P
1e77b 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 1 * * P4 *.**.**
1e77c 20 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 Remove the inte
1e77d 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 rnal (in-memory)
1e77e 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 data structures
1e77f 20 74 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a that describe.*
1e780 2a 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 * the index name
1e781 64 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 d P4 in database
1e782 20 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 P1. This is ca
1e783 6c 6c 65 64 20 61 66 74 65 72 20 61 6e 20 69 6e lled after an in
1e784 64 65 78 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 dex.** is droppe
1e785 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 d in order to ke
1e786 65 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 ep the internal
1e787 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f representation o
1e788 66 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 f the.** schema
1e789 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 consistent with
1e78a 77 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e what is on disk.
1e78b 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 .*/.case OP_Drop
1e78c 49 6e 64 65 78 3a 20 7b 0a 20 20 73 71 6c 69 74 Index: {. sqlit
1e78d 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 e3UnlinkAndDelet
1e78e 65 49 6e 64 65 78 28 64 62 2c 20 70 4f 70 2d 3e eIndex(db, pOp->
1e78f 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a p1, pOp->p4.z);.
1e790 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1e791 70 63 6f 64 65 3a 20 44 72 6f 70 54 72 69 67 67 pcode: DropTrigg
1e792 65 72 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a er P1 * * P4 *.*
1e793 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 *.** Remove the
1e794 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d internal (in-mem
1e795 6f 72 79 29 20 64 61 74 61 20 73 74 72 75 63 74 ory) data struct
1e796 75 72 65 73 20 74 68 61 74 20 64 65 73 63 72 69 ures that descri
1e797 62 65 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 65 be.** the trigge
1e798 72 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 r named P4 in da
1e799 74 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 tabase P1. This
1e79a 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 is called after
1e79b 20 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 73 a trigger.** is
1e79c 20 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 dropped in orde
1e79d 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e r to keep the in
1e79e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 ternal represent
1e79f 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 ation of the.**
1e7a0 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e schema consisten
1e7a1 74 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f t with what is o
1e7a2 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 n disk..*/.case
1e7a3 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 72 3a 20 OP_DropTrigger:
1e7a4 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e {. sqlite3Unlin
1e7a5 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 kAndDeleteTrigge
1e7a6 72 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 r(db, pOp->p1, p
1e7a7 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 Op->p4.z);. bre
1e7a8 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 ak;.}...#ifndef
1e7a9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 SQLITE_OMIT_INTE
1e7aa 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 20 4f GRITY_CHECK./* O
1e7ab 70 63 6f 64 65 3a 20 49 6e 74 65 67 72 69 74 79 pcode: Integrity
1e7ac 43 6b 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 Ck P1 P2 P3 * P5
1e7ad 0a 2a 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 6e 61 .**.** Do an ana
1e7ae 6c 79 73 69 73 20 6f 66 20 74 68 65 20 63 75 72 lysis of the cur
1e7af 72 65 6e 74 6c 79 20 6f 70 65 6e 20 64 61 74 61 rently open data
1e7b0 62 61 73 65 2e 20 20 53 74 6f 72 65 20 69 6e 0a base. Store in.
1e7b1 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 74 ** register P1 t
1e7b2 68 65 20 74 65 78 74 20 6f 66 20 61 6e 20 65 72 he text of an er
1e7b3 72 6f 72 20 6d 65 73 73 61 67 65 20 64 65 73 63 ror message desc
1e7b4 72 69 62 69 6e 67 20 61 6e 79 20 70 72 6f 62 6c ribing any probl
1e7b5 65 6d 73 2e 0a 2a 2a 20 49 66 20 6e 6f 20 70 72 ems..** If no pr
1e7b6 6f 62 6c 65 6d 73 20 61 72 65 20 66 6f 75 6e 64 oblems are found
1e7b7 2c 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 , store a NULL i
1e7b8 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a n register P1..*
1e7b9 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74 65 *.** The registe
1e7ba 72 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74 68 r P3 contains th
1e7bb 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
1e7bc 20 6f 66 20 61 6c 6c 6f 77 65 64 20 65 72 72 6f of allowed erro
1e7bd 72 73 2e 0a 2a 2a 20 41 74 20 6d 6f 73 74 20 72 rs..** At most r
1e7be 65 67 28 50 33 29 20 65 72 72 6f 72 73 20 77 69 eg(P3) errors wi
1e7bf 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64 2e 0a ll be reported..
1e7c0 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 ** In other word
1e7c1 73 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 s, the analysis
1e7c2 73 74 6f 70 73 20 61 73 20 73 6f 6f 6e 20 61 73 stops as soon as
1e7c3 20 72 65 67 28 50 31 29 20 65 72 72 6f 72 73 20 reg(P1) errors
1e7c4 61 72 65 20 0a 2a 2a 20 73 65 65 6e 2e 20 20 52 are .** seen. R
1e7c5 65 67 28 50 31 29 20 69 73 20 75 70 64 61 74 65 eg(P1) is update
1e7c6 64 20 77 69 74 68 20 74 68 65 20 6e 75 6d 62 65 d with the numbe
1e7c7 72 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61 r of errors rema
1e7c8 69 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ining..**.** The
1e7c9 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 root page numbe
1e7ca 72 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 rs of all tables
1e7cb 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1e7cc 20 61 72 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 are integer.**
1e7cd 73 74 6f 72 65 64 20 69 6e 20 72 65 67 28 50 31 stored in reg(P1
1e7ce 29 2c 20 72 65 67 28 50 31 2b 31 29 2c 20 72 65 ), reg(P1+1), re
1e7cf 67 28 50 31 2b 32 29 2c 20 2e 2e 2e 2e 20 20 54 g(P1+2), .... T
1e7d0 68 65 72 65 20 61 72 65 20 50 32 20 74 61 62 6c here are P2 tabl
1e7d1 65 73 0a 2a 2a 20 74 6f 74 61 6c 2e 0a 2a 2a 0a es.** total..**.
1e7d2 2a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 74 20 ** If P5 is not
1e7d3 7a 65 72 6f 2c 20 74 68 65 20 63 68 65 63 6b 20 zero, the check
1e7d4 69 73 20 64 6f 6e 65 20 6f 6e 20 74 68 65 20 61 is done on the a
1e7d5 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 uxiliary databas
1e7d6 65 0a 2a 2a 20 66 69 6c 65 2c 20 6e 6f 74 20 74 e.** file, not t
1e7d7 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
1e7d8 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 file..**.** Thi
1e7d9 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 s opcode is used
1e7da 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 to implement th
1e7db 65 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 e integrity_chec
1e7dc 6b 20 70 72 61 67 6d 61 2e 0a 2a 2f 0a 63 61 73 k pragma..*/.cas
1e7dd 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b e OP_IntegrityCk
1e7de 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f : {.#if 0 /* lo
1e7df 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1e7e0 76 65 64 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f ved into u.bv */
1e7e1 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 3b 20 20 20 . int nRoot;
1e7e2 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1e7e3 74 61 62 6c 65 73 20 74 6f 20 63 68 65 63 6b 2e tables to check.
1e7e4 20 20 28 4e 75 6d 62 65 72 20 6f 66 20 72 6f 6f (Number of roo
1e7e5 74 20 70 61 67 65 73 2e 29 20 2a 2f 0a 20 20 69 t pages.) */. i
1e7e6 6e 74 20 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f nt *aRoot; /
1e7e7 2a 20 41 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 * Array of rootp
1e7e8 61 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 age numbers for
1e7e9 74 61 62 6c 65 73 20 74 6f 20 62 65 20 63 68 65 tables to be che
1e7ea 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b cked */. int j;
1e7eb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
1e7ec 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 p counter */. i
1e7ed 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 2f nt nErr; /
1e7ee 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f * Number of erro
1e7ef 72 73 20 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20 rs reported */.
1e7f0 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 char *z;
1e7f1 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 /* Text of the
1e7f2 65 72 72 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0a error report */.
1e7f3 20 20 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 Mem *pnErr;
1e7f4 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 6b 65 /* Register ke
1e7f5 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 eping track of e
1e7f6 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 rrors remaining
1e7f7 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 */.#endif /* loc
1e7f8 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e7f9 65 64 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a ed into u.bv */.
1e7fa 0a 20 20 75 2e 62 76 2e 6e 52 6f 6f 74 20 3d 20 . u.bv.nRoot =
1e7fb 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 pOp->p2;. asser
1e7fc 74 28 20 75 2e 62 76 2e 6e 52 6f 6f 74 3e 30 20 t( u.bv.nRoot>0
1e7fd 29 3b 0a 20 20 75 2e 62 76 2e 61 52 6f 6f 74 20 );. u.bv.aRoot
1e7fe 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
1e7ff 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 cRaw(db, sizeof(
1e800 69 6e 74 29 2a 28 75 2e 62 76 2e 6e 52 6f 6f 74 int)*(u.bv.nRoot
1e801 2b 31 29 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 +1) );. if( u.b
1e802 76 2e 61 52 6f 6f 74 3d 3d 30 20 29 20 67 6f 74 v.aRoot==0 ) got
1e803 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 61 73 73 65 o no_mem;. asse
1e804 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 rt( pOp->p3>0 &&
1e805 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 pOp->p3<=p->nMe
1e806 6d 20 29 3b 0a 20 20 75 2e 62 76 2e 70 6e 45 72 m );. u.bv.pnEr
1e807 72 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 r = &p->aMem[pOp
1e808 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p3];. assert(
1e809 20 28 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 66 6c (u.bv.pnErr->fl
1e80a 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d ags & MEM_Int)!=
1e80b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 0 );. assert( (
1e80c 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 66 6c 61 67 u.bv.pnErr->flag
1e80d 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d s & (MEM_Str|MEM
1e80e 5f 42 6c 6f 62 29 29 3d 3d 30 20 29 3b 0a 20 20 _Blob))==0 );.
1e80f 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pIn1 = &p->aMem[
1e810 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 pOp->p1];. for(
1e811 75 2e 62 76 2e 6a 3d 30 3b 20 75 2e 62 76 2e 6a u.bv.j=0; u.bv.j
1e812 3c 75 2e 62 76 2e 6e 52 6f 6f 74 3b 20 75 2e 62 <u.bv.nRoot; u.b
1e813 76 2e 6a 2b 2b 29 7b 0a 20 20 20 20 75 2e 62 76 v.j++){. u.bv
1e814 2e 61 52 6f 6f 74 5b 75 2e 62 76 2e 6a 5d 20 3d .aRoot[u.bv.j] =
1e815 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62 (int)sqlite3Vdb
1e816 65 49 6e 74 56 61 6c 75 65 28 26 70 49 6e 31 5b eIntValue(&pIn1[
1e817 75 2e 62 76 2e 6a 5d 29 3b 0a 20 20 7d 0a 20 20 u.bv.j]);. }.
1e818 75 2e 62 76 2e 61 52 6f 6f 74 5b 75 2e 62 76 2e u.bv.aRoot[u.bv.
1e819 6a 5d 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 j] = 0;. assert
1e81a 28 20 70 4f 70 2d 3e 70 35 3c 64 62 2d 3e 6e 44 ( pOp->p5<db->nD
1e81b 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 b );. assert( (
1e81c 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 p->btreeMask & (
1e81d 31 3c 3c 70 4f 70 2d 3e 70 35 29 29 21 3d 30 20 1<<pOp->p5))!=0
1e81e 29 3b 0a 20 20 75 2e 62 76 2e 7a 20 3d 20 73 71 );. u.bv.z = sq
1e81f 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 lite3BtreeIntegr
1e820 69 74 79 43 68 65 63 6b 28 64 62 2d 3e 61 44 62 ityCheck(db->aDb
1e821 5b 70 4f 70 2d 3e 70 35 5d 2e 70 42 74 2c 20 75 [pOp->p5].pBt, u
1e822 2e 62 76 2e 61 52 6f 6f 74 2c 20 75 2e 62 76 2e .bv.aRoot, u.bv.
1e823 6e 52 6f 6f 74 2c 0a 20 20 20 20 20 20 20 20 20 nRoot,.
1e824 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e825 20 20 20 20 20 20 20 20 28 69 6e 74 29 75 2e 62 (int)u.b
1e826 76 2e 70 6e 45 72 72 2d 3e 75 2e 69 2c 20 26 75 v.pnErr->u.i, &u
1e827 2e 62 76 2e 6e 45 72 72 29 3b 0a 20 20 73 71 6c .bv.nErr);. sql
1e828 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 75 ite3DbFree(db, u
1e829 2e 62 76 2e 61 52 6f 6f 74 29 3b 0a 20 20 75 2e .bv.aRoot);. u.
1e82a 62 76 2e 70 6e 45 72 72 2d 3e 75 2e 69 20 2d 3d bv.pnErr->u.i -=
1e82b 20 75 2e 62 76 2e 6e 45 72 72 3b 0a 20 20 73 71 u.bv.nErr;. sq
1e82c 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e lite3VdbeMemSetN
1e82d 75 6c 6c 28 70 49 6e 31 29 3b 0a 20 20 69 66 28 ull(pIn1);. if(
1e82e 20 75 2e 62 76 2e 6e 45 72 72 3d 3d 30 20 29 7b u.bv.nErr==0 ){
1e82f 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 . assert( u.b
1e830 76 2e 7a 3d 3d 30 20 29 3b 0a 20 20 7d 65 6c 73 v.z==0 );. }els
1e831 65 20 69 66 28 20 75 2e 62 76 2e 7a 3d 3d 30 20 e if( u.bv.z==0
1e832 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d ){. goto no_m
1e833 65 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 em;. }else{.
1e834 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1e835 65 74 53 74 72 28 70 49 6e 31 2c 20 75 2e 62 76 etStr(pIn1, u.bv
1e836 2e 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 .z, -1, SQLITE_U
1e837 54 46 38 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 TF8, sqlite3_fre
1e838 65 29 3b 0a 20 20 7d 0a 20 20 55 50 44 41 54 45 e);. }. UPDATE
1e839 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 49 _MAX_BLOBSIZE(pI
1e83a 6e 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 n1);. sqlite3Vd
1e83b 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 beChangeEncoding
1e83c 28 70 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 (pIn1, encoding)
1e83d 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e ;. break;.}.#en
1e83e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1e83f 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 IT_INTEGRITY_CHE
1e840 43 4b 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 CK */../* Opcode
1e841 3a 20 52 6f 77 53 65 74 41 64 64 20 50 31 20 50 : RowSetAdd P1 P
1e842 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 2 * * *.**.** In
1e843 73 65 72 74 20 74 68 65 20 69 6e 74 65 67 65 72 sert the integer
1e844 20 76 61 6c 75 65 20 68 65 6c 64 20 62 79 20 72 value held by r
1e845 65 67 69 73 74 65 72 20 50 32 20 69 6e 74 6f 20 egister P2 into
1e846 61 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 0a a boolean index.
1e847 2a 2a 20 68 65 6c 64 20 69 6e 20 72 65 67 69 73 ** held in regis
1e848 74 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 41 6e ter P1..**.** An
1e849 20 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 73 assertion fails
1e84a 20 69 66 20 50 32 20 69 73 20 6e 6f 74 20 61 6e if P2 is not an
1e84b 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 61 73 integer..*/.cas
1e84c 65 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 3a 20 e OP_RowSetAdd:
1e84d 7b 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 2a { /* in2 *
1e84e 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1e84f 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e850 64 20 69 6e 74 6f 20 75 2e 62 77 20 2a 2f 0a 20 d into u.bw */.
1e851 20 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20 4d 65 Mem *pIdx;. Me
1e852 6d 20 2a 70 56 61 6c 3b 0a 23 65 6e 64 69 66 20 m *pVal;.#endif
1e853 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1e854 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1e855 62 77 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 bw */. assert(
1e856 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 pOp->p1>0 && pOp
1e857 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p1<=p->nMem );
1e858 0a 20 20 75 2e 62 77 2e 70 49 64 78 20 3d 20 26 . u.bw.pIdx = &
1e859 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d p->aMem[pOp->p1]
1e85a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1e85b 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 >p2>0 && pOp->p2
1e85c 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 <=p->nMem );. u
1e85d 2e 62 77 2e 70 56 61 6c 20 3d 20 26 70 2d 3e 61 .bw.pVal = &p->a
1e85e 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 Mem[pOp->p2];.
1e85f 61 73 73 65 72 74 28 20 28 75 2e 62 77 2e 70 56 assert( (u.bw.pV
1e860 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f al->flags & MEM_
1e861 49 6e 74 29 21 3d 30 20 29 3b 0a 20 20 69 66 28 Int)!=0 );. if(
1e862 20 28 75 2e 62 77 2e 70 49 64 78 2d 3e 66 6c 61 (u.bw.pIdx->fla
1e863 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 gs & MEM_RowSet)
1e864 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ==0 ){. sqlit
1e865 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 e3VdbeMemSetRowS
1e866 65 74 28 75 2e 62 77 2e 70 49 64 78 29 3b 0a 20 et(u.bw.pIdx);.
1e867 20 20 20 69 66 28 20 28 75 2e 62 77 2e 70 49 64 if( (u.bw.pId
1e868 78 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 x->flags & MEM_R
1e869 6f 77 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f owSet)==0 ) goto
1e86a 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 73 no_mem;. }. s
1e86b 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 qlite3RowSetInse
1e86c 72 74 28 75 2e 62 77 2e 70 49 64 78 2d 3e 75 2e rt(u.bw.pIdx->u.
1e86d 70 52 6f 77 53 65 74 2c 20 75 2e 62 77 2e 70 56 pRowSet, u.bw.pV
1e86e 61 6c 2d 3e 75 2e 69 29 3b 0a 20 20 62 72 65 61 al->u.i);. brea
1e86f 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1e870 20 52 6f 77 53 65 74 52 65 61 64 20 50 31 20 50 RowSetRead P1 P
1e871 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 45 2 P3 * *.**.** E
1e872 78 74 72 61 63 74 20 74 68 65 20 73 6d 61 6c 6c xtract the small
1e873 65 73 74 20 76 61 6c 75 65 20 66 72 6f 6d 20 62 est value from b
1e874 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20 oolean index P1
1e875 61 6e 64 20 70 75 74 20 74 68 61 74 20 76 61 6c and put that val
1e876 75 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 ue into.** regis
1e877 74 65 72 20 50 33 2e 20 20 4f 72 2c 20 69 66 20 ter P3. Or, if
1e878 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 boolean index P1
1e879 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d is initially em
1e87a 70 74 79 2c 20 6c 65 61 76 65 20 50 33 0a 2a 2a pty, leave P3.**
1e87b 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 6a unchanged and j
1e87c 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 ump to instructi
1e87d 6f 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f on P2..*/.case O
1e87e 50 5f 52 6f 77 53 65 74 52 65 61 64 3a 20 7b 20 P_RowSetRead: {
1e87f 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 6f /* jump, o
1e880 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a ut3 */.#if 0 /*
1e881 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e882 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 78 moved into u.bx
1e883 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 64 78 3b */. Mem *pIdx;
1e884 0a 20 20 69 36 34 20 76 61 6c 3b 0a 23 65 6e 64 . i64 val;.#end
1e885 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1e886 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e887 20 75 2e 62 78 20 2a 2f 0a 20 20 61 73 73 65 72 u.bx */. asser
1e888 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 t( pOp->p1>0 &&
1e889 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p1<=p->nMem
1e88a 20 29 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f );. CHECK_FOR_
1e88b 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 75 2e 62 INTERRUPT;. u.b
1e88c 78 2e 70 49 64 78 20 3d 20 26 70 2d 3e 61 4d 65 x.pIdx = &p->aMe
1e88d 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f m[pOp->p1];. pO
1e88e 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ut = &p->aMem[pO
1e88f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 28 75 p->p3];. if( (u
1e890 2e 62 78 2e 70 49 64 78 2d 3e 66 6c 61 67 73 20 .bx.pIdx->flags
1e891 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 & MEM_RowSet)==0
1e892 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 52 6f . || sqlite3Ro
1e893 77 53 65 74 4e 65 78 74 28 75 2e 62 78 2e 70 49 wSetNext(u.bx.pI
1e894 64 78 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 26 dx->u.pRowSet, &
1e895 75 2e 62 78 2e 76 61 6c 29 3d 3d 30 0a 20 20 29 u.bx.val)==0. )
1e896 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 6f 6f {. /* The boo
1e897 6c 65 61 6e 20 69 6e 64 65 78 20 69 73 20 65 6d lean index is em
1e898 70 74 79 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 pty */. sqlit
1e899 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c e3VdbeMemSetNull
1e89a 28 75 2e 62 78 2e 70 49 64 78 29 3b 0a 20 20 20 (u.bx.pIdx);.
1e89b 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1e89c 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 1;. }else{.
1e89d 2f 2a 20 41 20 76 61 6c 75 65 20 77 61 73 20 70 /* A value was p
1e89e 75 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 69 ulled from the i
1e89f 6e 64 65 78 20 2a 2f 0a 20 20 20 20 61 73 73 65 ndex */. asse
1e8a0 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 rt( pOp->p3>0 &&
1e8a1 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 pOp->p3<=p->nMe
1e8a2 6d 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 m );. sqlite3
1e8a3 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 VdbeMemSetInt64(
1e8a4 70 4f 75 74 2c 20 75 2e 62 78 2e 76 61 6c 29 3b pOut, u.bx.val);
1e8a5 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1e8a6 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 ./* Opcode: RowS
1e8a7 65 74 54 65 73 74 20 50 31 20 50 32 20 50 33 20 etTest P1 P2 P3
1e8a8 50 34 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 P4.**.** Registe
1e8a9 72 20 50 33 20 69 73 20 61 73 73 75 6d 65 64 20 r P3 is assumed
1e8aa 74 6f 20 68 6f 6c 64 20 61 20 36 34 2d 62 69 74 to hold a 64-bit
1e8ab 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 20 integer value.
1e8ac 49 66 20 72 65 67 69 73 74 65 72 20 50 31 0a 2a If register P1.*
1e8ad 2a 20 63 6f 6e 74 61 69 6e 73 20 61 20 52 6f 77 * contains a Row
1e8ae 53 65 74 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 Set object and t
1e8af 68 61 74 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 hat RowSet objec
1e8b0 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 t contains.** th
1e8b1 65 20 76 61 6c 75 65 20 68 65 6c 64 20 69 6e 20 e value held in
1e8b2 50 33 2c 20 6a 75 6d 70 20 74 6f 20 72 65 67 69 P3, jump to regi
1e8b3 73 74 65 72 20 50 32 2e 20 4f 74 68 65 72 77 69 ster P2. Otherwi
1e8b4 73 65 2c 20 69 6e 73 65 72 74 20 74 68 65 0a 2a se, insert the.*
1e8b5 2a 20 69 6e 74 65 67 65 72 20 69 6e 20 50 33 20 * integer in P3
1e8b6 69 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 74 20 into the RowSet
1e8b7 61 6e 64 20 63 6f 6e 74 69 6e 75 65 20 6f 6e 20 and continue on
1e8b8 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6f to the.** next o
1e8b9 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 pcode..**.** The
1e8ba 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 69 RowSet object i
1e8bb 73 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72 20 s optimized for
1e8bc 74 68 65 20 63 61 73 65 20 77 68 65 72 65 20 73 the case where s
1e8bd 75 63 63 65 73 73 69 76 65 20 73 65 74 73 0a 2a uccessive sets.*
1e8be 2a 20 6f 66 20 69 6e 74 65 67 65 72 73 2c 20 77 * of integers, w
1e8bf 68 65 72 65 20 65 61 63 68 20 73 65 74 20 63 6f here each set co
1e8c0 6e 74 61 69 6e 73 20 6e 6f 20 64 75 70 6c 69 63 ntains no duplic
1e8c1 61 74 65 73 2e 20 45 61 63 68 20 73 65 74 0a 2a ates. Each set.*
1e8c2 2a 20 6f 66 20 76 61 6c 75 65 73 20 69 73 20 69 * of values is i
1e8c3 64 65 6e 74 69 66 69 65 64 20 62 79 20 61 20 75 dentified by a u
1e8c4 6e 69 71 75 65 20 50 34 20 76 61 6c 75 65 2e 20 nique P4 value.
1e8c5 54 68 65 20 66 69 72 73 74 20 73 65 74 0a 2a 2a The first set.**
1e8c6 20 6d 75 73 74 20 68 61 76 65 20 50 34 3d 3d 30 must have P4==0
1e8c7 2c 20 74 68 65 20 66 69 6e 61 6c 20 73 65 74 20 , the final set
1e8c8 50 34 3d 2d 31 2e 20 20 50 34 20 6d 75 73 74 20 P4=-1. P4 must
1e8c9 62 65 20 65 69 74 68 65 72 20 2d 31 20 6f 72 0a be either -1 or.
1e8ca 2a 2a 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e ** non-negative.
1e8cb 20 20 46 6f 72 20 6e 6f 6e 2d 6e 65 67 61 74 69 For non-negati
1e8cc 76 65 20 76 61 6c 75 65 73 20 6f 66 20 50 34 20 ve values of P4
1e8cd 6f 6e 6c 79 20 74 68 65 20 6c 6f 77 65 72 20 34 only the lower 4
1e8ce 0a 2a 2a 20 62 69 74 73 20 61 72 65 20 73 69 67 .** bits are sig
1e8cf 6e 69 66 69 63 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 nificant..**.**
1e8d0 54 68 69 73 20 61 6c 6c 6f 77 73 20 6f 70 74 69 This allows opti
1e8d1 6d 69 7a 61 74 69 6f 6e 73 3a 20 28 61 29 20 77 mizations: (a) w
1e8d2 68 65 6e 20 50 34 3d 3d 30 20 74 68 65 72 65 20 hen P4==0 there
1e8d3 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65 is no need to te
1e8d4 73 74 0a 2a 2a 20 74 68 65 20 72 6f 77 73 65 74 st.** the rowset
1e8d5 20 6f 62 6a 65 63 74 20 66 6f 72 20 50 33 2c 20 object for P3,
1e8d6 61 73 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 as it is guarant
1e8d7 65 65 64 20 6e 6f 74 20 74 6f 20 63 6f 6e 74 61 eed not to conta
1e8d8 69 6e 20 69 74 2c 0a 2a 2a 20 28 62 29 20 77 68 in it,.** (b) wh
1e8d9 65 6e 20 50 34 3d 3d 2d 31 20 74 68 65 72 65 20 en P4==-1 there
1e8da 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e is no need to in
1e8db 73 65 72 74 20 74 68 65 20 76 61 6c 75 65 2c 20 sert the value,
1e8dc 61 73 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 6e 65 as it will.** ne
1e8dd 76 65 72 20 62 65 20 74 65 73 74 65 64 20 66 6f ver be tested fo
1e8de 72 2c 20 61 6e 64 20 28 63 29 20 77 68 65 6e 20 r, and (c) when
1e8df 61 20 76 61 6c 75 65 20 74 68 61 74 20 69 73 20 a value that is
1e8e0 70 61 72 74 20 6f 66 20 73 65 74 20 58 20 69 73 part of set X is
1e8e1 0a 2a 2a 20 69 6e 73 65 72 74 65 64 2c 20 74 68 .** inserted, th
1e8e2 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 ere is no need t
1e8e3 6f 20 73 65 61 72 63 68 20 74 6f 20 73 65 65 20 o search to see
1e8e4 69 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 if the same valu
1e8e5 65 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 e was.** previou
1e8e6 73 6c 79 20 69 6e 73 65 72 74 65 64 20 61 73 20 sly inserted as
1e8e7 70 61 72 74 20 6f 66 20 73 65 74 20 58 20 28 6f part of set X (o
1e8e8 6e 6c 79 20 69 66 20 69 74 20 77 61 73 20 70 72 nly if it was pr
1e8e9 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 69 6e 73 65 eviously.** inse
1e8ea 72 74 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 rted as part of
1e8eb 73 6f 6d 65 20 6f 74 68 65 72 20 73 65 74 29 2e some other set).
1e8ec 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 .*/.case OP_RowS
1e8ed 65 74 54 65 73 74 3a 20 7b 20 20 20 20 20 20 20 etTest: {
1e8ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1e8ef 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 jump, in1, in3
1e8f0 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1e8f1 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e8f2 65 64 20 69 6e 74 6f 20 75 2e 62 79 20 2a 2f 0a ed into u.by */.
1e8f3 20 20 69 6e 74 20 69 53 65 74 3b 0a 20 20 69 6e int iSet;. in
1e8f4 74 20 65 78 69 73 74 73 3b 0a 23 65 6e 64 69 66 t exists;.#endif
1e8f5 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e8f6 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e8f7 2e 62 79 20 2a 2f 0a 0a 20 20 75 2e 62 79 2e 69 .by */.. u.by.i
1e8f8 53 65 74 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b Set = pOp->p4.i;
1e8f9 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d . assert( pIn3-
1e8fa 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 >flags&MEM_Int )
1e8fb 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 ;.. /* If there
1e8fc 20 69 73 20 61 6e 79 74 68 69 6e 67 20 6f 74 68 is anything oth
1e8fd 65 72 20 74 68 61 6e 20 61 20 72 6f 77 73 65 74 er than a rowset
1e8fe 20 6f 62 6a 65 63 74 20 69 6e 20 6d 65 6d 6f 72 object in memor
1e8ff 79 20 63 65 6c 6c 20 50 31 2c 0a 20 20 2a 2a 20 y cell P1,. **
1e900 64 65 6c 65 74 65 20 69 74 20 6e 6f 77 20 61 6e delete it now an
1e901 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 50 31 20 d initialize P1
1e902 77 69 74 68 20 61 6e 20 65 6d 70 74 79 20 72 6f with an empty ro
1e903 77 73 65 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 wset. */. if(
1e904 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d (pIn1->flags & M
1e905 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b EM_RowSet)==0 ){
1e906 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1e907 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 70 49 6e MemSetRowSet(pIn
1e908 31 29 3b 0a 20 20 20 20 69 66 28 20 28 70 49 6e 1);. if( (pIn
1e909 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 1->flags & MEM_R
1e90a 6f 77 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f owSet)==0 ) goto
1e90b 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 no_mem;. }..
1e90c 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 assert( pOp->p4t
1e90d 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b ype==P4_INT32 );
1e90e 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 79 2e . assert( u.by.
1e90f 69 53 65 74 3d 3d 2d 31 20 7c 7c 20 75 2e 62 79 iSet==-1 || u.by
1e910 2e 69 53 65 74 3e 3d 30 20 29 3b 0a 20 20 69 66 .iSet>=0 );. if
1e911 28 20 75 2e 62 79 2e 69 53 65 74 20 29 7b 0a 20 ( u.by.iSet ){.
1e912 20 20 20 75 2e 62 79 2e 65 78 69 73 74 73 20 3d u.by.exists =
1e913 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 sqlite3RowSetTe
1e914 73 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 st(pIn1->u.pRowS
1e915 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 et,.
1e916 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e917 20 20 20 28 75 38 29 28 75 2e 62 79 2e 69 53 65 (u8)(u.by.iSe
1e918 74 3e 3d 30 20 3f 20 75 2e 62 79 2e 69 53 65 74 t>=0 ? u.by.iSet
1e919 20 26 20 30 78 66 20 3a 20 30 78 66 66 29 2c 0a & 0xf : 0xff),.
1e91a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e91b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
1e91c 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 69 In3->u.i);. i
1e91d 66 28 20 75 2e 62 79 2e 65 78 69 73 74 73 20 29 f( u.by.exists )
1e91e 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 {. pc = pOp
1e91f 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 ->p2 - 1;.
1e920 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
1e921 0a 20 20 69 66 28 20 75 2e 62 79 2e 69 53 65 74 . if( u.by.iSet
1e922 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 >=0 ){. sqlit
1e923 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 70 e3RowSetInsert(p
1e924 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 In1->u.pRowSet,
1e925 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 7d 0a pIn3->u.i);. }.
1e926 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 break;.}...#if
1e927 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1e928 5f 54 52 49 47 47 45 52 0a 0a 2f 2a 20 4f 70 63 _TRIGGER../* Opc
1e929 6f 64 65 3a 20 50 72 6f 67 72 61 6d 20 50 31 20 ode: Program P1
1e92a 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a P2 P3 P4 *.**.**
1e92b 20 45 78 65 63 75 74 65 20 74 68 65 20 74 72 69 Execute the tri
1e92c 67 67 65 72 20 70 72 6f 67 72 61 6d 20 70 61 73 gger program pas
1e92d 73 65 64 20 61 73 20 50 34 20 28 74 79 70 65 20 sed as P4 (type
1e92e 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 29 2e 20 P4_SUBPROGRAM).
1e92f 0a 2a 2a 0a 2a 2a 20 50 31 20 63 6f 6e 74 61 69 .**.** P1 contai
1e930 6e 73 20 74 68 65 20 61 64 64 72 65 73 73 20 6f ns the address o
1e931 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c f the memory cel
1e932 6c 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 l that contains
1e933 74 68 65 20 66 69 72 73 74 20 6d 65 6d 6f 72 79 the first memory
1e934 20 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 61 6e 20 .** cell in an
1e935 61 72 72 61 79 20 6f 66 20 76 61 6c 75 65 73 20 array of values
1e936 75 73 65 64 20 61 73 20 61 72 67 75 6d 65 6e 74 used as argument
1e937 73 20 74 6f 20 74 68 65 20 73 75 62 2d 70 72 6f s to the sub-pro
1e938 67 72 61 6d 2e 20 50 32 20 0a 2a 2a 20 63 6f 6e gram. P2 .** con
1e939 74 61 69 6e 73 20 74 68 65 20 61 64 64 72 65 73 tains the addres
1e93a 73 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66 20 s to jump to if
1e93b 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 the sub-program
1e93c 74 68 72 6f 77 73 20 61 6e 20 49 47 4e 4f 52 45 throws an IGNORE
1e93d 20 0a 2a 2a 20 65 78 63 65 70 74 69 6f 6e 20 75 .** exception u
1e93e 73 69 6e 67 20 74 68 65 20 52 41 49 53 45 28 29 sing the RAISE()
1e93f 20 66 75 6e 63 74 69 6f 6e 2e 20 52 65 67 69 73 function. Regis
1e940 74 65 72 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 ter P3 contains
1e941 74 68 65 20 61 64 64 72 65 73 73 20 0a 2a 2a 20 the address .**
1e942 6f 66 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c of a memory cell
1e943 20 69 6e 20 74 68 69 73 20 28 74 68 65 20 70 61 in this (the pa
1e944 72 65 6e 74 29 20 56 4d 20 74 68 61 74 20 69 73 rent) VM that is
1e945 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 used to allocat
1e946 65 20 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f 72 79 e the .** memory
1e947 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 required by the
1e948 20 73 75 62 2d 76 64 62 65 20 61 74 20 72 75 6e sub-vdbe at run
1e949 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 time..**.** P4 i
1e94a 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
1e94b 68 65 20 56 4d 20 63 6f 6e 74 61 69 6e 69 6e 67 he VM containing
1e94c 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f the trigger pro
1e94d 67 72 61 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 gram..*/.case OP
1e94e 5f 50 72 6f 67 72 61 6d 3a 20 7b 20 20 20 20 20 _Program: {
1e94f 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 /* jump */.#i
1e950 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1e951 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e952 74 6f 20 75 2e 62 7a 20 2a 2f 0a 20 20 69 6e 74 to u.bz */. int
1e953 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 nMem;
1e954 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1e955 66 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 f memory registe
1e956 72 73 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 rs for sub-progr
1e957 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 am */. int nByt
1e958 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
1e959 2f 2a 20 42 79 74 65 73 20 6f 66 20 72 75 6e 74 /* Bytes of runt
1e95a 69 6d 65 20 73 70 61 63 65 20 72 65 71 75 69 72 ime space requir
1e95b 65 64 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 ed for sub-progr
1e95c 61 6d 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 74 am */. Mem *pRt
1e95d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1e95e 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 61 /* Register to a
1e95f 6c 6c 6f 63 61 74 65 20 72 75 6e 74 69 6d 65 20 llocate runtime
1e960 73 70 61 63 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a space */. Mem *
1e961 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 pMem;
1e962 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 /* Used to it
1e963 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 6d 65 erate through me
1e964 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 mory cells */.
1e965 4d 65 6d 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 Mem *pEnd;
1e966 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 /* Last
1e967 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 6e memory cell in n
1e968 65 77 20 61 72 72 61 79 20 2a 2f 0a 20 20 56 64 ew array */. Vd
1e969 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b beFrame *pFrame;
1e96a 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 64 62 /* New vdb
1e96b 65 20 66 72 61 6d 65 20 74 6f 20 65 78 65 63 75 e frame to execu
1e96c 74 65 20 69 6e 20 2a 2f 0a 20 20 53 75 62 50 72 te in */. SubPr
1e96d 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b ogram *pProgram;
1e96e 20 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61 /* Sub-progra
1e96f 6d 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a m to execute */.
1e970 20 20 76 6f 69 64 20 2a 74 3b 20 20 20 20 20 20 void *t;
1e971 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b /* Tok
1e972 65 6e 20 69 64 65 6e 74 69 66 79 69 6e 67 20 74 en identifying t
1e973 72 69 67 67 65 72 20 2a 2f 0a 23 65 6e 64 69 66 rigger */.#endif
1e974 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e975 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e976 2e 62 7a 20 2a 2f 0a 0a 20 20 75 2e 62 7a 2e 70 .bz */.. u.bz.p
1e977 50 72 6f 67 72 61 6d 20 3d 20 70 4f 70 2d 3e 70 Program = pOp->p
1e978 34 2e 70 50 72 6f 67 72 61 6d 3b 0a 20 20 75 2e 4.pProgram;. u.
1e979 62 7a 2e 70 52 74 20 3d 20 26 70 2d 3e 61 4d 65 bz.pRt = &p->aMe
1e97a 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 m[pOp->p3];. as
1e97b 73 65 72 74 28 20 75 2e 62 7a 2e 70 50 72 6f 67 sert( u.bz.pProg
1e97c 72 61 6d 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 0a 20 ram->nOp>0 );..
1e97d 20 2f 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 /* If the SQLIT
1e97e 45 5f 52 65 63 54 72 69 67 67 65 72 73 20 66 6c E_RecTriggers fl
1e97f 61 67 20 69 73 20 63 6c 65 61 72 2c 20 74 68 65 ag is clear, the
1e980 6e 20 72 65 63 75 72 73 69 76 65 20 69 6e 76 6f n recursive invo
1e981 63 61 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 cation of. ** t
1e982 72 69 67 67 65 72 73 20 69 73 20 64 69 73 61 62 riggers is disab
1e983 6c 65 64 20 66 6f 72 20 62 61 63 6b 77 61 72 64 led for backward
1e984 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 s compatibility
1e985 28 66 6c 61 67 20 73 65 74 2f 63 6c 65 61 72 65 (flag set/cleare
1e986 64 20 62 79 0a 20 20 2a 2a 20 74 68 65 20 22 50 d by. ** the "P
1e987 52 41 47 4d 41 20 72 65 63 75 72 73 69 76 65 5f RAGMA recursive_
1e988 74 72 69 67 67 65 72 73 22 20 63 6f 6d 6d 61 6e triggers" comman
1e989 64 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 74 d).. **. ** It
1e98a 20 69 73 20 72 65 63 75 72 73 69 76 65 20 69 6e is recursive in
1e98b 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 72 69 67 vocation of trig
1e98c 67 65 72 73 2c 20 61 74 20 74 68 65 20 53 51 4c gers, at the SQL
1e98d 20 6c 65 76 65 6c 2c 20 74 68 61 74 20 69 73 0a level, that is.
1e98e 20 20 2a 2a 20 64 69 73 61 62 6c 65 64 2e 20 49 ** disabled. I
1e98f 6e 20 73 6f 6d 65 20 63 61 73 65 73 20 61 20 73 n some cases a s
1e990 69 6e 67 6c 65 20 74 72 69 67 67 65 72 20 6d 61 ingle trigger ma
1e991 79 20 67 65 6e 65 72 61 74 65 20 6d 6f 72 65 20 y generate more
1e992 74 68 61 6e 20 6f 6e 65 0a 20 20 2a 2a 20 53 75 than one. ** Su
1e993 62 50 72 6f 67 72 61 6d 20 28 69 66 20 74 68 65 bProgram (if the
1e994 20 74 72 69 67 67 65 72 20 6d 61 79 20 62 65 20 trigger may be
1e995 65 78 65 63 75 74 65 64 20 77 69 74 68 20 6d 6f executed with mo
1e996 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 69 66 66 re than one diff
1e997 65 72 65 6e 74 0a 20 20 2a 2a 20 4f 4e 20 43 4f erent. ** ON CO
1e998 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d NFLICT algorithm
1e999 29 2e 20 53 75 62 50 72 6f 67 72 61 6d 20 73 74 ). SubProgram st
1e99a 72 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 61 ructures associa
1e99b 74 65 64 20 77 69 74 68 20 61 0a 20 20 2a 2a 20 ted with a. **
1e99c 73 69 6e 67 6c 65 20 74 72 69 67 67 65 72 20 61 single trigger a
1e99d 6c 6c 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 ll have the same
1e99e 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 53 value for the S
1e99f 75 62 50 72 6f 67 72 61 6d 2e 74 6f 6b 65 6e 0a ubProgram.token.
1e9a0 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 2e 0a 20 ** variable..
1e9a1 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 28 64 62 */. if( 0==(db
1e9a2 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 ->flags&SQLITE_R
1e9a3 65 63 54 72 69 67 67 65 72 73 29 20 29 7b 0a 20 ecTriggers) ){.
1e9a4 20 20 20 75 2e 62 7a 2e 74 20 3d 20 75 2e 62 7a u.bz.t = u.bz
1e9a5 2e 70 50 72 6f 67 72 61 6d 2d 3e 74 6f 6b 65 6e .pProgram->token
1e9a6 3b 0a 20 20 20 20 66 6f 72 28 75 2e 62 7a 2e 70 ;. for(u.bz.p
1e9a7 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b Frame=p->pFrame;
1e9a8 20 75 2e 62 7a 2e 70 46 72 61 6d 65 20 26 26 20 u.bz.pFrame &&
1e9a9 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 74 6f 6b u.bz.pFrame->tok
1e9aa 65 6e 21 3d 75 2e 62 7a 2e 74 3b 20 75 2e 62 7a en!=u.bz.t; u.bz
1e9ab 2e 70 46 72 61 6d 65 3d 75 2e 62 7a 2e 70 46 72 .pFrame=u.bz.pFr
1e9ac 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 ame->pParent);.
1e9ad 20 20 20 69 66 28 20 75 2e 62 7a 2e 70 46 72 61 if( u.bz.pFra
1e9ae 6d 65 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a me ) break;. }.
1e9af 0a 20 20 69 66 28 20 70 2d 3e 6e 46 72 61 6d 65 . if( p->nFrame
1e9b0 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
1e9b1 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 TE_LIMIT_TRIGGER
1e9b2 5f 44 45 50 54 48 5d 20 29 7b 0a 20 20 20 20 72 _DEPTH] ){. r
1e9b3 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
1e9b4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 ;. sqlite3Set
1e9b5 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d String(&p->zErrM
1e9b6 73 67 2c 20 64 62 2c 20 22 74 6f 6f 20 6d 61 6e sg, db, "too man
1e9b7 79 20 6c 65 76 65 6c 73 20 6f 66 20 74 72 69 67 y levels of trig
1e9b8 67 65 72 20 72 65 63 75 72 73 69 6f 6e 22 29 3b ger recursion");
1e9b9 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a . break;. }.
1e9ba 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75 . /* Register u
1e9bb 2e 62 7a 2e 70 52 74 20 69 73 20 75 73 65 64 20 .bz.pRt is used
1e9bc 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6d 65 6d to store the mem
1e9bd 6f 72 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 ory required to
1e9be 73 61 76 65 20 74 68 65 20 73 74 61 74 65 0a 20 save the state.
1e9bf 20 2a 2a 20 6f 66 20 74 68 65 20 63 75 72 72 65 ** of the curre
1e9c0 6e 74 20 70 72 6f 67 72 61 6d 2c 20 61 6e 64 20 nt program, and
1e9c1 74 68 65 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 the memory requi
1e9c2 72 65 64 20 61 74 20 72 75 6e 74 69 6d 65 20 74 red at runtime t
1e9c3 6f 20 65 78 65 63 75 74 65 0a 20 20 2a 2a 20 74 o execute. ** t
1e9c4 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 he trigger progr
1e9c5 61 6d 2e 20 49 66 20 74 68 69 73 20 74 72 69 67 am. If this trig
1e9c6 67 65 72 20 68 61 73 20 62 65 65 6e 20 66 69 72 ger has been fir
1e9c7 65 64 20 62 65 66 6f 72 65 2c 20 74 68 65 6e 20 ed before, then
1e9c8 75 2e 62 7a 2e 70 52 74 0a 20 20 2a 2a 20 69 73 u.bz.pRt. ** is
1e9c9 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 already allocat
1e9ca 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 ed. Otherwise, i
1e9cb 74 20 6d 75 73 74 20 62 65 20 69 6e 69 74 69 61 t must be initia
1e9cc 6c 69 7a 65 64 2e 20 20 2a 2f 0a 20 20 69 66 28 lized. */. if(
1e9cd 20 28 75 2e 62 7a 2e 70 52 74 2d 3e 66 6c 61 67 (u.bz.pRt->flag
1e9ce 73 26 4d 45 4d 5f 46 72 61 6d 65 29 3d 3d 30 20 s&MEM_Frame)==0
1e9cf 29 7b 0a 20 20 20 20 2f 2a 20 53 75 62 50 72 6f ){. /* SubPro
1e9d0 67 72 61 6d 2e 6e 4d 65 6d 20 69 73 20 73 65 74 gram.nMem is set
1e9d1 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f to the number o
1e9d2 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 75 f memory cells u
1e9d3 73 65 64 20 62 79 20 74 68 65 0a 20 20 20 20 2a sed by the. *
1e9d4 2a 20 70 72 6f 67 72 61 6d 20 73 74 6f 72 65 64 * program stored
1e9d5 20 69 6e 20 53 75 62 50 72 6f 67 72 61 6d 2e 61 in SubProgram.a
1e9d6 4f 70 2e 20 41 73 20 77 65 6c 6c 20 61 73 20 74 Op. As well as t
1e9d7 68 65 73 65 2c 20 6f 6e 65 20 6d 65 6d 6f 72 79 hese, one memory
1e9d8 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 69 73 20 . ** cell is
1e9d9 72 65 71 75 69 72 65 64 20 66 6f 72 20 65 61 63 required for eac
1e9da 68 20 63 75 72 73 6f 72 20 75 73 65 64 20 62 79 h cursor used by
1e9db 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 53 65 the program. Se
1e9dc 74 20 6c 6f 63 61 6c 0a 20 20 20 20 2a 2a 20 76 t local. ** v
1e9dd 61 72 69 61 62 6c 65 20 75 2e 62 7a 2e 6e 4d 65 ariable u.bz.nMe
1e9de 6d 20 28 61 6e 64 20 6c 61 74 65 72 2c 20 56 64 m (and later, Vd
1e9df 62 65 46 72 61 6d 65 2e 6e 43 68 69 6c 64 4d 65 beFrame.nChildMe
1e9e0 6d 29 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 m) to this value
1e9e1 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 75 2e 62 .. */. u.b
1e9e2 7a 2e 6e 4d 65 6d 20 3d 20 75 2e 62 7a 2e 70 50 z.nMem = u.bz.pP
1e9e3 72 6f 67 72 61 6d 2d 3e 6e 4d 65 6d 20 2b 20 75 rogram->nMem + u
1e9e4 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 .bz.pProgram->nC
1e9e5 73 72 3b 0a 20 20 20 20 75 2e 62 7a 2e 6e 42 79 sr;. u.bz.nBy
1e9e6 74 65 20 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 te = ROUND8(size
1e9e7 6f 66 28 56 64 62 65 46 72 61 6d 65 29 29 0a 20 of(VdbeFrame)).
1e9e8 20 20 20 20 20 20 20 20 20 20 20 20 20 2b 20 75 + u
1e9e9 2e 62 7a 2e 6e 4d 65 6d 20 2a 20 73 69 7a 65 6f .bz.nMem * sizeo
1e9ea 66 28 4d 65 6d 29 0a 20 20 20 20 20 20 20 20 20 f(Mem).
1e9eb 20 20 20 20 20 2b 20 75 2e 62 7a 2e 70 50 72 6f + u.bz.pPro
1e9ec 67 72 61 6d 2d 3e 6e 43 73 72 20 2a 20 73 69 7a gram->nCsr * siz
1e9ed 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 20 2a eof(VdbeCursor *
1e9ee 29 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 );. u.bz.pFra
1e9ef 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 me = sqlite3DbMa
1e9f0 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 75 2e 62 llocZero(db, u.b
1e9f1 7a 2e 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 z.nByte);. if
1e9f2 28 20 21 75 2e 62 7a 2e 70 46 72 61 6d 65 20 29 ( !u.bz.pFrame )
1e9f3 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f {. goto no_
1e9f4 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 mem;. }. s
1e9f5 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1e9f6 65 61 73 65 28 75 2e 62 7a 2e 70 52 74 29 3b 0a ease(u.bz.pRt);.
1e9f7 20 20 20 20 75 2e 62 7a 2e 70 52 74 2d 3e 66 6c u.bz.pRt->fl
1e9f8 61 67 73 20 3d 20 4d 45 4d 5f 46 72 61 6d 65 3b ags = MEM_Frame;
1e9f9 0a 20 20 20 20 75 2e 62 7a 2e 70 52 74 2d 3e 75 . u.bz.pRt->u
1e9fa 2e 70 46 72 61 6d 65 20 3d 20 75 2e 62 7a 2e 70 .pFrame = u.bz.p
1e9fb 46 72 61 6d 65 3b 0a 0a 20 20 20 20 75 2e 62 7a Frame;.. u.bz
1e9fc 2e 70 46 72 61 6d 65 2d 3e 76 20 3d 20 70 3b 0a .pFrame->v = p;.
1e9fd 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d u.bz.pFrame-
1e9fe 3e 6e 43 68 69 6c 64 4d 65 6d 20 3d 20 75 2e 62 >nChildMem = u.b
1e9ff 7a 2e 6e 4d 65 6d 3b 0a 20 20 20 20 75 2e 62 7a z.nMem;. u.bz
1ea00 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 43 .pFrame->nChildC
1ea01 73 72 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 sr = u.bz.pProgr
1ea02 61 6d 2d 3e 6e 43 73 72 3b 0a 20 20 20 20 75 2e am->nCsr;. u.
1ea03 62 7a 2e 70 46 72 61 6d 65 2d 3e 70 63 20 3d 20 bz.pFrame->pc =
1ea04 70 63 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 pc;. u.bz.pFr
1ea05 61 6d 65 2d 3e 61 4d 65 6d 20 3d 20 70 2d 3e 61 ame->aMem = p->a
1ea06 4d 65 6d 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 Mem;. u.bz.pF
1ea07 72 61 6d 65 2d 3e 6e 4d 65 6d 20 3d 20 70 2d 3e rame->nMem = p->
1ea08 6e 4d 65 6d 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 nMem;. u.bz.p
1ea09 46 72 61 6d 65 2d 3e 61 70 43 73 72 20 3d 20 70 Frame->apCsr = p
1ea0a 2d 3e 61 70 43 73 72 3b 0a 20 20 20 20 75 2e 62 ->apCsr;. u.b
1ea0b 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 75 72 73 6f z.pFrame->nCurso
1ea0c 72 20 3d 20 70 2d 3e 6e 43 75 72 73 6f 72 3b 0a r = p->nCursor;.
1ea0d 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d u.bz.pFrame-
1ea0e 3e 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 >aOp = p->aOp;.
1ea0f 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e u.bz.pFrame->
1ea10 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 nOp = p->nOp;.
1ea11 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 74 u.bz.pFrame->t
1ea12 6f 6b 65 6e 20 3d 20 75 2e 62 7a 2e 70 50 72 6f oken = u.bz.pPro
1ea13 67 72 61 6d 2d 3e 74 6f 6b 65 6e 3b 0a 0a 20 20 gram->token;..
1ea14 20 20 75 2e 62 7a 2e 70 45 6e 64 20 3d 20 26 56 u.bz.pEnd = &V
1ea15 64 62 65 46 72 61 6d 65 4d 65 6d 28 75 2e 62 7a dbeFrameMem(u.bz
1ea16 2e 70 46 72 61 6d 65 29 5b 75 2e 62 7a 2e 70 46 .pFrame)[u.bz.pF
1ea17 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d 5d rame->nChildMem]
1ea18 3b 0a 20 20 20 20 66 6f 72 28 75 2e 62 7a 2e 70 ;. for(u.bz.p
1ea19 4d 65 6d 3d 56 64 62 65 46 72 61 6d 65 4d 65 6d Mem=VdbeFrameMem
1ea1a 28 75 2e 62 7a 2e 70 46 72 61 6d 65 29 3b 20 75 (u.bz.pFrame); u
1ea1b 2e 62 7a 2e 70 4d 65 6d 21 3d 75 2e 62 7a 2e 70 .bz.pMem!=u.bz.p
1ea1c 45 6e 64 3b 20 75 2e 62 7a 2e 70 4d 65 6d 2b 2b End; u.bz.pMem++
1ea1d 29 7b 0a 20 20 20 20 20 20 75 2e 62 7a 2e 70 4d ){. u.bz.pM
1ea1e 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
1ea1f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 75 2e 62 7a Null;. u.bz
1ea20 2e 70 4d 65 6d 2d 3e 64 62 20 3d 20 64 62 3b 0a .pMem->db = db;.
1ea21 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1ea22 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 20 3d u.bz.pFrame =
1ea23 20 75 2e 62 7a 2e 70 52 74 2d 3e 75 2e 70 46 72 u.bz.pRt->u.pFr
1ea24 61 6d 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ame;. assert(
1ea25 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e u.bz.pProgram->
1ea26 6e 4d 65 6d 2b 75 2e 62 7a 2e 70 50 72 6f 67 72 nMem+u.bz.pProgr
1ea27 61 6d 2d 3e 6e 43 73 72 3d 3d 75 2e 62 7a 2e 70 am->nCsr==u.bz.p
1ea28 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d Frame->nChildMem
1ea29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1ea2a 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e u.bz.pProgram->n
1ea2b 43 73 72 3d 3d 75 2e 62 7a 2e 70 46 72 61 6d 65 Csr==u.bz.pFrame
1ea2c 2d 3e 6e 43 68 69 6c 64 43 73 72 20 29 3b 0a 20 ->nChildCsr );.
1ea2d 20 20 20 61 73 73 65 72 74 28 20 70 63 3d 3d 75 assert( pc==u
1ea2e 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 70 63 20 29 .bz.pFrame->pc )
1ea2f 3b 0a 20 20 7d 0a 0a 20 20 70 2d 3e 6e 46 72 61 ;. }.. p->nFra
1ea30 6d 65 2b 2b 3b 0a 20 20 75 2e 62 7a 2e 70 46 72 me++;. u.bz.pFr
1ea31 61 6d 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 ame->pParent = p
1ea32 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 75 2e 62 7a ->pFrame;. u.bz
1ea33 2e 70 46 72 61 6d 65 2d 3e 6c 61 73 74 52 6f 77 .pFrame->lastRow
1ea34 69 64 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77 id = db->lastRow
1ea35 69 64 3b 0a 20 20 75 2e 62 7a 2e 70 46 72 61 6d id;. u.bz.pFram
1ea36 65 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70 2d 3e e->nChange = p->
1ea37 6e 43 68 61 6e 67 65 3b 0a 20 20 70 2d 3e 6e 43 nChange;. p->nC
1ea38 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e hange = 0;. p->
1ea39 70 46 72 61 6d 65 20 3d 20 75 2e 62 7a 2e 70 46 pFrame = u.bz.pF
1ea3a 72 61 6d 65 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20 rame;. p->aMem
1ea3b 3d 20 26 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 = &VdbeFrameMem(
1ea3c 75 2e 62 7a 2e 70 46 72 61 6d 65 29 5b 2d 31 5d u.bz.pFrame)[-1]
1ea3d 3b 0a 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 75 2e ;. p->nMem = u.
1ea3e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c bz.pFrame->nChil
1ea3f 64 4d 65 6d 3b 0a 20 20 70 2d 3e 6e 43 75 72 73 dMem;. p->nCurs
1ea40 6f 72 20 3d 20 28 75 31 36 29 75 2e 62 7a 2e 70 or = (u16)u.bz.p
1ea41 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 43 73 72 Frame->nChildCsr
1ea42 3b 0a 20 20 70 2d 3e 61 70 43 73 72 20 3d 20 28 ;. p->apCsr = (
1ea43 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 29 26 70 VdbeCursor **)&p
1ea44 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2b 31 ->aMem[p->nMem+1
1ea45 5d 3b 0a 20 20 70 2d 3e 61 4f 70 20 3d 20 75 2e ];. p->aOp = u.
1ea46 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 61 4f 70 bz.pProgram->aOp
1ea47 3b 0a 20 20 70 2d 3e 6e 4f 70 20 3d 20 75 2e 62 ;. p->nOp = u.b
1ea48 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 70 3b z.pProgram->nOp;
1ea49 0a 20 20 70 63 20 3d 20 2d 31 3b 0a 0a 20 20 62 . pc = -1;.. b
1ea4a 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1ea4b 64 65 3a 20 50 61 72 61 6d 20 50 31 20 50 32 20 de: Param P1 P2
1ea4c 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 * * *.**.** This
1ea4d 20 6f 70 63 6f 64 65 20 69 73 20 6f 6e 6c 79 20 opcode is only
1ea4e 65 76 65 72 20 70 72 65 73 65 6e 74 20 69 6e 20 ever present in
1ea4f 73 75 62 2d 70 72 6f 67 72 61 6d 73 20 63 61 6c sub-programs cal
1ea50 6c 65 64 20 76 69 61 20 74 68 65 20 0a 2a 2a 20 led via the .**
1ea51 4f 50 5f 50 72 6f 67 72 61 6d 20 69 6e 73 74 72 OP_Program instr
1ea52 75 63 74 69 6f 6e 2e 20 43 6f 70 79 20 61 20 76 uction. Copy a v
1ea53 61 6c 75 65 20 63 75 72 72 65 6e 74 6c 79 20 73 alue currently s
1ea54 74 6f 72 65 64 20 69 6e 20 61 20 6d 65 6d 6f 72 tored in a memor
1ea55 79 20 0a 2a 2a 20 63 65 6c 6c 20 6f 66 20 74 68 y .** cell of th
1ea56 65 20 63 61 6c 6c 69 6e 67 20 28 70 61 72 65 6e e calling (paren
1ea57 74 29 20 66 72 61 6d 65 20 74 6f 20 63 65 6c 6c t) frame to cell
1ea58 20 50 32 20 69 6e 20 74 68 65 20 63 75 72 72 65 P2 in the curre
1ea59 6e 74 20 66 72 61 6d 65 73 20 0a 2a 2a 20 61 64 nt frames .** ad
1ea5a 64 72 65 73 73 20 73 70 61 63 65 2e 20 54 68 69 dress space. Thi
1ea5b 73 20 69 73 20 75 73 65 64 20 62 79 20 74 72 69 s is used by tri
1ea5c 67 67 65 72 20 70 72 6f 67 72 61 6d 73 20 74 6f gger programs to
1ea5d 20 61 63 63 65 73 73 20 74 68 65 20 6e 65 77 2e access the new.
1ea5e 2a 20 0a 2a 2a 20 61 6e 64 20 6f 6c 64 2e 2a 20 * .** and old.*
1ea5f 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 values..**.** Th
1ea60 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 e address of the
1ea61 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 72 cell in the par
1ea62 65 6e 74 20 66 72 61 6d 65 20 69 73 20 64 65 74 ent frame is det
1ea63 65 72 6d 69 6e 65 64 20 62 79 20 61 64 64 69 6e ermined by addin
1ea64 67 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f g.** the value o
1ea65 66 20 74 68 65 20 50 31 20 61 72 67 75 6d 65 6e f the P1 argumen
1ea66 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f t to the value o
1ea67 66 20 74 68 65 20 50 31 20 61 72 67 75 6d 65 6e f the P1 argumen
1ea68 74 20 74 6f 20 74 68 65 0a 2a 2a 20 63 61 6c 6c t to the.** call
1ea69 69 6e 67 20 4f 50 5f 50 72 6f 67 72 61 6d 20 69 ing OP_Program i
1ea6a 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 nstruction..*/.c
1ea6b 61 73 65 20 4f 50 5f 50 61 72 61 6d 3a 20 7b 20 ase OP_Param: {
1ea6c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 /* out
1ea6d 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 2-prerelease */.
1ea6e 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1ea6f 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1ea70 69 6e 74 6f 20 75 2e 63 61 20 2a 2f 0a 20 20 56 into u.ca */. V
1ea71 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 dbeFrame *pFrame
1ea72 3b 0a 20 20 4d 65 6d 20 2a 70 49 6e 3b 0a 23 65 ;. Mem *pIn;.#e
1ea73 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1ea74 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1ea75 74 6f 20 75 2e 63 61 20 2a 2f 0a 20 20 75 2e 63 to u.ca */. u.c
1ea76 61 2e 70 46 72 61 6d 65 20 3d 20 70 2d 3e 70 46 a.pFrame = p->pF
1ea77 72 61 6d 65 3b 0a 20 20 75 2e 63 61 2e 70 49 6e rame;. u.ca.pIn
1ea78 20 3d 20 26 75 2e 63 61 2e 70 46 72 61 6d 65 2d = &u.ca.pFrame-
1ea79 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 20 2b 20 >aMem[pOp->p1 +
1ea7a 75 2e 63 61 2e 70 46 72 61 6d 65 2d 3e 61 4f 70 u.ca.pFrame->aOp
1ea7b 5b 75 2e 63 61 2e 70 46 72 61 6d 65 2d 3e 70 63 [u.ca.pFrame->pc
1ea7c 5d 2e 70 31 5d 3b 0a 20 20 73 71 6c 69 74 65 33 ].p1];. sqlite3
1ea7d 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f VdbeMemShallowCo
1ea7e 70 79 28 70 4f 75 74 2c 20 75 2e 63 61 2e 70 49 py(pOut, u.ca.pI
1ea7f 6e 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 n, MEM_Ephem);.
1ea80 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 65 6e 64 69 break;.}..#endi
1ea81 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c f /* #ifndef SQL
1ea82 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
1ea83 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
1ea84 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 ITE_OMIT_AUTOINC
1ea85 52 45 4d 45 4e 54 0a 2f 2a 20 4f 70 63 6f 64 65 REMENT./* Opcode
1ea86 3a 20 4d 65 6d 4d 61 78 20 50 31 20 50 32 20 2a : MemMax P1 P2 *
1ea87 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 * *.**.** P1 is
1ea88 20 61 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 a register in t
1ea89 68 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66 he root frame of
1ea8a 20 74 68 69 73 20 56 4d 20 28 74 68 65 20 72 6f this VM (the ro
1ea8b 6f 74 20 66 72 61 6d 65 20 69 73 0a 2a 2a 20 64 ot frame is.** d
1ea8c 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 ifferent from th
1ea8d 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 20 e current frame
1ea8e 69 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 if this instruct
1ea8f 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 65 78 65 ion is being exe
1ea90 63 75 74 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 cuted.** within
1ea91 61 20 73 75 62 2d 70 72 6f 67 72 61 6d 29 2e 20 a sub-program).
1ea92 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 Set the value of
1ea93 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 register P1 to
1ea94 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 0a the maximum of .
1ea95 2a 2a 20 69 74 73 20 63 75 72 72 65 6e 74 20 76 ** its current v
1ea96 61 6c 75 65 20 61 6e 64 20 74 68 65 20 76 61 6c alue and the val
1ea97 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1ea98 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 2..**.** This in
1ea99 73 74 72 75 63 74 69 6f 6e 20 74 68 72 6f 77 73 struction throws
1ea9a 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 an error if the
1ea9b 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20 memory cell is
1ea9c 6e 6f 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a not initially.**
1ea9d 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a an integer..*/.
1ea9e 63 61 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a 20 case OP_MemMax:
1ea9f 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 { /* in2
1eaa0 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1eaa1 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1eaa2 65 64 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a ed into u.cb */.
1eaa3 20 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20 56 Mem *pIn1;. V
1eaa4 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 dbeFrame *pFrame
1eaa5 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1eaa6 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1eaa7 64 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a 20 d into u.cb */.
1eaa8 20 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29 if( p->pFrame )
1eaa9 7b 0a 20 20 20 20 66 6f 72 28 75 2e 63 62 2e 70 {. for(u.cb.p
1eaaa 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b Frame=p->pFrame;
1eaab 20 75 2e 63 62 2e 70 46 72 61 6d 65 2d 3e 70 50 u.cb.pFrame->pP
1eaac 61 72 65 6e 74 3b 20 75 2e 63 62 2e 70 46 72 61 arent; u.cb.pFra
1eaad 6d 65 3d 75 2e 63 62 2e 70 46 72 61 6d 65 2d 3e me=u.cb.pFrame->
1eaae 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 75 2e pParent);. u.
1eaaf 63 62 2e 70 49 6e 31 20 3d 20 26 75 2e 63 62 2e cb.pIn1 = &u.cb.
1eab0 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 pFrame->aMem[pOp
1eab1 2d 3e 70 31 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a ->p1];. }else{.
1eab2 20 20 20 20 75 2e 63 62 2e 70 49 6e 31 20 3d 20 u.cb.pIn1 =
1eab3 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 &p->aMem[pOp->p1
1eab4 5d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 ];. }. sqlite3
1eab5 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 VdbeMemIntegerif
1eab6 79 28 75 2e 63 62 2e 70 49 6e 31 29 3b 0a 20 20 y(u.cb.pIn1);.
1eab7 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e sqlite3VdbeMemIn
1eab8 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b 0a tegerify(pIn2);.
1eab9 20 20 69 66 28 20 75 2e 63 62 2e 70 49 6e 31 2d if( u.cb.pIn1-
1eaba 3e 75 2e 69 3c 70 49 6e 32 2d 3e 75 2e 69 29 7b >u.i<pIn2->u.i){
1eabb 0a 20 20 20 20 75 2e 63 62 2e 70 49 6e 31 2d 3e . u.cb.pIn1->
1eabc 75 2e 69 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b u.i = pIn2->u.i;
1eabd 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1eabe 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1eabf 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d _OMIT_AUTOINCREM
1eac0 45 4e 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 ENT */../* Opcod
1eac1 65 3a 20 49 66 50 6f 73 20 50 31 20 50 32 20 2a e: IfPos P1 P2 *
1eac2 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 * *.**.** If th
1eac3 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 e value of regis
1eac4 74 65 72 20 50 31 20 69 73 20 31 20 6f 72 20 67 ter P1 is 1 or g
1eac5 72 65 61 74 65 72 2c 20 6a 75 6d 70 20 74 6f 20 reater, jump to
1eac6 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 P2..**.** It is
1eac7 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74 illegal to use t
1eac8 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1eac9 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68 on a register th
1eaca 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 at does.** not c
1eacb 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 ontain an intege
1eacc 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e r. An assertion
1eacd 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 fault will resu
1eace 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a lt if you try..*
1eacf 2f 0a 63 61 73 65 20 4f 50 5f 49 66 50 6f 73 3a /.case OP_IfPos:
1ead0 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1ead1 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 p, in1 */. asse
1ead2 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 rt( pIn1->flags&
1ead3 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28 MEM_Int );. if(
1ead4 20 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 29 7b 0a pIn1->u.i>0 ){.
1ead5 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 pc = pOp->p
1ead6 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 2 - 1;. }. bre
1ead7 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1ead8 3a 20 49 66 4e 65 67 20 50 31 20 50 32 20 2a 20 : IfNeg P1 P2 *
1ead9 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 * *.**.** If the
1eada 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 value of regist
1eadb 65 72 20 50 31 20 69 73 20 6c 65 73 73 20 74 68 er P1 is less th
1eadc 61 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 20 74 6f an zero, jump to
1eadd 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 P2. .**.** It i
1eade 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 s illegal to use
1eadf 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f this instructio
1eae0 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 n on a register
1eae1 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 that does.** not
1eae2 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 contain an inte
1eae3 67 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 ger. An asserti
1eae4 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 on fault will re
1eae5 73 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e sult if you try.
1eae6 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 65 .*/.case OP_IfNe
1eae7 67 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a g: { /* j
1eae8 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 ump, in1 */. as
1eae9 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 sert( pIn1->flag
1eaea 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 s&MEM_Int );. i
1eaeb 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3c 30 20 29 f( pIn1->u.i<0 )
1eaec 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d {. pc = pOp-
1eaed 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p2 - 1;. }. b
1eaee 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1eaef 64 65 3a 20 49 66 5a 65 72 6f 20 50 31 20 50 32 de: IfZero P1 P2
1eaf0 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 * * *.**.** If
1eaf1 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 the value of reg
1eaf2 69 73 74 65 72 20 50 31 20 69 73 20 65 78 61 63 ister P1 is exac
1eaf3 74 6c 79 20 30 2c 20 6a 75 6d 70 20 74 6f 20 50 tly 0, jump to P
1eaf4 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 2. .**.** It is
1eaf5 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74 illegal to use t
1eaf6 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1eaf7 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68 on a register th
1eaf8 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 at does.** not c
1eaf9 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 ontain an intege
1eafa 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e r. An assertion
1eafb 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 fault will resu
1eafc 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a lt if you try..*
1eafd 2f 0a 63 61 73 65 20 4f 50 5f 49 66 5a 65 72 6f /.case OP_IfZero
1eafe 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 : { /* ju
1eaff 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 mp, in1 */. ass
1eb00 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 ert( pIn1->flags
1eb01 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 &MEM_Int );. if
1eb02 28 20 70 49 6e 31 2d 3e 75 2e 69 3d 3d 30 20 29 ( pIn1->u.i==0 )
1eb03 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d {. pc = pOp-
1eb04 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p2 - 1;. }. b
1eb05 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1eb06 64 65 3a 20 41 67 67 53 74 65 70 20 2a 20 50 32 de: AggStep * P2
1eb07 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 P3 P4 P5.**.**
1eb08 45 78 65 63 75 74 65 20 74 68 65 20 73 74 65 70 Execute the step
1eb09 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e function for an
1eb0a 20 61 67 67 72 65 67 61 74 65 2e 20 20 54 68 65 aggregate. The
1eb0b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 .** function has
1eb0c 20 50 35 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 P5 arguments.
1eb0d 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 P4 is a pointer
1eb0e 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65 66 0a to the FuncDef.
1eb0f 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61 ** structure tha
1eb10 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 t specifies the
1eb11 66 75 6e 63 74 69 6f 6e 2e 20 20 55 73 65 20 72 function. Use r
1eb12 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 20 61 73 egister.** P3 as
1eb13 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 the accumulator
1eb14 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 35 20 61 ..**.** The P5 a
1eb15 72 67 75 6d 65 6e 74 73 20 61 72 65 20 74 61 6b rguments are tak
1eb16 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 en from register
1eb17 20 50 32 20 61 6e 64 20 69 74 73 0a 2a 2a 20 73 P2 and its.** s
1eb18 75 63 63 65 73 73 6f 72 73 2e 0a 2a 2f 0a 63 61 uccessors..*/.ca
1eb19 73 65 20 4f 50 5f 41 67 67 53 74 65 70 3a 20 7b se OP_AggStep: {
1eb1a 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1eb1b 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1eb1c 20 69 6e 74 6f 20 75 2e 63 63 20 2a 2f 0a 20 20 into u.cc */.
1eb1d 69 6e 74 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a int n;. int i;.
1eb1e 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 4d Mem *pMem;. M
1eb1f 65 6d 20 2a 70 52 65 63 3b 0a 20 20 73 71 6c 69 em *pRec;. sqli
1eb20 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b te3_context ctx;
1eb21 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
1eb22 20 2a 2a 61 70 56 61 6c 3b 0a 23 65 6e 64 69 66 **apVal;.#endif
1eb23 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1eb24 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1eb25 2e 63 63 20 2a 2f 0a 0a 20 20 75 2e 63 63 2e 6e .cc */.. u.cc.n
1eb26 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 61 73 = pOp->p5;. as
1eb27 73 65 72 74 28 20 75 2e 63 63 2e 6e 3e 3d 30 20 sert( u.cc.n>=0
1eb28 29 3b 0a 20 20 75 2e 63 63 2e 70 52 65 63 20 3d );. u.cc.pRec =
1eb29 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1eb2a 32 5d 3b 0a 20 20 75 2e 63 63 2e 61 70 56 61 6c 2];. u.cc.apVal
1eb2b 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61 = p->apArg;. a
1eb2c 73 73 65 72 74 28 20 75 2e 63 63 2e 61 70 56 61 ssert( u.cc.apVa
1eb2d 6c 20 7c 7c 20 75 2e 63 63 2e 6e 3d 3d 30 20 29 l || u.cc.n==0 )
1eb2e 3b 0a 20 20 66 6f 72 28 75 2e 63 63 2e 69 3d 30 ;. for(u.cc.i=0
1eb2f 3b 20 75 2e 63 63 2e 69 3c 75 2e 63 63 2e 6e 3b ; u.cc.i<u.cc.n;
1eb30 20 75 2e 63 63 2e 69 2b 2b 2c 20 75 2e 63 63 2e u.cc.i++, u.cc.
1eb31 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 75 2e 63 pRec++){. u.c
1eb32 63 2e 61 70 56 61 6c 5b 75 2e 63 63 2e 69 5d 20 c.apVal[u.cc.i]
1eb33 3d 20 75 2e 63 63 2e 70 52 65 63 3b 0a 20 20 20 = u.cc.pRec;.
1eb34 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 75 storeTypeInfo(u
1eb35 2e 63 63 2e 70 52 65 63 2c 20 65 6e 63 6f 64 69 .cc.pRec, encodi
1eb36 6e 67 29 3b 0a 20 20 7d 0a 20 20 75 2e 63 63 2e ng);. }. u.cc.
1eb37 63 74 78 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d ctx.pFunc = pOp-
1eb38 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 61 73 73 >p4.pFunc;. ass
1eb39 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 ert( pOp->p3>0 &
1eb3a 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d & pOp->p3<=p->nM
1eb3b 65 6d 20 29 3b 0a 20 20 75 2e 63 63 2e 63 74 78 em );. u.cc.ctx
1eb3c 2e 70 4d 65 6d 20 3d 20 75 2e 63 63 2e 70 4d 65 .pMem = u.cc.pMe
1eb3d 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 m = &p->aMem[pOp
1eb3e 2d 3e 70 33 5d 3b 0a 20 20 75 2e 63 63 2e 70 4d ->p3];. u.cc.pM
1eb3f 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 75 2e 63 63 2e em->n++;. u.cc.
1eb40 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 ctx.s.flags = ME
1eb41 4d 5f 4e 75 6c 6c 3b 0a 20 20 75 2e 63 63 2e 63 M_Null;. u.cc.c
1eb42 74 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20 20 75 2e tx.s.z = 0;. u.
1eb43 63 63 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 cc.ctx.s.zMalloc
1eb44 20 3d 20 30 3b 0a 20 20 75 2e 63 63 2e 63 74 78 = 0;. u.cc.ctx
1eb45 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 75 .s.xDel = 0;. u
1eb46 2e 63 63 2e 63 74 78 2e 73 2e 64 62 20 3d 20 64 .cc.ctx.s.db = d
1eb47 62 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e 69 73 b;. u.cc.ctx.is
1eb48 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 75 2e 63 Error = 0;. u.c
1eb49 63 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 30 3b c.ctx.pColl = 0;
1eb4a 0a 20 20 69 66 28 20 75 2e 63 63 2e 63 74 78 2e . if( u.cc.ctx.
1eb4b 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53 pFunc->flags & S
1eb4c 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 QLITE_FUNC_NEEDC
1eb4d 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 OLL ){. asser
1eb4e 74 28 20 70 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b t( pOp>p->aOp );
1eb4f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 . assert( pOp
1eb50 5b 2d 31 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f [-1].p4type==P4_
1eb51 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 COLLSEQ );. a
1eb52 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f ssert( pOp[-1].o
1eb53 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 pcode==OP_CollSe
1eb54 71 20 29 3b 0a 20 20 20 20 75 2e 63 63 2e 63 74 q );. u.cc.ct
1eb55 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 x.pColl = pOp[-1
1eb56 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a ].p4.pColl;. }.
1eb57 20 20 28 75 2e 63 63 2e 63 74 78 2e 70 46 75 6e (u.cc.ctx.pFun
1eb58 63 2d 3e 78 53 74 65 70 29 28 26 75 2e 63 63 2e c->xStep)(&u.cc.
1eb59 63 74 78 2c 20 75 2e 63 63 2e 6e 2c 20 75 2e 63 ctx, u.cc.n, u.c
1eb5a 63 2e 61 70 56 61 6c 29 3b 0a 20 20 69 66 28 20 c.apVal);. if(
1eb5b 75 2e 63 63 2e 63 74 78 2e 69 73 45 72 72 6f 72 u.cc.ctx.isError
1eb5c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 ){. sqlite3S
1eb5d 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 etString(&p->zEr
1eb5e 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 rMsg, db, "%s",
1eb5f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1eb60 78 74 28 26 75 2e 63 63 2e 63 74 78 2e 73 29 29 xt(&u.cc.ctx.s))
1eb61 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 63 2e ;. rc = u.cc.
1eb62 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d ctx.isError;. }
1eb63 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1eb64 6d 52 65 6c 65 61 73 65 28 26 75 2e 63 63 2e 63 mRelease(&u.cc.c
1eb65 74 78 2e 73 29 3b 0a 20 20 62 72 65 61 6b 3b 0a tx.s);. break;.
1eb66 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 }../* Opcode: Ag
1eb67 67 46 69 6e 61 6c 20 50 31 20 50 32 20 2a 20 50 gFinal P1 P2 * P
1eb68 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 4 *.**.** Execut
1eb69 65 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 e the finalizer
1eb6a 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 function for an
1eb6b 61 67 67 72 65 67 61 74 65 2e 20 20 50 31 20 69 aggregate. P1 i
1eb6c 73 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 s.** the memory
1eb6d 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 location that is
1eb6e 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 the accumulator
1eb6f 20 66 6f 72 20 74 68 65 20 61 67 67 72 65 67 61 for the aggrega
1eb70 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 te..**.** P2 is
1eb71 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 the number of ar
1eb72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65 guments that the
1eb73 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74 step function t
1eb74 61 6b 65 73 20 61 6e 64 0a 2a 2a 20 50 34 20 69 akes and.** P4 i
1eb75 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
1eb76 68 65 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74 he FuncDef for t
1eb77 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 his function. T
1eb78 68 65 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e he P2.** argumen
1eb79 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79 t is not used by
1eb7a 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49 this opcode. I
1eb7b 74 20 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20 t is only there
1eb7c 74 6f 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a to disambiguate.
1eb7d 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 ** functions tha
1eb7e 74 20 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69 t can take varyi
1eb7f 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72 ng numbers of ar
1eb80 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a guments. The.**
1eb81 20 50 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 P4 argument is
1eb82 6f 6e 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20 only needed for
1eb83 74 68 65 20 64 65 67 65 6e 65 72 61 74 65 20 63 the degenerate c
1eb84 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65 ase where.** the
1eb85 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77 step function w
1eb86 61 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c as not previousl
1eb87 79 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73 y called..*/.cas
1eb88 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b e OP_AggFinal: {
1eb89 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1eb8a 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1eb8b 20 69 6e 74 6f 20 75 2e 63 64 20 2a 2f 0a 20 20 into u.cd */.
1eb8c 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 23 65 6e 64 69 Mem *pMem;.#endi
1eb8d 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1eb8e 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1eb8f 75 2e 63 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 u.cd */. assert
1eb90 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 ( pOp->p1>0 && p
1eb91 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p1<=p->nMem
1eb92 29 3b 0a 20 20 75 2e 63 64 2e 70 4d 65 6d 20 3d );. u.cd.pMem =
1eb93 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1eb94 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75 1];. assert( (u
1eb95 2e 63 64 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 .cd.pMem->flags
1eb96 26 20 7e 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d & ~(MEM_Null|MEM
1eb97 5f 41 67 67 29 29 3d 3d 30 20 29 3b 0a 20 20 72 _Agg))==0 );. r
1eb98 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d c = sqlite3VdbeM
1eb99 65 6d 46 69 6e 61 6c 69 7a 65 28 75 2e 63 64 2e emFinalize(u.cd.
1eb9a 70 4d 65 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70 46 pMem, pOp->p4.pF
1eb9b 75 6e 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29 unc);. if( rc )
1eb9c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 {. sqlite3Set
1eb9d 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d String(&p->zErrM
1eb9e 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 sg, db, "%s", sq
1eb9f 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1eba0 28 75 2e 63 64 2e 70 4d 65 6d 29 29 3b 0a 20 20 (u.cd.pMem));.
1eba1 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 }. sqlite3VdbeC
1eba2 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 75 2e hangeEncoding(u.
1eba3 63 64 2e 70 4d 65 6d 2c 20 65 6e 63 6f 64 69 6e cd.pMem, encodin
1eba4 67 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 g);. UPDATE_MAX
1eba5 5f 42 4c 4f 42 53 49 5a 45 28 75 2e 63 64 2e 70 _BLOBSIZE(u.cd.p
1eba6 4d 65 6d 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 Mem);. if( sqli
1eba7 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 te3VdbeMemTooBig
1eba8 28 75 2e 63 64 2e 70 4d 65 6d 29 20 29 7b 0a 20 (u.cd.pMem) ){.
1eba9 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b goto too_big;
1ebaa 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1ebab 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
1ebac 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 QLITE_OMIT_VACUU
1ebad 4d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 M) && !defined(S
1ebae 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 QLITE_OMIT_ATTAC
1ebaf 48 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 H)./* Opcode: Va
1ebb0 63 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a cuum * * * * *.*
1ebb1 2a 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65 20 *.** Vacuum the
1ebb2 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 2e entire database.
1ebb3 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 69 This opcode wi
1ebb4 6c 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20 76 ll cause other v
1ebb5 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e irtual.** machin
1ebb6 65 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 es to be created
1ebb7 20 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61 and run. It ma
1ebb8 79 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 y not be called
1ebb9 66 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61 from within.** a
1ebba 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f transaction..*/
1ebbb 0a 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a .case OP_Vacuum:
1ebbc 20 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 {. if( sqlite3
1ebbd 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 SafetyOff(db) )
1ebbe 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1ebbf 6f 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63 20 o_misuse; . rc
1ebc0 3d 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 = sqlite3RunVacu
1ebc1 75 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 um(&p->zErrMsg,
1ebc2 64 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 db);. if( sqlit
1ebc3 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 e3SafetyOn(db) )
1ebc4 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1ebc5 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72 65 to_misuse;. bre
1ebc6 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 ak;.}.#endif..#i
1ebc7 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
1ebc8 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
1ebc9 4d 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e M)./* Opcode: In
1ebca 63 72 56 61 63 75 75 6d 20 50 31 20 50 32 20 2a crVacuum P1 P2 *
1ebcb 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f * *.**.** Perfo
1ebcc 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 rm a single step
1ebcd 20 6f 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e of the incremen
1ebce 74 61 6c 20 76 61 63 75 75 6d 20 70 72 6f 63 65 tal vacuum proce
1ebcf 64 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50 dure on.** the P
1ebd0 31 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 1 database. If t
1ebd1 68 65 20 76 61 63 75 75 6d 20 68 61 73 20 66 69 he vacuum has fi
1ebd2 6e 69 73 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20 nished, jump to
1ebd3 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50 instruction.** P
1ebd4 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 2. Otherwise, fa
1ebd5 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 ll through to th
1ebd6 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 e next instructi
1ebd7 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 on..*/.case OP_I
1ebd8 6e 63 72 56 61 63 75 75 6d 3a 20 7b 20 20 20 20 ncrVacuum: {
1ebd9 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 /* jump */.#
1ebda 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1ebdb 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1ebdc 6e 74 6f 20 75 2e 63 65 20 2a 2f 0a 20 20 42 74 nto u.ce */. Bt
1ebdd 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66 ree *pBt;.#endif
1ebde 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1ebdf 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1ebe0 2e 63 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 .ce */.. assert
1ebe1 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1ebe2 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 pOp->p1<db->nDb
1ebe3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d );. assert( (p-
1ebe4 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c >btreeMask & (1<
1ebe5 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b <pOp->p1))!=0 );
1ebe6 0a 20 20 75 2e 63 65 2e 70 42 74 20 3d 20 64 62 . u.ce.pBt = db
1ebe7 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 ->aDb[pOp->p1].p
1ebe8 42 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 Bt;. rc = sqlit
1ebe9 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 e3BtreeIncrVacuu
1ebea 6d 28 75 2e 63 65 2e 70 42 74 29 3b 0a 20 20 69 m(u.ce.pBt);. i
1ebeb 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f f( rc==SQLITE_DO
1ebec 4e 45 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 NE ){. pc = p
1ebed 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 Op->p2 - 1;.
1ebee 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1ebef 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 }. break;.}.#
1ebf0 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65 endif../* Opcode
1ebf1 3a 20 45 78 70 69 72 65 20 50 31 20 2a 20 2a 20 : Expire P1 * *
1ebf2 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 61 75 73 65 20 * *.**.** Cause
1ebf3 70 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 precompiled stat
1ebf4 65 6d 65 6e 74 73 20 74 6f 20 62 65 63 6f 6d 65 ements to become
1ebf5 20 65 78 70 69 72 65 64 2e 20 41 6e 20 65 78 70 expired. An exp
1ebf6 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a ired statement.*
1ebf7 2a 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20 * fails with an
1ebf8 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 53 51 error code of SQ
1ebf9 4c 49 54 45 5f 53 43 48 45 4d 41 20 69 66 20 69 LITE_SCHEMA if i
1ebfa 74 20 69 73 20 65 76 65 72 20 65 78 65 63 75 74 t is ever execut
1ebfb 65 64 20 0a 2a 2a 20 28 76 69 61 20 73 71 6c 69 ed .** (via sqli
1ebfc 74 65 33 5f 73 74 65 70 28 29 29 2e 0a 2a 2a 20 te3_step())..**
1ebfd 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 30 2c 20 .** If P1 is 0,
1ebfe 74 68 65 6e 20 61 6c 6c 20 53 51 4c 20 73 74 61 then all SQL sta
1ebff 74 65 6d 65 6e 74 73 20 62 65 63 6f 6d 65 20 65 tements become e
1ec00 78 70 69 72 65 64 2e 20 49 66 20 50 31 20 69 73 xpired. If P1 is
1ec01 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 non-zero,.** th
1ec02 65 6e 20 6f 6e 6c 79 20 74 68 65 20 63 75 72 72 en only the curr
1ec03 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 ently executing
1ec04 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 66 66 statement is aff
1ec05 65 63 74 65 64 2e 20 0a 2a 2f 0a 63 61 73 65 20 ected. .*/.case
1ec06 4f 50 5f 45 78 70 69 72 65 3a 20 7b 0a 20 20 69 OP_Expire: {. i
1ec07 66 28 20 21 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 f( !pOp->p1 ){.
1ec08 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 sqlite3Expire
1ec09 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e PreparedStatemen
1ec0a 74 73 28 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b ts(db);. }else{
1ec0b 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 . p->expired
1ec0c 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b = 1;. }. break
1ec0d 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
1ec0e 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
1ec0f 43 41 43 48 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a CACHE./* Opcode:
1ec10 20 54 61 62 6c 65 4c 6f 63 6b 20 50 31 20 50 32 TableLock P1 P2
1ec11 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f P3 P4 *.**.** O
1ec12 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 btain a lock on
1ec13 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 a particular tab
1ec14 6c 65 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63 le. This instruc
1ec15 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 tion is only use
1ec16 64 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 73 68 d when.** the sh
1ec17 61 72 65 64 2d 63 61 63 68 65 20 66 65 61 74 75 ared-cache featu
1ec18 72 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 0a re is enabled. .
1ec19 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 **.** P1 is the
1ec1a 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 index of the dat
1ec1b 61 62 61 73 65 20 69 6e 20 73 71 6c 69 74 65 33 abase in sqlite3
1ec1c 2e 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 .aDb[] of the da
1ec1d 74 61 62 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69 tabase.** on whi
1ec1e 63 68 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 ch the lock is a
1ec1f 63 71 75 69 72 65 64 2e 20 20 41 20 72 65 61 64 cquired. A read
1ec20 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 lock is obtained
1ec21 20 69 66 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20 if P3==0 or.**
1ec22 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 a write lock if
1ec23 50 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 P3==1..**.** P2
1ec24 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 6f contains the roo
1ec25 74 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 t-page of the ta
1ec26 62 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a ble to lock..**.
1ec27 2a 2a 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 ** P4 contains a
1ec28 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
1ec29 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c name of the tabl
1ec2a 65 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20 e being locked.
1ec2b 54 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 This is only.**
1ec2c 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 used to generate
1ec2d 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
1ec2e 65 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 e if the lock ca
1ec2f 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 nnot be obtained
1ec30 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62 ..*/.case OP_Tab
1ec31 6c 65 4c 6f 63 6b 3a 20 7b 0a 20 20 75 38 20 69 leLock: {. u8 i
1ec32 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 75 38 sWriteLock = (u8
1ec33 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 66 28 20 )pOp->p3;. if(
1ec34 69 73 57 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 30 isWriteLock || 0
1ec35 3d 3d 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c ==(db->flags&SQL
1ec36 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 ITE_ReadUncommit
1ec37 74 65 64 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 ted) ){. int
1ec38 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20 p1 = pOp->p1; .
1ec39 20 20 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30 assert( p1>=0
1ec3a 20 26 26 20 70 31 3c 64 62 2d 3e 6e 44 62 20 29 && p1<db->nDb )
1ec3b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 ;. assert( (p
1ec3c 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 ->btreeMask & (1
1ec3d 3c 3c 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 20 <<p1))!=0 );.
1ec3e 20 61 73 73 65 72 74 28 20 69 73 57 72 69 74 65 assert( isWrite
1ec3f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 Lock==0 || isWri
1ec40 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 teLock==1 );.
1ec41 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1ec42 65 65 4c 6f 63 6b 54 61 62 6c 65 28 64 62 2d 3e eeLockTable(db->
1ec43 61 44 62 5b 70 31 5d 2e 70 42 74 2c 20 70 4f 70 aDb[p1].pBt, pOp
1ec44 2d 3e 70 32 2c 20 69 73 57 72 69 74 65 4c 6f 63 ->p2, isWriteLoc
1ec45 6b 29 3b 0a 20 20 20 20 69 66 28 20 28 72 63 26 k);. if( (rc&
1ec46 30 78 46 46 29 3d 3d 53 51 4c 49 54 45 5f 4c 4f 0xFF)==SQLITE_LO
1ec47 43 4b 45 44 20 29 7b 0a 20 20 20 20 20 20 63 6f CKED ){. co
1ec48 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f nst char *z = pO
1ec49 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 73 p->p4.z;. s
1ec4a 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1ec4b 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1ec4c 20 22 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 "database table
1ec4d 20 69 73 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c is locked: %s",
1ec4e 20 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 z);. }. }.
1ec4f 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1ec50 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1ec51 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a SHARED_CACHE */.
1ec52 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1ec53 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1ec54 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 42 65 E./* Opcode: VBe
1ec55 67 69 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a gin * * * P4 *.*
1ec56 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61 *.** P4 may be a
1ec57 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 pointer to an s
1ec58 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 qlite3_vtab stru
1ec59 63 74 75 72 65 2e 20 49 66 20 73 6f 2c 20 63 61 cture. If so, ca
1ec5a 6c 6c 20 74 68 65 20 0a 2a 2a 20 78 42 65 67 69 ll the .** xBegi
1ec5b 6e 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61 n method for tha
1ec5c 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 t table..**.** A
1ec5d 6c 73 6f 2c 20 77 68 65 74 68 65 72 20 6f 72 20 lso, whether or
1ec5e 6e 6f 74 20 50 34 20 69 73 20 73 65 74 2c 20 63 not P4 is set, c
1ec5f 68 65 63 6b 20 74 68 61 74 20 74 68 69 73 20 69 heck that this i
1ec60 73 20 6e 6f 74 20 62 65 69 6e 67 20 63 61 6c 6c s not being call
1ec61 65 64 20 66 72 6f 6d 0a 2a 2a 20 77 69 74 68 69 ed from.** withi
1ec62 6e 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 n a callback to
1ec63 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 a virtual table
1ec64 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 2e 20 xSync() method.
1ec65 49 66 20 69 74 20 69 73 2c 20 74 68 65 20 65 72 If it is, the er
1ec66 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c ror.** code will
1ec67 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 be set to SQLIT
1ec68 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 63 61 73 E_LOCKED..*/.cas
1ec69 65 20 4f 50 5f 56 42 65 67 69 6e 3a 20 7b 0a 23 e OP_VBegin: {.#
1ec6a 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1ec6b 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1ec6c 6e 74 6f 20 75 2e 63 66 20 2a 2f 0a 20 20 56 54 nto u.cf */. VT
1ec6d 61 62 6c 65 20 2a 70 56 54 61 62 3b 0a 23 65 6e able *pVTab;.#en
1ec6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1ec6f 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1ec70 6f 20 75 2e 63 66 20 2a 2f 0a 20 20 75 2e 63 66 o u.cf */. u.cf
1ec71 2e 70 56 54 61 62 20 3d 20 70 4f 70 2d 3e 70 34 .pVTab = pOp->p4
1ec72 2e 70 56 74 61 62 3b 0a 20 20 72 63 20 3d 20 73 .pVtab;. rc = s
1ec73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28 qlite3VtabBegin(
1ec74 64 62 2c 20 75 2e 63 66 2e 70 56 54 61 62 29 3b db, u.cf.pVTab);
1ec75 0a 20 20 69 66 28 20 75 2e 63 66 2e 70 56 54 61 . if( u.cf.pVTa
1ec76 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 b ){. sqlite3
1ec77 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 DbFree(db, p->zE
1ec78 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a rrMsg);. p->z
1ec79 45 72 72 4d 73 67 20 3d 20 75 2e 63 66 2e 70 56 ErrMsg = u.cf.pV
1ec7a 54 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72 Tab->pVtab->zErr
1ec7b 4d 73 67 3b 0a 20 20 20 20 75 2e 63 66 2e 70 56 Msg;. u.cf.pV
1ec7c 54 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72 Tab->pVtab->zErr
1ec7d 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 Msg = 0;. }. b
1ec7e 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f reak;.}.#endif /
1ec7f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 * SQLITE_OMIT_VI
1ec80 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 RTUALTABLE */..#
1ec81 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1ec82 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1ec83 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 72 65 61 /* Opcode: VCrea
1ec84 74 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a te P1 * * P4 *.*
1ec85 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e *.** P4 is the n
1ec86 61 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c ame of a virtual
1ec87 20 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 table in databa
1ec88 73 65 20 50 31 2e 20 43 61 6c 6c 20 74 68 65 20 se P1. Call the
1ec89 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a xCreate method.*
1ec8a 2a 20 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65 * for that table
1ec8b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 43 72 ..*/.case OP_VCr
1ec8c 65 61 74 65 3a 20 7b 0a 20 20 72 63 20 3d 20 73 eate: {. rc = s
1ec8d 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72 qlite3VtabCallCr
1ec8e 65 61 74 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 eate(db, pOp->p1
1ec8f 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 26 70 2d , pOp->p4.z, &p-
1ec90 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 62 72 65 >zErrMsg);. bre
1ec91 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1ec92 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1ec93 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 UALTABLE */..#if
1ec94 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1ec95 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a _VIRTUALTABLE./*
1ec96 20 4f 70 63 6f 64 65 3a 20 56 44 65 73 74 72 6f Opcode: VDestro
1ec97 79 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a y P1 * * P4 *.**
1ec98 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61 .** P4 is the na
1ec99 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 me of a virtual
1ec9a 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73 table in databas
1ec9b 65 20 50 31 2e 20 20 43 61 6c 6c 20 74 68 65 20 e P1. Call the
1ec9c 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a xDestroy method.
1ec9d 2a 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 ** of that table
1ec9e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 44 65 ..*/.case OP_VDe
1ec9f 73 74 72 6f 79 3a 20 7b 0a 20 20 70 2d 3e 69 6e stroy: {. p->in
1eca0 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 32 3b 0a VtabMethod = 2;.
1eca1 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 rc = sqlite3Vt
1eca2 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 64 62 abCallDestroy(db
1eca3 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e , pOp->p1, pOp->
1eca4 70 34 2e 7a 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 p4.z);. p->inVt
1eca5 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 abMethod = 0;.
1eca6 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
1eca7 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 /* SQLITE_OMIT_V
1eca8 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a IRTUALTABLE */..
1eca9 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ecaa 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1ecab 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4f 70 65 ./* Opcode: VOpe
1ecac 6e 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a n P1 * * P4 *.**
1ecad 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e .** P4 is a poin
1ecae 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c ter to a virtual
1ecaf 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 table object, a
1ecb0 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 n sqlite3_vtab s
1ecb1 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 50 31 20 tructure..** P1
1ecb2 69 73 20 61 20 63 75 72 73 6f 72 20 6e 75 6d 62 is a cursor numb
1ecb3 65 72 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 er. This opcode
1ecb4 20 6f 70 65 6e 73 20 61 20 63 75 72 73 6f 72 20 opens a cursor
1ecb5 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a to the virtual.*
1ecb6 2a 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 * table and stor
1ecb7 65 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 69 es that cursor i
1ecb8 6e 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 n P1..*/.case OP
1ecb9 5f 56 4f 70 65 6e 3a 20 7b 0a 23 69 66 20 30 20 _VOpen: {.#if 0
1ecba 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1ecbb 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1ecbc 2e 63 67 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 .cg */. VdbeCur
1ecbd 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 73 71 6c sor *pCur;. sql
1ecbe 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
1ecbf 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 *pVtabCursor;.
1ecc0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1ecc1 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f Vtab;. sqlite3_
1ecc2 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b module *pModule;
1ecc3 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1ecc4 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ecc5 20 69 6e 74 6f 20 75 2e 63 67 20 2a 2f 0a 0a 20 into u.cg */..
1ecc6 20 75 2e 63 67 2e 70 43 75 72 20 3d 20 30 3b 0a u.cg.pCur = 0;.
1ecc7 20 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 u.cg.pVtabCurs
1ecc8 6f 72 20 3d 20 30 3b 0a 20 20 75 2e 63 67 2e 70 or = 0;. u.cg.p
1ecc9 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 Vtab = pOp->p4.p
1ecca 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 75 Vtab->pVtab;. u
1eccb 2e 63 67 2e 70 4d 6f 64 75 6c 65 20 3d 20 28 73 .cg.pModule = (s
1eccc 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 qlite3_module *)
1eccd 75 2e 63 67 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 u.cg.pVtab->pMod
1ecce 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 75 2e ule;. assert(u.
1eccf 63 67 2e 70 56 74 61 62 20 26 26 20 75 2e 63 67 cg.pVtab && u.cg
1ecd0 2e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28 .pModule);. if(
1ecd1 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1ecd2 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1ecd3 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1ecd4 3b 0a 20 20 72 63 20 3d 20 75 2e 63 67 2e 70 4d ;. rc = u.cg.pM
1ecd5 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 75 2e 63 odule->xOpen(u.c
1ecd6 67 2e 70 56 74 61 62 2c 20 26 75 2e 63 67 2e 70 g.pVtab, &u.cg.p
1ecd7 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 73 VtabCursor);. s
1ecd8 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1ecd9 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
1ecda 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 p->zErrMsg = u.c
1ecdb 67 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 g.pVtab->zErrMsg
1ecdc 3b 0a 20 20 75 2e 63 67 2e 70 56 74 61 62 2d 3e ;. u.cg.pVtab->
1ecdd 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 zErrMsg = 0;. i
1ecde 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1ecdf 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 On(db) ) goto ab
1ece0 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1ece1 65 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f e;. if( SQLITE_
1ece2 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 2f 2a OK==rc ){. /*
1ece3 20 49 6e 69 74 69 61 6c 69 7a 65 20 73 71 6c 69 Initialize sqli
1ece4 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 te3_vtab_cursor
1ece5 62 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a 20 20 base class */.
1ece6 20 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 u.cg.pVtabCurs
1ece7 6f 72 2d 3e 70 56 74 61 62 20 3d 20 75 2e 63 67 or->pVtab = u.cg
1ece8 2e 70 56 74 61 62 3b 0a 0a 20 20 20 20 2f 2a 20 .pVtab;.. /*
1ece9 49 6e 69 74 69 61 6c 69 73 65 20 76 64 62 65 20 Initialise vdbe
1ecea 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f cursor object */
1eceb 0a 20 20 20 20 75 2e 63 67 2e 70 43 75 72 20 3d . u.cg.pCur =
1ecec 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 allocateCursor(
1eced 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 2d p, pOp->p1, 0, -
1ecee 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 75 1, 0);. if( u
1ecef 2e 63 67 2e 70 43 75 72 20 29 7b 0a 20 20 20 20 .cg.pCur ){.
1ecf0 20 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70 56 74 u.cg.pCur->pVt
1ecf1 61 62 43 75 72 73 6f 72 20 3d 20 75 2e 63 67 2e abCursor = u.cg.
1ecf2 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 pVtabCursor;.
1ecf3 20 20 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70 4d u.cg.pCur->pM
1ecf4 6f 64 75 6c 65 20 3d 20 75 2e 63 67 2e 70 56 74 odule = u.cg.pVt
1ecf5 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d abCursor->pVtab-
1ecf6 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 65 >pModule;. }e
1ecf7 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d lse{. db->m
1ecf8 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
1ecf9 0a 20 20 20 20 20 20 75 2e 63 67 2e 70 4d 6f 64 . u.cg.pMod
1ecfa 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 75 2e 63 67 ule->xClose(u.cg
1ecfb 2e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 .pVtabCursor);.
1ecfc 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b }. }. break
1ecfd 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
1ecfe 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1ecff 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 LTABLE */..#ifnd
1ed00 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1ed01 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f IRTUALTABLE./* O
1ed02 70 63 6f 64 65 3a 20 56 46 69 6c 74 65 72 20 50 pcode: VFilter P
1ed03 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 1 P2 P3 P4 *.**.
1ed04 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72 73 6f ** P1 is a curso
1ed05 72 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 56 r opened using V
1ed06 4f 70 65 6e 2e 20 20 50 32 20 69 73 20 61 6e 20 Open. P2 is an
1ed07 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d 70 20 address to jump
1ed08 74 6f 20 69 66 0a 2a 2a 20 74 68 65 20 66 69 6c to if.** the fil
1ed09 74 65 72 65 64 20 72 65 73 75 6c 74 20 73 65 74 tered result set
1ed0a 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a is empty..**.**
1ed0b 20 50 34 20 69 73 20 65 69 74 68 65 72 20 4e 55 P4 is either NU
1ed0c 4c 4c 20 6f 72 20 61 20 73 74 72 69 6e 67 20 74 LL or a string t
1ed0d 68 61 74 20 77 61 73 20 67 65 6e 65 72 61 74 65 hat was generate
1ed0e 64 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e d by the xBestIn
1ed0f 64 65 78 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 dex.** method of
1ed10 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 the module. Th
1ed11 65 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e e interpretation
1ed12 20 6f 66 20 74 68 65 20 50 34 20 73 74 72 69 6e of the P4 strin
1ed13 67 20 69 73 20 6c 65 66 74 0a 2a 2a 20 74 6f 20 g is left.** to
1ed14 74 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 the module imple
1ed15 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a mentation..**.**
1ed16 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 This opcode inv
1ed17 6f 6b 65 73 20 74 68 65 20 78 46 69 6c 74 65 72 okes the xFilter
1ed18 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65 20 76 method on the v
1ed19 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 70 65 irtual table spe
1ed1a 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 50 31 2e cified.** by P1.
1ed1b 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 71 75 The integer qu
1ed1c 65 72 79 20 70 6c 61 6e 20 70 61 72 61 6d 65 74 ery plan paramet
1ed1d 65 72 20 74 6f 20 78 46 69 6c 74 65 72 20 69 73 er to xFilter is
1ed1e 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1ed1f 74 65 72 0a 2a 2a 20 50 33 2e 20 52 65 67 69 73 ter.** P3. Regis
1ed20 74 65 72 20 50 33 2b 31 20 73 74 6f 72 65 73 20 ter P3+1 stores
1ed21 74 68 65 20 61 72 67 63 20 70 61 72 61 6d 65 74 the argc paramet
1ed22 65 72 20 74 6f 20 62 65 20 70 61 73 73 65 64 20 er to be passed
1ed23 74 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c 74 65 to the.** xFilte
1ed24 72 20 6d 65 74 68 6f 64 2e 20 52 65 67 69 73 74 r method. Regist
1ed25 65 72 73 20 50 33 2b 32 2e 2e 50 33 2b 31 2b 61 ers P3+2..P3+1+a
1ed26 72 67 63 20 61 72 65 20 74 68 65 20 61 72 67 63 rgc are the argc
1ed27 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 70 .** additional p
1ed28 61 72 61 6d 65 74 65 72 73 20 77 68 69 63 68 20 arameters which
1ed29 61 72 65 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a are passed to.**
1ed2a 20 78 46 69 6c 74 65 72 20 61 73 20 61 72 67 76 xFilter as argv
1ed2b 2e 20 52 65 67 69 73 74 65 72 20 50 33 2b 32 20 . Register P3+2
1ed2c 62 65 63 6f 6d 65 73 20 61 72 67 76 5b 30 5d 20 becomes argv[0]
1ed2d 77 68 65 6e 20 70 61 73 73 65 64 20 74 6f 20 78 when passed to x
1ed2e 46 69 6c 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 Filter..**.** A
1ed2f 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20 jump is made to
1ed30 50 32 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 P2 if the result
1ed31 20 73 65 74 20 61 66 74 65 72 20 66 69 6c 74 65 set after filte
1ed32 72 69 6e 67 20 77 6f 75 6c 64 20 62 65 20 65 6d ring would be em
1ed33 70 74 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f pty..*/.case OP_
1ed34 56 46 69 6c 74 65 72 3a 20 7b 20 20 20 2f 2a 20 VFilter: { /*
1ed35 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f jump */.#if 0 /
1ed36 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1ed37 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 s moved into u.c
1ed38 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 3b h */. int nArg;
1ed39 0a 20 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 . int iQuery;.
1ed3a 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
1ed3b 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a odule *pModule;.
1ed3c 20 20 4d 65 6d 20 2a 70 51 75 65 72 79 3b 0a 20 Mem *pQuery;.
1ed3d 20 4d 65 6d 20 2a 70 41 72 67 63 3b 0a 20 20 73 Mem *pArgc;. s
1ed3e 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
1ed3f 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b or *pVtabCursor;
1ed40 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 . sqlite3_vtab
1ed41 2a 70 56 74 61 62 3b 0a 20 20 56 64 62 65 43 75 *pVtab;. VdbeCu
1ed42 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e rsor *pCur;. in
1ed43 74 20 72 65 73 3b 0a 20 20 69 6e 74 20 69 3b 0a t res;. int i;.
1ed44 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 23 Mem **apArg;.#
1ed45 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1ed46 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1ed47 6e 74 6f 20 75 2e 63 68 20 2a 2f 0a 0a 20 20 75 nto u.ch */.. u
1ed48 2e 63 68 2e 70 51 75 65 72 79 20 3d 20 26 70 2d .ch.pQuery = &p-
1ed49 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a >aMem[pOp->p3];.
1ed4a 20 20 75 2e 63 68 2e 70 41 72 67 63 20 3d 20 26 u.ch.pArgc = &
1ed4b 75 2e 63 68 2e 70 51 75 65 72 79 5b 31 5d 3b 0a u.ch.pQuery[1];.
1ed4c 20 20 75 2e 63 68 2e 70 43 75 72 20 3d 20 70 2d u.ch.pCur = p-
1ed4d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1ed4e 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 . REGISTER_TRAC
1ed4f 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 63 68 2e E(pOp->p3, u.ch.
1ed50 70 51 75 65 72 79 29 3b 0a 20 20 61 73 73 65 72 pQuery);. asser
1ed51 74 28 20 75 2e 63 68 2e 70 43 75 72 2d 3e 70 56 t( u.ch.pCur->pV
1ed52 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 tabCursor );. u
1ed53 2e 63 68 2e 70 56 74 61 62 43 75 72 73 6f 72 20 .ch.pVtabCursor
1ed54 3d 20 75 2e 63 68 2e 70 43 75 72 2d 3e 70 56 74 = u.ch.pCur->pVt
1ed55 61 62 43 75 72 73 6f 72 3b 0a 20 20 75 2e 63 68 abCursor;. u.ch
1ed56 2e 70 56 74 61 62 20 3d 20 75 2e 63 68 2e 70 56 .pVtab = u.ch.pV
1ed57 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 tabCursor->pVtab
1ed58 3b 0a 20 20 75 2e 63 68 2e 70 4d 6f 64 75 6c 65 ;. u.ch.pModule
1ed59 20 3d 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 70 = u.ch.pVtab->p
1ed5a 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20 47 72 Module;.. /* Gr
1ed5b 61 62 20 74 68 65 20 69 6e 64 65 78 20 6e 75 6d ab the index num
1ed5c 62 65 72 20 61 6e 64 20 61 72 67 63 20 70 61 72 ber and argc par
1ed5d 61 6d 65 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 ameters */. ass
1ed5e 65 72 74 28 20 28 75 2e 63 68 2e 70 51 75 65 72 ert( (u.ch.pQuer
1ed5f 79 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 y->flags&MEM_Int
1ed60 29 21 3d 30 20 26 26 20 75 2e 63 68 2e 70 41 72 )!=0 && u.ch.pAr
1ed61 67 63 2d 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49 gc->flags==MEM_I
1ed62 6e 74 20 29 3b 0a 20 20 75 2e 63 68 2e 6e 41 72 nt );. u.ch.nAr
1ed63 67 20 3d 20 28 69 6e 74 29 75 2e 63 68 2e 70 41 g = (int)u.ch.pA
1ed64 72 67 63 2d 3e 75 2e 69 3b 0a 20 20 75 2e 63 68 rgc->u.i;. u.ch
1ed65 2e 69 51 75 65 72 79 20 3d 20 28 69 6e 74 29 75 .iQuery = (int)u
1ed66 2e 63 68 2e 70 51 75 65 72 79 2d 3e 75 2e 69 3b .ch.pQuery->u.i;
1ed67 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 .. /* Invoke th
1ed68 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 e xFilter method
1ed69 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 2e 63 68 */. {. u.ch
1ed6a 2e 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e .res = 0;. u.
1ed6b 63 68 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 ch.apArg = p->ap
1ed6c 41 72 67 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63 Arg;. for(u.c
1ed6d 68 2e 69 20 3d 20 30 3b 20 75 2e 63 68 2e 69 3c h.i = 0; u.ch.i<
1ed6e 75 2e 63 68 2e 6e 41 72 67 3b 20 75 2e 63 68 2e u.ch.nArg; u.ch.
1ed6f 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 2e 63 68 i++){. u.ch
1ed70 2e 61 70 41 72 67 5b 75 2e 63 68 2e 69 5d 20 3d .apArg[u.ch.i] =
1ed71 20 26 75 2e 63 68 2e 70 41 72 67 63 5b 75 2e 63 &u.ch.pArgc[u.c
1ed72 68 2e 69 2b 31 5d 3b 0a 20 20 20 20 20 20 73 74 h.i+1];. st
1ed73 6f 72 65 54 79 70 65 49 6e 66 6f 28 75 2e 63 68 oreTypeInfo(u.ch
1ed74 2e 61 70 41 72 67 5b 75 2e 63 68 2e 69 5d 2c 20 .apArg[u.ch.i],
1ed75 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 0);. }.. i
1ed76 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1ed77 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 Off(db) ) goto a
1ed78 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1ed79 73 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 se;. p->inVta
1ed7a 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20 bMethod = 1;.
1ed7b 20 72 63 20 3d 20 75 2e 63 68 2e 70 4d 6f 64 75 rc = u.ch.pModu
1ed7c 6c 65 2d 3e 78 46 69 6c 74 65 72 28 75 2e 63 68 le->xFilter(u.ch
1ed7d 2e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 75 2e .pVtabCursor, u.
1ed7e 63 68 2e 69 51 75 65 72 79 2c 20 70 4f 70 2d 3e ch.iQuery, pOp->
1ed7f 70 34 2e 7a 2c 20 75 2e 63 68 2e 6e 41 72 67 2c p4.z, u.ch.nArg,
1ed80 20 75 2e 63 68 2e 61 70 41 72 67 29 3b 0a 20 20 u.ch.apArg);.
1ed81 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f p->inVtabMetho
1ed82 64 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 d = 0;. sqlit
1ed83 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1ed84 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d zErrMsg);. p-
1ed85 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 68 2e >zErrMsg = u.ch.
1ed86 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a pVtab->zErrMsg;.
1ed87 20 20 20 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e u.ch.pVtab->
1ed88 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 zErrMsg = 0;.
1ed89 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1ed8a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 2e 63 68 OK ){. u.ch
1ed8b 2e 72 65 73 20 3d 20 75 2e 63 68 2e 70 4d 6f 64 .res = u.ch.pMod
1ed8c 75 6c 65 2d 3e 78 45 6f 66 28 75 2e 63 68 2e 70 ule->xEof(u.ch.p
1ed8d 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 VtabCursor);.
1ed8e 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 }. if( sqlit
1ed8f 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 e3SafetyOn(db) )
1ed90 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1ed91 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 20 20 to_misuse;..
1ed92 69 66 28 20 75 2e 63 68 2e 72 65 73 20 29 7b 0a if( u.ch.res ){.
1ed93 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e pc = pOp->
1ed94 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 p2 - 1;. }.
1ed95 7d 0a 20 20 75 2e 63 68 2e 70 43 75 72 2d 3e 6e }. u.ch.pCur->n
1ed96 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 0a 20 20 62 ullRow = 0;.. b
1ed97 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f reak;.}.#endif /
1ed98 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 * SQLITE_OMIT_VI
1ed99 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 RTUALTABLE */..#
1ed9a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1ed9b 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1ed9c 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 6f 6c 75 /* Opcode: VColu
1ed9d 6d 6e 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a mn P1 P2 P3 * *.
1ed9e 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 **.** Store the
1ed9f 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 32 2d value of the P2-
1eda0 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 th column of.**
1eda1 74 68 65 20 72 6f 77 20 6f 66 20 74 68 65 20 76 the row of the v
1eda2 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 74 68 61 irtual-table tha
1eda3 74 20 74 68 65 20 0a 2a 2a 20 50 31 20 63 75 72 t the .** P1 cur
1eda4 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 sor is pointing
1eda5 74 6f 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 to into register
1eda6 20 50 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f P3..*/.case OP_
1eda7 56 43 6f 6c 75 6d 6e 3a 20 7b 0a 23 69 66 20 30 VColumn: {.#if 0
1eda8 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1eda9 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1edaa 75 2e 63 69 20 2a 2f 0a 20 20 73 71 6c 69 74 65 u.ci */. sqlite
1edab 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 3_vtab *pVtab;.
1edac 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
1edad 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a odule *pModule;.
1edae 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20 20 Mem *pDest;.
1edaf 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1edb0 73 43 6f 6e 74 65 78 74 3b 0a 23 65 6e 64 69 66 sContext;.#endif
1edb1 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1edb2 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1edb3 2e 63 69 20 2a 2f 0a 0a 20 20 56 64 62 65 43 75 .ci */.. VdbeCu
1edb4 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e rsor *pCur = p->
1edb5 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a apCsr[pOp->p1];.
1edb6 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
1edb7 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 pVtabCursor );.
1edb8 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 assert( pOp->p3
1edb9 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 >0 && pOp->p3<=p
1edba 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 63 69 ->nMem );. u.ci
1edbb 2e 70 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65 .pDest = &p->aMe
1edbc 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 m[pOp->p3];. if
1edbd 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 ( pCur->nullRow
1edbe 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1edbf 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 75 2e 63 beMemSetNull(u.c
1edc0 69 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 62 72 i.pDest);. br
1edc1 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63 69 2e eak;. }. u.ci.
1edc2 70 56 74 61 62 20 3d 20 70 43 75 72 2d 3e 70 56 pVtab = pCur->pV
1edc3 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 tabCursor->pVtab
1edc4 3b 0a 20 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 ;. u.ci.pModule
1edc5 20 3d 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e 70 = u.ci.pVtab->p
1edc6 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 Module;. assert
1edc7 28 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e ( u.ci.pModule->
1edc8 78 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 6d 65 6d xColumn );. mem
1edc9 73 65 74 28 26 75 2e 63 69 2e 73 43 6f 6e 74 65 set(&u.ci.sConte
1edca 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 2e xt, 0, sizeof(u.
1edcb 63 69 2e 73 43 6f 6e 74 65 78 74 29 29 3b 0a 0a ci.sContext));..
1edcc 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20 /* The output
1edcd 63 65 6c 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 cell may already
1edce 20 68 61 76 65 20 61 20 62 75 66 66 65 72 20 61 have a buffer a
1edcf 6c 6c 6f 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 llocated. Move.
1edd0 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 ** the current
1edd1 63 6f 6e 74 65 6e 74 73 20 74 6f 20 75 2e 63 69 contents to u.ci
1edd2 2e 73 43 6f 6e 74 65 78 74 2e 73 20 73 6f 20 69 .sContext.s so i
1edd3 6e 20 63 61 73 65 20 74 68 65 20 75 73 65 72 2d n case the user-
1edd4 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 61 function. ** ca
1edd5 6e 20 75 73 65 20 74 68 65 20 61 6c 72 65 61 64 n use the alread
1edd6 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 y allocated buff
1edd7 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c er instead of al
1edd8 6c 6f 63 61 74 69 6e 67 20 61 0a 20 20 2a 2a 20 locating a. **
1edd9 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 new one.. */.
1edda 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f sqlite3VdbeMemMo
1eddb 76 65 28 26 75 2e 63 69 2e 73 43 6f 6e 74 65 78 ve(&u.ci.sContex
1eddc 74 2e 73 2c 20 75 2e 63 69 2e 70 44 65 73 74 29 t.s, u.ci.pDest)
1eddd 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c ;. MemSetTypeFl
1edde 61 67 28 26 75 2e 63 69 2e 73 43 6f 6e 74 65 78 ag(&u.ci.sContex
1eddf 74 2e 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a t.s, MEM_Null);.
1ede0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1ede1 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1ede2 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1ede3 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75 misuse;. rc = u
1ede4 2e 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f .ci.pModule->xCo
1ede5 6c 75 6d 6e 28 70 43 75 72 2d 3e 70 56 74 61 62 lumn(pCur->pVtab
1ede6 43 75 72 73 6f 72 2c 20 26 75 2e 63 69 2e 73 43 Cursor, &u.ci.sC
1ede7 6f 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32 29 ontext, pOp->p2)
1ede8 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
1ede9 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 e(db, p->zErrMsg
1edea 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 );. p->zErrMsg
1edeb 3d 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e 7a 45 = u.ci.pVtab->zE
1edec 72 72 4d 73 67 3b 0a 20 20 75 2e 63 69 2e 70 56 rrMsg;. u.ci.pV
1eded 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 tab->zErrMsg = 0
1edee 3b 0a 20 20 69 66 28 20 75 2e 63 69 2e 73 43 6f ;. if( u.ci.sCo
1edef 6e 74 65 78 74 2e 69 73 45 72 72 6f 72 20 29 7b ntext.isError ){
1edf0 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 69 2e 73 . rc = u.ci.s
1edf1 43 6f 6e 74 65 78 74 2e 69 73 45 72 72 6f 72 3b Context.isError;
1edf2 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 . }.. /* Copy
1edf3 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 the result of th
1edf4 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 e function to th
1edf5 65 20 50 33 20 72 65 67 69 73 74 65 72 2e 20 57 e P3 register. W
1edf6 65 0a 20 20 2a 2a 20 64 6f 20 74 68 69 73 20 72 e. ** do this r
1edf7 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 egardless of whe
1edf8 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 ther or not an e
1edf9 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 74 6f rror occurred to
1edfa 20 65 6e 73 75 72 65 20 61 6e 79 0a 20 20 2a 2a ensure any. **
1edfb 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74 dynamic allocat
1edfc 69 6f 6e 20 69 6e 20 75 2e 63 69 2e 73 43 6f 6e ion in u.ci.sCon
1edfd 74 65 78 74 2e 73 20 28 61 20 4d 65 6d 20 73 74 text.s (a Mem st
1edfe 72 75 63 74 29 20 69 73 20 20 72 65 6c 65 61 73 ruct) is releas
1edff 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 ed.. */. sqlit
1ee00 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
1ee01 64 69 6e 67 28 26 75 2e 63 69 2e 73 43 6f 6e 74 ding(&u.ci.sCont
1ee02 65 78 74 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 ext.s, encoding)
1ee03 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 ;. REGISTER_TRA
1ee04 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 63 69 CE(pOp->p3, u.ci
1ee05 2e 70 44 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 .pDest);. sqlit
1ee06 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 75 2e e3VdbeMemMove(u.
1ee07 63 69 2e 70 44 65 73 74 2c 20 26 75 2e 63 69 2e ci.pDest, &u.ci.
1ee08 73 43 6f 6e 74 65 78 74 2e 73 29 3b 0a 20 20 55 sContext.s);. U
1ee09 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 PDATE_MAX_BLOBSI
1ee0a 5a 45 28 75 2e 63 69 2e 70 44 65 73 74 29 3b 0a ZE(u.ci.pDest);.
1ee0b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1ee0c 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 fetyOn(db) ){.
1ee0d 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 goto abort_due
1ee0e 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a _to_misuse;. }.
1ee0f 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
1ee10 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 63 69 2e eMemTooBig(u.ci.
1ee11 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 67 6f pDest) ){. go
1ee12 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a to too_big;. }.
1ee13 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 break;.}.#endi
1ee14 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1ee15 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f _VIRTUALTABLE */
1ee16 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1ee17 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1ee18 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4e LE./* Opcode: VN
1ee19 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a ext P1 P2 * * *.
1ee1a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76 69 **.** Advance vi
1ee1b 72 74 75 61 6c 20 74 61 62 6c 65 20 50 31 20 74 rtual table P1 t
1ee1c 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69 o the next row i
1ee1d 6e 20 69 74 73 20 72 65 73 75 6c 74 20 73 65 74 n its result set
1ee1e 20 61 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 and.** jump to
1ee1f 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 instruction P2.
1ee20 20 4f 72 2c 20 69 66 20 74 68 65 20 76 69 72 74 Or, if the virt
1ee21 75 61 6c 20 74 61 62 6c 65 20 68 61 73 20 72 65 ual table has re
1ee22 61 63 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e 64 ached.** the end
1ee23 20 6f 66 20 69 74 73 20 72 65 73 75 6c 74 20 73 of its result s
1ee24 65 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 et, then fall th
1ee25 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 rough to the nex
1ee26 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a t instruction..*
1ee27 2f 0a 63 61 73 65 20 4f 50 5f 56 4e 65 78 74 3a /.case OP_VNext:
1ee28 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a { /* jump */.
1ee29 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1ee2a 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1ee2b 69 6e 74 6f 20 75 2e 63 6a 20 2a 2f 0a 20 20 73 into u.cj */. s
1ee2c 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 qlite3_vtab *pVt
1ee2d 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 ab;. const sqli
1ee2e 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 te3_module *pMod
1ee2f 75 6c 65 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a ule;. int res;.
1ee30 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1ee31 75 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f ur;.#endif /* lo
1ee32 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1ee33 76 65 64 20 69 6e 74 6f 20 75 2e 63 6a 20 2a 2f ved into u.cj */
1ee34 0a 0a 20 20 75 2e 63 6a 2e 72 65 73 20 3d 20 30 .. u.cj.res = 0
1ee35 3b 0a 20 20 75 2e 63 6a 2e 70 43 75 72 20 3d 20 ;. u.cj.pCur =
1ee36 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 p->apCsr[pOp->p1
1ee37 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 ];. assert( u.c
1ee38 6a 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 j.pCur->pVtabCur
1ee39 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 75 2e 63 sor );. if( u.c
1ee3a 6a 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 j.pCur->nullRow
1ee3b 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 ){. break;.
1ee3c 7d 0a 20 20 75 2e 63 6a 2e 70 56 74 61 62 20 3d }. u.cj.pVtab =
1ee3d 20 75 2e 63 6a 2e 70 43 75 72 2d 3e 70 56 74 61 u.cj.pCur->pVta
1ee3e 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a bCursor->pVtab;.
1ee3f 20 20 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65 20 3d u.cj.pModule =
1ee40 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e 70 4d 6f u.cj.pVtab->pMo
1ee41 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 dule;. assert(
1ee42 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4e u.cj.pModule->xN
1ee43 65 78 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 ext );.. /* Inv
1ee44 6f 6b 65 20 74 68 65 20 78 4e 65 78 74 28 29 20 oke the xNext()
1ee45 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f method of the mo
1ee46 64 75 6c 65 2e 20 54 68 65 72 65 20 69 73 20 6e dule. There is n
1ee47 6f 20 77 61 79 20 66 6f 72 20 74 68 65 0a 20 20 o way for the.
1ee48 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 69 6d ** underlying im
1ee49 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f 20 plementation to
1ee4a 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 return an error
1ee4b 69 66 20 6f 6e 65 20 6f 63 63 75 72 73 20 64 75 if one occurs du
1ee4c 72 69 6e 67 0a 20 20 2a 2a 20 78 4e 65 78 74 28 ring. ** xNext(
1ee4d 29 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 20 61 ). Instead, if a
1ee4e 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
1ee4f 74 72 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 true is returned
1ee50 20 28 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 (indicating tha
1ee51 74 0a 20 20 2a 2a 20 64 61 74 61 20 69 73 20 61 t. ** data is a
1ee52 76 61 69 6c 61 62 6c 65 29 20 61 6e 64 20 74 68 vailable) and th
1ee53 65 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65 74 e error code ret
1ee54 75 72 6e 65 64 20 77 68 65 6e 20 78 43 6f 6c 75 urned when xColu
1ee55 6d 6e 20 6f 72 0a 20 20 2a 2a 20 73 6f 6d 65 20 mn or. ** some
1ee56 6f 74 68 65 72 20 6d 65 74 68 6f 64 20 69 73 20 other method is
1ee57 6e 65 78 74 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 next invoked on
1ee58 74 68 65 20 73 61 76 65 20 76 69 72 74 75 61 6c the save virtual
1ee59 20 74 61 62 6c 65 20 63 75 72 73 6f 72 2e 0a 20 table cursor..
1ee5a 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 */. if( sqlite
1ee5b 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 3SafetyOff(db) )
1ee5c 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1ee5d 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 70 2d 3e to_misuse;. p->
1ee5e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 inVtabMethod = 1
1ee5f 3b 0a 20 20 72 63 20 3d 20 75 2e 63 6a 2e 70 4d ;. rc = u.cj.pM
1ee60 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 75 2e 63 odule->xNext(u.c
1ee61 6a 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 j.pCur->pVtabCur
1ee62 73 6f 72 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 61 sor);. p->inVta
1ee63 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 73 bMethod = 0;. s
1ee64 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1ee65 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
1ee66 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 p->zErrMsg = u.c
1ee67 6a 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 j.pVtab->zErrMsg
1ee68 3b 0a 20 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e ;. u.cj.pVtab->
1ee69 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 zErrMsg = 0;. i
1ee6a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1ee6b 20 29 7b 0a 20 20 20 20 75 2e 63 6a 2e 72 65 73 ){. u.cj.res
1ee6c 20 3d 20 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65 2d = u.cj.pModule-
1ee6d 3e 78 45 6f 66 28 75 2e 63 6a 2e 70 43 75 72 2d >xEof(u.cj.pCur-
1ee6e 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 >pVtabCursor);.
1ee6f 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
1ee70 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 SafetyOn(db) ) g
1ee71 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1ee72 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 69 66 28 20 _misuse;.. if(
1ee73 21 75 2e 63 6a 2e 72 65 73 20 29 7b 0a 20 20 20 !u.cj.res ){.
1ee74 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 /* If there is
1ee75 64 61 74 61 2c 20 6a 75 6d 70 20 74 6f 20 50 32 data, jump to P2
1ee76 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 */. pc = pOp
1ee77 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 ->p2 - 1;. }.
1ee78 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
1ee79 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 /* SQLITE_OMIT_V
1ee7a 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a IRTUALTABLE */..
1ee7b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ee7c 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1ee7d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52 65 6e ./* Opcode: VRen
1ee7e 61 6d 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a ame P1 * * P4 *.
1ee7f 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f **.** P4 is a po
1ee80 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 inter to a virtu
1ee81 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c al table object,
1ee82 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 an sqlite3_vtab
1ee83 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 structure..** T
1ee84 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b his opcode invok
1ee85 65 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e es the correspon
1ee86 64 69 6e 67 20 78 52 65 6e 61 6d 65 20 6d 65 74 ding xRename met
1ee87 68 6f 64 2e 20 54 68 65 20 76 61 6c 75 65 0a 2a hod. The value.*
1ee88 2a 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 * in register P1
1ee89 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 is passed as th
1ee8a 65 20 7a 4e 61 6d 65 20 61 72 67 75 6d 65 6e 74 e zName argument
1ee8b 20 74 6f 20 74 68 65 20 78 52 65 6e 61 6d 65 20 to the xRename
1ee8c 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 63 61 73 65 20 method..*/.case
1ee8d 4f 50 5f 56 52 65 6e 61 6d 65 3a 20 7b 0a 23 69 OP_VRename: {.#i
1ee8e 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1ee8f 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1ee90 74 6f 20 75 2e 63 6b 20 2a 2f 0a 20 20 73 71 6c to u.ck */. sql
1ee91 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
1ee92 3b 0a 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b 0a ;. Mem *pName;.
1ee93 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1ee94 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1ee95 69 6e 74 6f 20 75 2e 63 6b 20 2a 2f 0a 0a 20 20 into u.ck */..
1ee96 75 2e 63 6b 2e 70 56 74 61 62 20 3d 20 70 4f 70 u.ck.pVtab = pOp
1ee97 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 ->p4.pVtab->pVta
1ee98 62 3b 0a 20 20 75 2e 63 6b 2e 70 4e 61 6d 65 20 b;. u.ck.pName
1ee99 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
1ee9a 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1ee9b 2e 63 6b 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 .ck.pVtab->pModu
1ee9c 6c 65 2d 3e 78 52 65 6e 61 6d 65 20 29 3b 0a 20 le->xRename );.
1ee9d 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1ee9e 70 4f 70 2d 3e 70 31 2c 20 75 2e 63 6b 2e 70 4e pOp->p1, u.ck.pN
1ee9f 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ame);. assert(
1eea0 75 2e 63 6b 2e 70 4e 61 6d 65 2d 3e 66 6c 61 67 u.ck.pName->flag
1eea1 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 s & MEM_Str );.
1eea2 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1eea3 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f tyOff(db) ) goto
1eea4 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1eea5 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75 2e 63 suse;. rc = u.c
1eea6 6b 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 k.pVtab->pModule
1eea7 2d 3e 78 52 65 6e 61 6d 65 28 75 2e 63 6b 2e 70 ->xRename(u.ck.p
1eea8 56 74 61 62 2c 20 75 2e 63 6b 2e 70 4e 61 6d 65 Vtab, u.ck.pName
1eea9 2d 3e 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 ->z);. sqlite3D
1eeaa 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 bFree(db, p->zEr
1eeab 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 rMsg);. p->zErr
1eeac 4d 73 67 20 3d 20 75 2e 63 6b 2e 70 56 74 61 62 Msg = u.ck.pVtab
1eead 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63 ->zErrMsg;. u.c
1eeae 6b 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 k.pVtab->zErrMsg
1eeaf 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 = 0;. if( sqli
1eeb0 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 te3SafetyOn(db)
1eeb1 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1eeb2 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 62 _to_misuse;.. b
1eeb3 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a reak;.}.#endif..
1eeb4 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1eeb5 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1eeb6 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 55 70 64 ./* Opcode: VUpd
1eeb7 61 74 65 20 50 31 20 50 32 20 50 33 20 50 34 20 ate P1 P2 P3 P4
1eeb8 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 *.**.** P4 is a
1eeb9 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 pointer to a vir
1eeba 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 tual table objec
1eebb 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 t, an sqlite3_vt
1eebc 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ab structure..**
1eebd 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 This opcode inv
1eebe 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 65 73 70 okes the corresp
1eebf 6f 6e 64 69 6e 67 20 78 55 70 64 61 74 65 20 6d onding xUpdate m
1eec0 65 74 68 6f 64 2e 20 50 32 20 76 61 6c 75 65 73 ethod. P2 values
1eec1 0a 2a 2a 20 61 72 65 20 63 6f 6e 74 69 67 75 6f .** are contiguo
1eec2 75 73 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 us memory cells
1eec3 73 74 61 72 74 69 6e 67 20 61 74 20 50 33 20 74 starting at P3 t
1eec4 6f 20 70 61 73 73 20 74 6f 20 74 68 65 20 78 55 o pass to the xU
1eec5 70 64 61 74 65 20 0a 2a 2a 20 69 6e 76 6f 63 61 pdate .** invoca
1eec6 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c 75 65 20 tion. The value
1eec7 69 6e 20 72 65 67 69 73 74 65 72 20 28 50 33 2b in register (P3+
1eec8 50 32 2d 31 29 20 63 6f 72 72 65 73 70 6f 6e 64 P2-1) correspond
1eec9 73 20 74 6f 20 74 68 65 20 0a 2a 2a 20 70 32 74 s to the .** p2t
1eeca 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 h element of the
1eecb 20 61 72 67 76 20 61 72 72 61 79 20 70 61 73 73 argv array pass
1eecc 65 64 20 74 6f 20 78 55 70 64 61 74 65 2e 0a 2a ed to xUpdate..*
1eecd 2a 0a 2a 2a 20 54 68 65 20 78 55 70 64 61 74 65 *.** The xUpdate
1eece 20 6d 65 74 68 6f 64 20 77 69 6c 6c 20 64 6f 20 method will do
1eecf 61 20 44 45 4c 45 54 45 20 6f 72 20 61 6e 20 49 a DELETE or an I
1eed0 4e 53 45 52 54 20 6f 72 20 62 6f 74 68 2e 0a 2a NSERT or both..*
1eed1 2a 20 54 68 65 20 61 72 67 76 5b 30 5d 20 65 6c * The argv[0] el
1eed2 65 6d 65 6e 74 20 28 77 68 69 63 68 20 63 6f 72 ement (which cor
1eed3 72 65 73 70 6f 6e 64 73 20 74 6f 20 6d 65 6d 6f responds to memo
1eed4 72 79 20 63 65 6c 6c 20 50 33 29 0a 2a 2a 20 69 ry cell P3).** i
1eed5 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 61 s the rowid of a
1eed6 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65 2e 20 row to delete.
1eed7 20 49 66 20 61 72 67 76 5b 30 5d 20 69 73 20 4e If argv[0] is N
1eed8 55 4c 4c 20 74 68 65 6e 20 6e 6f 20 0a 2a 2a 20 ULL then no .**
1eed9 64 65 6c 65 74 69 6f 6e 20 6f 63 63 75 72 73 2e deletion occurs.
1eeda 20 20 54 68 65 20 61 72 67 76 5b 31 5d 20 65 6c The argv[1] el
1eedb 65 6d 65 6e 74 20 69 73 20 74 68 65 20 72 6f 77 ement is the row
1eedc 69 64 20 6f 66 20 74 68 65 20 6e 65 77 20 0a 2a id of the new .*
1eedd 2a 20 72 6f 77 2e 20 20 54 68 69 73 20 63 61 6e * row. This can
1eede 20 62 65 20 4e 55 4c 4c 20 74 6f 20 68 61 76 65 be NULL to have
1eedf 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 the virtual tab
1eee0 6c 65 20 73 65 6c 65 63 74 20 74 68 65 20 6e 65 le select the ne
1eee1 77 20 0a 2a 2a 20 72 6f 77 69 64 20 66 6f 72 20 w .** rowid for
1eee2 69 74 73 65 6c 66 2e 20 20 54 68 65 20 73 75 62 itself. The sub
1eee3 73 65 71 75 65 6e 74 20 65 6c 65 6d 65 6e 74 73 sequent elements
1eee4 20 69 6e 20 74 68 65 20 61 72 72 61 79 20 61 72 in the array ar
1eee5 65 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 e .** the values
1eee6 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 of columns in t
1eee7 68 65 20 6e 65 77 20 72 6f 77 2e 0a 2a 2a 0a 2a he new row..**.*
1eee8 2a 20 49 66 20 50 32 3d 3d 31 20 74 68 65 6e 20 * If P2==1 then
1eee9 6e 6f 20 69 6e 73 65 72 74 20 69 73 20 70 65 72 no insert is per
1eeea 66 6f 72 6d 65 64 2e 20 20 61 72 67 76 5b 30 5d formed. argv[0]
1eeeb 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 is the rowid of
1eeec 0a 2a 2a 20 61 20 72 6f 77 20 74 6f 20 64 65 6c .** a row to del
1eeed 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 ete..**.** P1 is
1eeee 20 61 20 62 6f 6f 6c 65 61 6e 20 66 6c 61 67 2e a boolean flag.
1eeef 20 49 66 20 69 74 20 69 73 20 73 65 74 20 74 6f If it is set to
1eef0 20 74 72 75 65 20 61 6e 64 20 74 68 65 20 78 55 true and the xU
1eef1 70 64 61 74 65 20 63 61 6c 6c 0a 2a 2a 20 69 73 pdate call.** is
1eef2 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 successful, the
1eef3 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 n the value retu
1eef4 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f rned by sqlite3_
1eef5 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 last_insert_rowi
1eef6 64 28 29 20 0a 2a 2a 20 69 73 20 73 65 74 20 74 d() .** is set t
1eef7 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 o the value of t
1eef8 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 he rowid for the
1eef9 20 72 6f 77 20 6a 75 73 74 20 69 6e 73 65 72 74 row just insert
1eefa 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 ed..*/.case OP_V
1eefb 55 70 64 61 74 65 3a 20 7b 0a 23 69 66 20 30 20 Update: {.#if 0
1eefc 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1eefd 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1eefe 2e 63 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .cl */. sqlite3
1eeff 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 _vtab *pVtab;.
1ef00 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a sqlite3_module *
1ef01 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 6e pModule;. int n
1ef02 41 72 67 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 Arg;. int i;.
1ef03 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 6f 77 sqlite_int64 row
1ef04 69 64 3b 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 id;. Mem **apAr
1ef05 67 3b 0a 20 20 4d 65 6d 20 2a 70 58 3b 0a 23 65 g;. Mem *pX;.#e
1ef06 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1ef07 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1ef08 74 6f 20 75 2e 63 6c 20 2a 2f 0a 0a 20 20 75 2e to u.cl */.. u.
1ef09 63 6c 2e 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e cl.pVtab = pOp->
1ef0a 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 62 3b p4.pVtab->pVtab;
1ef0b 0a 20 20 75 2e 63 6c 2e 70 4d 6f 64 75 6c 65 20 . u.cl.pModule
1ef0c 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c = (sqlite3_modul
1ef0d 65 20 2a 29 75 2e 63 6c 2e 70 56 74 61 62 2d 3e e *)u.cl.pVtab->
1ef0e 70 4d 6f 64 75 6c 65 3b 0a 20 20 75 2e 63 6c 2e pModule;. u.cl.
1ef0f 6e 41 72 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a nArg = pOp->p2;.
1ef10 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1ef11 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 20 29 4type==P4_VTAB )
1ef12 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 ;. if( ALWAYS(u
1ef13 2e 63 6c 2e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 .cl.pModule->xUp
1ef14 64 61 74 65 29 20 29 7b 0a 20 20 20 20 75 2e 63 date) ){. u.c
1ef15 6c 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 41 l.apArg = p->apA
1ef16 72 67 3b 0a 20 20 20 20 75 2e 63 6c 2e 70 58 20 rg;. u.cl.pX
1ef17 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
1ef18 70 33 5d 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63 p3];. for(u.c
1ef19 6c 2e 69 3d 30 3b 20 75 2e 63 6c 2e 69 3c 75 2e l.i=0; u.cl.i<u.
1ef1a 63 6c 2e 6e 41 72 67 3b 20 75 2e 63 6c 2e 69 2b cl.nArg; u.cl.i+
1ef1b 2b 29 7b 0a 20 20 20 20 20 20 73 74 6f 72 65 54 +){. storeT
1ef1c 79 70 65 49 6e 66 6f 28 75 2e 63 6c 2e 70 58 2c ypeInfo(u.cl.pX,
1ef1d 20 30 29 3b 0a 20 20 20 20 20 20 75 2e 63 6c 2e 0);. u.cl.
1ef1e 61 70 41 72 67 5b 75 2e 63 6c 2e 69 5d 20 3d 20 apArg[u.cl.i] =
1ef1f 75 2e 63 6c 2e 70 58 3b 0a 20 20 20 20 20 20 75 u.cl.pX;. u
1ef20 2e 63 6c 2e 70 58 2b 2b 3b 0a 20 20 20 20 7d 0a .cl.pX++;. }.
1ef21 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1ef22 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 afetyOff(db) ) g
1ef23 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1ef24 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 72 63 20 _misuse;. rc
1ef25 3d 20 75 2e 63 6c 2e 70 4d 6f 64 75 6c 65 2d 3e = u.cl.pModule->
1ef26 78 55 70 64 61 74 65 28 75 2e 63 6c 2e 70 56 74 xUpdate(u.cl.pVt
1ef27 61 62 2c 20 75 2e 63 6c 2e 6e 41 72 67 2c 20 75 ab, u.cl.nArg, u
1ef28 2e 63 6c 2e 61 70 41 72 67 2c 20 26 75 2e 63 6c .cl.apArg, &u.cl
1ef29 2e 72 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c .rowid);. sql
1ef2a 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1ef2b 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 ->zErrMsg);.
1ef2c 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 p->zErrMsg = u.c
1ef2d 6c 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 l.pVtab->zErrMsg
1ef2e 3b 0a 20 20 20 20 75 2e 63 6c 2e 70 56 74 61 62 ;. u.cl.pVtab
1ef2f 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 ->zErrMsg = 0;.
1ef30 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 if( sqlite3Sa
1ef31 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 fetyOn(db) ) got
1ef32 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1ef33 69 73 75 73 65 3b 0a 20 20 20 20 69 66 28 20 72 isuse;. if( r
1ef34 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
1ef35 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 20 pOp->p1 ){.
1ef36 20 61 73 73 65 72 74 28 20 75 2e 63 6c 2e 6e 41 assert( u.cl.nA
1ef37 72 67 3e 31 20 26 26 20 75 2e 63 6c 2e 61 70 41 rg>1 && u.cl.apA
1ef38 72 67 5b 30 5d 20 26 26 20 28 75 2e 63 6c 2e 61 rg[0] && (u.cl.a
1ef39 70 41 72 67 5b 30 5d 2d 3e 66 6c 61 67 73 26 4d pArg[0]->flags&M
1ef3a 45 4d 5f 4e 75 6c 6c 29 20 29 3b 0a 20 20 20 20 EM_Null) );.
1ef3b 20 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 db->lastRowid
1ef3c 3d 20 75 2e 63 6c 2e 72 6f 77 69 64 3b 0a 20 20 = u.cl.rowid;.
1ef3d 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e }. p->nChan
1ef3e 67 65 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 ge++;. }. brea
1ef3f 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 k;.}.#endif /* S
1ef40 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1ef41 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e ALTABLE */..#ifn
1ef42 64 65 66 20 20 53 51 4c 49 54 45 5f 4f 4d 49 54 def SQLITE_OMIT
1ef43 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f _PAGER_PRAGMAS./
1ef44 2a 20 4f 70 63 6f 64 65 3a 20 50 61 67 65 63 6f * Opcode: Pageco
1ef45 75 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a unt P1 P2 * * *.
1ef46 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 **.** Write the
1ef47 63 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f current number o
1ef48 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 f pages in datab
1ef49 61 73 65 20 50 31 20 74 6f 20 6d 65 6d 6f 72 79 ase P1 to memory
1ef4a 20 63 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 cell P2..*/.cas
1ef4b 65 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 3a 20 e OP_Pagecount:
1ef4c 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1ef4d 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1ef4e 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1ef4f 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1ef50 65 64 20 69 6e 74 6f 20 75 2e 63 6d 20 2a 2f 0a ed into u.cm */.
1ef51 20 20 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20 int p1;. int
1ef52 6e 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a nPage;. Pager *
1ef53 70 50 61 67 65 72 3b 0a 23 65 6e 64 69 66 20 2f pPager;.#endif /
1ef54 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1ef55 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 s moved into u.c
1ef56 6d 20 2a 2f 0a 0a 20 20 75 2e 63 6d 2e 70 31 20 m */.. u.cm.p1
1ef57 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 63 = pOp->p1;. u.c
1ef58 6d 2e 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 m.pPager = sqlit
1ef59 65 33 42 74 72 65 65 50 61 67 65 72 28 64 62 2d e3BtreePager(db-
1ef5a 3e 61 44 62 5b 75 2e 63 6d 2e 70 31 5d 2e 70 42 >aDb[u.cm.p1].pB
1ef5b 74 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 t);. rc = sqlit
1ef5c 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
1ef5d 28 75 2e 63 6d 2e 70 50 61 67 65 72 2c 20 26 75 (u.cm.pPager, &u
1ef5e 2e 63 6d 2e 6e 50 61 67 65 29 3b 0a 20 20 2f 2a .cm.nPage);. /*
1ef5f 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 69 73 OP_Pagecount is
1ef60 20 61 6c 77 61 79 73 20 63 61 6c 6c 65 64 20 66 always called f
1ef61 72 6f 6d 20 77 69 74 68 69 6e 20 61 20 72 65 61 rom within a rea
1ef62 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 d transaction.
1ef63 54 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 63 6f The. ** page co
1ef64 75 6e 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 unt has already
1ef65 62 65 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c been successfull
1ef66 79 20 72 65 61 64 20 61 6e 64 20 63 61 63 68 65 y read and cache
1ef67 64 2e 20 20 53 6f 20 74 68 65 0a 20 20 2a 2a 20 d. So the. **
1ef68 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
1ef69 63 6f 75 6e 74 28 29 20 63 61 6c 6c 20 61 62 6f count() call abo
1ef6a 76 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 ve cannot fail.
1ef6b 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 */. if( ALWAYS(
1ef6c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 rc==SQLITE_OK) )
1ef6d 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 {. pOut->flag
1ef6e 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
1ef6f 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 63 pOut->u.i = u.c
1ef70 6d 2e 6e 50 61 67 65 3b 0a 20 20 7d 0a 20 20 62 m.nPage;. }. b
1ef71 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a reak;.}.#endif..
1ef72 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ef73 4d 49 54 5f 54 52 41 43 45 0a 2f 2a 20 4f 70 63 MIT_TRACE./* Opc
1ef74 6f 64 65 3a 20 54 72 61 63 65 20 2a 20 2a 20 2a ode: Trace * * *
1ef75 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 P4 *.**.** If t
1ef76 72 61 63 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 racing is enable
1ef77 64 20 28 62 79 20 74 68 65 20 73 71 6c 69 74 65 d (by the sqlite
1ef78 33 5f 74 72 61 63 65 28 29 29 20 69 6e 74 65 72 3_trace()) inter
1ef79 66 61 63 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 face, then.** th
1ef7a 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 63 e UTF-8 string c
1ef7b 6f 6e 74 61 69 6e 65 64 20 69 6e 20 50 34 20 69 ontained in P4 i
1ef7c 73 20 65 6d 69 74 74 65 64 20 6f 6e 20 74 68 65 s emitted on the
1ef7d 20 74 72 61 63 65 20 63 61 6c 6c 62 61 63 6b 2e trace callback.
1ef7e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 72 61 63 .*/.case OP_Trac
1ef7f 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c e: {.#if 0 /* l
1ef80 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1ef81 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6e 20 2a oved into u.cn *
1ef82 2f 0a 20 20 63 68 61 72 20 2a 7a 54 72 61 63 65 /. char *zTrace
1ef83 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1ef84 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1ef85 64 20 69 6e 74 6f 20 75 2e 63 6e 20 2a 2f 0a 0a d into u.cn */..
1ef86 20 20 75 2e 63 6e 2e 7a 54 72 61 63 65 20 3d 20 u.cn.zTrace =
1ef87 28 70 4f 70 2d 3e 70 34 2e 7a 20 3f 20 70 4f 70 (pOp->p4.z ? pOp
1ef88 2d 3e 70 34 2e 7a 20 3a 20 70 2d 3e 7a 53 71 6c ->p4.z : p->zSql
1ef89 29 3b 0a 20 20 69 66 28 20 75 2e 63 6e 2e 7a 54 );. if( u.cn.zT
1ef8a 72 61 63 65 20 29 7b 0a 20 20 20 20 69 66 28 20 race ){. if(
1ef8b 64 62 2d 3e 78 54 72 61 63 65 20 29 7b 0a 20 20 db->xTrace ){.
1ef8c 20 20 20 20 64 62 2d 3e 78 54 72 61 63 65 28 64 db->xTrace(d
1ef8d 62 2d 3e 70 54 72 61 63 65 41 72 67 2c 20 75 2e b->pTraceArg, u.
1ef8e 63 6e 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 cn.zTrace);.
1ef8f 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f }.#ifdef SQLITE_
1ef90 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 28 64 DEBUG. if( (d
1ef91 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 b->flags & SQLIT
1ef92 45 5f 53 71 6c 54 72 61 63 65 29 21 3d 30 20 29 E_SqlTrace)!=0 )
1ef93 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 {. sqlite3D
1ef94 65 62 75 67 50 72 69 6e 74 66 28 22 53 51 4c 2d ebugPrintf("SQL-
1ef95 74 72 61 63 65 3a 20 25 73 5c 6e 22 2c 20 75 2e trace: %s\n", u.
1ef96 63 6e 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 cn.zTrace);.
1ef97 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1ef98 54 45 5f 44 45 42 55 47 20 2a 2f 0a 20 20 7d 0a TE_DEBUG */. }.
1ef99 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 break;.}.#endi
1ef9a 66 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e f.../* Opcode: N
1ef9b 6f 6f 70 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a oop * * * * *.**
1ef9c 0a 2a 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 .** Do nothing.
1ef9d 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f This instructio
1ef9e 6e 20 69 73 20 6f 66 74 65 6e 20 75 73 65 66 75 n is often usefu
1ef9f 6c 20 61 73 20 61 20 6a 75 6d 70 0a 2a 2a 20 64 l as a jump.** d
1efa0 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f estination..*/./
1efa1 2a 0a 2a 2a 20 54 68 65 20 6d 61 67 69 63 20 45 *.** The magic E
1efa2 78 70 6c 61 69 6e 20 6f 70 63 6f 64 65 20 61 72 xplain opcode ar
1efa3 65 20 6f 6e 6c 79 20 69 6e 73 65 72 74 65 64 20 e only inserted
1efa4 77 68 65 6e 20 65 78 70 6c 61 69 6e 3d 3d 32 20 when explain==2
1efa5 28 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 6f 20 (which.** is to
1efa6 73 61 79 20 77 68 65 6e 20 74 68 65 20 45 58 50 say when the EXP
1efa7 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20 LAIN QUERY PLAN
1efa8 73 79 6e 74 61 78 20 69 73 20 75 73 65 64 2e 29 syntax is used.)
1efa9 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 .** This opcode
1efaa 72 65 63 6f 72 64 73 20 69 6e 66 6f 72 6d 61 74 records informat
1efab 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 6f 70 74 ion from the opt
1efac 69 6d 69 7a 65 72 2e 20 20 49 74 20 69 73 20 74 imizer. It is t
1efad 68 65 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 he.** the same a
1efae 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 69 73 s a no-op. This
1efaf 20 6f 70 63 6f 64 65 73 6e 65 76 65 72 20 61 70 opcodesnever ap
1efb0 70 65 61 72 73 20 69 6e 20 61 20 72 65 61 6c 20 pears in a real
1efb1 56 4d 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 64 VM program..*/.d
1efb2 65 66 61 75 6c 74 3a 20 7b 20 20 20 20 20 20 20 efault: {
1efb3 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 72 65 /* This is re
1efb4 61 6c 6c 79 20 4f 50 5f 4e 6f 6f 70 20 61 6e 64 ally OP_Noop and
1efb5 20 4f 50 5f 45 78 70 6c 61 69 6e 20 2a 2f 0a 20 OP_Explain */.
1efb6 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a break;.}../****
1efb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1efb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1efb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1efba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1efbb 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 *********.** The
1efbc 20 63 61 73 65 73 20 6f 66 20 74 68 65 20 73 77 cases of the sw
1efbd 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 61 itch statement a
1efbe 62 6f 76 65 20 74 68 69 73 20 6c 69 6e 65 20 73 bove this line s
1efbf 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20 69 6e 64 hould all be ind
1efc0 65 6e 74 65 64 0a 2a 2a 20 62 79 20 36 20 73 70 ented.** by 6 sp
1efc1 61 63 65 73 2e 20 20 42 75 74 20 74 68 65 20 6c aces. But the l
1efc2 65 66 74 2d 6d 6f 73 74 20 36 20 73 70 61 63 65 eft-most 6 space
1efc3 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 6d 6f s have been remo
1efc4 76 65 64 20 74 6f 20 69 6d 70 72 6f 76 65 20 74 ved to improve t
1efc5 68 65 0a 2a 2a 20 72 65 61 64 61 62 69 6c 69 74 he.** readabilit
1efc6 79 2e 20 20 46 72 6f 6d 20 74 68 69 73 20 70 6f y. From this po
1efc7 69 6e 74 20 6f 6e 20 64 6f 77 6e 2c 20 74 68 65 int on down, the
1efc8 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74 normal indentat
1efc9 69 6f 6e 20 72 75 6c 65 73 20 61 72 65 0a 2a 2a ion rules are.**
1efca 20 72 65 73 74 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a restored..*****
1efcb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1efcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1efcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1efce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1efcf 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 20 20 20 20 7d 0a ********/. }.
1efd0 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f .#ifdef VDBE_PRO
1efd1 46 49 4c 45 0a 20 20 20 20 7b 0a 20 20 20 20 20 FILE. {.
1efd2 20 75 36 34 20 65 6c 61 70 73 65 64 20 3d 20 73 u64 elapsed = s
1efd3 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 2d qlite3Hwtime() -
1efd4 20 73 74 61 72 74 3b 0a 20 20 20 20 20 20 70 4f start;. pO
1efd5 70 2d 3e 63 79 63 6c 65 73 20 2b 3d 20 65 6c 61 p->cycles += ela
1efd6 70 73 65 64 3b 0a 20 20 20 20 20 20 70 4f 70 2d psed;. pOp-
1efd7 3e 63 6e 74 2b 2b 3b 0a 23 69 66 20 30 0a 20 20 >cnt++;.#if 0.
1efd8 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 fprintf(st
1efd9 64 6f 75 74 2c 20 22 25 31 30 6c 6c 75 20 22 2c dout, "%10llu ",
1efda 20 65 6c 61 70 73 65 64 29 3b 0a 20 20 20 20 20 elapsed);.
1efdb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 sqlite3VdbePr
1efdc 69 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 6f 72 intOp(stdout, or
1efdd 69 67 50 63 2c 20 26 70 2d 3e 61 4f 70 5b 6f 72 igPc, &p->aOp[or
1efde 69 67 50 63 5d 29 3b 0a 23 65 6e 64 69 66 0a 20 igPc]);.#endif.
1efdf 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
1efe0 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e /* The followin
1efe1 67 20 63 6f 64 65 20 61 64 64 73 20 6e 6f 74 68 g code adds noth
1efe2 69 6e 67 20 74 6f 20 74 68 65 20 61 63 74 75 61 ing to the actua
1efe3 6c 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a l functionality.
1efe4 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 72 ** of the pr
1efe5 6f 67 72 61 6d 2e 20 20 49 74 20 69 73 20 6f 6e ogram. It is on
1efe6 6c 79 20 68 65 72 65 20 66 6f 72 20 74 65 73 74 ly here for test
1efe7 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e ing and debuggin
1efe8 67 2e 0a 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 g.. ** On the
1efe9 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 74 20 other hand, it
1efea 64 6f 65 73 20 62 75 72 6e 20 43 50 55 20 63 79 does burn CPU cy
1efeb 63 6c 65 73 20 65 76 65 72 79 20 74 69 6d 65 20 cles every time
1efec 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74 through. ** t
1efed 68 65 20 65 76 61 6c 75 61 74 6f 72 20 6c 6f 6f he evaluator loo
1efee 70 2e 20 20 53 6f 20 77 65 20 63 61 6e 20 6c 65 p. So we can le
1efef 61 76 65 20 69 74 20 6f 75 74 20 77 68 65 6e 20 ave it out when
1eff0 4e 44 45 42 55 47 20 69 73 20 64 65 66 69 6e 65 NDEBUG is define
1eff1 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 d.. */.#ifnde
1eff2 66 20 4e 44 45 42 55 47 0a 20 20 20 20 61 73 73 f NDEBUG. ass
1eff3 65 72 74 28 20 70 63 3e 3d 2d 31 20 26 26 20 70 ert( pc>=-1 && p
1eff4 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 0a 23 69 66 c<p->nOp );..#if
1eff5 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
1eff6 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 . if( p->trac
1eff7 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 e ){. if( r
1eff8 63 21 3d 30 20 29 20 66 70 72 69 6e 74 66 28 70 c!=0 ) fprintf(p
1eff9 2d 3e 74 72 61 63 65 2c 22 72 63 3d 25 64 5c 6e ->trace,"rc=%d\n
1effa 22 2c 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 ",rc);. if(
1effb 20 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 opProperty & OP
1effc 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 FLG_OUT2_PRERELE
1effd 41 53 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ASE ){. r
1effe 65 67 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e egisterTrace(p->
1efff 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70 32 2c 20 trace, pOp->p2,
1f000 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 pOut);. }.
1f001 20 20 20 20 20 69 66 28 20 6f 70 50 72 6f 70 65 if( opPrope
1f002 72 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 rty & OPFLG_OUT3
1f003 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 67 69 ){. regi
1f004 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 sterTrace(p->tra
1f005 63 65 2c 20 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 ce, pOp->p3, pOu
1f006 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 t);. }.
1f007 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c }.#endif /* SQL
1f008 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 23 65 6e ITE_DEBUG */.#en
1f009 64 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a dif /* NDEBUG *
1f00a 2f 0a 20 20 7d 20 20 2f 2a 20 54 68 65 20 65 6e /. } /* The en
1f00b 64 20 6f 66 20 74 68 65 20 66 6f 72 28 3b 3b 29 d of the for(;;)
1f00c 20 6c 6f 6f 70 20 74 68 65 20 6c 6f 6f 70 73 20 loop the loops
1f00d 74 68 72 6f 75 67 68 20 6f 70 63 6f 64 65 73 20 through opcodes
1f00e 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 */.. /* If we r
1f00f 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c each this point,
1f010 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 65 it means that e
1f011 78 65 63 75 74 69 6f 6e 20 69 73 20 66 69 6e 69 xecution is fini
1f012 73 68 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 61 shed with. ** a
1f013 6e 20 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20 n error of some
1f014 6b 69 6e 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 5f kind.. */.vdbe_
1f015 65 72 72 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 73 error_halt:. as
1f016 73 65 72 74 28 20 72 63 20 29 3b 0a 20 20 70 2d sert( rc );. p-
1f017 3e 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 >rc = rc;. sqli
1f018 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a te3VdbeHalt(p);.
1f019 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1f01a 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 64 _IOERR_NOMEM ) d
1f01b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1f01c 3d 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 = 1;. rc = SQLI
1f01d 54 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20 TE_ERROR;.. /*
1f01e 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 This is the only
1f01f 20 77 61 79 20 6f 75 74 20 6f 66 20 74 68 69 73 way out of this
1f020 20 70 72 6f 63 65 64 75 72 65 2e 20 20 57 65 20 procedure. We
1f021 68 61 76 65 20 74 6f 0a 20 20 2a 2a 20 72 65 6c have to. ** rel
1f022 65 61 73 65 20 74 68 65 20 6d 75 74 65 78 65 73 ease the mutexes
1f023 20 6f 6e 20 62 74 72 65 65 73 20 74 68 61 74 20 on btrees that
1f024 77 65 72 65 20 61 63 71 75 69 72 65 64 20 61 74 were acquired at
1f025 20 74 68 65 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a the. ** top. *
1f026 2f 0a 76 64 62 65 5f 72 65 74 75 72 6e 3a 0a 20 /.vdbe_return:.
1f027 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 sqlite3BtreeMut
1f028 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d exArrayLeave(&p-
1f029 3e 61 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 >aMutex);. retu
1f02a 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75 6d rn rc;.. /* Jum
1f02b 70 20 74 6f 20 68 65 72 65 20 69 66 20 61 20 73 p to here if a s
1f02c 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 61 tring or blob la
1f02d 72 67 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 rger than SQLITE
1f02e 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 20 20 2a 2a _MAX_LENGTH. **
1f02f 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e is encountered.
1f030 0a 20 20 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a 20 . */.too_big:.
1f031 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e sqlite3SetStrin
1f032 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 g(&p->zErrMsg, d
1f033 62 2c 20 22 73 74 72 69 6e 67 20 6f 72 20 62 6c b, "string or bl
1f034 6f 62 20 74 6f 6f 20 62 69 67 22 29 3b 0a 20 20 ob too big");.
1f035 72 63 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 rc = SQLITE_TOOB
1f036 49 47 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f IG;. goto vdbe_
1f037 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f error_halt;.. /
1f038 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 * Jump to here i
1f039 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 f a malloc() fai
1f03a 6c 73 2e 0a 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a ls.. */.no_mem:
1f03b 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 . db->mallocFai
1f03c 6c 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 led = 1;. sqlit
1f03d 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1f03e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 6f 75 zErrMsg, db, "ou
1f03f 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 t of memory");.
1f040 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
1f041 45 4d 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f EM;. goto vdbe_
1f042 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f error_halt;.. /
1f043 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 * Jump to here f
1f044 6f 72 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 or an SQLITE_MIS
1f045 55 53 45 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a USE error.. */.
1f046 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 abort_due_to_mis
1f047 75 73 65 3a 0a 20 20 72 63 20 3d 20 53 51 4c 49 use:. rc = SQLI
1f048 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 2f 2a 20 TE_MISUSE;. /*
1f049 46 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 61 Fall thru into a
1f04a 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1f04b 72 20 2a 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 r */.. /* Jump
1f04c 74 6f 20 68 65 72 65 20 66 6f 72 20 61 6e 79 20 to here for any
1f04d 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 other kind of fa
1f04e 74 61 6c 20 65 72 72 6f 72 2e 20 20 54 68 65 20 tal error. The
1f04f 22 72 63 22 20 76 61 72 69 61 62 6c 65 0a 20 20 "rc" variable.
1f050 2a 2a 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 ** should hold t
1f051 68 65 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e he error number.
1f052 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f . */.abort_due_
1f053 74 6f 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 to_error:. asse
1f054 72 74 28 20 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d rt( p->zErrMsg==
1f055 30 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 0 );. if( db->m
1f056 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 63 allocFailed ) rc
1f057 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
1f058 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
1f059 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b E_IOERR_NOMEM ){
1f05a 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 . sqlite3SetS
1f05b 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1f05c 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c g, db, "%s", sql
1f05d 69 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b ite3ErrStr(rc));
1f05e 0a 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 . }. goto vdbe
1f05f 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 _error_halt;..
1f060 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 /* Jump to here
1f061 69 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 if the sqlite3_i
1f062 6e 74 65 72 72 75 70 74 28 29 20 41 50 49 20 73 nterrupt() API s
1f063 65 74 73 20 74 68 65 20 69 6e 74 65 72 72 75 70 ets the interrup
1f064 74 0a 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 2a t. ** flag.. *
1f065 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 /.abort_due_to_i
1f066 6e 74 65 72 72 75 70 74 3a 0a 20 20 61 73 73 65 nterrupt:. asse
1f067 72 74 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 rt( db->u1.isInt
1f068 65 72 72 75 70 74 65 64 20 29 3b 0a 20 20 72 63 errupted );. rc
1f069 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 = SQLITE_INTERR
1f06a 55 50 54 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 UPT;. p->rc = r
1f06b 63 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 74 53 c;. sqlite3SetS
1f06c 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1f06d 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c g, db, "%s", sql
1f06e 69 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b ite3ErrStr(rc));
1f06f 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 . goto vdbe_err
1f070 6f 72 5f 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a or_halt;.}../***
1f071 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
1f072 6f 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a of vdbe.c ******
1f073 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f074 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f075 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
1f076 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
1f077 6e 20 66 69 6c 65 20 76 64 62 65 62 6c 6f 62 2e n file vdbeblob.
1f078 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1f079 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f07a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
1f07b 2a 20 32 30 30 37 20 4d 61 79 20 31 0a 2a 2a 0a * 2007 May 1.**.
1f07c 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
1f07d 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
1f07e 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
1f07f 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
1f080 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
1f081 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
1f082 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
1f083 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
1f084 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
1f085 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1f086 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
1f087 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
1f088 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
1f089 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1f08a 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
1f08b 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
1f08c 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
1f08d 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
1f08e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f08f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f091 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
1f092 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
1f093 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 ains code used t
1f094 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 69 6e 63 72 o implement incr
1f095 65 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f emental BLOB I/O
1f096 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 ..**.** $Id: vdb
1f097 65 62 6c 6f 62 2e 63 2c 76 20 31 2e 33 35 20 32 eblob.c,v 1.35 2
1f098 30 30 39 2f 30 37 2f 30 32 20 30 37 3a 34 37 3a 009/07/02 07:47:
1f099 33 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 33 danielk1977 E
1f09a 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 xp $.*/...#ifnde
1f09b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
1f09c 43 52 42 4c 4f 42 0a 0a 2f 2a 0a 2a 2a 20 56 61 CRBLOB../*.** Va
1f09d 6c 69 64 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 lid sqlite3_blob
1f09e 2a 20 68 61 6e 64 6c 65 73 20 70 6f 69 6e 74 20 * handles point
1f09f 74 6f 20 49 6e 63 72 62 6c 6f 62 20 73 74 72 75 to Incrblob stru
1f0a0 63 74 75 72 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 ctures..*/.typed
1f0a1 65 66 20 73 74 72 75 63 74 20 49 6e 63 72 62 6c ef struct Incrbl
1f0a2 6f 62 20 49 6e 63 72 62 6c 6f 62 3b 0a 73 74 72 ob Incrblob;.str
1f0a3 75 63 74 20 49 6e 63 72 62 6c 6f 62 20 7b 0a 20 uct Incrblob {.
1f0a4 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 int flags;
1f0a5 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 /* Copy
1f0a6 20 6f 66 20 22 66 6c 61 67 73 22 20 70 61 73 73 of "flags" pass
1f0a7 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 6c ed to sqlite3_bl
1f0a8 6f 62 5f 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 ob_open() */. i
1f0a9 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 nt nByte;
1f0aa 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
1f0ab 66 20 6f 70 65 6e 20 62 6c 6f 62 2c 20 69 6e 20 f open blob, in
1f0ac 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 bytes */. int i
1f0ad 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 Offset;
1f0ae 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 /* Byte offse
1f0af 74 20 6f 66 20 62 6c 6f 62 20 69 6e 20 63 75 72 t of blob in cur
1f0b0 73 6f 72 20 64 61 74 61 20 2a 2f 0a 20 20 42 74 sor data */. Bt
1f0b1 43 75 72 73 6f 72 20 2a 70 43 73 72 3b 20 20 20 Cursor *pCsr;
1f0b2 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 /* Cursor
1f0b3 70 6f 69 6e 74 69 6e 67 20 61 74 20 62 6c 6f 62 pointing at blob
1f0b4 20 72 6f 77 20 2a 2f 0a 20 20 73 71 6c 69 74 65 row */. sqlite
1f0b5 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 3_stmt *pStmt;
1f0b6 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 68 /* Statement h
1f0b7 6f 6c 64 69 6e 67 20 63 75 72 73 6f 72 20 6f 70 olding cursor op
1f0b8 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 en */. sqlite3
1f0b9 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 *db;
1f0ba 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 /* The associate
1f0bb 64 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 7d 3b d database */.};
1f0bc 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 62 ../*.** Open a b
1f0bd 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 lob handle..*/.S
1f0be 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1f0bf 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 lite3_blob_open(
1f0c0 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 . sqlite3* db,
1f0c1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1f0c2 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
1f0c3 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 ction */. const
1f0c4 20 63 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 char *zDb,
1f0c5 20 20 20 2f 2a 20 54 68 65 20 61 74 74 61 63 68 /* The attach
1f0c6 65 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 ed database cont
1f0c7 61 69 6e 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 aining the blob
1f0c8 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1f0c9 2a 7a 54 61 62 6c 65 2c 20 20 20 20 20 2f 2a 20 *zTable, /*
1f0ca 54 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 The table contai
1f0cb 6e 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f ning the blob */
1f0cc 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1f0cd 43 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 54 68 Column, /* Th
1f0ce 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e e column contain
1f0cf 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a ing the blob */.
1f0d0 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 sqlite_int64 i
1f0d1 52 6f 77 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 Row, /* The
1f0d2 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 row containing
1f0d3 74 68 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 69 6e the glob */. in
1f0d4 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 t flags,
1f0d5 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 2d 3e /* True ->
1f0d6 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 read/write acce
1f0d7 73 73 2c 20 66 61 6c 73 65 20 2d 3e 20 72 65 61 ss, false -> rea
1f0d8 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 71 6c 69 d-only */. sqli
1f0d9 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f te3_blob **ppBlo
1f0da 62 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f b /* Handle fo
1f0db 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 r accessing the
1f0dc 62 6c 6f 62 20 72 65 74 75 72 6e 65 64 20 68 65 blob returned he
1f0dd 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e re */.){. int n
1f0de 41 74 74 65 6d 70 74 20 3d 20 30 3b 0a 20 20 69 Attempt = 0;. i
1f0df 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 nt iCol;
1f0e0 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
1f0e1 6f 66 20 7a 43 6f 6c 75 6d 6e 20 69 6e 20 72 6f of zColumn in ro
1f0e2 77 2d 72 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 2f w-record */.. /
1f0e3 2a 20 54 68 69 73 20 56 44 42 45 20 70 72 6f 67 * This VDBE prog
1f0e4 72 61 6d 20 73 65 65 6b 73 20 61 20 62 74 72 65 ram seeks a btre
1f0e5 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 e cursor to the
1f0e6 69 64 65 6e 74 69 66 69 65 64 20 0a 20 20 2a 2a identified . **
1f0e7 20 64 62 2f 74 61 62 6c 65 2f 72 6f 77 20 65 6e db/table/row en
1f0e8 74 72 79 2e 20 54 68 65 20 72 65 61 73 6f 6e 20 try. The reason
1f0e9 66 6f 72 20 75 73 69 6e 67 20 61 20 76 64 62 65 for using a vdbe
1f0ea 20 70 72 6f 67 72 61 6d 20 69 6e 73 74 65 61 64 program instead
1f0eb 0a 20 20 2a 2a 20 6f 66 20 77 72 69 74 69 6e 67 . ** of writing
1f0ec 20 63 6f 64 65 20 74 6f 20 75 73 65 20 74 68 65 code to use the
1f0ed 20 62 2d 74 72 65 65 20 6c 61 79 65 72 20 64 69 b-tree layer di
1f0ee 72 65 63 74 6c 79 20 69 73 20 74 68 61 74 20 74 rectly is that t
1f0ef 68 65 0a 20 20 2a 2a 20 76 64 62 65 20 70 72 6f he. ** vdbe pro
1f0f0 67 72 61 6d 20 77 69 6c 6c 20 74 61 6b 65 20 61 gram will take a
1f0f1 64 76 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20 dvantage of the
1f0f2 76 61 72 69 6f 75 73 20 74 72 61 6e 73 61 63 74 various transact
1f0f3 69 6f 6e 2c 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e ion,. ** lockin
1f0f4 67 20 61 6e 64 20 65 72 72 6f 72 20 68 61 6e 64 g and error hand
1f0f5 6c 69 6e 67 20 69 6e 66 72 61 73 74 72 75 63 74 ling infrastruct
1f0f6 75 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 ure built into t
1f0f7 68 65 20 76 64 62 65 2e 0a 20 20 2a 2a 0a 20 20 he vdbe.. **.
1f0f8 2a 2a 20 41 66 74 65 72 20 73 65 65 6b 69 6e 67 ** After seeking
1f0f9 20 74 68 65 20 63 75 72 73 6f 72 2c 20 74 68 65 the cursor, the
1f0fa 20 76 64 62 65 20 65 78 65 63 75 74 65 73 20 61 vdbe executes a
1f0fb 6e 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 0a n OP_ResultRow..
1f0fc 20 20 2a 2a 20 43 6f 64 65 20 65 78 74 65 72 6e ** Code extern
1f0fd 61 6c 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 al to the Vdbe t
1f0fe 68 65 6e 20 22 62 6f 72 72 6f 77 73 22 20 74 68 hen "borrows" th
1f0ff 65 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 e b-tree cursor
1f100 61 6e 64 0a 20 20 2a 2a 20 75 73 65 73 20 69 74 and. ** uses it
1f101 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 to implement th
1f102 65 20 62 6c 6f 62 5f 72 65 61 64 28 29 2c 20 62 e blob_read(), b
1f103 6c 6f 62 5f 77 72 69 74 65 28 29 20 61 6e 64 20 lob_write() and
1f104 0a 20 20 2a 2a 20 62 6c 6f 62 5f 62 79 74 65 73 . ** blob_bytes
1f105 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 () functions..
1f106 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 **. ** The sqli
1f107 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 te3_blob_close()
1f108 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69 function finali
1f109 7a 65 73 20 74 68 65 20 76 64 62 65 20 70 72 6f zes the vdbe pro
1f10a 67 72 61 6d 2c 0a 20 20 2a 2a 20 77 68 69 63 68 gram,. ** which
1f10b 20 63 6c 6f 73 65 73 20 74 68 65 20 62 2d 74 72 closes the b-tr
1f10c 65 65 20 63 75 72 73 6f 72 20 61 6e 64 20 28 70 ee cursor and (p
1f10d 6f 73 73 69 62 6c 79 29 20 63 6f 6d 6d 69 74 73 ossibly) commits
1f10e 20 74 68 65 20 0a 20 20 2a 2a 20 74 72 61 6e 73 the . ** trans
1f10f 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 action.. */. s
1f110 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 tatic const Vdbe
1f111 4f 70 4c 69 73 74 20 6f 70 65 6e 42 6c 6f 62 5b OpList openBlob[
1f112 5d 20 3d 20 7b 0a 20 20 20 20 7b 4f 50 5f 54 72 ] = {. {OP_Tr
1f113 61 6e 73 61 63 74 69 6f 6e 2c 20 30 2c 20 30 2c ansaction, 0, 0,
1f114 20 30 7d 2c 20 20 20 20 20 2f 2a 20 30 3a 20 53 0}, /* 0: S
1f115 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 tart a transacti
1f116 6f 6e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 56 65 on */. {OP_Ve
1f117 72 69 66 79 43 6f 6f 6b 69 65 2c 20 30 2c 20 30 rifyCookie, 0, 0
1f118 2c 20 30 7d 2c 20 20 20 20 2f 2a 20 31 3a 20 43 , 0}, /* 1: C
1f119 68 65 63 6b 20 74 68 65 20 73 63 68 65 6d 61 20 heck the schema
1f11a 63 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 7b 4f cookie */. {O
1f11b 50 5f 54 61 62 6c 65 4c 6f 63 6b 2c 20 30 2c 20 P_TableLock, 0,
1f11c 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 0, 0}, /*
1f11d 32 3a 20 41 63 71 75 69 72 65 20 61 20 72 65 61 2: Acquire a rea
1f11e 64 20 6f 72 20 77 72 69 74 65 20 6c 6f 63 6b 20 d or write lock
1f11f 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f */.. /* One o
1f120 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
1f121 74 77 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 two instructions
1f122 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 is replaced by
1f123 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 20 2a 2f 0a 20 an OP_Noop. */.
1f124 20 20 20 7b 4f 50 5f 4f 70 65 6e 52 65 61 64 2c {OP_OpenRead,
1f125 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 0, 0, 0},
1f126 20 20 2f 2a 20 33 3a 20 4f 70 65 6e 20 63 75 72 /* 3: Open cur
1f127 73 6f 72 20 30 20 66 6f 72 20 72 65 61 64 69 6e sor 0 for readin
1f128 67 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70 65 g */. {OP_Ope
1f129 6e 57 72 69 74 65 2c 20 30 2c 20 30 2c 20 30 7d nWrite, 0, 0, 0}
1f12a 2c 20 20 20 20 20 20 20 2f 2a 20 34 3a 20 4f 70 , /* 4: Op
1f12b 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f 72 20 en cursor 0 for
1f12c 72 65 61 64 2f 77 72 69 74 65 20 2a 2f 0a 0a 20 read/write */..
1f12d 20 20 20 7b 4f 50 5f 56 61 72 69 61 62 6c 65 2c {OP_Variable,
1f12e 20 31 2c 20 31 2c 20 31 7d 2c 20 20 20 20 20 20 1, 1, 1},
1f12f 20 20 2f 2a 20 35 3a 20 50 75 73 68 20 74 68 65 /* 5: Push the
1f130 20 72 6f 77 69 64 20 74 6f 20 74 68 65 20 73 74 rowid to the st
1f131 61 63 6b 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4e ack */. {OP_N
1f132 6f 74 45 78 69 73 74 73 2c 20 30 2c 20 39 2c 20 otExists, 0, 9,
1f133 31 7d 2c 20 20 20 20 20 20 20 2f 2a 20 36 3a 20 1}, /* 6:
1f134 53 65 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 Seek the cursor
1f135 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6f 6c 75 6d */. {OP_Colum
1f136 6e 2c 20 30 2c 20 30 2c 20 31 7d 2c 20 20 20 20 n, 0, 0, 1},
1f137 20 20 20 20 20 20 2f 2a 20 37 20 20 2a 2f 0a 20 /* 7 */.
1f138 20 20 20 7b 4f 50 5f 52 65 73 75 6c 74 52 6f 77 {OP_ResultRow
1f139 2c 20 31 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 , 1, 0, 0},
1f13a 20 20 2f 2a 20 38 20 20 2a 2f 0a 20 20 20 20 7b /* 8 */. {
1f13b 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 2c 20 OP_Close, 0, 0,
1f13c 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 0}, /*
1f13d 20 39 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 48 9 */. {OP_H
1f13e 61 6c 74 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 alt, 0, 0, 0},
1f13f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30 20 /* 10
1f140 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 56 64 62 65 20 */. };.. Vdbe
1f141 2a 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 *v = 0;. int rc
1f142 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1f143 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a char *zErr = 0;.
1f144 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 Table *pTab;.
1f145 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a Parse *pParse;.
1f146 0a 20 20 2a 70 70 42 6c 6f 62 20 3d 20 30 3b 0a . *ppBlob = 0;.
1f147 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1f148 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 enter(db->mutex)
1f149 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 73 71 6c ;. pParse = sql
1f14a 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 ite3StackAllocRa
1f14b 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50 w(db, sizeof(*pP
1f14c 61 72 73 65 29 29 3b 0a 20 20 69 66 28 20 70 50 arse));. if( pP
1f14d 61 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 arse==0 ){. r
1f14e 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
1f14f 3b 0a 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f ;. goto blob_
1f150 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 open_out;. }.
1f151 64 6f 20 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 do {. memset(
1f152 70 50 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f pParse, 0, sizeo
1f153 66 28 50 61 72 73 65 29 29 3b 0a 20 20 20 20 70 f(Parse));. p
1f154 50 61 72 73 65 2d 3e 64 62 20 3d 20 64 62 3b 0a Parse->db = db;.
1f155 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1f156 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a SafetyOn(db) ){.
1f157 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1f158 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 ree(db, zErr);.
1f159 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 sqlite3Stac
1f15a 6b 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 kFree(db, pParse
1f15b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1f15c 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d _mutex_leave(db-
1f15d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 72 >mutex);. r
1f15e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 eturn SQLITE_MIS
1f15f 55 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 USE;. }..
1f160 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1f161 72 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 70 54 rAll(db);. pT
1f162 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 ab = sqlite3Loca
1f163 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 teTable(pParse,
1f164 30 2c 20 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0, zTable, zDb);
1f165 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 26 26 . if( pTab &&
1f166 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 IsVirtual(pTab)
1f167 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d ){. pTab =
1f168 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 0;. sqlite
1f169 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1f16a 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 , "cannot open v
1f16b 69 72 74 75 61 6c 20 74 61 62 6c 65 3a 20 25 73 irtual table: %s
1f16c 22 2c 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 ", zTable);.
1f16d 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
1f16e 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 69 _OMIT_VIEW. i
1f16f 66 28 20 70 54 61 62 20 26 26 20 70 54 61 62 2d f( pTab && pTab-
1f170 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 >pSelect ){.
1f171 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 pTab = 0;.
1f172 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1f173 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f g(pParse, "canno
1f174 74 20 6f 70 65 6e 20 76 69 65 77 3a 20 25 73 22 t open view: %s"
1f175 2c 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d , zTable);. }
1f176 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 .#endif. if(
1f177 21 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69 !pTab ){. i
1f178 66 28 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d f( pParse->zErrM
1f179 73 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 sg ){. sq
1f17a 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1f17b 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 20 20 7a zErr);. z
1f17c 45 72 72 20 3d 20 70 50 61 72 73 65 2d 3e 7a 45 Err = pParse->zE
1f17d 72 72 4d 73 67 3b 0a 20 20 20 20 20 20 20 20 70 rrMsg;. p
1f17e 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d Parse->zErrMsg =
1f17f 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
1f180 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 rc = SQLITE_ER
1f181 52 4f 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 ROR;. (void
1f182 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 )sqlite3SafetyOf
1f183 66 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c f(db);. sql
1f184 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c ite3BtreeLeaveAl
1f185 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 l(db);. got
1f186 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b o blob_open_out;
1f187 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e . }.. /* N
1f188 6f 77 20 73 65 61 72 63 68 20 70 54 61 62 20 66 ow search pTab f
1f189 6f 72 20 74 68 65 20 65 78 61 63 74 20 63 6f 6c or the exact col
1f18a 75 6d 6e 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 umn. */. for(
1f18b 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 iCol=0; iCol < p
1f18c 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b Tab->nCol; iCol+
1f18d 2b 29 20 7b 0a 20 20 20 20 20 20 69 66 28 20 73 +) {. if( s
1f18e 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 qlite3StrICmp(pT
1f18f 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a ab->aCol[iCol].z
1f190 4e 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d Name, zColumn)==
1f191 30 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 0 ){. bre
1f192 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
1f193 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d }. if( iCol==
1f194 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 pTab->nCol ){.
1f195 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1f196 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 e(db, zErr);.
1f197 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 zErr = sqlite
1f198 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 3MPrintf(db, "no
1f199 20 73 75 63 68 20 63 6f 6c 75 6d 6e 3a 20 5c 22 such column: \"
1f19a 25 73 5c 22 22 2c 20 7a 43 6f 6c 75 6d 6e 29 3b %s\"", zColumn);
1f19b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1f19c 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 TE_ERROR;.
1f19d 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 (void)sqlite3Saf
1f19e 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 etyOff(db);.
1f19f 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1f1a0 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 aveAll(db);.
1f1a1 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e goto blob_open
1f1a2 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 _out;. }..
1f1a3 20 2f 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 /* If the value
1f1a4 20 69 73 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 is being opened
1f1a5 20 66 6f 72 20 77 72 69 74 69 6e 67 2c 20 63 68 for writing, ch
1f1a6 65 63 6b 20 74 68 61 74 20 74 68 65 0a 20 20 20 eck that the.
1f1a7 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f ** column is no
1f1a8 74 20 69 6e 64 65 78 65 64 2e 20 49 74 20 69 73 t indexed. It is
1f1a9 20 61 67 61 69 6e 73 74 20 74 68 65 20 72 75 6c against the rul
1f1aa 65 73 20 74 6f 20 6f 70 65 6e 20 61 6e 0a 20 20 es to open an.
1f1ab 20 20 2a 2a 20 69 6e 64 65 78 65 64 20 63 6f 6c ** indexed col
1f1ac 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 2e umn for writing.
1f1ad 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1f1ae 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 20 49 flags ){. I
1f1af 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 ndex *pIdx;.
1f1b0 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d for(pIdx=pTab-
1f1b1 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 >pIndex; pIdx; p
1f1b2 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 Idx=pIdx->pNext)
1f1b3 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b {. int j;
1f1b4 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 . for(j=0
1f1b5 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d ; j<pIdx->nColum
1f1b6 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 n; j++){.
1f1b7 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43 if( pIdx->aiC
1f1b8 6f 6c 75 6d 6e 5b 6a 5d 3d 3d 69 43 6f 6c 20 29 olumn[j]==iCol )
1f1b9 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 {. sq
1f1ba 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1f1bb 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 20 20 20 zErr);.
1f1bc 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 zErr = sqlite
1f1bd 33 4d 50 72 69 6e 74 66 28 64 62 2c 0a 20 20 20 3MPrintf(db,.
1f1be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f1bf 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f "canno
1f1c0 74 20 6f 70 65 6e 20 69 6e 64 65 78 65 64 20 63 t open indexed c
1f1c1 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e olumn for writin
1f1c2 67 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 g");.
1f1c3 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 rc = SQLITE_ERR
1f1c4 4f 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 OR;.
1f1c5 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 (void)sqlite3Saf
1f1c6 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 etyOff(db);.
1f1c7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 sqlite3B
1f1c8 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 treeLeaveAll(db)
1f1c9 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f ;. go
1f1ca 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 to blob_open_out
1f1cb 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1f1cc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1f1cd 20 20 20 20 7d 0a 0a 20 20 20 20 76 20 3d 20 73 }.. v = s
1f1ce 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65 qlite3VdbeCreate
1f1cf 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 76 20 (db);. if( v
1f1d0 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 ){. int iDb
1f1d1 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 = sqlite3Schema
1f1d2 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 ToIndex(db, pTab
1f1d3 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 ->pSchema);.
1f1d4 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1f1d5 4f 70 4c 69 73 74 28 76 2c 20 73 69 7a 65 6f 66 OpList(v, sizeof
1f1d6 28 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 7a 65 6f (openBlob)/sizeo
1f1d7 66 28 56 64 62 65 4f 70 4c 69 73 74 29 2c 20 6f f(VdbeOpList), o
1f1d8 70 65 6e 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 penBlob);.
1f1d9 66 6c 61 67 73 20 3d 20 21 21 66 6c 61 67 73 3b flags = !!flags;
1f1da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f1db 20 2f 2a 20 66 6c 61 67 73 20 3d 20 28 66 6c 61 /* flags = (fla
1f1dc 67 73 20 3f 20 31 20 3a 20 30 29 3b 20 2a 2f 0a gs ? 1 : 0); */.
1f1dd 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 . /* Config
1f1de 75 72 65 20 74 68 65 20 4f 50 5f 54 72 61 6e 73 ure the OP_Trans
1f1df 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 action */.
1f1e0 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1f1e1 65 50 31 28 76 2c 20 30 2c 20 69 44 62 29 3b 0a eP1(v, 0, iDb);.
1f1e2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f1e3 65 43 68 61 6e 67 65 50 32 28 76 2c 20 30 2c 20 eChangeP2(v, 0,
1f1e4 66 6c 61 67 73 29 3b 0a 0a 20 20 20 20 20 20 2f flags);.. /
1f1e5 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 * Configure the
1f1e6 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 OP_VerifyCookie
1f1e7 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
1f1e8 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 VdbeChangeP1(v,
1f1e9 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 1, iDb);. s
1f1ea 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1f1eb 50 32 28 76 2c 20 31 2c 20 70 54 61 62 2d 3e 70 P2(v, 1, pTab->p
1f1ec 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 Schema->schema_c
1f1ed 6f 6f 6b 69 65 29 3b 0a 0a 20 20 20 20 20 20 2f ookie);.. /
1f1ee 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 20 6d 75 * Make sure a mu
1f1ef 74 65 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 tex is held on t
1f1f0 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 he table to be a
1f1f1 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 20 20 20 ccessed */.
1f1f2 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 sqlite3VdbeUses
1f1f3 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 20 0a Btree(v, iDb); .
1f1f4 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 . /* Config
1f1f5 75 72 65 20 74 68 65 20 4f 50 5f 54 61 62 6c 65 ure the OP_Table
1f1f6 4c 6f 63 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e Lock instruction
1f1f7 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 */. sqlite
1f1f8 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 3VdbeChangeP1(v,
1f1f9 20 32 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 2, iDb);.
1f1fa 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1f1fb 65 50 32 28 76 2c 20 32 2c 20 70 54 61 62 2d 3e eP2(v, 2, pTab->
1f1fc 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c tnum);. sql
1f1fd 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 ite3VdbeChangeP3
1f1fe 28 76 2c 20 32 2c 20 66 6c 61 67 73 29 3b 0a 20 (v, 2, flags);.
1f1ff 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f200 43 68 61 6e 67 65 50 34 28 76 2c 20 32 2c 20 70 ChangeP4(v, 2, p
1f201 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 54 Tab->zName, P4_T
1f202 52 41 4e 53 49 45 4e 54 29 3b 0a 0a 20 20 20 20 RANSIENT);..
1f203 20 20 2f 2a 20 52 65 6d 6f 76 65 20 65 69 74 68 /* Remove eith
1f204 65 72 20 74 68 65 20 4f 50 5f 4f 70 65 6e 57 72 er the OP_OpenWr
1f205 69 74 65 20 6f 72 20 4f 70 65 6e 52 65 61 64 2e ite or OpenRead.
1f206 20 53 65 74 20 74 68 65 20 50 32 20 0a 20 20 20 Set the P2 .
1f207 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 ** parameter
1f208 6f 66 20 74 68 65 20 6f 74 68 65 72 20 74 6f 20 of the other to
1f209 70 54 61 62 2d 3e 74 6e 75 6d 2e 20 20 2a 2f 0a pTab->tnum. */.
1f20a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f20b 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c eChangeToNoop(v,
1f20c 20 34 20 2d 20 66 6c 61 67 73 2c 20 31 29 3b 0a 4 - flags, 1);.
1f20d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f20e 65 43 68 61 6e 67 65 50 32 28 76 2c 20 33 20 2b eChangeP2(v, 3 +
1f20f 20 66 6c 61 67 73 2c 20 70 54 61 62 2d 3e 74 6e flags, pTab->tn
1f210 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 um);. sqlit
1f211 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 e3VdbeChangeP3(v
1f212 2c 20 33 20 2b 20 66 6c 61 67 73 2c 20 69 44 62 , 3 + flags, iDb
1f213 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e );.. /* Con
1f214 66 69 67 75 72 65 20 74 68 65 20 6e 75 6d 62 65 figure the numbe
1f215 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20 43 6f r of columns. Co
1f216 6e 66 69 67 75 72 65 20 74 68 65 20 63 75 72 73 nfigure the curs
1f217 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 or to. ** t
1f218 68 69 6e 6b 20 74 68 61 74 20 74 68 65 20 74 61 hink that the ta
1f219 62 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f 72 65 ble has one more
1f21a 20 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69 74 20 column than it
1f21b 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 really. **
1f21c 64 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f 6c 75 does. An OP_Colu
1f21d 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 mn to retrieve t
1f21e 68 69 73 20 69 6d 61 67 69 6e 61 72 79 20 63 6f his imaginary co
1f21f 6c 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20 lumn will.
1f220 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e ** always return
1f221 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 54 68 an SQL NULL. Th
1f222 69 73 20 69 73 20 75 73 65 66 75 6c 20 62 65 63 is is useful bec
1f223 61 75 73 65 20 69 74 20 6d 65 61 6e 73 0a 20 20 ause it means.
1f224 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 69 6e ** we can in
1f225 76 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 voke OP_Column t
1f226 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 76 64 o fill in the vd
1f227 62 65 20 63 75 72 73 6f 72 73 20 74 79 70 65 20 be cursors type
1f228 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6f 66 . ** and of
1f229 66 73 65 74 20 63 61 63 68 65 20 77 69 74 68 6f fset cache witho
1f22a 75 74 20 63 61 75 73 69 6e 67 20 61 6e 79 20 49 ut causing any I
1f22b 4f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 O.. */.
1f22c 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1f22d 6e 67 65 50 34 28 76 2c 20 33 2b 66 6c 61 67 73 ngeP4(v, 3+flags
1f22e 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f , SQLITE_INT_TO_
1f22f 50 54 52 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 PTR(pTab->nCol+1
1f230 29 2c 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 ),P4_INT32);.
1f231 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
1f232 61 6e 67 65 50 32 28 76 2c 20 37 2c 20 70 54 61 angeP2(v, 7, pTa
1f233 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 b->nCol);.
1f234 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( !db->mallocF
1f235 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 ailed ){.
1f236 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
1f237 52 65 61 64 79 28 76 2c 20 31 2c 20 31 2c 20 31 Ready(v, 1, 1, 1
1f238 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 , 0, 0, 0);.
1f239 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 0a 20 20 }. }. .
1f23a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1f23b 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 aveAll(db);.
1f23c 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65 rc = sqlite3Safe
1f23d 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 69 tyOff(db);. i
1f23e 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c f( NEVER(rc!=SQL
1f23f 49 54 45 5f 4f 4b 29 20 7c 7c 20 64 62 2d 3e 6d ITE_OK) || db->m
1f240 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1f241 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f goto blob_o
1f242 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a pen_out;. }..
1f243 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 sqlite3_bind
1f244 5f 69 6e 74 36 34 28 28 73 71 6c 69 74 65 33 5f _int64((sqlite3_
1f245 73 74 6d 74 20 2a 29 76 2c 20 31 2c 20 69 52 6f stmt *)v, 1, iRo
1f246 77 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c w);. rc = sql
1f247 69 74 65 33 5f 73 74 65 70 28 28 73 71 6c 69 74 ite3_step((sqlit
1f248 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20 e3_stmt *)v);.
1f249 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1f24a 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 6e 41 _ROW ){. nA
1f24b 74 74 65 6d 70 74 2b 2b 3b 0a 20 20 20 20 20 20 ttempt++;.
1f24c 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e rc = sqlite3_fin
1f24d 61 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 alize((sqlite3_s
1f24e 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20 20 20 20 tmt *)v);.
1f24f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1f250 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7a , zErr);. z
1f251 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 Err = sqlite3MPr
1f252 69 6e 74 66 28 64 62 2c 20 73 71 6c 69 74 65 33 intf(db, sqlite3
1f253 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 _errmsg(db));.
1f254 20 20 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 7d v = 0;. }
1f255 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 41 74 74 . } while( nAtt
1f256 65 6d 70 74 3c 35 20 26 26 20 72 63 3d 3d 53 51 empt<5 && rc==SQ
1f257 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 3b 0a 0a LITE_SCHEMA );..
1f258 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1f259 5f 52 4f 57 20 29 7b 0a 20 20 20 20 2f 2a 20 54 _ROW ){. /* T
1f25a 68 65 20 72 6f 77 2d 72 65 63 6f 72 64 20 68 61 he row-record ha
1f25b 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 73 75 s been opened su
1f25c 63 63 65 73 73 66 75 6c 6c 79 2e 20 43 68 65 63 ccessfully. Chec
1f25d 6b 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a k that the. *
1f25e 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 71 75 65 73 * column in ques
1f25f 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 65 tion contains te
1f260 78 74 20 6f 72 20 61 20 62 6c 6f 62 2e 20 49 66 xt or a blob. If
1f261 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 it contains.
1f262 20 2a 2a 20 74 65 78 74 2c 20 69 74 20 69 73 20 ** text, it is
1f263 75 70 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 up to the caller
1f264 20 74 6f 20 67 65 74 20 74 68 65 20 65 6e 63 6f to get the enco
1f265 64 69 6e 67 20 72 69 67 68 74 2e 0a 20 20 20 20 ding right..
1f266 2a 2f 0a 20 20 20 20 49 6e 63 72 62 6c 6f 62 20 */. Incrblob
1f267 2a 70 42 6c 6f 62 3b 0a 20 20 20 20 75 33 32 20 *pBlob;. u32
1f268 74 79 70 65 20 3d 20 76 2d 3e 61 70 43 73 72 5b type = v->apCsr[
1f269 30 5d 2d 3e 61 54 79 70 65 5b 69 43 6f 6c 5d 3b 0]->aType[iCol];
1f26a 0a 0a 20 20 20 20 69 66 28 20 74 79 70 65 3c 31 .. if( type<1
1f26b 32 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 2 ){. sqlit
1f26c 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 e3DbFree(db, zEr
1f26d 72 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 20 3d r);. zErr =
1f26e 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
1f26f 64 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e db, "cannot open
1f270 20 76 61 6c 75 65 20 6f 66 20 74 79 70 65 20 25 value of type %
1f271 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 74 79 s",. ty
1f272 70 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20 74 79 pe==0?"null": ty
1f273 70 65 3d 3d 37 3f 22 72 65 61 6c 22 3a 20 22 69 pe==7?"real": "i
1f274 6e 74 65 67 65 72 22 0a 20 20 20 20 20 20 29 3b nteger". );
1f275 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1f276 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 TE_ERROR;.
1f277 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f goto blob_open_o
1f278 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 ut;. }. pB
1f279 6c 6f 62 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 lob = (Incrblob
1f27a 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f *)sqlite3DbMallo
1f27b 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 cZero(db, sizeof
1f27c 28 49 6e 63 72 62 6c 6f 62 29 29 3b 0a 20 20 20 (Incrblob));.
1f27d 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
1f27e 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 73 ailed ){. s
1f27f 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1f280 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 67 pBlob);. g
1f281 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 oto blob_open_ou
1f282 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 6c t;. }. pBl
1f283 6f 62 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 ob->flags = flag
1f284 73 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 43 s;. pBlob->pC
1f285 73 72 20 3d 20 20 76 2d 3e 61 70 43 73 72 5b 30 sr = v->apCsr[0
1f286 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 ]->pCursor;.
1f287 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1f288 72 43 75 72 73 6f 72 28 70 42 6c 6f 62 2d 3e 70 rCursor(pBlob->p
1f289 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 Csr);. sqlite
1f28a 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 3BtreeCacheOverf
1f28b 6c 6f 77 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 low(pBlob->pCsr)
1f28c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1f28d 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 70 42 eeLeaveCursor(pB
1f28e 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 lob->pCsr);.
1f28f 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 3d 20 28 pBlob->pStmt = (
1f290 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 sqlite3_stmt *)v
1f291 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 69 4f 66 ;. pBlob->iOf
1f292 66 73 65 74 20 3d 20 76 2d 3e 61 70 43 73 72 5b fset = v->apCsr[
1f293 30 5d 2d 3e 61 4f 66 66 73 65 74 5b 69 43 6f 6c 0]->aOffset[iCol
1f294 5d 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 6e 42 ];. pBlob->nB
1f295 79 74 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 yte = sqlite3Vdb
1f296 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74 eSerialTypeLen(t
1f297 79 70 65 29 3b 0a 20 20 20 20 70 42 6c 6f 62 2d ype);. pBlob-
1f298 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 2a 70 >db = db;. *p
1f299 70 42 6c 6f 62 20 3d 20 28 73 71 6c 69 74 65 33 pBlob = (sqlite3
1f29a 5f 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 _blob *)pBlob;.
1f29b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
1f29c 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 K;. }else if( r
1f29d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1f29e 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1f29f 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 e(db, zErr);.
1f2a0 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d zErr = sqlite3M
1f2a1 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73 Printf(db, "no s
1f2a2 75 63 68 20 72 6f 77 69 64 3a 20 25 6c 6c 64 22 uch rowid: %lld"
1f2a3 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20 , iRow);. rc
1f2a4 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
1f2a5 20 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65 6e 5f 6f }..blob_open_o
1f2a6 75 74 3a 0a 20 20 69 66 28 20 76 20 26 26 20 28 ut:. if( v && (
1f2a7 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc!=SQLITE_OK ||
1f2a8 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1f2a9 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 d) ){. sqlite
1f2aa 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3VdbeFinalize(v)
1f2ab 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 ;. }. sqlite3E
1f2ac 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 7a 45 72 rror(db, rc, zEr
1f2ad 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 r);. sqlite3DbF
1f2ae 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 ree(db, zErr);.
1f2af 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 sqlite3StackFre
1f2b0 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 e(db, pParse);.
1f2b1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 rc = sqlite3Api
1f2b2 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 Exit(db, rc);.
1f2b3 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1f2b4 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a ave(db->mutex);.
1f2b5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1f2b6 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c /*.** Close a bl
1f2b7 6f 62 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 ob handle that w
1f2b8 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 63 72 as previously cr
1f2b9 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 eated using.** s
1f2ba 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e qlite3_blob_open
1f2bb 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ()..*/.SQLITE_AP
1f2bc 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
1f2bd 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 ob_close(sqlite3
1f2be 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 _blob *pBlob){.
1f2bf 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 Incrblob *p = (
1f2c0 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 Incrblob *)pBlob
1f2c1 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 ;. int rc;. sq
1f2c2 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 lite3 *db;.. if
1f2c3 28 20 70 20 29 7b 0a 20 20 20 20 64 62 20 3d 20 ( p ){. db =
1f2c4 70 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 p->db;. sqlit
1f2c5 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 e3_mutex_enter(d
1f2c6 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 b->mutex);. r
1f2c7 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 c = sqlite3_fina
1f2c8 6c 69 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0a lize(p->pStmt);.
1f2c9 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1f2ca 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 73 71 e(db, p);. sq
1f2cb 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1f2cc 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 e(db->mutex);.
1f2cd 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
1f2ce 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
1f2cf 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1f2d0 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 72 *.** Perform a r
1f2d1 65 61 64 20 6f 72 20 77 72 69 74 65 20 6f 70 65 ead or write ope
1f2d2 72 61 74 69 6f 6e 20 6f 6e 20 61 20 62 6c 6f 62 ration on a blob
1f2d3 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
1f2d4 6c 6f 62 52 65 61 64 57 72 69 74 65 28 0a 20 20 lobReadWrite(.
1f2d5 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 sqlite3_blob *pB
1f2d6 6c 6f 62 2c 20 0a 20 20 76 6f 69 64 20 2a 7a 2c lob, . void *z,
1f2d7 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 69 6e . int n, . in
1f2d8 74 20 69 4f 66 66 73 65 74 2c 20 0a 20 20 69 6e t iOffset, . in
1f2d9 74 20 28 2a 78 43 61 6c 6c 29 28 42 74 43 75 72 t (*xCall)(BtCur
1f2da 73 6f 72 2a 2c 20 75 33 32 2c 20 75 33 32 2c 20 sor*, u32, u32,
1f2db 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 void*).){. int
1f2dc 72 63 3b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a rc;. Incrblob *
1f2dd 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 p = (Incrblob *)
1f2de 70 42 6c 6f 62 3b 0a 20 20 56 64 62 65 20 2a 76 pBlob;. Vdbe *v
1f2df 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b ;. sqlite3 *db;
1f2e0 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 .. if( p==0 ) r
1f2e1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 eturn SQLITE_MIS
1f2e2 55 53 45 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 USE;. db = p->d
1f2e3 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 b;. sqlite3_mut
1f2e4 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 ex_enter(db->mut
1f2e5 65 78 29 3b 0a 20 20 76 20 3d 20 28 56 64 62 65 ex);. v = (Vdbe
1f2e6 2a 29 70 2d 3e 70 53 74 6d 74 3b 0a 0a 20 20 69 *)p->pStmt;.. i
1f2e7 66 28 20 6e 3c 30 20 7c 7c 20 69 4f 66 66 73 65 f( n<0 || iOffse
1f2e8 74 3c 30 20 7c 7c 20 28 69 4f 66 66 73 65 74 2b t<0 || (iOffset+
1f2e9 6e 29 3e 70 2d 3e 6e 42 79 74 65 20 29 7b 0a 20 n)>p->nByte ){.
1f2ea 20 20 20 2f 2a 20 52 65 71 75 65 73 74 20 69 73 /* Request is
1f2eb 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 52 out of range. R
1f2ec 65 74 75 72 6e 20 61 20 74 72 61 6e 73 69 65 6e eturn a transien
1f2ed 74 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 20 20 t error. */.
1f2ee 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
1f2ef 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 R;. sqlite3Er
1f2f0 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45 ror(db, SQLITE_E
1f2f1 52 52 4f 52 2c 20 30 29 3b 0a 20 20 7d 20 65 6c RROR, 0);. } el
1f2f2 73 65 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 se if( v==0 ){.
1f2f3 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 /* If there i
1f2f4 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 68 s no statement h
1f2f5 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 andle, then the
1f2f6 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 61 73 0a blob-handle has.
1f2f7 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62 ** already b
1f2f8 65 65 6e 20 69 6e 76 61 6c 69 64 61 74 65 64 2e een invalidated.
1f2f9 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 Return SQLITE_A
1f2fa 42 4f 52 54 20 69 6e 20 74 68 69 73 20 63 61 73 BORT in this cas
1f2fb 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 e.. */. rc
1f2fc 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b = SQLITE_ABORT;
1f2fd 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
1f2fe 20 43 61 6c 6c 20 65 69 74 68 65 72 20 42 74 72 Call either Btr
1f2ff 65 65 44 61 74 61 28 29 20 6f 72 20 42 74 72 65 eeData() or Btre
1f300 65 50 75 74 44 61 74 61 28 29 2e 20 49 66 20 53 ePutData(). If S
1f301 51 4c 49 54 45 5f 41 42 4f 52 54 20 69 73 0a 20 QLITE_ABORT is.
1f302 20 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 ** returned,
1f303 63 6c 65 61 6e 2d 75 70 20 74 68 65 20 73 74 61 clean-up the sta
1f304 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 20 tement handle..
1f305 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
1f306 28 20 64 62 20 3d 3d 20 76 2d 3e 64 62 20 29 3b ( db == v->db );
1f307 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1f308 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 2d 3e eEnterCursor(p->
1f309 70 43 73 72 29 3b 0a 20 20 20 20 72 63 20 3d 20 pCsr);. rc =
1f30a 78 43 61 6c 6c 28 70 2d 3e 70 43 73 72 2c 20 69 xCall(p->pCsr, i
1f30b 4f 66 66 73 65 74 2b 70 2d 3e 69 4f 66 66 73 65 Offset+p->iOffse
1f30c 74 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 73 71 t, n, z);. sq
1f30d 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 lite3BtreeLeaveC
1f30e 75 72 73 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a ursor(p->pCsr);.
1f30f 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1f310 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20 20 TE_ABORT ){.
1f311 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e sqlite3VdbeFin
1f312 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 20 20 alize(v);.
1f313 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 p->pStmt = 0;.
1f314 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 }else{. d
1f315 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b b->errCode = rc;
1f316 0a 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 72 . v->rc = r
1f317 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 c;. }. }. r
1f318 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 c = sqlite3ApiEx
1f319 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 it(db, rc);. sq
1f31a 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1f31b 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 e(db->mutex);.
1f31c 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1f31d 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 .** Read data fr
1f31e 6f 6d 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 om a blob handle
1f31f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1f320 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
1f321 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c _read(sqlite3_bl
1f322 6f 62 20 2a 70 42 6c 6f 62 2c 20 76 6f 69 64 20 ob *pBlob, void
1f323 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 *z, int n, int i
1f324 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 75 72 Offset){. retur
1f325 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28 n blobReadWrite(
1f326 70 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66 pBlob, z, n, iOf
1f327 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72 fset, sqlite3Btr
1f328 65 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a eeData);.}../*.*
1f329 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20 * Write data to
1f32a 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a a blob handle..*
1f32b 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1f32c 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 sqlite3_blob_wr
1f32d 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 ite(sqlite3_blob
1f32e 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 20 76 *pBlob, const v
1f32f 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 oid *z, int n, i
1f330 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72 nt iOffset){. r
1f331 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72 eturn blobReadWr
1f332 69 74 65 28 70 42 6c 6f 62 2c 20 28 76 6f 69 64 ite(pBlob, (void
1f333 20 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66 73 65 74 *)z, n, iOffset
1f334 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 , sqlite3BtreePu
1f335 74 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a tData);.}../*.**
1f336 20 51 75 65 72 79 20 61 20 62 6c 6f 62 20 68 61 Query a blob ha
1f337 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 73 69 7a ndle for the siz
1f338 65 20 6f 66 20 74 68 65 20 64 61 74 61 2e 0a 2a e of the data..*
1f339 2a 0a 2a 2a 20 54 68 65 20 49 6e 63 72 62 6c 6f *.** The Incrblo
1f33a 62 2e 6e 42 79 74 65 20 66 69 65 6c 64 20 69 73 b.nByte field is
1f33b 20 66 69 78 65 64 20 66 6f 72 20 74 68 65 20 6c fixed for the l
1f33c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20 49 ifetime of the I
1f33d 6e 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f 20 6e 6f ncrblob.** so no
1f33e 20 6d 75 74 65 78 20 69 73 20 72 65 71 75 69 72 mutex is requir
1f33f 65 64 20 66 6f 72 20 61 63 63 65 73 73 2e 0a 2a ed for access..*
1f340 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1f341 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 sqlite3_blob_by
1f342 74 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 tes(sqlite3_blob
1f343 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63 72 *pBlob){. Incr
1f344 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 blob *p = (Incrb
1f345 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 72 lob *)pBlob;. r
1f346 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 42 79 eturn p ? p->nBy
1f347 74 65 20 3a 20 30 3b 0a 7d 0a 0a 23 65 6e 64 69 te : 0;.}..#endi
1f348 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c f /* #ifndef SQL
1f349 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f ITE_OMIT_INCRBLO
1f34a 42 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a B */../*********
1f34b 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 ***** End of vdb
1f34c 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a eblob.c ********
1f34d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f34e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f34f 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1f350 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
1f351 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a journal.c *****
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 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
1f355 20 41 75 67 75 73 74 20 32 32 0a 2a 2a 0a 2a 2a August 22.**.**
1f356 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1f357 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1f358 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1f359 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1f35a 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1f35b 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1f35c 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1f35d 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1f35e 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1f35f 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1f360 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1f361 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1f362 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1f363 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1f364 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1f365 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1f366 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f36b 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
1f36c 40 28 23 29 20 24 49 64 3a 20 6a 6f 75 72 6e 61 @(#) $Id: journa
1f36d 6c 2e 63 2c 76 20 31 2e 39 20 32 30 30 39 2f 30 l.c,v 1.9 2009/0
1f36e 31 2f 32 30 20 31 37 3a 30 36 3a 32 37 20 64 61 1/20 17:06:27 da
1f36f 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a nielk1977 Exp $.
1f370 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */..#ifdef SQLIT
1f371 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f E_ENABLE_ATOMIC_
1f372 57 52 49 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 WRITE../*.** Thi
1f373 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 s file implement
1f374 73 20 61 20 73 70 65 63 69 61 6c 20 6b 69 6e 64 s a special kind
1f375 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 of sqlite3_file
1f376 20 6f 62 6a 65 63 74 20 75 73 65 64 0a 2a 2a 20 object used.**
1f377 62 79 20 53 51 4c 69 74 65 20 74 6f 20 63 72 65 by SQLite to cre
1f378 61 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ate journal file
1f379 73 20 69 66 20 74 68 65 20 61 74 6f 6d 69 63 2d s if the atomic-
1f37a 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 write optimizati
1f37b 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61 62 6c 65 64 on.** is enabled
1f37c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 73 74 ..**.** The dist
1f37d 69 6e 63 74 69 76 65 20 63 68 61 72 61 63 74 65 inctive characte
1f37e 72 69 73 74 69 63 20 6f 66 20 74 68 69 73 20 73 ristic of this s
1f37f 71 6c 69 74 65 33 5f 66 69 6c 65 20 69 73 20 74 qlite3_file is t
1f380 68 61 74 20 74 68 65 0a 2a 2a 20 61 63 74 75 61 hat the.** actua
1f381 6c 20 6f 6e 20 64 69 73 6b 20 66 69 6c 65 20 69 l on disk file i
1f382 73 20 63 72 65 61 74 65 64 20 6c 61 7a 69 6c 79 s created lazily
1f383 2e 20 57 68 65 6e 20 74 68 65 20 66 69 6c 65 20 . When the file
1f384 69 73 20 63 72 65 61 74 65 64 2c 0a 2a 2a 20 74 is created,.** t
1f385 68 65 20 63 61 6c 6c 65 72 20 73 70 65 63 69 66 he caller specif
1f386 69 65 73 20 61 20 62 75 66 66 65 72 20 73 69 7a ies a buffer siz
1f387 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f e for an in-memo
1f388 72 79 20 62 75 66 66 65 72 20 74 6f 0a 2a 2a 20 ry buffer to.**
1f389 62 65 20 75 73 65 64 20 74 6f 20 73 65 72 76 69 be used to servi
1f38a 63 65 20 72 65 61 64 28 29 20 61 6e 64 20 77 72 ce read() and wr
1f38b 69 74 65 28 29 20 72 65 71 75 65 73 74 73 2e 20 ite() requests.
1f38c 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 0a The actual file.
1f38d 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f ** on disk is no
1f38e 74 20 63 72 65 61 74 65 64 20 6f 72 20 70 6f 70 t created or pop
1f38f 75 6c 61 74 65 64 20 75 6e 74 69 6c 20 65 69 74 ulated until eit
1f390 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 her:.**.** 1)
1f391 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 The in-memory re
1f392 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 67 72 6f presentation gro
1f393 77 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72 ws too large for
1f394 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 0a the allocated .
1f395 2a 2a 20 20 20 20 20 20 62 75 66 66 65 72 2c 20 ** buffer,
1f396 6f 72 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 73 or.** 2) The s
1f397 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 qlite3JournalCre
1f398 61 74 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 ate() function i
1f399 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 0a 0a 0a s called..*/....
1f39a 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72 6e 61 6c 46 /*.** A JournalF
1f39b 69 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 61 20 ile object is a
1f39c 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 subclass of sqli
1f39d 74 65 33 5f 66 69 6c 65 20 75 73 65 64 20 62 79 te3_file used by
1f39e 0a 2a 2a 20 61 73 20 61 6e 20 6f 70 65 6e 20 66 .** as an open f
1f39f 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 6a ile handle for j
1f3a0 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f ournal files..*/
1f3a1 0a 73 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 .struct JournalF
1f3a2 69 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f ile {. sqlite3_
1f3a3 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 io_methods *pMet
1f3a4 68 6f 64 3b 20 20 20 20 2f 2a 20 49 2f 4f 20 6d hod; /* I/O m
1f3a5 65 74 68 6f 64 73 20 6f 6e 20 6a 6f 75 72 6e 61 ethods on journa
1f3a6 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 69 6e 74 l files */. int
1f3a7 20 6e 42 75 66 3b 20 20 20 20 20 20 20 20 20 20 nBuf;
1f3a8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f3a9 53 69 7a 65 20 6f 66 20 7a 42 75 66 5b 5d 20 69 Size of zBuf[] i
1f3aa 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 n bytes */. cha
1f3ab 72 20 2a 7a 42 75 66 3b 20 20 20 20 20 20 20 20 r *zBuf;
1f3ac 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f3ad 53 70 61 63 65 20 74 6f 20 62 75 66 66 65 72 20 Space to buffer
1f3ae 6a 6f 75 72 6e 61 6c 20 77 72 69 74 65 73 20 2a journal writes *
1f3af 2f 0a 20 20 69 6e 74 20 69 53 69 7a 65 3b 20 20 /. int iSize;
1f3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f3b1 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 /* Amount of
1f3b2 20 7a 42 75 66 5b 5d 20 63 75 72 72 65 6e 74 6c zBuf[] currentl
1f3b3 79 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20 y used */. int
1f3b4 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
1f3b5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1f3b6 4f 70 65 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 20 Open flags */.
1f3b7 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
1f3b8 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
1f3b9 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20 75 6e /* The "real" un
1f3ba 64 65 72 6c 79 69 6e 67 20 56 46 53 20 2a 2f 0a derlying VFS */.
1f3bb 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
1f3bc 70 52 65 61 6c 3b 20 20 20 20 20 20 20 20 20 20 pReal;
1f3bd 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20 /* The "real"
1f3be 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 underlying file
1f3bf 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 descriptor */.
1f3c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4a 6f 75 const char *zJou
1f3c1 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 rnal;
1f3c2 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a /* Name of the j
1f3c3 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 7d ournal file */.}
1f3c4 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
1f3c5 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 4a 6f 75 JournalFile Jou
1f3c6 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f 2a 0a 2a 2a rnalFile;../*.**
1f3c7 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 If it does not
1f3c8 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2c 20 already exists,
1f3c9 63 72 65 61 74 65 20 61 6e 64 20 70 6f 70 75 6c create and popul
1f3ca 61 74 65 20 74 68 65 20 6f 6e 2d 64 69 73 6b 20 ate the on-disk
1f3cb 66 69 6c 65 20 0a 2a 2a 20 66 6f 72 20 4a 6f 75 file .** for Jou
1f3cc 72 6e 61 6c 46 69 6c 65 20 70 2e 0a 2a 2f 0a 73 rnalFile p..*/.s
1f3cd 74 61 74 69 63 20 69 6e 74 20 63 72 65 61 74 65 tatic int create
1f3ce 46 69 6c 65 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 File(JournalFile
1f3cf 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d *p){. int rc =
1f3d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
1f3d1 28 20 21 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 ( !p->pReal ){.
1f3d2 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 sqlite3_file
1f3d3 2a 70 52 65 61 6c 20 3d 20 28 73 71 6c 69 74 65 *pReal = (sqlite
1f3d4 33 5f 66 69 6c 65 20 2a 29 26 70 5b 31 5d 3b 0a 3_file *)&p[1];.
1f3d5 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1f3d6 4f 73 4f 70 65 6e 28 70 2d 3e 70 56 66 73 2c 20 OsOpen(p->pVfs,
1f3d7 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 52 65 p->zJournal, pRe
1f3d8 61 6c 2c 20 70 2d 3e 66 6c 61 67 73 2c 20 30 29 al, p->flags, 0)
1f3d9 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
1f3da 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1f3db 20 70 2d 3e 70 52 65 61 6c 20 3d 20 70 52 65 61 p->pReal = pRea
1f3dc 6c 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e l;. if( p->
1f3dd 69 53 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 iSize>0 ){.
1f3de 20 20 20 61 73 73 65 72 74 28 70 2d 3e 69 53 69 assert(p->iSi
1f3df 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29 3b 0a 20 20 ze<=p->nBuf);.
1f3e0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1f3e1 65 33 4f 73 57 72 69 74 65 28 70 2d 3e 70 52 65 e3OsWrite(p->pRe
1f3e2 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c 20 70 2d 3e al, p->zBuf, p->
1f3e3 69 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20 iSize, 0);.
1f3e4 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 }. }. }. r
1f3e5 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1f3e6 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c ** Close the fil
1f3e7 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1f3e8 20 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74 jrnlClose(sqlit
1f3e9 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a e3_file *pJfd){.
1f3ea 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 JournalFile *p
1f3eb 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 = (JournalFile
1f3ec 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d *)pJfd;. if( p-
1f3ed 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 73 71 >pReal ){. sq
1f3ee 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 2d 3e lite3OsClose(p->
1f3ef 70 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 pReal);. }. sq
1f3f0 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 42 lite3_free(p->zB
1f3f1 75 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 uf);. return SQ
1f3f2 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1f3f3 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d * Read data from
1f3f4 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 the file..*/.st
1f3f5 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 52 65 61 atic int jrnlRea
1f3f6 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c d(. sqlite3_fil
1f3f7 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 e *pJfd, /* T
1f3f8 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1f3f9 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 72 65 from which to re
1f3fa 61 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42 ad */. void *zB
1f3fb 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f uf, /
1f3fc 2a 20 50 75 74 20 74 68 65 20 72 65 73 75 6c 74 * Put the result
1f3fd 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 s here */. int
1f3fe 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 iAmt,
1f3ff 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1f400 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f bytes to read */
1f401 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 . sqlite_int64
1f402 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 iOfst /* Beg
1f403 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 in reading at th
1f404 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a is offset */.){.
1f405 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1f406 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 E_OK;. JournalF
1f407 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 ile *p = (Journa
1f408 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 lFile *)pJfd;.
1f409 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a if( p->pReal ){.
1f40a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1f40b 4f 73 52 65 61 64 28 70 2d 3e 70 52 65 61 6c 2c OsRead(p->pReal,
1f40c 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66 zBuf, iAmt, iOf
1f40d 73 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 st);. }else if(
1f40e 20 28 69 41 6d 74 2b 69 4f 66 73 74 29 3e 70 2d (iAmt+iOfst)>p-
1f40f 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63 >iSize ){. rc
1f410 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
1f411 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 65 SHORT_READ;. }e
1f412 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 lse{. memcpy(
1f413 7a 42 75 66 2c 20 26 70 2d 3e 7a 42 75 66 5b 69 zBuf, &p->zBuf[i
1f414 4f 66 73 74 5d 2c 20 69 41 6d 74 29 3b 0a 20 20 Ofst], iAmt);.
1f415 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1f416 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 ../*.** Write da
1f417 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a ta to the file..
1f418 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 */.static int jr
1f419 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 nlWrite(. sqlit
1f41a 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 e3_file *pJfd,
1f41b 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c /* The journal
1f41c 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 file into which
1f41d 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63 to write */. c
1f41e 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c onst void *zBuf,
1f41f 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61 /* Take da
1f420 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e ta to be written
1f421 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20 from here */.
1f422 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 int iAmt,
1f423 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1f424 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 of bytes to wri
1f425 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 te */. sqlite_i
1f426 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f nt64 iOfst /
1f427 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20 * Begin writing
1f428 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69 at this offset i
1f429 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a nto the file */.
1f42a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1f42b 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e LITE_OK;. Journ
1f42c 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 alFile *p = (Jou
1f42d 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b rnalFile *)pJfd;
1f42e 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c . if( !p->pReal
1f42f 20 26 26 20 28 69 4f 66 73 74 2b 69 41 6d 74 29 && (iOfst+iAmt)
1f430 3e 70 2d 3e 6e 42 75 66 20 29 7b 0a 20 20 20 20 >p->nBuf ){.
1f431 72 63 20 3d 20 63 72 65 61 74 65 46 69 6c 65 28 rc = createFile(
1f432 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 p);. }. if( rc
1f433 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1f434 20 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 if( p->pReal
1f435 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
1f436 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d 3e lite3OsWrite(p->
1f437 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d pReal, zBuf, iAm
1f438 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 20 20 7d t, iOfst);. }
1f439 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 else{. memc
1f43a 70 79 28 26 70 2d 3e 7a 42 75 66 5b 69 4f 66 73 py(&p->zBuf[iOfs
1f43b 74 5d 2c 20 7a 42 75 66 2c 20 69 41 6d 74 29 3b t], zBuf, iAmt);
1f43c 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 53 . if( p->iS
1f43d 69 7a 65 3c 28 69 4f 66 73 74 2b 69 41 6d 74 29 ize<(iOfst+iAmt)
1f43e 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 ){. p->i
1f43f 53 69 7a 65 20 3d 20 28 69 4f 66 73 74 2b 69 41 Size = (iOfst+iA
1f440 6d 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 mt);. }.
1f441 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1f442 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 rc;.}../*.** Tru
1f443 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e 0a ncate the file..
1f444 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 */.static int jr
1f445 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 nlTruncate(sqlit
1f446 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 e3_file *pJfd, s
1f447 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65 qlite_int64 size
1f448 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1f449 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e LITE_OK;. Journ
1f44a 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 alFile *p = (Jou
1f44b 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b rnalFile *)pJfd;
1f44c 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 . if( p->pReal
1f44d 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
1f44e 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 2d te3OsTruncate(p-
1f44f 3e 70 52 65 61 6c 2c 20 73 69 7a 65 29 3b 0a 20 >pReal, size);.
1f450 20 7d 65 6c 73 65 20 69 66 28 20 73 69 7a 65 3c }else if( size<
1f451 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 p->iSize ){.
1f452 70 2d 3e 69 53 69 7a 65 20 3d 20 73 69 7a 65 3b p->iSize = size;
1f453 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1f454 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 ;.}../*.** Sync
1f455 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 the file..*/.sta
1f456 74 69 63 20 69 6e 74 20 6a 72 6e 6c 53 79 6e 63 tic int jrnlSync
1f457 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
1f458 4a 66 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b Jfd, int flags){
1f459 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4a 6f 75 . int rc;. Jou
1f45a 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a rnalFile *p = (J
1f45b 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 ournalFile *)pJf
1f45c 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 d;. if( p->pRea
1f45d 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 l ){. rc = sq
1f45e 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 2d 3e 70 lite3OsSync(p->p
1f45f 52 65 61 6c 2c 20 66 6c 61 67 73 29 3b 0a 20 20 Real, flags);.
1f460 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
1f461 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
1f462 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1f463 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73 *.** Query the s
1f464 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ize of the file
1f465 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 in bytes..*/.sta
1f466 74 69 63 20 69 6e 74 20 6a 72 6e 6c 46 69 6c 65 tic int jrnlFile
1f467 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c Size(sqlite3_fil
1f468 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f e *pJfd, sqlite_
1f469 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 int64 *pSize){.
1f46a 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1f46b 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 _OK;. JournalFi
1f46c 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c le *p = (Journal
1f46d 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 File *)pJfd;. i
1f46e 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 f( p->pReal ){.
1f46f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
1f470 73 46 69 6c 65 53 69 7a 65 28 70 2d 3e 70 52 65 sFileSize(p->pRe
1f471 61 6c 2c 20 70 53 69 7a 65 29 3b 0a 20 20 7d 65 al, pSize);. }e
1f472 6c 73 65 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 lse{. *pSize
1f473 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 = (sqlite_int64)
1f474 20 70 2d 3e 69 53 69 7a 65 3b 0a 20 20 7d 0a 20 p->iSize;. }.
1f475 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1f476 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 *.** Table of me
1f477 74 68 6f 64 73 20 66 6f 72 20 4a 6f 75 72 6e 61 thods for Journa
1f478 6c 46 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 69 lFile sqlite3_fi
1f479 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 le object..*/.st
1f47a 61 74 69 63 20 73 74 72 75 63 74 20 73 71 6c 69 atic struct sqli
1f47b 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4a te3_io_methods J
1f47c 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 ournalFileMethod
1f47d 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 s = {. 1,
1f47e 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 /* iVersi
1f47f 6f 6e 20 2a 2f 0a 20 20 6a 72 6e 6c 43 6c 6f 73 on */. jrnlClos
1f480 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 e, /* xClose
1f481 20 2a 2f 0a 20 20 6a 72 6e 6c 52 65 61 64 2c 20 */. jrnlRead,
1f482 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f /* xRead */
1f483 0a 20 20 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20 . jrnlWrite,
1f484 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20 /* xWrite */.
1f485 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20 20 jrnlTruncate,
1f486 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a /* xTruncate */.
1f487 20 20 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 jrnlSync,
1f488 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6a /* xSync */. j
1f489 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f 2a rnlFileSize, /*
1f48a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 20 xFileSize */.
1f48b 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0, /
1f48c 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 * xLock */. 0,
1f48d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1f48e 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 Unlock */. 0,
1f48f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 /* xC
1f490 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
1f491 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
1f492 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e /* xFileCon
1f493 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20 trol */. 0,
1f494 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 /* xSec
1f495 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20 torSize */. 0
1f496 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1f497 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
1f498 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 stics */.};../*
1f499 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e .** Open a journ
1f49a 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 al file..*/.SQLI
1f49b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1f49c 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 qlite3JournalOpe
1f49d 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 n(. sqlite3_vfs
1f49e 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 *pVfs,
1f49f 2f 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75 73 /* The VFS to us
1f4a0 65 20 66 6f 72 20 61 63 74 75 61 6c 20 66 69 6c e for actual fil
1f4a1 65 20 49 2f 4f 20 2a 2f 0a 20 20 63 6f 6e 73 74 e I/O */. const
1f4a2 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 char *zName,
1f4a3 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
1f4a4 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1f4a5 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 e */. sqlite3_f
1f4a6 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 20 20 ile *pJfd,
1f4a7 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65 /* Preallocate
1f4a8 64 2c 20 62 6c 61 6e 6b 20 66 69 6c 65 20 68 61 d, blank file ha
1f4a9 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c ndle */. int fl
1f4aa 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 ags,
1f4ab 20 20 20 20 20 2f 2a 20 4f 70 65 6e 69 6e 67 20 /* Opening
1f4ac 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 6e flags */. int n
1f4ad 42 75 66 20 20 20 20 20 20 20 20 20 20 20 20 20 Buf
1f4ae 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 62 /* Bytes b
1f4af 75 66 66 65 72 65 64 20 62 65 66 6f 72 65 20 6f uffered before o
1f4b0 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20 pening the file
1f4b1 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 */.){. JournalF
1f4b2 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 ile *p = (Journa
1f4b3 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 lFile *)pJfd;.
1f4b4 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71 6c memset(p, 0, sql
1f4b5 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 ite3JournalSize(
1f4b6 70 56 66 73 29 29 3b 0a 20 20 69 66 28 20 6e 42 pVfs));. if( nB
1f4b7 75 66 3e 30 20 29 7b 0a 20 20 20 20 70 2d 3e 7a uf>0 ){. p->z
1f4b8 42 75 66 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c Buf = sqlite3Mal
1f4b9 6c 6f 63 5a 65 72 6f 28 6e 42 75 66 29 3b 0a 20 locZero(nBuf);.
1f4ba 20 20 20 69 66 28 20 21 70 2d 3e 7a 42 75 66 20 if( !p->zBuf
1f4bb 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1f4bc 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
1f4bd 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
1f4be 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f return sqlite3O
1f4bf 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e 61 6d sOpen(pVfs, zNam
1f4c0 65 2c 20 70 4a 66 64 2c 20 66 6c 61 67 73 2c 20 e, pJfd, flags,
1f4c1 30 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4d 65 0);. }. p->pMe
1f4c2 74 68 6f 64 20 3d 20 26 4a 6f 75 72 6e 61 6c 46 thod = &JournalF
1f4c3 69 6c 65 4d 65 74 68 6f 64 73 3b 0a 20 20 70 2d ileMethods;. p-
1f4c4 3e 6e 42 75 66 20 3d 20 6e 42 75 66 3b 0a 20 20 >nBuf = nBuf;.
1f4c5 70 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 73 p->flags = flags
1f4c6 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 ;. p->zJournal
1f4c7 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 2d 3e 70 56 = zName;. p->pV
1f4c8 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 72 65 74 fs = pVfs;. ret
1f4c9 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1f4ca 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 ../*.** If the a
1f4cb 72 67 75 6d 65 6e 74 20 70 20 70 6f 69 6e 74 73 rgument p points
1f4cc 20 74 6f 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c to a JournalFil
1f4cd 65 20 73 74 72 75 63 74 75 72 65 2c 20 61 6e 64 e structure, and
1f4ce 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a the underlying.
1f4cf 2a 2a 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 ** file has not
1f4d0 79 65 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 yet been created
1f4d1 2c 20 63 72 65 61 74 65 20 69 74 20 6e 6f 77 2e , create it now.
1f4d2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1f4d3 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f TE int sqlite3Jo
1f4d4 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c 69 urnalCreate(sqli
1f4d5 74 65 33 5f 66 69 6c 65 20 2a 70 29 7b 0a 20 20 te3_file *p){.
1f4d6 69 66 28 20 70 2d 3e 70 4d 65 74 68 6f 64 73 21 if( p->pMethods!
1f4d7 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 =&JournalFileMet
1f4d8 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 65 74 75 hods ){. retu
1f4d9 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
1f4da 7d 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 74 }. return creat
1f4db 65 46 69 6c 65 28 28 4a 6f 75 72 6e 61 6c 46 69 eFile((JournalFi
1f4dc 6c 65 20 2a 29 70 29 3b 0a 7d 0a 0a 2f 2a 20 0a le *)p);.}../* .
1f4dd 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
1f4de 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 mber of bytes re
1f4df 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 quired to store
1f4e0 61 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 74 68 a JournalFile th
1f4e1 61 74 20 75 73 65 73 20 76 66 73 0a 2a 2a 20 70 at uses vfs.** p
1f4e2 56 66 73 20 74 6f 20 63 72 65 61 74 65 20 74 68 Vfs to create th
1f4e3 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 6e 2d e underlying on-
1f4e4 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 disk files..*/.S
1f4e5 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1f4e6 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c t sqlite3Journal
1f4e7 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73 Size(sqlite3_vfs
1f4e8 20 2a 70 56 66 73 29 7b 0a 20 20 72 65 74 75 72 *pVfs){. retur
1f4e9 6e 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c n (pVfs->szOsFil
1f4ea 65 2b 73 69 7a 65 6f 66 28 4a 6f 75 72 6e 61 6c e+sizeof(Journal
1f4eb 46 69 6c 65 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 File));.}.#endif
1f4ec 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
1f4ed 2a 20 45 6e 64 20 6f 66 20 6a 6f 75 72 6e 61 6c * End of journal
1f4ee 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1f4ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f4f1 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
1f4f2 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d * Begin file mem
1f4f3 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a journal.c ******
1f4f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f4f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f4f6 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 /./*.** 2008 Oct
1f4f7 6f 62 65 72 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 ober 7.**.** The
1f4f8 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
1f4f9 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
1f4fa 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
1f4fb 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
1f4fc 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
1f4fd 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
1f4fe 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
1f4ff 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
1f500 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
1f501 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
1f502 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
1f503 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
1f504 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
1f505 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
1f506 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
1f507 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
1f508 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
1f509 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f50a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f50b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f50c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f50d 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
1f50e 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
1f50f 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c 65 ode use to imple
1f510 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 ment an in-memor
1f511 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e y rollback journ
1f512 61 6c 2e 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 al..** The in-me
1f513 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f mory rollback jo
1f514 75 72 6e 61 6c 20 69 73 20 75 73 65 64 20 74 6f urnal is used to
1f515 20 6a 6f 75 72 6e 61 6c 20 74 72 61 6e 73 61 63 journal transac
1f516 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20 22 3a 6d tions for.** ":m
1f517 65 6d 6f 72 79 3a 22 20 64 61 74 61 62 61 73 65 emory:" database
1f518 73 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20 6a s and when the j
1f519 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4d 45 4d 4f ournal_mode=MEMO
1f51a 52 59 20 70 72 61 67 6d 61 20 69 73 20 75 73 65 RY pragma is use
1f51b 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 d..**.** @(#) $I
1f51c 64 3a 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 2c d: memjournal.c,
1f51d 76 20 31 2e 31 32 20 32 30 30 39 2f 30 35 2f 30 v 1.12 2009/05/0
1f51e 34 20 31 31 3a 34 32 3a 33 30 20 64 61 6e 69 65 4 11:42:30 danie
1f51f 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
1f520 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 ./* Forward refe
1f521 72 65 6e 63 65 73 20 74 6f 20 69 6e 74 65 72 6e rences to intern
1f522 61 6c 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f al structures */
1f523 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
1f524 4d 65 6d 4a 6f 75 72 6e 61 6c 20 4d 65 6d 4a 6f MemJournal MemJo
1f525 75 72 6e 61 6c 3b 0a 74 79 70 65 64 65 66 20 73 urnal;.typedef s
1f526 74 72 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 20 truct FilePoint
1f527 46 69 6c 65 50 6f 69 6e 74 3b 0a 74 79 70 65 64 FilePoint;.typed
1f528 65 66 20 73 74 72 75 63 74 20 46 69 6c 65 43 68 ef struct FileCh
1f529 75 6e 6b 20 46 69 6c 65 43 68 75 6e 6b 3b 0a 0a unk FileChunk;..
1f52a 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 /* Space to hold
1f52b 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
1f52c 75 72 6e 61 6c 20 69 73 20 61 6c 6c 6f 63 61 74 urnal is allocat
1f52d 65 64 20 69 6e 20 69 6e 63 72 65 6d 65 6e 74 73 ed in increments
1f52e 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6d 61 6e 79 of.** this many
1f52f 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 bytes..**.** Th
1f530 65 20 73 69 7a 65 20 63 68 6f 73 65 6e 20 69 73 e size chosen is
1f531 20 61 20 6c 69 74 74 6c 65 20 6c 65 73 73 20 74 a little less t
1f532 68 61 6e 20 61 20 70 6f 77 65 72 20 6f 66 20 74 han a power of t
1f533 77 6f 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 2a wo. That way,.*
1f534 2a 20 74 68 65 20 46 69 6c 65 43 68 75 6e 6b 20 * the FileChunk
1f535 6f 62 6a 65 63 74 20 77 69 6c 6c 20 68 61 76 65 object will have
1f536 20 61 20 73 69 7a 65 20 74 68 61 74 20 61 6c 6d a size that alm
1f537 6f 73 74 20 65 78 61 63 74 6c 79 20 66 69 6c 6c ost exactly fill
1f538 73 0a 2a 2a 20 61 20 70 6f 77 65 72 2d 6f 66 2d s.** a power-of-
1f539 74 77 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 two allocation.
1f53a 20 54 68 69 73 20 6d 69 6d 69 6d 69 7a 65 73 20 This mimimizes
1f53b 77 61 73 74 65 64 20 73 70 61 63 65 20 69 6e 20 wasted space in
1f53c 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 0a 2a 2a 20 power-of-two.**
1f53d 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
1f53e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f s..*/.#define JO
1f53f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 URNAL_CHUNKSIZE
1f540 28 28 69 6e 74 29 28 31 30 32 34 2d 73 69 7a 65 ((int)(1024-size
1f541 6f 66 28 46 69 6c 65 43 68 75 6e 6b 2a 29 29 29 of(FileChunk*)))
1f542 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69 ../* Macro to fi
1f543 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f nd the minimum o
1f544 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 61 f two numeric va
1f545 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 lues..*/.#ifndef
1f546 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49 MIN.# define MI
1f547 4e 28 78 2c 79 29 20 28 28 78 29 3c 28 79 29 3f N(x,y) ((x)<(y)?
1f548 28 78 29 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a (x):(y)).#endif.
1f549 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 ./*.** The rollb
1f54a 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 63 ack journal is c
1f54b 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20 6c 69 6e omposed of a lin
1f54c 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73 ked list of thes
1f54d 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f e structures..*/
1f54e 0a 73 74 72 75 63 74 20 46 69 6c 65 43 68 75 6e .struct FileChun
1f54f 6b 20 7b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 k {. FileChunk
1f550 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 *pNext;
1f551 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 /* Next ch
1f552 75 6e 6b 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e unk in the journ
1f553 61 6c 20 2a 2f 0a 20 20 75 38 20 7a 43 68 75 6e al */. u8 zChun
1f554 6b 5b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 k[JOURNAL_CHUNKS
1f555 49 5a 45 5d 3b 20 20 20 2f 2a 20 43 6f 6e 74 65 IZE]; /* Conte
1f556 6e 74 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b nt of this chunk
1f557 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e */.};../*.** An
1f558 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
1f559 73 20 6f 62 6a 65 63 74 20 73 65 72 76 65 73 20 s object serves
1f55a 61 73 20 61 20 63 75 72 73 6f 72 20 69 6e 74 6f as a cursor into
1f55b 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
1f55c 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 63 75 urnal..** The cu
1f55d 72 73 6f 72 20 63 61 6e 20 62 65 20 65 69 74 68 rsor can be eith
1f55e 65 72 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f er for reading o
1f55f 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a 73 74 r writing..*/.st
1f560 72 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 20 7b ruct FilePoint {
1f561 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
1f562 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 iOffset;
1f563 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f /* Offset fro
1f564 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 m the beginning
1f565 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 of the file */.
1f566 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 FileChunk *pChu
1f567 6e 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nk;
1f568 20 2f 2a 20 53 70 65 63 69 66 69 63 20 63 68 75 /* Specific chu
1f569 6e 6b 20 69 6e 74 6f 20 77 68 69 63 68 20 63 75 nk into which cu
1f56a 72 73 6f 72 20 70 6f 69 6e 74 73 20 2a 2f 0a 7d rsor points */.}
1f56b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 75 ;../*.** This su
1f56c 62 63 6c 61 73 73 20 69 73 20 61 20 73 75 62 63 bclass is a subc
1f56d 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f lass of sqlite3_
1f56e 66 69 6c 65 2e 20 20 45 61 63 68 20 6f 70 65 6e file. Each open
1f56f 20 6d 65 6d 6f 72 79 2d 6a 6f 75 72 6e 61 6c 0a memory-journal.
1f570 2a 2a 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 ** is an instanc
1f571 65 20 6f 66 20 74 68 69 73 20 63 6c 61 73 73 2e e of this class.
1f572 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 4a 6f .*/.struct MemJo
1f573 75 72 6e 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 urnal {. sqlite
1f574 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 3_io_methods *pM
1f575 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 50 61 72 ethod; /* Par
1f576 65 6e 74 20 63 6c 61 73 73 2e 20 4d 55 53 54 20 ent class. MUST
1f577 42 45 20 46 49 52 53 54 20 2a 2f 0a 20 20 46 69 BE FIRST */. Fi
1f578 6c 65 43 68 75 6e 6b 20 2a 70 46 69 72 73 74 3b leChunk *pFirst;
1f579 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1f57a 20 48 65 61 64 20 6f 66 20 69 6e 2d 6d 65 6d 6f Head of in-memo
1f57b 72 79 20 63 68 75 6e 6b 2d 6c 69 73 74 20 2a 2f ry chunk-list */
1f57c 0a 20 20 46 69 6c 65 50 6f 69 6e 74 20 65 6e 64 . FilePoint end
1f57d 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 point;
1f57e 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1f57f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
1f580 66 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65 50 6f file */. FilePo
1f581 69 6e 74 20 72 65 61 64 70 6f 69 6e 74 3b 20 20 int readpoint;
1f582 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
1f583 6e 74 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20 nter to the end
1f584 6f 66 20 74 68 65 20 6c 61 73 74 20 78 52 65 61 of the last xRea
1f585 64 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a d() */.};../*.**
1f586 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 Read data from
1f587 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f the in-memory jo
1f588 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 54 68 69 urnal file. Thi
1f589 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 s is the impleme
1f58a 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 ntation.** of th
1f58b 65 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 52 e sqlite3_vfs.xR
1f58c 65 61 64 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 ead method..*/.s
1f58d 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e tatic int memjrn
1f58e 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 lRead(. sqlite3
1f58f 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 _file *pJfd,
1f590 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 /* The journal f
1f591 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74 ile from which t
1f592 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 o read */. void
1f593 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20 *zBuf,
1f594 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65 /* Put the re
1f595 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 sults here */.
1f596 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 int iAmt,
1f597 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1f598 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 of bytes to rea
1f599 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e d */. sqlite_in
1f59a 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a t64 iOfst /*
1f59b 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 Begin reading a
1f59c 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f t this offset */
1f59d 0a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c .){. MemJournal
1f59e 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 *p = (MemJourna
1f59f 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 75 38 20 2a l *)pJfd;. u8 *
1f5a0 7a 4f 75 74 20 3d 20 7a 42 75 66 3b 0a 20 20 69 zOut = zBuf;. i
1f5a1 6e 74 20 6e 52 65 61 64 20 3d 20 69 41 6d 74 3b nt nRead = iAmt;
1f5a2 0a 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66 . int iChunkOff
1f5a3 73 65 74 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b set;. FileChunk
1f5a4 20 2a 70 43 68 75 6e 6b 3b 0a 0a 20 20 2f 2a 20 *pChunk;.. /*
1f5a5 53 51 4c 69 74 65 20 6e 65 76 65 72 20 74 72 69 SQLite never tri
1f5a6 65 73 20 74 6f 20 72 65 61 64 20 70 61 73 74 20 es to read past
1f5a7 74 68 65 20 65 6e 64 20 6f 66 20 61 20 72 6f 6c the end of a rol
1f5a8 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 lback journal fi
1f5a9 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 le */. assert(
1f5aa 69 4f 66 73 74 2b 69 41 6d 74 3c 3d 70 2d 3e 65 iOfst+iAmt<=p->e
1f5ab 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 ndpoint.iOffset
1f5ac 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 72 65 61 );.. if( p->rea
1f5ad 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 21 3d dpoint.iOffset!=
1f5ae 69 4f 66 73 74 20 7c 7c 20 69 4f 66 73 74 3d 3d iOfst || iOfst==
1f5af 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
1f5b0 5f 69 6e 74 36 34 20 69 4f 66 66 20 3d 20 30 3b _int64 iOff = 0;
1f5b1 0a 20 20 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d . for(pChunk=
1f5b2 70 2d 3e 70 46 69 72 73 74 3b 20 0a 20 20 20 20 p->pFirst; .
1f5b3 20 20 20 20 41 4c 57 41 59 53 28 70 43 68 75 6e ALWAYS(pChun
1f5b4 6b 29 20 26 26 20 28 69 4f 66 66 2b 4a 4f 55 52 k) && (iOff+JOUR
1f5b5 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3c 3d NAL_CHUNKSIZE)<=
1f5b6 69 4f 66 73 74 3b 0a 20 20 20 20 20 20 20 20 70 iOfst;. p
1f5b7 43 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e Chunk=pChunk->pN
1f5b8 65 78 74 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 ext. ){.
1f5b9 20 69 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c iOff += JOURNAL
1f5ba 5f 43 48 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20 _CHUNKSIZE;.
1f5bb 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 }. }else{. p
1f5bc 43 68 75 6e 6b 20 3d 20 70 2d 3e 72 65 61 64 70 Chunk = p->readp
1f5bd 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 7d oint.pChunk;. }
1f5be 0a 0a 20 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 .. iChunkOffset
1f5bf 20 3d 20 28 69 6e 74 29 28 69 4f 66 73 74 25 4a = (int)(iOfst%J
1f5c0 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 OURNAL_CHUNKSIZE
1f5c1 29 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 6e );. do {. in
1f5c2 74 20 69 53 70 61 63 65 20 3d 20 4a 4f 55 52 4e t iSpace = JOURN
1f5c3 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 AL_CHUNKSIZE - i
1f5c4 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 20 ChunkOffset;.
1f5c5 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 4e int nCopy = MIN
1f5c6 28 6e 52 65 61 64 2c 20 28 4a 4f 55 52 4e 41 4c (nRead, (JOURNAL
1f5c7 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 _CHUNKSIZE - iCh
1f5c8 75 6e 6b 4f 66 66 73 65 74 29 29 3b 0a 20 20 20 unkOffset));.
1f5c9 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c 20 26 70 memcpy(zOut, &p
1f5ca 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43 Chunk->zChunk[iC
1f5cb 68 75 6e 6b 4f 66 66 73 65 74 5d 2c 20 6e 43 6f hunkOffset], nCo
1f5cc 70 79 29 3b 0a 20 20 20 20 7a 4f 75 74 20 2b 3d py);. zOut +=
1f5cd 20 6e 43 6f 70 79 3b 0a 20 20 20 20 6e 52 65 61 nCopy;. nRea
1f5ce 64 20 2d 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 d -= iSpace;.
1f5cf 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 iChunkOffset =
1f5d0 30 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 52 0;. } while( nR
1f5d1 65 61 64 3e 3d 30 20 26 26 20 28 70 43 68 75 6e ead>=0 && (pChun
1f5d2 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 29 k=pChunk->pNext)
1f5d3 21 3d 30 20 26 26 20 6e 52 65 61 64 3e 30 20 29 !=0 && nRead>0 )
1f5d4 3b 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 ;. p->readpoint
1f5d5 2e 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 73 74 .iOffset = iOfst
1f5d6 2b 69 41 6d 74 3b 0a 20 20 70 2d 3e 72 65 61 64 +iAmt;. p->read
1f5d7 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 point.pChunk = p
1f5d8 43 68 75 6e 6b 3b 0a 0a 20 20 72 65 74 75 72 6e Chunk;.. return
1f5d9 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
1f5da 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 *.** Write data
1f5db 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a to the file..*/.
1f5dc 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 static int memjr
1f5dd 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 nlWrite(. sqlit
1f5de 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 e3_file *pJfd,
1f5df 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c /* The journal
1f5e0 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 file into which
1f5e1 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63 to write */. c
1f5e2 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c onst void *zBuf,
1f5e3 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61 /* Take da
1f5e4 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e ta to be written
1f5e5 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20 from here */.
1f5e6 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 int iAmt,
1f5e7 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1f5e8 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 of bytes to wri
1f5e9 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 te */. sqlite_i
1f5ea 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f nt64 iOfst /
1f5eb 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20 * Begin writing
1f5ec 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69 at this offset i
1f5ed 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a nto the file */.
1f5ee 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 ){. MemJournal
1f5ef 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c *p = (MemJournal
1f5f0 20 2a 29 70 4a 66 64 3b 0a 20 20 69 6e 74 20 6e *)pJfd;. int n
1f5f1 57 72 69 74 65 20 3d 20 69 41 6d 74 3b 0a 20 20 Write = iAmt;.
1f5f2 75 38 20 2a 7a 57 72 69 74 65 20 3d 20 28 75 38 u8 *zWrite = (u8
1f5f3 20 2a 29 7a 42 75 66 3b 0a 0a 20 20 2f 2a 20 41 *)zBuf;.. /* A
1f5f4 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 n in-memory jour
1f5f5 6e 61 6c 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 nal file should
1f5f6 6f 6e 6c 79 20 65 76 65 72 20 62 65 20 61 70 70 only ever be app
1f5f7 65 6e 64 65 64 20 74 6f 2e 20 52 61 6e 64 6f 6d ended to. Random
1f5f8 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 77 72 69 . ** access wri
1f5f9 74 65 73 20 61 72 65 20 6e 6f 74 20 72 65 71 75 tes are not requ
1f5fa 69 72 65 64 20 62 79 20 73 71 6c 69 74 65 2e 0a ired by sqlite..
1f5fb 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 */. assert( i
1f5fc 4f 66 73 74 3d 3d 70 2d 3e 65 6e 64 70 6f 69 6e Ofst==p->endpoin
1f5fd 74 2e 69 4f 66 66 73 65 74 20 29 3b 0a 20 20 55 t.iOffset );. U
1f5fe 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
1f5ff 69 4f 66 73 74 29 3b 0a 0a 20 20 77 68 69 6c 65 iOfst);.. while
1f600 28 20 6e 57 72 69 74 65 3e 30 20 29 7b 0a 20 20 ( nWrite>0 ){.
1f601 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 FileChunk *pCh
1f602 75 6e 6b 20 3d 20 70 2d 3e 65 6e 64 70 6f 69 6e unk = p->endpoin
1f603 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 20 20 69 6e t.pChunk;. in
1f604 74 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d t iChunkOffset =
1f605 20 28 69 6e 74 29 28 70 2d 3e 65 6e 64 70 6f 69 (int)(p->endpoi
1f606 6e 74 2e 69 4f 66 66 73 65 74 25 4a 4f 55 52 4e nt.iOffset%JOURN
1f607 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 AL_CHUNKSIZE);.
1f608 20 20 20 69 6e 74 20 69 53 70 61 63 65 20 3d 20 int iSpace =
1f609 4d 49 4e 28 6e 57 72 69 74 65 2c 20 4a 4f 55 52 MIN(nWrite, JOUR
1f60a 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 NAL_CHUNKSIZE -
1f60b 69 43 68 75 6e 6b 4f 66 66 73 65 74 29 3b 0a 0a iChunkOffset);..
1f60c 20 20 20 20 69 66 28 20 69 43 68 75 6e 6b 4f 66 if( iChunkOf
1f60d 66 73 65 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 fset==0 ){.
1f60e 20 2f 2a 20 4e 65 77 20 63 68 75 6e 6b 20 69 73 /* New chunk is
1f60f 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 78 74 required to ext
1f610 65 6e 64 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f end the file. */
1f611 0a 20 20 20 20 20 20 46 69 6c 65 43 68 75 6e 6b . FileChunk
1f612 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 *pNew = sqlite3
1f613 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 _malloc(sizeof(F
1f614 69 6c 65 43 68 75 6e 6b 29 29 3b 0a 20 20 20 20 ileChunk));.
1f615 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 if( !pNew ){.
1f616 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
1f617 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d LITE_IOERR_NOMEM
1f618 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1f619 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 30 3b pNew->pNext = 0;
1f61a 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 75 6e . if( pChun
1f61b 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 k ){. ass
1f61c 65 72 74 28 20 70 2d 3e 70 46 69 72 73 74 20 29 ert( p->pFirst )
1f61d 3b 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b ;. pChunk
1f61e 2d 3e 70 4e 65 78 74 20 3d 20 70 4e 65 77 3b 0a ->pNext = pNew;.
1f61f 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1f620 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 2d assert( !p-
1f621 3e 70 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20 >pFirst );.
1f622 20 20 20 70 2d 3e 70 46 69 72 73 74 20 3d 20 70 p->pFirst = p
1f623 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 New;. }.
1f624 20 20 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 p->endpoint.p
1f625 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 Chunk = pNew;.
1f626 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 }.. memcpy(
1f627 26 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 &p->endpoint.pCh
1f628 75 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 unk->zChunk[iChu
1f629 6e 6b 4f 66 66 73 65 74 5d 2c 20 7a 57 72 69 74 nkOffset], zWrit
1f62a 65 2c 20 69 53 70 61 63 65 29 3b 0a 20 20 20 20 e, iSpace);.
1f62b 7a 57 72 69 74 65 20 2b 3d 20 69 53 70 61 63 65 zWrite += iSpace
1f62c 3b 0a 20 20 20 20 6e 57 72 69 74 65 20 2d 3d 20 ;. nWrite -=
1f62d 69 53 70 61 63 65 3b 0a 20 20 20 20 70 2d 3e 65 iSpace;. p->e
1f62e 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 ndpoint.iOffset
1f62f 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20 7d 0a 0a += iSpace;. }..
1f630 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1f631 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 OK;.}../*.** Tru
1f632 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e 0a ncate the file..
1f633 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 */.static int me
1f634 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 mjrnlTruncate(sq
1f635 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 lite3_file *pJfd
1f636 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 , sqlite_int64 s
1f637 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e ize){. MemJourn
1f638 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 al *p = (MemJour
1f639 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 46 69 nal *)pJfd;. Fi
1f63a 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b leChunk *pChunk;
1f63b 0a 20 20 61 73 73 65 72 74 28 73 69 7a 65 3d 3d . assert(size==
1f63c 30 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 0);. UNUSED_PAR
1f63d 41 4d 45 54 45 52 28 73 69 7a 65 29 3b 0a 20 20 AMETER(size);.
1f63e 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 46 69 72 pChunk = p->pFir
1f63f 73 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 43 68 st;. while( pCh
1f640 75 6e 6b 20 29 7b 0a 20 20 20 20 46 69 6c 65 43 unk ){. FileC
1f641 68 75 6e 6b 20 2a 70 54 6d 70 20 3d 20 70 43 68 hunk *pTmp = pCh
1f642 75 6e 6b 3b 0a 20 20 20 20 70 43 68 75 6e 6b 20 unk;. pChunk
1f643 3d 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 3b = pChunk->pNext;
1f644 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
1f645 65 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 20 20 73 e(pTmp);. }. s
1f646 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c qlite3MemJournal
1f647 4f 70 65 6e 28 70 4a 66 64 29 3b 0a 20 20 72 65 Open(pJfd);. re
1f648 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1f649 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 }../*.** Close t
1f64a 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 he file..*/.stat
1f64b 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 43 6c ic int memjrnlCl
1f64c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ose(sqlite3_file
1f64d 20 2a 70 4a 66 64 29 7b 0a 20 20 6d 65 6d 6a 72 *pJfd){. memjr
1f64e 6e 6c 54 72 75 6e 63 61 74 65 28 70 4a 66 64 2c nlTruncate(pJfd,
1f64f 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 0);. return SQ
1f650 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a LITE_OK;.}.../*.
1f651 2a 2a 20 53 79 6e 63 20 74 68 65 20 66 69 6c 65 ** Sync the file
1f652 2e 0a 2a 2a 0a 2a 2a 20 53 79 6e 63 69 6e 67 20 ..**.** Syncing
1f653 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 an in-memory jou
1f654 72 6e 61 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e rnal is a no-op.
1f655 20 20 41 6e 64 2c 20 69 6e 20 66 61 63 74 2c 20 And, in fact,
1f656 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
1f657 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 is never called
1f658 69 6e 20 61 20 77 6f 72 6b 69 6e 67 20 69 6d 70 in a working imp
1f659 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 lementation. Th
1f65a 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f is implementatio
1f65b 6e 0a 2a 2a 20 65 78 69 73 74 73 20 70 75 72 65 n.** exists pure
1f65c 6c 79 20 61 73 20 61 20 63 6f 6e 74 69 6e 67 65 ly as a continge
1f65d 6e 63 79 2c 20 69 6e 20 63 61 73 65 20 73 6f 6d ncy, in case som
1f65e 65 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 20 69 6e e malfunction in
1f65f 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 some other.** p
1f660 61 72 74 20 6f 66 20 53 51 4c 69 74 65 20 63 61 art of SQLite ca
1f661 75 73 65 73 20 53 79 6e 63 20 74 6f 20 62 65 20 uses Sync to be
1f662 63 61 6c 6c 65 64 20 62 79 20 6d 69 73 74 61 6b called by mistak
1f663 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1f664 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 28 73 71 6c memjrnlSync(sql
1f665 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 ite3_file *NotUs
1f666 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 ed, int NotUsed2
1f667 29 7b 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f ){ /*NO_TEST*/
1f668 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
1f669 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
1f66a 74 55 73 65 64 32 29 3b 20 20 20 20 20 20 20 20 tUsed2);
1f66b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f66c 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 61 73 /*NO_TEST*/. as
1f66d 73 65 72 74 28 20 30 20 29 3b 20 20 20 20 20 20 sert( 0 );
1f66e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f66f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f670 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f /*NO_
1f671 54 45 53 54 2a 2f 0a 20 20 72 65 74 75 72 6e 20 TEST*/. return
1f672 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
1f673 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f674 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f675 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a /*NO_TEST*
1f676 2f 0a 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 /.}
1f677 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f678 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f679 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f67a 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 0a 2f 2a /*NO_TEST*/../*
1f67b 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73 69 .** Query the si
1f67c 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 ze of the file i
1f67d 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 n bytes..*/.stat
1f67e 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 46 69 ic int memjrnlFi
1f67f 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 leSize(sqlite3_f
1f680 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 ile *pJfd, sqlit
1f681 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b e_int64 *pSize){
1f682 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 . MemJournal *p
1f683 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a = (MemJournal *
1f684 29 70 4a 66 64 3b 0a 20 20 2a 70 53 69 7a 65 20 )pJfd;. *pSize
1f685 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 = (sqlite_int64)
1f686 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 p->endpoint.iOf
1f687 66 73 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 53 fset;. return S
1f688 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
1f689 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 74 68 ** Table of meth
1f68a 6f 64 73 20 66 6f 72 20 4d 65 6d 4a 6f 75 72 6e ods for MemJourn
1f68b 61 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 al sqlite3_file
1f68c 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 object..*/.stati
1f68d 63 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 c struct sqlite3
1f68e 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 65 6d 4a _io_methods MemJ
1f68f 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 20 3d 20 ournalMethods =
1f690 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 {. 1,
1f691 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f /* iVersio
1f692 6e 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 43 6c n */. memjrnlCl
1f693 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f ose, /* xClo
1f694 73 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 52 se */. memjrnlR
1f695 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 ead, /* xRe
1f696 61 64 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 57 ad */. memjrnlW
1f697 72 69 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 rite, /* xWr
1f698 69 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c ite */. memjrnl
1f699 54 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54 Truncate, /* xT
1f69a 72 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6d 65 6d runcate */. mem
1f69b 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 20 2f jrnlSync, /
1f69c 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6d 65 6d * xSync */. mem
1f69d 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f jrnlFileSize, /
1f69e 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 * xFileSize */.
1f69f 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1f6a0 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 /* xLock */.
1f6a1 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1f6a2 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f /* xUnlock */
1f6a3 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 . 0,
1f6a4 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 /* xCheckRe
1f6a5 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 servedLock */.
1f6a6 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1f6a7 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f /* xFileContro
1f6a8 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 l */. 0,
1f6a9 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 /* xSec
1f6aa 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20 torSize */. 0
1f6ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1f6ac 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74 * xDeviceCharact
1f6ad 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a eristics */.};..
1f6ae 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f /* .** Open a jo
1f6af 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 urnal file..*/.S
1f6b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1f6b1 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 id sqlite3MemJou
1f6b2 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 rnalOpen(sqlite3
1f6b3 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 _file *pJfd){.
1f6b4 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 MemJournal *p =
1f6b5 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a (MemJournal *)pJ
1f6b6 66 64 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 fd;. assert( EI
1f6b7 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
1f6b8 4e 54 28 70 29 20 29 3b 0a 20 20 6d 65 6d 73 65 NT(p) );. memse
1f6b9 74 28 70 2c 20 30 2c 20 73 71 6c 69 74 65 33 4d t(p, 0, sqlite3M
1f6ba 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 29 emJournalSize())
1f6bb 3b 0a 20 20 70 2d 3e 70 4d 65 74 68 6f 64 20 3d ;. p->pMethod =
1f6bc 20 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 &MemJournalMeth
1f6bd 6f 64 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ods;.}../*.** Re
1f6be 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 turn true if the
1f6bf 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 70 61 73 file-handle pas
1f6c0 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 sed as an argume
1f6c1 6e 74 20 69 73 20 0a 2a 2a 20 61 6e 20 69 6e 2d nt is .** an in-
1f6c2 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 0a memory journal .
1f6c3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1f6c4 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d E int sqlite3IsM
1f6c5 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 emJournal(sqlite
1f6c6 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 3_file *pJfd){.
1f6c7 20 72 65 74 75 72 6e 20 70 4a 66 64 2d 3e 70 4d return pJfd->pM
1f6c8 65 74 68 6f 64 73 3d 3d 26 4d 65 6d 4a 6f 75 72 ethods==&MemJour
1f6c9 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f nalMethods;.}../
1f6ca 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 * .** Return the
1f6cb 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
1f6cc 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f required to sto
1f6cd 72 65 20 61 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 re a MemJournal
1f6ce 74 68 61 74 20 75 73 65 73 20 76 66 73 0a 2a 2a that uses vfs.**
1f6cf 20 70 56 66 73 20 74 6f 20 63 72 65 61 74 65 20 pVfs to create
1f6d0 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f the underlying o
1f6d1 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f n-disk files..*/
1f6d2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f6d3 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f int sqlite3MemJo
1f6d4 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 29 7b urnalSize(void){
1f6d5 0a 20 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 . return sizeof
1f6d6 28 4d 65 6d 4a 6f 75 72 6e 61 6c 29 3b 0a 7d 0a (MemJournal);.}.
1f6d7 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1f6d8 20 45 6e 64 20 6f 66 20 6d 65 6d 6a 6f 75 72 6e End of memjourn
1f6d9 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a al.c ***********
1f6da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f6db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1f6dc 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1f6dd 20 42 65 67 69 6e 20 66 69 6c 65 20 77 61 6c 6b Begin file walk
1f6de 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a er.c ***********
1f6df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1f6e1 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 ./*.** 2008 Augu
1f6e2 73 74 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 16.**.** The
1f6e3 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
1f6e4 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
1f6e5 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
1f6e6 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
1f6e7 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
1f6e8 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
1f6e9 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
1f6ea 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
1f6eb 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
1f6ec 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
1f6ed 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
1f6ee 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
1f6ef 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
1f6f0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
1f6f1 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
1f6f2 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
1f6f3 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
1f6f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f6f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f6f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f6f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f6f8 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
1f6f9 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69 e contains routi
1f6fa 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61 6c nes used for wal
1f6fb 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72 20 king the parser
1f6fc 74 72 65 65 20 66 6f 72 0a 2a 2a 20 61 6e 20 53 tree for.** an S
1f6fd 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a QL statement..**
1f6fe 0a 2a 2a 20 24 49 64 3a 20 77 61 6c 6b 65 72 2e .** $Id: walker.
1f6ff 63 2c 76 20 31 2e 37 20 32 30 30 39 2f 30 36 2f c,v 1.7 2009/06/
1f700 31 35 20 32 33 3a 31 35 3a 35 39 20 64 72 68 20 15 23:15:59 drh
1f701 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a Exp $.*/.../*.**
1f702 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 Walk an express
1f703 69 6f 6e 20 74 72 65 65 2e 20 20 49 6e 76 6f 6b ion tree. Invok
1f704 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f e the callback o
1f705 6e 63 65 20 66 6f 72 20 65 61 63 68 20 6e 6f 64 nce for each nod
1f706 65 0a 2a 2a 20 6f 66 20 74 68 65 20 65 78 70 72 e.** of the expr
1f707 65 73 73 69 6f 6e 2c 20 77 68 69 6c 65 20 64 65 ession, while de
1f708 63 65 6e 64 69 6e 67 2e 20 20 28 49 6e 20 6f 74 cending. (In ot
1f709 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 63 her words, the c
1f70a 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 69 6e allback.** is in
1f70b 76 6f 6b 65 64 20 62 65 66 6f 72 65 20 76 69 73 voked before vis
1f70c 69 74 69 6e 67 20 63 68 69 6c 64 72 65 6e 2e 29 iting children.)
1f70d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 .**.** The retur
1f70e 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 n value from the
1f70f 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 callback should
1f710 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 57 be one of the W
1f711 52 43 5f 2a 0a 2a 2a 20 63 6f 6e 73 74 61 6e 74 RC_*.** constant
1f712 73 20 74 6f 20 73 70 65 63 69 66 79 20 68 6f 77 s to specify how
1f713 20 74 6f 20 70 72 6f 63 65 65 64 20 77 69 74 68 to proceed with
1f714 20 74 68 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a the walk..**.**
1f715 20 20 20 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 WRC_Continue
1f716 20 20 20 20 20 20 43 6f 6e 74 69 6e 75 65 20 64 Continue d
1f717 65 73 63 65 6e 64 69 6e 67 20 64 6f 77 6e 20 74 escending down t
1f718 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 20 he tree..**.**
1f719 20 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 WRC_Prune
1f71a 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 73 63 65 Do not desce
1f71b 6e 64 20 69 6e 74 6f 20 63 68 69 6c 64 20 6e 6f nd into child no
1f71c 64 65 73 2e 20 20 42 75 74 20 61 6c 6c 6f 77 0a des. But allow.
1f71d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
1f71e 20 20 20 20 20 20 20 20 74 68 65 20 77 61 6c 6b the walk
1f71f 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 to continue wit
1f720 68 20 73 69 62 6c 69 6e 67 20 6e 6f 64 65 73 2e h sibling nodes.
1f721 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 41 62 .**.** WRC_Ab
1f722 6f 72 74 20 20 20 20 20 20 20 20 20 44 6f 20 6e ort Do n
1f723 6f 20 6d 6f 72 65 20 63 61 6c 6c 62 61 63 6b 73 o more callbacks
1f724 2e 20 20 55 6e 77 69 6e 64 20 74 68 65 20 73 74 . Unwind the st
1f725 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 ack and.**
1f726 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f727 72 65 74 75 72 6e 20 74 68 65 20 74 6f 70 2d 6c return the top-l
1f728 65 76 65 6c 20 77 61 6c 6b 20 63 61 6c 6c 2e 0a evel walk call..
1f729 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e **.** The return
1f72a 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 69 73 value from this
1f72b 20 72 6f 75 74 69 6e 65 20 69 73 20 57 52 43 5f routine is WRC_
1f72c 41 62 6f 72 74 20 74 6f 20 61 62 61 6e 64 6f 6e Abort to abandon
1f72d 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b 0a 2a the tree walk.*
1f72e 2a 20 61 6e 64 20 57 52 43 5f 43 6f 6e 74 69 6e * and WRC_Contin
1f72f 75 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 2e 0a ue to continue..
1f730 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1f731 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c E int sqlite3Wal
1f732 6b 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 kExpr(Walker *pW
1f733 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 alker, Expr *pEx
1f734 70 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 pr){. int rc;.
1f735 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 if( pExpr==0 )
1f736 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 return WRC_Conti
1f737 6e 75 65 3b 0a 20 20 74 65 73 74 63 61 73 65 28 nue;. testcase(
1f738 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
1f739 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e (pExpr, EP_Token
1f73a 4f 6e 6c 79 29 20 29 3b 0a 20 20 74 65 73 74 63 Only) );. testc
1f73b 61 73 65 28 20 45 78 70 72 48 61 73 50 72 6f 70 ase( ExprHasProp
1f73c 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52 erty(pExpr, EP_R
1f73d 65 64 75 63 65 64 29 20 29 3b 0a 20 20 72 63 20 educed) );. rc
1f73e 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 45 78 70 72 = pWalker->xExpr
1f73f 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72 Callback(pWalker
1f740 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20 , pExpr);. if(
1f741 72 63 3d 3d 57 52 43 5f 43 6f 6e 74 69 6e 75 65 rc==WRC_Continue
1f742 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 . &
1f743 26 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f & !ExprHasAnyPro
1f744 70 65 72 74 79 28 70 45 78 70 72 2c 45 50 5f 54 perty(pExpr,EP_T
1f745 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 okenOnly) ){.
1f746 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1f747 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 45 Expr(pWalker, pE
1f748 78 70 72 2d 3e 70 4c 65 66 74 29 20 29 20 72 65 xpr->pLeft) ) re
1f749 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1f74a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 if( sqlite3W
1f74b 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c alkExpr(pWalker,
1f74c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 20 pExpr->pRight)
1f74d 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1f74e 72 74 3b 0a 20 20 20 20 69 66 28 20 45 78 70 72 rt;. if( Expr
1f74f 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
1f750 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 r, EP_xIsSelect)
1f751 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 ){. if( sq
1f752 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 lite3WalkSelect(
1f753 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e pWalker, pExpr->
1f754 78 2e 70 53 65 6c 65 63 74 29 20 29 20 72 65 74 x.pSelect) ) ret
1f755 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1f756 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1f757 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 if( sqlite3WalkE
1f758 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c xprList(pWalker,
1f759 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 29 pExpr->x.pList)
1f75a 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1f75b 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ort;. }. }.
1f75c 20 72 65 74 75 72 6e 20 72 63 20 26 20 57 52 43 return rc & WRC
1f75d 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a _Abort;.}../*.**
1f75e 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61 6c Call sqlite3Wal
1f75f 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 65 72 kExpr() for ever
1f760 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 y expression in
1f761 6c 69 73 74 20 70 20 6f 72 20 75 6e 74 69 6c 0a list p or until.
1f762 2a 2a 20 61 6e 20 61 62 6f 72 74 20 72 65 71 75 ** an abort requ
1f763 65 73 74 20 69 73 20 73 65 65 6e 2e 0a 2a 2f 0a est is seen..*/.
1f764 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1f765 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 nt sqlite3WalkEx
1f766 70 72 4c 69 73 74 28 57 61 6c 6b 65 72 20 2a 70 prList(Walker *p
1f767 57 61 6c 6b 65 72 2c 20 45 78 70 72 4c 69 73 74 Walker, ExprList
1f768 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 *p){. int i;.
1f769 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
1f76a 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 _item *pItem;.
1f76b 69 66 28 20 70 20 29 7b 0a 20 20 20 20 66 6f 72 if( p ){. for
1f76c 28 69 3d 70 2d 3e 6e 45 78 70 72 2c 20 70 49 74 (i=p->nExpr, pIt
1f76d 65 6d 3d 70 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d em=p->a; i>0; i-
1f76e 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 -, pItem++){.
1f76f 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 if( sqlite3Wa
1f770 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 lkExpr(pWalker,
1f771 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 20 pItem->pExpr) )
1f772 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1f773 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
1f774 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 turn WRC_Continu
1f775 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b e;.}../*.** Walk
1f776 20 61 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e 73 all expressions
1f777 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
1f778 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1f779 74 20 70 2e 20 20 44 6f 0a 2a 2a 20 6e 6f 74 20 t p. Do.** not
1f77a 69 6e 76 6f 6b 65 20 74 68 65 20 53 45 4c 45 43 invoke the SELEC
1f77b 54 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 70 2c T callback on p,
1f77c 20 62 75 74 20 64 6f 20 28 6f 66 20 63 6f 75 72 but do (of cour
1f77d 73 65 29 20 69 6e 76 6f 6b 65 0a 2a 2a 20 61 6e se) invoke.** an
1f77e 79 20 65 78 70 72 20 63 61 6c 6c 62 61 63 6b 73 y expr callbacks
1f77f 20 61 6e 64 20 53 45 4c 45 43 54 20 63 61 6c 6c and SELECT call
1f780 62 61 63 6b 73 20 74 68 61 74 20 63 6f 6d 65 20 backs that come
1f781 66 72 6f 6d 20 73 75 62 71 75 65 72 69 65 73 2e from subqueries.
1f782 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 43 5f 41 .** Return WRC_A
1f783 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 6f 6e 74 bort or WRC_Cont
1f784 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f inue..*/.SQLITE_
1f785 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1f786 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 te3WalkSelectExp
1f787 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 r(Walker *pWalke
1f788 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 r, Select *p){.
1f789 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1f78a 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 ExprList(pWalker
1f78b 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20 29 20 72 , p->pEList) ) r
1f78c 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1f78d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 . if( sqlite3Wa
1f78e 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 lkExpr(pWalker,
1f78f 70 2d 3e 70 57 68 65 72 65 29 20 29 20 72 65 74 p->pWhere) ) ret
1f790 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1f791 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1f792 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 ExprList(pWalker
1f793 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 29 20 29 , p->pGroupBy) )
1f794 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1f795 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 t;. if( sqlite3
1f796 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 WalkExpr(pWalker
1f797 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 20 29 20 , p->pHaving) )
1f798 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1f799 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 ;. if( sqlite3W
1f79a 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c alkExprList(pWal
1f79b 6b 65 72 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 ker, p->pOrderBy
1f79c 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1f79d 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 bort;. if( sqli
1f79e 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c te3WalkExpr(pWal
1f79f 6b 65 72 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 20 ker, p->pLimit)
1f7a0 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1f7a1 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 rt;. if( sqlite
1f7a2 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 3WalkExpr(pWalke
1f7a3 72 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 20 29 r, p->pOffset) )
1f7a4 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1f7a5 74 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f t;. return WRC_
1f7a6 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a Continue;.}../*.
1f7a7 2a 2a 20 57 61 6c 6b 20 74 68 65 20 70 61 72 73 ** Walk the pars
1f7a8 65 20 74 72 65 65 73 20 61 73 73 6f 63 69 61 74 e trees associat
1f7a9 65 64 20 77 69 74 68 20 61 6c 6c 20 73 75 62 71 ed with all subq
1f7aa 75 65 72 69 65 73 20 69 6e 20 74 68 65 0a 2a 2a ueries in the.**
1f7ab 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 FROM clause of
1f7ac 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
1f7ad 20 70 2e 20 20 44 6f 20 6e 6f 74 20 69 6e 76 6f p. Do not invo
1f7ae 6b 65 20 74 68 65 20 73 65 6c 65 63 74 0a 2a 2a ke the select.**
1f7af 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 70 2c 20 callback on p,
1f7b0 62 75 74 20 64 6f 20 69 6e 76 6f 6b 65 20 69 74 but do invoke it
1f7b1 20 6f 6e 20 65 61 63 68 20 46 52 4f 4d 20 63 6c on each FROM cl
1f7b2 61 75 73 65 20 73 75 62 71 75 65 72 79 0a 2a 2a ause subquery.**
1f7b3 20 61 6e 64 20 6f 6e 20 61 6e 79 20 73 75 62 71 and on any subq
1f7b4 75 65 72 69 65 73 20 66 75 72 74 68 65 72 20 64 ueries further d
1f7b5 6f 77 6e 20 69 6e 20 74 68 65 20 74 72 65 65 2e own in the tree.
1f7b6 20 20 52 65 74 75 72 6e 20 0a 2a 2a 20 57 52 43 Return .** WRC
1f7b7 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 6f _Abort or WRC_Co
1f7b8 6e 74 69 6e 75 65 3b 0a 2a 2f 0a 53 51 4c 49 54 ntinue;.*/.SQLIT
1f7b9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1f7ba 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 lite3WalkSelectF
1f7bb 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c rom(Walker *pWal
1f7bc 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b ker, Select *p){
1f7bd 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 . SrcList *pSrc
1f7be 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 ;. int i;. str
1f7bf 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
1f7c0 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 70 53 72 63 *pItem;.. pSrc
1f7c1 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 69 66 = p->pSrc;. if
1f7c2 28 20 41 4c 57 41 59 53 28 70 53 72 63 29 20 29 ( ALWAYS(pSrc) )
1f7c3 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70 53 72 63 {. for(i=pSrc
1f7c4 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d 3d 70 53 ->nSrc, pItem=pS
1f7c5 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c rc->a; i>0; i--,
1f7c6 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 pItem++){.
1f7c7 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1f7c8 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 Select(pWalker,
1f7c9 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 20 pItem->pSelect)
1f7ca 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
1f7cb 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 n WRC_Abort;.
1f7cc 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
1f7cd 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 return WRC_Cont
1f7ce 69 6e 75 65 3b 0a 7d 20 0a 0a 2f 2a 0a 2a 2a 20 inue;.} ../*.**
1f7cf 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61 6c 6b Call sqlite3Walk
1f7d0 45 78 70 72 28 29 20 66 6f 72 20 65 76 65 72 79 Expr() for every
1f7d1 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 53 expression in S
1f7d2 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 elect statement
1f7d3 70 2e 0a 2a 2a 20 49 6e 76 6f 6b 65 20 73 71 6c p..** Invoke sql
1f7d4 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 29 ite3WalkSelect()
1f7d5 20 66 6f 72 20 73 75 62 71 75 65 72 69 65 73 20 for subqueries
1f7d6 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 in the FROM clau
1f7d7 73 65 20 61 6e 64 0a 2a 2a 20 6f 6e 20 74 68 65 se and.** on the
1f7d8 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 compound select
1f7d9 20 63 68 61 69 6e 2c 20 70 2d 3e 70 50 72 69 6f chain, p->pPrio
1f7da 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 r..**.** Return
1f7db 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 75 6e 64 WRC_Continue und
1f7dc 65 72 20 6e 6f 72 6d 61 6c 20 63 6f 6e 64 69 74 er normal condit
1f7dd 69 6f 6e 73 2e 20 20 52 65 74 75 72 6e 20 57 52 ions. Return WR
1f7de 43 5f 41 62 6f 72 74 20 69 66 0a 2a 2a 20 74 68 C_Abort if.** th
1f7df 65 72 65 20 69 73 20 61 6e 20 61 62 6f 72 74 20 ere is an abort
1f7e0 72 65 71 75 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 request..**.** I
1f7e1 66 20 74 68 65 20 57 61 6c 6b 65 72 20 64 6f 65 f the Walker doe
1f7e2 73 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 78 53 s not have an xS
1f7e3 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 29 20 electCallback()
1f7e4 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e then this routin
1f7e5 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 20 e.** is a no-op
1f7e6 72 65 74 75 72 6e 69 6e 67 20 57 52 43 5f 43 6f returning WRC_Co
1f7e7 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 ntinue..*/.SQLIT
1f7e8 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1f7e9 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 lite3WalkSelect(
1f7ea 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c Walker *pWalker,
1f7eb 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69 Select *p){. i
1f7ec 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 3d 3d nt rc;. if( p==
1f7ed 30 20 7c 7c 20 70 57 61 6c 6b 65 72 2d 3e 78 53 0 || pWalker->xS
1f7ee 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 3d 3d 30 electCallback==0
1f7ef 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f ) return WRC_Co
1f7f0 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20 3d 20 57 ntinue;. rc = W
1f7f1 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 77 RC_Continue;. w
1f7f2 68 69 6c 65 28 20 70 20 20 29 7b 0a 20 20 20 20 hile( p ){.
1f7f3 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 53 rc = pWalker->xS
1f7f4 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 70 57 electCallback(pW
1f7f5 61 6c 6b 65 72 2c 20 70 29 3b 0a 20 20 20 20 69 alker, p);. i
1f7f6 66 28 20 72 63 20 29 20 62 72 65 61 6b 3b 0a 20 f( rc ) break;.
1f7f7 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 if( sqlite3Wa
1f7f8 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 70 57 61 lkSelectExpr(pWa
1f7f9 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75 72 lker, p) ) retur
1f7fa 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 n WRC_Abort;.
1f7fb 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1f7fc 53 65 6c 65 63 74 46 72 6f 6d 28 70 57 61 6c 6b SelectFrom(pWalk
1f7fd 65 72 2c 20 70 29 20 29 20 72 65 74 75 72 6e 20 er, p) ) return
1f7fe 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 70 WRC_Abort;. p
1f7ff 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 = p->pPrior;.
1f800 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 }. return rc &
1f801 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a WRC_Abort;.}../*
1f802 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
1f803 64 20 6f 66 20 77 61 6c 6b 65 72 2e 63 20 2a 2a d of walker.c **
1f804 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f805 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f806 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1f807 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
1f808 67 69 6e 20 66 69 6c 65 20 72 65 73 6f 6c 76 65 gin file resolve
1f809 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1f80a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f80b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1f80c 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 .** 2008 August
1f80d 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 18.**.** The aut
1f80e 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
1f80f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
1f810 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
1f811 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
1f812 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
1f813 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
1f814 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
1f815 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
1f816 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
1f817 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
1f818 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
1f819 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
1f81a 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
1f81b 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
1f81c 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
1f81d 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
1f81e 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
1f81f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f821 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f822 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f823 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
1f824 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69 e contains routi
1f825 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61 6c nes used for wal
1f826 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72 20 king the parser
1f827 74 72 65 65 20 61 6e 64 0a 2a 2a 20 72 65 73 6f tree and.** reso
1f828 6c 76 65 20 61 6c 6c 20 69 64 65 6e 74 69 66 69 lve all identifi
1f829 65 72 73 20 62 79 20 61 73 73 6f 63 69 61 74 69 ers by associati
1f82a 6e 67 20 74 68 65 6d 20 77 69 74 68 20 61 20 70 ng them with a p
1f82b 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 74 61 62 articular.** tab
1f82c 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a le and column..*
1f82d 2a 0a 2a 2a 20 24 49 64 3a 20 72 65 73 6f 6c 76 *.** $Id: resolv
1f82e 65 2e 63 2c 76 20 31 2e 33 30 20 32 30 30 39 2f e.c,v 1.30 2009/
1f82f 30 36 2f 31 35 20 32 33 3a 31 35 3a 35 39 20 64 06/15 23:15:59 d
1f830 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a rh Exp $.*/../*.
1f831 2a 2a 20 54 75 72 6e 20 74 68 65 20 70 45 78 70 ** Turn the pExp
1f832 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 74 r expression int
1f833 6f 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 74 o an alias for t
1f834 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d he iCol-th colum
1f835 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 73 75 n of the.** resu
1f836 6c 74 20 73 65 74 20 69 6e 20 70 45 4c 69 73 74 lt set in pEList
1f837 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 ..**.** If the r
1f838 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e esult set column
1f839 20 69 73 20 61 20 73 69 6d 70 6c 65 20 63 6f 6c is a simple col
1f83a 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 2c 20 74 umn reference, t
1f83b 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
1f83c 0a 2a 2a 20 6d 61 6b 65 73 20 61 6e 20 65 78 61 .** makes an exa
1f83d 63 74 20 63 6f 70 79 2e 20 20 42 75 74 20 66 6f ct copy. But fo
1f83e 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 r any other kind
1f83f 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 of expression,
1f840 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 this.** routine
1f841 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 make a copy of t
1f842 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f he result set co
1f843 6c 75 6d 6e 20 61 73 20 74 68 65 20 61 72 67 75 lumn as the argu
1f844 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 54 ment to the.** T
1f845 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 2e 20 20 K_AS operator.
1f846 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 The TK_AS operat
1f847 6f 72 20 63 61 75 73 65 73 20 74 68 65 20 65 78 or causes the ex
1f848 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 0a 2a pression to be.*
1f849 2a 20 65 76 61 6c 75 61 74 65 64 20 6a 75 73 74 * evaluated just
1f84a 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 6e 20 72 once and then r
1f84b 65 75 73 65 64 20 66 6f 72 20 65 61 63 68 20 61 eused for each a
1f84c 6c 69 61 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 lias..**.** The
1f84d 72 65 61 73 6f 6e 20 66 6f 72 20 73 75 70 70 72 reason for suppr
1f84e 65 73 73 69 6e 67 20 74 68 65 20 54 4b 5f 41 53 essing the TK_AS
1f84f 20 74 65 72 6d 20 77 68 65 6e 20 74 68 65 20 65 term when the e
1f850 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 73 xpression is a s
1f851 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 imple.** column
1f852 72 65 66 65 72 65 6e 63 65 20 69 73 20 73 6f 20 reference is so
1f853 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 that the column
1f854 72 65 66 65 72 65 6e 63 65 20 77 69 6c 6c 20 62 reference will b
1f855 65 20 72 65 63 6f 67 6e 69 7a 65 64 20 61 73 0a e recognized as.
1f856 2a 2a 20 75 73 61 62 6c 65 20 62 79 20 69 6e 64 ** usable by ind
1f857 69 63 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 ices within the
1f858 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f WHERE clause pro
1f859 63 65 73 73 69 6e 67 20 6c 6f 67 69 63 2e 20 0a cessing logic. .
1f85a 2a 2a 0a 2a 2a 20 48 61 63 6b 3a 20 20 54 68 65 **.** Hack: The
1f85b 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 20 TK_AS operator
1f85c 69 73 20 69 6e 68 69 62 69 74 65 64 20 69 66 20 is inhibited if
1f85d 7a 54 79 70 65 5b 30 5d 3d 3d 27 47 27 2e 20 20 zType[0]=='G'.
1f85e 54 68 69 73 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 This means.** th
1f85f 61 74 20 69 6e 20 61 20 47 52 4f 55 50 20 42 59 at in a GROUP BY
1f860 20 63 6c 61 75 73 65 2c 20 74 68 65 20 65 78 70 clause, the exp
1f861 72 65 73 73 69 6f 6e 20 69 73 20 65 76 61 6c 75 ression is evalu
1f862 61 74 65 64 20 74 77 69 63 65 2e 20 20 48 65 6e ated twice. Hen
1f863 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 ce:.**.** SE
1f864 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29 25 35 20 LECT random()%5
1f865 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46 AS x, count(*) F
1f866 52 4f 4d 20 74 61 62 20 47 52 4f 55 50 20 42 59 ROM tab GROUP BY
1f867 20 78 0a 2a 2a 0a 2a 2a 20 49 73 20 65 71 75 69 x.**.** Is equi
1f868 76 61 6c 65 6e 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a valent to:.**.**
1f869 20 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64 SELECT rand
1f86a 6f 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75 om()%5 AS x, cou
1f86b 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47 nt(*) FROM tab G
1f86c 52 4f 55 50 20 42 59 20 72 61 6e 64 6f 6d 28 29 ROUP BY random()
1f86d 25 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 %5.**.** The res
1f86e 75 6c 74 20 6f 66 20 72 61 6e 64 6f 6d 28 29 25 ult of random()%
1f86f 35 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42 5 in the GROUP B
1f870 59 20 63 6c 61 75 73 65 20 69 73 20 70 72 6f 62 Y clause is prob
1f871 61 62 6c 79 20 64 69 66 66 65 72 65 6e 74 0a 2a ably different.*
1f872 2a 20 66 72 6f 6d 20 74 68 65 20 72 65 73 75 6c * from the resul
1f873 74 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d t in the result-
1f874 73 65 74 2e 20 20 57 65 20 6d 69 67 68 74 20 66 set. We might f
1f875 69 78 20 74 68 69 73 20 73 6f 6d 65 64 61 79 2e ix this someday.
1f876 20 20 4f 72 0a 2a 2a 20 74 68 65 6e 20 61 67 61 Or.** then aga
1f877 69 6e 2c 20 77 65 20 6d 69 67 68 74 20 6e 6f 74 in, we might not
1f878 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ....*/.static vo
1f879 69 64 20 72 65 73 6f 6c 76 65 41 6c 69 61 73 28 id resolveAlias(
1f87a 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
1f87b 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 , /* Par
1f87c 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
1f87d 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 ExprList *pELi
1f87e 73 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 72 65 st, /* A re
1f87f 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69 6e sult set */. in
1f880 74 20 69 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 t iCol,
1f881 20 20 20 20 20 2f 2a 20 41 20 63 6f 6c 75 6d 6e /* A column
1f882 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
1f883 65 74 2e 20 20 30 2e 2e 70 45 4c 69 73 74 2d 3e et. 0..pEList->
1f884 6e 45 78 70 72 2d 31 20 2a 2f 0a 20 20 45 78 70 nExpr-1 */. Exp
1f885 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 r *pExpr,
1f886 20 20 20 20 2f 2a 20 54 72 61 6e 73 66 6f 72 6d /* Transform
1f887 20 74 68 69 73 20 69 6e 74 6f 20 61 6e 20 61 6c this into an al
1f888 69 61 73 20 74 6f 20 74 68 65 20 72 65 73 75 6c ias to the resul
1f889 74 20 73 65 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 t set */. const
1f88a 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20 20 char *zType
1f88b 20 20 2f 2a 20 22 47 52 4f 55 50 22 20 6f 72 20 /* "GROUP" or
1f88c 22 4f 52 44 45 52 22 20 6f 72 20 22 22 20 2a 2f "ORDER" or "" */
1f88d 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 4f 72 69 .){. Expr *pOri
1f88e 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 g; /*
1f88f 54 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 The iCol-th colu
1f890 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 mn of the result
1f891 20 73 65 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a set */. Expr *
1f892 70 44 75 70 3b 20 20 20 20 20 20 20 20 20 20 20 pDup;
1f893 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 4f 72 69 /* Copy of pOri
1f894 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a g */. sqlite3 *
1f895 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a db; /*
1f896 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
1f897 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 nnection */.. a
1f898 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20 26 ssert( iCol>=0 &
1f899 26 20 69 43 6f 6c 3c 70 45 4c 69 73 74 2d 3e 6e & iCol<pEList->n
1f89a 45 78 70 72 20 29 3b 0a 20 20 70 4f 72 69 67 20 Expr );. pOrig
1f89b 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c = pEList->a[iCol
1f89c 5d 2e 70 45 78 70 72 3b 0a 20 20 61 73 73 65 72 ].pExpr;. asser
1f89d 74 28 20 70 4f 72 69 67 21 3d 30 20 29 3b 0a 20 t( pOrig!=0 );.
1f89e 20 61 73 73 65 72 74 28 20 70 4f 72 69 67 2d 3e assert( pOrig->
1f89f 66 6c 61 67 73 20 26 20 45 50 5f 52 65 73 6f 6c flags & EP_Resol
1f8a0 76 65 64 20 29 3b 0a 20 20 64 62 20 3d 20 70 50 ved );. db = pP
1f8a1 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 arse->db;. if(
1f8a2 70 4f 72 69 67 2d 3e 6f 70 21 3d 54 4b 5f 43 4f pOrig->op!=TK_CO
1f8a3 4c 55 4d 4e 20 26 26 20 7a 54 79 70 65 5b 30 5d LUMN && zType[0]
1f8a4 21 3d 27 47 27 20 29 7b 0a 20 20 20 20 70 44 75 !='G' ){. pDu
1f8a5 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 p = sqlite3ExprD
1f8a6 75 70 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29 up(db, pOrig, 0)
1f8a7 3b 0a 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c ;. pDup = sql
1f8a8 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 ite3PExpr(pParse
1f8a9 2c 20 54 4b 5f 41 53 2c 20 70 44 75 70 2c 20 30 , TK_AS, pDup, 0
1f8aa 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 44 , 0);. if( pD
1f8ab 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a up==0 ) return;.
1f8ac 20 20 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e if( pEList->
1f8ad 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 3d 3d a[iCol].iAlias==
1f8ae 30 20 29 7b 0a 20 20 20 20 20 20 70 45 4c 69 73 0 ){. pELis
1f8af 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61 t->a[iCol].iAlia
1f8b0 73 20 3d 20 28 75 31 36 29 28 2b 2b 70 50 61 72 s = (u16)(++pPar
1f8b1 73 65 2d 3e 6e 41 6c 69 61 73 29 3b 0a 20 20 20 se->nAlias);.
1f8b2 20 7d 0a 20 20 20 20 70 44 75 70 2d 3e 69 54 61 }. pDup->iTa
1f8b3 62 6c 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b ble = pEList->a[
1f8b4 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 3b 0a 20 20 iCol].iAlias;.
1f8b5 7d 65 6c 73 65 20 69 66 28 20 45 78 70 72 48 61 }else if( ExprHa
1f8b6 73 50 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c sProperty(pOrig,
1f8b7 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 7c 7c EP_IntValue) ||
1f8b8 20 70 4f 72 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e pOrig->u.zToken
1f8b9 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 75 70 20 ==0 ){. pDup
1f8ba 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 = sqlite3ExprDup
1f8bb 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29 3b 0a (db, pOrig, 0);.
1f8bc 20 20 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20 if( pDup==0
1f8bd 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 ) return;. }els
1f8be 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 6f e{. char *zTo
1f8bf 6b 65 6e 20 3d 20 70 4f 72 69 67 2d 3e 75 2e 7a ken = pOrig->u.z
1f8c0 54 6f 6b 65 6e 3b 0a 20 20 20 20 61 73 73 65 72 Token;. asser
1f8c1 74 28 20 7a 54 6f 6b 65 6e 21 3d 30 20 29 3b 0a t( zToken!=0 );.
1f8c2 20 20 20 20 70 4f 72 69 67 2d 3e 75 2e 7a 54 6f pOrig->u.zTo
1f8c3 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 20 70 44 75 ken = 0;. pDu
1f8c4 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 p = sqlite3ExprD
1f8c5 75 70 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29 up(db, pOrig, 0)
1f8c6 3b 0a 20 20 20 20 70 4f 72 69 67 2d 3e 75 2e 7a ;. pOrig->u.z
1f8c7 54 6f 6b 65 6e 20 3d 20 7a 54 6f 6b 65 6e 3b 0a Token = zToken;.
1f8c8 20 20 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20 if( pDup==0
1f8c9 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 61 73 ) return;. as
1f8ca 73 65 72 74 28 20 28 70 44 75 70 2d 3e 66 6c 61 sert( (pDup->fla
1f8cb 67 73 20 26 20 28 45 50 5f 52 65 64 75 63 65 64 gs & (EP_Reduced
1f8cc 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 29 3d |EP_TokenOnly))=
1f8cd 3d 30 20 29 3b 0a 20 20 20 20 70 44 75 70 2d 3e =0 );. pDup->
1f8ce 66 6c 61 67 73 32 20 7c 3d 20 45 50 32 5f 4d 61 flags2 |= EP2_Ma
1f8cf 6c 6c 6f 63 65 64 54 6f 6b 65 6e 3b 0a 20 20 20 llocedToken;.
1f8d0 20 70 44 75 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 pDup->u.zToken
1f8d1 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
1f8d2 70 28 64 62 2c 20 7a 54 6f 6b 65 6e 29 3b 0a 20 p(db, zToken);.
1f8d3 20 7d 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e }. if( pExpr->
1f8d4 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f flags & EP_ExpCo
1f8d5 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 70 44 75 llate ){. pDu
1f8d6 70 2d 3e 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72 p->pColl = pExpr
1f8d7 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 70 44 75 ->pColl;. pDu
1f8d8 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 p->flags |= EP_E
1f8d9 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 7d 0a 20 xpCollate;. }.
1f8da 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 sqlite3ExprClea
1f8db 72 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 r(db, pExpr);.
1f8dc 6d 65 6d 63 70 79 28 70 45 78 70 72 2c 20 70 44 memcpy(pExpr, pD
1f8dd 75 70 2c 20 73 69 7a 65 6f 66 28 2a 70 45 78 70 up, sizeof(*pExp
1f8de 72 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 r));. sqlite3Db
1f8df 46 72 65 65 28 64 62 2c 20 70 44 75 70 29 3b 0a Free(db, pDup);.
1f8e0 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 }../*.** Given t
1f8e1 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c he name of a col
1f8e2 75 6d 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 umn of the form
1f8e3 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 X.Y.Z or Y.Z or
1f8e4 6a 75 73 74 20 5a 2c 20 6c 6f 6f 6b 20 75 70 0a just Z, look up.
1f8e5 2a 2a 20 74 68 61 74 20 6e 61 6d 65 20 69 6e 20 ** that name in
1f8e6 74 68 65 20 73 65 74 20 6f 66 20 73 6f 75 72 63 the set of sourc
1f8e7 65 20 74 61 62 6c 65 73 20 69 6e 20 70 53 72 63 e tables in pSrc
1f8e8 4c 69 73 74 20 61 6e 64 20 6d 61 6b 65 20 74 68 List and make th
1f8e9 65 20 70 45 78 70 72 20 0a 2a 2a 20 65 78 70 72 e pExpr .** expr
1f8ea 65 73 73 69 6f 6e 20 6e 6f 64 65 20 72 65 66 65 ession node refe
1f8eb 72 20 62 61 63 6b 20 74 6f 20 74 68 61 74 20 73 r back to that s
1f8ec 6f 75 72 63 65 20 63 6f 6c 75 6d 6e 2e 20 20 54 ource column. T
1f8ed 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 he following cha
1f8ee 6e 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 nges.** are made
1f8ef 20 74 6f 20 70 45 78 70 72 3a 0a 2a 2a 0a 2a 2a to pExpr:.**.**
1f8f0 20 20 20 20 70 45 78 70 72 2d 3e 69 44 62 20 20 pExpr->iDb
1f8f1 20 20 20 20 20 20 20 20 20 53 65 74 20 74 68 65 Set the
1f8f2 20 69 6e 64 65 78 20 69 6e 20 64 62 2d 3e 61 44 index in db->aD
1f8f3 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74 61 62 b[] of the datab
1f8f4 61 73 65 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 ase X.**
1f8f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f8f6 20 28 65 76 65 6e 20 69 66 20 58 20 69 73 20 69 (even if X is i
1f8f7 6d 70 6c 69 65 64 29 2e 0a 2a 2a 20 20 20 20 70 mplied)..** p
1f8f8 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 20 20 20 Expr->iTable
1f8f9 20 20 20 20 53 65 74 20 74 6f 20 74 68 65 20 63 Set to the c
1f8fa 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 ursor number for
1f8fb 20 74 68 65 20 74 61 62 6c 65 20 6f 62 74 61 69 the table obtai
1f8fc 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ned.**
1f8fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 f
1f8fe 72 6f 6d 20 70 53 72 63 4c 69 73 74 2e 0a 2a 2a rom pSrcList..**
1f8ff 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 pExpr->pTab
1f900 20 20 20 20 20 20 20 20 20 50 6f 69 6e 74 73 20 Points
1f901 74 6f 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 to the Table str
1f902 75 63 74 75 72 65 20 6f 66 20 58 2e 59 20 28 65 ucture of X.Y (e
1f903 76 65 6e 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 ven if.**
1f904 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f905 20 20 58 20 61 6e 64 2f 6f 72 20 59 20 61 72 65 X and/or Y are
1f906 20 69 6d 70 6c 69 65 64 2e 29 0a 2a 2a 20 20 20 implied.).**
1f907 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 pExpr->iColumn
1f908 20 20 20 20 20 20 53 65 74 20 74 6f 20 74 68 65 Set to the
1f909 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 column number w
1f90a 69 74 68 69 6e 20 74 68 65 20 74 61 62 6c 65 2e ithin the table.
1f90b 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 .** pExpr->op
1f90c 20 20 20 20 20 20 20 20 20 20 20 20 53 65 74 20 Set
1f90d 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a to TK_COLUMN..**
1f90e 20 20 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 pExpr->pLeft
1f90f 20 20 20 20 20 20 20 20 20 41 6e 79 20 65 78 70 Any exp
1f910 72 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f 69 ression this poi
1f911 6e 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74 65 nts to is delete
1f912 64 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 70 d.** pExpr->p
1f913 52 69 67 68 74 20 20 20 20 20 20 20 20 41 6e 79 Right Any
1f914 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 69 73 expression this
1f915 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 64 65 points to is de
1f916 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 leted..**.** The
1f917 20 7a 44 62 20 76 61 72 69 61 62 6c 65 20 69 73 zDb variable is
1f918 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1f919 20 64 61 74 61 62 61 73 65 20 28 74 68 65 20 22 database (the "
1f91a 58 22 29 2e 20 20 54 68 69 73 20 76 61 6c 75 65 X"). This value
1f91b 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 20 may be.** NULL
1f91c 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 6e 61 6d meaning that nam
1f91d 65 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d e is of the form
1f91e 20 59 2e 5a 20 6f 72 20 5a 2e 20 20 41 6e 79 20 Y.Z or Z. Any
1f91f 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 62 61 available databa
1f920 73 65 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 se.** can be use
1f921 64 2e 20 20 54 68 65 20 7a 54 61 62 6c 65 20 76 d. The zTable v
1f922 61 72 69 61 62 6c 65 20 69 73 20 74 68 65 20 6e ariable is the n
1f923 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
1f924 20 28 74 68 65 20 22 59 22 29 2e 20 20 54 68 69 (the "Y"). Thi
1f925 73 0a 2a 2a 20 76 61 6c 75 65 20 63 61 6e 20 62 s.** value can b
1f926 65 20 4e 55 4c 4c 20 69 66 20 7a 44 62 20 69 73 e NULL if zDb is
1f927 20 61 6c 73 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 also NULL. If
1f928 7a 54 61 62 6c 65 20 69 73 20 4e 55 4c 4c 20 69 zTable is NULL i
1f929 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 t.** means that
1f92a 74 68 65 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 the form of the
1f92b 6e 61 6d 65 20 69 73 20 5a 20 61 6e 64 20 74 68 name is Z and th
1f92c 61 74 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 at columns from
1f92d 61 6e 79 20 74 61 62 6c 65 0a 2a 2a 20 63 61 6e any table.** can
1f92e 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 be used..**.**
1f92f 49 66 20 74 68 65 20 6e 61 6d 65 20 63 61 6e 6e If the name cann
1f930 6f 74 20 62 65 20 72 65 73 6f 6c 76 65 64 20 75 ot be resolved u
1f931 6e 61 6d 62 69 67 75 6f 75 73 6c 79 2c 20 6c 65 nambiguously, le
1f932 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 ave an error mes
1f933 73 61 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72 73 sage.** in pPars
1f934 65 20 61 6e 64 20 72 65 74 75 72 6e 20 57 52 43 e and return WRC
1f935 5f 41 62 6f 72 74 2e 20 20 52 65 74 75 72 6e 20 _Abort. Return
1f936 57 52 43 5f 50 72 75 6e 65 20 6f 6e 20 73 75 63 WRC_Prune on suc
1f937 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 cess..*/.static
1f938 69 6e 74 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 0a int lookupName(.
1f939 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
1f93a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 /* The pa
1f93b 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f rsing context */
1f93c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1f93d 44 62 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 Db, /* Name
1f93e 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1f93f 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 containing table
1f940 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 , or NULL */. c
1f941 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 2c onst char *zTab,
1f942 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
1f943 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 able containing
1f944 63 6f 6c 75 6d 6e 2c 20 6f 72 20 4e 55 4c 4c 20 column, or NULL
1f945 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1f946 2a 7a 43 6f 6c 2c 20 20 20 20 2f 2a 20 4e 61 6d *zCol, /* Nam
1f947 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e e of the column.
1f948 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 */. NameContex
1f949 74 20 2a 70 4e 43 2c 20 20 20 20 2f 2a 20 54 68 t *pNC, /* Th
1f94a 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 75 e name context u
1f94b 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 sed to resolve t
1f94c 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 45 78 70 he name */. Exp
1f94d 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20 r *pExpr
1f94e 20 20 2f 2a 20 4d 61 6b 65 20 74 68 69 73 20 45 /* Make this E
1f94f 58 50 52 20 6e 6f 64 65 20 70 6f 69 6e 74 20 74 XPR node point t
1f950 6f 20 74 68 65 20 73 65 6c 65 63 74 65 64 20 63 o the selected c
1f951 6f 6c 75 6d 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e olumn */.){. in
1f952 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 t i, j;
1f953 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
1f954 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 ers */. int cnt
1f955 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1f956 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1f957 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67 mber of matching
1f958 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f column names */
1f959 0a 20 20 69 6e 74 20 63 6e 74 54 61 62 20 3d 20 . int cntTab =
1f95a 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1f95b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1f95c 66 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65 f matching table
1f95d 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 73 71 6c 69 names */. sqli
1f95e 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
1f95f 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a ->db; /*
1f960 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
1f961 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 nnection */. st
1f962 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
1f963 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 20 m *pItem;
1f964 2f 2a 20 55 73 65 20 66 6f 72 20 6c 6f 6f 70 69 /* Use for loopi
1f965 6e 67 20 6f 76 65 72 20 70 53 72 63 4c 69 73 74 ng over pSrcList
1f966 20 69 74 65 6d 73 20 2a 2f 0a 20 20 73 74 72 75 items */. stru
1f967 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
1f968 2a 70 4d 61 74 63 68 20 3d 20 30 3b 20 20 2f 2a *pMatch = 0; /*
1f969 20 54 68 65 20 6d 61 74 63 68 69 6e 67 20 70 53 The matching pS
1f96a 72 63 4c 69 73 74 20 69 74 65 6d 20 2a 2f 0a 20 rcList item */.
1f96b 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 54 NameContext *pT
1f96c 6f 70 4e 43 20 3d 20 70 4e 43 3b 20 20 20 20 20 opNC = pNC;
1f96d 20 20 20 2f 2a 20 46 69 72 73 74 20 6e 61 6d 65 /* First name
1f96e 63 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20 6c context in the l
1f96f 69 73 74 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 ist */. Schema
1f970 2a 70 53 63 68 65 6d 61 20 3d 20 30 3b 20 20 20 *pSchema = 0;
1f971 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63 /* Sc
1f972 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78 70 72 hema of the expr
1f973 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 ession */. int
1f974 69 73 54 72 69 67 67 65 72 20 3d 20 30 3b 0a 0a isTrigger = 0;..
1f975 20 20 61 73 73 65 72 74 28 20 70 4e 43 20 29 3b assert( pNC );
1f976 20 20 20 20 20 2f 2a 20 74 68 65 20 6e 61 6d 65 /* the name
1f977 20 63 6f 6e 74 65 78 74 20 63 61 6e 6e 6f 74 20 context cannot
1f978 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 be NULL. */. as
1f979 73 65 72 74 28 20 7a 43 6f 6c 20 29 3b 20 20 20 sert( zCol );
1f97a 20 2f 2a 20 54 68 65 20 5a 20 69 6e 20 58 2e 59 /* The Z in X.Y
1f97b 2e 5a 20 63 61 6e 6e 6f 74 20 62 65 20 4e 55 4c .Z cannot be NUL
1f97c 4c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7e L */. assert( ~
1f97d 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 ExprHasAnyProper
1f97e 74 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b ty(pExpr, EP_Tok
1f97f 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 enOnly|EP_Reduce
1f980 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 d) );.. /* Init
1f981 69 61 6c 69 7a 65 20 74 68 65 20 6e 6f 64 65 20 ialize the node
1f982 74 6f 20 6e 6f 2d 6d 61 74 63 68 20 2a 2f 0a 20 to no-match */.
1f983 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d pExpr->iTable =
1f984 20 2d 31 3b 0a 20 20 70 45 78 70 72 2d 3e 70 54 -1;. pExpr->pT
1f985 61 62 20 3d 20 30 3b 0a 20 20 45 78 70 72 53 65 ab = 0;. ExprSe
1f986 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78 tIrreducible(pEx
1f987 70 72 29 3b 0a 0a 20 20 2f 2a 20 53 74 61 72 74 pr);.. /* Start
1f988 20 61 74 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f at the inner-mo
1f989 73 74 20 63 6f 6e 74 65 78 74 20 61 6e 64 20 6d st context and m
1f98a 6f 76 65 20 6f 75 74 77 61 72 64 20 75 6e 74 69 ove outward unti
1f98b 6c 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 l a match is fou
1f98c 6e 64 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 70 nd */. while( p
1f98d 4e 43 20 26 26 20 63 6e 74 3d 3d 30 20 29 7b 0a NC && cnt==0 ){.
1f98e 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 ExprList *pE
1f98f 4c 69 73 74 3b 0a 20 20 20 20 53 72 63 4c 69 73 List;. SrcLis
1f990 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 4e t *pSrcList = pN
1f991 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 0a 20 20 C->pSrcList;..
1f992 20 20 69 66 28 20 70 53 72 63 4c 69 73 74 20 29 if( pSrcList )
1f993 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c {. for(i=0,
1f994 20 70 49 74 65 6d 3d 70 53 72 63 4c 69 73 74 2d pItem=pSrcList-
1f995 3e 61 3b 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e >a; i<pSrcList->
1f996 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d nSrc; i++, pItem
1f997 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 54 61 62 ++){. Tab
1f998 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20 20 le *pTab;.
1f999 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 int iDb;.
1f99a 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b Column *pCol;
1f99b 0a 20 20 0a 20 20 20 20 20 20 20 20 70 54 61 62 . . pTab
1f99c 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a = pItem->pTab;.
1f99d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1f99e 70 54 61 62 21 3d 30 20 26 26 20 70 54 61 62 2d pTab!=0 && pTab-
1f99f 3e 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 20 >zName!=0 );.
1f9a0 20 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 iDb = sqlit
1f9a1 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
1f9a2 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d db, pTab->pSchem
1f9a3 61 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 a);. asse
1f9a4 72 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e 30 rt( pTab->nCol>0
1f9a5 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 );. if(
1f9a6 7a 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 zTab ){.
1f9a7 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c if( pItem->zAl
1f9a8 69 61 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ias ){.
1f9a9 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d char *zTabNam
1f9aa 65 20 3d 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 e = pItem->zAlia
1f9ab 73 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 s;. i
1f9ac 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d f( sqlite3StrICm
1f9ad 70 28 7a 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 p(zTabName, zTab
1f9ae 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b )!=0 ) continue;
1f9af 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
1f9b0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68 {. ch
1f9b1 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70 ar *zTabName = p
1f9b2 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 Tab->zName;.
1f9b3 20 20 20 20 20 20 20 20 69 66 28 20 4e 45 56 45 if( NEVE
1f9b4 52 28 7a 54 61 62 4e 61 6d 65 3d 3d 30 29 20 7c R(zTabName==0) |
1f9b5 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 | sqlite3StrICmp
1f9b6 28 7a 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 29 (zTabName, zTab)
1f9b7 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 !=0 ){.
1f9b8 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 continue;.
1f9b9 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1f9ba 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 44 62 if( zDb
1f9bb 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 !=0 && sqlite3St
1f9bc 72 49 43 6d 70 28 64 62 2d 3e 61 44 62 5b 69 44 rICmp(db->aDb[iD
1f9bd 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 29 21 3d b].zName, zDb)!=
1f9be 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
1f9bf 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 continue;.
1f9c0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1f9c1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
1f9c2 0a 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d . if( 0==
1f9c3 28 63 6e 74 54 61 62 2b 2b 29 20 29 7b 0a 20 20 (cntTab++) ){.
1f9c4 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 pExpr->i
1f9c5 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 Table = pItem->i
1f9c6 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 20 20 Cursor;.
1f9c7 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 pExpr->pTab =
1f9c8 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 pTab;.
1f9c9 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e pSchema = pTab->
1f9ca 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 pSchema;.
1f9cb 20 20 20 70 4d 61 74 63 68 20 3d 20 70 49 74 65 pMatch = pIte
1f9cc 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 m;. }.
1f9cd 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 43 for(j=0, pC
1f9ce 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 6a ol=pTab->aCol; j
1f9cf 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b <pTab->nCol; j++
1f9d0 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 , pCol++){.
1f9d1 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1f9d2 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e StrICmp(pCol->zN
1f9d3 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b ame, zCol)==0 ){
1f9d4 0a 20 20 20 20 20 20 20 20 20 20 20 20 49 64 4c . IdL
1f9d5 69 73 74 20 2a 70 55 73 69 6e 67 3b 0a 20 20 20 ist *pUsing;.
1f9d6 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a cnt++;.
1f9d7 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 pExp
1f9d8 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 65 r->iTable = pIte
1f9d9 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 m->iCursor;.
1f9da 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 pExpr->p
1f9db 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 Tab = pTab;.
1f9dc 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 20 3d pMatch =
1f9dd 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 pItem;.
1f9de 20 20 20 20 70 53 63 68 65 6d 61 20 3d 20 70 54 pSchema = pT
1f9df 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 ab->pSchema;.
1f9e0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 73 /* Subs
1f9e1 74 69 74 75 74 65 20 74 68 65 20 72 6f 77 69 64 titute the rowid
1f9e2 20 28 63 6f 6c 75 6d 6e 20 2d 31 29 20 66 6f 72 (column -1) for
1f9e3 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 52 49 the INTEGER PRI
1f9e4 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 20 20 MARY KEY */.
1f9e5 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 pExpr->i
1f9e6 43 6f 6c 75 6d 6e 20 3d 20 6a 3d 3d 70 54 61 62 Column = j==pTab
1f9e7 2d 3e 69 50 4b 65 79 20 3f 20 2d 31 20 3a 20 28 ->iPKey ? -1 : (
1f9e8 69 31 36 29 6a 3b 0a 20 20 20 20 20 20 20 20 20 i16)j;.
1f9e9 20 20 20 69 66 28 20 69 3c 70 53 72 63 4c 69 73 if( i<pSrcLis
1f9ea 74 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a 20 20 20 t->nSrc-1 ){.
1f9eb 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 if( p
1f9ec 49 74 65 6d 5b 31 5d 2e 6a 6f 69 6e 74 79 70 65 Item[1].jointype
1f9ed 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20 29 7b & JT_NATURAL ){
1f9ee 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1f9ef 20 2f 2a 20 49 66 20 74 68 69 73 20 6d 61 74 63 /* If this matc
1f9f0 68 20 6f 63 63 75 72 72 65 64 20 69 6e 20 74 68 h occurred in th
1f9f1 65 20 6c 65 66 74 20 74 61 62 6c 65 20 6f 66 20 e left table of
1f9f2 61 20 6e 61 74 75 72 61 6c 20 6a 6f 69 6e 2c 0a a natural join,.
1f9f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f9f4 2a 2a 20 74 68 65 6e 20 73 6b 69 70 20 74 68 65 ** then skip the
1f9f5 20 72 69 67 68 74 20 74 61 62 6c 65 20 74 6f 20 right table to
1f9f6 61 76 6f 69 64 20 61 20 64 75 70 6c 69 63 61 74 avoid a duplicat
1f9f7 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20 20 20 e match */.
1f9f8 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d pItem
1f9f9 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ++;.
1f9fa 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 i++;.
1f9fb 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 }else if(
1f9fc 20 28 70 55 73 69 6e 67 20 3d 20 70 49 74 65 6d (pUsing = pItem
1f9fd 5b 31 5d 2e 70 55 73 69 6e 67 29 21 3d 30 20 29 [1].pUsing)!=0 )
1f9fe 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
1f9ff 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d 61 74 /* If this mat
1fa00 63 68 20 6f 63 63 75 72 73 20 6f 6e 20 61 20 63 ch occurs on a c
1fa01 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 69 6e olumn that is in
1fa02 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 the USING claus
1fa03 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e.
1fa04 20 20 2a 2a 20 6f 66 20 61 20 6a 6f 69 6e 2c 20 ** of a join,
1fa05 73 6b 69 70 20 74 68 65 20 73 65 61 72 63 68 20 skip the search
1fa06 6f 66 20 74 68 65 20 72 69 67 68 74 20 74 61 62 of the right tab
1fa07 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e 0a 20 le of the join.
1fa08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
1fa09 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 64 75 70 * to avoid a dup
1fa0a 6c 69 63 61 74 65 20 6d 61 74 63 68 20 74 68 65 licate match the
1fa0b 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 re. */.
1fa0c 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 int k;.
1fa0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6f fo
1fa0e 72 28 6b 3d 30 3b 20 6b 3c 70 55 73 69 6e 67 2d r(k=0; k<pUsing-
1fa0f 3e 6e 49 64 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 >nId; k++){.
1fa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 if
1fa11 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 ( sqlite3StrICmp
1fa12 28 70 55 73 69 6e 67 2d 3e 61 5b 6b 5d 2e 7a 4e (pUsing->a[k].zN
1fa13 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b ame, zCol)==0 ){
1fa14 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1fa15 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 pItem++;.
1fa16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa17 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 i++;.
1fa18 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b break
1fa19 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
1fa1a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1fa1b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1fa1c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1fa1d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1fa1e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
1fa1f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1fa20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 }. }..#i
1fa21 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1fa22 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 2f 2a T_TRIGGER. /*
1fa23 20 49 66 20 77 65 20 68 61 76 65 20 6e 6f 74 20 If we have not
1fa24 61 6c 72 65 61 64 79 20 72 65 73 6f 6c 76 65 64 already resolved
1fa25 20 74 68 65 20 6e 61 6d 65 2c 20 74 68 65 6e 20 the name, then
1fa26 6d 61 79 62 65 20 0a 20 20 20 20 2a 2a 20 69 74 maybe . ** it
1fa27 20 69 73 20 61 20 6e 65 77 2e 2a 20 6f 72 20 6f is a new.* or o
1fa28 6c 64 2e 2a 20 74 72 69 67 67 65 72 20 61 72 67 ld.* trigger arg
1fa29 75 6d 65 6e 74 20 72 65 66 65 72 65 6e 63 65 0a ument reference.
1fa2a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a */. if( z
1fa2b 44 62 3d 3d 30 20 26 26 20 7a 54 61 62 21 3d 30 Db==0 && zTab!=0
1fa2c 20 26 26 20 63 6e 74 3d 3d 30 20 26 26 20 70 50 && cnt==0 && pP
1fa2d 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 arse->pTriggerTa
1fa2e 62 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e b!=0 ){. in
1fa2f 74 20 6f 70 20 3d 20 70 50 61 72 73 65 2d 3e 65 t op = pParse->e
1fa30 54 72 69 67 67 65 72 4f 70 3b 0a 20 20 20 20 20 TriggerOp;.
1fa31 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 Table *pTab = 0
1fa32 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1fa33 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54 45 20 7c 7c op==TK_DELETE ||
1fa34 20 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45 20 7c op==TK_UPDATE |
1fa35 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52 54 20 | op==TK_INSERT
1fa36 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 21 );. if( op!
1fa37 3d 54 4b 5f 44 45 4c 45 54 45 20 26 26 20 73 71 =TK_DELETE && sq
1fa38 6c 69 74 65 33 53 74 72 49 43 6d 70 28 22 6e 65 lite3StrICmp("ne
1fa39 77 22 2c 7a 54 61 62 29 20 3d 3d 20 30 20 29 7b w",zTab) == 0 ){
1fa3a 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e . pExpr->
1fa3b 69 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 iTable = 1;.
1fa3c 20 20 20 20 70 54 61 62 20 3d 20 70 50 61 72 73 pTab = pPars
1fa3d 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3b 0a e->pTriggerTab;.
1fa3e 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1fa3f 6f 70 21 3d 54 4b 5f 49 4e 53 45 52 54 20 26 26 op!=TK_INSERT &&
1fa40 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
1fa41 22 6f 6c 64 22 2c 7a 54 61 62 29 3d 3d 30 20 29 "old",zTab)==0 )
1fa42 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d {. pExpr-
1fa43 3e 69 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 >iTable = 0;.
1fa44 20 20 20 20 20 70 54 61 62 20 3d 20 70 50 61 72 pTab = pPar
1fa45 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3b se->pTriggerTab;
1fa46 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
1fa47 69 66 28 20 70 54 61 62 20 29 7b 20 0a 20 20 20 if( pTab ){ .
1fa48 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 int iCol;.
1fa49 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d pSchema =
1fa4a 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a pTab->pSchema;.
1fa4b 20 20 20 20 20 20 20 20 63 6e 74 54 61 62 2b 2b cntTab++
1fa4c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 ;. if( sq
1fa4d 6c 69 74 65 33 49 73 52 6f 77 69 64 28 7a 43 6f lite3IsRowid(zCo
1fa4e 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 l) ){.
1fa4f 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 iCol = -1;.
1fa50 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1fa51 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 for(iCol=0;
1fa52 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b iCol<pTab->nCol;
1fa53 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 iCol++){.
1fa54 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 Column *pC
1fa55 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c ol = &pTab->aCol
1fa56 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 20 20 20 20 [iCol];.
1fa57 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1fa58 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 trICmp(pCol->zNa
1fa59 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a me, zCol)==0 ){.
1fa5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 if
1fa5b 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 69 50 ( iCol==pTab->iP
1fa5c 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 Key ){.
1fa5d 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 2d 31 iCol = -1
1fa5e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
1fa5f 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }.
1fa60 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 break;.
1fa61 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d }. }
1fa62 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1fa63 20 20 20 69 66 28 20 69 43 6f 6c 3c 70 54 61 62 if( iCol<pTab
1fa64 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 ->nCol ){.
1fa65 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 cnt++;.
1fa66 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 if( iCol<0
1fa67 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ){. p
1fa68 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d Expr->affinity =
1fa69 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 SQLITE_AFF_INTE
1fa6a 47 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 7d GER;. }
1fa6b 65 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e else if( pExpr->
1fa6c 69 54 61 62 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 iTable==0 ){.
1fa6d 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 testcas
1fa6e 65 28 20 69 43 6f 6c 3d 3d 33 31 20 29 3b 0a 20 e( iCol==31 );.
1fa6f 20 20 20 20 20 20 20 20 20 20 20 74 65 73 74 63 testc
1fa70 61 73 65 28 20 69 43 6f 6c 3d 3d 33 32 20 29 3b ase( iCol==32 );
1fa71 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 . pPa
1fa72 72 73 65 2d 3e 6f 6c 64 6d 61 73 6b 20 7c 3d 20 rse->oldmask |=
1fa73 28 69 43 6f 6c 3e 3d 33 32 20 3f 20 30 78 66 66 (iCol>=32 ? 0xff
1fa74 66 66 66 66 66 66 20 3a 20 28 28 28 75 33 32 29 ffffff : (((u32)
1fa75 31 29 3c 3c 69 43 6f 6c 29 29 3b 0a 20 20 20 20 1)<<iCol));.
1fa76 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1fa77 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e pExpr->iColumn
1fa78 20 3d 20 28 69 31 36 29 69 43 6f 6c 3b 0a 20 20 = (i16)iCol;.
1fa79 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 pExpr->p
1fa7a 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 Tab = pTab;.
1fa7b 20 20 20 20 20 20 69 73 54 72 69 67 67 65 72 20 isTrigger
1fa7c 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = 1;. }.
1fa7d 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e }. }.#en
1fa7e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 dif /* !defined(
1fa7f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 SQLITE_OMIT_TRIG
1fa80 47 45 52 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 0a GER) */.. /*.
1fa81 20 20 20 20 2a 2a 20 50 65 72 68 61 70 73 20 74 ** Perhaps t
1fa82 68 65 20 6e 61 6d 65 20 69 73 20 61 20 72 65 66 he name is a ref
1fa83 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 52 4f erence to the RO
1fa84 57 49 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 WID. */. i
1fa85 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 63 6e 74 f( cnt==0 && cnt
1fa86 54 61 62 3d 3d 31 20 26 26 20 73 71 6c 69 74 65 Tab==1 && sqlite
1fa87 33 49 73 52 6f 77 69 64 28 7a 43 6f 6c 29 20 29 3IsRowid(zCol) )
1fa88 7b 0a 20 20 20 20 20 20 63 6e 74 20 3d 20 31 3b {. cnt = 1;
1fa89 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 . pExpr->iC
1fa8a 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 olumn = -1;.
1fa8b 20 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 pExpr->affinit
1fa8c 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 y = SQLITE_AFF_I
1fa8d 4e 54 45 47 45 52 3b 0a 20 20 20 20 7d 0a 0a 20 NTEGER;. }..
1fa8e 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 49 66 20 /*. ** If
1fa8f 74 68 65 20 69 6e 70 75 74 20 69 73 20 6f 66 20 the input is of
1fa90 74 68 65 20 66 6f 72 6d 20 5a 20 28 6e 6f 74 20 the form Z (not
1fa91 59 2e 5a 20 6f 72 20 58 2e 59 2e 5a 29 20 74 68 Y.Z or X.Y.Z) th
1fa92 65 6e 20 74 68 65 20 6e 61 6d 65 20 5a 0a 20 20 en the name Z.
1fa93 20 20 2a 2a 20 6d 69 67 68 74 20 72 65 66 65 72 ** might refer
1fa94 20 74 6f 20 61 6e 20 72 65 73 75 6c 74 2d 73 65 to an result-se
1fa95 74 20 61 6c 69 61 73 2e 20 20 54 68 69 73 20 68 t alias. This h
1fa96 61 70 70 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d appens, for exam
1fa97 70 6c 65 2c 20 77 68 65 6e 0a 20 20 20 20 2a 2a ple, when. **
1fa98 20 77 65 20 61 72 65 20 72 65 73 6f 6c 76 69 6e we are resolvin
1fa99 67 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 57 g names in the W
1fa9a 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 HERE clause of t
1fa9b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d he following com
1fa9c 6d 61 6e 64 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 mand:. **.
1fa9d 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61 ** SELECT a
1fa9e 2b 62 20 41 53 20 78 20 46 52 4f 4d 20 74 61 62 +b AS x FROM tab
1fa9f 6c 65 20 57 48 45 52 45 20 78 3c 31 30 3b 0a 20 le WHERE x<10;.
1faa0 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 6e 20 **. ** In
1faa1 63 61 73 65 73 20 6c 69 6b 65 20 74 68 69 73 2c cases like this,
1faa2 20 72 65 70 6c 61 63 65 20 70 45 78 70 72 20 77 replace pExpr w
1faa3 69 74 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68 ith a copy of th
1faa4 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 e expression tha
1faa5 74 0a 20 20 20 20 2a 2a 20 66 6f 72 6d 73 20 74 t. ** forms t
1faa6 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 65 6e he result set en
1faa7 74 72 79 20 28 22 61 2b 62 22 20 69 6e 20 74 68 try ("a+b" in th
1faa8 65 20 65 78 61 6d 70 6c 65 29 20 61 6e 64 20 72 e example) and r
1faa9 65 74 75 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c eturn immediatel
1faaa 79 2e 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 y.. ** Note t
1faab 68 61 74 20 74 68 65 20 65 78 70 72 65 73 73 69 hat the expressi
1faac 6f 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 on in the result
1faad 20 73 65 74 20 73 68 6f 75 6c 64 20 68 61 76 65 set should have
1faae 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 20 20 already been.
1faaf 20 20 2a 2a 20 72 65 73 6f 6c 76 65 64 20 62 79 ** resolved by
1fab0 20 74 68 65 20 74 69 6d 65 20 74 68 65 20 57 48 the time the WH
1fab1 45 52 45 20 63 6c 61 75 73 65 20 69 73 20 72 65 ERE clause is re
1fab2 73 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 solved.. */.
1fab3 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 26 26 if( cnt==0 &&
1fab4 20 28 70 45 4c 69 73 74 20 3d 20 70 4e 43 2d 3e (pEList = pNC->
1fab5 70 45 4c 69 73 74 29 21 3d 30 20 26 26 20 7a 54 pEList)!=0 && zT
1fab6 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 ab==0 ){. f
1fab7 6f 72 28 6a 3d 30 3b 20 6a 3c 70 45 4c 69 73 74 or(j=0; j<pEList
1fab8 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b 0a 20 ->nExpr; j++){.
1fab9 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73 char *zAs
1faba 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e = pEList->a[j].
1fabb 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 zName;. i
1fabc 66 28 20 7a 41 73 21 3d 30 20 26 26 20 73 71 6c f( zAs!=0 && sql
1fabd 69 74 65 33 53 74 72 49 43 6d 70 28 7a 41 73 2c ite3StrICmp(zAs,
1fabe 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 zCol)==0 ){.
1fabf 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4f 72 Expr *pOr
1fac0 69 67 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 ig;. as
1fac1 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 sert( pExpr->pLe
1fac2 66 74 3d 3d 30 20 26 26 20 70 45 78 70 72 2d 3e ft==0 && pExpr->
1fac3 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20 pRight==0 );.
1fac4 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1fac5 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3d 3d 30 Expr->x.pList==0
1fac6 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 );. as
1fac7 73 65 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 sert( pExpr->x.p
1fac8 53 65 6c 65 63 74 3d 3d 30 20 29 3b 0a 20 20 20 Select==0 );.
1fac9 20 20 20 20 20 20 20 70 4f 72 69 67 20 3d 20 70 pOrig = p
1faca 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 EList->a[j].pExp
1facb 72 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 r;. if(
1facc 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 !pNC->allowAgg
1facd 26 26 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 && ExprHasProper
1face 74 79 28 70 4f 72 69 67 2c 20 45 50 5f 41 67 67 ty(pOrig, EP_Agg
1facf 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ) ){.
1fad0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1fad1 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 (pParse, "misuse
1fad2 20 6f 66 20 61 6c 69 61 73 65 64 20 61 67 67 72 of aliased aggr
1fad3 65 67 61 74 65 20 25 73 22 2c 20 7a 41 73 29 3b egate %s", zAs);
1fad4 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 . ret
1fad5 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1fad6 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1fad7 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69 61 resolveAlia
1fad8 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 s(pParse, pEList
1fad9 2c 20 6a 2c 20 70 45 78 70 72 2c 20 22 22 29 3b , j, pExpr, "");
1fada 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 20 3d . cnt =
1fadb 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d 1;. pM
1fadc 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 atch = 0;.
1fadd 20 20 20 20 61 73 73 65 72 74 28 20 7a 54 61 62 assert( zTab
1fade 3d 3d 30 20 26 26 20 7a 44 62 3d 3d 30 20 29 3b ==0 && zDb==0 );
1fadf 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
1fae0 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3b 0a lookupname_end;.
1fae1 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1fae2 7d 20 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a } . }.. /*
1fae3 20 41 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20 Advance to the
1fae4 6e 65 78 74 20 6e 61 6d 65 20 63 6f 6e 74 65 78 next name contex
1fae5 74 2e 20 20 54 68 65 20 6c 6f 6f 70 20 77 69 6c t. The loop wil
1fae6 6c 20 65 78 69 74 20 77 68 65 6e 20 65 69 74 68 l exit when eith
1fae7 65 72 0a 20 20 20 20 2a 2a 20 77 65 20 68 61 76 er. ** we hav
1fae8 65 20 61 20 6d 61 74 63 68 20 28 63 6e 74 3e 30 e a match (cnt>0
1fae9 29 20 6f 72 20 77 68 65 6e 20 77 65 20 72 75 6e ) or when we run
1faea 20 6f 75 74 20 6f 66 20 6e 61 6d 65 20 63 6f 6e out of name con
1faeb 74 65 78 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 texts.. */.
1faec 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 29 7b 0a if( cnt==0 ){.
1faed 20 20 20 20 20 20 70 4e 43 20 3d 20 70 4e 43 2d pNC = pNC-
1faee 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 >pNext;. }.
1faef 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49 66 20 }.. /*. ** If
1faf0 58 20 61 6e 64 20 59 20 61 72 65 20 4e 55 4c 4c X and Y are NULL
1faf1 20 28 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 (in other words
1faf2 20 69 66 20 6f 6e 6c 79 20 74 68 65 20 63 6f 6c if only the col
1faf3 75 6d 6e 20 6e 61 6d 65 20 5a 20 69 73 0a 20 20 umn name Z is.
1faf4 2a 2a 20 73 75 70 70 6c 69 65 64 29 20 61 6e 64 ** supplied) and
1faf5 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 5a 20 the value of Z
1faf6 69 73 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 64 is enclosed in d
1faf7 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2c 20 74 68 ouble-quotes, th
1faf8 65 6e 0a 20 20 2a 2a 20 5a 20 69 73 20 61 20 73 en. ** Z is a s
1faf9 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 69 66 tring literal if
1fafa 20 69 74 20 64 6f 65 73 6e 27 74 20 6d 61 74 63 it doesn't matc
1fafb 68 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d h any column nam
1fafc 65 73 2e 20 20 49 6e 20 74 68 61 74 0a 20 20 2a es. In that. *
1fafd 2a 20 63 61 73 65 2c 20 77 65 20 6e 65 65 64 20 * case, we need
1fafe 74 6f 20 72 65 74 75 72 6e 20 72 69 67 68 74 20 to return right
1faff 61 77 61 79 20 61 6e 64 20 6e 6f 74 20 6d 61 6b away and not mak
1fb00 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 74 6f e any changes to
1fb01 0a 20 20 2a 2a 20 70 45 78 70 72 2e 0a 20 20 2a . ** pExpr.. *
1fb02 2a 0a 20 20 2a 2a 20 42 65 63 61 75 73 65 20 6e *. ** Because n
1fb03 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61 73 20 o reference was
1fb04 6d 61 64 65 20 74 6f 20 6f 75 74 65 72 20 63 6f made to outer co
1fb05 6e 74 65 78 74 73 2c 20 74 68 65 20 70 4e 43 2d ntexts, the pNC-
1fb06 3e 6e 52 65 66 0a 20 20 2a 2a 20 66 69 65 6c 64 >nRef. ** field
1fb07 73 20 61 72 65 20 6e 6f 74 20 63 68 61 6e 67 65 s are not change
1fb08 64 20 69 6e 20 61 6e 79 20 63 6f 6e 74 65 78 74 d in any context
1fb09 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6e 74 .. */. if( cnt
1fb0a 3d 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30 20 26 ==0 && zTab==0 &
1fb0b 26 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 & ExprHasPropert
1fb0c 79 28 70 45 78 70 72 2c 45 50 5f 44 62 6c 51 75 y(pExpr,EP_DblQu
1fb0d 6f 74 65 64 29 20 29 7b 0a 20 20 20 20 70 45 78 oted) ){. pEx
1fb0e 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49 pr->op = TK_STRI
1fb0f 4e 47 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 70 NG;. pExpr->p
1fb10 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 Tab = 0;. ret
1fb11 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 urn WRC_Prune;.
1fb12 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 63 6e }.. /*. ** cn
1fb13 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 72 65 t==0 means there
1fb14 20 77 61 73 20 6e 6f 74 20 6d 61 74 63 68 2e 20 was not match.
1fb15 20 63 6e 74 3e 31 20 6d 65 61 6e 73 20 74 68 65 cnt>1 means the
1fb16 72 65 20 77 65 72 65 20 74 77 6f 20 6f 72 0a 20 re were two or.
1fb17 20 2a 2a 20 6d 6f 72 65 20 6d 61 74 63 68 65 73 ** more matches
1fb18 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c 20 77 . Either way, w
1fb19 65 20 68 61 76 65 20 61 6e 20 65 72 72 6f 72 2e e have an error.
1fb1a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6e 74 21 . */. if( cnt!
1fb1b 3d 31 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 =1 ){. const
1fb1c 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20 char *zErr;.
1fb1d 7a 45 72 72 20 3d 20 63 6e 74 3d 3d 30 20 3f 20 zErr = cnt==0 ?
1fb1e 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e 22 "no such column"
1fb1f 20 3a 20 22 61 6d 62 69 67 75 6f 75 73 20 63 6f : "ambiguous co
1fb20 6c 75 6d 6e 20 6e 61 6d 65 22 3b 0a 20 20 20 20 lumn name";.
1fb21 69 66 28 20 7a 44 62 20 29 7b 0a 20 20 20 20 20 if( zDb ){.
1fb22 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1fb23 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 (pParse, "%s: %s
1fb24 2e 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a .%s.%s", zErr, z
1fb25 44 62 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b Db, zTab, zCol);
1fb26 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 7a . }else if( z
1fb27 54 61 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c Tab ){. sql
1fb28 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1fb29 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 22 rse, "%s: %s.%s"
1fb2a 2c 20 7a 45 72 72 2c 20 7a 54 61 62 2c 20 7a 43 , zErr, zTab, zC
1fb2b 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ol);. }else{.
1fb2c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1fb2d 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 orMsg(pParse, "%
1fb2e 73 3a 20 25 73 22 2c 20 7a 45 72 72 2c 20 7a 43 s: %s", zErr, zC
1fb2f 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 ol);. }. p
1fb30 54 6f 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 TopNC->nErr++;.
1fb31 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 63 6f }.. /* If a co
1fb32 6c 75 6d 6e 20 66 72 6f 6d 20 61 20 74 61 62 6c lumn from a tabl
1fb33 65 20 69 6e 20 70 53 72 63 4c 69 73 74 20 69 73 e in pSrcList is
1fb34 20 72 65 66 65 72 65 6e 63 65 64 2c 20 74 68 65 referenced, the
1fb35 6e 20 72 65 63 6f 72 64 0a 20 20 2a 2a 20 74 68 n record. ** th
1fb36 69 73 20 66 61 63 74 20 69 6e 20 74 68 65 20 70 is fact in the p
1fb37 53 72 63 4c 69 73 74 2e 61 5b 5d 2e 63 6f 6c 55 SrcList.a[].colU
1fb38 73 65 64 20 62 69 74 6d 61 73 6b 2e 20 20 43 6f sed bitmask. Co
1fb39 6c 75 6d 6e 20 30 20 63 61 75 73 65 73 0a 20 20 lumn 0 causes.
1fb3a 2a 2a 20 62 69 74 20 30 20 74 6f 20 62 65 20 73 ** bit 0 to be s
1fb3b 65 74 2e 20 20 43 6f 6c 75 6d 6e 20 31 20 73 65 et. Column 1 se
1fb3c 74 73 20 62 69 74 20 31 2e 20 20 41 6e 64 20 73 ts bit 1. And s
1fb3d 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68 65 o forth. If the
1fb3e 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d . ** column num
1fb3f 62 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74 ber is greater t
1fb40 68 61 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f han the number o
1fb41 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 62 69 f bits in the bi
1fb42 74 6d 61 73 6b 0a 20 20 2a 2a 20 74 68 65 6e 20 tmask. ** then
1fb43 73 65 74 20 74 68 65 20 68 69 67 68 2d 6f 72 64 set the high-ord
1fb44 65 72 20 62 69 74 20 6f 66 20 74 68 65 20 62 69 er bit of the bi
1fb45 74 6d 61 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 tmask.. */. if
1fb46 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e ( pExpr->iColumn
1fb47 3e 3d 30 20 26 26 20 70 4d 61 74 63 68 21 3d 30 >=0 && pMatch!=0
1fb48 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 ){. int n =
1fb49 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a pExpr->iColumn;.
1fb4a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d testcase( n=
1fb4b 3d 42 4d 53 2d 31 20 29 3b 0a 20 20 20 20 69 66 =BMS-1 );. if
1fb4c 28 20 6e 3e 3d 42 4d 53 20 29 7b 0a 20 20 20 20 ( n>=BMS ){.
1fb4d 20 20 6e 20 3d 20 42 4d 53 2d 31 3b 0a 20 20 20 n = BMS-1;.
1fb4e 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
1fb4f 4d 61 74 63 68 2d 3e 69 43 75 72 73 6f 72 3d 3d Match->iCursor==
1fb50 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b pExpr->iTable );
1fb51 0a 20 20 20 20 70 4d 61 74 63 68 2d 3e 63 6f 6c . pMatch->col
1fb52 55 73 65 64 20 7c 3d 20 28 28 42 69 74 6d 61 73 Used |= ((Bitmas
1fb53 6b 29 31 29 3c 3c 6e 3b 0a 20 20 7d 0a 0a 20 20 k)1)<<n;. }..
1fb54 2f 2a 20 43 6c 65 61 6e 20 75 70 20 61 6e 64 20 /* Clean up and
1fb55 72 65 74 75 72 6e 0a 20 20 2a 2f 0a 20 20 73 71 return. */. sq
1fb56 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
1fb57 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 db, pExpr->pLeft
1fb58 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 );. pExpr->pLef
1fb59 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 t = 0;. sqlite3
1fb5a 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
1fb5b 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 Expr->pRight);.
1fb5c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 3d pExpr->pRight =
1fb5d 20 30 3b 0a 20 20 70 45 78 70 72 2d 3e 6f 70 20 0;. pExpr->op
1fb5e 3d 20 28 69 73 54 72 69 67 67 65 72 20 3f 20 54 = (isTrigger ? T
1fb5f 4b 5f 54 52 49 47 47 45 52 20 3a 20 54 4b 5f 43 K_TRIGGER : TK_C
1fb60 4f 4c 55 4d 4e 29 3b 0a 6c 6f 6f 6b 75 70 6e 61 OLUMN);.lookupna
1fb61 6d 65 5f 65 6e 64 3a 0a 20 20 69 66 28 20 63 6e me_end:. if( cn
1fb62 74 3d 3d 31 20 29 7b 0a 20 20 20 20 61 73 73 65 t==1 ){. asse
1fb63 72 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a 20 20 rt( pNC!=0 );.
1fb64 20 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 sqlite3AuthRea
1fb65 64 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c d(pParse, pExpr,
1fb66 20 70 53 63 68 65 6d 61 2c 20 70 4e 43 2d 3e 70 pSchema, pNC->p
1fb67 53 72 63 4c 69 73 74 29 3b 0a 20 20 20 20 2f 2a SrcList);. /*
1fb68 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 6e Increment the n
1fb69 52 65 66 20 76 61 6c 75 65 20 6f 6e 20 61 6c 6c Ref value on all
1fb6a 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 73 20 66 name contexts f
1fb6b 72 6f 6d 20 54 6f 70 4e 43 20 75 70 20 74 6f 0a rom TopNC up to.
1fb6c 20 20 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74 ** the point
1fb6d 20 77 68 65 72 65 20 74 68 65 20 6e 61 6d 65 20 where the name
1fb6e 6d 61 74 63 68 65 64 2e 20 2a 2f 0a 20 20 20 20 matched. */.
1fb6f 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 20 61 for(;;){. a
1fb70 73 73 65 72 74 28 20 70 54 6f 70 4e 43 21 3d 30 ssert( pTopNC!=0
1fb71 20 29 3b 0a 20 20 20 20 20 20 70 54 6f 70 4e 43 );. pTopNC
1fb72 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 ->nRef++;.
1fb73 69 66 28 20 70 54 6f 70 4e 43 3d 3d 70 4e 43 20 if( pTopNC==pNC
1fb74 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 70 ) break;. p
1fb75 54 6f 70 4e 43 20 3d 20 70 54 6f 70 4e 43 2d 3e TopNC = pTopNC->
1fb76 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 pNext;. }.
1fb77 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e return WRC_Prun
1fb78 65 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 e;. } else {.
1fb79 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f return WRC_Abo
1fb7a 72 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a rt;. }.}../*.**
1fb7b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
1fb7c 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 73 71 callback for sq
1fb7d 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 29 2e lite3WalkExpr().
1fb7e 0a 2a 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 73 .**.** Resolve s
1fb7f 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 73 20 69 6e ymbolic names in
1fb80 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 6f 70 65 to TK_COLUMN ope
1fb81 72 61 74 6f 72 73 20 66 6f 72 20 74 68 65 20 63 rators for the c
1fb82 75 72 72 65 6e 74 0a 2a 2a 20 6e 6f 64 65 20 69 urrent.** node i
1fb83 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e n the expression
1fb84 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 30 tree. Return 0
1fb85 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 68 65 to continue the
1fb86 20 73 65 61 72 63 68 20 64 6f 77 6e 0a 2a 2a 20 search down.**
1fb87 74 68 65 20 74 72 65 65 20 6f 72 20 32 20 74 6f the tree or 2 to
1fb88 20 61 62 6f 72 74 20 74 68 65 20 74 72 65 65 20 abort the tree
1fb89 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 walk..**.** This
1fb8a 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f routine also do
1fb8b 65 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e es error checkin
1fb8c 67 20 61 6e 64 20 6e 61 6d 65 20 72 65 73 6f 6c g and name resol
1fb8d 75 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 66 75 6e ution for.** fun
1fb8e 63 74 69 6f 6e 20 6e 61 6d 65 73 2e 20 20 54 68 ction names. Th
1fb8f 65 20 6f 70 65 72 61 74 6f 72 20 66 6f 72 20 61 e operator for a
1fb90 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
1fb91 6e 73 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a ns is changed.**
1fb92 20 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 to TK_AGG_FUNCT
1fb93 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ION..*/.static i
1fb94 6e 74 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74 nt resolveExprSt
1fb95 65 70 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b ep(Walker *pWalk
1fb96 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 er, Expr *pExpr)
1fb97 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 {. NameContext
1fb98 2a 70 4e 43 3b 0a 20 20 50 61 72 73 65 20 2a 70 *pNC;. Parse *p
1fb99 50 61 72 73 65 3b 0a 0a 20 20 70 4e 43 20 3d 20 Parse;.. pNC =
1fb9a 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a pWalker->u.pNC;.
1fb9b 20 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30 assert( pNC!=0
1fb9c 20 29 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70 );. pParse = p
1fb9d 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 61 73 NC->pParse;. as
1fb9e 73 65 72 74 28 20 70 50 61 72 73 65 3d 3d 70 57 sert( pParse==pW
1fb9f 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 20 29 3b alker->pParse );
1fba0 0a 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 41 .. if( ExprHasA
1fba1 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 nyProperty(pExpr
1fba2 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 20 29 , EP_Resolved) )
1fba3 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e return WRC_Prun
1fba4 65 3b 0a 20 20 45 78 70 72 53 65 74 50 72 6f 70 e;. ExprSetProp
1fba5 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52 erty(pExpr, EP_R
1fba6 65 73 6f 6c 76 65 64 29 3b 0a 23 69 66 6e 64 65 esolved);.#ifnde
1fba7 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 70 f NDEBUG. if( p
1fba8 4e 43 2d 3e 70 53 72 63 4c 69 73 74 20 26 26 20 NC->pSrcList &&
1fba9 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e 6e pNC->pSrcList->n
1fbaa 41 6c 6c 6f 63 3e 30 20 29 7b 0a 20 20 20 20 53 Alloc>0 ){. S
1fbab 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 rcList *pSrcList
1fbac 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 = pNC->pSrcList
1fbad 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 ;. int i;.
1fbae 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 43 2d for(i=0; i<pNC-
1fbaf 3e 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b >pSrcList->nSrc;
1fbb0 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 i++){. ass
1fbb1 65 72 74 28 20 70 53 72 63 4c 69 73 74 2d 3e 61 ert( pSrcList->a
1fbb2 5b 69 5d 2e 69 43 75 72 73 6f 72 3e 3d 30 20 26 [i].iCursor>=0 &
1fbb3 26 20 70 53 72 63 4c 69 73 74 2d 3e 61 5b 69 5d & pSrcList->a[i]
1fbb4 2e 69 43 75 72 73 6f 72 3c 70 50 61 72 73 65 2d .iCursor<pParse-
1fbb5 3e 6e 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 >nTab);. }.
1fbb6 7d 0a 23 65 6e 64 69 66 0a 20 20 73 77 69 74 63 }.#endif. switc
1fbb7 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a h( pExpr->op ){.
1fbb8 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
1fbb9 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 ITE_ENABLE_UPDAT
1fbba 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 E_DELETE_LIMIT)
1fbbb 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
1fbbc 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 TE_OMIT_SUBQUERY
1fbbd 29 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 ). /* The spe
1fbbe 63 69 61 6c 20 6f 70 65 72 61 74 6f 72 20 54 4b cial operator TK
1fbbf 5f 52 4f 57 20 6d 65 61 6e 73 20 75 73 65 20 74 _ROW means use t
1fbc0 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 he rowid for the
1fbc1 20 66 69 72 73 74 0a 20 20 20 20 2a 2a 20 63 6f first. ** co
1fbc2 6c 75 6d 6e 20 69 6e 20 74 68 65 20 46 52 4f 4d lumn in the FROM
1fbc3 20 63 6c 61 75 73 65 2e 20 20 54 68 69 73 20 69 clause. This i
1fbc4 73 20 75 73 65 64 20 62 79 20 74 68 65 20 4c 49 s used by the LI
1fbc5 4d 49 54 20 61 6e 64 20 4f 52 44 45 52 20 42 59 MIT and ORDER BY
1fbc6 0a 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 70 . ** clause p
1fbc7 72 6f 63 65 73 73 69 6e 67 20 6f 6e 20 55 50 44 rocessing on UPD
1fbc8 41 54 45 20 61 6e 64 20 44 45 4c 45 54 45 20 73 ATE and DELETE s
1fbc9 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a tatements.. *
1fbca 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 4f /. case TK_RO
1fbcb 57 3a 20 7b 0a 20 20 20 20 20 20 53 72 63 4c 69 W: {. SrcLi
1fbcc 73 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 st *pSrcList = p
1fbcd 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20 NC->pSrcList;.
1fbce 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 struct SrcLi
1fbcf 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a st_item *pItem;.
1fbd0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 assert( pS
1fbd1 72 63 4c 69 73 74 20 26 26 20 70 53 72 63 4c 69 rcList && pSrcLi
1fbd2 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 st->nSrc==1 );.
1fbd3 20 20 20 20 20 70 49 74 65 6d 20 3d 20 70 53 72 pItem = pSr
1fbd4 63 4c 69 73 74 2d 3e 61 3b 20 0a 20 20 20 20 20 cList->a; .
1fbd5 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f pExpr->op = TK_
1fbd6 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 70 45 COLUMN;. pE
1fbd7 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 49 74 65 xpr->pTab = pIte
1fbd8 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 70 m->pTab;. p
1fbd9 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 Expr->iTable = p
1fbda 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 Item->iCursor;.
1fbdb 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c pExpr->iCol
1fbdc 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 umn = -1;.
1fbdd 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 pExpr->affinity
1fbde 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 = SQLITE_AFF_INT
1fbdf 45 47 45 52 3b 0a 20 20 20 20 20 20 62 72 65 61 EGER;. brea
1fbe0 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 k;. }.#endif
1fbe1 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 /* defined(SQLIT
1fbe2 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f E_ENABLE_UPDATE_
1fbe3 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 DELETE_LIMIT) &&
1fbe4 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1fbe5 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 _OMIT_SUBQUERY)
1fbe6 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41 20 6c 6f 6e */.. /* A lon
1fbe7 65 20 69 64 65 6e 74 69 66 69 65 72 20 69 73 20 e identifier is
1fbe8 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f the name of a co
1fbe9 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 lumn.. */.
1fbea 20 63 61 73 65 20 54 4b 5f 49 44 3a 20 7b 0a 20 case TK_ID: {.
1fbeb 20 20 20 20 20 72 65 74 75 72 6e 20 6c 6f 6f 6b return look
1fbec 75 70 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 30 upName(pParse, 0
1fbed 2c 20 30 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 , 0, pExpr->u.zT
1fbee 6f 6b 65 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72 oken, pNC, pExpr
1fbef 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 );. }. .
1fbf0 2f 2a 20 41 20 74 61 62 6c 65 20 6e 61 6d 65 20 /* A table name
1fbf1 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a and column name:
1fbf2 20 20 20 20 20 49 44 2e 49 44 0a 20 20 20 20 2a ID.ID. *
1fbf3 2a 20 4f 72 20 61 20 64 61 74 61 62 61 73 65 2c * Or a database,
1fbf4 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d table and colum
1fbf5 6e 3a 20 20 49 44 2e 49 44 2e 49 44 0a 20 20 20 n: ID.ID.ID.
1fbf6 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f */. case TK_
1fbf7 44 4f 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e DOT: {. con
1fbf8 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e st char *zColumn
1fbf9 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 ;. const ch
1fbfa 61 72 20 2a 7a 54 61 62 6c 65 3b 0a 20 20 20 20 ar *zTable;.
1fbfb 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
1fbfc 62 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 b;. Expr *p
1fbfd 52 69 67 68 74 3b 0a 0a 20 20 20 20 20 20 2f 2a Right;.. /*
1fbfe 20 69 66 28 20 70 53 72 63 4c 69 73 74 3d 3d 30 if( pSrcList==0
1fbff 20 29 20 62 72 65 61 6b 3b 20 2a 2f 0a 20 20 20 ) break; */.
1fc00 20 20 20 70 52 69 67 68 74 20 3d 20 70 45 78 70 pRight = pExp
1fc01 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 r->pRight;.
1fc02 20 69 66 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d if( pRight->op=
1fc03 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 20 =TK_ID ){.
1fc04 20 20 7a 44 62 20 3d 20 30 3b 0a 20 20 20 20 20 zDb = 0;.
1fc05 20 20 20 7a 54 61 62 6c 65 20 3d 20 70 45 78 70 zTable = pExp
1fc06 72 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b r->pLeft->u.zTok
1fc07 65 6e 3b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c en;. zCol
1fc08 75 6d 6e 20 3d 20 70 52 69 67 68 74 2d 3e 75 2e umn = pRight->u.
1fc09 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 65 zToken;. }e
1fc0a 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 lse{. ass
1fc0b 65 72 74 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d ert( pRight->op=
1fc0c 3d 54 4b 5f 44 4f 54 20 29 3b 0a 20 20 20 20 20 =TK_DOT );.
1fc0d 20 20 20 7a 44 62 20 3d 20 70 45 78 70 72 2d 3e zDb = pExpr->
1fc0e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b pLeft->u.zToken;
1fc0f 0a 20 20 20 20 20 20 20 20 7a 54 61 62 6c 65 20 . zTable
1fc10 3d 20 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 2d = pRight->pLeft-
1fc11 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 >u.zToken;.
1fc12 20 20 20 7a 43 6f 6c 75 6d 6e 20 3d 20 70 52 69 zColumn = pRi
1fc13 67 68 74 2d 3e 70 52 69 67 68 74 2d 3e 75 2e 7a ght->pRight->u.z
1fc14 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 Token;. }.
1fc15 20 20 20 20 20 72 65 74 75 72 6e 20 6c 6f 6f 6b return look
1fc16 75 70 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a upName(pParse, z
1fc17 44 62 2c 20 7a 54 61 62 6c 65 2c 20 7a 43 6f 6c Db, zTable, zCol
1fc18 75 6d 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72 29 umn, pNC, pExpr)
1fc19 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
1fc1a 52 65 73 6f 6c 76 65 20 66 75 6e 63 74 69 6f 6e Resolve function
1fc1b 20 6e 61 6d 65 73 0a 20 20 20 20 2a 2f 0a 20 20 names. */.
1fc1c 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f case TK_CONST_
1fc1d 46 55 4e 43 3a 0a 20 20 20 20 63 61 73 65 20 54 FUNC:. case T
1fc1e 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 K_FUNCTION: {.
1fc1f 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c ExprList *pL
1fc20 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 ist = pExpr->x.p
1fc21 4c 69 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 List; /* The
1fc22 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 20 2a 2f argument list */
1fc23 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 . int n = p
1fc24 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 List ? pList->nE
1fc25 78 70 72 20 3a 20 30 3b 20 20 20 20 2f 2a 20 4e xpr : 0; /* N
1fc26 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e umber of argumen
1fc27 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 ts */. int
1fc28 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 30 no_such_func = 0
1fc29 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 ; /* True
1fc2a 69 66 20 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 if no such funct
1fc2b 69 6f 6e 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 ion exists */.
1fc2c 20 20 20 20 69 6e 74 20 77 72 6f 6e 67 5f 6e 75 int wrong_nu
1fc2d 6d 5f 61 72 67 73 20 3d 20 30 3b 20 20 20 20 20 m_args = 0;
1fc2e 2f 2a 20 54 72 75 65 20 69 66 20 77 72 6f 6e 67 /* True if wrong
1fc2f 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d number of argum
1fc30 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e ents */. in
1fc31 74 20 69 73 5f 61 67 67 20 3d 20 30 3b 20 20 20 t is_agg = 0;
1fc32 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
1fc33 65 20 69 66 20 69 73 20 61 6e 20 61 67 67 72 65 e if is an aggre
1fc34 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f gate function */
1fc35 0a 20 20 20 20 20 20 69 6e 74 20 61 75 74 68 3b . int auth;
1fc36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc37 20 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 /* Authorizat
1fc38 69 6f 6e 20 74 6f 20 75 73 65 20 74 68 65 20 66 ion to use the f
1fc39 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 unction */.
1fc3a 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 20 int nId;
1fc3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1fc3c 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 Number of charac
1fc3d 74 65 72 73 20 69 6e 20 66 75 6e 63 74 69 6f 6e ters in function
1fc3e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 63 name */. c
1fc3f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20 onst char *zId;
1fc40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1fc41 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 2e e function name.
1fc42 20 2a 2f 0a 20 20 20 20 20 20 46 75 6e 63 44 65 */. FuncDe
1fc43 66 20 2a 70 44 65 66 3b 20 20 20 20 20 20 20 20 f *pDef;
1fc44 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 /* Informa
1fc45 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 tion about the f
1fc46 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 unction */.
1fc47 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50 u8 enc = ENC(pP
1fc48 61 72 73 65 2d 3e 64 62 29 3b 20 20 20 2f 2a 20 arse->db); /*
1fc49 54 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63 The database enc
1fc4a 6f 64 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 20 20 oding */..
1fc4b 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d testcase( pExpr-
1fc4c 3e 6f 70 3d 3d 54 4b 5f 43 4f 4e 53 54 5f 46 55 >op==TK_CONST_FU
1fc4d 4e 43 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 NC );. asse
1fc4e 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 rt( !ExprHasProp
1fc4f 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 erty(pExpr, EP_x
1fc50 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 IsSelect) );.
1fc51 20 20 20 7a 49 64 20 3d 20 70 45 78 70 72 2d 3e zId = pExpr->
1fc52 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 u.zToken;.
1fc53 6e 49 64 20 3d 20 73 71 6c 69 74 65 33 53 74 72 nId = sqlite3Str
1fc54 6c 65 6e 33 30 28 7a 49 64 29 3b 0a 20 20 20 20 len30(zId);.
1fc55 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 pDef = sqlite3
1fc56 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 FindFunction(pPa
1fc57 72 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 rse->db, zId, nI
1fc58 64 2c 20 6e 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 d, n, enc, 0);.
1fc59 20 20 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30 if( pDef==0
1fc5a 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 ){. pDef
1fc5b 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 = sqlite3FindFu
1fc5c 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64 nction(pParse->d
1fc5d 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 2d 31 2c b, zId, nId, -1,
1fc5e 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 enc, 0);.
1fc5f 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29 7b if( pDef==0 ){
1fc60 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 5f 73 75 . no_su
1fc61 63 68 5f 66 75 6e 63 20 3d 20 31 3b 0a 20 20 20 ch_func = 1;.
1fc62 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1fc63 20 20 20 20 20 20 77 72 6f 6e 67 5f 6e 75 6d 5f wrong_num_
1fc64 61 72 67 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 args = 1;.
1fc65 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
1fc66 0a 20 20 20 20 20 20 20 20 69 73 5f 61 67 67 20 . is_agg
1fc67 3d 20 70 44 65 66 2d 3e 78 46 75 6e 63 3d 3d 30 = pDef->xFunc==0
1fc68 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 ;. }.#ifnde
1fc69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1fc6a 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 THORIZATION.
1fc6b 20 20 69 66 28 20 70 44 65 66 20 29 7b 0a 20 20 if( pDef ){.
1fc6c 20 20 20 20 20 20 61 75 74 68 20 3d 20 73 71 6c auth = sql
1fc6d 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 ite3AuthCheck(pP
1fc6e 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 46 55 4e arse, SQLITE_FUN
1fc6f 43 54 49 4f 4e 2c 20 30 2c 20 70 44 65 66 2d 3e CTION, 0, pDef->
1fc70 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 zName, 0);.
1fc71 20 20 20 69 66 28 20 61 75 74 68 21 3d 53 51 4c if( auth!=SQL
1fc72 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1fc73 20 20 20 20 69 66 28 20 61 75 74 68 3d 3d 53 51 if( auth==SQ
1fc74 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 LITE_DENY ){.
1fc75 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1fc76 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
1fc77 20 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 "not authorized
1fc78 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69 6f 6e to use function
1fc79 3a 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 : %s",.
1fc7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc7b 20 20 20 20 20 20 20 20 20 20 20 70 44 65 66 2d pDef-
1fc7c 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 >zName);.
1fc7d 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b pNC->nErr++
1fc7e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1fc7f 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f pExpr->o
1fc80 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 p = TK_NULL;.
1fc81 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 return WR
1fc82 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 20 20 20 C_Prune;.
1fc83 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 }. }.#endi
1fc84 66 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 f. if( is_a
1fc85 67 67 20 26 26 20 21 70 4e 43 2d 3e 61 6c 6c 6f gg && !pNC->allo
1fc86 77 41 67 67 20 29 7b 0a 20 20 20 20 20 20 20 20 wAgg ){.
1fc87 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1fc88 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20 pParse, "misuse
1fc89 6f 66 20 61 67 67 72 65 67 61 74 65 20 66 75 6e of aggregate fun
1fc8a 63 74 69 6f 6e 20 25 2e 2a 73 28 29 22 2c 20 6e ction %.*s()", n
1fc8b 49 64 2c 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 Id,zId);.
1fc8c 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 pNC->nErr++;.
1fc8d 20 20 20 20 20 20 69 73 5f 61 67 67 20 3d 20 30 is_agg = 0
1fc8e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
1fc8f 28 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 29 ( no_such_func )
1fc90 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1fc91 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1fc92 2c 20 22 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 , "no such funct
1fc93 69 6f 6e 3a 20 25 2e 2a 73 22 2c 20 6e 49 64 2c ion: %.*s", nId,
1fc94 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70 zId);. p
1fc95 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 NC->nErr++;.
1fc96 20 20 7d 65 6c 73 65 20 69 66 28 20 77 72 6f 6e }else if( wron
1fc97 67 5f 6e 75 6d 5f 61 72 67 73 20 29 7b 0a 20 20 g_num_args ){.
1fc98 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1fc99 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22 77 72 orMsg(pParse,"wr
1fc9a 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 ong number of ar
1fc9b 67 75 6d 65 6e 74 73 20 74 6f 20 66 75 6e 63 74 guments to funct
1fc9c 69 6f 6e 20 25 2e 2a 73 28 29 22 2c 0a 20 20 20 ion %.*s()",.
1fc9d 20 20 20 20 20 20 20 20 20 20 6e 49 64 2c 20 7a nId, z
1fc9e 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 Id);. pNC
1fc9f 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 ->nErr++;.
1fca0 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 }. if( is_a
1fca1 67 67 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45 gg ){. pE
1fca2 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 xpr->op = TK_AGG
1fca3 5f 46 55 4e 43 54 49 4f 4e 3b 0a 20 20 20 20 20 _FUNCTION;.
1fca4 20 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d pNC->hasAgg =
1fca5 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 1;. }.
1fca6 20 20 69 66 28 20 69 73 5f 61 67 67 20 29 20 70 if( is_agg ) p
1fca7 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 30 NC->allowAgg = 0
1fca8 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 ;. sqlite3W
1fca9 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c alkExprList(pWal
1fcaa 6b 65 72 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 ker, pList);.
1fcab 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 29 20 if( is_agg )
1fcac 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 pNC->allowAgg =
1fcad 31 3b 0a 20 20 20 20 20 20 2f 2a 20 46 49 58 20 1;. /* FIX
1fcae 4d 45 3a 20 20 43 6f 6d 70 75 74 65 20 70 45 78 ME: Compute pEx
1fcaf 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 62 61 73 pr->affinity bas
1fcb0 65 64 20 6f 6e 20 74 68 65 20 65 78 70 65 63 74 ed on the expect
1fcb1 65 64 20 72 65 74 75 72 6e 0a 20 20 20 20 20 20 ed return.
1fcb2 2a 2a 20 74 79 70 65 20 6f 66 20 74 68 65 20 66 ** type of the f
1fcb3 75 6e 63 74 69 6f 6e 20 0a 20 20 20 20 20 20 2a unction . *
1fcb4 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 /. return W
1fcb5 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a RC_Prune;. }.
1fcb6 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1fcb7 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 MIT_SUBQUERY.
1fcb8 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a case TK_SELECT:
1fcb9 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 . case TK_EXI
1fcba 53 54 53 3a 20 20 74 65 73 74 63 61 73 65 28 20 STS: testcase(
1fcbb 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 pExpr->op==TK_EX
1fcbc 49 53 54 53 20 29 3b 0a 23 65 6e 64 69 66 0a 20 ISTS );.#endif.
1fcbd 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b case TK_IN: {
1fcbe 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1fcbf 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 pExpr->op==TK_I
1fcc0 4e 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 45 N );. if( E
1fcc1 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
1fcc2 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 Expr, EP_xIsSele
1fcc3 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ct) ){. i
1fcc4 6e 74 20 6e 52 65 66 20 3d 20 70 4e 43 2d 3e 6e nt nRef = pNC->n
1fcc5 52 65 66 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c Ref;.#ifndef SQL
1fcc6 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 ITE_OMIT_CHECK.
1fcc7 20 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e if( pNC->
1fcc8 69 73 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 isCheck ){.
1fcc9 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1fcca 72 4d 73 67 28 70 50 61 72 73 65 2c 22 73 75 62 rMsg(pParse,"sub
1fccb 71 75 65 72 69 65 73 20 70 72 6f 68 69 62 69 74 queries prohibit
1fccc 65 64 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 ed in CHECK cons
1fccd 74 72 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 traints");.
1fcce 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
1fccf 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 sqlite3WalkS
1fcd0 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 elect(pWalker, p
1fcd1 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 Expr->x.pSelect)
1fcd2 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
1fcd3 28 20 70 4e 43 2d 3e 6e 52 65 66 3e 3d 6e 52 65 ( pNC->nRef>=nRe
1fcd4 66 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 f );. if(
1fcd5 20 6e 52 65 66 21 3d 70 4e 43 2d 3e 6e 52 65 66 nRef!=pNC->nRef
1fcd6 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 ){. Ex
1fcd7 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 prSetProperty(pE
1fcd8 78 70 72 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 xpr, EP_VarSelec
1fcd9 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 t);. }.
1fcda 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
1fcdb 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 k;. }.#ifndef
1fcdc 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 SQLITE_OMIT_CHE
1fcdd 43 4b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 56 CK. case TK_V
1fcde 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20 20 20 20 ARIABLE: {.
1fcdf 20 69 66 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 if( pNC->isChec
1fce0 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c k ){. sql
1fce1 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1fce2 72 73 65 2c 22 70 61 72 61 6d 65 74 65 72 73 20 rse,"parameters
1fce3 70 72 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48 prohibited in CH
1fce4 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 ECK constraints"
1fce5 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1fce6 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 break;. }.#e
1fce7 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 ndif. }. retur
1fce8 6e 20 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 n (pParse->nErr
1fce9 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d || pParse->db->m
1fcea 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 3f 20 57 allocFailed) ? W
1fceb 52 43 5f 41 62 6f 72 74 20 3a 20 57 52 43 5f 43 RC_Abort : WRC_C
1fcec 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a ontinue;.}../*.*
1fced 2a 20 70 45 4c 69 73 74 20 69 73 20 61 20 6c 69 * pEList is a li
1fcee 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e st of expression
1fcef 73 20 77 68 69 63 68 20 61 72 65 20 72 65 61 6c s which are real
1fcf0 6c 79 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 ly the result se
1fcf1 74 20 6f 66 20 74 68 65 0a 2a 2a 20 61 20 53 45 t of the.** a SE
1fcf2 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 LECT statement.
1fcf3 20 70 45 20 69 73 20 61 20 74 65 72 6d 20 69 6e pE is a term in
1fcf4 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 an ORDER BY or
1fcf5 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e GROUP BY clause.
1fcf6 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1fcf7 20 63 68 65 63 6b 73 20 74 6f 20 73 65 65 20 69 checks to see i
1fcf8 66 20 70 45 20 69 73 20 61 20 73 69 6d 70 6c 65 f pE is a simple
1fcf9 20 69 64 65 6e 74 69 66 69 65 72 20 77 68 69 63 identifier whic
1fcfa 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 0a 2a 2a h corresponds.**
1fcfb 20 74 6f 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 to the AS-name
1fcfc 6f 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 65 of one of the te
1fcfd 72 6d 73 20 6f 66 20 74 68 65 20 65 78 70 72 65 rms of the expre
1fcfe 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 49 66 20 ssion list. If
1fcff 69 74 20 69 73 2c 0a 2a 2a 20 74 68 69 73 20 72 it is,.** this r
1fd00 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 20 61 6e outine return an
1fd01 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e integer between
1fd02 20 31 20 61 6e 64 20 4e 20 77 68 65 72 65 20 4e 1 and N where N
1fd03 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
1fd04 66 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 69 6e f.** elements in
1fd05 20 70 45 4c 69 73 74 2c 20 63 6f 72 72 65 73 70 pEList, corresp
1fd06 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 6d 61 onding to the ma
1fd07 74 63 68 69 6e 67 20 65 6e 74 72 79 2e 20 20 49 tching entry. I
1fd08 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f f there is.** no
1fd09 20 6d 61 74 63 68 2c 20 6f 72 20 69 66 20 70 45 match, or if pE
1fd0a 20 69 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 is not a simple
1fd0b 20 69 64 65 6e 74 69 66 69 65 72 2c 20 74 68 65 identifier, the
1fd0c 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a n this routine.*
1fd0d 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a * return 0..**.*
1fd0e 2a 20 70 45 4c 69 73 74 20 68 61 73 20 62 65 65 * pEList has bee
1fd0f 6e 20 72 65 73 6f 6c 76 65 64 2e 20 20 70 45 20 n resolved. pE
1fd10 68 61 73 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 has not..*/.stat
1fd11 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 41 73 ic int resolveAs
1fd12 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70 Name(. Parse *p
1fd13 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 Parse, /* Pa
1fd14 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f rsing context fo
1fd15 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 r error messages
1fd16 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
1fd17 70 45 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 pEList, /* List
1fd18 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 of expressions
1fd19 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70 to scan */. Exp
1fd1a 72 20 2a 70 45 20 20 20 20 20 20 20 20 20 20 20 r *pE
1fd1b 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 77 65 /* Expression we
1fd1c 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20 6d are trying to m
1fd1d 61 74 63 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 atch */.){. int
1fd1e 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
1fd1f 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
1fd20 2a 2f 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 */.. UNUSED_PAR
1fd21 41 4d 45 54 45 52 28 70 50 61 72 73 65 29 3b 0a AMETER(pParse);.
1fd22 0a 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 . if( pE->op==T
1fd23 4b 5f 49 44 20 29 7b 0a 20 20 20 20 63 68 61 72 K_ID ){. char
1fd24 20 2a 7a 43 6f 6c 20 3d 20 70 45 2d 3e 75 2e 7a *zCol = pE->u.z
1fd25 54 6f 6b 65 6e 3b 0a 20 20 20 20 66 6f 72 28 69 Token;. for(i
1fd26 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 =0; i<pEList->nE
1fd27 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 xpr; i++){.
1fd28 20 63 68 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c char *zAs = pEL
1fd29 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b ist->a[i].zName;
1fd2a 0a 20 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d . if( zAs!=
1fd2b 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 0 && sqlite3StrI
1fd2c 43 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d Cmp(zAs, zCol)==
1fd2d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 0 ){. ret
1fd2e 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d urn i+1;. }
1fd2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
1fd30 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
1fd31 70 45 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 pE is a pointer
1fd32 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e to an expression
1fd33 20 77 68 69 63 68 20 69 73 20 61 20 73 69 6e 67 which is a sing
1fd34 6c 65 20 74 65 72 6d 20 69 6e 20 74 68 65 0a 2a le term in the.*
1fd35 2a 20 4f 52 44 45 52 20 42 59 20 6f 66 20 61 20 * ORDER BY of a
1fd36 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e compound SELECT.
1fd37 20 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e The expression
1fd38 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a has not been.**
1fd39 20 6e 61 6d 65 20 72 65 73 6f 6c 76 65 64 2e 0a name resolved..
1fd3a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 70 6f 69 **.** At the poi
1fd3b 6e 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 nt this routine
1fd3c 69 73 20 63 61 6c 6c 65 64 2c 20 77 65 20 61 6c is called, we al
1fd3d 72 65 61 64 79 20 6b 6e 6f 77 20 74 68 61 74 20 ready know that
1fd3e 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 the.** ORDER BY
1fd3f 74 65 72 6d 20 69 73 20 6e 6f 74 20 61 6e 20 69 term is not an i
1fd40 6e 74 65 67 65 72 20 69 6e 64 65 78 20 69 6e 74 nteger index int
1fd41 6f 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 o the result set
1fd42 2e 20 20 54 68 61 74 0a 2a 2a 20 63 61 73 65 20 . That.** case
1fd43 69 73 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 is handled by th
1fd44 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e e calling routin
1fd45 65 2e 0a 2a 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 e..**.** Attempt
1fd46 20 74 6f 20 6d 61 74 63 68 20 70 45 20 61 67 61 to match pE aga
1fd47 69 6e 73 74 20 72 65 73 75 6c 74 20 73 65 74 20 inst result set
1fd48 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6c columns in the l
1fd49 65 66 74 2d 6d 6f 73 74 0a 2a 2a 20 53 45 4c 45 eft-most.** SELE
1fd4a 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 CT statement. R
1fd4b 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 eturn the index
1fd4c 69 20 6f 66 20 74 68 65 20 6d 61 74 63 68 69 6e i of the matchin
1fd4d 67 20 63 6f 6c 75 6d 6e 2c 0a 2a 2a 20 61 73 20 g column,.** as
1fd4e 61 6e 20 69 6e 64 69 63 61 74 69 6f 6e 20 74 6f an indication to
1fd4f 20 74 68 65 20 63 61 6c 6c 65 72 20 74 68 61 74 the caller that
1fd50 20 69 74 20 73 68 6f 75 6c 64 20 73 6f 72 74 20 it should sort
1fd51 62 79 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 by the i-th colu
1fd52 6d 6e 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d mn..** The left-
1fd53 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 31 most column is 1
1fd54 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 . In other word
1fd55 73 2c 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 s, the value ret
1fd56 75 72 6e 65 64 20 69 73 20 74 68 65 0a 2a 2a 20 urned is the.**
1fd57 73 61 6d 65 20 69 6e 74 65 67 65 72 20 76 61 6c same integer val
1fd58 75 65 20 74 68 61 74 20 77 6f 75 6c 64 20 62 65 ue that would be
1fd59 20 75 73 65 64 20 69 6e 20 74 68 65 20 53 51 4c used in the SQL
1fd5a 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 69 6e statement to in
1fd5b 64 69 63 61 74 65 0a 2a 2a 20 74 68 65 20 63 6f dicate.** the co
1fd5c 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 lumn..**.** If t
1fd5d 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 here is no match
1fd5e 2c 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 , return 0. Ret
1fd5f 75 72 6e 20 2d 31 20 69 66 20 61 6e 20 65 72 72 urn -1 if an err
1fd60 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 or occurs..*/.st
1fd61 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 atic int resolve
1fd62 4f 72 64 65 72 42 79 54 65 72 6d 54 6f 45 78 70 OrderByTermToExp
1fd63 72 4c 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a rList(. Parse *
1fd64 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 pParse, /* P
1fd65 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 arsing context f
1fd66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 or error message
1fd67 73 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 s */. Select *p
1fd68 53 65 6c 65 63 74 2c 20 20 20 2f 2a 20 54 68 65 Select, /* The
1fd69 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1fd6a 74 20 77 69 74 68 20 74 68 65 20 4f 52 44 45 52 t with the ORDER
1fd6b 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 BY clause */.
1fd6c 45 78 70 72 20 2a 70 45 20 20 20 20 20 20 20 20 Expr *pE
1fd6d 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 66 /* The specif
1fd6e 69 63 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d ic ORDER BY term
1fd6f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 */.){. int i;
1fd70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1fd71 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
1fd72 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 ExprList *pELis
1fd73 74 3b 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d t; /* The colum
1fd74 6e 73 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 ns of the result
1fd75 20 73 65 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f set */. NameCo
1fd76 6e 74 65 78 74 20 6e 63 3b 20 20 20 20 2f 2a 20 ntext nc; /*
1fd77 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 Name context for
1fd78 20 72 65 73 6f 6c 76 69 6e 67 20 70 45 20 2a 2f resolving pE */
1fd79 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
1fd7a 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 te3ExprIsInteger
1fd7b 28 70 45 2c 20 26 69 29 3d 3d 30 20 29 3b 0a 20 (pE, &i)==0 );.
1fd7c 20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 pEList = pSelec
1fd7d 74 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a t->pEList;.. /*
1fd7e 20 52 65 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d Resolve all nam
1fd7f 65 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 es in the ORDER
1fd80 42 59 20 74 65 72 6d 20 65 78 70 72 65 73 73 69 BY term expressi
1fd81 6f 6e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 on. */. memset
1fd82 28 26 6e 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 (&nc, 0, sizeof(
1fd83 6e 63 29 29 3b 0a 20 20 6e 63 2e 70 50 61 72 73 nc));. nc.pPars
1fd84 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 6e 63 e = pParse;. nc
1fd85 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53 65 6c .pSrcList = pSel
1fd86 65 63 74 2d 3e 70 53 72 63 3b 0a 20 20 6e 63 2e ect->pSrc;. nc.
1fd87 70 45 4c 69 73 74 20 3d 20 70 45 4c 69 73 74 3b pEList = pEList;
1fd88 0a 20 20 6e 63 2e 61 6c 6c 6f 77 41 67 67 20 3d . nc.allowAgg =
1fd89 20 31 3b 0a 20 20 6e 63 2e 6e 45 72 72 20 3d 20 1;. nc.nErr =
1fd8a 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 0;. if( sqlite3
1fd8b 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
1fd8c 28 26 6e 63 2c 20 70 45 29 20 29 7b 0a 20 20 20 (&nc, pE) ){.
1fd8d 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 sqlite3ErrorCle
1fd8e 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 ar(pParse);.
1fd8f 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 return 0;. }..
1fd90 20 2f 2a 20 54 72 79 20 74 6f 20 6d 61 74 63 68 /* Try to match
1fd91 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 65 78 the ORDER BY ex
1fd92 70 72 65 73 73 69 6f 6e 20 61 67 61 69 6e 73 74 pression against
1fd93 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 an expression.
1fd94 20 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c ** in the resul
1fd95 74 20 73 65 74 2e 20 20 52 65 74 75 72 6e 20 61 t set. Return a
1fd96 6e 20 31 2d 62 61 73 65 64 20 69 6e 64 65 78 20 n 1-based index
1fd97 6f 66 20 74 68 65 20 6d 61 74 63 68 69 6e 67 0a of the matching.
1fd98 20 20 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 ** result-set
1fd99 65 6e 74 72 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f entry.. */. fo
1fd9a 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d r(i=0; i<pEList-
1fd9b 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 >nExpr; i++){.
1fd9c 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 if( sqlite3Exp
1fd9d 72 43 6f 6d 70 61 72 65 28 70 45 4c 69 73 74 2d rCompare(pEList-
1fd9e 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 70 45 29 >a[i].pExpr, pE)
1fd9f 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1fda0 20 69 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a i+1;. }. }.
1fda1 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 . /* If no matc
1fda2 68 2c 20 72 65 74 75 72 6e 20 30 2e 20 2a 2f 0a h, return 0. */.
1fda3 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
1fda4 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e *.** Generate an
1fda5 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f ORDER BY or GRO
1fda6 55 50 20 42 59 20 74 65 72 6d 20 6f 75 74 2d 6f UP BY term out-o
1fda7 66 2d 72 61 6e 67 65 20 65 72 72 6f 72 2e 0a 2a f-range error..*
1fda8 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 /.static void re
1fda9 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 solveOutOfRangeE
1fdaa 72 72 6f 72 28 0a 20 20 50 61 72 73 65 20 2a 70 rror(. Parse *p
1fdab 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f Parse, /
1fdac 2a 20 54 68 65 20 65 72 72 6f 72 20 63 6f 6e 74 * The error cont
1fdad 65 78 74 20 69 6e 74 6f 20 77 68 69 63 68 20 74 ext into which t
1fdae 6f 20 77 72 69 74 65 20 74 68 65 20 65 72 72 6f o write the erro
1fdaf 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 r */. const cha
1fdb0 72 20 2a 7a 54 79 70 65 2c 20 20 20 20 20 2f 2a r *zType, /*
1fdb1 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f "ORDER" or "GRO
1fdb2 55 50 22 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 UP" */. int i,
1fdb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fdb4 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 28 31 2d /* The index (1-
1fdb5 62 61 73 65 64 29 20 6f 66 20 74 68 65 20 74 65 based) of the te
1fdb6 72 6d 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 rm out of range
1fdb7 2a 2f 0a 20 20 69 6e 74 20 6d 78 20 20 20 20 20 */. int mx
1fdb8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1fdb9 61 72 67 65 73 74 20 70 65 72 6d 69 73 73 69 62 argest permissib
1fdba 6c 65 20 76 61 6c 75 65 20 6f 66 20 69 20 2a 2f le value of i */
1fdbb 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 .){. sqlite3Err
1fdbc 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 orMsg(pParse, .
1fdbd 20 20 20 22 25 72 20 25 73 20 42 59 20 74 65 72 "%r %s BY ter
1fdbe 6d 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2d m out of range -
1fdbf 20 73 68 6f 75 6c 64 20 62 65 20 22 0a 20 20 20 should be ".
1fdc0 20 22 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 "between 1 and
1fdc1 25 64 22 2c 20 69 2c 20 7a 54 79 70 65 2c 20 6d %d", i, zType, m
1fdc2 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 x);.}../*.** Ana
1fdc3 6c 79 7a 65 20 74 68 65 20 4f 52 44 45 52 20 42 lyze the ORDER B
1fdc4 59 20 63 6c 61 75 73 65 20 69 6e 20 61 20 63 6f Y clause in a co
1fdc5 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74 mpound SELECT st
1fdc6 61 74 65 6d 65 6e 74 2e 20 20 20 4d 6f 64 69 66 atement. Modif
1fdc7 79 0a 2a 2a 20 65 61 63 68 20 74 65 72 6d 20 6f y.** each term o
1fdc8 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 f the ORDER BY c
1fdc9 6c 61 75 73 65 20 69 73 20 61 20 63 6f 6e 73 74 lause is a const
1fdca 61 6e 74 20 69 6e 74 65 67 65 72 20 62 65 74 77 ant integer betw
1fdcb 65 65 6e 20 31 0a 2a 2a 20 61 6e 64 20 4e 20 77 een 1.** and N w
1fdcc 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 here N is the nu
1fdcd 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
1fdce 69 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 in the compound
1fdcf 53 45 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a 20 4f 52 SELECT..**.** OR
1fdd0 44 45 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 DER BY terms tha
1fdd1 74 20 61 72 65 20 61 6c 72 65 61 64 79 20 61 6e t are already an
1fdd2 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e integer between
1fdd3 20 31 20 61 6e 64 20 4e 20 61 72 65 0a 2a 2a 20 1 and N are.**
1fdd4 75 6e 6d 6f 64 69 66 69 65 64 2e 20 20 4f 52 44 unmodified. ORD
1fdd5 45 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 ER BY terms that
1fdd6 20 61 72 65 20 69 6e 74 65 67 65 72 73 20 6f 75 are integers ou
1fdd7 74 73 69 64 65 20 74 68 65 20 72 61 6e 67 65 20 tside the range
1fdd8 6f 66 0a 2a 2a 20 31 20 74 68 72 6f 75 67 68 20 of.** 1 through
1fdd9 4e 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 N generate an er
1fdda 72 6f 72 2e 20 20 4f 52 44 45 52 20 42 59 20 74 ror. ORDER BY t
1fddb 65 72 6d 73 20 74 68 61 74 20 61 72 65 20 65 78 erms that are ex
1fddc 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 pressions.** are
1fddd 20 6d 61 74 63 68 65 64 20 61 67 61 69 6e 73 74 matched against
1fdde 20 72 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 result set expr
1fddf 65 73 73 69 6f 6e 73 20 6f 66 20 63 6f 6d 70 6f essions of compo
1fde0 75 6e 64 20 53 45 4c 45 43 54 0a 2a 2a 20 62 65 und SELECT.** be
1fde1 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 74 68 65 ginning with the
1fde2 20 6c 65 66 74 2d 6d 6f 73 74 20 53 45 4c 45 43 left-most SELEC
1fde3 54 20 61 6e 64 20 77 6f 72 6b 69 6e 67 20 74 6f T and working to
1fde4 77 61 72 64 20 74 68 65 20 72 69 67 68 74 2e 0a ward the right..
1fde5 2a 2a 20 41 74 20 74 68 65 20 66 69 72 73 74 20 ** At the first
1fde6 6d 61 74 63 68 2c 20 74 68 65 20 4f 52 44 45 52 match, the ORDER
1fde7 20 42 59 20 65 78 70 72 65 73 73 69 6f 6e 20 69 BY expression i
1fde8 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e s transformed in
1fde9 74 6f 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 to.** the intege
1fdea 72 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e r column number.
1fdeb 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 .**.** Return th
1fdec 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f e number of erro
1fded 72 73 20 73 65 65 6e 2e 0a 2a 2f 0a 73 74 61 74 rs seen..*/.stat
1fdee 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 43 6f ic int resolveCo
1fdef 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79 28 0a 20 mpoundOrderBy(.
1fdf0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
1fdf1 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e /* Parsin
1fdf2 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 g context. Leav
1fdf3 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 e error messages
1fdf4 20 68 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 here */. Selec
1fdf5 74 20 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 t *pSelect
1fdf6 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 /* The SELECT s
1fdf7 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e tatement contain
1fdf8 69 6e 67 20 74 68 65 20 4f 52 44 45 52 20 42 59 ing the ORDER BY
1fdf9 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a */.){. int i;.
1fdfa 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 ExprList *pOrd
1fdfb 65 72 42 79 3b 0a 20 20 45 78 70 72 4c 69 73 74 erBy;. ExprList
1fdfc 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 71 6c 69 *pEList;. sqli
1fdfd 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 6d te3 *db;. int m
1fdfe 6f 72 65 54 6f 44 6f 20 3d 20 31 3b 0a 0a 20 20 oreToDo = 1;..
1fdff 70 4f 72 64 65 72 42 79 20 3d 20 70 53 65 6c 65 pOrderBy = pSele
1fe00 63 74 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 ct->pOrderBy;.
1fe01 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 if( pOrderBy==0
1fe02 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 ) return 0;. db
1fe03 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 = pParse->db;.#
1fe04 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f if SQLITE_MAX_CO
1fe05 4c 55 4d 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 LUMN. if( pOrde
1fe06 72 42 79 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 rBy->nExpr>db->a
1fe07 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
1fe08 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 IT_COLUMN] ){.
1fe09 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1fe0a 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d g(pParse, "too m
1fe0b 61 6e 79 20 74 65 72 6d 73 20 69 6e 20 4f 52 44 any terms in ORD
1fe0c 45 52 20 42 59 20 63 6c 61 75 73 65 22 29 3b 0a ER BY clause");.
1fe0d 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
1fe0e 7d 0a 23 65 6e 64 69 66 0a 20 20 66 6f 72 28 69 }.#endif. for(i
1fe0f 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e =0; i<pOrderBy->
1fe10 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 nExpr; i++){.
1fe11 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e pOrderBy->a[i].
1fe12 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 done = 0;. }.
1fe13 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78 74 20 3d pSelect->pNext =
1fe14 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 70 53 65 0;. while( pSe
1fe15 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20 29 7b 0a lect->pPrior ){.
1fe16 20 20 20 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 pSelect->pPr
1fe17 69 6f 72 2d 3e 70 4e 65 78 74 20 3d 20 70 53 65 ior->pNext = pSe
1fe18 6c 65 63 74 3b 0a 20 20 20 20 70 53 65 6c 65 63 lect;. pSelec
1fe19 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 t = pSelect->pPr
1fe1a 69 6f 72 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 ior;. }. while
1fe1b 28 20 70 53 65 6c 65 63 74 20 26 26 20 6d 6f 72 ( pSelect && mor
1fe1c 65 54 6f 44 6f 20 29 7b 0a 20 20 20 20 73 74 72 eToDo ){. str
1fe1d 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
1fe1e 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 6d 6f m *pItem;. mo
1fe1f 72 65 54 6f 44 6f 20 3d 20 30 3b 0a 20 20 20 20 reToDo = 0;.
1fe20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 pEList = pSelect
1fe21 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 61 73 ->pEList;. as
1fe22 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 sert( pEList!=0
1fe23 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 );. for(i=0,
1fe24 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e pItem=pOrderBy->
1fe25 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e a; i<pOrderBy->n
1fe26 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d Expr; i++, pItem
1fe27 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 ++){. int i
1fe28 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 Col = -1;.
1fe29 45 78 70 72 20 2a 70 45 2c 20 2a 70 44 75 70 3b Expr *pE, *pDup;
1fe2a 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d . if( pItem
1fe2b 2d 3e 64 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 ->done ) continu
1fe2c 65 3b 0a 20 20 20 20 20 20 70 45 20 3d 20 70 49 e;. pE = pI
1fe2d 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 tem->pExpr;.
1fe2e 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 if( sqlite3Exp
1fe2f 72 49 73 49 6e 74 65 67 65 72 28 70 45 2c 20 26 rIsInteger(pE, &
1fe30 69 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 iCol) ){.
1fe31 20 69 66 28 20 69 43 6f 6c 3c 3d 30 20 7c 7c 20 if( iCol<=0 ||
1fe32 69 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 iCol>pEList->nEx
1fe33 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 pr ){.
1fe34 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 resolveOutOfRang
1fe35 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 22 eError(pParse, "
1fe36 4f 52 44 45 52 22 2c 20 69 2b 31 2c 20 70 45 4c ORDER", i+1, pEL
1fe37 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 ist->nExpr);.
1fe38 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b return 1;
1fe39 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1fe3a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1fe3b 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73 iCol = resolveAs
1fe3c 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 45 4c Name(pParse, pEL
1fe3d 69 73 74 2c 20 70 45 29 3b 0a 20 20 20 20 20 20 ist, pE);.
1fe3e 20 20 69 66 28 20 69 43 6f 6c 3d 3d 30 20 29 7b if( iCol==0 ){
1fe3f 0a 20 20 20 20 20 20 20 20 20 20 70 44 75 70 20 . pDup
1fe40 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 = sqlite3ExprDup
1fe41 28 64 62 2c 20 70 45 2c 20 30 29 3b 0a 20 20 20 (db, pE, 0);.
1fe42 20 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e if( !db->
1fe43 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
1fe44 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 asse
1fe45 72 74 28 70 44 75 70 29 3b 0a 20 20 20 20 20 20 rt(pDup);.
1fe46 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 iCol = res
1fe47 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 72 6d 54 olveOrderByTermT
1fe48 6f 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 oExprList(pParse
1fe49 2c 20 70 53 65 6c 65 63 74 2c 20 70 44 75 70 29 , pSelect, pDup)
1fe4a 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1fe4b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
1fe4c 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 44 xprDelete(db, pD
1fe4d 75 70 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 up);. }.
1fe4e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
1fe4f 20 69 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20 iCol>0 ){.
1fe50 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c CollSeq *pCol
1fe51 6c 20 3d 20 70 45 2d 3e 70 43 6f 6c 6c 3b 0a 20 l = pE->pColl;.
1fe52 20 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 int flags
1fe53 20 3d 20 70 45 2d 3e 66 6c 61 67 73 20 26 20 45 = pE->flags & E
1fe54 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 P_ExpCollate;.
1fe55 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
1fe56 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 29 3b rDelete(db, pE);
1fe57 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e . pItem->
1fe58 70 45 78 70 72 20 3d 20 70 45 20 3d 20 73 71 6c pExpr = pE = sql
1fe59 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f ite3Expr(db, TK_
1fe5a 49 4e 54 45 47 45 52 2c 20 30 29 3b 0a 20 20 20 INTEGER, 0);.
1fe5b 20 20 20 20 20 69 66 28 20 70 45 3d 3d 30 20 29 if( pE==0 )
1fe5c 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
1fe5d 20 20 20 70 45 2d 3e 70 43 6f 6c 6c 20 3d 20 70 pE->pColl = p
1fe5e 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 45 Coll;. pE
1fe5f 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e ->flags |= EP_In
1fe60 74 56 61 6c 75 65 20 7c 20 66 6c 61 67 73 3b 0a tValue | flags;.
1fe61 20 20 20 20 20 20 20 20 70 45 2d 3e 75 2e 69 56 pE->u.iV
1fe62 61 6c 75 65 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 alue = iCol;.
1fe63 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c pItem->iCol
1fe64 20 3d 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20 = (u16)iCol;.
1fe65 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 64 6f 6e pItem->don
1fe66 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c e = 1;. }el
1fe67 73 65 7b 0a 20 20 20 20 20 20 20 20 6d 6f 72 65 se{. more
1fe68 54 6f 44 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 ToDo = 1;.
1fe69 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 65 6c }. }. pSel
1fe6a 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 ect = pSelect->p
1fe6b 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 Next;. }. for(
1fe6c 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d i=0; i<pOrderBy-
1fe6d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 >nExpr; i++){.
1fe6e 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e if( pOrderBy->
1fe6f 61 5b 69 5d 2e 64 6f 6e 65 3d 3d 30 20 29 7b 0a a[i].done==0 ){.
1fe70 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1fe71 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 orMsg(pParse, "%
1fe72 72 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 r ORDER BY term
1fe73 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 61 does not match a
1fe74 6e 79 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 ny ".
1fe75 20 22 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 "column in the
1fe76 72 65 73 75 6c 74 20 73 65 74 22 2c 20 69 2b 31 result set", i+1
1fe77 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1fe78 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 1;. }. }. r
1fe79 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
1fe7a 2a 20 43 68 65 63 6b 20 65 76 65 72 79 20 74 65 * Check every te
1fe7b 72 6d 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 rm in the ORDER
1fe7c 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 BY or GROUP BY c
1fe7d 6c 61 75 73 65 20 70 4f 72 64 65 72 42 79 20 6f lause pOrderBy o
1fe7e 66 0a 2a 2a 20 74 68 65 20 53 45 4c 45 43 54 20 f.** the SELECT
1fe7f 73 74 61 74 65 6d 65 6e 74 20 70 53 65 6c 65 63 statement pSelec
1fe80 74 2e 20 20 49 66 20 61 6e 79 20 74 65 72 6d 20 t. If any term
1fe81 69 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 is reference to
1fe82 61 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20 a.** result set
1fe83 65 78 70 72 65 73 73 69 6f 6e 20 28 61 73 20 64 expression (as d
1fe84 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 etermined by the
1fe85 20 45 78 70 72 4c 69 73 74 2e 61 2e 69 43 6f 6c ExprList.a.iCol
1fe86 20 66 69 65 6c 64 29 0a 2a 2a 20 74 68 65 6e 20 field).** then
1fe87 63 6f 6e 76 65 72 74 20 74 68 61 74 20 74 65 72 convert that ter
1fe88 6d 20 69 6e 74 6f 20 61 20 63 6f 70 79 20 6f 66 m into a copy of
1fe89 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
1fe8a 6e 67 20 72 65 73 75 6c 74 20 73 65 74 0a 2a 2a ng result set.**
1fe8b 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 column..**.** I
1fe8c 66 20 61 6e 79 20 65 72 72 6f 72 73 20 61 72 65 f any errors are
1fe8d 20 64 65 74 65 63 74 65 64 2c 20 61 64 64 20 61 detected, add a
1fe8e 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 n error message
1fe8f 74 6f 20 70 50 61 72 73 65 20 61 6e 64 0a 2a 2a to pParse and.**
1fe90 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f return non-zero
1fe91 2e 20 20 52 65 74 75 72 6e 20 7a 65 72 6f 20 69 . Return zero i
1fe92 66 20 6e 6f 20 65 72 72 6f 72 73 20 61 72 65 20 f no errors are
1fe93 73 65 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f seen..*/.SQLITE_
1fe94 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1fe95 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 te3ResolveOrderG
1fe96 72 6f 75 70 42 79 28 0a 20 20 50 61 72 73 65 20 roupBy(. Parse
1fe97 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 *pParse,
1fe98 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
1fe99 78 74 2e 20 20 4c 65 61 76 65 20 65 72 72 6f 72 xt. Leave error
1fe9a 20 6d 65 73 73 61 67 65 73 20 68 65 72 65 20 2a messages here *
1fe9b 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c /. Select *pSel
1fe9c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 ect, /* The
1fe9d 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1fe9e 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 t containing the
1fe9f 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 clause */. Exp
1fea0 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c rList *pOrderBy,
1fea1 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 /* The ORDER
1fea2 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 BY or GROUP BY c
1fea3 6c 61 75 73 65 20 74 6f 20 62 65 20 70 72 6f 63 lause to be proc
1fea4 65 73 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 essed */. const
1fea5 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20 20 char *zType
1fea6 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22 /* "ORDER" or "
1fea7 47 52 4f 55 50 22 20 2a 2f 0a 29 7b 0a 20 20 69 GROUP" */.){. i
1fea8 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 33 20 nt i;. sqlite3
1fea9 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
1feaa 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 ;. ExprList *pE
1feab 4c 69 73 74 3b 0a 20 20 73 74 72 75 63 74 20 45 List;. struct E
1feac 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 xprList_item *pI
1fead 74 65 6d 3b 0a 0a 20 20 69 66 28 20 70 4f 72 64 tem;.. if( pOrd
1feae 65 72 42 79 3d 3d 30 20 7c 7c 20 70 50 61 72 73 erBy==0 || pPars
1feaf 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 e->db->mallocFai
1feb0 6c 65 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a led ) return 0;.
1feb1 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 #if SQLITE_MAX_C
1feb2 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 4f 72 64 OLUMN. if( pOrd
1feb3 65 72 42 79 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e erBy->nExpr>db->
1feb4 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
1feb5 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 MIT_COLUMN] ){.
1feb6 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1feb7 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 sg(pParse, "too
1feb8 6d 61 6e 79 20 74 65 72 6d 73 20 69 6e 20 25 73 many terms in %s
1feb9 20 42 59 20 63 6c 61 75 73 65 22 2c 20 7a 54 79 BY clause", zTy
1feba 70 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 pe);. return
1febb 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 1;. }.#endif.
1febc 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 pEList = pSelect
1febd 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 61 73 73 65 ->pEList;. asse
1febe 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b rt( pEList!=0 );
1febf 20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 /* sqlite3Sele
1fec0 63 74 4e 65 77 28 29 20 67 75 61 72 61 6e 74 65 ctNew() guarante
1fec1 65 73 20 74 68 69 73 20 2a 2f 0a 20 20 66 6f 72 es this */. for
1fec2 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 (i=0, pItem=pOrd
1fec3 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 erBy->a; i<pOrde
1fec4 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c rBy->nExpr; i++,
1fec5 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 pItem++){. i
1fec6 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 29 f( pItem->iCol )
1fec7 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 {. if( pIte
1fec8 6d 2d 3e 69 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e m->iCol>pEList->
1fec9 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 nExpr ){.
1feca 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e resolveOutOfRan
1fecb 67 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 geError(pParse,
1fecc 7a 54 79 70 65 2c 20 69 2b 31 2c 20 70 45 4c 69 zType, i+1, pELi
1fecd 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 st->nExpr);.
1fece 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
1fecf 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 73 6f }. reso
1fed0 6c 76 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c lveAlias(pParse,
1fed1 20 70 45 4c 69 73 74 2c 20 70 49 74 65 6d 2d 3e pEList, pItem->
1fed2 69 43 6f 6c 2d 31 2c 20 70 49 74 65 6d 2d 3e 70 iCol-1, pItem->p
1fed3 45 78 70 72 2c 20 7a 54 79 70 65 29 3b 0a 20 20 Expr, zType);.
1fed4 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1fed5 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 4f 72 0;.}../*.** pOr
1fed6 64 65 72 42 79 20 69 73 20 61 6e 20 4f 52 44 45 derBy is an ORDE
1fed7 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 R BY or GROUP BY
1fed8 20 63 6c 61 75 73 65 20 69 6e 20 53 45 4c 45 43 clause in SELEC
1fed9 54 20 73 74 61 74 65 6d 65 6e 74 20 70 53 65 6c T statement pSel
1feda 65 63 74 2e 0a 2a 2a 20 54 68 65 20 4e 61 6d 65 ect..** The Name
1fedb 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 context of the
1fedc 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
1fedd 20 69 73 20 70 4e 43 2e 20 20 7a 54 79 70 65 20 is pNC. zType
1fede 69 73 20 65 69 74 68 65 72 0a 2a 2a 20 22 4f 52 is either.** "OR
1fedf 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 20 DER" or "GROUP"
1fee0 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 69 depending on whi
1fee1 63 68 20 74 79 70 65 20 6f 66 20 63 6c 61 75 73 ch type of claus
1fee2 65 20 70 4f 72 64 65 72 42 79 20 69 73 2e 0a 2a e pOrderBy is..*
1fee3 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1fee4 65 20 72 65 73 6f 6c 76 65 73 20 65 61 63 68 20 e resolves each
1fee5 74 65 72 6d 20 6f 66 20 74 68 65 20 63 6c 61 75 term of the clau
1fee6 73 65 20 69 6e 74 6f 20 61 6e 20 65 78 70 72 65 se into an expre
1fee7 73 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 ssion..** If the
1fee8 20 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 69 order-by term i
1fee9 73 20 61 6e 20 69 6e 74 65 67 65 72 20 49 20 62 s an integer I b
1feea 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 28 etween 1 and N (
1feeb 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a where N is the.*
1feec 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 * number of colu
1feed 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c mns in the resul
1feee 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c t set of the SEL
1feef 45 43 54 29 20 74 68 65 6e 20 74 68 65 20 65 78 ECT) then the ex
1fef0 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 20 74 pression.** in t
1fef1 68 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 69 73 he resolution is
1fef2 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 49 a copy of the I
1fef3 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 65 -th result-set e
1fef4 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 66 0a 2a xpression. If.*
1fef5 2a 20 74 68 65 20 6f 72 64 65 72 2d 62 79 20 74 * the order-by t
1fef6 65 72 6d 20 69 73 20 61 6e 20 69 64 65 6e 74 69 erm is an identi
1fef7 66 79 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f fy that correspo
1fef8 6e 64 73 20 74 6f 20 74 68 65 20 41 53 2d 6e 61 nds to the AS-na
1fef9 6d 65 20 6f 66 0a 2a 2a 20 61 20 72 65 73 75 6c me of.** a resul
1fefa 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e t-set expression
1fefb 2c 20 74 68 65 6e 20 74 68 65 20 74 65 72 6d 20 , then the term
1fefc 72 65 73 6f 6c 76 65 73 20 74 6f 20 61 20 63 6f resolves to a co
1fefd 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 73 py of the.** res
1fefe 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 ult-set expressi
1feff 6f 6e 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 on. Otherwise,
1ff00 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
1ff01 73 20 72 65 73 6f 6c 76 65 64 20 69 6e 0a 2a 2a s resolved in.**
1ff02 20 74 68 65 20 75 73 75 61 6c 20 77 61 79 20 2d the usual way -
1ff03 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 52 65 using sqlite3Re
1ff04 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 solveExprNames()
1ff05 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1ff06 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 tine returns the
1ff07 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 number of error
1ff08 73 2e 20 20 49 66 20 65 72 72 6f 72 73 20 6f 63 s. If errors oc
1ff09 63 75 72 2c 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 cur, then.** an
1ff0a 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f appropriate erro
1ff0b 72 20 6d 65 73 73 61 67 65 20 6d 69 67 68 74 20 r message might
1ff0c 62 65 20 6c 65 66 74 20 69 6e 20 70 50 61 72 73 be left in pPars
1ff0d 65 2e 20 20 28 4f 4f 4d 20 65 72 72 6f 72 73 0a e. (OOM errors.
1ff0e 2a 2a 20 65 78 63 65 70 74 65 64 2e 29 0a 2a 2f ** excepted.).*/
1ff0f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f .static int reso
1ff10 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 lveOrderGroupBy(
1ff11 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a . NameContext *
1ff12 70 4e 43 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 pNC, /* The
1ff13 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 name context of
1ff14 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 the SELECT state
1ff15 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 ment */. Select
1ff16 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 *pSelect,
1ff17 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 /* The SELECT st
1ff18 61 74 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 atement holding
1ff19 70 4f 72 64 65 72 42 79 20 2a 2f 0a 20 20 45 78 pOrderBy */. Ex
1ff1a 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 prList *pOrderBy
1ff1b 2c 20 20 20 2f 2a 20 41 6e 20 4f 52 44 45 52 20 , /* An ORDER
1ff1c 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 BY or GROUP BY c
1ff1d 6c 61 75 73 65 20 74 6f 20 72 65 73 6f 6c 76 65 lause to resolve
1ff1e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
1ff1f 20 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20 45 *zType /* E
1ff20 69 74 68 65 72 20 22 4f 52 44 45 52 22 20 6f 72 ither "ORDER" or
1ff21 20 22 47 52 4f 55 50 22 2c 20 61 73 20 61 70 70 "GROUP", as app
1ff22 72 6f 70 72 69 61 74 65 20 2a 2f 0a 29 7b 0a 20 ropriate */.){.
1ff23 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
1ff24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ff25 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
1ff26 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 */. int iCol;
1ff27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ff28 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 /* Column nu
1ff29 6d 62 65 72 20 2a 2f 0a 20 20 73 74 72 75 63 74 mber */. struct
1ff2a 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
1ff2b 70 49 74 65 6d 3b 20 20 20 2f 2a 20 41 20 74 65 pItem; /* A te
1ff2c 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 rm of the ORDER
1ff2d 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 50 BY clause */. P
1ff2e 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 arse *pParse;
1ff2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ff30 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 Parsing context
1ff31 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73 75 6c */. int nResul
1ff32 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
1ff33 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1ff34 66 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 72 f terms in the r
1ff35 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 0a 20 20 esult set */..
1ff36 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 if( pOrderBy==0
1ff37 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 52 ) return 0;. nR
1ff38 65 73 75 6c 74 20 3d 20 70 53 65 6c 65 63 74 2d esult = pSelect-
1ff39 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a >pEList->nExpr;.
1ff3a 20 20 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e pParse = pNC->
1ff3b 70 50 61 72 73 65 3b 0a 20 20 66 6f 72 28 69 3d pParse;. for(i=
1ff3c 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 0, pItem=pOrderB
1ff3d 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 y->a; i<pOrderBy
1ff3e 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 ->nExpr; i++, pI
1ff3f 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 tem++){. Expr
1ff40 20 2a 70 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 *pE = pItem->pE
1ff41 78 70 72 3b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 xpr;. iCol =
1ff42 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65 28 70 50 resolveAsName(pP
1ff43 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2d 3e 70 arse, pSelect->p
1ff44 45 4c 69 73 74 2c 20 70 45 29 3b 0a 20 20 20 20 EList, pE);.
1ff45 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b 0a 20 20 if( iCol>0 ){.
1ff46 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 41 53 2d /* If an AS-
1ff47 6e 61 6d 65 20 6d 61 74 63 68 20 69 73 20 66 6f name match is fo
1ff48 75 6e 64 2c 20 6d 61 72 6b 20 74 68 69 73 20 4f und, mark this O
1ff49 52 44 45 52 20 42 59 20 63 6f 6c 75 6d 6e 20 61 RDER BY column a
1ff4a 73 20 62 65 69 6e 67 0a 20 20 20 20 20 20 2a 2a s being. **
1ff4b 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69 a copy of the i
1ff4c 43 6f 6c 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 Col-th result-se
1ff4d 74 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 73 t column. The s
1ff4e 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 ubsequent call t
1ff4f 6f 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 o. ** sqlit
1ff50 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 e3ResolveOrderGr
1ff51 6f 75 70 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e oupBy() will con
1ff52 76 65 72 74 20 74 68 65 20 65 78 70 72 65 73 73 vert the express
1ff53 69 6f 6e 20 74 6f 20 61 0a 20 20 20 20 20 20 2a ion to a. *
1ff54 2a 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69 43 * copy of the iC
1ff55 6f 6c 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74 ol-th result-set
1ff56 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 2a 2f 0a expression. */.
1ff57 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f pItem->iCo
1ff58 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20 l = (u16)iCol;.
1ff59 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 continue;.
1ff5a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c }. if( sql
1ff5b 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 ite3ExprIsIntege
1ff5c 72 28 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a r(pE, &iCol) ){.
1ff5d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 /* The ORD
1ff5e 45 52 20 42 59 20 74 65 72 6d 20 69 73 20 61 6e ER BY term is an
1ff5f 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e integer constan
1ff60 74 2e 20 20 41 67 61 69 6e 2c 20 73 65 74 20 74 t. Again, set t
1ff61 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 he column.
1ff62 2a 2a 20 6e 75 6d 62 65 72 20 73 6f 20 74 68 61 ** number so tha
1ff63 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 t sqlite3Resolve
1ff64 4f 72 64 65 72 47 72 6f 75 70 42 79 28 29 20 77 OrderGroupBy() w
1ff65 69 6c 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 0a ill convert the.
1ff66 20 20 20 20 20 20 2a 2a 20 6f 72 64 65 72 2d 62 ** order-b
1ff67 79 20 74 65 72 6d 20 74 6f 20 61 20 63 6f 70 79 y term to a copy
1ff68 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 of the result-s
1ff69 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f et expression */
1ff6a 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c . if( iCol<
1ff6b 31 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73 1 ){. res
1ff6c 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 olveOutOfRangeEr
1ff6d 72 6f 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70 ror(pParse, zTyp
1ff6e 65 2c 20 69 2b 31 2c 20 6e 52 65 73 75 6c 74 29 e, i+1, nResult)
1ff6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
1ff70 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 1;. }.
1ff71 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 pItem->iCol =
1ff72 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 (u16)iCol;.
1ff73 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d continue;. }
1ff74 0a 0a 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 .. /* Otherwi
1ff75 73 65 2c 20 74 72 65 61 74 20 74 68 65 20 4f 52 se, treat the OR
1ff76 44 45 52 20 42 59 20 74 65 72 6d 20 61 73 20 61 DER BY term as a
1ff77 6e 20 6f 72 64 69 6e 61 72 79 20 65 78 70 72 65 n ordinary expre
1ff78 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 70 49 74 ssion */. pIt
1ff79 65 6d 2d 3e 69 43 6f 6c 20 3d 20 30 3b 0a 20 20 em->iCol = 0;.
1ff7a 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 if( sqlite3Res
1ff7b 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 70 4e olveExprNames(pN
1ff7c 43 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 20 20 C, pE) ){.
1ff7d 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
1ff7e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c }. return sql
1ff7f 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 ite3ResolveOrder
1ff80 47 72 6f 75 70 42 79 28 70 50 61 72 73 65 2c 20 GroupBy(pParse,
1ff81 70 53 65 6c 65 63 74 2c 20 70 4f 72 64 65 72 42 pSelect, pOrderB
1ff82 79 2c 20 7a 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a y, zType);.}../*
1ff83 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 .** Resolve name
1ff84 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 s in the SELECT
1ff85 73 74 61 74 65 6d 65 6e 74 20 70 20 61 6e 64 20 statement p and
1ff86 61 6c 6c 20 6f 66 20 69 74 73 20 64 65 73 63 65 all of its desce
1ff87 6e 64 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 ndents..*/.stati
1ff88 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 53 65 6c c int resolveSel
1ff89 65 63 74 53 74 65 70 28 57 61 6c 6b 65 72 20 2a ectStep(Walker *
1ff8a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 pWalker, Select
1ff8b 2a 70 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 *p){. NameConte
1ff8c 78 74 20 2a 70 4f 75 74 65 72 4e 43 3b 20 20 2f xt *pOuterNC; /
1ff8d 2a 20 43 6f 6e 74 65 78 74 20 74 68 61 74 20 63 * Context that c
1ff8e 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 53 45 4c ontains this SEL
1ff8f 45 43 54 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e ECT */. NameCon
1ff90 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20 text sNC;
1ff91 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 /* Name context
1ff92 20 6f 66 20 74 68 69 73 20 53 45 4c 45 43 54 20 of this SELECT
1ff93 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f 6d 70 6f */. int isCompo
1ff94 75 6e 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 und; /*
1ff95 54 72 75 65 20 69 66 20 70 20 69 73 20 61 20 63 True if p is a c
1ff96 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 2a ompound select *
1ff97 2f 0a 20 20 69 6e 74 20 6e 43 6f 6d 70 6f 75 6e /. int nCompoun
1ff98 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e d; /* N
1ff99 75 6d 62 65 72 20 6f 66 20 63 6f 6d 70 6f 75 6e umber of compoun
1ff9a 64 20 74 65 72 6d 73 20 70 72 6f 63 65 73 73 65 d terms processe
1ff9b 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 50 61 d so far */. Pa
1ff9c 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 rse *pParse;
1ff9d 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 /* Parsing
1ff9e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 context */. Ex
1ff9f 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 prList *pEList;
1ffa0 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 /* Result
1ffa1 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c set expression l
1ffa2 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 ist */. int i;
1ffa3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ffa4 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
1ffa5 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
1ffa6 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20 2f 2a pGroupBy; /*
1ffa7 20 54 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c The GROUP BY cl
1ffa8 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 ause */. Select
1ffa9 20 2a 70 4c 65 66 74 6d 6f 73 74 3b 20 20 20 20 *pLeftmost;
1ffaa 20 20 2f 2a 20 4c 65 66 74 2d 6d 6f 73 74 20 6f /* Left-most o
1ffab 66 20 53 45 4c 45 43 54 20 6f 66 20 61 20 63 6f f SELECT of a co
1ffac 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 73 71 6c 69 mpound */. sqli
1ffad 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
1ffae 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 /* Database
1ffaf 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 connection */.
1ffb0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 .. assert( p!=0
1ffb1 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c );. if( p->sel
1ffb2 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f 6c Flags & SF_Resol
1ffb3 76 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ved ){. retur
1ffb4 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d n WRC_Prune;. }
1ffb5 0a 20 20 70 4f 75 74 65 72 4e 43 20 3d 20 70 57 . pOuterNC = pW
1ffb6 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 alker->u.pNC;.
1ffb7 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 pParse = pWalker
1ffb8 2d 3e 70 50 61 72 73 65 3b 0a 20 20 64 62 20 3d ->pParse;. db =
1ffb9 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 pParse->db;..
1ffba 2f 2a 20 4e 6f 72 6d 61 6c 6c 79 20 73 71 6c 69 /* Normally sqli
1ffbb 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 te3SelectExpand(
1ffbc 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 ) will be called
1ffbd 20 66 69 72 73 74 20 61 6e 64 20 77 69 6c 6c 20 first and will
1ffbe 68 61 76 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 have. ** alread
1ffbf 79 20 65 78 70 61 6e 64 65 64 20 74 68 69 73 20 y expanded this
1ffc0 53 45 4c 45 43 54 2e 20 20 48 6f 77 65 76 65 72 SELECT. However
1ffc1 2c 20 69 66 20 74 68 69 73 20 69 73 20 61 20 73 , if this is a s
1ffc2 75 62 71 75 65 72 79 20 77 69 74 68 69 6e 0a 20 ubquery within.
1ffc3 20 2a 2a 20 61 6e 20 65 78 70 72 65 73 73 69 6f ** an expressio
1ffc4 6e 2c 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 n, sqlite3Resolv
1ffc5 65 45 78 70 72 4e 61 6d 65 73 28 29 20 77 69 6c eExprNames() wil
1ffc6 6c 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68 l be called with
1ffc7 6f 75 74 20 61 0a 20 20 2a 2a 20 70 72 69 6f 72 out a. ** prior
1ffc8 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
1ffc9 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 2e 20 SelectExpand().
1ffca 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65 When that happe
1ffcb 6e 73 2c 20 6c 65 74 0a 20 20 2a 2a 20 73 71 6c ns, let. ** sql
1ffcc 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 29 ite3SelectPrep()
1ffcd 20 64 6f 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 do all of the p
1ffce 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 74 68 rocessing for th
1ffcf 69 73 20 53 45 4c 45 43 54 2e 0a 20 20 2a 2a 20 is SELECT.. **
1ffd0 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 sqlite3SelectPre
1ffd1 70 28 29 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 p() will invoke
1ffd2 62 6f 74 68 20 73 71 6c 69 74 65 33 53 65 6c 65 both sqlite3Sele
1ffd3 63 74 45 78 70 61 6e 64 28 29 20 61 6e 64 0a 20 ctExpand() and.
1ffd4 20 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ** this routine
1ffd5 20 69 6e 20 74 68 65 20 63 6f 72 72 65 63 74 20 in the correct
1ffd6 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 order.. */. if
1ffd7 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 ( (p->selFlags &
1ffd8 20 53 46 5f 45 78 70 61 6e 64 65 64 29 3d 3d 30 SF_Expanded)==0
1ffd9 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 ){. sqlite3S
1ffda 65 6c 65 63 74 50 72 65 70 28 70 50 61 72 73 65 electPrep(pParse
1ffdb 2c 20 70 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a , p, pOuterNC);.
1ffdc 20 20 20 20 72 65 74 75 72 6e 20 28 70 50 61 72 return (pPar
1ffdd 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e se->nErr || db->
1ffde 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 3f 20 mallocFailed) ?
1ffdf 57 52 43 5f 41 62 6f 72 74 20 3a 20 57 52 43 5f WRC_Abort : WRC_
1ffe0 50 72 75 6e 65 3b 0a 20 20 7d 0a 0a 20 20 69 73 Prune;. }.. is
1ffe1 43 6f 6d 70 6f 75 6e 64 20 3d 20 70 2d 3e 70 50 Compound = p->pP
1ffe2 72 69 6f 72 21 3d 30 3b 0a 20 20 6e 43 6f 6d 70 rior!=0;. nComp
1ffe3 6f 75 6e 64 20 3d 20 30 3b 0a 20 20 70 4c 65 66 ound = 0;. pLef
1ffe4 74 6d 6f 73 74 20 3d 20 70 3b 0a 20 20 77 68 69 tmost = p;. whi
1ffe5 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 61 73 73 le( p ){. ass
1ffe6 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 ert( (p->selFlag
1ffe7 73 20 26 20 53 46 5f 45 78 70 61 6e 64 65 64 29 s & SF_Expanded)
1ffe8 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 !=0 );. asser
1ffe9 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 t( (p->selFlags
1ffea 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 29 3d 3d & SF_Resolved)==
1ffeb 30 20 29 3b 0a 20 20 20 20 70 2d 3e 73 65 6c 46 0 );. p->selF
1ffec 6c 61 67 73 20 7c 3d 20 53 46 5f 52 65 73 6f 6c lags |= SF_Resol
1ffed 76 65 64 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 73 ved;.. /* Res
1ffee 6f 6c 76 65 20 74 68 65 20 65 78 70 72 65 73 73 olve the express
1ffef 69 6f 6e 73 20 69 6e 20 74 68 65 20 4c 49 4d 49 ions in the LIMI
1fff0 54 20 61 6e 64 20 4f 46 46 53 45 54 20 63 6c 61 T and OFFSET cla
1fff1 75 73 65 73 2e 20 54 68 65 73 65 0a 20 20 20 20 uses. These.
1fff2 2a 2a 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 ** are not allow
1fff3 65 64 20 74 6f 20 72 65 66 65 72 20 74 6f 20 61 ed to refer to a
1fff4 6e 79 20 6e 61 6d 65 73 2c 20 73 6f 20 70 61 73 ny names, so pas
1fff5 73 20 61 6e 20 65 6d 70 74 79 20 4e 61 6d 65 43 s an empty NameC
1fff6 6f 6e 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 ontext.. */.
1fff7 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 memset(&sNC,
1fff8 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0, sizeof(sNC));
1fff9 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 . sNC.pParse
1fffa 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20 69 66 = pParse;. if
1fffb 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 ( sqlite3Resolve
1fffc 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 ExprNames(&sNC,
1fffd 70 2d 3e 70 4c 69 6d 69 74 29 20 7c 7c 0a 20 20 p->pLimit) ||.
1fffe 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 sqlite3Res
1ffff 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 olveExprNames(&s
20000 4e 43 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 20 NC, p->pOffset)
20001 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
20002 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d WRC_Abort;. }
20003 0a 20 20 0a 20 20 20 20 2f 2a 20 53 65 74 20 75 . . /* Set u
20004 70 20 74 68 65 20 6c 6f 63 61 6c 20 6e 61 6d 65 p the local name
20005 2d 63 6f 6e 74 65 78 74 20 74 6f 20 70 61 73 73 -context to pass
20006 20 74 6f 20 73 71 6c 69 74 65 33 52 65 73 6f 6c to sqlite3Resol
20007 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20 74 6f veExprNames() to
20008 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c 76 65 20 . ** resolve
20009 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 65 the result-set e
2000a 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a xpression list..
2000b 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 61 */. sNC.a
2000c 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 20 llowAgg = 1;.
2000d 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 sNC.pSrcList =
2000e 70 2d 3e 70 53 72 63 3b 0a 20 20 20 20 73 4e 43 p->pSrc;. sNC
2000f 2e 70 4e 65 78 74 20 3d 20 70 4f 75 74 65 72 4e .pNext = pOuterN
20010 43 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 73 C;. . /* Res
20011 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 74 68 olve names in th
20012 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 2a 2f e result set. */
20013 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 2d . pEList = p-
20014 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 >pEList;. ass
20015 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 ert( pEList!=0 )
20016 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
20017 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 <pEList->nExpr;
20018 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 i++){. Expr
20019 20 2a 70 58 20 3d 20 70 45 4c 69 73 74 2d 3e 61 *pX = pEList->a
2001a 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 [i].pExpr;.
2001b 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f if( sqlite3Reso
2001c 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e lveExprNames(&sN
2001d 43 2c 20 70 58 29 20 29 7b 0a 20 20 20 20 20 20 C, pX) ){.
2001e 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f return WRC_Abo
2001f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rt;. }.
20020 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 63 75 }. . /* Recu
20021 72 73 69 76 65 6c 79 20 72 65 73 6f 6c 76 65 20 rsively resolve
20022 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 73 75 62 names in all sub
20023 71 75 65 72 69 65 73 0a 20 20 20 20 2a 2f 0a 20 queries. */.
20024 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d for(i=0; i<p-
20025 3e 70 53 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b >pSrc->nSrc; i++
20026 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 ){. struct
20027 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 SrcList_item *pI
20028 74 65 6d 20 3d 20 26 70 2d 3e 70 53 72 63 2d 3e tem = &p->pSrc->
20029 61 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 a[i];. if(
2002a 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 pItem->pSelect )
2002b 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 {. const
2002c 63 68 61 72 20 2a 7a 53 61 76 65 64 43 6f 6e 74 char *zSavedCont
2002d 65 78 74 20 3d 20 70 50 61 72 73 65 2d 3e 7a 41 ext = pParse->zA
2002e 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 uthContext;.
2002f 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a if( pItem->z
20030 4e 61 6d 65 20 29 20 70 50 61 72 73 65 2d 3e 7a Name ) pParse->z
20031 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 49 AuthContext = pI
20032 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 tem->zName;.
20033 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c sqlite3Resol
20034 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 70 50 veSelectNames(pP
20035 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 53 65 arse, pItem->pSe
20036 6c 65 63 74 2c 20 70 4f 75 74 65 72 4e 43 29 3b lect, pOuterNC);
20037 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d . pParse-
20038 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 >zAuthContext =
20039 7a 53 61 76 65 64 43 6f 6e 74 65 78 74 3b 0a 20 zSavedContext;.
2003a 20 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73 if( pPars
2003b 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d e->nErr || db->m
2003c 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 allocFailed ) re
2003d 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
2003e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
2003f 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 . /* If there
20040 20 61 72 65 20 6e 6f 20 61 67 67 72 65 67 61 74 are no aggregat
20041 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 e functions in t
20042 68 65 20 72 65 73 75 6c 74 2d 73 65 74 2c 20 61 he result-set, a
20043 6e 64 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 0a nd no GROUP BY .
20044 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f ** expressio
20045 6e 2c 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 n, do not allow
20046 61 67 67 72 65 67 61 74 65 73 20 69 6e 20 61 6e aggregates in an
20047 79 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 65 y of the other e
20048 78 70 72 65 73 73 69 6f 6e 73 2e 0a 20 20 20 20 xpressions..
20049 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 */. assert( (
2004a 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 p->selFlags & SF
2004b 5f 41 67 67 72 65 67 61 74 65 29 3d 3d 30 20 29 _Aggregate)==0 )
2004c 3b 0a 20 20 20 20 70 47 72 6f 75 70 42 79 20 3d ;. pGroupBy =
2004d 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 p->pGroupBy;.
2004e 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 20 7c if( pGroupBy |
2004f 7c 20 73 4e 43 2e 68 61 73 41 67 67 20 29 7b 0a | sNC.hasAgg ){.
20050 20 20 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 p->selFlag
20051 73 20 7c 3d 20 53 46 5f 41 67 67 72 65 67 61 74 s |= SF_Aggregat
20052 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 e;. }else{.
20053 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 sNC.allowAgg
20054 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 = 0;. }. .
20055 20 20 20 2f 2a 20 49 66 20 61 20 48 41 56 49 4e /* If a HAVIN
20056 47 20 63 6c 61 75 73 65 20 69 73 20 70 72 65 73 G clause is pres
20057 65 6e 74 2c 20 74 68 65 6e 20 74 68 65 72 65 20 ent, then there
20058 6d 75 73 74 20 62 65 20 61 20 47 52 4f 55 50 20 must be a GROUP
20059 42 59 20 63 6c 61 75 73 65 2e 0a 20 20 20 20 2a BY clause.. *
2005a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 48 61 /. if( p->pHa
2005b 76 69 6e 67 20 26 26 20 21 70 47 72 6f 75 70 42 ving && !pGroupB
2005c 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 y ){. sqlit
2005d 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
2005e 65 2c 20 22 61 20 47 52 4f 55 50 20 42 59 20 63 e, "a GROUP BY c
2005f 6c 61 75 73 65 20 69 73 20 72 65 71 75 69 72 65 lause is require
20060 64 20 62 65 66 6f 72 65 20 48 41 56 49 4e 47 22 d before HAVING"
20061 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
20062 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d WRC_Abort;. }
20063 0a 20 20 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 . . /* Add t
20064 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 he expression li
20065 73 74 20 74 6f 20 74 68 65 20 6e 61 6d 65 2d 63 st to the name-c
20066 6f 6e 74 65 78 74 20 62 65 66 6f 72 65 20 70 61 ontext before pa
20067 72 73 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a rsing the. **
20068 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f other expressio
20069 6e 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 ns in the SELECT
2006a 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 statement. This
2006b 20 69 73 20 73 6f 20 74 68 61 74 0a 20 20 20 20 is so that.
2006c 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 ** expressions i
2006d 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 n the WHERE clau
2006e 73 65 20 28 65 74 63 2e 29 20 63 61 6e 20 72 65 se (etc.) can re
2006f 66 65 72 20 74 6f 20 65 78 70 72 65 73 73 69 6f fer to expressio
20070 6e 73 20 62 79 0a 20 20 20 20 2a 2a 20 61 6c 69 ns by. ** ali
20071 61 73 65 73 20 69 6e 20 74 68 65 20 72 65 73 75 ases in the resu
20072 6c 74 20 73 65 74 2e 0a 20 20 20 20 2a 2a 0a 20 lt set.. **.
20073 20 20 20 2a 2a 20 4d 69 6e 6f 72 20 70 6f 69 6e ** Minor poin
20074 74 3a 20 49 66 20 74 68 69 73 20 69 73 20 74 68 t: If this is th
20075 65 20 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65 e case, then the
20076 20 65 78 70 72 65 73 73 69 6f 6e 20 77 69 6c 6c expression will
20077 20 62 65 0a 20 20 20 20 2a 2a 20 72 65 2d 65 76 be. ** re-ev
20078 61 6c 75 61 74 65 64 20 66 6f 72 20 65 61 63 68 aluated for each
20079 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 69 74 reference to it
2007a 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 .. */. sNC
2007b 2e 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c .pEList = p->pEL
2007c 69 73 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c ist;. if( sql
2007d 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e ite3ResolveExprN
2007e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 57 ames(&sNC, p->pW
2007f 68 65 72 65 29 20 7c 7c 0a 20 20 20 20 20 20 20 here) ||.
20080 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
20081 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d prNames(&sNC, p-
20082 3e 70 48 61 76 69 6e 67 29 0a 20 20 20 20 29 7b >pHaving). ){
20083 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 . return WR
20084 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 0a C_Abort;. }..
20085 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 /* The ORDER
20086 20 42 59 20 61 6e 64 20 47 52 4f 55 50 20 42 59 BY and GROUP BY
20087 20 63 6c 61 75 73 65 73 20 6d 61 79 20 6e 6f 74 clauses may not
20088 20 72 65 66 65 72 20 74 6f 20 74 65 72 6d 73 20 refer to terms
20089 69 6e 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 in. ** outer
2008a 71 75 65 72 69 65 73 20 0a 20 20 20 20 2a 2f 0a queries . */.
2008b 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 sNC.pNext =
2008c 30 3b 0a 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 0;. sNC.allow
2008d 41 67 67 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a Agg = 1;.. /*
2008e 20 50 72 6f 63 65 73 73 20 74 68 65 20 4f 52 44 Process the ORD
2008f 45 52 20 42 59 20 63 6c 61 75 73 65 20 66 6f 72 ER BY clause for
20090 20 73 69 6e 67 6c 65 74 6f 6e 20 53 45 4c 45 43 singleton SELEC
20091 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 T statements..
20092 20 20 2a 2a 20 54 68 65 20 4f 52 44 45 52 20 42 ** The ORDER B
20093 59 20 63 6c 61 75 73 65 20 66 6f 72 20 63 6f 6d Y clause for com
20094 70 6f 75 6e 64 73 20 53 45 4c 45 43 54 20 73 74 pounds SELECT st
20095 61 74 65 6d 65 6e 74 73 20 69 73 20 68 61 6e 64 atements is hand
20096 6c 65 64 0a 20 20 20 20 2a 2a 20 62 65 6c 6f 77 led. ** below
20097 2c 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 , after all of t
20098 68 65 20 72 65 73 75 6c 74 2d 73 65 74 73 20 66 he result-sets f
20099 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 65 6c or all of the el
2009a 65 6d 65 6e 74 73 20 6f 66 0a 20 20 20 20 2a 2a ements of. **
2009b 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 the compound ha
2009c 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 ve been resolved
2009d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
2009e 20 21 69 73 43 6f 6d 70 6f 75 6e 64 20 26 26 20 !isCompound &&
2009f 72 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 resolveOrderGrou
200a0 70 42 79 28 26 73 4e 43 2c 20 70 2c 20 70 2d 3e pBy(&sNC, p, p->
200a1 70 4f 72 64 65 72 42 79 2c 20 22 4f 52 44 45 52 pOrderBy, "ORDER
200a2 22 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ") ){. retu
200a3 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
200a4 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e }. if( db->
200a5 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
200a6 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
200a7 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 _Abort;. }.
200a8 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 . /* Resolve
200a9 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 the GROUP BY cla
200aa 75 73 65 2e 20 20 41 74 20 74 68 65 20 73 61 6d use. At the sam
200ab 65 20 74 69 6d 65 2c 20 6d 61 6b 65 20 73 75 72 e time, make sur
200ac 65 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 47 52 e . ** the GR
200ad 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 64 6f OUP BY clause do
200ae 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 es not contain a
200af 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
200b0 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ns.. */. i
200b1 66 28 20 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 f( pGroupBy ){.
200b2 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 struct Expr
200b3 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
200b4 3b 0a 20 20 20 20 0a 20 20 20 20 20 20 69 66 28 ;. . if(
200b5 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f resolveOrderGro
200b6 75 70 42 79 28 26 73 4e 43 2c 20 70 2c 20 70 47 upBy(&sNC, p, pG
200b7 72 6f 75 70 42 79 2c 20 22 47 52 4f 55 50 22 29 roupBy, "GROUP")
200b8 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 || db->mallocFa
200b9 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 iled ){.
200ba 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
200bb 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
200bc 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 for(i=0, pItem=p
200bd 47 72 6f 75 70 42 79 2d 3e 61 3b 20 69 3c 70 47 GroupBy->a; i<pG
200be 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 20 69 roupBy->nExpr; i
200bf 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 ++, pItem++){.
200c0 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 if( ExprHa
200c1 73 50 72 6f 70 65 72 74 79 28 70 49 74 65 6d 2d sProperty(pItem-
200c2 3e 70 45 78 70 72 2c 20 45 50 5f 41 67 67 29 20 >pExpr, EP_Agg)
200c3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
200c4 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
200c5 72 73 65 2c 20 22 61 67 67 72 65 67 61 74 65 20 rse, "aggregate
200c6 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f functions are no
200c7 74 20 61 6c 6c 6f 77 65 64 20 69 6e 20 22 0a 20 t allowed in ".
200c8 20 20 20 20 20 20 20 20 20 20 20 20 20 22 74 68 "th
200c9 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 e GROUP BY claus
200ca 65 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 e");. r
200cb 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
200cc 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
200cd 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a }. }.. /*
200ce 20 41 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20 Advance to the
200cf 6e 65 78 74 20 74 65 72 6d 20 6f 66 20 74 68 65 next term of the
200d0 20 63 6f 6d 70 6f 75 6e 64 0a 20 20 20 20 2a 2f compound. */
200d1 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 . p = p->pPri
200d2 6f 72 3b 0a 20 20 20 20 6e 43 6f 6d 70 6f 75 6e or;. nCompoun
200d3 64 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 d++;. }.. /* R
200d4 65 73 6f 6c 76 65 20 74 68 65 20 4f 52 44 45 52 esolve the ORDER
200d5 20 42 59 20 6f 6e 20 61 20 63 6f 6d 70 6f 75 6e BY on a compoun
200d6 64 20 53 45 4c 45 43 54 20 61 66 74 65 72 20 61 d SELECT after a
200d7 6c 6c 20 74 65 72 6d 73 20 6f 66 0a 20 20 2a 2a ll terms of. **
200d8 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 the compound ha
200d9 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 ve been resolved
200da 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 43 .. */. if( isC
200db 6f 6d 70 6f 75 6e 64 20 26 26 20 72 65 73 6f 6c ompound && resol
200dc 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 72 42 veCompoundOrderB
200dd 79 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 6d y(pParse, pLeftm
200de 6f 73 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 ost) ){. retu
200df 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
200e0 7d 0a 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f }.. return WRC_
200e1 50 72 75 6e 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Prune;.}../*.**
200e2 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 6c This routine wal
200e3 6b 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e ks an expression
200e4 20 74 72 65 65 20 61 6e 64 20 72 65 73 6f 6c 76 tree and resolv
200e5 65 73 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f es references to
200e6 0a 2a 2a 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e .** table column
200e7 73 20 61 6e 64 20 72 65 73 75 6c 74 2d 73 65 74 s and result-set
200e8 20 63 6f 6c 75 6d 6e 73 2e 20 20 41 74 20 74 68 columns. At th
200e9 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 64 6f 20 e same time, do
200ea 65 72 72 6f 72 0a 2a 2a 20 63 68 65 63 6b 69 6e error.** checkin
200eb 67 20 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73 g on function us
200ec 61 67 65 20 61 6e 64 20 73 65 74 20 61 20 66 6c age and set a fl
200ed 61 67 20 69 66 20 61 6e 79 20 61 67 67 72 65 67 ag if any aggreg
200ee 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a ate functions.**
200ef 20 61 72 65 20 73 65 65 6e 2e 0a 2a 2a 0a 2a 2a are seen..**.**
200f0 20 54 6f 20 72 65 73 6f 6c 76 65 20 74 61 62 6c To resolve tabl
200f1 65 20 63 6f 6c 75 6d 6e 73 20 72 65 66 65 72 65 e columns refere
200f2 6e 63 65 73 20 77 65 20 6c 6f 6f 6b 20 66 6f 72 nces we look for
200f3 20 6e 6f 64 65 73 20 28 6f 72 20 73 75 62 74 72 nodes (or subtr
200f4 65 65 73 29 20 6f 66 20 74 68 65 20 0a 2a 2a 20 ees) of the .**
200f5 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e form X.Y.Z or Y.
200f6 5a 20 6f 72 20 6a 75 73 74 20 5a 20 77 68 65 72 Z or just Z wher
200f7 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 58 3a 20 e.**.** X:
200f8 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 The name of a
200f9 64 61 74 61 62 61 73 65 2e 20 20 45 78 3a 20 20 database. Ex:
200fa 22 6d 61 69 6e 22 20 6f 72 20 22 74 65 6d 70 22 "main" or "temp"
200fb 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 or.**
200fc 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 the symbolic na
200fd 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61 me assigned to a
200fe 6e 20 41 54 54 41 43 48 2d 65 64 20 64 61 74 61 n ATTACH-ed data
200ff 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 base..**.**
20100 20 59 3a 20 20 20 54 68 65 20 6e 61 6d 65 20 6f Y: The name o
20101 66 20 61 20 74 61 62 6c 65 20 69 6e 20 61 20 46 f a table in a F
20102 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 4f 72 20 ROM clause. Or
20103 69 6e 20 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 in a trigger.**
20104 20 20 20 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 one of
20105 20 74 68 65 20 73 70 65 63 69 61 6c 20 6e 61 6d the special nam
20106 65 73 20 22 6f 6c 64 22 20 6f 72 20 22 6e 65 77 es "old" or "new
20107 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 5a 3a "..**.** Z:
20108 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 The name of a
20109 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 column in table
2010a 20 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 6f Y..**.** The no
2010b 64 65 20 61 74 20 74 68 65 20 72 6f 6f 74 20 6f de at the root o
2010c 66 20 74 68 65 20 73 75 62 74 72 65 65 20 69 73 f the subtree is
2010d 20 6d 6f 64 69 66 69 65 64 20 61 73 20 66 6f 6c modified as fol
2010e 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 45 lows:.**.** E
2010f 78 70 72 2e 6f 70 20 20 20 20 20 20 20 20 43 68 xpr.op Ch
20110 61 6e 67 65 64 20 74 6f 20 54 4b 5f 43 4f 4c 55 anged to TK_COLU
20111 4d 4e 0a 2a 2a 20 20 20 20 45 78 70 72 2e 70 54 MN.** Expr.pT
20112 61 62 20 20 20 20 20 20 50 6f 69 6e 74 73 20 74 ab Points t
20113 6f 20 74 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 o the Table obje
20114 63 74 20 66 6f 72 20 58 2e 59 0a 2a 2a 20 20 20 ct for X.Y.**
20115 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 20 20 Expr.iColumn
20116 54 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 The column index
20117 20 69 6e 20 58 2e 59 2e 20 20 2d 31 20 66 6f 72 in X.Y. -1 for
20118 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 20 20 the rowid..**
20119 20 20 45 78 70 72 2e 69 54 61 62 6c 65 20 20 20 Expr.iTable
2011a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 The VDBE cursor
2011b 20 6e 75 6d 62 65 72 20 66 6f 72 20 58 2e 59 0a number for X.Y.
2011c 2a 2a 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f **.**.** To reso
2011d 6c 76 65 20 72 65 73 75 6c 74 2d 73 65 74 20 72 lve result-set r
2011e 65 66 65 72 65 6e 63 65 73 2c 20 6c 6f 6f 6b 20 eferences, look
2011f 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 20 6e for expression n
20120 6f 64 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 odes of the.** f
20121 6f 72 6d 20 5a 20 28 77 69 74 68 20 6e 6f 20 58 orm Z (with no X
20122 20 61 6e 64 20 59 20 70 72 65 66 69 78 29 20 77 and Y prefix) w
20123 68 65 72 65 20 74 68 65 20 5a 20 6d 61 74 63 68 here the Z match
20124 65 73 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e es the right-han
20125 64 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 61 6e 20 d.** size of an
20126 41 53 20 63 6c 61 75 73 65 20 69 6e 20 74 68 65 AS clause in the
20127 20 72 65 73 75 6c 74 2d 73 65 74 20 6f 66 20 61 result-set of a
20128 20 53 45 4c 45 43 54 2e 20 20 54 68 65 20 5a 20 SELECT. The Z
20129 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 expression.** is
2012a 20 72 65 70 6c 61 63 65 64 20 62 79 20 61 20 63 replaced by a c
2012b 6f 70 79 20 6f 66 20 74 68 65 20 6c 65 66 74 2d opy of the left-
2012c 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 hand side of the
2012d 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 result-set expr
2012e 65 73 73 69 6f 6e 2e 0a 2a 2a 20 54 61 62 6c 65 ession..** Table
2012f 2d 6e 61 6d 65 20 61 6e 64 20 66 75 6e 63 74 69 -name and functi
20130 6f 6e 20 72 65 73 6f 6c 75 74 69 6f 6e 20 6f 63 on resolution oc
20131 63 75 72 73 20 6f 6e 20 74 68 65 20 73 75 62 73 curs on the subs
20132 74 69 74 75 74 65 64 20 65 78 70 72 65 73 73 69 tituted expressi
20133 6f 6e 0a 2a 2a 20 74 72 65 65 2e 20 20 46 6f 72 on.** tree. For
20134 20 65 78 61 6d 70 6c 65 2c 20 69 6e 3a 0a 2a 2a example, in:.**
20135 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45 43 54 20 .** SELECT
20136 61 2b 62 20 41 53 20 78 2c 20 63 2b 64 20 41 53 a+b AS x, c+d AS
20137 20 79 20 46 52 4f 4d 20 74 31 20 4f 52 44 45 52 y FROM t1 ORDER
20138 20 42 59 20 78 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 BY x;.**.** The
20139 20 22 78 22 20 74 65 72 6d 20 6f 66 20 74 68 65 "x" term of the
2013a 20 6f 72 64 65 72 20 62 79 20 69 73 20 72 65 70 order by is rep
2013b 6c 61 63 65 64 20 62 79 20 22 61 2b 62 22 20 74 laced by "a+b" t
2013c 6f 20 72 65 6e 64 65 72 3a 0a 2a 2a 0a 2a 2a 20 o render:.**.**
2013d 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b 62 20 SELECT a+b
2013e 41 53 20 78 2c 20 63 2b 64 20 41 53 20 79 20 46 AS x, c+d AS y F
2013f 52 4f 4d 20 74 31 20 4f 52 44 45 52 20 42 59 20 ROM t1 ORDER BY
20140 61 2b 62 3b 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 a+b;.**.** Funct
20141 69 6f 6e 20 63 61 6c 6c 73 20 61 72 65 20 63 68 ion calls are ch
20142 65 63 6b 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 ecked to make su
20143 72 65 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 re that the func
20144 74 69 6f 6e 20 69 73 20 0a 2a 2a 20 64 65 66 69 tion is .** defi
20145 6e 65 64 20 61 6e 64 20 74 68 61 74 20 74 68 65 ned and that the
20146 20 63 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20 correct number
20147 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 of arguments are
20148 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a 20 49 specified..** I
20149 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 f the function i
2014a 73 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 66 s an aggregate f
2014b 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 unction, then th
2014c 65 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 69 73 e pNC->hasAgg is
2014d 0a 2a 2a 20 73 65 74 20 61 6e 64 20 74 68 65 20 .** set and the
2014e 6f 70 63 6f 64 65 20 69 73 20 63 68 61 6e 67 65 opcode is change
2014f 64 20 66 72 6f 6d 20 54 4b 5f 46 55 4e 43 54 49 d from TK_FUNCTI
20150 4f 4e 20 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e ON to TK_AGG_FUN
20151 43 54 49 4f 4e 2e 0a 2a 2a 20 49 66 20 61 6e 20 CTION..** If an
20152 65 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61 expression conta
20153 69 6e 73 20 61 67 67 72 65 67 61 74 65 20 66 75 ins aggregate fu
20154 6e 63 74 69 6f 6e 73 20 74 68 65 6e 20 74 68 65 nctions then the
20155 20 45 50 5f 41 67 67 0a 2a 2a 20 70 72 6f 70 65 EP_Agg.** prope
20156 72 74 79 20 6f 6e 20 74 68 65 20 65 78 70 72 65 rty on the expre
20157 73 73 69 6f 6e 20 69 73 20 73 65 74 2e 0a 2a 2a ssion is set..**
20158 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65 73 .** An error mes
20159 73 61 67 65 20 69 73 20 6c 65 66 74 20 69 6e 20 sage is left in
2015a 70 50 61 72 73 65 20 69 66 20 61 6e 79 74 68 69 pParse if anythi
2015b 6e 67 20 69 73 20 61 6d 69 73 73 2e 20 20 54 68 ng is amiss. Th
2015c 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 69 66 20 65 e number.** if e
2015d 72 72 6f 72 73 20 69 73 20 72 65 74 75 72 6e 65 rrors is returne
2015e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
2015f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
20160 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
20161 28 20 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 ( . NameContext
20162 20 2a 70 4e 43 2c 20 20 20 20 20 20 20 2f 2a 20 *pNC, /*
20163 4e 61 6d 65 73 70 61 63 65 20 74 6f 20 72 65 73 Namespace to res
20164 6f 6c 76 65 20 65 78 70 72 65 73 73 69 6f 6e 73 olve expressions
20165 20 69 6e 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a in. */. Expr *
20166 70 45 78 70 72 20 20 20 20 20 20 20 20 20 20 20 pExpr
20167 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 /* The express
20168 69 6f 6e 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a ion to be analyz
20169 65 64 2e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ed. */.){. int
2016a 73 61 76 65 64 48 61 73 41 67 67 3b 0a 20 20 57 savedHasAgg;. W
2016b 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 69 66 28 20 alker w;.. if(
2016c 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 pExpr==0 ) retur
2016d 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f n 0;.#if SQLITE_
2016e 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 MAX_EXPR_DEPTH>0
2016f 0a 20 20 7b 0a 20 20 20 20 50 61 72 73 65 20 2a . {. Parse *
20170 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 pParse = pNC->pP
20171 61 72 73 65 3b 0a 20 20 20 20 69 66 28 20 73 71 arse;. if( sq
20172 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 lite3ExprCheckHe
20173 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 45 78 ight(pParse, pEx
20174 70 72 2d 3e 6e 48 65 69 67 68 74 2b 70 4e 43 2d pr->nHeight+pNC-
20175 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 >pParse->nHeight
20176 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
20177 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 n 1;. }. p
20178 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2b Parse->nHeight +
20179 3d 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 = pExpr->nHeight
2017a 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 ;. }.#endif. s
2017b 61 76 65 64 48 61 73 41 67 67 20 3d 20 70 4e 43 avedHasAgg = pNC
2017c 2d 3e 68 61 73 41 67 67 3b 0a 20 20 70 4e 43 2d ->hasAgg;. pNC-
2017d 3e 68 61 73 41 67 67 20 3d 20 30 3b 0a 20 20 77 >hasAgg = 0;. w
2017e 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d .xExprCallback =
2017f 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74 65 70 resolveExprStep
20180 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c ;. w.xSelectCal
20181 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 53 lback = resolveS
20182 65 6c 65 63 74 53 74 65 70 3b 0a 20 20 77 2e 70 electStep;. w.p
20183 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 Parse = pNC->pPa
20184 72 73 65 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d rse;. w.u.pNC =
20185 20 70 4e 43 3b 0a 20 20 73 71 6c 69 74 65 33 57 pNC;. sqlite3W
20186 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 45 78 70 alkExpr(&w, pExp
20187 72 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d r);.#if SQLITE_M
20188 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a AX_EXPR_DEPTH>0.
20189 20 20 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e pNC->pParse->n
2018a 48 65 69 67 68 74 20 2d 3d 20 70 45 78 70 72 2d Height -= pExpr-
2018b 3e 6e 48 65 69 67 68 74 3b 0a 23 65 6e 64 69 66 >nHeight;.#endif
2018c 0a 20 20 69 66 28 20 70 4e 43 2d 3e 6e 45 72 72 . if( pNC->nErr
2018d 3e 30 20 7c 7c 20 77 2e 70 50 61 72 73 65 2d 3e >0 || w.pParse->
2018e 6e 45 72 72 3e 30 20 29 7b 0a 20 20 20 20 45 78 nErr>0 ){. Ex
2018f 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 prSetProperty(pE
20190 78 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0a xpr, EP_Error);.
20191 20 20 7d 0a 20 20 69 66 28 20 70 4e 43 2d 3e 68 }. if( pNC->h
20192 61 73 41 67 67 20 29 7b 0a 20 20 20 20 45 78 70 asAgg ){. Exp
20193 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 rSetProperty(pEx
20194 70 72 2c 20 45 50 5f 41 67 67 29 3b 0a 20 20 7d pr, EP_Agg);. }
20195 65 6c 73 65 20 69 66 28 20 73 61 76 65 64 48 61 else if( savedHa
20196 73 41 67 67 20 29 7b 0a 20 20 20 20 70 4e 43 2d sAgg ){. pNC-
20197 3e 68 61 73 41 67 67 20 3d 20 31 3b 0a 20 20 7d >hasAgg = 1;. }
20198 0a 20 20 72 65 74 75 72 6e 20 45 78 70 72 48 61 . return ExprHa
20199 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c sProperty(pExpr,
2019a 20 45 50 5f 45 72 72 6f 72 29 3b 0a 7d 0a 0a 0a EP_Error);.}...
2019b 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6c /*.** Resolve al
2019c 6c 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 65 l names in all e
2019d 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 20 xpressions of a
2019e 53 45 4c 45 43 54 20 61 6e 64 20 69 6e 20 61 6c SELECT and in al
2019f 6c 0a 2a 2a 20 64 65 63 65 6e 64 65 6e 74 73 20 l.** decendents
201a0 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2c 20 69 of the SELECT, i
201a1 6e 63 6c 75 64 69 6e 67 20 63 6f 6d 70 6f 75 6e ncluding compoun
201a2 64 73 20 6f 66 66 20 6f 66 20 70 2d 3e 70 50 72 ds off of p->pPr
201a3 69 6f 72 2c 0a 2a 2a 20 73 75 62 71 75 65 72 69 ior,.** subqueri
201a4 65 73 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e es in expression
201a5 73 2c 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 s, and subquerie
201a6 73 20 75 73 65 64 20 61 73 20 46 52 4f 4d 20 63 s used as FROM c
201a7 6c 61 75 73 65 0a 2a 2a 20 74 65 72 6d 73 2e 0a lause.** terms..
201a8 2a 2a 0a 2a 2a 20 53 65 65 20 73 71 6c 69 74 65 **.** See sqlite
201a9 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 3ResolveExprName
201aa 73 28 29 20 66 6f 72 20 61 20 64 65 73 63 72 69 s() for a descri
201ab 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 6b 69 6e ption of the kin
201ac 64 73 20 6f 66 0a 2a 2a 20 74 72 61 6e 73 66 6f ds of.** transfo
201ad 72 6d 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 63 rmations that oc
201ae 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 53 cur..**.** All S
201af 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 ELECT statements
201b0 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 should have bee
201b1 6e 20 65 78 70 61 6e 64 65 64 20 75 73 69 6e 67 n expanded using
201b2 0a 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 .** sqlite3Selec
201b3 74 45 78 70 61 6e 64 28 29 20 70 72 69 6f 72 20 tExpand() prior
201b4 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 to invoking this
201b5 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c routine..*/.SQL
201b6 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
201b7 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 sqlite3ResolveS
201b8 65 6c 65 63 74 4e 61 6d 65 73 28 0a 20 20 50 61 electNames(. Pa
201b9 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
201ba 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 /* The pars
201bb 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 er context */.
201bc 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 Select *p,
201bd 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 /* The SE
201be 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62 LECT statement b
201bf 65 69 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 eing coded. */.
201c0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f NameContext *pO
201c1 75 74 65 72 4e 43 20 20 2f 2a 20 4e 61 6d 65 20 uterNC /* Name
201c2 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 61 72 65 context for pare
201c3 6e 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d nt SELECT statem
201c4 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 57 61 6c 6b ent */.){. Walk
201c5 65 72 20 77 3b 0a 0a 20 20 61 73 73 65 72 74 28 er w;.. assert(
201c6 20 70 21 3d 30 20 29 3b 0a 20 20 77 2e 78 45 78 p!=0 );. w.xEx
201c7 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 prCallback = res
201c8 6f 6c 76 65 45 78 70 72 53 74 65 70 3b 0a 20 20 olveExprStep;.
201c9 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 w.xSelectCallbac
201ca 6b 20 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 k = resolveSelec
201cb 74 53 74 65 70 3b 0a 20 20 77 2e 70 50 61 72 73 tStep;. w.pPars
201cc 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 77 2e e = pParse;. w.
201cd 75 2e 70 4e 43 20 3d 20 70 4f 75 74 65 72 4e 43 u.pNC = pOuterNC
201ce 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 ;. sqlite3WalkS
201cf 65 6c 65 63 74 28 26 77 2c 20 70 29 3b 0a 7d 0a elect(&w, p);.}.
201d0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
201d1 20 45 6e 64 20 6f 66 20 72 65 73 6f 6c 76 65 2e End of resolve.
201d2 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
201d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
201d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
201d5 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
201d6 20 42 65 67 69 6e 20 66 69 6c 65 20 65 78 70 72 Begin file expr
201d7 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
201d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
201d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
201da 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 ./*.** 2001 Sept
201db 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 ember 15.**.** T
201dc 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
201dd 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
201de 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
201df 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
201e0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
201e1 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
201e2 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
201e3 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
201e4 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
201e5 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
201e6 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
201e7 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
201e8 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
201e9 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
201ea 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
201eb 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
201ec 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
201ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
201ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
201ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
201f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
201f1 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
201f2 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f file contains ro
201f3 75 74 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 utines used for
201f4 61 6e 61 6c 79 7a 69 6e 67 20 65 78 70 72 65 73 analyzing expres
201f5 73 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66 6f 72 sions and.** for
201f6 20 67 65 6e 65 72 61 74 69 6e 67 20 56 44 42 45 generating VDBE
201f7 20 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75 code that evalu
201f8 61 74 65 73 20 65 78 70 72 65 73 73 69 6f 6e 73 ates expressions
201f9 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a in SQLite..*/..
201fa 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
201fb 20 27 61 66 66 69 6e 69 74 79 27 20 6f 66 20 74 'affinity' of t
201fc 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 he expression pE
201fd 78 70 72 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a xpr if any..**.*
201fe 2a 20 49 66 20 70 45 78 70 72 20 69 73 20 61 20 * If pExpr is a
201ff 63 6f 6c 75 6d 6e 2c 20 61 20 72 65 66 65 72 65 column, a refere
20200 6e 63 65 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 nce to a column
20201 76 69 61 20 61 6e 20 27 41 53 27 20 61 6c 69 61 via an 'AS' alia
20202 73 2c 0a 2a 2a 20 6f 72 20 61 20 73 75 62 2d 73 s,.** or a sub-s
20203 65 6c 65 63 74 20 77 69 74 68 20 61 20 63 6f 6c elect with a col
20204 75 6d 6e 20 61 73 20 74 68 65 20 72 65 74 75 72 umn as the retur
20205 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 n value, then th
20206 65 20 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 6f e .** affinity o
20207 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 73 f that column is
20208 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 returned. Other
20209 77 69 73 65 2c 20 30 78 30 30 20 69 73 20 72 65 wise, 0x00 is re
2020a 74 75 72 6e 65 64 2c 0a 2a 2a 20 69 6e 64 69 63 turned,.** indic
2020b 61 74 69 6e 67 20 6e 6f 20 61 66 66 69 6e 69 74 ating no affinit
2020c 79 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 y for the expres
2020d 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e sion..**.** i.e.
2020e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 the WHERE claus
2020f 65 20 65 78 70 72 65 73 73 73 69 6f 6e 73 20 69 e expresssions i
20210 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 n the following
20211 73 74 61 74 65 6d 65 6e 74 73 20 61 6c 6c 0a 2a statements all.*
20212 2a 20 68 61 76 65 20 61 6e 20 61 66 66 69 6e 69 * have an affini
20213 74 79 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 ty:.**.** CREATE
20214 20 54 41 42 4c 45 20 74 31 28 61 29 3b 0a 2a 2a TABLE t1(a);.**
20215 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 SELECT * FROM t
20216 31 20 57 48 45 52 45 20 61 3b 0a 2a 2a 20 53 45 1 WHERE a;.** SE
20217 4c 45 43 54 20 61 20 41 53 20 62 20 46 52 4f 4d LECT a AS b FROM
20218 20 74 31 20 57 48 45 52 45 20 62 3b 0a 2a 2a 20 t1 WHERE b;.**
20219 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 SELECT * FROM t1
2021a 20 57 48 45 52 45 20 28 73 65 6c 65 63 74 20 61 WHERE (select a
2021b 20 66 72 6f 6d 20 74 31 29 3b 0a 2a 2f 0a 53 51 from t1);.*/.SQ
2021c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
2021d 72 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 r sqlite3ExprAff
2021e 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 inity(Expr *pExp
2021f 72 29 7b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 70 r){. int op = p
20220 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 Expr->op;. if(
20221 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 7b op==TK_SELECT ){
20222 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 . assert( pEx
20223 70 72 2d 3e 66 6c 61 67 73 26 45 50 5f 78 49 73 pr->flags&EP_xIs
20224 53 65 6c 65 63 74 20 29 3b 0a 20 20 20 20 72 65 Select );. re
20225 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 turn sqlite3Expr
20226 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e Affinity(pExpr->
20227 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 x.pSelect->pELis
20228 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a t->a[0].pExpr);.
20229 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
2022a 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 69 TE_OMIT_CAST. i
2022b 66 28 20 6f 70 3d 3d 54 4b 5f 43 41 53 54 20 29 f( op==TK_CAST )
2022c 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 45 {. assert( !E
2022d 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
2022e 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 Expr, EP_IntValu
2022f 65 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e e) );. return
20230 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 sqlite3Affinity
20231 54 79 70 65 28 70 45 78 70 72 2d 3e 75 2e 7a 54 Type(pExpr->u.zT
20232 6f 6b 65 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 oken);. }.#endi
20233 66 0a 20 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f f. if( (op==TK_
20234 41 47 47 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 AGG_COLUMN || op
20235 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f ==TK_COLUMN || o
20236 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 29 20 p==TK_REGISTER)
20237 0a 20 20 20 26 26 20 70 45 78 70 72 2d 3e 70 54 . && pExpr->pT
20238 61 62 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f ab!=0. ){. /
20239 2a 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 * op==TK_REGISTE
2023a 52 20 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62 R && pExpr->pTab
2023b 21 3d 30 20 68 61 70 70 65 6e 73 20 77 68 65 6e !=0 happens when
2023c 20 70 45 78 70 72 20 77 61 73 20 6f 72 69 67 69 pExpr was origi
2023d 6e 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 61 20 54 nally. ** a T
2023e 4b 5f 43 4f 4c 55 4d 4e 20 62 75 74 20 77 61 73 K_COLUMN but was
2023f 20 70 72 65 76 69 6f 75 73 6c 79 20 65 76 61 6c previously eval
20240 75 61 74 65 64 20 61 6e 64 20 63 61 63 68 65 64 uated and cached
20241 20 69 6e 20 61 20 72 65 67 69 73 74 65 72 20 2a in a register *
20242 2f 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 45 /. int j = pE
20243 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 xpr->iColumn;.
20244 20 20 69 66 28 20 6a 3c 30 20 29 20 72 65 74 75 if( j<0 ) retu
20245 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e rn SQLITE_AFF_IN
20246 54 45 47 45 52 3b 0a 20 20 20 20 61 73 73 65 72 TEGER;. asser
20247 74 28 20 70 45 78 70 72 2d 3e 70 54 61 62 20 26 t( pExpr->pTab &
20248 26 20 6a 3c 70 45 78 70 72 2d 3e 70 54 61 62 2d & j<pExpr->pTab-
20249 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 72 65 74 >nCol );. ret
2024a 75 72 6e 20 70 45 78 70 72 2d 3e 70 54 61 62 2d urn pExpr->pTab-
2024b 3e 61 43 6f 6c 5b 6a 5d 2e 61 66 66 69 6e 69 74 >aCol[j].affinit
2024c 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 y;. }. return
2024d 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3b pExpr->affinity;
2024e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
2024f 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 e collating sequ
20250 65 6e 63 65 20 66 6f 72 20 65 78 70 72 65 73 73 ence for express
20251 69 6f 6e 20 70 45 78 70 72 20 74 6f 20 62 65 20 ion pExpr to be
20252 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a the collating.**
20253 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 sequence named
20254 62 79 20 70 54 6f 6b 65 6e 2e 20 20 20 52 65 74 by pToken. Ret
20255 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
20256 20 74 68 65 20 72 65 76 69 73 65 64 20 65 78 70 the revised exp
20257 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 ression..** The
20258 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
20259 63 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 ce is marked as
2025a 22 65 78 70 6c 69 63 69 74 22 20 75 73 69 6e 67 "explicit" using
2025b 20 74 68 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 the EP_ExpColla
2025c 74 65 0a 2a 2a 20 66 6c 61 67 2e 20 20 41 6e 20 te.** flag. An
2025d 65 78 70 6c 69 63 69 74 20 63 6f 6c 6c 61 74 69 explicit collati
2025e 6e 67 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c ng sequence will
2025f 20 6f 76 65 72 72 69 64 65 20 69 6d 70 6c 69 63 override implic
20260 69 74 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 it.** collating
20261 73 65 71 75 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51 sequences..*/.SQ
20262 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
20263 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 53 65 r *sqlite3ExprSe
20264 74 43 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 tColl(Parse *pPa
20265 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 rse, Expr *pExpr
20266 2c 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c 6c 4e 61 , Token *pCollNa
20267 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f me){. char *zCo
20268 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ll = 0;
20269 20 20 20 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e /* Dequoted n
2026a 61 6d 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e ame of collation
2026b 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 43 sequence */. C
2026c 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 ollSeq *pColl;.
2026d 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
2026e 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f Parse->db;. zCo
2026f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 ll = sqlite3Name
20270 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 43 FromToken(db, pC
20271 6f 6c 6c 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 ollName);. if(
20272 70 45 78 70 72 20 26 26 20 7a 43 6f 6c 6c 20 29 pExpr && zColl )
20273 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 {. pColl = sq
20274 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 lite3LocateCollS
20275 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c eq(pParse, zColl
20276 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c );. if( pColl
20277 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d ){. pExpr-
20278 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a >pColl = pColl;.
20279 20 20 20 20 20 20 70 45 78 70 72 2d 3e 66 6c 61 pExpr->fla
2027a 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c gs |= EP_ExpColl
2027b 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ate;. }. }.
2027c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
2027d 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 72 65 74 b, zColl);. ret
2027e 75 72 6e 20 70 45 78 70 72 3b 0a 7d 0a 0a 2f 2a urn pExpr;.}../*
2027f 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 .** Return the d
20280 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e efault collation
20281 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 sequence for th
20282 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 e expression pEx
20283 70 72 2e 20 49 66 0a 2a 2a 20 74 68 65 72 65 20 pr. If.** there
20284 69 73 20 6e 6f 20 64 65 66 61 75 6c 74 20 63 6f is no default co
20285 6c 6c 61 74 69 6f 6e 20 74 79 70 65 2c 20 72 65 llation type, re
20286 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 turn 0..*/.SQLIT
20287 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 E_PRIVATE CollSe
20288 71 20 2a 73 71 6c 69 74 65 33 45 78 70 72 43 6f q *sqlite3ExprCo
20289 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 llSeq(Parse *pPa
2028a 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 rse, Expr *pExpr
2028b 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 ){. CollSeq *pC
2028c 6f 6c 6c 20 3d 20 30 3b 0a 20 20 45 78 70 72 20 oll = 0;. Expr
2028d 2a 70 20 3d 20 70 45 78 70 72 3b 0a 20 20 77 68 *p = pExpr;. wh
2028e 69 6c 65 28 20 41 4c 57 41 59 53 28 70 29 20 29 ile( ALWAYS(p) )
2028f 7b 0a 20 20 20 20 69 6e 74 20 6f 70 3b 0a 20 20 {. int op;.
20290 20 20 70 43 6f 6c 6c 20 3d 20 70 2d 3e 70 43 6f pColl = p->pCo
20291 6c 6c 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c ll;. if( pCol
20292 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 6f l ) break;. o
20293 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 20 20 69 p = p->op;. i
20294 66 28 20 70 2d 3e 70 54 61 62 21 3d 30 20 26 26 f( p->pTab!=0 &&
20295 20 28 0a 20 20 20 20 20 20 20 20 6f 70 3d 3d 54 (. op==T
20296 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 K_AGG_COLUMN ||
20297 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c op==TK_COLUMN ||
20298 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 op==TK_REGISTER
20299 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 54 52 49 47 47 || op==TK_TRIGG
2029a 45 52 0a 20 20 20 20 29 29 7b 0a 20 20 20 20 20 ER. )){.
2029b 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 /* op==TK_REGIS
2029c 54 45 52 20 26 26 20 70 2d 3e 70 54 61 62 21 3d TER && p->pTab!=
2029d 30 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 70 0 happens when p
2029e 45 78 70 72 20 77 61 73 20 6f 72 69 67 69 6e 61 Expr was origina
2029f 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 61 20 54 lly. ** a T
202a0 4b 5f 43 4f 4c 55 4d 4e 20 62 75 74 20 77 61 73 K_COLUMN but was
202a1 20 70 72 65 76 69 6f 75 73 6c 79 20 65 76 61 6c previously eval
202a2 75 61 74 65 64 20 61 6e 64 20 63 61 63 68 65 64 uated and cached
202a3 20 69 6e 20 61 20 72 65 67 69 73 74 65 72 20 2a in a register *
202a4 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 /. const ch
202a5 61 72 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 ar *zColl;.
202a6 20 69 6e 74 20 6a 20 3d 20 70 2d 3e 69 43 6f 6c int j = p->iCol
202a7 75 6d 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 6a umn;. if( j
202a8 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 >=0 ){. s
202a9 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
202aa 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20 rse->db;.
202ab 20 7a 43 6f 6c 6c 20 3d 20 70 2d 3e 70 54 61 62 zColl = p->pTab
202ac 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b ->aCol[j].zColl;
202ad 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d . pColl =
202ae 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c sqlite3FindColl
202af 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c Seq(db, ENC(db),
202b0 20 7a 43 6f 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 zColl, 0);.
202b1 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c pExpr->pColl
202b2 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 = pColl;.
202b3 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
202b4 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6f 70 21 }. if( op!
202b5 3d 54 4b 5f 43 41 53 54 20 26 26 20 6f 70 21 3d =TK_CAST && op!=
202b6 54 4b 5f 55 50 4c 55 53 20 29 7b 0a 20 20 20 20 TK_UPLUS ){.
202b7 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
202b8 20 20 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b p = p->pLeft;
202b9 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 . }. if( sqlit
202ba 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 70 e3CheckCollSeq(p
202bb 50 61 72 73 65 2c 20 70 43 6f 6c 6c 29 20 29 7b Parse, pColl) ){
202bc 20 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 30 3b . pColl = 0;
202bd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 . }. return pC
202be 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 oll;.}../*.** pE
202bf 78 70 72 20 69 73 20 61 6e 20 6f 70 65 72 61 6e xpr is an operan
202c0 64 20 6f 66 20 61 20 63 6f 6d 70 61 72 69 73 6f d of a compariso
202c1 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 61 66 66 n operator. aff
202c2 32 20 69 73 20 74 68 65 0a 2a 2a 20 74 79 70 65 2 is the.** type
202c3 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 affinity of the
202c4 20 6f 74 68 65 72 20 6f 70 65 72 61 6e 64 2e 20 other operand.
202c5 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 This routine re
202c6 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 74 79 70 turns the.** typ
202c7 65 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 e affinity that
202c8 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 should be used f
202c9 6f 72 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f or the compariso
202ca 6e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 53 n operator..*/.S
202cb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
202cc 61 72 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 ar sqlite3Compar
202cd 65 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a eAffinity(Expr *
202ce 70 45 78 70 72 2c 20 63 68 61 72 20 61 66 66 32 pExpr, char aff2
202cf 29 7b 0a 20 20 63 68 61 72 20 61 66 66 31 20 3d ){. char aff1 =
202d0 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 sqlite3ExprAffi
202d1 6e 69 74 79 28 70 45 78 70 72 29 3b 0a 20 20 69 nity(pExpr);. i
202d2 66 28 20 61 66 66 31 20 26 26 20 61 66 66 32 20 f( aff1 && aff2
202d3 29 7b 0a 20 20 20 20 2f 2a 20 42 6f 74 68 20 73 ){. /* Both s
202d4 69 64 65 73 20 6f 66 20 74 68 65 20 63 6f 6d 70 ides of the comp
202d5 61 72 69 73 6f 6e 20 61 72 65 20 63 6f 6c 75 6d arison are colum
202d6 6e 73 2e 20 49 66 20 6f 6e 65 20 68 61 73 20 6e ns. If one has n
202d7 75 6d 65 72 69 63 0a 20 20 20 20 2a 2a 20 61 66 umeric. ** af
202d8 66 69 6e 69 74 79 2c 20 75 73 65 20 74 68 61 74 finity, use that
202d9 2e 20 4f 74 68 65 72 77 69 73 65 20 75 73 65 20 . Otherwise use
202da 6e 6f 20 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 no affinity..
202db 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 */. if( sqli
202dc 74 65 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69 te3IsNumericAffi
202dd 6e 69 74 79 28 61 66 66 31 29 20 7c 7c 20 73 71 nity(aff1) || sq
202de 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41 66 lite3IsNumericAf
202df 66 69 6e 69 74 79 28 61 66 66 32 29 20 29 7b 0a finity(aff2) ){.
202e0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
202e1 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b ITE_AFF_NUMERIC;
202e2 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
202e3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
202e4 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a AFF_NONE;. }.
202e5 20 20 7d 65 6c 73 65 20 69 66 28 20 21 61 66 66 }else if( !aff
202e6 31 20 26 26 20 21 61 66 66 32 20 29 7b 0a 20 20 1 && !aff2 ){.
202e7 20 20 2f 2a 20 4e 65 69 74 68 65 72 20 73 69 64 /* Neither sid
202e8 65 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 e of the compari
202e9 73 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2e son is a column.
202ea 20 20 43 6f 6d 70 61 72 65 20 74 68 65 0a 20 20 Compare the.
202eb 20 20 2a 2a 20 72 65 73 75 6c 74 73 20 64 69 72 ** results dir
202ec 65 63 74 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 ectly.. */.
202ed 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
202ee 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 65 6c 73 AFF_NONE;. }els
202ef 65 7b 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 73 69 e{. /* One si
202f0 64 65 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 de is a column,
202f1 74 68 65 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 the other is not
202f2 2e 20 55 73 65 20 74 68 65 20 63 6f 6c 75 6d 6e . Use the column
202f3 73 20 61 66 66 69 6e 69 74 79 2e 20 2a 2f 0a 20 s affinity. */.
202f4 20 20 20 61 73 73 65 72 74 28 20 61 66 66 31 3d assert( aff1=
202f5 3d 30 20 7c 7c 20 61 66 66 32 3d 3d 30 20 29 3b =0 || aff2==0 );
202f6 0a 20 20 20 20 72 65 74 75 72 6e 20 28 61 66 66 . return (aff
202f7 31 20 2b 20 61 66 66 32 29 3b 0a 20 20 7d 0a 7d 1 + aff2);. }.}
202f8 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 ../*.** pExpr is
202f9 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 a comparison op
202fa 65 72 61 74 6f 72 2e 20 20 52 65 74 75 72 6e 20 erator. Return
202fb 74 68 65 20 74 79 70 65 20 61 66 66 69 6e 69 74 the type affinit
202fc 79 20 74 68 61 74 20 73 68 6f 75 6c 64 0a 2a 2a y that should.**
202fd 20 62 65 20 61 70 70 6c 69 65 64 20 74 6f 20 62 be applied to b
202fe 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 70 72 69 oth operands pri
202ff 6f 72 20 74 6f 20 64 6f 69 6e 67 20 74 68 65 20 or to doing the
20300 63 6f 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a 73 comparison..*/.s
20301 74 61 74 69 63 20 63 68 61 72 20 63 6f 6d 70 61 tatic char compa
20302 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 45 78 risonAffinity(Ex
20303 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 63 68 pr *pExpr){. ch
20304 61 72 20 61 66 66 3b 0a 20 20 61 73 73 65 72 74 ar aff;. assert
20305 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ( pExpr->op==TK_
20306 45 51 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d EQ || pExpr->op=
20307 3d 54 4b 5f 49 4e 20 7c 7c 20 70 45 78 70 72 2d =TK_IN || pExpr-
20308 3e 6f 70 3d 3d 54 4b 5f 4c 54 20 7c 7c 0a 20 20 >op==TK_LT ||.
20309 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f pExpr->o
2030a 70 3d 3d 54 4b 5f 47 54 20 7c 7c 20 70 45 78 70 p==TK_GT || pExp
2030b 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 45 20 7c 7c 20 r->op==TK_GE ||
2030c 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 45 pExpr->op==TK_LE
2030d 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70 45 ||. pE
2030e 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4e 45 20 29 xpr->op==TK_NE )
2030f 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 ;. assert( pExp
20310 72 2d 3e 70 4c 65 66 74 20 29 3b 0a 20 20 61 66 r->pLeft );. af
20311 66 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 f = sqlite3ExprA
20312 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 ffinity(pExpr->p
20313 4c 65 66 74 29 3b 0a 20 20 69 66 28 20 70 45 78 Left);. if( pEx
20314 70 72 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 pr->pRight ){.
20315 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43 aff = sqlite3C
20316 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 ompareAffinity(p
20317 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 61 66 Expr->pRight, af
20318 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 f);. }else if(
20319 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
2031a 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c pExpr, EP_xIsSel
2031b 65 63 74 29 20 29 7b 0a 20 20 20 20 61 66 66 20 ect) ){. aff
2031c 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 = sqlite3Compare
2031d 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e Affinity(pExpr->
2031e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 x.pSelect->pELis
2031f 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 61 t->a[0].pExpr, a
20320 66 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ff);. }else if(
20321 20 21 61 66 66 20 29 7b 0a 20 20 20 20 61 66 66 !aff ){. aff
20322 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f = SQLITE_AFF_NO
20323 4e 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e NE;. }. return
20324 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 aff;.}../*.** p
20325 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72 Expr is a compar
20326 69 73 6f 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c ison expression,
20327 20 65 67 2e 20 27 3d 27 2c 20 27 3c 27 2c 20 49 eg. '=', '<', I
20328 4e 28 2e 2e 2e 29 20 65 74 63 2e 0a 2a 2a 20 69 N(...) etc..** i
20329 64 78 5f 61 66 66 69 6e 69 74 79 20 69 73 20 74 dx_affinity is t
2032a 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 61 he affinity of a
2032b 6e 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e n indexed column
2032c 2e 20 52 65 74 75 72 6e 20 74 72 75 65 0a 2a 2a . Return true.**
2032d 20 69 66 20 74 68 65 20 69 6e 64 65 78 20 77 69 if the index wi
2032e 74 68 20 61 66 66 69 6e 69 74 79 20 69 64 78 5f th affinity idx_
2032f 61 66 66 69 6e 69 74 79 20 6d 61 79 20 62 65 20 affinity may be
20330 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
20331 74 0a 2a 2a 20 74 68 65 20 63 6f 6d 70 61 72 69 t.** the compari
20332 73 6f 6e 20 69 6e 20 70 45 78 70 72 2e 0a 2a 2f son in pExpr..*/
20333 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20334 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 int sqlite3Index
20335 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 20 AffinityOk(Expr
20336 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 64 78 *pExpr, char idx
20337 5f 61 66 66 69 6e 69 74 79 29 7b 0a 20 20 63 68 _affinity){. ch
20338 61 72 20 61 66 66 20 3d 20 63 6f 6d 70 61 72 69 ar aff = compari
20339 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70 sonAffinity(pExp
2033a 72 29 3b 0a 20 20 73 77 69 74 63 68 28 20 61 66 r);. switch( af
2033b 66 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 f ){. case SQ
2033c 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a 20 LITE_AFF_NONE:.
2033d 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
2033e 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 case SQLITE_A
2033f 46 46 5f 54 45 58 54 3a 0a 20 20 20 20 20 20 72 FF_TEXT:. r
20340 65 74 75 72 6e 20 69 64 78 5f 61 66 66 69 6e 69 eturn idx_affini
20341 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 ty==SQLITE_AFF_T
20342 45 58 54 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 EXT;. default
20343 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 :. return s
20344 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41 qlite3IsNumericA
20345 66 66 69 6e 69 74 79 28 69 64 78 5f 61 66 66 69 ffinity(idx_affi
20346 6e 69 74 79 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a nity);. }.}../*
20347 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 50 .** Return the P
20348 35 20 76 61 6c 75 65 20 74 68 61 74 20 73 68 6f 5 value that sho
20349 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 20 uld be used for
2034a 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72 69 a binary compari
2034b 73 6f 6e 0a 2a 2a 20 6f 70 63 6f 64 65 20 28 4f son.** opcode (O
2034c 50 5f 45 71 2c 20 4f 50 5f 47 65 20 65 74 63 2e P_Eq, OP_Ge etc.
2034d 29 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 ) used to compar
2034e 65 20 70 45 78 70 72 31 20 61 6e 64 20 70 45 78 e pExpr1 and pEx
2034f 70 72 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 pr2..*/.static u
20350 38 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 50 8 binaryCompareP
20351 35 28 45 78 70 72 20 2a 70 45 78 70 72 31 2c 20 5(Expr *pExpr1,
20352 45 78 70 72 20 2a 70 45 78 70 72 32 2c 20 69 6e Expr *pExpr2, in
20353 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 t jumpIfNull){.
20354 20 75 38 20 61 66 66 20 3d 20 28 63 68 61 72 29 u8 aff = (char)
20355 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e sqlite3ExprAffin
20356 69 74 79 28 70 45 78 70 72 32 29 3b 0a 20 20 61 ity(pExpr2);. a
20357 66 66 20 3d 20 28 75 38 29 73 71 6c 69 74 65 33 ff = (u8)sqlite3
20358 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 CompareAffinity(
20359 70 45 78 70 72 31 2c 20 61 66 66 29 20 7c 20 28 pExpr1, aff) | (
2035a 75 38 29 6a 75 6d 70 49 66 4e 75 6c 6c 3b 0a 20 u8)jumpIfNull;.
2035b 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a return aff;.}..
2035c 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 /*.** Return a p
2035d 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f ointer to the co
2035e 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
2035f 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 that should be
20360 75 73 65 64 20 62 79 0a 2a 2a 20 61 20 62 69 6e used by.** a bin
20361 61 72 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f ary comparison o
20362 70 65 72 61 74 6f 72 20 63 6f 6d 70 61 72 69 6e perator comparin
20363 67 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 g pLeft and pRig
20364 68 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ht..**.** If the
20365 20 6c 65 66 74 20 68 61 6e 64 20 65 78 70 72 65 left hand expre
20366 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c ssion has a coll
20367 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 ating sequence t
20368 79 70 65 2c 20 74 68 65 6e 20 69 74 20 69 73 0a ype, then it is.
20369 2a 2a 20 75 73 65 64 2e 20 4f 74 68 65 72 77 69 ** used. Otherwi
2036a 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e se the collation
2036b 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 sequence for th
2036c 65 20 72 69 67 68 74 20 68 61 6e 64 20 65 78 70 e right hand exp
2036d 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 75 73 ression.** is us
2036e 65 64 2c 20 6f 72 20 74 68 65 20 64 65 66 61 75 ed, or the defau
2036f 6c 74 20 28 42 49 4e 41 52 59 29 20 69 66 20 6e lt (BINARY) if n
20370 65 69 74 68 65 72 20 65 78 70 72 65 73 73 69 6f either expressio
20371 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e n has a collatin
20372 67 0a 2a 2a 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a g.** type..**.**
20373 20 41 72 67 75 6d 65 6e 74 20 70 52 69 67 68 74 Argument pRight
20374 20 28 62 75 74 20 6e 6f 74 20 70 4c 65 66 74 29 (but not pLeft)
20375 20 6d 61 79 20 62 65 20 61 20 6e 75 6c 6c 20 70 may be a null p
20376 6f 69 6e 74 65 72 2e 20 49 6e 20 74 68 69 73 20 ointer. In this
20377 63 61 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 6e case,.** it is n
20378 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a 2a ot considered..*
20379 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
2037a 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 CollSeq *sqlite
2037b 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 3BinaryCompareCo
2037c 6c 6c 53 65 71 28 0a 20 20 50 61 72 73 65 20 2a llSeq(. Parse *
2037d 70 50 61 72 73 65 2c 20 0a 20 20 45 78 70 72 20 pParse, . Expr
2037e 2a 70 4c 65 66 74 2c 20 0a 20 20 45 78 70 72 20 *pLeft, . Expr
2037f 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 43 6f 6c *pRight.){. Col
20380 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 61 lSeq *pColl;. a
20381 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b 0a ssert( pLeft );.
20382 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 66 6c 61 if( pLeft->fla
20383 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 gs & EP_ExpColla
20384 74 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 te ){. assert
20385 28 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 29 ( pLeft->pColl )
20386 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 4c ;. pColl = pL
20387 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 eft->pColl;. }e
20388 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 20 26 lse if( pRight &
20389 26 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 & pRight->flags
2038a 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 & EP_ExpCollate
2038b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
2038c 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 20 29 3b 0a Right->pColl );.
2038d 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 52 69 67 pColl = pRig
2038e 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 6c ht->pColl;. }el
2038f 73 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 se{. pColl =
20390 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 sqlite3ExprCollS
20391 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 eq(pParse, pLeft
20392 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c );. if( !pCol
20393 6c 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 6c l ){. pColl
20394 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
20395 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 52 llSeq(pParse, pR
20396 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ight);. }. }
20397 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b . return pColl;
20398 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 .}../*.** Genera
20399 74 65 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20 te the operands
2039a 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e for a comparison
2039b 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 65 66 operation. Bef
2039c 6f 72 65 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e ore.** generatin
2039d 67 20 74 68 65 20 63 6f 64 65 20 66 6f 72 20 65 g the code for e
2039e 61 63 68 20 6f 70 65 72 61 6e 64 2c 20 73 65 74 ach operand, set
2039f 20 74 68 65 20 45 50 5f 41 6e 79 41 66 66 0a 2a the EP_AnyAff.*
203a0 2a 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 65 78 * flag on the ex
203a1 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 74 pression so that
203a2 20 69 74 20 77 69 6c 6c 20 62 65 20 61 62 6c 65 it will be able
203a3 20 74 6f 20 75 73 65 64 20 61 0a 2a 2a 20 63 61 to used a.** ca
203a4 63 68 65 64 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 ched column valu
203a5 65 20 74 68 61 74 20 68 61 73 20 70 72 65 76 69 e that has previ
203a6 6f 75 73 6c 79 20 75 6e 64 65 72 67 6f 6e 65 20 ously undergone
203a7 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 63 an.** affinity c
203a8 68 61 6e 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 hange..*/.static
203a9 20 76 6f 69 64 20 63 6f 64 65 43 6f 6d 70 61 72 void codeCompar
203aa 65 4f 70 65 72 61 6e 64 73 28 0a 20 20 50 61 72 eOperands(. Par
203ab 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f se *pParse, /
203ac 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f * Parsing and co
203ad 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f de generating co
203ae 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 ntext */. Expr
203af 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 *pLeft, /*
203b0 54 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 The left operand
203b1 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 67 4c */. int *pRegL
203b2 65 66 74 2c 20 20 20 20 2f 2a 20 52 65 67 69 73 eft, /* Regis
203b3 74 65 72 20 77 68 65 72 65 20 6c 65 66 74 20 6f ter where left o
203b4 70 65 72 61 6e 64 20 69 73 20 73 74 6f 72 65 64 perand is stored
203b5 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72 65 65 */. int *pFree
203b6 4c 65 66 74 2c 20 20 20 2f 2a 20 46 72 65 65 20 Left, /* Free
203b7 74 68 69 73 20 72 65 67 69 73 74 65 72 20 77 68 this register wh
203b8 65 6e 20 64 6f 6e 65 20 2a 2f 0a 20 20 45 78 70 en done */. Exp
203b9 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f r *pRight, /
203ba 2a 20 54 68 65 20 72 69 67 68 74 20 6f 70 65 72 * The right oper
203bb 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 and */. int *pR
203bc 65 67 52 69 67 68 74 2c 20 20 20 2f 2a 20 52 65 egRight, /* Re
203bd 67 69 73 74 65 72 20 77 68 65 72 65 20 72 69 67 gister where rig
203be 68 74 20 6f 70 65 72 61 6e 64 20 69 73 20 73 74 ht operand is st
203bf 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 ored */. int *p
203c0 46 72 65 65 52 69 67 68 74 20 20 20 2f 2a 20 57 FreeRight /* W
203c1 72 69 74 65 20 74 65 6d 70 20 72 65 67 69 73 74 rite temp regist
203c2 65 72 20 66 6f 72 20 72 69 67 68 74 20 6f 70 65 er for right ope
203c3 72 61 6e 64 20 74 68 65 72 65 20 2a 2f 0a 29 7b rand there */.){
203c4 0a 20 20 77 68 69 6c 65 28 20 70 4c 65 66 74 2d . while( pLeft-
203c5 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 >op==TK_UPLUS )
203c6 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e 70 pLeft = pLeft->p
203c7 4c 65 66 74 3b 0a 20 20 70 4c 65 66 74 2d 3e 66 Left;. pLeft->f
203c8 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79 41 66 lags |= EP_AnyAf
203c9 66 3b 0a 20 20 2a 70 52 65 67 4c 65 66 74 20 3d f;. *pRegLeft =
203ca 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
203cb 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 4c 65 Temp(pParse, pLe
203cc 66 74 2c 20 70 46 72 65 65 4c 65 66 74 29 3b 0a ft, pFreeLeft);.
203cd 20 20 77 68 69 6c 65 28 20 70 52 69 67 68 74 2d while( pRight-
203ce 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 >op==TK_UPLUS )
203cf 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 74 2d pRight = pRight-
203d0 3e 70 4c 65 66 74 3b 0a 20 20 70 52 69 67 68 74 >pLeft;. pRight
203d1 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e ->flags |= EP_An
203d2 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 52 69 67 yAff;. *pRegRig
203d3 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 ht = sqlite3Expr
203d4 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c CodeTemp(pParse,
203d5 20 70 52 69 67 68 74 2c 20 70 46 72 65 65 52 69 pRight, pFreeRi
203d6 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 ght);.}../*.** G
203d7 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 enerate code for
203d8 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 a comparison op
203d9 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 erator..*/.stati
203da 63 20 69 6e 74 20 63 6f 64 65 43 6f 6d 70 61 72 c int codeCompar
203db 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 e(. Parse *pPar
203dc 73 65 2c 20 20 20 20 2f 2a 20 54 68 65 20 70 61 se, /* The pa
203dd 72 73 69 6e 67 20 28 61 6e 64 20 63 6f 64 65 20 rsing (and code
203de 67 65 6e 65 72 61 74 69 6e 67 29 20 63 6f 6e 74 generating) cont
203df 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ext */. Expr *p
203e0 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 Left, /* Th
203e1 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a e left operand *
203e2 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 /. Expr *pRight
203e3 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 , /* The rig
203e4 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 ht operand */.
203e5 69 6e 74 20 6f 70 63 6f 64 65 2c 20 20 20 20 20 int opcode,
203e6 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 61 72 69 /* The compari
203e7 73 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 son opcode */.
203e8 69 6e 74 20 69 6e 31 2c 20 69 6e 74 20 69 6e 32 int in1, int in2
203e9 2c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f , /* Register ho
203ea 6c 64 69 6e 67 20 6f 70 65 72 61 6e 64 73 20 2a lding operands *
203eb 2f 0a 20 20 69 6e 74 20 64 65 73 74 2c 20 20 20 /. int dest,
203ec 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 /* Jump he
203ed 72 65 20 69 66 20 74 72 75 65 2e 20 20 2a 2f 0a re if true. */.
203ee 20 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c int jumpIfNull
203ef 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 /* If true,
203f0 6a 75 6d 70 20 69 66 20 65 69 74 68 65 72 20 6f jump if either o
203f1 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a perand is NULL *
203f2 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 35 3b 0a 20 /.){. int p5;.
203f3 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 43 6f 6c int addr;. Col
203f4 6c 53 65 71 20 2a 70 34 3b 0a 0a 20 20 70 34 20 lSeq *p4;.. p4
203f5 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 = sqlite3BinaryC
203f6 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 ompareCollSeq(pP
203f7 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 arse, pLeft, pRi
203f8 67 68 74 29 3b 0a 20 20 70 35 20 3d 20 62 69 6e ght);. p5 = bin
203f9 61 72 79 43 6f 6d 70 61 72 65 50 35 28 70 4c 65 aryCompareP5(pLe
203fa 66 74 2c 20 70 52 69 67 68 74 2c 20 6a 75 6d 70 ft, pRight, jump
203fb 49 66 4e 75 6c 6c 29 3b 0a 20 20 61 64 64 72 20 IfNull);. addr
203fc 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 = sqlite3VdbeAdd
203fd 4f 70 34 28 70 50 61 72 73 65 2d 3e 70 56 64 62 Op4(pParse->pVdb
203fe 65 2c 20 6f 70 63 6f 64 65 2c 20 69 6e 32 2c 20 e, opcode, in2,
203ff 64 65 73 74 2c 20 69 6e 31 2c 0a 20 20 20 20 20 dest, in1,.
20400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20401 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 34 2c (void*)p4,
20402 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 P4_COLLSEQ);.
20403 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
20404 65 50 35 28 70 50 61 72 73 65 2d 3e 70 56 64 62 eP5(pParse->pVdb
20405 65 2c 20 28 75 38 29 70 35 29 3b 0a 20 20 69 66 e, (u8)p5);. if
20406 28 20 28 70 35 20 26 20 53 51 4c 49 54 45 5f 41 ( (p5 & SQLITE_A
20407 46 46 5f 4d 41 53 4b 29 21 3d 53 51 4c 49 54 45 FF_MASK)!=SQLITE
20408 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 _AFF_NONE ){.
20409 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
2040a 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 eAffinityChange(
2040b 70 50 61 72 73 65 2c 20 69 6e 31 2c 20 31 29 3b pParse, in1, 1);
2040c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 . sqlite3Expr
2040d 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 CacheAffinityCha
2040e 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e 32 2c nge(pParse, in2,
2040f 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 1);. }. retur
20410 6e 20 61 64 64 72 3b 0a 7d 0a 0a 23 69 66 20 53 n addr;.}..#if S
20411 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 QLITE_MAX_EXPR_D
20412 45 50 54 48 3e 30 0a 2f 2a 0a 2a 2a 20 43 68 65 EPTH>0./*.** Che
20413 63 6b 20 74 68 61 74 20 61 72 67 75 6d 65 6e 74 ck that argument
20414 20 6e 48 65 69 67 68 74 20 69 73 20 6c 65 73 73 nHeight is less
20415 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
20416 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a o the maximum.**
20417 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70 74 expression dept
20418 68 20 61 6c 6c 6f 77 65 64 2e 20 49 66 20 69 74 h allowed. If it
20419 20 69 73 20 6e 6f 74 2c 20 6c 65 61 76 65 20 61 is not, leave a
2041a 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 n error message
2041b 69 6e 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a 2f in.** pParse..*/
2041c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
2041d 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
2041e 68 65 63 6b 48 65 69 67 68 74 28 50 61 72 73 65 heckHeight(Parse
2041f 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6e 48 *pParse, int nH
20420 65 69 67 68 74 29 7b 0a 20 20 69 6e 74 20 72 63 eight){. int rc
20421 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
20422 69 6e 74 20 6d 78 48 65 69 67 68 74 20 3d 20 70 int mxHeight = p
20423 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69 Parse->db->aLimi
20424 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 t[SQLITE_LIMIT_E
20425 58 50 52 5f 44 45 50 54 48 5d 3b 0a 20 20 69 66 XPR_DEPTH];. if
20426 28 20 6e 48 65 69 67 68 74 3e 6d 78 48 65 69 67 ( nHeight>mxHeig
20427 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ht ){. sqlite
20428 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
20429 2c 20 0a 20 20 20 20 20 20 20 22 45 78 70 72 65 , . "Expre
2042a 73 73 69 6f 6e 20 74 72 65 65 20 69 73 20 74 6f ssion tree is to
2042b 6f 20 6c 61 72 67 65 20 28 6d 61 78 69 6d 75 6d o large (maximum
2042c 20 64 65 70 74 68 20 25 64 29 22 2c 20 6d 78 48 depth %d)", mxH
2042d 65 69 67 68 74 0a 20 20 20 20 29 3b 0a 20 20 20 eight. );.
2042e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 rc = SQLITE_ERR
2042f 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e OR;. }. return
20430 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 rc;.}../* The f
20431 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 66 ollowing three f
20432 75 6e 63 74 69 6f 6e 73 2c 20 68 65 69 67 68 74 unctions, height
20433 4f 66 45 78 70 72 28 29 2c 20 68 65 69 67 68 74 OfExpr(), height
20434 4f 66 45 78 70 72 4c 69 73 74 28 29 0a 2a 2a 20 OfExprList().**
20435 61 6e 64 20 68 65 69 67 68 74 4f 66 53 65 6c 65 and heightOfSele
20436 63 74 28 29 2c 20 61 72 65 20 75 73 65 64 20 74 ct(), are used t
20437 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
20438 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 0a 2a maximum height.*
20439 2a 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73 73 * of any express
2043a 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65 6e ion tree referen
2043b 63 65 64 20 62 79 20 74 68 65 20 73 74 72 75 63 ced by the struc
2043c 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20 74 ture passed as t
2043d 68 65 0a 2a 2a 20 66 69 72 73 74 20 61 72 67 75 he.** first argu
2043e 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ment..**.** If t
2043f 68 69 73 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 his maximum heig
20440 68 74 20 69 73 20 67 72 65 61 74 65 72 20 74 68 ht is greater th
20441 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 an the current v
20442 61 6c 75 65 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 alue pointed.**
20443 74 6f 20 62 79 20 70 6e 48 65 69 67 68 74 2c 20 to by pnHeight,
20444 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
20445 65 74 65 72 2c 20 74 68 65 6e 20 73 65 74 20 2a eter, then set *
20446 70 6e 48 65 69 67 68 74 20 74 6f 20 74 68 61 74 pnHeight to that
20447 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 .** value..*/.st
20448 61 74 69 63 20 76 6f 69 64 20 68 65 69 67 68 74 atic void height
20449 4f 66 45 78 70 72 28 45 78 70 72 20 2a 70 2c 20 OfExpr(Expr *p,
2044a 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a int *pnHeight){.
2044b 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 if( p ){. i
2044c 66 28 20 70 2d 3e 6e 48 65 69 67 68 74 3e 2a 70 f( p->nHeight>*p
2044d 6e 48 65 69 67 68 74 20 29 7b 0a 20 20 20 20 20 nHeight ){.
2044e 20 2a 70 6e 48 65 69 67 68 74 20 3d 20 70 2d 3e *pnHeight = p->
2044f 6e 48 65 69 67 68 74 3b 0a 20 20 20 20 7d 0a 20 nHeight;. }.
20450 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 }.}.static void
20451 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 heightOfExprLis
20452 74 28 45 78 70 72 4c 69 73 74 20 2a 70 2c 20 69 t(ExprList *p, i
20453 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20 nt *pnHeight){.
20454 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 6e if( p ){. in
20455 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 t i;. for(i=0
20456 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b 20 69 2b ; i<p->nExpr; i+
20457 2b 29 7b 0a 20 20 20 20 20 20 68 65 69 67 68 74 +){. height
20458 4f 66 45 78 70 72 28 70 2d 3e 61 5b 69 5d 2e 70 OfExpr(p->a[i].p
20459 45 78 70 72 2c 20 70 6e 48 65 69 67 68 74 29 3b Expr, pnHeight);
2045a 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 . }. }.}.sta
2045b 74 69 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f tic void heightO
2045c 66 53 65 6c 65 63 74 28 53 65 6c 65 63 74 20 2a fSelect(Select *
2045d 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 p, int *pnHeight
2045e 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 ){. if( p ){.
2045f 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 heightOfExpr(p
20460 2d 3e 70 57 68 65 72 65 2c 20 70 6e 48 65 69 67 ->pWhere, pnHeig
20461 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f ht);. heightO
20462 66 45 78 70 72 28 70 2d 3e 70 48 61 76 69 6e 67 fExpr(p->pHaving
20463 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 , pnHeight);.
20464 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d heightOfExpr(p-
20465 3e 70 4c 69 6d 69 74 2c 20 70 6e 48 65 69 67 68 >pLimit, pnHeigh
20466 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 t);. heightOf
20467 45 78 70 72 28 70 2d 3e 70 4f 66 66 73 65 74 2c Expr(p->pOffset,
20468 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 pnHeight);.
20469 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 heightOfExprList
2046a 28 70 2d 3e 70 45 4c 69 73 74 2c 20 70 6e 48 65 (p->pEList, pnHe
2046b 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 ight);. heigh
2046c 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 tOfExprList(p->p
2046d 47 72 6f 75 70 42 79 2c 20 70 6e 48 65 69 67 68 GroupBy, pnHeigh
2046e 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 t);. heightOf
2046f 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 4f 72 64 ExprList(p->pOrd
20470 65 72 42 79 2c 20 70 6e 48 65 69 67 68 74 29 3b erBy, pnHeight);
20471 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53 65 6c . heightOfSel
20472 65 63 74 28 70 2d 3e 70 50 72 69 6f 72 2c 20 70 ect(p->pPrior, p
20473 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 7d 0a nHeight);. }.}.
20474 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 ./*.** Set the E
20475 78 70 72 2e 6e 48 65 69 67 68 74 20 76 61 72 69 xpr.nHeight vari
20476 61 62 6c 65 20 69 6e 20 74 68 65 20 73 74 72 75 able in the stru
20477 63 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20 cture passed as
20478 61 6e 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e an .** argument.
20479 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 An expression w
2047a 69 74 68 20 6e 6f 20 63 68 69 6c 64 72 65 6e 2c ith no children,
2047b 20 45 78 70 72 2e 70 4c 69 73 74 20 6f 72 20 0a Expr.pList or .
2047c 2a 2a 20 45 78 70 72 2e 70 53 65 6c 65 63 74 20 ** Expr.pSelect
2047d 6d 65 6d 62 65 72 20 68 61 73 20 61 20 68 65 69 member has a hei
2047e 67 68 74 20 6f 66 20 31 2e 20 41 6e 79 20 6f 74 ght of 1. Any ot
2047f 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a her expression.*
20480 2a 20 68 61 73 20 61 20 68 65 69 67 68 74 20 65 * has a height e
20481 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 69 qual to the maxi
20482 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20 61 6e mum height of an
20483 79 20 6f 74 68 65 72 20 0a 2a 2a 20 72 65 66 65 y other .** refe
20484 72 65 6e 63 65 64 20 45 78 70 72 20 70 6c 75 73 renced Expr plus
20485 20 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 one..*/.static
20486 76 6f 69 64 20 65 78 70 72 53 65 74 48 65 69 67 void exprSetHeig
20487 68 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 ht(Expr *p){. i
20488 6e 74 20 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a nt nHeight = 0;.
20489 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 heightOfExpr(p
2048a 2d 3e 70 4c 65 66 74 2c 20 26 6e 48 65 69 67 68 ->pLeft, &nHeigh
2048b 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 78 t);. heightOfEx
2048c 70 72 28 70 2d 3e 70 52 69 67 68 74 2c 20 26 6e pr(p->pRight, &n
2048d 48 65 69 67 68 74 29 3b 0a 20 20 69 66 28 20 45 Height);. if( E
2048e 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
2048f 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 , EP_xIsSelect)
20490 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53 ){. heightOfS
20491 65 6c 65 63 74 28 70 2d 3e 78 2e 70 53 65 6c 65 elect(p->x.pSele
20492 63 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 ct, &nHeight);.
20493 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 65 69 67 }else{. heig
20494 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e htOfExprList(p->
20495 78 2e 70 4c 69 73 74 2c 20 26 6e 48 65 69 67 68 x.pList, &nHeigh
20496 74 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e 48 65 t);. }. p->nHe
20497 69 67 68 74 20 3d 20 6e 48 65 69 67 68 74 20 2b ight = nHeight +
20498 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 1;.}../*.** Set
20499 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 the Expr.nHeigh
2049a 74 20 76 61 72 69 61 62 6c 65 20 75 73 69 6e 67 t variable using
2049b 20 74 68 65 20 65 78 70 72 53 65 74 48 65 69 67 the exprSetHeig
2049c 68 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 49 ht() function. I
2049d 66 0a 2a 2a 20 74 68 65 20 68 65 69 67 68 74 20 f.** the height
2049e 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
2049f 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f the maximum allo
204a0 77 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 64 wed expression d
204a1 65 70 74 68 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 epth,.** leave a
204a2 6e 20 65 72 72 6f 72 20 69 6e 20 70 50 61 72 73 n error in pPars
204a3 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
204a4 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
204a5 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 50 3ExprSetHeight(P
204a6 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 arse *pParse, Ex
204a7 70 72 20 2a 70 29 7b 0a 20 20 65 78 70 72 53 65 pr *p){. exprSe
204a8 74 48 65 69 67 68 74 28 70 29 3b 0a 20 20 73 71 tHeight(p);. sq
204a9 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 lite3ExprCheckHe
204aa 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e ight(pParse, p->
204ab 6e 48 65 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a nHeight);.}../*.
204ac 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61 ** Return the ma
204ad 78 69 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20 ximum height of
204ae 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 any expression t
204af 72 65 65 20 72 65 66 65 72 65 6e 63 65 64 0a 2a ree referenced.*
204b0 2a 20 62 79 20 74 68 65 20 73 65 6c 65 63 74 20 * by the select
204b1 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 statement passed
204b2 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e as an argument.
204b3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
204b4 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 TE int sqlite3Se
204b5 6c 65 63 74 45 78 70 72 48 65 69 67 68 74 28 53 lectExprHeight(S
204b6 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 elect *p){. int
204b7 20 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 nHeight = 0;.
204b8 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 heightOfSelect(p
204b9 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 72 , &nHeight);. r
204ba 65 74 75 72 6e 20 6e 48 65 69 67 68 74 3b 0a 7d eturn nHeight;.}
204bb 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
204bc 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28 79 exprSetHeight(y
204bd 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ).#endif /* SQLI
204be 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 TE_MAX_EXPR_DEPT
204bf 48 3e 30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 H>0 */../*.** Th
204c0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 is routine is th
204c1 65 20 63 6f 72 65 20 61 6c 6c 6f 63 61 74 6f 72 e core allocator
204c2 20 66 6f 72 20 45 78 70 72 20 6e 6f 64 65 73 2e for Expr nodes.
204c3 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 .**.** Construct
204c4 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f a new expressio
204c5 6e 20 6e 6f 64 65 20 61 6e 64 20 72 65 74 75 72 n node and retur
204c6 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 n a pointer to i
204c7 74 2e 20 20 4d 65 6d 6f 72 79 0a 2a 2a 20 66 6f t. Memory.** fo
204c8 72 20 74 68 69 73 20 6e 6f 64 65 20 61 6e 64 20 r this node and
204c9 66 6f 72 20 74 68 65 20 70 54 6f 6b 65 6e 20 61 for the pToken a
204ca 72 67 75 6d 65 6e 74 20 69 73 20 61 20 73 69 6e rgument is a sin
204cb 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a gle allocation.*
204cc 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 * obtained from
204cd 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28 sqlite3DbMalloc(
204ce 29 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 ). The calling
204cf 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 72 function.** is r
204d0 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d esponsible for m
204d1 61 6b 69 6e 67 20 73 75 72 65 20 74 68 65 20 6e aking sure the n
204d2 6f 64 65 20 65 76 65 6e 74 75 61 6c 6c 79 20 67 ode eventually g
204d3 65 74 73 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a ets freed..**.**
204d4 20 49 66 20 64 65 71 75 6f 74 65 20 69 73 20 74 If dequote is t
204d5 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 74 6f rue, then the to
204d6 6b 65 6e 20 28 69 66 20 69 74 20 65 78 69 73 74 ken (if it exist
204d7 73 29 20 69 73 20 64 65 71 75 6f 74 65 64 2e 0a s) is dequoted..
204d8 2a 2a 20 49 66 20 64 65 71 75 6f 74 65 20 69 73 ** If dequote is
204d9 20 66 61 6c 73 65 2c 20 6e 6f 20 64 65 71 75 6f false, no dequo
204da 74 69 6e 67 20 69 73 20 70 65 72 66 6f 72 6d 61 ting is performa
204db 6e 63 65 2e 20 20 54 68 65 20 64 65 51 75 6f 74 nce. The deQuot
204dc 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 e.** parameter i
204dd 73 20 69 67 6e 6f 72 65 64 20 69 66 20 70 54 6f s ignored if pTo
204de 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 6f 72 20 69 ken is NULL or i
204df 66 20 74 68 65 20 74 6f 6b 65 6e 20 64 6f 65 73 f the token does
204e0 20 6e 6f 74 0a 2a 2a 20 61 70 70 65 61 72 20 74 not.** appear t
204e1 6f 20 62 65 20 71 75 6f 74 65 64 2e 20 20 49 66 o be quoted. If
204e2 20 74 68 65 20 71 75 6f 74 65 73 20 77 65 72 65 the quotes were
204e3 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 2e 2e of the form "..
204e4 2e 22 20 28 64 6f 75 62 6c 65 2d 71 75 6f 74 65 ." (double-quote
204e5 73 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 45 s).** then the E
204e6 50 5f 44 62 6c 51 75 6f 74 65 64 20 66 6c 61 67 P_DblQuoted flag
204e7 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20 65 is set on the e
204e8 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 2e 0a xpression node..
204e9 2a 2a 0a 2a 2a 20 53 70 65 63 69 61 6c 20 63 61 **.** Special ca
204ea 73 65 3a 20 20 49 66 20 6f 70 3d 3d 54 4b 5f 49 se: If op==TK_I
204eb 4e 54 45 47 45 52 20 61 6e 64 20 70 54 6f 6b 65 NTEGER and pToke
204ec 6e 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 n points to a st
204ed 72 69 6e 67 20 74 68 61 74 0a 2a 2a 20 63 61 6e ring that.** can
204ee 20 62 65 20 74 72 61 6e 73 6c 61 74 65 64 20 69 be translated i
204ef 6e 74 6f 20 61 20 33 32 2d 62 69 74 20 69 6e 74 nto a 32-bit int
204f0 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20 74 eger, then the t
204f1 6f 6b 65 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 73 oken is not.** s
204f2 74 6f 72 65 64 20 69 6e 20 75 2e 7a 54 6f 6b 65 tored in u.zToke
204f3 6e 2e 20 20 49 6e 73 74 65 61 64 2c 20 74 68 65 n. Instead, the
204f4 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 integer values
204f5 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e is written.** in
204f6 74 6f 20 75 2e 69 56 61 6c 75 65 20 61 6e 64 20 to u.iValue and
204f7 74 68 65 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 the EP_IntValue
204f8 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20 4e 6f flag is set. No
204f9 20 65 78 74 72 61 20 73 74 6f 72 61 67 65 0a 2a extra storage.*
204fa 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 * is allocated t
204fb 6f 20 68 6f 6c 64 20 74 68 65 20 69 6e 74 65 67 o hold the integ
204fc 65 72 20 74 65 78 74 20 61 6e 64 20 74 68 65 20 er text and the
204fd 64 65 71 75 6f 74 65 20 66 6c 61 67 20 69 73 20 dequote flag is
204fe 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 ignored..*/.SQLI
204ff 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
20500 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f *sqlite3ExprAllo
20501 63 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 c(. sqlite3 *db
20502 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
20503 48 61 6e 64 6c 65 20 66 6f 72 20 73 71 6c 69 74 Handle for sqlit
20504 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 e3DbMallocZero()
20505 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c 29 20 2a (may be null) *
20506 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 /. int op,
20507 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
20508 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65 xpression opcode
20509 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 */. const Toke
2050a 6e 20 2a 70 54 6f 6b 65 6e 2c 20 20 20 20 2f 2a n *pToken, /*
2050b 20 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74 2e Token argument.
2050c 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 Might be NULL
2050d 2a 2f 0a 20 20 69 6e 74 20 64 65 71 75 6f 74 65 */. int dequote
2050e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
2050f 54 72 75 65 20 74 6f 20 64 65 71 75 6f 74 65 20 True to dequote
20510 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e */.){. Expr *pN
20511 65 77 3b 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 ew;. int nExtra
20512 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 56 61 6c = 0;. int iVal
20513 75 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 ue = 0;.. if( p
20514 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 69 66 28 Token ){. if(
20515 20 6f 70 21 3d 54 4b 5f 49 4e 54 45 47 45 52 20 op!=TK_INTEGER
20516 7c 7c 20 70 54 6f 6b 65 6e 2d 3e 7a 3d 3d 30 0a || pToken->z==0.
20517 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c || sql
20518 69 74 65 33 47 65 74 49 6e 74 33 32 28 70 54 6f ite3GetInt32(pTo
20519 6b 65 6e 2d 3e 7a 2c 20 26 69 56 61 6c 75 65 29 ken->z, &iValue)
2051a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 45 78 ==0 ){. nEx
2051b 74 72 61 20 3d 20 70 54 6f 6b 65 6e 2d 3e 6e 2b tra = pToken->n+
2051c 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 1;. }. }. p
2051d 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d New = sqlite3DbM
2051e 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 allocZero(db, si
2051f 7a 65 6f 66 28 45 78 70 72 29 2b 6e 45 78 74 72 zeof(Expr)+nExtr
20520 61 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 a);. if( pNew )
20521 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6f 70 20 3d {. pNew->op =
20522 20 28 75 38 29 6f 70 3b 0a 20 20 20 20 70 4e 65 (u8)op;. pNe
20523 77 2d 3e 69 41 67 67 20 3d 20 2d 31 3b 0a 20 20 w->iAgg = -1;.
20524 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a if( pToken ){.
20525 20 20 20 20 20 20 69 66 28 20 6e 45 78 74 72 61 if( nExtra
20526 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ==0 ){. p
20527 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 New->flags |= EP
20528 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20 20 _IntValue;.
20529 20 20 20 70 4e 65 77 2d 3e 75 2e 69 56 61 6c 75 pNew->u.iValu
2052a 65 20 3d 20 69 56 61 6c 75 65 3b 0a 20 20 20 20 e = iValue;.
2052b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
2052c 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20 20 20 int c;.
2052d 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d pNew->u.zToken =
2052e 20 28 63 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d (char*)&pNew[1]
2052f 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 ;. memcpy
20530 28 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c (pNew->u.zToken,
20531 20 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b pToken->z, pTok
20532 65 6e 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20 en->n);.
20533 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 70 pNew->u.zToken[p
20534 54 6f 6b 65 6e 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 Token->n] = 0;.
20535 20 20 20 20 20 20 20 69 66 28 20 64 65 71 75 6f if( dequo
20536 74 65 20 26 26 20 6e 45 78 74 72 61 3e 3d 33 20 te && nExtra>=3
20537 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 . &&
20538 20 28 28 63 20 3d 20 70 54 6f 6b 65 6e 2d 3e 7a ((c = pToken->z
20539 5b 30 5d 29 3d 3d 27 5c 27 27 20 7c 7c 20 63 3d [0])=='\'' || c=
2053a 3d 27 22 27 20 7c 7c 20 63 3d 3d 27 5b 27 20 7c ='"' || c=='[' |
2053b 7c 20 63 3d 3d 27 60 27 29 20 29 7b 0a 20 20 20 | c=='`') ){.
2053c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 sqlite3De
2053d 71 75 6f 74 65 28 70 4e 65 77 2d 3e 75 2e 7a 54 quote(pNew->u.zT
2053e 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 oken);.
2053f 20 69 66 28 20 63 3d 3d 27 22 27 20 29 20 70 4e if( c=='"' ) pN
20540 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f ew->flags |= EP_
20541 44 62 6c 51 75 6f 74 65 64 3b 0a 20 20 20 20 20 DblQuoted;.
20542 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
20543 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 }.#if SQLITE_MA
20544 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 X_EXPR_DEPTH>0.
20545 20 20 20 70 4e 65 77 2d 3e 6e 48 65 69 67 68 74 pNew->nHeight
20546 20 3d 20 31 3b 0a 23 65 6e 64 69 66 20 20 0a 20 = 1;.#endif .
20547 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 }. return pNew
20548 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 ;.}../*.** Alloc
20549 61 74 65 20 61 20 6e 65 77 20 65 78 70 72 65 73 ate a new expres
2054a 73 69 6f 6e 20 6e 6f 64 65 20 66 72 6f 6d 20 61 sion node from a
2054b 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
2054c 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 61 73 0a token that has.
2054d 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ** already been
2054e 64 65 71 75 6f 74 65 64 2e 0a 2a 2f 0a 53 51 4c dequoted..*/.SQL
2054f 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
20550 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28 0a 20 *sqlite3Expr(.
20551 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 sqlite3 *db,
20552 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 /* Hand
20553 6c 65 20 66 6f 72 20 73 71 6c 69 74 65 33 44 62 le for sqlite3Db
20554 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28 6d 61 MallocZero() (ma
20555 79 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0a 20 20 y be null) */.
20556 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 int op,
20557 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 /* Expre
20558 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a ssion opcode */.
20559 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 const char *zT
2055a 6f 6b 65 6e 20 20 20 20 20 20 2f 2a 20 54 6f 6b oken /* Tok
2055b 65 6e 20 61 72 67 75 6d 65 6e 74 2e 20 20 4d 69 en argument. Mi
2055c 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 29 ght be NULL */.)
2055d 7b 0a 20 20 54 6f 6b 65 6e 20 78 3b 0a 20 20 78 {. Token x;. x
2055e 2e 7a 20 3d 20 7a 54 6f 6b 65 6e 3b 0a 20 20 78 .z = zToken;. x
2055f 2e 6e 20 3d 20 7a 54 6f 6b 65 6e 20 3f 20 73 71 .n = zToken ? sq
20560 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 lite3Strlen30(zT
20561 6f 6b 65 6e 29 20 3a 20 30 3b 0a 20 20 72 65 74 oken) : 0;. ret
20562 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 41 urn sqlite3ExprA
20563 6c 6c 6f 63 28 64 62 2c 20 6f 70 2c 20 26 78 2c lloc(db, op, &x,
20564 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 0);.}../*.** At
20565 74 61 63 68 20 73 75 62 74 72 65 65 73 20 70 4c tach subtrees pL
20566 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 20 74 eft and pRight t
20567 6f 20 74 68 65 20 45 78 70 72 20 6e 6f 64 65 20 o the Expr node
20568 70 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 pRoot..**.** If
20569 70 52 6f 6f 74 3d 3d 4e 55 4c 4c 20 74 68 61 74 pRoot==NULL that
2056a 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 65 means that a me
2056b 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
2056c 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 error has occurr
2056d 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 61 74 20 63 ed..** In that c
2056e 61 73 65 2c 20 64 65 6c 65 74 65 20 74 68 65 20 ase, delete the
2056f 73 75 62 74 72 65 65 73 20 70 4c 65 66 74 20 61 subtrees pLeft a
20570 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2f 0a 53 51 nd pRight..*/.SQ
20571 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
20572 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 74 74 d sqlite3ExprAtt
20573 61 63 68 53 75 62 74 72 65 65 73 28 0a 20 20 73 achSubtrees(. s
20574 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 45 78 qlite3 *db,. Ex
20575 70 72 20 2a 70 52 6f 6f 74 2c 0a 20 20 45 78 70 pr *pRoot,. Exp
20576 72 20 2a 70 4c 65 66 74 2c 0a 20 20 45 78 70 72 r *pLeft,. Expr
20577 20 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 69 66 *pRight.){. if
20578 28 20 70 52 6f 6f 74 3d 3d 30 20 29 7b 0a 20 20 ( pRoot==0 ){.
20579 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 assert( db->ma
2057a 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
2057b 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c sqlite3ExprDel
2057c 65 74 65 28 64 62 2c 20 70 4c 65 66 74 29 3b 0a ete(db, pLeft);.
2057d 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 sqlite3ExprD
2057e 65 6c 65 74 65 28 64 62 2c 20 70 52 69 67 68 74 elete(db, pRight
2057f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
20580 69 66 28 20 70 52 69 67 68 74 20 29 7b 0a 20 20 if( pRight ){.
20581 20 20 20 20 70 52 6f 6f 74 2d 3e 70 52 69 67 68 pRoot->pRigh
20582 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 20 20 20 t = pRight;.
20583 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 66 6c if( pRight->fl
20584 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c ags & EP_ExpColl
20585 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ate ){. p
20586 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 Root->flags |= E
20587 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 P_ExpCollate;.
20588 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70 43 6f pRoot->pCo
20589 6c 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f ll = pRight->pCo
2058a 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ll;. }.
2058b 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 20 }. if( pLeft
2058c 29 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e ){. pRoot->
2058d 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 pLeft = pLeft;.
2058e 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e if( pLeft->
2058f 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f flags & EP_ExpCo
20590 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 llate ){.
20591 20 70 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c 3d pRoot->flags |=
20592 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a EP_ExpCollate;.
20593 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70 pRoot->p
20594 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43 Coll = pLeft->pC
20595 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 oll;. }.
20596 20 7d 0a 20 20 20 20 65 78 70 72 53 65 74 48 65 }. exprSetHe
20597 69 67 68 74 28 70 52 6f 6f 74 29 3b 0a 20 20 7d ight(pRoot);. }
20598 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
20599 74 65 20 61 20 45 78 70 72 20 6e 6f 64 65 20 77 te a Expr node w
2059a 68 69 63 68 20 6a 6f 69 6e 73 20 61 73 20 6d 61 hich joins as ma
2059b 6e 79 20 61 73 20 74 77 6f 20 73 75 62 74 72 65 ny as two subtre
2059c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 6f 72 es..**.** One or
2059d 20 62 6f 74 68 20 6f 66 20 74 68 65 20 73 75 62 both of the sub
2059e 74 72 65 65 73 20 63 61 6e 20 62 65 20 4e 55 4c trees can be NUL
2059f 4c 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 L. Return a poi
205a0 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0a nter to the new.
205a1 2a 2a 20 45 78 70 72 20 6e 6f 64 65 2e 20 20 4f ** Expr node. O
205a2 72 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 65 72 72 r, if an OOM err
205a3 6f 72 20 6f 63 63 75 72 73 2c 20 73 65 74 20 70 or occurs, set p
205a4 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f Parse->db->mallo
205a5 63 46 61 69 6c 65 64 2c 0a 2a 2a 20 66 72 65 65 cFailed,.** free
205a6 20 74 68 65 20 73 75 62 74 72 65 65 73 20 61 6e the subtrees an
205a7 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a d return NULL..*
205a8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
205a9 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 50 45 Expr *sqlite3PE
205aa 78 70 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 xpr(. Parse *pP
205ab 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f arse, /
205ac 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
205ad 74 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 t */. int op,
205ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
205af 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 * Expression opc
205b0 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ode */. Expr *p
205b1 4c 65 66 74 2c 20 20 20 20 20 20 20 20 20 20 20 Left,
205b2 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 /* Left operand
205b3 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 */. Expr *pRig
205b4 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ht, /*
205b5 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a Right operand *
205b6 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 /. const Token
205b7 2a 70 54 6f 6b 65 6e 20 20 20 20 20 2f 2a 20 41 *pToken /* A
205b8 72 67 75 6d 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f rgument token */
205b9 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 .){. Expr *p =
205ba 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 sqlite3ExprAlloc
205bb 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 6f 70 2c (pParse->db, op,
205bc 20 70 54 6f 6b 65 6e 2c 20 31 29 3b 0a 20 20 73 pToken, 1);. s
205bd 71 6c 69 74 65 33 45 78 70 72 41 74 74 61 63 68 qlite3ExprAttach
205be 53 75 62 74 72 65 65 73 28 70 50 61 72 73 65 2d Subtrees(pParse-
205bf 3e 64 62 2c 20 70 2c 20 70 4c 65 66 74 2c 20 70 >db, p, pLeft, p
205c0 52 69 67 68 74 29 3b 0a 20 20 72 65 74 75 72 6e Right);. return
205c1 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 69 p;.}../*.** Joi
205c2 6e 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e n two expression
205c3 73 20 75 73 69 6e 67 20 61 6e 20 41 4e 44 20 6f s using an AND o
205c4 70 65 72 61 74 6f 72 2e 20 20 49 66 20 65 69 74 perator. If eit
205c5 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 her expression i
205c6 73 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 s.** NULL, then
205c7 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20 just return the
205c8 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e other expression
205c9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
205ca 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
205cb 33 45 78 70 72 41 6e 64 28 73 71 6c 69 74 65 33 3ExprAnd(sqlite3
205cc 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 4c 65 66 *db, Expr *pLef
205cd 74 2c 20 45 78 70 72 20 2a 70 52 69 67 68 74 29 t, Expr *pRight)
205ce 7b 0a 20 20 69 66 28 20 70 4c 65 66 74 3d 3d 30 {. if( pLeft==0
205cf 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 ){. return p
205d0 52 69 67 68 74 3b 0a 20 20 7d 65 6c 73 65 20 69 Right;. }else i
205d1 66 28 20 70 52 69 67 68 74 3d 3d 30 20 29 7b 0a f( pRight==0 ){.
205d2 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65 66 74 return pLeft
205d3 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 45 ;. }else{. E
205d4 78 70 72 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 xpr *pNew = sqli
205d5 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c te3ExprAlloc(db,
205d6 20 54 4b 5f 41 4e 44 2c 20 30 2c 20 30 29 3b 0a TK_AND, 0, 0);.
205d7 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 sqlite3ExprA
205d8 74 74 61 63 68 53 75 62 74 72 65 65 73 28 64 62 ttachSubtrees(db
205d9 2c 20 70 4e 65 77 2c 20 70 4c 65 66 74 2c 20 70 , pNew, pLeft, p
205da 52 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74 75 Right);. retu
205db 72 6e 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d 0a 0a rn pNew;. }.}..
205dc 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 /*.** Construct
205dd 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e a new expression
205de 20 6e 6f 64 65 20 66 6f 72 20 61 20 66 75 6e 63 node for a func
205df 74 69 6f 6e 20 77 69 74 68 20 6d 75 6c 74 69 70 tion with multip
205e0 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 2e le.** arguments.
205e1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
205e2 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
205e3 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72 ExprFunction(Par
205e4 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 se *pParse, Expr
205e5 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b List *pList, Tok
205e6 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 45 en *pToken){. E
205e7 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 73 71 6c xpr *pNew;. sql
205e8 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
205e9 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 e->db;. assert(
205ea 20 70 54 6f 6b 65 6e 20 29 3b 0a 20 20 70 4e 65 pToken );. pNe
205eb 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 w = sqlite3ExprA
205ec 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 46 55 4e 43 lloc(db, TK_FUNC
205ed 54 49 4f 4e 2c 20 70 54 6f 6b 65 6e 2c 20 31 29 TION, pToken, 1)
205ee 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 ;. if( pNew==0
205ef 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 ){. sqlite3Ex
205f0 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c prListDelete(db,
205f1 20 70 4c 69 73 74 29 3b 20 2f 2a 20 41 76 6f 69 pList); /* Avoi
205f2 64 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 77 68 d memory leak wh
205f3 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 en malloc fails
205f4 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b */. return 0;
205f5 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 78 2e 70 . }. pNew->x.p
205f6 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20 List = pList;.
205f7 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 assert( !ExprHas
205f8 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45 Property(pNew, E
205f9 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a P_xIsSelect) );.
205fa 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 sqlite3ExprSet
205fb 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 Height(pParse, p
205fc 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 New);. return p
205fd 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 New;.}../*.** As
205fe 73 69 67 6e 20 61 20 76 61 72 69 61 62 6c 65 20 sign a variable
205ff 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 65 78 70 number to an exp
20600 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65 6e 63 ression that enc
20601 6f 64 65 73 20 61 20 77 69 6c 64 63 61 72 64 0a odes a wildcard.
20602 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e ** in the origin
20603 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 al SQL statement
20604 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 . .**.** Wildca
20605 72 64 73 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f rds consisting o
20606 66 20 61 20 73 69 6e 67 6c 65 20 22 3f 22 20 61 f a single "?" a
20607 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20 re assigned the
20608 6e 65 78 74 20 73 65 71 75 65 6e 74 69 61 6c 0a next sequential.
20609 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 ** variable numb
2060a 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 er..**.** Wildca
2060b 72 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 rds of the form
2060c 22 3f 6e 6e 6e 22 20 61 72 65 20 61 73 73 69 67 "?nnn" are assig
2060d 6e 65 64 20 74 68 65 20 6e 75 6d 62 65 72 20 22 ned the number "
2060e 6e 6e 6e 22 2e 20 20 57 65 20 6d 61 6b 65 0a 2a nnn". We make.*
2060f 2a 20 73 75 72 65 20 22 6e 6e 6e 22 20 69 73 20 * sure "nnn" is
20610 6e 6f 74 20 74 6f 6f 20 62 65 20 74 6f 20 61 76 not too be to av
20611 6f 69 64 20 61 20 64 65 6e 69 61 6c 20 6f 66 20 oid a denial of
20612 73 65 72 76 69 63 65 20 61 74 74 61 63 6b 20 77 service attack w
20613 68 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 20 73 hen.** the SQL s
20614 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20 66 tatement comes f
20615 72 6f 6d 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 rom an external
20616 73 6f 75 72 63 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 source..**.** Wi
20617 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 20 66 ldcards of the f
20618 6f 72 6d 20 22 3a 61 61 61 22 2c 20 22 40 61 61 orm ":aaa", "@aa
20619 61 22 2c 20 6f 72 20 22 24 61 61 61 22 20 61 72 a", or "$aaa" ar
2061a 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20 73 e assigned the s
2061b 61 6d 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 61 73 ame number.** as
2061c 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 69 6e the previous in
2061d 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 61 stance of the sa
2061e 6d 65 20 77 69 6c 64 63 61 72 64 2e 20 20 4f 72 me wildcard. Or
2061f 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 if this is the
20620 66 69 72 73 74 0a 2a 2a 20 69 6e 73 74 61 6e 63 first.** instanc
20621 65 20 6f 66 20 74 68 65 20 77 69 6c 64 63 61 72 e of the wildcar
20622 64 2c 20 74 68 65 20 6e 65 78 74 20 73 65 71 75 d, the next sequ
20623 65 6e 69 61 6c 20 76 61 72 69 61 62 6c 65 20 6e enial variable n
20624 75 6d 62 65 72 20 69 73 0a 2a 2a 20 61 73 73 69 umber is.** assi
20625 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f gned..*/.SQLITE_
20626 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
20627 69 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61 ite3ExprAssignVa
20628 72 4e 75 6d 62 65 72 28 50 61 72 73 65 20 2a 70 rNumber(Parse *p
20629 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 Parse, Expr *pEx
2062a 70 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a pr){. sqlite3 *
2062b 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
2062c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
2062d 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d ;.. if( pExpr==
2062e 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 0 ) return;. as
2062f 73 65 72 74 28 20 21 45 78 70 72 48 61 73 41 6e sert( !ExprHasAn
20630 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c yProperty(pExpr,
20631 20 45 50 5f 49 6e 74 56 61 6c 75 65 7c 45 50 5f EP_IntValue|EP_
20632 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e Reduced|EP_Token
20633 4f 6e 6c 79 29 20 29 3b 0a 20 20 7a 20 3d 20 70 Only) );. z = p
20634 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a Expr->u.zToken;.
20635 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20 29 assert( z!=0 )
20636 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 5b 30 5d ;. assert( z[0]
20637 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 7a 5b 31 !=0 );. if( z[1
20638 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 ]==0 ){. /* W
20639 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20 66 ildcard of the f
2063a 6f 72 6d 20 22 3f 22 2e 20 20 41 73 73 69 67 6e orm "?". Assign
2063b 20 74 68 65 20 6e 65 78 74 20 76 61 72 69 61 62 the next variab
2063c 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 le number */.
2063d 20 61 73 73 65 72 74 28 20 7a 5b 30 5d 3d 3d 27 assert( z[0]=='
2063e 3f 27 20 29 3b 0a 20 20 20 20 70 45 78 70 72 2d ?' );. pExpr-
2063f 3e 69 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72 >iTable = ++pPar
20640 73 65 2d 3e 6e 56 61 72 3b 0a 20 20 7d 65 6c 73 se->nVar;. }els
20641 65 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 3f 27 20 e if( z[0]=='?'
20642 29 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 ){. /* Wildca
20643 72 64 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 rd of the form "
20644 3f 6e 6e 6e 22 2e 20 20 43 6f 6e 76 65 72 74 20 ?nnn". Convert
20645 22 6e 6e 6e 22 20 74 6f 20 61 6e 20 69 6e 74 65 "nnn" to an inte
20646 67 65 72 20 61 6e 64 0a 20 20 20 20 2a 2a 20 75 ger and. ** u
20647 73 65 20 69 74 20 61 73 20 74 68 65 20 76 61 72 se it as the var
20648 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a iable number */.
20649 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 70 int i;. p
2064a 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 Expr->iTable = i
2064b 20 3d 20 61 74 6f 69 28 28 63 68 61 72 2a 29 26 = atoi((char*)&
2064c 7a 5b 31 5d 29 3b 0a 20 20 20 20 74 65 73 74 63 z[1]);. testc
2064d 61 73 65 28 20 69 3d 3d 30 20 29 3b 0a 20 20 20 ase( i==0 );.
2064e 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 20 testcase( i==1
2064f 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
20650 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 i==db->aLimit[S
20651 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 QLITE_LIMIT_VARI
20652 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 2d 31 20 29 ABLE_NUMBER]-1 )
20653 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
20654 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 i==db->aLimit[SQ
20655 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 LITE_LIMIT_VARIA
20656 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 3b 0a 20 BLE_NUMBER] );.
20657 20 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e if( i<1 || i>
20658 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
20659 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 E_LIMIT_VARIABLE
2065a 5f 4e 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20 _NUMBER] ){.
2065b 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
2065c 67 28 70 50 61 72 73 65 2c 20 22 76 61 72 69 61 g(pParse, "varia
2065d 62 6c 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 20 ble number must
2065e 62 65 20 62 65 74 77 65 65 6e 20 3f 31 20 61 6e be between ?1 an
2065f 64 20 3f 25 64 22 2c 0a 20 20 20 20 20 20 20 20 d ?%d",.
20660 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c db->aLimit[SQL
20661 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 ITE_LIMIT_VARIAB
20662 4c 45 5f 4e 55 4d 42 45 52 5d 29 3b 0a 20 20 20 LE_NUMBER]);.
20663 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 70 50 61 }. if( i>pPa
20664 72 73 65 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 rse->nVar ){.
20665 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 pParse->nVar
20666 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c = i;. }. }el
20667 73 65 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 se{. /* Wildc
20668 61 72 64 73 20 6c 69 6b 65 20 22 3a 61 61 61 22 ards like ":aaa"
20669 2c 20 22 24 61 61 61 22 20 6f 72 20 22 40 61 61 , "$aaa" or "@aa
2066a 61 22 2e 20 20 52 65 75 73 65 20 74 68 65 20 73 a". Reuse the s
2066b 61 6d 65 20 76 61 72 69 61 62 6c 65 0a 20 20 20 ame variable.
2066c 20 2a 2a 20 6e 75 6d 62 65 72 20 61 73 20 74 68 ** number as th
2066d 65 20 70 72 69 6f 72 20 61 70 70 65 61 72 61 6e e prior appearan
2066e 63 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e ce of the same n
2066f 61 6d 65 2c 20 6f 72 20 69 66 20 74 68 65 20 6e ame, or if the n
20670 61 6d 65 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e ame. ** has n
20671 65 76 65 72 20 61 70 70 65 61 72 65 64 20 62 65 ever appeared be
20672 66 6f 72 65 2c 20 72 65 75 73 65 20 74 68 65 20 fore, reuse the
20673 73 61 6d 65 20 76 61 72 69 61 62 6c 65 20 6e 75 same variable nu
20674 6d 62 65 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 mber. */.
20675 69 6e 74 20 69 3b 0a 20 20 20 20 75 33 32 20 6e int i;. u32 n
20676 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 ;. n = sqlite
20677 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a 20 20 3Strlen30(z);.
20678 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 for(i=0; i<pPa
20679 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3b 20 69 rse->nVarExpr; i
2067a 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 ++){. Expr
2067b 2a 70 45 20 3d 20 70 50 61 72 73 65 2d 3e 61 70 *pE = pParse->ap
2067c 56 61 72 45 78 70 72 5b 69 5d 3b 0a 20 20 20 20 VarExpr[i];.
2067d 20 20 61 73 73 65 72 74 28 20 70 45 21 3d 30 20 assert( pE!=0
2067e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d );. if( mem
2067f 63 6d 70 28 70 45 2d 3e 75 2e 7a 54 6f 6b 65 6e cmp(pE->u.zToken
20680 2c 20 7a 2c 20 6e 29 3d 3d 30 20 26 26 20 70 45 , z, n)==0 && pE
20681 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 6e 5d 3d 3d 30 ->u.zToken[n]==0
20682 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 ){. pExp
20683 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 2d 3e r->iTable = pE->
20684 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 iTable;.
20685 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
20686 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 3d }. if( i>=
20687 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 pParse->nVarExpr
20688 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d ){. pExpr-
20689 3e 69 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72 >iTable = ++pPar
2068a 73 65 2d 3e 6e 56 61 72 3b 0a 20 20 20 20 20 20 se->nVar;.
2068b 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 if( pParse->nVar
2068c 45 78 70 72 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 Expr>=pParse->nV
2068d 61 72 45 78 70 72 41 6c 6c 6f 63 2d 31 20 29 7b arExprAlloc-1 ){
2068e 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d . pParse-
2068f 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b >nVarExprAlloc +
20690 3d 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 = pParse->nVarEx
20691 70 72 41 6c 6c 6f 63 20 2b 20 31 30 3b 0a 20 20 prAlloc + 10;.
20692 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 pParse->ap
20693 56 61 72 45 78 70 72 20 3d 0a 20 20 20 20 20 20 VarExpr =.
20694 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 52 sqlite3DbR
20695 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 0a 20 20 eallocOrFree(.
20696 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2c 0a db,.
20697 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 pP
20698 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 2c arse->apVarExpr,
20699 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 . p
2069a 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 Parse->nVarExprA
2069b 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 50 61 72 lloc*sizeof(pPar
2069c 73 65 2d 3e 61 70 56 61 72 45 78 70 72 5b 30 5d se->apVarExpr[0]
2069d 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 29 3b ). );
2069e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
2069f 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 f( !db->mallocFa
206a0 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 iled ){.
206a1 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e assert( pParse->
206a2 61 70 56 61 72 45 78 70 72 21 3d 30 20 29 3b 0a apVarExpr!=0 );.
206a3 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e pParse->
206a4 61 70 56 61 72 45 78 70 72 5b 70 50 61 72 73 65 apVarExpr[pParse
206a5 2d 3e 6e 56 61 72 45 78 70 72 2b 2b 5d 20 3d 20 ->nVarExpr++] =
206a6 70 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 0a 20 pExpr;. }.
206a7 20 20 20 7d 0a 20 20 7d 20 0a 20 20 69 66 28 20 }. } . if(
206a8 21 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 26 26 !pParse->nErr &&
206a9 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3e 64 62 pParse->nVar>db
206aa 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
206ab 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e LIMIT_VARIABLE_N
206ac 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20 73 71 UMBER] ){. sq
206ad 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
206ae 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 arse, "too many
206af 53 51 4c 20 76 61 72 69 61 62 6c 65 73 22 29 3b SQL variables");
206b0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c . }.}../*.** Cl
206b1 65 61 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f ear an expressio
206b2 6e 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68 n structure with
206b3 6f 75 74 20 64 65 6c 65 74 69 6e 67 20 74 68 65 out deleting the
206b4 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c structure itsel
206b5 66 2e 0a 2a 2a 20 53 75 62 73 74 72 75 63 74 75 f..** Substructu
206b6 72 65 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a re is deleted..*
206b7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
206b8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
206b9 72 43 6c 65 61 72 28 73 71 6c 69 74 65 33 20 2a rClear(sqlite3 *
206ba 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 db, Expr *p){.
206bb 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a assert( p!=0 );.
206bc 20 20 69 66 28 20 21 45 78 70 72 48 61 73 41 6e if( !ExprHasAn
206bd 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f yProperty(p, EP_
206be 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 TokenOnly) ){.
206bf 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c sqlite3ExprDel
206c0 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 ete(db, p->pLeft
206c1 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 );. sqlite3Ex
206c2 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e prDelete(db, p->
206c3 70 52 69 67 68 74 29 3b 0a 20 20 20 20 69 66 28 pRight);. if(
206c4 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 !ExprHasPropert
206c5 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64 29 y(p, EP_Reduced)
206c6 20 26 26 20 28 70 2d 3e 66 6c 61 67 73 32 20 26 && (p->flags2 &
206c7 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b EP2_MallocedTok
206c8 65 6e 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 en)!=0 ){.
206c9 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
206ca 2c 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a , p->u.zToken);.
206cb 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 45 78 }. if( Ex
206cc 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c prHasProperty(p,
206cd 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 EP_xIsSelect) )
206ce 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 {. sqlite3S
206cf 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 electDelete(db,
206d0 70 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 p->x.pSelect);.
206d1 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
206d2 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 sqlite3ExprListD
206d3 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 78 2e 70 elete(db, p->x.p
206d4 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d List);. }. }
206d5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 75 72 73 .}../*.** Recurs
206d6 69 76 65 6c 79 20 64 65 6c 65 74 65 20 61 6e 20 ively delete an
206d7 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e expression tree.
206d8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
206d9 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
206da 78 70 72 44 65 6c 65 74 65 28 73 71 6c 69 74 65 xprDelete(sqlite
206db 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 3 *db, Expr *p){
206dc 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
206dd 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 45 turn;. sqlite3E
206de 78 70 72 43 6c 65 61 72 28 64 62 2c 20 70 29 3b xprClear(db, p);
206df 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73 50 . if( !ExprHasP
206e0 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 53 74 roperty(p, EP_St
206e1 61 74 69 63 29 20 29 7b 0a 20 20 20 20 73 71 6c atic) ){. sql
206e2 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
206e3 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
206e4 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
206e5 72 20 6f 66 20 62 79 74 65 73 20 61 6c 6c 6f 63 r of bytes alloc
206e6 61 74 65 64 20 66 6f 72 20 74 68 65 20 65 78 70 ated for the exp
206e7 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75 72 ression structur
206e8 65 20 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20 e .** passed as
206e9 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
206ea 6e 74 2e 20 54 68 69 73 20 69 73 20 61 6c 77 61 nt. This is alwa
206eb 79 73 20 6f 6e 65 20 6f 66 20 45 58 50 52 5f 46 ys one of EXPR_F
206ec 55 4c 4c 53 49 5a 45 2c 0a 2a 2a 20 45 58 50 52 ULLSIZE,.** EXPR
206ed 5f 52 45 44 55 43 45 44 53 49 5a 45 20 6f 72 20 _REDUCEDSIZE or
206ee 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 EXPR_TOKENONLYSI
206ef 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ZE..*/.static in
206f0 74 20 65 78 70 72 53 74 72 75 63 74 53 69 7a 65 t exprStructSize
206f1 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 (Expr *p){. if(
206f2 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
206f3 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 (p, EP_TokenOnly
206f4 29 20 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f ) ) return EXPR_
206f5 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20 TOKENONLYSIZE;.
206f6 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 if( ExprHasProp
206f7 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64 75 63 erty(p, EP_Reduc
206f8 65 64 29 20 29 20 72 65 74 75 72 6e 20 45 58 50 ed) ) return EXP
206f9 52 5f 52 45 44 55 43 45 44 53 49 5a 45 3b 0a 20 R_REDUCEDSIZE;.
206fa 20 72 65 74 75 72 6e 20 45 58 50 52 5f 46 55 4c return EXPR_FUL
206fb 4c 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 LSIZE;.}../*.**
206fc 54 68 65 20 64 75 70 65 64 45 78 70 72 2a 53 69 The dupedExpr*Si
206fd 7a 65 28 29 20 72 6f 75 74 69 6e 65 73 20 65 61 ze() routines ea
206fe 63 68 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 ch return the nu
206ff 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 mber of bytes re
20700 71 75 69 72 65 64 0a 2a 2a 20 74 6f 20 73 74 6f quired.** to sto
20701 72 65 20 61 20 63 6f 70 79 20 6f 66 20 61 6e 20 re a copy of an
20702 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 65 78 expression or ex
20703 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 pression tree.
20704 54 68 65 79 20 64 69 66 66 65 72 20 69 6e 0a 2a They differ in.*
20705 2a 20 68 6f 77 20 6d 75 63 68 20 6f 66 20 74 68 * how much of th
20706 65 20 74 72 65 65 20 69 73 20 6d 65 61 73 75 72 e tree is measur
20707 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 64 75 ed..**.** du
20708 70 65 64 45 78 70 72 53 74 72 75 63 74 53 69 7a pedExprStructSiz
20709 65 28 29 20 20 20 20 20 53 69 7a 65 20 6f 66 20 e() Size of
2070a 6f 6e 6c 79 20 74 68 65 20 45 78 70 72 20 73 74 only the Expr st
2070b 72 75 63 74 75 72 65 20 0a 2a 2a 20 20 20 20 20 ructure .**
2070c 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a dupedExprNodeSiz
2070d 65 28 29 20 20 20 20 20 20 20 53 69 7a 65 20 6f e() Size o
2070e 66 20 45 78 70 72 20 2b 20 73 70 61 63 65 20 66 f Expr + space f
2070f 6f 72 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 or token.**
20710 64 75 70 65 64 45 78 70 72 53 69 7a 65 28 29 20 dupedExprSize()
20711 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2b Expr +
20712 20 74 6f 6b 65 6e 20 2b 20 73 75 62 74 72 65 65 token + subtree
20713 20 63 6f 6d 70 6f 6e 65 6e 74 73 0a 2a 2a 0a 2a components.**.*
20714 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20715 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20716 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20717 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20718 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
20719 20 54 68 65 20 64 75 70 65 64 45 78 70 72 53 74 The dupedExprSt
2071a 72 75 63 74 53 69 7a 65 28 29 20 66 75 6e 63 74 ructSize() funct
2071b 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 77 6f 20 ion returns two
2071c 76 61 6c 75 65 73 20 4f 52 2d 65 64 20 74 6f 67 values OR-ed tog
2071d 65 74 68 65 72 3a 20 20 0a 2a 2a 20 28 31 29 20 ether: .** (1)
2071e 74 68 65 20 73 70 61 63 65 20 72 65 71 75 69 72 the space requir
2071f 65 64 20 66 6f 72 20 61 20 63 6f 70 79 20 6f 66 ed for a copy of
20720 20 74 68 65 20 45 78 70 72 20 73 74 72 75 63 74 the Expr struct
20721 75 72 65 20 6f 6e 6c 79 20 61 6e 64 20 0a 2a 2a ure only and .**
20722 20 28 32 29 20 74 68 65 20 45 50 5f 78 78 78 20 (2) the EP_xxx
20723 66 6c 61 67 73 20 74 68 61 74 20 69 6e 64 69 63 flags that indic
20724 61 74 65 20 77 68 61 74 20 74 68 65 20 73 74 72 ate what the str
20725 75 63 74 75 72 65 20 73 69 7a 65 20 73 68 6f 75 ucture size shou
20726 6c 64 20 62 65 2e 0a 2a 2a 20 54 68 65 20 72 65 ld be..** The re
20727 74 75 72 6e 20 76 61 6c 75 65 73 20 69 73 20 61 turn values is a
20728 6c 77 61 79 73 20 6f 6e 65 20 6f 66 3a 0a 2a 2a lways one of:.**
20729 0a 2a 2a 20 20 20 20 20 20 45 58 50 52 5f 46 55 .** EXPR_FU
2072a 4c 4c 53 49 5a 45 0a 2a 2a 20 20 20 20 20 20 45 LLSIZE.** E
2072b 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 XPR_REDUCEDSIZE
2072c 20 20 7c 20 45 50 5f 52 65 64 75 63 65 64 0a 2a | EP_Reduced.*
2072d 2a 20 20 20 20 20 20 45 58 50 52 5f 54 4f 4b 45 * EXPR_TOKE
2072e 4e 4f 4e 4c 59 53 49 5a 45 20 7c 20 45 50 5f 54 NONLYSIZE | EP_T
2072f 6f 6b 65 6e 4f 6e 6c 79 0a 2a 2a 0a 2a 2a 20 54 okenOnly.**.** T
20730 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 73 he size of the s
20731 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 tructure can be
20732 66 6f 75 6e 64 20 62 79 20 6d 61 73 6b 69 6e 67 found by masking
20733 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 the return valu
20734 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 72 6f 75 e.** of this rou
20735 74 69 6e 65 20 77 69 74 68 20 30 78 66 66 66 2e tine with 0xfff.
20736 20 20 54 68 65 20 66 6c 61 67 73 20 63 61 6e 20 The flags can
20737 62 65 20 66 6f 75 6e 64 20 62 79 20 6d 61 73 6b be found by mask
20738 69 6e 67 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 ing the.** retur
20739 6e 20 76 61 6c 75 65 20 77 69 74 68 20 45 50 5f n value with EP_
2073a 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e Reduced|EP_Token
2073b 4f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 Only..**.** Note
2073c 20 74 68 61 74 20 77 69 74 68 20 66 6c 61 67 73 that with flags
2073d 3d 3d 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 ==EXPRDUP_REDUCE
2073e 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 73 20 , this routines
2073f 77 6f 72 6b 73 20 6f 6e 20 66 75 6c 6c 2d 73 69 works on full-si
20740 7a 65 0a 2a 2a 20 28 75 6e 72 65 64 75 63 65 64 ze.** (unreduced
20741 29 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 61 ) Expr objects a
20742 73 20 74 68 65 79 20 6f 72 20 6f 72 69 67 69 6e s they or origin
20743 61 6c 6c 79 20 63 6f 6e 73 74 72 75 63 74 65 64 ally constructed
20744 20 62 79 20 74 68 65 20 70 61 72 73 65 72 2e 0a by the parser..
20745 2a 2a 20 44 75 72 69 6e 67 20 65 78 70 72 65 73 ** During expres
20746 73 69 6f 6e 20 61 6e 61 6c 79 73 69 73 2c 20 65 sion analysis, e
20747 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e xtra information
20748 20 69 73 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 is computed and
20749 20 6d 6f 76 65 64 20 69 6e 74 6f 0a 2a 2a 20 6c moved into.** l
2074a 61 74 65 72 20 70 61 72 74 73 20 6f 66 20 74 65 ater parts of te
2074b 68 20 45 78 70 72 20 6f 62 6a 65 63 74 20 61 6e h Expr object an
2074c 64 20 74 68 61 74 20 65 78 74 72 61 20 69 6e 66 d that extra inf
2074d 6f 72 6d 61 74 69 6f 6e 20 6d 69 67 68 74 20 67 ormation might g
2074e 65 74 20 63 68 6f 70 70 65 64 0a 2a 2a 20 6f 66 et chopped.** of
2074f 66 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 f if the express
20750 69 6f 6e 20 69 73 20 72 65 64 75 63 65 64 2e 20 ion is reduced.
20751 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 Note also that
20752 69 74 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b it does not work
20753 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 61 20 45 58 to.** make a EX
20754 50 52 44 55 50 5f 52 45 44 55 43 45 20 63 6f 70 PRDUP_REDUCE cop
20755 79 20 6f 66 20 61 20 72 65 64 75 63 65 64 20 65 y of a reduced e
20756 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 74 20 69 xpression. It i
20757 73 20 6f 6e 6c 79 20 6c 65 67 61 6c 0a 2a 2a 20 s only legal.**
20758 74 6f 20 72 65 64 75 63 65 20 61 20 70 72 69 73 to reduce a pris
20759 74 69 6e 65 20 65 78 70 72 65 73 73 69 6f 6e 20 tine expression
2075a 74 72 65 65 20 66 72 6f 6d 20 74 68 65 20 70 61 tree from the pa
2075b 72 73 65 72 2e 20 20 54 68 65 20 69 6d 70 6c 65 rser. The imple
2075c 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 mentation.** of
2075d 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74 53 dupedExprStructS
2075e 69 7a 65 28 29 20 63 6f 6e 74 61 69 6e 20 6d 75 ize() contain mu
2075f 6c 74 69 70 6c 65 20 61 73 73 65 72 74 28 29 20 ltiple assert()
20760 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 statements that
20761 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e attempt.** to en
20762 66 6f 72 63 65 20 74 68 69 73 20 63 6f 6e 73 74 force this const
20763 72 61 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 raint..*/.static
20764 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 53 74 int dupedExprSt
20765 72 75 63 74 53 69 7a 65 28 45 78 70 72 20 2a 70 ructSize(Expr *p
20766 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 , int flags){.
20767 69 6e 74 20 6e 53 69 7a 65 3b 0a 20 20 61 73 73 int nSize;. ass
20768 65 72 74 28 20 66 6c 61 67 73 3d 3d 45 58 50 52 ert( flags==EXPR
20769 44 55 50 5f 52 45 44 55 43 45 20 7c 7c 20 66 6c DUP_REDUCE || fl
2076a 61 67 73 3d 3d 30 20 29 3b 20 2f 2a 20 4f 6e 6c ags==0 ); /* Onl
2076b 79 20 6f 6e 65 20 66 6c 61 67 20 76 61 6c 75 65 y one flag value
2076c 20 61 6c 6c 6f 77 65 64 20 2a 2f 0a 20 20 69 66 allowed */. if
2076d 28 20 30 3d 3d 28 66 6c 61 67 73 26 45 58 50 52 ( 0==(flags&EXPR
2076e 44 55 50 5f 52 45 44 55 43 45 29 20 29 7b 0a 20 DUP_REDUCE) ){.
2076f 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f nSize = EXPR_
20770 46 55 4c 4c 53 49 5a 45 3b 0a 20 20 7d 65 6c 73 FULLSIZE;. }els
20771 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 e{. assert( !
20772 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 ExprHasAnyProper
20773 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e ty(p, EP_TokenOn
20774 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29 ly|EP_Reduced) )
20775 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 45 ;. assert( !E
20776 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
20777 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 , EP_FromJoin) )
20778 3b 20 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 ; . assert( (
20779 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f p->flags2 & EP2_
2077a 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d MallocedToken)==
2077b 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
2077c 20 28 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 (p->flags2 & EP
2077d 32 5f 49 72 72 65 64 75 63 69 62 6c 65 29 3d 3d 2_Irreducible)==
2077e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 0 );. if( p->
2077f 70 4c 65 66 74 20 7c 7c 20 70 2d 3e 70 52 69 67 pLeft || p->pRig
20780 68 74 20 7c 7c 20 70 2d 3e 70 43 6f 6c 6c 20 7c ht || p->pColl |
20781 7c 20 70 2d 3e 78 2e 70 4c 69 73 74 20 29 7b 0a | p->x.pList ){.
20782 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 nSize = EX
20783 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 7c PR_REDUCEDSIZE |
20784 20 45 50 5f 52 65 64 75 63 65 64 3b 0a 20 20 20 EP_Reduced;.
20785 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 53 }else{. nS
20786 69 7a 65 20 3d 20 45 58 50 52 5f 54 4f 4b 45 4e ize = EXPR_TOKEN
20787 4f 4e 4c 59 53 49 5a 45 20 7c 20 45 50 5f 54 6f ONLYSIZE | EP_To
20788 6b 65 6e 4f 6e 6c 79 3b 0a 20 20 20 20 7d 0a 20 kenOnly;. }.
20789 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 53 69 7a }. return nSiz
2078a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 e;.}../*.** This
2078b 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
2078c 73 20 74 68 65 20 73 70 61 63 65 20 69 6e 20 62 s the space in b
2078d 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f ytes required to
2078e 20 73 74 6f 72 65 20 74 68 65 20 63 6f 70 79 20 store the copy
2078f 0a 2a 2a 20 6f 66 20 74 68 65 20 45 78 70 72 20 .** of the Expr
20790 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 61 20 structure and a
20791 63 6f 70 79 20 6f 66 20 74 68 65 20 45 78 70 72 copy of the Expr
20792 2e 75 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 .u.zToken string
20793 20 28 69 66 20 74 68 61 74 0a 2a 2a 20 73 74 72 (if that.** str
20794 69 6e 67 20 69 73 20 64 65 66 69 6e 65 64 2e 29 ing is defined.)
20795 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 .*/.static int d
20796 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 upedExprNodeSize
20797 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c (Expr *p, int fl
20798 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 ags){. int nByt
20799 65 20 3d 20 64 75 70 65 64 45 78 70 72 53 74 72 e = dupedExprStr
2079a 75 63 74 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 uctSize(p, flags
2079b 29 20 26 20 30 78 66 66 66 3b 0a 20 20 69 66 28 ) & 0xfff;. if(
2079c 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 !ExprHasPropert
2079d 79 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 y(p, EP_IntValue
2079e 29 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e ) && p->u.zToken
2079f 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20 2b 3d ){. nByte +=
207a0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
207a1 28 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2b 31 3b (p->u.zToken)+1;
207a2 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 52 4f . }. return RO
207a3 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 7d 0a 0a UND8(nByte);.}..
207a4 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
207a5 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
207a6 20 72 65 71 75 69 72 65 64 20 74 6f 20 63 72 65 required to cre
207a7 61 74 65 20 61 20 64 75 70 6c 69 63 61 74 65 20 ate a duplicate
207a8 6f 66 20 74 68 65 20 0a 2a 2a 20 65 78 70 72 65 of the .** expre
207a9 73 73 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20 ssion passed as
207aa 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
207ab 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61 nt. The second a
207ac 72 67 75 6d 65 6e 74 20 69 73 20 61 0a 2a 2a 20 rgument is a.**
207ad 6d 61 73 6b 20 63 6f 6e 74 61 69 6e 69 6e 67 20 mask containing
207ae 45 58 50 52 44 55 50 5f 58 58 58 20 66 6c 61 67 EXPRDUP_XXX flag
207af 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c s..**.** The val
207b0 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c ue returned incl
207b1 75 64 65 73 20 73 70 61 63 65 20 74 6f 20 63 72 udes space to cr
207b2 65 61 74 65 20 61 20 63 6f 70 79 20 6f 66 20 74 eate a copy of t
207b3 68 65 20 45 78 70 72 20 73 74 72 75 63 74 0a 2a he Expr struct.*
207b4 2a 20 69 74 73 65 6c 66 20 61 6e 64 20 74 68 65 * itself and the
207b5 20 62 75 66 66 65 72 20 72 65 66 65 72 72 65 64 buffer referred
207b6 20 74 6f 20 62 79 20 45 78 70 72 2e 75 2e 7a 54 to by Expr.u.zT
207b7 6f 6b 65 6e 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a oken, if any..**
207b8 0a 2a 2a 20 49 66 20 74 68 65 20 45 58 50 52 44 .** If the EXPRD
207b9 55 50 5f 52 45 44 55 43 45 20 66 6c 61 67 20 69 UP_REDUCE flag i
207ba 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 s set, then the
207bb 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 63 return value inc
207bc 6c 75 64 65 73 20 0a 2a 2a 20 73 70 61 63 65 20 ludes .** space
207bd 74 6f 20 64 75 70 6c 69 63 61 74 65 20 61 6c 6c to duplicate all
207be 20 45 78 70 72 20 6e 6f 64 65 73 20 69 6e 20 74 Expr nodes in t
207bf 68 65 20 74 72 65 65 20 66 6f 72 6d 65 64 20 62 he tree formed b
207c0 79 20 45 78 70 72 2e 70 4c 65 66 74 20 0a 2a 2a y Expr.pLeft .**
207c1 20 61 6e 64 20 45 78 70 72 2e 70 52 69 67 68 74 and Expr.pRight
207c2 20 76 61 72 69 61 62 6c 65 73 20 28 62 75 74 20 variables (but
207c3 6e 6f 74 20 66 6f 72 20 61 6e 79 20 73 74 72 75 not for any stru
207c4 63 74 75 72 65 73 20 70 6f 69 6e 74 65 64 20 74 ctures pointed t
207c5 6f 20 6f 72 20 0a 2a 2a 20 64 65 73 63 65 6e 64 o or .** descend
207c6 65 64 20 66 72 6f 6d 20 74 68 65 20 45 78 70 72 ed from the Expr
207c7 2e 78 2e 70 4c 69 73 74 20 6f 72 20 45 78 70 72 .x.pList or Expr
207c8 2e 78 2e 70 53 65 6c 65 63 74 20 76 61 72 69 61 .x.pSelect varia
207c9 62 6c 65 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 bles)..*/.static
207ca 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 53 69 int dupedExprSi
207cb 7a 65 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 ze(Expr *p, int
207cc 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 flags){. int nB
207cd 79 74 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 yte = 0;. if( p
207ce 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 ){. nByte =
207cf 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a dupedExprNodeSiz
207d0 65 28 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 e(p, flags);.
207d1 20 69 66 28 20 66 6c 61 67 73 26 45 58 50 52 44 if( flags&EXPRD
207d2 55 50 5f 52 45 44 55 43 45 20 29 7b 0a 20 20 20 UP_REDUCE ){.
207d3 20 20 20 6e 42 79 74 65 20 2b 3d 20 64 75 70 65 nByte += dupe
207d4 64 45 78 70 72 53 69 7a 65 28 70 2d 3e 70 4c 65 dExprSize(p->pLe
207d5 66 74 2c 20 66 6c 61 67 73 29 20 2b 20 64 75 70 ft, flags) + dup
207d6 65 64 45 78 70 72 53 69 7a 65 28 70 2d 3e 70 52 edExprSize(p->pR
207d7 69 67 68 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 ight, flags);.
207d8 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
207d9 20 6e 42 79 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a nByte;.}../*.**
207da 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
207db 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 71 6c s similar to sql
207dc 69 74 65 33 45 78 70 72 44 75 70 28 29 2c 20 65 ite3ExprDup(), e
207dd 78 63 65 70 74 20 74 68 61 74 20 69 66 20 70 7a xcept that if pz
207de 42 75 66 66 65 72 20 0a 2a 2a 20 69 73 20 6e 6f Buffer .** is no
207df 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a 70 7a 42 t NULL then *pzB
207e0 75 66 66 65 72 20 69 73 20 61 73 73 75 6d 65 64 uffer is assumed
207e1 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 to point to a b
207e2 75 66 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 uffer large enou
207e3 67 68 20 0a 2a 2a 20 74 6f 20 73 74 6f 72 65 20 gh .** to store
207e4 74 68 65 20 63 6f 70 79 20 6f 66 20 65 78 70 72 the copy of expr
207e5 65 73 73 69 6f 6e 20 70 2c 20 74 68 65 20 63 6f ession p, the co
207e6 70 69 65 73 20 6f 66 20 70 2d 3e 75 2e 7a 54 6f pies of p->u.zTo
207e7 6b 65 6e 0a 2a 2a 20 28 69 66 20 61 70 70 6c 69 ken.** (if appli
207e8 63 61 62 6c 65 29 2c 20 61 6e 64 20 74 68 65 20 cable), and the
207e9 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 70 2d copies of the p-
207ea 3e 70 4c 65 66 74 20 61 6e 64 20 70 2d 3e 70 52 >pLeft and p->pR
207eb 69 67 68 74 20 65 78 70 72 65 73 73 69 6f 6e 73 ight expressions
207ec 2c 0a 2a 2a 20 69 66 20 61 6e 79 2e 20 42 65 66 ,.** if any. Bef
207ed 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a ore returning, *
207ee 70 7a 42 75 66 66 65 72 20 69 73 20 73 65 74 20 pzBuffer is set
207ef 74 6f 20 74 68 65 20 66 69 72 73 74 20 62 79 74 to the first byt
207f0 65 20 70 61 73 73 65 64 20 74 68 65 0a 2a 2a 20 e passed the.**
207f1 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 62 portion of the b
207f2 75 66 66 65 72 20 63 6f 70 69 65 64 20 69 6e 74 uffer copied int
207f3 6f 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 o by this functi
207f4 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 45 78 on..*/.static Ex
207f5 70 72 20 2a 65 78 70 72 44 75 70 28 73 71 6c 69 pr *exprDup(sqli
207f6 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 te3 *db, Expr *p
207f7 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 75 38 20 , int flags, u8
207f8 2a 2a 70 7a 42 75 66 66 65 72 29 7b 0a 20 20 45 **pzBuffer){. E
207f9 78 70 72 20 2a 70 4e 65 77 20 3d 20 30 3b 20 20 xpr *pNew = 0;
207fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
207fb 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 /* Value to
207fc 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 66 28 20 return */. if(
207fd 70 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 p ){. const i
207fe 6e 74 20 69 73 52 65 64 75 63 65 64 20 3d 20 28 nt isReduced = (
207ff 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 52 45 flags&EXPRDUP_RE
20800 44 55 43 45 29 3b 0a 20 20 20 20 75 38 20 2a 7a DUCE);. u8 *z
20801 41 6c 6c 6f 63 3b 0a 20 20 20 20 75 33 32 20 73 Alloc;. u32 s
20802 74 61 74 69 63 46 6c 61 67 20 3d 20 30 3b 0a 0a taticFlag = 0;..
20803 20 20 20 20 61 73 73 65 72 74 28 20 70 7a 42 75 assert( pzBu
20804 66 66 65 72 3d 3d 30 20 7c 7c 20 69 73 52 65 64 ffer==0 || isRed
20805 75 63 65 64 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 uced );.. /*
20806 46 69 67 75 72 65 20 6f 75 74 20 77 68 65 72 65 Figure out where
20807 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 6e 65 to write the ne
20808 77 20 45 78 70 72 20 73 74 72 75 63 74 75 72 65 w Expr structure
20809 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 7a 42 . */. if( pzB
2080a 75 66 66 65 72 20 29 7b 0a 20 20 20 20 20 20 7a uffer ){. z
2080b 41 6c 6c 6f 63 20 3d 20 2a 70 7a 42 75 66 66 65 Alloc = *pzBuffe
2080c 72 3b 0a 20 20 20 20 20 20 73 74 61 74 69 63 46 r;. staticF
2080d 6c 61 67 20 3d 20 45 50 5f 53 74 61 74 69 63 3b lag = EP_Static;
2080e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
2080f 20 20 7a 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 zAlloc = sqlit
20810 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
20811 2c 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 28 , dupedExprSize(
20812 70 2c 20 66 6c 61 67 73 29 29 3b 0a 20 20 20 20 p, flags));.
20813 7d 0a 20 20 20 20 70 4e 65 77 20 3d 20 28 45 78 }. pNew = (Ex
20814 70 72 20 2a 29 7a 41 6c 6c 6f 63 3b 0a 0a 20 20 pr *)zAlloc;..
20815 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 if( pNew ){.
20816 20 20 20 20 2f 2a 20 53 65 74 20 6e 4e 65 77 53 /* Set nNewS
20817 69 7a 65 20 74 6f 20 74 68 65 20 73 69 7a 65 20 ize to the size
20818 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68 allocated for th
20819 65 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e e structure poin
2081a 74 65 64 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 ted to. **
2081b 62 79 20 70 4e 65 77 2e 20 54 68 69 73 20 69 73 by pNew. This is
2081c 20 65 69 74 68 65 72 20 45 58 50 52 5f 46 55 4c either EXPR_FUL
2081d 4c 53 49 5a 45 2c 20 45 58 50 52 5f 52 45 44 55 LSIZE, EXPR_REDU
2081e 43 45 44 53 49 5a 45 20 6f 72 0a 20 20 20 20 20 CEDSIZE or.
2081f 20 2a 2a 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e ** EXPR_TOKENON
20820 4c 59 53 49 5a 45 2e 20 6e 54 6f 6b 65 6e 20 69 LYSIZE. nToken i
20821 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d s set to the num
20822 62 65 72 20 6f 66 20 62 79 74 65 73 20 63 6f 6e ber of bytes con
20823 73 75 6d 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 sumed. ** b
20824 79 20 74 68 65 20 63 6f 70 79 20 6f 66 20 74 68 y the copy of th
20825 65 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 73 74 e p->u.zToken st
20826 72 69 6e 67 20 28 69 66 20 61 6e 79 29 2e 0a 20 ring (if any)..
20827 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f */. co
20828 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 6e 53 74 nst unsigned nSt
20829 72 75 63 74 53 69 7a 65 20 3d 20 64 75 70 65 64 ructSize = duped
2082a 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 70 ExprStructSize(p
2082b 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 , flags);.
2082c 63 6f 6e 73 74 20 69 6e 74 20 6e 4e 65 77 53 69 const int nNewSi
2082d 7a 65 20 3d 20 6e 53 74 72 75 63 74 53 69 7a 65 ze = nStructSize
2082e 20 26 20 30 78 66 66 66 3b 0a 20 20 20 20 20 20 & 0xfff;.
2082f 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 int nToken;.
20830 20 20 69 66 28 20 21 45 78 70 72 48 61 73 50 72 if( !ExprHasPr
20831 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 49 6e 74 operty(p, EP_Int
20832 56 61 6c 75 65 29 20 26 26 20 70 2d 3e 75 2e 7a Value) && p->u.z
20833 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 Token ){.
20834 20 6e 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 nToken = sqlite
20835 33 53 74 72 6c 65 6e 33 30 28 70 2d 3e 75 2e 7a 3Strlen30(p->u.z
20836 54 6f 6b 65 6e 29 20 2b 20 31 3b 0a 20 20 20 20 Token) + 1;.
20837 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
20838 20 6e 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 nToken = 0;.
20839 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 }. if( i
2083a 73 52 65 64 75 63 65 64 20 29 7b 0a 20 20 20 20 sReduced ){.
2083b 20 20 20 20 61 73 73 65 72 74 28 20 45 78 70 72 assert( Expr
2083c 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 HasProperty(p, E
2083d 50 5f 52 65 64 75 63 65 64 29 3d 3d 30 20 29 3b P_Reduced)==0 );
2083e 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 . memcpy(
2083f 7a 41 6c 6c 6f 63 2c 20 70 2c 20 6e 4e 65 77 53 zAlloc, p, nNewS
20840 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 ize);. }els
20841 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e e{. int n
20842 53 69 7a 65 20 3d 20 65 78 70 72 53 74 72 75 63 Size = exprStruc
20843 74 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 tSize(p);.
20844 20 20 6d 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c memcpy(zAlloc,
20845 20 70 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 20 20 p, nSize);.
20846 20 20 20 20 6d 65 6d 73 65 74 28 26 7a 41 6c 6c memset(&zAll
20847 6f 63 5b 6e 53 69 7a 65 5d 2c 20 30 2c 20 45 58 oc[nSize], 0, EX
20848 50 52 5f 46 55 4c 4c 53 49 5a 45 2d 6e 53 69 7a PR_FULLSIZE-nSiz
20849 65 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 e);. }..
2084a 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 45 50 /* Set the EP
2084b 5f 52 65 64 75 63 65 64 2c 20 45 50 5f 54 6f 6b _Reduced, EP_Tok
2084c 65 6e 4f 6e 6c 79 2c 20 61 6e 64 20 45 50 5f 53 enOnly, and EP_S
2084d 74 61 74 69 63 20 66 6c 61 67 73 20 61 70 70 72 tatic flags appr
2084e 6f 70 72 69 61 74 65 6c 79 2e 20 2a 2f 0a 20 20 opriately. */.
2084f 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 pNew->flags
20850 26 3d 20 7e 28 45 50 5f 52 65 64 75 63 65 64 7c &= ~(EP_Reduced|
20851 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f EP_TokenOnly|EP_
20852 53 74 61 74 69 63 29 3b 0a 20 20 20 20 20 20 70 Static);. p
20853 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 6e 53 New->flags |= nS
20854 74 72 75 63 74 53 69 7a 65 20 26 20 28 45 50 5f tructSize & (EP_
20855 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e Reduced|EP_Token
20856 4f 6e 6c 79 29 3b 0a 20 20 20 20 20 20 70 4e 65 Only);. pNe
20857 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 73 74 61 74 w->flags |= stat
20858 69 63 46 6c 61 67 3b 0a 0a 20 20 20 20 20 20 2f icFlag;.. /
20859 2a 20 43 6f 70 79 20 74 68 65 20 70 2d 3e 75 2e * Copy the p->u.
2085a 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 2c 20 69 zToken string, i
2085b 66 20 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 20 20 f any. */.
2085c 69 66 28 20 6e 54 6f 6b 65 6e 20 29 7b 0a 20 20 if( nToken ){.
2085d 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 6f 6b char *zTok
2085e 65 6e 20 3d 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f en = pNew->u.zTo
2085f 6b 65 6e 20 3d 20 28 63 68 61 72 2a 29 26 7a 41 ken = (char*)&zA
20860 6c 6c 6f 63 5b 6e 4e 65 77 53 69 7a 65 5d 3b 0a lloc[nNewSize];.
20861 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a memcpy(z
20862 54 6f 6b 65 6e 2c 20 70 2d 3e 75 2e 7a 54 6f 6b Token, p->u.zTok
20863 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 en, nToken);.
20864 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 }.. if(
20865 30 3d 3d 28 28 70 2d 3e 66 6c 61 67 73 7c 70 4e 0==((p->flags|pN
20866 65 77 2d 3e 66 6c 61 67 73 29 20 26 20 45 50 5f ew->flags) & EP_
20867 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 TokenOnly) ){.
20868 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e /* Fill in
20869 20 74 68 65 20 70 4e 65 77 2d 3e 78 2e 70 53 65 the pNew->x.pSe
2086a 6c 65 63 74 20 6f 72 20 70 4e 65 77 2d 3e 78 2e lect or pNew->x.
2086b 70 4c 69 73 74 20 6d 65 6d 62 65 72 2e 20 2a 2f pList member. */
2086c 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 78 70 . if( Exp
2086d 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 rHasProperty(p,
2086e 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b EP_xIsSelect) ){
2086f 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d . pNew-
20870 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c >x.pSelect = sql
20871 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 ite3SelectDup(db
20872 2c 20 70 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20 , p->x.pSelect,
20873 69 73 52 65 64 75 63 65 64 29 3b 0a 20 20 20 20 isReduced);.
20874 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
20875 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 pNew->x.pLi
20876 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 st = sqlite3Expr
20877 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 78 ListDup(db, p->x
20878 2e 70 4c 69 73 74 2c 20 69 73 52 65 64 75 63 65 .pList, isReduce
20879 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 d);. }.
2087a 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 }.. /*
2087b 46 69 6c 6c 20 69 6e 20 70 4e 65 77 2d 3e 70 4c Fill in pNew->pL
2087c 65 66 74 20 61 6e 64 20 70 4e 65 77 2d 3e 70 52 eft and pNew->pR
2087d 69 67 68 74 2e 20 2a 2f 0a 20 20 20 20 20 20 69 ight. */. i
2087e 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f f( ExprHasAnyPro
2087f 70 65 72 74 79 28 70 4e 65 77 2c 20 45 50 5f 52 perty(pNew, EP_R
20880 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f educed|EP_TokenO
20881 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 20 20 nly) ){.
20882 7a 41 6c 6c 6f 63 20 2b 3d 20 64 75 70 65 64 45 zAlloc += dupedE
20883 78 70 72 4e 6f 64 65 53 69 7a 65 28 70 2c 20 66 xprNodeSize(p, f
20884 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 69 lags);. i
20885 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 f( ExprHasProper
20886 74 79 28 70 4e 65 77 2c 20 45 50 5f 52 65 64 75 ty(pNew, EP_Redu
20887 63 65 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 ced) ){.
20888 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 pNew->pLeft =
20889 65 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 exprDup(db, p->p
2088a 4c 65 66 74 2c 20 45 58 50 52 44 55 50 5f 52 45 Left, EXPRDUP_RE
2088b 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b 0a DUCE, &zAlloc);.
2088c 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e pNew->
2088d 70 52 69 67 68 74 20 3d 20 65 78 70 72 44 75 70 pRight = exprDup
2088e 28 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20 (db, p->pRight,
2088f 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20 EXPRDUP_REDUCE,
20890 26 7a 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 &zAlloc);.
20891 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
20892 70 7a 42 75 66 66 65 72 20 29 7b 0a 20 20 20 20 pzBuffer ){.
20893 20 20 20 20 20 20 2a 70 7a 42 75 66 66 65 72 20 *pzBuffer
20894 3d 20 7a 41 6c 6c 6f 63 3b 0a 20 20 20 20 20 20 = zAlloc;.
20895 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
20896 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 . pNew->f
20897 6c 61 67 73 32 20 3d 20 30 3b 0a 20 20 20 20 20 lags2 = 0;.
20898 20 20 20 69 66 28 20 21 45 78 70 72 48 61 73 41 if( !ExprHasA
20899 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 nyProperty(p, EP
2089a 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 _TokenOnly) ){.
2089b 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 pNew->p
2089c 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 Left = sqlite3Ex
2089d 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 65 prDup(db, p->pLe
2089e 66 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 ft, 0);.
2089f 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d pNew->pRight =
208a0 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 sqlite3ExprDup(
208a1 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20 30 db, p->pRight, 0
208a2 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
208a3 20 20 20 7d 0a 0a 20 20 20 20 7d 0a 20 20 7d 0a }.. }. }.
208a4 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d return pNew;.}
208a5 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
208a6 6f 77 69 6e 67 20 67 72 6f 75 70 20 6f 66 20 72 owing group of r
208a7 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 64 65 65 outines make dee
208a8 70 20 63 6f 70 69 65 73 20 6f 66 20 65 78 70 72 p copies of expr
208a9 65 73 73 69 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72 essions,.** expr
208aa 65 73 73 69 6f 6e 20 6c 69 73 74 73 2c 20 49 44 ession lists, ID
208ab 20 6c 69 73 74 73 2c 20 61 6e 64 20 73 65 6c 65 lists, and sele
208ac 63 74 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 ct statements.
208ad 54 68 65 20 63 6f 70 69 65 73 20 63 61 6e 0a 2a The copies can.*
208ae 2a 20 62 65 20 64 65 6c 65 74 65 64 20 28 62 79 * be deleted (by
208af 20 62 65 69 6e 67 20 70 61 73 73 65 64 20 74 6f being passed to
208b0 20 74 68 65 69 72 20 72 65 73 70 65 63 74 69 76 their respectiv
208b1 65 20 2e 2e 2e 44 65 6c 65 74 65 28 29 20 72 6f e ...Delete() ro
208b2 75 74 69 6e 65 73 29 0a 2a 2a 20 77 69 74 68 6f utines).** witho
208b3 75 74 20 65 66 66 65 63 74 69 6e 67 20 74 68 65 ut effecting the
208b4 20 6f 72 69 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a originals..**.*
208b5 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e * The expression
208b6 20 6c 69 73 74 2c 20 49 44 2c 20 61 6e 64 20 73 list, ID, and s
208b7 6f 75 72 63 65 20 6c 69 73 74 73 20 72 65 74 75 ource lists retu
208b8 72 6e 20 62 79 20 73 71 6c 69 74 65 33 45 78 70 rn by sqlite3Exp
208b9 72 4c 69 73 74 44 75 70 28 29 2c 0a 2a 2a 20 73 rListDup(),.** s
208ba 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 qlite3IdListDup(
208bb 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 53 72 ), and sqlite3Sr
208bc 63 4c 69 73 74 44 75 70 28 29 20 63 61 6e 20 6e cListDup() can n
208bd 6f 74 20 62 65 20 66 75 72 74 68 65 72 20 65 78 ot be further ex
208be 70 61 6e 64 65 64 20 0a 2a 2a 20 62 79 20 73 75 panded .** by su
208bf 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 bsequent calls t
208c0 6f 20 73 71 6c 69 74 65 2a 4c 69 73 74 41 70 70 o sqlite*ListApp
208c1 65 6e 64 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a end() routines..
208c2 2a 2a 0a 2a 2a 20 41 6e 79 20 74 61 62 6c 65 73 **.** Any tables
208c3 20 74 68 61 74 20 74 68 65 20 53 72 63 4c 69 73 that the SrcLis
208c4 74 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f t might point to
208c5 20 61 72 65 20 6e 6f 74 20 64 75 70 6c 69 63 61 are not duplica
208c6 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ted..**.** The f
208c7 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 lags parameter c
208c8 6f 6e 74 61 69 6e 73 20 61 20 63 6f 6d 62 69 6e ontains a combin
208c9 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 45 58 50 ation of the EXP
208ca 52 44 55 50 5f 58 58 58 20 66 6c 61 67 73 2e 0a RDUP_XXX flags..
208cb 2a 2a 20 49 66 20 74 68 65 20 45 58 50 52 44 55 ** If the EXPRDU
208cc 50 5f 52 45 44 55 43 45 20 66 6c 61 67 20 69 73 P_REDUCE flag is
208cd 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 set, then the s
208ce 74 72 75 63 74 75 72 65 20 72 65 74 75 72 6e 65 tructure returne
208cf 64 20 69 73 20 61 0a 2a 2a 20 74 72 75 6e 63 61 d is a.** trunca
208d0 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 ted version of t
208d1 68 65 20 75 73 75 61 6c 20 45 78 70 72 20 73 74 he usual Expr st
208d2 72 75 63 74 75 72 65 20 74 68 61 74 20 77 69 6c ructure that wil
208d3 6c 20 62 65 20 73 74 6f 72 65 64 20 61 73 0a 2a l be stored as.*
208d4 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e * part of the in
208d5 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e -memory represen
208d6 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 tation of the da
208d7 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a tabase schema..*
208d8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
208d9 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 Expr *sqlite3Ex
208da 70 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 prDup(sqlite3 *d
208db 62 2c 20 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 b, Expr *p, int
208dc 66 6c 61 67 73 29 7b 0a 20 20 72 65 74 75 72 6e flags){. return
208dd 20 65 78 70 72 44 75 70 28 64 62 2c 20 70 2c 20 exprDup(db, p,
208de 66 6c 61 67 73 2c 20 30 29 3b 0a 7d 0a 53 51 4c flags, 0);.}.SQL
208df 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
208e0 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 List *sqlite3Exp
208e1 72 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 rListDup(sqlite3
208e2 20 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 20 2a *db, ExprList *
208e3 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 p, int flags){.
208e4 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65 77 3b ExprList *pNew;
208e5 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 . struct ExprLi
208e6 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 2c 20 st_item *pItem,
208e7 2a 70 4f 6c 64 49 74 65 6d 3b 0a 20 20 69 6e 74 *pOldItem;. int
208e8 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 i;. if( p==0 )
208e9 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 return 0;. pNe
208ea 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c w = sqlite3DbMal
208eb 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f locRaw(db, sizeo
208ec 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69 66 f(*pNew) );. if
208ed 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 ( pNew==0 ) retu
208ee 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 45 rn 0;. pNew->iE
208ef 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 70 4e Cursor = 0;. pN
208f0 65 77 2d 3e 6e 45 78 70 72 20 3d 20 70 4e 65 77 ew->nExpr = pNew
208f1 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 45 ->nAlloc = p->nE
208f2 78 70 72 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d xpr;. pNew->a =
208f3 20 70 49 74 65 6d 20 3d 20 73 71 6c 69 74 65 33 pItem = sqlite3
208f4 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
208f5 20 70 2d 3e 6e 45 78 70 72 2a 73 69 7a 65 6f 66 p->nExpr*sizeof
208f6 28 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 (p->a[0]) );. i
208f7 66 28 20 70 49 74 65 6d 3d 3d 30 20 29 7b 0a 20 f( pItem==0 ){.
208f8 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
208f9 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 (db, pNew);.
208fa 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 20 0a 20 return 0;. } .
208fb 20 70 4f 6c 64 49 74 65 6d 20 3d 20 70 2d 3e 61 pOldItem = p->a
208fc 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 ;. for(i=0; i<p
208fd 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 ->nExpr; i++, pI
208fe 74 65 6d 2b 2b 2c 20 70 4f 6c 64 49 74 65 6d 2b tem++, pOldItem+
208ff 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4f +){. Expr *pO
20900 6c 64 45 78 70 72 20 3d 20 70 4f 6c 64 49 74 65 ldExpr = pOldIte
20901 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 70 49 m->pExpr;. pI
20902 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c tem->pExpr = sql
20903 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 ite3ExprDup(db,
20904 70 4f 6c 64 45 78 70 72 2c 20 66 6c 61 67 73 29 pOldExpr, flags)
20905 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 ;. pItem->zNa
20906 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 me = sqlite3DbSt
20907 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 rDup(db, pOldIte
20908 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 m->zName);. p
20909 49 74 65 6d 2d 3e 7a 53 70 61 6e 20 3d 20 73 71 Item->zSpan = sq
2090a 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 lite3DbStrDup(db
2090b 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 53 70 61 , pOldItem->zSpa
2090c 6e 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 73 n);. pItem->s
2090d 6f 72 74 4f 72 64 65 72 20 3d 20 70 4f 6c 64 49 ortOrder = pOldI
2090e 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a tem->sortOrder;.
2090f 20 20 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 pItem->done
20910 3d 20 30 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e = 0;. pItem->
20911 69 43 6f 6c 20 3d 20 70 4f 6c 64 49 74 65 6d 2d iCol = pOldItem-
20912 3e 69 43 6f 6c 3b 0a 20 20 20 20 70 49 74 65 6d >iCol;. pItem
20913 2d 3e 69 41 6c 69 61 73 20 3d 20 70 4f 6c 64 49 ->iAlias = pOldI
20914 74 65 6d 2d 3e 69 41 6c 69 61 73 3b 0a 20 20 7d tem->iAlias;. }
20915 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a . return pNew;.
20916 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 75 72 73 }../*.** If curs
20917 6f 72 73 2c 20 74 72 69 67 67 65 72 73 2c 20 76 ors, triggers, v
20918 69 65 77 73 20 61 6e 64 20 73 75 62 71 75 65 72 iews and subquer
20919 69 65 73 20 61 72 65 20 61 6c 6c 20 6f 6d 69 74 ies are all omit
2091a 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 ted from.** the
2091b 62 75 69 6c 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 build, then none
2091c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
2091d 67 20 72 6f 75 74 69 6e 65 73 2c 20 65 78 63 65 g routines, exce
2091e 70 74 20 66 6f 72 20 0a 2a 2a 20 73 71 6c 69 74 pt for .** sqlit
2091f 65 33 53 65 6c 65 63 74 44 75 70 28 29 2c 20 63 e3SelectDup(), c
20920 61 6e 20 62 65 20 63 61 6c 6c 65 64 2e 20 73 71 an be called. sq
20921 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 29 lite3SelectDup()
20922 20 69 73 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a is sometimes.**
20923 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e called with a N
20924 55 4c 4c 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f ULL argument..*/
20925 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
20926 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 LITE_OMIT_VIEW)
20927 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 || !defined(SQLI
20928 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 TE_OMIT_TRIGGER)
20929 20 5c 0a 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 \. || !defined(
2092a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 SQLITE_OMIT_SUBQ
2092b 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 UERY).SQLITE_PRI
2092c 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 VATE SrcList *sq
2092d 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 lite3SrcListDup(
2092e 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 63 sqlite3 *db, Src
2092f 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 List *p, int fla
20930 67 73 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a gs){. SrcList *
20931 70 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 pNew;. int i;.
20932 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 66 int nByte;. if
20933 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
20934 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 0;. nByte = siz
20935 65 6f 66 28 2a 70 29 20 2b 20 28 70 2d 3e 6e 53 eof(*p) + (p->nS
20936 72 63 3e 30 20 3f 20 73 69 7a 65 6f 66 28 70 2d rc>0 ? sizeof(p-
20937 3e 61 5b 30 5d 29 20 2a 20 28 70 2d 3e 6e 53 72 >a[0]) * (p->nSr
20938 63 2d 31 29 20 3a 20 30 29 3b 0a 20 20 70 4e 65 c-1) : 0);. pNe
20939 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c w = sqlite3DbMal
2093a 6c 6f 63 52 61 77 28 64 62 2c 20 6e 42 79 74 65 locRaw(db, nByte
2093b 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d );. if( pNew==
2093c 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
2093d 70 4e 65 77 2d 3e 6e 53 72 63 20 3d 20 70 4e 65 pNew->nSrc = pNe
2093e 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e w->nAlloc = p->n
2093f 53 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 Src;. for(i=0;
20940 69 3c 70 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b i<p->nSrc; i++){
20941 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c . struct SrcL
20942 69 73 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 ist_item *pNewIt
20943 65 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d em = &pNew->a[i]
20944 3b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 ;. struct Src
20945 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 List_item *pOldI
20946 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a tem = &p->a[i];.
20947 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b Table *pTab;
20948 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a . pNewItem->z
20949 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 Database = sqlit
2094a 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 e3DbStrDup(db, p
2094b 4f 6c 64 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 OldItem->zDataba
2094c 73 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 se);. pNewIte
2094d 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 m->zName = sqlit
2094e 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 e3DbStrDup(db, p
2094f 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b OldItem->zName);
20950 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a . pNewItem->z
20951 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44 Alias = sqlite3D
20952 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 bStrDup(db, pOld
20953 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 Item->zAlias);.
20954 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6a 6f 69 pNewItem->joi
20955 6e 74 79 70 65 20 3d 20 70 4f 6c 64 49 74 65 6d ntype = pOldItem
20956 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20 ->jointype;.
20957 70 4e 65 77 49 74 65 6d 2d 3e 69 43 75 72 73 6f pNewItem->iCurso
20958 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 r = pOldItem->iC
20959 75 72 73 6f 72 3b 0a 20 20 20 20 70 4e 65 77 49 ursor;. pNewI
2095a 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 tem->isPopulated
2095b 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 73 50 = pOldItem->isP
2095c 6f 70 75 6c 61 74 65 64 3b 0a 20 20 20 20 70 4e opulated;. pN
2095d 65 77 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d ewItem->zIndex =
2095e 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 sqlite3DbStrDup
2095f 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a (db, pOldItem->z
20960 49 6e 64 65 78 29 3b 0a 20 20 20 20 70 4e 65 77 Index);. pNew
20961 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 Item->notIndexed
20962 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6e 6f 74 = pOldItem->not
20963 49 6e 64 65 78 65 64 3b 0a 20 20 20 20 70 4e 65 Indexed;. pNe
20964 77 49 74 65 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 wItem->pIndex =
20965 70 4f 6c 64 49 74 65 6d 2d 3e 70 49 6e 64 65 78 pOldItem->pIndex
20966 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 4e 65 ;. pTab = pNe
20967 77 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70 4f wItem->pTab = pO
20968 6c 64 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 ldItem->pTab;.
20969 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 if( pTab ){.
2096a 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b pTab->nRef++
2096b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 ;. }. pNew
2096c 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 Item->pSelect =
2096d 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 sqlite3SelectDup
2096e 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 (db, pOldItem->p
2096f 53 65 6c 65 63 74 2c 20 66 6c 61 67 73 29 3b 0a Select, flags);.
20970 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 4f pNewItem->pO
20971 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 n = sqlite3ExprD
20972 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d up(db, pOldItem-
20973 3e 70 4f 6e 2c 20 66 6c 61 67 73 29 3b 0a 20 20 >pOn, flags);.
20974 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 55 73 69 pNewItem->pUsi
20975 6e 67 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 ng = sqlite3IdLi
20976 73 74 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 stDup(db, pOldIt
20977 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20 20 em->pUsing);.
20978 20 70 4e 65 77 49 74 65 6d 2d 3e 63 6f 6c 55 73 pNewItem->colUs
20979 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 63 ed = pOldItem->c
2097a 6f 6c 55 73 65 64 3b 0a 20 20 7d 0a 20 20 72 65 olUsed;. }. re
2097b 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c turn pNew;.}.SQL
2097c 49 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69 ITE_PRIVATE IdLi
2097d 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 st *sqlite3IdLis
2097e 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 tDup(sqlite3 *db
2097f 2c 20 49 64 4c 69 73 74 20 2a 70 29 7b 0a 20 20 , IdList *p){.
20980 49 64 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 IdList *pNew;.
20981 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d int i;. if( p==
20982 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
20983 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 pNew = sqlite3Db
20984 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 MallocRaw(db, si
20985 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 zeof(*pNew) );.
20986 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 if( pNew==0 ) r
20987 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d eturn 0;. pNew-
20988 3e 6e 49 64 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c >nId = pNew->nAl
20989 6c 6f 63 20 3d 20 70 2d 3e 6e 49 64 3b 0a 20 20 loc = p->nId;.
2098a 70 4e 65 77 2d 3e 61 20 3d 20 73 71 6c 69 74 65 pNew->a = sqlite
2098b 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
2098c 20 70 2d 3e 6e 49 64 2a 73 69 7a 65 6f 66 28 70 p->nId*sizeof(p
2098d 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 ->a[0]) );. if(
2098e 20 70 4e 65 77 2d 3e 61 3d 3d 30 20 29 7b 0a 20 pNew->a==0 ){.
2098f 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
20990 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 (db, pNew);.
20991 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
20992 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 49 for(i=0; i<p->nI
20993 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 d; i++){. str
20994 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 uct IdList_item
20995 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e 65 *pNewItem = &pNe
20996 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72 w->a[i];. str
20997 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 uct IdList_item
20998 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e *pOldItem = &p->
20999 61 5b 69 5d 3b 0a 20 20 20 20 70 4e 65 77 49 74 a[i];. pNewIt
2099a 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 em->zName = sqli
2099b 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 te3DbStrDup(db,
2099c 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 pOldItem->zName)
2099d 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e ;. pNewItem->
2099e 69 64 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e idx = pOldItem->
2099f 69 64 78 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 idx;. }. retur
209a0 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 n pNew;.}.SQLITE
209a1 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 _PRIVATE Select
209a2 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 *sqlite3SelectDu
209a3 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 p(sqlite3 *db, S
209a4 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 66 6c elect *p, int fl
209a5 61 67 73 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a ags){. Select *
209a6 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 3d 3d 30 pNew;. if( p==0
209a7 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 ) return 0;. p
209a8 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d New = sqlite3DbM
209a9 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a allocRaw(db, siz
209aa 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69 66 28 eof(*p) );. if(
209ab 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 pNew==0 ) retur
209ac 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 70 45 4c n 0;. pNew->pEL
209ad 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 ist = sqlite3Exp
209ae 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e rListDup(db, p->
209af 70 45 4c 69 73 74 2c 20 66 6c 61 67 73 29 3b 0a pEList, flags);.
209b0 20 20 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20 73 pNew->pSrc = s
209b1 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 qlite3SrcListDup
209b2 28 64 62 2c 20 70 2d 3e 70 53 72 63 2c 20 66 6c (db, p->pSrc, fl
209b3 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 ags);. pNew->pW
209b4 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 here = sqlite3Ex
209b5 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 57 68 prDup(db, p->pWh
209b6 65 72 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 ere, flags);. p
209b7 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 New->pGroupBy =
209b8 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 sqlite3ExprListD
209b9 75 70 28 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 up(db, p->pGroup
209ba 42 79 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e By, flags);. pN
209bb 65 77 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 71 ew->pHaving = sq
209bc 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
209bd 20 70 2d 3e 70 48 61 76 69 6e 67 2c 20 66 6c 61 p->pHaving, fla
209be 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 gs);. pNew->pOr
209bf 64 65 72 42 79 20 3d 20 73 71 6c 69 74 65 33 45 derBy = sqlite3E
209c0 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 xprListDup(db, p
209c1 2d 3e 70 4f 72 64 65 72 42 79 2c 20 66 6c 61 67 ->pOrderBy, flag
209c2 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d s);. pNew->op =
209c3 20 70 2d 3e 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e p->op;. pNew->
209c4 70 50 72 69 6f 72 20 3d 20 73 71 6c 69 74 65 33 pPrior = sqlite3
209c5 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d SelectDup(db, p-
209c6 3e 70 50 72 69 6f 72 2c 20 66 6c 61 67 73 29 3b >pPrior, flags);
209c7 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 . pNew->pLimit
209c8 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 = sqlite3ExprDup
209c9 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 (db, p->pLimit,
209ca 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e flags);. pNew->
209cb 70 4f 66 66 73 65 74 20 3d 20 73 71 6c 69 74 65 pOffset = sqlite
209cc 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 3ExprDup(db, p->
209cd 70 4f 66 66 73 65 74 2c 20 66 6c 61 67 73 29 3b pOffset, flags);
209ce 0a 20 20 70 4e 65 77 2d 3e 69 4c 69 6d 69 74 20 . pNew->iLimit
209cf 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 4f 66 = 0;. pNew->iOf
209d0 66 73 65 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 fset = 0;. pNew
209d1 2d 3e 73 65 6c 46 6c 61 67 73 20 3d 20 70 2d 3e ->selFlags = p->
209d2 73 65 6c 46 6c 61 67 73 20 26 20 7e 53 46 5f 55 selFlags & ~SF_U
209d3 73 65 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 sesEphemeral;.
209d4 70 4e 65 77 2d 3e 70 52 69 67 68 74 6d 6f 73 74 pNew->pRightmost
209d5 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 = 0;. pNew->ad
209d6 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 drOpenEphm[0] =
209d7 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 -1;. pNew->addr
209d8 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 OpenEphm[1] = -1
209d9 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 ;. pNew->addrOp
209da 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a enEphm[2] = -1;.
209db 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d return pNew;.}
209dc 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 .#else.SQLITE_PR
209dd 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 IVATE Select *sq
209de 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 73 lite3SelectDup(s
209df 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 qlite3 *db, Sele
209e0 63 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 ct *p, int flags
209e1 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d ){. assert( p==
209e2 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0 );. return 0;
209e3 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a .}.#endif.../*.*
209e4 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d * Add a new elem
209e5 65 6e 74 20 74 6f 20 74 68 65 20 65 6e 64 20 6f ent to the end o
209e6 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 f an expression
209e7 6c 69 73 74 2e 20 20 49 66 20 70 4c 69 73 74 20 list. If pList
209e8 69 73 0a 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 is.** initially
209e9 4e 55 4c 4c 2c 20 74 68 65 6e 20 63 72 65 61 74 NULL, then creat
209ea 65 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 e a new expressi
209eb 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 on list..**.** I
209ec 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 f a memory alloc
209ed 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 ation error occu
209ee 72 73 2c 20 74 68 65 20 65 6e 74 69 72 65 20 6c rs, the entire l
209ef 69 73 74 20 69 73 20 66 72 65 65 64 20 61 6e 64 ist is freed and
209f0 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 .** NULL is retu
209f1 72 6e 65 64 2e 20 20 49 66 20 6e 6f 6e 2d 4e 55 rned. If non-NU
209f2 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 LL is returned,
209f3 74 68 65 6e 20 69 74 20 69 73 20 67 75 61 72 61 then it is guara
209f4 6e 74 65 65 64 0a 2a 2a 20 74 68 61 74 20 74 68 nteed.** that th
209f5 65 20 6e 65 77 20 65 6e 74 72 79 20 77 61 73 20 e new entry was
209f6 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 70 70 successfully app
209f7 65 6e 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ended..*/.SQLITE
209f8 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 _PRIVATE ExprLis
209f9 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 t *sqlite3ExprLi
209fa 73 74 41 70 70 65 6e 64 28 0a 20 20 50 61 72 73 stAppend(. Pars
209fb 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
209fc 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
209fd 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 ontext */. Expr
209fe 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 List *pList,
209ff 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 /* List to w
20a00 68 69 63 68 20 74 6f 20 61 70 70 65 6e 64 2e 20 hich to append.
20a01 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f Might be NULL */
20a02 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 . Expr *pExpr
20a03 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 /* Ex
20a04 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 pression to be a
20a05 70 70 65 6e 64 65 64 2e 20 4d 69 67 68 74 20 62 ppended. Might b
20a06 65 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 73 e NULL */.){. s
20a07 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
20a08 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 rse->db;. if( p
20a09 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 List==0 ){. p
20a0a 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 List = sqlite3Db
20a0b 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 MallocZero(db, s
20a0c 69 7a 65 6f 66 28 45 78 70 72 4c 69 73 74 29 20 izeof(ExprList)
20a0d 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 );. if( pList
20a0e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 ==0 ){. got
20a0f 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a o no_mem;. }.
20a10 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 assert( pLis
20a11 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a t->nAlloc==0 );.
20a12 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 2d }. if( pList-
20a13 3e 6e 41 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e >nAlloc<=pList->
20a14 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 73 74 72 nExpr ){. str
20a15 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
20a16 6d 20 2a 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 m *a;. int n
20a17 3d 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a = pList->nAlloc*
20a18 32 20 2b 20 34 3b 0a 20 20 20 20 61 20 3d 20 73 2 + 4;. a = s
20a19 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 qlite3DbRealloc(
20a1a 64 62 2c 20 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a db, pList->a, n*
20a1b 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b sizeof(pList->a[
20a1c 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 61 3d 0]));. if( a=
20a1d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f =0 ){. goto
20a1e 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 no_mem;. }.
20a1f 20 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 61 3b pList->a = a;
20a20 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c . pList->nAll
20a21 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 oc = sqlite3DbMa
20a22 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 61 29 2f llocSize(db, a)/
20a23 73 69 7a 65 6f 66 28 61 5b 30 5d 29 3b 0a 20 20 sizeof(a[0]);.
20a24 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 }. assert( pLis
20a25 74 2d 3e 61 21 3d 30 20 29 3b 0a 20 20 69 66 28 t->a!=0 );. if(
20a26 20 31 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 1 ){. struct
20a27 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
20a28 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e pItem = &pList->
20a29 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2b 2b a[pList->nExpr++
20a2a 5d 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 49 ];. memset(pI
20a2b 74 65 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a tem, 0, sizeof(*
20a2c 70 49 74 65 6d 29 29 3b 0a 20 20 20 20 70 49 74 pItem));. pIt
20a2d 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 78 70 em->pExpr = pExp
20a2e 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 r;. }. return
20a2f 70 4c 69 73 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 20 pList;..no_mem:
20a30 20 20 20 20 0a 20 20 2f 2a 20 41 76 6f 69 64 20 . /* Avoid
20a31 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 69 leaking memory i
20a32 66 20 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61 69 f malloc has fai
20a33 6c 65 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 led. */. sqlite
20a34 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 3ExprDelete(db,
20a35 70 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 pExpr);. sqlite
20a36 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 3ExprListDelete(
20a37 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 72 65 db, pList);. re
20a38 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
20a39 20 53 65 74 20 74 68 65 20 45 78 70 72 4c 69 73 Set the ExprLis
20a3a 74 2e 61 5b 5d 2e 7a 4e 61 6d 65 20 65 6c 65 6d t.a[].zName elem
20a3b 65 6e 74 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 ent of the most
20a3c 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 69 recently added i
20a3d 74 65 6d 0a 2a 2a 20 6f 6e 20 74 68 65 20 65 78 tem.** on the ex
20a3e 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a pression list..*
20a3f 2a 0a 2a 2a 20 70 4c 69 73 74 20 6d 69 67 68 74 *.** pList might
20a40 20 62 65 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 69 be NULL followi
20a41 6e 67 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e ng an OOM error.
20a42 20 20 42 75 74 20 70 4e 61 6d 65 20 73 68 6f 75 But pName shou
20a43 6c 64 20 6e 65 76 65 72 20 62 65 0a 2a 2a 20 4e ld never be.** N
20a44 55 4c 4c 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 ULL. If a memor
20a45 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 y allocation fai
20a46 6c 73 2c 20 74 68 65 20 70 50 61 72 73 65 2d 3e ls, the pParse->
20a47 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
20a48 20 66 6c 61 67 0a 2a 2a 20 69 73 20 73 65 74 2e flag.** is set.
20a49 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20a4a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
20a4b 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 0a xprListSetName(.
20a4c 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
20a4d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 /* Par
20a4e 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
20a4f 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 ExprList *pLis
20a50 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 t, /* Lis
20a51 74 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 64 t to which to ad
20a52 64 20 74 68 65 20 73 70 61 6e 2e 20 2a 2f 0a 20 d the span. */.
20a53 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 2c 20 20 Token *pName,
20a54 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
20a55 20 74 6f 20 62 65 20 61 64 64 65 64 20 2a 2f 0a to be added */.
20a56 20 20 69 6e 74 20 64 65 71 75 6f 74 65 20 20 20 int dequote
20a57 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
20a58 65 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 6e e to cause the n
20a59 61 6d 65 20 74 6f 20 62 65 20 64 65 71 75 6f 74 ame to be dequot
20a5a 65 64 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 ed */.){. asser
20a5b 74 28 20 70 4c 69 73 74 21 3d 30 20 7c 7c 20 70 t( pList!=0 || p
20a5c 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f Parse->db->mallo
20a5d 63 46 61 69 6c 65 64 21 3d 30 20 29 3b 0a 20 20 cFailed!=0 );.
20a5e 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 if( pList ){.
20a5f 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
20a60 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 _item *pItem;.
20a61 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d assert( pList-
20a62 3e 6e 45 78 70 72 3e 30 20 29 3b 0a 20 20 20 20 >nExpr>0 );.
20a63 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e pItem = &pList->
20a64 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 a[pList->nExpr-1
20a65 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ];. assert( p
20a66 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29 Item->zName==0 )
20a67 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 ;. pItem->zNa
20a68 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 me = sqlite3DbSt
20a69 72 4e 44 75 70 28 70 50 61 72 73 65 2d 3e 64 62 rNDup(pParse->db
20a6a 2c 20 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d , pName->z, pNam
20a6b 65 2d 3e 6e 29 3b 0a 20 20 20 20 69 66 28 20 64 e->n);. if( d
20a6c 65 71 75 6f 74 65 20 26 26 20 70 49 74 65 6d 2d equote && pItem-
20a6d 3e 7a 4e 61 6d 65 20 29 20 73 71 6c 69 74 65 33 >zName ) sqlite3
20a6e 44 65 71 75 6f 74 65 28 70 49 74 65 6d 2d 3e 7a Dequote(pItem->z
20a6f 4e 61 6d 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a Name);. }.}../*
20a70 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 .** Set the Expr
20a71 4c 69 73 74 2e 61 5b 5d 2e 7a 53 70 61 6e 20 65 List.a[].zSpan e
20a72 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 6d 6f lement of the mo
20a73 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 st recently adde
20a74 64 20 69 74 65 6d 0a 2a 2a 20 6f 6e 20 74 68 65 d item.** on the
20a75 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
20a76 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 6d 69 ..**.** pList mi
20a77 67 68 74 20 62 65 20 4e 55 4c 4c 20 66 6f 6c 6c ght be NULL foll
20a78 6f 77 69 6e 67 20 61 6e 20 4f 4f 4d 20 65 72 72 owing an OOM err
20a79 6f 72 2e 20 20 42 75 74 20 70 53 70 61 6e 20 73 or. But pSpan s
20a7a 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 0a 2a hould never be.*
20a7b 2a 20 4e 55 4c 4c 2e 20 20 49 66 20 61 20 6d 65 * NULL. If a me
20a7c 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
20a7d 66 61 69 6c 73 2c 20 74 68 65 20 70 50 61 72 73 fails, the pPars
20a7e 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 e->db->mallocFai
20a7f 6c 65 64 20 66 6c 61 67 0a 2a 2a 20 69 73 20 73 led flag.** is s
20a80 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 et..*/.SQLITE_PR
20a81 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
20a82 65 33 45 78 70 72 4c 69 73 74 53 65 74 53 70 61 e3ExprListSetSpa
20a83 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 n(. Parse *pPar
20a84 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 se, /*
20a85 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 Parsing context
20a86 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 */. ExprList *p
20a87 4c 69 73 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 List, /*
20a88 4c 69 73 74 20 74 6f 20 77 68 69 63 68 20 74 6f List to which to
20a89 20 61 64 64 20 74 68 65 20 73 70 61 6e 2e 20 2a add the span. *
20a8a 2f 0a 20 20 45 78 70 72 53 70 61 6e 20 2a 70 53 /. ExprSpan *pS
20a8b 70 61 6e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 pan /* T
20a8c 68 65 20 73 70 61 6e 20 74 6f 20 62 65 20 61 64 he span to be ad
20a8d 64 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 ded */.){. sqli
20a8e 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
20a8f 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 ->db;. assert(
20a90 70 4c 69 73 74 21 3d 30 20 7c 7c 20 64 62 2d 3e pList!=0 || db->
20a91 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 21 3d 30 20 mallocFailed!=0
20a92 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29 );. if( pList )
20a93 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 70 {. struct Exp
20a94 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 rList_item *pIte
20a95 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c m = &pList->a[pL
20a96 69 73 74 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0a 20 ist->nExpr-1];.
20a97 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 assert( pList
20a98 2d 3e 6e 45 78 70 72 3e 30 20 29 3b 0a 20 20 20 ->nExpr>0 );.
20a99 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c assert( db->mal
20a9a 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 70 49 74 locFailed || pIt
20a9b 65 6d 2d 3e 70 45 78 70 72 3d 3d 70 53 70 61 6e em->pExpr==pSpan
20a9c 2d 3e 70 45 78 70 72 20 29 3b 0a 20 20 20 20 73 ->pExpr );. s
20a9d 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
20a9e 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a pItem->zSpan);.
20a9f 20 20 20 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e pItem->zSpan
20aa0 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e = sqlite3DbStrN
20aa1 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 Dup(db, (char*)p
20aa2 53 70 61 6e 2d 3e 7a 53 74 61 72 74 2c 0a 20 20 Span->zStart,.
20aa3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20aa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20aa5 20 20 28 69 6e 74 29 28 70 53 70 61 6e 2d 3e 7a (int)(pSpan->z
20aa6 45 6e 64 20 2d 20 70 53 70 61 6e 2d 3e 7a 53 74 End - pSpan->zSt
20aa7 61 72 74 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a art));. }.}../*
20aa8 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 .** If the expre
20aa9 73 73 69 6f 6e 20 6c 69 73 74 20 70 45 4c 69 73 ssion list pELis
20aaa 74 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 t contains more
20aab 74 68 61 6e 20 69 4c 69 6d 69 74 20 65 6c 65 6d than iLimit elem
20aac 65 6e 74 73 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 ents,.** leave a
20aad 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 n error message
20aae 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 in pParse..*/.SQ
20aaf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
20ab0 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 d sqlite3ExprLis
20ab1 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 0a 20 20 tCheckLength(.
20ab2 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 Parse *pParse,.
20ab3 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 ExprList *pELis
20ab4 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 t,. const char
20ab5 2a 7a 4f 62 6a 65 63 74 0a 29 7b 0a 20 20 69 6e *zObject.){. in
20ab6 74 20 6d 78 20 3d 20 70 50 61 72 73 65 2d 3e 64 t mx = pParse->d
20ab7 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
20ab8 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 3b 0a _LIMIT_COLUMN];.
20ab9 20 20 74 65 73 74 63 61 73 65 28 20 70 45 4c 69 testcase( pELi
20aba 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 st && pEList->nE
20abb 78 70 72 3d 3d 6d 78 20 29 3b 0a 20 20 74 65 73 xpr==mx );. tes
20abc 74 63 61 73 65 28 20 70 45 4c 69 73 74 20 26 26 tcase( pEList &&
20abd 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d pEList->nExpr==
20abe 6d 78 2b 31 20 29 3b 0a 20 20 69 66 28 20 70 45 mx+1 );. if( pE
20abf 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e List && pEList->
20ac0 6e 45 78 70 72 3e 6d 78 20 29 7b 0a 20 20 20 20 nExpr>mx ){.
20ac1 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
20ac2 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e pParse, "too man
20ac3 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 25 73 22 y columns in %s"
20ac4 2c 20 7a 4f 62 6a 65 63 74 29 3b 0a 20 20 7d 0a , zObject);. }.
20ac5 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
20ac6 61 6e 20 65 6e 74 69 72 65 20 65 78 70 72 65 73 an entire expres
20ac7 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 sion list..*/.SQ
20ac8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
20ac9 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 d sqlite3ExprLis
20aca 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 tDelete(sqlite3
20acb 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 *db, ExprList *p
20acc 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a List){. int i;.
20acd 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
20ace 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 t_item *pItem;.
20acf 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 if( pList==0 )
20ad0 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 return;. assert
20ad1 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20 7c 7c ( pList->a!=0 ||
20ad2 20 28 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d (pList->nExpr==
20ad3 30 20 26 26 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 0 && pList->nAll
20ad4 6f 63 3d 3d 30 29 20 29 3b 0a 20 20 61 73 73 65 oc==0) );. asse
20ad5 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 rt( pList->nExpr
20ad6 3c 3d 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 <=pList->nAlloc
20ad7 29 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 );. for(pItem=p
20ad8 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c List->a, i=0; i<
20ad9 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b pList->nExpr; i+
20ada 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 +, pItem++){.
20adb 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 sqlite3ExprDele
20adc 74 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 45 te(db, pItem->pE
20add 78 70 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 xpr);. sqlite
20ade 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 3DbFree(db, pIte
20adf 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 m->zName);. s
20ae0 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
20ae1 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a pItem->zSpan);.
20ae2 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 }. sqlite3DbF
20ae3 72 65 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 ree(db, pList->a
20ae4 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
20ae5 65 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 7d ee(db, pList);.}
20ae6 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f ../*.** These ro
20ae7 75 74 69 6e 65 73 20 61 72 65 20 57 61 6c 6b 65 utines are Walke
20ae8 72 20 63 61 6c 6c 62 61 63 6b 73 2e 20 20 57 61 r callbacks. Wa
20ae9 6c 6b 65 72 2e 75 2e 70 69 20 69 73 20 61 20 70 lker.u.pi is a p
20aea 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 ointer.** to an
20aeb 69 6e 74 65 67 65 72 2e 20 20 54 68 65 73 65 20 integer. These
20aec 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 68 65 routines are che
20aed 63 6b 69 6e 67 20 61 6e 20 65 78 70 72 65 73 73 cking an express
20aee 69 6f 6e 20 74 6f 20 73 65 65 0a 2a 2a 20 69 66 ion to see.** if
20aef 20 69 74 20 69 73 20 61 20 63 6f 6e 73 74 61 6e it is a constan
20af0 74 2e 20 20 53 65 74 20 2a 57 61 6c 6b 65 72 2e t. Set *Walker.
20af1 75 2e 70 69 20 74 6f 20 30 20 69 66 20 74 68 65 u.pi to 0 if the
20af2 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 0a 2a expression is.*
20af3 2a 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 2e 0a * not constant..
20af4 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c 6c **.** These call
20af5 62 61 63 6b 20 72 6f 75 74 69 6e 65 73 20 61 72 back routines ar
20af6 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d e used to implem
20af7 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e ent the followin
20af8 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c g:.**.** sql
20af9 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 ite3ExprIsConsta
20afa 6e 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 nt().** sqli
20afb 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e te3ExprIsConstan
20afc 74 4e 6f 74 4a 6f 69 6e 28 29 0a 2a 2a 20 20 20 tNotJoin().**
20afd 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 sqlite3ExprIsC
20afe 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f onstantOrFunctio
20aff 6e 28 29 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 n().**.*/.static
20b00 20 69 6e 74 20 65 78 70 72 4e 6f 64 65 49 73 43 int exprNodeIsC
20b01 6f 6e 73 74 61 6e 74 28 57 61 6c 6b 65 72 20 2a onstant(Walker *
20b02 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 pWalker, Expr *p
20b03 45 78 70 72 29 7b 0a 0a 20 20 2f 2a 20 49 66 20 Expr){.. /* If
20b04 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 69 73 20 pWalker->u.i is
20b05 33 20 74 68 65 6e 20 61 6e 79 20 74 65 72 6d 20 3 then any term
20b06 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f of the expressio
20b07 6e 20 74 68 61 74 20 63 6f 6d 65 73 20 66 72 6f n that comes fro
20b08 6d 0a 20 20 2a 2a 20 74 68 65 20 4f 4e 20 6f 72 m. ** the ON or
20b09 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f USING clauses o
20b0a 66 20 61 20 6a 6f 69 6e 20 64 69 73 71 75 61 6c f a join disqual
20b0b 69 66 69 65 73 20 74 68 65 20 65 78 70 72 65 73 ifies the expres
20b0c 73 69 6f 6e 0a 20 20 2a 2a 20 66 72 6f 6d 20 62 sion. ** from b
20b0d 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 65 64 20 eing considered
20b0e 63 6f 6e 73 74 61 6e 74 2e 20 2a 2f 0a 20 20 69 constant. */. i
20b0f 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d f( pWalker->u.i=
20b10 3d 33 20 26 26 20 45 78 70 72 48 61 73 41 6e 79 =3 && ExprHasAny
20b11 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
20b12 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a EP_FromJoin) ){.
20b13 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 pWalker->u.i
20b14 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e = 0;. return
20b15 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a WRC_Abort;. }.
20b16 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 . switch( pExpr
20b17 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 ->op ){. /* C
20b18 6f 6e 73 69 64 65 72 20 66 75 6e 63 74 69 6f 6e onsider function
20b19 73 20 74 6f 20 62 65 20 63 6f 6e 73 74 61 6e 74 s to be constant
20b1a 20 69 66 20 61 6c 6c 20 74 68 65 69 72 20 61 72 if all their ar
20b1b 67 75 6d 65 6e 74 73 20 61 72 65 20 63 6f 6e 73 guments are cons
20b1c 74 61 6e 74 0a 20 20 20 20 2a 2a 20 61 6e 64 20 tant. ** and
20b1d 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 pWalker->u.i==2
20b1e 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 */. case TK_F
20b1f 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 20 20 69 UNCTION:. i
20b20 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d f( pWalker->u.i=
20b21 3d 32 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =2 ) return 0;.
20b22 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 /* Fall thr
20b23 6f 75 67 68 20 2a 2f 0a 20 20 20 20 63 61 73 65 ough */. case
20b24 20 54 4b 5f 49 44 3a 0a 20 20 20 20 63 61 73 65 TK_ID:. case
20b25 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 TK_COLUMN:.
20b26 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 case TK_AGG_FUNC
20b27 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 TION:. case T
20b28 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 K_AGG_COLUMN:.
20b29 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 testcase( pE
20b2a 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 xpr->op==TK_ID )
20b2b 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
20b2c 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ( pExpr->op==TK_
20b2d 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 COLUMN );.
20b2e 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d testcase( pExpr-
20b2f 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 >op==TK_AGG_FUNC
20b30 54 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 TION );. te
20b31 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f stcase( pExpr->o
20b32 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e p==TK_AGG_COLUMN
20b33 20 29 3b 0a 20 20 20 20 20 20 70 57 61 6c 6b 65 );. pWalke
20b34 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20 r->u.i = 0;.
20b35 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f return WRC_Abo
20b36 72 74 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a rt;. default:
20b37 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
20b38 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 pExpr->op==TK_S
20b39 45 4c 45 43 54 20 29 3b 20 2f 2a 20 73 65 6c 65 ELECT ); /* sele
20b3a 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 ctNodeIsConstant
20b3b 20 77 69 6c 6c 20 64 69 73 61 6c 6c 6f 77 20 2a will disallow *
20b3c 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 /. testcase
20b3d 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ( pExpr->op==TK_
20b3e 45 58 49 53 54 53 20 29 3b 20 2f 2a 20 73 65 6c EXISTS ); /* sel
20b3f 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e ectNodeIsConstan
20b40 74 20 77 69 6c 6c 20 64 69 73 61 6c 6c 6f 77 20 t will disallow
20b41 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 */. return
20b42 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 WRC_Continue;.
20b43 7d 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 }.}.static int s
20b44 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 electNodeIsConst
20b45 61 6e 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c ant(Walker *pWal
20b46 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 4e 6f 74 ker, Select *Not
20b47 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f Used){. UNUSED_
20b48 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
20b49 64 29 3b 0a 20 20 70 57 61 6c 6b 65 72 2d 3e 75 d);. pWalker->u
20b4a 2e 69 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e .i = 0;. return
20b4b 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74 WRC_Abort;.}.st
20b4c 61 74 69 63 20 69 6e 74 20 65 78 70 72 49 73 43 atic int exprIsC
20b4d 6f 6e 73 74 28 45 78 70 72 20 2a 70 2c 20 69 6e onst(Expr *p, in
20b4e 74 20 69 6e 69 74 46 6c 61 67 29 7b 0a 20 20 57 t initFlag){. W
20b4f 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 75 2e 69 alker w;. w.u.i
20b50 20 3d 20 69 6e 69 74 46 6c 61 67 3b 0a 20 20 77 = initFlag;. w
20b51 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d .xExprCallback =
20b52 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74 exprNodeIsConst
20b53 61 6e 74 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 ant;. w.xSelect
20b54 43 61 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63 Callback = selec
20b55 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b tNodeIsConstant;
20b56 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 . sqlite3WalkEx
20b57 70 72 28 26 77 2c 20 70 29 3b 0a 20 20 72 65 74 pr(&w, p);. ret
20b58 75 72 6e 20 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a urn w.u.i;.}../*
20b59 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 .** Walk an expr
20b5a 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 ession tree. Re
20b5b 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 turn 1 if the ex
20b5c 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 pression is cons
20b5d 74 61 6e 74 0a 2a 2a 20 61 6e 64 20 30 20 69 66 tant.** and 0 if
20b5e 20 69 74 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 it involves var
20b5f 69 61 62 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 iables or functi
20b60 6f 6e 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 on calls..**.**
20b61 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 For the purposes
20b62 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f of this functio
20b63 6e 2c 20 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 n, a double-quot
20b64 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 22 ed string (ex: "
20b65 61 62 63 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 abc").** is cons
20b66 69 64 65 72 65 64 20 61 20 76 61 72 69 61 62 6c idered a variabl
20b67 65 20 62 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 e but a single-q
20b68 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65 78 uoted string (ex
20b69 3a 20 27 61 62 63 27 29 20 69 73 0a 2a 2a 20 61 : 'abc') is.** a
20b6a 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 constant..*/.SQ
20b6b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
20b6c 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f sqlite3ExprIsCo
20b6d 6e 73 74 61 6e 74 28 45 78 70 72 20 2a 70 29 7b nstant(Expr *p){
20b6e 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 . return exprIs
20b6f 43 6f 6e 73 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a Const(p, 1);.}..
20b70 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 /*.** Walk an ex
20b71 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 pression tree.
20b72 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 Return 1 if the
20b73 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f expression is co
20b74 6e 73 74 61 6e 74 0a 2a 2a 20 74 68 61 74 20 64 nstant.** that d
20b75 6f 65 73 20 6e 6f 20 6f 72 69 67 69 6e 61 74 65 oes no originate
20b76 20 66 72 6f 6d 20 74 68 65 20 4f 4e 20 6f 72 20 from the ON or
20b77 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 USING clauses of
20b78 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75 a join..** Retu
20b79 72 6e 20 30 20 69 66 20 69 74 20 69 6e 76 6f 6c rn 0 if it invol
20b7a 76 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f 72 ves variables or
20b7b 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 function calls
20b7c 6f 72 20 74 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a or terms from.**
20b7d 20 61 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 an ON or USING
20b7e 63 6c 61 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 clause..*/.SQLIT
20b7f 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
20b80 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
20b81 61 6e 74 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 20 antNotJoin(Expr
20b82 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 *p){. return ex
20b83 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 33 29 3b prIsConst(p, 3);
20b84 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 .}../*.** Walk a
20b85 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 n expression tre
20b86 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 e. Return 1 if
20b87 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
20b88 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72 s constant.** or
20b89 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c a function call
20b8a 20 77 69 74 68 20 63 6f 6e 73 74 61 6e 74 20 61 with constant a
20b8b 72 67 75 6d 65 6e 74 73 2e 20 20 52 65 74 75 72 rguments. Retur
20b8c 6e 20 61 6e 64 20 30 20 69 66 20 74 68 65 72 65 n and 0 if there
20b8d 0a 2a 2a 20 61 72 65 20 61 6e 79 20 76 61 72 69 .** are any vari
20b8e 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 ables..**.** For
20b8f 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 the purposes of
20b90 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 this function,
20b91 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 a double-quoted
20b92 73 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63 string (ex: "abc
20b93 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 ").** is conside
20b94 72 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 62 red a variable b
20b95 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 ut a single-quot
20b96 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27 ed string (ex: '
20b97 61 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f abc') is.** a co
20b98 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 nstant..*/.SQLIT
20b99 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
20b9a 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
20b9b 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78 antOrFunction(Ex
20b9c 70 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e pr *p){. return
20b9d 20 65 78 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 exprIsConst(p,
20b9e 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 2);.}../*.** If
20b9f 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 the expression p
20ba0 20 63 6f 64 65 73 20 61 20 63 6f 6e 73 74 61 6e codes a constan
20ba1 74 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 t integer that i
20ba2 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a s small enough.*
20ba3 2a 20 74 6f 20 66 69 74 20 69 6e 20 61 20 33 32 * to fit in a 32
20ba4 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20 72 65 -bit integer, re
20ba5 74 75 72 6e 20 31 20 61 6e 64 20 70 75 74 20 74 turn 1 and put t
20ba6 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
20ba7 69 6e 74 65 67 65 72 0a 2a 2a 20 69 6e 20 2a 70 integer.** in *p
20ba8 56 61 6c 75 65 2e 20 20 49 66 20 74 68 65 20 65 Value. If the e
20ba9 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74 xpression is not
20baa 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 69 an integer or i
20bab 66 20 69 74 20 69 73 20 74 6f 6f 20 62 69 67 0a f it is too big.
20bac 2a 2a 20 74 6f 20 66 69 74 20 69 6e 20 61 20 73 ** to fit in a s
20bad 69 67 6e 65 64 20 33 32 2d 62 69 74 20 69 6e 74 igned 32-bit int
20bae 65 67 65 72 2c 20 72 65 74 75 72 6e 20 30 20 61 eger, return 0 a
20baf 6e 64 20 6c 65 61 76 65 20 2a 70 56 61 6c 75 65 nd leave *pValue
20bb0 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 unchanged..*/.S
20bb1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
20bb2 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 t sqlite3ExprIsI
20bb3 6e 74 65 67 65 72 28 45 78 70 72 20 2a 70 2c 20 nteger(Expr *p,
20bb4 69 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 int *pValue){.
20bb5 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 69 66 int rc = 0;. if
20bb6 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f ( p->flags & EP_
20bb7 49 6e 74 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 IntValue ){.
20bb8 2a 70 56 61 6c 75 65 20 3d 20 70 2d 3e 75 2e 69 *pValue = p->u.i
20bb9 56 61 6c 75 65 3b 0a 20 20 20 20 72 65 74 75 72 Value;. retur
20bba 6e 20 31 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63 n 1;. }. switc
20bbb 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 h( p->op ){.
20bbc 63 61 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a case TK_INTEGER:
20bbd 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 {. rc = sq
20bbe 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 70 2d lite3GetInt32(p-
20bbf 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 70 56 61 6c 75 >u.zToken, pValu
20bc0 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 e);. assert
20bc1 28 20 72 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ( rc==0 );.
20bc2 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
20bc3 20 20 63 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a case TK_UPLUS:
20bc4 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 {. rc = sq
20bc5 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 lite3ExprIsInteg
20bc6 65 72 28 70 2d 3e 70 4c 65 66 74 2c 20 70 56 61 er(p->pLeft, pVa
20bc7 6c 75 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 lue);. brea
20bc8 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
20bc9 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 e TK_UMINUS: {.
20bca 20 20 20 20 20 69 6e 74 20 76 3b 0a 20 20 20 20 int v;.
20bcb 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 if( sqlite3Exp
20bcc 72 49 73 49 6e 74 65 67 65 72 28 70 2d 3e 70 4c rIsInteger(p->pL
20bcd 65 66 74 2c 20 26 76 29 20 29 7b 0a 20 20 20 20 eft, &v) ){.
20bce 20 20 20 20 2a 70 56 61 6c 75 65 20 3d 20 2d 76 *pValue = -v
20bcf 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 31 ;. rc = 1
20bd0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
20bd1 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
20bd2 20 64 65 66 61 75 6c 74 3a 20 62 72 65 61 6b 3b default: break;
20bd3 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29 7b . }. if( rc ){
20bd4 0a 20 20 20 20 61 73 73 65 72 74 28 20 45 78 70 . assert( Exp
20bd5 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 rHasAnyProperty(
20bd6 70 2c 20 45 50 5f 52 65 64 75 63 65 64 7c 45 50 p, EP_Reduced|EP
20bd7 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 0a 20 20 20 20 _TokenOnly).
20bd8 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70 || (p
20bd9 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f 4d ->flags2 & EP2_M
20bda 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d 30 allocedToken)==0
20bdb 20 29 3b 0a 20 20 20 20 70 2d 3e 6f 70 20 3d 20 );. p->op =
20bdc 54 4b 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 TK_INTEGER;.
20bdd 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 p->flags |= EP_I
20bde 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20 70 2d 3e ntValue;. p->
20bdf 75 2e 69 56 61 6c 75 65 20 3d 20 2a 70 56 61 6c u.iValue = *pVal
20be0 75 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ue;. }. return
20be1 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
20be2 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 turn TRUE if the
20be3 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 73 given string is
20be4 20 61 20 72 6f 77 2d 69 64 20 63 6f 6c 75 6d 6e a row-id column
20be5 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 name..*/.SQLITE
20be6 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
20be7 69 74 65 33 49 73 52 6f 77 69 64 28 63 6f 6e 73 ite3IsRowid(cons
20be8 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66 t char *z){. if
20be9 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 ( sqlite3StrICmp
20bea 28 7a 2c 20 22 5f 52 4f 57 49 44 5f 22 29 3d 3d (z, "_ROWID_")==
20beb 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 0 ) return 1;.
20bec 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 if( sqlite3StrIC
20bed 6d 70 28 7a 2c 20 22 52 4f 57 49 44 22 29 3d 3d mp(z, "ROWID")==
20bee 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 0 ) return 1;.
20bef 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 if( sqlite3StrIC
20bf0 6d 70 28 7a 2c 20 22 4f 49 44 22 29 3d 3d 30 20 mp(z, "OID")==0
20bf1 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 ) return 1;. re
20bf2 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
20bf3 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
20bf4 77 65 20 61 72 65 20 61 62 6c 65 20 74 6f 20 74 we are able to t
20bf5 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 6f he IN operator o
20bf6 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 61 ptimization on a
20bf7 0a 2a 2a 20 71 75 65 72 79 20 6f 66 20 74 68 65 .** query of the
20bf8 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 form.**.**
20bf9 20 20 78 20 49 4e 20 28 53 45 4c 45 43 54 20 2e x IN (SELECT .
20bfa 2e 2e 29 0a 2a 2a 0a 2a 2a 20 57 68 65 72 65 20 ..).**.** Where
20bfb 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 20 63 6c the SELECT... cl
20bfc 61 75 73 65 20 69 73 20 61 73 20 73 70 65 63 69 ause is as speci
20bfd 66 69 65 64 20 62 79 20 74 68 65 20 70 61 72 61 fied by the para
20bfe 6d 65 74 65 72 20 74 6f 20 74 68 69 73 0a 2a 2a meter to this.**
20bff 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 routine..**.**
20c00 54 68 65 20 53 65 6c 65 63 74 20 6f 62 6a 65 63 The Select objec
20c01 74 20 70 61 73 73 65 64 20 69 6e 20 68 61 73 20 t passed in has
20c02 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 72 65 already been pre
20c03 70 72 6f 63 65 73 73 65 64 20 61 6e 64 20 6e 6f processed and no
20c04 0a 2a 2a 20 65 72 72 6f 72 73 20 68 61 76 65 20 .** errors have
20c05 62 65 65 6e 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 23 been found..*/.#
20c06 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
20c07 49 54 5f 53 55 42 51 55 45 52 59 0a 73 74 61 74 IT_SUBQUERY.stat
20c08 69 63 20 69 6e 74 20 69 73 43 61 6e 64 69 64 61 ic int isCandida
20c09 74 65 46 6f 72 49 6e 4f 70 74 28 53 65 6c 65 63 teForInOpt(Selec
20c0a 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 t *p){. SrcList
20c0b 20 2a 70 53 72 63 3b 0a 20 20 45 78 70 72 4c 69 *pSrc;. ExprLi
20c0c 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 54 61 st *pEList;. Ta
20c0d 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 69 66 28 ble *pTab;. if(
20c0e 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 p==0 ) return 0
20c0f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
20c10 20 20 20 20 2f 2a 20 72 69 67 68 74 2d 68 61 6e /* right-han
20c11 64 20 73 69 64 65 20 6f 66 20 49 4e 20 69 73 20 d side of IN is
20c12 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28 20 SELECT */. if(
20c13 70 2d 3e 70 50 72 69 6f 72 20 29 20 72 65 74 75 p->pPrior ) retu
20c14 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 rn 0;
20c15 20 20 20 2f 2a 20 4e 6f 74 20 61 20 63 6f 6d 70 /* Not a comp
20c16 6f 75 6e 64 20 53 45 4c 45 43 54 20 2a 2f 0a 20 ound SELECT */.
20c17 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 if( p->selFlags
20c18 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c & (SF_Distinct|
20c19 53 46 5f 41 67 67 72 65 67 61 74 65 29 20 29 7b SF_Aggregate) ){
20c1a 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 28 . testcase( (
20c1b 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 p->selFlags & (S
20c1c 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 F_Distinct|SF_Ag
20c1d 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 44 69 gregate))==SF_Di
20c1e 73 74 69 6e 63 74 20 29 3b 0a 20 20 20 20 74 65 stinct );. te
20c1f 73 74 63 61 73 65 28 20 28 70 2d 3e 73 65 6c 46 stcase( (p->selF
20c20 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69 lags & (SF_Disti
20c21 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65 nct|SF_Aggregate
20c22 29 29 3d 3d 53 46 5f 41 67 67 72 65 67 61 74 65 ))==SF_Aggregate
20c23 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 );. return 0
20c24 3b 20 2f 2a 20 4e 6f 20 44 49 53 54 49 4e 43 54 ; /* No DISTINCT
20c25 20 6b 65 79 77 6f 72 64 20 61 6e 64 20 6e 6f 20 keyword and no
20c26 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
20c27 6f 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 ons */. }. ass
20c28 65 72 74 28 20 70 2d 3e 70 47 72 6f 75 70 42 79 ert( p->pGroupBy
20c29 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 ==0 );
20c2a 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 47 52 /* Has no GR
20c2b 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f OUP BY clause */
20c2c 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d 69 74 . if( p->pLimit
20c2d 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 ) return 0;
20c2e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 /* Has
20c2f 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 no LIMIT clause
20c30 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d */. assert( p-
20c31 3e 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 20 >pOffset==0 );
20c32 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
20c33 4e 6f 20 4c 49 4d 49 54 20 6d 65 61 6e 73 20 6e No LIMIT means n
20c34 6f 20 4f 46 46 53 45 54 20 2a 2f 0a 20 20 69 66 o OFFSET */. if
20c35 28 20 70 2d 3e 70 57 68 65 72 65 20 29 20 72 65 ( p->pWhere ) re
20c36 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 turn 0;
20c37 20 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 57 /* Has no W
20c38 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 HERE clause */.
20c39 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b pSrc = p->pSrc;
20c3a 0a 20 20 61 73 73 65 72 74 28 20 70 53 72 63 21 . assert( pSrc!
20c3b 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 53 72 63 =0 );. if( pSrc
20c3c 2d 3e 6e 53 72 63 21 3d 31 20 29 20 72 65 74 75 ->nSrc!=1 ) retu
20c3d 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f rn 0; /
20c3e 2a 20 53 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e * Single term in
20c3f 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a FROM clause */.
20c40 20 20 69 66 28 20 70 53 72 63 2d 3e 61 5b 30 5d if( pSrc->a[0]
20c41 2e 70 53 65 6c 65 63 74 20 29 20 72 65 74 75 72 .pSelect ) retur
20c42 6e 20 30 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d n 0; /* FROM
20c43 20 69 73 20 6e 6f 74 20 61 20 73 75 62 71 75 65 is not a subque
20c44 72 79 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 ry or view */.
20c45 70 54 61 62 20 3d 20 70 53 72 63 2d 3e 61 5b 30 pTab = pSrc->a[0
20c46 5d 2e 70 54 61 62 3b 0a 20 20 69 66 28 20 4e 45 ].pTab;. if( NE
20c47 56 45 52 28 70 54 61 62 3d 3d 30 29 20 29 20 72 VER(pTab==0) ) r
20c48 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65 72 eturn 0;. asser
20c49 74 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 t( pTab->pSelect
20c4a 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 ==0 );
20c4b 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 /* FROM clause
20c4c 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20 2a is not a view *
20c4d 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 /. if( IsVirtua
20c4e 6c 28 70 54 61 62 29 20 29 20 72 65 74 75 72 6e l(pTab) ) return
20c4f 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 52 0; /* FR
20c50 4f 4d 20 63 6c 61 75 73 65 20 6e 6f 74 20 61 20 OM clause not a
20c51 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f virtual table */
20c52 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 . pEList = p->p
20c53 45 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 45 4c EList;. if( pEL
20c54 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 20 ist->nExpr!=1 )
20c55 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 return 0;
20c56 2f 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e 20 69 6e /* One column in
20c57 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
20c58 2a 2f 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d */. if( pEList-
20c59 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 >a[0].pExpr->op!
20c5a 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74 =TK_COLUMN ) ret
20c5b 75 72 6e 20 30 3b 20 2f 2a 20 52 65 73 75 6c 74 urn 0; /* Result
20c5c 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a is a column */.
20c5d 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 return 1;.}.#e
20c5e 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
20c5f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a MIT_SUBQUERY */.
20c60 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
20c61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 tion is used by
20c62 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
20c63 6f 6e 20 6f 66 20 74 68 65 20 49 4e 20 28 2e 2e on of the IN (..
20c64 2e 29 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 .) operator..**
20c65 49 74 27 73 20 6a 6f 62 20 69 73 20 74 6f 20 66 It's job is to f
20c66 69 6e 64 20 6f 72 20 63 72 65 61 74 65 20 61 20 ind or create a
20c67 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 b-tree structure
20c68 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 that may be use
20c69 64 0a 2a 2a 20 65 69 74 68 65 72 20 74 6f 20 74 d.** either to t
20c6a 65 73 74 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 est for membersh
20c6b 69 70 20 6f 66 20 74 68 65 20 28 2e 2e 2e 29 20 ip of the (...)
20c6c 73 65 74 20 6f 72 20 74 6f 20 69 74 65 72 61 74 set or to iterat
20c6d 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69 74 73 e through.** its
20c6e 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 70 69 members, skippi
20c6f 6e 67 20 64 75 70 6c 69 63 61 74 65 73 2e 0a 2a ng duplicates..*
20c70 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 6f *.** The index o
20c71 66 20 74 68 65 20 63 75 72 73 6f 72 20 6f 70 65 f the cursor ope
20c72 6e 65 64 20 6f 6e 20 74 68 65 20 62 2d 74 72 65 ned on the b-tre
20c73 65 20 28 64 61 74 61 62 61 73 65 20 74 61 62 6c e (database tabl
20c74 65 2c 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 e, database inde
20c75 78 20 0a 2a 2a 20 6f 72 20 65 70 68 65 72 6d 61 x .** or epherma
20c76 6c 20 74 61 62 6c 65 29 20 69 73 20 73 74 6f 72 l table) is stor
20c77 65 64 20 69 6e 20 70 58 2d 3e 69 54 61 62 6c 65 ed in pX->iTable
20c78 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e before this fun
20c79 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 2a ction returns..*
20c7a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 * The returned v
20c7b 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e alue of this fun
20c7c 63 74 69 6f 6e 20 69 6e 64 69 63 61 74 65 73 20 ction indicates
20c7d 74 68 65 20 62 2d 74 72 65 65 20 74 79 70 65 2c the b-tree type,
20c7e 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a as follows:.**.
20c7f 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 52 4f ** IN_INDEX_RO
20c80 57 49 44 20 2d 20 54 68 65 20 63 75 72 73 6f 72 WID - The cursor
20c81 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 was opened on a
20c82 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e database table.
20c83 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 49 .** IN_INDEX_I
20c84 4e 44 45 58 20 2d 20 54 68 65 20 63 75 72 73 6f NDEX - The curso
20c85 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 r was opened on
20c86 61 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 a database index
20c87 2e 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f ..** IN_INDEX_
20c88 45 50 48 20 2d 20 20 20 54 68 65 20 63 75 72 73 EPH - The curs
20c89 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e or was opened on
20c8a 20 61 20 73 70 65 63 69 61 6c 6c 79 20 63 72 65 a specially cre
20c8b 61 74 65 64 20 61 6e 64 0a 2a 2a 20 20 20 20 20 ated and.**
20c8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
20c8d 6f 70 75 6c 61 74 65 64 20 65 70 68 65 72 65 6d opulated epherem
20c8e 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 al table..**.**
20c8f 41 6e 20 65 78 69 73 74 69 6e 67 20 62 2d 74 72 An existing b-tr
20c90 65 65 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 ee may only be u
20c91 73 65 64 20 69 66 20 74 68 65 20 53 45 4c 45 43 sed if the SELEC
20c92 54 20 69 73 20 6f 66 20 74 68 65 20 73 69 6d 70 T is of the simp
20c93 6c 65 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a le.** form:.**.*
20c94 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f * SELECT <co
20c95 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c lumn> FROM <tabl
20c96 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e>.**.** If the
20c97 70 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d prNotFound param
20c98 65 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20 eter is 0, then
20c99 74 68 65 20 62 2d 74 72 65 65 20 77 69 6c 6c 20 the b-tree will
20c9a 62 65 20 75 73 65 64 20 74 6f 20 69 74 65 72 61 be used to itera
20c9b 74 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 te.** through th
20c9c 65 20 73 65 74 20 6d 65 6d 62 65 72 73 2c 20 73 e set members, s
20c9d 6b 69 70 70 69 6e 67 20 61 6e 79 20 64 75 70 6c kipping any dupl
20c9e 69 63 61 74 65 73 2e 20 49 6e 20 74 68 69 73 20 icates. In this
20c9f 63 61 73 65 20 61 6e 0a 2a 2a 20 65 70 68 65 72 case an.** epher
20ca0 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 20 emal table must
20ca1 62 65 20 75 73 65 64 20 75 6e 6c 65 73 73 20 74 be used unless t
20ca2 68 65 20 73 65 6c 65 63 74 65 64 20 3c 63 6f 6c he selected <col
20ca3 75 6d 6e 3e 20 69 73 20 67 75 61 72 61 6e 74 65 umn> is guarante
20ca4 65 64 0a 2a 2a 20 74 6f 20 62 65 20 75 6e 69 71 ed.** to be uniq
20ca5 75 65 20 2d 20 65 69 74 68 65 72 20 62 65 63 61 ue - either beca
20ca6 75 73 65 20 69 74 20 69 73 20 61 6e 20 49 4e 54 use it is an INT
20ca7 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY
20ca8 20 6f 72 20 69 74 0a 2a 2a 20 68 61 73 20 61 20 or it.** has a
20ca9 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e UNIQUE constrain
20caa 74 20 6f 72 20 55 4e 49 51 55 45 20 69 6e 64 65 t or UNIQUE inde
20cab 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 x..**.** If the
20cac 70 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d prNotFound param
20cad 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 eter is not 0, t
20cae 68 65 6e 20 74 68 65 20 62 2d 74 72 65 65 20 77 hen the b-tree w
20caf 69 6c 6c 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 ill be used .**
20cb0 66 6f 72 20 66 61 73 74 20 73 65 74 20 6d 65 6d for fast set mem
20cb1 62 65 72 73 68 69 70 20 74 65 73 74 73 2e 20 49 bership tests. I
20cb2 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 20 65 n this case an e
20cb3 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d pheremal table m
20cb4 75 73 74 20 0a 2a 2a 20 62 65 20 75 73 65 64 20 ust .** be used
20cb5 75 6e 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20 unless <column>
20cb6 69 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 is an INTEGER PR
20cb7 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 61 6e 20 IMARY KEY or an
20cb8 69 6e 64 65 78 20 63 61 6e 20 0a 2a 2a 20 62 65 index can .** be
20cb9 20 66 6f 75 6e 64 20 77 69 74 68 20 3c 63 6f 6c found with <col
20cba 75 6d 6e 3e 20 61 73 20 69 74 73 20 6c 65 66 74 umn> as its left
20cbb 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a -most column..**
20cbc 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 62 2d 74 .** When the b-t
20cbd 72 65 65 20 69 73 20 62 65 69 6e 67 20 75 73 65 ree is being use
20cbe 64 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70 d for membership
20cbf 20 74 65 73 74 73 2c 20 74 68 65 20 63 61 6c 6c tests, the call
20cc0 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 ing function.**
20cc1 6e 65 65 64 73 20 74 6f 20 6b 6e 6f 77 20 77 68 needs to know wh
20cc2 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 ether or not the
20cc3 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 structure conta
20cc4 69 6e 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 ins an SQL NULL
20cc5 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 6f 72 64 .** value in ord
20cc6 65 72 20 74 6f 20 63 6f 72 72 65 63 74 6c 79 20 er to correctly
20cc7 65 76 61 6c 75 61 74 65 20 65 78 70 72 65 73 73 evaluate express
20cc8 69 6f 6e 73 20 6c 69 6b 65 20 22 58 20 49 4e 20 ions like "X IN
20cc9 28 59 2c 20 5a 29 22 2e 0a 2a 2a 20 49 66 20 74 (Y, Z)"..** If t
20cca 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 here is a chance
20ccb 20 74 68 61 74 20 74 68 65 20 62 2d 74 72 65 65 that the b-tree
20ccc 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 61 might contain a
20ccd 20 4e 55 4c 4c 20 76 61 6c 75 65 20 61 74 0a 2a NULL value at.*
20cce 2a 20 72 75 6e 74 69 6d 65 2c 20 74 68 65 6e 20 * runtime, then
20ccf 61 20 72 65 67 69 73 74 65 72 20 69 73 20 61 6c a register is al
20cd0 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 20 located and the
20cd1 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 register number
20cd2 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 written.** to *p
20cd3 72 4e 6f 74 46 6f 75 6e 64 2e 20 49 66 20 74 68 rNotFound. If th
20cd4 65 72 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 ere is no chance
20cd5 20 74 68 61 74 20 74 68 65 20 62 2d 74 72 65 65 that the b-tree
20cd6 20 63 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 4e contains a.** N
20cd7 55 4c 4c 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 ULL value, then
20cd8 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 69 73 20 6c *prNotFound is l
20cd9 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a eft unchanged..*
20cda 2a 0a 2a 2a 20 49 66 20 61 20 72 65 67 69 73 74 *.** If a regist
20cdb 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 er is allocated
20cdc 61 6e 64 20 69 74 73 20 6c 6f 63 61 74 69 6f 6e and its location
20cdd 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 72 4e 6f stored in *prNo
20cde 74 46 6f 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20 tFound, then.**
20cdf 69 74 73 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 its initial valu
20ce0 65 20 69 73 20 4e 55 4c 4c 2e 20 49 66 20 74 68 e is NULL. If th
20ce1 65 20 62 2d 74 72 65 65 20 64 6f 65 73 20 6e 6f e b-tree does no
20ce2 74 20 72 65 6d 61 69 6e 20 63 6f 6e 73 74 61 6e t remain constan
20ce3 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 75 72 t.** for the dur
20ce4 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 65 ation of the que
20ce5 72 79 20 28 69 2e 65 2e 20 74 68 65 20 53 45 4c ry (i.e. the SEL
20ce6 45 43 54 20 74 68 61 74 20 67 65 6e 65 72 61 74 ECT that generat
20ce7 65 73 20 74 68 65 20 62 2d 74 72 65 65 0a 2a 2a es the b-tree.**
20ce8 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74 65 64 is a correlated
20ce9 20 73 75 62 71 75 65 72 79 29 20 74 68 65 6e 20 subquery) then
20cea 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
20ceb 20 61 6c 6c 6f 63 61 74 65 64 20 72 65 67 69 73 allocated regis
20cec 74 65 72 20 69 73 0a 2a 2a 20 72 65 73 65 74 20 ter is.** reset
20ced 74 6f 20 4e 55 4c 4c 20 65 61 63 68 20 74 69 6d to NULL each tim
20cee 65 20 74 68 65 20 62 2d 74 72 65 65 20 69 73 20 e the b-tree is
20cef 72 65 70 6f 70 75 6c 61 74 65 64 2e 20 54 68 69 repopulated. Thi
20cf0 73 20 61 6c 6c 6f 77 73 20 74 68 65 0a 2a 2a 20 s allows the.**
20cf1 63 61 6c 6c 65 72 20 74 6f 20 75 73 65 20 76 64 caller to use vd
20cf2 62 65 20 63 6f 64 65 20 65 71 75 69 76 61 6c 65 be code equivale
20cf3 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 nt to the follow
20cf4 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 ing:.**.** if(
20cf5 20 72 65 67 69 73 74 65 72 3d 3d 4e 55 4c 4c 20 register==NULL
20cf6 29 7b 0a 2a 2a 20 20 20 20 20 68 61 73 5f 6e 75 ){.** has_nu
20cf7 6c 6c 20 3d 20 3c 74 65 73 74 20 69 66 20 64 61 ll = <test if da
20cf8 74 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e ta structure con
20cf9 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 20 tains null>.**
20cfa 20 20 20 72 65 67 69 73 74 65 72 20 3d 20 31 0a register = 1.
20cfb 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 69 6e 20 ** }.**.** in
20cfc 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 72 order to avoid r
20cfd 75 6e 6e 69 6e 67 20 74 68 65 20 3c 74 65 73 74 unning the <test
20cfe 20 69 66 20 64 61 74 61 20 73 74 72 75 63 74 75 if data structu
20cff 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c re contains null
20d00 3e 0a 2a 2a 20 74 65 73 74 20 6d 6f 72 65 20 6f >.** test more o
20d01 66 74 65 6e 20 74 68 61 6e 20 69 73 20 6e 65 63 ften than is nec
20d02 65 73 73 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 essary..*/.#ifnd
20d03 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
20d04 55 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f 50 UBQUERY.SQLITE_P
20d05 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
20d06 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 50 61 e3FindInIndex(Pa
20d07 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 rse *pParse, Exp
20d08 72 20 2a 70 58 2c 20 69 6e 74 20 2a 70 72 4e 6f r *pX, int *prNo
20d09 74 46 6f 75 6e 64 29 7b 0a 20 20 53 65 6c 65 63 tFound){. Selec
20d0a 74 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 t *p;
20d0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d0c 20 2f 2a 20 53 45 4c 45 43 54 20 74 6f 20 74 68 /* SELECT to th
20d0d 65 20 72 69 67 68 74 20 6f 66 20 49 4e 20 6f 70 e right of IN op
20d0e 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 erator */. int
20d0f 65 54 79 70 65 20 3d 20 30 3b 20 20 20 20 20 20 eType = 0;
20d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d11 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 52 48 53 /* Type of RHS
20d12 20 74 61 62 6c 65 2e 20 49 4e 5f 49 4e 44 45 58 table. IN_INDEX
20d13 5f 2a 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 _* */. int iTab
20d14 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b = pParse->nTab+
20d15 2b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a +; /*
20d16 20 43 75 72 73 6f 72 20 6f 66 20 74 68 65 20 52 Cursor of the R
20d17 48 53 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e HS table */. in
20d18 74 20 6d 75 73 74 42 65 55 6e 69 71 75 65 20 3d t mustBeUnique =
20d19 20 28 70 72 4e 6f 74 46 6f 75 6e 64 3d 3d 30 29 (prNotFound==0)
20d1a 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 52 ; /* True if R
20d1b 48 53 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75 HS must be uniqu
20d1c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68 65 63 6b e */.. /* Check
20d1d 20 74 6f 20 73 65 65 20 69 66 20 61 6e 20 65 78 to see if an ex
20d1e 69 73 74 69 6e 67 20 74 61 62 6c 65 20 6f 72 20 isting table or
20d1f 69 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73 65 index can be use
20d20 64 20 74 6f 0a 20 20 2a 2a 20 73 61 74 69 73 66 d to. ** satisf
20d21 79 20 74 68 65 20 71 75 65 72 79 2e 20 20 54 68 y the query. Th
20d22 69 73 20 69 73 20 70 72 65 66 65 72 61 62 6c 65 is is preferable
20d23 20 74 6f 20 67 65 6e 65 72 61 74 69 6e 67 20 61 to generating a
20d24 20 6e 65 77 20 0a 20 20 2a 2a 20 65 70 68 65 6d new . ** ephem
20d25 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f eral table.. */
20d26 0a 20 20 70 20 3d 20 28 45 78 70 72 48 61 73 50 . p = (ExprHasP
20d27 72 6f 70 65 72 74 79 28 70 58 2c 20 45 50 5f 78 roperty(pX, EP_x
20d28 49 73 53 65 6c 65 63 74 29 20 3f 20 70 58 2d 3e IsSelect) ? pX->
20d29 78 2e 70 53 65 6c 65 63 74 20 3a 20 30 29 3b 0a x.pSelect : 0);.
20d2a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 50 61 if( ALWAYS(pPa
20d2b 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 29 20 26 26 rse->nErr==0) &&
20d2c 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72 49 isCandidateForI
20d2d 6e 4f 70 74 28 70 29 20 29 7b 0a 20 20 20 20 73 nOpt(p) ){. s
20d2e 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
20d2f 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 rse->db;
20d30 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
20d31 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
20d32 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 Expr *pExpr
20d33 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 = p->pEList->a[0
20d34 5d 2e 70 45 78 70 72 3b 20 20 20 2f 2a 20 45 78 ].pExpr; /* Ex
20d35 70 72 65 73 73 69 6f 6e 20 3c 63 6f 6c 75 6d 6e pression <column
20d36 3e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f > */. int iCo
20d37 6c 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 l = pExpr->iColu
20d38 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 mn;
20d39 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 /* Index of colu
20d3a 6d 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a 20 mn <column> */.
20d3b 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c Vdbe *v = sql
20d3c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 ite3GetVdbe(pPar
20d3d 73 65 29 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 se); /* Vir
20d3e 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 62 65 69 tual machine bei
20d3f 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 20 20 ng coded */.
20d40 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 2d Table *pTab = p-
20d41 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 >pSrc->a[0].pTab
20d42 3b 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 ; /* Table
20d43 3c 74 61 62 6c 65 3e 2e 20 2a 2f 0a 20 20 20 20 <table>. */.
20d44 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 int iDb;
20d45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d46 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
20d47 73 65 20 69 64 78 20 66 6f 72 20 70 54 61 62 20 se idx for pTab
20d48 2a 2f 0a 20 20 20 0a 20 20 20 20 2f 2a 20 43 6f */. . /* Co
20d49 64 65 20 61 6e 20 4f 50 5f 56 65 72 69 66 79 43 de an OP_VerifyC
20d4a 6f 6f 6b 69 65 20 61 6e 64 20 4f 50 5f 54 61 62 ookie and OP_Tab
20d4b 6c 65 4c 6f 63 6b 20 66 6f 72 20 3c 74 61 62 6c leLock for <tabl
20d4c 65 3e 2e 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d e>. */. iDb =
20d4d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f sqlite3SchemaTo
20d4e 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e Index(db, pTab->
20d4f 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 73 71 pSchema);. sq
20d50 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 lite3CodeVerifyS
20d51 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 chema(pParse, iD
20d52 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 b);. sqlite3T
20d53 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c ableLock(pParse,
20d54 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d iDb, pTab->tnum
20d55 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 , 0, pTab->zName
20d56 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 );.. /* This
20d57 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 function is only
20d58 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 77 6f called from two
20d59 20 70 6c 61 63 65 73 2e 20 49 6e 20 62 6f 74 68 places. In both
20d5a 20 63 61 73 65 73 20 74 68 65 20 76 64 62 65 0a cases the vdbe.
20d5b 20 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 61 ** has alrea
20d5c 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 dy been allocate
20d5d 64 2e 20 53 6f 20 61 73 73 75 6d 65 20 73 71 6c d. So assume sql
20d5e 69 74 65 33 47 65 74 56 64 62 65 28 29 20 69 73 ite3GetVdbe() is
20d5f 20 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 73 always. ** s
20d60 75 63 63 65 73 73 66 75 6c 20 68 65 72 65 2e 0a uccessful here..
20d61 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 */. asser
20d62 74 28 76 29 3b 0a 20 20 20 20 69 66 28 20 69 43 t(v);. if( iC
20d63 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 69 6e ol<0 ){. in
20d64 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 t iMem = ++pPars
20d65 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 69 e->nMem;. i
20d66 6e 74 20 69 41 64 64 72 3b 0a 0a 20 20 20 20 20 nt iAddr;..
20d67 20 69 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33 iAddr = sqlite3
20d68 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
20d69 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 _If, iMem);.
20d6a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
20d6b 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 Op2(v, OP_Intege
20d6c 72 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 r, 1, iMem);..
20d6d 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 sqlite3OpenT
20d6e 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61 able(pParse, iTa
20d6f 62 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 b, iDb, pTab, OP
20d70 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 20 20 _OpenRead);.
20d71 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 eType = IN_IND
20d72 45 58 5f 52 4f 57 49 44 3b 0a 0a 20 20 20 20 20 EX_ROWID;..
20d73 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 sqlite3VdbeJump
20d74 48 65 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a Here(v, iAddr);.
20d75 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
20d76 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 Index *pIdx;
20d77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d78 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f /* Iterato
20d79 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 r variable */..
20d7a 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c /* The coll
20d7b 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 75 ation sequence u
20d7c 73 65 64 20 62 79 20 74 68 65 20 63 6f 6d 70 61 sed by the compa
20d7d 72 69 73 6f 6e 2e 20 49 66 20 61 6e 20 69 6e 64 rison. If an ind
20d7e 65 78 20 69 73 20 74 6f 0a 20 20 20 20 20 20 2a ex is to. *
20d7f 2a 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61 * be used in pla
20d80 63 65 20 6f 66 20 61 20 74 65 6d 70 2d 74 61 62 ce of a temp-tab
20d81 6c 65 2c 20 69 74 20 6d 75 73 74 20 62 65 20 6f le, it must be o
20d82 72 64 65 72 65 64 20 61 63 63 6f 72 64 69 6e 67 rdered according
20d83 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 69 . ** to thi
20d84 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 s collation sequ
20d85 65 6e 63 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 ence. */.
20d86 43 6f 6c 6c 53 65 71 20 2a 70 52 65 71 20 3d 20 CollSeq *pReq =
20d87 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d sqlite3BinaryCom
20d88 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 pareCollSeq(pPar
20d89 73 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 se, pX->pLeft, p
20d8a 45 78 70 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a Expr);.. /*
20d8b 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 Check that the
20d8c 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 77 69 affinity that wi
20d8d 6c 6c 20 62 65 20 75 73 65 64 20 74 6f 20 70 65 ll be used to pe
20d8e 72 66 6f 72 6d 20 74 68 65 20 0a 20 20 20 20 20 rform the .
20d8f 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 ** comparison i
20d90 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 s the same as th
20d91 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 e affinity of th
20d92 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 0a 20 20 20 e column. If.
20d93 20 20 20 2a 2a 20 69 74 20 69 73 20 6e 6f 74 2c ** it is not,
20d94 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 it is not possi
20d95 62 6c 65 20 74 6f 20 75 73 65 20 61 6e 79 20 69 ble to use any i
20d96 6e 64 65 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ndex.. */.
20d97 20 20 20 20 20 63 68 61 72 20 61 66 66 20 3d 20 char aff =
20d98 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 comparisonAffini
20d99 74 79 28 70 58 29 3b 0a 20 20 20 20 20 20 69 6e ty(pX);. in
20d9a 74 20 61 66 66 69 6e 69 74 79 5f 6f 6b 20 3d 20 t affinity_ok =
20d9b 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c (pTab->aCol[iCol
20d9c 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 61 66 66 7c ].affinity==aff|
20d9d 7c 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 |aff==SQLITE_AFF
20d9e 5f 4e 4f 4e 45 29 3b 0a 0a 20 20 20 20 20 20 66 _NONE);.. f
20d9f 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 or(pIdx=pTab->pI
20da0 6e 64 65 78 3b 20 70 49 64 78 20 26 26 20 65 54 ndex; pIdx && eT
20da1 79 70 65 3d 3d 30 20 26 26 20 61 66 66 69 6e 69 ype==0 && affini
20da2 74 79 5f 6f 6b 3b 20 70 49 64 78 3d 70 49 64 78 ty_ok; pIdx=pIdx
20da3 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 ->pNext){.
20da4 20 20 69 66 28 20 28 70 49 64 78 2d 3e 61 69 43 if( (pIdx->aiC
20da5 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 43 6f 6c 29 0a olumn[0]==iCol).
20da6 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 && sqli
20da7 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 te3FindCollSeq(d
20da8 62 2c 20 45 4e 43 28 64 62 29 2c 20 70 49 64 78 b, ENC(db), pIdx
20da9 2d 3e 61 7a 43 6f 6c 6c 5b 30 5d 2c 20 30 29 3d ->azColl[0], 0)=
20daa 3d 70 52 65 71 0a 20 20 20 20 20 20 20 20 20 26 =pReq. &
20dab 26 20 28 21 6d 75 73 74 42 65 55 6e 69 71 75 65 & (!mustBeUnique
20dac 20 7c 7c 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 || (pIdx->nColu
20dad 6d 6e 3d 3d 31 20 26 26 20 70 49 64 78 2d 3e 6f mn==1 && pIdx->o
20dae 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 29 nError!=OE_None)
20daf 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 ). ){.
20db0 20 20 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 int iMem
20db1 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d = ++pParse->nMem
20db2 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 ;. int
20db3 69 41 64 64 72 3b 0a 20 20 20 20 20 20 20 20 20 iAddr;.
20db4 20 63 68 61 72 20 2a 70 4b 65 79 3b 0a 20 20 0a char *pKey;. .
20db5 20 20 20 20 20 20 20 20 20 20 70 4b 65 79 20 3d pKey =
20db6 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 (char *)sqlite3
20db7 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 IndexKeyinfo(pPa
20db8 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 rse, pIdx);.
20db9 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 71 iAddr = sq
20dba 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
20dbb 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 3b v, OP_If, iMem);
20dbc 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
20dbd 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
20dbe 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 OP_Integer, 1, i
20dbf 4d 65 6d 29 3b 0a 20 20 0a 20 20 20 20 20 20 20 Mem);. .
20dc0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
20dc1 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 dOp4(v, OP_OpenR
20dc2 65 61 64 2c 20 69 54 61 62 2c 20 70 49 64 78 2d ead, iTab, pIdx-
20dc3 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 >tnum, iDb,.
20dc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20dc5 20 20 20 20 20 20 20 20 20 20 20 70 4b 65 79 2c pKey,
20dc6 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f P4_KEYINFO_HANDO
20dc7 46 46 29 3b 0a 20 20 20 20 20 20 20 20 20 20 56 FF);. V
20dc8 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 dbeComment((v, "
20dc9 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 %s", pIdx->zName
20dca 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65 54 ));. eT
20dcb 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 49 ype = IN_INDEX_I
20dcc 4e 44 45 58 3b 0a 0a 20 20 20 20 20 20 20 20 20 NDEX;..
20dcd 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 sqlite3VdbeJump
20dce 48 65 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a Here(v, iAddr);.
20dcf 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 72 if( pr
20dd0 4e 6f 74 46 6f 75 6e 64 20 26 26 20 21 70 54 61 NotFound && !pTa
20dd1 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 6e 6f b->aCol[iCol].no
20dd2 74 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 tNull ){.
20dd3 20 20 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 *prNotFound
20dd4 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 = ++pParse->nMe
20dd5 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 m;. }.
20dd6 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
20dd7 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 . }. }.. if
20dd8 28 20 65 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 ( eType==0 ){.
20dd9 20 20 2f 2a 20 43 6f 75 6c 64 20 6e 6f 74 20 66 /* Could not f
20dda 6f 75 6e 64 20 61 6e 20 65 78 69 73 74 69 6e 67 ound an existing
20ddb 20 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 74 able or index t
20ddc 6f 20 75 73 65 20 61 73 20 74 68 65 20 52 48 53 o use as the RHS
20ddd 20 62 2d 74 72 65 65 2e 0a 20 20 20 20 2a 2a 20 b-tree.. **
20dde 57 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f 20 We will have to
20ddf 67 65 6e 65 72 61 74 65 20 61 6e 20 65 70 68 65 generate an ephe
20de0 6d 65 72 61 6c 20 74 61 62 6c 65 20 74 6f 20 64 meral table to d
20de1 6f 20 74 68 65 20 6a 6f 62 2e 0a 20 20 20 20 2a o the job.. *
20de2 2f 0a 20 20 20 20 69 6e 74 20 72 4d 61 79 48 61 /. int rMayHa
20de3 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 veNull = 0;.
20de4 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 eType = IN_INDEX
20de5 5f 45 50 48 3b 0a 20 20 20 20 69 66 28 20 70 72 _EPH;. if( pr
20de6 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20 20 20 NotFound ){.
20de7 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 *prNotFound =
20de8 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 2b rMayHaveNull = +
20de9 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 +pParse->nMem;.
20dea 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 58 2d }else if( pX-
20deb 3e 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3c >pLeft->iColumn<
20dec 30 20 26 26 20 21 45 78 70 72 48 61 73 41 6e 79 0 && !ExprHasAny
20ded 50 72 6f 70 65 72 74 79 28 70 58 2c 20 45 50 5f Property(pX, EP_
20dee 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 xIsSelect) ){.
20def 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 eType = IN_I
20df0 4e 44 45 58 5f 52 4f 57 49 44 3b 0a 20 20 20 20 NDEX_ROWID;.
20df1 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 }. sqlite3Cod
20df2 65 53 75 62 73 65 6c 65 63 74 28 70 50 61 72 73 eSubselect(pPars
20df3 65 2c 20 70 58 2c 20 72 4d 61 79 48 61 76 65 4e e, pX, rMayHaveN
20df4 75 6c 6c 2c 20 65 54 79 70 65 3d 3d 49 4e 5f 49 ull, eType==IN_I
20df5 4e 44 45 58 5f 52 4f 57 49 44 29 3b 0a 20 20 7d NDEX_ROWID);. }
20df6 65 6c 73 65 7b 0a 20 20 20 20 70 58 2d 3e 69 54 else{. pX->iT
20df7 61 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 7d able = iTab;. }
20df8 0a 20 20 72 65 74 75 72 6e 20 65 54 79 70 65 3b . return eType;
20df9 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
20dfa 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 Generate code f
20dfb 6f 72 20 73 63 61 6c 61 72 20 73 75 62 71 75 65 or scalar subque
20dfc 72 69 65 73 20 75 73 65 64 20 61 73 20 61 6e 20 ries used as an
20dfd 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 6e expression.** an
20dfe 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 2e 20 d IN operators.
20dff 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a Examples:.**.**
20e00 20 20 20 20 20 28 53 45 4c 45 43 54 20 61 20 46 (SELECT a F
20e01 52 4f 4d 20 62 29 20 20 20 20 20 20 20 20 20 20 ROM b)
20e02 2d 2d 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 -- subquery.**
20e03 20 20 20 45 58 49 53 54 53 20 28 53 45 4c 45 43 EXISTS (SELEC
20e04 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 2d 2d T a FROM b) --
20e05 20 45 58 49 53 54 53 20 73 75 62 71 75 65 72 79 EXISTS subquery
20e06 0a 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 34 2c .** x IN (4,
20e07 35 2c 31 31 29 20 20 20 20 20 20 20 20 20 20 20 5,11)
20e08 20 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f -- IN operato
20e09 72 20 77 69 74 68 20 6c 69 73 74 20 6f 6e 20 72 r with list on r
20e0a 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 0a 2a ight-hand side.*
20e0b 2a 20 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45 * x IN (SELE
20e0c 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 20 CT a FROM b)
20e0d 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 -- IN operator
20e0e 77 69 74 68 20 73 75 62 71 75 65 72 79 20 6f 6e with subquery on
20e0f 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 0a 2a 2a the right.**.**
20e10 20 54 68 65 20 70 45 78 70 72 20 70 61 72 61 6d The pExpr param
20e11 65 74 65 72 20 64 65 73 63 72 69 62 65 73 20 74 eter describes t
20e12 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 he expression th
20e13 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 at contains the
20e14 49 4e 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 6f IN.** operator o
20e15 72 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a r subquery..**.*
20e16 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 69 * If parameter i
20e17 73 52 6f 77 69 64 20 69 73 20 6e 6f 6e 2d 7a 65 sRowid is non-ze
20e18 72 6f 2c 20 74 68 65 6e 20 65 78 70 72 65 73 73 ro, then express
20e19 69 6f 6e 20 70 45 78 70 72 20 69 73 20 67 75 61 ion pExpr is gua
20e1a 72 61 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 62 65 ranteed.** to be
20e1b 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 72 of the form "<r
20e1c 6f 77 69 64 3e 20 49 4e 20 28 3f 2c 20 3f 2c 20 owid> IN (?, ?,
20e1d 3f 29 22 2c 20 77 68 65 72 65 20 3c 72 6f 77 69 ?)", where <rowi
20e1e 64 3e 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 d> is a referenc
20e1f 65 0a 2a 2a 20 74 6f 20 73 6f 6d 65 20 69 6e 74 e.** to some int
20e20 65 67 65 72 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 eger key column
20e21 6f 66 20 61 20 74 61 62 6c 65 20 42 2d 54 72 65 of a table B-Tre
20e22 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c e. In this case,
20e23 20 75 73 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65 use an.** intke
20e24 79 20 42 2d 54 72 65 65 20 74 6f 20 73 74 6f 72 y B-Tree to stor
20e25 65 20 74 68 65 20 73 65 74 20 6f 66 20 49 4e 28 e the set of IN(
20e26 2e 2e 2e 29 20 76 61 6c 75 65 73 20 69 6e 73 74 ...) values inst
20e27 65 61 64 20 6f 66 20 74 68 65 20 75 73 75 61 6c ead of the usual
20e28 0a 2a 2a 20 28 73 6c 6f 77 65 72 29 20 76 61 72 .** (slower) var
20e29 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 6b 65 79 iable length key
20e2a 73 20 42 2d 54 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 s B-Tree..**.**
20e2b 49 66 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 If rMayHaveNull
20e2c 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 61 is non-zero, tha
20e2d 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 t means that the
20e2e 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e operation is an
20e2f 20 49 4e 0a 2a 2a 20 28 6e 6f 74 20 61 20 53 45 IN.** (not a SE
20e30 4c 45 43 54 20 6f 72 20 45 58 49 53 54 53 29 20 LECT or EXISTS)
20e31 61 6e 64 20 74 68 61 74 20 74 68 65 20 52 48 53 and that the RHS
20e32 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 73 20 might contains
20e33 4e 55 4c 4c 73 2e 0a 2a 2a 20 46 75 72 74 68 65 NULLs..** Furthe
20e34 72 6d 6f 72 65 2c 20 74 68 65 20 49 4e 20 69 73 rmore, the IN is
20e35 20 69 6e 20 61 20 57 48 45 52 45 20 63 6c 61 75 in a WHERE clau
20e36 73 65 20 61 6e 64 20 74 68 61 74 20 77 65 20 72 se and that we r
20e37 65 61 6c 6c 79 20 77 61 6e 74 0a 2a 2a 20 74 6f eally want.** to
20e38 20 69 74 65 72 61 74 65 20 6f 76 65 72 20 74 68 iterate over th
20e39 65 20 52 48 53 20 6f 66 20 74 68 65 20 49 4e 20 e RHS of the IN
20e3a 6f 70 65 72 61 74 6f 72 20 69 6e 20 6f 72 64 65 operator in orde
20e3b 72 20 74 6f 20 71 75 69 63 6b 6c 79 20 6c 6f 63 r to quickly loc
20e3c 61 74 65 0a 2a 2a 20 61 6c 6c 20 63 6f 72 72 65 ate.** all corre
20e3d 73 70 6f 6e 64 69 6e 67 20 4c 48 53 20 65 6c 65 sponding LHS ele
20e3e 6d 65 6e 74 73 2e 20 20 41 6c 6c 20 74 68 69 73 ments. All this
20e3f 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 69 73 routine does is
20e40 20 69 6e 69 74 69 61 6c 69 7a 65 0a 2a 2a 20 74 initialize.** t
20e41 68 65 20 72 65 67 69 73 74 65 72 20 67 69 76 65 he register give
20e42 6e 20 62 79 20 72 4d 61 79 48 61 76 65 4e 75 6c n by rMayHaveNul
20e43 6c 20 74 6f 20 4e 55 4c 4c 2e 20 20 43 61 6c 6c l to NULL. Call
20e44 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 77 69 6c ing routines wil
20e45 6c 20 74 61 6b 65 0a 2a 2a 20 63 61 72 65 20 6f l take.** care o
20e46 66 20 63 68 61 6e 67 69 6e 67 20 74 68 69 73 20 f changing this
20e47 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 20 74 register value t
20e48 6f 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 66 20 74 68 o non-NULL if th
20e49 65 20 52 48 53 20 69 73 20 4e 55 4c 4c 2d 66 72 e RHS is NULL-fr
20e4a 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 4d 61 ee..**.** If rMa
20e4b 79 48 61 76 65 4e 75 6c 6c 20 69 73 20 7a 65 72 yHaveNull is zer
20e4c 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 o, that means th
20e4d 61 74 20 74 68 65 20 73 75 62 71 75 65 72 79 20 at the subquery
20e4e 69 73 20 62 65 69 6e 67 20 75 73 65 64 0a 2a 2a is being used.**
20e4f 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70 20 for membership
20e50 74 65 73 74 69 6e 67 20 6f 6e 6c 79 2e 20 20 54 testing only. T
20e51 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 here is no need
20e52 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 6e to initialize an
20e53 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 74 y.** registers t
20e54 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 70 o indicate the p
20e55 72 65 73 65 6e 73 65 20 6f 72 20 61 62 73 65 6e resense or absen
20e56 63 65 20 6f 66 20 4e 55 4c 4c 73 20 6f 6e 20 74 ce of NULLs on t
20e57 68 65 20 52 48 53 2e 0a 2a 2f 0a 23 69 66 6e 64 he RHS..*/.#ifnd
20e58 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
20e59 55 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f 50 UBQUERY.SQLITE_P
20e5a 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
20e5b 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 te3CodeSubselect
20e5c 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
20e5d 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 e, /* P
20e5e 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a arsing context *
20e5f 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c /. Expr *pExpr,
20e60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
20e61 68 65 20 49 4e 2c 20 53 45 4c 45 43 54 2c 20 6f he IN, SELECT, o
20e62 72 20 45 58 49 53 54 53 20 6f 70 65 72 61 74 6f r EXISTS operato
20e63 72 20 2a 2f 0a 20 20 69 6e 74 20 72 4d 61 79 48 r */. int rMayH
20e64 61 76 65 4e 75 6c 6c 2c 20 20 20 20 20 20 20 2f aveNull, /
20e65 2a 20 52 65 67 69 73 74 65 72 20 74 68 61 74 20 * Register that
20e66 72 65 63 6f 72 64 73 20 77 68 65 74 68 65 72 20 records whether
20e67 4e 55 4c 4c 73 20 65 78 69 73 74 20 69 6e 20 52 NULLs exist in R
20e68 48 53 20 2a 2f 0a 20 20 69 6e 74 20 69 73 52 6f HS */. int isRo
20e69 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20 20 wid
20e6a 2f 2a 20 49 66 20 74 72 75 65 2c 20 4c 48 53 20 /* If true, LHS
20e6b 6f 66 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 69 of IN operator i
20e6c 73 20 61 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a s a rowid */.){.
20e6d 20 20 69 6e 74 20 74 65 73 74 41 64 64 72 20 3d int testAddr =
20e6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
20e6f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
20e70 2d 74 69 6d 65 20 74 65 73 74 20 61 64 64 72 65 -time test addre
20e71 73 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 ss */. Vdbe *v
20e72 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 = sqlite3GetVdbe
20e73 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 (pParse);. if(
20e74 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20 72 65 NEVER(v==0) ) re
20e75 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 45 turn;. sqlite3E
20e76 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61 xprCachePush(pPa
20e77 72 73 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 rse);.. /* This
20e78 20 63 6f 64 65 20 6d 75 73 74 20 62 65 20 72 75 code must be ru
20e79 6e 20 69 6e 20 69 74 73 20 65 6e 74 69 72 65 74 n in its entiret
20e7a 79 20 65 76 65 72 79 20 74 69 6d 65 20 69 74 20 y every time it
20e7b 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 20 is encountered.
20e7c 20 2a 2a 20 69 66 20 61 6e 79 20 6f 66 20 74 68 ** if any of th
20e7d 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 e following is t
20e7e 72 75 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 rue:. **. **
20e7f 20 20 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68 * The right-h
20e80 61 6e 64 20 73 69 64 65 20 69 73 20 61 20 63 6f and side is a co
20e81 72 72 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 rrelated subquer
20e82 79 0a 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 y. ** * The
20e83 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 right-hand side
20e84 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f is an expressio
20e85 6e 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e n list containin
20e86 67 20 76 61 72 69 61 62 6c 65 73 0a 20 20 2a 2a g variables. **
20e87 20 20 20 20 2a 20 20 57 65 20 61 72 65 20 69 6e * We are in
20e88 73 69 64 65 20 61 20 74 72 69 67 67 65 72 0a 20 side a trigger.
20e89 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c 20 **. ** If all
20e8a 6f 66 20 74 68 65 20 61 62 6f 76 65 20 61 72 65 of the above are
20e8b 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 77 65 20 false, then we
20e8c 63 61 6e 20 72 75 6e 20 74 68 69 73 20 63 6f 64 can run this cod
20e8d 65 20 6a 75 73 74 20 6f 6e 63 65 0a 20 20 2a 2a e just once. **
20e8e 20 73 61 76 65 20 74 68 65 20 72 65 73 75 6c 74 save the result
20e8f 73 2c 20 61 6e 64 20 72 65 75 73 65 20 74 68 65 s, and reuse the
20e90 20 73 61 6d 65 20 72 65 73 75 6c 74 20 6f 6e 20 same result on
20e91 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 subsequent invoc
20e92 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 ations.. */. i
20e93 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 f( !ExprHasAnyPr
20e94 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
20e95 5f 56 61 72 53 65 6c 65 63 74 29 20 26 26 20 21 _VarSelect) && !
20e96 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 pParse->pTrigger
20e97 54 61 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d Tab ){. int m
20e98 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e em = ++pParse->n
20e99 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 Mem;. sqlite3
20e9a 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
20e9b 5f 49 66 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 74 _If, mem);. t
20e9c 65 73 74 41 64 64 72 20 3d 20 73 71 6c 69 74 65 estAddr = sqlite
20e9d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
20e9e 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 6d 65 P_Integer, 1, me
20e9f 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 m);. assert(
20ea0 74 65 73 74 41 64 64 72 3e 30 20 7c 7c 20 70 50 testAddr>0 || pP
20ea1 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 arse->db->malloc
20ea2 46 61 69 6c 65 64 20 29 3b 0a 20 20 7d 0a 0a 20 Failed );. }..
20ea3 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e switch( pExpr->
20ea4 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 op ){. case T
20ea5 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 63 68 K_IN: {. ch
20ea6 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 ar affinity;.
20ea7 20 20 20 4b 65 79 49 6e 66 6f 20 6b 65 79 49 6e KeyInfo keyIn
20ea8 66 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 fo;. int ad
20ea9 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 64 dr; /* Ad
20eaa 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 65 6e dress of OP_Open
20eab 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 Ephemeral instru
20eac 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 ction */. E
20ead 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 xpr *pLeft = pEx
20eae 70 72 2d 3e 70 4c 65 66 74 3b 0a 0a 20 20 20 20 pr->pLeft;..
20eaf 20 20 69 66 28 20 72 4d 61 79 48 61 76 65 4e 75 if( rMayHaveNu
20eb0 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 ll ){. sq
20eb1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
20eb2 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 v, OP_Null, 0, r
20eb3 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 MayHaveNull);.
20eb4 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 66 66 }.. aff
20eb5 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 45 inity = sqlite3E
20eb6 78 70 72 41 66 66 69 6e 69 74 79 28 70 4c 65 66 xprAffinity(pLef
20eb7 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57 68 t);.. /* Wh
20eb8 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61 6e ether this is an
20eb9 20 27 78 20 49 4e 28 53 45 4c 45 43 54 2e 2e 2e 'x IN(SELECT...
20eba 29 27 20 6f 72 20 61 6e 20 27 78 20 49 4e 28 3c )' or an 'x IN(<
20ebb 65 78 70 72 6c 69 73 74 3e 29 27 0a 20 20 20 20 exprlist>)'.
20ebc 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 ** expression
20ebd 69 74 20 69 73 20 68 61 6e 64 6c 65 64 20 74 68 it is handled th
20ebe 65 20 73 61 6d 65 20 77 61 79 2e 20 41 20 76 69 e same way. A vi
20ebf 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20 0a rtual table is .
20ec0 20 20 20 20 20 20 2a 2a 20 66 69 6c 6c 65 64 20 ** filled
20ec1 77 69 74 68 20 73 69 6e 67 6c 65 2d 66 69 65 6c with single-fiel
20ec2 64 20 69 6e 64 65 78 20 6b 65 79 73 20 72 65 70 d index keys rep
20ec3 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 72 65 resenting the re
20ec4 73 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a 20 66 sults. ** f
20ec5 72 6f 6d 20 74 68 65 20 53 45 4c 45 43 54 20 6f rom the SELECT o
20ec6 72 20 74 68 65 20 3c 65 78 70 72 6c 69 73 74 3e r the <exprlist>
20ec7 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 .. **.
20ec8 20 2a 2a 20 49 66 20 74 68 65 20 27 78 27 20 65 ** If the 'x' e
20ec9 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63 xpression is a c
20eca 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 6f 72 20 olumn value, or
20ecb 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 0a 20 20 the SELECT....
20ecc 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 ** statement
20ecd 20 72 65 74 75 72 6e 73 20 61 20 63 6f 6c 75 6d returns a colum
20ece 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 n value, then th
20ecf 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 e affinity of th
20ed0 61 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75 at. ** colu
20ed1 6d 6e 20 69 73 20 75 73 65 64 20 74 6f 20 62 75 mn is used to bu
20ed2 69 6c 64 20 74 68 65 20 69 6e 64 65 78 20 6b 65 ild the index ke
20ed3 79 73 2e 20 49 66 20 62 6f 74 68 20 27 78 27 20 ys. If both 'x'
20ed4 61 6e 64 20 74 68 65 0a 20 20 20 20 20 20 2a 2a and the. **
20ed5 20 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65 SELECT... state
20ed6 6d 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73 ment are columns
20ed7 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 61 , then numeric a
20ed8 66 66 69 6e 69 74 79 20 69 73 20 75 73 65 64 0a ffinity is used.
20ed9 20 20 20 20 20 20 2a 2a 20 69 66 20 65 69 74 68 ** if eith
20eda 65 72 20 63 6f 6c 75 6d 6e 20 68 61 73 20 4e 55 er column has NU
20edb 4d 45 52 49 43 20 6f 72 20 49 4e 54 45 47 45 52 MERIC or INTEGER
20edc 20 61 66 66 69 6e 69 74 79 2e 20 49 66 20 6e 65 affinity. If ne
20edd 69 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 27 ither. ** '
20ede 78 27 20 6e 6f 72 20 74 68 65 20 53 45 4c 45 43 x' nor the SELEC
20edf 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 20 61 T... statement a
20ee0 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e re columns, then
20ee1 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74 numeric affinit
20ee2 79 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 75 73 y. ** is us
20ee3 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ed.. */.
20ee4 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 pExpr->iTable
20ee5 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b = pParse->nTab+
20ee6 2b 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 +;. addr =
20ee7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20ee8 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 2(v, OP_OpenEphe
20ee9 6d 65 72 61 6c 2c 20 70 45 78 70 72 2d 3e 69 54 meral, pExpr->iT
20eea 61 62 6c 65 2c 20 21 69 73 52 6f 77 69 64 29 3b able, !isRowid);
20eeb 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 6b . memset(&k
20eec 65 79 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f eyInfo, 0, sizeo
20eed 66 28 6b 65 79 49 6e 66 6f 29 29 3b 0a 20 20 20 f(keyInfo));.
20eee 20 20 20 6b 65 79 49 6e 66 6f 2e 6e 46 69 65 6c keyInfo.nFiel
20eef 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 69 66 d = 1;.. if
20ef0 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 ( ExprHasPropert
20ef1 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 y(pExpr, EP_xIsS
20ef2 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 elect) ){.
20ef3 20 20 2f 2a 20 43 61 73 65 20 31 3a 20 20 20 20 /* Case 1:
20ef4 20 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43 54 expr IN (SELECT
20ef5 20 2e 2e 2e 29 0a 20 20 20 20 20 20 20 20 2a 2a ...). **
20ef6 0a 20 20 20 20 20 20 20 20 2a 2a 20 47 65 6e 65 . ** Gene
20ef7 72 61 74 65 20 63 6f 64 65 20 74 6f 20 77 72 69 rate code to wri
20ef8 74 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f te the results o
20ef9 66 20 74 68 65 20 73 65 6c 65 63 74 20 69 6e 74 f the select int
20efa 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 0a o the temporary.
20efb 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 ** table
20efc 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 6f allocated and o
20efd 70 65 6e 65 64 20 61 62 6f 76 65 2e 0a 20 20 20 pened above..
20efe 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
20eff 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b SelectDest dest;
20f00 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c 69 73 . ExprLis
20f01 74 20 2a 70 45 4c 69 73 74 3b 0a 0a 20 20 20 20 t *pEList;..
20f02 20 20 20 20 61 73 73 65 72 74 28 20 21 69 73 52 assert( !isR
20f03 6f 77 69 64 20 29 3b 0a 20 20 20 20 20 20 20 20 owid );.
20f04 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 sqlite3SelectDes
20f05 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 tInit(&dest, SRT
20f06 5f 53 65 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 _Set, pExpr->iTa
20f07 62 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 64 65 ble);. de
20f08 73 74 2e 61 66 66 69 6e 69 74 79 20 3d 20 28 75 st.affinity = (u
20f09 38 29 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 8)affinity;.
20f0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 45 78 assert( (pEx
20f0b 70 72 2d 3e 69 54 61 62 6c 65 26 30 78 30 30 30 pr->iTable&0x000
20f0c 30 46 46 46 46 29 3d 3d 70 45 78 70 72 2d 3e 69 0FFFF)==pExpr->i
20f0d 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20 20 20 Table );.
20f0e 20 69 66 28 20 73 71 6c 69 74 65 33 53 65 6c 65 if( sqlite3Sele
20f0f 63 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 ct(pParse, pExpr
20f10 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20 26 64 65 ->x.pSelect, &de
20f11 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 st) ){.
20f12 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 return;.
20f13 20 7d 0a 20 20 20 20 20 20 20 20 70 45 4c 69 73 }. pELis
20f14 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 t = pExpr->x.pSe
20f15 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 lect->pEList;.
20f16 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 if( ALWAYS
20f17 28 70 45 4c 69 73 74 21 3d 30 20 26 26 20 70 45 (pEList!=0 && pE
20f18 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 29 20 29 List->nExpr>0) )
20f19 7b 20 0a 20 20 20 20 20 20 20 20 20 20 6b 65 79 { . key
20f1a 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 Info.aColl[0] =
20f1b 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d sqlite3BinaryCom
20f1c 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 pareCollSeq(pPar
20f1d 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 se, pExpr->pLeft
20f1e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
20f1f 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 pEList->a[0].pEx
20f20 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 pr);. }.
20f21 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 }else if( p
20f22 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 21 3d 30 Expr->x.pList!=0
20f23 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 ){. /* C
20f24 61 73 65 20 32 3a 20 20 20 20 20 65 78 70 72 20 ase 2: expr
20f25 49 4e 20 28 65 78 70 72 6c 69 73 74 29 0a 20 20 IN (exprlist).
20f26 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 **.
20f27 20 2a 2a 20 46 6f 72 20 65 61 63 68 20 65 78 70 ** For each exp
20f28 72 65 73 73 69 6f 6e 2c 20 62 75 69 6c 64 20 61 ression, build a
20f29 6e 20 69 6e 64 65 78 20 6b 65 79 20 66 72 6f 6d n index key from
20f2a 20 74 68 65 20 65 76 61 6c 75 61 74 69 6f 6e 20 the evaluation
20f2b 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 and. ** s
20f2c 74 6f 72 65 20 69 74 20 69 6e 20 74 68 65 20 74 tore it in the t
20f2d 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20 emporary table.
20f2e 49 66 20 3c 65 78 70 72 3e 20 69 73 20 61 20 63 If <expr> is a c
20f2f 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 75 73 65 0a olumn, then use.
20f30 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 ** that
20f31 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 79 columns affinity
20f32 20 77 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 69 when building i
20f33 6e 64 65 78 20 6b 65 79 73 2e 20 49 66 20 3c 65 ndex keys. If <e
20f34 78 70 72 3e 20 69 73 20 6e 6f 74 0a 20 20 20 20 xpr> is not.
20f35 20 20 20 20 2a 2a 20 61 20 63 6f 6c 75 6d 6e 2c ** a column,
20f36 20 75 73 65 20 6e 75 6d 65 72 69 63 20 61 66 66 use numeric aff
20f37 69 6e 69 74 79 2e 0a 20 20 20 20 20 20 20 20 2a inity.. *
20f38 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b /. int i;
20f39 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c 69 73 . ExprLis
20f3a 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72 t *pList = pExpr
20f3b 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 ->x.pList;.
20f3c 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 struct ExprLi
20f3d 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a st_item *pItem;.
20f3e 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 2c 20 int r1,
20f3f 72 32 2c 20 72 33 3b 0a 0a 20 20 20 20 20 20 20 r2, r3;..
20f40 20 69 66 28 20 21 61 66 66 69 6e 69 74 79 20 29 if( !affinity )
20f41 7b 0a 20 20 20 20 20 20 20 20 20 20 61 66 66 69 {. affi
20f42 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 nity = SQLITE_AF
20f43 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20 20 F_NONE;.
20f44 7d 0a 20 20 20 20 20 20 20 20 6b 65 79 49 6e 66 }. keyInf
20f45 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c o.aColl[0] = sql
20f46 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 ite3ExprCollSeq(
20f47 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
20f48 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 20 20 Left);..
20f49 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 /* Loop through
20f4a 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 each expression
20f4b 69 6e 20 3c 65 78 70 72 6c 69 73 74 3e 2e 20 2a in <exprlist>. *
20f4c 2f 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 73 /. r1 = s
20f4d 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 qlite3GetTempReg
20f4e 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 (pParse);.
20f4f 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47 65 r2 = sqlite3Ge
20f50 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 tTempReg(pParse)
20f51 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
20f52 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
20f53 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 32 29 3b 0a P_Null, 0, r2);.
20f54 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 70 4c for(i=pL
20f55 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 ist->nExpr, pIte
20f56 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3e 30 3b m=pList->a; i>0;
20f57 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a i--, pItem++){.
20f58 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2a Expr *
20f59 70 45 32 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 pE2 = pItem->pEx
20f5a 70 72 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f pr;.. /
20f5b 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 * If the express
20f5c 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74 ion is not const
20f5d 61 6e 74 20 74 68 65 6e 20 77 65 20 77 69 6c 6c ant then we will
20f5e 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20 need to.
20f5f 20 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 74 68 ** disable th
20f60 65 20 74 65 73 74 20 74 68 61 74 20 77 61 73 20 e test that was
20f61 67 65 6e 65 72 61 74 65 64 20 61 62 6f 76 65 20 generated above
20f62 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65 0a that makes sure.
20f63 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 ** thi
20f64 73 20 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65 63 s code only exec
20f65 75 74 65 73 20 6f 6e 63 65 2e 20 20 42 65 63 61 utes once. Beca
20f66 75 73 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63 6f use for a non-co
20f67 6e 73 74 61 6e 74 0a 20 20 20 20 20 20 20 20 20 nstant.
20f68 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 77 ** expression w
20f69 65 20 6e 65 65 64 20 74 6f 20 72 65 72 75 6e 20 e need to rerun
20f6a 74 68 69 73 20 63 6f 64 65 20 65 61 63 68 20 74 this code each t
20f6b 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a ime.. *
20f6c 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 /. if(
20f6d 74 65 73 74 41 64 64 72 20 26 26 20 21 73 71 6c testAddr && !sql
20f6e 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 ite3ExprIsConsta
20f6f 6e 74 28 70 45 32 29 20 29 7b 0a 20 20 20 20 20 nt(pE2) ){.
20f70 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
20f71 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 beChangeToNoop(v
20f72 2c 20 74 65 73 74 41 64 64 72 2d 31 2c 20 32 29 , testAddr-1, 2)
20f73 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 65 ;. te
20f74 73 74 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 stAddr = 0;.
20f75 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 }..
20f76 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 /* Evaluate t
20f77 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e he expression an
20f78 64 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74 6f d insert it into
20f79 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65 20 the temp table
20f7a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 33 20 */. r3
20f7b 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
20f7c 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 eTarget(pParse,
20f7d 70 45 32 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 pE2, r1);.
20f7e 20 20 20 20 69 66 28 20 69 73 52 6f 77 69 64 20 if( isRowid
20f7f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 ){. s
20f80 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
20f81 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 (v, OP_MustBeInt
20f82 2c 20 72 33 2c 20 73 71 6c 69 74 65 33 56 64 62 , r3, sqlite3Vdb
20f83 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b eCurrentAddr(v)+
20f84 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2);.
20f85 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20f86 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 3(v, OP_Insert,
20f87 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 pExpr->iTable, r
20f88 32 2c 20 72 33 29 3b 0a 20 20 20 20 20 20 20 20 2, r3);.
20f89 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
20f8a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
20f8b 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b AddOp4(v, OP_Mak
20f8c 65 52 65 63 6f 72 64 2c 20 72 33 2c 20 31 2c 20 eRecord, r3, 1,
20f8d 72 32 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31 r2, &affinity, 1
20f8e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 );. s
20f8f 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 qlite3ExprCacheA
20f90 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 ffinityChange(pP
20f91 61 72 73 65 2c 20 72 33 2c 20 31 29 3b 0a 20 20 arse, r3, 1);.
20f92 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
20f93 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
20f94 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 45 78 P_IdxInsert, pEx
20f95 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 29 3b pr->iTable, r2);
20f96 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
20f97 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 }. s
20f98 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
20f99 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 pReg(pParse, r1)
20f9a 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
20f9b 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
20f9c 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 pParse, r2);.
20f9d 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 }. if( !
20f9e 69 73 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 isRowid ){.
20f9f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
20fa0 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20 angeP4(v, addr,
20fa1 28 76 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66 6f (void *)&keyInfo
20fa2 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 , P4_KEYINFO);.
20fa3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
20fa4 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 ak;. }.. c
20fa5 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 ase TK_EXISTS:.
20fa6 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 case TK_SELEC
20fa7 54 3a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 T:. default:
20fa8 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 {. /* If th
20fa9 69 73 20 68 61 73 20 74 6f 20 62 65 20 61 20 73 is has to be a s
20faa 63 61 6c 61 72 20 53 45 4c 45 43 54 2e 20 20 47 calar SELECT. G
20fab 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 enerate code to
20fac 70 75 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a put the. **
20fad 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 73 value of this s
20fae 65 6c 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f 72 elect in a memor
20faf 79 20 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f 72 y cell and recor
20fb0 64 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 20 d the number.
20fb1 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d ** of the mem
20fb2 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c ory cell in iCol
20fb3 75 6d 6e 2e 20 20 49 66 20 74 68 69 73 20 69 73 umn. If this is
20fb4 20 61 6e 20 45 58 49 53 54 53 2c 20 77 72 69 74 an EXISTS, writ
20fb5 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e e. ** an in
20fb6 74 65 67 65 72 20 30 20 28 6e 6f 74 20 65 78 69 teger 0 (not exi
20fb7 73 74 73 29 20 6f 72 20 31 20 28 65 78 69 73 74 sts) or 1 (exist
20fb8 73 29 20 69 6e 74 6f 20 61 20 6d 65 6d 6f 72 79 s) into a memory
20fb9 20 63 65 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61 cell. ** a
20fba 6e 64 20 72 65 63 6f 72 64 20 74 68 61 74 20 6d nd record that m
20fbb 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 emory cell in iC
20fbc 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a olumn.. */.
20fbd 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e static con
20fbe 73 74 20 54 6f 6b 65 6e 20 6f 6e 65 20 3d 20 7b st Token one = {
20fbf 20 22 31 22 2c 20 31 20 7d 3b 20 20 2f 2a 20 54 "1", 1 }; /* T
20fc0 6f 6b 65 6e 20 66 6f 72 20 6c 69 74 65 72 61 6c oken for literal
20fc1 20 76 61 6c 75 65 20 31 20 2a 2f 0a 20 20 20 20 value 1 */.
20fc2 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 Select *pSel;
20fc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20fc4 20 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 /* SELEC
20fc5 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 65 T statement to e
20fc6 6e 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 53 ncode */. S
20fc7 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 20 electDest dest;
20fc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20fc9 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 /* How to d
20fca 65 61 6c 20 77 69 74 68 20 53 45 4c 45 43 74 20 eal with SELECt
20fcb 72 65 73 75 6c 74 20 2a 2f 0a 0a 20 20 20 20 20 result */..
20fcc 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 testcase( pExpr
20fcd 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 ->op==TK_EXISTS
20fce 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
20fcf 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b e( pExpr->op==TK
20fd0 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 _SELECT );.
20fd1 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
20fd2 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 7c 7c op==TK_EXISTS ||
20fd3 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 pExpr->op==TK_S
20fd4 45 4c 45 43 54 20 29 3b 0a 0a 20 20 20 20 20 20 ELECT );..
20fd5 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73 50 assert( ExprHasP
20fd6 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
20fd7 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a P_xIsSelect) );.
20fd8 20 20 20 20 20 20 70 53 65 6c 20 3d 20 70 45 78 pSel = pEx
20fd9 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3b 0a 20 pr->x.pSelect;.
20fda 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 sqlite3Sele
20fdb 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 ctDestInit(&dest
20fdc 2c 20 30 2c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e , 0, ++pParse->n
20fdd 4d 65 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 Mem);. if(
20fde 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 pExpr->op==TK_SE
20fdf 4c 45 43 54 20 29 7b 0a 20 20 20 20 20 20 20 20 LECT ){.
20fe0 64 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 dest.eDest = SRT
20fe1 5f 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 73 71 _Mem;. sq
20fe2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
20fe3 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 64 v, OP_Null, 0, d
20fe4 65 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 est.iParm);.
20fe5 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 VdbeComment(
20fe6 28 76 2c 20 22 49 6e 69 74 20 73 75 62 71 75 65 (v, "Init subque
20fe7 72 79 20 72 65 73 75 6c 74 22 29 29 3b 0a 20 20 ry result"));.
20fe8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
20fe9 20 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d 20 dest.eDest =
20fea 53 52 54 5f 45 78 69 73 74 73 3b 0a 20 20 20 20 SRT_Exists;.
20feb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
20fec 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 ddOp2(v, OP_Inte
20fed 67 65 72 2c 20 30 2c 20 64 65 73 74 2e 69 50 61 ger, 0, dest.iPa
20fee 72 6d 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62 rm);. Vdb
20fef 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 49 6e eComment((v, "In
20ff0 69 74 20 45 58 49 53 54 53 20 72 65 73 75 6c 74 it EXISTS result
20ff1 22 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 "));. }.
20ff2 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 sqlite3ExprDe
20ff3 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c lete(pParse->db,
20ff4 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 29 3b 0a pSel->pLimit);.
20ff5 20 20 20 20 20 20 70 53 65 6c 2d 3e 70 4c 69 6d pSel->pLim
20ff6 69 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 it = sqlite3PExp
20ff7 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 54 r(pParse, TK_INT
20ff8 45 47 45 52 2c 20 30 2c 20 30 2c 20 26 6f 6e 65 EGER, 0, 0, &one
20ff9 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c );. if( sql
20ffa 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 ite3Select(pPars
20ffb 65 2c 20 70 53 65 6c 2c 20 26 64 65 73 74 29 20 e, pSel, &dest)
20ffc 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
20ffd 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 n;. }.
20ffe 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 pExpr->iColumn
20fff 3d 20 28 69 31 36 29 64 65 73 74 2e 69 50 61 72 = (i16)dest.iPar
21000 6d 3b 0a 20 20 20 20 20 20 45 78 70 72 53 65 74 m;. ExprSet
21001 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78 70 Irreducible(pExp
21002 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b r);. break;
21003 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 . }. }.. if
21004 28 20 74 65 73 74 41 64 64 72 20 29 7b 0a 20 20 ( testAddr ){.
21005 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
21006 70 48 65 72 65 28 76 2c 20 74 65 73 74 41 64 64 pHere(v, testAdd
21007 72 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 r-1);. }. sqli
21008 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 te3ExprCachePop(
21009 70 50 61 72 73 65 2c 20 31 29 3b 0a 0a 20 20 72 pParse, 1);.. r
2100a 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 eturn;.}.#endif
2100b 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 /* SQLITE_OMIT_S
2100c 55 42 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a UBQUERY */../*.*
2100d 2a 20 44 75 70 6c 69 63 61 74 65 20 61 6e 20 38 * Duplicate an 8
2100e 2d 62 79 74 65 20 76 61 6c 75 65 0a 2a 2f 0a 73 -byte value.*/.s
2100f 74 61 74 69 63 20 63 68 61 72 20 2a 64 75 70 38 tatic char *dup8
21010 62 79 74 65 73 28 56 64 62 65 20 2a 76 2c 20 63 bytes(Vdbe *v, c
21011 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 29 7b 0a onst char *in){.
21012 20 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 73 71 char *out = sq
21013 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
21014 28 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 (sqlite3VdbeDb(v
21015 29 2c 20 38 29 3b 0a 20 20 69 66 28 20 6f 75 74 ), 8);. if( out
21016 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 6f ){. memcpy(o
21017 75 74 2c 20 69 6e 2c 20 38 29 3b 0a 20 20 7d 0a ut, in, 8);. }.
21018 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a return out;.}.
21019 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
2101a 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 an instruction t
2101b 68 61 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 hat will put the
2101c 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a floating point.
2101d 2a 2a 20 76 61 6c 75 65 20 64 65 73 63 72 69 62 ** value describ
2101e 65 64 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 ed by z[0..n-1]
2101f 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d into register iM
21020 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b em..**.** The z[
21021 5d 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 ] string will pr
21022 6f 62 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 obably not be ze
21023 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 ro-terminated.
21024 42 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d But the .** z[n]
21025 20 63 68 61 72 61 63 74 65 72 20 69 73 20 67 75 character is gu
21026 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 aranteed to be s
21027 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f omething that do
21028 65 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c es not look.** l
21029 69 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 ike the continua
2102a 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 tion of the numb
2102b 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f er..*/.static vo
2102c 69 64 20 63 6f 64 65 52 65 61 6c 28 56 64 62 65 id codeReal(Vdbe
2102d 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *v, const char
2102e 2a 7a 2c 20 69 6e 74 20 6e 65 67 61 74 65 46 6c *z, int negateFl
2102f 61 67 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 ag, int iMem){.
21030 20 69 66 28 20 41 4c 57 41 59 53 28 7a 21 3d 30 if( ALWAYS(z!=0
21031 29 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 ) ){. double
21032 76 61 6c 75 65 3b 0a 20 20 20 20 63 68 61 72 20 value;. char
21033 2a 7a 56 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 *zV;. sqlite3
21034 41 74 6f 46 28 7a 2c 20 26 76 61 6c 75 65 29 3b AtoF(z, &value);
21035 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 73 71 . assert( !sq
21036 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 75 65 lite3IsNaN(value
21037 29 20 29 3b 20 2f 2a 20 54 68 65 20 6e 65 77 20 ) ); /* The new
21038 41 74 6f 46 20 6e 65 76 65 72 20 72 65 74 75 72 AtoF never retur
21039 6e 73 20 4e 61 4e 20 2a 2f 0a 20 20 20 20 69 66 ns NaN */. if
2103a 28 20 6e 65 67 61 74 65 46 6c 61 67 20 29 20 76 ( negateFlag ) v
2103b 61 6c 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 alue = -value;.
2103c 20 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74 65 zV = dup8byte
2103d 73 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61 6c s(v, (char*)&val
2103e 75 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ue);. sqlite3
2103f 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
21040 5f 52 65 61 6c 2c 20 30 2c 20 69 4d 65 6d 2c 20 _Real, 0, iMem,
21041 30 2c 20 7a 56 2c 20 50 34 5f 52 45 41 4c 29 3b 0, zV, P4_REAL);
21042 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 . }.}.../*.** G
21043 65 6e 65 72 61 74 65 20 61 6e 20 69 6e 73 74 72 enerate an instr
21044 75 63 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c uction that will
21045 20 70 75 74 20 74 68 65 20 69 6e 74 65 67 65 72 put the integer
21046 20 64 65 73 63 72 69 62 65 20 62 79 0a 2a 2a 20 describe by.**
21047 74 65 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 text z[0..n-1] i
21048 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 nto register iMe
21049 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d m..**.** The z[]
2104a 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f string will pro
2104b 62 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 bably not be zer
2104c 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 o-terminated. B
2104d 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 ut the .** z[n]
2104e 63 68 61 72 61 63 74 65 72 20 69 73 20 67 75 61 character is gua
2104f 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f ranteed to be so
21050 6d 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 mething that doe
21051 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 s not look.** li
21052 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 ke the continuat
21053 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 ion of the numbe
21054 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 r..*/.static voi
21055 64 20 63 6f 64 65 49 6e 74 65 67 65 72 28 56 64 d codeInteger(Vd
21056 62 65 20 2a 76 2c 20 45 78 70 72 20 2a 70 45 78 be *v, Expr *pEx
21057 70 72 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 2c pr, int negFlag,
21058 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 69 66 int iMem){. if
21059 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 ( pExpr->flags &
2105a 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 29 7b 0a EP_IntValue ){.
2105b 20 20 20 20 69 6e 74 20 69 20 3d 20 70 45 78 70 int i = pExp
2105c 72 2d 3e 75 2e 69 56 61 6c 75 65 3b 0a 20 20 20 r->u.iValue;.
2105d 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 69 if( negFlag ) i
2105e 20 3d 20 2d 69 3b 0a 20 20 20 20 73 71 6c 69 74 = -i;. sqlit
2105f 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
21060 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 69 OP_Integer, i, i
21061 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Mem);. }else{.
21062 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
21063 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b = pExpr->u.zTok
21064 65 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 en;. assert(
21065 7a 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 z!=0 );. if(
21066 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 sqlite3FitsIn64B
21067 69 74 73 28 7a 2c 20 6e 65 67 46 6c 61 67 29 20 its(z, negFlag)
21068 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76 61 6c ){. i64 val
21069 75 65 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a ue;. char *
2106a 7a 56 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 zV;. sqlite
2106b 33 41 74 6f 69 36 34 28 7a 2c 20 26 76 61 6c 75 3Atoi64(z, &valu
2106c 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 e);. if( ne
2106d 67 46 6c 61 67 20 29 20 76 61 6c 75 65 20 3d 20 gFlag ) value =
2106e 2d 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7a 56 -value;. zV
2106f 20 3d 20 64 75 70 38 62 79 74 65 73 28 76 2c 20 = dup8bytes(v,
21070 28 63 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a (char*)&value);.
21071 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21072 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 6e eAddOp4(v, OP_In
21073 74 36 34 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c t64, 0, iMem, 0,
21074 20 7a 56 2c 20 50 34 5f 49 4e 54 36 34 29 3b 0a zV, P4_INT64);.
21075 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
21076 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 7a 2c 20 codeReal(v, z,
21077 6e 65 67 46 6c 61 67 2c 20 69 4d 65 6d 29 3b 0a negFlag, iMem);.
21078 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
21079 2a 2a 20 43 6c 65 61 72 20 61 20 63 61 63 68 65 ** Clear a cache
2107a 20 65 6e 74 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 entry..*/.stati
2107b 63 20 76 6f 69 64 20 63 61 63 68 65 45 6e 74 72 c void cacheEntr
2107c 79 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70 50 yClear(Parse *pP
2107d 61 72 73 65 2c 20 73 74 72 75 63 74 20 79 43 6f arse, struct yCo
2107e 6c 43 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 66 lCache *p){. if
2107f 28 20 70 2d 3e 74 65 6d 70 52 65 67 20 29 7b 0a ( p->tempReg ){.
21080 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e if( pParse->
21081 6e 54 65 6d 70 52 65 67 3c 41 72 72 61 79 53 69 nTempReg<ArraySi
21082 7a 65 28 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 ze(pParse->aTemp
21083 52 65 67 29 20 29 7b 0a 20 20 20 20 20 20 70 50 Reg) ){. pP
21084 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b 70 arse->aTempReg[p
21085 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 2b Parse->nTempReg+
21086 2b 5d 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 +] = p->iReg;.
21087 20 20 7d 0a 20 20 20 20 70 2d 3e 74 65 6d 70 52 }. p->tempR
21088 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a eg = 0;. }.}...
21089 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 69 6e 20 /*.** Record in
2108a 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 the column cache
2108b 20 74 68 61 74 20 61 20 70 61 72 74 69 63 75 6c that a particul
2108c 61 72 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61 ar column from a
2108d 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 74 .** particular t
2108e 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69 able is stored i
2108f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 n a particular r
21090 65 67 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 egister..*/.SQLI
21091 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
21092 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
21093 53 74 6f 72 65 28 50 61 72 73 65 20 2a 70 50 61 Store(Parse *pPa
21094 72 73 65 2c 20 69 6e 74 20 69 54 61 62 2c 20 69 rse, int iTab, i
21095 6e 74 20 69 43 6f 6c 2c 20 69 6e 74 20 69 52 65 nt iCol, int iRe
21096 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 g){. int i;. i
21097 6e 74 20 6d 69 6e 4c 72 75 3b 0a 20 20 69 6e 74 nt minLru;. int
21098 20 69 64 78 4c 72 75 3b 0a 20 20 73 74 72 75 63 idxLru;. struc
21099 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a t yColCache *p;.
2109a 0a 20 20 61 73 73 65 72 74 28 20 69 52 65 67 3e . assert( iReg>
2109b 30 20 29 3b 20 20 2f 2a 20 52 65 67 69 73 74 65 0 ); /* Registe
2109c 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61 6c r numbers are al
2109d 77 61 79 73 20 70 6f 73 69 74 69 76 65 20 2a 2f ways positive */
2109e 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e . assert( iCol>
2109f 3d 2d 31 20 26 26 20 69 43 6f 6c 3c 33 32 37 36 =-1 && iCol<3276
210a0 38 20 29 3b 20 20 2f 2a 20 46 69 6e 69 74 65 20 8 ); /* Finite
210a1 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 73 20 2a column numbers *
210a2 2f 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 72 65 /.. /* First re
210a3 70 6c 61 63 65 20 61 6e 79 20 65 78 69 73 74 69 place any existi
210a4 6e 67 20 65 6e 74 72 79 20 2a 2f 0a 20 20 66 6f ng entry */. fo
210a5 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d r(i=0, p=pParse-
210a6 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 >aColCache; i<SQ
210a7 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b LITE_N_COLCACHE;
210a8 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 i++, p++){.
210a9 69 66 28 20 70 2d 3e 69 52 65 67 20 26 26 20 70 if( p->iReg && p
210aa 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 ->iTable==iTab &
210ab 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 43 & p->iColumn==iC
210ac 6f 6c 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68 ol ){. cach
210ad 65 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72 eEntryClear(pPar
210ae 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d se, p);. p-
210af 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65 >iLevel = pParse
210b0 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 ->iCacheLevel;.
210b1 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 p->iReg = i
210b2 52 65 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 Reg;. p->af
210b3 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 fChange = 0;.
210b4 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 p->lru = pPar
210b5 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b se->iCacheCnt++;
210b6 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 . return;.
210b7 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 }. }.. /* F
210b8 69 6e 64 20 61 6e 20 65 6d 70 74 79 20 73 6c 6f ind an empty slo
210b9 74 20 61 6e 64 20 72 65 70 6c 61 63 65 20 69 74 t and replace it
210ba 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 */. for(i=0, p
210bb 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 =pParse->aColCac
210bc 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 he; i<SQLITE_N_C
210bd 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b OLCACHE; i++, p+
210be 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 +){. if( p->i
210bf 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Reg==0 ){.
210c0 70 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 p->iLevel = pPar
210c1 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b se->iCacheLevel;
210c2 0a 20 20 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 . p->iTable
210c3 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 20 20 70 = iTab;. p
210c4 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c ->iColumn = iCol
210c5 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 ;. p->iReg
210c6 3d 20 69 52 65 67 3b 0a 20 20 20 20 20 20 70 2d = iReg;. p-
210c7 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a >affChange = 0;.
210c8 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 p->tempReg
210c9 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6c = 0;. p->l
210ca 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 ru = pParse->iCa
210cb 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 cheCnt++;.
210cc 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
210cd 7d 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 }.. /* Replace
210ce 74 68 65 20 6c 61 73 74 20 72 65 63 65 6e 74 6c the last recentl
210cf 79 20 75 73 65 64 20 2a 2f 0a 20 20 6d 69 6e 4c y used */. minL
210d0 72 75 20 3d 20 30 78 37 66 66 66 66 66 66 66 3b ru = 0x7fffffff;
210d1 0a 20 20 69 64 78 4c 72 75 20 3d 20 2d 31 3b 0a . idxLru = -1;.
210d2 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 for(i=0, p=pPa
210d3 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 rse->aColCache;
210d4 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 i<SQLITE_N_COLCA
210d5 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a CHE; i++, p++){.
210d6 20 20 20 20 69 66 28 20 70 2d 3e 6c 72 75 3c 6d if( p->lru<m
210d7 69 6e 4c 72 75 20 29 7b 0a 20 20 20 20 20 20 69 inLru ){. i
210d8 64 78 4c 72 75 20 3d 20 69 3b 0a 20 20 20 20 20 dxLru = i;.
210d9 20 6d 69 6e 4c 72 75 20 3d 20 70 2d 3e 6c 72 75 minLru = p->lru
210da 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
210db 28 20 41 4c 57 41 59 53 28 69 64 78 4c 72 75 3e ( ALWAYS(idxLru>
210dc 3d 30 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 26 =0) ){. p = &
210dd 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 pParse->aColCach
210de 65 5b 69 64 78 4c 72 75 5d 3b 0a 20 20 20 20 70 e[idxLru];. p
210df 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 ->iLevel = pPars
210e0 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a e->iCacheLevel;.
210e1 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 p->iTable =
210e2 69 54 61 62 3b 0a 20 20 20 20 70 2d 3e 69 43 6f iTab;. p->iCo
210e3 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 lumn = iCol;.
210e4 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b p->iReg = iReg;
210e5 0a 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 . p->affChang
210e6 65 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 74 65 e = 0;. p->te
210e7 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 70 mpReg = 0;. p
210e8 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e ->lru = pParse->
210e9 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 iCacheCnt++;.
210ea 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 7d 0a 0a return;. }.}..
210eb 2f 2a 0a 2a 2a 20 49 6e 64 69 63 61 74 65 20 74 /*.** Indicate t
210ec 68 61 74 20 61 20 72 65 67 69 73 74 65 72 20 69 hat a register i
210ed 73 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74 s being overwrit
210ee 74 65 6e 2e 20 20 50 75 72 67 65 20 74 68 65 20 ten. Purge the
210ef 72 65 67 69 73 74 65 72 0a 2a 2a 20 66 72 6f 6d register.** from
210f0 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 the column cach
210f1 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
210f2 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
210f3 33 45 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65 3ExprCacheRemove
210f4 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
210f5 69 6e 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 int iReg){. int
210f6 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f i;. struct yCo
210f7 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 lCache *p;. for
210f8 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e (i=0, p=pParse->
210f9 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c aColCache; i<SQL
210fa 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 ITE_N_COLCACHE;
210fb 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 i++, p++){. i
210fc 66 28 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67 f( p->iReg==iReg
210fd 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45 ){. cacheE
210fe 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72 73 65 ntryClear(pParse
210ff 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 , p);. p->i
21100 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 Reg = 0;. }.
21101 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 }.}../*.** Reme
21102 6d 62 65 72 20 74 68 65 20 63 75 72 72 65 6e 74 mber the current
21103 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 63 6f column cache co
21104 6e 74 65 78 74 2e 20 20 41 6e 79 20 6e 65 77 20 ntext. Any new
21105 65 6e 74 72 69 65 73 20 61 64 64 65 64 0a 2a 2a entries added.**
21106 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 63 6f added to the co
21107 6c 75 6d 6e 20 63 61 63 68 65 20 61 66 74 65 72 lumn cache after
21108 20 74 68 69 73 20 63 61 6c 6c 20 61 72 65 20 72 this call are r
21109 65 6d 6f 76 65 64 20 77 68 65 6e 20 74 68 65 0a emoved when the.
2110a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ** corresponding
2110b 20 70 6f 70 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a pop occurs..*/.
2110c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
2110d 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
2110e 61 63 68 65 50 75 73 68 28 50 61 72 73 65 20 2a achePush(Parse *
2110f 70 50 61 72 73 65 29 7b 0a 20 20 70 50 61 72 73 pParse){. pPars
21110 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 2b 2b e->iCacheLevel++
21111 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 ;.}../*.** Remov
21112 65 20 66 72 6f 6d 20 74 68 65 20 63 6f 6c 75 6d e from the colum
21113 6e 20 63 61 63 68 65 20 61 6e 79 20 65 6e 74 72 n cache any entr
21114 69 65 73 20 74 68 61 74 20 77 65 72 65 20 61 64 ies that were ad
21115 64 65 64 20 73 69 6e 63 65 20 74 68 65 0a 2a 2a ded since the.**
21116 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 4e 20 the previous N
21117 50 75 73 68 20 6f 70 65 72 61 74 69 6f 6e 73 2e Push operations.
21118 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 In other words
21119 2c 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 61 , restore the ca
2111a 63 68 65 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 che.** to the st
2111b 61 74 65 20 69 74 20 77 61 73 20 69 6e 20 4e 20 ate it was in N
2111c 50 75 73 68 65 73 20 61 67 6f 2e 0a 2a 2f 0a 53 Pushes ago..*/.S
2111d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
2111e 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 id sqlite3ExprCa
2111f 63 68 65 50 6f 70 28 50 61 72 73 65 20 2a 70 50 chePop(Parse *pP
21120 61 72 73 65 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 arse, int N){.
21121 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 int i;. struct
21122 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 yColCache *p;.
21123 61 73 73 65 72 74 28 20 4e 3e 30 20 29 3b 0a 20 assert( N>0 );.
21124 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d assert( pParse-
21125 3e 69 43 61 63 68 65 4c 65 76 65 6c 3e 3d 4e 20 >iCacheLevel>=N
21126 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 69 43 61 );. pParse->iCa
21127 63 68 65 4c 65 76 65 6c 20 2d 3d 20 4e 3b 0a 20 cheLevel -= N;.
21128 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 for(i=0, p=pPar
21129 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 se->aColCache; i
2112a 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 <SQLITE_N_COLCAC
2112b 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 HE; i++, p++){.
2112c 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 20 26 if( p->iReg &
2112d 26 20 70 2d 3e 69 4c 65 76 65 6c 3e 70 50 61 72 & p->iLevel>pPar
2112e 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 20 se->iCacheLevel
2112f 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e ){. cacheEn
21130 74 72 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c tryClear(pParse,
21131 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 p);. p->iR
21132 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 eg = 0;. }.
21133 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 }.}../*.** When
21134 61 20 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 a cached column
21135 69 73 20 72 65 75 73 65 64 2c 20 6d 61 6b 65 20 is reused, make
21136 73 75 72 65 20 74 68 61 74 20 69 74 73 20 72 65 sure that its re
21137 67 69 73 74 65 72 20 69 73 0a 2a 2a 20 6e 6f 20 gister is.** no
21138 6c 6f 6e 67 65 72 20 61 76 61 69 6c 61 62 6c 65 longer available
21139 20 61 73 20 61 20 74 65 6d 70 20 72 65 67 69 73 as a temp regis
2113a 74 65 72 2e 20 20 74 69 63 6b 65 74 20 23 33 38 ter. ticket #38
2113b 37 39 3a 20 20 74 68 61 74 20 73 61 6d 65 0a 2a 79: that same.*
2113c 2a 20 72 65 67 69 73 74 65 72 20 6d 69 67 68 74 * register might
2113d 20 62 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 be in the cache
2113e 20 69 6e 20 6d 75 6c 74 69 70 6c 65 20 70 6c 61 in multiple pla
2113f 63 65 73 2c 20 73 6f 20 62 65 20 73 75 72 65 20 ces, so be sure
21140 74 6f 0a 2a 2a 20 67 65 74 20 74 68 65 6d 20 61 to.** get them a
21141 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ll..*/.static vo
21142 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 id sqlite3ExprCa
21143 63 68 65 50 69 6e 52 65 67 69 73 74 65 72 28 50 chePinRegister(P
21144 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e arse *pParse, in
21145 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 t iReg){. int i
21146 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 ;. struct yColC
21147 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 ache *p;. for(i
21148 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 =0, p=pParse->aC
21149 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 olCache; i<SQLIT
2114a 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b E_N_COLCACHE; i+
2114b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 +, p++){. if(
2114c 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 p->iReg==iReg )
2114d 7b 0a 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 52 {. p->tempR
2114e 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 eg = 0;. }.
2114f 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 }.}../*.** Gener
21150 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 ate code that wi
21151 6c 6c 20 65 78 74 72 61 63 74 20 74 68 65 20 69 ll extract the i
21152 43 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c 75 6d 6e Column-th column
21153 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c 65 20 70 from.** table p
21154 54 61 62 20 61 6e 64 20 73 74 6f 72 65 20 74 68 Tab and store th
21155 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 e column value i
21156 6e 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 41 n a register. A
21157 6e 20 65 66 66 6f 72 74 0a 2a 2a 20 69 73 20 6d n effort.** is m
21158 61 64 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 ade to store the
21159 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 6e column value in
2115a 20 72 65 67 69 73 74 65 72 20 69 52 65 67 2c 20 register iReg,
2115b 62 75 74 20 74 68 69 73 20 69 73 0a 2a 2a 20 6e but this is.** n
2115c 6f 74 20 67 75 61 72 61 6e 74 65 65 64 2e 20 20 ot guaranteed.
2115d 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 The location of
2115e 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 the column value
2115f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
21160 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62 .** There must b
21161 65 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72 e an open cursor
21162 20 74 6f 20 70 54 61 62 20 69 6e 20 69 54 61 62 to pTab in iTab
21163 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 le when this rou
21164 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 tine.** is calle
21165 64 2e 20 20 49 66 20 69 43 6f 6c 75 6d 6e 3c 30 d. If iColumn<0
21166 20 74 68 65 6e 20 63 6f 64 65 20 69 73 20 67 65 then code is ge
21167 6e 65 72 61 74 65 64 20 74 68 61 74 20 65 78 74 nerated that ext
21168 72 61 63 74 73 20 74 68 65 20 72 6f 77 69 64 2e racts the rowid.
21169 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
2116a 69 6e 65 20 6d 69 67 68 74 20 61 74 74 65 6d 70 ine might attemp
2116b 74 20 74 6f 20 72 65 75 73 65 20 74 68 65 20 76 t to reuse the v
2116c 61 6c 75 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 alue of the colu
2116d 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 73 20 61 mn that.** has a
2116e 6c 72 65 61 64 79 20 62 65 65 6e 20 6c 6f 61 64 lready been load
2116f 65 64 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74 ed into a regist
21170 65 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20 77 er. The value w
21171 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62 65 ill always.** be
21172 20 75 73 65 64 20 69 66 20 69 74 20 68 61 73 20 used if it has
21173 6e 6f 74 20 75 6e 64 65 72 67 6f 6e 65 20 61 6e not undergone an
21174 79 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 y affinity chang
21175 65 73 2e 20 20 42 75 74 20 69 66 0a 2a 2a 20 61 es. But if.** a
21176 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 n affinity chang
21177 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2c 20 e has occurred,
21178 74 68 65 6e 20 74 68 65 20 63 61 63 68 65 64 20 then the cached
21179 76 61 6c 75 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 value will only
2117a 62 65 0a 2a 2a 20 75 73 65 64 20 69 66 20 61 6c be.** used if al
2117b 6c 6f 77 41 66 66 43 68 6e 67 20 69 73 20 74 72 lowAffChng is tr
2117c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ue..*/.SQLITE_PR
2117d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
2117e 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 3ExprCodeGetColu
2117f 6d 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 mn(. Parse *pPa
21180 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 69 6e rse, /* Parsin
21181 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 g and code gener
21182 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f ating context */
21183 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 . Table *pTab,
21184 20 20 20 20 2f 2a 20 44 65 73 63 72 69 70 74 69 /* Descripti
21185 6f 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 on of the table
21186 77 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 66 we are reading f
21187 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f rom */. int iCo
21188 6c 75 6d 6e 2c 20 20 20 20 20 2f 2a 20 49 6e 64 lumn, /* Ind
21189 65 78 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 ex of the table
2118a 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 column */. int
2118b 69 54 61 62 6c 65 2c 20 20 20 20 20 20 2f 2a 20 iTable, /*
2118c 54 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 The cursor point
2118d 69 6e 67 20 74 6f 20 74 68 65 20 74 61 62 6c 65 ing to the table
2118e 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 2c 20 */. int iReg,
2118f 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 /* Store
21190 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a results here */.
21191 20 20 69 6e 74 20 61 6c 6c 6f 77 41 66 66 43 68 int allowAffCh
21192 6e 67 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72 ng /* True if pr
21193 69 6f 72 20 61 66 66 69 6e 69 74 79 20 63 68 61 ior affinity cha
21194 6e 67 65 73 20 61 72 65 20 4f 4b 20 2a 2f 0a 29 nges are OK */.)
21195 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 {. Vdbe *v = pP
21196 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 arse->pVdbe;. i
21197 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 nt i;. struct y
21198 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 ColCache *p;..
21199 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 for(i=0, p=pPars
2119a 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c e->aColCache; i<
2119b 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 SQLITE_N_COLCACH
2119c 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 E; i++, p++){.
2119d 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3e 30 20 if( p->iReg>0
2119e 26 26 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 && p->iTable==iT
2119f 61 62 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75 able && p->iColu
211a0 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 mn==iColumn.
211a1 20 20 20 20 20 20 20 26 26 20 28 21 70 2d 3e 61 && (!p->a
211a2 66 66 43 68 61 6e 67 65 20 7c 7c 20 61 6c 6c 6f ffChange || allo
211a3 77 41 66 66 43 68 6e 67 29 20 29 7b 0a 20 20 20 wAffChng) ){.
211a4 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 p->lru = pPar
211a5 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b se->iCacheCnt++;
211a6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
211a7 70 72 43 61 63 68 65 50 69 6e 52 65 67 69 73 74 prCachePinRegist
211a8 65 72 28 70 50 61 72 73 65 2c 20 70 2d 3e 69 52 er(pParse, p->iR
211a9 65 67 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 eg);. retur
211aa 6e 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 7d n p->iReg;. }
211ab 0a 20 20 7d 20 20 0a 20 20 61 73 73 65 72 74 28 . } . assert(
211ac 20 76 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 v!=0 );. if( i
211ad 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 Column<0 ){.
211ae 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
211af 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 2(v, OP_Rowid, i
211b0 54 61 62 6c 65 2c 20 69 52 65 67 29 3b 0a 20 20 Table, iReg);.
211b1 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53 }else if( ALWAYS
211b2 28 70 54 61 62 21 3d 30 29 20 29 7b 0a 20 20 20 (pTab!=0) ){.
211b3 20 69 6e 74 20 6f 70 20 3d 20 49 73 56 69 72 74 int op = IsVirt
211b4 75 61 6c 28 70 54 61 62 29 20 3f 20 4f 50 5f 56 ual(pTab) ? OP_V
211b5 43 6f 6c 75 6d 6e 20 3a 20 4f 50 5f 43 6f 6c 75 Column : OP_Colu
211b6 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 mn;. sqlite3V
211b7 64 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 2c dbeAddOp3(v, op,
211b8 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e iTable, iColumn
211b9 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 73 71 6c , iReg);. sql
211ba 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c ite3ColumnDefaul
211bb 74 28 76 2c 20 70 54 61 62 2c 20 69 43 6f 6c 75 t(v, pTab, iColu
211bc 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 7d 0a 20 mn, iReg);. }.
211bd 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
211be 65 53 74 6f 72 65 28 70 50 61 72 73 65 2c 20 69 eStore(pParse, i
211bf 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 Table, iColumn,
211c0 69 52 65 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 iReg);. return
211c1 69 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 iReg;.}../*.** C
211c2 6c 65 61 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 lear all column
211c3 63 61 63 68 65 20 65 6e 74 72 69 65 73 2e 0a 2a cache entries..*
211c4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
211c5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
211c6 72 43 61 63 68 65 43 6c 65 61 72 28 50 61 72 73 rCacheClear(Pars
211c7 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 6e e *pParse){. in
211c8 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 t i;. struct yC
211c9 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 66 olCache *p;.. f
211ca 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 or(i=0, p=pParse
211cb 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 ->aColCache; i<S
211cc 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 QLITE_N_COLCACHE
211cd 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 ; i++, p++){.
211ce 20 69 66 28 20 70 2d 3e 69 52 65 67 20 29 7b 0a if( p->iReg ){.
211cf 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 cacheEntry
211d0 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 Clear(pParse, p)
211d1 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 ;. p->iReg
211d2 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d = 0;. }. }.}
211d3 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 74 ../*.** Record t
211d4 68 65 20 66 61 63 74 20 74 68 61 74 20 61 6e 20 he fact that an
211d5 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 affinity change
211d6 68 61 73 20 6f 63 63 75 72 72 65 64 20 6f 6e 20 has occurred on
211d7 69 43 6f 75 6e 74 0a 2a 2a 20 72 65 67 69 73 74 iCount.** regist
211d8 65 72 73 20 73 74 61 72 74 69 6e 67 20 77 69 74 ers starting wit
211d9 68 20 69 53 74 61 72 74 2e 0a 2a 2f 0a 53 51 4c h iStart..*/.SQL
211da 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
211db 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
211dc 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 eAffinityChange(
211dd 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
211de 6e 74 20 69 53 74 61 72 74 2c 20 69 6e 74 20 69 nt iStart, int i
211df 43 6f 75 6e 74 29 7b 0a 20 20 69 6e 74 20 69 45 Count){. int iE
211e0 6e 64 20 3d 20 69 53 74 61 72 74 20 2b 20 69 43 nd = iStart + iC
211e1 6f 75 6e 74 20 2d 20 31 3b 0a 20 20 69 6e 74 20 ount - 1;. int
211e2 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c i;. struct yCol
211e3 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 Cache *p;. for(
211e4 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 i=0, p=pParse->a
211e5 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 ColCache; i<SQLI
211e6 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 TE_N_COLCACHE; i
211e7 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e ++, p++){. in
211e8 74 20 72 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 t r = p->iReg;.
211e9 20 20 20 69 66 28 20 72 3e 3d 69 53 74 61 72 74 if( r>=iStart
211ea 20 26 26 20 72 3c 3d 69 45 6e 64 20 29 7b 0a 20 && r<=iEnd ){.
211eb 20 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 p->affChang
211ec 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d e = 1;. }. }
211ed 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 .}../*.** Genera
211ee 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 76 65 20 te code to move
211ef 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 content from reg
211f0 69 73 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 isters iFrom...i
211f1 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f From+nReg-1.** o
211f2 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b ver to iTo..iTo+
211f3 6e 52 65 67 2d 31 2e 20 4b 65 65 70 20 74 68 65 nReg-1. Keep the
211f4 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 75 70 column cache up
211f5 2d 74 6f 2d 64 61 74 65 2e 0a 2a 2f 0a 53 51 4c -to-date..*/.SQL
211f6 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
211f7 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
211f8 4d 6f 76 65 28 50 61 72 73 65 20 2a 70 50 61 72 Move(Parse *pPar
211f9 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 se, int iFrom, i
211fa 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67 nt iTo, int nReg
211fb 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 ){. int i;. st
211fc 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a ruct yColCache *
211fd 70 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69 p;. if( NEVER(i
211fe 46 72 6f 6d 3d 3d 69 54 6f 29 20 29 20 72 65 74 From==iTo) ) ret
211ff 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 urn;. sqlite3Vd
21200 62 65 41 64 64 4f 70 33 28 70 50 61 72 73 65 2d beAddOp3(pParse-
21201 3e 70 56 64 62 65 2c 20 4f 50 5f 4d 6f 76 65 2c >pVdbe, OP_Move,
21202 20 69 46 72 6f 6d 2c 20 69 54 6f 2c 20 6e 52 65 iFrom, iTo, nRe
21203 67 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 g);. for(i=0, p
21204 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 =pParse->aColCac
21205 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 he; i<SQLITE_N_C
21206 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b OLCACHE; i++, p+
21207 2b 29 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d 20 +){. int x =
21208 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69 66 28 p->iReg;. if(
21209 20 78 3e 3d 69 46 72 6f 6d 20 26 26 20 78 3c 69 x>=iFrom && x<i
2120a 46 72 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20 20 20 From+nReg ){.
2120b 20 20 20 70 2d 3e 69 52 65 67 20 2b 3d 20 69 54 p->iReg += iT
2120c 6f 2d 69 46 72 6f 6d 3b 0a 20 20 20 20 7d 0a 20 o-iFrom;. }.
2120d 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 }.}../*.** Gene
2120e 72 61 74 65 20 63 6f 64 65 20 74 6f 20 63 6f 70 rate code to cop
2120f 79 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 y content from r
21210 65 67 69 73 74 65 72 73 20 69 46 72 6f 6d 2e 2e egisters iFrom..
21211 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a .iFrom+nReg-1.**
21212 20 6f 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69 54 over to iTo..iT
21213 6f 2b 6e 52 65 67 2d 31 2e 0a 2a 2f 0a 53 51 4c o+nReg-1..*/.SQL
21214 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
21215 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21216 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 61 72 Copy(Parse *pPar
21217 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 se, int iFrom, i
21218 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67 nt iTo, int nReg
21219 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 ){. int i;. if
2121a 28 20 4e 45 56 45 52 28 69 46 72 6f 6d 3d 3d 69 ( NEVER(iFrom==i
2121b 54 6f 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 To) ) return;.
2121c 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 67 3b for(i=0; i<nReg;
2121d 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 i++){. sqlit
2121e 65 33 56 64 62 65 41 64 64 4f 70 32 28 70 50 61 e3VdbeAddOp2(pPa
2121f 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 43 rse->pVdbe, OP_C
21220 6f 70 79 2c 20 69 46 72 6f 6d 2b 69 2c 20 69 54 opy, iFrom+i, iT
21221 6f 2b 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a o+i);. }.}../*.
21222 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 ** Return true i
21223 66 20 61 6e 79 20 72 65 67 69 73 74 65 72 20 69 f any register i
21224 6e 20 74 68 65 20 72 61 6e 67 65 20 69 46 72 6f n the range iFro
21225 6d 2e 2e 69 54 6f 20 28 69 6e 63 6c 75 73 69 76 m..iTo (inclusiv
21226 65 29 0a 2a 2a 20 69 73 20 75 73 65 64 20 61 73 e).** is used as
21227 20 70 61 72 74 20 6f 66 20 74 68 65 20 63 6f 6c part of the col
21228 75 6d 6e 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 umn cache..*/.st
21229 61 74 69 63 20 69 6e 74 20 75 73 65 64 41 73 43 atic int usedAsC
2122a 6f 6c 75 6d 6e 43 61 63 68 65 28 50 61 72 73 65 olumnCache(Parse
2122b 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46 *pParse, int iF
2122c 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 29 7b 0a 20 rom, int iTo){.
2122d 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 int i;. struct
2122e 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 yColCache *p;.
2122f 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 for(i=0, p=pPar
21230 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 se->aColCache; i
21231 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 <SQLITE_N_COLCAC
21232 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 HE; i++, p++){.
21233 20 20 20 69 6e 74 20 72 20 3d 20 70 2d 3e 69 52 int r = p->iR
21234 65 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d 69 eg;. if( r>=i
21235 46 72 6f 6d 20 26 26 20 72 3c 3d 69 54 6f 20 29 From && r<=iTo )
21236 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 return 1;. }.
21237 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
21238 0a 2a 2a 20 49 66 20 74 68 65 20 6c 61 73 74 20 .** If the last
21239 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 64 65 instruction code
2123a 64 20 69 73 20 61 6e 20 65 70 68 65 6d 65 72 61 d is an ephemera
2123b 6c 20 63 6f 70 79 20 6f 66 20 61 6e 79 20 6f 66 l copy of any of
2123c 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 .** the register
2123d 73 20 69 6e 20 74 68 65 20 6e 52 65 67 20 72 65 s in the nReg re
2123e 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e gisters beginnin
2123f 67 20 77 69 74 68 20 69 52 65 67 2c 20 74 68 65 g with iReg, the
21240 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 74 68 65 n.** convert the
21241 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f last instructio
21242 6e 20 66 72 6f 6d 20 4f 50 5f 53 43 6f 70 79 20 n from OP_SCopy
21243 74 6f 20 4f 50 5f 43 6f 70 79 2e 0a 2a 2f 0a 53 to OP_Copy..*/.S
21244 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
21245 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 id sqlite3ExprHa
21246 72 64 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 rdCopy(Parse *pP
21247 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 2c 20 arse, int iReg,
21248 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 56 64 62 int nReg){. Vdb
21249 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62 65 eOp *pOp;. Vdbe
2124a 20 2a 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 *v;.. assert(
2124b 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c pParse->db->mall
2124c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 3b 0a 20 ocFailed==0 );.
2124d 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 v = pParse->pVd
2124e 62 65 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 be;. assert( v!
2124f 3d 30 20 29 3b 0a 20 20 70 4f 70 20 3d 20 73 71 =0 );. pOp = sq
21250 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 lite3VdbeGetOp(v
21251 2c 20 2d 31 29 3b 0a 20 20 61 73 73 65 72 74 28 , -1);. assert(
21252 20 70 4f 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 pOp!=0 );. if(
21253 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
21254 5f 53 43 6f 70 79 20 26 26 20 70 4f 70 2d 3e 70 _SCopy && pOp->p
21255 31 3e 3d 69 52 65 67 20 26 26 20 70 4f 70 2d 3e 1>=iReg && pOp->
21256 70 31 3c 69 52 65 67 2b 6e 52 65 67 20 29 7b 0a p1<iReg+nReg ){.
21257 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 pOp->opcode
21258 3d 20 4f 50 5f 43 6f 70 79 3b 0a 20 20 7d 0a 7d = OP_Copy;. }.}
21259 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
2125a 20 63 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74 code to store t
2125b 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
2125c 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20 iAlias-th alias
2125d 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 74 in register.** t
2125e 61 72 67 65 74 2e 20 20 54 68 65 20 66 69 72 73 arget. The firs
2125f 74 20 74 69 6d 65 20 74 68 69 73 20 69 73 20 63 t time this is c
21260 61 6c 6c 65 64 2c 20 70 45 78 70 72 20 69 73 20 alled, pExpr is
21261 65 76 61 6c 75 61 74 65 64 20 74 6f 20 63 6f 6d evaluated to com
21262 70 75 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75 pute.** the valu
21263 65 20 6f 66 20 74 68 65 20 61 6c 69 61 73 2e 20 e of the alias.
21264 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 The value is st
21265 6f 72 65 64 20 69 6e 20 61 6e 20 61 75 78 69 6c ored in an auxil
21266 69 61 72 79 20 72 65 67 69 73 74 65 72 0a 2a 2a iary register.**
21267 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 and the number
21268 6f 66 20 74 68 61 74 20 72 65 67 69 73 74 65 72 of that register
21269 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f is returned. O
2126a 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c n subsequent cal
2126b 6c 73 2c 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 ls,.** the regis
2126c 74 65 72 20 6e 75 6d 62 65 72 20 69 73 20 72 65 ter number is re
2126d 74 75 72 6e 65 64 20 77 69 74 68 6f 75 74 20 67 turned without g
2126e 65 6e 65 72 61 74 69 6e 67 20 61 6e 79 20 63 6f enerating any co
2126f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 de..**.** Note t
21270 68 61 74 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 hat in order for
21271 20 74 68 69 73 20 74 6f 20 77 6f 72 6b 2c 20 63 this to work, c
21272 6f 64 65 20 6d 75 73 74 20 62 65 20 67 65 6e 65 ode must be gene
21273 72 61 74 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 rated in the.**
21274 73 61 6d 65 20 6f 72 64 65 72 20 74 68 61 74 20 same order that
21275 69 74 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a it is executed..
21276 2a 2a 0a 2a 2a 20 41 6c 69 61 73 65 73 20 61 72 **.** Aliases ar
21277 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74 e numbered start
21278 69 6e 67 20 77 69 74 68 20 31 2e 20 20 53 6f 20 ing with 1. So
21279 69 41 6c 69 61 73 20 69 73 20 69 6e 20 74 68 65 iAlias is in the
2127a 20 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 31 20 74 range.** of 1 t
2127b 6f 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 o pParse->nAlias
2127c 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 0a 2a 2a inclusive. .**
2127d 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 .** pParse->aAli
2127e 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20 72 65 63 as[iAlias-1] rec
2127f 6f 72 64 73 20 74 68 65 20 72 65 67 69 73 74 65 ords the registe
21280 72 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20 74 r number where t
21281 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 he value.** of t
21282 68 65 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 he iAlias-th ali
21283 61 73 20 69 73 20 73 74 6f 72 65 64 2e 20 20 49 as is stored. I
21284 66 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 f zero, that mea
21285 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 ns that the.** a
21286 6c 69 61 73 20 68 61 73 20 6e 6f 74 20 79 65 74 lias has not yet
21287 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 2e 0a been computed..
21288 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f */.static int co
21289 64 65 41 6c 69 61 73 28 50 61 72 73 65 20 2a 70 deAlias(Parse *p
2128a 50 61 72 73 65 2c 20 69 6e 74 20 69 41 6c 69 61 Parse, int iAlia
2128b 73 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 s, Expr *pExpr,
2128c 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 23 69 66 int target){.#if
2128d 20 30 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 0. sqlite3 *db
2128e 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
2128f 20 69 6e 74 20 69 52 65 67 3b 0a 20 20 69 66 28 int iReg;. if(
21290 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 pParse->nAliasA
21291 6c 6c 6f 63 3c 70 50 61 72 73 65 2d 3e 6e 41 6c lloc<pParse->nAl
21292 69 61 73 20 29 7b 0a 20 20 20 20 70 50 61 72 73 ias ){. pPars
21293 65 2d 3e 61 41 6c 69 61 73 20 3d 20 73 71 6c 69 e->aAlias = sqli
21294 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 te3DbReallocOrFr
21295 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 ee(db, pParse->a
21296 41 6c 69 61 73 2c 0a 20 20 20 20 20 20 20 20 20 Alias,.
21297 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21298 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 sizeof(p
21299 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30 5d Parse->aAlias[0]
2129a 29 2a 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 )*pParse->nAlias
2129b 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 );. testcase
2129c 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
2129d 65 64 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 41 ed && pParse->nA
2129e 6c 69 61 73 41 6c 6c 6f 63 3e 30 20 29 3b 0a 20 liasAlloc>0 );.
2129f 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f if( db->mallo
212a0 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e cFailed ) return
212a1 20 30 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 0;. memset(&
212a2 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 70 pParse->aAlias[p
212a3 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c Parse->nAliasAll
212a4 6f 63 5d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 oc], 0,.
212a5 20 20 20 28 70 50 61 72 73 65 2d 3e 6e 41 6c 69 (pParse->nAli
212a6 61 73 2d 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 as-pParse->nAlia
212a7 73 41 6c 6c 6f 63 29 2a 73 69 7a 65 6f 66 28 70 sAlloc)*sizeof(p
212a8 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30 5d Parse->aAlias[0]
212a9 29 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e ));. pParse->
212aa 6e 41 6c 69 61 73 41 6c 6c 6f 63 20 3d 20 70 50 nAliasAlloc = pP
212ab 61 72 73 65 2d 3e 6e 41 6c 69 61 73 3b 0a 20 20 arse->nAlias;.
212ac 7d 0a 20 20 61 73 73 65 72 74 28 20 69 41 6c 69 }. assert( iAli
212ad 61 73 3e 30 20 26 26 20 69 41 6c 69 61 73 3c 3d as>0 && iAlias<=
212ae 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 pParse->nAlias )
212af 3b 0a 20 20 69 52 65 67 20 3d 20 70 50 61 72 73 ;. iReg = pPars
212b0 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 e->aAlias[iAlias
212b1 2d 31 5d 3b 0a 20 20 69 66 28 20 69 52 65 67 3d -1];. if( iReg=
212b2 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 =0 ){. if( pP
212b3 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 arse->iCacheLeve
212b4 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 52 65 l>0 ){. iRe
212b5 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 g = sqlite3ExprC
212b6 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 odeTarget(pParse
212b7 2c 20 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 , pExpr, target)
212b8 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
212b9 20 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 iReg = ++pPar
212ba 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 se->nMem;.
212bb 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 sqlite3ExprCode(
212bc 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 69 pParse, pExpr, i
212bd 52 65 67 29 3b 0a 20 20 20 20 20 20 70 50 61 72 Reg);. pPar
212be 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 se->aAlias[iAlia
212bf 73 2d 31 5d 20 3d 20 69 52 65 67 3b 0a 20 20 20 s-1] = iReg;.
212c0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
212c1 69 52 65 67 3b 0a 23 65 6c 73 65 0a 20 20 55 4e iReg;.#else. UN
212c2 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 USED_PARAMETER(i
212c3 41 6c 69 61 73 29 3b 0a 20 20 72 65 74 75 72 6e Alias);. return
212c4 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
212c5 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 Target(pParse, p
212c6 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 23 Expr, target);.#
212c7 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 endif.}../*.** G
212c8 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74 enerate code int
212c9 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 56 64 o the current Vd
212ca 62 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 be to evaluate t
212cb 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 he given.** expr
212cc 65 73 73 69 6f 6e 2e 20 20 41 74 74 65 6d 70 74 ession. Attempt
212cd 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65 to store the re
212ce 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74 65 sults in registe
212cf 72 20 22 74 61 72 67 65 74 22 2e 0a 2a 2a 20 52 r "target"..** R
212d0 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 eturn the regist
212d1 65 72 20 77 68 65 72 65 20 72 65 73 75 6c 74 73 er where results
212d2 20 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a are stored..**.
212d3 2a 2a 20 57 69 74 68 20 74 68 69 73 20 72 6f 75 ** With this rou
212d4 74 69 6e 65 2c 20 74 68 65 72 65 20 69 73 20 6e tine, there is n
212d5 6f 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 o guarantee that
212d6 20 72 65 73 75 6c 74 73 20 77 69 6c 6c 0a 2a 2a results will.**
212d7 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74 61 be stored in ta
212d8 72 67 65 74 2e 20 20 54 68 65 20 72 65 73 75 6c rget. The resul
212d9 74 20 6d 69 67 68 74 20 62 65 20 73 74 6f 72 65 t might be store
212da 64 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 0a d in some other.
212db 2a 2a 20 72 65 67 69 73 74 65 72 20 69 66 20 69 ** register if i
212dc 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 t is convenient
212dd 74 6f 20 64 6f 20 73 6f 2e 20 20 54 68 65 20 63 to do so. The c
212de 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a alling function.
212df 2a 2a 20 6d 75 73 74 20 63 68 65 63 6b 20 74 68 ** must check th
212e0 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e e return code an
212e1 64 20 6d 6f 76 65 20 74 68 65 20 72 65 73 75 6c d move the resul
212e2 74 73 20 74 6f 20 74 68 65 20 64 65 73 69 72 65 ts to the desire
212e3 64 0a 2a 2a 20 72 65 67 69 73 74 65 72 2e 0a 2a d.** register..*
212e4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
212e5 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
212e6 43 6f 64 65 54 61 72 67 65 74 28 50 61 72 73 65 CodeTarget(Parse
212e7 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
212e8 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 pExpr, int targe
212e9 74 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 t){. Vdbe *v =
212ea 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 pParse->pVdbe;
212eb 2f 2a 20 54 68 65 20 56 4d 20 75 6e 64 65 72 20 /* The VM under
212ec 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a construction */.
212ed 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 int op;
212ee 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
212ef 68 65 20 6f 70 63 6f 64 65 20 62 65 69 6e 67 20 he opcode being
212f0 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 coded */. int i
212f1 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 20 20 nReg = target;
212f2 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 /* Results
212f3 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 stored in regist
212f4 65 72 20 69 6e 52 65 67 20 2a 2f 0a 20 20 69 6e er inReg */. in
212f5 74 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b 20 t regFree1 = 0;
212f6 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f /* If no
212f7 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74 68 69 73 n-zero free this
212f8 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 temporary regis
212f9 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 ter */. int reg
212fa 46 72 65 65 32 20 3d 20 30 3b 20 20 20 20 20 20 Free2 = 0;
212fb 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 /* If non-zer
212fc 6f 20 66 72 65 65 20 74 68 69 73 20 74 65 6d 70 o free this temp
212fd 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 20 2a orary register *
212fe 2f 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 2c 20 /. int r1, r2,
212ff 72 33 2c 20 72 34 3b 20 20 20 20 20 20 20 2f 2a r3, r4; /*
21300 20 56 61 72 69 6f 75 73 20 72 65 67 69 73 74 65 Various registe
21301 72 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 73 r numbers */. s
21302 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
21303 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 54 68 65 20 rse->db; /* The
21304 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
21305 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ion */.. assert
21306 28 20 74 61 72 67 65 74 3e 30 20 26 26 20 74 61 ( target>0 && ta
21307 72 67 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d rget<=pParse->nM
21308 65 6d 20 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 em );. if( v==0
21309 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
2130a 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c pParse->db->mall
2130b 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 ocFailed );.
2130c 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 return 0;. }..
2130d 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 7b if( pExpr==0 ){
2130e 0a 20 20 20 20 6f 70 20 3d 20 54 4b 5f 4e 55 4c . op = TK_NUL
2130f 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 L;. }else{.
21310 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a op = pExpr->op;.
21311 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 6f 70 }. switch( op
21312 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ){. case TK_
21313 41 47 47 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 AGG_COLUMN: {.
21314 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 AggInfo *pAg
21315 67 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e 70 gInfo = pExpr->p
21316 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 AggInfo;. s
21317 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f truct AggInfo_co
21318 6c 20 2a 70 43 6f 6c 20 3d 20 26 70 41 67 67 49 l *pCol = &pAggI
21319 6e 66 6f 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d nfo->aCol[pExpr-
2131a 3e 69 41 67 67 5d 3b 0a 20 20 20 20 20 20 69 66 >iAgg];. if
2131b 28 20 21 70 41 67 67 49 6e 66 6f 2d 3e 64 69 72 ( !pAggInfo->dir
2131c 65 63 74 4d 6f 64 65 20 29 7b 0a 20 20 20 20 20 ectMode ){.
2131d 20 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d assert( pCol-
2131e 3e 69 4d 65 6d 3e 30 20 29 3b 0a 20 20 20 20 20 >iMem>0 );.
2131f 20 20 20 69 6e 52 65 67 20 3d 20 70 43 6f 6c 2d inReg = pCol-
21320 3e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 62 >iMem;. b
21321 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 reak;. }els
21322 65 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e e if( pAggInfo->
21323 75 73 65 53 6f 72 74 69 6e 67 49 64 78 20 29 7b useSortingIdx ){
21324 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
21325 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
21326 5f 43 6f 6c 75 6d 6e 2c 20 70 41 67 67 49 6e 66 _Column, pAggInf
21327 6f 2d 3e 73 6f 72 74 69 6e 67 49 64 78 2c 0a 20 o->sortingIdx,.
21328 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21329 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f pCo
2132a 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e l->iSorterColumn
2132b 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
2132c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
2132d 7d 0a 20 20 20 20 20 20 2f 2a 20 4f 74 68 65 72 }. /* Other
2132e 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 75 20 wise, fall thru
2132f 69 6e 74 6f 20 74 68 65 20 54 4b 5f 43 4f 4c 55 into the TK_COLU
21330 4d 4e 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d MN case */. }
21331 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c . case TK_COL
21332 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 UMN: {. if(
21333 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3c 30 pExpr->iTable<0
21334 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 ){. /* T
21335 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 his only happens
21336 20 77 68 65 6e 20 63 6f 64 69 6e 67 20 63 68 65 when coding che
21337 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a ck constraints *
21338 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 /. assert
21339 28 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 ( pParse->ckBase
2133a 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e >0 );. in
2133b 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f Reg = pExpr->iCo
2133c 6c 75 6d 6e 20 2b 20 70 50 61 72 73 65 2d 3e 63 lumn + pParse->c
2133d 6b 42 61 73 65 3b 0a 20 20 20 20 20 20 7d 65 6c kBase;. }el
2133e 73 65 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74 se{. test
2133f 63 61 73 65 28 20 28 70 45 78 70 72 2d 3e 66 6c case( (pExpr->fl
21340 61 67 73 20 26 20 45 50 5f 41 6e 79 41 66 66 29 ags & EP_AnyAff)
21341 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 !=0 );. i
21342 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 nReg = sqlite3Ex
21343 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 prCodeGetColumn(
21344 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
21345 54 61 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Tab,.
21346 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21347 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f pExpr->iCo
21348 6c 75 6d 6e 2c 20 70 45 78 70 72 2d 3e 69 54 61 lumn, pExpr->iTa
21349 62 6c 65 2c 20 74 61 72 67 65 74 2c 0a 20 20 20 ble, target,.
2134a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2134b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 pE
2134c 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f xpr->flags & EP_
2134d 41 6e 79 41 66 66 29 3b 0a 20 20 20 20 20 20 7d AnyAff);. }
2134e 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
2134f 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
21350 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 INTEGER: {.
21351 20 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20 codeInteger(v,
21352 70 45 78 70 72 2c 20 30 2c 20 74 61 72 67 65 74 pExpr, 0, target
21353 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
21354 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
21355 4b 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 K_FLOAT: {.
21356 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 assert( !ExprHa
21357 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c sProperty(pExpr,
21358 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b EP_IntValue) );
21359 0a 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 . codeReal(
2135a 76 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b v, pExpr->u.zTok
2135b 65 6e 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a en, 0, target);.
2135c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
2135d 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 }. case TK_S
2135e 54 52 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 61 TRING: {. a
2135f 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 ssert( !ExprHasP
21360 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
21361 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 P_IntValue) );.
21362 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21363 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 AddOp4(v, OP_Str
21364 69 6e 67 38 2c 20 30 2c 20 74 61 72 67 65 74 2c ing8, 0, target,
21365 20 30 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 0, pExpr->u.zTo
21366 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 ken, 0);. b
21367 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
21368 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a case TK_NULL: {.
21369 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
2136a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 eAddOp2(v, OP_Nu
2136b 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a ll, 0, target);.
2136c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
2136d 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
2136e 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 E_OMIT_BLOB_LITE
2136f 52 41 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f RAL. case TK_
21370 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 6e BLOB: {. in
21371 74 20 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 t n;. const
21372 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 char *z;.
21373 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 20 char *zBlob;.
21374 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 assert( !Expr
21375 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
21376 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 r, EP_IntValue)
21377 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
21378 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e pExpr->u.zToken
21379 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78 70 [0]=='x' || pExp
2137a 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d r->u.zToken[0]==
2137b 27 58 27 20 29 3b 0a 20 20 20 20 20 20 61 73 73 'X' );. ass
2137c 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 ert( pExpr->u.zT
2137d 6f 6b 65 6e 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b oken[1]=='\'' );
2137e 0a 20 20 20 20 20 20 7a 20 3d 20 26 70 45 78 70 . z = &pExp
2137f 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a r->u.zToken[2];.
21380 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 n = sqlite
21381 33 53 74 72 6c 65 6e 33 30 28 7a 29 20 2d 20 31 3Strlen30(z) - 1
21382 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
21383 7a 5b 6e 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 z[n]=='\'' );.
21384 20 20 20 20 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 zBlob = sqli
21385 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c te3HexToBlob(sql
21386 69 74 65 33 56 64 62 65 44 62 28 76 29 2c 20 7a ite3VdbeDb(v), z
21387 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 , n);. sqli
21388 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
21389 20 4f 50 5f 42 6c 6f 62 2c 20 6e 2f 32 2c 20 74 OP_Blob, n/2, t
2138a 61 72 67 65 74 2c 20 30 2c 20 7a 42 6c 6f 62 2c arget, 0, zBlob,
2138b 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 P4_DYNAMIC);.
2138c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
2138d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 .#endif. case
2138e 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a TK_VARIABLE: {.
2138f 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f VdbeOp *pO
21390 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 p;. assert(
21391 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 !ExprHasPropert
21392 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 y(pExpr, EP_IntV
21393 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 61 alue) );. a
21394 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e ssert( pExpr->u.
21395 7a 54 6f 6b 65 6e 21 3d 30 20 29 3b 0a 20 20 20 zToken!=0 );.
21396 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
21397 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 21 3d 30 ->u.zToken[0]!=0
21398 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 );. if( pE
21399 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d xpr->u.zToken[1]
2139a 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 ==0. &&
2139b 28 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 (pOp = sqlite3Vd
2139c 62 65 47 65 74 4f 70 28 76 2c 20 2d 31 29 29 2d beGetOp(v, -1))-
2139d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 61 72 69 >opcode==OP_Vari
2139e 61 62 6c 65 0a 20 20 20 20 20 20 20 20 20 26 26 able. &&
2139f 20 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 33 pOp->p1+pOp->p3
213a0 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 0a ==pExpr->iTable.
213a1 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70 2d && pOp-
213a2 3e 70 32 2b 70 4f 70 2d 3e 70 33 3d 3d 74 61 72 >p2+pOp->p3==tar
213a3 67 65 74 0a 20 20 20 20 20 20 20 20 20 26 26 20 get. &&
213a4 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 0a 20 20 20 pOp->p4.z==0.
213a5 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a ){. /*
213a6 20 49 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 If the previous
213a7 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 61 73 instruction was
213a8 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 70 a copy of the p
213a9 72 65 76 69 6f 75 73 20 75 6e 6e 61 6d 65 64 0a revious unnamed.
213aa 20 20 20 20 20 20 20 20 2a 2a 20 70 61 72 61 6d ** param
213ab 65 74 65 72 20 69 6e 74 6f 20 74 68 65 20 70 72 eter into the pr
213ac 65 76 69 6f 75 73 20 72 65 67 69 73 74 65 72 2c evious register,
213ad 20 74 68 65 6e 20 73 69 6d 70 6c 79 20 69 6e 63 then simply inc
213ae 72 65 6d 65 6e 74 20 74 68 65 0a 20 20 20 20 20 rement the.
213af 20 20 20 2a 2a 20 72 65 70 65 61 74 20 63 6f 75 ** repeat cou
213b0 6e 74 20 6f 6e 20 74 68 65 20 70 72 69 6f 72 20 nt on the prior
213b1 69 6e 73 74 72 75 63 74 69 6f 6e 20 72 61 74 68 instruction rath
213b2 65 72 20 74 68 61 6e 20 6d 61 6b 69 6e 67 20 61 er than making a
213b3 20 6e 65 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 new. **
213b4 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 20 20 20 instruction..
213b5 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
213b6 70 4f 70 2d 3e 70 33 2b 2b 3b 0a 20 20 20 20 20 pOp->p3++;.
213b7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
213b8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
213b9 33 28 76 2c 20 4f 50 5f 56 61 72 69 61 62 6c 65 3(v, OP_Variable
213ba 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c , pExpr->iTable,
213bb 20 74 61 72 67 65 74 2c 20 31 29 3b 0a 20 20 20 target, 1);.
213bc 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e if( pExpr->
213bd 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 21 3d 30 20 29 u.zToken[1]!=0 )
213be 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 {. sqli
213bf 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 te3VdbeChangeP4(
213c0 76 2c 20 2d 31 2c 20 70 45 78 70 72 2d 3e 75 2e v, -1, pExpr->u.
213c1 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 zToken, 0);.
213c2 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
213c3 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
213c4 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 47 . case TK_REG
213c5 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 69 ISTER: {. i
213c6 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 54 nReg = pExpr->iT
213c7 61 62 6c 65 3b 0a 20 20 20 20 20 20 62 72 65 61 able;. brea
213c8 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
213c9 65 20 54 4b 5f 41 53 3a 20 7b 0a 20 20 20 20 20 e TK_AS: {.
213ca 20 69 6e 52 65 67 20 3d 20 63 6f 64 65 41 6c 69 inReg = codeAli
213cb 61 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 as(pParse, pExpr
213cc 2d 3e 69 54 61 62 6c 65 2c 20 70 45 78 70 72 2d ->iTable, pExpr-
213cd 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b >pLeft, target);
213ce 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
213cf 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
213d0 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 20 TE_OMIT_CAST.
213d1 20 63 61 73 65 20 54 4b 5f 43 41 53 54 3a 20 7b case TK_CAST: {
213d2 0a 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 . /* Expres
213d3 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f 72 sions of the for
213d4 6d 3a 20 20 20 43 41 53 54 28 70 4c 65 66 74 20 m: CAST(pLeft
213d5 41 53 20 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20 20 AS token) */.
213d6 20 20 20 69 6e 74 20 61 66 66 2c 20 74 6f 5f 6f int aff, to_o
213d7 70 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d p;. inReg =
213d8 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
213d9 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 Target(pParse, p
213da 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 Expr->pLeft, tar
213db 67 65 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 get);. asse
213dc 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 rt( !ExprHasProp
213dd 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 erty(pExpr, EP_I
213de 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 ntValue) );.
213df 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 41 aff = sqlite3A
213e0 66 66 69 6e 69 74 79 54 79 70 65 28 70 45 78 70 ffinityType(pExp
213e1 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 r->u.zToken);.
213e2 20 20 20 20 74 6f 5f 6f 70 20 3d 20 61 66 66 20 to_op = aff
213e3 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 - SQLITE_AFF_TEX
213e4 54 20 2b 20 4f 50 5f 54 6f 54 65 78 74 3b 0a 20 T + OP_ToText;.
213e5 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f assert( to_
213e6 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 20 20 op==OP_ToText
213e7 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f || aff!=SQLITE_
213e8 41 46 46 5f 54 45 58 54 20 20 20 20 29 3b 0a 20 AFF_TEXT );.
213e9 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f assert( to_
213ea 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 op==OP_ToBlob
213eb 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f || aff!=SQLITE_
213ec 41 46 46 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a 20 AFF_NONE );.
213ed 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f assert( to_
213ee 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 op==OP_ToNumeric
213ef 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f || aff!=SQLITE_
213f0 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 AFF_NUMERIC );.
213f1 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f assert( to_
213f2 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 20 op==OP_ToInt
213f3 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f || aff!=SQLITE_
213f4 41 46 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 AFF_INTEGER );.
213f5 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f assert( to_
213f6 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 20 op==OP_ToReal
213f7 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f || aff!=SQLITE_
213f8 41 46 46 5f 52 45 41 4c 20 20 20 20 29 3b 0a 20 AFF_REAL );.
213f9 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 testcase( t
213fa 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 o_op==OP_ToText
213fb 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
213fc 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 e( to_op==OP_ToB
213fd 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20 74 65 73 lob );. tes
213fe 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 tcase( to_op==OP
213ff 5f 54 6f 4e 75 6d 65 72 69 63 20 29 3b 0a 20 20 _ToNumeric );.
21400 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f testcase( to
21401 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29 3b _op==OP_ToInt );
21402 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21403 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 to_op==OP_ToRea
21404 6c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 l );. if( i
21405 6e 52 65 67 21 3d 74 61 72 67 65 74 20 29 7b 0a nReg!=target ){.
21406 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
21407 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
21408 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 SCopy, inReg, ta
21409 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 rget);. i
2140a 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 nReg = target;.
2140b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
2140c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
2140d 2c 20 74 6f 5f 6f 70 2c 20 69 6e 52 65 67 29 3b , to_op, inReg);
2140e 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
2140f 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 usedAsColumnCac
21410 68 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 he(pParse, inReg
21411 2c 20 69 6e 52 65 67 29 20 29 3b 0a 20 20 20 20 , inReg) );.
21412 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
21413 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 heAffinityChange
21414 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 (pParse, inReg,
21415 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 1);. break;
21416 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a . }.#endif /*
21417 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 SQLITE_OMIT_CAS
21418 54 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b T */. case TK
21419 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _LT:. case TK
2141a 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _LE:. case TK
2141b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _GT:. case TK
2141c 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _GE:. case TK
2141d 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _NE:. case TK
2141e 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 _EQ: {. ass
2141f 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c ert( TK_LT==OP_L
21420 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 t );. asser
21421 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 t( TK_LE==OP_Le
21422 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
21423 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b TK_GT==OP_Gt );
21424 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
21425 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 K_GE==OP_Ge );.
21426 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
21427 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 EQ==OP_Eq );.
21428 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 assert( TK_NE
21429 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 ==OP_Ne );.
2142a 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
2142b 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65 K_LT );. te
2142c 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c stcase( op==TK_L
2142d 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 E );. testc
2142e 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 ase( op==TK_GT )
2142f 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21430 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 ( op==TK_GE );.
21431 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
21432 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 p==TK_EQ );.
21433 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
21434 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 63 TK_NE );. c
21435 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e odeCompareOperan
21436 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 ds(pParse, pExpr
21437 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 ->pLeft, &r1, &r
21438 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 egFree1,.
21439 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2143a 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 pExpr
2143b 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 ->pRight, &r2, &
2143c 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 regFree2);.
2143d 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 codeCompare(pPa
2143e 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
2143f 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 t, pExpr->pRight
21440 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20 , op,.
21441 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 r1, r2,
21442 69 6e 52 65 67 2c 20 53 51 4c 49 54 45 5f 53 54 inReg, SQLITE_ST
21443 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20 74 65 OREP2);. te
21444 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 stcase( regFree1
21445 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 ==0 );. tes
21446 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d tcase( regFree2=
21447 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 =0 );. brea
21448 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
21449 65 20 54 4b 5f 41 4e 44 3a 0a 20 20 20 20 63 61 e TK_AND:. ca
2144a 73 65 20 54 4b 5f 4f 52 3a 0a 20 20 20 20 63 61 se TK_OR:. ca
2144b 73 65 20 54 4b 5f 50 4c 55 53 3a 0a 20 20 20 20 se TK_PLUS:.
2144c 63 61 73 65 20 54 4b 5f 53 54 41 52 3a 0a 20 20 case TK_STAR:.
2144d 20 20 63 61 73 65 20 54 4b 5f 4d 49 4e 55 53 3a case TK_MINUS:
2144e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 4d . case TK_REM
2144f 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 :. case TK_BI
21450 54 41 4e 44 3a 0a 20 20 20 20 63 61 73 65 20 54 TAND:. case T
21451 4b 5f 42 49 54 4f 52 3a 0a 20 20 20 20 63 61 73 K_BITOR:. cas
21452 65 20 54 4b 5f 53 4c 41 53 48 3a 0a 20 20 20 20 e TK_SLASH:.
21453 63 61 73 65 20 54 4b 5f 4c 53 48 49 46 54 3a 0a case TK_LSHIFT:.
21454 20 20 20 20 63 61 73 65 20 54 4b 5f 52 53 48 49 case TK_RSHI
21455 46 54 3a 20 0a 20 20 20 20 63 61 73 65 20 54 4b FT: . case TK
21456 5f 43 4f 4e 43 41 54 3a 20 7b 0a 20 20 20 20 20 _CONCAT: {.
21457 20 61 73 73 65 72 74 28 20 54 4b 5f 41 4e 44 3d assert( TK_AND=
21458 3d 4f 50 5f 41 6e 64 20 29 3b 0a 20 20 20 20 20 =OP_And );.
21459 20 61 73 73 65 72 74 28 20 54 4b 5f 4f 52 3d 3d assert( TK_OR==
2145a 4f 50 5f 4f 72 20 29 3b 0a 20 20 20 20 20 20 61 OP_Or );. a
2145b 73 73 65 72 74 28 20 54 4b 5f 50 4c 55 53 3d 3d ssert( TK_PLUS==
2145c 4f 50 5f 41 64 64 20 29 3b 0a 20 20 20 20 20 20 OP_Add );.
2145d 61 73 73 65 72 74 28 20 54 4b 5f 4d 49 4e 55 53 assert( TK_MINUS
2145e 3d 3d 4f 50 5f 53 75 62 74 72 61 63 74 20 29 3b ==OP_Subtract );
2145f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
21460 4b 5f 52 45 4d 3d 3d 4f 50 5f 52 65 6d 61 69 6e K_REM==OP_Remain
21461 64 65 72 20 29 3b 0a 20 20 20 20 20 20 61 73 73 der );. ass
21462 65 72 74 28 20 54 4b 5f 42 49 54 41 4e 44 3d 3d ert( TK_BITAND==
21463 4f 50 5f 42 69 74 41 6e 64 20 29 3b 0a 20 20 20 OP_BitAnd );.
21464 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 42 49 assert( TK_BI
21465 54 4f 52 3d 3d 4f 50 5f 42 69 74 4f 72 20 29 3b TOR==OP_BitOr );
21466 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
21467 4b 5f 53 4c 41 53 48 3d 3d 4f 50 5f 44 69 76 69 K_SLASH==OP_Divi
21468 64 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 de );. asse
21469 72 74 28 20 54 4b 5f 4c 53 48 49 46 54 3d 3d 4f rt( TK_LSHIFT==O
2146a 50 5f 53 68 69 66 74 4c 65 66 74 20 29 3b 0a 20 P_ShiftLeft );.
2146b 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
2146c 52 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74 RSHIFT==OP_Shift
2146d 52 69 67 68 74 20 29 3b 0a 20 20 20 20 20 20 61 Right );. a
2146e 73 73 65 72 74 28 20 54 4b 5f 43 4f 4e 43 41 54 ssert( TK_CONCAT
2146f 3d 3d 4f 50 5f 43 6f 6e 63 61 74 20 29 3b 0a 20 ==OP_Concat );.
21470 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
21471 70 3d 3d 54 4b 5f 41 4e 44 20 29 3b 0a 20 20 20 p==TK_AND );.
21472 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
21473 3d 54 4b 5f 4f 52 20 29 3b 0a 20 20 20 20 20 20 =TK_OR );.
21474 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
21475 5f 50 4c 55 53 20 29 3b 0a 20 20 20 20 20 20 74 _PLUS );. t
21476 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
21477 4d 49 4e 55 53 20 29 3b 0a 20 20 20 20 20 20 74 MINUS );. t
21478 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
21479 52 45 4d 20 29 3b 0a 20 20 20 20 20 20 74 65 73 REM );. tes
2147a 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 tcase( op==TK_BI
2147b 54 41 4e 44 20 29 3b 0a 20 20 20 20 20 20 74 65 TAND );. te
2147c 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 stcase( op==TK_B
2147d 49 54 4f 52 20 29 3b 0a 20 20 20 20 20 20 74 65 ITOR );. te
2147e 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 53 stcase( op==TK_S
2147f 4c 41 53 48 20 29 3b 0a 20 20 20 20 20 20 74 65 LASH );. te
21480 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c stcase( op==TK_L
21481 53 48 49 46 54 20 29 3b 0a 20 20 20 20 20 20 74 SHIFT );. t
21482 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
21483 52 53 48 49 46 54 20 29 3b 0a 20 20 20 20 20 20 RSHIFT );.
21484 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
21485 5f 43 4f 4e 43 41 54 20 29 3b 0a 20 20 20 20 20 _CONCAT );.
21486 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 r1 = sqlite3Exp
21487 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 rCodeTemp(pParse
21488 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 , pExpr->pLeft,
21489 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 ®Free1);.
2148a 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 r2 = sqlite3Ex
2148b 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 prCodeTemp(pPars
2148c 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 e, pExpr->pRight
2148d 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 , ®Free2);.
2148e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
2148f 64 64 4f 70 33 28 76 2c 20 6f 70 2c 20 72 32 2c ddOp3(v, op, r2,
21490 20 72 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 r1, target);.
21491 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 testcase( re
21492 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 gFree1==0 );.
21493 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
21494 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 Free2==0 );.
21495 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
21496 20 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55 case TK_UMINU
21497 53 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20 S: {. Expr
21498 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e *pLeft = pExpr->
21499 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 61 73 73 pLeft;. ass
2149a 65 72 74 28 20 70 4c 65 66 74 20 29 3b 0a 20 20 ert( pLeft );.
2149b 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f if( pLeft->o
2149c 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 7b 0a 20 p==TK_FLOAT ){.
2149d 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 assert( !
2149e 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
2149f 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c pExpr, EP_IntVal
214a0 75 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 63 ue) );. c
214a1 6f 64 65 52 65 61 6c 28 76 2c 20 70 4c 65 66 74 odeReal(v, pLeft
214a2 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 31 2c 20 74 ->u.zToken, 1, t
214a3 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 arget);. }e
214a4 6c 73 65 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f lse if( pLeft->o
214a5 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b p==TK_INTEGER ){
214a6 0a 20 20 20 20 20 20 20 20 63 6f 64 65 49 6e 74 . codeInt
214a7 65 67 65 72 28 76 2c 20 70 4c 65 66 74 2c 20 31 eger(v, pLeft, 1
214a8 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
214a9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
214aa 72 65 67 46 72 65 65 31 20 3d 20 72 31 20 3d 20 regFree1 = r1 =
214ab 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 sqlite3GetTempRe
214ac 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 g(pParse);.
214ad 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
214ae 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 dOp2(v, OP_Integ
214af 65 72 2c 20 30 2c 20 72 31 29 3b 0a 20 20 20 20 er, 0, r1);.
214b0 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 r2 = sqlite3
214b1 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 ExprCodeTemp(pPa
214b2 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
214b3 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 t, ®Free2);.
214b4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
214b5 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 beAddOp3(v, OP_S
214b6 75 62 74 72 61 63 74 2c 20 72 32 2c 20 72 31 2c ubtract, r2, r1,
214b7 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
214b8 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 testcase( regF
214b9 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ree2==0 );.
214ba 20 7d 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d }. inReg =
214bb 20 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20 62 target;. b
214bc 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
214bd 63 61 73 65 20 54 4b 5f 42 49 54 4e 4f 54 3a 0a case TK_BITNOT:.
214be 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 3a case TK_NOT:
214bf 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 {. assert(
214c0 20 54 4b 5f 42 49 54 4e 4f 54 3d 3d 4f 50 5f 42 TK_BITNOT==OP_B
214c1 69 74 4e 6f 74 20 29 3b 0a 20 20 20 20 20 20 61 itNot );. a
214c2 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 3d 3d 4f ssert( TK_NOT==O
214c3 50 5f 4e 6f 74 20 29 3b 0a 20 20 20 20 20 20 74 P_Not );. t
214c4 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
214c5 42 49 54 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 BITNOT );.
214c6 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
214c7 5f 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 72 31 _NOT );. r1
214c8 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
214c9 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 deTemp(pParse, p
214ca 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 Expr->pLeft, &re
214cb 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74 gFree1);. t
214cc 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 estcase( regFree
214cd 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 6e 1==0 );. in
214ce 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 20 Reg = target;.
214cf 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
214d0 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c ddOp2(v, op, r1,
214d1 20 69 6e 52 65 67 29 3b 0a 20 20 20 20 20 20 62 inReg);. b
214d2 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
214d3 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a case TK_ISNULL:.
214d4 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e case TK_NOTN
214d5 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 ULL: {. int
214d6 20 61 64 64 72 3b 0a 20 20 20 20 20 20 61 73 73 addr;. ass
214d7 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d ert( TK_ISNULL==
214d8 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 20 OP_IsNull );.
214d9 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f assert( TK_NO
214da 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c TNULL==OP_NotNul
214db 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 l );. testc
214dc 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 ase( op==TK_ISNU
214dd 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 LL );. test
214de 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 case( op==TK_NOT
214df 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 73 71 NULL );. sq
214e0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
214e1 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 v, OP_Integer, 1
214e2 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
214e3 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 r1 = sqlite3Exp
214e4 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 rCodeTemp(pParse
214e5 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 , pExpr->pLeft,
214e6 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 ®Free1);.
214e7 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 testcase( regF
214e8 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ree1==0 );.
214e9 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 addr = sqlite3V
214ea 64 62 65 41 64 64 4f 70 31 28 76 2c 20 6f 70 2c dbeAddOp1(v, op,
214eb 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 r1);. sqli
214ec 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
214ed 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 74 61 72 67 OP_AddImm, targ
214ee 65 74 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 et, -1);. s
214ef 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
214f0 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 re(v, addr);.
214f1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
214f2 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f case TK_AGG_
214f3 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 FUNCTION: {.
214f4 20 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f AggInfo *pInfo
214f5 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e = pExpr->pAggIn
214f6 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 fo;. if( pI
214f7 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 nfo==0 ){.
214f8 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 assert( !ExprH
214f9 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
214fa 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 , EP_IntValue) )
214fb 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
214fc 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
214fd 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61 67 67 , "misuse of agg
214fe 72 65 67 61 74 65 3a 20 25 73 28 29 22 2c 20 70 regate: %s()", p
214ff 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b Expr->u.zToken);
21500 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
21501 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 49 inReg = pI
21502 6e 66 6f 2d 3e 61 46 75 6e 63 5b 70 45 78 70 72 nfo->aFunc[pExpr
21503 2d 3e 69 41 67 67 5d 2e 69 4d 65 6d 3b 0a 20 20 ->iAgg].iMem;.
21504 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
21505 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
21506 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a e TK_CONST_FUNC:
21507 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e . case TK_FUN
21508 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 45 CTION: {. E
21509 78 70 72 4c 69 73 74 20 2a 70 46 61 72 67 3b 20 xprList *pFarg;
2150a 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 /* List of
2150b 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 function argume
2150c 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 nts */. int
2150d 20 6e 46 61 72 67 3b 20 20 20 20 20 20 20 20 20 nFarg;
2150e 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
2150f 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 function argume
21510 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 46 75 6e nts */. Fun
21511 63 44 65 66 20 2a 70 44 65 66 3b 20 20 20 20 20 cDef *pDef;
21512 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 /* The funct
21513 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f ion definition o
21514 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 20 20 69 bject */. i
21515 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 20 20 20 nt nId;
21516 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 /* Length
21517 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 of the function
21518 6e 61 6d 65 20 69 6e 20 62 79 74 65 73 20 2a 2f name in bytes */
21519 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 . const cha
2151a 72 20 2a 7a 49 64 3b 20 20 20 20 20 20 20 2f 2a r *zId; /*
2151b 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 The function na
2151c 6d 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 me */. int
2151d 63 6f 6e 73 74 4d 61 73 6b 20 3d 20 30 3b 20 20 constMask = 0;
2151e 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 66 75 /* Mask of fu
2151f 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 nction arguments
21520 20 74 68 61 74 20 61 72 65 20 63 6f 6e 73 74 61 that are consta
21521 6e 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 nt */. int
21522 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
21523 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
21524 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 65 er */. u8 e
21525 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 20 20 20 nc = ENC(db);
21526 20 20 20 2f 2a 20 54 68 65 20 74 65 78 74 20 65 /* The text e
21527 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20 ncoding used by
21528 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f this database */
21529 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a . CollSeq *
2152a 70 43 6f 6c 6c 20 3d 20 30 3b 20 20 20 20 2f 2a pColl = 0; /*
2152b 20 41 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 A collating seq
2152c 75 65 6e 63 65 20 2a 2f 0a 0a 20 20 20 20 20 20 uence */..
2152d 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 assert( !ExprHas
2152e 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
2152f 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b EP_xIsSelect) );
21530 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21531 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 53 54 5f 46 55 op==TK_CONST_FU
21532 4e 43 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 NC );. test
21533 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 46 55 4e case( op==TK_FUN
21534 43 54 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 69 CTION );. i
21535 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f f( ExprHasAnyPro
21536 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
21537 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 TokenOnly) ){.
21538 20 20 20 20 20 20 70 46 61 72 67 20 3d 20 30 3b pFarg = 0;
21539 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
2153a 20 20 20 20 20 20 70 46 61 72 67 20 3d 20 70 45 pFarg = pE
2153b 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 xpr->x.pList;.
2153c 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 46 61 72 }. nFar
2153d 67 20 3d 20 70 46 61 72 67 20 3f 20 70 46 61 72 g = pFarg ? pFar
2153e 67 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 0a 20 20 g->nExpr : 0;.
2153f 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 assert( !Exp
21540 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 rHasProperty(pEx
21541 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 pr, EP_IntValue)
21542 20 29 3b 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 );. zId =
21543 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b pExpr->u.zToken;
21544 0a 20 20 20 20 20 20 6e 49 64 20 3d 20 73 71 6c . nId = sql
21545 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 49 64 ite3Strlen30(zId
21546 29 3b 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20 );. pDef =
21547 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 sqlite3FindFunct
21548 69 6f 6e 28 64 62 2c 20 7a 49 64 2c 20 6e 49 64 ion(db, zId, nId
21549 2c 20 6e 46 61 72 67 2c 20 65 6e 63 2c 20 30 29 , nFarg, enc, 0)
2154a 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 66 ;. if( pDef
2154b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ==0 ){. s
2154c 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
2154d 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 Parse, "unknown
2154e 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73 28 29 function: %.*s()
2154f 22 2c 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 ", nId, zId);.
21550 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21551 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 }. if( p
21552 46 61 72 67 20 29 7b 0a 20 20 20 20 20 20 20 20 Farg ){.
21553 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 r1 = sqlite3GetT
21554 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c empRange(pParse,
21555 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 20 20 nFarg);.
21556 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
21557 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 20 20 ePush(pParse);
21558 20 20 20 2f 2a 20 54 69 63 6b 65 74 20 32 65 61 /* Ticket 2ea
21559 32 34 32 35 64 33 34 62 65 20 2a 2f 0a 20 20 20 2425d34be */.
2155a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
2155b 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 CodeExprList(pPa
2155c 72 73 65 2c 20 70 46 61 72 67 2c 20 72 31 2c 20 rse, pFarg, r1,
2155d 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 1);. sqli
2155e 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 te3ExprCachePop(
2155f 70 50 61 72 73 65 2c 20 31 29 3b 20 20 20 2f 2a pParse, 1); /*
21560 20 54 69 63 6b 65 74 20 32 65 61 32 34 32 35 64 Ticket 2ea2425d
21561 33 34 62 65 20 2a 2f 0a 20 20 20 20 20 20 7d 65 34be */. }e
21562 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 31 20 lse{. r1
21563 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 = 0;. }.#if
21564 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
21565 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
21566 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 /* Possibly
21567 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 66 75 6e overload the fun
21568 63 74 69 6f 6e 20 69 66 20 74 68 65 20 66 69 72 ction if the fir
21569 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 20 st argument is.
2156a 20 20 20 20 20 2a 2a 20 61 20 76 69 72 74 75 61 ** a virtua
2156b 6c 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 2e 0a l table column..
2156c 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
2156d 2a 20 46 6f 72 20 69 6e 66 69 78 20 66 75 6e 63 * For infix func
2156e 74 69 6f 6e 73 20 28 4c 49 4b 45 2c 20 47 4c 4f tions (LIKE, GLO
2156f 42 2c 20 52 45 47 45 58 50 2c 20 61 6e 64 20 4d B, REGEXP, and M
21570 41 54 43 48 29 20 75 73 65 20 74 68 65 0a 20 20 ATCH) use the.
21571 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64 20 61 72 ** second ar
21572 67 75 6d 65 6e 74 2c 20 6e 6f 74 20 74 68 65 20 gument, not the
21573 66 69 72 73 74 2c 20 61 73 20 74 68 65 20 61 72 first, as the ar
21574 67 75 6d 65 6e 74 20 74 6f 20 74 65 73 74 20 74 gument to test t
21575 6f 0a 20 20 20 20 20 20 2a 2a 20 73 65 65 20 69 o. ** see i
21576 66 20 69 74 20 69 73 20 61 20 63 6f 6c 75 6d 6e f it is a column
21577 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61 in a virtual ta
21578 62 6c 65 2e 20 20 54 68 69 73 20 69 73 20 64 6f ble. This is do
21579 6e 65 20 62 65 63 61 75 73 65 0a 20 20 20 20 20 ne because.
2157a 20 2a 2a 20 74 68 65 20 6c 65 66 74 20 6f 70 65 ** the left ope
2157b 72 61 6e 64 20 6f 66 20 69 6e 66 69 78 20 66 75 rand of infix fu
2157c 6e 63 74 69 6f 6e 73 20 28 74 68 65 20 6f 70 65 nctions (the ope
2157d 72 61 6e 64 20 77 65 20 77 61 6e 74 20 74 6f 0a rand we want to.
2157e 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 72 6f 6c ** control
2157f 20 6f 76 65 72 6c 6f 61 64 69 6e 67 29 20 65 6e overloading) en
21580 64 73 20 75 70 20 61 73 20 74 68 65 20 73 65 63 ds up as the sec
21581 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ond argument to
21582 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 66 75 6e the. ** fun
21583 63 74 69 6f 6e 2e 20 20 54 68 65 20 65 78 70 72 ction. The expr
21584 65 73 73 69 6f 6e 20 22 41 20 67 6c 6f 62 20 42 ession "A glob B
21585 22 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 " is equivalent
21586 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 22 67 6c to . ** "gl
21587 6f 62 28 42 2c 41 29 2e 20 20 57 65 20 77 61 6e ob(B,A). We wan
21588 74 20 74 6f 20 75 73 65 20 74 68 65 20 41 20 69 t to use the A i
21589 6e 20 22 41 20 67 6c 6f 62 20 42 22 20 74 6f 20 n "A glob B" to
2158a 74 65 73 74 0a 20 20 20 20 20 20 2a 2a 20 66 6f test. ** fo
2158b 72 20 66 75 6e 63 74 69 6f 6e 20 6f 76 65 72 6c r function overl
2158c 6f 61 64 69 6e 67 2e 20 20 42 75 74 20 77 65 20 oading. But we
2158d 75 73 65 20 74 68 65 20 42 20 74 65 72 6d 20 69 use the B term i
2158e 6e 20 22 67 6c 6f 62 28 42 2c 41 29 22 2e 0a 20 n "glob(B,A)"..
2158f 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
21590 28 20 6e 46 61 72 67 3e 3d 32 20 26 26 20 28 70 ( nFarg>=2 && (p
21591 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 Expr->flags & EP
21592 5f 49 6e 66 69 78 46 75 6e 63 29 20 29 7b 0a 20 _InfixFunc) ){.
21593 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 pDef = sq
21594 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 lite3VtabOverloa
21595 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 44 dFunction(db, pD
21596 65 66 2c 20 6e 46 61 72 67 2c 20 70 46 61 72 67 ef, nFarg, pFarg
21597 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 29 3b 0a 20 ->a[1].pExpr);.
21598 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e }else if( n
21599 46 61 72 67 3e 30 20 29 7b 0a 20 20 20 20 20 20 Farg>0 ){.
2159a 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 pDef = sqlite3
2159b 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 VtabOverloadFunc
2159c 74 69 6f 6e 28 64 62 2c 20 70 44 65 66 2c 20 6e tion(db, pDef, n
2159d 46 61 72 67 2c 20 70 46 61 72 67 2d 3e 61 5b 30 Farg, pFarg->a[0
2159e 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 ].pExpr);.
2159f 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 66 }.#endif. f
215a0 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 61 72 67 3b or(i=0; i<nFarg;
215a1 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 i++){. i
215a2 66 28 20 69 3c 33 32 20 26 26 20 73 71 6c 69 74 f( i<32 && sqlit
215a3 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 e3ExprIsConstant
215a4 28 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 78 (pFarg->a[i].pEx
215a5 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 pr) ){.
215a6 20 63 6f 6e 73 74 4d 61 73 6b 20 7c 3d 20 28 31 constMask |= (1
215a7 3c 3c 69 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a <<i);. }.
215a8 20 20 20 20 20 20 20 20 69 66 28 20 28 70 44 65 if( (pDe
215a9 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f->flags & SQLIT
215aa 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 29 E_FUNC_NEEDCOLL)
215ab 21 3d 30 20 26 26 20 21 70 43 6f 6c 6c 20 29 7b !=0 && !pColl ){
215ac 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 6c . pColl
215ad 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
215ae 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 46 llSeq(pParse, pF
215af 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 arg->a[i].pExpr)
215b0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
215b1 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 44 }. if( pD
215b2 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 ef->flags & SQLI
215b3 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c TE_FUNC_NEEDCOLL
215b4 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
215b5 21 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20 3d !pColl ) pColl =
215b6 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 20 db->pDfltColl;
215b7 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
215b8 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
215b9 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 30 2c 20 _CollSeq, 0, 0,
215ba 30 2c 20 28 63 68 61 72 20 2a 29 70 43 6f 6c 6c 0, (char *)pColl
215bb 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 , P4_COLLSEQ);.
215bc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
215bd 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
215be 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 63 , OP_Function, c
215bf 6f 6e 73 74 4d 61 73 6b 2c 20 72 31 2c 20 74 61 onstMask, r1, ta
215c0 72 67 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20 rget,.
215c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 (c
215c2 68 61 72 2a 29 70 44 65 66 2c 20 50 34 5f 46 55 har*)pDef, P4_FU
215c3 4e 43 44 45 46 29 3b 0a 20 20 20 20 20 20 73 71 NCDEF);. sq
215c4 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
215c5 35 28 76 2c 20 28 75 38 29 6e 46 61 72 67 29 3b 5(v, (u8)nFarg);
215c6 0a 20 20 20 20 20 20 69 66 28 20 6e 46 61 72 67 . if( nFarg
215c7 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
215c8 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 te3ReleaseTempRa
215c9 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c 20 nge(pParse, r1,
215ca 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 20 7d 0a nFarg);. }.
215cb 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
215cc 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 rCacheAffinityCh
215cd 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c ange(pParse, r1,
215ce 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 20 62 nFarg);. b
215cf 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e reak;. }.#ifn
215d0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
215d1 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 SUBQUERY. cas
215d2 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20 e TK_EXISTS:.
215d3 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a case TK_SELECT:
215d4 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 {. testcas
215d5 65 28 20 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 e( op==TK_EXISTS
215d6 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
215d7 73 65 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 se( op==TK_SELEC
215d8 54 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 T );. sqlit
215d9 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 e3CodeSubselect(
215da 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 30 pParse, pExpr, 0
215db 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 6e 52 65 , 0);. inRe
215dc 67 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 g = pExpr->iColu
215dd 6d 6e 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b mn;. break;
215de 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
215df 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69 TK_IN: {. i
215e0 6e 74 20 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 30 nt rNotFound = 0
215e1 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 4d 61 79 ;. int rMay
215e2 48 61 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 HaveNull = 0;.
215e3 20 20 20 20 69 6e 74 20 6a 32 2c 20 6a 33 2c 20 int j2, j3,
215e4 6a 34 2c 20 6a 35 3b 0a 20 20 20 20 20 20 63 68 j4, j5;. ch
215e5 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 ar affinity;.
215e6 20 20 20 69 6e 74 20 65 54 79 70 65 3b 0a 0a 20 int eType;..
215e7 20 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d VdbeNoopCom
215e8 6d 65 6e 74 28 28 76 2c 20 22 62 65 67 69 6e 20 ment((v, "begin
215e9 49 4e 20 65 78 70 72 20 72 25 64 22 2c 20 74 61 IN expr r%d", ta
215ea 72 67 65 74 29 29 3b 0a 20 20 20 20 20 20 65 54 rget));. eT
215eb 79 70 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e ype = sqlite3Fin
215ec 64 49 6e 49 6e 64 65 78 28 70 50 61 72 73 65 2c dInIndex(pParse,
215ed 20 70 45 78 70 72 2c 20 26 72 4d 61 79 48 61 76 pExpr, &rMayHav
215ee 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 69 66 eNull);. if
215ef 28 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29 ( rMayHaveNull )
215f0 7b 0a 20 20 20 20 20 20 20 20 72 4e 6f 74 46 6f {. rNotFo
215f1 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e und = ++pParse->
215f2 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 nMem;. }..
215f3 20 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f /* Figure o
215f4 75 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 ut the affinity
215f5 74 6f 20 75 73 65 20 74 6f 20 63 72 65 61 74 65 to use to create
215f6 20 61 20 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 a key from the
215f7 72 65 73 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a results. **
215f8 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 of the expressi
215f9 6f 6e 2e 20 61 66 66 69 6e 69 74 79 53 74 72 20 on. affinityStr
215fa 73 74 6f 72 65 73 20 61 20 73 74 61 74 69 63 20 stores a static
215fb 73 74 72 69 6e 67 20 73 75 69 74 61 62 6c 65 20 string suitable
215fc 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 50 34 20 for. ** P4
215fd 6f 66 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 of OP_MakeRecord
215fe 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
215ff 20 61 66 66 69 6e 69 74 79 20 3d 20 63 6f 6d 70 affinity = comp
21600 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 arisonAffinity(p
21601 45 78 70 72 29 3b 0a 0a 0a 20 20 20 20 20 20 2f Expr);... /
21602 2a 20 43 6f 64 65 20 74 68 65 20 3c 65 78 70 72 * Code the <expr
21603 3e 20 66 72 6f 6d 20 22 3c 65 78 70 72 3e 20 49 > from "<expr> I
21604 4e 20 28 2e 2e 2e 29 22 2e 20 54 68 65 20 74 65 N (...)". The te
21605 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 0a 20 20 mporary table.
21606 20 20 20 20 2a 2a 20 70 45 78 70 72 2d 3e 69 54 ** pExpr->iT
21607 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 able contains th
21608 65 20 76 61 6c 75 65 73 20 74 68 61 74 20 6d 61 e values that ma
21609 6b 65 20 75 70 20 74 68 65 20 28 2e 2e 2e 29 20 ke up the (...)
2160a 73 65 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 set.. */.
2160b 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
2160c 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65 29 achePush(pParse)
2160d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
2160e 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 xprCode(pParse,
2160f 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 pExpr->pLeft, ta
21610 72 67 65 74 29 3b 0a 20 20 20 20 20 20 6a 32 20 rget);. j2
21611 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 = sqlite3VdbeAdd
21612 4f 70 31 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c Op1(v, OP_IsNull
21613 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
21614 20 69 66 28 20 65 54 79 70 65 3d 3d 49 4e 5f 49 if( eType==IN_I
21615 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b 0a 20 20 NDEX_ROWID ){.
21616 20 20 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 j3 = sqlit
21617 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 e3VdbeAddOp1(v,
21618 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 74 61 OP_MustBeInt, ta
21619 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6a rget);. j
2161a 34 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 4 = sqlite3VdbeA
2161b 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 ddOp3(v, OP_NotE
2161c 78 69 73 74 73 2c 20 70 45 78 70 72 2d 3e 69 54 xists, pExpr->iT
2161d 61 62 6c 65 2c 20 30 2c 20 74 61 72 67 65 74 29 able, 0, target)
2161e 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
2161f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
21620 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 61 P_Integer, 1, ta
21621 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6a rget);. j
21622 35 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 5 = sqlite3VdbeA
21623 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f ddOp0(v, OP_Goto
21624 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
21625 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 e3VdbeJumpHere(v
21626 2c 20 6a 33 29 3b 0a 20 20 20 20 20 20 20 20 73 , j3);. s
21627 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
21628 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20 20 20 20 re(v, j4);.
21629 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
2162a 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 dOp2(v, OP_Integ
2162b 65 72 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a er, 0, target);.
2162c 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
2162d 20 20 20 20 20 72 32 20 3d 20 72 65 67 46 72 65 r2 = regFre
2162e 65 32 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 e2 = sqlite3GetT
2162f 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a empReg(pParse);.
21630 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 72 65 61 . /* Crea
21631 74 65 20 61 20 72 65 63 6f 72 64 20 61 6e 64 20 te a record and
21632 74 65 73 74 20 66 6f 72 20 73 65 74 20 6d 65 6d test for set mem
21633 62 65 72 73 68 69 70 2e 20 49 66 20 74 68 65 20 bership. If the
21634 73 65 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 set contains.
21635 20 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 ** the valu
21636 65 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 e, then jump to
21637 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 74 the end of the t
21638 65 73 74 20 63 6f 64 65 2e 20 54 68 65 20 74 61 est code. The ta
21639 72 67 65 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 rget. **
2163a 72 65 67 69 73 74 65 72 20 73 74 69 6c 6c 20 63 register still c
2163b 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 72 75 65 ontains the true
2163c 20 28 31 29 20 76 61 6c 75 65 20 77 72 69 74 74 (1) value writt
2163d 65 6e 20 74 6f 20 69 74 20 65 61 72 6c 69 65 72 en to it earlier
2163e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
2163f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21640 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b AddOp4(v, OP_Mak
21641 65 52 65 63 6f 72 64 2c 20 74 61 72 67 65 74 2c eRecord, target,
21642 20 31 2c 20 72 32 2c 20 26 61 66 66 69 6e 69 74 1, r2, &affinit
21643 79 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 y, 1);. s
21644 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
21645 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
21646 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 1, target);.
21647 20 20 20 20 6a 35 20 3d 20 73 71 6c 69 74 65 33 j5 = sqlite3
21648 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
21649 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d 3e 69 _Found, pExpr->i
2164a 54 61 62 6c 65 2c 20 30 2c 20 72 32 29 3b 0a 0a Table, 0, r2);..
2164b 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 /* If th
2164c 65 20 73 65 74 20 6d 65 6d 62 65 72 73 68 69 70 e set membership
2164d 20 74 65 73 74 20 66 61 69 6c 73 2c 20 74 68 65 test fails, the
2164e 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 n the result of
2164f 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 the . **
21650 22 78 20 49 4e 20 28 2e 2e 2e 29 22 20 65 78 70 "x IN (...)" exp
21651 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 ression must be
21652 65 69 74 68 65 72 20 30 20 6f 72 20 4e 55 4c 4c either 0 or NULL
21653 2e 20 49 66 20 74 68 65 20 73 65 74 0a 20 20 20 . If the set.
21654 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 ** contains
21655 20 6e 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2c no NULL values,
21656 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 then the result
21657 20 69 73 20 30 2e 20 49 66 20 74 68 65 20 73 65 is 0. If the se
21658 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f t . ** co
21659 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f ntains one or mo
2165a 72 65 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2c 20 re NULL values,
2165b 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 then the result
2165c 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a of the. *
2165d 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 * expression is
2165e 61 6c 73 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 also NULL..
2165f 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 */. if
21660 28 20 72 4e 6f 74 46 6f 75 6e 64 3d 3d 30 20 29 ( rNotFound==0 )
21661 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 {. /* T
21662 68 69 73 20 62 72 61 6e 63 68 20 72 75 6e 73 20 his branch runs
21663 69 66 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 61 if it is known a
21664 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 28 t compile time (
21665 6e 6f 77 29 20 74 68 61 74 20 0a 20 20 20 20 20 now) that .
21666 20 20 20 20 20 2a 2a 20 74 68 65 20 73 65 74 20 ** the set
21667 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c contains no NULL
21668 20 76 61 6c 75 65 73 2e 20 54 68 69 73 20 68 61 values. This ha
21669 70 70 65 6e 73 20 61 73 20 74 68 65 20 72 65 73 ppens as the res
2166a 75 6c 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ult. **
2166b 20 6f 66 20 61 20 22 4e 4f 54 20 4e 55 4c 4c 22 of a "NOT NULL"
2166c 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 74 constraint in t
2166d 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 he database sche
2166e 6d 61 2e 20 4e 6f 20 6e 65 65 64 0a 20 20 20 20 ma. No need.
2166f 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 65 73 74 ** to test
21670 20 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74 the data struct
21671 75 72 65 20 61 74 20 72 75 6e 74 69 6d 65 20 69 ure at runtime i
21672 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 n this case..
21673 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
21674 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21675 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 ddOp2(v, OP_Inte
21676 67 65 72 2c 20 30 2c 20 74 61 72 67 65 74 29 3b ger, 0, target);
21677 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
21678 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 /* Thi
21679 73 20 62 6c 6f 63 6b 20 70 6f 70 75 6c 61 74 65 s block populate
2167a 73 20 74 68 65 20 72 4e 6f 74 46 6f 75 6e 64 20 s the rNotFound
2167b 72 65 67 69 73 74 65 72 20 77 69 74 68 20 65 69 register with ei
2167c 74 68 65 72 20 4e 55 4c 4c 0a 20 20 20 20 20 20 ther NULL.
2167d 20 20 20 20 2a 2a 20 6f 72 20 30 20 28 61 6e 20 ** or 0 (an
2167e 69 6e 74 65 67 65 72 20 76 61 6c 75 65 29 2e 20 integer value).
2167f 49 66 20 74 68 65 20 64 61 74 61 20 73 74 72 75 If the data stru
21680 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6f cture contains o
21681 6e 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 ne. **
21682 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 73 2c 20 74 or more NULLs, t
21683 68 65 6e 20 73 65 74 20 72 4e 6f 74 46 6f 75 6e hen set rNotFoun
21684 64 20 74 6f 20 4e 55 4c 4c 2e 20 4f 74 68 65 72 d to NULL. Other
21685 77 69 73 65 2c 20 73 65 74 20 69 74 0a 20 20 20 wise, set it.
21686 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 30 2e 20 ** to 0.
21687 49 66 20 72 65 67 69 73 74 65 72 20 72 4d 61 79 If register rMay
21688 48 61 76 65 4e 75 6c 6c 20 69 73 20 61 6c 72 65 HaveNull is alre
21689 61 64 79 20 73 65 74 20 74 6f 20 73 6f 6d 65 20 ady set to some
2168a 76 61 6c 75 65 0a 20 20 20 20 20 20 20 20 20 20 value.
2168b 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 4e 55 ** other than NU
2168c 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 74 65 73 LL, then the tes
2168d 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 t has already be
2168e 65 6e 20 72 75 6e 20 61 6e 64 20 0a 20 20 20 20 en run and .
2168f 20 20 20 20 20 20 2a 2a 20 72 4e 6f 74 46 6f 75 ** rNotFou
21690 6e 64 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f nd is already po
21691 70 75 6c 61 74 65 64 2e 0a 20 20 20 20 20 20 20 pulated..
21692 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 */.
21693 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 static const cha
21694 72 20 6e 75 6c 6c 52 65 63 6f 72 64 5b 5d 20 3d r nullRecord[] =
21695 20 7b 20 30 78 30 32 2c 20 30 78 30 30 20 7d 3b { 0x02, 0x00 };
21696 0a 20 20 20 20 20 20 20 20 20 20 6a 33 20 3d 20 . j3 =
21697 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21698 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 1(v, OP_NotNull,
21699 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a rMayHaveNull);.
2169a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
2169b 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
2169c 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4e 6f 74 46 P_Null, 0, rNotF
2169d 6f 75 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 20 ound);.
2169e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
2169f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20 32 p4(v, OP_Blob, 2
216a0 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 , rMayHaveNull,
216a1 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 0, .
216a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
216a3 20 6e 75 6c 6c 52 65 63 6f 72 64 2c 20 50 34 5f nullRecord, P4_
216a4 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 STATIC);.
216a5 20 20 20 6a 34 20 3d 20 73 71 6c 69 74 65 33 56 j4 = sqlite3V
216a6 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
216a7 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54 Found, pExpr->iT
216a8 61 62 6c 65 2c 20 30 2c 20 72 4d 61 79 48 61 76 able, 0, rMayHav
216a9 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 eNull);.
216aa 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
216ab 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 Op2(v, OP_Intege
216ac 72 2c 20 30 2c 20 72 4e 6f 74 46 6f 75 6e 64 29 r, 0, rNotFound)
216ad 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
216ae 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
216af 76 2c 20 6a 34 29 3b 0a 20 20 20 20 20 20 20 20 v, j4);.
216b0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
216b1 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 0a 20 pHere(v, j3);..
216b2 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 /* Copy
216b3 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 the value of re
216b4 67 69 73 74 65 72 20 72 4e 6f 74 46 6f 75 6e 64 gister rNotFound
216b5 20 28 77 68 69 63 68 20 69 73 20 65 69 74 68 65 (which is eithe
216b6 72 20 4e 55 4c 4c 20 6f 72 20 30 29 0a 20 20 20 r NULL or 0).
216b7 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 ** into t
216b8 68 65 20 74 61 72 67 65 74 20 72 65 67 69 73 74 he target regist
216b9 65 72 2e 20 54 68 69 73 20 77 69 6c 6c 20 62 65 er. This will be
216ba 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 the result of t
216bb 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 he. **
216bc 65 78 70 72 65 73 73 69 6f 6e 2e 0a 20 20 20 20 expression..
216bd 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
216be 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
216bf 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c dOp2(v, OP_Copy,
216c0 20 72 4e 6f 74 46 6f 75 6e 64 2c 20 74 61 72 67 rNotFound, targ
216c1 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 et);. }.
216c2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
216c3 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
216c4 28 76 2c 20 6a 32 29 3b 0a 20 20 20 20 20 20 73 (v, j2);. s
216c5 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
216c6 72 65 28 76 2c 20 6a 35 29 3b 0a 20 20 20 20 20 re(v, j5);.
216c7 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
216c8 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b ePop(pParse, 1);
216c9 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 . VdbeComme
216ca 6e 74 28 28 76 2c 20 22 65 6e 64 20 49 4e 20 65 nt((v, "end IN e
216cb 78 70 72 20 72 25 64 22 2c 20 74 61 72 67 65 74 xpr r%d", target
216cc 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b ));. break;
216cd 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
216ce 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 /*. ** x
216cf 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44 20 7a BETWEEN y AND z
216d0 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
216d1 68 69 73 20 69 73 20 65 71 75 69 76 61 6c 65 6e his is equivalen
216d2 74 20 74 6f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 t to. **.
216d3 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44 20 78 ** x>=y AND x
216d4 3c 3d 7a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a <=z. **. *
216d5 2a 20 58 20 69 73 20 73 74 6f 72 65 64 20 69 6e * X is stored in
216d6 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2e 0a 20 pExpr->pLeft..
216d7 20 20 20 2a 2a 20 59 20 69 73 20 73 74 6f 72 65 ** Y is store
216d8 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 d in pExpr->pLis
216d9 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2e 0a 20 t->a[0].pExpr..
216da 20 20 20 2a 2a 20 5a 20 69 73 20 73 74 6f 72 65 ** Z is store
216db 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 d in pExpr->pLis
216dc 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 2e 0a 20 t->a[1].pExpr..
216dd 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 */. case T
216de 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 K_BETWEEN: {.
216df 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d Expr *pLeft =
216e0 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 pExpr->pLeft;.
216e1 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 struct Expr
216e2 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 49 74 65 List_item *pLIte
216e3 6d 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 m = pExpr->x.pLi
216e4 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 45 78 70 st->a;. Exp
216e5 72 20 2a 70 52 69 67 68 74 20 3d 20 70 4c 49 74 r *pRight = pLIt
216e6 65 6d 2d 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20 em->pExpr;..
216e7 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 codeCompareOpe
216e8 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 4c rands(pParse, pL
216e9 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67 46 72 eft, &r1, ®Fr
216ea 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ee1,.
216eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
216ec 20 20 20 20 20 20 20 70 52 69 67 68 74 2c 20 26 pRight, &
216ed 72 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a r2, ®Free2);.
216ee 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
216ef 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 regFree1==0 );.
216f0 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
216f1 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 egFree2==0 );.
216f2 20 20 20 20 72 33 20 3d 20 73 71 6c 69 74 65 33 r3 = sqlite3
216f3 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 GetTempReg(pPars
216f4 65 29 3b 0a 20 20 20 20 20 20 72 34 20 3d 20 73 e);. r4 = s
216f5 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 qlite3GetTempReg
216f6 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 (pParse);.
216f7 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 codeCompare(pPar
216f8 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 se, pLeft, pRigh
216f9 74 2c 20 4f 50 5f 47 65 2c 0a 20 20 20 20 20 20 t, OP_Ge,.
216fa 20 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 r1,
216fb 72 32 2c 20 72 33 2c 20 53 51 4c 49 54 45 5f 53 r2, r3, SQLITE_S
216fc 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20 70 TOREP2);. p
216fd 4c 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 70 LItem++;. p
216fe 52 69 67 68 74 20 3d 20 70 4c 49 74 65 6d 2d 3e Right = pLItem->
216ff 70 45 78 70 72 3b 0a 20 20 20 20 20 20 73 71 6c pExpr;. sql
21700 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
21701 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 eg(pParse, regFr
21702 65 65 32 29 3b 0a 20 20 20 20 20 20 72 32 20 3d ee2);. r2 =
21703 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21704 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 52 69 Temp(pParse, pRi
21705 67 68 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b ght, ®Free2);
21706 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21707 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a regFree2==0 );.
21708 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 codeCompar
21709 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c e(pParse, pLeft,
2170a 20 70 52 69 67 68 74 2c 20 4f 50 5f 4c 65 2c 20 pRight, OP_Le,
2170b 72 31 2c 20 72 32 2c 20 72 34 2c 20 53 51 4c 49 r1, r2, r4, SQLI
2170c 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 TE_STOREP2);.
2170d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
2170e 64 4f 70 33 28 76 2c 20 4f 50 5f 41 6e 64 2c 20 dOp3(v, OP_And,
2170f 72 33 2c 20 72 34 2c 20 74 61 72 67 65 74 29 3b r3, r4, target);
21710 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 . sqlite3Re
21711 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 leaseTempReg(pPa
21712 72 73 65 2c 20 72 33 29 3b 0a 20 20 20 20 20 20 rse, r3);.
21713 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 sqlite3ReleaseTe
21714 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 34 mpReg(pParse, r4
21715 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
21716 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
21717 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20 K_UPLUS: {.
21718 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 inReg = sqlite3
21719 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 ExprCodeTarget(p
2171a 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c Parse, pExpr->pL
2171b 65 66 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 eft, target);.
2171c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
2171d 0a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 54 52 .. case TK_TR
2171e 49 47 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 2f IGGER: {. /
2171f 2a 20 49 66 20 74 68 65 20 6f 70 63 6f 64 65 20 * If the opcode
21720 69 73 20 54 4b 5f 54 52 49 47 47 45 52 2c 20 74 is TK_TRIGGER, t
21721 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 hen the expressi
21722 6f 6e 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 on is a referenc
21723 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 20 e. ** to a
21724 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 6e 65 column in the ne
21725 77 2e 2a 20 6f 72 20 6f 6c 64 2e 2a 20 70 73 65 w.* or old.* pse
21726 75 64 6f 2d 74 61 62 6c 65 73 20 61 76 61 69 6c udo-tables avail
21727 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 20 2a 2a able to. **
21728 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d trigger program
21729 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 s. In this case
2172a 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73 20 73 Expr.iTable is s
2172b 65 74 20 74 6f 20 31 20 66 6f 72 20 74 68 65 0a et to 1 for the.
2172c 20 20 20 20 20 20 2a 2a 20 6e 65 77 2e 2a 20 70 ** new.* p
2172d 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 6f 72 20 seudo-table, or
2172e 30 20 66 6f 72 20 74 68 65 20 6f 6c 64 2e 2a 20 0 for the old.*
2172f 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 20 45 78 pseudo-table. Ex
21730 70 72 2e 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 pr.iColumn.
21731 20 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 ** is set to th
21732 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 e column of the
21733 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 74 6f 20 pseudo-table to
21734 72 65 61 64 2c 20 6f 72 20 74 6f 20 2d 31 20 74 read, or to -1 t
21735 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20 o. ** read
21736 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c 64 2e the rowid field.
21737 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
21738 2a 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f ** The expressio
21739 6e 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 n is implemented
2173a 20 75 73 69 6e 67 20 61 6e 20 4f 50 5f 50 61 72 using an OP_Par
2173b 61 6d 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 70 am opcode. The p
2173c 31 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 61 6d 1. ** param
2173d 65 74 65 72 20 69 73 20 73 65 74 20 74 6f 20 30 eter is set to 0
2173e 20 66 6f 72 20 61 6e 20 6f 6c 64 2e 72 6f 77 69 for an old.rowi
2173f 64 20 72 65 66 65 72 65 6e 63 65 2c 20 6f 72 20 d reference, or
21740 74 6f 20 28 69 2b 31 29 0a 20 20 20 20 20 20 2a to (i+1). *
21741 2a 20 74 6f 20 72 65 66 65 72 65 6e 63 65 20 61 * to reference a
21742 6e 6f 74 68 65 72 20 63 6f 6c 75 6d 6e 20 6f 66 nother column of
21743 20 74 68 65 20 6f 6c 64 2e 2a 20 70 73 65 75 64 the old.* pseud
21744 6f 2d 74 61 62 6c 65 2c 20 77 68 65 72 65 20 0a o-table, where .
21745 20 20 20 20 20 20 2a 2a 20 69 20 69 73 20 74 68 ** i is th
21746 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 e index of the c
21747 6f 6c 75 6d 6e 2e 20 46 6f 72 20 61 20 6e 65 77 olumn. For a new
21748 2e 72 6f 77 69 64 20 72 65 66 65 72 65 6e 63 65 .rowid reference
21749 2c 20 70 31 20 69 73 0a 20 20 20 20 20 20 2a 2a , p1 is. **
2174a 20 73 65 74 20 74 6f 20 28 6e 2b 31 29 2c 20 77 set to (n+1), w
2174b 68 65 72 65 20 6e 20 69 73 20 74 68 65 20 6e 75 here n is the nu
2174c 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
2174d 69 6e 20 65 61 63 68 20 70 73 65 75 64 6f 2d 74 in each pseudo-t
2174e 61 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2a 20 46 able.. ** F
2174f 6f 72 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 or a reference t
21750 6f 20 61 6e 79 20 6f 74 68 65 72 20 63 6f 6c 75 o any other colu
21751 6d 6e 20 69 6e 20 74 68 65 20 6e 65 77 2e 2a 20 mn in the new.*
21752 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 70 31 pseudo-table, p1
21753 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73 65 74 . ** is set
21754 20 74 6f 20 28 6e 2b 32 2b 69 29 2c 20 77 68 65 to (n+2+i), whe
21755 72 65 20 6e 20 61 6e 64 20 69 20 61 72 65 20 61 re n and i are a
21756 73 20 64 65 66 69 6e 65 64 20 70 72 65 76 69 6f s defined previo
21757 75 73 6c 79 2e 20 46 6f 72 0a 20 20 20 20 20 20 usly. For.
21758 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 ** example, if t
21759 68 65 20 74 61 62 6c 65 20 6f 6e 20 77 68 69 63 he table on whic
2175a 68 20 74 72 69 67 67 65 72 73 20 61 72 65 20 62 h triggers are b
2175b 65 69 6e 67 20 66 69 72 65 64 20 69 73 0a 20 20 eing fired is.
2175c 20 20 20 20 2a 2a 20 64 65 63 6c 61 72 65 64 20 ** declared
2175d 61 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 as:. **.
2175e 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 54 ** CREATE T
2175f 41 42 4c 45 20 74 31 28 61 2c 20 62 29 3b 0a 20 ABLE t1(a, b);.
21760 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
21761 20 54 68 65 6e 20 70 31 20 69 73 20 69 6e 74 65 Then p1 is inte
21762 72 70 72 65 74 65 64 20 61 73 20 66 6f 6c 6c 6f rpreted as follo
21763 77 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 ws:. **.
21764 20 20 20 2a 2a 20 20 20 70 31 3d 3d 30 20 20 20 ** p1==0
21765 2d 3e 20 20 20 20 6f 6c 64 2e 72 6f 77 69 64 20 -> old.rowid
21766 20 20 20 20 70 31 3d 3d 33 20 20 20 2d 3e 20 20 p1==3 ->
21767 20 20 6e 65 77 2e 72 6f 77 69 64 0a 20 20 20 20 new.rowid.
21768 20 20 2a 2a 20 20 20 70 31 3d 3d 31 20 20 20 2d ** p1==1 -
21769 3e 20 20 20 20 6f 6c 64 2e 61 20 20 20 20 20 20 > old.a
2176a 20 20 20 70 31 3d 3d 34 20 20 20 2d 3e 20 20 20 p1==4 ->
2176b 20 6e 65 77 2e 61 0a 20 20 20 20 20 20 2a 2a 20 new.a. **
2176c 20 20 70 31 3d 3d 32 20 20 20 2d 3e 20 20 20 20 p1==2 ->
2176d 6f 6c 64 2e 62 20 20 20 20 20 20 20 20 20 70 31 old.b p1
2176e 3d 3d 35 20 20 20 2d 3e 20 20 20 20 6e 65 77 2e ==5 -> new.
2176f 62 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 2a b . *
21770 2f 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 /. Table *p
21771 54 61 62 20 3d 20 70 45 78 70 72 2d 3e 70 54 61 Tab = pExpr->pTa
21772 62 3b 0a 20 20 20 20 20 20 69 6e 74 20 70 31 20 b;. int p1
21773 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 = pExpr->iTable
21774 2a 20 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 * (pTab->nCol+1)
21775 20 2b 20 31 20 2b 20 70 45 78 70 72 2d 3e 69 43 + 1 + pExpr->iC
21776 6f 6c 75 6d 6e 3b 0a 0a 20 20 20 20 20 20 61 73 olumn;.. as
21777 73 65 72 74 28 20 70 45 78 70 72 2d 3e 69 54 61 sert( pExpr->iTa
21778 62 6c 65 3d 3d 30 20 7c 7c 20 70 45 78 70 72 2d ble==0 || pExpr-
21779 3e 69 54 61 62 6c 65 3d 3d 31 20 29 3b 0a 20 20 >iTable==1 );.
2177a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 assert( pExp
2177b 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 2d 31 20 26 r->iColumn>=-1 &
2177c 26 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e & pExpr->iColumn
2177d 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 <pTab->nCol );.
2177e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 assert( pTa
2177f 62 2d 3e 69 50 4b 65 79 3c 30 20 7c 7c 20 70 45 b->iPKey<0 || pE
21780 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 70 54 xpr->iColumn!=pT
21781 61 62 2d 3e 69 50 4b 65 79 20 29 3b 0a 20 20 20 ab->iPKey );.
21782 20 20 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30 assert( p1>=0
21783 20 26 26 20 70 31 3c 28 70 54 61 62 2d 3e 6e 43 && p1<(pTab->nC
21784 6f 6c 2a 32 2b 32 29 20 29 3b 0a 0a 20 20 20 20 ol*2+2) );..
21785 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21786 4f 70 32 28 76 2c 20 4f 50 5f 50 61 72 61 6d 2c Op2(v, OP_Param,
21787 20 70 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 p1, target);.
21788 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 VdbeComment(
21789 28 76 2c 20 22 25 73 2e 25 73 20 2d 3e 20 24 25 (v, "%s.%s -> $%
2178a 64 22 2c 0a 20 20 20 20 20 20 20 20 28 70 45 78 d",. (pEx
2178b 70 72 2d 3e 69 54 61 62 6c 65 20 3f 20 22 6e 65 pr->iTable ? "ne
2178c 77 22 20 3a 20 22 6f 6c 64 22 29 2c 0a 20 20 20 w" : "old"),.
2178d 20 20 20 20 20 28 70 45 78 70 72 2d 3e 69 43 6f (pExpr->iCo
2178e 6c 75 6d 6e 3c 30 20 3f 20 22 72 6f 77 69 64 22 lumn<0 ? "rowid"
2178f 20 3a 20 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e : pExpr->pTab->
21790 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c aCol[pExpr->iCol
21791 75 6d 6e 5d 2e 7a 4e 61 6d 65 29 2c 0a 20 20 20 umn].zName),.
21792 20 20 20 20 20 74 61 72 67 65 74 0a 20 20 20 20 target.
21793 20 20 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 ));.. /*
21794 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 68 61 If the column ha
21795 73 20 52 45 41 4c 20 61 66 66 69 6e 69 74 79 2c s REAL affinity,
21796 20 69 74 20 6d 61 79 20 63 75 72 72 65 6e 74 6c it may currentl
21797 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20 61 y be stored as a
21798 6e 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 n. ** integ
21799 65 72 2e 20 55 73 65 20 4f 50 5f 52 65 61 6c 41 er. Use OP_RealA
2179a 66 66 69 6e 69 74 79 20 74 6f 20 6d 61 6b 65 20 ffinity to make
2179b 73 75 72 65 20 69 74 20 69 73 20 72 65 61 6c 6c sure it is reall
2179c 79 20 72 65 61 6c 2e 20 20 2a 2f 0a 20 20 20 20 y real. */.
2179d 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f if( pExpr->iCo
2179e 6c 75 6d 6e 3e 3d 30 20 0a 20 20 20 20 20 20 20 lumn>=0 .
2179f 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 45 && pTab->aCol[pE
217a0 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 61 66 xpr->iColumn].af
217a1 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 finity==SQLITE_A
217a2 46 46 5f 52 45 41 4c 0a 20 20 20 20 20 20 29 7b FF_REAL. ){
217a3 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
217a4 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
217a5 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 2c 20 74 _RealAffinity, t
217a6 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a arget);. }.
217a7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
217a8 20 7d 0a 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 }... /*.
217a9 2a 2a 20 46 6f 72 6d 20 41 3a 0a 20 20 20 20 2a ** Form A:. *
217aa 2a 20 20 20 43 41 53 45 20 78 20 57 48 45 4e 20 * CASE x WHEN
217ab 65 31 20 54 48 45 4e 20 72 31 20 57 48 45 4e 20 e1 THEN r1 WHEN
217ac 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e 20 57 e2 THEN r2 ... W
217ad 48 45 4e 20 65 4e 20 54 48 45 4e 20 72 4e 20 45 HEN eN THEN rN E
217ae 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a LSE y END. **
217af 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 42 3a 0a . ** Form B:.
217b0 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 57 48 ** CASE WH
217b1 45 4e 20 65 31 20 54 48 45 4e 20 72 31 20 57 48 EN e1 THEN r1 WH
217b2 45 4e 20 65 32 20 54 48 45 4e 20 72 32 20 2e 2e EN e2 THEN r2 ..
217b3 2e 20 57 48 45 4e 20 65 4e 20 54 48 45 4e 20 72 . WHEN eN THEN r
217b4 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 N ELSE y END.
217b5 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 **. ** Form
217b6 41 20 69 73 20 63 61 6e 20 62 65 20 74 72 61 6e A is can be tran
217b7 73 66 6f 72 6d 65 64 20 69 6e 74 6f 20 74 68 65 sformed into the
217b8 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f 72 6d equivalent form
217b9 20 42 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 B as follows:.
217ba 20 20 20 2a 2a 20 20 20 43 41 53 45 20 57 48 45 ** CASE WHE
217bb 4e 20 78 3d 65 31 20 54 48 45 4e 20 72 31 20 57 N x=e1 THEN r1 W
217bc 48 45 4e 20 78 3d 65 32 20 54 48 45 4e 20 72 32 HEN x=e2 THEN r2
217bd 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 20 20 .... **
217be 20 20 20 57 48 45 4e 20 78 3d 65 4e 20 54 48 45 WHEN x=eN THE
217bf 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a N rN ELSE y END.
217c0 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 58 20 **. ** X
217c1 28 69 66 20 69 74 20 65 78 69 73 74 73 29 20 69 (if it exists) i
217c2 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 65 66 s in pExpr->pLef
217c3 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73 20 69 t.. ** Y is i
217c4 6e 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2e n pExpr->pRight.
217c5 20 20 54 68 65 20 59 20 69 73 20 61 6c 73 6f 20 The Y is also
217c6 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66 20 74 68 optional. If th
217c7 65 72 65 20 69 73 20 6e 6f 0a 20 20 20 20 2a 2a ere is no. **
217c8 20 45 4c 53 45 20 63 6c 61 75 73 65 20 61 6e 64 ELSE clause and
217c9 20 6e 6f 20 6f 74 68 65 72 20 74 65 72 6d 20 6d no other term m
217ca 61 74 63 68 65 73 2c 20 74 68 65 6e 20 74 68 65 atches, then the
217cb 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 result of the.
217cc 20 20 20 2a 2a 20 65 78 70 72 73 73 69 6f 6e 20 ** exprssion
217cd 69 73 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2a 20 is NULL.. **
217ce 45 69 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e Ei is in pExpr->
217cf 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 5d 20 61 6e pList->a[i*2] an
217d0 64 20 52 69 20 69 73 20 70 45 78 70 72 2d 3e 70 d Ri is pExpr->p
217d1 4c 69 73 74 2d 3e 61 5b 69 2a 32 2b 31 5d 2e 0a List->a[i*2+1]..
217d2 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 **. ** Th
217d3 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 e result of the
217d4 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 68 expression is th
217d5 65 20 52 69 20 66 6f 72 20 74 68 65 20 66 69 72 e Ri for the fir
217d6 73 74 20 6d 61 74 63 68 69 6e 67 20 45 69 2c 0a st matching Ei,.
217d7 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 ** or if the
217d8 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 69 6e re is no matchin
217d9 67 20 45 69 2c 20 74 68 65 20 45 4c 53 45 20 74 g Ei, the ELSE t
217da 65 72 6d 20 59 2c 20 6f 72 20 69 66 20 74 68 65 erm Y, or if the
217db 72 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f 20 re is. ** no
217dc 45 4c 53 45 20 74 65 72 6d 2c 20 4e 55 4c 4c 2e ELSE term, NULL.
217dd 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 65 66 61 . */. defa
217de 75 6c 74 3a 20 61 73 73 65 72 74 28 20 6f 70 3d ult: assert( op=
217df 3d 54 4b 5f 43 41 53 45 20 29 3b 20 7b 0a 20 20 =TK_CASE ); {.
217e0 20 20 20 20 69 6e 74 20 65 6e 64 4c 61 62 65 6c int endLabel
217e1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
217e2 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 /* GOTO la
217e3 62 65 6c 20 66 6f 72 20 65 6e 64 20 6f 66 20 43 bel for end of C
217e4 41 53 45 20 73 74 6d 74 20 2a 2f 0a 20 20 20 20 ASE stmt */.
217e5 20 20 69 6e 74 20 6e 65 78 74 43 61 73 65 3b 20 int nextCase;
217e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
217e7 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 62 65 /* GOTO labe
217e8 6c 20 66 6f 72 20 6e 65 78 74 20 57 48 45 4e 20 l for next WHEN
217e9 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 clause */.
217ea 69 6e 74 20 6e 45 78 70 72 3b 20 20 20 20 20 20 int nExpr;
217eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
217ec 20 20 2f 2a 20 32 78 20 6e 75 6d 62 65 72 20 6f /* 2x number o
217ed 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a f WHEN terms */.
217ee 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 int i;
217ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
217f0 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
217f1 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 counter */.
217f2 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 ExprList *pELis
217f3 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
217f4 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 57 48 /* List of WH
217f5 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 EN terms */.
217f6 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
217f7 74 5f 69 74 65 6d 20 2a 61 4c 69 73 74 65 6c 65 t_item *aListele
217f8 6d 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 m; /* Array of
217f9 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 WHEN terms */.
217fa 20 20 20 20 45 78 70 72 20 6f 70 43 6f 6d 70 61 Expr opCompa
217fb 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 re;
217fc 20 20 20 20 20 20 2f 2a 20 54 68 65 20 58 3d 3d /* The X==
217fd 45 69 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f Ei expression */
217fe 0a 20 20 20 20 20 20 45 78 70 72 20 63 61 63 68 . Expr cach
217ff 65 58 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 eX;
21800 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 /* Cach
21801 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 58 20 ed expression X
21802 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 */. Expr *p
21803 58 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 X;
21804 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
21805 65 20 58 20 65 78 70 72 65 73 73 69 6f 6e 20 2a e X expression *
21806 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 54 /. Expr *pT
21807 65 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 est = 0;
21808 20 20 20 20 20 20 20 20 20 20 2f 2a 20 58 3d 3d /* X==
21809 45 69 20 28 66 6f 72 6d 20 41 29 20 6f 72 20 6a Ei (form A) or j
2180a 75 73 74 20 45 69 20 28 66 6f 72 6d 20 42 29 20 ust Ei (form B)
2180b 2a 2f 0a 20 20 20 20 20 20 56 56 41 5f 4f 4e 4c */. VVA_ONL
2180c 59 28 20 69 6e 74 20 69 43 61 63 68 65 4c 65 76 Y( int iCacheLev
2180d 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 el = pParse->iCa
2180e 63 68 65 4c 65 76 65 6c 3b 20 29 0a 0a 20 20 20 cheLevel; )..
2180f 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 assert( !Expr
21810 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
21811 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 r, EP_xIsSelect)
21812 20 26 26 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 && pExpr->x.pLi
21813 73 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 st );. asse
21814 72 74 28 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69 rt((pExpr->x.pLi
21815 73 74 2d 3e 6e 45 78 70 72 20 25 20 32 29 20 3d st->nExpr % 2) =
21816 3d 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 = 0);. asse
21817 72 74 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 rt(pExpr->x.pLis
21818 74 2d 3e 6e 45 78 70 72 20 3e 20 30 29 3b 0a 20 t->nExpr > 0);.
21819 20 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 45 pEList = pE
2181a 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 xpr->x.pList;.
2181b 20 20 20 20 61 4c 69 73 74 65 6c 65 6d 20 3d 20 aListelem =
2181c 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 pEList->a;.
2181d 20 6e 45 78 70 72 20 3d 20 70 45 4c 69 73 74 2d nExpr = pEList-
2181e 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 65 6e >nExpr;. en
2181f 64 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 33 dLabel = sqlite3
21820 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 VdbeMakeLabel(v)
21821 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 58 20 ;. if( (pX
21822 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 21 = pExpr->pLeft)!
21823 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 =0 ){. ca
21824 63 68 65 58 20 3d 20 2a 70 58 3b 0a 20 20 20 20 cheX = *pX;.
21825 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 58 testcase( pX
21826 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 ->op==TK_COLUMN
21827 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 );. testc
21828 61 73 65 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f ase( pX->op==TK_
21829 52 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20 REGISTER );.
2182a 20 20 20 20 63 61 63 68 65 58 2e 69 54 61 62 6c cacheX.iTabl
2182b 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 e = sqlite3ExprC
2182c 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 odeTemp(pParse,
2182d 70 58 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a pX, ®Free1);.
2182e 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
2182f 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b ( regFree1==0 );
21830 0a 20 20 20 20 20 20 20 20 63 61 63 68 65 58 2e . cacheX.
21831 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 op = TK_REGISTER
21832 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 ;. opComp
21833 61 72 65 2e 6f 70 20 3d 20 54 4b 5f 45 51 3b 0a are.op = TK_EQ;.
21834 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 opCompar
21835 65 2e 70 4c 65 66 74 20 3d 20 26 63 61 63 68 65 e.pLeft = &cache
21836 58 3b 0a 20 20 20 20 20 20 20 20 70 54 65 73 74 X;. pTest
21837 20 3d 20 26 6f 70 43 6f 6d 70 61 72 65 3b 0a 20 = &opCompare;.
21838 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 }. for
21839 28 69 3d 30 3b 20 69 3c 6e 45 78 70 72 3b 20 69 (i=0; i<nExpr; i
2183a 3d 69 2b 32 29 7b 0a 20 20 20 20 20 20 20 20 73 =i+2){. s
2183b 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 qlite3ExprCacheP
2183c 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20 20 ush(pParse);.
2183d 20 20 20 20 20 69 66 28 20 70 58 20 29 7b 0a 20 if( pX ){.
2183e 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
2183f 20 70 54 65 73 74 21 3d 30 20 29 3b 0a 20 20 20 pTest!=0 );.
21840 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 opCompare
21841 2e 70 52 69 67 68 74 20 3d 20 61 4c 69 73 74 65 .pRight = aListe
21842 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 lem[i].pExpr;.
21843 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
21844 20 20 20 20 20 20 20 70 54 65 73 74 20 3d 20 61 pTest = a
21845 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 Listelem[i].pExp
21846 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 r;. }.
21847 20 20 20 20 20 6e 65 78 74 43 61 73 65 20 3d 20 nextCase =
21848 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c sqlite3VdbeMakeL
21849 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 abel(v);.
2184a 20 74 65 73 74 63 61 73 65 28 20 70 54 65 73 74 testcase( pTest
2184b 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 ->op==TK_COLUMN
2184c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
2184d 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 e3ExprIfFalse(pP
2184e 61 72 73 65 2c 20 70 54 65 73 74 2c 20 6e 65 78 arse, pTest, nex
2184f 74 43 61 73 65 2c 20 53 51 4c 49 54 45 5f 4a 55 tCase, SQLITE_JU
21850 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 MPIFNULL);.
21851 20 20 20 74 65 73 74 63 61 73 65 28 20 61 4c 69 testcase( aLi
21852 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 stelem[i+1].pExp
21853 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e r->op==TK_COLUMN
21854 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 );. test
21855 63 61 73 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b case( aListelem[
21856 69 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d i+1].pExpr->op==
21857 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b 0a 20 TK_REGISTER );.
21858 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 sqlite3Ex
21859 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 61 prCode(pParse, a
2185a 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 Listelem[i+1].pE
2185b 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 xpr, target);.
2185c 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
2185d 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f eAddOp2(v, OP_Go
2185e 74 6f 2c 20 30 2c 20 65 6e 64 4c 61 62 65 6c 29 to, 0, endLabel)
2185f 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
21860 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 3ExprCachePop(pP
21861 61 72 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 arse, 1);.
21862 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 sqlite3VdbeRes
21863 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6e 65 78 olveLabel(v, nex
21864 74 43 61 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a tCase);. }.
21865 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d if( pExpr-
21866 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 >pRight ){.
21867 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
21868 63 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b chePush(pParse);
21869 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
2186a 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c ExprCode(pParse,
2186b 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 pExpr->pRight,
2186c 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 target);.
2186d 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
2186e 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b ePop(pParse, 1);
2186f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
21870 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21871 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 eAddOp2(v, OP_Nu
21872 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a ll, 0, target);.
21873 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 }. as
21874 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 sert( db->malloc
21875 46 61 69 6c 65 64 20 7c 7c 20 70 50 61 72 73 65 Failed || pParse
21876 2d 3e 6e 45 72 72 3e 30 20 0a 20 20 20 20 20 20 ->nErr>0 .
21877 20 20 20 20 20 7c 7c 20 70 50 61 72 73 65 2d 3e || pParse->
21878 69 43 61 63 68 65 4c 65 76 65 6c 3d 3d 69 43 61 iCacheLevel==iCa
21879 63 68 65 4c 65 76 65 6c 20 29 3b 0a 20 20 20 20 cheLevel );.
2187a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 sqlite3VdbeRes
2187b 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 olveLabel(v, end
2187c 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20 62 72 Label);. br
2187d 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 eak;. }.#ifnd
2187e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
2187f 52 49 47 47 45 52 0a 20 20 20 20 63 61 73 65 20 RIGGER. case
21880 54 4b 5f 52 41 49 53 45 3a 20 7b 0a 20 20 20 20 TK_RAISE: {.
21881 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
21882 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 52 6f >affinity==OE_Ro
21883 6c 6c 62 61 63 6b 20 0a 20 20 20 20 20 20 20 20 llback .
21884 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e 61 66 66 || pExpr->aff
21885 69 6e 69 74 79 3d 3d 4f 45 5f 41 62 6f 72 74 0a inity==OE_Abort.
21886 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 45 || pE
21887 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f xpr->affinity==O
21888 45 5f 46 61 69 6c 0a 20 20 20 20 20 20 20 20 20 E_Fail.
21889 20 20 7c 7c 20 70 45 78 70 72 2d 3e 61 66 66 69 || pExpr->affi
2188a 6e 69 74 79 3d 3d 4f 45 5f 49 67 6e 6f 72 65 0a nity==OE_Ignore.
2188b 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 69 );. i
2188c 66 28 20 21 70 50 61 72 73 65 2d 3e 70 54 72 69 f( !pParse->pTri
2188d 67 67 65 72 54 61 62 20 29 7b 0a 20 20 20 20 20 ggerTab ){.
2188e 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
2188f 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 sg(pParse,.
21890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21891 20 20 22 52 41 49 53 45 28 29 20 6d 61 79 20 6f "RAISE() may o
21892 6e 6c 79 20 62 65 20 75 73 65 64 20 77 69 74 68 nly be used with
21893 69 6e 20 61 20 74 72 69 67 67 65 72 2d 70 72 6f in a trigger-pro
21894 67 72 61 6d 22 29 3b 0a 20 20 20 20 20 20 20 20 gram");.
21895 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 return 0;.
21896 7d 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 }. if( pExp
21897 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f r->affinity==OE_
21898 41 62 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20 Abort ){.
21899 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 sqlite3MayAbort
2189a 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 (pParse);.
2189b 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
2189c 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 !ExprHasProperty
2189d 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 (pExpr, EP_IntVa
2189e 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 69 66 lue) );. if
2189f 28 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 ( pExpr->affinit
218a0 79 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a y==OE_Ignore ){.
218a1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
218a2 64 62 65 41 64 64 4f 70 34 28 0a 20 20 20 20 20 dbeAddOp4(.
218a3 20 20 20 20 20 20 20 76 2c 20 4f 50 5f 48 61 6c v, OP_Hal
218a4 74 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 4f 45 t, SQLITE_OK, OE
218a5 5f 49 67 6e 6f 72 65 2c 20 30 2c 20 70 45 78 70 _Ignore, 0, pExp
218a6 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 30 29 3b 0a r->u.zToken,0);.
218a7 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
218a8 20 20 20 20 20 73 71 6c 69 74 65 33 48 61 6c 74 sqlite3Halt
218a9 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73 Constraint(pPars
218aa 65 2c 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 e, pExpr->affini
218ab 74 79 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f ty, pExpr->u.zTo
218ac 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d ken, 0);. }
218ad 0a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 .. break;.
218ae 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a }.#endif. }.
218af 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
218b0 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
218b1 72 65 67 46 72 65 65 31 29 3b 0a 20 20 73 71 6c regFree1);. sql
218b2 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
218b3 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 eg(pParse, regFr
218b4 65 65 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 ee2);. return i
218b5 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 nReg;.}../*.** G
218b6 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 enerate code to
218b7 65 76 61 6c 75 61 74 65 20 61 6e 20 65 78 70 72 evaluate an expr
218b8 65 73 73 69 6f 6e 20 61 6e 64 20 73 74 6f 72 65 ession and store
218b9 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 the results.**
218ba 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 2e into a register.
218bb 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 67 Return the reg
218bc 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 68 65 ister number whe
218bd 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a re the results.*
218be 2a 20 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a * are stored..**
218bf 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 67 69 73 .** If the regis
218c0 74 65 72 20 69 73 20 61 20 74 65 6d 70 6f 72 61 ter is a tempora
218c1 72 79 20 72 65 67 69 73 74 65 72 20 74 68 61 74 ry register that
218c2 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61 can be dealloca
218c3 74 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 77 72 69 ted,.** then wri
218c4 74 65 20 69 74 73 20 6e 75 6d 62 65 72 20 69 6e te its number in
218c5 74 6f 20 2a 70 52 65 67 2e 20 20 49 66 20 74 68 to *pReg. If th
218c6 65 20 72 65 73 75 6c 74 20 72 65 67 69 73 74 65 e result registe
218c7 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 74 65 r is not.** a te
218c8 6d 70 6f 72 61 72 79 2c 20 74 68 65 6e 20 73 65 mporary, then se
218c9 74 20 2a 70 52 65 67 20 74 6f 20 7a 65 72 6f 2e t *pReg to zero.
218ca 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
218cb 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 TE int sqlite3Ex
218cc 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72 73 65 prCodeTemp(Parse
218cd 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
218ce 70 45 78 70 72 2c 20 69 6e 74 20 2a 70 52 65 67 pExpr, int *pReg
218cf 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71 ){. int r1 = sq
218d0 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 lite3GetTempReg(
218d1 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 72 pParse);. int r
218d2 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 2 = sqlite3ExprC
218d3 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 odeTarget(pParse
218d4 2c 20 70 45 78 70 72 2c 20 72 31 29 3b 0a 20 20 , pExpr, r1);.
218d5 69 66 28 20 72 32 3d 3d 72 31 20 29 7b 0a 20 20 if( r2==r1 ){.
218d6 20 20 2a 70 52 65 67 20 3d 20 72 31 3b 0a 20 20 *pReg = r1;.
218d7 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
218d8 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
218d9 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 (pParse, r1);.
218da 20 20 2a 70 52 65 67 20 3d 20 30 3b 0a 20 20 7d *pReg = 0;. }
218db 0a 20 20 72 65 74 75 72 6e 20 72 32 3b 0a 7d 0a . return r2;.}.
218dc 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
218dd 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 code that will e
218de 76 61 6c 75 61 74 65 20 65 78 70 72 65 73 73 69 valuate expressi
218df 6f 6e 20 70 45 78 70 72 20 61 6e 64 20 73 74 6f on pExpr and sto
218e0 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 re the.** result
218e1 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 s in register ta
218e2 72 67 65 74 2e 20 20 54 68 65 20 72 65 73 75 6c rget. The resul
218e3 74 73 20 61 72 65 20 67 75 61 72 61 6e 74 65 65 ts are guarantee
218e4 64 20 74 6f 20 61 70 70 65 61 72 0a 2a 2a 20 69 d to appear.** i
218e5 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67 65 n register targe
218e6 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
218e7 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
218e8 45 78 70 72 43 6f 64 65 28 50 61 72 73 65 20 2a ExprCode(Parse *
218e9 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 pParse, Expr *pE
218ea 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29 xpr, int target)
218eb 7b 0a 20 20 69 6e 74 20 69 6e 52 65 67 3b 0a 0a {. int inReg;..
218ec 20 20 61 73 73 65 72 74 28 20 74 61 72 67 65 74 assert( target
218ed 3e 30 20 26 26 20 74 61 72 67 65 74 3c 3d 70 50 >0 && target<=pP
218ee 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 arse->nMem );.
218ef 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 inReg = sqlite3E
218f0 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 xprCodeTarget(pP
218f1 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 72 arse, pExpr, tar
218f2 67 65 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 get);. assert(
218f3 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 7c 7c pParse->pVdbe ||
218f4 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c pParse->db->mal
218f5 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 locFailed );. i
218f6 66 28 20 69 6e 52 65 67 21 3d 74 61 72 67 65 74 f( inReg!=target
218f7 20 26 26 20 70 50 61 72 73 65 2d 3e 70 56 64 62 && pParse->pVdb
218f8 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 e ){. sqlite3
218f9 56 64 62 65 41 64 64 4f 70 32 28 70 50 61 72 73 VdbeAddOp2(pPars
218fa 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 53 43 6f e->pVdbe, OP_SCo
218fb 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 67 65 py, inReg, targe
218fc 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e t);. }. return
218fd 20 74 61 72 67 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a target;.}../*.*
218fe 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
218ff 74 68 61 74 20 65 76 61 6c 75 74 65 73 20 74 68 that evalutes th
21900 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 e given expressi
21901 6f 6e 20 61 6e 64 20 70 75 74 73 20 74 68 65 20 on and puts the
21902 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 72 65 67 result.** in reg
21903 69 73 74 65 72 20 74 61 72 67 65 74 2e 0a 2a 2a ister target..**
21904 0a 2a 2a 20 41 6c 73 6f 20 6d 61 6b 65 20 61 20 .** Also make a
21905 63 6f 70 79 20 6f 66 20 74 68 65 20 65 78 70 72 copy of the expr
21906 65 73 73 69 6f 6e 20 72 65 73 75 6c 74 73 20 69 ession results i
21907 6e 74 6f 20 61 6e 6f 74 68 65 72 20 22 63 61 63 nto another "cac
21908 68 65 22 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 he" register.**
21909 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 20 65 and modify the e
2190a 78 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 xpression so tha
2190b 74 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 t the next time
2190c 69 74 20 69 73 20 65 76 61 6c 75 61 74 65 64 2c it is evaluated,
2190d 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 69 .** the result i
2190e 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 s a copy of the
2190f 63 61 63 68 65 20 72 65 67 69 73 74 65 72 2e 0a cache register..
21910 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
21911 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 65 ne is used for e
21912 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20 xpressions that
21913 61 72 65 20 75 73 65 64 20 6d 75 6c 74 69 70 6c are used multipl
21914 65 20 0a 2a 2a 20 74 69 6d 65 73 2e 20 20 54 68 e .** times. Th
21915 65 79 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 ey are evaluated
21916 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 20 72 65 once and the re
21917 73 75 6c 74 73 20 6f 66 20 74 68 65 20 65 78 70 sults of the exp
21918 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 72 65 20 72 ression.** are r
21919 65 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 eused..*/.SQLITE
2191a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
2191b 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43 ite3ExprCodeAndC
2191c 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 61 72 ache(Parse *pPar
2191d 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c se, Expr *pExpr,
2191e 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 20 int target){.
2191f 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 Vdbe *v = pParse
21920 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 ->pVdbe;. int i
21921 6e 52 65 67 3b 0a 20 20 69 6e 52 65 67 20 3d 20 nReg;. inReg =
21922 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 sqlite3ExprCode(
21923 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 pParse, pExpr, t
21924 61 72 67 65 74 29 3b 0a 20 20 61 73 73 65 72 74 arget);. assert
21925 28 20 74 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 ( target>0 );.
21926 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 /* This routine
21927 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 74 65 is called for te
21928 72 6d 73 20 74 6f 20 49 4e 53 45 52 54 20 6f 72 rms to INSERT or
21929 20 55 50 44 41 54 45 2e 20 20 41 6e 64 20 74 68 UPDATE. And th
2192a 65 20 6f 6e 6c 79 0a 20 20 2a 2a 20 6f 74 68 65 e only. ** othe
2192b 72 20 70 6c 61 63 65 20 77 68 65 72 65 20 65 78 r place where ex
2192c 70 72 65 73 73 69 6f 6e 73 20 63 61 6e 20 62 65 pressions can be
2192d 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 converted into
2192e 54 4b 5f 52 45 47 49 53 54 45 52 20 69 73 0a 20 TK_REGISTER is.
2192f 20 2a 2a 20 69 6e 20 57 48 45 52 45 20 63 6c 61 ** in WHERE cla
21930 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 use processing.
21931 20 53 6f 20 61 73 20 63 75 72 72 65 6e 74 6c 79 So as currently
21932 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2c 20 74 68 implemented, th
21933 65 72 65 20 69 73 0a 20 20 2a 2a 20 6e 6f 20 77 ere is. ** no w
21934 61 79 20 66 6f 72 20 61 20 54 4b 5f 52 45 47 49 ay for a TK_REGI
21935 53 54 45 52 20 74 6f 20 65 78 69 73 74 20 68 65 STER to exist he
21936 72 65 2e 20 20 42 75 74 20 69 74 20 73 65 65 6d re. But it seem
21937 73 20 70 72 75 64 65 6e 74 20 74 6f 0a 20 20 2a s prudent to. *
21938 2a 20 6b 65 65 70 20 74 68 65 20 41 4c 57 41 59 * keep the ALWAY
21939 53 28 29 20 69 6e 20 63 61 73 65 20 74 68 65 20 S() in case the
2193a 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76 65 conditions above
2193b 20 63 68 61 6e 67 65 20 77 69 74 68 20 66 75 74 change with fut
2193c 75 72 65 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 63 ure. ** modific
2193d 61 74 69 6f 6e 73 20 6f 72 20 65 6e 68 61 6e 63 ations or enhanc
2193e 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 69 66 28 ements. */. if(
2193f 20 41 4c 57 41 59 53 28 70 45 78 70 72 2d 3e 6f ALWAYS(pExpr->o
21940 70 21 3d 54 4b 5f 52 45 47 49 53 54 45 52 29 20 p!=TK_REGISTER)
21941 29 7b 20 20 0a 20 20 20 20 69 6e 74 20 69 4d 65 ){ . int iMe
21942 6d 3b 0a 20 20 20 20 69 4d 65 6d 20 3d 20 2b 2b m;. iMem = ++
21943 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 pParse->nMem;.
21944 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21945 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 Op2(v, OP_Copy,
21946 69 6e 52 65 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 inReg, iMem);.
21947 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 pExpr->iTable
21948 3d 20 69 4d 65 6d 3b 0a 20 20 20 20 70 45 78 70 = iMem;. pExp
21949 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 r->op = TK_REGIS
2194a 54 45 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 TER;. }. retur
2194b 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a n inReg;.}../*.*
2194c 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 * Return TRUE if
2194d 20 70 45 78 70 72 20 69 73 20 61 6e 20 63 6f 6e pExpr is an con
2194e 73 74 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e stant expression
2194f 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72 that is appropr
21950 69 61 74 65 0a 2a 2a 20 66 6f 72 20 66 61 63 74 iate.** for fact
21951 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20 61 20 6c oring out of a l
21952 6f 6f 70 2e 20 20 41 70 70 72 6f 70 72 69 61 74 oop. Appropriat
21953 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 e expressions ar
21954 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 e:.**.** * A
21955 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 ny expression th
21956 61 74 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 at evaluates to
21957 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 70 63 6f two or more opco
21958 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 des..**.** *
21959 20 41 6e 79 20 4f 50 5f 49 6e 74 65 67 65 72 2c Any OP_Integer,
2195a 20 4f 50 5f 52 65 61 6c 2c 20 4f 50 5f 53 74 72 OP_Real, OP_Str
2195b 69 6e 67 2c 20 4f 50 5f 42 6c 6f 62 2c 20 4f 50 ing, OP_Blob, OP
2195c 5f 4e 75 6c 6c 2c 20 0a 2a 2a 20 20 20 20 20 20 _Null, .**
2195d 20 6f 72 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 or OP_Variable
2195e 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 that does not ne
2195f 65 64 20 74 6f 20 62 65 20 70 6c 61 63 65 64 20 ed to be placed
21960 69 6e 20 61 20 0a 2a 2a 20 20 20 20 20 20 20 73 in a .** s
21961 70 65 63 69 66 69 63 20 72 65 67 69 73 74 65 72 pecific register
21962 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 ..**.** There is
21963 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 66 61 63 no point in fac
21964 74 6f 72 69 6e 67 20 6f 75 74 20 73 69 6e 67 6c toring out singl
21965 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f e-instruction co
21966 6e 73 74 61 6e 74 0a 2a 2a 20 65 78 70 72 65 73 nstant.** expres
21967 73 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 sions that need
21968 74 6f 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20 to be placed in
21969 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 67 a particular reg
2196a 69 73 74 65 72 2e 20 20 0a 2a 2a 20 57 65 20 63 ister. .** We c
2196b 6f 75 6c 64 20 66 61 63 74 6f 72 20 74 68 65 6d ould factor them
2196c 20 6f 75 74 2c 20 62 75 74 20 74 68 65 6e 20 77 out, but then w
2196d 65 20 77 6f 75 6c 64 20 65 6e 64 20 75 70 20 61 e would end up a
2196e 64 64 69 6e 67 20 61 6e 0a 2a 2a 20 4f 50 5f 53 dding an.** OP_S
2196f 43 6f 70 79 20 69 6e 73 74 72 75 63 74 69 6f 6e Copy instruction
21970 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 6c to move the val
21971 75 65 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 72 ue into the corr
21972 65 63 74 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 ect register.**
21973 6c 61 74 65 72 2e 20 20 57 65 20 6d 69 67 68 74 later. We might
21974 20 61 73 20 77 65 6c 6c 20 6a 75 73 74 20 75 73 as well just us
21975 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 69 e the original i
21976 6e 73 74 72 75 63 74 69 6f 6e 20 61 6e 64 0a 2a nstruction and.*
21977 2a 20 61 76 6f 69 64 20 74 68 65 20 4f 50 5f 53 * avoid the OP_S
21978 43 6f 70 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 Copy..*/.static
21979 69 6e 74 20 69 73 41 70 70 72 6f 70 72 69 61 74 int isAppropriat
2197a 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 45 78 eForFactoring(Ex
2197b 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 73 pr *p){. if( !s
2197c 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 qlite3ExprIsCons
2197d 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 29 20 29 tantNotJoin(p) )
2197e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 {. return 0;
2197f 20 2f 2a 20 4f 6e 6c 79 20 63 6f 6e 73 74 61 6e /* Only constan
21980 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 t expressions ar
21981 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f e appropriate fo
21982 72 20 66 61 63 74 6f 72 69 6e 67 20 2a 2f 0a 20 r factoring */.
21983 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 }. if( (p->fla
21984 67 73 20 26 20 45 50 5f 46 69 78 65 64 44 65 73 gs & EP_FixedDes
21985 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 t)==0 ){. ret
21986 75 72 6e 20 31 3b 20 20 2f 2a 20 41 6e 79 20 63 urn 1; /* Any c
21987 6f 6e 73 74 61 6e 74 20 77 69 74 68 6f 75 74 20 onstant without
21988 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 74 a fixed destinat
21989 69 6f 6e 20 69 73 20 61 70 70 72 6f 70 72 69 61 ion is appropria
2198a 74 65 20 2a 2f 0a 20 20 7d 0a 20 20 77 68 69 6c te */. }. whil
2198b 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c e( p->op==TK_UPL
2198c 55 53 20 29 20 70 20 3d 20 70 2d 3e 70 4c 65 66 US ) p = p->pLef
2198d 74 3b 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e t;. switch( p->
2198e 6f 70 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 op ){.#ifndef SQ
2198f 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c LITE_OMIT_BLOB_L
21990 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20 ITERAL. case
21991 54 4b 5f 42 4c 4f 42 3a 0a 23 65 6e 64 69 66 0a TK_BLOB:.#endif.
21992 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 case TK_VARI
21993 41 42 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 ABLE:. case T
21994 4b 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 K_INTEGER:. c
21995 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20 ase TK_FLOAT:.
21996 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 0a case TK_NULL:.
21997 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49 case TK_STRI
21998 4e 47 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 NG: {. test
21999 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f case( p->op==TK_
2199a 42 4c 4f 42 20 29 3b 0a 20 20 20 20 20 20 74 65 BLOB );. te
2199b 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 stcase( p->op==T
2199c 4b 5f 56 41 52 49 41 42 4c 45 20 29 3b 0a 20 20 K_VARIABLE );.
2199d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d testcase( p-
2199e 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 >op==TK_INTEGER
2199f 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
219a0 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f e( p->op==TK_FLO
219a1 41 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 AT );. test
219a2 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f case( p->op==TK_
219a3 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 NULL );. te
219a4 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 stcase( p->op==T
219a5 4b 5f 53 54 52 49 4e 47 20 29 3b 0a 20 20 20 20 K_STRING );.
219a6 20 20 2f 2a 20 53 69 6e 67 6c 65 2d 69 6e 73 74 /* Single-inst
219a7 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 ruction constant
219a8 73 20 77 69 74 68 20 61 20 66 69 78 65 64 20 64 s with a fixed d
219a9 65 73 74 69 6e 61 74 69 6f 6e 20 61 72 65 0a 20 estination are.
219aa 20 20 20 20 20 2a 2a 20 62 65 74 74 65 72 20 64 ** better d
219ab 6f 6e 65 20 69 6e 2d 6c 69 6e 65 2e 20 20 49 66 one in-line. If
219ac 20 77 65 20 66 61 63 74 6f 72 20 74 68 65 6d 2c we factor them,
219ad 20 74 68 65 79 20 77 69 6c 6c 20 6a 75 73 74 20 they will just
219ae 65 6e 64 0a 20 20 20 20 20 20 2a 2a 20 75 70 20 end. ** up
219af 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 20 4f 50 generating an OP
219b0 5f 53 43 6f 70 79 20 74 6f 20 6d 6f 76 65 20 74 _SCopy to move t
219b1 68 65 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20 he value to the
219b2 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20 destination.
219b3 20 20 2a 2a 20 72 65 67 69 73 74 65 72 2e 20 2a ** register. *
219b4 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 /. return 0
219b5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
219b6 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 TK_UMINUS: {.
219b7 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 65 66 74 if( p->pLeft
219b8 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c ->op==TK_FLOAT |
219b9 7c 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d | p->pLeft->op==
219ba 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 TK_INTEGER ){.
219bb 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
219bc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
219bd 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 eak;. }. d
219be 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
219bf 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
219c0 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a . return 1;.}..
219c1 2f 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 69 /*.** If pExpr i
219c2 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 s a constant exp
219c3 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 ression that is
219c4 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a appropriate for.
219c5 2a 2a 20 66 61 63 74 6f 72 69 6e 67 20 6f 75 74 ** factoring out
219c6 20 6f 66 20 61 20 6c 6f 6f 70 2c 20 74 68 65 6e of a loop, then
219c7 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 65 78 evaluate the ex
219c8 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 74 6f pression.** into
219c9 20 61 20 72 65 67 69 73 74 65 72 20 61 6e 64 20 a register and
219ca 63 6f 6e 76 65 72 74 20 74 68 65 20 65 78 70 72 convert the expr
219cb 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b ession into a TK
219cc 5f 52 45 47 49 53 54 45 52 0a 2a 2a 20 65 78 70 _REGISTER.** exp
219cd 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 ression..*/.stat
219ce 69 63 20 69 6e 74 20 65 76 61 6c 43 6f 6e 73 74 ic int evalConst
219cf 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 Expr(Walker *pWa
219d0 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 lker, Expr *pExp
219d1 72 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 r){. Parse *pPa
219d2 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 rse = pWalker->p
219d3 50 61 72 73 65 3b 0a 20 20 73 77 69 74 63 68 28 Parse;. switch(
219d4 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 pExpr->op ){.
219d5 20 20 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54 case TK_REGIST
219d6 45 52 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 ER: {. retu
219d7 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 rn WRC_Prune;.
219d8 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
219d9 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 FUNCTION:. ca
219da 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 se TK_AGG_FUNCTI
219db 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ON:. case TK_
219dc 43 4f 4e 53 54 5f 46 55 4e 43 3a 20 7b 0a 20 20 CONST_FUNC: {.
219dd 20 20 20 20 2f 2a 20 54 68 65 20 61 72 67 75 6d /* The argum
219de 65 6e 74 73 20 74 6f 20 61 20 66 75 6e 63 74 69 ents to a functi
219df 6f 6e 20 68 61 76 65 20 61 20 66 69 78 65 64 20 on have a fixed
219e0 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 20 20 20 destination..
219e1 20 20 20 2a 2a 20 4d 61 72 6b 20 74 68 65 6d 20 ** Mark them
219e2 74 68 69 73 20 77 61 79 20 74 6f 20 61 76 6f 69 this way to avoi
219e3 64 20 67 65 6e 65 72 61 74 65 64 20 75 6e 6e 65 d generated unne
219e4 65 64 65 64 20 4f 50 5f 53 43 6f 70 79 0a 20 20 eded OP_SCopy.
219e5 20 20 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 ** instructi
219e6 6f 6e 73 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 ons. . */.
219e7 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 ExprList *p
219e8 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e List = pExpr->x.
219e9 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 61 73 73 pList;. ass
219ea 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f ert( !ExprHasPro
219eb 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
219ec 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 xIsSelect) );.
219ed 20 20 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b if( pList ){
219ee 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 20 3d . int i =
219ef 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 pList->nExpr;.
219f0 20 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78 struct Ex
219f1 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 prList_item *pIt
219f2 65 6d 20 3d 20 70 4c 69 73 74 2d 3e 61 3b 0a 20 em = pList->a;.
219f3 20 20 20 20 20 20 20 66 6f 72 28 3b 20 69 3e 30 for(; i>0
219f4 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i--, pItem++){
219f5 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 41 . if( A
219f6 4c 57 41 59 53 28 70 49 74 65 6d 2d 3e 70 45 78 LWAYS(pItem->pEx
219f7 70 72 29 20 29 20 70 49 74 65 6d 2d 3e 70 45 78 pr) ) pItem->pEx
219f8 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f pr->flags |= EP_
219f9 46 69 78 65 64 44 65 73 74 3b 0a 20 20 20 20 20 FixedDest;.
219fa 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
219fb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
219fc 20 20 7d 0a 20 20 69 66 28 20 69 73 41 70 70 72 }. if( isAppr
219fd 6f 70 72 69 61 74 65 46 6f 72 46 61 63 74 6f 72 opriateForFactor
219fe 69 6e 67 28 70 45 78 70 72 29 20 29 7b 0a 20 20 ing(pExpr) ){.
219ff 20 20 69 6e 74 20 72 31 20 3d 20 2b 2b 70 50 61 int r1 = ++pPa
21a00 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 69 rse->nMem;. i
21a01 6e 74 20 72 32 3b 0a 20 20 20 20 72 32 20 3d 20 nt r2;. r2 =
21a02 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
21a03 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 arget(pParse, pE
21a04 78 70 72 2c 20 72 31 29 3b 0a 20 20 20 20 69 66 xpr, r1);. if
21a05 28 20 4e 45 56 45 52 28 72 31 21 3d 72 32 29 20 ( NEVER(r1!=r2)
21a06 29 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 ) sqlite3Release
21a07 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
21a08 72 31 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e r1);. pExpr->
21a09 6f 70 32 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b op2 = pExpr->op;
21a0a 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d . pExpr->op =
21a0b 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 TK_REGISTER;.
21a0c 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 pExpr->iTable
21a0d 3d 20 72 32 3b 0a 20 20 20 20 72 65 74 75 72 6e = r2;. return
21a0e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a WRC_Prune;. }.
21a0f 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e return WRC_Con
21a10 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 tinue;.}../*.**
21a11 50 72 65 65 76 61 6c 75 61 74 65 20 63 6f 6e 73 Preevaluate cons
21a12 74 61 6e 74 20 73 75 62 65 78 70 72 65 73 73 69 tant subexpressi
21a13 6f 6e 73 20 77 69 74 68 69 6e 20 70 45 78 70 72 ons within pExpr
21a14 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 0a 2a and store the.*
21a15 2a 20 72 65 73 75 6c 74 73 20 69 6e 20 72 65 67 * results in reg
21a16 69 73 74 65 72 73 2e 20 20 4d 6f 64 69 66 79 20 isters. Modify
21a17 70 45 78 70 72 20 73 6f 20 74 68 61 74 20 74 68 pExpr so that th
21a18 65 20 63 6f 6e 73 74 61 6e 74 20 73 75 62 65 78 e constant subex
21a19 70 72 65 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 presions.** are
21a1a 54 4b 5f 52 45 47 49 53 54 45 52 20 6f 70 63 6f TK_REGISTER opco
21a1b 64 65 73 20 74 68 61 74 20 72 65 66 65 72 20 74 des that refer t
21a1c 6f 20 74 68 65 20 70 72 65 63 6f 6d 70 75 74 65 o the precompute
21a1d 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51 4c d values..*/.SQL
21a1e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
21a1f 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21a20 43 6f 6e 73 74 61 6e 74 73 28 50 61 72 73 65 20 Constants(Parse
21a21 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 *pParse, Expr *p
21a22 45 78 70 72 29 7b 0a 20 20 57 61 6c 6b 65 72 20 Expr){. Walker
21a23 77 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c w;. w.xExprCall
21a24 62 61 63 6b 20 3d 20 65 76 61 6c 43 6f 6e 73 74 back = evalConst
21a25 45 78 70 72 3b 0a 20 20 77 2e 78 53 65 6c 65 63 Expr;. w.xSelec
21a26 74 43 61 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 tCallback = 0;.
21a27 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 w.pParse = pPar
21a28 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c se;. sqlite3Wal
21a29 6b 45 78 70 72 28 26 77 2c 20 70 45 78 70 72 29 kExpr(&w, pExpr)
21a2a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 ;.}.../*.** Gene
21a2b 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 70 rate code that p
21a2c 75 73 68 65 73 20 74 68 65 20 76 61 6c 75 65 20 ushes the value
21a2d 6f 66 20 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 of every element
21a2e 20 6f 66 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a of the given.**
21a2f 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
21a30 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e 63 65 into a sequence
21a31 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 62 65 of registers be
21a32 67 69 6e 6e 69 6e 67 20 61 74 20 74 61 72 67 65 ginning at targe
21a33 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 t..**.** Return
21a34 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c the number of el
21a35 65 6d 65 6e 74 73 20 65 76 61 6c 75 61 74 65 64 ements evaluated
21a36 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
21a37 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
21a38 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 xprCodeExprList(
21a39 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
21a3a 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 , /* Parsing
21a3b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 context */. Ex
21a3c 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 prList *pList,
21a3d 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 /* The expressi
21a3e 6f 6e 20 6c 69 73 74 20 74 6f 20 62 65 20 63 6f on list to be co
21a3f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74 61 72 ded */. int tar
21a40 67 65 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 get, /* W
21a41 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 72 65 here to write re
21a42 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74 20 64 sults */. int d
21a43 6f 48 61 72 64 43 6f 70 79 20 20 20 20 20 2f 2a oHardCopy /*
21a44 20 4d 61 6b 65 20 61 20 68 61 72 64 20 63 6f 70 Make a hard cop
21a45 79 20 6f 66 20 65 76 65 72 79 20 65 6c 65 6d 65 y of every eleme
21a46 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 nt */.){. struc
21a47 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 t ExprList_item
21a48 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 2c *pItem;. int i,
21a49 20 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c n;. assert( pL
21a4a 69 73 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 ist!=0 );. asse
21a4b 72 74 28 20 74 61 72 67 65 74 3e 30 20 29 3b 0a rt( target>0 );.
21a4c 20 20 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 n = pList->nEx
21a4d 70 72 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d pr;. for(pItem=
21a4e 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 pList->a, i=0; i
21a4f 3c 6e 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b <n; i++, pItem++
21a50 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d ){. if( pItem
21a51 2d 3e 69 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 ->iAlias ){.
21a52 20 20 69 6e 74 20 69 52 65 67 20 3d 20 63 6f 64 int iReg = cod
21a53 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70 eAlias(pParse, p
21a54 49 74 65 6d 2d 3e 69 41 6c 69 61 73 2c 20 70 49 Item->iAlias, pI
21a55 74 65 6d 2d 3e 70 45 78 70 72 2c 20 74 61 72 67 tem->pExpr, targ
21a56 65 74 2b 69 29 3b 0a 20 20 20 20 20 20 56 64 62 et+i);. Vdb
21a57 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 e *v = sqlite3Ge
21a58 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 tVdbe(pParse);.
21a59 20 20 20 20 20 69 66 28 20 69 52 65 67 21 3d 74 if( iReg!=t
21a5a 61 72 67 65 74 2b 69 20 29 7b 0a 20 20 20 20 20 arget+i ){.
21a5b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21a5c 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 dOp2(v, OP_SCopy
21a5d 2c 20 69 52 65 67 2c 20 74 61 72 67 65 74 2b 69 , iReg, target+i
21a5e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
21a5f 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 else{. sqli
21a60 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 te3ExprCode(pPar
21a61 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 se, pItem->pExpr
21a62 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20 20 20 , target+i);.
21a63 20 7d 0a 20 20 20 20 69 66 28 20 64 6f 48 61 72 }. if( doHar
21a64 64 43 6f 70 79 20 26 26 20 21 70 50 61 72 73 65 dCopy && !pParse
21a65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
21a66 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ed ){. sqli
21a67 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 te3ExprHardCopy(
21a68 70 50 61 72 73 65 2c 20 74 61 72 67 65 74 2c 20 pParse, target,
21a69 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 n);. }. }.
21a6a 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a return n;.}../*.
21a6b 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
21a6c 20 66 6f 72 20 61 20 62 6f 6f 6c 65 61 6e 20 65 for a boolean e
21a6d 78 70 72 65 73 73 69 6f 6e 20 73 75 63 68 20 74 xpression such t
21a6e 68 61 74 20 61 20 6a 75 6d 70 20 69 73 20 6d 61 hat a jump is ma
21a6f 64 65 0a 2a 2a 20 74 6f 20 74 68 65 20 6c 61 62 de.** to the lab
21a70 65 6c 20 22 64 65 73 74 22 20 69 66 20 74 68 65 el "dest" if the
21a71 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 expression is t
21a72 72 75 65 20 62 75 74 20 65 78 65 63 75 74 69 6f rue but executio
21a73 6e 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73 n.** continues s
21a74 74 72 61 69 67 68 74 20 74 68 72 75 20 69 66 20 traight thru if
21a75 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
21a76 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 s false..**.** I
21a77 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
21a78 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 evaluates to NU
21a79 4c 4c 20 28 6e 65 69 74 68 65 72 20 74 72 75 65 LL (neither true
21a7a 20 6e 6f 72 20 66 61 6c 73 65 29 2c 20 74 68 65 nor false), the
21a7b 6e 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a 75 n.** take the ju
21a7c 6d 70 20 69 66 20 74 68 65 20 6a 75 6d 70 49 66 mp if the jumpIf
21a7d 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 53 51 4c Null flag is SQL
21a7e 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 2e 0a ITE_JUMPIFNULL..
21a7f 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 **.** This code
21a80 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 66 depends on the f
21a81 61 63 74 20 74 68 61 74 20 63 65 72 74 61 69 6e act that certain
21a82 20 74 6f 6b 65 6e 20 76 61 6c 75 65 73 20 28 65 token values (e
21a83 78 3a 20 54 4b 5f 45 51 29 0a 2a 2a 20 61 72 65 x: TK_EQ).** are
21a84 20 74 68 65 20 73 61 6d 65 20 61 73 20 6f 70 63 the same as opc
21a85 6f 64 65 20 76 61 6c 75 65 73 20 28 65 78 3a 20 ode values (ex:
21a86 4f 50 5f 45 71 29 20 74 68 61 74 20 69 6d 70 6c OP_Eq) that impl
21a87 65 6d 65 6e 74 20 74 68 65 20 63 6f 72 72 65 73 ement the corres
21a88 70 6f 6e 64 69 6e 67 0a 2a 2a 20 6f 70 65 72 61 ponding.** opera
21a89 74 69 6f 6e 2e 20 20 53 70 65 63 69 61 6c 20 63 tion. Special c
21a8a 6f 6d 6d 65 6e 74 73 20 69 6e 20 76 64 62 65 2e omments in vdbe.
21a8b 63 20 61 6e 64 20 74 68 65 20 6d 6b 6f 70 63 6f c and the mkopco
21a8c 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 69 deh.awk script i
21a8d 6e 0a 2a 2a 20 74 68 65 20 6d 61 6b 65 20 70 72 n.** the make pr
21a8e 6f 63 65 73 73 20 63 61 75 73 65 20 74 68 65 73 ocess cause thes
21a8f 65 20 76 61 6c 75 65 73 20 74 6f 20 61 6c 69 67 e values to alig
21a90 6e 2e 20 20 41 73 73 65 72 74 28 29 73 20 69 6e n. Assert()s in
21a91 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65 6c the code.** bel
21a92 6f 77 20 76 65 72 69 66 79 20 74 68 61 74 20 74 ow verify that t
21a93 68 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61 he numbers are a
21a94 6c 69 67 6e 65 64 20 63 6f 72 72 65 63 74 6c 79 ligned correctly
21a95 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
21a96 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
21a97 45 78 70 72 49 66 54 72 75 65 28 50 61 72 73 65 ExprIfTrue(Parse
21a98 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
21a99 70 45 78 70 72 2c 20 69 6e 74 20 64 65 73 74 2c pExpr, int dest,
21a9a 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 int jumpIfNull)
21a9b 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 {. Vdbe *v = pP
21a9c 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 arse->pVdbe;. i
21a9d 6e 74 20 6f 70 20 3d 20 30 3b 0a 20 20 69 6e 74 nt op = 0;. int
21a9e 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b 0a 20 regFree1 = 0;.
21a9f 20 69 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20 int regFree2 =
21aa0 30 3b 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 3b 0;. int r1, r2;
21aa1 0a 0a 20 20 61 73 73 65 72 74 28 20 6a 75 6d 70 .. assert( jump
21aa2 49 66 4e 75 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a IfNull==SQLITE_J
21aa3 55 4d 50 49 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d UMPIFNULL || jum
21aa4 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 pIfNull==0 );.
21aa5 69 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 if( NEVER(v==0)
21aa6 29 20 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f ) return; /
21aa7 2a 20 45 78 69 73 74 61 6e 63 65 20 6f 66 20 56 * Existance of V
21aa8 44 42 45 20 63 68 65 63 6b 65 64 20 62 79 20 63 DBE checked by c
21aa9 61 6c 6c 65 72 20 2a 2f 0a 20 20 69 66 28 20 4e aller */. if( N
21aaa 45 56 45 52 28 70 45 78 70 72 3d 3d 30 29 20 29 EVER(pExpr==0) )
21aab 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 4e 6f 20 return; /* No
21aac 77 61 79 20 74 68 69 73 20 63 61 6e 20 68 61 70 way this can hap
21aad 70 65 6e 20 2a 2f 0a 20 20 6f 70 20 3d 20 70 45 pen */. op = pE
21aae 78 70 72 2d 3e 6f 70 3b 0a 20 20 73 77 69 74 63 xpr->op;. switc
21aaf 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 h( op ){. cas
21ab0 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 e TK_AND: {.
21ab1 20 20 69 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 int d2 = sqlit
21ab2 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 e3VdbeMakeLabel(
21ab3 76 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 v);. testca
21ab4 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d se( jumpIfNull==
21ab5 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 );. sqlit
21ab6 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 e3ExprCachePush(
21ab7 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 pParse);. s
21ab8 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 qlite3ExprIfFals
21ab9 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d e(pParse, pExpr-
21aba 3e 70 4c 65 66 74 2c 20 64 32 2c 6a 75 6d 70 49 >pLeft, d2,jumpI
21abb 66 4e 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d fNull^SQLITE_JUM
21abc 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 PIFNULL);.
21abd 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 sqlite3ExprIfTru
21abe 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d e(pParse, pExpr-
21abf 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a >pRight, dest, j
21ac0 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 umpIfNull);.
21ac1 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 sqlite3VdbeRes
21ac2 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 olveLabel(v, d2)
21ac3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
21ac4 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 xprCachePop(pPar
21ac5 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72 se, 1);. br
21ac6 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
21ac7 61 73 65 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 ase TK_OR: {.
21ac8 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d testcase( jum
21ac9 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 pIfNull==0 );.
21aca 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 sqlite3ExprI
21acb 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 45 fTrue(pParse, pE
21acc 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 xpr->pLeft, dest
21acd 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 , jumpIfNull);.
21ace 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
21acf 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 IfTrue(pParse, p
21ad0 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 Expr->pRight, de
21ad1 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b st, jumpIfNull);
21ad2 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
21ad3 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
21ad4 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 NOT: {. tes
21ad5 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c tcase( jumpIfNul
21ad6 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 l==0 );. sq
21ad7 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 lite3ExprIfFalse
21ad8 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
21ad9 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d pLeft, dest, jum
21ada 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 pIfNull);.
21adb 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
21adc 20 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 case TK_LT:.
21add 20 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 case TK_LE:.
21ade 20 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 case TK_GT:.
21adf 20 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 case TK_GE:.
21ae0 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 case TK_NE:.
21ae1 20 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 case TK_EQ: {.
21ae2 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
21ae3 4c 54 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20 LT==OP_Lt );.
21ae4 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 assert( TK_LE
21ae5 3d 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20 ==OP_Le );.
21ae6 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d assert( TK_GT==
21ae7 4f 50 5f 47 74 20 29 3b 0a 20 20 20 20 20 20 61 OP_Gt );. a
21ae8 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50 ssert( TK_GE==OP
21ae9 5f 47 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 _Ge );. ass
21aea 65 72 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45 ert( TK_EQ==OP_E
21aeb 71 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 q );. asser
21aec 74 28 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 t( TK_NE==OP_Ne
21aed 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21aee 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a e( op==TK_LT );.
21aef 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21af0 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 op==TK_LE );.
21af1 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
21af2 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 =TK_GT );.
21af3 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
21af4 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 _GE );. tes
21af5 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 tcase( op==TK_EQ
21af6 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21af7 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b se( op==TK_NE );
21af8 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21af9 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 jumpIfNull==0 )
21afa 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 ;. codeComp
21afb 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 areOperands(pPar
21afc 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 se, pExpr->pLeft
21afd 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 , &r1, ®Free1
21afe 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
21aff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21b00 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 pExpr->pRigh
21b01 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 t, &r2, ®Free
21b02 32 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 2);. codeCo
21b03 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 mpare(pParse, pE
21b04 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 xpr->pLeft, pExp
21b05 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 r->pRight, op,.
21b06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21b07 20 72 31 2c 20 72 32 2c 20 64 65 73 74 2c 20 6a r1, r2, dest, j
21b08 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 umpIfNull);.
21b09 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 testcase( regF
21b0a 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ree1==0 );.
21b0b 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 testcase( regFr
21b0c 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ee2==0 );.
21b0d 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
21b0e 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a case TK_ISNULL:
21b0f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 . case TK_NOT
21b10 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 61 73 NULL: {. as
21b11 73 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d sert( TK_ISNULL=
21b12 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 =OP_IsNull );.
21b13 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e assert( TK_N
21b14 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 OTNULL==OP_NotNu
21b15 6c 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ll );. test
21b16 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e case( op==TK_ISN
21b17 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 ULL );. tes
21b18 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f tcase( op==TK_NO
21b19 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 72 TNULL );. r
21b1a 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 1 = sqlite3ExprC
21b1b 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 odeTemp(pParse,
21b1c 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 pExpr->pLeft, &r
21b1d 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 egFree1);.
21b1e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21b1f 32 28 76 2c 20 6f 70 2c 20 72 31 2c 20 64 65 73 2(v, op, r1, des
21b20 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 t);. testca
21b21 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 se( regFree1==0
21b22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
21b23 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
21b24 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 K_BETWEEN: {.
21b25 20 20 20 2f 2a 20 20 20 20 78 20 42 45 54 57 45 /* x BETWE
21b26 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20 20 20 20 EN y AND z.
21b27 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 73 20 **. ** Is
21b28 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 equivalent to .
21b29 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
21b2a 20 20 20 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d x>=y AND x<=
21b2b 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 z. **.
21b2c 20 2a 2a 20 43 6f 64 65 20 69 74 20 61 73 20 73 ** Code it as s
21b2d 75 63 68 2c 20 74 61 6b 69 6e 67 20 63 61 72 65 uch, taking care
21b2e 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 6d 6f to do the commo
21b2f 6e 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 0a n subexpression.
21b30 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 ** element
21b31 61 74 69 6f 6e 20 6f 66 20 78 2e 0a 20 20 20 20 ation of x..
21b32 20 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 */. Expr
21b33 65 78 70 72 41 6e 64 3b 0a 20 20 20 20 20 20 45 exprAnd;. E
21b34 78 70 72 20 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 xpr compLeft;.
21b35 20 20 20 20 45 78 70 72 20 63 6f 6d 70 52 69 67 Expr compRig
21b36 68 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20 65 ht;. Expr e
21b37 78 70 72 58 3b 0a 0a 20 20 20 20 20 20 61 73 73 xprX;.. ass
21b38 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f ert( !ExprHasPro
21b39 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
21b3a 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 xIsSelect) );.
21b3b 20 20 20 20 65 78 70 72 58 20 3d 20 2a 70 45 78 exprX = *pEx
21b3c 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 pr->pLeft;.
21b3d 20 65 78 70 72 41 6e 64 2e 6f 70 20 3d 20 54 4b exprAnd.op = TK
21b3e 5f 41 4e 44 3b 0a 20 20 20 20 20 20 65 78 70 72 _AND;. expr
21b3f 41 6e 64 2e 70 4c 65 66 74 20 3d 20 26 63 6f 6d And.pLeft = &com
21b40 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78 70 pLeft;. exp
21b41 72 41 6e 64 2e 70 52 69 67 68 74 20 3d 20 26 63 rAnd.pRight = &c
21b42 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 ompRight;.
21b43 63 6f 6d 70 4c 65 66 74 2e 6f 70 20 3d 20 54 4b compLeft.op = TK
21b44 5f 47 45 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c _GE;. compL
21b45 65 66 74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 eft.pLeft = &exp
21b46 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 rX;. compLe
21b47 66 74 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70 ft.pRight = pExp
21b48 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d r->x.pList->a[0]
21b49 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 63 6f .pExpr;. co
21b4a 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20 54 4b 5f mpRight.op = TK_
21b4b 4c 45 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 LE;. compRi
21b4c 67 68 74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 ght.pLeft = &exp
21b4d 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 rX;. compRi
21b4e 67 68 74 2e 70 52 69 67 68 74 20 3d 20 70 45 78 ght.pRight = pEx
21b4f 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 pr->x.pList->a[1
21b50 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 65 ].pExpr;. e
21b51 78 70 72 58 2e 69 54 61 62 6c 65 20 3d 20 73 71 xprX.iTable = sq
21b52 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d lite3ExprCodeTem
21b53 70 28 70 50 61 72 73 65 2c 20 26 65 78 70 72 58 p(pParse, &exprX
21b54 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 , ®Free1);.
21b55 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 testcase( re
21b56 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 gFree1==0 );.
21b57 20 20 20 65 78 70 72 58 2e 6f 70 20 3d 20 54 4b exprX.op = TK
21b58 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20 20 _REGISTER;.
21b59 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 testcase( jumpI
21b5a 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 fNull==0 );.
21b5b 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 sqlite3ExprIfT
21b5c 72 75 65 28 70 50 61 72 73 65 2c 20 26 65 78 70 rue(pParse, &exp
21b5d 72 41 6e 64 2c 20 64 65 73 74 2c 20 6a 75 6d 70 rAnd, dest, jump
21b5e 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 IfNull);. b
21b5f 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
21b60 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
21b61 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 r1 = sqlite3Exp
21b62 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 rCodeTemp(pParse
21b63 2c 20 70 45 78 70 72 2c 20 26 72 65 67 46 72 65 , pExpr, ®Fre
21b64 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 e1);. sqlit
21b65 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
21b66 4f 50 5f 49 66 2c 20 72 31 2c 20 64 65 73 74 2c OP_If, r1, dest,
21b67 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b jumpIfNull!=0);
21b68 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21b69 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a regFree1==0 );.
21b6a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21b6b 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b jumpIfNull==0 );
21b6c 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
21b6d 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 }. }. sqlite
21b6e 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
21b6f 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 pParse, regFree1
21b70 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 );. sqlite3Rele
21b71 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 aseTempReg(pPars
21b72 65 2c 20 72 65 67 46 72 65 65 32 29 3b 20 20 0a e, regFree2); .
21b73 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
21b74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f e code for a boo
21b75 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 lean expression
21b76 73 75 63 68 20 74 68 61 74 20 61 20 6a 75 6d 70 such that a jump
21b77 20 69 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 is made.** to t
21b78 68 65 20 6c 61 62 65 6c 20 22 64 65 73 74 22 20 he label "dest"
21b79 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f if the expressio
21b7a 6e 20 69 73 20 66 61 6c 73 65 20 62 75 74 20 65 n is false but e
21b7b 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 xecution.** cont
21b7c 69 6e 75 65 73 20 73 74 72 61 69 67 68 74 20 74 inues straight t
21b7d 68 72 75 20 69 66 20 74 68 65 20 65 78 70 72 65 hru if the expre
21b7e 73 73 69 6f 6e 20 69 73 20 74 72 75 65 2e 0a 2a ssion is true..*
21b7f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 *.** If the expr
21b80 65 73 73 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 ession evaluates
21b81 20 74 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65 to NULL (neithe
21b82 72 20 74 72 75 65 20 6e 6f 72 20 66 61 6c 73 65 r true nor false
21b83 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 69 ) then.** jump i
21b84 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 69 73 20 f jumpIfNull is
21b85 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c SQLITE_JUMPIFNUL
21b86 4c 20 6f 72 20 66 61 6c 6c 20 74 68 72 6f 75 67 L or fall throug
21b87 68 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 0a h if jumpIfNull.
21b88 2a 2a 20 69 73 20 30 2e 0a 2a 2f 0a 53 51 4c 49 ** is 0..*/.SQLI
21b89 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
21b8a 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c sqlite3ExprIfFal
21b8b 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 se(Parse *pParse
21b8c 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 , Expr *pExpr, i
21b8d 6e 74 20 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d nt dest, int jum
21b8e 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65 pIfNull){. Vdbe
21b8f 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 *v = pParse->pV
21b90 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 dbe;. int op =
21b91 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 0;. int regFree
21b92 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 1 = 0;. int reg
21b93 46 72 65 65 32 20 3d 20 30 3b 0a 20 20 69 6e 74 Free2 = 0;. int
21b94 20 72 31 2c 20 72 32 3b 0a 0a 20 20 61 73 73 65 r1, r2;.. asse
21b95 72 74 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d rt( jumpIfNull==
21b96 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c SQLITE_JUMPIFNUL
21b97 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d L || jumpIfNull=
21b98 3d 30 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 =0 );. if( NEVE
21b99 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e R(v==0) ) return
21b9a 3b 20 2f 2a 20 45 78 69 73 74 61 6e 63 65 20 6f ; /* Existance o
21b9b 66 20 56 44 42 45 20 63 68 65 63 6b 65 64 20 62 f VDBE checked b
21b9c 79 20 63 61 6c 6c 65 72 20 2a 2f 0a 20 20 69 66 y caller */. if
21b9d 28 20 70 45 78 70 72 3d 3d 30 20 29 20 20 20 20 ( pExpr==0 )
21b9e 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 54 68 return;.. /* Th
21b9f 65 20 76 61 6c 75 65 20 6f 66 20 70 45 78 70 72 e value of pExpr
21ba0 2d 3e 6f 70 20 61 6e 64 20 6f 70 20 61 72 65 20 ->op and op are
21ba1 72 65 6c 61 74 65 64 20 61 73 20 66 6f 6c 6c 6f related as follo
21ba2 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 ws:. **. **
21ba3 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 20 20 pExpr->op
21ba4 20 20 20 20 20 20 20 20 20 6f 70 0a 20 20 2a 2a op. **
21ba5 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------
21ba6 20 20 20 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d ------
21ba7 2d 2d 2d 2d 0a 20 20 2a 2a 20 20 20 20 20 20 20 ----. **
21ba8 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20 20 20 TK_ISNULL
21ba9 20 20 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 0a 20 20 OP_NotNull.
21baa 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4e 4f 54 4e ** TK_NOTN
21bab 55 4c 4c 20 20 20 20 20 20 20 20 20 4f 50 5f 49 ULL OP_I
21bac 73 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 20 sNull. **
21bad 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 20 TK_NE
21bae 20 20 20 20 4f 50 5f 45 71 0a 20 20 2a 2a 20 20 OP_Eq. **
21baf 20 20 20 20 20 54 4b 5f 45 51 20 20 20 20 20 20 TK_EQ
21bb0 20 20 20 20 20 20 20 20 4f 50 5f 4e 65 0a 20 20 OP_Ne.
21bb1 2a 2a 20 20 20 20 20 20 20 54 4b 5f 47 54 20 20 ** TK_GT
21bb2 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4c OP_L
21bb3 65 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f e. ** TK_
21bb4 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 LE
21bb5 4f 50 5f 47 74 0a 20 20 2a 2a 20 20 20 20 20 20 OP_Gt. **
21bb6 20 54 4b 5f 47 45 20 20 20 20 20 20 20 20 20 20 TK_GE
21bb7 20 20 20 20 4f 50 5f 4c 74 0a 20 20 2a 2a 20 20 OP_Lt. **
21bb8 20 20 20 20 20 54 4b 5f 4c 54 20 20 20 20 20 20 TK_LT
21bb9 20 20 20 20 20 20 20 20 4f 50 5f 47 65 0a 20 20 OP_Ge.
21bba 2a 2a 0a 20 20 2a 2a 20 46 6f 72 20 6f 74 68 65 **. ** For othe
21bbb 72 20 76 61 6c 75 65 73 20 6f 66 20 70 45 78 70 r values of pExp
21bbc 72 2d 3e 6f 70 2c 20 6f 70 20 69 73 20 75 6e 64 r->op, op is und
21bbd 65 66 69 6e 65 64 20 61 6e 64 20 75 6e 75 73 65 efined and unuse
21bbe 64 2e 0a 20 20 2a 2a 20 54 68 65 20 76 61 6c 75 d.. ** The valu
21bbf 65 20 6f 66 20 54 4b 5f 20 61 6e 64 20 4f 50 5f e of TK_ and OP_
21bc0 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 61 constants are a
21bc1 72 72 61 6e 67 65 64 20 73 75 63 68 20 74 68 61 rranged such tha
21bc2 74 20 77 65 0a 20 20 2a 2a 20 63 61 6e 20 63 6f t we. ** can co
21bc3 6d 70 75 74 65 20 74 68 65 20 6d 61 70 70 69 6e mpute the mappin
21bc4 67 20 61 62 6f 76 65 20 75 73 69 6e 67 20 74 68 g above using th
21bc5 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72 e following expr
21bc6 65 73 73 69 6f 6e 2e 0a 20 20 2a 2a 20 41 73 73 ession.. ** Ass
21bc7 65 72 74 28 29 73 20 76 65 72 69 66 79 20 74 68 ert()s verify th
21bc8 61 74 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69 at the computati
21bc9 6f 6e 20 69 73 20 63 6f 72 72 65 63 74 2e 0a 20 on is correct..
21bca 20 2a 2f 0a 20 20 6f 70 20 3d 20 28 28 70 45 78 */. op = ((pEx
21bcb 70 72 2d 3e 6f 70 2b 28 54 4b 5f 49 53 4e 55 4c pr->op+(TK_ISNUL
21bcc 4c 26 31 29 29 5e 31 29 2d 28 54 4b 5f 49 53 4e L&1))^1)-(TK_ISN
21bcd 55 4c 4c 26 31 29 3b 0a 0a 20 20 2f 2a 20 56 65 ULL&1);.. /* Ve
21bce 72 69 66 79 20 63 6f 72 72 65 63 74 20 61 6c 69 rify correct ali
21bcf 67 6e 6d 65 6e 74 20 6f 66 20 54 4b 5f 20 61 6e gnment of TK_ an
21bd0 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73 0a d OP_ constants.
21bd1 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
21bd2 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 53 4e Expr->op!=TK_ISN
21bd3 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 6f ULL || op==OP_No
21bd4 74 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 tNull );. asser
21bd5 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b t( pExpr->op!=TK
21bd6 5f 4e 4f 54 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d _NOTNULL || op==
21bd7 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 61 OP_IsNull );. a
21bd8 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 ssert( pExpr->op
21bd9 21 3d 54 4b 5f 4e 45 20 7c 7c 20 6f 70 3d 3d 4f !=TK_NE || op==O
21bda 50 5f 45 71 20 29 3b 0a 20 20 61 73 73 65 72 74 P_Eq );. assert
21bdb 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f ( pExpr->op!=TK_
21bdc 45 51 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 65 20 EQ || op==OP_Ne
21bdd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 );. assert( pEx
21bde 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 54 20 7c 7c pr->op!=TK_LT ||
21bdf 20 6f 70 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 op==OP_Ge );.
21be0 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f assert( pExpr->o
21be1 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 6f 70 3d 3d p!=TK_LE || op==
21be2 4f 50 5f 47 74 20 29 3b 0a 20 20 61 73 73 65 72 OP_Gt );. asser
21be3 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b t( pExpr->op!=TK
21be4 5f 47 54 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 65 _GT || op==OP_Le
21be5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 );. assert( pE
21be6 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47 45 20 7c xpr->op!=TK_GE |
21be7 7c 20 6f 70 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 0a | op==OP_Lt );..
21be8 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d switch( pExpr-
21be9 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 >op ){. case
21bea 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 TK_AND: {.
21beb 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 testcase( jumpIf
21bec 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 Null==0 );.
21bed 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 sqlite3ExprIfFa
21bee 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 lse(pParse, pExp
21bef 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 r->pLeft, dest,
21bf0 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 jumpIfNull);.
21bf1 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 sqlite3ExprIf
21bf2 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 False(pParse, pE
21bf3 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 xpr->pRight, des
21bf4 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a t, jumpIfNull);.
21bf5 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21bf6 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f }. case TK_O
21bf7 52 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 R: {. int d
21bf8 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 2 = sqlite3VdbeM
21bf9 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 akeLabel(v);.
21bfa 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d testcase( jum
21bfb 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 pIfNull==0 );.
21bfc 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
21bfd 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65 29 achePush(pParse)
21bfe 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
21bff 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65 xprIfTrue(pParse
21c00 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 , pExpr->pLeft,
21c01 64 32 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53 d2, jumpIfNull^S
21c02 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c QLITE_JUMPIFNULL
21c03 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
21c04 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 ExprIfFalse(pPar
21c05 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 se, pExpr->pRigh
21c06 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e t, dest, jumpIfN
21c07 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ull);. sqli
21c08 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 te3VdbeResolveLa
21c09 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 20 bel(v, d2);.
21c0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
21c0b 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 hePop(pParse, 1)
21c0c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
21c0d 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
21c0e 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 _NOT: {. sq
21c0f 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 lite3ExprIfTrue(
21c10 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
21c11 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 Left, dest, jump
21c12 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 IfNull);. b
21c13 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
21c14 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 case TK_LT:.
21c15 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 case TK_LE:.
21c16 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 case TK_GT:.
21c17 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 case TK_GE:.
21c18 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 case TK_NE:.
21c19 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 case TK_EQ: {.
21c1a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
21c1b 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 ==TK_LT );.
21c1c 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
21c1d 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 K_LE );. te
21c1e 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 stcase( op==TK_G
21c1f 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 T );. testc
21c20 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 ase( op==TK_GE )
21c21 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21c22 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 ( op==TK_EQ );.
21c23 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
21c24 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 p==TK_NE );.
21c25 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 testcase( jump
21c26 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 IfNull==0 );.
21c27 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 codeCompareOp
21c28 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 erands(pParse, p
21c29 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 Expr->pLeft, &r1
21c2a 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 , ®Free1,.
21c2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
21c2d 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 Expr->pRight, &r
21c2e 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 2, ®Free2);.
21c2f 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 codeCompare
21c30 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
21c31 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 pLeft, pExpr->pR
21c32 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 ight, op,.
21c33 20 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 r1,
21c34 72 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 r2, dest, jumpIf
21c35 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65 73 Null);. tes
21c36 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d tcase( regFree1=
21c37 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 =0 );. test
21c38 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d case( regFree2==
21c39 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 0 );. break
21c3a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
21c3b 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 TK_ISNULL:.
21c3c 63 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a case TK_NOTNULL:
21c3d 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 {. testcas
21c3e 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c e( op==TK_ISNULL
21c3f 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21c40 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 se( op==TK_NOTNU
21c41 4c 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d LL );. r1 =
21c42 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21c43 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 Temp(pParse, pEx
21c44 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 pr->pLeft, ®F
21c45 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c ree1);. sql
21c46 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
21c47 2c 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 29 3b , op, r1, dest);
21c48 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21c49 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a regFree1==0 );.
21c4a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21c4b 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 }. case TK_B
21c4c 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 ETWEEN: {.
21c4d 2f 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 /* x BETWEEN
21c4e 79 20 41 4e 44 20 7a 0a 20 20 20 20 20 20 2a 2a y AND z. **
21c4f 0a 20 20 20 20 20 20 2a 2a 20 49 73 20 65 71 75 . ** Is equ
21c50 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 ivalent to .
21c51 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 **. **
21c52 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 x>=y AND x<=z.
21c53 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
21c54 20 43 6f 64 65 20 69 74 20 61 73 20 73 75 63 68 Code it as such
21c55 2c 20 74 61 6b 69 6e 67 20 63 61 72 65 20 74 6f , taking care to
21c56 20 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 do the common s
21c57 75 62 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 ubexpression.
21c58 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 61 74 69 ** elementati
21c59 6f 6e 20 6f 66 20 78 2e 0a 20 20 20 20 20 20 2a on of x.. *
21c5a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70 /. Expr exp
21c5b 72 41 6e 64 3b 0a 20 20 20 20 20 20 45 78 70 72 rAnd;. Expr
21c5c 20 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 compLeft;.
21c5d 20 45 78 70 72 20 63 6f 6d 70 52 69 67 68 74 3b Expr compRight;
21c5e 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72 . Expr expr
21c5f 58 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 X;.. assert
21c60 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
21c61 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 ty(pExpr, EP_xIs
21c62 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 Select) );.
21c63 20 65 78 70 72 58 20 3d 20 2a 70 45 78 70 72 2d exprX = *pExpr-
21c64 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78 >pLeft;. ex
21c65 70 72 41 6e 64 2e 6f 70 20 3d 20 54 4b 5f 41 4e prAnd.op = TK_AN
21c66 44 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64 D;. exprAnd
21c67 2e 70 4c 65 66 74 20 3d 20 26 63 6f 6d 70 4c 65 .pLeft = &compLe
21c68 66 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e ft;. exprAn
21c69 64 2e 70 52 69 67 68 74 20 3d 20 26 63 6f 6d 70 d.pRight = &comp
21c6a 52 69 67 68 74 3b 0a 20 20 20 20 20 20 63 6f 6d Right;. com
21c6b 70 4c 65 66 74 2e 6f 70 20 3d 20 54 4b 5f 47 45 pLeft.op = TK_GE
21c6c 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 ;. compLeft
21c6d 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b .pLeft = &exprX;
21c6e 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e . compLeft.
21c6f 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e pRight = pExpr->
21c70 78 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 x.pList->a[0].pE
21c71 78 70 72 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 xpr;. compR
21c72 69 67 68 74 2e 6f 70 20 3d 20 54 4b 5f 4c 45 3b ight.op = TK_LE;
21c73 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 . compRight
21c74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b .pLeft = &exprX;
21c75 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 . compRight
21c76 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d .pRight = pExpr-
21c77 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 >x.pList->a[1].p
21c78 45 78 70 72 3b 0a 20 20 20 20 20 20 65 78 70 72 Expr;. expr
21c79 58 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 X.iTable = sqlit
21c7a 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 e3ExprCodeTemp(p
21c7b 50 61 72 73 65 2c 20 26 65 78 70 72 58 2c 20 26 Parse, &exprX, &
21c7c 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 regFree1);.
21c7d 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 testcase( regFr
21c7e 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ee1==0 );.
21c7f 65 78 70 72 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 exprX.op = TK_RE
21c80 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20 74 65 GISTER;. te
21c81 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 stcase( jumpIfNu
21c82 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 ll==0 );. s
21c83 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 qlite3ExprIfFals
21c84 65 28 70 50 61 72 73 65 2c 20 26 65 78 70 72 41 e(pParse, &exprA
21c85 6e 64 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 nd, dest, jumpIf
21c86 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 Null);. bre
21c87 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
21c88 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 fault: {. r
21c89 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 1 = sqlite3ExprC
21c8a 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 odeTemp(pParse,
21c8b 70 45 78 70 72 2c 20 26 72 65 67 46 72 65 65 31 pExpr, ®Free1
21c8c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
21c8d 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
21c8e 5f 49 66 4e 6f 74 2c 20 72 31 2c 20 64 65 73 74 _IfNot, r1, dest
21c8f 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 , jumpIfNull!=0)
21c90 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21c91 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b ( regFree1==0 );
21c92 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21c93 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 jumpIfNull==0 )
21c94 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
21c95 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 }. }. sqlit
21c96 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
21c97 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 (pParse, regFree
21c98 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 1);. sqlite3Rel
21c99 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 easeTempReg(pPar
21c9a 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b 0a 7d se, regFree2);.}
21c9b 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20 64 65 65 ../*.** Do a dee
21c9c 70 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 p comparison of
21c9d 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 20 74 two expression t
21c9e 72 65 65 73 2e 20 20 52 65 74 75 72 6e 20 54 52 rees. Return TR
21c9f 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29 0a 2a 2a UE (non-zero).**
21ca0 20 69 66 20 74 68 65 79 20 61 72 65 20 69 64 65 if they are ide
21ca1 6e 74 69 63 61 6c 20 61 6e 64 20 72 65 74 75 72 ntical and retur
21ca2 6e 20 46 41 4c 53 45 20 69 66 20 74 68 65 79 20 n FALSE if they
21ca3 64 69 66 66 65 72 20 69 6e 20 61 6e 79 20 77 61 differ in any wa
21ca4 79 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d y..**.** Sometim
21ca5 65 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 es this routine
21ca6 77 69 6c 6c 20 72 65 74 75 72 6e 20 46 41 4c 53 will return FALS
21ca7 45 20 65 76 65 6e 20 69 66 20 74 68 65 20 74 77 E even if the tw
21ca8 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a o expressions.**
21ca9 20 72 65 61 6c 6c 79 20 61 72 65 20 65 71 75 69 really are equi
21caa 76 61 6c 65 6e 74 2e 20 20 49 66 20 77 65 20 63 valent. If we c
21cab 61 6e 6e 6f 74 20 70 72 6f 76 65 20 74 68 61 74 annot prove that
21cac 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 the expressions
21cad 20 61 72 65 0a 2a 2a 20 69 64 65 6e 74 69 63 61 are.** identica
21cae 6c 2c 20 77 65 20 72 65 74 75 72 6e 20 46 41 4c l, we return FAL
21caf 53 45 20 6a 75 73 74 20 74 6f 20 62 65 20 73 61 SE just to be sa
21cb0 66 65 2e 20 20 53 6f 20 69 66 20 74 68 69 73 20 fe. So if this
21cb1 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 routine.** retur
21cb2 6e 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 79 ns false, then y
21cb3 6f 75 20 64 6f 20 6e 6f 74 20 72 65 61 6c 6c 79 ou do not really
21cb4 20 6b 6e 6f 77 20 66 6f 72 20 63 65 72 74 61 69 know for certai
21cb5 6e 20 69 66 20 74 68 65 20 74 77 6f 0a 2a 2a 20 n if the two.**
21cb6 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 expressions are
21cb7 74 68 65 20 73 61 6d 65 2e 20 20 42 75 74 20 69 the same. But i
21cb8 66 20 79 6f 75 20 67 65 74 20 61 20 54 52 55 45 f you get a TRUE
21cb9 20 72 65 74 75 72 6e 2c 20 74 68 65 6e 20 79 6f return, then yo
21cba 75 0a 2a 2a 20 63 61 6e 20 62 65 20 73 75 72 65 u.** can be sure
21cbb 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 the expressions
21cbc 20 61 72 65 20 74 68 65 20 73 61 6d 65 2e 20 20 are the same.
21cbd 49 6e 20 74 68 65 20 70 6c 61 63 65 73 20 77 68 In the places wh
21cbe 65 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 ere.** this rout
21cbf 69 6e 65 20 69 73 20 75 73 65 64 2c 20 69 74 20 ine is used, it
21cc0 64 6f 65 73 20 6e 6f 74 20 68 75 72 74 20 74 6f does not hurt to
21cc1 20 67 65 74 20 61 6e 20 65 78 74 72 61 20 46 41 get an extra FA
21cc2 4c 53 45 20 2d 20 74 68 61 74 0a 2a 2a 20 6a 75 LSE - that.** ju
21cc3 73 74 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 st might result
21cc4 69 6e 20 73 6f 6d 65 20 73 6c 69 67 68 74 6c 79 in some slightly
21cc5 20 73 6c 6f 77 65 72 20 63 6f 64 65 2e 20 20 42 slower code. B
21cc6 75 74 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 ut returning.**
21cc7 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 54 52 55 an incorrect TRU
21cc8 45 20 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20 E could lead to
21cc9 61 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a 2a a malfunction..*
21cca 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
21ccb 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
21ccc 43 6f 6d 70 61 72 65 28 45 78 70 72 20 2a 70 41 Compare(Expr *pA
21ccd 2c 20 45 78 70 72 20 2a 70 42 29 7b 0a 20 20 69 , Expr *pB){. i
21cce 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 41 3d 3d nt i;. if( pA==
21ccf 30 7c 7c 70 42 3d 3d 30 20 29 7b 0a 20 20 20 20 0||pB==0 ){.
21cd0 72 65 74 75 72 6e 20 70 42 3d 3d 70 41 3b 0a 20 return pB==pA;.
21cd1 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 }. assert( !Ex
21cd2 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 prHasAnyProperty
21cd3 28 70 41 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c (pA, EP_TokenOnl
21cd4 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b y|EP_Reduced) );
21cd5 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 . assert( !Expr
21cd6 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 HasAnyProperty(p
21cd7 42 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c B, EP_TokenOnly|
21cd8 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20 EP_Reduced) );.
21cd9 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 if( ExprHasProp
21cda 65 72 74 79 28 70 41 2c 20 45 50 5f 78 49 73 53 erty(pA, EP_xIsS
21cdb 65 6c 65 63 74 29 20 7c 7c 20 45 78 70 72 48 61 elect) || ExprHa
21cdc 73 50 72 6f 70 65 72 74 79 28 70 42 2c 20 45 50 sProperty(pB, EP
21cdd 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 _xIsSelect) ){.
21cde 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
21cdf 0a 20 20 69 66 28 20 28 70 41 2d 3e 66 6c 61 67 . if( (pA->flag
21ce0 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74 29 s & EP_Distinct)
21ce1 21 3d 28 70 42 2d 3e 66 6c 61 67 73 20 26 20 45 !=(pB->flags & E
21ce2 50 5f 44 69 73 74 69 6e 63 74 29 20 29 20 72 65 P_Distinct) ) re
21ce3 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 41 turn 0;. if( pA
21ce4 2d 3e 6f 70 21 3d 70 42 2d 3e 6f 70 20 29 20 72 ->op!=pB->op ) r
21ce5 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 21 eturn 0;. if( !
21ce6 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 sqlite3ExprCompa
21ce7 72 65 28 70 41 2d 3e 70 4c 65 66 74 2c 20 70 42 re(pA->pLeft, pB
21ce8 2d 3e 70 4c 65 66 74 29 20 29 20 72 65 74 75 72 ->pLeft) ) retur
21ce9 6e 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 n 0;. if( !sqli
21cea 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 te3ExprCompare(p
21ceb 41 2d 3e 70 52 69 67 68 74 2c 20 70 42 2d 3e 70 A->pRight, pB->p
21cec 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 Right) ) return
21ced 30 3b 0a 0a 20 20 69 66 28 20 70 41 2d 3e 78 2e 0;.. if( pA->x.
21cee 70 4c 69 73 74 20 26 26 20 70 42 2d 3e 78 2e 70 pList && pB->x.p
21cef 4c 69 73 74 20 29 7b 0a 20 20 20 20 69 66 28 20 List ){. if(
21cf0 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 pA->x.pList->nEx
21cf1 70 72 21 3d 70 42 2d 3e 78 2e 70 4c 69 73 74 2d pr!=pB->x.pList-
21cf2 3e 6e 45 78 70 72 20 29 20 72 65 74 75 72 6e 20 >nExpr ) return
21cf3 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 0;. for(i=0;
21cf4 69 3c 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e i<pA->x.pList->n
21cf5 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Expr; i++){.
21cf6 20 20 45 78 70 72 20 2a 70 45 78 70 72 41 20 3d Expr *pExprA =
21cf7 20 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b pA->x.pList->a[
21cf8 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 i].pExpr;.
21cf9 45 78 70 72 20 2a 70 45 78 70 72 42 20 3d 20 70 Expr *pExprB = p
21cfa 42 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 69 5d B->x.pList->a[i]
21cfb 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66 .pExpr;. if
21cfc 28 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f ( !sqlite3ExprCo
21cfd 6d 70 61 72 65 28 70 45 78 70 72 41 2c 20 70 45 mpare(pExprA, pE
21cfe 78 70 72 42 29 20 29 20 72 65 74 75 72 6e 20 30 xprB) ) return 0
21cff 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 ;. }. }else
21d00 69 66 28 20 70 41 2d 3e 78 2e 70 4c 69 73 74 20 if( pA->x.pList
21d01 7c 7c 20 70 42 2d 3e 78 2e 70 4c 69 73 74 20 29 || pB->x.pList )
21d02 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
21d03 20 20 7d 0a 0a 20 20 69 66 28 20 70 41 2d 3e 69 }.. if( pA->i
21d04 54 61 62 6c 65 21 3d 70 42 2d 3e 69 54 61 62 6c Table!=pB->iTabl
21d05 65 20 7c 7c 20 70 41 2d 3e 69 43 6f 6c 75 6d 6e e || pA->iColumn
21d06 21 3d 70 42 2d 3e 69 43 6f 6c 75 6d 6e 20 29 20 !=pB->iColumn )
21d07 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 return 0;. if(
21d08 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
21d09 70 41 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 pA, EP_IntValue)
21d0a 20 29 7b 0a 20 20 20 20 69 66 28 20 21 45 78 70 ){. if( !Exp
21d0b 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 42 2c rHasProperty(pB,
21d0c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 7c 7c EP_IntValue) ||
21d0d 20 70 41 2d 3e 75 2e 69 56 61 6c 75 65 21 3d 70 pA->u.iValue!=p
21d0e 42 2d 3e 75 2e 69 56 61 6c 75 65 20 29 7b 0a 20 B->u.iValue ){.
21d0f 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
21d10 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 }. }else if(
21d11 20 70 41 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 pA->op!=TK_COLU
21d12 4d 4e 20 26 26 20 70 41 2d 3e 75 2e 7a 54 6f 6b MN && pA->u.zTok
21d13 65 6e 20 29 7b 0a 20 20 20 20 69 66 28 20 45 78 en ){. if( Ex
21d14 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 42 prHasProperty(pB
21d15 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 7c , EP_IntValue) |
21d16 7c 20 4e 45 56 45 52 28 70 42 2d 3e 75 2e 7a 54 | NEVER(pB->u.zT
21d17 6f 6b 65 6e 3d 3d 30 29 20 29 20 72 65 74 75 72 oken==0) ) retur
21d18 6e 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c n 0;. if( sql
21d19 69 74 65 33 53 74 72 49 43 6d 70 28 70 41 2d 3e ite3StrICmp(pA->
21d1a 75 2e 7a 54 6f 6b 65 6e 2c 70 42 2d 3e 75 2e 7a u.zToken,pB->u.z
21d1b 54 6f 6b 65 6e 29 21 3d 30 20 29 7b 0a 20 20 20 Token)!=0 ){.
21d1c 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 return 0;.
21d1d 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
21d1e 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 1;.}.../*.** Add
21d1f 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 a new element t
21d20 6f 20 74 68 65 20 70 41 67 67 49 6e 66 6f 2d 3e o the pAggInfo->
21d21 61 43 6f 6c 5b 5d 20 61 72 72 61 79 2e 20 20 52 aCol[] array. R
21d22 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 eturn the index
21d23 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 65 6c of.** the new el
21d24 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 61 ement. Return a
21d25 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 negative number
21d26 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 if malloc fails
21d27 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
21d28 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e addAggInfoColumn
21d29 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 41 67 (sqlite3 *db, Ag
21d2a 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20 gInfo *pInfo){.
21d2b 20 69 6e 74 20 69 3b 0a 20 20 70 49 6e 66 6f 2d int i;. pInfo-
21d2c 3e 61 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 41 >aCol = sqlite3A
21d2d 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 rrayAllocate(.
21d2e 20 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20 db,.
21d2f 70 49 6e 66 6f 2d 3e 61 43 6f 6c 2c 0a 20 20 20 pInfo->aCol,.
21d30 20 20 20 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f sizeof(pInfo
21d31 2d 3e 61 43 6f 6c 5b 30 5d 29 2c 0a 20 20 20 20 ->aCol[0]),.
21d32 20 20 20 33 2c 0a 20 20 20 20 20 20 20 26 70 49 3,. &pI
21d33 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 2c 0a 20 20 nfo->nColumn,.
21d34 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f &pInfo->nCo
21d35 6c 75 6d 6e 41 6c 6c 6f 63 2c 0a 20 20 20 20 20 lumnAlloc,.
21d36 20 20 26 69 0a 20 20 29 3b 0a 20 20 72 65 74 75 &i. );. retu
21d37 72 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a 2f 2a 0a rn i;.} ../*.
21d38 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 ** Add a new ele
21d39 6d 65 6e 74 20 74 6f 20 74 68 65 20 70 41 67 67 ment to the pAgg
21d3a 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20 61 72 Info->aFunc[] ar
21d3b 72 61 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 ray. Return the
21d3c 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 index of.** the
21d3d 20 6e 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52 new element. R
21d3e 65 74 75 72 6e 20 61 20 6e 65 67 61 74 69 76 65 eturn a negative
21d3f 20 6e 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f number if mallo
21d40 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 c fails..*/.stat
21d41 69 63 20 69 6e 74 20 61 64 64 41 67 67 49 6e 66 ic int addAggInf
21d42 6f 46 75 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 oFunc(sqlite3 *d
21d43 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 b, AggInfo *pInf
21d44 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70 o){. int i;. p
21d45 49 6e 66 6f 2d 3e 61 46 75 6e 63 20 3d 20 73 71 Info->aFunc = sq
21d46 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 lite3ArrayAlloca
21d47 74 65 28 0a 20 20 20 20 20 20 20 64 62 2c 20 0a te(. db, .
21d48 20 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 46 pInfo->aF
21d49 75 6e 63 2c 0a 20 20 20 20 20 20 20 73 69 7a 65 unc,. size
21d4a 6f 66 28 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b of(pInfo->aFunc[
21d4b 30 5d 29 2c 0a 20 20 20 20 20 20 20 33 2c 0a 20 0]),. 3,.
21d4c 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 46 &pInfo->nF
21d4d 75 6e 63 2c 0a 20 20 20 20 20 20 20 26 70 49 6e unc,. &pIn
21d4e 66 6f 2d 3e 6e 46 75 6e 63 41 6c 6c 6f 63 2c 0a fo->nFuncAlloc,.
21d4f 20 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 &i. );.
21d50 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20 return i;.}
21d51 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ../*.** This is
21d52 74 68 65 20 78 45 78 70 72 43 61 6c 6c 62 61 63 the xExprCallbac
21d53 6b 20 66 6f 72 20 61 20 74 72 65 65 20 77 61 6c k for a tree wal
21d54 6b 65 72 2e 20 20 49 74 20 69 73 20 75 73 65 64 ker. It is used
21d55 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 to.** implement
21d56 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c sqlite3ExprAnal
21d57 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 29 2e yzeAggregates().
21d58 20 20 53 65 65 20 73 71 6c 69 74 65 33 45 78 70 See sqlite3Exp
21d59 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 rAnalyzeAggregat
21d5a 65 73 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 69 es.** for additi
21d5b 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
21d5c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
21d5d 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 analyzeAggregate
21d5e 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 (Walker *pWalker
21d5f 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a , Expr *pExpr){.
21d60 20 20 69 6e 74 20 69 3b 0a 20 20 4e 61 6d 65 43 int i;. NameC
21d61 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d 20 70 57 ontext *pNC = pW
21d62 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 alker->u.pNC;.
21d63 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 Parse *pParse =
21d64 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 53 pNC->pParse;. S
21d65 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 rcList *pSrcList
21d66 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 = pNC->pSrcList
21d67 3b 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 ;. AggInfo *pAg
21d68 67 49 6e 66 6f 20 3d 20 70 4e 43 2d 3e 70 41 67 gInfo = pNC->pAg
21d69 67 49 6e 66 6f 3b 0a 0a 20 20 73 77 69 74 63 68 gInfo;.. switch
21d6a 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 ( pExpr->op ){.
21d6b 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 case TK_AGG_C
21d6c 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 20 OLUMN:. case
21d6d 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 TK_COLUMN: {.
21d6e 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 testcase( pEx
21d6f 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 pr->op==TK_AGG_C
21d70 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74 OLUMN );. t
21d71 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e estcase( pExpr->
21d72 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b op==TK_COLUMN );
21d73 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 . /* Check
21d74 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 63 6f to see if the co
21d75 6c 75 6d 6e 20 69 73 20 69 6e 20 6f 6e 65 20 6f lumn is in one o
21d76 66 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 f the tables in
21d77 74 68 65 20 46 52 4f 4d 0a 20 20 20 20 20 20 2a the FROM. *
21d78 2a 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 * clause of the
21d79 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79 20 aggregate query
21d7a 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 */. if( ALW
21d7b 41 59 53 28 70 53 72 63 4c 69 73 74 21 3d 30 29 AYS(pSrcList!=0)
21d7c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75 ){. stru
21d7d 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
21d7e 2a 70 49 74 65 6d 20 3d 20 70 53 72 63 4c 69 73 *pItem = pSrcLis
21d7f 74 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 66 6f t->a;. fo
21d80 72 28 69 3d 30 3b 20 69 3c 70 53 72 63 4c 69 73 r(i=0; i<pSrcLis
21d81 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 t->nSrc; i++, pI
21d82 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 tem++){.
21d83 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f struct AggInfo
21d84 5f 63 6f 6c 20 2a 70 43 6f 6c 3b 0a 20 20 20 20 _col *pCol;.
21d85 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 assert( !E
21d86 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 xprHasAnyPropert
21d87 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 y(pExpr, EP_Toke
21d88 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 nOnly|EP_Reduced
21d89 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ) );. i
21d8a 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 f( pExpr->iTable
21d8b 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 ==pItem->iCursor
21d8c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
21d8d 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68 20 74 /* If we reach t
21d8e 68 69 73 20 70 6f 69 6e 74 2c 20 69 74 20 6d 65 his point, it me
21d8f 61 6e 73 20 74 68 61 74 20 70 45 78 70 72 20 72 ans that pExpr r
21d90 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65 efers to a table
21d91 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
21d92 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 46 that is in the F
21d93 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 ROM clause of th
21d94 65 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72 e aggregate quer
21d95 79 2e 20 20 0a 20 20 20 20 20 20 20 20 20 20 20 y. .
21d96 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 20 20 **.
21d97 2a 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72 79 ** Make an entry
21d98 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 for the column
21d99 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f in pAggInfo->aCo
21d9a 6c 5b 5d 20 69 66 20 74 68 65 72 65 0a 20 20 20 l[] if there.
21d9b 20 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6e ** is n
21d9c 6f 74 20 61 6e 20 65 6e 74 72 79 20 74 68 65 72 ot an entry ther
21d9d 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 20 e already..
21d9e 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
21d9f 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 int k;.
21da0 20 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 pCol =
21da1 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 0a pAggInfo->aCol;.
21da2 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 for(
21da3 6b 3d 30 3b 20 6b 3c 70 41 67 67 49 6e 66 6f 2d k=0; k<pAggInfo-
21da4 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 2c 20 70 >nColumn; k++, p
21da5 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 Col++){.
21da6 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e if( pCol->
21da7 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69 iTable==pExpr->i
21da8 54 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20 20 Table &&.
21da9 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d pCol-
21daa 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d >iColumn==pExpr-
21dab 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 >iColumn ){.
21dac 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 brea
21dad 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 k;.
21dae 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d }. }
21daf 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
21db0 20 28 6b 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e 6e (k>=pAggInfo->n
21db1 43 6f 6c 75 6d 6e 29 0a 20 20 20 20 20 20 20 20 Column).
21db2 20 20 20 20 20 26 26 20 28 6b 20 3d 20 61 64 64 && (k = add
21db3 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 70 50 AggInfoColumn(pP
21db4 61 72 73 65 2d 3e 64 62 2c 20 70 41 67 67 49 6e arse->db, pAggIn
21db5 66 6f 29 29 3e 3d 30 20 0a 20 20 20 20 20 20 20 fo))>=0 .
21db6 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 ){.
21db7 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 41 pCol = &pA
21db8 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 6b 5d 3b ggInfo->aCol[k];
21db9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 . p
21dba 43 6f 6c 2d 3e 70 54 61 62 20 3d 20 70 45 78 70 Col->pTab = pExp
21dbb 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 r->pTab;.
21dbc 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 54 61 pCol->iTa
21dbd 62 6c 65 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 ble = pExpr->iTa
21dbe 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ble;.
21dbf 20 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e pCol->iColumn
21dc0 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d = pExpr->iColum
21dc1 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n;.
21dc2 20 70 43 6f 6c 2d 3e 69 4d 65 6d 20 3d 20 2b 2b pCol->iMem = ++
21dc3 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 pParse->nMem;.
21dc4 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c pCol
21dc5 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20 ->iSorterColumn
21dc6 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20 = -1;.
21dc7 20 20 20 20 70 43 6f 6c 2d 3e 70 45 78 70 72 20 pCol->pExpr
21dc8 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 = pExpr;.
21dc9 20 20 20 20 20 20 20 69 66 28 20 70 41 67 67 49 if( pAggI
21dca 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b nfo->pGroupBy ){
21dcb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
21dcc 20 69 6e 74 20 6a 2c 20 6e 3b 0a 20 20 20 20 20 int j, n;.
21dcd 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 4c ExprL
21dce 69 73 74 20 2a 70 47 42 20 3d 20 70 41 67 67 49 ist *pGB = pAggI
21dcf 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 nfo->pGroupBy;.
21dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 s
21dd1 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
21dd2 74 65 6d 20 2a 70 54 65 72 6d 20 3d 20 70 47 42 tem *pTerm = pGB
21dd3 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ->a;.
21dd4 20 20 20 20 20 6e 20 3d 20 70 47 42 2d 3e 6e 45 n = pGB->nE
21dd5 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 xpr;.
21dd6 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c for(j=0; j<
21dd7 6e 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 n; j++, pTerm++)
21dd8 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
21dd9 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 Expr *pE = p
21dda 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 Term->pExpr;.
21ddb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
21ddc 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f f( pE->op==TK_CO
21ddd 4c 55 4d 4e 20 26 26 20 70 45 2d 3e 69 54 61 62 LUMN && pE->iTab
21dde 6c 65 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c le==pExpr->iTabl
21ddf 65 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 e &&.
21de0 20 20 20 20 20 20 20 20 20 20 20 70 45 2d 3e 69 pE->i
21de1 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e 69 Column==pExpr->i
21de2 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 Column ){.
21de3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 pC
21de4 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d ol->iSorterColum
21de5 6e 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 n = j;.
21de6 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b break
21de7 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
21de8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
21de9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
21dea 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
21deb 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e if( pCol->
21dec 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3c 30 20 iSorterColumn<0
21ded 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
21dee 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 pCol->iSorter
21def 43 6f 6c 75 6d 6e 20 3d 20 70 41 67 67 49 6e 66 Column = pAggInf
21df0 6f 2d 3e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d o->nSortingColum
21df1 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 n++;.
21df2 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 }.
21df3 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f }. /
21df4 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 77 20 61 * There is now a
21df5 6e 20 65 6e 74 72 79 20 66 6f 72 20 70 45 78 70 n entry for pExp
21df6 72 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 r in pAggInfo->a
21df7 43 6f 6c 5b 5d 20 28 65 69 74 68 65 72 0a 20 20 Col[] (either.
21df8 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 63 ** bec
21df9 61 75 73 65 20 69 74 20 77 61 73 20 74 68 65 72 ause it was ther
21dfa 65 20 62 65 66 6f 72 65 20 6f 72 20 62 65 63 61 e before or beca
21dfb 75 73 65 20 77 65 20 6a 75 73 74 20 63 72 65 61 use we just crea
21dfc 74 65 64 20 69 74 29 2e 0a 20 20 20 20 20 20 20 ted it)..
21dfd 20 20 20 20 20 2a 2a 20 43 6f 6e 76 65 72 74 20 ** Convert
21dfe 74 68 65 20 70 45 78 70 72 20 74 6f 20 62 65 20 the pExpr to be
21dff 61 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 a TK_AGG_COLUMN
21e00 72 65 66 65 72 72 69 6e 67 20 74 6f 20 74 68 61 referring to tha
21e01 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a t. **
21e02 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b pAggInfo->aCol[
21e03 5d 20 65 6e 74 72 79 2e 0a 20 20 20 20 20 20 20 ] entry..
21e04 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
21e05 20 20 20 20 45 78 70 72 53 65 74 49 72 72 65 64 ExprSetIrred
21e06 75 63 69 62 6c 65 28 70 45 78 70 72 29 3b 0a 20 ucible(pExpr);.
21e07 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 pExpr
21e08 2d 3e 70 41 67 67 49 6e 66 6f 20 3d 20 70 41 67 ->pAggInfo = pAg
21e09 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 20 gInfo;.
21e0a 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 pExpr->op = T
21e0b 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 K_AGG_COLUMN;.
21e0c 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
21e0d 3e 69 41 67 67 20 3d 20 28 69 31 36 29 6b 3b 0a >iAgg = (i16)k;.
21e0e 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 brea
21e0f 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 20 2f k;. } /
21e10 2a 20 65 6e 64 69 66 20 70 45 78 70 72 2d 3e 69 * endif pExpr->i
21e11 54 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 Table==pItem->iC
21e12 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 ursor */.
21e13 20 7d 20 2f 2a 20 65 6e 64 20 6c 6f 6f 70 20 6f } /* end loop o
21e14 76 65 72 20 70 53 72 63 4c 69 73 74 20 2a 2f 0a ver pSrcList */.
21e15 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
21e16 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a turn WRC_Prune;.
21e17 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
21e18 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20 K_AGG_FUNCTION:
21e19 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 {. /* The p
21e1a 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 74 65 NC->nDepth==0 te
21e1b 73 74 20 63 61 75 73 65 73 20 61 67 67 72 65 67 st causes aggreg
21e1c 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e ate functions in
21e1d 20 73 75 62 71 75 65 72 69 65 73 0a 20 20 20 20 subqueries.
21e1e 20 20 2a 2a 20 74 6f 20 62 65 20 69 67 6e 6f 72 ** to be ignor
21e1f 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 ed */. if(
21e20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 29 pNC->nDepth==0 )
21e21 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 {. /* Che
21e22 63 6b 20 74 6f 20 73 65 65 20 69 66 20 70 45 78 ck to see if pEx
21e23 70 72 20 69 73 20 61 20 64 75 70 6c 69 63 61 74 pr is a duplicat
21e24 65 20 6f 66 20 61 6e 6f 74 68 65 72 20 61 67 67 e of another agg
21e25 72 65 67 61 74 65 20 0a 20 20 20 20 20 20 20 20 regate .
21e26 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 ** function that
21e27 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 is already in t
21e28 68 65 20 70 41 67 67 49 6e 66 6f 20 73 74 72 75 he pAggInfo stru
21e29 63 74 75 72 65 0a 20 20 20 20 20 20 20 20 2a 2f cture. */
21e2a 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 . struct
21e2b 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 49 AggInfo_func *pI
21e2c 74 65 6d 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e tem = pAggInfo->
21e2d 61 46 75 6e 63 3b 0a 20 20 20 20 20 20 20 20 66 aFunc;. f
21e2e 6f 72 28 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e or(i=0; i<pAggIn
21e2f 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 fo->nFunc; i++,
21e30 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 pItem++){.
21e31 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 if( sqlite3E
21e32 78 70 72 43 6f 6d 70 61 72 65 28 70 49 74 65 6d xprCompare(pItem
21e33 2d 3e 70 45 78 70 72 2c 20 70 45 78 70 72 29 20 ->pExpr, pExpr)
21e34 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 ){. b
21e35 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 reak;.
21e36 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
21e37 20 20 20 20 69 66 28 20 69 3e 3d 70 41 67 67 49 if( i>=pAggI
21e38 6e 66 6f 2d 3e 6e 46 75 6e 63 20 29 7b 0a 20 20 nfo->nFunc ){.
21e39 20 20 20 20 20 20 20 20 2f 2a 20 70 45 78 70 72 /* pExpr
21e3a 20 69 73 20 6f 72 69 67 69 6e 61 6c 2e 20 20 4d is original. M
21e3b 61 6b 65 20 61 20 6e 65 77 20 65 6e 74 72 79 20 ake a new entry
21e3c 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 in pAggInfo->aFu
21e3d 6e 63 5b 5d 0a 20 20 20 20 20 20 20 20 20 20 2a nc[]. *
21e3e 2f 0a 20 20 20 20 20 20 20 20 20 20 75 38 20 65 /. u8 e
21e3f 6e 63 20 3d 20 45 4e 43 28 70 50 61 72 73 65 2d nc = ENC(pParse-
21e40 3e 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 >db);.
21e41 69 20 3d 20 61 64 64 41 67 67 49 6e 66 6f 46 75 i = addAggInfoFu
21e42 6e 63 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 nc(pParse->db, p
21e43 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 AggInfo);.
21e44 20 20 20 20 69 66 28 20 69 3e 3d 30 20 29 7b 0a if( i>=0 ){.
21e45 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 asse
21e46 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 rt( !ExprHasProp
21e47 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 erty(pExpr, EP_x
21e48 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 IsSelect) );.
21e49 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 20 3d pItem =
21e4a 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e &pAggInfo->aFun
21e4b 63 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 c[i];.
21e4c 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d pItem->pExpr =
21e4d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 pExpr;.
21e4e 20 20 20 20 70 49 74 65 6d 2d 3e 69 4d 65 6d 20 pItem->iMem
21e4f 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d = ++pParse->nMem
21e50 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 ;. as
21e51 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 sert( !ExprHasPr
21e52 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
21e53 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 _IntValue) );.
21e54 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d pItem-
21e55 3e 70 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 >pFunc = sqlite3
21e56 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 FindFunction(pPa
21e57 72 73 65 2d 3e 64 62 2c 0a 20 20 20 20 20 20 20 rse->db,.
21e58 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 pExp
21e59 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 73 71 6c r->u.zToken, sql
21e5a 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 45 78 ite3Strlen30(pEx
21e5b 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2c 0a 20 pr->u.zToken),.
21e5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21e5d 20 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 pExpr->x.pList
21e5e 20 3f 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 ? pExpr->x.pLis
21e5f 74 2d 3e 6e 45 78 70 72 20 3a 20 30 2c 20 65 6e t->nExpr : 0, en
21e60 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 c, 0);.
21e61 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c if( pExpr->fl
21e62 61 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 ags & EP_Distinc
21e63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 t ){.
21e64 20 20 20 70 49 74 65 6d 2d 3e 69 44 69 73 74 69 pItem->iDisti
21e65 6e 63 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 nct = pParse->nT
21e66 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 ab++;.
21e67 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
21e68 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 44 pItem->iD
21e69 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20 20 istinct = -1;.
21e6a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
21e6b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
21e6c 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b }. /* Mak
21e6d 65 20 70 45 78 70 72 20 70 6f 69 6e 74 20 74 6f e pExpr point to
21e6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 the appropriate
21e6f 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 pAggInfo->aFunc
21e70 5b 5d 20 65 6e 74 72 79 0a 20 20 20 20 20 20 20 [] entry.
21e71 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 */. asse
21e72 72 74 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 rt( !ExprHasAnyP
21e73 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
21e74 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 P_TokenOnly|EP_R
21e75 65 64 75 63 65 64 29 20 29 3b 0a 20 20 20 20 20 educed) );.
21e76 20 20 20 45 78 70 72 53 65 74 49 72 72 65 64 75 ExprSetIrredu
21e77 63 69 62 6c 65 28 70 45 78 70 72 29 3b 0a 20 20 cible(pExpr);.
21e78 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 41 67 pExpr->iAg
21e79 67 20 3d 20 28 69 31 36 29 69 3b 0a 20 20 20 20 g = (i16)i;.
21e7a 20 20 20 20 70 45 78 70 72 2d 3e 70 41 67 67 49 pExpr->pAggI
21e7b 6e 66 6f 20 3d 20 70 41 67 67 49 6e 66 6f 3b 0a nfo = pAggInfo;.
21e7c 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 return W
21e7d 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 20 20 RC_Prune;.
21e7e 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 }. }. }. re
21e7f 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 turn WRC_Continu
21e80 65 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 e;.}.static int
21e81 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 analyzeAggregate
21e82 73 49 6e 53 65 6c 65 63 74 28 57 61 6c 6b 65 72 sInSelect(Walker
21e83 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 *pWalker, Selec
21e84 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20 4e t *pSelect){. N
21e85 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 20 ameContext *pNC
21e86 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 = pWalker->u.pNC
21e87 3b 0a 20 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65 ;. if( pNC->